Allow NVIDIA's nForce 2 chipset to use proper ATA DMA modes.
[dragonfly.git] / sys / dev / disk / ata / ata-pci.c
1 /*-
2  * Copyright (c) 1998,1999,2000,2001,2002 Søren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/dev/ata/ata-pci.c,v 1.32.2.15 2003/06/06 13:27:05 fjoe Exp $
29  * $DragonFly: src/sys/dev/disk/ata/ata-pci.c,v 1.4 2003/10/27 21:12:29 asmodai Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/disk.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/buf.h>
39 #include <sys/malloc.h>
40 #include <sys/devicestat.h>
41 #include <sys/sysctl.h>
42 #include <machine/stdarg.h>
43 #include <machine/resource.h>
44 #include <machine/bus.h>
45 #include <machine/clock.h>
46 #ifdef __alpha__
47 #include <machine/md_var.h>
48 #endif
49 #include <sys/rman.h>
50 #include <bus/pci/pcivar.h>
51 #include <bus/pci/pcireg.h>
52 #include "ata-all.h"
53
54 /* device structures */
55 struct ata_pci_controller {
56     struct resource *bmio;
57     int bmaddr;
58     struct resource *irq;
59     int irqcnt;
60 };
61
62 /* misc defines */
63 #define IOMASK                  0xfffffffc
64 #define GRANDPARENT(dev)        device_get_parent(device_get_parent(dev))
65 #define ATA_MASTERDEV(dev)      ((pci_get_progif(dev) & 0x80) && \
66                                  (pci_get_progif(dev) & 0x05) != 0x05)
67
68 int
69 ata_find_dev(device_t dev, u_int32_t devid, u_int32_t revid)
70 {
71     device_t *children;
72     int nchildren, i;
73
74     if (device_get_children(device_get_parent(dev), &children, &nchildren))
75         return 0;
76
77     for (i = 0; i < nchildren; i++) {
78         if (pci_get_devid(children[i]) == devid &&
79             pci_get_revid(children[i]) >= revid) {
80             free(children, M_TEMP);
81             return 1;
82         }
83     }
84     free(children, M_TEMP);
85     return 0;
86 }
87
88 static void
89 ata_via_southbridge_fixup(device_t dev)
90 {
91     device_t *children;
92     int nchildren, i;
93
94     if (device_get_children(device_get_parent(dev), &children, &nchildren))
95         return;
96
97     for (i = 0; i < nchildren; i++) {
98         if (pci_get_devid(children[i]) == 0x03051106 ||         /* VIA VT8363 */
99             pci_get_devid(children[i]) == 0x03911106 ||         /* VIA VT8371 */
100             pci_get_devid(children[i]) == 0x31021106 ||         /* VIA VT8662 */
101             pci_get_devid(children[i]) == 0x31121106) {         /* VIA VT8361 */
102             u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
103
104             if ((reg76 & 0xf0) != 0xd0) {
105                 device_printf(dev,
106                 "Correcting VIA config for southbridge data corruption bug\n");
107                 pci_write_config(children[i], 0x75, 0x80, 1);
108                 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
109             }
110             break;
111         }
112     }
113     free(children, M_TEMP);
114 }
115
116 static const char *
117 ata_pci_match(device_t dev)
118 {
119     if (pci_get_class(dev) != PCIC_STORAGE)
120         return NULL;
121
122     switch (pci_get_devid(dev)) {
123     /* supported chipsets */
124     case 0x12308086:
125         return "Intel PIIX ATA controller";
126
127     case 0x70108086:
128         return "Intel PIIX3 ATA controller";
129
130     case 0x71118086:
131     case 0x71998086:
132     case 0x84ca8086:
133         return "Intel PIIX4 ATA33 controller";
134
135     case 0x24218086:
136         return "Intel ICH0 ATA33 controller";
137
138     case 0x24118086:
139     case 0x76018086:
140         return "Intel ICH ATA66 controller";
141
142     case 0x244a8086:
143     case 0x244b8086:
144         return "Intel ICH2 ATA100 controller";
145
146     case 0x248a8086:
147     case 0x248b8086:
148         return "Intel ICH3 ATA100 controller";
149
150     case 0x24cb8086:
151         return "Intel ICH4 ATA100 controller";
152
153     case 0x24db8086:
154         return "Intel ICH5 ATA100 controller";
155
156     case 0x522910b9:
157         if (pci_get_revid(dev) >= 0xc4)
158             return "AcerLabs Aladdin ATA100 controller";
159         else if (pci_get_revid(dev) >= 0xc2)
160             return "AcerLabs Aladdin ATA66 controller";
161         else if (pci_get_revid(dev) >= 0x20)
162             return "AcerLabs Aladdin ATA33 controller";
163         else
164             return "AcerLabs Aladdin ATA controller";
165
166     case 0x05711106: 
167         if (ata_find_dev(dev, 0x05861106, 0x02))
168             return "VIA 82C586 ATA33 controller";
169         if (ata_find_dev(dev, 0x05861106, 0))
170             return "VIA 82C586 ATA controller";
171         if (ata_find_dev(dev, 0x05961106, 0x12))
172             return "VIA 82C596 ATA66 controller";
173         if (ata_find_dev(dev, 0x05961106, 0))
174             return "VIA 82C596 ATA33 controller";
175         if (ata_find_dev(dev, 0x06861106, 0x40))
176             return "VIA 82C686 ATA100 controller";
177         if (ata_find_dev(dev, 0x06861106, 0x10))
178             return "VIA 82C686 ATA66 controller";
179         if (ata_find_dev(dev, 0x06861106, 0))
180             return "VIA 82C686 ATA33 controller";
181         if (ata_find_dev(dev, 0x82311106, 0))
182             return "VIA 8231 ATA100 controller";
183         if (ata_find_dev(dev, 0x30741106, 0) ||
184             ata_find_dev(dev, 0x31091106, 0))
185             return "VIA 8233 ATA100 controller";
186         if (ata_find_dev(dev, 0x31471106, 0))
187             return "VIA 8233 ATA133 controller";
188         if (ata_find_dev(dev, 0x31771106, 0))
189             return "VIA 8235 ATA133 controller";
190         return "VIA Apollo ATA controller";
191
192     case 0x55131039:
193         if (ata_find_dev(dev, 0x06301039, 0x30) ||
194             ata_find_dev(dev, 0x06331039, 0) ||
195             ata_find_dev(dev, 0x06351039, 0) ||
196             ata_find_dev(dev, 0x06401039, 0) ||
197             ata_find_dev(dev, 0x06451039, 0) ||
198             ata_find_dev(dev, 0x06501039, 0) ||
199             ata_find_dev(dev, 0x07301039, 0) ||
200             ata_find_dev(dev, 0x07331039, 0) ||
201             ata_find_dev(dev, 0x07351039, 0) ||
202             ata_find_dev(dev, 0x07401039, 0) ||
203             ata_find_dev(dev, 0x07451039, 0) ||
204             ata_find_dev(dev, 0x07501039, 0))
205             return "SiS 5591 ATA100 controller";
206         else if (ata_find_dev(dev, 0x05301039, 0) ||
207             ata_find_dev(dev, 0x05401039, 0) ||
208             ata_find_dev(dev, 0x06201039, 0) ||
209             ata_find_dev(dev, 0x06301039, 0))
210             return "SiS 5591 ATA66 controller";
211         else
212             return "SiS 5591 ATA33 controller";
213
214     case 0x06801095:
215         return "SiI 0680 ATA133 controller";
216
217     case 0x06491095:
218         return "CMD 649 ATA100 controller";
219
220     case 0x06481095:
221         return "CMD 648 ATA66 controller";
222
223     case 0x06461095:
224         return "CMD 646 ATA controller";
225
226     case 0xc6931080:
227         if (pci_get_subclass(dev) == PCIS_STORAGE_IDE)
228             return "Cypress 82C693 ATA controller";
229         return NULL;
230
231     case 0x01021078:
232         return "Cyrix 5530 ATA33 controller";
233
234     case 0x74091022:
235         return "AMD 756 ATA66 controller";
236
237     case 0x74111022:
238         return "AMD 766 ATA100 controller";
239
240     case 0x74411022:
241         return "AMD 768 ATA100 controller";
242
243     case 0x01bc10de:
244         return "NVIDIA nForce ATA100 controller";
245
246     case 0x006510de:
247         return "NVIDIA nForce ATA133 controller";
248
249     case 0x02111166:
250         return "ServerWorks ROSB4 ATA33 controller";
251
252     case 0x02121166:
253         if (pci_get_revid(dev) >= 0x92)
254             return "ServerWorks CSB5 ATA100 controller";
255         else
256             return "ServerWorks CSB5 ATA66 controller";
257
258     case 0x4d33105a:
259         return "Promise ATA33 controller";
260
261     case 0x0d38105a:
262     case 0x4d38105a:
263         return "Promise ATA66 controller";
264
265     case 0x0d30105a:
266     case 0x4d30105a:
267         return "Promise ATA100 controller";
268
269     case 0x4d68105a:
270     case 0x6268105a: 
271         if (pci_get_devid(GRANDPARENT(dev)) == 0x00221011 &&
272             pci_get_class(GRANDPARENT(dev)) == PCIC_BRIDGE) {
273             static long start = 0, end = 0;
274
275             /* we belive we are on a TX4, now do our (simple) magic */
276             if (pci_get_slot(dev) == 1) {
277                 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
278                 return "Promise TX4 ATA100 controller (channel 0+1)";
279             }
280             else if (pci_get_slot(dev) == 2 && start && end) {
281                 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
282                 start = end = 0;
283                 return "Promise TX4 ATA100 controller (channel 2+3)";
284             }
285             else
286                 start = end = 0;
287         }
288         return "Promise TX2 ATA100 controller";
289
290     case 0x4d69105a:
291     case 0x5275105a:
292     case 0x6269105a: 
293     case 0x7275105a: 
294         return "Promise TX2 ATA133 controller";
295
296     case 0x00041103:
297         switch (pci_get_revid(dev)) {
298         case 0x00:
299         case 0x01:
300             return "HighPoint HPT366 ATA66 controller";
301         case 0x02:
302             return "HighPoint HPT368 ATA66 controller";
303         case 0x03:
304         case 0x04:
305             return "HighPoint HPT370 ATA100 controller";
306         case 0x05:
307             return "HighPoint HPT372 ATA133 controller";
308         }
309         return NULL;
310
311     case 0x00051103:
312         switch (pci_get_revid(dev)) {
313         case 0x01:
314         case 0x02:
315             return "HighPoint HPT372 ATA133 controller";
316         }
317         return NULL;
318
319     case 0x00081103:
320         switch (pci_get_revid(dev)) {
321         case 0x07:
322             return "HighPoint HPT374 ATA133 controller";
323         }
324         return NULL;
325
326     case 0x000116ca:
327         return "Cenatek Rocket Drive controller";
328
329    /* unsupported but known chipsets, generic DMA only */
330     case 0x10001042:
331     case 0x10011042:
332         return "RZ 100? ATA controller !WARNING! buggy chip data loss possible";
333
334     case 0x06401095:
335         return "CMD 640 ATA controller !WARNING! buggy chip data loss possible";
336
337     /* unknown chipsets, try generic DMA if it seems possible */
338     default:
339         if (pci_get_class(dev) == PCIC_STORAGE &&
340             (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
341             return "Generic PCI ATA controller";
342     }
343     return NULL;
344 }
345
346 static int
347 ata_pci_probe(device_t dev)
348 {
349     const char *desc = ata_pci_match(dev);
350     
351     if (desc) {
352         device_set_desc(dev, desc);
353         return 0;
354     } 
355     else
356         return ENXIO;
357 }
358
359 static int
360 ata_pci_add_child(device_t dev, int unit)
361 {
362     device_t child;
363
364     /* check if this is located at one of the std addresses */
365     if (ATA_MASTERDEV(dev)) {
366         if (!(child = device_add_child(dev, "ata", unit)))
367             return ENOMEM;
368     }
369     else {
370         if (!(child = device_add_child(dev, "ata", 2)))
371             return ENOMEM;
372     }
373     return 0;
374 }
375
376 static int
377 ata_pci_attach(device_t dev)
378 {
379     struct ata_pci_controller *controller = device_get_softc(dev);
380     u_int8_t class, subclass;
381     u_int32_t type, cmd;
382     int rid;
383
384     /* set up vendor-specific stuff */
385     type = pci_get_devid(dev);
386     class = pci_get_class(dev);
387     subclass = pci_get_subclass(dev);
388     cmd = pci_read_config(dev, PCIR_COMMAND, 4);
389
390     if (!(cmd & PCIM_CMD_PORTEN)) {
391         device_printf(dev, "ATA channel disabled by BIOS\n");
392         return 0;
393     }
394
395     /* is busmastering supported ? */
396     if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) == 
397         (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) {
398
399         /* is there a valid port range to connect to ? */
400         rid = 0x20;
401         controller->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
402                                               0, ~0, 1, RF_ACTIVE);
403         if (!controller->bmio)
404             device_printf(dev, "Busmastering DMA not configured\n");
405     }
406     else
407         device_printf(dev, "Busmastering DMA not supported\n");
408
409     /* do extra chipset specific setups */
410     switch (type) {
411     case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
412         pci_write_config(dev, 0x53, 
413                          (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
414         break;
415
416     case 0x4d38105a: /* Promise 66 & 100 (before TX2) need the clock changed */
417     case 0x4d30105a:
418     case 0x0d30105a:
419         ATA_OUTB(controller->bmio, 0x11, ATA_INB(controller->bmio, 0x11)|0x0a);
420         /* FALLTHROUGH */
421
422     case 0x4d33105a: /* Promise (before TX2) need burst mode turned on */
423         ATA_OUTB(controller->bmio, 0x1f, ATA_INB(controller->bmio, 0x1f)|0x01);
424         break;
425
426     case 0x00041103:    /* HighPoint HPT366/368/370/372 */
427         if (pci_get_revid(dev) < 2) {   /* HPT 366 */
428             /* turn off interrupt prediction */
429             pci_write_config(dev, 0x51, 
430                              (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
431             break;
432         }
433         if (pci_get_revid(dev) < 5) {   /* HPT368/370 */
434             /* turn off interrupt prediction */
435             pci_write_config(dev, 0x51,
436                              (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
437             pci_write_config(dev, 0x55,
438                              (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
439
440             /* turn on interrupts */
441             pci_write_config(dev, 0x5a,
442                              (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
443
444             /* set clocks etc */
445             pci_write_config(dev, 0x5b, 0x22, 1);
446             break;
447         }
448         /* FALLTHROUGH */
449
450     case 0x00051103:    /* HighPoint HPT372 */
451     case 0x00081103:    /* HighPoint HPT374 */
452         /* turn off interrupt prediction */
453         pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
454         pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
455
456         /* turn on interrupts */
457         pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
458
459         /* set clocks etc */
460         pci_write_config(dev, 0x5b,
461                          (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
462         break;
463
464     case 0x05711106: /* VIA 82C586, '596, '686 default setup */
465         /* prepare for ATA-66 on the 82C686a and 82C596b */
466         if ((ata_find_dev(dev, 0x06861106, 0x10) && 
467              !ata_find_dev(dev, 0x06861106, 0x40)) || 
468             ata_find_dev(dev, 0x05961106, 0x12))
469             pci_write_config(dev, 0x50, 0x030b030b, 4);   
470
471         /* the southbridge might need the data corruption fix */
472         if (ata_find_dev(dev, 0x06861106, 0x40) ||
473             ata_find_dev(dev, 0x82311106, 0x10))
474             ata_via_southbridge_fixup(dev);
475         /* FALLTHROUGH */
476
477     case 0x74091022: /* AMD 756 default setup */
478     case 0x74111022: /* AMD 766 default setup */
479     case 0x74411022: /* AMD 768 default setup */
480     case 0x01bc10de: /* NVIDIA nForce default setup */
481     case 0x006510de: /* NVIDIA nForce2 default setup */
482         /* set prefetch, postwrite */
483         pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
484
485         /* set fifo configuration half'n'half */
486         pci_write_config(dev, 0x43, 
487                          (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
488
489         /* set status register read retry */
490         pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
491
492         /* set DMA read & end-of-sector fifo flush */
493         pci_write_config(dev, 0x46, 
494                          (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
495
496         /* set sector size */
497         pci_write_config(dev, 0x60, DEV_BSIZE, 2);
498         pci_write_config(dev, 0x68, DEV_BSIZE, 2);
499         break;
500
501     case 0x02111166: /* ServerWorks ROSB4 enable UDMA33 */
502         pci_write_config(dev, 0x64,   
503                          (pci_read_config(dev, 0x64, 4) & ~0x00002000) |
504                          0x00004000, 4);
505         break;
506         
507     case 0x02121166: /* ServerWorks CSB5 enable UDMA66/100 depending on rev */
508         pci_write_config(dev, 0x5a,   
509                          (pci_read_config(dev, 0x5a, 1) & ~0x40) |
510                          (pci_get_revid(dev) >= 0x92) ? 0x03 : 0x02, 1);
511         break;
512
513     case 0x06801095: /* SiI 0680 set ATA reference clock speed */
514         if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
515             pci_write_config(dev, 0x8a, 
516                              (pci_read_config(dev, 0x8a, 1) & 0x0F) | 0x10, 1);
517         if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
518             device_printf(dev, "SiI 0680 could not set clock\n");
519         break;
520
521     case 0x06491095:
522     case 0x06481095:
523     case 0x06461095: /* CMD 646 enable interrupts, set DMA read mode */
524         pci_write_config(dev, 0x71, 0x01, 1);
525         break;
526
527     case 0x10001042:   /* RZ 100? known bad, no DMA */
528     case 0x10011042:
529     case 0x06401095:   /* CMD 640 known bad, no DMA */
530         controller->bmio = NULL;
531         device_printf(dev, "Busmastering DMA disabled\n");
532     }
533
534     if (controller->bmio) {
535         controller->bmaddr = rman_get_start(controller->bmio);
536         BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
537                              SYS_RES_IOPORT, rid, controller->bmio);
538         controller->bmio = NULL;
539     }
540
541     /*
542      * the Cypress chip is a mess, it contains two ATA functions, but 
543      * both channels are visible on the first one.
544      * simply ignore the second function for now, as the right
545      * solution (ignoring the second channel on the first function)
546      * doesn't work with the crappy ATA interrupt setup on the alpha.
547      */
548     if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1)
549         return 0;
550
551     ata_pci_add_child(dev, 0);
552
553     if (ATA_MASTERDEV(dev) || pci_read_config(dev, 0x18, 4) & IOMASK)
554         ata_pci_add_child(dev, 1);
555
556     return bus_generic_attach(dev);
557 }
558
559 static int
560 ata_pci_intr(struct ata_channel *ch)
561 {
562     u_int8_t dmastat;
563
564     /* 
565      * since we might share the IRQ with another device, and in some
566      * cases with our twin channel, we only want to process interrupts
567      * that we know this channel generated.
568      */
569     switch (ch->chiptype) {
570     case 0x00041103:    /* HighPoint HPT366/368/370/372 */
571     case 0x00051103:    /* HighPoint HPT372 */
572     case 0x00081103:    /* HighPoint HPT374 */
573         if (((dmastat = ata_dmastatus(ch)) &
574             (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) != ATA_BMSTAT_INTERRUPT)
575             return 1;
576         ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
577         DELAY(1);
578         return 0;
579
580     case 0x06481095:    /* CMD 648 */
581     case 0x06491095:    /* CMD 649 */
582         if (!(pci_read_config(device_get_parent(ch->dev), 0x71, 1) &
583               (ch->unit ? 0x08 : 0x04)))
584             return 1;
585         break;
586
587     case 0x4d33105a:    /* Promise Ultra/Fasttrak 33 */
588     case 0x0d38105a:    /* Promise Fasttrak 66 */
589     case 0x4d38105a:    /* Promise Ultra/Fasttrak 66 */
590     case 0x0d30105a:    /* Promise OEM ATA100 */
591     case 0x4d30105a:    /* Promise Ultra/Fasttrak 100 */
592         if (!(ATA_INL(ch->r_bmio, (ch->unit ? 0x14 : 0x1c)) &
593               (ch->unit ? 0x00004000 : 0x00000400)))
594             return 1;
595         break;
596
597     case 0x4d68105a:    /* Promise TX2 ATA100 */
598     case 0x6268105a:    /* Promise TX2 ATA100 */
599     case 0x4d69105a:    /* Promise TX2 ATA133 */
600     case 0x5275105a:    /* Promise TX2 ATA133 */
601     case 0x6269105a:    /* Promise TX2 ATA133 */
602     case 0x7275105a:    /* Promise TX2 ATA133 */
603         ATA_OUTB(ch->r_bmio, ATA_BMDEVSPEC_0, 0x0b);
604         if (!(ATA_INB(ch->r_bmio, ATA_BMDEVSPEC_1) & 0x20))
605             return 1;
606         break;
607     }
608
609     if (ch->flags & ATA_DMA_ACTIVE) {
610         if (!((dmastat = ata_dmastatus(ch)) & ATA_BMSTAT_INTERRUPT))
611             return 1;
612         ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
613         DELAY(1);
614     }
615     return 0;
616 }
617
618 static int
619 ata_pci_print_child(device_t dev, device_t child)
620 {
621     struct ata_channel *ch = device_get_softc(child);
622     int retval = 0;
623
624     retval += bus_print_child_header(dev, child);
625     retval += printf(": at 0x%lx", rman_get_start(ch->r_io));
626
627     if (ATA_MASTERDEV(dev))
628         retval += printf(" irq %d", 14 + ch->unit);
629     
630     retval += bus_print_child_footer(dev, child);
631
632     return retval;
633 }
634
635 static struct resource *
636 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
637                        u_long start, u_long end, u_long count, u_int flags)
638 {
639     struct ata_pci_controller *controller = device_get_softc(dev);
640     struct resource *res = NULL;
641     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
642     int myrid;
643
644     if (type == SYS_RES_IOPORT) {
645         switch (*rid) {
646         case ATA_IOADDR_RID:
647             if (ATA_MASTERDEV(dev)) {
648                 myrid = 0;
649                 start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
650                 end = start + ATA_IOSIZE - 1;
651                 count = ATA_IOSIZE;
652                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
653                                          SYS_RES_IOPORT, &myrid,
654                                          start, end, count, flags);
655             }
656             else {
657                 myrid = 0x10 + 8 * unit;
658                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
659                                          SYS_RES_IOPORT, &myrid,
660                                          start, end, count, flags);
661             }
662             break;
663
664         case ATA_ALTADDR_RID:
665             if (ATA_MASTERDEV(dev)) {
666                 myrid = 0;
667                 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_ALTOFFSET;
668                 end = start + ATA_ALTIOSIZE - 1;
669                 count = ATA_ALTIOSIZE;
670                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
671                                          SYS_RES_IOPORT, &myrid,
672                                          start, end, count, flags);
673             }
674             else {
675                 myrid = 0x14 + 8 * unit;
676                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
677                                          SYS_RES_IOPORT, &myrid,
678                                          start, end, count, flags);
679                 if (res) {
680                         start = rman_get_start(res) + 2;
681                         end = rman_get_start(res) + ATA_ALTIOSIZE - 1;
682                         count = ATA_ALTIOSIZE;
683                         BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
684                                              SYS_RES_IOPORT, myrid, res);
685                         res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
686                                                  SYS_RES_IOPORT, &myrid,
687                                                  start, end, count, flags);
688                 }
689             }
690             break;
691
692         case ATA_BMADDR_RID:
693             if (controller->bmaddr) {
694                 myrid = 0x20;
695                 start = (unit == 0 ? 
696                          controller->bmaddr : controller->bmaddr+ATA_BMIOSIZE);
697                 end = start + ATA_BMIOSIZE - 1;
698                 count = ATA_BMIOSIZE;
699                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
700                                          SYS_RES_IOPORT, &myrid,
701                                          start, end, count, flags);
702             }
703         }
704         return res;
705     }
706
707     if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
708         if (ATA_MASTERDEV(dev)) {
709 #ifdef __alpha__
710             return alpha_platform_alloc_ide_intr(unit);
711 #else
712             int irq = (unit == 0 ? 14 : 15);
713
714             return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
715                                       SYS_RES_IRQ, rid, irq, irq, 1, flags);
716 #endif
717         }
718         else {
719             /* primary and secondary channels share interrupt, keep track */
720             if (!controller->irq)
721                 controller->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), 
722                                                      dev, SYS_RES_IRQ,
723                                                      rid, 0, ~0, 1, flags);
724             controller->irqcnt++;
725             return controller->irq;
726         }
727     }
728     return 0;
729 }
730
731 static int
732 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
733                          struct resource *r)
734 {
735     struct ata_pci_controller *controller = device_get_softc(dev);
736     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
737
738     if (type == SYS_RES_IOPORT) {
739         switch (rid) {
740         case ATA_IOADDR_RID:
741             if (ATA_MASTERDEV(dev))
742                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
743                                             SYS_RES_IOPORT, 0x0, r);
744             else
745                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
746                                             SYS_RES_IOPORT, 0x10 + 8 * unit, r);
747             break;
748
749         case ATA_ALTADDR_RID:
750             if (ATA_MASTERDEV(dev))
751                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
752                                             SYS_RES_IOPORT, 0x0, r);
753             else
754                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
755                                             SYS_RES_IOPORT, 0x14 + 8 * unit, r);
756             break;
757
758         case ATA_BMADDR_RID:
759             return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
760                                         SYS_RES_IOPORT, 0x20, r);
761         default:
762             return ENOENT;
763         }
764     }
765     if (type == SYS_RES_IRQ) {
766         if (rid != ATA_IRQ_RID)
767             return ENOENT;
768
769         if (ATA_MASTERDEV(dev)) {
770 #ifdef __alpha__
771             return alpha_platform_release_ide_intr(unit, r);
772 #else
773             return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
774                                         SYS_RES_IRQ, rid, r);
775 #endif
776         }
777         else {
778             /* primary and secondary channels share interrupt, keep track */
779             if (--controller->irqcnt)
780                 return 0;
781             controller->irq = NULL;
782             return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
783                                         SYS_RES_IRQ, rid, r);
784         }
785     }
786     return EINVAL;
787 }
788
789 static int
790 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 
791                    int flags, driver_intr_t *intr, void *arg,
792                    void **cookiep)
793 {
794     if (ATA_MASTERDEV(dev)) {
795 #ifdef __alpha__
796         return alpha_platform_setup_ide_intr(irq, intr, arg, cookiep);
797 #else
798         return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
799                               flags, intr, arg, cookiep);
800 #endif
801     }
802     else
803         return BUS_SETUP_INTR(device_get_parent(dev), dev, irq,
804                               flags, intr, arg, cookiep);
805 }
806
807 static int
808 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
809                       void *cookie)
810 {
811     if (ATA_MASTERDEV(dev)) {
812 #ifdef __alpha__
813         return alpha_platform_teardown_ide_intr(irq, cookie);
814 #else
815         return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
816 #endif
817     }
818     else
819         return BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie);
820 }
821
822 static device_method_t ata_pci_methods[] = {
823     /* device interface */
824     DEVMETHOD(device_probe,             ata_pci_probe),
825     DEVMETHOD(device_attach,            ata_pci_attach),
826     DEVMETHOD(device_shutdown,          bus_generic_shutdown),
827     DEVMETHOD(device_suspend,           bus_generic_suspend),
828     DEVMETHOD(device_resume,            bus_generic_resume),
829
830     /* bus methods */
831     DEVMETHOD(bus_print_child,          ata_pci_print_child),
832     DEVMETHOD(bus_alloc_resource,       ata_pci_alloc_resource),
833     DEVMETHOD(bus_release_resource,     ata_pci_release_resource),
834     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
835     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
836     DEVMETHOD(bus_setup_intr,           ata_pci_setup_intr),
837     DEVMETHOD(bus_teardown_intr,        ata_pci_teardown_intr),
838     { 0, 0 }
839 };
840
841 static driver_t ata_pci_driver = {
842     "atapci",
843     ata_pci_methods,
844     sizeof(struct ata_pci_controller),
845 };
846
847 static devclass_t ata_pci_devclass;
848
849 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0);
850
851 static int
852 ata_pcisub_probe(device_t dev)
853 {
854     struct ata_channel *ch = device_get_softc(dev);
855     device_t *children;
856     int count, i;
857
858     /* find channel number on this controller */
859     device_get_children(device_get_parent(dev), &children, &count);
860     for (i = 0; i < count; i++) {
861         if (children[i] == dev)
862             ch->unit = i;
863     }
864     free(children, M_TEMP);
865     ch->chiptype = pci_get_devid(device_get_parent(dev));
866     ch->intr_func = ata_pci_intr;
867     return ata_probe(dev);
868 }
869
870 static device_method_t ata_pcisub_methods[] = {
871     /* device interface */
872     DEVMETHOD(device_probe,     ata_pcisub_probe),
873     DEVMETHOD(device_attach,    ata_attach),
874     DEVMETHOD(device_detach,    ata_detach),
875     DEVMETHOD(device_resume,    ata_resume),
876     { 0, 0 }
877 };
878
879 static driver_t ata_pcisub_driver = {
880     "ata",
881     ata_pcisub_methods,
882     sizeof(struct ata_channel),
883 };
884
885 DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);