2 * linux/drivers/ide/ide-pmac.c Version ?.?? Mar. 18, 2000
4 * Support for IDE interfaces on PowerMacs.
5 * These IDE interfaces are memory-mapped and have a DBDMA channel
8 * Copyright (C) 1998 Paul Mackerras.
10 * Bits from Benjamin Herrenschmidt
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
17 * Some code taken from drivers/ide/ide-dma.c:
19 * Copyright (c) 1995-1998 Mark Lord
22 #include <linux/config.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/ide.h>
32 #include <asm/dbdma.h>
34 #include <asm/mediabay.h>
35 #include <asm/feature.h>
36 #ifdef CONFIG_PMAC_PBOOK
37 #include <linux/adb.h>
38 #include <linux/pmu.h>
41 #include "ide_modes.h"
43 extern char *ide_dmafunc_verbose(ide_dma_action_t dmafunc
);
47 #define IDE_SYSCLK_NS 30
48 #define IDE_SYSCLK_ULTRA_PS 0x1d4c /* (15 * 1000 / 2)*/
50 struct pmac_ide_hwif
{
55 struct device_node
* node
;
57 #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
58 volatile struct dbdma_regs
* dma_regs
;
59 struct dbdma_cmd
* dma_table
;
62 } pmac_ide
[MAX_HWIFS
];
64 static int pmac_ide_count
;
67 controller_ohare
, /* OHare based */
68 controller_heathrow
, /* Heathrow/Paddington */
69 controller_kl_ata3
, /* KeyLargo ATA-3 */
70 controller_kl_ata4
/* KeyLargo ATA-4 */
74 #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
81 /* Multiword DMA timings */
82 static pmac_ide_timing mdma_timings
[] =
84 { 215, 480 }, /* Mode 0 */
89 /* Ultra DMA timings (for use when I know how to calculate them */
90 static pmac_ide_timing udma_timings
[] =
92 { 0, 114 }, /* Mode 0 */
99 /* allow up to 256 DBDMA commands per xfer */
100 #define MAX_DCMDS 256
102 /* Wait 1.5s for disk to answer on IDE bus after
104 * NOTE: There is at least one case I know of a disk that needs about 10sec
105 * before anwering on the bus. I beleive we could add a kernel command
106 * line arg to override this delay for such cases.
108 #define IDE_WAKEUP_DELAY_MS 1500
110 static void pmac_ide_setup_dma(struct device_node
*np
, int ix
);
111 static int pmac_ide_dmaproc(ide_dma_action_t func
, ide_drive_t
*drive
);
112 static int pmac_ide_build_dmatable(ide_drive_t
*drive
, int ix
, int wr
);
113 static int pmac_ide_tune_chipset(ide_drive_t
*drive
, byte speed
);
114 static void pmac_ide_tuneproc(ide_drive_t
*drive
, byte pio
);
115 static void pmac_ide_selectproc(ide_drive_t
*drive
);
117 #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
119 #ifdef CONFIG_PMAC_PBOOK
120 static int idepmac_notify_sleep(struct pmu_sleep_notifier
*self
, int when
);
121 struct pmu_sleep_notifier idepmac_sleep_notifier
= {
122 idepmac_notify_sleep
, SLEEP_LEVEL_BLOCK
,
124 #endif /* CONFIG_PMAC_PBOOK */
127 pmac_ide_find(ide_drive_t
*drive
)
129 ide_hwif_t
*hwif
= HWIF(drive
);
133 for (i
=0; i
<pmac_ide_count
; i
++) {
134 base
= pmac_ide
[i
].regbase
;
135 if (base
&& base
== hwif
->io_ports
[0])
142 * N.B. this can't be an initfunc, because the media-bay task can
143 * call ide_[un]register at any time.
145 void pmac_ide_init_hwif_ports(hw_regs_t
*hw
,
146 ide_ioreg_t data_port
, ide_ioreg_t ctrl_port
,
154 for (ix
= 0; ix
< MAX_HWIFS
; ++ix
)
155 if (data_port
== pmac_ide
[ix
].regbase
)
158 if (ix
>= MAX_HWIFS
) {
159 /* Probably a PCI interface... */
160 for (i
= IDE_DATA_OFFSET
; i
<= IDE_STATUS_OFFSET
; ++i
)
161 hw
->io_ports
[i
] = data_port
+ i
- IDE_DATA_OFFSET
;
162 /* XXX is this right? */
163 hw
->io_ports
[IDE_CONTROL_OFFSET
] = 0;
169 for (i
= 0; i
< 8; ++i
)
170 hw
->io_ports
[i
] = data_port
+ i
* 0x10;
171 hw
->io_ports
[8] = data_port
+ 0x160;
174 *irq
= pmac_ide
[ix
].irq
;
176 ide_hwifs
[ix
].tuneproc
= pmac_ide_tuneproc
;
177 ide_hwifs
[ix
].selectproc
= pmac_ide_selectproc
;
178 if (pmac_ide
[ix
].dma_regs
&& pmac_ide
[ix
].dma_table
) {
179 ide_hwifs
[ix
].dmaproc
= &pmac_ide_dmaproc
;
180 #ifdef CONFIG_PMAC_IDEDMA_AUTO
181 ide_hwifs
[ix
].autodma
= 1;
183 // ide_hwifs[ix].speedproc = &pmac_ide_tune_chipset;
188 /* This one could be later extended to handle CMD IDE and be used by some kind
189 * of /proc interface. I want to be able to get the devicetree path of a block
190 * device for yaboot configuration
193 pmac_ide_get_devnode(ide_drive_t
*drive
)
195 int i
= pmac_ide_find(drive
);
198 return pmac_ide
[i
].node
;
202 /* Setup timings for the selected drive (master/slave). I still need to verify if this
203 * is enough, I beleive selectproc will be called whenever an IDE command is started,
206 pmac_ide_selectproc(ide_drive_t
*drive
)
208 int i
= pmac_ide_find(drive
);
212 if (drive
->select
.all
& 0x10)
213 out_le32((unsigned *)(IDE_DATA_REG
+ 0x200 + _IO_BASE
), pmac_ide
[i
].timings
[1]);
215 out_le32((unsigned *)(IDE_DATA_REG
+ 0x200 + _IO_BASE
), pmac_ide
[i
].timings
[0]);
218 /* Number of IDE_SYSCLK_NS ticks, argument is in nanoseconds */
219 #define SYSCLK_TICKS(t) (((t) + IDE_SYSCLK_NS - 1) / IDE_SYSCLK_NS)
220 #define SYSCLK_TICKS_UDMA(t) (((t) + IDE_SYSCLK_ULTRA_PS - 1) / IDE_SYSCLK_ULTRA_PS)
222 /* Calculate PIO timings */
224 pmac_ide_tuneproc(ide_drive_t
*drive
, byte pio
)
229 int accessTicks
, recTicks
;
231 i
= pmac_ide_find(drive
);
235 pio
= ide_get_best_pio_mode(drive
, pio
, 4, &d
);
236 accessTicks
= SYSCLK_TICKS(ide_pio_timings
[pio
].active_time
);
237 if (drive
->select
.all
& 0x10)
238 timings
= &pmac_ide
[i
].timings
[1];
240 timings
= &pmac_ide
[i
].timings
[0];
242 if (pmac_ide
[i
].kind
== controller_kl_ata4
) {
243 /* The "ata-4" IDE controller of Core99 machines */
244 accessTicks
= SYSCLK_TICKS_UDMA(ide_pio_timings
[pio
].active_time
* 1000);
245 recTicks
= SYSCLK_TICKS_UDMA(d
.cycle_time
* 1000) - accessTicks
;
247 *timings
= ((*timings
) & 0x1FFFFFC00) | accessTicks
| (recTicks
<< 5);
249 /* The old "ata-3" IDE controller */
250 accessTicks
= SYSCLK_TICKS(ide_pio_timings
[pio
].active_time
);
253 recTicks
= SYSCLK_TICKS(d
.cycle_time
) - accessTicks
- 4;
257 *timings
= ((*timings
) & 0xFFFFFF800) | accessTicks
| (recTicks
<< 5);
260 #ifdef IDE_PMAC_DEBUG
261 printk("ide_pmac: Set PIO timing for mode %d, reg: 0x%08x\n",
265 if (drive
->select
.all
== IN_BYTE(IDE_SELECT_REG
))
266 pmac_ide_selectproc(drive
);
270 pmac_ide_get_base(int index
)
272 return pmac_ide
[index
].regbase
;
275 static int ide_majors
[] = { 3, 22, 33, 34, 56, 57 };
278 pmac_find_ide_boot(char *bootdevice
, int n
)
283 * Look through the list of IDE interfaces for this one.
285 for (i
= 0; i
< pmac_ide_count
; ++i
) {
287 if (!pmac_ide
[i
].node
|| !pmac_ide
[i
].node
->full_name
)
289 name
= pmac_ide
[i
].node
->full_name
;
290 if (memcmp(name
, bootdevice
, n
) == 0 && name
[n
] == 0) {
291 /* XXX should cope with the 2nd drive as well... */
292 return MKDEV(ide_majors
[i
], 0);
302 struct device_node
*np
;
304 struct device_node
*atas
;
305 struct device_node
*p
, **pp
, *removables
, **rp
;
310 if (_machine
!= _MACH_Pmac
)
314 p
= find_devices("ATA");
316 p
= find_devices("IDE");
318 p
= find_type_devices("ide");
320 p
= find_type_devices("ata");
321 /* Move removable devices such as the media-bay CDROM
322 on the PB3400 to the end of the list. */
323 for (; p
!= NULL
; p
= p
->next
) {
324 if (p
->parent
&& p
->parent
->type
325 && strcasecmp(p
->parent
->type
, "media-bay") == 0) {
337 for (i
= 0, np
= atas
; i
< MAX_HWIFS
&& np
!= NULL
; np
= np
->next
) {
338 struct device_node
*tp
;
342 * If this node is not under a mac-io or dbdma node,
343 * leave it to the generic PCI driver.
345 for (tp
= np
->parent
; tp
!= 0; tp
= tp
->parent
)
346 if (tp
->type
&& (strcmp(tp
->type
, "mac-io") == 0
347 || strcmp(tp
->type
, "dbdma") == 0))
352 if (np
->n_addrs
== 0) {
353 printk(KERN_WARNING
"ide: no address for device %s\n",
359 * If this slot is taken (e.g. by ide-pci.c) try the next one.
362 && ide_hwifs
[i
].io_ports
[IDE_DATA_OFFSET
] != 0)
367 base
= (unsigned long) ioremap(np
->addrs
[0].address
, 0x200) - _IO_BASE
;
369 /* XXX This is bogus. Should be fixed in the registry by checking
370 the kind of host interrupt controller, a bit like gatwick
373 if (np
->n_intrs
== 0) {
374 printk("ide: no intrs for device %s, using 13\n",
378 irq
= np
->intrs
[0].line
;
380 pmac_ide
[i
].regbase
= base
;
381 pmac_ide
[i
].irq
= irq
;
382 pmac_ide
[i
].node
= np
;
383 if (device_is_compatible(np
, "keylargo-ata")) {
384 if (strcmp(np
->name
, "ata-4") == 0)
385 pmac_ide
[i
].kind
= controller_kl_ata4
;
387 pmac_ide
[i
].kind
= controller_kl_ata3
;
388 } else if (device_is_compatible(np
, "heathrow-ata"))
389 pmac_ide
[i
].kind
= controller_heathrow
;
391 pmac_ide
[i
].kind
= controller_ohare
;
393 bidp
= (int *)get_property(np
, "AAPL,bus-id", NULL
);
394 pmac_ide
[i
].aapl_bus_id
= bidp
? *bidp
: 0;
396 if (np
->parent
&& np
->parent
->name
397 && strcasecmp(np
->parent
->name
, "media-bay") == 0) {
398 media_bay_set_ide_infos(np
->parent
,base
,irq
,i
);
399 } else if (pmac_ide
[i
].kind
== controller_ohare
) {
400 /* The code below is having trouble on some ohare machines
401 * (timing related ?). Until I can put my hand on one of these
402 * units, I keep the old way
404 feature_set(np
, FEATURE_IDE0_enable
);
406 /* This is necessary to enable IDE when net-booting */
407 printk("pmac_ide: enabling IDE bus ID %d\n",
408 pmac_ide
[i
].aapl_bus_id
);
409 switch(pmac_ide
[i
].aapl_bus_id
) {
411 feature_set(np
, FEATURE_IDE0_reset
);
413 feature_set(np
, FEATURE_IDE0_enable
);
415 feature_clear(np
, FEATURE_IDE0_reset
);
418 feature_set(np
, FEATURE_IDE1_reset
);
420 feature_set(np
, FEATURE_IDE1_enable
);
422 feature_clear(np
, FEATURE_IDE1_reset
);
425 /* This one exists only for KL, I don't know
426 about any enable bit */
427 feature_set(np
, FEATURE_IDE2_reset
);
429 feature_clear(np
, FEATURE_IDE2_reset
);
435 hwif
= &ide_hwifs
[i
];
436 pmac_ide_init_hwif_ports(&hwif
->hw
, base
, 0, &hwif
->irq
);
437 memcpy(hwif
->io_ports
, hwif
->hw
.io_ports
, sizeof(hwif
->io_ports
));
438 hwif
->chipset
= ide_pmac
;
439 hwif
->noprobe
= (!hwif
->io_ports
[IDE_DATA_OFFSET
]) ||
440 (check_media_bay_by_base(base
, MB_CD
) == -EINVAL
);
442 #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
443 if (np
->n_addrs
>= 2) {
444 /* has a DBDMA controller channel */
445 pmac_ide_setup_dma(np
, i
);
447 #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
453 mdelay(IDE_WAKEUP_DELAY_MS
);
455 #ifdef CONFIG_PMAC_PBOOK
456 pmu_register_sleep_notifier(&idepmac_sleep_notifier
);
457 #endif /* CONFIG_PMAC_PBOOK */
460 #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
463 pmac_ide_setup_dma(struct device_node
*np
, int ix
)
465 pmac_ide
[ix
].dma_regs
=
466 (volatile struct dbdma_regs
*)ioremap(np
->addrs
[1].address
, 0x200);
469 * Allocate space for the DBDMA commands.
470 * The +2 is +1 for the stop command and +1 to allow for
471 * aligning the start address to a multiple of 16 bytes.
473 pmac_ide
[ix
].dma_table
= (struct dbdma_cmd
*)
474 kmalloc((MAX_DCMDS
+ 2) * sizeof(struct dbdma_cmd
), GFP_KERNEL
);
475 if (pmac_ide
[ix
].dma_table
== 0) {
476 printk(KERN_ERR
"%s: unable to allocate DMA command list\n",
481 ide_hwifs
[ix
].dmaproc
= &pmac_ide_dmaproc
;
482 #ifdef CONFIG_PMAC_IDEDMA_AUTO
483 ide_hwifs
[ix
].autodma
= 1;
488 * pmac_ide_build_dmatable builds the DBDMA command list
489 * for a transfer and sets the DBDMA channel to point to it.
492 pmac_ide_build_dmatable(ide_drive_t
*drive
, int ix
, int wr
)
494 struct dbdma_cmd
*table
, *tstart
;
496 struct request
*rq
= HWGROUP(drive
)->rq
;
497 struct buffer_head
*bh
= rq
->bh
;
498 unsigned int size
, addr
;
499 volatile struct dbdma_regs
*dma
= pmac_ide
[ix
].dma_regs
;
501 table
= tstart
= (struct dbdma_cmd
*) DBDMA_ALIGN(pmac_ide
[ix
].dma_table
);
502 out_le32(&dma
->control
, (RUN
|PAUSE
|FLUSH
|WAKE
|DEAD
) << 16);
503 while (in_le32(&dma
->status
) & RUN
)
508 * Determine addr and size of next buffer area. We assume that
509 * individual virtual buffers are always composed linearly in
510 * physical memory. For example, we assume that any 8kB buffer
511 * is always composed of two adjacent physical 4kB pages rather
512 * than two possibly non-adjacent physical 4kB pages.
514 if (bh
== NULL
) { /* paging requests have (rq->bh == NULL) */
515 addr
= virt_to_bus(rq
->buffer
);
516 size
= rq
->nr_sectors
<< 9;
518 /* group sequential buffers into one large buffer */
519 addr
= virt_to_bus(bh
->b_data
);
521 while ((bh
= bh
->b_reqnext
) != NULL
) {
522 if ((addr
+ size
) != virt_to_bus(bh
->b_data
))
529 * Fill in the next DBDMA command block.
530 * Note that one DBDMA command can transfer
531 * at most 65535 bytes.
534 unsigned int tc
= (size
< 0xfe00)? size
: 0xfe00;
536 if (++count
>= MAX_DCMDS
) {
537 printk("%s: DMA table too small\n",
539 return 0; /* revert to PIO for this request */
541 st_le16(&table
->command
, wr
? OUTPUT_MORE
: INPUT_MORE
);
542 st_le16(&table
->req_count
, tc
);
543 st_le32(&table
->phy_addr
, addr
);
545 table
->xfer_status
= 0;
546 table
->res_count
= 0;
551 } while (bh
!= NULL
);
553 /* convert the last command to an input/output last command */
555 st_le16(&table
[-1].command
, wr
? OUTPUT_LAST
: INPUT_LAST
);
557 printk(KERN_DEBUG
"%s: empty DMA table?\n", drive
->name
);
559 /* add the stop command to the end of the list */
560 memset(table
, 0, sizeof(struct dbdma_cmd
));
561 out_le16(&table
->command
, DBDMA_STOP
);
563 out_le32(&dma
->cmdptr
, virt_to_bus(tstart
));
568 /* This is fun. -DaveM */
569 #define IDE_SETXFER 0x03
570 #define IDE_SETFEATURE 0xef
571 #define IDE_DMA2_ENABLE 0x22
572 #define IDE_DMA1_ENABLE 0x21
573 #define IDE_DMA0_ENABLE 0x20
574 #define IDE_UDMA4_ENABLE 0x44
575 #define IDE_UDMA3_ENABLE 0x43
576 #define IDE_UDMA2_ENABLE 0x42
577 #define IDE_UDMA1_ENABLE 0x41
578 #define IDE_UDMA0_ENABLE 0x40
580 static __inline__
unsigned char
581 dma_bits_to_command(unsigned char bits
)
584 return IDE_DMA2_ENABLE
;
586 return IDE_DMA1_ENABLE
;
587 return IDE_DMA0_ENABLE
;
590 static __inline__
unsigned char
591 udma_bits_to_command(unsigned char bits
)
594 return IDE_UDMA4_ENABLE
;
596 return IDE_UDMA3_ENABLE
;
598 return IDE_UDMA2_ENABLE
;
600 return IDE_UDMA1_ENABLE
;
602 return IDE_UDMA0_ENABLE
;
606 static __inline__
int
607 wait_for_ready(ide_drive_t
*drive
)
609 /* Timeout bumped for some powerbooks */
615 if(!(stat
& BUSY_STAT
)) {
616 if (drive
->ready_stat
== 0)
618 else if((stat
& drive
->ready_stat
) || (stat
& ERR_STAT
))
623 if((stat
& ERR_STAT
) || timeout
<= 0) {
624 if (stat
& ERR_STAT
) {
625 printk("ide_pmace: wait_for_ready, error status: %x\n", stat
);
633 pmac_ide_do_setfeature(ide_drive_t
*drive
, byte command
)
641 old_select
= IN_BYTE(IDE_SELECT_REG
);
642 OUT_BYTE(drive
->select
.all
, IDE_SELECT_REG
);
644 OUT_BYTE(IDE_SETXFER
, IDE_FEATURE_REG
);
645 OUT_BYTE(command
, IDE_NSECTOR_REG
);
646 if(wait_for_ready(drive
)) {
647 printk("pmac_ide_do_setfeature disk not ready before SET_FEATURE!\n");
650 OUT_BYTE(IDE_SETFEATURE
, IDE_COMMAND_REG
);
651 result
= wait_for_ready(drive
);
653 printk("pmac_ide_do_setfeature disk not ready after SET_FEATURE !\n");
655 OUT_BYTE(old_select
, IDE_SELECT_REG
);
656 restore_flags(flags
);
661 /* Calculate MultiWord DMA timings */
663 pmac_ide_mdma_enable(ide_drive_t
*drive
, int idx
)
665 byte bits
= drive
->id
->dma_mword
& 0x07;
666 byte feature
= dma_bits_to_command(bits
);
668 int cycleTime
, accessTime
;
669 int accessTicks
, recTicks
;
670 struct hd_driveid
*id
= drive
->id
;
672 /* Set feature on drive */
673 printk("%s: Enabling MultiWord DMA %d\n", drive
->name
, feature
& 0xf);
674 if (pmac_ide_do_setfeature(drive
, feature
)) {
675 printk("%s: Failed !\n", drive
->name
);
679 if (!drive
->init_speed
)
680 drive
->init_speed
= feature
;
682 /* which drive is it ? */
683 if (drive
->select
.all
& 0x10)
684 timings
= &pmac_ide
[idx
].timings
[1];
686 timings
= &pmac_ide
[idx
].timings
[0];
688 /* Calculate accesstime and cycle time */
689 cycleTime
= mdma_timings
[feature
& 0xf].cycleTime
;
690 accessTime
= mdma_timings
[feature
& 0xf].accessTime
;
691 if ((id
->field_valid
& 2) && (id
->eide_dma_time
))
692 cycleTime
= id
->eide_dma_time
;
693 if ((pmac_ide
[idx
].kind
== controller_ohare
) && (cycleTime
< 150))
696 /* For ata-4 controller, we don't know the calculation */
697 if (pmac_ide
[idx
].kind
== controller_kl_ata4
) {
698 accessTicks
= SYSCLK_TICKS_UDMA(accessTime
* 1000);
699 recTicks
= SYSCLK_TICKS_UDMA(cycleTime
* 1000) - accessTicks
;
700 *timings
= ((*timings
) & 0xffe003ff) |
701 (accessTicks
| (recTicks
<< 5)) << 10;
704 int origAccessTime
= accessTime
;
705 int origCycleTime
= cycleTime
;
707 accessTicks
= SYSCLK_TICKS(accessTime
);
710 accessTime
= accessTicks
* IDE_SYSCLK_NS
;
711 recTicks
= SYSCLK_TICKS(cycleTime
- accessTime
) - 1;
714 cycleTime
= (recTicks
+ 1 + accessTicks
) * IDE_SYSCLK_NS
;
716 /* KeyLargo ata-3 don't support the half-tick stuff */
717 if ((pmac_ide
[idx
].kind
!= controller_kl_ata3
) &&
719 ((accessTime
- IDE_SYSCLK_NS
/2) >= origAccessTime
) &&
720 ((cycleTime
- IDE_SYSCLK_NS
) >= origCycleTime
)) {
724 *timings
= ((*timings
) & 0x7FF) |
725 (accessTicks
| (recTicks
<< 5) | (halfTick
<< 10)) << 11;
727 #ifdef IDE_PMAC_DEBUG
728 printk("ide_pmac: Set MDMA timing for mode %d, reg: 0x%08x\n",
729 feature
& 0xf, *timings
);
731 drive
->current_speed
= feature
;
735 /* Calculate Ultra DMA timings */
737 pmac_ide_udma_enable(ide_drive_t
*drive
, int idx
)
739 byte bits
= drive
->id
->dma_ultra
& 0x1f;
740 byte feature
= udma_bits_to_command(bits
);
741 int cycleTime
, accessTime
;
742 int rdyToPauseTicks
, cycleTicks
;
745 /* Set feature on drive */
746 printk("%s: Enabling Ultra DMA %d\n", drive
->name
, feature
& 0xf);
747 if (pmac_ide_do_setfeature(drive
, feature
)) {
748 printk("%s: Failed !\n", drive
->name
);
752 if (!drive
->init_speed
)
753 drive
->init_speed
= feature
;
755 /* which drive is it ? */
756 if (drive
->select
.all
& 0x10)
757 timings
= &pmac_ide
[idx
].timings
[1];
759 timings
= &pmac_ide
[idx
].timings
[0];
761 cycleTime
= udma_timings
[feature
& 0xf].cycleTime
;
762 accessTime
= udma_timings
[feature
& 0xf].accessTime
;
764 rdyToPauseTicks
= SYSCLK_TICKS_UDMA(accessTime
* 1000);
765 cycleTicks
= SYSCLK_TICKS_UDMA(cycleTime
* 1000);
767 *timings
= ((*timings
) & 0xe00fffff) |
768 ((cycleTicks
<< 1) | (rdyToPauseTicks
<< 5) | 1) << 20;
770 drive
->current_speed
= feature
;
775 pmac_ide_dma_onoff(ide_drive_t
*drive
, int enable
)
778 struct hd_driveid
*id
= drive
->id
;
780 drive
->using_dma
= 0;
782 idx
= pmac_ide_find(drive
);
786 if (drive
->media
== ide_floppy
)
788 if (((id
->capability
& 1) == 0) && !check_drive_lists(drive
, GOOD_DMA_DRIVE
))
790 if (check_drive_lists(drive
, BAD_DMA_DRIVE
))
794 ata4
= (pmac_ide
[idx
].kind
== controller_kl_ata4
);
797 if (ata4
&& (drive
->media
== ide_disk
) &&
798 (id
->field_valid
& 0x0004) && (id
->dma_ultra
& 0x17)) {
799 /* UltraDMA modes. */
800 drive
->using_dma
= pmac_ide_udma_enable(drive
, idx
);
802 if (!drive
->using_dma
&& (id
->dma_mword
& 0x0007)) {
803 /* Normal MultiWord DMA modes. */
804 drive
->using_dma
= pmac_ide_mdma_enable(drive
, idx
);
806 /* Without this, strange things will happen on Keylargo-based
809 OUT_BYTE(0, IDE_CONTROL_REG
);
810 if (drive
->select
.all
== IN_BYTE(IDE_SELECT_REG
))
811 pmac_ide_selectproc(drive
);
817 pmac_ide_tune_chipset(ide_drive_t
*drive
, byte speed
)
822 int pmac_ide_dmaproc(ide_dma_action_t func
, ide_drive_t
*drive
)
824 ide_hwif_t
*hwif
= HWIF(drive
);
826 volatile struct dbdma_regs
*dma
;
828 /* Can we stuff a pointer to our intf structure in config_data
829 * or select_data in hwif ?
831 ix
= pmac_ide_find(drive
);
834 dma
= pmac_ide
[ix
].dma_regs
;
839 case ide_dma_off_quietly
:
840 pmac_ide_dma_onoff(drive
, (func
== ide_dma_on
));
843 printk("IDE-DMA check !\n");
845 pmac_ide_dma_onoff(drive
, 1);
849 if (!pmac_ide_build_dmatable(drive
, ix
, func
==ide_dma_write
))
851 drive
->waiting_for_dma
= 1;
852 if (drive
->media
!= ide_disk
)
854 ide_set_handler(drive
, &ide_dma_intr
, WAIT_CMD
, NULL
);
855 OUT_BYTE(func
==ide_dma_write
? WIN_WRITEDMA
: WIN_READDMA
,
858 out_le32(&dma
->control
, (RUN
<< 16) | RUN
);
861 drive
->waiting_for_dma
= 0;
862 dstat
= in_le32(&dma
->status
);
863 out_le32(&dma
->control
, ((RUN
|WAKE
|DEAD
) << 16));
864 /* verify good dma status */
865 return (dstat
& (RUN
|DEAD
|ACTIVE
)) != RUN
;
866 case ide_dma_test_irq
:
867 return (in_le32(&dma
->status
) & (RUN
|ACTIVE
)) == RUN
;
869 /* Let's implement tose just in case someone wants them */
870 case ide_dma_bad_drive
:
871 case ide_dma_good_drive
:
872 return check_drive_lists(drive
, (func
== ide_dma_good_drive
));
873 case ide_dma_verbose
:
874 return report_drive_dmaing(drive
);
876 case ide_dma_lostirq
:
877 case ide_dma_timeout
:
878 printk("ide_pmac_dmaproc: chipset supported %s func only: %d\n", ide_dmafunc_verbose(func
), func
);
881 printk("ide_pmac_dmaproc: unsupported %s func: %d\n", ide_dmafunc_verbose(func
), func
);
886 #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
888 #ifdef CONFIG_PMAC_PBOOK
889 static void idepmac_sleep_disk(int i
, unsigned long base
)
891 struct device_node
* np
= pmac_ide
[i
].node
;
894 /* FIXME: We only handle the master IDE */
895 if (ide_hwifs
[i
].drives
[0].media
== ide_disk
) {
896 /* Spin down the drive */
897 outb(0xa0, base
+0x60);
898 outb(0x0, base
+0x30);
899 outb(0x0, base
+0x20);
900 outb(0x0, base
+0x40);
901 outb(0x0, base
+0x50);
902 outb(0xe0, base
+0x70);
903 outb(0x2, base
+0x160);
904 for (j
= 0; j
< 10; j
++) {
907 status
= inb(base
+0x70);
908 if (!(status
& BUSY_STAT
) && (status
& DRQ_STAT
))
912 feature_set(np
, FEATURE_IDE0_reset
);
913 feature_clear(np
, FEATURE_IDE0_enable
);
914 switch(pmac_ide
[i
].aapl_bus_id
) {
916 feature_set(np
, FEATURE_IDE0_reset
);
917 feature_clear(np
, FEATURE_IDE0_enable
);
920 feature_set(np
, FEATURE_IDE1_reset
);
921 feature_clear(np
, FEATURE_IDE1_enable
);
924 feature_set(np
, FEATURE_IDE2_reset
);
927 pmac_ide
[i
].timings
[0] = 0;
928 pmac_ide
[i
].timings
[1] = 0;
931 static void idepmac_wake_disk(int i
, unsigned long base
)
933 struct device_node
* np
= pmac_ide
[i
].node
;
936 /* Revive IDE disk and controller */
937 switch(pmac_ide
[i
].aapl_bus_id
) {
939 feature_set(np
, FEATURE_IDE0_reset
);
941 feature_set(np
, FEATURE_IDE0_enable
);
943 feature_clear(np
, FEATURE_IDE0_reset
);
946 feature_set(np
, FEATURE_IDE1_reset
);
948 feature_set(np
, FEATURE_IDE1_enable
);
950 feature_clear(np
, FEATURE_IDE1_reset
);
953 /* This one exists only for KL, I don't know
954 about any enable bit */
955 feature_set(np
, FEATURE_IDE2_reset
);
957 feature_clear(np
, FEATURE_IDE2_reset
);
960 mdelay(IDE_WAKEUP_DELAY_MS
);
963 pmac_ide_selectproc(&ide_hwifs
[i
].drives
[0]);
966 /* Wait up to 10 seconds (enough for recent drives) */
967 for (j
= 0; j
< 100; j
++) {
970 status
= inb(base
+ 0x70);
971 if (!(status
& BUSY_STAT
))
976 /* Here we handle media bay devices */
978 idepmac_wake_bay(int i
, unsigned long base
)
983 pmac_ide_selectproc(&ide_hwifs
[i
].drives
[0]);
987 while ((inb(base
+ 0x70) & BUSY_STAT
) && timeout
) {
993 /* Note: We support only master drives for now. This will have to be
994 * improved if we want to handle sleep on the iMacDV where the CD-ROM
997 static int idepmac_notify_sleep(struct pmu_sleep_notifier
*self
, int when
)
1003 case PBOOK_SLEEP_REQUEST
:
1005 case PBOOK_SLEEP_REJECT
:
1007 case PBOOK_SLEEP_NOW
:
1008 for (i
= 0; i
< pmac_ide_count
; ++i
) {
1009 if ((base
= pmac_ide
[i
].regbase
) == 0)
1011 /* Disable irq during sleep */
1012 disable_irq(pmac_ide
[i
].irq
);
1013 ret
= check_media_bay_by_base(base
, MB_CD
);
1014 if ((ret
== -ENODEV
) && ide_hwifs
[i
].drives
[0].present
)
1015 /* not media bay - put the disk to sleep */
1016 idepmac_sleep_disk(i
, base
);
1020 for (i
= 0; i
< pmac_ide_count
; ++i
) {
1022 if ((base
= pmac_ide
[i
].regbase
) == 0)
1024 hwif
= &ide_hwifs
[i
];
1025 /* We don't handle media bay devices this way */
1026 ret
= check_media_bay_by_base(base
, MB_CD
);
1027 if ((ret
== -ENODEV
) && ide_hwifs
[i
].drives
[0].present
)
1028 idepmac_wake_disk(i
, base
);
1030 idepmac_wake_bay(i
, base
);
1031 enable_irq(pmac_ide
[i
].irq
);
1033 #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
1034 if (hwif
->drives
[0].present
&& hwif
->drives
[0].using_dma
)
1035 pmac_ide_dma_onoff(&hwif
->drives
[0], 1);
1040 return PBOOK_SLEEP_OK
;
1042 #endif /* CONFIG_PMAC_PBOOK */