fbbf313d9dcfd0cf3673d6b6dc273f2378b3b741
[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 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
16  *  it under the terms of the GNU General Public License version 2 as
17  *  published 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.2.2"
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         void __iomem    *iobase;
47         unsigned int    gpio_line;
48         int             frozen;
49 };
50
51 /* ------------------------------------------------------------------------ */
52
53 static inline void rb153_pata_finish_io(struct ata_port *ap)
54 {
55         struct ata_host *ah = ap->host;
56
57         ata_altstatus(ap);
58         ndelay(RB153_CF_IO_DELAY);
59
60         set_irq_type(ah->irq, IRQ_TYPE_LEVEL_HIGH);
61 }
62
63 static void rb153_pata_exec_command(struct ata_port *ap,
64                                 const struct ata_taskfile *tf)
65 {
66         writeb(tf->command, ap->ioaddr.command_addr);
67         rb153_pata_finish_io(ap);
68 }
69
70 static void rb153_pata_data_xfer(struct ata_device *adev, unsigned char *buf,
71                                 unsigned int buflen, int write_data)
72 {
73         void __iomem *ioaddr = adev->ap->ioaddr.data_addr;
74
75         if (write_data) {
76                 for (; buflen > 0; buflen--, buf++)
77                         writeb(*buf, ioaddr);
78         } else {
79                 for (; buflen > 0; buflen--, buf++)
80                         *buf = readb(ioaddr);
81         }
82
83         rb153_pata_finish_io(adev->ap);
84 }
85
86 static void rb153_pata_freeze(struct ata_port *ap)
87 {
88         struct rb153_cf_info *info = ap->host->private_data;
89
90         info->frozen = 1;
91 }
92
93 static void rb153_pata_thaw(struct ata_port *ap)
94 {
95         struct rb153_cf_info *info = ap->host->private_data;
96
97         info->frozen = 0;
98 }
99
100 static irqreturn_t rb153_pata_irq_handler(int irq, void *dev_instance)
101 {
102         struct ata_host *ah = dev_instance;
103         struct rb153_cf_info *info = ah->private_data;
104
105         if (gpio_get_value(info->gpio_line)) {
106                 set_irq_type(ah->irq, IRQ_TYPE_LEVEL_LOW);
107                 if (!info->frozen)
108                         ata_interrupt(irq, dev_instance);
109         } else {
110                 set_irq_type(ah->irq, IRQ_TYPE_LEVEL_HIGH);
111         }
112
113         return IRQ_HANDLED;
114 }
115
116 static void rb153_pata_irq_clear(struct ata_port *ap)
117 {
118 }
119
120 static int rb153_pata_port_start(struct ata_port *ap)
121 {
122         return 0;
123 }
124
125 static struct ata_port_operations rb153_pata_port_ops = {
126         .port_disable           = ata_port_disable,
127
128         .tf_load                = ata_tf_load,
129         .tf_read                = ata_tf_read,
130
131         .exec_command           = rb153_pata_exec_command,
132         .check_status           = ata_check_status,
133         .dev_select             = ata_std_dev_select,
134
135         .data_xfer              = rb153_pata_data_xfer,
136
137         .qc_prep                = ata_qc_prep,
138         .qc_issue               = ata_qc_issue_prot,
139
140         .freeze                 = rb153_pata_freeze,
141         .thaw                   = rb153_pata_thaw,
142         .error_handler          = ata_bmdma_error_handler,
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_ports(struct ata_host *ah)
175 {
176         struct rb153_cf_info *info = ah->private_data;
177         struct ata_port *ap;
178
179         ap = ah->ports[0];
180
181         ap->ops         = &rb153_pata_port_ops;
182         ap->pio_mask    = 0x1f; /* PIO4 */
183         ap->flags       = ATA_FLAG_NO_LEGACY | ATA_FLAG_MMIO;
184
185         ap->ioaddr.cmd_addr     = info->iobase + RB153_CF_REG_CMD;
186         ap->ioaddr.ctl_addr     = info->iobase + RB153_CF_REG_CTRL;
187         ap->ioaddr.altstatus_addr = info->iobase + RB153_CF_REG_CTRL;
188
189         ata_std_ports(&ap->ioaddr);
190
191         ap->ioaddr.data_addr    = info->iobase + RB153_CF_REG_DATA;
192 }
193
194 static __devinit int rb153_pata_driver_probe(struct platform_device *pdev)
195 {
196         unsigned int irq;
197         int gpio;
198         struct resource *res;
199         struct ata_host *ah;
200         struct rb153_cf_info *info;
201         int ret;
202
203         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
204         if (!res) {
205                 dev_err(&pdev->dev, "no IOMEM resource found\n");
206                 return -EINVAL;
207         }
208
209         irq = platform_get_irq(pdev, 0);
210         if (irq <= 0) {
211                 dev_err(&pdev->dev, "no IRQ resource found\n");
212                 return -ENOENT;
213         }
214
215         gpio = irq_to_gpio(irq);
216         if (gpio < 0) {
217                 dev_err(&pdev->dev, "no GPIO found for irq%d\n", irq);
218                 return -ENOENT;
219         }
220
221         ret = gpio_request(gpio, DRV_NAME);
222         if (ret) {
223                 dev_err(&pdev->dev, "GPIO request failed\n");
224                 return ret;
225         }
226
227         /* allocate host */
228         ah = ata_host_alloc(&pdev->dev, RB153_CF_MAXPORTS);
229         if (!ah)
230                 return -ENOMEM;
231
232         platform_set_drvdata(pdev, ah);
233
234         info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
235         if (!info)
236                 return -ENOMEM;
237
238         ah->private_data = info;
239         info->gpio_line = gpio;
240
241         info->iobase = devm_ioremap_nocache(&pdev->dev, res->start,
242                                 res->end - res->start + 1);
243         if (!info->iobase)
244                 return -ENOMEM;
245
246         ret = gpio_direction_input(gpio);
247         if (ret) {
248                 dev_err(&pdev->dev, "unable to set GPIO direction, err=%d\n",
249                                 ret);
250                 goto err_free_gpio;
251         }
252
253         rb153_pata_setup_ports(ah);
254
255         ret = ata_host_activate(ah, irq, rb153_pata_irq_handler,
256                                 IRQF_TRIGGER_LOW, &rb153_pata_sht);
257         if (ret)
258                 goto err_free_gpio;
259
260         return 0;
261
262 err_free_gpio:
263         gpio_free(gpio);
264
265         return ret;
266 }
267
268 static __devexit int rb153_pata_driver_remove(struct platform_device *pdev)
269 {
270         struct ata_host *ah = platform_get_drvdata(pdev);
271         struct rb153_cf_info *info = ah->private_data;
272
273         ata_host_detach(ah);
274         gpio_free(info->gpio_line);
275
276         return 0;
277 }
278
279 static struct platform_driver rb153_pata_platform_driver = {
280         .probe          = rb153_pata_driver_probe,
281         .remove         = __devexit_p(rb153_pata_driver_remove),
282         .driver  = {
283                 .name   = DRV_NAME,
284                 .owner  = THIS_MODULE,
285         },
286 };
287
288 /* ------------------------------------------------------------------------ */
289
290 #define DRV_INFO DRV_DESC " version " DRV_VERSION
291
292 static int __init rb153_pata_module_init(void)
293 {
294         printk(KERN_INFO DRV_INFO "\n");
295
296         return platform_driver_register(&rb153_pata_platform_driver);
297 }
298
299 static void __exit rb153_pata_module_exit(void)
300 {
301         platform_driver_unregister(&rb153_pata_platform_driver);
302 }
303
304 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
305 MODULE_DESCRIPTION(DRV_DESC);
306 MODULE_VERSION(DRV_VERSION);
307 MODULE_LICENSE("GPL");
308
309 module_init(rb153_pata_module_init);
310 module_exit(rb153_pata_module_exit);