Import 2.3.9pre5
[davej-history.git] / drivers / block / ide-pmac.c
blob52c658311c85c70f706c49bf1da3b16fb2db9c95
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 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,
66 int *irq)
68 int i, r;
70 if (data_port == 0)
71 return;
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);
76 if (r == -EINVAL)
77 return;
79 for ( i = 0; i < 8 ; ++i )
80 hw->io_ports[i] = data_port + i * 0x10;
81 hw->io_ports[8] = data_port + 0x160;
83 if (irq != NULL) {
84 *irq = 0;
85 for (i = 0; i < MAX_HWIFS; ++i) {
86 if (data_port == pmac_ide_regbase[i]) {
87 *irq = pmac_ide_irq[i];
88 break;
94 void pmac_ide_tuneproc(ide_drive_t *drive, byte pio)
96 ide_pio_data_t d;
98 if (_machine != _MACH_Pmac)
99 return;
100 pio = ide_get_best_pio_mode(drive, pio, 4, &d);
101 switch (pio) {
102 case 4:
103 out_le32((unsigned *)(IDE_DATA_REG + 0x200), 0x211025);
104 break;
105 default:
106 out_le32((unsigned *)(IDE_DATA_REG + 0x200), 0x2f8526);
107 break;
111 __initfunc(void pmac_ide_probe(void))
113 struct device_node *np;
114 int i;
115 struct device_node *atas;
116 struct device_node *p, **pp, *removables, **rp;
117 unsigned long base;
118 int irq;
119 ide_hwif_t *hwif;
121 if (_machine != _MACH_Pmac)
122 return;
123 pp = &atas;
124 rp = &removables;
125 p = find_devices("ATA");
126 if (p == NULL)
127 p = find_devices("IDE");
128 if (p == NULL)
129 p = find_type_devices("ide");
130 if (p == NULL)
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) {
137 *rp = p;
138 rp = &p->next;
139 } else {
140 *pp = p;
141 pp = &p->next;
144 *rp = NULL;
145 *pp = removables;
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",
150 np->full_name);
151 continue;
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
158 fixes in irq.c
160 if (np->n_intrs == 0) {
161 printk("ide: no intrs for device %s, using 13\n",
162 np->full_name);
163 irq = 13;
164 } else {
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);
174 } else
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 */
191 ++i;
193 pmac_ide_count = i;
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",
216 hwif->name);
217 return;
220 hwif->dmaproc = &pmac_ide_dmaproc;
221 #ifdef CONFIG_IDEDMA_PMAC_AUTO
222 hwif->autodma = 1;
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.
230 static int
231 pmac_ide_build_dmatable(ide_drive_t *drive, int wr)
233 ide_hwif_t *hwif = HWIF(drive);
234 struct dbdma_cmd *table, *tstart;
235 int count = 0;
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);
245 do {
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;
256 } else {
257 /* group sequential buffers into one large buffer */
258 addr = virt_to_bus(bh->b_data);
259 size = bh->b_size;
260 while ((bh = bh->b_reqnext) != NULL) {
261 if ((addr + size) != virt_to_bus(bh->b_data))
262 break;
263 size += bh->b_size;
268 * Fill in the next DBDMA command block.
269 * Note that one DBDMA command can transfer
270 * at most 65535 bytes.
272 while (size) {
273 unsigned int tc = (size < 0xfe00)? size: 0xfe00;
275 if (++count >= MAX_DCMDS) {
276 printk("%s: DMA table too small\n",
277 drive->name);
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);
283 table->cmd_dep = 0;
284 table->xfer_status = 0;
285 table->res_count = 0;
286 addr += tc;
287 size -= tc;
288 ++table;
290 } while (bh != NULL);
292 /* convert the last command to an input/output last command */
293 if (count)
294 st_le16(&table[-1].command, wr? OUTPUT_LAST: INPUT_LAST);
295 else
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));
303 return 1;
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;
311 int dstat;
313 switch (func) {
314 case ide_dma_on:
315 /* ide-floppy DMA doesn't work yet... */
316 drive->using_dma = drive->media != ide_floppy;
317 break;
318 case ide_dma_off:
319 printk(KERN_INFO "%s: DMA disabled\n", drive->name);
320 case ide_dma_off_quietly:
321 drive->using_dma = 0;
322 break;
323 case ide_dma_check:
324 /* ide-floppy DMA doesn't work yet... */
325 drive->using_dma = hwif->autodma && drive->media != ide_floppy;
326 break;
327 case ide_dma_read:
328 case ide_dma_write:
329 if (!pmac_ide_build_dmatable(drive, func==ide_dma_write))
330 return 1;
331 drive->waiting_for_dma = 1;
332 if (drive->media != ide_disk)
333 return 0;
334 ide_set_handler(drive, &ide_dma_intr, WAIT_CMD);
335 OUT_BYTE(func==ide_dma_write? WIN_WRITEDMA: WIN_READDMA,
336 IDE_COMMAND_REG);
337 case ide_dma_begin:
338 out_le32(&dma->control, (RUN << 16) | RUN);
339 break;
340 case ide_dma_end:
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;
348 default:
349 printk(KERN_ERR "pmac_ide_dmaproc: bad func %d\n", func);
351 return 0;
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)
359 int i, timeout;
361 switch (code) {
362 case PBOOK_SLEEP:
363 /* do anything here?? */
364 break;
365 case PBOOK_WAKE:
366 /* wait for the controller(s) to become ready */
367 timeout = 5000;
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)
371 continue;
372 while ((inb(base + 0x70) & BUSY_STAT) && timeout) {
373 mdelay(1);
374 --timeout;
377 break;
379 return NOTIFY_DONE;
381 #endif /* CONFIG_PMAC_PBOOK */