1 /*
2 * linux/drivers/ide/ns87415.c Version 1.01 Mar. 18, 2000
3 *
4 * Copyright (C) 1997-1998 Mark Lord <mlord@pobox.com>
5 * Copyright (C) 1998 Eddie C. Dost <ecd@skynet.be>
6 * Copyright (C) 1999-2000 Andre Hedrick <andre@linux-ide.org>
7 *
8 * Inspired by an earlier effort from David S. Miller <davem@redhat.com>
9 */
10
11 #include <linux/config.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/timer.h>
15 #include <linux/mm.h>
16 #include <linux/ioport.h>
17 #include <linux/interrupt.h>
18 #include <linux/blkdev.h>
19 #include <linux/hdreg.h>
20 #include <linux/pci.h>
21 #include <linux/delay.h>
22 #include <linux/ide.h>
23 #include <linux/init.h>
24
25 #include <asm/io.h>
26
27 static unsigned int ns87415_count = 0, ns87415_control[MAX_HWIFS] = { 0 };
28
29 /*
30 * This routine either enables/disables (according to drive->present)
31 * the IRQ associated with the port (HWIF(drive)),
32 * and selects either PIO or DMA handshaking for the next I/O operation.
33 */
34 static void ns87415_prepare_drive (ide_drive_t *drive, unsigned int use_dma)
35 {
36 ide_hwif_t *hwif = HWIF(drive);
37 unsigned int bit, other, new, *old = (unsigned int *) hwif->select_data;
38 struct pci_dev *dev = hwif->pci_dev;
39 unsigned long flags;
40
41 __save_flags(flags); /* local CPU only */
42 __cli(); /* local CPU only */
43 new = *old;
44
45 /* Adjust IRQ enable bit */
46 bit = 1 << (8 + hwif->channel);
47 new = drive->present ? (new & ~bit) : (new | bit);
48
49 /* Select PIO or DMA, DMA may only be selected for one drive/channel. */
50 bit = 1 << (20 + drive->select.b.unit + (hwif->channel << 1));
51 other = 1 << (20 + (1 - drive->select.b.unit) + (hwif->channel << 1));
52 new = use_dma ? ((new & ~other) | bit) : (new & ~bit);
53
54 if (new != *old) {
55 unsigned char stat;
56
57 /*
58 * Don't change DMA engine settings while Write Buffers
59 * are busy.
60 */
61 (void) pci_read_config_byte(dev, 0x43, &stat);
62 while (stat & 0x03) {
63 udelay(1);
64 (void) pci_read_config_byte(dev, 0x43, &stat);
65 }
66
67 *old = new;
68 (void) pci_write_config_dword(dev, 0x40, new);
69
70 /*
71 * And let things settle...
72 */
73 udelay(10);
74 }
75
76 __restore_flags(flags); /* local CPU only */
77 }
78
79 static void ns87415_selectproc (ide_drive_t *drive)
80 {
81 ns87415_prepare_drive (drive, drive->using_dma);
82 }
83
84 #ifdef CONFIG_BLK_DEV_IDEDMA
85 static int ns87415_dmaproc(ide_dma_action_t func, ide_drive_t *drive)
86 {
87 ide_hwif_t *hwif = HWIF(drive);
88 byte dma_stat;
89
90 switch (func) {
91 case ide_dma_end: /* returns 1 on error, 0 otherwise */
92 drive->waiting_for_dma = 0;
93 dma_stat = inb(hwif->dma_base+2);
94 outb(inb(hwif->dma_base)&~1, hwif->dma_base); /* stop DMA */
95 outb(inb(hwif->dma_base)|6, hwif->dma_base); /* from ERRATA: clear the INTR & ERROR bits */
96 ide_destroy_dmatable(drive); /* and free any DMA resources */
97 return (dma_stat & 7) != 4; /* verify good DMA status */
98 case ide_dma_write:
99 case ide_dma_read:
100 ns87415_prepare_drive(drive, 1); /* select DMA xfer */
101 if (!ide_dmaproc(func, drive)) /* use standard DMA stuff */
102 return 0;
103 ns87415_prepare_drive(drive, 0); /* DMA failed: select PIO xfer */
104 return 1;
105 case ide_dma_check:
106 if (drive->media != ide_disk)
107 return ide_dmaproc(ide_dma_off_quietly, drive);
108 /* Fallthrough... */
109 default:
110 return ide_dmaproc(func, drive); /* use standard DMA stuff */
111 }
112 }
113 #endif /* CONFIG_BLK_DEV_IDEDMA */
114
115 void __init ide_init_ns87415 (ide_hwif_t *hwif)
116 {
117 struct pci_dev *dev = hwif->pci_dev;
118 unsigned int ctrl, using_inta;
119 byte progif;
120 #ifdef __sparc_v9__
121 int timeout;
122 byte stat;
123 #endif
124
125 /* Set a good latency timer and cache line size value. */
126 (void) pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
127 #ifdef __sparc_v9__
128 (void) pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 0x10);
129 #endif
130
131 /*
132 * We cannot probe for IRQ: both ports share common IRQ on INTA.
133 * Also, leave IRQ masked during drive probing, to prevent infinite
134 * interrupts from a potentially floating INTA..
135 *
136 * IRQs get unmasked in selectproc when drive is first used.
137 */
138 (void) pci_read_config_dword(dev, 0x40, &ctrl);
139 (void) pci_read_config_byte(dev, 0x09, &progif);
140 /* is irq in "native" mode? */
141 using_inta = progif & (1 << (hwif->channel << 1));
142 if (!using_inta)
143 using_inta = ctrl & (1 << (4 + hwif->channel));
144 if (hwif->mate) {
145 hwif->select_data = hwif->mate->select_data;
146 } else {
147 hwif->select_data = (unsigned long)
148 &ns87415_control[ns87415_count++];
149 ctrl |= (1 << 8) | (1 << 9); /* mask both IRQs */
150 if (using_inta)
151 ctrl &= ~(1 << 6); /* unmask INTA */
152 *((unsigned int *)hwif->select_data) = ctrl;
153 (void) pci_write_config_dword(dev, 0x40, ctrl);
154
155 /*
156 * Set prefetch size to 512 bytes for both ports,
157 * but don't turn on/off prefetching here.
158 */
159 pci_write_config_byte(dev, 0x55, 0xee);
160
161 #ifdef __sparc_v9__
162 /*
163 * XXX: Reset the device, if we don't it will not respond
164 * to SELECT_DRIVE() properly during first probe_hwif().
165 */
166 timeout = 10000;
167 outb(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
168 udelay(10);
169 outb(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
170 do {
171 udelay(50);
172 stat = inb(hwif->io_ports[IDE_STATUS_OFFSET]);
173 if (stat == 0xff)
174 break;
175 } while ((stat & BUSY_STAT) && --timeout);
176 #endif
177 }
178
179 if (hwif->dma_base)
180 outb(0x60, hwif->dma_base + 2);
181
182 if (!using_inta)
183 hwif->irq = hwif->channel ? 15 : 14; /* legacy mode */
184 else if (!hwif->irq && hwif->mate && hwif->mate->irq)
185 hwif->irq = hwif->mate->irq; /* share IRQ with mate */
186
187 #ifdef CONFIG_BLK_DEV_IDEDMA
188 if (hwif->dma_base)
189 hwif->dmaproc = &ns87415_dmaproc;
190 #endif /* CONFIG_BLK_DEV_IDEDMA */
191
192 hwif->selectproc = &ns87415_selectproc;
193 }
194
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.