2 * Support for IDE interfaces on PowerMacs.
3 * These IDE interfaces are memory-mapped and have a DBDMA channel
6 * Copyright (C) 1998 Paul Mackerras.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
13 * Some code taken from drivers/block/ide-dma.c:
15 * Copyright (c) 1995-1998 Mark Lord
18 #include <linux/config.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/ide.h>
28 #include <asm/dbdma.h>
30 #include <asm/mediabay.h>
31 #include <asm/feature.h>
32 #ifdef CONFIG_PMAC_PBOOK
36 #include "ide_modes.h"
38 int pmac_ide_ports_known
;
39 ide_ioreg_t pmac_ide_regbase
[MAX_HWIFS
];
40 int pmac_ide_irq
[MAX_HWIFS
];
42 struct device_node
*pmac_ide_node
[MAX_HWIFS
];
44 #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
45 #define MAX_DCMDS 256 /* allow up to 256 DBDMA commands per xfer */
47 static void pmac_ide_setup_dma(struct device_node
*np
, ide_hwif_t
*hwif
);
48 static int pmac_ide_dmaproc(ide_dma_action_t func
, ide_drive_t
*drive
);
49 static int pmac_ide_build_dmatable(ide_drive_t
*drive
, int wr
);
50 #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
52 #ifdef CONFIG_PMAC_PBOOK
53 static int idepmac_notify(struct notifier_block
*, unsigned long, void *);
54 struct notifier_block idepmac_sleep_notifier
= {
57 #endif /* CONFIG_PMAC_PBOOK */
60 * N.B. this can't be an __init, because the media-bay task can
61 * call ide_[un]register at any time.
63 void pmac_ide_init_hwif_ports(hw_regs_t
*hw
,
64 ide_ioreg_t data_port
, ide_ioreg_t ctrl_port
,
72 for (ix
= 0; ix
< MAX_HWIFS
; ++ix
)
73 if (data_port
== pmac_ide_regbase
[ix
])
76 if (ix
>= MAX_HWIFS
) {
77 /* Probably a PCI interface... */
78 for (i
= IDE_DATA_OFFSET
; i
<= IDE_STATUS_OFFSET
; ++i
)
79 hw
->io_ports
[i
] = data_port
+ i
- IDE_DATA_OFFSET
;
80 /* XXX is this right? */
81 hw
->io_ports
[IDE_CONTROL_OFFSET
] = 0;
87 /* we check only for -EINVAL meaning that we have found a matching
88 bay but with the wrong device type */
89 i
= check_media_bay_by_base(data_port
, MB_CD
);
91 hw
->io_ports
[IDE_DATA_OFFSET
] = 0;
95 for (i
= 0; i
< 8; ++i
)
96 hw
->io_ports
[i
] = data_port
+ i
* 0x10;
97 hw
->io_ports
[8] = data_port
+ 0x160;
100 *irq
= pmac_ide_irq
[ix
];
103 void pmac_ide_tuneproc(ide_drive_t
*drive
, byte pio
)
107 if (_machine
!= _MACH_Pmac
)
109 pio
= ide_get_best_pio_mode(drive
, pio
, 4, &d
);
112 out_le32((unsigned *)(IDE_DATA_REG
+ 0x200 + _IO_BASE
), 0x211025);
115 out_le32((unsigned *)(IDE_DATA_REG
+ 0x200 + _IO_BASE
), 0x2f8526);
120 void __init
pmac_ide_probe(void)
122 struct device_node
*np
;
124 struct device_node
*atas
;
125 struct device_node
*p
, **pp
, *removables
, **rp
;
130 if (_machine
!= _MACH_Pmac
)
134 p
= find_devices("ATA");
136 p
= find_devices("IDE");
138 p
= find_type_devices("ide");
140 p
= find_type_devices("ata");
141 /* Move removable devices such as the media-bay CDROM
142 on the PB3400 to the end of the list. */
143 for (; p
!= NULL
; p
= p
->next
) {
144 if (p
->parent
&& p
->parent
->type
145 && strcasecmp(p
->parent
->type
, "media-bay") == 0) {
156 for (i
= 0, np
= atas
; i
< MAX_HWIFS
&& np
!= NULL
; np
= np
->next
) {
157 struct device_node
*tp
;
160 * If this node is not under a mac-io or dbdma node,
161 * leave it to the generic PCI driver.
163 for (tp
= np
->parent
; tp
!= 0; tp
= tp
->parent
)
164 if (tp
->type
&& (strcmp(tp
->type
, "mac-io") == 0
165 || strcmp(tp
->type
, "dbdma") == 0))
170 if (np
->n_addrs
== 0) {
171 printk(KERN_WARNING
"ide: no address for device %s\n",
177 * If this slot is taken (e.g. by ide-pci.c) try the next one.
180 && ide_hwifs
[i
].io_ports
[IDE_DATA_OFFSET
] != 0)
185 base
= (unsigned long) ioremap(np
->addrs
[0].address
, 0x200) - _IO_BASE
;
187 /* XXX This is bogus. Should be fixed in the registry by checking
188 the kind of host interrupt controller, a bit like gatwick
191 if (np
->n_intrs
== 0) {
192 printk("ide: no intrs for device %s, using 13\n",
196 irq
= np
->intrs
[0].line
;
198 pmac_ide_regbase
[i
] = base
;
199 pmac_ide_irq
[i
] = irq
;
200 pmac_ide_node
[i
] = np
;
202 if (np
->parent
&& np
->parent
->name
203 && strcasecmp(np
->parent
->name
, "media-bay") == 0) {
204 media_bay_set_ide_infos(np
->parent
,base
,irq
,i
);
206 feature_set(np
, FEATURE_IDE_enable
);
208 hwif
= &ide_hwifs
[i
];
209 pmac_ide_init_hwif_ports(&hwif
->hw
, base
, 0, &hwif
->irq
);
210 memcpy(hwif
->io_ports
, hwif
->hw
.io_ports
, sizeof(hwif
->io_ports
));
211 hwif
->chipset
= ide_generic
;
212 hwif
->noprobe
= !hwif
->io_ports
[IDE_DATA_OFFSET
];
213 hwif
->tuneproc
= pmac_ide_tuneproc
;
215 #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
216 if (np
->n_addrs
>= 2) {
217 /* has a DBDMA controller channel */
218 pmac_ide_setup_dma(np
, hwif
);
220 #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
226 #ifdef CONFIG_PMAC_PBOOK
227 notifier_chain_register(&sleep_notifier_list
, &idepmac_sleep_notifier
);
228 #endif /* CONFIG_PMAC_PBOOK */
231 #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
234 pmac_ide_setup_dma(struct device_node
*np
, ide_hwif_t
*hwif
)
236 hwif
->dma_base
= (unsigned long) ioremap(np
->addrs
[1].address
, 0x200);
239 * Allocate space for the DBDMA commands.
240 * The +2 is +1 for the stop command and +1 to allow for
241 * aligning the start address to a multiple of 16 bytes.
243 hwif
->dmatable
= (unsigned long *)
244 kmalloc((MAX_DCMDS
+ 2) * sizeof(struct dbdma_cmd
), GFP_KERNEL
);
245 if (hwif
->dmatable
== 0) {
246 printk(KERN_ERR
"%s: unable to allocate DMA command list\n",
251 hwif
->dmaproc
= &pmac_ide_dmaproc
;
252 #ifdef CONFIG_IDEDMA_PMAC_AUTO
254 #endif /* CONFIG_IDEDMA_PMAC_AUTO */
258 * pmac_ide_build_dmatable builds the DBDMA command list
259 * for a transfer and sets the DBDMA channel to point to it.
262 pmac_ide_build_dmatable(ide_drive_t
*drive
, int wr
)
264 ide_hwif_t
*hwif
= HWIF(drive
);
265 struct dbdma_cmd
*table
, *tstart
;
267 struct request
*rq
= HWGROUP(drive
)->rq
;
268 struct buffer_head
*bh
= rq
->bh
;
269 unsigned int size
, addr
;
270 volatile struct dbdma_regs
*dma
271 = (volatile struct dbdma_regs
*) hwif
->dma_base
;
273 table
= tstart
= (struct dbdma_cmd
*) DBDMA_ALIGN(hwif
->dmatable
);
274 out_le32(&dma
->control
, (RUN
|PAUSE
|FLUSH
|WAKE
|DEAD
) << 16);
278 * Determine addr and size of next buffer area. We assume that
279 * individual virtual buffers are always composed linearly in
280 * physical memory. For example, we assume that any 8kB buffer
281 * is always composed of two adjacent physical 4kB pages rather
282 * than two possibly non-adjacent physical 4kB pages.
284 if (bh
== NULL
) { /* paging requests have (rq->bh == NULL) */
285 addr
= virt_to_bus(rq
->buffer
);
286 size
= rq
->nr_sectors
<< 9;
288 /* group sequential buffers into one large buffer */
289 addr
= virt_to_bus(bh
->b_data
);
291 while ((bh
= bh
->b_reqnext
) != NULL
) {
292 if ((addr
+ size
) != virt_to_bus(bh
->b_data
))
299 * Fill in the next DBDMA command block.
300 * Note that one DBDMA command can transfer
301 * at most 65535 bytes.
304 unsigned int tc
= (size
< 0xfe00)? size
: 0xfe00;
306 if (++count
>= MAX_DCMDS
) {
307 printk("%s: DMA table too small\n",
309 return 0; /* revert to PIO for this request */
311 st_le16(&table
->command
, wr
? OUTPUT_MORE
: INPUT_MORE
);
312 st_le16(&table
->req_count
, tc
);
313 st_le32(&table
->phy_addr
, addr
);
315 table
->xfer_status
= 0;
316 table
->res_count
= 0;
321 } while (bh
!= NULL
);
323 /* convert the last command to an input/output last command */
325 st_le16(&table
[-1].command
, wr
? OUTPUT_LAST
: INPUT_LAST
);
327 printk(KERN_DEBUG
"%s: empty DMA table?\n", drive
->name
);
329 /* add the stop command to the end of the list */
330 memset(table
, 0, sizeof(struct dbdma_cmd
));
331 out_le16(&table
->command
, DBDMA_STOP
);
333 out_le32(&dma
->cmdptr
, virt_to_bus(tstart
));
337 int pmac_ide_dmaproc(ide_dma_action_t func
, ide_drive_t
*drive
)
339 ide_hwif_t
*hwif
= HWIF(drive
);
340 volatile struct dbdma_regs
*dma
341 = (volatile struct dbdma_regs
*) hwif
->dma_base
;
346 /* ide-floppy DMA doesn't work yet... */
347 drive
->using_dma
= drive
->media
!= ide_floppy
;
350 printk(KERN_INFO
"%s: DMA disabled\n", drive
->name
);
351 case ide_dma_off_quietly
:
352 drive
->using_dma
= 0;
355 /* ide-floppy DMA doesn't work yet... */
356 drive
->using_dma
= hwif
->autodma
&& drive
->media
!= ide_floppy
;
360 if (!pmac_ide_build_dmatable(drive
, func
==ide_dma_write
))
362 drive
->waiting_for_dma
= 1;
363 if (drive
->media
!= ide_disk
)
365 ide_set_handler(drive
, &ide_dma_intr
, WAIT_CMD
);
366 OUT_BYTE(func
==ide_dma_write
? WIN_WRITEDMA
: WIN_READDMA
,
369 out_le32(&dma
->control
, (RUN
<< 16) | RUN
);
372 drive
->waiting_for_dma
= 0;
373 dstat
= in_le32(&dma
->status
);
374 out_le32(&dma
->control
, ((RUN
|WAKE
|DEAD
) << 16));
375 /* verify good dma status */
376 return (dstat
& (RUN
|DEAD
|ACTIVE
)) != RUN
;
377 case ide_dma_test_irq
:
378 return (in_le32(&dma
->status
) & (RUN
|ACTIVE
)) == RUN
;
380 printk(KERN_ERR
"pmac_ide_dmaproc: bad func %d\n", func
);
384 #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
386 #ifdef CONFIG_PMAC_PBOOK
387 static int idepmac_notify(struct notifier_block
*this,
388 unsigned long code
, void *p
)
394 /* do anything here?? */
397 /* wait for the controller(s) to become ready */
399 for (i
= 0; i
< pmac_ide_count
; ++i
) {
400 unsigned long base
= pmac_ide_regbase
[i
];
401 if (check_media_bay_by_base(base
, MB_CD
) == -EINVAL
)
403 while ((inb(base
+ 0x70) & BUSY_STAT
) && timeout
) {
412 #endif /* CONFIG_PMAC_PBOOK */