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 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 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>
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.4.0"
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 {
48 unsigned int gpio_line;
52 /* ------------------------------------------------------------------------ */
54 static inline void rb153_pata_finish_io(struct ata_port *ap)
56 struct rb153_cf_info *info = ap->host->private_data;
59 ndelay(RB153_CF_IO_DELAY);
61 set_irq_type(info->irq, IRQ_TYPE_LEVEL_HIGH);
64 static void rb153_pata_exec_command(struct ata_port *ap,
65 const struct ata_taskfile *tf)
67 writeb(tf->command, ap->ioaddr.command_addr);
68 rb153_pata_finish_io(ap);
71 static void rb153_pata_data_xfer(struct ata_device *adev, unsigned char *buf,
72 unsigned int buflen, int write_data)
74 void __iomem *ioaddr = adev->link->ap->ioaddr.data_addr;
77 for (; buflen > 0; buflen--, buf++)
80 for (; buflen > 0; buflen--, buf++)
84 rb153_pata_finish_io(adev->link->ap);
87 static void rb153_pata_freeze(struct ata_port *ap)
89 struct rb153_cf_info *info = ap->host->private_data;
94 static void rb153_pata_thaw(struct ata_port *ap)
96 struct rb153_cf_info *info = ap->host->private_data;
101 static irqreturn_t rb153_pata_irq_handler(int irq, void *dev_instance)
103 struct ata_host *ah = dev_instance;
104 struct rb153_cf_info *info = ah->private_data;
106 if (gpio_get_value(info->gpio_line)) {
107 set_irq_type(info->irq, IRQ_TYPE_LEVEL_LOW);
109 ata_interrupt(irq, dev_instance);
111 set_irq_type(info->irq, IRQ_TYPE_LEVEL_HIGH);
117 static void rb153_pata_irq_clear(struct ata_port *ap)
121 static int rb153_pata_port_start(struct ata_port *ap)
126 static struct ata_port_operations rb153_pata_port_ops = {
127 .tf_load = ata_tf_load,
128 .tf_read = ata_tf_read,
130 .exec_command = rb153_pata_exec_command,
131 .check_status = ata_check_status,
132 .dev_select = ata_std_dev_select,
134 .data_xfer = rb153_pata_data_xfer,
136 .qc_prep = ata_qc_prep,
137 .qc_issue = ata_qc_issue_prot,
139 .freeze = rb153_pata_freeze,
140 .thaw = rb153_pata_thaw,
141 .error_handler = ata_bmdma_error_handler,
142 .cable_detect = ata_cable_40wire,
144 .irq_handler = rb153_pata_irq_handler,
145 .irq_clear = rb153_pata_irq_clear,
146 .irq_on = ata_irq_on,
148 .port_start = rb153_pata_port_start,
151 /* ------------------------------------------------------------------------ */
153 static struct scsi_host_template rb153_pata_sht = {
154 .module = THIS_MODULE,
156 .ioctl = ata_scsi_ioctl,
157 .queuecommand = ata_scsi_queuecmd,
158 .slave_configure = ata_scsi_slave_config,
159 .slave_destroy = ata_scsi_slave_destroy,
160 .bios_param = ata_std_bios_param,
161 .proc_name = DRV_NAME,
163 .can_queue = ATA_DEF_QUEUE,
164 .this_id = ATA_SHT_THIS_ID,
165 .sg_tablesize = LIBATA_MAX_PRD,
166 .dma_boundary = ATA_DMA_BOUNDARY,
167 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
168 .emulated = ATA_SHT_EMULATED,
169 .use_clustering = ATA_SHT_USE_CLUSTERING,
172 /* ------------------------------------------------------------------------ */
174 static void rb153_pata_setup_port(struct ata_port *ap,
175 struct rb153_cf_info *info, unsigned long raw_cmd)
177 ap->ioaddr.cmd_addr = info->iobase + RB153_CF_REG_CMD;
178 ap->ioaddr.ctl_addr = info->iobase + RB153_CF_REG_CTRL;
179 ap->ioaddr.altstatus_addr = info->iobase + RB153_CF_REG_CTRL;
181 ata_std_ports(&ap->ioaddr);
183 ap->ioaddr.data_addr = info->iobase + RB153_CF_REG_DATA;
185 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", raw_cmd,
186 raw_cmd + RB153_CF_REG_CTRL);
189 static __devinit int rb153_pata_driver_probe(struct platform_device *pdev)
193 struct resource *res;
196 struct rb153_cf_info *info;
199 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
201 dev_err(&pdev->dev, "no IOMEM resource found\n");
205 irq = platform_get_irq(pdev, 0);
207 dev_err(&pdev->dev, "no IRQ resource found\n");
211 gpio = irq_to_gpio(irq);
213 dev_err(&pdev->dev, "no GPIO found for irq%d\n", irq);
217 ret = gpio_request(gpio, DRV_NAME);
219 dev_err(&pdev->dev, "GPIO request failed\n");
224 ah = ata_host_alloc(&pdev->dev, RB153_CF_MAXPORTS);
228 platform_set_drvdata(pdev, ah);
230 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
234 ah->private_data = info;
235 info->gpio_line = gpio;
238 info->iobase = devm_ioremap_nocache(&pdev->dev, res->start,
239 res->end - res->start + 1);
243 ret = gpio_direction_input(gpio);
245 dev_err(&pdev->dev, "unable to set GPIO direction, err=%d\n",
252 ap->ops = &rb153_pata_port_ops;
253 ap->pio_mask = 0x1f; /* PIO4 */
254 ap->flags = ATA_FLAG_NO_LEGACY | ATA_FLAG_MMIO;
256 rb153_pata_setup_port(ap, info, res->start);
258 ret = ata_host_activate(ah, irq, rb153_pata_irq_handler,
259 IRQF_TRIGGER_LOW, &rb153_pata_sht);
271 static __devexit int rb153_pata_driver_remove(struct platform_device *pdev)
273 struct ata_host *ah = platform_get_drvdata(pdev);
274 struct rb153_cf_info *info = ah->private_data;
277 gpio_free(info->gpio_line);
282 static struct platform_driver rb153_pata_platform_driver = {
283 .probe = rb153_pata_driver_probe,
284 .remove = __devexit_p(rb153_pata_driver_remove),
287 .owner = THIS_MODULE,
291 /* ------------------------------------------------------------------------ */
293 #define DRV_INFO DRV_DESC " version " DRV_VERSION
295 static int __init rb153_pata_module_init(void)
297 printk(KERN_INFO DRV_INFO "\n");
299 return platform_driver_register(&rb153_pata_platform_driver);
302 static void __exit rb153_pata_module_exit(void)
304 platform_driver_unregister(&rb153_pata_platform_driver);
307 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
308 MODULE_DESCRIPTION(DRV_DESC);
309 MODULE_VERSION(DRV_VERSION);
310 MODULE_LICENSE("GPL v2");
312 module_init(rb153_pata_module_init);
313 module_exit(rb153_pata_module_exit);