patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / ide / pci / ns87415.c
1 /*
2  * linux/drivers/ide/pci/ns87415.c              Version 2.00  Sep. 10, 2002
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/module.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/mm.h>
17 #include <linux/ioport.h>
18 #include <linux/interrupt.h>
19 #include <linux/blkdev.h>
20 #include <linux/hdreg.h>
21 #include <linux/pci.h>
22 #include <linux/delay.h>
23 #include <linux/ide.h>
24 #include <linux/init.h>
25
26 #include <asm/io.h>
27
28 static unsigned int ns87415_count = 0, ns87415_control[MAX_HWIFS] = { 0 };
29
30 /*
31  * This routine either enables/disables (according to drive->present)
32  * the IRQ associated with the port (HWIF(drive)),
33  * and selects either PIO or DMA handshaking for the next I/O operation.
34  */
35 static void ns87415_prepare_drive (ide_drive_t *drive, unsigned int use_dma)
36 {
37         ide_hwif_t *hwif = HWIF(drive);
38         unsigned int bit, other, new, *old = (unsigned int *) hwif->select_data;
39         struct pci_dev *dev = hwif->pci_dev;
40         unsigned long flags;
41
42         local_irq_save(flags);
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         local_irq_restore(flags);
77 }
78
79 static void ns87415_selectproc (ide_drive_t *drive)
80 {
81         ns87415_prepare_drive (drive, drive->using_dma);
82 }
83
84 static int ns87415_ide_dma_end (ide_drive_t *drive)
85 {
86         ide_hwif_t      *hwif = HWIF(drive);
87         u8 dma_stat = 0, dma_cmd = 0;
88
89         drive->waiting_for_dma = 0;
90         dma_stat = hwif->INB(hwif->dma_status);
91         /* get dma command mode */
92         dma_cmd = hwif->INB(hwif->dma_command);
93         /* stop DMA */
94         hwif->OUTB(dma_cmd & ~1, hwif->dma_command);
95         /* from ERRATA: clear the INTR & ERROR bits */
96         dma_cmd = hwif->INB(hwif->dma_command);
97         hwif->OUTB(dma_cmd|6, hwif->dma_command);
98         /* and free any DMA resources */
99         ide_destroy_dmatable(drive);
100         /* verify good DMA status */
101         return (dma_stat & 7) != 4;
102 }
103
104 static int ns87415_ide_dma_read (ide_drive_t *drive)
105 {
106         /* select DMA xfer */
107         ns87415_prepare_drive(drive, 1);
108         if (!(__ide_dma_read(drive)))
109                 return 0;
110         /* DMA failed: select PIO xfer */
111         ns87415_prepare_drive(drive, 0);
112         return 1;
113 }
114
115 static int ns87415_ide_dma_write (ide_drive_t *drive)
116 {
117         /* select DMA xfer */
118         ns87415_prepare_drive(drive, 1);
119         if (!(__ide_dma_write(drive)))
120                 return 0;
121         /* DMA failed: select PIO xfer */
122         ns87415_prepare_drive(drive, 0);
123         return 1;
124 }
125
126 static int ns87415_ide_dma_check (ide_drive_t *drive)
127 {
128         if (drive->media != ide_disk)
129                 return HWIF(drive)->ide_dma_off_quietly(drive);
130         return __ide_dma_check(drive);
131 }
132
133 static void __init init_hwif_ns87415 (ide_hwif_t *hwif)
134 {
135         struct pci_dev *dev = hwif->pci_dev;
136         unsigned int ctrl, using_inta;
137         u8 progif;
138 #ifdef __sparc_v9__
139         int timeout;
140         u8 stat;
141 #endif
142
143         hwif->autodma = 0;
144         hwif->selectproc = &ns87415_selectproc;
145
146         /* Set a good latency timer and cache line size value. */
147         (void) pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
148         /* FIXME: use pci_set_master() to ensure good latency timer value */
149
150         /*
151          * We cannot probe for IRQ: both ports share common IRQ on INTA.
152          * Also, leave IRQ masked during drive probing, to prevent infinite
153          * interrupts from a potentially floating INTA..
154          *
155          * IRQs get unmasked in selectproc when drive is first used.
156          */
157         (void) pci_read_config_dword(dev, 0x40, &ctrl);
158         (void) pci_read_config_byte(dev, 0x09, &progif);
159         /* is irq in "native" mode? */
160         using_inta = progif & (1 << (hwif->channel << 1));
161         if (!using_inta)
162                 using_inta = ctrl & (1 << (4 + hwif->channel));
163         if (hwif->mate) {
164                 hwif->select_data = hwif->mate->select_data;
165         } else {
166                 hwif->select_data = (unsigned long)
167                                         &ns87415_control[ns87415_count++];
168                 ctrl |= (1 << 8) | (1 << 9);    /* mask both IRQs */
169                 if (using_inta)
170                         ctrl &= ~(1 << 6);      /* unmask INTA */
171                 *((unsigned int *)hwif->select_data) = ctrl;
172                 (void) pci_write_config_dword(dev, 0x40, ctrl);
173
174                 /*
175                  * Set prefetch size to 512 bytes for both ports,
176                  * but don't turn on/off prefetching here.
177                  */
178                 pci_write_config_byte(dev, 0x55, 0xee);
179
180 #ifdef __sparc_v9__
181                 /*
182                  * XXX: Reset the device, if we don't it will not respond
183                  *      to SELECT_DRIVE() properly during first probe_hwif().
184                  */
185                 timeout = 10000;
186                 hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
187                 udelay(10);
188                 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
189                 do {
190                         udelay(50);
191                         stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
192                         if (stat == 0xff)
193                                 break;
194                 } while ((stat & BUSY_STAT) && --timeout);
195 #endif
196         }
197
198         if (!using_inta)
199                 hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]);
200         else if (!hwif->irq && hwif->mate && hwif->mate->irq)
201                 hwif->irq = hwif->mate->irq;    /* share IRQ with mate */
202
203         if (!hwif->dma_base)
204                 return;
205
206         hwif->OUTB(0x60, hwif->dma_status);
207         hwif->ide_dma_read = &ns87415_ide_dma_read;
208         hwif->ide_dma_write = &ns87415_ide_dma_write;
209         hwif->ide_dma_check = &ns87415_ide_dma_check;
210         hwif->ide_dma_end = &ns87415_ide_dma_end;
211
212         if (!noautodma)
213                 hwif->autodma = 1;
214         hwif->drives[0].autodma = hwif->autodma;
215         hwif->drives[1].autodma = hwif->autodma;
216 }
217
218 static ide_pci_device_t ns87415_chipset __devinitdata = {
219         .name           = "NS87415",
220         .init_hwif      = init_hwif_ns87415,
221         .channels       = 2,
222         .autodma        = AUTODMA,
223         .bootable       = ON_BOARD,
224 };
225
226 static int __devinit ns87415_init_one(struct pci_dev *dev, const struct pci_device_id *id)
227 {
228         ide_setup_pci_device(dev, &ns87415_chipset);
229         return 0;
230 }
231
232 static struct pci_device_id ns87415_pci_tbl[] = {
233         { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87415, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
234         { 0, },
235 };
236 MODULE_DEVICE_TABLE(pci, ns87415_pci_tbl);
237
238 static struct pci_driver driver = {
239         .name           = "NS87415IDE",
240         .id_table       = ns87415_pci_tbl,
241         .probe          = ns87415_init_one,
242 };
243
244 static int ns87415_ide_init(void)
245 {
246         return ide_pci_register_driver(&driver);
247 }
248
249 module_init(ns87415_ide_init);
250
251 MODULE_AUTHOR("Mark Lord, Eddie Dost, Andre Hedrick");
252 MODULE_DESCRIPTION("PCI driver module for NS87415 IDE");
253 MODULE_LICENSE("GPL");