blob: 4117b618a9d908b9ceaf8670f1d57c28460aaf8a [file] [log] [blame]
Sylvain Munaut155d2912006-12-08 00:14:16 +01001/*
2 * drivers/ata/pata_mpc52xx.c
3 *
4 * libata driver for the Freescale MPC52xx on-chip IDE interface
5 *
6 * Copyright (C) 2006 Sylvain Munaut <tnt@246tNt.com>
7 * Copyright (C) 2003 Mipsys - Benjamin Herrenschmidt
8 *
9 * This file is licensed under the terms of the GNU General Public License
10 * version 2. This program is licensed "as is" without any warranty of any
11 * kind, whether express or implied.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/delay.h>
18#include <linux/libata.h>
19
Sylvain Munaut155d2912006-12-08 00:14:16 +010020#include <asm/types.h>
21#include <asm/prom.h>
22#include <asm/of_platform.h>
23#include <asm/mpc52xx.h>
24
25
26#define DRV_NAME "mpc52xx_ata"
Jeff Garzik2a3103c2007-08-31 04:54:06 -040027#define DRV_VERSION "0.1.2"
Sylvain Munaut155d2912006-12-08 00:14:16 +010028
29
30/* Private structures used by the driver */
31struct mpc52xx_ata_timings {
32 u32 pio1;
33 u32 pio2;
34};
35
36struct mpc52xx_ata_priv {
37 unsigned int ipb_period;
38 struct mpc52xx_ata __iomem * ata_regs;
39 int ata_irq;
40 struct mpc52xx_ata_timings timings[2];
41 int csel;
42};
43
44
45/* ATAPI-4 PIO specs (in ns) */
46static const int ataspec_t0[5] = {600, 383, 240, 180, 120};
47static const int ataspec_t1[5] = { 70, 50, 30, 30, 25};
48static const int ataspec_t2_8[5] = {290, 290, 290, 80, 70};
49static const int ataspec_t2_16[5] = {165, 125, 100, 80, 70};
50static const int ataspec_t2i[5] = { 0, 0, 0, 70, 25};
51static const int ataspec_t4[5] = { 30, 20, 15, 10, 10};
52static const int ataspec_ta[5] = { 35, 35, 35, 35, 35};
53
54#define CALC_CLKCYC(c,v) ((((v)+(c)-1)/(c)))
55
56
57/* Bit definitions inside the registers */
58#define MPC52xx_ATA_HOSTCONF_SMR 0x80000000UL /* State machine reset */
59#define MPC52xx_ATA_HOSTCONF_FR 0x40000000UL /* FIFO Reset */
60#define MPC52xx_ATA_HOSTCONF_IE 0x02000000UL /* Enable interrupt in PIO */
61#define MPC52xx_ATA_HOSTCONF_IORDY 0x01000000UL /* Drive supports IORDY protocol */
62
63#define MPC52xx_ATA_HOSTSTAT_TIP 0x80000000UL /* Transaction in progress */
64#define MPC52xx_ATA_HOSTSTAT_UREP 0x40000000UL /* UDMA Read Extended Pause */
65#define MPC52xx_ATA_HOSTSTAT_RERR 0x02000000UL /* Read Error */
66#define MPC52xx_ATA_HOSTSTAT_WERR 0x01000000UL /* Write Error */
67
68#define MPC52xx_ATA_FIFOSTAT_EMPTY 0x01 /* FIFO Empty */
69
70#define MPC52xx_ATA_DMAMODE_WRITE 0x01 /* Write DMA */
71#define MPC52xx_ATA_DMAMODE_READ 0x02 /* Read DMA */
72#define MPC52xx_ATA_DMAMODE_UDMA 0x04 /* UDMA enabled */
73#define MPC52xx_ATA_DMAMODE_IE 0x08 /* Enable drive interrupt to CPU in DMA mode */
74#define MPC52xx_ATA_DMAMODE_FE 0x10 /* FIFO Flush enable in Rx mode */
75#define MPC52xx_ATA_DMAMODE_FR 0x20 /* FIFO Reset */
76#define MPC52xx_ATA_DMAMODE_HUT 0x40 /* Host UDMA burst terminate */
77
78
79/* Structure of the hardware registers */
80struct mpc52xx_ata {
81
82 /* Host interface registers */
83 u32 config; /* ATA + 0x00 Host configuration */
84 u32 host_status; /* ATA + 0x04 Host controller status */
85 u32 pio1; /* ATA + 0x08 PIO Timing 1 */
86 u32 pio2; /* ATA + 0x0c PIO Timing 2 */
87 u32 mdma1; /* ATA + 0x10 MDMA Timing 1 */
88 u32 mdma2; /* ATA + 0x14 MDMA Timing 2 */
89 u32 udma1; /* ATA + 0x18 UDMA Timing 1 */
90 u32 udma2; /* ATA + 0x1c UDMA Timing 2 */
91 u32 udma3; /* ATA + 0x20 UDMA Timing 3 */
92 u32 udma4; /* ATA + 0x24 UDMA Timing 4 */
93 u32 udma5; /* ATA + 0x28 UDMA Timing 5 */
94 u32 share_cnt; /* ATA + 0x2c ATA share counter */
95 u32 reserved0[3];
96
97 /* FIFO registers */
98 u32 fifo_data; /* ATA + 0x3c */
99 u8 fifo_status_frame; /* ATA + 0x40 */
100 u8 fifo_status; /* ATA + 0x41 */
101 u16 reserved7[1];
102 u8 fifo_control; /* ATA + 0x44 */
103 u8 reserved8[5];
104 u16 fifo_alarm; /* ATA + 0x4a */
105 u16 reserved9;
106 u16 fifo_rdp; /* ATA + 0x4e */
107 u16 reserved10;
108 u16 fifo_wrp; /* ATA + 0x52 */
109 u16 reserved11;
110 u16 fifo_lfrdp; /* ATA + 0x56 */
111 u16 reserved12;
112 u16 fifo_lfwrp; /* ATA + 0x5a */
113
114 /* Drive TaskFile registers */
115 u8 tf_control; /* ATA + 0x5c TASKFILE Control/Alt Status */
116 u8 reserved13[3];
117 u16 tf_data; /* ATA + 0x60 TASKFILE Data */
118 u16 reserved14;
119 u8 tf_features; /* ATA + 0x64 TASKFILE Features/Error */
120 u8 reserved15[3];
121 u8 tf_sec_count; /* ATA + 0x68 TASKFILE Sector Count */
122 u8 reserved16[3];
123 u8 tf_sec_num; /* ATA + 0x6c TASKFILE Sector Number */
124 u8 reserved17[3];
125 u8 tf_cyl_low; /* ATA + 0x70 TASKFILE Cylinder Low */
126 u8 reserved18[3];
127 u8 tf_cyl_high; /* ATA + 0x74 TASKFILE Cylinder High */
128 u8 reserved19[3];
129 u8 tf_dev_head; /* ATA + 0x78 TASKFILE Device/Head */
130 u8 reserved20[3];
131 u8 tf_command; /* ATA + 0x7c TASKFILE Command/Status */
132 u8 dma_mode; /* ATA + 0x7d ATA Host DMA Mode configuration */
133 u8 reserved21[2];
134};
135
136
137/* ======================================================================== */
138/* Aux fns */
139/* ======================================================================== */
140
141
142/* MPC52xx low level hw control */
143
144static int
145mpc52xx_ata_compute_pio_timings(struct mpc52xx_ata_priv *priv, int dev, int pio)
146{
147 struct mpc52xx_ata_timings *timing = &priv->timings[dev];
148 unsigned int ipb_period = priv->ipb_period;
149 unsigned int t0, t1, t2_8, t2_16, t2i, t4, ta;
150
151 if ((pio<0) || (pio>4))
152 return -EINVAL;
153
154 t0 = CALC_CLKCYC(ipb_period, 1000 * ataspec_t0[pio]);
155 t1 = CALC_CLKCYC(ipb_period, 1000 * ataspec_t1[pio]);
156 t2_8 = CALC_CLKCYC(ipb_period, 1000 * ataspec_t2_8[pio]);
157 t2_16 = CALC_CLKCYC(ipb_period, 1000 * ataspec_t2_16[pio]);
158 t2i = CALC_CLKCYC(ipb_period, 1000 * ataspec_t2i[pio]);
159 t4 = CALC_CLKCYC(ipb_period, 1000 * ataspec_t4[pio]);
160 ta = CALC_CLKCYC(ipb_period, 1000 * ataspec_ta[pio]);
161
162 timing->pio1 = (t0 << 24) | (t2_8 << 16) | (t2_16 << 8) | (t2i);
163 timing->pio2 = (t4 << 24) | (t1 << 16) | (ta << 8);
164
165 return 0;
166}
167
168static void
169mpc52xx_ata_apply_timings(struct mpc52xx_ata_priv *priv, int device)
170{
171 struct mpc52xx_ata __iomem *regs = priv->ata_regs;
172 struct mpc52xx_ata_timings *timing = &priv->timings[device];
173
174 out_be32(&regs->pio1, timing->pio1);
175 out_be32(&regs->pio2, timing->pio2);
176 out_be32(&regs->mdma1, 0);
177 out_be32(&regs->mdma2, 0);
178 out_be32(&regs->udma1, 0);
179 out_be32(&regs->udma2, 0);
180 out_be32(&regs->udma3, 0);
181 out_be32(&regs->udma4, 0);
182 out_be32(&regs->udma5, 0);
183
184 priv->csel = device;
185}
186
187static int
188mpc52xx_ata_hw_init(struct mpc52xx_ata_priv *priv)
189{
190 struct mpc52xx_ata __iomem *regs = priv->ata_regs;
191 int tslot;
192
193 /* Clear share_cnt (all sample code do this ...) */
194 out_be32(&regs->share_cnt, 0);
195
196 /* Configure and reset host */
197 out_be32(&regs->config,
198 MPC52xx_ATA_HOSTCONF_IE |
199 MPC52xx_ATA_HOSTCONF_IORDY |
200 MPC52xx_ATA_HOSTCONF_SMR |
201 MPC52xx_ATA_HOSTCONF_FR);
202
203 udelay(10);
204
205 out_be32(&regs->config,
206 MPC52xx_ATA_HOSTCONF_IE |
207 MPC52xx_ATA_HOSTCONF_IORDY);
208
209 /* Set the time slot to 1us */
210 tslot = CALC_CLKCYC(priv->ipb_period, 1000000);
211 out_be32(&regs->share_cnt, tslot << 16 );
212
213 /* Init timings to PIO0 */
214 memset(priv->timings, 0x00, 2*sizeof(struct mpc52xx_ata_timings));
215
216 mpc52xx_ata_compute_pio_timings(priv, 0, 0);
217 mpc52xx_ata_compute_pio_timings(priv, 1, 0);
218
219 mpc52xx_ata_apply_timings(priv, 0);
220
221 return 0;
222}
223
224
225/* ======================================================================== */
226/* libata driver */
227/* ======================================================================== */
228
229static void
230mpc52xx_ata_set_piomode(struct ata_port *ap, struct ata_device *adev)
231{
232 struct mpc52xx_ata_priv *priv = ap->host->private_data;
233 int pio, rv;
234
235 pio = adev->pio_mode - XFER_PIO_0;
236
237 rv = mpc52xx_ata_compute_pio_timings(priv, adev->devno, pio);
238
239 if (rv) {
240 printk(KERN_ERR DRV_NAME
241 ": Trying to select invalid PIO mode %d\n", pio);
242 return;
243 }
244
245 mpc52xx_ata_apply_timings(priv, adev->devno);
246}
247static void
248mpc52xx_ata_dev_select(struct ata_port *ap, unsigned int device)
249{
250 struct mpc52xx_ata_priv *priv = ap->host->private_data;
251
252 if (device != priv->csel)
253 mpc52xx_ata_apply_timings(priv, device);
254
255 ata_std_dev_select(ap,device);
256}
257
258static void
259mpc52xx_ata_error_handler(struct ata_port *ap)
260{
261 ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, NULL,
262 ata_std_postreset);
263}
264
265
266
267static struct scsi_host_template mpc52xx_ata_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900268 ATA_PIO_SHT(DRV_NAME),
Sylvain Munaut155d2912006-12-08 00:14:16 +0100269};
270
271static struct ata_port_operations mpc52xx_ata_port_ops = {
Sylvain Munaut155d2912006-12-08 00:14:16 +0100272 .set_piomode = mpc52xx_ata_set_piomode,
273 .dev_select = mpc52xx_ata_dev_select,
274 .tf_load = ata_tf_load,
275 .tf_read = ata_tf_read,
276 .check_status = ata_check_status,
277 .exec_command = ata_exec_command,
278 .freeze = ata_bmdma_freeze,
279 .thaw = ata_bmdma_thaw,
280 .error_handler = mpc52xx_ata_error_handler,
Jeff Garzika0fcdc02007-03-09 07:24:15 -0500281 .cable_detect = ata_cable_40wire,
Sylvain Munaut155d2912006-12-08 00:14:16 +0100282 .qc_prep = ata_qc_prep,
283 .qc_issue = ata_qc_issue_prot,
Tejun Heo0d5ff562007-02-01 15:06:36 +0900284 .data_xfer = ata_data_xfer,
Tejun Heo358f9a72008-03-25 12:22:47 +0900285 .irq_clear = ata_noop_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900286 .irq_on = ata_irq_on,
Tejun Heo6bd99b42008-03-25 12:22:48 +0900287 .port_start = ata_sff_port_start,
Sylvain Munaut155d2912006-12-08 00:14:16 +0100288};
289
Sylvain Munaut155d2912006-12-08 00:14:16 +0100290static int __devinit
Tejun Heocbcdd872007-08-18 13:14:55 +0900291mpc52xx_ata_init_one(struct device *dev, struct mpc52xx_ata_priv *priv,
292 unsigned long raw_ata_regs)
Sylvain Munaut155d2912006-12-08 00:14:16 +0100293{
Tejun Heo5d728822007-04-17 23:44:08 +0900294 struct ata_host *host;
295 struct ata_port *ap;
296 struct ata_ioports *aio;
Sylvain Munaut155d2912006-12-08 00:14:16 +0100297
Tejun Heo5d728822007-04-17 23:44:08 +0900298 host = ata_host_alloc(dev, 1);
299 if (!host)
300 return -ENOMEM;
Sylvain Munaut155d2912006-12-08 00:14:16 +0100301
Tejun Heo5d728822007-04-17 23:44:08 +0900302 ap = host->ports[0];
303 ap->flags |= ATA_FLAG_SLAVE_POSS;
304 ap->pio_mask = 0x1f; /* Up to PIO4 */
305 ap->mwdma_mask = 0x00; /* No MWDMA */
306 ap->udma_mask = 0x00; /* No UDMA */
307 ap->ops = &mpc52xx_ata_port_ops;
308 host->private_data = priv;
309
310 aio = &ap->ioaddr;
Al Viro89952d12007-03-14 09:17:59 +0000311 aio->cmd_addr = NULL; /* Don't have a classic reg block */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900312 aio->altstatus_addr = &priv->ata_regs->tf_control;
313 aio->ctl_addr = &priv->ata_regs->tf_control;
314 aio->data_addr = &priv->ata_regs->tf_data;
315 aio->error_addr = &priv->ata_regs->tf_features;
316 aio->feature_addr = &priv->ata_regs->tf_features;
317 aio->nsect_addr = &priv->ata_regs->tf_sec_count;
318 aio->lbal_addr = &priv->ata_regs->tf_sec_num;
319 aio->lbam_addr = &priv->ata_regs->tf_cyl_low;
320 aio->lbah_addr = &priv->ata_regs->tf_cyl_high;
321 aio->device_addr = &priv->ata_regs->tf_dev_head;
322 aio->status_addr = &priv->ata_regs->tf_command;
323 aio->command_addr = &priv->ata_regs->tf_command;
Sylvain Munaut155d2912006-12-08 00:14:16 +0100324
Tejun Heocbcdd872007-08-18 13:14:55 +0900325 ata_port_desc(ap, "ata_regs 0x%lx", raw_ata_regs);
326
Tejun Heo5d728822007-04-17 23:44:08 +0900327 /* activate host */
328 return ata_host_activate(host, priv->ata_irq, ata_interrupt, 0,
329 &mpc52xx_ata_sht);
Sylvain Munaut155d2912006-12-08 00:14:16 +0100330}
331
332static struct mpc52xx_ata_priv *
333mpc52xx_ata_remove_one(struct device *dev)
334{
335 struct ata_host *host = dev_get_drvdata(dev);
336 struct mpc52xx_ata_priv *priv = host->private_data;
337
Tejun Heo24dc5f32007-01-20 16:00:28 +0900338 ata_host_detach(host);
Sylvain Munaut155d2912006-12-08 00:14:16 +0100339
340 return priv;
341}
342
343
344/* ======================================================================== */
345/* OF Platform driver */
346/* ======================================================================== */
347
348static int __devinit
349mpc52xx_ata_probe(struct of_device *op, const struct of_device_id *match)
350{
351 unsigned int ipb_freq;
352 struct resource res_mem;
Alan Cox0eaea362008-01-19 15:48:59 +0000353 int ata_irq;
Tejun Heo24dc5f32007-01-20 16:00:28 +0900354 struct mpc52xx_ata __iomem *ata_regs;
355 struct mpc52xx_ata_priv *priv;
Sylvain Munaut155d2912006-12-08 00:14:16 +0100356 int rv;
357
358 /* Get ipb frequency */
359 ipb_freq = mpc52xx_find_ipb_freq(op->node);
360 if (!ipb_freq) {
361 printk(KERN_ERR DRV_NAME ": "
362 "Unable to find IPB Bus frequency\n" );
363 return -ENODEV;
364 }
365
366 /* Get IRQ and register */
367 rv = of_address_to_resource(op->node, 0, &res_mem);
368 if (rv) {
369 printk(KERN_ERR DRV_NAME ": "
370 "Error while parsing device node resource\n" );
371 return rv;
372 }
373
374 ata_irq = irq_of_parse_and_map(op->node, 0);
375 if (ata_irq == NO_IRQ) {
376 printk(KERN_ERR DRV_NAME ": "
377 "Error while mapping the irq\n");
378 return -EINVAL;
379 }
380
381 /* Request mem region */
Tejun Heo24dc5f32007-01-20 16:00:28 +0900382 if (!devm_request_mem_region(&op->dev, res_mem.start,
383 sizeof(struct mpc52xx_ata), DRV_NAME)) {
Sylvain Munaut155d2912006-12-08 00:14:16 +0100384 printk(KERN_ERR DRV_NAME ": "
385 "Error while requesting mem region\n");
Tejun Heo24dc5f32007-01-20 16:00:28 +0900386 rv = -EBUSY;
387 goto err;
Sylvain Munaut155d2912006-12-08 00:14:16 +0100388 }
389
390 /* Remap registers */
Tejun Heo24dc5f32007-01-20 16:00:28 +0900391 ata_regs = devm_ioremap(&op->dev, res_mem.start,
392 sizeof(struct mpc52xx_ata));
Sylvain Munaut155d2912006-12-08 00:14:16 +0100393 if (!ata_regs) {
394 printk(KERN_ERR DRV_NAME ": "
395 "Error while mapping register set\n");
396 rv = -ENOMEM;
397 goto err;
398 }
399
400 /* Prepare our private structure */
Tejun Heo24dc5f32007-01-20 16:00:28 +0900401 priv = devm_kzalloc(&op->dev, sizeof(struct mpc52xx_ata_priv),
402 GFP_ATOMIC);
Sylvain Munaut155d2912006-12-08 00:14:16 +0100403 if (!priv) {
404 printk(KERN_ERR DRV_NAME ": "
405 "Error while allocating private structure\n");
406 rv = -ENOMEM;
407 goto err;
408 }
409
410 priv->ipb_period = 1000000000 / (ipb_freq / 1000);
411 priv->ata_regs = ata_regs;
412 priv->ata_irq = ata_irq;
413 priv->csel = -1;
414
415 /* Init the hw */
416 rv = mpc52xx_ata_hw_init(priv);
417 if (rv) {
418 printk(KERN_ERR DRV_NAME ": Error during HW init\n");
419 goto err;
420 }
421
422 /* Register ourselves to libata */
Tejun Heocbcdd872007-08-18 13:14:55 +0900423 rv = mpc52xx_ata_init_one(&op->dev, priv, res_mem.start);
Sylvain Munaut155d2912006-12-08 00:14:16 +0100424 if (rv) {
425 printk(KERN_ERR DRV_NAME ": "
426 "Error while registering to ATA layer\n");
427 return rv;
428 }
429
430 /* Done */
431 return 0;
432
433 /* Error path */
434err:
Sylvain Munaut155d2912006-12-08 00:14:16 +0100435 irq_dispose_mapping(ata_irq);
Sylvain Munaut155d2912006-12-08 00:14:16 +0100436 return rv;
437}
438
439static int
440mpc52xx_ata_remove(struct of_device *op)
441{
442 struct mpc52xx_ata_priv *priv;
Sylvain Munaut155d2912006-12-08 00:14:16 +0100443
Sylvain Munaut155d2912006-12-08 00:14:16 +0100444 priv = mpc52xx_ata_remove_one(&op->dev);
Sylvain Munaut155d2912006-12-08 00:14:16 +0100445 irq_dispose_mapping(priv->ata_irq);
446
Sylvain Munaut155d2912006-12-08 00:14:16 +0100447 return 0;
448}
449
450
451#ifdef CONFIG_PM
452
453static int
454mpc52xx_ata_suspend(struct of_device *op, pm_message_t state)
455{
Domen Puncer35142dd2007-07-03 10:27:38 +0200456 struct ata_host *host = dev_get_drvdata(&op->dev);
457
458 return ata_host_suspend(host, state);
Sylvain Munaut155d2912006-12-08 00:14:16 +0100459}
460
461static int
462mpc52xx_ata_resume(struct of_device *op)
463{
Domen Puncer35142dd2007-07-03 10:27:38 +0200464 struct ata_host *host = dev_get_drvdata(&op->dev);
465 struct mpc52xx_ata_priv *priv = host->private_data;
466 int rv;
467
468 rv = mpc52xx_ata_hw_init(priv);
469 if (rv) {
470 printk(KERN_ERR DRV_NAME ": Error during HW init\n");
471 return rv;
472 }
473
474 ata_host_resume(host);
475
476 return 0;
Sylvain Munaut155d2912006-12-08 00:14:16 +0100477}
478
479#endif
480
481
482static struct of_device_id mpc52xx_ata_of_match[] = {
Grant Likely66ffbe42008-01-24 22:25:31 -0700483 { .compatible = "fsl,mpc5200-ata", },
484 { .compatible = "mpc5200-ata", },
Sylvain Munaut155d2912006-12-08 00:14:16 +0100485 {},
486};
487
488
489static struct of_platform_driver mpc52xx_ata_of_platform_driver = {
490 .owner = THIS_MODULE,
491 .name = DRV_NAME,
492 .match_table = mpc52xx_ata_of_match,
493 .probe = mpc52xx_ata_probe,
494 .remove = mpc52xx_ata_remove,
495#ifdef CONFIG_PM
496 .suspend = mpc52xx_ata_suspend,
497 .resume = mpc52xx_ata_resume,
498#endif
499 .driver = {
500 .name = DRV_NAME,
501 .owner = THIS_MODULE,
502 },
503};
504
505
506/* ======================================================================== */
507/* Module */
508/* ======================================================================== */
509
510static int __init
511mpc52xx_ata_init(void)
512{
513 printk(KERN_INFO "ata: MPC52xx IDE/ATA libata driver\n");
514 return of_register_platform_driver(&mpc52xx_ata_of_platform_driver);
515}
516
517static void __exit
518mpc52xx_ata_exit(void)
519{
520 of_unregister_platform_driver(&mpc52xx_ata_of_platform_driver);
521}
522
523module_init(mpc52xx_ata_init);
524module_exit(mpc52xx_ata_exit);
525
526MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
527MODULE_DESCRIPTION("Freescale MPC52xx IDE/ATA libata driver");
528MODULE_LICENSE("GPL");
529MODULE_DEVICE_TABLE(of, mpc52xx_ata_of_match);
530MODULE_VERSION(DRV_VERSION);
531