blob: 03a77713caf2ea1397d0f59507d2482aaf712d64 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Support for IDE interfaces on PowerMacs.
Bartlomiej Zolnierkiewicz58f189f2008-02-01 23:09:33 +01003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * These IDE interfaces are memory-mapped and have a DBDMA channel
5 * for doing DMA.
6 *
7 * Copyright (C) 1998-2003 Paul Mackerras & Ben. Herrenschmidt
Bartlomiej Zolnierkiewiczc15d5d42007-10-11 23:54:01 +02008 * Copyright (C) 2007 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * Some code taken from drivers/ide/ide-dma.c:
16 *
17 * Copyright (c) 1995-1998 Mark Lord
18 *
19 * TODO: - Use pre-calculated (kauai) timing tables all the time and
20 * get rid of the "rounded" tables used previously, so we have the
21 * same table format for all controllers and can then just have one
22 * big table
23 *
24 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/types.h>
26#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
28#include <linux/delay.h>
29#include <linux/ide.h>
30#include <linux/notifier.h>
31#include <linux/reboot.h>
32#include <linux/pci.h>
33#include <linux/adb.h>
34#include <linux/pmu.h>
35#include <linux/scatterlist.h>
36
37#include <asm/prom.h>
38#include <asm/io.h>
39#include <asm/dbdma.h>
40#include <asm/ide.h>
41#include <asm/pci-bridge.h>
42#include <asm/machdep.h>
43#include <asm/pmac_feature.h>
44#include <asm/sections.h>
45#include <asm/irq.h>
46
47#ifndef CONFIG_PPC64
48#include <asm/mediabay.h>
49#endif
50
Andrew Morton9e5755b2007-03-03 17:48:54 +010051#include "../ide-timing.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#undef IDE_PMAC_DEBUG
54
55#define DMA_WAIT_TIMEOUT 50
56
57typedef struct pmac_ide_hwif {
58 unsigned long regbase;
59 int irq;
60 int kind;
61 int aapl_bus_id;
62 unsigned cable_80 : 1;
63 unsigned mediabay : 1;
64 unsigned broken_dma : 1;
65 unsigned broken_dma_warn : 1;
66 struct device_node* node;
67 struct macio_dev *mdev;
68 u32 timings[4];
69 volatile u32 __iomem * *kauai_fcr;
70#ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
71 /* Those fields are duplicating what is in hwif. We currently
72 * can't use the hwif ones because of some assumptions that are
73 * beeing done by the generic code about the kind of dma controller
74 * and format of the dma table. This will have to be fixed though.
75 */
76 volatile struct dbdma_regs __iomem * dma_regs;
77 struct dbdma_cmd* dma_table_cpu;
78#endif
79
80} pmac_ide_hwif_t;
81
Jon Loeligeraacaf9b2005-09-17 10:36:54 -050082static pmac_ide_hwif_t pmac_ide[MAX_HWIFS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static int pmac_ide_count;
84
85enum {
86 controller_ohare, /* OHare based */
87 controller_heathrow, /* Heathrow/Paddington */
88 controller_kl_ata3, /* KeyLargo ATA-3 */
89 controller_kl_ata4, /* KeyLargo ATA-4 */
90 controller_un_ata6, /* UniNorth2 ATA-6 */
91 controller_k2_ata6, /* K2 ATA-6 */
92 controller_sh_ata6, /* Shasta ATA-6 */
93};
94
95static const char* model_name[] = {
96 "OHare ATA", /* OHare based */
97 "Heathrow ATA", /* Heathrow/Paddington */
98 "KeyLargo ATA-3", /* KeyLargo ATA-3 (MDMA only) */
99 "KeyLargo ATA-4", /* KeyLargo ATA-4 (UDMA/66) */
100 "UniNorth ATA-6", /* UniNorth2 ATA-6 (UDMA/100) */
101 "K2 ATA-6", /* K2 ATA-6 (UDMA/100) */
102 "Shasta ATA-6", /* Shasta ATA-6 (UDMA/133) */
103};
104
105/*
106 * Extra registers, both 32-bit little-endian
107 */
108#define IDE_TIMING_CONFIG 0x200
109#define IDE_INTERRUPT 0x300
110
111/* Kauai (U2) ATA has different register setup */
112#define IDE_KAUAI_PIO_CONFIG 0x200
113#define IDE_KAUAI_ULTRA_CONFIG 0x210
114#define IDE_KAUAI_POLL_CONFIG 0x220
115
116/*
117 * Timing configuration register definitions
118 */
119
120/* Number of IDE_SYSCLK_NS ticks, argument is in nanoseconds */
121#define SYSCLK_TICKS(t) (((t) + IDE_SYSCLK_NS - 1) / IDE_SYSCLK_NS)
122#define SYSCLK_TICKS_66(t) (((t) + IDE_SYSCLK_66_NS - 1) / IDE_SYSCLK_66_NS)
123#define IDE_SYSCLK_NS 30 /* 33Mhz cell */
124#define IDE_SYSCLK_66_NS 15 /* 66Mhz cell */
125
126/* 133Mhz cell, found in shasta.
127 * See comments about 100 Mhz Uninorth 2...
128 * Note that PIO_MASK and MDMA_MASK seem to overlap
129 */
130#define TR_133_PIOREG_PIO_MASK 0xff000fff
131#define TR_133_PIOREG_MDMA_MASK 0x00fff800
132#define TR_133_UDMAREG_UDMA_MASK 0x0003ffff
133#define TR_133_UDMAREG_UDMA_EN 0x00000001
134
135/* 100Mhz cell, found in Uninorth 2. I don't have much infos about
136 * this one yet, it appears as a pci device (106b/0033) on uninorth
137 * internal PCI bus and it's clock is controlled like gem or fw. It
138 * appears to be an evolution of keylargo ATA4 with a timing register
139 * extended to 2 32bits registers and a similar DBDMA channel. Other
140 * registers seem to exist but I can't tell much about them.
141 *
142 * So far, I'm using pre-calculated tables for this extracted from
143 * the values used by the MacOS X driver.
144 *
145 * The "PIO" register controls PIO and MDMA timings, the "ULTRA"
146 * register controls the UDMA timings. At least, it seems bit 0
147 * of this one enables UDMA vs. MDMA, and bits 4..7 are the
148 * cycle time in units of 10ns. Bits 8..15 are used by I don't
149 * know their meaning yet
150 */
151#define TR_100_PIOREG_PIO_MASK 0xff000fff
152#define TR_100_PIOREG_MDMA_MASK 0x00fff000
153#define TR_100_UDMAREG_UDMA_MASK 0x0000ffff
154#define TR_100_UDMAREG_UDMA_EN 0x00000001
155
156
157/* 66Mhz cell, found in KeyLargo. Can do ultra mode 0 to 2 on
158 * 40 connector cable and to 4 on 80 connector one.
159 * Clock unit is 15ns (66Mhz)
160 *
161 * 3 Values can be programmed:
162 * - Write data setup, which appears to match the cycle time. They
163 * also call it DIOW setup.
164 * - Ready to pause time (from spec)
165 * - Address setup. That one is weird. I don't see where exactly
166 * it fits in UDMA cycles, I got it's name from an obscure piece
167 * of commented out code in Darwin. They leave it to 0, we do as
168 * well, despite a comment that would lead to think it has a
169 * min value of 45ns.
170 * Apple also add 60ns to the write data setup (or cycle time ?) on
171 * reads.
172 */
173#define TR_66_UDMA_MASK 0xfff00000
174#define TR_66_UDMA_EN 0x00100000 /* Enable Ultra mode for DMA */
175#define TR_66_UDMA_ADDRSETUP_MASK 0xe0000000 /* Address setup */
176#define TR_66_UDMA_ADDRSETUP_SHIFT 29
177#define TR_66_UDMA_RDY2PAUS_MASK 0x1e000000 /* Ready 2 pause time */
178#define TR_66_UDMA_RDY2PAUS_SHIFT 25
179#define TR_66_UDMA_WRDATASETUP_MASK 0x01e00000 /* Write data setup time */
180#define TR_66_UDMA_WRDATASETUP_SHIFT 21
181#define TR_66_MDMA_MASK 0x000ffc00
182#define TR_66_MDMA_RECOVERY_MASK 0x000f8000
183#define TR_66_MDMA_RECOVERY_SHIFT 15
184#define TR_66_MDMA_ACCESS_MASK 0x00007c00
185#define TR_66_MDMA_ACCESS_SHIFT 10
186#define TR_66_PIO_MASK 0x000003ff
187#define TR_66_PIO_RECOVERY_MASK 0x000003e0
188#define TR_66_PIO_RECOVERY_SHIFT 5
189#define TR_66_PIO_ACCESS_MASK 0x0000001f
190#define TR_66_PIO_ACCESS_SHIFT 0
191
192/* 33Mhz cell, found in OHare, Heathrow (& Paddington) and KeyLargo
193 * Can do pio & mdma modes, clock unit is 30ns (33Mhz)
194 *
195 * The access time and recovery time can be programmed. Some older
196 * Darwin code base limit OHare to 150ns cycle time. I decided to do
197 * the same here fore safety against broken old hardware ;)
198 * The HalfTick bit, when set, adds half a clock (15ns) to the access
199 * time and removes one from recovery. It's not supported on KeyLargo
200 * implementation afaik. The E bit appears to be set for PIO mode 0 and
201 * is used to reach long timings used in this mode.
202 */
203#define TR_33_MDMA_MASK 0x003ff800
204#define TR_33_MDMA_RECOVERY_MASK 0x001f0000
205#define TR_33_MDMA_RECOVERY_SHIFT 16
206#define TR_33_MDMA_ACCESS_MASK 0x0000f800
207#define TR_33_MDMA_ACCESS_SHIFT 11
208#define TR_33_MDMA_HALFTICK 0x00200000
209#define TR_33_PIO_MASK 0x000007ff
210#define TR_33_PIO_E 0x00000400
211#define TR_33_PIO_RECOVERY_MASK 0x000003e0
212#define TR_33_PIO_RECOVERY_SHIFT 5
213#define TR_33_PIO_ACCESS_MASK 0x0000001f
214#define TR_33_PIO_ACCESS_SHIFT 0
215
216/*
217 * Interrupt register definitions
218 */
219#define IDE_INTR_DMA 0x80000000
220#define IDE_INTR_DEVICE 0x40000000
221
222/*
223 * FCR Register on Kauai. Not sure what bit 0x4 is ...
224 */
225#define KAUAI_FCR_UATA_MAGIC 0x00000004
226#define KAUAI_FCR_UATA_RESET_N 0x00000002
227#define KAUAI_FCR_UATA_ENABLE 0x00000001
228
229#ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
230
231/* Rounded Multiword DMA timings
232 *
233 * I gave up finding a generic formula for all controller
234 * types and instead, built tables based on timing values
235 * used by Apple in Darwin's implementation.
236 */
237struct mdma_timings_t {
238 int accessTime;
239 int recoveryTime;
240 int cycleTime;
241};
242
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500243struct mdma_timings_t mdma_timings_33[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 { 240, 240, 480 },
246 { 180, 180, 360 },
247 { 135, 135, 270 },
248 { 120, 120, 240 },
249 { 105, 105, 210 },
250 { 90, 90, 180 },
251 { 75, 75, 150 },
252 { 75, 45, 120 },
253 { 0, 0, 0 }
254};
255
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500256struct mdma_timings_t mdma_timings_33k[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 { 240, 240, 480 },
259 { 180, 180, 360 },
260 { 150, 150, 300 },
261 { 120, 120, 240 },
262 { 90, 120, 210 },
263 { 90, 90, 180 },
264 { 90, 60, 150 },
265 { 90, 30, 120 },
266 { 0, 0, 0 }
267};
268
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500269struct mdma_timings_t mdma_timings_66[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 { 240, 240, 480 },
272 { 180, 180, 360 },
273 { 135, 135, 270 },
274 { 120, 120, 240 },
275 { 105, 105, 210 },
276 { 90, 90, 180 },
277 { 90, 75, 165 },
278 { 75, 45, 120 },
279 { 0, 0, 0 }
280};
281
282/* KeyLargo ATA-4 Ultra DMA timings (rounded) */
283struct {
284 int addrSetup; /* ??? */
285 int rdy2pause;
286 int wrDataSetup;
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500287} kl66_udma_timings[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 { 0, 180, 120 }, /* Mode 0 */
290 { 0, 150, 90 }, /* 1 */
291 { 0, 120, 60 }, /* 2 */
292 { 0, 90, 45 }, /* 3 */
293 { 0, 90, 30 } /* 4 */
294};
295
296/* UniNorth 2 ATA/100 timings */
297struct kauai_timing {
298 int cycle_time;
299 u32 timing_reg;
300};
301
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500302static struct kauai_timing kauai_pio_timings[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 { 930 , 0x08000fff },
305 { 600 , 0x08000a92 },
306 { 383 , 0x0800060f },
307 { 360 , 0x08000492 },
308 { 330 , 0x0800048f },
309 { 300 , 0x080003cf },
310 { 270 , 0x080003cc },
311 { 240 , 0x0800038b },
312 { 239 , 0x0800030c },
313 { 180 , 0x05000249 },
Bartlomiej Zolnierkiewiczc15d5d42007-10-11 23:54:01 +0200314 { 120 , 0x04000148 },
315 { 0 , 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316};
317
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500318static struct kauai_timing kauai_mdma_timings[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 { 1260 , 0x00fff000 },
321 { 480 , 0x00618000 },
322 { 360 , 0x00492000 },
323 { 270 , 0x0038e000 },
324 { 240 , 0x0030c000 },
325 { 210 , 0x002cb000 },
326 { 180 , 0x00249000 },
327 { 150 , 0x00209000 },
328 { 120 , 0x00148000 },
329 { 0 , 0 },
330};
331
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500332static struct kauai_timing kauai_udma_timings[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 { 120 , 0x000070c0 },
335 { 90 , 0x00005d80 },
336 { 60 , 0x00004a60 },
337 { 45 , 0x00003a50 },
338 { 30 , 0x00002a30 },
339 { 20 , 0x00002921 },
340 { 0 , 0 },
341};
342
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500343static struct kauai_timing shasta_pio_timings[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 { 930 , 0x08000fff },
346 { 600 , 0x0A000c97 },
347 { 383 , 0x07000712 },
348 { 360 , 0x040003cd },
349 { 330 , 0x040003cd },
350 { 300 , 0x040003cd },
351 { 270 , 0x040003cd },
352 { 240 , 0x040003cd },
353 { 239 , 0x040003cd },
354 { 180 , 0x0400028b },
Bartlomiej Zolnierkiewiczc15d5d42007-10-11 23:54:01 +0200355 { 120 , 0x0400010a },
356 { 0 , 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357};
358
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500359static struct kauai_timing shasta_mdma_timings[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 { 1260 , 0x00fff000 },
362 { 480 , 0x00820800 },
363 { 360 , 0x00820800 },
364 { 270 , 0x00820800 },
365 { 240 , 0x00820800 },
366 { 210 , 0x00820800 },
367 { 180 , 0x00820800 },
368 { 150 , 0x0028b000 },
369 { 120 , 0x001ca000 },
370 { 0 , 0 },
371};
372
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500373static struct kauai_timing shasta_udma133_timings[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 { 120 , 0x00035901, },
376 { 90 , 0x000348b1, },
377 { 60 , 0x00033881, },
378 { 45 , 0x00033861, },
379 { 30 , 0x00033841, },
380 { 20 , 0x00033031, },
381 { 15 , 0x00033021, },
382 { 0 , 0 },
383};
384
385
386static inline u32
387kauai_lookup_timing(struct kauai_timing* table, int cycle_time)
388{
389 int i;
390
391 for (i=0; table[i].cycle_time; i++)
392 if (cycle_time > table[i+1].cycle_time)
393 return table[i].timing_reg;
Bartlomiej Zolnierkiewicz90a87ea2007-10-13 17:47:48 +0200394 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return 0;
396}
397
398/* allow up to 256 DBDMA commands per xfer */
399#define MAX_DCMDS 256
400
401/*
402 * Wait 1s for disk to answer on IDE bus after a hard reset
403 * of the device (via GPIO/FCR).
404 *
405 * Some devices seem to "pollute" the bus even after dropping
406 * the BSY bit (typically some combo drives slave on the UDMA
407 * bus) after a hard reset. Since we hard reset all drives on
408 * KeyLargo ATA66, we have to keep that delay around. I may end
409 * up not hard resetting anymore on these and keep the delay only
410 * for older interfaces instead (we have to reset when coming
411 * from MacOS...) --BenH.
412 */
413#define IDE_WAKEUP_DELAY (1*HZ)
414
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +0100415static int pmac_ide_setup_dma(pmac_ide_hwif_t *pmif, ide_hwif_t *hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static int pmac_ide_build_dmatable(ide_drive_t *drive, struct request *rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417static void pmac_ide_selectproc(ide_drive_t *drive);
418static void pmac_ide_kauai_selectproc(ide_drive_t *drive);
419
420#endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
421
422/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * N.B. this can't be an initfunc, because the media-bay task can
424 * call ide_[un]register at any time.
425 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500426void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427pmac_ide_init_hwif_ports(hw_regs_t *hw,
428 unsigned long data_port, unsigned long ctrl_port,
429 int *irq)
430{
431 int i, ix;
432
433 if (data_port == 0)
434 return;
435
436 for (ix = 0; ix < MAX_HWIFS; ++ix)
437 if (data_port == pmac_ide[ix].regbase)
438 break;
439
Bartlomiej Zolnierkiewiczd26805f2008-01-25 22:17:07 +0100440 if (ix >= MAX_HWIFS)
441 return; /* not an IDE PMAC interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 for (i = 0; i < 8; ++i)
444 hw->io_ports[i] = data_port + i * 0x10;
445 hw->io_ports[8] = data_port + 0x160;
446
447 if (irq != NULL)
448 *irq = pmac_ide[ix].irq;
Benjamin Herrenschmidt22192cc2006-05-20 14:59:53 -0700449
450 hw->dev = &pmac_ide[ix].mdev->ofdev.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200453#define PMAC_IDE_REG(x) \
454 ((void __iomem *)((drive)->hwif->io_ports[IDE_DATA_OFFSET] + (x)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456/*
457 * Apply the timings of the proper unit (master/slave) to the shared
458 * timing register when selecting that unit. This version is for
459 * ASICs with a single timing register
460 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500461static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462pmac_ide_selectproc(ide_drive_t *drive)
463{
464 pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
465
466 if (pmif == NULL)
467 return;
468
469 if (drive->select.b.unit & 0x01)
470 writel(pmif->timings[1], PMAC_IDE_REG(IDE_TIMING_CONFIG));
471 else
472 writel(pmif->timings[0], PMAC_IDE_REG(IDE_TIMING_CONFIG));
473 (void)readl(PMAC_IDE_REG(IDE_TIMING_CONFIG));
474}
475
476/*
477 * Apply the timings of the proper unit (master/slave) to the shared
478 * timing register when selecting that unit. This version is for
479 * ASICs with a dual timing register (Kauai)
480 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500481static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482pmac_ide_kauai_selectproc(ide_drive_t *drive)
483{
484 pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
485
486 if (pmif == NULL)
487 return;
488
489 if (drive->select.b.unit & 0x01) {
490 writel(pmif->timings[1], PMAC_IDE_REG(IDE_KAUAI_PIO_CONFIG));
491 writel(pmif->timings[3], PMAC_IDE_REG(IDE_KAUAI_ULTRA_CONFIG));
492 } else {
493 writel(pmif->timings[0], PMAC_IDE_REG(IDE_KAUAI_PIO_CONFIG));
494 writel(pmif->timings[2], PMAC_IDE_REG(IDE_KAUAI_ULTRA_CONFIG));
495 }
496 (void)readl(PMAC_IDE_REG(IDE_KAUAI_PIO_CONFIG));
497}
498
499/*
500 * Force an update of controller timing values for a given drive
501 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500502static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503pmac_ide_do_update_timings(ide_drive_t *drive)
504{
505 pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
506
507 if (pmif == NULL)
508 return;
509
510 if (pmif->kind == controller_sh_ata6 ||
511 pmif->kind == controller_un_ata6 ||
512 pmif->kind == controller_k2_ata6)
513 pmac_ide_kauai_selectproc(drive);
514 else
515 pmac_ide_selectproc(drive);
516}
517
518static void
519pmac_outbsync(ide_drive_t *drive, u8 value, unsigned long port)
520{
521 u32 tmp;
522
523 writeb(value, (void __iomem *) port);
524 tmp = readl(PMAC_IDE_REG(IDE_TIMING_CONFIG));
525}
526
527/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * Old tuning functions (called on hdparm -p), sets up drive PIO timings
529 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500530static void
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200531pmac_ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Benjamin Herrenschmidt0b46ff22007-10-13 17:47:50 +0200533 u32 *timings, t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 unsigned accessTicks, recTicks;
535 unsigned accessTime, recTime;
536 pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200537 unsigned int cycle_time;
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (pmif == NULL)
540 return;
541
542 /* which drive is it ? */
543 timings = &pmif->timings[drive->select.b.unit & 0x01];
Benjamin Herrenschmidt0b46ff22007-10-13 17:47:50 +0200544 t = *timings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200546 cycle_time = ide_pio_cycle_time(drive, pio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 switch (pmif->kind) {
549 case controller_sh_ata6: {
550 /* 133Mhz cell */
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200551 u32 tr = kauai_lookup_timing(shasta_pio_timings, cycle_time);
Benjamin Herrenschmidt0b46ff22007-10-13 17:47:50 +0200552 t = (t & ~TR_133_PIOREG_PIO_MASK) | tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 break;
554 }
555 case controller_un_ata6:
556 case controller_k2_ata6: {
557 /* 100Mhz cell */
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200558 u32 tr = kauai_lookup_timing(kauai_pio_timings, cycle_time);
Benjamin Herrenschmidt0b46ff22007-10-13 17:47:50 +0200559 t = (t & ~TR_100_PIOREG_PIO_MASK) | tr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 break;
561 }
562 case controller_kl_ata4:
563 /* 66Mhz cell */
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200564 recTime = cycle_time - ide_pio_timings[pio].active_time
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 - ide_pio_timings[pio].setup_time;
566 recTime = max(recTime, 150U);
567 accessTime = ide_pio_timings[pio].active_time;
568 accessTime = max(accessTime, 150U);
569 accessTicks = SYSCLK_TICKS_66(accessTime);
570 accessTicks = min(accessTicks, 0x1fU);
571 recTicks = SYSCLK_TICKS_66(recTime);
572 recTicks = min(recTicks, 0x1fU);
Benjamin Herrenschmidt0b46ff22007-10-13 17:47:50 +0200573 t = (t & ~TR_66_PIO_MASK) |
574 (accessTicks << TR_66_PIO_ACCESS_SHIFT) |
575 (recTicks << TR_66_PIO_RECOVERY_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 break;
577 default: {
578 /* 33Mhz cell */
579 int ebit = 0;
Bartlomiej Zolnierkiewicz7dd00082007-07-20 01:11:56 +0200580 recTime = cycle_time - ide_pio_timings[pio].active_time
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 - ide_pio_timings[pio].setup_time;
582 recTime = max(recTime, 150U);
583 accessTime = ide_pio_timings[pio].active_time;
584 accessTime = max(accessTime, 150U);
585 accessTicks = SYSCLK_TICKS(accessTime);
586 accessTicks = min(accessTicks, 0x1fU);
587 accessTicks = max(accessTicks, 4U);
588 recTicks = SYSCLK_TICKS(recTime);
589 recTicks = min(recTicks, 0x1fU);
590 recTicks = max(recTicks, 5U) - 4;
591 if (recTicks > 9) {
592 recTicks--; /* guess, but it's only for PIO0, so... */
593 ebit = 1;
594 }
Benjamin Herrenschmidt0b46ff22007-10-13 17:47:50 +0200595 t = (t & ~TR_33_PIO_MASK) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 (accessTicks << TR_33_PIO_ACCESS_SHIFT) |
597 (recTicks << TR_33_PIO_RECOVERY_SHIFT);
598 if (ebit)
Benjamin Herrenschmidt0b46ff22007-10-13 17:47:50 +0200599 t |= TR_33_PIO_E;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 break;
601 }
602 }
603
604#ifdef IDE_PMAC_DEBUG
605 printk(KERN_ERR "%s: Set PIO timing for mode %d, reg: 0x%08x\n",
606 drive->name, pio, *timings);
607#endif
608
Benjamin Herrenschmidt0b46ff22007-10-13 17:47:50 +0200609 *timings = t;
Bartlomiej Zolnierkiewiczc15d5d42007-10-11 23:54:01 +0200610 pmac_ide_do_update_timings(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613#ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
614
615/*
616 * Calculate KeyLargo ATA/66 UDMA timings
617 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500618static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619set_timings_udma_ata4(u32 *timings, u8 speed)
620{
621 unsigned rdyToPauseTicks, wrDataSetupTicks, addrTicks;
622
623 if (speed > XFER_UDMA_4)
624 return 1;
625
626 rdyToPauseTicks = SYSCLK_TICKS_66(kl66_udma_timings[speed & 0xf].rdy2pause);
627 wrDataSetupTicks = SYSCLK_TICKS_66(kl66_udma_timings[speed & 0xf].wrDataSetup);
628 addrTicks = SYSCLK_TICKS_66(kl66_udma_timings[speed & 0xf].addrSetup);
629
630 *timings = ((*timings) & ~(TR_66_UDMA_MASK | TR_66_MDMA_MASK)) |
631 (wrDataSetupTicks << TR_66_UDMA_WRDATASETUP_SHIFT) |
632 (rdyToPauseTicks << TR_66_UDMA_RDY2PAUS_SHIFT) |
633 (addrTicks <<TR_66_UDMA_ADDRSETUP_SHIFT) |
634 TR_66_UDMA_EN;
635#ifdef IDE_PMAC_DEBUG
636 printk(KERN_ERR "ide_pmac: Set UDMA timing for mode %d, reg: 0x%08x\n",
637 speed & 0xf, *timings);
638#endif
639
640 return 0;
641}
642
643/*
644 * Calculate Kauai ATA/100 UDMA timings
645 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500646static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647set_timings_udma_ata6(u32 *pio_timings, u32 *ultra_timings, u8 speed)
648{
649 struct ide_timing *t = ide_timing_find_mode(speed);
650 u32 tr;
651
652 if (speed > XFER_UDMA_5 || t == NULL)
653 return 1;
654 tr = kauai_lookup_timing(kauai_udma_timings, (int)t->udma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 *ultra_timings = ((*ultra_timings) & ~TR_100_UDMAREG_UDMA_MASK) | tr;
656 *ultra_timings = (*ultra_timings) | TR_100_UDMAREG_UDMA_EN;
657
658 return 0;
659}
660
661/*
662 * Calculate Shasta ATA/133 UDMA timings
663 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500664static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665set_timings_udma_shasta(u32 *pio_timings, u32 *ultra_timings, u8 speed)
666{
667 struct ide_timing *t = ide_timing_find_mode(speed);
668 u32 tr;
669
670 if (speed > XFER_UDMA_6 || t == NULL)
671 return 1;
672 tr = kauai_lookup_timing(shasta_udma133_timings, (int)t->udma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 *ultra_timings = ((*ultra_timings) & ~TR_133_UDMAREG_UDMA_MASK) | tr;
674 *ultra_timings = (*ultra_timings) | TR_133_UDMAREG_UDMA_EN;
675
676 return 0;
677}
678
679/*
680 * Calculate MDMA timings for all cells
681 */
Bartlomiej Zolnierkiewicz90f72ec2007-10-13 17:47:48 +0200682static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683set_timings_mdma(ide_drive_t *drive, int intf_type, u32 *timings, u32 *timings2,
Bartlomiej Zolnierkiewicz90f72ec2007-10-13 17:47:48 +0200684 u8 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 int cycleTime, accessTime = 0, recTime = 0;
687 unsigned accessTicks, recTicks;
Bartlomiej Zolnierkiewicz90f72ec2007-10-13 17:47:48 +0200688 struct hd_driveid *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct mdma_timings_t* tm = NULL;
690 int i;
691
692 /* Get default cycle time for mode */
693 switch(speed & 0xf) {
694 case 0: cycleTime = 480; break;
695 case 1: cycleTime = 150; break;
696 case 2: cycleTime = 120; break;
697 default:
Bartlomiej Zolnierkiewicz90f72ec2007-10-13 17:47:48 +0200698 BUG();
699 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Bartlomiej Zolnierkiewicz90f72ec2007-10-13 17:47:48 +0200701
702 /* Check if drive provides explicit DMA cycle time */
703 if ((id->field_valid & 2) && id->eide_dma_time)
704 cycleTime = max_t(int, id->eide_dma_time, cycleTime);
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /* OHare limits according to some old Apple sources */
707 if ((intf_type == controller_ohare) && (cycleTime < 150))
708 cycleTime = 150;
709 /* Get the proper timing array for this controller */
710 switch(intf_type) {
711 case controller_sh_ata6:
712 case controller_un_ata6:
713 case controller_k2_ata6:
714 break;
715 case controller_kl_ata4:
716 tm = mdma_timings_66;
717 break;
718 case controller_kl_ata3:
719 tm = mdma_timings_33k;
720 break;
721 default:
722 tm = mdma_timings_33;
723 break;
724 }
725 if (tm != NULL) {
726 /* Lookup matching access & recovery times */
727 i = -1;
728 for (;;) {
729 if (tm[i+1].cycleTime < cycleTime)
730 break;
731 i++;
732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 cycleTime = tm[i].cycleTime;
734 accessTime = tm[i].accessTime;
735 recTime = tm[i].recoveryTime;
736
737#ifdef IDE_PMAC_DEBUG
738 printk(KERN_ERR "%s: MDMA, cycleTime: %d, accessTime: %d, recTime: %d\n",
739 drive->name, cycleTime, accessTime, recTime);
740#endif
741 }
742 switch(intf_type) {
743 case controller_sh_ata6: {
744 /* 133Mhz cell */
745 u32 tr = kauai_lookup_timing(shasta_mdma_timings, cycleTime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 *timings = ((*timings) & ~TR_133_PIOREG_MDMA_MASK) | tr;
747 *timings2 = (*timings2) & ~TR_133_UDMAREG_UDMA_EN;
748 }
749 case controller_un_ata6:
750 case controller_k2_ata6: {
751 /* 100Mhz cell */
752 u32 tr = kauai_lookup_timing(kauai_mdma_timings, cycleTime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 *timings = ((*timings) & ~TR_100_PIOREG_MDMA_MASK) | tr;
754 *timings2 = (*timings2) & ~TR_100_UDMAREG_UDMA_EN;
755 }
756 break;
757 case controller_kl_ata4:
758 /* 66Mhz cell */
759 accessTicks = SYSCLK_TICKS_66(accessTime);
760 accessTicks = min(accessTicks, 0x1fU);
761 accessTicks = max(accessTicks, 0x1U);
762 recTicks = SYSCLK_TICKS_66(recTime);
763 recTicks = min(recTicks, 0x1fU);
764 recTicks = max(recTicks, 0x3U);
765 /* Clear out mdma bits and disable udma */
766 *timings = ((*timings) & ~(TR_66_MDMA_MASK | TR_66_UDMA_MASK)) |
767 (accessTicks << TR_66_MDMA_ACCESS_SHIFT) |
768 (recTicks << TR_66_MDMA_RECOVERY_SHIFT);
769 break;
770 case controller_kl_ata3:
771 /* 33Mhz cell on KeyLargo */
772 accessTicks = SYSCLK_TICKS(accessTime);
773 accessTicks = max(accessTicks, 1U);
774 accessTicks = min(accessTicks, 0x1fU);
775 accessTime = accessTicks * IDE_SYSCLK_NS;
776 recTicks = SYSCLK_TICKS(recTime);
777 recTicks = max(recTicks, 1U);
778 recTicks = min(recTicks, 0x1fU);
779 *timings = ((*timings) & ~TR_33_MDMA_MASK) |
780 (accessTicks << TR_33_MDMA_ACCESS_SHIFT) |
781 (recTicks << TR_33_MDMA_RECOVERY_SHIFT);
782 break;
783 default: {
784 /* 33Mhz cell on others */
785 int halfTick = 0;
786 int origAccessTime = accessTime;
787 int origRecTime = recTime;
788
789 accessTicks = SYSCLK_TICKS(accessTime);
790 accessTicks = max(accessTicks, 1U);
791 accessTicks = min(accessTicks, 0x1fU);
792 accessTime = accessTicks * IDE_SYSCLK_NS;
793 recTicks = SYSCLK_TICKS(recTime);
794 recTicks = max(recTicks, 2U) - 1;
795 recTicks = min(recTicks, 0x1fU);
796 recTime = (recTicks + 1) * IDE_SYSCLK_NS;
797 if ((accessTicks > 1) &&
798 ((accessTime - IDE_SYSCLK_NS/2) >= origAccessTime) &&
799 ((recTime - IDE_SYSCLK_NS/2) >= origRecTime)) {
800 halfTick = 1;
801 accessTicks--;
802 }
803 *timings = ((*timings) & ~TR_33_MDMA_MASK) |
804 (accessTicks << TR_33_MDMA_ACCESS_SHIFT) |
805 (recTicks << TR_33_MDMA_RECOVERY_SHIFT);
806 if (halfTick)
807 *timings |= TR_33_MDMA_HALFTICK;
808 }
809 }
810#ifdef IDE_PMAC_DEBUG
811 printk(KERN_ERR "%s: Set MDMA timing for mode %d, reg: 0x%08x\n",
812 drive->name, speed & 0xf, *timings);
813#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815#endif /* #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC */
816
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200817static void pmac_ide_set_dma_mode(ide_drive_t *drive, const u8 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 int unit = (drive->select.b.unit & 0x01);
820 int ret = 0;
821 pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
Bartlomiej Zolnierkiewicz085798b2007-10-13 17:47:48 +0200822 u32 *timings, *timings2, tl[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 timings = &pmif->timings[unit];
825 timings2 = &pmif->timings[unit+2];
Bartlomiej Zolnierkiewicz085798b2007-10-13 17:47:48 +0200826
827 /* Copy timings to local image */
828 tl[0] = *timings;
829 tl[1] = *timings2;
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831#ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
Bartlomiej Zolnierkiewicz4db90a12008-01-25 22:17:18 +0100832 if (speed >= XFER_UDMA_0) {
833 if (pmif->kind == controller_kl_ata4)
834 ret = set_timings_udma_ata4(&tl[0], speed);
835 else if (pmif->kind == controller_un_ata6
836 || pmif->kind == controller_k2_ata6)
837 ret = set_timings_udma_ata6(&tl[0], &tl[1], speed);
838 else if (pmif->kind == controller_sh_ata6)
839 ret = set_timings_udma_shasta(&tl[0], &tl[1], speed);
840 else
841 ret = -1;
842 } else
843 set_timings_mdma(drive, pmif->kind, &tl[0], &tl[1], speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844#endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (ret)
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200846 return;
Bartlomiej Zolnierkiewicz085798b2007-10-13 17:47:48 +0200847
848 /* Apply timings to controller */
849 *timings = tl[0];
850 *timings2 = tl[1];
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 pmac_ide_do_update_timings(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
855/*
856 * Blast some well known "safe" values to the timing registers at init or
857 * wakeup from sleep time, before we do real calculation
858 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500859static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860sanitize_timings(pmac_ide_hwif_t *pmif)
861{
862 unsigned int value, value2 = 0;
863
864 switch(pmif->kind) {
865 case controller_sh_ata6:
866 value = 0x0a820c97;
867 value2 = 0x00033031;
868 break;
869 case controller_un_ata6:
870 case controller_k2_ata6:
871 value = 0x08618a92;
872 value2 = 0x00002921;
873 break;
874 case controller_kl_ata4:
875 value = 0x0008438c;
876 break;
877 case controller_kl_ata3:
878 value = 0x00084526;
879 break;
880 case controller_heathrow:
881 case controller_ohare:
882 default:
883 value = 0x00074526;
884 break;
885 }
886 pmif->timings[0] = pmif->timings[1] = value;
887 pmif->timings[2] = pmif->timings[3] = value2;
888}
889
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500890unsigned long
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891pmac_ide_get_base(int index)
892{
893 return pmac_ide[index].regbase;
894}
895
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500896int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897pmac_ide_check_base(unsigned long base)
898{
899 int ix;
900
901 for (ix = 0; ix < MAX_HWIFS; ++ix)
902 if (base == pmac_ide[ix].regbase)
903 return ix;
904 return -1;
905}
906
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500907int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908pmac_ide_get_irq(unsigned long base)
909{
910 int ix;
911
912 for (ix = 0; ix < MAX_HWIFS; ++ix)
913 if (base == pmac_ide[ix].regbase)
914 return pmac_ide[ix].irq;
915 return 0;
916}
917
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500918static int ide_majors[] = { 3, 22, 33, 34, 56, 57 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920dev_t __init
921pmac_find_ide_boot(char *bootdevice, int n)
922{
923 int i;
924
925 /*
926 * Look through the list of IDE interfaces for this one.
927 */
928 for (i = 0; i < pmac_ide_count; ++i) {
929 char *name;
930 if (!pmac_ide[i].node || !pmac_ide[i].node->full_name)
931 continue;
932 name = pmac_ide[i].node->full_name;
933 if (memcmp(name, bootdevice, n) == 0 && name[n] == 0) {
934 /* XXX should cope with the 2nd drive as well... */
935 return MKDEV(ide_majors[i], 0);
936 }
937 }
938
939 return 0;
940}
941
942/* Suspend call back, should be called after the child devices
943 * have actually been suspended
944 */
945static int
946pmac_ide_do_suspend(ide_hwif_t *hwif)
947{
948 pmac_ide_hwif_t *pmif = (pmac_ide_hwif_t *)hwif->hwif_data;
949
950 /* We clear the timings */
951 pmif->timings[0] = 0;
952 pmif->timings[1] = 0;
953
Benjamin Herrenschmidt616299a2005-05-01 08:58:41 -0700954 disable_irq(pmif->irq);
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /* The media bay will handle itself just fine */
957 if (pmif->mediabay)
958 return 0;
959
960 /* Kauai has bus control FCRs directly here */
961 if (pmif->kauai_fcr) {
962 u32 fcr = readl(pmif->kauai_fcr);
963 fcr &= ~(KAUAI_FCR_UATA_RESET_N | KAUAI_FCR_UATA_ENABLE);
964 writel(fcr, pmif->kauai_fcr);
965 }
966
967 /* Disable the bus on older machines and the cell on kauai */
968 ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, pmif->node, pmif->aapl_bus_id,
969 0);
970
971 return 0;
972}
973
974/* Resume call back, should be called before the child devices
975 * are resumed
976 */
977static int
978pmac_ide_do_resume(ide_hwif_t *hwif)
979{
980 pmac_ide_hwif_t *pmif = (pmac_ide_hwif_t *)hwif->hwif_data;
981
982 /* Hard reset & re-enable controller (do we really need to reset ? -BenH) */
983 if (!pmif->mediabay) {
984 ppc_md.feature_call(PMAC_FTR_IDE_RESET, pmif->node, pmif->aapl_bus_id, 1);
985 ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, pmif->node, pmif->aapl_bus_id, 1);
986 msleep(10);
987 ppc_md.feature_call(PMAC_FTR_IDE_RESET, pmif->node, pmif->aapl_bus_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 /* Kauai has it different */
990 if (pmif->kauai_fcr) {
991 u32 fcr = readl(pmif->kauai_fcr);
992 fcr |= KAUAI_FCR_UATA_RESET_N | KAUAI_FCR_UATA_ENABLE;
993 writel(fcr, pmif->kauai_fcr);
994 }
Benjamin Herrenschmidt616299a2005-05-01 08:58:41 -0700995
996 msleep(jiffies_to_msecs(IDE_WAKEUP_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998
999 /* Sanitize drive timings */
1000 sanitize_timings(pmif);
1001
Benjamin Herrenschmidt616299a2005-05-01 08:58:41 -07001002 enable_irq(pmif->irq);
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return 0;
1005}
1006
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001007static const struct ide_port_info pmac_port_info = {
1008 .chipset = ide_pmac,
1009 .host_flags = IDE_HFLAG_SET_PIO_MODE_KEEP_DMA |
1010 IDE_HFLAG_PIO_NO_DOWNGRADE |
1011 IDE_HFLAG_POST_SET_MODE |
1012 IDE_HFLAG_NO_DMA | /* no SFF-style DMA */
1013 IDE_HFLAG_UNMASK_IRQS,
1014 .pio_mask = ATA_PIO4,
1015 .mwdma_mask = ATA_MWDMA2,
1016};
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018/*
1019 * Setup, register & probe an IDE channel driven by this driver, this is
1020 * called by one of the 2 probe functions (macio or PCI). Note that a channel
1021 * that ends up beeing free of any device is not kept around by this driver
1022 * (it is kept in 2.4). This introduce an interface numbering change on some
1023 * rare machines unfortunately, but it's better this way.
1024 */
Adrian Bunk468e4682008-02-01 23:09:16 +01001025static int __devinit
Bartlomiej Zolnierkiewicz57c802e2008-01-26 20:13:05 +01001026pmac_ide_setup_device(pmac_ide_hwif_t *pmif, ide_hwif_t *hwif, hw_regs_t *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 struct device_node *np = pmif->node;
Jeremy Kerr018a3d12006-07-12 15:40:29 +10001029 const int *bidp;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001030 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001031 struct ide_port_info d = pmac_port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 pmif->cable_80 = 0;
1034 pmif->broken_dma = pmif->broken_dma_warn = 0;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001035 if (of_device_is_compatible(np, "shasta-ata")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 pmif->kind = controller_sh_ata6;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001037 d.udma_mask = ATA_UDMA6;
1038 } else if (of_device_is_compatible(np, "kauai-ata")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 pmif->kind = controller_un_ata6;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001040 d.udma_mask = ATA_UDMA5;
1041 } else if (of_device_is_compatible(np, "K2-UATA")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 pmif->kind = controller_k2_ata6;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001043 d.udma_mask = ATA_UDMA5;
1044 } else if (of_device_is_compatible(np, "keylargo-ata")) {
1045 if (strcmp(np->name, "ata-4") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 pmif->kind = controller_kl_ata4;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001047 d.udma_mask = ATA_UDMA4;
1048 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 pmif->kind = controller_kl_ata3;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001050 } else if (of_device_is_compatible(np, "heathrow-ata")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 pmif->kind = controller_heathrow;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001052 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 pmif->kind = controller_ohare;
1054 pmif->broken_dma = 1;
1055 }
1056
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10001057 bidp = of_get_property(np, "AAPL,bus-id", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 pmif->aapl_bus_id = bidp ? *bidp : 0;
1059
1060 /* Get cable type from device-tree */
1061 if (pmif->kind == controller_kl_ata4 || pmif->kind == controller_un_ata6
1062 || pmif->kind == controller_k2_ata6
1063 || pmif->kind == controller_sh_ata6) {
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10001064 const char* cable = of_get_property(np, "cable-type", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (cable && !strncmp(cable, "80-", 3))
1066 pmif->cable_80 = 1;
1067 }
1068 /* G5's seem to have incorrect cable type in device-tree. Let's assume
1069 * they have a 80 conductor cable, this seem to be always the case unless
1070 * the user mucked around
1071 */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +10001072 if (of_device_is_compatible(np, "K2-UATA") ||
1073 of_device_is_compatible(np, "shasta-ata"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 pmif->cable_80 = 1;
1075
1076 /* On Kauai-type controllers, we make sure the FCR is correct */
1077 if (pmif->kauai_fcr)
1078 writel(KAUAI_FCR_UATA_MAGIC |
1079 KAUAI_FCR_UATA_RESET_N |
1080 KAUAI_FCR_UATA_ENABLE, pmif->kauai_fcr);
1081
1082 pmif->mediabay = 0;
1083
1084 /* Make sure we have sane timings */
1085 sanitize_timings(pmif);
1086
1087#ifndef CONFIG_PPC64
1088 /* XXX FIXME: Media bay stuff need re-organizing */
1089 if (np->parent && np->parent->name
1090 && strcasecmp(np->parent->name, "media-bay") == 0) {
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001091#ifdef CONFIG_PMAC_MEDIABAY
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001092 media_bay_set_ide_infos(np->parent, pmif->regbase, pmif->irq,
1093 hwif);
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001094#endif /* CONFIG_PMAC_MEDIABAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 pmif->mediabay = 1;
1096 if (!bidp)
1097 pmif->aapl_bus_id = 1;
1098 } else if (pmif->kind == controller_ohare) {
1099 /* The code below is having trouble on some ohare machines
1100 * (timing related ?). Until I can put my hand on one of these
1101 * units, I keep the old way
1102 */
1103 ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, np, 0, 1);
1104 } else
1105#endif
1106 {
1107 /* This is necessary to enable IDE when net-booting */
1108 ppc_md.feature_call(PMAC_FTR_IDE_RESET, np, pmif->aapl_bus_id, 1);
1109 ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, np, pmif->aapl_bus_id, 1);
1110 msleep(10);
1111 ppc_md.feature_call(PMAC_FTR_IDE_RESET, np, pmif->aapl_bus_id, 0);
1112 msleep(jiffies_to_msecs(IDE_WAKEUP_DELAY));
1113 }
1114
1115 /* Setup MMIO ops */
1116 default_hwif_mmiops(hwif);
1117 hwif->OUTBSYNC = pmac_outbsync;
1118
1119 /* Tell common code _not_ to mess with resources */
Bartlomiej Zolnierkiewicz2ad1e552007-02-17 02:40:25 +01001120 hwif->mmio = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 hwif->hwif_data = pmif;
Bartlomiej Zolnierkiewicz57c802e2008-01-26 20:13:05 +01001122 ide_init_port_hw(hwif, hw);
1123 hwif->noprobe = pmif->mediabay;
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +02001124 hwif->cbl = pmif->cable_80 ? ATA_CBL_PATA80 : ATA_CBL_PATA40;
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +02001125 hwif->set_pio_mode = pmac_ide_set_pio_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (pmif->kind == controller_un_ata6
1127 || pmif->kind == controller_k2_ata6
1128 || pmif->kind == controller_sh_ata6)
1129 hwif->selectproc = pmac_ide_kauai_selectproc;
1130 else
1131 hwif->selectproc = pmac_ide_selectproc;
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +02001132 hwif->set_dma_mode = pmac_ide_set_dma_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 printk(KERN_INFO "ide%d: Found Apple %s controller, bus ID %d%s, irq %d\n",
1135 hwif->index, model_name[pmif->kind], pmif->aapl_bus_id,
1136 pmif->mediabay ? " (mediabay)" : "", hwif->irq);
1137
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001138#ifdef CONFIG_PMAC_MEDIABAY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (pmif->mediabay && check_media_bay_by_base(pmif->regbase, MB_CD) == 0)
1140 hwif->noprobe = 0;
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001141#endif /* CONFIG_PMAC_MEDIABAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143#ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001144 if (pmif->cable_80 == 0)
1145 d.udma_mask &= ATA_UDMA2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 /* has a DBDMA controller channel */
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001147 if (pmif->dma_regs == 0 || pmac_ide_setup_dma(pmif, hwif) < 0)
1148#endif
1149 d.udma_mask = d.mwdma_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001151 idx[0] = hwif->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001153 ide_device_add(idx, &d);
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +02001154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return 0;
1156}
1157
1158/*
1159 * Attach to a macio probed interface
1160 */
1161static int __devinit
Jeff Mahoney5e655772005-07-06 15:44:41 -04001162pmac_ide_macio_attach(struct macio_dev *mdev, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 void __iomem *base;
1165 unsigned long regbase;
1166 int irq;
1167 ide_hwif_t *hwif;
1168 pmac_ide_hwif_t *pmif;
1169 int i, rc;
Bartlomiej Zolnierkiewicz57c802e2008-01-26 20:13:05 +01001170 hw_regs_t hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 i = 0;
1173 while (i < MAX_HWIFS && (ide_hwifs[i].io_ports[IDE_DATA_OFFSET] != 0
1174 || pmac_ide[i].node != NULL))
1175 ++i;
1176 if (i >= MAX_HWIFS) {
1177 printk(KERN_ERR "ide-pmac: MacIO interface attach with no slot\n");
1178 printk(KERN_ERR " %s\n", mdev->ofdev.node->full_name);
1179 return -ENODEV;
1180 }
1181
1182 pmif = &pmac_ide[i];
1183 hwif = &ide_hwifs[i];
1184
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001185 if (macio_resource_count(mdev) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 printk(KERN_WARNING "ide%d: no address for %s\n",
1187 i, mdev->ofdev.node->full_name);
1188 return -ENXIO;
1189 }
1190
1191 /* Request memory resource for IO ports */
1192 if (macio_request_resource(mdev, 0, "ide-pmac (ports)")) {
1193 printk(KERN_ERR "ide%d: can't request mmio resource !\n", i);
1194 return -EBUSY;
1195 }
1196
1197 /* XXX This is bogus. Should be fixed in the registry by checking
1198 * the kind of host interrupt controller, a bit like gatwick
1199 * fixes in irq.c. That works well enough for the single case
1200 * where that happens though...
1201 */
1202 if (macio_irq_count(mdev) == 0) {
1203 printk(KERN_WARNING "ide%d: no intrs for device %s, using 13\n",
1204 i, mdev->ofdev.node->full_name);
Benjamin Herrenschmidt69917c22006-09-22 12:56:30 +10001205 irq = irq_create_mapping(NULL, 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 } else
1207 irq = macio_irq(mdev, 0);
1208
1209 base = ioremap(macio_resource_start(mdev, 0), 0x400);
1210 regbase = (unsigned long) base;
1211
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +01001212 hwif->dev = &mdev->bus->pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 pmif->mdev = mdev;
1215 pmif->node = mdev->ofdev.node;
1216 pmif->regbase = regbase;
1217 pmif->irq = irq;
1218 pmif->kauai_fcr = NULL;
1219#ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
1220 if (macio_resource_count(mdev) >= 2) {
1221 if (macio_request_resource(mdev, 1, "ide-pmac (dma)"))
1222 printk(KERN_WARNING "ide%d: can't request DMA resource !\n", i);
1223 else
1224 pmif->dma_regs = ioremap(macio_resource_start(mdev, 1), 0x1000);
1225 } else
1226 pmif->dma_regs = NULL;
1227#endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
1228 dev_set_drvdata(&mdev->ofdev.dev, hwif);
1229
Bartlomiej Zolnierkiewicz57c802e2008-01-26 20:13:05 +01001230 memset(&hw, 0, sizeof(hw));
1231 pmac_ide_init_hwif_ports(&hw, pmif->regbase, 0, NULL);
1232 hw.irq = irq;
1233 hw.dev = &mdev->ofdev.dev;
1234
1235 rc = pmac_ide_setup_device(pmif, hwif, &hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (rc != 0) {
1237 /* The inteface is released to the common IDE layer */
1238 dev_set_drvdata(&mdev->ofdev.dev, NULL);
1239 iounmap(base);
Bartlomiej Zolnierkiewiczed908fa2008-02-01 23:09:32 +01001240 if (pmif->dma_regs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 iounmap(pmif->dma_regs);
Bartlomiej Zolnierkiewiczed908fa2008-02-01 23:09:32 +01001242 macio_release_resource(mdev, 1);
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 memset(pmif, 0, sizeof(*pmif));
1245 macio_release_resource(mdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
1248 return rc;
1249}
1250
1251static int
David Brownell8b4b8a22006-08-14 23:11:03 -07001252pmac_ide_macio_suspend(struct macio_dev *mdev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 ide_hwif_t *hwif = (ide_hwif_t *)dev_get_drvdata(&mdev->ofdev.dev);
1255 int rc = 0;
1256
David Brownell8b4b8a22006-08-14 23:11:03 -07001257 if (mesg.event != mdev->ofdev.dev.power.power_state.event
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01001258 && (mesg.event & PM_EVENT_SLEEP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 rc = pmac_ide_do_suspend(hwif);
1260 if (rc == 0)
David Brownell8b4b8a22006-08-14 23:11:03 -07001261 mdev->ofdev.dev.power.power_state = mesg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263
1264 return rc;
1265}
1266
1267static int
1268pmac_ide_macio_resume(struct macio_dev *mdev)
1269{
1270 ide_hwif_t *hwif = (ide_hwif_t *)dev_get_drvdata(&mdev->ofdev.dev);
1271 int rc = 0;
1272
Pavel Machekca078ba2005-09-03 15:56:57 -07001273 if (mdev->ofdev.dev.power.power_state.event != PM_EVENT_ON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 rc = pmac_ide_do_resume(hwif);
1275 if (rc == 0)
Pavel Machek829ca9a2005-09-03 15:56:56 -07001276 mdev->ofdev.dev.power.power_state = PMSG_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278
1279 return rc;
1280}
1281
1282/*
1283 * Attach to a PCI probed interface
1284 */
1285static int __devinit
1286pmac_ide_pci_attach(struct pci_dev *pdev, const struct pci_device_id *id)
1287{
1288 ide_hwif_t *hwif;
1289 struct device_node *np;
1290 pmac_ide_hwif_t *pmif;
1291 void __iomem *base;
1292 unsigned long rbase, rlen;
1293 int i, rc;
Bartlomiej Zolnierkiewicz57c802e2008-01-26 20:13:05 +01001294 hw_regs_t hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 np = pci_device_to_OF_node(pdev);
1297 if (np == NULL) {
1298 printk(KERN_ERR "ide-pmac: cannot find MacIO node for Kauai ATA interface\n");
1299 return -ENODEV;
1300 }
1301 i = 0;
1302 while (i < MAX_HWIFS && (ide_hwifs[i].io_ports[IDE_DATA_OFFSET] != 0
1303 || pmac_ide[i].node != NULL))
1304 ++i;
1305 if (i >= MAX_HWIFS) {
1306 printk(KERN_ERR "ide-pmac: PCI interface attach with no slot\n");
1307 printk(KERN_ERR " %s\n", np->full_name);
1308 return -ENODEV;
1309 }
1310
1311 pmif = &pmac_ide[i];
1312 hwif = &ide_hwifs[i];
1313
1314 if (pci_enable_device(pdev)) {
1315 printk(KERN_WARNING "ide%i: Can't enable PCI device for %s\n",
1316 i, np->full_name);
1317 return -ENXIO;
1318 }
1319 pci_set_master(pdev);
1320
1321 if (pci_request_regions(pdev, "Kauai ATA")) {
1322 printk(KERN_ERR "ide%d: Cannot obtain PCI resources for %s\n",
1323 i, np->full_name);
1324 return -ENXIO;
1325 }
1326
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +01001327 hwif->dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 pmif->mdev = NULL;
1329 pmif->node = np;
1330
1331 rbase = pci_resource_start(pdev, 0);
1332 rlen = pci_resource_len(pdev, 0);
1333
1334 base = ioremap(rbase, rlen);
1335 pmif->regbase = (unsigned long) base + 0x2000;
1336#ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
1337 pmif->dma_regs = base + 0x1000;
1338#endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
1339 pmif->kauai_fcr = base;
1340 pmif->irq = pdev->irq;
1341
1342 pci_set_drvdata(pdev, hwif);
1343
Bartlomiej Zolnierkiewicz57c802e2008-01-26 20:13:05 +01001344 memset(&hw, 0, sizeof(hw));
1345 pmac_ide_init_hwif_ports(&hw, pmif->regbase, 0, NULL);
1346 hw.irq = pdev->irq;
1347 hw.dev = &pdev->dev;
1348
1349 rc = pmac_ide_setup_device(pmif, hwif, &hw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (rc != 0) {
1351 /* The inteface is released to the common IDE layer */
1352 pci_set_drvdata(pdev, NULL);
1353 iounmap(base);
1354 memset(pmif, 0, sizeof(*pmif));
1355 pci_release_regions(pdev);
1356 }
1357
1358 return rc;
1359}
1360
1361static int
David Brownell8b4b8a22006-08-14 23:11:03 -07001362pmac_ide_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 ide_hwif_t *hwif = (ide_hwif_t *)pci_get_drvdata(pdev);
1365 int rc = 0;
1366
David Brownell8b4b8a22006-08-14 23:11:03 -07001367 if (mesg.event != pdev->dev.power.power_state.event
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01001368 && (mesg.event & PM_EVENT_SLEEP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 rc = pmac_ide_do_suspend(hwif);
1370 if (rc == 0)
David Brownell8b4b8a22006-08-14 23:11:03 -07001371 pdev->dev.power.power_state = mesg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373
1374 return rc;
1375}
1376
1377static int
1378pmac_ide_pci_resume(struct pci_dev *pdev)
1379{
1380 ide_hwif_t *hwif = (ide_hwif_t *)pci_get_drvdata(pdev);
1381 int rc = 0;
1382
Pavel Machekca078ba2005-09-03 15:56:57 -07001383 if (pdev->dev.power.power_state.event != PM_EVENT_ON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 rc = pmac_ide_do_resume(hwif);
1385 if (rc == 0)
Pavel Machek829ca9a2005-09-03 15:56:56 -07001386 pdev->dev.power.power_state = PMSG_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388
1389 return rc;
1390}
1391
Jeff Mahoney5e655772005-07-06 15:44:41 -04001392static struct of_device_id pmac_ide_macio_match[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394 {
1395 .name = "IDE",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 },
1397 {
1398 .name = "ATA",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 },
1400 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 .type = "ide",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 },
1403 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 .type = "ata",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 },
1406 {},
1407};
1408
1409static struct macio_driver pmac_ide_macio_driver =
1410{
1411 .name = "ide-pmac",
1412 .match_table = pmac_ide_macio_match,
1413 .probe = pmac_ide_macio_attach,
1414 .suspend = pmac_ide_macio_suspend,
1415 .resume = pmac_ide_macio_resume,
1416};
1417
Bartlomiej Zolnierkiewicz9cbcc5e2007-10-16 22:29:56 +02001418static const struct pci_device_id pmac_ide_pci_match[] = {
1419 { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_UNI_N_ATA), 0 },
1420 { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_IPID_ATA100), 0 },
1421 { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_K2_ATA100), 0 },
1422 { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_SH_ATA), 0 },
1423 { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_IPID2_ATA), 0 },
Benjamin Herrenschmidt71e4eda2007-10-06 18:52:27 +10001424 {},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425};
1426
1427static struct pci_driver pmac_ide_pci_driver = {
1428 .name = "ide-pmac",
1429 .id_table = pmac_ide_pci_match,
1430 .probe = pmac_ide_pci_attach,
1431 .suspend = pmac_ide_pci_suspend,
1432 .resume = pmac_ide_pci_resume,
1433};
1434MODULE_DEVICE_TABLE(pci, pmac_ide_pci_match);
1435
Andrew Morton9e5755b2007-03-03 17:48:54 +01001436int __init pmac_ide_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Andrew Morton9e5755b2007-03-03 17:48:54 +01001438 int error;
1439
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001440 if (!machine_is(powermac))
Andrew Morton9e5755b2007-03-03 17:48:54 +01001441 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443#ifdef CONFIG_BLK_DEV_IDE_PMAC_ATA100FIRST
Andrew Morton9e5755b2007-03-03 17:48:54 +01001444 error = pci_register_driver(&pmac_ide_pci_driver);
1445 if (error)
1446 goto out;
1447 error = macio_register_driver(&pmac_ide_macio_driver);
1448 if (error) {
1449 pci_unregister_driver(&pmac_ide_pci_driver);
1450 goto out;
1451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452#else
Andrew Morton9e5755b2007-03-03 17:48:54 +01001453 error = macio_register_driver(&pmac_ide_macio_driver);
1454 if (error)
1455 goto out;
1456 error = pci_register_driver(&pmac_ide_pci_driver);
1457 if (error) {
1458 macio_unregister_driver(&pmac_ide_macio_driver);
1459 goto out;
1460 }
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001461#endif
Andrew Morton9e5755b2007-03-03 17:48:54 +01001462out:
1463 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466#ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
1467
1468/*
1469 * pmac_ide_build_dmatable builds the DBDMA command list
1470 * for a transfer and sets the DBDMA channel to point to it.
1471 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001472static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473pmac_ide_build_dmatable(ide_drive_t *drive, struct request *rq)
1474{
1475 struct dbdma_cmd *table;
1476 int i, count = 0;
1477 ide_hwif_t *hwif = HWIF(drive);
1478 pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)hwif->hwif_data;
1479 volatile struct dbdma_regs __iomem *dma = pmif->dma_regs;
1480 struct scatterlist *sg;
1481 int wr = (rq_data_dir(rq) == WRITE);
1482
1483 /* DMA table is already aligned */
1484 table = (struct dbdma_cmd *) pmif->dma_table_cpu;
1485
1486 /* Make sure DMA controller is stopped (necessary ?) */
1487 writel((RUN|PAUSE|FLUSH|WAKE|DEAD) << 16, &dma->control);
1488 while (readl(&dma->status) & RUN)
1489 udelay(1);
1490
1491 hwif->sg_nents = i = ide_build_sglist(drive, rq);
1492
1493 if (!i)
1494 return 0;
1495
1496 /* Build DBDMA commands list */
1497 sg = hwif->sg_table;
1498 while (i && sg_dma_len(sg)) {
1499 u32 cur_addr;
1500 u32 cur_len;
1501
1502 cur_addr = sg_dma_address(sg);
1503 cur_len = sg_dma_len(sg);
1504
1505 if (pmif->broken_dma && cur_addr & (L1_CACHE_BYTES - 1)) {
1506 if (pmif->broken_dma_warn == 0) {
Joe Perchesaca38a52007-11-27 21:35:55 +01001507 printk(KERN_WARNING "%s: DMA on non aligned address, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 "switching to PIO on Ohare chipset\n", drive->name);
1509 pmif->broken_dma_warn = 1;
1510 }
1511 goto use_pio_instead;
1512 }
1513 while (cur_len) {
1514 unsigned int tc = (cur_len < 0xfe00)? cur_len: 0xfe00;
1515
1516 if (count++ >= MAX_DCMDS) {
1517 printk(KERN_WARNING "%s: DMA table too small\n",
1518 drive->name);
1519 goto use_pio_instead;
1520 }
1521 st_le16(&table->command, wr? OUTPUT_MORE: INPUT_MORE);
1522 st_le16(&table->req_count, tc);
1523 st_le32(&table->phy_addr, cur_addr);
1524 table->cmd_dep = 0;
1525 table->xfer_status = 0;
1526 table->res_count = 0;
1527 cur_addr += tc;
1528 cur_len -= tc;
1529 ++table;
1530 }
Jens Axboe55c16a72007-07-25 08:13:56 +02001531 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 i--;
1533 }
1534
1535 /* convert the last command to an input/output last command */
1536 if (count) {
1537 st_le16(&table[-1].command, wr? OUTPUT_LAST: INPUT_LAST);
1538 /* add the stop command to the end of the list */
1539 memset(table, 0, sizeof(struct dbdma_cmd));
1540 st_le16(&table->command, DBDMA_STOP);
1541 mb();
1542 writel(hwif->dmatable_dma, &dma->cmdptr);
1543 return 1;
1544 }
1545
1546 printk(KERN_DEBUG "%s: empty DMA table?\n", drive->name);
Bartlomiej Zolnierkiewiczf6fb7862008-02-01 23:09:31 +01001547
1548use_pio_instead:
1549 ide_destroy_dmatable(drive);
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return 0; /* revert to PIO for this request */
1552}
1553
1554/* Teardown mappings after DMA has completed. */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001555static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556pmac_ide_destroy_dmatable (ide_drive_t *drive)
1557{
1558 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Bartlomiej Zolnierkiewiczf6fb7862008-02-01 23:09:31 +01001560 if (hwif->sg_nents) {
1561 ide_destroy_dmatable(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 hwif->sg_nents = 0;
1563 }
1564}
1565
1566/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 * Prepare a DMA transfer. We build the DMA table, adjust the timings for
1568 * a read on KeyLargo ATA/66 and mark us as waiting for DMA completion
1569 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001570static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571pmac_ide_dma_setup(ide_drive_t *drive)
1572{
1573 ide_hwif_t *hwif = HWIF(drive);
1574 pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)hwif->hwif_data;
1575 struct request *rq = HWGROUP(drive)->rq;
1576 u8 unit = (drive->select.b.unit & 0x01);
1577 u8 ata4;
1578
1579 if (pmif == NULL)
1580 return 1;
1581 ata4 = (pmif->kind == controller_kl_ata4);
1582
1583 if (!pmac_ide_build_dmatable(drive, rq)) {
1584 ide_map_sg(drive, rq);
1585 return 1;
1586 }
1587
1588 /* Apple adds 60ns to wrDataSetup on reads */
1589 if (ata4 && (pmif->timings[unit] & TR_66_UDMA_EN)) {
1590 writel(pmif->timings[unit] + (!rq_data_dir(rq) ? 0x00800000UL : 0),
1591 PMAC_IDE_REG(IDE_TIMING_CONFIG));
1592 (void)readl(PMAC_IDE_REG(IDE_TIMING_CONFIG));
1593 }
1594
1595 drive->waiting_for_dma = 1;
1596
1597 return 0;
1598}
1599
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001600static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601pmac_ide_dma_exec_cmd(ide_drive_t *drive, u8 command)
1602{
1603 /* issue cmd to drive */
1604 ide_execute_command(drive, command, &ide_dma_intr, 2*WAIT_CMD, NULL);
1605}
1606
1607/*
1608 * Kick the DMA controller into life after the DMA command has been issued
1609 * to the drive.
1610 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001611static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612pmac_ide_dma_start(ide_drive_t *drive)
1613{
1614 pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
1615 volatile struct dbdma_regs __iomem *dma;
1616
1617 dma = pmif->dma_regs;
1618
1619 writel((RUN << 16) | RUN, &dma->control);
1620 /* Make sure it gets to the controller right now */
1621 (void)readl(&dma->control);
1622}
1623
1624/*
1625 * After a DMA transfer, make sure the controller is stopped
1626 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001627static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628pmac_ide_dma_end (ide_drive_t *drive)
1629{
1630 pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
1631 volatile struct dbdma_regs __iomem *dma;
1632 u32 dstat;
1633
1634 if (pmif == NULL)
1635 return 0;
1636 dma = pmif->dma_regs;
1637
1638 drive->waiting_for_dma = 0;
1639 dstat = readl(&dma->status);
1640 writel(((RUN|WAKE|DEAD) << 16), &dma->control);
1641 pmac_ide_destroy_dmatable(drive);
1642 /* verify good dma status. we don't check for ACTIVE beeing 0. We should...
1643 * in theory, but with ATAPI decices doing buffer underruns, that would
1644 * cause us to disable DMA, which isn't what we want
1645 */
1646 return (dstat & (RUN|DEAD)) != RUN;
1647}
1648
1649/*
1650 * Check out that the interrupt we got was for us. We can't always know this
1651 * for sure with those Apple interfaces (well, we could on the recent ones but
1652 * that's not implemented yet), on the other hand, we don't have shared interrupts
1653 * so it's not really a problem
1654 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001655static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656pmac_ide_dma_test_irq (ide_drive_t *drive)
1657{
1658 pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
1659 volatile struct dbdma_regs __iomem *dma;
1660 unsigned long status, timeout;
1661
1662 if (pmif == NULL)
1663 return 0;
1664 dma = pmif->dma_regs;
1665
1666 /* We have to things to deal with here:
1667 *
1668 * - The dbdma won't stop if the command was started
1669 * but completed with an error without transferring all
1670 * datas. This happens when bad blocks are met during
1671 * a multi-block transfer.
1672 *
1673 * - The dbdma fifo hasn't yet finished flushing to
1674 * to system memory when the disk interrupt occurs.
1675 *
1676 */
1677
1678 /* If ACTIVE is cleared, the STOP command have passed and
1679 * transfer is complete.
1680 */
1681 status = readl(&dma->status);
1682 if (!(status & ACTIVE))
1683 return 1;
1684 if (!drive->waiting_for_dma)
1685 printk(KERN_WARNING "ide%d, ide_dma_test_irq \
1686 called while not waiting\n", HWIF(drive)->index);
1687
1688 /* If dbdma didn't execute the STOP command yet, the
1689 * active bit is still set. We consider that we aren't
1690 * sharing interrupts (which is hopefully the case with
1691 * those controllers) and so we just try to flush the
1692 * channel for pending data in the fifo
1693 */
1694 udelay(1);
1695 writel((FLUSH << 16) | FLUSH, &dma->control);
1696 timeout = 0;
1697 for (;;) {
1698 udelay(1);
1699 status = readl(&dma->status);
1700 if ((status & FLUSH) == 0)
1701 break;
1702 if (++timeout > 100) {
1703 printk(KERN_WARNING "ide%d, ide_dma_test_irq \
1704 timeout flushing channel\n", HWIF(drive)->index);
1705 break;
1706 }
1707 }
1708 return 1;
1709}
1710
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +01001711static void pmac_ide_dma_host_set(ide_drive_t *drive, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
Sergei Shtylyov841d2a92007-07-09 23:17:54 +02001715static void
1716pmac_ide_dma_lost_irq (ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
1718 pmac_ide_hwif_t* pmif = (pmac_ide_hwif_t *)HWIF(drive)->hwif_data;
1719 volatile struct dbdma_regs __iomem *dma;
1720 unsigned long status;
1721
1722 if (pmif == NULL)
Sergei Shtylyov841d2a92007-07-09 23:17:54 +02001723 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 dma = pmif->dma_regs;
1725
1726 status = readl(&dma->status);
1727 printk(KERN_ERR "ide-pmac lost interrupt, dma status: %lx\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
1730/*
1731 * Allocate the data structures needed for using DMA with an interface
1732 * and fill the proper list of functions pointers
1733 */
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001734static int __devinit pmac_ide_setup_dma(pmac_ide_hwif_t *pmif, ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +01001736 struct pci_dev *dev = to_pci_dev(hwif->dev);
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 /* We won't need pci_dev if we switch to generic consistent
1739 * DMA routines ...
1740 */
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +01001741 if (dev == NULL)
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001742 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /*
1744 * Allocate space for the DBDMA commands.
1745 * The +2 is +1 for the stop command and +1 to allow for
1746 * aligning the start address to a multiple of 16 bytes.
1747 */
1748 pmif->dma_table_cpu = (struct dbdma_cmd*)pci_alloc_consistent(
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +01001749 dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 (MAX_DCMDS + 2) * sizeof(struct dbdma_cmd),
1751 &hwif->dmatable_dma);
1752 if (pmif->dma_table_cpu == NULL) {
1753 printk(KERN_ERR "%s: unable to allocate DMA command list\n",
1754 hwif->name);
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001755 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757
Bartlomiej Zolnierkiewicz4f52a322008-01-26 20:13:08 +01001758 hwif->sg_max_nents = MAX_DCMDS;
1759
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +01001760 hwif->dma_host_set = &pmac_ide_dma_host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 hwif->dma_setup = &pmac_ide_dma_setup;
1762 hwif->dma_exec_cmd = &pmac_ide_dma_exec_cmd;
1763 hwif->dma_start = &pmac_ide_dma_start;
1764 hwif->ide_dma_end = &pmac_ide_dma_end;
1765 hwif->ide_dma_test_irq = &pmac_ide_dma_test_irq;
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +02001766 hwif->dma_timeout = &ide_dma_timeout;
Sergei Shtylyov841d2a92007-07-09 23:17:54 +02001767 hwif->dma_lost_irq = &pmac_ide_dma_lost_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
1772#endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
Bartlomiej Zolnierkiewiczade2daf2008-01-26 20:13:07 +01001773
1774module_init(pmac_ide_probe);
Adrian Bunkde9facb2008-04-02 21:22:03 +02001775
1776MODULE_LICENSE("GPL");