Import 2.3.16
[davej-history.git] / drivers / block / ide-pmac.c
blob90fbf9cbc9cad6efbc3a0b024b1638f9b51cc868
1 /*
2 * Support for IDE interfaces on PowerMacs.
3 * These IDE interfaces are memory-mapped and have a DBDMA channel
4 * for doing DMA.
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>
26 #include <asm/prom.h>
27 #include <asm/io.h>
28 #include <asm/dbdma.h>
29 #include <asm/ide.h>
30 #include <asm/mediabay.h>
31 #include <asm/feature.h>
32 #ifdef CONFIG_PMAC_PBOOK
33 #include <asm/adb.h>
34 #include <asm/pmu.h>
35 #endif
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];
41 int pmac_ide_count;
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 = {
55 idepmac_notify
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,
65 int *irq)
67 int i, ix;
69 if (data_port == 0)
70 return;
72 for (ix = 0; ix < MAX_HWIFS; ++ix)
73 if (data_port == pmac_ide_regbase[ix])
74 break;
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;
82 if (irq != 0)
83 *irq = 0;
84 return;
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);
90 if (i == -EINVAL) {
91 hw->io_ports[IDE_DATA_OFFSET] = 0;
92 return;
95 for (i = 0; i < 8; ++i)
96 hw->io_ports[i] = data_port + i * 0x10;
97 hw->io_ports[8] = data_port + 0x160;
99 if (irq != NULL)
100 *irq = pmac_ide_irq[ix];
103 void pmac_ide_tuneproc(ide_drive_t *drive, byte pio)
105 ide_pio_data_t d;
107 if (_machine != _MACH_Pmac)
108 return;
109 pio = ide_get_best_pio_mode(drive, pio, 4, &d);
110 switch (pio) {
111 case 4:
112 out_le32((unsigned *)(IDE_DATA_REG + 0x200 + _IO_BASE), 0x211025);
113 break;
114 default:
115 out_le32((unsigned *)(IDE_DATA_REG + 0x200 + _IO_BASE), 0x2f8526);
116 break;
120 void __init pmac_ide_probe(void)
122 struct device_node *np;
123 int i;
124 struct device_node *atas;
125 struct device_node *p, **pp, *removables, **rp;
126 unsigned long base;
127 int irq;
128 ide_hwif_t *hwif;
130 if (_machine != _MACH_Pmac)
131 return;
132 pp = &atas;
133 rp = &removables;
134 p = find_devices("ATA");
135 if (p == NULL)
136 p = find_devices("IDE");
137 if (p == NULL)
138 p = find_type_devices("ide");
139 if (p == NULL)
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) {
146 *rp = p;
147 rp = &p->next;
148 } else {
149 *pp = p;
150 pp = &p->next;
153 *rp = NULL;
154 *pp = removables;
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))
166 break;
167 if (tp == 0)
168 continue;
170 if (np->n_addrs == 0) {
171 printk(KERN_WARNING "ide: no address for device %s\n",
172 np->full_name);
173 continue;
177 * If this slot is taken (e.g. by ide-pci.c) try the next one.
179 while (i < MAX_HWIFS
180 && ide_hwifs[i].io_ports[IDE_DATA_OFFSET] != 0)
181 ++i;
182 if (i >= MAX_HWIFS)
183 break;
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
189 fixes in irq.c
191 if (np->n_intrs == 0) {
192 printk("ide: no intrs for device %s, using 13\n",
193 np->full_name);
194 irq = 13;
195 } else {
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);
205 } else
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 */
222 ++i;
224 pmac_ide_count = i;
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
233 static void __init
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",
247 hwif->name);
248 return;
251 hwif->dmaproc = &pmac_ide_dmaproc;
252 #ifdef CONFIG_IDEDMA_PMAC_AUTO
253 hwif->autodma = 1;
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.
261 static int
262 pmac_ide_build_dmatable(ide_drive_t *drive, int wr)
264 ide_hwif_t *hwif = HWIF(drive);
265 struct dbdma_cmd *table, *tstart;
266 int count = 0;
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);
276 do {
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;
287 } else {
288 /* group sequential buffers into one large buffer */
289 addr = virt_to_bus(bh->b_data);
290 size = bh->b_size;
291 while ((bh = bh->b_reqnext) != NULL) {
292 if ((addr + size) != virt_to_bus(bh->b_data))
293 break;
294 size += bh->b_size;
299 * Fill in the next DBDMA command block.
300 * Note that one DBDMA command can transfer
301 * at most 65535 bytes.
303 while (size) {
304 unsigned int tc = (size < 0xfe00)? size: 0xfe00;
306 if (++count >= MAX_DCMDS) {
307 printk("%s: DMA table too small\n",
308 drive->name);
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);
314 table->cmd_dep = 0;
315 table->xfer_status = 0;
316 table->res_count = 0;
317 addr += tc;
318 size -= tc;
319 ++table;
321 } while (bh != NULL);
323 /* convert the last command to an input/output last command */
324 if (count)
325 st_le16(&table[-1].command, wr? OUTPUT_LAST: INPUT_LAST);
326 else
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));
334 return 1;
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;
342 int dstat;
344 switch (func) {
345 case ide_dma_on:
346 /* ide-floppy DMA doesn't work yet... */
347 drive->using_dma = drive->media != ide_floppy;
348 break;
349 case ide_dma_off:
350 printk(KERN_INFO "%s: DMA disabled\n", drive->name);
351 case ide_dma_off_quietly:
352 drive->using_dma = 0;
353 break;
354 case ide_dma_check:
355 /* ide-floppy DMA doesn't work yet... */
356 drive->using_dma = hwif->autodma && drive->media != ide_floppy;
357 break;
358 case ide_dma_read:
359 case ide_dma_write:
360 if (!pmac_ide_build_dmatable(drive, func==ide_dma_write))
361 return 1;
362 drive->waiting_for_dma = 1;
363 if (drive->media != ide_disk)
364 return 0;
365 ide_set_handler(drive, &ide_dma_intr, WAIT_CMD);
366 OUT_BYTE(func==ide_dma_write? WIN_WRITEDMA: WIN_READDMA,
367 IDE_COMMAND_REG);
368 case ide_dma_begin:
369 out_le32(&dma->control, (RUN << 16) | RUN);
370 break;
371 case ide_dma_end:
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;
379 default:
380 printk(KERN_ERR "pmac_ide_dmaproc: bad func %d\n", func);
382 return 0;
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)
390 int i, timeout;
392 switch (code) {
393 case PBOOK_SLEEP:
394 /* do anything here?? */
395 break;
396 case PBOOK_WAKE:
397 /* wait for the controller(s) to become ready */
398 timeout = 5000;
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)
402 continue;
403 while ((inb(base + 0x70) & BUSY_STAT) && timeout) {
404 mdelay(1);
405 --timeout;
408 break;
410 return NOTIFY_DONE;
412 #endif /* CONFIG_PMAC_PBOOK */