Merge with 2.3.99-pre1.
[linux-2.6/linux-mips.git] / drivers / ide / icside.c
blobd0e8f832827e164f03813523024c2b492c8ae7c9
1 /*
2 * linux/drivers/block/icside.c
4 * Copyright (c) 1996,1997 Russell King.
6 * Changelog:
7 * 08-Jun-1996 RMK Created
8 * 12-Sep-1997 RMK Added interrupt enable/disable
9 * 17-Apr-1999 RMK Added support for V6 EASI
10 * 22-May-1999 RMK Added support for V6 DMA
13 #include <linux/config.h>
14 #include <linux/string.h>
15 #include <linux/module.h>
16 #include <linux/ioport.h>
17 #include <linux/malloc.h>
18 #include <linux/blkdev.h>
19 #include <linux/errno.h>
20 #include <linux/hdreg.h>
21 #include <linux/ide.h>
23 #include <asm/dma.h>
24 #include <asm/ecard.h>
25 #include <asm/io.h>
27 extern char *ide_xfer_verbose (byte xfer_rate);
30 * Maximum number of interfaces per card
32 #define MAX_IFS 2
34 #define ICS_IDENT_OFFSET 0x8a0
36 #define ICS_ARCIN_V5_INTRSTAT 0x000
37 #define ICS_ARCIN_V5_INTROFFSET 0x001
38 #define ICS_ARCIN_V5_IDEOFFSET 0xa00
39 #define ICS_ARCIN_V5_IDEALTOFFSET 0xae0
40 #define ICS_ARCIN_V5_IDESTEPPING 4
42 #define ICS_ARCIN_V6_IDEOFFSET_1 0x800
43 #define ICS_ARCIN_V6_INTROFFSET_1 0x880
44 #define ICS_ARCIN_V6_INTRSTAT_1 0x8a4
45 #define ICS_ARCIN_V6_IDEALTOFFSET_1 0x8e0
46 #define ICS_ARCIN_V6_IDEOFFSET_2 0xc00
47 #define ICS_ARCIN_V6_INTROFFSET_2 0xc80
48 #define ICS_ARCIN_V6_INTRSTAT_2 0xca4
49 #define ICS_ARCIN_V6_IDEALTOFFSET_2 0xce0
50 #define ICS_ARCIN_V6_IDESTEPPING 4
52 struct cardinfo {
53 unsigned int dataoffset;
54 unsigned int ctrloffset;
55 unsigned int stepping;
58 static struct cardinfo icside_cardinfo_v5 = {
59 ICS_ARCIN_V5_IDEOFFSET,
60 ICS_ARCIN_V5_IDEALTOFFSET,
61 ICS_ARCIN_V5_IDESTEPPING
64 static struct cardinfo icside_cardinfo_v6_1 = {
65 ICS_ARCIN_V6_IDEOFFSET_1,
66 ICS_ARCIN_V6_IDEALTOFFSET_1,
67 ICS_ARCIN_V6_IDESTEPPING
70 static struct cardinfo icside_cardinfo_v6_2 = {
71 ICS_ARCIN_V6_IDEOFFSET_2,
72 ICS_ARCIN_V6_IDEALTOFFSET_2,
73 ICS_ARCIN_V6_IDESTEPPING
76 static const card_ids icside_cids[] = {
77 { MANU_ICS, PROD_ICS_IDE },
78 { MANU_ICS2, PROD_ICS2_IDE },
79 { 0xffff, 0xffff }
82 typedef enum {
83 ics_if_unknown,
84 ics_if_arcin_v5,
85 ics_if_arcin_v6
86 } iftype_t;
88 /* ---------------- Version 5 PCB Support Functions --------------------- */
89 /* Prototype: icside_irqenable_arcin_v5 (struct expansion_card *ec, int irqnr)
90 * Purpose : enable interrupts from card
92 static void icside_irqenable_arcin_v5 (struct expansion_card *ec, int irqnr)
94 unsigned int memc_port = (unsigned int)ec->irq_data;
95 outb (0, memc_port + ICS_ARCIN_V5_INTROFFSET);
98 /* Prototype: icside_irqdisable_arcin_v5 (struct expansion_card *ec, int irqnr)
99 * Purpose : disable interrupts from card
101 static void icside_irqdisable_arcin_v5 (struct expansion_card *ec, int irqnr)
103 unsigned int memc_port = (unsigned int)ec->irq_data;
104 inb (memc_port + ICS_ARCIN_V5_INTROFFSET);
107 static const expansioncard_ops_t icside_ops_arcin_v5 = {
108 icside_irqenable_arcin_v5,
109 icside_irqdisable_arcin_v5,
110 NULL,
111 NULL,
112 NULL,
113 NULL
117 /* ---------------- Version 6 PCB Support Functions --------------------- */
118 /* Prototype: icside_irqenable_arcin_v6 (struct expansion_card *ec, int irqnr)
119 * Purpose : enable interrupts from card
121 static void icside_irqenable_arcin_v6 (struct expansion_card *ec, int irqnr)
123 unsigned int ide_base_port = (unsigned int)ec->irq_data;
125 outb (0, ide_base_port + ICS_ARCIN_V6_INTROFFSET_1);
126 outb (0, ide_base_port + ICS_ARCIN_V6_INTROFFSET_2);
129 /* Prototype: icside_irqdisable_arcin_v6 (struct expansion_card *ec, int irqnr)
130 * Purpose : disable interrupts from card
132 static void icside_irqdisable_arcin_v6 (struct expansion_card *ec, int irqnr)
134 unsigned int ide_base_port = (unsigned int)ec->irq_data;
136 inb (ide_base_port + ICS_ARCIN_V6_INTROFFSET_1);
137 inb (ide_base_port + ICS_ARCIN_V6_INTROFFSET_2);
140 /* Prototype: icside_irqprobe(struct expansion_card *ec)
141 * Purpose : detect an active interrupt from card
143 static int icside_irqpending_arcin_v6(struct expansion_card *ec)
145 unsigned int ide_base_port = (unsigned int)ec->irq_data;
147 return inb(ide_base_port + ICS_ARCIN_V6_INTRSTAT_1) & 1 ||
148 inb(ide_base_port + ICS_ARCIN_V6_INTRSTAT_2) & 1;
151 static const expansioncard_ops_t icside_ops_arcin_v6 = {
152 icside_irqenable_arcin_v6,
153 icside_irqdisable_arcin_v6,
154 icside_irqpending_arcin_v6,
155 NULL,
156 NULL,
157 NULL
160 /* Prototype: icside_identifyif (struct expansion_card *ec)
161 * Purpose : identify IDE interface type
162 * Notes : checks the description string
164 static iftype_t icside_identifyif (struct expansion_card *ec)
166 unsigned int addr;
167 iftype_t iftype;
168 int id = 0;
170 iftype = ics_if_unknown;
172 addr = ecard_address (ec, ECARD_IOC, ECARD_FAST) + ICS_IDENT_OFFSET;
174 id = inb (addr) & 1;
175 id |= (inb (addr + 1) & 1) << 1;
176 id |= (inb (addr + 2) & 1) << 2;
177 id |= (inb (addr + 3) & 1) << 3;
179 switch (id) {
180 case 0: /* A3IN */
181 printk("icside: A3IN unsupported\n");
182 break;
184 case 1: /* A3USER */
185 printk("icside: A3USER unsupported\n");
186 break;
188 case 3: /* ARCIN V6 */
189 printk(KERN_DEBUG "icside: detected ARCIN V6 in slot %d\n", ec->slot_no);
190 iftype = ics_if_arcin_v6;
191 break;
193 case 15:/* ARCIN V5 (no id) */
194 printk(KERN_DEBUG "icside: detected ARCIN V5 in slot %d\n", ec->slot_no);
195 iftype = ics_if_arcin_v5;
196 break;
198 default:/* we don't know - complain very loudly */
199 printk("icside: ***********************************\n");
200 printk("icside: *** UNKNOWN ICS INTERFACE id=%d ***\n", id);
201 printk("icside: ***********************************\n");
202 printk("icside: please report this to linux@arm.linux.org.uk\n");
203 printk("icside: defaulting to ARCIN V5\n");
204 iftype = ics_if_arcin_v5;
205 break;
208 return iftype;
211 #ifdef CONFIG_BLK_DEV_IDEDMA_ICS
213 * SG-DMA support.
215 * Similar to the BM-DMA, but we use the RiscPCs IOMD DMA controllers.
216 * There is only one DMA controller per card, which means that only
217 * one drive can be accessed at one time. NOTE! We do not enforce that
218 * here, but we rely on the main IDE driver spotting that both
219 * interfaces use the same IRQ, which should guarantee this.
221 #define TABLE_SIZE 2048
223 static int
224 icside_build_dmatable(ide_drive_t *drive, int reading)
226 struct request *rq = HWGROUP(drive)->rq;
227 struct buffer_head *bh = rq->bh;
228 unsigned long addr, size;
229 unsigned char *virt_addr;
230 unsigned int count = 0;
231 dmasg_t *sg = (dmasg_t *)HWIF(drive)->dmatable_cpu;
233 do {
234 if (bh == NULL) {
235 /* paging requests have (rq->bh == NULL) */
236 virt_addr = rq->buffer;
237 addr = virt_to_bus (virt_addr);
238 size = rq->nr_sectors << 9;
239 } else {
240 /* group sequential buffers into one large buffer */
241 virt_addr = bh->b_data;
242 addr = virt_to_bus (virt_addr);
243 size = bh->b_size;
244 while ((bh = bh->b_reqnext) != NULL) {
245 if ((addr + size) != virt_to_bus (bh->b_data))
246 break;
247 size += bh->b_size;
251 if (addr & 3) {
252 printk("%s: misaligned DMA buffer\n", drive->name);
253 return 0;
256 if (size) {
257 if (reading)
258 dma_cache_inv((unsigned int)virt_addr, size);
259 else
260 dma_cache_wback((unsigned int)virt_addr, size);
263 sg[count].address = addr;
264 sg[count].length = size;
265 if (++count >= (TABLE_SIZE / sizeof(dmasg_t))) {
266 printk("%s: DMA table too small\n", drive->name);
267 return 0;
269 } while (bh != NULL);
271 if (!count)
272 printk("%s: empty DMA table?\n", drive->name);
274 return count;
277 static int
278 icside_config_if(ide_drive_t *drive, int xfer_mode)
280 int func = ide_dma_off;
282 switch (xfer_mode) {
283 case XFER_MW_DMA_2:
285 * The cycle time is limited to 250ns by the r/w
286 * pulse width (90ns), however we should still
287 * have a maximum burst transfer rate of 8MB/s.
289 drive->drive_data = 250;
290 break;
292 case XFER_MW_DMA_1:
293 drive->drive_data = 250;
294 break;
296 case XFER_MW_DMA_0:
297 drive->drive_data = 480;
298 break;
300 default:
301 drive->drive_data = 0;
302 break;
305 if (drive->drive_data &&
306 ide_config_drive_speed(drive, (byte) xfer_mode) == 0)
307 func = ide_dma_on;
308 else
309 drive->drive_data = 480;
311 printk("%s: %s selected (peak %dMB/s)\n", drive->name,
312 ide_xfer_verbose(xfer_mode), 2000 / drive->drive_data);
314 return func;
317 static int
318 icside_dma_check(ide_drive_t *drive)
320 struct hd_driveid *id = drive->id;
321 ide_hwif_t *hwif = HWIF(drive);
322 int autodma = hwif->autodma;
323 int xfer_mode = XFER_PIO_2;
324 int func = ide_dma_off_quietly;
326 if (!id || !(id->capability & 1) || !autodma)
327 goto out;
330 * Consult the list of known "bad" drives
332 if (ide_dmaproc(ide_dma_bad_drive, drive)) {
333 func = ide_dma_off;
334 goto out;
338 * Enable DMA on any drive that has multiword DMA
340 if (id->field_valid & 2) {
341 if (id->dma_mword & 4) {
342 xfer_mode = XFER_MW_DMA_2;
343 func = ide_dma_on;
344 } else if (id->dma_mword & 2) {
345 xfer_mode = XFER_MW_DMA_1;
346 func = ide_dma_on;
347 } else if (id->dma_mword & 1) {
348 xfer_mode = XFER_MW_DMA_0;
349 func = ide_dma_on;
351 goto out;
355 * Consult the list of known "good" drives
357 if (ide_dmaproc(ide_dma_good_drive, drive)) {
358 if (id->eide_dma_time > 150)
359 goto out;
360 xfer_mode = XFER_MW_DMA_1;
361 func = ide_dma_on;
364 out:
365 func = icside_config_if(drive, xfer_mode);
367 return hwif->dmaproc(func, drive);
370 static int
371 icside_dmaproc(ide_dma_action_t func, ide_drive_t *drive)
373 ide_hwif_t *hwif = HWIF(drive);
374 int count, reading = 0;
376 switch (func) {
377 case ide_dma_check:
378 return icside_dma_check(drive);
380 case ide_dma_read:
381 reading = 1;
382 case ide_dma_write:
383 count = icside_build_dmatable(drive, reading);
384 if (!count)
385 return 1;
386 disable_dma(hwif->hw.dma);
388 /* Route the DMA signals to
389 * to the correct interface.
391 outb(hwif->select_data, hwif->config_data);
393 /* Select the correct timing
394 * for this drive
396 set_dma_speed(hwif->hw.dma, drive->drive_data);
398 set_dma_sg(hwif->hw.dma, (dmasg_t *)hwif->dmatable_cpu, count);
399 set_dma_mode(hwif->hw.dma, reading ? DMA_MODE_READ
400 : DMA_MODE_WRITE);
402 drive->waiting_for_dma = 1;
403 if (drive->media != ide_disk)
404 return 0;
406 ide_set_handler(drive, &ide_dma_intr, WAIT_CMD, NULL);
407 OUT_BYTE(reading ? WIN_READDMA : WIN_WRITEDMA,
408 IDE_COMMAND_REG);
410 case ide_dma_begin:
411 enable_dma(hwif->hw.dma);
412 return 0;
414 case ide_dma_end:
415 drive->waiting_for_dma = 0;
416 disable_dma(hwif->hw.dma);
417 return get_dma_residue(hwif->hw.dma) != 0;
419 case ide_dma_test_irq:
420 return inb((unsigned long)hwif->hw.priv) & 1;
422 default:
423 return ide_dmaproc(func, drive);
427 static unsigned long
428 icside_alloc_dmatable(void)
430 static unsigned long dmatable;
431 static unsigned int leftover;
432 unsigned long table;
434 if (leftover < TABLE_SIZE) {
435 #if PAGE_SIZE == TABLE_SIZE * 2
436 dmatable = __get_free_pages(GFP_KERNEL, 1);
437 leftover = PAGE_SIZE;
438 #else
439 dmatable = kmalloc(TABLE_SIZE, GFP_KERNEL);
440 leftover = TABLE_SIZE;
441 #endif
444 table = dmatable;
445 if (table) {
446 dmatable += TABLE_SIZE;
447 leftover -= TABLE_SIZE;
450 return table;
453 static int
454 icside_setup_dma(ide_hwif_t *hwif, int autodma)
456 unsigned long table = icside_alloc_dmatable();
458 printk(" %s: SG-DMA", hwif->name);
460 if (!table)
461 printk(" -- ERROR, unable to allocate DMA table\n");
462 else {
463 hwif->dmatable_cpu = (void *)table;
464 hwif->dmaproc = icside_dmaproc;
465 hwif->autodma = autodma;
467 printk(" capable%s\n", autodma ?
468 ", auto-enable" : "");
471 return hwif->dmatable_cpu != NULL;
473 #endif
475 static ide_hwif_t *
476 icside_find_hwif(unsigned long dataport)
478 ide_hwif_t *hwif;
479 int index;
481 for (index = 0; index < MAX_HWIFS; ++index) {
482 hwif = &ide_hwifs[index];
483 if (hwif->hw.io_ports[IDE_DATA_OFFSET] == (ide_ioreg_t)dataport)
484 goto found;
487 for (index = 0; index < MAX_HWIFS; ++index) {
488 hwif = &ide_hwifs[index];
489 if (!hwif->hw.io_ports[IDE_DATA_OFFSET])
490 goto found;
493 return NULL;
494 found:
495 return hwif;
498 static ide_hwif_t *
499 icside_setup(unsigned long base, struct cardinfo *info, int irq)
501 unsigned long port = base + info->dataoffset;
502 ide_hwif_t *hwif;
504 hwif = icside_find_hwif(base);
505 if (hwif) {
506 int i;
508 memset(&hwif->hw, 0, sizeof(hw_regs_t));
510 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
511 hwif->hw.io_ports[i] = (ide_ioreg_t)port;
512 port += 1 << info->stepping;
514 hwif->hw.io_ports[IDE_CONTROL_OFFSET] = base + info->ctrloffset;
515 hwif->hw.irq = irq;
516 hwif->hw.dma = NO_DMA;
517 hwif->noprobe = 0;
518 hwif->chipset = ide_acorn;
521 return hwif;
524 static int icside_register_v5(struct expansion_card *ec, int autodma)
526 unsigned long slot_port;
527 ide_hwif_t *hwif;
529 slot_port = ecard_address(ec, ECARD_MEMC, 0);
531 ec->irqaddr = (unsigned char *)ioaddr(slot_port + ICS_ARCIN_V5_INTRSTAT);
532 ec->irqmask = 1;
533 ec->irq_data = (void *)slot_port;
534 ec->ops = (expansioncard_ops_t *)&icside_ops_arcin_v5;
537 * Be on the safe side - disable interrupts
539 inb(slot_port + ICS_ARCIN_V5_INTROFFSET);
541 hwif = icside_setup(slot_port, &icside_cardinfo_v5, ec->irq);
543 return hwif ? 0 : -1;
546 static int icside_register_v6(struct expansion_card *ec, int autodma)
548 unsigned long slot_port, port;
549 ide_hwif_t *hwif, *mate;
550 int sel = 0;
552 slot_port = ecard_address(ec, ECARD_IOC, ECARD_FAST);
553 port = ecard_address(ec, ECARD_EASI, ECARD_FAST);
555 if (port == 0)
556 port = slot_port;
557 else
558 sel = 1 << 5;
560 outb(sel, slot_port);
562 ec->irq_data = (void *)port;
563 ec->ops = (expansioncard_ops_t *)&icside_ops_arcin_v6;
566 * Be on the safe side - disable interrupts
568 inb(port + ICS_ARCIN_V6_INTROFFSET_1);
569 inb(port + ICS_ARCIN_V6_INTROFFSET_2);
571 hwif = icside_setup(port, &icside_cardinfo_v6_1, ec->irq);
572 mate = icside_setup(port, &icside_cardinfo_v6_2, ec->irq);
574 #ifdef CONFIG_BLK_DEV_IDEDMA_ICS
575 if (ec->dma != NO_DMA) {
576 if (request_dma(ec->dma, hwif->name))
577 goto no_dma;
579 if (hwif) {
580 hwif->config_data = slot_port;
581 hwif->select_data = sel;
582 hwif->hw.dma = ec->dma;
583 hwif->hw.priv = (void *)
584 (port + ICS_ARCIN_V6_INTRSTAT_1);
585 hwif->channel = 0;
586 icside_setup_dma(hwif, autodma);
588 if (mate) {
589 mate->config_data = slot_port;
590 mate->select_data = sel | 1;
591 mate->hw.dma = ec->dma;
592 mate->hw.priv = (void *)
593 (port + ICS_ARCIN_V6_INTRSTAT_2);
594 mate->channel = 1;
595 icside_setup_dma(mate, autodma);
598 #endif
600 no_dma:
601 return hwif || mate ? 0 : -1;
604 int icside_init(void)
606 int autodma = 0;
608 #ifdef CONFIG_IDEDMA_ICS_AUTO
609 autodma = 1;
610 #endif
612 ecard_startfind ();
614 do {
615 struct expansion_card *ec;
616 int result;
618 ec = ecard_find(0, icside_cids);
619 if (ec == NULL)
620 break;
622 ecard_claim(ec);
624 switch (icside_identifyif(ec)) {
625 case ics_if_arcin_v5:
626 result = icside_register_v5(ec, autodma);
627 break;
629 case ics_if_arcin_v6:
630 result = icside_register_v6(ec, autodma);
631 break;
633 default:
634 result = -1;
635 break;
638 if (result)
639 ecard_release(ec);
640 } while (1);
642 return 0;