2 * Copyright (c) 1998 - 2007 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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
36 #include <sys/endian.h>
37 #include <sys/malloc.h>
39 #include <sys/mutex.h>
41 #include <sys/taskqueue.h>
43 #include <machine/stdarg.h>
44 #include <machine/resource.h>
45 #include <machine/bus.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/ata/ata-all.h>
50 #include <dev/ata/ata-pci.h>
53 /* local prototypes */
55 static int ata_generic_chipinit(device_t dev);
56 static void ata_generic_intr(void *data);
57 static void ata_generic_setmode(device_t dev, int mode);
58 static void ata_sata_phy_check_events(device_t dev);
59 static void ata_sata_phy_event(void *context, int dummy);
60 static int ata_sata_phy_reset(device_t dev);
61 static int ata_sata_connect(struct ata_channel *ch);
62 static void ata_sata_setmode(device_t dev, int mode);
63 static int ata_request2fis_h2d(struct ata_request *request, u_int8_t *fis);
64 static int ata_ahci_chipinit(device_t dev);
65 static int ata_ahci_allocate(device_t dev);
66 static int ata_ahci_status(device_t dev);
67 static int ata_ahci_begin_transaction(struct ata_request *request);
68 static int ata_ahci_end_transaction(struct ata_request *request);
69 static void ata_ahci_reset(device_t dev);
70 static void ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
71 static void ata_ahci_dmainit(device_t dev);
72 static int ata_ahci_setup_fis(struct ata_ahci_cmd_tab *ctp, struct ata_request *request);
73 static int ata_acard_chipinit(device_t dev);
74 static int ata_acard_allocate(device_t dev);
75 static int ata_acard_status(device_t dev);
76 static void ata_acard_850_setmode(device_t dev, int mode);
77 static void ata_acard_86X_setmode(device_t dev, int mode);
78 static int ata_ali_chipinit(device_t dev);
79 static int ata_ali_allocate(device_t dev);
80 static int ata_ali_sata_allocate(device_t dev);
81 static void ata_ali_reset(device_t dev);
82 static void ata_ali_setmode(device_t dev, int mode);
83 static int ata_amd_chipinit(device_t dev);
84 static int ata_ati_chipinit(device_t dev);
85 static void ata_ati_setmode(device_t dev, int mode);
86 static int ata_cyrix_chipinit(device_t dev);
87 static void ata_cyrix_setmode(device_t dev, int mode);
88 static int ata_cypress_chipinit(device_t dev);
89 static void ata_cypress_setmode(device_t dev, int mode);
90 static int ata_highpoint_chipinit(device_t dev);
91 static int ata_highpoint_allocate(device_t dev);
92 static void ata_highpoint_setmode(device_t dev, int mode);
93 static int ata_highpoint_check_80pin(device_t dev, int mode);
94 static int ata_intel_chipinit(device_t dev);
95 static int ata_intel_allocate(device_t dev);
96 static void ata_intel_reset(device_t dev);
97 static void ata_intel_old_setmode(device_t dev, int mode);
98 static void ata_intel_new_setmode(device_t dev, int mode);
99 static int ata_intel_31244_allocate(device_t dev);
100 static int ata_intel_31244_status(device_t dev);
101 static int ata_intel_31244_command(struct ata_request *request);
102 static void ata_intel_31244_reset(device_t dev);
103 static int ata_ite_chipinit(device_t dev);
104 static void ata_ite_setmode(device_t dev, int mode);
105 static int ata_jmicron_chipinit(device_t dev);
106 static int ata_jmicron_allocate(device_t dev);
107 static void ata_jmicron_reset(device_t dev);
108 static void ata_jmicron_dmainit(device_t dev);
109 static void ata_jmicron_setmode(device_t dev, int mode);
110 static int ata_marvell_pata_chipinit(device_t dev);
111 static int ata_marvell_pata_allocate(device_t dev);
112 static void ata_marvell_pata_setmode(device_t dev, int mode);
113 static int ata_marvell_edma_chipinit(device_t dev);
114 static int ata_marvell_edma_allocate(device_t dev);
115 static int ata_marvell_edma_status(device_t dev);
116 static int ata_marvell_edma_begin_transaction(struct ata_request *request);
117 static int ata_marvell_edma_end_transaction(struct ata_request *request);
118 static void ata_marvell_edma_reset(device_t dev);
119 static void ata_marvell_edma_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
120 static void ata_marvell_edma_dmainit(device_t dev);
121 static int ata_national_chipinit(device_t dev);
122 static void ata_national_setmode(device_t dev, int mode);
123 static int ata_netcell_chipinit(device_t dev);
124 static int ata_netcell_allocate(device_t dev);
125 static int ata_nvidia_chipinit(device_t dev);
126 static int ata_nvidia_allocate(device_t dev);
127 static int ata_nvidia_status(device_t dev);
128 static void ata_nvidia_reset(device_t dev);
129 static int ata_promise_chipinit(device_t dev);
130 static int ata_promise_allocate(device_t dev);
131 static int ata_promise_status(device_t dev);
132 static int ata_promise_dmastart(device_t dev);
133 static int ata_promise_dmastop(device_t dev);
134 static void ata_promise_dmareset(device_t dev);
135 static void ata_promise_dmainit(device_t dev);
136 static void ata_promise_setmode(device_t dev, int mode);
137 static int ata_promise_tx2_allocate(device_t dev);
138 static int ata_promise_tx2_status(device_t dev);
139 static int ata_promise_mio_allocate(device_t dev);
140 static void ata_promise_mio_intr(void *data);
141 static int ata_promise_mio_status(device_t dev);
142 static int ata_promise_mio_command(struct ata_request *request);
143 static void ata_promise_mio_reset(device_t dev);
144 static void ata_promise_mio_dmainit(device_t dev);
145 static void ata_promise_mio_setmode(device_t dev, int mode);
146 static void ata_promise_sx4_intr(void *data);
147 static int ata_promise_sx4_command(struct ata_request *request);
148 static int ata_promise_apkt(u_int8_t *bytep, struct ata_request *request);
149 static void ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt);
150 static void ata_promise_next_hpkt(struct ata_pci_controller *ctlr);
151 static int ata_serverworks_chipinit(device_t dev);
152 static int ata_serverworks_allocate(device_t dev);
153 static void ata_serverworks_setmode(device_t dev, int mode);
154 static int ata_sii_chipinit(device_t dev);
155 static int ata_cmd_allocate(device_t dev);
156 static int ata_cmd_status(device_t dev);
157 static void ata_cmd_setmode(device_t dev, int mode);
158 static int ata_sii_allocate(device_t dev);
159 static int ata_sii_status(device_t dev);
160 static void ata_sii_reset(device_t dev);
161 static void ata_sii_setmode(device_t dev, int mode);
162 static int ata_siiprb_allocate(device_t dev);
163 static int ata_siiprb_status(device_t dev);
164 static int ata_siiprb_begin_transaction(struct ata_request *request);
165 static int ata_siiprb_end_transaction(struct ata_request *request);
166 static void ata_siiprb_reset(device_t dev);
167 static void ata_siiprb_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
168 static void ata_siiprb_dmainit(device_t dev);
169 static int ata_sis_chipinit(device_t dev);
170 static int ata_sis_allocate(device_t dev);
171 static void ata_sis_reset(device_t dev);
172 static void ata_sis_setmode(device_t dev, int mode);
173 static int ata_via_chipinit(device_t dev);
174 static int ata_via_allocate(device_t dev);
175 static void ata_via_reset(device_t dev);
176 static void ata_via_setmode(device_t dev, int mode);
177 static void ata_via_southbridge_fixup(device_t dev);
178 static void ata_via_family_setmode(device_t dev, int mode);
179 static struct ata_chip_id *ata_match_chip(device_t dev, struct ata_chip_id *index);
180 static struct ata_chip_id *ata_find_chip(device_t dev, struct ata_chip_id *index, int slot);
181 static int ata_setup_interrupt(device_t dev);
182 static int ata_serialize(device_t dev, int flags);
183 static void ata_print_cable(device_t dev, u_int8_t *who);
184 static int ata_atapi(device_t dev);
185 static int ata_check_80pin(device_t dev, int mode);
186 static int ata_mode2idx(int mode);
190 * generic ATA support functions
193 ata_generic_ident(device_t dev)
195 struct ata_pci_controller *ctlr = device_get_softc(dev);
197 device_set_desc(dev, "GENERIC ATA controller");
198 ctlr->chipinit = ata_generic_chipinit;
203 ata_generic_chipinit(device_t dev)
205 struct ata_pci_controller *ctlr = device_get_softc(dev);
207 if (ata_setup_interrupt(dev))
209 ctlr->setmode = ata_generic_setmode;
214 ata_generic_intr(void *data)
216 struct ata_pci_controller *ctlr = data;
217 struct ata_channel *ch;
220 for (unit = 0; unit < ctlr->channels; unit++) {
221 if ((ch = ctlr->interrupt[unit].argument))
222 ctlr->interrupt[unit].function(ch);
227 ata_generic_setmode(device_t dev, int mode)
229 struct ata_device *atadev = device_get_softc(dev);
231 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
232 mode = ata_check_80pin(dev, mode);
233 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
239 * SATA support functions
242 ata_sata_phy_check_events(device_t dev)
244 struct ata_channel *ch = device_get_softc(dev);
245 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
247 /* clear error bits/interrupt */
248 ATA_IDX_OUTL(ch, ATA_SERROR, error);
250 /* do we have any events flagged ? */
252 struct ata_connect_task *tp;
253 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
255 /* if we have a connection event deal with it */
256 if ((error & ATA_SE_PHY_CHANGED) &&
257 (tp = (struct ata_connect_task *)
258 malloc(sizeof(struct ata_connect_task),
259 M_ATA, M_NOWAIT | M_ZERO))) {
261 if (((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) ||
262 ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)) {
264 device_printf(ch->dev, "CONNECT requested\n");
265 tp->action = ATA_C_ATTACH;
269 device_printf(ch->dev, "DISCONNECT requested\n");
270 tp->action = ATA_C_DETACH;
273 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
274 taskqueue_enqueue(taskqueue_thread, &tp->task);
280 ata_sata_phy_event(void *context, int dummy)
282 struct ata_connect_task *tp = (struct ata_connect_task *)context;
283 struct ata_channel *ch = device_get_softc(tp->dev);
287 mtx_lock(&Giant); /* newbus suckage it needs Giant */
288 if (tp->action == ATA_C_ATTACH) {
290 device_printf(tp->dev, "CONNECTED\n");
292 ata_identify(tp->dev);
294 if (tp->action == ATA_C_DETACH) {
295 if (!device_get_children(tp->dev, &children, &nchildren)) {
296 for (i = 0; i < nchildren; i++)
298 device_delete_child(tp->dev, children[i]);
299 free(children, M_TEMP);
301 mtx_lock(&ch->state_mtx);
302 ch->state = ATA_IDLE;
303 mtx_unlock(&ch->state_mtx);
305 device_printf(tp->dev, "DISCONNECTED\n");
307 mtx_unlock(&Giant); /* suckage code dealt with, release Giant */
312 ata_sata_phy_reset(device_t dev)
314 struct ata_channel *ch = device_get_softc(dev);
317 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE)
318 return ata_sata_connect(ch);
320 for (retry = 0; retry < 10; retry++) {
321 for (loop = 0; loop < 10; loop++) {
322 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_RESET);
324 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) ==
329 for (loop = 0; loop < 10; loop++) {
330 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_IDLE |
331 ATA_SC_IPM_DIS_PARTIAL |
332 ATA_SC_IPM_DIS_SLUMBER);
334 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == 0)
335 return ata_sata_connect(ch);
342 ata_sata_connect(struct ata_channel *ch)
347 /* wait up to 1 second for "connect well" */
348 for (timeout = 0; timeout < 100 ; timeout++) {
349 status = ATA_IDX_INL(ch, ATA_SSTATUS);
350 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1 ||
351 (status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)
355 if (timeout >= 100) {
357 device_printf(ch->dev, "SATA connect status=%08x\n", status);
361 device_printf(ch->dev, "SATA connect time=%dms\n", timeout * 10);
363 /* clear SATA error register */
364 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
370 ata_sata_setmode(device_t dev, int mode)
372 struct ata_device *atadev = device_get_softc(dev);
375 * if we detect that the device isn't a real SATA device we limit
376 * the transfer mode to UDMA5/ATA100.
377 * this works around the problems some devices has with the
378 * Marvell 88SX8030 SATA->PATA converters and UDMA6/ATA133.
380 if (atadev->param.satacapabilities != 0x0000 &&
381 atadev->param.satacapabilities != 0xffff) {
382 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
384 /* on some drives we need to set the transfer mode */
385 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
386 ata_limit_mode(dev, mode, ATA_UDMA6));
388 /* query SATA STATUS for the speed */
389 if (ch->r_io[ATA_SSTATUS].res &&
390 ((ATA_IDX_INL(ch, ATA_SSTATUS) & ATA_SS_CONWELL_MASK) ==
391 ATA_SS_CONWELL_GEN2))
392 atadev->mode = ATA_SA300;
394 atadev->mode = ATA_SA150;
397 mode = ata_limit_mode(dev, mode, ATA_UDMA5);
398 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
404 ata_request2fis_h2d(struct ata_request *request, u_int8_t *fis)
406 struct ata_device *atadev = device_get_softc(request->dev);
408 if (request->flags & ATA_R_ATAPI) {
409 fis[0] = 0x27; /* host to device */
410 fis[1] = 0x80; /* command FIS (note PM goes here) */
411 fis[2] = ATA_PACKET_CMD;
412 if (request->flags & (ATA_R_READ | ATA_R_WRITE))
415 fis[5] = request->transfersize;
416 fis[6] = request->transfersize >> 8;
418 fis[7] = ATA_D_LBA | atadev->unit;
419 fis[15] = ATA_A_4BIT;
423 ata_modify_if_48bit(request);
424 fis[0] = 0x27; /* host to device */
425 fis[1] = 0x80; /* command FIS (note PM goes here) */
426 fis[2] = request->u.ata.command;
427 fis[3] = request->u.ata.feature;
428 fis[4] = request->u.ata.lba;
429 fis[5] = request->u.ata.lba >> 8;
430 fis[6] = request->u.ata.lba >> 16;
431 fis[7] = ATA_D_LBA | atadev->unit;
432 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
433 fis[7] |= (request->u.ata.lba >> 24 & 0x0f);
434 fis[8] = request->u.ata.lba >> 24;
435 fis[9] = request->u.ata.lba >> 32;
436 fis[10] = request->u.ata.lba >> 40;
437 fis[11] = request->u.ata.feature >> 8;
438 fis[12] = request->u.ata.count;
439 fis[13] = request->u.ata.count >> 8;
440 fis[15] = ATA_A_4BIT;
448 * AHCI v1.x compliant SATA chipset support functions
451 ata_ahci_chipinit(device_t dev)
453 struct ata_pci_controller *ctlr = device_get_softc(dev);
456 /* reset AHCI controller */
457 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
458 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) | ATA_AHCI_GHC_HR);
460 if (ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) & ATA_AHCI_GHC_HR) {
461 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
462 device_printf(dev, "AHCI controller reset failure\n");
466 /* enable AHCI mode */
467 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
468 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) | ATA_AHCI_GHC_AE);
470 /* get the number of HW channels */
472 MAX(flsl(ATA_INL(ctlr->r_res2, ATA_AHCI_PI)),
473 (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_NPMASK) + 1);
475 /* clear interrupts */
476 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, ATA_INL(ctlr->r_res2, ATA_AHCI_IS));
478 /* enable AHCI interrupts */
479 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
480 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) | ATA_AHCI_GHC_IE);
482 ctlr->reset = ata_ahci_reset;
483 ctlr->dmainit = ata_ahci_dmainit;
484 ctlr->allocate = ata_ahci_allocate;
485 ctlr->setmode = ata_sata_setmode;
487 /* enable PCI interrupt */
488 pci_write_config(dev, PCIR_COMMAND,
489 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
491 /* announce we support the HW */
492 version = ATA_INL(ctlr->r_res2, ATA_AHCI_VS);
494 "AHCI Version %x%x.%x%x controller with %d ports detected\n",
495 (version >> 24) & 0xff, (version >> 16) & 0xff,
496 (version >> 8) & 0xff, version & 0xff,
497 (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_NPMASK) + 1);
502 ata_ahci_allocate(device_t dev)
504 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
505 struct ata_channel *ch = device_get_softc(dev);
507 int offset = ch->unit << 7;
509 /* set the SATA resources */
510 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
511 ch->r_io[ATA_SSTATUS].offset = ATA_AHCI_P_SSTS + offset;
512 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
513 ch->r_io[ATA_SERROR].offset = ATA_AHCI_P_SERR + offset;
514 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
515 ch->r_io[ATA_SCONTROL].offset = ATA_AHCI_P_SCTL + offset;
516 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
517 ch->r_io[ATA_SACTIVE].offset = ATA_AHCI_P_SACT + offset;
519 ch->hw.status = ata_ahci_status;
520 ch->hw.begin_transaction = ata_ahci_begin_transaction;
521 ch->hw.end_transaction = ata_ahci_end_transaction;
522 ch->hw.command = NULL; /* not used here */
524 /* setup work areas */
525 work = ch->dma->work_bus + ATA_AHCI_CL_OFFSET;
526 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLB + offset, work & 0xffffffff);
527 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLBU + offset, work >> 32);
529 work = ch->dma->work_bus + ATA_AHCI_FB_OFFSET;
530 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FB + offset, work & 0xffffffff);
531 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FBU + offset, work >> 32);
533 /* enable wanted port interrupts */
534 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset,
535 (ATA_AHCI_P_IX_CPD | ATA_AHCI_P_IX_TFE | ATA_AHCI_P_IX_HBF |
536 ATA_AHCI_P_IX_HBD | ATA_AHCI_P_IX_IF | ATA_AHCI_P_IX_OF |
537 ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC | ATA_AHCI_P_IX_DP |
538 ATA_AHCI_P_IX_UF | ATA_AHCI_P_IX_SDB | ATA_AHCI_P_IX_DS |
539 ATA_AHCI_P_IX_PS | ATA_AHCI_P_IX_DHR));
541 /* start operations on this channel */
542 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
543 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
544 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
549 ata_ahci_status(device_t dev)
551 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
552 struct ata_channel *ch = device_get_softc(dev);
553 u_int32_t action = ATA_INL(ctlr->r_res2, ATA_AHCI_IS);
554 int offset = ch->unit << 7;
557 if (action & (1 << ch->unit)) {
558 u_int32_t istatus = ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset);
560 /* clear interrupt(s) */
561 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, action & (1 << ch->unit));
562 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset, istatus);
564 /* do we have any PHY events ? */
565 ata_sata_phy_check_events(dev);
567 /* do we have any device action ? */
568 return (!(ATA_INL(ctlr->r_res2, ATA_AHCI_P_CI + offset) & (1 << tag)));
573 /* must be called with ATA channel locked and state_mtx held */
575 ata_ahci_begin_transaction(struct ata_request *request)
577 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
578 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
579 struct ata_ahci_cmd_tab *ctp;
580 struct ata_ahci_cmd_list *clp;
581 int offset = ch->unit << 7;
582 int tag = 0, entries = 0;
585 /* get a piece of the workspace for this request */
586 ctp = (struct ata_ahci_cmd_tab *)
587 (ch->dma->work + ATA_AHCI_CT_OFFSET + (ATA_AHCI_CT_SIZE * tag));
589 /* setup the FIS for this request */
590 if (!(fis_size = ata_ahci_setup_fis(ctp, request))) {
591 device_printf(request->dev, "setting up SATA FIS failed\n");
592 request->result = EIO;
593 return ATA_OP_FINISHED;
596 /* if request moves data setup and load SG list */
597 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
598 if (ch->dma->load(ch->dev, request->data, request->bytecount,
599 request->flags & ATA_R_READ,
600 ctp->prd_tab, &entries)) {
601 device_printf(request->dev, "setting up DMA failed\n");
602 request->result = EIO;
603 return ATA_OP_FINISHED;
607 /* setup the command list entry */
608 clp = (struct ata_ahci_cmd_list *)
609 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
611 clp->prd_length = entries;
612 clp->cmd_flags = (request->flags & ATA_R_WRITE ? (1<<6) : 0) |
613 (request->flags & ATA_R_ATAPI ? ((1<<5) | (1<<7)) : 0) |
614 (fis_size / sizeof(u_int32_t));
616 clp->cmd_table_phys = htole64(ch->dma->work_bus + ATA_AHCI_CT_OFFSET +
617 (ATA_AHCI_CT_SIZE * tag));
619 /* clear eventual ACTIVE bit */
620 ATA_IDX_OUTL(ch, ATA_SACTIVE, ATA_IDX_INL(ch, ATA_SACTIVE) & (1 << tag));
622 /* set command type bit */
623 if (ch->devices & ATA_ATAPI_MASTER)
624 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
625 ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) |
626 ATA_AHCI_P_CMD_ATAPI);
628 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
629 ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) &
630 ~ATA_AHCI_P_CMD_ATAPI);
632 /* issue command to controller */
633 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CI + offset, (1 << tag));
635 if (!(request->flags & ATA_R_ATAPI)) {
636 /* device reset doesn't interrupt */
637 if (request->u.ata.command == ATA_DEVICE_RESET) {
639 int timeout = 1000000;
643 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + (ch->unit<<7));
644 } while ((tf_data & ATA_S_BUSY) && timeout--);
646 device_printf(ch->dev, "device_reset timeout=%dus\n",
647 (1000000-timeout)*10);
648 request->status = tf_data;
649 if (request->status & ATA_S_ERROR)
650 request->error = tf_data >> 8;
651 return ATA_OP_FINISHED;
655 /* start the timeout */
656 callout_reset(&request->callout, request->timeout * hz,
657 (timeout_t*)ata_timeout, request);
658 return ATA_OP_CONTINUES;
661 /* must be called with ATA channel locked and state_mtx held */
663 ata_ahci_end_transaction(struct ata_request *request)
665 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
666 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
667 struct ata_ahci_cmd_list *clp;
669 int offset = ch->unit << 7;
672 /* kill the timeout */
673 callout_stop(&request->callout);
676 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + offset);
677 request->status = tf_data;
679 /* if error status get details */
680 if (request->status & ATA_S_ERROR)
681 request->error = tf_data >> 8;
683 /* record how much data we actually moved */
684 clp = (struct ata_ahci_cmd_list *)
685 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
686 request->donecount = clp->bytecount;
688 /* release SG list etc */
689 ch->dma->unload(ch->dev);
691 return ATA_OP_FINISHED;
695 ata_ahci_reset(device_t dev)
697 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
698 struct ata_channel *ch = device_get_softc(dev);
699 u_int32_t cmd, signature;
700 int offset = ch->unit << 7;
703 if (!(ATA_INL(ctlr->r_res2, ATA_AHCI_PI) & (1 << ch->unit))) {
704 device_printf(dev, "port not implemented\n");
709 /* kill off all activity on this channel */
710 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
711 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
712 cmd & ~(ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
714 /* XXX SOS this is not entirely wrong */
718 if (timeout++ > 500) {
719 device_printf(dev, "stopping AHCI engine failed\n");
723 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) & ATA_AHCI_P_CMD_CR);
725 /* issue Command List Override if supported */
726 if (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_CAP_CLO) {
727 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
728 cmd |= ATA_AHCI_P_CMD_CLO;
729 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, cmd);
733 if (timeout++ > 500) {
734 device_printf(dev, "executing CLO failed\n");
738 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD+offset)&ATA_AHCI_P_CMD_CLO);
741 /* reset PHY and decide what is present */
742 if (ata_sata_phy_reset(dev)) {
744 /* clear any interrupts pending on this channel */
745 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset,
746 ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset));
748 /* clear SATA error register */
749 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
751 /* start operations on this channel */
752 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
753 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
754 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
756 signature = ATA_INL(ctlr->r_res2, ATA_AHCI_P_SIG + offset);
758 device_printf(dev, "SIGNATURE: %08x\n", signature);
761 ch->devices = ATA_ATA_MASTER;
764 ch->devices = ATA_PORTMULTIPLIER;
765 device_printf(ch->dev, "Portmultipliers not supported yet\n");
769 ch->devices = ATA_ATAPI_MASTER;
771 default: /* SOS XXX */
773 device_printf(ch->dev, "No signature, asuming disk device\n");
774 ch->devices = ATA_ATA_MASTER;
778 device_printf(dev, "ahci_reset devices=0x%b\n", ch->devices,
779 "\20\4ATAPI_SLAVE\3ATAPI_MASTER\2ATA_SLAVE\1ATA_MASTER");
783 ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
785 struct ata_dmasetprd_args *args = xsc;
786 struct ata_ahci_dma_prd *prd = args->dmatab;
789 if (!(args->error = error)) {
790 for (i = 0; i < nsegs; i++) {
791 prd[i].dba = htole64(segs[i].ds_addr);
792 prd[i].dbc = htole32((segs[i].ds_len - 1) & ATA_AHCI_PRD_MASK);
799 ata_ahci_dmainit(device_t dev)
801 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
802 struct ata_channel *ch = device_get_softc(dev);
806 /* note start and stop are not used here */
807 ch->dma->setprd = ata_ahci_dmasetprd;
808 ch->dma->max_iosize = 8192 * DEV_BSIZE;
809 if (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_CAP_64BIT)
810 ch->dma->max_address = BUS_SPACE_MAXADDR;
815 ata_ahci_setup_fis(struct ata_ahci_cmd_tab *ctp, struct ata_request *request)
817 bzero(ctp->cfis, 64);
818 if (request->flags & ATA_R_ATAPI) {
819 bzero(ctp->acmd, 32);
820 bcopy(request->u.atapi.ccb, ctp->acmd, 16);
822 return ata_request2fis_h2d(request, &ctp->cfis[0]);
827 * Acard chipset support functions
830 ata_acard_ident(device_t dev)
832 struct ata_pci_controller *ctlr = device_get_softc(dev);
833 struct ata_chip_id *idx;
834 static struct ata_chip_id ids[] =
835 {{ ATA_ATP850R, 0, ATPOLD, 0x00, ATA_UDMA2, "ATP850" },
836 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "ATP860A" },
837 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "ATP860R" },
838 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "ATP865A" },
839 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "ATP865R" },
840 { 0, 0, 0, 0, 0, 0}};
843 if (!(idx = ata_match_chip(dev, ids)))
846 sprintf(buffer, "Acard %s %s controller",
847 idx->text, ata_mode2str(idx->max_dma));
848 device_set_desc_copy(dev, buffer);
850 ctlr->chipinit = ata_acard_chipinit;
855 ata_acard_chipinit(device_t dev)
857 struct ata_pci_controller *ctlr = device_get_softc(dev);
859 if (ata_setup_interrupt(dev))
862 ctlr->allocate = ata_acard_allocate;
863 if (ctlr->chip->cfg1 == ATPOLD) {
864 ctlr->setmode = ata_acard_850_setmode;
865 ctlr->locking = ata_serialize;
868 ctlr->setmode = ata_acard_86X_setmode;
873 ata_acard_allocate(device_t dev)
875 struct ata_channel *ch = device_get_softc(dev);
877 /* setup the usual register normal pci style */
878 if (ata_pci_allocate(dev))
881 ch->hw.status = ata_acard_status;
886 ata_acard_status(device_t dev)
888 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
889 struct ata_channel *ch = device_get_softc(dev);
891 if (ctlr->chip->cfg1 == ATPOLD &&
892 ATA_LOCKING(ch->dev, ATA_LF_WHICH) != ch->unit)
894 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
895 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
897 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
898 ATA_BMSTAT_INTERRUPT)
900 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
902 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
903 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
906 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
908 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
915 ata_acard_850_setmode(device_t dev, int mode)
917 device_t gparent = GRANDPARENT(dev);
918 struct ata_pci_controller *ctlr = device_get_softc(gparent);
919 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
920 struct ata_device *atadev = device_get_softc(dev);
921 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
924 mode = ata_limit_mode(dev, mode,
925 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
927 /* XXX SOS missing WDMA0+1 + PIO modes */
928 if (mode >= ATA_WDMA2) {
929 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
931 device_printf(dev, "%ssetting %s on %s chip\n",
932 (error) ? "FAILURE " : "",
933 ata_mode2str(mode), ctlr->chip->text);
935 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
937 reg54 &= ~(0x03 << (devno << 1));
938 if (mode >= ATA_UDMA0)
939 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
940 pci_write_config(gparent, 0x54, reg54, 1);
941 pci_write_config(gparent, 0x4a, 0xa6, 1);
942 pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
947 /* we could set PIO mode timings, but we assume the BIOS did that */
951 ata_acard_86X_setmode(device_t dev, int mode)
953 device_t gparent = GRANDPARENT(dev);
954 struct ata_pci_controller *ctlr = device_get_softc(gparent);
955 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
956 struct ata_device *atadev = device_get_softc(dev);
957 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
961 mode = ata_limit_mode(dev, mode,
962 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
964 mode = ata_check_80pin(dev, mode);
966 /* XXX SOS missing WDMA0+1 + PIO modes */
967 if (mode >= ATA_WDMA2) {
968 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
970 device_printf(dev, "%ssetting %s on %s chip\n",
971 (error) ? "FAILURE " : "",
972 ata_mode2str(mode), ctlr->chip->text);
974 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
976 reg44 &= ~(0x000f << (devno << 2));
977 if (mode >= ATA_UDMA0)
978 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
979 pci_write_config(gparent, 0x44, reg44, 2);
980 pci_write_config(gparent, 0x4a, 0xa6, 1);
981 pci_write_config(gparent, 0x40 + devno, 0x31, 1);
986 /* we could set PIO mode timings, but we assume the BIOS did that */
991 * Acer Labs Inc (ALI) chipset support functions
994 ata_ali_ident(device_t dev)
996 struct ata_pci_controller *ctlr = device_get_softc(dev);
997 struct ata_chip_id *idx;
998 static struct ata_chip_id ids[] =
999 {{ ATA_ALI_5289, 0x00, 2, ALISATA, ATA_SA150, "M5289" },
1000 { ATA_ALI_5288, 0x00, 4, ALISATA, ATA_SA300, "M5288" },
1001 { ATA_ALI_5287, 0x00, 4, ALISATA, ATA_SA150, "M5287" },
1002 { ATA_ALI_5281, 0x00, 2, ALISATA, ATA_SA150, "M5281" },
1003 { ATA_ALI_5229, 0xc5, 0, ALINEW, ATA_UDMA6, "M5229" },
1004 { ATA_ALI_5229, 0xc4, 0, ALINEW, ATA_UDMA5, "M5229" },
1005 { ATA_ALI_5229, 0xc2, 0, ALINEW, ATA_UDMA4, "M5229" },
1006 { ATA_ALI_5229, 0x20, 0, ALIOLD, ATA_UDMA2, "M5229" },
1007 { ATA_ALI_5229, 0x00, 0, ALIOLD, ATA_WDMA2, "M5229" },
1008 { 0, 0, 0, 0, 0, 0}};
1011 if (!(idx = ata_match_chip(dev, ids)))
1014 sprintf(buffer, "AcerLabs %s %s controller",
1015 idx->text, ata_mode2str(idx->max_dma));
1016 device_set_desc_copy(dev, buffer);
1018 ctlr->chipinit = ata_ali_chipinit;
1023 ata_ali_chipinit(device_t dev)
1025 struct ata_pci_controller *ctlr = device_get_softc(dev);
1027 if (ata_setup_interrupt(dev))
1030 switch (ctlr->chip->cfg2) {
1032 ctlr->channels = ctlr->chip->cfg1;
1033 ctlr->allocate = ata_ali_sata_allocate;
1034 ctlr->setmode = ata_sata_setmode;
1036 /* if we have a memory resource we can likely do AHCI */
1037 ctlr->r_type2 = SYS_RES_MEMORY;
1038 ctlr->r_rid2 = PCIR_BAR(5);
1040 /* AHCI mode is correctly supported only on the ALi 5288. */
1041 if ((ctlr->chip->chipid == ATA_ALI_5288) &&
1042 (ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1043 &ctlr->r_rid2, RF_ACTIVE)))
1044 return ata_ahci_chipinit(dev);
1046 /* enable PCI interrupt */
1047 pci_write_config(dev, PCIR_COMMAND,
1048 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1052 /* use device interrupt as byte count end */
1053 pci_write_config(dev, 0x4a, pci_read_config(dev, 0x4a, 1) | 0x20, 1);
1055 /* enable cable detection and UDMA support on newer chips */
1056 pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
1058 /* enable ATAPI UDMA mode */
1059 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x01, 1);
1061 /* only chips with revision > 0xc4 can do 48bit DMA */
1062 if (ctlr->chip->chiprev <= 0xc4)
1064 "using PIO transfers above 137GB as workaround for "
1065 "48bit DMA access bug, expect reduced performance\n");
1066 ctlr->allocate = ata_ali_allocate;
1067 ctlr->reset = ata_ali_reset;
1068 ctlr->setmode = ata_ali_setmode;
1072 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
1073 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x03, 1);
1074 ctlr->setmode = ata_ali_setmode;
1081 ata_ali_allocate(device_t dev)
1083 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1084 struct ata_channel *ch = device_get_softc(dev);
1086 /* setup the usual register normal pci style */
1087 if (ata_pci_allocate(dev))
1090 /* older chips can't do 48bit DMA transfers */
1091 if (ctlr->chip->chiprev <= 0xc4)
1092 ch->flags |= ATA_NO_48BIT_DMA;
1098 ata_ali_sata_allocate(device_t dev)
1100 device_t parent = device_get_parent(dev);
1101 struct ata_pci_controller *ctlr = device_get_softc(parent);
1102 struct ata_channel *ch = device_get_softc(dev);
1103 struct resource *io = NULL, *ctlio = NULL;
1104 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
1107 rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
1108 io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
1112 rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
1113 ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
1115 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
1119 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
1120 ch->r_io[i].res = io;
1121 ch->r_io[i].offset = i + (unit10 ? 8 : 0);
1123 ch->r_io[ATA_CONTROL].res = ctlio;
1124 ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
1125 ch->r_io[ATA_IDX_ADDR].res = io;
1126 ata_default_registers(dev);
1128 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
1129 ch->r_io[i].res = ctlr->r_res1;
1130 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
1133 ch->flags |= ATA_NO_SLAVE;
1135 /* XXX SOS PHY handling awkward in ALI chip not supported yet */
1141 ata_ali_reset(device_t dev)
1143 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1144 struct ata_channel *ch = device_get_softc(dev);
1148 ata_generic_reset(dev);
1151 * workaround for datacorruption bug found on at least SUN Blade-100
1152 * find the ISA function on the southbridge and disable then enable
1153 * the ATA channel tristate buffer
1155 if (ctlr->chip->chiprev == 0xc3 || ctlr->chip->chiprev == 0xc2) {
1156 if (!device_get_children(GRANDPARENT(dev), &children, &nchildren)) {
1157 for (i = 0; i < nchildren; i++) {
1158 if (pci_get_devid(children[i]) == ATA_ALI_1533) {
1159 pci_write_config(children[i], 0x58,
1160 pci_read_config(children[i], 0x58, 1) &
1161 ~(0x04 << ch->unit), 1);
1162 pci_write_config(children[i], 0x58,
1163 pci_read_config(children[i], 0x58, 1) |
1164 (0x04 << ch->unit), 1);
1168 free(children, M_TEMP);
1174 ata_ali_setmode(device_t dev, int mode)
1176 device_t gparent = GRANDPARENT(dev);
1177 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1178 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1179 struct ata_device *atadev = device_get_softc(dev);
1180 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1183 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1185 if (ctlr->chip->cfg2 & ALINEW) {
1186 if (mode > ATA_UDMA2 &&
1187 pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
1188 ata_print_cable(dev, "controller");
1193 mode = ata_check_80pin(dev, mode);
1195 if (ctlr->chip->cfg2 & ALIOLD) {
1196 /* doesn't support ATAPI DMA on write */
1197 ch->flags |= ATA_ATAPI_DMA_RO;
1198 if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
1199 /* doesn't support ATAPI DMA on two ATAPI devices */
1200 device_printf(dev, "two atapi devices on this channel, no DMA\n");
1201 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1205 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1208 device_printf(dev, "%ssetting %s on %s chip\n",
1209 (error) ? "FAILURE " : "",
1210 ata_mode2str(mode), ctlr->chip->text);
1212 if (mode >= ATA_UDMA0) {
1213 u_int8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
1214 u_int32_t word54 = pci_read_config(gparent, 0x54, 4);
1216 word54 &= ~(0x000f000f << (devno << 2));
1217 word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
1218 pci_write_config(gparent, 0x54, word54, 4);
1219 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1223 u_int32_t piotimings[] =
1224 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
1225 0x00310001, 0x00440001, 0x00330001, 0x00310001};
1227 pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
1228 ~(0x0008000f << (devno << 2)), 4);
1229 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1230 piotimings[ata_mode2idx(mode)], 4);
1232 atadev->mode = mode;
1238 * American Micro Devices (AMD) chipset support functions
1241 ata_amd_ident(device_t dev)
1243 struct ata_pci_controller *ctlr = device_get_softc(dev);
1244 struct ata_chip_id *idx;
1245 static struct ata_chip_id ids[] =
1246 {{ ATA_AMD756, 0x00, AMDNVIDIA, 0x00, ATA_UDMA4, "756" },
1247 { ATA_AMD766, 0x00, AMDNVIDIA, AMDCABLE|AMDBUG, ATA_UDMA5, "766" },
1248 { ATA_AMD768, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA5, "768" },
1249 { ATA_AMD8111, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA6, "8111" },
1250 { ATA_AMD5536, 0x00, AMDNVIDIA, 0x00, ATA_UDMA5, "CS5536" },
1251 { 0, 0, 0, 0, 0, 0}};
1254 if (!(idx = ata_match_chip(dev, ids)))
1257 sprintf(buffer, "AMD %s %s controller",
1258 idx->text, ata_mode2str(idx->max_dma));
1259 device_set_desc_copy(dev, buffer);
1261 ctlr->chipinit = ata_amd_chipinit;
1266 ata_amd_chipinit(device_t dev)
1268 struct ata_pci_controller *ctlr = device_get_softc(dev);
1270 if (ata_setup_interrupt(dev))
1273 /* disable/set prefetch, postwrite */
1274 if (ctlr->chip->cfg2 & AMDBUG)
1275 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
1277 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
1279 ctlr->setmode = ata_via_family_setmode;
1285 * ATI chipset support functions
1288 ata_ati_ident(device_t dev)
1290 struct ata_pci_controller *ctlr = device_get_softc(dev);
1291 struct ata_chip_id *idx;
1292 static struct ata_chip_id ids[] =
1293 {{ ATA_ATI_IXP200, 0x00, 0, 0, ATA_UDMA5, "IXP200" },
1294 { ATA_ATI_IXP300, 0x00, 0, 0, ATA_UDMA6, "IXP300" },
1295 { ATA_ATI_IXP400, 0x00, 0, 0, ATA_UDMA6, "IXP400" },
1296 { ATA_ATI_IXP300_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP300" },
1297 { ATA_ATI_IXP400_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP400" },
1298 { ATA_ATI_IXP400_S2, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP400" },
1299 { 0, 0, 0, 0, 0, 0}};
1302 if (!(idx = ata_match_chip(dev, ids)))
1305 sprintf(buffer, "ATI %s %s controller",
1306 idx->text, ata_mode2str(idx->max_dma));
1307 device_set_desc_copy(dev, buffer);
1310 /* the ATI SATA controller is actually a SiI 3112 controller*/
1311 if (ctlr->chip->cfg1 & SIIMEMIO)
1312 ctlr->chipinit = ata_sii_chipinit;
1314 ctlr->chipinit = ata_ati_chipinit;
1319 ata_ati_chipinit(device_t dev)
1321 struct ata_pci_controller *ctlr = device_get_softc(dev);
1323 if (ata_setup_interrupt(dev))
1326 ctlr->setmode = ata_ati_setmode;
1331 ata_ati_setmode(device_t dev, int mode)
1333 device_t gparent = GRANDPARENT(dev);
1334 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1335 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1336 struct ata_device *atadev = device_get_softc(dev);
1337 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1338 int offset = (devno ^ 0x01) << 3;
1340 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
1341 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
1342 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
1344 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1346 mode = ata_check_80pin(dev, mode);
1348 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1351 device_printf(dev, "%ssetting %s on %s chip\n",
1352 (error) ? "FAILURE " : "",
1353 ata_mode2str(mode), ctlr->chip->text);
1355 if (mode >= ATA_UDMA0) {
1356 pci_write_config(gparent, 0x56,
1357 (pci_read_config(gparent, 0x56, 2) &
1358 ~(0xf << (devno << 2))) |
1359 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
1360 pci_write_config(gparent, 0x54,
1361 pci_read_config(gparent, 0x54, 1) |
1362 (0x01 << devno), 1);
1363 pci_write_config(gparent, 0x44,
1364 (pci_read_config(gparent, 0x44, 4) &
1365 ~(0xff << offset)) |
1366 (dmatimings[2] << offset), 4);
1368 else if (mode >= ATA_WDMA0) {
1369 pci_write_config(gparent, 0x54,
1370 pci_read_config(gparent, 0x54, 1) &
1371 ~(0x01 << devno), 1);
1372 pci_write_config(gparent, 0x44,
1373 (pci_read_config(gparent, 0x44, 4) &
1374 ~(0xff << offset)) |
1375 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
1378 pci_write_config(gparent, 0x54,
1379 pci_read_config(gparent, 0x54, 1) &
1380 ~(0x01 << devno), 1);
1382 pci_write_config(gparent, 0x4a,
1383 (pci_read_config(gparent, 0x4a, 2) &
1384 ~(0xf << (devno << 2))) |
1385 (((mode - ATA_PIO0) & ATA_MODE_MASK) << (devno<<2)),2);
1386 pci_write_config(gparent, 0x40,
1387 (pci_read_config(gparent, 0x40, 4) &
1388 ~(0xff << offset)) |
1389 (piotimings[ata_mode2idx(mode)] << offset), 4);
1390 atadev->mode = mode;
1396 * Cyrix chipset support functions
1399 ata_cyrix_ident(device_t dev)
1401 struct ata_pci_controller *ctlr = device_get_softc(dev);
1403 if (pci_get_devid(dev) == ATA_CYRIX_5530) {
1404 device_set_desc(dev, "Cyrix 5530 ATA33 controller");
1405 ctlr->chipinit = ata_cyrix_chipinit;
1412 ata_cyrix_chipinit(device_t dev)
1414 struct ata_pci_controller *ctlr = device_get_softc(dev);
1416 if (ata_setup_interrupt(dev))
1420 ctlr->setmode = ata_cyrix_setmode;
1422 ctlr->setmode = ata_generic_setmode;
1427 ata_cyrix_setmode(device_t dev, int mode)
1429 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1430 struct ata_device *atadev = device_get_softc(dev);
1431 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1432 u_int32_t piotiming[] =
1433 { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
1434 u_int32_t dmatiming[] = { 0x00077771, 0x00012121, 0x00002020 };
1435 u_int32_t udmatiming[] = { 0x00921250, 0x00911140, 0x00911030 };
1438 ch->dma->alignment = 16;
1439 ch->dma->max_iosize = 126 * DEV_BSIZE;
1441 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1443 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1446 device_printf(dev, "%ssetting %s on Cyrix chip\n",
1447 (error) ? "FAILURE " : "", ata_mode2str(mode));
1449 if (mode >= ATA_UDMA0) {
1450 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1451 0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
1453 else if (mode >= ATA_WDMA0) {
1454 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1455 0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
1458 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1459 0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
1461 atadev->mode = mode;
1467 * Cypress chipset support functions
1470 ata_cypress_ident(device_t dev)
1472 struct ata_pci_controller *ctlr = device_get_softc(dev);
1475 * the Cypress chip is a mess, it contains two ATA functions, but
1476 * both channels are visible on the first one.
1477 * simply ignore the second function for now, as the right
1478 * solution (ignoring the second channel on the first function)
1479 * doesn't work with the crappy ATA interrupt setup on the alpha.
1481 if (pci_get_devid(dev) == ATA_CYPRESS_82C693 &&
1482 pci_get_function(dev) == 1 &&
1483 pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
1484 device_set_desc(dev, "Cypress 82C693 ATA controller");
1485 ctlr->chipinit = ata_cypress_chipinit;
1492 ata_cypress_chipinit(device_t dev)
1494 struct ata_pci_controller *ctlr = device_get_softc(dev);
1496 if (ata_setup_interrupt(dev))
1499 ctlr->setmode = ata_cypress_setmode;
1504 ata_cypress_setmode(device_t dev, int mode)
1506 device_t gparent = GRANDPARENT(dev);
1507 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1508 struct ata_device *atadev = device_get_softc(dev);
1511 mode = ata_limit_mode(dev, mode, ATA_WDMA2);
1513 /* XXX SOS missing WDMA0+1 + PIO modes */
1514 if (mode == ATA_WDMA2) {
1515 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1517 device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
1518 error ? "FAILURE " : "");
1520 pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
1521 atadev->mode = mode;
1525 /* we could set PIO mode timings, but we assume the BIOS did that */
1530 * HighPoint chipset support functions
1533 ata_highpoint_ident(device_t dev)
1535 struct ata_pci_controller *ctlr = device_get_softc(dev);
1536 struct ata_chip_id *idx;
1537 static struct ata_chip_id ids[] =
1538 {{ ATA_HPT374, 0x07, HPT374, 0x00, ATA_UDMA6, "HPT374" },
1539 { ATA_HPT372, 0x02, HPT372, 0x00, ATA_UDMA6, "HPT372N" },
1540 { ATA_HPT372, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT372" },
1541 { ATA_HPT371, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT371" },
1542 { ATA_HPT366, 0x05, HPT372, 0x00, ATA_UDMA6, "HPT372" },
1543 { ATA_HPT366, 0x03, HPT370, 0x00, ATA_UDMA5, "HPT370" },
1544 { ATA_HPT366, 0x02, HPT366, 0x00, ATA_UDMA4, "HPT368" },
1545 { ATA_HPT366, 0x00, HPT366, HPTOLD, ATA_UDMA4, "HPT366" },
1546 { ATA_HPT302, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT302" },
1547 { 0, 0, 0, 0, 0, 0}};
1550 if (!(idx = ata_match_chip(dev, ids)))
1553 strcpy(buffer, "HighPoint ");
1554 strcat(buffer, idx->text);
1555 if (idx->cfg1 == HPT374) {
1556 if (pci_get_function(dev) == 0)
1557 strcat(buffer, " (channel 0+1)");
1558 if (pci_get_function(dev) == 1)
1559 strcat(buffer, " (channel 2+3)");
1561 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
1562 device_set_desc_copy(dev, buffer);
1564 ctlr->chipinit = ata_highpoint_chipinit;
1569 ata_highpoint_chipinit(device_t dev)
1571 struct ata_pci_controller *ctlr = device_get_softc(dev);
1573 if (ata_setup_interrupt(dev))
1576 if (ctlr->chip->cfg2 == HPTOLD) {
1577 /* disable interrupt prediction */
1578 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
1581 /* disable interrupt prediction */
1582 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
1583 pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
1585 /* enable interrupts */
1586 pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
1588 /* set clocks etc */
1589 if (ctlr->chip->cfg1 < HPT372)
1590 pci_write_config(dev, 0x5b, 0x22, 1);
1592 pci_write_config(dev, 0x5b,
1593 (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
1595 ctlr->allocate = ata_highpoint_allocate;
1596 ctlr->setmode = ata_highpoint_setmode;
1601 ata_highpoint_allocate(device_t dev)
1603 struct ata_channel *ch = device_get_softc(dev);
1605 /* setup the usual register normal pci style */
1606 if (ata_pci_allocate(dev))
1609 ch->flags |= ATA_ALWAYS_DMASTAT;
1614 ata_highpoint_setmode(device_t dev, int mode)
1616 device_t gparent = GRANDPARENT(dev);
1617 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1618 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1619 struct ata_device *atadev = device_get_softc(dev);
1620 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1622 u_int32_t timings33[][4] = {
1623 /* HPT366 HPT370 HPT372 HPT374 mode */
1624 { 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a }, /* PIO 0 */
1625 { 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 }, /* PIO 1 */
1626 { 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 }, /* PIO 2 */
1627 { 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 }, /* PIO 3 */
1628 { 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 }, /* PIO 4 */
1629 { 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea }, /* MWDMA 0 */
1630 { 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 }, /* MWDMA 1 */
1631 { 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 }, /* MWDMA 2 */
1632 { 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea }, /* UDMA 0 */
1633 { 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 }, /* UDMA 1 */
1634 { 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 }, /* UDMA 2 */
1635 { 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 }, /* UDMA 3 */
1636 { 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 }, /* UDMA 4 */
1637 { 0, 0x16454e31, 0x1c8a9c62, 0x12848242 }, /* UDMA 5 */
1638 { 0, 0, 0x1c869c62, 0x12808242 } /* UDMA 6 */
1641 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1643 if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
1644 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1646 mode = ata_highpoint_check_80pin(dev, mode);
1649 * most if not all HPT chips cant really handle that the device is
1650 * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
1651 * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
1653 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
1654 ata_limit_mode(dev, mode, ATA_UDMA5));
1656 device_printf(dev, "%ssetting %s on HighPoint chip\n",
1657 (error) ? "FAILURE " : "", ata_mode2str(mode));
1659 pci_write_config(gparent, 0x40 + (devno << 2),
1660 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
1661 atadev->mode = mode;
1665 ata_highpoint_check_80pin(device_t dev, int mode)
1667 device_t gparent = GRANDPARENT(dev);
1668 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1669 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1670 u_int8_t reg, val, res;
1672 if (ctlr->chip->cfg1 == HPT374 && pci_get_function(gparent) == 1) {
1673 reg = ch->unit ? 0x57 : 0x53;
1674 val = pci_read_config(gparent, reg, 1);
1675 pci_write_config(gparent, reg, val | 0x80, 1);
1679 val = pci_read_config(gparent, reg, 1);
1680 pci_write_config(gparent, reg, val & 0xfe, 1);
1682 res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1683 pci_write_config(gparent, reg, val, 1);
1685 if (mode > ATA_UDMA2 && res) {
1686 ata_print_cable(dev, "controller");
1694 * Intel chipset support functions
1697 ata_intel_ident(device_t dev)
1699 struct ata_pci_controller *ctlr = device_get_softc(dev);
1700 struct ata_chip_id *idx;
1701 static struct ata_chip_id ids[] =
1702 {{ ATA_I82371FB, 0, 0, 0x00, ATA_WDMA2, "PIIX" },
1703 { ATA_I82371SB, 0, 0, 0x00, ATA_WDMA2, "PIIX3" },
1704 { ATA_I82371AB, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1705 { ATA_I82443MX, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1706 { ATA_I82451NX, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1707 { ATA_I82801AB, 0, 0, 0x00, ATA_UDMA2, "ICH0" },
1708 { ATA_I82801AA, 0, 0, 0x00, ATA_UDMA4, "ICH" },
1709 { ATA_I82372FB, 0, 0, 0x00, ATA_UDMA4, "ICH" },
1710 { ATA_I82801BA, 0, 0, 0x00, ATA_UDMA5, "ICH2" },
1711 { ATA_I82801BA_1, 0, 0, 0x00, ATA_UDMA5, "ICH2" },
1712 { ATA_I82801CA, 0, 0, 0x00, ATA_UDMA5, "ICH3" },
1713 { ATA_I82801CA_1, 0, 0, 0x00, ATA_UDMA5, "ICH3" },
1714 { ATA_I82801DB, 0, 0, 0x00, ATA_UDMA5, "ICH4" },
1715 { ATA_I82801DB_1, 0, 0, 0x00, ATA_UDMA5, "ICH4" },
1716 { ATA_I82801EB, 0, 0, 0x00, ATA_UDMA5, "ICH5" },
1717 { ATA_I82801EB_S1, 0, 0, 0x00, ATA_SA150, "ICH5" },
1718 { ATA_I82801EB_R1, 0, 0, 0x00, ATA_SA150, "ICH5" },
1719 { ATA_I6300ESB, 0, 0, 0x00, ATA_UDMA5, "6300ESB" },
1720 { ATA_I6300ESB_S1, 0, 0, 0x00, ATA_SA150, "6300ESB" },
1721 { ATA_I6300ESB_R1, 0, 0, 0x00, ATA_SA150, "6300ESB" },
1722 { ATA_I82801FB, 0, 0, 0x00, ATA_UDMA5, "ICH6" },
1723 { ATA_I82801FB_S1, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1724 { ATA_I82801FB_R1, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1725 { ATA_I82801FBM, 0, AHCI, 0x00, ATA_SA150, "ICH6M" },
1726 { ATA_I82801GB, 0, 0, 0x00, ATA_UDMA5, "ICH7" },
1727 { ATA_I82801GB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1728 { ATA_I82801GB_R1, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1729 { ATA_I82801GB_AH, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1730 { ATA_I82801GBM_S1, 0, AHCI, 0x00, ATA_SA300, "ICH7M" },
1731 { ATA_I82801GBM_R1, 0, AHCI, 0x00, ATA_SA300, "ICH7M" },
1732 { ATA_I82801GBM_AH, 0, AHCI, 0x00, ATA_SA300, "ICH7M" },
1733 { ATA_I63XXESB2, 0, 0, 0x00, ATA_UDMA5, "63XXESB2" },
1734 { ATA_I63XXESB2_S1, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1735 { ATA_I63XXESB2_S2, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1736 { ATA_I63XXESB2_R1, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1737 { ATA_I63XXESB2_R2, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1738 { ATA_I82801HB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1739 { ATA_I82801HB_S2, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1740 { ATA_I82801HB_R1, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1741 { ATA_I82801HB_AH4, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1742 { ATA_I82801HB_AH6, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1743 { ATA_I82801HBM_S1, 0, AHCI, 0x00, ATA_SA300, "ICH8M" },
1744 { ATA_I82801HBM_S2, 0, AHCI, 0x00, ATA_SA300, "ICH8M" },
1745 { ATA_I82801IB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1746 { ATA_I82801IB_S2, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1747 { ATA_I82801IB_AH2, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1748 { ATA_I82801IB_AH4, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1749 { ATA_I82801IB_AH6, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1750 { ATA_I31244, 0, 0, 0x00, ATA_SA150, "31244" },
1751 { 0, 0, 0, 0, 0, 0}};
1754 if (!(idx = ata_match_chip(dev, ids)))
1757 sprintf(buffer, "Intel %s %s controller",
1758 idx->text, ata_mode2str(idx->max_dma));
1759 device_set_desc_copy(dev, buffer);
1761 ctlr->chipinit = ata_intel_chipinit;
1766 ata_intel_chipinit(device_t dev)
1768 struct ata_pci_controller *ctlr = device_get_softc(dev);
1770 if (ata_setup_interrupt(dev))
1773 /* good old PIIX needs special treatment (not implemented) */
1774 if (ctlr->chip->chipid == ATA_I82371FB) {
1775 ctlr->setmode = ata_intel_old_setmode;
1778 /* the intel 31244 needs special care if in DPA mode */
1779 else if (ctlr->chip->chipid == ATA_I31244) {
1780 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE) {
1781 ctlr->r_type2 = SYS_RES_MEMORY;
1782 ctlr->r_rid2 = PCIR_BAR(0);
1783 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1788 ctlr->allocate = ata_intel_31244_allocate;
1789 ctlr->reset = ata_intel_31244_reset;
1791 ctlr->setmode = ata_sata_setmode;
1794 /* non SATA intel chips goes here */
1795 else if (ctlr->chip->max_dma < ATA_SA150) {
1796 ctlr->allocate = ata_intel_allocate;
1797 ctlr->setmode = ata_intel_new_setmode;
1800 /* SATA parts can be either compat or AHCI */
1802 /* force all ports active "the legacy way" */
1803 pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f, 2);
1805 ctlr->allocate = ata_intel_allocate;
1806 ctlr->reset = ata_intel_reset;
1809 * if we have AHCI capability and BAR(5) as a memory resource
1810 * and AHCI or RAID mode enabled in BIOS we go for AHCI mode
1812 if ((ctlr->chip->cfg1 == AHCI) &&
1813 (pci_read_config(dev, 0x90, 1) & 0xc0)) {
1814 ctlr->r_type2 = SYS_RES_MEMORY;
1815 ctlr->r_rid2 = PCIR_BAR(5);
1816 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1819 return ata_ahci_chipinit(dev);
1821 ctlr->setmode = ata_sata_setmode;
1823 /* enable PCI interrupt */
1824 pci_write_config(dev, PCIR_COMMAND,
1825 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1831 ata_intel_allocate(device_t dev)
1833 struct ata_channel *ch = device_get_softc(dev);
1835 /* setup the usual register normal pci style */
1836 if (ata_pci_allocate(dev))
1839 ch->flags |= ATA_ALWAYS_DMASTAT;
1844 ata_intel_reset(device_t dev)
1846 device_t parent = device_get_parent(dev);
1847 struct ata_pci_controller *ctlr = device_get_softc(parent);
1848 struct ata_channel *ch = device_get_softc(dev);
1851 /* ICH6 & ICH7 in compat mode has 4 SATA ports as master/slave on 2 ch's */
1852 if (ctlr->chip->cfg1) {
1853 mask = (0x0005 << ch->unit);
1856 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1857 if (pci_read_config(parent, 0x90, 1) & 0x04)
1860 mask = (0x0001 << ch->unit);
1861 /* XXX SOS should be in intel_allocate if we grow it */
1862 ch->flags |= ATA_NO_SLAVE;
1865 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1867 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1869 /* wait up to 1 sec for "connect well" */
1870 for (timeout = 0; timeout < 100 ; timeout++) {
1871 if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1872 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1876 ata_generic_reset(dev);
1880 ata_intel_old_setmode(device_t dev, int mode)
1886 ata_intel_new_setmode(device_t dev, int mode)
1888 device_t gparent = GRANDPARENT(dev);
1889 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1890 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1891 struct ata_device *atadev = device_get_softc(dev);
1892 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1893 u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
1894 u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
1895 u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
1896 u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
1897 u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
1898 u_int32_t mask40 = 0, new40 = 0;
1899 u_int8_t mask44 = 0, new44 = 0;
1901 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1902 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1904 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1906 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1907 ata_print_cable(dev, "controller");
1911 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1914 device_printf(dev, "%ssetting %s on %s chip\n",
1915 (error) ? "FAILURE " : "",
1916 ata_mode2str(mode), ctlr->chip->text);
1920 if (mode >= ATA_UDMA0) {
1921 pci_write_config(gparent, 0x48, reg48 | (0x0001 << devno), 2);
1922 pci_write_config(gparent, 0x4a,
1923 (reg4a & ~(0x3 << (devno << 2))) |
1924 ((0x01 + !(mode & 0x01)) << (devno << 2)), 2);
1927 pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
1928 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))), 2);
1931 if (mode >= ATA_UDMA2)
1932 pci_write_config(gparent, 0x54, reg54 | (0x1 << devno), 2);
1934 pci_write_config(gparent, 0x54, reg54 & ~(0x1 << devno), 2);
1936 if (mode >= ATA_UDMA5)
1937 pci_write_config(gparent, 0x54, reg54 | (0x1000 << devno), 2);
1939 pci_write_config(gparent, 0x54, reg54 & ~(0x1000 << devno), 2);
1941 reg40 &= ~0x00ff00ff;
1942 reg40 |= 0x40774077;
1944 if (atadev->unit == ATA_MASTER) {
1946 new40 = timings[ata_mode2idx(mode)] << 8;
1950 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
1951 (timings[ata_mode2idx(mode)] & 0x03);
1959 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
1960 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
1962 atadev->mode = mode;
1966 ata_intel_31244_allocate(device_t dev)
1968 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1969 struct ata_channel *ch = device_get_softc(dev);
1973 ch_offset = 0x200 + ch->unit * 0x200;
1975 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
1976 ch->r_io[i].res = ctlr->r_res2;
1978 /* setup ATA registers */
1979 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
1980 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x06;
1981 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
1982 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
1983 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
1984 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
1985 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
1986 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1d;
1987 ch->r_io[ATA_ERROR].offset = ch_offset + 0x04;
1988 ch->r_io[ATA_STATUS].offset = ch_offset + 0x1c;
1989 ch->r_io[ATA_ALTSTAT].offset = ch_offset + 0x28;
1990 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x29;
1992 /* setup DMA registers */
1993 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x100;
1994 ch->r_io[ATA_SERROR].offset = ch_offset + 0x104;
1995 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x108;
1997 /* setup SATA registers */
1998 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x70;
1999 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x72;
2000 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x74;
2002 ch->flags |= ATA_NO_SLAVE;
2004 ch->hw.status = ata_intel_31244_status;
2005 ch->hw.command = ata_intel_31244_command;
2007 /* enable PHY state change interrupt */
2008 ATA_OUTL(ctlr->r_res2, 0x4,
2009 ATA_INL(ctlr->r_res2, 0x04) | (0x01 << (ch->unit << 3)));
2014 ata_intel_31244_status(device_t dev)
2016 /* do we have any PHY events ? */
2017 ata_sata_phy_check_events(dev);
2019 /* any drive action to take care of ? */
2020 return ata_pci_status(dev);
2024 ata_intel_31244_command(struct ata_request *request)
2026 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2027 struct ata_device *atadev = device_get_softc(request->dev);
2030 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
2031 return (ata_generic_command(request));
2033 lba = request->u.ata.lba;
2034 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | atadev->unit);
2035 /* enable interrupt */
2036 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
2037 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
2038 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
2039 ATA_IDX_OUTW(ch, ATA_SECTOR, ((lba >> 16) & 0xff00) | (lba & 0x00ff));
2040 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((lba >> 24) & 0xff00) |
2041 ((lba >> 8) & 0x00ff));
2042 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((lba >> 32) & 0xff00) |
2043 ((lba >> 16) & 0x00ff));
2045 /* issue command to controller */
2046 ATA_IDX_OUTB(ch, ATA_COMMAND, request->u.ata.command);
2052 ata_intel_31244_reset(device_t dev)
2054 if (ata_sata_phy_reset(dev))
2055 ata_generic_reset(dev);
2060 * Integrated Technology Express Inc. (ITE) chipset support functions
2063 ata_ite_ident(device_t dev)
2065 struct ata_pci_controller *ctlr = device_get_softc(dev);
2066 struct ata_chip_id *idx;
2067 static struct ata_chip_id ids[] =
2068 {{ ATA_IT8212F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8212F" },
2069 { ATA_IT8211F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8211F" },
2070 { 0, 0, 0, 0, 0, 0}};
2073 if (!(idx = ata_match_chip(dev, ids)))
2076 sprintf(buffer, "ITE %s %s controller",
2077 idx->text, ata_mode2str(idx->max_dma));
2078 device_set_desc_copy(dev, buffer);
2080 ctlr->chipinit = ata_ite_chipinit;
2085 ata_ite_chipinit(device_t dev)
2087 struct ata_pci_controller *ctlr = device_get_softc(dev);
2089 if (ata_setup_interrupt(dev))
2092 ctlr->setmode = ata_ite_setmode;
2094 /* set PCI mode and 66Mhz reference clock */
2095 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
2097 /* set default active & recover timings */
2098 pci_write_config(dev, 0x54, 0x31, 1);
2099 pci_write_config(dev, 0x56, 0x31, 1);
2104 ata_ite_setmode(device_t dev, int mode)
2106 device_t gparent = GRANDPARENT(dev);
2107 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2108 struct ata_device *atadev = device_get_softc(dev);
2109 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2112 /* correct the mode for what the HW supports */
2113 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2115 /* check the CBLID bits for 80 conductor cable detection */
2116 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
2117 (ch->unit ? (1<<3) : (1<<2)))) {
2118 ata_print_cable(dev, "controller");
2122 /* set the wanted mode on the device */
2123 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2126 device_printf(dev, "%s setting %s on ITE8212F chip\n",
2127 (error) ? "failed" : "success", ata_mode2str(mode));
2129 /* if the device accepted the mode change, setup the HW accordingly */
2131 if (mode >= ATA_UDMA0) {
2132 u_int8_t udmatiming[] =
2133 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
2135 /* enable UDMA mode */
2136 pci_write_config(gparent, 0x50,
2137 pci_read_config(gparent, 0x50, 1) &
2138 ~(1 << (devno + 3)), 1);
2140 /* set UDMA timing */
2141 pci_write_config(gparent,
2142 0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
2143 udmatiming[mode & ATA_MODE_MASK], 1);
2146 u_int8_t chtiming[] =
2147 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
2149 /* disable UDMA mode */
2150 pci_write_config(gparent, 0x50,
2151 pci_read_config(gparent, 0x50, 1) |
2152 (1 << (devno + 3)), 1);
2154 /* set active and recover timing (shared between master & slave) */
2155 if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
2156 chtiming[ata_mode2idx(mode)])
2157 pci_write_config(gparent, 0x54 + (ch->unit << 2),
2158 chtiming[ata_mode2idx(mode)], 1);
2160 atadev->mode = mode;
2166 * JMicron chipset support functions
2169 ata_jmicron_ident(device_t dev)
2171 struct ata_pci_controller *ctlr = device_get_softc(dev);
2172 struct ata_chip_id *idx;
2173 static struct ata_chip_id ids[] =
2174 {{ ATA_JMB360, 0, 1, 0, ATA_SA300, "JMB360" },
2175 { ATA_JMB361, 0, 1, 1, ATA_SA300, "JMB361" },
2176 { ATA_JMB363, 0, 2, 1, ATA_SA300, "JMB363" },
2177 { ATA_JMB365, 0, 1, 2, ATA_SA300, "JMB365" },
2178 { ATA_JMB366, 0, 2, 2, ATA_SA300, "JMB366" },
2179 { ATA_JMB368, 0, 0, 1, ATA_UDMA6, "JMB368" },
2180 { 0, 0, 0, 0, 0, 0}};
2183 if (!(idx = ata_match_chip(dev, ids)))
2186 if ((pci_read_config(dev, 0xdf, 1) & 0x40) &&
2187 (pci_get_function(dev) == (pci_read_config(dev, 0x40, 1) & 0x02 >> 1)))
2188 sprintf(buffer, "JMicron %s %s controller",
2189 idx->text, ata_mode2str(ATA_UDMA6));
2191 sprintf(buffer, "JMicron %s %s controller",
2192 idx->text, ata_mode2str(idx->max_dma));
2193 device_set_desc_copy(dev, buffer);
2195 ctlr->chipinit = ata_jmicron_chipinit;
2200 ata_jmicron_chipinit(device_t dev)
2202 struct ata_pci_controller *ctlr = device_get_softc(dev);
2205 if (ata_setup_interrupt(dev))
2208 /* do we have multiple PCI functions ? */
2209 if (pci_read_config(dev, 0xdf, 1) & 0x40) {
2210 /* if we have a memory BAR(5) we are on the AHCI part */
2211 ctlr->r_type2 = SYS_RES_MEMORY;
2212 ctlr->r_rid2 = PCIR_BAR(5);
2213 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2214 &ctlr->r_rid2, RF_ACTIVE)))
2215 return ata_ahci_chipinit(dev);
2217 /* otherwise we are on the PATA part */
2218 ctlr->allocate = ata_pci_allocate;
2219 ctlr->reset = ata_generic_reset;
2220 ctlr->dmainit = ata_pci_dmainit;
2221 ctlr->setmode = ata_jmicron_setmode;
2222 ctlr->channels = ctlr->chip->cfg2;
2225 /* set controller configuration to a combined setup we support */
2226 pci_write_config(dev, 0x40, 0x80c0a131, 4);
2227 pci_write_config(dev, 0x80, 0x01200000, 4);
2229 ctlr->r_type2 = SYS_RES_MEMORY;
2230 ctlr->r_rid2 = PCIR_BAR(5);
2231 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2232 &ctlr->r_rid2, RF_ACTIVE))){
2233 if ((error = ata_ahci_chipinit(dev)))
2237 ctlr->allocate = ata_jmicron_allocate;
2238 ctlr->reset = ata_jmicron_reset;
2239 ctlr->dmainit = ata_jmicron_dmainit;
2240 ctlr->setmode = ata_jmicron_setmode;
2242 /* set the number of HW channels */
2243 ctlr->channels = ctlr->chip->cfg1 + ctlr->chip->cfg2;
2249 ata_jmicron_allocate(device_t dev)
2251 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2252 struct ata_channel *ch = device_get_softc(dev);
2255 if (ch->unit >= ctlr->chip->cfg1) {
2256 ch->unit -= ctlr->chip->cfg1;
2257 error = ata_pci_allocate(dev);
2258 ch->unit += ctlr->chip->cfg1;
2261 error = ata_ahci_allocate(dev);
2266 ata_jmicron_reset(device_t dev)
2268 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2269 struct ata_channel *ch = device_get_softc(dev);
2271 if (ch->unit >= ctlr->chip->cfg1)
2272 ata_generic_reset(dev);
2274 ata_ahci_reset(dev);
2278 ata_jmicron_dmainit(device_t dev)
2280 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2281 struct ata_channel *ch = device_get_softc(dev);
2283 if (ch->unit >= ctlr->chip->cfg1)
2284 ata_pci_dmainit(dev);
2286 ata_ahci_dmainit(dev);
2290 ata_jmicron_setmode(device_t dev, int mode)
2292 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2293 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2295 if (pci_read_config(dev, 0xdf, 1) & 0x40 || ch->unit >= ctlr->chip->cfg1) {
2296 struct ata_device *atadev = device_get_softc(dev);
2298 /* check for 80pin cable present */
2299 if (pci_read_config(dev, 0x40, 1) & 0x08)
2300 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2302 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2304 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
2305 atadev->mode = mode;
2308 ata_sata_setmode(dev, mode);
2313 * Marvell chipset support functions
2315 #define ATA_MV_HOST_BASE(ch) \
2316 ((ch->unit & 3) * 0x0100) + (ch->unit > 3 ? 0x30000 : 0x20000)
2317 #define ATA_MV_EDMA_BASE(ch) \
2318 ((ch->unit & 3) * 0x2000) + (ch->unit > 3 ? 0x30000 : 0x20000)
2320 struct ata_marvell_response {
2322 u_int8_t edma_status;
2323 u_int8_t dev_status;
2324 u_int32_t timestamp;
2327 struct ata_marvell_dma_prdentry {
2335 ata_marvell_ident(device_t dev)
2337 struct ata_pci_controller *ctlr = device_get_softc(dev);
2338 struct ata_chip_id *idx;
2339 static struct ata_chip_id ids[] =
2340 {{ ATA_M88SX5040, 0, 4, MV50XX, ATA_SA150, "88SX5040" },
2341 { ATA_M88SX5041, 0, 4, MV50XX, ATA_SA150, "88SX5041" },
2342 { ATA_M88SX5080, 0, 8, MV50XX, ATA_SA150, "88SX5080" },
2343 { ATA_M88SX5081, 0, 8, MV50XX, ATA_SA150, "88SX5081" },
2344 { ATA_M88SX6041, 0, 4, MV60XX, ATA_SA300, "88SX6041" },
2345 { ATA_M88SX6081, 0, 8, MV60XX, ATA_SA300, "88SX6081" },
2346 { ATA_M88SX6101, 0, 1, MV61XX, ATA_UDMA6, "88SX6101" },
2347 { ATA_M88SX6145, 0, 2, MV61XX, ATA_UDMA6, "88SX6145" },
2348 { 0, 0, 0, 0, 0, 0}};
2351 if (!(idx = ata_match_chip(dev, ids)))
2354 sprintf(buffer, "Marvell %s %s controller",
2355 idx->text, ata_mode2str(idx->max_dma));
2356 device_set_desc_copy(dev, buffer);
2358 switch (ctlr->chip->cfg2) {
2361 ctlr->chipinit = ata_marvell_edma_chipinit;
2364 ctlr->chipinit = ata_marvell_pata_chipinit;
2371 ata_marvell_pata_chipinit(device_t dev)
2373 struct ata_pci_controller *ctlr = device_get_softc(dev);
2375 if (ata_setup_interrupt(dev))
2378 ctlr->allocate = ata_marvell_pata_allocate;
2379 ctlr->setmode = ata_marvell_pata_setmode;
2380 ctlr->channels = ctlr->chip->cfg1;
2385 ata_marvell_pata_allocate(device_t dev)
2387 struct ata_channel *ch = device_get_softc(dev);
2389 /* setup the usual register normal pci style */
2390 if (ata_pci_allocate(dev))
2393 /* dont use 32 bit PIO transfers */
2394 ch->flags |= ATA_USE_16BIT;
2400 ata_marvell_pata_setmode(device_t dev, int mode)
2402 device_t gparent = GRANDPARENT(dev);
2403 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2404 struct ata_device *atadev = device_get_softc(dev);
2406 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2407 mode = ata_check_80pin(dev, mode);
2408 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
2409 atadev->mode = mode;
2413 ata_marvell_edma_chipinit(device_t dev)
2415 struct ata_pci_controller *ctlr = device_get_softc(dev);
2417 if (ata_setup_interrupt(dev))
2420 ctlr->r_type1 = SYS_RES_MEMORY;
2421 ctlr->r_rid1 = PCIR_BAR(0);
2422 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
2423 &ctlr->r_rid1, RF_ACTIVE)))
2426 /* mask all host controller interrupts */
2427 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x00000000);
2429 /* mask all PCI interrupts */
2430 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x00000000);
2432 ctlr->allocate = ata_marvell_edma_allocate;
2433 ctlr->reset = ata_marvell_edma_reset;
2434 ctlr->dmainit = ata_marvell_edma_dmainit;
2435 ctlr->setmode = ata_sata_setmode;
2436 ctlr->channels = ctlr->chip->cfg1;
2438 /* clear host controller interrupts */
2439 ATA_OUTL(ctlr->r_res1, 0x20014, 0x00000000);
2440 if (ctlr->chip->cfg1 > 4)
2441 ATA_OUTL(ctlr->r_res1, 0x30014, 0x00000000);
2443 /* clear PCI interrupts */
2444 ATA_OUTL(ctlr->r_res1, 0x01d58, 0x00000000);
2446 /* unmask PCI interrupts we want */
2447 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x007fffff);
2449 /* unmask host controller interrupts we want */
2450 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x000000ff/*HC0*/ | 0x0001fe00/*HC1*/ |
2451 /*(1<<19) | (1<<20) | (1<<21) |*/(1<<22) | (1<<24) | (0x7f << 25));
2453 /* enable PCI interrupt */
2454 pci_write_config(dev, PCIR_COMMAND,
2455 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
2460 ata_marvell_edma_allocate(device_t dev)
2462 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2463 struct ata_channel *ch = device_get_softc(dev);
2464 u_int64_t work = ch->dma->work_bus;
2467 /* clear work area */
2468 bzero(ch->dma->work, 1024+256);
2470 /* set legacy ATA resources */
2471 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2472 ch->r_io[i].res = ctlr->r_res1;
2473 ch->r_io[i].offset = 0x02100 + (i << 2) + ATA_MV_EDMA_BASE(ch);
2475 ch->r_io[ATA_CONTROL].res = ctlr->r_res1;
2476 ch->r_io[ATA_CONTROL].offset = 0x02120 + ATA_MV_EDMA_BASE(ch);
2477 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res1;
2478 ata_default_registers(dev);
2480 /* set SATA resources */
2481 switch (ctlr->chip->cfg2) {
2483 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2484 ch->r_io[ATA_SSTATUS].offset = 0x00100 + ATA_MV_HOST_BASE(ch);
2485 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2486 ch->r_io[ATA_SERROR].offset = 0x00104 + ATA_MV_HOST_BASE(ch);
2487 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2488 ch->r_io[ATA_SCONTROL].offset = 0x00108 + ATA_MV_HOST_BASE(ch);
2491 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2492 ch->r_io[ATA_SSTATUS].offset = 0x02300 + ATA_MV_EDMA_BASE(ch);
2493 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2494 ch->r_io[ATA_SERROR].offset = 0x02304 + ATA_MV_EDMA_BASE(ch);
2495 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2496 ch->r_io[ATA_SCONTROL].offset = 0x02308 + ATA_MV_EDMA_BASE(ch);
2497 ch->r_io[ATA_SACTIVE].res = ctlr->r_res1;
2498 ch->r_io[ATA_SACTIVE].offset = 0x02350 + ATA_MV_EDMA_BASE(ch);
2502 ch->flags |= ATA_NO_SLAVE;
2503 ch->flags |= ATA_USE_16BIT; /* XXX SOS needed ? */
2504 ata_generic_hw(dev);
2505 ch->hw.begin_transaction = ata_marvell_edma_begin_transaction;
2506 ch->hw.end_transaction = ata_marvell_edma_end_transaction;
2507 ch->hw.status = ata_marvell_edma_status;
2509 /* disable the EDMA machinery */
2510 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2511 DELAY(100000); /* SOS should poll for disabled */
2513 /* set configuration to non-queued 128b read transfers stop on error */
2514 ATA_OUTL(ctlr->r_res1, 0x02000 + ATA_MV_EDMA_BASE(ch), (1<<11) | (1<<13));
2516 /* request queue base high */
2517 ATA_OUTL(ctlr->r_res1, 0x02010 + ATA_MV_EDMA_BASE(ch), work >> 32);
2519 /* request queue in ptr */
2520 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2522 /* request queue out ptr */
2523 ATA_OUTL(ctlr->r_res1, 0x02018 + ATA_MV_EDMA_BASE(ch), 0x0);
2525 /* response queue base high */
2527 ATA_OUTL(ctlr->r_res1, 0x0201c + ATA_MV_EDMA_BASE(ch), work >> 32);
2529 /* response queue in ptr */
2530 ATA_OUTL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch), 0x0);
2532 /* response queue out ptr */
2533 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2535 /* clear SATA error register */
2536 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2538 /* clear any outstanding error interrupts */
2539 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2541 /* unmask all error interrupts */
2542 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2544 /* enable EDMA machinery */
2545 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2550 ata_marvell_edma_status(device_t dev)
2552 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2553 struct ata_channel *ch = device_get_softc(dev);
2554 u_int32_t cause = ATA_INL(ctlr->r_res1, 0x01d60);
2555 int shift = (ch->unit << 1) + (ch->unit > 3);
2557 if (cause & (1 << shift)) {
2559 /* clear interrupt(s) */
2560 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2562 /* do we have any PHY events ? */
2563 ata_sata_phy_check_events(dev);
2566 /* do we have any device action ? */
2567 return (cause & (2 << shift));
2570 /* must be called with ATA channel locked and state_mtx held */
2572 ata_marvell_edma_begin_transaction(struct ata_request *request)
2574 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2575 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2581 int dummy, error, slot;
2583 /* only DMA R/W goes through the EMDA machine */
2584 if (request->u.ata.command != ATA_READ_DMA &&
2585 request->u.ata.command != ATA_WRITE_DMA) {
2587 /* disable the EDMA machinery */
2588 if (ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)
2589 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2590 return ata_begin_transaction(request);
2593 /* check for 48 bit access and convert if needed */
2594 ata_modify_if_48bit(request);
2596 /* check sanity, setup SG list and DMA engine */
2597 if ((error = ch->dma->load(ch->dev, request->data, request->bytecount,
2598 request->flags & ATA_R_READ, ch->dma->sg,
2600 device_printf(request->dev, "setting up DMA failed\n");
2601 request->result = error;
2602 return ATA_OP_FINISHED;
2605 /* get next free request queue slot */
2606 req_in = ATA_INL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch));
2607 slot = (((req_in & ~0xfffffc00) >> 5) + 0) & 0x1f;
2608 bytep = (u_int8_t *)(ch->dma->work);
2609 bytep += (slot << 5);
2610 wordp = (u_int16_t *)bytep;
2611 quadp = (u_int32_t *)bytep;
2613 /* fill in this request */
2614 quadp[0] = (long)ch->dma->sg_bus & 0xffffffff;
2615 quadp[1] = (u_int64_t)ch->dma->sg_bus >> 32;
2616 wordp[4] = (request->flags & ATA_R_READ ? 0x01 : 0x00) | (tag<<1);
2619 bytep[i++] = (request->u.ata.count >> 8) & 0xff;
2620 bytep[i++] = 0x10 | ATA_COUNT;
2621 bytep[i++] = request->u.ata.count & 0xff;
2622 bytep[i++] = 0x10 | ATA_COUNT;
2624 bytep[i++] = (request->u.ata.lba >> 24) & 0xff;
2625 bytep[i++] = 0x10 | ATA_SECTOR;
2626 bytep[i++] = request->u.ata.lba & 0xff;
2627 bytep[i++] = 0x10 | ATA_SECTOR;
2629 bytep[i++] = (request->u.ata.lba >> 32) & 0xff;
2630 bytep[i++] = 0x10 | ATA_CYL_LSB;
2631 bytep[i++] = (request->u.ata.lba >> 8) & 0xff;
2632 bytep[i++] = 0x10 | ATA_CYL_LSB;
2634 bytep[i++] = (request->u.ata.lba >> 40) & 0xff;
2635 bytep[i++] = 0x10 | ATA_CYL_MSB;
2636 bytep[i++] = (request->u.ata.lba >> 16) & 0xff;
2637 bytep[i++] = 0x10 | ATA_CYL_MSB;
2639 bytep[i++] = ATA_D_LBA | ATA_D_IBM | ((request->u.ata.lba >> 24) & 0xf);
2640 bytep[i++] = 0x10 | ATA_DRIVE;
2642 bytep[i++] = request->u.ata.command;
2643 bytep[i++] = 0x90 | ATA_COMMAND;
2645 /* enable EDMA machinery if needed */
2646 if (!(ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)) {
2647 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2648 while (!(ATA_INL(ctlr->r_res1,
2649 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
2653 /* tell EDMA it has a new request */
2654 slot = (((req_in & ~0xfffffc00) >> 5) + 1) & 0x1f;
2655 req_in &= 0xfffffc00;
2656 req_in += (slot << 5);
2657 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), req_in);
2659 return ATA_OP_CONTINUES;
2662 /* must be called with ATA channel locked and state_mtx held */
2664 ata_marvell_edma_end_transaction(struct ata_request *request)
2666 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2667 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2668 int offset = (ch->unit > 3 ? 0x30014 : 0x20014);
2669 u_int32_t icr = ATA_INL(ctlr->r_res1, offset);
2672 /* EDMA interrupt */
2673 if ((icr & (0x0001 << (ch->unit & 3)))) {
2674 struct ata_marvell_response *response;
2675 u_int32_t rsp_in, rsp_out;
2679 callout_stop(&request->callout);
2681 /* get response ptr's */
2682 rsp_in = ATA_INL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch));
2683 rsp_out = ATA_INL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch));
2684 slot = (((rsp_in & ~0xffffff00) >> 3)) & 0x1f;
2685 rsp_out &= 0xffffff00;
2686 rsp_out += (slot << 3);
2687 response = (struct ata_marvell_response *)
2688 (ch->dma->work + 1024 + (slot << 3));
2690 /* record status for this request */
2691 request->status = response->dev_status;
2695 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), rsp_out);
2697 /* update progress */
2698 if (!(request->status & ATA_S_ERROR) &&
2699 !(request->flags & ATA_R_TIMEOUT))
2700 request->donecount = request->bytecount;
2702 /* unload SG list */
2703 ch->dma->unload(ch->dev);
2705 res = ATA_OP_FINISHED;
2708 /* legacy ATA interrupt */
2710 res = ata_end_transaction(request);
2714 ATA_OUTL(ctlr->r_res1, offset, ~(icr & (0x0101 << (ch->unit & 3))));
2719 ata_marvell_edma_reset(device_t dev)
2721 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2722 struct ata_channel *ch = device_get_softc(dev);
2724 /* disable the EDMA machinery */
2725 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2726 while ((ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
2729 /* clear SATA error register */
2730 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2732 /* clear any outstanding error interrupts */
2733 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2735 /* unmask all error interrupts */
2736 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2738 /* enable channel and test for devices */
2739 if (ata_sata_phy_reset(dev))
2740 ata_generic_reset(dev);
2742 /* enable EDMA machinery */
2743 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2747 ata_marvell_edma_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs,
2750 struct ata_dmasetprd_args *args = xsc;
2751 struct ata_marvell_dma_prdentry *prd = args->dmatab;
2754 if ((args->error = error))
2757 for (i = 0; i < nsegs; i++) {
2758 prd[i].addrlo = htole32(segs[i].ds_addr);
2759 prd[i].count = htole32(segs[i].ds_len);
2760 prd[i].addrhi = htole32((u_int64_t)segs[i].ds_addr >> 32);
2762 prd[i - 1].count |= htole32(ATA_DMA_EOT);
2766 ata_marvell_edma_dmainit(device_t dev)
2768 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2769 struct ata_channel *ch = device_get_softc(dev);
2773 /* note start and stop are not used here */
2774 ch->dma->setprd = ata_marvell_edma_dmasetprd;
2776 if (ATA_INL(ctlr->r_res1, 0x00d00) & 0x00000004)
2777 ch->dma->max_address = BUS_SPACE_MAXADDR;
2783 * National chipset support functions
2786 ata_national_ident(device_t dev)
2788 struct ata_pci_controller *ctlr = device_get_softc(dev);
2790 /* this chip is a clone of the Cyrix chip, bugs and all */
2791 if (pci_get_devid(dev) == ATA_SC1100) {
2792 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
2793 ctlr->chipinit = ata_national_chipinit;
2800 ata_national_chipinit(device_t dev)
2802 struct ata_pci_controller *ctlr = device_get_softc(dev);
2804 if (ata_setup_interrupt(dev))
2807 ctlr->setmode = ata_national_setmode;
2812 ata_national_setmode(device_t dev, int mode)
2814 device_t gparent = GRANDPARENT(dev);
2815 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2816 struct ata_device *atadev = device_get_softc(dev);
2817 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2818 u_int32_t piotiming[] =
2819 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
2820 0x00803020, 0x20102010, 0x00100010,
2821 0x00100010, 0x00100010, 0x00100010 };
2822 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
2823 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
2826 ch->dma->alignment = 16;
2827 ch->dma->max_iosize = 126 * DEV_BSIZE;
2829 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2831 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2834 device_printf(dev, "%s setting %s on National chip\n",
2835 (error) ? "failed" : "success", ata_mode2str(mode));
2837 if (mode >= ATA_UDMA0) {
2838 pci_write_config(gparent, 0x44 + (devno << 3),
2839 udmatiming[mode & ATA_MODE_MASK], 4);
2841 else if (mode >= ATA_WDMA0) {
2842 pci_write_config(gparent, 0x44 + (devno << 3),
2843 dmatiming[mode & ATA_MODE_MASK], 4);
2846 pci_write_config(gparent, 0x44 + (devno << 3),
2847 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
2850 pci_write_config(gparent, 0x40 + (devno << 3),
2851 piotiming[ata_mode2idx(mode)], 4);
2852 atadev->mode = mode;
2858 * NetCell chipset support functions
2861 ata_netcell_ident(device_t dev)
2863 struct ata_pci_controller *ctlr = device_get_softc(dev);
2865 if (pci_get_devid(dev) == ATA_NETCELL_SR) {
2866 device_set_desc(dev, "Netcell SyncRAID SR3000/5000 RAID Controller");
2867 ctlr->chipinit = ata_netcell_chipinit;
2874 ata_netcell_chipinit(device_t dev)
2876 struct ata_pci_controller *ctlr = device_get_softc(dev);
2878 if (ata_generic_chipinit(dev))
2881 ctlr->allocate = ata_netcell_allocate;
2886 ata_netcell_allocate(device_t dev)
2888 struct ata_channel *ch = device_get_softc(dev);
2890 /* setup the usual register normal pci style */
2891 if (ata_pci_allocate(dev))
2894 /* the NetCell only supports 16 bit PIO transfers */
2895 ch->flags |= ATA_USE_16BIT;
2902 * nVidia chipset support functions
2905 ata_nvidia_ident(device_t dev)
2907 struct ata_pci_controller *ctlr = device_get_softc(dev);
2908 struct ata_chip_id *idx;
2909 static struct ata_chip_id ids[] =
2910 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nForce" },
2911 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2" },
2912 { ATA_NFORCE2_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2 Pro" },
2913 { ATA_NFORCE2_PRO_S1, 0, 0, 0, ATA_SA150, "nForce2 Pro" },
2914 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3" },
2915 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3 Pro" },
2916 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
2917 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
2918 { ATA_NFORCE_MCP04, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP" },
2919 { ATA_NFORCE_MCP04_S1, 0, 0, NV4, ATA_SA150, "nForce MCP" },
2920 { ATA_NFORCE_MCP04_S2, 0, 0, NV4, ATA_SA150, "nForce MCP" },
2921 { ATA_NFORCE_CK804, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce CK804" },
2922 { ATA_NFORCE_CK804_S1, 0, 0, NV4, ATA_SA300, "nForce CK804" },
2923 { ATA_NFORCE_CK804_S2, 0, 0, NV4, ATA_SA300, "nForce CK804" },
2924 { ATA_NFORCE_MCP51, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP51" },
2925 { ATA_NFORCE_MCP51_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP51" },
2926 { ATA_NFORCE_MCP51_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP51" },
2927 { ATA_NFORCE_MCP55, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP55" },
2928 { ATA_NFORCE_MCP55_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP55" },
2929 { ATA_NFORCE_MCP55_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP55" },
2930 { ATA_NFORCE_MCP61, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP61" },
2931 { ATA_NFORCE_MCP61_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP61" },
2932 { ATA_NFORCE_MCP61_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP61" },
2933 { ATA_NFORCE_MCP61_S3, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP61" },
2934 { 0, 0, 0, 0, 0, 0}} ;
2937 if (!(idx = ata_match_chip(dev, ids)))
2940 sprintf(buffer, "nVidia %s %s controller",
2941 idx->text, ata_mode2str(idx->max_dma));
2942 device_set_desc_copy(dev, buffer);
2944 ctlr->chipinit = ata_nvidia_chipinit;
2949 ata_nvidia_chipinit(device_t dev)
2951 struct ata_pci_controller *ctlr = device_get_softc(dev);
2953 if (ata_setup_interrupt(dev))
2956 if (ctlr->chip->max_dma >= ATA_SA150) {
2957 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
2958 ctlr->r_type2 = SYS_RES_IOPORT;
2960 ctlr->r_type2 = SYS_RES_MEMORY;
2961 ctlr->r_rid2 = PCIR_BAR(5);
2962 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2963 &ctlr->r_rid2, RF_ACTIVE))) {
2964 int offset = ctlr->chip->cfg2 & NV4 ? 0x0440 : 0x0010;
2966 ctlr->allocate = ata_nvidia_allocate;
2967 ctlr->reset = ata_nvidia_reset;
2969 /* enable control access */
2970 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
2972 if (ctlr->chip->cfg2 & NVQ) {
2973 /* clear interrupt status */
2974 ATA_OUTL(ctlr->r_res2, offset, 0x00ff00ff);
2976 /* enable device and PHY state change interrupts */
2977 ATA_OUTL(ctlr->r_res2, offset + 4, 0x000d000d);
2979 /* disable NCQ support */
2980 ATA_OUTL(ctlr->r_res2, 0x0400,
2981 ATA_INL(ctlr->r_res2, 0x0400) & 0xfffffff9);
2984 /* clear interrupt status */
2985 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2987 /* enable device and PHY state change interrupts */
2988 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
2991 /* enable PCI interrupt */
2992 pci_write_config(dev, PCIR_COMMAND,
2993 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
2996 ctlr->setmode = ata_sata_setmode;
2999 /* disable prefetch, postwrite */
3000 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
3001 ctlr->setmode = ata_via_family_setmode;
3007 ata_nvidia_allocate(device_t dev)
3009 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3010 struct ata_channel *ch = device_get_softc(dev);
3012 /* setup the usual register normal pci style */
3013 if (ata_pci_allocate(dev))
3016 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3017 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
3018 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3019 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
3020 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3021 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
3023 ch->hw.status = ata_nvidia_status;
3024 ch->flags |= ATA_NO_SLAVE;
3030 ata_nvidia_status(device_t dev)
3032 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3033 struct ata_channel *ch = device_get_softc(dev);
3034 int offset = ctlr->chip->cfg2 & NV4 ? 0x0440 : 0x0010;
3035 int shift = ch->unit << (ctlr->chip->cfg2 & NVQ ? 4 : 2);
3036 u_int32_t istatus = ATA_INL(ctlr->r_res2, offset);
3038 /* do we have any PHY events ? */
3039 if (istatus & (0x0c << shift))
3040 ata_sata_phy_check_events(dev);
3042 /* clear interrupt(s) */
3043 ATA_OUTB(ctlr->r_res2, offset,
3044 (0x0f << shift) | (ctlr->chip->cfg2 & NVQ ? 0x00f000f0 : 0));
3046 /* do we have any device action ? */
3047 return (istatus & (0x01 << shift));
3051 ata_nvidia_reset(device_t dev)
3053 if (ata_sata_phy_reset(dev))
3054 ata_generic_reset(dev);
3059 * Promise chipset support functions
3061 #define ATA_PDC_APKT_OFFSET 0x00000010
3062 #define ATA_PDC_HPKT_OFFSET 0x00000040
3063 #define ATA_PDC_ASG_OFFSET 0x00000080
3064 #define ATA_PDC_LSG_OFFSET 0x000000c0
3065 #define ATA_PDC_HSG_OFFSET 0x00000100
3066 #define ATA_PDC_CHN_OFFSET 0x00000400
3067 #define ATA_PDC_BUF_BASE 0x00400000
3068 #define ATA_PDC_BUF_OFFSET 0x00100000
3069 #define ATA_PDC_MAX_HPKT 8
3070 #define ATA_PDC_WRITE_REG 0x00
3071 #define ATA_PDC_WRITE_CTL 0x0e
3072 #define ATA_PDC_WRITE_END 0x08
3073 #define ATA_PDC_WAIT_NBUSY 0x10
3074 #define ATA_PDC_WAIT_READY 0x18
3075 #define ATA_PDC_1B 0x20
3076 #define ATA_PDC_2B 0x40
3078 struct host_packet {
3080 TAILQ_ENTRY(host_packet) chain;
3083 struct ata_promise_sx4 {
3085 TAILQ_HEAD(, host_packet) queue;
3090 ata_promise_ident(device_t dev)
3092 struct ata_pci_controller *ctlr = device_get_softc(dev);
3093 struct ata_chip_id *idx;
3094 static struct ata_chip_id ids[] =
3095 {{ ATA_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "PDC20246" },
3096 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20262" },
3097 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20263" },
3098 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20265" },
3099 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20267" },
3100 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20268" },
3101 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "PDC20269" },
3102 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20270" },
3103 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "PDC20271" },
3104 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "PDC20275" },
3105 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "PDC20276" },
3106 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "PDC20277" },
3107 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "PDC20318" },
3108 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "PDC20319" },
3109 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20371" },
3110 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20375" },
3111 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20376" },
3112 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20377" },
3113 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20378" },
3114 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20379" },
3115 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20571" },
3116 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20575" },
3117 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20579" },
3118 { ATA_PDC20771, 0, PRMIO, PRCMBO2, ATA_SA300, "PDC20771" },
3119 { ATA_PDC40775, 0, PRMIO, PRCMBO2, ATA_SA300, "PDC40775" },
3120 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20617" },
3121 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20618" },
3122 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20619" },
3123 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20620" },
3124 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "PDC20621" },
3125 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "PDC20622" },
3126 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40518" },
3127 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40519" },
3128 { ATA_PDC40718, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40718" },
3129 { ATA_PDC40719, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40719" },
3130 { ATA_PDC40779, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40779" },
3131 { 0, 0, 0, 0, 0, 0}};
3133 uintptr_t devid = 0;
3135 if (!(idx = ata_match_chip(dev, ids)))
3138 /* if we are on a SuperTrak SX6000 dont attach */
3139 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
3140 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
3141 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
3142 devid == ATA_I960RM)
3145 strcpy(buffer, "Promise ");
3146 strcat(buffer, idx->text);
3148 /* if we are on a FastTrak TX4, adjust the interrupt resource */
3149 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
3150 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
3151 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
3152 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
3153 static long start = 0, end = 0;
3155 if (pci_get_slot(dev) == 1) {
3156 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
3157 strcat(buffer, " (channel 0+1)");
3159 else if (pci_get_slot(dev) == 2 && start && end) {
3160 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
3161 strcat(buffer, " (channel 2+3)");
3167 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
3168 device_set_desc_copy(dev, buffer);
3170 ctlr->chipinit = ata_promise_chipinit;
3175 ata_promise_chipinit(device_t dev)
3177 struct ata_pci_controller *ctlr = device_get_softc(dev);
3178 int fake_reg, stat_reg;
3180 if (ata_setup_interrupt(dev))
3183 switch (ctlr->chip->cfg1) {
3186 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
3188 ctlr->dmainit = ata_promise_dmainit;
3192 /* enable burst mode */
3193 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
3194 ctlr->allocate = ata_promise_allocate;
3195 ctlr->setmode = ata_promise_setmode;
3199 ctlr->allocate = ata_promise_tx2_allocate;
3200 ctlr->setmode = ata_promise_setmode;
3204 ctlr->r_type1 = SYS_RES_MEMORY;
3205 ctlr->r_rid1 = PCIR_BAR(4);
3206 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
3207 &ctlr->r_rid1, RF_ACTIVE)))
3210 ctlr->r_type2 = SYS_RES_MEMORY;
3211 ctlr->r_rid2 = PCIR_BAR(3);
3212 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3213 &ctlr->r_rid2, RF_ACTIVE)))
3216 if (ctlr->chip->cfg2 == PRSX4X) {
3217 struct ata_promise_sx4 *hpkt;
3218 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
3220 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
3221 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
3222 ata_promise_sx4_intr, ctlr, &ctlr->handle)) {
3223 device_printf(dev, "unable to setup interrupt\n");
3227 /* print info about cache memory */
3228 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
3229 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
3230 ((dimm >> 24) & 0xff),
3231 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
3232 " ECC enabled" : "" );
3234 /* adjust cache memory parameters */
3235 ATA_OUTL(ctlr->r_res2, 0x000c000c,
3236 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
3238 /* setup host packet controls */
3239 hpkt = malloc(sizeof(struct ata_promise_sx4),
3240 M_TEMP, M_NOWAIT | M_ZERO);
3241 mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
3242 TAILQ_INIT(&hpkt->queue);
3244 device_set_ivars(dev, hpkt);
3245 ctlr->allocate = ata_promise_mio_allocate;
3246 ctlr->reset = ata_promise_mio_reset;
3247 ctlr->dmainit = ata_promise_mio_dmainit;
3248 ctlr->setmode = ata_promise_setmode;
3253 /* mio type controllers need an interrupt intercept */
3254 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
3255 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
3256 ata_promise_mio_intr, ctlr, &ctlr->handle)) {
3257 device_printf(dev, "unable to setup interrupt\n");
3261 switch (ctlr->chip->cfg2) {
3263 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
3264 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
3288 /* prime fake interrupt register */
3289 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3291 /* clear SATA status */
3292 ATA_OUTL(ctlr->r_res2, stat_reg, 0x000000ff);
3294 ctlr->allocate = ata_promise_mio_allocate;
3295 ctlr->reset = ata_promise_mio_reset;
3296 ctlr->dmainit = ata_promise_mio_dmainit;
3297 ctlr->setmode = ata_promise_mio_setmode;
3304 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
3306 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
3311 ata_promise_allocate(device_t dev)
3313 struct ata_channel *ch = device_get_softc(dev);
3315 if (ata_pci_allocate(dev))
3318 ch->hw.status = ata_promise_status;
3323 ata_promise_status(device_t dev)
3325 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3326 struct ata_channel *ch = device_get_softc(dev);
3328 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)) {
3329 return ata_pci_status(dev);
3335 ata_promise_dmastart(device_t dev)
3337 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3338 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3339 struct ata_device *atadev = device_get_softc(dev);
3341 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3342 ATA_OUTB(ctlr->r_res1, 0x11,
3343 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
3344 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20,
3345 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
3346 (ch->dma->cur_iosize >> 1));
3348 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
3349 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
3350 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
3351 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3352 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
3353 ATA_BMCMD_START_STOP);
3354 ch->flags |= ATA_DMA_ACTIVE;
3359 ata_promise_dmastop(device_t dev)
3361 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3362 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3363 struct ata_device *atadev = device_get_softc(dev);
3366 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3367 ATA_OUTB(ctlr->r_res1, 0x11,
3368 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
3369 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20, 0);
3371 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
3372 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3373 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3374 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3375 ch->flags &= ~ATA_DMA_ACTIVE;
3380 ata_promise_dmareset(device_t dev)
3382 struct ata_channel *ch = device_get_softc(dev);
3384 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3385 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3386 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3387 ch->flags &= ~ATA_DMA_ACTIVE;
3391 ata_promise_dmainit(device_t dev)
3393 struct ata_channel *ch = device_get_softc(dev);
3397 ch->dma->start = ata_promise_dmastart;
3398 ch->dma->stop = ata_promise_dmastop;
3399 ch->dma->reset = ata_promise_dmareset;
3404 ata_promise_setmode(device_t dev, int mode)
3406 device_t gparent = GRANDPARENT(dev);
3407 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3408 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3409 struct ata_device *atadev = device_get_softc(dev);
3410 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3412 u_int32_t timings[][2] = {
3413 /* PROLD PRNEW mode */
3414 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
3415 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
3416 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
3417 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
3418 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
3419 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
3420 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
3421 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
3422 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
3423 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
3424 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
3425 { 0, 0x00424ef6 }, /* UDMA 3 */
3426 { 0, 0x004127f3 }, /* UDMA 4 */
3427 { 0, 0x004127f3 } /* UDMA 5 */
3430 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3432 switch (ctlr->chip->cfg1) {
3435 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
3436 (ch->unit ? 1 << 11 : 1 << 10))) {
3437 ata_print_cable(dev, "controller");
3440 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
3441 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
3445 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3446 if (mode > ATA_UDMA2 &&
3447 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
3448 ata_print_cable(dev, "controller");
3454 if (mode > ATA_UDMA2 &&
3455 (ATA_INL(ctlr->r_res2,
3456 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
3457 (ch->unit << 7)) & 0x01000000)) {
3458 ata_print_cable(dev, "controller");
3464 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3467 device_printf(dev, "%ssetting %s on %s chip\n",
3468 (error) ? "FAILURE " : "",
3469 ata_mode2str(mode), ctlr->chip->text);
3471 if (ctlr->chip->cfg1 < PRTX)
3472 pci_write_config(gparent, 0x60 + (devno << 2),
3473 timings[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
3474 atadev->mode = mode;
3480 ata_promise_tx2_allocate(device_t dev)
3482 struct ata_channel *ch = device_get_softc(dev);
3484 if (ata_pci_allocate(dev))
3487 ch->hw.status = ata_promise_tx2_status;
3492 ata_promise_tx2_status(device_t dev)
3494 struct ata_channel *ch = device_get_softc(dev);
3496 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3497 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
3498 return ata_pci_status(dev);
3504 ata_promise_mio_allocate(device_t dev)
3506 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3507 struct ata_channel *ch = device_get_softc(dev);
3508 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
3511 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
3512 ch->r_io[i].res = ctlr->r_res2;
3513 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
3515 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
3516 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
3517 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
3518 ata_default_registers(dev);
3519 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
3520 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
3521 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3522 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
3523 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3524 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
3525 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3526 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
3527 ch->flags |= ATA_NO_SLAVE;
3529 ch->flags |= ATA_USE_16BIT;
3531 ata_generic_hw(dev);
3532 if (ctlr->chip->cfg2 & PRSX4X) {
3533 ch->hw.command = ata_promise_sx4_command;
3536 ch->hw.command = ata_promise_mio_command;
3537 ch->hw.status = ata_promise_mio_status;
3543 ata_promise_mio_intr(void *data)
3545 struct ata_pci_controller *ctlr = data;
3546 struct ata_channel *ch;
3550 switch (ctlr->chip->cfg2) {
3564 * since reading interrupt status register on early "mio" chips
3565 * clears the status bits we cannot read it for each channel later on
3566 * in the generic interrupt routine.
3567 * store the bits in an unused register in the chip so we can read
3568 * it from there safely to get around this "feature".
3570 vector = ATA_INL(ctlr->r_res2, 0x040);
3571 ATA_OUTL(ctlr->r_res2, 0x040, vector);
3572 ATA_OUTL(ctlr->r_res2, fake_reg, vector);
3574 for (unit = 0; unit < ctlr->channels; unit++) {
3575 if ((ch = ctlr->interrupt[unit].argument))
3576 ctlr->interrupt[unit].function(ch);
3579 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3583 ata_promise_mio_status(device_t dev)
3585 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3586 struct ata_channel *ch = device_get_softc(dev);
3587 struct ata_connect_task *tp;
3588 u_int32_t fake_reg, stat_reg, vector, status;
3590 switch (ctlr->chip->cfg2) {
3605 /* read and acknowledge interrupt */
3606 vector = ATA_INL(ctlr->r_res2, fake_reg);
3608 /* read and clear interface status */
3609 status = ATA_INL(ctlr->r_res2, stat_reg);
3610 ATA_OUTL(ctlr->r_res2, stat_reg, status & (0x00000011 << ch->unit));
3612 /* check for and handle disconnect events */
3613 if ((status & (0x00000001 << ch->unit)) &&
3614 (tp = (struct ata_connect_task *)
3615 malloc(sizeof(struct ata_connect_task),
3616 M_ATA, M_NOWAIT | M_ZERO))) {
3619 device_printf(ch->dev, "DISCONNECT requested\n");
3620 tp->action = ATA_C_DETACH;
3622 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3623 taskqueue_enqueue(taskqueue_thread, &tp->task);
3626 /* check for and handle connect events */
3627 if ((status & (0x00000010 << ch->unit)) &&
3628 (tp = (struct ata_connect_task *)
3629 malloc(sizeof(struct ata_connect_task),
3630 M_ATA, M_NOWAIT | M_ZERO))) {
3633 device_printf(ch->dev, "CONNECT requested\n");
3634 tp->action = ATA_C_ATTACH;
3636 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3637 taskqueue_enqueue(taskqueue_thread, &tp->task);
3640 /* do we have any device action ? */
3641 return (vector & (1 << (ch->unit + 1)));
3645 ata_promise_mio_command(struct ata_request *request)
3647 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
3648 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
3649 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
3651 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
3653 /* XXX SOS add ATAPI commands support later */
3654 switch (request->u.ata.command) {
3656 return ata_generic_command(request);
3659 case ATA_READ_DMA48:
3660 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
3664 case ATA_WRITE_DMA48:
3665 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
3668 wordp[1] = htole32(ch->dma->sg_bus);
3670 ata_promise_apkt((u_int8_t*)wordp, request);
3672 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
3677 ata_promise_mio_reset(device_t dev)
3679 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3680 struct ata_channel *ch = device_get_softc(dev);
3681 struct ata_promise_sx4 *hpktp;
3683 switch (ctlr->chip->cfg2) {
3686 /* softreset channel ATA module */
3687 hpktp = device_get_ivars(ctlr->dev);
3688 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
3690 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
3691 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
3692 ~0x00003f9f) | (ch->unit + 1));
3694 /* softreset HOST module */ /* XXX SOS what about other outstandings */
3695 mtx_lock(&hpktp->mtx);
3696 ATA_OUTL(ctlr->r_res2, 0xc012c,
3697 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
3699 ATA_OUTL(ctlr->r_res2, 0xc012c,
3700 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
3702 mtx_unlock(&hpktp->mtx);
3703 ata_generic_reset(dev);
3709 if ((ctlr->chip->cfg2 == PRSATA) ||
3710 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3712 /* mask plug/unplug intr */
3713 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
3716 /* softreset channels ATA module */
3717 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
3719 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
3720 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
3721 ~0x00003f9f) | (ch->unit + 1));
3723 if ((ctlr->chip->cfg2 == PRSATA) ||
3724 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3726 if (ata_sata_phy_reset(dev))
3727 ata_generic_reset(dev);
3729 /* reset and enable plug/unplug intr */
3730 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
3733 ata_generic_reset(dev);
3738 if ((ctlr->chip->cfg2 == PRSATA2) ||
3739 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
3740 /* set portmultiplier port */
3741 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
3743 /* mask plug/unplug intr */
3744 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
3747 /* softreset channels ATA module */
3748 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
3750 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
3751 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
3752 ~0x00003f9f) | (ch->unit + 1));
3754 if ((ctlr->chip->cfg2 == PRSATA2) ||
3755 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
3757 /* set PHY mode to "improved" */
3758 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
3759 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
3760 ~0x00000003) | 0x00000001);
3762 if (ata_sata_phy_reset(dev))
3763 ata_generic_reset(dev);
3765 /* reset and enable plug/unplug intr */
3766 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
3768 /* set portmultiplier port */
3769 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
3772 ata_generic_reset(dev);
3779 ata_promise_mio_dmainit(device_t dev)
3781 /* note start and stop are not used here */
3786 ata_promise_mio_setmode(device_t dev, int mode)
3788 device_t gparent = GRANDPARENT(dev);
3789 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3790 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3792 if ( (ctlr->chip->cfg2 == PRSATA) ||
3793 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2)) ||
3794 (ctlr->chip->cfg2 == PRSATA2) ||
3795 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2)))
3796 ata_sata_setmode(dev, mode);
3798 ata_promise_setmode(dev, mode);
3802 ata_promise_sx4_intr(void *data)
3804 struct ata_pci_controller *ctlr = data;
3805 struct ata_channel *ch;
3806 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
3809 for (unit = 0; unit < ctlr->channels; unit++) {
3810 if (vector & (1 << (unit + 1)))
3811 if ((ch = ctlr->interrupt[unit].argument))
3812 ctlr->interrupt[unit].function(ch);
3813 if (vector & (1 << (unit + 5)))
3814 if ((ch = ctlr->interrupt[unit].argument))
3815 ata_promise_queue_hpkt(ctlr,
3816 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
3817 ATA_PDC_HPKT_OFFSET));
3818 if (vector & (1 << (unit + 9))) {
3819 ata_promise_next_hpkt(ctlr);
3820 if ((ch = ctlr->interrupt[unit].argument))
3821 ctlr->interrupt[unit].function(ch);
3823 if (vector & (1 << (unit + 13))) {
3824 ata_promise_next_hpkt(ctlr);
3825 if ((ch = ctlr->interrupt[unit].argument))
3826 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
3827 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
3828 ATA_PDC_APKT_OFFSET));
3834 ata_promise_sx4_command(struct ata_request *request)
3836 device_t gparent = GRANDPARENT(request->dev);
3837 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3838 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
3839 struct ata_dma_prdentry *prd = ch->dma->sg;
3840 caddr_t window = rman_get_virtual(ctlr->r_res1);
3842 int i, idx, length = 0;
3844 /* XXX SOS add ATAPI commands support later */
3845 switch (request->u.ata.command) {
3850 case ATA_ATA_IDENTIFY:
3854 case ATA_READ_MUL48:
3858 case ATA_WRITE_MUL48:
3859 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
3860 return ata_generic_command(request);
3862 case ATA_SETFEATURES:
3863 case ATA_FLUSHCACHE:
3864 case ATA_FLUSHCACHE48:
3867 wordp = (u_int32_t *)
3868 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
3869 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
3872 ata_promise_apkt((u_int8_t *)wordp, request);
3873 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
3874 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
3875 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
3876 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
3880 case ATA_READ_DMA48:
3882 case ATA_WRITE_DMA48:
3883 wordp = (u_int32_t *)
3884 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
3887 wordp[idx++] = prd[i].addr;
3888 wordp[idx++] = prd[i].count;
3889 length += (prd[i].count & ~ATA_DMA_EOT);
3890 } while (!(prd[i++].count & ATA_DMA_EOT));
3892 wordp = (u_int32_t *)
3893 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
3894 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
3895 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
3897 wordp = (u_int32_t *)
3898 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
3899 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
3900 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
3902 wordp = (u_int32_t *)
3903 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
3904 if (request->flags & ATA_R_READ)
3905 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
3906 if (request->flags & ATA_R_WRITE)
3907 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
3908 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
3909 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
3912 wordp = (u_int32_t *)
3913 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
3914 if (request->flags & ATA_R_READ)
3915 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
3916 if (request->flags & ATA_R_WRITE)
3917 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
3918 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
3920 ata_promise_apkt((u_int8_t *)wordp, request);
3921 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
3923 if (request->flags & ATA_R_READ) {
3924 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
3925 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
3926 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
3927 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
3929 if (request->flags & ATA_R_WRITE) {
3930 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
3931 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
3932 ata_promise_queue_hpkt(ctlr,
3933 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
3940 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
3942 struct ata_device *atadev = device_get_softc(request->dev);
3945 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
3946 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
3947 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
3948 bytep[i++] = ATA_A_4BIT;
3950 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3951 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
3952 bytep[i++] = request->u.ata.feature >> 8;
3953 bytep[i++] = request->u.ata.feature;
3954 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
3955 bytep[i++] = request->u.ata.count >> 8;
3956 bytep[i++] = request->u.ata.count;
3957 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
3958 bytep[i++] = request->u.ata.lba >> 24;
3959 bytep[i++] = request->u.ata.lba;
3960 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
3961 bytep[i++] = request->u.ata.lba >> 32;
3962 bytep[i++] = request->u.ata.lba >> 8;
3963 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
3964 bytep[i++] = request->u.ata.lba >> 40;
3965 bytep[i++] = request->u.ata.lba >> 16;
3966 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
3967 bytep[i++] = ATA_D_LBA | atadev->unit;
3970 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
3971 bytep[i++] = request->u.ata.feature;
3972 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
3973 bytep[i++] = request->u.ata.count;
3974 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
3975 bytep[i++] = request->u.ata.lba;
3976 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
3977 bytep[i++] = request->u.ata.lba >> 8;
3978 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
3979 bytep[i++] = request->u.ata.lba >> 16;
3980 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
3981 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
3982 ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
3984 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
3985 bytep[i++] = request->u.ata.command;
3990 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
3992 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
3994 mtx_lock(&hpktp->mtx);
3996 struct host_packet *hp =
3997 malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
3999 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
4003 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
4005 mtx_unlock(&hpktp->mtx);
4009 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
4011 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
4012 struct host_packet *hp;
4014 mtx_lock(&hpktp->mtx);
4015 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
4016 TAILQ_REMOVE(&hpktp->queue, hp, chain);
4017 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
4022 mtx_unlock(&hpktp->mtx);
4027 * ServerWorks chipset support functions
4030 ata_serverworks_ident(device_t dev)
4032 struct ata_pci_controller *ctlr = device_get_softc(dev);
4033 struct ata_chip_id *idx;
4034 static struct ata_chip_id ids[] =
4035 {{ ATA_ROSB4, 0x00, SWKS33, 0, ATA_UDMA2, "ROSB4" },
4036 { ATA_CSB5, 0x92, SWKS100, 0, ATA_UDMA5, "CSB5" },
4037 { ATA_CSB5, 0x00, SWKS66, 0, ATA_UDMA4, "CSB5" },
4038 { ATA_CSB6, 0x00, SWKS100, 0, ATA_UDMA5, "CSB6" },
4039 { ATA_CSB6_1, 0x00, SWKS66, 0, ATA_UDMA4, "CSB6" },
4040 { ATA_HT1000, 0x00, SWKS100, 0, ATA_UDMA5, "HT1000" },
4041 { ATA_HT1000_S1, 0x00, SWKS100, 4, ATA_SA150, "HT1000" },
4042 { ATA_HT1000_S2, 0x00, SWKSMIO, 4, ATA_SA150, "HT1000" },
4043 { ATA_K2, 0x00, SWKSMIO, 4, ATA_SA150, "K2" },
4044 { ATA_FRODO4, 0x00, SWKSMIO, 4, ATA_SA150, "Frodo4" },
4045 { ATA_FRODO8, 0x00, SWKSMIO, 8, ATA_SA150, "Frodo8" },
4046 { 0, 0, 0, 0, 0, 0}};
4049 if (!(idx = ata_match_chip(dev, ids)))
4052 sprintf(buffer, "ServerWorks %s %s controller",
4053 idx->text, ata_mode2str(idx->max_dma));
4054 device_set_desc_copy(dev, buffer);
4056 ctlr->chipinit = ata_serverworks_chipinit;
4061 ata_serverworks_chipinit(device_t dev)
4063 struct ata_pci_controller *ctlr = device_get_softc(dev);
4065 if (ata_setup_interrupt(dev))
4068 if (ctlr->chip->cfg1 == SWKSMIO) {
4069 ctlr->r_type2 = SYS_RES_MEMORY;
4070 ctlr->r_rid2 = PCIR_BAR(5);
4071 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4072 &ctlr->r_rid2, RF_ACTIVE)))
4075 ctlr->channels = ctlr->chip->cfg2;
4076 ctlr->allocate = ata_serverworks_allocate;
4077 ctlr->setmode = ata_sata_setmode;
4080 else if (ctlr->chip->cfg1 == SWKS33) {
4084 /* locate the ISA part in the southbridge and enable UDMA33 */
4085 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
4086 for (i = 0; i < nchildren; i++) {
4087 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
4088 pci_write_config(children[i], 0x64,
4089 (pci_read_config(children[i], 0x64, 4) &
4090 ~0x00002000) | 0x00004000, 4);
4094 free(children, M_TEMP);
4098 pci_write_config(dev, 0x5a,
4099 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
4100 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
4102 ctlr->setmode = ata_serverworks_setmode;
4107 ata_serverworks_allocate(device_t dev)
4109 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4110 struct ata_channel *ch = device_get_softc(dev);
4114 ch_offset = ch->unit * 0x100;
4116 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
4117 ch->r_io[i].res = ctlr->r_res2;
4119 /* setup ATA registers */
4120 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
4121 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x04;
4122 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
4123 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
4124 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
4125 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
4126 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
4127 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1c;
4128 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x20;
4129 ata_default_registers(dev);
4131 /* setup DMA registers */
4132 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x30;
4133 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x32;
4134 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x34;
4136 /* setup SATA registers */
4137 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x40;
4138 ch->r_io[ATA_SERROR].offset = ch_offset + 0x44;
4139 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x48;
4141 ch->flags |= ATA_NO_SLAVE;
4147 ata_serverworks_setmode(device_t dev, int mode)
4149 device_t gparent = GRANDPARENT(dev);
4150 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4151 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4152 struct ata_device *atadev = device_get_softc(dev);
4153 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4154 int offset = (devno ^ 0x01) << 3;
4156 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
4157 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
4158 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
4160 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4162 mode = ata_check_80pin(dev, mode);
4164 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4167 device_printf(dev, "%ssetting %s on %s chip\n",
4168 (error) ? "FAILURE " : "",
4169 ata_mode2str(mode), ctlr->chip->text);
4171 if (mode >= ATA_UDMA0) {
4172 pci_write_config(gparent, 0x56,
4173 (pci_read_config(gparent, 0x56, 2) &
4174 ~(0xf << (devno << 2))) |
4175 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
4176 pci_write_config(gparent, 0x54,
4177 pci_read_config(gparent, 0x54, 1) |
4178 (0x01 << devno), 1);
4179 pci_write_config(gparent, 0x44,
4180 (pci_read_config(gparent, 0x44, 4) &
4181 ~(0xff << offset)) |
4182 (dmatimings[2] << offset), 4);
4184 else if (mode >= ATA_WDMA0) {
4185 pci_write_config(gparent, 0x54,
4186 pci_read_config(gparent, 0x54, 1) &
4187 ~(0x01 << devno), 1);
4188 pci_write_config(gparent, 0x44,
4189 (pci_read_config(gparent, 0x44, 4) &
4190 ~(0xff << offset)) |
4191 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
4194 pci_write_config(gparent, 0x54,
4195 pci_read_config(gparent, 0x54, 1) &
4196 ~(0x01 << devno), 1);
4198 pci_write_config(gparent, 0x40,
4199 (pci_read_config(gparent, 0x40, 4) &
4200 ~(0xff << offset)) |
4201 (piotimings[ata_mode2idx(mode)] << offset), 4);
4202 atadev->mode = mode;
4208 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
4211 ata_sii_ident(device_t dev)
4213 struct ata_pci_controller *ctlr = device_get_softc(dev);
4214 struct ata_chip_id *idx;
4215 static struct ata_chip_id ids[] =
4216 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
4217 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
4218 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
4219 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
4220 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
4221 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
4222 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
4223 { ATA_SII3124, 0x00, SIIPRBIO, SII4CH, ATA_SA300, "SiI 3124" },
4224 { ATA_SII3132, 0x00, SIIPRBIO, 0, ATA_SA300, "SiI 3132" },
4225 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
4226 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
4227 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
4228 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
4229 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
4230 { 0, 0, 0, 0, 0, 0}};
4233 if (!(idx = ata_match_chip(dev, ids)))
4236 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
4237 device_set_desc_copy(dev, buffer);
4239 ctlr->chipinit = ata_sii_chipinit;
4244 ata_sii_chipinit(device_t dev)
4246 struct ata_pci_controller *ctlr = device_get_softc(dev);
4248 if (ata_setup_interrupt(dev))
4251 switch (ctlr->chip->cfg1) {
4253 ctlr->r_type1 = SYS_RES_MEMORY;
4254 ctlr->r_rid1 = PCIR_BAR(0);
4255 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
4256 &ctlr->r_rid1, RF_ACTIVE)))
4259 ctlr->r_rid2 = PCIR_BAR(2);
4260 ctlr->r_type2 = SYS_RES_MEMORY;
4261 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4262 &ctlr->r_rid2, RF_ACTIVE))){
4263 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
4266 ctlr->allocate = ata_siiprb_allocate;
4267 ctlr->reset = ata_siiprb_reset;
4268 ctlr->dmainit = ata_siiprb_dmainit;
4269 ctlr->setmode = ata_sata_setmode;
4270 ctlr->channels = (ctlr->chip->cfg2 == SII4CH) ? 4 : 2;
4272 /* reset controller */
4273 ATA_OUTL(ctlr->r_res1, 0x0040, 0x80000000);
4275 ATA_OUTL(ctlr->r_res1, 0x0040, 0x0000000f);
4277 /* enable PCI interrupt */
4278 pci_write_config(dev, PCIR_COMMAND,
4279 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
4283 ctlr->r_type2 = SYS_RES_MEMORY;
4284 ctlr->r_rid2 = PCIR_BAR(5);
4285 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4286 &ctlr->r_rid2, RF_ACTIVE)))
4289 if (ctlr->chip->cfg2 & SIISETCLK) {
4290 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
4291 pci_write_config(dev, 0x8a,
4292 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
4293 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
4294 device_printf(dev, "%s could not set ATA133 clock\n",
4298 /* if we have 4 channels enable the second set */
4299 if (ctlr->chip->cfg2 & SII4CH) {
4300 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
4304 /* dont block interrupts from any channel */
4305 pci_write_config(dev, 0x48,
4306 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
4308 /* enable PCI interrupt as BIOS might not */
4309 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
4311 ctlr->allocate = ata_sii_allocate;
4312 if (ctlr->chip->max_dma >= ATA_SA150) {
4313 ctlr->reset = ata_sii_reset;
4314 ctlr->setmode = ata_sata_setmode;
4317 ctlr->setmode = ata_sii_setmode;
4321 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
4322 device_printf(dev, "HW has secondary channel disabled\n");
4326 /* enable interrupt as BIOS might not */
4327 pci_write_config(dev, 0x71, 0x01, 1);
4329 ctlr->allocate = ata_cmd_allocate;
4330 ctlr->setmode = ata_cmd_setmode;
4337 ata_cmd_allocate(device_t dev)
4339 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4340 struct ata_channel *ch = device_get_softc(dev);
4342 /* setup the usual register normal pci style */
4343 if (ata_pci_allocate(dev))
4346 if (ctlr->chip->cfg2 & SIIINTR)
4347 ch->hw.status = ata_cmd_status;
4353 ata_cmd_status(device_t dev)
4355 struct ata_channel *ch = device_get_softc(dev);
4358 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
4359 (ch->unit ? 0x08 : 0x04))) {
4360 pci_write_config(device_get_parent(ch->dev), 0x71,
4361 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
4362 return ata_pci_status(dev);
4368 ata_cmd_setmode(device_t dev, int mode)
4370 device_t gparent = GRANDPARENT(dev);
4371 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4372 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4373 struct ata_device *atadev = device_get_softc(dev);
4374 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4377 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4379 mode = ata_check_80pin(dev, mode);
4381 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4384 device_printf(dev, "%ssetting %s on %s chip\n",
4385 (error) ? "FAILURE " : "",
4386 ata_mode2str(mode), ctlr->chip->text);
4388 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
4389 int ureg = ch->unit ? 0x7b : 0x73;
4391 if (mode >= ATA_UDMA0) {
4392 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
4393 { 0x11, 0x42 }, { 0x25, 0x8a },
4394 { 0x15, 0x4a }, { 0x05, 0x0a } };
4396 u_int8_t umode = pci_read_config(gparent, ureg, 1);
4398 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
4399 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
4400 pci_write_config(gparent, ureg, umode, 1);
4402 else if (mode >= ATA_WDMA0) {
4403 int dmatimings[] = { 0x87, 0x32, 0x3f };
4405 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
4406 pci_write_config(gparent, ureg,
4407 pci_read_config(gparent, ureg, 1) &
4408 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4411 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
4412 pci_write_config(gparent, treg,
4413 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
4414 pci_write_config(gparent, ureg,
4415 pci_read_config(gparent, ureg, 1) &
4416 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4418 atadev->mode = mode;
4423 ata_sii_allocate(device_t dev)
4425 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4426 struct ata_channel *ch = device_get_softc(dev);
4427 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
4430 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
4431 ch->r_io[i].res = ctlr->r_res2;
4432 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
4434 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
4435 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
4436 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
4437 ata_default_registers(dev);
4439 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
4440 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
4441 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
4442 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
4443 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
4444 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
4446 if (ctlr->chip->max_dma >= ATA_SA150) {
4447 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4448 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
4449 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4450 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
4451 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4452 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
4453 ch->flags |= ATA_NO_SLAVE;
4455 /* enable PHY state change interrupt */
4456 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
4459 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
4460 /* work around errata in early chips */
4461 ch->dma->boundary = 16 * DEV_BSIZE;
4462 ch->dma->segsize = 15 * DEV_BSIZE;
4466 ch->hw.status = ata_sii_status;
4471 ata_sii_status(device_t dev)
4473 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4474 struct ata_channel *ch = device_get_softc(dev);
4475 int offset0 = ((ch->unit & 1) << 3) + ((ch->unit & 2) << 8);
4476 int offset1 = ((ch->unit & 1) << 6) + ((ch->unit & 2) << 8);
4478 /* do we have any PHY events ? */
4479 if (ctlr->chip->max_dma >= ATA_SA150 &&
4480 (ATA_INL(ctlr->r_res2, 0x10 + offset0) & 0x00000010))
4481 ata_sata_phy_check_events(dev);
4483 if (ATA_INL(ctlr->r_res2, 0xa0 + offset1) & 0x00000800)
4484 return ata_pci_status(dev);
4490 ata_sii_reset(device_t dev)
4492 if (ata_sata_phy_reset(dev))
4493 ata_generic_reset(dev);
4497 ata_sii_setmode(device_t dev, int mode)
4499 device_t gparent = GRANDPARENT(dev);
4500 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4501 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4502 struct ata_device *atadev = device_get_softc(dev);
4503 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
4504 int mreg = ch->unit ? 0x84 : 0x80;
4505 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
4506 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
4509 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4511 if (ctlr->chip->cfg2 & SIISETCLK) {
4512 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
4513 (ch->unit ? 0x02 : 0x01))) {
4514 ata_print_cable(dev, "controller");
4519 mode = ata_check_80pin(dev, mode);
4521 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4524 device_printf(dev, "%ssetting %s on %s chip\n",
4525 (error) ? "FAILURE " : "",
4526 ata_mode2str(mode), ctlr->chip->text);
4530 if (mode >= ATA_UDMA0) {
4531 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
4532 u_int8_t ureg = 0xac + rego;
4534 pci_write_config(gparent, mreg,
4535 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
4536 pci_write_config(gparent, ureg,
4537 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
4538 udmatimings[mode & ATA_MODE_MASK], 1);
4541 else if (mode >= ATA_WDMA0) {
4542 u_int8_t dreg = 0xa8 + rego;
4543 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
4545 pci_write_config(gparent, mreg,
4546 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
4547 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
4551 u_int8_t preg = 0xa4 + rego;
4552 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
4554 pci_write_config(gparent, mreg,
4555 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
4556 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
4558 atadev->mode = mode;
4562 struct ata_siiprb_dma_prdentry {
4568 struct ata_siiprb_ata_command {
4569 u_int32_t reserved0;
4570 struct ata_siiprb_dma_prdentry prd[126];
4573 struct ata_siiprb_atapi_command {
4575 struct ata_siiprb_dma_prdentry prd[125];
4578 struct ata_siiprb_command {
4580 u_int16_t protocol_override;
4581 u_int32_t transfer_count;
4584 struct ata_siiprb_ata_command ata;
4585 struct ata_siiprb_atapi_command atapi;
4590 ata_siiprb_allocate(device_t dev)
4592 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4593 struct ata_channel *ch = device_get_softc(dev);
4594 int offset = ch->unit * 0x2000;
4596 /* set the SATA resources */
4597 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4598 ch->r_io[ATA_SSTATUS].offset = 0x1f04 + offset;
4599 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4600 ch->r_io[ATA_SERROR].offset = 0x1f08 + offset;
4601 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4602 ch->r_io[ATA_SCONTROL].offset = 0x1f00 + offset;
4603 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
4604 ch->r_io[ATA_SACTIVE].offset = 0x1f0c + offset;
4606 ch->hw.begin_transaction = ata_siiprb_begin_transaction;
4607 ch->hw.end_transaction = ata_siiprb_end_transaction;
4608 ch->hw.status = ata_siiprb_status;
4609 ch->hw.command = NULL; /* not used here */
4614 ata_siiprb_status(device_t dev)
4616 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4617 struct ata_channel *ch = device_get_softc(dev);
4618 int offset = ch->unit * 0x2000;
4620 if ((ATA_INL(ctlr->r_res1, 0x0044) & (1 << ch->unit))) {
4621 u_int32_t istatus = ATA_INL(ctlr->r_res2, 0x1008 + offset);
4623 /* do we have any PHY events ? */
4624 ata_sata_phy_check_events(dev);
4626 /* clear interrupt(s) */
4627 ATA_OUTL(ctlr->r_res2, 0x1008 + offset, istatus);
4629 /* do we have any device action ? */
4630 return (istatus & 0x00000001);
4636 ata_siiprb_begin_transaction(struct ata_request *request)
4638 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
4639 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4640 struct ata_siiprb_command *prb;
4641 int offset = ch->unit * 0x2000;
4645 /* check for 48 bit access and convert if needed */
4646 ata_modify_if_48bit(request);
4648 /* get a piece of the workspace for this request */
4649 prb = (struct ata_siiprb_command *)
4650 (ch->dma->work + (sizeof(struct ata_siiprb_command) * tag));
4652 /* set basic prd options ata/atapi etc etc */
4653 bzero(prb, sizeof(struct ata_siiprb_command));
4655 /* setup the FIS for this request */
4656 if (!ata_request2fis_h2d(request, &prb->fis[0])) {
4657 device_printf(request->dev, "setting up SATA FIS failed\n");
4658 request->result = EIO;
4659 return ATA_OP_FINISHED;
4662 /* if request moves data setup and load SG list */
4663 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
4664 struct ata_siiprb_dma_prdentry *prd;
4666 if (request->flags & ATA_R_ATAPI)
4667 prd = &prb->u.atapi.prd[0];
4669 prd = &prb->u.ata.prd[0];
4670 if (ch->dma->load(ch->dev, request->data, request->bytecount,
4671 request->flags & ATA_R_READ, prd, &dummy)) {
4672 device_printf(request->dev, "setting up DMA failed\n");
4673 request->result = EIO;
4674 return ATA_OP_FINISHED;
4678 /* activate the prb */
4679 prb_bus = ch->dma->work_bus + (sizeof(struct ata_siiprb_command) * tag);
4680 ATA_OUTL(ctlr->r_res2,
4681 0x1c00 + offset + (tag * sizeof(u_int64_t)), prb_bus);
4682 ATA_OUTL(ctlr->r_res2,
4683 0x1c04 + offset + (tag * sizeof(u_int64_t)), prb_bus>>32);
4685 /* start the timeout */
4686 callout_reset(&request->callout, request->timeout * hz,
4687 (timeout_t*)ata_timeout, request);
4688 return ATA_OP_CONTINUES;
4692 ata_siiprb_end_transaction(struct ata_request *request)
4694 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
4695 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4696 struct ata_siiprb_command *prb;
4697 int offset = ch->unit * 0x2000;
4700 /* kill the timeout */
4701 callout_stop(&request->callout);
4703 prb = (struct ata_siiprb_command *)
4704 ((u_int8_t *)rman_get_virtual(ctlr->r_res2) + (tag << 7) + offset);
4706 /* if error status get details */
4707 request->status = prb->fis[2];
4708 if (request->status & ATA_S_ERROR)
4709 request->error = prb->fis[3];
4711 /* update progress */
4712 if (!(request->status & ATA_S_ERROR) && !(request->flags & ATA_R_TIMEOUT)) {
4713 if (request->flags & ATA_R_READ)
4714 request->donecount = prb->transfer_count;
4716 request->donecount = request->bytecount;
4719 /* any controller errors flagged ? */
4720 if ((error = ATA_INL(ctlr->r_res2, 0x1024 + offset))) {
4721 printf("ata_siiprb_end_transaction %s error=%08x\n",
4722 ata_cmd2str(request), error);
4725 /* release SG list etc */
4726 ch->dma->unload(ch->dev);
4728 return ATA_OP_FINISHED;
4732 ata_siiprb_reset(device_t dev)
4734 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4735 struct ata_channel *ch = device_get_softc(dev);
4736 int offset = ch->unit * 0x2000;
4737 struct ata_siiprb_command *prb;
4739 u_int32_t status, signature;
4740 int timeout, tag = 0;
4742 /* reset channel HW */
4743 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000001);
4745 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000001);
4748 /* poll for channel ready */
4749 for (timeout = 0; timeout < 1000; timeout++) {
4750 if ((status = ATA_INL(ctlr->r_res2, 0x1000 + offset)) & 0x00040000)
4754 if (timeout >= 1000) {
4755 device_printf(ch->dev, "channel HW reset timeout reset failure\n");
4760 device_printf(ch->dev, "channel HW reset time=%dms\n", timeout * 1);
4763 if (!ata_sata_phy_reset(dev)) {
4765 device_printf(ch->dev, "phy reset found no device\n");
4770 /* get a piece of the workspace for a soft reset request */
4771 prb = (struct ata_siiprb_command *)
4772 (ch->dma->work + (sizeof(struct ata_siiprb_command) * tag));
4773 bzero(prb, sizeof(struct ata_siiprb_command));
4774 prb->control = htole16(0x0080);
4776 /* activate the soft reset prb */
4777 prb_bus = ch->dma->work_bus + (sizeof(struct ata_siiprb_command) * tag);
4778 ATA_OUTL(ctlr->r_res2,
4779 0x1c00 + offset + (tag * sizeof(u_int64_t)), prb_bus);
4780 ATA_OUTL(ctlr->r_res2,
4781 0x1c04 + offset + (tag * sizeof(u_int64_t)), prb_bus>>32);
4783 /* poll for channel ready */
4784 for (timeout = 0; timeout < 1000; timeout++) {
4786 if ((status = ATA_INL(ctlr->r_res2, 0x1008 + offset)) & 0x00010000)
4789 if (timeout >= 1000) {
4790 device_printf(ch->dev, "reset timeout - no device found\n");
4795 device_printf(ch->dev, "soft reset exec time=%dms status=%08x\n",
4798 /* find out whats there */
4799 prb = (struct ata_siiprb_command *)
4800 ((u_int8_t *)rman_get_virtual(ctlr->r_res2) + (tag << 7) + offset);
4802 prb->fis[12]|(prb->fis[4]<<8)|(prb->fis[5]<<16)|(prb->fis[6]<<24);
4804 device_printf(ch->dev, "signature=%08x\n", signature);
4805 switch (signature) {
4807 ch->devices = ATA_ATAPI_MASTER;
4808 device_printf(ch->dev, "SATA ATAPI devices not supported yet\n");
4812 ch->devices = ATA_PORTMULTIPLIER;
4813 device_printf(ch->dev, "Portmultipliers not supported yet\n");
4817 ch->devices = ATA_ATA_MASTER;
4824 /* clear interrupt(s) */
4825 ATA_OUTL(ctlr->r_res2, 0x1008 + offset, 0x000008ff);
4827 /* require explicit interrupt ack */
4828 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000008);
4831 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000400);
4833 /* enable interrupts wanted */
4834 ATA_OUTL(ctlr->r_res2, 0x1010 + offset, 0x000000ff);
4838 ata_siiprb_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
4840 struct ata_dmasetprd_args *args = xsc;
4841 struct ata_siiprb_dma_prdentry *prd = args->dmatab;
4844 if ((args->error = error))
4847 for (i = 0; i < nsegs; i++) {
4848 prd[i].addr = htole64(segs[i].ds_addr);
4849 prd[i].count = htole32(segs[i].ds_len);
4851 prd[i - 1].control = htole32(ATA_DMA_EOT);
4855 ata_siiprb_dmainit(device_t dev)
4857 struct ata_channel *ch = device_get_softc(dev);
4861 /* note start and stop are not used here */
4862 ch->dma->setprd = ata_siiprb_dmasetprd;
4863 ch->dma->max_address = BUS_SPACE_MAXADDR;
4869 * Silicon Integrated Systems Corp. (SiS) chipset support functions
4872 ata_sis_ident(device_t dev)
4874 struct ata_pci_controller *ctlr = device_get_softc(dev);
4875 struct ata_chip_id *idx;
4876 static struct ata_chip_id ids[] =
4877 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "182" }, /* south */
4878 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "181" }, /* south */
4879 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "180" }, /* south */
4880 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "965" }, /* south */
4881 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "964" }, /* south */
4882 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "963" }, /* south */
4883 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "962" }, /* south */
4885 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "745" }, /* 1chip */
4886 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "735" }, /* 1chip */
4887 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "733" }, /* 1chip */
4888 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "730" }, /* 1chip */
4890 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "635" }, /* 1chip */
4891 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "633" }, /* unknown */
4892 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "630S"}, /* 1chip */
4893 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "630" }, /* 1chip */
4894 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "620" }, /* 1chip */
4896 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "550" },
4897 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "540" },
4898 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "530" },
4900 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "5513" },
4901 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "5513" },
4902 { 0, 0, 0, 0, 0, 0 }};
4906 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
4909 if (idx->cfg2 && !found) {
4910 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
4912 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
4913 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
4915 idx->cfg1 = SIS133NEW;
4916 idx->max_dma = ATA_UDMA6;
4917 sprintf(buffer, "SiS 962/963 %s controller",
4918 ata_mode2str(idx->max_dma));
4920 pci_write_config(dev, 0x57, reg57, 1);
4922 if (idx->cfg2 && !found) {
4923 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
4925 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
4926 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
4927 struct ata_chip_id id[] =
4928 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
4931 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
4932 idx->cfg1 = SIS133OLD;
4933 idx->max_dma = ATA_UDMA6;
4936 idx->cfg1 = SIS100NEW;
4937 idx->max_dma = ATA_UDMA5;
4939 sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
4941 pci_write_config(dev, 0x4a, reg4a, 1);
4944 sprintf(buffer,"SiS %s %s controller",
4945 idx->text, ata_mode2str(idx->max_dma));
4947 device_set_desc_copy(dev, buffer);
4949 ctlr->chipinit = ata_sis_chipinit;
4954 ata_sis_chipinit(device_t dev)
4956 struct ata_pci_controller *ctlr = device_get_softc(dev);
4958 if (ata_setup_interrupt(dev))
4961 switch (ctlr->chip->cfg1) {
4966 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
4970 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
4973 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
4974 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
4977 ctlr->r_type2 = SYS_RES_IOPORT;
4978 ctlr->r_rid2 = PCIR_BAR(5);
4979 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4980 &ctlr->r_rid2, RF_ACTIVE))) {
4981 ctlr->allocate = ata_sis_allocate;
4982 ctlr->reset = ata_sis_reset;
4984 /* enable PCI interrupt */
4985 pci_write_config(dev, PCIR_COMMAND,
4986 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
4988 ctlr->setmode = ata_sata_setmode;
4993 ctlr->setmode = ata_sis_setmode;
4998 ata_sis_allocate(device_t dev)
5000 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5001 struct ata_channel *ch = device_get_softc(dev);
5002 int offset = ch->unit << ((ctlr->chip->chipid == ATA_SIS182) ? 5 : 6);
5004 /* setup the usual register normal pci style */
5005 if (ata_pci_allocate(dev))
5008 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
5009 ch->r_io[ATA_SSTATUS].offset = 0x00 + offset;
5010 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
5011 ch->r_io[ATA_SERROR].offset = 0x04 + offset;
5012 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
5013 ch->r_io[ATA_SCONTROL].offset = 0x08 + offset;
5014 ch->flags |= ATA_NO_SLAVE;
5016 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
5017 /* XXX SOS unknown how to enable PHY state change interrupt */
5022 ata_sis_reset(device_t dev)
5024 if (ata_sata_phy_reset(dev))
5025 ata_generic_reset(dev);
5029 ata_sis_setmode(device_t dev, int mode)
5031 device_t gparent = GRANDPARENT(dev);
5032 struct ata_pci_controller *ctlr = device_get_softc(gparent);
5033 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5034 struct ata_device *atadev = device_get_softc(dev);
5035 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
5038 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
5040 if (ctlr->chip->cfg1 == SIS133NEW) {
5041 if (mode > ATA_UDMA2 &&
5042 pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
5043 ata_print_cable(dev, "controller");
5048 if (mode > ATA_UDMA2 &&
5049 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
5050 ata_print_cable(dev, "controller");
5055 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5058 device_printf(dev, "%ssetting %s on %s chip\n",
5059 (error) ? "FAILURE " : "",
5060 ata_mode2str(mode), ctlr->chip->text);
5062 switch (ctlr->chip->cfg1) {
5064 u_int32_t timings[] =
5065 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
5066 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
5067 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
5070 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
5071 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
5075 u_int16_t timings[] =
5076 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
5077 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
5079 u_int16_t reg = 0x40 + (devno << 1);
5081 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5085 u_int16_t timings[] =
5086 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
5087 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
5088 u_int16_t reg = 0x40 + (devno << 1);
5090 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5096 u_int16_t timings[] =
5097 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
5098 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
5099 u_int16_t reg = 0x40 + (devno << 1);
5101 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5105 atadev->mode = mode;
5110 /* VIA Technologies Inc. chipset support functions */
5112 ata_via_ident(device_t dev)
5114 struct ata_pci_controller *ctlr = device_get_softc(dev);
5115 struct ata_chip_id *idx;
5116 static struct ata_chip_id ids[] =
5117 {{ ATA_VIA82C586, 0x02, VIA33, 0x00, ATA_UDMA2, "82C586B" },
5118 { ATA_VIA82C586, 0x00, VIA33, 0x00, ATA_WDMA2, "82C586" },
5119 { ATA_VIA82C596, 0x12, VIA66, VIACLK, ATA_UDMA4, "82C596B" },
5120 { ATA_VIA82C596, 0x00, VIA33, 0x00, ATA_UDMA2, "82C596" },
5121 { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "82C686B"},
5122 { ATA_VIA82C686, 0x10, VIA66, VIACLK, ATA_UDMA4, "82C686A" },
5123 { ATA_VIA82C686, 0x00, VIA33, 0x00, ATA_UDMA2, "82C686" },
5124 { ATA_VIA8231, 0x00, VIA100, VIABUG, ATA_UDMA5, "8231" },
5125 { ATA_VIA8233, 0x00, VIA100, 0x00, ATA_UDMA5, "8233" },
5126 { ATA_VIA8233C, 0x00, VIA100, 0x00, ATA_UDMA5, "8233C" },
5127 { ATA_VIA8233A, 0x00, VIA133, 0x00, ATA_UDMA6, "8233A" },
5128 { ATA_VIA8235, 0x00, VIA133, 0x00, ATA_UDMA6, "8235" },
5129 { ATA_VIA8237, 0x00, VIA133, 0x00, ATA_UDMA6, "8237" },
5130 { ATA_VIA8237A, 0x00, VIA133, 0x00, ATA_UDMA6, "8237A" },
5131 { ATA_VIA8237S, 0x00, VIA133, 0x00, ATA_UDMA6, "8237S" },
5132 { ATA_VIA8251, 0x00, VIA133, 0x00, ATA_UDMA6, "8251" },
5133 { 0, 0, 0, 0, 0, 0 }};
5134 static struct ata_chip_id new_ids[] =
5135 {{ ATA_VIA6410, 0x00, 0, 0x00, ATA_UDMA6, "6410" },
5136 { ATA_VIA6420, 0x00, 7, 0x00, ATA_SA150, "6420" },
5137 { ATA_VIA6421, 0x00, 6, VIABAR, ATA_SA150, "6421" },
5138 { ATA_VIA8237A, 0x00, 7, 0x00, ATA_SA150, "8237A" },
5139 { ATA_VIA8237S, 0x00, 7, 0x00, ATA_SA150, "8237S" },
5140 { ATA_VIA8251, 0x00, 0, VIAAHCI, ATA_SA300, "8251" },
5141 { 0, 0, 0, 0, 0, 0 }};
5144 if (pci_get_devid(dev) == ATA_VIA82C571) {
5145 if (!(idx = ata_find_chip(dev, ids, -99)))
5149 if (!(idx = ata_match_chip(dev, new_ids)))
5153 sprintf(buffer, "VIA %s %s controller",
5154 idx->text, ata_mode2str(idx->max_dma));
5155 device_set_desc_copy(dev, buffer);
5157 ctlr->chipinit = ata_via_chipinit;
5162 ata_via_chipinit(device_t dev)
5164 struct ata_pci_controller *ctlr = device_get_softc(dev);
5166 if (ata_setup_interrupt(dev))
5169 if (ctlr->chip->max_dma >= ATA_SA150) {
5170 if (ctlr->chip->cfg2 == VIAAHCI) {
5171 ctlr->r_type2 = SYS_RES_MEMORY;
5172 ctlr->r_rid2 = PCIR_BAR(5);
5173 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
5176 return ata_ahci_chipinit(dev);
5179 ctlr->r_type2 = SYS_RES_IOPORT;
5180 ctlr->r_rid2 = PCIR_BAR(5);
5181 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
5182 &ctlr->r_rid2, RF_ACTIVE))) {
5183 ctlr->allocate = ata_via_allocate;
5184 ctlr->reset = ata_via_reset;
5186 /* enable PCI interrupt */
5187 pci_write_config(dev, PCIR_COMMAND,
5188 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
5191 if (ctlr->chip->cfg2 & VIABAR) {
5193 ctlr->setmode = ata_via_setmode;
5196 ctlr->setmode = ata_sata_setmode;
5200 /* prepare for ATA-66 on the 82C686a and 82C596b */
5201 if (ctlr->chip->cfg2 & VIACLK)
5202 pci_write_config(dev, 0x50, 0x030b030b, 4);
5204 /* the southbridge might need the data corruption fix */
5205 if (ctlr->chip->cfg2 & VIABUG)
5206 ata_via_southbridge_fixup(dev);
5208 /* set fifo configuration half'n'half */
5209 pci_write_config(dev, 0x43,
5210 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
5212 /* set status register read retry */
5213 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
5215 /* set DMA read & end-of-sector fifo flush */
5216 pci_write_config(dev, 0x46,
5217 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
5219 /* set sector size */
5220 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
5221 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
5223 ctlr->setmode = ata_via_family_setmode;
5228 ata_via_allocate(device_t dev)
5230 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5231 struct ata_channel *ch = device_get_softc(dev);
5233 /* newer SATA chips has resources in one BAR for each channel */
5234 if (ctlr->chip->cfg2 & VIABAR) {
5235 struct resource *r_io;
5238 rid = PCIR_BAR(ch->unit);
5239 if (!(r_io = bus_alloc_resource_any(device_get_parent(dev),
5244 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
5245 ch->r_io[i].res = r_io;
5246 ch->r_io[i].offset = i;
5248 ch->r_io[ATA_CONTROL].res = r_io;
5249 ch->r_io[ATA_CONTROL].offset = 2 + ATA_IOSIZE;
5250 ch->r_io[ATA_IDX_ADDR].res = r_io;
5251 ata_default_registers(dev);
5252 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
5253 ch->r_io[i].res = ctlr->r_res1;
5254 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
5261 /* setup the usual register normal pci style */
5262 if (ata_pci_allocate(dev))
5266 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
5267 ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
5268 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
5269 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
5270 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
5271 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
5272 ch->flags |= ATA_NO_SLAVE;
5274 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
5275 /* XXX SOS unknown how to enable PHY state change interrupt */
5280 ata_via_reset(device_t dev)
5282 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5283 struct ata_channel *ch = device_get_softc(dev);
5285 if ((ctlr->chip->cfg2 & VIABAR) && (ch->unit > 1))
5286 ata_generic_reset(dev);
5288 if (ata_sata_phy_reset(dev))
5289 ata_generic_reset(dev);
5293 ata_via_setmode(device_t dev, int mode)
5295 device_t gparent = GRANDPARENT(dev);
5296 struct ata_pci_controller *ctlr = device_get_softc(gparent);
5297 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5298 struct ata_device *atadev = device_get_softc(dev);
5301 if ((ctlr->chip->cfg2 & VIABAR) && (ch->unit > 1)) {
5302 u_int8_t pio_timings[] = { 0xa8, 0x65, 0x65, 0x32, 0x20,
5304 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
5305 u_int8_t dma_timings[] = { 0xee, 0xe8, 0xe6, 0xe4, 0xe2, 0xe1, 0xe0 };
5307 mode = ata_check_80pin(dev, ata_limit_mode(dev, mode, ATA_UDMA6));
5308 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5310 device_printf(dev, "%ssetting %s on %s chip\n",
5311 (error) ? "FAILURE " : "", ata_mode2str(mode),
5314 pci_write_config(gparent, 0xab, pio_timings[ata_mode2idx(mode)], 1);
5315 if (mode >= ATA_UDMA0)
5316 pci_write_config(gparent, 0xb3,
5317 dma_timings[mode & ATA_MODE_MASK], 1);
5318 atadev->mode = mode;
5322 ata_sata_setmode(dev, mode);
5326 ata_via_southbridge_fixup(device_t dev)
5331 if (device_get_children(device_get_parent(dev), &children, &nchildren))
5334 for (i = 0; i < nchildren; i++) {
5335 if (pci_get_devid(children[i]) == ATA_VIA8363 ||
5336 pci_get_devid(children[i]) == ATA_VIA8371 ||
5337 pci_get_devid(children[i]) == ATA_VIA8662 ||
5338 pci_get_devid(children[i]) == ATA_VIA8361) {
5339 u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
5341 if ((reg76 & 0xf0) != 0xd0) {
5343 "Correcting VIA config for southbridge data corruption bug\n");
5344 pci_write_config(children[i], 0x75, 0x80, 1);
5345 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
5350 free(children, M_TEMP);
5354 /* common code for VIA, AMD & nVidia */
5356 ata_via_family_setmode(device_t dev, int mode)
5358 device_t gparent = GRANDPARENT(dev);
5359 struct ata_pci_controller *ctlr = device_get_softc(gparent);
5360 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5361 struct ata_device *atadev = device_get_softc(dev);
5362 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
5363 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
5365 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* VIA ATA33 */
5366 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* VIA ATA66 */
5367 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* VIA ATA100 */
5368 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
5369 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }}; /* AMD/nVIDIA */
5370 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
5371 int reg = 0x53 - devno;
5374 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
5376 if (ctlr->chip->cfg2 & AMDCABLE) {
5377 if (mode > ATA_UDMA2 &&
5378 !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
5379 ata_print_cable(dev, "controller");
5384 mode = ata_check_80pin(dev, mode);
5386 if (ctlr->chip->cfg2 & NVIDIA)
5389 if (ctlr->chip->cfg1 != VIA133)
5390 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
5392 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5395 device_printf(dev, "%ssetting %s on %s chip\n",
5396 (error) ? "FAILURE " : "", ata_mode2str(mode),
5399 if (mode >= ATA_UDMA0)
5400 pci_write_config(gparent, reg,
5401 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
5403 pci_write_config(gparent, reg, 0x8b, 1);
5404 atadev->mode = mode;
5409 /* misc functions */
5410 static struct ata_chip_id *
5411 ata_match_chip(device_t dev, struct ata_chip_id *index)
5413 while (index->chipid != 0) {
5414 if (pci_get_devid(dev) == index->chipid &&
5415 pci_get_revid(dev) >= index->chiprev)
5422 static struct ata_chip_id *
5423 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
5428 if (device_get_children(device_get_parent(dev), &children, &nchildren))
5431 while (index->chipid != 0) {
5432 for (i = 0; i < nchildren; i++) {
5433 if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
5434 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
5435 pci_get_devid(children[i]) == index->chipid &&
5436 pci_get_revid(children[i]) >= index->chiprev) {
5437 free(children, M_TEMP);
5443 free(children, M_TEMP);
5448 ata_setup_interrupt(device_t dev)
5450 struct ata_pci_controller *ctlr = device_get_softc(dev);
5451 int rid = ATA_IRQ_RID;
5453 if (!ata_legacy(dev)) {
5454 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
5455 RF_SHAREABLE | RF_ACTIVE))) {
5456 device_printf(dev, "unable to map interrupt\n");
5459 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
5460 ata_generic_intr, ctlr, &ctlr->handle))) {
5461 device_printf(dev, "unable to setup interrupt\n");
5468 struct ata_serialize {
5469 struct mtx locked_mtx;
5475 ata_serialize(device_t dev, int flags)
5477 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5478 struct ata_channel *ch = device_get_softc(dev);
5479 struct ata_serialize *serial;
5480 static int inited = 0;
5484 serial = malloc(sizeof(struct ata_serialize),
5485 M_TEMP, M_NOWAIT | M_ZERO);
5486 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
5487 serial->locked_ch = -1;
5488 serial->restart_ch = -1;
5489 device_set_ivars(ctlr->dev, serial);
5493 serial = device_get_ivars(ctlr->dev);
5495 mtx_lock(&serial->locked_mtx);
5498 if (serial->locked_ch == -1)
5499 serial->locked_ch = ch->unit;
5500 if (serial->locked_ch != ch->unit)
5501 serial->restart_ch = ch->unit;
5505 if (serial->locked_ch == ch->unit) {
5506 serial->locked_ch = -1;
5507 if (serial->restart_ch != -1) {
5508 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
5509 serial->restart_ch = -1;
5510 mtx_unlock(&serial->locked_mtx);
5521 res = serial->locked_ch;
5522 mtx_unlock(&serial->locked_mtx);
5527 ata_print_cable(device_t dev, u_int8_t *who)
5530 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
5534 ata_atapi(device_t dev)
5536 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5537 struct ata_device *atadev = device_get_softc(dev);
5539 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
5540 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
5544 ata_check_80pin(device_t dev, int mode)
5546 struct ata_device *atadev = device_get_softc(dev);
5548 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
5549 ata_print_cable(dev, "device");
5556 ata_mode2idx(int mode)
5558 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
5559 return (mode & ATA_MODE_MASK) + 8;
5560 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
5561 return (mode & ATA_MODE_MASK) + 5;
5562 return (mode & ATA_MODE_MASK) - ATA_PIO0;