KVM: Avoid using x86_emulate_ctxt.vcpu
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ata / pata_at91.c
bloba5fdbdcb0faf2ed8daa5c4fa77767da6cc3fa7ea
1 /*
2 * PATA driver for AT91SAM9260 Static Memory Controller
3 * with CompactFlash interface in True IDE mode
5 * Copyright (C) 2009 Matyukevich Sergey
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
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 {
44 unsigned long mode;
45 unsigned int cs;
47 struct clk *mck;
49 void __iomem *ide_addr;
50 void __iomem *alt_addr;
53 static 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
66 static unsigned long calc_mck_cycles(unsigned long ns, unsigned long mck_hz)
68 unsigned long mul;
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 =
77 mul = (mck_hz / 10000) << 16;
78 mul /= 100000;
80 return (ns * mul + 65536) >> 16; /* rounding */
83 static void set_smc_mode(struct at91_ide_info *info)
85 at91_sys_write(AT91_SMC_MODE(info->cs), info->mode);
86 return;
89 static void set_smc_timing(struct device *dev,
90 struct at91_ide_info *info, const struct ata_timing *ata)
92 unsigned long read_cycle, write_cycle, active, recover;
93 unsigned long nrd_setup, nrd_pulse, nrd_recover;
94 unsigned long nwe_setup, nwe_pulse;
96 unsigned long ncs_write_setup, ncs_write_pulse;
97 unsigned long ncs_read_setup, ncs_read_pulse;
99 unsigned long mck_hz;
101 read_cycle = ata->cyc8b;
102 nrd_setup = ata->setup;
103 nrd_pulse = ata->act8b;
104 nrd_recover = ata->rec8b;
106 mck_hz = clk_get_rate(info->mck);
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);
113 active = nrd_setup + nrd_pulse;
114 recover = read_cycle - active;
116 /* Need at least two cycles recovery */
117 if (recover < 2)
118 read_cycle = active + 2;
120 /* (CS0, CS1, DIR, OE) <= (CFCE1, CFCE2, CFRNW, NCSX) timings */
121 ncs_read_setup = 1;
122 ncs_read_pulse = read_cycle - 2;
123 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);
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;
136 dev_dbg(dev, "ATA timings: nrd_setup = %lu nrd_pulse = %lu nrd_cycle = %lu\n",
137 nrd_setup, nrd_pulse, read_cycle);
138 dev_dbg(dev, "ATA timings: nwe_setup = %lu nwe_pulse = %lu nwe_cycle = %lu\n",
139 nwe_setup, nwe_pulse, write_cycle);
140 dev_dbg(dev, "ATA timings: ncs_read_setup = %lu ncs_read_pulse = %lu\n",
141 ncs_read_setup, ncs_read_pulse);
142 dev_dbg(dev, "ATA timings: ncs_write_setup = %lu ncs_write_pulse = %lu\n",
143 ncs_write_setup, ncs_write_pulse);
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));
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));
157 at91_sys_write(AT91_SMC_CYCLE(info->cs),
158 AT91_SMC_NWECYCLE_(write_cycle) |
159 AT91_SMC_NRDCYCLE_(read_cycle));
161 return;
164 static void pata_at91_set_piomode(struct ata_port *ap, struct ata_device *adev)
166 struct at91_ide_info *info = ap->host->private_data;
167 struct ata_timing timing;
168 int ret;
170 /* Compute ATA timing and set it to SMC */
171 ret = ata_timing_compute(adev, adev->pio_mode, &timing, 1000, 0);
172 if (ret) {
173 dev_warn(ap->dev, "Failed to compute ATA timing %d, "
174 "set PIO_0 timing\n", ret);
175 set_smc_timing(ap->dev, info, &initial_timing);
176 } else {
177 set_smc_timing(ap->dev, info, &timing);
180 /* Setup SMC mode */
181 set_smc_mode(info);
183 return;
186 static unsigned int pata_at91_data_xfer_noirq(struct ata_device *dev,
187 unsigned char *buf, unsigned int buflen, int rw)
189 struct at91_ide_info *info = dev->link->ap->host->private_data;
190 unsigned int consumed;
191 unsigned long flags;
192 unsigned int mode;
194 local_irq_save(flags);
195 mode = at91_sys_read(AT91_SMC_MODE(info->cs));
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);
201 consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
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);
207 local_irq_restore(flags);
208 return consumed;
211 static struct scsi_host_template pata_at91_sht = {
212 ATA_PIO_SHT(DRV_NAME),
215 static struct ata_port_operations pata_at91_port_ops = {
216 .inherits = &ata_sff_port_ops,
218 .sff_data_xfer = pata_at91_data_xfer_noirq,
219 .set_piomode = pata_at91_set_piomode,
220 .cable_detect = ata_cable_40wire,
223 static int __devinit pata_at91_probe(struct platform_device *pdev)
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;
232 int irq_flags = 0;
233 int irq = 0;
234 int ret;
236 /* get platform resources: IO/CTL memories and irq/rst pins */
238 if (pdev->num_resources != 1) {
239 dev_err(&pdev->dev, "invalid number of resources\n");
240 return -EINVAL;
243 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
245 if (!mem_res) {
246 dev_err(dev, "failed to get mem resource\n");
247 return -EINVAL;
250 irq = board->irq_pin;
252 /* init ata host */
254 host = ata_host_alloc(dev, 1);
256 if (!host)
257 return -ENOMEM;
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;
264 if (!irq) {
265 ap->flags |= ATA_FLAG_PIO_POLLING;
266 ata_port_desc(ap, "no IRQ, using PIO polling");
269 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
271 if (!info) {
272 dev_err(dev, "failed to allocate memory for private data\n");
273 return -ENOMEM;
276 info->mck = clk_get(NULL, "mck");
278 if (IS_ERR(info->mck)) {
279 dev_err(dev, "failed to get access to mck clock\n");
280 return -ENODEV;
283 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);
288 info->ide_addr = devm_ioremap(dev,
289 mem_res->start + CF_IDE_OFFSET, CF_IDE_RES_SIZE);
291 if (!info->ide_addr) {
292 dev_err(dev, "failed to map IO base\n");
293 ret = -ENOMEM;
294 goto err_put;
297 info->alt_addr = devm_ioremap(dev,
298 mem_res->start + CF_ALT_IDE_OFFSET, CF_IDE_RES_SIZE);
300 if (!info->alt_addr) {
301 dev_err(dev, "failed to map CTL base\n");
302 ret = -ENOMEM;
303 goto err_put;
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;
310 ata_sff_std_ports(&ap->ioaddr);
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);
316 host->private_data = info;
318 return ata_host_activate(host, irq ? gpio_to_irq(irq) : 0,
319 irq ? ata_sff_interrupt : NULL,
320 irq_flags, &pata_at91_sht);
322 err_put:
323 clk_put(info->mck);
324 return ret;
327 static int __devexit pata_at91_remove(struct platform_device *pdev)
329 struct ata_host *host = dev_get_drvdata(&pdev->dev);
330 struct at91_ide_info *info;
332 if (!host)
333 return 0;
334 info = host->private_data;
336 ata_host_detach(host);
338 if (!info)
339 return 0;
341 clk_put(info->mck);
343 return 0;
346 static 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,
355 static int __init pata_at91_init(void)
357 return platform_driver_register(&pata_at91_driver);
360 static void __exit pata_at91_exit(void)
362 platform_driver_unregister(&pata_at91_driver);
366 module_init(pata_at91_init);
367 module_exit(pata_at91_exit);
370 MODULE_LICENSE("GPL");
371 MODULE_DESCRIPTION("Driver for CF in True IDE mode on AT91SAM9260 SoC");
372 MODULE_AUTHOR("Matyukevich Sergey");
373 MODULE_VERSION(DRV_VERSION);