2 * Copyright (c) 1998 - 2005 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.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
38 #include <sys/endian.h>
39 #include <sys/malloc.h>
41 #include <sys/mutex.h>
43 #include <sys/taskqueue.h>
45 #include <machine/stdarg.h>
46 #include <machine/resource.h>
47 #include <machine/bus.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcireg.h>
51 #include <dev/ata/ata-all.h>
52 #include <dev/ata/ata-pci.h>
55 /* local prototypes */
56 static int ata_generic_chipinit(device_t);
57 static void ata_generic_intr(void *);
58 static void ata_generic_setmode(device_t, int);
59 static int ata_ahci_allocate(device_t dev);
60 static int ata_ahci_begin_transaction(struct ata_request *);
61 static int ata_ahci_end_transaction(struct ata_request *);
62 static void ata_ahci_intr(void *);
63 static void ata_ahci_reset(device_t);
64 static int ata_acard_chipinit(device_t);
65 static void ata_acard_intr(void *);
66 static void ata_acard_850_setmode(device_t, int);
67 static void ata_acard_86X_setmode(device_t, int);
68 static int ata_ali_chipinit(device_t);
69 static int ata_ali_allocate(device_t);
70 static void ata_ali_setmode(device_t, int);
71 static int ata_amd_chipinit(device_t);
72 static int ata_cyrix_chipinit(device_t);
73 static void ata_cyrix_setmode(device_t, int);
74 static int ata_cypress_chipinit(device_t);
75 static void ata_cypress_setmode(device_t, int);
76 static int ata_highpoint_chipinit(device_t);
77 static void ata_highpoint_intr(void *);
78 static void ata_highpoint_setmode(device_t, int);
79 static int ata_highpoint_check_80pin(device_t, int);
80 static int ata_intel_chipinit(device_t);
81 static void ata_intel_intr(void *);
82 static void ata_intel_reset(device_t);
83 static void ata_intel_old_setmode(device_t, int);
84 static void ata_intel_new_setmode(device_t, int);
85 static int ata_ite_chipinit(device_t);
86 static void ata_ite_setmode(device_t, int);
87 static int ata_national_chipinit(device_t);
88 static void ata_national_setmode(device_t, int);
89 static int ata_nvidia_chipinit(device_t);
90 static int ata_nvidia_allocate(device_t);
91 static void ata_nvidia_intr(void *);
92 static void ata_nvidia_reset(device_t);
93 static int ata_promise_chipinit(device_t);
94 static int ata_promise_mio_allocate(device_t);
95 static void ata_promise_mio_intr(void *);
96 static void ata_promise_sx4_intr(void *);
97 static void ata_promise_mio_dmainit(device_t);
98 static void ata_promise_mio_reset(device_t);
99 static int ata_promise_mio_command(struct ata_request *);
100 static int ata_promise_sx4_command(struct ata_request *);
101 static int ata_promise_apkt(u_int8_t *, struct ata_request *);
102 static void ata_promise_queue_hpkt(struct ata_pci_controller *, u_int32_t);
103 static void ata_promise_next_hpkt(struct ata_pci_controller *);
104 static void ata_promise_tx2_intr(void *);
105 static void ata_promise_old_intr(void *);
106 static void ata_promise_new_dmainit(device_t);
107 static void ata_promise_setmode(device_t, int);
108 static int ata_serverworks_chipinit(device_t);
109 static void ata_serverworks_setmode(device_t, int);
110 static int ata_sii_chipinit(device_t);
111 static int ata_sii_allocate(device_t);
112 static void ata_sii_intr(void *);
113 static void ata_sii_reset(device_t);
114 static void ata_cmd_intr(void *);
115 static void ata_cmd_old_intr(void *);
116 static void ata_sii_setmode(device_t, int);
117 static void ata_cmd_setmode(device_t, int);
118 static int ata_sis_chipinit(device_t);
119 static int ata_sis_allocate(device_t dev);
120 static void ata_sis_reset(device_t);
121 static void ata_sis_setmode(device_t, int);
122 static int ata_via_chipinit(device_t);
123 static int ata_via_allocate(device_t dev);
124 static void ata_via_reset(device_t);
125 static void ata_via_southbridge_fixup(device_t);
126 static void ata_via_family_setmode(device_t, int);
127 static void ata_print_cable(device_t, u_int8_t *);
128 static int ata_atapi(device_t);
129 static int ata_check_80pin(device_t, int);
130 static struct ata_chip_id *ata_find_chip(device_t, struct ata_chip_id *, int);
131 static int ata_setup_interrupt(device_t);
132 static int ata_serialize(device_t, int);
133 static int ata_mode2idx(int);
136 /* generic or unknown ATA chipset support functions */
138 ata_generic_ident(device_t dev)
140 struct ata_pci_controller *ctlr = device_get_softc(dev);
142 device_set_desc(dev, "GENERIC ATA controller");
143 ctlr->chipinit = ata_generic_chipinit;
148 ata_generic_chipinit(device_t dev)
150 struct ata_pci_controller *ctlr = device_get_softc(dev);
152 if (ata_setup_interrupt(dev))
154 ctlr->setmode = ata_generic_setmode;
159 ata_generic_intr(void *data)
161 struct ata_pci_controller *ctlr = data;
162 struct ata_channel *ch;
165 for (unit = 0; unit < ctlr->channels; unit++) {
166 if (!(ch = ctlr->interrupt[unit].argument))
168 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
169 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
171 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
172 ATA_BMSTAT_INTERRUPT)
174 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
177 ctlr->interrupt[unit].function(ch);
182 ata_generic_setmode(device_t dev, int mode)
184 struct ata_device *atadev = device_get_softc(dev);
186 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
187 mode = ata_check_80pin(dev, mode);
188 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
193 /* SATA support functions */
195 ata_sata_setmode(device_t dev, int mode)
197 struct ata_device *atadev = device_get_softc(dev);
200 * if we detect that the device isn't a real SATA device we limit
201 * the transfer mode to UDMA5/ATA100.
202 * this works around the problems some devices has with the
203 * Marvell 88SX8030 SATA->PATA converters and UDMA6/ATA133.
205 if (atadev->param.satacapabilities != 0x0000 &&
206 atadev->param.satacapabilities != 0xffff) {
207 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
208 ata_limit_mode(dev, mode, ATA_UDMA6)))
209 atadev->mode = ATA_SA150;
212 mode = ata_limit_mode(dev, mode, ATA_UDMA5);
213 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
219 ata_sata_connect(struct ata_channel *ch)
224 /* wait up to 1 second for "connect well" */
225 for (timeout = 0; timeout < 100 ; timeout++) {
226 status = ATA_IDX_INL(ch, ATA_SSTATUS);
227 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1 ||
228 (status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)
232 if (timeout >= 100) {
234 device_printf(ch->dev, "SATA connect status=%08x\n", status);
238 /* clear SATA error register */
239 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
241 /* find out what type device we got poll for spec'd 31 seconds */
242 /* XXX SOS 10 secs for now as I have little patience */
244 for (timeout = 0; timeout < 1000; timeout++) {
245 if (ATA_IDX_INB(ch, ATA_STATUS) & ATA_S_BUSY)
251 device_printf(ch->dev, "SATA connect ready time=%dms\n", timeout * 10);
252 if (timeout < 1000) {
253 if ((ATA_IDX_INB(ch, ATA_CYL_LSB) == ATAPI_MAGIC_LSB) &&
254 (ATA_IDX_INB(ch, ATA_CYL_MSB) == ATAPI_MAGIC_MSB))
255 ch->devices = ATA_ATAPI_MASTER;
257 ch->devices = ATA_ATA_MASTER;
260 device_printf(ch->dev, "sata_connect devices=0x%b\n",
261 ch->devices, "\20\3ATAPI_MASTER\1ATA_MASTER");
266 ata_sata_phy_enable(struct ata_channel *ch)
270 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE) {
271 ata_sata_connect(ch);
275 for (retry = 0; retry < 10; retry++) {
276 for (loop = 0; loop < 10; loop++) {
277 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_RESET);
279 if ((ATA_IDX_INL(ch, ATA_SCONTROL) &
280 ATA_SC_DET_MASK) == ATA_SC_DET_RESET)
284 for (loop = 0; loop < 10; loop++) {
285 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_IDLE);
287 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == 0) {
288 ata_sata_connect(ch);
296 ata_sata_phy_event(void *context, int dummy)
298 struct ata_connect_task *tp = (struct ata_connect_task *)context;
302 mtx_lock(&Giant); /* newbus suckage it needs Giant */
303 if (tp->action == ATA_C_ATTACH) {
304 struct ata_channel *ch = device_get_softc(tp->dev);
306 device_printf(tp->dev, "CONNECTED\n");
307 ata_sata_connect(ch);
308 ata_identify(tp->dev);
310 if (tp->action == ATA_C_DETACH) {
311 if (!device_get_children(tp->dev, &children, &nchildren)) {
312 for (i = 0; i < nchildren; i++)
314 device_delete_child(tp->dev, children[i]);
315 free(children, M_TEMP);
317 device_printf(tp->dev, "DISCONNECTED\n");
319 mtx_unlock(&Giant); /* suckage code dealt with, release Giant */
325 * AHCI v1.0 compliant SATA chipset support functions
327 struct ata_ahci_dma_prd {
330 u_int32_t dbc; /* 0 based */
331 #define ATA_AHCI_PRD_MASK 0x003fffff /* max 4MB */
332 #define ATA_AHCI_PRD_IPC (1<<31)
335 struct ata_ahci_cmd_tab {
338 u_int8_t reserved[32];
339 struct ata_ahci_dma_prd prd_tab[16];
342 struct ata_ahci_cmd_list {
344 u_int16_t prd_length; /* PRD entries */
346 u_int64_t cmd_table_phys; /* 128byte aligned */
351 ata_ahci_allocate(device_t dev)
353 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
354 struct ata_channel *ch = device_get_softc(dev);
355 int offset = (ch->unit << 7);
357 /* XXX SOS this is a hack to satisfy various legacy cruft */
358 ch->r_io[ATA_CYL_LSB].res = ctlr->r_res2;
359 ch->r_io[ATA_CYL_LSB].offset = ATA_AHCI_P_SIG + 1 + offset;
360 ch->r_io[ATA_CYL_LSB].res = ctlr->r_res2;
361 ch->r_io[ATA_CYL_MSB].offset = ATA_AHCI_P_SIG + 3 + offset;
362 ch->r_io[ATA_STATUS].res = ctlr->r_res2;
363 ch->r_io[ATA_STATUS].offset = ATA_AHCI_P_TFD + offset;
364 ch->r_io[ATA_ALTSTAT].res = ctlr->r_res2;
365 ch->r_io[ATA_ALTSTAT].offset = ATA_AHCI_P_TFD + offset;
367 /* set the SATA resources */
368 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
369 ch->r_io[ATA_SSTATUS].offset = ATA_AHCI_P_SSTS + offset;
370 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
371 ch->r_io[ATA_SERROR].offset = ATA_AHCI_P_SERR + offset;
372 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
373 ch->r_io[ATA_SCONTROL].offset = ATA_AHCI_P_SCTL + offset;
374 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
375 ch->r_io[ATA_SACTIVE].offset = ATA_AHCI_P_SACT + offset;
377 ch->hw.begin_transaction = ata_ahci_begin_transaction;
378 ch->hw.end_transaction = ata_ahci_end_transaction;
379 ch->hw.command = NULL; /* not used here */
381 /* setup the work areas */
382 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLB + offset,
383 ch->dma->work_bus + ATA_AHCI_CL_OFFSET);
384 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLBU + offset, 0x00000000);
386 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FB + offset,
387 ch->dma->work_bus + ATA_AHCI_FB_OFFSET);
388 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FBU + offset, 0x00000000);
390 /* enable wanted port interrupts */
391 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset,
392 (ATA_AHCI_P_IX_CPD | ATA_AHCI_P_IX_TFE | ATA_AHCI_P_IX_HBF |
393 ATA_AHCI_P_IX_HBD | ATA_AHCI_P_IX_IF | ATA_AHCI_P_IX_OF |
394 ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC | ATA_AHCI_P_IX_DP |
395 ATA_AHCI_P_IX_UF | ATA_AHCI_P_IX_SDB | ATA_AHCI_P_IX_DS |
396 ATA_AHCI_P_IX_PS | ATA_AHCI_P_IX_DHR));
398 /* start operations on this channel */
399 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
400 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
401 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
406 ata_ahci_setup_fis(u_int8_t *fis, struct ata_request *request)
408 struct ata_device *atadev = device_get_softc(request->dev);
411 /* XXX SOS add ATAPI commands support later */
412 fis[idx++] = 0x27; /* host to device */
413 fis[idx++] = 0x80; /* command FIS (note PM goes here) */
414 fis[idx++] = ata_modify_if_48bit(request);
415 fis[idx++] = request->u.ata.feature;
417 fis[idx++] = request->u.ata.lba;
418 fis[idx++] = request->u.ata.lba >> 8;
419 fis[idx++] = request->u.ata.lba >> 16;
420 fis[idx++] = ATA_D_LBA | atadev->unit;
422 fis[idx++] = request->u.ata.lba >> 24;
423 fis[idx++] = request->u.ata.lba >> 32;
424 fis[idx++] = request->u.ata.lba >> 40;
425 fis[idx++] = request->u.ata.feature >> 8;
427 fis[idx++] = request->u.ata.count;
428 fis[idx++] = request->u.ata.count >> 8;
430 fis[idx++] = ATA_A_4BIT;
439 /* must be called with ATA channel locked and state_mtx held */
441 ata_ahci_begin_transaction(struct ata_request *request)
443 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
444 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
445 struct ata_ahci_cmd_tab *ctp;
446 struct ata_ahci_cmd_list *clp;
447 int fis_size, entries;
450 /* get a piece of the workspace for this request */
451 ctp = (struct ata_ahci_cmd_tab *)
452 (ch->dma->work + ATA_AHCI_CT_OFFSET + (ATA_AHCI_CT_SIZE * tag));
454 /* setup the FIS for this request */ /* XXX SOS ATAPI missing still */
455 if (!(fis_size = ata_ahci_setup_fis(&ctp->cfis[0], request))) {
456 device_printf(request->dev, "setting up SATA FIS failed\n");
457 request->result = EIO;
458 return ATA_OP_FINISHED;
461 /* if request moves data setup and load SG list */
462 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
463 if (ch->dma->load(ch->dev, request->data, request->bytecount,
464 request->flags & ATA_R_READ,
465 ctp->prd_tab, &entries)) {
466 device_printf(request->dev, "setting up DMA failed\n");
467 request->result = EIO;
468 return ATA_OP_FINISHED;
472 /* setup the command list entry */
473 clp = (struct ata_ahci_cmd_list *)
474 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
476 clp->prd_length = entries;
477 clp->cmd_flags = (request->flags & ATA_R_WRITE ? (1<<6) : 0) |
478 (request->flags & ATA_R_ATAPI ? (1<<5) : 0) |
479 (fis_size / sizeof(u_int32_t));
481 clp->cmd_table_phys = htole64(ch->dma->work_bus + ATA_AHCI_CT_OFFSET +
482 (ATA_AHCI_CT_SIZE * tag));
484 /* clear eventual ACTIVE bit */
485 ATA_IDX_OUTL(ch, ATA_SACTIVE, ATA_IDX_INL(ch, ATA_SACTIVE) & (1 << tag));
487 /* issue the command */
488 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CI + (ch->unit << 7), (1 << tag));
490 /* start the timeout */
491 callout_reset(&request->callout, request->timeout * hz,
492 (timeout_t*)ata_timeout, request);
493 return ATA_OP_CONTINUES;
496 /* must be called with ATA channel locked and state_mtx held */
498 ata_ahci_end_transaction(struct ata_request *request)
500 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
501 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
502 struct ata_ahci_cmd_list *clp;
506 /* kill the timeout */
507 callout_stop(&request->callout);
510 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + (ch->unit << 7));
511 request->status = tf_data;
513 /* if error status get details */
514 if (request->status & ATA_S_ERROR)
515 request->error = tf_data >> 8;
517 /* record how much data we actually moved */
518 clp = (struct ata_ahci_cmd_list *)
519 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
520 request->donecount = clp->bytecount;
522 /* release SG list etc */
523 ch->dma->unload(ch->dev);
525 return ATA_OP_FINISHED;
529 ata_ahci_intr(void *data)
531 struct ata_pci_controller *ctlr = data;
532 struct ata_channel *ch;
533 u_int32_t port, status, error, issued;
537 port = ATA_INL(ctlr->r_res2, ATA_AHCI_IS);
539 for (unit = 0; unit < ctlr->channels; unit++) {
540 if (port & (1 << unit)) {
541 if ((ch = ctlr->interrupt[unit].argument)) {
542 struct ata_connect_task *tp;
543 int offset = (ch->unit << 7);
545 error = ATA_INL(ctlr->r_res2, ATA_AHCI_P_SERR + offset);
546 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_SERR + offset, error);
547 status = ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset);
548 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset, status);
549 issued = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CI + offset);
551 /* do we have cold connect surprise */
552 if (status & ATA_AHCI_P_IX_CPD) {
553 printf("ata_ahci_intr status=%08x error=%08x issued=%08x\n",
554 status, error, issued);
557 /* check for and handle connect events */
558 if ((status & ATA_AHCI_P_IX_PC) &&
559 (tp = (struct ata_connect_task *)
560 malloc(sizeof(struct ata_connect_task),
561 M_ATA, M_NOWAIT | M_ZERO))) {
563 device_printf(ch->dev, "CONNECT requested\n");
564 tp->action = ATA_C_ATTACH;
566 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
567 taskqueue_enqueue(taskqueue_thread, &tp->task);
570 /* check for and handle disconnect events */
571 if (((status & (ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC)) ==
572 ATA_AHCI_P_IX_PRC) &&
573 (tp = (struct ata_connect_task *)
574 malloc(sizeof(struct ata_connect_task),
575 M_ATA, M_NOWAIT | M_ZERO))) {
577 device_printf(ch->dev, "DISCONNECT requested\n");
578 tp->action = ATA_C_DETACH;
580 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
581 taskqueue_enqueue(taskqueue_thread, &tp->task);
584 /* any drive action to take care of ? */
585 if (!(issued & (1<<tag)))
586 ctlr->interrupt[unit].function(ch);
590 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, port);
594 ata_ahci_reset(device_t dev)
596 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
597 struct ata_channel *ch = device_get_softc(dev);
599 int offset = (ch->unit << 7);
601 /* kill off all activity on this channel */
602 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
603 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
604 cmd & ~(ATA_AHCI_P_CMD_CR | ATA_AHCI_P_CMD_FR |
605 ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
607 DELAY(500000); /* XXX SOS this is not entirely wrong */
610 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, ATA_AHCI_P_CMD_SUD);
612 ata_sata_phy_enable(ch);
614 /* clear any interrupts pending on this channel */
615 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset,
616 ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset));
618 /* start operations on this channel */
619 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
620 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
621 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
625 ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
627 struct ata_dmasetprd_args *args = xsc;
628 struct ata_ahci_dma_prd *prd = args->dmatab;
631 if (!(args->error = error)) {
632 for (i = 0; i < nsegs; i++) {
633 prd[i].dba = htole64(segs[i].ds_addr);
634 prd[i].dbc = htole32((segs[i].ds_len - 1) & ATA_AHCI_PRD_MASK);
641 ata_ahci_dmainit(device_t dev)
643 struct ata_channel *ch = device_get_softc(dev);
647 /* note start and stop are not used here */
648 ch->dma->setprd = ata_ahci_dmasetprd;
649 ch->dma->max_iosize = 8192 * DEV_BSIZE;
655 * Acard chipset support functions
658 ata_acard_ident(device_t dev)
660 struct ata_pci_controller *ctlr = device_get_softc(dev);
661 struct ata_chip_id *idx;
662 static struct ata_chip_id ids[] =
663 {{ ATA_ATP850R, 0, ATPOLD, 0x00, ATA_UDMA2, "Acard ATP850" },
664 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "Acard ATP860A" },
665 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "Acard ATP860R" },
666 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "Acard ATP865A" },
667 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "Acard ATP865R" },
668 { 0, 0, 0, 0, 0, 0}};
671 if (!(idx = ata_match_chip(dev, ids)))
674 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
675 device_set_desc_copy(dev, buffer);
677 ctlr->chipinit = ata_acard_chipinit;
682 ata_acard_chipinit(device_t dev)
684 struct ata_pci_controller *ctlr = device_get_softc(dev);
685 int rid = ATA_IRQ_RID;
687 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
688 RF_SHAREABLE | RF_ACTIVE))) {
689 device_printf(dev, "unable to map interrupt\n");
692 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
693 ata_acard_intr, ctlr, &ctlr->handle))) {
694 device_printf(dev, "unable to setup interrupt\n");
697 if (ctlr->chip->cfg1 == ATPOLD) {
698 ctlr->setmode = ata_acard_850_setmode;
699 ctlr->locking = ata_serialize;
702 ctlr->setmode = ata_acard_86X_setmode;
707 ata_acard_intr(void *data)
709 struct ata_pci_controller *ctlr = data;
710 struct ata_channel *ch;
713 for (unit = 0; unit < ctlr->channels; unit++) {
714 if (!(ch = ctlr->interrupt[unit].argument))
716 if (ctlr->chip->cfg1 == ATPOLD &&
717 ATA_LOCKING(ch->dev, ATA_LF_WHICH) != unit)
719 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
720 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
722 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
723 ATA_BMSTAT_INTERRUPT)
725 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
727 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
728 ATA_IDX_INB(ch, ATA_BMCMD_PORT)&~ATA_BMCMD_START_STOP);
731 ctlr->interrupt[unit].function(ch);
736 ata_acard_850_setmode(device_t dev, int mode)
738 device_t gparent = GRANDPARENT(dev);
739 struct ata_pci_controller *ctlr = device_get_softc(gparent);
740 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
741 struct ata_device *atadev = device_get_softc(dev);
742 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
745 mode = ata_limit_mode(dev, mode,
746 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
748 /* XXX SOS missing WDMA0+1 + PIO modes */
749 if (mode >= ATA_WDMA2) {
750 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
752 device_printf(dev, "%ssetting %s on %s chip\n",
753 (error) ? "FAILURE " : "",
754 ata_mode2str(mode), ctlr->chip->text);
756 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
758 reg54 &= ~(0x03 << (devno << 1));
759 if (mode >= ATA_UDMA0)
760 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
761 pci_write_config(gparent, 0x54, reg54, 1);
762 pci_write_config(gparent, 0x4a, 0xa6, 1);
763 pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
768 /* we could set PIO mode timings, but we assume the BIOS did that */
772 ata_acard_86X_setmode(device_t dev, int mode)
774 device_t gparent = GRANDPARENT(dev);
775 struct ata_pci_controller *ctlr = device_get_softc(gparent);
776 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
777 struct ata_device *atadev = device_get_softc(dev);
778 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
782 mode = ata_limit_mode(dev, mode,
783 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
785 mode = ata_check_80pin(dev, mode);
787 /* XXX SOS missing WDMA0+1 + PIO modes */
788 if (mode >= ATA_WDMA2) {
789 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
791 device_printf(dev, "%ssetting %s on %s chip\n",
792 (error) ? "FAILURE " : "",
793 ata_mode2str(mode), ctlr->chip->text);
795 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
797 reg44 &= ~(0x000f << (devno << 2));
798 if (mode >= ATA_UDMA0)
799 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
800 pci_write_config(gparent, 0x44, reg44, 2);
801 pci_write_config(gparent, 0x4a, 0xa6, 1);
802 pci_write_config(gparent, 0x40 + devno, 0x31, 1);
807 /* we could set PIO mode timings, but we assume the BIOS did that */
812 * Acer Labs Inc (ALI) chipset support functions
815 ata_ali_ident(device_t dev)
817 struct ata_pci_controller *ctlr = device_get_softc(dev);
818 struct ata_chip_id *idx;
819 static struct ata_chip_id ids[] =
820 {{ ATA_ALI_5289, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5289" },
821 { ATA_ALI_5287, 0x00, 4, ALISATA, ATA_SA150, "AcerLabs M5287" },
822 { ATA_ALI_5281, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5281" },
823 { ATA_ALI_5229, 0xc5, 0, ALINEW, ATA_UDMA6, "AcerLabs M5229" },
824 { ATA_ALI_5229, 0xc4, 0, ALINEW, ATA_UDMA5, "AcerLabs M5229" },
825 { ATA_ALI_5229, 0xc2, 0, ALINEW, ATA_UDMA4, "AcerLabs M5229" },
826 { ATA_ALI_5229, 0x20, 0, ALIOLD, ATA_UDMA2, "AcerLabs M5229" },
827 { ATA_ALI_5229, 0x00, 0, ALIOLD, ATA_WDMA2, "AcerLabs M5229" },
828 { 0, 0, 0, 0, 0, 0}};
831 if (!(idx = ata_match_chip(dev, ids)))
834 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
835 device_set_desc_copy(dev, buffer);
837 ctlr->chipinit = ata_ali_chipinit;
842 ata_ali_chipinit(device_t dev)
844 struct ata_pci_controller *ctlr = device_get_softc(dev);
846 if (ata_setup_interrupt(dev))
849 switch (ctlr->chip->cfg2) {
851 pci_write_config(dev, PCIR_COMMAND,
852 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
853 ctlr->channels = ctlr->chip->cfg1;
854 ctlr->allocate = ata_ali_allocate;
855 ctlr->setmode = ata_sata_setmode;
859 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
860 pci_write_config(dev, 0x53,
861 pci_read_config(dev, 0x53, 1) | 0x01, 1);
863 /* enable cable detection and UDMA support on newer chips */
864 pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
865 ctlr->setmode = ata_ali_setmode;
869 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
870 pci_write_config(dev, 0x53,
871 (pci_read_config(dev, 0x53, 1) & ~0x02) | 0x03, 1);
872 ctlr->setmode = ata_ali_setmode;
879 ata_ali_allocate(device_t dev)
881 device_t parent = device_get_parent(dev);
882 struct ata_pci_controller *ctlr = device_get_softc(parent);
883 struct ata_channel *ch = device_get_softc(dev);
884 struct resource *io = NULL, *ctlio = NULL;
885 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
888 rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
889 io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
893 rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
894 ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
896 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
900 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
901 ch->r_io[i].res = io;
902 ch->r_io[i].offset = i + (unit10 ? 8 : 0);
904 ch->r_io[ATA_CONTROL].res = ctlio;
905 ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
906 ch->r_io[ATA_IDX_ADDR].res = io;
907 ata_default_registers(dev);
909 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
910 ch->r_io[i].res = ctlr->r_res1;
911 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
914 ch->flags |= ATA_NO_SLAVE;
916 /* XXX SOS PHY handling awkward in ALI chip not supported yet */
922 ata_ali_setmode(device_t dev, int mode)
924 device_t gparent = GRANDPARENT(dev);
925 struct ata_pci_controller *ctlr = device_get_softc(gparent);
926 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
927 struct ata_device *atadev = device_get_softc(dev);
928 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
931 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
933 if (ctlr->chip->cfg2 & ALINEW) {
934 if (mode > ATA_UDMA2 &&
935 pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
936 ata_print_cable(dev, "controller");
941 mode = ata_check_80pin(dev, mode);
943 if (ctlr->chip->cfg2 & ALIOLD) {
944 /* doesn't support ATAPI DMA on write */
945 ch->flags |= ATA_ATAPI_DMA_RO;
946 if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
947 /* doesn't support ATAPI DMA on two ATAPI devices */
948 device_printf(dev, "two atapi devices on this channel, no DMA\n");
949 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
953 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
956 device_printf(dev, "%ssetting %s on %s chip\n",
957 (error) ? "FAILURE " : "",
958 ata_mode2str(mode), ctlr->chip->text);
960 if (mode >= ATA_UDMA0) {
961 u_int8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
962 u_int32_t word54 = pci_read_config(gparent, 0x54, 4);
964 word54 &= ~(0x000f000f << (devno << 2));
965 word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
966 pci_write_config(gparent, 0x54, word54, 4);
967 pci_write_config(gparent, 0x58 + (ch->unit << 2),
971 u_int32_t piotimings[] =
972 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
973 0x00310001, 0x00440001, 0x00330001, 0x00310001};
975 pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
976 ~(0x0008000f << (devno << 2)), 4);
977 pci_write_config(gparent, 0x58 + (ch->unit << 2),
978 piotimings[ata_mode2idx(mode)], 4);
986 * American Micro Devices (AMD) chipset support functions
989 ata_amd_ident(device_t dev)
991 struct ata_pci_controller *ctlr = device_get_softc(dev);
992 struct ata_chip_id *idx;
993 static struct ata_chip_id ids[] =
994 {{ ATA_AMD756, 0x00, AMDNVIDIA, 0x00, ATA_UDMA4, "AMD 756" },
995 { ATA_AMD766, 0x00, AMDNVIDIA, AMDCABLE|AMDBUG, ATA_UDMA5, "AMD 766" },
996 { ATA_AMD768, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA5, "AMD 768" },
997 { ATA_AMD8111, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA6, "AMD 8111" },
998 { 0, 0, 0, 0, 0, 0}};
1001 if (!(idx = ata_match_chip(dev, ids)))
1004 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1005 device_set_desc_copy(dev, buffer);
1007 ctlr->chipinit = ata_amd_chipinit;
1012 ata_amd_chipinit(device_t dev)
1014 struct ata_pci_controller *ctlr = device_get_softc(dev);
1016 if (ata_setup_interrupt(dev))
1019 /* disable/set prefetch, postwrite */
1020 if (ctlr->chip->cfg2 & AMDBUG)
1021 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
1023 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
1025 ctlr->setmode = ata_via_family_setmode;
1031 * Cyrix chipset support functions
1034 ata_cyrix_ident(device_t dev)
1036 struct ata_pci_controller *ctlr = device_get_softc(dev);
1038 if (pci_get_devid(dev) == ATA_CYRIX_5530) {
1039 device_set_desc(dev, "Cyrix 5530 ATA33 controller");
1040 ctlr->chipinit = ata_cyrix_chipinit;
1047 ata_cyrix_chipinit(device_t dev)
1049 struct ata_pci_controller *ctlr = device_get_softc(dev);
1051 if (ata_setup_interrupt(dev))
1055 ctlr->setmode = ata_cyrix_setmode;
1057 ctlr->setmode = ata_generic_setmode;
1062 ata_cyrix_setmode(device_t dev, int mode)
1064 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1065 struct ata_device *atadev = device_get_softc(dev);
1066 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1067 u_int32_t piotiming[] =
1068 { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
1069 u_int32_t dmatiming[] = { 0x00077771, 0x00012121, 0x00002020 };
1070 u_int32_t udmatiming[] = { 0x00921250, 0x00911140, 0x00911030 };
1073 ch->dma->alignment = 16;
1074 ch->dma->max_iosize = 126 * DEV_BSIZE;
1076 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1078 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1081 device_printf(dev, "%ssetting %s on Cyrix chip\n",
1082 (error) ? "FAILURE " : "", ata_mode2str(mode));
1084 if (mode >= ATA_UDMA0) {
1085 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1086 0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
1088 else if (mode >= ATA_WDMA0) {
1089 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1090 0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
1093 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1094 0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
1096 atadev->mode = mode;
1102 * Cypress chipset support functions
1105 ata_cypress_ident(device_t dev)
1107 struct ata_pci_controller *ctlr = device_get_softc(dev);
1110 * the Cypress chip is a mess, it contains two ATA functions, but
1111 * both channels are visible on the first one.
1112 * simply ignore the second function for now, as the right
1113 * solution (ignoring the second channel on the first function)
1114 * doesn't work with the crappy ATA interrupt setup on the alpha.
1116 if (pci_get_devid(dev) == ATA_CYPRESS_82C693 &&
1117 pci_get_function(dev) == 1 &&
1118 pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
1119 device_set_desc(dev, "Cypress 82C693 ATA controller");
1120 ctlr->chipinit = ata_cypress_chipinit;
1127 ata_cypress_chipinit(device_t dev)
1129 struct ata_pci_controller *ctlr = device_get_softc(dev);
1131 if (ata_setup_interrupt(dev))
1134 ctlr->setmode = ata_cypress_setmode;
1139 ata_cypress_setmode(device_t dev, int mode)
1141 device_t gparent = GRANDPARENT(dev);
1142 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1143 struct ata_device *atadev = device_get_softc(dev);
1146 mode = ata_limit_mode(dev, mode, ATA_WDMA2);
1148 /* XXX SOS missing WDMA0+1 + PIO modes */
1149 if (mode == ATA_WDMA2) {
1150 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1152 device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
1153 error ? "FAILURE " : "");
1155 pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
1156 atadev->mode = mode;
1160 /* we could set PIO mode timings, but we assume the BIOS did that */
1165 * HighPoint chipset support functions
1168 ata_highpoint_ident(device_t dev)
1170 struct ata_pci_controller *ctlr = device_get_softc(dev);
1171 struct ata_chip_id *idx;
1172 static struct ata_chip_id ids[] =
1173 {{ ATA_HPT374, 0x07, HPT374, 0x00, ATA_UDMA6, "HighPoint HPT374" },
1174 { ATA_HPT372, 0x02, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372N" },
1175 { ATA_HPT372, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372" },
1176 { ATA_HPT371, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT371" },
1177 { ATA_HPT366, 0x05, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372" },
1178 { ATA_HPT366, 0x03, HPT370, 0x00, ATA_UDMA5, "HighPoint HPT370" },
1179 { ATA_HPT366, 0x02, HPT366, 0x00, ATA_UDMA4, "HighPoint HPT368" },
1180 { ATA_HPT366, 0x00, HPT366, HPTOLD, ATA_UDMA4, "HighPoint HPT366" },
1181 { ATA_HPT302, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT302" },
1182 { 0, 0, 0, 0, 0, 0}};
1185 if (!(idx = ata_match_chip(dev, ids)))
1188 strcpy(buffer, idx->text);
1189 if (idx->cfg1 == HPT374) {
1190 if (pci_get_function(dev) == 0)
1191 strcat(buffer, " (channel 0+1)");
1192 else if (pci_get_function(dev) == 1)
1193 strcat(buffer, " (channel 2+3)");
1195 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
1196 device_set_desc_copy(dev, buffer);
1198 ctlr->chipinit = ata_highpoint_chipinit;
1203 ata_highpoint_chipinit(device_t dev)
1205 struct ata_pci_controller *ctlr = device_get_softc(dev);
1206 int rid = ATA_IRQ_RID;
1208 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1209 RF_SHAREABLE | RF_ACTIVE))) {
1210 device_printf(dev, "unable to map interrupt\n");
1213 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1214 ata_highpoint_intr, ctlr, &ctlr->handle))) {
1215 device_printf(dev, "unable to setup interrupt\n");
1219 if (ctlr->chip->cfg2 == HPTOLD) {
1220 /* disable interrupt prediction */
1221 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
1224 /* disable interrupt prediction */
1225 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
1226 pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
1228 /* enable interrupts */
1229 pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
1231 /* set clocks etc */
1232 if (ctlr->chip->cfg1 < HPT372)
1233 pci_write_config(dev, 0x5b, 0x22, 1);
1235 pci_write_config(dev, 0x5b,
1236 (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
1238 ctlr->setmode = ata_highpoint_setmode;
1243 ata_highpoint_intr(void *data)
1245 struct ata_pci_controller *ctlr = data;
1246 struct ata_channel *ch;
1249 for (unit = 0; unit < ctlr->channels; unit++) {
1250 if (!(ch = ctlr->interrupt[unit].argument))
1253 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1255 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1256 ATA_BMSTAT_INTERRUPT)
1258 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1261 ctlr->interrupt[unit].function(ch);
1266 ata_highpoint_setmode(device_t dev, int mode)
1268 device_t gparent = GRANDPARENT(dev);
1269 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1270 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1271 struct ata_device *atadev = device_get_softc(dev);
1272 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1274 u_int32_t timings33[][4] = {
1275 /* HPT366 HPT370 HPT372 HPT374 mode */
1276 { 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a }, /* PIO 0 */
1277 { 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 }, /* PIO 1 */
1278 { 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 }, /* PIO 2 */
1279 { 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 }, /* PIO 3 */
1280 { 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 }, /* PIO 4 */
1281 { 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea }, /* MWDMA 0 */
1282 { 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 }, /* MWDMA 1 */
1283 { 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 }, /* MWDMA 2 */
1284 { 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea }, /* UDMA 0 */
1285 { 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 }, /* UDMA 1 */
1286 { 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 }, /* UDMA 2 */
1287 { 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 }, /* UDMA 3 */
1288 { 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 }, /* UDMA 4 */
1289 { 0, 0x16454e31, 0x1c8a9c62, 0x12848242 }, /* UDMA 5 */
1290 { 0, 0, 0x1c869c62, 0x12808242 } /* UDMA 6 */
1293 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1295 if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
1296 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1298 mode = ata_highpoint_check_80pin(dev, mode);
1301 * most if not all HPT chips cant really handle that the device is
1302 * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
1303 * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
1305 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
1306 ata_limit_mode(dev, mode, ATA_UDMA5));
1308 device_printf(dev, "%ssetting %s on HighPoint chip\n",
1309 (error) ? "FAILURE " : "", ata_mode2str(mode));
1311 pci_write_config(gparent, 0x40 + (devno << 2),
1312 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
1313 atadev->mode = mode;
1317 ata_highpoint_check_80pin(device_t dev, int mode)
1319 device_t gparent = GRANDPARENT(dev);
1320 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1321 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1322 u_int8_t reg, val, res;
1324 if (ctlr->chip->cfg1 == HPT374 && pci_get_function(gparent) == 1) {
1325 reg = ch->unit ? 0x57 : 0x53;
1326 val = pci_read_config(gparent, reg, 1);
1327 pci_write_config(gparent, reg, val | 0x80, 1);
1331 val = pci_read_config(gparent, reg, 1);
1332 pci_write_config(gparent, reg, val & 0xfe, 1);
1334 res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1335 pci_write_config(gparent, reg, val, 1);
1337 if (mode > ATA_UDMA2 && res) {
1338 ata_print_cable(dev, "controller");
1346 * Intel chipset support functions
1349 ata_intel_ident(device_t dev)
1351 struct ata_pci_controller *ctlr = device_get_softc(dev);
1352 struct ata_chip_id *idx;
1353 static struct ata_chip_id ids[] =
1354 {{ ATA_I82371FB, 0, 0, 0x00, ATA_WDMA2, "Intel PIIX" },
1355 { ATA_I82371SB, 0, 0, 0x00, ATA_WDMA2, "Intel PIIX3" },
1356 { ATA_I82371AB, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1357 { ATA_I82443MX, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1358 { ATA_I82451NX, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1359 { ATA_I82801AB, 0, 0, 0x00, ATA_UDMA2, "Intel ICH0" },
1360 { ATA_I82801AA, 0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1361 { ATA_I82372FB, 0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1362 { ATA_I82801BA, 0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1363 { ATA_I82801BA_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1364 { ATA_I82801CA, 0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1365 { ATA_I82801CA_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1366 { ATA_I82801DB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1367 { ATA_I82801DB_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1368 { ATA_I82801EB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH5" },
1369 { ATA_I82801EB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1370 { ATA_I82801EB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1371 { ATA_I6300ESB, 0, 0, 0x00, ATA_UDMA5, "Intel 6300ESB" },
1372 { ATA_I6300ESB_S1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1373 { ATA_I6300ESB_R1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1374 { ATA_I82801FB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH6" },
1375 { ATA_I82801FB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1376 { ATA_I82801FB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1377 { ATA_I82801FB_M, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1378 { ATA_I82801GB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH7" },
1379 { ATA_I82801GB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH7" },
1380 { ATA_I82801GB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH7" },
1381 { ATA_I82801GB_M, 0, 0, 0x00, ATA_SA150, "Intel ICH7" },
1382 { ATA_I82801GB_AH, 0, 0, 0x00, ATA_SA150, "Intel ICH7" },
1383 { 0, 0, 0, 0, 0, 0}};
1386 if (!(idx = ata_match_chip(dev, ids)))
1389 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1390 device_set_desc_copy(dev, buffer);
1392 ctlr->chipinit = ata_intel_chipinit;
1397 ata_intel_chipinit(device_t dev)
1399 struct ata_pci_controller *ctlr = device_get_softc(dev);
1400 int rid = ATA_IRQ_RID;
1402 if (!ata_legacy(dev)) {
1403 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1404 RF_SHAREABLE | RF_ACTIVE))) {
1405 device_printf(dev, "unable to map interrupt\n");
1408 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1409 ata_intel_intr, ctlr, &ctlr->handle))) {
1410 device_printf(dev, "unable to setup interrupt\n");
1415 if (ctlr->chip->chipid == ATA_I82371FB) {
1416 ctlr->setmode = ata_intel_old_setmode;
1418 else if (ctlr->chip->max_dma < ATA_SA150) {
1419 ctlr->setmode = ata_intel_new_setmode;
1422 /* if we have BAR(5) as a memory resource we should use AHCI mode */
1423 ctlr->r_type2 = SYS_RES_MEMORY;
1424 ctlr->r_rid2 = PCIR_BAR(5);
1425 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1426 &ctlr->r_rid2, RF_ACTIVE))) {
1427 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
1428 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1429 ata_ahci_intr, ctlr, &ctlr->handle)) {
1430 device_printf(dev, "unable to setup interrupt\n");
1434 /* force all ports active "the legacy way" */
1435 pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f,2);
1437 /* enable AHCI mode */
1438 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC, ATA_AHCI_GHC_AE);
1440 /* get the number of HW channels */
1441 ctlr->channels = (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) &
1442 ATA_AHCI_NPMASK) + 1;
1444 /* enable AHCI interrupts */
1445 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
1446 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) | ATA_AHCI_GHC_IE);
1448 ctlr->reset = ata_ahci_reset;
1449 ctlr->dmainit = ata_ahci_dmainit;
1450 ctlr->allocate = ata_ahci_allocate;
1453 ctlr->reset = ata_intel_reset;
1455 ctlr->setmode = ata_sata_setmode;
1456 pci_write_config(dev, PCIR_COMMAND,
1457 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1463 ata_intel_intr(void *data)
1465 struct ata_pci_controller *ctlr = data;
1466 struct ata_channel *ch;
1469 for (unit = 0; unit < ctlr->channels; unit++) {
1470 if (!(ch = ctlr->interrupt[unit].argument))
1473 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1475 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1476 ATA_BMSTAT_INTERRUPT)
1478 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1481 ctlr->interrupt[unit].function(ch);
1486 ata_intel_old_setmode(device_t dev, int mode)
1492 ata_intel_reset(device_t dev)
1494 device_t parent = device_get_parent(dev);
1495 struct ata_pci_controller *ctlr = device_get_softc(parent);
1496 struct ata_channel *ch = device_get_softc(dev);
1499 /* ICH6 has 4 SATA ports as master/slave on 2 channels so deal with pairs */
1500 if (ctlr->chip->chipid == ATA_I82801FB_S1 ||
1501 ctlr->chip->chipid == ATA_I82801FB_R1) {
1502 mask = (0x0005 << ch->unit);
1505 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1506 if (pci_read_config(parent, 0x90, 1) & 0x04)
1509 mask = (0x0001 << ch->unit);
1510 /* XXX SOS should be in intel_allocate when we grow it */
1511 ch->flags |= ATA_NO_SLAVE;
1514 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1516 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1518 /* wait up to 1 sec for "connect well" */
1519 for (timeout = 0; timeout < 100 ; timeout++) {
1520 if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1521 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1525 ata_generic_reset(dev);
1529 ata_intel_new_setmode(device_t dev, int mode)
1531 device_t gparent = GRANDPARENT(dev);
1532 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1533 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1534 struct ata_device *atadev = device_get_softc(dev);
1535 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1536 u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
1537 u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
1538 u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
1539 u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
1540 u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
1541 u_int32_t mask40 = 0, new40 = 0;
1542 u_int8_t mask44 = 0, new44 = 0;
1544 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1545 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1547 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1549 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1550 ata_print_cable(dev, "controller");
1554 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1557 device_printf(dev, "%ssetting %s on %s chip\n",
1558 (error) ? "FAILURE " : "",
1559 ata_mode2str(mode), ctlr->chip->text);
1563 if (mode >= ATA_UDMA0) {
1564 pci_write_config(gparent, 0x48, reg48 | (0x0001 << devno), 2);
1565 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno<<2))) |
1566 (0x01 + !(mode & 0x01)), 2);
1569 pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
1570 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))), 2);
1573 if (mode >= ATA_UDMA2)
1574 pci_write_config(gparent, 0x54, reg54 | (0x1 << devno), 2);
1576 pci_write_config(gparent, 0x54, reg54 & ~(0x1 << devno), 2);
1578 if (mode >= ATA_UDMA5)
1579 pci_write_config(gparent, 0x54, reg54 | (0x1000 << devno), 2);
1581 pci_write_config(gparent, 0x54, reg54 & ~(0x1000 << devno), 2);
1583 reg40 &= ~0x00ff00ff;
1584 reg40 |= 0x40774077;
1586 if (atadev->unit == ATA_MASTER) {
1588 new40 = timings[ata_mode2idx(mode)] << 8;
1592 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
1593 (timings[ata_mode2idx(mode)] & 0x03);
1601 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
1602 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
1604 atadev->mode = mode;
1609 * Integrated Technology Express Inc. (ITE) chipset support functions
1612 ata_ite_ident(device_t dev)
1614 struct ata_pci_controller *ctlr = device_get_softc(dev);
1616 if (pci_get_devid(dev) == ATA_IT8212F) {
1617 device_set_desc(dev, "ITE IT8212F ATA133 controller");
1618 ctlr->chipinit = ata_ite_chipinit;
1625 ata_ite_chipinit(device_t dev)
1627 struct ata_pci_controller *ctlr = device_get_softc(dev);
1629 if (ata_setup_interrupt(dev))
1632 ctlr->setmode = ata_ite_setmode;
1634 /* set PCI mode and 66Mhz reference clock */
1635 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
1637 /* set default active & recover timings */
1638 pci_write_config(dev, 0x54, 0x31, 1);
1639 pci_write_config(dev, 0x56, 0x31, 1);
1644 ata_ite_setmode(device_t dev, int mode)
1646 device_t gparent = GRANDPARENT(dev);
1647 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1648 struct ata_device *atadev = device_get_softc(dev);
1649 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1652 /* correct the mode for what the HW supports */
1653 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
1655 /* check the CBLID bits for 80 conductor cable detection */
1656 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
1657 (ch->unit ? (1<<3) : (1<<2)))) {
1658 ata_print_cable(dev, "controller");
1662 /* set the wanted mode on the device */
1663 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1666 device_printf(dev, "%s setting %s on ITE8212F chip\n",
1667 (error) ? "failed" : "success", ata_mode2str(mode));
1669 /* if the device accepted the mode change, setup the HW accordingly */
1671 if (mode >= ATA_UDMA0) {
1672 u_int8_t udmatiming[] =
1673 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
1675 /* enable UDMA mode */
1676 pci_write_config(gparent, 0x50,
1677 pci_read_config(gparent, 0x50, 1) &
1678 ~(1 << (devno + 3)), 1);
1680 /* set UDMA timing */
1681 pci_write_config(gparent,
1682 0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
1683 udmatiming[mode & ATA_MODE_MASK], 1);
1686 u_int8_t chtiming[] =
1687 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
1689 /* disable UDMA mode */
1690 pci_write_config(gparent, 0x50,
1691 pci_read_config(gparent, 0x50, 1) |
1692 (1 << (devno + 3)), 1);
1694 /* set active and recover timing (shared between master & slave) */
1695 if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
1696 chtiming[ata_mode2idx(mode)])
1697 pci_write_config(gparent, 0x54 + (ch->unit << 2),
1698 chtiming[ata_mode2idx(mode)], 1);
1700 atadev->mode = mode;
1706 * National chipset support functions
1709 ata_national_ident(device_t dev)
1711 struct ata_pci_controller *ctlr = device_get_softc(dev);
1713 /* this chip is a clone of the Cyrix chip, bugs and all */
1714 if (pci_get_devid(dev) == ATA_SC1100) {
1715 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
1716 ctlr->chipinit = ata_national_chipinit;
1722 static device_t nat_host = NULL;
1725 ata_national_chipinit(device_t dev)
1727 struct ata_pci_controller *ctlr = device_get_softc(dev);
1731 if (ata_setup_interrupt(dev))
1734 /* locate the ISA part in the southbridge and enable UDMA33 */
1735 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
1736 for (i = 0; i < nchildren; i++) {
1737 if (pci_get_devid(children[i]) == 0x0510100b) {
1738 nat_host = children[i];
1742 free(children, M_TEMP);
1744 ctlr->setmode = ata_national_setmode;
1749 ata_national_setmode(device_t dev, int mode)
1751 device_t gparent = GRANDPARENT(dev);
1752 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1753 struct ata_device *atadev = device_get_softc(dev);
1754 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1755 u_int32_t piotiming[] =
1756 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
1757 0x00803020, 0x20102010, 0x00100010,
1758 0x00100010, 0x00100010, 0x00100010 };
1759 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
1760 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
1763 ch->dma->alignment = 16;
1764 ch->dma->max_iosize = 126 * DEV_BSIZE;
1766 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1768 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1771 device_printf(dev, "%s setting %s on National chip\n",
1772 (error) ? "failed" : "success", ata_mode2str(mode));
1774 if (mode >= ATA_UDMA0) {
1775 pci_write_config(gparent, 0x44 + (devno << 3),
1776 udmatiming[mode & ATA_MODE_MASK], 4);
1778 else if (mode >= ATA_WDMA0) {
1779 pci_write_config(gparent, 0x44 + (devno << 3),
1780 dmatiming[mode & ATA_MODE_MASK], 4);
1783 pci_write_config(gparent, 0x44 + (devno << 3),
1784 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
1787 pci_write_config(gparent, 0x40 + (devno << 3),
1788 piotiming[ata_mode2idx(mode)], 4);
1789 atadev->mode = mode;
1795 * nVidia chipset support functions
1798 ata_nvidia_ident(device_t dev)
1800 struct ata_pci_controller *ctlr = device_get_softc(dev);
1801 struct ata_chip_id *idx;
1802 static struct ata_chip_id ids[] =
1803 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nVidia nForce" },
1804 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2" },
1805 { ATA_NFORCE2_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2 MCP" },
1806 { ATA_NFORCE2_MCP_S1, 0, 0, 0, ATA_SA150, "nVidia nForce2 MCP" },
1807 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3" },
1808 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 Pro" },
1809 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
1810 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
1811 { ATA_NFORCE3_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 MCP" },
1812 { ATA_NFORCE3_MCP_S1, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
1813 { ATA_NFORCE3_MCP_S2, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
1814 { ATA_NFORCE4, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce4" },
1815 { ATA_NFORCE4_S1, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce4" },
1816 { ATA_NFORCE4_S2, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce4" },
1817 { 0, 0, 0, 0, 0, 0}};
1820 if (!(idx = ata_match_chip(dev, ids)))
1823 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1824 device_set_desc_copy(dev, buffer);
1826 ctlr->chipinit = ata_nvidia_chipinit;
1831 ata_nvidia_chipinit(device_t dev)
1833 struct ata_pci_controller *ctlr = device_get_softc(dev);
1835 if (ata_setup_interrupt(dev))
1838 if (ctlr->chip->max_dma >= ATA_SA150) {
1839 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
1840 ctlr->r_type2 = SYS_RES_IOPORT;
1842 ctlr->r_type2 = SYS_RES_MEMORY;
1843 ctlr->r_rid2 = PCIR_BAR(5);
1844 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1845 &ctlr->r_rid2, RF_ACTIVE))) {
1846 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
1847 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1848 ata_nvidia_intr, ctlr, &ctlr->handle)) {
1849 device_printf(dev, "unable to setup interrupt\n");
1852 pci_write_config(dev, PCIR_COMMAND,
1853 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
1854 ctlr->allocate = ata_nvidia_allocate;
1855 ctlr->reset = ata_nvidia_reset;
1857 ctlr->setmode = ata_sata_setmode;
1860 /* disable prefetch, postwrite */
1861 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
1862 ctlr->setmode = ata_via_family_setmode;
1868 ata_nvidia_allocate(device_t dev)
1870 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1871 struct ata_channel *ch = device_get_softc(dev);
1872 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0441 : 0x0011;
1874 /* setup the usual register normal pci style */
1875 ata_pci_allocate(dev);
1877 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
1878 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
1879 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
1880 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
1881 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
1882 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
1883 ch->flags |= ATA_NO_SLAVE;
1885 /* enable PHY state change interrupts */
1886 ATA_OUTB(ctlr->r_res2, offset,
1887 ATA_INB(ctlr->r_res2, offset) | (0x0c << (ch->unit << 2)));
1892 ata_nvidia_intr(void *data)
1894 struct ata_pci_controller *ctlr = data;
1895 struct ata_channel *ch;
1896 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
1900 for (unit = 0; unit < ctlr->channels; unit++) {
1901 if (!(ch = ctlr->interrupt[unit].argument))
1904 if ((status = ATA_INB(ctlr->r_res2, offset))) {
1905 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
1906 struct ata_connect_task *tp;
1908 /* clear error bits/interrupt */
1909 ATA_IDX_OUTL(ch, ATA_SERROR, error);
1910 ATA_OUTB(ctlr->r_res2, offset, status);
1912 /* check for and handle connect events */
1913 if ((status & (0x04 << (ch->unit << 2))) &&
1914 (tp = (struct ata_connect_task *)
1915 malloc(sizeof(struct ata_connect_task),
1916 M_ATA, M_NOWAIT | M_ZERO))) {
1918 device_printf(ch->dev, "CONNECT requested\n");
1919 tp->action = ATA_C_ATTACH;
1921 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1922 taskqueue_enqueue(taskqueue_thread, &tp->task);
1926 /* check for and handle disconnect events */
1927 if ((status & (0x08 << (ch->unit << 2))) &&
1928 (tp = (struct ata_connect_task *)
1929 malloc(sizeof(struct ata_connect_task),
1930 M_ATA, M_NOWAIT | M_ZERO))) {
1932 device_printf(ch->dev, "DISCONNECT requested\n");
1933 tp->action = ATA_C_DETACH;
1935 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1936 taskqueue_enqueue(taskqueue_thread, &tp->task);
1939 /* any drive action to take care of ? */
1940 if (status & (0x01 << (ch->unit << 2))) {
1941 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
1943 ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1945 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
1947 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat&~ATA_BMSTAT_ERROR);
1950 ctlr->interrupt[unit].function(ch);
1957 ata_nvidia_reset(device_t dev)
1959 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1960 struct ata_channel *ch = device_get_softc(dev);
1961 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0441 : 0x0011;
1963 /* disable PHY state change interrupt */
1964 ATA_OUTB(ctlr->r_res2, offset,
1965 ATA_INB(ctlr->r_res2, offset) & (~0x0c << (ch->unit << 2)));
1967 ata_sata_phy_enable(ch);
1969 /* enable PHY state change interrupt */
1970 ATA_OUTB(ctlr->r_res2, offset,
1971 ATA_INB(ctlr->r_res2, offset) | (0x0c << (ch->unit << 2)));
1976 * Promise chipset support functions
1978 #define ATA_PDC_APKT_OFFSET 0x00000010
1979 #define ATA_PDC_HPKT_OFFSET 0x00000040
1980 #define ATA_PDC_ASG_OFFSET 0x00000080
1981 #define ATA_PDC_LSG_OFFSET 0x000000c0
1982 #define ATA_PDC_HSG_OFFSET 0x00000100
1983 #define ATA_PDC_CHN_OFFSET 0x00000400
1984 #define ATA_PDC_BUF_BASE 0x00400000
1985 #define ATA_PDC_BUF_OFFSET 0x00100000
1986 #define ATA_PDC_MAX_HPKT 8
1987 #define ATA_PDC_WRITE_REG 0x00
1988 #define ATA_PDC_WRITE_CTL 0x0e
1989 #define ATA_PDC_WRITE_END 0x08
1990 #define ATA_PDC_WAIT_NBUSY 0x10
1991 #define ATA_PDC_WAIT_READY 0x18
1992 #define ATA_PDC_1B 0x20
1993 #define ATA_PDC_2B 0x40
1995 struct host_packet {
1997 TAILQ_ENTRY(host_packet) chain;
2000 struct ata_promise_sx4 {
2002 TAILQ_HEAD(, host_packet) queue;
2007 ata_promise_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_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "Promise PDC20246" },
2013 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20262" },
2014 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20263" },
2015 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20265" },
2016 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20267" },
2017 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20268" },
2018 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20269" },
2019 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20270" },
2020 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20271" },
2021 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20275" },
2022 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "Promise PDC20276" },
2023 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20277" },
2024 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20318" },
2025 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20319" },
2026 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20371" },
2027 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20375" },
2028 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20376" },
2029 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20377" },
2030 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20378" },
2031 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20379" },
2032 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20571" },
2033 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20575" },
2034 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20579" },
2035 { ATA_PDC20580, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20580" },
2036 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20617" },
2037 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20618" },
2038 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20619" },
2039 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20620" },
2040 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "Promise PDC20621" },
2041 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "Promise PDC20622" },
2042 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40518" },
2043 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40519" },
2044 { 0, 0, 0, 0, 0, 0}};
2046 uintptr_t devid = 0;
2048 if (!(idx = ata_match_chip(dev, ids)))
2051 /* if we are on a SuperTrak SX6000 dont attach */
2052 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
2053 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
2054 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
2055 devid == ATA_I960RM)
2058 strcpy(buffer, idx->text);
2060 /* if we are on a FastTrak TX4, adjust the interrupt resource */
2061 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
2062 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
2063 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
2064 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
2065 static long start = 0, end = 0;
2067 if (pci_get_slot(dev) == 1) {
2068 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
2069 strcat(buffer, " (channel 0+1)");
2071 else if (pci_get_slot(dev) == 2 && start && end) {
2072 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
2074 strcat(buffer, " (channel 2+3)");
2080 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
2081 device_set_desc_copy(dev, buffer);
2083 ctlr->chipinit = ata_promise_chipinit;
2088 ata_promise_chipinit(device_t dev)
2090 struct ata_pci_controller *ctlr = device_get_softc(dev);
2091 int rid = ATA_IRQ_RID;
2093 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2094 RF_SHAREABLE | RF_ACTIVE))) {
2095 device_printf(dev, "unable to map interrupt\n");
2099 if (ctlr->chip->max_dma >= ATA_SA150)
2100 ctlr->setmode = ata_sata_setmode;
2102 ctlr->setmode = ata_promise_setmode;
2104 switch (ctlr->chip->cfg1) {
2107 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
2109 ctlr->dmainit = ata_promise_new_dmainit;
2113 /* enable burst mode */
2114 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
2116 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2117 ata_promise_old_intr, ctlr, &ctlr->handle))) {
2118 device_printf(dev, "unable to setup interrupt\n");
2124 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2125 ata_promise_tx2_intr, ctlr, &ctlr->handle))) {
2126 device_printf(dev, "unable to setup interrupt\n");
2132 // if (ctlr->r_res1)
2133 // bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
2134 ctlr->r_type1 = SYS_RES_MEMORY;
2135 ctlr->r_rid1 = PCIR_BAR(4);
2136 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
2137 &ctlr->r_rid1, RF_ACTIVE)))
2140 ctlr->r_type2 = SYS_RES_MEMORY;
2141 ctlr->r_rid2 = PCIR_BAR(3);
2142 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2143 &ctlr->r_rid2, RF_ACTIVE))){
2144 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
2147 ctlr->reset = ata_promise_mio_reset;
2148 ctlr->dmainit = ata_promise_mio_dmainit;
2149 ctlr->allocate = ata_promise_mio_allocate;
2151 if (ctlr->chip->cfg2 == PRSX4X) {
2152 struct ata_promise_sx4 *hpkt;
2153 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
2155 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2156 ata_promise_sx4_intr, ctlr, &ctlr->handle))) {
2157 device_printf(dev, "unable to setup interrupt\n");
2158 /* XXX SOS release resources */
2162 /* print info about cache memory */
2163 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
2164 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
2165 ((dimm >> 24) & 0xff),
2166 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
2167 " ECC enabled" : "" );
2169 /* adjust cache memory parameters */
2170 ATA_OUTL(ctlr->r_res2, 0x000c000c,
2171 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
2173 /* setup host packet controls */
2174 hpkt = malloc(sizeof(struct ata_promise_sx4),
2175 M_TEMP, M_NOWAIT | M_ZERO);
2176 mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
2177 TAILQ_INIT(&hpkt->queue);
2178 hpkt->busy = 0; //hpkt->head = hpkt->tail = 0;
2179 device_set_ivars(dev, hpkt);
2184 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2185 ata_promise_mio_intr, ctlr, &ctlr->handle))) {
2186 device_printf(dev, "unable to setup interrupt\n");
2187 /* XXX SOS release resources */
2191 switch (ctlr->chip->cfg2) {
2193 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
2194 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
2198 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
2199 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 3;
2203 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
2208 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
2213 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
2218 /* XXX SOS release resources */
2222 /* XXX SOS release resources */
2229 ata_promise_mio_allocate(device_t dev)
2231 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2232 struct ata_channel *ch = device_get_softc(dev);
2233 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
2236 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2237 ch->r_io[i].res = ctlr->r_res2;
2238 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
2240 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
2241 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
2242 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
2243 ata_default_registers(dev);
2244 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
2245 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
2246 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2247 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
2248 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2249 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
2250 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2251 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
2252 ch->flags |= ATA_NO_SLAVE;
2254 ch->flags |= ATA_USE_16BIT;
2256 ata_generic_hw(dev);
2258 ch->hw.command = ata_promise_sx4_command;
2260 ch->hw.command = ata_promise_mio_command;
2265 ata_promise_mio_intr(void *data)
2267 struct ata_pci_controller *ctlr = data;
2268 struct ata_channel *ch;
2269 u_int32_t vector = 0, status = 0;
2272 switch (ctlr->chip->cfg2) {
2275 /* read and acknowledge interrupt(s) */
2276 vector = ATA_INL(ctlr->r_res2, 0x040);
2278 /* read and clear interface status */
2279 status = ATA_INL(ctlr->r_res2, 0x06c);
2280 ATA_OUTL(ctlr->r_res2, 0x06c, status & 0x000000ff);
2285 /* read and acknowledge interrupt(s) */
2286 vector = ATA_INL(ctlr->r_res2, 0x040);
2287 ATA_OUTL(ctlr->r_res2, 0x040, vector & 0x0000ffff);
2289 /* read and clear interface status */
2290 status = ATA_INL(ctlr->r_res2, 0x060);
2291 ATA_OUTL(ctlr->r_res2, 0x060, status & 0x000000ff);
2295 for (unit = 0; unit < ctlr->channels; unit++) {
2297 if ((ch = ctlr->interrupt[unit].argument)) {
2298 struct ata_connect_task *tp;
2300 /* check for and handle disconnect events */
2301 if ((status & (0x00000001 << unit)) &&
2302 (tp = (struct ata_connect_task *)
2303 malloc(sizeof(struct ata_connect_task),
2304 M_ATA, M_NOWAIT | M_ZERO))) {
2307 device_printf(ch->dev, "DISCONNECT requested\n");
2308 tp->action = ATA_C_DETACH;
2310 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2311 taskqueue_enqueue(taskqueue_thread, &tp->task);
2314 /* check for and handle connect events */
2315 if ((status & (0x00000010 << unit)) &&
2316 (tp = (struct ata_connect_task *)
2317 malloc(sizeof(struct ata_connect_task),
2318 M_ATA, M_NOWAIT | M_ZERO))) {
2321 device_printf(ch->dev, "CONNECT requested\n");
2322 tp->action = ATA_C_ATTACH;
2324 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2325 taskqueue_enqueue(taskqueue_thread, &tp->task);
2328 /* active interrupt(s) need to call the interrupt handler */
2329 if (vector & (1 << (unit + 1)))
2330 if ((ch = ctlr->interrupt[unit].argument))
2331 ctlr->interrupt[unit].function(ch);
2337 ata_promise_sx4_intr(void *data)
2339 struct ata_pci_controller *ctlr = data;
2340 struct ata_channel *ch;
2341 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
2344 for (unit = 0; unit < ctlr->channels; unit++) {
2345 if (vector & (1 << (unit + 1)))
2346 if ((ch = ctlr->interrupt[unit].argument))
2347 ctlr->interrupt[unit].function(ch);
2348 if (vector & (1 << (unit + 5)))
2349 if ((ch = ctlr->interrupt[unit].argument))
2350 ata_promise_queue_hpkt(ctlr,
2351 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
2352 ATA_PDC_HPKT_OFFSET));
2353 if (vector & (1 << (unit + 9))) {
2354 ata_promise_next_hpkt(ctlr);
2355 if ((ch = ctlr->interrupt[unit].argument))
2356 ctlr->interrupt[unit].function(ch);
2358 if (vector & (1 << (unit + 13))) {
2359 ata_promise_next_hpkt(ctlr);
2360 if ((ch = ctlr->interrupt[unit].argument))
2361 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2362 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
2363 ATA_PDC_APKT_OFFSET));
2369 ata_promise_mio_dmainit(device_t dev)
2371 struct ata_channel *ch = device_get_softc(dev);
2375 /* note start and stop are not used here */
2380 ata_promise_mio_reset(device_t dev)
2382 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2383 struct ata_channel *ch = device_get_softc(dev);
2384 struct ata_promise_sx4 *hpktp;
2386 switch (ctlr->chip->cfg2) {
2389 /* softreset channel ATA module */
2390 hpktp = device_get_ivars(ctlr->dev);
2391 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
2393 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
2394 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
2395 ~0x00003f9f) | (ch->unit + 1));
2397 /* softreset HOST module */ /* XXX SOS what about other outstandings */
2398 mtx_lock(&hpktp->mtx);
2399 ATA_OUTL(ctlr->r_res2, 0xc012c,
2400 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
2402 ATA_OUTL(ctlr->r_res2, 0xc012c,
2403 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
2405 mtx_unlock(&hpktp->mtx);
2406 ata_generic_reset(dev);
2411 if ((ctlr->chip->cfg2 == PRSATA) ||
2412 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2414 /* mask plug/unplug intr */
2415 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
2418 /* softreset channels ATA module */
2419 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2421 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2422 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2423 ~0x00003f9f) | (ch->unit + 1));
2425 if ((ctlr->chip->cfg2 == PRSATA) ||
2426 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2428 ata_sata_phy_enable(ch);
2430 /* reset and enable plug/unplug intr */
2431 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
2434 ata_generic_reset(dev);
2439 if ((ctlr->chip->cfg2 == PRSATA2) ||
2440 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2441 /* set portmultiplier port */
2442 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
2444 /* mask plug/unplug intr */
2445 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
2448 /* softreset channels ATA module */
2449 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2451 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2452 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2453 ~0x00003f9f) | (ch->unit + 1));
2455 if ((ctlr->chip->cfg2 == PRSATA2) ||
2456 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2458 /* set PHY mode to "improved" */
2459 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
2460 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
2461 ~0x00000003) | 0x00000001);
2463 ata_sata_phy_enable(ch);
2465 /* reset and enable plug/unplug intr */
2466 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
2468 /* set portmultiplier port */
2469 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
2472 ata_generic_reset(dev);
2479 ata_promise_mio_command(struct ata_request *request)
2481 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2482 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2483 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
2485 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
2487 /* XXX SOS add ATAPI commands support later */
2488 switch (request->u.ata.command) {
2490 return ata_generic_command(request);
2493 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
2497 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
2500 wordp[1] = htole32(ch->dma->sg_bus);
2502 ata_promise_apkt((u_int8_t*)wordp, request);
2504 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
2509 ata_promise_sx4_command(struct ata_request *request)
2511 device_t gparent = GRANDPARENT(request->dev);
2512 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2513 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2514 struct ata_dma_prdentry *prd = ch->dma->sg;
2515 caddr_t window = rman_get_virtual(ctlr->r_res1);
2517 int i, idx, length = 0;
2519 /* XXX SOS add ATAPI commands support later */
2520 switch (request->u.ata.command) {
2525 case ATA_ATA_IDENTIFY:
2530 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2531 return ata_generic_command(request);
2533 case ATA_SETFEATURES:
2534 case ATA_FLUSHCACHE:
2537 wordp = (u_int32_t *)
2538 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2539 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
2542 ata_promise_apkt((u_int8_t *)wordp, request);
2543 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2544 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2545 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2546 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
2551 wordp = (u_int32_t *)
2552 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
2555 wordp[idx++] = prd[i].addr;
2556 wordp[idx++] = prd[i].count;
2557 length += (prd[i].count & ~ATA_DMA_EOT);
2558 } while (!(prd[i++].count & ATA_DMA_EOT));
2560 wordp = (u_int32_t *)
2561 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
2562 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2563 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
2565 wordp = (u_int32_t *)
2566 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
2567 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2568 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
2570 wordp = (u_int32_t *)
2571 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
2572 if (request->flags & ATA_R_READ)
2573 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
2574 if (request->flags & ATA_R_WRITE)
2575 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
2576 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
2577 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
2580 wordp = (u_int32_t *)
2581 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2582 if (request->flags & ATA_R_READ)
2583 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
2584 if (request->flags & ATA_R_WRITE)
2585 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
2586 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
2588 ata_promise_apkt((u_int8_t *)wordp, request);
2589 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2591 if (request->flags & ATA_R_READ) {
2592 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
2593 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
2594 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2595 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
2597 if (request->flags & ATA_R_WRITE) {
2598 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
2599 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
2600 ata_promise_queue_hpkt(ctlr,
2601 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
2608 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
2610 struct ata_device *atadev = device_get_softc(request->dev);
2614 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
2615 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
2616 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
2617 bytep[i++] = ATA_A_4BIT;
2619 command = ata_modify_if_48bit(request);
2621 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2622 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
2623 bytep[i++] = request->u.ata.feature >> 8;
2624 bytep[i++] = request->u.ata.feature;
2625 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
2626 bytep[i++] = request->u.ata.count >> 8;
2627 bytep[i++] = request->u.ata.count;
2628 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
2629 bytep[i++] = request->u.ata.lba >> 24;
2630 bytep[i++] = request->u.ata.lba;
2631 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
2632 bytep[i++] = request->u.ata.lba >> 32;
2633 bytep[i++] = request->u.ata.lba >> 8;
2634 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
2635 bytep[i++] = request->u.ata.lba >> 40;
2636 bytep[i++] = request->u.ata.lba >> 16;
2637 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
2638 bytep[i++] = ATA_D_LBA | atadev->unit;
2641 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
2642 bytep[i++] = request->u.ata.feature;
2643 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
2644 bytep[i++] = request->u.ata.count;
2645 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
2646 bytep[i++] = request->u.ata.lba;
2647 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
2648 bytep[i++] = request->u.ata.lba >> 8;
2649 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
2650 bytep[i++] = request->u.ata.lba >> 16;
2651 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
2652 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
2653 ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
2655 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
2656 bytep[i++] = command;
2661 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
2663 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
2665 mtx_lock(&hpktp->mtx);
2667 struct host_packet *hp =
2668 malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
2670 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
2674 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
2676 mtx_unlock(&hpktp->mtx);
2680 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
2682 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
2683 struct host_packet *hp;
2685 mtx_lock(&hpktp->mtx);
2686 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
2687 TAILQ_REMOVE(&hpktp->queue, hp, chain);
2688 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
2693 mtx_unlock(&hpktp->mtx);
2697 ata_promise_tx2_intr(void *data)
2699 struct ata_pci_controller *ctlr = data;
2700 struct ata_channel *ch;
2703 for (unit = 0; unit < ctlr->channels; unit++) {
2704 if (!(ch = ctlr->interrupt[unit].argument))
2706 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
2707 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
2708 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2709 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2711 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2712 ATA_BMSTAT_INTERRUPT)
2714 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2717 ctlr->interrupt[unit].function(ch);
2723 ata_promise_old_intr(void *data)
2725 struct ata_pci_controller *ctlr = data;
2726 struct ata_channel *ch;
2729 for (unit = 0; unit < ctlr->channels; unit++) {
2730 if (!(ch = ctlr->interrupt[unit].argument))
2732 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)){
2733 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2734 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2736 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2737 ATA_BMSTAT_INTERRUPT)
2739 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2742 ctlr->interrupt[unit].function(ch);
2748 ata_promise_new_dmastart(device_t dev)
2750 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2751 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2752 struct ata_device *atadev = device_get_softc(dev);
2754 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2755 ATA_OUTB(ctlr->r_res1, 0x11,
2756 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
2757 ATA_OUTL(ctlr->r_res1, 0x20,
2758 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
2759 (ch->dma->cur_iosize >> 1));
2761 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
2762 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
2763 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
2764 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2765 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
2766 ATA_BMCMD_START_STOP);
2767 ch->flags |= ATA_DMA_ACTIVE;
2772 ata_promise_new_dmastop(device_t dev)
2774 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2775 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2776 struct ata_device *atadev = device_get_softc(dev);
2779 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2780 ATA_OUTB(ctlr->r_res1, 0x11,
2781 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
2782 ATA_OUTL(ctlr->r_res1, 0x20, 0);
2784 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
2785 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2786 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
2787 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
2788 ch->flags &= ~ATA_DMA_ACTIVE;
2793 ata_promise_new_dmareset(device_t dev)
2795 struct ata_channel *ch = device_get_softc(dev);
2797 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2798 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
2799 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
2800 ch->flags &= ~ATA_DMA_ACTIVE;
2804 ata_promise_new_dmainit(device_t dev)
2806 struct ata_channel *ch = device_get_softc(dev);
2810 ch->dma->start = ata_promise_new_dmastart;
2811 ch->dma->stop = ata_promise_new_dmastop;
2812 ch->dma->reset = ata_promise_new_dmareset;
2817 ata_promise_setmode(device_t dev, int mode)
2819 device_t gparent = GRANDPARENT(dev);
2820 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2821 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2822 struct ata_device *atadev = device_get_softc(dev);
2823 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2825 u_int32_t timings33[][2] = {
2826 /* PROLD PRNEW mode */
2827 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
2828 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
2829 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
2830 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
2831 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
2832 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
2833 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
2834 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
2835 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
2836 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
2837 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
2838 { 0, 0x00424ef6 }, /* UDMA 3 */
2839 { 0, 0x004127f3 }, /* UDMA 4 */
2840 { 0, 0x004127f3 } /* UDMA 5 */
2843 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2845 switch (ctlr->chip->cfg1) {
2848 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
2849 (ch->unit ? 1 << 11 : 1 << 10))) {
2850 ata_print_cable(dev, "controller");
2853 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
2854 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
2858 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
2859 if (mode > ATA_UDMA2 &&
2860 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
2861 ata_print_cable(dev, "controller");
2867 if (mode > ATA_UDMA2 &&
2868 (ATA_INL(ctlr->r_res2,
2869 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
2870 (ch->unit << 7)) & 0x01000000)) {
2871 ata_print_cable(dev, "controller");
2877 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2880 device_printf(dev, "%ssetting %s on %s chip\n",
2881 (error) ? "FAILURE " : "",
2882 ata_mode2str(mode), ctlr->chip->text);
2884 if (ctlr->chip->cfg1 < PRTX)
2885 pci_write_config(gparent, 0x60 + (devno << 2),
2886 timings33[ctlr->chip->cfg1][ata_mode2idx(mode)],4);
2887 atadev->mode = mode;
2894 * ServerWorks chipset support functions
2897 ata_serverworks_ident(device_t dev)
2899 struct ata_pci_controller *ctlr = device_get_softc(dev);
2900 struct ata_chip_id *idx;
2901 static struct ata_chip_id ids[] =
2902 {{ ATA_ROSB4, 0x00, SWKS33, 0x00, ATA_UDMA2, "ServerWorks ROSB4" },
2903 { ATA_CSB5, 0x92, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB5" },
2904 { ATA_CSB5, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB5" },
2905 { ATA_CSB6, 0x00, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB6" },
2906 { ATA_CSB6_1, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB6" },
2907 { 0, 0, 0, 0, 0, 0}};
2910 if (!(idx = ata_match_chip(dev, ids)))
2913 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
2914 device_set_desc_copy(dev, buffer);
2916 ctlr->chipinit = ata_serverworks_chipinit;
2921 ata_serverworks_chipinit(device_t dev)
2923 struct ata_pci_controller *ctlr = device_get_softc(dev);
2925 if (ata_setup_interrupt(dev))
2928 if (ctlr->chip->cfg1 == SWKS33) {
2932 /* locate the ISA part in the southbridge and enable UDMA33 */
2933 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
2934 for (i = 0; i < nchildren; i++) {
2935 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
2936 pci_write_config(children[i], 0x64,
2937 (pci_read_config(children[i], 0x64, 4) &
2938 ~0x00002000) | 0x00004000, 4);
2942 free(children, M_TEMP);
2946 pci_write_config(dev, 0x5a,
2947 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
2948 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
2950 ctlr->setmode = ata_serverworks_setmode;
2955 ata_serverworks_setmode(device_t dev, int mode)
2957 device_t gparent = GRANDPARENT(dev);
2958 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2959 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2960 struct ata_device *atadev = device_get_softc(dev);
2961 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2962 int offset = (devno ^ 0x01) << 3;
2964 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
2965 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
2966 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
2968 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2970 mode = ata_check_80pin(dev, mode);
2972 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2975 device_printf(dev, "%ssetting %s on %s chip\n",
2976 (error) ? "FAILURE " : "",
2977 ata_mode2str(mode), ctlr->chip->text);
2979 if (mode >= ATA_UDMA0) {
2980 pci_write_config(gparent, 0x56,
2981 (pci_read_config(gparent, 0x56, 2) &
2982 ~(0xf << (devno << 2))) |
2983 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
2984 pci_write_config(gparent, 0x54,
2985 pci_read_config(gparent, 0x54, 1) |
2986 (0x01 << devno), 1);
2987 pci_write_config(gparent, 0x44,
2988 (pci_read_config(gparent, 0x44, 4) &
2989 ~(0xff << offset)) |
2990 (dmatimings[2] << offset), 4);
2992 else if (mode >= ATA_WDMA0) {
2993 pci_write_config(gparent, 0x54,
2994 pci_read_config(gparent, 0x54, 1) &
2995 ~(0x01 << devno), 1);
2996 pci_write_config(gparent, 0x44,
2997 (pci_read_config(gparent, 0x44, 4) &
2998 ~(0xff << offset)) |
2999 (dmatimings[mode & ATA_MODE_MASK] << offset),4);
3002 pci_write_config(gparent, 0x54,
3003 pci_read_config(gparent, 0x54, 1) &
3004 ~(0x01 << devno), 1);
3006 pci_write_config(gparent, 0x40,
3007 (pci_read_config(gparent, 0x40, 4) &
3008 ~(0xff << offset)) |
3009 (piotimings[ata_mode2idx(mode)] << offset), 4);
3010 atadev->mode = mode;
3016 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
3019 ata_sii_ident(device_t dev)
3021 struct ata_pci_controller *ctlr = device_get_softc(dev);
3022 struct ata_chip_id *idx;
3023 static struct ata_chip_id ids[] =
3024 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
3025 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
3026 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
3027 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
3028 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
3029 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
3030 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
3031 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
3032 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
3033 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
3034 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
3035 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
3036 { 0, 0, 0, 0, 0, 0}};
3039 if (!(idx = ata_match_chip(dev, ids)))
3042 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3043 device_set_desc_copy(dev, buffer);
3045 ctlr->chipinit = ata_sii_chipinit;
3050 ata_sii_chipinit(device_t dev)
3052 struct ata_pci_controller *ctlr = device_get_softc(dev);
3053 int rid = ATA_IRQ_RID;
3055 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3056 RF_SHAREABLE | RF_ACTIVE))) {
3057 device_printf(dev, "unable to map interrupt\n");
3061 if (ctlr->chip->cfg1 == SIIMEMIO) {
3062 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3063 ata_sii_intr, ctlr, &ctlr->handle))) {
3064 device_printf(dev, "unable to setup interrupt\n");
3068 ctlr->r_type2 = SYS_RES_MEMORY;
3069 ctlr->r_rid2 = PCIR_BAR(5);
3070 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3071 &ctlr->r_rid2, RF_ACTIVE)))
3074 if (ctlr->chip->cfg2 & SIISETCLK) {
3075 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
3076 pci_write_config(dev, 0x8a,
3077 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
3078 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
3079 device_printf(dev, "%s could not set ATA133 clock\n",
3083 /* if we have 4 channels enable the second set */
3084 if (ctlr->chip->cfg2 & SII4CH) {
3085 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
3089 /* enable PCI interrupt as BIOS might not */
3090 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
3092 /* dont block interrupts from any channel */
3093 pci_write_config(dev, 0x48,
3094 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
3096 ctlr->allocate = ata_sii_allocate;
3097 if (ctlr->chip->max_dma >= ATA_SA150) {
3098 ctlr->reset = ata_sii_reset;
3099 ctlr->setmode = ata_sata_setmode;
3102 ctlr->setmode = ata_sii_setmode;
3105 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3106 ctlr->chip->cfg2 & SIIINTR ?
3107 ata_cmd_intr : ata_cmd_old_intr,
3108 ctlr, &ctlr->handle))) {
3109 device_printf(dev, "unable to setup interrupt\n");
3113 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
3114 device_printf(dev, "HW has secondary channel disabled\n");
3118 /* enable interrupt as BIOS might not */
3119 pci_write_config(dev, 0x71, 0x01, 1);
3121 ctlr->setmode = ata_cmd_setmode;
3127 ata_sii_allocate(device_t dev)
3129 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3130 struct ata_channel *ch = device_get_softc(dev);
3131 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
3134 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
3135 ch->r_io[i].res = ctlr->r_res2;
3136 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
3138 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
3139 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
3140 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
3141 ata_default_registers(dev);
3142 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
3143 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
3144 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
3145 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
3146 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
3147 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
3148 ch->r_io[ATA_BMDEVSPEC_0].res = ctlr->r_res2;
3149 ch->r_io[ATA_BMDEVSPEC_0].offset = 0xa1 + (unit01 << 6) + (unit10 << 8);
3151 if (ctlr->chip->max_dma >= ATA_SA150) {
3152 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3153 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
3154 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3155 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
3156 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3157 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
3158 ch->flags |= ATA_NO_SLAVE;
3160 /* enable PHY state change interrupt */
3161 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
3164 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
3165 /* work around errata in early chips */
3166 ch->dma->boundary = 16 * DEV_BSIZE;
3167 ch->dma->max_iosize = 15 * DEV_BSIZE;
3170 ata_generic_hw(dev);
3175 ata_sii_intr(void *data)
3177 struct ata_pci_controller *ctlr = data;
3178 struct ata_channel *ch;
3181 for (unit = 0; unit < ctlr->channels; unit++) {
3182 if (!(ch = ctlr->interrupt[unit].argument))
3185 /* check for PHY related interrupts on SATA capable HW */
3186 if (ctlr->chip->max_dma >= ATA_SA150) {
3187 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
3188 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
3189 struct ata_connect_task *tp;
3192 /* clear error bits/interrupt */
3193 ATA_IDX_OUTL(ch, ATA_SERROR, error);
3195 /* if we have a connection event deal with it */
3196 if ((error & ATA_SE_PHY_CHANGED) &&
3197 (tp = (struct ata_connect_task *)
3198 malloc(sizeof(struct ata_connect_task),
3199 M_ATA, M_NOWAIT | M_ZERO))) {
3201 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
3202 device_printf(ch->dev, "CONNECT requested\n");
3203 tp->action = ATA_C_ATTACH;
3206 device_printf(ch->dev, "DISCONNECT requested\n");
3207 tp->action = ATA_C_DETACH;
3210 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3211 taskqueue_enqueue(taskqueue_thread, &tp->task);
3216 /* any drive action to take care of ? */
3217 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_0) & 0x08) {
3218 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3219 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3221 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
3223 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3226 ctlr->interrupt[unit].function(ch);
3233 ata_cmd_intr(void *data)
3235 struct ata_pci_controller *ctlr = data;
3236 struct ata_channel *ch;
3240 for (unit = 0; unit < ctlr->channels; unit++) {
3241 if (!(ch = ctlr->interrupt[unit].argument))
3243 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
3244 (ch->unit ? 0x08 : 0x04))) {
3245 pci_write_config(device_get_parent(ch->dev), 0x71,
3246 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
3247 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3248 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3250 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3251 ATA_BMSTAT_INTERRUPT)
3253 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3256 ctlr->interrupt[unit].function(ch);
3262 ata_cmd_old_intr(void *data)
3264 struct ata_pci_controller *ctlr = data;
3265 struct ata_channel *ch;
3268 for (unit = 0; unit < ctlr->channels; unit++) {
3269 if (!(ch = ctlr->interrupt[unit].argument))
3271 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3272 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3274 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3275 ATA_BMSTAT_INTERRUPT)
3277 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3280 ctlr->interrupt[unit].function(ch);
3285 ata_sii_reset(device_t dev)
3287 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3288 struct ata_channel *ch = device_get_softc(dev);
3289 int offset = ((ch->unit & 1) << 7) + ((ch->unit & 2) << 8);
3291 /* disable PHY state change interrupt */
3292 ATA_OUTL(ctlr->r_res2, 0x148 + offset, ~(1 << 16));
3294 ata_sata_phy_enable(ch);
3296 /* enable PHY state change interrupt */
3297 ATA_OUTL(ctlr->r_res2, 0x148 + offset, (1 << 16));
3301 ata_sii_setmode(device_t dev, int mode)
3303 device_t gparent = GRANDPARENT(dev);
3304 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3305 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3306 struct ata_device *atadev = device_get_softc(dev);
3307 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
3308 int mreg = ch->unit ? 0x84 : 0x80;
3309 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
3310 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
3313 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3315 if (ctlr->chip->cfg2 & SIISETCLK) {
3316 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
3317 (ch->unit ? 0x02 : 0x01))) {
3318 ata_print_cable(dev, "controller");
3323 mode = ata_check_80pin(dev, mode);
3325 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3328 device_printf(dev, "%ssetting %s on %s chip\n",
3329 (error) ? "FAILURE " : "",
3330 ata_mode2str(mode), ctlr->chip->text);
3334 if (mode >= ATA_UDMA0) {
3335 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
3336 u_int8_t ureg = 0xac + rego;
3338 pci_write_config(gparent, mreg,
3339 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
3340 pci_write_config(gparent, ureg,
3341 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
3342 udmatimings[mode & ATA_MODE_MASK], 1);
3345 else if (mode >= ATA_WDMA0) {
3346 u_int8_t dreg = 0xa8 + rego;
3347 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
3349 pci_write_config(gparent, mreg,
3350 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
3351 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
3355 u_int8_t preg = 0xa4 + rego;
3356 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
3358 pci_write_config(gparent, mreg,
3359 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
3360 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
3362 atadev->mode = mode;
3366 ata_cmd_setmode(device_t dev, int mode)
3368 device_t gparent = GRANDPARENT(dev);
3369 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3370 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3371 struct ata_device *atadev = device_get_softc(dev);
3372 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3375 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3377 mode = ata_check_80pin(dev, mode);
3379 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3382 device_printf(dev, "%ssetting %s on %s chip\n",
3383 (error) ? "FAILURE " : "",
3384 ata_mode2str(mode), ctlr->chip->text);
3386 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
3387 int ureg = ch->unit ? 0x7b : 0x73;
3389 if (mode >= ATA_UDMA0) {
3390 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
3391 { 0x11, 0x42 }, { 0x25, 0x8a },
3392 { 0x15, 0x4a }, { 0x05, 0x0a } };
3394 u_int8_t umode = pci_read_config(gparent, ureg, 1);
3396 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
3397 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
3398 pci_write_config(gparent, ureg, umode, 1);
3400 else if (mode >= ATA_WDMA0) {
3401 int dmatimings[] = { 0x87, 0x32, 0x3f };
3403 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
3404 pci_write_config(gparent, ureg,
3405 pci_read_config(gparent, ureg, 1) &
3406 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3409 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
3410 pci_write_config(gparent, treg,
3411 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
3412 pci_write_config(gparent, ureg,
3413 pci_read_config(gparent, ureg, 1) &
3414 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3416 atadev->mode = mode;
3422 * Silicon Integrated Systems Corp. (SiS) chipset support functions
3425 ata_sis_ident(device_t dev)
3427 struct ata_pci_controller *ctlr = device_get_softc(dev);
3428 struct ata_chip_id *idx;
3429 static struct ata_chip_id ids[] =
3430 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "SiS 182" }, /* south */
3431 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "SiS 181" }, /* south */
3432 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "SiS 180" }, /* south */
3433 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 965" }, /* south */
3434 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 964" }, /* south */
3435 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 963" }, /* south */
3436 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 962" }, /* south */
3438 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 745" }, /* 1chip */
3439 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 735" }, /* 1chip */
3440 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 733" }, /* 1chip */
3441 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "SiS 730" }, /* 1chip */
3443 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 635" }, /* 1chip */
3444 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 633" }, /* unknown */
3445 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "SiS 630S"}, /* 1chip */
3446 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "SiS 630" }, /* 1chip */
3447 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "SiS 620" }, /* 1chip */
3449 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "SiS 550" },
3450 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "SiS 540" },
3451 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "SiS 530" },
3453 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "SiS 5513" },
3454 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "SiS 5513" },
3455 { 0, 0, 0, 0, 0, 0 }};
3459 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
3462 if (idx->cfg2 && !found) {
3463 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
3465 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
3466 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
3468 idx->cfg1 = SIS133NEW;
3469 idx->max_dma = ATA_UDMA6;
3470 sprintf(buffer, "SiS 962/963 %s controller",
3471 ata_mode2str(idx->max_dma));
3473 pci_write_config(dev, 0x57, reg57, 1);
3475 if (idx->cfg2 && !found) {
3476 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
3478 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
3479 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
3480 struct ata_chip_id id[] =
3481 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
3484 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
3485 idx->cfg1 = SIS133OLD;
3486 idx->max_dma = ATA_UDMA6;
3489 idx->cfg1 = SIS100NEW;
3490 idx->max_dma = ATA_UDMA5;
3492 sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
3494 pci_write_config(dev, 0x4a, reg4a, 1);
3497 sprintf(buffer,"%s %s controller",idx->text,ata_mode2str(idx->max_dma));
3499 device_set_desc_copy(dev, buffer);
3501 ctlr->chipinit = ata_sis_chipinit;
3506 ata_sis_chipinit(device_t dev)
3508 struct ata_pci_controller *ctlr = device_get_softc(dev);
3510 if (ata_setup_interrupt(dev))
3513 switch (ctlr->chip->cfg1) {
3518 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
3522 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
3525 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
3526 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
3529 ctlr->r_type2 = SYS_RES_IOPORT;
3530 ctlr->r_rid2 = PCIR_BAR(5);
3531 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3532 &ctlr->r_rid2, RF_ACTIVE))) {
3533 pci_write_config(dev, PCIR_COMMAND,
3534 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3535 ctlr->allocate = ata_sis_allocate;
3536 ctlr->reset = ata_sis_reset;
3538 ctlr->setmode = ata_sata_setmode;
3543 ctlr->setmode = ata_sis_setmode;
3548 ata_sis_allocate(device_t dev)
3550 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3551 struct ata_channel *ch = device_get_softc(dev);
3553 /* setup the usual register normal pci style */
3554 ata_pci_allocate(dev);
3556 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3557 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 4);
3558 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3559 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 4);
3560 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3561 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 4);
3562 ch->flags |= ATA_NO_SLAVE;
3564 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
3565 /* XXX SOS unknown how to enable PHY state change interrupt */
3570 ata_sis_reset(device_t dev)
3572 struct ata_channel *ch = device_get_softc(dev);
3574 ata_sata_phy_enable(ch);
3579 ata_sis_setmode(device_t dev, int mode)
3581 device_t gparent = GRANDPARENT(dev);
3582 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3583 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3584 struct ata_device *atadev = device_get_softc(dev);
3585 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3588 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3590 if (ctlr->chip->cfg1 == SIS133NEW) {
3591 if (mode > ATA_UDMA2 &&
3592 pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
3593 ata_print_cable(dev, "controller");
3598 if (mode > ATA_UDMA2 &&
3599 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
3600 ata_print_cable(dev, "controller");
3605 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3608 device_printf(dev, "%ssetting %s on %s chip\n",
3609 (error) ? "FAILURE " : "",
3610 ata_mode2str(mode), ctlr->chip->text);
3612 switch (ctlr->chip->cfg1) {
3614 u_int32_t timings[] =
3615 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
3616 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
3617 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
3620 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
3621 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
3625 u_int16_t timings[] =
3626 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
3627 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
3629 u_int16_t reg = 0x40 + (devno << 1);
3631 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3635 u_int16_t timings[] =
3636 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
3637 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
3638 u_int16_t reg = 0x40 + (devno << 1);
3640 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3646 u_int16_t timings[] =
3647 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
3648 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
3649 u_int16_t reg = 0x40 + (devno << 1);
3651 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3655 atadev->mode = mode;
3660 /* VIA Technologies Inc. chipset support functions */
3662 ata_via_ident(device_t dev)
3664 struct ata_pci_controller *ctlr = device_get_softc(dev);
3665 struct ata_chip_id *idx;
3666 static struct ata_chip_id ids[] =
3667 {{ ATA_VIA82C586, 0x02, VIA33, 0x00, ATA_UDMA2, "VIA 82C586B" },
3668 { ATA_VIA82C586, 0x00, VIA33, 0x00, ATA_WDMA2, "VIA 82C586" },
3669 { ATA_VIA82C596, 0x12, VIA66, VIACLK, ATA_UDMA4, "VIA 82C596B" },
3670 { ATA_VIA82C596, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C596" },
3671 { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "VIA 82C686B"},
3672 { ATA_VIA82C686, 0x10, VIA66, VIACLK, ATA_UDMA4, "VIA 82C686A" },
3673 { ATA_VIA82C686, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C686" },
3674 { ATA_VIA8231, 0x00, VIA100, VIABUG, ATA_UDMA5, "VIA 8231" },
3675 { ATA_VIA8233, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233" },
3676 { ATA_VIA8233C, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233C" },
3677 { ATA_VIA8233A, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8233A" },
3678 { ATA_VIA8235, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8235" },
3679 { ATA_VIA8237, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8237" },
3680 { 0, 0, 0, 0, 0, 0 }};
3681 static struct ata_chip_id new_ids[] =
3682 {{ ATA_VIA6410, 0x00, 0, 0x00, ATA_UDMA6, "VIA 6410" },
3683 { ATA_VIA6420, 0x00, 7, 0x00, ATA_SA150, "VIA 6420" },
3684 { ATA_VIA6421, 0x00, 6, 0x00, ATA_SA150, "VIA 6421" },
3685 { 0, 0, 0, 0, 0, 0 }};
3688 if (pci_get_devid(dev) == ATA_VIA82C571) {
3689 if (!(idx = ata_find_chip(dev, ids, -99)))
3693 if (!(idx = ata_match_chip(dev, new_ids)))
3697 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3698 device_set_desc_copy(dev, buffer);
3700 ctlr->chipinit = ata_via_chipinit;
3705 ata_via_chipinit(device_t dev)
3707 struct ata_pci_controller *ctlr = device_get_softc(dev);
3709 if (ata_setup_interrupt(dev))
3712 if (ctlr->chip->max_dma >= ATA_SA150) {
3713 ctlr->r_type2 = SYS_RES_IOPORT;
3714 ctlr->r_rid2 = PCIR_BAR(5);
3715 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3716 &ctlr->r_rid2, RF_ACTIVE))) {
3717 pci_write_config(dev, PCIR_COMMAND,
3718 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3719 ctlr->allocate = ata_via_allocate;
3720 ctlr->reset = ata_via_reset;
3722 ctlr->setmode = ata_sata_setmode;
3726 /* prepare for ATA-66 on the 82C686a and 82C596b */
3727 if (ctlr->chip->cfg2 & VIACLK)
3728 pci_write_config(dev, 0x50, 0x030b030b, 4);
3730 /* the southbridge might need the data corruption fix */
3731 if (ctlr->chip->cfg2 & VIABUG)
3732 ata_via_southbridge_fixup(dev);
3734 /* set fifo configuration half'n'half */
3735 pci_write_config(dev, 0x43,
3736 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
3738 /* set status register read retry */
3739 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
3741 /* set DMA read & end-of-sector fifo flush */
3742 pci_write_config(dev, 0x46,
3743 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
3745 /* set sector size */
3746 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
3747 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
3749 ctlr->setmode = ata_via_family_setmode;
3754 ata_via_allocate(device_t dev)
3756 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3757 struct ata_channel *ch = device_get_softc(dev);
3759 /* setup the usual register normal pci style */
3760 ata_pci_allocate(dev);
3762 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3763 ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
3764 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3765 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
3766 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3767 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
3768 ch->flags |= ATA_NO_SLAVE;
3770 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
3771 /* XXX SOS unknown how to enable PHY state change interrupt */
3776 ata_via_reset(device_t dev)
3778 struct ata_channel *ch = device_get_softc(dev);
3780 ata_sata_phy_enable(ch);
3784 ata_via_southbridge_fixup(device_t dev)
3789 if (device_get_children(device_get_parent(dev), &children, &nchildren))
3792 for (i = 0; i < nchildren; i++) {
3793 if (pci_get_devid(children[i]) == ATA_VIA8363 ||
3794 pci_get_devid(children[i]) == ATA_VIA8371 ||
3795 pci_get_devid(children[i]) == ATA_VIA8662 ||
3796 pci_get_devid(children[i]) == ATA_VIA8361) {
3797 u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
3799 if ((reg76 & 0xf0) != 0xd0) {
3801 "Correcting VIA config for southbridge data corruption bug\n");
3802 pci_write_config(children[i], 0x75, 0x80, 1);
3803 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
3808 free(children, M_TEMP);
3812 /* common code for VIA, AMD & nVidia */
3814 ata_via_family_setmode(device_t dev, int mode)
3816 device_t gparent = GRANDPARENT(dev);
3817 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3818 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3819 struct ata_device *atadev = device_get_softc(dev);
3820 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
3821 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
3823 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* VIA ATA33 */
3824 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* VIA ATA66 */
3825 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* VIA ATA100 */
3826 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
3827 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }}; /* AMD/nVIDIA */
3828 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3829 int reg = 0x53 - devno;
3832 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3834 if (ctlr->chip->cfg2 & AMDCABLE) {
3835 if (mode > ATA_UDMA2 &&
3836 !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
3837 ata_print_cable(dev, "controller");
3842 mode = ata_check_80pin(dev, mode);
3844 if (ctlr->chip->cfg2 & NVIDIA)
3847 if (ctlr->chip->cfg1 != VIA133)
3848 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
3850 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3853 device_printf(dev, "%ssetting %s on %s chip\n",
3854 (error) ? "FAILURE " : "", ata_mode2str(mode),
3857 if (mode >= ATA_UDMA0)
3858 pci_write_config(gparent, reg,
3859 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
3861 pci_write_config(gparent, reg, 0x8b, 1);
3862 atadev->mode = mode;
3867 /* misc functions */
3868 struct ata_chip_id *
3869 ata_match_chip(device_t dev, struct ata_chip_id *index)
3871 while (index->chipid != 0) {
3872 if (pci_get_devid(dev) == index->chipid &&
3873 pci_get_revid(dev) >= index->chiprev)
3880 static struct ata_chip_id *
3881 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
3886 if (device_get_children(device_get_parent(dev), &children, &nchildren))
3889 while (index->chipid != 0) {
3890 for (i = 0; i < nchildren; i++) {
3891 if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
3892 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
3893 pci_get_devid(children[i]) == index->chipid &&
3894 pci_get_revid(children[i]) >= index->chiprev) {
3895 free(children, M_TEMP);
3901 free(children, M_TEMP);
3906 ata_setup_interrupt(device_t dev)
3908 struct ata_pci_controller *ctlr = device_get_softc(dev);
3909 int rid = ATA_IRQ_RID;
3911 if (!ata_legacy(dev)) {
3912 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3913 RF_SHAREABLE | RF_ACTIVE))) {
3914 device_printf(dev, "unable to map interrupt\n");
3917 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3918 ata_generic_intr, ctlr, &ctlr->handle))) {
3919 device_printf(dev, "unable to setup interrupt\n");
3926 struct ata_serialize {
3927 struct mtx locked_mtx;
3933 ata_serialize(device_t dev, int flags)
3935 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3936 struct ata_channel *ch = device_get_softc(dev);
3937 struct ata_serialize *serial;
3938 static int inited = 0;
3942 serial = malloc(sizeof(struct ata_serialize),
3943 M_TEMP, M_NOWAIT | M_ZERO);
3944 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
3945 serial->locked_ch = -1;
3946 serial->restart_ch = -1;
3947 device_set_ivars(ctlr->dev, serial);
3951 serial = device_get_ivars(ctlr->dev);
3953 mtx_lock(&serial->locked_mtx);
3956 if (serial->locked_ch == -1)
3957 serial->locked_ch = ch->unit;
3958 if (serial->locked_ch != ch->unit)
3959 serial->restart_ch = ch->unit;
3963 if (serial->locked_ch == ch->unit) {
3964 serial->locked_ch = -1;
3965 if (serial->restart_ch != -1) {
3966 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
3967 serial->restart_ch = -1;
3968 mtx_unlock(&serial->locked_mtx);
3979 res = serial->locked_ch;
3980 mtx_unlock(&serial->locked_mtx);
3985 ata_print_cable(device_t dev, u_int8_t *who)
3988 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
3992 ata_atapi(device_t dev)
3994 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3995 struct ata_device *atadev = device_get_softc(dev);
3997 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
3998 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
4002 ata_check_80pin(device_t dev, int mode)
4004 struct ata_device *atadev = device_get_softc(dev);
4006 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
4007 ata_print_cable(dev, "device");
4014 ata_mode2idx(int mode)
4016 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
4017 return (mode & ATA_MODE_MASK) + 8;
4018 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
4019 return (mode & ATA_MODE_MASK) + 5;
4020 return (mode & ATA_MODE_MASK) - ATA_PIO0;