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 unsigned int 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
;
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_interrupt(irq
, dev_instance
);
114 set_irq_type(info
->irq
, IRQ_TYPE_LEVEL_HIGH
);
120 static void rb153_pata_irq_clear(struct ata_port
*ap
)
124 static int rb153_pata_port_start(struct ata_port
*ap
)
129 static struct ata_port_operations rb153_pata_port_ops
= {
130 .tf_load
= ata_tf_load
,
131 .tf_read
= ata_tf_read
,
133 .exec_command
= rb153_pata_exec_command
,
134 .check_status
= ata_check_status
,
135 .dev_select
= ata_std_dev_select
,
137 .data_xfer
= rb153_pata_data_xfer
,
139 .qc_prep
= ata_qc_prep
,
140 .qc_issue
= ata_qc_issue_prot
,
142 .freeze
= rb153_pata_freeze
,
143 .thaw
= rb153_pata_thaw
,
144 .error_handler
= ata_bmdma_error_handler
,
145 .cable_detect
= ata_cable_40wire
,
147 .irq_handler
= rb153_pata_irq_handler
,
148 .irq_clear
= rb153_pata_irq_clear
,
149 .irq_on
= ata_irq_on
,
151 .port_start
= rb153_pata_port_start
,
154 /* ------------------------------------------------------------------------ */
156 static struct scsi_host_template rb153_pata_sht
= {
157 .module
= THIS_MODULE
,
159 .ioctl
= ata_scsi_ioctl
,
160 .queuecommand
= ata_scsi_queuecmd
,
161 .slave_configure
= ata_scsi_slave_config
,
162 .slave_destroy
= ata_scsi_slave_destroy
,
163 .bios_param
= ata_std_bios_param
,
164 .proc_name
= DRV_NAME
,
166 .can_queue
= ATA_DEF_QUEUE
,
167 .this_id
= ATA_SHT_THIS_ID
,
168 .sg_tablesize
= LIBATA_MAX_PRD
,
169 .dma_boundary
= ATA_DMA_BOUNDARY
,
170 .cmd_per_lun
= ATA_SHT_CMD_PER_LUN
,
171 .emulated
= ATA_SHT_EMULATED
,
172 .use_clustering
= ATA_SHT_USE_CLUSTERING
,
175 /* ------------------------------------------------------------------------ */
177 static void rb153_pata_setup_port(struct ata_port
*ap
,
178 struct rb153_cf_info
*info
, unsigned long raw_cmd
)
180 ap
->ioaddr
.cmd_addr
= info
->iobase
+ RB153_CF_REG_CMD
;
181 ap
->ioaddr
.ctl_addr
= info
->iobase
+ RB153_CF_REG_CTRL
;
182 ap
->ioaddr
.altstatus_addr
= info
->iobase
+ RB153_CF_REG_CTRL
;
184 ata_std_ports(&ap
->ioaddr
);
186 ap
->ioaddr
.data_addr
= info
->iobase
+ RB153_CF_REG_DATA
;
188 ata_port_desc(ap
, "cmd 0x%lx ctl 0x%lx", raw_cmd
,
189 raw_cmd
+ RB153_CF_REG_CTRL
);
192 static __devinit
int rb153_pata_driver_probe(struct platform_device
*pdev
)
196 struct resource
*res
;
199 struct rb153_cf_info
*info
;
202 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
204 dev_err(&pdev
->dev
, "no IOMEM resource found\n");
208 irq
= platform_get_irq(pdev
, 0);
210 dev_err(&pdev
->dev
, "no IRQ resource found\n");
214 gpio
= irq_to_gpio(irq
);
216 dev_err(&pdev
->dev
, "no GPIO found for irq%d\n", irq
);
220 ret
= gpio_request(gpio
, DRV_NAME
);
222 dev_err(&pdev
->dev
, "GPIO request failed\n");
227 ah
= ata_host_alloc(&pdev
->dev
, RB153_CF_MAXPORTS
);
231 platform_set_drvdata(pdev
, ah
);
233 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
237 ah
->private_data
= info
;
238 info
->gpio_line
= gpio
;
241 info
->iobase
= devm_ioremap_nocache(&pdev
->dev
, res
->start
,
242 res
->end
- res
->start
+ 1);
246 ret
= gpio_direction_input(gpio
);
248 dev_err(&pdev
->dev
, "unable to set GPIO direction, err=%d\n",
255 ap
->ops
= &rb153_pata_port_ops
;
256 ap
->pio_mask
= 0x1f; /* PIO4 */
257 ap
->flags
= ATA_FLAG_NO_LEGACY
| ATA_FLAG_MMIO
;
259 rb153_pata_setup_port(ap
, info
, res
->start
);
261 ret
= ata_host_activate(ah
, irq
, rb153_pata_irq_handler
,
262 IRQF_TRIGGER_LOW
, &rb153_pata_sht
);
274 static __devexit
int rb153_pata_driver_remove(struct platform_device
*pdev
)
276 struct ata_host
*ah
= platform_get_drvdata(pdev
);
277 struct rb153_cf_info
*info
= ah
->private_data
;
280 gpio_free(info
->gpio_line
);
285 static struct platform_driver rb153_pata_platform_driver
= {
286 .probe
= rb153_pata_driver_probe
,
287 .remove
= __devexit_p(rb153_pata_driver_remove
),
290 .owner
= THIS_MODULE
,
294 /* ------------------------------------------------------------------------ */
296 #define DRV_INFO DRV_DESC " version " DRV_VERSION
298 static int __init
rb153_pata_module_init(void)
300 printk(KERN_INFO DRV_INFO
"\n");
302 return platform_driver_register(&rb153_pata_platform_driver
);
305 static void __exit
rb153_pata_module_exit(void)
307 platform_driver_unregister(&rb153_pata_platform_driver
);
310 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
311 MODULE_DESCRIPTION(DRV_DESC
);
312 MODULE_VERSION(DRV_VERSION
);
313 MODULE_LICENSE("GPL v2");
315 module_init(rb153_pata_module_init
);
316 module_exit(rb153_pata_module_exit
);