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_marvell_chipinit(device_t dev);
102 static int ata_marvell_allocate(device_t dev);
103 static void ata_marvell_intr(void *data);
104 static void ata_marvell_dmainit(device_t dev);
105 static int ata_marvell_command(struct ata_request *request);
106 static void ata_marvell_reset(device_t dev);
107 static int ata_national_chipinit(device_t dev);
108 static void ata_national_setmode(device_t dev, int mode);
109 static int ata_nvidia_chipinit(device_t dev);
110 static int ata_nvidia_allocate(device_t dev);
111 static void ata_nvidia_intr(void *data);
112 static void ata_nvidia_reset(device_t dev);
113 static int ata_promise_chipinit(device_t dev);
114 static int ata_promise_mio_allocate(device_t dev);
115 static void ata_promise_mio_intr(void *data);
116 static void ata_promise_sx4_intr(void *data);
117 static void ata_promise_mio_dmainit(device_t dev);
118 static void ata_promise_mio_reset(device_t dev);
119 static int ata_promise_mio_command(struct ata_request *request);
120 static int ata_promise_sx4_command(struct ata_request *request);
121 static int ata_promise_apkt(u_int8_t *bytep, struct ata_request *request);
122 static void ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt);
123 static void ata_promise_next_hpkt(struct ata_pci_controller *ctlr);
124 static void ata_promise_tx2_intr(void *data);
125 static void ata_promise_old_intr(void *data);
126 static int ata_promise_new_dmastart(device_t dev);
127 static int ata_promise_new_dmastop(device_t dev);
128 static void ata_promise_new_dmareset(device_t dev);
129 static void ata_promise_new_dmainit(device_t dev);
130 static void ata_promise_setmode(device_t dev, int mode);
131 static int ata_serverworks_chipinit(device_t dev);
132 static void ata_serverworks_setmode(device_t dev, int mode);
133 static int ata_sii_chipinit(device_t dev);
134 static int ata_sii_allocate(device_t dev);
135 static void ata_sii_intr(void *data);
136 static void ata_cmd_intr(void *data);
137 static void ata_cmd_old_intr(void *data);
138 static void ata_sii_reset(device_t dev);
139 static void ata_sii_setmode(device_t dev, int mode);
140 static void ata_cmd_setmode(device_t dev, int mode);
141 static int ata_sis_chipinit(device_t dev);
142 static int ata_sis_allocate(device_t dev);
143 static void ata_sis_reset(device_t dev);
144 static void ata_sis_setmode(device_t dev, int mode);
145 static int ata_via_chipinit(device_t dev);
146 static int ata_via_allocate(device_t dev);
147 static void ata_via_reset(device_t dev);
148 static void ata_via_southbridge_fixup(device_t dev);
149 static void ata_via_family_setmode(device_t dev, int mode);
150 static struct ata_chip_id *ata_find_chip(device_t dev, struct ata_chip_id *index, int slot);
151 static int ata_setup_interrupt(device_t dev);
152 static int ata_serialize(device_t dev, int flags);
153 static void ata_print_cable(device_t dev, u_int8_t *who);
154 static int ata_atapi(device_t dev);
155 static int ata_check_80pin(device_t dev, int mode);
156 static int ata_mode2idx(int mode);
159 /* generic or unknown ATA chipset support functions */
161 ata_generic_ident(device_t dev)
163 struct ata_pci_controller *ctlr = device_get_softc(dev);
165 device_set_desc(dev, "GENERIC ATA controller");
166 ctlr->chipinit = ata_generic_chipinit;
171 ata_generic_chipinit(device_t dev)
173 struct ata_pci_controller *ctlr = device_get_softc(dev);
175 if (ata_setup_interrupt(dev))
177 ctlr->setmode = ata_generic_setmode;
182 ata_generic_intr(void *data)
184 struct ata_pci_controller *ctlr = data;
185 struct ata_channel *ch;
188 for (unit = 0; unit < ctlr->channels; unit++) {
189 if (!(ch = ctlr->interrupt[unit].argument))
191 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
192 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
194 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
195 ATA_BMSTAT_INTERRUPT)
197 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
200 ctlr->interrupt[unit].function(ch);
205 ata_generic_setmode(device_t dev, int mode)
207 struct ata_device *atadev = device_get_softc(dev);
209 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
210 mode = ata_check_80pin(dev, mode);
211 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
216 /* SATA support functions */
218 ata_sata_setmode(device_t dev, int mode)
220 struct ata_device *atadev = device_get_softc(dev);
223 * if we detect that the device isn't a real SATA device we limit
224 * the transfer mode to UDMA5/ATA100.
225 * this works around the problems some devices has with the
226 * Marvell 88SX8030 SATA->PATA converters and UDMA6/ATA133.
228 if (atadev->param.satacapabilities != 0x0000 &&
229 atadev->param.satacapabilities != 0xffff) {
230 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
231 ata_limit_mode(dev, mode, ATA_UDMA6)))
232 /* XXX SOS we should query SATA STATUS for the speed */
233 atadev->mode = ATA_SA150;
236 mode = ata_limit_mode(dev, mode, ATA_UDMA5);
237 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
243 ata_sata_connect(struct ata_channel *ch)
248 /* wait up to 1 second for "connect well" */
249 for (timeout = 0; timeout < 100 ; timeout++) {
250 status = ATA_IDX_INL(ch, ATA_SSTATUS);
251 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1 ||
252 (status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)
256 if (timeout >= 100) {
258 device_printf(ch->dev, "SATA connect status=%08x\n", status);
262 /* clear SATA error register */
263 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
265 /* find out what type device we got poll for spec'd 31 seconds */
266 /* XXX SOS 10 secs for now as I have little patience */
268 for (timeout = 0; timeout < 1000; timeout++) {
269 if (ATA_IDX_INB(ch, ATA_STATUS) & ATA_S_BUSY)
275 device_printf(ch->dev, "SATA connect ready time=%dms\n", timeout * 10);
276 if (timeout < 1000) {
277 if ((ATA_IDX_INB(ch, ATA_CYL_LSB) == ATAPI_MAGIC_LSB) &&
278 (ATA_IDX_INB(ch, ATA_CYL_MSB) == ATAPI_MAGIC_MSB))
279 ch->devices = ATA_ATAPI_MASTER;
281 ch->devices = ATA_ATA_MASTER;
284 device_printf(ch->dev, "sata_connect devices=0x%b\n",
285 ch->devices, "\20\3ATAPI_MASTER\1ATA_MASTER");
290 ata_sata_phy_enable(struct ata_channel *ch)
294 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE) {
295 ata_sata_connect(ch);
299 for (retry = 0; retry < 10; retry++) {
300 for (loop = 0; loop < 10; loop++) {
301 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_RESET);
303 if ((ATA_IDX_INL(ch, ATA_SCONTROL) &
304 ATA_SC_DET_MASK) == ATA_SC_DET_RESET)
308 for (loop = 0; loop < 10; loop++) {
309 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_IDLE);
311 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == 0) {
312 ata_sata_connect(ch);
320 ata_sata_phy_event(void *context, int dummy)
322 struct ata_connect_task *tp = (struct ata_connect_task *)context;
323 struct ata_channel *ch = device_get_softc(tp->dev);
327 mtx_lock(&Giant); /* newbus suckage it needs Giant */
328 if (tp->action == ATA_C_ATTACH) {
329 device_printf(tp->dev, "CONNECTED\n");
330 ata_sata_connect(ch);
331 ata_identify(tp->dev);
333 if (tp->action == ATA_C_DETACH) {
334 if (!device_get_children(tp->dev, &children, &nchildren)) {
335 for (i = 0; i < nchildren; i++)
337 device_delete_child(tp->dev, children[i]);
338 free(children, M_TEMP);
340 mtx_lock(&ch->state_mtx);
341 ch->state = ATA_IDLE;
342 mtx_unlock(&ch->state_mtx);
343 device_printf(tp->dev, "DISCONNECTED\n");
345 mtx_unlock(&Giant); /* suckage code dealt with, release Giant */
351 * AHCI v1.0 compliant SATA chipset support functions
353 struct ata_ahci_dma_prd {
356 u_int32_t dbc; /* 0 based */
357 #define ATA_AHCI_PRD_MASK 0x003fffff /* max 4MB */
358 #define ATA_AHCI_PRD_IPC (1<<31)
361 struct ata_ahci_cmd_tab {
364 u_int8_t reserved[32];
365 struct ata_ahci_dma_prd prd_tab[16];
368 struct ata_ahci_cmd_list {
370 u_int16_t prd_length; /* PRD entries */
372 u_int64_t cmd_table_phys; /* 128byte aligned */
377 ata_ahci_allocate(device_t dev)
379 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
380 struct ata_channel *ch = device_get_softc(dev);
381 int offset = (ch->unit << 7);
383 /* XXX SOS this is a hack to satisfy various legacy cruft */
384 ch->r_io[ATA_CYL_LSB].res = ctlr->r_res2;
385 ch->r_io[ATA_CYL_LSB].offset = ATA_AHCI_P_SIG + 1 + offset;
386 ch->r_io[ATA_CYL_MSB].res = ctlr->r_res2;
387 ch->r_io[ATA_CYL_MSB].offset = ATA_AHCI_P_SIG + 3 + offset;
388 ch->r_io[ATA_STATUS].res = ctlr->r_res2;
389 ch->r_io[ATA_STATUS].offset = ATA_AHCI_P_TFD + offset;
390 ch->r_io[ATA_ALTSTAT].res = ctlr->r_res2;
391 ch->r_io[ATA_ALTSTAT].offset = ATA_AHCI_P_TFD + offset;
393 /* set the SATA resources */
394 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
395 ch->r_io[ATA_SSTATUS].offset = ATA_AHCI_P_SSTS + offset;
396 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
397 ch->r_io[ATA_SERROR].offset = ATA_AHCI_P_SERR + offset;
398 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
399 ch->r_io[ATA_SCONTROL].offset = ATA_AHCI_P_SCTL + offset;
400 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
401 ch->r_io[ATA_SACTIVE].offset = ATA_AHCI_P_SACT + offset;
403 ch->hw.begin_transaction = ata_ahci_begin_transaction;
404 ch->hw.end_transaction = ata_ahci_end_transaction;
405 ch->hw.command = NULL; /* not used here */
407 /* setup the work areas */
408 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLB + offset,
409 ch->dma->work_bus + ATA_AHCI_CL_OFFSET);
410 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLBU + offset, 0x00000000);
412 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FB + offset,
413 ch->dma->work_bus + ATA_AHCI_FB_OFFSET);
414 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FBU + offset, 0x00000000);
416 /* enable wanted port interrupts */
417 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset,
418 (ATA_AHCI_P_IX_CPD | ATA_AHCI_P_IX_TFE | ATA_AHCI_P_IX_HBF |
419 ATA_AHCI_P_IX_HBD | ATA_AHCI_P_IX_IF | ATA_AHCI_P_IX_OF |
420 ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC | ATA_AHCI_P_IX_DP |
421 ATA_AHCI_P_IX_UF | ATA_AHCI_P_IX_SDB | ATA_AHCI_P_IX_DS |
422 ATA_AHCI_P_IX_PS | ATA_AHCI_P_IX_DHR));
424 /* start operations on this channel */
425 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
426 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
427 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
432 ata_ahci_setup_fis(u_int8_t *fis, struct ata_request *request)
434 struct ata_device *atadev = device_get_softc(request->dev);
437 /* XXX SOS add ATAPI commands support later */
438 ata_modify_if_48bit(request);
440 fis[idx++] = 0x27; /* host to device */
441 fis[idx++] = 0x80; /* command FIS (note PM goes here) */
442 fis[idx++] = request->u.ata.command;
443 fis[idx++] = request->u.ata.feature;
445 fis[idx++] = request->u.ata.lba;
446 fis[idx++] = request->u.ata.lba >> 8;
447 fis[idx++] = request->u.ata.lba >> 16;
448 fis[idx] = ATA_D_LBA | atadev->unit;
449 if (atadev->flags & ATA_D_48BIT_ACTIVE)
452 fis[idx++] |= (request->u.ata.lba >> 24 & 0x0f);
454 fis[idx++] = request->u.ata.lba >> 24;
455 fis[idx++] = request->u.ata.lba >> 32;
456 fis[idx++] = request->u.ata.lba >> 40;
457 fis[idx++] = request->u.ata.feature >> 8;
459 fis[idx++] = request->u.ata.count;
460 fis[idx++] = request->u.ata.count >> 8;
462 fis[idx++] = ATA_A_4BIT;
471 /* must be called with ATA channel locked and state_mtx held */
473 ata_ahci_begin_transaction(struct ata_request *request)
475 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
476 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
477 struct ata_ahci_cmd_tab *ctp;
478 struct ata_ahci_cmd_list *clp;
479 int fis_size, entries;
482 /* get a piece of the workspace for this request */
483 ctp = (struct ata_ahci_cmd_tab *)
484 (ch->dma->work + ATA_AHCI_CT_OFFSET + (ATA_AHCI_CT_SIZE * tag));
486 /* setup the FIS for this request */ /* XXX SOS ATAPI missing still */
487 if (!(fis_size = ata_ahci_setup_fis(&ctp->cfis[0], request))) {
488 device_printf(request->dev, "setting up SATA FIS failed\n");
489 request->result = EIO;
490 return ATA_OP_FINISHED;
493 /* if request moves data setup and load SG list */
494 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
495 if (ch->dma->load(ch->dev, request->data, request->bytecount,
496 request->flags & ATA_R_READ,
497 ctp->prd_tab, &entries)) {
498 device_printf(request->dev, "setting up DMA failed\n");
499 request->result = EIO;
500 return ATA_OP_FINISHED;
504 /* setup the command list entry */
505 clp = (struct ata_ahci_cmd_list *)
506 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
508 clp->prd_length = entries;
509 clp->cmd_flags = (request->flags & ATA_R_WRITE ? (1<<6) : 0) |
510 (request->flags & ATA_R_ATAPI ? (1<<5) : 0) |
511 (fis_size / sizeof(u_int32_t));
513 clp->cmd_table_phys = htole64(ch->dma->work_bus + ATA_AHCI_CT_OFFSET +
514 (ATA_AHCI_CT_SIZE * tag));
516 /* clear eventual ACTIVE bit */
517 ATA_IDX_OUTL(ch, ATA_SACTIVE, ATA_IDX_INL(ch, ATA_SACTIVE) & (1 << tag));
519 /* issue the command */
520 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CI + (ch->unit << 7), (1 << tag));
522 /* start the timeout */
523 callout_reset(&request->callout, request->timeout * hz,
524 (timeout_t*)ata_timeout, request);
525 return ATA_OP_CONTINUES;
528 /* must be called with ATA channel locked and state_mtx held */
530 ata_ahci_end_transaction(struct ata_request *request)
532 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
533 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
534 struct ata_ahci_cmd_list *clp;
538 /* kill the timeout */
539 callout_stop(&request->callout);
542 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + (ch->unit << 7));
543 request->status = tf_data;
545 /* if error status get details */
546 if (request->status & ATA_S_ERROR)
547 request->error = tf_data >> 8;
549 /* record how much data we actually moved */
550 clp = (struct ata_ahci_cmd_list *)
551 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
552 request->donecount = clp->bytecount;
554 /* release SG list etc */
555 ch->dma->unload(ch->dev);
557 return ATA_OP_FINISHED;
561 ata_ahci_intr(void *data)
563 struct ata_pci_controller *ctlr = data;
564 struct ata_channel *ch;
565 u_int32_t port, status, error, issued;
569 port = ATA_INL(ctlr->r_res2, ATA_AHCI_IS);
571 for (unit = 0; unit < ctlr->channels; unit++) {
572 if (port & (1 << unit)) {
573 if ((ch = ctlr->interrupt[unit].argument)) {
574 struct ata_connect_task *tp;
575 int offset = (ch->unit << 7);
577 error = ATA_INL(ctlr->r_res2, ATA_AHCI_P_SERR + offset);
578 status = ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset);
579 issued = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CI + offset);
580 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_SERR + offset, error);
581 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset, status);
583 /* do we have cold connect surprise */
584 if (status & ATA_AHCI_P_IX_CPD) {
585 printf("ata_ahci_intr status=%08x error=%08x issued=%08x\n",
586 status, error, issued);
589 /* check for and handle connect events */
590 if (((status & (ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC)) ==
592 (tp = (struct ata_connect_task *)
593 malloc(sizeof(struct ata_connect_task),
594 M_ATA, M_NOWAIT | M_ZERO))) {
596 device_printf(ch->dev, "CONNECT requested\n");
597 tp->action = ATA_C_ATTACH;
599 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
600 taskqueue_enqueue(taskqueue_thread, &tp->task);
603 /* check for and handle disconnect events */
604 if (((status & (ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC)) ==
605 ATA_AHCI_P_IX_PRC) &&
606 (tp = (struct ata_connect_task *)
607 malloc(sizeof(struct ata_connect_task),
608 M_ATA, M_NOWAIT | M_ZERO))) {
610 device_printf(ch->dev, "DISCONNECT requested\n");
611 tp->action = ATA_C_DETACH;
613 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
614 taskqueue_enqueue(taskqueue_thread, &tp->task);
617 /* any drive action to take care of ? */
618 if (!(issued & (1<<tag)))
619 ctlr->interrupt[unit].function(ch);
623 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, port);
627 ata_ahci_reset(device_t dev)
629 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
630 struct ata_channel *ch = device_get_softc(dev);
632 int offset = (ch->unit << 7);
634 /* kill off all activity on this channel */
635 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
636 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
637 cmd & ~(ATA_AHCI_P_CMD_CR | ATA_AHCI_P_CMD_FR |
638 ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
640 DELAY(500000); /* XXX SOS this is not entirely wrong */
643 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, ATA_AHCI_P_CMD_SUD);
645 ata_sata_phy_enable(ch);
647 /* clear any interrupts pending on this channel */
648 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset,
649 ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset));
651 /* start operations on this channel */
652 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
653 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
654 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
658 ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
660 struct ata_dmasetprd_args *args = xsc;
661 struct ata_ahci_dma_prd *prd = args->dmatab;
664 if (!(args->error = error)) {
665 for (i = 0; i < nsegs; i++) {
666 prd[i].dba = htole64(segs[i].ds_addr);
667 prd[i].dbc = htole32((segs[i].ds_len - 1) & ATA_AHCI_PRD_MASK);
674 ata_ahci_dmainit(device_t dev)
676 struct ata_channel *ch = device_get_softc(dev);
680 /* note start and stop are not used here */
681 ch->dma->setprd = ata_ahci_dmasetprd;
682 ch->dma->max_iosize = 8192 * DEV_BSIZE;
688 * Acard chipset support functions
691 ata_acard_ident(device_t dev)
693 struct ata_pci_controller *ctlr = device_get_softc(dev);
694 struct ata_chip_id *idx;
695 static struct ata_chip_id ids[] =
696 {{ ATA_ATP850R, 0, ATPOLD, 0x00, ATA_UDMA2, "ATP850" },
697 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "ATP860A" },
698 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "ATP860R" },
699 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "ATP865A" },
700 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "ATP865R" },
701 { 0, 0, 0, 0, 0, 0}};
704 if (!(idx = ata_match_chip(dev, ids)))
707 sprintf(buffer, "Acard %s %s controller",
708 idx->text, ata_mode2str(idx->max_dma));
709 device_set_desc_copy(dev, buffer);
711 ctlr->chipinit = ata_acard_chipinit;
716 ata_acard_chipinit(device_t dev)
718 struct ata_pci_controller *ctlr = device_get_softc(dev);
719 int rid = ATA_IRQ_RID;
721 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
722 RF_SHAREABLE | RF_ACTIVE))) {
723 device_printf(dev, "unable to map interrupt\n");
726 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
727 ata_acard_intr, ctlr, &ctlr->handle))) {
728 device_printf(dev, "unable to setup interrupt\n");
731 if (ctlr->chip->cfg1 == ATPOLD) {
732 ctlr->setmode = ata_acard_850_setmode;
733 ctlr->locking = ata_serialize;
736 ctlr->setmode = ata_acard_86X_setmode;
741 ata_acard_intr(void *data)
743 struct ata_pci_controller *ctlr = data;
744 struct ata_channel *ch;
747 for (unit = 0; unit < ctlr->channels; unit++) {
748 if (!(ch = ctlr->interrupt[unit].argument))
750 if (ctlr->chip->cfg1 == ATPOLD &&
751 ATA_LOCKING(ch->dev, ATA_LF_WHICH) != unit)
753 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
754 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
756 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
757 ATA_BMSTAT_INTERRUPT)
759 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
761 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
762 ATA_IDX_INB(ch, ATA_BMCMD_PORT)&~ATA_BMCMD_START_STOP);
765 ctlr->interrupt[unit].function(ch);
770 ata_acard_850_setmode(device_t dev, int mode)
772 device_t gparent = GRANDPARENT(dev);
773 struct ata_pci_controller *ctlr = device_get_softc(gparent);
774 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
775 struct ata_device *atadev = device_get_softc(dev);
776 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
779 mode = ata_limit_mode(dev, mode,
780 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
782 /* XXX SOS missing WDMA0+1 + PIO modes */
783 if (mode >= ATA_WDMA2) {
784 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
786 device_printf(dev, "%ssetting %s on %s chip\n",
787 (error) ? "FAILURE " : "",
788 ata_mode2str(mode), ctlr->chip->text);
790 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
792 reg54 &= ~(0x03 << (devno << 1));
793 if (mode >= ATA_UDMA0)
794 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
795 pci_write_config(gparent, 0x54, reg54, 1);
796 pci_write_config(gparent, 0x4a, 0xa6, 1);
797 pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
802 /* we could set PIO mode timings, but we assume the BIOS did that */
806 ata_acard_86X_setmode(device_t dev, int mode)
808 device_t gparent = GRANDPARENT(dev);
809 struct ata_pci_controller *ctlr = device_get_softc(gparent);
810 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
811 struct ata_device *atadev = device_get_softc(dev);
812 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
816 mode = ata_limit_mode(dev, mode,
817 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
819 mode = ata_check_80pin(dev, mode);
821 /* XXX SOS missing WDMA0+1 + PIO modes */
822 if (mode >= ATA_WDMA2) {
823 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
825 device_printf(dev, "%ssetting %s on %s chip\n",
826 (error) ? "FAILURE " : "",
827 ata_mode2str(mode), ctlr->chip->text);
829 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
831 reg44 &= ~(0x000f << (devno << 2));
832 if (mode >= ATA_UDMA0)
833 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
834 pci_write_config(gparent, 0x44, reg44, 2);
835 pci_write_config(gparent, 0x4a, 0xa6, 1);
836 pci_write_config(gparent, 0x40 + devno, 0x31, 1);
841 /* we could set PIO mode timings, but we assume the BIOS did that */
846 * Acer Labs Inc (ALI) chipset support functions
849 ata_ali_ident(device_t dev)
851 struct ata_pci_controller *ctlr = device_get_softc(dev);
852 struct ata_chip_id *idx;
853 static struct ata_chip_id ids[] =
854 {{ ATA_ALI_5289, 0x00, 2, ALISATA, ATA_SA150, "M5289" },
855 { ATA_ALI_5287, 0x00, 4, ALISATA, ATA_SA150, "M5287" },
856 { ATA_ALI_5281, 0x00, 2, ALISATA, ATA_SA150, "M5281" },
857 { ATA_ALI_5229, 0xc5, 0, ALINEW, ATA_UDMA6, "M5229" },
858 { ATA_ALI_5229, 0xc4, 0, ALINEW, ATA_UDMA5, "M5229" },
859 { ATA_ALI_5229, 0xc2, 0, ALINEW, ATA_UDMA4, "M5229" },
860 { ATA_ALI_5229, 0x20, 0, ALIOLD, ATA_UDMA2, "M5229" },
861 { ATA_ALI_5229, 0x00, 0, ALIOLD, ATA_WDMA2, "M5229" },
862 { 0, 0, 0, 0, 0, 0}};
865 if (!(idx = ata_match_chip(dev, ids)))
868 sprintf(buffer, "AcerLabs %s %s controller",
869 idx->text, ata_mode2str(idx->max_dma));
870 device_set_desc_copy(dev, buffer);
872 ctlr->chipinit = ata_ali_chipinit;
877 ata_ali_chipinit(device_t dev)
879 struct ata_pci_controller *ctlr = device_get_softc(dev);
881 if (ata_setup_interrupt(dev))
884 switch (ctlr->chip->cfg2) {
886 pci_write_config(dev, PCIR_COMMAND,
887 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
888 ctlr->channels = ctlr->chip->cfg1;
889 ctlr->allocate = ata_ali_sata_allocate;
890 ctlr->setmode = ata_sata_setmode;
894 /* use device interrupt as byte count end */
895 pci_write_config(dev, 0x4a, pci_read_config(dev, 0x4a, 1) | 0x20, 1);
897 /* enable cable detection and UDMA support on newer chips */
898 pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
900 /* enable ATAPI UDMA mode */
901 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x01, 1);
903 /* only chips with revision > 0xc4 can do 48bit DMA */
904 if (ctlr->chip->chiprev <= 0xc4)
906 "using PIO transfers above 137GB as workaround for "
907 "48bit DMA access bug, expect reduced performance\n");
908 ctlr->reset = ata_ali_reset;
909 ctlr->allocate = ata_ali_allocate;
910 ctlr->setmode = ata_ali_setmode;
914 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
915 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x03, 1);
916 ctlr->setmode = ata_ali_setmode;
923 ata_ali_allocate(device_t dev)
925 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
926 struct ata_channel *ch = device_get_softc(dev);
928 /* setup the usual register normal pci style */
929 if (ata_pci_allocate(dev))
932 /* older chips can't do 48bit DMA transfers */
933 if (ctlr->chip->chiprev <= 0xc4)
934 ch->flags |= ATA_NO_48BIT_DMA;
940 ata_ali_sata_allocate(device_t dev)
942 device_t parent = device_get_parent(dev);
943 struct ata_pci_controller *ctlr = device_get_softc(parent);
944 struct ata_channel *ch = device_get_softc(dev);
945 struct resource *io = NULL, *ctlio = NULL;
946 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
949 rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
950 io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
954 rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
955 ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
957 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
961 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
962 ch->r_io[i].res = io;
963 ch->r_io[i].offset = i + (unit10 ? 8 : 0);
965 ch->r_io[ATA_CONTROL].res = ctlio;
966 ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
967 ch->r_io[ATA_IDX_ADDR].res = io;
968 ata_default_registers(dev);
970 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
971 ch->r_io[i].res = ctlr->r_res1;
972 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
975 ch->flags |= ATA_NO_SLAVE;
977 /* XXX SOS PHY handling awkward in ALI chip not supported yet */
983 ata_ali_reset(device_t dev)
985 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
986 struct ata_channel *ch = device_get_softc(dev);
990 ata_generic_reset(dev);
993 * workaround for datacorruption bug found on at least SUN Blade-100
994 * find the ISA function on the southbridge and disable then enable
995 * the ATA channel tristate buffer
997 if (ctlr->chip->chiprev == 0xc3 || ctlr->chip->chiprev == 0xc2) {
998 if (!device_get_children(GRANDPARENT(dev), &children, &nchildren)) {
999 for (i = 0; i < nchildren; i++) {
1000 if (pci_get_devid(children[i]) == ATA_ALI_1533) {
1001 pci_write_config(children[i], 0x58,
1002 pci_read_config(children[i], 0x58, 1) &
1003 ~(0x04 << ch->unit), 1);
1004 pci_write_config(children[i], 0x58,
1005 pci_read_config(children[i], 0x58, 1) |
1006 (0x04 << ch->unit), 1);
1010 free(children, M_TEMP);
1016 ata_ali_setmode(device_t dev, int mode)
1018 device_t gparent = GRANDPARENT(dev);
1019 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1020 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1021 struct ata_device *atadev = device_get_softc(dev);
1022 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1025 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1027 if (ctlr->chip->cfg2 & ALINEW) {
1028 if (mode > ATA_UDMA2 &&
1029 pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
1030 ata_print_cable(dev, "controller");
1035 mode = ata_check_80pin(dev, mode);
1037 if (ctlr->chip->cfg2 & ALIOLD) {
1038 /* doesn't support ATAPI DMA on write */
1039 ch->flags |= ATA_ATAPI_DMA_RO;
1040 if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
1041 /* doesn't support ATAPI DMA on two ATAPI devices */
1042 device_printf(dev, "two atapi devices on this channel, no DMA\n");
1043 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1047 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1050 device_printf(dev, "%ssetting %s on %s chip\n",
1051 (error) ? "FAILURE " : "",
1052 ata_mode2str(mode), ctlr->chip->text);
1054 if (mode >= ATA_UDMA0) {
1055 u_int8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
1056 u_int32_t word54 = pci_read_config(gparent, 0x54, 4);
1058 word54 &= ~(0x000f000f << (devno << 2));
1059 word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
1060 pci_write_config(gparent, 0x54, word54, 4);
1061 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1065 u_int32_t piotimings[] =
1066 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
1067 0x00310001, 0x00440001, 0x00330001, 0x00310001};
1069 pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
1070 ~(0x0008000f << (devno << 2)), 4);
1071 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1072 piotimings[ata_mode2idx(mode)], 4);
1074 atadev->mode = mode;
1080 * American Micro Devices (AMD) chipset support functions
1083 ata_amd_ident(device_t dev)
1085 struct ata_pci_controller *ctlr = device_get_softc(dev);
1086 struct ata_chip_id *idx;
1087 static struct ata_chip_id ids[] =
1088 {{ ATA_AMD756, 0x00, AMDNVIDIA, 0x00, ATA_UDMA4, "756" },
1089 { ATA_AMD766, 0x00, AMDNVIDIA, AMDCABLE|AMDBUG, ATA_UDMA5, "766" },
1090 { ATA_AMD768, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA5, "768" },
1091 { ATA_AMD8111, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA6, "8111" },
1092 { 0, 0, 0, 0, 0, 0}};
1095 if (!(idx = ata_match_chip(dev, ids)))
1098 sprintf(buffer, "AMD %s %s controller",
1099 idx->text, ata_mode2str(idx->max_dma));
1100 device_set_desc_copy(dev, buffer);
1102 ctlr->chipinit = ata_amd_chipinit;
1107 ata_amd_chipinit(device_t dev)
1109 struct ata_pci_controller *ctlr = device_get_softc(dev);
1111 if (ata_setup_interrupt(dev))
1114 /* disable/set prefetch, postwrite */
1115 if (ctlr->chip->cfg2 & AMDBUG)
1116 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
1118 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
1120 ctlr->setmode = ata_via_family_setmode;
1126 * ATI chipset support functions
1129 ata_ati_ident(device_t dev)
1131 struct ata_pci_controller *ctlr = device_get_softc(dev);
1132 struct ata_chip_id *idx;
1133 static struct ata_chip_id ids[] =
1134 {{ ATA_ATI_IXP200, 0x00, 0, 0, ATA_UDMA5, "IXP200" },
1135 { ATA_ATI_IXP300, 0x00, 0, 0, ATA_UDMA6, "IXP300" },
1136 { ATA_ATI_IXP400, 0x00, 0, 0, ATA_UDMA6, "IXP400" },
1137 { ATA_ATI_IXP300_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP300" },
1138 { ATA_ATI_IXP400_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP400" },
1139 { ATA_ATI_IXP400_S2, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP400" },
1140 { 0, 0, 0, 0, 0, 0}};
1143 if (!(idx = ata_match_chip(dev, ids)))
1146 sprintf(buffer, "ATI %s %s controller",
1147 idx->text, ata_mode2str(idx->max_dma));
1148 device_set_desc_copy(dev, buffer);
1151 /* the ATI SATA controller is actually a SiI 3112 controller*/
1152 if (ctlr->chip->cfg1 & SIIMEMIO)
1153 ctlr->chipinit = ata_sii_chipinit;
1155 ctlr->chipinit = ata_ati_chipinit;
1160 ata_ati_chipinit(device_t dev)
1162 struct ata_pci_controller *ctlr = device_get_softc(dev);
1164 if (ata_setup_interrupt(dev))
1167 ctlr->setmode = ata_ati_setmode;
1172 ata_ati_setmode(device_t dev, int mode)
1174 device_t gparent = GRANDPARENT(dev);
1175 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1176 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1177 struct ata_device *atadev = device_get_softc(dev);
1178 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1179 int offset = (devno ^ 0x01) << 3;
1181 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
1182 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
1183 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
1185 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1187 mode = ata_check_80pin(dev, mode);
1189 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1192 device_printf(dev, "%ssetting %s on %s chip\n",
1193 (error) ? "FAILURE " : "",
1194 ata_mode2str(mode), ctlr->chip->text);
1196 if (mode >= ATA_UDMA0) {
1197 pci_write_config(gparent, 0x56,
1198 (pci_read_config(gparent, 0x56, 2) &
1199 ~(0xf << (devno << 2))) |
1200 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
1201 pci_write_config(gparent, 0x54,
1202 pci_read_config(gparent, 0x54, 1) |
1203 (0x01 << devno), 1);
1204 pci_write_config(gparent, 0x44,
1205 (pci_read_config(gparent, 0x44, 4) &
1206 ~(0xff << offset)) |
1207 (dmatimings[2] << offset), 4);
1209 else if (mode >= ATA_WDMA0) {
1210 pci_write_config(gparent, 0x54,
1211 pci_read_config(gparent, 0x54, 1) &
1212 ~(0x01 << devno), 1);
1213 pci_write_config(gparent, 0x44,
1214 (pci_read_config(gparent, 0x44, 4) &
1215 ~(0xff << offset)) |
1216 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
1219 pci_write_config(gparent, 0x54,
1220 pci_read_config(gparent, 0x54, 1) &
1221 ~(0x01 << devno), 1);
1223 pci_write_config(gparent, 0x4a,
1224 (pci_read_config(gparent, 0x4a, 2) &
1225 ~(0xf << (devno << 2))) |
1226 (((mode - ATA_PIO0) & ATA_MODE_MASK) << (devno<<2)),2);
1227 pci_write_config(gparent, 0x40,
1228 (pci_read_config(gparent, 0x40, 4) &
1229 ~(0xff << offset)) |
1230 (piotimings[ata_mode2idx(mode)] << offset), 4);
1231 atadev->mode = mode;
1236 * Cyrix chipset support functions
1239 ata_cyrix_ident(device_t dev)
1241 struct ata_pci_controller *ctlr = device_get_softc(dev);
1243 if (pci_get_devid(dev) == ATA_CYRIX_5530) {
1244 device_set_desc(dev, "Cyrix 5530 ATA33 controller");
1245 ctlr->chipinit = ata_cyrix_chipinit;
1252 ata_cyrix_chipinit(device_t dev)
1254 struct ata_pci_controller *ctlr = device_get_softc(dev);
1256 if (ata_setup_interrupt(dev))
1260 ctlr->setmode = ata_cyrix_setmode;
1262 ctlr->setmode = ata_generic_setmode;
1267 ata_cyrix_setmode(device_t dev, int mode)
1269 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1270 struct ata_device *atadev = device_get_softc(dev);
1271 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1272 u_int32_t piotiming[] =
1273 { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
1274 u_int32_t dmatiming[] = { 0x00077771, 0x00012121, 0x00002020 };
1275 u_int32_t udmatiming[] = { 0x00921250, 0x00911140, 0x00911030 };
1278 ch->dma->alignment = 16;
1279 ch->dma->max_iosize = 126 * DEV_BSIZE;
1281 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1283 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1286 device_printf(dev, "%ssetting %s on Cyrix chip\n",
1287 (error) ? "FAILURE " : "", ata_mode2str(mode));
1289 if (mode >= ATA_UDMA0) {
1290 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1291 0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
1293 else if (mode >= ATA_WDMA0) {
1294 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1295 0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
1298 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1299 0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
1301 atadev->mode = mode;
1307 * Cypress chipset support functions
1310 ata_cypress_ident(device_t dev)
1312 struct ata_pci_controller *ctlr = device_get_softc(dev);
1315 * the Cypress chip is a mess, it contains two ATA functions, but
1316 * both channels are visible on the first one.
1317 * simply ignore the second function for now, as the right
1318 * solution (ignoring the second channel on the first function)
1319 * doesn't work with the crappy ATA interrupt setup on the alpha.
1321 if (pci_get_devid(dev) == ATA_CYPRESS_82C693 &&
1322 pci_get_function(dev) == 1 &&
1323 pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
1324 device_set_desc(dev, "Cypress 82C693 ATA controller");
1325 ctlr->chipinit = ata_cypress_chipinit;
1332 ata_cypress_chipinit(device_t dev)
1334 struct ata_pci_controller *ctlr = device_get_softc(dev);
1336 if (ata_setup_interrupt(dev))
1339 ctlr->setmode = ata_cypress_setmode;
1344 ata_cypress_setmode(device_t dev, int mode)
1346 device_t gparent = GRANDPARENT(dev);
1347 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1348 struct ata_device *atadev = device_get_softc(dev);
1351 mode = ata_limit_mode(dev, mode, ATA_WDMA2);
1353 /* XXX SOS missing WDMA0+1 + PIO modes */
1354 if (mode == ATA_WDMA2) {
1355 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1357 device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
1358 error ? "FAILURE " : "");
1360 pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
1361 atadev->mode = mode;
1365 /* we could set PIO mode timings, but we assume the BIOS did that */
1370 * HighPoint chipset support functions
1373 ata_highpoint_ident(device_t dev)
1375 struct ata_pci_controller *ctlr = device_get_softc(dev);
1376 struct ata_chip_id *idx;
1377 static struct ata_chip_id ids[] =
1378 {{ ATA_HPT374, 0x07, HPT374, 0x00, ATA_UDMA6, "HPT374" },
1379 { ATA_HPT372, 0x02, HPT372, 0x00, ATA_UDMA6, "HPT372N" },
1380 { ATA_HPT372, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT372" },
1381 { ATA_HPT371, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT371" },
1382 { ATA_HPT366, 0x05, HPT372, 0x00, ATA_UDMA6, "HPT372" },
1383 { ATA_HPT366, 0x03, HPT370, 0x00, ATA_UDMA5, "HPT370" },
1384 { ATA_HPT366, 0x02, HPT366, 0x00, ATA_UDMA4, "HPT368" },
1385 { ATA_HPT366, 0x00, HPT366, HPTOLD, ATA_UDMA4, "HPT366" },
1386 { ATA_HPT302, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT302" },
1387 { 0, 0, 0, 0, 0, 0}};
1390 if (!(idx = ata_match_chip(dev, ids)))
1393 strcpy(buffer, idx->text);
1394 if (idx->cfg1 == HPT374) {
1395 if (pci_get_function(dev) == 0)
1396 strcat(buffer, " (channel 0+1)");
1397 else if (pci_get_function(dev) == 1)
1398 strcat(buffer, " (channel 2+3)");
1400 sprintf(buffer, "HighPoint %s %s controller",
1401 buffer, ata_mode2str(idx->max_dma));
1402 device_set_desc_copy(dev, buffer);
1404 ctlr->chipinit = ata_highpoint_chipinit;
1409 ata_highpoint_chipinit(device_t dev)
1411 struct ata_pci_controller *ctlr = device_get_softc(dev);
1412 int rid = ATA_IRQ_RID;
1414 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1415 RF_SHAREABLE | RF_ACTIVE))) {
1416 device_printf(dev, "unable to map interrupt\n");
1419 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1420 ata_highpoint_intr, ctlr, &ctlr->handle))) {
1421 device_printf(dev, "unable to setup interrupt\n");
1425 if (ctlr->chip->cfg2 == HPTOLD) {
1426 /* disable interrupt prediction */
1427 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
1430 /* disable interrupt prediction */
1431 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
1432 pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
1434 /* enable interrupts */
1435 pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
1437 /* set clocks etc */
1438 if (ctlr->chip->cfg1 < HPT372)
1439 pci_write_config(dev, 0x5b, 0x22, 1);
1441 pci_write_config(dev, 0x5b,
1442 (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
1444 ctlr->setmode = ata_highpoint_setmode;
1449 ata_highpoint_intr(void *data)
1451 struct ata_pci_controller *ctlr = data;
1452 struct ata_channel *ch;
1455 for (unit = 0; unit < ctlr->channels; unit++) {
1456 if (!(ch = ctlr->interrupt[unit].argument))
1459 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1461 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1462 ATA_BMSTAT_INTERRUPT)
1464 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1467 ctlr->interrupt[unit].function(ch);
1472 ata_highpoint_setmode(device_t dev, int mode)
1474 device_t gparent = GRANDPARENT(dev);
1475 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1476 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1477 struct ata_device *atadev = device_get_softc(dev);
1478 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1480 u_int32_t timings33[][4] = {
1481 /* HPT366 HPT370 HPT372 HPT374 mode */
1482 { 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a }, /* PIO 0 */
1483 { 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 }, /* PIO 1 */
1484 { 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 }, /* PIO 2 */
1485 { 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 }, /* PIO 3 */
1486 { 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 }, /* PIO 4 */
1487 { 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea }, /* MWDMA 0 */
1488 { 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 }, /* MWDMA 1 */
1489 { 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 }, /* MWDMA 2 */
1490 { 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea }, /* UDMA 0 */
1491 { 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 }, /* UDMA 1 */
1492 { 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 }, /* UDMA 2 */
1493 { 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 }, /* UDMA 3 */
1494 { 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 }, /* UDMA 4 */
1495 { 0, 0x16454e31, 0x1c8a9c62, 0x12848242 }, /* UDMA 5 */
1496 { 0, 0, 0x1c869c62, 0x12808242 } /* UDMA 6 */
1499 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1501 if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
1502 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1504 mode = ata_highpoint_check_80pin(dev, mode);
1507 * most if not all HPT chips cant really handle that the device is
1508 * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
1509 * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
1511 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
1512 ata_limit_mode(dev, mode, ATA_UDMA5));
1514 device_printf(dev, "%ssetting %s on HighPoint chip\n",
1515 (error) ? "FAILURE " : "", ata_mode2str(mode));
1517 pci_write_config(gparent, 0x40 + (devno << 2),
1518 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
1519 atadev->mode = mode;
1523 ata_highpoint_check_80pin(device_t dev, int mode)
1525 device_t gparent = GRANDPARENT(dev);
1526 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1527 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1528 u_int8_t reg, val, res;
1530 if (ctlr->chip->cfg1 == HPT374 && pci_get_function(gparent) == 1) {
1531 reg = ch->unit ? 0x57 : 0x53;
1532 val = pci_read_config(gparent, reg, 1);
1533 pci_write_config(gparent, reg, val | 0x80, 1);
1537 val = pci_read_config(gparent, reg, 1);
1538 pci_write_config(gparent, reg, val & 0xfe, 1);
1540 res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1541 pci_write_config(gparent, reg, val, 1);
1543 if (mode > ATA_UDMA2 && res) {
1544 ata_print_cable(dev, "controller");
1552 * Intel chipset support functions
1555 ata_intel_ident(device_t dev)
1557 struct ata_pci_controller *ctlr = device_get_softc(dev);
1558 struct ata_chip_id *idx;
1559 static struct ata_chip_id ids[] =
1560 {{ ATA_I82371FB, 0, 0, 0x00, ATA_WDMA2, "PIIX" },
1561 { ATA_I82371SB, 0, 0, 0x00, ATA_WDMA2, "PIIX3" },
1562 { ATA_I82371AB, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1563 { ATA_I82443MX, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1564 { ATA_I82451NX, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1565 { ATA_I82801AB, 0, 0, 0x00, ATA_UDMA2, "ICH0" },
1566 { ATA_I82801AA, 0, 0, 0x00, ATA_UDMA4, "ICH" },
1567 { ATA_I82372FB, 0, 0, 0x00, ATA_UDMA4, "ICH" },
1568 { ATA_I82801BA, 0, 0, 0x00, ATA_UDMA5, "ICH2" },
1569 { ATA_I82801BA_1, 0, 0, 0x00, ATA_UDMA5, "ICH2" },
1570 { ATA_I82801CA, 0, 0, 0x00, ATA_UDMA5, "ICH3" },
1571 { ATA_I82801CA_1, 0, 0, 0x00, ATA_UDMA5, "ICH3" },
1572 { ATA_I82801DB, 0, 0, 0x00, ATA_UDMA5, "ICH4" },
1573 { ATA_I82801DB_1, 0, 0, 0x00, ATA_UDMA5, "ICH4" },
1574 { ATA_I82801EB, 0, 0, 0x00, ATA_UDMA5, "ICH5" },
1575 { ATA_I82801EB_S1, 0, 0, 0x00, ATA_SA150, "ICH5" },
1576 { ATA_I82801EB_R1, 0, 0, 0x00, ATA_SA150, "ICH5" },
1577 { ATA_I6300ESB, 0, 0, 0x00, ATA_UDMA5, "6300ESB" },
1578 { ATA_I6300ESB_S1, 0, 0, 0x00, ATA_SA150, "6300ESB" },
1579 { ATA_I6300ESB_R1, 0, 0, 0x00, ATA_SA150, "6300ESB" },
1580 { ATA_I82801FB, 0, 0, 0x00, ATA_UDMA5, "ICH6" },
1581 { ATA_I82801FB_S1, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1582 { ATA_I82801FB_R1, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1583 { ATA_I82801FB_M, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1584 { ATA_I82801GB, 0, 0, 0x00, ATA_UDMA5, "ICH7" },
1585 { ATA_I82801GB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1586 { ATA_I82801GB_R1, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1587 { ATA_I82801GB_M, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1588 { ATA_I82801GB_AH, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1589 { ATA_I31244, 0, 0, 0x00, ATA_SA150, "31244" },
1590 { 0, 0, 0, 0, 0, 0}};
1593 if (!(idx = ata_match_chip(dev, ids)))
1596 sprintf(buffer, "Intel %s %s controller",
1597 idx->text, ata_mode2str(idx->max_dma));
1598 device_set_desc_copy(dev, buffer);
1600 ctlr->chipinit = ata_intel_chipinit;
1605 ata_intel_chipinit(device_t dev)
1607 struct ata_pci_controller *ctlr = device_get_softc(dev);
1608 int rid = ATA_IRQ_RID;
1610 if (!ata_legacy(dev)) {
1611 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1612 RF_SHAREABLE | RF_ACTIVE))) {
1613 device_printf(dev, "unable to map interrupt\n");
1616 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1617 ata_intel_intr, ctlr, &ctlr->handle))) {
1618 device_printf(dev, "unable to setup interrupt\n");
1623 /* good old PIIX needs special treatment (not implemented) */
1624 if (ctlr->chip->chipid == ATA_I82371FB) {
1625 ctlr->setmode = ata_intel_old_setmode;
1628 /* the intel 31244 needs special care if in DPA mode */
1629 else if (ctlr->chip->chipid == ATA_I31244) {
1630 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE) {
1631 ctlr->r_type2 = SYS_RES_MEMORY;
1632 ctlr->r_rid2 = PCIR_BAR(0);
1633 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1638 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
1639 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1640 ata_intel_31244_intr, ctlr, &ctlr->handle)) {
1641 device_printf(dev, "unable to setup interrupt\n");
1645 ctlr->reset = ata_intel_31244_reset;
1646 ctlr->allocate = ata_intel_31244_allocate;
1648 ctlr->setmode = ata_sata_setmode;
1651 /* non SATA intel chips goes here */
1652 else if (ctlr->chip->max_dma < ATA_SA150) {
1653 ctlr->setmode = ata_intel_new_setmode;
1656 /* SATA parts can be either compat or AHCI */
1658 /* force all ports active "the legacy way" */
1659 pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f,2);
1661 ctlr->reset = ata_intel_reset;
1663 /* if we have AHCI capability and BAR(5) as a memory resource */
1664 if (ctlr->chip->cfg1 == AHCI) {
1665 ctlr->r_type2 = SYS_RES_MEMORY;
1666 ctlr->r_rid2 = PCIR_BAR(5);
1667 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1670 /* is AHCI or RAID mode enabled in BIOS ? */
1671 if (pci_read_config(dev, 0x90, 1) & 0xc0) {
1672 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
1673 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1674 ata_ahci_intr, ctlr, &ctlr->handle)) {
1675 device_printf(dev, "unable to setup interrupt\n");
1679 /* enable AHCI mode */
1680 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC, ATA_AHCI_GHC_AE);
1682 /* get the number of HW channels */
1683 ctlr->channels = (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) &
1684 ATA_AHCI_NPMASK) + 1;
1686 /* enable AHCI interrupts */
1687 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
1688 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) |
1690 ctlr->reset = ata_ahci_reset;
1691 ctlr->dmainit = ata_ahci_dmainit;
1692 ctlr->allocate = ata_ahci_allocate;
1696 ctlr->setmode = ata_sata_setmode;
1698 /* enable PCI interrupt */
1699 pci_write_config(dev, PCIR_COMMAND,
1700 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1706 ata_intel_31244_allocate(device_t dev)
1708 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1709 struct ata_channel *ch = device_get_softc(dev);
1713 ch_offset = 0x200 + ch->unit * 0x200;
1715 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
1716 ch->r_io[i].res = ctlr->r_res2;
1717 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
1718 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x06;
1719 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
1720 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
1721 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
1722 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
1723 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
1724 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1d;
1725 ch->r_io[ATA_ERROR].offset = ch_offset + 0x04;
1726 ch->r_io[ATA_STATUS].offset = ch_offset + 0x1c;
1727 ch->r_io[ATA_ALTSTAT].offset = ch_offset + 0x28;
1728 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x29;
1729 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x100;
1730 ch->r_io[ATA_SERROR].offset = ch_offset + 0x104;
1731 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x108;
1732 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x70;
1733 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x72;
1734 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x74;
1736 ch->flags |= ATA_NO_SLAVE;
1737 ata_generic_hw(dev);
1738 ch->hw.command = ata_intel_31244_command;
1740 /* enable PHY state change interrupt */
1741 ATA_OUTL(ctlr->r_res2, 0x4,
1742 ATA_INL(ctlr->r_res2, 0x04) | (0x01 << (ch->unit << 3)));
1747 ata_intel_31244_intr(void *data)
1749 struct ata_pci_controller *ctlr = data;
1750 struct ata_channel *ch;
1753 for (unit = 0; unit < ctlr->channels; unit++) {
1754 if (!(ch = ctlr->interrupt[unit].argument))
1757 /* check for PHY related interrupts on SATA capable HW */
1758 if (ctlr->chip->max_dma >= ATA_SA150) {
1759 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
1760 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
1761 struct ata_connect_task *tp;
1764 /* clear error bits/interrupt */
1765 ATA_IDX_OUTL(ch, ATA_SERROR, error);
1767 /* if we have a connection event deal with it */
1768 if ((error & ATA_SE_PHY_CHANGED) &&
1769 (tp = (struct ata_connect_task *)
1770 malloc(sizeof(struct ata_connect_task),
1771 M_ATA, M_NOWAIT | M_ZERO))) {
1773 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
1774 device_printf(ch->dev, "CONNECT requested\n");
1775 tp->action = ATA_C_ATTACH;
1778 device_printf(ch->dev, "DISCONNECT requested\n");
1779 tp->action = ATA_C_DETACH;
1782 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1783 taskqueue_enqueue(taskqueue_thread, &tp->task);
1788 /* any drive action to take care of ? */
1789 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
1790 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1792 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
1794 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1797 ctlr->interrupt[unit].function(ch);
1802 ata_intel_31244_reset(device_t dev)
1804 struct ata_channel *ch = device_get_softc(dev);
1806 ata_sata_phy_enable(ch);
1810 ata_intel_31244_command(struct ata_request *request)
1812 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
1813 struct ata_device *atadev = device_get_softc(request->dev);
1816 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
1817 return (ata_generic_command(request));
1819 lba = request->u.ata.lba;
1820 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | atadev->unit);
1821 /* enable interrupt */
1822 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
1823 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
1824 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
1825 ATA_IDX_OUTW(ch, ATA_SECTOR, ((lba >> 16) & 0xff00) | (lba & 0x00ff));
1826 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((lba >> 24) & 0xff00) |
1827 ((lba >> 8) & 0x00ff));
1828 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((lba >> 32) & 0xff00) |
1829 ((lba >> 16) & 0x00ff));
1831 /* issue command to controller */
1832 ATA_IDX_OUTB(ch, ATA_COMMAND, request->u.ata.command);
1838 ata_intel_intr(void *data)
1840 struct ata_pci_controller *ctlr = data;
1841 struct ata_channel *ch;
1844 for (unit = 0; unit < ctlr->channels; unit++) {
1845 if (!(ch = ctlr->interrupt[unit].argument))
1848 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1850 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1851 ATA_BMSTAT_INTERRUPT)
1853 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1856 ctlr->interrupt[unit].function(ch);
1861 ata_intel_reset(device_t dev)
1863 device_t parent = device_get_parent(dev);
1864 struct ata_pci_controller *ctlr = device_get_softc(parent);
1865 struct ata_channel *ch = device_get_softc(dev);
1868 /* ICH6 & ICH7 in compat mode has 4 SATA ports as master/slave on 2 ch's */
1869 if (ctlr->chip->cfg1) {
1870 mask = (0x0005 << ch->unit);
1873 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1874 if (pci_read_config(parent, 0x90, 1) & 0x04)
1877 mask = (0x0001 << ch->unit);
1878 /* XXX SOS should be in intel_allocate if we grow it */
1879 ch->flags |= ATA_NO_SLAVE;
1882 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1884 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1886 /* wait up to 1 sec for "connect well" */
1887 for (timeout = 0; timeout < 100 ; timeout++) {
1888 if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1889 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1893 ata_generic_reset(dev);
1897 ata_intel_old_setmode(device_t dev, int mode)
1903 ata_intel_new_setmode(device_t dev, int mode)
1905 device_t gparent = GRANDPARENT(dev);
1906 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1907 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1908 struct ata_device *atadev = device_get_softc(dev);
1909 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1910 u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
1911 u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
1912 u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
1913 u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
1914 u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
1915 u_int32_t mask40 = 0, new40 = 0;
1916 u_int8_t mask44 = 0, new44 = 0;
1918 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1919 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1921 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1923 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1924 ata_print_cable(dev, "controller");
1928 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1931 device_printf(dev, "%ssetting %s on %s chip\n",
1932 (error) ? "FAILURE " : "",
1933 ata_mode2str(mode), ctlr->chip->text);
1937 if (mode >= ATA_UDMA0) {
1938 pci_write_config(gparent, 0x48, reg48 | (0x0001 << devno), 2);
1939 pci_write_config(gparent, 0x4a,
1940 (reg4a & ~(0x3 << (devno << 2))) |
1941 ((0x01 + !(mode & 0x01)) << (devno << 2)), 2);
1944 pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
1945 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))), 2);
1948 if (mode >= ATA_UDMA2)
1949 pci_write_config(gparent, 0x54, reg54 | (0x1 << devno), 2);
1951 pci_write_config(gparent, 0x54, reg54 & ~(0x1 << devno), 2);
1953 if (mode >= ATA_UDMA5)
1954 pci_write_config(gparent, 0x54, reg54 | (0x1000 << devno), 2);
1956 pci_write_config(gparent, 0x54, reg54 & ~(0x1000 << devno), 2);
1958 reg40 &= ~0x00ff00ff;
1959 reg40 |= 0x40774077;
1961 if (atadev->unit == ATA_MASTER) {
1963 new40 = timings[ata_mode2idx(mode)] << 8;
1967 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
1968 (timings[ata_mode2idx(mode)] & 0x03);
1976 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
1977 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
1979 atadev->mode = mode;
1984 * Integrated Technology Express Inc. (ITE) chipset support functions
1987 ata_ite_ident(device_t dev)
1989 struct ata_pci_controller *ctlr = device_get_softc(dev);
1990 struct ata_chip_id *idx;
1991 static struct ata_chip_id ids[] =
1992 {{ ATA_IT8212F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8212F" },
1993 { ATA_IT8211F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8211F" },
1994 { 0, 0, 0, 0, 0, 0}};
1997 if (!(idx = ata_match_chip(dev, ids)))
2000 sprintf(buffer, "ITE %s %s controller",
2001 idx->text, ata_mode2str(idx->max_dma));
2002 device_set_desc_copy(dev, buffer);
2004 ctlr->chipinit = ata_ite_chipinit;
2009 ata_ite_chipinit(device_t dev)
2011 struct ata_pci_controller *ctlr = device_get_softc(dev);
2013 if (ata_setup_interrupt(dev))
2016 ctlr->setmode = ata_ite_setmode;
2018 /* set PCI mode and 66Mhz reference clock */
2019 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
2021 /* set default active & recover timings */
2022 pci_write_config(dev, 0x54, 0x31, 1);
2023 pci_write_config(dev, 0x56, 0x31, 1);
2028 ata_ite_setmode(device_t dev, int mode)
2030 device_t gparent = GRANDPARENT(dev);
2031 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2032 struct ata_device *atadev = device_get_softc(dev);
2033 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2036 /* correct the mode for what the HW supports */
2037 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2039 /* check the CBLID bits for 80 conductor cable detection */
2040 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
2041 (ch->unit ? (1<<3) : (1<<2)))) {
2042 ata_print_cable(dev, "controller");
2046 /* set the wanted mode on the device */
2047 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2050 device_printf(dev, "%s setting %s on ITE8212F chip\n",
2051 (error) ? "failed" : "success", ata_mode2str(mode));
2053 /* if the device accepted the mode change, setup the HW accordingly */
2055 if (mode >= ATA_UDMA0) {
2056 u_int8_t udmatiming[] =
2057 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
2059 /* enable UDMA mode */
2060 pci_write_config(gparent, 0x50,
2061 pci_read_config(gparent, 0x50, 1) &
2062 ~(1 << (devno + 3)), 1);
2064 /* set UDMA timing */
2065 pci_write_config(gparent,
2066 0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
2067 udmatiming[mode & ATA_MODE_MASK], 1);
2070 u_int8_t chtiming[] =
2071 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
2073 /* disable UDMA mode */
2074 pci_write_config(gparent, 0x50,
2075 pci_read_config(gparent, 0x50, 1) |
2076 (1 << (devno + 3)), 1);
2078 /* set active and recover timing (shared between master & slave) */
2079 if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
2080 chtiming[ata_mode2idx(mode)])
2081 pci_write_config(gparent, 0x54 + (ch->unit << 2),
2082 chtiming[ata_mode2idx(mode)], 1);
2084 atadev->mode = mode;
2090 * Marvell chipset support functions
2092 #define ATA_MV_HOST_BASE(ch) \
2093 ((ch->unit & 3) * 0x0100) + (ch->unit > 3 ? 0x30000 : 0x20000)
2094 #define ATA_MV_EDMA_BASE(ch) \
2095 ((ch->unit & 3) * 0x2000) + (ch->unit > 3 ? 0x30000 : 0x20000)
2098 ata_marvell_ident(device_t dev)
2100 struct ata_pci_controller *ctlr = device_get_softc(dev);
2101 struct ata_chip_id *idx;
2102 static struct ata_chip_id ids[] =
2103 {{ ATA_M88SX5040, 0, 4, MV5XXX, ATA_SA150, "88SX5040" },
2104 { ATA_M88SX5041, 0, 4, MV5XXX, ATA_SA150, "88SX5041" },
2105 { ATA_M88SX5080, 0, 8, MV5XXX, ATA_SA150, "88SX5080" },
2106 { ATA_M88SX5081, 0, 8, MV5XXX, ATA_SA150, "88SX5081" },
2107 { ATA_M88SX6041, 0, 4, MV6XXX, ATA_SA300, "88SX6041" },
2108 { ATA_M88SX6081, 0, 8, MV6XXX, ATA_SA300, "88SX6081" },
2109 { 0, 0, 0, 0, 0, 0}};
2112 if (!(idx = ata_match_chip(dev, ids)))
2115 sprintf(buffer, "Marvell %s %s controller",
2116 idx->text, ata_mode2str(idx->max_dma));
2117 device_set_desc_copy(dev, buffer);
2119 ctlr->chipinit = ata_marvell_chipinit;
2124 ata_marvell_chipinit(device_t dev)
2126 struct ata_pci_controller *ctlr = device_get_softc(dev);
2127 int rid = ATA_IRQ_RID;
2129 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2130 RF_SHAREABLE | RF_ACTIVE))) {
2131 device_printf(dev, "unable to map interrupt\n");
2135 ctlr->r_type1 = SYS_RES_MEMORY;
2136 ctlr->r_rid1 = PCIR_BAR(0);
2137 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
2138 &ctlr->r_rid1, RF_ACTIVE)))
2141 /* mask all host controller interrupts */
2142 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x00000000);
2144 /* mask all PCI interrupts */
2145 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x00000000);
2147 ctlr->reset = ata_marvell_reset;
2148 ctlr->dmainit = ata_marvell_dmainit;
2149 ctlr->allocate = ata_marvell_allocate;
2150 ctlr->setmode = ata_sata_setmode;
2151 ctlr->channels = ctlr->chip->cfg1;
2153 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2154 ata_marvell_intr, ctlr, &ctlr->handle))) {
2155 device_printf(dev, "unable to setup interrupt\n");
2159 /* clear host controller interrupts */
2160 ATA_OUTL(ctlr->r_res1, 0x20014, 0x00000000);
2161 if (ctlr->chip->cfg1 > 4)
2162 ATA_OUTL(ctlr->r_res1, 0x30014, 0x00000000);
2164 /* clear PCI interrupts */
2165 ATA_OUTL(ctlr->r_res1, 0x01d58, 0x00000000);
2167 /* unmask PCI interrupts we want */
2168 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x007fffff);
2170 /* unmask host controller interrupts we want */
2171 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x000000ff/*HC0*/ | 0x0001fe00/*HC1*/ |
2172 /*(1<<19) | (1<<20) | (1<<21) |*/(1<<22) | (1<<24) | (0x7f << 25));
2174 pci_write_config(dev, PCIR_COMMAND,
2175 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
2180 ata_marvell_allocate(device_t dev)
2182 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2183 struct ata_channel *ch = device_get_softc(dev);
2184 bus_addr_t wordp = ch->dma->work_bus;
2187 /* set legacy ATA resources */
2188 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2189 ch->r_io[i].res = ctlr->r_res1;
2190 ch->r_io[i].offset = 0x02100 + (i << 2) + ATA_MV_EDMA_BASE(ch);
2192 ch->r_io[ATA_CONTROL].res = ctlr->r_res1;
2193 ch->r_io[ATA_CONTROL].offset = 0x02120 + ATA_MV_EDMA_BASE(ch);
2194 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res1;
2195 ata_default_registers(dev);
2197 /* set SATA resources */
2198 switch (ctlr->chip->cfg2) {
2200 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2201 ch->r_io[ATA_SSTATUS].offset = 0x00100 + ATA_MV_HOST_BASE(ch);
2202 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2203 ch->r_io[ATA_SERROR].offset = 0x00104 + ATA_MV_HOST_BASE(ch);
2204 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2205 ch->r_io[ATA_SCONTROL].offset = 0x00108 + ATA_MV_HOST_BASE(ch);
2208 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2209 ch->r_io[ATA_SSTATUS].offset = 0x02300 + ATA_MV_EDMA_BASE(ch);
2210 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2211 ch->r_io[ATA_SERROR].offset = 0x02304 + ATA_MV_EDMA_BASE(ch);
2212 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2213 ch->r_io[ATA_SCONTROL].offset = 0x02308 + ATA_MV_EDMA_BASE(ch);
2214 ch->r_io[ATA_SACTIVE].res = ctlr->r_res1;
2215 ch->r_io[ATA_SACTIVE].offset = 0x02350 + ATA_MV_EDMA_BASE(ch);
2219 ch->flags |= ATA_NO_SLAVE;
2220 ch->flags |= ATA_USE_16BIT; /* XXX SOS needed ? */
2221 ata_generic_hw(dev);
2222 ch->hw.command = ata_marvell_command;
2224 /* disable the EDMA machinery */
2225 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2226 DELAY(100000); /* SOS should poll for disabled */
2228 /* set configuration to non-queued 128b read transfers stop on error */
2229 ATA_OUTL(ctlr->r_res1, 0x02000 + ATA_MV_EDMA_BASE(ch), (1<<11) | (1<<13));
2231 /* request queue base high */
2232 ATA_OUTL(ctlr->r_res1, 0x02010 + ATA_MV_EDMA_BASE(ch), (wordp >> 16) >> 16);
2234 /* request queue in ptr */
2235 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), wordp & 0xffffffff);
2237 /* request queue out ptr */
2238 ATA_OUTL(ctlr->r_res1, 0x02018 + ATA_MV_EDMA_BASE(ch), 0x0);
2240 /* response queue base high */
2242 ATA_OUTL(ctlr->r_res1, 0x0201c + ATA_MV_EDMA_BASE(ch), (wordp >> 16) >> 16);
2244 /* response queue in ptr */
2245 ATA_OUTL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch), 0x0);
2247 /* response queue out ptr */
2248 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), wordp & 0xffffffff);
2250 /* clear SATA error register */
2251 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2253 /* clear any outstanding error interrupts */
2254 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2256 /* unmask all error interrupts */
2257 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2259 /* enable EDMA machinery */
2260 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2264 struct ata_marvell_response {
2266 u_int8_t edma_status;
2267 u_int8_t dev_status;
2268 u_int32_t timestamp;
2272 ata_marvell_intr(void *data)
2274 struct ata_pci_controller *ctlr = data;
2275 struct ata_channel *ch;
2276 struct ata_request *request;
2277 struct ata_marvell_response *response;
2278 u_int32_t cause, icr0 = 0, icr1 = 0;
2281 cause = ATA_INL(ctlr->r_res1, 0x01d60);
2282 if ((icr0 = ATA_INL(ctlr->r_res1, 0x20014)))
2283 ATA_OUTL(ctlr->r_res1, 0x20014, ~icr0);
2284 if (ctlr->channels > 4) {
2285 if ((icr1 = ATA_INL(ctlr->r_res1, 0x30014)))
2286 ATA_OUTL(ctlr->r_res1, 0x30014, ~icr1);
2289 for (unit = 0; unit < ctlr->channels; unit++) {
2293 if (!(ch = ctlr->interrupt[unit].argument))
2296 mtx_lock(&ch->state_mtx);
2305 /* do we have any errors flagged ? */
2306 if (cause & (1 << shift)) {
2307 /* clear SATA error register */
2308 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2310 /* clear any outstanding error interrupts */
2311 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2314 /* EDMA interrupt */
2315 if ((icr & (0x0001 << (unit & 3))) && (request = ch->running)) {
2316 u_int32_t rsp_in, rsp_out;
2319 request->dmastat = ch->dma->stop(request->dev);
2320 ch->dma->unload(ch->dev);
2321 callout_stop(&request->callout);
2323 /* get response ptr's */
2324 rsp_in = ATA_INL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch));
2325 rsp_out = ATA_INL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch));
2326 slot = (((rsp_in & ~0xffffff00) >> 3)) & 0x1f;
2327 rsp_out &= 0xffffff00;
2328 rsp_out += (slot << 3);
2329 response = (struct ata_marvell_response *)
2330 (u_int8_t *)(ch->dma->work) + 1024 + (slot << 3);
2332 /* record status for this request */
2333 request->status = response->dev_status;
2337 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), rsp_out);
2339 /* update progress */
2340 if (!(request->status & ATA_S_ERROR) &&
2341 !(request->flags & ATA_R_TIMEOUT))
2342 request->donecount = request->bytecount;
2344 /* finish up this request */
2346 if (ch->state == ATA_ACTIVE)
2347 ch->state = ATA_IDLE;
2348 mtx_unlock(&ch->state_mtx);
2349 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
2350 ata_finish(request);
2353 /* legacy ATA interrupt */
2354 else if (icr & (0x0100 << (unit & 3))) {
2355 mtx_unlock(&ch->state_mtx);
2356 ctlr->interrupt[unit].function(ch);
2359 /* no device action to handle */
2361 mtx_unlock(&ch->state_mtx);
2366 struct ata_marvell_dma_prdentry {
2374 ata_marvell_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
2376 struct ata_dmasetprd_args *args = xsc;
2377 struct ata_marvell_dma_prdentry *prd = args->dmatab;
2380 if ((args->error = error))
2383 for (i = 0; i < nsegs; i++) {
2384 prd[i].addrlo = htole32(segs[i].ds_addr);
2386 prd[i].count = htole32(segs[i].ds_len);
2388 prd[i - 1].count |= htole32(ATA_DMA_EOT);
2392 ata_marvell_dmastart(device_t dev)
2394 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2396 ch->flags |= ATA_DMA_ACTIVE;
2401 ata_marvell_dmastop(device_t dev)
2403 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2405 ch->flags &= ~ATA_DMA_ACTIVE;
2407 /* get status XXX SOS */
2412 ata_marvell_dmainit(device_t dev)
2414 struct ata_channel *ch = device_get_softc(dev);
2418 ch->dma->start = ata_marvell_dmastart;
2419 ch->dma->stop = ata_marvell_dmastop;
2420 ch->dma->setprd = ata_marvell_dmasetprd;
2425 ata_marvell_command(struct ata_request *request)
2427 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2428 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2436 /* only DMA R/W goes through the EMDA machine */
2437 if (request->u.ata.command != ATA_READ_DMA &&
2438 request->u.ata.command != ATA_READ_DMA48 &&
2439 request->u.ata.command != ATA_WRITE_DMA &&
2440 request->u.ata.command != ATA_WRITE_DMA48) {
2442 /* disable the EDMA machinery */
2443 if (ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)
2444 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2445 return ata_generic_command(request);
2448 /* get next free request queue slot */
2449 req_in = ATA_INL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch));
2450 slot = (((req_in & ~0xfffffc00) >> 5) + 0) & 0x1f;
2451 bytep = (u_int8_t *)(ch->dma->work);
2452 bytep += (slot << 5);
2453 wordp = (u_int16_t *)bytep;
2454 quadp = (u_int32_t *)bytep;
2456 /* fill in this request */
2457 quadp[0] = (long)ch->dma->sg_bus & 0xffffffff;
2458 quadp[1] = (ch->dma->sg_bus & 0xffffffff00000000) >> 32;
2459 wordp[4] = (request->flags & ATA_R_READ ? 0x01 : 0x00) | (tag<<1);
2462 bytep[i++] = (request->u.ata.count >> 8) & 0xff;
2463 bytep[i++] = 0x10 | ATA_COUNT;
2464 bytep[i++] = request->u.ata.count & 0xff;
2465 bytep[i++] = 0x10 | ATA_COUNT;
2467 bytep[i++] = (request->u.ata.lba >> 24) & 0xff;
2468 bytep[i++] = 0x10 | ATA_SECTOR;
2469 bytep[i++] = request->u.ata.lba & 0xff;
2470 bytep[i++] = 0x10 | ATA_SECTOR;
2472 bytep[i++] = (request->u.ata.lba >> 32) & 0xff;
2473 bytep[i++] = 0x10 | ATA_CYL_LSB;
2474 bytep[i++] = (request->u.ata.lba >> 8) & 0xff;
2475 bytep[i++] = 0x10 | ATA_CYL_LSB;
2477 bytep[i++] = (request->u.ata.lba >> 40) & 0xff;
2478 bytep[i++] = 0x10 | ATA_CYL_MSB;
2479 bytep[i++] = (request->u.ata.lba >> 16) & 0xff;
2480 bytep[i++] = 0x10 | ATA_CYL_MSB;
2482 bytep[i++] = ATA_D_LBA | ATA_D_IBM | ((request->u.ata.lba >> 24) & 0xf);
2483 bytep[i++] = 0x10 | ATA_DRIVE;
2484 bytep[i++] = request->u.ata.command;
2485 bytep[i++] = 0x90 | ATA_COMMAND;
2487 /* enable EDMA machinery if needed */
2488 if (!(ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)) {
2489 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2490 while (!(ATA_INL(ctlr->r_res1,
2491 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
2495 /* tell EDMA it has a new request */
2496 slot = (((req_in & ~0xfffffc00) >> 5) + 1) & 0x1f;
2497 req_in &= 0xfffffc00;
2498 req_in += (slot << 5);
2499 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), req_in);
2505 ata_marvell_reset(device_t dev)
2507 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2508 struct ata_channel *ch = device_get_softc(dev);
2510 /* disable the EDMA machinery */
2511 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2512 while ((ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
2515 /* clear SATA error register */
2516 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2518 /* clear any outstanding error interrupts */
2519 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2521 /* unmask all error interrupts */
2522 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2524 /* enable channel and test for devices */
2525 ata_sata_phy_enable(ch);
2527 /* enable EDMA machinery */
2528 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2533 * National chipset support functions
2536 ata_national_ident(device_t dev)
2538 struct ata_pci_controller *ctlr = device_get_softc(dev);
2540 /* this chip is a clone of the Cyrix chip, bugs and all */
2541 if (pci_get_devid(dev) == ATA_SC1100) {
2542 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
2543 ctlr->chipinit = ata_national_chipinit;
2549 static device_t nat_host = NULL;
2552 ata_national_chipinit(device_t dev)
2554 struct ata_pci_controller *ctlr = device_get_softc(dev);
2558 if (ata_setup_interrupt(dev))
2561 /* locate the ISA part in the southbridge and enable UDMA33 */
2562 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
2563 for (i = 0; i < nchildren; i++) {
2564 if (pci_get_devid(children[i]) == 0x0510100b) {
2565 nat_host = children[i];
2569 free(children, M_TEMP);
2571 ctlr->setmode = ata_national_setmode;
2576 ata_national_setmode(device_t dev, int mode)
2578 device_t gparent = GRANDPARENT(dev);
2579 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2580 struct ata_device *atadev = device_get_softc(dev);
2581 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2582 u_int32_t piotiming[] =
2583 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
2584 0x00803020, 0x20102010, 0x00100010,
2585 0x00100010, 0x00100010, 0x00100010 };
2586 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
2587 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
2590 ch->dma->alignment = 16;
2591 ch->dma->max_iosize = 126 * DEV_BSIZE;
2593 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2595 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2598 device_printf(dev, "%s setting %s on National chip\n",
2599 (error) ? "failed" : "success", ata_mode2str(mode));
2601 if (mode >= ATA_UDMA0) {
2602 pci_write_config(gparent, 0x44 + (devno << 3),
2603 udmatiming[mode & ATA_MODE_MASK], 4);
2605 else if (mode >= ATA_WDMA0) {
2606 pci_write_config(gparent, 0x44 + (devno << 3),
2607 dmatiming[mode & ATA_MODE_MASK], 4);
2610 pci_write_config(gparent, 0x44 + (devno << 3),
2611 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
2614 pci_write_config(gparent, 0x40 + (devno << 3),
2615 piotiming[ata_mode2idx(mode)], 4);
2616 atadev->mode = mode;
2622 * nVidia chipset support functions
2625 ata_nvidia_ident(device_t dev)
2627 struct ata_pci_controller *ctlr = device_get_softc(dev);
2628 struct ata_chip_id *idx;
2629 static struct ata_chip_id ids[] =
2630 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nForce" },
2631 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2" },
2632 { ATA_NFORCE2_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2 Pro" },
2633 { ATA_NFORCE2_PRO_S1, 0, 0, 0, ATA_SA150, "nForce2 Pro" },
2634 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3" },
2635 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3 Pro" },
2636 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
2637 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
2638 { ATA_NFORCE_MCP04, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP" },
2639 { ATA_NFORCE_MCP04_S1, 0, 0, NV4OFF, ATA_SA150, "nForce MCP" },
2640 { ATA_NFORCE_MCP04_S2, 0, 0, NV4OFF, ATA_SA150, "nForce MCP" },
2641 { ATA_NFORCE_CK804, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce CK804" },
2642 { ATA_NFORCE_CK804_S1, 0, 0, NV4OFF, ATA_SA300, "nForce CK804" },
2643 { ATA_NFORCE_CK804_S2, 0, 0, NV4OFF, ATA_SA300, "nForce CK804" },
2644 { ATA_NFORCE_MCP51, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP51" },
2645 { ATA_NFORCE_MCP51_S1, 0, 0, NV4OFF, ATA_SA300, "nForce MCP51" },
2646 { ATA_NFORCE_MCP51_S2, 0, 0, NV4OFF, ATA_SA300, "nForce MCP51" },
2647 { ATA_NFORCE_MCP55, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP55" },
2648 { ATA_NFORCE_MCP55_S1, 0, 0, NV4OFF, ATA_SA300, "nForce MCP55" },
2649 { ATA_NFORCE_MCP55_S2, 0, 0, NV4OFF, ATA_SA300, "nForce MCP55" },
2650 { 0, 0, 0, 0, 0, 0}} ;
2653 if (!(idx = ata_match_chip(dev, ids)))
2656 sprintf(buffer, "nVidia %s %s controller",
2657 idx->text, ata_mode2str(idx->max_dma));
2658 device_set_desc_copy(dev, buffer);
2660 ctlr->chipinit = ata_nvidia_chipinit;
2665 ata_nvidia_chipinit(device_t dev)
2667 struct ata_pci_controller *ctlr = device_get_softc(dev);
2669 if (ata_setup_interrupt(dev))
2672 if (ctlr->chip->max_dma >= ATA_SA150) {
2673 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
2674 ctlr->r_type2 = SYS_RES_IOPORT;
2676 ctlr->r_type2 = SYS_RES_MEMORY;
2677 ctlr->r_rid2 = PCIR_BAR(5);
2678 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2679 &ctlr->r_rid2, RF_ACTIVE))) {
2680 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
2682 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
2683 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2684 ata_nvidia_intr, ctlr, &ctlr->handle)) {
2685 device_printf(dev, "unable to setup interrupt\n");
2689 /* enable control access */
2690 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
2692 /* clear interrupt status */
2693 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2695 /* enable device and PHY state change interrupts */
2696 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
2698 /* enable PCI interrupt */
2699 pci_write_config(dev, PCIR_COMMAND,
2700 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
2702 ctlr->allocate = ata_nvidia_allocate;
2703 ctlr->reset = ata_nvidia_reset;
2705 ctlr->setmode = ata_sata_setmode;
2708 /* disable prefetch, postwrite */
2709 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
2710 ctlr->setmode = ata_via_family_setmode;
2716 ata_nvidia_allocate(device_t dev)
2718 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2719 struct ata_channel *ch = device_get_softc(dev);
2721 /* setup the usual register normal pci style */
2722 if (ata_pci_allocate(dev))
2725 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2726 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
2727 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2728 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
2729 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2730 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
2731 ch->flags |= ATA_NO_SLAVE;
2737 ata_nvidia_intr(void *data)
2739 struct ata_pci_controller *ctlr = data;
2740 struct ata_channel *ch;
2741 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
2745 /* get interrupt status */
2746 status = ATA_INB(ctlr->r_res2, offset);
2748 /* clear interrupt status */
2749 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2751 for (unit = 0; unit < ctlr->channels; unit++) {
2752 if ((ch = ctlr->interrupt[unit].argument)) {
2753 struct ata_connect_task *tp;
2754 int maskshift = ch->unit << 2;
2756 /* clear error status */
2757 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2759 /* check for and handle connect events */
2760 if (((status & (0x0c << maskshift)) == (0x04 << maskshift)) &&
2761 (tp = (struct ata_connect_task *)
2762 malloc(sizeof(struct ata_connect_task),
2763 M_ATA, M_NOWAIT | M_ZERO))) {
2765 device_printf(ch->dev, "CONNECT requested\n");
2766 tp->action = ATA_C_ATTACH;
2768 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2769 taskqueue_enqueue(taskqueue_thread, &tp->task);
2772 /* check for and handle disconnect events */
2773 if ((status & (0x08 << maskshift)) &&
2774 (tp = (struct ata_connect_task *)
2775 malloc(sizeof(struct ata_connect_task),
2776 M_ATA, M_NOWAIT | M_ZERO))) {
2778 device_printf(ch->dev, "DISCONNECT requested\n");
2779 tp->action = ATA_C_DETACH;
2781 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2782 taskqueue_enqueue(taskqueue_thread, &tp->task);
2785 /* any drive action to take care of ? */
2786 if (status & (0x01 << maskshift)) {
2787 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2789 ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2791 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
2793 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat&~ATA_BMSTAT_ERROR);
2796 ctlr->interrupt[unit].function(ch);
2803 ata_nvidia_reset(device_t dev)
2805 struct ata_channel *ch = device_get_softc(dev);
2807 ata_sata_phy_enable(ch);
2812 * Promise chipset support functions
2814 #define ATA_PDC_APKT_OFFSET 0x00000010
2815 #define ATA_PDC_HPKT_OFFSET 0x00000040
2816 #define ATA_PDC_ASG_OFFSET 0x00000080
2817 #define ATA_PDC_LSG_OFFSET 0x000000c0
2818 #define ATA_PDC_HSG_OFFSET 0x00000100
2819 #define ATA_PDC_CHN_OFFSET 0x00000400
2820 #define ATA_PDC_BUF_BASE 0x00400000
2821 #define ATA_PDC_BUF_OFFSET 0x00100000
2822 #define ATA_PDC_MAX_HPKT 8
2823 #define ATA_PDC_WRITE_REG 0x00
2824 #define ATA_PDC_WRITE_CTL 0x0e
2825 #define ATA_PDC_WRITE_END 0x08
2826 #define ATA_PDC_WAIT_NBUSY 0x10
2827 #define ATA_PDC_WAIT_READY 0x18
2828 #define ATA_PDC_1B 0x20
2829 #define ATA_PDC_2B 0x40
2831 struct host_packet {
2833 TAILQ_ENTRY(host_packet) chain;
2836 struct ata_promise_sx4 {
2838 TAILQ_HEAD(, host_packet) queue;
2843 ata_promise_ident(device_t dev)
2845 struct ata_pci_controller *ctlr = device_get_softc(dev);
2846 struct ata_chip_id *idx;
2847 static struct ata_chip_id ids[] =
2848 {{ ATA_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "PDC20246" },
2849 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20262" },
2850 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20263" },
2851 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20265" },
2852 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20267" },
2853 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20268" },
2854 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "PDC20269" },
2855 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20270" },
2856 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "PDC20271" },
2857 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "PDC20275" },
2858 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "PDC20276" },
2859 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "PDC20277" },
2860 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "PDC20318" },
2861 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "PDC20319" },
2862 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20371" },
2863 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20375" },
2864 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20376" },
2865 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20377" },
2866 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20378" },
2867 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20379" },
2868 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20571" },
2869 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20575" },
2870 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20579" },
2871 { ATA_PDC20580, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20580" },
2872 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20617" },
2873 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20618" },
2874 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20619" },
2875 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20620" },
2876 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "PDC20621" },
2877 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "PDC20622" },
2878 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40518" },
2879 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40519" },
2880 { ATA_PDC40718, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40718" },
2881 { ATA_PDC40719, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40719" },
2882 { 0, 0, 0, 0, 0, 0}};
2884 uintptr_t devid = 0;
2886 if (!(idx = ata_match_chip(dev, ids)))
2889 /* if we are on a SuperTrak SX6000 dont attach */
2890 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
2891 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
2892 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
2893 devid == ATA_I960RM)
2896 strcpy(buffer, idx->text);
2898 /* if we are on a FastTrak TX4, adjust the interrupt resource */
2899 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
2900 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
2901 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
2902 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
2903 static long start = 0, end = 0;
2905 if (pci_get_slot(dev) == 1) {
2906 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
2907 strcat(buffer, " (channel 0+1)");
2909 else if (pci_get_slot(dev) == 2 && start && end) {
2910 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
2912 strcat(buffer, " (channel 2+3)");
2918 sprintf(buffer, "Promise %s %s controller",
2919 buffer, ata_mode2str(idx->max_dma));
2920 device_set_desc_copy(dev, buffer);
2922 ctlr->chipinit = ata_promise_chipinit;
2927 ata_promise_chipinit(device_t dev)
2929 struct ata_pci_controller *ctlr = device_get_softc(dev);
2930 int rid = ATA_IRQ_RID;
2932 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2933 RF_SHAREABLE | RF_ACTIVE))) {
2934 device_printf(dev, "unable to map interrupt\n");
2938 if (ctlr->chip->max_dma >= ATA_SA150)
2939 ctlr->setmode = ata_sata_setmode;
2941 ctlr->setmode = ata_promise_setmode;
2943 switch (ctlr->chip->cfg1) {
2946 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
2948 ctlr->dmainit = ata_promise_new_dmainit;
2952 /* enable burst mode */
2953 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
2955 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2956 ata_promise_old_intr, ctlr, &ctlr->handle))) {
2957 device_printf(dev, "unable to setup interrupt\n");
2963 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2964 ata_promise_tx2_intr, ctlr, &ctlr->handle))) {
2965 device_printf(dev, "unable to setup interrupt\n");
2971 // if (ctlr->r_res1)
2972 // bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
2973 ctlr->r_type1 = SYS_RES_MEMORY;
2974 ctlr->r_rid1 = PCIR_BAR(4);
2975 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
2976 &ctlr->r_rid1, RF_ACTIVE)))
2979 ctlr->r_type2 = SYS_RES_MEMORY;
2980 ctlr->r_rid2 = PCIR_BAR(3);
2981 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2982 &ctlr->r_rid2, RF_ACTIVE))){
2983 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
2986 ctlr->reset = ata_promise_mio_reset;
2987 ctlr->dmainit = ata_promise_mio_dmainit;
2988 ctlr->allocate = ata_promise_mio_allocate;
2990 if (ctlr->chip->cfg2 == PRSX4X) {
2991 struct ata_promise_sx4 *hpkt;
2992 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
2994 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2995 ata_promise_sx4_intr, ctlr, &ctlr->handle))) {
2996 device_printf(dev, "unable to setup interrupt\n");
2997 /* XXX SOS release resources */
3001 /* print info about cache memory */
3002 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
3003 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
3004 ((dimm >> 24) & 0xff),
3005 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
3006 " ECC enabled" : "" );
3008 /* adjust cache memory parameters */
3009 ATA_OUTL(ctlr->r_res2, 0x000c000c,
3010 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
3012 /* setup host packet controls */
3013 hpkt = malloc(sizeof(struct ata_promise_sx4),
3014 M_TEMP, M_NOWAIT | M_ZERO);
3015 mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
3016 TAILQ_INIT(&hpkt->queue);
3017 hpkt->busy = 0; //hpkt->head = hpkt->tail = 0;
3018 device_set_ivars(dev, hpkt);
3023 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3024 ata_promise_mio_intr, ctlr, &ctlr->handle))) {
3025 device_printf(dev, "unable to setup interrupt\n");
3026 /* XXX SOS release resources */
3030 switch (ctlr->chip->cfg2) {
3032 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
3033 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
3037 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
3038 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 3;
3042 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
3047 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
3052 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
3057 /* XXX SOS release resources */
3061 /* XXX SOS release resources */
3068 ata_promise_mio_allocate(device_t dev)
3070 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3071 struct ata_channel *ch = device_get_softc(dev);
3072 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
3075 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
3076 ch->r_io[i].res = ctlr->r_res2;
3077 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
3079 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
3080 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
3081 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
3082 ata_default_registers(dev);
3083 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
3084 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
3085 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3086 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
3087 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3088 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
3089 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3090 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
3091 ch->flags |= ATA_NO_SLAVE;
3093 ch->flags |= ATA_USE_16BIT;
3095 ata_generic_hw(dev);
3097 ch->hw.command = ata_promise_sx4_command;
3099 ch->hw.command = ata_promise_mio_command;
3104 ata_promise_mio_intr(void *data)
3106 struct ata_pci_controller *ctlr = data;
3107 struct ata_channel *ch;
3108 u_int32_t vector = 0, status = 0;
3111 switch (ctlr->chip->cfg2) {
3114 /* read and acknowledge interrupt(s) */
3115 vector = ATA_INL(ctlr->r_res2, 0x040);
3117 /* read and clear interface status */
3118 status = ATA_INL(ctlr->r_res2, 0x06c);
3119 ATA_OUTL(ctlr->r_res2, 0x06c, status & 0x000000ff);
3124 /* read and acknowledge interrupt(s) */
3125 vector = ATA_INL(ctlr->r_res2, 0x040);
3126 ATA_OUTL(ctlr->r_res2, 0x040, vector & 0x0000ffff);
3128 /* read and clear interface status */
3129 status = ATA_INL(ctlr->r_res2, 0x060);
3130 ATA_OUTL(ctlr->r_res2, 0x060, status & 0x000000ff);
3134 for (unit = 0; unit < ctlr->channels; unit++) {
3136 if ((ch = ctlr->interrupt[unit].argument)) {
3137 struct ata_connect_task *tp;
3139 /* check for and handle disconnect events */
3140 if ((status & (0x00000001 << unit)) &&
3141 (tp = (struct ata_connect_task *)
3142 malloc(sizeof(struct ata_connect_task),
3143 M_ATA, M_NOWAIT | M_ZERO))) {
3146 device_printf(ch->dev, "DISCONNECT requested\n");
3147 tp->action = ATA_C_DETACH;
3149 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3150 taskqueue_enqueue(taskqueue_thread, &tp->task);
3153 /* check for and handle connect events */
3154 if ((status & (0x00000010 << unit)) &&
3155 (tp = (struct ata_connect_task *)
3156 malloc(sizeof(struct ata_connect_task),
3157 M_ATA, M_NOWAIT | M_ZERO))) {
3160 device_printf(ch->dev, "CONNECT requested\n");
3161 tp->action = ATA_C_ATTACH;
3163 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3164 taskqueue_enqueue(taskqueue_thread, &tp->task);
3167 /* active interrupt(s) need to call the interrupt handler */
3168 if (vector & (1 << (unit + 1)))
3169 if ((ch = ctlr->interrupt[unit].argument))
3170 ctlr->interrupt[unit].function(ch);
3176 ata_promise_sx4_intr(void *data)
3178 struct ata_pci_controller *ctlr = data;
3179 struct ata_channel *ch;
3180 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
3183 for (unit = 0; unit < ctlr->channels; unit++) {
3184 if (vector & (1 << (unit + 1)))
3185 if ((ch = ctlr->interrupt[unit].argument))
3186 ctlr->interrupt[unit].function(ch);
3187 if (vector & (1 << (unit + 5)))
3188 if ((ch = ctlr->interrupt[unit].argument))
3189 ata_promise_queue_hpkt(ctlr,
3190 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
3191 ATA_PDC_HPKT_OFFSET));
3192 if (vector & (1 << (unit + 9))) {
3193 ata_promise_next_hpkt(ctlr);
3194 if ((ch = ctlr->interrupt[unit].argument))
3195 ctlr->interrupt[unit].function(ch);
3197 if (vector & (1 << (unit + 13))) {
3198 ata_promise_next_hpkt(ctlr);
3199 if ((ch = ctlr->interrupt[unit].argument))
3200 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
3201 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
3202 ATA_PDC_APKT_OFFSET));
3208 ata_promise_mio_dmainit(device_t dev)
3210 struct ata_channel *ch = device_get_softc(dev);
3214 /* note start and stop are not used here */
3219 ata_promise_mio_reset(device_t dev)
3221 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3222 struct ata_channel *ch = device_get_softc(dev);
3223 struct ata_promise_sx4 *hpktp;
3225 switch (ctlr->chip->cfg2) {
3228 /* softreset channel ATA module */
3229 hpktp = device_get_ivars(ctlr->dev);
3230 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
3232 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
3233 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
3234 ~0x00003f9f) | (ch->unit + 1));
3236 /* softreset HOST module */ /* XXX SOS what about other outstandings */
3237 mtx_lock(&hpktp->mtx);
3238 ATA_OUTL(ctlr->r_res2, 0xc012c,
3239 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
3241 ATA_OUTL(ctlr->r_res2, 0xc012c,
3242 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
3244 mtx_unlock(&hpktp->mtx);
3245 ata_generic_reset(dev);
3250 if ((ctlr->chip->cfg2 == PRSATA) ||
3251 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3253 /* mask plug/unplug intr */
3254 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
3257 /* softreset channels ATA module */
3258 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
3260 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
3261 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
3262 ~0x00003f9f) | (ch->unit + 1));
3264 if ((ctlr->chip->cfg2 == PRSATA) ||
3265 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3267 ata_sata_phy_enable(ch);
3269 /* reset and enable plug/unplug intr */
3270 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
3273 ata_generic_reset(dev);
3278 if ((ctlr->chip->cfg2 == PRSATA2) ||
3279 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
3280 /* set portmultiplier port */
3281 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
3283 /* mask plug/unplug intr */
3284 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
3287 /* softreset channels ATA module */
3288 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
3290 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
3291 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
3292 ~0x00003f9f) | (ch->unit + 1));
3294 if ((ctlr->chip->cfg2 == PRSATA2) ||
3295 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
3297 /* set PHY mode to "improved" */
3298 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
3299 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
3300 ~0x00000003) | 0x00000001);
3302 ata_sata_phy_enable(ch);
3304 /* reset and enable plug/unplug intr */
3305 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
3307 /* set portmultiplier port */
3308 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
3311 ata_generic_reset(dev);
3318 ata_promise_mio_command(struct ata_request *request)
3320 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
3321 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
3322 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
3324 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
3326 /* XXX SOS add ATAPI commands support later */
3327 switch (request->u.ata.command) {
3329 return ata_generic_command(request);
3332 case ATA_READ_DMA48:
3333 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
3337 case ATA_WRITE_DMA48:
3338 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
3341 wordp[1] = htole32(ch->dma->sg_bus);
3343 ata_promise_apkt((u_int8_t*)wordp, request);
3345 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
3350 ata_promise_sx4_command(struct ata_request *request)
3352 device_t gparent = GRANDPARENT(request->dev);
3353 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3354 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
3355 struct ata_dma_prdentry *prd = ch->dma->sg;
3356 caddr_t window = rman_get_virtual(ctlr->r_res1);
3358 int i, idx, length = 0;
3360 /* XXX SOS add ATAPI commands support later */
3361 switch (request->u.ata.command) {
3366 case ATA_ATA_IDENTIFY:
3370 case ATA_READ_MUL48:
3374 case ATA_WRITE_MUL48:
3375 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
3376 return ata_generic_command(request);
3378 case ATA_SETFEATURES:
3379 case ATA_FLUSHCACHE:
3380 case ATA_FLUSHCACHE48:
3383 wordp = (u_int32_t *)
3384 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
3385 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
3388 ata_promise_apkt((u_int8_t *)wordp, request);
3389 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
3390 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
3391 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
3392 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
3396 case ATA_READ_DMA48:
3398 case ATA_WRITE_DMA48:
3399 wordp = (u_int32_t *)
3400 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
3403 wordp[idx++] = prd[i].addr;
3404 wordp[idx++] = prd[i].count;
3405 length += (prd[i].count & ~ATA_DMA_EOT);
3406 } while (!(prd[i++].count & ATA_DMA_EOT));
3408 wordp = (u_int32_t *)
3409 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
3410 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
3411 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
3413 wordp = (u_int32_t *)
3414 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
3415 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
3416 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
3418 wordp = (u_int32_t *)
3419 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
3420 if (request->flags & ATA_R_READ)
3421 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
3422 if (request->flags & ATA_R_WRITE)
3423 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
3424 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
3425 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
3428 wordp = (u_int32_t *)
3429 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
3430 if (request->flags & ATA_R_READ)
3431 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
3432 if (request->flags & ATA_R_WRITE)
3433 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
3434 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
3436 ata_promise_apkt((u_int8_t *)wordp, request);
3437 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
3439 if (request->flags & ATA_R_READ) {
3440 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
3441 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
3442 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
3443 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
3445 if (request->flags & ATA_R_WRITE) {
3446 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
3447 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
3448 ata_promise_queue_hpkt(ctlr,
3449 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
3456 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
3458 struct ata_device *atadev = device_get_softc(request->dev);
3461 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
3462 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
3463 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
3464 bytep[i++] = ATA_A_4BIT;
3466 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3467 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
3468 bytep[i++] = request->u.ata.feature >> 8;
3469 bytep[i++] = request->u.ata.feature;
3470 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
3471 bytep[i++] = request->u.ata.count >> 8;
3472 bytep[i++] = request->u.ata.count;
3473 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
3474 bytep[i++] = request->u.ata.lba >> 24;
3475 bytep[i++] = request->u.ata.lba;
3476 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
3477 bytep[i++] = request->u.ata.lba >> 32;
3478 bytep[i++] = request->u.ata.lba >> 8;
3479 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
3480 bytep[i++] = request->u.ata.lba >> 40;
3481 bytep[i++] = request->u.ata.lba >> 16;
3482 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
3483 bytep[i++] = ATA_D_LBA | atadev->unit;
3486 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
3487 bytep[i++] = request->u.ata.feature;
3488 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
3489 bytep[i++] = request->u.ata.count;
3490 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
3491 bytep[i++] = request->u.ata.lba;
3492 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
3493 bytep[i++] = request->u.ata.lba >> 8;
3494 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
3495 bytep[i++] = request->u.ata.lba >> 16;
3496 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
3497 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
3498 ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
3500 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
3501 bytep[i++] = request->u.ata.command;
3506 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
3508 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
3510 mtx_lock(&hpktp->mtx);
3512 struct host_packet *hp =
3513 malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
3515 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
3519 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
3521 mtx_unlock(&hpktp->mtx);
3525 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
3527 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
3528 struct host_packet *hp;
3530 mtx_lock(&hpktp->mtx);
3531 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
3532 TAILQ_REMOVE(&hpktp->queue, hp, chain);
3533 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
3538 mtx_unlock(&hpktp->mtx);
3542 ata_promise_tx2_intr(void *data)
3544 struct ata_pci_controller *ctlr = data;
3545 struct ata_channel *ch;
3548 for (unit = 0; unit < ctlr->channels; unit++) {
3549 if (!(ch = ctlr->interrupt[unit].argument))
3551 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3552 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
3553 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3554 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3556 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3557 ATA_BMSTAT_INTERRUPT)
3559 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3562 ctlr->interrupt[unit].function(ch);
3568 ata_promise_old_intr(void *data)
3570 struct ata_pci_controller *ctlr = data;
3571 struct ata_channel *ch;
3574 for (unit = 0; unit < ctlr->channels; unit++) {
3575 if (!(ch = ctlr->interrupt[unit].argument))
3577 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)){
3578 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3579 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3581 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3582 ATA_BMSTAT_INTERRUPT)
3584 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3587 ctlr->interrupt[unit].function(ch);
3593 ata_promise_new_dmastart(device_t dev)
3595 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3596 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3597 struct ata_device *atadev = device_get_softc(dev);
3599 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3600 ATA_OUTB(ctlr->r_res1, 0x11,
3601 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
3602 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20,
3603 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
3604 (ch->dma->cur_iosize >> 1));
3606 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
3607 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
3608 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
3609 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3610 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
3611 ATA_BMCMD_START_STOP);
3612 ch->flags |= ATA_DMA_ACTIVE;
3617 ata_promise_new_dmastop(device_t dev)
3619 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3620 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3621 struct ata_device *atadev = device_get_softc(dev);
3624 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3625 ATA_OUTB(ctlr->r_res1, 0x11,
3626 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
3627 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20, 0);
3629 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
3630 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3631 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3632 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3633 ch->flags &= ~ATA_DMA_ACTIVE;
3638 ata_promise_new_dmareset(device_t dev)
3640 struct ata_channel *ch = device_get_softc(dev);
3642 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3643 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3644 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3645 ch->flags &= ~ATA_DMA_ACTIVE;
3649 ata_promise_new_dmainit(device_t dev)
3651 struct ata_channel *ch = device_get_softc(dev);
3655 ch->dma->start = ata_promise_new_dmastart;
3656 ch->dma->stop = ata_promise_new_dmastop;
3657 ch->dma->reset = ata_promise_new_dmareset;
3662 ata_promise_setmode(device_t dev, int mode)
3664 device_t gparent = GRANDPARENT(dev);
3665 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3666 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3667 struct ata_device *atadev = device_get_softc(dev);
3668 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3670 u_int32_t timings33[][2] = {
3671 /* PROLD PRNEW mode */
3672 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
3673 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
3674 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
3675 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
3676 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
3677 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
3678 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
3679 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
3680 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
3681 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
3682 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
3683 { 0, 0x00424ef6 }, /* UDMA 3 */
3684 { 0, 0x004127f3 }, /* UDMA 4 */
3685 { 0, 0x004127f3 } /* UDMA 5 */
3688 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3690 switch (ctlr->chip->cfg1) {
3693 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
3694 (ch->unit ? 1 << 11 : 1 << 10))) {
3695 ata_print_cable(dev, "controller");
3698 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
3699 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
3703 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3704 if (mode > ATA_UDMA2 &&
3705 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
3706 ata_print_cable(dev, "controller");
3712 if (mode > ATA_UDMA2 &&
3713 (ATA_INL(ctlr->r_res2,
3714 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
3715 (ch->unit << 7)) & 0x01000000)) {
3716 ata_print_cable(dev, "controller");
3722 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3725 device_printf(dev, "%ssetting %s on %s chip\n",
3726 (error) ? "FAILURE " : "",
3727 ata_mode2str(mode), ctlr->chip->text);
3729 if (ctlr->chip->cfg1 < PRTX)
3730 pci_write_config(gparent, 0x60 + (devno << 2),
3731 timings33[ctlr->chip->cfg1][ata_mode2idx(mode)],4);
3732 atadev->mode = mode;
3739 * ServerWorks chipset support functions
3742 ata_serverworks_ident(device_t dev)
3744 struct ata_pci_controller *ctlr = device_get_softc(dev);
3745 struct ata_chip_id *idx;
3746 static struct ata_chip_id ids[] =
3747 {{ ATA_ROSB4, 0x00, SWKS33, 0x00, ATA_UDMA2, "ROSB4" },
3748 { ATA_CSB5, 0x92, SWKS100, 0x00, ATA_UDMA5, "CSB5" },
3749 { ATA_CSB5, 0x00, SWKS66, 0x00, ATA_UDMA4, "CSB5" },
3750 { ATA_CSB6, 0x00, SWKS100, 0x00, ATA_UDMA5, "CSB6" },
3751 { ATA_CSB6_1, 0x00, SWKS66, 0x00, ATA_UDMA4, "CSB6" },
3752 { 0, 0, 0, 0, 0, 0}};
3755 if (!(idx = ata_match_chip(dev, ids)))
3758 sprintf(buffer, "ServerWorks %s %s controller",
3759 idx->text, ata_mode2str(idx->max_dma));
3760 device_set_desc_copy(dev, buffer);
3762 ctlr->chipinit = ata_serverworks_chipinit;
3767 ata_serverworks_chipinit(device_t dev)
3769 struct ata_pci_controller *ctlr = device_get_softc(dev);
3771 if (ata_setup_interrupt(dev))
3774 if (ctlr->chip->cfg1 == SWKS33) {
3778 /* locate the ISA part in the southbridge and enable UDMA33 */
3779 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
3780 for (i = 0; i < nchildren; i++) {
3781 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
3782 pci_write_config(children[i], 0x64,
3783 (pci_read_config(children[i], 0x64, 4) &
3784 ~0x00002000) | 0x00004000, 4);
3788 free(children, M_TEMP);
3792 pci_write_config(dev, 0x5a,
3793 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
3794 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
3796 ctlr->setmode = ata_serverworks_setmode;
3801 ata_serverworks_setmode(device_t dev, int mode)
3803 device_t gparent = GRANDPARENT(dev);
3804 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3805 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3806 struct ata_device *atadev = device_get_softc(dev);
3807 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3808 int offset = (devno ^ 0x01) << 3;
3810 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
3811 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
3812 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
3814 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3816 mode = ata_check_80pin(dev, mode);
3818 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3821 device_printf(dev, "%ssetting %s on %s chip\n",
3822 (error) ? "FAILURE " : "",
3823 ata_mode2str(mode), ctlr->chip->text);
3825 if (mode >= ATA_UDMA0) {
3826 pci_write_config(gparent, 0x56,
3827 (pci_read_config(gparent, 0x56, 2) &
3828 ~(0xf << (devno << 2))) |
3829 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
3830 pci_write_config(gparent, 0x54,
3831 pci_read_config(gparent, 0x54, 1) |
3832 (0x01 << devno), 1);
3833 pci_write_config(gparent, 0x44,
3834 (pci_read_config(gparent, 0x44, 4) &
3835 ~(0xff << offset)) |
3836 (dmatimings[2] << offset), 4);
3838 else if (mode >= ATA_WDMA0) {
3839 pci_write_config(gparent, 0x54,
3840 pci_read_config(gparent, 0x54, 1) &
3841 ~(0x01 << devno), 1);
3842 pci_write_config(gparent, 0x44,
3843 (pci_read_config(gparent, 0x44, 4) &
3844 ~(0xff << offset)) |
3845 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
3848 pci_write_config(gparent, 0x54,
3849 pci_read_config(gparent, 0x54, 1) &
3850 ~(0x01 << devno), 1);
3852 pci_write_config(gparent, 0x40,
3853 (pci_read_config(gparent, 0x40, 4) &
3854 ~(0xff << offset)) |
3855 (piotimings[ata_mode2idx(mode)] << offset), 4);
3856 atadev->mode = mode;
3862 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
3865 ata_sii_ident(device_t dev)
3867 struct ata_pci_controller *ctlr = device_get_softc(dev);
3868 struct ata_chip_id *idx;
3869 static struct ata_chip_id ids[] =
3870 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
3871 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
3872 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
3873 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
3874 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
3875 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
3876 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
3877 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
3878 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
3879 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
3880 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
3881 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
3882 { 0, 0, 0, 0, 0, 0}};
3885 if (!(idx = ata_match_chip(dev, ids)))
3888 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3889 device_set_desc_copy(dev, buffer);
3891 ctlr->chipinit = ata_sii_chipinit;
3896 ata_sii_chipinit(device_t dev)
3898 struct ata_pci_controller *ctlr = device_get_softc(dev);
3899 int rid = ATA_IRQ_RID;
3901 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3902 RF_SHAREABLE | RF_ACTIVE))) {
3903 device_printf(dev, "unable to map interrupt\n");
3907 if (ctlr->chip->cfg1 == SIIMEMIO) {
3908 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3909 ata_sii_intr, ctlr, &ctlr->handle))) {
3910 device_printf(dev, "unable to setup interrupt\n");
3914 ctlr->r_type2 = SYS_RES_MEMORY;
3915 ctlr->r_rid2 = PCIR_BAR(5);
3916 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3917 &ctlr->r_rid2, RF_ACTIVE)))
3920 if (ctlr->chip->cfg2 & SIISETCLK) {
3921 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
3922 pci_write_config(dev, 0x8a,
3923 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
3924 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
3925 device_printf(dev, "%s could not set ATA133 clock\n",
3929 /* if we have 4 channels enable the second set */
3930 if (ctlr->chip->cfg2 & SII4CH) {
3931 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
3935 /* enable PCI interrupt as BIOS might not */
3936 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
3938 /* dont block interrupts from any channel */
3939 pci_write_config(dev, 0x48,
3940 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
3942 ctlr->allocate = ata_sii_allocate;
3943 if (ctlr->chip->max_dma >= ATA_SA150) {
3944 ctlr->reset = ata_sii_reset;
3945 ctlr->setmode = ata_sata_setmode;
3948 ctlr->setmode = ata_sii_setmode;
3951 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3952 ctlr->chip->cfg2 & SIIINTR ?
3953 ata_cmd_intr : ata_cmd_old_intr,
3954 ctlr, &ctlr->handle))) {
3955 device_printf(dev, "unable to setup interrupt\n");
3959 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
3960 device_printf(dev, "HW has secondary channel disabled\n");
3964 /* enable interrupt as BIOS might not */
3965 pci_write_config(dev, 0x71, 0x01, 1);
3967 ctlr->setmode = ata_cmd_setmode;
3973 ata_sii_allocate(device_t dev)
3975 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3976 struct ata_channel *ch = device_get_softc(dev);
3977 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
3980 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
3981 ch->r_io[i].res = ctlr->r_res2;
3982 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
3984 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
3985 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
3986 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
3987 ata_default_registers(dev);
3988 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
3989 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
3990 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
3991 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
3992 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
3993 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
3994 ch->r_io[ATA_BMDEVSPEC_0].res = ctlr->r_res2;
3995 ch->r_io[ATA_BMDEVSPEC_0].offset = 0xa1 + (unit01 << 6) + (unit10 << 8);
3997 if (ctlr->chip->max_dma >= ATA_SA150) {
3998 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3999 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
4000 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4001 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
4002 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4003 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
4004 ch->flags |= ATA_NO_SLAVE;
4006 /* enable PHY state change interrupt */
4007 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
4010 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
4011 /* work around errata in early chips */
4012 ch->dma->boundary = 16 * DEV_BSIZE;
4013 ch->dma->segsize = 15 * DEV_BSIZE;
4016 ata_generic_hw(dev);
4021 ata_sii_intr(void *data)
4023 struct ata_pci_controller *ctlr = data;
4024 struct ata_channel *ch;
4027 for (unit = 0; unit < ctlr->channels; unit++) {
4028 if (!(ch = ctlr->interrupt[unit].argument))
4031 /* check for PHY related interrupts on SATA capable HW */
4032 if (ctlr->chip->max_dma >= ATA_SA150) {
4033 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
4034 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
4035 struct ata_connect_task *tp;
4038 /* clear error bits/interrupt */
4039 ATA_IDX_OUTL(ch, ATA_SERROR, error);
4041 /* if we have a connection event deal with it */
4042 if ((error & ATA_SE_PHY_CHANGED) &&
4043 (tp = (struct ata_connect_task *)
4044 malloc(sizeof(struct ata_connect_task),
4045 M_ATA, M_NOWAIT | M_ZERO))) {
4047 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
4048 device_printf(ch->dev, "CONNECT requested\n");
4049 tp->action = ATA_C_ATTACH;
4052 device_printf(ch->dev, "DISCONNECT requested\n");
4053 tp->action = ATA_C_DETACH;
4056 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
4057 taskqueue_enqueue(taskqueue_thread, &tp->task);
4062 /* any drive action to take care of ? */
4063 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_0) & 0x08) {
4064 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
4065 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
4067 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
4069 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
4072 ctlr->interrupt[unit].function(ch);
4079 ata_cmd_intr(void *data)
4081 struct ata_pci_controller *ctlr = data;
4082 struct ata_channel *ch;
4086 for (unit = 0; unit < ctlr->channels; unit++) {
4087 if (!(ch = ctlr->interrupt[unit].argument))
4089 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
4090 (ch->unit ? 0x08 : 0x04))) {
4091 pci_write_config(device_get_parent(ch->dev), 0x71,
4092 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
4093 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
4094 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
4096 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
4097 ATA_BMSTAT_INTERRUPT)
4099 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
4102 ctlr->interrupt[unit].function(ch);
4108 ata_cmd_old_intr(void *data)
4110 struct ata_pci_controller *ctlr = data;
4111 struct ata_channel *ch;
4114 for (unit = 0; unit < ctlr->channels; unit++) {
4115 if (!(ch = ctlr->interrupt[unit].argument))
4117 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
4118 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
4120 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
4121 ATA_BMSTAT_INTERRUPT)
4123 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
4126 ctlr->interrupt[unit].function(ch);
4131 ata_sii_reset(device_t dev)
4133 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4134 struct ata_channel *ch = device_get_softc(dev);
4135 int offset = ((ch->unit & 1) << 7) + ((ch->unit & 2) << 8);
4137 /* disable PHY state change interrupt */
4138 ATA_OUTL(ctlr->r_res2, 0x148 + offset, ~(1 << 16));
4140 /* reset controller part for this channel */
4141 ATA_OUTL(ctlr->r_res2, 0x48,
4142 ATA_INL(ctlr->r_res2, 0x48) | (0xc0 >> ch->unit));
4144 ATA_OUTL(ctlr->r_res2, 0x48,
4145 ATA_INL(ctlr->r_res2, 0x48) & ~(0xc0 >> ch->unit));
4147 ata_sata_phy_enable(ch);
4149 /* enable PHY state change interrupt */
4150 ATA_OUTL(ctlr->r_res2, 0x148 + offset, (1 << 16));
4154 ata_sii_setmode(device_t dev, int mode)
4156 device_t gparent = GRANDPARENT(dev);
4157 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4158 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4159 struct ata_device *atadev = device_get_softc(dev);
4160 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
4161 int mreg = ch->unit ? 0x84 : 0x80;
4162 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
4163 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
4166 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4168 if (ctlr->chip->cfg2 & SIISETCLK) {
4169 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
4170 (ch->unit ? 0x02 : 0x01))) {
4171 ata_print_cable(dev, "controller");
4176 mode = ata_check_80pin(dev, mode);
4178 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4181 device_printf(dev, "%ssetting %s on %s chip\n",
4182 (error) ? "FAILURE " : "",
4183 ata_mode2str(mode), ctlr->chip->text);
4187 if (mode >= ATA_UDMA0) {
4188 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
4189 u_int8_t ureg = 0xac + rego;
4191 pci_write_config(gparent, mreg,
4192 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
4193 pci_write_config(gparent, ureg,
4194 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
4195 udmatimings[mode & ATA_MODE_MASK], 1);
4198 else if (mode >= ATA_WDMA0) {
4199 u_int8_t dreg = 0xa8 + rego;
4200 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
4202 pci_write_config(gparent, mreg,
4203 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
4204 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
4208 u_int8_t preg = 0xa4 + rego;
4209 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
4211 pci_write_config(gparent, mreg,
4212 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
4213 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
4215 atadev->mode = mode;
4219 ata_cmd_setmode(device_t dev, int mode)
4221 device_t gparent = GRANDPARENT(dev);
4222 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4223 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4224 struct ata_device *atadev = device_get_softc(dev);
4225 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4228 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4230 mode = ata_check_80pin(dev, mode);
4232 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4235 device_printf(dev, "%ssetting %s on %s chip\n",
4236 (error) ? "FAILURE " : "",
4237 ata_mode2str(mode), ctlr->chip->text);
4239 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
4240 int ureg = ch->unit ? 0x7b : 0x73;
4242 if (mode >= ATA_UDMA0) {
4243 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
4244 { 0x11, 0x42 }, { 0x25, 0x8a },
4245 { 0x15, 0x4a }, { 0x05, 0x0a } };
4247 u_int8_t umode = pci_read_config(gparent, ureg, 1);
4249 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
4250 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
4251 pci_write_config(gparent, ureg, umode, 1);
4253 else if (mode >= ATA_WDMA0) {
4254 int dmatimings[] = { 0x87, 0x32, 0x3f };
4256 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
4257 pci_write_config(gparent, ureg,
4258 pci_read_config(gparent, ureg, 1) &
4259 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4262 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
4263 pci_write_config(gparent, treg,
4264 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
4265 pci_write_config(gparent, ureg,
4266 pci_read_config(gparent, ureg, 1) &
4267 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4269 atadev->mode = mode;
4275 * Silicon Integrated Systems Corp. (SiS) chipset support functions
4278 ata_sis_ident(device_t dev)
4280 struct ata_pci_controller *ctlr = device_get_softc(dev);
4281 struct ata_chip_id *idx;
4282 static struct ata_chip_id ids[] =
4283 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "182" }, /* south */
4284 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "181" }, /* south */
4285 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "180" }, /* south */
4286 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "965" }, /* south */
4287 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "964" }, /* south */
4288 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "963" }, /* south */
4289 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "962" }, /* south */
4291 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "745" }, /* 1chip */
4292 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "735" }, /* 1chip */
4293 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "733" }, /* 1chip */
4294 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "730" }, /* 1chip */
4296 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "635" }, /* 1chip */
4297 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "633" }, /* unknown */
4298 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "630S"}, /* 1chip */
4299 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "630" }, /* 1chip */
4300 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "620" }, /* 1chip */
4302 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "550" },
4303 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "540" },
4304 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "530" },
4306 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "5513" },
4307 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "5513" },
4308 { 0, 0, 0, 0, 0, 0 }};
4312 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
4315 if (idx->cfg2 && !found) {
4316 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
4318 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
4319 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
4321 idx->cfg1 = SIS133NEW;
4322 idx->max_dma = ATA_UDMA6;
4323 sprintf(buffer, "SiS 962/963 %s controller",
4324 ata_mode2str(idx->max_dma));
4326 pci_write_config(dev, 0x57, reg57, 1);
4328 if (idx->cfg2 && !found) {
4329 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
4331 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
4332 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
4333 struct ata_chip_id id[] =
4334 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
4337 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
4338 idx->cfg1 = SIS133OLD;
4339 idx->max_dma = ATA_UDMA6;
4342 idx->cfg1 = SIS100NEW;
4343 idx->max_dma = ATA_UDMA5;
4345 sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
4347 pci_write_config(dev, 0x4a, reg4a, 1);
4350 sprintf(buffer,"SiS %s %s controller",
4351 idx->text, ata_mode2str(idx->max_dma));
4353 device_set_desc_copy(dev, buffer);
4355 ctlr->chipinit = ata_sis_chipinit;
4360 ata_sis_chipinit(device_t dev)
4362 struct ata_pci_controller *ctlr = device_get_softc(dev);
4364 if (ata_setup_interrupt(dev))
4367 switch (ctlr->chip->cfg1) {
4372 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
4376 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
4379 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
4380 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
4383 ctlr->r_type2 = SYS_RES_IOPORT;
4384 ctlr->r_rid2 = PCIR_BAR(5);
4385 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4386 &ctlr->r_rid2, RF_ACTIVE))) {
4387 pci_write_config(dev, PCIR_COMMAND,
4388 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
4389 ctlr->allocate = ata_sis_allocate;
4390 ctlr->reset = ata_sis_reset;
4392 ctlr->setmode = ata_sata_setmode;
4397 ctlr->setmode = ata_sis_setmode;
4402 ata_sis_allocate(device_t dev)
4404 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4405 struct ata_channel *ch = device_get_softc(dev);
4406 int offset = ch->unit << ((ctlr->chip->chipid == ATA_SIS182) ? 5 : 6);
4408 /* setup the usual register normal pci style */
4409 if (ata_pci_allocate(dev))
4412 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4413 ch->r_io[ATA_SSTATUS].offset = 0x00 + offset;
4414 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4415 ch->r_io[ATA_SERROR].offset = 0x04 + offset;
4416 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4417 ch->r_io[ATA_SCONTROL].offset = 0x08 + offset;
4418 ch->flags |= ATA_NO_SLAVE;
4420 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
4421 /* XXX SOS unknown how to enable PHY state change interrupt */
4426 ata_sis_reset(device_t dev)
4428 struct ata_channel *ch = device_get_softc(dev);
4430 ata_sata_phy_enable(ch);
4435 ata_sis_setmode(device_t dev, int mode)
4437 device_t gparent = GRANDPARENT(dev);
4438 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4439 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4440 struct ata_device *atadev = device_get_softc(dev);
4441 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4444 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4446 if (ctlr->chip->cfg1 == SIS133NEW) {
4447 if (mode > ATA_UDMA2 &&
4448 pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
4449 ata_print_cable(dev, "controller");
4454 if (mode > ATA_UDMA2 &&
4455 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
4456 ata_print_cable(dev, "controller");
4461 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4464 device_printf(dev, "%ssetting %s on %s chip\n",
4465 (error) ? "FAILURE " : "",
4466 ata_mode2str(mode), ctlr->chip->text);
4468 switch (ctlr->chip->cfg1) {
4470 u_int32_t timings[] =
4471 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
4472 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
4473 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
4476 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
4477 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
4481 u_int16_t timings[] =
4482 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
4483 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
4485 u_int16_t reg = 0x40 + (devno << 1);
4487 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
4491 u_int16_t timings[] =
4492 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
4493 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
4494 u_int16_t reg = 0x40 + (devno << 1);
4496 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
4502 u_int16_t timings[] =
4503 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
4504 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
4505 u_int16_t reg = 0x40 + (devno << 1);
4507 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
4511 atadev->mode = mode;
4516 /* VIA Technologies Inc. chipset support functions */
4518 ata_via_ident(device_t dev)
4520 struct ata_pci_controller *ctlr = device_get_softc(dev);
4521 struct ata_chip_id *idx;
4522 static struct ata_chip_id ids[] =
4523 {{ ATA_VIA82C586, 0x02, VIA33, 0x00, ATA_UDMA2, "82C586B" },
4524 { ATA_VIA82C586, 0x00, VIA33, 0x00, ATA_WDMA2, "82C586" },
4525 { ATA_VIA82C596, 0x12, VIA66, VIACLK, ATA_UDMA4, "82C596B" },
4526 { ATA_VIA82C596, 0x00, VIA33, 0x00, ATA_UDMA2, "82C596" },
4527 { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "82C686B"},
4528 { ATA_VIA82C686, 0x10, VIA66, VIACLK, ATA_UDMA4, "82C686A" },
4529 { ATA_VIA82C686, 0x00, VIA33, 0x00, ATA_UDMA2, "82C686" },
4530 { ATA_VIA8231, 0x00, VIA100, VIABUG, ATA_UDMA5, "8231" },
4531 { ATA_VIA8233, 0x00, VIA100, 0x00, ATA_UDMA5, "8233" },
4532 { ATA_VIA8233C, 0x00, VIA100, 0x00, ATA_UDMA5, "8233C" },
4533 { ATA_VIA8233A, 0x00, VIA133, 0x00, ATA_UDMA6, "8233A" },
4534 { ATA_VIA8235, 0x00, VIA133, 0x00, ATA_UDMA6, "8235" },
4535 { ATA_VIA8237, 0x00, VIA133, 0x00, ATA_UDMA6, "8237" },
4536 { ATA_VIA8251, 0x00, VIA133, 0x00, ATA_UDMA6, "8251" },
4537 { 0, 0, 0, 0, 0, 0 }};
4538 static struct ata_chip_id new_ids[] =
4539 {{ ATA_VIA6410, 0x00, 0, 0x00, ATA_UDMA6, "6410" },
4540 { ATA_VIA6420, 0x00, 7, 0x00, ATA_SA150, "6420" },
4541 { ATA_VIA6421, 0x00, 6, VIABAR, ATA_SA150, "6421" },
4542 { ATA_VIA8251, 0x00, 0, VIAAHCI, ATA_SA150, "8251" },
4543 { 0, 0, 0, 0, 0, 0 }};
4546 if (pci_get_devid(dev) == ATA_VIA82C571) {
4547 if (!(idx = ata_find_chip(dev, ids, -99)))
4551 if (!(idx = ata_match_chip(dev, new_ids)))
4555 sprintf(buffer, "VIA %s %s controller",
4556 idx->text, ata_mode2str(idx->max_dma));
4557 device_set_desc_copy(dev, buffer);
4559 ctlr->chipinit = ata_via_chipinit;
4564 ata_via_chipinit(device_t dev)
4566 struct ata_pci_controller *ctlr = device_get_softc(dev);
4568 if (ata_setup_interrupt(dev))
4571 if (ctlr->chip->max_dma >= ATA_SA150) {
4572 ctlr->r_type2 = SYS_RES_IOPORT;
4573 ctlr->r_rid2 = PCIR_BAR(5);
4574 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4575 &ctlr->r_rid2, RF_ACTIVE))) {
4576 pci_write_config(dev, PCIR_COMMAND,
4577 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
4578 ctlr->allocate = ata_via_allocate;
4579 ctlr->reset = ata_via_reset;
4581 ctlr->setmode = ata_sata_setmode;
4585 /* prepare for ATA-66 on the 82C686a and 82C596b */
4586 if (ctlr->chip->cfg2 & VIACLK)
4587 pci_write_config(dev, 0x50, 0x030b030b, 4);
4589 /* the southbridge might need the data corruption fix */
4590 if (ctlr->chip->cfg2 & VIABUG)
4591 ata_via_southbridge_fixup(dev);
4593 /* set fifo configuration half'n'half */
4594 pci_write_config(dev, 0x43,
4595 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
4597 /* set status register read retry */
4598 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
4600 /* set DMA read & end-of-sector fifo flush */
4601 pci_write_config(dev, 0x46,
4602 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
4604 /* set sector size */
4605 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
4606 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
4608 ctlr->setmode = ata_via_family_setmode;
4613 ata_via_allocate(device_t dev)
4615 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4616 struct ata_channel *ch = device_get_softc(dev);
4618 /* newer SATA chips has resources in one BAR for each channel */
4619 if (ctlr->chip->cfg2 & VIABAR) {
4620 struct resource *r_io;
4623 rid = PCIR_BAR(ch->unit);
4624 if (!(r_io = bus_alloc_resource_any(device_get_parent(dev),
4629 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
4630 ch->r_io[i].res = r_io;
4631 ch->r_io[i].offset = i;
4633 ch->r_io[ATA_CONTROL].res = r_io;
4634 ch->r_io[ATA_CONTROL].offset = 2 + ATA_IOSIZE;
4635 ch->r_io[ATA_IDX_ADDR].res = r_io;
4636 ata_default_registers(dev);
4637 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
4638 ch->r_io[i].res = ctlr->r_res1;
4639 ch->r_io[i].offset = i - ATA_BMCMD_PORT;
4641 ata_generic_hw(dev);
4644 if (ata_pci_allocate(dev))
4648 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4649 ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
4650 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4651 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
4652 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4653 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
4654 ch->flags |= ATA_NO_SLAVE;
4656 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
4657 /* XXX SOS unknown how to enable PHY state change interrupt */
4662 ata_via_reset(device_t dev)
4664 struct ata_channel *ch = device_get_softc(dev);
4666 ata_sata_phy_enable(ch);
4670 ata_via_southbridge_fixup(device_t dev)
4675 if (device_get_children(device_get_parent(dev), &children, &nchildren))
4678 for (i = 0; i < nchildren; i++) {
4679 if (pci_get_devid(children[i]) == ATA_VIA8363 ||
4680 pci_get_devid(children[i]) == ATA_VIA8371 ||
4681 pci_get_devid(children[i]) == ATA_VIA8662 ||
4682 pci_get_devid(children[i]) == ATA_VIA8361) {
4683 u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
4685 if ((reg76 & 0xf0) != 0xd0) {
4687 "Correcting VIA config for southbridge data corruption bug\n");
4688 pci_write_config(children[i], 0x75, 0x80, 1);
4689 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
4694 free(children, M_TEMP);
4698 /* common code for VIA, AMD & nVidia */
4700 ata_via_family_setmode(device_t dev, int mode)
4702 device_t gparent = GRANDPARENT(dev);
4703 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4704 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4705 struct ata_device *atadev = device_get_softc(dev);
4706 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
4707 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
4709 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* VIA ATA33 */
4710 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* VIA ATA66 */
4711 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* VIA ATA100 */
4712 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
4713 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }}; /* AMD/nVIDIA */
4714 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4715 int reg = 0x53 - devno;
4718 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4720 if (ctlr->chip->cfg2 & AMDCABLE) {
4721 if (mode > ATA_UDMA2 &&
4722 !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
4723 ata_print_cable(dev, "controller");
4728 mode = ata_check_80pin(dev, mode);
4730 if (ctlr->chip->cfg2 & NVIDIA)
4733 if (ctlr->chip->cfg1 != VIA133)
4734 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
4736 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4739 device_printf(dev, "%ssetting %s on %s chip\n",
4740 (error) ? "FAILURE " : "", ata_mode2str(mode),
4743 if (mode >= ATA_UDMA0)
4744 pci_write_config(gparent, reg,
4745 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
4747 pci_write_config(gparent, reg, 0x8b, 1);
4748 atadev->mode = mode;
4753 /* misc functions */
4754 struct ata_chip_id *
4755 ata_match_chip(device_t dev, struct ata_chip_id *index)
4757 while (index->chipid != 0) {
4758 if (pci_get_devid(dev) == index->chipid &&
4759 pci_get_revid(dev) >= index->chiprev)
4766 static struct ata_chip_id *
4767 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
4772 if (device_get_children(device_get_parent(dev), &children, &nchildren))
4775 while (index->chipid != 0) {
4776 for (i = 0; i < nchildren; i++) {
4777 if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
4778 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
4779 pci_get_devid(children[i]) == index->chipid &&
4780 pci_get_revid(children[i]) >= index->chiprev) {
4781 free(children, M_TEMP);
4787 free(children, M_TEMP);
4792 ata_setup_interrupt(device_t dev)
4794 struct ata_pci_controller *ctlr = device_get_softc(dev);
4795 int rid = ATA_IRQ_RID;
4797 if (!ata_legacy(dev)) {
4798 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
4799 RF_SHAREABLE | RF_ACTIVE))) {
4800 device_printf(dev, "unable to map interrupt\n");
4803 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
4804 ata_generic_intr, ctlr, &ctlr->handle))) {
4805 device_printf(dev, "unable to setup interrupt\n");
4812 struct ata_serialize {
4813 struct mtx locked_mtx;
4819 ata_serialize(device_t dev, int flags)
4821 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4822 struct ata_channel *ch = device_get_softc(dev);
4823 struct ata_serialize *serial;
4824 static int inited = 0;
4828 serial = malloc(sizeof(struct ata_serialize),
4829 M_TEMP, M_NOWAIT | M_ZERO);
4830 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
4831 serial->locked_ch = -1;
4832 serial->restart_ch = -1;
4833 device_set_ivars(ctlr->dev, serial);
4837 serial = device_get_ivars(ctlr->dev);
4839 mtx_lock(&serial->locked_mtx);
4842 if (serial->locked_ch == -1)
4843 serial->locked_ch = ch->unit;
4844 if (serial->locked_ch != ch->unit)
4845 serial->restart_ch = ch->unit;
4849 if (serial->locked_ch == ch->unit) {
4850 serial->locked_ch = -1;
4851 if (serial->restart_ch != -1) {
4852 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
4853 serial->restart_ch = -1;
4854 mtx_unlock(&serial->locked_mtx);
4865 res = serial->locked_ch;
4866 mtx_unlock(&serial->locked_mtx);
4871 ata_print_cable(device_t dev, u_int8_t *who)
4874 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
4878 ata_atapi(device_t dev)
4880 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4881 struct ata_device *atadev = device_get_softc(dev);
4883 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
4884 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
4888 ata_check_80pin(device_t dev, int mode)
4890 struct ata_device *atadev = device_get_softc(dev);
4892 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
4893 ata_print_cable(dev, "device");
4900 ata_mode2idx(int mode)
4902 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
4903 return (mode & ATA_MODE_MASK) + 8;
4904 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
4905 return (mode & ATA_MODE_MASK) + 5;
4906 return (mode & ATA_MODE_MASK) - ATA_PIO0;