blob: d7b7b7fde3620a37d11291d7682fe683a496b828 [file] [log] [blame]
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +02001/*
2 * AVR32 SMC/CFC PATA Driver
3 *
4 * Copyright (C) 2007 Atmel Norway
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 */
10
11#define DEBUG
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/device.h>
17#include <linux/platform_device.h>
18#include <linux/delay.h>
19#include <linux/interrupt.h>
20#include <linux/irq.h>
21#include <scsi/scsi_host.h>
22#include <linux/ata.h>
23#include <linux/libata.h>
24#include <linux/err.h>
25#include <linux/io.h>
26
27#include <asm/arch/board.h>
28#include <asm/arch/smc.h>
29
30#define DRV_NAME "pata_at32"
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +010031#define DRV_VERSION "0.0.3"
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +020032
33/*
34 * CompactFlash controller memory layout relative to the base address:
35 *
36 * Attribute memory: 0000 0000 -> 003f ffff
37 * Common memory: 0040 0000 -> 007f ffff
38 * I/O memory: 0080 0000 -> 00bf ffff
39 * True IDE Mode: 00c0 0000 -> 00df ffff
40 * Alt IDE Mode: 00e0 0000 -> 00ff ffff
41 *
42 * Only True IDE and Alt True IDE mode are needed for this driver.
43 *
44 * True IDE mode => CS0 = 0, CS1 = 1 (cmd, error, stat, etc)
45 * Alt True IDE mode => CS0 = 1, CS1 = 0 (ctl, alt_stat)
46 */
47#define CF_IDE_OFFSET 0x00c00000
48#define CF_ALT_IDE_OFFSET 0x00e00000
49#define CF_RES_SIZE 2048
50
51/*
52 * Define DEBUG_BUS if you are doing debugging of your own EBI -> PATA
53 * adaptor with a logic analyzer or similar.
54 */
55#undef DEBUG_BUS
56
57/*
58 * ATA PIO modes
59 *
60 * Name | Mb/s | Min cycle time | Mask
61 * --------+-------+----------------+--------
62 * Mode 0 | 3.3 | 600 ns | 0x01
63 * Mode 1 | 5.2 | 383 ns | 0x03
64 * Mode 2 | 8.3 | 240 ns | 0x07
65 * Mode 3 | 11.1 | 180 ns | 0x0f
66 * Mode 4 | 16.7 | 120 ns | 0x1f
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +010067 *
68 * Alter PIO_MASK below according to table to set maximal PIO mode.
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +020069 */
70#define PIO_MASK (0x1f)
71
72/*
73 * Struct containing private information about device.
74 */
75struct at32_ide_info {
76 unsigned int irq;
77 struct resource res_ide;
78 struct resource res_alt;
79 void __iomem *ide_addr;
80 void __iomem *alt_addr;
81 unsigned int cs;
82 struct smc_config smc;
83};
84
85/*
86 * Setup SMC for the given ATA timing.
87 */
88static int pata_at32_setup_timing(struct device *dev,
89 struct at32_ide_info *info,
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +010090 const struct ata_timing *ata)
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +020091{
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +020092 struct smc_config *smc = &info->smc;
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +010093 struct smc_timing timing;
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +020094
95 int active;
96 int recover;
97
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +010098 memset(&timing, 0, sizeof(struct smc_timing));
99
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200100 /* Total cycle time */
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +0100101 timing.read_cycle = ata->cyc8b;
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200102
103 /* DIOR <= CFIOR timings */
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +0100104 timing.nrd_setup = ata->setup;
105 timing.nrd_pulse = ata->act8b;
106 timing.nrd_recover = ata->rec8b;
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200107
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +0100108 /* Convert nanosecond timing to clock cycles */
109 smc_set_timing(smc, &timing);
110
111 /* Add one extra cycle setup due to signal ring */
112 smc->nrd_setup = smc->nrd_setup + 1;
113
114 active = smc->nrd_setup + smc->nrd_pulse;
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200115 recover = smc->read_cycle - active;
116
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +0100117 /* Need at least two cycles recovery */
118 if (recover < 2)
119 smc->read_cycle = active + 2;
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200120
121 /* (CS0, CS1, DIR, OE) <= (CFCE1, CFCE2, CFRNW, NCSX) timings */
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +0100122 smc->ncs_read_setup = 1;
123 smc->ncs_read_pulse = smc->read_cycle - 2;
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200124
125 /* Write timings same as read timings */
126 smc->write_cycle = smc->read_cycle;
127 smc->nwe_setup = smc->nrd_setup;
128 smc->nwe_pulse = smc->nrd_pulse;
129 smc->ncs_write_setup = smc->ncs_read_setup;
130 smc->ncs_write_pulse = smc->ncs_read_pulse;
131
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +0100132 /* Do some debugging output of ATA and SMC timings */
133 dev_dbg(dev, "ATA: C=%d S=%d P=%d R=%d\n",
134 ata->cyc8b, ata->setup, ata->act8b, ata->rec8b);
135
136 dev_dbg(dev, "SMC: C=%d S=%d P=%d NS=%d NP=%d\n",
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200137 smc->read_cycle, smc->nrd_setup, smc->nrd_pulse,
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +0100138 smc->ncs_read_setup, smc->ncs_read_pulse);
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200139
140 /* Finally, configure the SMC */
141 return smc_set_configuration(info->cs, smc);
142}
143
144/*
145 * Procedures for libATA.
146 */
147static void pata_at32_set_piomode(struct ata_port *ap, struct ata_device *adev)
148{
149 struct ata_timing timing;
150 struct at32_ide_info *info = ap->host->private_data;
151
152 int ret;
153
154 /* Compute ATA timing */
155 ret = ata_timing_compute(adev, adev->pio_mode, &timing, 1000, 0);
156 if (ret) {
157 dev_warn(ap->dev, "Failed to compute ATA timing %d\n", ret);
158 return;
159 }
160
161 /* Setup SMC to ATA timing */
162 ret = pata_at32_setup_timing(ap->dev, info, &timing);
163 if (ret) {
164 dev_warn(ap->dev, "Failed to setup ATA timing %d\n", ret);
165 return;
166 }
167}
168
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200169static struct scsi_host_template at32_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900170 ATA_PIO_SHT(DRV_NAME),
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200171};
172
173static struct ata_port_operations at32_port_ops = {
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200174 .set_piomode = pata_at32_set_piomode,
175 .tf_load = ata_tf_load,
176 .tf_read = ata_tf_read,
177 .exec_command = ata_exec_command,
178 .check_status = ata_check_status,
179 .dev_select = ata_std_dev_select,
180
181 .freeze = ata_bmdma_freeze,
182 .thaw = ata_bmdma_thaw,
183 .error_handler = ata_bmdma_error_handler,
184 .post_internal_cmd = ata_bmdma_post_internal_cmd,
185 .cable_detect = ata_cable_40wire,
186
187 .qc_prep = ata_qc_prep,
188 .qc_issue = ata_qc_issue_prot,
189
190 .data_xfer = ata_data_xfer,
191
Tejun Heo358f9a72008-03-25 12:22:47 +0900192 .irq_clear = ata_noop_irq_clear,
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200193 .irq_on = ata_irq_on,
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200194
195 .port_start = ata_sff_port_start,
196};
197
198static int __init pata_at32_init_one(struct device *dev,
199 struct at32_ide_info *info)
200{
201 struct ata_host *host;
202 struct ata_port *ap;
203
204 host = ata_host_alloc(dev, 1);
205 if (!host)
206 return -ENOMEM;
207
208 ap = host->ports[0];
209
210 /* Setup ATA bindings */
211 ap->ops = &at32_port_ops;
212 ap->pio_mask = PIO_MASK;
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +0100213 ap->flags |= ATA_FLAG_MMIO | ATA_FLAG_SLAVE_POSS;
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200214
215 /*
216 * Since all 8-bit taskfile transfers has to go on the lower
217 * byte of the data bus and there is a bug in the SMC that
218 * makes it impossible to alter the bus width during runtime,
219 * we need to hardwire the address signals as follows:
220 *
221 * A_IDE(2:0) <= A_EBI(3:1)
222 *
223 * This makes all addresses on the EBI even, thus all data
224 * will be on the lower byte of the data bus. All addresses
225 * used by libATA need to be altered according to this.
226 */
227 ap->ioaddr.altstatus_addr = info->alt_addr + (0x06 << 1);
228 ap->ioaddr.ctl_addr = info->alt_addr + (0x06 << 1);
229
230 ap->ioaddr.data_addr = info->ide_addr + (ATA_REG_DATA << 1);
231 ap->ioaddr.error_addr = info->ide_addr + (ATA_REG_ERR << 1);
232 ap->ioaddr.feature_addr = info->ide_addr + (ATA_REG_FEATURE << 1);
233 ap->ioaddr.nsect_addr = info->ide_addr + (ATA_REG_NSECT << 1);
234 ap->ioaddr.lbal_addr = info->ide_addr + (ATA_REG_LBAL << 1);
235 ap->ioaddr.lbam_addr = info->ide_addr + (ATA_REG_LBAM << 1);
236 ap->ioaddr.lbah_addr = info->ide_addr + (ATA_REG_LBAH << 1);
237 ap->ioaddr.device_addr = info->ide_addr + (ATA_REG_DEVICE << 1);
238 ap->ioaddr.status_addr = info->ide_addr + (ATA_REG_STATUS << 1);
239 ap->ioaddr.command_addr = info->ide_addr + (ATA_REG_CMD << 1);
240
241 /* Set info as private data of ATA host */
242 host->private_data = info;
243
244 /* Register ATA device and return */
245 return ata_host_activate(host, info->irq, ata_interrupt,
246 IRQF_SHARED | IRQF_TRIGGER_RISING,
247 &at32_sht);
248}
249
250/*
251 * This function may come in handy for people analyzing their own
252 * EBI -> PATA adaptors.
253 */
254#ifdef DEBUG_BUS
255
256static void __init pata_at32_debug_bus(struct device *dev,
257 struct at32_ide_info *info)
258{
259 const int d1 = 0xff;
260 const int d2 = 0x00;
261
262 int i;
263
264 /* Write 8-bit values (registers) */
265 iowrite8(d1, info->alt_addr + (0x06 << 1));
266 iowrite8(d2, info->alt_addr + (0x06 << 1));
267
268 for (i = 0; i < 8; i++) {
269 iowrite8(d1, info->ide_addr + (i << 1));
270 iowrite8(d2, info->ide_addr + (i << 1));
271 }
272
273 /* Write 16 bit values (data) */
274 iowrite16(d1, info->ide_addr);
275 iowrite16(d1 << 8, info->ide_addr);
276
277 iowrite16(d1, info->ide_addr);
278 iowrite16(d1 << 8, info->ide_addr);
279}
280
281#endif
282
283static int __init pata_at32_probe(struct platform_device *pdev)
284{
285 const struct ata_timing initial_timing =
286 {XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0};
287
288 struct device *dev = &pdev->dev;
289 struct at32_ide_info *info;
290 struct ide_platform_data *board = pdev->dev.platform_data;
291 struct resource *res;
292
293 int irq;
294 int ret;
295
296 if (!board)
297 return -ENXIO;
298
299 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
300 if (!res)
301 return -ENXIO;
302
303 /* Retrive IRQ */
304 irq = platform_get_irq(pdev, 0);
305 if (irq < 0)
306 return irq;
307
Joe Perches1967b7f2008-02-03 17:08:11 +0200308 /* Setup struct containing private information */
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200309 info = kzalloc(sizeof(struct at32_ide_info), GFP_KERNEL);
310 if (!info)
311 return -ENOMEM;
312
313 memset(info, 0, sizeof(struct at32_ide_info));
314
315 info->irq = irq;
316 info->cs = board->cs;
317
318 /* Request memory resources */
319 info->res_ide.start = res->start + CF_IDE_OFFSET;
320 info->res_ide.end = info->res_ide.start + CF_RES_SIZE - 1;
321 info->res_ide.name = "ide";
322 info->res_ide.flags = IORESOURCE_MEM;
323
324 ret = request_resource(res, &info->res_ide);
325 if (ret)
326 goto err_req_res_ide;
327
328 info->res_alt.start = res->start + CF_ALT_IDE_OFFSET;
329 info->res_alt.end = info->res_alt.start + CF_RES_SIZE - 1;
330 info->res_alt.name = "alt";
331 info->res_alt.flags = IORESOURCE_MEM;
332
333 ret = request_resource(res, &info->res_alt);
334 if (ret)
335 goto err_req_res_alt;
336
337 /* Setup non-timing elements of SMC */
338 info->smc.bus_width = 2; /* 16 bit data bus */
339 info->smc.nrd_controlled = 1; /* Sample data on rising edge of NRD */
340 info->smc.nwe_controlled = 0; /* Drive data on falling edge of NCS */
341 info->smc.nwait_mode = 3; /* NWAIT is in READY mode */
342 info->smc.byte_write = 0; /* Byte select access type */
343 info->smc.tdf_mode = 0; /* TDF optimization disabled */
344 info->smc.tdf_cycles = 0; /* No TDF wait cycles */
345
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +0100346 /* Setup SMC to ATA timing */
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200347 ret = pata_at32_setup_timing(dev, info, &initial_timing);
348 if (ret)
349 goto err_setup_timing;
350
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +0100351 /* Map ATA address space */
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200352 ret = -ENOMEM;
353 info->ide_addr = devm_ioremap(dev, info->res_ide.start, 16);
354 info->alt_addr = devm_ioremap(dev, info->res_alt.start, 16);
355 if (!info->ide_addr || !info->alt_addr)
356 goto err_ioremap;
357
358#ifdef DEBUG_BUS
359 pata_at32_debug_bus(dev, info);
360#endif
361
Kristoffer Nyborg Gregertsen1c20a492007-11-29 12:01:51 +0100362 /* Setup and register ATA device */
Kristoffer Nyborg Gregertsen7c9ef8e2007-08-08 16:57:08 +0200363 ret = pata_at32_init_one(dev, info);
364 if (ret)
365 goto err_ata_device;
366
367 return 0;
368
369 err_ata_device:
370 err_ioremap:
371 err_setup_timing:
372 release_resource(&info->res_alt);
373 err_req_res_alt:
374 release_resource(&info->res_ide);
375 err_req_res_ide:
376 kfree(info);
377
378 return ret;
379}
380
381static int __exit pata_at32_remove(struct platform_device *pdev)
382{
383 struct ata_host *host = platform_get_drvdata(pdev);
384 struct at32_ide_info *info;
385
386 if (!host)
387 return 0;
388
389 info = host->private_data;
390 ata_host_detach(host);
391
392 if (!info)
393 return 0;
394
395 release_resource(&info->res_ide);
396 release_resource(&info->res_alt);
397
398 kfree(info);
399
400 return 0;
401}
402
403static struct platform_driver pata_at32_driver = {
404 .remove = __exit_p(pata_at32_remove),
405 .driver = {
406 .name = "at32_ide",
407 .owner = THIS_MODULE,
408 },
409};
410
411static int __init pata_at32_init(void)
412{
413 return platform_driver_probe(&pata_at32_driver, pata_at32_probe);
414}
415
416static void __exit pata_at32_exit(void)
417{
418 platform_driver_unregister(&pata_at32_driver);
419}
420
421module_init(pata_at32_init);
422module_exit(pata_at32_exit);
423
424MODULE_LICENSE("GPL");
425MODULE_DESCRIPTION("AVR32 SMC/CFC PATA Driver");
426MODULE_AUTHOR("Kristoffer Nyborg Gregertsen <kngregertsen@norway.atmel.com>");
427MODULE_VERSION(DRV_VERSION);