7dd697721b192fd6ee4e5b0241f4f71f9b57ea66
[15.05/openwrt.git] / target / linux / adm5120 / files / drivers / ata / pata_rb153_cf.c
1 /*
2  *  A low-level PATA driver to handle a Compact Flash connected on the
3  *  Mikrotik's RouterBoard 153 board.
4  *
5  *  Copyright (C) 2007,2008 Gabor Juhos <juhosg at openwrt.org>
6  *
7  *  This file was based on: drivers/ata/pata_ixp4xx_cf.c
8  *      Copyright (C) 2006-07 Tower Technologies
9  *      Author: Alessandro Zummo <a.zummo@towertech.it>
10  *
11  *  Also was based on the driver for Linux 2.4.xx published by Mikrotik for
12  *  their RouterBoard 1xx and 5xx series devices. The original Mikrotik code
13  *  seems not to have a license.
14  *
15  *  This program is free software; you can redistribute it and/or modify it
16  *  under the terms of the GNU General Public License version 2 as published
17  *  by the Free Software Foundation.
18  *
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24
25 #include <linux/io.h>
26 #include <linux/interrupt.h>
27 #include <linux/irq.h>
28
29 #include <linux/libata.h>
30 #include <scsi/scsi_host.h>
31
32 #include <asm/gpio.h>
33
34 #define DRV_NAME        "pata-rb153-cf"
35 #define DRV_VERSION     "0.4.0"
36 #define DRV_DESC        "PATA driver for RouterBOARD 153 Compact Flash"
37
38 #define RB153_CF_MAXPORTS       1
39 #define RB153_CF_IO_DELAY       100
40
41 #define RB153_CF_REG_CMD        0x0800
42 #define RB153_CF_REG_CTRL       0x080E
43 #define RB153_CF_REG_DATA       0x0C00
44
45 struct rb153_cf_info {
46         unsigned int    irq;
47         void __iomem    *iobase;
48         unsigned int    gpio_line;
49         int             frozen;
50 };
51
52 /* ------------------------------------------------------------------------ */
53
54 static inline void rb153_pata_finish_io(struct ata_port *ap)
55 {
56         struct rb153_cf_info *info = ap->host->private_data;
57
58         ata_altstatus(ap);
59         ndelay(RB153_CF_IO_DELAY);
60
61         set_irq_type(info->irq, IRQ_TYPE_LEVEL_HIGH);
62 }
63
64 static void rb153_pata_exec_command(struct ata_port *ap,
65                                 const struct ata_taskfile *tf)
66 {
67         writeb(tf->command, ap->ioaddr.command_addr);
68         rb153_pata_finish_io(ap);
69 }
70
71 static void rb153_pata_data_xfer(struct ata_device *adev, unsigned char *buf,
72                                 unsigned int buflen, int write_data)
73 {
74         void __iomem *ioaddr = adev->link->ap->ioaddr.data_addr;
75
76         if (write_data) {
77                 for (; buflen > 0; buflen--, buf++)
78                         writeb(*buf, ioaddr);
79         } else {
80                 for (; buflen > 0; buflen--, buf++)
81                         *buf = readb(ioaddr);
82         }
83
84         rb153_pata_finish_io(adev->link->ap);
85 }
86
87 static void rb153_pata_freeze(struct ata_port *ap)
88 {
89         struct rb153_cf_info *info = ap->host->private_data;
90
91         info->frozen = 1;
92 }
93
94 static void rb153_pata_thaw(struct ata_port *ap)
95 {
96         struct rb153_cf_info *info = ap->host->private_data;
97
98         info->frozen = 0;
99 }
100
101 static irqreturn_t rb153_pata_irq_handler(int irq, void *dev_instance)
102 {
103         struct ata_host *ah = dev_instance;
104         struct rb153_cf_info *info = ah->private_data;
105
106         if (gpio_get_value(info->gpio_line)) {
107                 set_irq_type(info->irq, IRQ_TYPE_LEVEL_LOW);
108                 if (!info->frozen)
109                         ata_interrupt(irq, dev_instance);
110         } else {
111                 set_irq_type(info->irq, IRQ_TYPE_LEVEL_HIGH);
112         }
113
114         return IRQ_HANDLED;
115 }
116
117 static void rb153_pata_irq_clear(struct ata_port *ap)
118 {
119 }
120
121 static int rb153_pata_port_start(struct ata_port *ap)
122 {
123         return 0;
124 }
125
126 static struct ata_port_operations rb153_pata_port_ops = {
127         .tf_load                = ata_tf_load,
128         .tf_read                = ata_tf_read,
129
130         .exec_command           = rb153_pata_exec_command,
131         .check_status           = ata_check_status,
132         .dev_select             = ata_std_dev_select,
133
134         .data_xfer              = rb153_pata_data_xfer,
135
136         .qc_prep                = ata_qc_prep,
137         .qc_issue               = ata_qc_issue_prot,
138
139         .freeze                 = rb153_pata_freeze,
140         .thaw                   = rb153_pata_thaw,
141         .error_handler          = ata_bmdma_error_handler,
142         .cable_detect           = ata_cable_40wire,
143
144         .irq_handler            = rb153_pata_irq_handler,
145         .irq_clear              = rb153_pata_irq_clear,
146         .irq_on                 = ata_irq_on,
147
148         .port_start             = rb153_pata_port_start,
149 };
150
151 /* ------------------------------------------------------------------------ */
152
153 static struct scsi_host_template rb153_pata_sht = {
154         .module                 = THIS_MODULE,
155         .name                   = DRV_NAME,
156         .ioctl                  = ata_scsi_ioctl,
157         .queuecommand           = ata_scsi_queuecmd,
158         .slave_configure        = ata_scsi_slave_config,
159         .slave_destroy          = ata_scsi_slave_destroy,
160         .bios_param             = ata_std_bios_param,
161         .proc_name              = DRV_NAME,
162
163         .can_queue              = ATA_DEF_QUEUE,
164         .this_id                = ATA_SHT_THIS_ID,
165         .sg_tablesize           = LIBATA_MAX_PRD,
166         .dma_boundary           = ATA_DMA_BOUNDARY,
167         .cmd_per_lun            = ATA_SHT_CMD_PER_LUN,
168         .emulated               = ATA_SHT_EMULATED,
169         .use_clustering         = ATA_SHT_USE_CLUSTERING,
170 };
171
172 /* ------------------------------------------------------------------------ */
173
174 static void rb153_pata_setup_port(struct ata_port *ap,
175                         struct rb153_cf_info *info, unsigned long raw_cmd)
176 {
177         ap->ioaddr.cmd_addr     = info->iobase + RB153_CF_REG_CMD;
178         ap->ioaddr.ctl_addr     = info->iobase + RB153_CF_REG_CTRL;
179         ap->ioaddr.altstatus_addr = info->iobase + RB153_CF_REG_CTRL;
180
181         ata_std_ports(&ap->ioaddr);
182
183         ap->ioaddr.data_addr    = info->iobase + RB153_CF_REG_DATA;
184
185         ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", raw_cmd,
186                                 raw_cmd + RB153_CF_REG_CTRL);
187 }
188
189 static __devinit int rb153_pata_driver_probe(struct platform_device *pdev)
190 {
191         unsigned int irq;
192         int gpio;
193         struct resource *res;
194         struct ata_host *ah;
195         struct ata_port *ap;
196         struct rb153_cf_info *info;
197         int ret;
198
199         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200         if (!res) {
201                 dev_err(&pdev->dev, "no IOMEM resource found\n");
202                 return -EINVAL;
203         }
204
205         irq = platform_get_irq(pdev, 0);
206         if (irq <= 0) {
207                 dev_err(&pdev->dev, "no IRQ resource found\n");
208                 return -ENOENT;
209         }
210
211         gpio = irq_to_gpio(irq);
212         if (gpio < 0) {
213                 dev_err(&pdev->dev, "no GPIO found for irq%d\n", irq);
214                 return -ENOENT;
215         }
216
217         ret = gpio_request(gpio, DRV_NAME);
218         if (ret) {
219                 dev_err(&pdev->dev, "GPIO request failed\n");
220                 return ret;
221         }
222
223         /* allocate host */
224         ah = ata_host_alloc(&pdev->dev, RB153_CF_MAXPORTS);
225         if (!ah)
226                 return -ENOMEM;
227
228         platform_set_drvdata(pdev, ah);
229
230         info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
231         if (!info)
232                 return -ENOMEM;
233
234         ah->private_data = info;
235         info->gpio_line = gpio;
236         info->irq = irq;
237
238         info->iobase = devm_ioremap_nocache(&pdev->dev, res->start,
239                                 res->end - res->start + 1);
240         if (!info->iobase)
241                 return -ENOMEM;
242
243         ret = gpio_direction_input(gpio);
244         if (ret) {
245                 dev_err(&pdev->dev, "unable to set GPIO direction, err=%d\n",
246                                 ret);
247                 goto err_free_gpio;
248         }
249
250         ap = ah->ports[0];
251
252         ap->ops         = &rb153_pata_port_ops;
253         ap->pio_mask    = 0x1f; /* PIO4 */
254         ap->flags       = ATA_FLAG_NO_LEGACY | ATA_FLAG_MMIO;
255
256         rb153_pata_setup_port(ap, info, res->start);
257
258         ret = ata_host_activate(ah, irq, rb153_pata_irq_handler,
259                                 IRQF_TRIGGER_LOW, &rb153_pata_sht);
260         if (ret)
261                 goto err_free_gpio;
262
263         return 0;
264
265 err_free_gpio:
266         gpio_free(gpio);
267
268         return ret;
269 }
270
271 static __devexit int rb153_pata_driver_remove(struct platform_device *pdev)
272 {
273         struct ata_host *ah = platform_get_drvdata(pdev);
274         struct rb153_cf_info *info = ah->private_data;
275
276         ata_host_detach(ah);
277         gpio_free(info->gpio_line);
278
279         return 0;
280 }
281
282 static struct platform_driver rb153_pata_platform_driver = {
283         .probe          = rb153_pata_driver_probe,
284         .remove         = __devexit_p(rb153_pata_driver_remove),
285         .driver  = {
286                 .name   = DRV_NAME,
287                 .owner  = THIS_MODULE,
288         },
289 };
290
291 /* ------------------------------------------------------------------------ */
292
293 #define DRV_INFO DRV_DESC " version " DRV_VERSION
294
295 static int __init rb153_pata_module_init(void)
296 {
297         printk(KERN_INFO DRV_INFO "\n");
298
299         return platform_driver_register(&rb153_pata_platform_driver);
300 }
301
302 static void __exit rb153_pata_module_exit(void)
303 {
304         platform_driver_unregister(&rb153_pata_platform_driver);
305 }
306
307 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
308 MODULE_DESCRIPTION(DRV_DESC);
309 MODULE_VERSION(DRV_VERSION);
310 MODULE_LICENSE("GPL v2");
311
312 module_init(rb153_pata_module_init);
313 module_exit(rb153_pata_module_exit);