switch to 2.6.25.7
[librecmc/librecmc.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 unsigned int 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         unsigned int t;
76         
77         t = buflen;     
78         if (write_data) {
79                 for (; t > 0; t--, buf++)
80                         writeb(*buf, ioaddr);
81         } else {
82                 for (; t > 0; t--, buf++)
83                         *buf = readb(ioaddr);
84         }
85
86         rb153_pata_finish_io(adev->link->ap);
87         return buflen;
88 }
89
90 static void rb153_pata_freeze(struct ata_port *ap)
91 {
92         struct rb153_cf_info *info = ap->host->private_data;
93
94         info->frozen = 1;
95 }
96
97 static void rb153_pata_thaw(struct ata_port *ap)
98 {
99         struct rb153_cf_info *info = ap->host->private_data;
100
101         info->frozen = 0;
102 }
103
104 static irqreturn_t rb153_pata_irq_handler(int irq, void *dev_instance)
105 {
106         struct ata_host *ah = dev_instance;
107         struct rb153_cf_info *info = ah->private_data;
108
109         if (gpio_get_value(info->gpio_line)) {
110                 set_irq_type(info->irq, IRQ_TYPE_LEVEL_LOW);
111                 if (!info->frozen)
112                         ata_interrupt(irq, dev_instance);
113         } else {
114                 set_irq_type(info->irq, IRQ_TYPE_LEVEL_HIGH);
115         }
116
117         return IRQ_HANDLED;
118 }
119
120 static void rb153_pata_irq_clear(struct ata_port *ap)
121 {
122 }
123
124 static int rb153_pata_port_start(struct ata_port *ap)
125 {
126         return 0;
127 }
128
129 static struct ata_port_operations rb153_pata_port_ops = {
130         .tf_load                = ata_tf_load,
131         .tf_read                = ata_tf_read,
132
133         .exec_command           = rb153_pata_exec_command,
134         .check_status           = ata_check_status,
135         .dev_select             = ata_std_dev_select,
136
137         .data_xfer              = rb153_pata_data_xfer,
138
139         .qc_prep                = ata_qc_prep,
140         .qc_issue               = ata_qc_issue_prot,
141
142         .freeze                 = rb153_pata_freeze,
143         .thaw                   = rb153_pata_thaw,
144         .error_handler          = ata_bmdma_error_handler,
145         .cable_detect           = ata_cable_40wire,
146
147         .irq_handler            = rb153_pata_irq_handler,
148         .irq_clear              = rb153_pata_irq_clear,
149         .irq_on                 = ata_irq_on,
150
151         .port_start             = rb153_pata_port_start,
152 };
153
154 /* ------------------------------------------------------------------------ */
155
156 static struct scsi_host_template rb153_pata_sht = {
157         .module                 = THIS_MODULE,
158         .name                   = DRV_NAME,
159         .ioctl                  = ata_scsi_ioctl,
160         .queuecommand           = ata_scsi_queuecmd,
161         .slave_configure        = ata_scsi_slave_config,
162         .slave_destroy          = ata_scsi_slave_destroy,
163         .bios_param             = ata_std_bios_param,
164         .proc_name              = DRV_NAME,
165
166         .can_queue              = ATA_DEF_QUEUE,
167         .this_id                = ATA_SHT_THIS_ID,
168         .sg_tablesize           = LIBATA_MAX_PRD,
169         .dma_boundary           = ATA_DMA_BOUNDARY,
170         .cmd_per_lun            = ATA_SHT_CMD_PER_LUN,
171         .emulated               = ATA_SHT_EMULATED,
172         .use_clustering         = ATA_SHT_USE_CLUSTERING,
173 };
174
175 /* ------------------------------------------------------------------------ */
176
177 static void rb153_pata_setup_port(struct ata_port *ap,
178                         struct rb153_cf_info *info, unsigned long raw_cmd)
179 {
180         ap->ioaddr.cmd_addr     = info->iobase + RB153_CF_REG_CMD;
181         ap->ioaddr.ctl_addr     = info->iobase + RB153_CF_REG_CTRL;
182         ap->ioaddr.altstatus_addr = info->iobase + RB153_CF_REG_CTRL;
183
184         ata_std_ports(&ap->ioaddr);
185
186         ap->ioaddr.data_addr    = info->iobase + RB153_CF_REG_DATA;
187
188         ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", raw_cmd,
189                                 raw_cmd + RB153_CF_REG_CTRL);
190 }
191
192 static __devinit int rb153_pata_driver_probe(struct platform_device *pdev)
193 {
194         unsigned int irq;
195         int gpio;
196         struct resource *res;
197         struct ata_host *ah;
198         struct ata_port *ap;
199         struct rb153_cf_info *info;
200         int ret;
201
202         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
203         if (!res) {
204                 dev_err(&pdev->dev, "no IOMEM resource found\n");
205                 return -EINVAL;
206         }
207
208         irq = platform_get_irq(pdev, 0);
209         if (irq <= 0) {
210                 dev_err(&pdev->dev, "no IRQ resource found\n");
211                 return -ENOENT;
212         }
213
214         gpio = irq_to_gpio(irq);
215         if (gpio < 0) {
216                 dev_err(&pdev->dev, "no GPIO found for irq%d\n", irq);
217                 return -ENOENT;
218         }
219
220         ret = gpio_request(gpio, DRV_NAME);
221         if (ret) {
222                 dev_err(&pdev->dev, "GPIO request failed\n");
223                 return ret;
224         }
225
226         /* allocate host */
227         ah = ata_host_alloc(&pdev->dev, RB153_CF_MAXPORTS);
228         if (!ah)
229                 return -ENOMEM;
230
231         platform_set_drvdata(pdev, ah);
232
233         info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
234         if (!info)
235                 return -ENOMEM;
236
237         ah->private_data = info;
238         info->gpio_line = gpio;
239         info->irq = irq;
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         ap = ah->ports[0];
254
255         ap->ops         = &rb153_pata_port_ops;
256         ap->pio_mask    = 0x1f; /* PIO4 */
257         ap->flags       = ATA_FLAG_NO_LEGACY | ATA_FLAG_MMIO;
258
259         rb153_pata_setup_port(ap, info, res->start);
260
261         ret = ata_host_activate(ah, irq, rb153_pata_irq_handler,
262                                 IRQF_TRIGGER_LOW, &rb153_pata_sht);
263         if (ret)
264                 goto err_free_gpio;
265
266         return 0;
267
268 err_free_gpio:
269         gpio_free(gpio);
270
271         return ret;
272 }
273
274 static __devexit int rb153_pata_driver_remove(struct platform_device *pdev)
275 {
276         struct ata_host *ah = platform_get_drvdata(pdev);
277         struct rb153_cf_info *info = ah->private_data;
278
279         ata_host_detach(ah);
280         gpio_free(info->gpio_line);
281
282         return 0;
283 }
284
285 static struct platform_driver rb153_pata_platform_driver = {
286         .probe          = rb153_pata_driver_probe,
287         .remove         = __devexit_p(rb153_pata_driver_remove),
288         .driver  = {
289                 .name   = DRV_NAME,
290                 .owner  = THIS_MODULE,
291         },
292 };
293
294 /* ------------------------------------------------------------------------ */
295
296 #define DRV_INFO DRV_DESC " version " DRV_VERSION
297
298 static int __init rb153_pata_module_init(void)
299 {
300         printk(KERN_INFO DRV_INFO "\n");
301
302         return platform_driver_register(&rb153_pata_platform_driver);
303 }
304
305 static void __exit rb153_pata_module_exit(void)
306 {
307         platform_driver_unregister(&rb153_pata_platform_driver);
308 }
309
310 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
311 MODULE_DESCRIPTION(DRV_DESC);
312 MODULE_VERSION(DRV_VERSION);
313 MODULE_LICENSE("GPL v2");
314
315 module_init(rb153_pata_module_init);
316 module_exit(rb153_pata_module_exit);