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.2"
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 void __iomem
*ioaddr
= adev
->ap
->ioaddr
.data_addr
;
76 for (; buflen
> 0; buflen
--, buf
++)
79 for (; buflen
> 0; buflen
--, buf
++)
83 rb153_pata_finish_io(adev
->ap
);
86 static void rb153_pata_freeze(struct ata_port
*ap
)
88 struct rb153_cf_info
*info
= ap
->host
->private_data
;
93 static void rb153_pata_thaw(struct ata_port
*ap
)
95 struct rb153_cf_info
*info
= ap
->host
->private_data
;
100 static irqreturn_t
rb153_pata_irq_handler(int irq
, void *dev_instance
)
102 struct ata_host
*ah
= dev_instance
;
103 struct rb153_cf_info
*info
= ah
->private_data
;
105 if (gpio_get_value(info
->gpio_line
)) {
106 set_irq_type(ah
->irq
, IRQ_TYPE_LEVEL_LOW
);
108 ata_interrupt(irq
, dev_instance
);
110 set_irq_type(ah
->irq
, IRQ_TYPE_LEVEL_HIGH
);
116 static void rb153_pata_irq_clear(struct ata_port
*ap
)
120 static int rb153_pata_port_start(struct ata_port
*ap
)
125 static struct ata_port_operations rb153_pata_port_ops
= {
126 .port_disable
= ata_port_disable
,
128 .tf_load
= ata_tf_load
,
129 .tf_read
= ata_tf_read
,
131 .exec_command
= rb153_pata_exec_command
,
132 .check_status
= ata_check_status
,
133 .dev_select
= ata_std_dev_select
,
135 .data_xfer
= rb153_pata_data_xfer
,
137 .qc_prep
= ata_qc_prep
,
138 .qc_issue
= ata_qc_issue_prot
,
140 .freeze
= rb153_pata_freeze
,
141 .thaw
= rb153_pata_thaw
,
142 .error_handler
= ata_bmdma_error_handler
,
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_ports(struct ata_host
*ah
)
176 struct rb153_cf_info
*info
= ah
->private_data
;
178 struct ata_ioports
*iop
;
183 ap
->ops
= &rb153_pata_port_ops
;
184 ap
->pio_mask
= 0x1f; /* PIO4 */
185 ap
->flags
|= ATA_FLAG_MMIO
| ATA_FLAG_NO_LEGACY
;
187 iop
->cmd_addr
= info
->iobase
+ RB153_CF_REG_CMD
;
188 iop
->ctl_addr
= info
->iobase
+ RB153_CF_REG_CTRL
;
189 iop
->altstatus_addr
= info
->iobase
+ RB153_CF_REG_CTRL
;
190 iop
->data_addr
= info
->iobase
+ RB153_CF_REG_DATA
;
195 static __devinit
int rb153_pata_driver_probe(struct platform_device
*pdev
)
199 struct resource
*res
;
201 struct rb153_cf_info
*info
;
204 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
206 dev_err(&pdev
->dev
, "no IOMEM resource found\n");
210 irq
= platform_get_irq(pdev
, 0);
212 dev_err(&pdev
->dev
, "no IRQ resource found\n");
216 gpio
= irq_to_gpio(irq
);
218 dev_err(&pdev
->dev
, "no GPIO found for irq%d\n", irq
);
222 ret
= gpio_request(gpio
, DRV_NAME
);
224 dev_err(&pdev
->dev
, "GPIO request failed\n");
229 ah
= ata_host_alloc(&pdev
->dev
, RB153_CF_MAXPORTS
);
233 platform_set_drvdata(pdev
, ah
);
235 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
239 ah
->private_data
= info
;
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",
253 rb153_pata_setup_ports(ah
);
255 ret
= ata_host_activate(ah
, irq
, ata_interrupt
, IRQF_TRIGGER_HIGH
,
268 static __devexit
int rb153_pata_driver_remove(struct platform_device
*pdev
)
270 struct ata_host
*ah
= platform_get_drvdata(pdev
);
271 struct rb153_cf_info
*info
= ah
->private_data
;
274 gpio_free(info
->gpio_line
);
279 static struct platform_driver rb153_pata_platform_driver
= {
280 .probe
= rb153_pata_driver_probe
,
281 .remove
= __devexit_p(rb153_pata_driver_remove
),
284 .owner
= THIS_MODULE
,
288 /* ------------------------------------------------------------------------ */
290 #define DRV_INFO DRV_DESC " version " DRV_VERSION
292 static int __init
rb153_pata_module_init(void)
294 printk(KERN_INFO DRV_INFO
"\n");
296 return platform_driver_register(&rb153_pata_platform_driver
);
299 static void __exit
rb153_pata_module_exit(void)
301 platform_driver_unregister(&rb153_pata_platform_driver
);
304 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
305 MODULE_DESCRIPTION(DRV_DESC
);
306 MODULE_VERSION(DRV_VERSION
);
307 MODULE_LICENSE("GPL");
309 module_init(rb153_pata_module_init
);
310 module_exit(rb153_pata_module_exit
);