Add a couple more PCI IDs for the Intel ICH5 ATA100, ICH6 SATA150,
[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.18 2005/03/08 18:40:28 dillon 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 0x24ca8086:
151     case 0x24cb8086:
152         return "Intel ICH4 ATA100 controller";
153
154     case 0x24d18086:
155         return "Intel ICH5 SATA150 controller";
156
157     case 0x24db8086:
158         return "Intel ICH5 ATA100 controller";
159
160     case 0x26518086:
161         return "Intel ICH6/W SATA150 controller";
162
163     case 0x26528086:
164         return "Intel ICH6R/RW SATA150 controller";
165
166     case 0x266f8086:
167         return "Intel ICH6R/RW ATA100 controller";
168
169     case 0x522910b9:
170         if (pci_get_revid(dev) >= 0xc4)
171             return "AcerLabs Aladdin ATA100 controller";
172         else if (pci_get_revid(dev) >= 0xc2)
173             return "AcerLabs Aladdin ATA66 controller";
174         else if (pci_get_revid(dev) >= 0x20)
175             return "AcerLabs Aladdin ATA33 controller";
176         else
177             return "AcerLabs Aladdin ATA controller";
178
179     case 0x05711106: 
180         if (ata_find_dev(dev, 0x05861106, 0x02))
181             return "VIA 82C586 ATA33 controller";
182         if (ata_find_dev(dev, 0x05861106, 0))
183             return "VIA 82C586 ATA controller";
184         if (ata_find_dev(dev, 0x05961106, 0x12))
185             return "VIA 82C596 ATA66 controller";
186         if (ata_find_dev(dev, 0x05961106, 0))
187             return "VIA 82C596 ATA33 controller";
188         if (ata_find_dev(dev, 0x06861106, 0x40))
189             return "VIA 82C686 ATA100 controller";
190         if (ata_find_dev(dev, 0x06861106, 0x10))
191             return "VIA 82C686 ATA66 controller";
192         if (ata_find_dev(dev, 0x06861106, 0))
193             return "VIA 82C686 ATA33 controller";
194         if (ata_find_dev(dev, 0x82311106, 0))
195             return "VIA 8231 ATA100 controller";
196         if (ata_find_dev(dev, 0x30741106, 0) ||
197             ata_find_dev(dev, 0x31091106, 0))
198             return "VIA 8233 ATA100 controller";
199         if (ata_find_dev(dev, 0x31471106, 0))
200             return "VIA 8233 ATA133 controller";
201         if (ata_find_dev(dev, 0x31771106, 0))
202             return "VIA 8235 ATA133 controller";
203         if (ata_find_dev(dev, 0x31491106, 0))
204             return "VIA 8237 ATA133 controller";
205         return "VIA Apollo ATA controller";
206
207     case 0x31491106:
208          return "VIA 8237 SATA 150 controller";
209
210     case 0x55131039:
211         if (ata_find_dev(dev, 0x07461039, 0))
212             return "SiS 5591 ATA133 controller";
213         if (ata_find_dev(dev, 0x06301039, 0x30) ||
214             ata_find_dev(dev, 0x06331039, 0) ||
215             ata_find_dev(dev, 0x06351039, 0) ||
216             ata_find_dev(dev, 0x06401039, 0) ||
217             ata_find_dev(dev, 0x06451039, 0) ||
218             ata_find_dev(dev, 0x06461039, 0) ||
219             ata_find_dev(dev, 0x06481039, 0) ||
220             ata_find_dev(dev, 0x06501039, 0) ||
221             ata_find_dev(dev, 0x07301039, 0) ||
222             ata_find_dev(dev, 0x07331039, 0) ||
223             ata_find_dev(dev, 0x07351039, 0) ||
224             ata_find_dev(dev, 0x07401039, 0) ||
225             ata_find_dev(dev, 0x07451039, 0) ||
226             ata_find_dev(dev, 0x07501039, 0))
227             return "SiS 5591 ATA100 controller";
228         else if (ata_find_dev(dev, 0x05301039, 0) ||
229             ata_find_dev(dev, 0x05401039, 0) ||
230             ata_find_dev(dev, 0x06201039, 0) ||
231             ata_find_dev(dev, 0x06301039, 0))
232             return "SiS 5591 ATA66 controller";
233         else
234             return "SiS 5591 ATA33 controller";
235
236     case 0x35121095:
237         return "SiI 3512 SATA controller";
238
239     case 0x31121095:
240         return "SiI 3112 SATA controller";
241
242     case 0x31141095:
243         return "SiI 3114 SATA controller";
244
245     case 0x31241095:
246         return "SiI 3124 SATA controller";
247
248     case 0x06801095:
249         return "SiI 0680 ATA133 controller";
250
251     case 0x06491095:
252         return "CMD 649 ATA100 controller";
253
254     case 0x06481095:
255         return "CMD 648 ATA66 controller";
256
257     case 0x06461095:
258         return "CMD 646 ATA controller";
259
260     case 0xc6931080:
261         if (pci_get_subclass(dev) == PCIS_STORAGE_IDE)
262             return "Cypress 82C693 ATA controller";
263         return NULL;
264
265     case 0x01021078:
266         return "Cyrix 5530 ATA33 controller";
267
268     case 0x74091022:
269         return "AMD 756 ATA66 controller";
270
271     case 0x74111022:
272         return "AMD 766 ATA100 controller";
273
274     case 0x74411022:
275         return "AMD 768 ATA100 controller";
276
277     case 0x74691022:
278         return "AMD 8111 UltraATA/133 controller";
279
280     case 0x01bc10de:
281         return "nVIDIA nForce1 ATA100 controller";
282
283     case 0x006510de:
284         return "nVIDIA nForce2 ATA133 controller";
285
286     case 0x00d510de:
287         return "nVIDIA nForce3 ATA133 controller";
288
289     case 0x02111166:
290         return "ServerWorks ROSB4 ATA33 controller";
291
292     case 0x02121166:
293         if (pci_get_revid(dev) >= 0x92)
294             return "ServerWorks CSB5 ATA100 controller";
295         else
296             return "ServerWorks CSB5 ATA66 controller";
297
298     case 0x02131166:
299         return "ServerWorks CSB6 ATA100 controller (channel 0+1)";
300
301     case 0x02171166:
302         return "ServerWorks CSB6 ATA66 controller (channel 2)";
303
304     case 0x4d33105a:
305         return "Promise ATA33 controller";
306
307     case 0x0d38105a:
308     case 0x4d38105a:
309         return "Promise ATA66 controller";
310
311     case 0x0d30105a:
312     case 0x4d30105a:
313         return "Promise ATA100 controller";
314
315     case 0x4d68105a:
316     case 0x6268105a: 
317         if (pci_get_devid(GRANDPARENT(dev)) == 0x00221011 &&
318             pci_get_class(GRANDPARENT(dev)) == PCIC_BRIDGE) {
319             static long start = 0, end = 0;
320
321             /* we belive we are on a TX4, now do our (simple) magic */
322             if (pci_get_slot(dev) == 1) {
323                 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
324                 return "Promise TX4 ATA100 controller (channel 0+1)";
325             }
326             else if (pci_get_slot(dev) == 2 && start && end) {
327                 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
328                 start = end = 0;
329                 return "Promise TX4 ATA100 controller (channel 2+3)";
330             }
331             else
332                 start = end = 0;
333         }
334         return "Promise TX2 ATA100 controller";
335
336     case 0x4d69105a:
337     case 0x5275105a:
338     case 0x6269105a: 
339     case 0x7275105a: 
340         return "Promise TX2 ATA133 controller";
341
342     case 0x00041103:
343         switch (pci_get_revid(dev)) {
344         case 0x00:
345         case 0x01:
346             return "HighPoint HPT366 ATA66 controller";
347         case 0x02:
348             return "HighPoint HPT368 ATA66 controller";
349         case 0x03:
350         case 0x04:
351             return "HighPoint HPT370 ATA100 controller";
352         case 0x05:
353             return "HighPoint HPT372 ATA133 controller";
354         }
355         return NULL;
356
357     case 0x00051103:
358         switch (pci_get_revid(dev)) {
359         case 0x01:
360         case 0x02:
361             return "HighPoint HPT372 ATA133 controller";
362         }
363         return NULL;
364
365     case 0x00081103:
366         switch (pci_get_revid(dev)) {
367         case 0x07:
368             return "HighPoint HPT374 ATA133 controller";
369         }
370         return NULL;
371
372     case 0x000116ca:
373         return "Cenatek Rocket Drive controller";
374
375    /* unsupported but known chipsets, generic DMA only */
376     case 0x10001042:
377     case 0x10011042:
378         return "RZ 100? ATA controller !WARNING! buggy chip data loss possible";
379
380     case 0x06401095:
381         return "CMD 640 ATA controller !WARNING! buggy chip data loss possible";
382
383     /* unknown chipsets, try generic DMA if it seems possible */
384     default:
385         if (pci_get_class(dev) == PCIC_STORAGE &&
386             (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
387             return "Generic PCI ATA controller";
388     }
389     return NULL;
390 }
391
392 static int
393 ata_pci_probe(device_t dev)
394 {
395     const char *desc = ata_pci_match(dev);
396     
397     if (desc) {
398         device_set_desc(dev, desc);
399         return 0;
400     } 
401     else
402         return ENXIO;
403 }
404
405 static int
406 ata_pci_add_child(device_t dev, int unit)
407 {
408     device_t child;
409
410     /* check if this is located at one of the std addresses */
411     if (ATA_MASTERDEV(dev)) {
412         if (!(child = device_add_child(dev, "ata", unit)))
413             return ENOMEM;
414     }
415     else {
416         if (!(child = device_add_child(dev, "ata", 2)))
417             return ENOMEM;
418     }
419     return 0;
420 }
421
422 static int
423 ata_pci_attach(device_t dev)
424 {
425     struct ata_pci_controller *controller = device_get_softc(dev);
426     u_int8_t class, subclass;
427     u_int32_t type, cmd;
428     int rid;
429     int flags;
430
431     /* set up vendor-specific stuff */
432     type = pci_get_devid(dev);
433     class = pci_get_class(dev);
434     subclass = pci_get_subclass(dev);
435     cmd = pci_read_config(dev, PCIR_COMMAND, 4);
436
437     if (!(cmd & PCIM_CMD_PORTEN)) {
438         device_printf(dev, "ATA channel disabled by BIOS\n");
439         return 0;
440     }
441
442     /* is busmastering supported ? */
443     if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) == 
444         (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) {
445
446         /* is there a valid port range to connect to ? */
447         rid = 0x20;
448         controller->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
449                                               0, ~0, 1, RF_ACTIVE);
450         if (!controller->bmio)
451             device_printf(dev, "Busmastering DMA not configured\n");
452     }
453     else
454         device_printf(dev, "Busmastering DMA not supported\n");
455
456     /* do extra chipset specific setups */
457     flags = 0;
458
459     switch (type) {
460     case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
461         pci_write_config(dev, 0x53, 
462                          (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
463         break;
464
465     case 0x4d38105a: /* Promise 66 & 100 (before TX2) need the clock changed */
466     case 0x4d30105a:
467     case 0x0d30105a:
468         ATA_OUTB(controller->bmio, 0x11, ATA_INB(controller->bmio, 0x11)|0x0a);
469         /* FALLTHROUGH */
470
471     case 0x4d33105a: /* Promise (before TX2) need burst mode turned on */
472         ATA_OUTB(controller->bmio, 0x1f, ATA_INB(controller->bmio, 0x1f)|0x01);
473         break;
474
475     case 0x00041103:    /* HighPoint HPT366/368/370/372 */
476         if (pci_get_revid(dev) < 2) {   /* HPT 366 */
477             /* turn off interrupt prediction */
478             pci_write_config(dev, 0x51, 
479                              (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
480             break;
481         }
482         if (pci_get_revid(dev) < 5) {   /* HPT368/370 */
483             /* turn off interrupt prediction */
484             pci_write_config(dev, 0x51,
485                              (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
486             pci_write_config(dev, 0x55,
487                              (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
488
489             /* turn on interrupts */
490             pci_write_config(dev, 0x5a,
491                              (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
492
493             /* set clocks etc */
494             pci_write_config(dev, 0x5b, 0x22, 1);
495             break;
496         }
497         /* FALLTHROUGH */
498
499     case 0x00051103:    /* HighPoint HPT372 */
500     case 0x00081103:    /* HighPoint HPT374 */
501         /* turn off interrupt prediction */
502         pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
503         pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
504
505         /* turn on interrupts */
506         pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
507
508         /* set clocks etc */
509         pci_write_config(dev, 0x5b,
510                          (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
511         break;
512
513     case 0x05711106: /* VIA 82C586, '596, '686 default setup */
514         /* prepare for ATA-66 on the 82C686a and 82C596b */
515         if ((ata_find_dev(dev, 0x06861106, 0x10) && 
516              !ata_find_dev(dev, 0x06861106, 0x40)) || 
517             ata_find_dev(dev, 0x05961106, 0x12))
518             pci_write_config(dev, 0x50, 0x030b030b, 4);   
519
520         /* the southbridge might need the data corruption fix */
521         if (ata_find_dev(dev, 0x06861106, 0x40) ||
522             ata_find_dev(dev, 0x82311106, 0x10))
523             ata_via_southbridge_fixup(dev);
524
525         /* set fifo configuration half'n'half */
526         pci_write_config(dev, 0x43, 
527                          (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
528
529         /* set status register read retry */
530         pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
531
532         /* set DMA read & end-of-sector fifo flush */
533         pci_write_config(dev, 0x46, 
534                          (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
535
536         /* set sector size */
537         pci_write_config(dev, 0x60, DEV_BSIZE, 2);
538         pci_write_config(dev, 0x68, DEV_BSIZE, 2);
539         break;
540     case 0x74111022: /* AMD 766 default setup */
541         flags = 1;      /* bugged */
542         /* fall through */
543     case 0x74091022: /* AMD 756 default setup */
544     case 0x74411022: /* AMD 768 default setup */
545     case 0x746d1022: /* AMD 8111 default setup */
546         if (flags) {
547                 pci_write_config(dev, 0x41,  
548                                 pci_read_config(dev, 0x41, 1) & 0x0f, 1);
549         } else {
550                 pci_write_config(dev, 0x41, 
551                                 pci_read_config(dev, 0x41, 1) | 0xf0, 1);
552         }
553         break;
554     case 0x01bc10de: /* NVIDIA nForce1 default setup */
555     case 0x006510de: /* NVIDIA nForce2 default setup */
556         flags = 1;
557         /* fall through */
558     case 0x00d510de: /* NVIDIA nForce3 default setup */
559         if (flags) {
560                 pci_write_config(dev, 0x51, 
561                                 pci_read_config(dev, 0x51, 1) & 0x0f, 1);  
562         } else {
563                 pci_write_config(dev, 0x51,
564                                 pci_read_config(dev, 0x51, 1) | 0xf0, 1);  
565         }
566         break;
567
568     case 0x02111166: /* ServerWorks ROSB4 enable UDMA33 */
569         pci_write_config(dev, 0x64,   
570                          (pci_read_config(dev, 0x64, 4) & ~0x00002000) |
571                          0x00004000, 4);
572         break;
573         
574     case 0x02121166: /* ServerWorks CSB5 enable UDMA66/100 depending on rev */
575         pci_write_config(dev, 0x5a,   
576                          (pci_read_config(dev, 0x5a, 1) & ~0x40) |
577                          (pci_get_revid(dev) >= 0x92) ? 0x03 : 0x02, 1);
578         break;
579
580     case 0x06801095: /* SiI 0680 set ATA reference clock speed */
581         if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
582             pci_write_config(dev, 0x8a, 
583                              (pci_read_config(dev, 0x8a, 1) & 0x0F) | 0x10, 1);
584         if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
585             device_printf(dev, "SiI 0680 could not set clock\n");
586         break;
587
588     case 0x06491095:
589     case 0x06481095:
590     case 0x06461095: /* CMD 646 enable interrupts, set DMA read mode */
591         pci_write_config(dev, 0x71, 0x01, 1);
592         break;
593
594     case 0x10001042:   /* RZ 100? known bad, no DMA */
595     case 0x10011042:
596     case 0x06401095:   /* CMD 640 known bad, no DMA */
597         controller->bmio = NULL;
598         device_printf(dev, "Busmastering DMA disabled\n");
599     }
600
601     if (controller->bmio) {
602         controller->bmaddr = rman_get_start(controller->bmio);
603         BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
604                              SYS_RES_IOPORT, rid, controller->bmio);
605         controller->bmio = NULL;
606     }
607
608     /*
609      * the Cypress chip is a mess, it contains two ATA functions, but 
610      * both channels are visible on the first one.
611      * simply ignore the second function for now, as the right
612      * solution (ignoring the second channel on the first function)
613      * doesn't work with the crappy ATA interrupt setup on the alpha.
614      */
615     if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1)
616         return 0;
617
618     ata_pci_add_child(dev, 0);
619
620     if (ATA_MASTERDEV(dev) || pci_read_config(dev, 0x18, 4) & IOMASK)
621         ata_pci_add_child(dev, 1);
622
623     return bus_generic_attach(dev);
624 }
625
626 static int
627 ata_pci_intr(struct ata_channel *ch)
628 {
629     u_int8_t dmastat;
630
631     /* 
632      * since we might share the IRQ with another device, and in some
633      * cases with our twin channel, we only want to process interrupts
634      * that we know this channel generated.
635      */
636     switch (ch->chiptype) {
637     case 0x00041103:    /* HighPoint HPT366/368/370/372 */
638     case 0x00051103:    /* HighPoint HPT372 */
639     case 0x00081103:    /* HighPoint HPT374 */
640         if (((dmastat = ata_dmastatus(ch)) &
641             (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) != ATA_BMSTAT_INTERRUPT)
642             return 1;
643         ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
644         DELAY(1);
645         return 0;
646
647     case 0x06481095:    /* CMD 648 */
648     case 0x06491095:    /* CMD 649 */
649         if (!(pci_read_config(device_get_parent(ch->dev), 0x71, 1) &
650               (ch->unit ? 0x08 : 0x04)))
651             return 1;
652 #if !defined(NO_ATANG)
653         pci_write_config(device_get_parent(ch->dev), 0x71,
654                 pci_read_config(device_get_parent(ch->dev), 0x71, 1) &
655                 ~(ch->unit ? 0x04 : 0x08), 1);
656         break;
657 #endif
658
659     case 0x06801095:   /* SiI 680 */
660         if (!(pci_read_config(device_get_parent(ch->dev),
661                                 (ch->unit ? 0xb1 : 0xa1), 1) & 0x08))
662             return 1;
663         break;
664
665     case 0x4d33105a:    /* Promise Ultra/Fasttrak 33 */
666     case 0x0d38105a:    /* Promise Fasttrak 66 */
667     case 0x4d38105a:    /* Promise Ultra/Fasttrak 66 */
668     case 0x0d30105a:    /* Promise OEM ATA100 */
669     case 0x4d30105a:    /* Promise Ultra/Fasttrak 100 */
670         if (!(ATA_INL(ch->r_bmio, (ch->unit ? 0x14 : 0x1c)) &
671               (ch->unit ? 0x00004000 : 0x00000400)))
672             return 1;
673         break;
674
675     case 0x4d68105a:    /* Promise TX2 ATA100 */
676     case 0x6268105a:    /* Promise TX2 ATA100 */
677     case 0x4d69105a:    /* Promise TX2 ATA133 */
678     case 0x5275105a:    /* Promise TX2 ATA133 */
679     case 0x6269105a:    /* Promise TX2 ATA133 */
680     case 0x7275105a:    /* Promise TX2 ATA133 */
681         ATA_OUTB(ch->r_bmio, ATA_BMDEVSPEC_0, 0x0b);
682         if (!(ATA_INB(ch->r_bmio, ATA_BMDEVSPEC_1) & 0x20))
683             return 1;
684         break;
685
686     case 0x24d18086:   /* Intel ICH5 SATA150 */
687     case 0x24db8086:   /* Intel ICH5 ATA100 */
688     case 0x26518086:   /* Intel ICH6 SATA150 */
689     case 0x26528086:   /* Intel ICH6R SATA150 */
690         dmastat = ATA_INB(ch->r_bmio, ATA_BMSTAT_PORT);
691         if ((dmastat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
692                 ATA_BMSTAT_INTERRUPT)
693             return 1;
694         ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, dmastat &
695             ~(ATA_BMSTAT_DMA_SIMPLEX | ATA_BMSTAT_ERROR));
696         DELAY(1);
697         return 0;
698
699     }
700
701     if (ch->flags & ATA_DMA_ACTIVE) {
702         if (!((dmastat = ata_dmastatus(ch)) & ATA_BMSTAT_INTERRUPT))
703             return 1;
704         ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
705         DELAY(1);
706     }
707     return 0;
708 }
709
710 static int
711 ata_pci_print_child(device_t dev, device_t child)
712 {
713     struct ata_channel *ch = device_get_softc(child);
714     int retval = 0;
715
716     retval += bus_print_child_header(dev, child);
717     retval += printf(": at 0x%lx", rman_get_start(ch->r_io));
718
719     if (ATA_MASTERDEV(dev))
720         retval += printf(" irq %d", 14 + ch->unit);
721     
722     retval += bus_print_child_footer(dev, child);
723
724     return retval;
725 }
726
727 static struct resource *
728 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
729                        u_long start, u_long end, u_long count, u_int flags)
730 {
731     struct ata_pci_controller *controller = device_get_softc(dev);
732     struct resource *res = NULL;
733     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
734     int myrid;
735
736     if (type == SYS_RES_IOPORT) {
737         switch (*rid) {
738         case ATA_IOADDR_RID:
739             if (ATA_MASTERDEV(dev)) {
740                 myrid = 0;
741                 start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
742                 end = start + ATA_IOSIZE - 1;
743                 count = ATA_IOSIZE;
744                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
745                                          SYS_RES_IOPORT, &myrid,
746                                          start, end, count, flags);
747             }
748             else {
749                 myrid = 0x10 + 8 * unit;
750                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
751                                          SYS_RES_IOPORT, &myrid,
752                                          start, end, count, flags);
753             }
754             break;
755
756         case ATA_ALTADDR_RID:
757             if (ATA_MASTERDEV(dev)) {
758                 myrid = 0;
759                 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_ALTOFFSET;
760                 end = start + ATA_ALTIOSIZE - 1;
761                 count = ATA_ALTIOSIZE;
762                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
763                                          SYS_RES_IOPORT, &myrid,
764                                          start, end, count, flags);
765             }
766             else {
767                 myrid = 0x14 + 8 * unit;
768                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
769                                          SYS_RES_IOPORT, &myrid,
770                                          start, end, count, flags);
771                 if (res) {
772                         start = rman_get_start(res) + 2;
773                         end = start + ATA_ALTIOSIZE - 1;
774                         count = ATA_ALTIOSIZE;
775                         BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
776                                              SYS_RES_IOPORT, myrid, res);
777                         res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
778                                                  SYS_RES_IOPORT, &myrid,
779                                                  start, end, count, flags);
780                 }
781             }
782             break;
783
784         case ATA_BMADDR_RID:
785             if (controller->bmaddr) {
786                 myrid = 0x20;
787                 start = (unit == 0 ? 
788                          controller->bmaddr : controller->bmaddr+ATA_BMIOSIZE);
789                 end = start + ATA_BMIOSIZE - 1;
790                 count = ATA_BMIOSIZE;
791                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
792                                          SYS_RES_IOPORT, &myrid,
793                                          start, end, count, flags);
794             }
795         }
796         return res;
797     }
798
799     if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
800         if (ATA_MASTERDEV(dev)) {
801 #ifdef __alpha__
802             return alpha_platform_alloc_ide_intr(unit);
803 #else
804             int irq = (unit == 0 ? 14 : 15);
805
806             return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
807                                       SYS_RES_IRQ, rid, irq, irq, 1, flags);
808 #endif
809         }
810         else {
811             /* primary and secondary channels share interrupt, keep track */
812             if (!controller->irq)
813                 controller->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), 
814                                                      dev, SYS_RES_IRQ,
815                                                      rid, 0, ~0, 1, flags);
816             controller->irqcnt++;
817             return controller->irq;
818         }
819     }
820     return 0;
821 }
822
823 static int
824 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
825                          struct resource *r)
826 {
827     struct ata_pci_controller *controller = device_get_softc(dev);
828     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
829
830     if (type == SYS_RES_IOPORT) {
831         switch (rid) {
832         case ATA_IOADDR_RID:
833             if (ATA_MASTERDEV(dev))
834                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
835                                             SYS_RES_IOPORT, 0x0, r);
836             else
837                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
838                                             SYS_RES_IOPORT, 0x10 + 8 * unit, r);
839             break;
840
841         case ATA_ALTADDR_RID:
842             if (ATA_MASTERDEV(dev))
843                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
844                                             SYS_RES_IOPORT, 0x0, r);
845             else
846                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
847                                             SYS_RES_IOPORT, 0x14 + 8 * unit, r);
848             break;
849
850         case ATA_BMADDR_RID:
851             return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
852                                         SYS_RES_IOPORT, 0x20, r);
853         default:
854             return ENOENT;
855         }
856     }
857     if (type == SYS_RES_IRQ) {
858         if (rid != ATA_IRQ_RID)
859             return ENOENT;
860
861         if (ATA_MASTERDEV(dev)) {
862 #ifdef __alpha__
863             return alpha_platform_release_ide_intr(unit, r);
864 #else
865             return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
866                                         SYS_RES_IRQ, rid, r);
867 #endif
868         }
869         else {
870             /* primary and secondary channels share interrupt, keep track */
871             if (--controller->irqcnt)
872                 return 0;
873             controller->irq = NULL;
874             return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
875                                         SYS_RES_IRQ, rid, r);
876         }
877     }
878     return EINVAL;
879 }
880
881 static int
882 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 
883                    int flags, driver_intr_t *intr, void *arg,
884                    void **cookiep)
885 {
886     if (ATA_MASTERDEV(dev)) {
887 #ifdef __alpha__
888         return alpha_platform_setup_ide_intr(irq, intr, arg, cookiep);
889 #else
890         return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
891                               flags, intr, arg, cookiep);
892 #endif
893     }
894     else
895         return BUS_SETUP_INTR(device_get_parent(dev), dev, irq,
896                               flags, intr, arg, cookiep);
897 }
898
899 static int
900 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
901                       void *cookie)
902 {
903     if (ATA_MASTERDEV(dev)) {
904 #ifdef __alpha__
905         return alpha_platform_teardown_ide_intr(irq, cookie);
906 #else
907         return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
908 #endif
909     }
910     else
911         return BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie);
912 }
913
914 static device_method_t ata_pci_methods[] = {
915     /* device interface */
916     DEVMETHOD(device_probe,             ata_pci_probe),
917     DEVMETHOD(device_attach,            ata_pci_attach),
918     DEVMETHOD(device_shutdown,          bus_generic_shutdown),
919     DEVMETHOD(device_suspend,           bus_generic_suspend),
920     DEVMETHOD(device_resume,            bus_generic_resume),
921
922     /* bus methods */
923     DEVMETHOD(bus_print_child,          ata_pci_print_child),
924     DEVMETHOD(bus_alloc_resource,       ata_pci_alloc_resource),
925     DEVMETHOD(bus_release_resource,     ata_pci_release_resource),
926     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
927     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
928     DEVMETHOD(bus_setup_intr,           ata_pci_setup_intr),
929     DEVMETHOD(bus_teardown_intr,        ata_pci_teardown_intr),
930     { 0, 0 }
931 };
932
933 static driver_t ata_pci_driver = {
934     "atapci",
935     ata_pci_methods,
936     sizeof(struct ata_pci_controller),
937 };
938
939 static devclass_t ata_pci_devclass;
940
941 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0);
942
943 static int
944 ata_pcisub_probe(device_t dev)
945 {
946     struct ata_channel *ch = device_get_softc(dev);
947     device_t *children;
948     int count, i;
949
950     /* find channel number on this controller */
951     device_get_children(device_get_parent(dev), &children, &count);
952     for (i = 0; i < count; i++) {
953         if (children[i] == dev)
954             ch->unit = i;
955     }
956     free(children, M_TEMP);
957     ch->chiptype = pci_get_devid(device_get_parent(dev));
958     ch->intr_func = ata_pci_intr;
959     return ata_probe(dev);
960 }
961
962 static device_method_t ata_pcisub_methods[] = {
963     /* device interface */
964     DEVMETHOD(device_probe,     ata_pcisub_probe),
965     DEVMETHOD(device_attach,    ata_attach),
966     DEVMETHOD(device_detach,    ata_detach),
967     DEVMETHOD(device_resume,    ata_resume),
968     { 0, 0 }
969 };
970
971 static driver_t ata_pcisub_driver = {
972     "ata",
973     ata_pcisub_methods,
974     sizeof(struct ata_channel),
975 };
976
977 DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);