2 * Copyright (c) 1998 - 2006 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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 $
32 #include <sys/param.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>
42 #include <sys/queue.h>
44 #include <sys/spinlock.h>
45 #include <sys/spinlock2.h>
46 #include <sys/systm.h>
47 #include <sys/taskqueue.h>
49 #include <machine/bus_dma.h>
51 #include <bus/pci/pcireg.h>
52 #include <bus/pci/pcivar.h>
58 /* local prototypes */
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);
195 * generic ATA support functions
198 ata_generic_ident(device_t dev)
200 struct ata_pci_controller *ctlr = device_get_softc(dev);
202 device_set_desc(dev, "GENERIC ATA controller");
203 ctlr->chipinit = ata_generic_chipinit;
208 ata_generic_chipinit(device_t dev)
210 struct ata_pci_controller *ctlr = device_get_softc(dev);
212 if (ata_setup_interrupt(dev))
214 ctlr->setmode = ata_generic_setmode;
219 ata_generic_intr(void *data)
221 struct ata_pci_controller *ctlr = data;
222 struct ata_channel *ch;
225 for (unit = 0; unit < ctlr->channels; unit++) {
226 if ((ch = ctlr->interrupt[unit].argument))
227 ctlr->interrupt[unit].function(ch);
232 ata_generic_setmode(device_t dev, int mode)
234 struct ata_device *atadev = device_get_softc(dev);
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))
244 * SATA support functions
247 ata_sata_phy_check_events(device_t dev)
249 struct ata_channel *ch = device_get_softc(dev);
250 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
252 /* clear error bits/interrupt */
253 ATA_IDX_OUTL(ch, ATA_SERROR, error);
255 /* do we have any events flagged ? */
257 struct ata_connect_task *tp;
258 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
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))) {
266 if (((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) ||
267 ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)) {
269 device_printf(ch->dev, "CONNECT requested\n");
270 tp->action = ATA_C_ATTACH;
274 device_printf(ch->dev, "DISCONNECT requested\n");
275 tp->action = ATA_C_DETACH;
278 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
279 taskqueue_enqueue(taskqueue_thread[mycpuid], &tp->task);
285 ata_sata_phy_event(void *context, int dummy)
287 struct ata_connect_task *tp = (struct ata_connect_task *)context;
288 struct ata_channel *ch = device_get_softc(tp->dev);
293 if (tp->action == ATA_C_ATTACH) {
295 device_printf(tp->dev, "CONNECTED\n");
297 ata_identify(tp->dev);
299 if (tp->action == ATA_C_DETACH) {
300 if (!device_get_children(tp->dev, &children, &nchildren)) {
301 for (i = 0; i < nchildren; i++)
303 device_delete_child(tp->dev, children[i]);
304 kfree(children, M_TEMP);
306 spin_lock_wr(&ch->state_mtx);
307 ch->state = ATA_IDLE;
308 spin_unlock_wr(&ch->state_mtx);
310 device_printf(tp->dev, "DISCONNECTED\n");
317 ata_sata_phy_reset(device_t dev)
319 struct ata_channel *ch = device_get_softc(dev);
322 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE)
323 return ata_sata_connect(ch);
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);
329 if ((ATA_IDX_INL(ch, ATA_SCONTROL) &
330 ATA_SC_DET_MASK) == ATA_SC_DET_RESET)
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);
339 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == 0)
340 return ata_sata_connect(ch);
347 ata_sata_connect(struct ata_channel *ch)
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)
360 if (timeout >= 100) {
362 device_printf(ch->dev, "SATA connect status=%08x\n", status);
367 device_printf(ch->dev, "SATA connect time=%dms\n", timeout * 10);
369 /* clear SATA error register */
370 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
376 ata_sata_setmode(device_t dev, int mode)
378 struct ata_device *atadev = device_get_softc(dev);
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.
386 if (atadev->param.satacapabilities != 0x0000 &&
387 atadev->param.satacapabilities != 0xffff) {
388 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
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));
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;
400 atadev->mode = ATA_SA150;
403 mode = ata_limit_mode(dev, mode, ATA_UDMA5);
404 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
410 ata_request2fis_h2d(struct ata_request *request, u_int8_t *fis)
412 struct ata_device *atadev = device_get_softc(request->dev);
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)
421 fis[5] = request->transfersize;
422 fis[6] = request->transfersize >> 8;
424 fis[7] = ATA_D_LBA | atadev->unit;
425 fis[15] = ATA_A_4BIT;
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;
453 * AHCI v1.x compliant SATA chipset support functions
456 ata_ahci_chipinit(device_t dev)
458 struct ata_pci_controller *ctlr = device_get_softc(dev);
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);
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");
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);
475 /* get the number of HW 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);
480 /* clear interrupts */
481 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, ATA_INL(ctlr->r_res2, ATA_AHCI_IS));
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);
487 ctlr->reset = ata_ahci_reset;
488 ctlr->dmainit = ata_ahci_dmainit;
489 ctlr->allocate = ata_ahci_allocate;
490 ctlr->setmode = ata_sata_setmode;
492 /* enable PCI interrupt */
493 pci_write_config(dev, PCIR_COMMAND,
494 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
496 /* announce we support the HW */
497 version = ATA_INL(ctlr->r_res2, ATA_AHCI_VS);
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);
507 ata_ahci_allocate(device_t dev)
509 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
510 struct ata_channel *ch = device_get_softc(dev);
512 int offset = ch->unit << 7;
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;
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 */
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);
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);
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));
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));
554 ata_ahci_status(device_t dev)
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;
562 if (action & (1 << ch->unit)) {
563 u_int32_t istatus = ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset);
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);
569 /* do we have any PHY events ? */
570 ata_sata_phy_check_events(dev);
572 /* do we have any device action ? */
573 return (!(ATA_INL(ctlr->r_res2, ATA_AHCI_P_CI + offset) & (1 << tag)));
578 /* must be called with ATA channel locked and state_mtx held */
580 ata_ahci_begin_transaction(struct ata_request *request)
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;
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));
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;
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;
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));
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));
621 clp->cmd_table_phys = htole64(ch->dma->work_bus + ATA_AHCI_CT_OFFSET +
622 (ATA_AHCI_CT_SIZE * tag));
624 /* clear eventual ACTIVE bit */
625 ATA_IDX_OUTL(ch, ATA_SACTIVE, ATA_IDX_INL(ch, ATA_SACTIVE) & (1 << tag));
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);
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);
637 /* issue the command */
638 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CI + offset, (1 << tag));
640 /* start the timeout */
641 callout_reset(&request->callout, request->timeout * hz,
642 (timeout_t*)ata_timeout, request);
643 return ATA_OP_CONTINUES;
646 /* must be called with ATA channel locked and state_mtx held */
648 ata_ahci_end_transaction(struct ata_request *request)
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;
654 int offset = ch->unit << 7;
657 /* kill the timeout */
658 callout_stop(&request->callout);
661 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + offset);
662 request->status = tf_data;
664 /* if error status get details */
665 if (request->status & ATA_S_ERROR)
666 request->error = tf_data >> 8;
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;
673 /* release SG list etc */
674 ch->dma->unload(ch->dev);
676 return ATA_OP_FINISHED;
680 ata_ahci_reset(device_t dev)
682 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
683 struct ata_channel *ch = device_get_softc(dev);
685 int offset = ch->unit << 7;
688 if (!(ATA_INL(ctlr->r_res2, ATA_AHCI_PI) & (1 << ch->unit))) {
689 device_printf(dev, "port not implemented\n");
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));
699 /* XXX SOS this is not entirely wrong */
704 device_printf(dev, "stopping AHCI engine failed\n");
707 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) & ATA_AHCI_P_CMD_CR);
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);
718 device_printf(dev, "executing CLO failed\n");
721 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD+offset)&ATA_AHCI_P_CMD_CLO);
725 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, ATA_AHCI_P_CMD_SUD);
727 /* enable interface */
728 if (ata_sata_phy_reset(dev)) {
729 switch (ATA_INL(ctlr->r_res2, ATA_AHCI_P_SIG + offset)) {
731 ch->devices = ATA_ATAPI_MASTER;
732 device_printf(ch->dev, "SATA ATAPI devices not supported yet\n");
736 ch->devices = ATA_PORTMULTIPLIER;
737 device_printf(ch->dev, "Portmultipliers not supported yet\n");
741 ch->devices = ATA_ATA_MASTER;
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));
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));
757 ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
759 struct ata_dmasetprd_args *args = xsc;
760 struct ata_ahci_dma_prd *prd = args->dmatab;
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);
773 ata_ahci_dmainit(device_t dev)
775 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
776 struct ata_channel *ch = device_get_softc(dev);
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;
789 ata_ahci_setup_fis(struct ata_ahci_cmd_tab *ctp, struct ata_request *request)
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);
796 return ata_request2fis_h2d(request, &ctp->cfis[0]);
801 * Acard chipset support functions
804 ata_acard_ident(device_t dev)
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}};
817 if (!(idx = ata_match_chip(dev, ids)))
820 ksprintf(buffer, "Acard %s %s controller",
821 idx->text, ata_mode2str(idx->max_dma));
822 device_set_desc_copy(dev, buffer);
824 ctlr->chipinit = ata_acard_chipinit;
829 ata_acard_chipinit(device_t dev)
831 struct ata_pci_controller *ctlr = device_get_softc(dev);
833 if (ata_setup_interrupt(dev))
836 ctlr->allocate = ata_acard_allocate;
837 if (ctlr->chip->cfg1 == ATPOLD) {
838 ctlr->setmode = ata_acard_850_setmode;
839 ctlr->locking = ata_serialize;
842 ctlr->setmode = ata_acard_86X_setmode;
847 ata_acard_allocate(device_t dev)
849 struct ata_channel *ch = device_get_softc(dev);
851 /* setup the usual register normal pci style */
852 if (ata_pci_allocate(dev))
855 ch->hw.status = ata_acard_status;
860 ata_acard_status(device_t dev)
862 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
863 struct ata_channel *ch = device_get_softc(dev);
865 if (ctlr->chip->cfg1 == ATPOLD &&
866 ATA_LOCKING(ch->dev, ATA_LF_WHICH) != ch->unit)
868 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
869 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
871 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
872 ATA_BMSTAT_INTERRUPT)
874 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
876 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
877 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
880 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
882 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
889 ata_acard_850_setmode(device_t dev, int mode)
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);
898 mode = ata_limit_mode(dev, mode,
899 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
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);
905 device_printf(dev, "%ssetting %s on %s chip\n",
906 (error) ? "FAILURE " : "",
907 ata_mode2str(mode), ctlr->chip->text);
909 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
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);
921 /* we could set PIO mode timings, but we assume the BIOS did that */
925 ata_acard_86X_setmode(device_t dev, int mode)
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);
935 mode = ata_limit_mode(dev, mode,
936 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
938 mode = ata_check_80pin(dev, mode);
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);
944 device_printf(dev, "%ssetting %s on %s chip\n",
945 (error) ? "FAILURE " : "",
946 ata_mode2str(mode), ctlr->chip->text);
948 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
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);
960 /* we could set PIO mode timings, but we assume the BIOS did that */
965 * Acer Labs Inc (ALI) chipset support functions
968 ata_ali_ident(device_t dev)
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}};
985 if (!(idx = ata_match_chip(dev, ids)))
988 ksprintf(buffer, "AcerLabs %s %s controller",
989 idx->text, ata_mode2str(idx->max_dma));
990 device_set_desc_copy(dev, buffer);
992 ctlr->chipinit = ata_ali_chipinit;
997 ata_ali_chipinit(device_t dev)
999 struct ata_pci_controller *ctlr = device_get_softc(dev);
1001 if (ata_setup_interrupt(dev))
1004 switch (ctlr->chip->cfg2) {
1006 ctlr->channels = ctlr->chip->cfg1;
1007 ctlr->allocate = ata_ali_sata_allocate;
1008 ctlr->setmode = ata_sata_setmode;
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);
1017 /* enable PCI interrupt */
1018 pci_write_config(dev, PCIR_COMMAND,
1019 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1023 /* use device interrupt as byte count end */
1024 pci_write_config(dev, 0x4a, pci_read_config(dev, 0x4a, 1) | 0x20, 1);
1026 /* enable cable detection and UDMA support on newer chips */
1027 pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
1029 /* enable ATAPI UDMA mode */
1030 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x01, 1);
1032 /* only chips with revision > 0xc4 can do 48bit DMA */
1033 if (ctlr->chip->chiprev <= 0xc4)
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;
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;
1052 ata_ali_allocate(device_t dev)
1054 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1055 struct ata_channel *ch = device_get_softc(dev);
1057 /* setup the usual register normal pci style */
1058 if (ata_pci_allocate(dev))
1061 /* older chips can't do 48bit DMA transfers */
1062 if (ctlr->chip->chiprev <= 0xc4)
1063 ch->flags |= ATA_NO_48BIT_DMA;
1069 ata_ali_sata_allocate(device_t dev)
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);
1078 rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
1079 io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
1083 rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
1084 ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
1086 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
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);
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);
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);
1104 ch->flags |= ATA_NO_SLAVE;
1106 /* XXX SOS PHY handling awkward in ALI chip not supported yet */
1112 ata_ali_reset(device_t dev)
1114 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1115 struct ata_channel *ch = device_get_softc(dev);
1119 ata_generic_reset(dev);
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
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);
1139 kfree(children, M_TEMP);
1145 ata_ali_setmode(device_t dev, int mode)
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);
1154 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
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");
1164 mode = ata_check_80pin(dev, mode);
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);
1176 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1179 device_printf(dev, "%ssetting %s on %s chip\n",
1180 (error) ? "FAILURE " : "",
1181 ata_mode2str(mode), ctlr->chip->text);
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);
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),
1194 u_int32_t piotimings[] =
1195 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
1196 0x00310001, 0x00440001, 0x00330001, 0x00310001};
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);
1203 atadev->mode = mode;
1209 * American Micro Devices (AMD) chipset support functions
1212 ata_amd_ident(device_t dev)
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}};
1224 if (!(idx = ata_match_chip(dev, ids)))
1227 ksprintf(buffer, "AMD %s %s controller",
1228 idx->text, ata_mode2str(idx->max_dma));
1229 device_set_desc_copy(dev, buffer);
1231 ctlr->chipinit = ata_amd_chipinit;
1236 ata_amd_chipinit(device_t dev)
1238 struct ata_pci_controller *ctlr = device_get_softc(dev);
1240 if (ata_setup_interrupt(dev))
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);
1247 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
1249 ctlr->setmode = ata_via_family_setmode;
1255 * ATI chipset support functions
1258 ata_ati_ident(device_t dev)
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}};
1272 if (!(idx = ata_match_chip(dev, ids)))
1275 ksprintf(buffer, "ATI %s %s controller",
1276 idx->text, ata_mode2str(idx->max_dma));
1277 device_set_desc_copy(dev, buffer);
1280 /* the ATI SATA controller is actually a SiI 3112 controller*/
1281 if (ctlr->chip->cfg1 & SIIMEMIO)
1282 ctlr->chipinit = ata_sii_chipinit;
1284 ctlr->chipinit = ata_ati_chipinit;
1289 ata_ati_chipinit(device_t dev)
1291 struct ata_pci_controller *ctlr = device_get_softc(dev);
1293 if (ata_setup_interrupt(dev))
1296 ctlr->setmode = ata_ati_setmode;
1301 ata_ati_setmode(device_t dev, int mode)
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;
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 };
1314 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1316 mode = ata_check_80pin(dev, mode);
1318 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1321 device_printf(dev, "%ssetting %s on %s chip\n",
1322 (error) ? "FAILURE " : "",
1323 ata_mode2str(mode), ctlr->chip->text);
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);
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);
1348 pci_write_config(gparent, 0x54,
1349 pci_read_config(gparent, 0x54, 1) &
1350 ~(0x01 << devno), 1);
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;
1365 * Cyrix chipset support functions
1368 ata_cyrix_ident(device_t dev)
1370 struct ata_pci_controller *ctlr = device_get_softc(dev);
1372 if (pci_get_devid(dev) == ATA_CYRIX_5530) {
1373 device_set_desc(dev, "Cyrix 5530 ATA33 controller");
1374 ctlr->chipinit = ata_cyrix_chipinit;
1381 ata_cyrix_chipinit(device_t dev)
1383 struct ata_pci_controller *ctlr = device_get_softc(dev);
1385 if (ata_setup_interrupt(dev))
1389 ctlr->setmode = ata_cyrix_setmode;
1391 ctlr->setmode = ata_generic_setmode;
1396 ata_cyrix_setmode(device_t dev, int mode)
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 };
1407 ch->dma->alignment = 16;
1408 ch->dma->max_iosize = 126 * DEV_BSIZE;
1410 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1412 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1415 device_printf(dev, "%ssetting %s on Cyrix chip\n",
1416 (error) ? "FAILURE " : "", ata_mode2str(mode));
1418 if (mode >= ATA_UDMA0) {
1419 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1420 0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
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]);
1427 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1428 0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
1430 atadev->mode = mode;
1436 * Cypress chipset support functions
1439 ata_cypress_ident(device_t dev)
1441 struct ata_pci_controller *ctlr = device_get_softc(dev);
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.
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;
1461 ata_cypress_chipinit(device_t dev)
1463 struct ata_pci_controller *ctlr = device_get_softc(dev);
1465 if (ata_setup_interrupt(dev))
1468 ctlr->setmode = ata_cypress_setmode;
1473 ata_cypress_setmode(device_t dev, int mode)
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);
1480 mode = ata_limit_mode(dev, mode, ATA_WDMA2);
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);
1486 device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
1487 error ? "FAILURE " : "");
1489 pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
1490 atadev->mode = mode;
1494 /* we could set PIO mode timings, but we assume the BIOS did that */
1499 * HighPoint chipset support functions
1502 ata_highpoint_ident(device_t dev)
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}};
1519 if (!(idx = ata_match_chip(dev, ids)))
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)");
1530 ksprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
1531 device_set_desc_copy(dev, buffer);
1533 ctlr->chipinit = ata_highpoint_chipinit;
1538 ata_highpoint_chipinit(device_t dev)
1540 struct ata_pci_controller *ctlr = device_get_softc(dev);
1542 if (ata_setup_interrupt(dev))
1545 if (ctlr->chip->cfg2 == HPTOLD) {
1546 /* disable interrupt prediction */
1547 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
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);
1554 /* enable interrupts */
1555 pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
1557 /* set clocks etc */
1558 if (ctlr->chip->cfg1 < HPT372)
1559 pci_write_config(dev, 0x5b, 0x22, 1);
1561 pci_write_config(dev, 0x5b,
1562 (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
1564 ctlr->allocate = ata_highpoint_allocate;
1565 ctlr->setmode = ata_highpoint_setmode;
1570 ata_highpoint_allocate(device_t dev)
1572 struct ata_channel *ch = device_get_softc(dev);
1574 /* setup the usual register normal pci style */
1575 if (ata_pci_allocate(dev))
1578 ch->flags |= ATA_ALWAYS_DMASTAT;
1583 ata_highpoint_setmode(device_t dev, int mode)
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);
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 */
1610 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1612 if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
1613 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1615 mode = ata_highpoint_check_80pin(dev, mode);
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
1622 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
1623 ata_limit_mode(dev, mode, ATA_UDMA5));
1625 device_printf(dev, "%ssetting %s on HighPoint chip\n",
1626 (error) ? "FAILURE " : "", ata_mode2str(mode));
1628 pci_write_config(gparent, 0x40 + (devno << 2),
1629 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
1630 atadev->mode = mode;
1634 ata_highpoint_check_80pin(device_t dev, int mode)
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;
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);
1648 val = pci_read_config(gparent, reg, 1);
1649 pci_write_config(gparent, reg, val & 0xfe, 1);
1651 res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1652 pci_write_config(gparent, reg, val, 1);
1654 if (mode > ATA_UDMA2 && res) {
1655 ata_print_cable(dev, "controller");
1663 * Intel chipset support functions
1666 ata_intel_ident(device_t dev)
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}};
1718 if (!(idx = ata_match_chip(dev, ids)))
1721 ksprintf(buffer, "Intel %s %s controller",
1722 idx->text, ata_mode2str(idx->max_dma));
1723 device_set_desc_copy(dev, buffer);
1725 ctlr->chipinit = ata_intel_chipinit;
1730 ata_intel_chipinit(device_t dev)
1732 struct ata_pci_controller *ctlr = device_get_softc(dev);
1734 if (ata_setup_interrupt(dev))
1737 /* good old PIIX needs special treatment (not implemented) */
1738 if (ctlr->chip->chipid == ATA_I82371FB) {
1739 ctlr->setmode = ata_intel_old_setmode;
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,
1752 ctlr->allocate = ata_intel_31244_allocate;
1753 ctlr->reset = ata_intel_31244_reset;
1755 ctlr->setmode = ata_sata_setmode;
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;
1764 /* SATA parts can be either compat or AHCI */
1766 /* force all ports active "the legacy way" */
1767 pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f,2);
1769 ctlr->allocate = ata_intel_allocate;
1770 ctlr->reset = ata_intel_reset;
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
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,
1783 return ata_ahci_chipinit(dev);
1785 ctlr->setmode = ata_sata_setmode;
1787 /* enable PCI interrupt */
1788 pci_write_config(dev, PCIR_COMMAND,
1789 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1795 ata_intel_allocate(device_t dev)
1797 struct ata_channel *ch = device_get_softc(dev);
1799 /* setup the usual register normal pci style */
1800 if (ata_pci_allocate(dev))
1803 ch->flags |= ATA_ALWAYS_DMASTAT;
1808 ata_intel_reset(device_t dev)
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);
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);
1820 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1821 if (pci_read_config(parent, 0x90, 1) & 0x04)
1824 mask = (0x0001 << ch->unit);
1825 /* XXX SOS should be in intel_allocate if we grow it */
1826 ch->flags |= ATA_NO_SLAVE;
1829 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1831 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
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))
1840 ata_generic_reset(dev);
1844 ata_intel_old_setmode(device_t dev, int mode)
1850 ata_intel_new_setmode(device_t dev, int mode)
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;
1865 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1866 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1868 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1870 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1871 ata_print_cable(dev, "controller");
1875 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1878 device_printf(dev, "%ssetting %s on %s chip\n",
1879 (error) ? "FAILURE " : "",
1880 ata_mode2str(mode), ctlr->chip->text);
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);
1891 pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
1892 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))), 2);
1895 if (mode >= ATA_UDMA2)
1896 pci_write_config(gparent, 0x54, reg54 | (0x1 << devno), 2);
1898 pci_write_config(gparent, 0x54, reg54 & ~(0x1 << devno), 2);
1900 if (mode >= ATA_UDMA5)
1901 pci_write_config(gparent, 0x54, reg54 | (0x1000 << devno), 2);
1903 pci_write_config(gparent, 0x54, reg54 & ~(0x1000 << devno), 2);
1905 reg40 &= ~0x00ff00ff;
1906 reg40 |= 0x40774077;
1908 if (atadev->unit == ATA_MASTER) {
1910 new40 = timings[ata_mode2idx(mode)] << 8;
1914 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
1915 (timings[ata_mode2idx(mode)] & 0x03);
1923 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
1924 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
1926 atadev->mode = mode;
1930 ata_intel_31244_allocate(device_t dev)
1932 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1933 struct ata_channel *ch = device_get_softc(dev);
1937 ch_offset = 0x200 + ch->unit * 0x200;
1939 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
1940 ch->r_io[i].res = ctlr->r_res2;
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;
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;
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;
1966 ch->flags |= ATA_NO_SLAVE;
1968 ch->hw.status = ata_intel_31244_status;
1969 ch->hw.command = ata_intel_31244_command;
1971 /* enable PHY state change interrupt */
1972 ATA_OUTL(ctlr->r_res2, 0x4,
1973 ATA_INL(ctlr->r_res2, 0x04) | (0x01 << (ch->unit << 3)));
1978 ata_intel_31244_status(device_t dev)
1980 /* do we have any PHY events ? */
1981 ata_sata_phy_check_events(dev);
1983 /* any drive action to take care of ? */
1984 return ata_pci_status(dev);
1988 ata_intel_31244_command(struct ata_request *request)
1990 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
1991 struct ata_device *atadev = device_get_softc(request->dev);
1994 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
1995 return (ata_generic_command(request));
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));
2009 /* issue command to controller */
2010 ATA_IDX_OUTB(ch, ATA_COMMAND, request->u.ata.command);
2016 ata_intel_31244_reset(device_t dev)
2018 if (ata_sata_phy_reset(dev))
2019 ata_generic_reset(dev);
2024 * Integrated Technology Express Inc. (ITE) chipset support functions
2027 ata_ite_ident(device_t dev)
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}};
2037 if (!(idx = ata_match_chip(dev, ids)))
2040 ksprintf(buffer, "ITE %s %s controller",
2041 idx->text, ata_mode2str(idx->max_dma));
2042 device_set_desc_copy(dev, buffer);
2044 ctlr->chipinit = ata_ite_chipinit;
2049 ata_ite_chipinit(device_t dev)
2051 struct ata_pci_controller *ctlr = device_get_softc(dev);
2053 if (ata_setup_interrupt(dev))
2056 ctlr->setmode = ata_ite_setmode;
2058 /* set PCI mode and 66Mhz reference clock */
2059 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
2061 /* set default active & recover timings */
2062 pci_write_config(dev, 0x54, 0x31, 1);
2063 pci_write_config(dev, 0x56, 0x31, 1);
2068 ata_ite_setmode(device_t dev, int mode)
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);
2076 /* correct the mode for what the HW supports */
2077 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
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");
2086 /* set the wanted mode on the device */
2087 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2090 device_printf(dev, "%s setting %s on ITE8212F chip\n",
2091 (error) ? "failed" : "success", ata_mode2str(mode));
2093 /* if the device accepted the mode change, setup the HW accordingly */
2095 if (mode >= ATA_UDMA0) {
2096 u_int8_t udmatiming[] =
2097 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
2099 /* enable UDMA mode */
2100 pci_write_config(gparent, 0x50,
2101 pci_read_config(gparent, 0x50, 1) &
2102 ~(1 << (devno + 3)), 1);
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);
2110 u_int8_t chtiming[] =
2111 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
2113 /* disable UDMA mode */
2114 pci_write_config(gparent, 0x50,
2115 pci_read_config(gparent, 0x50, 1) |
2116 (1 << (devno + 3)), 1);
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);
2124 atadev->mode = mode;
2130 * JMicron chipset support functions
2133 ata_jmicron_ident(device_t dev)
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}};
2147 if (!(idx = ata_match_chip(dev, ids)))
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));
2155 ksnprintf(buffer, sizeof(buffer), "JMicron %s %s controller",
2156 idx->text, ata_mode2str(idx->max_dma));
2157 device_set_desc_copy(dev, buffer);
2159 ctlr->chipinit = ata_jmicron_chipinit;
2164 ata_jmicron_chipinit(device_t dev)
2166 struct ata_pci_controller *ctlr = device_get_softc(dev);
2169 if (ata_setup_interrupt(dev))
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);
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;
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);
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)))
2201 ctlr->allocate = ata_jmicron_allocate;
2202 ctlr->reset = ata_jmicron_reset;
2203 ctlr->dmainit = ata_jmicron_dmainit;
2204 ctlr->setmode = ata_jmicron_setmode;
2206 /* set the number of HW channels */
2207 ctlr->channels = ctlr->chip->cfg1 + ctlr->chip->cfg2;
2213 ata_jmicron_allocate(device_t dev)
2215 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2216 struct ata_channel *ch = device_get_softc(dev);
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;
2225 error = ata_ahci_allocate(dev);
2230 ata_jmicron_reset(device_t dev)
2232 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2233 struct ata_channel *ch = device_get_softc(dev);
2235 if (ch->unit >= ctlr->chip->cfg1)
2236 ata_generic_reset(dev);
2238 ata_ahci_reset(dev);
2242 ata_jmicron_dmainit(device_t dev)
2244 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2245 struct ata_channel *ch = device_get_softc(dev);
2247 if (ch->unit >= ctlr->chip->cfg1)
2248 ata_pci_dmainit(dev);
2250 ata_ahci_dmainit(dev);
2254 ata_jmicron_setmode(device_t dev, int mode)
2256 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2257 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2259 if (pci_read_config(dev, 0xdf, 1) & 0x40 || ch->unit >= ctlr->chip->cfg1) {
2260 struct ata_device *atadev = device_get_softc(dev);
2262 /* check for 80pin cable present */
2263 if (pci_read_config(dev, 0x40, 1) & 0x08)
2264 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2266 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2268 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
2269 atadev->mode = mode;
2272 ata_sata_setmode(dev, mode);
2277 * Marvell chipset support functions
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)
2284 struct ata_marvell_response {
2286 u_int8_t edma_status;
2287 u_int8_t dev_status;
2288 u_int32_t timestamp;
2291 struct ata_marvell_dma_prdentry {
2299 ata_marvell_ident(device_t dev)
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}};
2315 if (!(idx = ata_match_chip(dev, ids)))
2318 ksprintf(buffer, "Marvell %s %s controller",
2319 idx->text, ata_mode2str(idx->max_dma));
2320 device_set_desc_copy(dev, buffer);
2322 switch (ctlr->chip->cfg2) {
2325 ctlr->chipinit = ata_marvell_edma_chipinit;
2328 ctlr->chipinit = ata_marvell_pata_chipinit;
2335 ata_marvell_pata_chipinit(device_t dev)
2337 struct ata_pci_controller *ctlr = device_get_softc(dev);
2339 if (ata_setup_interrupt(dev))
2342 ctlr->allocate = ata_marvell_pata_allocate;
2343 ctlr->setmode = ata_marvell_pata_setmode;
2344 ctlr->channels = ctlr->chip->cfg1;
2349 ata_marvell_pata_allocate(device_t dev)
2351 struct ata_channel *ch = device_get_softc(dev);
2353 /* setup the usual register normal pci style */
2354 if (ata_pci_allocate(dev))
2357 /* dont use 32 bit PIO transfers */
2358 ch->flags |= ATA_USE_16BIT;
2364 ata_marvell_pata_setmode(device_t dev, int mode)
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);
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;
2377 ata_marvell_edma_chipinit(device_t dev)
2379 struct ata_pci_controller *ctlr = device_get_softc(dev);
2381 if (ata_setup_interrupt(dev))
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)))
2390 /* mask all host controller interrupts */
2391 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x00000000);
2393 /* mask all PCI interrupts */
2394 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x00000000);
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;
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);
2407 /* clear PCI interrupts */
2408 ATA_OUTL(ctlr->r_res1, 0x01d58, 0x00000000);
2410 /* unmask PCI interrupts we want */
2411 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x007fffff);
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));
2417 /* enable PCI interrupt */
2418 pci_write_config(dev, PCIR_COMMAND,
2419 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
2424 ata_marvell_edma_allocate(device_t dev)
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;
2431 /* clear work area */
2432 bzero(ch->dma->work, 1024+256);
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);
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);
2444 /* set SATA resources */
2445 switch (ctlr->chip->cfg2) {
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);
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);
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;
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 */
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));
2480 /* request queue base high */
2481 ATA_OUTL(ctlr->r_res1, 0x02010 + ATA_MV_EDMA_BASE(ch), work >> 32);
2483 /* request queue in ptr */
2484 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2486 /* request queue out ptr */
2487 ATA_OUTL(ctlr->r_res1, 0x02018 + ATA_MV_EDMA_BASE(ch), 0x0);
2489 /* response queue base high */
2491 ATA_OUTL(ctlr->r_res1, 0x0201c + ATA_MV_EDMA_BASE(ch), work >> 32);
2493 /* response queue in ptr */
2494 ATA_OUTL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch), 0x0);
2496 /* response queue out ptr */
2497 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2499 /* clear SATA error register */
2500 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2502 /* clear any outstanding error interrupts */
2503 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2505 /* unmask all error interrupts */
2506 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2508 /* enable EDMA machinery */
2509 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2514 ata_marvell_edma_status(device_t dev)
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);
2521 if (cause & (1 << shift)) {
2523 /* clear interrupt(s) */
2524 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2526 /* do we have any PHY events ? */
2527 ata_sata_phy_check_events(dev);
2530 /* do we have any device action ? */
2531 return (cause & (2 << shift));
2534 /* must be called with ATA channel locked and state_mtx held */
2536 ata_marvell_edma_begin_transaction(struct ata_request *request)
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));
2545 int dummy, error, slot;
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) {
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);
2557 /* check for 48 bit access and convert if needed */
2558 ata_modify_if_48bit(request);
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,
2564 device_printf(request->dev, "setting up DMA failed\n");
2565 request->result = error;
2566 return ATA_OP_FINISHED;
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;
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);
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;
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;
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;
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;
2603 bytep[i++] = ATA_D_LBA | ATA_D_IBM | ((request->u.ata.lba >> 24) & 0xf);
2604 bytep[i++] = 0x10 | ATA_DRIVE;
2606 bytep[i++] = request->u.ata.command;
2607 bytep[i++] = 0x90 | ATA_COMMAND;
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))
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);
2623 return ATA_OP_CONTINUES;
2626 /* must be called with ATA channel locked and state_mtx held */
2628 ata_marvell_edma_end_transaction(struct ata_request *request)
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);
2636 /* EDMA interrupt */
2637 if ((icr & (0x0001 << (ch->unit & 3)))) {
2638 struct ata_marvell_response *response;
2639 u_int32_t rsp_in, rsp_out;
2643 callout_stop(&request->callout);
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));
2654 /* record status for this request */
2655 request->status = response->dev_status;
2659 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), rsp_out);
2661 /* update progress */
2662 if (!(request->status & ATA_S_ERROR) &&
2663 !(request->flags & ATA_R_TIMEOUT))
2664 request->donecount = request->bytecount;
2666 /* unload SG list */
2667 ch->dma->unload(ch->dev);
2669 res = ATA_OP_FINISHED;
2672 /* legacy ATA interrupt */
2674 res = ata_end_transaction(request);
2678 ATA_OUTL(ctlr->r_res1, offset, ~(icr & (0x0101 << (ch->unit & 3))));
2683 ata_marvell_edma_reset(device_t dev)
2685 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2686 struct ata_channel *ch = device_get_softc(dev);
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))
2693 /* clear SATA error register */
2694 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2696 /* clear any outstanding error interrupts */
2697 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2699 /* unmask all error interrupts */
2700 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2702 /* enable channel and test for devices */
2703 if (ata_sata_phy_reset(dev))
2704 ata_generic_reset(dev);
2706 /* enable EDMA machinery */
2707 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2711 ata_marvell_edma_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs,
2714 struct ata_dmasetprd_args *args = xsc;
2715 struct ata_marvell_dma_prdentry *prd = args->dmatab;
2718 if ((args->error = error))
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);
2726 prd[i - 1].count |= htole32(ATA_DMA_EOT);
2730 ata_marvell_edma_dmainit(device_t dev)
2732 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2733 struct ata_channel *ch = device_get_softc(dev);
2737 /* note start and stop are not used here */
2738 ch->dma->setprd = ata_marvell_edma_dmasetprd;
2740 if (ATA_INL(ctlr->r_res1, 0x00d00) & 0x00000004)
2741 ch->dma->max_address = BUS_SPACE_MAXADDR;
2747 * National chipset support functions
2750 ata_national_ident(device_t dev)
2752 struct ata_pci_controller *ctlr = device_get_softc(dev);
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;
2764 ata_national_chipinit(device_t dev)
2766 struct ata_pci_controller *ctlr = device_get_softc(dev);
2768 if (ata_setup_interrupt(dev))
2771 ctlr->setmode = ata_national_setmode;
2776 ata_national_setmode(device_t dev, int mode)
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 };
2790 ch->dma->alignment = 16;
2791 ch->dma->max_iosize = 126 * DEV_BSIZE;
2793 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2795 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2798 device_printf(dev, "%s setting %s on National chip\n",
2799 (error) ? "failed" : "success", ata_mode2str(mode));
2801 if (mode >= ATA_UDMA0) {
2802 pci_write_config(gparent, 0x44 + (devno << 3),
2803 udmatiming[mode & ATA_MODE_MASK], 4);
2805 else if (mode >= ATA_WDMA0) {
2806 pci_write_config(gparent, 0x44 + (devno << 3),
2807 dmatiming[mode & ATA_MODE_MASK], 4);
2810 pci_write_config(gparent, 0x44 + (devno << 3),
2811 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
2814 pci_write_config(gparent, 0x40 + (devno << 3),
2815 piotiming[ata_mode2idx(mode)], 4);
2816 atadev->mode = mode;
2821 * NetCell chipset support functions
2824 ata_netcell_ident(device_t dev)
2826 struct ata_pci_controller *ctlr = device_get_softc(dev);
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;
2837 ata_netcell_chipinit(device_t dev)
2839 struct ata_pci_controller *ctlr = device_get_softc(dev);
2841 if (ata_generic_chipinit(dev))
2844 ctlr->allocate = ata_netcell_allocate;
2849 ata_netcell_allocate(device_t dev)
2851 struct ata_channel *ch = device_get_softc(dev);
2853 /* setup the usual register normal pci style */
2854 if (ata_pci_allocate(dev))
2857 /* the NetCell only supports 16 bit PIO transfers */
2858 ch->flags |= ATA_USE_16BIT;
2865 * nVidia chipset support functions
2868 ata_nvidia_ident(device_t dev)
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}} ;
2896 if (!(idx = ata_match_chip(dev, ids)))
2899 ksprintf(buffer, "nVidia %s %s controller",
2900 idx->text, ata_mode2str(idx->max_dma));
2901 device_set_desc_copy(dev, buffer);
2903 ctlr->chipinit = ata_nvidia_chipinit;
2908 ata_nvidia_chipinit(device_t dev)
2910 struct ata_pci_controller *ctlr = device_get_softc(dev);
2912 if (ata_setup_interrupt(dev))
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;
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;
2925 ctlr->allocate = ata_nvidia_allocate;
2926 ctlr->reset = ata_nvidia_reset;
2928 /* enable control access */
2929 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
2931 if (ctlr->chip->cfg2 & NVQ) {
2932 /* clear interrupt status */
2933 ATA_OUTL(ctlr->r_res2, offset, 0x00ff00ff);
2935 /* enable device and PHY state change interrupts */
2936 ATA_OUTL(ctlr->r_res2, offset + 4, 0x000d000d);
2938 /* disable NCQ support */
2939 ATA_OUTL(ctlr->r_res2, 0x0400,
2940 ATA_INL(ctlr->r_res2, 0x0400) & 0xfffffff9);
2943 /* clear interrupt status */
2944 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2946 /* enable device and PHY state change interrupts */
2947 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
2950 /* enable PCI interrupt */
2951 pci_write_config(dev, PCIR_COMMAND,
2952 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
2955 ctlr->setmode = ata_sata_setmode;
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;
2966 ata_nvidia_allocate(device_t dev)
2968 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2969 struct ata_channel *ch = device_get_softc(dev);
2971 /* setup the usual register normal pci style */
2972 if (ata_pci_allocate(dev))
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);
2982 ch->hw.status = ata_nvidia_status;
2983 ch->flags |= ATA_NO_SLAVE;
2989 ata_nvidia_status(device_t dev)
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);
2997 /* do we have any PHY events ? */
2998 if (istatus & (0x0c << shift))
2999 ata_sata_phy_check_events(dev);
3001 /* clear interrupt(s) */
3002 ATA_OUTB(ctlr->r_res2, offset,
3003 (0x0f << shift) | (ctlr->chip->cfg2 & NVQ ? 0x00f000f0 : 0));
3005 /* do we have any device action ? */
3006 return (istatus & (0x01 << shift));
3010 ata_nvidia_reset(device_t dev)
3012 if (ata_sata_phy_reset(dev))
3013 ata_generic_reset(dev);
3018 * Promise chipset support functions
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
3037 struct host_packet {
3039 TAILQ_ENTRY(host_packet) chain;
3042 struct ata_promise_sx4 {
3043 struct spinlock mtx;
3044 TAILQ_HEAD(, host_packet) queue;
3049 ata_promise_ident(device_t dev)
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}};
3092 uintptr_t devid = 0;
3094 if (!(idx = ata_match_chip(dev, ids)))
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)
3104 strcpy(buffer, "Promise ");
3105 strcat(buffer, idx->text);
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;
3114 if (pci_get_slot(dev) == 1) {
3115 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
3116 strcat(buffer, " (channel 0+1)");
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)");
3126 ksprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
3127 device_set_desc_copy(dev, buffer);
3129 ctlr->chipinit = ata_promise_chipinit;
3134 ata_promise_chipinit(device_t dev)
3136 struct ata_pci_controller *ctlr = device_get_softc(dev);
3137 int fake_reg, stat_reg;
3139 if (ata_setup_interrupt(dev))
3142 switch (ctlr->chip->cfg1) {
3145 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
3147 ctlr->dmainit = ata_promise_dmainit;
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;
3158 ctlr->allocate = ata_promise_tx2_allocate;
3159 ctlr->setmode = ata_promise_setmode;
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)))
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)))
3175 if (ctlr->chip->cfg2 == PRSX4X) {
3176 struct ata_promise_sx4 *hpkt;
3177 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
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");
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" : "" );
3193 /* adjust cache memory parameters */
3194 ATA_OUTL(ctlr->r_res2, 0x000c000c,
3195 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
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);
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;
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");
3220 switch (ctlr->chip->cfg2) {
3222 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
3223 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
3247 /* prime fake interrupt register */
3248 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3250 /* clear SATA status */
3251 ATA_OUTL(ctlr->r_res2, stat_reg, 0x000000ff);
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;
3263 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
3265 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
3270 ata_promise_allocate(device_t dev)
3272 struct ata_channel *ch = device_get_softc(dev);
3274 if (ata_pci_allocate(dev))
3277 ch->hw.status = ata_promise_status;
3282 ata_promise_status(device_t dev)
3284 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3285 struct ata_channel *ch = device_get_softc(dev);
3287 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)) {
3288 return ata_pci_status(dev);
3294 ata_promise_dmastart(device_t dev)
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);
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));
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;
3318 ata_promise_dmastop(device_t dev)
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);
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);
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;
3339 ata_promise_dmareset(device_t dev)
3341 struct ata_channel *ch = device_get_softc(dev);
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;
3350 ata_promise_dmainit(device_t dev)
3352 struct ata_channel *ch = device_get_softc(dev);
3356 ch->dma->start = ata_promise_dmastart;
3357 ch->dma->stop = ata_promise_dmastop;
3358 ch->dma->reset = ata_promise_dmareset;
3363 ata_promise_setmode(device_t dev, int mode)
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);
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 */
3389 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3391 switch (ctlr->chip->cfg1) {
3394 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
3395 (ch->unit ? 1 << 11 : 1 << 10))) {
3396 ata_print_cable(dev, "controller");
3399 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
3400 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
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");
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");
3423 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3426 device_printf(dev, "%ssetting %s on %s chip\n",
3427 (error) ? "FAILURE " : "",
3428 ata_mode2str(mode), ctlr->chip->text);
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;
3439 ata_promise_tx2_allocate(device_t dev)
3441 struct ata_channel *ch = device_get_softc(dev);
3443 if (ata_pci_allocate(dev))
3446 ch->hw.status = ata_promise_tx2_status;
3451 ata_promise_tx2_status(device_t dev)
3453 struct ata_channel *ch = device_get_softc(dev);
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);
3463 ata_promise_mio_allocate(device_t dev)
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;
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);
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;
3488 ch->flags |= ATA_USE_16BIT;
3490 ata_generic_hw(dev);
3491 if (ctlr->chip->cfg2 & PRSX4X) {
3492 ch->hw.command = ata_promise_sx4_command;
3495 ch->hw.command = ata_promise_mio_command;
3496 ch->hw.status = ata_promise_mio_status;
3502 ata_promise_mio_intr(void *data)
3504 struct ata_pci_controller *ctlr = data;
3505 struct ata_channel *ch;
3509 switch (ctlr->chip->cfg2) {
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".
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);
3533 for (unit = 0; unit < ctlr->channels; unit++) {
3534 if ((ch = ctlr->interrupt[unit].argument))
3535 ctlr->interrupt[unit].function(ch);
3538 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3542 ata_promise_mio_status(device_t dev)
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;
3549 switch (ctlr->chip->cfg2) {
3564 /* read and acknowledge interrupt */
3565 vector = ATA_INL(ctlr->r_res2, fake_reg);
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));
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))) {
3578 device_printf(ch->dev, "DISCONNECT requested\n");
3579 tp->action = ATA_C_DETACH;
3581 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3582 taskqueue_enqueue(taskqueue_thread[mycpuid], &tp->task);
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))) {
3592 device_printf(ch->dev, "CONNECT requested\n");
3593 tp->action = ATA_C_ATTACH;
3595 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3596 taskqueue_enqueue(taskqueue_thread[mycpuid], &tp->task);
3599 /* do we have any device action ? */
3600 return (vector & (1 << (ch->unit + 1)));
3604 ata_promise_mio_command(struct ata_request *request)
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;
3610 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
3612 /* XXX SOS add ATAPI commands support later */
3613 switch (request->u.ata.command) {
3615 return ata_generic_command(request);
3618 case ATA_READ_DMA48:
3619 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
3623 case ATA_WRITE_DMA48:
3624 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
3627 wordp[1] = htole32(ch->dma->sg_bus);
3629 ata_promise_apkt((u_int8_t*)wordp, request);
3631 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
3636 ata_promise_mio_reset(device_t dev)
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;
3642 switch (ctlr->chip->cfg2) {
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);
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));
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));
3658 ATA_OUTL(ctlr->r_res2, 0xc012c,
3659 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
3661 spin_unlock_wr(&hpktp->mtx);
3662 ata_generic_reset(dev);
3668 if ((ctlr->chip->cfg2 == PRSATA) ||
3669 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3671 /* mask plug/unplug intr */
3672 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
3675 /* softreset channels ATA module */
3676 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
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));
3682 if ((ctlr->chip->cfg2 == PRSATA) ||
3683 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3685 if (ata_sata_phy_reset(dev))
3686 ata_generic_reset(dev);
3688 /* reset and enable plug/unplug intr */
3689 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
3692 ata_generic_reset(dev);
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);
3702 /* mask plug/unplug intr */
3703 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
3706 /* softreset channels ATA module */
3707 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
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));
3713 if ((ctlr->chip->cfg2 == PRSATA2) ||
3714 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
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);
3721 if (ata_sata_phy_reset(dev))
3722 ata_generic_reset(dev);
3724 /* reset and enable plug/unplug intr */
3725 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
3727 /* set portmultiplier port */
3728 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
3731 ata_generic_reset(dev);
3738 ata_promise_mio_dmainit(device_t dev)
3740 /* note start and stop are not used here */
3745 ata_promise_mio_setmode(device_t dev, int mode)
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));
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);
3757 ata_promise_setmode(dev, mode);
3761 ata_promise_sx4_intr(void *data)
3763 struct ata_pci_controller *ctlr = data;
3764 struct ata_channel *ch;
3765 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
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);
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));
3793 ata_promise_sx4_command(struct ata_request *request)
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);
3801 int i, idx, length = 0;
3803 /* XXX SOS add ATAPI commands support later */
3804 switch (request->u.ata.command) {
3809 case ATA_ATA_IDENTIFY:
3813 case ATA_READ_MUL48:
3817 case ATA_WRITE_MUL48:
3818 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
3819 return ata_generic_command(request);
3821 case ATA_SETFEATURES:
3822 case ATA_FLUSHCACHE:
3823 case ATA_FLUSHCACHE48:
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));
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));
3839 case ATA_READ_DMA48:
3841 case ATA_WRITE_DMA48:
3842 wordp = (u_int32_t *)
3843 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
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));
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);
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);
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);
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);
3879 ata_promise_apkt((u_int8_t *)wordp, request);
3880 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
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));
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));
3899 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
3901 struct ata_device *atadev = device_get_softc(request->dev);
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;
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;
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);
3943 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
3944 bytep[i++] = request->u.ata.command;
3949 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
3951 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
3953 spin_lock_wr(&hpktp->mtx);
3955 struct host_packet *hp =
3956 kmalloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
3958 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
3962 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
3964 spin_unlock_wr(&hpktp->mtx);
3968 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
3970 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
3971 struct host_packet *hp;
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);
3981 spin_unlock_wr(&hpktp->mtx);
3986 * ServerWorks chipset support functions
3989 ata_serverworks_ident(device_t dev)
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}};
4008 if (!(idx = ata_match_chip(dev, ids)))
4011 ksprintf(buffer, "ServerWorks %s %s controller",
4012 idx->text, ata_mode2str(idx->max_dma));
4013 device_set_desc_copy(dev, buffer);
4015 ctlr->chipinit = ata_serverworks_chipinit;
4020 ata_serverworks_chipinit(device_t dev)
4022 struct ata_pci_controller *ctlr = device_get_softc(dev);
4024 if (ata_setup_interrupt(dev))
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)))
4034 ctlr->channels = ctlr->chip->cfg2;
4035 ctlr->allocate = ata_serverworks_allocate;
4036 ctlr->setmode = ata_sata_setmode;
4039 else if (ctlr->chip->cfg1 == SWKS33) {
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);
4053 kfree(children, M_TEMP);
4057 pci_write_config(dev, 0x5a,
4058 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
4059 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
4061 ctlr->setmode = ata_serverworks_setmode;
4066 ata_serverworks_allocate(device_t dev)
4068 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4069 struct ata_channel *ch = device_get_softc(dev);
4073 ch_offset = ch->unit * 0x100;
4075 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
4076 ch->r_io[i].res = ctlr->r_res2;
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);
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;
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;
4100 ch->flags |= ATA_NO_SLAVE;
4106 ata_serverworks_setmode(device_t dev, int mode)
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;
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 };
4119 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4121 mode = ata_check_80pin(dev, mode);
4123 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4126 device_printf(dev, "%ssetting %s on %s chip\n",
4127 (error) ? "FAILURE " : "",
4128 ata_mode2str(mode), ctlr->chip->text);
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);
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);
4153 pci_write_config(gparent, 0x54,
4154 pci_read_config(gparent, 0x54, 1) &
4155 ~(0x01 << devno), 1);
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;
4167 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
4170 ata_sii_ident(device_t dev)
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}};
4192 if (!(idx = ata_match_chip(dev, ids)))
4195 ksprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
4196 device_set_desc_copy(dev, buffer);
4198 ctlr->chipinit = ata_sii_chipinit;
4203 ata_sii_chipinit(device_t dev)
4205 struct ata_pci_controller *ctlr = device_get_softc(dev);
4207 if (ata_setup_interrupt(dev))
4210 switch (ctlr->chip->cfg1) {
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)))
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);
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;
4231 /* reset controller */
4232 ATA_OUTL(ctlr->r_res1, 0x0040, 0x80000000);
4234 ATA_OUTL(ctlr->r_res1, 0x0040, 0x0000000f);
4236 /* enable PCI interrupt */
4237 pci_write_config(dev, PCIR_COMMAND,
4238 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
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)))
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",
4257 /* if we have 4 channels enable the second set */
4258 if (ctlr->chip->cfg2 & SII4CH) {
4259 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
4263 /* dont block interrupts from any channel */
4264 pci_write_config(dev, 0x48,
4265 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
4267 /* enable PCI interrupt as BIOS might not */
4268 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
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;
4276 ctlr->setmode = ata_sii_setmode;
4280 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
4281 device_printf(dev, "HW has secondary channel disabled\n");
4285 /* enable interrupt as BIOS might not */
4286 pci_write_config(dev, 0x71, 0x01, 1);
4288 ctlr->allocate = ata_cmd_allocate;
4289 ctlr->setmode = ata_cmd_setmode;
4296 ata_cmd_allocate(device_t dev)
4298 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4299 struct ata_channel *ch = device_get_softc(dev);
4301 /* setup the usual register normal pci style */
4302 if (ata_pci_allocate(dev))
4305 if (ctlr->chip->cfg2 & SIIINTR)
4306 ch->hw.status = ata_cmd_status;
4312 ata_cmd_status(device_t dev)
4314 struct ata_channel *ch = device_get_softc(dev);
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);
4327 ata_cmd_setmode(device_t dev, int mode)
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);
4336 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4338 mode = ata_check_80pin(dev, mode);
4340 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4343 device_printf(dev, "%ssetting %s on %s chip\n",
4344 (error) ? "FAILURE " : "",
4345 ata_mode2str(mode), ctlr->chip->text);
4347 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
4348 int ureg = ch->unit ? 0x7b : 0x73;
4350 if (mode >= ATA_UDMA0) {
4351 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
4352 { 0x11, 0x42 }, { 0x25, 0x8a },
4353 { 0x15, 0x4a }, { 0x05, 0x0a } };
4355 u_int8_t umode = pci_read_config(gparent, ureg, 1);
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);
4361 else if (mode >= ATA_WDMA0) {
4362 int dmatimings[] = { 0x87, 0x32, 0x3f };
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);
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);
4377 atadev->mode = mode;
4382 ata_sii_allocate(device_t dev)
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);
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);
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);
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);
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;
4414 /* enable PHY state change interrupt */
4415 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
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;
4425 ch->hw.status = ata_sii_status;
4430 ata_sii_status(device_t dev)
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);
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);
4442 if (ATA_INL(ctlr->r_res2, 0xa0 + offset1) & 0x00000800)
4443 return ata_pci_status(dev);
4449 ata_sii_reset(device_t dev)
4451 if (ata_sata_phy_reset(dev))
4452 ata_generic_reset(dev);
4456 ata_sii_setmode(device_t dev, int mode)
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;
4468 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
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");
4478 mode = ata_check_80pin(dev, mode);
4480 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4483 device_printf(dev, "%ssetting %s on %s chip\n",
4484 (error) ? "FAILURE " : "",
4485 ata_mode2str(mode), ctlr->chip->text);
4489 if (mode >= ATA_UDMA0) {
4490 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
4491 u_int8_t ureg = 0xac + rego;
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);
4500 else if (mode >= ATA_WDMA0) {
4501 u_int8_t dreg = 0xa8 + rego;
4502 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
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);
4510 u_int8_t preg = 0xa4 + rego;
4511 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
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);
4517 atadev->mode = mode;
4520 struct ata_siiprb_dma_prdentry {
4526 struct ata_siiprb_ata_command {
4527 u_int32_t reserved0;
4528 struct ata_siiprb_dma_prdentry prd[126];
4531 struct ata_siiprb_atapi_command {
4533 struct ata_siiprb_dma_prdentry prd[125];
4536 struct ata_siiprb_command {
4538 u_int16_t protocol_override;
4539 u_int32_t transfer_count;
4542 struct ata_siiprb_ata_command ata;
4543 struct ata_siiprb_atapi_command atapi;
4548 ata_siiprb_allocate(device_t dev)
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;
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;
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 */
4572 ata_siiprb_status(device_t dev)
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;
4578 if ((ATA_INL(ctlr->r_res1, 0x0044) & (1 << ch->unit))) {
4579 u_int32_t istatus = ATA_INL(ctlr->r_res2, 0x1008 + offset);
4581 /* do we have any PHY events ? */
4582 ata_sata_phy_check_events(dev);
4584 /* clear interrupt(s) */
4585 ATA_OUTL(ctlr->r_res2, 0x1008 + offset, istatus);
4587 /* do we have any device action ? */
4588 return (istatus & 0x00000001);
4594 ata_siiprb_begin_transaction(struct ata_request *request)
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;
4603 /* check for 48 bit access and convert if needed */
4604 ata_modify_if_48bit(request);
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));
4610 /* set basic prd options ata/atapi etc etc */
4611 bzero(prb, sizeof(struct ata_siiprb_command));
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;
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;
4624 if (request->flags & ATA_R_ATAPI)
4625 prd = &prb->u.atapi.prd[0];
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;
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);
4643 /* start the timeout */
4644 callout_reset(&request->callout, request->timeout * hz,
4645 (timeout_t*)ata_timeout, request);
4646 return ATA_OP_CONTINUES;
4650 ata_siiprb_end_transaction(struct ata_request *request)
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;
4658 /* kill the timeout */
4659 callout_stop(&request->callout);
4661 prb = (struct ata_siiprb_command *)
4662 ((u_int8_t *)rman_get_virtual(ctlr->r_res2) + (tag << 7) + offset);
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];
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;
4674 request->donecount = request->bytecount;
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);
4683 /* release SG list etc */
4684 ch->dma->unload(ch->dev);
4686 return ATA_OP_FINISHED;
4690 ata_siiprb_reset(device_t dev)
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;
4697 u_int32_t status, signature;
4698 int timeout, tag = 0;
4700 /* reset channel HW */
4701 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000001);
4703 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000001);
4706 /* poll for channel ready */
4707 for (timeout = 0; timeout < 1000; timeout++) {
4708 if ((status = ATA_INL(ctlr->r_res2, 0x1000 + offset)) & 0x00040000)
4712 if (timeout >= 1000) {
4713 device_printf(ch->dev, "channel HW reset timeout reset failure\n");
4718 device_printf(ch->dev, "channel HW reset time=%dms\n", timeout * 1);
4721 if (!ata_sata_phy_reset(dev)) {
4723 device_printf(ch->dev, "phy reset found no device\n");
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);
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);
4741 /* poll for channel ready */
4742 for (timeout = 0; timeout < 1000; timeout++) {
4744 if ((status = ATA_INL(ctlr->r_res2, 0x1008 + offset)) & 0x00010000)
4747 if (timeout >= 1000) {
4748 device_printf(ch->dev, "reset timeout - no device found\n");
4753 device_printf(ch->dev, "soft reset exec time=%dms status=%08x\n",
4756 /* find out whats there */
4757 prb = (struct ata_siiprb_command *)
4758 ((u_int8_t *)rman_get_virtual(ctlr->r_res2) + (tag << 7) + offset);
4760 prb->fis[12]|(prb->fis[4]<<8)|(prb->fis[5]<<16)|(prb->fis[6]<<24);
4762 device_printf(ch->dev, "signature=%08x\n", signature);
4763 switch (signature) {
4765 ch->devices = ATA_ATAPI_MASTER;
4766 device_printf(ch->dev, "SATA ATAPI devices not supported yet\n");
4770 ch->devices = ATA_PORTMULTIPLIER;
4771 device_printf(ch->dev, "Portmultipliers not supported yet\n");
4775 ch->devices = ATA_ATA_MASTER;
4782 /* clear interrupt(s) */
4783 ATA_OUTL(ctlr->r_res2, 0x1008 + offset, 0x000008ff);
4785 /* require explicit interrupt ack */
4786 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000008);
4789 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000400);
4791 /* enable interrupts wanted */
4792 ATA_OUTL(ctlr->r_res2, 0x1010 + offset, 0x000000ff);
4796 ata_siiprb_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
4798 struct ata_dmasetprd_args *args = xsc;
4799 struct ata_siiprb_dma_prdentry *prd = args->dmatab;
4802 if ((args->error = error))
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);
4809 prd[i - 1].control = htole32(ATA_DMA_EOT);
4813 ata_siiprb_dmainit(device_t dev)
4815 struct ata_channel *ch = device_get_softc(dev);
4819 /* note start and stop are not used here */
4820 ch->dma->setprd = ata_siiprb_dmasetprd;
4821 ch->dma->max_address = BUS_SPACE_MAXADDR;
4827 * Silicon Integrated Systems Corp. (SiS) chipset support functions
4830 ata_sis_ident(device_t dev)
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 */
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 */
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 */
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" },
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 }};
4864 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
4867 if (idx->cfg2 && !found) {
4868 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
4870 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
4871 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
4873 idx->cfg1 = SIS133NEW;
4874 idx->max_dma = ATA_UDMA6;
4875 ksprintf(buffer, "SiS 962/963 %s controller",
4876 ata_mode2str(idx->max_dma));
4878 pci_write_config(dev, 0x57, reg57, 1);
4880 if (idx->cfg2 && !found) {
4881 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
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 }};
4889 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
4890 idx->cfg1 = SIS133OLD;
4891 idx->max_dma = ATA_UDMA6;
4894 idx->cfg1 = SIS100NEW;
4895 idx->max_dma = ATA_UDMA5;
4897 ksprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
4899 pci_write_config(dev, 0x4a, reg4a, 1);
4902 ksprintf(buffer,"SiS %s %s controller",
4903 idx->text, ata_mode2str(idx->max_dma));
4905 device_set_desc_copy(dev, buffer);
4907 ctlr->chipinit = ata_sis_chipinit;
4912 ata_sis_chipinit(device_t dev)
4914 struct ata_pci_controller *ctlr = device_get_softc(dev);
4916 if (ata_setup_interrupt(dev))
4919 switch (ctlr->chip->cfg1) {
4924 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
4928 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
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);
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;
4942 /* enable PCI interrupt */
4943 pci_write_config(dev, PCIR_COMMAND,
4944 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
4946 ctlr->setmode = ata_sata_setmode;
4951 ctlr->setmode = ata_sis_setmode;
4956 ata_sis_allocate(device_t dev)
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);
4962 /* setup the usual register normal pci style */
4963 if (ata_pci_allocate(dev))
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;
4974 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
4975 /* XXX SOS unknown how to enable PHY state change interrupt */
4980 ata_sis_reset(device_t dev)
4982 if (ata_sata_phy_reset(dev))
4983 ata_generic_reset(dev);
4987 ata_sis_setmode(device_t dev, int mode)
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);
4996 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
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");
5006 if (mode > ATA_UDMA2 &&
5007 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
5008 ata_print_cable(dev, "controller");
5013 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5016 device_printf(dev, "%ssetting %s on %s chip\n",
5017 (error) ? "FAILURE " : "",
5018 ata_mode2str(mode), ctlr->chip->text);
5020 switch (ctlr->chip->cfg1) {
5022 u_int32_t timings[] =
5023 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
5024 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
5025 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
5028 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
5029 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
5033 u_int16_t timings[] =
5034 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
5035 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
5037 u_int16_t reg = 0x40 + (devno << 1);
5039 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
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);
5048 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
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);
5059 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5063 atadev->mode = mode;
5068 /* VIA Technologies Inc. chipset support functions */
5070 ata_via_ident(device_t dev)
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 }};
5100 if (pci_get_devid(dev) == ATA_VIA82C571) {
5101 if (!(idx = ata_find_chip(dev, ids, -99)))
5105 if (!(idx = ata_match_chip(dev, new_ids)))
5109 ksprintf(buffer, "VIA %s %s controller",
5110 idx->text, ata_mode2str(idx->max_dma));
5111 device_set_desc_copy(dev, buffer);
5113 ctlr->chipinit = ata_via_chipinit;
5118 ata_via_chipinit(device_t dev)
5120 struct ata_pci_controller *ctlr = device_get_softc(dev);
5122 if (ata_setup_interrupt(dev))
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,
5132 return ata_ahci_chipinit(dev);
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;
5142 /* enable PCI interrupt */
5143 pci_write_config(dev, PCIR_COMMAND,
5144 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
5147 if (ctlr->chip->cfg2 & VIABAR) {
5149 ctlr->setmode = ata_via_setmode;
5152 ctlr->setmode = ata_sata_setmode;
5156 /* prepare for ATA-66 on the 82C686a and 82C596b */
5157 if (ctlr->chip->cfg2 & VIACLK)
5158 pci_write_config(dev, 0x50, 0x030b030b, 4);
5160 /* the southbridge might need the data corruption fix */
5161 if (ctlr->chip->cfg2 & VIABUG)
5162 ata_via_southbridge_fixup(dev);
5164 /* set fifo configuration half'n'half */
5165 pci_write_config(dev, 0x43,
5166 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
5168 /* set status register read retry */
5169 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
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);
5175 /* set sector size */
5176 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
5177 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
5179 ctlr->setmode = ata_via_family_setmode;
5184 ata_via_allocate(device_t dev)
5186 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5187 struct ata_channel *ch = device_get_softc(dev);
5189 /* newer SATA chips has resources in one BAR for each channel */
5190 if (ctlr->chip->cfg2 & VIABAR) {
5191 struct resource *r_io;
5194 rid = PCIR_BAR(ch->unit);
5195 if (!(r_io = bus_alloc_resource_any(device_get_parent(dev),
5200 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
5201 ch->r_io[i].res = r_io;
5202 ch->r_io[i].offset = i;
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);
5217 /* setup the usual register normal pci style */
5218 if (ata_pci_allocate(dev))
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;
5230 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
5231 /* XXX SOS unknown how to enable PHY state change interrupt */
5236 ata_via_reset(device_t dev)
5238 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5239 struct ata_channel *ch = device_get_softc(dev);
5241 if ((ctlr->chip->cfg2 & VIABAR) && (ch->unit > 1))
5242 ata_generic_reset(dev);
5244 if (ata_sata_phy_reset(dev))
5245 ata_generic_reset(dev);
5249 ata_via_setmode(device_t dev, int mode)
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);
5257 if ((ctlr->chip->cfg2 & VIABAR) && (ch->unit > 1)) {
5258 u_int8_t pio_timings[] = { 0xa8, 0x65, 0x65, 0x32, 0x20,
5260 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
5261 u_int8_t dma_timings[] = { 0xee, 0xe8, 0xe6, 0xe4, 0xe2, 0xe1, 0xe0 };
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);
5266 device_printf(dev, "%ssetting %s on %s chip\n",
5267 (error) ? "FAILURE " : "", ata_mode2str(mode),
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;
5278 ata_sata_setmode(dev, mode);
5282 ata_via_southbridge_fixup(device_t dev)
5287 if (device_get_children(device_get_parent(dev), &children, &nchildren))
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);
5297 if ((reg76 & 0xf0) != 0xd0) {
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);
5306 kfree(children, M_TEMP);
5310 /* common code for VIA, AMD & nVidia */
5312 ata_via_family_setmode(device_t dev, int mode)
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 };
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;
5330 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
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");
5340 mode = ata_check_80pin(dev, mode);
5342 if (ctlr->chip->cfg2 & NVIDIA)
5345 if (ctlr->chip->cfg1 != VIA133)
5346 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
5348 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5351 device_printf(dev, "%ssetting %s on %s chip\n",
5352 (error) ? "FAILURE " : "", ata_mode2str(mode),
5355 if (mode >= ATA_UDMA0)
5356 pci_write_config(gparent, reg,
5357 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
5359 pci_write_config(gparent, reg, 0x8b, 1);
5360 atadev->mode = mode;
5365 /* misc functions */
5366 static struct ata_chip_id *
5367 ata_match_chip(device_t dev, struct ata_chip_id *index)
5369 while (index->chipid != 0) {
5370 if (pci_get_devid(dev) == index->chipid &&
5371 pci_get_revid(dev) >= index->chiprev)
5378 static struct ata_chip_id *
5379 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
5384 if (device_get_children(device_get_parent(dev), &children, &nchildren))
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);
5399 kfree(children, M_TEMP);
5404 ata_setup_interrupt(device_t dev)
5406 struct ata_pci_controller *ctlr = device_get_softc(dev);
5407 int rid = ATA_IRQ_RID;
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");
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");
5424 struct ata_serialize {
5425 struct spinlock locked_mtx;
5431 ata_serialize(device_t dev, int flags)
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;
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);
5449 serial = device_get_ivars(ctlr->dev);
5451 spin_lock_wr(&serial->locked_mtx);
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;
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);
5477 res = serial->locked_ch;
5478 spin_unlock_wr(&serial->locked_mtx);
5483 ata_print_cable(device_t dev, u_int8_t *who)
5486 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
5490 ata_atapi(device_t dev)
5492 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5493 struct ata_device *atadev = device_get_softc(dev);
5495 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
5496 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
5500 ata_check_80pin(device_t dev, int mode)
5502 struct ata_device *atadev = device_get_softc(dev);
5504 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
5505 ata_print_cable(dev, "device");
5512 ata_mode2idx(int mode)
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;