Merge tag 'rpmsg-v5.3' of git://github.com/andersson/remoteproc
[linux-2.6-block.git] / drivers / ide / pmac.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/*
1da177e4 3 * Support for IDE interfaces on PowerMacs.
58f189fc 4 *
1da177e4
LT
5 * These IDE interfaces are memory-mapped and have a DBDMA channel
6 * for doing DMA.
7 *
8 * Copyright (C) 1998-2003 Paul Mackerras & Ben. Herrenschmidt
8a97206e 9 * Copyright (C) 2007-2008 Bartlomiej Zolnierkiewicz
1da177e4 10 *
1da177e4
LT
11 * Some code taken from drivers/ide/ide-dma.c:
12 *
13 * Copyright (c) 1995-1998 Mark Lord
14 *
15 * TODO: - Use pre-calculated (kauai) timing tables all the time and
16 * get rid of the "rounded" tables used previously, so we have the
17 * same table format for all controllers and can then just have one
18 * big table
1da177e4 19 */
1da177e4
LT
20#include <linux/types.h>
21#include <linux/kernel.h>
1da177e4
LT
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/ide.h>
25#include <linux/notifier.h>
bff7832d 26#include <linux/module.h>
1da177e4
LT
27#include <linux/reboot.h>
28#include <linux/pci.h>
29#include <linux/adb.h>
30#include <linux/pmu.h>
31#include <linux/scatterlist.h>
5a0e3ad6 32#include <linux/slab.h>
1da177e4
LT
33
34#include <asm/prom.h>
35#include <asm/io.h>
36#include <asm/dbdma.h>
37#include <asm/ide.h>
1da177e4
LT
38#include <asm/machdep.h>
39#include <asm/pmac_feature.h>
40#include <asm/sections.h>
41#include <asm/irq.h>
1da177e4 42#include <asm/mediabay.h>
1da177e4 43
b36ba532
BZ
44#define DRV_NAME "ide-pmac"
45
1da177e4
LT
46#undef IDE_PMAC_DEBUG
47
48#define DMA_WAIT_TIMEOUT 50
49
50typedef struct pmac_ide_hwif {
51 unsigned long regbase;
52 int irq;
53 int kind;
54 int aapl_bus_id;
1da177e4
LT
55 unsigned broken_dma : 1;
56 unsigned broken_dma_warn : 1;
57 struct device_node* node;
58 struct macio_dev *mdev;
59 u32 timings[4];
60 volatile u32 __iomem * *kauai_fcr;
d58b0c39
BH
61 ide_hwif_t *hwif;
62
1da177e4
LT
63 /* Those fields are duplicating what is in hwif. We currently
64 * can't use the hwif ones because of some assumptions that are
65 * beeing done by the generic code about the kind of dma controller
66 * and format of the dma table. This will have to be fixed though.
67 */
68 volatile struct dbdma_regs __iomem * dma_regs;
69 struct dbdma_cmd* dma_table_cpu;
1da177e4
LT
70} pmac_ide_hwif_t;
71
1da177e4
LT
72enum {
73 controller_ohare, /* OHare based */
74 controller_heathrow, /* Heathrow/Paddington */
75 controller_kl_ata3, /* KeyLargo ATA-3 */
76 controller_kl_ata4, /* KeyLargo ATA-4 */
77 controller_un_ata6, /* UniNorth2 ATA-6 */
78 controller_k2_ata6, /* K2 ATA-6 */
79 controller_sh_ata6, /* Shasta ATA-6 */
80};
81
82static const char* model_name[] = {
83 "OHare ATA", /* OHare based */
84 "Heathrow ATA", /* Heathrow/Paddington */
85 "KeyLargo ATA-3", /* KeyLargo ATA-3 (MDMA only) */
86 "KeyLargo ATA-4", /* KeyLargo ATA-4 (UDMA/66) */
87 "UniNorth ATA-6", /* UniNorth2 ATA-6 (UDMA/100) */
88 "K2 ATA-6", /* K2 ATA-6 (UDMA/100) */
89 "Shasta ATA-6", /* Shasta ATA-6 (UDMA/133) */
90};
91
92/*
93 * Extra registers, both 32-bit little-endian
94 */
95#define IDE_TIMING_CONFIG 0x200
96#define IDE_INTERRUPT 0x300
97
98/* Kauai (U2) ATA has different register setup */
99#define IDE_KAUAI_PIO_CONFIG 0x200
100#define IDE_KAUAI_ULTRA_CONFIG 0x210
101#define IDE_KAUAI_POLL_CONFIG 0x220
102
103/*
104 * Timing configuration register definitions
105 */
106
107/* Number of IDE_SYSCLK_NS ticks, argument is in nanoseconds */
108#define SYSCLK_TICKS(t) (((t) + IDE_SYSCLK_NS - 1) / IDE_SYSCLK_NS)
109#define SYSCLK_TICKS_66(t) (((t) + IDE_SYSCLK_66_NS - 1) / IDE_SYSCLK_66_NS)
110#define IDE_SYSCLK_NS 30 /* 33Mhz cell */
111#define IDE_SYSCLK_66_NS 15 /* 66Mhz cell */
112
113/* 133Mhz cell, found in shasta.
114 * See comments about 100 Mhz Uninorth 2...
115 * Note that PIO_MASK and MDMA_MASK seem to overlap
116 */
117#define TR_133_PIOREG_PIO_MASK 0xff000fff
118#define TR_133_PIOREG_MDMA_MASK 0x00fff800
119#define TR_133_UDMAREG_UDMA_MASK 0x0003ffff
120#define TR_133_UDMAREG_UDMA_EN 0x00000001
121
122/* 100Mhz cell, found in Uninorth 2. I don't have much infos about
123 * this one yet, it appears as a pci device (106b/0033) on uninorth
124 * internal PCI bus and it's clock is controlled like gem or fw. It
125 * appears to be an evolution of keylargo ATA4 with a timing register
126 * extended to 2 32bits registers and a similar DBDMA channel. Other
127 * registers seem to exist but I can't tell much about them.
128 *
129 * So far, I'm using pre-calculated tables for this extracted from
130 * the values used by the MacOS X driver.
131 *
132 * The "PIO" register controls PIO and MDMA timings, the "ULTRA"
133 * register controls the UDMA timings. At least, it seems bit 0
134 * of this one enables UDMA vs. MDMA, and bits 4..7 are the
135 * cycle time in units of 10ns. Bits 8..15 are used by I don't
136 * know their meaning yet
137 */
138#define TR_100_PIOREG_PIO_MASK 0xff000fff
139#define TR_100_PIOREG_MDMA_MASK 0x00fff000
140#define TR_100_UDMAREG_UDMA_MASK 0x0000ffff
141#define TR_100_UDMAREG_UDMA_EN 0x00000001
142
143
144/* 66Mhz cell, found in KeyLargo. Can do ultra mode 0 to 2 on
145 * 40 connector cable and to 4 on 80 connector one.
146 * Clock unit is 15ns (66Mhz)
147 *
148 * 3 Values can be programmed:
149 * - Write data setup, which appears to match the cycle time. They
150 * also call it DIOW setup.
151 * - Ready to pause time (from spec)
152 * - Address setup. That one is weird. I don't see where exactly
153 * it fits in UDMA cycles, I got it's name from an obscure piece
154 * of commented out code in Darwin. They leave it to 0, we do as
155 * well, despite a comment that would lead to think it has a
156 * min value of 45ns.
157 * Apple also add 60ns to the write data setup (or cycle time ?) on
158 * reads.
159 */
160#define TR_66_UDMA_MASK 0xfff00000
161#define TR_66_UDMA_EN 0x00100000 /* Enable Ultra mode for DMA */
162#define TR_66_UDMA_ADDRSETUP_MASK 0xe0000000 /* Address setup */
163#define TR_66_UDMA_ADDRSETUP_SHIFT 29
164#define TR_66_UDMA_RDY2PAUS_MASK 0x1e000000 /* Ready 2 pause time */
165#define TR_66_UDMA_RDY2PAUS_SHIFT 25
166#define TR_66_UDMA_WRDATASETUP_MASK 0x01e00000 /* Write data setup time */
167#define TR_66_UDMA_WRDATASETUP_SHIFT 21
168#define TR_66_MDMA_MASK 0x000ffc00
169#define TR_66_MDMA_RECOVERY_MASK 0x000f8000
170#define TR_66_MDMA_RECOVERY_SHIFT 15
171#define TR_66_MDMA_ACCESS_MASK 0x00007c00
172#define TR_66_MDMA_ACCESS_SHIFT 10
173#define TR_66_PIO_MASK 0x000003ff
174#define TR_66_PIO_RECOVERY_MASK 0x000003e0
175#define TR_66_PIO_RECOVERY_SHIFT 5
176#define TR_66_PIO_ACCESS_MASK 0x0000001f
177#define TR_66_PIO_ACCESS_SHIFT 0
178
179/* 33Mhz cell, found in OHare, Heathrow (& Paddington) and KeyLargo
180 * Can do pio & mdma modes, clock unit is 30ns (33Mhz)
181 *
182 * The access time and recovery time can be programmed. Some older
183 * Darwin code base limit OHare to 150ns cycle time. I decided to do
184 * the same here fore safety against broken old hardware ;)
185 * The HalfTick bit, when set, adds half a clock (15ns) to the access
186 * time and removes one from recovery. It's not supported on KeyLargo
187 * implementation afaik. The E bit appears to be set for PIO mode 0 and
188 * is used to reach long timings used in this mode.
189 */
190#define TR_33_MDMA_MASK 0x003ff800
191#define TR_33_MDMA_RECOVERY_MASK 0x001f0000
192#define TR_33_MDMA_RECOVERY_SHIFT 16
193#define TR_33_MDMA_ACCESS_MASK 0x0000f800
194#define TR_33_MDMA_ACCESS_SHIFT 11
195#define TR_33_MDMA_HALFTICK 0x00200000
196#define TR_33_PIO_MASK 0x000007ff
197#define TR_33_PIO_E 0x00000400
198#define TR_33_PIO_RECOVERY_MASK 0x000003e0
199#define TR_33_PIO_RECOVERY_SHIFT 5
200#define TR_33_PIO_ACCESS_MASK 0x0000001f
201#define TR_33_PIO_ACCESS_SHIFT 0
202
203/*
204 * Interrupt register definitions
205 */
206#define IDE_INTR_DMA 0x80000000
207#define IDE_INTR_DEVICE 0x40000000
208
209/*
210 * FCR Register on Kauai. Not sure what bit 0x4 is ...
211 */
212#define KAUAI_FCR_UATA_MAGIC 0x00000004
213#define KAUAI_FCR_UATA_RESET_N 0x00000002
214#define KAUAI_FCR_UATA_ENABLE 0x00000001
215
1da177e4
LT
216/* Rounded Multiword DMA timings
217 *
218 * I gave up finding a generic formula for all controller
219 * types and instead, built tables based on timing values
220 * used by Apple in Darwin's implementation.
221 */
222struct mdma_timings_t {
223 int accessTime;
224 int recoveryTime;
225 int cycleTime;
226};
227
aacaf9bd 228struct mdma_timings_t mdma_timings_33[] =
1da177e4
LT
229{
230 { 240, 240, 480 },
231 { 180, 180, 360 },
232 { 135, 135, 270 },
233 { 120, 120, 240 },
234 { 105, 105, 210 },
235 { 90, 90, 180 },
236 { 75, 75, 150 },
237 { 75, 45, 120 },
238 { 0, 0, 0 }
239};
240
aacaf9bd 241struct mdma_timings_t mdma_timings_33k[] =
1da177e4
LT
242{
243 { 240, 240, 480 },
244 { 180, 180, 360 },
245 { 150, 150, 300 },
246 { 120, 120, 240 },
247 { 90, 120, 210 },
248 { 90, 90, 180 },
249 { 90, 60, 150 },
250 { 90, 30, 120 },
251 { 0, 0, 0 }
252};
253
aacaf9bd 254struct mdma_timings_t mdma_timings_66[] =
1da177e4
LT
255{
256 { 240, 240, 480 },
257 { 180, 180, 360 },
258 { 135, 135, 270 },
259 { 120, 120, 240 },
260 { 105, 105, 210 },
261 { 90, 90, 180 },
262 { 90, 75, 165 },
263 { 75, 45, 120 },
264 { 0, 0, 0 }
265};
266
267/* KeyLargo ATA-4 Ultra DMA timings (rounded) */
268struct {
269 int addrSetup; /* ??? */
270 int rdy2pause;
271 int wrDataSetup;
aacaf9bd 272} kl66_udma_timings[] =
1da177e4
LT
273{
274 { 0, 180, 120 }, /* Mode 0 */
275 { 0, 150, 90 }, /* 1 */
276 { 0, 120, 60 }, /* 2 */
277 { 0, 90, 45 }, /* 3 */
278 { 0, 90, 30 } /* 4 */
279};
280
281/* UniNorth 2 ATA/100 timings */
282struct kauai_timing {
283 int cycle_time;
284 u32 timing_reg;
285};
286
aacaf9bd 287static struct kauai_timing kauai_pio_timings[] =
1da177e4
LT
288{
289 { 930 , 0x08000fff },
290 { 600 , 0x08000a92 },
291 { 383 , 0x0800060f },
292 { 360 , 0x08000492 },
293 { 330 , 0x0800048f },
294 { 300 , 0x080003cf },
295 { 270 , 0x080003cc },
296 { 240 , 0x0800038b },
297 { 239 , 0x0800030c },
298 { 180 , 0x05000249 },
c15d5d43
BZ
299 { 120 , 0x04000148 },
300 { 0 , 0 },
1da177e4
LT
301};
302
aacaf9bd 303static struct kauai_timing kauai_mdma_timings[] =
1da177e4
LT
304{
305 { 1260 , 0x00fff000 },
306 { 480 , 0x00618000 },
307 { 360 , 0x00492000 },
308 { 270 , 0x0038e000 },
309 { 240 , 0x0030c000 },
310 { 210 , 0x002cb000 },
311 { 180 , 0x00249000 },
312 { 150 , 0x00209000 },
313 { 120 , 0x00148000 },
314 { 0 , 0 },
315};
316
aacaf9bd 317static struct kauai_timing kauai_udma_timings[] =
1da177e4
LT
318{
319 { 120 , 0x000070c0 },
320 { 90 , 0x00005d80 },
321 { 60 , 0x00004a60 },
322 { 45 , 0x00003a50 },
323 { 30 , 0x00002a30 },
324 { 20 , 0x00002921 },
325 { 0 , 0 },
326};
327
aacaf9bd 328static struct kauai_timing shasta_pio_timings[] =
1da177e4
LT
329{
330 { 930 , 0x08000fff },
331 { 600 , 0x0A000c97 },
332 { 383 , 0x07000712 },
333 { 360 , 0x040003cd },
334 { 330 , 0x040003cd },
335 { 300 , 0x040003cd },
336 { 270 , 0x040003cd },
337 { 240 , 0x040003cd },
338 { 239 , 0x040003cd },
339 { 180 , 0x0400028b },
c15d5d43
BZ
340 { 120 , 0x0400010a },
341 { 0 , 0 },
1da177e4
LT
342};
343
aacaf9bd 344static struct kauai_timing shasta_mdma_timings[] =
1da177e4
LT
345{
346 { 1260 , 0x00fff000 },
347 { 480 , 0x00820800 },
348 { 360 , 0x00820800 },
349 { 270 , 0x00820800 },
350 { 240 , 0x00820800 },
351 { 210 , 0x00820800 },
352 { 180 , 0x00820800 },
353 { 150 , 0x0028b000 },
354 { 120 , 0x001ca000 },
355 { 0 , 0 },
356};
357
aacaf9bd 358static struct kauai_timing shasta_udma133_timings[] =
1da177e4
LT
359{
360 { 120 , 0x00035901, },
361 { 90 , 0x000348b1, },
362 { 60 , 0x00033881, },
363 { 45 , 0x00033861, },
364 { 30 , 0x00033841, },
365 { 20 , 0x00033031, },
366 { 15 , 0x00033021, },
367 { 0 , 0 },
368};
369
370
371static inline u32
372kauai_lookup_timing(struct kauai_timing* table, int cycle_time)
373{
374 int i;
375
376 for (i=0; table[i].cycle_time; i++)
377 if (cycle_time > table[i+1].cycle_time)
378 return table[i].timing_reg;
90a87ea4 379 BUG();
1da177e4
LT
380 return 0;
381}
382
383/* allow up to 256 DBDMA commands per xfer */
384#define MAX_DCMDS 256
385
386/*
387 * Wait 1s for disk to answer on IDE bus after a hard reset
388 * of the device (via GPIO/FCR).
389 *
390 * Some devices seem to "pollute" the bus even after dropping
391 * the BSY bit (typically some combo drives slave on the UDMA
392 * bus) after a hard reset. Since we hard reset all drives on
393 * KeyLargo ATA66, we have to keep that delay around. I may end
394 * up not hard resetting anymore on these and keep the delay only
395 * for older interfaces instead (we have to reset when coming
396 * from MacOS...) --BenH.
397 */
398#define IDE_WAKEUP_DELAY (1*HZ)
399
0d071922 400static int pmac_ide_init_dma(ide_hwif_t *, const struct ide_port_info *);
1da177e4 401
23579a2a 402#define PMAC_IDE_REG(x) \
4c3032d8 403 ((void __iomem *)((drive)->hwif->io_ports.data_addr + (x)))
1da177e4
LT
404
405/*
406 * Apply the timings of the proper unit (master/slave) to the shared
407 * timing register when selecting that unit. This version is for
408 * ASICs with a single timing register
409 */
abb596b2 410static void pmac_ide_apply_timings(ide_drive_t *drive)
1da177e4 411{
7b8797ac 412 ide_hwif_t *hwif = drive->hwif;
58e48be7 413 pmac_ide_hwif_t *pmif = dev_get_drvdata(hwif->gendev.parent);
1da177e4 414
123995b9 415 if (drive->dn & 1)
1da177e4
LT
416 writel(pmif->timings[1], PMAC_IDE_REG(IDE_TIMING_CONFIG));
417 else
418 writel(pmif->timings[0], PMAC_IDE_REG(IDE_TIMING_CONFIG));
419 (void)readl(PMAC_IDE_REG(IDE_TIMING_CONFIG));
420}
421
422/*
423 * Apply the timings of the proper unit (master/slave) to the shared
424 * timing register when selecting that unit. This version is for
425 * ASICs with a dual timing register (Kauai)
426 */
abb596b2 427static void pmac_ide_kauai_apply_timings(ide_drive_t *drive)
1da177e4 428{
7b8797ac 429 ide_hwif_t *hwif = drive->hwif;
58e48be7 430 pmac_ide_hwif_t *pmif = dev_get_drvdata(hwif->gendev.parent);
1da177e4 431
123995b9 432 if (drive->dn & 1) {
1da177e4
LT
433 writel(pmif->timings[1], PMAC_IDE_REG(IDE_KAUAI_PIO_CONFIG));
434 writel(pmif->timings[3], PMAC_IDE_REG(IDE_KAUAI_ULTRA_CONFIG));
435 } else {
436 writel(pmif->timings[0], PMAC_IDE_REG(IDE_KAUAI_PIO_CONFIG));
437 writel(pmif->timings[2], PMAC_IDE_REG(IDE_KAUAI_ULTRA_CONFIG));
438 }
439 (void)readl(PMAC_IDE_REG(IDE_KAUAI_PIO_CONFIG));
440}
441
442/*
443 * Force an update of controller timing values for a given drive
444 */
aacaf9bd 445static void
1da177e4
LT
446pmac_ide_do_update_timings(ide_drive_t *drive)
447{
7b8797ac 448 ide_hwif_t *hwif = drive->hwif;
58e48be7 449 pmac_ide_hwif_t *pmif = dev_get_drvdata(hwif->gendev.parent);
1da177e4 450
1da177e4
LT
451 if (pmif->kind == controller_sh_ata6 ||
452 pmif->kind == controller_un_ata6 ||
453 pmif->kind == controller_k2_ata6)
abb596b2 454 pmac_ide_kauai_apply_timings(drive);
1da177e4 455 else
abb596b2
SS
456 pmac_ide_apply_timings(drive);
457}
458
459static void pmac_dev_select(ide_drive_t *drive)
460{
461 pmac_ide_apply_timings(drive);
462
463 writeb(drive->select | ATA_DEVICE_OBS,
464 (void __iomem *)drive->hwif->io_ports.device_addr);
465}
466
467static void pmac_kauai_dev_select(ide_drive_t *drive)
468{
469 pmac_ide_kauai_apply_timings(drive);
470
471 writeb(drive->select | ATA_DEVICE_OBS,
472 (void __iomem *)drive->hwif->io_ports.device_addr);
1da177e4
LT
473}
474
c6dfa867
BZ
475static void pmac_exec_command(ide_hwif_t *hwif, u8 cmd)
476{
477 writeb(cmd, (void __iomem *)hwif->io_ports.command_addr);
478 (void)readl((void __iomem *)(hwif->io_ports.data_addr
479 + IDE_TIMING_CONFIG));
480}
481
ecf3a31d 482static void pmac_write_devctl(ide_hwif_t *hwif, u8 ctl)
6e6afb3b 483{
6e6afb3b
BZ
484 writeb(ctl, (void __iomem *)hwif->io_ports.ctl_addr);
485 (void)readl((void __iomem *)(hwif->io_ports.data_addr
486 + IDE_TIMING_CONFIG));
487}
488
1da177e4
LT
489/*
490 * Old tuning functions (called on hdparm -p), sets up drive PIO timings
491 */
e085b3ca 492static void pmac_ide_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
1da177e4 493{
58e48be7 494 pmac_ide_hwif_t *pmif = dev_get_drvdata(hwif->gendev.parent);
e085b3ca 495 const u8 pio = drive->pio_mode - XFER_PIO_0;
8a97206e 496 struct ide_timing *tim = ide_timing_find_mode(XFER_PIO_0 + pio);
0b46ff2e 497 u32 *timings, t;
1da177e4
LT
498 unsigned accessTicks, recTicks;
499 unsigned accessTime, recTime;
7dd00083
BZ
500 unsigned int cycle_time;
501
1da177e4 502 /* which drive is it ? */
123995b9 503 timings = &pmif->timings[drive->dn & 1];
0b46ff2e 504 t = *timings;
1da177e4 505
7dd00083 506 cycle_time = ide_pio_cycle_time(drive, pio);
1da177e4
LT
507
508 switch (pmif->kind) {
509 case controller_sh_ata6: {
510 /* 133Mhz cell */
7dd00083 511 u32 tr = kauai_lookup_timing(shasta_pio_timings, cycle_time);
0b46ff2e 512 t = (t & ~TR_133_PIOREG_PIO_MASK) | tr;
1da177e4
LT
513 break;
514 }
515 case controller_un_ata6:
516 case controller_k2_ata6: {
517 /* 100Mhz cell */
7dd00083 518 u32 tr = kauai_lookup_timing(kauai_pio_timings, cycle_time);
0b46ff2e 519 t = (t & ~TR_100_PIOREG_PIO_MASK) | tr;
1da177e4
LT
520 break;
521 }
522 case controller_kl_ata4:
523 /* 66Mhz cell */
8a97206e 524 recTime = cycle_time - tim->active - tim->setup;
1da177e4 525 recTime = max(recTime, 150U);
8a97206e 526 accessTime = tim->active;
1da177e4
LT
527 accessTime = max(accessTime, 150U);
528 accessTicks = SYSCLK_TICKS_66(accessTime);
529 accessTicks = min(accessTicks, 0x1fU);
530 recTicks = SYSCLK_TICKS_66(recTime);
531 recTicks = min(recTicks, 0x1fU);
0b46ff2e
BH
532 t = (t & ~TR_66_PIO_MASK) |
533 (accessTicks << TR_66_PIO_ACCESS_SHIFT) |
534 (recTicks << TR_66_PIO_RECOVERY_SHIFT);
1da177e4
LT
535 break;
536 default: {
537 /* 33Mhz cell */
538 int ebit = 0;
8a97206e 539 recTime = cycle_time - tim->active - tim->setup;
1da177e4 540 recTime = max(recTime, 150U);
8a97206e 541 accessTime = tim->active;
1da177e4
LT
542 accessTime = max(accessTime, 150U);
543 accessTicks = SYSCLK_TICKS(accessTime);
544 accessTicks = min(accessTicks, 0x1fU);
545 accessTicks = max(accessTicks, 4U);
546 recTicks = SYSCLK_TICKS(recTime);
547 recTicks = min(recTicks, 0x1fU);
548 recTicks = max(recTicks, 5U) - 4;
549 if (recTicks > 9) {
550 recTicks--; /* guess, but it's only for PIO0, so... */
551 ebit = 1;
552 }
0b46ff2e 553 t = (t & ~TR_33_PIO_MASK) |
1da177e4
LT
554 (accessTicks << TR_33_PIO_ACCESS_SHIFT) |
555 (recTicks << TR_33_PIO_RECOVERY_SHIFT);
556 if (ebit)
0b46ff2e 557 t |= TR_33_PIO_E;
1da177e4
LT
558 break;
559 }
560 }
561
562#ifdef IDE_PMAC_DEBUG
563 printk(KERN_ERR "%s: Set PIO timing for mode %d, reg: 0x%08x\n",
564 drive->name, pio, *timings);
565#endif
566
0b46ff2e 567 *timings = t;
c15d5d43 568 pmac_ide_do_update_timings(drive);
1da177e4
LT
569}
570
1da177e4
LT
571/*
572 * Calculate KeyLargo ATA/66 UDMA timings
573 */
aacaf9bd 574static int
1da177e4
LT
575set_timings_udma_ata4(u32 *timings, u8 speed)
576{
577 unsigned rdyToPauseTicks, wrDataSetupTicks, addrTicks;
578
579 if (speed > XFER_UDMA_4)
580 return 1;
581
582 rdyToPauseTicks = SYSCLK_TICKS_66(kl66_udma_timings[speed & 0xf].rdy2pause);
583 wrDataSetupTicks = SYSCLK_TICKS_66(kl66_udma_timings[speed & 0xf].wrDataSetup);
584 addrTicks = SYSCLK_TICKS_66(kl66_udma_timings[speed & 0xf].addrSetup);
585
586 *timings = ((*timings) & ~(TR_66_UDMA_MASK | TR_66_MDMA_MASK)) |
587 (wrDataSetupTicks << TR_66_UDMA_WRDATASETUP_SHIFT) |
588 (rdyToPauseTicks << TR_66_UDMA_RDY2PAUS_SHIFT) |
589 (addrTicks <<TR_66_UDMA_ADDRSETUP_SHIFT) |
590 TR_66_UDMA_EN;
591#ifdef IDE_PMAC_DEBUG
592 printk(KERN_ERR "ide_pmac: Set UDMA timing for mode %d, reg: 0x%08x\n",
593 speed & 0xf, *timings);
594#endif
595
596 return 0;
597}
598
599/*
600 * Calculate Kauai ATA/100 UDMA timings
601 */
aacaf9bd 602static int
1da177e4
LT
603set_timings_udma_ata6(u32 *pio_timings, u32 *ultra_timings, u8 speed)
604{
605 struct ide_timing *t = ide_timing_find_mode(speed);
606 u32 tr;
607
608 if (speed > XFER_UDMA_5 || t == NULL)
609 return 1;
610 tr = kauai_lookup_timing(kauai_udma_timings, (int)t->udma);
1da177e4
LT
611 *ultra_timings = ((*ultra_timings) & ~TR_100_UDMAREG_UDMA_MASK) | tr;
612 *ultra_timings = (*ultra_timings) | TR_100_UDMAREG_UDMA_EN;
613
614 return 0;
615}
616
617/*
618 * Calculate Shasta ATA/133 UDMA timings
619 */
aacaf9bd 620static int
1da177e4
LT
621set_timings_udma_shasta(u32 *pio_timings, u32 *ultra_timings, u8 speed)
622{
623 struct ide_timing *t = ide_timing_find_mode(speed);
624 u32 tr;
625
626 if (speed > XFER_UDMA_6 || t == NULL)
627 return 1;
628 tr = kauai_lookup_timing(shasta_udma133_timings, (int)t->udma);
1da177e4
LT
629 *ultra_timings = ((*ultra_timings) & ~TR_133_UDMAREG_UDMA_MASK) | tr;
630 *ultra_timings = (*ultra_timings) | TR_133_UDMAREG_UDMA_EN;
631
632 return 0;
633}
634
635/*
636 * Calculate MDMA timings for all cells
637 */
90f72eca 638static void
1da177e4 639set_timings_mdma(ide_drive_t *drive, int intf_type, u32 *timings, u32 *timings2,
90f72eca 640 u8 speed)
1da177e4 641{
4dde4492 642 u16 *id = drive->id;
1da177e4
LT
643 int cycleTime, accessTime = 0, recTime = 0;
644 unsigned accessTicks, recTicks;
645 struct mdma_timings_t* tm = NULL;
646 int i;
647
648 /* Get default cycle time for mode */
649 switch(speed & 0xf) {
650 case 0: cycleTime = 480; break;
651 case 1: cycleTime = 150; break;
652 case 2: cycleTime = 120; break;
653 default:
90f72eca
BZ
654 BUG();
655 break;
1da177e4 656 }
90f72eca
BZ
657
658 /* Check if drive provides explicit DMA cycle time */
4dde4492
BZ
659 if ((id[ATA_ID_FIELD_VALID] & 2) && id[ATA_ID_EIDE_DMA_TIME])
660 cycleTime = max_t(int, id[ATA_ID_EIDE_DMA_TIME], cycleTime);
90f72eca 661
1da177e4
LT
662 /* OHare limits according to some old Apple sources */
663 if ((intf_type == controller_ohare) && (cycleTime < 150))
664 cycleTime = 150;
665 /* Get the proper timing array for this controller */
666 switch(intf_type) {
667 case controller_sh_ata6:
668 case controller_un_ata6:
669 case controller_k2_ata6:
670 break;
671 case controller_kl_ata4:
672 tm = mdma_timings_66;
673 break;
674 case controller_kl_ata3:
675 tm = mdma_timings_33k;
676 break;
677 default:
678 tm = mdma_timings_33;
679 break;
680 }
681 if (tm != NULL) {
682 /* Lookup matching access & recovery times */
683 i = -1;
684 for (;;) {
685 if (tm[i+1].cycleTime < cycleTime)
686 break;
687 i++;
688 }
1da177e4
LT
689 cycleTime = tm[i].cycleTime;
690 accessTime = tm[i].accessTime;
691 recTime = tm[i].recoveryTime;
692
693#ifdef IDE_PMAC_DEBUG
694 printk(KERN_ERR "%s: MDMA, cycleTime: %d, accessTime: %d, recTime: %d\n",
695 drive->name, cycleTime, accessTime, recTime);
696#endif
697 }
698 switch(intf_type) {
699 case controller_sh_ata6: {
700 /* 133Mhz cell */
701 u32 tr = kauai_lookup_timing(shasta_mdma_timings, cycleTime);
1da177e4
LT
702 *timings = ((*timings) & ~TR_133_PIOREG_MDMA_MASK) | tr;
703 *timings2 = (*timings2) & ~TR_133_UDMAREG_UDMA_EN;
704 }
d4f8c2e0 705 break;
1da177e4
LT
706 case controller_un_ata6:
707 case controller_k2_ata6: {
708 /* 100Mhz cell */
709 u32 tr = kauai_lookup_timing(kauai_mdma_timings, cycleTime);
1da177e4
LT
710 *timings = ((*timings) & ~TR_100_PIOREG_MDMA_MASK) | tr;
711 *timings2 = (*timings2) & ~TR_100_UDMAREG_UDMA_EN;
712 }
713 break;
714 case controller_kl_ata4:
715 /* 66Mhz cell */
716 accessTicks = SYSCLK_TICKS_66(accessTime);
717 accessTicks = min(accessTicks, 0x1fU);
718 accessTicks = max(accessTicks, 0x1U);
719 recTicks = SYSCLK_TICKS_66(recTime);
720 recTicks = min(recTicks, 0x1fU);
721 recTicks = max(recTicks, 0x3U);
722 /* Clear out mdma bits and disable udma */
723 *timings = ((*timings) & ~(TR_66_MDMA_MASK | TR_66_UDMA_MASK)) |
724 (accessTicks << TR_66_MDMA_ACCESS_SHIFT) |
725 (recTicks << TR_66_MDMA_RECOVERY_SHIFT);
726 break;
727 case controller_kl_ata3:
728 /* 33Mhz cell on KeyLargo */
729 accessTicks = SYSCLK_TICKS(accessTime);
730 accessTicks = max(accessTicks, 1U);
731 accessTicks = min(accessTicks, 0x1fU);
732 accessTime = accessTicks * IDE_SYSCLK_NS;
733 recTicks = SYSCLK_TICKS(recTime);
734 recTicks = max(recTicks, 1U);
735 recTicks = min(recTicks, 0x1fU);
736 *timings = ((*timings) & ~TR_33_MDMA_MASK) |
737 (accessTicks << TR_33_MDMA_ACCESS_SHIFT) |
738 (recTicks << TR_33_MDMA_RECOVERY_SHIFT);
739 break;
740 default: {
741 /* 33Mhz cell on others */
742 int halfTick = 0;
743 int origAccessTime = accessTime;
744 int origRecTime = recTime;
745
746 accessTicks = SYSCLK_TICKS(accessTime);
747 accessTicks = max(accessTicks, 1U);
748 accessTicks = min(accessTicks, 0x1fU);
749 accessTime = accessTicks * IDE_SYSCLK_NS;
750 recTicks = SYSCLK_TICKS(recTime);
751 recTicks = max(recTicks, 2U) - 1;
752 recTicks = min(recTicks, 0x1fU);
753 recTime = (recTicks + 1) * IDE_SYSCLK_NS;
754 if ((accessTicks > 1) &&
755 ((accessTime - IDE_SYSCLK_NS/2) >= origAccessTime) &&
756 ((recTime - IDE_SYSCLK_NS/2) >= origRecTime)) {
757 halfTick = 1;
758 accessTicks--;
759 }
760 *timings = ((*timings) & ~TR_33_MDMA_MASK) |
761 (accessTicks << TR_33_MDMA_ACCESS_SHIFT) |
762 (recTicks << TR_33_MDMA_RECOVERY_SHIFT);
763 if (halfTick)
764 *timings |= TR_33_MDMA_HALFTICK;
765 }
766 }
767#ifdef IDE_PMAC_DEBUG
768 printk(KERN_ERR "%s: Set MDMA timing for mode %d, reg: 0x%08x\n",
769 drive->name, speed & 0xf, *timings);
770#endif
1da177e4 771}
1da177e4 772
8776168c 773static void pmac_ide_set_dma_mode(ide_hwif_t *hwif, ide_drive_t *drive)
1da177e4 774{
58e48be7 775 pmac_ide_hwif_t *pmif = dev_get_drvdata(hwif->gendev.parent);
1da177e4 776 int ret = 0;
085798b1 777 u32 *timings, *timings2, tl[2];
123995b9 778 u8 unit = drive->dn & 1;
8776168c 779 const u8 speed = drive->dma_mode;
1da177e4 780
1da177e4
LT
781 timings = &pmif->timings[unit];
782 timings2 = &pmif->timings[unit+2];
085798b1
BZ
783
784 /* Copy timings to local image */
785 tl[0] = *timings;
786 tl[1] = *timings2;
787
4db90a14
BZ
788 if (speed >= XFER_UDMA_0) {
789 if (pmif->kind == controller_kl_ata4)
790 ret = set_timings_udma_ata4(&tl[0], speed);
791 else if (pmif->kind == controller_un_ata6
792 || pmif->kind == controller_k2_ata6)
793 ret = set_timings_udma_ata6(&tl[0], &tl[1], speed);
794 else if (pmif->kind == controller_sh_ata6)
795 ret = set_timings_udma_shasta(&tl[0], &tl[1], speed);
796 else
797 ret = -1;
798 } else
799 set_timings_mdma(drive, pmif->kind, &tl[0], &tl[1], speed);
53846574 800
1da177e4 801 if (ret)
88b2b32b 802 return;
085798b1
BZ
803
804 /* Apply timings to controller */
805 *timings = tl[0];
806 *timings2 = tl[1];
807
1da177e4 808 pmac_ide_do_update_timings(drive);
1da177e4
LT
809}
810
811/*
812 * Blast some well known "safe" values to the timing registers at init or
813 * wakeup from sleep time, before we do real calculation
814 */
aacaf9bd 815static void
1da177e4
LT
816sanitize_timings(pmac_ide_hwif_t *pmif)
817{
818 unsigned int value, value2 = 0;
819
820 switch(pmif->kind) {
821 case controller_sh_ata6:
822 value = 0x0a820c97;
823 value2 = 0x00033031;
824 break;
825 case controller_un_ata6:
826 case controller_k2_ata6:
827 value = 0x08618a92;
828 value2 = 0x00002921;
829 break;
830 case controller_kl_ata4:
831 value = 0x0008438c;
832 break;
833 case controller_kl_ata3:
834 value = 0x00084526;
835 break;
836 case controller_heathrow:
837 case controller_ohare:
838 default:
839 value = 0x00074526;
840 break;
841 }
842 pmif->timings[0] = pmif->timings[1] = value;
843 pmif->timings[2] = pmif->timings[3] = value2;
844}
845
d58b0c39
BH
846static int on_media_bay(pmac_ide_hwif_t *pmif)
847{
848 return pmif->mdev && pmif->mdev->media_bay != NULL;
849}
850
1da177e4
LT
851/* Suspend call back, should be called after the child devices
852 * have actually been suspended
853 */
7b8797ac 854static int pmac_ide_do_suspend(pmac_ide_hwif_t *pmif)
1da177e4 855{
1da177e4
LT
856 /* We clear the timings */
857 pmif->timings[0] = 0;
858 pmif->timings[1] = 0;
859
616299af
BH
860 disable_irq(pmif->irq);
861
1da177e4 862 /* The media bay will handle itself just fine */
d58b0c39 863 if (on_media_bay(pmif))
1da177e4
LT
864 return 0;
865
866 /* Kauai has bus control FCRs directly here */
867 if (pmif->kauai_fcr) {
868 u32 fcr = readl(pmif->kauai_fcr);
869 fcr &= ~(KAUAI_FCR_UATA_RESET_N | KAUAI_FCR_UATA_ENABLE);
870 writel(fcr, pmif->kauai_fcr);
871 }
872
873 /* Disable the bus on older machines and the cell on kauai */
874 ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, pmif->node, pmif->aapl_bus_id,
875 0);
876
877 return 0;
878}
879
880/* Resume call back, should be called before the child devices
881 * are resumed
882 */
7b8797ac 883static int pmac_ide_do_resume(pmac_ide_hwif_t *pmif)
1da177e4 884{
1da177e4 885 /* Hard reset & re-enable controller (do we really need to reset ? -BenH) */
d58b0c39 886 if (!on_media_bay(pmif)) {
1da177e4
LT
887 ppc_md.feature_call(PMAC_FTR_IDE_RESET, pmif->node, pmif->aapl_bus_id, 1);
888 ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, pmif->node, pmif->aapl_bus_id, 1);
889 msleep(10);
890 ppc_md.feature_call(PMAC_FTR_IDE_RESET, pmif->node, pmif->aapl_bus_id, 0);
1da177e4
LT
891
892 /* Kauai has it different */
893 if (pmif->kauai_fcr) {
894 u32 fcr = readl(pmif->kauai_fcr);
895 fcr |= KAUAI_FCR_UATA_RESET_N | KAUAI_FCR_UATA_ENABLE;
896 writel(fcr, pmif->kauai_fcr);
897 }
616299af
BH
898
899 msleep(jiffies_to_msecs(IDE_WAKEUP_DELAY));
1da177e4
LT
900 }
901
902 /* Sanitize drive timings */
903 sanitize_timings(pmif);
904
616299af
BH
905 enable_irq(pmif->irq);
906
1da177e4
LT
907 return 0;
908}
909
07a6c66d
BZ
910static u8 pmac_ide_cable_detect(ide_hwif_t *hwif)
911{
58e48be7 912 pmac_ide_hwif_t *pmif = dev_get_drvdata(hwif->gendev.parent);
07a6c66d
BZ
913 struct device_node *np = pmif->node;
914 const char *cable = of_get_property(np, "cable-type", NULL);
a9d5a97f
TH
915 struct device_node *root = of_find_node_by_path("/");
916 const char *model = of_get_property(root, "model", NULL);
07a6c66d 917
a51921c0 918 of_node_put(root);
07a6c66d 919 /* Get cable type from device-tree. */
a9d5a97f
TH
920 if (cable && !strncmp(cable, "80-", 3)) {
921 /* Some drives fail to detect 80c cable in PowerBook */
922 /* These machine use proprietary short IDE cable anyway */
923 if (!strncmp(model, "PowerBook", 9))
924 return ATA_CBL_PATA40_SHORT;
925 else
926 return ATA_CBL_PATA80;
927 }
07a6c66d
BZ
928
929 /*
930 * G5's seem to have incorrect cable type in device-tree.
931 * Let's assume they have a 80 conductor cable, this seem
932 * to be always the case unless the user mucked around.
933 */
934 if (of_device_is_compatible(np, "K2-UATA") ||
935 of_device_is_compatible(np, "shasta-ata"))
936 return ATA_CBL_PATA80;
937
938 return ATA_CBL_PATA40;
939}
940
07eb106f
BZ
941static void pmac_ide_init_dev(ide_drive_t *drive)
942{
943 ide_hwif_t *hwif = drive->hwif;
58e48be7 944 pmac_ide_hwif_t *pmif = dev_get_drvdata(hwif->gendev.parent);
07eb106f 945
d58b0c39
BH
946 if (on_media_bay(pmif)) {
947 if (check_media_bay(pmif->mdev->media_bay) == MB_CD) {
97100fc8 948 drive->dev_flags &= ~IDE_DFLAG_NOPROBE;
07eb106f
BZ
949 return;
950 }
97100fc8 951 drive->dev_flags |= IDE_DFLAG_NOPROBE;
07eb106f
BZ
952 }
953}
954
374e042c
BZ
955static const struct ide_tp_ops pmac_tp_ops = {
956 .exec_command = pmac_exec_command,
957 .read_status = ide_read_status,
958 .read_altstatus = ide_read_altstatus,
ecf3a31d 959 .write_devctl = pmac_write_devctl,
374e042c 960
abb596b2 961 .dev_select = pmac_dev_select,
374e042c
BZ
962 .tf_load = ide_tf_load,
963 .tf_read = ide_tf_read,
964
965 .input_data = ide_input_data,
966 .output_data = ide_output_data,
967};
968
abb596b2
SS
969static const struct ide_tp_ops pmac_ata6_tp_ops = {
970 .exec_command = pmac_exec_command,
971 .read_status = ide_read_status,
972 .read_altstatus = ide_read_altstatus,
973 .write_devctl = pmac_write_devctl,
974
975 .dev_select = pmac_kauai_dev_select,
976 .tf_load = ide_tf_load,
977 .tf_read = ide_tf_read,
978
979 .input_data = ide_input_data,
980 .output_data = ide_output_data,
07a6c66d
BZ
981};
982
983static const struct ide_port_ops pmac_ide_ata4_port_ops = {
07eb106f 984 .init_dev = pmac_ide_init_dev,
07a6c66d
BZ
985 .set_pio_mode = pmac_ide_set_pio_mode,
986 .set_dma_mode = pmac_ide_set_dma_mode,
07a6c66d 987 .cable_detect = pmac_ide_cable_detect,
ac95beed
BZ
988};
989
990static const struct ide_port_ops pmac_ide_port_ops = {
07eb106f 991 .init_dev = pmac_ide_init_dev,
ac95beed
BZ
992 .set_pio_mode = pmac_ide_set_pio_mode,
993 .set_dma_mode = pmac_ide_set_dma_mode,
ac95beed
BZ
994};
995
f37afdac 996static const struct ide_dma_ops pmac_dma_ops;
5e37bdc0 997
c413b9b9 998static const struct ide_port_info pmac_port_info = {
b36ba532 999 .name = DRV_NAME,
0d071922 1000 .init_dma = pmac_ide_init_dma,
c413b9b9 1001 .chipset = ide_pmac,
374e042c
BZ
1002 .tp_ops = &pmac_tp_ops,
1003 .port_ops = &pmac_ide_port_ops,
5e37bdc0 1004 .dma_ops = &pmac_dma_ops,
c413b9b9 1005 .host_flags = IDE_HFLAG_SET_PIO_MODE_KEEP_DMA |
c413b9b9 1006 IDE_HFLAG_POST_SET_MODE |
c5dd43ec 1007 IDE_HFLAG_MMIO |
c413b9b9
BZ
1008 IDE_HFLAG_UNMASK_IRQS,
1009 .pio_mask = ATA_PIO4,
1010 .mwdma_mask = ATA_MWDMA2,
1011};
1012
1da177e4
LT
1013/*
1014 * Setup, register & probe an IDE channel driven by this driver, this is
5b16464a 1015 * called by one of the 2 probe functions (macio or PCI).
1da177e4 1016 */
fe31edc8 1017static int pmac_ide_setup_device(pmac_ide_hwif_t *pmif, struct ide_hw *hw)
1da177e4
LT
1018{
1019 struct device_node *np = pmif->node;
018a3d1d 1020 const int *bidp;
48c3c107 1021 struct ide_host *host;
b36ba532 1022 ide_hwif_t *hwif;
9f36d314 1023 struct ide_hw *hws[] = { hw };
c413b9b9 1024 struct ide_port_info d = pmac_port_info;
6f904d01 1025 int rc;
1da177e4 1026
1da177e4 1027 pmif->broken_dma = pmif->broken_dma_warn = 0;
c413b9b9 1028 if (of_device_is_compatible(np, "shasta-ata")) {
1da177e4 1029 pmif->kind = controller_sh_ata6;
abb596b2
SS
1030 d.tp_ops = &pmac_ata6_tp_ops;
1031 d.port_ops = &pmac_ide_ata4_port_ops;
c413b9b9
BZ
1032 d.udma_mask = ATA_UDMA6;
1033 } else if (of_device_is_compatible(np, "kauai-ata")) {
1da177e4 1034 pmif->kind = controller_un_ata6;
abb596b2
SS
1035 d.tp_ops = &pmac_ata6_tp_ops;
1036 d.port_ops = &pmac_ide_ata4_port_ops;
c413b9b9
BZ
1037 d.udma_mask = ATA_UDMA5;
1038 } else if (of_device_is_compatible(np, "K2-UATA")) {
1da177e4 1039 pmif->kind = controller_k2_ata6;
abb596b2
SS
1040 d.tp_ops = &pmac_ata6_tp_ops;
1041 d.port_ops = &pmac_ide_ata4_port_ops;
c413b9b9
BZ
1042 d.udma_mask = ATA_UDMA5;
1043 } else if (of_device_is_compatible(np, "keylargo-ata")) {
c1fa31b0 1044 if (of_node_name_eq(np, "ata-4")) {
1da177e4 1045 pmif->kind = controller_kl_ata4;
07a6c66d 1046 d.port_ops = &pmac_ide_ata4_port_ops;
c413b9b9
BZ
1047 d.udma_mask = ATA_UDMA4;
1048 } else
1da177e4 1049 pmif->kind = controller_kl_ata3;
c413b9b9 1050 } else if (of_device_is_compatible(np, "heathrow-ata")) {
1da177e4 1051 pmif->kind = controller_heathrow;
c413b9b9 1052 } else {
1da177e4
LT
1053 pmif->kind = controller_ohare;
1054 pmif->broken_dma = 1;
1055 }
1056
40cd3a45 1057 bidp = of_get_property(np, "AAPL,bus-id", NULL);
1da177e4
LT
1058 pmif->aapl_bus_id = bidp ? *bidp : 0;
1059
1da177e4
LT
1060 /* On Kauai-type controllers, we make sure the FCR is correct */
1061 if (pmif->kauai_fcr)
1062 writel(KAUAI_FCR_UATA_MAGIC |
1063 KAUAI_FCR_UATA_RESET_N |
1064 KAUAI_FCR_UATA_ENABLE, pmif->kauai_fcr);
1da177e4
LT
1065
1066 /* Make sure we have sane timings */
1067 sanitize_timings(pmif);
1068
d58b0c39
BH
1069 /* If we are on a media bay, wait for it to settle and lock it */
1070 if (pmif->mdev)
1071 lock_media_bay(pmif->mdev->media_bay);
1072
dca39830 1073 host = ide_host_alloc(&d, hws, 1);
d58b0c39
BH
1074 if (host == NULL) {
1075 rc = -ENOMEM;
1076 goto bail;
1077 }
1078 hwif = pmif->hwif = host->ports[0];
9842727d 1079
d58b0c39
BH
1080 if (on_media_bay(pmif)) {
1081 /* Fixup bus ID for media bay */
1da177e4
LT
1082 if (!bidp)
1083 pmif->aapl_bus_id = 1;
1084 } else if (pmif->kind == controller_ohare) {
1085 /* The code below is having trouble on some ohare machines
1086 * (timing related ?). Until I can put my hand on one of these
1087 * units, I keep the old way
1088 */
1089 ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, np, 0, 1);
d58b0c39 1090 } else {
1da177e4
LT
1091 /* This is necessary to enable IDE when net-booting */
1092 ppc_md.feature_call(PMAC_FTR_IDE_RESET, np, pmif->aapl_bus_id, 1);
1093 ppc_md.feature_call(PMAC_FTR_IDE_ENABLE, np, pmif->aapl_bus_id, 1);
1094 msleep(10);
1095 ppc_md.feature_call(PMAC_FTR_IDE_RESET, np, pmif->aapl_bus_id, 0);
1096 msleep(jiffies_to_msecs(IDE_WAKEUP_DELAY));
1097 }
1098
b36ba532 1099 printk(KERN_INFO DRV_NAME ": Found Apple %s controller (%s), "
d58b0c39
BH
1100 "bus ID %d%s, irq %d\n", model_name[pmif->kind],
1101 pmif->mdev ? "macio" : "PCI", pmif->aapl_bus_id,
1102 on_media_bay(pmif) ? " (mediabay)" : "", hw->irq);
b36ba532 1103
9842727d 1104 rc = ide_host_register(host, &d, hws);
d58b0c39
BH
1105 if (rc)
1106 pmif->hwif = NULL;
5cbf79cd 1107
d58b0c39
BH
1108 if (pmif->mdev)
1109 unlock_media_bay(pmif->mdev->media_bay);
1110
1111 bail:
1112 if (rc && host)
1113 ide_host_free(host);
1114 return rc;
1da177e4
LT
1115}
1116
fe31edc8 1117static void pmac_ide_init_ports(struct ide_hw *hw, unsigned long base)
5c58666f
BZ
1118{
1119 int i;
1120
1121 for (i = 0; i < 8; ++i)
4c3032d8
BZ
1122 hw->io_ports_array[i] = base + i * 0x10;
1123
1124 hw->io_ports.ctl_addr = base + 0x160;
5c58666f
BZ
1125}
1126
1da177e4
LT
1127/*
1128 * Attach to a macio probed interface
1129 */
fe31edc8
GKH
1130static int pmac_ide_macio_attach(struct macio_dev *mdev,
1131 const struct of_device_id *match)
1da177e4
LT
1132{
1133 void __iomem *base;
1134 unsigned long regbase;
1da177e4 1135 pmac_ide_hwif_t *pmif;
939b0f1d 1136 int irq, rc;
9f36d314 1137 struct ide_hw hw;
1da177e4 1138
5297a3e5
BZ
1139 pmif = kzalloc(sizeof(*pmif), GFP_KERNEL);
1140 if (pmif == NULL)
1141 return -ENOMEM;
1142
cc5d0189 1143 if (macio_resource_count(mdev) == 0) {
859420e3
RH
1144 printk(KERN_WARNING "ide-pmac: no address for %pOF\n",
1145 mdev->ofdev.dev.of_node);
5297a3e5
BZ
1146 rc = -ENXIO;
1147 goto out_free_pmif;
1da177e4
LT
1148 }
1149
1150 /* Request memory resource for IO ports */
1151 if (macio_request_resource(mdev, 0, "ide-pmac (ports)")) {
939b0f1d 1152 printk(KERN_ERR "ide-pmac: can't request MMIO resource for "
859420e3 1153 "%pOF!\n", mdev->ofdev.dev.of_node);
5297a3e5
BZ
1154 rc = -EBUSY;
1155 goto out_free_pmif;
1da177e4
LT
1156 }
1157
1158 /* XXX This is bogus. Should be fixed in the registry by checking
1159 * the kind of host interrupt controller, a bit like gatwick
1160 * fixes in irq.c. That works well enough for the single case
1161 * where that happens though...
1162 */
1163 if (macio_irq_count(mdev) == 0) {
859420e3
RH
1164 printk(KERN_WARNING "ide-pmac: no intrs for device %pOF, using "
1165 "13\n", mdev->ofdev.dev.of_node);
69917c26 1166 irq = irq_create_mapping(NULL, 13);
1da177e4
LT
1167 } else
1168 irq = macio_irq(mdev, 0);
1169
1170 base = ioremap(macio_resource_start(mdev, 0), 0x400);
1171 regbase = (unsigned long) base;
1172
1da177e4 1173 pmif->mdev = mdev;
61c7a080 1174 pmif->node = mdev->ofdev.dev.of_node;
1da177e4
LT
1175 pmif->regbase = regbase;
1176 pmif->irq = irq;
1177 pmif->kauai_fcr = NULL;
53846574 1178
1da177e4
LT
1179 if (macio_resource_count(mdev) >= 2) {
1180 if (macio_request_resource(mdev, 1, "ide-pmac (dma)"))
939b0f1d 1181 printk(KERN_WARNING "ide-pmac: can't request DMA "
859420e3
RH
1182 "resource for %pOF!\n",
1183 mdev->ofdev.dev.of_node);
1da177e4
LT
1184 else
1185 pmif->dma_regs = ioremap(macio_resource_start(mdev, 1), 0x1000);
1186 } else
1187 pmif->dma_regs = NULL;
53846574 1188
7b8797ac 1189 dev_set_drvdata(&mdev->ofdev.dev, pmif);
1da177e4 1190
57c802e8 1191 memset(&hw, 0, sizeof(hw));
5c58666f 1192 pmac_ide_init_ports(&hw, pmif->regbase);
57c802e8 1193 hw.irq = irq;
c56c5648
BZ
1194 hw.dev = &mdev->bus->pdev->dev;
1195 hw.parent = &mdev->ofdev.dev;
57c802e8 1196
b36ba532 1197 rc = pmac_ide_setup_device(pmif, &hw);
1da177e4
LT
1198 if (rc != 0) {
1199 /* The inteface is released to the common IDE layer */
1200 dev_set_drvdata(&mdev->ofdev.dev, NULL);
1201 iounmap(base);
ed908fa1 1202 if (pmif->dma_regs) {
1da177e4 1203 iounmap(pmif->dma_regs);
ed908fa1
BZ
1204 macio_release_resource(mdev, 1);
1205 }
1da177e4 1206 macio_release_resource(mdev, 0);
5297a3e5 1207 kfree(pmif);
1da177e4
LT
1208 }
1209
1210 return rc;
5297a3e5
BZ
1211
1212out_free_pmif:
1213 kfree(pmif);
1214 return rc;
1da177e4
LT
1215}
1216
1217static int
8b4b8a24 1218pmac_ide_macio_suspend(struct macio_dev *mdev, pm_message_t mesg)
1da177e4 1219{
58e48be7 1220 pmac_ide_hwif_t *pmif = dev_get_drvdata(&mdev->ofdev.dev);
7b8797ac 1221 int rc = 0;
1da177e4 1222
8b4b8a24 1223 if (mesg.event != mdev->ofdev.dev.power.power_state.event
3a2d5b70 1224 && (mesg.event & PM_EVENT_SLEEP)) {
7b8797ac 1225 rc = pmac_ide_do_suspend(pmif);
1da177e4 1226 if (rc == 0)
8b4b8a24 1227 mdev->ofdev.dev.power.power_state = mesg;
1da177e4
LT
1228 }
1229
1230 return rc;
1231}
1232
1233static int
1234pmac_ide_macio_resume(struct macio_dev *mdev)
1235{
58e48be7 1236 pmac_ide_hwif_t *pmif = dev_get_drvdata(&mdev->ofdev.dev);
7b8797ac
BZ
1237 int rc = 0;
1238
ca078bae 1239 if (mdev->ofdev.dev.power.power_state.event != PM_EVENT_ON) {
7b8797ac 1240 rc = pmac_ide_do_resume(pmif);
1da177e4 1241 if (rc == 0)
829ca9a3 1242 mdev->ofdev.dev.power.power_state = PMSG_ON;
1da177e4
LT
1243 }
1244
1245 return rc;
1246}
1247
1248/*
1249 * Attach to a PCI probed interface
1250 */
fe31edc8
GKH
1251static int pmac_ide_pci_attach(struct pci_dev *pdev,
1252 const struct pci_device_id *id)
1da177e4 1253{
1da177e4
LT
1254 struct device_node *np;
1255 pmac_ide_hwif_t *pmif;
1256 void __iomem *base;
1257 unsigned long rbase, rlen;
939b0f1d 1258 int rc;
9f36d314 1259 struct ide_hw hw;
1da177e4
LT
1260
1261 np = pci_device_to_OF_node(pdev);
1262 if (np == NULL) {
1263 printk(KERN_ERR "ide-pmac: cannot find MacIO node for Kauai ATA interface\n");
1264 return -ENODEV;
1265 }
5297a3e5
BZ
1266
1267 pmif = kzalloc(sizeof(*pmif), GFP_KERNEL);
1268 if (pmif == NULL)
1269 return -ENOMEM;
1270
1da177e4 1271 if (pci_enable_device(pdev)) {
939b0f1d 1272 printk(KERN_WARNING "ide-pmac: Can't enable PCI device for "
859420e3 1273 "%pOF\n", np);
5297a3e5
BZ
1274 rc = -ENXIO;
1275 goto out_free_pmif;
1da177e4
LT
1276 }
1277 pci_set_master(pdev);
1278
1279 if (pci_request_regions(pdev, "Kauai ATA")) {
939b0f1d 1280 printk(KERN_ERR "ide-pmac: Cannot obtain PCI resources for "
859420e3 1281 "%pOF\n", np);
5297a3e5
BZ
1282 rc = -ENXIO;
1283 goto out_free_pmif;
1da177e4
LT
1284 }
1285
1da177e4
LT
1286 pmif->mdev = NULL;
1287 pmif->node = np;
1288
1289 rbase = pci_resource_start(pdev, 0);
1290 rlen = pci_resource_len(pdev, 0);
1291
1292 base = ioremap(rbase, rlen);
1293 pmif->regbase = (unsigned long) base + 0x2000;
1da177e4 1294 pmif->dma_regs = base + 0x1000;
1da177e4
LT
1295 pmif->kauai_fcr = base;
1296 pmif->irq = pdev->irq;
1297
7b8797ac 1298 pci_set_drvdata(pdev, pmif);
1da177e4 1299
57c802e8 1300 memset(&hw, 0, sizeof(hw));
5c58666f 1301 pmac_ide_init_ports(&hw, pmif->regbase);
57c802e8
BZ
1302 hw.irq = pdev->irq;
1303 hw.dev = &pdev->dev;
1304
b36ba532 1305 rc = pmac_ide_setup_device(pmif, &hw);
1da177e4
LT
1306 if (rc != 0) {
1307 /* The inteface is released to the common IDE layer */
1da177e4 1308 iounmap(base);
1da177e4 1309 pci_release_regions(pdev);
5297a3e5 1310 kfree(pmif);
1da177e4
LT
1311 }
1312
1313 return rc;
5297a3e5
BZ
1314
1315out_free_pmif:
1316 kfree(pmif);
1317 return rc;
1da177e4
LT
1318}
1319
1320static int
8b4b8a24 1321pmac_ide_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
1da177e4 1322{
f2ba70a2 1323 pmac_ide_hwif_t *pmif = pci_get_drvdata(pdev);
7b8797ac
BZ
1324 int rc = 0;
1325
8b4b8a24 1326 if (mesg.event != pdev->dev.power.power_state.event
3a2d5b70 1327 && (mesg.event & PM_EVENT_SLEEP)) {
7b8797ac 1328 rc = pmac_ide_do_suspend(pmif);
1da177e4 1329 if (rc == 0)
8b4b8a24 1330 pdev->dev.power.power_state = mesg;
1da177e4
LT
1331 }
1332
1333 return rc;
1334}
1335
1336static int
1337pmac_ide_pci_resume(struct pci_dev *pdev)
1338{
f2ba70a2 1339 pmac_ide_hwif_t *pmif = pci_get_drvdata(pdev);
7b8797ac
BZ
1340 int rc = 0;
1341
ca078bae 1342 if (pdev->dev.power.power_state.event != PM_EVENT_ON) {
7b8797ac 1343 rc = pmac_ide_do_resume(pmif);
1da177e4 1344 if (rc == 0)
829ca9a3 1345 pdev->dev.power.power_state = PMSG_ON;
1da177e4
LT
1346 }
1347
1348 return rc;
1349}
1350
d58b0c39
BH
1351#ifdef CONFIG_PMAC_MEDIABAY
1352static void pmac_ide_macio_mb_event(struct macio_dev* mdev, int mb_state)
1353{
58e48be7 1354 pmac_ide_hwif_t *pmif = dev_get_drvdata(&mdev->ofdev.dev);
d58b0c39
BH
1355
1356 switch(mb_state) {
1357 case MB_CD:
1358 if (!pmif->hwif->present)
1359 ide_port_scan(pmif->hwif);
1360 break;
1361 default:
1362 if (pmif->hwif->present)
1363 ide_port_unregister_devices(pmif->hwif);
1364 }
1365}
1366#endif /* CONFIG_PMAC_MEDIABAY */
1367
1368
5e655772 1369static struct of_device_id pmac_ide_macio_match[] =
1da177e4
LT
1370{
1371 {
1372 .name = "IDE",
1da177e4
LT
1373 },
1374 {
1375 .name = "ATA",
1da177e4
LT
1376 },
1377 {
1da177e4 1378 .type = "ide",
1da177e4
LT
1379 },
1380 {
1da177e4 1381 .type = "ata",
1da177e4
LT
1382 },
1383 {},
1384};
1385
1386static struct macio_driver pmac_ide_macio_driver =
1387{
c2cdf6ab
BH
1388 .driver = {
1389 .name = "ide-pmac",
1390 .owner = THIS_MODULE,
1391 .of_match_table = pmac_ide_macio_match,
1392 },
1da177e4
LT
1393 .probe = pmac_ide_macio_attach,
1394 .suspend = pmac_ide_macio_suspend,
1395 .resume = pmac_ide_macio_resume,
d58b0c39
BH
1396#ifdef CONFIG_PMAC_MEDIABAY
1397 .mediabay_event = pmac_ide_macio_mb_event,
1398#endif
1da177e4
LT
1399};
1400
9cbcc5e3
BZ
1401static const struct pci_device_id pmac_ide_pci_match[] = {
1402 { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_UNI_N_ATA), 0 },
1403 { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_IPID_ATA100), 0 },
1404 { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_K2_ATA100), 0 },
1405 { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_SH_ATA), 0 },
1406 { PCI_VDEVICE(APPLE, PCI_DEVICE_ID_APPLE_IPID2_ATA), 0 },
71e4eda8 1407 {},
1da177e4
LT
1408};
1409
1410static struct pci_driver pmac_ide_pci_driver = {
1411 .name = "ide-pmac",
1412 .id_table = pmac_ide_pci_match,
1413 .probe = pmac_ide_pci_attach,
1414 .suspend = pmac_ide_pci_suspend,
1415 .resume = pmac_ide_pci_resume,
1416};
1417MODULE_DEVICE_TABLE(pci, pmac_ide_pci_match);
1418
9e5755bc 1419int __init pmac_ide_probe(void)
1da177e4 1420{
9e5755bc
AM
1421 int error;
1422
e8222502 1423 if (!machine_is(powermac))
9e5755bc 1424 return -ENODEV;
1da177e4
LT
1425
1426#ifdef CONFIG_BLK_DEV_IDE_PMAC_ATA100FIRST
9e5755bc
AM
1427 error = pci_register_driver(&pmac_ide_pci_driver);
1428 if (error)
1429 goto out;
1430 error = macio_register_driver(&pmac_ide_macio_driver);
1431 if (error) {
1432 pci_unregister_driver(&pmac_ide_pci_driver);
1433 goto out;
1434 }
1da177e4 1435#else
9e5755bc
AM
1436 error = macio_register_driver(&pmac_ide_macio_driver);
1437 if (error)
1438 goto out;
1439 error = pci_register_driver(&pmac_ide_pci_driver);
1440 if (error) {
1441 macio_unregister_driver(&pmac_ide_macio_driver);
1442 goto out;
1443 }
1beb6a7d 1444#endif
9e5755bc
AM
1445out:
1446 return error;
1da177e4
LT
1447}
1448
1da177e4
LT
1449/*
1450 * pmac_ide_build_dmatable builds the DBDMA command list
1451 * for a transfer and sets the DBDMA channel to point to it.
1452 */
22981694 1453static int pmac_ide_build_dmatable(ide_drive_t *drive, struct ide_cmd *cmd)
1da177e4 1454{
7b8797ac 1455 ide_hwif_t *hwif = drive->hwif;
58e48be7 1456 pmac_ide_hwif_t *pmif = dev_get_drvdata(hwif->gendev.parent);
1da177e4 1457 struct dbdma_cmd *table;
1da177e4
LT
1458 volatile struct dbdma_regs __iomem *dma = pmif->dma_regs;
1459 struct scatterlist *sg;
22981694
BZ
1460 int wr = !!(cmd->tf_flags & IDE_TFLAG_WRITE);
1461 int i = cmd->sg_nents, count = 0;
1da177e4
LT
1462
1463 /* DMA table is already aligned */
1464 table = (struct dbdma_cmd *) pmif->dma_table_cpu;
1465
1466 /* Make sure DMA controller is stopped (necessary ?) */
1467 writel((RUN|PAUSE|FLUSH|WAKE|DEAD) << 16, &dma->control);
1468 while (readl(&dma->status) & RUN)
1469 udelay(1);
1470
1da177e4
LT
1471 /* Build DBDMA commands list */
1472 sg = hwif->sg_table;
1473 while (i && sg_dma_len(sg)) {
1474 u32 cur_addr;
1475 u32 cur_len;
1476
1477 cur_addr = sg_dma_address(sg);
1478 cur_len = sg_dma_len(sg);
1479
1480 if (pmif->broken_dma && cur_addr & (L1_CACHE_BYTES - 1)) {
1481 if (pmif->broken_dma_warn == 0) {
aca38a51 1482 printk(KERN_WARNING "%s: DMA on non aligned address, "
1da177e4
LT
1483 "switching to PIO on Ohare chipset\n", drive->name);
1484 pmif->broken_dma_warn = 1;
1485 }
11998b31 1486 return 0;
1da177e4
LT
1487 }
1488 while (cur_len) {
1489 unsigned int tc = (cur_len < 0xfe00)? cur_len: 0xfe00;
1490
1491 if (count++ >= MAX_DCMDS) {
1492 printk(KERN_WARNING "%s: DMA table too small\n",
1493 drive->name);
11998b31 1494 return 0;
1da177e4 1495 }
f5718726
DG
1496 table->command = cpu_to_le16(wr? OUTPUT_MORE: INPUT_MORE);
1497 table->req_count = cpu_to_le16(tc);
1498 table->phy_addr = cpu_to_le32(cur_addr);
1da177e4
LT
1499 table->cmd_dep = 0;
1500 table->xfer_status = 0;
1501 table->res_count = 0;
1502 cur_addr += tc;
1503 cur_len -= tc;
1504 ++table;
1505 }
55c16a70 1506 sg = sg_next(sg);
1da177e4
LT
1507 i--;
1508 }
1509
1510 /* convert the last command to an input/output last command */
1511 if (count) {
f5718726 1512 table[-1].command = cpu_to_le16(wr? OUTPUT_LAST: INPUT_LAST);
1da177e4
LT
1513 /* add the stop command to the end of the list */
1514 memset(table, 0, sizeof(struct dbdma_cmd));
f5718726 1515 table->command = cpu_to_le16(DBDMA_STOP);
1da177e4
LT
1516 mb();
1517 writel(hwif->dmatable_dma, &dma->cmdptr);
1518 return 1;
1519 }
1520
1521 printk(KERN_DEBUG "%s: empty DMA table?\n", drive->name);
f6fb786d 1522
1da177e4
LT
1523 return 0; /* revert to PIO for this request */
1524}
1525
1da177e4
LT
1526/*
1527 * Prepare a DMA transfer. We build the DMA table, adjust the timings for
1528 * a read on KeyLargo ATA/66 and mark us as waiting for DMA completion
1529 */
22981694 1530static int pmac_ide_dma_setup(ide_drive_t *drive, struct ide_cmd *cmd)
1da177e4 1531{
898ec223 1532 ide_hwif_t *hwif = drive->hwif;
58e48be7 1533 pmac_ide_hwif_t *pmif = dev_get_drvdata(hwif->gendev.parent);
9055ba3e 1534 u8 unit = drive->dn & 1, ata4 = (pmif->kind == controller_kl_ata4);
22981694 1535 u8 write = !!(cmd->tf_flags & IDE_TFLAG_WRITE);
1da177e4 1536
11998b31 1537 if (pmac_ide_build_dmatable(drive, cmd) == 0)
1da177e4 1538 return 1;
1da177e4
LT
1539
1540 /* Apple adds 60ns to wrDataSetup on reads */
1541 if (ata4 && (pmif->timings[unit] & TR_66_UDMA_EN)) {
22981694 1542 writel(pmif->timings[unit] + (write ? 0 : 0x00800000UL),
1da177e4
LT
1543 PMAC_IDE_REG(IDE_TIMING_CONFIG));
1544 (void)readl(PMAC_IDE_REG(IDE_TIMING_CONFIG));
1545 }
1546
1da177e4
LT
1547 return 0;
1548}
1549
1da177e4
LT
1550/*
1551 * Kick the DMA controller into life after the DMA command has been issued
1552 * to the drive.
1553 */
aacaf9bd 1554static void
1da177e4
LT
1555pmac_ide_dma_start(ide_drive_t *drive)
1556{
7b8797ac 1557 ide_hwif_t *hwif = drive->hwif;
58e48be7 1558 pmac_ide_hwif_t *pmif = dev_get_drvdata(hwif->gendev.parent);
1da177e4
LT
1559 volatile struct dbdma_regs __iomem *dma;
1560
1561 dma = pmif->dma_regs;
1562
1563 writel((RUN << 16) | RUN, &dma->control);
1564 /* Make sure it gets to the controller right now */
1565 (void)readl(&dma->control);
1566}
1567
1568/*
1569 * After a DMA transfer, make sure the controller is stopped
1570 */
aacaf9bd 1571static int
1da177e4
LT
1572pmac_ide_dma_end (ide_drive_t *drive)
1573{
7b8797ac 1574 ide_hwif_t *hwif = drive->hwif;
58e48be7 1575 pmac_ide_hwif_t *pmif = dev_get_drvdata(hwif->gendev.parent);
9055ba3e 1576 volatile struct dbdma_regs __iomem *dma = pmif->dma_regs;
1da177e4 1577 u32 dstat;
1da177e4 1578
1da177e4
LT
1579 dstat = readl(&dma->status);
1580 writel(((RUN|WAKE|DEAD) << 16), &dma->control);
f5e0b5ec 1581
1da177e4
LT
1582 /* verify good dma status. we don't check for ACTIVE beeing 0. We should...
1583 * in theory, but with ATAPI decices doing buffer underruns, that would
1584 * cause us to disable DMA, which isn't what we want
1585 */
1586 return (dstat & (RUN|DEAD)) != RUN;
1587}
1588
1589/*
1590 * Check out that the interrupt we got was for us. We can't always know this
1591 * for sure with those Apple interfaces (well, we could on the recent ones but
1592 * that's not implemented yet), on the other hand, we don't have shared interrupts
1593 * so it's not really a problem
1594 */
aacaf9bd 1595static int
1da177e4
LT
1596pmac_ide_dma_test_irq (ide_drive_t *drive)
1597{
7b8797ac 1598 ide_hwif_t *hwif = drive->hwif;
58e48be7 1599 pmac_ide_hwif_t *pmif = dev_get_drvdata(hwif->gendev.parent);
9055ba3e 1600 volatile struct dbdma_regs __iomem *dma = pmif->dma_regs;
1da177e4
LT
1601 unsigned long status, timeout;
1602
1da177e4
LT
1603 /* We have to things to deal with here:
1604 *
1605 * - The dbdma won't stop if the command was started
1606 * but completed with an error without transferring all
1607 * datas. This happens when bad blocks are met during
1608 * a multi-block transfer.
1609 *
1610 * - The dbdma fifo hasn't yet finished flushing to
1611 * to system memory when the disk interrupt occurs.
1612 *
1613 */
1614
1615 /* If ACTIVE is cleared, the STOP command have passed and
1616 * transfer is complete.
1617 */
1618 status = readl(&dma->status);
1619 if (!(status & ACTIVE))
1620 return 1;
1da177e4
LT
1621
1622 /* If dbdma didn't execute the STOP command yet, the
1623 * active bit is still set. We consider that we aren't
1624 * sharing interrupts (which is hopefully the case with
1625 * those controllers) and so we just try to flush the
1626 * channel for pending data in the fifo
1627 */
1628 udelay(1);
1629 writel((FLUSH << 16) | FLUSH, &dma->control);
1630 timeout = 0;
1631 for (;;) {
1632 udelay(1);
1633 status = readl(&dma->status);
1634 if ((status & FLUSH) == 0)
1635 break;
1636 if (++timeout > 100) {
b1681c56
JP
1637 printk(KERN_WARNING "ide%d, ide_dma_test_irq timeout flushing channel\n",
1638 hwif->index);
1da177e4
LT
1639 break;
1640 }
1641 }
1642 return 1;
1643}
1644
15ce926a 1645static void pmac_ide_dma_host_set(ide_drive_t *drive, int on)
1da177e4 1646{
1da177e4
LT
1647}
1648
841d2a9b
SS
1649static void
1650pmac_ide_dma_lost_irq (ide_drive_t *drive)
1da177e4 1651{
7b8797ac 1652 ide_hwif_t *hwif = drive->hwif;
58e48be7 1653 pmac_ide_hwif_t *pmif = dev_get_drvdata(hwif->gendev.parent);
9055ba3e
BZ
1654 volatile struct dbdma_regs __iomem *dma = pmif->dma_regs;
1655 unsigned long status = readl(&dma->status);
1da177e4 1656
1da177e4 1657 printk(KERN_ERR "ide-pmac lost interrupt, dma status: %lx\n", status);
1da177e4
LT
1658}
1659
f37afdac 1660static const struct ide_dma_ops pmac_dma_ops = {
5e37bdc0
BZ
1661 .dma_host_set = pmac_ide_dma_host_set,
1662 .dma_setup = pmac_ide_dma_setup,
5e37bdc0
BZ
1663 .dma_start = pmac_ide_dma_start,
1664 .dma_end = pmac_ide_dma_end,
1665 .dma_test_irq = pmac_ide_dma_test_irq,
5e37bdc0
BZ
1666 .dma_lost_irq = pmac_ide_dma_lost_irq,
1667};
1668
1da177e4
LT
1669/*
1670 * Allocate the data structures needed for using DMA with an interface
1671 * and fill the proper list of functions pointers
1672 */
fe31edc8 1673static int pmac_ide_init_dma(ide_hwif_t *hwif, const struct ide_port_info *d)
1da177e4 1674{
58e48be7 1675 pmac_ide_hwif_t *pmif = dev_get_drvdata(hwif->gendev.parent);
36501650
BZ
1676 struct pci_dev *dev = to_pci_dev(hwif->dev);
1677
1da177e4
LT
1678 /* We won't need pci_dev if we switch to generic consistent
1679 * DMA routines ...
1680 */
0d071922 1681 if (dev == NULL || pmif->dma_regs == 0)
c413b9b9 1682 return -ENODEV;
1da177e4
LT
1683 /*
1684 * Allocate space for the DBDMA commands.
1685 * The +2 is +1 for the stop command and +1 to allow for
1686 * aligning the start address to a multiple of 16 bytes.
1687 */
d681f116 1688 pmif->dma_table_cpu = dma_alloc_coherent(&dev->dev,
1da177e4 1689 (MAX_DCMDS + 2) * sizeof(struct dbdma_cmd),
d681f116 1690 &hwif->dmatable_dma, GFP_KERNEL);
1da177e4
LT
1691 if (pmif->dma_table_cpu == NULL) {
1692 printk(KERN_ERR "%s: unable to allocate DMA command list\n",
1693 hwif->name);
c413b9b9 1694 return -ENOMEM;
1da177e4
LT
1695 }
1696
4f52a329
BZ
1697 hwif->sg_max_nents = MAX_DCMDS;
1698
c413b9b9 1699 return 0;
1da177e4 1700}
ade2daf9
BZ
1701
1702module_init(pmac_ide_probe);
de9facbf
AB
1703
1704MODULE_LICENSE("GPL");