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 initfunc, 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
,
65 ide_ioreg_t ctrl_port
,
72 /* we check only for -EINVAL meaning that we have found a matching
73 bay but with the wrong device type */
75 r
= check_media_bay_by_base(data_port
, MB_CD
);
79 for ( i
= 0; i
< 8 ; ++i
)
80 hw
->io_ports
[i
] = data_port
+ i
* 0x10;
81 hw
->io_ports
[8] = data_port
+ 0x160;
85 for (i
= 0; i
< MAX_HWIFS
; ++i
) {
86 if (data_port
== pmac_ide_regbase
[i
]) {
87 *irq
= pmac_ide_irq
[i
];
94 void pmac_ide_tuneproc(ide_drive_t
*drive
, byte pio
)
98 if (_machine
!= _MACH_Pmac
)
100 pio
= ide_get_best_pio_mode(drive
, pio
, 4, &d
);
103 out_le32((unsigned *)(IDE_DATA_REG
+ 0x200), 0x211025);
106 out_le32((unsigned *)(IDE_DATA_REG
+ 0x200), 0x2f8526);
111 __initfunc(void pmac_ide_probe(void))
113 struct device_node
*np
;
115 struct device_node
*atas
;
116 struct device_node
*p
, **pp
, *removables
, **rp
;
121 if (_machine
!= _MACH_Pmac
)
125 p
= find_devices("ATA");
127 p
= find_devices("IDE");
129 p
= find_type_devices("ide");
131 p
= find_type_devices("ata");
132 /* Move removable devices such as the media-bay CDROM
133 on the PB3400 to the end of the list. */
134 for (; p
!= NULL
; p
= p
->next
) {
135 if (p
->parent
&& p
->parent
->type
136 && strcasecmp(p
->parent
->type
, "media-bay") == 0) {
147 for (i
= 0, np
= atas
; i
< MAX_HWIFS
&& np
!= NULL
; np
= np
->next
) {
148 if (np
->n_addrs
== 0) {
149 printk(KERN_WARNING
"ide: no address for device %s\n",
154 base
= (unsigned long) ioremap(np
->addrs
[0].address
, 0x200);
156 /* XXX This is bogus. Should be fixed in the registry by checking
157 the kind of host interrupt controller, a bit like gatwick
160 if (np
->n_intrs
== 0) {
161 printk("ide: no intrs for device %s, using 13\n",
165 irq
= np
->intrs
[0].line
;
167 pmac_ide_regbase
[i
] = base
;
168 pmac_ide_irq
[i
] = irq
;
169 pmac_ide_node
[i
] = np
;
171 if (np
->parent
&& np
->parent
->name
172 && strcasecmp(np
->parent
->name
, "media-bay") == 0) {
173 media_bay_set_ide_infos(np
->parent
,base
,irq
,i
);
175 feature_set(np
, FEATURE_IDE_enable
);
177 hwif
= &ide_hwifs
[i
];
178 pmac_ide_init_hwif_ports(&hwif
->hw
, base
, 0, &hwif
->irq
);
179 memcpy(hwif
->io_ports
, hwif
->hw
.io_ports
, sizeof(hwif
->io_ports
));
180 hwif
->chipset
= ide_generic
;
181 hwif
->noprobe
= !hwif
->io_ports
[IDE_DATA_OFFSET
];
182 hwif
->tuneproc
= pmac_ide_tuneproc
;
184 #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
185 if (np
->n_addrs
>= 2) {
186 /* has a DBDMA controller channel */
187 pmac_ide_setup_dma(np
, hwif
);
189 #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
195 #ifdef CONFIG_PMAC_PBOOK
196 notifier_chain_register(&sleep_notifier_list
, &idepmac_sleep_notifier
);
197 #endif /* CONFIG_PMAC_PBOOK */
200 #ifdef CONFIG_BLK_DEV_IDEDMA_PMAC
202 __initfunc(static void
203 pmac_ide_setup_dma(struct device_node
*np
, ide_hwif_t
*hwif
))
205 hwif
->dma_base
= (unsigned long) ioremap(np
->addrs
[1].address
, 0x200);
208 * Allocate space for the DBDMA commands.
209 * The +2 is +1 for the stop command and +1 to allow for
210 * aligning the start address to a multiple of 16 bytes.
212 hwif
->dmatable
= (unsigned long *)
213 kmalloc((MAX_DCMDS
+ 2) * sizeof(struct dbdma_cmd
), GFP_KERNEL
);
214 if (hwif
->dmatable
== 0) {
215 printk(KERN_ERR
"%s: unable to allocate DMA command list\n",
220 hwif
->dmaproc
= &pmac_ide_dmaproc
;
221 #ifdef CONFIG_IDEDMA_PMAC_AUTO
223 #endif /* CONFIG_IDEDMA_PMAC_AUTO */
227 * pmac_ide_build_dmatable builds the DBDMA command list
228 * for a transfer and sets the DBDMA channel to point to it.
231 pmac_ide_build_dmatable(ide_drive_t
*drive
, int wr
)
233 ide_hwif_t
*hwif
= HWIF(drive
);
234 struct dbdma_cmd
*table
, *tstart
;
236 struct request
*rq
= HWGROUP(drive
)->rq
;
237 struct buffer_head
*bh
= rq
->bh
;
238 unsigned int size
, addr
;
239 volatile struct dbdma_regs
*dma
240 = (volatile struct dbdma_regs
*) hwif
->dma_base
;
242 table
= tstart
= (struct dbdma_cmd
*) DBDMA_ALIGN(hwif
->dmatable
);
243 out_le32(&dma
->control
, (RUN
|PAUSE
|FLUSH
|WAKE
|DEAD
) << 16);
247 * Determine addr and size of next buffer area. We assume that
248 * individual virtual buffers are always composed linearly in
249 * physical memory. For example, we assume that any 8kB buffer
250 * is always composed of two adjacent physical 4kB pages rather
251 * than two possibly non-adjacent physical 4kB pages.
253 if (bh
== NULL
) { /* paging requests have (rq->bh == NULL) */
254 addr
= virt_to_bus(rq
->buffer
);
255 size
= rq
->nr_sectors
<< 9;
257 /* group sequential buffers into one large buffer */
258 addr
= virt_to_bus(bh
->b_data
);
260 while ((bh
= bh
->b_reqnext
) != NULL
) {
261 if ((addr
+ size
) != virt_to_bus(bh
->b_data
))
268 * Fill in the next DBDMA command block.
269 * Note that one DBDMA command can transfer
270 * at most 65535 bytes.
273 unsigned int tc
= (size
< 0xfe00)? size
: 0xfe00;
275 if (++count
>= MAX_DCMDS
) {
276 printk("%s: DMA table too small\n",
278 return 0; /* revert to PIO for this request */
280 st_le16(&table
->command
, wr
? OUTPUT_MORE
: INPUT_MORE
);
281 st_le16(&table
->req_count
, tc
);
282 st_le32(&table
->phy_addr
, addr
);
284 table
->xfer_status
= 0;
285 table
->res_count
= 0;
290 } while (bh
!= NULL
);
292 /* convert the last command to an input/output last command */
294 st_le16(&table
[-1].command
, wr
? OUTPUT_LAST
: INPUT_LAST
);
296 printk(KERN_DEBUG
"%s: empty DMA table?\n", drive
->name
);
298 /* add the stop command to the end of the list */
299 memset(table
, 0, sizeof(struct dbdma_cmd
));
300 out_le16(&table
->command
, DBDMA_STOP
);
302 out_le32(&dma
->cmdptr
, virt_to_bus(tstart
));
306 int pmac_ide_dmaproc(ide_dma_action_t func
, ide_drive_t
*drive
)
308 ide_hwif_t
*hwif
= HWIF(drive
);
309 volatile struct dbdma_regs
*dma
310 = (volatile struct dbdma_regs
*) hwif
->dma_base
;
315 /* ide-floppy DMA doesn't work yet... */
316 drive
->using_dma
= drive
->media
!= ide_floppy
;
319 printk(KERN_INFO
"%s: DMA disabled\n", drive
->name
);
320 case ide_dma_off_quietly
:
321 drive
->using_dma
= 0;
324 /* ide-floppy DMA doesn't work yet... */
325 drive
->using_dma
= hwif
->autodma
&& drive
->media
!= ide_floppy
;
329 if (!pmac_ide_build_dmatable(drive
, func
==ide_dma_write
))
331 drive
->waiting_for_dma
= 1;
332 if (drive
->media
!= ide_disk
)
334 ide_set_handler(drive
, &ide_dma_intr
, WAIT_CMD
);
335 OUT_BYTE(func
==ide_dma_write
? WIN_WRITEDMA
: WIN_READDMA
,
338 out_le32(&dma
->control
, (RUN
<< 16) | RUN
);
341 drive
->waiting_for_dma
= 0;
342 dstat
= in_le32(&dma
->status
);
343 out_le32(&dma
->control
, ((RUN
|WAKE
|DEAD
) << 16));
344 /* verify good dma status */
345 return (dstat
& (RUN
|DEAD
|ACTIVE
)) != RUN
;
346 case ide_dma_test_irq
:
347 return (in_le32(&dma
->status
) & (RUN
|ACTIVE
)) == RUN
;
349 printk(KERN_ERR
"pmac_ide_dmaproc: bad func %d\n", func
);
353 #endif /* CONFIG_BLK_DEV_IDEDMA_PMAC */
355 #ifdef CONFIG_PMAC_PBOOK
356 static int idepmac_notify(struct notifier_block
*this,
357 unsigned long code
, void *p
)
363 /* do anything here?? */
366 /* wait for the controller(s) to become ready */
368 for (i
= 0; i
< pmac_ide_count
; ++i
) {
369 unsigned long base
= pmac_ide_regbase
[i
];
370 if (check_media_bay_by_base(base
, MB_CD
) == -EINVAL
)
372 while ((inb(base
+ 0x70) & BUSY_STAT
) && timeout
) {
381 #endif /* CONFIG_PMAC_PBOOK */