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