1 --- a/drivers/ata/Kconfig
2 +++ b/drivers/ata/Kconfig
3 @@ -144,6 +144,16 @@ config PDC_ADMA
7 +config PATA_MAGICBOX_CF
8 + tristate "Magicbox/OpenRB Compact Flash support"
9 + depends on MAGICBOX || OPENRB
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.
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 @@ -81,6 +81,7 @@ obj-$(CONFIG_PATA_AT91) += pata_at91.o
23 obj-$(CONFIG_PATA_CMD640_PCI) += pata_cmd640.o
24 obj-$(CONFIG_PATA_ISAPNP) += pata_isapnp.o
25 obj-$(CONFIG_PATA_IXP4XX_CF) += pata_ixp4xx_cf.o
26 +obj-$(CONFIG_PATA_MAGICBOX_CF) += pata_magicbox_cf.o
27 obj-$(CONFIG_PATA_MPIIX) += pata_mpiix.o
28 obj-$(CONFIG_PATA_NS87410) += pata_ns87410.o
29 obj-$(CONFIG_PATA_OPTI) += pata_opti.o
31 +++ b/drivers/ata/pata_magicbox_cf.c
34 + * PATA/CompactFlash driver for the MagicBox v2/OpenRB boards.
36 + * Copyright (C) 2009,2012 Gabor Juhos <juhosg@openwrt.org>
38 + * Based on the IDE driver by Wojtek Kaniewski <wojtekka@toxygen.net>
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.
45 +#include <linux/kernel.h>
46 +#include <linux/module.h>
47 +#include <linux/slab.h>
48 +#include <linux/types.h>
49 +#include <linux/ioport.h>
50 +#include <linux/libata.h>
51 +#include <linux/irq.h>
52 +//#include <linux/of.h>
53 +//#include <linux/of_device.h>
54 +#include <linux/of_platform.h>
55 +#include <scsi/scsi_host.h>
57 +#define DRV_DESC "PATA/CompactFlash driver for Magicbox/OpenRB boards"
58 +#define DRV_NAME "pata_magicbox_cf"
59 +#define DRV_VERSION "0.1.0"
61 +#define MAGICBOX_CF_REG_CMD (2 * ATA_REG_CMD)
62 +#define MAGICBOX_CF_REG_DATA (2 * ATA_REG_DATA)
63 +#define MAGICBOX_CF_REG_ERR (2 * ATA_REG_ERR)
64 +#define MAGICBOX_CF_REG_FEATURE (2 * ATA_REG_FEATURE)
65 +#define MAGICBOX_CF_REG_NSECT (2 * ATA_REG_NSECT)
66 +#define MAGICBOX_CF_REG_LBAL (2 * ATA_REG_LBAL)
67 +#define MAGICBOX_CF_REG_LBAM (2 * ATA_REG_LBAM)
68 +#define MAGICBOX_CF_REG_LBAH (2 * ATA_REG_LBAH)
69 +#define MAGICBOX_CF_REG_DEVICE (2 * ATA_REG_DEVICE)
70 +#define MAGICBOX_CF_REG_STATUS (2 * ATA_REG_STATUS)
71 +#define MAGICBOX_CF_REG_ALTSTATUS (2 * 6)
72 +#define MAGICBOX_CF_REG_CTL (2 * 6)
74 +#define MAGICBOX_CF_MAXPORTS 1
76 +struct magicbox_cf_info {
81 +static inline u8 magicbox_cf_inb(void __iomem *port)
83 + return (u8) (readw(port) >> 8) & 0xff;
86 +static inline void magicbox_cf_outb(void __iomem *port, u8 value)
88 + writew(value << 8, port);
91 +static int magicbox_cf_set_mode(struct ata_link *link,
92 + struct ata_device **error)
94 + struct ata_device *dev;
96 + ata_for_each_dev(dev, link, ENABLED) {
97 + ata_dev_printk(dev, KERN_INFO, "configured for PIO0\n");
98 + dev->pio_mode = XFER_PIO_0;
99 + dev->xfer_mode = XFER_PIO_0;
100 + dev->xfer_shift = ATA_SHIFT_PIO;
101 + dev->flags |= ATA_DFLAG_PIO;
107 +static void magicbox_cf_exec_command(struct ata_port *ap,
108 + const struct ata_taskfile *tf)
110 + DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
112 + magicbox_cf_outb(ap->ioaddr.command_addr, tf->command);
116 +static u8 magicbox_cf_check_status(struct ata_port *ap)
120 + status = magicbox_cf_inb(ap->ioaddr.status_addr);
122 + DPRINTK("ata%u: status 0x%X, from %p\n", ap->print_id, status,
123 + ap->ioaddr.status_addr);
128 +static u8 magicbox_cf_check_altstatus(struct ata_port *ap)
132 + altstatus = magicbox_cf_inb(ap->ioaddr.altstatus_addr);
134 + DPRINTK("ata%u: altstatus 0x%X, from %p\n", ap->print_id,
135 + altstatus, ap->ioaddr.altstatus_addr);
140 +static void magicbox_cf_dev_select(struct ata_port *ap, unsigned int device)
142 + /* Nothing to do. We are supporting one device only. */
145 +static void magicbox_cf_tf_load(struct ata_port *ap,
146 + const struct ata_taskfile *tf)
148 + struct ata_ioports *ioaddr = &ap->ioaddr;
149 + unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
151 + if (tf->ctl != ap->last_ctl) {
152 + magicbox_cf_outb(ioaddr->ctl_addr, tf->ctl);
153 + ap->last_ctl = tf->ctl;
157 + if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
158 + magicbox_cf_outb(ioaddr->feature_addr, tf->hob_feature);
159 + magicbox_cf_outb(ioaddr->nsect_addr, tf->hob_nsect);
160 + magicbox_cf_outb(ioaddr->lbal_addr, tf->hob_lbal);
161 + magicbox_cf_outb(ioaddr->lbam_addr, tf->hob_lbam);
162 + magicbox_cf_outb(ioaddr->lbah_addr, tf->hob_lbah);
163 + VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
172 + magicbox_cf_outb(ioaddr->feature_addr, tf->feature);
173 + magicbox_cf_outb(ioaddr->nsect_addr, tf->nsect);
174 + magicbox_cf_outb(ioaddr->lbal_addr, tf->lbal);
175 + magicbox_cf_outb(ioaddr->lbam_addr, tf->lbam);
176 + magicbox_cf_outb(ioaddr->lbah_addr, tf->lbah);
177 + VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
185 + if (tf->flags & ATA_TFLAG_DEVICE) {
186 + magicbox_cf_outb(ioaddr->device_addr, tf->device);
187 + VPRINTK("device 0x%X\n", tf->device);
193 +static void magicbox_cf_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
195 + struct ata_ioports *ioaddr = &ap->ioaddr;
197 + tf->command = magicbox_cf_inb(ap->ioaddr.status_addr);
198 + tf->feature = magicbox_cf_inb(ioaddr->error_addr);
199 + tf->nsect = magicbox_cf_inb(ioaddr->nsect_addr);
200 + tf->lbal = magicbox_cf_inb(ioaddr->lbal_addr);
201 + tf->lbam = magicbox_cf_inb(ioaddr->lbam_addr);
202 + tf->lbah = magicbox_cf_inb(ioaddr->lbah_addr);
203 + tf->device = magicbox_cf_inb(ioaddr->device_addr);
204 + VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
211 + if (tf->flags & ATA_TFLAG_LBA48) {
212 + magicbox_cf_outb(ioaddr->ctl_addr, tf->ctl | ATA_HOB);
213 + tf->hob_feature = magicbox_cf_inb(ioaddr->error_addr);
214 + tf->hob_nsect = magicbox_cf_inb(ioaddr->nsect_addr);
215 + tf->hob_lbal = magicbox_cf_inb(ioaddr->lbal_addr);
216 + tf->hob_lbam = magicbox_cf_inb(ioaddr->lbam_addr);
217 + tf->hob_lbah = magicbox_cf_inb(ioaddr->lbah_addr);
218 + magicbox_cf_outb(ioaddr->ctl_addr, tf->ctl);
219 + ap->last_ctl = tf->ctl;
220 + VPRINTK("hob: feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
229 +static unsigned int magicbox_cf_data_xfer(struct ata_device *dev,
230 + unsigned char *buf,
231 + unsigned int buflen, int rw)
233 + struct ata_port *ap = dev->link->ap;
234 + unsigned int words = buflen >> 1;
236 + u16 *buf16 = (u16 *) buf;
237 + void __iomem *mmio = ap->ioaddr.data_addr;
239 + /* Transfer multiple of 2 bytes */
241 + for (i = 0; i < words; i++)
242 + buf16[i] = readw(mmio);
244 + for (i = 0; i < words; i++)
245 + writew(buf16[i], mmio);
247 + /* Transfer trailing 1 byte, if any. */
248 + if (unlikely(buflen & 0x01)) {
249 + u16 align_buf[1] = { 0 };
250 + unsigned char *trailing_buf = buf + buflen - 1;
253 + align_buf[0] = readw(mmio);
254 + memcpy(trailing_buf, align_buf, 1);
256 + memcpy(align_buf, trailing_buf, 1);
257 + writew(align_buf[0], mmio);
265 +static void magicbox_cf_irq_on(struct ata_port *ap)
267 + /* Nothing to do. */
270 +static void magicbox_cf_irq_clear(struct ata_port *ap)
272 + /* Nothing to do. */
275 +static struct ata_port_operations magicbox_cf_port_ops = {
276 + .inherits = &ata_sff_port_ops,
278 + .cable_detect = ata_cable_40wire,
279 + .set_mode = magicbox_cf_set_mode,
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,
289 + .sff_irq_on = magicbox_cf_irq_on,
290 + .sff_irq_clear = magicbox_cf_irq_clear,
292 + .port_start = ATA_OP_NULL,
295 +static struct scsi_host_template magicbox_cf_sht = {
296 + ATA_PIO_SHT(DRV_NAME),
299 +static inline void magicbox_cf_setup_port(struct ata_host *host)
301 + struct magicbox_cf_info *info = host->private_data;
302 + struct ata_port *ap;
304 + ap = host->ports[0];
306 + ap->ops = &magicbox_cf_port_ops;
307 + ap->pio_mask = ATA_PIO4;
308 + ap->flags |= ATA_FLAG_NO_ATAPI;
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;
322 + ap->ioaddr.altstatus_addr = info->ctrl + MAGICBOX_CF_REG_ALTSTATUS;
323 + ap->ioaddr.ctl_addr = info->ctrl + MAGICBOX_CF_REG_CTL;
325 + ata_port_desc(ap, "cmd 0x%p ctl 0x%p", ap->ioaddr.cmd_addr,
326 + ap->ioaddr.ctl_addr);
329 +static int __devinit magicbox_cf_of_probe(struct platform_device *op)
331 + struct magicbox_cf_info *info;
332 + struct ata_host *host;
336 + info = kzalloc(sizeof(struct magicbox_cf_info), GFP_KERNEL);
337 + if (info == NULL) {
342 + irq = irq_of_parse_and_map(op->dev.of_node, 0);
344 + dev_err(&op->dev, "invalid irq\n");
346 + goto err_free_info;
349 + info->base = of_iomap(op->dev.of_node, 0);
350 + if (info->base == NULL) {
352 + goto err_free_info;
355 + info->ctrl = of_iomap(op->dev.of_node, 1);
356 + if (info->ctrl == NULL) {
358 + goto err_unmap_base;
361 + host = ata_host_alloc(&op->dev, MAGICBOX_CF_MAXPORTS);
362 + if (host == NULL) {
364 + goto err_unmap_ctrl;
367 + host->private_data = info;
368 + magicbox_cf_setup_port(host);
370 + ret = ata_host_activate(host, irq, ata_sff_interrupt,
371 + IRQF_TRIGGER_RISING, &magicbox_cf_sht);
373 + goto err_unmap_ctrl;
375 + dev_set_drvdata(&op->dev, host);
379 + iounmap(info->ctrl);
381 + iounmap(info->base);
388 +static __devexit int magicbox_cf_of_remove(struct platform_device *op)
390 + struct ata_host *host = dev_get_drvdata(&op->dev);
391 + struct magicbox_cf_info *info = host->private_data;
393 + ata_host_detach(host);
394 + iounmap(info->ctrl);
395 + iounmap(info->base);
401 +static struct of_device_id magicbox_cf_of_match[] = {
402 + { .compatible = "pata-magicbox-cf", },
406 +static struct platform_driver magicbox_cf_of_platform_driver = {
407 + .probe = magicbox_cf_of_probe,
408 + .remove = __devexit_p(magicbox_cf_of_remove),
411 + .owner = THIS_MODULE,
412 + .of_match_table = magicbox_cf_of_match,
416 +static int __init magicbox_cf_init(void)
418 + return platform_driver_register(&magicbox_cf_of_platform_driver);
421 +static void __exit magicbox_cf_exit(void)
423 + platform_driver_unregister(&magicbox_cf_of_platform_driver);
426 +module_init(magicbox_cf_init);
427 +module_exit(magicbox_cf_exit);
429 +MODULE_DESCRIPTION(DRV_DESC);
430 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
431 +MODULE_LICENSE("GPL v2");
432 +MODULE_VERSION(DRV_VERSION);
433 +MODULE_DEVICE_TABLE(of, magicbox_cf_of_match);