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