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