2 * AVR32 SMC/CFC PATA Driver
4 * Copyright (C) 2007 Atmel Norway
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.
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 <linux/slab.h>
22 #include <scsi/scsi_host.h>
23 #include <linux/ata.h>
24 #include <linux/libata.h>
25 #include <linux/err.h>
28 #include <mach/board.h>
31 #define DRV_NAME "pata_at32"
32 #define DRV_VERSION "0.0.3"
35 * CompactFlash controller memory layout relative to the base address:
37 * Attribute memory: 0000 0000 -> 003f ffff
38 * Common memory: 0040 0000 -> 007f ffff
39 * I/O memory: 0080 0000 -> 00bf ffff
40 * True IDE Mode: 00c0 0000 -> 00df ffff
41 * Alt IDE Mode: 00e0 0000 -> 00ff ffff
43 * Only True IDE and Alt True IDE mode are needed for this driver.
45 * True IDE mode => CS0 = 0, CS1 = 1 (cmd, error, stat, etc)
46 * Alt True IDE mode => CS0 = 1, CS1 = 0 (ctl, alt_stat)
48 #define CF_IDE_OFFSET 0x00c00000
49 #define CF_ALT_IDE_OFFSET 0x00e00000
50 #define CF_RES_SIZE 2048
53 * Define DEBUG_BUS if you are doing debugging of your own EBI -> PATA
54 * adaptor with a logic analyzer or similar.
61 * Name | Mb/s | Min cycle time | Mask
62 * --------+-------+----------------+--------
63 * Mode 0 | 3.3 | 600 ns | 0x01
64 * Mode 1 | 5.2 | 383 ns | 0x03
65 * Mode 2 | 8.3 | 240 ns | 0x07
66 * Mode 3 | 11.1 | 180 ns | 0x0f
67 * Mode 4 | 16.7 | 120 ns | 0x1f
69 * Alter PIO_MASK below according to table to set maximal PIO mode.
76 * Struct containing private information about device.
78 struct at32_ide_info {
80 struct resource res_ide;
81 struct resource res_alt;
82 void __iomem *ide_addr;
83 void __iomem *alt_addr;
85 struct smc_config smc;
89 * Setup SMC for the given ATA timing.
91 static int pata_at32_setup_timing(struct device *dev,
92 struct at32_ide_info *info,
93 const struct ata_timing *ata)
95 struct smc_config *smc = &info->smc;
96 struct smc_timing timing;
101 memset(&timing, 0, sizeof(struct smc_timing));
103 /* Total cycle time */
104 timing.read_cycle = ata->cyc8b;
106 /* DIOR <= CFIOR timings */
107 timing.nrd_setup = ata->setup;
108 timing.nrd_pulse = ata->act8b;
109 timing.nrd_recover = ata->rec8b;
111 /* Convert nanosecond timing to clock cycles */
112 smc_set_timing(smc, &timing);
114 /* Add one extra cycle setup due to signal ring */
115 smc->nrd_setup = smc->nrd_setup + 1;
117 active = smc->nrd_setup + smc->nrd_pulse;
118 recover = smc->read_cycle - active;
120 /* Need at least two cycles recovery */
122 smc->read_cycle = active + 2;
124 /* (CS0, CS1, DIR, OE) <= (CFCE1, CFCE2, CFRNW, NCSX) timings */
125 smc->ncs_read_setup = 1;
126 smc->ncs_read_pulse = smc->read_cycle - 2;
128 /* Write timings same as read timings */
129 smc->write_cycle = smc->read_cycle;
130 smc->nwe_setup = smc->nrd_setup;
131 smc->nwe_pulse = smc->nrd_pulse;
132 smc->ncs_write_setup = smc->ncs_read_setup;
133 smc->ncs_write_pulse = smc->ncs_read_pulse;
135 /* Do some debugging output of ATA and SMC timings */
136 dev_dbg(dev, "ATA: C=%d S=%d P=%d R=%d\n",
137 ata->cyc8b, ata->setup, ata->act8b, ata->rec8b);
139 dev_dbg(dev, "SMC: C=%d S=%d P=%d NS=%d NP=%d\n",
140 smc->read_cycle, smc->nrd_setup, smc->nrd_pulse,
141 smc->ncs_read_setup, smc->ncs_read_pulse);
143 /* Finally, configure the SMC */
144 return smc_set_configuration(info->cs, smc);
148 * Procedures for libATA.
150 static void pata_at32_set_piomode(struct ata_port *ap, struct ata_device *adev)
152 struct ata_timing timing;
153 struct at32_ide_info *info = ap->host->private_data;
157 /* Compute ATA timing */
158 ret = ata_timing_compute(adev, adev->pio_mode, &timing, 1000, 0);
160 dev_warn(ap->dev, "Failed to compute ATA timing %d\n", ret);
164 /* Setup SMC to ATA timing */
165 ret = pata_at32_setup_timing(ap->dev, info, &timing);
167 dev_warn(ap->dev, "Failed to setup ATA timing %d\n", ret);
172 static struct scsi_host_template at32_sht = {
173 ATA_PIO_SHT(DRV_NAME),
176 static struct ata_port_operations at32_port_ops = {
177 .inherits = &ata_sff_port_ops,
178 .cable_detect = ata_cable_40wire,
179 .set_piomode = pata_at32_set_piomode,
182 static int __init pata_at32_init_one(struct device *dev,
183 struct at32_ide_info *info)
185 struct ata_host *host;
188 host = ata_host_alloc(dev, 1);
194 /* Setup ATA bindings */
195 ap->ops = &at32_port_ops;
196 ap->pio_mask = PIO_MASK;
197 ap->flags |= ATA_FLAG_SLAVE_POSS;
200 * Since all 8-bit taskfile transfers has to go on the lower
201 * byte of the data bus and there is a bug in the SMC that
202 * makes it impossible to alter the bus width during runtime,
203 * we need to hardwire the address signals as follows:
205 * A_IDE(2:0) <= A_EBI(3:1)
207 * This makes all addresses on the EBI even, thus all data
208 * will be on the lower byte of the data bus. All addresses
209 * used by libATA need to be altered according to this.
211 ap->ioaddr.altstatus_addr = info->alt_addr + (0x06 << 1);
212 ap->ioaddr.ctl_addr = info->alt_addr + (0x06 << 1);
214 ap->ioaddr.data_addr = info->ide_addr + (ATA_REG_DATA << 1);
215 ap->ioaddr.error_addr = info->ide_addr + (ATA_REG_ERR << 1);
216 ap->ioaddr.feature_addr = info->ide_addr + (ATA_REG_FEATURE << 1);
217 ap->ioaddr.nsect_addr = info->ide_addr + (ATA_REG_NSECT << 1);
218 ap->ioaddr.lbal_addr = info->ide_addr + (ATA_REG_LBAL << 1);
219 ap->ioaddr.lbam_addr = info->ide_addr + (ATA_REG_LBAM << 1);
220 ap->ioaddr.lbah_addr = info->ide_addr + (ATA_REG_LBAH << 1);
221 ap->ioaddr.device_addr = info->ide_addr + (ATA_REG_DEVICE << 1);
222 ap->ioaddr.status_addr = info->ide_addr + (ATA_REG_STATUS << 1);
223 ap->ioaddr.command_addr = info->ide_addr + (ATA_REG_CMD << 1);
225 /* Set info as private data of ATA host */
226 host->private_data = info;
228 /* Register ATA device and return */
229 return ata_host_activate(host, info->irq, ata_sff_interrupt,
230 IRQF_SHARED | IRQF_TRIGGER_RISING,
235 * This function may come in handy for people analyzing their own
236 * EBI -> PATA adaptors.
240 static void __init pata_at32_debug_bus(struct device *dev,
241 struct at32_ide_info *info)
248 /* Write 8-bit values (registers) */
249 iowrite8(d1, info->alt_addr + (0x06 << 1));
250 iowrite8(d2, info->alt_addr + (0x06 << 1));
252 for (i = 0; i < 8; i++) {
253 iowrite8(d1, info->ide_addr + (i << 1));
254 iowrite8(d2, info->ide_addr + (i << 1));
257 /* Write 16 bit values (data) */
258 iowrite16(d1, info->ide_addr);
259 iowrite16(d1 << 8, info->ide_addr);
261 iowrite16(d1, info->ide_addr);
262 iowrite16(d1 << 8, info->ide_addr);
267 static int __init pata_at32_probe(struct platform_device *pdev)
269 const struct ata_timing initial_timing =
270 {XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0};
272 struct device *dev = &pdev->dev;
273 struct at32_ide_info *info;
274 struct ide_platform_data *board = pdev->dev.platform_data;
275 struct resource *res;
283 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
288 irq = platform_get_irq(pdev, 0);
292 /* Setup struct containing private information */
293 info = kzalloc(sizeof(struct at32_ide_info), GFP_KERNEL);
298 info->cs = board->cs;
300 /* Request memory resources */
301 info->res_ide.start = res->start + CF_IDE_OFFSET;
302 info->res_ide.end = info->res_ide.start + CF_RES_SIZE - 1;
303 info->res_ide.name = "ide";
304 info->res_ide.flags = IORESOURCE_MEM;
306 ret = request_resource(res, &info->res_ide);
308 goto err_req_res_ide;
310 info->res_alt.start = res->start + CF_ALT_IDE_OFFSET;
311 info->res_alt.end = info->res_alt.start + CF_RES_SIZE - 1;
312 info->res_alt.name = "alt";
313 info->res_alt.flags = IORESOURCE_MEM;
315 ret = request_resource(res, &info->res_alt);
317 goto err_req_res_alt;
319 /* Setup non-timing elements of SMC */
320 info->smc.bus_width = 2; /* 16 bit data bus */
321 info->smc.nrd_controlled = 1; /* Sample data on rising edge of NRD */
322 info->smc.nwe_controlled = 0; /* Drive data on falling edge of NCS */
323 info->smc.nwait_mode = 3; /* NWAIT is in READY mode */
324 info->smc.byte_write = 0; /* Byte select access type */
325 info->smc.tdf_mode = 0; /* TDF optimization disabled */
326 info->smc.tdf_cycles = 0; /* No TDF wait cycles */
328 /* Setup SMC to ATA timing */
329 ret = pata_at32_setup_timing(dev, info, &initial_timing);
331 goto err_setup_timing;
333 /* Map ATA address space */
335 info->ide_addr = devm_ioremap(dev, info->res_ide.start, 16);
336 info->alt_addr = devm_ioremap(dev, info->res_alt.start, 16);
337 if (!info->ide_addr || !info->alt_addr)
341 pata_at32_debug_bus(dev, info);
344 /* Setup and register ATA device */
345 ret = pata_at32_init_one(dev, info);
354 release_resource(&info->res_alt);
356 release_resource(&info->res_ide);
363 static int __exit pata_at32_remove(struct platform_device *pdev)
365 struct ata_host *host = platform_get_drvdata(pdev);
366 struct at32_ide_info *info;
371 info = host->private_data;
372 ata_host_detach(host);
377 release_resource(&info->res_ide);
378 release_resource(&info->res_alt);
385 /* work with hotplug and coldplug */
386 MODULE_ALIAS("platform:at32_ide");
388 static struct platform_driver pata_at32_driver = {
389 .remove = __exit_p(pata_at32_remove),
392 .owner = THIS_MODULE,
396 module_platform_driver_probe(pata_at32_driver, pata_at32_probe);
398 MODULE_LICENSE("GPL");
399 MODULE_DESCRIPTION("AVR32 SMC/CFC PATA Driver");
400 MODULE_AUTHOR("Kristoffer Nyborg Gregertsen <kngregertsen@norway.atmel.com>");
401 MODULE_VERSION(DRV_VERSION);