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