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