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 #include "ns87415.h"
29 
30 static unsigned int ns87415_count = 0, ns87415_control[MAX_HWIFS] = { 0 };
31 
32 /*
33  * This routine either enables/disables (according to drive->present)
34  * the IRQ associated with the port (HWIF(drive)),
35  * and selects either PIO or DMA handshaking for the next I/O operation.
36  */
ns87415_prepare_drive(ide_drive_t * drive,unsigned int use_dma)37 static void ns87415_prepare_drive (ide_drive_t *drive, unsigned int use_dma)
38 {
39 	ide_hwif_t *hwif = HWIF(drive);
40 	unsigned int bit, other, new, *old = (unsigned int *) hwif->select_data;
41 	struct pci_dev *dev = hwif->pci_dev;
42 	unsigned long flags;
43 
44 	local_irq_save(flags);
45 	new = *old;
46 
47 	/* Adjust IRQ enable bit */
48 	bit = 1 << (8 + hwif->channel);
49 	new = drive->present ? (new & ~bit) : (new | bit);
50 
51 	/* Select PIO or DMA, DMA may only be selected for one drive/channel. */
52 	bit   = 1 << (20 + drive->select.b.unit       + (hwif->channel << 1));
53 	other = 1 << (20 + (1 - drive->select.b.unit) + (hwif->channel << 1));
54 	new = use_dma ? ((new & ~other) | bit) : (new & ~bit);
55 
56 	if (new != *old) {
57 		unsigned char stat;
58 
59 		/*
60 		 * Don't change DMA engine settings while Write Buffers
61 		 * are busy.
62 		 */
63 		(void) pci_read_config_byte(dev, 0x43, &stat);
64 		while (stat & 0x03) {
65 			udelay(1);
66 			(void) pci_read_config_byte(dev, 0x43, &stat);
67 		}
68 
69 		*old = new;
70 		(void) pci_write_config_dword(dev, 0x40, new);
71 
72 		/*
73 		 * And let things settle...
74 		 */
75 		udelay(10);
76 	}
77 
78 	local_irq_restore(flags);
79 }
80 
ns87415_selectproc(ide_drive_t * drive)81 static void ns87415_selectproc (ide_drive_t *drive)
82 {
83 	ns87415_prepare_drive (drive, drive->using_dma);
84 }
85 
ns87415_ide_dma_end(ide_drive_t * drive)86 static int ns87415_ide_dma_end (ide_drive_t *drive)
87 {
88 	ide_hwif_t      *hwif = HWIF(drive);
89 	u8 dma_stat = 0, dma_cmd = 0;
90 
91 	drive->waiting_for_dma = 0;
92 	dma_stat = hwif->INB(hwif->dma_status);
93 	/* get dma command mode */
94 	dma_cmd = hwif->INB(hwif->dma_command);
95 	/* stop DMA */
96 	hwif->OUTB(dma_cmd & ~1, hwif->dma_command);
97 	/* from ERRATA: clear the INTR & ERROR bits */
98 	dma_cmd = hwif->INB(hwif->dma_command);
99 	hwif->OUTB(dma_cmd|6, hwif->dma_command);
100 	/* and free any DMA resources */
101 	ide_destroy_dmatable(drive);
102 	/* verify good DMA status */
103 	return (dma_stat & 7) != 4;
104 }
105 
ns87415_ide_dma_read(ide_drive_t * drive)106 static int ns87415_ide_dma_read (ide_drive_t *drive)
107 {
108 	/* select DMA xfer */
109 	ns87415_prepare_drive(drive, 1);
110 	if (!(__ide_dma_read(drive)))
111 		return 0;
112 	/* DMA failed: select PIO xfer */
113 	ns87415_prepare_drive(drive, 0);
114 	return 1;
115 }
116 
ns87415_ide_dma_write(ide_drive_t * drive)117 static int ns87415_ide_dma_write (ide_drive_t *drive)
118 {
119 	/* select DMA xfer */
120 	ns87415_prepare_drive(drive, 1);
121 	if (!(__ide_dma_write(drive)))
122 		return 0;
123 	/* DMA failed: select PIO xfer */
124 	ns87415_prepare_drive(drive, 0);
125 	return 1;
126 }
127 
ns87415_ide_dma_check(ide_drive_t * drive)128 static int ns87415_ide_dma_check (ide_drive_t *drive)
129 {
130 	if (drive->media != ide_disk)
131 		return HWIF(drive)->ide_dma_off_quietly(drive);
132 	return __ide_dma_check(drive);
133 }
134 
init_hwif_ns87415(ide_hwif_t * hwif)135 static void __init init_hwif_ns87415 (ide_hwif_t *hwif)
136 {
137 	struct pci_dev *dev = hwif->pci_dev;
138 	unsigned int ctrl, using_inta;
139 	u8 progif;
140 #ifdef __sparc_v9__
141 	int timeout;
142 	u8 stat;
143 #endif
144 
145 	hwif->autodma = 0;
146 	hwif->selectproc = &ns87415_selectproc;
147 
148 	/* Set a good latency timer and cache line size value. */
149 	(void) pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
150 #ifdef __sparc_v9__
151 	(void) pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 0x10);
152 #endif
153 
154 	/*
155 	 * We cannot probe for IRQ: both ports share common IRQ on INTA.
156 	 * Also, leave IRQ masked during drive probing, to prevent infinite
157 	 * interrupts from a potentially floating INTA..
158 	 *
159 	 * IRQs get unmasked in selectproc when drive is first used.
160 	 */
161 	(void) pci_read_config_dword(dev, 0x40, &ctrl);
162 	(void) pci_read_config_byte(dev, 0x09, &progif);
163 	/* is irq in "native" mode? */
164 	using_inta = progif & (1 << (hwif->channel << 1));
165 	if (!using_inta)
166 		using_inta = ctrl & (1 << (4 + hwif->channel));
167 	if (hwif->mate) {
168 		hwif->select_data = hwif->mate->select_data;
169 	} else {
170 		hwif->select_data = (unsigned long)
171 					&ns87415_control[ns87415_count++];
172 		ctrl |= (1 << 8) | (1 << 9);	/* mask both IRQs */
173 		if (using_inta)
174 			ctrl &= ~(1 << 6);	/* unmask INTA */
175 		*((unsigned int *)hwif->select_data) = ctrl;
176 		(void) pci_write_config_dword(dev, 0x40, ctrl);
177 
178 		/*
179 		 * Set prefetch size to 512 bytes for both ports,
180 		 * but don't turn on/off prefetching here.
181 		 */
182 		pci_write_config_byte(dev, 0x55, 0xee);
183 
184 #ifdef __sparc_v9__
185 		/*
186 		 * XXX: Reset the device, if we don't it will not respond
187 		 *      to SELECT_DRIVE() properly during first probe_hwif().
188 		 */
189 		timeout = 10000;
190 		hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
191 		udelay(10);
192 		hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
193 		do {
194 			udelay(50);
195 			stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
196                 	if (stat == 0xff)
197                         	break;
198         	} while ((stat & BUSY_STAT) && --timeout);
199 #endif
200 	}
201 
202 	if (!using_inta)
203 		hwif->irq = hwif->channel ? 15 : 14;	/* legacy mode */
204 	else if (!hwif->irq && hwif->mate && hwif->mate->irq)
205 		hwif->irq = hwif->mate->irq;	/* share IRQ with mate */
206 
207 	if (!hwif->dma_base)
208 		return;
209 
210 	hwif->OUTB(0x60, hwif->dma_status);
211 	hwif->ide_dma_read = &ns87415_ide_dma_read;
212 	hwif->ide_dma_write = &ns87415_ide_dma_write;
213 	hwif->ide_dma_check = &ns87415_ide_dma_check;
214 	hwif->ide_dma_end = &ns87415_ide_dma_end;
215 
216 	if (!noautodma)
217 		hwif->autodma = 1;
218 	hwif->drives[0].autodma = hwif->autodma;
219 	hwif->drives[1].autodma = hwif->autodma;
220 }
221 
init_dma_ns87415(ide_hwif_t * hwif,unsigned long dmabase)222 static void __init init_dma_ns87415 (ide_hwif_t *hwif, unsigned long dmabase)
223 {
224 	ide_setup_dma(hwif, dmabase, 8);
225 }
226 
227 extern void ide_setup_pci_device(struct pci_dev *, ide_pci_device_t *);
228 
ns87415_init_one(struct pci_dev * dev,const struct pci_device_id * id)229 static int __devinit ns87415_init_one(struct pci_dev *dev, const struct pci_device_id *id)
230 {
231 	ide_pci_device_t *d = &ns87415_chipsets[id->driver_data];
232 	if (dev->device != d->device)
233 		BUG();
234 	ide_setup_pci_device(dev, d);
235 	MOD_INC_USE_COUNT;
236 	return 0;
237 }
238 
239 static struct pci_device_id ns87415_pci_tbl[] __devinitdata = {
240 	{ PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87415, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
241 	{ 0, },
242 };
243 
244 static struct pci_driver driver = {
245 	.name		= "NS87415IDE",
246 	.id_table	= ns87415_pci_tbl,
247 	.probe		= ns87415_init_one,
248 };
249 
ns87415_ide_init(void)250 static int ns87415_ide_init(void)
251 {
252 	return ide_pci_register_driver(&driver);
253 }
254 
ns87415_ide_exit(void)255 static void ns87415_ide_exit(void)
256 {
257 	ide_pci_unregister_driver(&driver);
258 }
259 
260 module_init(ns87415_ide_init);
261 module_exit(ns87415_ide_exit);
262 
263 MODULE_AUTHOR("Mark Lord, Eddie Dost, Andre Hedrick");
264 MODULE_DESCRIPTION("PCI driver module for NS87415 IDE");
265 MODULE_LICENSE("GPL");
266 
267 EXPORT_NO_SYMBOLS;
268