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