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_cyrix_chipinit(device_t dev);
81 static void ata_cyrix_setmode(device_t dev, int mode);
82 static int ata_cypress_chipinit(device_t dev);
83 static void ata_cypress_setmode(device_t dev, int mode);
84 static int ata_highpoint_chipinit(device_t dev);
85 static void ata_highpoint_intr(void *data);
86 static void ata_highpoint_setmode(device_t dev, int mode);
87 static int ata_highpoint_check_80pin(device_t dev, int mode);
88 static int ata_intel_chipinit(device_t dev);
89 static int ata_intel_31244_allocate(device_t dev);
90 static void ata_intel_31244_intr(void *data);
91 static void ata_intel_31244_reset(device_t dev);
92 static int ata_intel_31244_command(struct ata_request *request);
93 static void ata_intel_intr(void *data);
94 static void ata_intel_reset(device_t dev);
95 static void ata_intel_old_setmode(device_t dev, int mode);
96 static void ata_intel_new_setmode(device_t dev, int mode);
97 static int ata_ite_chipinit(device_t dev);
98 static void ata_ite_setmode(device_t dev, int mode);
99 static int ata_national_chipinit(device_t dev);
100 static void ata_national_setmode(device_t dev, int mode);
101 static int ata_nvidia_chipinit(device_t dev);
102 static int ata_nvidia_allocate(device_t dev);
103 static void ata_nvidia_intr(void *data);
104 static void ata_nvidia_reset(device_t dev);
105 static int ata_promise_chipinit(device_t dev);
106 static int ata_promise_mio_allocate(device_t dev);
107 static void ata_promise_mio_intr(void *data);
108 static void ata_promise_sx4_intr(void *data);
109 static void ata_promise_mio_dmainit(device_t dev);
110 static void ata_promise_mio_reset(device_t dev);
111 static int ata_promise_mio_command(struct ata_request *request);
112 static int ata_promise_sx4_command(struct ata_request *request);
113 static int ata_promise_apkt(u_int8_t *bytep, struct ata_request *request);
114 static void ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt);
115 static void ata_promise_next_hpkt(struct ata_pci_controller *ctlr);
116 static void ata_promise_tx2_intr(void *data);
117 static void ata_promise_old_intr(void *data);
118 static int ata_promise_new_dmastart(device_t dev);
119 static int ata_promise_new_dmastop(device_t dev);
120 static void ata_promise_new_dmareset(device_t dev);
121 static void ata_promise_new_dmainit(device_t dev);
122 static void ata_promise_setmode(device_t dev, int mode);
123 static int ata_serverworks_chipinit(device_t dev);
124 static void ata_serverworks_setmode(device_t dev, int mode);
125 static int ata_sii_chipinit(device_t dev);
126 static int ata_sii_allocate(device_t dev);
127 static void ata_sii_intr(void *data);
128 static void ata_cmd_intr(void *data);
129 static void ata_cmd_old_intr(void *data);
130 static void ata_sii_reset(device_t dev);
131 static void ata_sii_setmode(device_t dev, int mode);
132 static void ata_cmd_setmode(device_t dev, int mode);
133 static int ata_sis_chipinit(device_t dev);
134 static int ata_sis_allocate(device_t dev);
135 static void ata_sis_reset(device_t dev);
136 static void ata_sis_setmode(device_t dev, int mode);
137 static int ata_via_chipinit(device_t dev);
138 static int ata_via_allocate(device_t dev);
139 static void ata_via_reset(device_t dev);
140 static void ata_via_southbridge_fixup(device_t dev);
141 static void ata_via_family_setmode(device_t dev, int mode);
142 static struct ata_chip_id *ata_find_chip(device_t dev, struct ata_chip_id *index, int slot);
143 static int ata_setup_interrupt(device_t dev);
144 static int ata_serialize(device_t dev, int flags);
145 static void ata_print_cable(device_t dev, u_int8_t *who);
146 static int ata_atapi(device_t dev);
147 static int ata_check_80pin(device_t dev, int mode);
148 static int ata_mode2idx(int mode);
151 /* generic or unknown ATA chipset support functions */
153 ata_generic_ident(device_t dev)
155 struct ata_pci_controller *ctlr = device_get_softc(dev);
157 device_set_desc(dev, "GENERIC ATA controller");
158 ctlr->chipinit = ata_generic_chipinit;
163 ata_generic_chipinit(device_t dev)
165 struct ata_pci_controller *ctlr = device_get_softc(dev);
167 if (ata_setup_interrupt(dev))
169 ctlr->setmode = ata_generic_setmode;
174 ata_generic_intr(void *data)
176 struct ata_pci_controller *ctlr = data;
177 struct ata_channel *ch;
180 for (unit = 0; unit < ctlr->channels; unit++) {
181 if (!(ch = ctlr->interrupt[unit].argument))
183 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
184 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
186 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
187 ATA_BMSTAT_INTERRUPT)
189 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
192 ctlr->interrupt[unit].function(ch);
197 ata_generic_setmode(device_t dev, int mode)
199 struct ata_device *atadev = device_get_softc(dev);
201 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
202 mode = ata_check_80pin(dev, mode);
203 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
208 /* SATA support functions */
210 ata_sata_setmode(device_t dev, int mode)
212 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
213 struct ata_device *atadev = device_get_softc(dev);
216 * if we detect that the device isn't a real SATA device we limit
217 * the transfer mode to UDMA5/ATA100.
218 * this works around the problems some devices has with the
219 * Marvell 88SX8030 SATA->PATA converters and UDMA6/ATA133.
221 if (atadev->param.satacapabilities != 0x0000 &&
222 atadev->param.satacapabilities != 0xffff) {
223 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
224 ata_limit_mode(dev, mode, ATA_UDMA6)))
225 atadev->mode = ctlr->chip->max_dma;
228 mode = ata_limit_mode(dev, mode, ATA_UDMA5);
229 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
235 ata_sata_connect(struct ata_channel *ch)
240 /* wait up to 1 second for "connect well" */
241 for (timeout = 0; timeout < 100 ; timeout++) {
242 status = ATA_IDX_INL(ch, ATA_SSTATUS);
243 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1 ||
244 (status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)
248 if (timeout >= 100) {
250 device_printf(ch->dev, "SATA connect status=%08x\n", status);
254 /* clear SATA error register */
255 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
257 /* find out what type device we got poll for spec'd 31 seconds */
258 /* XXX SOS 10 secs for now as I have little patience */
260 for (timeout = 0; timeout < 1000; timeout++) {
261 if (ATA_IDX_INB(ch, ATA_STATUS) & ATA_S_BUSY)
267 device_printf(ch->dev, "SATA connect ready time=%dms\n", timeout * 10);
268 if (timeout < 1000) {
269 if ((ATA_IDX_INB(ch, ATA_CYL_LSB) == ATAPI_MAGIC_LSB) &&
270 (ATA_IDX_INB(ch, ATA_CYL_MSB) == ATAPI_MAGIC_MSB))
271 ch->devices = ATA_ATAPI_MASTER;
273 ch->devices = ATA_ATA_MASTER;
276 device_printf(ch->dev, "sata_connect devices=0x%b\n",
277 ch->devices, "\20\3ATAPI_MASTER\1ATA_MASTER");
282 ata_sata_phy_enable(struct ata_channel *ch)
286 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE) {
287 ata_sata_connect(ch);
291 for (retry = 0; retry < 10; retry++) {
292 for (loop = 0; loop < 10; loop++) {
293 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_RESET);
295 if ((ATA_IDX_INL(ch, ATA_SCONTROL) &
296 ATA_SC_DET_MASK) == ATA_SC_DET_RESET)
300 for (loop = 0; loop < 10; loop++) {
301 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_IDLE);
303 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == 0) {
304 ata_sata_connect(ch);
312 ata_sata_phy_event(void *context, int dummy)
314 struct ata_connect_task *tp = (struct ata_connect_task *)context;
315 struct ata_channel *ch = device_get_softc(tp->dev);
319 mtx_lock(&Giant); /* newbus suckage it needs Giant */
320 if (tp->action == ATA_C_ATTACH) {
321 device_printf(tp->dev, "CONNECTED\n");
322 ata_sata_connect(ch);
323 ata_identify(tp->dev);
325 if (tp->action == ATA_C_DETACH) {
326 if (!device_get_children(tp->dev, &children, &nchildren)) {
327 for (i = 0; i < nchildren; i++)
329 device_delete_child(tp->dev, children[i]);
330 free(children, M_TEMP);
332 mtx_lock(&ch->state_mtx);
333 ch->state = ATA_IDLE;
334 mtx_unlock(&ch->state_mtx);
335 device_printf(tp->dev, "DISCONNECTED\n");
337 mtx_unlock(&Giant); /* suckage code dealt with, release Giant */
343 * AHCI v1.0 compliant SATA chipset support functions
345 struct ata_ahci_dma_prd {
348 u_int32_t dbc; /* 0 based */
349 #define ATA_AHCI_PRD_MASK 0x003fffff /* max 4MB */
350 #define ATA_AHCI_PRD_IPC (1<<31)
353 struct ata_ahci_cmd_tab {
356 u_int8_t reserved[32];
357 struct ata_ahci_dma_prd prd_tab[16];
360 struct ata_ahci_cmd_list {
362 u_int16_t prd_length; /* PRD entries */
364 u_int64_t cmd_table_phys; /* 128byte aligned */
369 ata_ahci_allocate(device_t dev)
371 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
372 struct ata_channel *ch = device_get_softc(dev);
373 int offset = (ch->unit << 7);
375 /* XXX SOS this is a hack to satisfy various legacy cruft */
376 ch->r_io[ATA_CYL_LSB].res = ctlr->r_res2;
377 ch->r_io[ATA_CYL_LSB].offset = ATA_AHCI_P_SIG + 1 + offset;
378 ch->r_io[ATA_CYL_MSB].res = ctlr->r_res2;
379 ch->r_io[ATA_CYL_MSB].offset = ATA_AHCI_P_SIG + 3 + offset;
380 ch->r_io[ATA_STATUS].res = ctlr->r_res2;
381 ch->r_io[ATA_STATUS].offset = ATA_AHCI_P_TFD + offset;
382 ch->r_io[ATA_ALTSTAT].res = ctlr->r_res2;
383 ch->r_io[ATA_ALTSTAT].offset = ATA_AHCI_P_TFD + offset;
385 /* set the SATA resources */
386 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
387 ch->r_io[ATA_SSTATUS].offset = ATA_AHCI_P_SSTS + offset;
388 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
389 ch->r_io[ATA_SERROR].offset = ATA_AHCI_P_SERR + offset;
390 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
391 ch->r_io[ATA_SCONTROL].offset = ATA_AHCI_P_SCTL + offset;
392 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
393 ch->r_io[ATA_SACTIVE].offset = ATA_AHCI_P_SACT + offset;
395 ch->hw.begin_transaction = ata_ahci_begin_transaction;
396 ch->hw.end_transaction = ata_ahci_end_transaction;
397 ch->hw.command = NULL; /* not used here */
399 /* setup the work areas */
400 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLB + offset,
401 ch->dma->work_bus + ATA_AHCI_CL_OFFSET);
402 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLBU + offset, 0x00000000);
404 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FB + offset,
405 ch->dma->work_bus + ATA_AHCI_FB_OFFSET);
406 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FBU + offset, 0x00000000);
408 /* enable wanted port interrupts */
409 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset,
410 (ATA_AHCI_P_IX_CPD | ATA_AHCI_P_IX_TFE | ATA_AHCI_P_IX_HBF |
411 ATA_AHCI_P_IX_HBD | ATA_AHCI_P_IX_IF | ATA_AHCI_P_IX_OF |
412 ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC | ATA_AHCI_P_IX_DP |
413 ATA_AHCI_P_IX_UF | ATA_AHCI_P_IX_SDB | ATA_AHCI_P_IX_DS |
414 ATA_AHCI_P_IX_PS | ATA_AHCI_P_IX_DHR));
416 /* start operations on this channel */
417 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
418 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
419 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
424 ata_ahci_setup_fis(u_int8_t *fis, struct ata_request *request)
426 struct ata_device *atadev = device_get_softc(request->dev);
429 /* XXX SOS add ATAPI commands support later */
430 ata_modify_if_48bit(request);
432 fis[idx++] = 0x27; /* host to device */
433 fis[idx++] = 0x80; /* command FIS (note PM goes here) */
434 fis[idx++] = request->u.ata.command;
435 fis[idx++] = request->u.ata.feature;
437 fis[idx++] = request->u.ata.lba;
438 fis[idx++] = request->u.ata.lba >> 8;
439 fis[idx++] = request->u.ata.lba >> 16;
440 fis[idx] = ATA_D_LBA | atadev->unit;
441 if (atadev->flags & ATA_D_48BIT_ACTIVE)
444 fis[idx++] |= (request->u.ata.lba >> 24 & 0x0f);
446 fis[idx++] = request->u.ata.lba >> 24;
447 fis[idx++] = request->u.ata.lba >> 32;
448 fis[idx++] = request->u.ata.lba >> 40;
449 fis[idx++] = request->u.ata.feature >> 8;
451 fis[idx++] = request->u.ata.count;
452 fis[idx++] = request->u.ata.count >> 8;
454 fis[idx++] = ATA_A_4BIT;
463 /* must be called with ATA channel locked and state_mtx held */
465 ata_ahci_begin_transaction(struct ata_request *request)
467 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
468 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
469 struct ata_ahci_cmd_tab *ctp;
470 struct ata_ahci_cmd_list *clp;
471 int fis_size, entries;
474 /* get a piece of the workspace for this request */
475 ctp = (struct ata_ahci_cmd_tab *)
476 (ch->dma->work + ATA_AHCI_CT_OFFSET + (ATA_AHCI_CT_SIZE * tag));
478 /* setup the FIS for this request */ /* XXX SOS ATAPI missing still */
479 if (!(fis_size = ata_ahci_setup_fis(&ctp->cfis[0], request))) {
480 device_printf(request->dev, "setting up SATA FIS failed\n");
481 request->result = EIO;
482 return ATA_OP_FINISHED;
485 /* if request moves data setup and load SG list */
486 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
487 if (ch->dma->load(ch->dev, request->data, request->bytecount,
488 request->flags & ATA_R_READ,
489 ctp->prd_tab, &entries)) {
490 device_printf(request->dev, "setting up DMA failed\n");
491 request->result = EIO;
492 return ATA_OP_FINISHED;
496 /* setup the command list entry */
497 clp = (struct ata_ahci_cmd_list *)
498 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
500 clp->prd_length = entries;
501 clp->cmd_flags = (request->flags & ATA_R_WRITE ? (1<<6) : 0) |
502 (request->flags & ATA_R_ATAPI ? (1<<5) : 0) |
503 (fis_size / sizeof(u_int32_t));
505 clp->cmd_table_phys = htole64(ch->dma->work_bus + ATA_AHCI_CT_OFFSET +
506 (ATA_AHCI_CT_SIZE * tag));
508 /* clear eventual ACTIVE bit */
509 ATA_IDX_OUTL(ch, ATA_SACTIVE, ATA_IDX_INL(ch, ATA_SACTIVE) & (1 << tag));
511 /* issue the command */
512 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CI + (ch->unit << 7), (1 << tag));
514 /* start the timeout */
515 callout_reset(&request->callout, request->timeout * hz,
516 (timeout_t*)ata_timeout, request);
517 return ATA_OP_CONTINUES;
520 /* must be called with ATA channel locked and state_mtx held */
522 ata_ahci_end_transaction(struct ata_request *request)
524 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
525 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
526 struct ata_ahci_cmd_list *clp;
530 /* kill the timeout */
531 callout_stop(&request->callout);
534 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + (ch->unit << 7));
535 request->status = tf_data;
537 /* if error status get details */
538 if (request->status & ATA_S_ERROR)
539 request->error = tf_data >> 8;
541 /* record how much data we actually moved */
542 clp = (struct ata_ahci_cmd_list *)
543 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
544 request->donecount = clp->bytecount;
546 /* release SG list etc */
547 ch->dma->unload(ch->dev);
549 return ATA_OP_FINISHED;
553 ata_ahci_intr(void *data)
555 struct ata_pci_controller *ctlr = data;
556 struct ata_channel *ch;
557 u_int32_t port, status, error, issued;
561 port = ATA_INL(ctlr->r_res2, ATA_AHCI_IS);
563 for (unit = 0; unit < ctlr->channels; unit++) {
564 if (port & (1 << unit)) {
565 if ((ch = ctlr->interrupt[unit].argument)) {
566 struct ata_connect_task *tp;
567 int offset = (ch->unit << 7);
569 error = ATA_INL(ctlr->r_res2, ATA_AHCI_P_SERR + offset);
570 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_SERR + offset, error);
571 status = ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset);
572 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset, status);
573 issued = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CI + offset);
575 /* do we have cold connect surprise */
576 if (status & ATA_AHCI_P_IX_CPD) {
577 printf("ata_ahci_intr status=%08x error=%08x issued=%08x\n",
578 status, error, issued);
581 /* check for and handle connect events */
582 if ((status & ATA_AHCI_P_IX_PC) &&
583 (tp = (struct ata_connect_task *)
584 malloc(sizeof(struct ata_connect_task),
585 M_ATA, M_NOWAIT | M_ZERO))) {
587 device_printf(ch->dev, "CONNECT requested\n");
588 tp->action = ATA_C_ATTACH;
590 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
591 taskqueue_enqueue(taskqueue_thread, &tp->task);
594 /* check for and handle disconnect events */
595 if (((status & (ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC)) ==
596 ATA_AHCI_P_IX_PRC) &&
597 (tp = (struct ata_connect_task *)
598 malloc(sizeof(struct ata_connect_task),
599 M_ATA, M_NOWAIT | M_ZERO))) {
601 device_printf(ch->dev, "DISCONNECT requested\n");
602 tp->action = ATA_C_DETACH;
604 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
605 taskqueue_enqueue(taskqueue_thread, &tp->task);
608 /* any drive action to take care of ? */
609 if (!(issued & (1<<tag)))
610 ctlr->interrupt[unit].function(ch);
614 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, port);
618 ata_ahci_reset(device_t dev)
620 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
621 struct ata_channel *ch = device_get_softc(dev);
623 int offset = (ch->unit << 7);
625 /* kill off all activity on this channel */
626 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
627 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
628 cmd & ~(ATA_AHCI_P_CMD_CR | ATA_AHCI_P_CMD_FR |
629 ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
631 DELAY(500000); /* XXX SOS this is not entirely wrong */
634 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, ATA_AHCI_P_CMD_SUD);
636 ata_sata_phy_enable(ch);
638 /* clear any interrupts pending on this channel */
639 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset,
640 ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset));
642 /* start operations on this channel */
643 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
644 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
645 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
649 ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
651 struct ata_dmasetprd_args *args = xsc;
652 struct ata_ahci_dma_prd *prd = args->dmatab;
655 if (!(args->error = error)) {
656 for (i = 0; i < nsegs; i++) {
657 prd[i].dba = htole64(segs[i].ds_addr);
658 prd[i].dbc = htole32((segs[i].ds_len - 1) & ATA_AHCI_PRD_MASK);
665 ata_ahci_dmainit(device_t dev)
667 struct ata_channel *ch = device_get_softc(dev);
671 /* note start and stop are not used here */
672 ch->dma->setprd = ata_ahci_dmasetprd;
673 ch->dma->max_iosize = 8192 * DEV_BSIZE;
679 * Acard chipset support functions
682 ata_acard_ident(device_t dev)
684 struct ata_pci_controller *ctlr = device_get_softc(dev);
685 struct ata_chip_id *idx;
686 static struct ata_chip_id ids[] =
687 {{ ATA_ATP850R, 0, ATPOLD, 0x00, ATA_UDMA2, "Acard ATP850" },
688 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "Acard ATP860A" },
689 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "Acard ATP860R" },
690 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "Acard ATP865A" },
691 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "Acard ATP865R" },
692 { 0, 0, 0, 0, 0, 0}};
695 if (!(idx = ata_match_chip(dev, ids)))
698 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
699 device_set_desc_copy(dev, buffer);
701 ctlr->chipinit = ata_acard_chipinit;
706 ata_acard_chipinit(device_t dev)
708 struct ata_pci_controller *ctlr = device_get_softc(dev);
709 int rid = ATA_IRQ_RID;
711 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
712 RF_SHAREABLE | RF_ACTIVE))) {
713 device_printf(dev, "unable to map interrupt\n");
716 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
717 ata_acard_intr, ctlr, &ctlr->handle))) {
718 device_printf(dev, "unable to setup interrupt\n");
721 if (ctlr->chip->cfg1 == ATPOLD) {
722 ctlr->setmode = ata_acard_850_setmode;
723 ctlr->locking = ata_serialize;
726 ctlr->setmode = ata_acard_86X_setmode;
731 ata_acard_intr(void *data)
733 struct ata_pci_controller *ctlr = data;
734 struct ata_channel *ch;
737 for (unit = 0; unit < ctlr->channels; unit++) {
738 if (!(ch = ctlr->interrupt[unit].argument))
740 if (ctlr->chip->cfg1 == ATPOLD &&
741 ATA_LOCKING(ch->dev, ATA_LF_WHICH) != unit)
743 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
744 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
746 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
747 ATA_BMSTAT_INTERRUPT)
749 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
751 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
752 ATA_IDX_INB(ch, ATA_BMCMD_PORT)&~ATA_BMCMD_START_STOP);
755 ctlr->interrupt[unit].function(ch);
760 ata_acard_850_setmode(device_t dev, int mode)
762 device_t gparent = GRANDPARENT(dev);
763 struct ata_pci_controller *ctlr = device_get_softc(gparent);
764 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
765 struct ata_device *atadev = device_get_softc(dev);
766 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
769 mode = ata_limit_mode(dev, mode,
770 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
772 /* XXX SOS missing WDMA0+1 + PIO modes */
773 if (mode >= ATA_WDMA2) {
774 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
776 device_printf(dev, "%ssetting %s on %s chip\n",
777 (error) ? "FAILURE " : "",
778 ata_mode2str(mode), ctlr->chip->text);
780 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
782 reg54 &= ~(0x03 << (devno << 1));
783 if (mode >= ATA_UDMA0)
784 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
785 pci_write_config(gparent, 0x54, reg54, 1);
786 pci_write_config(gparent, 0x4a, 0xa6, 1);
787 pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
792 /* we could set PIO mode timings, but we assume the BIOS did that */
796 ata_acard_86X_setmode(device_t dev, int mode)
798 device_t gparent = GRANDPARENT(dev);
799 struct ata_pci_controller *ctlr = device_get_softc(gparent);
800 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
801 struct ata_device *atadev = device_get_softc(dev);
802 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
806 mode = ata_limit_mode(dev, mode,
807 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
809 mode = ata_check_80pin(dev, mode);
811 /* XXX SOS missing WDMA0+1 + PIO modes */
812 if (mode >= ATA_WDMA2) {
813 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
815 device_printf(dev, "%ssetting %s on %s chip\n",
816 (error) ? "FAILURE " : "",
817 ata_mode2str(mode), ctlr->chip->text);
819 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
821 reg44 &= ~(0x000f << (devno << 2));
822 if (mode >= ATA_UDMA0)
823 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
824 pci_write_config(gparent, 0x44, reg44, 2);
825 pci_write_config(gparent, 0x4a, 0xa6, 1);
826 pci_write_config(gparent, 0x40 + devno, 0x31, 1);
831 /* we could set PIO mode timings, but we assume the BIOS did that */
836 * Acer Labs Inc (ALI) chipset support functions
839 ata_ali_ident(device_t dev)
841 struct ata_pci_controller *ctlr = device_get_softc(dev);
842 struct ata_chip_id *idx;
843 static struct ata_chip_id ids[] =
844 {{ ATA_ALI_5289, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5289" },
845 { ATA_ALI_5287, 0x00, 4, ALISATA, ATA_SA150, "AcerLabs M5287" },
846 { ATA_ALI_5281, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5281" },
847 { ATA_ALI_5229, 0xc5, 0, ALINEW, ATA_UDMA6, "AcerLabs M5229" },
848 { ATA_ALI_5229, 0xc4, 0, ALINEW, ATA_UDMA5, "AcerLabs M5229" },
849 { ATA_ALI_5229, 0xc2, 0, ALINEW, ATA_UDMA4, "AcerLabs M5229" },
850 { ATA_ALI_5229, 0x20, 0, ALIOLD, ATA_UDMA2, "AcerLabs M5229" },
851 { ATA_ALI_5229, 0x00, 0, ALIOLD, ATA_WDMA2, "AcerLabs M5229" },
852 { 0, 0, 0, 0, 0, 0}};
855 if (!(idx = ata_match_chip(dev, ids)))
858 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
859 device_set_desc_copy(dev, buffer);
861 ctlr->chipinit = ata_ali_chipinit;
866 ata_ali_chipinit(device_t dev)
868 struct ata_pci_controller *ctlr = device_get_softc(dev);
870 if (ata_setup_interrupt(dev))
873 switch (ctlr->chip->cfg2) {
875 pci_write_config(dev, PCIR_COMMAND,
876 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
877 ctlr->channels = ctlr->chip->cfg1;
878 ctlr->allocate = ata_ali_sata_allocate;
879 ctlr->setmode = ata_sata_setmode;
883 /* use device interrupt as byte count end */
884 pci_write_config(dev, 0x4a, pci_read_config(dev, 0x4a, 1) | 0x20, 1);
886 /* enable cable detection and UDMA support on newer chips */
887 pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
889 /* enable ATAPI UDMA mode */
890 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x01, 1);
892 /* only chips with revision > 0xc4 can do 48bit DMA */
893 if (ctlr->chip->chiprev <= 0xc4)
895 "using PIO transfers above 137GB as workaround for "
896 "48bit DMA access bug, expect reduced performance\n");
897 ctlr->reset = ata_ali_reset;
898 ctlr->allocate = ata_ali_allocate;
899 ctlr->setmode = ata_ali_setmode;
903 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
904 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x03, 1);
905 ctlr->setmode = ata_ali_setmode;
912 ata_ali_allocate(device_t dev)
914 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
915 struct ata_channel *ch = device_get_softc(dev);
917 /* setup the usual register normal pci style */
918 ata_pci_allocate(dev);
920 if (ctlr->chip->chiprev <= 0xc4)
921 ch->flags |= ATA_NO_48BIT_DMA;
927 ata_ali_sata_allocate(device_t dev)
929 device_t parent = device_get_parent(dev);
930 struct ata_pci_controller *ctlr = device_get_softc(parent);
931 struct ata_channel *ch = device_get_softc(dev);
932 struct resource *io = NULL, *ctlio = NULL;
933 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
936 rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
937 io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
941 rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
942 ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
944 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
948 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
949 ch->r_io[i].res = io;
950 ch->r_io[i].offset = i + (unit10 ? 8 : 0);
952 ch->r_io[ATA_CONTROL].res = ctlio;
953 ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
954 ch->r_io[ATA_IDX_ADDR].res = io;
955 ata_default_registers(dev);
957 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
958 ch->r_io[i].res = ctlr->r_res1;
959 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
962 ch->flags |= ATA_NO_SLAVE;
964 /* XXX SOS PHY handling awkward in ALI chip not supported yet */
970 ata_ali_reset(device_t dev)
972 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
973 struct ata_channel *ch = device_get_softc(dev);
977 ata_generic_reset(dev);
980 * workaround for datacorruption bug found on at least SUN Blade-100
981 * find the ISA function on the southbridge and disable then enable
982 * the ATA channel tristate buffer
984 if (ctlr->chip->chiprev == 0xc3 || ctlr->chip->chiprev == 0xc2) {
985 if (!device_get_children(GRANDPARENT(dev), &children, &nchildren)) {
986 for (i = 0; i < nchildren; i++) {
987 if (pci_get_devid(children[i]) == ATA_ALI_1533) {
988 pci_write_config(children[i], 0x58,
989 pci_read_config(children[i], 0x58, 1) &
990 ~(0x04 << ch->unit), 1);
991 pci_write_config(children[i], 0x58,
992 pci_read_config(children[i], 0x58, 1) |
993 (0x04 << ch->unit), 1);
997 free(children, M_TEMP);
1003 ata_ali_setmode(device_t dev, int mode)
1005 device_t gparent = GRANDPARENT(dev);
1006 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1007 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1008 struct ata_device *atadev = device_get_softc(dev);
1009 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1012 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1014 if (ctlr->chip->cfg2 & ALINEW) {
1015 if (mode > ATA_UDMA2 &&
1016 pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
1017 ata_print_cable(dev, "controller");
1022 mode = ata_check_80pin(dev, mode);
1024 if (ctlr->chip->cfg2 & ALIOLD) {
1025 /* doesn't support ATAPI DMA on write */
1026 ch->flags |= ATA_ATAPI_DMA_RO;
1027 if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
1028 /* doesn't support ATAPI DMA on two ATAPI devices */
1029 device_printf(dev, "two atapi devices on this channel, no DMA\n");
1030 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1034 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1037 device_printf(dev, "%ssetting %s on %s chip\n",
1038 (error) ? "FAILURE " : "",
1039 ata_mode2str(mode), ctlr->chip->text);
1041 if (mode >= ATA_UDMA0) {
1042 u_int8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
1043 u_int32_t word54 = pci_read_config(gparent, 0x54, 4);
1045 word54 &= ~(0x000f000f << (devno << 2));
1046 word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
1047 pci_write_config(gparent, 0x54, word54, 4);
1048 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1052 u_int32_t piotimings[] =
1053 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
1054 0x00310001, 0x00440001, 0x00330001, 0x00310001};
1056 pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
1057 ~(0x0008000f << (devno << 2)), 4);
1058 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1059 piotimings[ata_mode2idx(mode)], 4);
1061 atadev->mode = mode;
1067 * American Micro Devices (AMD) chipset support functions
1070 ata_amd_ident(device_t dev)
1072 struct ata_pci_controller *ctlr = device_get_softc(dev);
1073 struct ata_chip_id *idx;
1074 static struct ata_chip_id ids[] =
1075 {{ ATA_AMD756, 0x00, AMDNVIDIA, 0x00, ATA_UDMA4, "AMD 756" },
1076 { ATA_AMD766, 0x00, AMDNVIDIA, AMDCABLE|AMDBUG, ATA_UDMA5, "AMD 766" },
1077 { ATA_AMD768, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA5, "AMD 768" },
1078 { ATA_AMD8111, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA6, "AMD 8111" },
1079 { 0, 0, 0, 0, 0, 0}};
1082 if (!(idx = ata_match_chip(dev, ids)))
1085 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1086 device_set_desc_copy(dev, buffer);
1088 ctlr->chipinit = ata_amd_chipinit;
1093 ata_amd_chipinit(device_t dev)
1095 struct ata_pci_controller *ctlr = device_get_softc(dev);
1097 if (ata_setup_interrupt(dev))
1100 /* disable/set prefetch, postwrite */
1101 if (ctlr->chip->cfg2 & AMDBUG)
1102 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
1104 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
1106 ctlr->setmode = ata_via_family_setmode;
1112 * Cyrix chipset support functions
1115 ata_cyrix_ident(device_t dev)
1117 struct ata_pci_controller *ctlr = device_get_softc(dev);
1119 if (pci_get_devid(dev) == ATA_CYRIX_5530) {
1120 device_set_desc(dev, "Cyrix 5530 ATA33 controller");
1121 ctlr->chipinit = ata_cyrix_chipinit;
1128 ata_cyrix_chipinit(device_t dev)
1130 struct ata_pci_controller *ctlr = device_get_softc(dev);
1132 if (ata_setup_interrupt(dev))
1136 ctlr->setmode = ata_cyrix_setmode;
1138 ctlr->setmode = ata_generic_setmode;
1143 ata_cyrix_setmode(device_t dev, int mode)
1145 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1146 struct ata_device *atadev = device_get_softc(dev);
1147 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1148 u_int32_t piotiming[] =
1149 { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
1150 u_int32_t dmatiming[] = { 0x00077771, 0x00012121, 0x00002020 };
1151 u_int32_t udmatiming[] = { 0x00921250, 0x00911140, 0x00911030 };
1154 ch->dma->alignment = 16;
1155 ch->dma->max_iosize = 126 * DEV_BSIZE;
1157 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1159 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1162 device_printf(dev, "%ssetting %s on Cyrix chip\n",
1163 (error) ? "FAILURE " : "", ata_mode2str(mode));
1165 if (mode >= ATA_UDMA0) {
1166 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1167 0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
1169 else if (mode >= ATA_WDMA0) {
1170 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1171 0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
1174 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1175 0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
1177 atadev->mode = mode;
1183 * Cypress chipset support functions
1186 ata_cypress_ident(device_t dev)
1188 struct ata_pci_controller *ctlr = device_get_softc(dev);
1191 * the Cypress chip is a mess, it contains two ATA functions, but
1192 * both channels are visible on the first one.
1193 * simply ignore the second function for now, as the right
1194 * solution (ignoring the second channel on the first function)
1195 * doesn't work with the crappy ATA interrupt setup on the alpha.
1197 if (pci_get_devid(dev) == ATA_CYPRESS_82C693 &&
1198 pci_get_function(dev) == 1 &&
1199 pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
1200 device_set_desc(dev, "Cypress 82C693 ATA controller");
1201 ctlr->chipinit = ata_cypress_chipinit;
1208 ata_cypress_chipinit(device_t dev)
1210 struct ata_pci_controller *ctlr = device_get_softc(dev);
1212 if (ata_setup_interrupt(dev))
1215 ctlr->setmode = ata_cypress_setmode;
1220 ata_cypress_setmode(device_t dev, int mode)
1222 device_t gparent = GRANDPARENT(dev);
1223 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1224 struct ata_device *atadev = device_get_softc(dev);
1227 mode = ata_limit_mode(dev, mode, ATA_WDMA2);
1229 /* XXX SOS missing WDMA0+1 + PIO modes */
1230 if (mode == ATA_WDMA2) {
1231 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1233 device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
1234 error ? "FAILURE " : "");
1236 pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
1237 atadev->mode = mode;
1241 /* we could set PIO mode timings, but we assume the BIOS did that */
1246 * HighPoint chipset support functions
1249 ata_highpoint_ident(device_t dev)
1251 struct ata_pci_controller *ctlr = device_get_softc(dev);
1252 struct ata_chip_id *idx;
1253 static struct ata_chip_id ids[] =
1254 {{ ATA_HPT374, 0x07, HPT374, 0x00, ATA_UDMA6, "HighPoint HPT374" },
1255 { ATA_HPT372, 0x02, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372N" },
1256 { ATA_HPT372, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372" },
1257 { ATA_HPT371, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT371" },
1258 { ATA_HPT366, 0x05, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372" },
1259 { ATA_HPT366, 0x03, HPT370, 0x00, ATA_UDMA5, "HighPoint HPT370" },
1260 { ATA_HPT366, 0x02, HPT366, 0x00, ATA_UDMA4, "HighPoint HPT368" },
1261 { ATA_HPT366, 0x00, HPT366, HPTOLD, ATA_UDMA4, "HighPoint HPT366" },
1262 { ATA_HPT302, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT302" },
1263 { 0, 0, 0, 0, 0, 0}};
1266 if (!(idx = ata_match_chip(dev, ids)))
1269 strcpy(buffer, idx->text);
1270 if (idx->cfg1 == HPT374) {
1271 if (pci_get_function(dev) == 0)
1272 strcat(buffer, " (channel 0+1)");
1273 else if (pci_get_function(dev) == 1)
1274 strcat(buffer, " (channel 2+3)");
1276 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
1277 device_set_desc_copy(dev, buffer);
1279 ctlr->chipinit = ata_highpoint_chipinit;
1284 ata_highpoint_chipinit(device_t dev)
1286 struct ata_pci_controller *ctlr = device_get_softc(dev);
1287 int rid = ATA_IRQ_RID;
1289 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1290 RF_SHAREABLE | RF_ACTIVE))) {
1291 device_printf(dev, "unable to map interrupt\n");
1294 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1295 ata_highpoint_intr, ctlr, &ctlr->handle))) {
1296 device_printf(dev, "unable to setup interrupt\n");
1300 if (ctlr->chip->cfg2 == HPTOLD) {
1301 /* disable interrupt prediction */
1302 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
1305 /* disable interrupt prediction */
1306 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
1307 pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
1309 /* enable interrupts */
1310 pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
1312 /* set clocks etc */
1313 if (ctlr->chip->cfg1 < HPT372)
1314 pci_write_config(dev, 0x5b, 0x22, 1);
1316 pci_write_config(dev, 0x5b,
1317 (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
1319 ctlr->setmode = ata_highpoint_setmode;
1324 ata_highpoint_intr(void *data)
1326 struct ata_pci_controller *ctlr = data;
1327 struct ata_channel *ch;
1330 for (unit = 0; unit < ctlr->channels; unit++) {
1331 if (!(ch = ctlr->interrupt[unit].argument))
1334 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1336 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1337 ATA_BMSTAT_INTERRUPT)
1339 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1342 ctlr->interrupt[unit].function(ch);
1347 ata_highpoint_setmode(device_t dev, int mode)
1349 device_t gparent = GRANDPARENT(dev);
1350 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1351 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1352 struct ata_device *atadev = device_get_softc(dev);
1353 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1355 u_int32_t timings33[][4] = {
1356 /* HPT366 HPT370 HPT372 HPT374 mode */
1357 { 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a }, /* PIO 0 */
1358 { 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 }, /* PIO 1 */
1359 { 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 }, /* PIO 2 */
1360 { 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 }, /* PIO 3 */
1361 { 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 }, /* PIO 4 */
1362 { 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea }, /* MWDMA 0 */
1363 { 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 }, /* MWDMA 1 */
1364 { 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 }, /* MWDMA 2 */
1365 { 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea }, /* UDMA 0 */
1366 { 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 }, /* UDMA 1 */
1367 { 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 }, /* UDMA 2 */
1368 { 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 }, /* UDMA 3 */
1369 { 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 }, /* UDMA 4 */
1370 { 0, 0x16454e31, 0x1c8a9c62, 0x12848242 }, /* UDMA 5 */
1371 { 0, 0, 0x1c869c62, 0x12808242 } /* UDMA 6 */
1374 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1376 if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
1377 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1379 mode = ata_highpoint_check_80pin(dev, mode);
1382 * most if not all HPT chips cant really handle that the device is
1383 * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
1384 * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
1386 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
1387 ata_limit_mode(dev, mode, ATA_UDMA5));
1389 device_printf(dev, "%ssetting %s on HighPoint chip\n",
1390 (error) ? "FAILURE " : "", ata_mode2str(mode));
1392 pci_write_config(gparent, 0x40 + (devno << 2),
1393 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
1394 atadev->mode = mode;
1398 ata_highpoint_check_80pin(device_t dev, int mode)
1400 device_t gparent = GRANDPARENT(dev);
1401 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1402 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1403 u_int8_t reg, val, res;
1405 if (ctlr->chip->cfg1 == HPT374 && pci_get_function(gparent) == 1) {
1406 reg = ch->unit ? 0x57 : 0x53;
1407 val = pci_read_config(gparent, reg, 1);
1408 pci_write_config(gparent, reg, val | 0x80, 1);
1412 val = pci_read_config(gparent, reg, 1);
1413 pci_write_config(gparent, reg, val & 0xfe, 1);
1415 res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1416 pci_write_config(gparent, reg, val, 1);
1418 if (mode > ATA_UDMA2 && res) {
1419 ata_print_cable(dev, "controller");
1427 * Intel chipset support functions
1430 ata_intel_ident(device_t dev)
1432 struct ata_pci_controller *ctlr = device_get_softc(dev);
1433 struct ata_chip_id *idx;
1434 static struct ata_chip_id ids[] =
1435 {{ ATA_I82371FB, 0, 0, 0x00, ATA_WDMA2, "Intel PIIX" },
1436 { ATA_I82371SB, 0, 0, 0x00, ATA_WDMA2, "Intel PIIX3" },
1437 { ATA_I82371AB, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1438 { ATA_I82443MX, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1439 { ATA_I82451NX, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1440 { ATA_I82801AB, 0, 0, 0x00, ATA_UDMA2, "Intel ICH0" },
1441 { ATA_I82801AA, 0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1442 { ATA_I82372FB, 0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1443 { ATA_I82801BA, 0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1444 { ATA_I82801BA_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1445 { ATA_I82801CA, 0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1446 { ATA_I82801CA_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1447 { ATA_I82801DB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1448 { ATA_I82801DB_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1449 { ATA_I82801EB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH5" },
1450 { ATA_I82801EB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1451 { ATA_I82801EB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1452 { ATA_I6300ESB, 0, 0, 0x00, ATA_UDMA5, "Intel 6300ESB" },
1453 { ATA_I6300ESB_S1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1454 { ATA_I6300ESB_R1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1455 { ATA_I82801FB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH6" },
1456 { ATA_I82801FB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1457 { ATA_I82801FB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1458 { ATA_I82801FB_M, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1459 { ATA_I82801GB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH7" },
1460 { ATA_I82801GB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH7" },
1461 { ATA_I82801GB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH7" },
1462 { ATA_I82801GB_M, 0, 0, 0x00, ATA_SA150, "Intel ICH7" },
1463 { ATA_I82801GB_AH, 0, 0, 0x00, ATA_SA150, "Intel ICH7" },
1464 { ATA_I31244, 0, 0, 0x00, ATA_SA150, "Intel 31244" },
1465 { 0, 0, 0, 0, 0, 0}};
1468 if (!(idx = ata_match_chip(dev, ids)))
1471 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1472 device_set_desc_copy(dev, buffer);
1474 ctlr->chipinit = ata_intel_chipinit;
1479 ata_intel_chipinit(device_t dev)
1481 struct ata_pci_controller *ctlr = device_get_softc(dev);
1482 int rid = ATA_IRQ_RID;
1484 if (!ata_legacy(dev)) {
1485 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1486 RF_SHAREABLE | RF_ACTIVE))) {
1487 device_printf(dev, "unable to map interrupt\n");
1490 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1491 ata_intel_intr, ctlr, &ctlr->handle))) {
1492 device_printf(dev, "unable to setup interrupt\n");
1497 /* good old PIIX needs special treatment (not implemented) */
1498 if (ctlr->chip->chipid == ATA_I82371FB) {
1499 ctlr->setmode = ata_intel_old_setmode;
1502 /* the intel 31244 needs special care if in DPA mode */
1503 else if (ctlr->chip->chipid == ATA_I31244) {
1504 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE) {
1505 ctlr->r_type2 = SYS_RES_MEMORY;
1506 ctlr->r_rid2 = PCIR_BAR(0);
1507 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1512 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
1513 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1514 ata_intel_31244_intr, ctlr, &ctlr->handle)) {
1515 device_printf(dev, "unable to setup interrupt\n");
1519 ctlr->reset = ata_intel_31244_reset;
1520 ctlr->allocate = ata_intel_31244_allocate;
1522 ctlr->setmode = ata_sata_setmode;
1525 /* non SATA intel chips goes here */
1526 else if (ctlr->chip->max_dma < ATA_SA150) {
1527 ctlr->setmode = ata_intel_new_setmode;
1530 /* SATA parts can be either compat or AHCI */
1532 /* if we have BAR(5) as a memory resource we should use AHCI mode */
1533 ctlr->r_type2 = SYS_RES_MEMORY;
1534 ctlr->r_rid2 = PCIR_BAR(5);
1535 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1536 &ctlr->r_rid2, RF_ACTIVE))) {
1537 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
1538 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1539 ata_ahci_intr, ctlr, &ctlr->handle)) {
1540 device_printf(dev, "unable to setup interrupt\n");
1544 /* force all ports active "the legacy way" */
1545 pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f,2);
1547 /* enable AHCI mode */
1548 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC, ATA_AHCI_GHC_AE);
1550 /* get the number of HW channels */
1551 ctlr->channels = (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) &
1552 ATA_AHCI_NPMASK) + 1;
1554 /* enable AHCI interrupts */
1555 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
1556 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) | ATA_AHCI_GHC_IE);
1558 ctlr->reset = ata_ahci_reset;
1559 ctlr->dmainit = ata_ahci_dmainit;
1560 ctlr->allocate = ata_ahci_allocate;
1563 ctlr->reset = ata_intel_reset;
1565 ctlr->setmode = ata_sata_setmode;
1566 pci_write_config(dev, PCIR_COMMAND,
1567 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1573 ata_intel_31244_allocate(device_t dev)
1575 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1576 struct ata_channel *ch = device_get_softc(dev);
1580 ch_offset = 0x200 + ch->unit * 0x200;
1582 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
1583 ch->r_io[i].res = ctlr->r_res2;
1584 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
1585 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x06;
1586 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
1587 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
1588 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
1589 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
1590 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
1591 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1d;
1592 ch->r_io[ATA_ERROR].offset = ch_offset + 0x04;
1593 ch->r_io[ATA_STATUS].offset = ch_offset + 0x1c;
1594 ch->r_io[ATA_ALTSTAT].offset = ch_offset + 0x28;
1595 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x29;
1596 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x100;
1597 ch->r_io[ATA_SERROR].offset = ch_offset + 0x104;
1598 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x108;
1599 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x70;
1600 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x72;
1601 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x74;
1603 ch->flags |= ATA_NO_SLAVE;
1604 ata_generic_hw(dev);
1605 ch->hw.command = ata_intel_31244_command;
1607 /* enable PHY state change interrupt */
1608 ATA_OUTL(ctlr->r_res2, 0x4,
1609 ATA_INL(ctlr->r_res2, 0x04) | (0x01 << (ch->unit << 3)));
1614 ata_intel_31244_intr(void *data)
1616 struct ata_pci_controller *ctlr = data;
1617 struct ata_channel *ch;
1620 for (unit = 0; unit < ctlr->channels; unit++) {
1621 if (!(ch = ctlr->interrupt[unit].argument))
1624 /* check for PHY related interrupts on SATA capable HW */
1625 if (ctlr->chip->max_dma >= ATA_SA150) {
1626 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
1627 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
1628 struct ata_connect_task *tp;
1631 /* clear error bits/interrupt */
1632 ATA_IDX_OUTL(ch, ATA_SERROR, error);
1634 /* if we have a connection event deal with it */
1635 if ((error & ATA_SE_PHY_CHANGED) &&
1636 (tp = (struct ata_connect_task *)
1637 malloc(sizeof(struct ata_connect_task),
1638 M_ATA, M_NOWAIT | M_ZERO))) {
1640 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
1641 device_printf(ch->dev, "CONNECT requested\n");
1642 tp->action = ATA_C_ATTACH;
1645 device_printf(ch->dev, "DISCONNECT requested\n");
1646 tp->action = ATA_C_DETACH;
1649 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1650 taskqueue_enqueue(taskqueue_thread, &tp->task);
1655 /* any drive action to take care of ? */
1656 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
1657 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1659 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
1661 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1664 ctlr->interrupt[unit].function(ch);
1669 ata_intel_31244_reset(device_t dev)
1671 struct ata_channel *ch = device_get_softc(dev);
1673 ata_sata_phy_enable(ch);
1677 ata_intel_31244_command(struct ata_request *request)
1679 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
1680 struct ata_device *atadev = device_get_softc(request->dev);
1683 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
1684 return (ata_generic_command(request));
1686 lba = request->u.ata.lba;
1687 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | atadev->unit);
1688 /* enable interrupt */
1689 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
1690 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
1691 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
1692 ATA_IDX_OUTW(ch, ATA_SECTOR, ((lba >> 16) & 0xff00) | (lba & 0x00ff));
1693 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((lba >> 24) & 0xff00) |
1694 ((lba >> 8) & 0x00ff));
1695 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((lba >> 32) & 0xff00) |
1696 ((lba >> 16) & 0x00ff));
1698 /* issue command to controller */
1699 ATA_IDX_OUTB(ch, ATA_COMMAND, request->u.ata.command);
1705 ata_intel_intr(void *data)
1707 struct ata_pci_controller *ctlr = data;
1708 struct ata_channel *ch;
1711 for (unit = 0; unit < ctlr->channels; unit++) {
1712 if (!(ch = ctlr->interrupt[unit].argument))
1715 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1717 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1718 ATA_BMSTAT_INTERRUPT)
1720 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1723 ctlr->interrupt[unit].function(ch);
1728 ata_intel_reset(device_t dev)
1730 device_t parent = device_get_parent(dev);
1731 struct ata_pci_controller *ctlr = device_get_softc(parent);
1732 struct ata_channel *ch = device_get_softc(dev);
1735 /* ICH6 has 4 SATA ports as master/slave on 2 channels so deal with pairs */
1736 if (ctlr->chip->chipid == ATA_I82801FB_S1 ||
1737 ctlr->chip->chipid == ATA_I82801FB_R1 ||
1738 ctlr->chip->chipid == ATA_I82801FB_M) {
1739 mask = (0x0005 << ch->unit);
1742 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1743 if (pci_read_config(parent, 0x90, 1) & 0x04)
1746 mask = (0x0001 << ch->unit);
1747 /* XXX SOS should be in intel_allocate when we grow it */
1748 ch->flags |= ATA_NO_SLAVE;
1751 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1753 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1755 /* wait up to 1 sec for "connect well" */
1756 for (timeout = 0; timeout < 100 ; timeout++) {
1757 if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1758 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1762 ata_generic_reset(dev);
1766 ata_intel_old_setmode(device_t dev, int mode)
1772 ata_intel_new_setmode(device_t dev, int mode)
1774 device_t gparent = GRANDPARENT(dev);
1775 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1776 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1777 struct ata_device *atadev = device_get_softc(dev);
1778 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1779 u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
1780 u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
1781 u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
1782 u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
1783 u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
1784 u_int32_t mask40 = 0, new40 = 0;
1785 u_int8_t mask44 = 0, new44 = 0;
1787 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1788 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1790 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1792 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1793 ata_print_cable(dev, "controller");
1797 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1800 device_printf(dev, "%ssetting %s on %s chip\n",
1801 (error) ? "FAILURE " : "",
1802 ata_mode2str(mode), ctlr->chip->text);
1806 if (mode >= ATA_UDMA0) {
1807 pci_write_config(gparent, 0x48, reg48 | (0x0001 << devno), 2);
1808 pci_write_config(gparent, 0x4a,
1809 (reg4a & ~(0x3 << (devno << 2))) |
1810 ((0x01 + !(mode & 0x01)) << (devno << 2)), 2);
1813 pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
1814 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))), 2);
1817 if (mode >= ATA_UDMA2)
1818 pci_write_config(gparent, 0x54, reg54 | (0x1 << devno), 2);
1820 pci_write_config(gparent, 0x54, reg54 & ~(0x1 << devno), 2);
1822 if (mode >= ATA_UDMA5)
1823 pci_write_config(gparent, 0x54, reg54 | (0x1000 << devno), 2);
1825 pci_write_config(gparent, 0x54, reg54 & ~(0x1000 << devno), 2);
1827 reg40 &= ~0x00ff00ff;
1828 reg40 |= 0x40774077;
1830 if (atadev->unit == ATA_MASTER) {
1832 new40 = timings[ata_mode2idx(mode)] << 8;
1836 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
1837 (timings[ata_mode2idx(mode)] & 0x03);
1845 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
1846 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
1848 atadev->mode = mode;
1853 * Integrated Technology Express Inc. (ITE) chipset support functions
1856 ata_ite_ident(device_t dev)
1858 struct ata_pci_controller *ctlr = device_get_softc(dev);
1859 struct ata_chip_id *idx;
1860 static struct ata_chip_id ids[] =
1861 {{ ATA_IT8212F, 0x00, 0x00, 0x00, ATA_UDMA6, "ITE IT8212F" },
1862 { ATA_IT8211F, 0x00, 0x00, 0x00, ATA_UDMA6, "ITE IT8211F" },
1863 { 0, 0, 0, 0, 0, 0}};
1866 if (!(idx = ata_match_chip(dev, ids)))
1869 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1870 device_set_desc_copy(dev, buffer);
1872 ctlr->chipinit = ata_ite_chipinit;
1877 ata_ite_chipinit(device_t dev)
1879 struct ata_pci_controller *ctlr = device_get_softc(dev);
1881 if (ata_setup_interrupt(dev))
1884 ctlr->setmode = ata_ite_setmode;
1886 /* set PCI mode and 66Mhz reference clock */
1887 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
1889 /* set default active & recover timings */
1890 pci_write_config(dev, 0x54, 0x31, 1);
1891 pci_write_config(dev, 0x56, 0x31, 1);
1896 ata_ite_setmode(device_t dev, int mode)
1898 device_t gparent = GRANDPARENT(dev);
1899 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1900 struct ata_device *atadev = device_get_softc(dev);
1901 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1904 /* correct the mode for what the HW supports */
1905 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
1907 /* check the CBLID bits for 80 conductor cable detection */
1908 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
1909 (ch->unit ? (1<<3) : (1<<2)))) {
1910 ata_print_cable(dev, "controller");
1914 /* set the wanted mode on the device */
1915 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1918 device_printf(dev, "%s setting %s on ITE8212F chip\n",
1919 (error) ? "failed" : "success", ata_mode2str(mode));
1921 /* if the device accepted the mode change, setup the HW accordingly */
1923 if (mode >= ATA_UDMA0) {
1924 u_int8_t udmatiming[] =
1925 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
1927 /* enable UDMA mode */
1928 pci_write_config(gparent, 0x50,
1929 pci_read_config(gparent, 0x50, 1) &
1930 ~(1 << (devno + 3)), 1);
1932 /* set UDMA timing */
1933 pci_write_config(gparent,
1934 0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
1935 udmatiming[mode & ATA_MODE_MASK], 1);
1938 u_int8_t chtiming[] =
1939 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
1941 /* disable UDMA mode */
1942 pci_write_config(gparent, 0x50,
1943 pci_read_config(gparent, 0x50, 1) |
1944 (1 << (devno + 3)), 1);
1946 /* set active and recover timing (shared between master & slave) */
1947 if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
1948 chtiming[ata_mode2idx(mode)])
1949 pci_write_config(gparent, 0x54 + (ch->unit << 2),
1950 chtiming[ata_mode2idx(mode)], 1);
1952 atadev->mode = mode;
1958 * National chipset support functions
1961 ata_national_ident(device_t dev)
1963 struct ata_pci_controller *ctlr = device_get_softc(dev);
1965 /* this chip is a clone of the Cyrix chip, bugs and all */
1966 if (pci_get_devid(dev) == ATA_SC1100) {
1967 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
1968 ctlr->chipinit = ata_national_chipinit;
1974 static device_t nat_host = NULL;
1977 ata_national_chipinit(device_t dev)
1979 struct ata_pci_controller *ctlr = device_get_softc(dev);
1983 if (ata_setup_interrupt(dev))
1986 /* locate the ISA part in the southbridge and enable UDMA33 */
1987 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
1988 for (i = 0; i < nchildren; i++) {
1989 if (pci_get_devid(children[i]) == 0x0510100b) {
1990 nat_host = children[i];
1994 free(children, M_TEMP);
1996 ctlr->setmode = ata_national_setmode;
2001 ata_national_setmode(device_t dev, int mode)
2003 device_t gparent = GRANDPARENT(dev);
2004 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2005 struct ata_device *atadev = device_get_softc(dev);
2006 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2007 u_int32_t piotiming[] =
2008 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
2009 0x00803020, 0x20102010, 0x00100010,
2010 0x00100010, 0x00100010, 0x00100010 };
2011 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
2012 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
2015 ch->dma->alignment = 16;
2016 ch->dma->max_iosize = 126 * DEV_BSIZE;
2018 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2020 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2023 device_printf(dev, "%s setting %s on National chip\n",
2024 (error) ? "failed" : "success", ata_mode2str(mode));
2026 if (mode >= ATA_UDMA0) {
2027 pci_write_config(gparent, 0x44 + (devno << 3),
2028 udmatiming[mode & ATA_MODE_MASK], 4);
2030 else if (mode >= ATA_WDMA0) {
2031 pci_write_config(gparent, 0x44 + (devno << 3),
2032 dmatiming[mode & ATA_MODE_MASK], 4);
2035 pci_write_config(gparent, 0x44 + (devno << 3),
2036 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
2039 pci_write_config(gparent, 0x40 + (devno << 3),
2040 piotiming[ata_mode2idx(mode)], 4);
2041 atadev->mode = mode;
2047 * nVidia chipset support functions
2050 ata_nvidia_ident(device_t dev)
2052 struct ata_pci_controller *ctlr = device_get_softc(dev);
2053 struct ata_chip_id *idx;
2054 static struct ata_chip_id ids[] =
2055 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nVidia nForce" },
2056 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2" },
2057 { ATA_NFORCE2_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2 MCP" },
2058 { ATA_NFORCE2_MCP_S1, 0, 0, 0, ATA_SA150, "nVidia nForce2 MCP" },
2059 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3" },
2060 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 Pro" },
2061 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
2062 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
2063 { ATA_NFORCE3_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 MCP" },
2064 { ATA_NFORCE3_MCP_S1, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
2065 { ATA_NFORCE3_MCP_S2, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
2066 { ATA_NFORCE4, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce4" },
2067 { ATA_NFORCE4_S1, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce4" },
2068 { ATA_NFORCE4_S2, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce4" },
2069 { 0, 0, 0, 0, 0, 0}};
2072 if (!(idx = ata_match_chip(dev, ids)))
2075 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
2076 device_set_desc_copy(dev, buffer);
2078 ctlr->chipinit = ata_nvidia_chipinit;
2083 ata_nvidia_chipinit(device_t dev)
2085 struct ata_pci_controller *ctlr = device_get_softc(dev);
2087 if (ata_setup_interrupt(dev))
2090 if (ctlr->chip->max_dma >= ATA_SA150) {
2091 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
2092 ctlr->r_type2 = SYS_RES_IOPORT;
2094 ctlr->r_type2 = SYS_RES_MEMORY;
2095 ctlr->r_rid2 = PCIR_BAR(5);
2096 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2097 &ctlr->r_rid2, RF_ACTIVE))) {
2098 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
2100 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
2101 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2102 ata_nvidia_intr, ctlr, &ctlr->handle)) {
2103 device_printf(dev, "unable to setup interrupt\n");
2107 /* enable control access */
2108 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
2110 /* clear interrupt status */
2111 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2113 /* enable device and PHY state change interrupts */
2114 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
2116 /* enable PCI interrupt */
2117 pci_write_config(dev, PCIR_COMMAND,
2118 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
2120 ctlr->allocate = ata_nvidia_allocate;
2121 ctlr->reset = ata_nvidia_reset;
2123 ctlr->setmode = ata_sata_setmode;
2126 /* disable prefetch, postwrite */
2127 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
2128 ctlr->setmode = ata_via_family_setmode;
2134 ata_nvidia_allocate(device_t dev)
2136 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2137 struct ata_channel *ch = device_get_softc(dev);
2139 /* setup the usual register normal pci style */
2140 ata_pci_allocate(dev);
2142 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2143 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
2144 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2145 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
2146 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2147 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
2148 ch->flags |= ATA_NO_SLAVE;
2154 ata_nvidia_intr(void *data)
2156 struct ata_pci_controller *ctlr = data;
2157 struct ata_channel *ch;
2158 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
2162 /* get interrupt status */
2163 status = ATA_INB(ctlr->r_res2, offset);
2165 /* clear interrupt status */
2166 ATA_OUTB(ctlr->r_res2, offset, 0xff);
2168 for (unit = 0; unit < ctlr->channels; unit++) {
2169 if ((ch = ctlr->interrupt[unit].argument)) {
2170 struct ata_connect_task *tp;
2171 int maskshift = ch->unit << 2;
2173 /* clear error status */
2174 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2176 /* check for and handle connect events */
2177 if (((status & (0x0c << maskshift)) == (0x04 << maskshift)) &&
2178 (tp = (struct ata_connect_task *)
2179 malloc(sizeof(struct ata_connect_task),
2180 M_ATA, M_NOWAIT | M_ZERO))) {
2182 device_printf(ch->dev, "CONNECT requested\n");
2183 tp->action = ATA_C_ATTACH;
2185 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2186 taskqueue_enqueue(taskqueue_thread, &tp->task);
2189 /* check for and handle disconnect events */
2190 if ((status & (0x08 << maskshift)) &&
2191 (tp = (struct ata_connect_task *)
2192 malloc(sizeof(struct ata_connect_task),
2193 M_ATA, M_NOWAIT | M_ZERO))) {
2195 device_printf(ch->dev, "DISCONNECT requested\n");
2196 tp->action = ATA_C_DETACH;
2198 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2199 taskqueue_enqueue(taskqueue_thread, &tp->task);
2202 /* any drive action to take care of ? */
2203 if (status & (0x01 << maskshift)) {
2204 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2206 ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2208 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
2210 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat&~ATA_BMSTAT_ERROR);
2213 ctlr->interrupt[unit].function(ch);
2220 ata_nvidia_reset(device_t dev)
2222 struct ata_channel *ch = device_get_softc(dev);
2224 ata_sata_phy_enable(ch);
2229 * Promise chipset support functions
2231 #define ATA_PDC_APKT_OFFSET 0x00000010
2232 #define ATA_PDC_HPKT_OFFSET 0x00000040
2233 #define ATA_PDC_ASG_OFFSET 0x00000080
2234 #define ATA_PDC_LSG_OFFSET 0x000000c0
2235 #define ATA_PDC_HSG_OFFSET 0x00000100
2236 #define ATA_PDC_CHN_OFFSET 0x00000400
2237 #define ATA_PDC_BUF_BASE 0x00400000
2238 #define ATA_PDC_BUF_OFFSET 0x00100000
2239 #define ATA_PDC_MAX_HPKT 8
2240 #define ATA_PDC_WRITE_REG 0x00
2241 #define ATA_PDC_WRITE_CTL 0x0e
2242 #define ATA_PDC_WRITE_END 0x08
2243 #define ATA_PDC_WAIT_NBUSY 0x10
2244 #define ATA_PDC_WAIT_READY 0x18
2245 #define ATA_PDC_1B 0x20
2246 #define ATA_PDC_2B 0x40
2248 struct host_packet {
2250 TAILQ_ENTRY(host_packet) chain;
2253 struct ata_promise_sx4 {
2255 TAILQ_HEAD(, host_packet) queue;
2260 ata_promise_ident(device_t dev)
2262 struct ata_pci_controller *ctlr = device_get_softc(dev);
2263 struct ata_chip_id *idx;
2264 static struct ata_chip_id ids[] =
2265 {{ ATA_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "Promise PDC20246" },
2266 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20262" },
2267 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20263" },
2268 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20265" },
2269 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20267" },
2270 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20268" },
2271 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20269" },
2272 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20270" },
2273 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20271" },
2274 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20275" },
2275 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "Promise PDC20276" },
2276 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20277" },
2277 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20318" },
2278 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20319" },
2279 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20371" },
2280 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20375" },
2281 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20376" },
2282 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20377" },
2283 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20378" },
2284 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20379" },
2285 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20571" },
2286 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20575" },
2287 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20579" },
2288 { ATA_PDC20580, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20580" },
2289 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20617" },
2290 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20618" },
2291 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20619" },
2292 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20620" },
2293 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "Promise PDC20621" },
2294 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "Promise PDC20622" },
2295 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40518" },
2296 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40519" },
2297 { ATA_PDC40718, 0, PRMIO, PRSATA2, ATA_SA300, "Promise PDC40718" },
2298 { ATA_PDC40719, 0, PRMIO, PRSATA2, ATA_SA300, "Promise PDC40719" },
2299 { 0, 0, 0, 0, 0, 0}};
2301 uintptr_t devid = 0;
2303 if (!(idx = ata_match_chip(dev, ids)))
2306 /* if we are on a SuperTrak SX6000 dont attach */
2307 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
2308 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
2309 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
2310 devid == ATA_I960RM)
2313 strcpy(buffer, idx->text);
2315 /* if we are on a FastTrak TX4, adjust the interrupt resource */
2316 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
2317 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
2318 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
2319 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
2320 static long start = 0, end = 0;
2322 if (pci_get_slot(dev) == 1) {
2323 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
2324 strcat(buffer, " (channel 0+1)");
2326 else if (pci_get_slot(dev) == 2 && start && end) {
2327 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
2329 strcat(buffer, " (channel 2+3)");
2335 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
2336 device_set_desc_copy(dev, buffer);
2338 ctlr->chipinit = ata_promise_chipinit;
2343 ata_promise_chipinit(device_t dev)
2345 struct ata_pci_controller *ctlr = device_get_softc(dev);
2346 int rid = ATA_IRQ_RID;
2348 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2349 RF_SHAREABLE | RF_ACTIVE))) {
2350 device_printf(dev, "unable to map interrupt\n");
2354 if (ctlr->chip->max_dma >= ATA_SA150)
2355 ctlr->setmode = ata_sata_setmode;
2357 ctlr->setmode = ata_promise_setmode;
2359 switch (ctlr->chip->cfg1) {
2362 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
2364 ctlr->dmainit = ata_promise_new_dmainit;
2368 /* enable burst mode */
2369 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
2371 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2372 ata_promise_old_intr, ctlr, &ctlr->handle))) {
2373 device_printf(dev, "unable to setup interrupt\n");
2379 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2380 ata_promise_tx2_intr, ctlr, &ctlr->handle))) {
2381 device_printf(dev, "unable to setup interrupt\n");
2387 // if (ctlr->r_res1)
2388 // bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
2389 ctlr->r_type1 = SYS_RES_MEMORY;
2390 ctlr->r_rid1 = PCIR_BAR(4);
2391 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
2392 &ctlr->r_rid1, RF_ACTIVE)))
2395 ctlr->r_type2 = SYS_RES_MEMORY;
2396 ctlr->r_rid2 = PCIR_BAR(3);
2397 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2398 &ctlr->r_rid2, RF_ACTIVE))){
2399 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
2402 ctlr->reset = ata_promise_mio_reset;
2403 ctlr->dmainit = ata_promise_mio_dmainit;
2404 ctlr->allocate = ata_promise_mio_allocate;
2406 if (ctlr->chip->cfg2 == PRSX4X) {
2407 struct ata_promise_sx4 *hpkt;
2408 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
2410 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2411 ata_promise_sx4_intr, ctlr, &ctlr->handle))) {
2412 device_printf(dev, "unable to setup interrupt\n");
2413 /* XXX SOS release resources */
2417 /* print info about cache memory */
2418 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
2419 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
2420 ((dimm >> 24) & 0xff),
2421 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
2422 " ECC enabled" : "" );
2424 /* adjust cache memory parameters */
2425 ATA_OUTL(ctlr->r_res2, 0x000c000c,
2426 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
2428 /* setup host packet controls */
2429 hpkt = malloc(sizeof(struct ata_promise_sx4),
2430 M_TEMP, M_NOWAIT | M_ZERO);
2431 mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
2432 TAILQ_INIT(&hpkt->queue);
2433 hpkt->busy = 0; //hpkt->head = hpkt->tail = 0;
2434 device_set_ivars(dev, hpkt);
2439 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2440 ata_promise_mio_intr, ctlr, &ctlr->handle))) {
2441 device_printf(dev, "unable to setup interrupt\n");
2442 /* XXX SOS release resources */
2446 switch (ctlr->chip->cfg2) {
2448 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
2449 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
2453 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
2454 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 3;
2458 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
2463 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
2468 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
2473 /* XXX SOS release resources */
2477 /* XXX SOS release resources */
2484 ata_promise_mio_allocate(device_t dev)
2486 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2487 struct ata_channel *ch = device_get_softc(dev);
2488 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
2491 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2492 ch->r_io[i].res = ctlr->r_res2;
2493 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
2495 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
2496 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
2497 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
2498 ata_default_registers(dev);
2499 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
2500 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
2501 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2502 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
2503 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2504 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
2505 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2506 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
2507 ch->flags |= ATA_NO_SLAVE;
2509 ch->flags |= ATA_USE_16BIT;
2511 ata_generic_hw(dev);
2513 ch->hw.command = ata_promise_sx4_command;
2515 ch->hw.command = ata_promise_mio_command;
2520 ata_promise_mio_intr(void *data)
2522 struct ata_pci_controller *ctlr = data;
2523 struct ata_channel *ch;
2524 u_int32_t vector = 0, status = 0;
2527 switch (ctlr->chip->cfg2) {
2530 /* read and acknowledge interrupt(s) */
2531 vector = ATA_INL(ctlr->r_res2, 0x040);
2533 /* read and clear interface status */
2534 status = ATA_INL(ctlr->r_res2, 0x06c);
2535 ATA_OUTL(ctlr->r_res2, 0x06c, status & 0x000000ff);
2540 /* read and acknowledge interrupt(s) */
2541 vector = ATA_INL(ctlr->r_res2, 0x040);
2542 ATA_OUTL(ctlr->r_res2, 0x040, vector & 0x0000ffff);
2544 /* read and clear interface status */
2545 status = ATA_INL(ctlr->r_res2, 0x060);
2546 ATA_OUTL(ctlr->r_res2, 0x060, status & 0x000000ff);
2550 for (unit = 0; unit < ctlr->channels; unit++) {
2552 if ((ch = ctlr->interrupt[unit].argument)) {
2553 struct ata_connect_task *tp;
2555 /* check for and handle disconnect events */
2556 if ((status & (0x00000001 << unit)) &&
2557 (tp = (struct ata_connect_task *)
2558 malloc(sizeof(struct ata_connect_task),
2559 M_ATA, M_NOWAIT | M_ZERO))) {
2562 device_printf(ch->dev, "DISCONNECT requested\n");
2563 tp->action = ATA_C_DETACH;
2565 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2566 taskqueue_enqueue(taskqueue_thread, &tp->task);
2569 /* check for and handle connect events */
2570 if ((status & (0x00000010 << unit)) &&
2571 (tp = (struct ata_connect_task *)
2572 malloc(sizeof(struct ata_connect_task),
2573 M_ATA, M_NOWAIT | M_ZERO))) {
2576 device_printf(ch->dev, "CONNECT requested\n");
2577 tp->action = ATA_C_ATTACH;
2579 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2580 taskqueue_enqueue(taskqueue_thread, &tp->task);
2583 /* active interrupt(s) need to call the interrupt handler */
2584 if (vector & (1 << (unit + 1)))
2585 if ((ch = ctlr->interrupt[unit].argument))
2586 ctlr->interrupt[unit].function(ch);
2592 ata_promise_sx4_intr(void *data)
2594 struct ata_pci_controller *ctlr = data;
2595 struct ata_channel *ch;
2596 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
2599 for (unit = 0; unit < ctlr->channels; unit++) {
2600 if (vector & (1 << (unit + 1)))
2601 if ((ch = ctlr->interrupt[unit].argument))
2602 ctlr->interrupt[unit].function(ch);
2603 if (vector & (1 << (unit + 5)))
2604 if ((ch = ctlr->interrupt[unit].argument))
2605 ata_promise_queue_hpkt(ctlr,
2606 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
2607 ATA_PDC_HPKT_OFFSET));
2608 if (vector & (1 << (unit + 9))) {
2609 ata_promise_next_hpkt(ctlr);
2610 if ((ch = ctlr->interrupt[unit].argument))
2611 ctlr->interrupt[unit].function(ch);
2613 if (vector & (1 << (unit + 13))) {
2614 ata_promise_next_hpkt(ctlr);
2615 if ((ch = ctlr->interrupt[unit].argument))
2616 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2617 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
2618 ATA_PDC_APKT_OFFSET));
2624 ata_promise_mio_dmainit(device_t dev)
2626 struct ata_channel *ch = device_get_softc(dev);
2630 /* note start and stop are not used here */
2635 ata_promise_mio_reset(device_t dev)
2637 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2638 struct ata_channel *ch = device_get_softc(dev);
2639 struct ata_promise_sx4 *hpktp;
2641 switch (ctlr->chip->cfg2) {
2644 /* softreset channel ATA module */
2645 hpktp = device_get_ivars(ctlr->dev);
2646 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
2648 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
2649 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
2650 ~0x00003f9f) | (ch->unit + 1));
2652 /* softreset HOST module */ /* XXX SOS what about other outstandings */
2653 mtx_lock(&hpktp->mtx);
2654 ATA_OUTL(ctlr->r_res2, 0xc012c,
2655 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
2657 ATA_OUTL(ctlr->r_res2, 0xc012c,
2658 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
2660 mtx_unlock(&hpktp->mtx);
2661 ata_generic_reset(dev);
2666 if ((ctlr->chip->cfg2 == PRSATA) ||
2667 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2669 /* mask plug/unplug intr */
2670 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
2673 /* softreset channels ATA module */
2674 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2676 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2677 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2678 ~0x00003f9f) | (ch->unit + 1));
2680 if ((ctlr->chip->cfg2 == PRSATA) ||
2681 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2683 ata_sata_phy_enable(ch);
2685 /* reset and enable plug/unplug intr */
2686 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
2689 ata_generic_reset(dev);
2694 if ((ctlr->chip->cfg2 == PRSATA2) ||
2695 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2696 /* set portmultiplier port */
2697 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
2699 /* mask plug/unplug intr */
2700 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
2703 /* softreset channels ATA module */
2704 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2706 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2707 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2708 ~0x00003f9f) | (ch->unit + 1));
2710 if ((ctlr->chip->cfg2 == PRSATA2) ||
2711 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2713 /* set PHY mode to "improved" */
2714 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
2715 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
2716 ~0x00000003) | 0x00000001);
2718 ata_sata_phy_enable(ch);
2720 /* reset and enable plug/unplug intr */
2721 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
2723 /* set portmultiplier port */
2724 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
2727 ata_generic_reset(dev);
2734 ata_promise_mio_command(struct ata_request *request)
2736 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2737 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2738 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
2740 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
2742 /* XXX SOS add ATAPI commands support later */
2743 switch (request->u.ata.command) {
2745 return ata_generic_command(request);
2748 case ATA_READ_DMA48:
2749 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
2753 case ATA_WRITE_DMA48:
2754 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
2757 wordp[1] = htole32(ch->dma->sg_bus);
2759 ata_promise_apkt((u_int8_t*)wordp, request);
2761 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
2766 ata_promise_sx4_command(struct ata_request *request)
2768 device_t gparent = GRANDPARENT(request->dev);
2769 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2770 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2771 struct ata_dma_prdentry *prd = ch->dma->sg;
2772 caddr_t window = rman_get_virtual(ctlr->r_res1);
2774 int i, idx, length = 0;
2776 /* XXX SOS add ATAPI commands support later */
2777 switch (request->u.ata.command) {
2782 case ATA_ATA_IDENTIFY:
2786 case ATA_READ_MUL48:
2790 case ATA_WRITE_MUL48:
2791 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2792 return ata_generic_command(request);
2794 case ATA_SETFEATURES:
2795 case ATA_FLUSHCACHE:
2796 case ATA_FLUSHCACHE48:
2799 wordp = (u_int32_t *)
2800 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2801 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
2804 ata_promise_apkt((u_int8_t *)wordp, request);
2805 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2806 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2807 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2808 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
2812 case ATA_READ_DMA48:
2814 case ATA_WRITE_DMA48:
2815 wordp = (u_int32_t *)
2816 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
2819 wordp[idx++] = prd[i].addr;
2820 wordp[idx++] = prd[i].count;
2821 length += (prd[i].count & ~ATA_DMA_EOT);
2822 } while (!(prd[i++].count & ATA_DMA_EOT));
2824 wordp = (u_int32_t *)
2825 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
2826 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2827 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
2829 wordp = (u_int32_t *)
2830 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
2831 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2832 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
2834 wordp = (u_int32_t *)
2835 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
2836 if (request->flags & ATA_R_READ)
2837 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
2838 if (request->flags & ATA_R_WRITE)
2839 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
2840 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
2841 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
2844 wordp = (u_int32_t *)
2845 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2846 if (request->flags & ATA_R_READ)
2847 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
2848 if (request->flags & ATA_R_WRITE)
2849 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
2850 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
2852 ata_promise_apkt((u_int8_t *)wordp, request);
2853 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2855 if (request->flags & ATA_R_READ) {
2856 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
2857 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
2858 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2859 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
2861 if (request->flags & ATA_R_WRITE) {
2862 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
2863 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
2864 ata_promise_queue_hpkt(ctlr,
2865 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
2872 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
2874 struct ata_device *atadev = device_get_softc(request->dev);
2877 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
2878 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
2879 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
2880 bytep[i++] = ATA_A_4BIT;
2882 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2883 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
2884 bytep[i++] = request->u.ata.feature >> 8;
2885 bytep[i++] = request->u.ata.feature;
2886 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
2887 bytep[i++] = request->u.ata.count >> 8;
2888 bytep[i++] = request->u.ata.count;
2889 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
2890 bytep[i++] = request->u.ata.lba >> 24;
2891 bytep[i++] = request->u.ata.lba;
2892 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
2893 bytep[i++] = request->u.ata.lba >> 32;
2894 bytep[i++] = request->u.ata.lba >> 8;
2895 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
2896 bytep[i++] = request->u.ata.lba >> 40;
2897 bytep[i++] = request->u.ata.lba >> 16;
2898 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
2899 bytep[i++] = ATA_D_LBA | atadev->unit;
2902 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
2903 bytep[i++] = request->u.ata.feature;
2904 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
2905 bytep[i++] = request->u.ata.count;
2906 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
2907 bytep[i++] = request->u.ata.lba;
2908 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
2909 bytep[i++] = request->u.ata.lba >> 8;
2910 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
2911 bytep[i++] = request->u.ata.lba >> 16;
2912 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
2913 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
2914 ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
2916 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
2917 bytep[i++] = request->u.ata.command;
2922 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
2924 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
2926 mtx_lock(&hpktp->mtx);
2928 struct host_packet *hp =
2929 malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
2931 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
2935 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
2937 mtx_unlock(&hpktp->mtx);
2941 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
2943 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
2944 struct host_packet *hp;
2946 mtx_lock(&hpktp->mtx);
2947 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
2948 TAILQ_REMOVE(&hpktp->queue, hp, chain);
2949 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
2954 mtx_unlock(&hpktp->mtx);
2958 ata_promise_tx2_intr(void *data)
2960 struct ata_pci_controller *ctlr = data;
2961 struct ata_channel *ch;
2964 for (unit = 0; unit < ctlr->channels; unit++) {
2965 if (!(ch = ctlr->interrupt[unit].argument))
2967 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
2968 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
2969 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2970 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2972 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2973 ATA_BMSTAT_INTERRUPT)
2975 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2978 ctlr->interrupt[unit].function(ch);
2984 ata_promise_old_intr(void *data)
2986 struct ata_pci_controller *ctlr = data;
2987 struct ata_channel *ch;
2990 for (unit = 0; unit < ctlr->channels; unit++) {
2991 if (!(ch = ctlr->interrupt[unit].argument))
2993 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)){
2994 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2995 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2997 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2998 ATA_BMSTAT_INTERRUPT)
3000 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3003 ctlr->interrupt[unit].function(ch);
3009 ata_promise_new_dmastart(device_t dev)
3011 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3012 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3013 struct ata_device *atadev = device_get_softc(dev);
3015 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3016 ATA_OUTB(ctlr->r_res1, 0x11,
3017 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
3018 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20,
3019 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
3020 (ch->dma->cur_iosize >> 1));
3022 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
3023 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
3024 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
3025 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3026 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
3027 ATA_BMCMD_START_STOP);
3028 ch->flags |= ATA_DMA_ACTIVE;
3033 ata_promise_new_dmastop(device_t dev)
3035 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3036 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3037 struct ata_device *atadev = device_get_softc(dev);
3040 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3041 ATA_OUTB(ctlr->r_res1, 0x11,
3042 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
3043 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20, 0);
3045 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
3046 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3047 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3048 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3049 ch->flags &= ~ATA_DMA_ACTIVE;
3054 ata_promise_new_dmareset(device_t dev)
3056 struct ata_channel *ch = device_get_softc(dev);
3058 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3059 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3060 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3061 ch->flags &= ~ATA_DMA_ACTIVE;
3065 ata_promise_new_dmainit(device_t dev)
3067 struct ata_channel *ch = device_get_softc(dev);
3071 ch->dma->start = ata_promise_new_dmastart;
3072 ch->dma->stop = ata_promise_new_dmastop;
3073 ch->dma->reset = ata_promise_new_dmareset;
3078 ata_promise_setmode(device_t dev, int mode)
3080 device_t gparent = GRANDPARENT(dev);
3081 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3082 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3083 struct ata_device *atadev = device_get_softc(dev);
3084 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3086 u_int32_t timings33[][2] = {
3087 /* PROLD PRNEW mode */
3088 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
3089 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
3090 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
3091 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
3092 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
3093 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
3094 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
3095 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
3096 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
3097 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
3098 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
3099 { 0, 0x00424ef6 }, /* UDMA 3 */
3100 { 0, 0x004127f3 }, /* UDMA 4 */
3101 { 0, 0x004127f3 } /* UDMA 5 */
3104 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3106 switch (ctlr->chip->cfg1) {
3109 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
3110 (ch->unit ? 1 << 11 : 1 << 10))) {
3111 ata_print_cable(dev, "controller");
3114 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
3115 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
3119 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3120 if (mode > ATA_UDMA2 &&
3121 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
3122 ata_print_cable(dev, "controller");
3128 if (mode > ATA_UDMA2 &&
3129 (ATA_INL(ctlr->r_res2,
3130 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
3131 (ch->unit << 7)) & 0x01000000)) {
3132 ata_print_cable(dev, "controller");
3138 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3141 device_printf(dev, "%ssetting %s on %s chip\n",
3142 (error) ? "FAILURE " : "",
3143 ata_mode2str(mode), ctlr->chip->text);
3145 if (ctlr->chip->cfg1 < PRTX)
3146 pci_write_config(gparent, 0x60 + (devno << 2),
3147 timings33[ctlr->chip->cfg1][ata_mode2idx(mode)],4);
3148 atadev->mode = mode;
3155 * ServerWorks chipset support functions
3158 ata_serverworks_ident(device_t dev)
3160 struct ata_pci_controller *ctlr = device_get_softc(dev);
3161 struct ata_chip_id *idx;
3162 static struct ata_chip_id ids[] =
3163 {{ ATA_ROSB4, 0x00, SWKS33, 0x00, ATA_UDMA2, "ServerWorks ROSB4" },
3164 { ATA_CSB5, 0x92, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB5" },
3165 { ATA_CSB5, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB5" },
3166 { ATA_CSB6, 0x00, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB6" },
3167 { ATA_CSB6_1, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB6" },
3168 { 0, 0, 0, 0, 0, 0}};
3171 if (!(idx = ata_match_chip(dev, ids)))
3174 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3175 device_set_desc_copy(dev, buffer);
3177 ctlr->chipinit = ata_serverworks_chipinit;
3182 ata_serverworks_chipinit(device_t dev)
3184 struct ata_pci_controller *ctlr = device_get_softc(dev);
3186 if (ata_setup_interrupt(dev))
3189 if (ctlr->chip->cfg1 == SWKS33) {
3193 /* locate the ISA part in the southbridge and enable UDMA33 */
3194 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
3195 for (i = 0; i < nchildren; i++) {
3196 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
3197 pci_write_config(children[i], 0x64,
3198 (pci_read_config(children[i], 0x64, 4) &
3199 ~0x00002000) | 0x00004000, 4);
3203 free(children, M_TEMP);
3207 pci_write_config(dev, 0x5a,
3208 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
3209 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
3211 ctlr->setmode = ata_serverworks_setmode;
3216 ata_serverworks_setmode(device_t dev, int mode)
3218 device_t gparent = GRANDPARENT(dev);
3219 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3220 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3221 struct ata_device *atadev = device_get_softc(dev);
3222 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3223 int offset = (devno ^ 0x01) << 3;
3225 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
3226 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
3227 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
3229 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3231 mode = ata_check_80pin(dev, mode);
3233 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3236 device_printf(dev, "%ssetting %s on %s chip\n",
3237 (error) ? "FAILURE " : "",
3238 ata_mode2str(mode), ctlr->chip->text);
3240 if (mode >= ATA_UDMA0) {
3241 pci_write_config(gparent, 0x56,
3242 (pci_read_config(gparent, 0x56, 2) &
3243 ~(0xf << (devno << 2))) |
3244 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
3245 pci_write_config(gparent, 0x54,
3246 pci_read_config(gparent, 0x54, 1) |
3247 (0x01 << devno), 1);
3248 pci_write_config(gparent, 0x44,
3249 (pci_read_config(gparent, 0x44, 4) &
3250 ~(0xff << offset)) |
3251 (dmatimings[2] << offset), 4);
3253 else if (mode >= ATA_WDMA0) {
3254 pci_write_config(gparent, 0x54,
3255 pci_read_config(gparent, 0x54, 1) &
3256 ~(0x01 << devno), 1);
3257 pci_write_config(gparent, 0x44,
3258 (pci_read_config(gparent, 0x44, 4) &
3259 ~(0xff << offset)) |
3260 (dmatimings[mode & ATA_MODE_MASK] << offset),4);
3263 pci_write_config(gparent, 0x54,
3264 pci_read_config(gparent, 0x54, 1) &
3265 ~(0x01 << devno), 1);
3267 pci_write_config(gparent, 0x40,
3268 (pci_read_config(gparent, 0x40, 4) &
3269 ~(0xff << offset)) |
3270 (piotimings[ata_mode2idx(mode)] << offset), 4);
3271 atadev->mode = mode;
3277 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
3280 ata_sii_ident(device_t dev)
3282 struct ata_pci_controller *ctlr = device_get_softc(dev);
3283 struct ata_chip_id *idx;
3284 static struct ata_chip_id ids[] =
3285 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
3286 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
3287 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
3288 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
3289 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
3290 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
3291 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
3292 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
3293 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
3294 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
3295 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
3296 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
3297 { 0, 0, 0, 0, 0, 0}};
3300 if (!(idx = ata_match_chip(dev, ids)))
3303 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3304 device_set_desc_copy(dev, buffer);
3306 ctlr->chipinit = ata_sii_chipinit;
3311 ata_sii_chipinit(device_t dev)
3313 struct ata_pci_controller *ctlr = device_get_softc(dev);
3314 int rid = ATA_IRQ_RID;
3316 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3317 RF_SHAREABLE | RF_ACTIVE))) {
3318 device_printf(dev, "unable to map interrupt\n");
3322 if (ctlr->chip->cfg1 == SIIMEMIO) {
3323 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3324 ata_sii_intr, ctlr, &ctlr->handle))) {
3325 device_printf(dev, "unable to setup interrupt\n");
3329 ctlr->r_type2 = SYS_RES_MEMORY;
3330 ctlr->r_rid2 = PCIR_BAR(5);
3331 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3332 &ctlr->r_rid2, RF_ACTIVE)))
3335 if (ctlr->chip->cfg2 & SIISETCLK) {
3336 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
3337 pci_write_config(dev, 0x8a,
3338 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
3339 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
3340 device_printf(dev, "%s could not set ATA133 clock\n",
3344 /* if we have 4 channels enable the second set */
3345 if (ctlr->chip->cfg2 & SII4CH) {
3346 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
3350 /* enable PCI interrupt as BIOS might not */
3351 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
3353 /* dont block interrupts from any channel */
3354 pci_write_config(dev, 0x48,
3355 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
3357 ctlr->allocate = ata_sii_allocate;
3358 if (ctlr->chip->max_dma >= ATA_SA150) {
3359 ctlr->reset = ata_sii_reset;
3360 ctlr->setmode = ata_sata_setmode;
3363 ctlr->setmode = ata_sii_setmode;
3366 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3367 ctlr->chip->cfg2 & SIIINTR ?
3368 ata_cmd_intr : ata_cmd_old_intr,
3369 ctlr, &ctlr->handle))) {
3370 device_printf(dev, "unable to setup interrupt\n");
3374 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
3375 device_printf(dev, "HW has secondary channel disabled\n");
3379 /* enable interrupt as BIOS might not */
3380 pci_write_config(dev, 0x71, 0x01, 1);
3382 ctlr->setmode = ata_cmd_setmode;
3388 ata_sii_allocate(device_t dev)
3390 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3391 struct ata_channel *ch = device_get_softc(dev);
3392 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
3395 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
3396 ch->r_io[i].res = ctlr->r_res2;
3397 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
3399 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
3400 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
3401 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
3402 ata_default_registers(dev);
3403 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
3404 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
3405 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
3406 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
3407 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
3408 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
3409 ch->r_io[ATA_BMDEVSPEC_0].res = ctlr->r_res2;
3410 ch->r_io[ATA_BMDEVSPEC_0].offset = 0xa1 + (unit01 << 6) + (unit10 << 8);
3412 if (ctlr->chip->max_dma >= ATA_SA150) {
3413 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3414 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
3415 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3416 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
3417 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3418 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
3419 ch->flags |= ATA_NO_SLAVE;
3421 /* enable PHY state change interrupt */
3422 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
3425 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
3426 /* work around errata in early chips */
3427 ch->dma->boundary = 16 * DEV_BSIZE;
3428 ch->dma->segsize = 15 * DEV_BSIZE;
3431 ata_generic_hw(dev);
3436 ata_sii_intr(void *data)
3438 struct ata_pci_controller *ctlr = data;
3439 struct ata_channel *ch;
3442 for (unit = 0; unit < ctlr->channels; unit++) {
3443 if (!(ch = ctlr->interrupt[unit].argument))
3446 /* check for PHY related interrupts on SATA capable HW */
3447 if (ctlr->chip->max_dma >= ATA_SA150) {
3448 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
3449 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
3450 struct ata_connect_task *tp;
3453 /* clear error bits/interrupt */
3454 ATA_IDX_OUTL(ch, ATA_SERROR, error);
3456 /* if we have a connection event deal with it */
3457 if ((error & ATA_SE_PHY_CHANGED) &&
3458 (tp = (struct ata_connect_task *)
3459 malloc(sizeof(struct ata_connect_task),
3460 M_ATA, M_NOWAIT | M_ZERO))) {
3462 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
3463 device_printf(ch->dev, "CONNECT requested\n");
3464 tp->action = ATA_C_ATTACH;
3467 device_printf(ch->dev, "DISCONNECT requested\n");
3468 tp->action = ATA_C_DETACH;
3471 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3472 taskqueue_enqueue(taskqueue_thread, &tp->task);
3477 /* any drive action to take care of ? */
3478 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_0) & 0x08) {
3479 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3480 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3482 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
3484 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3487 ctlr->interrupt[unit].function(ch);
3494 ata_cmd_intr(void *data)
3496 struct ata_pci_controller *ctlr = data;
3497 struct ata_channel *ch;
3501 for (unit = 0; unit < ctlr->channels; unit++) {
3502 if (!(ch = ctlr->interrupt[unit].argument))
3504 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
3505 (ch->unit ? 0x08 : 0x04))) {
3506 pci_write_config(device_get_parent(ch->dev), 0x71,
3507 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
3508 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3509 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3511 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3512 ATA_BMSTAT_INTERRUPT)
3514 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3517 ctlr->interrupt[unit].function(ch);
3523 ata_cmd_old_intr(void *data)
3525 struct ata_pci_controller *ctlr = data;
3526 struct ata_channel *ch;
3529 for (unit = 0; unit < ctlr->channels; unit++) {
3530 if (!(ch = ctlr->interrupt[unit].argument))
3532 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
3533 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
3535 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
3536 ATA_BMSTAT_INTERRUPT)
3538 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
3541 ctlr->interrupt[unit].function(ch);
3546 ata_sii_reset(device_t dev)
3548 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3549 struct ata_channel *ch = device_get_softc(dev);
3550 int offset = ((ch->unit & 1) << 7) + ((ch->unit & 2) << 8);
3552 /* disable PHY state change interrupt */
3553 ATA_OUTL(ctlr->r_res2, 0x148 + offset, ~(1 << 16));
3555 /* reset controller part for this channel */
3556 ATA_OUTL(ctlr->r_res2, 0x48,
3557 ATA_INL(ctlr->r_res2, 0x48) | (0xc0 >> ch->unit));
3559 ATA_OUTL(ctlr->r_res2, 0x48,
3560 ATA_INL(ctlr->r_res2, 0x48) & ~(0xc0 >> ch->unit));
3562 ata_sata_phy_enable(ch);
3564 /* enable PHY state change interrupt */
3565 ATA_OUTL(ctlr->r_res2, 0x148 + offset, (1 << 16));
3569 ata_sii_setmode(device_t dev, int mode)
3571 device_t gparent = GRANDPARENT(dev);
3572 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3573 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3574 struct ata_device *atadev = device_get_softc(dev);
3575 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
3576 int mreg = ch->unit ? 0x84 : 0x80;
3577 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
3578 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
3581 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3583 if (ctlr->chip->cfg2 & SIISETCLK) {
3584 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
3585 (ch->unit ? 0x02 : 0x01))) {
3586 ata_print_cable(dev, "controller");
3591 mode = ata_check_80pin(dev, mode);
3593 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3596 device_printf(dev, "%ssetting %s on %s chip\n",
3597 (error) ? "FAILURE " : "",
3598 ata_mode2str(mode), ctlr->chip->text);
3602 if (mode >= ATA_UDMA0) {
3603 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
3604 u_int8_t ureg = 0xac + rego;
3606 pci_write_config(gparent, mreg,
3607 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
3608 pci_write_config(gparent, ureg,
3609 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
3610 udmatimings[mode & ATA_MODE_MASK], 1);
3613 else if (mode >= ATA_WDMA0) {
3614 u_int8_t dreg = 0xa8 + rego;
3615 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
3617 pci_write_config(gparent, mreg,
3618 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
3619 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
3623 u_int8_t preg = 0xa4 + rego;
3624 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
3626 pci_write_config(gparent, mreg,
3627 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
3628 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
3630 atadev->mode = mode;
3634 ata_cmd_setmode(device_t dev, int mode)
3636 device_t gparent = GRANDPARENT(dev);
3637 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3638 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3639 struct ata_device *atadev = device_get_softc(dev);
3640 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3643 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3645 mode = ata_check_80pin(dev, mode);
3647 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3650 device_printf(dev, "%ssetting %s on %s chip\n",
3651 (error) ? "FAILURE " : "",
3652 ata_mode2str(mode), ctlr->chip->text);
3654 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
3655 int ureg = ch->unit ? 0x7b : 0x73;
3657 if (mode >= ATA_UDMA0) {
3658 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
3659 { 0x11, 0x42 }, { 0x25, 0x8a },
3660 { 0x15, 0x4a }, { 0x05, 0x0a } };
3662 u_int8_t umode = pci_read_config(gparent, ureg, 1);
3664 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
3665 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
3666 pci_write_config(gparent, ureg, umode, 1);
3668 else if (mode >= ATA_WDMA0) {
3669 int dmatimings[] = { 0x87, 0x32, 0x3f };
3671 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
3672 pci_write_config(gparent, ureg,
3673 pci_read_config(gparent, ureg, 1) &
3674 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3677 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
3678 pci_write_config(gparent, treg,
3679 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
3680 pci_write_config(gparent, ureg,
3681 pci_read_config(gparent, ureg, 1) &
3682 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3684 atadev->mode = mode;
3690 * Silicon Integrated Systems Corp. (SiS) chipset support functions
3693 ata_sis_ident(device_t dev)
3695 struct ata_pci_controller *ctlr = device_get_softc(dev);
3696 struct ata_chip_id *idx;
3697 static struct ata_chip_id ids[] =
3698 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "SiS 182" }, /* south */
3699 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "SiS 181" }, /* south */
3700 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "SiS 180" }, /* south */
3701 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 965" }, /* south */
3702 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 964" }, /* south */
3703 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 963" }, /* south */
3704 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 962" }, /* south */
3706 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 745" }, /* 1chip */
3707 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 735" }, /* 1chip */
3708 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 733" }, /* 1chip */
3709 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "SiS 730" }, /* 1chip */
3711 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 635" }, /* 1chip */
3712 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 633" }, /* unknown */
3713 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "SiS 630S"}, /* 1chip */
3714 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "SiS 630" }, /* 1chip */
3715 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "SiS 620" }, /* 1chip */
3717 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "SiS 550" },
3718 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "SiS 540" },
3719 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "SiS 530" },
3721 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "SiS 5513" },
3722 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "SiS 5513" },
3723 { 0, 0, 0, 0, 0, 0 }};
3727 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
3730 if (idx->cfg2 && !found) {
3731 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
3733 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
3734 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
3736 idx->cfg1 = SIS133NEW;
3737 idx->max_dma = ATA_UDMA6;
3738 sprintf(buffer, "SiS 962/963 %s controller",
3739 ata_mode2str(idx->max_dma));
3741 pci_write_config(dev, 0x57, reg57, 1);
3743 if (idx->cfg2 && !found) {
3744 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
3746 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
3747 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
3748 struct ata_chip_id id[] =
3749 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
3752 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
3753 idx->cfg1 = SIS133OLD;
3754 idx->max_dma = ATA_UDMA6;
3757 idx->cfg1 = SIS100NEW;
3758 idx->max_dma = ATA_UDMA5;
3760 sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
3762 pci_write_config(dev, 0x4a, reg4a, 1);
3765 sprintf(buffer,"%s %s controller",idx->text,ata_mode2str(idx->max_dma));
3767 device_set_desc_copy(dev, buffer);
3769 ctlr->chipinit = ata_sis_chipinit;
3774 ata_sis_chipinit(device_t dev)
3776 struct ata_pci_controller *ctlr = device_get_softc(dev);
3778 if (ata_setup_interrupt(dev))
3781 switch (ctlr->chip->cfg1) {
3786 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
3790 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
3793 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
3794 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
3797 ctlr->r_type2 = SYS_RES_IOPORT;
3798 ctlr->r_rid2 = PCIR_BAR(5);
3799 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3800 &ctlr->r_rid2, RF_ACTIVE))) {
3801 pci_write_config(dev, PCIR_COMMAND,
3802 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3803 ctlr->allocate = ata_sis_allocate;
3804 ctlr->reset = ata_sis_reset;
3806 ctlr->setmode = ata_sata_setmode;
3811 ctlr->setmode = ata_sis_setmode;
3816 ata_sis_allocate(device_t dev)
3818 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3819 struct ata_channel *ch = device_get_softc(dev);
3821 /* setup the usual register normal pci style */
3822 ata_pci_allocate(dev);
3824 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3825 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 4);
3826 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3827 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 4);
3828 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3829 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 4);
3830 ch->flags |= ATA_NO_SLAVE;
3832 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
3833 /* XXX SOS unknown how to enable PHY state change interrupt */
3838 ata_sis_reset(device_t dev)
3840 struct ata_channel *ch = device_get_softc(dev);
3842 ata_sata_phy_enable(ch);
3847 ata_sis_setmode(device_t dev, int mode)
3849 device_t gparent = GRANDPARENT(dev);
3850 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3851 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3852 struct ata_device *atadev = device_get_softc(dev);
3853 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3856 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3858 if (ctlr->chip->cfg1 == SIS133NEW) {
3859 if (mode > ATA_UDMA2 &&
3860 pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
3861 ata_print_cable(dev, "controller");
3866 if (mode > ATA_UDMA2 &&
3867 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
3868 ata_print_cable(dev, "controller");
3873 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3876 device_printf(dev, "%ssetting %s on %s chip\n",
3877 (error) ? "FAILURE " : "",
3878 ata_mode2str(mode), ctlr->chip->text);
3880 switch (ctlr->chip->cfg1) {
3882 u_int32_t timings[] =
3883 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
3884 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
3885 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
3888 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
3889 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
3893 u_int16_t timings[] =
3894 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
3895 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
3897 u_int16_t reg = 0x40 + (devno << 1);
3899 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3903 u_int16_t timings[] =
3904 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
3905 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
3906 u_int16_t reg = 0x40 + (devno << 1);
3908 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3914 u_int16_t timings[] =
3915 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
3916 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
3917 u_int16_t reg = 0x40 + (devno << 1);
3919 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3923 atadev->mode = mode;
3928 /* VIA Technologies Inc. chipset support functions */
3930 ata_via_ident(device_t dev)
3932 struct ata_pci_controller *ctlr = device_get_softc(dev);
3933 struct ata_chip_id *idx;
3934 static struct ata_chip_id ids[] =
3935 {{ ATA_VIA82C586, 0x02, VIA33, 0x00, ATA_UDMA2, "VIA 82C586B" },
3936 { ATA_VIA82C586, 0x00, VIA33, 0x00, ATA_WDMA2, "VIA 82C586" },
3937 { ATA_VIA82C596, 0x12, VIA66, VIACLK, ATA_UDMA4, "VIA 82C596B" },
3938 { ATA_VIA82C596, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C596" },
3939 { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "VIA 82C686B"},
3940 { ATA_VIA82C686, 0x10, VIA66, VIACLK, ATA_UDMA4, "VIA 82C686A" },
3941 { ATA_VIA82C686, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C686" },
3942 { ATA_VIA8231, 0x00, VIA100, VIABUG, ATA_UDMA5, "VIA 8231" },
3943 { ATA_VIA8233, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233" },
3944 { ATA_VIA8233C, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233C" },
3945 { ATA_VIA8233A, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8233A" },
3946 { ATA_VIA8235, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8235" },
3947 { ATA_VIA8237, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8237" },
3948 { 0, 0, 0, 0, 0, 0 }};
3949 static struct ata_chip_id new_ids[] =
3950 {{ ATA_VIA6410, 0x00, 0, 0x00, ATA_UDMA6, "VIA 6410" },
3951 { ATA_VIA6420, 0x00, 7, 0x00, ATA_SA150, "VIA 6420" },
3952 { ATA_VIA6421, 0x00, 6, VIABAR, ATA_SA150, "VIA 6421" },
3953 { 0, 0, 0, 0, 0, 0 }};
3956 if (pci_get_devid(dev) == ATA_VIA82C571) {
3957 if (!(idx = ata_find_chip(dev, ids, -99)))
3961 if (!(idx = ata_match_chip(dev, new_ids)))
3965 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3966 device_set_desc_copy(dev, buffer);
3968 ctlr->chipinit = ata_via_chipinit;
3973 ata_via_chipinit(device_t dev)
3975 struct ata_pci_controller *ctlr = device_get_softc(dev);
3977 if (ata_setup_interrupt(dev))
3980 if (ctlr->chip->max_dma >= ATA_SA150) {
3981 ctlr->r_type2 = SYS_RES_IOPORT;
3982 ctlr->r_rid2 = PCIR_BAR(5);
3983 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3984 &ctlr->r_rid2, RF_ACTIVE))) {
3985 pci_write_config(dev, PCIR_COMMAND,
3986 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3987 ctlr->allocate = ata_via_allocate;
3988 ctlr->reset = ata_via_reset;
3990 ctlr->setmode = ata_sata_setmode;
3994 /* prepare for ATA-66 on the 82C686a and 82C596b */
3995 if (ctlr->chip->cfg2 & VIACLK)
3996 pci_write_config(dev, 0x50, 0x030b030b, 4);
3998 /* the southbridge might need the data corruption fix */
3999 if (ctlr->chip->cfg2 & VIABUG)
4000 ata_via_southbridge_fixup(dev);
4002 /* set fifo configuration half'n'half */
4003 pci_write_config(dev, 0x43,
4004 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
4006 /* set status register read retry */
4007 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
4009 /* set DMA read & end-of-sector fifo flush */
4010 pci_write_config(dev, 0x46,
4011 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
4013 /* set sector size */
4014 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
4015 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
4017 ctlr->setmode = ata_via_family_setmode;
4022 ata_via_allocate(device_t dev)
4024 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4025 struct ata_channel *ch = device_get_softc(dev);
4028 /* newer SATA chips has resources in one BAR for each channel */
4029 if (ctlr->chip->cfg2 & VIABAR) {
4030 struct resource *r_io;
4033 rid = PCIR_BAR(ch->unit);
4034 if (!(r_io = bus_alloc_resource_any(device_get_parent(dev),
4039 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
4040 ch->r_io[i].res = r_io;
4041 ch->r_io[i].offset = i;
4043 ch->r_io[ATA_CONTROL].res = r_io;
4044 ch->r_io[ATA_CONTROL].offset = 2 + ATA_IOSIZE;
4045 ch->r_io[ATA_IDX_ADDR].res = r_io;
4046 ata_default_registers(dev);
4047 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
4048 ch->r_io[i].res = ctlr->r_res1;
4049 ch->r_io[i].offset = i - ATA_BMCMD_PORT;
4051 ata_generic_hw(dev);
4054 ata_pci_allocate(dev);
4056 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4057 ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
4058 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4059 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
4060 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4061 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
4062 ch->flags |= ATA_NO_SLAVE;
4064 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
4065 /* XXX SOS unknown how to enable PHY state change interrupt */
4070 ata_via_reset(device_t dev)
4072 struct ata_channel *ch = device_get_softc(dev);
4074 ata_sata_phy_enable(ch);
4078 ata_via_southbridge_fixup(device_t dev)
4083 if (device_get_children(device_get_parent(dev), &children, &nchildren))
4086 for (i = 0; i < nchildren; i++) {
4087 if (pci_get_devid(children[i]) == ATA_VIA8363 ||
4088 pci_get_devid(children[i]) == ATA_VIA8371 ||
4089 pci_get_devid(children[i]) == ATA_VIA8662 ||
4090 pci_get_devid(children[i]) == ATA_VIA8361) {
4091 u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
4093 if ((reg76 & 0xf0) != 0xd0) {
4095 "Correcting VIA config for southbridge data corruption bug\n");
4096 pci_write_config(children[i], 0x75, 0x80, 1);
4097 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
4102 free(children, M_TEMP);
4106 /* common code for VIA, AMD & nVidia */
4108 ata_via_family_setmode(device_t dev, int mode)
4110 device_t gparent = GRANDPARENT(dev);
4111 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4112 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4113 struct ata_device *atadev = device_get_softc(dev);
4114 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
4115 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
4117 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* VIA ATA33 */
4118 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* VIA ATA66 */
4119 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* VIA ATA100 */
4120 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
4121 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }}; /* AMD/nVIDIA */
4122 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4123 int reg = 0x53 - devno;
4126 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4128 if (ctlr->chip->cfg2 & AMDCABLE) {
4129 if (mode > ATA_UDMA2 &&
4130 !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
4131 ata_print_cable(dev, "controller");
4136 mode = ata_check_80pin(dev, mode);
4138 if (ctlr->chip->cfg2 & NVIDIA)
4141 if (ctlr->chip->cfg1 != VIA133)
4142 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
4144 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4147 device_printf(dev, "%ssetting %s on %s chip\n",
4148 (error) ? "FAILURE " : "", ata_mode2str(mode),
4151 if (mode >= ATA_UDMA0)
4152 pci_write_config(gparent, reg,
4153 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
4155 pci_write_config(gparent, reg, 0x8b, 1);
4156 atadev->mode = mode;
4161 /* misc functions */
4162 struct ata_chip_id *
4163 ata_match_chip(device_t dev, struct ata_chip_id *index)
4165 while (index->chipid != 0) {
4166 if (pci_get_devid(dev) == index->chipid &&
4167 pci_get_revid(dev) >= index->chiprev)
4174 static struct ata_chip_id *
4175 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
4180 if (device_get_children(device_get_parent(dev), &children, &nchildren))
4183 while (index->chipid != 0) {
4184 for (i = 0; i < nchildren; i++) {
4185 if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
4186 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
4187 pci_get_devid(children[i]) == index->chipid &&
4188 pci_get_revid(children[i]) >= index->chiprev) {
4189 free(children, M_TEMP);
4195 free(children, M_TEMP);
4200 ata_setup_interrupt(device_t dev)
4202 struct ata_pci_controller *ctlr = device_get_softc(dev);
4203 int rid = ATA_IRQ_RID;
4205 if (!ata_legacy(dev)) {
4206 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
4207 RF_SHAREABLE | RF_ACTIVE))) {
4208 device_printf(dev, "unable to map interrupt\n");
4211 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
4212 ata_generic_intr, ctlr, &ctlr->handle))) {
4213 device_printf(dev, "unable to setup interrupt\n");
4220 struct ata_serialize {
4221 struct mtx locked_mtx;
4227 ata_serialize(device_t dev, int flags)
4229 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4230 struct ata_channel *ch = device_get_softc(dev);
4231 struct ata_serialize *serial;
4232 static int inited = 0;
4236 serial = malloc(sizeof(struct ata_serialize),
4237 M_TEMP, M_NOWAIT | M_ZERO);
4238 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
4239 serial->locked_ch = -1;
4240 serial->restart_ch = -1;
4241 device_set_ivars(ctlr->dev, serial);
4245 serial = device_get_ivars(ctlr->dev);
4247 mtx_lock(&serial->locked_mtx);
4250 if (serial->locked_ch == -1)
4251 serial->locked_ch = ch->unit;
4252 if (serial->locked_ch != ch->unit)
4253 serial->restart_ch = ch->unit;
4257 if (serial->locked_ch == ch->unit) {
4258 serial->locked_ch = -1;
4259 if (serial->restart_ch != -1) {
4260 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
4261 serial->restart_ch = -1;
4262 mtx_unlock(&serial->locked_mtx);
4273 res = serial->locked_ch;
4274 mtx_unlock(&serial->locked_mtx);
4279 ata_print_cable(device_t dev, u_int8_t *who)
4282 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
4286 ata_atapi(device_t dev)
4288 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4289 struct ata_device *atadev = device_get_softc(dev);
4291 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
4292 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
4296 ata_check_80pin(device_t dev, int mode)
4298 struct ata_device *atadev = device_get_softc(dev);
4300 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
4301 ata_print_cable(dev, "device");
4308 ata_mode2idx(int mode)
4310 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
4311 return (mode & ATA_MODE_MASK) + 8;
4312 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
4313 return (mode & ATA_MODE_MASK) + 5;
4314 return (mode & ATA_MODE_MASK) - ATA_PIO0;