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