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