]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ata/ata-chipset.c
This commit was generated by cvs2svn to compensate for changes in r146040,
[FreeBSD/FreeBSD.git] / sys / dev / ata / ata-chipset.c
1 /*-
2  * Copyright (c) 1998 - 2005 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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_ata.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/ata.h>
37 #include <sys/bus.h>
38 #include <sys/endian.h>
39 #include <sys/malloc.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/sema.h>
43 #include <sys/taskqueue.h>
44 #include <vm/uma.h>
45 #include <machine/stdarg.h>
46 #include <machine/resource.h>
47 #include <machine/bus.h>
48 #include <sys/rman.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcireg.h>
51 #include <dev/ata/ata-all.h>
52 #include <dev/ata/ata-pci.h>
53 #include <ata_if.h>
54
55 /* local prototypes */
56 static int ata_generic_chipinit(device_t);
57 static void ata_generic_intr(void *);
58 static void ata_generic_setmode(device_t, int);
59 static int ata_acard_chipinit(device_t);
60 static void ata_acard_intr(void *);
61 static void ata_acard_850_setmode(device_t, int);
62 static void ata_acard_86X_setmode(device_t, int);
63 static int ata_ali_chipinit(device_t);
64 static int ata_ali_allocate(device_t);
65 static void ata_ali_setmode(device_t, int);
66 static int ata_amd_chipinit(device_t);
67 static int ata_cyrix_chipinit(device_t);
68 static void ata_cyrix_setmode(device_t, int);
69 static int ata_cypress_chipinit(device_t);
70 static void ata_cypress_setmode(device_t, int);
71 static int ata_highpoint_chipinit(device_t);
72 static void ata_highpoint_intr(void *);
73 static void ata_highpoint_setmode(device_t, int);
74 static int ata_highpoint_check_80pin(device_t, int);
75 static int ata_intel_chipinit(device_t);
76 static void ata_intel_intr(void *);
77 static void ata_intel_reset(device_t);
78 static void ata_intel_old_setmode(device_t, int);
79 static void ata_intel_new_setmode(device_t, int);
80 static int ata_ite_chipinit(device_t);
81 static void ata_ite_setmode(device_t, int);
82 static int ata_national_chipinit(device_t);
83 static void ata_national_setmode(device_t, int);
84 static int ata_nvidia_chipinit(device_t);
85 static int ata_nvidia_allocate(device_t);
86 static void ata_nvidia_intr(void *);
87 static void ata_nvidia_reset(device_t);
88 static int ata_promise_chipinit(device_t);
89 static int ata_promise_mio_allocate(device_t);
90 static void ata_promise_mio_intr(void *);
91 static void ata_promise_sx4_intr(void *);
92 static void ata_promise_mio_dmainit(device_t);
93 static void ata_promise_mio_reset(device_t);
94 static int ata_promise_mio_command(struct ata_request *);
95 static int ata_promise_sx4_command(struct ata_request *);
96 static int ata_promise_apkt(u_int8_t *, struct ata_request *);
97 static void ata_promise_queue_hpkt(struct ata_pci_controller *, u_int32_t);
98 static void ata_promise_next_hpkt(struct ata_pci_controller *);
99 static void ata_promise_tx2_intr(void *);
100 static void ata_promise_old_intr(void *);
101 static void ata_promise_new_dmainit(device_t);
102 static void ata_promise_setmode(device_t, int);
103 static int ata_serverworks_chipinit(device_t);
104 static void ata_serverworks_setmode(device_t, int);
105 static int ata_sii_chipinit(device_t);
106 static int ata_sii_allocate(device_t);
107 static void ata_sii_intr(void *);
108 static void ata_sii_reset(device_t);
109 static void ata_cmd_intr(void *);
110 static void ata_cmd_old_intr(void *);
111 static void ata_sii_setmode(device_t, int);
112 static void ata_cmd_setmode(device_t, int);
113 static int ata_sis_chipinit(device_t);
114 static int ata_sis_allocate(device_t dev);
115 static void ata_sis_reset(device_t);
116 static void ata_sis_setmode(device_t, int);
117 static int ata_via_chipinit(device_t);
118 static int ata_via_allocate(device_t dev);
119 static void ata_via_reset(device_t);
120 static void ata_via_southbridge_fixup(device_t);
121 static void ata_via_family_setmode(device_t, int);
122 static void ata_print_cable(device_t, u_int8_t *);
123 static int ata_atapi(device_t);
124 static int ata_check_80pin(device_t, int);
125 static struct ata_chip_id *ata_find_chip(device_t, struct ata_chip_id *, int);
126 static int ata_setup_interrupt(device_t);
127 static int ata_serialize(device_t, int);
128 static int ata_mode2idx(int);
129
130
131 /* generic or unknown ATA chipset support functions */
132 int
133 ata_generic_ident(device_t dev)
134 {
135     struct ata_pci_controller *ctlr = device_get_softc(dev);
136
137     device_set_desc(dev, "GENERIC ATA controller");
138     ctlr->chipinit = ata_generic_chipinit;
139     return 0;
140 }
141
142 static int
143 ata_generic_chipinit(device_t dev)
144 {
145     struct ata_pci_controller *ctlr = device_get_softc(dev);
146
147     if (ata_setup_interrupt(dev))
148         return ENXIO;
149     ctlr->setmode = ata_generic_setmode;
150     return 0;
151 }
152
153 static void
154 ata_generic_intr(void *data)
155 {
156     struct ata_pci_controller *ctlr = data;
157     struct ata_channel *ch;
158     int unit;
159
160     /* implement this as a toggle instead to balance load XXX */
161     for (unit = 0; unit < ctlr->channels; unit++) {
162         if (!(ch = ctlr->interrupt[unit].argument))
163             continue;
164         if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
165             int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
166
167             if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
168                 ATA_BMSTAT_INTERRUPT)
169                 continue;
170             ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
171             DELAY(1);
172         }
173         ctlr->interrupt[unit].function(ch);
174     }
175 }
176
177 static void
178 ata_generic_setmode(device_t dev, int mode)
179 {
180     struct ata_device *atadev = device_get_softc(dev);
181
182     mode = ata_limit_mode(dev, mode, ATA_UDMA2);
183     mode = ata_check_80pin(dev, mode);
184     if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
185         atadev->mode = mode;
186 }
187
188
189 /* SATA support functions */
190 static void
191 ata_sata_setmode(device_t dev, int mode)
192 {
193     struct ata_device *atadev = device_get_softc(dev);
194
195     /*
196      * if we detect that the device isn't a real SATA device we limit 
197      * the transfer mode to UDMA5/ATA100.
198      * this works around the problems some devices has with the 
199      * Marvell 88SX8030 SATA->PATA converters and UDMA6/ATA133.
200      */
201     if (atadev->param.satacapabilities != 0x0000 &&
202         atadev->param.satacapabilities != 0xffff) {
203         if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
204                             ata_limit_mode(dev, mode, ATA_UDMA6)))
205             atadev->mode = ATA_SA150;
206     }
207     else {
208         mode = ata_limit_mode(dev, mode, ATA_UDMA5);
209         if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
210             atadev->mode = mode;
211     }
212 }
213
214 static int
215 ata_sata_connect(struct ata_channel *ch)
216 {
217     u_int32_t status;
218     int timeout;
219
220     /* wait up to 1 second for "connect well" */
221     for (timeout = 0; timeout < 100 ; timeout++) {
222         status = ATA_IDX_INL(ch, ATA_SSTATUS);
223         if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1 ||
224             (status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)
225             break;
226         ata_udelay(10000);
227     }
228     if (timeout >= 100) {
229         if (bootverbose)
230             device_printf(ch->dev, "SATA connect status=%08x\n", status);
231         return 0;
232     }
233
234     /* clear SATA error register */
235     ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
236
237     /* find out what type device we got poll for spec'd 31 seconds */
238     /* XXX SOS 10 secs for now as I have little patience */
239     ch->devices = 0;
240     for (timeout = 0; timeout < 1000; timeout++) {
241         if (ATA_IDX_INB(ch, ATA_STATUS) & ATA_S_BUSY) 
242             DELAY(10000);
243         else
244             break;
245     }
246     if (1 | bootverbose)
247         device_printf(ch->dev, "SATA connect ready time=%dms\n", timeout * 10);
248     if (timeout < 1000) {
249         if ((ATA_IDX_INB(ch, ATA_CYL_LSB) == ATAPI_MAGIC_LSB) &&
250             (ATA_IDX_INB(ch, ATA_CYL_MSB) == ATAPI_MAGIC_MSB))
251             ch->devices = ATA_ATAPI_MASTER;
252         else /*if ((ATA_IDX_INB(ch, ATA_COUNT) == 0x01) &&
253             (ATA_IDX_INB(ch, ATA_CYL_LSB) == 0x01)) */
254             ch->devices = ATA_ATA_MASTER;
255     }
256     if (1 | bootverbose)
257         device_printf(ch->dev, "sata_connect devices=0x%b\n",
258                       ch->devices, "\20\3ATAPI_MASTER\1ATA_MASTER");
259     return 1;
260 }
261
262 static void
263 ata_sata_phy_enable(struct ata_channel *ch)
264 {
265     int loop, retry;
266
267     if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE) {
268         ata_sata_connect(ch);
269         return;
270     }
271
272     for (retry = 0; retry < 10; retry++) {
273         for (loop = 0; loop < 10; loop++) {
274             ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_RESET);
275             ata_udelay(100);
276             if ((ATA_IDX_INL(ch, ATA_SCONTROL) &
277                  ATA_SC_DET_MASK) == ATA_SC_DET_RESET)
278                 break;
279         }
280         ata_udelay(5000);
281         for (loop = 0; loop < 10; loop++) {
282             ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_IDLE);
283             ata_udelay(100);
284             if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == 0) {
285                 ata_sata_connect(ch);
286                 return;
287             }
288         }
289     }
290 }
291
292 static void
293 ata_sata_phy_event(void *context, int dummy)
294 {
295     struct ata_connect_task *tp = (struct ata_connect_task *)context;
296     device_t *children;
297     int nchildren, i;
298
299     mtx_lock(&Giant);   /* newbus suckage it needs Giant */
300     if (tp->action == ATA_C_ATTACH) {
301         struct ata_channel *ch = device_get_softc(tp->dev);
302
303         device_printf(tp->dev, "CONNECTED\n");
304         ata_sata_connect(ch);
305         ata_identify(tp->dev);
306     }
307     if (tp->action == ATA_C_DETACH) {
308         if (!device_get_children(tp->dev, &children, &nchildren)) {
309             for (i = 0; i < nchildren; i++)
310                 if (children[i])
311                     device_delete_child(tp->dev, children[i]);
312             free(children, M_TEMP);
313         }    
314         device_printf(tp->dev, "DISCONNECTED\n");
315     }
316     mtx_unlock(&Giant); /* suckage code dealt with, release Giant */
317     free(tp, M_ATA);
318 }
319
320
321 /*
322  * Acard chipset support functions
323  */
324 int
325 ata_acard_ident(device_t dev)
326 {
327     struct ata_pci_controller *ctlr = device_get_softc(dev);
328     struct ata_chip_id *idx;
329     static struct ata_chip_id ids[] =
330     {{ ATA_ATP850R, 0, ATPOLD, 0x00, ATA_UDMA2, "Acard ATP850" },
331      { ATA_ATP860A, 0, 0,      0x00, ATA_UDMA4, "Acard ATP860A" },
332      { ATA_ATP860R, 0, 0,      0x00, ATA_UDMA4, "Acard ATP860R" },
333      { ATA_ATP865A, 0, 0,      0x00, ATA_UDMA6, "Acard ATP865A" },
334      { ATA_ATP865R, 0, 0,      0x00, ATA_UDMA6, "Acard ATP865R" },
335      { 0, 0, 0, 0, 0, 0}};
336     char buffer[64]; 
337
338     if (!(idx = ata_match_chip(dev, ids)))
339         return ENXIO;
340
341     sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
342     device_set_desc_copy(dev, buffer);
343     ctlr->chip = idx;
344     ctlr->chipinit = ata_acard_chipinit;
345     return 0;
346 }
347
348 static int
349 ata_acard_chipinit(device_t dev)
350 {
351     struct ata_pci_controller *ctlr = device_get_softc(dev);
352     int rid = ATA_IRQ_RID;
353
354     if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
355                                                RF_SHAREABLE | RF_ACTIVE))) {
356         device_printf(dev, "unable to map interrupt\n");
357         return ENXIO;
358     }
359     if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
360                         ata_acard_intr, ctlr, &ctlr->handle))) {
361         device_printf(dev, "unable to setup interrupt\n");
362         return ENXIO;
363     }
364     if (ctlr->chip->cfg1 == ATPOLD) {
365         ctlr->setmode = ata_acard_850_setmode;
366         ctlr->locking = ata_serialize;
367     }
368     else
369         ctlr->setmode = ata_acard_86X_setmode;
370     return 0;
371 }
372
373 static void
374 ata_acard_intr(void *data)
375 {
376     struct ata_pci_controller *ctlr = data;
377     struct ata_channel *ch;
378     int unit;
379
380     /* implement this as a toggle instead to balance load XXX */
381     for (unit = 0; unit < ctlr->channels; unit++) {
382         if (!(ch = ctlr->interrupt[unit].argument))
383             continue;
384         if (ctlr->chip->cfg1 == ATPOLD &&
385             ATA_LOCKING(ch->dev, ATA_LF_WHICH) != unit)
386             continue;
387         if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
388             int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
389
390             if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
391                 ATA_BMSTAT_INTERRUPT)
392                 continue;
393             ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
394             DELAY(1);
395             ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
396                          ATA_IDX_INB(ch, ATA_BMCMD_PORT)&~ATA_BMCMD_START_STOP);
397             DELAY(1);
398         }
399         ctlr->interrupt[unit].function(ch);
400     }
401 }
402
403 static void
404 ata_acard_850_setmode(device_t dev, int mode)
405 {
406     device_t gparent = GRANDPARENT(dev);
407     struct ata_pci_controller *ctlr = device_get_softc(gparent);
408     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
409     struct ata_device *atadev = device_get_softc(dev);
410     int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
411     int error;
412
413     mode = ata_limit_mode(dev, mode,
414                           ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
415
416     /* XXX SOS missing WDMA0+1 + PIO modes */
417     if (mode >= ATA_WDMA2) {
418         error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
419         if (bootverbose)
420             device_printf(dev, "%ssetting %s on %s chip\n",
421                           (error) ? "FAILURE " : "",
422                           ata_mode2str(mode), ctlr->chip->text);
423         if (!error) {
424             u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
425             
426             reg54 &= ~(0x03 << (devno << 1));
427             if (mode >= ATA_UDMA0)
428                 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
429             pci_write_config(gparent, 0x54, reg54, 1);
430             pci_write_config(gparent, 0x4a, 0xa6, 1);
431             pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
432             atadev->mode = mode;
433             return;
434         }
435     }
436     /* we could set PIO mode timings, but we assume the BIOS did that */
437 }
438
439 static void
440 ata_acard_86X_setmode(device_t dev, int mode)
441 {
442     device_t gparent = GRANDPARENT(dev);
443     struct ata_pci_controller *ctlr = device_get_softc(gparent);
444     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
445     struct ata_device *atadev = device_get_softc(dev);
446     int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
447     int error;
448
449
450     mode = ata_limit_mode(dev, mode,
451                           ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
452
453     mode = ata_check_80pin(dev, mode);
454
455     /* XXX SOS missing WDMA0+1 + PIO modes */
456     if (mode >= ATA_WDMA2) {
457         error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
458         if (bootverbose)
459             device_printf(dev, "%ssetting %s on %s chip\n",
460                           (error) ? "FAILURE " : "",
461                           ata_mode2str(mode), ctlr->chip->text);
462         if (!error) {
463             u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
464             
465             reg44 &= ~(0x000f << (devno << 2));
466             if (mode >= ATA_UDMA0)
467                 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
468             pci_write_config(gparent, 0x44, reg44, 2);
469             pci_write_config(gparent, 0x4a, 0xa6, 1);
470             pci_write_config(gparent, 0x40 + devno, 0x31, 1);
471             atadev->mode = mode;
472             return;
473         }
474     }
475     /* we could set PIO mode timings, but we assume the BIOS did that */
476 }
477
478
479 /*
480  * Acer Labs Inc (ALI) chipset support functions
481  */
482 int
483 ata_ali_ident(device_t dev)
484 {
485     struct ata_pci_controller *ctlr = device_get_softc(dev);
486     struct ata_chip_id *idx;
487     static struct ata_chip_id ids[] =
488     {{ ATA_ALI_5289, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5289" },
489      { ATA_ALI_5287, 0x00, 4, ALISATA, ATA_SA150, "AcerLabs M5287" },
490      { ATA_ALI_5281, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5281" },
491      { ATA_ALI_5229, 0xc5, 0, ALINEW,  ATA_UDMA6, "AcerLabs M5229" },
492      { ATA_ALI_5229, 0xc4, 0, ALINEW,  ATA_UDMA5, "AcerLabs M5229" },
493      { ATA_ALI_5229, 0xc2, 0, ALINEW,  ATA_UDMA4, "AcerLabs M5229" },
494      { ATA_ALI_5229, 0x20, 0, ALIOLD,  ATA_UDMA2, "AcerLabs M5229" },
495      { ATA_ALI_5229, 0x00, 0, ALIOLD,  ATA_WDMA2, "AcerLabs M5229" },
496      { 0, 0, 0, 0, 0, 0}};
497     char buffer[64]; 
498
499     if (!(idx = ata_match_chip(dev, ids)))
500         return ENXIO;
501
502     sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
503     device_set_desc_copy(dev, buffer);
504     ctlr->chip = idx;
505     ctlr->chipinit = ata_ali_chipinit;
506     return 0;
507 }
508
509 static int
510 ata_ali_chipinit(device_t dev)
511 {
512     struct ata_pci_controller *ctlr = device_get_softc(dev);
513
514     if (ata_setup_interrupt(dev))
515         return ENXIO;
516
517     switch (ctlr->chip->cfg2) {
518     case ALISATA:
519         pci_write_config(dev, PCIR_COMMAND,
520                          pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
521         ctlr->channels = ctlr->chip->cfg1;
522         ctlr->allocate = ata_ali_allocate;
523         ctlr->setmode = ata_sata_setmode;
524         break;
525
526     case ALINEW:
527         /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
528         pci_write_config(dev, 0x53,
529                          pci_read_config(dev, 0x53, 1) | 0x01, 1);
530
531         /* enable cable detection and UDMA support on newer chips */
532         pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
533         ctlr->setmode = ata_ali_setmode;
534         break;
535
536     case ALIOLD:
537         /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
538         pci_write_config(dev, 0x53,
539                          (pci_read_config(dev, 0x53, 1) & ~0x02) | 0x03, 1);
540         ctlr->setmode = ata_ali_setmode;
541         break;
542     }
543     return 0;
544 }
545
546 static int
547 ata_ali_allocate(device_t dev)
548 {
549     device_t parent = device_get_parent(dev);
550     struct ata_pci_controller *ctlr = device_get_softc(parent);
551     struct ata_channel *ch = device_get_softc(dev);
552     struct resource *io = NULL, *ctlio = NULL;
553     int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
554     int i, rid;
555                 
556     rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
557     io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
558     if (!io)
559         return ENXIO;
560
561     rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
562     ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
563     if (!ctlio) {
564         bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
565         return ENXIO;
566     }
567                 
568     for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
569         ch->r_io[i].res = io;
570         ch->r_io[i].offset = i + (unit10 ? 8 : 0);
571     }
572     ch->r_io[ATA_CONTROL].res = ctlio;
573     ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
574     ch->r_io[ATA_IDX_ADDR].res = io;
575     ata_default_registers(dev);
576     if (ctlr->r_res1) {
577         for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
578             ch->r_io[i].res = ctlr->r_res1;
579             ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
580         }
581     }
582     ch->flags |= ATA_NO_SLAVE;
583
584     /* XXX SOS PHY handling awkward in ALI chip not supported yet */
585     ata_generic_hw(dev);
586     return 0;
587 }
588
589 static void
590 ata_ali_setmode(device_t dev, int mode)
591 {
592     device_t gparent = GRANDPARENT(dev);
593     struct ata_pci_controller *ctlr = device_get_softc(gparent);
594     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
595     struct ata_device *atadev = device_get_softc(dev);
596     int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
597     int error;
598
599     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
600
601     if (ctlr->chip->cfg2 & ALINEW) {
602         if (mode > ATA_UDMA2 &&
603             pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
604             ata_print_cable(dev, "controller");
605             mode = ATA_UDMA2;
606         }
607     }
608     else
609         mode = ata_check_80pin(dev, mode);
610
611     if (ctlr->chip->cfg2 & ALIOLD) {
612         /* doesn't support ATAPI DMA on write */
613         ch->flags |= ATA_ATAPI_DMA_RO;
614         if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
615             /* doesn't support ATAPI DMA on two ATAPI devices */
616             device_printf(dev, "two atapi devices on this channel, no DMA\n");
617             mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
618         }
619     }
620
621     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
622
623     if (bootverbose)
624         device_printf(dev, "%ssetting %s on %s chip\n",
625                    (error) ? "FAILURE " : "", 
626                    ata_mode2str(mode), ctlr->chip->text);
627     if (!error) {
628         if (mode >= ATA_UDMA0) {
629             u_int8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
630             u_int32_t word54 = pci_read_config(gparent, 0x54, 4);
631
632             word54 &= ~(0x000f000f << (devno << 2));
633             word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
634             pci_write_config(gparent, 0x54, word54, 4);
635             pci_write_config(gparent, 0x58 + (ch->unit << 2),
636                              0x00310001, 4);
637         }
638         else {
639             u_int32_t piotimings[] =
640                 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
641                   0x00310001, 0x00440001, 0x00330001, 0x00310001};
642
643             pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
644                                             ~(0x0008000f << (devno << 2)), 4);
645             pci_write_config(gparent, 0x58 + (ch->unit << 2),
646                              piotimings[ata_mode2idx(mode)], 4);
647         }
648         atadev->mode = mode;
649     }
650 }
651
652
653 /*
654  * American Micro Devices (AMD) chipset support functions
655  */
656 int
657 ata_amd_ident(device_t dev)
658 {
659     struct ata_pci_controller *ctlr = device_get_softc(dev);
660     struct ata_chip_id *idx;
661     static struct ata_chip_id ids[] =
662     {{ ATA_AMD756,  0x00, AMDNVIDIA, 0x00,            ATA_UDMA4, "AMD 756" },
663      { ATA_AMD766,  0x00, AMDNVIDIA, AMDCABLE|AMDBUG, ATA_UDMA5, "AMD 766" },
664      { ATA_AMD768,  0x00, AMDNVIDIA, AMDCABLE,        ATA_UDMA5, "AMD 768" },
665      { ATA_AMD8111, 0x00, AMDNVIDIA, AMDCABLE,        ATA_UDMA6, "AMD 8111" },
666      { 0, 0, 0, 0, 0, 0}};
667     char buffer[64]; 
668
669     if (!(idx = ata_match_chip(dev, ids)))
670         return ENXIO;
671
672     sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
673     device_set_desc_copy(dev, buffer);
674     ctlr->chip = idx;
675     ctlr->chipinit = ata_amd_chipinit;
676     return 0;
677 }
678
679 static int
680 ata_amd_chipinit(device_t dev)
681 {
682     struct ata_pci_controller *ctlr = device_get_softc(dev);
683
684     if (ata_setup_interrupt(dev))
685         return ENXIO;
686
687     /* disable/set prefetch, postwrite */
688     if (ctlr->chip->cfg2 & AMDBUG)
689         pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
690     else
691         pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
692
693     ctlr->setmode = ata_via_family_setmode;
694     return 0;
695 }
696
697
698 /*
699  * Cyrix chipset support functions
700  */
701 int
702 ata_cyrix_ident(device_t dev)
703 {
704     struct ata_pci_controller *ctlr = device_get_softc(dev);
705
706     if (pci_get_devid(dev) == ATA_CYRIX_5530) {
707         device_set_desc(dev, "Cyrix 5530 ATA33 controller");
708         ctlr->chipinit = ata_cyrix_chipinit;
709         return 0;
710     }
711     return ENXIO;
712 }
713
714 static int
715 ata_cyrix_chipinit(device_t dev)
716 {
717     struct ata_pci_controller *ctlr = device_get_softc(dev);
718
719     if (ata_setup_interrupt(dev))
720         return ENXIO;
721
722     if (ctlr->r_res1)
723         ctlr->setmode = ata_cyrix_setmode;
724     else
725         ctlr->setmode = ata_generic_setmode;
726     return 0;
727 }
728
729 static void
730 ata_cyrix_setmode(device_t dev, int mode)
731 {
732     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
733     struct ata_device *atadev = device_get_softc(dev);
734     int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
735     u_int32_t piotiming[] = 
736         { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
737     u_int32_t dmatiming[] = { 0x00077771, 0x00012121, 0x00002020 };
738     u_int32_t udmatiming[] = { 0x00921250, 0x00911140, 0x00911030 };
739     int error;
740
741     ch->dma->alignment = 16;
742     ch->dma->max_iosize = 126 * DEV_BSIZE;
743
744     mode = ata_limit_mode(dev, mode, ATA_UDMA2);
745
746     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
747
748     if (bootverbose)
749         device_printf(dev, "%ssetting %s on Cyrix chip\n",
750                       (error) ? "FAILURE " : "", ata_mode2str(mode));
751     if (!error) {
752         if (mode >= ATA_UDMA0) {
753             ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
754                      0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
755         }
756         else if (mode >= ATA_WDMA0) {
757             ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
758                      0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
759         }
760         else {
761             ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
762                      0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
763         }
764         atadev->mode = mode;
765     }
766 }
767
768
769 /*
770  * Cypress chipset support functions
771  */
772 int
773 ata_cypress_ident(device_t dev)
774 {
775     struct ata_pci_controller *ctlr = device_get_softc(dev);
776
777     /*
778      * the Cypress chip is a mess, it contains two ATA functions, but
779      * both channels are visible on the first one.
780      * simply ignore the second function for now, as the right
781      * solution (ignoring the second channel on the first function)
782      * doesn't work with the crappy ATA interrupt setup on the alpha.
783      */
784     if (pci_get_devid(dev) == ATA_CYPRESS_82C693 &&
785         pci_get_function(dev) == 1 &&
786         pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
787         device_set_desc(dev, "Cypress 82C693 ATA controller");
788         ctlr->chipinit = ata_cypress_chipinit;
789         return 0;
790     }
791     return ENXIO;
792 }
793
794 static int
795 ata_cypress_chipinit(device_t dev)
796 {
797     struct ata_pci_controller *ctlr = device_get_softc(dev);
798
799     if (ata_setup_interrupt(dev))
800         return ENXIO;
801
802     ctlr->setmode = ata_cypress_setmode;
803     return 0;
804 }
805
806 static void
807 ata_cypress_setmode(device_t dev, int mode)
808 {
809     device_t gparent = GRANDPARENT(dev);
810     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
811     struct ata_device *atadev = device_get_softc(dev);
812     int error;
813
814     mode = ata_limit_mode(dev, mode, ATA_WDMA2);
815
816     /* XXX missing WDMA0+1 + PIO modes */
817     if (mode == ATA_WDMA2) { 
818         error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
819         if (bootverbose)
820             device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
821                           error ? "FAILURE " : "");
822         if (!error) {
823             pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
824             atadev->mode = mode;
825             return;
826         }
827     }
828     /* we could set PIO mode timings, but we assume the BIOS did that */
829 }
830
831
832 /*
833  * HighPoint chipset support functions
834  */
835 int
836 ata_highpoint_ident(device_t dev)
837 {
838     struct ata_pci_controller *ctlr = device_get_softc(dev);
839     struct ata_chip_id *idx;
840     static struct ata_chip_id ids[] =
841     {{ ATA_HPT374, 0x07, HPT374, 0x00,   ATA_UDMA6, "HighPoint HPT374" },
842      { ATA_HPT372, 0x02, HPT372, 0x00,   ATA_UDMA6, "HighPoint HPT372N" },
843      { ATA_HPT372, 0x01, HPT372, 0x00,   ATA_UDMA6, "HighPoint HPT372" },
844      { ATA_HPT371, 0x01, HPT372, 0x00,   ATA_UDMA6, "HighPoint HPT371" },
845      { ATA_HPT366, 0x05, HPT372, 0x00,   ATA_UDMA6, "HighPoint HPT372" },
846      { ATA_HPT366, 0x03, HPT370, 0x00,   ATA_UDMA5, "HighPoint HPT370" },
847      { ATA_HPT366, 0x02, HPT366, 0x00,   ATA_UDMA4, "HighPoint HPT368" },
848      { ATA_HPT366, 0x00, HPT366, HPTOLD, ATA_UDMA4, "HighPoint HPT366" },
849      { ATA_HPT302, 0x01, HPT372, 0x00,   ATA_UDMA6, "HighPoint HPT302" },
850      { 0, 0, 0, 0, 0, 0}};
851     char buffer[64];
852
853     if (!(idx = ata_match_chip(dev, ids)))
854         return ENXIO;
855
856     strcpy(buffer, idx->text);
857     if (idx->cfg1 == HPT374) {
858         if (pci_get_function(dev) == 0)
859             strcat(buffer, " (channel 0+1)");
860         else if (pci_get_function(dev) == 1)
861             strcat(buffer, " (channel 2+3)");
862     }
863     sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
864     device_set_desc_copy(dev, buffer);
865     ctlr->chip = idx;
866     ctlr->chipinit = ata_highpoint_chipinit;
867     return 0;
868 }
869
870 static int
871 ata_highpoint_chipinit(device_t dev)
872 {
873     struct ata_pci_controller *ctlr = device_get_softc(dev);
874     int rid = ATA_IRQ_RID;
875
876     if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
877                                                RF_SHAREABLE | RF_ACTIVE))) {
878         device_printf(dev, "unable to map interrupt\n");
879         return ENXIO;
880     }
881     if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
882                         ata_highpoint_intr, ctlr, &ctlr->handle))) {
883         device_printf(dev, "unable to setup interrupt\n");
884         return ENXIO;
885     }
886
887     if (ctlr->chip->cfg2 == HPTOLD) {
888         /* disable interrupt prediction */
889         pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
890     }
891     else {
892         /* disable interrupt prediction */
893         pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
894         pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
895
896         /* enable interrupts */
897         pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
898
899         /* set clocks etc */
900         if (ctlr->chip->cfg1 < HPT372)
901             pci_write_config(dev, 0x5b, 0x22, 1);
902         else
903             pci_write_config(dev, 0x5b,
904                              (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
905     }
906     ctlr->setmode = ata_highpoint_setmode;
907     return 0;
908 }
909
910 static void
911 ata_highpoint_intr(void *data)
912 {
913     struct ata_pci_controller *ctlr = data;
914     struct ata_channel *ch;
915     int unit;
916
917     /* implement this as a toggle instead to balance load XXX */
918     for (unit = 0; unit < ctlr->channels; unit++) {
919         if (!(ch = ctlr->interrupt[unit].argument))
920             continue;
921         if (ch->dma) {
922             int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
923
924             if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
925                 ATA_BMSTAT_INTERRUPT)
926                 continue;
927             ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
928             DELAY(1);
929         }
930         ctlr->interrupt[unit].function(ch);
931     }
932 }
933
934 static void
935 ata_highpoint_setmode(device_t dev, int mode)
936 {
937     device_t gparent = GRANDPARENT(dev);
938     struct ata_pci_controller *ctlr = device_get_softc(gparent);
939     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
940     struct ata_device *atadev = device_get_softc(dev);
941     int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
942     int error;
943     u_int32_t timings33[][4] = {
944     /*    HPT366      HPT370      HPT372      HPT374               mode */
945         { 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a },     /* PIO 0 */
946         { 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 },     /* PIO 1 */
947         { 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 },     /* PIO 2 */
948         { 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 },     /* PIO 3 */
949         { 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 },     /* PIO 4 */
950         { 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea },     /* MWDMA 0 */
951         { 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 },     /* MWDMA 1 */
952         { 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 },     /* MWDMA 2 */
953         { 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea },     /* UDMA 0 */
954         { 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 },     /* UDMA 1 */
955         { 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 },     /* UDMA 2 */
956         { 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 },     /* UDMA 3 */
957         { 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 },     /* UDMA 4 */
958         { 0,          0x16454e31, 0x1c8a9c62, 0x12848242 },     /* UDMA 5 */
959         { 0,          0,          0x1c869c62, 0x12808242 }      /* UDMA 6 */
960     };
961
962     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
963
964     if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
965         mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
966
967     mode = ata_highpoint_check_80pin(dev, mode);
968
969     /*
970      * most if not all HPT chips cant really handle that the device is
971      * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
972      * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
973      */
974     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
975                            ata_limit_mode(dev, mode, ATA_UDMA5));
976     if (bootverbose)
977         device_printf(dev, "%ssetting %s on HighPoint chip\n",
978                       (error) ? "FAILURE " : "", ata_mode2str(mode));
979     if (!error)
980         pci_write_config(gparent, 0x40 + (devno << 2),
981                          timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
982     atadev->mode = mode;
983 }
984
985 static int
986 ata_highpoint_check_80pin(device_t dev, int mode)
987 {
988     device_t gparent = GRANDPARENT(dev);
989     struct ata_pci_controller *ctlr = device_get_softc(gparent);
990     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
991     u_int8_t reg, val, res;
992
993     if (ctlr->chip->cfg1 == HPT374 && pci_get_function(gparent) == 1) {
994         reg = ch->unit ? 0x57 : 0x53;
995         val = pci_read_config(gparent, reg, 1);
996         pci_write_config(gparent, reg, val | 0x80, 1);
997     }
998     else {
999         reg = 0x5b;
1000         val = pci_read_config(gparent, reg, 1);
1001         pci_write_config(gparent, reg, val & 0xfe, 1);
1002     }
1003     res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1004     pci_write_config(gparent, reg, val, 1);
1005
1006     if (mode > ATA_UDMA2 && res) {
1007         ata_print_cable(dev, "controller");
1008         mode = ATA_UDMA2;
1009     }
1010     return mode;
1011 }
1012
1013
1014 /*
1015  * Intel chipset support functions
1016  */
1017 int
1018 ata_intel_ident(device_t dev)
1019 {
1020     struct ata_pci_controller *ctlr = device_get_softc(dev);
1021     struct ata_chip_id *idx;
1022     static struct ata_chip_id ids[] =
1023     {{ ATA_I82371FB,    0, 0, 0x00, ATA_WDMA2, "Intel PIIX" },
1024      { ATA_I82371SB,    0, 0, 0x00, ATA_WDMA2, "Intel PIIX3" },
1025      { ATA_I82371AB,    0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1026      { ATA_I82443MX,    0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1027      { ATA_I82451NX,    0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1028      { ATA_I82801AB,    0, 0, 0x00, ATA_UDMA2, "Intel ICH0" },
1029      { ATA_I82801AA,    0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1030      { ATA_I82372FB,    0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1031      { ATA_I82801BA,    0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1032      { ATA_I82801BA_1,  0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1033      { ATA_I82801CA,    0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1034      { ATA_I82801CA_1,  0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1035      { ATA_I82801DB,    0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1036      { ATA_I82801DB_1,  0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1037      { ATA_I82801EB,    0, 0, 0x00, ATA_UDMA5, "Intel ICH5" },
1038      { ATA_I82801EB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1039      { ATA_I82801EB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1040      { ATA_I6300ESB,    0, 0, 0x00, ATA_UDMA5, "Intel 6300ESB" },
1041      { ATA_I6300ESB_S1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1042      { ATA_I6300ESB_R1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1043      { ATA_I82801FB,    0, 0, 0x00, ATA_UDMA5, "Intel ICH6" },
1044      { ATA_I82801FB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1045      { ATA_I82801FB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1046      { ATA_I82801FBM,   0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1047      { 0, 0, 0, 0, 0, 0}};
1048     char buffer[64]; 
1049
1050     if (!(idx = ata_match_chip(dev, ids)))
1051         return ENXIO;
1052
1053     sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1054     device_set_desc_copy(dev, buffer);
1055     ctlr->chip = idx;
1056     ctlr->chipinit = ata_intel_chipinit;
1057     return 0;
1058 }
1059
1060 static int
1061 ata_intel_chipinit(device_t dev)
1062 {
1063     struct ata_pci_controller *ctlr = device_get_softc(dev);
1064     int rid = ATA_IRQ_RID;
1065
1066     if (!ata_legacy(dev)) {
1067         if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1068                                                    RF_SHAREABLE | RF_ACTIVE))) {
1069             device_printf(dev, "unable to map interrupt\n");
1070             return ENXIO;
1071         }
1072         if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1073                             ata_intel_intr, ctlr, &ctlr->handle))) {
1074             device_printf(dev, "unable to setup interrupt\n");
1075             return ENXIO;
1076         }
1077     }
1078
1079     if (ctlr->chip->chipid == ATA_I82371FB) {
1080         ctlr->setmode = ata_intel_old_setmode;
1081     }
1082     else if (ctlr->chip->max_dma < ATA_SA150) {
1083         ctlr->setmode = ata_intel_new_setmode;
1084     }
1085     else {
1086         pci_write_config(dev, PCIR_COMMAND,
1087                          pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1088         ctlr->reset = ata_intel_reset;
1089         ctlr->setmode = ata_sata_setmode;
1090     }
1091     return 0;
1092 }
1093
1094 static void
1095 ata_intel_intr(void *data)
1096 {
1097     struct ata_pci_controller *ctlr = data;
1098     struct ata_channel *ch;
1099     int unit;
1100
1101     /* implement this as a toggle instead to balance load XXX */
1102     for (unit = 0; unit < ctlr->channels; unit++) {
1103         if (!(ch = ctlr->interrupt[unit].argument))
1104             continue;
1105         if (ch->dma) {
1106             int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1107
1108             if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1109                 ATA_BMSTAT_INTERRUPT)
1110                 continue;
1111             ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1112             DELAY(1);
1113         }
1114         ctlr->interrupt[unit].function(ch);
1115     }
1116 }
1117
1118 static void
1119 ata_intel_old_setmode(device_t dev, int mode)
1120 {
1121     /* NOT YET */
1122 }
1123
1124 static void
1125 ata_intel_reset(device_t dev)
1126 {
1127     device_t parent = device_get_parent(dev);
1128     struct ata_pci_controller *ctlr = device_get_softc(parent);
1129     struct ata_channel *ch = device_get_softc(dev);
1130     int mask, timeout;
1131
1132     /* ICH6 has 4 SATA ports as master/slave on 2 channels so deal with pairs */
1133     if (ctlr->chip->chipid == ATA_I82801FB_S1 ||
1134         ctlr->chip->chipid == ATA_I82801FB_R1) {
1135         mask = (0x0005 << ch->unit);
1136     }
1137     else {
1138         /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1139         if (pci_read_config(parent, 0x90, 1) & 0x04)
1140             mask = 0x0003;
1141         else {
1142             mask = (0x0001 << ch->unit);
1143             /* XXX SOS should be in intel_allocate when we grow it */
1144             ch->flags |= ATA_NO_SLAVE;
1145         }
1146     }
1147     pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1148     DELAY(10);
1149     pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1150
1151     /* wait up to 1 sec for "connect well" */
1152     for (timeout = 0; timeout < 100 ; timeout++) {
1153         if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1154             (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1155             break;
1156         ata_udelay(10000);
1157     }
1158     ata_generic_reset(dev);
1159 }
1160
1161 static void
1162 ata_intel_new_setmode(device_t dev, int mode)
1163 {
1164     device_t gparent = GRANDPARENT(dev);
1165     struct ata_pci_controller *ctlr = device_get_softc(gparent);
1166     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1167     struct ata_device *atadev = device_get_softc(dev);
1168     int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1169     u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
1170     u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
1171     u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
1172     u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
1173     u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
1174     u_int32_t mask40 = 0, new40 = 0;
1175     u_int8_t mask44 = 0, new44 = 0;
1176     int error;
1177     u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1178                            0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1179
1180     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1181
1182     if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1183         ata_print_cable(dev, "controller");
1184         mode = ATA_UDMA2;
1185     }
1186
1187     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1188
1189     if (bootverbose)
1190         device_printf(dev, "%ssetting %s on %s chip\n",
1191                       (error) ? "FAILURE " : "",
1192                       ata_mode2str(mode), ctlr->chip->text);
1193     if (error)
1194         return;
1195
1196     if (mode >= ATA_UDMA0) {
1197         pci_write_config(gparent, 0x48, reg48 | (0x0001 << devno), 2);
1198         pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno<<2))) | 
1199                                        (0x01 + !(mode & 0x01)), 2);
1200     }
1201     else {
1202         pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
1203         pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))), 2);
1204     }
1205     reg54 |= 0x0400;
1206     if (mode >= ATA_UDMA2)
1207         pci_write_config(gparent, 0x54, reg54 | (0x1 << devno), 2);
1208     else
1209         pci_write_config(gparent, 0x54, reg54 & ~(0x1 << devno), 2);
1210
1211     if (mode >= ATA_UDMA5)
1212         pci_write_config(gparent, 0x54, reg54 | (0x1000 << devno), 2);
1213     else 
1214         pci_write_config(gparent, 0x54, reg54 & ~(0x1000 << devno), 2);
1215
1216     reg40 &= ~0x00ff00ff;
1217     reg40 |= 0x40774077;
1218
1219     if (atadev->unit == ATA_MASTER) {
1220         mask40 = 0x3300;
1221         new40 = timings[ata_mode2idx(mode)] << 8;
1222     }
1223     else {
1224         mask44 = 0x0f;
1225         new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
1226                 (timings[ata_mode2idx(mode)] & 0x03);
1227     }
1228     if (ch->unit) {
1229         mask40 <<= 16;
1230         new40 <<= 16;
1231         mask44 <<= 4;
1232         new44 <<= 4;
1233     }
1234     pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
1235     pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
1236
1237     atadev->mode = mode;
1238 }
1239
1240
1241 /*
1242  * Integrated Technology Express Inc. (ITE) chipset support functions
1243  */
1244 int
1245 ata_ite_ident(device_t dev)
1246 {
1247     struct ata_pci_controller *ctlr = device_get_softc(dev);
1248
1249     if (pci_get_devid(dev) == ATA_IT8212F) {
1250         device_set_desc(dev, "ITE IT8212F ATA133 controller");
1251         ctlr->chipinit = ata_ite_chipinit;
1252         return 0;
1253     }
1254     return ENXIO;
1255 }
1256
1257 static int
1258 ata_ite_chipinit(device_t dev)
1259 {
1260     struct ata_pci_controller *ctlr = device_get_softc(dev);
1261
1262     if (ata_setup_interrupt(dev))
1263         return ENXIO;
1264
1265     ctlr->setmode = ata_ite_setmode;
1266
1267     /* set PCI mode and 66Mhz reference clock */
1268     pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
1269
1270     /* set default active & recover timings */
1271     pci_write_config(dev, 0x54, 0x31, 1);
1272     pci_write_config(dev, 0x56, 0x31, 1);
1273     return 0;
1274 }
1275  
1276 static void
1277 ata_ite_setmode(device_t dev, int mode)
1278 {
1279     device_t gparent = GRANDPARENT(dev);
1280     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1281     struct ata_device *atadev = device_get_softc(dev);
1282     int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1283     int error;
1284
1285     /* correct the mode for what the HW supports */
1286     mode = ata_limit_mode(dev, mode, ATA_UDMA6);
1287
1288     /* check the CBLID bits for 80 conductor cable detection */
1289     if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
1290                              (ch->unit ? (1<<3) : (1<<2)))) {
1291         ata_print_cable(dev, "controller");
1292         mode = ATA_UDMA2;
1293     }
1294
1295     /* set the wanted mode on the device */
1296     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1297
1298     if (bootverbose)
1299         device_printf(dev, "%s setting %s on ITE8212F chip\n",
1300                       (error) ? "failed" : "success", ata_mode2str(mode));
1301
1302     /* if the device accepted the mode change, setup the HW accordingly */
1303     if (!error) {
1304         if (mode >= ATA_UDMA0) {
1305             u_int8_t udmatiming[] =
1306                 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
1307
1308             /* enable UDMA mode */
1309             pci_write_config(gparent, 0x50,
1310                              pci_read_config(gparent, 0x50, 1) &
1311                              ~(1 << (devno + 3)), 1);
1312
1313             /* set UDMA timing */
1314             pci_write_config(gparent,
1315                              0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
1316                              udmatiming[mode & ATA_MODE_MASK], 1);
1317         }
1318         else {
1319             u_int8_t chtiming[] =
1320                 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
1321
1322             /* disable UDMA mode */
1323             pci_write_config(gparent, 0x50,
1324                              pci_read_config(gparent, 0x50, 1) |
1325                              (1 << (devno + 3)), 1);
1326
1327             /* set active and recover timing (shared between master & slave) */
1328             if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
1329                 chtiming[ata_mode2idx(mode)])
1330                 pci_write_config(gparent, 0x54 + (ch->unit << 2),
1331                                  chtiming[ata_mode2idx(mode)], 1);
1332         }
1333         atadev->mode = mode;
1334     }
1335 }
1336
1337
1338 /*
1339  * National chipset support functions
1340  */
1341 int
1342 ata_national_ident(device_t dev)
1343 {
1344     struct ata_pci_controller *ctlr = device_get_softc(dev);
1345
1346     /* this chip is a clone of the Cyrix chip, bugs and all */
1347     if (pci_get_devid(dev) == ATA_SC1100) {
1348         device_set_desc(dev, "National Geode SC1100 ATA33 controller");
1349         ctlr->chipinit = ata_national_chipinit;
1350         return 0;
1351     }
1352     return ENXIO;
1353 }
1354     
1355 static device_t nat_host = NULL;
1356
1357 static int
1358 ata_national_chipinit(device_t dev)
1359 {
1360     struct ata_pci_controller *ctlr = device_get_softc(dev);
1361     device_t *children;
1362     int nchildren, i;
1363     
1364     if (ata_setup_interrupt(dev))
1365         return ENXIO;
1366                     
1367     /* locate the ISA part in the southbridge and enable UDMA33 */
1368     if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
1369         for (i = 0; i < nchildren; i++) {
1370             if (pci_get_devid(children[i]) == 0x0510100b) {
1371                 nat_host = children[i];
1372                 break;
1373             }
1374         }
1375         free(children, M_TEMP);
1376     }
1377     ctlr->setmode = ata_national_setmode;
1378     return 0;
1379 }
1380
1381 static void
1382 ata_national_setmode(device_t dev, int mode)
1383 {
1384     device_t gparent = GRANDPARENT(dev);
1385     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1386     struct ata_device *atadev = device_get_softc(dev);
1387     int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1388     u_int32_t piotiming[] =
1389         { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
1390           0x00803020, 0x20102010, 0x00100010,
1391           0x00100010, 0x00100010, 0x00100010 };
1392     u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
1393     u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
1394     int error;
1395
1396     ch->dma->alignment = 16;
1397     ch->dma->max_iosize = 126 * DEV_BSIZE;
1398
1399     mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1400
1401     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1402
1403     if (bootverbose)
1404         device_printf(dev, "%s setting %s on National chip\n",
1405                       (error) ? "failed" : "success", ata_mode2str(mode));
1406     if (!error) {
1407         if (mode >= ATA_UDMA0) {
1408             pci_write_config(gparent, 0x44 + (devno << 3),
1409                              udmatiming[mode & ATA_MODE_MASK], 4);
1410         }
1411         else if (mode >= ATA_WDMA0) {
1412             pci_write_config(gparent, 0x44 + (devno << 3),
1413                              dmatiming[mode & ATA_MODE_MASK], 4);
1414         }
1415         else {
1416             pci_write_config(gparent, 0x44 + (devno << 3),
1417                              pci_read_config(gparent, 0x44 + (devno << 3), 4) |
1418                              0x80000000, 4);
1419         }
1420         pci_write_config(gparent, 0x40 + (devno << 3),
1421                          piotiming[ata_mode2idx(mode)], 4);
1422         atadev->mode = mode;
1423     }
1424 }
1425
1426
1427 /*
1428  * nVidia chipset support functions
1429  */
1430 int
1431 ata_nvidia_ident(device_t dev)
1432 {
1433     struct ata_pci_controller *ctlr = device_get_softc(dev);
1434     struct ata_chip_id *idx;
1435     static struct ata_chip_id ids[] =
1436     {{ ATA_NFORCE1,     0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nVidia nForce" },
1437      { ATA_NFORCE2,     0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2" },
1438      { ATA_NFORCE2_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2 MCP" },
1439      { ATA_NFORCE2_MCP_S1, 0, 0,      0,      ATA_SA150, "nVidia nForce2 MCP" },
1440      { ATA_NFORCE3,     0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3" },
1441      { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 Pro" },
1442      { ATA_NFORCE3_PRO_S1, 0, 0,      0,      ATA_SA150, "nVidia nForce3 Pro" },
1443      { ATA_NFORCE3_PRO_S2, 0, 0,      0,      ATA_SA150, "nVidia nForce3 Pro" },
1444      { ATA_NFORCE3_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 MCP" },
1445      { ATA_NFORCE3_MCP_S1, 0, 0,      NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
1446      { ATA_NFORCE3_MCP_S2, 0, 0,      NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
1447      { ATA_NFORCE4,     0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce4" },
1448      { ATA_NFORCE4_S1,  0, 0,         NV4OFF, ATA_SA150, "nVidia nForce4" },
1449      { ATA_NFORCE4_S2,  0, 0,         NV4OFF, ATA_SA150, "nVidia nForce4" },
1450      { 0, 0, 0, 0, 0, 0}};
1451     char buffer[64];
1452
1453     if (!(idx = ata_match_chip(dev, ids)))
1454         return ENXIO;
1455
1456     sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1457     device_set_desc_copy(dev, buffer);
1458     ctlr->chip = idx;
1459     ctlr->chipinit = ata_nvidia_chipinit;
1460     return 0;
1461 }
1462
1463 static int
1464 ata_nvidia_chipinit(device_t dev)
1465 {
1466     struct ata_pci_controller *ctlr = device_get_softc(dev);
1467
1468     if (ata_setup_interrupt(dev))
1469         return ENXIO;
1470
1471     if (ctlr->chip->max_dma >= ATA_SA150) {
1472         if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
1473             ctlr->r_type2 = SYS_RES_IOPORT;
1474         else
1475             ctlr->r_type2 = SYS_RES_MEMORY;
1476         ctlr->r_rid2 = PCIR_BAR(5);
1477         if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1478                                                    &ctlr->r_rid2, RF_ACTIVE))) {
1479             if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
1480                 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1481                                 ata_nvidia_intr, ctlr, &ctlr->handle)) {
1482                 device_printf(dev, "unable to setup interrupt\n");
1483                 return ENXIO;
1484             }
1485             pci_write_config(dev, PCIR_COMMAND,
1486                              pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
1487             ctlr->allocate = ata_nvidia_allocate;
1488             ctlr->reset = ata_nvidia_reset;
1489         }
1490         ctlr->setmode = ata_sata_setmode;
1491     }
1492     else {
1493         /* disable prefetch, postwrite */
1494         pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
1495         ctlr->setmode = ata_via_family_setmode;
1496     }
1497     return 0;
1498 }
1499
1500 static int
1501 ata_nvidia_allocate(device_t dev)
1502 {
1503     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1504     struct ata_channel *ch = device_get_softc(dev);
1505     int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0441 : 0x0011;
1506
1507     /* setup the usual register normal pci style */
1508     ata_pci_allocate(dev);
1509
1510     ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
1511     ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
1512     ch->r_io[ATA_SERROR].res = ctlr->r_res2;
1513     ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
1514     ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
1515     ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
1516     ch->flags |= ATA_NO_SLAVE;
1517
1518     /* enable PHY state change interrupts */
1519     ATA_OUTB(ctlr->r_res2, offset,
1520              ATA_INB(ctlr->r_res2, offset) | (0x0c << (ch->unit << 2)));
1521     return 0;
1522 }
1523
1524 static void 
1525 ata_nvidia_intr(void *data)
1526 {
1527     struct ata_pci_controller *ctlr = data;
1528     struct ata_channel *ch;
1529     int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
1530     u_int8_t status;
1531     int unit;
1532
1533     /* implement this as a toggle instead to balance load XXX */
1534     for (unit = 0; unit < ctlr->channels; unit++) {
1535         if (!(ch = ctlr->interrupt[unit].argument))
1536             continue;
1537
1538         if ((status = ATA_INB(ctlr->r_res2, offset))) {
1539             u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
1540             struct ata_connect_task *tp;
1541
1542             /* clear error bits/interrupt */
1543             ATA_IDX_OUTL(ch, ATA_SERROR, error);
1544             ATA_OUTB(ctlr->r_res2, offset, status);
1545
1546             /* check for and handle connect events */
1547             if ((status & (0x04 << (ch->unit << 2))) &&
1548                 (tp = (struct ata_connect_task *)
1549                       malloc(sizeof(struct ata_connect_task),
1550                              M_ATA, M_NOWAIT | M_ZERO))) {
1551
1552                 device_printf(ch->dev, "CONNECT requested\n");
1553                 tp->action = ATA_C_ATTACH;
1554                 tp->dev = ch->dev;
1555                 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1556                 taskqueue_enqueue(taskqueue_thread, &tp->task);
1557
1558             }
1559
1560             /* check for and handle disconnect events */
1561             if ((status & (0x08 << (ch->unit << 2))) &&
1562                 (tp = (struct ata_connect_task *)
1563                       malloc(sizeof(struct ata_connect_task),
1564                            M_ATA, M_NOWAIT | M_ZERO))) {
1565
1566                 device_printf(ch->dev, "DISCONNECT requested\n");
1567                 tp->action = ATA_C_DETACH;
1568                 tp->dev = ch->dev;
1569                 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1570                 taskqueue_enqueue(taskqueue_thread, &tp->task);
1571             }
1572
1573             /* any drive action to take care of ? */
1574             if (status & (0x01 << (ch->unit << 2))) {
1575                 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
1576                     int bmstat = 
1577                         ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1578
1579                     if (!(bmstat & ATA_BMSTAT_INTERRUPT))
1580                         continue;
1581                     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat&~ATA_BMSTAT_ERROR);
1582                     DELAY(1);
1583                 }
1584                 ctlr->interrupt[unit].function(ch);
1585             }
1586         }
1587     }
1588 }
1589
1590 static void
1591 ata_nvidia_reset(device_t dev)
1592 {
1593     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1594     struct ata_channel *ch = device_get_softc(dev);
1595     int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0441 : 0x0011;
1596
1597     /* disable PHY state change interrupt */
1598     ATA_OUTB(ctlr->r_res2, offset,
1599              ATA_INB(ctlr->r_res2, offset) & (~0x0c << (ch->unit << 2)));
1600
1601     ata_sata_phy_enable(ch);
1602
1603     /* enable PHY state change interrupt */
1604     ATA_OUTB(ctlr->r_res2, offset,
1605              ATA_INB(ctlr->r_res2, offset) | (0x0c << (ch->unit << 2)));
1606 }
1607
1608
1609 /*
1610  * Promise chipset support functions
1611  */
1612 #define ATA_PDC_APKT_OFFSET     0x00000010 
1613 #define ATA_PDC_HPKT_OFFSET     0x00000040
1614 #define ATA_PDC_ASG_OFFSET      0x00000080
1615 #define ATA_PDC_LSG_OFFSET      0x000000c0
1616 #define ATA_PDC_HSG_OFFSET      0x00000100
1617 #define ATA_PDC_CHN_OFFSET      0x00000400
1618 #define ATA_PDC_BUF_BASE        0x00400000
1619 #define ATA_PDC_BUF_OFFSET      0x00100000
1620 #define ATA_PDC_MAX_HPKT        8
1621 #define ATA_PDC_WRITE_REG       0x00
1622 #define ATA_PDC_WRITE_CTL       0x0e
1623 #define ATA_PDC_WRITE_END       0x08
1624 #define ATA_PDC_WAIT_NBUSY      0x10
1625 #define ATA_PDC_WAIT_READY      0x18
1626 #define ATA_PDC_1B              0x20
1627 #define ATA_PDC_2B              0x40
1628
1629 struct host_packet {
1630 u_int32_t                       addr;
1631     TAILQ_ENTRY(host_packet)    chain;
1632 };
1633
1634 struct ata_promise_sx4 {
1635     struct mtx                  mtx;
1636     TAILQ_HEAD(, host_packet)   queue;
1637     int                         busy;
1638 };
1639
1640 int
1641 ata_promise_ident(device_t dev)
1642 {
1643     struct ata_pci_controller *ctlr = device_get_softc(dev);
1644     struct ata_chip_id *idx;
1645     static struct ata_chip_id ids[] =
1646     {{ ATA_PDC20246,  0, PROLD, 0x00,    ATA_UDMA2, "Promise PDC20246" },
1647      { ATA_PDC20262,  0, PRNEW, 0x00,    ATA_UDMA4, "Promise PDC20262" },
1648      { ATA_PDC20263,  0, PRNEW, 0x00,    ATA_UDMA4, "Promise PDC20263" },
1649      { ATA_PDC20265,  0, PRNEW, 0x00,    ATA_UDMA5, "Promise PDC20265" },
1650      { ATA_PDC20267,  0, PRNEW, 0x00,    ATA_UDMA5, "Promise PDC20267" },
1651      { ATA_PDC20268,  0, PRTX,  PRTX4,   ATA_UDMA5, "Promise PDC20268" },
1652      { ATA_PDC20269,  0, PRTX,  0x00,    ATA_UDMA6, "Promise PDC20269" },
1653      { ATA_PDC20270,  0, PRTX,  PRTX4,   ATA_UDMA5, "Promise PDC20270" },
1654      { ATA_PDC20271,  0, PRTX,  0x00,    ATA_UDMA6, "Promise PDC20271" },
1655      { ATA_PDC20275,  0, PRTX,  0x00,    ATA_UDMA6, "Promise PDC20275" },
1656      { ATA_PDC20276,  0, PRTX,  PRSX6K,  ATA_UDMA6, "Promise PDC20276" },
1657      { ATA_PDC20277,  0, PRTX,  0x00,    ATA_UDMA6, "Promise PDC20277" },
1658      { ATA_PDC20318,  0, PRMIO, PRSATA,  ATA_SA150, "Promise PDC20318" },
1659      { ATA_PDC20319,  0, PRMIO, PRSATA,  ATA_SA150, "Promise PDC20319" },
1660      { ATA_PDC20371,  0, PRMIO, PRCMBO,  ATA_SA150, "Promise PDC20371" },
1661      { ATA_PDC20375,  0, PRMIO, PRCMBO,  ATA_SA150, "Promise PDC20375" },
1662      { ATA_PDC20376,  0, PRMIO, PRCMBO,  ATA_SA150, "Promise PDC20376" },
1663      { ATA_PDC20377,  0, PRMIO, PRCMBO,  ATA_SA150, "Promise PDC20377" },
1664      { ATA_PDC20378,  0, PRMIO, PRCMBO,  ATA_SA150, "Promise PDC20378" },
1665      { ATA_PDC20379,  0, PRMIO, PRCMBO,  ATA_SA150, "Promise PDC20379" },
1666      { ATA_PDC20571,  0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20571" },
1667      { ATA_PDC20575,  0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20575" },
1668      { ATA_PDC20579,  0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20579" },
1669      { ATA_PDC20580,  0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20580" },
1670      { ATA_PDC20617,  0, PRMIO, PRPATA,  ATA_UDMA6, "Promise PDC20617" },
1671      { ATA_PDC20618,  0, PRMIO, PRPATA,  ATA_UDMA6, "Promise PDC20618" },
1672      { ATA_PDC20619,  0, PRMIO, PRPATA,  ATA_UDMA6, "Promise PDC20619" },
1673      { ATA_PDC20620,  0, PRMIO, PRPATA,  ATA_UDMA6, "Promise PDC20620" },
1674      { ATA_PDC20621,  0, PRMIO, PRSX4X,  ATA_UDMA5, "Promise PDC20621" },
1675      { ATA_PDC20622,  0, PRMIO, PRSX4X,  ATA_SA150, "Promise PDC20622" },
1676      { ATA_PDC40518,  0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40518" },
1677      { ATA_PDC40519,  0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40519" },
1678      { 0, 0, 0, 0, 0, 0}};
1679     char buffer[64];
1680     uintptr_t devid = 0;
1681
1682     if (!(idx = ata_match_chip(dev, ids)))
1683         return ENXIO;
1684
1685     /* if we are on a SuperTrak SX6000 dont attach */
1686     if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
1687         !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
1688                        GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
1689         devid == ATA_I960RM) 
1690         return ENXIO;
1691
1692     strcpy(buffer, idx->text);
1693
1694     /* if we are on a FastTrak TX4, adjust the interrupt resource */
1695     if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
1696         !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
1697                        GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
1698         ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
1699         static long start = 0, end = 0;
1700
1701         if (pci_get_slot(dev) == 1) {
1702             bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
1703             strcat(buffer, " (channel 0+1)");
1704         }
1705         else if (pci_get_slot(dev) == 2 && start && end) {
1706             bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
1707             start = end = 0;
1708             strcat(buffer, " (channel 2+3)");
1709         }
1710         else {
1711             start = end = 0;
1712         }
1713     }
1714     sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
1715     device_set_desc_copy(dev, buffer);
1716     ctlr->chip = idx;
1717     ctlr->chipinit = ata_promise_chipinit;
1718     return 0;
1719 }
1720
1721 static int
1722 ata_promise_chipinit(device_t dev)
1723 {
1724     struct ata_pci_controller *ctlr = device_get_softc(dev);
1725     int rid = ATA_IRQ_RID;
1726
1727     if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1728                                                RF_SHAREABLE | RF_ACTIVE))) {
1729         device_printf(dev, "unable to map interrupt\n");
1730         return ENXIO;
1731     }
1732
1733     if (ctlr->chip->max_dma >= ATA_SA150)
1734         ctlr->setmode = ata_sata_setmode;
1735     else
1736         ctlr->setmode = ata_promise_setmode;
1737
1738     switch  (ctlr->chip->cfg1) {
1739     case PRNEW:
1740         /* setup clocks */
1741         ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
1742
1743         ctlr->dmainit = ata_promise_new_dmainit;
1744         /* FALLTHROUGH */
1745
1746     case PROLD:
1747         /* enable burst mode */
1748         ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
1749
1750         if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1751                             ata_promise_old_intr, ctlr, &ctlr->handle))) {
1752             device_printf(dev, "unable to setup interrupt\n");
1753             return ENXIO;
1754         }
1755         break;
1756
1757     case PRTX:
1758         if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1759                             ata_promise_tx2_intr, ctlr, &ctlr->handle))) {
1760             device_printf(dev, "unable to setup interrupt\n");
1761             return ENXIO;
1762         }
1763         break;
1764
1765     case PRMIO:
1766 //      if (ctlr->r_res1)
1767 //          bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
1768         ctlr->r_type1 = SYS_RES_MEMORY;
1769         ctlr->r_rid1 = PCIR_BAR(4);
1770         if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
1771                                                     &ctlr->r_rid1, RF_ACTIVE)))
1772             return ENXIO;
1773
1774         ctlr->r_type2 = SYS_RES_MEMORY;
1775         ctlr->r_rid2 = PCIR_BAR(3);
1776         if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1777                                                     &ctlr->r_rid2, RF_ACTIVE))){
1778             bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
1779             return ENXIO;
1780         }
1781         ctlr->reset = ata_promise_mio_reset;
1782         ctlr->dmainit = ata_promise_mio_dmainit;
1783         ctlr->allocate = ata_promise_mio_allocate;
1784
1785         if (ctlr->chip->cfg2 == PRSX4X) {
1786             struct ata_promise_sx4 *hpkt;
1787             u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
1788
1789             if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1790                                 ata_promise_sx4_intr, ctlr, &ctlr->handle))) {
1791                 device_printf(dev, "unable to setup interrupt\n");
1792                 /* XXX SOS release resources */
1793                 return ENXIO;
1794             }
1795
1796             /* print info about cache memory */
1797             device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
1798                           (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
1799                           ((dimm >> 24) & 0xff),
1800                           ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
1801                           " ECC enabled" : "" );
1802
1803             /* adjust cache memory parameters */
1804             ATA_OUTL(ctlr->r_res2, 0x000c000c, 
1805                      (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
1806
1807             /* setup host packet controls */
1808             hpkt = malloc(sizeof(struct ata_promise_sx4),
1809                           M_TEMP, M_NOWAIT | M_ZERO);
1810             mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
1811             TAILQ_INIT(&hpkt->queue);
1812             hpkt->busy = 0; //hpkt->head = hpkt->tail = 0;
1813             device_set_ivars(dev, hpkt);
1814             ctlr->channels = 4;
1815             return 0;
1816         }
1817
1818         if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1819                             ata_promise_mio_intr, ctlr, &ctlr->handle))) {
1820             device_printf(dev, "unable to setup interrupt\n");
1821             /* XXX SOS release resources */
1822             return ENXIO;
1823         }
1824
1825         switch (ctlr->chip->cfg2) {
1826         case PRPATA:
1827             ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
1828                              ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
1829             break;
1830
1831         case PRCMBO:
1832             ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
1833             ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 3;
1834             break;
1835
1836         case PRSATA:
1837             ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
1838             ctlr->channels = 4;
1839             break;
1840
1841         case PRCMBO2:
1842             ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
1843             ctlr->channels = 3;
1844             break;
1845
1846         case PRSATA2:
1847             ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
1848             ctlr->channels = 4;
1849             break;
1850
1851         default:
1852             /* XXX SOS release resources */
1853             return ENXIO;
1854         }
1855     default:
1856         /* XXX SOS release resources */
1857         return ENXIO;
1858     }
1859     return 0;
1860 }
1861
1862 static int
1863 ata_promise_mio_allocate(device_t dev)
1864 {
1865     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1866     struct ata_channel *ch = device_get_softc(dev);
1867     int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
1868     int i;
1869  
1870     for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
1871         ch->r_io[i].res = ctlr->r_res2;
1872         ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7); 
1873     }
1874     ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
1875     ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
1876     ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
1877     ata_default_registers(dev);
1878     if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
1879         ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
1880         ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
1881         ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
1882         ch->r_io[ATA_SERROR].res = ctlr->r_res2;
1883         ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
1884         ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
1885         ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
1886         ch->flags |= ATA_NO_SLAVE;
1887     }
1888     ch->flags |= ATA_USE_16BIT;
1889
1890     ata_generic_hw(dev);
1891     if (offset)
1892         ch->hw.command = ata_promise_sx4_command;
1893     else
1894         ch->hw.command = ata_promise_mio_command;
1895     return 0;
1896 }
1897
1898 static void
1899 ata_promise_mio_intr(void *data)
1900 {
1901     struct ata_pci_controller *ctlr = data;
1902     struct ata_channel *ch;
1903     u_int32_t vector = 0, status = 0;
1904     int unit;
1905
1906     switch (ctlr->chip->cfg2) {
1907     case PRSATA:
1908     case PRCMBO:
1909         /* read and acknowledge interrupt(s) */
1910         vector = ATA_INL(ctlr->r_res2, 0x040);
1911
1912         /* read and clear interface status */
1913         status = ATA_INL(ctlr->r_res2, 0x06c);
1914         ATA_OUTL(ctlr->r_res2, 0x06c, status & 0x000000ff);
1915         break;
1916
1917     case PRSATA2:
1918     case PRCMBO2:
1919         /* read and acknowledge interrupt(s) */
1920         vector = ATA_INL(ctlr->r_res2, 0x040);
1921         ATA_OUTL(ctlr->r_res2, 0x040, vector & 0x0000ffff);
1922
1923         /* read and clear interface status */
1924         status = ATA_INL(ctlr->r_res2, 0x060);
1925         ATA_OUTL(ctlr->r_res2, 0x060, status & 0x000000ff);
1926         break;
1927     }
1928
1929     for (unit = 0; unit < ctlr->channels; unit++) {
1930
1931         if ((ch = ctlr->interrupt[unit].argument)) {
1932             struct ata_connect_task *tp;
1933
1934             /* check for and handle disconnect events */
1935             if ((status & (0x00000001 << unit)) &&
1936                 (tp = (struct ata_connect_task *)
1937                       malloc(sizeof(struct ata_connect_task),
1938                              M_ATA, M_NOWAIT | M_ZERO))) {
1939
1940                 if (bootverbose)
1941                     device_printf(ch->dev, "DISCONNECT requested\n");
1942                 tp->action = ATA_C_DETACH;
1943                 tp->dev = ch->dev;
1944                 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1945                 taskqueue_enqueue(taskqueue_thread, &tp->task);
1946             }
1947
1948             /* check for and handle connect events */
1949             if ((status & (0x00000010 << unit)) &&
1950                 (tp = (struct ata_connect_task *)
1951                       malloc(sizeof(struct ata_connect_task),
1952                              M_ATA, M_NOWAIT | M_ZERO))) {
1953
1954                 if (bootverbose)
1955                     device_printf(ch->dev, "CONNECT requested\n");
1956                 tp->action = ATA_C_ATTACH;
1957                 tp->dev = ch->dev;
1958                 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1959                 taskqueue_enqueue(taskqueue_thread, &tp->task);
1960             }
1961
1962             /* active interrupt(s) need to call the interrupt handler */
1963             if (vector & (1 << (unit + 1)))
1964                 if ((ch = ctlr->interrupt[unit].argument))
1965                     ctlr->interrupt[unit].function(ch);
1966         }
1967     }
1968 }
1969
1970 static void
1971 ata_promise_sx4_intr(void *data)
1972 {
1973     struct ata_pci_controller *ctlr = data;
1974     struct ata_channel *ch;
1975     u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
1976     int unit;
1977
1978     for (unit = 0; unit < ctlr->channels; unit++) {
1979         if (vector & (1 << (unit + 1)))
1980             if ((ch = ctlr->interrupt[unit].argument))
1981                 ctlr->interrupt[unit].function(ch);
1982         if (vector & (1 << (unit + 5)))
1983             if ((ch = ctlr->interrupt[unit].argument))
1984                 ata_promise_queue_hpkt(ctlr,
1985                                        htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
1986                                                ATA_PDC_HPKT_OFFSET));
1987         if (vector & (1 << (unit + 9))) {
1988             ata_promise_next_hpkt(ctlr);
1989             if ((ch = ctlr->interrupt[unit].argument))
1990                 ctlr->interrupt[unit].function(ch);
1991         }
1992         if (vector & (1 << (unit + 13))) {
1993             ata_promise_next_hpkt(ctlr);
1994             if ((ch = ctlr->interrupt[unit].argument))
1995                 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
1996                          htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
1997                          ATA_PDC_APKT_OFFSET));
1998         }
1999     }
2000 }
2001
2002 static void
2003 ata_promise_mio_dmainit(device_t dev)
2004 {
2005     struct ata_channel *ch = device_get_softc(dev);
2006
2007     ata_dmainit(dev);
2008     if (ch->dma) {
2009         /* note start and stop are not used here */
2010     }
2011 }
2012
2013 static void
2014 ata_promise_mio_reset(device_t dev)
2015 {
2016     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2017     struct ata_channel *ch = device_get_softc(dev);
2018     struct ata_promise_sx4 *hpktp;
2019
2020     switch (ctlr->chip->cfg2) {
2021     case PRSX4X:
2022
2023         /* softreset channel ATA module */
2024         hpktp = device_get_ivars(ctlr->dev);
2025         ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
2026         ata_udelay(1000);
2027         ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
2028                  (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
2029                   ~0x00003f9f) | (ch->unit + 1));
2030
2031         /* softreset HOST module XXX SOS what about other outstandings */
2032         mtx_lock(&hpktp->mtx);
2033         ATA_OUTL(ctlr->r_res2, 0xc012c,
2034                  (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
2035         DELAY(10);
2036         ATA_OUTL(ctlr->r_res2, 0xc012c,
2037                  (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
2038         hpktp->busy = 0;
2039         mtx_unlock(&hpktp->mtx);
2040         ata_generic_reset(dev);
2041         break;
2042
2043     case PRCMBO:
2044     case PRSATA:
2045         if ((ctlr->chip->cfg2 == PRSATA) ||
2046             ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2047
2048             /* mask plug/unplug intr */
2049             ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
2050         }
2051
2052         /* softreset channels ATA module */
2053         ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2054         ata_udelay(10000);
2055         ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2056                  (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2057                   ~0x00003f9f) | (ch->unit + 1));
2058
2059         if ((ctlr->chip->cfg2 == PRSATA) ||
2060             ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2061
2062             ata_sata_phy_enable(ch);
2063
2064             /* reset and enable plug/unplug intr */
2065             ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
2066         }
2067         else
2068             ata_generic_reset(dev);
2069         break;
2070
2071     case PRCMBO2:
2072     case PRSATA2:
2073         if ((ctlr->chip->cfg2 == PRSATA2) ||
2074             ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2075             /* set portmultiplier port */
2076             ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
2077
2078             /* mask plug/unplug intr */
2079             ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
2080         }
2081
2082         /* softreset channels ATA module */
2083         ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2084         ata_udelay(10000);
2085         ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2086                  (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2087                   ~0x00003f9f) | (ch->unit + 1));
2088
2089         if ((ctlr->chip->cfg2 == PRSATA2) ||
2090             ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2091
2092             /* set PHY mode to "improved" */
2093             ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
2094                      (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
2095                      ~0x00000003) | 0x00000001);
2096
2097             ata_sata_phy_enable(ch);
2098
2099             /* reset and enable plug/unplug intr */
2100             ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
2101
2102             /* set portmultiplier port */
2103             ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
2104         }
2105         else
2106             ata_generic_reset(dev);
2107         break;
2108
2109     }
2110 }
2111
2112 static int
2113 ata_promise_mio_command(struct ata_request *request)
2114 {
2115     struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2116     struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2117     u_int32_t *wordp = (u_int32_t *)ch->dma->work;
2118
2119     ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
2120
2121     /* XXX SOS add ATAPI commands support later */
2122     switch (request->u.ata.command) {
2123     default:
2124         return ata_generic_command(request);
2125
2126     case ATA_READ_DMA:
2127         wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
2128         break;
2129
2130     case ATA_WRITE_DMA:
2131         wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
2132         break;
2133     }
2134     wordp[1] = htole32(ch->dma->sg_bus);
2135     wordp[2] = 0;
2136     ata_promise_apkt((u_int8_t*)wordp, request);
2137
2138     ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
2139     return 0;
2140 }
2141
2142 static int
2143 ata_promise_sx4_command(struct ata_request *request)
2144 {
2145     device_t gparent = GRANDPARENT(request->dev);
2146     struct ata_pci_controller *ctlr = device_get_softc(gparent);
2147     struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2148     struct ata_dma_prdentry *prd = ch->dma->sg;
2149     caddr_t window = rman_get_virtual(ctlr->r_res1);
2150     u_int32_t *wordp;
2151     int i, idx, length = 0;
2152
2153     /* XXX SOS add ATAPI commands support later */
2154     switch (request->u.ata.command) {    
2155
2156     default:
2157         return -1;
2158
2159     case ATA_ATA_IDENTIFY:
2160     case ATA_READ:
2161     case ATA_READ_MUL:
2162     case ATA_WRITE:
2163     case ATA_WRITE_MUL:
2164         ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2165         return ata_generic_command(request);
2166
2167     case ATA_SETFEATURES:
2168     case ATA_FLUSHCACHE:
2169     case ATA_SLEEP:
2170     case ATA_SET_MULTI:
2171         wordp = (u_int32_t *)
2172             (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2173         wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
2174         wordp[1] = 0;
2175         wordp[2] = 0;
2176         ata_promise_apkt((u_int8_t *)wordp, request);
2177         ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2178         ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2179         ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2180                  htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
2181         return 0;
2182
2183     case ATA_READ_DMA:
2184     case ATA_WRITE_DMA:
2185         wordp = (u_int32_t *)
2186             (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
2187         i = idx = 0;
2188         do {
2189             wordp[idx++] = prd[i].addr;
2190             wordp[idx++] = prd[i].count;
2191             length += (prd[i].count & ~ATA_DMA_EOT);
2192         } while (!(prd[i++].count & ATA_DMA_EOT));
2193
2194         wordp = (u_int32_t *)
2195             (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
2196         wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2197         wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
2198
2199         wordp = (u_int32_t *)
2200             (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
2201         wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2202         wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
2203
2204         wordp = (u_int32_t *)
2205             (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
2206         if (request->flags & ATA_R_READ)
2207             wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
2208         if (request->flags & ATA_R_WRITE)
2209             wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
2210         wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
2211         wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
2212         wordp[3] = 0;
2213
2214         wordp = (u_int32_t *)
2215             (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2216         if (request->flags & ATA_R_READ)
2217             wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
2218         if (request->flags & ATA_R_WRITE)
2219             wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
2220         wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
2221         wordp[2] = 0;
2222         ata_promise_apkt((u_int8_t *)wordp, request);
2223         ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2224
2225         if (request->flags & ATA_R_READ) {
2226             ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
2227             ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
2228             ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2229                 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
2230         }
2231         if (request->flags & ATA_R_WRITE) {
2232             ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
2233             ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
2234             ata_promise_queue_hpkt(ctlr,
2235                 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
2236         }
2237         return 0;
2238     }
2239 }
2240
2241 static int
2242 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
2243
2244     struct ata_device *atadev = device_get_softc(request->dev);
2245     u_int8_t command;
2246     int i = 12;
2247
2248     bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
2249     bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
2250     bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
2251     bytep[i++] = ATA_A_4BIT;
2252
2253     command = ata_modify_if_48bit(request);
2254
2255     if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2256         bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
2257         bytep[i++] = request->u.ata.feature >> 8;
2258         bytep[i++] = request->u.ata.feature;
2259         bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
2260         bytep[i++] = request->u.ata.count >> 8;
2261         bytep[i++] = request->u.ata.count;
2262         bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
2263         bytep[i++] = request->u.ata.lba >> 24;
2264         bytep[i++] = request->u.ata.lba;
2265         bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
2266         bytep[i++] = request->u.ata.lba >> 32;
2267         bytep[i++] = request->u.ata.lba >> 8;
2268         bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
2269         bytep[i++] = request->u.ata.lba >> 40;
2270         bytep[i++] = request->u.ata.lba >> 16;
2271         bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
2272         bytep[i++] = ATA_D_LBA | atadev->unit;
2273     }
2274     else {
2275         bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
2276         bytep[i++] = request->u.ata.feature;
2277         bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
2278         bytep[i++] = request->u.ata.count;
2279         bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
2280         bytep[i++] = request->u.ata.lba;
2281         bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
2282         bytep[i++] = request->u.ata.lba >> 8;
2283         bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
2284         bytep[i++] = request->u.ata.lba >> 16;
2285         bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
2286         bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
2287                    ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
2288     }
2289     bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
2290     bytep[i++] = command;
2291     return i;
2292 }
2293
2294 static void
2295 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
2296 {
2297     struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
2298
2299     mtx_lock(&hpktp->mtx);
2300     if (hpktp->busy) {
2301         struct host_packet *hp = 
2302             malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
2303         hp->addr = hpkt;
2304         TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
2305     }
2306     else {
2307         hpktp->busy = 1;
2308         ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
2309     }
2310     mtx_unlock(&hpktp->mtx);
2311 }
2312
2313 static void
2314 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
2315 {
2316     struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
2317     struct host_packet *hp;
2318
2319     mtx_lock(&hpktp->mtx);
2320     if ((hp = TAILQ_FIRST(&hpktp->queue))) {
2321         TAILQ_REMOVE(&hpktp->queue, hp, chain);
2322         ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
2323         free(hp, M_TEMP);
2324     }
2325     else
2326         hpktp->busy = 0;
2327     mtx_unlock(&hpktp->mtx);
2328 }
2329
2330 static void
2331 ata_promise_tx2_intr(void *data)
2332 {
2333     struct ata_pci_controller *ctlr = data;
2334     struct ata_channel *ch;
2335     int unit;
2336
2337     /* implement this as a toggle instead to balance load XXX */
2338     for (unit = 0; unit < ctlr->channels; unit++) {
2339         if (!(ch = ctlr->interrupt[unit].argument))
2340             continue;
2341         ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
2342         if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
2343             if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2344                 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2345
2346                 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2347                     ATA_BMSTAT_INTERRUPT)
2348                     continue;
2349                 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2350                 DELAY(1);
2351             }
2352             ctlr->interrupt[unit].function(ch);
2353         }
2354     }
2355 }
2356
2357 static void
2358 ata_promise_old_intr(void *data)
2359 {
2360     struct ata_pci_controller *ctlr = data;
2361     struct ata_channel *ch;
2362     int unit;
2363
2364     /* implement this as a toggle instead to balance load XXX */
2365     for (unit = 0; unit < ctlr->channels; unit++) {
2366         if (!(ch = ctlr->interrupt[unit].argument))
2367             continue;
2368         if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)){
2369             if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2370                 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2371
2372                 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2373                     ATA_BMSTAT_INTERRUPT)
2374                     continue;
2375                 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2376                 DELAY(1);
2377             }
2378             ctlr->interrupt[unit].function(ch);
2379         }
2380     }
2381 }
2382
2383 static int
2384 ata_promise_new_dmastart(device_t dev)
2385 {
2386     struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2387     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2388     struct ata_device *atadev  = device_get_softc(dev);
2389
2390     if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2391         ATA_OUTB(ctlr->r_res1, 0x11,
2392                  ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
2393         ATA_OUTL(ctlr->r_res1, 0x20,
2394                  ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
2395                  (ch->dma->cur_iosize >> 1));
2396     }
2397     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
2398                  (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
2399     ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
2400     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2401                  ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
2402                  ATA_BMCMD_START_STOP);
2403     ch->flags |= ATA_DMA_ACTIVE;
2404     return 0;
2405 }
2406
2407 static int
2408 ata_promise_new_dmastop(device_t dev)
2409 {
2410     struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2411     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2412     struct ata_device *atadev  = device_get_softc(dev);
2413     int error;
2414
2415     if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2416         ATA_OUTB(ctlr->r_res1, 0x11,
2417                  ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
2418         ATA_OUTL(ctlr->r_res1, 0x20, 0);
2419     }
2420     error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
2421     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2422                  ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
2423     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR); 
2424     ch->flags &= ~ATA_DMA_ACTIVE;
2425     return error;
2426 }
2427
2428 static void
2429 ata_promise_new_dmareset(device_t dev)
2430 {
2431     struct ata_channel *ch = device_get_softc(dev);
2432
2433     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2434                  ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
2435     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR); 
2436     ch->flags &= ~ATA_DMA_ACTIVE;
2437 }
2438
2439 static void
2440 ata_promise_new_dmainit(device_t dev)
2441 {
2442     struct ata_channel *ch = device_get_softc(dev);
2443
2444     ata_dmainit(dev);
2445     if (ch->dma) {
2446         ch->dma->start = ata_promise_new_dmastart;
2447         ch->dma->stop = ata_promise_new_dmastop;
2448         ch->dma->reset = ata_promise_new_dmareset;
2449     }
2450 }
2451
2452 static void
2453 ata_promise_setmode(device_t dev, int mode)
2454 {
2455     device_t gparent = GRANDPARENT(dev);
2456     struct ata_pci_controller *ctlr = device_get_softc(gparent);
2457     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2458     struct ata_device *atadev = device_get_softc(dev);
2459     int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2460     int error;
2461     u_int32_t timings33[][2] = {
2462     /*    PROLD       PRNEW                mode */
2463         { 0x004ff329, 0x004fff2f },     /* PIO 0 */
2464         { 0x004fec25, 0x004ff82a },     /* PIO 1 */
2465         { 0x004fe823, 0x004ff026 },     /* PIO 2 */
2466         { 0x004fe622, 0x004fec24 },     /* PIO 3 */
2467         { 0x004fe421, 0x004fe822 },     /* PIO 4 */
2468         { 0x004567f3, 0x004acef6 },     /* MWDMA 0 */
2469         { 0x004467f3, 0x0048cef6 },     /* MWDMA 1 */
2470         { 0x004367f3, 0x0046cef6 },     /* MWDMA 2 */
2471         { 0x004367f3, 0x0046cef6 },     /* UDMA 0 */
2472         { 0x004247f3, 0x00448ef6 },     /* UDMA 1 */
2473         { 0x004127f3, 0x00436ef6 },     /* UDMA 2 */
2474         { 0,          0x00424ef6 },     /* UDMA 3 */
2475         { 0,          0x004127f3 },     /* UDMA 4 */
2476         { 0,          0x004127f3 }      /* UDMA 5 */
2477     };
2478
2479     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2480
2481     switch (ctlr->chip->cfg1) {
2482     case PROLD:
2483     case PRNEW:
2484         if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
2485                                  (ch->unit ? 1 << 11 : 1 << 10))) {
2486             ata_print_cable(dev, "controller");
2487             mode = ATA_UDMA2;
2488         }
2489         if (ata_atapi(dev) && mode > ATA_PIO_MAX)
2490             mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
2491         break;
2492
2493     case PRTX:
2494         ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
2495         if (mode > ATA_UDMA2 &&
2496             ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
2497             ata_print_cable(dev, "controller");
2498             mode = ATA_UDMA2;
2499         }
2500         break;
2501    
2502     case PRMIO:
2503         if (mode > ATA_UDMA2 &&
2504             (ATA_INL(ctlr->r_res2,
2505                      (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
2506                      (ch->unit << 7)) & 0x01000000)) {
2507             ata_print_cable(dev, "controller");
2508             mode = ATA_UDMA2;
2509         }
2510         break;
2511     }
2512
2513     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2514
2515     if (bootverbose)
2516         device_printf(dev, "%ssetting %s on %s chip\n",
2517                      (error) ? "FAILURE " : "",
2518                      ata_mode2str(mode), ctlr->chip->text);
2519     if (!error) {
2520         if (ctlr->chip->cfg1 < PRTX)
2521             pci_write_config(gparent, 0x60 + (devno << 2),
2522                              timings33[ctlr->chip->cfg1][ata_mode2idx(mode)],4);
2523         atadev->mode = mode;
2524     }
2525     return;
2526 }
2527
2528
2529 /*
2530  * ServerWorks chipset support functions
2531  */
2532 int
2533 ata_serverworks_ident(device_t dev)
2534 {
2535     struct ata_pci_controller *ctlr = device_get_softc(dev);
2536     struct ata_chip_id *idx;
2537     static struct ata_chip_id ids[] =
2538     {{ ATA_ROSB4,  0x00, SWKS33,  0x00, ATA_UDMA2, "ServerWorks ROSB4" },
2539      { ATA_CSB5,   0x92, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB5" },
2540      { ATA_CSB5,   0x00, SWKS66,  0x00, ATA_UDMA4, "ServerWorks CSB5" },
2541      { ATA_CSB6,   0x00, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB6" },
2542      { ATA_CSB6_1, 0x00, SWKS66,  0x00, ATA_UDMA4, "ServerWorks CSB6" },
2543      { 0, 0, 0, 0, 0, 0}};
2544     char buffer[64];
2545
2546     if (!(idx = ata_match_chip(dev, ids)))
2547         return ENXIO;
2548
2549     sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
2550     device_set_desc_copy(dev, buffer);
2551     ctlr->chip = idx;
2552     ctlr->chipinit = ata_serverworks_chipinit;
2553     return 0;
2554 }
2555
2556 static int
2557 ata_serverworks_chipinit(device_t dev)
2558 {
2559     struct ata_pci_controller *ctlr = device_get_softc(dev);
2560
2561     if (ata_setup_interrupt(dev))
2562         return ENXIO;
2563
2564     if (ctlr->chip->cfg1 == SWKS33) {
2565         device_t *children;
2566         int nchildren, i;
2567
2568         /* locate the ISA part in the southbridge and enable UDMA33 */
2569         if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
2570             for (i = 0; i < nchildren; i++) {
2571                 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
2572                     pci_write_config(children[i], 0x64,
2573                                      (pci_read_config(children[i], 0x64, 4) &
2574                                       ~0x00002000) | 0x00004000, 4);
2575                     break;
2576                 }
2577             }
2578             free(children, M_TEMP);
2579         }
2580     }
2581     else {
2582         pci_write_config(dev, 0x5a,
2583                          (pci_read_config(dev, 0x5a, 1) & ~0x40) |
2584                          (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
2585     }
2586     ctlr->setmode = ata_serverworks_setmode;
2587     return 0;
2588 }
2589
2590 static void
2591 ata_serverworks_setmode(device_t dev, int mode)
2592 {
2593     device_t gparent = GRANDPARENT(dev);
2594     struct ata_pci_controller *ctlr = device_get_softc(gparent);
2595     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2596     struct ata_device *atadev = device_get_softc(dev);
2597     int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2598     int offset = (devno ^ 0x01) << 3;
2599     int error;
2600     u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
2601                               0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
2602     u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
2603
2604     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2605
2606     mode = ata_check_80pin(dev, mode);
2607
2608     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2609
2610     if (bootverbose)
2611         device_printf(dev, "%ssetting %s on %s chip\n",
2612                       (error) ? "FAILURE " : "",
2613                       ata_mode2str(mode), ctlr->chip->text);
2614     if (!error) {
2615         if (mode >= ATA_UDMA0) {
2616             pci_write_config(gparent, 0x56, 
2617                              (pci_read_config(gparent, 0x56, 2) &
2618                               ~(0xf << (devno << 2))) |
2619                              ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
2620             pci_write_config(gparent, 0x54,
2621                              pci_read_config(gparent, 0x54, 1) |
2622                              (0x01 << devno), 1);
2623             pci_write_config(gparent, 0x44, 
2624                              (pci_read_config(gparent, 0x44, 4) &
2625                               ~(0xff << offset)) |
2626                              (dmatimings[2] << offset), 4);
2627         }
2628         else if (mode >= ATA_WDMA0) {
2629             pci_write_config(gparent, 0x54,
2630                              pci_read_config(gparent, 0x54, 1) &
2631                               ~(0x01 << devno), 1);
2632             pci_write_config(gparent, 0x44, 
2633                              (pci_read_config(gparent, 0x44, 4) &
2634                               ~(0xff << offset)) |
2635                              (dmatimings[mode & ATA_MODE_MASK] << offset),4);
2636         }
2637         else
2638             pci_write_config(gparent, 0x54,
2639                              pci_read_config(gparent, 0x54, 1) &
2640                              ~(0x01 << devno), 1);
2641
2642         pci_write_config(gparent, 0x40, 
2643                          (pci_read_config(gparent, 0x40, 4) &
2644                           ~(0xff << offset)) |
2645                          (piotimings[ata_mode2idx(mode)] << offset), 4);
2646         atadev->mode = mode;
2647     }
2648 }
2649
2650
2651 /*
2652  * Silicon Image Inc. (SiI) (former CMD) chipset support functions
2653  */
2654 int
2655 ata_sii_ident(device_t dev)
2656 {
2657     struct ata_pci_controller *ctlr = device_get_softc(dev);
2658     struct ata_chip_id *idx;
2659     static struct ata_chip_id ids[] =
2660     {{ ATA_SII3114,   0x00, SIIMEMIO, SII4CH,    ATA_SA150, "SiI 3114" },
2661      { ATA_SII3512,   0x02, SIIMEMIO, 0,         ATA_SA150, "SiI 3512" },
2662      { ATA_SII3112,   0x02, SIIMEMIO, 0,         ATA_SA150, "SiI 3112" },
2663      { ATA_SII3112_1, 0x02, SIIMEMIO, 0,         ATA_SA150, "SiI 3112" },
2664      { ATA_SII3512,   0x00, SIIMEMIO, SIIBUG,    ATA_SA150, "SiI 3512" },
2665      { ATA_SII3112,   0x00, SIIMEMIO, SIIBUG,    ATA_SA150, "SiI 3112" },
2666      { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG,    ATA_SA150, "SiI 3112" },
2667      { ATA_SII0680,   0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
2668      { ATA_CMD649,    0x00, 0,        SIIINTR,   ATA_UDMA5, "CMD 649" },
2669      { ATA_CMD648,    0x00, 0,        SIIINTR,   ATA_UDMA4, "CMD 648" },
2670      { ATA_CMD646,    0x07, 0,        0,         ATA_UDMA2, "CMD 646U2" },
2671      { ATA_CMD646,    0x00, 0,        0,         ATA_WDMA2, "CMD 646" },
2672      { 0, 0, 0, 0, 0, 0}};
2673     char buffer[64];
2674
2675     if (!(idx = ata_match_chip(dev, ids)))
2676         return ENXIO;
2677
2678     sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
2679     device_set_desc_copy(dev, buffer);
2680     ctlr->chip = idx;
2681     ctlr->chipinit = ata_sii_chipinit;
2682     return 0;
2683 }
2684
2685 static int
2686 ata_sii_chipinit(device_t dev)
2687 {
2688     struct ata_pci_controller *ctlr = device_get_softc(dev);
2689     int rid = ATA_IRQ_RID;
2690
2691     if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2692                                                RF_SHAREABLE | RF_ACTIVE))) {
2693         device_printf(dev, "unable to map interrupt\n");
2694         return ENXIO;
2695     }
2696
2697     if (ctlr->chip->cfg1 == SIIMEMIO) {
2698         if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2699                             ata_sii_intr, ctlr, &ctlr->handle))) {
2700             device_printf(dev, "unable to setup interrupt\n");
2701             return ENXIO;
2702         }
2703
2704         ctlr->r_type2 = SYS_RES_MEMORY;
2705         ctlr->r_rid2 = PCIR_BAR(5);
2706         if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2707                                                     &ctlr->r_rid2, RF_ACTIVE)))
2708             return ENXIO;
2709
2710         if (ctlr->chip->cfg2 & SIISETCLK) {
2711             if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
2712                 pci_write_config(dev, 0x8a, 
2713                                  (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
2714             if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
2715                 device_printf(dev, "%s could not set ATA133 clock\n",
2716                               ctlr->chip->text);
2717         }
2718
2719         /* if we have 4 channels enable the second set */
2720         if (ctlr->chip->cfg2 & SII4CH) {
2721             ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
2722             ctlr->channels = 4;
2723         }
2724
2725         /* enable PCI interrupt as BIOS might not */
2726         pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
2727
2728         /* dont block interrupts from any channel */
2729         pci_write_config(dev, 0x48,
2730                          (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
2731
2732         ctlr->allocate = ata_sii_allocate;
2733         if (ctlr->chip->max_dma >= ATA_SA150) {
2734             ctlr->reset = ata_sii_reset;
2735             ctlr->setmode = ata_sata_setmode;
2736         }
2737         else
2738             ctlr->setmode = ata_sii_setmode;
2739     }
2740     else {
2741         if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2742                             ctlr->chip->cfg2 & SIIINTR ? 
2743                             ata_cmd_intr : ata_cmd_old_intr,
2744                             ctlr, &ctlr->handle))) {
2745             device_printf(dev, "unable to setup interrupt\n");
2746             return ENXIO;
2747         }
2748
2749         if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
2750             device_printf(dev, "HW has secondary channel disabled\n");
2751             ctlr->channels = 1;
2752         }    
2753
2754         /* enable interrupt as BIOS might not */
2755         pci_write_config(dev, 0x71, 0x01, 1);
2756
2757         ctlr->setmode = ata_cmd_setmode;
2758     }
2759     return 0;
2760 }
2761
2762 static int
2763 ata_sii_allocate(device_t dev)
2764 {
2765     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2766     struct ata_channel *ch = device_get_softc(dev);
2767     int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
2768     int i;
2769
2770     for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2771         ch->r_io[i].res = ctlr->r_res2;
2772         ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
2773     }
2774     ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
2775     ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
2776     ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
2777     ata_default_registers(dev);
2778     ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
2779     ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
2780     ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
2781     ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
2782     ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
2783     ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
2784     ch->r_io[ATA_BMDEVSPEC_0].res = ctlr->r_res2;
2785     ch->r_io[ATA_BMDEVSPEC_0].offset = 0xa1 + (unit01 << 6) + (unit10 << 8);
2786
2787     if (ctlr->chip->max_dma >= ATA_SA150) {
2788         ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2789         ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
2790         ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2791         ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
2792         ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2793         ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
2794         ch->flags |= ATA_NO_SLAVE;
2795
2796         /* enable PHY state change interrupt */
2797         ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
2798     }
2799
2800     if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
2801         /* work around errata in early chips */
2802         ch->dma->boundary = 16 * DEV_BSIZE;
2803         ch->dma->max_iosize = 15 * DEV_BSIZE;
2804     }
2805
2806     ata_generic_hw(dev);
2807     return 0;
2808 }
2809
2810 static void
2811 ata_sii_intr(void *data)
2812 {
2813     struct ata_pci_controller *ctlr = data;
2814     struct ata_channel *ch;
2815     int unit;
2816
2817     /* implement this as a toggle instead to balance load XXX */
2818     for (unit = 0; unit < ctlr->channels; unit++) {
2819         if (!(ch = ctlr->interrupt[unit].argument))
2820             continue;
2821
2822         /* check for PHY related interrupts on SATA capable HW */
2823         if (ctlr->chip->max_dma >= ATA_SA150) {
2824             u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
2825             u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
2826             struct ata_connect_task *tp;
2827
2828             if (error) {
2829                 /* clear error bits/interrupt */
2830                 ATA_IDX_OUTL(ch, ATA_SERROR, error);
2831
2832                 /* if we have a connection event deal with it */
2833                 if ((error & ATA_SE_PHY_CHANGED) &&
2834                     (tp = (struct ata_connect_task *)
2835                           malloc(sizeof(struct ata_connect_task),
2836                                  M_ATA, M_NOWAIT | M_ZERO))) {
2837
2838                     if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
2839                         device_printf(ch->dev, "CONNECT requested\n");
2840                         tp->action = ATA_C_ATTACH;
2841                     }
2842                     else {
2843                         device_printf(ch->dev, "DISCONNECT requested\n");
2844                         tp->action = ATA_C_DETACH;
2845                     }
2846                     tp->dev = ch->dev;
2847                     TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2848                     taskqueue_enqueue(taskqueue_thread, &tp->task);
2849                 }
2850             }
2851         }
2852
2853         /* any drive action to take care of ? */
2854         if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_0) & 0x08) {
2855             if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2856                 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2857
2858                 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
2859                     continue;
2860                 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2861                 DELAY(1);
2862             }
2863             ctlr->interrupt[unit].function(ch);
2864         }
2865
2866     }
2867 }
2868
2869 static void
2870 ata_cmd_intr(void *data)
2871 {
2872     struct ata_pci_controller *ctlr = data;
2873     struct ata_channel *ch;
2874     u_int8_t reg71;
2875     int unit;
2876
2877     /* implement this as a toggle instead to balance load XXX */
2878     for (unit = 0; unit < ctlr->channels; unit++) {
2879         if (!(ch = ctlr->interrupt[unit].argument))
2880             continue;
2881         if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
2882              (ch->unit ? 0x08 : 0x04))) {
2883             pci_write_config(device_get_parent(ch->dev), 0x71,
2884                              reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
2885             if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2886                 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2887
2888                 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2889                     ATA_BMSTAT_INTERRUPT)
2890                     continue;
2891                 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2892                 DELAY(1);
2893             }
2894             ctlr->interrupt[unit].function(ch);
2895         }
2896     }
2897 }
2898
2899 static void
2900 ata_cmd_old_intr(void *data)
2901 {
2902     struct ata_pci_controller *ctlr = data;
2903     struct ata_channel *ch;
2904     int unit;
2905
2906     /* implement this as a toggle instead to balance load XXX */
2907     for (unit = 0; unit < ctlr->channels; unit++) {
2908         if (!(ch = ctlr->interrupt[unit].argument))
2909             continue;
2910         if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2911             int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2912
2913             if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2914                 ATA_BMSTAT_INTERRUPT)
2915                 continue;
2916             ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2917             DELAY(1);
2918         }
2919         ctlr->interrupt[unit].function(ch);
2920     }
2921 }
2922
2923 static void
2924 ata_sii_reset(device_t dev)
2925 {
2926     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2927     struct ata_channel *ch = device_get_softc(dev);
2928     int offset = ((ch->unit & 1) << 7) + ((ch->unit & 2) << 8);
2929
2930     /* disable PHY state change interrupt */
2931     ATA_OUTL(ctlr->r_res2, 0x148 + offset, ~(1 << 16));
2932
2933     ata_sata_phy_enable(ch);
2934
2935     /* enable PHY state change interrupt */
2936     ATA_OUTL(ctlr->r_res2, 0x148 + offset, (1 << 16));
2937 }
2938
2939 static void
2940 ata_sii_setmode(device_t dev, int mode)
2941 {
2942     device_t gparent = GRANDPARENT(dev);
2943     struct ata_pci_controller *ctlr = device_get_softc(gparent);
2944     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2945     struct ata_device *atadev = device_get_softc(dev);
2946     int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
2947     int mreg = ch->unit ? 0x84 : 0x80;
2948     int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
2949     int mval = pci_read_config(gparent, mreg, 1) & ~mask;
2950     int error;
2951
2952     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2953
2954     if (ctlr->chip->cfg2 & SIISETCLK) {
2955         if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
2956                                  (ch->unit ? 0x02 : 0x01))) {
2957             ata_print_cable(dev, "controller");
2958             mode = ATA_UDMA2;
2959         }
2960     }
2961     else
2962         mode = ata_check_80pin(dev, mode);
2963
2964     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2965
2966     if (bootverbose)
2967         device_printf(dev, "%ssetting %s on %s chip\n",
2968                       (error) ? "FAILURE " : "",
2969                       ata_mode2str(mode), ctlr->chip->text);
2970     if (error)
2971         return;
2972
2973     if (mode >= ATA_UDMA0) {
2974         u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
2975         u_int8_t ureg = 0xac + rego;
2976
2977         pci_write_config(gparent, mreg,
2978                          mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
2979         pci_write_config(gparent, ureg, 
2980                          (pci_read_config(gparent, ureg, 1) & ~0x3f) |
2981                          udmatimings[mode & ATA_MODE_MASK], 1);
2982
2983     }
2984     else if (mode >= ATA_WDMA0) {
2985         u_int8_t dreg = 0xa8 + rego;
2986         u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
2987
2988         pci_write_config(gparent, mreg,
2989                          mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
2990         pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
2991
2992     }
2993     else {
2994         u_int8_t preg = 0xa4 + rego;
2995         u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
2996
2997         pci_write_config(gparent, mreg,
2998                          mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
2999         pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
3000     }
3001     atadev->mode = mode;
3002 }
3003
3004 static void
3005 ata_cmd_setmode(device_t dev, int mode)
3006 {
3007     device_t gparent = GRANDPARENT(dev);
3008     struct ata_pci_controller *ctlr = device_get_softc(gparent);
3009     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3010     struct ata_device *atadev = device_get_softc(dev);
3011     int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3012     int error;
3013
3014     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3015
3016     mode = ata_check_80pin(dev, mode);
3017
3018     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3019
3020     if (bootverbose)
3021         device_printf(dev, "%ssetting %s on %s chip\n",
3022                       (error) ? "FAILURE " : "",
3023                       ata_mode2str(mode), ctlr->chip->text);
3024     if (!error) {
3025         int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
3026         int ureg = ch->unit ? 0x7b : 0x73;
3027
3028         if (mode >= ATA_UDMA0) {        
3029             int udmatimings[][2] = { { 0x31,  0xc2 }, { 0x21,  0x82 },
3030                                      { 0x11,  0x42 }, { 0x25,  0x8a },
3031                                      { 0x15,  0x4a }, { 0x05,  0x0a } };
3032
3033             u_int8_t umode = pci_read_config(gparent, ureg, 1);
3034
3035             umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
3036             umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
3037             pci_write_config(gparent, ureg, umode, 1);
3038         }
3039         else if (mode >= ATA_WDMA0) { 
3040             int dmatimings[] = { 0x87, 0x32, 0x3f };
3041
3042             pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
3043             pci_write_config(gparent, ureg, 
3044                              pci_read_config(gparent, ureg, 1) &
3045                              ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3046         }
3047         else {
3048            int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
3049             pci_write_config(gparent, treg,
3050                              piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
3051             pci_write_config(gparent, ureg, 
3052                              pci_read_config(gparent, ureg, 1) &
3053                              ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3054         }
3055         atadev->mode = mode;
3056     }
3057 }
3058
3059
3060 /*
3061  * Silicon Integrated Systems Corp. (SiS) chipset support functions
3062  */
3063 int
3064 ata_sis_ident(device_t dev)
3065 {
3066     struct ata_pci_controller *ctlr = device_get_softc(dev);
3067     struct ata_chip_id *idx;
3068     static struct ata_chip_id ids[] =
3069     {{ ATA_SIS182,  0x00, SISSATA,   0, ATA_SA150, "SiS 182" }, /* south */
3070      { ATA_SIS181,  0x00, SISSATA,   0, ATA_SA150, "SiS 181" }, /* south */
3071      { ATA_SIS180,  0x00, SISSATA,   0, ATA_SA150, "SiS 180" }, /* south */
3072      { ATA_SIS965,  0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 965" }, /* south */
3073      { ATA_SIS964,  0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 964" }, /* south */
3074      { ATA_SIS963,  0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 963" }, /* south */
3075      { ATA_SIS962,  0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 962" }, /* south */
3076
3077      { ATA_SIS745,  0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 745" }, /* 1chip */
3078      { ATA_SIS735,  0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 735" }, /* 1chip */
3079      { ATA_SIS733,  0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 733" }, /* 1chip */
3080      { ATA_SIS730,  0x00, SIS100OLD, 0, ATA_UDMA5, "SiS 730" }, /* 1chip */
3081
3082      { ATA_SIS635,  0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 635" }, /* 1chip */
3083      { ATA_SIS633,  0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 633" }, /* unknown */
3084      { ATA_SIS630,  0x30, SIS100OLD, 0, ATA_UDMA5, "SiS 630S"}, /* 1chip */
3085      { ATA_SIS630,  0x00, SIS66,     0, ATA_UDMA4, "SiS 630" }, /* 1chip */
3086      { ATA_SIS620,  0x00, SIS66,     0, ATA_UDMA4, "SiS 620" }, /* 1chip */
3087
3088      { ATA_SIS550,  0x00, SIS66,     0, ATA_UDMA5, "SiS 550" },
3089      { ATA_SIS540,  0x00, SIS66,     0, ATA_UDMA4, "SiS 540" },
3090      { ATA_SIS530,  0x00, SIS66,     0, ATA_UDMA4, "SiS 530" },
3091
3092      { ATA_SIS5513, 0xc2, SIS33,     1, ATA_UDMA2, "SiS 5513" },
3093      { ATA_SIS5513, 0x00, SIS33,     1, ATA_WDMA2, "SiS 5513" },
3094      { 0, 0, 0, 0, 0, 0 }};
3095     char buffer[64];
3096     int found = 0;
3097
3098     if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev)))) 
3099         return ENXIO;
3100
3101     if (idx->cfg2 && !found) {
3102         u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
3103
3104         pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
3105         if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
3106             found = 1;
3107             idx->cfg1 = SIS133NEW;
3108             idx->max_dma = ATA_UDMA6;
3109             sprintf(buffer, "SiS 962/963 %s controller",
3110                     ata_mode2str(idx->max_dma));
3111         }
3112         pci_write_config(dev, 0x57, reg57, 1);
3113     }
3114     if (idx->cfg2 && !found) {
3115         u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
3116
3117         pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
3118         if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
3119             struct ata_chip_id id[] =
3120                 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
3121
3122             found = 1;
3123             if (ata_find_chip(dev, id, pci_get_slot(dev))) {
3124                 idx->cfg1 = SIS133OLD;
3125                 idx->max_dma = ATA_UDMA6;
3126             }
3127             else {
3128                 idx->cfg1 = SIS100NEW;
3129                 idx->max_dma = ATA_UDMA5;
3130             }
3131             sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
3132         }
3133         pci_write_config(dev, 0x4a, reg4a, 1);
3134     }
3135     if (!found)
3136         sprintf(buffer,"%s %s controller",idx->text,ata_mode2str(idx->max_dma));
3137
3138     device_set_desc_copy(dev, buffer);
3139     ctlr->chip = idx;
3140     ctlr->chipinit = ata_sis_chipinit;
3141     return 0;
3142 }
3143
3144 static int
3145 ata_sis_chipinit(device_t dev)
3146 {
3147     struct ata_pci_controller *ctlr = device_get_softc(dev);
3148
3149     if (ata_setup_interrupt(dev))
3150         return ENXIO;
3151     
3152     switch (ctlr->chip->cfg1) {
3153     case SIS33:
3154         break;
3155     case SIS66:
3156     case SIS100OLD:
3157         pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
3158         break;
3159     case SIS100NEW:
3160     case SIS133OLD:
3161         pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
3162         break;
3163     case SIS133NEW:
3164         pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
3165         pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
3166         break;
3167     case SISSATA:
3168         ctlr->r_type2 = SYS_RES_IOPORT;
3169         ctlr->r_rid2 = PCIR_BAR(5);
3170         if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3171                                                    &ctlr->r_rid2, RF_ACTIVE))) {
3172             pci_write_config(dev, PCIR_COMMAND,
3173                              pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3174             ctlr->allocate = ata_sis_allocate;
3175             ctlr->reset = ata_sis_reset;
3176         }
3177         ctlr->setmode = ata_sata_setmode;
3178         return 0;
3179     default:
3180         return ENXIO;
3181     }
3182     ctlr->setmode = ata_sis_setmode;
3183     return 0;
3184 }
3185
3186 static int
3187 ata_sis_allocate(device_t dev)
3188 {
3189     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3190     struct ata_channel *ch = device_get_softc(dev);
3191
3192     /* setup the usual register normal pci style */
3193     ata_pci_allocate(dev);
3194
3195     ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3196     ch->r_io[ATA_SSTATUS].offset = (ch->unit << 4);
3197     ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3198     ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 4);
3199     ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3200     ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 4);
3201     ch->flags |= ATA_NO_SLAVE;
3202
3203     /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
3204     /* XXX SOS unknown how to enable PHY state change interrupt */
3205     return 0;
3206 }
3207
3208 static void
3209 ata_sis_reset(device_t dev)
3210 {
3211     struct ata_channel *ch = device_get_softc(dev);
3212
3213     ata_sata_phy_enable(ch);
3214 }
3215
3216
3217 static void
3218 ata_sis_setmode(device_t dev, int mode)
3219 {
3220     device_t gparent = GRANDPARENT(dev);
3221     struct ata_pci_controller *ctlr = device_get_softc(gparent);
3222     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3223     struct ata_device *atadev = device_get_softc(dev);
3224     int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3225     int error;
3226
3227     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3228
3229     if (ctlr->chip->cfg1 == SIS133NEW) {
3230         if (mode > ATA_UDMA2 &&
3231             pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
3232             ata_print_cable(dev, "controller");
3233             mode = ATA_UDMA2;
3234         }
3235     }
3236     else {
3237         if (mode > ATA_UDMA2 &&
3238             pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
3239             ata_print_cable(dev, "controller");
3240             mode = ATA_UDMA2;
3241         }
3242     }
3243
3244     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3245
3246     if (bootverbose)
3247         device_printf(dev, "%ssetting %s on %s chip\n",
3248                       (error) ? "FAILURE " : "",
3249                       ata_mode2str(mode), ctlr->chip->text);
3250     if (!error) {
3251         switch (ctlr->chip->cfg1) {
3252         case SIS133NEW: {
3253             u_int32_t timings[] = 
3254                 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
3255                   0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
3256                   0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
3257             u_int32_t reg;
3258
3259             reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
3260             pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
3261             break;
3262             }
3263         case SIS133OLD: {
3264             u_int16_t timings[] =
3265              { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
3266                0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
3267                   
3268             u_int16_t reg = 0x40 + (devno << 1);
3269
3270             pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3271             break;
3272             }
3273         case SIS100NEW: {
3274             u_int16_t timings[] =
3275                 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
3276                   0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
3277             u_int16_t reg = 0x40 + (devno << 1);
3278
3279             pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3280             break;
3281             }
3282         case SIS100OLD:
3283         case SIS66:
3284         case SIS33: {
3285             u_int16_t timings[] =
3286                 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
3287                   0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
3288             u_int16_t reg = 0x40 + (devno << 1);
3289
3290             pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3291             break;
3292             }
3293         }
3294         atadev->mode = mode;
3295     }
3296 }
3297
3298
3299 /* VIA Technologies Inc. chipset support functions */
3300 int
3301 ata_via_ident(device_t dev)
3302 {
3303     struct ata_pci_controller *ctlr = device_get_softc(dev);
3304     struct ata_chip_id *idx;
3305     static struct ata_chip_id ids[] =
3306     {{ ATA_VIA82C586, 0x02, VIA33,  0x00,   ATA_UDMA2, "VIA 82C586B" },
3307      { ATA_VIA82C586, 0x00, VIA33,  0x00,   ATA_WDMA2, "VIA 82C586" },
3308      { ATA_VIA82C596, 0x12, VIA66,  VIACLK, ATA_UDMA4, "VIA 82C596B" },
3309      { ATA_VIA82C596, 0x00, VIA33,  0x00,   ATA_UDMA2, "VIA 82C596" },
3310      { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "VIA 82C686B"},
3311      { ATA_VIA82C686, 0x10, VIA66,  VIACLK, ATA_UDMA4, "VIA 82C686A" },
3312      { ATA_VIA82C686, 0x00, VIA33,  0x00,   ATA_UDMA2, "VIA 82C686" },
3313      { ATA_VIA8231,   0x00, VIA100, VIABUG, ATA_UDMA5, "VIA 8231" },
3314      { ATA_VIA8233,   0x00, VIA100, 0x00,   ATA_UDMA5, "VIA 8233" },
3315      { ATA_VIA8233C,  0x00, VIA100, 0x00,   ATA_UDMA5, "VIA 8233C" },
3316      { ATA_VIA8233A,  0x00, VIA133, 0x00,   ATA_UDMA6, "VIA 8233A" },
3317      { ATA_VIA8235,   0x00, VIA133, 0x00,   ATA_UDMA6, "VIA 8235" },
3318      { ATA_VIA8237,   0x00, VIA133, 0x00,   ATA_UDMA6, "VIA 8237" },
3319      { 0, 0, 0, 0, 0, 0 }};
3320     static struct ata_chip_id new_ids[] =
3321     {{ ATA_VIA6410,   0x00, 0,      0x00,   ATA_UDMA6, "VIA 6410" },
3322      { ATA_VIA6420,   0x00, 7,      0x00,   ATA_SA150, "VIA 6420" },
3323      { ATA_VIA6421,   0x00, 6,      0x00,   ATA_SA150, "VIA 6421" },
3324      { 0, 0, 0, 0, 0, 0 }};
3325     char buffer[64];
3326
3327     if (pci_get_devid(dev) == ATA_VIA82C571) {
3328         if (!(idx = ata_find_chip(dev, ids, -99))) 
3329             return ENXIO;
3330     }
3331     else {
3332         if (!(idx = ata_match_chip(dev, new_ids))) 
3333             return ENXIO;
3334     }
3335
3336     sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3337     device_set_desc_copy(dev, buffer);
3338     ctlr->chip = idx;
3339     ctlr->chipinit = ata_via_chipinit;
3340     return 0;
3341 }
3342
3343 static int
3344 ata_via_chipinit(device_t dev)
3345 {
3346     struct ata_pci_controller *ctlr = device_get_softc(dev);
3347
3348     if (ata_setup_interrupt(dev))
3349         return ENXIO;
3350     
3351     if (ctlr->chip->max_dma >= ATA_SA150) {
3352         ctlr->r_type2 = SYS_RES_IOPORT;
3353         ctlr->r_rid2 = PCIR_BAR(5);
3354         if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3355                                                    &ctlr->r_rid2, RF_ACTIVE))) {
3356             pci_write_config(dev, PCIR_COMMAND,
3357                              pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3358             ctlr->allocate = ata_via_allocate;
3359             ctlr->reset = ata_via_reset;
3360         }
3361         ctlr->setmode = ata_sata_setmode;
3362         return 0;
3363     }
3364
3365     /* prepare for ATA-66 on the 82C686a and 82C596b */
3366     if (ctlr->chip->cfg2 & VIACLK)
3367         pci_write_config(dev, 0x50, 0x030b030b, 4);       
3368
3369     /* the southbridge might need the data corruption fix */
3370     if (ctlr->chip->cfg2 & VIABUG)
3371         ata_via_southbridge_fixup(dev);
3372
3373     /* set fifo configuration half'n'half */
3374     pci_write_config(dev, 0x43, 
3375                      (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
3376
3377     /* set status register read retry */
3378     pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
3379
3380     /* set DMA read & end-of-sector fifo flush */
3381     pci_write_config(dev, 0x46, 
3382                      (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
3383
3384     /* set sector size */
3385     pci_write_config(dev, 0x60, DEV_BSIZE, 2);
3386     pci_write_config(dev, 0x68, DEV_BSIZE, 2);
3387
3388     ctlr->setmode = ata_via_family_setmode;
3389     return 0;
3390 }
3391
3392 static int
3393 ata_via_allocate(device_t dev)
3394 {
3395     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3396     struct ata_channel *ch = device_get_softc(dev);
3397
3398     /* setup the usual register normal pci style */
3399     ata_pci_allocate(dev);
3400
3401     ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3402     ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
3403     ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3404     ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
3405     ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3406     ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
3407     ch->flags |= ATA_NO_SLAVE;
3408
3409     /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
3410     /* XXX SOS unknown how to enable PHY state change interrupt */
3411     return 0;
3412 }
3413
3414 static void
3415 ata_via_reset(device_t dev)
3416 {
3417     struct ata_channel *ch = device_get_softc(dev);
3418
3419     ata_sata_phy_enable(ch);
3420 }
3421
3422 static void
3423 ata_via_southbridge_fixup(device_t dev)
3424 {
3425     device_t *children;
3426     int nchildren, i;
3427
3428     if (device_get_children(device_get_parent(dev), &children, &nchildren))
3429         return;
3430
3431     for (i = 0; i < nchildren; i++) {
3432         if (pci_get_devid(children[i]) == ATA_VIA8363 ||
3433             pci_get_devid(children[i]) == ATA_VIA8371 ||
3434             pci_get_devid(children[i]) == ATA_VIA8662 ||
3435             pci_get_devid(children[i]) == ATA_VIA8361) {
3436             u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
3437
3438             if ((reg76 & 0xf0) != 0xd0) {
3439                 device_printf(dev,
3440                 "Correcting VIA config for southbridge data corruption bug\n");
3441                 pci_write_config(children[i], 0x75, 0x80, 1);
3442                 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
3443             }
3444             break;
3445         }
3446     }
3447     free(children, M_TEMP);
3448 }
3449
3450
3451 /* common code for VIA, AMD & nVidia */
3452 static void
3453 ata_via_family_setmode(device_t dev, int mode)
3454 {
3455     device_t gparent = GRANDPARENT(dev);
3456     struct ata_pci_controller *ctlr = device_get_softc(gparent);
3457     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3458     struct ata_device *atadev = device_get_softc(dev);
3459     u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
3460                            0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
3461     int modes[][7] = {
3462         { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 },   /* VIA ATA33 */
3463         { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 },   /* VIA ATA66 */
3464         { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 },   /* VIA ATA100 */
3465         { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 },   /* VIA ATA133 */
3466         { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }};  /* AMD/nVIDIA */
3467     int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3468     int reg = 0x53 - devno;
3469     int error;
3470
3471     mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3472
3473     if (ctlr->chip->cfg2 & AMDCABLE) {
3474         if (mode > ATA_UDMA2 &&
3475             !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
3476             ata_print_cable(dev, "controller");
3477             mode = ATA_UDMA2;
3478         }
3479     }
3480     else 
3481         mode = ata_check_80pin(dev, mode);
3482
3483     if (ctlr->chip->cfg2 & NVIDIA)
3484         reg += 0x10;
3485
3486     if (ctlr->chip->cfg1 != VIA133)
3487         pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
3488
3489     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3490
3491     if (bootverbose)
3492         device_printf(dev, "%ssetting %s on %s chip\n",
3493                       (error) ? "FAILURE " : "", ata_mode2str(mode),
3494                       ctlr->chip->text);
3495     if (!error) {
3496         if (mode >= ATA_UDMA0)
3497             pci_write_config(gparent, reg,
3498                              modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
3499         else
3500             pci_write_config(gparent, reg, 0x8b, 1);
3501         atadev->mode = mode;
3502     }
3503 }
3504
3505
3506 /* misc functions */
3507 struct ata_chip_id *
3508 ata_match_chip(device_t dev, struct ata_chip_id *index)
3509 {
3510     while (index->chipid != 0) {
3511         if (pci_get_devid(dev) == index->chipid &&
3512             pci_get_revid(dev) >= index->chiprev)
3513             return index;
3514         index++;
3515     }
3516     return NULL;
3517 }
3518
3519 static struct ata_chip_id *
3520 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
3521 {
3522     device_t *children;
3523     int nchildren, i;
3524
3525     if (device_get_children(device_get_parent(dev), &children, &nchildren))
3526         return 0;
3527
3528     while (index->chipid != 0) {
3529         for (i = 0; i < nchildren; i++) {
3530             if (((slot >= 0 && pci_get_slot(children[i]) == slot) || 
3531                  (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
3532                 pci_get_devid(children[i]) == index->chipid &&
3533                 pci_get_revid(children[i]) >= index->chiprev) {
3534                 free(children, M_TEMP);
3535                 return index;
3536             }
3537         }
3538         index++;
3539     }
3540     free(children, M_TEMP);
3541     return NULL;
3542 }
3543
3544 static int
3545 ata_setup_interrupt(device_t dev)
3546 {
3547     struct ata_pci_controller *ctlr = device_get_softc(dev);
3548     int rid = ATA_IRQ_RID;
3549
3550     if (!ata_legacy(dev)) {
3551         if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3552                                                    RF_SHAREABLE | RF_ACTIVE))) {
3553             device_printf(dev, "unable to map interrupt\n");
3554             return ENXIO;
3555         }
3556         if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3557                             ata_generic_intr, ctlr, &ctlr->handle))) {
3558             device_printf(dev, "unable to setup interrupt\n");
3559             return ENXIO;
3560         }
3561     }
3562     return 0;
3563 }
3564
3565 struct ata_serialize {
3566     struct mtx  locked_mtx;
3567     int         locked_ch;
3568     int         restart_ch;
3569 };
3570
3571 static int
3572 ata_serialize(device_t dev, int flags)
3573 {
3574     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3575     struct ata_channel *ch = device_get_softc(dev);
3576     struct ata_serialize *serial;
3577     static int inited = 0;
3578     int res;
3579
3580     if (!inited) {
3581         serial = malloc(sizeof(struct ata_serialize),
3582                               M_TEMP, M_NOWAIT | M_ZERO);
3583         mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF); 
3584         serial->locked_ch = -1;
3585         serial->restart_ch = -1;
3586         device_set_ivars(ctlr->dev, serial);
3587         inited = 1;
3588     }
3589     else
3590         serial = device_get_ivars(ctlr->dev);
3591
3592     mtx_lock(&serial->locked_mtx);
3593     switch (flags) {
3594     case ATA_LF_LOCK:
3595         if (serial->locked_ch == -1)
3596             serial->locked_ch = ch->unit;
3597         if (serial->locked_ch != ch->unit)
3598             serial->restart_ch = ch->unit;
3599         break;
3600
3601     case ATA_LF_UNLOCK:
3602         if (serial->locked_ch == ch->unit) {
3603             serial->locked_ch = -1;
3604             if (serial->restart_ch != -1) {
3605                 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
3606                     serial->restart_ch = -1;
3607                     mtx_unlock(&serial->locked_mtx);
3608                     ata_start(ch->dev);
3609                     return -1;
3610                 }
3611             }
3612         }
3613         break;
3614
3615     case ATA_LF_WHICH:
3616         break;
3617     }
3618     res = serial->locked_ch;
3619     mtx_unlock(&serial->locked_mtx);
3620     return res;
3621 }
3622
3623 static void
3624 ata_print_cable(device_t dev, u_int8_t *who)
3625 {
3626     device_printf(dev,
3627                   "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
3628 }
3629
3630 static int
3631 ata_atapi(device_t dev)
3632 {
3633     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3634     struct ata_device *atadev = device_get_softc(dev);
3635
3636     return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
3637             (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
3638 }
3639
3640 static int
3641 ata_check_80pin(device_t dev, int mode)
3642 {
3643     struct ata_device *atadev = device_get_softc(dev);
3644
3645     if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
3646         ata_print_cable(dev, "device");
3647         mode = ATA_UDMA2;
3648     }
3649     return mode;
3650 }
3651
3652 static int
3653 ata_mode2idx(int mode)
3654 {
3655     if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
3656          return (mode & ATA_MODE_MASK) + 8;
3657     if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
3658          return (mode & ATA_MODE_MASK) + 5;
3659     return (mode & ATA_MODE_MASK) - ATA_PIO0;
3660 }