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