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