2 * Copyright (c) 1998 - 2006 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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_enable(struct ata_channel *ch);
59 static void ata_sata_phy_event(void *context, int dummy);
60 static int ata_sata_connect(struct ata_channel *ch);
61 static void ata_sata_setmode(device_t dev, int mode);
62 static int ata_ahci_chipinit(device_t dev);
63 static int ata_ahci_allocate(device_t dev);
64 static int ata_ahci_status(device_t dev);
65 static int ata_ahci_begin_transaction(struct ata_request *request);
66 static int ata_ahci_end_transaction(struct ata_request *request);
67 static void ata_ahci_reset(device_t dev);
68 static void ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
69 static void ata_ahci_dmainit(device_t dev);
70 static int ata_ahci_setup_fis(u_int8_t *fis, struct ata_request *request);
71 static int ata_acard_chipinit(device_t dev);
72 static int ata_acard_allocate(device_t dev);
73 static int ata_acard_status(device_t dev);
74 static void ata_acard_850_setmode(device_t dev, int mode);
75 static void ata_acard_86X_setmode(device_t dev, int mode);
76 static int ata_ali_chipinit(device_t dev);
77 static int ata_ali_allocate(device_t dev);
78 static int ata_ali_sata_allocate(device_t dev);
79 static void ata_ali_reset(device_t dev);
80 static void ata_ali_setmode(device_t dev, int mode);
81 static int ata_amd_chipinit(device_t dev);
82 static int ata_ati_chipinit(device_t dev);
83 static void ata_ati_setmode(device_t dev, int mode);
84 static int ata_cyrix_chipinit(device_t dev);
85 static void ata_cyrix_setmode(device_t dev, int mode);
86 static int ata_cypress_chipinit(device_t dev);
87 static void ata_cypress_setmode(device_t dev, int mode);
88 static int ata_highpoint_chipinit(device_t dev);
89 static int ata_highpoint_allocate(device_t dev);
90 static void ata_highpoint_setmode(device_t dev, int mode);
91 static int ata_highpoint_check_80pin(device_t dev, int mode);
92 static int ata_intel_chipinit(device_t dev);
93 static int ata_intel_allocate(device_t dev);
94 static void ata_intel_reset(device_t dev);
95 static void ata_intel_old_setmode(device_t dev, int mode);
96 static void ata_intel_new_setmode(device_t dev, int mode);
97 static int ata_intel_31244_allocate(device_t dev);
98 static int ata_intel_31244_status(device_t dev);
99 static int ata_intel_31244_command(struct ata_request *request);
100 static void ata_intel_31244_reset(device_t dev);
101 static int ata_ite_chipinit(device_t dev);
102 static void ata_ite_setmode(device_t dev, int mode);
103 static int ata_jmicron_chipinit(device_t dev);
104 static int ata_jmicron_allocate(device_t dev);
105 static void ata_jmicron_reset(device_t dev);
106 static void ata_jmicron_dmainit(device_t dev);
107 static void ata_jmicron_setmode(device_t dev, int mode);
108 static int ata_marvell_chipinit(device_t dev);
109 static int ata_marvell_allocate(device_t dev);
110 static int ata_marvell_status(device_t dev);
111 static int ata_marvell_begin_transaction(struct ata_request *request);
112 static int ata_marvell_end_transaction(struct ata_request *request);
113 static void ata_marvell_reset(device_t dev);
114 static void ata_marvell_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
115 static void ata_marvell_dmainit(device_t dev);
116 static int ata_national_chipinit(device_t dev);
117 static void ata_national_setmode(device_t dev, int mode);
118 static int ata_nvidia_chipinit(device_t dev);
119 static int ata_nvidia_allocate(device_t dev);
120 static int ata_nvidia_status(device_t dev);
121 static void ata_nvidia_reset(device_t dev);
122 static int ata_promise_chipinit(device_t dev);
123 static int ata_promise_allocate(device_t dev);
124 static int ata_promise_status(device_t dev);
125 static int ata_promise_dmastart(device_t dev);
126 static int ata_promise_dmastop(device_t dev);
127 static void ata_promise_dmareset(device_t dev);
128 static void ata_promise_dmainit(device_t dev);
129 static void ata_promise_setmode(device_t dev, int mode);
130 static int ata_promise_tx2_allocate(device_t dev);
131 static int ata_promise_tx2_status(device_t dev);
132 static int ata_promise_mio_allocate(device_t dev);
133 static void ata_promise_mio_intr(void *data);
134 static int ata_promise_mio_status(device_t dev);
135 static int ata_promise_mio_command(struct ata_request *request);
136 static void ata_promise_mio_reset(device_t dev);
137 static void ata_promise_mio_dmainit(device_t dev);
138 static void ata_promise_mio_setmode(device_t dev, int mode);
139 static void ata_promise_sx4_intr(void *data);
140 static int ata_promise_sx4_command(struct ata_request *request);
141 static int ata_promise_apkt(u_int8_t *bytep, struct ata_request *request);
142 static void ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt);
143 static void ata_promise_next_hpkt(struct ata_pci_controller *ctlr);
144 static int ata_serverworks_chipinit(device_t dev);
145 static void ata_serverworks_setmode(device_t dev, int mode);
146 static int ata_sii_chipinit(device_t dev);
147 static int ata_cmd_allocate(device_t dev);
148 static int ata_cmd_status(device_t dev);
149 static void ata_cmd_setmode(device_t dev, int mode);
150 static int ata_sii_allocate(device_t dev);
151 static int ata_sii_status(device_t dev);
152 static void ata_sii_reset(device_t dev);
153 static void ata_sii_setmode(device_t dev, int mode);
154 static int ata_sis_chipinit(device_t dev);
155 static int ata_sis_allocate(device_t dev);
156 static void ata_sis_reset(device_t dev);
157 static void ata_sis_setmode(device_t dev, int mode);
158 static int ata_via_chipinit(device_t dev);
159 static int ata_via_allocate(device_t dev);
160 static void ata_via_reset(device_t dev);
161 static void ata_via_southbridge_fixup(device_t dev);
162 static void ata_via_family_setmode(device_t dev, int mode);
163 static struct ata_chip_id *ata_match_chip(device_t dev, struct ata_chip_id *index);
164 static struct ata_chip_id *ata_find_chip(device_t dev, struct ata_chip_id *index, int slot);
165 static int ata_setup_interrupt(device_t dev);
166 static int ata_serialize(device_t dev, int flags);
167 static void ata_print_cable(device_t dev, u_int8_t *who);
168 static int ata_atapi(device_t dev);
169 static int ata_check_80pin(device_t dev, int mode);
170 static int ata_mode2idx(int mode);
174 * generic ATA support functions
177 ata_generic_ident(device_t dev)
179 struct ata_pci_controller *ctlr = device_get_softc(dev);
181 device_set_desc(dev, "GENERIC ATA controller");
182 ctlr->chipinit = ata_generic_chipinit;
187 ata_generic_chipinit(device_t dev)
189 struct ata_pci_controller *ctlr = device_get_softc(dev);
191 if (ata_setup_interrupt(dev))
193 ctlr->setmode = ata_generic_setmode;
198 ata_generic_intr(void *data)
200 struct ata_pci_controller *ctlr = data;
201 struct ata_channel *ch;
204 for (unit = 0; unit < ctlr->channels; unit++) {
205 if ((ch = ctlr->interrupt[unit].argument))
206 ctlr->interrupt[unit].function(ch);
211 ata_generic_setmode(device_t dev, int mode)
213 struct ata_device *atadev = device_get_softc(dev);
215 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
216 mode = ata_check_80pin(dev, mode);
217 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
223 * SATA support functions
226 ata_sata_phy_enable(struct ata_channel *ch)
230 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE) {
231 ata_sata_connect(ch);
235 for (retry = 0; retry < 10; retry++) {
236 for (loop = 0; loop < 10; loop++) {
237 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_RESET);
239 if ((ATA_IDX_INL(ch, ATA_SCONTROL) &
240 ATA_SC_DET_MASK) == ATA_SC_DET_RESET)
244 for (loop = 0; loop < 10; loop++) {
245 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_IDLE |
246 ATA_SC_IPM_DIS_PARTIAL |
247 ATA_SC_IPM_DIS_SLUMBER);
249 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == 0) {
250 ata_sata_connect(ch);
258 ata_sata_phy_event(void *context, int dummy)
260 struct ata_connect_task *tp = (struct ata_connect_task *)context;
261 struct ata_channel *ch = device_get_softc(tp->dev);
265 mtx_lock(&Giant); /* newbus suckage it needs Giant */
266 if (tp->action == ATA_C_ATTACH) {
268 device_printf(tp->dev, "CONNECTED\n");
269 ata_sata_connect(ch);
270 ata_identify(tp->dev);
272 if (tp->action == ATA_C_DETACH) {
273 if (!device_get_children(tp->dev, &children, &nchildren)) {
274 for (i = 0; i < nchildren; i++)
276 device_delete_child(tp->dev, children[i]);
277 free(children, M_TEMP);
279 mtx_lock(&ch->state_mtx);
280 ch->state = ATA_IDLE;
281 mtx_unlock(&ch->state_mtx);
283 device_printf(tp->dev, "DISCONNECTED\n");
285 mtx_unlock(&Giant); /* suckage code dealt with, release Giant */
290 ata_sata_connect(struct ata_channel *ch)
295 /* wait up to 1 second for "connect well" */
296 for (timeout = 0; timeout < 100 ; timeout++) {
297 status = ATA_IDX_INL(ch, ATA_SSTATUS);
298 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1 ||
299 (status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)
303 if (timeout >= 100) {
305 device_printf(ch->dev, "SATA connect status=%08x\n", status);
309 /* clear SATA error register */
310 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
312 /* find out what type device we got poll for spec'd 31 seconds */
313 /* XXX SOS 10 secs for now as I have little patience */
315 for (timeout = 0; timeout < 1000; timeout++) {
316 if (ATA_IDX_INB(ch, ATA_STATUS) & ATA_S_BUSY)
322 device_printf(ch->dev, "SATA connect ready time=%dms\n", timeout * 10);
323 if (timeout < 1000) {
324 if ((ATA_IDX_INB(ch, ATA_CYL_LSB) == ATAPI_MAGIC_LSB) &&
325 (ATA_IDX_INB(ch, ATA_CYL_MSB) == ATAPI_MAGIC_MSB))
326 ch->devices = ATA_ATAPI_MASTER;
328 ch->devices = ATA_ATA_MASTER;
331 device_printf(ch->dev, "sata_connect devices=0x%b\n",
332 ch->devices, "\20\3ATAPI_MASTER\1ATA_MASTER");
337 ata_sata_setmode(device_t dev, int mode)
339 struct ata_device *atadev = device_get_softc(dev);
342 * if we detect that the device isn't a real SATA device we limit
343 * the transfer mode to UDMA5/ATA100.
344 * this works around the problems some devices has with the
345 * Marvell 88SX8030 SATA->PATA converters and UDMA6/ATA133.
347 if (atadev->param.satacapabilities != 0x0000 &&
348 atadev->param.satacapabilities != 0xffff) {
349 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
351 /* on some drives we need to set the transfer mode */
352 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
353 ata_limit_mode(dev, mode, ATA_UDMA6));
355 /* query SATA STATUS for the speed */
356 if (ch->r_io[ATA_SSTATUS].res &&
357 ((ATA_IDX_INL(ch, ATA_SSTATUS) & ATA_SS_CONWELL_MASK) ==
358 ATA_SS_CONWELL_GEN2))
359 atadev->mode = ATA_SA300;
361 atadev->mode = ATA_SA150;
364 mode = ata_limit_mode(dev, mode, ATA_UDMA5);
365 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
372 * AHCI v1.0 compliant SATA chipset support functions
374 struct ata_ahci_dma_prd {
377 u_int32_t dbc; /* 0 based */
378 #define ATA_AHCI_PRD_MASK 0x003fffff /* max 4MB */
379 #define ATA_AHCI_PRD_IPC (1<<31)
382 struct ata_ahci_cmd_tab {
385 u_int8_t reserved[32];
386 struct ata_ahci_dma_prd prd_tab[16];
389 struct ata_ahci_cmd_list {
391 u_int16_t prd_length; /* PRD entries */
393 u_int64_t cmd_table_phys; /* 128byte aligned */
398 ata_ahci_chipinit(device_t dev)
400 struct ata_pci_controller *ctlr = device_get_softc(dev);
403 /* reset AHCI controller */
404 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
405 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) | ATA_AHCI_GHC_HR);
407 if (ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) & ATA_AHCI_GHC_HR) {
408 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
409 device_printf(dev, "AHCI controller reset failure\n");
413 /* enable AHCI mode */
414 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
415 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) | ATA_AHCI_GHC_AE);
417 /* get the number of HW channels */
418 ctlr->channels = (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_NPMASK)+1;
420 /* clear interrupts */
421 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, ATA_INL(ctlr->r_res2, ATA_AHCI_IS));
423 /* enable AHCI interrupts */
424 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
425 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) | ATA_AHCI_GHC_IE);
427 ctlr->reset = ata_ahci_reset;
428 ctlr->dmainit = ata_ahci_dmainit;
429 ctlr->allocate = ata_ahci_allocate;
430 ctlr->setmode = ata_sata_setmode;
432 /* enable PCI interrupt */
433 pci_write_config(dev, PCIR_COMMAND,
434 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
436 /* announce we support the HW */
437 version = ATA_INL(ctlr->r_res2, ATA_AHCI_VS);
439 "AHCI Version %x%x.%x%x controller with %d ports detected\n",
440 (version >> 24) & 0xff, (version >> 16) & 0xff,
441 (version >> 8) & 0xff, version & 0xff, ctlr->channels);
446 ata_ahci_allocate(device_t dev)
448 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
449 struct ata_channel *ch = device_get_softc(dev);
450 int offset = (ch->unit << 7);
452 /* setup legacy cruft we need */
453 ch->r_io[ATA_CYL_LSB].res = ctlr->r_res2;
454 ch->r_io[ATA_CYL_LSB].offset = ATA_AHCI_P_SIG + 1 + offset;
455 ch->r_io[ATA_CYL_MSB].res = ctlr->r_res2;
456 ch->r_io[ATA_CYL_MSB].offset = ATA_AHCI_P_SIG + 3 + offset;
457 ch->r_io[ATA_STATUS].res = ctlr->r_res2;
458 ch->r_io[ATA_STATUS].offset = ATA_AHCI_P_TFD + offset;
459 ch->r_io[ATA_ALTSTAT].res = ctlr->r_res2;
460 ch->r_io[ATA_ALTSTAT].offset = ATA_AHCI_P_TFD + offset;
462 /* set the SATA resources */
463 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
464 ch->r_io[ATA_SSTATUS].offset = ATA_AHCI_P_SSTS + offset;
465 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
466 ch->r_io[ATA_SERROR].offset = ATA_AHCI_P_SERR + offset;
467 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
468 ch->r_io[ATA_SCONTROL].offset = ATA_AHCI_P_SCTL + offset;
469 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
470 ch->r_io[ATA_SACTIVE].offset = ATA_AHCI_P_SACT + offset;
472 ch->hw.status = ata_ahci_status;
473 ch->hw.begin_transaction = ata_ahci_begin_transaction;
474 ch->hw.end_transaction = ata_ahci_end_transaction;
475 ch->hw.command = NULL; /* not used here */
477 /* setup the work areas */
478 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLB + offset,
479 ch->dma->work_bus + ATA_AHCI_CL_OFFSET);
480 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLBU + offset, 0x00000000);
482 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FB + offset,
483 ch->dma->work_bus + ATA_AHCI_FB_OFFSET);
484 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FBU + offset, 0x00000000);
486 /* enable wanted port interrupts */
487 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset,
488 (ATA_AHCI_P_IX_CPD | ATA_AHCI_P_IX_TFE | ATA_AHCI_P_IX_HBF |
489 ATA_AHCI_P_IX_HBD | ATA_AHCI_P_IX_IF | ATA_AHCI_P_IX_OF |
490 ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC | ATA_AHCI_P_IX_DP |
491 ATA_AHCI_P_IX_UF | ATA_AHCI_P_IX_SDB | ATA_AHCI_P_IX_DS |
492 ATA_AHCI_P_IX_PS | ATA_AHCI_P_IX_DHR));
494 /* start operations on this channel */
495 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
496 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
497 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
502 ata_ahci_status(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);
506 struct ata_connect_task *tp;
507 u_int32_t action, istatus, sstatus, error, issued;
508 int offset = (ch->unit << 7);
511 action = ATA_INL(ctlr->r_res2, ATA_AHCI_IS);
512 if (action & (1 << ch->unit)) {
513 istatus = ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset);
514 issued = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CI + offset);
515 sstatus = ATA_INL(ctlr->r_res2, ATA_AHCI_P_SSTS + offset);
516 error = ATA_INL(ctlr->r_res2, ATA_AHCI_P_SERR + offset);
518 /* clear interrupt(s) */
519 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, action);
520 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset, istatus);
521 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_SERR + offset, error);
523 /* do we have cold connect surprise */
524 if (istatus & ATA_AHCI_P_IX_CPD) {
525 printf("ata_ahci_status status=%08x sstatus=%08x error=%08x\n",
526 istatus, sstatus, error);
529 /* check for and handle connect events */
530 if ((istatus & ATA_AHCI_P_IX_PC) &&
531 (tp = (struct ata_connect_task *)
532 malloc(sizeof(struct ata_connect_task),
533 M_ATA, M_NOWAIT | M_ZERO))) {
536 device_printf(ch->dev, "CONNECT requested\n");
537 tp->action = ATA_C_ATTACH;
539 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
540 taskqueue_enqueue(taskqueue_thread, &tp->task);
543 /* check for and handle disconnect events */
544 else if ((istatus & ATA_AHCI_P_IX_PRC) &&
545 !((sstatus & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1 ||
546 (sstatus & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2) &&
547 (tp = (struct ata_connect_task *)
548 malloc(sizeof(struct ata_connect_task),
549 M_ATA, M_NOWAIT | M_ZERO))) {
552 device_printf(ch->dev, "DISCONNECT requested\n");
553 tp->action = ATA_C_DETACH;
555 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
556 taskqueue_enqueue(taskqueue_thread, &tp->task);
559 /* do we have any device action ? */
560 if (!(issued & (1 << tag)))
566 /* must be called with ATA channel locked and state_mtx held */
568 ata_ahci_begin_transaction(struct ata_request *request)
570 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
571 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
572 struct ata_ahci_cmd_tab *ctp;
573 struct ata_ahci_cmd_list *clp;
574 int tag = 0, entries = 0;
577 /* get a piece of the workspace for this request */
578 ctp = (struct ata_ahci_cmd_tab *)
579 (ch->dma->work + ATA_AHCI_CT_OFFSET + (ATA_AHCI_CT_SIZE * tag));
581 /* setup the FIS for this request */ /* XXX SOS ATAPI missing still */
582 if (!(fis_size = ata_ahci_setup_fis(&ctp->cfis[0], request))) {
583 device_printf(request->dev, "setting up SATA FIS failed\n");
584 request->result = EIO;
585 return ATA_OP_FINISHED;
588 /* if request moves data setup and load SG list */
589 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
590 if (ch->dma->load(ch->dev, request->data, request->bytecount,
591 request->flags & ATA_R_READ,
592 ctp->prd_tab, &entries)) {
593 device_printf(request->dev, "setting up DMA failed\n");
594 request->result = EIO;
595 return ATA_OP_FINISHED;
599 /* setup the command list entry */
600 clp = (struct ata_ahci_cmd_list *)
601 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
603 clp->prd_length = entries;
604 clp->cmd_flags = (request->flags & ATA_R_WRITE ? (1<<6) : 0) |
605 (request->flags & ATA_R_ATAPI ? (1<<5) : 0) |
606 (fis_size / sizeof(u_int32_t));
608 clp->cmd_table_phys = htole64(ch->dma->work_bus + ATA_AHCI_CT_OFFSET +
609 (ATA_AHCI_CT_SIZE * tag));
611 /* clear eventual ACTIVE bit */
612 ATA_IDX_OUTL(ch, ATA_SACTIVE, ATA_IDX_INL(ch, ATA_SACTIVE) & (1 << tag));
614 /* issue the command */
615 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CI + (ch->unit << 7), (1 << tag));
617 /* start the timeout */
618 callout_reset(&request->callout, request->timeout * hz,
619 (timeout_t*)ata_timeout, request);
620 return ATA_OP_CONTINUES;
623 /* must be called with ATA channel locked and state_mtx held */
625 ata_ahci_end_transaction(struct ata_request *request)
627 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
628 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
629 struct ata_ahci_cmd_list *clp;
633 /* kill the timeout */
634 callout_stop(&request->callout);
637 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + (ch->unit << 7));
638 request->status = tf_data;
640 /* if error status get details */
641 if (request->status & ATA_S_ERROR)
642 request->error = tf_data >> 8;
644 /* record how much data we actually moved */
645 clp = (struct ata_ahci_cmd_list *)
646 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
647 request->donecount = clp->bytecount;
649 /* release SG list etc */
650 ch->dma->unload(ch->dev);
652 return ATA_OP_FINISHED;
656 ata_ahci_reset(device_t dev)
658 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
659 struct ata_channel *ch = device_get_softc(dev);
661 int timeout, offset = (ch->unit << 7);
663 /* kill off all activity on this channel */
664 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
665 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
666 cmd & ~(ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
668 /* XXX SOS this is not entirely wrong */
673 device_printf(dev, "stopping AHCI engine failed\n");
676 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) & ATA_AHCI_P_CMD_CR);
678 /* issue Command List Override if supported */
679 if (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_CAP_CLO) {
680 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
681 cmd |= ATA_AHCI_P_CMD_CLO;
682 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, cmd);
687 device_printf(dev, "executing CLO failed\n");
690 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD+offset)&ATA_AHCI_P_CMD_CLO);
694 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, ATA_AHCI_P_CMD_SUD);
696 ata_sata_phy_enable(ch);
698 /* clear any interrupts pending on this channel */
699 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset,
700 ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset));
702 /* start operations on this channel */
703 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
704 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
705 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
709 ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
711 struct ata_dmasetprd_args *args = xsc;
712 struct ata_ahci_dma_prd *prd = args->dmatab;
715 if (!(args->error = error)) {
716 for (i = 0; i < nsegs; i++) {
717 prd[i].dba = htole64(segs[i].ds_addr);
718 prd[i].dbc = htole32((segs[i].ds_len - 1) & ATA_AHCI_PRD_MASK);
725 ata_ahci_dmainit(device_t dev)
727 struct ata_channel *ch = device_get_softc(dev);
731 /* note start and stop are not used here */
732 ch->dma->setprd = ata_ahci_dmasetprd;
733 ch->dma->max_iosize = 8192 * DEV_BSIZE;
738 ata_ahci_setup_fis(u_int8_t *fis, struct ata_request *request)
740 struct ata_device *atadev = device_get_softc(request->dev);
743 /* XXX SOS add ATAPI commands support later */
744 ata_modify_if_48bit(request);
746 fis[idx++] = 0x27; /* host to device */
747 fis[idx++] = 0x80; /* command FIS (note PM goes here) */
748 fis[idx++] = request->u.ata.command;
749 fis[idx++] = request->u.ata.feature;
751 fis[idx++] = request->u.ata.lba;
752 fis[idx++] = request->u.ata.lba >> 8;
753 fis[idx++] = request->u.ata.lba >> 16;
754 fis[idx] = ATA_D_LBA | atadev->unit;
755 if (atadev->flags & ATA_D_48BIT_ACTIVE)
758 fis[idx++] |= (request->u.ata.lba >> 24 & 0x0f);
760 fis[idx++] = request->u.ata.lba >> 24;
761 fis[idx++] = request->u.ata.lba >> 32;
762 fis[idx++] = request->u.ata.lba >> 40;
763 fis[idx++] = request->u.ata.feature >> 8;
765 fis[idx++] = request->u.ata.count;
766 fis[idx++] = request->u.ata.count >> 8;
768 fis[idx++] = ATA_A_4BIT;
779 * Acard chipset support functions
782 ata_acard_ident(device_t dev)
784 struct ata_pci_controller *ctlr = device_get_softc(dev);
785 struct ata_chip_id *idx;
786 static struct ata_chip_id ids[] =
787 {{ ATA_ATP850R, 0, ATPOLD, 0x00, ATA_UDMA2, "ATP850" },
788 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "ATP860A" },
789 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "ATP860R" },
790 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "ATP865A" },
791 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "ATP865R" },
792 { 0, 0, 0, 0, 0, 0}};
795 if (!(idx = ata_match_chip(dev, ids)))
798 sprintf(buffer, "Acard %s %s controller",
799 idx->text, ata_mode2str(idx->max_dma));
800 device_set_desc_copy(dev, buffer);
802 ctlr->chipinit = ata_acard_chipinit;
807 ata_acard_chipinit(device_t dev)
809 struct ata_pci_controller *ctlr = device_get_softc(dev);
811 if (ata_setup_interrupt(dev))
814 ctlr->allocate = ata_acard_allocate;
815 if (ctlr->chip->cfg1 == ATPOLD) {
816 ctlr->setmode = ata_acard_850_setmode;
817 ctlr->locking = ata_serialize;
820 ctlr->setmode = ata_acard_86X_setmode;
825 ata_acard_allocate(device_t dev)
827 struct ata_channel *ch = device_get_softc(dev);
829 /* setup the usual register normal pci style */
830 if (ata_pci_allocate(dev))
833 ch->hw.status = ata_acard_status;
838 ata_acard_status(device_t dev)
840 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
841 struct ata_channel *ch = device_get_softc(dev);
843 if (ctlr->chip->cfg1 == ATPOLD &&
844 ATA_LOCKING(ch->dev, ATA_LF_WHICH) != ch->unit)
846 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
847 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
849 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
850 ATA_BMSTAT_INTERRUPT)
852 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
854 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
855 ATA_IDX_INB(ch, ATA_BMCMD_PORT)&~ATA_BMCMD_START_STOP);
858 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
860 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
867 ata_acard_850_setmode(device_t dev, int mode)
869 device_t gparent = GRANDPARENT(dev);
870 struct ata_pci_controller *ctlr = device_get_softc(gparent);
871 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
872 struct ata_device *atadev = device_get_softc(dev);
873 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
876 mode = ata_limit_mode(dev, mode,
877 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
879 /* XXX SOS missing WDMA0+1 + PIO modes */
880 if (mode >= ATA_WDMA2) {
881 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
883 device_printf(dev, "%ssetting %s on %s chip\n",
884 (error) ? "FAILURE " : "",
885 ata_mode2str(mode), ctlr->chip->text);
887 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
889 reg54 &= ~(0x03 << (devno << 1));
890 if (mode >= ATA_UDMA0)
891 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
892 pci_write_config(gparent, 0x54, reg54, 1);
893 pci_write_config(gparent, 0x4a, 0xa6, 1);
894 pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
899 /* we could set PIO mode timings, but we assume the BIOS did that */
903 ata_acard_86X_setmode(device_t dev, int mode)
905 device_t gparent = GRANDPARENT(dev);
906 struct ata_pci_controller *ctlr = device_get_softc(gparent);
907 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
908 struct ata_device *atadev = device_get_softc(dev);
909 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
913 mode = ata_limit_mode(dev, mode,
914 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
916 mode = ata_check_80pin(dev, mode);
918 /* XXX SOS missing WDMA0+1 + PIO modes */
919 if (mode >= ATA_WDMA2) {
920 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
922 device_printf(dev, "%ssetting %s on %s chip\n",
923 (error) ? "FAILURE " : "",
924 ata_mode2str(mode), ctlr->chip->text);
926 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
928 reg44 &= ~(0x000f << (devno << 2));
929 if (mode >= ATA_UDMA0)
930 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
931 pci_write_config(gparent, 0x44, reg44, 2);
932 pci_write_config(gparent, 0x4a, 0xa6, 1);
933 pci_write_config(gparent, 0x40 + devno, 0x31, 1);
938 /* we could set PIO mode timings, but we assume the BIOS did that */
943 * Acer Labs Inc (ALI) chipset support functions
946 ata_ali_ident(device_t dev)
948 struct ata_pci_controller *ctlr = device_get_softc(dev);
949 struct ata_chip_id *idx;
950 static struct ata_chip_id ids[] =
951 {{ ATA_ALI_5289, 0x00, 2, ALISATA, ATA_SA150, "M5289" },
952 { ATA_ALI_5287, 0x00, 4, ALISATA, ATA_SA150, "M5287" },
953 { ATA_ALI_5281, 0x00, 2, ALISATA, ATA_SA150, "M5281" },
954 { ATA_ALI_5229, 0xc5, 0, ALINEW, ATA_UDMA6, "M5229" },
955 { ATA_ALI_5229, 0xc4, 0, ALINEW, ATA_UDMA5, "M5229" },
956 { ATA_ALI_5229, 0xc2, 0, ALINEW, ATA_UDMA4, "M5229" },
957 { ATA_ALI_5229, 0x20, 0, ALIOLD, ATA_UDMA2, "M5229" },
958 { ATA_ALI_5229, 0x00, 0, ALIOLD, ATA_WDMA2, "M5229" },
959 { 0, 0, 0, 0, 0, 0}};
962 if (!(idx = ata_match_chip(dev, ids)))
965 sprintf(buffer, "AcerLabs %s %s controller",
966 idx->text, ata_mode2str(idx->max_dma));
967 device_set_desc_copy(dev, buffer);
969 ctlr->chipinit = ata_ali_chipinit;
974 ata_ali_chipinit(device_t dev)
976 struct ata_pci_controller *ctlr = device_get_softc(dev);
978 if (ata_setup_interrupt(dev))
981 switch (ctlr->chip->cfg2) {
983 ctlr->channels = ctlr->chip->cfg1;
984 ctlr->allocate = ata_ali_sata_allocate;
985 ctlr->setmode = ata_sata_setmode;
987 /* enable PCI interrupt */
988 pci_write_config(dev, PCIR_COMMAND,
989 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
993 /* use device interrupt as byte count end */
994 pci_write_config(dev, 0x4a, pci_read_config(dev, 0x4a, 1) | 0x20, 1);
996 /* enable cable detection and UDMA support on newer chips */
997 pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
999 /* enable ATAPI UDMA mode */
1000 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x01, 1);
1002 /* only chips with revision > 0xc4 can do 48bit DMA */
1003 if (ctlr->chip->chiprev <= 0xc4)
1005 "using PIO transfers above 137GB as workaround for "
1006 "48bit DMA access bug, expect reduced performance\n");
1007 ctlr->allocate = ata_ali_allocate;
1008 ctlr->reset = ata_ali_reset;
1009 ctlr->setmode = ata_ali_setmode;
1013 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
1014 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x03, 1);
1015 ctlr->setmode = ata_ali_setmode;
1022 ata_ali_allocate(device_t dev)
1024 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1025 struct ata_channel *ch = device_get_softc(dev);
1027 /* setup the usual register normal pci style */
1028 if (ata_pci_allocate(dev))
1031 /* older chips can't do 48bit DMA transfers */
1032 if (ctlr->chip->chiprev <= 0xc4)
1033 ch->flags |= ATA_NO_48BIT_DMA;
1039 ata_ali_sata_allocate(device_t dev)
1041 device_t parent = device_get_parent(dev);
1042 struct ata_pci_controller *ctlr = device_get_softc(parent);
1043 struct ata_channel *ch = device_get_softc(dev);
1044 struct resource *io = NULL, *ctlio = NULL;
1045 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
1048 rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
1049 io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
1053 rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
1054 ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
1056 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
1060 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
1061 ch->r_io[i].res = io;
1062 ch->r_io[i].offset = i + (unit10 ? 8 : 0);
1064 ch->r_io[ATA_CONTROL].res = ctlio;
1065 ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
1066 ch->r_io[ATA_IDX_ADDR].res = io;
1067 ata_default_registers(dev);
1069 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
1070 ch->r_io[i].res = ctlr->r_res1;
1071 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
1074 ch->flags |= ATA_NO_SLAVE;
1076 /* XXX SOS PHY handling awkward in ALI chip not supported yet */
1082 ata_ali_reset(device_t dev)
1084 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1085 struct ata_channel *ch = device_get_softc(dev);
1089 ata_generic_reset(dev);
1092 * workaround for datacorruption bug found on at least SUN Blade-100
1093 * find the ISA function on the southbridge and disable then enable
1094 * the ATA channel tristate buffer
1096 if (ctlr->chip->chiprev == 0xc3 || ctlr->chip->chiprev == 0xc2) {
1097 if (!device_get_children(GRANDPARENT(dev), &children, &nchildren)) {
1098 for (i = 0; i < nchildren; i++) {
1099 if (pci_get_devid(children[i]) == ATA_ALI_1533) {
1100 pci_write_config(children[i], 0x58,
1101 pci_read_config(children[i], 0x58, 1) &
1102 ~(0x04 << ch->unit), 1);
1103 pci_write_config(children[i], 0x58,
1104 pci_read_config(children[i], 0x58, 1) |
1105 (0x04 << ch->unit), 1);
1109 free(children, M_TEMP);
1115 ata_ali_setmode(device_t dev, int mode)
1117 device_t gparent = GRANDPARENT(dev);
1118 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1119 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1120 struct ata_device *atadev = device_get_softc(dev);
1121 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1124 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1126 if (ctlr->chip->cfg2 & ALINEW) {
1127 if (mode > ATA_UDMA2 &&
1128 pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
1129 ata_print_cable(dev, "controller");
1134 mode = ata_check_80pin(dev, mode);
1136 if (ctlr->chip->cfg2 & ALIOLD) {
1137 /* doesn't support ATAPI DMA on write */
1138 ch->flags |= ATA_ATAPI_DMA_RO;
1139 if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
1140 /* doesn't support ATAPI DMA on two ATAPI devices */
1141 device_printf(dev, "two atapi devices on this channel, no DMA\n");
1142 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1146 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1149 device_printf(dev, "%ssetting %s on %s chip\n",
1150 (error) ? "FAILURE " : "",
1151 ata_mode2str(mode), ctlr->chip->text);
1153 if (mode >= ATA_UDMA0) {
1154 u_int8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
1155 u_int32_t word54 = pci_read_config(gparent, 0x54, 4);
1157 word54 &= ~(0x000f000f << (devno << 2));
1158 word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
1159 pci_write_config(gparent, 0x54, word54, 4);
1160 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1164 u_int32_t piotimings[] =
1165 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
1166 0x00310001, 0x00440001, 0x00330001, 0x00310001};
1168 pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
1169 ~(0x0008000f << (devno << 2)), 4);
1170 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1171 piotimings[ata_mode2idx(mode)], 4);
1173 atadev->mode = mode;
1179 * American Micro Devices (AMD) chipset support functions
1182 ata_amd_ident(device_t dev)
1184 struct ata_pci_controller *ctlr = device_get_softc(dev);
1185 struct ata_chip_id *idx;
1186 static struct ata_chip_id ids[] =
1187 {{ ATA_AMD756, 0x00, AMDNVIDIA, 0x00, ATA_UDMA4, "756" },
1188 { ATA_AMD766, 0x00, AMDNVIDIA, AMDCABLE|AMDBUG, ATA_UDMA5, "766" },
1189 { ATA_AMD768, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA5, "768" },
1190 { ATA_AMD8111, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA6, "8111" },
1191 { 0, 0, 0, 0, 0, 0}};
1194 if (!(idx = ata_match_chip(dev, ids)))
1197 sprintf(buffer, "AMD %s %s controller",
1198 idx->text, ata_mode2str(idx->max_dma));
1199 device_set_desc_copy(dev, buffer);
1201 ctlr->chipinit = ata_amd_chipinit;
1206 ata_amd_chipinit(device_t dev)
1208 struct ata_pci_controller *ctlr = device_get_softc(dev);
1210 if (ata_setup_interrupt(dev))
1213 /* disable/set prefetch, postwrite */
1214 if (ctlr->chip->cfg2 & AMDBUG)
1215 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
1217 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
1219 ctlr->setmode = ata_via_family_setmode;
1225 * ATI chipset support functions
1228 ata_ati_ident(device_t dev)
1230 struct ata_pci_controller *ctlr = device_get_softc(dev);
1231 struct ata_chip_id *idx;
1232 static struct ata_chip_id ids[] =
1233 {{ ATA_ATI_IXP200, 0x00, 0, 0, ATA_UDMA5, "IXP200" },
1234 { ATA_ATI_IXP300, 0x00, 0, 0, ATA_UDMA6, "IXP300" },
1235 { ATA_ATI_IXP400, 0x00, 0, 0, ATA_UDMA6, "IXP400" },
1236 { ATA_ATI_IXP300_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP300" },
1237 { ATA_ATI_IXP400_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP400" },
1238 { ATA_ATI_IXP400_S2, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP400" },
1239 { 0, 0, 0, 0, 0, 0}};
1242 if (!(idx = ata_match_chip(dev, ids)))
1245 sprintf(buffer, "ATI %s %s controller",
1246 idx->text, ata_mode2str(idx->max_dma));
1247 device_set_desc_copy(dev, buffer);
1250 /* the ATI SATA controller is actually a SiI 3112 controller*/
1251 if (ctlr->chip->cfg1 & SIIMEMIO)
1252 ctlr->chipinit = ata_sii_chipinit;
1254 ctlr->chipinit = ata_ati_chipinit;
1259 ata_ati_chipinit(device_t dev)
1261 struct ata_pci_controller *ctlr = device_get_softc(dev);
1263 if (ata_setup_interrupt(dev))
1266 ctlr->setmode = ata_ati_setmode;
1271 ata_ati_setmode(device_t dev, int mode)
1273 device_t gparent = GRANDPARENT(dev);
1274 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1275 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1276 struct ata_device *atadev = device_get_softc(dev);
1277 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1278 int offset = (devno ^ 0x01) << 3;
1280 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
1281 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
1282 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
1284 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1286 mode = ata_check_80pin(dev, mode);
1288 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1291 device_printf(dev, "%ssetting %s on %s chip\n",
1292 (error) ? "FAILURE " : "",
1293 ata_mode2str(mode), ctlr->chip->text);
1295 if (mode >= ATA_UDMA0) {
1296 pci_write_config(gparent, 0x56,
1297 (pci_read_config(gparent, 0x56, 2) &
1298 ~(0xf << (devno << 2))) |
1299 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
1300 pci_write_config(gparent, 0x54,
1301 pci_read_config(gparent, 0x54, 1) |
1302 (0x01 << devno), 1);
1303 pci_write_config(gparent, 0x44,
1304 (pci_read_config(gparent, 0x44, 4) &
1305 ~(0xff << offset)) |
1306 (dmatimings[2] << offset), 4);
1308 else if (mode >= ATA_WDMA0) {
1309 pci_write_config(gparent, 0x54,
1310 pci_read_config(gparent, 0x54, 1) &
1311 ~(0x01 << devno), 1);
1312 pci_write_config(gparent, 0x44,
1313 (pci_read_config(gparent, 0x44, 4) &
1314 ~(0xff << offset)) |
1315 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
1318 pci_write_config(gparent, 0x54,
1319 pci_read_config(gparent, 0x54, 1) &
1320 ~(0x01 << devno), 1);
1322 pci_write_config(gparent, 0x4a,
1323 (pci_read_config(gparent, 0x4a, 2) &
1324 ~(0xf << (devno << 2))) |
1325 (((mode - ATA_PIO0) & ATA_MODE_MASK) << (devno<<2)),2);
1326 pci_write_config(gparent, 0x40,
1327 (pci_read_config(gparent, 0x40, 4) &
1328 ~(0xff << offset)) |
1329 (piotimings[ata_mode2idx(mode)] << offset), 4);
1330 atadev->mode = mode;
1335 * Cyrix chipset support functions
1338 ata_cyrix_ident(device_t dev)
1340 struct ata_pci_controller *ctlr = device_get_softc(dev);
1342 if (pci_get_devid(dev) == ATA_CYRIX_5530) {
1343 device_set_desc(dev, "Cyrix 5530 ATA33 controller");
1344 ctlr->chipinit = ata_cyrix_chipinit;
1351 ata_cyrix_chipinit(device_t dev)
1353 struct ata_pci_controller *ctlr = device_get_softc(dev);
1355 if (ata_setup_interrupt(dev))
1359 ctlr->setmode = ata_cyrix_setmode;
1361 ctlr->setmode = ata_generic_setmode;
1366 ata_cyrix_setmode(device_t dev, int mode)
1368 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1369 struct ata_device *atadev = device_get_softc(dev);
1370 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1371 u_int32_t piotiming[] =
1372 { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
1373 u_int32_t dmatiming[] = { 0x00077771, 0x00012121, 0x00002020 };
1374 u_int32_t udmatiming[] = { 0x00921250, 0x00911140, 0x00911030 };
1377 ch->dma->alignment = 16;
1378 ch->dma->max_iosize = 126 * DEV_BSIZE;
1380 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1382 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1385 device_printf(dev, "%ssetting %s on Cyrix chip\n",
1386 (error) ? "FAILURE " : "", ata_mode2str(mode));
1388 if (mode >= ATA_UDMA0) {
1389 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1390 0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
1392 else if (mode >= ATA_WDMA0) {
1393 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1394 0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
1397 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1398 0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
1400 atadev->mode = mode;
1406 * Cypress chipset support functions
1409 ata_cypress_ident(device_t dev)
1411 struct ata_pci_controller *ctlr = device_get_softc(dev);
1414 * the Cypress chip is a mess, it contains two ATA functions, but
1415 * both channels are visible on the first one.
1416 * simply ignore the second function for now, as the right
1417 * solution (ignoring the second channel on the first function)
1418 * doesn't work with the crappy ATA interrupt setup on the alpha.
1420 if (pci_get_devid(dev) == ATA_CYPRESS_82C693 &&
1421 pci_get_function(dev) == 1 &&
1422 pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
1423 device_set_desc(dev, "Cypress 82C693 ATA controller");
1424 ctlr->chipinit = ata_cypress_chipinit;
1431 ata_cypress_chipinit(device_t dev)
1433 struct ata_pci_controller *ctlr = device_get_softc(dev);
1435 if (ata_setup_interrupt(dev))
1438 ctlr->setmode = ata_cypress_setmode;
1443 ata_cypress_setmode(device_t dev, int mode)
1445 device_t gparent = GRANDPARENT(dev);
1446 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1447 struct ata_device *atadev = device_get_softc(dev);
1450 mode = ata_limit_mode(dev, mode, ATA_WDMA2);
1452 /* XXX SOS missing WDMA0+1 + PIO modes */
1453 if (mode == ATA_WDMA2) {
1454 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1456 device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
1457 error ? "FAILURE " : "");
1459 pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
1460 atadev->mode = mode;
1464 /* we could set PIO mode timings, but we assume the BIOS did that */
1469 * HighPoint chipset support functions
1472 ata_highpoint_ident(device_t dev)
1474 struct ata_pci_controller *ctlr = device_get_softc(dev);
1475 struct ata_chip_id *idx;
1476 static struct ata_chip_id ids[] =
1477 {{ ATA_HPT374, 0x07, HPT374, 0x00, ATA_UDMA6, "HPT374" },
1478 { ATA_HPT372, 0x02, HPT372, 0x00, ATA_UDMA6, "HPT372N" },
1479 { ATA_HPT372, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT372" },
1480 { ATA_HPT371, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT371" },
1481 { ATA_HPT366, 0x05, HPT372, 0x00, ATA_UDMA6, "HPT372" },
1482 { ATA_HPT366, 0x03, HPT370, 0x00, ATA_UDMA5, "HPT370" },
1483 { ATA_HPT366, 0x02, HPT366, 0x00, ATA_UDMA4, "HPT368" },
1484 { ATA_HPT366, 0x00, HPT366, HPTOLD, ATA_UDMA4, "HPT366" },
1485 { ATA_HPT302, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT302" },
1486 { 0, 0, 0, 0, 0, 0}};
1489 if (!(idx = ata_match_chip(dev, ids)))
1492 strcpy(buffer, "HighPoint ");
1493 strcat(buffer, idx->text);
1494 if (idx->cfg1 == HPT374) {
1495 if (pci_get_function(dev) == 0)
1496 strcat(buffer, " (channel 0+1)");
1497 if (pci_get_function(dev) == 1)
1498 strcat(buffer, " (channel 2+3)");
1500 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
1501 device_set_desc_copy(dev, buffer);
1503 ctlr->chipinit = ata_highpoint_chipinit;
1508 ata_highpoint_chipinit(device_t dev)
1510 struct ata_pci_controller *ctlr = device_get_softc(dev);
1512 if (ata_setup_interrupt(dev))
1515 if (ctlr->chip->cfg2 == HPTOLD) {
1516 /* disable interrupt prediction */
1517 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
1520 /* disable interrupt prediction */
1521 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
1522 pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
1524 /* enable interrupts */
1525 pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
1527 /* set clocks etc */
1528 if (ctlr->chip->cfg1 < HPT372)
1529 pci_write_config(dev, 0x5b, 0x22, 1);
1531 pci_write_config(dev, 0x5b,
1532 (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
1534 ctlr->allocate = ata_highpoint_allocate;
1535 ctlr->setmode = ata_highpoint_setmode;
1540 ata_highpoint_allocate(device_t dev)
1542 struct ata_channel *ch = device_get_softc(dev);
1544 /* setup the usual register normal pci style */
1545 if (ata_pci_allocate(dev))
1548 ch->flags |= ATA_ALWAYS_DMASTAT;
1553 ata_highpoint_setmode(device_t dev, int mode)
1555 device_t gparent = GRANDPARENT(dev);
1556 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1557 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1558 struct ata_device *atadev = device_get_softc(dev);
1559 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1561 u_int32_t timings33[][4] = {
1562 /* HPT366 HPT370 HPT372 HPT374 mode */
1563 { 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a }, /* PIO 0 */
1564 { 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 }, /* PIO 1 */
1565 { 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 }, /* PIO 2 */
1566 { 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 }, /* PIO 3 */
1567 { 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 }, /* PIO 4 */
1568 { 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea }, /* MWDMA 0 */
1569 { 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 }, /* MWDMA 1 */
1570 { 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 }, /* MWDMA 2 */
1571 { 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea }, /* UDMA 0 */
1572 { 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 }, /* UDMA 1 */
1573 { 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 }, /* UDMA 2 */
1574 { 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 }, /* UDMA 3 */
1575 { 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 }, /* UDMA 4 */
1576 { 0, 0x16454e31, 0x1c8a9c62, 0x12848242 }, /* UDMA 5 */
1577 { 0, 0, 0x1c869c62, 0x12808242 } /* UDMA 6 */
1580 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1582 if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
1583 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1585 mode = ata_highpoint_check_80pin(dev, mode);
1588 * most if not all HPT chips cant really handle that the device is
1589 * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
1590 * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
1592 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
1593 ata_limit_mode(dev, mode, ATA_UDMA5));
1595 device_printf(dev, "%ssetting %s on HighPoint chip\n",
1596 (error) ? "FAILURE " : "", ata_mode2str(mode));
1598 pci_write_config(gparent, 0x40 + (devno << 2),
1599 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
1600 atadev->mode = mode;
1604 ata_highpoint_check_80pin(device_t dev, int mode)
1606 device_t gparent = GRANDPARENT(dev);
1607 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1608 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1609 u_int8_t reg, val, res;
1611 if (ctlr->chip->cfg1 == HPT374 && pci_get_function(gparent) == 1) {
1612 reg = ch->unit ? 0x57 : 0x53;
1613 val = pci_read_config(gparent, reg, 1);
1614 pci_write_config(gparent, reg, val | 0x80, 1);
1618 val = pci_read_config(gparent, reg, 1);
1619 pci_write_config(gparent, reg, val & 0xfe, 1);
1621 res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1622 pci_write_config(gparent, reg, val, 1);
1624 if (mode > ATA_UDMA2 && res) {
1625 ata_print_cable(dev, "controller");
1633 * Intel chipset support functions
1636 ata_intel_ident(device_t dev)
1638 struct ata_pci_controller *ctlr = device_get_softc(dev);
1639 struct ata_chip_id *idx;
1640 static struct ata_chip_id ids[] =
1641 {{ ATA_I82371FB, 0, 0, 0x00, ATA_WDMA2, "PIIX" },
1642 { ATA_I82371SB, 0, 0, 0x00, ATA_WDMA2, "PIIX3" },
1643 { ATA_I82371AB, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1644 { ATA_I82443MX, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1645 { ATA_I82451NX, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1646 { ATA_I82801AB, 0, 0, 0x00, ATA_UDMA2, "ICH0" },
1647 { ATA_I82801AA, 0, 0, 0x00, ATA_UDMA4, "ICH" },
1648 { ATA_I82372FB, 0, 0, 0x00, ATA_UDMA4, "ICH" },
1649 { ATA_I82801BA, 0, 0, 0x00, ATA_UDMA5, "ICH2" },
1650 { ATA_I82801BA_1, 0, 0, 0x00, ATA_UDMA5, "ICH2" },
1651 { ATA_I82801CA, 0, 0, 0x00, ATA_UDMA5, "ICH3" },
1652 { ATA_I82801CA_1, 0, 0, 0x00, ATA_UDMA5, "ICH3" },
1653 { ATA_I82801DB, 0, 0, 0x00, ATA_UDMA5, "ICH4" },
1654 { ATA_I82801DB_1, 0, 0, 0x00, ATA_UDMA5, "ICH4" },
1655 { ATA_I82801EB, 0, 0, 0x00, ATA_UDMA5, "ICH5" },
1656 { ATA_I82801EB_S1, 0, 0, 0x00, ATA_SA150, "ICH5" },
1657 { ATA_I82801EB_R1, 0, 0, 0x00, ATA_SA150, "ICH5" },
1658 { ATA_I6300ESB, 0, 0, 0x00, ATA_UDMA5, "6300ESB" },
1659 { ATA_I6300ESB_S1, 0, 0, 0x00, ATA_SA150, "6300ESB" },
1660 { ATA_I6300ESB_R1, 0, 0, 0x00, ATA_SA150, "6300ESB" },
1661 { ATA_I82801FB, 0, 0, 0x00, ATA_UDMA5, "ICH6" },
1662 { ATA_I82801FB_S1, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1663 { ATA_I82801FB_R1, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1664 { ATA_I82801FB_M, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1665 { ATA_I82801GB, 0, 0, 0x00, ATA_UDMA5, "ICH7" },
1666 { ATA_I82801GB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1667 { ATA_I82801GB_R1, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1668 { ATA_I82801GB_M, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1669 { ATA_I82801GB_AH, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1670 { ATA_I31244, 0, 0, 0x00, ATA_SA150, "31244" },
1671 { 0, 0, 0, 0, 0, 0}};
1674 if (!(idx = ata_match_chip(dev, ids)))
1677 sprintf(buffer, "Intel %s %s controller",
1678 idx->text, ata_mode2str(idx->max_dma));
1679 device_set_desc_copy(dev, buffer);
1681 ctlr->chipinit = ata_intel_chipinit;
1686 ata_intel_chipinit(device_t dev)
1688 struct ata_pci_controller *ctlr = device_get_softc(dev);
1690 if (ata_setup_interrupt(dev))
1693 /* good old PIIX needs special treatment (not implemented) */
1694 if (ctlr->chip->chipid == ATA_I82371FB) {
1695 ctlr->setmode = ata_intel_old_setmode;
1698 /* the intel 31244 needs special care if in DPA mode */
1699 else if (ctlr->chip->chipid == ATA_I31244) {
1700 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE) {
1701 ctlr->r_type2 = SYS_RES_MEMORY;
1702 ctlr->r_rid2 = PCIR_BAR(0);
1703 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1708 ctlr->allocate = ata_intel_31244_allocate;
1709 ctlr->reset = ata_intel_31244_reset;
1711 ctlr->setmode = ata_sata_setmode;
1714 /* non SATA intel chips goes here */
1715 else if (ctlr->chip->max_dma < ATA_SA150) {
1716 ctlr->allocate = ata_intel_allocate;
1717 ctlr->setmode = ata_intel_new_setmode;
1720 /* SATA parts can be either compat or AHCI */
1722 /* force all ports active "the legacy way" */
1723 pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f,2);
1725 ctlr->allocate = ata_intel_allocate;
1726 ctlr->reset = ata_intel_reset;
1729 * if we have AHCI capability and BAR(5) as a memory resource
1730 * and AHCI or RAID mode enabled in BIOS we go for AHCI mode
1732 if ((ctlr->chip->cfg1 == AHCI) &&
1733 (pci_read_config(dev, 0x90, 1) & 0xc0)) {
1734 ctlr->r_type2 = SYS_RES_MEMORY;
1735 ctlr->r_rid2 = PCIR_BAR(5);
1736 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1739 return ata_ahci_chipinit(dev);
1741 ctlr->setmode = ata_sata_setmode;
1743 /* enable PCI interrupt */
1744 pci_write_config(dev, PCIR_COMMAND,
1745 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1751 ata_intel_allocate(device_t dev)
1753 struct ata_channel *ch = device_get_softc(dev);
1755 /* setup the usual register normal pci style */
1756 if (ata_pci_allocate(dev))
1759 ch->flags |= ATA_ALWAYS_DMASTAT;
1764 ata_intel_reset(device_t dev)
1766 device_t parent = device_get_parent(dev);
1767 struct ata_pci_controller *ctlr = device_get_softc(parent);
1768 struct ata_channel *ch = device_get_softc(dev);
1771 /* ICH6 & ICH7 in compat mode has 4 SATA ports as master/slave on 2 ch's */
1772 if (ctlr->chip->cfg1) {
1773 mask = (0x0005 << ch->unit);
1776 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1777 if (pci_read_config(parent, 0x90, 1) & 0x04)
1780 mask = (0x0001 << ch->unit);
1781 /* XXX SOS should be in intel_allocate if we grow it */
1782 ch->flags |= ATA_NO_SLAVE;
1785 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1787 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1789 /* wait up to 1 sec for "connect well" */
1790 for (timeout = 0; timeout < 100 ; timeout++) {
1791 if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1792 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1796 ata_generic_reset(dev);
1800 ata_intel_old_setmode(device_t dev, int mode)
1806 ata_intel_new_setmode(device_t dev, int mode)
1808 device_t gparent = GRANDPARENT(dev);
1809 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1810 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1811 struct ata_device *atadev = device_get_softc(dev);
1812 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1813 u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
1814 u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
1815 u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
1816 u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
1817 u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
1818 u_int32_t mask40 = 0, new40 = 0;
1819 u_int8_t mask44 = 0, new44 = 0;
1821 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1822 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1824 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1826 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1827 ata_print_cable(dev, "controller");
1831 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1834 device_printf(dev, "%ssetting %s on %s chip\n",
1835 (error) ? "FAILURE " : "",
1836 ata_mode2str(mode), ctlr->chip->text);
1840 if (mode >= ATA_UDMA0) {
1841 pci_write_config(gparent, 0x48, reg48 | (0x0001 << devno), 2);
1842 pci_write_config(gparent, 0x4a,
1843 (reg4a & ~(0x3 << (devno << 2))) |
1844 ((0x01 + !(mode & 0x01)) << (devno << 2)), 2);
1847 pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
1848 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))), 2);
1851 if (mode >= ATA_UDMA2)
1852 pci_write_config(gparent, 0x54, reg54 | (0x1 << devno), 2);
1854 pci_write_config(gparent, 0x54, reg54 & ~(0x1 << devno), 2);
1856 if (mode >= ATA_UDMA5)
1857 pci_write_config(gparent, 0x54, reg54 | (0x1000 << devno), 2);
1859 pci_write_config(gparent, 0x54, reg54 & ~(0x1000 << devno), 2);
1861 reg40 &= ~0x00ff00ff;
1862 reg40 |= 0x40774077;
1864 if (atadev->unit == ATA_MASTER) {
1866 new40 = timings[ata_mode2idx(mode)] << 8;
1870 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
1871 (timings[ata_mode2idx(mode)] & 0x03);
1879 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
1880 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
1882 atadev->mode = mode;
1886 ata_intel_31244_allocate(device_t dev)
1888 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1889 struct ata_channel *ch = device_get_softc(dev);
1893 ch_offset = 0x200 + ch->unit * 0x200;
1895 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
1896 ch->r_io[i].res = ctlr->r_res2;
1897 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
1898 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x06;
1899 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
1900 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
1901 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
1902 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
1903 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
1904 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1d;
1905 ch->r_io[ATA_ERROR].offset = ch_offset + 0x04;
1906 ch->r_io[ATA_STATUS].offset = ch_offset + 0x1c;
1907 ch->r_io[ATA_ALTSTAT].offset = ch_offset + 0x28;
1908 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x29;
1909 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x100;
1910 ch->r_io[ATA_SERROR].offset = ch_offset + 0x104;
1911 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x108;
1912 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x70;
1913 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x72;
1914 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x74;
1916 ch->flags |= ATA_NO_SLAVE;
1918 ch->hw.status = ata_intel_31244_status;
1919 ch->hw.command = ata_intel_31244_command;
1921 /* enable PHY state change interrupt */
1922 ATA_OUTL(ctlr->r_res2, 0x4,
1923 ATA_INL(ctlr->r_res2, 0x04) | (0x01 << (ch->unit << 3)));
1928 ata_intel_31244_status(device_t dev)
1930 struct ata_channel *ch = device_get_softc(dev);
1931 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
1932 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
1933 struct ata_connect_task *tp;
1935 /* check for PHY related interrupts on SATA capable HW */
1937 /* clear error bits/interrupt */
1938 ATA_IDX_OUTL(ch, ATA_SERROR, error);
1940 /* if we have a connection event deal with it */
1941 if ((error & ATA_SE_PHY_CHANGED) &&
1942 (tp = (struct ata_connect_task *)
1943 malloc(sizeof(struct ata_connect_task),
1944 M_ATA, M_NOWAIT | M_ZERO))) {
1946 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
1948 device_printf(ch->dev, "CONNECT requested\n");
1949 tp->action = ATA_C_ATTACH;
1953 device_printf(ch->dev, "DISCONNECT requested\n");
1954 tp->action = ATA_C_DETACH;
1957 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1958 taskqueue_enqueue(taskqueue_thread, &tp->task);
1962 /* any drive action to take care of ? */
1963 return ata_pci_status(dev);
1967 ata_intel_31244_command(struct ata_request *request)
1969 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
1970 struct ata_device *atadev = device_get_softc(request->dev);
1973 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
1974 return (ata_generic_command(request));
1976 lba = request->u.ata.lba;
1977 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | atadev->unit);
1978 /* enable interrupt */
1979 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
1980 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
1981 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
1982 ATA_IDX_OUTW(ch, ATA_SECTOR, ((lba >> 16) & 0xff00) | (lba & 0x00ff));
1983 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((lba >> 24) & 0xff00) |
1984 ((lba >> 8) & 0x00ff));
1985 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((lba >> 32) & 0xff00) |
1986 ((lba >> 16) & 0x00ff));
1988 /* issue command to controller */
1989 ATA_IDX_OUTB(ch, ATA_COMMAND, request->u.ata.command);
1995 ata_intel_31244_reset(device_t dev)
1997 struct ata_channel *ch = device_get_softc(dev);
1999 ata_sata_phy_enable(ch);
2004 * Integrated Technology Express Inc. (ITE) chipset support functions
2007 ata_ite_ident(device_t dev)
2009 struct ata_pci_controller *ctlr = device_get_softc(dev);
2010 struct ata_chip_id *idx;
2011 static struct ata_chip_id ids[] =
2012 {{ ATA_IT8212F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8212F" },
2013 { ATA_IT8211F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8211F" },
2014 { 0, 0, 0, 0, 0, 0}};
2017 if (!(idx = ata_match_chip(dev, ids)))
2020 sprintf(buffer, "ITE %s %s controller",
2021 idx->text, ata_mode2str(idx->max_dma));
2022 device_set_desc_copy(dev, buffer);
2024 ctlr->chipinit = ata_ite_chipinit;
2029 ata_ite_chipinit(device_t dev)
2031 struct ata_pci_controller *ctlr = device_get_softc(dev);
2033 if (ata_setup_interrupt(dev))
2036 ctlr->setmode = ata_ite_setmode;
2038 /* set PCI mode and 66Mhz reference clock */
2039 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
2041 /* set default active & recover timings */
2042 pci_write_config(dev, 0x54, 0x31, 1);
2043 pci_write_config(dev, 0x56, 0x31, 1);
2048 ata_ite_setmode(device_t dev, int mode)
2050 device_t gparent = GRANDPARENT(dev);
2051 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2052 struct ata_device *atadev = device_get_softc(dev);
2053 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2056 /* correct the mode for what the HW supports */
2057 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2059 /* check the CBLID bits for 80 conductor cable detection */
2060 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
2061 (ch->unit ? (1<<3) : (1<<2)))) {
2062 ata_print_cable(dev, "controller");
2066 /* set the wanted mode on the device */
2067 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2070 device_printf(dev, "%s setting %s on ITE8212F chip\n",
2071 (error) ? "failed" : "success", ata_mode2str(mode));
2073 /* if the device accepted the mode change, setup the HW accordingly */
2075 if (mode >= ATA_UDMA0) {
2076 u_int8_t udmatiming[] =
2077 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
2079 /* enable UDMA mode */
2080 pci_write_config(gparent, 0x50,
2081 pci_read_config(gparent, 0x50, 1) &
2082 ~(1 << (devno + 3)), 1);
2084 /* set UDMA timing */
2085 pci_write_config(gparent,
2086 0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
2087 udmatiming[mode & ATA_MODE_MASK], 1);
2090 u_int8_t chtiming[] =
2091 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
2093 /* disable UDMA mode */
2094 pci_write_config(gparent, 0x50,
2095 pci_read_config(gparent, 0x50, 1) |
2096 (1 << (devno + 3)), 1);
2098 /* set active and recover timing (shared between master & slave) */
2099 if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
2100 chtiming[ata_mode2idx(mode)])
2101 pci_write_config(gparent, 0x54 + (ch->unit << 2),
2102 chtiming[ata_mode2idx(mode)], 1);
2104 atadev->mode = mode;
2110 * JMicron chipset support functions
2113 ata_jmicron_ident(device_t dev)
2115 struct ata_pci_controller *ctlr = device_get_softc(dev);
2116 struct ata_chip_id *idx;
2117 static struct ata_chip_id ids[] =
2118 {{ ATA_JMB360, 0, 1, 0, ATA_SA300, "JMB360" },
2119 { ATA_JMB361, 0, 1, 1, ATA_SA300, "JMB361" },
2120 { ATA_JMB363, 0, 2, 1, ATA_SA300, "JMB363" },
2121 { ATA_JMB365, 0, 1, 2, ATA_SA300, "JMB365" },
2122 { ATA_JMB366, 0, 2, 2, ATA_SA300, "JMB366" },
2123 { 0, 0, 0, 0, 0, 0}};
2126 if (!(idx = ata_match_chip(dev, ids)))
2129 sprintf(buffer, "JMicron %s %s controller",
2130 idx->text, ata_mode2str(idx->max_dma));
2131 device_set_desc_copy(dev, buffer);
2133 ctlr->chipinit = ata_jmicron_chipinit;
2138 ata_jmicron_chipinit(device_t dev)
2140 struct ata_pci_controller *ctlr = device_get_softc(dev);
2143 if (ata_setup_interrupt(dev))
2146 /* set controller configuration to a setup we support */
2147 pci_write_config(dev, 0x40, 0x80c0a131, 4);
2148 pci_write_config(dev, 0x80, 0x01200000, 4);
2150 ctlr->allocate = ata_jmicron_allocate;
2151 ctlr->reset = ata_jmicron_reset;
2152 ctlr->dmainit = ata_jmicron_dmainit;
2153 ctlr->setmode = ata_jmicron_setmode;
2155 ctlr->r_type2 = SYS_RES_MEMORY;
2156 ctlr->r_rid2 = PCIR_BAR(5);
2157 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2158 &ctlr->r_rid2, RF_ACTIVE))) {
2159 if ((error = ata_ahci_chipinit(dev)))
2163 /* set the number of HW channels */
2164 ctlr->channels = ctlr->chip->cfg1 + ctlr->chip->cfg2;
2169 ata_jmicron_allocate(device_t dev)
2171 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2172 struct ata_channel *ch = device_get_softc(dev);
2175 if (ch->unit >= ctlr->chip->cfg1) {
2176 ch->unit -= ctlr->chip->cfg1;
2177 error = ata_pci_allocate(dev);
2178 ch->unit += ctlr->chip->cfg1;
2181 error = ata_ahci_allocate(dev);
2186 ata_jmicron_reset(device_t dev)
2188 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2189 struct ata_channel *ch = device_get_softc(dev);
2191 if (ch->unit >= ctlr->chip->cfg1)
2192 ata_generic_reset(dev);
2194 ata_ahci_reset(dev);
2198 ata_jmicron_dmainit(device_t dev)
2200 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2201 struct ata_channel *ch = device_get_softc(dev);
2203 if (ch->unit >= ctlr->chip->cfg1)
2204 ata_pci_dmainit(dev);
2206 ata_ahci_dmainit(dev);
2210 ata_jmicron_setmode(device_t dev, int mode)
2212 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2213 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2215 if (ch->unit >= ctlr->chip->cfg1) {
2216 struct ata_device *atadev = device_get_softc(dev);
2218 /* check for 80pin cable present */
2219 if (pci_read_config(dev, 0x40, 1) & 0x08)
2220 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2222 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2224 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
2225 atadev->mode = mode;
2228 ata_sata_setmode(dev, mode);
2233 * Marvell chipset support functions
2235 #define ATA_MV_HOST_BASE(ch) \
2236 ((ch->unit & 3) * 0x0100) + (ch->unit > 3 ? 0x30000 : 0x20000)
2237 #define ATA_MV_EDMA_BASE(ch) \
2238 ((ch->unit & 3) * 0x2000) + (ch->unit > 3 ? 0x30000 : 0x20000)
2240 struct ata_marvell_response {
2242 u_int8_t edma_status;
2243 u_int8_t dev_status;
2244 u_int32_t timestamp;
2247 struct ata_marvell_dma_prdentry {
2255 ata_marvell_ident(device_t dev)
2257 struct ata_pci_controller *ctlr = device_get_softc(dev);
2258 struct ata_chip_id *idx;
2259 static struct ata_chip_id ids[] =
2260 {{ ATA_M88SX5040, 0, 4, MV5XXX, ATA_SA150, "88SX5040" },
2261 { ATA_M88SX5041, 0, 4, MV5XXX, ATA_SA150, "88SX5041" },
2262 { ATA_M88SX5080, 0, 8, MV5XXX, ATA_SA150, "88SX5080" },
2263 { ATA_M88SX5081, 0, 8, MV5XXX, ATA_SA150, "88SX5081" },
2264 { ATA_M88SX6041, 0, 4, MV6XXX, ATA_SA300, "88SX6041" },
2265 { ATA_M88SX6081, 0, 8, MV6XXX, ATA_SA300, "88SX6081" },
2266 { 0, 0, 0, 0, 0, 0}};
2269 if (!(idx = ata_match_chip(dev, ids)))
2272 sprintf(buffer, "Marvell %s %s controller",
2273 idx->text, ata_mode2str(idx->max_dma));
2274 device_set_desc_copy(dev, buffer);
2276 ctlr->chipinit = ata_marvell_chipinit;
2281 ata_marvell_chipinit(device_t dev)
2283 struct ata_pci_controller *ctlr = device_get_softc(dev);
2285 if (ata_setup_interrupt(dev))
2288 ctlr->r_type1 = SYS_RES_MEMORY;
2289 ctlr->r_rid1 = PCIR_BAR(0);
2290 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
2291 &ctlr->r_rid1, RF_ACTIVE)))
2294 /* mask all host controller interrupts */
2295 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x00000000);
2297 /* mask all PCI interrupts */
2298 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x00000000);
2300 ctlr->allocate = ata_marvell_allocate;
2301 ctlr->reset = ata_marvell_reset;
2302 ctlr->dmainit = ata_marvell_dmainit;
2303 ctlr->setmode = ata_sata_setmode;
2304 ctlr->channels = ctlr->chip->cfg1;
2306 /* clear host controller interrupts */
2307 ATA_OUTL(ctlr->r_res1, 0x20014, 0x00000000);
2308 if (ctlr->chip->cfg1 > 4)
2309 ATA_OUTL(ctlr->r_res1, 0x30014, 0x00000000);
2311 /* clear PCI interrupts */
2312 ATA_OUTL(ctlr->r_res1, 0x01d58, 0x00000000);
2314 /* unmask PCI interrupts we want */
2315 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x007fffff);
2317 /* unmask host controller interrupts we want */
2318 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x000000ff/*HC0*/ | 0x0001fe00/*HC1*/ |
2319 /*(1<<19) | (1<<20) | (1<<21) |*/(1<<22) | (1<<24) | (0x7f << 25));
2321 /* enable PCI interrupt */
2322 pci_write_config(dev, PCIR_COMMAND,
2323 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
2328 ata_marvell_allocate(device_t dev)
2330 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2331 struct ata_channel *ch = device_get_softc(dev);
2332 bus_addr_t work = ch->dma->work_bus;
2335 /* clear work area */
2336 bzero(ch->dma->work, 1024+256);
2338 /* set legacy ATA resources */
2339 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2340 ch->r_io[i].res = ctlr->r_res1;
2341 ch->r_io[i].offset = 0x02100 + (i << 2) + ATA_MV_EDMA_BASE(ch);
2343 ch->r_io[ATA_CONTROL].res = ctlr->r_res1;
2344 ch->r_io[ATA_CONTROL].offset = 0x02120 + ATA_MV_EDMA_BASE(ch);
2345 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res1;
2346 ata_default_registers(dev);
2348 /* set SATA resources */
2349 switch (ctlr->chip->cfg2) {
2351 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2352 ch->r_io[ATA_SSTATUS].offset = 0x00100 + ATA_MV_HOST_BASE(ch);
2353 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2354 ch->r_io[ATA_SERROR].offset = 0x00104 + ATA_MV_HOST_BASE(ch);
2355 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2356 ch->r_io[ATA_SCONTROL].offset = 0x00108 + ATA_MV_HOST_BASE(ch);
2359 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2360 ch->r_io[ATA_SSTATUS].offset = 0x02300 + ATA_MV_EDMA_BASE(ch);
2361 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2362 ch->r_io[ATA_SERROR].offset = 0x02304 + ATA_MV_EDMA_BASE(ch);
2363 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2364 ch->r_io[ATA_SCONTROL].offset = 0x02308 + ATA_MV_EDMA_BASE(ch);
2365 ch->r_io[ATA_SACTIVE].res = ctlr->r_res1;
2366 ch->r_io[ATA_SACTIVE].offset = 0x02350 + ATA_MV_EDMA_BASE(ch);
2370 ch->flags |= ATA_NO_SLAVE;
2371 ch->flags |= ATA_USE_16BIT; /* XXX SOS needed ? */
2372 ata_generic_hw(dev);
2373 ch->hw.begin_transaction = ata_marvell_begin_transaction;
2374 ch->hw.end_transaction = ata_marvell_end_transaction;
2375 ch->hw.status = ata_marvell_status;
2377 /* disable the EDMA machinery */
2378 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2379 DELAY(100000); /* SOS should poll for disabled */
2381 /* set configuration to non-queued 128b read transfers stop on error */
2382 ATA_OUTL(ctlr->r_res1, 0x02000 + ATA_MV_EDMA_BASE(ch), (1<<11) | (1<<13));
2384 /* request queue base high */
2385 ATA_OUTL(ctlr->r_res1, 0x02010 + ATA_MV_EDMA_BASE(ch), (work >> 16) >> 16);
2387 /* request queue in ptr */
2388 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2390 /* request queue out ptr */
2391 ATA_OUTL(ctlr->r_res1, 0x02018 + ATA_MV_EDMA_BASE(ch), 0x0);
2393 /* response queue base high */
2395 ATA_OUTL(ctlr->r_res1, 0x0201c + ATA_MV_EDMA_BASE(ch), (work >> 16) >> 16);
2397 /* response queue in ptr */
2398 ATA_OUTL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch), 0x0);
2400 /* response queue out ptr */
2401 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2403 /* clear SATA error register */
2404 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2406 /* clear any outstanding error interrupts */
2407 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2409 /* unmask all error interrupts */
2410 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2412 /* enable EDMA machinery */
2413 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2418 ata_marvell_status(device_t dev)
2420 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2421 struct ata_channel *ch = device_get_softc(dev);
2422 u_int32_t cause = ATA_INL(ctlr->r_res1, 0x01d60);
2423 int shift = (ch->unit << 1) + (ch->unit > 3);
2425 /* do we have any errors flagged ? */
2426 if (cause & (1 << shift)) {
2427 struct ata_connect_task *tp;
2429 ATA_INL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch));
2431 /* check for and handle disconnect events */
2432 if ((error & 0x00000008) &&
2433 (tp = (struct ata_connect_task *)
2434 malloc(sizeof(struct ata_connect_task),
2435 M_ATA, M_NOWAIT | M_ZERO))) {
2438 device_printf(ch->dev, "DISCONNECT requested\n");
2439 tp->action = ATA_C_DETACH;
2441 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2442 taskqueue_enqueue(taskqueue_thread, &tp->task);
2445 /* check for and handle connect events */
2446 if ((error & 0x00000010) &&
2447 (tp = (struct ata_connect_task *)
2448 malloc(sizeof(struct ata_connect_task),
2449 M_ATA, M_NOWAIT | M_ZERO))) {
2452 device_printf(ch->dev, "CONNECT requested\n");
2453 tp->action = ATA_C_ATTACH;
2455 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2456 taskqueue_enqueue(taskqueue_thread, &tp->task);
2459 /* clear SATA error register */
2460 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2462 /* clear any outstanding error interrupts */
2463 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2466 /* do we have any device action ? */
2467 return (cause & (2 << shift));
2470 /* must be called with ATA channel locked and state_mtx held */
2472 ata_marvell_begin_transaction(struct ata_request *request)
2474 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2475 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2481 int dummy, error, slot;
2483 /* only DMA R/W goes through the EMDA machine */
2484 if (request->u.ata.command != ATA_READ_DMA &&
2485 request->u.ata.command != ATA_WRITE_DMA) {
2487 /* disable the EDMA machinery */
2488 if (ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)
2489 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2490 return ata_begin_transaction(request);
2493 /* check for 48 bit access and convert if needed */
2494 ata_modify_if_48bit(request);
2496 /* check sanity, setup SG list and DMA engine */
2497 if ((error = ch->dma->load(ch->dev, request->data, request->bytecount,
2498 request->flags & ATA_R_READ, ch->dma->sg,
2500 device_printf(request->dev, "setting up DMA failed\n");
2501 request->result = error;
2502 return ATA_OP_FINISHED;
2505 /* get next free request queue slot */
2506 req_in = ATA_INL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch));
2507 slot = (((req_in & ~0xfffffc00) >> 5) + 0) & 0x1f;
2508 bytep = (u_int8_t *)(ch->dma->work);
2509 bytep += (slot << 5);
2510 wordp = (u_int16_t *)bytep;
2511 quadp = (u_int32_t *)bytep;
2513 /* fill in this request */
2514 quadp[0] = (long)ch->dma->sg_bus & 0xffffffff;
2515 quadp[1] = (ch->dma->sg_bus & 0xffffffff00000000) >> 32;
2516 wordp[4] = (request->flags & ATA_R_READ ? 0x01 : 0x00) | (tag<<1);
2519 bytep[i++] = (request->u.ata.count >> 8) & 0xff;
2520 bytep[i++] = 0x10 | ATA_COUNT;
2521 bytep[i++] = request->u.ata.count & 0xff;
2522 bytep[i++] = 0x10 | ATA_COUNT;
2524 bytep[i++] = (request->u.ata.lba >> 24) & 0xff;
2525 bytep[i++] = 0x10 | ATA_SECTOR;
2526 bytep[i++] = request->u.ata.lba & 0xff;
2527 bytep[i++] = 0x10 | ATA_SECTOR;
2529 bytep[i++] = (request->u.ata.lba >> 32) & 0xff;
2530 bytep[i++] = 0x10 | ATA_CYL_LSB;
2531 bytep[i++] = (request->u.ata.lba >> 8) & 0xff;
2532 bytep[i++] = 0x10 | ATA_CYL_LSB;
2534 bytep[i++] = (request->u.ata.lba >> 40) & 0xff;
2535 bytep[i++] = 0x10 | ATA_CYL_MSB;
2536 bytep[i++] = (request->u.ata.lba >> 16) & 0xff;
2537 bytep[i++] = 0x10 | ATA_CYL_MSB;
2539 bytep[i++] = ATA_D_LBA | ATA_D_IBM | ((request->u.ata.lba >> 24) & 0xf);
2540 bytep[i++] = 0x10 | ATA_DRIVE;
2542 bytep[i++] = request->u.ata.command;
2543 bytep[i++] = 0x90 | ATA_COMMAND;
2545 /* enable EDMA machinery if needed */
2546 if (!(ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)) {
2547 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2548 while (!(ATA_INL(ctlr->r_res1,
2549 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
2553 /* tell EDMA it has a new request */
2554 slot = (((req_in & ~0xfffffc00) >> 5) + 1) & 0x1f;
2555 req_in &= 0xfffffc00;
2556 req_in += (slot << 5);
2557 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), req_in);
2559 return ATA_OP_CONTINUES;
2562 /* must be called with ATA channel locked and state_mtx held */
2564 ata_marvell_end_transaction(struct ata_request *request)
2566 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2567 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2568 int offset = (ch->unit > 3 ? 0x30014 : 0x20014);
2569 u_int32_t icr = ATA_INL(ctlr->r_res1, offset);
2572 /* EDMA interrupt */
2573 if ((icr & (0x0001 << (ch->unit & 3)))) {
2574 struct ata_marvell_response *response;
2575 u_int32_t rsp_in, rsp_out;
2578 /* unload SG list */
2579 ch->dma->unload(ch->dev);
2582 callout_stop(&request->callout);
2584 /* get response ptr's */
2585 rsp_in = ATA_INL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch));
2586 rsp_out = ATA_INL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch));
2587 slot = (((rsp_in & ~0xffffff00) >> 3)) & 0x1f;
2588 rsp_out &= 0xffffff00;
2589 rsp_out += (slot << 3);
2590 response = (struct ata_marvell_response *)
2591 (ch->dma->work + 1024 + (slot << 3));
2593 /* record status for this request */
2594 request->status = response->dev_status;
2598 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), rsp_out);
2600 /* update progress */
2601 if (!(request->status & ATA_S_ERROR) &&
2602 !(request->flags & ATA_R_TIMEOUT))
2603 request->donecount = request->bytecount;
2604 res = ATA_OP_FINISHED;
2607 /* legacy ATA interrupt */
2609 res = ata_end_transaction(request);
2613 ATA_OUTL(ctlr->r_res1, offset, ~(icr & (0x0101 << (ch->unit & 3))));
2618 ata_marvell_reset(device_t dev)
2620 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2621 struct ata_channel *ch = device_get_softc(dev);
2623 /* disable the EDMA machinery */
2624 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2625 while ((ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
2628 /* clear SATA error register */
2629 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2631 /* clear any outstanding error interrupts */
2632 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2634 /* unmask all error interrupts */
2635 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2637 /* enable channel and test for devices */
2638 ata_sata_phy_enable(ch);
2640 /* enable EDMA machinery */
2641 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2645 ata_marvell_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
2647 struct ata_dmasetprd_args *args = xsc;
2648 struct ata_marvell_dma_prdentry *prd = args->dmatab;
2651 if ((args->error = error))
2654 for (i = 0; i < nsegs; i++) {
2655 prd[i].addrlo = htole32(segs[i].ds_addr);
2657 prd[i].count = htole32(segs[i].ds_len);
2659 prd[i - 1].count |= htole32(ATA_DMA_EOT);
2663 ata_marvell_dmainit(device_t dev)
2665 struct ata_channel *ch = device_get_softc(dev);
2669 /* note start and stop are not used here */
2670 ch->dma->setprd = ata_marvell_dmasetprd;
2676 * National chipset support functions
2679 ata_national_ident(device_t dev)
2681 struct ata_pci_controller *ctlr = device_get_softc(dev);
2683 /* this chip is a clone of the Cyrix chip, bugs and all */
2684 if (pci_get_devid(dev) == ATA_SC1100) {
2685 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
2686 ctlr->chipinit = ata_national_chipinit;
2693 ata_national_chipinit(device_t dev)
2695 struct ata_pci_controller *ctlr = device_get_softc(dev);
2697 if (ata_setup_interrupt(dev))
2700 ctlr->setmode = ata_national_setmode;
2705 ata_national_setmode(device_t dev, int mode)
2707 device_t gparent = GRANDPARENT(dev);
2708 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2709 struct ata_device *atadev = device_get_softc(dev);
2710 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2711 u_int32_t piotiming[] =
2712 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
2713 0x00803020, 0x20102010, 0x00100010,
2714 0x00100010, 0x00100010, 0x00100010 };
2715 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
2716 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
2719 ch->dma->alignment = 16;
2720 ch->dma->max_iosize = 126 * DEV_BSIZE;
2722 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2724 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2727 device_printf(dev, "%s setting %s on National chip\n",
2728 (error) ? "failed" : "success", ata_mode2str(mode));
2730 if (mode >= ATA_UDMA0) {
2731 pci_write_config(gparent, 0x44 + (devno << 3),
2732 udmatiming[mode & ATA_MODE_MASK], 4);
2734 else if (mode >= ATA_WDMA0) {
2735 pci_write_config(gparent, 0x44 + (devno << 3),
2736 dmatiming[mode & ATA_MODE_MASK], 4);
2739 pci_write_config(gparent, 0x44 + (devno << 3),
2740 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
2743 pci_write_config(gparent, 0x40 + (devno << 3),
2744 piotiming[ata_mode2idx(mode)], 4);
2745 atadev->mode = mode;
2751 * nVidia chipset support functions
2754 ata_nvidia_ident(device_t dev)
2756 struct ata_pci_controller *ctlr = device_get_softc(dev);
2757 struct ata_chip_id *idx;
2758 static struct ata_chip_id ids[] =
2759 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nForce" },
2760 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2" },
2761 { ATA_NFORCE2_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2 Pro" },
2762 { ATA_NFORCE2_PRO_S1, 0, 0, 0, ATA_SA150, "nForce2 Pro" },
2763 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3" },
2764 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3 Pro" },
2765 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
2766 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
2767 { ATA_NFORCE_MCP04, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP" },
2768 { ATA_NFORCE_MCP04_S1, 0, 0, NV4OFF, ATA_SA150, "nForce MCP" },
2769 { ATA_NFORCE_MCP04_S2, 0, 0, NV4OFF, ATA_SA150, "nForce MCP" },
2770 { ATA_NFORCE_CK804, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce CK804" },
2771 { ATA_NFORCE_CK804_S1, 0, 0, NV4OFF, ATA_SA300, "nForce CK804" },
2772 { ATA_NFORCE_CK804_S2, 0, 0, NV4OFF, ATA_SA300, "nForce CK804" },
2773 { ATA_NFORCE_MCP51, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP51" },
2774 { ATA_NFORCE_MCP51_S1, 0, 0, NV4OFF, ATA_SA300, "nForce MCP51" },
2775 { ATA_NFORCE_MCP51_S2, 0, 0, NV4OFF, ATA_SA300, "nForce MCP51" },
2776 { ATA_NFORCE_MCP55, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP55" },
2777 { ATA_NFORCE_MCP55_S1, 0, 0, NV4OFF, ATA_SA300, "nForce MCP55" },
2778 { ATA_NFORCE_MCP55_S2, 0, 0, NV4OFF, ATA_SA300, "nForce MCP55" },
2779 { 0, 0, 0, 0, 0, 0}} ;
2782 if (!(idx = ata_match_chip(dev, ids)))
2785 sprintf(buffer, "nVidia %s %s controller",
2786 idx->text, ata_mode2str(idx->max_dma));
2787 device_set_desc_copy(dev, buffer);
2789 ctlr->chipinit = ata_nvidia_chipinit;
2794 ata_nvidia_chipinit(device_t dev)
2796 struct ata_pci_controller *ctlr = device_get_softc(dev);
2798 if (ata_setup_interrupt(dev))
2801 if (ctlr->chip->max_dma >= ATA_SA150) {
2802 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
2803 ctlr->r_type2 = SYS_RES_IOPORT;
2805 ctlr->r_type2 = SYS_RES_MEMORY;
2806 ctlr->r_rid2 = PCIR_BAR(5);
2807 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2808 &ctlr->r_rid2, RF_ACTIVE))) {
2809 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
2811 ctlr->allocate = ata_nvidia_allocate;
2812 ctlr->reset = ata_nvidia_reset;
2814 /* enable control access */
2815 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
2817 /* clear interrupt status */
2818 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2820 /* enable device and PHY state change interrupts */
2821 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
2823 /* enable PCI interrupt */
2824 pci_write_config(dev, PCIR_COMMAND,
2825 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
2828 ctlr->setmode = ata_sata_setmode;
2831 /* disable prefetch, postwrite */
2832 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
2833 ctlr->setmode = ata_via_family_setmode;
2839 ata_nvidia_allocate(device_t dev)
2841 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2842 struct ata_channel *ch = device_get_softc(dev);
2844 /* setup the usual register normal pci style */
2845 if (ata_pci_allocate(dev))
2848 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2849 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
2850 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2851 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
2852 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2853 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
2855 ch->hw.status = ata_nvidia_status;
2856 ch->flags |= ATA_NO_SLAVE;
2862 ata_nvidia_status(device_t dev)
2864 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2865 struct ata_channel *ch = device_get_softc(dev);
2866 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
2867 struct ata_connect_task *tp;
2868 int shift = ch->unit << 2;
2871 /* get interrupt status */
2872 status = ATA_INB(ctlr->r_res2, offset);
2874 /* check for and handle connect events */
2875 if (((status & (0x0c << shift)) == (0x04 << shift)) &&
2876 (tp = (struct ata_connect_task *)
2877 malloc(sizeof(struct ata_connect_task),
2878 M_ATA, M_NOWAIT | M_ZERO))) {
2881 device_printf(ch->dev, "CONNECT requested\n");
2882 tp->action = ATA_C_ATTACH;
2884 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2885 taskqueue_enqueue(taskqueue_thread, &tp->task);
2888 /* check for and handle disconnect events */
2889 if ((status & (0x08 << shift)) &&
2890 (tp = (struct ata_connect_task *)
2891 malloc(sizeof(struct ata_connect_task),
2892 M_ATA, M_NOWAIT | M_ZERO))) {
2895 device_printf(ch->dev, "DISCONNECT requested\n");
2896 tp->action = ATA_C_DETACH;
2898 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2899 taskqueue_enqueue(taskqueue_thread, &tp->task);
2902 /* clear interrupt status */
2903 ATA_OUTB(ctlr->r_res2, offset, (0x0f << shift));
2905 /* do we have any device action ? */
2906 return (status & (0x01 << shift));
2910 ata_nvidia_reset(device_t dev)
2912 struct ata_channel *ch = device_get_softc(dev);
2914 ata_sata_phy_enable(ch);
2919 * Promise chipset support functions
2921 #define ATA_PDC_APKT_OFFSET 0x00000010
2922 #define ATA_PDC_HPKT_OFFSET 0x00000040
2923 #define ATA_PDC_ASG_OFFSET 0x00000080
2924 #define ATA_PDC_LSG_OFFSET 0x000000c0
2925 #define ATA_PDC_HSG_OFFSET 0x00000100
2926 #define ATA_PDC_CHN_OFFSET 0x00000400
2927 #define ATA_PDC_BUF_BASE 0x00400000
2928 #define ATA_PDC_BUF_OFFSET 0x00100000
2929 #define ATA_PDC_MAX_HPKT 8
2930 #define ATA_PDC_WRITE_REG 0x00
2931 #define ATA_PDC_WRITE_CTL 0x0e
2932 #define ATA_PDC_WRITE_END 0x08
2933 #define ATA_PDC_WAIT_NBUSY 0x10
2934 #define ATA_PDC_WAIT_READY 0x18
2935 #define ATA_PDC_1B 0x20
2936 #define ATA_PDC_2B 0x40
2938 struct host_packet {
2940 TAILQ_ENTRY(host_packet) chain;
2943 struct ata_promise_sx4 {
2945 TAILQ_HEAD(, host_packet) queue;
2950 ata_promise_ident(device_t dev)
2952 struct ata_pci_controller *ctlr = device_get_softc(dev);
2953 struct ata_chip_id *idx;
2954 static struct ata_chip_id ids[] =
2955 {{ ATA_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "PDC20246" },
2956 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20262" },
2957 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20263" },
2958 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20265" },
2959 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20267" },
2960 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20268" },
2961 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "PDC20269" },
2962 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20270" },
2963 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "PDC20271" },
2964 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "PDC20275" },
2965 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "PDC20276" },
2966 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "PDC20277" },
2967 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "PDC20318" },
2968 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "PDC20319" },
2969 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20371" },
2970 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20375" },
2971 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20376" },
2972 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20377" },
2973 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20378" },
2974 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20379" },
2975 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20571" },
2976 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20575" },
2977 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20579" },
2978 { ATA_PDC20771, 0, PRMIO, PRCMBO2, ATA_SA300, "PDC20771" },
2979 { ATA_PDC40775, 0, PRMIO, PRCMBO2, ATA_SA300, "PDC40775" },
2980 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20617" },
2981 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20618" },
2982 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20619" },
2983 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20620" },
2984 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "PDC20621" },
2985 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "PDC20622" },
2986 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40518" },
2987 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40519" },
2988 { ATA_PDC40718, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40718" },
2989 { ATA_PDC40719, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40719" },
2990 { ATA_PDC40779, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40779" },
2991 { 0, 0, 0, 0, 0, 0}};
2993 uintptr_t devid = 0;
2995 if (!(idx = ata_match_chip(dev, ids)))
2998 /* if we are on a SuperTrak SX6000 dont attach */
2999 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
3000 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
3001 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
3002 devid == ATA_I960RM)
3005 strcpy(buffer, "Promise ");
3006 strcat(buffer, idx->text);
3008 /* if we are on a FastTrak TX4, adjust the interrupt resource */
3009 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
3010 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
3011 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
3012 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
3013 static long start = 0, end = 0;
3015 if (pci_get_slot(dev) == 1) {
3016 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
3017 strcat(buffer, " (channel 0+1)");
3019 else if (pci_get_slot(dev) == 2 && start && end) {
3020 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
3021 strcat(buffer, " (channel 2+3)");
3027 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
3028 device_set_desc_copy(dev, buffer);
3030 ctlr->chipinit = ata_promise_chipinit;
3035 ata_promise_chipinit(device_t dev)
3037 struct ata_pci_controller *ctlr = device_get_softc(dev);
3038 int fake_reg, stat_reg;
3040 if (ata_setup_interrupt(dev))
3043 switch (ctlr->chip->cfg1) {
3046 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
3048 ctlr->dmainit = ata_promise_dmainit;
3052 /* enable burst mode */
3053 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
3054 ctlr->allocate = ata_promise_allocate;
3055 ctlr->setmode = ata_promise_setmode;
3059 ctlr->allocate = ata_promise_tx2_allocate;
3060 ctlr->setmode = ata_promise_setmode;
3064 ctlr->r_type1 = SYS_RES_MEMORY;
3065 ctlr->r_rid1 = PCIR_BAR(4);
3066 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
3067 &ctlr->r_rid1, RF_ACTIVE)))
3070 ctlr->r_type2 = SYS_RES_MEMORY;
3071 ctlr->r_rid2 = PCIR_BAR(3);
3072 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3073 &ctlr->r_rid2, RF_ACTIVE)))
3076 if (ctlr->chip->cfg2 == PRSX4X) {
3077 struct ata_promise_sx4 *hpkt;
3078 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
3080 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
3081 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3082 ata_promise_sx4_intr, ctlr, &ctlr->handle)) {
3083 device_printf(dev, "unable to setup interrupt\n");
3087 /* print info about cache memory */
3088 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
3089 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
3090 ((dimm >> 24) & 0xff),
3091 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
3092 " ECC enabled" : "" );
3094 /* adjust cache memory parameters */
3095 ATA_OUTL(ctlr->r_res2, 0x000c000c,
3096 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
3098 /* setup host packet controls */
3099 hpkt = malloc(sizeof(struct ata_promise_sx4),
3100 M_TEMP, M_NOWAIT | M_ZERO);
3101 mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
3102 TAILQ_INIT(&hpkt->queue);
3104 device_set_ivars(dev, hpkt);
3105 ctlr->allocate = ata_promise_mio_allocate;
3106 ctlr->reset = ata_promise_mio_reset;
3107 ctlr->dmainit = ata_promise_mio_dmainit;
3108 ctlr->setmode = ata_promise_setmode;
3113 /* mio type controllers need an interrupt intercept */
3114 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
3115 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3116 ata_promise_mio_intr, ctlr, &ctlr->handle)) {
3117 device_printf(dev, "unable to setup interrupt\n");
3121 switch (ctlr->chip->cfg2) {
3123 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
3124 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
3148 /* prime fake interrupt register */
3149 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3151 /* clear SATA status */
3152 ATA_OUTL(ctlr->r_res2, stat_reg, 0x000000ff);
3154 ctlr->allocate = ata_promise_mio_allocate;
3155 ctlr->reset = ata_promise_mio_reset;
3156 ctlr->dmainit = ata_promise_mio_dmainit;
3157 ctlr->setmode = ata_promise_mio_setmode;
3164 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
3166 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
3171 ata_promise_allocate(device_t dev)
3173 struct ata_channel *ch = device_get_softc(dev);
3175 if (ata_pci_allocate(dev))
3178 ch->hw.status = ata_promise_status;
3183 ata_promise_status(device_t dev)
3185 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3186 struct ata_channel *ch = device_get_softc(dev);
3188 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)) {
3189 return ata_pci_status(dev);
3195 ata_promise_dmastart(device_t dev)
3197 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3198 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3199 struct ata_device *atadev = device_get_softc(dev);
3201 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3202 ATA_OUTB(ctlr->r_res1, 0x11,
3203 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
3204 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20,
3205 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
3206 (ch->dma->cur_iosize >> 1));
3208 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
3209 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
3210 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
3211 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3212 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
3213 ATA_BMCMD_START_STOP);
3214 ch->flags |= ATA_DMA_ACTIVE;
3219 ata_promise_dmastop(device_t dev)
3221 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3222 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3223 struct ata_device *atadev = device_get_softc(dev);
3226 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3227 ATA_OUTB(ctlr->r_res1, 0x11,
3228 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
3229 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20, 0);
3231 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
3232 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3233 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3234 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3235 ch->flags &= ~ATA_DMA_ACTIVE;
3240 ata_promise_dmareset(device_t dev)
3242 struct ata_channel *ch = device_get_softc(dev);
3244 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3245 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3246 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3247 ch->flags &= ~ATA_DMA_ACTIVE;
3251 ata_promise_dmainit(device_t dev)
3253 struct ata_channel *ch = device_get_softc(dev);
3257 ch->dma->start = ata_promise_dmastart;
3258 ch->dma->stop = ata_promise_dmastop;
3259 ch->dma->reset = ata_promise_dmareset;
3264 ata_promise_setmode(device_t dev, int mode)
3266 device_t gparent = GRANDPARENT(dev);
3267 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3268 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3269 struct ata_device *atadev = device_get_softc(dev);
3270 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3272 u_int32_t timings[][2] = {
3273 /* PROLD PRNEW mode */
3274 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
3275 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
3276 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
3277 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
3278 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
3279 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
3280 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
3281 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
3282 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
3283 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
3284 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
3285 { 0, 0x00424ef6 }, /* UDMA 3 */
3286 { 0, 0x004127f3 }, /* UDMA 4 */
3287 { 0, 0x004127f3 } /* UDMA 5 */
3290 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3292 switch (ctlr->chip->cfg1) {
3295 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
3296 (ch->unit ? 1 << 11 : 1 << 10))) {
3297 ata_print_cable(dev, "controller");
3300 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
3301 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
3305 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3306 if (mode > ATA_UDMA2 &&
3307 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
3308 ata_print_cable(dev, "controller");
3314 if (mode > ATA_UDMA2 &&
3315 (ATA_INL(ctlr->r_res2,
3316 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
3317 (ch->unit << 7)) & 0x01000000)) {
3318 ata_print_cable(dev, "controller");
3324 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3327 device_printf(dev, "%ssetting %s on %s chip\n",
3328 (error) ? "FAILURE " : "",
3329 ata_mode2str(mode), ctlr->chip->text);
3331 if (ctlr->chip->cfg1 < PRTX)
3332 pci_write_config(gparent, 0x60 + (devno << 2),
3333 timings[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
3334 atadev->mode = mode;
3340 ata_promise_tx2_allocate(device_t dev)
3342 struct ata_channel *ch = device_get_softc(dev);
3344 if (ata_pci_allocate(dev))
3347 ch->hw.status = ata_promise_tx2_status;
3352 ata_promise_tx2_status(device_t dev)
3354 struct ata_channel *ch = device_get_softc(dev);
3356 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3357 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
3358 return ata_pci_status(dev);
3364 ata_promise_mio_allocate(device_t dev)
3366 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3367 struct ata_channel *ch = device_get_softc(dev);
3368 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
3371 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
3372 ch->r_io[i].res = ctlr->r_res2;
3373 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
3375 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
3376 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
3377 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
3378 ata_default_registers(dev);
3379 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
3380 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
3381 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3382 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
3383 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3384 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
3385 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3386 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
3387 ch->flags |= ATA_NO_SLAVE;
3389 ch->flags |= ATA_USE_16BIT;
3391 ata_generic_hw(dev);
3392 if (ctlr->chip->cfg2 & PRSX4X) {
3393 ch->hw.command = ata_promise_sx4_command;
3396 ch->hw.command = ata_promise_mio_command;
3397 ch->hw.status = ata_promise_mio_status;
3403 ata_promise_mio_intr(void *data)
3405 struct ata_pci_controller *ctlr = data;
3406 struct ata_channel *ch;
3410 switch (ctlr->chip->cfg2) {
3424 * since reading interrupt status register on early "mio" chips
3425 * clears the status bits we cannot read it for each channel later on
3426 * in the generic interrupt routine.
3427 * store the bits in an unused register in the chip so we can read
3428 * it from there safely to get around this "feature".
3430 vector = ATA_INL(ctlr->r_res2, 0x040);
3431 ATA_OUTL(ctlr->r_res2, 0x040, vector);
3432 ATA_OUTL(ctlr->r_res2, fake_reg, vector);
3434 for (unit = 0; unit < ctlr->channels; unit++) {
3435 if ((ch = ctlr->interrupt[unit].argument))
3436 ctlr->interrupt[unit].function(ch);
3439 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3443 ata_promise_mio_status(device_t dev)
3445 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3446 struct ata_channel *ch = device_get_softc(dev);
3447 struct ata_connect_task *tp;
3448 u_int32_t fake_reg, stat_reg, vector, status;
3450 switch (ctlr->chip->cfg2) {
3465 /* read and acknowledge interrupt */
3466 vector = ATA_INL(ctlr->r_res2, fake_reg);
3468 /* read and clear interface status */
3469 status = ATA_INL(ctlr->r_res2, stat_reg);
3470 ATA_OUTL(ctlr->r_res2, stat_reg, status & (0x00000011 << ch->unit));
3472 /* check for and handle disconnect events */
3473 if ((status & (0x00000001 << ch->unit)) &&
3474 (tp = (struct ata_connect_task *)
3475 malloc(sizeof(struct ata_connect_task),
3476 M_ATA, M_NOWAIT | M_ZERO))) {
3479 device_printf(ch->dev, "DISCONNECT requested\n");
3480 tp->action = ATA_C_DETACH;
3482 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3483 taskqueue_enqueue(taskqueue_thread, &tp->task);
3486 /* check for and handle connect events */
3487 if ((status & (0x00000010 << ch->unit)) &&
3488 (tp = (struct ata_connect_task *)
3489 malloc(sizeof(struct ata_connect_task),
3490 M_ATA, M_NOWAIT | M_ZERO))) {
3493 device_printf(ch->dev, "CONNECT requested\n");
3494 tp->action = ATA_C_ATTACH;
3496 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3497 taskqueue_enqueue(taskqueue_thread, &tp->task);
3500 /* do we have any device action ? */
3501 return (vector & (1 << (ch->unit + 1)));
3505 ata_promise_mio_command(struct ata_request *request)
3507 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
3508 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
3509 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
3511 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
3513 /* XXX SOS add ATAPI commands support later */
3514 switch (request->u.ata.command) {
3516 return ata_generic_command(request);
3519 case ATA_READ_DMA48:
3520 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
3524 case ATA_WRITE_DMA48:
3525 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
3528 wordp[1] = htole32(ch->dma->sg_bus);
3530 ata_promise_apkt((u_int8_t*)wordp, request);
3532 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
3537 ata_promise_mio_reset(device_t dev)
3539 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3540 struct ata_channel *ch = device_get_softc(dev);
3541 struct ata_promise_sx4 *hpktp;
3543 switch (ctlr->chip->cfg2) {
3546 /* softreset channel ATA module */
3547 hpktp = device_get_ivars(ctlr->dev);
3548 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
3550 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
3551 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
3552 ~0x00003f9f) | (ch->unit + 1));
3554 /* softreset HOST module */ /* XXX SOS what about other outstandings */
3555 mtx_lock(&hpktp->mtx);
3556 ATA_OUTL(ctlr->r_res2, 0xc012c,
3557 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
3559 ATA_OUTL(ctlr->r_res2, 0xc012c,
3560 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
3562 mtx_unlock(&hpktp->mtx);
3563 ata_generic_reset(dev);
3569 if ((ctlr->chip->cfg2 == PRSATA) ||
3570 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3572 /* mask plug/unplug intr */
3573 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
3576 /* softreset channels ATA module */
3577 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
3579 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
3580 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
3581 ~0x00003f9f) | (ch->unit + 1));
3583 if ((ctlr->chip->cfg2 == PRSATA) ||
3584 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3586 ata_sata_phy_enable(ch);
3588 /* reset and enable plug/unplug intr */
3589 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
3592 ata_generic_reset(dev);
3597 if ((ctlr->chip->cfg2 == PRSATA2) ||
3598 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
3599 /* set portmultiplier port */
3600 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
3602 /* mask plug/unplug intr */
3603 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
3606 /* softreset channels ATA module */
3607 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
3609 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
3610 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
3611 ~0x00003f9f) | (ch->unit + 1));
3613 if ((ctlr->chip->cfg2 == PRSATA2) ||
3614 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
3616 /* set PHY mode to "improved" */
3617 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
3618 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
3619 ~0x00000003) | 0x00000001);
3621 ata_sata_phy_enable(ch);
3623 /* reset and enable plug/unplug intr */
3624 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
3626 /* set portmultiplier port */
3627 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
3630 ata_generic_reset(dev);
3637 ata_promise_mio_dmainit(device_t dev)
3639 /* note start and stop are not used here */
3644 ata_promise_mio_setmode(device_t dev, int mode)
3646 device_t gparent = GRANDPARENT(dev);
3647 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3648 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3650 if ( (ctlr->chip->cfg2 == PRSATA) ||
3651 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2)) ||
3652 (ctlr->chip->cfg2 == PRSATA2) ||
3653 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2)))
3654 ata_sata_setmode(dev, mode);
3656 ata_promise_setmode(dev, mode);
3660 ata_promise_sx4_intr(void *data)
3662 struct ata_pci_controller *ctlr = data;
3663 struct ata_channel *ch;
3664 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
3667 for (unit = 0; unit < ctlr->channels; unit++) {
3668 if (vector & (1 << (unit + 1)))
3669 if ((ch = ctlr->interrupt[unit].argument))
3670 ctlr->interrupt[unit].function(ch);
3671 if (vector & (1 << (unit + 5)))
3672 if ((ch = ctlr->interrupt[unit].argument))
3673 ata_promise_queue_hpkt(ctlr,
3674 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
3675 ATA_PDC_HPKT_OFFSET));
3676 if (vector & (1 << (unit + 9))) {
3677 ata_promise_next_hpkt(ctlr);
3678 if ((ch = ctlr->interrupt[unit].argument))
3679 ctlr->interrupt[unit].function(ch);
3681 if (vector & (1 << (unit + 13))) {
3682 ata_promise_next_hpkt(ctlr);
3683 if ((ch = ctlr->interrupt[unit].argument))
3684 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
3685 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
3686 ATA_PDC_APKT_OFFSET));
3692 ata_promise_sx4_command(struct ata_request *request)
3694 device_t gparent = GRANDPARENT(request->dev);
3695 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3696 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
3697 struct ata_dma_prdentry *prd = ch->dma->sg;
3698 caddr_t window = rman_get_virtual(ctlr->r_res1);
3700 int i, idx, length = 0;
3702 /* XXX SOS add ATAPI commands support later */
3703 switch (request->u.ata.command) {
3708 case ATA_ATA_IDENTIFY:
3712 case ATA_READ_MUL48:
3716 case ATA_WRITE_MUL48:
3717 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
3718 return ata_generic_command(request);
3720 case ATA_SETFEATURES:
3721 case ATA_FLUSHCACHE:
3722 case ATA_FLUSHCACHE48:
3725 wordp = (u_int32_t *)
3726 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
3727 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
3730 ata_promise_apkt((u_int8_t *)wordp, request);
3731 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
3732 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
3733 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
3734 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
3738 case ATA_READ_DMA48:
3740 case ATA_WRITE_DMA48:
3741 wordp = (u_int32_t *)
3742 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
3745 wordp[idx++] = prd[i].addr;
3746 wordp[idx++] = prd[i].count;
3747 length += (prd[i].count & ~ATA_DMA_EOT);
3748 } while (!(prd[i++].count & ATA_DMA_EOT));
3750 wordp = (u_int32_t *)
3751 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
3752 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
3753 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
3755 wordp = (u_int32_t *)
3756 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
3757 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
3758 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
3760 wordp = (u_int32_t *)
3761 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
3762 if (request->flags & ATA_R_READ)
3763 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
3764 if (request->flags & ATA_R_WRITE)
3765 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
3766 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
3767 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
3770 wordp = (u_int32_t *)
3771 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
3772 if (request->flags & ATA_R_READ)
3773 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
3774 if (request->flags & ATA_R_WRITE)
3775 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
3776 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
3778 ata_promise_apkt((u_int8_t *)wordp, request);
3779 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
3781 if (request->flags & ATA_R_READ) {
3782 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
3783 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
3784 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
3785 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
3787 if (request->flags & ATA_R_WRITE) {
3788 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
3789 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
3790 ata_promise_queue_hpkt(ctlr,
3791 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
3798 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
3800 struct ata_device *atadev = device_get_softc(request->dev);
3803 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
3804 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
3805 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
3806 bytep[i++] = ATA_A_4BIT;
3808 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3809 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
3810 bytep[i++] = request->u.ata.feature >> 8;
3811 bytep[i++] = request->u.ata.feature;
3812 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
3813 bytep[i++] = request->u.ata.count >> 8;
3814 bytep[i++] = request->u.ata.count;
3815 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
3816 bytep[i++] = request->u.ata.lba >> 24;
3817 bytep[i++] = request->u.ata.lba;
3818 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
3819 bytep[i++] = request->u.ata.lba >> 32;
3820 bytep[i++] = request->u.ata.lba >> 8;
3821 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
3822 bytep[i++] = request->u.ata.lba >> 40;
3823 bytep[i++] = request->u.ata.lba >> 16;
3824 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
3825 bytep[i++] = ATA_D_LBA | atadev->unit;
3828 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
3829 bytep[i++] = request->u.ata.feature;
3830 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
3831 bytep[i++] = request->u.ata.count;
3832 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
3833 bytep[i++] = request->u.ata.lba;
3834 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
3835 bytep[i++] = request->u.ata.lba >> 8;
3836 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
3837 bytep[i++] = request->u.ata.lba >> 16;
3838 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
3839 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
3840 ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
3842 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
3843 bytep[i++] = request->u.ata.command;
3848 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
3850 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
3852 mtx_lock(&hpktp->mtx);
3854 struct host_packet *hp =
3855 malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
3857 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
3861 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
3863 mtx_unlock(&hpktp->mtx);
3867 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
3869 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
3870 struct host_packet *hp;
3872 mtx_lock(&hpktp->mtx);
3873 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
3874 TAILQ_REMOVE(&hpktp->queue, hp, chain);
3875 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
3880 mtx_unlock(&hpktp->mtx);
3885 * ServerWorks chipset support functions
3888 ata_serverworks_ident(device_t dev)
3890 struct ata_pci_controller *ctlr = device_get_softc(dev);
3891 struct ata_chip_id *idx;
3892 static struct ata_chip_id ids[] =
3893 {{ ATA_ROSB4, 0x00, SWKS33, 0x00, ATA_UDMA2, "ROSB4" },
3894 { ATA_CSB5, 0x92, SWKS100, 0x00, ATA_UDMA5, "CSB5" },
3895 { ATA_CSB5, 0x00, SWKS66, 0x00, ATA_UDMA4, "CSB5" },
3896 { ATA_CSB6, 0x00, SWKS100, 0x00, ATA_UDMA5, "CSB6" },
3897 { ATA_CSB6_1, 0x00, SWKS66, 0x00, ATA_UDMA4, "CSB6" },
3898 { 0, 0, 0, 0, 0, 0}};
3901 if (!(idx = ata_match_chip(dev, ids)))
3904 sprintf(buffer, "ServerWorks %s %s controller",
3905 idx->text, ata_mode2str(idx->max_dma));
3906 device_set_desc_copy(dev, buffer);
3908 ctlr->chipinit = ata_serverworks_chipinit;
3913 ata_serverworks_chipinit(device_t dev)
3915 struct ata_pci_controller *ctlr = device_get_softc(dev);
3917 if (ata_setup_interrupt(dev))
3920 if (ctlr->chip->cfg1 == SWKS33) {
3924 /* locate the ISA part in the southbridge and enable UDMA33 */
3925 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
3926 for (i = 0; i < nchildren; i++) {
3927 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
3928 pci_write_config(children[i], 0x64,
3929 (pci_read_config(children[i], 0x64, 4) &
3930 ~0x00002000) | 0x00004000, 4);
3934 free(children, M_TEMP);
3938 pci_write_config(dev, 0x5a,
3939 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
3940 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
3942 ctlr->setmode = ata_serverworks_setmode;
3947 ata_serverworks_setmode(device_t dev, int mode)
3949 device_t gparent = GRANDPARENT(dev);
3950 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3951 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3952 struct ata_device *atadev = device_get_softc(dev);
3953 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3954 int offset = (devno ^ 0x01) << 3;
3956 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
3957 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
3958 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
3960 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3962 mode = ata_check_80pin(dev, mode);
3964 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3967 device_printf(dev, "%ssetting %s on %s chip\n",
3968 (error) ? "FAILURE " : "",
3969 ata_mode2str(mode), ctlr->chip->text);
3971 if (mode >= ATA_UDMA0) {
3972 pci_write_config(gparent, 0x56,
3973 (pci_read_config(gparent, 0x56, 2) &
3974 ~(0xf << (devno << 2))) |
3975 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
3976 pci_write_config(gparent, 0x54,
3977 pci_read_config(gparent, 0x54, 1) |
3978 (0x01 << devno), 1);
3979 pci_write_config(gparent, 0x44,
3980 (pci_read_config(gparent, 0x44, 4) &
3981 ~(0xff << offset)) |
3982 (dmatimings[2] << offset), 4);
3984 else if (mode >= ATA_WDMA0) {
3985 pci_write_config(gparent, 0x54,
3986 pci_read_config(gparent, 0x54, 1) &
3987 ~(0x01 << devno), 1);
3988 pci_write_config(gparent, 0x44,
3989 (pci_read_config(gparent, 0x44, 4) &
3990 ~(0xff << offset)) |
3991 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
3994 pci_write_config(gparent, 0x54,
3995 pci_read_config(gparent, 0x54, 1) &
3996 ~(0x01 << devno), 1);
3998 pci_write_config(gparent, 0x40,
3999 (pci_read_config(gparent, 0x40, 4) &
4000 ~(0xff << offset)) |
4001 (piotimings[ata_mode2idx(mode)] << offset), 4);
4002 atadev->mode = mode;
4008 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
4011 ata_sii_ident(device_t dev)
4013 struct ata_pci_controller *ctlr = device_get_softc(dev);
4014 struct ata_chip_id *idx;
4015 static struct ata_chip_id ids[] =
4016 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
4017 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
4018 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
4019 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
4020 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
4021 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
4022 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
4023 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
4024 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
4025 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
4026 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
4027 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
4028 { 0, 0, 0, 0, 0, 0}};
4031 if (!(idx = ata_match_chip(dev, ids)))
4034 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
4035 device_set_desc_copy(dev, buffer);
4037 ctlr->chipinit = ata_sii_chipinit;
4042 ata_sii_chipinit(device_t dev)
4044 struct ata_pci_controller *ctlr = device_get_softc(dev);
4046 if (ata_setup_interrupt(dev))
4049 if (ctlr->chip->cfg1 == SIIMEMIO) {
4050 ctlr->r_type2 = SYS_RES_MEMORY;
4051 ctlr->r_rid2 = PCIR_BAR(5);
4052 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4053 &ctlr->r_rid2, RF_ACTIVE)))
4056 if (ctlr->chip->cfg2 & SIISETCLK) {
4057 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
4058 pci_write_config(dev, 0x8a,
4059 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
4060 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
4061 device_printf(dev, "%s could not set ATA133 clock\n",
4065 /* if we have 4 channels enable the second set */
4066 if (ctlr->chip->cfg2 & SII4CH) {
4067 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
4071 /* enable PCI interrupt as BIOS might not */
4072 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
4074 /* dont block interrupts from any channel */
4075 pci_write_config(dev, 0x48,
4076 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
4078 ctlr->allocate = ata_sii_allocate;
4079 if (ctlr->chip->max_dma >= ATA_SA150) {
4080 ctlr->reset = ata_sii_reset;
4081 ctlr->setmode = ata_sata_setmode;
4084 ctlr->setmode = ata_sii_setmode;
4087 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
4088 device_printf(dev, "HW has secondary channel disabled\n");
4092 /* enable interrupt as BIOS might not */
4093 pci_write_config(dev, 0x71, 0x01, 1);
4095 ctlr->allocate = ata_cmd_allocate;
4096 ctlr->setmode = ata_cmd_setmode;
4102 ata_cmd_allocate(device_t dev)
4104 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4105 struct ata_channel *ch = device_get_softc(dev);
4107 /* setup the usual register normal pci style */
4108 if (ata_pci_allocate(dev))
4111 if (ctlr->chip->cfg2 & SIIINTR)
4112 ch->hw.status = ata_cmd_status;
4118 ata_cmd_status(device_t dev)
4120 struct ata_channel *ch = device_get_softc(dev);
4123 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
4124 (ch->unit ? 0x08 : 0x04))) {
4125 pci_write_config(device_get_parent(ch->dev), 0x71,
4126 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
4127 return ata_pci_status(dev);
4133 ata_cmd_setmode(device_t dev, int mode)
4135 device_t gparent = GRANDPARENT(dev);
4136 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4137 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4138 struct ata_device *atadev = device_get_softc(dev);
4139 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4142 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4144 mode = ata_check_80pin(dev, mode);
4146 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4149 device_printf(dev, "%ssetting %s on %s chip\n",
4150 (error) ? "FAILURE " : "",
4151 ata_mode2str(mode), ctlr->chip->text);
4153 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
4154 int ureg = ch->unit ? 0x7b : 0x73;
4156 if (mode >= ATA_UDMA0) {
4157 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
4158 { 0x11, 0x42 }, { 0x25, 0x8a },
4159 { 0x15, 0x4a }, { 0x05, 0x0a } };
4161 u_int8_t umode = pci_read_config(gparent, ureg, 1);
4163 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
4164 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
4165 pci_write_config(gparent, ureg, umode, 1);
4167 else if (mode >= ATA_WDMA0) {
4168 int dmatimings[] = { 0x87, 0x32, 0x3f };
4170 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
4171 pci_write_config(gparent, ureg,
4172 pci_read_config(gparent, ureg, 1) &
4173 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4176 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
4177 pci_write_config(gparent, treg,
4178 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
4179 pci_write_config(gparent, ureg,
4180 pci_read_config(gparent, ureg, 1) &
4181 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4183 atadev->mode = mode;
4188 ata_sii_allocate(device_t dev)
4190 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4191 struct ata_channel *ch = device_get_softc(dev);
4192 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
4195 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
4196 ch->r_io[i].res = ctlr->r_res2;
4197 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
4199 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
4200 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
4201 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
4202 ata_default_registers(dev);
4203 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
4204 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
4205 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
4206 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
4207 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
4208 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
4209 ch->r_io[ATA_BMDEVSPEC_0].res = ctlr->r_res2;
4210 ch->r_io[ATA_BMDEVSPEC_0].offset = 0xa1 + (unit01 << 6) + (unit10 << 8);
4212 if (ctlr->chip->max_dma >= ATA_SA150) {
4213 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4214 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
4215 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4216 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
4217 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4218 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
4219 ch->flags |= ATA_NO_SLAVE;
4221 /* enable PHY state change interrupt */
4222 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
4225 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
4226 /* work around errata in early chips */
4227 ch->dma->boundary = 16 * DEV_BSIZE;
4228 ch->dma->segsize = 15 * DEV_BSIZE;
4232 ch->hw.status = ata_sii_status;
4237 ata_sii_status(device_t dev)
4239 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4240 struct ata_channel *ch = device_get_softc(dev);
4242 /* check for PHY related interrupts on SATA capable HW */
4243 if (ctlr->chip->max_dma >= ATA_SA150) {
4244 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
4245 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
4246 struct ata_connect_task *tp;
4249 /* clear error bits/interrupt */
4250 ATA_IDX_OUTL(ch, ATA_SERROR, error);
4252 /* if we have a connection event deal with it */
4253 if ((error & ATA_SE_PHY_CHANGED) &&
4254 (tp = (struct ata_connect_task *)
4255 malloc(sizeof(struct ata_connect_task),
4256 M_ATA, M_NOWAIT | M_ZERO))) {
4258 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
4260 device_printf(ch->dev, "CONNECT requested\n");
4261 tp->action = ATA_C_ATTACH;
4265 device_printf(ch->dev, "DISCONNECT requested\n");
4266 tp->action = ATA_C_DETACH;
4269 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
4270 taskqueue_enqueue(taskqueue_thread, &tp->task);
4275 /* any drive action to take care of ? */
4276 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_0) & 0x08)
4277 return ata_pci_status(dev);
4283 ata_sii_reset(device_t dev)
4285 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4286 struct ata_channel *ch = device_get_softc(dev);
4287 int offset = ((ch->unit & 1) << 7) + ((ch->unit & 2) << 8);
4289 /* disable PHY state change interrupt */
4290 ATA_OUTL(ctlr->r_res2, 0x148 + offset, ~(1 << 16));
4292 /* reset controller part for this channel */
4293 ATA_OUTL(ctlr->r_res2, 0x48,
4294 ATA_INL(ctlr->r_res2, 0x48) | (0xc0 >> ch->unit));
4296 ATA_OUTL(ctlr->r_res2, 0x48,
4297 ATA_INL(ctlr->r_res2, 0x48) & ~(0xc0 >> ch->unit));
4299 ata_sata_phy_enable(ch);
4301 /* enable PHY state change interrupt */
4302 ATA_OUTL(ctlr->r_res2, 0x148 + offset, (1 << 16));
4306 ata_sii_setmode(device_t dev, int mode)
4308 device_t gparent = GRANDPARENT(dev);
4309 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4310 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4311 struct ata_device *atadev = device_get_softc(dev);
4312 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
4313 int mreg = ch->unit ? 0x84 : 0x80;
4314 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
4315 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
4318 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4320 if (ctlr->chip->cfg2 & SIISETCLK) {
4321 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
4322 (ch->unit ? 0x02 : 0x01))) {
4323 ata_print_cable(dev, "controller");
4328 mode = ata_check_80pin(dev, mode);
4330 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4333 device_printf(dev, "%ssetting %s on %s chip\n",
4334 (error) ? "FAILURE " : "",
4335 ata_mode2str(mode), ctlr->chip->text);
4339 if (mode >= ATA_UDMA0) {
4340 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
4341 u_int8_t ureg = 0xac + rego;
4343 pci_write_config(gparent, mreg,
4344 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
4345 pci_write_config(gparent, ureg,
4346 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
4347 udmatimings[mode & ATA_MODE_MASK], 1);
4350 else if (mode >= ATA_WDMA0) {
4351 u_int8_t dreg = 0xa8 + rego;
4352 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
4354 pci_write_config(gparent, mreg,
4355 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
4356 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
4360 u_int8_t preg = 0xa4 + rego;
4361 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
4363 pci_write_config(gparent, mreg,
4364 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
4365 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
4367 atadev->mode = mode;
4372 * Silicon Integrated Systems Corp. (SiS) chipset support functions
4375 ata_sis_ident(device_t dev)
4377 struct ata_pci_controller *ctlr = device_get_softc(dev);
4378 struct ata_chip_id *idx;
4379 static struct ata_chip_id ids[] =
4380 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "182" }, /* south */
4381 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "181" }, /* south */
4382 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "180" }, /* south */
4383 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "965" }, /* south */
4384 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "964" }, /* south */
4385 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "963" }, /* south */
4386 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "962" }, /* south */
4388 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "745" }, /* 1chip */
4389 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "735" }, /* 1chip */
4390 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "733" }, /* 1chip */
4391 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "730" }, /* 1chip */
4393 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "635" }, /* 1chip */
4394 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "633" }, /* unknown */
4395 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "630S"}, /* 1chip */
4396 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "630" }, /* 1chip */
4397 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "620" }, /* 1chip */
4399 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "550" },
4400 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "540" },
4401 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "530" },
4403 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "5513" },
4404 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "5513" },
4405 { 0, 0, 0, 0, 0, 0 }};
4409 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
4412 if (idx->cfg2 && !found) {
4413 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
4415 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
4416 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
4418 idx->cfg1 = SIS133NEW;
4419 idx->max_dma = ATA_UDMA6;
4420 sprintf(buffer, "SiS 962/963 %s controller",
4421 ata_mode2str(idx->max_dma));
4423 pci_write_config(dev, 0x57, reg57, 1);
4425 if (idx->cfg2 && !found) {
4426 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
4428 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
4429 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
4430 struct ata_chip_id id[] =
4431 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
4434 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
4435 idx->cfg1 = SIS133OLD;
4436 idx->max_dma = ATA_UDMA6;
4439 idx->cfg1 = SIS100NEW;
4440 idx->max_dma = ATA_UDMA5;
4442 sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
4444 pci_write_config(dev, 0x4a, reg4a, 1);
4447 sprintf(buffer,"SiS %s %s controller",
4448 idx->text, ata_mode2str(idx->max_dma));
4450 device_set_desc_copy(dev, buffer);
4452 ctlr->chipinit = ata_sis_chipinit;
4457 ata_sis_chipinit(device_t dev)
4459 struct ata_pci_controller *ctlr = device_get_softc(dev);
4461 if (ata_setup_interrupt(dev))
4464 switch (ctlr->chip->cfg1) {
4469 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
4473 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
4476 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
4477 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
4480 ctlr->r_type2 = SYS_RES_IOPORT;
4481 ctlr->r_rid2 = PCIR_BAR(5);
4482 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4483 &ctlr->r_rid2, RF_ACTIVE))) {
4484 ctlr->allocate = ata_sis_allocate;
4485 ctlr->reset = ata_sis_reset;
4487 /* enable PCI interrupt */
4488 pci_write_config(dev, PCIR_COMMAND,
4489 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
4491 ctlr->setmode = ata_sata_setmode;
4496 ctlr->setmode = ata_sis_setmode;
4501 ata_sis_allocate(device_t dev)
4503 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4504 struct ata_channel *ch = device_get_softc(dev);
4505 int offset = ch->unit << ((ctlr->chip->chipid == ATA_SIS182) ? 5 : 6);
4507 /* setup the usual register normal pci style */
4508 if (ata_pci_allocate(dev))
4511 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4512 ch->r_io[ATA_SSTATUS].offset = 0x00 + offset;
4513 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4514 ch->r_io[ATA_SERROR].offset = 0x04 + offset;
4515 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4516 ch->r_io[ATA_SCONTROL].offset = 0x08 + offset;
4517 ch->flags |= ATA_NO_SLAVE;
4519 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
4520 /* XXX SOS unknown how to enable PHY state change interrupt */
4525 ata_sis_reset(device_t dev)
4527 struct ata_channel *ch = device_get_softc(dev);
4529 ata_sata_phy_enable(ch);
4534 ata_sis_setmode(device_t dev, int mode)
4536 device_t gparent = GRANDPARENT(dev);
4537 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4538 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4539 struct ata_device *atadev = device_get_softc(dev);
4540 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4543 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4545 if (ctlr->chip->cfg1 == SIS133NEW) {
4546 if (mode > ATA_UDMA2 &&
4547 pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
4548 ata_print_cable(dev, "controller");
4553 if (mode > ATA_UDMA2 &&
4554 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
4555 ata_print_cable(dev, "controller");
4560 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4563 device_printf(dev, "%ssetting %s on %s chip\n",
4564 (error) ? "FAILURE " : "",
4565 ata_mode2str(mode), ctlr->chip->text);
4567 switch (ctlr->chip->cfg1) {
4569 u_int32_t timings[] =
4570 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
4571 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
4572 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
4575 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
4576 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
4580 u_int16_t timings[] =
4581 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
4582 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
4584 u_int16_t reg = 0x40 + (devno << 1);
4586 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
4590 u_int16_t timings[] =
4591 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
4592 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
4593 u_int16_t reg = 0x40 + (devno << 1);
4595 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
4601 u_int16_t timings[] =
4602 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
4603 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
4604 u_int16_t reg = 0x40 + (devno << 1);
4606 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
4610 atadev->mode = mode;
4615 /* VIA Technologies Inc. chipset support functions */
4617 ata_via_ident(device_t dev)
4619 struct ata_pci_controller *ctlr = device_get_softc(dev);
4620 struct ata_chip_id *idx;
4621 static struct ata_chip_id ids[] =
4622 {{ ATA_VIA82C586, 0x02, VIA33, 0x00, ATA_UDMA2, "82C586B" },
4623 { ATA_VIA82C586, 0x00, VIA33, 0x00, ATA_WDMA2, "82C586" },
4624 { ATA_VIA82C596, 0x12, VIA66, VIACLK, ATA_UDMA4, "82C596B" },
4625 { ATA_VIA82C596, 0x00, VIA33, 0x00, ATA_UDMA2, "82C596" },
4626 { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "82C686B"},
4627 { ATA_VIA82C686, 0x10, VIA66, VIACLK, ATA_UDMA4, "82C686A" },
4628 { ATA_VIA82C686, 0x00, VIA33, 0x00, ATA_UDMA2, "82C686" },
4629 { ATA_VIA8231, 0x00, VIA100, VIABUG, ATA_UDMA5, "8231" },
4630 { ATA_VIA8233, 0x00, VIA100, 0x00, ATA_UDMA5, "8233" },
4631 { ATA_VIA8233C, 0x00, VIA100, 0x00, ATA_UDMA5, "8233C" },
4632 { ATA_VIA8233A, 0x00, VIA133, 0x00, ATA_UDMA6, "8233A" },
4633 { ATA_VIA8235, 0x00, VIA133, 0x00, ATA_UDMA6, "8235" },
4634 { ATA_VIA8237, 0x00, VIA133, 0x00, ATA_UDMA6, "8237" },
4635 { ATA_VIA8251, 0x00, VIA133, 0x00, ATA_UDMA6, "8251" },
4636 { 0, 0, 0, 0, 0, 0 }};
4637 static struct ata_chip_id new_ids[] =
4638 {{ ATA_VIA6410, 0x00, 0, 0x00, ATA_UDMA6, "6410" },
4639 { ATA_VIA6420, 0x00, 7, 0x00, ATA_SA150, "6420" },
4640 { ATA_VIA6421, 0x00, 6, VIABAR, ATA_SA150, "6421" },
4641 { ATA_VIA8251, 0x00, 0, VIAAHCI, ATA_SA300, "8251" },
4642 { 0, 0, 0, 0, 0, 0 }};
4645 if (pci_get_devid(dev) == ATA_VIA82C571) {
4646 if (!(idx = ata_find_chip(dev, ids, -99)))
4650 if (!(idx = ata_match_chip(dev, new_ids)))
4654 sprintf(buffer, "VIA %s %s controller",
4655 idx->text, ata_mode2str(idx->max_dma));
4656 device_set_desc_copy(dev, buffer);
4658 ctlr->chipinit = ata_via_chipinit;
4663 ata_via_chipinit(device_t dev)
4665 struct ata_pci_controller *ctlr = device_get_softc(dev);
4667 if (ata_setup_interrupt(dev))
4670 if (ctlr->chip->max_dma >= ATA_SA150) {
4671 if (ctlr->chip->cfg2 == VIAAHCI) {
4672 ctlr->r_type2 = SYS_RES_MEMORY;
4673 ctlr->r_rid2 = PCIR_BAR(5);
4674 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4677 return ata_ahci_chipinit(dev);
4680 ctlr->r_type2 = SYS_RES_IOPORT;
4681 ctlr->r_rid2 = PCIR_BAR(5);
4682 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4683 &ctlr->r_rid2, RF_ACTIVE))) {
4684 ctlr->allocate = ata_via_allocate;
4685 ctlr->reset = ata_via_reset;
4687 /* enable PCI interrupt */
4688 pci_write_config(dev, PCIR_COMMAND,
4689 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
4691 ctlr->setmode = ata_sata_setmode;
4695 /* prepare for ATA-66 on the 82C686a and 82C596b */
4696 if (ctlr->chip->cfg2 & VIACLK)
4697 pci_write_config(dev, 0x50, 0x030b030b, 4);
4699 /* the southbridge might need the data corruption fix */
4700 if (ctlr->chip->cfg2 & VIABUG)
4701 ata_via_southbridge_fixup(dev);
4703 /* set fifo configuration half'n'half */
4704 pci_write_config(dev, 0x43,
4705 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
4707 /* set status register read retry */
4708 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
4710 /* set DMA read & end-of-sector fifo flush */
4711 pci_write_config(dev, 0x46,
4712 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
4714 /* set sector size */
4715 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
4716 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
4718 ctlr->setmode = ata_via_family_setmode;
4723 ata_via_allocate(device_t dev)
4725 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4726 struct ata_channel *ch = device_get_softc(dev);
4728 /* newer SATA chips has resources in one BAR for each channel */
4729 if (ctlr->chip->cfg2 & VIABAR) {
4730 struct resource *r_io;
4733 rid = PCIR_BAR(ch->unit);
4734 if (!(r_io = bus_alloc_resource_any(device_get_parent(dev),
4739 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
4740 ch->r_io[i].res = r_io;
4741 ch->r_io[i].offset = i;
4743 ch->r_io[ATA_CONTROL].res = r_io;
4744 ch->r_io[ATA_CONTROL].offset = 2 + ATA_IOSIZE;
4745 ch->r_io[ATA_IDX_ADDR].res = r_io;
4746 ata_default_registers(dev);
4747 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
4748 ch->r_io[i].res = ctlr->r_res1;
4749 ch->r_io[i].offset = i - ATA_BMCMD_PORT;
4754 /* setup the usual register normal pci style */
4755 if (ata_pci_allocate(dev))
4759 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4760 ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
4761 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4762 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
4763 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4764 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
4765 ch->flags |= ATA_NO_SLAVE;
4767 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
4768 /* XXX SOS unknown how to enable PHY state change interrupt */
4773 ata_via_reset(device_t dev)
4775 struct ata_channel *ch = device_get_softc(dev);
4777 ata_sata_phy_enable(ch);
4781 ata_via_southbridge_fixup(device_t dev)
4786 if (device_get_children(device_get_parent(dev), &children, &nchildren))
4789 for (i = 0; i < nchildren; i++) {
4790 if (pci_get_devid(children[i]) == ATA_VIA8363 ||
4791 pci_get_devid(children[i]) == ATA_VIA8371 ||
4792 pci_get_devid(children[i]) == ATA_VIA8662 ||
4793 pci_get_devid(children[i]) == ATA_VIA8361) {
4794 u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
4796 if ((reg76 & 0xf0) != 0xd0) {
4798 "Correcting VIA config for southbridge data corruption bug\n");
4799 pci_write_config(children[i], 0x75, 0x80, 1);
4800 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
4805 free(children, M_TEMP);
4809 /* common code for VIA, AMD & nVidia */
4811 ata_via_family_setmode(device_t dev, int mode)
4813 device_t gparent = GRANDPARENT(dev);
4814 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4815 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4816 struct ata_device *atadev = device_get_softc(dev);
4817 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
4818 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
4820 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* VIA ATA33 */
4821 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* VIA ATA66 */
4822 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* VIA ATA100 */
4823 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
4824 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }}; /* AMD/nVIDIA */
4825 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4826 int reg = 0x53 - devno;
4829 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4831 if (ctlr->chip->cfg2 & AMDCABLE) {
4832 if (mode > ATA_UDMA2 &&
4833 !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
4834 ata_print_cable(dev, "controller");
4839 mode = ata_check_80pin(dev, mode);
4841 if (ctlr->chip->cfg2 & NVIDIA)
4844 if (ctlr->chip->cfg1 != VIA133)
4845 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
4847 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4850 device_printf(dev, "%ssetting %s on %s chip\n",
4851 (error) ? "FAILURE " : "", ata_mode2str(mode),
4854 if (mode >= ATA_UDMA0)
4855 pci_write_config(gparent, reg,
4856 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
4858 pci_write_config(gparent, reg, 0x8b, 1);
4859 atadev->mode = mode;
4864 /* misc functions */
4865 static struct ata_chip_id *
4866 ata_match_chip(device_t dev, struct ata_chip_id *index)
4868 while (index->chipid != 0) {
4869 if (pci_get_devid(dev) == index->chipid &&
4870 pci_get_revid(dev) >= index->chiprev)
4877 static struct ata_chip_id *
4878 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
4883 if (device_get_children(device_get_parent(dev), &children, &nchildren))
4886 while (index->chipid != 0) {
4887 for (i = 0; i < nchildren; i++) {
4888 if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
4889 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
4890 pci_get_devid(children[i]) == index->chipid &&
4891 pci_get_revid(children[i]) >= index->chiprev) {
4892 free(children, M_TEMP);
4898 free(children, M_TEMP);
4903 ata_setup_interrupt(device_t dev)
4905 struct ata_pci_controller *ctlr = device_get_softc(dev);
4906 int rid = ATA_IRQ_RID;
4908 if (!ata_legacy(dev)) {
4909 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
4910 RF_SHAREABLE | RF_ACTIVE))) {
4911 device_printf(dev, "unable to map interrupt\n");
4914 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
4915 ata_generic_intr, ctlr, &ctlr->handle))) {
4916 device_printf(dev, "unable to setup interrupt\n");
4923 struct ata_serialize {
4924 struct mtx locked_mtx;
4930 ata_serialize(device_t dev, int flags)
4932 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4933 struct ata_channel *ch = device_get_softc(dev);
4934 struct ata_serialize *serial;
4935 static int inited = 0;
4939 serial = malloc(sizeof(struct ata_serialize),
4940 M_TEMP, M_NOWAIT | M_ZERO);
4941 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
4942 serial->locked_ch = -1;
4943 serial->restart_ch = -1;
4944 device_set_ivars(ctlr->dev, serial);
4948 serial = device_get_ivars(ctlr->dev);
4950 mtx_lock(&serial->locked_mtx);
4953 if (serial->locked_ch == -1)
4954 serial->locked_ch = ch->unit;
4955 if (serial->locked_ch != ch->unit)
4956 serial->restart_ch = ch->unit;
4960 if (serial->locked_ch == ch->unit) {
4961 serial->locked_ch = -1;
4962 if (serial->restart_ch != -1) {
4963 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
4964 serial->restart_ch = -1;
4965 mtx_unlock(&serial->locked_mtx);
4976 res = serial->locked_ch;
4977 mtx_unlock(&serial->locked_mtx);
4982 ata_print_cable(device_t dev, u_int8_t *who)
4985 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
4989 ata_atapi(device_t dev)
4991 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4992 struct ata_device *atadev = device_get_softc(dev);
4994 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
4995 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
4999 ata_check_80pin(device_t dev, int mode)
5001 struct ata_device *atadev = device_get_softc(dev);
5003 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
5004 ata_print_cable(dev, "device");
5011 ata_mode2idx(int mode)
5013 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
5014 return (mode & ATA_MODE_MASK) + 8;
5015 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
5016 return (mode & ATA_MODE_MASK) + 5;
5017 return (mode & ATA_MODE_MASK) - ATA_PIO0;