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 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_SERR + offset, error);
573 status = ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset);
574 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset, status);
575 issued = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CI + offset);
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_PC) &&
585 (tp = (struct ata_connect_task *)
586 malloc(sizeof(struct ata_connect_task),
587 M_ATA, M_NOWAIT | M_ZERO))) {
589 device_printf(ch->dev, "CONNECT requested\n");
590 tp->action = ATA_C_ATTACH;
592 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
593 taskqueue_enqueue(taskqueue_thread, &tp->task);
596 /* check for and handle disconnect events */
597 if (((status & (ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC)) ==
598 ATA_AHCI_P_IX_PRC) &&
599 (tp = (struct ata_connect_task *)
600 malloc(sizeof(struct ata_connect_task),
601 M_ATA, M_NOWAIT | M_ZERO))) {
603 device_printf(ch->dev, "DISCONNECT requested\n");
604 tp->action = ATA_C_DETACH;
606 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
607 taskqueue_enqueue(taskqueue_thread, &tp->task);
610 /* any drive action to take care of ? */
611 if (!(issued & (1<<tag)))
612 ctlr->interrupt[unit].function(ch);
616 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, port);
620 ata_ahci_reset(device_t dev)
622 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
623 struct ata_channel *ch = device_get_softc(dev);
625 int offset = (ch->unit << 7);
627 /* kill off all activity on this channel */
628 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
629 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
630 cmd & ~(ATA_AHCI_P_CMD_CR | ATA_AHCI_P_CMD_FR |
631 ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
633 DELAY(500000); /* XXX SOS this is not entirely wrong */
636 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, ATA_AHCI_P_CMD_SUD);
638 ata_sata_phy_enable(ch);
640 /* clear any interrupts pending on this channel */
641 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset,
642 ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset));
644 /* start operations on this channel */
645 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
646 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
647 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
651 ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
653 struct ata_dmasetprd_args *args = xsc;
654 struct ata_ahci_dma_prd *prd = args->dmatab;
657 if (!(args->error = error)) {
658 for (i = 0; i < nsegs; i++) {
659 prd[i].dba = htole64(segs[i].ds_addr);
660 prd[i].dbc = htole32((segs[i].ds_len - 1) & ATA_AHCI_PRD_MASK);
667 ata_ahci_dmainit(device_t dev)
669 struct ata_channel *ch = device_get_softc(dev);
673 /* note start and stop are not used here */
674 ch->dma->setprd = ata_ahci_dmasetprd;
675 ch->dma->max_iosize = 8192 * DEV_BSIZE;
681 * Acard chipset support functions
684 ata_acard_ident(device_t dev)
686 struct ata_pci_controller *ctlr = device_get_softc(dev);
687 struct ata_chip_id *idx;
688 static struct ata_chip_id ids[] =
689 {{ ATA_ATP850R, 0, ATPOLD, 0x00, ATA_UDMA2, "Acard ATP850" },
690 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "Acard ATP860A" },
691 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "Acard ATP860R" },
692 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "Acard ATP865A" },
693 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "Acard ATP865R" },
694 { 0, 0, 0, 0, 0, 0}};
697 if (!(idx = ata_match_chip(dev, ids)))
700 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
701 device_set_desc_copy(dev, buffer);
703 ctlr->chipinit = ata_acard_chipinit;
708 ata_acard_chipinit(device_t dev)
710 struct ata_pci_controller *ctlr = device_get_softc(dev);
711 int rid = ATA_IRQ_RID;
713 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
714 RF_SHAREABLE | RF_ACTIVE))) {
715 device_printf(dev, "unable to map interrupt\n");
718 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
719 ata_acard_intr, ctlr, &ctlr->handle))) {
720 device_printf(dev, "unable to setup interrupt\n");
723 if (ctlr->chip->cfg1 == ATPOLD) {
724 ctlr->setmode = ata_acard_850_setmode;
725 ctlr->locking = ata_serialize;
728 ctlr->setmode = ata_acard_86X_setmode;
733 ata_acard_intr(void *data)
735 struct ata_pci_controller *ctlr = data;
736 struct ata_channel *ch;
739 for (unit = 0; unit < ctlr->channels; unit++) {
740 if (!(ch = ctlr->interrupt[unit].argument))
742 if (ctlr->chip->cfg1 == ATPOLD &&
743 ATA_LOCKING(ch->dev, ATA_LF_WHICH) != unit)
745 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
746 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
748 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
749 ATA_BMSTAT_INTERRUPT)
751 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
753 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
754 ATA_IDX_INB(ch, ATA_BMCMD_PORT)&~ATA_BMCMD_START_STOP);
757 ctlr->interrupt[unit].function(ch);
762 ata_acard_850_setmode(device_t dev, int mode)
764 device_t gparent = GRANDPARENT(dev);
765 struct ata_pci_controller *ctlr = device_get_softc(gparent);
766 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
767 struct ata_device *atadev = device_get_softc(dev);
768 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
771 mode = ata_limit_mode(dev, mode,
772 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
774 /* XXX SOS missing WDMA0+1 + PIO modes */
775 if (mode >= ATA_WDMA2) {
776 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
778 device_printf(dev, "%ssetting %s on %s chip\n",
779 (error) ? "FAILURE " : "",
780 ata_mode2str(mode), ctlr->chip->text);
782 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
784 reg54 &= ~(0x03 << (devno << 1));
785 if (mode >= ATA_UDMA0)
786 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
787 pci_write_config(gparent, 0x54, reg54, 1);
788 pci_write_config(gparent, 0x4a, 0xa6, 1);
789 pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
794 /* we could set PIO mode timings, but we assume the BIOS did that */
798 ata_acard_86X_setmode(device_t dev, int mode)
800 device_t gparent = GRANDPARENT(dev);
801 struct ata_pci_controller *ctlr = device_get_softc(gparent);
802 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
803 struct ata_device *atadev = device_get_softc(dev);
804 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
808 mode = ata_limit_mode(dev, mode,
809 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
811 mode = ata_check_80pin(dev, mode);
813 /* XXX SOS missing WDMA0+1 + PIO modes */
814 if (mode >= ATA_WDMA2) {
815 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
817 device_printf(dev, "%ssetting %s on %s chip\n",
818 (error) ? "FAILURE " : "",
819 ata_mode2str(mode), ctlr->chip->text);
821 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
823 reg44 &= ~(0x000f << (devno << 2));
824 if (mode >= ATA_UDMA0)
825 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
826 pci_write_config(gparent, 0x44, reg44, 2);
827 pci_write_config(gparent, 0x4a, 0xa6, 1);
828 pci_write_config(gparent, 0x40 + devno, 0x31, 1);
833 /* we could set PIO mode timings, but we assume the BIOS did that */
838 * Acer Labs Inc (ALI) chipset support functions
841 ata_ali_ident(device_t dev)
843 struct ata_pci_controller *ctlr = device_get_softc(dev);
844 struct ata_chip_id *idx;
845 static struct ata_chip_id ids[] =
846 {{ ATA_ALI_5289, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5289" },
847 { ATA_ALI_5287, 0x00, 4, ALISATA, ATA_SA150, "AcerLabs M5287" },
848 { ATA_ALI_5281, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5281" },
849 { ATA_ALI_5229, 0xc5, 0, ALINEW, ATA_UDMA6, "AcerLabs M5229" },
850 { ATA_ALI_5229, 0xc4, 0, ALINEW, ATA_UDMA5, "AcerLabs M5229" },
851 { ATA_ALI_5229, 0xc2, 0, ALINEW, ATA_UDMA4, "AcerLabs M5229" },
852 { ATA_ALI_5229, 0x20, 0, ALIOLD, ATA_UDMA2, "AcerLabs M5229" },
853 { ATA_ALI_5229, 0x00, 0, ALIOLD, ATA_WDMA2, "AcerLabs M5229" },
854 { 0, 0, 0, 0, 0, 0}};
857 if (!(idx = ata_match_chip(dev, ids)))
860 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
861 device_set_desc_copy(dev, buffer);
863 ctlr->chipinit = ata_ali_chipinit;
868 ata_ali_chipinit(device_t dev)
870 struct ata_pci_controller *ctlr = device_get_softc(dev);
872 if (ata_setup_interrupt(dev))
875 switch (ctlr->chip->cfg2) {
877 pci_write_config(dev, PCIR_COMMAND,
878 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
879 ctlr->channels = ctlr->chip->cfg1;
880 ctlr->allocate = ata_ali_sata_allocate;
881 ctlr->setmode = ata_sata_setmode;
885 /* use device interrupt as byte count end */
886 pci_write_config(dev, 0x4a, pci_read_config(dev, 0x4a, 1) | 0x20, 1);
888 /* enable cable detection and UDMA support on newer chips */
889 pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
891 /* enable ATAPI UDMA mode */
892 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x01, 1);
894 /* only chips with revision > 0xc4 can do 48bit DMA */
895 if (ctlr->chip->chiprev <= 0xc4)
897 "using PIO transfers above 137GB as workaround for "
898 "48bit DMA access bug, expect reduced performance\n");
899 ctlr->reset = ata_ali_reset;
900 ctlr->allocate = ata_ali_allocate;
901 ctlr->setmode = ata_ali_setmode;
905 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
906 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x03, 1);
907 ctlr->setmode = ata_ali_setmode;
914 ata_ali_allocate(device_t dev)
916 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
917 struct ata_channel *ch = device_get_softc(dev);
919 /* setup the usual register normal pci style */
920 if (ata_pci_allocate(dev))
923 /* older chips can't do 48bit DMA transfers */
924 if (ctlr->chip->chiprev <= 0xc4)
925 ch->flags |= ATA_NO_48BIT_DMA;
931 ata_ali_sata_allocate(device_t dev)
933 device_t parent = device_get_parent(dev);
934 struct ata_pci_controller *ctlr = device_get_softc(parent);
935 struct ata_channel *ch = device_get_softc(dev);
936 struct resource *io = NULL, *ctlio = NULL;
937 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
940 rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
941 io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
945 rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
946 ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
948 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
952 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
953 ch->r_io[i].res = io;
954 ch->r_io[i].offset = i + (unit10 ? 8 : 0);
956 ch->r_io[ATA_CONTROL].res = ctlio;
957 ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
958 ch->r_io[ATA_IDX_ADDR].res = io;
959 ata_default_registers(dev);
961 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
962 ch->r_io[i].res = ctlr->r_res1;
963 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
966 ch->flags |= ATA_NO_SLAVE;
968 /* XXX SOS PHY handling awkward in ALI chip not supported yet */
974 ata_ali_reset(device_t dev)
976 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
977 struct ata_channel *ch = device_get_softc(dev);
981 ata_generic_reset(dev);
984 * workaround for datacorruption bug found on at least SUN Blade-100
985 * find the ISA function on the southbridge and disable then enable
986 * the ATA channel tristate buffer
988 if (ctlr->chip->chiprev == 0xc3 || ctlr->chip->chiprev == 0xc2) {
989 if (!device_get_children(GRANDPARENT(dev), &children, &nchildren)) {
990 for (i = 0; i < nchildren; i++) {
991 if (pci_get_devid(children[i]) == ATA_ALI_1533) {
992 pci_write_config(children[i], 0x58,
993 pci_read_config(children[i], 0x58, 1) &
994 ~(0x04 << ch->unit), 1);
995 pci_write_config(children[i], 0x58,
996 pci_read_config(children[i], 0x58, 1) |
997 (0x04 << ch->unit), 1);
1001 free(children, M_TEMP);
1007 ata_ali_setmode(device_t dev, int mode)
1009 device_t gparent = GRANDPARENT(dev);
1010 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1011 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1012 struct ata_device *atadev = device_get_softc(dev);
1013 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1016 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1018 if (ctlr->chip->cfg2 & ALINEW) {
1019 if (mode > ATA_UDMA2 &&
1020 pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
1021 ata_print_cable(dev, "controller");
1026 mode = ata_check_80pin(dev, mode);
1028 if (ctlr->chip->cfg2 & ALIOLD) {
1029 /* doesn't support ATAPI DMA on write */
1030 ch->flags |= ATA_ATAPI_DMA_RO;
1031 if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
1032 /* doesn't support ATAPI DMA on two ATAPI devices */
1033 device_printf(dev, "two atapi devices on this channel, no DMA\n");
1034 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1038 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1041 device_printf(dev, "%ssetting %s on %s chip\n",
1042 (error) ? "FAILURE " : "",
1043 ata_mode2str(mode), ctlr->chip->text);
1045 if (mode >= ATA_UDMA0) {
1046 u_int8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
1047 u_int32_t word54 = pci_read_config(gparent, 0x54, 4);
1049 word54 &= ~(0x000f000f << (devno << 2));
1050 word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
1051 pci_write_config(gparent, 0x54, word54, 4);
1052 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1056 u_int32_t piotimings[] =
1057 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
1058 0x00310001, 0x00440001, 0x00330001, 0x00310001};
1060 pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
1061 ~(0x0008000f << (devno << 2)), 4);
1062 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1063 piotimings[ata_mode2idx(mode)], 4);
1065 atadev->mode = mode;
1071 * American Micro Devices (AMD) chipset support functions
1074 ata_amd_ident(device_t dev)
1076 struct ata_pci_controller *ctlr = device_get_softc(dev);
1077 struct ata_chip_id *idx;
1078 static struct ata_chip_id ids[] =
1079 {{ ATA_AMD756, 0x00, AMDNVIDIA, 0x00, ATA_UDMA4, "AMD 756" },
1080 { ATA_AMD766, 0x00, AMDNVIDIA, AMDCABLE|AMDBUG, ATA_UDMA5, "AMD 766" },
1081 { ATA_AMD768, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA5, "AMD 768" },
1082 { ATA_AMD8111, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA6, "AMD 8111" },
1083 { 0, 0, 0, 0, 0, 0}};
1086 if (!(idx = ata_match_chip(dev, ids)))
1089 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1090 device_set_desc_copy(dev, buffer);
1092 ctlr->chipinit = ata_amd_chipinit;
1097 ata_amd_chipinit(device_t dev)
1099 struct ata_pci_controller *ctlr = device_get_softc(dev);
1101 if (ata_setup_interrupt(dev))
1104 /* disable/set prefetch, postwrite */
1105 if (ctlr->chip->cfg2 & AMDBUG)
1106 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
1108 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
1110 ctlr->setmode = ata_via_family_setmode;
1116 * ATI chipset support functions
1119 ata_ati_ident(device_t dev)
1121 struct ata_pci_controller *ctlr = device_get_softc(dev);
1122 struct ata_chip_id *idx;
1123 static struct ata_chip_id ids[] =
1124 {{ ATA_ATI_IXP200, 0x00, 0, 0, ATA_UDMA5, "ATI IXP200" },
1125 { ATA_ATI_IXP300, 0x00, 0, 0, ATA_UDMA6, "ATI IXP300" },
1126 { ATA_ATI_IXP400, 0x00, 0, 0, ATA_UDMA6, "ATI IXP400" },
1127 { ATA_ATI_IXP300_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "ATI IXP300" },
1128 { ATA_ATI_IXP400_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "ATI IXP400" },
1129 { ATA_ATI_IXP400_S2, 0x00, SIIMEMIO, 0, ATA_SA150, "ATI IXP400" },
1130 { 0, 0, 0, 0, 0, 0}};
1133 if (!(idx = ata_match_chip(dev, ids)))
1136 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1137 device_set_desc_copy(dev, buffer);
1140 /* the ATI SATA controller is actually a SiI 3112 controller*/
1141 if (ctlr->chip->cfg1 & SIIMEMIO)
1142 ctlr->chipinit = ata_sii_chipinit;
1144 ctlr->chipinit = ata_ati_chipinit;
1149 ata_ati_chipinit(device_t dev)
1151 struct ata_pci_controller *ctlr = device_get_softc(dev);
1153 if (ata_setup_interrupt(dev))
1156 ctlr->setmode = ata_ati_setmode;
1161 ata_ati_setmode(device_t dev, int mode)
1163 device_t gparent = GRANDPARENT(dev);
1164 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1165 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1166 struct ata_device *atadev = device_get_softc(dev);
1167 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1168 int offset = (devno ^ 0x01) << 3;
1170 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
1171 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
1172 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
1174 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1176 mode = ata_check_80pin(dev, mode);
1178 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1181 device_printf(dev, "%ssetting %s on %s chip\n",
1182 (error) ? "FAILURE " : "",
1183 ata_mode2str(mode), ctlr->chip->text);
1185 if (mode >= ATA_UDMA0) {
1186 pci_write_config(gparent, 0x56,
1187 (pci_read_config(gparent, 0x56, 2) &
1188 ~(0xf << (devno << 2))) |
1189 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
1190 pci_write_config(gparent, 0x54,
1191 pci_read_config(gparent, 0x54, 1) |
1192 (0x01 << devno), 1);
1193 pci_write_config(gparent, 0x44,
1194 (pci_read_config(gparent, 0x44, 4) &
1195 ~(0xff << offset)) |
1196 (dmatimings[2] << offset), 4);
1198 else if (mode >= ATA_WDMA0) {
1199 pci_write_config(gparent, 0x54,
1200 pci_read_config(gparent, 0x54, 1) &
1201 ~(0x01 << devno), 1);
1202 pci_write_config(gparent, 0x44,
1203 (pci_read_config(gparent, 0x44, 4) &
1204 ~(0xff << offset)) |
1205 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
1208 pci_write_config(gparent, 0x54,
1209 pci_read_config(gparent, 0x54, 1) &
1210 ~(0x01 << devno), 1);
1212 pci_write_config(gparent, 0x4a,
1213 (pci_read_config(gparent, 0x4a, 2) &
1214 ~(0xf << (devno << 2))) |
1215 (((mode - ATA_PIO0) & ATA_MODE_MASK) << (devno<<2)),2);
1216 pci_write_config(gparent, 0x40,
1217 (pci_read_config(gparent, 0x40, 4) &
1218 ~(0xff << offset)) |
1219 (piotimings[ata_mode2idx(mode)] << offset), 4);
1220 atadev->mode = mode;
1225 * Cyrix chipset support functions
1228 ata_cyrix_ident(device_t dev)
1230 struct ata_pci_controller *ctlr = device_get_softc(dev);
1232 if (pci_get_devid(dev) == ATA_CYRIX_5530) {
1233 device_set_desc(dev, "Cyrix 5530 ATA33 controller");
1234 ctlr->chipinit = ata_cyrix_chipinit;
1241 ata_cyrix_chipinit(device_t dev)
1243 struct ata_pci_controller *ctlr = device_get_softc(dev);
1245 if (ata_setup_interrupt(dev))
1249 ctlr->setmode = ata_cyrix_setmode;
1251 ctlr->setmode = ata_generic_setmode;
1256 ata_cyrix_setmode(device_t dev, int mode)
1258 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1259 struct ata_device *atadev = device_get_softc(dev);
1260 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1261 u_int32_t piotiming[] =
1262 { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
1263 u_int32_t dmatiming[] = { 0x00077771, 0x00012121, 0x00002020 };
1264 u_int32_t udmatiming[] = { 0x00921250, 0x00911140, 0x00911030 };
1267 ch->dma->alignment = 16;
1268 ch->dma->max_iosize = 126 * DEV_BSIZE;
1270 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1272 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1275 device_printf(dev, "%ssetting %s on Cyrix chip\n",
1276 (error) ? "FAILURE " : "", ata_mode2str(mode));
1278 if (mode >= ATA_UDMA0) {
1279 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1280 0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
1282 else if (mode >= ATA_WDMA0) {
1283 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1284 0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
1287 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1288 0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
1290 atadev->mode = mode;
1296 * Cypress chipset support functions
1299 ata_cypress_ident(device_t dev)
1301 struct ata_pci_controller *ctlr = device_get_softc(dev);
1304 * the Cypress chip is a mess, it contains two ATA functions, but
1305 * both channels are visible on the first one.
1306 * simply ignore the second function for now, as the right
1307 * solution (ignoring the second channel on the first function)
1308 * doesn't work with the crappy ATA interrupt setup on the alpha.
1310 if (pci_get_devid(dev) == ATA_CYPRESS_82C693 &&
1311 pci_get_function(dev) == 1 &&
1312 pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
1313 device_set_desc(dev, "Cypress 82C693 ATA controller");
1314 ctlr->chipinit = ata_cypress_chipinit;
1321 ata_cypress_chipinit(device_t dev)
1323 struct ata_pci_controller *ctlr = device_get_softc(dev);
1325 if (ata_setup_interrupt(dev))
1328 ctlr->setmode = ata_cypress_setmode;
1333 ata_cypress_setmode(device_t dev, int mode)
1335 device_t gparent = GRANDPARENT(dev);
1336 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1337 struct ata_device *atadev = device_get_softc(dev);
1340 mode = ata_limit_mode(dev, mode, ATA_WDMA2);
1342 /* XXX SOS missing WDMA0+1 + PIO modes */
1343 if (mode == ATA_WDMA2) {
1344 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1346 device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
1347 error ? "FAILURE " : "");
1349 pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
1350 atadev->mode = mode;
1354 /* we could set PIO mode timings, but we assume the BIOS did that */
1359 * HighPoint chipset support functions
1362 ata_highpoint_ident(device_t dev)
1364 struct ata_pci_controller *ctlr = device_get_softc(dev);
1365 struct ata_chip_id *idx;
1366 static struct ata_chip_id ids[] =
1367 {{ ATA_HPT374, 0x07, HPT374, 0x00, ATA_UDMA6, "HighPoint HPT374" },
1368 { ATA_HPT372, 0x02, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372N" },
1369 { ATA_HPT372, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372" },
1370 { ATA_HPT371, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT371" },
1371 { ATA_HPT366, 0x05, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372" },
1372 { ATA_HPT366, 0x03, HPT370, 0x00, ATA_UDMA5, "HighPoint HPT370" },
1373 { ATA_HPT366, 0x02, HPT366, 0x00, ATA_UDMA4, "HighPoint HPT368" },
1374 { ATA_HPT366, 0x00, HPT366, HPTOLD, ATA_UDMA4, "HighPoint HPT366" },
1375 { ATA_HPT302, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT302" },
1376 { 0, 0, 0, 0, 0, 0}};
1379 if (!(idx = ata_match_chip(dev, ids)))
1382 strcpy(buffer, idx->text);
1383 if (idx->cfg1 == HPT374) {
1384 if (pci_get_function(dev) == 0)
1385 strcat(buffer, " (channel 0+1)");
1386 else if (pci_get_function(dev) == 1)
1387 strcat(buffer, " (channel 2+3)");
1389 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
1390 device_set_desc_copy(dev, buffer);
1392 ctlr->chipinit = ata_highpoint_chipinit;
1397 ata_highpoint_chipinit(device_t dev)
1399 struct ata_pci_controller *ctlr = device_get_softc(dev);
1400 int rid = ATA_IRQ_RID;
1402 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1403 RF_SHAREABLE | RF_ACTIVE))) {
1404 device_printf(dev, "unable to map interrupt\n");
1407 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1408 ata_highpoint_intr, ctlr, &ctlr->handle))) {
1409 device_printf(dev, "unable to setup interrupt\n");
1413 if (ctlr->chip->cfg2 == HPTOLD) {
1414 /* disable interrupt prediction */
1415 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
1418 /* disable interrupt prediction */
1419 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
1420 pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
1422 /* enable interrupts */
1423 pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
1425 /* set clocks etc */
1426 if (ctlr->chip->cfg1 < HPT372)
1427 pci_write_config(dev, 0x5b, 0x22, 1);
1429 pci_write_config(dev, 0x5b,
1430 (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
1432 ctlr->setmode = ata_highpoint_setmode;
1437 ata_highpoint_intr(void *data)
1439 struct ata_pci_controller *ctlr = data;
1440 struct ata_channel *ch;
1443 for (unit = 0; unit < ctlr->channels; unit++) {
1444 if (!(ch = ctlr->interrupt[unit].argument))
1447 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1449 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1450 ATA_BMSTAT_INTERRUPT)
1452 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1455 ctlr->interrupt[unit].function(ch);
1460 ata_highpoint_setmode(device_t dev, int mode)
1462 device_t gparent = GRANDPARENT(dev);
1463 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1464 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1465 struct ata_device *atadev = device_get_softc(dev);
1466 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1468 u_int32_t timings33[][4] = {
1469 /* HPT366 HPT370 HPT372 HPT374 mode */
1470 { 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a }, /* PIO 0 */
1471 { 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 }, /* PIO 1 */
1472 { 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 }, /* PIO 2 */
1473 { 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 }, /* PIO 3 */
1474 { 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 }, /* PIO 4 */
1475 { 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea }, /* MWDMA 0 */
1476 { 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 }, /* MWDMA 1 */
1477 { 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 }, /* MWDMA 2 */
1478 { 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea }, /* UDMA 0 */
1479 { 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 }, /* UDMA 1 */
1480 { 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 }, /* UDMA 2 */
1481 { 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 }, /* UDMA 3 */
1482 { 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 }, /* UDMA 4 */
1483 { 0, 0x16454e31, 0x1c8a9c62, 0x12848242 }, /* UDMA 5 */
1484 { 0, 0, 0x1c869c62, 0x12808242 } /* UDMA 6 */
1487 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1489 if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
1490 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1492 mode = ata_highpoint_check_80pin(dev, mode);
1495 * most if not all HPT chips cant really handle that the device is
1496 * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
1497 * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
1499 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
1500 ata_limit_mode(dev, mode, ATA_UDMA5));
1502 device_printf(dev, "%ssetting %s on HighPoint chip\n",
1503 (error) ? "FAILURE " : "", ata_mode2str(mode));
1505 pci_write_config(gparent, 0x40 + (devno << 2),
1506 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
1507 atadev->mode = mode;
1511 ata_highpoint_check_80pin(device_t dev, int mode)
1513 device_t gparent = GRANDPARENT(dev);
1514 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1515 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1516 u_int8_t reg, val, res;
1518 if (ctlr->chip->cfg1 == HPT374 && pci_get_function(gparent) == 1) {
1519 reg = ch->unit ? 0x57 : 0x53;
1520 val = pci_read_config(gparent, reg, 1);
1521 pci_write_config(gparent, reg, val | 0x80, 1);
1525 val = pci_read_config(gparent, reg, 1);
1526 pci_write_config(gparent, reg, val & 0xfe, 1);
1528 res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1529 pci_write_config(gparent, reg, val, 1);
1531 if (mode > ATA_UDMA2 && res) {
1532 ata_print_cable(dev, "controller");
1540 * Intel chipset support functions
1543 ata_intel_ident(device_t dev)
1545 struct ata_pci_controller *ctlr = device_get_softc(dev);
1546 struct ata_chip_id *idx;
1547 static struct ata_chip_id ids[] =
1548 {{ ATA_I82371FB, 0, 0, 0x00, ATA_WDMA2, "Intel PIIX" },
1549 { ATA_I82371SB, 0, 0, 0x00, ATA_WDMA2, "Intel PIIX3" },
1550 { ATA_I82371AB, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1551 { ATA_I82443MX, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1552 { ATA_I82451NX, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1553 { ATA_I82801AB, 0, 0, 0x00, ATA_UDMA2, "Intel ICH0" },
1554 { ATA_I82801AA, 0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1555 { ATA_I82372FB, 0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1556 { ATA_I82801BA, 0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1557 { ATA_I82801BA_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1558 { ATA_I82801CA, 0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1559 { ATA_I82801CA_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1560 { ATA_I82801DB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1561 { ATA_I82801DB_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1562 { ATA_I82801EB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH5" },
1563 { ATA_I82801EB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1564 { ATA_I82801EB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1565 { ATA_I6300ESB, 0, 0, 0x00, ATA_UDMA5, "Intel 6300ESB" },
1566 { ATA_I6300ESB_S1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1567 { ATA_I6300ESB_R1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1568 { ATA_I82801FB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH6" },
1569 { ATA_I82801FB_S1, 0, AHCI, 0x00, ATA_SA150, "Intel ICH6" },
1570 { ATA_I82801FB_R1, 0, AHCI, 0x00, ATA_SA150, "Intel ICH6" },
1571 { ATA_I82801FB_M, 0, AHCI, 0x00, ATA_SA150, "Intel ICH6" },
1572 { ATA_I82801GB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH7" },
1573 { ATA_I82801GB_S1, 0, AHCI, 0x00, ATA_SA300, "Intel ICH7" },
1574 { ATA_I82801GB_R1, 0, AHCI, 0x00, ATA_SA300, "Intel ICH7" },
1575 { ATA_I82801GB_M, 0, AHCI, 0x00, ATA_SA300, "Intel ICH7" },
1576 { ATA_I82801GB_AH, 0, AHCI, 0x00, ATA_SA300, "Intel ICH7" },
1577 { ATA_I31244, 0, 0, 0x00, ATA_SA150, "Intel 31244" },
1578 { 0, 0, 0, 0, 0, 0}};
1581 if (!(idx = ata_match_chip(dev, ids)))
1584 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1585 device_set_desc_copy(dev, buffer);
1587 ctlr->chipinit = ata_intel_chipinit;
1592 ata_intel_chipinit(device_t dev)
1594 struct ata_pci_controller *ctlr = device_get_softc(dev);
1595 int rid = ATA_IRQ_RID;
1597 if (!ata_legacy(dev)) {
1598 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1599 RF_SHAREABLE | RF_ACTIVE))) {
1600 device_printf(dev, "unable to map interrupt\n");
1603 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1604 ata_intel_intr, ctlr, &ctlr->handle))) {
1605 device_printf(dev, "unable to setup interrupt\n");
1610 /* good old PIIX needs special treatment (not implemented) */
1611 if (ctlr->chip->chipid == ATA_I82371FB) {
1612 ctlr->setmode = ata_intel_old_setmode;
1615 /* the intel 31244 needs special care if in DPA mode */
1616 else if (ctlr->chip->chipid == ATA_I31244) {
1617 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE) {
1618 ctlr->r_type2 = SYS_RES_MEMORY;
1619 ctlr->r_rid2 = PCIR_BAR(0);
1620 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1625 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
1626 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1627 ata_intel_31244_intr, ctlr, &ctlr->handle)) {
1628 device_printf(dev, "unable to setup interrupt\n");
1632 ctlr->reset = ata_intel_31244_reset;
1633 ctlr->allocate = ata_intel_31244_allocate;
1635 ctlr->setmode = ata_sata_setmode;
1638 /* non SATA intel chips goes here */
1639 else if (ctlr->chip->max_dma < ATA_SA150) {
1640 ctlr->setmode = ata_intel_new_setmode;
1643 /* SATA parts can be either compat or AHCI */
1645 /* force all ports active "the legacy way" */
1646 pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f,2);
1648 ctlr->reset = ata_intel_reset;
1650 /* if we have AHCI capability and BAR(5) as a memory resource */
1651 if (ctlr->chip->cfg1 == AHCI) {
1652 ctlr->r_type2 = SYS_RES_MEMORY;
1653 ctlr->r_rid2 = PCIR_BAR(5);
1654 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1657 /* is AHCI or RAID mode enabled in BIOS ? */
1658 if (pci_read_config(dev, 0x90, 1) & 0xc0) {
1659 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
1660 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1661 ata_ahci_intr, ctlr, &ctlr->handle)) {
1662 device_printf(dev, "unable to setup interrupt\n");
1666 /* enable AHCI mode */
1667 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC, ATA_AHCI_GHC_AE);
1669 /* get the number of HW channels */
1670 ctlr->channels = (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) &
1671 ATA_AHCI_NPMASK) + 1;
1673 /* enable AHCI interrupts */
1674 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
1675 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) |
1677 ctlr->reset = ata_ahci_reset;
1678 ctlr->dmainit = ata_ahci_dmainit;
1679 ctlr->allocate = ata_ahci_allocate;
1683 /* enable SATA registers in compat mode */
1684 pci_write_config(dev, 0x94,
1685 pci_read_config(dev, 0x94, 4) | 1 << 9, 4);
1690 ctlr->setmode = ata_sata_setmode;
1692 /* enable PCI interrupt */
1693 pci_write_config(dev, PCIR_COMMAND,
1694 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1700 ata_intel_31244_allocate(device_t dev)
1702 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1703 struct ata_channel *ch = device_get_softc(dev);
1707 ch_offset = 0x200 + ch->unit * 0x200;
1709 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
1710 ch->r_io[i].res = ctlr->r_res2;
1711 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
1712 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x06;
1713 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
1714 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
1715 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
1716 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
1717 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
1718 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1d;
1719 ch->r_io[ATA_ERROR].offset = ch_offset + 0x04;
1720 ch->r_io[ATA_STATUS].offset = ch_offset + 0x1c;
1721 ch->r_io[ATA_ALTSTAT].offset = ch_offset + 0x28;
1722 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x29;
1723 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x100;
1724 ch->r_io[ATA_SERROR].offset = ch_offset + 0x104;
1725 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x108;
1726 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x70;
1727 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x72;
1728 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x74;
1730 ch->flags |= ATA_NO_SLAVE;
1731 ata_generic_hw(dev);
1732 ch->hw.command = ata_intel_31244_command;
1734 /* enable PHY state change interrupt */
1735 ATA_OUTL(ctlr->r_res2, 0x4,
1736 ATA_INL(ctlr->r_res2, 0x04) | (0x01 << (ch->unit << 3)));
1741 ata_intel_31244_intr(void *data)
1743 struct ata_pci_controller *ctlr = data;
1744 struct ata_channel *ch;
1747 for (unit = 0; unit < ctlr->channels; unit++) {
1748 if (!(ch = ctlr->interrupt[unit].argument))
1751 /* check for PHY related interrupts on SATA capable HW */
1752 if (ctlr->chip->max_dma >= ATA_SA150) {
1753 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
1754 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
1755 struct ata_connect_task *tp;
1758 /* clear error bits/interrupt */
1759 ATA_IDX_OUTL(ch, ATA_SERROR, error);
1761 /* if we have a connection event deal with it */
1762 if ((error & ATA_SE_PHY_CHANGED) &&
1763 (tp = (struct ata_connect_task *)
1764 malloc(sizeof(struct ata_connect_task),
1765 M_ATA, M_NOWAIT | M_ZERO))) {
1767 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
1768 device_printf(ch->dev, "CONNECT requested\n");
1769 tp->action = ATA_C_ATTACH;
1772 device_printf(ch->dev, "DISCONNECT requested\n");
1773 tp->action = ATA_C_DETACH;
1776 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1777 taskqueue_enqueue(taskqueue_thread, &tp->task);
1782 /* any drive action to take care of ? */
1783 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
1784 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1786 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
1788 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1791 ctlr->interrupt[unit].function(ch);
1796 ata_intel_31244_reset(device_t dev)
1798 struct ata_channel *ch = device_get_softc(dev);
1800 ata_sata_phy_enable(ch);
1804 ata_intel_31244_command(struct ata_request *request)
1806 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
1807 struct ata_device *atadev = device_get_softc(request->dev);
1810 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
1811 return (ata_generic_command(request));
1813 lba = request->u.ata.lba;
1814 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | atadev->unit);
1815 /* enable interrupt */
1816 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
1817 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
1818 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
1819 ATA_IDX_OUTW(ch, ATA_SECTOR, ((lba >> 16) & 0xff00) | (lba & 0x00ff));
1820 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((lba >> 24) & 0xff00) |
1821 ((lba >> 8) & 0x00ff));
1822 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((lba >> 32) & 0xff00) |
1823 ((lba >> 16) & 0x00ff));
1825 /* issue command to controller */
1826 ATA_IDX_OUTB(ch, ATA_COMMAND, request->u.ata.command);
1832 ata_intel_intr(void *data)
1834 struct ata_pci_controller *ctlr = data;
1835 struct ata_channel *ch;
1838 for (unit = 0; unit < ctlr->channels; unit++) {
1839 if (!(ch = ctlr->interrupt[unit].argument))
1842 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1844 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1845 ATA_BMSTAT_INTERRUPT)
1847 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1850 ctlr->interrupt[unit].function(ch);
1855 ata_intel_reset(device_t dev)
1857 device_t parent = device_get_parent(dev);
1858 struct ata_pci_controller *ctlr = device_get_softc(parent);
1859 struct ata_channel *ch = device_get_softc(dev);
1862 /* ICH6 & ICH7 in compat mode has 4 SATA ports as master/slave on 2 ch's */
1863 if (ctlr->chip->cfg1) {
1864 mask = (0x0005 << ch->unit);
1867 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1868 if (pci_read_config(parent, 0x90, 1) & 0x04)
1871 mask = (0x0001 << ch->unit);
1872 /* XXX SOS should be in intel_allocate if we grow it */
1873 ch->flags |= ATA_NO_SLAVE;
1876 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1878 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1880 /* wait up to 1 sec for "connect well" */
1881 for (timeout = 0; timeout < 100 ; timeout++) {
1882 if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1883 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1887 ata_generic_reset(dev);
1891 ata_intel_old_setmode(device_t dev, int mode)
1897 ata_intel_new_setmode(device_t dev, int mode)
1899 device_t gparent = GRANDPARENT(dev);
1900 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1901 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1902 struct ata_device *atadev = device_get_softc(dev);
1903 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1904 u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
1905 u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
1906 u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
1907 u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
1908 u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
1909 u_int32_t mask40 = 0, new40 = 0;
1910 u_int8_t mask44 = 0, new44 = 0;
1912 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1913 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1915 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1917 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1918 ata_print_cable(dev, "controller");
1922 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1925 device_printf(dev, "%ssetting %s on %s chip\n",
1926 (error) ? "FAILURE " : "",
1927 ata_mode2str(mode), ctlr->chip->text);
1931 if (mode >= ATA_UDMA0) {
1932 pci_write_config(gparent, 0x48, reg48 | (0x0001 << devno), 2);
1933 pci_write_config(gparent, 0x4a,
1934 (reg4a & ~(0x3 << (devno << 2))) |
1935 ((0x01 + !(mode & 0x01)) << (devno << 2)), 2);
1938 pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
1939 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))), 2);
1942 if (mode >= ATA_UDMA2)
1943 pci_write_config(gparent, 0x54, reg54 | (0x1 << devno), 2);
1945 pci_write_config(gparent, 0x54, reg54 & ~(0x1 << devno), 2);
1947 if (mode >= ATA_UDMA5)
1948 pci_write_config(gparent, 0x54, reg54 | (0x1000 << devno), 2);
1950 pci_write_config(gparent, 0x54, reg54 & ~(0x1000 << devno), 2);
1952 reg40 &= ~0x00ff00ff;
1953 reg40 |= 0x40774077;
1955 if (atadev->unit == ATA_MASTER) {
1957 new40 = timings[ata_mode2idx(mode)] << 8;
1961 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
1962 (timings[ata_mode2idx(mode)] & 0x03);
1970 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
1971 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
1973 atadev->mode = mode;
1978 * Integrated Technology Express Inc. (ITE) chipset support functions
1981 ata_ite_ident(device_t dev)
1983 struct ata_pci_controller *ctlr = device_get_softc(dev);
1984 struct ata_chip_id *idx;
1985 static struct ata_chip_id ids[] =
1986 {{ ATA_IT8212F, 0x00, 0x00, 0x00, ATA_UDMA6, "ITE IT8212F" },
1987 { ATA_IT8211F, 0x00, 0x00, 0x00, ATA_UDMA6, "ITE IT8211F" },
1988 { 0, 0, 0, 0, 0, 0}};
1991 if (!(idx = ata_match_chip(dev, ids)))
1994 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1995 device_set_desc_copy(dev, buffer);
1997 ctlr->chipinit = ata_ite_chipinit;
2002 ata_ite_chipinit(device_t dev)
2004 struct ata_pci_controller *ctlr = device_get_softc(dev);
2006 if (ata_setup_interrupt(dev))
2009 ctlr->setmode = ata_ite_setmode;
2011 /* set PCI mode and 66Mhz reference clock */
2012 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
2014 /* set default active & recover timings */
2015 pci_write_config(dev, 0x54, 0x31, 1);
2016 pci_write_config(dev, 0x56, 0x31, 1);
2021 ata_ite_setmode(device_t dev, int mode)
2023 device_t gparent = GRANDPARENT(dev);
2024 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2025 struct ata_device *atadev = device_get_softc(dev);
2026 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2029 /* correct the mode for what the HW supports */
2030 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2032 /* check the CBLID bits for 80 conductor cable detection */
2033 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
2034 (ch->unit ? (1<<3) : (1<<2)))) {
2035 ata_print_cable(dev, "controller");
2039 /* set the wanted mode on the device */
2040 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2043 device_printf(dev, "%s setting %s on ITE8212F chip\n",
2044 (error) ? "failed" : "success", ata_mode2str(mode));
2046 /* if the device accepted the mode change, setup the HW accordingly */
2048 if (mode >= ATA_UDMA0) {
2049 u_int8_t udmatiming[] =
2050 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
2052 /* enable UDMA mode */
2053 pci_write_config(gparent, 0x50,
2054 pci_read_config(gparent, 0x50, 1) &
2055 ~(1 << (devno + 3)), 1);
2057 /* set UDMA timing */
2058 pci_write_config(gparent,
2059 0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
2060 udmatiming[mode & ATA_MODE_MASK], 1);
2063 u_int8_t chtiming[] =
2064 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
2066 /* disable UDMA mode */
2067 pci_write_config(gparent, 0x50,
2068 pci_read_config(gparent, 0x50, 1) |
2069 (1 << (devno + 3)), 1);
2071 /* set active and recover timing (shared between master & slave) */
2072 if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
2073 chtiming[ata_mode2idx(mode)])
2074 pci_write_config(gparent, 0x54 + (ch->unit << 2),
2075 chtiming[ata_mode2idx(mode)], 1);
2077 atadev->mode = mode;
2083 * National chipset support functions
2086 ata_national_ident(device_t dev)
2088 struct ata_pci_controller *ctlr = device_get_softc(dev);
2090 /* this chip is a clone of the Cyrix chip, bugs and all */
2091 if (pci_get_devid(dev) == ATA_SC1100) {
2092 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
2093 ctlr->chipinit = ata_national_chipinit;
2099 static device_t nat_host = NULL;
2102 ata_national_chipinit(device_t dev)
2104 struct ata_pci_controller *ctlr = device_get_softc(dev);
2108 if (ata_setup_interrupt(dev))
2111 /* locate the ISA part in the southbridge and enable UDMA33 */
2112 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
2113 for (i = 0; i < nchildren; i++) {
2114 if (pci_get_devid(children[i]) == 0x0510100b) {
2115 nat_host = children[i];
2119 free(children, M_TEMP);
2121 ctlr->setmode = ata_national_setmode;
2126 ata_national_setmode(device_t dev, int mode)
2128 device_t gparent = GRANDPARENT(dev);
2129 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2130 struct ata_device *atadev = device_get_softc(dev);
2131 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2132 u_int32_t piotiming[] =
2133 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
2134 0x00803020, 0x20102010, 0x00100010,
2135 0x00100010, 0x00100010, 0x00100010 };
2136 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
2137 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
2140 ch->dma->alignment = 16;
2141 ch->dma->max_iosize = 126 * DEV_BSIZE;
2143 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2145 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2148 device_printf(dev, "%s setting %s on National chip\n",
2149 (error) ? "failed" : "success", ata_mode2str(mode));
2151 if (mode >= ATA_UDMA0) {
2152 pci_write_config(gparent, 0x44 + (devno << 3),
2153 udmatiming[mode & ATA_MODE_MASK], 4);
2155 else if (mode >= ATA_WDMA0) {
2156 pci_write_config(gparent, 0x44 + (devno << 3),
2157 dmatiming[mode & ATA_MODE_MASK], 4);
2160 pci_write_config(gparent, 0x44 + (devno << 3),
2161 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
2164 pci_write_config(gparent, 0x40 + (devno << 3),
2165 piotiming[ata_mode2idx(mode)], 4);
2166 atadev->mode = mode;
2172 * nVidia chipset support functions
2175 ata_nvidia_ident(device_t dev)
2177 struct ata_pci_controller *ctlr = device_get_softc(dev);
2178 struct ata_chip_id *idx;
2179 static struct ata_chip_id ids[] =
2180 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nVidia nForce" },
2181 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2" },
2182 { ATA_NFORCE2_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2 MCP" },
2183 { ATA_NFORCE2_MCP_S1, 0, 0, 0, ATA_SA150, "nVidia nForce2 MCP" },
2184 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3" },
2185 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 Pro" },
2186 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
2187 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
2188 { ATA_NFORCE3_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 MCP" },
2189 { ATA_NFORCE3_MCP_S1, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
2190 { ATA_NFORCE3_MCP_S2, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
2191 { ATA_NFORCE4, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce4" },
2192 { ATA_NFORCE4_S1, 0, 0, NV4OFF, ATA_SA300, "nVidia nForce4" },
2193 { ATA_NFORCE4_S2, 0, 0, NV4OFF, ATA_SA300, "nVidia nForce4" },
2194 { 0, 0, 0, 0, 0, 0}};
2197 if (!(idx = ata_match_chip(dev, ids)))
2200 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
2201 device_set_desc_copy(dev, buffer);
2203 ctlr->chipinit = ata_nvidia_chipinit;
2208 ata_nvidia_chipinit(device_t dev)
2210 struct ata_pci_controller *ctlr = device_get_softc(dev);
2212 if (ata_setup_interrupt(dev))
2215 if (ctlr->chip->max_dma >= ATA_SA150) {
2216 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
2217 ctlr->r_type2 = SYS_RES_IOPORT;
2219 ctlr->r_type2 = SYS_RES_MEMORY;
2220 ctlr->r_rid2 = PCIR_BAR(5);
2221 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2222 &ctlr->r_rid2, RF_ACTIVE))) {
2223 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
2225 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
2226 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2227 ata_nvidia_intr, ctlr, &ctlr->handle)) {
2228 device_printf(dev, "unable to setup interrupt\n");
2232 /* enable control access */
2233 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
2235 /* clear interrupt status */
2236 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2238 /* enable device and PHY state change interrupts */
2239 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
2241 /* enable PCI interrupt */
2242 pci_write_config(dev, PCIR_COMMAND,
2243 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
2245 ctlr->allocate = ata_nvidia_allocate;
2246 ctlr->reset = ata_nvidia_reset;
2248 ctlr->setmode = ata_sata_setmode;
2251 /* disable prefetch, postwrite */
2252 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
2253 ctlr->setmode = ata_via_family_setmode;
2259 ata_nvidia_allocate(device_t dev)
2261 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2262 struct ata_channel *ch = device_get_softc(dev);
2264 /* setup the usual register normal pci style */
2265 if (ata_pci_allocate(dev))
2268 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2269 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
2270 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2271 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
2272 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2273 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
2274 ch->flags |= ATA_NO_SLAVE;
2280 ata_nvidia_intr(void *data)
2282 struct ata_pci_controller *ctlr = data;
2283 struct ata_channel *ch;
2284 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
2288 /* get interrupt status */
2289 status = ATA_INB(ctlr->r_res2, offset);
2291 /* clear interrupt status */
2292 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2294 for (unit = 0; unit < ctlr->channels; unit++) {
2295 if ((ch = ctlr->interrupt[unit].argument)) {
2296 struct ata_connect_task *tp;
2297 int maskshift = ch->unit << 2;
2299 /* clear error status */
2300 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2302 /* check for and handle connect events */
2303 if (((status & (0x0c << maskshift)) == (0x04 << maskshift)) &&
2304 (tp = (struct ata_connect_task *)
2305 malloc(sizeof(struct ata_connect_task),
2306 M_ATA, M_NOWAIT | M_ZERO))) {
2308 device_printf(ch->dev, "CONNECT requested\n");
2309 tp->action = ATA_C_ATTACH;
2311 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2312 taskqueue_enqueue(taskqueue_thread, &tp->task);
2315 /* check for and handle disconnect events */
2316 if ((status & (0x08 << maskshift)) &&
2317 (tp = (struct ata_connect_task *)
2318 malloc(sizeof(struct ata_connect_task),
2319 M_ATA, M_NOWAIT | M_ZERO))) {
2321 device_printf(ch->dev, "DISCONNECT requested\n");
2322 tp->action = ATA_C_DETACH;
2324 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2325 taskqueue_enqueue(taskqueue_thread, &tp->task);
2328 /* any drive action to take care of ? */
2329 if (status & (0x01 << maskshift)) {
2330 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2332 ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2334 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
2336 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat&~ATA_BMSTAT_ERROR);
2339 ctlr->interrupt[unit].function(ch);
2346 ata_nvidia_reset(device_t dev)
2348 struct ata_channel *ch = device_get_softc(dev);
2350 ata_sata_phy_enable(ch);
2355 * Promise chipset support functions
2357 #define ATA_PDC_APKT_OFFSET 0x00000010
2358 #define ATA_PDC_HPKT_OFFSET 0x00000040
2359 #define ATA_PDC_ASG_OFFSET 0x00000080
2360 #define ATA_PDC_LSG_OFFSET 0x000000c0
2361 #define ATA_PDC_HSG_OFFSET 0x00000100
2362 #define ATA_PDC_CHN_OFFSET 0x00000400
2363 #define ATA_PDC_BUF_BASE 0x00400000
2364 #define ATA_PDC_BUF_OFFSET 0x00100000
2365 #define ATA_PDC_MAX_HPKT 8
2366 #define ATA_PDC_WRITE_REG 0x00
2367 #define ATA_PDC_WRITE_CTL 0x0e
2368 #define ATA_PDC_WRITE_END 0x08
2369 #define ATA_PDC_WAIT_NBUSY 0x10
2370 #define ATA_PDC_WAIT_READY 0x18
2371 #define ATA_PDC_1B 0x20
2372 #define ATA_PDC_2B 0x40
2374 struct host_packet {
2376 TAILQ_ENTRY(host_packet) chain;
2379 struct ata_promise_sx4 {
2381 TAILQ_HEAD(, host_packet) queue;
2386 ata_promise_ident(device_t dev)
2388 struct ata_pci_controller *ctlr = device_get_softc(dev);
2389 struct ata_chip_id *idx;
2390 static struct ata_chip_id ids[] =
2391 {{ ATA_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "Promise PDC20246" },
2392 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20262" },
2393 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20263" },
2394 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20265" },
2395 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20267" },
2396 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20268" },
2397 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20269" },
2398 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20270" },
2399 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20271" },
2400 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20275" },
2401 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "Promise PDC20276" },
2402 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20277" },
2403 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20318" },
2404 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20319" },
2405 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20371" },
2406 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20375" },
2407 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20376" },
2408 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20377" },
2409 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20378" },
2410 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20379" },
2411 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20571" },
2412 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20575" },
2413 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20579" },
2414 { ATA_PDC20580, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20580" },
2415 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20617" },
2416 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20618" },
2417 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20619" },
2418 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20620" },
2419 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "Promise PDC20621" },
2420 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "Promise PDC20622" },
2421 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40518" },
2422 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40519" },
2423 { ATA_PDC40718, 0, PRMIO, PRSATA2, ATA_SA300, "Promise PDC40718" },
2424 { ATA_PDC40719, 0, PRMIO, PRSATA2, ATA_SA300, "Promise PDC40719" },
2425 { 0, 0, 0, 0, 0, 0}};
2427 uintptr_t devid = 0;
2429 if (!(idx = ata_match_chip(dev, ids)))
2432 /* if we are on a SuperTrak SX6000 dont attach */
2433 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
2434 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
2435 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
2436 devid == ATA_I960RM)
2439 strcpy(buffer, idx->text);
2441 /* if we are on a FastTrak TX4, adjust the interrupt resource */
2442 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
2443 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
2444 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
2445 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
2446 static long start = 0, end = 0;
2448 if (pci_get_slot(dev) == 1) {
2449 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
2450 strcat(buffer, " (channel 0+1)");
2452 else if (pci_get_slot(dev) == 2 && start && end) {
2453 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
2455 strcat(buffer, " (channel 2+3)");
2461 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
2462 device_set_desc_copy(dev, buffer);
2464 ctlr->chipinit = ata_promise_chipinit;
2469 ata_promise_chipinit(device_t dev)
2471 struct ata_pci_controller *ctlr = device_get_softc(dev);
2472 int rid = ATA_IRQ_RID;
2474 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2475 RF_SHAREABLE | RF_ACTIVE))) {
2476 device_printf(dev, "unable to map interrupt\n");
2480 if (ctlr->chip->max_dma >= ATA_SA150)
2481 ctlr->setmode = ata_sata_setmode;
2483 ctlr->setmode = ata_promise_setmode;
2485 switch (ctlr->chip->cfg1) {
2488 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
2490 ctlr->dmainit = ata_promise_new_dmainit;
2494 /* enable burst mode */
2495 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
2497 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2498 ata_promise_old_intr, ctlr, &ctlr->handle))) {
2499 device_printf(dev, "unable to setup interrupt\n");
2505 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2506 ata_promise_tx2_intr, ctlr, &ctlr->handle))) {
2507 device_printf(dev, "unable to setup interrupt\n");
2513 // if (ctlr->r_res1)
2514 // bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
2515 ctlr->r_type1 = SYS_RES_MEMORY;
2516 ctlr->r_rid1 = PCIR_BAR(4);
2517 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
2518 &ctlr->r_rid1, RF_ACTIVE)))
2521 ctlr->r_type2 = SYS_RES_MEMORY;
2522 ctlr->r_rid2 = PCIR_BAR(3);
2523 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2524 &ctlr->r_rid2, RF_ACTIVE))){
2525 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
2528 ctlr->reset = ata_promise_mio_reset;
2529 ctlr->dmainit = ata_promise_mio_dmainit;
2530 ctlr->allocate = ata_promise_mio_allocate;
2532 if (ctlr->chip->cfg2 == PRSX4X) {
2533 struct ata_promise_sx4 *hpkt;
2534 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
2536 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2537 ata_promise_sx4_intr, ctlr, &ctlr->handle))) {
2538 device_printf(dev, "unable to setup interrupt\n");
2539 /* XXX SOS release resources */
2543 /* print info about cache memory */
2544 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
2545 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
2546 ((dimm >> 24) & 0xff),
2547 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
2548 " ECC enabled" : "" );
2550 /* adjust cache memory parameters */
2551 ATA_OUTL(ctlr->r_res2, 0x000c000c,
2552 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
2554 /* setup host packet controls */
2555 hpkt = malloc(sizeof(struct ata_promise_sx4),
2556 M_TEMP, M_NOWAIT | M_ZERO);
2557 mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
2558 TAILQ_INIT(&hpkt->queue);
2559 hpkt->busy = 0; //hpkt->head = hpkt->tail = 0;
2560 device_set_ivars(dev, hpkt);
2565 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2566 ata_promise_mio_intr, ctlr, &ctlr->handle))) {
2567 device_printf(dev, "unable to setup interrupt\n");
2568 /* XXX SOS release resources */
2572 switch (ctlr->chip->cfg2) {
2574 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
2575 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
2579 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
2580 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 3;
2584 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
2589 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
2594 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
2599 /* XXX SOS release resources */
2603 /* XXX SOS release resources */
2610 ata_promise_mio_allocate(device_t dev)
2612 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2613 struct ata_channel *ch = device_get_softc(dev);
2614 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
2617 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2618 ch->r_io[i].res = ctlr->r_res2;
2619 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
2621 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
2622 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
2623 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
2624 ata_default_registers(dev);
2625 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
2626 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
2627 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2628 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
2629 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2630 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
2631 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2632 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
2633 ch->flags |= ATA_NO_SLAVE;
2635 ch->flags |= ATA_USE_16BIT;
2637 ata_generic_hw(dev);
2639 ch->hw.command = ata_promise_sx4_command;
2641 ch->hw.command = ata_promise_mio_command;
2646 ata_promise_mio_intr(void *data)
2648 struct ata_pci_controller *ctlr = data;
2649 struct ata_channel *ch;
2650 u_int32_t vector = 0, status = 0;
2653 switch (ctlr->chip->cfg2) {
2656 /* read and acknowledge interrupt(s) */
2657 vector = ATA_INL(ctlr->r_res2, 0x040);
2659 /* read and clear interface status */
2660 status = ATA_INL(ctlr->r_res2, 0x06c);
2661 ATA_OUTL(ctlr->r_res2, 0x06c, status & 0x000000ff);
2666 /* read and acknowledge interrupt(s) */
2667 vector = ATA_INL(ctlr->r_res2, 0x040);
2668 ATA_OUTL(ctlr->r_res2, 0x040, vector & 0x0000ffff);
2670 /* read and clear interface status */
2671 status = ATA_INL(ctlr->r_res2, 0x060);
2672 ATA_OUTL(ctlr->r_res2, 0x060, status & 0x000000ff);
2676 for (unit = 0; unit < ctlr->channels; unit++) {
2678 if ((ch = ctlr->interrupt[unit].argument)) {
2679 struct ata_connect_task *tp;
2681 /* check for and handle disconnect events */
2682 if ((status & (0x00000001 << unit)) &&
2683 (tp = (struct ata_connect_task *)
2684 malloc(sizeof(struct ata_connect_task),
2685 M_ATA, M_NOWAIT | M_ZERO))) {
2688 device_printf(ch->dev, "DISCONNECT requested\n");
2689 tp->action = ATA_C_DETACH;
2691 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2692 taskqueue_enqueue(taskqueue_thread, &tp->task);
2695 /* check for and handle connect events */
2696 if ((status & (0x00000010 << unit)) &&
2697 (tp = (struct ata_connect_task *)
2698 malloc(sizeof(struct ata_connect_task),
2699 M_ATA, M_NOWAIT | M_ZERO))) {
2702 device_printf(ch->dev, "CONNECT requested\n");
2703 tp->action = ATA_C_ATTACH;
2705 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2706 taskqueue_enqueue(taskqueue_thread, &tp->task);
2709 /* active interrupt(s) need to call the interrupt handler */
2710 if (vector & (1 << (unit + 1)))
2711 if ((ch = ctlr->interrupt[unit].argument))
2712 ctlr->interrupt[unit].function(ch);
2718 ata_promise_sx4_intr(void *data)
2720 struct ata_pci_controller *ctlr = data;
2721 struct ata_channel *ch;
2722 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
2725 for (unit = 0; unit < ctlr->channels; unit++) {
2726 if (vector & (1 << (unit + 1)))
2727 if ((ch = ctlr->interrupt[unit].argument))
2728 ctlr->interrupt[unit].function(ch);
2729 if (vector & (1 << (unit + 5)))
2730 if ((ch = ctlr->interrupt[unit].argument))
2731 ata_promise_queue_hpkt(ctlr,
2732 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
2733 ATA_PDC_HPKT_OFFSET));
2734 if (vector & (1 << (unit + 9))) {
2735 ata_promise_next_hpkt(ctlr);
2736 if ((ch = ctlr->interrupt[unit].argument))
2737 ctlr->interrupt[unit].function(ch);
2739 if (vector & (1 << (unit + 13))) {
2740 ata_promise_next_hpkt(ctlr);
2741 if ((ch = ctlr->interrupt[unit].argument))
2742 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2743 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
2744 ATA_PDC_APKT_OFFSET));
2750 ata_promise_mio_dmainit(device_t dev)
2752 struct ata_channel *ch = device_get_softc(dev);
2756 /* note start and stop are not used here */
2761 ata_promise_mio_reset(device_t dev)
2763 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2764 struct ata_channel *ch = device_get_softc(dev);
2765 struct ata_promise_sx4 *hpktp;
2767 switch (ctlr->chip->cfg2) {
2770 /* softreset channel ATA module */
2771 hpktp = device_get_ivars(ctlr->dev);
2772 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
2774 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
2775 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
2776 ~0x00003f9f) | (ch->unit + 1));
2778 /* softreset HOST module */ /* XXX SOS what about other outstandings */
2779 mtx_lock(&hpktp->mtx);
2780 ATA_OUTL(ctlr->r_res2, 0xc012c,
2781 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
2783 ATA_OUTL(ctlr->r_res2, 0xc012c,
2784 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
2786 mtx_unlock(&hpktp->mtx);
2787 ata_generic_reset(dev);
2792 if ((ctlr->chip->cfg2 == PRSATA) ||
2793 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2795 /* mask plug/unplug intr */
2796 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
2799 /* softreset channels ATA module */
2800 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2802 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2803 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2804 ~0x00003f9f) | (ch->unit + 1));
2806 if ((ctlr->chip->cfg2 == PRSATA) ||
2807 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2809 ata_sata_phy_enable(ch);
2811 /* reset and enable plug/unplug intr */
2812 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
2815 ata_generic_reset(dev);
2820 if ((ctlr->chip->cfg2 == PRSATA2) ||
2821 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2822 /* set portmultiplier port */
2823 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
2825 /* mask plug/unplug intr */
2826 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
2829 /* softreset channels ATA module */
2830 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2832 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2833 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2834 ~0x00003f9f) | (ch->unit + 1));
2836 if ((ctlr->chip->cfg2 == PRSATA2) ||
2837 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2839 /* set PHY mode to "improved" */
2840 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
2841 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
2842 ~0x00000003) | 0x00000001);
2844 ata_sata_phy_enable(ch);
2846 /* reset and enable plug/unplug intr */
2847 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
2849 /* set portmultiplier port */
2850 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
2853 ata_generic_reset(dev);
2860 ata_promise_mio_command(struct ata_request *request)
2862 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2863 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2864 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
2866 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
2868 /* XXX SOS add ATAPI commands support later */
2869 switch (request->u.ata.command) {
2871 return ata_generic_command(request);
2874 case ATA_READ_DMA48:
2875 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
2879 case ATA_WRITE_DMA48:
2880 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
2883 wordp[1] = htole32(ch->dma->sg_bus);
2885 ata_promise_apkt((u_int8_t*)wordp, request);
2887 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
2892 ata_promise_sx4_command(struct ata_request *request)
2894 device_t gparent = GRANDPARENT(request->dev);
2895 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2896 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2897 struct ata_dma_prdentry *prd = ch->dma->sg;
2898 caddr_t window = rman_get_virtual(ctlr->r_res1);
2900 int i, idx, length = 0;
2902 /* XXX SOS add ATAPI commands support later */
2903 switch (request->u.ata.command) {
2908 case ATA_ATA_IDENTIFY:
2912 case ATA_READ_MUL48:
2916 case ATA_WRITE_MUL48:
2917 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2918 return ata_generic_command(request);
2920 case ATA_SETFEATURES:
2921 case ATA_FLUSHCACHE:
2922 case ATA_FLUSHCACHE48:
2925 wordp = (u_int32_t *)
2926 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2927 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
2930 ata_promise_apkt((u_int8_t *)wordp, request);
2931 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2932 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2933 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2934 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
2938 case ATA_READ_DMA48:
2940 case ATA_WRITE_DMA48:
2941 wordp = (u_int32_t *)
2942 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
2945 wordp[idx++] = prd[i].addr;
2946 wordp[idx++] = prd[i].count;
2947 length += (prd[i].count & ~ATA_DMA_EOT);
2948 } while (!(prd[i++].count & ATA_DMA_EOT));
2950 wordp = (u_int32_t *)
2951 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
2952 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2953 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
2955 wordp = (u_int32_t *)
2956 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
2957 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2958 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
2960 wordp = (u_int32_t *)
2961 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
2962 if (request->flags & ATA_R_READ)
2963 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
2964 if (request->flags & ATA_R_WRITE)
2965 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
2966 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
2967 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
2970 wordp = (u_int32_t *)
2971 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2972 if (request->flags & ATA_R_READ)
2973 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
2974 if (request->flags & ATA_R_WRITE)
2975 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
2976 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
2978 ata_promise_apkt((u_int8_t *)wordp, request);
2979 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2981 if (request->flags & ATA_R_READ) {
2982 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
2983 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
2984 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2985 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
2987 if (request->flags & ATA_R_WRITE) {
2988 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
2989 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
2990 ata_promise_queue_hpkt(ctlr,
2991 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
2998 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
3000 struct ata_device *atadev = device_get_softc(request->dev);
3003 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
3004 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
3005 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
3006 bytep[i++] = ATA_A_4BIT;
3008 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3009 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
3010 bytep[i++] = request->u.ata.feature >> 8;
3011 bytep[i++] = request->u.ata.feature;
3012 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
3013 bytep[i++] = request->u.ata.count >> 8;
3014 bytep[i++] = request->u.ata.count;
3015 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
3016 bytep[i++] = request->u.ata.lba >> 24;
3017 bytep[i++] = request->u.ata.lba;
3018 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
3019 bytep[i++] = request->u.ata.lba >> 32;
3020 bytep[i++] = request->u.ata.lba >> 8;
3021 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
3022 bytep[i++] = request->u.ata.lba >> 40;
3023 bytep[i++] = request->u.ata.lba >> 16;
3024 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
3025 bytep[i++] = ATA_D_LBA | atadev->unit;
3028 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
3029 bytep[i++] = request->u.ata.feature;
3030 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
3031 bytep[i++] = request->u.ata.count;
3032 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
3033 bytep[i++] = request->u.ata.lba;
3034 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
3035 bytep[i++] = request->u.ata.lba >> 8;
3036 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
3037 bytep[i++] = request->u.ata.lba >> 16;
3038 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
3039 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
3040 ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
3042 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
3043 bytep[i++] = request->u.ata.command;
3048 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
3050 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
3052 mtx_lock(&hpktp->mtx);
3054 struct host_packet *hp =
3055 malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
3057 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
3061 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
3063 mtx_unlock(&hpktp->mtx);
3067 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
3069 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
3070 struct host_packet *hp;
3072 mtx_lock(&hpktp->mtx);
3073 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
3074 TAILQ_REMOVE(&hpktp->queue, hp, chain);
3075 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
3080 mtx_unlock(&hpktp->mtx);
3084 ata_promise_tx2_intr(void *data)
3086 struct ata_pci_controller *ctlr = data;
3087 struct ata_channel *ch;
3090 for (unit = 0; unit < ctlr->channels; unit++) {
3091 if (!(ch = ctlr->interrupt[unit].argument))
3093 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3094 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
3095 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3096 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3098 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3099 ATA_BMSTAT_INTERRUPT)
3101 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3104 ctlr->interrupt[unit].function(ch);
3110 ata_promise_old_intr(void *data)
3112 struct ata_pci_controller *ctlr = data;
3113 struct ata_channel *ch;
3116 for (unit = 0; unit < ctlr->channels; unit++) {
3117 if (!(ch = ctlr->interrupt[unit].argument))
3119 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)){
3120 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3121 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3123 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3124 ATA_BMSTAT_INTERRUPT)
3126 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3129 ctlr->interrupt[unit].function(ch);
3135 ata_promise_new_dmastart(device_t dev)
3137 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3138 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3139 struct ata_device *atadev = device_get_softc(dev);
3141 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3142 ATA_OUTB(ctlr->r_res1, 0x11,
3143 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
3144 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20,
3145 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
3146 (ch->dma->cur_iosize >> 1));
3148 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
3149 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
3150 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
3151 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3152 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
3153 ATA_BMCMD_START_STOP);
3154 ch->flags |= ATA_DMA_ACTIVE;
3159 ata_promise_new_dmastop(device_t dev)
3161 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3162 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3163 struct ata_device *atadev = device_get_softc(dev);
3166 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3167 ATA_OUTB(ctlr->r_res1, 0x11,
3168 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
3169 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20, 0);
3171 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
3172 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3173 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3174 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3175 ch->flags &= ~ATA_DMA_ACTIVE;
3180 ata_promise_new_dmareset(device_t dev)
3182 struct ata_channel *ch = device_get_softc(dev);
3184 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3185 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3186 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3187 ch->flags &= ~ATA_DMA_ACTIVE;
3191 ata_promise_new_dmainit(device_t dev)
3193 struct ata_channel *ch = device_get_softc(dev);
3197 ch->dma->start = ata_promise_new_dmastart;
3198 ch->dma->stop = ata_promise_new_dmastop;
3199 ch->dma->reset = ata_promise_new_dmareset;
3204 ata_promise_setmode(device_t dev, int mode)
3206 device_t gparent = GRANDPARENT(dev);
3207 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3208 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3209 struct ata_device *atadev = device_get_softc(dev);
3210 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3212 u_int32_t timings33[][2] = {
3213 /* PROLD PRNEW mode */
3214 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
3215 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
3216 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
3217 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
3218 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
3219 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
3220 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
3221 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
3222 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
3223 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
3224 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
3225 { 0, 0x00424ef6 }, /* UDMA 3 */
3226 { 0, 0x004127f3 }, /* UDMA 4 */
3227 { 0, 0x004127f3 } /* UDMA 5 */
3230 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3232 switch (ctlr->chip->cfg1) {
3235 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
3236 (ch->unit ? 1 << 11 : 1 << 10))) {
3237 ata_print_cable(dev, "controller");
3240 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
3241 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
3245 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3246 if (mode > ATA_UDMA2 &&
3247 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
3248 ata_print_cable(dev, "controller");
3254 if (mode > ATA_UDMA2 &&
3255 (ATA_INL(ctlr->r_res2,
3256 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
3257 (ch->unit << 7)) & 0x01000000)) {
3258 ata_print_cable(dev, "controller");
3264 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3267 device_printf(dev, "%ssetting %s on %s chip\n",
3268 (error) ? "FAILURE " : "",
3269 ata_mode2str(mode), ctlr->chip->text);
3271 if (ctlr->chip->cfg1 < PRTX)
3272 pci_write_config(gparent, 0x60 + (devno << 2),
3273 timings33[ctlr->chip->cfg1][ata_mode2idx(mode)],4);
3274 atadev->mode = mode;
3281 * ServerWorks chipset support functions
3284 ata_serverworks_ident(device_t dev)
3286 struct ata_pci_controller *ctlr = device_get_softc(dev);
3287 struct ata_chip_id *idx;
3288 static struct ata_chip_id ids[] =
3289 {{ ATA_ROSB4, 0x00, SWKS33, 0x00, ATA_UDMA2, "ServerWorks ROSB4" },
3290 { ATA_CSB5, 0x92, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB5" },
3291 { ATA_CSB5, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB5" },
3292 { ATA_CSB6, 0x00, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB6" },
3293 { ATA_CSB6_1, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB6" },
3294 { 0, 0, 0, 0, 0, 0}};
3297 if (!(idx = ata_match_chip(dev, ids)))
3300 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3301 device_set_desc_copy(dev, buffer);
3303 ctlr->chipinit = ata_serverworks_chipinit;
3308 ata_serverworks_chipinit(device_t dev)
3310 struct ata_pci_controller *ctlr = device_get_softc(dev);
3312 if (ata_setup_interrupt(dev))
3315 if (ctlr->chip->cfg1 == SWKS33) {
3319 /* locate the ISA part in the southbridge and enable UDMA33 */
3320 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
3321 for (i = 0; i < nchildren; i++) {
3322 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
3323 pci_write_config(children[i], 0x64,
3324 (pci_read_config(children[i], 0x64, 4) &
3325 ~0x00002000) | 0x00004000, 4);
3329 free(children, M_TEMP);
3333 pci_write_config(dev, 0x5a,
3334 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
3335 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
3337 ctlr->setmode = ata_serverworks_setmode;
3342 ata_serverworks_setmode(device_t dev, int mode)
3344 device_t gparent = GRANDPARENT(dev);
3345 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3346 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3347 struct ata_device *atadev = device_get_softc(dev);
3348 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3349 int offset = (devno ^ 0x01) << 3;
3351 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
3352 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
3353 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
3355 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3357 mode = ata_check_80pin(dev, mode);
3359 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3362 device_printf(dev, "%ssetting %s on %s chip\n",
3363 (error) ? "FAILURE " : "",
3364 ata_mode2str(mode), ctlr->chip->text);
3366 if (mode >= ATA_UDMA0) {
3367 pci_write_config(gparent, 0x56,
3368 (pci_read_config(gparent, 0x56, 2) &
3369 ~(0xf << (devno << 2))) |
3370 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
3371 pci_write_config(gparent, 0x54,
3372 pci_read_config(gparent, 0x54, 1) |
3373 (0x01 << devno), 1);
3374 pci_write_config(gparent, 0x44,
3375 (pci_read_config(gparent, 0x44, 4) &
3376 ~(0xff << offset)) |
3377 (dmatimings[2] << offset), 4);
3379 else if (mode >= ATA_WDMA0) {
3380 pci_write_config(gparent, 0x54,
3381 pci_read_config(gparent, 0x54, 1) &
3382 ~(0x01 << devno), 1);
3383 pci_write_config(gparent, 0x44,
3384 (pci_read_config(gparent, 0x44, 4) &
3385 ~(0xff << offset)) |
3386 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
3389 pci_write_config(gparent, 0x54,
3390 pci_read_config(gparent, 0x54, 1) &
3391 ~(0x01 << devno), 1);
3393 pci_write_config(gparent, 0x40,
3394 (pci_read_config(gparent, 0x40, 4) &
3395 ~(0xff << offset)) |
3396 (piotimings[ata_mode2idx(mode)] << offset), 4);
3397 atadev->mode = mode;
3403 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
3406 ata_sii_ident(device_t dev)
3408 struct ata_pci_controller *ctlr = device_get_softc(dev);
3409 struct ata_chip_id *idx;
3410 static struct ata_chip_id ids[] =
3411 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
3412 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
3413 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
3414 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
3415 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
3416 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
3417 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
3418 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
3419 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
3420 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
3421 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
3422 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
3423 { 0, 0, 0, 0, 0, 0}};
3426 if (!(idx = ata_match_chip(dev, ids)))
3429 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3430 device_set_desc_copy(dev, buffer);
3432 ctlr->chipinit = ata_sii_chipinit;
3437 ata_sii_chipinit(device_t dev)
3439 struct ata_pci_controller *ctlr = device_get_softc(dev);
3440 int rid = ATA_IRQ_RID;
3442 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3443 RF_SHAREABLE | RF_ACTIVE))) {
3444 device_printf(dev, "unable to map interrupt\n");
3448 if (ctlr->chip->cfg1 == SIIMEMIO) {
3449 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3450 ata_sii_intr, ctlr, &ctlr->handle))) {
3451 device_printf(dev, "unable to setup interrupt\n");
3455 ctlr->r_type2 = SYS_RES_MEMORY;
3456 ctlr->r_rid2 = PCIR_BAR(5);
3457 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3458 &ctlr->r_rid2, RF_ACTIVE)))
3461 if (ctlr->chip->cfg2 & SIISETCLK) {
3462 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
3463 pci_write_config(dev, 0x8a,
3464 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
3465 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
3466 device_printf(dev, "%s could not set ATA133 clock\n",
3470 /* if we have 4 channels enable the second set */
3471 if (ctlr->chip->cfg2 & SII4CH) {
3472 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
3476 /* enable PCI interrupt as BIOS might not */
3477 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
3479 /* dont block interrupts from any channel */
3480 pci_write_config(dev, 0x48,
3481 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
3483 ctlr->allocate = ata_sii_allocate;
3484 if (ctlr->chip->max_dma >= ATA_SA150) {
3485 ctlr->reset = ata_sii_reset;
3486 ctlr->setmode = ata_sata_setmode;
3489 ctlr->setmode = ata_sii_setmode;
3492 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3493 ctlr->chip->cfg2 & SIIINTR ?
3494 ata_cmd_intr : ata_cmd_old_intr,
3495 ctlr, &ctlr->handle))) {
3496 device_printf(dev, "unable to setup interrupt\n");
3500 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
3501 device_printf(dev, "HW has secondary channel disabled\n");
3505 /* enable interrupt as BIOS might not */
3506 pci_write_config(dev, 0x71, 0x01, 1);
3508 ctlr->setmode = ata_cmd_setmode;
3514 ata_sii_allocate(device_t dev)
3516 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3517 struct ata_channel *ch = device_get_softc(dev);
3518 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
3521 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
3522 ch->r_io[i].res = ctlr->r_res2;
3523 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
3525 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
3526 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
3527 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
3528 ata_default_registers(dev);
3529 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
3530 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
3531 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
3532 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
3533 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
3534 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
3535 ch->r_io[ATA_BMDEVSPEC_0].res = ctlr->r_res2;
3536 ch->r_io[ATA_BMDEVSPEC_0].offset = 0xa1 + (unit01 << 6) + (unit10 << 8);
3538 if (ctlr->chip->max_dma >= ATA_SA150) {
3539 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3540 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
3541 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3542 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
3543 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3544 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
3545 ch->flags |= ATA_NO_SLAVE;
3547 /* enable PHY state change interrupt */
3548 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
3551 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
3552 /* work around errata in early chips */
3553 ch->dma->boundary = 16 * DEV_BSIZE;
3554 ch->dma->segsize = 15 * DEV_BSIZE;
3557 ata_generic_hw(dev);
3562 ata_sii_intr(void *data)
3564 struct ata_pci_controller *ctlr = data;
3565 struct ata_channel *ch;
3568 for (unit = 0; unit < ctlr->channels; unit++) {
3569 if (!(ch = ctlr->interrupt[unit].argument))
3572 /* check for PHY related interrupts on SATA capable HW */
3573 if (ctlr->chip->max_dma >= ATA_SA150) {
3574 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
3575 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
3576 struct ata_connect_task *tp;
3579 /* clear error bits/interrupt */
3580 ATA_IDX_OUTL(ch, ATA_SERROR, error);
3582 /* if we have a connection event deal with it */
3583 if ((error & ATA_SE_PHY_CHANGED) &&
3584 (tp = (struct ata_connect_task *)
3585 malloc(sizeof(struct ata_connect_task),
3586 M_ATA, M_NOWAIT | M_ZERO))) {
3588 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
3589 device_printf(ch->dev, "CONNECT requested\n");
3590 tp->action = ATA_C_ATTACH;
3593 device_printf(ch->dev, "DISCONNECT requested\n");
3594 tp->action = ATA_C_DETACH;
3597 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3598 taskqueue_enqueue(taskqueue_thread, &tp->task);
3603 /* any drive action to take care of ? */
3604 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_0) & 0x08) {
3605 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3606 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3608 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
3610 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3613 ctlr->interrupt[unit].function(ch);
3620 ata_cmd_intr(void *data)
3622 struct ata_pci_controller *ctlr = data;
3623 struct ata_channel *ch;
3627 for (unit = 0; unit < ctlr->channels; unit++) {
3628 if (!(ch = ctlr->interrupt[unit].argument))
3630 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
3631 (ch->unit ? 0x08 : 0x04))) {
3632 pci_write_config(device_get_parent(ch->dev), 0x71,
3633 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
3634 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3635 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3637 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3638 ATA_BMSTAT_INTERRUPT)
3640 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3643 ctlr->interrupt[unit].function(ch);
3649 ata_cmd_old_intr(void *data)
3651 struct ata_pci_controller *ctlr = data;
3652 struct ata_channel *ch;
3655 for (unit = 0; unit < ctlr->channels; unit++) {
3656 if (!(ch = ctlr->interrupt[unit].argument))
3658 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3659 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3661 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3662 ATA_BMSTAT_INTERRUPT)
3664 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3667 ctlr->interrupt[unit].function(ch);
3672 ata_sii_reset(device_t dev)
3674 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3675 struct ata_channel *ch = device_get_softc(dev);
3676 int offset = ((ch->unit & 1) << 7) + ((ch->unit & 2) << 8);
3678 /* disable PHY state change interrupt */
3679 ATA_OUTL(ctlr->r_res2, 0x148 + offset, ~(1 << 16));
3681 /* reset controller part for this channel */
3682 ATA_OUTL(ctlr->r_res2, 0x48,
3683 ATA_INL(ctlr->r_res2, 0x48) | (0xc0 >> ch->unit));
3685 ATA_OUTL(ctlr->r_res2, 0x48,
3686 ATA_INL(ctlr->r_res2, 0x48) & ~(0xc0 >> ch->unit));
3688 ata_sata_phy_enable(ch);
3690 /* enable PHY state change interrupt */
3691 ATA_OUTL(ctlr->r_res2, 0x148 + offset, (1 << 16));
3695 ata_sii_setmode(device_t dev, int mode)
3697 device_t gparent = GRANDPARENT(dev);
3698 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3699 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3700 struct ata_device *atadev = device_get_softc(dev);
3701 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
3702 int mreg = ch->unit ? 0x84 : 0x80;
3703 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
3704 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
3707 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3709 if (ctlr->chip->cfg2 & SIISETCLK) {
3710 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
3711 (ch->unit ? 0x02 : 0x01))) {
3712 ata_print_cable(dev, "controller");
3717 mode = ata_check_80pin(dev, mode);
3719 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3722 device_printf(dev, "%ssetting %s on %s chip\n",
3723 (error) ? "FAILURE " : "",
3724 ata_mode2str(mode), ctlr->chip->text);
3728 if (mode >= ATA_UDMA0) {
3729 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
3730 u_int8_t ureg = 0xac + rego;
3732 pci_write_config(gparent, mreg,
3733 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
3734 pci_write_config(gparent, ureg,
3735 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
3736 udmatimings[mode & ATA_MODE_MASK], 1);
3739 else if (mode >= ATA_WDMA0) {
3740 u_int8_t dreg = 0xa8 + rego;
3741 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
3743 pci_write_config(gparent, mreg,
3744 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
3745 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
3749 u_int8_t preg = 0xa4 + rego;
3750 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
3752 pci_write_config(gparent, mreg,
3753 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
3754 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
3756 atadev->mode = mode;
3760 ata_cmd_setmode(device_t dev, int mode)
3762 device_t gparent = GRANDPARENT(dev);
3763 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3764 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3765 struct ata_device *atadev = device_get_softc(dev);
3766 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3769 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3771 mode = ata_check_80pin(dev, mode);
3773 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3776 device_printf(dev, "%ssetting %s on %s chip\n",
3777 (error) ? "FAILURE " : "",
3778 ata_mode2str(mode), ctlr->chip->text);
3780 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
3781 int ureg = ch->unit ? 0x7b : 0x73;
3783 if (mode >= ATA_UDMA0) {
3784 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
3785 { 0x11, 0x42 }, { 0x25, 0x8a },
3786 { 0x15, 0x4a }, { 0x05, 0x0a } };
3788 u_int8_t umode = pci_read_config(gparent, ureg, 1);
3790 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
3791 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
3792 pci_write_config(gparent, ureg, umode, 1);
3794 else if (mode >= ATA_WDMA0) {
3795 int dmatimings[] = { 0x87, 0x32, 0x3f };
3797 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
3798 pci_write_config(gparent, ureg,
3799 pci_read_config(gparent, ureg, 1) &
3800 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3803 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
3804 pci_write_config(gparent, treg,
3805 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
3806 pci_write_config(gparent, ureg,
3807 pci_read_config(gparent, ureg, 1) &
3808 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3810 atadev->mode = mode;
3816 * Silicon Integrated Systems Corp. (SiS) chipset support functions
3819 ata_sis_ident(device_t dev)
3821 struct ata_pci_controller *ctlr = device_get_softc(dev);
3822 struct ata_chip_id *idx;
3823 static struct ata_chip_id ids[] =
3824 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "SiS 182" }, /* south */
3825 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "SiS 181" }, /* south */
3826 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "SiS 180" }, /* south */
3827 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 965" }, /* south */
3828 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 964" }, /* south */
3829 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 963" }, /* south */
3830 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 962" }, /* south */
3832 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 745" }, /* 1chip */
3833 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 735" }, /* 1chip */
3834 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 733" }, /* 1chip */
3835 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "SiS 730" }, /* 1chip */
3837 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 635" }, /* 1chip */
3838 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 633" }, /* unknown */
3839 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "SiS 630S"}, /* 1chip */
3840 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "SiS 630" }, /* 1chip */
3841 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "SiS 620" }, /* 1chip */
3843 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "SiS 550" },
3844 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "SiS 540" },
3845 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "SiS 530" },
3847 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "SiS 5513" },
3848 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "SiS 5513" },
3849 { 0, 0, 0, 0, 0, 0 }};
3853 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
3856 if (idx->cfg2 && !found) {
3857 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
3859 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
3860 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
3862 idx->cfg1 = SIS133NEW;
3863 idx->max_dma = ATA_UDMA6;
3864 sprintf(buffer, "SiS 962/963 %s controller",
3865 ata_mode2str(idx->max_dma));
3867 pci_write_config(dev, 0x57, reg57, 1);
3869 if (idx->cfg2 && !found) {
3870 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
3872 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
3873 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
3874 struct ata_chip_id id[] =
3875 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
3878 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
3879 idx->cfg1 = SIS133OLD;
3880 idx->max_dma = ATA_UDMA6;
3883 idx->cfg1 = SIS100NEW;
3884 idx->max_dma = ATA_UDMA5;
3886 sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
3888 pci_write_config(dev, 0x4a, reg4a, 1);
3891 sprintf(buffer,"%s %s controller",idx->text,ata_mode2str(idx->max_dma));
3893 device_set_desc_copy(dev, buffer);
3895 ctlr->chipinit = ata_sis_chipinit;
3900 ata_sis_chipinit(device_t dev)
3902 struct ata_pci_controller *ctlr = device_get_softc(dev);
3904 if (ata_setup_interrupt(dev))
3907 switch (ctlr->chip->cfg1) {
3912 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
3916 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
3919 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
3920 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
3923 ctlr->r_type2 = SYS_RES_IOPORT;
3924 ctlr->r_rid2 = PCIR_BAR(5);
3925 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3926 &ctlr->r_rid2, RF_ACTIVE))) {
3927 pci_write_config(dev, PCIR_COMMAND,
3928 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3929 ctlr->allocate = ata_sis_allocate;
3930 ctlr->reset = ata_sis_reset;
3932 ctlr->setmode = ata_sata_setmode;
3937 ctlr->setmode = ata_sis_setmode;
3942 ata_sis_allocate(device_t dev)
3944 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3945 struct ata_channel *ch = device_get_softc(dev);
3946 int offset = ch->unit << ((ctlr->chip->chipid == ATA_SIS182) ? 5 : 6);
3948 /* setup the usual register normal pci style */
3949 if (ata_pci_allocate(dev))
3952 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3953 ch->r_io[ATA_SSTATUS].offset = 0x00 + offset;
3954 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3955 ch->r_io[ATA_SERROR].offset = 0x04 + offset;
3956 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3957 ch->r_io[ATA_SCONTROL].offset = 0x08 + offset;
3958 ch->flags |= ATA_NO_SLAVE;
3960 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
3961 /* XXX SOS unknown how to enable PHY state change interrupt */
3966 ata_sis_reset(device_t dev)
3968 struct ata_channel *ch = device_get_softc(dev);
3970 ata_sata_phy_enable(ch);
3975 ata_sis_setmode(device_t dev, int mode)
3977 device_t gparent = GRANDPARENT(dev);
3978 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3979 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3980 struct ata_device *atadev = device_get_softc(dev);
3981 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3984 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3986 if (ctlr->chip->cfg1 == SIS133NEW) {
3987 if (mode > ATA_UDMA2 &&
3988 pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
3989 ata_print_cable(dev, "controller");
3994 if (mode > ATA_UDMA2 &&
3995 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
3996 ata_print_cable(dev, "controller");
4001 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4004 device_printf(dev, "%ssetting %s on %s chip\n",
4005 (error) ? "FAILURE " : "",
4006 ata_mode2str(mode), ctlr->chip->text);
4008 switch (ctlr->chip->cfg1) {
4010 u_int32_t timings[] =
4011 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
4012 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
4013 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
4016 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
4017 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
4021 u_int16_t timings[] =
4022 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
4023 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
4025 u_int16_t reg = 0x40 + (devno << 1);
4027 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
4031 u_int16_t timings[] =
4032 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
4033 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
4034 u_int16_t reg = 0x40 + (devno << 1);
4036 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
4042 u_int16_t timings[] =
4043 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
4044 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
4045 u_int16_t reg = 0x40 + (devno << 1);
4047 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
4051 atadev->mode = mode;
4056 /* VIA Technologies Inc. chipset support functions */
4058 ata_via_ident(device_t dev)
4060 struct ata_pci_controller *ctlr = device_get_softc(dev);
4061 struct ata_chip_id *idx;
4062 static struct ata_chip_id ids[] =
4063 {{ ATA_VIA82C586, 0x02, VIA33, 0x00, ATA_UDMA2, "VIA 82C586B" },
4064 { ATA_VIA82C586, 0x00, VIA33, 0x00, ATA_WDMA2, "VIA 82C586" },
4065 { ATA_VIA82C596, 0x12, VIA66, VIACLK, ATA_UDMA4, "VIA 82C596B" },
4066 { ATA_VIA82C596, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C596" },
4067 { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "VIA 82C686B"},
4068 { ATA_VIA82C686, 0x10, VIA66, VIACLK, ATA_UDMA4, "VIA 82C686A" },
4069 { ATA_VIA82C686, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C686" },
4070 { ATA_VIA8231, 0x00, VIA100, VIABUG, ATA_UDMA5, "VIA 8231" },
4071 { ATA_VIA8233, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233" },
4072 { ATA_VIA8233C, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233C" },
4073 { ATA_VIA8233A, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8233A" },
4074 { ATA_VIA8235, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8235" },
4075 { ATA_VIA8237, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8237" },
4076 { 0, 0, 0, 0, 0, 0 }};
4077 static struct ata_chip_id new_ids[] =
4078 {{ ATA_VIA6410, 0x00, 0, 0x00, ATA_UDMA6, "VIA 6410" },
4079 { ATA_VIA6420, 0x00, 7, 0x00, ATA_SA150, "VIA 6420" },
4080 { ATA_VIA6421, 0x00, 6, VIABAR, ATA_SA150, "VIA 6421" },
4081 { 0, 0, 0, 0, 0, 0 }};
4084 if (pci_get_devid(dev) == ATA_VIA82C571) {
4085 if (!(idx = ata_find_chip(dev, ids, -99)))
4089 if (!(idx = ata_match_chip(dev, new_ids)))
4093 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
4094 device_set_desc_copy(dev, buffer);
4096 ctlr->chipinit = ata_via_chipinit;
4101 ata_via_chipinit(device_t dev)
4103 struct ata_pci_controller *ctlr = device_get_softc(dev);
4105 if (ata_setup_interrupt(dev))
4108 if (ctlr->chip->max_dma >= ATA_SA150) {
4109 ctlr->r_type2 = SYS_RES_IOPORT;
4110 ctlr->r_rid2 = PCIR_BAR(5);
4111 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4112 &ctlr->r_rid2, RF_ACTIVE))) {
4113 pci_write_config(dev, PCIR_COMMAND,
4114 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
4115 ctlr->allocate = ata_via_allocate;
4116 ctlr->reset = ata_via_reset;
4118 ctlr->setmode = ata_sata_setmode;
4122 /* prepare for ATA-66 on the 82C686a and 82C596b */
4123 if (ctlr->chip->cfg2 & VIACLK)
4124 pci_write_config(dev, 0x50, 0x030b030b, 4);
4126 /* the southbridge might need the data corruption fix */
4127 if (ctlr->chip->cfg2 & VIABUG)
4128 ata_via_southbridge_fixup(dev);
4130 /* set fifo configuration half'n'half */
4131 pci_write_config(dev, 0x43,
4132 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
4134 /* set status register read retry */
4135 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
4137 /* set DMA read & end-of-sector fifo flush */
4138 pci_write_config(dev, 0x46,
4139 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
4141 /* set sector size */
4142 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
4143 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
4145 ctlr->setmode = ata_via_family_setmode;
4150 ata_via_allocate(device_t dev)
4152 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4153 struct ata_channel *ch = device_get_softc(dev);
4155 /* newer SATA chips has resources in one BAR for each channel */
4156 if (ctlr->chip->cfg2 & VIABAR) {
4157 struct resource *r_io;
4160 rid = PCIR_BAR(ch->unit);
4161 if (!(r_io = bus_alloc_resource_any(device_get_parent(dev),
4166 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
4167 ch->r_io[i].res = r_io;
4168 ch->r_io[i].offset = i;
4170 ch->r_io[ATA_CONTROL].res = r_io;
4171 ch->r_io[ATA_CONTROL].offset = 2 + ATA_IOSIZE;
4172 ch->r_io[ATA_IDX_ADDR].res = r_io;
4173 ata_default_registers(dev);
4174 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
4175 ch->r_io[i].res = ctlr->r_res1;
4176 ch->r_io[i].offset = i - ATA_BMCMD_PORT;
4178 ata_generic_hw(dev);
4181 if (ata_pci_allocate(dev))
4185 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4186 ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
4187 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4188 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
4189 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4190 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
4191 ch->flags |= ATA_NO_SLAVE;
4193 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
4194 /* XXX SOS unknown how to enable PHY state change interrupt */
4199 ata_via_reset(device_t dev)
4201 struct ata_channel *ch = device_get_softc(dev);
4203 ata_sata_phy_enable(ch);
4207 ata_via_southbridge_fixup(device_t dev)
4212 if (device_get_children(device_get_parent(dev), &children, &nchildren))
4215 for (i = 0; i < nchildren; i++) {
4216 if (pci_get_devid(children[i]) == ATA_VIA8363 ||
4217 pci_get_devid(children[i]) == ATA_VIA8371 ||
4218 pci_get_devid(children[i]) == ATA_VIA8662 ||
4219 pci_get_devid(children[i]) == ATA_VIA8361) {
4220 u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
4222 if ((reg76 & 0xf0) != 0xd0) {
4224 "Correcting VIA config for southbridge data corruption bug\n");
4225 pci_write_config(children[i], 0x75, 0x80, 1);
4226 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
4231 free(children, M_TEMP);
4235 /* common code for VIA, AMD & nVidia */
4237 ata_via_family_setmode(device_t dev, int mode)
4239 device_t gparent = GRANDPARENT(dev);
4240 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4241 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4242 struct ata_device *atadev = device_get_softc(dev);
4243 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
4244 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
4246 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* VIA ATA33 */
4247 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* VIA ATA66 */
4248 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* VIA ATA100 */
4249 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
4250 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }}; /* AMD/nVIDIA */
4251 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4252 int reg = 0x53 - devno;
4255 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4257 if (ctlr->chip->cfg2 & AMDCABLE) {
4258 if (mode > ATA_UDMA2 &&
4259 !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
4260 ata_print_cable(dev, "controller");
4265 mode = ata_check_80pin(dev, mode);
4267 if (ctlr->chip->cfg2 & NVIDIA)
4270 if (ctlr->chip->cfg1 != VIA133)
4271 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
4273 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4276 device_printf(dev, "%ssetting %s on %s chip\n",
4277 (error) ? "FAILURE " : "", ata_mode2str(mode),
4280 if (mode >= ATA_UDMA0)
4281 pci_write_config(gparent, reg,
4282 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
4284 pci_write_config(gparent, reg, 0x8b, 1);
4285 atadev->mode = mode;
4290 /* misc functions */
4291 struct ata_chip_id *
4292 ata_match_chip(device_t dev, struct ata_chip_id *index)
4294 while (index->chipid != 0) {
4295 if (pci_get_devid(dev) == index->chipid &&
4296 pci_get_revid(dev) >= index->chiprev)
4303 static struct ata_chip_id *
4304 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
4309 if (device_get_children(device_get_parent(dev), &children, &nchildren))
4312 while (index->chipid != 0) {
4313 for (i = 0; i < nchildren; i++) {
4314 if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
4315 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
4316 pci_get_devid(children[i]) == index->chipid &&
4317 pci_get_revid(children[i]) >= index->chiprev) {
4318 free(children, M_TEMP);
4324 free(children, M_TEMP);
4329 ata_setup_interrupt(device_t dev)
4331 struct ata_pci_controller *ctlr = device_get_softc(dev);
4332 int rid = ATA_IRQ_RID;
4334 if (!ata_legacy(dev)) {
4335 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
4336 RF_SHAREABLE | RF_ACTIVE))) {
4337 device_printf(dev, "unable to map interrupt\n");
4340 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
4341 ata_generic_intr, ctlr, &ctlr->handle))) {
4342 device_printf(dev, "unable to setup interrupt\n");
4349 struct ata_serialize {
4350 struct mtx locked_mtx;
4356 ata_serialize(device_t dev, int flags)
4358 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4359 struct ata_channel *ch = device_get_softc(dev);
4360 struct ata_serialize *serial;
4361 static int inited = 0;
4365 serial = malloc(sizeof(struct ata_serialize),
4366 M_TEMP, M_NOWAIT | M_ZERO);
4367 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
4368 serial->locked_ch = -1;
4369 serial->restart_ch = -1;
4370 device_set_ivars(ctlr->dev, serial);
4374 serial = device_get_ivars(ctlr->dev);
4376 mtx_lock(&serial->locked_mtx);
4379 if (serial->locked_ch == -1)
4380 serial->locked_ch = ch->unit;
4381 if (serial->locked_ch != ch->unit)
4382 serial->restart_ch = ch->unit;
4386 if (serial->locked_ch == ch->unit) {
4387 serial->locked_ch = -1;
4388 if (serial->restart_ch != -1) {
4389 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
4390 serial->restart_ch = -1;
4391 mtx_unlock(&serial->locked_mtx);
4402 res = serial->locked_ch;
4403 mtx_unlock(&serial->locked_mtx);
4408 ata_print_cable(device_t dev, u_int8_t *who)
4411 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
4415 ata_atapi(device_t dev)
4417 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4418 struct ata_device *atadev = device_get_softc(dev);
4420 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
4421 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
4425 ata_check_80pin(device_t dev, int mode)
4427 struct ata_device *atadev = device_get_softc(dev);
4429 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
4430 ata_print_cable(dev, "device");
4437 ata_mode2idx(int mode)
4439 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
4440 return (mode & ATA_MODE_MASK) + 8;
4441 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
4442 return (mode & ATA_MODE_MASK) + 5;
4443 return (mode & ATA_MODE_MASK) - ATA_PIO0;