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