2 * A low-level PATA driver to handle a Compact Flash connected on the
3 * Mikrotik's RouterBoard 153 board.
5 * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
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>
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.
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.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/gpio.h>
27 #include <linux/platform_device.h>
29 #include <linux/libata.h>
30 #include <scsi/scsi_host.h>
32 #define DRV_NAME "pata-rb153-cf"
33 #define DRV_VERSION "0.5.0"
34 #define DRV_DESC "PATA driver for RouterBOARD 153 Compact Flash"
36 #define RB153_CF_MAXPORTS 1
37 #define RB153_CF_IO_DELAY 100
39 #define RB153_CF_REG_CMD 0x0800
40 #define RB153_CF_REG_CTRL 0x080E
41 #define RB153_CF_REG_DATA 0x0C00
43 struct rb153_cf_info {
45 unsigned int gpio_line;
50 static inline void rb153_pata_finish_io(struct ata_port *ap)
52 struct rb153_cf_info *info = ap->host->private_data;
54 /* FIXME: Keep previous delay. If this is merely a fence then
55 * ata_sff_sync might be sufficient. */
56 ata_sff_dma_pause(ap);
57 ndelay(RB153_CF_IO_DELAY);
59 irq_set_irq_type(info->irq, IRQ_TYPE_LEVEL_HIGH);
62 static void rb153_pata_exec_command(struct ata_port *ap,
63 const struct ata_taskfile *tf)
65 writeb(tf->command, ap->ioaddr.command_addr);
66 rb153_pata_finish_io(ap);
69 static unsigned int rb153_pata_data_xfer(struct ata_device *adev,
74 void __iomem *ioaddr = adev->link->ap->ioaddr.data_addr;
79 for (; t > 0; t--, buf++)
82 for (; t > 0; t--, buf++)
86 rb153_pata_finish_io(adev->link->ap);
90 static void rb153_pata_freeze(struct ata_port *ap)
92 struct rb153_cf_info *info = ap->host->private_data;
97 static void rb153_pata_thaw(struct ata_port *ap)
99 struct rb153_cf_info *info = ap->host->private_data;
104 static irqreturn_t rb153_pata_irq_handler(int irq, void *dev_instance)
106 struct ata_host *ah = dev_instance;
107 struct rb153_cf_info *info = ah->private_data;
109 if (gpio_get_value(info->gpio_line)) {
110 irq_set_irq_type(info->irq, IRQ_TYPE_LEVEL_LOW);
112 ata_sff_interrupt(irq, dev_instance);
114 irq_set_irq_type(info->irq, IRQ_TYPE_LEVEL_HIGH);
120 static struct ata_port_operations rb153_pata_port_ops = {
121 .inherits = &ata_sff_port_ops,
122 .sff_exec_command = rb153_pata_exec_command,
123 .sff_data_xfer = rb153_pata_data_xfer,
124 .freeze = rb153_pata_freeze,
125 .thaw = rb153_pata_thaw,
128 static struct scsi_host_template rb153_pata_sht = {
129 ATA_PIO_SHT(DRV_NAME),
132 static void rb153_pata_setup_port(struct ata_host *ah)
134 struct rb153_cf_info *info = ah->private_data;
139 ap->ops = &rb153_pata_port_ops;
140 ap->pio_mask = 0x1f; /* PIO4 */
142 ap->ioaddr.cmd_addr = info->iobase + RB153_CF_REG_CMD;
143 ap->ioaddr.ctl_addr = info->iobase + RB153_CF_REG_CTRL;
144 ap->ioaddr.altstatus_addr = info->iobase + RB153_CF_REG_CTRL;
146 ata_sff_std_ports(&ap->ioaddr);
148 ap->ioaddr.data_addr = info->iobase + RB153_CF_REG_DATA;
151 static int rb153_pata_driver_probe(struct platform_device *pdev)
155 struct resource *res;
157 struct rb153_cf_info *info;
160 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
162 dev_err(&pdev->dev, "no IOMEM resource found\n");
166 irq = platform_get_irq(pdev, 0);
168 dev_err(&pdev->dev, "no IRQ resource found\n");
172 gpio = irq_to_gpio(irq);
174 dev_err(&pdev->dev, "no GPIO found for irq%d\n", irq);
178 ret = gpio_request(gpio, DRV_NAME);
180 dev_err(&pdev->dev, "GPIO request failed\n");
184 ah = ata_host_alloc(&pdev->dev, RB153_CF_MAXPORTS);
188 platform_set_drvdata(pdev, ah);
190 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
194 ah->private_data = info;
195 info->gpio_line = gpio;
198 info->iobase = devm_ioremap_nocache(&pdev->dev, res->start,
199 res->end - res->start + 1);
203 ret = gpio_direction_input(gpio);
205 dev_err(&pdev->dev, "unable to set GPIO direction, err=%d\n",
210 rb153_pata_setup_port(ah);
212 ret = ata_host_activate(ah, irq, rb153_pata_irq_handler,
213 IRQF_TRIGGER_LOW, &rb153_pata_sht);
225 static int rb153_pata_driver_remove(struct platform_device *pdev)
227 struct ata_host *ah = platform_get_drvdata(pdev);
228 struct rb153_cf_info *info = ah->private_data;
231 gpio_free(info->gpio_line);
236 static struct platform_driver rb153_pata_platform_driver = {
237 .probe = rb153_pata_driver_probe,
238 .remove = rb153_pata_driver_remove,
241 .owner = THIS_MODULE,
245 /* ------------------------------------------------------------------------ */
247 #define DRV_INFO DRV_DESC " version " DRV_VERSION
249 static int __init rb153_pata_module_init(void)
251 printk(KERN_INFO DRV_INFO "\n");
253 return platform_driver_register(&rb153_pata_platform_driver);
256 static void __exit rb153_pata_module_exit(void)
258 platform_driver_unregister(&rb153_pata_platform_driver);
261 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
262 MODULE_DESCRIPTION(DRV_DESC);
263 MODULE_VERSION(DRV_VERSION);
264 MODULE_LICENSE("GPL v2");
266 module_init(rb153_pata_module_init);
267 module_exit(rb153_pata_module_exit);