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