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