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_dmainit(device_t dev);
70 static int ata_acard_chipinit(device_t dev);
71 static void ata_acard_intr(void *data);
72 static void ata_acard_850_setmode(device_t dev, int mode);
73 static void ata_acard_86X_setmode(device_t dev, int mode);
74 static int ata_ali_chipinit(device_t dev);
75 static int ata_ali_allocate(device_t dev);
76 static int ata_ali_sata_allocate(device_t dev);
77 static void ata_ali_reset(device_t dev);
78 static void ata_ali_setmode(device_t dev, int mode);
79 static int ata_amd_chipinit(device_t dev);
80 static int ata_ati_chipinit(device_t dev);
81 static void ata_ati_setmode(device_t dev, int mode);
82 static int ata_cyrix_chipinit(device_t dev);
83 static void ata_cyrix_setmode(device_t dev, int mode);
84 static int ata_cypress_chipinit(device_t dev);
85 static void ata_cypress_setmode(device_t dev, int mode);
86 static int ata_highpoint_chipinit(device_t dev);
87 static void ata_highpoint_intr(void *data);
88 static void ata_highpoint_setmode(device_t dev, int mode);
89 static int ata_highpoint_check_80pin(device_t dev, int mode);
90 static int ata_intel_chipinit(device_t dev);
91 static int ata_intel_31244_allocate(device_t dev);
92 static void ata_intel_31244_intr(void *data);
93 static void ata_intel_31244_reset(device_t dev);
94 static int ata_intel_31244_command(struct ata_request *request);
95 static void ata_intel_intr(void *data);
96 static void ata_intel_reset(device_t dev);
97 static void ata_intel_old_setmode(device_t dev, int mode);
98 static void ata_intel_new_setmode(device_t dev, int mode);
99 static int ata_ite_chipinit(device_t dev);
100 static void ata_ite_setmode(device_t dev, int mode);
101 static int ata_national_chipinit(device_t dev);
102 static void ata_national_setmode(device_t dev, int mode);
103 static int ata_nvidia_chipinit(device_t dev);
104 static int ata_nvidia_allocate(device_t dev);
105 static void ata_nvidia_intr(void *data);
106 static void ata_nvidia_reset(device_t dev);
107 static int ata_promise_chipinit(device_t dev);
108 static int ata_promise_mio_allocate(device_t dev);
109 static void ata_promise_mio_intr(void *data);
110 static void ata_promise_sx4_intr(void *data);
111 static void ata_promise_mio_dmainit(device_t dev);
112 static void ata_promise_mio_reset(device_t dev);
113 static int ata_promise_mio_command(struct ata_request *request);
114 static int ata_promise_sx4_command(struct ata_request *request);
115 static int ata_promise_apkt(u_int8_t *bytep, struct ata_request *request);
116 static void ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt);
117 static void ata_promise_next_hpkt(struct ata_pci_controller *ctlr);
118 static void ata_promise_tx2_intr(void *data);
119 static void ata_promise_old_intr(void *data);
120 static int ata_promise_new_dmastart(device_t dev);
121 static int ata_promise_new_dmastop(device_t dev);
122 static void ata_promise_new_dmareset(device_t dev);
123 static void ata_promise_new_dmainit(device_t dev);
124 static void ata_promise_setmode(device_t dev, int mode);
125 static int ata_serverworks_chipinit(device_t dev);
126 static void ata_serverworks_setmode(device_t dev, int mode);
127 static int ata_sii_chipinit(device_t dev);
128 static int ata_sii_allocate(device_t dev);
129 static void ata_sii_intr(void *data);
130 static void ata_cmd_intr(void *data);
131 static void ata_cmd_old_intr(void *data);
132 static void ata_sii_reset(device_t dev);
133 static void ata_sii_setmode(device_t dev, int mode);
134 static void ata_cmd_setmode(device_t dev, int mode);
135 static int ata_sis_chipinit(device_t dev);
136 static int ata_sis_allocate(device_t dev);
137 static void ata_sis_reset(device_t dev);
138 static void ata_sis_setmode(device_t dev, int mode);
139 static int ata_via_chipinit(device_t dev);
140 static int ata_via_allocate(device_t dev);
141 static void ata_via_reset(device_t dev);
142 static void ata_via_southbridge_fixup(device_t dev);
143 static void ata_via_family_setmode(device_t dev, int mode);
144 static struct ata_chip_id *ata_find_chip(device_t dev, struct ata_chip_id *index, int slot);
145 static int ata_setup_interrupt(device_t dev);
146 static int ata_serialize(device_t dev, int flags);
147 static void ata_print_cable(device_t dev, u_int8_t *who);
148 static int ata_atapi(device_t dev);
149 static int ata_check_80pin(device_t dev, int mode);
150 static int ata_mode2idx(int mode);
153 /* generic or unknown ATA chipset support functions */
155 ata_generic_ident(device_t dev)
157 struct ata_pci_controller *ctlr = device_get_softc(dev);
159 device_set_desc(dev, "GENERIC ATA controller");
160 ctlr->chipinit = ata_generic_chipinit;
165 ata_generic_chipinit(device_t dev)
167 struct ata_pci_controller *ctlr = device_get_softc(dev);
169 if (ata_setup_interrupt(dev))
171 ctlr->setmode = ata_generic_setmode;
176 ata_generic_intr(void *data)
178 struct ata_pci_controller *ctlr = data;
179 struct ata_channel *ch;
182 for (unit = 0; unit < ctlr->channels; unit++) {
183 if (!(ch = ctlr->interrupt[unit].argument))
185 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
186 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
188 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
189 ATA_BMSTAT_INTERRUPT)
191 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
194 ctlr->interrupt[unit].function(ch);
199 ata_generic_setmode(device_t dev, int mode)
201 struct ata_device *atadev = device_get_softc(dev);
203 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
204 mode = ata_check_80pin(dev, mode);
205 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
210 /* SATA support functions */
212 ata_sata_setmode(device_t dev, int mode)
214 struct ata_device *atadev = device_get_softc(dev);
217 * if we detect that the device isn't a real SATA device we limit
218 * the transfer mode to UDMA5/ATA100.
219 * this works around the problems some devices has with the
220 * Marvell 88SX8030 SATA->PATA converters and UDMA6/ATA133.
222 if (atadev->param.satacapabilities != 0x0000 &&
223 atadev->param.satacapabilities != 0xffff) {
224 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
225 ata_limit_mode(dev, mode, ATA_UDMA6)))
226 /* XXX SOS we should query SATA STATUS for the speed */
227 atadev->mode = ATA_SA150;
230 mode = ata_limit_mode(dev, mode, ATA_UDMA5);
231 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
237 ata_sata_connect(struct ata_channel *ch)
242 /* wait up to 1 second for "connect well" */
243 for (timeout = 0; timeout < 100 ; timeout++) {
244 status = ATA_IDX_INL(ch, ATA_SSTATUS);
245 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1 ||
246 (status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)
250 if (timeout >= 100) {
252 device_printf(ch->dev, "SATA connect status=%08x\n", status);
256 /* clear SATA error register */
257 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
259 /* find out what type device we got poll for spec'd 31 seconds */
260 /* XXX SOS 10 secs for now as I have little patience */
262 for (timeout = 0; timeout < 1000; timeout++) {
263 if (ATA_IDX_INB(ch, ATA_STATUS) & ATA_S_BUSY)
269 device_printf(ch->dev, "SATA connect ready time=%dms\n", timeout * 10);
270 if (timeout < 1000) {
271 if ((ATA_IDX_INB(ch, ATA_CYL_LSB) == ATAPI_MAGIC_LSB) &&
272 (ATA_IDX_INB(ch, ATA_CYL_MSB) == ATAPI_MAGIC_MSB))
273 ch->devices = ATA_ATAPI_MASTER;
275 ch->devices = ATA_ATA_MASTER;
278 device_printf(ch->dev, "sata_connect devices=0x%b\n",
279 ch->devices, "\20\3ATAPI_MASTER\1ATA_MASTER");
284 ata_sata_phy_enable(struct ata_channel *ch)
288 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE) {
289 ata_sata_connect(ch);
293 for (retry = 0; retry < 10; retry++) {
294 for (loop = 0; loop < 10; loop++) {
295 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_RESET);
297 if ((ATA_IDX_INL(ch, ATA_SCONTROL) &
298 ATA_SC_DET_MASK) == ATA_SC_DET_RESET)
302 for (loop = 0; loop < 10; loop++) {
303 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_IDLE);
305 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == 0) {
306 ata_sata_connect(ch);
314 ata_sata_phy_event(void *context, int dummy)
316 struct ata_connect_task *tp = (struct ata_connect_task *)context;
317 struct ata_channel *ch = device_get_softc(tp->dev);
321 mtx_lock(&Giant); /* newbus suckage it needs Giant */
322 if (tp->action == ATA_C_ATTACH) {
323 device_printf(tp->dev, "CONNECTED\n");
324 ata_sata_connect(ch);
325 ata_identify(tp->dev);
327 if (tp->action == ATA_C_DETACH) {
328 if (!device_get_children(tp->dev, &children, &nchildren)) {
329 for (i = 0; i < nchildren; i++)
331 device_delete_child(tp->dev, children[i]);
332 free(children, M_TEMP);
334 mtx_lock(&ch->state_mtx);
335 ch->state = ATA_IDLE;
336 mtx_unlock(&ch->state_mtx);
337 device_printf(tp->dev, "DISCONNECTED\n");
339 mtx_unlock(&Giant); /* suckage code dealt with, release Giant */
345 * AHCI v1.0 compliant SATA chipset support functions
347 struct ata_ahci_dma_prd {
350 u_int32_t dbc; /* 0 based */
351 #define ATA_AHCI_PRD_MASK 0x003fffff /* max 4MB */
352 #define ATA_AHCI_PRD_IPC (1<<31)
355 struct ata_ahci_cmd_tab {
358 u_int8_t reserved[32];
359 struct ata_ahci_dma_prd prd_tab[16];
362 struct ata_ahci_cmd_list {
364 u_int16_t prd_length; /* PRD entries */
366 u_int64_t cmd_table_phys; /* 128byte aligned */
371 ata_ahci_allocate(device_t dev)
373 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
374 struct ata_channel *ch = device_get_softc(dev);
375 int offset = (ch->unit << 7);
377 /* XXX SOS this is a hack to satisfy various legacy cruft */
378 ch->r_io[ATA_CYL_LSB].res = ctlr->r_res2;
379 ch->r_io[ATA_CYL_LSB].offset = ATA_AHCI_P_SIG + 1 + offset;
380 ch->r_io[ATA_CYL_MSB].res = ctlr->r_res2;
381 ch->r_io[ATA_CYL_MSB].offset = ATA_AHCI_P_SIG + 3 + offset;
382 ch->r_io[ATA_STATUS].res = ctlr->r_res2;
383 ch->r_io[ATA_STATUS].offset = ATA_AHCI_P_TFD + offset;
384 ch->r_io[ATA_ALTSTAT].res = ctlr->r_res2;
385 ch->r_io[ATA_ALTSTAT].offset = ATA_AHCI_P_TFD + offset;
387 /* set the SATA resources */
388 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
389 ch->r_io[ATA_SSTATUS].offset = ATA_AHCI_P_SSTS + offset;
390 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
391 ch->r_io[ATA_SERROR].offset = ATA_AHCI_P_SERR + offset;
392 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
393 ch->r_io[ATA_SCONTROL].offset = ATA_AHCI_P_SCTL + offset;
394 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
395 ch->r_io[ATA_SACTIVE].offset = ATA_AHCI_P_SACT + offset;
397 ch->hw.begin_transaction = ata_ahci_begin_transaction;
398 ch->hw.end_transaction = ata_ahci_end_transaction;
399 ch->hw.command = NULL; /* not used here */
401 /* setup the work areas */
402 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLB + offset,
403 ch->dma->work_bus + ATA_AHCI_CL_OFFSET);
404 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLBU + offset, 0x00000000);
406 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FB + offset,
407 ch->dma->work_bus + ATA_AHCI_FB_OFFSET);
408 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FBU + offset, 0x00000000);
410 /* enable wanted port interrupts */
411 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset,
412 (ATA_AHCI_P_IX_CPD | ATA_AHCI_P_IX_TFE | ATA_AHCI_P_IX_HBF |
413 ATA_AHCI_P_IX_HBD | ATA_AHCI_P_IX_IF | ATA_AHCI_P_IX_OF |
414 ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC | ATA_AHCI_P_IX_DP |
415 ATA_AHCI_P_IX_UF | ATA_AHCI_P_IX_SDB | ATA_AHCI_P_IX_DS |
416 ATA_AHCI_P_IX_PS | ATA_AHCI_P_IX_DHR));
418 /* start operations on this channel */
419 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
420 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
421 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
426 ata_ahci_setup_fis(u_int8_t *fis, struct ata_request *request)
428 struct ata_device *atadev = device_get_softc(request->dev);
431 /* XXX SOS add ATAPI commands support later */
432 ata_modify_if_48bit(request);
434 fis[idx++] = 0x27; /* host to device */
435 fis[idx++] = 0x80; /* command FIS (note PM goes here) */
436 fis[idx++] = request->u.ata.command;
437 fis[idx++] = request->u.ata.feature;
439 fis[idx++] = request->u.ata.lba;
440 fis[idx++] = request->u.ata.lba >> 8;
441 fis[idx++] = request->u.ata.lba >> 16;
442 fis[idx] = ATA_D_LBA | atadev->unit;
443 if (atadev->flags & ATA_D_48BIT_ACTIVE)
446 fis[idx++] |= (request->u.ata.lba >> 24 & 0x0f);
448 fis[idx++] = request->u.ata.lba >> 24;
449 fis[idx++] = request->u.ata.lba >> 32;
450 fis[idx++] = request->u.ata.lba >> 40;
451 fis[idx++] = request->u.ata.feature >> 8;
453 fis[idx++] = request->u.ata.count;
454 fis[idx++] = request->u.ata.count >> 8;
456 fis[idx++] = ATA_A_4BIT;
465 /* must be called with ATA channel locked and state_mtx held */
467 ata_ahci_begin_transaction(struct ata_request *request)
469 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
470 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
471 struct ata_ahci_cmd_tab *ctp;
472 struct ata_ahci_cmd_list *clp;
473 int fis_size, entries;
476 /* get a piece of the workspace for this request */
477 ctp = (struct ata_ahci_cmd_tab *)
478 (ch->dma->work + ATA_AHCI_CT_OFFSET + (ATA_AHCI_CT_SIZE * tag));
480 /* setup the FIS for this request */ /* XXX SOS ATAPI missing still */
481 if (!(fis_size = ata_ahci_setup_fis(&ctp->cfis[0], request))) {
482 device_printf(request->dev, "setting up SATA FIS failed\n");
483 request->result = EIO;
484 return ATA_OP_FINISHED;
487 /* if request moves data setup and load SG list */
488 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
489 if (ch->dma->load(ch->dev, request->data, request->bytecount,
490 request->flags & ATA_R_READ,
491 ctp->prd_tab, &entries)) {
492 device_printf(request->dev, "setting up DMA failed\n");
493 request->result = EIO;
494 return ATA_OP_FINISHED;
498 /* setup the command list entry */
499 clp = (struct ata_ahci_cmd_list *)
500 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
502 clp->prd_length = entries;
503 clp->cmd_flags = (request->flags & ATA_R_WRITE ? (1<<6) : 0) |
504 (request->flags & ATA_R_ATAPI ? (1<<5) : 0) |
505 (fis_size / sizeof(u_int32_t));
507 clp->cmd_table_phys = htole64(ch->dma->work_bus + ATA_AHCI_CT_OFFSET +
508 (ATA_AHCI_CT_SIZE * tag));
510 /* clear eventual ACTIVE bit */
511 ATA_IDX_OUTL(ch, ATA_SACTIVE, ATA_IDX_INL(ch, ATA_SACTIVE) & (1 << tag));
513 /* issue the command */
514 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CI + (ch->unit << 7), (1 << tag));
516 /* start the timeout */
517 callout_reset(&request->callout, request->timeout * hz,
518 (timeout_t*)ata_timeout, request);
519 return ATA_OP_CONTINUES;
522 /* must be called with ATA channel locked and state_mtx held */
524 ata_ahci_end_transaction(struct ata_request *request)
526 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
527 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
528 struct ata_ahci_cmd_list *clp;
532 /* kill the timeout */
533 callout_stop(&request->callout);
536 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + (ch->unit << 7));
537 request->status = tf_data;
539 /* if error status get details */
540 if (request->status & ATA_S_ERROR)
541 request->error = tf_data >> 8;
543 /* record how much data we actually moved */
544 clp = (struct ata_ahci_cmd_list *)
545 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
546 request->donecount = clp->bytecount;
548 /* release SG list etc */
549 ch->dma->unload(ch->dev);
551 return ATA_OP_FINISHED;
555 ata_ahci_intr(void *data)
557 struct ata_pci_controller *ctlr = data;
558 struct ata_channel *ch;
559 u_int32_t port, status, error, issued;
563 port = ATA_INL(ctlr->r_res2, ATA_AHCI_IS);
565 for (unit = 0; unit < ctlr->channels; unit++) {
566 if (port & (1 << unit)) {
567 if ((ch = ctlr->interrupt[unit].argument)) {
568 struct ata_connect_task *tp;
569 int offset = (ch->unit << 7);
571 error = ATA_INL(ctlr->r_res2, ATA_AHCI_P_SERR + offset);
572 status = ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset);
573 issued = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CI + offset);
574 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_SERR + offset, error);
575 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset, status);
577 /* do we have cold connect surprise */
578 if (status & ATA_AHCI_P_IX_CPD) {
579 printf("ata_ahci_intr status=%08x error=%08x issued=%08x\n",
580 status, error, issued);
583 /* check for and handle connect events */
584 if (((status & (ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC)) ==
586 (tp = (struct ata_connect_task *)
587 malloc(sizeof(struct ata_connect_task),
588 M_ATA, M_NOWAIT | M_ZERO))) {
590 device_printf(ch->dev, "CONNECT requested\n");
591 tp->action = ATA_C_ATTACH;
593 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
594 taskqueue_enqueue(taskqueue_thread, &tp->task);
597 /* check for and handle disconnect events */
598 if (((status & (ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC)) ==
599 ATA_AHCI_P_IX_PRC) &&
600 (tp = (struct ata_connect_task *)
601 malloc(sizeof(struct ata_connect_task),
602 M_ATA, M_NOWAIT | M_ZERO))) {
604 device_printf(ch->dev, "DISCONNECT requested\n");
605 tp->action = ATA_C_DETACH;
607 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
608 taskqueue_enqueue(taskqueue_thread, &tp->task);
611 /* any drive action to take care of ? */
612 if (!(issued & (1<<tag)))
613 ctlr->interrupt[unit].function(ch);
617 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, port);
621 ata_ahci_reset(device_t dev)
623 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
624 struct ata_channel *ch = device_get_softc(dev);
626 int offset = (ch->unit << 7);
628 /* kill off all activity on this channel */
629 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
630 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
631 cmd & ~(ATA_AHCI_P_CMD_CR | ATA_AHCI_P_CMD_FR |
632 ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
634 DELAY(500000); /* XXX SOS this is not entirely wrong */
637 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, ATA_AHCI_P_CMD_SUD);
639 ata_sata_phy_enable(ch);
641 /* clear any interrupts pending on this channel */
642 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset,
643 ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset));
645 /* start operations on this channel */
646 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
647 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
648 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
652 ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
654 struct ata_dmasetprd_args *args = xsc;
655 struct ata_ahci_dma_prd *prd = args->dmatab;
658 if (!(args->error = error)) {
659 for (i = 0; i < nsegs; i++) {
660 prd[i].dba = htole64(segs[i].ds_addr);
661 prd[i].dbc = htole32((segs[i].ds_len - 1) & ATA_AHCI_PRD_MASK);
668 ata_ahci_dmainit(device_t dev)
670 struct ata_channel *ch = device_get_softc(dev);
674 /* note start and stop are not used here */
675 ch->dma->setprd = ata_ahci_dmasetprd;
676 ch->dma->max_iosize = 8192 * DEV_BSIZE;
682 * Acard chipset support functions
685 ata_acard_ident(device_t dev)
687 struct ata_pci_controller *ctlr = device_get_softc(dev);
688 struct ata_chip_id *idx;
689 static struct ata_chip_id ids[] =
690 {{ ATA_ATP850R, 0, ATPOLD, 0x00, ATA_UDMA2, "Acard ATP850" },
691 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "Acard ATP860A" },
692 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "Acard ATP860R" },
693 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "Acard ATP865A" },
694 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "Acard ATP865R" },
695 { 0, 0, 0, 0, 0, 0}};
698 if (!(idx = ata_match_chip(dev, ids)))
701 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
702 device_set_desc_copy(dev, buffer);
704 ctlr->chipinit = ata_acard_chipinit;
709 ata_acard_chipinit(device_t dev)
711 struct ata_pci_controller *ctlr = device_get_softc(dev);
712 int rid = ATA_IRQ_RID;
714 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
715 RF_SHAREABLE | RF_ACTIVE))) {
716 device_printf(dev, "unable to map interrupt\n");
719 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
720 ata_acard_intr, ctlr, &ctlr->handle))) {
721 device_printf(dev, "unable to setup interrupt\n");
724 if (ctlr->chip->cfg1 == ATPOLD) {
725 ctlr->setmode = ata_acard_850_setmode;
726 ctlr->locking = ata_serialize;
729 ctlr->setmode = ata_acard_86X_setmode;
734 ata_acard_intr(void *data)
736 struct ata_pci_controller *ctlr = data;
737 struct ata_channel *ch;
740 for (unit = 0; unit < ctlr->channels; unit++) {
741 if (!(ch = ctlr->interrupt[unit].argument))
743 if (ctlr->chip->cfg1 == ATPOLD &&
744 ATA_LOCKING(ch->dev, ATA_LF_WHICH) != unit)
746 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
747 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
749 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
750 ATA_BMSTAT_INTERRUPT)
752 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
754 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
755 ATA_IDX_INB(ch, ATA_BMCMD_PORT)&~ATA_BMCMD_START_STOP);
758 ctlr->interrupt[unit].function(ch);
763 ata_acard_850_setmode(device_t dev, int mode)
765 device_t gparent = GRANDPARENT(dev);
766 struct ata_pci_controller *ctlr = device_get_softc(gparent);
767 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
768 struct ata_device *atadev = device_get_softc(dev);
769 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
772 mode = ata_limit_mode(dev, mode,
773 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
775 /* XXX SOS missing WDMA0+1 + PIO modes */
776 if (mode >= ATA_WDMA2) {
777 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
779 device_printf(dev, "%ssetting %s on %s chip\n",
780 (error) ? "FAILURE " : "",
781 ata_mode2str(mode), ctlr->chip->text);
783 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
785 reg54 &= ~(0x03 << (devno << 1));
786 if (mode >= ATA_UDMA0)
787 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
788 pci_write_config(gparent, 0x54, reg54, 1);
789 pci_write_config(gparent, 0x4a, 0xa6, 1);
790 pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
795 /* we could set PIO mode timings, but we assume the BIOS did that */
799 ata_acard_86X_setmode(device_t dev, int mode)
801 device_t gparent = GRANDPARENT(dev);
802 struct ata_pci_controller *ctlr = device_get_softc(gparent);
803 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
804 struct ata_device *atadev = device_get_softc(dev);
805 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
809 mode = ata_limit_mode(dev, mode,
810 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
812 mode = ata_check_80pin(dev, mode);
814 /* XXX SOS missing WDMA0+1 + PIO modes */
815 if (mode >= ATA_WDMA2) {
816 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
818 device_printf(dev, "%ssetting %s on %s chip\n",
819 (error) ? "FAILURE " : "",
820 ata_mode2str(mode), ctlr->chip->text);
822 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
824 reg44 &= ~(0x000f << (devno << 2));
825 if (mode >= ATA_UDMA0)
826 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
827 pci_write_config(gparent, 0x44, reg44, 2);
828 pci_write_config(gparent, 0x4a, 0xa6, 1);
829 pci_write_config(gparent, 0x40 + devno, 0x31, 1);
834 /* we could set PIO mode timings, but we assume the BIOS did that */
839 * Acer Labs Inc (ALI) chipset support functions
842 ata_ali_ident(device_t dev)
844 struct ata_pci_controller *ctlr = device_get_softc(dev);
845 struct ata_chip_id *idx;
846 static struct ata_chip_id ids[] =
847 {{ ATA_ALI_5289, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5289" },
848 { ATA_ALI_5287, 0x00, 4, ALISATA, ATA_SA150, "AcerLabs M5287" },
849 { ATA_ALI_5281, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5281" },
850 { ATA_ALI_5229, 0xc5, 0, ALINEW, ATA_UDMA6, "AcerLabs M5229" },
851 { ATA_ALI_5229, 0xc4, 0, ALINEW, ATA_UDMA5, "AcerLabs M5229" },
852 { ATA_ALI_5229, 0xc2, 0, ALINEW, ATA_UDMA4, "AcerLabs M5229" },
853 { ATA_ALI_5229, 0x20, 0, ALIOLD, ATA_UDMA2, "AcerLabs M5229" },
854 { ATA_ALI_5229, 0x00, 0, ALIOLD, ATA_WDMA2, "AcerLabs M5229" },
855 { 0, 0, 0, 0, 0, 0}};
858 if (!(idx = ata_match_chip(dev, ids)))
861 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
862 device_set_desc_copy(dev, buffer);
864 ctlr->chipinit = ata_ali_chipinit;
869 ata_ali_chipinit(device_t dev)
871 struct ata_pci_controller *ctlr = device_get_softc(dev);
873 if (ata_setup_interrupt(dev))
876 switch (ctlr->chip->cfg2) {
878 pci_write_config(dev, PCIR_COMMAND,
879 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
880 ctlr->channels = ctlr->chip->cfg1;
881 ctlr->allocate = ata_ali_sata_allocate;
882 ctlr->setmode = ata_sata_setmode;
886 /* use device interrupt as byte count end */
887 pci_write_config(dev, 0x4a, pci_read_config(dev, 0x4a, 1) | 0x20, 1);
889 /* enable cable detection and UDMA support on newer chips */
890 pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
892 /* enable ATAPI UDMA mode */
893 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x01, 1);
895 /* only chips with revision > 0xc4 can do 48bit DMA */
896 if (ctlr->chip->chiprev <= 0xc4)
898 "using PIO transfers above 137GB as workaround for "
899 "48bit DMA access bug, expect reduced performance\n");
900 ctlr->reset = ata_ali_reset;
901 ctlr->allocate = ata_ali_allocate;
902 ctlr->setmode = ata_ali_setmode;
906 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
907 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x03, 1);
908 ctlr->setmode = ata_ali_setmode;
915 ata_ali_allocate(device_t dev)
917 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
918 struct ata_channel *ch = device_get_softc(dev);
920 /* setup the usual register normal pci style */
921 if (ata_pci_allocate(dev))
924 /* older chips can't do 48bit DMA transfers */
925 if (ctlr->chip->chiprev <= 0xc4)
926 ch->flags |= ATA_NO_48BIT_DMA;
932 ata_ali_sata_allocate(device_t dev)
934 device_t parent = device_get_parent(dev);
935 struct ata_pci_controller *ctlr = device_get_softc(parent);
936 struct ata_channel *ch = device_get_softc(dev);
937 struct resource *io = NULL, *ctlio = NULL;
938 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
941 rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
942 io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
946 rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
947 ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
949 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
953 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
954 ch->r_io[i].res = io;
955 ch->r_io[i].offset = i + (unit10 ? 8 : 0);
957 ch->r_io[ATA_CONTROL].res = ctlio;
958 ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
959 ch->r_io[ATA_IDX_ADDR].res = io;
960 ata_default_registers(dev);
962 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
963 ch->r_io[i].res = ctlr->r_res1;
964 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
967 ch->flags |= ATA_NO_SLAVE;
969 /* XXX SOS PHY handling awkward in ALI chip not supported yet */
975 ata_ali_reset(device_t dev)
977 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
978 struct ata_channel *ch = device_get_softc(dev);
982 ata_generic_reset(dev);
985 * workaround for datacorruption bug found on at least SUN Blade-100
986 * find the ISA function on the southbridge and disable then enable
987 * the ATA channel tristate buffer
989 if (ctlr->chip->chiprev == 0xc3 || ctlr->chip->chiprev == 0xc2) {
990 if (!device_get_children(GRANDPARENT(dev), &children, &nchildren)) {
991 for (i = 0; i < nchildren; i++) {
992 if (pci_get_devid(children[i]) == ATA_ALI_1533) {
993 pci_write_config(children[i], 0x58,
994 pci_read_config(children[i], 0x58, 1) &
995 ~(0x04 << ch->unit), 1);
996 pci_write_config(children[i], 0x58,
997 pci_read_config(children[i], 0x58, 1) |
998 (0x04 << ch->unit), 1);
1002 free(children, M_TEMP);
1008 ata_ali_setmode(device_t dev, int mode)
1010 device_t gparent = GRANDPARENT(dev);
1011 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1012 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1013 struct ata_device *atadev = device_get_softc(dev);
1014 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1017 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1019 if (ctlr->chip->cfg2 & ALINEW) {
1020 if (mode > ATA_UDMA2 &&
1021 pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
1022 ata_print_cable(dev, "controller");
1027 mode = ata_check_80pin(dev, mode);
1029 if (ctlr->chip->cfg2 & ALIOLD) {
1030 /* doesn't support ATAPI DMA on write */
1031 ch->flags |= ATA_ATAPI_DMA_RO;
1032 if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
1033 /* doesn't support ATAPI DMA on two ATAPI devices */
1034 device_printf(dev, "two atapi devices on this channel, no DMA\n");
1035 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1039 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1042 device_printf(dev, "%ssetting %s on %s chip\n",
1043 (error) ? "FAILURE " : "",
1044 ata_mode2str(mode), ctlr->chip->text);
1046 if (mode >= ATA_UDMA0) {
1047 u_int8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
1048 u_int32_t word54 = pci_read_config(gparent, 0x54, 4);
1050 word54 &= ~(0x000f000f << (devno << 2));
1051 word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
1052 pci_write_config(gparent, 0x54, word54, 4);
1053 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1057 u_int32_t piotimings[] =
1058 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
1059 0x00310001, 0x00440001, 0x00330001, 0x00310001};
1061 pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
1062 ~(0x0008000f << (devno << 2)), 4);
1063 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1064 piotimings[ata_mode2idx(mode)], 4);
1066 atadev->mode = mode;
1072 * American Micro Devices (AMD) chipset support functions
1075 ata_amd_ident(device_t dev)
1077 struct ata_pci_controller *ctlr = device_get_softc(dev);
1078 struct ata_chip_id *idx;
1079 static struct ata_chip_id ids[] =
1080 {{ ATA_AMD756, 0x00, AMDNVIDIA, 0x00, ATA_UDMA4, "AMD 756" },
1081 { ATA_AMD766, 0x00, AMDNVIDIA, AMDCABLE|AMDBUG, ATA_UDMA5, "AMD 766" },
1082 { ATA_AMD768, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA5, "AMD 768" },
1083 { ATA_AMD8111, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA6, "AMD 8111" },
1084 { 0, 0, 0, 0, 0, 0}};
1087 if (!(idx = ata_match_chip(dev, ids)))
1090 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1091 device_set_desc_copy(dev, buffer);
1093 ctlr->chipinit = ata_amd_chipinit;
1098 ata_amd_chipinit(device_t dev)
1100 struct ata_pci_controller *ctlr = device_get_softc(dev);
1102 if (ata_setup_interrupt(dev))
1105 /* disable/set prefetch, postwrite */
1106 if (ctlr->chip->cfg2 & AMDBUG)
1107 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
1109 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
1111 ctlr->setmode = ata_via_family_setmode;
1117 * ATI chipset support functions
1120 ata_ati_ident(device_t dev)
1122 struct ata_pci_controller *ctlr = device_get_softc(dev);
1123 struct ata_chip_id *idx;
1124 static struct ata_chip_id ids[] =
1125 {{ ATA_ATI_IXP200, 0x00, 0, 0, ATA_UDMA5, "ATI IXP200" },
1126 { ATA_ATI_IXP300, 0x00, 0, 0, ATA_UDMA6, "ATI IXP300" },
1127 { ATA_ATI_IXP400, 0x00, 0, 0, ATA_UDMA6, "ATI IXP400" },
1128 { ATA_ATI_IXP300_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "ATI IXP300" },
1129 { ATA_ATI_IXP400_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "ATI IXP400" },
1130 { ATA_ATI_IXP400_S2, 0x00, SIIMEMIO, 0, ATA_SA150, "ATI IXP400" },
1131 { 0, 0, 0, 0, 0, 0}};
1134 if (!(idx = ata_match_chip(dev, ids)))
1137 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1138 device_set_desc_copy(dev, buffer);
1141 /* the ATI SATA controller is actually a SiI 3112 controller*/
1142 if (ctlr->chip->cfg1 & SIIMEMIO)
1143 ctlr->chipinit = ata_sii_chipinit;
1145 ctlr->chipinit = ata_ati_chipinit;
1150 ata_ati_chipinit(device_t dev)
1152 struct ata_pci_controller *ctlr = device_get_softc(dev);
1154 if (ata_setup_interrupt(dev))
1157 ctlr->setmode = ata_ati_setmode;
1162 ata_ati_setmode(device_t dev, int mode)
1164 device_t gparent = GRANDPARENT(dev);
1165 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1166 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1167 struct ata_device *atadev = device_get_softc(dev);
1168 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1169 int offset = (devno ^ 0x01) << 3;
1171 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
1172 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
1173 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
1175 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1177 mode = ata_check_80pin(dev, mode);
1179 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1182 device_printf(dev, "%ssetting %s on %s chip\n",
1183 (error) ? "FAILURE " : "",
1184 ata_mode2str(mode), ctlr->chip->text);
1186 if (mode >= ATA_UDMA0) {
1187 pci_write_config(gparent, 0x56,
1188 (pci_read_config(gparent, 0x56, 2) &
1189 ~(0xf << (devno << 2))) |
1190 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
1191 pci_write_config(gparent, 0x54,
1192 pci_read_config(gparent, 0x54, 1) |
1193 (0x01 << devno), 1);
1194 pci_write_config(gparent, 0x44,
1195 (pci_read_config(gparent, 0x44, 4) &
1196 ~(0xff << offset)) |
1197 (dmatimings[2] << offset), 4);
1199 else if (mode >= ATA_WDMA0) {
1200 pci_write_config(gparent, 0x54,
1201 pci_read_config(gparent, 0x54, 1) &
1202 ~(0x01 << devno), 1);
1203 pci_write_config(gparent, 0x44,
1204 (pci_read_config(gparent, 0x44, 4) &
1205 ~(0xff << offset)) |
1206 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
1209 pci_write_config(gparent, 0x54,
1210 pci_read_config(gparent, 0x54, 1) &
1211 ~(0x01 << devno), 1);
1213 pci_write_config(gparent, 0x4a,
1214 (pci_read_config(gparent, 0x4a, 2) &
1215 ~(0xf << (devno << 2))) |
1216 (((mode - ATA_PIO0) & ATA_MODE_MASK) << (devno<<2)),2);
1217 pci_write_config(gparent, 0x40,
1218 (pci_read_config(gparent, 0x40, 4) &
1219 ~(0xff << offset)) |
1220 (piotimings[ata_mode2idx(mode)] << offset), 4);
1221 atadev->mode = mode;
1226 * Cyrix chipset support functions
1229 ata_cyrix_ident(device_t dev)
1231 struct ata_pci_controller *ctlr = device_get_softc(dev);
1233 if (pci_get_devid(dev) == ATA_CYRIX_5530) {
1234 device_set_desc(dev, "Cyrix 5530 ATA33 controller");
1235 ctlr->chipinit = ata_cyrix_chipinit;
1242 ata_cyrix_chipinit(device_t dev)
1244 struct ata_pci_controller *ctlr = device_get_softc(dev);
1246 if (ata_setup_interrupt(dev))
1250 ctlr->setmode = ata_cyrix_setmode;
1252 ctlr->setmode = ata_generic_setmode;
1257 ata_cyrix_setmode(device_t dev, int mode)
1259 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1260 struct ata_device *atadev = device_get_softc(dev);
1261 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1262 u_int32_t piotiming[] =
1263 { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
1264 u_int32_t dmatiming[] = { 0x00077771, 0x00012121, 0x00002020 };
1265 u_int32_t udmatiming[] = { 0x00921250, 0x00911140, 0x00911030 };
1268 ch->dma->alignment = 16;
1269 ch->dma->max_iosize = 126 * DEV_BSIZE;
1271 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1273 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1276 device_printf(dev, "%ssetting %s on Cyrix chip\n",
1277 (error) ? "FAILURE " : "", ata_mode2str(mode));
1279 if (mode >= ATA_UDMA0) {
1280 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1281 0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
1283 else if (mode >= ATA_WDMA0) {
1284 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1285 0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
1288 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1289 0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
1291 atadev->mode = mode;
1297 * Cypress chipset support functions
1300 ata_cypress_ident(device_t dev)
1302 struct ata_pci_controller *ctlr = device_get_softc(dev);
1305 * the Cypress chip is a mess, it contains two ATA functions, but
1306 * both channels are visible on the first one.
1307 * simply ignore the second function for now, as the right
1308 * solution (ignoring the second channel on the first function)
1309 * doesn't work with the crappy ATA interrupt setup on the alpha.
1311 if (pci_get_devid(dev) == ATA_CYPRESS_82C693 &&
1312 pci_get_function(dev) == 1 &&
1313 pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
1314 device_set_desc(dev, "Cypress 82C693 ATA controller");
1315 ctlr->chipinit = ata_cypress_chipinit;
1322 ata_cypress_chipinit(device_t dev)
1324 struct ata_pci_controller *ctlr = device_get_softc(dev);
1326 if (ata_setup_interrupt(dev))
1329 ctlr->setmode = ata_cypress_setmode;
1334 ata_cypress_setmode(device_t dev, int mode)
1336 device_t gparent = GRANDPARENT(dev);
1337 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1338 struct ata_device *atadev = device_get_softc(dev);
1341 mode = ata_limit_mode(dev, mode, ATA_WDMA2);
1343 /* XXX SOS missing WDMA0+1 + PIO modes */
1344 if (mode == ATA_WDMA2) {
1345 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1347 device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
1348 error ? "FAILURE " : "");
1350 pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
1351 atadev->mode = mode;
1355 /* we could set PIO mode timings, but we assume the BIOS did that */
1360 * HighPoint chipset support functions
1363 ata_highpoint_ident(device_t dev)
1365 struct ata_pci_controller *ctlr = device_get_softc(dev);
1366 struct ata_chip_id *idx;
1367 static struct ata_chip_id ids[] =
1368 {{ ATA_HPT374, 0x07, HPT374, 0x00, ATA_UDMA6, "HighPoint HPT374" },
1369 { ATA_HPT372, 0x02, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372N" },
1370 { ATA_HPT372, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372" },
1371 { ATA_HPT371, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT371" },
1372 { ATA_HPT366, 0x05, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372" },
1373 { ATA_HPT366, 0x03, HPT370, 0x00, ATA_UDMA5, "HighPoint HPT370" },
1374 { ATA_HPT366, 0x02, HPT366, 0x00, ATA_UDMA4, "HighPoint HPT368" },
1375 { ATA_HPT366, 0x00, HPT366, HPTOLD, ATA_UDMA4, "HighPoint HPT366" },
1376 { ATA_HPT302, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT302" },
1377 { 0, 0, 0, 0, 0, 0}};
1380 if (!(idx = ata_match_chip(dev, ids)))
1383 strcpy(buffer, idx->text);
1384 if (idx->cfg1 == HPT374) {
1385 if (pci_get_function(dev) == 0)
1386 strcat(buffer, " (channel 0+1)");
1387 else if (pci_get_function(dev) == 1)
1388 strcat(buffer, " (channel 2+3)");
1390 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
1391 device_set_desc_copy(dev, buffer);
1393 ctlr->chipinit = ata_highpoint_chipinit;
1398 ata_highpoint_chipinit(device_t dev)
1400 struct ata_pci_controller *ctlr = device_get_softc(dev);
1401 int rid = ATA_IRQ_RID;
1403 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1404 RF_SHAREABLE | RF_ACTIVE))) {
1405 device_printf(dev, "unable to map interrupt\n");
1408 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1409 ata_highpoint_intr, ctlr, &ctlr->handle))) {
1410 device_printf(dev, "unable to setup interrupt\n");
1414 if (ctlr->chip->cfg2 == HPTOLD) {
1415 /* disable interrupt prediction */
1416 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
1419 /* disable interrupt prediction */
1420 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
1421 pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
1423 /* enable interrupts */
1424 pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
1426 /* set clocks etc */
1427 if (ctlr->chip->cfg1 < HPT372)
1428 pci_write_config(dev, 0x5b, 0x22, 1);
1430 pci_write_config(dev, 0x5b,
1431 (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
1433 ctlr->setmode = ata_highpoint_setmode;
1438 ata_highpoint_intr(void *data)
1440 struct ata_pci_controller *ctlr = data;
1441 struct ata_channel *ch;
1444 for (unit = 0; unit < ctlr->channels; unit++) {
1445 if (!(ch = ctlr->interrupt[unit].argument))
1448 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1450 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1451 ATA_BMSTAT_INTERRUPT)
1453 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1456 ctlr->interrupt[unit].function(ch);
1461 ata_highpoint_setmode(device_t dev, int mode)
1463 device_t gparent = GRANDPARENT(dev);
1464 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1465 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1466 struct ata_device *atadev = device_get_softc(dev);
1467 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1469 u_int32_t timings33[][4] = {
1470 /* HPT366 HPT370 HPT372 HPT374 mode */
1471 { 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a }, /* PIO 0 */
1472 { 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 }, /* PIO 1 */
1473 { 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 }, /* PIO 2 */
1474 { 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 }, /* PIO 3 */
1475 { 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 }, /* PIO 4 */
1476 { 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea }, /* MWDMA 0 */
1477 { 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 }, /* MWDMA 1 */
1478 { 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 }, /* MWDMA 2 */
1479 { 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea }, /* UDMA 0 */
1480 { 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 }, /* UDMA 1 */
1481 { 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 }, /* UDMA 2 */
1482 { 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 }, /* UDMA 3 */
1483 { 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 }, /* UDMA 4 */
1484 { 0, 0x16454e31, 0x1c8a9c62, 0x12848242 }, /* UDMA 5 */
1485 { 0, 0, 0x1c869c62, 0x12808242 } /* UDMA 6 */
1488 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1490 if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
1491 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1493 mode = ata_highpoint_check_80pin(dev, mode);
1496 * most if not all HPT chips cant really handle that the device is
1497 * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
1498 * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
1500 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
1501 ata_limit_mode(dev, mode, ATA_UDMA5));
1503 device_printf(dev, "%ssetting %s on HighPoint chip\n",
1504 (error) ? "FAILURE " : "", ata_mode2str(mode));
1506 pci_write_config(gparent, 0x40 + (devno << 2),
1507 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
1508 atadev->mode = mode;
1512 ata_highpoint_check_80pin(device_t dev, int mode)
1514 device_t gparent = GRANDPARENT(dev);
1515 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1516 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1517 u_int8_t reg, val, res;
1519 if (ctlr->chip->cfg1 == HPT374 && pci_get_function(gparent) == 1) {
1520 reg = ch->unit ? 0x57 : 0x53;
1521 val = pci_read_config(gparent, reg, 1);
1522 pci_write_config(gparent, reg, val | 0x80, 1);
1526 val = pci_read_config(gparent, reg, 1);
1527 pci_write_config(gparent, reg, val & 0xfe, 1);
1529 res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1530 pci_write_config(gparent, reg, val, 1);
1532 if (mode > ATA_UDMA2 && res) {
1533 ata_print_cable(dev, "controller");
1541 * Intel chipset support functions
1544 ata_intel_ident(device_t dev)
1546 struct ata_pci_controller *ctlr = device_get_softc(dev);
1547 struct ata_chip_id *idx;
1548 static struct ata_chip_id ids[] =
1549 {{ ATA_I82371FB, 0, 0, 0x00, ATA_WDMA2, "Intel PIIX" },
1550 { ATA_I82371SB, 0, 0, 0x00, ATA_WDMA2, "Intel PIIX3" },
1551 { ATA_I82371AB, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1552 { ATA_I82443MX, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1553 { ATA_I82451NX, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1554 { ATA_I82801AB, 0, 0, 0x00, ATA_UDMA2, "Intel ICH0" },
1555 { ATA_I82801AA, 0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1556 { ATA_I82372FB, 0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1557 { ATA_I82801BA, 0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1558 { ATA_I82801BA_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1559 { ATA_I82801CA, 0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1560 { ATA_I82801CA_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1561 { ATA_I82801DB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1562 { ATA_I82801DB_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1563 { ATA_I82801EB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH5" },
1564 { ATA_I82801EB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1565 { ATA_I82801EB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1566 { ATA_I6300ESB, 0, 0, 0x00, ATA_UDMA5, "Intel 6300ESB" },
1567 { ATA_I6300ESB_S1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1568 { ATA_I6300ESB_R1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1569 { ATA_I82801FB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH6" },
1570 { ATA_I82801FB_S1, 0, AHCI, 0x00, ATA_SA150, "Intel ICH6" },
1571 { ATA_I82801FB_R1, 0, AHCI, 0x00, ATA_SA150, "Intel ICH6" },
1572 { ATA_I82801FB_M, 0, AHCI, 0x00, ATA_SA150, "Intel ICH6" },
1573 { ATA_I82801GB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH7" },
1574 { ATA_I82801GB_S1, 0, AHCI, 0x00, ATA_SA300, "Intel ICH7" },
1575 { ATA_I82801GB_R1, 0, AHCI, 0x00, ATA_SA300, "Intel ICH7" },
1576 { ATA_I82801GB_M, 0, AHCI, 0x00, ATA_SA300, "Intel ICH7" },
1577 { ATA_I82801GB_AH, 0, AHCI, 0x00, ATA_SA300, "Intel ICH7" },
1578 { ATA_I31244, 0, 0, 0x00, ATA_SA150, "Intel 31244" },
1579 { 0, 0, 0, 0, 0, 0}};
1582 if (!(idx = ata_match_chip(dev, ids)))
1585 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1586 device_set_desc_copy(dev, buffer);
1588 ctlr->chipinit = ata_intel_chipinit;
1593 ata_intel_chipinit(device_t dev)
1595 struct ata_pci_controller *ctlr = device_get_softc(dev);
1596 int rid = ATA_IRQ_RID;
1598 if (!ata_legacy(dev)) {
1599 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1600 RF_SHAREABLE | RF_ACTIVE))) {
1601 device_printf(dev, "unable to map interrupt\n");
1604 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1605 ata_intel_intr, ctlr, &ctlr->handle))) {
1606 device_printf(dev, "unable to setup interrupt\n");
1611 /* good old PIIX needs special treatment (not implemented) */
1612 if (ctlr->chip->chipid == ATA_I82371FB) {
1613 ctlr->setmode = ata_intel_old_setmode;
1616 /* the intel 31244 needs special care if in DPA mode */
1617 else if (ctlr->chip->chipid == ATA_I31244) {
1618 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE) {
1619 ctlr->r_type2 = SYS_RES_MEMORY;
1620 ctlr->r_rid2 = PCIR_BAR(0);
1621 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1626 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
1627 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1628 ata_intel_31244_intr, ctlr, &ctlr->handle)) {
1629 device_printf(dev, "unable to setup interrupt\n");
1633 ctlr->reset = ata_intel_31244_reset;
1634 ctlr->allocate = ata_intel_31244_allocate;
1636 ctlr->setmode = ata_sata_setmode;
1639 /* non SATA intel chips goes here */
1640 else if (ctlr->chip->max_dma < ATA_SA150) {
1641 ctlr->setmode = ata_intel_new_setmode;
1644 /* SATA parts can be either compat or AHCI */
1646 /* force all ports active "the legacy way" */
1647 pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f,2);
1649 ctlr->reset = ata_intel_reset;
1651 /* if we have AHCI capability and BAR(5) as a memory resource */
1652 if (ctlr->chip->cfg1 == AHCI) {
1653 ctlr->r_type2 = SYS_RES_MEMORY;
1654 ctlr->r_rid2 = PCIR_BAR(5);
1655 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1658 /* is AHCI or RAID mode enabled in BIOS ? */
1659 if (pci_read_config(dev, 0x90, 1) & 0xc0) {
1660 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
1661 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1662 ata_ahci_intr, ctlr, &ctlr->handle)) {
1663 device_printf(dev, "unable to setup interrupt\n");
1667 /* enable AHCI mode */
1668 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC, ATA_AHCI_GHC_AE);
1670 /* get the number of HW channels */
1671 ctlr->channels = (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) &
1672 ATA_AHCI_NPMASK) + 1;
1674 /* enable AHCI interrupts */
1675 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
1676 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) |
1678 ctlr->reset = ata_ahci_reset;
1679 ctlr->dmainit = ata_ahci_dmainit;
1680 ctlr->allocate = ata_ahci_allocate;
1684 ctlr->setmode = ata_sata_setmode;
1686 /* enable PCI interrupt */
1687 pci_write_config(dev, PCIR_COMMAND,
1688 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1694 ata_intel_31244_allocate(device_t dev)
1696 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1697 struct ata_channel *ch = device_get_softc(dev);
1701 ch_offset = 0x200 + ch->unit * 0x200;
1703 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
1704 ch->r_io[i].res = ctlr->r_res2;
1705 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
1706 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x06;
1707 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
1708 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
1709 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
1710 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
1711 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
1712 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1d;
1713 ch->r_io[ATA_ERROR].offset = ch_offset + 0x04;
1714 ch->r_io[ATA_STATUS].offset = ch_offset + 0x1c;
1715 ch->r_io[ATA_ALTSTAT].offset = ch_offset + 0x28;
1716 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x29;
1717 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x100;
1718 ch->r_io[ATA_SERROR].offset = ch_offset + 0x104;
1719 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x108;
1720 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x70;
1721 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x72;
1722 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x74;
1724 ch->flags |= ATA_NO_SLAVE;
1725 ata_generic_hw(dev);
1726 ch->hw.command = ata_intel_31244_command;
1728 /* enable PHY state change interrupt */
1729 ATA_OUTL(ctlr->r_res2, 0x4,
1730 ATA_INL(ctlr->r_res2, 0x04) | (0x01 << (ch->unit << 3)));
1735 ata_intel_31244_intr(void *data)
1737 struct ata_pci_controller *ctlr = data;
1738 struct ata_channel *ch;
1741 for (unit = 0; unit < ctlr->channels; unit++) {
1742 if (!(ch = ctlr->interrupt[unit].argument))
1745 /* check for PHY related interrupts on SATA capable HW */
1746 if (ctlr->chip->max_dma >= ATA_SA150) {
1747 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
1748 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
1749 struct ata_connect_task *tp;
1752 /* clear error bits/interrupt */
1753 ATA_IDX_OUTL(ch, ATA_SERROR, error);
1755 /* if we have a connection event deal with it */
1756 if ((error & ATA_SE_PHY_CHANGED) &&
1757 (tp = (struct ata_connect_task *)
1758 malloc(sizeof(struct ata_connect_task),
1759 M_ATA, M_NOWAIT | M_ZERO))) {
1761 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
1762 device_printf(ch->dev, "CONNECT requested\n");
1763 tp->action = ATA_C_ATTACH;
1766 device_printf(ch->dev, "DISCONNECT requested\n");
1767 tp->action = ATA_C_DETACH;
1770 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1771 taskqueue_enqueue(taskqueue_thread, &tp->task);
1776 /* any drive action to take care of ? */
1777 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
1778 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1780 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
1782 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1785 ctlr->interrupt[unit].function(ch);
1790 ata_intel_31244_reset(device_t dev)
1792 struct ata_channel *ch = device_get_softc(dev);
1794 ata_sata_phy_enable(ch);
1798 ata_intel_31244_command(struct ata_request *request)
1800 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
1801 struct ata_device *atadev = device_get_softc(request->dev);
1804 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
1805 return (ata_generic_command(request));
1807 lba = request->u.ata.lba;
1808 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | atadev->unit);
1809 /* enable interrupt */
1810 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
1811 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
1812 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
1813 ATA_IDX_OUTW(ch, ATA_SECTOR, ((lba >> 16) & 0xff00) | (lba & 0x00ff));
1814 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((lba >> 24) & 0xff00) |
1815 ((lba >> 8) & 0x00ff));
1816 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((lba >> 32) & 0xff00) |
1817 ((lba >> 16) & 0x00ff));
1819 /* issue command to controller */
1820 ATA_IDX_OUTB(ch, ATA_COMMAND, request->u.ata.command);
1826 ata_intel_intr(void *data)
1828 struct ata_pci_controller *ctlr = data;
1829 struct ata_channel *ch;
1832 for (unit = 0; unit < ctlr->channels; unit++) {
1833 if (!(ch = ctlr->interrupt[unit].argument))
1836 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1838 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1839 ATA_BMSTAT_INTERRUPT)
1841 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1844 ctlr->interrupt[unit].function(ch);
1849 ata_intel_reset(device_t dev)
1851 device_t parent = device_get_parent(dev);
1852 struct ata_pci_controller *ctlr = device_get_softc(parent);
1853 struct ata_channel *ch = device_get_softc(dev);
1856 /* ICH6 & ICH7 in compat mode has 4 SATA ports as master/slave on 2 ch's */
1857 if (ctlr->chip->cfg1) {
1858 mask = (0x0005 << ch->unit);
1861 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1862 if (pci_read_config(parent, 0x90, 1) & 0x04)
1865 mask = (0x0001 << ch->unit);
1866 /* XXX SOS should be in intel_allocate if we grow it */
1867 ch->flags |= ATA_NO_SLAVE;
1870 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1872 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1874 /* wait up to 1 sec for "connect well" */
1875 for (timeout = 0; timeout < 100 ; timeout++) {
1876 if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1877 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1881 ata_generic_reset(dev);
1885 ata_intel_old_setmode(device_t dev, int mode)
1891 ata_intel_new_setmode(device_t dev, int mode)
1893 device_t gparent = GRANDPARENT(dev);
1894 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1895 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1896 struct ata_device *atadev = device_get_softc(dev);
1897 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1898 u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
1899 u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
1900 u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
1901 u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
1902 u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
1903 u_int32_t mask40 = 0, new40 = 0;
1904 u_int8_t mask44 = 0, new44 = 0;
1906 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1907 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1909 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1911 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1912 ata_print_cable(dev, "controller");
1916 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1919 device_printf(dev, "%ssetting %s on %s chip\n",
1920 (error) ? "FAILURE " : "",
1921 ata_mode2str(mode), ctlr->chip->text);
1925 if (mode >= ATA_UDMA0) {
1926 pci_write_config(gparent, 0x48, reg48 | (0x0001 << devno), 2);
1927 pci_write_config(gparent, 0x4a,
1928 (reg4a & ~(0x3 << (devno << 2))) |
1929 ((0x01 + !(mode & 0x01)) << (devno << 2)), 2);
1932 pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
1933 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))), 2);
1936 if (mode >= ATA_UDMA2)
1937 pci_write_config(gparent, 0x54, reg54 | (0x1 << devno), 2);
1939 pci_write_config(gparent, 0x54, reg54 & ~(0x1 << devno), 2);
1941 if (mode >= ATA_UDMA5)
1942 pci_write_config(gparent, 0x54, reg54 | (0x1000 << devno), 2);
1944 pci_write_config(gparent, 0x54, reg54 & ~(0x1000 << devno), 2);
1946 reg40 &= ~0x00ff00ff;
1947 reg40 |= 0x40774077;
1949 if (atadev->unit == ATA_MASTER) {
1951 new40 = timings[ata_mode2idx(mode)] << 8;
1955 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
1956 (timings[ata_mode2idx(mode)] & 0x03);
1964 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
1965 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
1967 atadev->mode = mode;
1972 * Integrated Technology Express Inc. (ITE) chipset support functions
1975 ata_ite_ident(device_t dev)
1977 struct ata_pci_controller *ctlr = device_get_softc(dev);
1978 struct ata_chip_id *idx;
1979 static struct ata_chip_id ids[] =
1980 {{ ATA_IT8212F, 0x00, 0x00, 0x00, ATA_UDMA6, "ITE IT8212F" },
1981 { ATA_IT8211F, 0x00, 0x00, 0x00, ATA_UDMA6, "ITE IT8211F" },
1982 { 0, 0, 0, 0, 0, 0}};
1985 if (!(idx = ata_match_chip(dev, ids)))
1988 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1989 device_set_desc_copy(dev, buffer);
1991 ctlr->chipinit = ata_ite_chipinit;
1996 ata_ite_chipinit(device_t dev)
1998 struct ata_pci_controller *ctlr = device_get_softc(dev);
2000 if (ata_setup_interrupt(dev))
2003 ctlr->setmode = ata_ite_setmode;
2005 /* set PCI mode and 66Mhz reference clock */
2006 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
2008 /* set default active & recover timings */
2009 pci_write_config(dev, 0x54, 0x31, 1);
2010 pci_write_config(dev, 0x56, 0x31, 1);
2015 ata_ite_setmode(device_t dev, int mode)
2017 device_t gparent = GRANDPARENT(dev);
2018 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2019 struct ata_device *atadev = device_get_softc(dev);
2020 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2023 /* correct the mode for what the HW supports */
2024 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2026 /* check the CBLID bits for 80 conductor cable detection */
2027 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
2028 (ch->unit ? (1<<3) : (1<<2)))) {
2029 ata_print_cable(dev, "controller");
2033 /* set the wanted mode on the device */
2034 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2037 device_printf(dev, "%s setting %s on ITE8212F chip\n",
2038 (error) ? "failed" : "success", ata_mode2str(mode));
2040 /* if the device accepted the mode change, setup the HW accordingly */
2042 if (mode >= ATA_UDMA0) {
2043 u_int8_t udmatiming[] =
2044 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
2046 /* enable UDMA mode */
2047 pci_write_config(gparent, 0x50,
2048 pci_read_config(gparent, 0x50, 1) &
2049 ~(1 << (devno + 3)), 1);
2051 /* set UDMA timing */
2052 pci_write_config(gparent,
2053 0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
2054 udmatiming[mode & ATA_MODE_MASK], 1);
2057 u_int8_t chtiming[] =
2058 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
2060 /* disable UDMA mode */
2061 pci_write_config(gparent, 0x50,
2062 pci_read_config(gparent, 0x50, 1) |
2063 (1 << (devno + 3)), 1);
2065 /* set active and recover timing (shared between master & slave) */
2066 if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
2067 chtiming[ata_mode2idx(mode)])
2068 pci_write_config(gparent, 0x54 + (ch->unit << 2),
2069 chtiming[ata_mode2idx(mode)], 1);
2071 atadev->mode = mode;
2077 * National chipset support functions
2080 ata_national_ident(device_t dev)
2082 struct ata_pci_controller *ctlr = device_get_softc(dev);
2084 /* this chip is a clone of the Cyrix chip, bugs and all */
2085 if (pci_get_devid(dev) == ATA_SC1100) {
2086 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
2087 ctlr->chipinit = ata_national_chipinit;
2093 static device_t nat_host = NULL;
2096 ata_national_chipinit(device_t dev)
2098 struct ata_pci_controller *ctlr = device_get_softc(dev);
2102 if (ata_setup_interrupt(dev))
2105 /* locate the ISA part in the southbridge and enable UDMA33 */
2106 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
2107 for (i = 0; i < nchildren; i++) {
2108 if (pci_get_devid(children[i]) == 0x0510100b) {
2109 nat_host = children[i];
2113 free(children, M_TEMP);
2115 ctlr->setmode = ata_national_setmode;
2120 ata_national_setmode(device_t dev, int mode)
2122 device_t gparent = GRANDPARENT(dev);
2123 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2124 struct ata_device *atadev = device_get_softc(dev);
2125 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2126 u_int32_t piotiming[] =
2127 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
2128 0x00803020, 0x20102010, 0x00100010,
2129 0x00100010, 0x00100010, 0x00100010 };
2130 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
2131 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
2134 ch->dma->alignment = 16;
2135 ch->dma->max_iosize = 126 * DEV_BSIZE;
2137 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2139 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2142 device_printf(dev, "%s setting %s on National chip\n",
2143 (error) ? "failed" : "success", ata_mode2str(mode));
2145 if (mode >= ATA_UDMA0) {
2146 pci_write_config(gparent, 0x44 + (devno << 3),
2147 udmatiming[mode & ATA_MODE_MASK], 4);
2149 else if (mode >= ATA_WDMA0) {
2150 pci_write_config(gparent, 0x44 + (devno << 3),
2151 dmatiming[mode & ATA_MODE_MASK], 4);
2154 pci_write_config(gparent, 0x44 + (devno << 3),
2155 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
2158 pci_write_config(gparent, 0x40 + (devno << 3),
2159 piotiming[ata_mode2idx(mode)], 4);
2160 atadev->mode = mode;
2166 * nVidia chipset support functions
2169 ata_nvidia_ident(device_t dev)
2171 struct ata_pci_controller *ctlr = device_get_softc(dev);
2172 struct ata_chip_id *idx;
2173 static struct ata_chip_id ids[] =
2174 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nVidia nForce" },
2175 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2" },
2176 { ATA_NFORCE2_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2 MCP" },
2177 { ATA_NFORCE2_MCP_S1, 0, 0, 0, ATA_SA150, "nVidia nForce2 MCP" },
2178 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3" },
2179 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 Pro" },
2180 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
2181 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
2182 { ATA_NFORCE3_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 MCP" },
2183 { ATA_NFORCE3_MCP_S1, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
2184 { ATA_NFORCE3_MCP_S2, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
2185 { ATA_NFORCE4, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce4" },
2186 { ATA_NFORCE4_S1, 0, 0, NV4OFF, ATA_SA300, "nVidia nForce4" },
2187 { ATA_NFORCE4_S2, 0, 0, NV4OFF, ATA_SA300, "nVidia nForce4" },
2188 { 0, 0, 0, 0, 0, 0}};
2191 if (!(idx = ata_match_chip(dev, ids)))
2194 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
2195 device_set_desc_copy(dev, buffer);
2197 ctlr->chipinit = ata_nvidia_chipinit;
2202 ata_nvidia_chipinit(device_t dev)
2204 struct ata_pci_controller *ctlr = device_get_softc(dev);
2206 if (ata_setup_interrupt(dev))
2209 if (ctlr->chip->max_dma >= ATA_SA150) {
2210 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
2211 ctlr->r_type2 = SYS_RES_IOPORT;
2213 ctlr->r_type2 = SYS_RES_MEMORY;
2214 ctlr->r_rid2 = PCIR_BAR(5);
2215 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2216 &ctlr->r_rid2, RF_ACTIVE))) {
2217 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
2219 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
2220 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2221 ata_nvidia_intr, ctlr, &ctlr->handle)) {
2222 device_printf(dev, "unable to setup interrupt\n");
2226 /* enable control access */
2227 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
2229 /* clear interrupt status */
2230 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2232 /* enable device and PHY state change interrupts */
2233 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
2235 /* enable PCI interrupt */
2236 pci_write_config(dev, PCIR_COMMAND,
2237 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
2239 ctlr->allocate = ata_nvidia_allocate;
2240 ctlr->reset = ata_nvidia_reset;
2242 ctlr->setmode = ata_sata_setmode;
2245 /* disable prefetch, postwrite */
2246 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
2247 ctlr->setmode = ata_via_family_setmode;
2253 ata_nvidia_allocate(device_t dev)
2255 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2256 struct ata_channel *ch = device_get_softc(dev);
2258 /* setup the usual register normal pci style */
2259 if (ata_pci_allocate(dev))
2262 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2263 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
2264 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2265 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
2266 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2267 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
2268 ch->flags |= ATA_NO_SLAVE;
2274 ata_nvidia_intr(void *data)
2276 struct ata_pci_controller *ctlr = data;
2277 struct ata_channel *ch;
2278 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
2282 /* get interrupt status */
2283 status = ATA_INB(ctlr->r_res2, offset);
2285 /* clear interrupt status */
2286 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2288 for (unit = 0; unit < ctlr->channels; unit++) {
2289 if ((ch = ctlr->interrupt[unit].argument)) {
2290 struct ata_connect_task *tp;
2291 int maskshift = ch->unit << 2;
2293 /* clear error status */
2294 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2296 /* check for and handle connect events */
2297 if (((status & (0x0c << maskshift)) == (0x04 << maskshift)) &&
2298 (tp = (struct ata_connect_task *)
2299 malloc(sizeof(struct ata_connect_task),
2300 M_ATA, M_NOWAIT | M_ZERO))) {
2302 device_printf(ch->dev, "CONNECT requested\n");
2303 tp->action = ATA_C_ATTACH;
2305 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2306 taskqueue_enqueue(taskqueue_thread, &tp->task);
2309 /* check for and handle disconnect events */
2310 if ((status & (0x08 << maskshift)) &&
2311 (tp = (struct ata_connect_task *)
2312 malloc(sizeof(struct ata_connect_task),
2313 M_ATA, M_NOWAIT | M_ZERO))) {
2315 device_printf(ch->dev, "DISCONNECT requested\n");
2316 tp->action = ATA_C_DETACH;
2318 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2319 taskqueue_enqueue(taskqueue_thread, &tp->task);
2322 /* any drive action to take care of ? */
2323 if (status & (0x01 << maskshift)) {
2324 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2326 ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2328 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
2330 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat&~ATA_BMSTAT_ERROR);
2333 ctlr->interrupt[unit].function(ch);
2340 ata_nvidia_reset(device_t dev)
2342 struct ata_channel *ch = device_get_softc(dev);
2344 ata_sata_phy_enable(ch);
2349 * Promise chipset support functions
2351 #define ATA_PDC_APKT_OFFSET 0x00000010
2352 #define ATA_PDC_HPKT_OFFSET 0x00000040
2353 #define ATA_PDC_ASG_OFFSET 0x00000080
2354 #define ATA_PDC_LSG_OFFSET 0x000000c0
2355 #define ATA_PDC_HSG_OFFSET 0x00000100
2356 #define ATA_PDC_CHN_OFFSET 0x00000400
2357 #define ATA_PDC_BUF_BASE 0x00400000
2358 #define ATA_PDC_BUF_OFFSET 0x00100000
2359 #define ATA_PDC_MAX_HPKT 8
2360 #define ATA_PDC_WRITE_REG 0x00
2361 #define ATA_PDC_WRITE_CTL 0x0e
2362 #define ATA_PDC_WRITE_END 0x08
2363 #define ATA_PDC_WAIT_NBUSY 0x10
2364 #define ATA_PDC_WAIT_READY 0x18
2365 #define ATA_PDC_1B 0x20
2366 #define ATA_PDC_2B 0x40
2368 struct host_packet {
2370 TAILQ_ENTRY(host_packet) chain;
2373 struct ata_promise_sx4 {
2375 TAILQ_HEAD(, host_packet) queue;
2380 ata_promise_ident(device_t dev)
2382 struct ata_pci_controller *ctlr = device_get_softc(dev);
2383 struct ata_chip_id *idx;
2384 static struct ata_chip_id ids[] =
2385 {{ ATA_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "Promise PDC20246" },
2386 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20262" },
2387 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20263" },
2388 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20265" },
2389 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20267" },
2390 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20268" },
2391 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20269" },
2392 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20270" },
2393 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20271" },
2394 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20275" },
2395 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "Promise PDC20276" },
2396 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20277" },
2397 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20318" },
2398 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20319" },
2399 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20371" },
2400 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20375" },
2401 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20376" },
2402 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20377" },
2403 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20378" },
2404 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20379" },
2405 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20571" },
2406 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20575" },
2407 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20579" },
2408 { ATA_PDC20580, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20580" },
2409 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20617" },
2410 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20618" },
2411 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20619" },
2412 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20620" },
2413 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "Promise PDC20621" },
2414 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "Promise PDC20622" },
2415 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40518" },
2416 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40519" },
2417 { ATA_PDC40718, 0, PRMIO, PRSATA2, ATA_SA300, "Promise PDC40718" },
2418 { ATA_PDC40719, 0, PRMIO, PRSATA2, ATA_SA300, "Promise PDC40719" },
2419 { 0, 0, 0, 0, 0, 0}};
2421 uintptr_t devid = 0;
2423 if (!(idx = ata_match_chip(dev, ids)))
2426 /* if we are on a SuperTrak SX6000 dont attach */
2427 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
2428 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
2429 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
2430 devid == ATA_I960RM)
2433 strcpy(buffer, idx->text);
2435 /* if we are on a FastTrak TX4, adjust the interrupt resource */
2436 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
2437 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
2438 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
2439 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
2440 static long start = 0, end = 0;
2442 if (pci_get_slot(dev) == 1) {
2443 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
2444 strcat(buffer, " (channel 0+1)");
2446 else if (pci_get_slot(dev) == 2 && start && end) {
2447 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
2449 strcat(buffer, " (channel 2+3)");
2455 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
2456 device_set_desc_copy(dev, buffer);
2458 ctlr->chipinit = ata_promise_chipinit;
2463 ata_promise_chipinit(device_t dev)
2465 struct ata_pci_controller *ctlr = device_get_softc(dev);
2466 int rid = ATA_IRQ_RID;
2468 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2469 RF_SHAREABLE | RF_ACTIVE))) {
2470 device_printf(dev, "unable to map interrupt\n");
2474 if (ctlr->chip->max_dma >= ATA_SA150)
2475 ctlr->setmode = ata_sata_setmode;
2477 ctlr->setmode = ata_promise_setmode;
2479 switch (ctlr->chip->cfg1) {
2482 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
2484 ctlr->dmainit = ata_promise_new_dmainit;
2488 /* enable burst mode */
2489 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
2491 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2492 ata_promise_old_intr, ctlr, &ctlr->handle))) {
2493 device_printf(dev, "unable to setup interrupt\n");
2499 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2500 ata_promise_tx2_intr, ctlr, &ctlr->handle))) {
2501 device_printf(dev, "unable to setup interrupt\n");
2507 // if (ctlr->r_res1)
2508 // bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
2509 ctlr->r_type1 = SYS_RES_MEMORY;
2510 ctlr->r_rid1 = PCIR_BAR(4);
2511 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
2512 &ctlr->r_rid1, RF_ACTIVE)))
2515 ctlr->r_type2 = SYS_RES_MEMORY;
2516 ctlr->r_rid2 = PCIR_BAR(3);
2517 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2518 &ctlr->r_rid2, RF_ACTIVE))){
2519 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
2522 ctlr->reset = ata_promise_mio_reset;
2523 ctlr->dmainit = ata_promise_mio_dmainit;
2524 ctlr->allocate = ata_promise_mio_allocate;
2526 if (ctlr->chip->cfg2 == PRSX4X) {
2527 struct ata_promise_sx4 *hpkt;
2528 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
2530 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2531 ata_promise_sx4_intr, ctlr, &ctlr->handle))) {
2532 device_printf(dev, "unable to setup interrupt\n");
2533 /* XXX SOS release resources */
2537 /* print info about cache memory */
2538 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
2539 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
2540 ((dimm >> 24) & 0xff),
2541 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
2542 " ECC enabled" : "" );
2544 /* adjust cache memory parameters */
2545 ATA_OUTL(ctlr->r_res2, 0x000c000c,
2546 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
2548 /* setup host packet controls */
2549 hpkt = malloc(sizeof(struct ata_promise_sx4),
2550 M_TEMP, M_NOWAIT | M_ZERO);
2551 mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
2552 TAILQ_INIT(&hpkt->queue);
2553 hpkt->busy = 0; //hpkt->head = hpkt->tail = 0;
2554 device_set_ivars(dev, hpkt);
2559 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2560 ata_promise_mio_intr, ctlr, &ctlr->handle))) {
2561 device_printf(dev, "unable to setup interrupt\n");
2562 /* XXX SOS release resources */
2566 switch (ctlr->chip->cfg2) {
2568 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
2569 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
2573 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
2574 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 3;
2578 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
2583 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
2588 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
2593 /* XXX SOS release resources */
2597 /* XXX SOS release resources */
2604 ata_promise_mio_allocate(device_t dev)
2606 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2607 struct ata_channel *ch = device_get_softc(dev);
2608 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
2611 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2612 ch->r_io[i].res = ctlr->r_res2;
2613 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
2615 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
2616 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
2617 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
2618 ata_default_registers(dev);
2619 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
2620 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
2621 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2622 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
2623 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2624 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
2625 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2626 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
2627 ch->flags |= ATA_NO_SLAVE;
2629 ch->flags |= ATA_USE_16BIT;
2631 ata_generic_hw(dev);
2633 ch->hw.command = ata_promise_sx4_command;
2635 ch->hw.command = ata_promise_mio_command;
2640 ata_promise_mio_intr(void *data)
2642 struct ata_pci_controller *ctlr = data;
2643 struct ata_channel *ch;
2644 u_int32_t vector = 0, status = 0;
2647 switch (ctlr->chip->cfg2) {
2650 /* read and acknowledge interrupt(s) */
2651 vector = ATA_INL(ctlr->r_res2, 0x040);
2653 /* read and clear interface status */
2654 status = ATA_INL(ctlr->r_res2, 0x06c);
2655 ATA_OUTL(ctlr->r_res2, 0x06c, status & 0x000000ff);
2660 /* read and acknowledge interrupt(s) */
2661 vector = ATA_INL(ctlr->r_res2, 0x040);
2662 ATA_OUTL(ctlr->r_res2, 0x040, vector & 0x0000ffff);
2664 /* read and clear interface status */
2665 status = ATA_INL(ctlr->r_res2, 0x060);
2666 ATA_OUTL(ctlr->r_res2, 0x060, status & 0x000000ff);
2670 for (unit = 0; unit < ctlr->channels; unit++) {
2672 if ((ch = ctlr->interrupt[unit].argument)) {
2673 struct ata_connect_task *tp;
2675 /* check for and handle disconnect events */
2676 if ((status & (0x00000001 << unit)) &&
2677 (tp = (struct ata_connect_task *)
2678 malloc(sizeof(struct ata_connect_task),
2679 M_ATA, M_NOWAIT | M_ZERO))) {
2682 device_printf(ch->dev, "DISCONNECT requested\n");
2683 tp->action = ATA_C_DETACH;
2685 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2686 taskqueue_enqueue(taskqueue_thread, &tp->task);
2689 /* check for and handle connect events */
2690 if ((status & (0x00000010 << unit)) &&
2691 (tp = (struct ata_connect_task *)
2692 malloc(sizeof(struct ata_connect_task),
2693 M_ATA, M_NOWAIT | M_ZERO))) {
2696 device_printf(ch->dev, "CONNECT requested\n");
2697 tp->action = ATA_C_ATTACH;
2699 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2700 taskqueue_enqueue(taskqueue_thread, &tp->task);
2703 /* active interrupt(s) need to call the interrupt handler */
2704 if (vector & (1 << (unit + 1)))
2705 if ((ch = ctlr->interrupt[unit].argument))
2706 ctlr->interrupt[unit].function(ch);
2712 ata_promise_sx4_intr(void *data)
2714 struct ata_pci_controller *ctlr = data;
2715 struct ata_channel *ch;
2716 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
2719 for (unit = 0; unit < ctlr->channels; unit++) {
2720 if (vector & (1 << (unit + 1)))
2721 if ((ch = ctlr->interrupt[unit].argument))
2722 ctlr->interrupt[unit].function(ch);
2723 if (vector & (1 << (unit + 5)))
2724 if ((ch = ctlr->interrupt[unit].argument))
2725 ata_promise_queue_hpkt(ctlr,
2726 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
2727 ATA_PDC_HPKT_OFFSET));
2728 if (vector & (1 << (unit + 9))) {
2729 ata_promise_next_hpkt(ctlr);
2730 if ((ch = ctlr->interrupt[unit].argument))
2731 ctlr->interrupt[unit].function(ch);
2733 if (vector & (1 << (unit + 13))) {
2734 ata_promise_next_hpkt(ctlr);
2735 if ((ch = ctlr->interrupt[unit].argument))
2736 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2737 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
2738 ATA_PDC_APKT_OFFSET));
2744 ata_promise_mio_dmainit(device_t dev)
2746 struct ata_channel *ch = device_get_softc(dev);
2750 /* note start and stop are not used here */
2755 ata_promise_mio_reset(device_t dev)
2757 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2758 struct ata_channel *ch = device_get_softc(dev);
2759 struct ata_promise_sx4 *hpktp;
2761 switch (ctlr->chip->cfg2) {
2764 /* softreset channel ATA module */
2765 hpktp = device_get_ivars(ctlr->dev);
2766 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
2768 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
2769 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
2770 ~0x00003f9f) | (ch->unit + 1));
2772 /* softreset HOST module */ /* XXX SOS what about other outstandings */
2773 mtx_lock(&hpktp->mtx);
2774 ATA_OUTL(ctlr->r_res2, 0xc012c,
2775 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
2777 ATA_OUTL(ctlr->r_res2, 0xc012c,
2778 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
2780 mtx_unlock(&hpktp->mtx);
2781 ata_generic_reset(dev);
2786 if ((ctlr->chip->cfg2 == PRSATA) ||
2787 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2789 /* mask plug/unplug intr */
2790 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
2793 /* softreset channels ATA module */
2794 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2796 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2797 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2798 ~0x00003f9f) | (ch->unit + 1));
2800 if ((ctlr->chip->cfg2 == PRSATA) ||
2801 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2803 ata_sata_phy_enable(ch);
2805 /* reset and enable plug/unplug intr */
2806 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
2809 ata_generic_reset(dev);
2814 if ((ctlr->chip->cfg2 == PRSATA2) ||
2815 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2816 /* set portmultiplier port */
2817 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
2819 /* mask plug/unplug intr */
2820 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
2823 /* softreset channels ATA module */
2824 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2826 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2827 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2828 ~0x00003f9f) | (ch->unit + 1));
2830 if ((ctlr->chip->cfg2 == PRSATA2) ||
2831 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2833 /* set PHY mode to "improved" */
2834 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
2835 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
2836 ~0x00000003) | 0x00000001);
2838 ata_sata_phy_enable(ch);
2840 /* reset and enable plug/unplug intr */
2841 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
2843 /* set portmultiplier port */
2844 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
2847 ata_generic_reset(dev);
2854 ata_promise_mio_command(struct ata_request *request)
2856 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2857 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2858 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
2860 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
2862 /* XXX SOS add ATAPI commands support later */
2863 switch (request->u.ata.command) {
2865 return ata_generic_command(request);
2868 case ATA_READ_DMA48:
2869 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
2873 case ATA_WRITE_DMA48:
2874 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
2877 wordp[1] = htole32(ch->dma->sg_bus);
2879 ata_promise_apkt((u_int8_t*)wordp, request);
2881 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
2886 ata_promise_sx4_command(struct ata_request *request)
2888 device_t gparent = GRANDPARENT(request->dev);
2889 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2890 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2891 struct ata_dma_prdentry *prd = ch->dma->sg;
2892 caddr_t window = rman_get_virtual(ctlr->r_res1);
2894 int i, idx, length = 0;
2896 /* XXX SOS add ATAPI commands support later */
2897 switch (request->u.ata.command) {
2902 case ATA_ATA_IDENTIFY:
2906 case ATA_READ_MUL48:
2910 case ATA_WRITE_MUL48:
2911 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2912 return ata_generic_command(request);
2914 case ATA_SETFEATURES:
2915 case ATA_FLUSHCACHE:
2916 case ATA_FLUSHCACHE48:
2919 wordp = (u_int32_t *)
2920 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2921 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
2924 ata_promise_apkt((u_int8_t *)wordp, request);
2925 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2926 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2927 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2928 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
2932 case ATA_READ_DMA48:
2934 case ATA_WRITE_DMA48:
2935 wordp = (u_int32_t *)
2936 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
2939 wordp[idx++] = prd[i].addr;
2940 wordp[idx++] = prd[i].count;
2941 length += (prd[i].count & ~ATA_DMA_EOT);
2942 } while (!(prd[i++].count & ATA_DMA_EOT));
2944 wordp = (u_int32_t *)
2945 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
2946 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2947 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
2949 wordp = (u_int32_t *)
2950 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
2951 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2952 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
2954 wordp = (u_int32_t *)
2955 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
2956 if (request->flags & ATA_R_READ)
2957 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
2958 if (request->flags & ATA_R_WRITE)
2959 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
2960 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
2961 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
2964 wordp = (u_int32_t *)
2965 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2966 if (request->flags & ATA_R_READ)
2967 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
2968 if (request->flags & ATA_R_WRITE)
2969 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
2970 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
2972 ata_promise_apkt((u_int8_t *)wordp, request);
2973 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2975 if (request->flags & ATA_R_READ) {
2976 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
2977 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
2978 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2979 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
2981 if (request->flags & ATA_R_WRITE) {
2982 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
2983 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
2984 ata_promise_queue_hpkt(ctlr,
2985 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
2992 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
2994 struct ata_device *atadev = device_get_softc(request->dev);
2997 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
2998 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
2999 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
3000 bytep[i++] = ATA_A_4BIT;
3002 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3003 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
3004 bytep[i++] = request->u.ata.feature >> 8;
3005 bytep[i++] = request->u.ata.feature;
3006 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
3007 bytep[i++] = request->u.ata.count >> 8;
3008 bytep[i++] = request->u.ata.count;
3009 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
3010 bytep[i++] = request->u.ata.lba >> 24;
3011 bytep[i++] = request->u.ata.lba;
3012 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
3013 bytep[i++] = request->u.ata.lba >> 32;
3014 bytep[i++] = request->u.ata.lba >> 8;
3015 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
3016 bytep[i++] = request->u.ata.lba >> 40;
3017 bytep[i++] = request->u.ata.lba >> 16;
3018 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
3019 bytep[i++] = ATA_D_LBA | atadev->unit;
3022 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
3023 bytep[i++] = request->u.ata.feature;
3024 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
3025 bytep[i++] = request->u.ata.count;
3026 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
3027 bytep[i++] = request->u.ata.lba;
3028 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
3029 bytep[i++] = request->u.ata.lba >> 8;
3030 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
3031 bytep[i++] = request->u.ata.lba >> 16;
3032 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
3033 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
3034 ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
3036 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
3037 bytep[i++] = request->u.ata.command;
3042 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
3044 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
3046 mtx_lock(&hpktp->mtx);
3048 struct host_packet *hp =
3049 malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
3051 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
3055 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
3057 mtx_unlock(&hpktp->mtx);
3061 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
3063 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
3064 struct host_packet *hp;
3066 mtx_lock(&hpktp->mtx);
3067 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
3068 TAILQ_REMOVE(&hpktp->queue, hp, chain);
3069 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
3074 mtx_unlock(&hpktp->mtx);
3078 ata_promise_tx2_intr(void *data)
3080 struct ata_pci_controller *ctlr = data;
3081 struct ata_channel *ch;
3084 for (unit = 0; unit < ctlr->channels; unit++) {
3085 if (!(ch = ctlr->interrupt[unit].argument))
3087 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3088 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
3089 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3090 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3092 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3093 ATA_BMSTAT_INTERRUPT)
3095 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3098 ctlr->interrupt[unit].function(ch);
3104 ata_promise_old_intr(void *data)
3106 struct ata_pci_controller *ctlr = data;
3107 struct ata_channel *ch;
3110 for (unit = 0; unit < ctlr->channels; unit++) {
3111 if (!(ch = ctlr->interrupt[unit].argument))
3113 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)){
3114 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3115 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3117 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3118 ATA_BMSTAT_INTERRUPT)
3120 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3123 ctlr->interrupt[unit].function(ch);
3129 ata_promise_new_dmastart(device_t dev)
3131 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3132 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3133 struct ata_device *atadev = device_get_softc(dev);
3135 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3136 ATA_OUTB(ctlr->r_res1, 0x11,
3137 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
3138 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20,
3139 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
3140 (ch->dma->cur_iosize >> 1));
3142 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
3143 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
3144 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
3145 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3146 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
3147 ATA_BMCMD_START_STOP);
3148 ch->flags |= ATA_DMA_ACTIVE;
3153 ata_promise_new_dmastop(device_t dev)
3155 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3156 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3157 struct ata_device *atadev = device_get_softc(dev);
3160 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3161 ATA_OUTB(ctlr->r_res1, 0x11,
3162 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
3163 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20, 0);
3165 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
3166 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3167 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3168 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3169 ch->flags &= ~ATA_DMA_ACTIVE;
3174 ata_promise_new_dmareset(device_t dev)
3176 struct ata_channel *ch = device_get_softc(dev);
3178 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3179 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3180 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3181 ch->flags &= ~ATA_DMA_ACTIVE;
3185 ata_promise_new_dmainit(device_t dev)
3187 struct ata_channel *ch = device_get_softc(dev);
3191 ch->dma->start = ata_promise_new_dmastart;
3192 ch->dma->stop = ata_promise_new_dmastop;
3193 ch->dma->reset = ata_promise_new_dmareset;
3198 ata_promise_setmode(device_t dev, int mode)
3200 device_t gparent = GRANDPARENT(dev);
3201 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3202 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3203 struct ata_device *atadev = device_get_softc(dev);
3204 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3206 u_int32_t timings33[][2] = {
3207 /* PROLD PRNEW mode */
3208 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
3209 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
3210 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
3211 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
3212 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
3213 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
3214 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
3215 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
3216 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
3217 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
3218 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
3219 { 0, 0x00424ef6 }, /* UDMA 3 */
3220 { 0, 0x004127f3 }, /* UDMA 4 */
3221 { 0, 0x004127f3 } /* UDMA 5 */
3224 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3226 switch (ctlr->chip->cfg1) {
3229 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
3230 (ch->unit ? 1 << 11 : 1 << 10))) {
3231 ata_print_cable(dev, "controller");
3234 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
3235 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
3239 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3240 if (mode > ATA_UDMA2 &&
3241 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
3242 ata_print_cable(dev, "controller");
3248 if (mode > ATA_UDMA2 &&
3249 (ATA_INL(ctlr->r_res2,
3250 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
3251 (ch->unit << 7)) & 0x01000000)) {
3252 ata_print_cable(dev, "controller");
3258 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3261 device_printf(dev, "%ssetting %s on %s chip\n",
3262 (error) ? "FAILURE " : "",
3263 ata_mode2str(mode), ctlr->chip->text);
3265 if (ctlr->chip->cfg1 < PRTX)
3266 pci_write_config(gparent, 0x60 + (devno << 2),
3267 timings33[ctlr->chip->cfg1][ata_mode2idx(mode)],4);
3268 atadev->mode = mode;
3275 * ServerWorks chipset support functions
3278 ata_serverworks_ident(device_t dev)
3280 struct ata_pci_controller *ctlr = device_get_softc(dev);
3281 struct ata_chip_id *idx;
3282 static struct ata_chip_id ids[] =
3283 {{ ATA_ROSB4, 0x00, SWKS33, 0x00, ATA_UDMA2, "ServerWorks ROSB4" },
3284 { ATA_CSB5, 0x92, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB5" },
3285 { ATA_CSB5, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB5" },
3286 { ATA_CSB6, 0x00, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB6" },
3287 { ATA_CSB6_1, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB6" },
3288 { 0, 0, 0, 0, 0, 0}};
3291 if (!(idx = ata_match_chip(dev, ids)))
3294 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3295 device_set_desc_copy(dev, buffer);
3297 ctlr->chipinit = ata_serverworks_chipinit;
3302 ata_serverworks_chipinit(device_t dev)
3304 struct ata_pci_controller *ctlr = device_get_softc(dev);
3306 if (ata_setup_interrupt(dev))
3309 if (ctlr->chip->cfg1 == SWKS33) {
3313 /* locate the ISA part in the southbridge and enable UDMA33 */
3314 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
3315 for (i = 0; i < nchildren; i++) {
3316 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
3317 pci_write_config(children[i], 0x64,
3318 (pci_read_config(children[i], 0x64, 4) &
3319 ~0x00002000) | 0x00004000, 4);
3323 free(children, M_TEMP);
3327 pci_write_config(dev, 0x5a,
3328 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
3329 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
3331 ctlr->setmode = ata_serverworks_setmode;
3336 ata_serverworks_setmode(device_t dev, int mode)
3338 device_t gparent = GRANDPARENT(dev);
3339 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3340 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3341 struct ata_device *atadev = device_get_softc(dev);
3342 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3343 int offset = (devno ^ 0x01) << 3;
3345 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
3346 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
3347 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
3349 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3351 mode = ata_check_80pin(dev, mode);
3353 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3356 device_printf(dev, "%ssetting %s on %s chip\n",
3357 (error) ? "FAILURE " : "",
3358 ata_mode2str(mode), ctlr->chip->text);
3360 if (mode >= ATA_UDMA0) {
3361 pci_write_config(gparent, 0x56,
3362 (pci_read_config(gparent, 0x56, 2) &
3363 ~(0xf << (devno << 2))) |
3364 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
3365 pci_write_config(gparent, 0x54,
3366 pci_read_config(gparent, 0x54, 1) |
3367 (0x01 << devno), 1);
3368 pci_write_config(gparent, 0x44,
3369 (pci_read_config(gparent, 0x44, 4) &
3370 ~(0xff << offset)) |
3371 (dmatimings[2] << offset), 4);
3373 else if (mode >= ATA_WDMA0) {
3374 pci_write_config(gparent, 0x54,
3375 pci_read_config(gparent, 0x54, 1) &
3376 ~(0x01 << devno), 1);
3377 pci_write_config(gparent, 0x44,
3378 (pci_read_config(gparent, 0x44, 4) &
3379 ~(0xff << offset)) |
3380 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
3383 pci_write_config(gparent, 0x54,
3384 pci_read_config(gparent, 0x54, 1) &
3385 ~(0x01 << devno), 1);
3387 pci_write_config(gparent, 0x40,
3388 (pci_read_config(gparent, 0x40, 4) &
3389 ~(0xff << offset)) |
3390 (piotimings[ata_mode2idx(mode)] << offset), 4);
3391 atadev->mode = mode;
3397 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
3400 ata_sii_ident(device_t dev)
3402 struct ata_pci_controller *ctlr = device_get_softc(dev);
3403 struct ata_chip_id *idx;
3404 static struct ata_chip_id ids[] =
3405 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
3406 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
3407 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
3408 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
3409 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
3410 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
3411 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
3412 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
3413 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
3414 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
3415 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
3416 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
3417 { 0, 0, 0, 0, 0, 0}};
3420 if (!(idx = ata_match_chip(dev, ids)))
3423 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3424 device_set_desc_copy(dev, buffer);
3426 ctlr->chipinit = ata_sii_chipinit;
3431 ata_sii_chipinit(device_t dev)
3433 struct ata_pci_controller *ctlr = device_get_softc(dev);
3434 int rid = ATA_IRQ_RID;
3436 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3437 RF_SHAREABLE | RF_ACTIVE))) {
3438 device_printf(dev, "unable to map interrupt\n");
3442 if (ctlr->chip->cfg1 == SIIMEMIO) {
3443 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3444 ata_sii_intr, ctlr, &ctlr->handle))) {
3445 device_printf(dev, "unable to setup interrupt\n");
3449 ctlr->r_type2 = SYS_RES_MEMORY;
3450 ctlr->r_rid2 = PCIR_BAR(5);
3451 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3452 &ctlr->r_rid2, RF_ACTIVE)))
3455 if (ctlr->chip->cfg2 & SIISETCLK) {
3456 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
3457 pci_write_config(dev, 0x8a,
3458 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
3459 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
3460 device_printf(dev, "%s could not set ATA133 clock\n",
3464 /* if we have 4 channels enable the second set */
3465 if (ctlr->chip->cfg2 & SII4CH) {
3466 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
3470 /* enable PCI interrupt as BIOS might not */
3471 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
3473 /* dont block interrupts from any channel */
3474 pci_write_config(dev, 0x48,
3475 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
3477 ctlr->allocate = ata_sii_allocate;
3478 if (ctlr->chip->max_dma >= ATA_SA150) {
3479 ctlr->reset = ata_sii_reset;
3480 ctlr->setmode = ata_sata_setmode;
3483 ctlr->setmode = ata_sii_setmode;
3486 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3487 ctlr->chip->cfg2 & SIIINTR ?
3488 ata_cmd_intr : ata_cmd_old_intr,
3489 ctlr, &ctlr->handle))) {
3490 device_printf(dev, "unable to setup interrupt\n");
3494 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
3495 device_printf(dev, "HW has secondary channel disabled\n");
3499 /* enable interrupt as BIOS might not */
3500 pci_write_config(dev, 0x71, 0x01, 1);
3502 ctlr->setmode = ata_cmd_setmode;
3508 ata_sii_allocate(device_t dev)
3510 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3511 struct ata_channel *ch = device_get_softc(dev);
3512 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
3515 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
3516 ch->r_io[i].res = ctlr->r_res2;
3517 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
3519 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
3520 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
3521 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
3522 ata_default_registers(dev);
3523 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
3524 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
3525 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
3526 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
3527 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
3528 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
3529 ch->r_io[ATA_BMDEVSPEC_0].res = ctlr->r_res2;
3530 ch->r_io[ATA_BMDEVSPEC_0].offset = 0xa1 + (unit01 << 6) + (unit10 << 8);
3532 if (ctlr->chip->max_dma >= ATA_SA150) {
3533 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3534 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
3535 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3536 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
3537 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3538 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
3539 ch->flags |= ATA_NO_SLAVE;
3541 /* enable PHY state change interrupt */
3542 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
3545 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
3546 /* work around errata in early chips */
3547 ch->dma->boundary = 16 * DEV_BSIZE;
3548 ch->dma->segsize = 15 * DEV_BSIZE;
3551 ata_generic_hw(dev);
3556 ata_sii_intr(void *data)
3558 struct ata_pci_controller *ctlr = data;
3559 struct ata_channel *ch;
3562 for (unit = 0; unit < ctlr->channels; unit++) {
3563 if (!(ch = ctlr->interrupt[unit].argument))
3566 /* check for PHY related interrupts on SATA capable HW */
3567 if (ctlr->chip->max_dma >= ATA_SA150) {
3568 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
3569 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
3570 struct ata_connect_task *tp;
3573 /* clear error bits/interrupt */
3574 ATA_IDX_OUTL(ch, ATA_SERROR, error);
3576 /* if we have a connection event deal with it */
3577 if ((error & ATA_SE_PHY_CHANGED) &&
3578 (tp = (struct ata_connect_task *)
3579 malloc(sizeof(struct ata_connect_task),
3580 M_ATA, M_NOWAIT | M_ZERO))) {
3582 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
3583 device_printf(ch->dev, "CONNECT requested\n");
3584 tp->action = ATA_C_ATTACH;
3587 device_printf(ch->dev, "DISCONNECT requested\n");
3588 tp->action = ATA_C_DETACH;
3591 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3592 taskqueue_enqueue(taskqueue_thread, &tp->task);
3597 /* any drive action to take care of ? */
3598 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_0) & 0x08) {
3599 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3600 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3602 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
3604 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3607 ctlr->interrupt[unit].function(ch);
3614 ata_cmd_intr(void *data)
3616 struct ata_pci_controller *ctlr = data;
3617 struct ata_channel *ch;
3621 for (unit = 0; unit < ctlr->channels; unit++) {
3622 if (!(ch = ctlr->interrupt[unit].argument))
3624 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
3625 (ch->unit ? 0x08 : 0x04))) {
3626 pci_write_config(device_get_parent(ch->dev), 0x71,
3627 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
3628 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3629 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3631 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3632 ATA_BMSTAT_INTERRUPT)
3634 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3637 ctlr->interrupt[unit].function(ch);
3643 ata_cmd_old_intr(void *data)
3645 struct ata_pci_controller *ctlr = data;
3646 struct ata_channel *ch;
3649 for (unit = 0; unit < ctlr->channels; unit++) {
3650 if (!(ch = ctlr->interrupt[unit].argument))
3652 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3653 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3655 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3656 ATA_BMSTAT_INTERRUPT)
3658 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3661 ctlr->interrupt[unit].function(ch);
3666 ata_sii_reset(device_t dev)
3668 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3669 struct ata_channel *ch = device_get_softc(dev);
3670 int offset = ((ch->unit & 1) << 7) + ((ch->unit & 2) << 8);
3672 /* disable PHY state change interrupt */
3673 ATA_OUTL(ctlr->r_res2, 0x148 + offset, ~(1 << 16));
3675 /* reset controller part for this channel */
3676 ATA_OUTL(ctlr->r_res2, 0x48,
3677 ATA_INL(ctlr->r_res2, 0x48) | (0xc0 >> ch->unit));
3679 ATA_OUTL(ctlr->r_res2, 0x48,
3680 ATA_INL(ctlr->r_res2, 0x48) & ~(0xc0 >> ch->unit));
3682 ata_sata_phy_enable(ch);
3684 /* enable PHY state change interrupt */
3685 ATA_OUTL(ctlr->r_res2, 0x148 + offset, (1 << 16));
3689 ata_sii_setmode(device_t dev, int mode)
3691 device_t gparent = GRANDPARENT(dev);
3692 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3693 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3694 struct ata_device *atadev = device_get_softc(dev);
3695 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
3696 int mreg = ch->unit ? 0x84 : 0x80;
3697 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
3698 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
3701 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3703 if (ctlr->chip->cfg2 & SIISETCLK) {
3704 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
3705 (ch->unit ? 0x02 : 0x01))) {
3706 ata_print_cable(dev, "controller");
3711 mode = ata_check_80pin(dev, mode);
3713 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3716 device_printf(dev, "%ssetting %s on %s chip\n",
3717 (error) ? "FAILURE " : "",
3718 ata_mode2str(mode), ctlr->chip->text);
3722 if (mode >= ATA_UDMA0) {
3723 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
3724 u_int8_t ureg = 0xac + rego;
3726 pci_write_config(gparent, mreg,
3727 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
3728 pci_write_config(gparent, ureg,
3729 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
3730 udmatimings[mode & ATA_MODE_MASK], 1);
3733 else if (mode >= ATA_WDMA0) {
3734 u_int8_t dreg = 0xa8 + rego;
3735 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
3737 pci_write_config(gparent, mreg,
3738 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
3739 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
3743 u_int8_t preg = 0xa4 + rego;
3744 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
3746 pci_write_config(gparent, mreg,
3747 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
3748 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
3750 atadev->mode = mode;
3754 ata_cmd_setmode(device_t dev, int mode)
3756 device_t gparent = GRANDPARENT(dev);
3757 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3758 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3759 struct ata_device *atadev = device_get_softc(dev);
3760 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3763 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3765 mode = ata_check_80pin(dev, mode);
3767 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3770 device_printf(dev, "%ssetting %s on %s chip\n",
3771 (error) ? "FAILURE " : "",
3772 ata_mode2str(mode), ctlr->chip->text);
3774 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
3775 int ureg = ch->unit ? 0x7b : 0x73;
3777 if (mode >= ATA_UDMA0) {
3778 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
3779 { 0x11, 0x42 }, { 0x25, 0x8a },
3780 { 0x15, 0x4a }, { 0x05, 0x0a } };
3782 u_int8_t umode = pci_read_config(gparent, ureg, 1);
3784 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
3785 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
3786 pci_write_config(gparent, ureg, umode, 1);
3788 else if (mode >= ATA_WDMA0) {
3789 int dmatimings[] = { 0x87, 0x32, 0x3f };
3791 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
3792 pci_write_config(gparent, ureg,
3793 pci_read_config(gparent, ureg, 1) &
3794 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3797 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
3798 pci_write_config(gparent, treg,
3799 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
3800 pci_write_config(gparent, ureg,
3801 pci_read_config(gparent, ureg, 1) &
3802 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3804 atadev->mode = mode;
3810 * Silicon Integrated Systems Corp. (SiS) chipset support functions
3813 ata_sis_ident(device_t dev)
3815 struct ata_pci_controller *ctlr = device_get_softc(dev);
3816 struct ata_chip_id *idx;
3817 static struct ata_chip_id ids[] =
3818 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "SiS 182" }, /* south */
3819 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "SiS 181" }, /* south */
3820 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "SiS 180" }, /* south */
3821 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 965" }, /* south */
3822 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 964" }, /* south */
3823 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 963" }, /* south */
3824 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 962" }, /* south */
3826 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 745" }, /* 1chip */
3827 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 735" }, /* 1chip */
3828 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 733" }, /* 1chip */
3829 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "SiS 730" }, /* 1chip */
3831 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 635" }, /* 1chip */
3832 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 633" }, /* unknown */
3833 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "SiS 630S"}, /* 1chip */
3834 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "SiS 630" }, /* 1chip */
3835 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "SiS 620" }, /* 1chip */
3837 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "SiS 550" },
3838 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "SiS 540" },
3839 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "SiS 530" },
3841 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "SiS 5513" },
3842 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "SiS 5513" },
3843 { 0, 0, 0, 0, 0, 0 }};
3847 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
3850 if (idx->cfg2 && !found) {
3851 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
3853 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
3854 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
3856 idx->cfg1 = SIS133NEW;
3857 idx->max_dma = ATA_UDMA6;
3858 sprintf(buffer, "SiS 962/963 %s controller",
3859 ata_mode2str(idx->max_dma));
3861 pci_write_config(dev, 0x57, reg57, 1);
3863 if (idx->cfg2 && !found) {
3864 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
3866 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
3867 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
3868 struct ata_chip_id id[] =
3869 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
3872 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
3873 idx->cfg1 = SIS133OLD;
3874 idx->max_dma = ATA_UDMA6;
3877 idx->cfg1 = SIS100NEW;
3878 idx->max_dma = ATA_UDMA5;
3880 sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
3882 pci_write_config(dev, 0x4a, reg4a, 1);
3885 sprintf(buffer,"%s %s controller",idx->text,ata_mode2str(idx->max_dma));
3887 device_set_desc_copy(dev, buffer);
3889 ctlr->chipinit = ata_sis_chipinit;
3894 ata_sis_chipinit(device_t dev)
3896 struct ata_pci_controller *ctlr = device_get_softc(dev);
3898 if (ata_setup_interrupt(dev))
3901 switch (ctlr->chip->cfg1) {
3906 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
3910 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
3913 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
3914 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
3917 ctlr->r_type2 = SYS_RES_IOPORT;
3918 ctlr->r_rid2 = PCIR_BAR(5);
3919 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3920 &ctlr->r_rid2, RF_ACTIVE))) {
3921 pci_write_config(dev, PCIR_COMMAND,
3922 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3923 ctlr->allocate = ata_sis_allocate;
3924 ctlr->reset = ata_sis_reset;
3926 ctlr->setmode = ata_sata_setmode;
3931 ctlr->setmode = ata_sis_setmode;
3936 ata_sis_allocate(device_t dev)
3938 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3939 struct ata_channel *ch = device_get_softc(dev);
3940 int offset = ch->unit << ((ctlr->chip->chipid == ATA_SIS182) ? 5 : 6);
3942 /* setup the usual register normal pci style */
3943 if (ata_pci_allocate(dev))
3946 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3947 ch->r_io[ATA_SSTATUS].offset = 0x00 + offset;
3948 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3949 ch->r_io[ATA_SERROR].offset = 0x04 + offset;
3950 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3951 ch->r_io[ATA_SCONTROL].offset = 0x08 + offset;
3952 ch->flags |= ATA_NO_SLAVE;
3954 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
3955 /* XXX SOS unknown how to enable PHY state change interrupt */
3960 ata_sis_reset(device_t dev)
3962 struct ata_channel *ch = device_get_softc(dev);
3964 ata_sata_phy_enable(ch);
3969 ata_sis_setmode(device_t dev, int mode)
3971 device_t gparent = GRANDPARENT(dev);
3972 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3973 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3974 struct ata_device *atadev = device_get_softc(dev);
3975 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3978 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3980 if (ctlr->chip->cfg1 == SIS133NEW) {
3981 if (mode > ATA_UDMA2 &&
3982 pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
3983 ata_print_cable(dev, "controller");
3988 if (mode > ATA_UDMA2 &&
3989 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
3990 ata_print_cable(dev, "controller");
3995 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3998 device_printf(dev, "%ssetting %s on %s chip\n",
3999 (error) ? "FAILURE " : "",
4000 ata_mode2str(mode), ctlr->chip->text);
4002 switch (ctlr->chip->cfg1) {
4004 u_int32_t timings[] =
4005 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
4006 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
4007 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
4010 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
4011 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
4015 u_int16_t timings[] =
4016 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
4017 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
4019 u_int16_t reg = 0x40 + (devno << 1);
4021 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
4025 u_int16_t timings[] =
4026 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
4027 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
4028 u_int16_t reg = 0x40 + (devno << 1);
4030 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
4036 u_int16_t timings[] =
4037 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
4038 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
4039 u_int16_t reg = 0x40 + (devno << 1);
4041 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
4045 atadev->mode = mode;
4050 /* VIA Technologies Inc. chipset support functions */
4052 ata_via_ident(device_t dev)
4054 struct ata_pci_controller *ctlr = device_get_softc(dev);
4055 struct ata_chip_id *idx;
4056 static struct ata_chip_id ids[] =
4057 {{ ATA_VIA82C586, 0x02, VIA33, 0x00, ATA_UDMA2, "VIA 82C586B" },
4058 { ATA_VIA82C586, 0x00, VIA33, 0x00, ATA_WDMA2, "VIA 82C586" },
4059 { ATA_VIA82C596, 0x12, VIA66, VIACLK, ATA_UDMA4, "VIA 82C596B" },
4060 { ATA_VIA82C596, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C596" },
4061 { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "VIA 82C686B"},
4062 { ATA_VIA82C686, 0x10, VIA66, VIACLK, ATA_UDMA4, "VIA 82C686A" },
4063 { ATA_VIA82C686, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C686" },
4064 { ATA_VIA8231, 0x00, VIA100, VIABUG, ATA_UDMA5, "VIA 8231" },
4065 { ATA_VIA8233, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233" },
4066 { ATA_VIA8233C, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233C" },
4067 { ATA_VIA8233A, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8233A" },
4068 { ATA_VIA8235, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8235" },
4069 { ATA_VIA8237, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8237" },
4070 { ATA_VIA8251, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8251" },
4071 { 0, 0, 0, 0, 0, 0 }};
4072 static struct ata_chip_id new_ids[] =
4073 {{ ATA_VIA6410, 0x00, 0, 0x00, ATA_UDMA6, "VIA 6410" },
4074 { ATA_VIA6420, 0x00, 7, 0x00, ATA_SA150, "VIA 6420" },
4075 { ATA_VIA6421, 0x00, 6, VIABAR, ATA_SA150, "VIA 6421" },
4076 { ATA_VIA8251, 0x00, 0, VIAAHCI, ATA_SA150, "VIA 8251" },
4077 { 0, 0, 0, 0, 0, 0 }};
4080 if (pci_get_devid(dev) == ATA_VIA82C571) {
4081 if (!(idx = ata_find_chip(dev, ids, -99)))
4085 if (!(idx = ata_match_chip(dev, new_ids)))
4089 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
4090 device_set_desc_copy(dev, buffer);
4092 ctlr->chipinit = ata_via_chipinit;
4097 ata_via_chipinit(device_t dev)
4099 struct ata_pci_controller *ctlr = device_get_softc(dev);
4101 if (ata_setup_interrupt(dev))
4104 if (ctlr->chip->max_dma >= ATA_SA150) {
4105 ctlr->r_type2 = SYS_RES_IOPORT;
4106 ctlr->r_rid2 = PCIR_BAR(5);
4107 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4108 &ctlr->r_rid2, RF_ACTIVE))) {
4109 pci_write_config(dev, PCIR_COMMAND,
4110 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
4111 ctlr->allocate = ata_via_allocate;
4112 ctlr->reset = ata_via_reset;
4114 ctlr->setmode = ata_sata_setmode;
4118 /* prepare for ATA-66 on the 82C686a and 82C596b */
4119 if (ctlr->chip->cfg2 & VIACLK)
4120 pci_write_config(dev, 0x50, 0x030b030b, 4);
4122 /* the southbridge might need the data corruption fix */
4123 if (ctlr->chip->cfg2 & VIABUG)
4124 ata_via_southbridge_fixup(dev);
4126 /* set fifo configuration half'n'half */
4127 pci_write_config(dev, 0x43,
4128 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
4130 /* set status register read retry */
4131 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
4133 /* set DMA read & end-of-sector fifo flush */
4134 pci_write_config(dev, 0x46,
4135 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
4137 /* set sector size */
4138 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
4139 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
4141 ctlr->setmode = ata_via_family_setmode;
4146 ata_via_allocate(device_t dev)
4148 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4149 struct ata_channel *ch = device_get_softc(dev);
4151 /* newer SATA chips has resources in one BAR for each channel */
4152 if (ctlr->chip->cfg2 & VIABAR) {
4153 struct resource *r_io;
4156 rid = PCIR_BAR(ch->unit);
4157 if (!(r_io = bus_alloc_resource_any(device_get_parent(dev),
4162 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
4163 ch->r_io[i].res = r_io;
4164 ch->r_io[i].offset = i;
4166 ch->r_io[ATA_CONTROL].res = r_io;
4167 ch->r_io[ATA_CONTROL].offset = 2 + ATA_IOSIZE;
4168 ch->r_io[ATA_IDX_ADDR].res = r_io;
4169 ata_default_registers(dev);
4170 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
4171 ch->r_io[i].res = ctlr->r_res1;
4172 ch->r_io[i].offset = i - ATA_BMCMD_PORT;
4174 ata_generic_hw(dev);
4177 if (ata_pci_allocate(dev))
4181 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4182 ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
4183 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4184 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
4185 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4186 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
4187 ch->flags |= ATA_NO_SLAVE;
4189 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
4190 /* XXX SOS unknown how to enable PHY state change interrupt */
4195 ata_via_reset(device_t dev)
4197 struct ata_channel *ch = device_get_softc(dev);
4199 ata_sata_phy_enable(ch);
4203 ata_via_southbridge_fixup(device_t dev)
4208 if (device_get_children(device_get_parent(dev), &children, &nchildren))
4211 for (i = 0; i < nchildren; i++) {
4212 if (pci_get_devid(children[i]) == ATA_VIA8363 ||
4213 pci_get_devid(children[i]) == ATA_VIA8371 ||
4214 pci_get_devid(children[i]) == ATA_VIA8662 ||
4215 pci_get_devid(children[i]) == ATA_VIA8361) {
4216 u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
4218 if ((reg76 & 0xf0) != 0xd0) {
4220 "Correcting VIA config for southbridge data corruption bug\n");
4221 pci_write_config(children[i], 0x75, 0x80, 1);
4222 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
4227 free(children, M_TEMP);
4231 /* common code for VIA, AMD & nVidia */
4233 ata_via_family_setmode(device_t dev, int mode)
4235 device_t gparent = GRANDPARENT(dev);
4236 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4237 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4238 struct ata_device *atadev = device_get_softc(dev);
4239 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
4240 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
4242 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* VIA ATA33 */
4243 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* VIA ATA66 */
4244 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* VIA ATA100 */
4245 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
4246 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }}; /* AMD/nVIDIA */
4247 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4248 int reg = 0x53 - devno;
4251 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4253 if (ctlr->chip->cfg2 & AMDCABLE) {
4254 if (mode > ATA_UDMA2 &&
4255 !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
4256 ata_print_cable(dev, "controller");
4261 mode = ata_check_80pin(dev, mode);
4263 if (ctlr->chip->cfg2 & NVIDIA)
4266 if (ctlr->chip->cfg1 != VIA133)
4267 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
4269 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4272 device_printf(dev, "%ssetting %s on %s chip\n",
4273 (error) ? "FAILURE " : "", ata_mode2str(mode),
4276 if (mode >= ATA_UDMA0)
4277 pci_write_config(gparent, reg,
4278 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
4280 pci_write_config(gparent, reg, 0x8b, 1);
4281 atadev->mode = mode;
4286 /* misc functions */
4287 struct ata_chip_id *
4288 ata_match_chip(device_t dev, struct ata_chip_id *index)
4290 while (index->chipid != 0) {
4291 if (pci_get_devid(dev) == index->chipid &&
4292 pci_get_revid(dev) >= index->chiprev)
4299 static struct ata_chip_id *
4300 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
4305 if (device_get_children(device_get_parent(dev), &children, &nchildren))
4308 while (index->chipid != 0) {
4309 for (i = 0; i < nchildren; i++) {
4310 if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
4311 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
4312 pci_get_devid(children[i]) == index->chipid &&
4313 pci_get_revid(children[i]) >= index->chiprev) {
4314 free(children, M_TEMP);
4320 free(children, M_TEMP);
4325 ata_setup_interrupt(device_t dev)
4327 struct ata_pci_controller *ctlr = device_get_softc(dev);
4328 int rid = ATA_IRQ_RID;
4330 if (!ata_legacy(dev)) {
4331 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
4332 RF_SHAREABLE | RF_ACTIVE))) {
4333 device_printf(dev, "unable to map interrupt\n");
4336 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
4337 ata_generic_intr, ctlr, &ctlr->handle))) {
4338 device_printf(dev, "unable to setup interrupt\n");
4345 struct ata_serialize {
4346 struct mtx locked_mtx;
4352 ata_serialize(device_t dev, int flags)
4354 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4355 struct ata_channel *ch = device_get_softc(dev);
4356 struct ata_serialize *serial;
4357 static int inited = 0;
4361 serial = malloc(sizeof(struct ata_serialize),
4362 M_TEMP, M_NOWAIT | M_ZERO);
4363 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
4364 serial->locked_ch = -1;
4365 serial->restart_ch = -1;
4366 device_set_ivars(ctlr->dev, serial);
4370 serial = device_get_ivars(ctlr->dev);
4372 mtx_lock(&serial->locked_mtx);
4375 if (serial->locked_ch == -1)
4376 serial->locked_ch = ch->unit;
4377 if (serial->locked_ch != ch->unit)
4378 serial->restart_ch = ch->unit;
4382 if (serial->locked_ch == ch->unit) {
4383 serial->locked_ch = -1;
4384 if (serial->restart_ch != -1) {
4385 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
4386 serial->restart_ch = -1;
4387 mtx_unlock(&serial->locked_mtx);
4398 res = serial->locked_ch;
4399 mtx_unlock(&serial->locked_mtx);
4404 ata_print_cable(device_t dev, u_int8_t *who)
4407 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
4411 ata_atapi(device_t dev)
4413 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4414 struct ata_device *atadev = device_get_softc(dev);
4416 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
4417 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
4421 ata_check_80pin(device_t dev, int mode)
4423 struct ata_device *atadev = device_get_softc(dev);
4425 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
4426 ata_print_cable(dev, "device");
4433 ata_mode2idx(int mode)
4435 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
4436 return (mode & ATA_MODE_MASK) + 8;
4437 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
4438 return (mode & ATA_MODE_MASK) + 5;
4439 return (mode & ATA_MODE_MASK) - ATA_PIO0;