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