blob: a5fdbdcb0faf2ed8daa5c4fa77767da6cc3fa7ea [file] [log] [blame]
Sergey Matyukevich918d7b72009-06-19 08:27:40 +04001/*
2 * PATA driver for AT91SAM9260 Static Memory Controller
3 * with CompactFlash interface in True IDE mode
4 *
5 * Copyright (C) 2009 Matyukevich Sergey
6 *
7 * Based on:
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
11 *
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.
15 *
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/blkdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Sergey Matyukevich918d7b72009-06-19 08:27:40 +040023#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>
29
Sergey Matyukevich918d7b72009-06-19 08:27:40 +040030#include <mach/at91sam9_smc.h>
Sergey Matyukevich918d7b72009-06-19 08:27:40 +040031#include <mach/board.h>
32#include <mach/gpio.h>
33
34
35#define DRV_NAME "pata_at91"
Igor Plyatov9719b8f2011-03-28 16:56:15 +040036#define DRV_VERSION "0.2"
Sergey Matyukevich918d7b72009-06-19 08:27:40 +040037
38#define CF_IDE_OFFSET 0x00c00000
39#define CF_ALT_IDE_OFFSET 0x00e00000
40#define CF_IDE_RES_SIZE 0x08
Igor Plyatov9719b8f2011-03-28 16:56:15 +040041#define NCS_RD_PULSE_LIMIT 0x3f /* maximal value for pulse bitfields */
Sergey Matyukevich918d7b72009-06-19 08:27:40 +040042
43struct at91_ide_info {
44 unsigned long mode;
45 unsigned int cs;
46
Sergey Matyukevich7d084d92009-07-16 22:38:55 +040047 struct clk *mck;
48
Sergey Matyukevich918d7b72009-06-19 08:27:40 +040049 void __iomem *ide_addr;
50 void __iomem *alt_addr;
51};
52
Igor Plyatov792d37a2011-03-28 16:56:14 +040053static const struct ata_timing initial_timing = {
54 .mode = XFER_PIO_0,
55 .setup = 70,
56 .act8b = 290,
57 .rec8b = 240,
58 .cyc8b = 600,
59 .active = 165,
60 .recover = 150,
61 .dmack_hold = 0,
62 .cycle = 600,
63 .udma = 0
64};
Sergey Matyukevich918d7b72009-06-19 08:27:40 +040065
Sergey Matyukevich7d084d92009-07-16 22:38:55 +040066static unsigned long calc_mck_cycles(unsigned long ns, unsigned long mck_hz)
Sergey Matyukevich918d7b72009-06-19 08:27:40 +040067{
68 unsigned long mul;
69
Sergey Matyukevich7d084d92009-07-16 22:38:55 +040070 /*
71 * cycles = x [nsec] * f [Hz] / 10^9 [ns in sec] =
72 * x * (f / 1_000_000_000) =
73 * x * ((f * 65536) / 1_000_000_000) / 65536 =
74 * x * (((f / 10_000) * 65536) / 100_000) / 65536 =
75 */
Sergey Matyukevich918d7b72009-06-19 08:27:40 +040076
Sergey Matyukevich7d084d92009-07-16 22:38:55 +040077 mul = (mck_hz / 10000) << 16;
78 mul /= 100000;
Sergey Matyukevich918d7b72009-06-19 08:27:40 +040079
Sergey Matyukevich7d084d92009-07-16 22:38:55 +040080 return (ns * mul + 65536) >> 16; /* rounding */
Sergey Matyukevich918d7b72009-06-19 08:27:40 +040081}
82
83static void set_smc_mode(struct at91_ide_info *info)
84{
Sergey Matyukevich7d084d92009-07-16 22:38:55 +040085 at91_sys_write(AT91_SMC_MODE(info->cs), info->mode);
86 return;
Sergey Matyukevich918d7b72009-06-19 08:27:40 +040087}
88
89static void set_smc_timing(struct device *dev,
90 struct at91_ide_info *info, const struct ata_timing *ata)
91{
Sergey Matyukevich7d084d92009-07-16 22:38:55 +040092 unsigned long read_cycle, write_cycle, active, recover;
93 unsigned long nrd_setup, nrd_pulse, nrd_recover;
94 unsigned long nwe_setup, nwe_pulse;
Sergey Matyukevich918d7b72009-06-19 08:27:40 +040095
Sergey Matyukevich7d084d92009-07-16 22:38:55 +040096 unsigned long ncs_write_setup, ncs_write_pulse;
97 unsigned long ncs_read_setup, ncs_read_pulse;
Sergey Matyukevich918d7b72009-06-19 08:27:40 +040098
Sergey Matyukevich7d084d92009-07-16 22:38:55 +040099 unsigned long mck_hz;
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400100
101 read_cycle = ata->cyc8b;
102 nrd_setup = ata->setup;
103 nrd_pulse = ata->act8b;
104 nrd_recover = ata->rec8b;
105
Sergey Matyukevich7d084d92009-07-16 22:38:55 +0400106 mck_hz = clk_get_rate(info->mck);
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400107
108 read_cycle = calc_mck_cycles(read_cycle, mck_hz);
109 nrd_setup = calc_mck_cycles(nrd_setup, mck_hz);
110 nrd_pulse = calc_mck_cycles(nrd_pulse, mck_hz);
111 nrd_recover = calc_mck_cycles(nrd_recover, mck_hz);
112
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400113 active = nrd_setup + nrd_pulse;
114 recover = read_cycle - active;
115
116 /* Need at least two cycles recovery */
117 if (recover < 2)
118 read_cycle = active + 2;
119
120 /* (CS0, CS1, DIR, OE) <= (CFCE1, CFCE2, CFRNW, NCSX) timings */
121 ncs_read_setup = 1;
122 ncs_read_pulse = read_cycle - 2;
Igor Plyatov9719b8f2011-03-28 16:56:15 +0400123 if (ncs_read_pulse > NCS_RD_PULSE_LIMIT) {
124 ncs_read_pulse = NCS_RD_PULSE_LIMIT;
125 dev_warn(dev, "ncs_read_pulse limited to maximal value %lu\n",
126 ncs_read_pulse);
127 }
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400128
129 /* Write timings same as read timings */
130 write_cycle = read_cycle;
131 nwe_setup = nrd_setup;
132 nwe_pulse = nrd_pulse;
133 ncs_write_setup = ncs_read_setup;
134 ncs_write_pulse = ncs_read_pulse;
135
Sergey Matyukevich7d084d92009-07-16 22:38:55 +0400136 dev_dbg(dev, "ATA timings: nrd_setup = %lu nrd_pulse = %lu nrd_cycle = %lu\n",
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400137 nrd_setup, nrd_pulse, read_cycle);
Sergey Matyukevich7d084d92009-07-16 22:38:55 +0400138 dev_dbg(dev, "ATA timings: nwe_setup = %lu nwe_pulse = %lu nwe_cycle = %lu\n",
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400139 nwe_setup, nwe_pulse, write_cycle);
Sergey Matyukevich7d084d92009-07-16 22:38:55 +0400140 dev_dbg(dev, "ATA timings: ncs_read_setup = %lu ncs_read_pulse = %lu\n",
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400141 ncs_read_setup, ncs_read_pulse);
Sergey Matyukevich7d084d92009-07-16 22:38:55 +0400142 dev_dbg(dev, "ATA timings: ncs_write_setup = %lu ncs_write_pulse = %lu\n",
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400143 ncs_write_setup, ncs_write_pulse);
144
145 at91_sys_write(AT91_SMC_SETUP(info->cs),
146 AT91_SMC_NWESETUP_(nwe_setup) |
147 AT91_SMC_NRDSETUP_(nrd_setup) |
148 AT91_SMC_NCS_WRSETUP_(ncs_write_setup) |
149 AT91_SMC_NCS_RDSETUP_(ncs_read_setup));
150
151 at91_sys_write(AT91_SMC_PULSE(info->cs),
152 AT91_SMC_NWEPULSE_(nwe_pulse) |
153 AT91_SMC_NRDPULSE_(nrd_pulse) |
154 AT91_SMC_NCS_WRPULSE_(ncs_write_pulse) |
155 AT91_SMC_NCS_RDPULSE_(ncs_read_pulse));
156
157 at91_sys_write(AT91_SMC_CYCLE(info->cs),
158 AT91_SMC_NWECYCLE_(write_cycle) |
159 AT91_SMC_NRDCYCLE_(read_cycle));
160
161 return;
162}
163
164static void pata_at91_set_piomode(struct ata_port *ap, struct ata_device *adev)
165{
166 struct at91_ide_info *info = ap->host->private_data;
167 struct ata_timing timing;
168 int ret;
169
170 /* Compute ATA timing and set it to SMC */
171 ret = ata_timing_compute(adev, adev->pio_mode, &timing, 1000, 0);
172 if (ret) {
Jeff Garzik429e3862010-02-04 01:09:54 -0500173 dev_warn(ap->dev, "Failed to compute ATA timing %d, "
174 "set PIO_0 timing\n", ret);
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400175 set_smc_timing(ap->dev, info, &initial_timing);
176 } else {
177 set_smc_timing(ap->dev, info, &timing);
178 }
179
180 /* Setup SMC mode */
181 set_smc_mode(info);
182
183 return;
184}
185
186static unsigned int pata_at91_data_xfer_noirq(struct ata_device *dev,
187 unsigned char *buf, unsigned int buflen, int rw)
188{
189 struct at91_ide_info *info = dev->link->ap->host->private_data;
190 unsigned int consumed;
191 unsigned long flags;
192 unsigned int mode;
193
194 local_irq_save(flags);
195 mode = at91_sys_read(AT91_SMC_MODE(info->cs));
196
197 /* set 16bit mode before writing data */
198 at91_sys_write(AT91_SMC_MODE(info->cs),
199 (mode & ~AT91_SMC_DBW) | AT91_SMC_DBW_16);
200
201 consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
202
203 /* restore 8bit mode after data is written */
204 at91_sys_write(AT91_SMC_MODE(info->cs),
205 (mode & ~AT91_SMC_DBW) | AT91_SMC_DBW_8);
206
207 local_irq_restore(flags);
208 return consumed;
209}
210
211static struct scsi_host_template pata_at91_sht = {
212 ATA_PIO_SHT(DRV_NAME),
213};
214
215static struct ata_port_operations pata_at91_port_ops = {
216 .inherits = &ata_sff_port_ops,
217
218 .sff_data_xfer = pata_at91_data_xfer_noirq,
219 .set_piomode = pata_at91_set_piomode,
220 .cable_detect = ata_cable_40wire,
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400221};
222
223static int __devinit pata_at91_probe(struct platform_device *pdev)
224{
225 struct at91_cf_data *board = pdev->dev.platform_data;
226 struct device *dev = &pdev->dev;
227 struct at91_ide_info *info;
228 struct resource *mem_res;
229 struct ata_host *host;
230 struct ata_port *ap;
Sergey Matyukevich7d084d92009-07-16 22:38:55 +0400231
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400232 int irq_flags = 0;
233 int irq = 0;
234 int ret;
235
236 /* get platform resources: IO/CTL memories and irq/rst pins */
237
238 if (pdev->num_resources != 1) {
239 dev_err(&pdev->dev, "invalid number of resources\n");
240 return -EINVAL;
241 }
242
243 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
244
245 if (!mem_res) {
246 dev_err(dev, "failed to get mem resource\n");
247 return -EINVAL;
248 }
249
250 irq = board->irq_pin;
251
252 /* init ata host */
253
254 host = ata_host_alloc(dev, 1);
255
256 if (!host)
257 return -ENOMEM;
258
259 ap = host->ports[0];
260 ap->ops = &pata_at91_port_ops;
261 ap->flags |= ATA_FLAG_SLAVE_POSS;
262 ap->pio_mask = ATA_PIO4;
263
264 if (!irq) {
265 ap->flags |= ATA_FLAG_PIO_POLLING;
266 ata_port_desc(ap, "no IRQ, using PIO polling");
267 }
268
Tejun Heodf9eba82009-08-07 11:15:20 +0900269 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400270
271 if (!info) {
272 dev_err(dev, "failed to allocate memory for private data\n");
273 return -ENOMEM;
274 }
275
Sergey Matyukevich7d084d92009-07-16 22:38:55 +0400276 info->mck = clk_get(NULL, "mck");
277
278 if (IS_ERR(info->mck)) {
279 dev_err(dev, "failed to get access to mck clock\n");
280 return -ENODEV;
281 }
282
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400283 info->cs = board->chipselect;
284 info->mode = AT91_SMC_READMODE | AT91_SMC_WRITEMODE |
285 AT91_SMC_EXNWMODE_READY | AT91_SMC_BAT_SELECT |
286 AT91_SMC_DBW_8 | AT91_SMC_TDF_(0);
287
288 info->ide_addr = devm_ioremap(dev,
289 mem_res->start + CF_IDE_OFFSET, CF_IDE_RES_SIZE);
290
291 if (!info->ide_addr) {
292 dev_err(dev, "failed to map IO base\n");
293 ret = -ENOMEM;
Tejun Heodf9eba82009-08-07 11:15:20 +0900294 goto err_put;
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400295 }
296
297 info->alt_addr = devm_ioremap(dev,
298 mem_res->start + CF_ALT_IDE_OFFSET, CF_IDE_RES_SIZE);
299
300 if (!info->alt_addr) {
301 dev_err(dev, "failed to map CTL base\n");
302 ret = -ENOMEM;
Tejun Heodf9eba82009-08-07 11:15:20 +0900303 goto err_put;
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400304 }
305
306 ap->ioaddr.cmd_addr = info->ide_addr;
307 ap->ioaddr.ctl_addr = info->alt_addr + 0x06;
308 ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
309
310 ata_sff_std_ports(&ap->ioaddr);
311
312 ata_port_desc(ap, "mmio cmd 0x%llx ctl 0x%llx",
313 (unsigned long long)mem_res->start + CF_IDE_OFFSET,
314 (unsigned long long)mem_res->start + CF_ALT_IDE_OFFSET);
315
316 host->private_data = info;
317
318 return ata_host_activate(host, irq ? gpio_to_irq(irq) : 0,
319 irq ? ata_sff_interrupt : NULL,
320 irq_flags, &pata_at91_sht);
321
Tejun Heodf9eba82009-08-07 11:15:20 +0900322err_put:
Sergey Matyukevich7d084d92009-07-16 22:38:55 +0400323 clk_put(info->mck);
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400324 return ret;
325}
326
327static int __devexit pata_at91_remove(struct platform_device *pdev)
328{
329 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Julia Lawall1e1f4212009-07-11 09:49:48 +0200330 struct at91_ide_info *info;
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400331
332 if (!host)
333 return 0;
Julia Lawall1e1f4212009-07-11 09:49:48 +0200334 info = host->private_data;
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400335
336 ata_host_detach(host);
337
338 if (!info)
339 return 0;
340
Sergey Matyukevich7d084d92009-07-16 22:38:55 +0400341 clk_put(info->mck);
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400342
Sergey Matyukevich918d7b72009-06-19 08:27:40 +0400343 return 0;
344}
345
346static struct platform_driver pata_at91_driver = {
347 .probe = pata_at91_probe,
348 .remove = __devexit_p(pata_at91_remove),
349 .driver = {
350 .name = DRV_NAME,
351 .owner = THIS_MODULE,
352 },
353};
354
355static int __init pata_at91_init(void)
356{
357 return platform_driver_register(&pata_at91_driver);
358}
359
360static void __exit pata_at91_exit(void)
361{
362 platform_driver_unregister(&pata_at91_driver);
363}
364
365
366module_init(pata_at91_init);
367module_exit(pata_at91_exit);
368
369
370MODULE_LICENSE("GPL");
371MODULE_DESCRIPTION("Driver for CF in True IDE mode on AT91SAM9260 SoC");
372MODULE_AUTHOR("Matyukevich Sergey");
373MODULE_VERSION(DRV_VERSION);
374