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;
1544 /* enable PHY state change interrupt */
1545 ATA_OUTL(ctlr->r_res2, 0x4,
1546 ATA_INL(ctlr->r_res2, 0x04) | (0x01 << (ch->unit << 3)));
1551 ata_intel_31244_intr(void *data)
1553 struct ata_pci_controller *ctlr = data;
1554 struct ata_channel *ch;
1557 for (unit = 0; unit < ctlr->channels; unit++) {
1558 if (!(ch = ctlr->interrupt[unit].argument))
1561 /* check for PHY related interrupts on SATA capable HW */
1562 if (ctlr->chip->max_dma >= ATA_SA150) {
1563 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
1564 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
1565 struct ata_connect_task *tp;
1568 /* clear error bits/interrupt */
1569 ATA_IDX_OUTL(ch, ATA_SERROR, error);
1571 /* if we have a connection event deal with it */
1572 if ((error & ATA_SE_PHY_CHANGED) &&
1573 (tp = (struct ata_connect_task *)
1574 malloc(sizeof(struct ata_connect_task),
1575 M_ATA, M_NOWAIT | M_ZERO))) {
1577 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
1578 device_printf(ch->dev, "CONNECT requested\n");
1579 tp->action = ATA_C_ATTACH;
1582 device_printf(ch->dev, "DISCONNECT requested\n");
1583 tp->action = ATA_C_DETACH;
1586 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1587 taskqueue_enqueue(taskqueue_thread, &tp->task);
1592 /* any drive action to take care of ? */
1593 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
1594 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1596 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
1598 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1601 ctlr->interrupt[unit].function(ch);
1606 ata_intel_31244_reset(device_t dev)
1608 struct ata_channel *ch = device_get_softc(dev);
1610 ata_sata_phy_enable(ch);
1614 ata_intel_31244_command(struct ata_request *request)
1616 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
1617 struct ata_device *atadev = device_get_softc(request->dev);
1621 command = ata_modify_if_48bit(request);
1622 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
1623 return (ata_generic_command(request));
1625 lba = request->u.ata.lba;
1626 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | atadev->unit);
1627 /* enable interrupt */
1628 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
1629 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
1630 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
1631 ATA_IDX_OUTW(ch, ATA_SECTOR, ((lba >> 16) & 0xff00) | (lba & 0x00ff));
1632 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((lba >> 24) & 0xff00) | ((lba >> 8) &
1634 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((lba >> 32) & 0xff00) | ((lba >> 16) &
1637 /* issue command to controller */
1638 ATA_IDX_OUTB(ch, ATA_COMMAND, command);
1644 ata_intel_intr(void *data)
1646 struct ata_pci_controller *ctlr = data;
1647 struct ata_channel *ch;
1650 for (unit = 0; unit < ctlr->channels; unit++) {
1651 if (!(ch = ctlr->interrupt[unit].argument))
1654 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1656 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1657 ATA_BMSTAT_INTERRUPT)
1659 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1662 ctlr->interrupt[unit].function(ch);
1667 ata_intel_reset(device_t dev)
1669 device_t parent = device_get_parent(dev);
1670 struct ata_pci_controller *ctlr = device_get_softc(parent);
1671 struct ata_channel *ch = device_get_softc(dev);
1674 /* ICH6 has 4 SATA ports as master/slave on 2 channels so deal with pairs */
1675 if (ctlr->chip->chipid == ATA_I82801FB_S1 ||
1676 ctlr->chip->chipid == ATA_I82801FB_R1 ||
1677 ctlr->chip->chipid == ATA_I82801FB_M) {
1678 mask = (0x0005 << ch->unit);
1681 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1682 if (pci_read_config(parent, 0x90, 1) & 0x04)
1685 mask = (0x0001 << ch->unit);
1686 /* XXX SOS should be in intel_allocate when we grow it */
1687 ch->flags |= ATA_NO_SLAVE;
1690 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1692 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1694 /* wait up to 1 sec for "connect well" */
1695 for (timeout = 0; timeout < 100 ; timeout++) {
1696 if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1697 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1701 ata_generic_reset(dev);
1705 ata_intel_old_setmode(device_t dev, int mode)
1711 ata_intel_new_setmode(device_t dev, int mode)
1713 device_t gparent = GRANDPARENT(dev);
1714 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1715 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1716 struct ata_device *atadev = device_get_softc(dev);
1717 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1718 u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
1719 u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
1720 u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
1721 u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
1722 u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
1723 u_int32_t mask40 = 0, new40 = 0;
1724 u_int8_t mask44 = 0, new44 = 0;
1726 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1727 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1729 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1731 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1732 ata_print_cable(dev, "controller");
1736 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1739 device_printf(dev, "%ssetting %s on %s chip\n",
1740 (error) ? "FAILURE " : "",
1741 ata_mode2str(mode), ctlr->chip->text);
1745 if (mode >= ATA_UDMA0) {
1746 pci_write_config(gparent, 0x48, reg48 | (0x0001 << devno), 2);
1747 pci_write_config(gparent, 0x4a,
1748 (reg4a & ~(0x3 << (devno << 2))) |
1749 ((0x01 + !(mode & 0x01)) << (devno << 2)), 2);
1752 pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
1753 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))), 2);
1756 if (mode >= ATA_UDMA2)
1757 pci_write_config(gparent, 0x54, reg54 | (0x1 << devno), 2);
1759 pci_write_config(gparent, 0x54, reg54 & ~(0x1 << devno), 2);
1761 if (mode >= ATA_UDMA5)
1762 pci_write_config(gparent, 0x54, reg54 | (0x1000 << devno), 2);
1764 pci_write_config(gparent, 0x54, reg54 & ~(0x1000 << devno), 2);
1766 reg40 &= ~0x00ff00ff;
1767 reg40 |= 0x40774077;
1769 if (atadev->unit == ATA_MASTER) {
1771 new40 = timings[ata_mode2idx(mode)] << 8;
1775 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
1776 (timings[ata_mode2idx(mode)] & 0x03);
1784 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
1785 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
1787 atadev->mode = mode;
1792 * Integrated Technology Express Inc. (ITE) chipset support functions
1795 ata_ite_ident(device_t dev)
1797 struct ata_pci_controller *ctlr = device_get_softc(dev);
1799 if (pci_get_devid(dev) == ATA_IT8212F) {
1800 device_set_desc(dev, "ITE IT8212F ATA133 controller");
1801 ctlr->chipinit = ata_ite_chipinit;
1808 ata_ite_chipinit(device_t dev)
1810 struct ata_pci_controller *ctlr = device_get_softc(dev);
1812 if (ata_setup_interrupt(dev))
1815 ctlr->setmode = ata_ite_setmode;
1817 /* set PCI mode and 66Mhz reference clock */
1818 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
1820 /* set default active & recover timings */
1821 pci_write_config(dev, 0x54, 0x31, 1);
1822 pci_write_config(dev, 0x56, 0x31, 1);
1827 ata_ite_setmode(device_t dev, int mode)
1829 device_t gparent = GRANDPARENT(dev);
1830 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1831 struct ata_device *atadev = device_get_softc(dev);
1832 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1835 /* correct the mode for what the HW supports */
1836 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
1838 /* check the CBLID bits for 80 conductor cable detection */
1839 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
1840 (ch->unit ? (1<<3) : (1<<2)))) {
1841 ata_print_cable(dev, "controller");
1845 /* set the wanted mode on the device */
1846 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1849 device_printf(dev, "%s setting %s on ITE8212F chip\n",
1850 (error) ? "failed" : "success", ata_mode2str(mode));
1852 /* if the device accepted the mode change, setup the HW accordingly */
1854 if (mode >= ATA_UDMA0) {
1855 u_int8_t udmatiming[] =
1856 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
1858 /* enable UDMA mode */
1859 pci_write_config(gparent, 0x50,
1860 pci_read_config(gparent, 0x50, 1) &
1861 ~(1 << (devno + 3)), 1);
1863 /* set UDMA timing */
1864 pci_write_config(gparent,
1865 0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
1866 udmatiming[mode & ATA_MODE_MASK], 1);
1869 u_int8_t chtiming[] =
1870 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
1872 /* disable UDMA mode */
1873 pci_write_config(gparent, 0x50,
1874 pci_read_config(gparent, 0x50, 1) |
1875 (1 << (devno + 3)), 1);
1877 /* set active and recover timing (shared between master & slave) */
1878 if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
1879 chtiming[ata_mode2idx(mode)])
1880 pci_write_config(gparent, 0x54 + (ch->unit << 2),
1881 chtiming[ata_mode2idx(mode)], 1);
1883 atadev->mode = mode;
1889 * National chipset support functions
1892 ata_national_ident(device_t dev)
1894 struct ata_pci_controller *ctlr = device_get_softc(dev);
1896 /* this chip is a clone of the Cyrix chip, bugs and all */
1897 if (pci_get_devid(dev) == ATA_SC1100) {
1898 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
1899 ctlr->chipinit = ata_national_chipinit;
1905 static device_t nat_host = NULL;
1908 ata_national_chipinit(device_t dev)
1910 struct ata_pci_controller *ctlr = device_get_softc(dev);
1914 if (ata_setup_interrupt(dev))
1917 /* locate the ISA part in the southbridge and enable UDMA33 */
1918 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
1919 for (i = 0; i < nchildren; i++) {
1920 if (pci_get_devid(children[i]) == 0x0510100b) {
1921 nat_host = children[i];
1925 free(children, M_TEMP);
1927 ctlr->setmode = ata_national_setmode;
1932 ata_national_setmode(device_t dev, int mode)
1934 device_t gparent = GRANDPARENT(dev);
1935 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1936 struct ata_device *atadev = device_get_softc(dev);
1937 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1938 u_int32_t piotiming[] =
1939 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
1940 0x00803020, 0x20102010, 0x00100010,
1941 0x00100010, 0x00100010, 0x00100010 };
1942 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
1943 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
1946 ch->dma->alignment = 16;
1947 ch->dma->max_iosize = 126 * DEV_BSIZE;
1949 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1951 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1954 device_printf(dev, "%s setting %s on National chip\n",
1955 (error) ? "failed" : "success", ata_mode2str(mode));
1957 if (mode >= ATA_UDMA0) {
1958 pci_write_config(gparent, 0x44 + (devno << 3),
1959 udmatiming[mode & ATA_MODE_MASK], 4);
1961 else if (mode >= ATA_WDMA0) {
1962 pci_write_config(gparent, 0x44 + (devno << 3),
1963 dmatiming[mode & ATA_MODE_MASK], 4);
1966 pci_write_config(gparent, 0x44 + (devno << 3),
1967 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
1970 pci_write_config(gparent, 0x40 + (devno << 3),
1971 piotiming[ata_mode2idx(mode)], 4);
1972 atadev->mode = mode;
1978 * nVidia chipset support functions
1981 ata_nvidia_ident(device_t dev)
1983 struct ata_pci_controller *ctlr = device_get_softc(dev);
1984 struct ata_chip_id *idx;
1985 static struct ata_chip_id ids[] =
1986 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nVidia nForce" },
1987 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2" },
1988 { ATA_NFORCE2_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2 MCP" },
1989 { ATA_NFORCE2_MCP_S1, 0, 0, 0, ATA_SA150, "nVidia nForce2 MCP" },
1990 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3" },
1991 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 Pro" },
1992 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
1993 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
1994 { ATA_NFORCE3_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 MCP" },
1995 { ATA_NFORCE3_MCP_S1, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
1996 { ATA_NFORCE3_MCP_S2, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
1997 { ATA_NFORCE4, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce4" },
1998 { ATA_NFORCE4_S1, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce4" },
1999 { ATA_NFORCE4_S2, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce4" },
2000 { 0, 0, 0, 0, 0, 0}};
2003 if (!(idx = ata_match_chip(dev, ids)))
2006 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
2007 device_set_desc_copy(dev, buffer);
2009 ctlr->chipinit = ata_nvidia_chipinit;
2014 ata_nvidia_chipinit(device_t dev)
2016 struct ata_pci_controller *ctlr = device_get_softc(dev);
2018 if (ata_setup_interrupt(dev))
2021 if (ctlr->chip->max_dma >= ATA_SA150) {
2022 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
2023 ctlr->r_type2 = SYS_RES_IOPORT;
2025 ctlr->r_type2 = SYS_RES_MEMORY;
2026 ctlr->r_rid2 = PCIR_BAR(5);
2027 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2028 &ctlr->r_rid2, RF_ACTIVE))) {
2029 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
2031 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
2032 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2033 ata_nvidia_intr, ctlr, &ctlr->handle)) {
2034 device_printf(dev, "unable to setup interrupt\n");
2038 /* enable control access */
2039 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
2041 /* clear interrupt status */
2042 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2044 /* enable device and PHY state change interrupts */
2045 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
2047 /* enable PCI interrupt */
2048 pci_write_config(dev, PCIR_COMMAND,
2049 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
2051 ctlr->allocate = ata_nvidia_allocate;
2052 ctlr->reset = ata_nvidia_reset;
2054 ctlr->setmode = ata_sata_setmode;
2057 /* disable prefetch, postwrite */
2058 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
2059 ctlr->setmode = ata_via_family_setmode;
2065 ata_nvidia_allocate(device_t dev)
2067 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2068 struct ata_channel *ch = device_get_softc(dev);
2070 /* setup the usual register normal pci style */
2071 ata_pci_allocate(dev);
2073 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2074 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
2075 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2076 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
2077 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2078 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
2079 ch->flags |= ATA_NO_SLAVE;
2085 ata_nvidia_intr(void *data)
2087 struct ata_pci_controller *ctlr = data;
2088 struct ata_channel *ch;
2089 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
2093 /* get interrupt status */
2094 status = ATA_INB(ctlr->r_res2, offset);
2096 /* clear interrupt status */
2097 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2099 for (unit = 0; unit < ctlr->channels; unit++) {
2100 if ((ch = ctlr->interrupt[unit].argument)) {
2101 struct ata_connect_task *tp;
2102 int maskshift = ch->unit << 2;
2104 /* clear error status */
2105 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2107 /* check for and handle connect events */
2108 if (((status & (0x0c << maskshift)) == (0x04 << maskshift)) &&
2109 (tp = (struct ata_connect_task *)
2110 malloc(sizeof(struct ata_connect_task),
2111 M_ATA, M_NOWAIT | M_ZERO))) {
2113 device_printf(ch->dev, "CONNECT requested\n");
2114 tp->action = ATA_C_ATTACH;
2116 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2117 taskqueue_enqueue(taskqueue_thread, &tp->task);
2120 /* check for and handle disconnect events */
2121 if ((status & (0x08 << maskshift)) &&
2122 (tp = (struct ata_connect_task *)
2123 malloc(sizeof(struct ata_connect_task),
2124 M_ATA, M_NOWAIT | M_ZERO))) {
2126 device_printf(ch->dev, "DISCONNECT requested\n");
2127 tp->action = ATA_C_DETACH;
2129 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2130 taskqueue_enqueue(taskqueue_thread, &tp->task);
2133 /* any drive action to take care of ? */
2134 if (status & (0x01 << maskshift)) {
2135 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2137 ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2139 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
2141 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat&~ATA_BMSTAT_ERROR);
2144 ctlr->interrupt[unit].function(ch);
2151 ata_nvidia_reset(device_t dev)
2153 struct ata_channel *ch = device_get_softc(dev);
2155 ata_sata_phy_enable(ch);
2160 * Promise chipset support functions
2162 #define ATA_PDC_APKT_OFFSET 0x00000010
2163 #define ATA_PDC_HPKT_OFFSET 0x00000040
2164 #define ATA_PDC_ASG_OFFSET 0x00000080
2165 #define ATA_PDC_LSG_OFFSET 0x000000c0
2166 #define ATA_PDC_HSG_OFFSET 0x00000100
2167 #define ATA_PDC_CHN_OFFSET 0x00000400
2168 #define ATA_PDC_BUF_BASE 0x00400000
2169 #define ATA_PDC_BUF_OFFSET 0x00100000
2170 #define ATA_PDC_MAX_HPKT 8
2171 #define ATA_PDC_WRITE_REG 0x00
2172 #define ATA_PDC_WRITE_CTL 0x0e
2173 #define ATA_PDC_WRITE_END 0x08
2174 #define ATA_PDC_WAIT_NBUSY 0x10
2175 #define ATA_PDC_WAIT_READY 0x18
2176 #define ATA_PDC_1B 0x20
2177 #define ATA_PDC_2B 0x40
2179 struct host_packet {
2181 TAILQ_ENTRY(host_packet) chain;
2184 struct ata_promise_sx4 {
2186 TAILQ_HEAD(, host_packet) queue;
2191 ata_promise_ident(device_t dev)
2193 struct ata_pci_controller *ctlr = device_get_softc(dev);
2194 struct ata_chip_id *idx;
2195 static struct ata_chip_id ids[] =
2196 {{ ATA_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "Promise PDC20246" },
2197 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20262" },
2198 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20263" },
2199 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20265" },
2200 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20267" },
2201 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20268" },
2202 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20269" },
2203 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20270" },
2204 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20271" },
2205 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20275" },
2206 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "Promise PDC20276" },
2207 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20277" },
2208 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20318" },
2209 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20319" },
2210 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20371" },
2211 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20375" },
2212 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20376" },
2213 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20377" },
2214 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20378" },
2215 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20379" },
2216 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20571" },
2217 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20575" },
2218 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20579" },
2219 { ATA_PDC20580, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20580" },
2220 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20617" },
2221 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20618" },
2222 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20619" },
2223 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20620" },
2224 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "Promise PDC20621" },
2225 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "Promise PDC20622" },
2226 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40518" },
2227 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40519" },
2228 { 0, 0, 0, 0, 0, 0}};
2230 uintptr_t devid = 0;
2232 if (!(idx = ata_match_chip(dev, ids)))
2235 /* if we are on a SuperTrak SX6000 dont attach */
2236 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
2237 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
2238 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
2239 devid == ATA_I960RM)
2242 strcpy(buffer, idx->text);
2244 /* if we are on a FastTrak TX4, adjust the interrupt resource */
2245 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
2246 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
2247 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
2248 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
2249 static long start = 0, end = 0;
2251 if (pci_get_slot(dev) == 1) {
2252 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
2253 strcat(buffer, " (channel 0+1)");
2255 else if (pci_get_slot(dev) == 2 && start && end) {
2256 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
2258 strcat(buffer, " (channel 2+3)");
2264 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
2265 device_set_desc_copy(dev, buffer);
2267 ctlr->chipinit = ata_promise_chipinit;
2272 ata_promise_chipinit(device_t dev)
2274 struct ata_pci_controller *ctlr = device_get_softc(dev);
2275 int rid = ATA_IRQ_RID;
2277 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2278 RF_SHAREABLE | RF_ACTIVE))) {
2279 device_printf(dev, "unable to map interrupt\n");
2283 if (ctlr->chip->max_dma >= ATA_SA150)
2284 ctlr->setmode = ata_sata_setmode;
2286 ctlr->setmode = ata_promise_setmode;
2288 switch (ctlr->chip->cfg1) {
2291 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
2293 ctlr->dmainit = ata_promise_new_dmainit;
2297 /* enable burst mode */
2298 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
2300 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2301 ata_promise_old_intr, ctlr, &ctlr->handle))) {
2302 device_printf(dev, "unable to setup interrupt\n");
2308 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2309 ata_promise_tx2_intr, ctlr, &ctlr->handle))) {
2310 device_printf(dev, "unable to setup interrupt\n");
2316 // if (ctlr->r_res1)
2317 // bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
2318 ctlr->r_type1 = SYS_RES_MEMORY;
2319 ctlr->r_rid1 = PCIR_BAR(4);
2320 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
2321 &ctlr->r_rid1, RF_ACTIVE)))
2324 ctlr->r_type2 = SYS_RES_MEMORY;
2325 ctlr->r_rid2 = PCIR_BAR(3);
2326 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2327 &ctlr->r_rid2, RF_ACTIVE))){
2328 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
2331 ctlr->reset = ata_promise_mio_reset;
2332 ctlr->dmainit = ata_promise_mio_dmainit;
2333 ctlr->allocate = ata_promise_mio_allocate;
2335 if (ctlr->chip->cfg2 == PRSX4X) {
2336 struct ata_promise_sx4 *hpkt;
2337 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
2339 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2340 ata_promise_sx4_intr, ctlr, &ctlr->handle))) {
2341 device_printf(dev, "unable to setup interrupt\n");
2342 /* XXX SOS release resources */
2346 /* print info about cache memory */
2347 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
2348 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
2349 ((dimm >> 24) & 0xff),
2350 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
2351 " ECC enabled" : "" );
2353 /* adjust cache memory parameters */
2354 ATA_OUTL(ctlr->r_res2, 0x000c000c,
2355 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
2357 /* setup host packet controls */
2358 hpkt = malloc(sizeof(struct ata_promise_sx4),
2359 M_TEMP, M_NOWAIT | M_ZERO);
2360 mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
2361 TAILQ_INIT(&hpkt->queue);
2362 hpkt->busy = 0; //hpkt->head = hpkt->tail = 0;
2363 device_set_ivars(dev, hpkt);
2368 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2369 ata_promise_mio_intr, ctlr, &ctlr->handle))) {
2370 device_printf(dev, "unable to setup interrupt\n");
2371 /* XXX SOS release resources */
2375 switch (ctlr->chip->cfg2) {
2377 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
2378 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
2382 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
2383 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 3;
2387 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
2392 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
2397 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
2402 /* XXX SOS release resources */
2406 /* XXX SOS release resources */
2413 ata_promise_mio_allocate(device_t dev)
2415 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2416 struct ata_channel *ch = device_get_softc(dev);
2417 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
2420 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2421 ch->r_io[i].res = ctlr->r_res2;
2422 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
2424 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
2425 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
2426 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
2427 ata_default_registers(dev);
2428 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
2429 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
2430 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2431 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
2432 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2433 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
2434 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2435 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
2436 ch->flags |= ATA_NO_SLAVE;
2438 ch->flags |= ATA_USE_16BIT;
2440 ata_generic_hw(dev);
2442 ch->hw.command = ata_promise_sx4_command;
2444 ch->hw.command = ata_promise_mio_command;
2449 ata_promise_mio_intr(void *data)
2451 struct ata_pci_controller *ctlr = data;
2452 struct ata_channel *ch;
2453 u_int32_t vector = 0, status = 0;
2456 switch (ctlr->chip->cfg2) {
2459 /* read and acknowledge interrupt(s) */
2460 vector = ATA_INL(ctlr->r_res2, 0x040);
2462 /* read and clear interface status */
2463 status = ATA_INL(ctlr->r_res2, 0x06c);
2464 ATA_OUTL(ctlr->r_res2, 0x06c, status & 0x000000ff);
2469 /* read and acknowledge interrupt(s) */
2470 vector = ATA_INL(ctlr->r_res2, 0x040);
2471 ATA_OUTL(ctlr->r_res2, 0x040, vector & 0x0000ffff);
2473 /* read and clear interface status */
2474 status = ATA_INL(ctlr->r_res2, 0x060);
2475 ATA_OUTL(ctlr->r_res2, 0x060, status & 0x000000ff);
2479 for (unit = 0; unit < ctlr->channels; unit++) {
2481 if ((ch = ctlr->interrupt[unit].argument)) {
2482 struct ata_connect_task *tp;
2484 /* check for and handle disconnect events */
2485 if ((status & (0x00000001 << unit)) &&
2486 (tp = (struct ata_connect_task *)
2487 malloc(sizeof(struct ata_connect_task),
2488 M_ATA, M_NOWAIT | M_ZERO))) {
2491 device_printf(ch->dev, "DISCONNECT requested\n");
2492 tp->action = ATA_C_DETACH;
2494 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2495 taskqueue_enqueue(taskqueue_thread, &tp->task);
2498 /* check for and handle connect events */
2499 if ((status & (0x00000010 << unit)) &&
2500 (tp = (struct ata_connect_task *)
2501 malloc(sizeof(struct ata_connect_task),
2502 M_ATA, M_NOWAIT | M_ZERO))) {
2505 device_printf(ch->dev, "CONNECT requested\n");
2506 tp->action = ATA_C_ATTACH;
2508 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2509 taskqueue_enqueue(taskqueue_thread, &tp->task);
2512 /* active interrupt(s) need to call the interrupt handler */
2513 if (vector & (1 << (unit + 1)))
2514 if ((ch = ctlr->interrupt[unit].argument))
2515 ctlr->interrupt[unit].function(ch);
2521 ata_promise_sx4_intr(void *data)
2523 struct ata_pci_controller *ctlr = data;
2524 struct ata_channel *ch;
2525 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
2528 for (unit = 0; unit < ctlr->channels; unit++) {
2529 if (vector & (1 << (unit + 1)))
2530 if ((ch = ctlr->interrupt[unit].argument))
2531 ctlr->interrupt[unit].function(ch);
2532 if (vector & (1 << (unit + 5)))
2533 if ((ch = ctlr->interrupt[unit].argument))
2534 ata_promise_queue_hpkt(ctlr,
2535 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
2536 ATA_PDC_HPKT_OFFSET));
2537 if (vector & (1 << (unit + 9))) {
2538 ata_promise_next_hpkt(ctlr);
2539 if ((ch = ctlr->interrupt[unit].argument))
2540 ctlr->interrupt[unit].function(ch);
2542 if (vector & (1 << (unit + 13))) {
2543 ata_promise_next_hpkt(ctlr);
2544 if ((ch = ctlr->interrupt[unit].argument))
2545 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2546 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
2547 ATA_PDC_APKT_OFFSET));
2553 ata_promise_mio_dmainit(device_t dev)
2555 struct ata_channel *ch = device_get_softc(dev);
2559 /* note start and stop are not used here */
2564 ata_promise_mio_reset(device_t dev)
2566 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2567 struct ata_channel *ch = device_get_softc(dev);
2568 struct ata_promise_sx4 *hpktp;
2570 switch (ctlr->chip->cfg2) {
2573 /* softreset channel ATA module */
2574 hpktp = device_get_ivars(ctlr->dev);
2575 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
2577 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
2578 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
2579 ~0x00003f9f) | (ch->unit + 1));
2581 /* softreset HOST module */ /* XXX SOS what about other outstandings */
2582 mtx_lock(&hpktp->mtx);
2583 ATA_OUTL(ctlr->r_res2, 0xc012c,
2584 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
2586 ATA_OUTL(ctlr->r_res2, 0xc012c,
2587 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
2589 mtx_unlock(&hpktp->mtx);
2590 ata_generic_reset(dev);
2595 if ((ctlr->chip->cfg2 == PRSATA) ||
2596 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2598 /* mask plug/unplug intr */
2599 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
2602 /* softreset channels ATA module */
2603 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2605 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2606 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2607 ~0x00003f9f) | (ch->unit + 1));
2609 if ((ctlr->chip->cfg2 == PRSATA) ||
2610 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2612 ata_sata_phy_enable(ch);
2614 /* reset and enable plug/unplug intr */
2615 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
2618 ata_generic_reset(dev);
2623 if ((ctlr->chip->cfg2 == PRSATA2) ||
2624 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2625 /* set portmultiplier port */
2626 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
2628 /* mask plug/unplug intr */
2629 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
2632 /* softreset channels ATA module */
2633 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2635 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2636 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2637 ~0x00003f9f) | (ch->unit + 1));
2639 if ((ctlr->chip->cfg2 == PRSATA2) ||
2640 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2642 /* set PHY mode to "improved" */
2643 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
2644 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
2645 ~0x00000003) | 0x00000001);
2647 ata_sata_phy_enable(ch);
2649 /* reset and enable plug/unplug intr */
2650 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
2652 /* set portmultiplier port */
2653 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
2656 ata_generic_reset(dev);
2663 ata_promise_mio_command(struct ata_request *request)
2665 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2666 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2667 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
2669 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
2671 /* XXX SOS add ATAPI commands support later */
2672 switch (request->u.ata.command) {
2674 return ata_generic_command(request);
2677 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
2681 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
2684 wordp[1] = htole32(ch->dma->sg_bus);
2686 ata_promise_apkt((u_int8_t*)wordp, request);
2688 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
2693 ata_promise_sx4_command(struct ata_request *request)
2695 device_t gparent = GRANDPARENT(request->dev);
2696 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2697 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2698 struct ata_dma_prdentry *prd = ch->dma->sg;
2699 caddr_t window = rman_get_virtual(ctlr->r_res1);
2701 int i, idx, length = 0;
2703 /* XXX SOS add ATAPI commands support later */
2704 switch (request->u.ata.command) {
2709 case ATA_ATA_IDENTIFY:
2714 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2715 return ata_generic_command(request);
2717 case ATA_SETFEATURES:
2718 case ATA_FLUSHCACHE:
2721 wordp = (u_int32_t *)
2722 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2723 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
2726 ata_promise_apkt((u_int8_t *)wordp, request);
2727 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2728 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2729 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2730 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
2735 wordp = (u_int32_t *)
2736 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
2739 wordp[idx++] = prd[i].addr;
2740 wordp[idx++] = prd[i].count;
2741 length += (prd[i].count & ~ATA_DMA_EOT);
2742 } while (!(prd[i++].count & ATA_DMA_EOT));
2744 wordp = (u_int32_t *)
2745 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_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_ASG_OFFSET);
2751 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2752 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
2754 wordp = (u_int32_t *)
2755 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
2756 if (request->flags & ATA_R_READ)
2757 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
2758 if (request->flags & ATA_R_WRITE)
2759 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
2760 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
2761 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
2764 wordp = (u_int32_t *)
2765 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2766 if (request->flags & ATA_R_READ)
2767 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
2768 if (request->flags & ATA_R_WRITE)
2769 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
2770 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
2772 ata_promise_apkt((u_int8_t *)wordp, request);
2773 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2775 if (request->flags & ATA_R_READ) {
2776 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
2777 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
2778 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2779 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
2781 if (request->flags & ATA_R_WRITE) {
2782 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
2783 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
2784 ata_promise_queue_hpkt(ctlr,
2785 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
2792 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
2794 struct ata_device *atadev = device_get_softc(request->dev);
2798 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
2799 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
2800 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
2801 bytep[i++] = ATA_A_4BIT;
2803 command = ata_modify_if_48bit(request);
2805 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2806 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
2807 bytep[i++] = request->u.ata.feature >> 8;
2808 bytep[i++] = request->u.ata.feature;
2809 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
2810 bytep[i++] = request->u.ata.count >> 8;
2811 bytep[i++] = request->u.ata.count;
2812 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
2813 bytep[i++] = request->u.ata.lba >> 24;
2814 bytep[i++] = request->u.ata.lba;
2815 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
2816 bytep[i++] = request->u.ata.lba >> 32;
2817 bytep[i++] = request->u.ata.lba >> 8;
2818 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
2819 bytep[i++] = request->u.ata.lba >> 40;
2820 bytep[i++] = request->u.ata.lba >> 16;
2821 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
2822 bytep[i++] = ATA_D_LBA | atadev->unit;
2825 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
2826 bytep[i++] = request->u.ata.feature;
2827 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
2828 bytep[i++] = request->u.ata.count;
2829 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
2830 bytep[i++] = request->u.ata.lba;
2831 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
2832 bytep[i++] = request->u.ata.lba >> 8;
2833 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
2834 bytep[i++] = request->u.ata.lba >> 16;
2835 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
2836 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
2837 ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
2839 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
2840 bytep[i++] = command;
2845 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
2847 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
2849 mtx_lock(&hpktp->mtx);
2851 struct host_packet *hp =
2852 malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
2854 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
2858 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
2860 mtx_unlock(&hpktp->mtx);
2864 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
2866 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
2867 struct host_packet *hp;
2869 mtx_lock(&hpktp->mtx);
2870 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
2871 TAILQ_REMOVE(&hpktp->queue, hp, chain);
2872 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
2877 mtx_unlock(&hpktp->mtx);
2881 ata_promise_tx2_intr(void *data)
2883 struct ata_pci_controller *ctlr = data;
2884 struct ata_channel *ch;
2887 for (unit = 0; unit < ctlr->channels; unit++) {
2888 if (!(ch = ctlr->interrupt[unit].argument))
2890 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
2891 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
2892 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2893 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2895 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2896 ATA_BMSTAT_INTERRUPT)
2898 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2901 ctlr->interrupt[unit].function(ch);
2907 ata_promise_old_intr(void *data)
2909 struct ata_pci_controller *ctlr = data;
2910 struct ata_channel *ch;
2913 for (unit = 0; unit < ctlr->channels; unit++) {
2914 if (!(ch = ctlr->interrupt[unit].argument))
2916 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)){
2917 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2918 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2920 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2921 ATA_BMSTAT_INTERRUPT)
2923 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2926 ctlr->interrupt[unit].function(ch);
2932 ata_promise_new_dmastart(device_t dev)
2934 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2935 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2936 struct ata_device *atadev = device_get_softc(dev);
2938 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2939 ATA_OUTB(ctlr->r_res1, 0x11,
2940 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
2941 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20,
2942 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
2943 (ch->dma->cur_iosize >> 1));
2945 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
2946 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
2947 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
2948 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2949 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
2950 ATA_BMCMD_START_STOP);
2951 ch->flags |= ATA_DMA_ACTIVE;
2956 ata_promise_new_dmastop(device_t dev)
2958 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2959 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2960 struct ata_device *atadev = device_get_softc(dev);
2963 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2964 ATA_OUTB(ctlr->r_res1, 0x11,
2965 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
2966 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20, 0);
2968 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
2969 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2970 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
2971 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
2972 ch->flags &= ~ATA_DMA_ACTIVE;
2977 ata_promise_new_dmareset(device_t dev)
2979 struct ata_channel *ch = device_get_softc(dev);
2981 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2982 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
2983 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
2984 ch->flags &= ~ATA_DMA_ACTIVE;
2988 ata_promise_new_dmainit(device_t dev)
2990 struct ata_channel *ch = device_get_softc(dev);
2994 ch->dma->start = ata_promise_new_dmastart;
2995 ch->dma->stop = ata_promise_new_dmastop;
2996 ch->dma->reset = ata_promise_new_dmareset;
3001 ata_promise_setmode(device_t dev, int mode)
3003 device_t gparent = GRANDPARENT(dev);
3004 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3005 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3006 struct ata_device *atadev = device_get_softc(dev);
3007 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3009 u_int32_t timings33[][2] = {
3010 /* PROLD PRNEW mode */
3011 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
3012 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
3013 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
3014 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
3015 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
3016 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
3017 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
3018 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
3019 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
3020 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
3021 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
3022 { 0, 0x00424ef6 }, /* UDMA 3 */
3023 { 0, 0x004127f3 }, /* UDMA 4 */
3024 { 0, 0x004127f3 } /* UDMA 5 */
3027 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3029 switch (ctlr->chip->cfg1) {
3032 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
3033 (ch->unit ? 1 << 11 : 1 << 10))) {
3034 ata_print_cable(dev, "controller");
3037 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
3038 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
3042 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3043 if (mode > ATA_UDMA2 &&
3044 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
3045 ata_print_cable(dev, "controller");
3051 if (mode > ATA_UDMA2 &&
3052 (ATA_INL(ctlr->r_res2,
3053 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
3054 (ch->unit << 7)) & 0x01000000)) {
3055 ata_print_cable(dev, "controller");
3061 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3064 device_printf(dev, "%ssetting %s on %s chip\n",
3065 (error) ? "FAILURE " : "",
3066 ata_mode2str(mode), ctlr->chip->text);
3068 if (ctlr->chip->cfg1 < PRTX)
3069 pci_write_config(gparent, 0x60 + (devno << 2),
3070 timings33[ctlr->chip->cfg1][ata_mode2idx(mode)],4);
3071 atadev->mode = mode;
3078 * ServerWorks chipset support functions
3081 ata_serverworks_ident(device_t dev)
3083 struct ata_pci_controller *ctlr = device_get_softc(dev);
3084 struct ata_chip_id *idx;
3085 static struct ata_chip_id ids[] =
3086 {{ ATA_ROSB4, 0x00, SWKS33, 0x00, ATA_UDMA2, "ServerWorks ROSB4" },
3087 { ATA_CSB5, 0x92, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB5" },
3088 { ATA_CSB5, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB5" },
3089 { ATA_CSB6, 0x00, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB6" },
3090 { ATA_CSB6_1, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB6" },
3091 { 0, 0, 0, 0, 0, 0}};
3094 if (!(idx = ata_match_chip(dev, ids)))
3097 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3098 device_set_desc_copy(dev, buffer);
3100 ctlr->chipinit = ata_serverworks_chipinit;
3105 ata_serverworks_chipinit(device_t dev)
3107 struct ata_pci_controller *ctlr = device_get_softc(dev);
3109 if (ata_setup_interrupt(dev))
3112 if (ctlr->chip->cfg1 == SWKS33) {
3116 /* locate the ISA part in the southbridge and enable UDMA33 */
3117 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
3118 for (i = 0; i < nchildren; i++) {
3119 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
3120 pci_write_config(children[i], 0x64,
3121 (pci_read_config(children[i], 0x64, 4) &
3122 ~0x00002000) | 0x00004000, 4);
3126 free(children, M_TEMP);
3130 pci_write_config(dev, 0x5a,
3131 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
3132 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
3134 ctlr->setmode = ata_serverworks_setmode;
3139 ata_serverworks_setmode(device_t dev, int mode)
3141 device_t gparent = GRANDPARENT(dev);
3142 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3143 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3144 struct ata_device *atadev = device_get_softc(dev);
3145 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3146 int offset = (devno ^ 0x01) << 3;
3148 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
3149 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
3150 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
3152 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3154 mode = ata_check_80pin(dev, mode);
3156 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3159 device_printf(dev, "%ssetting %s on %s chip\n",
3160 (error) ? "FAILURE " : "",
3161 ata_mode2str(mode), ctlr->chip->text);
3163 if (mode >= ATA_UDMA0) {
3164 pci_write_config(gparent, 0x56,
3165 (pci_read_config(gparent, 0x56, 2) &
3166 ~(0xf << (devno << 2))) |
3167 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
3168 pci_write_config(gparent, 0x54,
3169 pci_read_config(gparent, 0x54, 1) |
3170 (0x01 << devno), 1);
3171 pci_write_config(gparent, 0x44,
3172 (pci_read_config(gparent, 0x44, 4) &
3173 ~(0xff << offset)) |
3174 (dmatimings[2] << offset), 4);
3176 else if (mode >= ATA_WDMA0) {
3177 pci_write_config(gparent, 0x54,
3178 pci_read_config(gparent, 0x54, 1) &
3179 ~(0x01 << devno), 1);
3180 pci_write_config(gparent, 0x44,
3181 (pci_read_config(gparent, 0x44, 4) &
3182 ~(0xff << offset)) |
3183 (dmatimings[mode & ATA_MODE_MASK] << offset),4);
3186 pci_write_config(gparent, 0x54,
3187 pci_read_config(gparent, 0x54, 1) &
3188 ~(0x01 << devno), 1);
3190 pci_write_config(gparent, 0x40,
3191 (pci_read_config(gparent, 0x40, 4) &
3192 ~(0xff << offset)) |
3193 (piotimings[ata_mode2idx(mode)] << offset), 4);
3194 atadev->mode = mode;
3200 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
3203 ata_sii_ident(device_t dev)
3205 struct ata_pci_controller *ctlr = device_get_softc(dev);
3206 struct ata_chip_id *idx;
3207 static struct ata_chip_id ids[] =
3208 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
3209 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
3210 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
3211 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
3212 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
3213 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
3214 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
3215 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
3216 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
3217 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
3218 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
3219 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
3220 { 0, 0, 0, 0, 0, 0}};
3223 if (!(idx = ata_match_chip(dev, ids)))
3226 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3227 device_set_desc_copy(dev, buffer);
3229 ctlr->chipinit = ata_sii_chipinit;
3234 ata_sii_chipinit(device_t dev)
3236 struct ata_pci_controller *ctlr = device_get_softc(dev);
3237 int rid = ATA_IRQ_RID;
3239 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3240 RF_SHAREABLE | RF_ACTIVE))) {
3241 device_printf(dev, "unable to map interrupt\n");
3245 if (ctlr->chip->cfg1 == SIIMEMIO) {
3246 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3247 ata_sii_intr, ctlr, &ctlr->handle))) {
3248 device_printf(dev, "unable to setup interrupt\n");
3252 ctlr->r_type2 = SYS_RES_MEMORY;
3253 ctlr->r_rid2 = PCIR_BAR(5);
3254 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3255 &ctlr->r_rid2, RF_ACTIVE)))
3258 if (ctlr->chip->cfg2 & SIISETCLK) {
3259 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
3260 pci_write_config(dev, 0x8a,
3261 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
3262 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
3263 device_printf(dev, "%s could not set ATA133 clock\n",
3267 /* if we have 4 channels enable the second set */
3268 if (ctlr->chip->cfg2 & SII4CH) {
3269 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
3273 /* enable PCI interrupt as BIOS might not */
3274 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
3276 /* dont block interrupts from any channel */
3277 pci_write_config(dev, 0x48,
3278 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
3280 ctlr->allocate = ata_sii_allocate;
3281 if (ctlr->chip->max_dma >= ATA_SA150) {
3282 ctlr->reset = ata_sii_reset;
3283 ctlr->setmode = ata_sata_setmode;
3286 ctlr->setmode = ata_sii_setmode;
3289 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3290 ctlr->chip->cfg2 & SIIINTR ?
3291 ata_cmd_intr : ata_cmd_old_intr,
3292 ctlr, &ctlr->handle))) {
3293 device_printf(dev, "unable to setup interrupt\n");
3297 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
3298 device_printf(dev, "HW has secondary channel disabled\n");
3302 /* enable interrupt as BIOS might not */
3303 pci_write_config(dev, 0x71, 0x01, 1);
3305 ctlr->setmode = ata_cmd_setmode;
3311 ata_sii_allocate(device_t dev)
3313 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3314 struct ata_channel *ch = device_get_softc(dev);
3315 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
3318 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
3319 ch->r_io[i].res = ctlr->r_res2;
3320 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
3322 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
3323 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
3324 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
3325 ata_default_registers(dev);
3326 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
3327 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
3328 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
3329 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
3330 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
3331 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
3332 ch->r_io[ATA_BMDEVSPEC_0].res = ctlr->r_res2;
3333 ch->r_io[ATA_BMDEVSPEC_0].offset = 0xa1 + (unit01 << 6) + (unit10 << 8);
3335 if (ctlr->chip->max_dma >= ATA_SA150) {
3336 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3337 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
3338 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3339 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
3340 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3341 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
3342 ch->flags |= ATA_NO_SLAVE;
3344 /* enable PHY state change interrupt */
3345 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
3348 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
3349 /* work around errata in early chips */
3350 ch->dma->boundary = 16 * DEV_BSIZE;
3351 ch->dma->max_iosize = 15 * DEV_BSIZE;
3354 ata_generic_hw(dev);
3359 ata_sii_intr(void *data)
3361 struct ata_pci_controller *ctlr = data;
3362 struct ata_channel *ch;
3365 for (unit = 0; unit < ctlr->channels; unit++) {
3366 if (!(ch = ctlr->interrupt[unit].argument))
3369 /* check for PHY related interrupts on SATA capable HW */
3370 if (ctlr->chip->max_dma >= ATA_SA150) {
3371 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
3372 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
3373 struct ata_connect_task *tp;
3376 /* clear error bits/interrupt */
3377 ATA_IDX_OUTL(ch, ATA_SERROR, error);
3379 /* if we have a connection event deal with it */
3380 if ((error & ATA_SE_PHY_CHANGED) &&
3381 (tp = (struct ata_connect_task *)
3382 malloc(sizeof(struct ata_connect_task),
3383 M_ATA, M_NOWAIT | M_ZERO))) {
3385 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
3386 device_printf(ch->dev, "CONNECT requested\n");
3387 tp->action = ATA_C_ATTACH;
3390 device_printf(ch->dev, "DISCONNECT requested\n");
3391 tp->action = ATA_C_DETACH;
3394 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3395 taskqueue_enqueue(taskqueue_thread, &tp->task);
3400 /* any drive action to take care of ? */
3401 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_0) & 0x08) {
3402 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3403 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3405 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
3407 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3410 ctlr->interrupt[unit].function(ch);
3417 ata_cmd_intr(void *data)
3419 struct ata_pci_controller *ctlr = data;
3420 struct ata_channel *ch;
3424 for (unit = 0; unit < ctlr->channels; unit++) {
3425 if (!(ch = ctlr->interrupt[unit].argument))
3427 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
3428 (ch->unit ? 0x08 : 0x04))) {
3429 pci_write_config(device_get_parent(ch->dev), 0x71,
3430 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
3431 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3432 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3434 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3435 ATA_BMSTAT_INTERRUPT)
3437 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3440 ctlr->interrupt[unit].function(ch);
3446 ata_cmd_old_intr(void *data)
3448 struct ata_pci_controller *ctlr = data;
3449 struct ata_channel *ch;
3452 for (unit = 0; unit < ctlr->channels; unit++) {
3453 if (!(ch = ctlr->interrupt[unit].argument))
3455 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3456 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3458 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3459 ATA_BMSTAT_INTERRUPT)
3461 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3464 ctlr->interrupt[unit].function(ch);
3469 ata_sii_reset(device_t dev)
3471 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3472 struct ata_channel *ch = device_get_softc(dev);
3473 int offset = ((ch->unit & 1) << 7) + ((ch->unit & 2) << 8);
3475 /* disable PHY state change interrupt */
3476 ATA_OUTL(ctlr->r_res2, 0x148 + offset, ~(1 << 16));
3478 ata_sata_phy_enable(ch);
3480 /* enable PHY state change interrupt */
3481 ATA_OUTL(ctlr->r_res2, 0x148 + offset, (1 << 16));
3485 ata_sii_setmode(device_t dev, int mode)
3487 device_t gparent = GRANDPARENT(dev);
3488 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3489 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3490 struct ata_device *atadev = device_get_softc(dev);
3491 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
3492 int mreg = ch->unit ? 0x84 : 0x80;
3493 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
3494 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
3497 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3499 if (ctlr->chip->cfg2 & SIISETCLK) {
3500 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
3501 (ch->unit ? 0x02 : 0x01))) {
3502 ata_print_cable(dev, "controller");
3507 mode = ata_check_80pin(dev, mode);
3509 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3512 device_printf(dev, "%ssetting %s on %s chip\n",
3513 (error) ? "FAILURE " : "",
3514 ata_mode2str(mode), ctlr->chip->text);
3518 if (mode >= ATA_UDMA0) {
3519 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
3520 u_int8_t ureg = 0xac + rego;
3522 pci_write_config(gparent, mreg,
3523 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
3524 pci_write_config(gparent, ureg,
3525 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
3526 udmatimings[mode & ATA_MODE_MASK], 1);
3529 else if (mode >= ATA_WDMA0) {
3530 u_int8_t dreg = 0xa8 + rego;
3531 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
3533 pci_write_config(gparent, mreg,
3534 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
3535 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
3539 u_int8_t preg = 0xa4 + rego;
3540 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
3542 pci_write_config(gparent, mreg,
3543 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
3544 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
3546 atadev->mode = mode;
3550 ata_cmd_setmode(device_t dev, int mode)
3552 device_t gparent = GRANDPARENT(dev);
3553 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3554 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3555 struct ata_device *atadev = device_get_softc(dev);
3556 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3559 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3561 mode = ata_check_80pin(dev, mode);
3563 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3566 device_printf(dev, "%ssetting %s on %s chip\n",
3567 (error) ? "FAILURE " : "",
3568 ata_mode2str(mode), ctlr->chip->text);
3570 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
3571 int ureg = ch->unit ? 0x7b : 0x73;
3573 if (mode >= ATA_UDMA0) {
3574 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
3575 { 0x11, 0x42 }, { 0x25, 0x8a },
3576 { 0x15, 0x4a }, { 0x05, 0x0a } };
3578 u_int8_t umode = pci_read_config(gparent, ureg, 1);
3580 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
3581 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
3582 pci_write_config(gparent, ureg, umode, 1);
3584 else if (mode >= ATA_WDMA0) {
3585 int dmatimings[] = { 0x87, 0x32, 0x3f };
3587 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
3588 pci_write_config(gparent, ureg,
3589 pci_read_config(gparent, ureg, 1) &
3590 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3593 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
3594 pci_write_config(gparent, treg,
3595 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
3596 pci_write_config(gparent, ureg,
3597 pci_read_config(gparent, ureg, 1) &
3598 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3600 atadev->mode = mode;
3606 * Silicon Integrated Systems Corp. (SiS) chipset support functions
3609 ata_sis_ident(device_t dev)
3611 struct ata_pci_controller *ctlr = device_get_softc(dev);
3612 struct ata_chip_id *idx;
3613 static struct ata_chip_id ids[] =
3614 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "SiS 182" }, /* south */
3615 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "SiS 181" }, /* south */
3616 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "SiS 180" }, /* south */
3617 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 965" }, /* south */
3618 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 964" }, /* south */
3619 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 963" }, /* south */
3620 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 962" }, /* south */
3622 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 745" }, /* 1chip */
3623 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 735" }, /* 1chip */
3624 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 733" }, /* 1chip */
3625 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "SiS 730" }, /* 1chip */
3627 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 635" }, /* 1chip */
3628 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 633" }, /* unknown */
3629 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "SiS 630S"}, /* 1chip */
3630 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "SiS 630" }, /* 1chip */
3631 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "SiS 620" }, /* 1chip */
3633 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "SiS 550" },
3634 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "SiS 540" },
3635 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "SiS 530" },
3637 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "SiS 5513" },
3638 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "SiS 5513" },
3639 { 0, 0, 0, 0, 0, 0 }};
3643 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
3646 if (idx->cfg2 && !found) {
3647 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
3649 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
3650 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
3652 idx->cfg1 = SIS133NEW;
3653 idx->max_dma = ATA_UDMA6;
3654 sprintf(buffer, "SiS 962/963 %s controller",
3655 ata_mode2str(idx->max_dma));
3657 pci_write_config(dev, 0x57, reg57, 1);
3659 if (idx->cfg2 && !found) {
3660 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
3662 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
3663 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
3664 struct ata_chip_id id[] =
3665 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
3668 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
3669 idx->cfg1 = SIS133OLD;
3670 idx->max_dma = ATA_UDMA6;
3673 idx->cfg1 = SIS100NEW;
3674 idx->max_dma = ATA_UDMA5;
3676 sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
3678 pci_write_config(dev, 0x4a, reg4a, 1);
3681 sprintf(buffer,"%s %s controller",idx->text,ata_mode2str(idx->max_dma));
3683 device_set_desc_copy(dev, buffer);
3685 ctlr->chipinit = ata_sis_chipinit;
3690 ata_sis_chipinit(device_t dev)
3692 struct ata_pci_controller *ctlr = device_get_softc(dev);
3694 if (ata_setup_interrupt(dev))
3697 switch (ctlr->chip->cfg1) {
3702 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
3706 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
3709 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
3710 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
3713 ctlr->r_type2 = SYS_RES_IOPORT;
3714 ctlr->r_rid2 = PCIR_BAR(5);
3715 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3716 &ctlr->r_rid2, RF_ACTIVE))) {
3717 pci_write_config(dev, PCIR_COMMAND,
3718 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3719 ctlr->allocate = ata_sis_allocate;
3720 ctlr->reset = ata_sis_reset;
3722 ctlr->setmode = ata_sata_setmode;
3727 ctlr->setmode = ata_sis_setmode;
3732 ata_sis_allocate(device_t dev)
3734 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3735 struct ata_channel *ch = device_get_softc(dev);
3737 /* setup the usual register normal pci style */
3738 ata_pci_allocate(dev);
3740 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3741 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 4);
3742 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3743 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 4);
3744 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3745 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 4);
3746 ch->flags |= ATA_NO_SLAVE;
3748 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
3749 /* XXX SOS unknown how to enable PHY state change interrupt */
3754 ata_sis_reset(device_t dev)
3756 struct ata_channel *ch = device_get_softc(dev);
3758 ata_sata_phy_enable(ch);
3763 ata_sis_setmode(device_t dev, int mode)
3765 device_t gparent = GRANDPARENT(dev);
3766 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3767 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3768 struct ata_device *atadev = device_get_softc(dev);
3769 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3772 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3774 if (ctlr->chip->cfg1 == SIS133NEW) {
3775 if (mode > ATA_UDMA2 &&
3776 pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
3777 ata_print_cable(dev, "controller");
3782 if (mode > ATA_UDMA2 &&
3783 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
3784 ata_print_cable(dev, "controller");
3789 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3792 device_printf(dev, "%ssetting %s on %s chip\n",
3793 (error) ? "FAILURE " : "",
3794 ata_mode2str(mode), ctlr->chip->text);
3796 switch (ctlr->chip->cfg1) {
3798 u_int32_t timings[] =
3799 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
3800 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
3801 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
3804 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
3805 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
3809 u_int16_t timings[] =
3810 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
3811 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
3813 u_int16_t reg = 0x40 + (devno << 1);
3815 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3819 u_int16_t timings[] =
3820 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
3821 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
3822 u_int16_t reg = 0x40 + (devno << 1);
3824 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3830 u_int16_t timings[] =
3831 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
3832 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
3833 u_int16_t reg = 0x40 + (devno << 1);
3835 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3839 atadev->mode = mode;
3844 /* VIA Technologies Inc. chipset support functions */
3846 ata_via_ident(device_t dev)
3848 struct ata_pci_controller *ctlr = device_get_softc(dev);
3849 struct ata_chip_id *idx;
3850 static struct ata_chip_id ids[] =
3851 {{ ATA_VIA82C586, 0x02, VIA33, 0x00, ATA_UDMA2, "VIA 82C586B" },
3852 { ATA_VIA82C586, 0x00, VIA33, 0x00, ATA_WDMA2, "VIA 82C586" },
3853 { ATA_VIA82C596, 0x12, VIA66, VIACLK, ATA_UDMA4, "VIA 82C596B" },
3854 { ATA_VIA82C596, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C596" },
3855 { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "VIA 82C686B"},
3856 { ATA_VIA82C686, 0x10, VIA66, VIACLK, ATA_UDMA4, "VIA 82C686A" },
3857 { ATA_VIA82C686, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C686" },
3858 { ATA_VIA8231, 0x00, VIA100, VIABUG, ATA_UDMA5, "VIA 8231" },
3859 { ATA_VIA8233, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233" },
3860 { ATA_VIA8233C, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233C" },
3861 { ATA_VIA8233A, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8233A" },
3862 { ATA_VIA8235, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8235" },
3863 { ATA_VIA8237, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8237" },
3864 { 0, 0, 0, 0, 0, 0 }};
3865 static struct ata_chip_id new_ids[] =
3866 {{ ATA_VIA6410, 0x00, 0, 0x00, ATA_UDMA6, "VIA 6410" },
3867 { ATA_VIA6420, 0x00, 7, 0x00, ATA_SA150, "VIA 6420" },
3868 { ATA_VIA6421, 0x00, 6, 0x00, ATA_SA150, "VIA 6421" },
3869 { 0, 0, 0, 0, 0, 0 }};
3872 if (pci_get_devid(dev) == ATA_VIA82C571) {
3873 if (!(idx = ata_find_chip(dev, ids, -99)))
3877 if (!(idx = ata_match_chip(dev, new_ids)))
3881 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3882 device_set_desc_copy(dev, buffer);
3884 ctlr->chipinit = ata_via_chipinit;
3889 ata_via_chipinit(device_t dev)
3891 struct ata_pci_controller *ctlr = device_get_softc(dev);
3893 if (ata_setup_interrupt(dev))
3896 if (ctlr->chip->max_dma >= ATA_SA150) {
3897 ctlr->r_type2 = SYS_RES_IOPORT;
3898 ctlr->r_rid2 = PCIR_BAR(5);
3899 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3900 &ctlr->r_rid2, RF_ACTIVE))) {
3901 pci_write_config(dev, PCIR_COMMAND,
3902 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3903 ctlr->allocate = ata_via_allocate;
3904 ctlr->reset = ata_via_reset;
3906 ctlr->setmode = ata_sata_setmode;
3910 /* prepare for ATA-66 on the 82C686a and 82C596b */
3911 if (ctlr->chip->cfg2 & VIACLK)
3912 pci_write_config(dev, 0x50, 0x030b030b, 4);
3914 /* the southbridge might need the data corruption fix */
3915 if (ctlr->chip->cfg2 & VIABUG)
3916 ata_via_southbridge_fixup(dev);
3918 /* set fifo configuration half'n'half */
3919 pci_write_config(dev, 0x43,
3920 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
3922 /* set status register read retry */
3923 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
3925 /* set DMA read & end-of-sector fifo flush */
3926 pci_write_config(dev, 0x46,
3927 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
3929 /* set sector size */
3930 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
3931 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
3933 ctlr->setmode = ata_via_family_setmode;
3938 ata_via_allocate(device_t dev)
3940 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3941 struct ata_channel *ch = device_get_softc(dev);
3943 /* setup the usual register normal pci style */
3944 ata_pci_allocate(dev);
3946 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3947 ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
3948 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3949 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
3950 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3951 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
3952 ch->flags |= ATA_NO_SLAVE;
3954 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
3955 /* XXX SOS unknown how to enable PHY state change interrupt */
3960 ata_via_reset(device_t dev)
3962 struct ata_channel *ch = device_get_softc(dev);
3964 ata_sata_phy_enable(ch);
3968 ata_via_southbridge_fixup(device_t dev)
3973 if (device_get_children(device_get_parent(dev), &children, &nchildren))
3976 for (i = 0; i < nchildren; i++) {
3977 if (pci_get_devid(children[i]) == ATA_VIA8363 ||
3978 pci_get_devid(children[i]) == ATA_VIA8371 ||
3979 pci_get_devid(children[i]) == ATA_VIA8662 ||
3980 pci_get_devid(children[i]) == ATA_VIA8361) {
3981 u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
3983 if ((reg76 & 0xf0) != 0xd0) {
3985 "Correcting VIA config for southbridge data corruption bug\n");
3986 pci_write_config(children[i], 0x75, 0x80, 1);
3987 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
3992 free(children, M_TEMP);
3996 /* common code for VIA, AMD & nVidia */
3998 ata_via_family_setmode(device_t dev, int mode)
4000 device_t gparent = GRANDPARENT(dev);
4001 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4002 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4003 struct ata_device *atadev = device_get_softc(dev);
4004 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
4005 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
4007 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* VIA ATA33 */
4008 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* VIA ATA66 */
4009 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* VIA ATA100 */
4010 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
4011 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }}; /* AMD/nVIDIA */
4012 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4013 int reg = 0x53 - devno;
4016 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4018 if (ctlr->chip->cfg2 & AMDCABLE) {
4019 if (mode > ATA_UDMA2 &&
4020 !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
4021 ata_print_cable(dev, "controller");
4026 mode = ata_check_80pin(dev, mode);
4028 if (ctlr->chip->cfg2 & NVIDIA)
4031 if (ctlr->chip->cfg1 != VIA133)
4032 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
4034 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4037 device_printf(dev, "%ssetting %s on %s chip\n",
4038 (error) ? "FAILURE " : "", ata_mode2str(mode),
4041 if (mode >= ATA_UDMA0)
4042 pci_write_config(gparent, reg,
4043 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
4045 pci_write_config(gparent, reg, 0x8b, 1);
4046 atadev->mode = mode;
4051 /* misc functions */
4052 struct ata_chip_id *
4053 ata_match_chip(device_t dev, struct ata_chip_id *index)
4055 while (index->chipid != 0) {
4056 if (pci_get_devid(dev) == index->chipid &&
4057 pci_get_revid(dev) >= index->chiprev)
4064 static struct ata_chip_id *
4065 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
4070 if (device_get_children(device_get_parent(dev), &children, &nchildren))
4073 while (index->chipid != 0) {
4074 for (i = 0; i < nchildren; i++) {
4075 if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
4076 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
4077 pci_get_devid(children[i]) == index->chipid &&
4078 pci_get_revid(children[i]) >= index->chiprev) {
4079 free(children, M_TEMP);
4085 free(children, M_TEMP);
4090 ata_setup_interrupt(device_t dev)
4092 struct ata_pci_controller *ctlr = device_get_softc(dev);
4093 int rid = ATA_IRQ_RID;
4095 if (!ata_legacy(dev)) {
4096 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
4097 RF_SHAREABLE | RF_ACTIVE))) {
4098 device_printf(dev, "unable to map interrupt\n");
4101 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
4102 ata_generic_intr, ctlr, &ctlr->handle))) {
4103 device_printf(dev, "unable to setup interrupt\n");
4110 struct ata_serialize {
4111 struct mtx locked_mtx;
4117 ata_serialize(device_t dev, int flags)
4119 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4120 struct ata_channel *ch = device_get_softc(dev);
4121 struct ata_serialize *serial;
4122 static int inited = 0;
4126 serial = malloc(sizeof(struct ata_serialize),
4127 M_TEMP, M_NOWAIT | M_ZERO);
4128 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
4129 serial->locked_ch = -1;
4130 serial->restart_ch = -1;
4131 device_set_ivars(ctlr->dev, serial);
4135 serial = device_get_ivars(ctlr->dev);
4137 mtx_lock(&serial->locked_mtx);
4140 if (serial->locked_ch == -1)
4141 serial->locked_ch = ch->unit;
4142 if (serial->locked_ch != ch->unit)
4143 serial->restart_ch = ch->unit;
4147 if (serial->locked_ch == ch->unit) {
4148 serial->locked_ch = -1;
4149 if (serial->restart_ch != -1) {
4150 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
4151 serial->restart_ch = -1;
4152 mtx_unlock(&serial->locked_mtx);
4163 res = serial->locked_ch;
4164 mtx_unlock(&serial->locked_mtx);
4169 ata_print_cable(device_t dev, u_int8_t *who)
4172 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
4176 ata_atapi(device_t dev)
4178 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4179 struct ata_device *atadev = device_get_softc(dev);
4181 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
4182 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
4186 ata_check_80pin(device_t dev, int mode)
4188 struct ata_device *atadev = device_get_softc(dev);
4190 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
4191 ata_print_cable(dev, "device");
4198 ata_mode2idx(int mode)
4200 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
4201 return (mode & ATA_MODE_MASK) + 8;
4202 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
4203 return (mode & ATA_MODE_MASK) + 5;
4204 return (mode & ATA_MODE_MASK) - ATA_PIO0;