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 dev);
57 static void ata_generic_intr(void *data);
58 static void ata_generic_setmode(device_t dev, int mode);
59 static void ata_sata_setmode(device_t dev, int mode);
60 static int ata_sata_connect(struct ata_channel *ch);
61 static void ata_sata_phy_enable(struct ata_channel *ch);
62 static void ata_sata_phy_event(void *context, int dummy);
63 static int ata_ahci_allocate(device_t dev);
64 static int ata_ahci_setup_fis(u_int8_t *fis, struct ata_request *request);
65 static int ata_ahci_begin_transaction(struct ata_request *request);
66 static int ata_ahci_end_transaction(struct ata_request *request);
67 static void ata_ahci_intr(void *data);
68 static void ata_ahci_reset(device_t dev);
69 static void ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
70 static void ata_ahci_dmainit(device_t dev);
71 static int ata_acard_chipinit(device_t dev);
72 static void ata_acard_intr(void *data);
73 static void ata_acard_850_setmode(device_t dev, int mode);
74 static void ata_acard_86X_setmode(device_t dev, int mode);
75 static int ata_ali_chipinit(device_t dev);
76 static int ata_ali_allocate(device_t dev);
77 static void ata_ali_setmode(device_t dev, int mode);
78 static int ata_amd_chipinit(device_t dev);
79 static int ata_cyrix_chipinit(device_t dev);
80 static void ata_cyrix_setmode(device_t dev, int mode);
81 static int ata_cypress_chipinit(device_t dev);
82 static void ata_cypress_setmode(device_t dev, int mode);
83 static int ata_highpoint_chipinit(device_t dev);
84 static void ata_highpoint_intr(void *data);
85 static void ata_highpoint_setmode(device_t dev, int mode);
86 static int ata_highpoint_check_80pin(device_t dev, int mode);
87 static int ata_intel_chipinit(device_t dev);
88 static int ata_intel_31244_allocate(device_t dev);
89 static void ata_intel_31244_intr(void *data);
90 static void ata_intel_31244_reset(device_t dev);
91 static int ata_intel_31244_command(struct ata_request *request);
92 static void ata_intel_intr(void *data);
93 static void ata_intel_reset(device_t dev);
94 static void ata_intel_old_setmode(device_t dev, int mode);
95 static void ata_intel_new_setmode(device_t dev, int mode);
96 static int ata_ite_chipinit(device_t dev);
97 static void ata_ite_setmode(device_t dev, int mode);
98 static int ata_national_chipinit(device_t dev);
99 static void ata_national_setmode(device_t dev, int mode);
100 static int ata_nvidia_chipinit(device_t dev);
101 static int ata_nvidia_allocate(device_t dev);
102 static void ata_nvidia_intr(void *data);
103 static void ata_nvidia_reset(device_t dev);
104 static int ata_promise_chipinit(device_t dev);
105 static int ata_promise_mio_allocate(device_t dev);
106 static void ata_promise_mio_intr(void *data);
107 static void ata_promise_sx4_intr(void *data);
108 static void ata_promise_mio_dmainit(device_t dev);
109 static void ata_promise_mio_reset(device_t dev);
110 static int ata_promise_mio_command(struct ata_request *request);
111 static int ata_promise_sx4_command(struct ata_request *request);
112 static int ata_promise_apkt(u_int8_t *bytep, struct ata_request *request);
113 static void ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt);
114 static void ata_promise_next_hpkt(struct ata_pci_controller *ctlr);
115 static void ata_promise_tx2_intr(void *data);
116 static void ata_promise_old_intr(void *data);
117 static int ata_promise_new_dmastart(device_t dev);
118 static int ata_promise_new_dmastop(device_t dev);
119 static void ata_promise_new_dmareset(device_t dev);
120 static void ata_promise_new_dmainit(device_t dev);
121 static void ata_promise_setmode(device_t dev, int mode);
122 static int ata_serverworks_chipinit(device_t dev);
123 static void ata_serverworks_setmode(device_t dev, int mode);
124 static int ata_sii_chipinit(device_t dev);
125 static int ata_sii_allocate(device_t dev);
126 static void ata_sii_intr(void *data);
127 static void ata_cmd_intr(void *data);
128 static void ata_cmd_old_intr(void *data);
129 static void ata_sii_reset(device_t dev);
130 static void ata_sii_setmode(device_t dev, int mode);
131 static void ata_cmd_setmode(device_t dev, int mode);
132 static int ata_sis_chipinit(device_t dev);
133 static int ata_sis_allocate(device_t dev);
134 static void ata_sis_reset(device_t dev);
135 static void ata_sis_setmode(device_t dev, int mode);
136 static int ata_via_chipinit(device_t dev);
137 static int ata_via_allocate(device_t dev);
138 static void ata_via_reset(device_t dev);
139 static void ata_via_southbridge_fixup(device_t dev);
140 static void ata_via_family_setmode(device_t dev, int mode);
141 static struct ata_chip_id *ata_find_chip(device_t dev, struct ata_chip_id *index, int slot);
142 static int ata_setup_interrupt(device_t dev);
143 static int ata_serialize(device_t dev, int flags);
144 static void ata_print_cable(device_t dev, u_int8_t *who);
145 static int ata_atapi(device_t dev);
146 static int ata_check_80pin(device_t dev, int mode);
147 static int ata_mode2idx(int mode);
150 /* generic or unknown ATA chipset support functions */
152 ata_generic_ident(device_t dev)
154 struct ata_pci_controller *ctlr = device_get_softc(dev);
156 device_set_desc(dev, "GENERIC ATA controller");
157 ctlr->chipinit = ata_generic_chipinit;
162 ata_generic_chipinit(device_t dev)
164 struct ata_pci_controller *ctlr = device_get_softc(dev);
166 if (ata_setup_interrupt(dev))
168 ctlr->setmode = ata_generic_setmode;
173 ata_generic_intr(void *data)
175 struct ata_pci_controller *ctlr = data;
176 struct ata_channel *ch;
179 for (unit = 0; unit < ctlr->channels; unit++) {
180 if (!(ch = ctlr->interrupt[unit].argument))
182 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
183 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
185 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
186 ATA_BMSTAT_INTERRUPT)
188 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
191 ctlr->interrupt[unit].function(ch);
196 ata_generic_setmode(device_t dev, int mode)
198 struct ata_device *atadev = device_get_softc(dev);
200 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
201 mode = ata_check_80pin(dev, mode);
202 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
207 /* SATA support functions */
209 ata_sata_setmode(device_t dev, int mode)
211 struct ata_device *atadev = device_get_softc(dev);
214 * if we detect that the device isn't a real SATA device we limit
215 * the transfer mode to UDMA5/ATA100.
216 * this works around the problems some devices has with the
217 * Marvell 88SX8030 SATA->PATA converters and UDMA6/ATA133.
219 if (atadev->param.satacapabilities != 0x0000 &&
220 atadev->param.satacapabilities != 0xffff) {
221 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
222 ata_limit_mode(dev, mode, ATA_UDMA6)))
223 atadev->mode = ATA_SA150;
226 mode = ata_limit_mode(dev, mode, ATA_UDMA5);
227 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
233 ata_sata_connect(struct ata_channel *ch)
238 /* wait up to 1 second for "connect well" */
239 for (timeout = 0; timeout < 100 ; timeout++) {
240 status = ATA_IDX_INL(ch, ATA_SSTATUS);
241 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1 ||
242 (status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)
246 if (timeout >= 100) {
248 device_printf(ch->dev, "SATA connect status=%08x\n", status);
252 /* clear SATA error register */
253 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
255 /* find out what type device we got poll for spec'd 31 seconds */
256 /* XXX SOS 10 secs for now as I have little patience */
258 for (timeout = 0; timeout < 1000; timeout++) {
259 if (ATA_IDX_INB(ch, ATA_STATUS) & ATA_S_BUSY)
265 device_printf(ch->dev, "SATA connect ready time=%dms\n", timeout * 10);
266 if (timeout < 1000) {
267 if ((ATA_IDX_INB(ch, ATA_CYL_LSB) == ATAPI_MAGIC_LSB) &&
268 (ATA_IDX_INB(ch, ATA_CYL_MSB) == ATAPI_MAGIC_MSB))
269 ch->devices = ATA_ATAPI_MASTER;
271 ch->devices = ATA_ATA_MASTER;
274 device_printf(ch->dev, "sata_connect devices=0x%b\n",
275 ch->devices, "\20\3ATAPI_MASTER\1ATA_MASTER");
280 ata_sata_phy_enable(struct ata_channel *ch)
284 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE) {
285 ata_sata_connect(ch);
289 for (retry = 0; retry < 10; retry++) {
290 for (loop = 0; loop < 10; loop++) {
291 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_RESET);
293 if ((ATA_IDX_INL(ch, ATA_SCONTROL) &
294 ATA_SC_DET_MASK) == ATA_SC_DET_RESET)
298 for (loop = 0; loop < 10; loop++) {
299 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_IDLE);
301 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == 0) {
302 ata_sata_connect(ch);
310 ata_sata_phy_event(void *context, int dummy)
312 struct ata_connect_task *tp = (struct ata_connect_task *)context;
316 mtx_lock(&Giant); /* newbus suckage it needs Giant */
317 if (tp->action == ATA_C_ATTACH) {
318 struct ata_channel *ch = device_get_softc(tp->dev);
320 device_printf(tp->dev, "CONNECTED\n");
321 ata_sata_connect(ch);
322 ata_identify(tp->dev);
324 if (tp->action == ATA_C_DETACH) {
325 if (!device_get_children(tp->dev, &children, &nchildren)) {
326 for (i = 0; i < nchildren; i++)
328 device_delete_child(tp->dev, children[i]);
329 free(children, M_TEMP);
331 device_printf(tp->dev, "DISCONNECTED\n");
333 mtx_unlock(&Giant); /* suckage code dealt with, release Giant */
339 * AHCI v1.0 compliant SATA chipset support functions
341 struct ata_ahci_dma_prd {
344 u_int32_t dbc; /* 0 based */
345 #define ATA_AHCI_PRD_MASK 0x003fffff /* max 4MB */
346 #define ATA_AHCI_PRD_IPC (1<<31)
349 struct ata_ahci_cmd_tab {
352 u_int8_t reserved[32];
353 struct ata_ahci_dma_prd prd_tab[16];
356 struct ata_ahci_cmd_list {
358 u_int16_t prd_length; /* PRD entries */
360 u_int64_t cmd_table_phys; /* 128byte aligned */
365 ata_ahci_allocate(device_t dev)
367 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
368 struct ata_channel *ch = device_get_softc(dev);
369 int offset = (ch->unit << 7);
371 /* XXX SOS this is a hack to satisfy various legacy cruft */
372 ch->r_io[ATA_CYL_LSB].res = ctlr->r_res2;
373 ch->r_io[ATA_CYL_LSB].offset = ATA_AHCI_P_SIG + 1 + offset;
374 ch->r_io[ATA_CYL_LSB].res = ctlr->r_res2;
375 ch->r_io[ATA_CYL_MSB].offset = ATA_AHCI_P_SIG + 3 + offset;
376 ch->r_io[ATA_STATUS].res = ctlr->r_res2;
377 ch->r_io[ATA_STATUS].offset = ATA_AHCI_P_TFD + offset;
378 ch->r_io[ATA_ALTSTAT].res = ctlr->r_res2;
379 ch->r_io[ATA_ALTSTAT].offset = ATA_AHCI_P_TFD + offset;
381 /* set the SATA resources */
382 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
383 ch->r_io[ATA_SSTATUS].offset = ATA_AHCI_P_SSTS + offset;
384 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
385 ch->r_io[ATA_SERROR].offset = ATA_AHCI_P_SERR + offset;
386 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
387 ch->r_io[ATA_SCONTROL].offset = ATA_AHCI_P_SCTL + offset;
388 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
389 ch->r_io[ATA_SACTIVE].offset = ATA_AHCI_P_SACT + offset;
391 ch->hw.begin_transaction = ata_ahci_begin_transaction;
392 ch->hw.end_transaction = ata_ahci_end_transaction;
393 ch->hw.command = NULL; /* not used here */
395 /* setup the work areas */
396 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLB + offset,
397 ch->dma->work_bus + ATA_AHCI_CL_OFFSET);
398 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLBU + offset, 0x00000000);
400 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FB + offset,
401 ch->dma->work_bus + ATA_AHCI_FB_OFFSET);
402 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FBU + offset, 0x00000000);
404 /* enable wanted port interrupts */
405 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset,
406 (ATA_AHCI_P_IX_CPD | ATA_AHCI_P_IX_TFE | ATA_AHCI_P_IX_HBF |
407 ATA_AHCI_P_IX_HBD | ATA_AHCI_P_IX_IF | ATA_AHCI_P_IX_OF |
408 ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC | ATA_AHCI_P_IX_DP |
409 ATA_AHCI_P_IX_UF | ATA_AHCI_P_IX_SDB | ATA_AHCI_P_IX_DS |
410 ATA_AHCI_P_IX_PS | ATA_AHCI_P_IX_DHR));
412 /* start operations on this channel */
413 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
414 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
415 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
420 ata_ahci_setup_fis(u_int8_t *fis, struct ata_request *request)
422 struct ata_device *atadev = device_get_softc(request->dev);
425 /* XXX SOS add ATAPI commands support later */
426 fis[idx++] = 0x27; /* host to device */
427 fis[idx++] = 0x80; /* command FIS (note PM goes here) */
428 fis[idx++] = ata_modify_if_48bit(request);
429 fis[idx++] = request->u.ata.feature;
431 fis[idx++] = request->u.ata.lba;
432 fis[idx++] = request->u.ata.lba >> 8;
433 fis[idx++] = request->u.ata.lba >> 16;
434 fis[idx] = ATA_D_LBA | atadev->unit;
435 if (atadev->flags & ATA_D_48BIT_ACTIVE)
438 fis[idx++] |= (request->u.ata.lba >> 24 & 0x0f);
440 fis[idx++] = request->u.ata.lba >> 24;
441 fis[idx++] = request->u.ata.lba >> 32;
442 fis[idx++] = request->u.ata.lba >> 40;
443 fis[idx++] = request->u.ata.feature >> 8;
445 fis[idx++] = request->u.ata.count;
446 fis[idx++] = request->u.ata.count >> 8;
448 fis[idx++] = ATA_A_4BIT;
457 /* must be called with ATA channel locked and state_mtx held */
459 ata_ahci_begin_transaction(struct ata_request *request)
461 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
462 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
463 struct ata_ahci_cmd_tab *ctp;
464 struct ata_ahci_cmd_list *clp;
465 int fis_size, entries;
468 /* get a piece of the workspace for this request */
469 ctp = (struct ata_ahci_cmd_tab *)
470 (ch->dma->work + ATA_AHCI_CT_OFFSET + (ATA_AHCI_CT_SIZE * tag));
472 /* setup the FIS for this request */ /* XXX SOS ATAPI missing still */
473 if (!(fis_size = ata_ahci_setup_fis(&ctp->cfis[0], request))) {
474 device_printf(request->dev, "setting up SATA FIS failed\n");
475 request->result = EIO;
476 return ATA_OP_FINISHED;
479 /* if request moves data setup and load SG list */
480 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
481 if (ch->dma->load(ch->dev, request->data, request->bytecount,
482 request->flags & ATA_R_READ,
483 ctp->prd_tab, &entries)) {
484 device_printf(request->dev, "setting up DMA failed\n");
485 request->result = EIO;
486 return ATA_OP_FINISHED;
490 /* setup the command list entry */
491 clp = (struct ata_ahci_cmd_list *)
492 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
494 clp->prd_length = entries;
495 clp->cmd_flags = (request->flags & ATA_R_WRITE ? (1<<6) : 0) |
496 (request->flags & ATA_R_ATAPI ? (1<<5) : 0) |
497 (fis_size / sizeof(u_int32_t));
499 clp->cmd_table_phys = htole64(ch->dma->work_bus + ATA_AHCI_CT_OFFSET +
500 (ATA_AHCI_CT_SIZE * tag));
502 /* clear eventual ACTIVE bit */
503 ATA_IDX_OUTL(ch, ATA_SACTIVE, ATA_IDX_INL(ch, ATA_SACTIVE) & (1 << tag));
505 /* issue the command */
506 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CI + (ch->unit << 7), (1 << tag));
508 /* start the timeout */
509 callout_reset(&request->callout, request->timeout * hz,
510 (timeout_t*)ata_timeout, request);
511 return ATA_OP_CONTINUES;
514 /* must be called with ATA channel locked and state_mtx held */
516 ata_ahci_end_transaction(struct ata_request *request)
518 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
519 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
520 struct ata_ahci_cmd_list *clp;
524 /* kill the timeout */
525 callout_stop(&request->callout);
528 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + (ch->unit << 7));
529 request->status = tf_data;
531 /* if error status get details */
532 if (request->status & ATA_S_ERROR)
533 request->error = tf_data >> 8;
535 /* record how much data we actually moved */
536 clp = (struct ata_ahci_cmd_list *)
537 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
538 request->donecount = clp->bytecount;
540 /* release SG list etc */
541 ch->dma->unload(ch->dev);
543 return ATA_OP_FINISHED;
547 ata_ahci_intr(void *data)
549 struct ata_pci_controller *ctlr = data;
550 struct ata_channel *ch;
551 u_int32_t port, status, error, issued;
555 port = ATA_INL(ctlr->r_res2, ATA_AHCI_IS);
557 for (unit = 0; unit < ctlr->channels; unit++) {
558 if (port & (1 << unit)) {
559 if ((ch = ctlr->interrupt[unit].argument)) {
560 struct ata_connect_task *tp;
561 int offset = (ch->unit << 7);
563 error = ATA_INL(ctlr->r_res2, ATA_AHCI_P_SERR + offset);
564 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_SERR + offset, error);
565 status = ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset);
566 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset, status);
567 issued = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CI + offset);
569 /* do we have cold connect surprise */
570 if (status & ATA_AHCI_P_IX_CPD) {
571 printf("ata_ahci_intr status=%08x error=%08x issued=%08x\n",
572 status, error, issued);
575 /* check for and handle connect events */
576 if ((status & ATA_AHCI_P_IX_PC) &&
577 (tp = (struct ata_connect_task *)
578 malloc(sizeof(struct ata_connect_task),
579 M_ATA, M_NOWAIT | M_ZERO))) {
581 device_printf(ch->dev, "CONNECT requested\n");
582 tp->action = ATA_C_ATTACH;
584 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
585 taskqueue_enqueue(taskqueue_thread, &tp->task);
588 /* check for and handle disconnect events */
589 if (((status & (ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC)) ==
590 ATA_AHCI_P_IX_PRC) &&
591 (tp = (struct ata_connect_task *)
592 malloc(sizeof(struct ata_connect_task),
593 M_ATA, M_NOWAIT | M_ZERO))) {
595 device_printf(ch->dev, "DISCONNECT requested\n");
596 tp->action = ATA_C_DETACH;
598 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
599 taskqueue_enqueue(taskqueue_thread, &tp->task);
602 /* any drive action to take care of ? */
603 if (!(issued & (1<<tag)))
604 ctlr->interrupt[unit].function(ch);
608 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, port);
612 ata_ahci_reset(device_t dev)
614 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
615 struct ata_channel *ch = device_get_softc(dev);
617 int offset = (ch->unit << 7);
619 /* kill off all activity on this channel */
620 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
621 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
622 cmd & ~(ATA_AHCI_P_CMD_CR | ATA_AHCI_P_CMD_FR |
623 ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
625 DELAY(500000); /* XXX SOS this is not entirely wrong */
628 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, ATA_AHCI_P_CMD_SUD);
630 ata_sata_phy_enable(ch);
632 /* clear any interrupts pending on this channel */
633 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset,
634 ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset));
636 /* start operations on this channel */
637 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
638 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
639 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
643 ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
645 struct ata_dmasetprd_args *args = xsc;
646 struct ata_ahci_dma_prd *prd = args->dmatab;
649 if (!(args->error = error)) {
650 for (i = 0; i < nsegs; i++) {
651 prd[i].dba = htole64(segs[i].ds_addr);
652 prd[i].dbc = htole32((segs[i].ds_len - 1) & ATA_AHCI_PRD_MASK);
659 ata_ahci_dmainit(device_t dev)
661 struct ata_channel *ch = device_get_softc(dev);
665 /* note start and stop are not used here */
666 ch->dma->setprd = ata_ahci_dmasetprd;
667 ch->dma->max_iosize = 8192 * DEV_BSIZE;
673 * Acard chipset support functions
676 ata_acard_ident(device_t dev)
678 struct ata_pci_controller *ctlr = device_get_softc(dev);
679 struct ata_chip_id *idx;
680 static struct ata_chip_id ids[] =
681 {{ ATA_ATP850R, 0, ATPOLD, 0x00, ATA_UDMA2, "Acard ATP850" },
682 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "Acard ATP860A" },
683 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "Acard ATP860R" },
684 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "Acard ATP865A" },
685 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "Acard ATP865R" },
686 { 0, 0, 0, 0, 0, 0}};
689 if (!(idx = ata_match_chip(dev, ids)))
692 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
693 device_set_desc_copy(dev, buffer);
695 ctlr->chipinit = ata_acard_chipinit;
700 ata_acard_chipinit(device_t dev)
702 struct ata_pci_controller *ctlr = device_get_softc(dev);
703 int rid = ATA_IRQ_RID;
705 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
706 RF_SHAREABLE | RF_ACTIVE))) {
707 device_printf(dev, "unable to map interrupt\n");
710 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
711 ata_acard_intr, ctlr, &ctlr->handle))) {
712 device_printf(dev, "unable to setup interrupt\n");
715 if (ctlr->chip->cfg1 == ATPOLD) {
716 ctlr->setmode = ata_acard_850_setmode;
717 ctlr->locking = ata_serialize;
720 ctlr->setmode = ata_acard_86X_setmode;
725 ata_acard_intr(void *data)
727 struct ata_pci_controller *ctlr = data;
728 struct ata_channel *ch;
731 for (unit = 0; unit < ctlr->channels; unit++) {
732 if (!(ch = ctlr->interrupt[unit].argument))
734 if (ctlr->chip->cfg1 == ATPOLD &&
735 ATA_LOCKING(ch->dev, ATA_LF_WHICH) != unit)
737 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
738 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
740 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
741 ATA_BMSTAT_INTERRUPT)
743 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
745 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
746 ATA_IDX_INB(ch, ATA_BMCMD_PORT)&~ATA_BMCMD_START_STOP);
749 ctlr->interrupt[unit].function(ch);
754 ata_acard_850_setmode(device_t dev, int mode)
756 device_t gparent = GRANDPARENT(dev);
757 struct ata_pci_controller *ctlr = device_get_softc(gparent);
758 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
759 struct ata_device *atadev = device_get_softc(dev);
760 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
763 mode = ata_limit_mode(dev, mode,
764 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
766 /* XXX SOS missing WDMA0+1 + PIO modes */
767 if (mode >= ATA_WDMA2) {
768 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
770 device_printf(dev, "%ssetting %s on %s chip\n",
771 (error) ? "FAILURE " : "",
772 ata_mode2str(mode), ctlr->chip->text);
774 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
776 reg54 &= ~(0x03 << (devno << 1));
777 if (mode >= ATA_UDMA0)
778 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
779 pci_write_config(gparent, 0x54, reg54, 1);
780 pci_write_config(gparent, 0x4a, 0xa6, 1);
781 pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
786 /* we could set PIO mode timings, but we assume the BIOS did that */
790 ata_acard_86X_setmode(device_t dev, int mode)
792 device_t gparent = GRANDPARENT(dev);
793 struct ata_pci_controller *ctlr = device_get_softc(gparent);
794 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
795 struct ata_device *atadev = device_get_softc(dev);
796 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
800 mode = ata_limit_mode(dev, mode,
801 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
803 mode = ata_check_80pin(dev, mode);
805 /* XXX SOS missing WDMA0+1 + PIO modes */
806 if (mode >= ATA_WDMA2) {
807 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
809 device_printf(dev, "%ssetting %s on %s chip\n",
810 (error) ? "FAILURE " : "",
811 ata_mode2str(mode), ctlr->chip->text);
813 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
815 reg44 &= ~(0x000f << (devno << 2));
816 if (mode >= ATA_UDMA0)
817 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
818 pci_write_config(gparent, 0x44, reg44, 2);
819 pci_write_config(gparent, 0x4a, 0xa6, 1);
820 pci_write_config(gparent, 0x40 + devno, 0x31, 1);
825 /* we could set PIO mode timings, but we assume the BIOS did that */
830 * Acer Labs Inc (ALI) chipset support functions
833 ata_ali_ident(device_t dev)
835 struct ata_pci_controller *ctlr = device_get_softc(dev);
836 struct ata_chip_id *idx;
837 static struct ata_chip_id ids[] =
838 {{ ATA_ALI_5289, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5289" },
839 { ATA_ALI_5287, 0x00, 4, ALISATA, ATA_SA150, "AcerLabs M5287" },
840 { ATA_ALI_5281, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5281" },
841 { ATA_ALI_5229, 0xc5, 0, ALINEW, ATA_UDMA6, "AcerLabs M5229" },
842 { ATA_ALI_5229, 0xc4, 0, ALINEW, ATA_UDMA5, "AcerLabs M5229" },
843 { ATA_ALI_5229, 0xc2, 0, ALINEW, ATA_UDMA4, "AcerLabs M5229" },
844 { ATA_ALI_5229, 0x20, 0, ALIOLD, ATA_UDMA2, "AcerLabs M5229" },
845 { ATA_ALI_5229, 0x00, 0, ALIOLD, ATA_WDMA2, "AcerLabs M5229" },
846 { 0, 0, 0, 0, 0, 0}};
849 if (!(idx = ata_match_chip(dev, ids)))
852 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
853 device_set_desc_copy(dev, buffer);
855 ctlr->chipinit = ata_ali_chipinit;
860 ata_ali_chipinit(device_t dev)
862 struct ata_pci_controller *ctlr = device_get_softc(dev);
864 if (ata_setup_interrupt(dev))
867 switch (ctlr->chip->cfg2) {
869 pci_write_config(dev, PCIR_COMMAND,
870 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
871 ctlr->channels = ctlr->chip->cfg1;
872 ctlr->allocate = ata_ali_allocate;
873 ctlr->setmode = ata_sata_setmode;
877 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
878 pci_write_config(dev, 0x53,
879 pci_read_config(dev, 0x53, 1) | 0x01, 1);
881 /* enable cable detection and UDMA support on newer chips */
882 pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
883 ctlr->setmode = ata_ali_setmode;
887 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
888 pci_write_config(dev, 0x53,
889 (pci_read_config(dev, 0x53, 1) & ~0x02) | 0x03, 1);
890 ctlr->setmode = ata_ali_setmode;
897 ata_ali_allocate(device_t dev)
899 device_t parent = device_get_parent(dev);
900 struct ata_pci_controller *ctlr = device_get_softc(parent);
901 struct ata_channel *ch = device_get_softc(dev);
902 struct resource *io = NULL, *ctlio = NULL;
903 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
906 rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
907 io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
911 rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
912 ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
914 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
918 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
919 ch->r_io[i].res = io;
920 ch->r_io[i].offset = i + (unit10 ? 8 : 0);
922 ch->r_io[ATA_CONTROL].res = ctlio;
923 ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
924 ch->r_io[ATA_IDX_ADDR].res = io;
925 ata_default_registers(dev);
927 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
928 ch->r_io[i].res = ctlr->r_res1;
929 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
932 ch->flags |= ATA_NO_SLAVE;
934 /* XXX SOS PHY handling awkward in ALI chip not supported yet */
940 ata_ali_setmode(device_t dev, int mode)
942 device_t gparent = GRANDPARENT(dev);
943 struct ata_pci_controller *ctlr = device_get_softc(gparent);
944 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
945 struct ata_device *atadev = device_get_softc(dev);
946 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
949 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
951 if (ctlr->chip->cfg2 & ALINEW) {
952 if (mode > ATA_UDMA2 &&
953 pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
954 ata_print_cable(dev, "controller");
959 mode = ata_check_80pin(dev, mode);
961 if (ctlr->chip->cfg2 & ALIOLD) {
962 /* doesn't support ATAPI DMA on write */
963 ch->flags |= ATA_ATAPI_DMA_RO;
964 if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
965 /* doesn't support ATAPI DMA on two ATAPI devices */
966 device_printf(dev, "two atapi devices on this channel, no DMA\n");
967 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
971 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
974 device_printf(dev, "%ssetting %s on %s chip\n",
975 (error) ? "FAILURE " : "",
976 ata_mode2str(mode), ctlr->chip->text);
978 if (mode >= ATA_UDMA0) {
979 u_int8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
980 u_int32_t word54 = pci_read_config(gparent, 0x54, 4);
982 word54 &= ~(0x000f000f << (devno << 2));
983 word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
984 pci_write_config(gparent, 0x54, word54, 4);
985 pci_write_config(gparent, 0x58 + (ch->unit << 2),
989 u_int32_t piotimings[] =
990 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
991 0x00310001, 0x00440001, 0x00330001, 0x00310001};
993 pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
994 ~(0x0008000f << (devno << 2)), 4);
995 pci_write_config(gparent, 0x58 + (ch->unit << 2),
996 piotimings[ata_mode2idx(mode)], 4);
1004 * American Micro Devices (AMD) chipset support functions
1007 ata_amd_ident(device_t dev)
1009 struct ata_pci_controller *ctlr = device_get_softc(dev);
1010 struct ata_chip_id *idx;
1011 static struct ata_chip_id ids[] =
1012 {{ ATA_AMD756, 0x00, AMDNVIDIA, 0x00, ATA_UDMA4, "AMD 756" },
1013 { ATA_AMD766, 0x00, AMDNVIDIA, AMDCABLE|AMDBUG, ATA_UDMA5, "AMD 766" },
1014 { ATA_AMD768, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA5, "AMD 768" },
1015 { ATA_AMD8111, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA6, "AMD 8111" },
1016 { 0, 0, 0, 0, 0, 0}};
1019 if (!(idx = ata_match_chip(dev, ids)))
1022 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1023 device_set_desc_copy(dev, buffer);
1025 ctlr->chipinit = ata_amd_chipinit;
1030 ata_amd_chipinit(device_t dev)
1032 struct ata_pci_controller *ctlr = device_get_softc(dev);
1034 if (ata_setup_interrupt(dev))
1037 /* disable/set prefetch, postwrite */
1038 if (ctlr->chip->cfg2 & AMDBUG)
1039 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
1041 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
1043 ctlr->setmode = ata_via_family_setmode;
1049 * Cyrix chipset support functions
1052 ata_cyrix_ident(device_t dev)
1054 struct ata_pci_controller *ctlr = device_get_softc(dev);
1056 if (pci_get_devid(dev) == ATA_CYRIX_5530) {
1057 device_set_desc(dev, "Cyrix 5530 ATA33 controller");
1058 ctlr->chipinit = ata_cyrix_chipinit;
1065 ata_cyrix_chipinit(device_t dev)
1067 struct ata_pci_controller *ctlr = device_get_softc(dev);
1069 if (ata_setup_interrupt(dev))
1073 ctlr->setmode = ata_cyrix_setmode;
1075 ctlr->setmode = ata_generic_setmode;
1080 ata_cyrix_setmode(device_t dev, int mode)
1082 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1083 struct ata_device *atadev = device_get_softc(dev);
1084 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1085 u_int32_t piotiming[] =
1086 { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
1087 u_int32_t dmatiming[] = { 0x00077771, 0x00012121, 0x00002020 };
1088 u_int32_t udmatiming[] = { 0x00921250, 0x00911140, 0x00911030 };
1091 ch->dma->alignment = 16;
1092 ch->dma->max_iosize = 126 * DEV_BSIZE;
1094 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1096 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1099 device_printf(dev, "%ssetting %s on Cyrix chip\n",
1100 (error) ? "FAILURE " : "", ata_mode2str(mode));
1102 if (mode >= ATA_UDMA0) {
1103 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1104 0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
1106 else if (mode >= ATA_WDMA0) {
1107 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1108 0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
1111 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1112 0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
1114 atadev->mode = mode;
1120 * Cypress chipset support functions
1123 ata_cypress_ident(device_t dev)
1125 struct ata_pci_controller *ctlr = device_get_softc(dev);
1128 * the Cypress chip is a mess, it contains two ATA functions, but
1129 * both channels are visible on the first one.
1130 * simply ignore the second function for now, as the right
1131 * solution (ignoring the second channel on the first function)
1132 * doesn't work with the crappy ATA interrupt setup on the alpha.
1134 if (pci_get_devid(dev) == ATA_CYPRESS_82C693 &&
1135 pci_get_function(dev) == 1 &&
1136 pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
1137 device_set_desc(dev, "Cypress 82C693 ATA controller");
1138 ctlr->chipinit = ata_cypress_chipinit;
1145 ata_cypress_chipinit(device_t dev)
1147 struct ata_pci_controller *ctlr = device_get_softc(dev);
1149 if (ata_setup_interrupt(dev))
1152 ctlr->setmode = ata_cypress_setmode;
1157 ata_cypress_setmode(device_t dev, int mode)
1159 device_t gparent = GRANDPARENT(dev);
1160 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1161 struct ata_device *atadev = device_get_softc(dev);
1164 mode = ata_limit_mode(dev, mode, ATA_WDMA2);
1166 /* XXX SOS missing WDMA0+1 + PIO modes */
1167 if (mode == ATA_WDMA2) {
1168 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1170 device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
1171 error ? "FAILURE " : "");
1173 pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
1174 atadev->mode = mode;
1178 /* we could set PIO mode timings, but we assume the BIOS did that */
1183 * HighPoint chipset support functions
1186 ata_highpoint_ident(device_t dev)
1188 struct ata_pci_controller *ctlr = device_get_softc(dev);
1189 struct ata_chip_id *idx;
1190 static struct ata_chip_id ids[] =
1191 {{ ATA_HPT374, 0x07, HPT374, 0x00, ATA_UDMA6, "HighPoint HPT374" },
1192 { ATA_HPT372, 0x02, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372N" },
1193 { ATA_HPT372, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372" },
1194 { ATA_HPT371, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT371" },
1195 { ATA_HPT366, 0x05, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372" },
1196 { ATA_HPT366, 0x03, HPT370, 0x00, ATA_UDMA5, "HighPoint HPT370" },
1197 { ATA_HPT366, 0x02, HPT366, 0x00, ATA_UDMA4, "HighPoint HPT368" },
1198 { ATA_HPT366, 0x00, HPT366, HPTOLD, ATA_UDMA4, "HighPoint HPT366" },
1199 { ATA_HPT302, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT302" },
1200 { 0, 0, 0, 0, 0, 0}};
1203 if (!(idx = ata_match_chip(dev, ids)))
1206 strcpy(buffer, idx->text);
1207 if (idx->cfg1 == HPT374) {
1208 if (pci_get_function(dev) == 0)
1209 strcat(buffer, " (channel 0+1)");
1210 else if (pci_get_function(dev) == 1)
1211 strcat(buffer, " (channel 2+3)");
1213 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
1214 device_set_desc_copy(dev, buffer);
1216 ctlr->chipinit = ata_highpoint_chipinit;
1221 ata_highpoint_chipinit(device_t dev)
1223 struct ata_pci_controller *ctlr = device_get_softc(dev);
1224 int rid = ATA_IRQ_RID;
1226 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1227 RF_SHAREABLE | RF_ACTIVE))) {
1228 device_printf(dev, "unable to map interrupt\n");
1231 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1232 ata_highpoint_intr, ctlr, &ctlr->handle))) {
1233 device_printf(dev, "unable to setup interrupt\n");
1237 if (ctlr->chip->cfg2 == HPTOLD) {
1238 /* disable interrupt prediction */
1239 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
1242 /* disable interrupt prediction */
1243 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
1244 pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
1246 /* enable interrupts */
1247 pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
1249 /* set clocks etc */
1250 if (ctlr->chip->cfg1 < HPT372)
1251 pci_write_config(dev, 0x5b, 0x22, 1);
1253 pci_write_config(dev, 0x5b,
1254 (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
1256 ctlr->setmode = ata_highpoint_setmode;
1261 ata_highpoint_intr(void *data)
1263 struct ata_pci_controller *ctlr = data;
1264 struct ata_channel *ch;
1267 for (unit = 0; unit < ctlr->channels; unit++) {
1268 if (!(ch = ctlr->interrupt[unit].argument))
1271 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1273 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1274 ATA_BMSTAT_INTERRUPT)
1276 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1279 ctlr->interrupt[unit].function(ch);
1284 ata_highpoint_setmode(device_t dev, int mode)
1286 device_t gparent = GRANDPARENT(dev);
1287 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1288 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1289 struct ata_device *atadev = device_get_softc(dev);
1290 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1292 u_int32_t timings33[][4] = {
1293 /* HPT366 HPT370 HPT372 HPT374 mode */
1294 { 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a }, /* PIO 0 */
1295 { 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 }, /* PIO 1 */
1296 { 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 }, /* PIO 2 */
1297 { 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 }, /* PIO 3 */
1298 { 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 }, /* PIO 4 */
1299 { 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea }, /* MWDMA 0 */
1300 { 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 }, /* MWDMA 1 */
1301 { 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 }, /* MWDMA 2 */
1302 { 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea }, /* UDMA 0 */
1303 { 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 }, /* UDMA 1 */
1304 { 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 }, /* UDMA 2 */
1305 { 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 }, /* UDMA 3 */
1306 { 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 }, /* UDMA 4 */
1307 { 0, 0x16454e31, 0x1c8a9c62, 0x12848242 }, /* UDMA 5 */
1308 { 0, 0, 0x1c869c62, 0x12808242 } /* UDMA 6 */
1311 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1313 if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
1314 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1316 mode = ata_highpoint_check_80pin(dev, mode);
1319 * most if not all HPT chips cant really handle that the device is
1320 * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
1321 * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
1323 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
1324 ata_limit_mode(dev, mode, ATA_UDMA5));
1326 device_printf(dev, "%ssetting %s on HighPoint chip\n",
1327 (error) ? "FAILURE " : "", ata_mode2str(mode));
1329 pci_write_config(gparent, 0x40 + (devno << 2),
1330 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
1331 atadev->mode = mode;
1335 ata_highpoint_check_80pin(device_t dev, int mode)
1337 device_t gparent = GRANDPARENT(dev);
1338 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1339 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1340 u_int8_t reg, val, res;
1342 if (ctlr->chip->cfg1 == HPT374 && pci_get_function(gparent) == 1) {
1343 reg = ch->unit ? 0x57 : 0x53;
1344 val = pci_read_config(gparent, reg, 1);
1345 pci_write_config(gparent, reg, val | 0x80, 1);
1349 val = pci_read_config(gparent, reg, 1);
1350 pci_write_config(gparent, reg, val & 0xfe, 1);
1352 res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1353 pci_write_config(gparent, reg, val, 1);
1355 if (mode > ATA_UDMA2 && res) {
1356 ata_print_cable(dev, "controller");
1364 * Intel chipset support functions
1367 ata_intel_ident(device_t dev)
1369 struct ata_pci_controller *ctlr = device_get_softc(dev);
1370 struct ata_chip_id *idx;
1371 static struct ata_chip_id ids[] =
1372 {{ ATA_I82371FB, 0, 0, 0x00, ATA_WDMA2, "Intel PIIX" },
1373 { ATA_I82371SB, 0, 0, 0x00, ATA_WDMA2, "Intel PIIX3" },
1374 { ATA_I82371AB, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1375 { ATA_I82443MX, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1376 { ATA_I82451NX, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1377 { ATA_I82801AB, 0, 0, 0x00, ATA_UDMA2, "Intel ICH0" },
1378 { ATA_I82801AA, 0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1379 { ATA_I82372FB, 0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1380 { ATA_I82801BA, 0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1381 { ATA_I82801BA_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1382 { ATA_I82801CA, 0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1383 { ATA_I82801CA_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1384 { ATA_I82801DB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1385 { ATA_I82801DB_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1386 { ATA_I82801EB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH5" },
1387 { ATA_I82801EB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1388 { ATA_I82801EB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1389 { ATA_I6300ESB, 0, 0, 0x00, ATA_UDMA5, "Intel 6300ESB" },
1390 { ATA_I6300ESB_S1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1391 { ATA_I6300ESB_R1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1392 { ATA_I82801FB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH6" },
1393 { ATA_I82801FB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1394 { ATA_I82801FB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1395 { ATA_I82801FB_M, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1396 { ATA_I82801GB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH7" },
1397 { ATA_I82801GB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH7" },
1398 { ATA_I82801GB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH7" },
1399 { ATA_I82801GB_M, 0, 0, 0x00, ATA_SA150, "Intel ICH7" },
1400 { ATA_I82801GB_AH, 0, 0, 0x00, ATA_SA150, "Intel ICH7" },
1401 { ATA_I31244, 0, 0, 0x00, ATA_SA150, "Intel 31244" },
1402 { 0, 0, 0, 0, 0, 0}};
1405 if (!(idx = ata_match_chip(dev, ids)))
1408 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1409 device_set_desc_copy(dev, buffer);
1411 ctlr->chipinit = ata_intel_chipinit;
1416 ata_intel_chipinit(device_t dev)
1418 struct ata_pci_controller *ctlr = device_get_softc(dev);
1419 int rid = ATA_IRQ_RID;
1421 if (!ata_legacy(dev)) {
1422 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1423 RF_SHAREABLE | RF_ACTIVE))) {
1424 device_printf(dev, "unable to map interrupt\n");
1427 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1428 ata_intel_intr, ctlr, &ctlr->handle))) {
1429 device_printf(dev, "unable to setup interrupt\n");
1434 /* good old PIIX needs special treatment (not implemented) */
1435 if (ctlr->chip->chipid == ATA_I82371FB) {
1436 ctlr->setmode = ata_intel_old_setmode;
1439 /* the intel 31244 needs special care if in DPA mode */
1440 else if (ctlr->chip->chipid == ATA_I31244) {
1441 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE) {
1442 ctlr->r_type2 = SYS_RES_MEMORY;
1443 ctlr->r_rid2 = PCIR_BAR(0);
1444 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1449 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
1450 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1451 ata_intel_31244_intr, ctlr, &ctlr->handle)) {
1452 device_printf(dev, "unable to setup interrupt\n");
1456 ctlr->reset = ata_intel_31244_reset;
1457 ctlr->allocate = ata_intel_31244_allocate;
1459 ctlr->setmode = ata_sata_setmode;
1462 /* non SATA intel chips goes here */
1463 else if (ctlr->chip->max_dma < ATA_SA150) {
1464 ctlr->setmode = ata_intel_new_setmode;
1467 /* SATA parts can be either compat or AHCI */
1469 /* if we have BAR(5) as a memory resource we should use AHCI mode */
1470 ctlr->r_type2 = SYS_RES_MEMORY;
1471 ctlr->r_rid2 = PCIR_BAR(5);
1472 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1473 &ctlr->r_rid2, RF_ACTIVE))) {
1474 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
1475 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1476 ata_ahci_intr, ctlr, &ctlr->handle)) {
1477 device_printf(dev, "unable to setup interrupt\n");
1481 /* force all ports active "the legacy way" */
1482 pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f,2);
1484 /* enable AHCI mode */
1485 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC, ATA_AHCI_GHC_AE);
1487 /* get the number of HW channels */
1488 ctlr->channels = (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) &
1489 ATA_AHCI_NPMASK) + 1;
1491 /* enable AHCI interrupts */
1492 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
1493 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) | ATA_AHCI_GHC_IE);
1495 ctlr->reset = ata_ahci_reset;
1496 ctlr->dmainit = ata_ahci_dmainit;
1497 ctlr->allocate = ata_ahci_allocate;
1500 ctlr->reset = ata_intel_reset;
1502 ctlr->setmode = ata_sata_setmode;
1503 pci_write_config(dev, PCIR_COMMAND,
1504 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1510 ata_intel_31244_allocate(device_t dev)
1512 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1513 struct ata_channel *ch = device_get_softc(dev);
1517 ch_offset = 0x200 + ch->unit * 0x200;
1519 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
1520 ch->r_io[i].res = ctlr->r_res2;
1521 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
1522 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x06;
1523 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
1524 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
1525 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
1526 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
1527 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
1528 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1d;
1529 ch->r_io[ATA_ERROR].offset = ch_offset + 0x04;
1530 ch->r_io[ATA_STATUS].offset = ch_offset + 0x1c;
1531 ch->r_io[ATA_ALTSTAT].offset = ch_offset + 0x28;
1532 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x29;
1533 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x100;
1534 ch->r_io[ATA_SERROR].offset = ch_offset + 0x104;
1535 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x108;
1536 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x70;
1537 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x72;
1538 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x74;
1540 ch->flags |= ATA_NO_SLAVE;
1541 ata_generic_hw(dev);
1542 ch->hw.command = ata_intel_31244_command;
1547 ata_intel_31244_intr(void *data)
1549 struct ata_pci_controller *ctlr = data;
1550 struct ata_channel *ch;
1553 for (unit = 0; unit < ctlr->channels; unit++) {
1554 if (!(ch = ctlr->interrupt[unit].argument))
1557 /* check for PHY related interrupts on SATA capable HW */
1558 if (ctlr->chip->max_dma >= ATA_SA150) {
1559 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
1560 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
1561 struct ata_connect_task *tp;
1564 /* clear error bits/interrupt */
1565 ATA_IDX_OUTL(ch, ATA_SERROR, error);
1567 /* if we have a connection event deal with it */
1568 if ((error & ATA_SE_PHY_CHANGED) &&
1569 (tp = (struct ata_connect_task *)
1570 malloc(sizeof(struct ata_connect_task),
1571 M_ATA, M_NOWAIT | M_ZERO))) {
1573 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
1574 device_printf(ch->dev, "CONNECT requested\n");
1575 tp->action = ATA_C_ATTACH;
1578 device_printf(ch->dev, "DISCONNECT requested\n");
1579 tp->action = ATA_C_DETACH;
1582 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1583 taskqueue_enqueue(taskqueue_thread, &tp->task);
1588 /* any drive action to take care of ? */
1589 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
1590 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1592 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
1594 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1597 ctlr->interrupt[unit].function(ch);
1602 ata_intel_31244_reset(device_t dev)
1604 struct ata_channel *ch = device_get_softc(dev);
1606 ata_sata_phy_enable(ch);
1610 ata_intel_31244_command(struct ata_request *request)
1612 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
1613 struct ata_device *atadev = device_get_softc(request->dev);
1617 command = ata_modify_if_48bit(request);
1618 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
1619 return (ata_generic_command(request));
1621 lba = request->u.ata.lba;
1622 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | atadev->unit);
1623 /* enable interrupt */
1624 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
1625 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
1626 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
1627 ATA_IDX_OUTW(ch, ATA_SECTOR, ((lba >> 16) & 0xff00) | (lba & 0x00ff));
1628 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((lba >> 24) & 0xff00) | ((lba >> 8) &
1630 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((lba >> 32) & 0xff00) | ((lba >> 16) &
1633 /* issue command to controller */
1634 ATA_IDX_OUTB(ch, ATA_COMMAND, command);
1640 ata_intel_intr(void *data)
1642 struct ata_pci_controller *ctlr = data;
1643 struct ata_channel *ch;
1646 for (unit = 0; unit < ctlr->channels; unit++) {
1647 if (!(ch = ctlr->interrupt[unit].argument))
1650 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1652 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1653 ATA_BMSTAT_INTERRUPT)
1655 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1658 ctlr->interrupt[unit].function(ch);
1663 ata_intel_reset(device_t dev)
1665 device_t parent = device_get_parent(dev);
1666 struct ata_pci_controller *ctlr = device_get_softc(parent);
1667 struct ata_channel *ch = device_get_softc(dev);
1670 /* ICH6 has 4 SATA ports as master/slave on 2 channels so deal with pairs */
1671 if (ctlr->chip->chipid == ATA_I82801FB_S1 ||
1672 ctlr->chip->chipid == ATA_I82801FB_R1 ||
1673 ctlr->chip->chipid == ATA_I82801FB_M) {
1674 mask = (0x0005 << ch->unit);
1677 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1678 if (pci_read_config(parent, 0x90, 1) & 0x04)
1681 mask = (0x0001 << ch->unit);
1682 /* XXX SOS should be in intel_allocate when we grow it */
1683 ch->flags |= ATA_NO_SLAVE;
1686 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1688 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1690 /* wait up to 1 sec for "connect well" */
1691 for (timeout = 0; timeout < 100 ; timeout++) {
1692 if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1693 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1697 ata_generic_reset(dev);
1701 ata_intel_old_setmode(device_t dev, int mode)
1707 ata_intel_new_setmode(device_t dev, int mode)
1709 device_t gparent = GRANDPARENT(dev);
1710 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1711 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1712 struct ata_device *atadev = device_get_softc(dev);
1713 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1714 u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
1715 u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
1716 u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
1717 u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
1718 u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
1719 u_int32_t mask40 = 0, new40 = 0;
1720 u_int8_t mask44 = 0, new44 = 0;
1722 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1723 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1725 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1727 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1728 ata_print_cable(dev, "controller");
1732 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1735 device_printf(dev, "%ssetting %s on %s chip\n",
1736 (error) ? "FAILURE " : "",
1737 ata_mode2str(mode), ctlr->chip->text);
1741 if (mode >= ATA_UDMA0) {
1742 pci_write_config(gparent, 0x48, reg48 | (0x0001 << devno), 2);
1743 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno<<2))) |
1744 (0x01 + !(mode & 0x01)), 2);
1747 pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
1748 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))), 2);
1751 if (mode >= ATA_UDMA2)
1752 pci_write_config(gparent, 0x54, reg54 | (0x1 << devno), 2);
1754 pci_write_config(gparent, 0x54, reg54 & ~(0x1 << devno), 2);
1756 if (mode >= ATA_UDMA5)
1757 pci_write_config(gparent, 0x54, reg54 | (0x1000 << devno), 2);
1759 pci_write_config(gparent, 0x54, reg54 & ~(0x1000 << devno), 2);
1761 reg40 &= ~0x00ff00ff;
1762 reg40 |= 0x40774077;
1764 if (atadev->unit == ATA_MASTER) {
1766 new40 = timings[ata_mode2idx(mode)] << 8;
1770 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
1771 (timings[ata_mode2idx(mode)] & 0x03);
1779 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
1780 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
1782 atadev->mode = mode;
1787 * Integrated Technology Express Inc. (ITE) chipset support functions
1790 ata_ite_ident(device_t dev)
1792 struct ata_pci_controller *ctlr = device_get_softc(dev);
1794 if (pci_get_devid(dev) == ATA_IT8212F) {
1795 device_set_desc(dev, "ITE IT8212F ATA133 controller");
1796 ctlr->chipinit = ata_ite_chipinit;
1803 ata_ite_chipinit(device_t dev)
1805 struct ata_pci_controller *ctlr = device_get_softc(dev);
1807 if (ata_setup_interrupt(dev))
1810 ctlr->setmode = ata_ite_setmode;
1812 /* set PCI mode and 66Mhz reference clock */
1813 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
1815 /* set default active & recover timings */
1816 pci_write_config(dev, 0x54, 0x31, 1);
1817 pci_write_config(dev, 0x56, 0x31, 1);
1822 ata_ite_setmode(device_t dev, int mode)
1824 device_t gparent = GRANDPARENT(dev);
1825 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1826 struct ata_device *atadev = device_get_softc(dev);
1827 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1830 /* correct the mode for what the HW supports */
1831 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
1833 /* check the CBLID bits for 80 conductor cable detection */
1834 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
1835 (ch->unit ? (1<<3) : (1<<2)))) {
1836 ata_print_cable(dev, "controller");
1840 /* set the wanted mode on the device */
1841 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1844 device_printf(dev, "%s setting %s on ITE8212F chip\n",
1845 (error) ? "failed" : "success", ata_mode2str(mode));
1847 /* if the device accepted the mode change, setup the HW accordingly */
1849 if (mode >= ATA_UDMA0) {
1850 u_int8_t udmatiming[] =
1851 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
1853 /* enable UDMA mode */
1854 pci_write_config(gparent, 0x50,
1855 pci_read_config(gparent, 0x50, 1) &
1856 ~(1 << (devno + 3)), 1);
1858 /* set UDMA timing */
1859 pci_write_config(gparent,
1860 0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
1861 udmatiming[mode & ATA_MODE_MASK], 1);
1864 u_int8_t chtiming[] =
1865 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
1867 /* disable UDMA mode */
1868 pci_write_config(gparent, 0x50,
1869 pci_read_config(gparent, 0x50, 1) |
1870 (1 << (devno + 3)), 1);
1872 /* set active and recover timing (shared between master & slave) */
1873 if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
1874 chtiming[ata_mode2idx(mode)])
1875 pci_write_config(gparent, 0x54 + (ch->unit << 2),
1876 chtiming[ata_mode2idx(mode)], 1);
1878 atadev->mode = mode;
1884 * National chipset support functions
1887 ata_national_ident(device_t dev)
1889 struct ata_pci_controller *ctlr = device_get_softc(dev);
1891 /* this chip is a clone of the Cyrix chip, bugs and all */
1892 if (pci_get_devid(dev) == ATA_SC1100) {
1893 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
1894 ctlr->chipinit = ata_national_chipinit;
1900 static device_t nat_host = NULL;
1903 ata_national_chipinit(device_t dev)
1905 struct ata_pci_controller *ctlr = device_get_softc(dev);
1909 if (ata_setup_interrupt(dev))
1912 /* locate the ISA part in the southbridge and enable UDMA33 */
1913 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
1914 for (i = 0; i < nchildren; i++) {
1915 if (pci_get_devid(children[i]) == 0x0510100b) {
1916 nat_host = children[i];
1920 free(children, M_TEMP);
1922 ctlr->setmode = ata_national_setmode;
1927 ata_national_setmode(device_t dev, int mode)
1929 device_t gparent = GRANDPARENT(dev);
1930 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1931 struct ata_device *atadev = device_get_softc(dev);
1932 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1933 u_int32_t piotiming[] =
1934 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
1935 0x00803020, 0x20102010, 0x00100010,
1936 0x00100010, 0x00100010, 0x00100010 };
1937 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
1938 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
1941 ch->dma->alignment = 16;
1942 ch->dma->max_iosize = 126 * DEV_BSIZE;
1944 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1946 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1949 device_printf(dev, "%s setting %s on National chip\n",
1950 (error) ? "failed" : "success", ata_mode2str(mode));
1952 if (mode >= ATA_UDMA0) {
1953 pci_write_config(gparent, 0x44 + (devno << 3),
1954 udmatiming[mode & ATA_MODE_MASK], 4);
1956 else if (mode >= ATA_WDMA0) {
1957 pci_write_config(gparent, 0x44 + (devno << 3),
1958 dmatiming[mode & ATA_MODE_MASK], 4);
1961 pci_write_config(gparent, 0x44 + (devno << 3),
1962 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
1965 pci_write_config(gparent, 0x40 + (devno << 3),
1966 piotiming[ata_mode2idx(mode)], 4);
1967 atadev->mode = mode;
1973 * nVidia chipset support functions
1976 ata_nvidia_ident(device_t dev)
1978 struct ata_pci_controller *ctlr = device_get_softc(dev);
1979 struct ata_chip_id *idx;
1980 static struct ata_chip_id ids[] =
1981 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nVidia nForce" },
1982 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2" },
1983 { ATA_NFORCE2_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2 MCP" },
1984 { ATA_NFORCE2_MCP_S1, 0, 0, 0, ATA_SA150, "nVidia nForce2 MCP" },
1985 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3" },
1986 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 Pro" },
1987 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
1988 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
1989 { ATA_NFORCE3_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 MCP" },
1990 { ATA_NFORCE3_MCP_S1, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
1991 { ATA_NFORCE3_MCP_S2, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
1992 { ATA_NFORCE4, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce4" },
1993 { ATA_NFORCE4_S1, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce4" },
1994 { ATA_NFORCE4_S2, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce4" },
1995 { 0, 0, 0, 0, 0, 0}};
1998 if (!(idx = ata_match_chip(dev, ids)))
2001 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
2002 device_set_desc_copy(dev, buffer);
2004 ctlr->chipinit = ata_nvidia_chipinit;
2009 ata_nvidia_chipinit(device_t dev)
2011 struct ata_pci_controller *ctlr = device_get_softc(dev);
2013 if (ata_setup_interrupt(dev))
2016 if (ctlr->chip->max_dma >= ATA_SA150) {
2017 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
2018 ctlr->r_type2 = SYS_RES_IOPORT;
2020 ctlr->r_type2 = SYS_RES_MEMORY;
2021 ctlr->r_rid2 = PCIR_BAR(5);
2022 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2023 &ctlr->r_rid2, RF_ACTIVE))) {
2024 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
2026 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
2027 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2028 ata_nvidia_intr, ctlr, &ctlr->handle)) {
2029 device_printf(dev, "unable to setup interrupt\n");
2033 /* enable control access */
2034 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
2036 /* clear interrupt status */
2037 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2039 /* enable device and PHY state change interrupts */
2040 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
2042 /* enable PCI interrupt */
2043 pci_write_config(dev, PCIR_COMMAND,
2044 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
2046 ctlr->allocate = ata_nvidia_allocate;
2047 ctlr->reset = ata_nvidia_reset;
2049 ctlr->setmode = ata_sata_setmode;
2052 /* disable prefetch, postwrite */
2053 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
2054 ctlr->setmode = ata_via_family_setmode;
2060 ata_nvidia_allocate(device_t dev)
2062 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2063 struct ata_channel *ch = device_get_softc(dev);
2065 /* setup the usual register normal pci style */
2066 ata_pci_allocate(dev);
2068 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2069 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
2070 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2071 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
2072 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2073 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
2074 ch->flags |= ATA_NO_SLAVE;
2080 ata_nvidia_intr(void *data)
2082 struct ata_pci_controller *ctlr = data;
2083 struct ata_channel *ch;
2084 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
2088 /* get interrupt status */
2089 status = ATA_INB(ctlr->r_res2, offset);
2091 /* clear interrupt status */
2092 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2094 for (unit = 0; unit < ctlr->channels; unit++) {
2095 if ((ch = ctlr->interrupt[unit].argument)) {
2096 struct ata_connect_task *tp;
2097 int maskshift = ch->unit << 2;
2099 /* clear error status */
2100 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2102 /* check for and handle connect events */
2103 if (((status & (0x0c << maskshift)) == (0x04 << maskshift)) &&
2104 (tp = (struct ata_connect_task *)
2105 malloc(sizeof(struct ata_connect_task),
2106 M_ATA, M_NOWAIT | M_ZERO))) {
2108 device_printf(ch->dev, "CONNECT requested\n");
2109 tp->action = ATA_C_ATTACH;
2111 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2112 taskqueue_enqueue(taskqueue_thread, &tp->task);
2115 /* check for and handle disconnect events */
2116 if ((status & (0x08 << maskshift)) &&
2117 (tp = (struct ata_connect_task *)
2118 malloc(sizeof(struct ata_connect_task),
2119 M_ATA, M_NOWAIT | M_ZERO))) {
2121 device_printf(ch->dev, "DISCONNECT requested\n");
2122 tp->action = ATA_C_DETACH;
2124 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2125 taskqueue_enqueue(taskqueue_thread, &tp->task);
2128 /* any drive action to take care of ? */
2129 if (status & (0x01 << maskshift)) {
2130 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2132 ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2134 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
2136 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat&~ATA_BMSTAT_ERROR);
2139 ctlr->interrupt[unit].function(ch);
2146 ata_nvidia_reset(device_t dev)
2148 struct ata_channel *ch = device_get_softc(dev);
2150 ata_sata_phy_enable(ch);
2155 * Promise chipset support functions
2157 #define ATA_PDC_APKT_OFFSET 0x00000010
2158 #define ATA_PDC_HPKT_OFFSET 0x00000040
2159 #define ATA_PDC_ASG_OFFSET 0x00000080
2160 #define ATA_PDC_LSG_OFFSET 0x000000c0
2161 #define ATA_PDC_HSG_OFFSET 0x00000100
2162 #define ATA_PDC_CHN_OFFSET 0x00000400
2163 #define ATA_PDC_BUF_BASE 0x00400000
2164 #define ATA_PDC_BUF_OFFSET 0x00100000
2165 #define ATA_PDC_MAX_HPKT 8
2166 #define ATA_PDC_WRITE_REG 0x00
2167 #define ATA_PDC_WRITE_CTL 0x0e
2168 #define ATA_PDC_WRITE_END 0x08
2169 #define ATA_PDC_WAIT_NBUSY 0x10
2170 #define ATA_PDC_WAIT_READY 0x18
2171 #define ATA_PDC_1B 0x20
2172 #define ATA_PDC_2B 0x40
2174 struct host_packet {
2176 TAILQ_ENTRY(host_packet) chain;
2179 struct ata_promise_sx4 {
2181 TAILQ_HEAD(, host_packet) queue;
2186 ata_promise_ident(device_t dev)
2188 struct ata_pci_controller *ctlr = device_get_softc(dev);
2189 struct ata_chip_id *idx;
2190 static struct ata_chip_id ids[] =
2191 {{ ATA_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "Promise PDC20246" },
2192 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20262" },
2193 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20263" },
2194 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20265" },
2195 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20267" },
2196 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20268" },
2197 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20269" },
2198 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20270" },
2199 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20271" },
2200 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20275" },
2201 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "Promise PDC20276" },
2202 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20277" },
2203 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20318" },
2204 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20319" },
2205 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20371" },
2206 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20375" },
2207 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20376" },
2208 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20377" },
2209 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20378" },
2210 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20379" },
2211 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20571" },
2212 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20575" },
2213 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20579" },
2214 { ATA_PDC20580, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20580" },
2215 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20617" },
2216 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20618" },
2217 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20619" },
2218 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20620" },
2219 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "Promise PDC20621" },
2220 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "Promise PDC20622" },
2221 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40518" },
2222 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40519" },
2223 { 0, 0, 0, 0, 0, 0}};
2225 uintptr_t devid = 0;
2227 if (!(idx = ata_match_chip(dev, ids)))
2230 /* if we are on a SuperTrak SX6000 dont attach */
2231 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
2232 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
2233 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
2234 devid == ATA_I960RM)
2237 strcpy(buffer, idx->text);
2239 /* if we are on a FastTrak TX4, adjust the interrupt resource */
2240 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
2241 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
2242 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
2243 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
2244 static long start = 0, end = 0;
2246 if (pci_get_slot(dev) == 1) {
2247 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
2248 strcat(buffer, " (channel 0+1)");
2250 else if (pci_get_slot(dev) == 2 && start && end) {
2251 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
2253 strcat(buffer, " (channel 2+3)");
2259 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
2260 device_set_desc_copy(dev, buffer);
2262 ctlr->chipinit = ata_promise_chipinit;
2267 ata_promise_chipinit(device_t dev)
2269 struct ata_pci_controller *ctlr = device_get_softc(dev);
2270 int rid = ATA_IRQ_RID;
2272 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2273 RF_SHAREABLE | RF_ACTIVE))) {
2274 device_printf(dev, "unable to map interrupt\n");
2278 if (ctlr->chip->max_dma >= ATA_SA150)
2279 ctlr->setmode = ata_sata_setmode;
2281 ctlr->setmode = ata_promise_setmode;
2283 switch (ctlr->chip->cfg1) {
2286 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
2288 ctlr->dmainit = ata_promise_new_dmainit;
2292 /* enable burst mode */
2293 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
2295 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2296 ata_promise_old_intr, ctlr, &ctlr->handle))) {
2297 device_printf(dev, "unable to setup interrupt\n");
2303 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2304 ata_promise_tx2_intr, ctlr, &ctlr->handle))) {
2305 device_printf(dev, "unable to setup interrupt\n");
2311 // if (ctlr->r_res1)
2312 // bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
2313 ctlr->r_type1 = SYS_RES_MEMORY;
2314 ctlr->r_rid1 = PCIR_BAR(4);
2315 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
2316 &ctlr->r_rid1, RF_ACTIVE)))
2319 ctlr->r_type2 = SYS_RES_MEMORY;
2320 ctlr->r_rid2 = PCIR_BAR(3);
2321 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2322 &ctlr->r_rid2, RF_ACTIVE))){
2323 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
2326 ctlr->reset = ata_promise_mio_reset;
2327 ctlr->dmainit = ata_promise_mio_dmainit;
2328 ctlr->allocate = ata_promise_mio_allocate;
2330 if (ctlr->chip->cfg2 == PRSX4X) {
2331 struct ata_promise_sx4 *hpkt;
2332 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
2334 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2335 ata_promise_sx4_intr, ctlr, &ctlr->handle))) {
2336 device_printf(dev, "unable to setup interrupt\n");
2337 /* XXX SOS release resources */
2341 /* print info about cache memory */
2342 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
2343 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
2344 ((dimm >> 24) & 0xff),
2345 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
2346 " ECC enabled" : "" );
2348 /* adjust cache memory parameters */
2349 ATA_OUTL(ctlr->r_res2, 0x000c000c,
2350 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
2352 /* setup host packet controls */
2353 hpkt = malloc(sizeof(struct ata_promise_sx4),
2354 M_TEMP, M_NOWAIT | M_ZERO);
2355 mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
2356 TAILQ_INIT(&hpkt->queue);
2357 hpkt->busy = 0; //hpkt->head = hpkt->tail = 0;
2358 device_set_ivars(dev, hpkt);
2363 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2364 ata_promise_mio_intr, ctlr, &ctlr->handle))) {
2365 device_printf(dev, "unable to setup interrupt\n");
2366 /* XXX SOS release resources */
2370 switch (ctlr->chip->cfg2) {
2372 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
2373 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
2377 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
2378 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 3;
2382 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
2387 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
2392 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
2397 /* XXX SOS release resources */
2401 /* XXX SOS release resources */
2408 ata_promise_mio_allocate(device_t dev)
2410 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2411 struct ata_channel *ch = device_get_softc(dev);
2412 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
2415 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2416 ch->r_io[i].res = ctlr->r_res2;
2417 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
2419 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
2420 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
2421 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
2422 ata_default_registers(dev);
2423 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
2424 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
2425 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2426 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
2427 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2428 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
2429 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2430 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
2431 ch->flags |= ATA_NO_SLAVE;
2433 ch->flags |= ATA_USE_16BIT;
2435 ata_generic_hw(dev);
2437 ch->hw.command = ata_promise_sx4_command;
2439 ch->hw.command = ata_promise_mio_command;
2444 ata_promise_mio_intr(void *data)
2446 struct ata_pci_controller *ctlr = data;
2447 struct ata_channel *ch;
2448 u_int32_t vector = 0, status = 0;
2451 switch (ctlr->chip->cfg2) {
2454 /* read and acknowledge interrupt(s) */
2455 vector = ATA_INL(ctlr->r_res2, 0x040);
2457 /* read and clear interface status */
2458 status = ATA_INL(ctlr->r_res2, 0x06c);
2459 ATA_OUTL(ctlr->r_res2, 0x06c, status & 0x000000ff);
2464 /* read and acknowledge interrupt(s) */
2465 vector = ATA_INL(ctlr->r_res2, 0x040);
2466 ATA_OUTL(ctlr->r_res2, 0x040, vector & 0x0000ffff);
2468 /* read and clear interface status */
2469 status = ATA_INL(ctlr->r_res2, 0x060);
2470 ATA_OUTL(ctlr->r_res2, 0x060, status & 0x000000ff);
2474 for (unit = 0; unit < ctlr->channels; unit++) {
2476 if ((ch = ctlr->interrupt[unit].argument)) {
2477 struct ata_connect_task *tp;
2479 /* check for and handle disconnect events */
2480 if ((status & (0x00000001 << unit)) &&
2481 (tp = (struct ata_connect_task *)
2482 malloc(sizeof(struct ata_connect_task),
2483 M_ATA, M_NOWAIT | M_ZERO))) {
2486 device_printf(ch->dev, "DISCONNECT requested\n");
2487 tp->action = ATA_C_DETACH;
2489 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2490 taskqueue_enqueue(taskqueue_thread, &tp->task);
2493 /* check for and handle connect events */
2494 if ((status & (0x00000010 << unit)) &&
2495 (tp = (struct ata_connect_task *)
2496 malloc(sizeof(struct ata_connect_task),
2497 M_ATA, M_NOWAIT | M_ZERO))) {
2500 device_printf(ch->dev, "CONNECT requested\n");
2501 tp->action = ATA_C_ATTACH;
2503 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2504 taskqueue_enqueue(taskqueue_thread, &tp->task);
2507 /* active interrupt(s) need to call the interrupt handler */
2508 if (vector & (1 << (unit + 1)))
2509 if ((ch = ctlr->interrupt[unit].argument))
2510 ctlr->interrupt[unit].function(ch);
2516 ata_promise_sx4_intr(void *data)
2518 struct ata_pci_controller *ctlr = data;
2519 struct ata_channel *ch;
2520 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
2523 for (unit = 0; unit < ctlr->channels; unit++) {
2524 if (vector & (1 << (unit + 1)))
2525 if ((ch = ctlr->interrupt[unit].argument))
2526 ctlr->interrupt[unit].function(ch);
2527 if (vector & (1 << (unit + 5)))
2528 if ((ch = ctlr->interrupt[unit].argument))
2529 ata_promise_queue_hpkt(ctlr,
2530 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
2531 ATA_PDC_HPKT_OFFSET));
2532 if (vector & (1 << (unit + 9))) {
2533 ata_promise_next_hpkt(ctlr);
2534 if ((ch = ctlr->interrupt[unit].argument))
2535 ctlr->interrupt[unit].function(ch);
2537 if (vector & (1 << (unit + 13))) {
2538 ata_promise_next_hpkt(ctlr);
2539 if ((ch = ctlr->interrupt[unit].argument))
2540 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2541 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
2542 ATA_PDC_APKT_OFFSET));
2548 ata_promise_mio_dmainit(device_t dev)
2550 struct ata_channel *ch = device_get_softc(dev);
2554 /* note start and stop are not used here */
2559 ata_promise_mio_reset(device_t dev)
2561 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2562 struct ata_channel *ch = device_get_softc(dev);
2563 struct ata_promise_sx4 *hpktp;
2565 switch (ctlr->chip->cfg2) {
2568 /* softreset channel ATA module */
2569 hpktp = device_get_ivars(ctlr->dev);
2570 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
2572 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
2573 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
2574 ~0x00003f9f) | (ch->unit + 1));
2576 /* softreset HOST module */ /* XXX SOS what about other outstandings */
2577 mtx_lock(&hpktp->mtx);
2578 ATA_OUTL(ctlr->r_res2, 0xc012c,
2579 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
2581 ATA_OUTL(ctlr->r_res2, 0xc012c,
2582 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
2584 mtx_unlock(&hpktp->mtx);
2585 ata_generic_reset(dev);
2590 if ((ctlr->chip->cfg2 == PRSATA) ||
2591 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2593 /* mask plug/unplug intr */
2594 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
2597 /* softreset channels ATA module */
2598 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2600 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2601 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2602 ~0x00003f9f) | (ch->unit + 1));
2604 if ((ctlr->chip->cfg2 == PRSATA) ||
2605 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2607 ata_sata_phy_enable(ch);
2609 /* reset and enable plug/unplug intr */
2610 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
2613 ata_generic_reset(dev);
2618 if ((ctlr->chip->cfg2 == PRSATA2) ||
2619 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2620 /* set portmultiplier port */
2621 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
2623 /* mask plug/unplug intr */
2624 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
2627 /* softreset channels ATA module */
2628 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2630 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2631 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2632 ~0x00003f9f) | (ch->unit + 1));
2634 if ((ctlr->chip->cfg2 == PRSATA2) ||
2635 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2637 /* set PHY mode to "improved" */
2638 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
2639 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
2640 ~0x00000003) | 0x00000001);
2642 ata_sata_phy_enable(ch);
2644 /* reset and enable plug/unplug intr */
2645 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
2647 /* set portmultiplier port */
2648 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
2651 ata_generic_reset(dev);
2658 ata_promise_mio_command(struct ata_request *request)
2660 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2661 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2662 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
2664 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
2666 /* XXX SOS add ATAPI commands support later */
2667 switch (request->u.ata.command) {
2669 return ata_generic_command(request);
2672 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
2676 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
2679 wordp[1] = htole32(ch->dma->sg_bus);
2681 ata_promise_apkt((u_int8_t*)wordp, request);
2683 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
2688 ata_promise_sx4_command(struct ata_request *request)
2690 device_t gparent = GRANDPARENT(request->dev);
2691 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2692 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2693 struct ata_dma_prdentry *prd = ch->dma->sg;
2694 caddr_t window = rman_get_virtual(ctlr->r_res1);
2696 int i, idx, length = 0;
2698 /* XXX SOS add ATAPI commands support later */
2699 switch (request->u.ata.command) {
2704 case ATA_ATA_IDENTIFY:
2709 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2710 return ata_generic_command(request);
2712 case ATA_SETFEATURES:
2713 case ATA_FLUSHCACHE:
2716 wordp = (u_int32_t *)
2717 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2718 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
2721 ata_promise_apkt((u_int8_t *)wordp, request);
2722 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2723 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2724 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2725 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
2730 wordp = (u_int32_t *)
2731 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
2734 wordp[idx++] = prd[i].addr;
2735 wordp[idx++] = prd[i].count;
2736 length += (prd[i].count & ~ATA_DMA_EOT);
2737 } while (!(prd[i++].count & ATA_DMA_EOT));
2739 wordp = (u_int32_t *)
2740 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
2741 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2742 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
2744 wordp = (u_int32_t *)
2745 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
2746 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2747 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
2749 wordp = (u_int32_t *)
2750 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
2751 if (request->flags & ATA_R_READ)
2752 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
2753 if (request->flags & ATA_R_WRITE)
2754 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
2755 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
2756 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
2759 wordp = (u_int32_t *)
2760 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2761 if (request->flags & ATA_R_READ)
2762 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
2763 if (request->flags & ATA_R_WRITE)
2764 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
2765 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
2767 ata_promise_apkt((u_int8_t *)wordp, request);
2768 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2770 if (request->flags & ATA_R_READ) {
2771 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
2772 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
2773 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2774 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
2776 if (request->flags & ATA_R_WRITE) {
2777 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
2778 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
2779 ata_promise_queue_hpkt(ctlr,
2780 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
2787 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
2789 struct ata_device *atadev = device_get_softc(request->dev);
2793 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
2794 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
2795 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
2796 bytep[i++] = ATA_A_4BIT;
2798 command = ata_modify_if_48bit(request);
2800 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2801 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
2802 bytep[i++] = request->u.ata.feature >> 8;
2803 bytep[i++] = request->u.ata.feature;
2804 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
2805 bytep[i++] = request->u.ata.count >> 8;
2806 bytep[i++] = request->u.ata.count;
2807 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
2808 bytep[i++] = request->u.ata.lba >> 24;
2809 bytep[i++] = request->u.ata.lba;
2810 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
2811 bytep[i++] = request->u.ata.lba >> 32;
2812 bytep[i++] = request->u.ata.lba >> 8;
2813 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
2814 bytep[i++] = request->u.ata.lba >> 40;
2815 bytep[i++] = request->u.ata.lba >> 16;
2816 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
2817 bytep[i++] = ATA_D_LBA | atadev->unit;
2820 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
2821 bytep[i++] = request->u.ata.feature;
2822 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
2823 bytep[i++] = request->u.ata.count;
2824 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
2825 bytep[i++] = request->u.ata.lba;
2826 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
2827 bytep[i++] = request->u.ata.lba >> 8;
2828 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
2829 bytep[i++] = request->u.ata.lba >> 16;
2830 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
2831 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
2832 ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
2834 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
2835 bytep[i++] = command;
2840 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
2842 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
2844 mtx_lock(&hpktp->mtx);
2846 struct host_packet *hp =
2847 malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
2849 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
2853 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
2855 mtx_unlock(&hpktp->mtx);
2859 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
2861 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
2862 struct host_packet *hp;
2864 mtx_lock(&hpktp->mtx);
2865 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
2866 TAILQ_REMOVE(&hpktp->queue, hp, chain);
2867 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
2872 mtx_unlock(&hpktp->mtx);
2876 ata_promise_tx2_intr(void *data)
2878 struct ata_pci_controller *ctlr = data;
2879 struct ata_channel *ch;
2882 for (unit = 0; unit < ctlr->channels; unit++) {
2883 if (!(ch = ctlr->interrupt[unit].argument))
2885 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
2886 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
2887 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2888 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2890 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2891 ATA_BMSTAT_INTERRUPT)
2893 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2896 ctlr->interrupt[unit].function(ch);
2902 ata_promise_old_intr(void *data)
2904 struct ata_pci_controller *ctlr = data;
2905 struct ata_channel *ch;
2908 for (unit = 0; unit < ctlr->channels; unit++) {
2909 if (!(ch = ctlr->interrupt[unit].argument))
2911 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)){
2912 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2913 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2915 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2916 ATA_BMSTAT_INTERRUPT)
2918 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2921 ctlr->interrupt[unit].function(ch);
2927 ata_promise_new_dmastart(device_t dev)
2929 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2930 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2931 struct ata_device *atadev = device_get_softc(dev);
2933 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2934 ATA_OUTB(ctlr->r_res1, 0x11,
2935 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
2936 ATA_OUTL(ctlr->r_res1, 0x20,
2937 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
2938 (ch->dma->cur_iosize >> 1));
2940 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
2941 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
2942 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
2943 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2944 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
2945 ATA_BMCMD_START_STOP);
2946 ch->flags |= ATA_DMA_ACTIVE;
2951 ata_promise_new_dmastop(device_t dev)
2953 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2954 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2955 struct ata_device *atadev = device_get_softc(dev);
2958 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2959 ATA_OUTB(ctlr->r_res1, 0x11,
2960 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
2961 ATA_OUTL(ctlr->r_res1, 0x20, 0);
2963 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
2964 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2965 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
2966 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
2967 ch->flags &= ~ATA_DMA_ACTIVE;
2972 ata_promise_new_dmareset(device_t dev)
2974 struct ata_channel *ch = device_get_softc(dev);
2976 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2977 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
2978 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
2979 ch->flags &= ~ATA_DMA_ACTIVE;
2983 ata_promise_new_dmainit(device_t dev)
2985 struct ata_channel *ch = device_get_softc(dev);
2989 ch->dma->start = ata_promise_new_dmastart;
2990 ch->dma->stop = ata_promise_new_dmastop;
2991 ch->dma->reset = ata_promise_new_dmareset;
2996 ata_promise_setmode(device_t dev, int mode)
2998 device_t gparent = GRANDPARENT(dev);
2999 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3000 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3001 struct ata_device *atadev = device_get_softc(dev);
3002 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3004 u_int32_t timings33[][2] = {
3005 /* PROLD PRNEW mode */
3006 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
3007 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
3008 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
3009 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
3010 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
3011 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
3012 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
3013 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
3014 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
3015 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
3016 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
3017 { 0, 0x00424ef6 }, /* UDMA 3 */
3018 { 0, 0x004127f3 }, /* UDMA 4 */
3019 { 0, 0x004127f3 } /* UDMA 5 */
3022 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3024 switch (ctlr->chip->cfg1) {
3027 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
3028 (ch->unit ? 1 << 11 : 1 << 10))) {
3029 ata_print_cable(dev, "controller");
3032 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
3033 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
3037 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3038 if (mode > ATA_UDMA2 &&
3039 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
3040 ata_print_cable(dev, "controller");
3046 if (mode > ATA_UDMA2 &&
3047 (ATA_INL(ctlr->r_res2,
3048 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
3049 (ch->unit << 7)) & 0x01000000)) {
3050 ata_print_cable(dev, "controller");
3056 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3059 device_printf(dev, "%ssetting %s on %s chip\n",
3060 (error) ? "FAILURE " : "",
3061 ata_mode2str(mode), ctlr->chip->text);
3063 if (ctlr->chip->cfg1 < PRTX)
3064 pci_write_config(gparent, 0x60 + (devno << 2),
3065 timings33[ctlr->chip->cfg1][ata_mode2idx(mode)],4);
3066 atadev->mode = mode;
3073 * ServerWorks chipset support functions
3076 ata_serverworks_ident(device_t dev)
3078 struct ata_pci_controller *ctlr = device_get_softc(dev);
3079 struct ata_chip_id *idx;
3080 static struct ata_chip_id ids[] =
3081 {{ ATA_ROSB4, 0x00, SWKS33, 0x00, ATA_UDMA2, "ServerWorks ROSB4" },
3082 { ATA_CSB5, 0x92, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB5" },
3083 { ATA_CSB5, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB5" },
3084 { ATA_CSB6, 0x00, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB6" },
3085 { ATA_CSB6_1, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB6" },
3086 { 0, 0, 0, 0, 0, 0}};
3089 if (!(idx = ata_match_chip(dev, ids)))
3092 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3093 device_set_desc_copy(dev, buffer);
3095 ctlr->chipinit = ata_serverworks_chipinit;
3100 ata_serverworks_chipinit(device_t dev)
3102 struct ata_pci_controller *ctlr = device_get_softc(dev);
3104 if (ata_setup_interrupt(dev))
3107 if (ctlr->chip->cfg1 == SWKS33) {
3111 /* locate the ISA part in the southbridge and enable UDMA33 */
3112 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
3113 for (i = 0; i < nchildren; i++) {
3114 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
3115 pci_write_config(children[i], 0x64,
3116 (pci_read_config(children[i], 0x64, 4) &
3117 ~0x00002000) | 0x00004000, 4);
3121 free(children, M_TEMP);
3125 pci_write_config(dev, 0x5a,
3126 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
3127 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
3129 ctlr->setmode = ata_serverworks_setmode;
3134 ata_serverworks_setmode(device_t dev, int mode)
3136 device_t gparent = GRANDPARENT(dev);
3137 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3138 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3139 struct ata_device *atadev = device_get_softc(dev);
3140 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3141 int offset = (devno ^ 0x01) << 3;
3143 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
3144 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
3145 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
3147 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3149 mode = ata_check_80pin(dev, mode);
3151 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3154 device_printf(dev, "%ssetting %s on %s chip\n",
3155 (error) ? "FAILURE " : "",
3156 ata_mode2str(mode), ctlr->chip->text);
3158 if (mode >= ATA_UDMA0) {
3159 pci_write_config(gparent, 0x56,
3160 (pci_read_config(gparent, 0x56, 2) &
3161 ~(0xf << (devno << 2))) |
3162 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
3163 pci_write_config(gparent, 0x54,
3164 pci_read_config(gparent, 0x54, 1) |
3165 (0x01 << devno), 1);
3166 pci_write_config(gparent, 0x44,
3167 (pci_read_config(gparent, 0x44, 4) &
3168 ~(0xff << offset)) |
3169 (dmatimings[2] << offset), 4);
3171 else if (mode >= ATA_WDMA0) {
3172 pci_write_config(gparent, 0x54,
3173 pci_read_config(gparent, 0x54, 1) &
3174 ~(0x01 << devno), 1);
3175 pci_write_config(gparent, 0x44,
3176 (pci_read_config(gparent, 0x44, 4) &
3177 ~(0xff << offset)) |
3178 (dmatimings[mode & ATA_MODE_MASK] << offset),4);
3181 pci_write_config(gparent, 0x54,
3182 pci_read_config(gparent, 0x54, 1) &
3183 ~(0x01 << devno), 1);
3185 pci_write_config(gparent, 0x40,
3186 (pci_read_config(gparent, 0x40, 4) &
3187 ~(0xff << offset)) |
3188 (piotimings[ata_mode2idx(mode)] << offset), 4);
3189 atadev->mode = mode;
3195 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
3198 ata_sii_ident(device_t dev)
3200 struct ata_pci_controller *ctlr = device_get_softc(dev);
3201 struct ata_chip_id *idx;
3202 static struct ata_chip_id ids[] =
3203 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
3204 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
3205 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
3206 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
3207 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
3208 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
3209 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
3210 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
3211 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
3212 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
3213 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
3214 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
3215 { 0, 0, 0, 0, 0, 0}};
3218 if (!(idx = ata_match_chip(dev, ids)))
3221 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3222 device_set_desc_copy(dev, buffer);
3224 ctlr->chipinit = ata_sii_chipinit;
3229 ata_sii_chipinit(device_t dev)
3231 struct ata_pci_controller *ctlr = device_get_softc(dev);
3232 int rid = ATA_IRQ_RID;
3234 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3235 RF_SHAREABLE | RF_ACTIVE))) {
3236 device_printf(dev, "unable to map interrupt\n");
3240 if (ctlr->chip->cfg1 == SIIMEMIO) {
3241 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3242 ata_sii_intr, ctlr, &ctlr->handle))) {
3243 device_printf(dev, "unable to setup interrupt\n");
3247 ctlr->r_type2 = SYS_RES_MEMORY;
3248 ctlr->r_rid2 = PCIR_BAR(5);
3249 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3250 &ctlr->r_rid2, RF_ACTIVE)))
3253 if (ctlr->chip->cfg2 & SIISETCLK) {
3254 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
3255 pci_write_config(dev, 0x8a,
3256 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
3257 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
3258 device_printf(dev, "%s could not set ATA133 clock\n",
3262 /* if we have 4 channels enable the second set */
3263 if (ctlr->chip->cfg2 & SII4CH) {
3264 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
3268 /* enable PCI interrupt as BIOS might not */
3269 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
3271 /* dont block interrupts from any channel */
3272 pci_write_config(dev, 0x48,
3273 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
3275 ctlr->allocate = ata_sii_allocate;
3276 if (ctlr->chip->max_dma >= ATA_SA150) {
3277 ctlr->reset = ata_sii_reset;
3278 ctlr->setmode = ata_sata_setmode;
3281 ctlr->setmode = ata_sii_setmode;
3284 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3285 ctlr->chip->cfg2 & SIIINTR ?
3286 ata_cmd_intr : ata_cmd_old_intr,
3287 ctlr, &ctlr->handle))) {
3288 device_printf(dev, "unable to setup interrupt\n");
3292 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
3293 device_printf(dev, "HW has secondary channel disabled\n");
3297 /* enable interrupt as BIOS might not */
3298 pci_write_config(dev, 0x71, 0x01, 1);
3300 ctlr->setmode = ata_cmd_setmode;
3306 ata_sii_allocate(device_t dev)
3308 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3309 struct ata_channel *ch = device_get_softc(dev);
3310 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
3313 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
3314 ch->r_io[i].res = ctlr->r_res2;
3315 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
3317 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
3318 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
3319 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
3320 ata_default_registers(dev);
3321 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
3322 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
3323 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
3324 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
3325 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
3326 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
3327 ch->r_io[ATA_BMDEVSPEC_0].res = ctlr->r_res2;
3328 ch->r_io[ATA_BMDEVSPEC_0].offset = 0xa1 + (unit01 << 6) + (unit10 << 8);
3330 if (ctlr->chip->max_dma >= ATA_SA150) {
3331 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3332 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
3333 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3334 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
3335 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3336 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
3337 ch->flags |= ATA_NO_SLAVE;
3339 /* enable PHY state change interrupt */
3340 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
3343 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
3344 /* work around errata in early chips */
3345 ch->dma->boundary = 16 * DEV_BSIZE;
3346 ch->dma->max_iosize = 15 * DEV_BSIZE;
3349 ata_generic_hw(dev);
3354 ata_sii_intr(void *data)
3356 struct ata_pci_controller *ctlr = data;
3357 struct ata_channel *ch;
3360 for (unit = 0; unit < ctlr->channels; unit++) {
3361 if (!(ch = ctlr->interrupt[unit].argument))
3364 /* check for PHY related interrupts on SATA capable HW */
3365 if (ctlr->chip->max_dma >= ATA_SA150) {
3366 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
3367 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
3368 struct ata_connect_task *tp;
3371 /* clear error bits/interrupt */
3372 ATA_IDX_OUTL(ch, ATA_SERROR, error);
3374 /* if we have a connection event deal with it */
3375 if ((error & ATA_SE_PHY_CHANGED) &&
3376 (tp = (struct ata_connect_task *)
3377 malloc(sizeof(struct ata_connect_task),
3378 M_ATA, M_NOWAIT | M_ZERO))) {
3380 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
3381 device_printf(ch->dev, "CONNECT requested\n");
3382 tp->action = ATA_C_ATTACH;
3385 device_printf(ch->dev, "DISCONNECT requested\n");
3386 tp->action = ATA_C_DETACH;
3389 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3390 taskqueue_enqueue(taskqueue_thread, &tp->task);
3395 /* any drive action to take care of ? */
3396 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_0) & 0x08) {
3397 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3398 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3400 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
3402 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3405 ctlr->interrupt[unit].function(ch);
3412 ata_cmd_intr(void *data)
3414 struct ata_pci_controller *ctlr = data;
3415 struct ata_channel *ch;
3419 for (unit = 0; unit < ctlr->channels; unit++) {
3420 if (!(ch = ctlr->interrupt[unit].argument))
3422 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
3423 (ch->unit ? 0x08 : 0x04))) {
3424 pci_write_config(device_get_parent(ch->dev), 0x71,
3425 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
3426 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3427 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3429 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3430 ATA_BMSTAT_INTERRUPT)
3432 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3435 ctlr->interrupt[unit].function(ch);
3441 ata_cmd_old_intr(void *data)
3443 struct ata_pci_controller *ctlr = data;
3444 struct ata_channel *ch;
3447 for (unit = 0; unit < ctlr->channels; unit++) {
3448 if (!(ch = ctlr->interrupt[unit].argument))
3450 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3451 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3453 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3454 ATA_BMSTAT_INTERRUPT)
3456 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3459 ctlr->interrupt[unit].function(ch);
3464 ata_sii_reset(device_t dev)
3466 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3467 struct ata_channel *ch = device_get_softc(dev);
3468 int offset = ((ch->unit & 1) << 7) + ((ch->unit & 2) << 8);
3470 /* disable PHY state change interrupt */
3471 ATA_OUTL(ctlr->r_res2, 0x148 + offset, ~(1 << 16));
3473 ata_sata_phy_enable(ch);
3475 /* enable PHY state change interrupt */
3476 ATA_OUTL(ctlr->r_res2, 0x148 + offset, (1 << 16));
3480 ata_sii_setmode(device_t dev, int mode)
3482 device_t gparent = GRANDPARENT(dev);
3483 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3484 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3485 struct ata_device *atadev = device_get_softc(dev);
3486 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
3487 int mreg = ch->unit ? 0x84 : 0x80;
3488 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
3489 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
3492 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3494 if (ctlr->chip->cfg2 & SIISETCLK) {
3495 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
3496 (ch->unit ? 0x02 : 0x01))) {
3497 ata_print_cable(dev, "controller");
3502 mode = ata_check_80pin(dev, mode);
3504 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3507 device_printf(dev, "%ssetting %s on %s chip\n",
3508 (error) ? "FAILURE " : "",
3509 ata_mode2str(mode), ctlr->chip->text);
3513 if (mode >= ATA_UDMA0) {
3514 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
3515 u_int8_t ureg = 0xac + rego;
3517 pci_write_config(gparent, mreg,
3518 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
3519 pci_write_config(gparent, ureg,
3520 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
3521 udmatimings[mode & ATA_MODE_MASK], 1);
3524 else if (mode >= ATA_WDMA0) {
3525 u_int8_t dreg = 0xa8 + rego;
3526 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
3528 pci_write_config(gparent, mreg,
3529 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
3530 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
3534 u_int8_t preg = 0xa4 + rego;
3535 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
3537 pci_write_config(gparent, mreg,
3538 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
3539 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
3541 atadev->mode = mode;
3545 ata_cmd_setmode(device_t dev, int mode)
3547 device_t gparent = GRANDPARENT(dev);
3548 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3549 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3550 struct ata_device *atadev = device_get_softc(dev);
3551 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3554 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3556 mode = ata_check_80pin(dev, mode);
3558 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3561 device_printf(dev, "%ssetting %s on %s chip\n",
3562 (error) ? "FAILURE " : "",
3563 ata_mode2str(mode), ctlr->chip->text);
3565 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
3566 int ureg = ch->unit ? 0x7b : 0x73;
3568 if (mode >= ATA_UDMA0) {
3569 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
3570 { 0x11, 0x42 }, { 0x25, 0x8a },
3571 { 0x15, 0x4a }, { 0x05, 0x0a } };
3573 u_int8_t umode = pci_read_config(gparent, ureg, 1);
3575 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
3576 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
3577 pci_write_config(gparent, ureg, umode, 1);
3579 else if (mode >= ATA_WDMA0) {
3580 int dmatimings[] = { 0x87, 0x32, 0x3f };
3582 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
3583 pci_write_config(gparent, ureg,
3584 pci_read_config(gparent, ureg, 1) &
3585 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3588 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
3589 pci_write_config(gparent, treg,
3590 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
3591 pci_write_config(gparent, ureg,
3592 pci_read_config(gparent, ureg, 1) &
3593 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3595 atadev->mode = mode;
3601 * Silicon Integrated Systems Corp. (SiS) chipset support functions
3604 ata_sis_ident(device_t dev)
3606 struct ata_pci_controller *ctlr = device_get_softc(dev);
3607 struct ata_chip_id *idx;
3608 static struct ata_chip_id ids[] =
3609 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "SiS 182" }, /* south */
3610 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "SiS 181" }, /* south */
3611 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "SiS 180" }, /* south */
3612 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 965" }, /* south */
3613 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 964" }, /* south */
3614 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 963" }, /* south */
3615 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 962" }, /* south */
3617 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 745" }, /* 1chip */
3618 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 735" }, /* 1chip */
3619 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 733" }, /* 1chip */
3620 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "SiS 730" }, /* 1chip */
3622 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 635" }, /* 1chip */
3623 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 633" }, /* unknown */
3624 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "SiS 630S"}, /* 1chip */
3625 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "SiS 630" }, /* 1chip */
3626 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "SiS 620" }, /* 1chip */
3628 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "SiS 550" },
3629 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "SiS 540" },
3630 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "SiS 530" },
3632 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "SiS 5513" },
3633 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "SiS 5513" },
3634 { 0, 0, 0, 0, 0, 0 }};
3638 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
3641 if (idx->cfg2 && !found) {
3642 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
3644 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
3645 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
3647 idx->cfg1 = SIS133NEW;
3648 idx->max_dma = ATA_UDMA6;
3649 sprintf(buffer, "SiS 962/963 %s controller",
3650 ata_mode2str(idx->max_dma));
3652 pci_write_config(dev, 0x57, reg57, 1);
3654 if (idx->cfg2 && !found) {
3655 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
3657 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
3658 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
3659 struct ata_chip_id id[] =
3660 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
3663 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
3664 idx->cfg1 = SIS133OLD;
3665 idx->max_dma = ATA_UDMA6;
3668 idx->cfg1 = SIS100NEW;
3669 idx->max_dma = ATA_UDMA5;
3671 sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
3673 pci_write_config(dev, 0x4a, reg4a, 1);
3676 sprintf(buffer,"%s %s controller",idx->text,ata_mode2str(idx->max_dma));
3678 device_set_desc_copy(dev, buffer);
3680 ctlr->chipinit = ata_sis_chipinit;
3685 ata_sis_chipinit(device_t dev)
3687 struct ata_pci_controller *ctlr = device_get_softc(dev);
3689 if (ata_setup_interrupt(dev))
3692 switch (ctlr->chip->cfg1) {
3697 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
3701 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
3704 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
3705 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
3708 ctlr->r_type2 = SYS_RES_IOPORT;
3709 ctlr->r_rid2 = PCIR_BAR(5);
3710 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3711 &ctlr->r_rid2, RF_ACTIVE))) {
3712 pci_write_config(dev, PCIR_COMMAND,
3713 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3714 ctlr->allocate = ata_sis_allocate;
3715 ctlr->reset = ata_sis_reset;
3717 ctlr->setmode = ata_sata_setmode;
3722 ctlr->setmode = ata_sis_setmode;
3727 ata_sis_allocate(device_t dev)
3729 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3730 struct ata_channel *ch = device_get_softc(dev);
3732 /* setup the usual register normal pci style */
3733 ata_pci_allocate(dev);
3735 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3736 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 4);
3737 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3738 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 4);
3739 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3740 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 4);
3741 ch->flags |= ATA_NO_SLAVE;
3743 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
3744 /* XXX SOS unknown how to enable PHY state change interrupt */
3749 ata_sis_reset(device_t dev)
3751 struct ata_channel *ch = device_get_softc(dev);
3753 ata_sata_phy_enable(ch);
3758 ata_sis_setmode(device_t dev, int mode)
3760 device_t gparent = GRANDPARENT(dev);
3761 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3762 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3763 struct ata_device *atadev = device_get_softc(dev);
3764 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3767 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3769 if (ctlr->chip->cfg1 == SIS133NEW) {
3770 if (mode > ATA_UDMA2 &&
3771 pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
3772 ata_print_cable(dev, "controller");
3777 if (mode > ATA_UDMA2 &&
3778 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
3779 ata_print_cable(dev, "controller");
3784 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3787 device_printf(dev, "%ssetting %s on %s chip\n",
3788 (error) ? "FAILURE " : "",
3789 ata_mode2str(mode), ctlr->chip->text);
3791 switch (ctlr->chip->cfg1) {
3793 u_int32_t timings[] =
3794 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
3795 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
3796 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
3799 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
3800 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
3804 u_int16_t timings[] =
3805 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
3806 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
3808 u_int16_t reg = 0x40 + (devno << 1);
3810 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3814 u_int16_t timings[] =
3815 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
3816 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
3817 u_int16_t reg = 0x40 + (devno << 1);
3819 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3825 u_int16_t timings[] =
3826 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
3827 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
3828 u_int16_t reg = 0x40 + (devno << 1);
3830 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3834 atadev->mode = mode;
3839 /* VIA Technologies Inc. chipset support functions */
3841 ata_via_ident(device_t dev)
3843 struct ata_pci_controller *ctlr = device_get_softc(dev);
3844 struct ata_chip_id *idx;
3845 static struct ata_chip_id ids[] =
3846 {{ ATA_VIA82C586, 0x02, VIA33, 0x00, ATA_UDMA2, "VIA 82C586B" },
3847 { ATA_VIA82C586, 0x00, VIA33, 0x00, ATA_WDMA2, "VIA 82C586" },
3848 { ATA_VIA82C596, 0x12, VIA66, VIACLK, ATA_UDMA4, "VIA 82C596B" },
3849 { ATA_VIA82C596, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C596" },
3850 { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "VIA 82C686B"},
3851 { ATA_VIA82C686, 0x10, VIA66, VIACLK, ATA_UDMA4, "VIA 82C686A" },
3852 { ATA_VIA82C686, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C686" },
3853 { ATA_VIA8231, 0x00, VIA100, VIABUG, ATA_UDMA5, "VIA 8231" },
3854 { ATA_VIA8233, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233" },
3855 { ATA_VIA8233C, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233C" },
3856 { ATA_VIA8233A, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8233A" },
3857 { ATA_VIA8235, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8235" },
3858 { ATA_VIA8237, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8237" },
3859 { 0, 0, 0, 0, 0, 0 }};
3860 static struct ata_chip_id new_ids[] =
3861 {{ ATA_VIA6410, 0x00, 0, 0x00, ATA_UDMA6, "VIA 6410" },
3862 { ATA_VIA6420, 0x00, 7, 0x00, ATA_SA150, "VIA 6420" },
3863 { ATA_VIA6421, 0x00, 6, 0x00, ATA_SA150, "VIA 6421" },
3864 { 0, 0, 0, 0, 0, 0 }};
3867 if (pci_get_devid(dev) == ATA_VIA82C571) {
3868 if (!(idx = ata_find_chip(dev, ids, -99)))
3872 if (!(idx = ata_match_chip(dev, new_ids)))
3876 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3877 device_set_desc_copy(dev, buffer);
3879 ctlr->chipinit = ata_via_chipinit;
3884 ata_via_chipinit(device_t dev)
3886 struct ata_pci_controller *ctlr = device_get_softc(dev);
3888 if (ata_setup_interrupt(dev))
3891 if (ctlr->chip->max_dma >= ATA_SA150) {
3892 ctlr->r_type2 = SYS_RES_IOPORT;
3893 ctlr->r_rid2 = PCIR_BAR(5);
3894 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3895 &ctlr->r_rid2, RF_ACTIVE))) {
3896 pci_write_config(dev, PCIR_COMMAND,
3897 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3898 ctlr->allocate = ata_via_allocate;
3899 ctlr->reset = ata_via_reset;
3901 ctlr->setmode = ata_sata_setmode;
3905 /* prepare for ATA-66 on the 82C686a and 82C596b */
3906 if (ctlr->chip->cfg2 & VIACLK)
3907 pci_write_config(dev, 0x50, 0x030b030b, 4);
3909 /* the southbridge might need the data corruption fix */
3910 if (ctlr->chip->cfg2 & VIABUG)
3911 ata_via_southbridge_fixup(dev);
3913 /* set fifo configuration half'n'half */
3914 pci_write_config(dev, 0x43,
3915 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
3917 /* set status register read retry */
3918 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
3920 /* set DMA read & end-of-sector fifo flush */
3921 pci_write_config(dev, 0x46,
3922 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
3924 /* set sector size */
3925 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
3926 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
3928 ctlr->setmode = ata_via_family_setmode;
3933 ata_via_allocate(device_t dev)
3935 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3936 struct ata_channel *ch = device_get_softc(dev);
3938 /* setup the usual register normal pci style */
3939 ata_pci_allocate(dev);
3941 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3942 ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
3943 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3944 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
3945 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3946 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
3947 ch->flags |= ATA_NO_SLAVE;
3949 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
3950 /* XXX SOS unknown how to enable PHY state change interrupt */
3955 ata_via_reset(device_t dev)
3957 struct ata_channel *ch = device_get_softc(dev);
3959 ata_sata_phy_enable(ch);
3963 ata_via_southbridge_fixup(device_t dev)
3968 if (device_get_children(device_get_parent(dev), &children, &nchildren))
3971 for (i = 0; i < nchildren; i++) {
3972 if (pci_get_devid(children[i]) == ATA_VIA8363 ||
3973 pci_get_devid(children[i]) == ATA_VIA8371 ||
3974 pci_get_devid(children[i]) == ATA_VIA8662 ||
3975 pci_get_devid(children[i]) == ATA_VIA8361) {
3976 u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
3978 if ((reg76 & 0xf0) != 0xd0) {
3980 "Correcting VIA config for southbridge data corruption bug\n");
3981 pci_write_config(children[i], 0x75, 0x80, 1);
3982 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
3987 free(children, M_TEMP);
3991 /* common code for VIA, AMD & nVidia */
3993 ata_via_family_setmode(device_t dev, int mode)
3995 device_t gparent = GRANDPARENT(dev);
3996 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3997 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3998 struct ata_device *atadev = device_get_softc(dev);
3999 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
4000 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
4002 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* VIA ATA33 */
4003 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* VIA ATA66 */
4004 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* VIA ATA100 */
4005 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
4006 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }}; /* AMD/nVIDIA */
4007 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4008 int reg = 0x53 - devno;
4011 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4013 if (ctlr->chip->cfg2 & AMDCABLE) {
4014 if (mode > ATA_UDMA2 &&
4015 !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
4016 ata_print_cable(dev, "controller");
4021 mode = ata_check_80pin(dev, mode);
4023 if (ctlr->chip->cfg2 & NVIDIA)
4026 if (ctlr->chip->cfg1 != VIA133)
4027 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
4029 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4032 device_printf(dev, "%ssetting %s on %s chip\n",
4033 (error) ? "FAILURE " : "", ata_mode2str(mode),
4036 if (mode >= ATA_UDMA0)
4037 pci_write_config(gparent, reg,
4038 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
4040 pci_write_config(gparent, reg, 0x8b, 1);
4041 atadev->mode = mode;
4046 /* misc functions */
4047 struct ata_chip_id *
4048 ata_match_chip(device_t dev, struct ata_chip_id *index)
4050 while (index->chipid != 0) {
4051 if (pci_get_devid(dev) == index->chipid &&
4052 pci_get_revid(dev) >= index->chiprev)
4059 static struct ata_chip_id *
4060 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
4065 if (device_get_children(device_get_parent(dev), &children, &nchildren))
4068 while (index->chipid != 0) {
4069 for (i = 0; i < nchildren; i++) {
4070 if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
4071 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
4072 pci_get_devid(children[i]) == index->chipid &&
4073 pci_get_revid(children[i]) >= index->chiprev) {
4074 free(children, M_TEMP);
4080 free(children, M_TEMP);
4085 ata_setup_interrupt(device_t dev)
4087 struct ata_pci_controller *ctlr = device_get_softc(dev);
4088 int rid = ATA_IRQ_RID;
4090 if (!ata_legacy(dev)) {
4091 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
4092 RF_SHAREABLE | RF_ACTIVE))) {
4093 device_printf(dev, "unable to map interrupt\n");
4096 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
4097 ata_generic_intr, ctlr, &ctlr->handle))) {
4098 device_printf(dev, "unable to setup interrupt\n");
4105 struct ata_serialize {
4106 struct mtx locked_mtx;
4112 ata_serialize(device_t dev, int flags)
4114 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4115 struct ata_channel *ch = device_get_softc(dev);
4116 struct ata_serialize *serial;
4117 static int inited = 0;
4121 serial = malloc(sizeof(struct ata_serialize),
4122 M_TEMP, M_NOWAIT | M_ZERO);
4123 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
4124 serial->locked_ch = -1;
4125 serial->restart_ch = -1;
4126 device_set_ivars(ctlr->dev, serial);
4130 serial = device_get_ivars(ctlr->dev);
4132 mtx_lock(&serial->locked_mtx);
4135 if (serial->locked_ch == -1)
4136 serial->locked_ch = ch->unit;
4137 if (serial->locked_ch != ch->unit)
4138 serial->restart_ch = ch->unit;
4142 if (serial->locked_ch == ch->unit) {
4143 serial->locked_ch = -1;
4144 if (serial->restart_ch != -1) {
4145 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
4146 serial->restart_ch = -1;
4147 mtx_unlock(&serial->locked_mtx);
4158 res = serial->locked_ch;
4159 mtx_unlock(&serial->locked_mtx);
4164 ata_print_cable(device_t dev, u_int8_t *who)
4167 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
4171 ata_atapi(device_t dev)
4173 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4174 struct ata_device *atadev = device_get_softc(dev);
4176 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
4177 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
4181 ata_check_80pin(device_t dev, int mode)
4183 struct ata_device *atadev = device_get_softc(dev);
4185 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
4186 ata_print_cable(dev, "device");
4193 ata_mode2idx(int mode)
4195 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
4196 return (mode & ATA_MODE_MASK) + 8;
4197 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
4198 return (mode & ATA_MODE_MASK) + 5;
4199 return (mode & ATA_MODE_MASK) - ATA_PIO0;