1
2
3
4
5
6
7
8
9
10
11#undef DEBUG
12#undef DEBUG_DMA
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/blkdev.h>
18#include <linux/ata.h>
19#include <linux/libata.h>
20#include <linux/adb.h>
21#include <linux/pmu.h>
22#include <linux/scatterlist.h>
23#include <linux/of.h>
24#include <linux/gfp.h>
25#include <linux/pci.h>
26
27#include <scsi/scsi.h>
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_device.h>
30
31#include <asm/macio.h>
32#include <asm/io.h>
33#include <asm/dbdma.h>
34#include <asm/machdep.h>
35#include <asm/pmac_feature.h>
36#include <asm/mediabay.h>
37
38#ifdef DEBUG_DMA
39#define dev_dbgdma(dev, format, arg...) \
40 dev_printk(KERN_DEBUG , dev , format , ## arg)
41#else
42#define dev_dbgdma(dev, format, arg...) \
43 ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
44#endif
45
46#define DRV_NAME "pata_macio"
47#define DRV_VERSION "0.9"
48
49
50enum {
51 controller_ohare,
52 controller_heathrow,
53 controller_kl_ata3,
54 controller_kl_ata4,
55 controller_un_ata6,
56 controller_k2_ata6,
57 controller_sh_ata6,
58};
59
60static const char* macio_ata_names[] = {
61 "OHare ATA",
62 "Heathrow ATA",
63 "KeyLargo ATA-3",
64 "KeyLargo ATA-4",
65 "UniNorth ATA-6",
66 "K2 ATA-6",
67 "Shasta ATA-6",
68};
69
70
71
72
73#define IDE_TIMING_CONFIG 0x200
74#define IDE_INTERRUPT 0x300
75
76
77#define IDE_KAUAI_PIO_CONFIG 0x200
78#define IDE_KAUAI_ULTRA_CONFIG 0x210
79#define IDE_KAUAI_POLL_CONFIG 0x220
80
81
82
83
84
85
86#define SYSCLK_TICKS(t) (((t) + IDE_SYSCLK_NS - 1) / IDE_SYSCLK_NS)
87#define SYSCLK_TICKS_66(t) (((t) + IDE_SYSCLK_66_NS - 1) / IDE_SYSCLK_66_NS)
88#define IDE_SYSCLK_NS 30
89#define IDE_SYSCLK_66_NS 15
90
91
92
93
94
95
96#define TR_133_PIOREG_PIO_MASK 0xff000fff
97#define TR_133_PIOREG_MDMA_MASK 0x00fff800
98#define TR_133_UDMAREG_UDMA_MASK 0x0003ffff
99#define TR_133_UDMAREG_UDMA_EN 0x00000001
100
101
102
103
104
105
106
107
108
109
110
111
112#define TR_100_PIO_ADDRSETUP_MASK 0xff000000
113#define TR_100_PIO_ADDRSETUP_SHIFT 24
114#define TR_100_MDMA_MASK 0x00fff000
115#define TR_100_MDMA_RECOVERY_MASK 0x00fc0000
116#define TR_100_MDMA_RECOVERY_SHIFT 18
117#define TR_100_MDMA_ACCESS_MASK 0x0003f000
118#define TR_100_MDMA_ACCESS_SHIFT 12
119#define TR_100_PIO_MASK 0xff000fff
120#define TR_100_PIO_RECOVERY_MASK 0x00000fc0
121#define TR_100_PIO_RECOVERY_SHIFT 6
122#define TR_100_PIO_ACCESS_MASK 0x0000003f
123#define TR_100_PIO_ACCESS_SHIFT 0
124
125#define TR_100_UDMAREG_UDMA_MASK 0x0000ffff
126#define TR_100_UDMAREG_UDMA_EN 0x00000001
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145#define TR_66_UDMA_MASK 0xfff00000
146#define TR_66_UDMA_EN 0x00100000
147#define TR_66_PIO_ADDRSETUP_MASK 0xe0000000
148#define TR_66_PIO_ADDRSETUP_SHIFT 29
149#define TR_66_UDMA_RDY2PAUS_MASK 0x1e000000
150#define TR_66_UDMA_RDY2PAUS_SHIFT 25
151#define TR_66_UDMA_WRDATASETUP_MASK 0x01e00000
152#define TR_66_UDMA_WRDATASETUP_SHIFT 21
153#define TR_66_MDMA_MASK 0x000ffc00
154#define TR_66_MDMA_RECOVERY_MASK 0x000f8000
155#define TR_66_MDMA_RECOVERY_SHIFT 15
156#define TR_66_MDMA_ACCESS_MASK 0x00007c00
157#define TR_66_MDMA_ACCESS_SHIFT 10
158#define TR_66_PIO_MASK 0xe00003ff
159#define TR_66_PIO_RECOVERY_MASK 0x000003e0
160#define TR_66_PIO_RECOVERY_SHIFT 5
161#define TR_66_PIO_ACCESS_MASK 0x0000001f
162#define TR_66_PIO_ACCESS_SHIFT 0
163
164
165
166
167
168
169
170
171
172
173
174
175#define TR_33_MDMA_MASK 0x003ff800
176#define TR_33_MDMA_RECOVERY_MASK 0x001f0000
177#define TR_33_MDMA_RECOVERY_SHIFT 16
178#define TR_33_MDMA_ACCESS_MASK 0x0000f800
179#define TR_33_MDMA_ACCESS_SHIFT 11
180#define TR_33_MDMA_HALFTICK 0x00200000
181#define TR_33_PIO_MASK 0x000007ff
182#define TR_33_PIO_E 0x00000400
183#define TR_33_PIO_RECOVERY_MASK 0x000003e0
184#define TR_33_PIO_RECOVERY_SHIFT 5
185#define TR_33_PIO_ACCESS_MASK 0x0000001f
186#define TR_33_PIO_ACCESS_SHIFT 0
187
188
189
190
191
192#define IDE_INTR_DMA 0x80000000
193#define IDE_INTR_DEVICE 0x40000000
194
195
196
197
198#define KAUAI_FCR_UATA_MAGIC 0x00000004
199#define KAUAI_FCR_UATA_RESET_N 0x00000002
200#define KAUAI_FCR_UATA_ENABLE 0x00000001
201
202
203
204#define MAX_DCMDS 256
205
206
207#define MAX_DBDMA_SEG 0xff00
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222#define IDE_WAKEUP_DELAY_MS 1000
223
224struct pata_macio_timing;
225
226struct pata_macio_priv {
227 int kind;
228 int aapl_bus_id;
229 int mediabay : 1;
230 struct device_node *node;
231 struct macio_dev *mdev;
232 struct pci_dev *pdev;
233 struct device *dev;
234 int irq;
235 u32 treg[2][2];
236 void __iomem *tfregs;
237 void __iomem *kauai_fcr;
238 struct dbdma_cmd * dma_table_cpu;
239 dma_addr_t dma_table_dma;
240 struct ata_host *host;
241 const struct pata_macio_timing *timings;
242};
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258struct pata_macio_timing {
259 int mode;
260 u32 reg1;
261 u32 reg2;
262};
263
264static const struct pata_macio_timing pata_macio_ohare_timings[] = {
265 { XFER_PIO_0, 0x00000526, 0, },
266 { XFER_PIO_1, 0x00000085, 0, },
267 { XFER_PIO_2, 0x00000025, 0, },
268 { XFER_PIO_3, 0x00000025, 0, },
269 { XFER_PIO_4, 0x00000025, 0, },
270 { XFER_MW_DMA_0, 0x00074000, 0, },
271 { XFER_MW_DMA_1, 0x00221000, 0, },
272 { XFER_MW_DMA_2, 0x00211000, 0, },
273 { -1, 0, 0 }
274};
275
276static const struct pata_macio_timing pata_macio_heathrow_timings[] = {
277 { XFER_PIO_0, 0x00000526, 0, },
278 { XFER_PIO_1, 0x00000085, 0, },
279 { XFER_PIO_2, 0x00000025, 0, },
280 { XFER_PIO_3, 0x00000025, 0, },
281 { XFER_PIO_4, 0x00000025, 0, },
282 { XFER_MW_DMA_0, 0x00074000, 0, },
283 { XFER_MW_DMA_1, 0x00221000, 0, },
284 { XFER_MW_DMA_2, 0x00211000, 0, },
285 { -1, 0, 0 }
286};
287
288static const struct pata_macio_timing pata_macio_kl33_timings[] = {
289 { XFER_PIO_0, 0x00000526, 0, },
290 { XFER_PIO_1, 0x00000085, 0, },
291 { XFER_PIO_2, 0x00000025, 0, },
292 { XFER_PIO_3, 0x00000025, 0, },
293 { XFER_PIO_4, 0x00000025, 0, },
294 { XFER_MW_DMA_0, 0x00084000, 0, },
295 { XFER_MW_DMA_1, 0x00021800, 0, },
296 { XFER_MW_DMA_2, 0x00011800, 0, },
297 { -1, 0, 0 }
298};
299
300static const struct pata_macio_timing pata_macio_kl66_timings[] = {
301 { XFER_PIO_0, 0x0000038c, 0, },
302 { XFER_PIO_1, 0x0000020a, 0, },
303 { XFER_PIO_2, 0x00000127, 0, },
304 { XFER_PIO_3, 0x000000c6, 0, },
305 { XFER_PIO_4, 0x00000065, 0, },
306 { XFER_MW_DMA_0, 0x00084000, 0, },
307 { XFER_MW_DMA_1, 0x00029800, 0, },
308 { XFER_MW_DMA_2, 0x00019400, 0, },
309 { XFER_UDMA_0, 0x19100000, 0, },
310 { XFER_UDMA_1, 0x14d00000, 0, },
311 { XFER_UDMA_2, 0x10900000, 0, },
312 { XFER_UDMA_3, 0x0c700000, 0, },
313 { XFER_UDMA_4, 0x0c500000, 0, },
314 { -1, 0, 0 }
315};
316
317static const struct pata_macio_timing pata_macio_kauai_timings[] = {
318 { XFER_PIO_0, 0x08000a92, 0, },
319 { XFER_PIO_1, 0x0800060f, 0, },
320 { XFER_PIO_2, 0x0800038b, 0, },
321 { XFER_PIO_3, 0x05000249, 0, },
322 { XFER_PIO_4, 0x04000148, 0, },
323 { XFER_MW_DMA_0, 0x00618000, 0, },
324 { XFER_MW_DMA_1, 0x00209000, 0, },
325 { XFER_MW_DMA_2, 0x00148000, 0, },
326 { XFER_UDMA_0, 0, 0x000070c1, },
327 { XFER_UDMA_1, 0, 0x00005d81, },
328 { XFER_UDMA_2, 0, 0x00004a61, },
329 { XFER_UDMA_3, 0, 0x00003a51, },
330 { XFER_UDMA_4, 0, 0x00002a31, },
331 { XFER_UDMA_5, 0, 0x00002921, },
332 { -1, 0, 0 }
333};
334
335static const struct pata_macio_timing pata_macio_shasta_timings[] = {
336 { XFER_PIO_0, 0x0a000c97, 0, },
337 { XFER_PIO_1, 0x07000712, 0, },
338 { XFER_PIO_2, 0x040003cd, 0, },
339 { XFER_PIO_3, 0x0500028b, 0, },
340 { XFER_PIO_4, 0x0400010a, 0, },
341 { XFER_MW_DMA_0, 0x00820800, 0, },
342 { XFER_MW_DMA_1, 0x0028b000, 0, },
343 { XFER_MW_DMA_2, 0x001ca000, 0, },
344 { XFER_UDMA_0, 0, 0x00035901, },
345 { XFER_UDMA_1, 0, 0x000348b1, },
346 { XFER_UDMA_2, 0, 0x00033881, },
347 { XFER_UDMA_3, 0, 0x00033861, },
348 { XFER_UDMA_4, 0, 0x00033841, },
349 { XFER_UDMA_5, 0, 0x00033031, },
350 { XFER_UDMA_6, 0, 0x00033021, },
351 { -1, 0, 0 }
352};
353
354static const struct pata_macio_timing *pata_macio_find_timing(
355 struct pata_macio_priv *priv,
356 int mode)
357{
358 int i;
359
360 for (i = 0; priv->timings[i].mode > 0; i++) {
361 if (priv->timings[i].mode == mode)
362 return &priv->timings[i];
363 }
364 return NULL;
365}
366
367
368static void pata_macio_apply_timings(struct ata_port *ap, unsigned int device)
369{
370 struct pata_macio_priv *priv = ap->private_data;
371 void __iomem *rbase = ap->ioaddr.cmd_addr;
372
373 if (priv->kind == controller_sh_ata6 ||
374 priv->kind == controller_un_ata6 ||
375 priv->kind == controller_k2_ata6) {
376 writel(priv->treg[device][0], rbase + IDE_KAUAI_PIO_CONFIG);
377 writel(priv->treg[device][1], rbase + IDE_KAUAI_ULTRA_CONFIG);
378 } else
379 writel(priv->treg[device][0], rbase + IDE_TIMING_CONFIG);
380}
381
382static void pata_macio_dev_select(struct ata_port *ap, unsigned int device)
383{
384 ata_sff_dev_select(ap, device);
385
386
387 pata_macio_apply_timings(ap, device);
388}
389
390static void pata_macio_set_timings(struct ata_port *ap,
391 struct ata_device *adev)
392{
393 struct pata_macio_priv *priv = ap->private_data;
394 const struct pata_macio_timing *t;
395
396 dev_dbg(priv->dev, "Set timings: DEV=%d,PIO=0x%x (%s),DMA=0x%x (%s)\n",
397 adev->devno,
398 adev->pio_mode,
399 ata_mode_string(ata_xfer_mode2mask(adev->pio_mode)),
400 adev->dma_mode,
401 ata_mode_string(ata_xfer_mode2mask(adev->dma_mode)));
402
403
404 priv->treg[adev->devno][0] = priv->treg[adev->devno][1] = 0;
405
406
407 t = pata_macio_find_timing(priv, adev->pio_mode);
408 if (t == NULL) {
409 dev_warn(priv->dev, "Invalid PIO timing requested: 0x%x\n",
410 adev->pio_mode);
411 t = pata_macio_find_timing(priv, XFER_PIO_0);
412 }
413 BUG_ON(t == NULL);
414
415
416 priv->treg[adev->devno][0] |= t->reg1;
417
418
419 t = pata_macio_find_timing(priv, adev->dma_mode);
420 if (t == NULL || (t->reg1 == 0 && t->reg2 == 0)) {
421 dev_dbg(priv->dev, "DMA timing not set yet, using MW_DMA_0\n");
422 t = pata_macio_find_timing(priv, XFER_MW_DMA_0);
423 }
424 BUG_ON(t == NULL);
425
426
427 priv->treg[adev->devno][0] |= t->reg1;
428 priv->treg[adev->devno][1] |= t->reg2;
429
430 dev_dbg(priv->dev, " -> %08x %08x\n",
431 priv->treg[adev->devno][0],
432 priv->treg[adev->devno][1]);
433
434
435 pata_macio_apply_timings(ap, adev->devno);
436}
437
438
439
440
441
442static void pata_macio_default_timings(struct pata_macio_priv *priv)
443{
444 unsigned int value, value2 = 0;
445
446 switch(priv->kind) {
447 case controller_sh_ata6:
448 value = 0x0a820c97;
449 value2 = 0x00033031;
450 break;
451 case controller_un_ata6:
452 case controller_k2_ata6:
453 value = 0x08618a92;
454 value2 = 0x00002921;
455 break;
456 case controller_kl_ata4:
457 value = 0x0008438c;
458 break;
459 case controller_kl_ata3:
460 value = 0x00084526;
461 break;
462 case controller_heathrow:
463 case controller_ohare:
464 default:
465 value = 0x00074526;
466 break;
467 }
468 priv->treg[0][0] = priv->treg[1][0] = value;
469 priv->treg[0][1] = priv->treg[1][1] = value2;
470}
471
472static int pata_macio_cable_detect(struct ata_port *ap)
473{
474 struct pata_macio_priv *priv = ap->private_data;
475
476
477 if (priv->kind == controller_kl_ata4 ||
478 priv->kind == controller_un_ata6 ||
479 priv->kind == controller_k2_ata6 ||
480 priv->kind == controller_sh_ata6) {
481 const char* cable = of_get_property(priv->node, "cable-type",
482 NULL);
483 struct device_node *root = of_find_node_by_path("/");
484 const char *model = of_get_property(root, "model", NULL);
485
486 if (cable && !strncmp(cable, "80-", 3)) {
487
488
489
490
491 if (!strncmp(model, "PowerBook", 9))
492 return ATA_CBL_PATA40_SHORT;
493 else
494 return ATA_CBL_PATA80;
495 }
496 }
497
498
499
500
501
502 if (of_device_is_compatible(priv->node, "K2-UATA") ||
503 of_device_is_compatible(priv->node, "shasta-ata"))
504 return ATA_CBL_PATA80;
505
506
507 return ATA_CBL_PATA40;
508}
509
510static void pata_macio_qc_prep(struct ata_queued_cmd *qc)
511{
512 unsigned int write = (qc->tf.flags & ATA_TFLAG_WRITE);
513 struct ata_port *ap = qc->ap;
514 struct pata_macio_priv *priv = ap->private_data;
515 struct scatterlist *sg;
516 struct dbdma_cmd *table;
517 unsigned int si, pi;
518
519 dev_dbgdma(priv->dev, "%s: qc %p flags %lx, write %d dev %d\n",
520 __func__, qc, qc->flags, write, qc->dev->devno);
521
522 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
523 return;
524
525 table = (struct dbdma_cmd *) priv->dma_table_cpu;
526
527 pi = 0;
528 for_each_sg(qc->sg, sg, qc->n_elem, si) {
529 u32 addr, sg_len, len;
530
531
532
533
534
535 addr = (u32) sg_dma_address(sg);
536 sg_len = sg_dma_len(sg);
537
538 while (sg_len) {
539
540 BUG_ON (pi++ >= MAX_DCMDS);
541
542 len = (sg_len < MAX_DBDMA_SEG) ? sg_len : MAX_DBDMA_SEG;
543 table->command = cpu_to_le16(write ? OUTPUT_MORE: INPUT_MORE);
544 table->req_count = cpu_to_le16(len);
545 table->phy_addr = cpu_to_le32(addr);
546 table->cmd_dep = 0;
547 table->xfer_status = 0;
548 table->res_count = 0;
549 addr += len;
550 sg_len -= len;
551 ++table;
552 }
553 }
554
555
556 BUG_ON(!pi);
557
558
559 table--;
560 table->command = cpu_to_le16(write ? OUTPUT_LAST: INPUT_LAST);
561 table++;
562
563
564 memset(table, 0, sizeof(struct dbdma_cmd));
565 table->command = cpu_to_le16(DBDMA_STOP);
566
567 dev_dbgdma(priv->dev, "%s: %d DMA list entries\n", __func__, pi);
568}
569
570
571static void pata_macio_freeze(struct ata_port *ap)
572{
573 struct dbdma_regs __iomem *dma_regs = ap->ioaddr.bmdma_addr;
574
575 if (dma_regs) {
576 unsigned int timeout = 1000000;
577
578
579 writel((RUN|PAUSE|FLUSH|WAKE|DEAD) << 16, &dma_regs->control);
580 while (--timeout && (readl(&dma_regs->status) & RUN))
581 udelay(1);
582 }
583
584 ata_sff_freeze(ap);
585}
586
587
588static void pata_macio_bmdma_setup(struct ata_queued_cmd *qc)
589{
590 struct ata_port *ap = qc->ap;
591 struct pata_macio_priv *priv = ap->private_data;
592 struct dbdma_regs __iomem *dma_regs = ap->ioaddr.bmdma_addr;
593 int dev = qc->dev->devno;
594
595 dev_dbgdma(priv->dev, "%s: qc %p\n", __func__, qc);
596
597
598 writel(priv->dma_table_dma, &dma_regs->cmdptr);
599
600
601
602
603 if (priv->kind == controller_kl_ata4 &&
604 (priv->treg[dev][0] & TR_66_UDMA_EN)) {
605 void __iomem *rbase = ap->ioaddr.cmd_addr;
606 u32 reg = priv->treg[dev][0];
607
608 if (!(qc->tf.flags & ATA_TFLAG_WRITE))
609 reg += 0x00800000;
610 writel(reg, rbase + IDE_TIMING_CONFIG);
611 }
612
613
614 ap->ops->sff_exec_command(ap, &qc->tf);
615}
616
617static void pata_macio_bmdma_start(struct ata_queued_cmd *qc)
618{
619 struct ata_port *ap = qc->ap;
620 struct pata_macio_priv *priv = ap->private_data;
621 struct dbdma_regs __iomem *dma_regs = ap->ioaddr.bmdma_addr;
622
623 dev_dbgdma(priv->dev, "%s: qc %p\n", __func__, qc);
624
625 writel((RUN << 16) | RUN, &dma_regs->control);
626
627 (void)readl(&dma_regs->control);
628}
629
630static void pata_macio_bmdma_stop(struct ata_queued_cmd *qc)
631{
632 struct ata_port *ap = qc->ap;
633 struct pata_macio_priv *priv = ap->private_data;
634 struct dbdma_regs __iomem *dma_regs = ap->ioaddr.bmdma_addr;
635 unsigned int timeout = 1000000;
636
637 dev_dbgdma(priv->dev, "%s: qc %p\n", __func__, qc);
638
639
640 writel (((RUN|WAKE|DEAD) << 16), &dma_regs->control);
641 while (--timeout && (readl(&dma_regs->status) & RUN))
642 udelay(1);
643}
644
645static u8 pata_macio_bmdma_status(struct ata_port *ap)
646{
647 struct pata_macio_priv *priv = ap->private_data;
648 struct dbdma_regs __iomem *dma_regs = ap->ioaddr.bmdma_addr;
649 u32 dstat, rstat = ATA_DMA_INTR;
650 unsigned long timeout = 0;
651
652 dstat = readl(&dma_regs->status);
653
654 dev_dbgdma(priv->dev, "%s: dstat=%x\n", __func__, dstat);
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669 if ((dstat & (RUN|DEAD)) != RUN)
670 rstat |= ATA_DMA_ERR;
671
672
673
674
675
676 if ((dstat & ACTIVE) == 0)
677 return rstat;
678
679 dev_dbgdma(priv->dev, "%s: DMA still active, flushing...\n", __func__);
680
681
682
683
684
685
686
687 udelay(1);
688 writel((FLUSH << 16) | FLUSH, &dma_regs->control);
689 for (;;) {
690 udelay(1);
691 dstat = readl(&dma_regs->status);
692 if ((dstat & FLUSH) == 0)
693 break;
694 if (++timeout > 1000) {
695 dev_warn(priv->dev, "timeout flushing DMA\n");
696 rstat |= ATA_DMA_ERR;
697 break;
698 }
699 }
700 return rstat;
701}
702
703
704static int pata_macio_port_start(struct ata_port *ap)
705{
706 struct pata_macio_priv *priv = ap->private_data;
707
708 if (ap->ioaddr.bmdma_addr == NULL)
709 return 0;
710
711
712
713
714
715
716 priv->dma_table_cpu =
717 dmam_alloc_coherent(priv->dev,
718 (MAX_DCMDS + 2) * sizeof(struct dbdma_cmd),
719 &priv->dma_table_dma, GFP_KERNEL);
720 if (priv->dma_table_cpu == NULL) {
721 dev_err(priv->dev, "Unable to allocate DMA command list\n");
722 ap->ioaddr.bmdma_addr = NULL;
723 ap->mwdma_mask = 0;
724 ap->udma_mask = 0;
725 }
726 return 0;
727}
728
729static void pata_macio_irq_clear(struct ata_port *ap)
730{
731 struct pata_macio_priv *priv = ap->private_data;
732
733
734
735 dev_dbgdma(priv->dev, "%s\n", __func__);
736}
737
738static void pata_macio_reset_hw(struct pata_macio_priv *priv, int resume)
739{
740 dev_dbg(priv->dev, "Enabling & resetting... \n");
741
742 if (priv->mediabay)
743 return;
744
745 if (priv->kind == controller_ohare && !resume) {
746
747
748
749
750 ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, priv->node, 0, 1);
751 } else {
752 int rc;
753
754
755 rc = ppc_md.feature_call(PMAC_FTR_IDE_RESET,
756 priv->node, priv->aapl_bus_id, 1);
757 ppc_md.feature_call(PMAC_FTR_IDE_ENABLE,
758 priv->node, priv->aapl_bus_id, 1);
759 msleep(10);
760
761 if (rc == 0) {
762 ppc_md.feature_call(PMAC_FTR_IDE_RESET,
763 priv->node, priv->aapl_bus_id, 0);
764 msleep(IDE_WAKEUP_DELAY_MS);
765 }
766 }
767
768
769 if (priv->pdev && resume) {
770 int rc;
771
772 pci_restore_state(priv->pdev);
773 rc = pcim_enable_device(priv->pdev);
774 if (rc)
775 dev_err(&priv->pdev->dev,
776 "Failed to enable device after resume (%d)\n",
777 rc);
778 else
779 pci_set_master(priv->pdev);
780 }
781
782
783
784
785 if (priv->kauai_fcr)
786 writel(KAUAI_FCR_UATA_MAGIC |
787 KAUAI_FCR_UATA_RESET_N |
788 KAUAI_FCR_UATA_ENABLE, priv->kauai_fcr);
789}
790
791
792
793
794static int pata_macio_slave_config(struct scsi_device *sdev)
795{
796 struct ata_port *ap = ata_shost_to_port(sdev->host);
797 struct pata_macio_priv *priv = ap->private_data;
798 struct ata_device *dev;
799 u16 cmd;
800 int rc;
801
802
803 rc = ata_scsi_slave_config(sdev);
804 if (rc)
805 return rc;
806
807
808 dev = &ap->link.device[sdev->id];
809
810
811 if (priv->kind == controller_ohare) {
812 blk_queue_update_dma_alignment(sdev->request_queue, 31);
813 blk_queue_update_dma_pad(sdev->request_queue, 31);
814
815
816 ata_dev_info(dev, "OHare alignment limits applied\n");
817 return 0;
818 }
819
820
821 if (dev->class != ATA_DEV_ATAPI)
822 return 0;
823
824
825 if (priv->kind == controller_sh_ata6 || priv->kind == controller_k2_ata6) {
826
827 blk_queue_update_dma_alignment(sdev->request_queue, 15);
828 blk_queue_update_dma_pad(sdev->request_queue, 15);
829
830
831
832
833
834
835 BUG_ON(!priv->pdev);
836 pci_write_config_byte(priv->pdev, PCI_CACHE_LINE_SIZE, 0x08);
837 pci_read_config_word(priv->pdev, PCI_COMMAND, &cmd);
838 pci_write_config_word(priv->pdev, PCI_COMMAND,
839 cmd | PCI_COMMAND_INVALIDATE);
840
841
842 ata_dev_info(dev, "K2/Shasta alignment limits applied\n");
843 }
844
845 return 0;
846}
847
848#ifdef CONFIG_PM_SLEEP
849static int pata_macio_do_suspend(struct pata_macio_priv *priv, pm_message_t mesg)
850{
851 int rc;
852
853
854 rc = ata_host_suspend(priv->host, mesg);
855 if (rc)
856 return rc;
857
858
859 pata_macio_default_timings(priv);
860
861
862
863 disable_irq(priv->irq);
864
865
866 if (priv->mediabay)
867 return 0;
868
869
870 if (priv->kauai_fcr) {
871 u32 fcr = readl(priv->kauai_fcr);
872 fcr &= ~(KAUAI_FCR_UATA_RESET_N | KAUAI_FCR_UATA_ENABLE);
873 writel(fcr, priv->kauai_fcr);
874 }
875
876
877
878
879
880
881 if (priv->pdev) {
882 pci_save_state(priv->pdev);
883 pci_disable_device(priv->pdev);
884 }
885
886
887 ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, priv->node,
888 priv->aapl_bus_id, 0);
889
890 return 0;
891}
892
893static int pata_macio_do_resume(struct pata_macio_priv *priv)
894{
895
896 pata_macio_reset_hw(priv, 1);
897
898
899 pata_macio_apply_timings(priv->host->ports[0], 0);
900
901
902 enable_irq(priv->irq);
903
904
905 ata_host_resume(priv->host);
906
907 return 0;
908}
909#endif
910
911static struct scsi_host_template pata_macio_sht = {
912 ATA_BASE_SHT(DRV_NAME),
913 .sg_tablesize = MAX_DCMDS,
914
915 .dma_boundary = ATA_DMA_BOUNDARY,
916 .slave_configure = pata_macio_slave_config,
917};
918
919static struct ata_port_operations pata_macio_ops = {
920 .inherits = &ata_bmdma_port_ops,
921
922 .freeze = pata_macio_freeze,
923 .set_piomode = pata_macio_set_timings,
924 .set_dmamode = pata_macio_set_timings,
925 .cable_detect = pata_macio_cable_detect,
926 .sff_dev_select = pata_macio_dev_select,
927 .qc_prep = pata_macio_qc_prep,
928 .bmdma_setup = pata_macio_bmdma_setup,
929 .bmdma_start = pata_macio_bmdma_start,
930 .bmdma_stop = pata_macio_bmdma_stop,
931 .bmdma_status = pata_macio_bmdma_status,
932 .port_start = pata_macio_port_start,
933 .sff_irq_clear = pata_macio_irq_clear,
934};
935
936static void pata_macio_invariants(struct pata_macio_priv *priv)
937{
938 const int *bidp;
939
940
941 if (of_device_is_compatible(priv->node, "shasta-ata")) {
942 priv->kind = controller_sh_ata6;
943 priv->timings = pata_macio_shasta_timings;
944 } else if (of_device_is_compatible(priv->node, "kauai-ata")) {
945 priv->kind = controller_un_ata6;
946 priv->timings = pata_macio_kauai_timings;
947 } else if (of_device_is_compatible(priv->node, "K2-UATA")) {
948 priv->kind = controller_k2_ata6;
949 priv->timings = pata_macio_kauai_timings;
950 } else if (of_device_is_compatible(priv->node, "keylargo-ata")) {
951 if (strcmp(priv->node->name, "ata-4") == 0) {
952 priv->kind = controller_kl_ata4;
953 priv->timings = pata_macio_kl66_timings;
954 } else {
955 priv->kind = controller_kl_ata3;
956 priv->timings = pata_macio_kl33_timings;
957 }
958 } else if (of_device_is_compatible(priv->node, "heathrow-ata")) {
959 priv->kind = controller_heathrow;
960 priv->timings = pata_macio_heathrow_timings;
961 } else {
962 priv->kind = controller_ohare;
963 priv->timings = pata_macio_ohare_timings;
964 }
965
966
967
968
969 bidp = of_get_property(priv->node, "AAPL,bus-id", NULL);
970 priv->aapl_bus_id = bidp ? *bidp : 0;
971
972
973 if (priv->mediabay && bidp == 0)
974 priv->aapl_bus_id = 1;
975}
976
977static void pata_macio_setup_ios(struct ata_ioports *ioaddr,
978 void __iomem * base, void __iomem * dma)
979{
980
981 ioaddr->cmd_addr = base;
982
983
984 ioaddr->data_addr = base + (ATA_REG_DATA << 4);
985 ioaddr->error_addr = base + (ATA_REG_ERR << 4);
986 ioaddr->feature_addr = base + (ATA_REG_FEATURE << 4);
987 ioaddr->nsect_addr = base + (ATA_REG_NSECT << 4);
988 ioaddr->lbal_addr = base + (ATA_REG_LBAL << 4);
989 ioaddr->lbam_addr = base + (ATA_REG_LBAM << 4);
990 ioaddr->lbah_addr = base + (ATA_REG_LBAH << 4);
991 ioaddr->device_addr = base + (ATA_REG_DEVICE << 4);
992 ioaddr->status_addr = base + (ATA_REG_STATUS << 4);
993 ioaddr->command_addr = base + (ATA_REG_CMD << 4);
994 ioaddr->altstatus_addr = base + 0x160;
995 ioaddr->ctl_addr = base + 0x160;
996 ioaddr->bmdma_addr = dma;
997}
998
999static void pmac_macio_calc_timing_masks(struct pata_macio_priv *priv,
1000 struct ata_port_info *pinfo)
1001{
1002 int i = 0;
1003
1004 pinfo->pio_mask = 0;
1005 pinfo->mwdma_mask = 0;
1006 pinfo->udma_mask = 0;
1007
1008 while (priv->timings[i].mode > 0) {
1009 unsigned int mask = 1U << (priv->timings[i].mode & 0x0f);
1010 switch(priv->timings[i].mode & 0xf0) {
1011 case 0x00:
1012 pinfo->pio_mask |= (mask >> 8);
1013 break;
1014 case 0x20:
1015 pinfo->mwdma_mask |= mask;
1016 break;
1017 case 0x40:
1018 pinfo->udma_mask |= mask;
1019 break;
1020 }
1021 i++;
1022 }
1023 dev_dbg(priv->dev, "Supported masks: PIO=%lx, MWDMA=%lx, UDMA=%lx\n",
1024 pinfo->pio_mask, pinfo->mwdma_mask, pinfo->udma_mask);
1025}
1026
1027static int pata_macio_common_init(struct pata_macio_priv *priv,
1028 resource_size_t tfregs,
1029 resource_size_t dmaregs,
1030 resource_size_t fcregs,
1031 unsigned long irq)
1032{
1033 struct ata_port_info pinfo;
1034 const struct ata_port_info *ppi[] = { &pinfo, NULL };
1035 void __iomem *dma_regs = NULL;
1036
1037
1038
1039
1040 pata_macio_invariants(priv);
1041
1042
1043 pata_macio_default_timings(priv);
1044
1045
1046
1047
1048 dma_set_max_seg_size(priv->dev, MAX_DBDMA_SEG);
1049
1050
1051 memset(&pinfo, 0, sizeof(struct ata_port_info));
1052 pmac_macio_calc_timing_masks(priv, &pinfo);
1053 pinfo.flags = ATA_FLAG_SLAVE_POSS;
1054 pinfo.port_ops = &pata_macio_ops;
1055 pinfo.private_data = priv;
1056
1057 priv->host = ata_host_alloc_pinfo(priv->dev, ppi, 1);
1058 if (priv->host == NULL) {
1059 dev_err(priv->dev, "Failed to allocate ATA port structure\n");
1060 return -ENOMEM;
1061 }
1062
1063
1064 priv->host->private_data = priv;
1065
1066
1067 priv->tfregs = devm_ioremap(priv->dev, tfregs, 0x100);
1068 if (priv->tfregs == NULL) {
1069 dev_err(priv->dev, "Failed to map ATA ports\n");
1070 return -ENOMEM;
1071 }
1072 priv->host->iomap = &priv->tfregs;
1073
1074
1075 if (dmaregs != 0) {
1076 dma_regs = devm_ioremap(priv->dev, dmaregs,
1077 sizeof(struct dbdma_regs));
1078 if (dma_regs == NULL)
1079 dev_warn(priv->dev, "Failed to map ATA DMA registers\n");
1080 }
1081
1082
1083 if (fcregs != 0) {
1084 priv->kauai_fcr = devm_ioremap(priv->dev, fcregs, 4);
1085 if (priv->kauai_fcr == NULL) {
1086 dev_err(priv->dev, "Failed to map ATA FCR register\n");
1087 return -ENOMEM;
1088 }
1089 }
1090
1091
1092 pata_macio_setup_ios(&priv->host->ports[0]->ioaddr,
1093 priv->tfregs, dma_regs);
1094 priv->host->ports[0]->private_data = priv;
1095
1096
1097 pata_macio_reset_hw(priv, 0);
1098 pata_macio_apply_timings(priv->host->ports[0], 0);
1099
1100
1101 if (priv->pdev && dma_regs)
1102 pci_set_master(priv->pdev);
1103
1104 dev_info(priv->dev, "Activating pata-macio chipset %s, Apple bus ID %d\n",
1105 macio_ata_names[priv->kind], priv->aapl_bus_id);
1106
1107
1108 priv->irq = irq;
1109 return ata_host_activate(priv->host, irq, ata_bmdma_interrupt, 0,
1110 &pata_macio_sht);
1111}
1112
1113static int pata_macio_attach(struct macio_dev *mdev,
1114 const struct of_device_id *match)
1115{
1116 struct pata_macio_priv *priv;
1117 resource_size_t tfregs, dmaregs = 0;
1118 unsigned long irq;
1119 int rc;
1120
1121
1122 if (macio_resource_count(mdev) == 0) {
1123 dev_err(&mdev->ofdev.dev,
1124 "No addresses for controller\n");
1125 return -ENXIO;
1126 }
1127
1128
1129 macio_enable_devres(mdev);
1130
1131
1132 priv = devm_kzalloc(&mdev->ofdev.dev,
1133 sizeof(struct pata_macio_priv), GFP_KERNEL);
1134 if (!priv)
1135 return -ENOMEM;
1136
1137 priv->node = of_node_get(mdev->ofdev.dev.of_node);
1138 priv->mdev = mdev;
1139 priv->dev = &mdev->ofdev.dev;
1140
1141
1142 if (macio_request_resource(mdev, 0, "pata-macio")) {
1143 dev_err(&mdev->ofdev.dev,
1144 "Cannot obtain taskfile resource\n");
1145 return -EBUSY;
1146 }
1147 tfregs = macio_resource_start(mdev, 0);
1148
1149
1150 if (macio_resource_count(mdev) >= 2) {
1151 if (macio_request_resource(mdev, 1, "pata-macio-dma"))
1152 dev_err(&mdev->ofdev.dev,
1153 "Cannot obtain DMA resource\n");
1154 else
1155 dmaregs = macio_resource_start(mdev, 1);
1156 }
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167 if (macio_irq_count(mdev) == 0) {
1168 dev_warn(&mdev->ofdev.dev,
1169 "No interrupts for controller, using 13\n");
1170 irq = irq_create_mapping(NULL, 13);
1171 } else
1172 irq = macio_irq(mdev, 0);
1173
1174
1175 lock_media_bay(priv->mdev->media_bay);
1176
1177
1178 rc = pata_macio_common_init(priv,
1179 tfregs,
1180 dmaregs,
1181 0,
1182 irq);
1183 unlock_media_bay(priv->mdev->media_bay);
1184
1185 return rc;
1186}
1187
1188static int pata_macio_detach(struct macio_dev *mdev)
1189{
1190 struct ata_host *host = macio_get_drvdata(mdev);
1191 struct pata_macio_priv *priv = host->private_data;
1192
1193 lock_media_bay(priv->mdev->media_bay);
1194
1195
1196
1197
1198 priv->host->private_data = NULL;
1199
1200 ata_host_detach(host);
1201
1202 unlock_media_bay(priv->mdev->media_bay);
1203
1204 return 0;
1205}
1206
1207#ifdef CONFIG_PM_SLEEP
1208static int pata_macio_suspend(struct macio_dev *mdev, pm_message_t mesg)
1209{
1210 struct ata_host *host = macio_get_drvdata(mdev);
1211
1212 return pata_macio_do_suspend(host->private_data, mesg);
1213}
1214
1215static int pata_macio_resume(struct macio_dev *mdev)
1216{
1217 struct ata_host *host = macio_get_drvdata(mdev);
1218
1219 return pata_macio_do_resume(host->private_data);
1220}
1221#endif
1222
1223#ifdef CONFIG_PMAC_MEDIABAY
1224static void pata_macio_mb_event(struct macio_dev* mdev, int mb_state)
1225{
1226 struct ata_host *host = macio_get_drvdata(mdev);
1227 struct ata_port *ap;
1228 struct ata_eh_info *ehi;
1229 struct ata_device *dev;
1230 unsigned long flags;
1231
1232 if (!host || !host->private_data)
1233 return;
1234 ap = host->ports[0];
1235 spin_lock_irqsave(ap->lock, flags);
1236 ehi = &ap->link.eh_info;
1237 if (mb_state == MB_CD) {
1238 ata_ehi_push_desc(ehi, "mediabay plug");
1239 ata_ehi_hotplugged(ehi);
1240 ata_port_freeze(ap);
1241 } else {
1242 ata_ehi_push_desc(ehi, "mediabay unplug");
1243 ata_for_each_dev(dev, &ap->link, ALL)
1244 dev->flags |= ATA_DFLAG_DETACH;
1245 ata_port_abort(ap);
1246 }
1247 spin_unlock_irqrestore(ap->lock, flags);
1248
1249}
1250#endif
1251
1252
1253static int pata_macio_pci_attach(struct pci_dev *pdev,
1254 const struct pci_device_id *id)
1255{
1256 struct pata_macio_priv *priv;
1257 struct device_node *np;
1258 resource_size_t rbase;
1259
1260
1261 np = pci_device_to_OF_node(pdev);
1262 if (np == NULL) {
1263 dev_err(&pdev->dev,
1264 "Cannot find OF device node for controller\n");
1265 return -ENODEV;
1266 }
1267
1268
1269 if (pcim_enable_device(pdev)) {
1270 dev_err(&pdev->dev,
1271 "Cannot enable controller PCI device\n");
1272 return -ENXIO;
1273 }
1274
1275
1276 priv = devm_kzalloc(&pdev->dev,
1277 sizeof(struct pata_macio_priv), GFP_KERNEL);
1278 if (!priv)
1279 return -ENOMEM;
1280
1281 priv->node = of_node_get(np);
1282 priv->pdev = pdev;
1283 priv->dev = &pdev->dev;
1284
1285
1286 if (pci_request_regions(pdev, "pata-macio")) {
1287 dev_err(&pdev->dev,
1288 "Cannot obtain PCI resources\n");
1289 return -EBUSY;
1290 }
1291
1292
1293 rbase = pci_resource_start(pdev, 0);
1294 if (pata_macio_common_init(priv,
1295 rbase + 0x2000,
1296 rbase + 0x1000,
1297 rbase,
1298 pdev->irq))
1299 return -ENXIO;
1300
1301 return 0;
1302}
1303
1304static void pata_macio_pci_detach(struct pci_dev *pdev)
1305{
1306 struct ata_host *host = pci_get_drvdata(pdev);
1307
1308 ata_host_detach(host);
1309}
1310
1311#ifdef CONFIG_PM_SLEEP
1312static int pata_macio_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
1313{
1314 struct ata_host *host = pci_get_drvdata(pdev);
1315
1316 return pata_macio_do_suspend(host->private_data, mesg);
1317}
1318
1319static int pata_macio_pci_resume(struct pci_dev *pdev)
1320{
1321 struct ata_host *host = pci_get_drvdata(pdev);
1322
1323 return pata_macio_do_resume(host->private_data);
1324}
1325#endif
1326
1327static const struct of_device_id pata_macio_match[] =
1328{
1329 {
1330 .name = "IDE",
1331 },
1332 {
1333 .name = "ATA",
1334 },
1335 {
1336 .type = "ide",
1337 },
1338 {
1339 .type = "ata",
1340 },
1341 {},
1342};
1343MODULE_DEVICE_TABLE(of, pata_macio_match);
1344
1345static struct macio_driver pata_macio_driver =
1346{
1347 .driver = {
1348 .name = "pata-macio",
1349 .owner = THIS_MODULE,
1350 .of_match_table = pata_macio_match,
1351 },
1352 .probe = pata_macio_attach,
1353 .remove = pata_macio_detach,
1354#ifdef CONFIG_PM_SLEEP
1355 .suspend = pata_macio_suspend,
1356 .resume = pata_macio_resume,
1357#endif
1358#ifdef CONFIG_PMAC_MEDIABAY
1359 .mediabay_event = pata_macio_mb_event,
1360#endif
1361};
1362
1363static const struct pci_device_id pata_macio_pci_match[] = {
1364 { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_UNI_N_ATA), 0 },
1365 { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_IPID_ATA100), 0 },
1366 { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_K2_ATA100), 0 },
1367 { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_SH_ATA), 0 },
1368 { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_IPID2_ATA), 0 },
1369 {},
1370};
1371
1372static struct pci_driver pata_macio_pci_driver = {
1373 .name = "pata-pci-macio",
1374 .id_table = pata_macio_pci_match,
1375 .probe = pata_macio_pci_attach,
1376 .remove = pata_macio_pci_detach,
1377#ifdef CONFIG_PM_SLEEP
1378 .suspend = pata_macio_pci_suspend,
1379 .resume = pata_macio_pci_resume,
1380#endif
1381 .driver = {
1382 .owner = THIS_MODULE,
1383 },
1384};
1385MODULE_DEVICE_TABLE(pci, pata_macio_pci_match);
1386
1387
1388static int __init pata_macio_init(void)
1389{
1390 int rc;
1391
1392 if (!machine_is(powermac))
1393 return -ENODEV;
1394
1395 rc = pci_register_driver(&pata_macio_pci_driver);
1396 if (rc)
1397 return rc;
1398 rc = macio_register_driver(&pata_macio_driver);
1399 if (rc) {
1400 pci_unregister_driver(&pata_macio_pci_driver);
1401 return rc;
1402 }
1403 return 0;
1404}
1405
1406static void __exit pata_macio_exit(void)
1407{
1408 macio_unregister_driver(&pata_macio_driver);
1409 pci_unregister_driver(&pata_macio_pci_driver);
1410}
1411
1412module_init(pata_macio_init);
1413module_exit(pata_macio_exit);
1414
1415MODULE_AUTHOR("Benjamin Herrenschmidt");
1416MODULE_DESCRIPTION("Apple MacIO PATA driver");
1417MODULE_LICENSE("GPL");
1418MODULE_VERSION(DRV_VERSION);
1419