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) &
325 ATA_SC_DET_MASK) == ATA_SC_DET_RESET)
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_DMA)
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 (request->flags & ATA_R_ATAPI)
624 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
625 ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) |
626 ATA_AHCI_P_CMD_ATAPI);
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 the command */
633 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CI + offset, (1 << tag));
635 /* start the timeout */
636 callout_reset(&request->callout, request->timeout * hz,
637 (timeout_t*)ata_timeout, request);
638 return ATA_OP_CONTINUES;
641 /* must be called with ATA channel locked and state_mtx held */
643 ata_ahci_end_transaction(struct ata_request *request)
645 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
646 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
647 struct ata_ahci_cmd_list *clp;
649 int offset = ch->unit << 7;
652 /* kill the timeout */
653 callout_stop(&request->callout);
656 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + offset);
657 request->status = tf_data;
659 /* if error status get details */
660 if (request->status & ATA_S_ERROR)
661 request->error = tf_data >> 8;
663 /* record how much data we actually moved */
664 clp = (struct ata_ahci_cmd_list *)
665 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
666 request->donecount = clp->bytecount;
668 /* release SG list etc */
669 ch->dma->unload(ch->dev);
671 return ATA_OP_FINISHED;
675 ata_ahci_reset(device_t dev)
677 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
678 struct ata_channel *ch = device_get_softc(dev);
680 int offset = ch->unit << 7;
683 if (!(ATA_INL(ctlr->r_res2, ATA_AHCI_PI) & (1 << ch->unit))) {
684 device_printf(dev, "port not implemented\n");
689 /* kill off all activity on this channel */
690 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
691 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
692 cmd & ~(ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
694 /* XXX SOS this is not entirely wrong */
699 device_printf(dev, "stopping AHCI engine failed\n");
702 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) & ATA_AHCI_P_CMD_CR);
704 /* issue Command List Override if supported */
705 if (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_CAP_CLO) {
706 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
707 cmd |= ATA_AHCI_P_CMD_CLO;
708 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, cmd);
713 device_printf(dev, "executing CLO failed\n");
716 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD+offset)&ATA_AHCI_P_CMD_CLO);
720 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, ATA_AHCI_P_CMD_SUD);
722 /* enable interface */
723 if (ata_sata_phy_reset(dev)) {
724 switch (ATA_INL(ctlr->r_res2, ATA_AHCI_P_SIG + offset)) {
726 ch->devices = ATA_ATAPI_MASTER;
727 device_printf(ch->dev, "SATA ATAPI devices not supported yet\n");
731 ch->devices = ATA_PORTMULTIPLIER;
732 device_printf(ch->dev, "Portmultipliers not supported yet\n");
736 ch->devices = ATA_ATA_MASTER;
741 /* clear any interrupts pending on this channel */
742 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset,
743 ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset));
745 /* start operations on this channel */
746 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
747 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
748 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
752 ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
754 struct ata_dmasetprd_args *args = xsc;
755 struct ata_ahci_dma_prd *prd = args->dmatab;
758 if (!(args->error = error)) {
759 for (i = 0; i < nsegs; i++) {
760 prd[i].dba = htole64(segs[i].ds_addr);
761 prd[i].dbc = htole32((segs[i].ds_len - 1) & ATA_AHCI_PRD_MASK);
768 ata_ahci_dmainit(device_t dev)
770 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
771 struct ata_channel *ch = device_get_softc(dev);
775 /* note start and stop are not used here */
776 ch->dma->setprd = ata_ahci_dmasetprd;
777 ch->dma->max_iosize = 8192 * DEV_BSIZE;
778 if (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_CAP_64BIT)
779 ch->dma->max_address = BUS_SPACE_MAXADDR;
784 ata_ahci_setup_fis(struct ata_ahci_cmd_tab *ctp, struct ata_request *request)
786 bzero(ctp->cfis, 64);
787 if (request->flags & ATA_R_ATAPI) {
788 bzero(ctp->acmd, 32);
789 bcopy(request->u.atapi.ccb, ctp->acmd, 12);
791 return ata_request2fis_h2d(request, &ctp->cfis[0]);
796 * Acard chipset support functions
799 ata_acard_ident(device_t dev)
801 struct ata_pci_controller *ctlr = device_get_softc(dev);
802 struct ata_chip_id *idx;
803 static struct ata_chip_id ids[] =
804 {{ ATA_ATP850R, 0, ATPOLD, 0x00, ATA_UDMA2, "ATP850" },
805 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "ATP860A" },
806 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "ATP860R" },
807 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "ATP865A" },
808 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "ATP865R" },
809 { 0, 0, 0, 0, 0, 0}};
812 if (!(idx = ata_match_chip(dev, ids)))
815 sprintf(buffer, "Acard %s %s controller",
816 idx->text, ata_mode2str(idx->max_dma));
817 device_set_desc_copy(dev, buffer);
819 ctlr->chipinit = ata_acard_chipinit;
824 ata_acard_chipinit(device_t dev)
826 struct ata_pci_controller *ctlr = device_get_softc(dev);
828 if (ata_setup_interrupt(dev))
831 ctlr->allocate = ata_acard_allocate;
832 if (ctlr->chip->cfg1 == ATPOLD) {
833 ctlr->setmode = ata_acard_850_setmode;
834 ctlr->locking = ata_serialize;
837 ctlr->setmode = ata_acard_86X_setmode;
842 ata_acard_allocate(device_t dev)
844 struct ata_channel *ch = device_get_softc(dev);
846 /* setup the usual register normal pci style */
847 if (ata_pci_allocate(dev))
850 ch->hw.status = ata_acard_status;
855 ata_acard_status(device_t dev)
857 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
858 struct ata_channel *ch = device_get_softc(dev);
860 if (ctlr->chip->cfg1 == ATPOLD &&
861 ATA_LOCKING(ch->dev, ATA_LF_WHICH) != ch->unit)
863 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
864 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
866 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
867 ATA_BMSTAT_INTERRUPT)
869 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
871 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
872 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
875 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
877 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
884 ata_acard_850_setmode(device_t dev, int mode)
886 device_t gparent = GRANDPARENT(dev);
887 struct ata_pci_controller *ctlr = device_get_softc(gparent);
888 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
889 struct ata_device *atadev = device_get_softc(dev);
890 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
893 mode = ata_limit_mode(dev, mode,
894 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
896 /* XXX SOS missing WDMA0+1 + PIO modes */
897 if (mode >= ATA_WDMA2) {
898 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
900 device_printf(dev, "%ssetting %s on %s chip\n",
901 (error) ? "FAILURE " : "",
902 ata_mode2str(mode), ctlr->chip->text);
904 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
906 reg54 &= ~(0x03 << (devno << 1));
907 if (mode >= ATA_UDMA0)
908 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
909 pci_write_config(gparent, 0x54, reg54, 1);
910 pci_write_config(gparent, 0x4a, 0xa6, 1);
911 pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
916 /* we could set PIO mode timings, but we assume the BIOS did that */
920 ata_acard_86X_setmode(device_t dev, int mode)
922 device_t gparent = GRANDPARENT(dev);
923 struct ata_pci_controller *ctlr = device_get_softc(gparent);
924 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
925 struct ata_device *atadev = device_get_softc(dev);
926 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
930 mode = ata_limit_mode(dev, mode,
931 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
933 mode = ata_check_80pin(dev, mode);
935 /* XXX SOS missing WDMA0+1 + PIO modes */
936 if (mode >= ATA_WDMA2) {
937 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
939 device_printf(dev, "%ssetting %s on %s chip\n",
940 (error) ? "FAILURE " : "",
941 ata_mode2str(mode), ctlr->chip->text);
943 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
945 reg44 &= ~(0x000f << (devno << 2));
946 if (mode >= ATA_UDMA0)
947 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
948 pci_write_config(gparent, 0x44, reg44, 2);
949 pci_write_config(gparent, 0x4a, 0xa6, 1);
950 pci_write_config(gparent, 0x40 + devno, 0x31, 1);
955 /* we could set PIO mode timings, but we assume the BIOS did that */
960 * Acer Labs Inc (ALI) chipset support functions
963 ata_ali_ident(device_t dev)
965 struct ata_pci_controller *ctlr = device_get_softc(dev);
966 struct ata_chip_id *idx;
967 static struct ata_chip_id ids[] =
968 {{ ATA_ALI_5289, 0x00, 2, ALISATA, ATA_SA150, "M5289" },
969 { ATA_ALI_5288, 0x00, 4, ALISATA, ATA_SA300, "M5288" },
970 { ATA_ALI_5287, 0x00, 4, ALISATA, ATA_SA150, "M5287" },
971 { ATA_ALI_5281, 0x00, 2, ALISATA, ATA_SA150, "M5281" },
972 { ATA_ALI_5229, 0xc5, 0, ALINEW, ATA_UDMA6, "M5229" },
973 { ATA_ALI_5229, 0xc4, 0, ALINEW, ATA_UDMA5, "M5229" },
974 { ATA_ALI_5229, 0xc2, 0, ALINEW, ATA_UDMA4, "M5229" },
975 { ATA_ALI_5229, 0x20, 0, ALIOLD, ATA_UDMA2, "M5229" },
976 { ATA_ALI_5229, 0x00, 0, ALIOLD, ATA_WDMA2, "M5229" },
977 { 0, 0, 0, 0, 0, 0}};
980 if (!(idx = ata_match_chip(dev, ids)))
983 sprintf(buffer, "AcerLabs %s %s controller",
984 idx->text, ata_mode2str(idx->max_dma));
985 device_set_desc_copy(dev, buffer);
987 ctlr->chipinit = ata_ali_chipinit;
992 ata_ali_chipinit(device_t dev)
994 struct ata_pci_controller *ctlr = device_get_softc(dev);
996 if (ata_setup_interrupt(dev))
999 switch (ctlr->chip->cfg2) {
1001 ctlr->channels = ctlr->chip->cfg1;
1002 ctlr->allocate = ata_ali_sata_allocate;
1003 ctlr->setmode = ata_sata_setmode;
1005 /* if we have a memory resource we can likely do AHCI */
1006 ctlr->r_type2 = SYS_RES_MEMORY;
1007 ctlr->r_rid2 = PCIR_BAR(5);
1009 /* AHCI mode is correctly supported only on the ALi 5288. */
1010 if ((ctlr->chip->chipid == ATA_ALI_5288) &&
1011 (ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1012 &ctlr->r_rid2, RF_ACTIVE)))
1013 return ata_ahci_chipinit(dev);
1015 /* enable PCI interrupt */
1016 pci_write_config(dev, PCIR_COMMAND,
1017 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1021 /* use device interrupt as byte count end */
1022 pci_write_config(dev, 0x4a, pci_read_config(dev, 0x4a, 1) | 0x20, 1);
1024 /* enable cable detection and UDMA support on newer chips */
1025 pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
1027 /* enable ATAPI UDMA mode */
1028 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x01, 1);
1030 /* only chips with revision > 0xc4 can do 48bit DMA */
1031 if (ctlr->chip->chiprev <= 0xc4)
1033 "using PIO transfers above 137GB as workaround for "
1034 "48bit DMA access bug, expect reduced performance\n");
1035 ctlr->allocate = ata_ali_allocate;
1036 ctlr->reset = ata_ali_reset;
1037 ctlr->setmode = ata_ali_setmode;
1041 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
1042 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x03, 1);
1043 ctlr->setmode = ata_ali_setmode;
1050 ata_ali_allocate(device_t dev)
1052 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1053 struct ata_channel *ch = device_get_softc(dev);
1055 /* setup the usual register normal pci style */
1056 if (ata_pci_allocate(dev))
1059 /* older chips can't do 48bit DMA transfers */
1060 if (ctlr->chip->chiprev <= 0xc4)
1061 ch->flags |= ATA_NO_48BIT_DMA;
1067 ata_ali_sata_allocate(device_t dev)
1069 device_t parent = device_get_parent(dev);
1070 struct ata_pci_controller *ctlr = device_get_softc(parent);
1071 struct ata_channel *ch = device_get_softc(dev);
1072 struct resource *io = NULL, *ctlio = NULL;
1073 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
1076 rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
1077 io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
1081 rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
1082 ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
1084 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
1088 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
1089 ch->r_io[i].res = io;
1090 ch->r_io[i].offset = i + (unit10 ? 8 : 0);
1092 ch->r_io[ATA_CONTROL].res = ctlio;
1093 ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
1094 ch->r_io[ATA_IDX_ADDR].res = io;
1095 ata_default_registers(dev);
1097 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
1098 ch->r_io[i].res = ctlr->r_res1;
1099 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
1102 ch->flags |= ATA_NO_SLAVE;
1104 /* XXX SOS PHY handling awkward in ALI chip not supported yet */
1110 ata_ali_reset(device_t dev)
1112 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1113 struct ata_channel *ch = device_get_softc(dev);
1117 ata_generic_reset(dev);
1120 * workaround for datacorruption bug found on at least SUN Blade-100
1121 * find the ISA function on the southbridge and disable then enable
1122 * the ATA channel tristate buffer
1124 if (ctlr->chip->chiprev == 0xc3 || ctlr->chip->chiprev == 0xc2) {
1125 if (!device_get_children(GRANDPARENT(dev), &children, &nchildren)) {
1126 for (i = 0; i < nchildren; i++) {
1127 if (pci_get_devid(children[i]) == ATA_ALI_1533) {
1128 pci_write_config(children[i], 0x58,
1129 pci_read_config(children[i], 0x58, 1) &
1130 ~(0x04 << ch->unit), 1);
1131 pci_write_config(children[i], 0x58,
1132 pci_read_config(children[i], 0x58, 1) |
1133 (0x04 << ch->unit), 1);
1137 free(children, M_TEMP);
1143 ata_ali_setmode(device_t dev, int mode)
1145 device_t gparent = GRANDPARENT(dev);
1146 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1147 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1148 struct ata_device *atadev = device_get_softc(dev);
1149 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1152 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1154 if (ctlr->chip->cfg2 & ALINEW) {
1155 if (mode > ATA_UDMA2 &&
1156 pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
1157 ata_print_cable(dev, "controller");
1162 mode = ata_check_80pin(dev, mode);
1164 if (ctlr->chip->cfg2 & ALIOLD) {
1165 /* doesn't support ATAPI DMA on write */
1166 ch->flags |= ATA_ATAPI_DMA_RO;
1167 if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
1168 /* doesn't support ATAPI DMA on two ATAPI devices */
1169 device_printf(dev, "two atapi devices on this channel, no DMA\n");
1170 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1174 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1177 device_printf(dev, "%ssetting %s on %s chip\n",
1178 (error) ? "FAILURE " : "",
1179 ata_mode2str(mode), ctlr->chip->text);
1181 if (mode >= ATA_UDMA0) {
1182 u_int8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
1183 u_int32_t word54 = pci_read_config(gparent, 0x54, 4);
1185 word54 &= ~(0x000f000f << (devno << 2));
1186 word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
1187 pci_write_config(gparent, 0x54, word54, 4);
1188 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1192 u_int32_t piotimings[] =
1193 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
1194 0x00310001, 0x00440001, 0x00330001, 0x00310001};
1196 pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
1197 ~(0x0008000f << (devno << 2)), 4);
1198 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1199 piotimings[ata_mode2idx(mode)], 4);
1201 atadev->mode = mode;
1207 * American Micro Devices (AMD) chipset support functions
1210 ata_amd_ident(device_t dev)
1212 struct ata_pci_controller *ctlr = device_get_softc(dev);
1213 struct ata_chip_id *idx;
1214 static struct ata_chip_id ids[] =
1215 {{ ATA_AMD756, 0x00, AMDNVIDIA, 0x00, ATA_UDMA4, "756" },
1216 { ATA_AMD766, 0x00, AMDNVIDIA, AMDCABLE|AMDBUG, ATA_UDMA5, "766" },
1217 { ATA_AMD768, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA5, "768" },
1218 { ATA_AMD8111, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA6, "8111" },
1219 { ATA_AMD5536, 0x00, AMDNVIDIA, 0x00, ATA_UDMA5, "CS5536" },
1220 { 0, 0, 0, 0, 0, 0}};
1223 if (!(idx = ata_match_chip(dev, ids)))
1226 sprintf(buffer, "AMD %s %s controller",
1227 idx->text, ata_mode2str(idx->max_dma));
1228 device_set_desc_copy(dev, buffer);
1230 ctlr->chipinit = ata_amd_chipinit;
1235 ata_amd_chipinit(device_t dev)
1237 struct ata_pci_controller *ctlr = device_get_softc(dev);
1239 if (ata_setup_interrupt(dev))
1242 /* disable/set prefetch, postwrite */
1243 if (ctlr->chip->cfg2 & AMDBUG)
1244 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
1246 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
1248 ctlr->setmode = ata_via_family_setmode;
1254 * ATI chipset support functions
1257 ata_ati_ident(device_t dev)
1259 struct ata_pci_controller *ctlr = device_get_softc(dev);
1260 struct ata_chip_id *idx;
1261 static struct ata_chip_id ids[] =
1262 {{ ATA_ATI_IXP200, 0x00, 0, 0, ATA_UDMA5, "IXP200" },
1263 { ATA_ATI_IXP300, 0x00, 0, 0, ATA_UDMA6, "IXP300" },
1264 { ATA_ATI_IXP400, 0x00, 0, 0, ATA_UDMA6, "IXP400" },
1265 { ATA_ATI_IXP300_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP300" },
1266 { ATA_ATI_IXP400_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP400" },
1267 { ATA_ATI_IXP400_S2, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP400" },
1268 { 0, 0, 0, 0, 0, 0}};
1271 if (!(idx = ata_match_chip(dev, ids)))
1274 sprintf(buffer, "ATI %s %s controller",
1275 idx->text, ata_mode2str(idx->max_dma));
1276 device_set_desc_copy(dev, buffer);
1279 /* the ATI SATA controller is actually a SiI 3112 controller*/
1280 if (ctlr->chip->cfg1 & SIIMEMIO)
1281 ctlr->chipinit = ata_sii_chipinit;
1283 ctlr->chipinit = ata_ati_chipinit;
1288 ata_ati_chipinit(device_t dev)
1290 struct ata_pci_controller *ctlr = device_get_softc(dev);
1292 if (ata_setup_interrupt(dev))
1295 ctlr->setmode = ata_ati_setmode;
1300 ata_ati_setmode(device_t dev, int mode)
1302 device_t gparent = GRANDPARENT(dev);
1303 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1304 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1305 struct ata_device *atadev = device_get_softc(dev);
1306 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1307 int offset = (devno ^ 0x01) << 3;
1309 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
1310 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
1311 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
1313 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1315 mode = ata_check_80pin(dev, mode);
1317 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1320 device_printf(dev, "%ssetting %s on %s chip\n",
1321 (error) ? "FAILURE " : "",
1322 ata_mode2str(mode), ctlr->chip->text);
1324 if (mode >= ATA_UDMA0) {
1325 pci_write_config(gparent, 0x56,
1326 (pci_read_config(gparent, 0x56, 2) &
1327 ~(0xf << (devno << 2))) |
1328 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
1329 pci_write_config(gparent, 0x54,
1330 pci_read_config(gparent, 0x54, 1) |
1331 (0x01 << devno), 1);
1332 pci_write_config(gparent, 0x44,
1333 (pci_read_config(gparent, 0x44, 4) &
1334 ~(0xff << offset)) |
1335 (dmatimings[2] << offset), 4);
1337 else if (mode >= ATA_WDMA0) {
1338 pci_write_config(gparent, 0x54,
1339 pci_read_config(gparent, 0x54, 1) &
1340 ~(0x01 << devno), 1);
1341 pci_write_config(gparent, 0x44,
1342 (pci_read_config(gparent, 0x44, 4) &
1343 ~(0xff << offset)) |
1344 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
1347 pci_write_config(gparent, 0x54,
1348 pci_read_config(gparent, 0x54, 1) &
1349 ~(0x01 << devno), 1);
1351 pci_write_config(gparent, 0x4a,
1352 (pci_read_config(gparent, 0x4a, 2) &
1353 ~(0xf << (devno << 2))) |
1354 (((mode - ATA_PIO0) & ATA_MODE_MASK) << (devno<<2)),2);
1355 pci_write_config(gparent, 0x40,
1356 (pci_read_config(gparent, 0x40, 4) &
1357 ~(0xff << offset)) |
1358 (piotimings[ata_mode2idx(mode)] << offset), 4);
1359 atadev->mode = mode;
1365 * Cyrix chipset support functions
1368 ata_cyrix_ident(device_t dev)
1370 struct ata_pci_controller *ctlr = device_get_softc(dev);
1372 if (pci_get_devid(dev) == ATA_CYRIX_5530) {
1373 device_set_desc(dev, "Cyrix 5530 ATA33 controller");
1374 ctlr->chipinit = ata_cyrix_chipinit;
1381 ata_cyrix_chipinit(device_t dev)
1383 struct ata_pci_controller *ctlr = device_get_softc(dev);
1385 if (ata_setup_interrupt(dev))
1389 ctlr->setmode = ata_cyrix_setmode;
1391 ctlr->setmode = ata_generic_setmode;
1396 ata_cyrix_setmode(device_t dev, int mode)
1398 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1399 struct ata_device *atadev = device_get_softc(dev);
1400 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1401 u_int32_t piotiming[] =
1402 { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
1403 u_int32_t dmatiming[] = { 0x00077771, 0x00012121, 0x00002020 };
1404 u_int32_t udmatiming[] = { 0x00921250, 0x00911140, 0x00911030 };
1407 ch->dma->alignment = 16;
1408 ch->dma->max_iosize = 126 * DEV_BSIZE;
1410 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1412 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1415 device_printf(dev, "%ssetting %s on Cyrix chip\n",
1416 (error) ? "FAILURE " : "", ata_mode2str(mode));
1418 if (mode >= ATA_UDMA0) {
1419 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1420 0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
1422 else if (mode >= ATA_WDMA0) {
1423 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1424 0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
1427 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1428 0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
1430 atadev->mode = mode;
1436 * Cypress chipset support functions
1439 ata_cypress_ident(device_t dev)
1441 struct ata_pci_controller *ctlr = device_get_softc(dev);
1444 * the Cypress chip is a mess, it contains two ATA functions, but
1445 * both channels are visible on the first one.
1446 * simply ignore the second function for now, as the right
1447 * solution (ignoring the second channel on the first function)
1448 * doesn't work with the crappy ATA interrupt setup on the alpha.
1450 if (pci_get_devid(dev) == ATA_CYPRESS_82C693 &&
1451 pci_get_function(dev) == 1 &&
1452 pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
1453 device_set_desc(dev, "Cypress 82C693 ATA controller");
1454 ctlr->chipinit = ata_cypress_chipinit;
1461 ata_cypress_chipinit(device_t dev)
1463 struct ata_pci_controller *ctlr = device_get_softc(dev);
1465 if (ata_setup_interrupt(dev))
1468 ctlr->setmode = ata_cypress_setmode;
1473 ata_cypress_setmode(device_t dev, int mode)
1475 device_t gparent = GRANDPARENT(dev);
1476 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1477 struct ata_device *atadev = device_get_softc(dev);
1480 mode = ata_limit_mode(dev, mode, ATA_WDMA2);
1482 /* XXX SOS missing WDMA0+1 + PIO modes */
1483 if (mode == ATA_WDMA2) {
1484 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1486 device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
1487 error ? "FAILURE " : "");
1489 pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
1490 atadev->mode = mode;
1494 /* we could set PIO mode timings, but we assume the BIOS did that */
1499 * HighPoint chipset support functions
1502 ata_highpoint_ident(device_t dev)
1504 struct ata_pci_controller *ctlr = device_get_softc(dev);
1505 struct ata_chip_id *idx;
1506 static struct ata_chip_id ids[] =
1507 {{ ATA_HPT374, 0x07, HPT374, 0x00, ATA_UDMA6, "HPT374" },
1508 { ATA_HPT372, 0x02, HPT372, 0x00, ATA_UDMA6, "HPT372N" },
1509 { ATA_HPT372, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT372" },
1510 { ATA_HPT371, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT371" },
1511 { ATA_HPT366, 0x05, HPT372, 0x00, ATA_UDMA6, "HPT372" },
1512 { ATA_HPT366, 0x03, HPT370, 0x00, ATA_UDMA5, "HPT370" },
1513 { ATA_HPT366, 0x02, HPT366, 0x00, ATA_UDMA4, "HPT368" },
1514 { ATA_HPT366, 0x00, HPT366, HPTOLD, ATA_UDMA4, "HPT366" },
1515 { ATA_HPT302, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT302" },
1516 { 0, 0, 0, 0, 0, 0}};
1519 if (!(idx = ata_match_chip(dev, ids)))
1522 strcpy(buffer, "HighPoint ");
1523 strcat(buffer, idx->text);
1524 if (idx->cfg1 == HPT374) {
1525 if (pci_get_function(dev) == 0)
1526 strcat(buffer, " (channel 0+1)");
1527 if (pci_get_function(dev) == 1)
1528 strcat(buffer, " (channel 2+3)");
1530 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
1531 device_set_desc_copy(dev, buffer);
1533 ctlr->chipinit = ata_highpoint_chipinit;
1538 ata_highpoint_chipinit(device_t dev)
1540 struct ata_pci_controller *ctlr = device_get_softc(dev);
1542 if (ata_setup_interrupt(dev))
1545 if (ctlr->chip->cfg2 == HPTOLD) {
1546 /* disable interrupt prediction */
1547 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
1550 /* disable interrupt prediction */
1551 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
1552 pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
1554 /* enable interrupts */
1555 pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
1557 /* set clocks etc */
1558 if (ctlr->chip->cfg1 < HPT372)
1559 pci_write_config(dev, 0x5b, 0x22, 1);
1561 pci_write_config(dev, 0x5b,
1562 (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
1564 ctlr->allocate = ata_highpoint_allocate;
1565 ctlr->setmode = ata_highpoint_setmode;
1570 ata_highpoint_allocate(device_t dev)
1572 struct ata_channel *ch = device_get_softc(dev);
1574 /* setup the usual register normal pci style */
1575 if (ata_pci_allocate(dev))
1578 ch->flags |= ATA_ALWAYS_DMASTAT;
1583 ata_highpoint_setmode(device_t dev, int mode)
1585 device_t gparent = GRANDPARENT(dev);
1586 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1587 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1588 struct ata_device *atadev = device_get_softc(dev);
1589 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1591 u_int32_t timings33[][4] = {
1592 /* HPT366 HPT370 HPT372 HPT374 mode */
1593 { 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a }, /* PIO 0 */
1594 { 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 }, /* PIO 1 */
1595 { 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 }, /* PIO 2 */
1596 { 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 }, /* PIO 3 */
1597 { 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 }, /* PIO 4 */
1598 { 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea }, /* MWDMA 0 */
1599 { 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 }, /* MWDMA 1 */
1600 { 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 }, /* MWDMA 2 */
1601 { 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea }, /* UDMA 0 */
1602 { 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 }, /* UDMA 1 */
1603 { 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 }, /* UDMA 2 */
1604 { 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 }, /* UDMA 3 */
1605 { 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 }, /* UDMA 4 */
1606 { 0, 0x16454e31, 0x1c8a9c62, 0x12848242 }, /* UDMA 5 */
1607 { 0, 0, 0x1c869c62, 0x12808242 } /* UDMA 6 */
1610 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1612 if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
1613 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1615 mode = ata_highpoint_check_80pin(dev, mode);
1618 * most if not all HPT chips cant really handle that the device is
1619 * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
1620 * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
1622 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
1623 ata_limit_mode(dev, mode, ATA_UDMA5));
1625 device_printf(dev, "%ssetting %s on HighPoint chip\n",
1626 (error) ? "FAILURE " : "", ata_mode2str(mode));
1628 pci_write_config(gparent, 0x40 + (devno << 2),
1629 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
1630 atadev->mode = mode;
1634 ata_highpoint_check_80pin(device_t dev, int mode)
1636 device_t gparent = GRANDPARENT(dev);
1637 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1638 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1639 u_int8_t reg, val, res;
1641 if (ctlr->chip->cfg1 == HPT374 && pci_get_function(gparent) == 1) {
1642 reg = ch->unit ? 0x57 : 0x53;
1643 val = pci_read_config(gparent, reg, 1);
1644 pci_write_config(gparent, reg, val | 0x80, 1);
1648 val = pci_read_config(gparent, reg, 1);
1649 pci_write_config(gparent, reg, val & 0xfe, 1);
1651 res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1652 pci_write_config(gparent, reg, val, 1);
1654 if (mode > ATA_UDMA2 && res) {
1655 ata_print_cable(dev, "controller");
1663 * Intel chipset support functions
1666 ata_intel_ident(device_t dev)
1668 struct ata_pci_controller *ctlr = device_get_softc(dev);
1669 struct ata_chip_id *idx;
1670 static struct ata_chip_id ids[] =
1671 {{ ATA_I82371FB, 0, 0, 0x00, ATA_WDMA2, "PIIX" },
1672 { ATA_I82371SB, 0, 0, 0x00, ATA_WDMA2, "PIIX3" },
1673 { ATA_I82371AB, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1674 { ATA_I82443MX, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1675 { ATA_I82451NX, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1676 { ATA_I82801AB, 0, 0, 0x00, ATA_UDMA2, "ICH0" },
1677 { ATA_I82801AA, 0, 0, 0x00, ATA_UDMA4, "ICH" },
1678 { ATA_I82372FB, 0, 0, 0x00, ATA_UDMA4, "ICH" },
1679 { ATA_I82801BA, 0, 0, 0x00, ATA_UDMA5, "ICH2" },
1680 { ATA_I82801BA_1, 0, 0, 0x00, ATA_UDMA5, "ICH2" },
1681 { ATA_I82801CA, 0, 0, 0x00, ATA_UDMA5, "ICH3" },
1682 { ATA_I82801CA_1, 0, 0, 0x00, ATA_UDMA5, "ICH3" },
1683 { ATA_I82801DB, 0, 0, 0x00, ATA_UDMA5, "ICH4" },
1684 { ATA_I82801DB_1, 0, 0, 0x00, ATA_UDMA5, "ICH4" },
1685 { ATA_I82801EB, 0, 0, 0x00, ATA_UDMA5, "ICH5" },
1686 { ATA_I82801EB_S1, 0, 0, 0x00, ATA_SA150, "ICH5" },
1687 { ATA_I82801EB_R1, 0, 0, 0x00, ATA_SA150, "ICH5" },
1688 { ATA_I6300ESB, 0, 0, 0x00, ATA_UDMA5, "6300ESB" },
1689 { ATA_I6300ESB_S1, 0, 0, 0x00, ATA_SA150, "6300ESB" },
1690 { ATA_I6300ESB_R1, 0, 0, 0x00, ATA_SA150, "6300ESB" },
1691 { ATA_I82801FB, 0, 0, 0x00, ATA_UDMA5, "ICH6" },
1692 { ATA_I82801FB_S1, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1693 { ATA_I82801FB_R1, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1694 { ATA_I82801FBM, 0, AHCI, 0x00, ATA_SA150, "ICH6M" },
1695 { ATA_I82801GB, 0, 0, 0x00, ATA_UDMA5, "ICH7" },
1696 { ATA_I82801GB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1697 { ATA_I82801GB_R1, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1698 { ATA_I82801GB_AH, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1699 { ATA_I82801GBM_S1, 0, AHCI, 0x00, ATA_SA300, "ICH7M" },
1700 { ATA_I82801GBM_R1, 0, AHCI, 0x00, ATA_SA300, "ICH7M" },
1701 { ATA_I82801GBM_AH, 0, AHCI, 0x00, ATA_SA300, "ICH7M" },
1702 { ATA_I63XXESB2, 0, 0, 0x00, ATA_UDMA5, "63XXESB2" },
1703 { ATA_I63XXESB2_S1, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1704 { ATA_I63XXESB2_S2, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1705 { ATA_I63XXESB2_R1, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1706 { ATA_I63XXESB2_R2, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1707 { ATA_I82801HB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1708 { ATA_I82801HB_S2, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1709 { ATA_I82801HB_R1, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1710 { ATA_I82801HB_AH4, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1711 { ATA_I82801HB_AH6, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1712 { ATA_I82801HBM_S1, 0, AHCI, 0x00, ATA_SA300, "ICH8M" },
1713 { ATA_I82801HBM_S2, 0, AHCI, 0x00, ATA_SA300, "ICH8M" },
1714 { ATA_I82801IB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1715 { ATA_I82801IB_S2, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1716 { ATA_I82801IB_AH2, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1717 { ATA_I82801IB_AH4, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1718 { ATA_I82801IB_AH6, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1719 { ATA_I31244, 0, 0, 0x00, ATA_SA150, "31244" },
1720 { 0, 0, 0, 0, 0, 0}};
1723 if (!(idx = ata_match_chip(dev, ids)))
1726 sprintf(buffer, "Intel %s %s controller",
1727 idx->text, ata_mode2str(idx->max_dma));
1728 device_set_desc_copy(dev, buffer);
1730 ctlr->chipinit = ata_intel_chipinit;
1735 ata_intel_chipinit(device_t dev)
1737 struct ata_pci_controller *ctlr = device_get_softc(dev);
1739 if (ata_setup_interrupt(dev))
1742 /* good old PIIX needs special treatment (not implemented) */
1743 if (ctlr->chip->chipid == ATA_I82371FB) {
1744 ctlr->setmode = ata_intel_old_setmode;
1747 /* the intel 31244 needs special care if in DPA mode */
1748 else if (ctlr->chip->chipid == ATA_I31244) {
1749 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE) {
1750 ctlr->r_type2 = SYS_RES_MEMORY;
1751 ctlr->r_rid2 = PCIR_BAR(0);
1752 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1757 ctlr->allocate = ata_intel_31244_allocate;
1758 ctlr->reset = ata_intel_31244_reset;
1760 ctlr->setmode = ata_sata_setmode;
1763 /* non SATA intel chips goes here */
1764 else if (ctlr->chip->max_dma < ATA_SA150) {
1765 ctlr->allocate = ata_intel_allocate;
1766 ctlr->setmode = ata_intel_new_setmode;
1769 /* SATA parts can be either compat or AHCI */
1771 /* force all ports active "the legacy way" */
1772 pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f, 2);
1774 ctlr->allocate = ata_intel_allocate;
1775 ctlr->reset = ata_intel_reset;
1778 * if we have AHCI capability and BAR(5) as a memory resource
1779 * and AHCI or RAID mode enabled in BIOS we go for AHCI mode
1781 if ((ctlr->chip->cfg1 == AHCI) &&
1782 (pci_read_config(dev, 0x90, 1) & 0xc0)) {
1783 ctlr->r_type2 = SYS_RES_MEMORY;
1784 ctlr->r_rid2 = PCIR_BAR(5);
1785 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1788 return ata_ahci_chipinit(dev);
1790 ctlr->setmode = ata_sata_setmode;
1792 /* enable PCI interrupt */
1793 pci_write_config(dev, PCIR_COMMAND,
1794 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1800 ata_intel_allocate(device_t dev)
1802 struct ata_channel *ch = device_get_softc(dev);
1804 /* setup the usual register normal pci style */
1805 if (ata_pci_allocate(dev))
1808 ch->flags |= ATA_ALWAYS_DMASTAT;
1813 ata_intel_reset(device_t dev)
1815 device_t parent = device_get_parent(dev);
1816 struct ata_pci_controller *ctlr = device_get_softc(parent);
1817 struct ata_channel *ch = device_get_softc(dev);
1820 /* ICH6 & ICH7 in compat mode has 4 SATA ports as master/slave on 2 ch's */
1821 if (ctlr->chip->cfg1) {
1822 mask = (0x0005 << ch->unit);
1825 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1826 if (pci_read_config(parent, 0x90, 1) & 0x04)
1829 mask = (0x0001 << ch->unit);
1830 /* XXX SOS should be in intel_allocate if we grow it */
1831 ch->flags |= ATA_NO_SLAVE;
1834 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1836 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1838 /* wait up to 1 sec for "connect well" */
1839 for (timeout = 0; timeout < 100 ; timeout++) {
1840 if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1841 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1845 ata_generic_reset(dev);
1849 ata_intel_old_setmode(device_t dev, int mode)
1855 ata_intel_new_setmode(device_t dev, int mode)
1857 device_t gparent = GRANDPARENT(dev);
1858 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1859 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1860 struct ata_device *atadev = device_get_softc(dev);
1861 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1862 u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
1863 u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
1864 u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
1865 u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
1866 u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
1867 u_int32_t mask40 = 0, new40 = 0;
1868 u_int8_t mask44 = 0, new44 = 0;
1870 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1871 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1873 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1875 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1876 ata_print_cable(dev, "controller");
1880 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1883 device_printf(dev, "%ssetting %s on %s chip\n",
1884 (error) ? "FAILURE " : "",
1885 ata_mode2str(mode), ctlr->chip->text);
1889 if (mode >= ATA_UDMA0) {
1890 pci_write_config(gparent, 0x48, reg48 | (0x0001 << devno), 2);
1891 pci_write_config(gparent, 0x4a,
1892 (reg4a & ~(0x3 << (devno << 2))) |
1893 ((0x01 + !(mode & 0x01)) << (devno << 2)), 2);
1896 pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
1897 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))), 2);
1900 if (mode >= ATA_UDMA2)
1901 pci_write_config(gparent, 0x54, reg54 | (0x1 << devno), 2);
1903 pci_write_config(gparent, 0x54, reg54 & ~(0x1 << devno), 2);
1905 if (mode >= ATA_UDMA5)
1906 pci_write_config(gparent, 0x54, reg54 | (0x1000 << devno), 2);
1908 pci_write_config(gparent, 0x54, reg54 & ~(0x1000 << devno), 2);
1910 reg40 &= ~0x00ff00ff;
1911 reg40 |= 0x40774077;
1913 if (atadev->unit == ATA_MASTER) {
1915 new40 = timings[ata_mode2idx(mode)] << 8;
1919 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
1920 (timings[ata_mode2idx(mode)] & 0x03);
1928 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
1929 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
1931 atadev->mode = mode;
1935 ata_intel_31244_allocate(device_t dev)
1937 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1938 struct ata_channel *ch = device_get_softc(dev);
1942 ch_offset = 0x200 + ch->unit * 0x200;
1944 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
1945 ch->r_io[i].res = ctlr->r_res2;
1947 /* setup ATA registers */
1948 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
1949 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x06;
1950 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
1951 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
1952 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
1953 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
1954 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
1955 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1d;
1956 ch->r_io[ATA_ERROR].offset = ch_offset + 0x04;
1957 ch->r_io[ATA_STATUS].offset = ch_offset + 0x1c;
1958 ch->r_io[ATA_ALTSTAT].offset = ch_offset + 0x28;
1959 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x29;
1961 /* setup DMA registers */
1962 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x100;
1963 ch->r_io[ATA_SERROR].offset = ch_offset + 0x104;
1964 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x108;
1966 /* setup SATA registers */
1967 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x70;
1968 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x72;
1969 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x74;
1971 ch->flags |= ATA_NO_SLAVE;
1973 ch->hw.status = ata_intel_31244_status;
1974 ch->hw.command = ata_intel_31244_command;
1976 /* enable PHY state change interrupt */
1977 ATA_OUTL(ctlr->r_res2, 0x4,
1978 ATA_INL(ctlr->r_res2, 0x04) | (0x01 << (ch->unit << 3)));
1983 ata_intel_31244_status(device_t dev)
1985 /* do we have any PHY events ? */
1986 ata_sata_phy_check_events(dev);
1988 /* any drive action to take care of ? */
1989 return ata_pci_status(dev);
1993 ata_intel_31244_command(struct ata_request *request)
1995 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
1996 struct ata_device *atadev = device_get_softc(request->dev);
1999 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
2000 return (ata_generic_command(request));
2002 lba = request->u.ata.lba;
2003 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | atadev->unit);
2004 /* enable interrupt */
2005 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
2006 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
2007 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
2008 ATA_IDX_OUTW(ch, ATA_SECTOR, ((lba >> 16) & 0xff00) | (lba & 0x00ff));
2009 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((lba >> 24) & 0xff00) |
2010 ((lba >> 8) & 0x00ff));
2011 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((lba >> 32) & 0xff00) |
2012 ((lba >> 16) & 0x00ff));
2014 /* issue command to controller */
2015 ATA_IDX_OUTB(ch, ATA_COMMAND, request->u.ata.command);
2021 ata_intel_31244_reset(device_t dev)
2023 if (ata_sata_phy_reset(dev))
2024 ata_generic_reset(dev);
2029 * Integrated Technology Express Inc. (ITE) chipset support functions
2032 ata_ite_ident(device_t dev)
2034 struct ata_pci_controller *ctlr = device_get_softc(dev);
2035 struct ata_chip_id *idx;
2036 static struct ata_chip_id ids[] =
2037 {{ ATA_IT8212F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8212F" },
2038 { ATA_IT8211F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8211F" },
2039 { 0, 0, 0, 0, 0, 0}};
2042 if (!(idx = ata_match_chip(dev, ids)))
2045 sprintf(buffer, "ITE %s %s controller",
2046 idx->text, ata_mode2str(idx->max_dma));
2047 device_set_desc_copy(dev, buffer);
2049 ctlr->chipinit = ata_ite_chipinit;
2054 ata_ite_chipinit(device_t dev)
2056 struct ata_pci_controller *ctlr = device_get_softc(dev);
2058 if (ata_setup_interrupt(dev))
2061 ctlr->setmode = ata_ite_setmode;
2063 /* set PCI mode and 66Mhz reference clock */
2064 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
2066 /* set default active & recover timings */
2067 pci_write_config(dev, 0x54, 0x31, 1);
2068 pci_write_config(dev, 0x56, 0x31, 1);
2073 ata_ite_setmode(device_t dev, int mode)
2075 device_t gparent = GRANDPARENT(dev);
2076 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2077 struct ata_device *atadev = device_get_softc(dev);
2078 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2081 /* correct the mode for what the HW supports */
2082 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2084 /* check the CBLID bits for 80 conductor cable detection */
2085 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
2086 (ch->unit ? (1<<3) : (1<<2)))) {
2087 ata_print_cable(dev, "controller");
2091 /* set the wanted mode on the device */
2092 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2095 device_printf(dev, "%s setting %s on ITE8212F chip\n",
2096 (error) ? "failed" : "success", ata_mode2str(mode));
2098 /* if the device accepted the mode change, setup the HW accordingly */
2100 if (mode >= ATA_UDMA0) {
2101 u_int8_t udmatiming[] =
2102 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
2104 /* enable UDMA mode */
2105 pci_write_config(gparent, 0x50,
2106 pci_read_config(gparent, 0x50, 1) &
2107 ~(1 << (devno + 3)), 1);
2109 /* set UDMA timing */
2110 pci_write_config(gparent,
2111 0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
2112 udmatiming[mode & ATA_MODE_MASK], 1);
2115 u_int8_t chtiming[] =
2116 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
2118 /* disable UDMA mode */
2119 pci_write_config(gparent, 0x50,
2120 pci_read_config(gparent, 0x50, 1) |
2121 (1 << (devno + 3)), 1);
2123 /* set active and recover timing (shared between master & slave) */
2124 if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
2125 chtiming[ata_mode2idx(mode)])
2126 pci_write_config(gparent, 0x54 + (ch->unit << 2),
2127 chtiming[ata_mode2idx(mode)], 1);
2129 atadev->mode = mode;
2135 * JMicron chipset support functions
2138 ata_jmicron_ident(device_t dev)
2140 struct ata_pci_controller *ctlr = device_get_softc(dev);
2141 struct ata_chip_id *idx;
2142 static struct ata_chip_id ids[] =
2143 {{ ATA_JMB360, 0, 1, 0, ATA_SA300, "JMB360" },
2144 { ATA_JMB361, 0, 1, 1, ATA_SA300, "JMB361" },
2145 { ATA_JMB363, 0, 2, 1, ATA_SA300, "JMB363" },
2146 { ATA_JMB365, 0, 1, 2, ATA_SA300, "JMB365" },
2147 { ATA_JMB366, 0, 2, 2, ATA_SA300, "JMB366" },
2148 { ATA_JMB368, 0, 0, 1, ATA_UDMA6, "JMB368" },
2149 { 0, 0, 0, 0, 0, 0}};
2152 if (!(idx = ata_match_chip(dev, ids)))
2155 if ((pci_read_config(dev, 0xdf, 1) & 0x40) &&
2156 (pci_get_function(dev) == (pci_read_config(dev, 0x40, 1) & 0x02 >> 1)))
2157 sprintf(buffer, "JMicron %s %s controller",
2158 idx->text, ata_mode2str(ATA_UDMA6));
2160 sprintf(buffer, "JMicron %s %s controller",
2161 idx->text, ata_mode2str(idx->max_dma));
2162 device_set_desc_copy(dev, buffer);
2164 ctlr->chipinit = ata_jmicron_chipinit;
2169 ata_jmicron_chipinit(device_t dev)
2171 struct ata_pci_controller *ctlr = device_get_softc(dev);
2174 if (ata_setup_interrupt(dev))
2177 /* do we have multiple PCI functions ? */
2178 if (pci_read_config(dev, 0xdf, 1) & 0x40) {
2179 /* if we have a memory BAR(5) we are on the AHCI part */
2180 ctlr->r_type2 = SYS_RES_MEMORY;
2181 ctlr->r_rid2 = PCIR_BAR(5);
2182 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2183 &ctlr->r_rid2, RF_ACTIVE)))
2184 return ata_ahci_chipinit(dev);
2186 /* otherwise we are on the PATA part */
2187 ctlr->allocate = ata_pci_allocate;
2188 ctlr->reset = ata_generic_reset;
2189 ctlr->dmainit = ata_pci_dmainit;
2190 ctlr->setmode = ata_jmicron_setmode;
2191 ctlr->channels = ctlr->chip->cfg2;
2194 /* set controller configuration to a combined setup we support */
2195 pci_write_config(dev, 0x40, 0x80c0a131, 4);
2196 pci_write_config(dev, 0x80, 0x01200000, 4);
2198 ctlr->r_type2 = SYS_RES_MEMORY;
2199 ctlr->r_rid2 = PCIR_BAR(5);
2200 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2201 &ctlr->r_rid2, RF_ACTIVE))){
2202 if ((error = ata_ahci_chipinit(dev)))
2206 ctlr->allocate = ata_jmicron_allocate;
2207 ctlr->reset = ata_jmicron_reset;
2208 ctlr->dmainit = ata_jmicron_dmainit;
2209 ctlr->setmode = ata_jmicron_setmode;
2211 /* set the number of HW channels */
2212 ctlr->channels = ctlr->chip->cfg1 + ctlr->chip->cfg2;
2218 ata_jmicron_allocate(device_t dev)
2220 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2221 struct ata_channel *ch = device_get_softc(dev);
2224 if (ch->unit >= ctlr->chip->cfg1) {
2225 ch->unit -= ctlr->chip->cfg1;
2226 error = ata_pci_allocate(dev);
2227 ch->unit += ctlr->chip->cfg1;
2230 error = ata_ahci_allocate(dev);
2235 ata_jmicron_reset(device_t dev)
2237 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2238 struct ata_channel *ch = device_get_softc(dev);
2240 if (ch->unit >= ctlr->chip->cfg1)
2241 ata_generic_reset(dev);
2243 ata_ahci_reset(dev);
2247 ata_jmicron_dmainit(device_t dev)
2249 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2250 struct ata_channel *ch = device_get_softc(dev);
2252 if (ch->unit >= ctlr->chip->cfg1)
2253 ata_pci_dmainit(dev);
2255 ata_ahci_dmainit(dev);
2259 ata_jmicron_setmode(device_t dev, int mode)
2261 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2262 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2264 if (pci_read_config(dev, 0xdf, 1) & 0x40 || ch->unit >= ctlr->chip->cfg1) {
2265 struct ata_device *atadev = device_get_softc(dev);
2267 /* check for 80pin cable present */
2268 if (pci_read_config(dev, 0x40, 1) & 0x08)
2269 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2271 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2273 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
2274 atadev->mode = mode;
2277 ata_sata_setmode(dev, mode);
2282 * Marvell chipset support functions
2284 #define ATA_MV_HOST_BASE(ch) \
2285 ((ch->unit & 3) * 0x0100) + (ch->unit > 3 ? 0x30000 : 0x20000)
2286 #define ATA_MV_EDMA_BASE(ch) \
2287 ((ch->unit & 3) * 0x2000) + (ch->unit > 3 ? 0x30000 : 0x20000)
2289 struct ata_marvell_response {
2291 u_int8_t edma_status;
2292 u_int8_t dev_status;
2293 u_int32_t timestamp;
2296 struct ata_marvell_dma_prdentry {
2304 ata_marvell_ident(device_t dev)
2306 struct ata_pci_controller *ctlr = device_get_softc(dev);
2307 struct ata_chip_id *idx;
2308 static struct ata_chip_id ids[] =
2309 {{ ATA_M88SX5040, 0, 4, MV50XX, ATA_SA150, "88SX5040" },
2310 { ATA_M88SX5041, 0, 4, MV50XX, ATA_SA150, "88SX5041" },
2311 { ATA_M88SX5080, 0, 8, MV50XX, ATA_SA150, "88SX5080" },
2312 { ATA_M88SX5081, 0, 8, MV50XX, ATA_SA150, "88SX5081" },
2313 { ATA_M88SX6041, 0, 4, MV60XX, ATA_SA300, "88SX6041" },
2314 { ATA_M88SX6081, 0, 8, MV60XX, ATA_SA300, "88SX6081" },
2315 { ATA_M88SX6101, 0, 1, MV61XX, ATA_UDMA6, "88SX6101" },
2316 { ATA_M88SX6145, 0, 2, MV61XX, ATA_UDMA6, "88SX6145" },
2317 { 0, 0, 0, 0, 0, 0}};
2320 if (!(idx = ata_match_chip(dev, ids)))
2323 sprintf(buffer, "Marvell %s %s controller",
2324 idx->text, ata_mode2str(idx->max_dma));
2325 device_set_desc_copy(dev, buffer);
2327 switch (ctlr->chip->cfg2) {
2330 ctlr->chipinit = ata_marvell_edma_chipinit;
2333 ctlr->chipinit = ata_marvell_pata_chipinit;
2340 ata_marvell_pata_chipinit(device_t dev)
2342 struct ata_pci_controller *ctlr = device_get_softc(dev);
2344 if (ata_setup_interrupt(dev))
2347 ctlr->allocate = ata_marvell_pata_allocate;
2348 ctlr->setmode = ata_marvell_pata_setmode;
2349 ctlr->channels = ctlr->chip->cfg1;
2354 ata_marvell_pata_allocate(device_t dev)
2356 struct ata_channel *ch = device_get_softc(dev);
2358 /* setup the usual register normal pci style */
2359 if (ata_pci_allocate(dev))
2362 /* dont use 32 bit PIO transfers */
2363 ch->flags |= ATA_USE_16BIT;
2369 ata_marvell_pata_setmode(device_t dev, int mode)
2371 device_t gparent = GRANDPARENT(dev);
2372 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2373 struct ata_device *atadev = device_get_softc(dev);
2375 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2376 mode = ata_check_80pin(dev, mode);
2377 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
2378 atadev->mode = mode;
2382 ata_marvell_edma_chipinit(device_t dev)
2384 struct ata_pci_controller *ctlr = device_get_softc(dev);
2386 if (ata_setup_interrupt(dev))
2389 ctlr->r_type1 = SYS_RES_MEMORY;
2390 ctlr->r_rid1 = PCIR_BAR(0);
2391 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
2392 &ctlr->r_rid1, RF_ACTIVE)))
2395 /* mask all host controller interrupts */
2396 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x00000000);
2398 /* mask all PCI interrupts */
2399 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x00000000);
2401 ctlr->allocate = ata_marvell_edma_allocate;
2402 ctlr->reset = ata_marvell_edma_reset;
2403 ctlr->dmainit = ata_marvell_edma_dmainit;
2404 ctlr->setmode = ata_sata_setmode;
2405 ctlr->channels = ctlr->chip->cfg1;
2407 /* clear host controller interrupts */
2408 ATA_OUTL(ctlr->r_res1, 0x20014, 0x00000000);
2409 if (ctlr->chip->cfg1 > 4)
2410 ATA_OUTL(ctlr->r_res1, 0x30014, 0x00000000);
2412 /* clear PCI interrupts */
2413 ATA_OUTL(ctlr->r_res1, 0x01d58, 0x00000000);
2415 /* unmask PCI interrupts we want */
2416 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x007fffff);
2418 /* unmask host controller interrupts we want */
2419 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x000000ff/*HC0*/ | 0x0001fe00/*HC1*/ |
2420 /*(1<<19) | (1<<20) | (1<<21) |*/(1<<22) | (1<<24) | (0x7f << 25));
2422 /* enable PCI interrupt */
2423 pci_write_config(dev, PCIR_COMMAND,
2424 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
2429 ata_marvell_edma_allocate(device_t dev)
2431 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2432 struct ata_channel *ch = device_get_softc(dev);
2433 u_int64_t work = ch->dma->work_bus;
2436 /* clear work area */
2437 bzero(ch->dma->work, 1024+256);
2439 /* set legacy ATA resources */
2440 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2441 ch->r_io[i].res = ctlr->r_res1;
2442 ch->r_io[i].offset = 0x02100 + (i << 2) + ATA_MV_EDMA_BASE(ch);
2444 ch->r_io[ATA_CONTROL].res = ctlr->r_res1;
2445 ch->r_io[ATA_CONTROL].offset = 0x02120 + ATA_MV_EDMA_BASE(ch);
2446 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res1;
2447 ata_default_registers(dev);
2449 /* set SATA resources */
2450 switch (ctlr->chip->cfg2) {
2452 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2453 ch->r_io[ATA_SSTATUS].offset = 0x00100 + ATA_MV_HOST_BASE(ch);
2454 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2455 ch->r_io[ATA_SERROR].offset = 0x00104 + ATA_MV_HOST_BASE(ch);
2456 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2457 ch->r_io[ATA_SCONTROL].offset = 0x00108 + ATA_MV_HOST_BASE(ch);
2460 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2461 ch->r_io[ATA_SSTATUS].offset = 0x02300 + ATA_MV_EDMA_BASE(ch);
2462 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2463 ch->r_io[ATA_SERROR].offset = 0x02304 + ATA_MV_EDMA_BASE(ch);
2464 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2465 ch->r_io[ATA_SCONTROL].offset = 0x02308 + ATA_MV_EDMA_BASE(ch);
2466 ch->r_io[ATA_SACTIVE].res = ctlr->r_res1;
2467 ch->r_io[ATA_SACTIVE].offset = 0x02350 + ATA_MV_EDMA_BASE(ch);
2471 ch->flags |= ATA_NO_SLAVE;
2472 ch->flags |= ATA_USE_16BIT; /* XXX SOS needed ? */
2473 ata_generic_hw(dev);
2474 ch->hw.begin_transaction = ata_marvell_edma_begin_transaction;
2475 ch->hw.end_transaction = ata_marvell_edma_end_transaction;
2476 ch->hw.status = ata_marvell_edma_status;
2478 /* disable the EDMA machinery */
2479 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2480 DELAY(100000); /* SOS should poll for disabled */
2482 /* set configuration to non-queued 128b read transfers stop on error */
2483 ATA_OUTL(ctlr->r_res1, 0x02000 + ATA_MV_EDMA_BASE(ch), (1<<11) | (1<<13));
2485 /* request queue base high */
2486 ATA_OUTL(ctlr->r_res1, 0x02010 + ATA_MV_EDMA_BASE(ch), work >> 32);
2488 /* request queue in ptr */
2489 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2491 /* request queue out ptr */
2492 ATA_OUTL(ctlr->r_res1, 0x02018 + ATA_MV_EDMA_BASE(ch), 0x0);
2494 /* response queue base high */
2496 ATA_OUTL(ctlr->r_res1, 0x0201c + ATA_MV_EDMA_BASE(ch), work >> 32);
2498 /* response queue in ptr */
2499 ATA_OUTL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch), 0x0);
2501 /* response queue out ptr */
2502 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2504 /* clear SATA error register */
2505 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2507 /* clear any outstanding error interrupts */
2508 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2510 /* unmask all error interrupts */
2511 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2513 /* enable EDMA machinery */
2514 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2519 ata_marvell_edma_status(device_t dev)
2521 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2522 struct ata_channel *ch = device_get_softc(dev);
2523 u_int32_t cause = ATA_INL(ctlr->r_res1, 0x01d60);
2524 int shift = (ch->unit << 1) + (ch->unit > 3);
2526 if (cause & (1 << shift)) {
2528 /* clear interrupt(s) */
2529 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2531 /* do we have any PHY events ? */
2532 ata_sata_phy_check_events(dev);
2535 /* do we have any device action ? */
2536 return (cause & (2 << shift));
2539 /* must be called with ATA channel locked and state_mtx held */
2541 ata_marvell_edma_begin_transaction(struct ata_request *request)
2543 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2544 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2550 int dummy, error, slot;
2552 /* only DMA R/W goes through the EMDA machine */
2553 if (request->u.ata.command != ATA_READ_DMA &&
2554 request->u.ata.command != ATA_WRITE_DMA) {
2556 /* disable the EDMA machinery */
2557 if (ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)
2558 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2559 return ata_begin_transaction(request);
2562 /* check for 48 bit access and convert if needed */
2563 ata_modify_if_48bit(request);
2565 /* check sanity, setup SG list and DMA engine */
2566 if ((error = ch->dma->load(ch->dev, request->data, request->bytecount,
2567 request->flags & ATA_R_READ, ch->dma->sg,
2569 device_printf(request->dev, "setting up DMA failed\n");
2570 request->result = error;
2571 return ATA_OP_FINISHED;
2574 /* get next free request queue slot */
2575 req_in = ATA_INL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch));
2576 slot = (((req_in & ~0xfffffc00) >> 5) + 0) & 0x1f;
2577 bytep = (u_int8_t *)(ch->dma->work);
2578 bytep += (slot << 5);
2579 wordp = (u_int16_t *)bytep;
2580 quadp = (u_int32_t *)bytep;
2582 /* fill in this request */
2583 quadp[0] = (long)ch->dma->sg_bus & 0xffffffff;
2584 quadp[1] = (u_int64_t)ch->dma->sg_bus >> 32;
2585 wordp[4] = (request->flags & ATA_R_READ ? 0x01 : 0x00) | (tag<<1);
2588 bytep[i++] = (request->u.ata.count >> 8) & 0xff;
2589 bytep[i++] = 0x10 | ATA_COUNT;
2590 bytep[i++] = request->u.ata.count & 0xff;
2591 bytep[i++] = 0x10 | ATA_COUNT;
2593 bytep[i++] = (request->u.ata.lba >> 24) & 0xff;
2594 bytep[i++] = 0x10 | ATA_SECTOR;
2595 bytep[i++] = request->u.ata.lba & 0xff;
2596 bytep[i++] = 0x10 | ATA_SECTOR;
2598 bytep[i++] = (request->u.ata.lba >> 32) & 0xff;
2599 bytep[i++] = 0x10 | ATA_CYL_LSB;
2600 bytep[i++] = (request->u.ata.lba >> 8) & 0xff;
2601 bytep[i++] = 0x10 | ATA_CYL_LSB;
2603 bytep[i++] = (request->u.ata.lba >> 40) & 0xff;
2604 bytep[i++] = 0x10 | ATA_CYL_MSB;
2605 bytep[i++] = (request->u.ata.lba >> 16) & 0xff;
2606 bytep[i++] = 0x10 | ATA_CYL_MSB;
2608 bytep[i++] = ATA_D_LBA | ATA_D_IBM | ((request->u.ata.lba >> 24) & 0xf);
2609 bytep[i++] = 0x10 | ATA_DRIVE;
2611 bytep[i++] = request->u.ata.command;
2612 bytep[i++] = 0x90 | ATA_COMMAND;
2614 /* enable EDMA machinery if needed */
2615 if (!(ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)) {
2616 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2617 while (!(ATA_INL(ctlr->r_res1,
2618 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
2622 /* tell EDMA it has a new request */
2623 slot = (((req_in & ~0xfffffc00) >> 5) + 1) & 0x1f;
2624 req_in &= 0xfffffc00;
2625 req_in += (slot << 5);
2626 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), req_in);
2628 return ATA_OP_CONTINUES;
2631 /* must be called with ATA channel locked and state_mtx held */
2633 ata_marvell_edma_end_transaction(struct ata_request *request)
2635 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2636 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2637 int offset = (ch->unit > 3 ? 0x30014 : 0x20014);
2638 u_int32_t icr = ATA_INL(ctlr->r_res1, offset);
2641 /* EDMA interrupt */
2642 if ((icr & (0x0001 << (ch->unit & 3)))) {
2643 struct ata_marvell_response *response;
2644 u_int32_t rsp_in, rsp_out;
2648 callout_stop(&request->callout);
2650 /* get response ptr's */
2651 rsp_in = ATA_INL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch));
2652 rsp_out = ATA_INL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch));
2653 slot = (((rsp_in & ~0xffffff00) >> 3)) & 0x1f;
2654 rsp_out &= 0xffffff00;
2655 rsp_out += (slot << 3);
2656 response = (struct ata_marvell_response *)
2657 (ch->dma->work + 1024 + (slot << 3));
2659 /* record status for this request */
2660 request->status = response->dev_status;
2664 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), rsp_out);
2666 /* update progress */
2667 if (!(request->status & ATA_S_ERROR) &&
2668 !(request->flags & ATA_R_TIMEOUT))
2669 request->donecount = request->bytecount;
2671 /* unload SG list */
2672 ch->dma->unload(ch->dev);
2674 res = ATA_OP_FINISHED;
2677 /* legacy ATA interrupt */
2679 res = ata_end_transaction(request);
2683 ATA_OUTL(ctlr->r_res1, offset, ~(icr & (0x0101 << (ch->unit & 3))));
2688 ata_marvell_edma_reset(device_t dev)
2690 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2691 struct ata_channel *ch = device_get_softc(dev);
2693 /* disable the EDMA machinery */
2694 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2695 while ((ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
2698 /* clear SATA error register */
2699 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2701 /* clear any outstanding error interrupts */
2702 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2704 /* unmask all error interrupts */
2705 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2707 /* enable channel and test for devices */
2708 if (ata_sata_phy_reset(dev))
2709 ata_generic_reset(dev);
2711 /* enable EDMA machinery */
2712 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2716 ata_marvell_edma_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs,
2719 struct ata_dmasetprd_args *args = xsc;
2720 struct ata_marvell_dma_prdentry *prd = args->dmatab;
2723 if ((args->error = error))
2726 for (i = 0; i < nsegs; i++) {
2727 prd[i].addrlo = htole32(segs[i].ds_addr);
2728 prd[i].count = htole32(segs[i].ds_len);
2729 prd[i].addrhi = htole32((u_int64_t)segs[i].ds_addr >> 32);
2731 prd[i - 1].count |= htole32(ATA_DMA_EOT);
2735 ata_marvell_edma_dmainit(device_t dev)
2737 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2738 struct ata_channel *ch = device_get_softc(dev);
2742 /* note start and stop are not used here */
2743 ch->dma->setprd = ata_marvell_edma_dmasetprd;
2745 if (ATA_INL(ctlr->r_res1, 0x00d00) & 0x00000004)
2746 ch->dma->max_address = BUS_SPACE_MAXADDR;
2752 * National chipset support functions
2755 ata_national_ident(device_t dev)
2757 struct ata_pci_controller *ctlr = device_get_softc(dev);
2759 /* this chip is a clone of the Cyrix chip, bugs and all */
2760 if (pci_get_devid(dev) == ATA_SC1100) {
2761 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
2762 ctlr->chipinit = ata_national_chipinit;
2769 ata_national_chipinit(device_t dev)
2771 struct ata_pci_controller *ctlr = device_get_softc(dev);
2773 if (ata_setup_interrupt(dev))
2776 ctlr->setmode = ata_national_setmode;
2781 ata_national_setmode(device_t dev, int mode)
2783 device_t gparent = GRANDPARENT(dev);
2784 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2785 struct ata_device *atadev = device_get_softc(dev);
2786 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2787 u_int32_t piotiming[] =
2788 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
2789 0x00803020, 0x20102010, 0x00100010,
2790 0x00100010, 0x00100010, 0x00100010 };
2791 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
2792 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
2795 ch->dma->alignment = 16;
2796 ch->dma->max_iosize = 126 * DEV_BSIZE;
2798 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2800 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2803 device_printf(dev, "%s setting %s on National chip\n",
2804 (error) ? "failed" : "success", ata_mode2str(mode));
2806 if (mode >= ATA_UDMA0) {
2807 pci_write_config(gparent, 0x44 + (devno << 3),
2808 udmatiming[mode & ATA_MODE_MASK], 4);
2810 else if (mode >= ATA_WDMA0) {
2811 pci_write_config(gparent, 0x44 + (devno << 3),
2812 dmatiming[mode & ATA_MODE_MASK], 4);
2815 pci_write_config(gparent, 0x44 + (devno << 3),
2816 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
2819 pci_write_config(gparent, 0x40 + (devno << 3),
2820 piotiming[ata_mode2idx(mode)], 4);
2821 atadev->mode = mode;
2827 * NetCell chipset support functions
2830 ata_netcell_ident(device_t dev)
2832 struct ata_pci_controller *ctlr = device_get_softc(dev);
2834 if (pci_get_devid(dev) == ATA_NETCELL_SR) {
2835 device_set_desc(dev, "Netcell SyncRAID SR3000/5000 RAID Controller");
2836 ctlr->chipinit = ata_netcell_chipinit;
2843 ata_netcell_chipinit(device_t dev)
2845 struct ata_pci_controller *ctlr = device_get_softc(dev);
2847 if (ata_generic_chipinit(dev))
2850 ctlr->allocate = ata_netcell_allocate;
2855 ata_netcell_allocate(device_t dev)
2857 struct ata_channel *ch = device_get_softc(dev);
2859 /* setup the usual register normal pci style */
2860 if (ata_pci_allocate(dev))
2863 /* the NetCell only supports 16 bit PIO transfers */
2864 ch->flags |= ATA_USE_16BIT;
2871 * nVidia chipset support functions
2874 ata_nvidia_ident(device_t dev)
2876 struct ata_pci_controller *ctlr = device_get_softc(dev);
2877 struct ata_chip_id *idx;
2878 static struct ata_chip_id ids[] =
2879 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nForce" },
2880 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2" },
2881 { ATA_NFORCE2_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2 Pro" },
2882 { ATA_NFORCE2_PRO_S1, 0, 0, 0, ATA_SA150, "nForce2 Pro" },
2883 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3" },
2884 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3 Pro" },
2885 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
2886 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
2887 { ATA_NFORCE_MCP04, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP" },
2888 { ATA_NFORCE_MCP04_S1, 0, 0, NV4, ATA_SA150, "nForce MCP" },
2889 { ATA_NFORCE_MCP04_S2, 0, 0, NV4, ATA_SA150, "nForce MCP" },
2890 { ATA_NFORCE_CK804, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce CK804" },
2891 { ATA_NFORCE_CK804_S1, 0, 0, NV4, ATA_SA300, "nForce CK804" },
2892 { ATA_NFORCE_CK804_S2, 0, 0, NV4, ATA_SA300, "nForce CK804" },
2893 { ATA_NFORCE_MCP51, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP51" },
2894 { ATA_NFORCE_MCP51_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP51" },
2895 { ATA_NFORCE_MCP51_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP51" },
2896 { ATA_NFORCE_MCP55, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP55" },
2897 { ATA_NFORCE_MCP55_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP55" },
2898 { ATA_NFORCE_MCP55_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP55" },
2899 { ATA_NFORCE_MCP61, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP61" },
2900 { ATA_NFORCE_MCP61_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP61" },
2901 { ATA_NFORCE_MCP61_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP61" },
2902 { ATA_NFORCE_MCP61_S3, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP61" },
2903 { 0, 0, 0, 0, 0, 0}} ;
2906 if (!(idx = ata_match_chip(dev, ids)))
2909 sprintf(buffer, "nVidia %s %s controller",
2910 idx->text, ata_mode2str(idx->max_dma));
2911 device_set_desc_copy(dev, buffer);
2913 ctlr->chipinit = ata_nvidia_chipinit;
2918 ata_nvidia_chipinit(device_t dev)
2920 struct ata_pci_controller *ctlr = device_get_softc(dev);
2922 if (ata_setup_interrupt(dev))
2925 if (ctlr->chip->max_dma >= ATA_SA150) {
2926 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
2927 ctlr->r_type2 = SYS_RES_IOPORT;
2929 ctlr->r_type2 = SYS_RES_MEMORY;
2930 ctlr->r_rid2 = PCIR_BAR(5);
2931 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2932 &ctlr->r_rid2, RF_ACTIVE))) {
2933 int offset = ctlr->chip->cfg2 & NV4 ? 0x0440 : 0x0010;
2935 ctlr->allocate = ata_nvidia_allocate;
2936 ctlr->reset = ata_nvidia_reset;
2938 /* enable control access */
2939 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
2941 if (ctlr->chip->cfg2 & NVQ) {
2942 /* clear interrupt status */
2943 ATA_OUTL(ctlr->r_res2, offset, 0x00ff00ff);
2945 /* enable device and PHY state change interrupts */
2946 ATA_OUTL(ctlr->r_res2, offset + 4, 0x000d000d);
2948 /* disable NCQ support */
2949 ATA_OUTL(ctlr->r_res2, 0x0400,
2950 ATA_INL(ctlr->r_res2, 0x0400) & 0xfffffff9);
2953 /* clear interrupt status */
2954 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2956 /* enable device and PHY state change interrupts */
2957 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
2960 /* enable PCI interrupt */
2961 pci_write_config(dev, PCIR_COMMAND,
2962 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
2965 ctlr->setmode = ata_sata_setmode;
2968 /* disable prefetch, postwrite */
2969 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
2970 ctlr->setmode = ata_via_family_setmode;
2976 ata_nvidia_allocate(device_t dev)
2978 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2979 struct ata_channel *ch = device_get_softc(dev);
2981 /* setup the usual register normal pci style */
2982 if (ata_pci_allocate(dev))
2985 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2986 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
2987 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2988 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
2989 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2990 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
2992 ch->hw.status = ata_nvidia_status;
2993 ch->flags |= ATA_NO_SLAVE;
2999 ata_nvidia_status(device_t dev)
3001 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3002 struct ata_channel *ch = device_get_softc(dev);
3003 int offset = ctlr->chip->cfg2 & NV4 ? 0x0440 : 0x0010;
3004 int shift = ch->unit << (ctlr->chip->cfg2 & NVQ ? 4 : 2);
3005 u_int32_t istatus = ATA_INL(ctlr->r_res2, offset);
3007 /* do we have any PHY events ? */
3008 if (istatus & (0x0c << shift))
3009 ata_sata_phy_check_events(dev);
3011 /* clear interrupt(s) */
3012 ATA_OUTB(ctlr->r_res2, offset,
3013 (0x0f << shift) | (ctlr->chip->cfg2 & NVQ ? 0x00f000f0 : 0));
3015 /* do we have any device action ? */
3016 return (istatus & (0x01 << shift));
3020 ata_nvidia_reset(device_t dev)
3022 if (ata_sata_phy_reset(dev))
3023 ata_generic_reset(dev);
3028 * Promise chipset support functions
3030 #define ATA_PDC_APKT_OFFSET 0x00000010
3031 #define ATA_PDC_HPKT_OFFSET 0x00000040
3032 #define ATA_PDC_ASG_OFFSET 0x00000080
3033 #define ATA_PDC_LSG_OFFSET 0x000000c0
3034 #define ATA_PDC_HSG_OFFSET 0x00000100
3035 #define ATA_PDC_CHN_OFFSET 0x00000400
3036 #define ATA_PDC_BUF_BASE 0x00400000
3037 #define ATA_PDC_BUF_OFFSET 0x00100000
3038 #define ATA_PDC_MAX_HPKT 8
3039 #define ATA_PDC_WRITE_REG 0x00
3040 #define ATA_PDC_WRITE_CTL 0x0e
3041 #define ATA_PDC_WRITE_END 0x08
3042 #define ATA_PDC_WAIT_NBUSY 0x10
3043 #define ATA_PDC_WAIT_READY 0x18
3044 #define ATA_PDC_1B 0x20
3045 #define ATA_PDC_2B 0x40
3047 struct host_packet {
3049 TAILQ_ENTRY(host_packet) chain;
3052 struct ata_promise_sx4 {
3054 TAILQ_HEAD(, host_packet) queue;
3059 ata_promise_ident(device_t dev)
3061 struct ata_pci_controller *ctlr = device_get_softc(dev);
3062 struct ata_chip_id *idx;
3063 static struct ata_chip_id ids[] =
3064 {{ ATA_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "PDC20246" },
3065 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20262" },
3066 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20263" },
3067 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20265" },
3068 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20267" },
3069 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20268" },
3070 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "PDC20269" },
3071 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20270" },
3072 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "PDC20271" },
3073 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "PDC20275" },
3074 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "PDC20276" },
3075 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "PDC20277" },
3076 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "PDC20318" },
3077 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "PDC20319" },
3078 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20371" },
3079 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20375" },
3080 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20376" },
3081 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20377" },
3082 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20378" },
3083 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20379" },
3084 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20571" },
3085 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20575" },
3086 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20579" },
3087 { ATA_PDC20771, 0, PRMIO, PRCMBO2, ATA_SA300, "PDC20771" },
3088 { ATA_PDC40775, 0, PRMIO, PRCMBO2, ATA_SA300, "PDC40775" },
3089 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20617" },
3090 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20618" },
3091 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20619" },
3092 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20620" },
3093 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "PDC20621" },
3094 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "PDC20622" },
3095 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40518" },
3096 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40519" },
3097 { ATA_PDC40718, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40718" },
3098 { ATA_PDC40719, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40719" },
3099 { ATA_PDC40779, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40779" },
3100 { 0, 0, 0, 0, 0, 0}};
3102 uintptr_t devid = 0;
3104 if (!(idx = ata_match_chip(dev, ids)))
3107 /* if we are on a SuperTrak SX6000 dont attach */
3108 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
3109 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
3110 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
3111 devid == ATA_I960RM)
3114 strcpy(buffer, "Promise ");
3115 strcat(buffer, idx->text);
3117 /* if we are on a FastTrak TX4, adjust the interrupt resource */
3118 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
3119 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
3120 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
3121 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
3122 static long start = 0, end = 0;
3124 if (pci_get_slot(dev) == 1) {
3125 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
3126 strcat(buffer, " (channel 0+1)");
3128 else if (pci_get_slot(dev) == 2 && start && end) {
3129 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
3130 strcat(buffer, " (channel 2+3)");
3136 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
3137 device_set_desc_copy(dev, buffer);
3139 ctlr->chipinit = ata_promise_chipinit;
3144 ata_promise_chipinit(device_t dev)
3146 struct ata_pci_controller *ctlr = device_get_softc(dev);
3147 int fake_reg, stat_reg;
3149 if (ata_setup_interrupt(dev))
3152 switch (ctlr->chip->cfg1) {
3155 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
3157 ctlr->dmainit = ata_promise_dmainit;
3161 /* enable burst mode */
3162 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
3163 ctlr->allocate = ata_promise_allocate;
3164 ctlr->setmode = ata_promise_setmode;
3168 ctlr->allocate = ata_promise_tx2_allocate;
3169 ctlr->setmode = ata_promise_setmode;
3173 ctlr->r_type1 = SYS_RES_MEMORY;
3174 ctlr->r_rid1 = PCIR_BAR(4);
3175 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
3176 &ctlr->r_rid1, RF_ACTIVE)))
3179 ctlr->r_type2 = SYS_RES_MEMORY;
3180 ctlr->r_rid2 = PCIR_BAR(3);
3181 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3182 &ctlr->r_rid2, RF_ACTIVE)))
3185 if (ctlr->chip->cfg2 == PRSX4X) {
3186 struct ata_promise_sx4 *hpkt;
3187 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
3189 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
3190 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
3191 ata_promise_sx4_intr, ctlr, &ctlr->handle)) {
3192 device_printf(dev, "unable to setup interrupt\n");
3196 /* print info about cache memory */
3197 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
3198 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
3199 ((dimm >> 24) & 0xff),
3200 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
3201 " ECC enabled" : "" );
3203 /* adjust cache memory parameters */
3204 ATA_OUTL(ctlr->r_res2, 0x000c000c,
3205 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
3207 /* setup host packet controls */
3208 hpkt = malloc(sizeof(struct ata_promise_sx4),
3209 M_TEMP, M_NOWAIT | M_ZERO);
3210 mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
3211 TAILQ_INIT(&hpkt->queue);
3213 device_set_ivars(dev, hpkt);
3214 ctlr->allocate = ata_promise_mio_allocate;
3215 ctlr->reset = ata_promise_mio_reset;
3216 ctlr->dmainit = ata_promise_mio_dmainit;
3217 ctlr->setmode = ata_promise_setmode;
3222 /* mio type controllers need an interrupt intercept */
3223 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
3224 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
3225 ata_promise_mio_intr, ctlr, &ctlr->handle)) {
3226 device_printf(dev, "unable to setup interrupt\n");
3230 switch (ctlr->chip->cfg2) {
3232 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
3233 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
3257 /* prime fake interrupt register */
3258 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3260 /* clear SATA status */
3261 ATA_OUTL(ctlr->r_res2, stat_reg, 0x000000ff);
3263 ctlr->allocate = ata_promise_mio_allocate;
3264 ctlr->reset = ata_promise_mio_reset;
3265 ctlr->dmainit = ata_promise_mio_dmainit;
3266 ctlr->setmode = ata_promise_mio_setmode;
3273 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
3275 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
3280 ata_promise_allocate(device_t dev)
3282 struct ata_channel *ch = device_get_softc(dev);
3284 if (ata_pci_allocate(dev))
3287 ch->hw.status = ata_promise_status;
3292 ata_promise_status(device_t dev)
3294 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3295 struct ata_channel *ch = device_get_softc(dev);
3297 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)) {
3298 return ata_pci_status(dev);
3304 ata_promise_dmastart(device_t dev)
3306 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3307 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3308 struct ata_device *atadev = device_get_softc(dev);
3310 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3311 ATA_OUTB(ctlr->r_res1, 0x11,
3312 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
3313 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20,
3314 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
3315 (ch->dma->cur_iosize >> 1));
3317 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
3318 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
3319 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
3320 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3321 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
3322 ATA_BMCMD_START_STOP);
3323 ch->flags |= ATA_DMA_ACTIVE;
3328 ata_promise_dmastop(device_t dev)
3330 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3331 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3332 struct ata_device *atadev = device_get_softc(dev);
3335 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3336 ATA_OUTB(ctlr->r_res1, 0x11,
3337 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
3338 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20, 0);
3340 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
3341 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3342 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3343 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3344 ch->flags &= ~ATA_DMA_ACTIVE;
3349 ata_promise_dmareset(device_t dev)
3351 struct ata_channel *ch = device_get_softc(dev);
3353 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3354 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3355 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3356 ch->flags &= ~ATA_DMA_ACTIVE;
3360 ata_promise_dmainit(device_t dev)
3362 struct ata_channel *ch = device_get_softc(dev);
3366 ch->dma->start = ata_promise_dmastart;
3367 ch->dma->stop = ata_promise_dmastop;
3368 ch->dma->reset = ata_promise_dmareset;
3373 ata_promise_setmode(device_t dev, int mode)
3375 device_t gparent = GRANDPARENT(dev);
3376 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3377 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3378 struct ata_device *atadev = device_get_softc(dev);
3379 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3381 u_int32_t timings[][2] = {
3382 /* PROLD PRNEW mode */
3383 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
3384 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
3385 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
3386 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
3387 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
3388 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
3389 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
3390 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
3391 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
3392 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
3393 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
3394 { 0, 0x00424ef6 }, /* UDMA 3 */
3395 { 0, 0x004127f3 }, /* UDMA 4 */
3396 { 0, 0x004127f3 } /* UDMA 5 */
3399 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3401 switch (ctlr->chip->cfg1) {
3404 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
3405 (ch->unit ? 1 << 11 : 1 << 10))) {
3406 ata_print_cable(dev, "controller");
3409 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
3410 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
3414 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3415 if (mode > ATA_UDMA2 &&
3416 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
3417 ata_print_cable(dev, "controller");
3423 if (mode > ATA_UDMA2 &&
3424 (ATA_INL(ctlr->r_res2,
3425 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
3426 (ch->unit << 7)) & 0x01000000)) {
3427 ata_print_cable(dev, "controller");
3433 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3436 device_printf(dev, "%ssetting %s on %s chip\n",
3437 (error) ? "FAILURE " : "",
3438 ata_mode2str(mode), ctlr->chip->text);
3440 if (ctlr->chip->cfg1 < PRTX)
3441 pci_write_config(gparent, 0x60 + (devno << 2),
3442 timings[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
3443 atadev->mode = mode;
3449 ata_promise_tx2_allocate(device_t dev)
3451 struct ata_channel *ch = device_get_softc(dev);
3453 if (ata_pci_allocate(dev))
3456 ch->hw.status = ata_promise_tx2_status;
3461 ata_promise_tx2_status(device_t dev)
3463 struct ata_channel *ch = device_get_softc(dev);
3465 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3466 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
3467 return ata_pci_status(dev);
3473 ata_promise_mio_allocate(device_t dev)
3475 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3476 struct ata_channel *ch = device_get_softc(dev);
3477 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
3480 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
3481 ch->r_io[i].res = ctlr->r_res2;
3482 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
3484 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
3485 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
3486 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
3487 ata_default_registers(dev);
3488 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
3489 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
3490 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3491 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
3492 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3493 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
3494 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3495 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
3496 ch->flags |= ATA_NO_SLAVE;
3498 ch->flags |= ATA_USE_16BIT;
3500 ata_generic_hw(dev);
3501 if (ctlr->chip->cfg2 & PRSX4X) {
3502 ch->hw.command = ata_promise_sx4_command;
3505 ch->hw.command = ata_promise_mio_command;
3506 ch->hw.status = ata_promise_mio_status;
3512 ata_promise_mio_intr(void *data)
3514 struct ata_pci_controller *ctlr = data;
3515 struct ata_channel *ch;
3519 switch (ctlr->chip->cfg2) {
3533 * since reading interrupt status register on early "mio" chips
3534 * clears the status bits we cannot read it for each channel later on
3535 * in the generic interrupt routine.
3536 * store the bits in an unused register in the chip so we can read
3537 * it from there safely to get around this "feature".
3539 vector = ATA_INL(ctlr->r_res2, 0x040);
3540 ATA_OUTL(ctlr->r_res2, 0x040, vector);
3541 ATA_OUTL(ctlr->r_res2, fake_reg, vector);
3543 for (unit = 0; unit < ctlr->channels; unit++) {
3544 if ((ch = ctlr->interrupt[unit].argument))
3545 ctlr->interrupt[unit].function(ch);
3548 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3552 ata_promise_mio_status(device_t dev)
3554 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3555 struct ata_channel *ch = device_get_softc(dev);
3556 struct ata_connect_task *tp;
3557 u_int32_t fake_reg, stat_reg, vector, status;
3559 switch (ctlr->chip->cfg2) {
3574 /* read and acknowledge interrupt */
3575 vector = ATA_INL(ctlr->r_res2, fake_reg);
3577 /* read and clear interface status */
3578 status = ATA_INL(ctlr->r_res2, stat_reg);
3579 ATA_OUTL(ctlr->r_res2, stat_reg, status & (0x00000011 << ch->unit));
3581 /* check for and handle disconnect events */
3582 if ((status & (0x00000001 << ch->unit)) &&
3583 (tp = (struct ata_connect_task *)
3584 malloc(sizeof(struct ata_connect_task),
3585 M_ATA, M_NOWAIT | M_ZERO))) {
3588 device_printf(ch->dev, "DISCONNECT requested\n");
3589 tp->action = ATA_C_DETACH;
3591 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3592 taskqueue_enqueue(taskqueue_thread, &tp->task);
3595 /* check for and handle connect events */
3596 if ((status & (0x00000010 << ch->unit)) &&
3597 (tp = (struct ata_connect_task *)
3598 malloc(sizeof(struct ata_connect_task),
3599 M_ATA, M_NOWAIT | M_ZERO))) {
3602 device_printf(ch->dev, "CONNECT requested\n");
3603 tp->action = ATA_C_ATTACH;
3605 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3606 taskqueue_enqueue(taskqueue_thread, &tp->task);
3609 /* do we have any device action ? */
3610 return (vector & (1 << (ch->unit + 1)));
3614 ata_promise_mio_command(struct ata_request *request)
3616 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
3617 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
3618 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
3620 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
3622 /* XXX SOS add ATAPI commands support later */
3623 switch (request->u.ata.command) {
3625 return ata_generic_command(request);
3628 case ATA_READ_DMA48:
3629 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
3633 case ATA_WRITE_DMA48:
3634 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
3637 wordp[1] = htole32(ch->dma->sg_bus);
3639 ata_promise_apkt((u_int8_t*)wordp, request);
3641 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
3646 ata_promise_mio_reset(device_t dev)
3648 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3649 struct ata_channel *ch = device_get_softc(dev);
3650 struct ata_promise_sx4 *hpktp;
3652 switch (ctlr->chip->cfg2) {
3655 /* softreset channel ATA module */
3656 hpktp = device_get_ivars(ctlr->dev);
3657 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
3659 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
3660 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
3661 ~0x00003f9f) | (ch->unit + 1));
3663 /* softreset HOST module */ /* XXX SOS what about other outstandings */
3664 mtx_lock(&hpktp->mtx);
3665 ATA_OUTL(ctlr->r_res2, 0xc012c,
3666 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
3668 ATA_OUTL(ctlr->r_res2, 0xc012c,
3669 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
3671 mtx_unlock(&hpktp->mtx);
3672 ata_generic_reset(dev);
3678 if ((ctlr->chip->cfg2 == PRSATA) ||
3679 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3681 /* mask plug/unplug intr */
3682 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
3685 /* softreset channels ATA module */
3686 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
3688 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
3689 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
3690 ~0x00003f9f) | (ch->unit + 1));
3692 if ((ctlr->chip->cfg2 == PRSATA) ||
3693 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3695 if (ata_sata_phy_reset(dev))
3696 ata_generic_reset(dev);
3698 /* reset and enable plug/unplug intr */
3699 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
3702 ata_generic_reset(dev);
3707 if ((ctlr->chip->cfg2 == PRSATA2) ||
3708 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
3709 /* set portmultiplier port */
3710 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
3712 /* mask plug/unplug intr */
3713 ATA_OUTL(ctlr->r_res2, 0x060, (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 == PRSATA2) ||
3724 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
3726 /* set PHY mode to "improved" */
3727 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
3728 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
3729 ~0x00000003) | 0x00000001);
3731 if (ata_sata_phy_reset(dev))
3732 ata_generic_reset(dev);
3734 /* reset and enable plug/unplug intr */
3735 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
3737 /* set portmultiplier port */
3738 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
3741 ata_generic_reset(dev);
3748 ata_promise_mio_dmainit(device_t dev)
3750 /* note start and stop are not used here */
3755 ata_promise_mio_setmode(device_t dev, int mode)
3757 device_t gparent = GRANDPARENT(dev);
3758 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3759 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3761 if ( (ctlr->chip->cfg2 == PRSATA) ||
3762 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2)) ||
3763 (ctlr->chip->cfg2 == PRSATA2) ||
3764 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2)))
3765 ata_sata_setmode(dev, mode);
3767 ata_promise_setmode(dev, mode);
3771 ata_promise_sx4_intr(void *data)
3773 struct ata_pci_controller *ctlr = data;
3774 struct ata_channel *ch;
3775 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
3778 for (unit = 0; unit < ctlr->channels; unit++) {
3779 if (vector & (1 << (unit + 1)))
3780 if ((ch = ctlr->interrupt[unit].argument))
3781 ctlr->interrupt[unit].function(ch);
3782 if (vector & (1 << (unit + 5)))
3783 if ((ch = ctlr->interrupt[unit].argument))
3784 ata_promise_queue_hpkt(ctlr,
3785 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
3786 ATA_PDC_HPKT_OFFSET));
3787 if (vector & (1 << (unit + 9))) {
3788 ata_promise_next_hpkt(ctlr);
3789 if ((ch = ctlr->interrupt[unit].argument))
3790 ctlr->interrupt[unit].function(ch);
3792 if (vector & (1 << (unit + 13))) {
3793 ata_promise_next_hpkt(ctlr);
3794 if ((ch = ctlr->interrupt[unit].argument))
3795 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
3796 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
3797 ATA_PDC_APKT_OFFSET));
3803 ata_promise_sx4_command(struct ata_request *request)
3805 device_t gparent = GRANDPARENT(request->dev);
3806 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3807 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
3808 struct ata_dma_prdentry *prd = ch->dma->sg;
3809 caddr_t window = rman_get_virtual(ctlr->r_res1);
3811 int i, idx, length = 0;
3813 /* XXX SOS add ATAPI commands support later */
3814 switch (request->u.ata.command) {
3819 case ATA_ATA_IDENTIFY:
3823 case ATA_READ_MUL48:
3827 case ATA_WRITE_MUL48:
3828 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
3829 return ata_generic_command(request);
3831 case ATA_SETFEATURES:
3832 case ATA_FLUSHCACHE:
3833 case ATA_FLUSHCACHE48:
3836 wordp = (u_int32_t *)
3837 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
3838 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
3841 ata_promise_apkt((u_int8_t *)wordp, request);
3842 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
3843 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
3844 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
3845 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
3849 case ATA_READ_DMA48:
3851 case ATA_WRITE_DMA48:
3852 wordp = (u_int32_t *)
3853 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
3856 wordp[idx++] = prd[i].addr;
3857 wordp[idx++] = prd[i].count;
3858 length += (prd[i].count & ~ATA_DMA_EOT);
3859 } while (!(prd[i++].count & ATA_DMA_EOT));
3861 wordp = (u_int32_t *)
3862 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
3863 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
3864 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
3866 wordp = (u_int32_t *)
3867 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
3868 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
3869 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
3871 wordp = (u_int32_t *)
3872 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
3873 if (request->flags & ATA_R_READ)
3874 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
3875 if (request->flags & ATA_R_WRITE)
3876 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
3877 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
3878 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
3881 wordp = (u_int32_t *)
3882 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
3883 if (request->flags & ATA_R_READ)
3884 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
3885 if (request->flags & ATA_R_WRITE)
3886 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
3887 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
3889 ata_promise_apkt((u_int8_t *)wordp, request);
3890 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
3892 if (request->flags & ATA_R_READ) {
3893 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
3894 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
3895 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
3896 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
3898 if (request->flags & ATA_R_WRITE) {
3899 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
3900 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
3901 ata_promise_queue_hpkt(ctlr,
3902 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
3909 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
3911 struct ata_device *atadev = device_get_softc(request->dev);
3914 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
3915 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
3916 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
3917 bytep[i++] = ATA_A_4BIT;
3919 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3920 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
3921 bytep[i++] = request->u.ata.feature >> 8;
3922 bytep[i++] = request->u.ata.feature;
3923 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
3924 bytep[i++] = request->u.ata.count >> 8;
3925 bytep[i++] = request->u.ata.count;
3926 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
3927 bytep[i++] = request->u.ata.lba >> 24;
3928 bytep[i++] = request->u.ata.lba;
3929 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
3930 bytep[i++] = request->u.ata.lba >> 32;
3931 bytep[i++] = request->u.ata.lba >> 8;
3932 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
3933 bytep[i++] = request->u.ata.lba >> 40;
3934 bytep[i++] = request->u.ata.lba >> 16;
3935 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
3936 bytep[i++] = ATA_D_LBA | atadev->unit;
3939 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
3940 bytep[i++] = request->u.ata.feature;
3941 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
3942 bytep[i++] = request->u.ata.count;
3943 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
3944 bytep[i++] = request->u.ata.lba;
3945 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
3946 bytep[i++] = request->u.ata.lba >> 8;
3947 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
3948 bytep[i++] = request->u.ata.lba >> 16;
3949 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
3950 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
3951 ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
3953 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
3954 bytep[i++] = request->u.ata.command;
3959 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
3961 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
3963 mtx_lock(&hpktp->mtx);
3965 struct host_packet *hp =
3966 malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
3968 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
3972 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
3974 mtx_unlock(&hpktp->mtx);
3978 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
3980 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
3981 struct host_packet *hp;
3983 mtx_lock(&hpktp->mtx);
3984 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
3985 TAILQ_REMOVE(&hpktp->queue, hp, chain);
3986 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
3991 mtx_unlock(&hpktp->mtx);
3996 * ServerWorks chipset support functions
3999 ata_serverworks_ident(device_t dev)
4001 struct ata_pci_controller *ctlr = device_get_softc(dev);
4002 struct ata_chip_id *idx;
4003 static struct ata_chip_id ids[] =
4004 {{ ATA_ROSB4, 0x00, SWKS33, 0, ATA_UDMA2, "ROSB4" },
4005 { ATA_CSB5, 0x92, SWKS100, 0, ATA_UDMA5, "CSB5" },
4006 { ATA_CSB5, 0x00, SWKS66, 0, ATA_UDMA4, "CSB5" },
4007 { ATA_CSB6, 0x00, SWKS100, 0, ATA_UDMA5, "CSB6" },
4008 { ATA_CSB6_1, 0x00, SWKS66, 0, ATA_UDMA4, "CSB6" },
4009 { ATA_HT1000, 0x00, SWKS100, 0, ATA_UDMA5, "HT1000" },
4010 { ATA_HT1000_S1, 0x00, SWKS100, 4, ATA_SA150, "HT1000" },
4011 { ATA_HT1000_S2, 0x00, SWKSMIO, 4, ATA_SA150, "HT1000" },
4012 { ATA_K2, 0x00, SWKSMIO, 4, ATA_SA150, "K2" },
4013 { ATA_FRODO4, 0x00, SWKSMIO, 4, ATA_SA150, "Frodo4" },
4014 { ATA_FRODO8, 0x00, SWKSMIO, 8, ATA_SA150, "Frodo8" },
4015 { 0, 0, 0, 0, 0, 0}};
4018 if (!(idx = ata_match_chip(dev, ids)))
4021 sprintf(buffer, "ServerWorks %s %s controller",
4022 idx->text, ata_mode2str(idx->max_dma));
4023 device_set_desc_copy(dev, buffer);
4025 ctlr->chipinit = ata_serverworks_chipinit;
4030 ata_serverworks_chipinit(device_t dev)
4032 struct ata_pci_controller *ctlr = device_get_softc(dev);
4034 if (ata_setup_interrupt(dev))
4037 if (ctlr->chip->cfg1 == SWKSMIO) {
4038 ctlr->r_type2 = SYS_RES_MEMORY;
4039 ctlr->r_rid2 = PCIR_BAR(5);
4040 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4041 &ctlr->r_rid2, RF_ACTIVE)))
4044 ctlr->channels = ctlr->chip->cfg2;
4045 ctlr->allocate = ata_serverworks_allocate;
4046 ctlr->setmode = ata_sata_setmode;
4049 else if (ctlr->chip->cfg1 == SWKS33) {
4053 /* locate the ISA part in the southbridge and enable UDMA33 */
4054 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
4055 for (i = 0; i < nchildren; i++) {
4056 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
4057 pci_write_config(children[i], 0x64,
4058 (pci_read_config(children[i], 0x64, 4) &
4059 ~0x00002000) | 0x00004000, 4);
4063 free(children, M_TEMP);
4067 pci_write_config(dev, 0x5a,
4068 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
4069 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
4071 ctlr->setmode = ata_serverworks_setmode;
4076 ata_serverworks_allocate(device_t dev)
4078 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4079 struct ata_channel *ch = device_get_softc(dev);
4083 ch_offset = ch->unit * 0x100;
4085 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
4086 ch->r_io[i].res = ctlr->r_res2;
4088 /* setup ATA registers */
4089 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
4090 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x04;
4091 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
4092 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
4093 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
4094 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
4095 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
4096 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1c;
4097 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x20;
4098 ata_default_registers(dev);
4100 /* setup DMA registers */
4101 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x30;
4102 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x32;
4103 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x34;
4105 /* setup SATA registers */
4106 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x40;
4107 ch->r_io[ATA_SERROR].offset = ch_offset + 0x44;
4108 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x48;
4110 ch->flags |= ATA_NO_SLAVE;
4116 ata_serverworks_setmode(device_t dev, int mode)
4118 device_t gparent = GRANDPARENT(dev);
4119 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4120 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4121 struct ata_device *atadev = device_get_softc(dev);
4122 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4123 int offset = (devno ^ 0x01) << 3;
4125 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
4126 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
4127 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
4129 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4131 mode = ata_check_80pin(dev, mode);
4133 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4136 device_printf(dev, "%ssetting %s on %s chip\n",
4137 (error) ? "FAILURE " : "",
4138 ata_mode2str(mode), ctlr->chip->text);
4140 if (mode >= ATA_UDMA0) {
4141 pci_write_config(gparent, 0x56,
4142 (pci_read_config(gparent, 0x56, 2) &
4143 ~(0xf << (devno << 2))) |
4144 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
4145 pci_write_config(gparent, 0x54,
4146 pci_read_config(gparent, 0x54, 1) |
4147 (0x01 << devno), 1);
4148 pci_write_config(gparent, 0x44,
4149 (pci_read_config(gparent, 0x44, 4) &
4150 ~(0xff << offset)) |
4151 (dmatimings[2] << offset), 4);
4153 else if (mode >= ATA_WDMA0) {
4154 pci_write_config(gparent, 0x54,
4155 pci_read_config(gparent, 0x54, 1) &
4156 ~(0x01 << devno), 1);
4157 pci_write_config(gparent, 0x44,
4158 (pci_read_config(gparent, 0x44, 4) &
4159 ~(0xff << offset)) |
4160 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
4163 pci_write_config(gparent, 0x54,
4164 pci_read_config(gparent, 0x54, 1) &
4165 ~(0x01 << devno), 1);
4167 pci_write_config(gparent, 0x40,
4168 (pci_read_config(gparent, 0x40, 4) &
4169 ~(0xff << offset)) |
4170 (piotimings[ata_mode2idx(mode)] << offset), 4);
4171 atadev->mode = mode;
4177 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
4180 ata_sii_ident(device_t dev)
4182 struct ata_pci_controller *ctlr = device_get_softc(dev);
4183 struct ata_chip_id *idx;
4184 static struct ata_chip_id ids[] =
4185 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
4186 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
4187 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
4188 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
4189 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
4190 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
4191 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
4192 { ATA_SII3124, 0x00, SIIPRBIO, SII4CH, ATA_SA300, "SiI 3124" },
4193 { ATA_SII3132, 0x00, SIIPRBIO, 0, ATA_SA300, "SiI 3132" },
4194 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
4195 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
4196 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
4197 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
4198 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
4199 { 0, 0, 0, 0, 0, 0}};
4202 if (!(idx = ata_match_chip(dev, ids)))
4205 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
4206 device_set_desc_copy(dev, buffer);
4208 ctlr->chipinit = ata_sii_chipinit;
4213 ata_sii_chipinit(device_t dev)
4215 struct ata_pci_controller *ctlr = device_get_softc(dev);
4217 if (ata_setup_interrupt(dev))
4220 switch (ctlr->chip->cfg1) {
4222 ctlr->r_type1 = SYS_RES_MEMORY;
4223 ctlr->r_rid1 = PCIR_BAR(0);
4224 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
4225 &ctlr->r_rid1, RF_ACTIVE)))
4228 ctlr->r_rid2 = PCIR_BAR(2);
4229 ctlr->r_type2 = SYS_RES_MEMORY;
4230 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4231 &ctlr->r_rid2, RF_ACTIVE))){
4232 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
4235 ctlr->allocate = ata_siiprb_allocate;
4236 ctlr->reset = ata_siiprb_reset;
4237 ctlr->dmainit = ata_siiprb_dmainit;
4238 ctlr->setmode = ata_sata_setmode;
4239 ctlr->channels = (ctlr->chip->cfg2 == SII4CH) ? 4 : 2;
4241 /* reset controller */
4242 ATA_OUTL(ctlr->r_res1, 0x0040, 0x80000000);
4244 ATA_OUTL(ctlr->r_res1, 0x0040, 0x0000000f);
4246 /* enable PCI interrupt */
4247 pci_write_config(dev, PCIR_COMMAND,
4248 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
4252 ctlr->r_type2 = SYS_RES_MEMORY;
4253 ctlr->r_rid2 = PCIR_BAR(5);
4254 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4255 &ctlr->r_rid2, RF_ACTIVE)))
4258 if (ctlr->chip->cfg2 & SIISETCLK) {
4259 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
4260 pci_write_config(dev, 0x8a,
4261 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
4262 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
4263 device_printf(dev, "%s could not set ATA133 clock\n",
4267 /* if we have 4 channels enable the second set */
4268 if (ctlr->chip->cfg2 & SII4CH) {
4269 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
4273 /* dont block interrupts from any channel */
4274 pci_write_config(dev, 0x48,
4275 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
4277 /* enable PCI interrupt as BIOS might not */
4278 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
4280 ctlr->allocate = ata_sii_allocate;
4281 if (ctlr->chip->max_dma >= ATA_SA150) {
4282 ctlr->reset = ata_sii_reset;
4283 ctlr->setmode = ata_sata_setmode;
4286 ctlr->setmode = ata_sii_setmode;
4290 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
4291 device_printf(dev, "HW has secondary channel disabled\n");
4295 /* enable interrupt as BIOS might not */
4296 pci_write_config(dev, 0x71, 0x01, 1);
4298 ctlr->allocate = ata_cmd_allocate;
4299 ctlr->setmode = ata_cmd_setmode;
4306 ata_cmd_allocate(device_t dev)
4308 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4309 struct ata_channel *ch = device_get_softc(dev);
4311 /* setup the usual register normal pci style */
4312 if (ata_pci_allocate(dev))
4315 if (ctlr->chip->cfg2 & SIIINTR)
4316 ch->hw.status = ata_cmd_status;
4322 ata_cmd_status(device_t dev)
4324 struct ata_channel *ch = device_get_softc(dev);
4327 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
4328 (ch->unit ? 0x08 : 0x04))) {
4329 pci_write_config(device_get_parent(ch->dev), 0x71,
4330 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
4331 return ata_pci_status(dev);
4337 ata_cmd_setmode(device_t dev, int mode)
4339 device_t gparent = GRANDPARENT(dev);
4340 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4341 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4342 struct ata_device *atadev = device_get_softc(dev);
4343 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4346 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4348 mode = ata_check_80pin(dev, mode);
4350 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4353 device_printf(dev, "%ssetting %s on %s chip\n",
4354 (error) ? "FAILURE " : "",
4355 ata_mode2str(mode), ctlr->chip->text);
4357 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
4358 int ureg = ch->unit ? 0x7b : 0x73;
4360 if (mode >= ATA_UDMA0) {
4361 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
4362 { 0x11, 0x42 }, { 0x25, 0x8a },
4363 { 0x15, 0x4a }, { 0x05, 0x0a } };
4365 u_int8_t umode = pci_read_config(gparent, ureg, 1);
4367 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
4368 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
4369 pci_write_config(gparent, ureg, umode, 1);
4371 else if (mode >= ATA_WDMA0) {
4372 int dmatimings[] = { 0x87, 0x32, 0x3f };
4374 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
4375 pci_write_config(gparent, ureg,
4376 pci_read_config(gparent, ureg, 1) &
4377 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4380 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
4381 pci_write_config(gparent, treg,
4382 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
4383 pci_write_config(gparent, ureg,
4384 pci_read_config(gparent, ureg, 1) &
4385 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4387 atadev->mode = mode;
4392 ata_sii_allocate(device_t dev)
4394 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4395 struct ata_channel *ch = device_get_softc(dev);
4396 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
4399 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
4400 ch->r_io[i].res = ctlr->r_res2;
4401 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
4403 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
4404 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
4405 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
4406 ata_default_registers(dev);
4408 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
4409 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
4410 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
4411 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
4412 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
4413 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
4415 if (ctlr->chip->max_dma >= ATA_SA150) {
4416 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4417 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
4418 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4419 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
4420 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4421 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
4422 ch->flags |= ATA_NO_SLAVE;
4424 /* enable PHY state change interrupt */
4425 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
4428 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
4429 /* work around errata in early chips */
4430 ch->dma->boundary = 16 * DEV_BSIZE;
4431 ch->dma->segsize = 15 * DEV_BSIZE;
4435 ch->hw.status = ata_sii_status;
4440 ata_sii_status(device_t dev)
4442 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4443 struct ata_channel *ch = device_get_softc(dev);
4444 int offset0 = ((ch->unit & 1) << 3) + ((ch->unit & 2) << 8);
4445 int offset1 = ((ch->unit & 1) << 6) + ((ch->unit & 2) << 8);
4447 /* do we have any PHY events ? */
4448 if (ctlr->chip->max_dma >= ATA_SA150 &&
4449 (ATA_INL(ctlr->r_res2, 0x10 + offset0) & 0x00000010))
4450 ata_sata_phy_check_events(dev);
4452 if (ATA_INL(ctlr->r_res2, 0xa0 + offset1) & 0x00000800)
4453 return ata_pci_status(dev);
4459 ata_sii_reset(device_t dev)
4461 if (ata_sata_phy_reset(dev))
4462 ata_generic_reset(dev);
4466 ata_sii_setmode(device_t dev, int mode)
4468 device_t gparent = GRANDPARENT(dev);
4469 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4470 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4471 struct ata_device *atadev = device_get_softc(dev);
4472 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
4473 int mreg = ch->unit ? 0x84 : 0x80;
4474 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
4475 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
4478 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4480 if (ctlr->chip->cfg2 & SIISETCLK) {
4481 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
4482 (ch->unit ? 0x02 : 0x01))) {
4483 ata_print_cable(dev, "controller");
4488 mode = ata_check_80pin(dev, mode);
4490 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4493 device_printf(dev, "%ssetting %s on %s chip\n",
4494 (error) ? "FAILURE " : "",
4495 ata_mode2str(mode), ctlr->chip->text);
4499 if (mode >= ATA_UDMA0) {
4500 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
4501 u_int8_t ureg = 0xac + rego;
4503 pci_write_config(gparent, mreg,
4504 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
4505 pci_write_config(gparent, ureg,
4506 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
4507 udmatimings[mode & ATA_MODE_MASK], 1);
4510 else if (mode >= ATA_WDMA0) {
4511 u_int8_t dreg = 0xa8 + rego;
4512 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
4514 pci_write_config(gparent, mreg,
4515 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
4516 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
4520 u_int8_t preg = 0xa4 + rego;
4521 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
4523 pci_write_config(gparent, mreg,
4524 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
4525 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
4527 atadev->mode = mode;
4531 struct ata_siiprb_dma_prdentry {
4537 struct ata_siiprb_ata_command {
4538 u_int32_t reserved0;
4539 struct ata_siiprb_dma_prdentry prd[126];
4542 struct ata_siiprb_atapi_command {
4544 struct ata_siiprb_dma_prdentry prd[125];
4547 struct ata_siiprb_command {
4549 u_int16_t protocol_override;
4550 u_int32_t transfer_count;
4553 struct ata_siiprb_ata_command ata;
4554 struct ata_siiprb_atapi_command atapi;
4559 ata_siiprb_allocate(device_t dev)
4561 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4562 struct ata_channel *ch = device_get_softc(dev);
4563 int offset = ch->unit * 0x2000;
4565 /* set the SATA resources */
4566 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4567 ch->r_io[ATA_SSTATUS].offset = 0x1f04 + offset;
4568 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4569 ch->r_io[ATA_SERROR].offset = 0x1f08 + offset;
4570 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4571 ch->r_io[ATA_SCONTROL].offset = 0x1f00 + offset;
4572 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
4573 ch->r_io[ATA_SACTIVE].offset = 0x1f0c + offset;
4575 ch->hw.begin_transaction = ata_siiprb_begin_transaction;
4576 ch->hw.end_transaction = ata_siiprb_end_transaction;
4577 ch->hw.status = ata_siiprb_status;
4578 ch->hw.command = NULL; /* not used here */
4583 ata_siiprb_status(device_t dev)
4585 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4586 struct ata_channel *ch = device_get_softc(dev);
4587 int offset = ch->unit * 0x2000;
4589 if ((ATA_INL(ctlr->r_res1, 0x0044) & (1 << ch->unit))) {
4590 u_int32_t istatus = ATA_INL(ctlr->r_res2, 0x1008 + offset);
4592 /* do we have any PHY events ? */
4593 ata_sata_phy_check_events(dev);
4595 /* clear interrupt(s) */
4596 ATA_OUTL(ctlr->r_res2, 0x1008 + offset, istatus);
4598 /* do we have any device action ? */
4599 return (istatus & 0x00000001);
4605 ata_siiprb_begin_transaction(struct ata_request *request)
4607 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
4608 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4609 struct ata_siiprb_command *prb;
4610 int offset = ch->unit * 0x2000;
4614 /* check for 48 bit access and convert if needed */
4615 ata_modify_if_48bit(request);
4617 /* get a piece of the workspace for this request */
4618 prb = (struct ata_siiprb_command *)
4619 (ch->dma->work + (sizeof(struct ata_siiprb_command) * tag));
4621 /* set basic prd options ata/atapi etc etc */
4622 bzero(prb, sizeof(struct ata_siiprb_command));
4624 /* setup the FIS for this request */
4625 if (!ata_request2fis_h2d(request, &prb->fis[0])) {
4626 device_printf(request->dev, "setting up SATA FIS failed\n");
4627 request->result = EIO;
4628 return ATA_OP_FINISHED;
4631 /* if request moves data setup and load SG list */
4632 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
4633 struct ata_siiprb_dma_prdentry *prd;
4635 if (request->flags & ATA_R_ATAPI)
4636 prd = &prb->u.atapi.prd[0];
4638 prd = &prb->u.ata.prd[0];
4639 if (ch->dma->load(ch->dev, request->data, request->bytecount,
4640 request->flags & ATA_R_READ, prd, &dummy)) {
4641 device_printf(request->dev, "setting up DMA failed\n");
4642 request->result = EIO;
4643 return ATA_OP_FINISHED;
4647 /* activate the prb */
4648 prb_bus = ch->dma->work_bus + (sizeof(struct ata_siiprb_command) * tag);
4649 ATA_OUTL(ctlr->r_res2,
4650 0x1c00 + offset + (tag * sizeof(u_int64_t)), prb_bus);
4651 ATA_OUTL(ctlr->r_res2,
4652 0x1c04 + offset + (tag * sizeof(u_int64_t)), prb_bus>>32);
4654 /* start the timeout */
4655 callout_reset(&request->callout, request->timeout * hz,
4656 (timeout_t*)ata_timeout, request);
4657 return ATA_OP_CONTINUES;
4661 ata_siiprb_end_transaction(struct ata_request *request)
4663 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
4664 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4665 struct ata_siiprb_command *prb;
4666 int offset = ch->unit * 0x2000;
4669 /* kill the timeout */
4670 callout_stop(&request->callout);
4672 prb = (struct ata_siiprb_command *)
4673 ((u_int8_t *)rman_get_virtual(ctlr->r_res2) + (tag << 7) + offset);
4675 /* if error status get details */
4676 request->status = prb->fis[2];
4677 if (request->status & ATA_S_ERROR)
4678 request->error = prb->fis[3];
4680 /* update progress */
4681 if (!(request->status & ATA_S_ERROR) && !(request->flags & ATA_R_TIMEOUT)) {
4682 if (request->flags & ATA_R_READ)
4683 request->donecount = prb->transfer_count;
4685 request->donecount = request->bytecount;
4688 /* any controller errors flagged ? */
4689 if ((error = ATA_INL(ctlr->r_res2, 0x1024 + offset))) {
4690 printf("ata_siiprb_end_transaction %s error=%08x\n",
4691 ata_cmd2str(request), error);
4694 /* release SG list etc */
4695 ch->dma->unload(ch->dev);
4697 return ATA_OP_FINISHED;
4701 ata_siiprb_reset(device_t dev)
4703 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4704 struct ata_channel *ch = device_get_softc(dev);
4705 int offset = ch->unit * 0x2000;
4706 struct ata_siiprb_command *prb;
4708 u_int32_t status, signature;
4709 int timeout, tag = 0;
4711 /* reset channel HW */
4712 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000001);
4714 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000001);
4717 /* poll for channel ready */
4718 for (timeout = 0; timeout < 1000; timeout++) {
4719 if ((status = ATA_INL(ctlr->r_res2, 0x1000 + offset)) & 0x00040000)
4723 if (timeout >= 1000) {
4724 device_printf(ch->dev, "channel HW reset timeout reset failure\n");
4729 device_printf(ch->dev, "channel HW reset time=%dms\n", timeout * 1);
4732 if (!ata_sata_phy_reset(dev)) {
4734 device_printf(ch->dev, "phy reset found no device\n");
4739 /* get a piece of the workspace for a soft reset request */
4740 prb = (struct ata_siiprb_command *)
4741 (ch->dma->work + (sizeof(struct ata_siiprb_command) * tag));
4742 bzero(prb, sizeof(struct ata_siiprb_command));
4743 prb->control = htole16(0x0080);
4745 /* activate the soft reset prb */
4746 prb_bus = ch->dma->work_bus + (sizeof(struct ata_siiprb_command) * tag);
4747 ATA_OUTL(ctlr->r_res2,
4748 0x1c00 + offset + (tag * sizeof(u_int64_t)), prb_bus);
4749 ATA_OUTL(ctlr->r_res2,
4750 0x1c04 + offset + (tag * sizeof(u_int64_t)), prb_bus>>32);
4752 /* poll for channel ready */
4753 for (timeout = 0; timeout < 1000; timeout++) {
4755 if ((status = ATA_INL(ctlr->r_res2, 0x1008 + offset)) & 0x00010000)
4758 if (timeout >= 1000) {
4759 device_printf(ch->dev, "reset timeout - no device found\n");
4764 device_printf(ch->dev, "soft reset exec time=%dms status=%08x\n",
4767 /* find out whats there */
4768 prb = (struct ata_siiprb_command *)
4769 ((u_int8_t *)rman_get_virtual(ctlr->r_res2) + (tag << 7) + offset);
4771 prb->fis[12]|(prb->fis[4]<<8)|(prb->fis[5]<<16)|(prb->fis[6]<<24);
4773 device_printf(ch->dev, "signature=%08x\n", signature);
4774 switch (signature) {
4776 ch->devices = ATA_ATAPI_MASTER;
4777 device_printf(ch->dev, "SATA ATAPI devices not supported yet\n");
4781 ch->devices = ATA_PORTMULTIPLIER;
4782 device_printf(ch->dev, "Portmultipliers not supported yet\n");
4786 ch->devices = ATA_ATA_MASTER;
4793 /* clear interrupt(s) */
4794 ATA_OUTL(ctlr->r_res2, 0x1008 + offset, 0x000008ff);
4796 /* require explicit interrupt ack */
4797 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000008);
4800 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000400);
4802 /* enable interrupts wanted */
4803 ATA_OUTL(ctlr->r_res2, 0x1010 + offset, 0x000000ff);
4807 ata_siiprb_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
4809 struct ata_dmasetprd_args *args = xsc;
4810 struct ata_siiprb_dma_prdentry *prd = args->dmatab;
4813 if ((args->error = error))
4816 for (i = 0; i < nsegs; i++) {
4817 prd[i].addr = htole64(segs[i].ds_addr);
4818 prd[i].count = htole32(segs[i].ds_len);
4820 prd[i - 1].control = htole32(ATA_DMA_EOT);
4824 ata_siiprb_dmainit(device_t dev)
4826 struct ata_channel *ch = device_get_softc(dev);
4830 /* note start and stop are not used here */
4831 ch->dma->setprd = ata_siiprb_dmasetprd;
4832 ch->dma->max_address = BUS_SPACE_MAXADDR;
4838 * Silicon Integrated Systems Corp. (SiS) chipset support functions
4841 ata_sis_ident(device_t dev)
4843 struct ata_pci_controller *ctlr = device_get_softc(dev);
4844 struct ata_chip_id *idx;
4845 static struct ata_chip_id ids[] =
4846 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "182" }, /* south */
4847 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "181" }, /* south */
4848 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "180" }, /* south */
4849 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "965" }, /* south */
4850 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "964" }, /* south */
4851 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "963" }, /* south */
4852 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "962" }, /* south */
4854 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "745" }, /* 1chip */
4855 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "735" }, /* 1chip */
4856 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "733" }, /* 1chip */
4857 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "730" }, /* 1chip */
4859 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "635" }, /* 1chip */
4860 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "633" }, /* unknown */
4861 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "630S"}, /* 1chip */
4862 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "630" }, /* 1chip */
4863 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "620" }, /* 1chip */
4865 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "550" },
4866 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "540" },
4867 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "530" },
4869 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "5513" },
4870 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "5513" },
4871 { 0, 0, 0, 0, 0, 0 }};
4875 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
4878 if (idx->cfg2 && !found) {
4879 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
4881 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
4882 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
4884 idx->cfg1 = SIS133NEW;
4885 idx->max_dma = ATA_UDMA6;
4886 sprintf(buffer, "SiS 962/963 %s controller",
4887 ata_mode2str(idx->max_dma));
4889 pci_write_config(dev, 0x57, reg57, 1);
4891 if (idx->cfg2 && !found) {
4892 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
4894 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
4895 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
4896 struct ata_chip_id id[] =
4897 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
4900 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
4901 idx->cfg1 = SIS133OLD;
4902 idx->max_dma = ATA_UDMA6;
4905 idx->cfg1 = SIS100NEW;
4906 idx->max_dma = ATA_UDMA5;
4908 sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
4910 pci_write_config(dev, 0x4a, reg4a, 1);
4913 sprintf(buffer,"SiS %s %s controller",
4914 idx->text, ata_mode2str(idx->max_dma));
4916 device_set_desc_copy(dev, buffer);
4918 ctlr->chipinit = ata_sis_chipinit;
4923 ata_sis_chipinit(device_t dev)
4925 struct ata_pci_controller *ctlr = device_get_softc(dev);
4927 if (ata_setup_interrupt(dev))
4930 switch (ctlr->chip->cfg1) {
4935 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
4939 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
4942 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
4943 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
4946 ctlr->r_type2 = SYS_RES_IOPORT;
4947 ctlr->r_rid2 = PCIR_BAR(5);
4948 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4949 &ctlr->r_rid2, RF_ACTIVE))) {
4950 ctlr->allocate = ata_sis_allocate;
4951 ctlr->reset = ata_sis_reset;
4953 /* enable PCI interrupt */
4954 pci_write_config(dev, PCIR_COMMAND,
4955 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
4957 ctlr->setmode = ata_sata_setmode;
4962 ctlr->setmode = ata_sis_setmode;
4967 ata_sis_allocate(device_t dev)
4969 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4970 struct ata_channel *ch = device_get_softc(dev);
4971 int offset = ch->unit << ((ctlr->chip->chipid == ATA_SIS182) ? 5 : 6);
4973 /* setup the usual register normal pci style */
4974 if (ata_pci_allocate(dev))
4977 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4978 ch->r_io[ATA_SSTATUS].offset = 0x00 + offset;
4979 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4980 ch->r_io[ATA_SERROR].offset = 0x04 + offset;
4981 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4982 ch->r_io[ATA_SCONTROL].offset = 0x08 + offset;
4983 ch->flags |= ATA_NO_SLAVE;
4985 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
4986 /* XXX SOS unknown how to enable PHY state change interrupt */
4991 ata_sis_reset(device_t dev)
4993 if (ata_sata_phy_reset(dev))
4994 ata_generic_reset(dev);
4998 ata_sis_setmode(device_t dev, int mode)
5000 device_t gparent = GRANDPARENT(dev);
5001 struct ata_pci_controller *ctlr = device_get_softc(gparent);
5002 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5003 struct ata_device *atadev = device_get_softc(dev);
5004 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
5007 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
5009 if (ctlr->chip->cfg1 == SIS133NEW) {
5010 if (mode > ATA_UDMA2 &&
5011 pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
5012 ata_print_cable(dev, "controller");
5017 if (mode > ATA_UDMA2 &&
5018 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
5019 ata_print_cable(dev, "controller");
5024 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5027 device_printf(dev, "%ssetting %s on %s chip\n",
5028 (error) ? "FAILURE " : "",
5029 ata_mode2str(mode), ctlr->chip->text);
5031 switch (ctlr->chip->cfg1) {
5033 u_int32_t timings[] =
5034 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
5035 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
5036 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
5039 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
5040 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
5044 u_int16_t timings[] =
5045 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
5046 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
5048 u_int16_t reg = 0x40 + (devno << 1);
5050 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5054 u_int16_t timings[] =
5055 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
5056 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
5057 u_int16_t reg = 0x40 + (devno << 1);
5059 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5065 u_int16_t timings[] =
5066 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
5067 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
5068 u_int16_t reg = 0x40 + (devno << 1);
5070 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5074 atadev->mode = mode;
5079 /* VIA Technologies Inc. chipset support functions */
5081 ata_via_ident(device_t dev)
5083 struct ata_pci_controller *ctlr = device_get_softc(dev);
5084 struct ata_chip_id *idx;
5085 static struct ata_chip_id ids[] =
5086 {{ ATA_VIA82C586, 0x02, VIA33, 0x00, ATA_UDMA2, "82C586B" },
5087 { ATA_VIA82C586, 0x00, VIA33, 0x00, ATA_WDMA2, "82C586" },
5088 { ATA_VIA82C596, 0x12, VIA66, VIACLK, ATA_UDMA4, "82C596B" },
5089 { ATA_VIA82C596, 0x00, VIA33, 0x00, ATA_UDMA2, "82C596" },
5090 { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "82C686B"},
5091 { ATA_VIA82C686, 0x10, VIA66, VIACLK, ATA_UDMA4, "82C686A" },
5092 { ATA_VIA82C686, 0x00, VIA33, 0x00, ATA_UDMA2, "82C686" },
5093 { ATA_VIA8231, 0x00, VIA100, VIABUG, ATA_UDMA5, "8231" },
5094 { ATA_VIA8233, 0x00, VIA100, 0x00, ATA_UDMA5, "8233" },
5095 { ATA_VIA8233C, 0x00, VIA100, 0x00, ATA_UDMA5, "8233C" },
5096 { ATA_VIA8233A, 0x00, VIA133, 0x00, ATA_UDMA6, "8233A" },
5097 { ATA_VIA8235, 0x00, VIA133, 0x00, ATA_UDMA6, "8235" },
5098 { ATA_VIA8237, 0x00, VIA133, 0x00, ATA_UDMA6, "8237" },
5099 { ATA_VIA8237A, 0x00, VIA133, 0x00, ATA_UDMA6, "8237A" },
5100 { ATA_VIA8237S, 0x00, VIA133, 0x00, ATA_UDMA6, "8237S" },
5101 { ATA_VIA8251, 0x00, VIA133, 0x00, ATA_UDMA6, "8251" },
5102 { 0, 0, 0, 0, 0, 0 }};
5103 static struct ata_chip_id new_ids[] =
5104 {{ ATA_VIA6410, 0x00, 0, 0x00, ATA_UDMA6, "6410" },
5105 { ATA_VIA6420, 0x00, 7, 0x00, ATA_SA150, "6420" },
5106 { ATA_VIA6421, 0x00, 6, VIABAR, ATA_SA150, "6421" },
5107 { ATA_VIA8237A, 0x00, 7, 0x00, ATA_SA150, "8237A" },
5108 { ATA_VIA8237S, 0x00, 7, 0x00, ATA_SA150, "8237S" },
5109 { ATA_VIA8251, 0x00, 0, VIAAHCI, ATA_SA300, "8251" },
5110 { 0, 0, 0, 0, 0, 0 }};
5113 if (pci_get_devid(dev) == ATA_VIA82C571) {
5114 if (!(idx = ata_find_chip(dev, ids, -99)))
5118 if (!(idx = ata_match_chip(dev, new_ids)))
5122 sprintf(buffer, "VIA %s %s controller",
5123 idx->text, ata_mode2str(idx->max_dma));
5124 device_set_desc_copy(dev, buffer);
5126 ctlr->chipinit = ata_via_chipinit;
5131 ata_via_chipinit(device_t dev)
5133 struct ata_pci_controller *ctlr = device_get_softc(dev);
5135 if (ata_setup_interrupt(dev))
5138 if (ctlr->chip->max_dma >= ATA_SA150) {
5139 if (ctlr->chip->cfg2 == VIAAHCI) {
5140 ctlr->r_type2 = SYS_RES_MEMORY;
5141 ctlr->r_rid2 = PCIR_BAR(5);
5142 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
5145 return ata_ahci_chipinit(dev);
5148 ctlr->r_type2 = SYS_RES_IOPORT;
5149 ctlr->r_rid2 = PCIR_BAR(5);
5150 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
5151 &ctlr->r_rid2, RF_ACTIVE))) {
5152 ctlr->allocate = ata_via_allocate;
5153 ctlr->reset = ata_via_reset;
5155 /* enable PCI interrupt */
5156 pci_write_config(dev, PCIR_COMMAND,
5157 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
5160 if (ctlr->chip->cfg2 & VIABAR) {
5162 ctlr->setmode = ata_via_setmode;
5165 ctlr->setmode = ata_sata_setmode;
5169 /* prepare for ATA-66 on the 82C686a and 82C596b */
5170 if (ctlr->chip->cfg2 & VIACLK)
5171 pci_write_config(dev, 0x50, 0x030b030b, 4);
5173 /* the southbridge might need the data corruption fix */
5174 if (ctlr->chip->cfg2 & VIABUG)
5175 ata_via_southbridge_fixup(dev);
5177 /* set fifo configuration half'n'half */
5178 pci_write_config(dev, 0x43,
5179 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
5181 /* set status register read retry */
5182 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
5184 /* set DMA read & end-of-sector fifo flush */
5185 pci_write_config(dev, 0x46,
5186 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
5188 /* set sector size */
5189 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
5190 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
5192 ctlr->setmode = ata_via_family_setmode;
5197 ata_via_allocate(device_t dev)
5199 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5200 struct ata_channel *ch = device_get_softc(dev);
5202 /* newer SATA chips has resources in one BAR for each channel */
5203 if (ctlr->chip->cfg2 & VIABAR) {
5204 struct resource *r_io;
5207 rid = PCIR_BAR(ch->unit);
5208 if (!(r_io = bus_alloc_resource_any(device_get_parent(dev),
5213 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
5214 ch->r_io[i].res = r_io;
5215 ch->r_io[i].offset = i;
5217 ch->r_io[ATA_CONTROL].res = r_io;
5218 ch->r_io[ATA_CONTROL].offset = 2 + ATA_IOSIZE;
5219 ch->r_io[ATA_IDX_ADDR].res = r_io;
5220 ata_default_registers(dev);
5221 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
5222 ch->r_io[i].res = ctlr->r_res1;
5223 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
5230 /* setup the usual register normal pci style */
5231 if (ata_pci_allocate(dev))
5235 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
5236 ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
5237 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
5238 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
5239 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
5240 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
5241 ch->flags |= ATA_NO_SLAVE;
5243 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
5244 /* XXX SOS unknown how to enable PHY state change interrupt */
5249 ata_via_reset(device_t dev)
5251 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5252 struct ata_channel *ch = device_get_softc(dev);
5254 if ((ctlr->chip->cfg2 & VIABAR) && (ch->unit > 1))
5255 ata_generic_reset(dev);
5257 if (ata_sata_phy_reset(dev))
5258 ata_generic_reset(dev);
5262 ata_via_setmode(device_t dev, int mode)
5264 device_t gparent = GRANDPARENT(dev);
5265 struct ata_pci_controller *ctlr = device_get_softc(gparent);
5266 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5267 struct ata_device *atadev = device_get_softc(dev);
5270 if ((ctlr->chip->cfg2 & VIABAR) && (ch->unit > 1)) {
5271 u_int8_t pio_timings[] = { 0xa8, 0x65, 0x65, 0x32, 0x20,
5273 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
5274 u_int8_t dma_timings[] = { 0xee, 0xe8, 0xe6, 0xe4, 0xe2, 0xe1, 0xe0 };
5276 mode = ata_check_80pin(dev, ata_limit_mode(dev, mode, ATA_UDMA6));
5277 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5279 device_printf(dev, "%ssetting %s on %s chip\n",
5280 (error) ? "FAILURE " : "", ata_mode2str(mode),
5283 pci_write_config(gparent, 0xab, pio_timings[ata_mode2idx(mode)], 1);
5284 if (mode >= ATA_UDMA0)
5285 pci_write_config(gparent, 0xb3,
5286 dma_timings[mode & ATA_MODE_MASK], 1);
5287 atadev->mode = mode;
5291 ata_sata_setmode(dev, mode);
5295 ata_via_southbridge_fixup(device_t dev)
5300 if (device_get_children(device_get_parent(dev), &children, &nchildren))
5303 for (i = 0; i < nchildren; i++) {
5304 if (pci_get_devid(children[i]) == ATA_VIA8363 ||
5305 pci_get_devid(children[i]) == ATA_VIA8371 ||
5306 pci_get_devid(children[i]) == ATA_VIA8662 ||
5307 pci_get_devid(children[i]) == ATA_VIA8361) {
5308 u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
5310 if ((reg76 & 0xf0) != 0xd0) {
5312 "Correcting VIA config for southbridge data corruption bug\n");
5313 pci_write_config(children[i], 0x75, 0x80, 1);
5314 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
5319 free(children, M_TEMP);
5323 /* common code for VIA, AMD & nVidia */
5325 ata_via_family_setmode(device_t dev, int mode)
5327 device_t gparent = GRANDPARENT(dev);
5328 struct ata_pci_controller *ctlr = device_get_softc(gparent);
5329 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5330 struct ata_device *atadev = device_get_softc(dev);
5331 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
5332 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
5334 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* VIA ATA33 */
5335 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* VIA ATA66 */
5336 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* VIA ATA100 */
5337 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
5338 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }}; /* AMD/nVIDIA */
5339 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
5340 int reg = 0x53 - devno;
5343 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
5345 if (ctlr->chip->cfg2 & AMDCABLE) {
5346 if (mode > ATA_UDMA2 &&
5347 !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
5348 ata_print_cable(dev, "controller");
5353 mode = ata_check_80pin(dev, mode);
5355 if (ctlr->chip->cfg2 & NVIDIA)
5358 if (ctlr->chip->cfg1 != VIA133)
5359 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
5361 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5364 device_printf(dev, "%ssetting %s on %s chip\n",
5365 (error) ? "FAILURE " : "", ata_mode2str(mode),
5368 if (mode >= ATA_UDMA0)
5369 pci_write_config(gparent, reg,
5370 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
5372 pci_write_config(gparent, reg, 0x8b, 1);
5373 atadev->mode = mode;
5378 /* misc functions */
5379 static struct ata_chip_id *
5380 ata_match_chip(device_t dev, struct ata_chip_id *index)
5382 while (index->chipid != 0) {
5383 if (pci_get_devid(dev) == index->chipid &&
5384 pci_get_revid(dev) >= index->chiprev)
5391 static struct ata_chip_id *
5392 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
5397 if (device_get_children(device_get_parent(dev), &children, &nchildren))
5400 while (index->chipid != 0) {
5401 for (i = 0; i < nchildren; i++) {
5402 if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
5403 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
5404 pci_get_devid(children[i]) == index->chipid &&
5405 pci_get_revid(children[i]) >= index->chiprev) {
5406 free(children, M_TEMP);
5412 free(children, M_TEMP);
5417 ata_setup_interrupt(device_t dev)
5419 struct ata_pci_controller *ctlr = device_get_softc(dev);
5420 int rid = ATA_IRQ_RID;
5422 if (!ata_legacy(dev)) {
5423 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
5424 RF_SHAREABLE | RF_ACTIVE))) {
5425 device_printf(dev, "unable to map interrupt\n");
5428 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
5429 ata_generic_intr, ctlr, &ctlr->handle))) {
5430 device_printf(dev, "unable to setup interrupt\n");
5437 struct ata_serialize {
5438 struct mtx locked_mtx;
5444 ata_serialize(device_t dev, int flags)
5446 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5447 struct ata_channel *ch = device_get_softc(dev);
5448 struct ata_serialize *serial;
5449 static int inited = 0;
5453 serial = malloc(sizeof(struct ata_serialize),
5454 M_TEMP, M_NOWAIT | M_ZERO);
5455 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
5456 serial->locked_ch = -1;
5457 serial->restart_ch = -1;
5458 device_set_ivars(ctlr->dev, serial);
5462 serial = device_get_ivars(ctlr->dev);
5464 mtx_lock(&serial->locked_mtx);
5467 if (serial->locked_ch == -1)
5468 serial->locked_ch = ch->unit;
5469 if (serial->locked_ch != ch->unit)
5470 serial->restart_ch = ch->unit;
5474 if (serial->locked_ch == ch->unit) {
5475 serial->locked_ch = -1;
5476 if (serial->restart_ch != -1) {
5477 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
5478 serial->restart_ch = -1;
5479 mtx_unlock(&serial->locked_mtx);
5490 res = serial->locked_ch;
5491 mtx_unlock(&serial->locked_mtx);
5496 ata_print_cable(device_t dev, u_int8_t *who)
5499 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
5503 ata_atapi(device_t dev)
5505 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5506 struct ata_device *atadev = device_get_softc(dev);
5508 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
5509 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
5513 ata_check_80pin(device_t dev, int mode)
5515 struct ata_device *atadev = device_get_softc(dev);
5517 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
5518 ata_print_cable(dev, "device");
5525 ata_mode2idx(int mode)
5527 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
5528 return (mode & ATA_MODE_MASK) + 8;
5529 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
5530 return (mode & ATA_MODE_MASK) + 5;
5531 return (mode & ATA_MODE_MASK) - ATA_PIO0;