2 * PATA driver for AT91SAM9260 Static Memory Controller
3 * with CompactFlash interface in True IDE mode
5 * Copyright (C) 2009 Matyukevich Sergey
8 * * generic platform driver by Paul Mundt: drivers/ata/pata_platform.c
9 * * pata_at32 driver by Kristoffer Nyborg Gregertsen
10 * * at91_ide driver by Stanislaw Gruszka
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2
14 * as published by the Free Software Foundation.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/blkdev.h>
22 #include <linux/gfp.h>
23 #include <scsi/scsi_host.h>
24 #include <linux/ata.h>
25 #include <linux/clk.h>
26 #include <linux/libata.h>
27 #include <linux/platform_device.h>
28 #include <linux/ata_platform.h>
30 #include <mach/at91sam9_smc.h>
31 #include <mach/board.h>
32 #include <mach/gpio.h>
35 #define DRV_NAME "pata_at91"
36 #define DRV_VERSION "0.2"
38 #define CF_IDE_OFFSET 0x00c00000
39 #define CF_ALT_IDE_OFFSET 0x00e00000
40 #define CF_IDE_RES_SIZE 0x08
41 #define NCS_RD_PULSE_LIMIT 0x3f /* maximal value for pulse bitfields */
43 struct at91_ide_info
{
49 void __iomem
*ide_addr
;
50 void __iomem
*alt_addr
;
53 static const struct ata_timing initial_timing
=
54 {XFER_PIO_0
, 70, 290, 240, 600, 165, 150, 600, 0};
56 static unsigned long calc_mck_cycles(unsigned long ns
, unsigned long mck_hz
)
61 * cycles = x [nsec] * f [Hz] / 10^9 [ns in sec] =
62 * x * (f / 1_000_000_000) =
63 * x * ((f * 65536) / 1_000_000_000) / 65536 =
64 * x * (((f / 10_000) * 65536) / 100_000) / 65536 =
67 mul
= (mck_hz
/ 10000) << 16;
70 return (ns
* mul
+ 65536) >> 16; /* rounding */
73 static void set_smc_mode(struct at91_ide_info
*info
)
75 at91_sys_write(AT91_SMC_MODE(info
->cs
), info
->mode
);
79 static void set_smc_timing(struct device
*dev
,
80 struct at91_ide_info
*info
, const struct ata_timing
*ata
)
82 unsigned long read_cycle
, write_cycle
, active
, recover
;
83 unsigned long nrd_setup
, nrd_pulse
, nrd_recover
;
84 unsigned long nwe_setup
, nwe_pulse
;
86 unsigned long ncs_write_setup
, ncs_write_pulse
;
87 unsigned long ncs_read_setup
, ncs_read_pulse
;
91 read_cycle
= ata
->cyc8b
;
92 nrd_setup
= ata
->setup
;
93 nrd_pulse
= ata
->act8b
;
94 nrd_recover
= ata
->rec8b
;
96 mck_hz
= clk_get_rate(info
->mck
);
98 read_cycle
= calc_mck_cycles(read_cycle
, mck_hz
);
99 nrd_setup
= calc_mck_cycles(nrd_setup
, mck_hz
);
100 nrd_pulse
= calc_mck_cycles(nrd_pulse
, mck_hz
);
101 nrd_recover
= calc_mck_cycles(nrd_recover
, mck_hz
);
103 active
= nrd_setup
+ nrd_pulse
;
104 recover
= read_cycle
- active
;
106 /* Need at least two cycles recovery */
108 read_cycle
= active
+ 2;
110 /* (CS0, CS1, DIR, OE) <= (CFCE1, CFCE2, CFRNW, NCSX) timings */
112 ncs_read_pulse
= read_cycle
- 2;
113 if (ncs_read_pulse
> NCS_RD_PULSE_LIMIT
) {
114 ncs_read_pulse
= NCS_RD_PULSE_LIMIT
;
115 dev_warn(dev
, "ncs_read_pulse limited to maximal value %lu\n",
119 /* Write timings same as read timings */
120 write_cycle
= read_cycle
;
121 nwe_setup
= nrd_setup
;
122 nwe_pulse
= nrd_pulse
;
123 ncs_write_setup
= ncs_read_setup
;
124 ncs_write_pulse
= ncs_read_pulse
;
126 dev_dbg(dev
, "ATA timings: nrd_setup = %lu nrd_pulse = %lu nrd_cycle = %lu\n",
127 nrd_setup
, nrd_pulse
, read_cycle
);
128 dev_dbg(dev
, "ATA timings: nwe_setup = %lu nwe_pulse = %lu nwe_cycle = %lu\n",
129 nwe_setup
, nwe_pulse
, write_cycle
);
130 dev_dbg(dev
, "ATA timings: ncs_read_setup = %lu ncs_read_pulse = %lu\n",
131 ncs_read_setup
, ncs_read_pulse
);
132 dev_dbg(dev
, "ATA timings: ncs_write_setup = %lu ncs_write_pulse = %lu\n",
133 ncs_write_setup
, ncs_write_pulse
);
135 at91_sys_write(AT91_SMC_SETUP(info
->cs
),
136 AT91_SMC_NWESETUP_(nwe_setup
) |
137 AT91_SMC_NRDSETUP_(nrd_setup
) |
138 AT91_SMC_NCS_WRSETUP_(ncs_write_setup
) |
139 AT91_SMC_NCS_RDSETUP_(ncs_read_setup
));
141 at91_sys_write(AT91_SMC_PULSE(info
->cs
),
142 AT91_SMC_NWEPULSE_(nwe_pulse
) |
143 AT91_SMC_NRDPULSE_(nrd_pulse
) |
144 AT91_SMC_NCS_WRPULSE_(ncs_write_pulse
) |
145 AT91_SMC_NCS_RDPULSE_(ncs_read_pulse
));
147 at91_sys_write(AT91_SMC_CYCLE(info
->cs
),
148 AT91_SMC_NWECYCLE_(write_cycle
) |
149 AT91_SMC_NRDCYCLE_(read_cycle
));
154 static void pata_at91_set_piomode(struct ata_port
*ap
, struct ata_device
*adev
)
156 struct at91_ide_info
*info
= ap
->host
->private_data
;
157 struct ata_timing timing
;
160 /* Compute ATA timing and set it to SMC */
161 ret
= ata_timing_compute(adev
, adev
->pio_mode
, &timing
, 1000, 0);
163 dev_warn(ap
->dev
, "Failed to compute ATA timing %d, "
164 "set PIO_0 timing\n", ret
);
165 set_smc_timing(ap
->dev
, info
, &initial_timing
);
167 set_smc_timing(ap
->dev
, info
, &timing
);
176 static unsigned int pata_at91_data_xfer_noirq(struct ata_device
*dev
,
177 unsigned char *buf
, unsigned int buflen
, int rw
)
179 struct at91_ide_info
*info
= dev
->link
->ap
->host
->private_data
;
180 unsigned int consumed
;
184 local_irq_save(flags
);
185 mode
= at91_sys_read(AT91_SMC_MODE(info
->cs
));
187 /* set 16bit mode before writing data */
188 at91_sys_write(AT91_SMC_MODE(info
->cs
),
189 (mode
& ~AT91_SMC_DBW
) | AT91_SMC_DBW_16
);
191 consumed
= ata_sff_data_xfer(dev
, buf
, buflen
, rw
);
193 /* restore 8bit mode after data is written */
194 at91_sys_write(AT91_SMC_MODE(info
->cs
),
195 (mode
& ~AT91_SMC_DBW
) | AT91_SMC_DBW_8
);
197 local_irq_restore(flags
);
201 static struct scsi_host_template pata_at91_sht
= {
202 ATA_PIO_SHT(DRV_NAME
),
205 static struct ata_port_operations pata_at91_port_ops
= {
206 .inherits
= &ata_sff_port_ops
,
208 .sff_data_xfer
= pata_at91_data_xfer_noirq
,
209 .set_piomode
= pata_at91_set_piomode
,
210 .cable_detect
= ata_cable_40wire
,
213 static int __devinit
pata_at91_probe(struct platform_device
*pdev
)
215 struct at91_cf_data
*board
= pdev
->dev
.platform_data
;
216 struct device
*dev
= &pdev
->dev
;
217 struct at91_ide_info
*info
;
218 struct resource
*mem_res
;
219 struct ata_host
*host
;
226 /* get platform resources: IO/CTL memories and irq/rst pins */
228 if (pdev
->num_resources
!= 1) {
229 dev_err(&pdev
->dev
, "invalid number of resources\n");
233 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
236 dev_err(dev
, "failed to get mem resource\n");
240 irq
= board
->irq_pin
;
244 host
= ata_host_alloc(dev
, 1);
250 ap
->ops
= &pata_at91_port_ops
;
251 ap
->flags
|= ATA_FLAG_SLAVE_POSS
;
252 ap
->pio_mask
= ATA_PIO4
;
255 ap
->flags
|= ATA_FLAG_PIO_POLLING
;
256 ata_port_desc(ap
, "no IRQ, using PIO polling");
259 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
262 dev_err(dev
, "failed to allocate memory for private data\n");
266 info
->mck
= clk_get(NULL
, "mck");
268 if (IS_ERR(info
->mck
)) {
269 dev_err(dev
, "failed to get access to mck clock\n");
273 info
->cs
= board
->chipselect
;
274 info
->mode
= AT91_SMC_READMODE
| AT91_SMC_WRITEMODE
|
275 AT91_SMC_EXNWMODE_READY
| AT91_SMC_BAT_SELECT
|
276 AT91_SMC_DBW_8
| AT91_SMC_TDF_(0);
278 info
->ide_addr
= devm_ioremap(dev
,
279 mem_res
->start
+ CF_IDE_OFFSET
, CF_IDE_RES_SIZE
);
281 if (!info
->ide_addr
) {
282 dev_err(dev
, "failed to map IO base\n");
287 info
->alt_addr
= devm_ioremap(dev
,
288 mem_res
->start
+ CF_ALT_IDE_OFFSET
, CF_IDE_RES_SIZE
);
290 if (!info
->alt_addr
) {
291 dev_err(dev
, "failed to map CTL base\n");
296 ap
->ioaddr
.cmd_addr
= info
->ide_addr
;
297 ap
->ioaddr
.ctl_addr
= info
->alt_addr
+ 0x06;
298 ap
->ioaddr
.altstatus_addr
= ap
->ioaddr
.ctl_addr
;
300 ata_sff_std_ports(&ap
->ioaddr
);
302 ata_port_desc(ap
, "mmio cmd 0x%llx ctl 0x%llx",
303 (unsigned long long)mem_res
->start
+ CF_IDE_OFFSET
,
304 (unsigned long long)mem_res
->start
+ CF_ALT_IDE_OFFSET
);
306 host
->private_data
= info
;
308 return ata_host_activate(host
, irq
? gpio_to_irq(irq
) : 0,
309 irq
? ata_sff_interrupt
: NULL
,
310 irq_flags
, &pata_at91_sht
);
317 static int __devexit
pata_at91_remove(struct platform_device
*pdev
)
319 struct ata_host
*host
= dev_get_drvdata(&pdev
->dev
);
320 struct at91_ide_info
*info
;
324 info
= host
->private_data
;
326 ata_host_detach(host
);
336 static struct platform_driver pata_at91_driver
= {
337 .probe
= pata_at91_probe
,
338 .remove
= __devexit_p(pata_at91_remove
),
341 .owner
= THIS_MODULE
,
345 static int __init
pata_at91_init(void)
347 return platform_driver_register(&pata_at91_driver
);
350 static void __exit
pata_at91_exit(void)
352 platform_driver_unregister(&pata_at91_driver
);
356 module_init(pata_at91_init
);
357 module_exit(pata_at91_exit
);
360 MODULE_LICENSE("GPL");
361 MODULE_DESCRIPTION("Driver for CF in True IDE mode on AT91SAM9260 SoC");
362 MODULE_AUTHOR("Matyukevich Sergey");
363 MODULE_VERSION(DRV_VERSION
);