2 * A low-level PATA driver to handle a Compact Flash connected on the
3 * Mikrotik's RouterBoard 153 board.
5 * Copyright (C) 2007 Gabor Juhos <juhosg at 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
16 * it under the terms of the GNU General Public License version 2 as
17 * published by the Free Software Foundation.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
26 #include <linux/interrupt.h>
27 #include <linux/irq.h>
29 #include <linux/libata.h>
30 #include <scsi/scsi_host.h>
34 #define DRV_NAME "pata-rb153-cf"
35 #define DRV_VERSION "0.1"
36 #define DRV_DESC "PATA driver for RouterBOARD 153 Compact Flash"
38 #define RB153_CF_MAXPORTS 1
39 #define RB153_CF_IO_DELAY 100
41 #define RB153_CF_REG_CMD 0x0800
42 #define RB153_CF_REG_CTRL 0x080E
43 #define RB153_CF_REG_DATA 0x0C00
45 struct rb153_cf_info {
47 unsigned int gpio_line;
51 /* ------------------------------------------------------------------------ */
53 static inline void rb153_pata_finish_io(struct ata_port *ap)
55 struct ata_host *ah = ap->host;
58 ndelay(RB153_CF_IO_DELAY);
60 set_irq_type(ah->irq, IRQ_TYPE_LEVEL_HIGH);
63 static void rb153_pata_exec_command(struct ata_port *ap,
64 const struct ata_taskfile *tf)
66 writeb(tf->command, ap->ioaddr.command_addr);
67 rb153_pata_finish_io(ap);
70 static void rb153_pata_data_xfer(struct ata_device *adev, unsigned char *buf,
71 unsigned int buflen, int write_data)
73 struct ata_port *ap = adev->ap;
77 for (i = 0; i < buflen; i++)
78 writeb(buf[i], ap->ioaddr.data_addr);
80 for (; buflen > 0; buflen--)
81 buf[i] = readb(ap->ioaddr.data_addr);
84 rb153_pata_finish_io(ap);
87 static void rb153_pata_thaw(struct ata_port *ap)
89 struct rb153_cf_info *info = ap->host->private_data;
94 static irqreturn_t rb153_pata_irq_handler(int irq, void *dev_instance)
96 struct ata_host *ah = dev_instance;
97 struct rb153_cf_info *info = ah->private_data;
99 if (gpio_get_value(info->gpio_line)) {
100 set_irq_type(ah->irq, IRQ_TYPE_LEVEL_LOW);
102 ata_interrupt(irq, dev_instance);
104 set_irq_type(ah->irq, IRQ_TYPE_LEVEL_HIGH);
110 static void rb153_pata_irq_clear(struct ata_port *ap)
114 static int rb153_pata_port_start(struct ata_port *ap)
119 static struct ata_port_operations rb153_pata_port_ops = {
120 .port_disable = ata_port_disable,
122 .tf_load = ata_tf_load,
123 .tf_read = ata_tf_read,
125 .exec_command = rb153_pata_exec_command,
126 .check_status = ata_check_status,
127 .dev_select = ata_std_dev_select,
129 .data_xfer = rb153_pata_data_xfer,
131 .qc_prep = ata_qc_prep,
132 .qc_issue = ata_qc_issue_prot,
134 .thaw = rb153_pata_thaw,
135 .error_handler = ata_bmdma_error_handler,
137 .irq_handler = rb153_pata_irq_handler,
138 .irq_clear = rb153_pata_irq_clear,
139 .irq_on = ata_irq_on,
141 .port_start = rb153_pata_port_start,
144 /* ------------------------------------------------------------------------ */
146 static struct scsi_host_template rb153_pata_sht = {
147 .module = THIS_MODULE,
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,
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,
165 /* ------------------------------------------------------------------------ */
167 static void rb153_pata_setup_ports(struct ata_host *ah)
169 struct rb153_cf_info *info = ah->private_data;
171 struct ata_ioports *iop;
176 ap->ops = &rb153_pata_port_ops;
177 ap->pio_mask = 0x1f; /* PIO4 */
178 ap->flags |= ATA_FLAG_MMIO | ATA_FLAG_NO_LEGACY;
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;
188 static __devinit int rb153_pata_driver_probe(struct platform_device *pdev)
192 struct resource *res;
194 struct rb153_cf_info *info;
197 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
199 dev_err(&pdev->dev, "no IOMEM resource found\n");
203 irq = platform_get_irq(pdev, 0);
205 dev_err(&pdev->dev, "no IRQ resource found\n");
209 gpio = irq_to_gpio(irq);
211 dev_err(&pdev->dev, "no GPIO found for irq%d\n", irq);
215 ret = gpio_request(gpio, DRV_NAME);
217 dev_err(&pdev->dev, "GPIO request failed\n");
222 ah = ata_host_alloc(&pdev->dev, RB153_CF_MAXPORTS);
226 platform_set_drvdata(pdev, ah);
228 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
232 ah->private_data = info;
234 info->iobase = devm_ioremap_nocache(&pdev->dev, res->start,
235 res->end - res->start + 1);
239 ret = gpio_direction_input(gpio);
241 dev_err(&pdev->dev, "unable to set GPIO direction, err=%d\n",
246 rb153_pata_setup_ports(ah);
248 ret = ata_host_activate(ah, irq, ata_interrupt, IRQF_TRIGGER_HIGH,
261 static __devexit int rb153_pata_driver_remove(struct platform_device *pdev)
263 struct ata_host *ah = platform_get_drvdata(pdev);
264 struct rb153_cf_info *info = ah->private_data;
267 gpio_free(info->gpio_line);
272 static struct platform_driver rb153_pata_platform_driver = {
273 .probe = rb153_pata_driver_probe,
274 .remove = __devexit_p(rb153_pata_driver_remove),
277 .owner = THIS_MODULE,
281 /* ------------------------------------------------------------------------ */
283 #define DRV_INFO DRV_DESC " version " DRV_VERSION
285 static int __init rb153_pata_module_init(void)
287 printk(KERN_INFO DRV_INFO "\n");
289 return platform_driver_register(&rb153_pata_platform_driver);
292 static void __exit rb153_pata_module_exit(void)
294 platform_driver_unregister(&rb153_pata_platform_driver);
297 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
298 MODULE_DESCRIPTION(DRV_DESC);
299 MODULE_VERSION(DRV_VERSION);
300 MODULE_LICENSE("GPL");
302 module_init(rb153_pata_module_init);
303 module_exit(rb153_pata_module_exit);