3893587777c1343b48f22e9a50ecfb5145a53e37
[openwrt.git] / target / linux / ppc40x / patches / 101-pata-magicbox-cf-driver.patch
1 --- a/drivers/ata/Kconfig
2 +++ b/drivers/ata/Kconfig
3 @@ -745,6 +745,16 @@ config PATA_IXP4XX_CF
4  
5           If unsure, say N.
6  
7 +config PATA_MAGICBOX_CF
8 +       tristate "Magicbox/OpenRB Compact Flash support"
9 +       depends on MAGICBOX || OPENRB
10 +       help
11 +         This option enables support for a Compact Flash conected on
12 +         the ppc405ep expansion bus. This driver had been written for
13 +         the Magicbox v2 and OpenRB boards.
14 +
15 +         If unsure, say N.
16 +
17  config PATA_OCTEON_CF
18         tristate "OCTEON Boot Bus Compact Flash support"
19         depends on CPU_CAVIUM_OCTEON
20 --- a/drivers/ata/Makefile
21 +++ b/drivers/ata/Makefile
22 @@ -49,6 +49,7 @@ obj-$(CONFIG_PATA_OPTI)               += pata_opti.o
23  obj-$(CONFIG_PATA_OPTIDMA)     += pata_optidma.o
24  obj-$(CONFIG_PATA_MPC52xx)     += pata_mpc52xx.o
25  obj-$(CONFIG_PATA_MARVELL)     += pata_marvell.o
26 +obj-$(CONFIG_PATA_MAGICBOX_CF) += pata_magicbox_cf.o
27  obj-$(CONFIG_PATA_MPIIX)       += pata_mpiix.o
28  obj-$(CONFIG_PATA_OLDPIIX)     += pata_oldpiix.o
29  obj-$(CONFIG_PATA_PALMLD)      += pata_palmld.o
30 --- /dev/null
31 +++ b/drivers/ata/pata_magicbox_cf.c
32 @@ -0,0 +1,404 @@
33 +/*
34 + *  PATA/CompactFlash driver for the MagicBox v2/OpenRB boards.
35 + *
36 + *  Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
37 + *
38 + *  Based on the IDE driver by Wojtek Kaniewski <wojtekka@toxygen.net>
39 + *
40 + *  This program is free software; you can redistribute it and/or modify it
41 + *  under the terms of the GNU General Public License version 2 as published
42 + *  by the Free Software Foundation.
43 + */
44 +
45 +#include <linux/kernel.h>
46 +#include <linux/module.h>
47 +#include <linux/types.h>
48 +#include <linux/ioport.h>
49 +#include <linux/libata.h>
50 +#include <linux/irq.h>
51 +#include <linux/of.h>
52 +#include <linux/of_device.h>
53 +#include <linux/of_platform.h>
54 +#include <scsi/scsi_host.h>
55 +
56 +#define DRV_DESC       "PATA/CompactFlash driver for Magicbox/OpenRB boards"
57 +#define DRV_NAME       "pata_magicbox_cf"
58 +#define DRV_VERSION    "0.1.0"
59 +
60 +#define MAGICBOX_CF_REG_CMD            (2 * ATA_REG_CMD)
61 +#define MAGICBOX_CF_REG_DATA           (2 * ATA_REG_DATA)
62 +#define MAGICBOX_CF_REG_ERR            (2 * ATA_REG_ERR)
63 +#define MAGICBOX_CF_REG_FEATURE        (2 * ATA_REG_FEATURE)
64 +#define MAGICBOX_CF_REG_NSECT          (2 * ATA_REG_NSECT)
65 +#define MAGICBOX_CF_REG_LBAL           (2 * ATA_REG_LBAL)
66 +#define MAGICBOX_CF_REG_LBAM           (2 * ATA_REG_LBAM)
67 +#define MAGICBOX_CF_REG_LBAH           (2 * ATA_REG_LBAH)
68 +#define MAGICBOX_CF_REG_DEVICE         (2 * ATA_REG_DEVICE)
69 +#define MAGICBOX_CF_REG_STATUS         (2 * ATA_REG_STATUS)
70 +#define MAGICBOX_CF_REG_ALTSTATUS      (2 * 6)
71 +#define MAGICBOX_CF_REG_CTL            (2 * 6)
72 +
73 +#define MAGICBOX_CF_MAXPORTS   1
74 +
75 +struct magicbox_cf_info {
76 +       void __iomem    *base;
77 +       void __iomem    *ctrl;
78 +};
79 +
80 +static inline u8 magicbox_cf_inb(void __iomem *port)
81 +{
82 +       return (u8) (readw(port) >> 8) & 0xff;
83 +}
84 +
85 +static inline void magicbox_cf_outb(void __iomem *port, u8 value)
86 +{
87 +       writew(value << 8, port);
88 +}
89 +
90 +static int magicbox_cf_set_mode(struct ata_link *link,
91 +                               struct ata_device **error)
92 +{
93 +       struct ata_device *dev;
94 +
95 +       ata_for_each_dev(dev, link, ENABLED) {
96 +               ata_dev_printk(dev, KERN_INFO, "configured for PIO0\n");
97 +               dev->pio_mode = XFER_PIO_0;
98 +               dev->xfer_mode = XFER_PIO_0;
99 +               dev->xfer_shift = ATA_SHIFT_PIO;
100 +               dev->flags |= ATA_DFLAG_PIO;
101 +       }
102 +
103 +       return 0;
104 +}
105 +
106 +static void magicbox_cf_exec_command(struct ata_port *ap,
107 +                                    const struct ata_taskfile *tf)
108 +{
109 +       DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
110 +
111 +       magicbox_cf_outb(ap->ioaddr.command_addr, tf->command);
112 +       ata_sff_pause(ap);
113 +}
114 +
115 +static u8 magicbox_cf_check_status(struct ata_port *ap)
116 +{
117 +       u8 status;
118 +
119 +       status = magicbox_cf_inb(ap->ioaddr.status_addr);
120 +
121 +       DPRINTK("ata%u: status 0x%X, from %p\n", ap->print_id, status,
122 +               ap->ioaddr.status_addr);
123 +
124 +       return status;
125 +}
126 +
127 +static u8 magicbox_cf_check_altstatus(struct ata_port *ap)
128 +{
129 +       u8 altstatus;
130 +
131 +       altstatus = magicbox_cf_inb(ap->ioaddr.altstatus_addr);
132 +
133 +       DPRINTK("ata%u: altstatus 0x%X, from %p\n", ap->print_id,
134 +               altstatus, ap->ioaddr.altstatus_addr);
135 +
136 +       return altstatus;
137 +}
138 +
139 +static void magicbox_cf_dev_select(struct ata_port *ap, unsigned int device)
140 +{
141 +       /* Nothing to do. We are supporting one device only. */
142 +}
143 +
144 +static void magicbox_cf_tf_load(struct ata_port *ap,
145 +                               const struct ata_taskfile *tf)
146 +{
147 +       struct ata_ioports *ioaddr = &ap->ioaddr;
148 +       unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
149 +
150 +       if (tf->ctl != ap->last_ctl) {
151 +               magicbox_cf_outb(ioaddr->ctl_addr, tf->ctl);
152 +               ap->last_ctl = tf->ctl;
153 +               ata_wait_idle(ap);
154 +       }
155 +
156 +       if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
157 +               magicbox_cf_outb(ioaddr->feature_addr, tf->hob_feature);
158 +               magicbox_cf_outb(ioaddr->nsect_addr, tf->hob_nsect);
159 +               magicbox_cf_outb(ioaddr->lbal_addr, tf->hob_lbal);
160 +               magicbox_cf_outb(ioaddr->lbam_addr, tf->hob_lbam);
161 +               magicbox_cf_outb(ioaddr->lbah_addr, tf->hob_lbah);
162 +               VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
163 +                       tf->hob_feature,
164 +                       tf->hob_nsect,
165 +                       tf->hob_lbal,
166 +                       tf->hob_lbam,
167 +                       tf->hob_lbah);
168 +       }
169 +
170 +       if (is_addr) {
171 +               magicbox_cf_outb(ioaddr->feature_addr, tf->feature);
172 +               magicbox_cf_outb(ioaddr->nsect_addr, tf->nsect);
173 +               magicbox_cf_outb(ioaddr->lbal_addr, tf->lbal);
174 +               magicbox_cf_outb(ioaddr->lbam_addr, tf->lbam);
175 +               magicbox_cf_outb(ioaddr->lbah_addr, tf->lbah);
176 +               VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
177 +                       tf->feature,
178 +                       tf->nsect,
179 +                       tf->lbal,
180 +                       tf->lbam,
181 +                       tf->lbah);
182 +       }
183 +
184 +       if (tf->flags & ATA_TFLAG_DEVICE) {
185 +               magicbox_cf_outb(ioaddr->device_addr, tf->device);
186 +               VPRINTK("device 0x%X\n", tf->device);
187 +       }
188 +
189 +       ata_wait_idle(ap);
190 +}
191 +
192 +static void magicbox_cf_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
193 +{
194 +       struct ata_ioports *ioaddr = &ap->ioaddr;
195 +
196 +       tf->command = magicbox_cf_inb(ap->ioaddr.status_addr);
197 +       tf->feature = magicbox_cf_inb(ioaddr->error_addr);
198 +       tf->nsect = magicbox_cf_inb(ioaddr->nsect_addr);
199 +       tf->lbal = magicbox_cf_inb(ioaddr->lbal_addr);
200 +       tf->lbam = magicbox_cf_inb(ioaddr->lbam_addr);
201 +       tf->lbah = magicbox_cf_inb(ioaddr->lbah_addr);
202 +       tf->device = magicbox_cf_inb(ioaddr->device_addr);
203 +       VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
204 +               tf->feature,
205 +               tf->nsect,
206 +               tf->lbal,
207 +               tf->lbam,
208 +               tf->lbah);
209 +
210 +       if (tf->flags & ATA_TFLAG_LBA48) {
211 +               magicbox_cf_outb(ioaddr->ctl_addr, tf->ctl | ATA_HOB);
212 +               tf->hob_feature = magicbox_cf_inb(ioaddr->error_addr);
213 +               tf->hob_nsect = magicbox_cf_inb(ioaddr->nsect_addr);
214 +               tf->hob_lbal = magicbox_cf_inb(ioaddr->lbal_addr);
215 +               tf->hob_lbam = magicbox_cf_inb(ioaddr->lbam_addr);
216 +               tf->hob_lbah = magicbox_cf_inb(ioaddr->lbah_addr);
217 +               magicbox_cf_outb(ioaddr->ctl_addr, tf->ctl);
218 +               ap->last_ctl = tf->ctl;
219 +               VPRINTK("hob: feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
220 +                       tf->feature,
221 +                       tf->nsect,
222 +                       tf->lbal,
223 +                       tf->lbam,
224 +                       tf->lbah);
225 +       }
226 +}
227 +
228 +static unsigned int magicbox_cf_data_xfer(struct ata_device *dev,
229 +                                         unsigned char *buf,
230 +                                         unsigned int buflen, int rw)
231 +{
232 +       struct ata_port *ap = dev->link->ap;
233 +       unsigned int words = buflen >> 1;
234 +       unsigned int i;
235 +       u16 *buf16 = (u16 *) buf;
236 +       void __iomem *mmio = ap->ioaddr.data_addr;
237 +
238 +       /* Transfer multiple of 2 bytes */
239 +       if (rw == READ)
240 +               for (i = 0; i < words; i++)
241 +                       buf16[i] = readw(mmio);
242 +       else
243 +               for (i = 0; i < words; i++)
244 +                       writew(buf16[i], mmio);
245 +
246 +       /* Transfer trailing 1 byte, if any. */
247 +       if (unlikely(buflen & 0x01)) {
248 +               u16 align_buf[1] = { 0 };
249 +               unsigned char *trailing_buf = buf + buflen - 1;
250 +
251 +               if (rw == READ) {
252 +                       align_buf[0] = readw(mmio);
253 +                       memcpy(trailing_buf, align_buf, 1);
254 +               } else {
255 +                       memcpy(align_buf, trailing_buf, 1);
256 +                       writew(align_buf[0], mmio);
257 +               }
258 +               words++;
259 +       }
260 +
261 +       return words << 1;
262 +}
263 +
264 +static u8 magicbox_cf_irq_on(struct ata_port *ap)
265 +{
266 +       /* Nothing to do. */
267 +       return 0;
268 +}
269 +
270 +static void magicbox_cf_irq_clear(struct ata_port *ap)
271 +{
272 +       /* Nothing to do. */
273 +}
274 +
275 +static struct ata_port_operations magicbox_cf_port_ops = {
276 +       .inherits               = &ata_sff_port_ops,
277 +
278 +       .cable_detect           = ata_cable_40wire,
279 +       .set_mode               = magicbox_cf_set_mode,
280 +
281 +       .sff_exec_command       = magicbox_cf_exec_command,
282 +       .sff_check_status       = magicbox_cf_check_status,
283 +       .sff_check_altstatus    = magicbox_cf_check_altstatus,
284 +       .sff_dev_select         = magicbox_cf_dev_select,
285 +       .sff_tf_load            = magicbox_cf_tf_load,
286 +       .sff_tf_read            = magicbox_cf_tf_read,
287 +       .sff_data_xfer          = magicbox_cf_data_xfer,
288 +
289 +       .sff_irq_on             = magicbox_cf_irq_on,
290 +       .sff_irq_clear          = magicbox_cf_irq_clear,
291 +
292 +       .port_start             = ATA_OP_NULL,
293 +};
294 +
295 +static struct scsi_host_template magicbox_cf_sht = {
296 +       ATA_PIO_SHT(DRV_NAME),
297 +};
298 +
299 +static inline void magicbox_cf_setup_port(struct ata_host *host)
300 +{
301 +       struct magicbox_cf_info *info = host->private_data;
302 +       struct ata_port *ap;
303 +
304 +       ap = host->ports[0];
305 +
306 +       ap->ops = &magicbox_cf_port_ops;
307 +       ap->pio_mask = ATA_PIO4;
308 +       ap->flags |= ATA_FLAG_MMIO | ATA_FLAG_NO_LEGACY | ATA_FLAG_NO_ATAPI;
309 +
310 +       ap->ioaddr.cmd_addr = info->base + MAGICBOX_CF_REG_CMD;
311 +       ap->ioaddr.data_addr = info->base + MAGICBOX_CF_REG_DATA;
312 +       ap->ioaddr.error_addr = info->base + MAGICBOX_CF_REG_ERR;
313 +       ap->ioaddr.feature_addr = info->base + MAGICBOX_CF_REG_FEATURE;
314 +       ap->ioaddr.nsect_addr = info->base + MAGICBOX_CF_REG_NSECT;
315 +       ap->ioaddr.lbal_addr = info->base + MAGICBOX_CF_REG_LBAL;
316 +       ap->ioaddr.lbam_addr = info->base + MAGICBOX_CF_REG_LBAM;
317 +       ap->ioaddr.lbah_addr = info->base + MAGICBOX_CF_REG_LBAH;
318 +       ap->ioaddr.device_addr = info->base + MAGICBOX_CF_REG_DEVICE;
319 +       ap->ioaddr.status_addr = info->base + MAGICBOX_CF_REG_STATUS;
320 +       ap->ioaddr.command_addr = info->base + MAGICBOX_CF_REG_CMD;
321 +
322 +       ap->ioaddr.altstatus_addr = info->ctrl + MAGICBOX_CF_REG_ALTSTATUS;
323 +       ap->ioaddr.ctl_addr = info->ctrl + MAGICBOX_CF_REG_CTL;
324 +
325 +       ata_port_desc(ap, "cmd 0x%p ctl 0x%p", ap->ioaddr.cmd_addr,
326 +                     ap->ioaddr.ctl_addr);
327 +}
328 +
329 +static int __devinit magicbox_cf_of_probe(struct of_device *op,
330 +                                          const struct of_device_id *match)
331 +{
332 +       struct magicbox_cf_info *info;
333 +       struct ata_host *host;
334 +       int irq;
335 +       int ret = 0;
336 +
337 +       info = kzalloc(sizeof(struct magicbox_cf_info), GFP_KERNEL);
338 +       if (info == NULL) {
339 +               ret = -ENOMEM;
340 +               goto err_exit;
341 +       }
342 +
343 +       irq = irq_of_parse_and_map(op->node, 0);
344 +       if (irq < 0) {
345 +               dev_err(&op->dev, "invalid irq\n");
346 +               ret = -EINVAL;
347 +               goto err_free_info;
348 +       }
349 +
350 +       info->base = of_iomap(op->node, 0);
351 +       if (info->base == NULL) {
352 +               ret = -ENOMEM;
353 +               goto err_free_info;
354 +       }
355 +
356 +       info->ctrl = of_iomap(op->node, 1);
357 +       if (info->ctrl == NULL) {
358 +               ret = -ENOMEM;
359 +               goto err_unmap_base;
360 +       }
361 +
362 +       host = ata_host_alloc(&op->dev, MAGICBOX_CF_MAXPORTS);
363 +       if (host == NULL) {
364 +               ret = -ENOMEM;
365 +               goto err_unmap_ctrl;
366 +       }
367 +
368 +       host->private_data = info;
369 +       magicbox_cf_setup_port(host);
370 +
371 +       ret = ata_host_activate(host, irq, ata_sff_interrupt,
372 +                               IRQF_TRIGGER_RISING, &magicbox_cf_sht);
373 +       if (ret)
374 +               goto err_unmap_ctrl;
375 +
376 +       dev_set_drvdata(&op->dev, host);
377 +       return 0;
378 +
379 + err_unmap_ctrl:
380 +       iounmap(info->ctrl);
381 + err_unmap_base:
382 +       iounmap(info->base);
383 + err_free_info:
384 +       kfree(info);
385 + err_exit:
386 +       return ret;
387 +}
388 +
389 +static __devexit int magicbox_cf_of_remove(struct of_device *op)
390 +{
391 +       struct ata_host *host = dev_get_drvdata(&op->dev);
392 +       struct magicbox_cf_info *info = host->private_data;
393 +
394 +       ata_host_detach(host);
395 +       iounmap(info->ctrl);
396 +       iounmap(info->base);
397 +       kfree(info);
398 +
399 +       return 0;
400 +}
401 +
402 +static struct of_device_id magicbox_cf_of_match[] = {
403 +       { .compatible = "pata-magicbox-cf", },
404 +       {},
405 +};
406 +
407 +static struct of_platform_driver magicbox_cf_of_platform_driver = {
408 +       .owner          = THIS_MODULE,
409 +       .name           = DRV_NAME,
410 +       .match_table    = magicbox_cf_of_match,
411 +       .probe          = magicbox_cf_of_probe,
412 +       .remove         = __devexit_p(magicbox_cf_of_remove),
413 +       .driver         = {
414 +               .name   = DRV_NAME,
415 +               .owner  = THIS_MODULE,
416 +       },
417 +};
418 +
419 +static int __init magicbox_cf_init(void)
420 +{
421 +       return of_register_platform_driver(&magicbox_cf_of_platform_driver);
422 +}
423 +
424 +static void __exit magicbox_cf_exit(void)
425 +{
426 +       of_unregister_platform_driver(&magicbox_cf_of_platform_driver);
427 +}
428 +
429 +module_init(magicbox_cf_init);
430 +module_exit(magicbox_cf_exit);
431 +
432 +MODULE_DESCRIPTION(DRV_DESC);
433 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
434 +MODULE_LICENSE("GPL v2");
435 +MODULE_VERSION(DRV_VERSION);
436 +MODULE_DEVICE_TABLE(of, magicbox_cf_of_match);