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 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 set_irq_type(info
->irq
, IRQ_TYPE_LEVEL_LOW
);
112 ata_sff_interrupt(irq
, dev_instance
);
114 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 */
141 ap
->flags
= ATA_FLAG_NO_LEGACY
| ATA_FLAG_MMIO
;
143 ap
->ioaddr
.cmd_addr
= info
->iobase
+ RB153_CF_REG_CMD
;
144 ap
->ioaddr
.ctl_addr
= info
->iobase
+ RB153_CF_REG_CTRL
;
145 ap
->ioaddr
.altstatus_addr
= info
->iobase
+ RB153_CF_REG_CTRL
;
147 ata_sff_std_ports(&ap
->ioaddr
);
149 ap
->ioaddr
.data_addr
= info
->iobase
+ RB153_CF_REG_DATA
;
152 static __devinit
int rb153_pata_driver_probe(struct platform_device
*pdev
)
156 struct resource
*res
;
158 struct rb153_cf_info
*info
;
161 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
163 dev_err(&pdev
->dev
, "no IOMEM resource found\n");
167 irq
= platform_get_irq(pdev
, 0);
169 dev_err(&pdev
->dev
, "no IRQ resource found\n");
173 gpio
= irq_to_gpio(irq
);
175 dev_err(&pdev
->dev
, "no GPIO found for irq%d\n", irq
);
179 ret
= gpio_request(gpio
, DRV_NAME
);
181 dev_err(&pdev
->dev
, "GPIO request failed\n");
185 ah
= ata_host_alloc(&pdev
->dev
, RB153_CF_MAXPORTS
);
189 platform_set_drvdata(pdev
, ah
);
191 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
195 ah
->private_data
= info
;
196 info
->gpio_line
= gpio
;
199 info
->iobase
= devm_ioremap_nocache(&pdev
->dev
, res
->start
,
200 res
->end
- res
->start
+ 1);
204 ret
= gpio_direction_input(gpio
);
206 dev_err(&pdev
->dev
, "unable to set GPIO direction, err=%d\n",
211 rb153_pata_setup_port(ah
);
213 ret
= ata_host_activate(ah
, irq
, rb153_pata_irq_handler
,
214 IRQF_TRIGGER_LOW
, &rb153_pata_sht
);
226 static __devexit
int rb153_pata_driver_remove(struct platform_device
*pdev
)
228 struct ata_host
*ah
= platform_get_drvdata(pdev
);
229 struct rb153_cf_info
*info
= ah
->private_data
;
232 gpio_free(info
->gpio_line
);
237 static struct platform_driver rb153_pata_platform_driver
= {
238 .probe
= rb153_pata_driver_probe
,
239 .remove
= __devexit_p(rb153_pata_driver_remove
),
242 .owner
= THIS_MODULE
,
246 /* ------------------------------------------------------------------------ */
248 #define DRV_INFO DRV_DESC " version " DRV_VERSION
250 static int __init
rb153_pata_module_init(void)
252 printk(KERN_INFO DRV_INFO
"\n");
254 return platform_driver_register(&rb153_pata_platform_driver
);
257 static void __exit
rb153_pata_module_exit(void)
259 platform_driver_unregister(&rb153_pata_platform_driver
);
262 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
263 MODULE_DESCRIPTION(DRV_DESC
);
264 MODULE_VERSION(DRV_VERSION
);
265 MODULE_LICENSE("GPL v2");
267 module_init(rb153_pata_module_init
);
268 module_exit(rb153_pata_module_exit
);