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);
57 static void ata_generic_intr(void *);
58 static void ata_generic_setmode(device_t, int);
59 static int ata_acard_chipinit(device_t);
60 static void ata_acard_intr(void *);
61 static void ata_acard_850_setmode(device_t, int);
62 static void ata_acard_86X_setmode(device_t, int);
63 static int ata_ali_chipinit(device_t);
64 static int ata_ali_allocate(device_t);
65 static void ata_ali_setmode(device_t, int);
66 static int ata_amd_chipinit(device_t);
67 static int ata_cyrix_chipinit(device_t);
68 static void ata_cyrix_setmode(device_t, int);
69 static int ata_cypress_chipinit(device_t);
70 static void ata_cypress_setmode(device_t, int);
71 static int ata_highpoint_chipinit(device_t);
72 static void ata_highpoint_intr(void *);
73 static void ata_highpoint_setmode(device_t, int);
74 static int ata_highpoint_check_80pin(device_t, int);
75 static int ata_intel_chipinit(device_t);
76 static void ata_intel_intr(void *);
77 static void ata_intel_reset(device_t);
78 static void ata_intel_old_setmode(device_t, int);
79 static void ata_intel_new_setmode(device_t, int);
80 static int ata_ite_chipinit(device_t);
81 static void ata_ite_setmode(device_t, int);
82 static int ata_national_chipinit(device_t);
83 static void ata_national_setmode(device_t, int);
84 static int ata_nvidia_chipinit(device_t);
85 static int ata_nvidia_allocate(device_t);
86 static void ata_nvidia_intr(void *);
87 static void ata_nvidia_reset(device_t);
88 static int ata_promise_chipinit(device_t);
89 static int ata_promise_mio_allocate(device_t);
90 static void ata_promise_mio_intr(void *);
91 static void ata_promise_sx4_intr(void *);
92 static void ata_promise_mio_dmainit(device_t);
93 static void ata_promise_mio_reset(device_t);
94 static int ata_promise_mio_command(device_t dev, u_int8_t command, u_int64_t lba, u_int16_t count, u_int16_t feature);
95 static int ata_promise_sx4_command(device_t dev, u_int8_t command, u_int64_t lba, u_int16_t count, u_int16_t feature);
96 static int ata_promise_apkt(u_int8_t *bytep, device_t dev, u_int8_t command, u_int64_t lba, u_int16_t count, u_int16_t feature);
97 static void ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt);
98 static void ata_promise_next_hpkt(struct ata_pci_controller *ctlr);
99 static void ata_promise_tx2_intr(void *);
100 static void ata_promise_old_intr(void *);
101 static void ata_promise_new_dmainit(device_t);
102 static void ata_promise_setmode(device_t, int);
103 static int ata_serverworks_chipinit(device_t);
104 static void ata_serverworks_setmode(device_t, int);
105 static int ata_sii_chipinit(device_t);
106 static int ata_sii_allocate(device_t);
107 static void ata_sii_intr(void *);
108 static void ata_sii_reset(device_t);
109 static void ata_cmd_intr(void *);
110 static void ata_cmd_old_intr(void *);
111 static void ata_sii_setmode(device_t, int);
112 static void ata_cmd_setmode(device_t, int);
113 static int ata_sis_chipinit(device_t);
114 static int ata_sis_allocate(device_t dev);
115 static void ata_sis_reset(device_t);
116 static void ata_sis_setmode(device_t, int);
117 static int ata_via_chipinit(device_t);
118 static int ata_via_allocate(device_t dev);
119 static void ata_via_reset(device_t);
120 static void ata_via_southbridge_fixup(device_t);
121 static void ata_via_family_setmode(device_t, int);
122 static void ata_print_cable(device_t dev, u_int8_t *who);
123 static int ata_atapi(device_t);
124 static int ata_check_80pin(device_t, int);
125 static struct ata_chip_id *ata_find_chip(device_t, struct ata_chip_id *, int);
126 static int ata_setup_interrupt(device_t);
127 static int ata_serialize(device_t, int);
128 static int ata_mode2idx(int);
131 /* generic or unknown ATA chipset support functions */
133 ata_generic_ident(device_t dev)
135 struct ata_pci_controller *ctlr = device_get_softc(dev);
137 device_set_desc(dev, "GENERIC ATA controller");
138 ctlr->chipinit = ata_generic_chipinit;
143 ata_generic_chipinit(device_t dev)
145 struct ata_pci_controller *ctlr = device_get_softc(dev);
147 if (ata_setup_interrupt(dev))
149 ctlr->setmode = ata_generic_setmode;
154 ata_generic_intr(void *data)
156 struct ata_pci_controller *ctlr = data;
157 struct ata_channel *ch;
160 /* implement this as a toggle instead to balance load XXX */
161 for (unit = 0; unit < ctlr->channels; unit++) {
162 if (!(ch = ctlr->interrupt[unit].argument))
164 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
165 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
167 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
168 ATA_BMSTAT_INTERRUPT)
170 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
173 ctlr->interrupt[unit].function(ch);
178 ata_generic_setmode(device_t dev, int mode)
180 struct ata_device *atadev = device_get_softc(dev);
182 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
183 mode = ata_check_80pin(dev, mode);
184 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
189 /* SATA support functions */
191 ata_sata_setmode(device_t dev, int mode)
193 struct ata_device *atadev = device_get_softc(dev);
196 * if we detect that the device isn't a real SATA device we limit
197 * the transfer mode to UDMA5/ATA100.
198 * this works around the problems some devices has with the
199 * Marvell 88SX8030 SATA->PATA converters and UDMA6/ATA133.
201 if (atadev->param.satacapabilities != 0x0000 &&
202 atadev->param.satacapabilities != 0xffff) {
203 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
204 ata_limit_mode(dev, mode, ATA_UDMA6)))
205 atadev->mode = ATA_SA150;
208 mode = ata_limit_mode(dev, mode, ATA_UDMA5);
209 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
215 ata_sata_connect(struct ata_channel *ch)
220 /* wait up to 1 second for "connect well" */
221 for (timeout = 0; timeout < 100 ; timeout++) {
222 status = ATA_IDX_INL(ch, ATA_SSTATUS);
223 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1 ||
224 (status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)
228 if (timeout >= 100) {
230 device_printf(ch->dev, "SATA connect status=%08x\n", status);
234 /* clear SATA error register */
235 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
237 /* find out what type device we got poll for spec'd 31 seconds */
238 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM);
240 for (timeout = 0; timeout < 3100; timeout++) {
241 if (ATA_IDX_INB(ch, ATA_STATUS) & ATA_S_BUSY)
247 device_printf(ch->dev, "SATA connect ready time=%dms\n", timeout * 10);
248 if ((ATA_IDX_INB(ch, ATA_CYL_LSB) == ATAPI_MAGIC_LSB) &&
249 (ATA_IDX_INB(ch, ATA_CYL_MSB) == ATAPI_MAGIC_MSB))
250 ch->devices = ATA_ATAPI_MASTER;
252 ch->devices = ATA_ATA_MASTER;
258 ata_sata_phy_enable(struct ata_channel *ch)
262 if ((ATA_IDX_INL(ch, ATA_SCONTROL) &
263 ATA_SC_DET_MASK) == ATA_SC_DET_IDLE) {
264 ata_sata_connect(ch);
268 for (retry = 0; retry < 10; retry++) {
269 for (loop = 0; loop < 10; loop++) {
270 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_RESET);
272 if ((ATA_IDX_INL(ch, ATA_SCONTROL) &
273 ATA_SC_DET_MASK) == ATA_SC_DET_RESET)
277 for (loop = 0; loop < 10; loop++) {
278 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_IDLE);
280 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == 0) {
281 ata_sata_connect(ch);
289 ata_sata_phy_event(void *context, int dummy)
291 struct ata_connect_task *tp = (struct ata_connect_task *)context;
295 mtx_lock(&Giant); /* newbus suckage it needs Giant */
296 if (tp->action == ATA_C_ATTACH) {
297 struct ata_channel *ch = device_get_softc(tp->dev);
299 device_printf(tp->dev, "CONNECTED\n");
300 ata_sata_connect(ch);
301 ata_identify(tp->dev);
303 if (tp->action == ATA_C_DETACH) {
304 if (!device_get_children(tp->dev, &children, &nchildren)) {
305 for (i = 0; i < nchildren; i++)
307 device_delete_child(tp->dev, children[i]);
308 free(children, M_TEMP);
310 device_printf(tp->dev, "DISCONNECTED\n");
312 mtx_unlock(&Giant); /* suckage code dealt with, release Giant */
318 * Acard chipset support functions
321 ata_acard_ident(device_t dev)
323 struct ata_pci_controller *ctlr = device_get_softc(dev);
324 struct ata_chip_id *idx;
325 static struct ata_chip_id ids[] =
326 {{ ATA_ATP850R, 0, ATPOLD, 0x00, ATA_UDMA2, "Acard ATP850" },
327 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "Acard ATP860A" },
328 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "Acard ATP860R" },
329 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "Acard ATP865A" },
330 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "Acard ATP865R" },
331 { 0, 0, 0, 0, 0, 0}};
334 if (!(idx = ata_match_chip(dev, ids)))
337 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
338 device_set_desc_copy(dev, buffer);
340 ctlr->chipinit = ata_acard_chipinit;
345 ata_acard_chipinit(device_t dev)
347 struct ata_pci_controller *ctlr = device_get_softc(dev);
348 int rid = ATA_IRQ_RID;
350 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
351 RF_SHAREABLE | RF_ACTIVE))) {
352 device_printf(dev, "unable to map interrupt\n");
355 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
356 ata_acard_intr, ctlr, &ctlr->handle))) {
357 device_printf(dev, "unable to setup interrupt\n");
360 if (ctlr->chip->cfg1 == ATPOLD) {
361 ctlr->setmode = ata_acard_850_setmode;
362 ctlr->locking = ata_serialize;
365 ctlr->setmode = ata_acard_86X_setmode;
370 ata_acard_intr(void *data)
372 struct ata_pci_controller *ctlr = data;
373 struct ata_channel *ch;
376 /* implement this as a toggle instead to balance load XXX */
377 for (unit = 0; unit < ctlr->channels; unit++) {
378 if (!(ch = ctlr->interrupt[unit].argument))
380 if (ctlr->chip->cfg1 == ATPOLD &&
381 ATA_LOCKING(ch->dev, ATA_LF_WHICH) != unit)
383 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
384 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
386 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
387 ATA_BMSTAT_INTERRUPT)
389 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
391 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
392 ATA_IDX_INB(ch, ATA_BMCMD_PORT)&~ATA_BMCMD_START_STOP);
395 ctlr->interrupt[unit].function(ch);
400 ata_acard_850_setmode(device_t dev, int mode)
402 device_t gparent = GRANDPARENT(dev);
403 struct ata_pci_controller *ctlr = device_get_softc(gparent);
404 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
405 struct ata_device *atadev = device_get_softc(dev);
406 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
409 mode = ata_limit_mode(dev, mode,
410 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
412 /* XXX SOS missing WDMA0+1 + PIO modes */
413 if (mode >= ATA_WDMA2) {
414 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
416 device_printf(dev, "%ssetting %s on %s chip\n",
417 (error) ? "FAILURE " : "",
418 ata_mode2str(mode), ctlr->chip->text);
420 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
422 reg54 &= ~(0x03 << (devno << 1));
423 if (mode >= ATA_UDMA0)
424 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
425 pci_write_config(gparent, 0x54, reg54, 1);
426 pci_write_config(gparent, 0x4a, 0xa6, 1);
427 pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
432 /* we could set PIO mode timings, but we assume the BIOS did that */
436 ata_acard_86X_setmode(device_t dev, int mode)
438 device_t gparent = GRANDPARENT(dev);
439 struct ata_pci_controller *ctlr = device_get_softc(gparent);
440 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
441 struct ata_device *atadev = device_get_softc(dev);
442 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
446 mode = ata_limit_mode(dev, mode,
447 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
449 mode = ata_check_80pin(dev, mode);
451 /* XXX SOS missing WDMA0+1 + PIO modes */
452 if (mode >= ATA_WDMA2) {
453 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
455 device_printf(dev, "%ssetting %s on %s chip\n",
456 (error) ? "FAILURE " : "",
457 ata_mode2str(mode), ctlr->chip->text);
459 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
461 reg44 &= ~(0x000f << (devno << 2));
462 if (mode >= ATA_UDMA0)
463 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
464 pci_write_config(gparent, 0x44, reg44, 2);
465 pci_write_config(gparent, 0x4a, 0xa6, 1);
466 pci_write_config(gparent, 0x40 + devno, 0x31, 1);
471 /* we could set PIO mode timings, but we assume the BIOS did that */
476 * Acer Labs Inc (ALI) chipset support functions
479 ata_ali_ident(device_t dev)
481 struct ata_pci_controller *ctlr = device_get_softc(dev);
482 struct ata_chip_id *idx;
483 static struct ata_chip_id ids[] =
484 {{ ATA_ALI_5289, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5289" },
485 { ATA_ALI_5287, 0x00, 4, ALISATA, ATA_SA150, "AcerLabs M5287" },
486 { ATA_ALI_5281, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5281" },
487 { ATA_ALI_5229, 0xc5, 0, ALINEW, ATA_UDMA6, "AcerLabs M5229" },
488 { ATA_ALI_5229, 0xc4, 0, ALINEW, ATA_UDMA5, "AcerLabs M5229" },
489 { ATA_ALI_5229, 0xc2, 0, ALINEW, ATA_UDMA4, "AcerLabs M5229" },
490 { ATA_ALI_5229, 0x20, 0, ALIOLD, ATA_UDMA2, "AcerLabs M5229" },
491 { ATA_ALI_5229, 0x00, 0, ALIOLD, ATA_WDMA2, "AcerLabs M5229" },
492 { 0, 0, 0, 0, 0, 0}};
495 if (!(idx = ata_match_chip(dev, ids)))
498 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
499 device_set_desc_copy(dev, buffer);
501 ctlr->chipinit = ata_ali_chipinit;
506 ata_ali_chipinit(device_t dev)
508 struct ata_pci_controller *ctlr = device_get_softc(dev);
510 if (ata_setup_interrupt(dev))
513 switch (ctlr->chip->cfg2) {
515 pci_write_config(dev, PCIR_COMMAND,
516 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
517 ctlr->channels = ctlr->chip->cfg1;
518 ctlr->allocate = ata_ali_allocate;
519 ctlr->setmode = ata_sata_setmode;
523 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
524 pci_write_config(dev, 0x53,
525 pci_read_config(dev, 0x53, 1) | 0x01, 1);
527 /* enable cable detection and UDMA support on newer chips */
528 pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
529 ctlr->setmode = ata_ali_setmode;
533 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
534 pci_write_config(dev, 0x53,
535 (pci_read_config(dev, 0x53, 1) & ~0x02) | 0x03, 1);
536 ctlr->setmode = ata_ali_setmode;
543 ata_ali_allocate(device_t dev)
545 device_t parent = device_get_parent(dev);
546 struct ata_pci_controller *ctlr = device_get_softc(parent);
547 struct ata_channel *ch = device_get_softc(dev);
548 struct resource *io = NULL, *ctlio = NULL;
549 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
552 rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
553 io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
557 rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
558 ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
560 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
564 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
565 ch->r_io[i].res = io;
566 ch->r_io[i].offset = i + (unit10 ? 8 : 0);
568 ch->r_io[ATA_CONTROL].res = ctlio;
569 ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
570 ch->r_io[ATA_IDX_ADDR].res = io;
571 ata_default_registers(dev);
573 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
574 ch->r_io[i].res = ctlr->r_res1;
575 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
578 ch->flags |= ATA_NO_SLAVE;
580 /* XXX SOS PHY handling awkward in ALI chip not supported yet */
586 ata_ali_setmode(device_t dev, int mode)
588 device_t gparent = GRANDPARENT(dev);
589 struct ata_pci_controller *ctlr = device_get_softc(gparent);
590 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
591 struct ata_device *atadev = device_get_softc(dev);
592 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
595 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
597 if (ctlr->chip->cfg2 & ALINEW) {
598 if (mode > ATA_UDMA2 &&
599 pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
600 ata_print_cable(dev, "controller");
605 mode = ata_check_80pin(dev, mode);
607 if (ctlr->chip->cfg2 & ALIOLD) {
608 /* doesn't support ATAPI DMA on write */
609 ch->flags |= ATA_ATAPI_DMA_RO;
610 if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
611 /* doesn't support ATAPI DMA on two ATAPI devices */
612 device_printf(dev, "two atapi devices on this channel, no DMA\n");
613 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
617 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
620 device_printf(dev, "%ssetting %s on %s chip\n",
621 (error) ? "FAILURE " : "",
622 ata_mode2str(mode), ctlr->chip->text);
624 if (mode >= ATA_UDMA0) {
625 u_int8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
626 u_int32_t word54 = pci_read_config(gparent, 0x54, 4);
628 word54 &= ~(0x000f000f << (devno << 2));
629 word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
630 pci_write_config(gparent, 0x54, word54, 4);
631 pci_write_config(gparent, 0x58 + (ch->unit << 2),
635 u_int32_t piotimings[] =
636 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
637 0x00310001, 0x00440001, 0x00330001, 0x00310001};
639 pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
640 ~(0x0008000f << (devno << 2)), 4);
641 pci_write_config(gparent, 0x58 + (ch->unit << 2),
642 piotimings[ata_mode2idx(mode)], 4);
650 * American Micro Devices (AMD) chipset support functions
653 ata_amd_ident(device_t dev)
655 struct ata_pci_controller *ctlr = device_get_softc(dev);
656 struct ata_chip_id *idx;
657 static struct ata_chip_id ids[] =
658 {{ ATA_AMD756, 0x00, AMDNVIDIA, 0x00, ATA_UDMA4, "AMD 756" },
659 { ATA_AMD766, 0x00, AMDNVIDIA, AMDCABLE|AMDBUG, ATA_UDMA5, "AMD 766" },
660 { ATA_AMD768, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA5, "AMD 768" },
661 { ATA_AMD8111, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA6, "AMD 8111" },
662 { 0, 0, 0, 0, 0, 0}};
665 if (!(idx = ata_match_chip(dev, ids)))
668 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
669 device_set_desc_copy(dev, buffer);
671 ctlr->chipinit = ata_amd_chipinit;
676 ata_amd_chipinit(device_t dev)
678 struct ata_pci_controller *ctlr = device_get_softc(dev);
680 if (ata_setup_interrupt(dev))
683 /* disable/set prefetch, postwrite */
684 if (ctlr->chip->cfg2 & AMDBUG)
685 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
687 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
689 ctlr->setmode = ata_via_family_setmode;
695 * Cyrix chipset support functions
698 ata_cyrix_ident(device_t dev)
700 struct ata_pci_controller *ctlr = device_get_softc(dev);
702 if (pci_get_devid(dev) == ATA_CYRIX_5530) {
703 device_set_desc(dev, "Cyrix 5530 ATA33 controller");
704 ctlr->chipinit = ata_cyrix_chipinit;
711 ata_cyrix_chipinit(device_t dev)
713 struct ata_pci_controller *ctlr = device_get_softc(dev);
715 if (ata_setup_interrupt(dev))
719 ctlr->setmode = ata_cyrix_setmode;
721 ctlr->setmode = ata_generic_setmode;
726 ata_cyrix_setmode(device_t dev, int mode)
728 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
729 struct ata_device *atadev = device_get_softc(dev);
730 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
731 u_int32_t piotiming[] =
732 { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
733 u_int32_t dmatiming[] = { 0x00077771, 0x00012121, 0x00002020 };
734 u_int32_t udmatiming[] = { 0x00921250, 0x00911140, 0x00911030 };
737 ch->dma->alignment = 16;
738 ch->dma->max_iosize = 126 * DEV_BSIZE;
740 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
742 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
745 device_printf(dev, "%ssetting %s on Cyrix chip\n",
746 (error) ? "FAILURE " : "", ata_mode2str(mode));
748 if (mode >= ATA_UDMA0) {
749 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
750 0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
752 else if (mode >= ATA_WDMA0) {
753 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
754 0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
757 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
758 0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
766 * Cypress chipset support functions
769 ata_cypress_ident(device_t dev)
771 struct ata_pci_controller *ctlr = device_get_softc(dev);
774 * the Cypress chip is a mess, it contains two ATA functions, but
775 * both channels are visible on the first one.
776 * simply ignore the second function for now, as the right
777 * solution (ignoring the second channel on the first function)
778 * doesn't work with the crappy ATA interrupt setup on the alpha.
780 if (pci_get_devid(dev) == ATA_CYPRESS_82C693 &&
781 pci_get_function(dev) == 1 &&
782 pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
783 device_set_desc(dev, "Cypress 82C693 ATA controller");
784 ctlr->chipinit = ata_cypress_chipinit;
791 ata_cypress_chipinit(device_t dev)
793 struct ata_pci_controller *ctlr = device_get_softc(dev);
795 if (ata_setup_interrupt(dev))
798 ctlr->setmode = ata_cypress_setmode;
803 ata_cypress_setmode(device_t dev, int mode)
805 device_t gparent = GRANDPARENT(dev);
806 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
807 struct ata_device *atadev = device_get_softc(dev);
810 mode = ata_limit_mode(dev, mode, ATA_WDMA2);
812 /* XXX missing WDMA0+1 + PIO modes */
813 if (mode == ATA_WDMA2) {
814 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
816 device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
817 error ? "FAILURE " : "");
819 pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
824 /* we could set PIO mode timings, but we assume the BIOS did that */
829 * HighPoint chipset support functions
832 ata_highpoint_ident(device_t dev)
834 struct ata_pci_controller *ctlr = device_get_softc(dev);
835 struct ata_chip_id *idx;
836 static struct ata_chip_id ids[] =
837 {{ ATA_HPT374, 0x07, HPT374, 0x00, ATA_UDMA6, "HighPoint HPT374" },
838 { ATA_HPT372, 0x02, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372N" },
839 { ATA_HPT372, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372" },
840 { ATA_HPT371, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT371" },
841 { ATA_HPT366, 0x05, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372" },
842 { ATA_HPT366, 0x03, HPT370, 0x00, ATA_UDMA5, "HighPoint HPT370" },
843 { ATA_HPT366, 0x02, HPT366, 0x00, ATA_UDMA4, "HighPoint HPT368" },
844 { ATA_HPT366, 0x00, HPT366, HPTOLD, ATA_UDMA4, "HighPoint HPT366" },
845 { ATA_HPT302, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT302" },
846 { 0, 0, 0, 0, 0, 0}};
849 if (!(idx = ata_match_chip(dev, ids)))
852 strcpy(buffer, idx->text);
853 if (idx->cfg1 == HPT374) {
854 if (pci_get_function(dev) == 0)
855 strcat(buffer, " (channel 0+1)");
856 else if (pci_get_function(dev) == 1)
857 strcat(buffer, " (channel 2+3)");
859 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
860 device_set_desc_copy(dev, buffer);
862 ctlr->chipinit = ata_highpoint_chipinit;
867 ata_highpoint_chipinit(device_t dev)
869 struct ata_pci_controller *ctlr = device_get_softc(dev);
870 int rid = ATA_IRQ_RID;
872 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
873 RF_SHAREABLE | RF_ACTIVE))) {
874 device_printf(dev, "unable to map interrupt\n");
877 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
878 ata_highpoint_intr, ctlr, &ctlr->handle))) {
879 device_printf(dev, "unable to setup interrupt\n");
883 if (ctlr->chip->cfg2 == HPTOLD) {
884 /* disable interrupt prediction */
885 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
888 /* disable interrupt prediction */
889 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
890 pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
892 /* enable interrupts */
893 pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
896 if (ctlr->chip->cfg1 < HPT372)
897 pci_write_config(dev, 0x5b, 0x22, 1);
899 pci_write_config(dev, 0x5b,
900 (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
902 ctlr->setmode = ata_highpoint_setmode;
907 ata_highpoint_intr(void *data)
909 struct ata_pci_controller *ctlr = data;
910 struct ata_channel *ch;
913 /* implement this as a toggle instead to balance load XXX */
914 for (unit = 0; unit < ctlr->channels; unit++) {
915 if (!(ch = ctlr->interrupt[unit].argument))
918 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
920 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
921 ATA_BMSTAT_INTERRUPT)
923 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
926 ctlr->interrupt[unit].function(ch);
931 ata_highpoint_setmode(device_t dev, int mode)
933 device_t gparent = GRANDPARENT(dev);
934 struct ata_pci_controller *ctlr = device_get_softc(gparent);
935 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
936 struct ata_device *atadev = device_get_softc(dev);
937 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
939 u_int32_t timings33[][4] = {
940 /* HPT366 HPT370 HPT372 HPT374 mode */
941 { 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a }, /* PIO 0 */
942 { 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 }, /* PIO 1 */
943 { 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 }, /* PIO 2 */
944 { 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 }, /* PIO 3 */
945 { 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 }, /* PIO 4 */
946 { 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea }, /* MWDMA 0 */
947 { 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 }, /* MWDMA 1 */
948 { 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 }, /* MWDMA 2 */
949 { 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea }, /* UDMA 0 */
950 { 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 }, /* UDMA 1 */
951 { 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 }, /* UDMA 2 */
952 { 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 }, /* UDMA 3 */
953 { 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 }, /* UDMA 4 */
954 { 0, 0x16454e31, 0x1c8a9c62, 0x12848242 }, /* UDMA 5 */
955 { 0, 0, 0x1c869c62, 0x12808242 } /* UDMA 6 */
958 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
960 if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
961 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
963 mode = ata_highpoint_check_80pin(dev, mode);
966 * most if not all HPT chips cant really handle that the device is
967 * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
968 * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
970 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
971 ata_limit_mode(dev, mode, ATA_UDMA5));
974 device_printf(dev, "%ssetting %s on HighPoint chip\n",
975 (error) ? "FAILURE " : "", ata_mode2str(mode));
977 pci_write_config(gparent, 0x40 + (devno << 2),
978 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
983 ata_highpoint_check_80pin(device_t dev, int mode)
985 device_t gparent = GRANDPARENT(dev);
986 struct ata_pci_controller *ctlr = device_get_softc(gparent);
987 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
988 u_int8_t reg, val, res;
990 if (ctlr->chip->cfg1 == HPT374 && pci_get_function(gparent) == 1) {
991 reg = ch->unit ? 0x57 : 0x53;
992 val = pci_read_config(gparent, reg, 1);
993 pci_write_config(gparent, reg, val | 0x80, 1);
997 val = pci_read_config(gparent, reg, 1);
998 pci_write_config(gparent, reg, val & 0xfe, 1);
1000 res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1001 pci_write_config(gparent, reg, val, 1);
1003 if (mode > ATA_UDMA2 && res) {
1004 ata_print_cable(dev, "controller");
1012 * Intel chipset support functions
1015 ata_intel_ident(device_t dev)
1017 struct ata_pci_controller *ctlr = device_get_softc(dev);
1018 struct ata_chip_id *idx;
1019 static struct ata_chip_id ids[] =
1020 {{ ATA_I82371FB, 0, 0, 0x00, ATA_WDMA2, "Intel PIIX" },
1021 { ATA_I82371SB, 0, 0, 0x00, ATA_WDMA2, "Intel PIIX3" },
1022 { ATA_I82371AB, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1023 { ATA_I82443MX, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1024 { ATA_I82451NX, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1025 { ATA_I82801AB, 0, 0, 0x00, ATA_UDMA2, "Intel ICH0" },
1026 { ATA_I82801AA, 0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1027 { ATA_I82372FB, 0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1028 { ATA_I82801BA, 0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1029 { ATA_I82801BA_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1030 { ATA_I82801CA, 0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1031 { ATA_I82801CA_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1032 { ATA_I82801DB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1033 { ATA_I82801DB_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1034 { ATA_I82801EB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH5" },
1035 { ATA_I82801EB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1036 { ATA_I82801EB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1037 { ATA_I6300ESB, 0, 0, 0x00, ATA_UDMA5, "Intel 6300ESB" },
1038 { ATA_I6300ESB_S1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1039 { ATA_I6300ESB_R1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1040 { ATA_I82801FB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH6" },
1041 { ATA_I82801FB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1042 { ATA_I82801FB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1043 { ATA_I82801FBM, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1044 { 0, 0, 0, 0, 0, 0}};
1047 if (!(idx = ata_match_chip(dev, ids)))
1050 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1051 device_set_desc_copy(dev, buffer);
1053 ctlr->chipinit = ata_intel_chipinit;
1058 ata_intel_chipinit(device_t dev)
1060 struct ata_pci_controller *ctlr = device_get_softc(dev);
1061 int rid = ATA_IRQ_RID;
1063 if (!ata_legacy(dev)) {
1064 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1065 RF_SHAREABLE | RF_ACTIVE))) {
1066 device_printf(dev, "unable to map interrupt\n");
1069 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1070 ata_intel_intr, ctlr, &ctlr->handle))) {
1071 device_printf(dev, "unable to setup interrupt\n");
1076 if (ctlr->chip->chipid == ATA_I82371FB) {
1077 ctlr->setmode = ata_intel_old_setmode;
1079 else if (ctlr->chip->max_dma < ATA_SA150) {
1080 ctlr->setmode = ata_intel_new_setmode;
1083 pci_write_config(dev, PCIR_COMMAND,
1084 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1085 ctlr->reset = ata_intel_reset;
1086 ctlr->setmode = ata_sata_setmode;
1092 ata_intel_intr(void *data)
1094 struct ata_pci_controller *ctlr = data;
1095 struct ata_channel *ch;
1098 /* implement this as a toggle instead to balance load XXX */
1099 for (unit = 0; unit < ctlr->channels; unit++) {
1100 if (!(ch = ctlr->interrupt[unit].argument))
1103 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1105 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1106 ATA_BMSTAT_INTERRUPT)
1108 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1111 ctlr->interrupt[unit].function(ch);
1116 ata_intel_old_setmode(device_t dev, int mode)
1122 ata_intel_reset(device_t dev)
1124 device_t parent = device_get_parent(dev);
1125 struct ata_pci_controller *ctlr = device_get_softc(parent);
1126 struct ata_channel *ch = device_get_softc(dev);
1129 /* ICH6 has 4 SATA ports as master/slave on 2 channels so deal with pairs */
1130 if (ctlr->chip->chipid == ATA_I82801FB_S1 ||
1131 ctlr->chip->chipid == ATA_I82801FB_R1) {
1132 mask = (0x0005 << ch->unit);
1135 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1136 if (pci_read_config(parent, 0x90, 1) & 0x04)
1139 mask = (0x0001 << ch->unit);
1140 /* XXX SOS should be in intel_allocate when we grow it */
1141 ch->flags |= ATA_NO_SLAVE;
1144 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1146 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1148 /* wait up to 1 sec for "connect well" */
1149 for (timeout = 0; timeout < 100 ; timeout++) {
1150 if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1151 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1155 ata_generic_reset(dev);
1159 ata_intel_new_setmode(device_t dev, int mode)
1161 device_t gparent = GRANDPARENT(dev);
1162 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1163 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1164 struct ata_device *atadev = device_get_softc(dev);
1165 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1166 u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
1167 u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
1168 u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
1169 u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
1170 u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
1171 u_int32_t mask40 = 0, new40 = 0;
1172 u_int8_t mask44 = 0, new44 = 0;
1174 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1175 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1177 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1179 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1180 ata_print_cable(dev, "controller");
1184 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1187 device_printf(dev, "%ssetting %s on %s chip\n",
1188 (error) ? "FAILURE " : "",
1189 ata_mode2str(mode), ctlr->chip->text);
1193 if (mode >= ATA_UDMA0) {
1194 pci_write_config(gparent, 0x48, reg48 | (0x0001 << devno), 2);
1195 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno<<2))) |
1196 (0x01 + !(mode & 0x01)), 2);
1199 pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
1200 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))), 2);
1203 if (mode >= ATA_UDMA2)
1204 pci_write_config(gparent, 0x54, reg54 | (0x1 << devno), 2);
1206 pci_write_config(gparent, 0x54, reg54 & ~(0x1 << devno), 2);
1208 if (mode >= ATA_UDMA5)
1209 pci_write_config(gparent, 0x54, reg54 | (0x1000 << devno), 2);
1211 pci_write_config(gparent, 0x54, reg54 & ~(0x1000 << devno), 2);
1213 reg40 &= ~0x00ff00ff;
1214 reg40 |= 0x40774077;
1216 if (atadev->unit == ATA_MASTER) {
1218 new40 = timings[ata_mode2idx(mode)] << 8;
1222 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
1223 (timings[ata_mode2idx(mode)] & 0x03);
1231 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
1232 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
1234 atadev->mode = mode;
1239 * Integrated Technology Express Inc. (ITE) chipset support functions
1242 ata_ite_ident(device_t dev)
1244 struct ata_pci_controller *ctlr = device_get_softc(dev);
1246 if (pci_get_devid(dev) == ATA_IT8212F) {
1247 device_set_desc(dev, "ITE IT8212F ATA133 controller");
1248 ctlr->chipinit = ata_ite_chipinit;
1255 ata_ite_chipinit(device_t dev)
1257 struct ata_pci_controller *ctlr = device_get_softc(dev);
1259 if (ata_setup_interrupt(dev))
1262 ctlr->setmode = ata_ite_setmode;
1264 /* set PCI mode and 66Mhz reference clock */
1265 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
1267 /* set default active & recover timings */
1268 pci_write_config(dev, 0x54, 0x31, 1);
1269 pci_write_config(dev, 0x56, 0x31, 1);
1274 ata_ite_setmode(device_t dev, int mode)
1276 device_t gparent = GRANDPARENT(dev);
1277 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1278 struct ata_device *atadev = device_get_softc(dev);
1279 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1282 /* correct the mode for what the HW supports */
1283 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
1285 /* check the CBLID bits for 80 conductor cable detection */
1286 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
1287 (ch->unit ? (1<<3) : (1<<2)))) {
1288 ata_print_cable(dev, "controller");
1292 /* set the wanted mode on the device */
1293 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1296 device_printf(dev, "%s setting %s on ITE8212F chip\n",
1297 (error) ? "failed" : "success", ata_mode2str(mode));
1299 /* if the device accepted the mode change, setup the HW accordingly */
1301 if (mode >= ATA_UDMA0) {
1302 u_int8_t udmatiming[] =
1303 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
1305 /* enable UDMA mode */
1306 pci_write_config(gparent, 0x50,
1307 pci_read_config(gparent, 0x50, 1) &
1308 ~(1 << (devno + 3)), 1);
1310 /* set UDMA timing */
1311 pci_write_config(gparent,
1312 0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
1313 udmatiming[mode & ATA_MODE_MASK], 1);
1316 u_int8_t chtiming[] =
1317 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
1319 /* disable UDMA mode */
1320 pci_write_config(gparent, 0x50,
1321 pci_read_config(gparent, 0x50, 1) |
1322 (1 << (devno + 3)), 1);
1324 /* set active and recover timing (shared between master & slave) */
1325 if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
1326 chtiming[ata_mode2idx(mode)])
1327 pci_write_config(gparent, 0x54 + (ch->unit << 2),
1328 chtiming[ata_mode2idx(mode)], 1);
1330 atadev->mode = mode;
1336 * National chipset support functions
1339 ata_national_ident(device_t dev)
1341 struct ata_pci_controller *ctlr = device_get_softc(dev);
1343 /* this chip is a clone of the Cyrix chip, bugs and all */
1344 if (pci_get_devid(dev) == ATA_SC1100) {
1345 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
1346 ctlr->chipinit = ata_national_chipinit;
1352 static device_t nat_host = NULL;
1355 ata_national_chipinit(device_t dev)
1357 struct ata_pci_controller *ctlr = device_get_softc(dev);
1361 if (ata_setup_interrupt(dev))
1364 /* locate the ISA part in the southbridge and enable UDMA33 */
1365 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
1366 for (i = 0; i < nchildren; i++) {
1367 if (pci_get_devid(children[i]) == 0x0510100b) {
1368 nat_host = children[i];
1372 free(children, M_TEMP);
1374 ctlr->setmode = ata_national_setmode;
1379 ata_national_setmode(device_t dev, int mode)
1381 device_t gparent = GRANDPARENT(dev);
1382 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1383 struct ata_device *atadev = device_get_softc(dev);
1384 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1385 u_int32_t piotiming[] =
1386 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
1387 0x00803020, 0x20102010, 0x00100010,
1388 0x00100010, 0x00100010, 0x00100010 };
1389 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
1390 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
1393 ch->dma->alignment = 16;
1394 ch->dma->max_iosize = 126 * DEV_BSIZE;
1396 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1398 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1401 device_printf(dev, "%s setting %s on National chip\n",
1402 (error) ? "failed" : "success", ata_mode2str(mode));
1404 if (mode >= ATA_UDMA0) {
1405 pci_write_config(gparent, 0x44 + (devno << 3),
1406 udmatiming[mode & ATA_MODE_MASK], 4);
1408 else if (mode >= ATA_WDMA0) {
1409 pci_write_config(gparent, 0x44 + (devno << 3),
1410 dmatiming[mode & ATA_MODE_MASK], 4);
1413 pci_write_config(gparent, 0x44 + (devno << 3),
1414 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
1417 pci_write_config(gparent, 0x40 + (devno << 3),
1418 piotiming[ata_mode2idx(mode)], 4);
1419 atadev->mode = mode;
1425 * nVidia chipset support functions
1428 ata_nvidia_ident(device_t dev)
1430 struct ata_pci_controller *ctlr = device_get_softc(dev);
1431 struct ata_chip_id *idx;
1432 static struct ata_chip_id ids[] =
1433 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nVidia nForce" },
1434 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2" },
1435 { ATA_NFORCE2_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2 MCP" },
1436 { ATA_NFORCE2_MCP_S1, 0, 0, 0, ATA_SA150, "nVidia nForce2 MCP" },
1437 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3" },
1438 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 Pro" },
1439 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
1440 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
1441 { ATA_NFORCE3_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 MCP" },
1442 { ATA_NFORCE3_MCP_S1, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
1443 { ATA_NFORCE3_MCP_S2, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
1444 { ATA_NFORCE4, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce4" },
1445 { ATA_NFORCE4_S1, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce4" },
1446 { ATA_NFORCE4_S2, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce4" },
1447 { 0, 0, 0, 0, 0, 0}};
1450 if (!(idx = ata_match_chip(dev, ids)))
1453 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1454 device_set_desc_copy(dev, buffer);
1456 ctlr->chipinit = ata_nvidia_chipinit;
1461 ata_nvidia_chipinit(device_t dev)
1463 struct ata_pci_controller *ctlr = device_get_softc(dev);
1465 if (ata_setup_interrupt(dev))
1468 if (ctlr->chip->max_dma >= ATA_SA150) {
1469 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
1470 ctlr->r_type2 = SYS_RES_IOPORT;
1472 ctlr->r_type2 = SYS_RES_MEMORY;
1473 ctlr->r_rid2 = PCIR_BAR(5);
1474 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1475 &ctlr->r_rid2, RF_ACTIVE))) {
1476 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
1477 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1478 ata_nvidia_intr, ctlr, &ctlr->handle)) {
1479 device_printf(dev, "unable to setup interrupt\n");
1482 pci_write_config(dev, PCIR_COMMAND,
1483 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
1484 ctlr->allocate = ata_nvidia_allocate;
1485 ctlr->reset = ata_nvidia_reset;
1487 ctlr->setmode = ata_sata_setmode;
1490 /* disable prefetch, postwrite */
1491 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
1492 ctlr->setmode = ata_via_family_setmode;
1498 ata_nvidia_allocate(device_t dev)
1500 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1501 struct ata_channel *ch = device_get_softc(dev);
1502 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0441 : 0x0011;
1504 /* setup the usual register normal pci style */
1505 ata_pci_allocate(dev);
1507 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
1508 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
1509 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
1510 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
1511 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
1512 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
1513 ch->flags |= ATA_NO_SLAVE;
1515 /* enable PHY state change interrupts */
1516 ATA_OUTB(ctlr->r_res2, offset,
1517 ATA_INB(ctlr->r_res2, offset) | (0x0c << (ch->unit << 2)));
1522 ata_nvidia_intr(void *data)
1524 struct ata_pci_controller *ctlr = data;
1525 struct ata_channel *ch;
1526 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
1530 /* implement this as a toggle instead to balance load XXX */
1531 for (unit = 0; unit < ctlr->channels; unit++) {
1532 if (!(ch = ctlr->interrupt[unit].argument))
1535 if ((status = ATA_INB(ctlr->r_res2, offset))) {
1536 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
1537 struct ata_connect_task *tp;
1539 /* clear error bits/interrupt */
1540 ATA_IDX_OUTL(ch, ATA_SERROR, error);
1541 ATA_OUTB(ctlr->r_res2, offset, status);
1543 /* check for and handle connect events */
1544 if ((status & (0x04 << (ch->unit << 2))) &&
1545 (tp = (struct ata_connect_task *)
1546 malloc(sizeof(struct ata_connect_task),
1547 M_ATA, M_NOWAIT | M_ZERO))) {
1549 device_printf(ch->dev, "CONNECT requested\n");
1550 tp->action = ATA_C_ATTACH;
1552 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1553 taskqueue_enqueue(taskqueue_thread, &tp->task);
1557 /* check for and handle disconnect events */
1558 if ((status & (0x08 << (ch->unit << 2))) &&
1559 (tp = (struct ata_connect_task *)
1560 malloc(sizeof(struct ata_connect_task),
1561 M_ATA, M_NOWAIT | M_ZERO))) {
1563 device_printf(ch->dev, "DISCONNECT requested\n");
1564 tp->action = ATA_C_DETACH;
1566 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1567 taskqueue_enqueue(taskqueue_thread, &tp->task);
1570 /* any drive action to take care of ? */
1571 if (status & (0x01 << (ch->unit << 2))) {
1572 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
1574 ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1576 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
1578 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat&~ATA_BMSTAT_ERROR);
1581 ctlr->interrupt[unit].function(ch);
1588 ata_nvidia_reset(device_t dev)
1590 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1591 struct ata_channel *ch = device_get_softc(dev);
1592 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0441 : 0x0011;
1594 /* disable PHY state change interrupt */
1595 ATA_OUTB(ctlr->r_res2, offset,
1596 ATA_INB(ctlr->r_res2, offset) & (~0x0c << (ch->unit << 2)));
1598 ata_sata_phy_enable(ch);
1600 /* enable PHY state change interrupt */
1601 ATA_OUTB(ctlr->r_res2, offset,
1602 ATA_INB(ctlr->r_res2, offset) | (0x0c << (ch->unit << 2)));
1607 * Promise chipset support functions
1609 #define ATA_PDC_APKT_OFFSET 0x00000010
1610 #define ATA_PDC_HPKT_OFFSET 0x00000040
1611 #define ATA_PDC_ASG_OFFSET 0x00000080
1612 #define ATA_PDC_LSG_OFFSET 0x000000c0
1613 #define ATA_PDC_HSG_OFFSET 0x00000100
1614 #define ATA_PDC_CHN_OFFSET 0x00000400
1615 #define ATA_PDC_BUF_BASE 0x00400000
1616 #define ATA_PDC_BUF_OFFSET 0x00100000
1617 #define ATA_PDC_MAX_HPKT 8
1618 #define ATA_PDC_WRITE_REG 0x00
1619 #define ATA_PDC_WRITE_CTL 0x0e
1620 #define ATA_PDC_WRITE_END 0x08
1621 #define ATA_PDC_WAIT_NBUSY 0x10
1622 #define ATA_PDC_WAIT_READY 0x18
1623 #define ATA_PDC_1B 0x20
1624 #define ATA_PDC_2B 0x40
1626 struct host_packet {
1628 TAILQ_ENTRY(host_packet) chain;
1631 struct ata_promise_sx4 {
1634 u_int32_t array[ATA_PDC_MAX_HPKT];
1637 TAILQ_HEAD(, host_packet) queue;
1643 ata_promise_ident(device_t dev)
1645 struct ata_pci_controller *ctlr = device_get_softc(dev);
1646 struct ata_chip_id *idx;
1647 static struct ata_chip_id ids[] =
1648 {{ ATA_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "Promise PDC20246" },
1649 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20262" },
1650 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20263" },
1651 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20265" },
1652 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20267" },
1653 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20268" },
1654 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20269" },
1655 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20270" },
1656 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20271" },
1657 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20275" },
1658 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "Promise PDC20276" },
1659 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20277" },
1660 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20318" },
1661 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20319" },
1662 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20371" },
1663 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20375" },
1664 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20376" },
1665 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20377" },
1666 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20378" },
1667 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20379" },
1668 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20571" },
1669 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20575" },
1670 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20579" },
1671 { ATA_PDC20580, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20580" },
1672 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20617" },
1673 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20618" },
1674 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20619" },
1675 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20620" },
1676 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "Promise PDC20621" },
1677 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "Promise PDC20622" },
1678 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40518" },
1679 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40519" },
1680 { 0, 0, 0, 0, 0, 0}};
1682 uintptr_t devid = 0;
1684 if (!(idx = ata_match_chip(dev, ids)))
1687 /* if we are on a SuperTrak SX6000 dont attach */
1688 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
1689 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
1690 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
1691 devid == ATA_I960RM)
1694 strcpy(buffer, idx->text);
1696 /* if we are on a FastTrak TX4, adjust the interrupt resource */
1697 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
1698 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
1699 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
1700 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
1701 static long start = 0, end = 0;
1703 if (pci_get_slot(dev) == 1) {
1704 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
1705 strcat(buffer, " (channel 0+1)");
1707 else if (pci_get_slot(dev) == 2 && start && end) {
1708 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
1710 strcat(buffer, " (channel 2+3)");
1716 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
1717 device_set_desc_copy(dev, buffer);
1719 ctlr->chipinit = ata_promise_chipinit;
1724 ata_promise_chipinit(device_t dev)
1726 struct ata_pci_controller *ctlr = device_get_softc(dev);
1727 int rid = ATA_IRQ_RID;
1729 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1730 RF_SHAREABLE | RF_ACTIVE))) {
1731 device_printf(dev, "unable to map interrupt\n");
1735 if (ctlr->chip->max_dma >= ATA_SA150)
1736 ctlr->setmode = ata_sata_setmode;
1738 ctlr->setmode = ata_promise_setmode;
1740 switch (ctlr->chip->cfg1) {
1743 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
1745 ctlr->dmainit = ata_promise_new_dmainit;
1749 /* enable burst mode */
1750 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
1752 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1753 ata_promise_old_intr, ctlr, &ctlr->handle))) {
1754 device_printf(dev, "unable to setup interrupt\n");
1760 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1761 ata_promise_tx2_intr, ctlr, &ctlr->handle))) {
1762 device_printf(dev, "unable to setup interrupt\n");
1768 // if (ctlr->r_res1)
1769 // bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
1770 ctlr->r_type1 = SYS_RES_MEMORY;
1771 ctlr->r_rid1 = PCIR_BAR(4);
1772 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
1773 &ctlr->r_rid1, RF_ACTIVE)))
1776 ctlr->r_type2 = SYS_RES_MEMORY;
1777 ctlr->r_rid2 = PCIR_BAR(3);
1778 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1779 &ctlr->r_rid2, RF_ACTIVE))){
1780 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
1783 ctlr->reset = ata_promise_mio_reset;
1784 ctlr->dmainit = ata_promise_mio_dmainit;
1785 ctlr->allocate = ata_promise_mio_allocate;
1787 if (ctlr->chip->cfg2 == PRSX4X) {
1788 struct ata_promise_sx4 *hpkt;
1789 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
1791 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1792 ata_promise_sx4_intr, ctlr, &ctlr->handle))) {
1793 device_printf(dev, "unable to setup interrupt\n");
1794 /* XXX SOS release resources */
1798 /* print info about cache memory */
1799 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
1800 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
1801 ((dimm >> 24) & 0xff),
1802 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
1803 " ECC enabled" : "" );
1805 /* adjust cache memory parameters */
1806 ATA_OUTL(ctlr->r_res2, 0x000c000c,
1807 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
1809 /* setup host packet controls */
1810 hpkt = malloc(sizeof(struct ata_promise_sx4),
1811 M_TEMP, M_NOWAIT | M_ZERO);
1812 mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
1813 TAILQ_INIT(&hpkt->queue);
1814 hpkt->busy = 0; //hpkt->head = hpkt->tail = 0;
1815 device_set_ivars(dev, hpkt);
1820 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1821 ata_promise_mio_intr, ctlr, &ctlr->handle))) {
1822 device_printf(dev, "unable to setup interrupt\n");
1823 /* XXX SOS release resources */
1827 switch (ctlr->chip->cfg2) {
1829 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
1830 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
1834 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
1835 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 3;
1839 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
1844 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
1849 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
1854 /* XXX SOS release resources */
1858 /* XXX SOS release resources */
1865 ata_promise_mio_allocate(device_t dev)
1867 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1868 struct ata_channel *ch = device_get_softc(dev);
1869 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
1872 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
1873 ch->r_io[i].res = ctlr->r_res2;
1874 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
1876 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
1877 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
1878 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
1879 ata_default_registers(dev);
1880 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
1881 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
1882 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
1883 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
1884 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
1885 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
1886 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
1887 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
1888 ch->flags |= ATA_NO_SLAVE;
1890 ch->flags |= ATA_USE_16BIT;
1892 ata_generic_hw(dev);
1894 ch->hw.command = ata_promise_sx4_command;
1896 ch->hw.command = ata_promise_mio_command;
1901 ata_promise_mio_intr(void *data)
1903 struct ata_pci_controller *ctlr = data;
1904 struct ata_channel *ch;
1905 u_int32_t vector = 0, status = 0;
1908 switch (ctlr->chip->cfg2) {
1911 /* read and acknowledge interrupt(s) */
1912 vector = ATA_INL(ctlr->r_res2, 0x040);
1914 /* read and clear interface status */
1915 status = ATA_INL(ctlr->r_res2, 0x06c);
1916 ATA_OUTL(ctlr->r_res2, 0x06c, status & 0x000000ff);
1921 /* read and acknowledge interrupt(s) */
1922 vector = ATA_INL(ctlr->r_res2, 0x040);
1923 ATA_OUTL(ctlr->r_res2, 0x040, vector & 0x0000ffff);
1925 /* read and clear interface status */
1926 status = ATA_INL(ctlr->r_res2, 0x060);
1927 ATA_OUTL(ctlr->r_res2, 0x060, status & 0x000000ff);
1931 for (unit = 0; unit < ctlr->channels; unit++) {
1933 if ((ch = ctlr->interrupt[unit].argument)) {
1934 struct ata_connect_task *tp;
1936 /* check for and handle disconnect events */
1937 if ((status & (0x00000001 << unit)) &&
1938 (tp = (struct ata_connect_task *)
1939 malloc(sizeof(struct ata_connect_task),
1940 M_ATA, M_NOWAIT | M_ZERO))) {
1943 device_printf(ch->dev, "DISCONNECT requested\n");
1944 tp->action = ATA_C_DETACH;
1946 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1947 taskqueue_enqueue(taskqueue_thread, &tp->task);
1950 /* check for and handle connect events */
1951 if ((status & (0x00000010 << unit)) &&
1952 (tp = (struct ata_connect_task *)
1953 malloc(sizeof(struct ata_connect_task),
1954 M_ATA, M_NOWAIT | M_ZERO))) {
1957 device_printf(ch->dev, "CONNECT requested\n");
1958 tp->action = ATA_C_ATTACH;
1960 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1961 taskqueue_enqueue(taskqueue_thread, &tp->task);
1964 /* active interrupt(s) need to call the interrupt handler */
1965 if (vector & (1 << (unit + 1)))
1966 if ((ch = ctlr->interrupt[unit].argument))
1967 ctlr->interrupt[unit].function(ch);
1973 ata_promise_sx4_intr(void *data)
1975 struct ata_pci_controller *ctlr = data;
1976 struct ata_channel *ch;
1977 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
1980 for (unit = 0; unit < ctlr->channels; unit++) {
1981 if (vector & (1 << (unit + 1)))
1982 if ((ch = ctlr->interrupt[unit].argument))
1983 ctlr->interrupt[unit].function(ch);
1984 if (vector & (1 << (unit + 5)))
1985 if ((ch = ctlr->interrupt[unit].argument))
1986 ata_promise_queue_hpkt(ctlr,
1987 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
1988 ATA_PDC_HPKT_OFFSET));
1989 if (vector & (1 << (unit + 9))) {
1990 ata_promise_next_hpkt(ctlr);
1991 if ((ch = ctlr->interrupt[unit].argument))
1992 ctlr->interrupt[unit].function(ch);
1994 if (vector & (1 << (unit + 13))) {
1995 ata_promise_next_hpkt(ctlr);
1996 if ((ch = ctlr->interrupt[unit].argument))
1997 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
1998 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
1999 ATA_PDC_APKT_OFFSET));
2005 ata_promise_mio_dmastart(device_t dev)
2007 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2009 ch->flags |= ATA_DMA_ACTIVE;
2014 ata_promise_mio_dmastop(device_t dev)
2016 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2018 ch->flags &= ~ATA_DMA_ACTIVE;
2019 /* get status XXX SOS */
2024 ata_promise_mio_dmainit(device_t dev)
2026 struct ata_channel *ch = device_get_softc(dev);
2030 ch->dma->start = ata_promise_mio_dmastart;
2031 ch->dma->stop = ata_promise_mio_dmastop;
2036 ata_promise_mio_reset(device_t dev)
2038 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2039 struct ata_channel *ch = device_get_softc(dev);
2040 struct ata_promise_sx4 *hpktp;
2042 switch (ctlr->chip->cfg2) {
2045 /* softreset channel ATA module */
2046 hpktp = device_get_ivars(ctlr->dev);
2047 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
2049 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
2050 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
2051 ~0x00003f9f) | (ch->unit + 1));
2053 /* softreset HOST module XXX SOS what about other outstandings */
2054 mtx_lock(&hpktp->mtx);
2055 ATA_OUTL(ctlr->r_res2, 0xc012c,
2056 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
2058 ATA_OUTL(ctlr->r_res2, 0xc012c,
2059 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
2061 mtx_unlock(&hpktp->mtx);
2062 ata_generic_reset(dev);
2067 if ((ctlr->chip->cfg2 == PRSATA) ||
2068 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2070 /* mask plug/unplug intr */
2071 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
2074 /* softreset channels ATA module */
2075 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2077 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2078 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2079 ~0x00003f9f) | (ch->unit + 1));
2081 if ((ctlr->chip->cfg2 == PRSATA) ||
2082 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2084 ata_sata_phy_enable(ch);
2086 /* reset and enable plug/unplug intr */
2087 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
2090 ata_generic_reset(dev);
2095 if ((ctlr->chip->cfg2 == PRSATA2) ||
2096 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2097 /* set portmultiplier port */
2098 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
2100 /* mask plug/unplug intr */
2101 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
2104 /* softreset channels ATA module */
2105 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2107 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2108 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2109 ~0x00003f9f) | (ch->unit + 1));
2111 if ((ctlr->chip->cfg2 == PRSATA2) ||
2112 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2114 /* set PHY mode to "improved" */
2115 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
2116 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
2117 ~0x00000003) | 0x00000001);
2119 ata_sata_phy_enable(ch);
2121 /* reset and enable plug/unplug intr */
2122 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
2124 /* set portmultiplier port */
2125 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
2128 ata_generic_reset(dev);
2135 ata_promise_mio_command(device_t dev, u_int8_t command,
2136 u_int64_t lba, u_int16_t count, u_int16_t feature)
2138 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2139 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2140 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
2142 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
2146 return ata_generic_command(dev, command, lba, count, feature);
2149 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
2153 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
2156 wordp[1] = htole32(ch->dma->sg_bus);
2158 ata_promise_apkt((u_int8_t*)wordp, dev, command, lba, count, feature);
2160 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
2165 ata_promise_sx4_command(device_t dev, u_int8_t command,
2166 u_int64_t lba, u_int16_t count, u_int16_t feature)
2168 device_t gparent = GRANDPARENT(dev);
2169 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2170 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2171 struct ata_dma_prdentry *prd = ch->dma->sg;
2172 caddr_t window = rman_get_virtual(ctlr->r_res1);
2174 int i, idx, length = 0;
2181 case ATA_ATA_IDENTIFY:
2186 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2187 return ata_generic_command(dev, command, lba, count, feature);
2189 case ATA_SETFEATURES:
2190 case ATA_FLUSHCACHE:
2193 wordp = (u_int32_t *)
2194 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2195 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
2198 ata_promise_apkt((u_int8_t *)wordp, dev, command, lba,count,feature);
2199 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2200 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2201 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2202 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
2207 wordp = (u_int32_t *)
2208 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
2211 wordp[idx++] = htole32(prd[i].addr);
2212 wordp[idx++] = htole32(prd[i].count & ~ATA_DMA_EOT);
2213 length += (prd[i].count & ~ATA_DMA_EOT);
2214 } while (!(prd[i++].count & ATA_DMA_EOT));
2215 wordp[idx - 1] |= htole32(ATA_DMA_EOT);
2217 wordp = (u_int32_t *)
2218 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
2219 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2220 wordp[1] = htole32((count * DEV_BSIZE) | ATA_DMA_EOT);
2222 wordp = (u_int32_t *)
2223 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
2224 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2225 wordp[1] = htole32((count * DEV_BSIZE) | ATA_DMA_EOT);
2227 wordp = (u_int32_t *)
2228 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
2229 if (command == ATA_READ_DMA)
2230 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
2231 if (command == ATA_WRITE_DMA)
2232 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
2233 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
2234 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
2237 wordp = (u_int32_t *)
2238 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2239 if (command == ATA_READ_DMA)
2240 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
2241 if (command == ATA_WRITE_DMA)
2242 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
2243 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
2245 ata_promise_apkt((u_int8_t *)wordp, dev, command, lba,count,feature);
2246 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2248 if (command == ATA_READ_DMA) {
2249 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
2250 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
2251 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2252 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
2254 if (command == ATA_WRITE_DMA) {
2255 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
2256 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
2257 ata_promise_queue_hpkt(ctlr,
2258 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
2265 ata_promise_apkt(u_int8_t *bytep, device_t dev, u_int8_t command,
2266 u_int64_t lba, u_int16_t count, u_int16_t feature)
2268 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2269 struct ata_device *atadev = device_get_softc(dev);
2272 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
2273 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
2274 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
2275 bytep[i++] = ATA_A_4BIT;
2277 if ((lba >= ATA_MAX_28BIT_LBA || count > 256) &&
2278 (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48)) {
2279 ch->flags |= ATA_48BIT_ACTIVE;
2280 if (command == ATA_READ_DMA)
2281 command = ATA_READ_DMA48;
2282 if (command == ATA_WRITE_DMA)
2283 command = ATA_WRITE_DMA48;
2284 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
2285 bytep[i++] = (feature >> 8) & 0xff;
2286 bytep[i++] = feature & 0xff;
2287 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
2288 bytep[i++] = (count >> 8) & 0xff;
2289 bytep[i++] = count & 0xff;
2290 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
2291 bytep[i++] = (lba >> 24) & 0xff;
2292 bytep[i++] = lba & 0xff;
2293 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
2294 bytep[i++] = (lba >> 32) & 0xff;
2295 bytep[i++] = (lba >> 8) & 0xff;
2296 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
2297 bytep[i++] = (lba >> 40) & 0xff;
2298 bytep[i++] = (lba >> 16) & 0xff;
2299 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
2300 bytep[i++] = ATA_D_LBA | atadev->unit;
2303 ch->flags &= ~ATA_48BIT_ACTIVE;
2304 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
2305 bytep[i++] = feature;
2306 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
2308 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
2309 bytep[i++] = lba & 0xff;
2310 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
2311 bytep[i++] = (lba >> 8) & 0xff;
2312 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
2313 bytep[i++] = (lba >> 16) & 0xff;
2314 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
2315 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
2316 ATA_D_IBM | atadev->unit | ((lba >> 24) & 0xf);
2318 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
2319 bytep[i++] = command;
2324 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
2326 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
2328 mtx_lock(&hpktp->mtx);
2330 if (hpktp->tail == hpktp->head && !hpktp->busy) {
2331 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
2335 hpktp->array[(hpktp->head++) & (ATA_PDC_MAX_HPKT - 1)] = hpkt;
2338 struct host_packet *hp =
2339 malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
2341 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
2345 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
2348 mtx_unlock(&hpktp->mtx);
2352 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
2354 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
2355 struct host_packet *hp;
2357 mtx_lock(&hpktp->mtx);
2359 if (hpktp->tail != hpktp->head) {
2360 ATA_OUTL(ctlr->r_res2, 0x000c0100,
2361 hpktp->array[(hpktp->tail++) & (ATA_PDC_MAX_HPKT - 1)]);
2364 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
2365 TAILQ_REMOVE(&hpktp->queue, hp, chain);
2366 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
2372 mtx_unlock(&hpktp->mtx);
2376 ata_promise_tx2_intr(void *data)
2378 struct ata_pci_controller *ctlr = data;
2379 struct ata_channel *ch;
2382 /* implement this as a toggle instead to balance load XXX */
2383 for (unit = 0; unit < ctlr->channels; unit++) {
2384 if (!(ch = ctlr->interrupt[unit].argument))
2386 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
2387 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
2388 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2389 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2391 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2392 ATA_BMSTAT_INTERRUPT)
2394 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2397 ctlr->interrupt[unit].function(ch);
2403 ata_promise_old_intr(void *data)
2405 struct ata_pci_controller *ctlr = data;
2406 struct ata_channel *ch;
2409 /* implement this as a toggle instead to balance load XXX */
2410 for (unit = 0; unit < ctlr->channels; unit++) {
2411 if (!(ch = ctlr->interrupt[unit].argument))
2413 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)){
2414 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2415 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2417 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2418 ATA_BMSTAT_INTERRUPT)
2420 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2423 ctlr->interrupt[unit].function(ch);
2429 ata_promise_new_dmastart(device_t dev)
2431 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2432 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2434 if (ch->flags & ATA_48BIT_ACTIVE) {
2435 ATA_OUTB(ctlr->r_res1, 0x11,
2436 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
2437 ATA_OUTL(ctlr->r_res1, 0x20,
2438 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
2439 (ch->dma->cur_iosize >> 1));
2441 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
2442 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
2443 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
2444 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2445 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
2446 ATA_BMCMD_START_STOP);
2447 ch->flags |= ATA_DMA_ACTIVE;
2452 ata_promise_new_dmastop(device_t dev)
2454 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2455 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2458 if (ch->flags & ATA_48BIT_ACTIVE) {
2459 ATA_OUTB(ctlr->r_res1, 0x11,
2460 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
2461 ATA_OUTL(ctlr->r_res1, 0x20, 0);
2463 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
2464 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2465 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
2466 ch->flags &= ~ATA_DMA_ACTIVE;
2467 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
2472 ata_promise_new_dmainit(device_t dev)
2474 struct ata_channel *ch = device_get_softc(dev);
2478 ch->dma->start = ata_promise_new_dmastart;
2479 ch->dma->stop = ata_promise_new_dmastop;
2484 ata_promise_setmode(device_t dev, int mode)
2486 device_t gparent = GRANDPARENT(dev);
2487 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2488 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2489 struct ata_device *atadev = device_get_softc(dev);
2490 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2492 u_int32_t timings33[][2] = {
2493 /* PROLD PRNEW mode */
2494 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
2495 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
2496 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
2497 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
2498 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
2499 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
2500 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
2501 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
2502 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
2503 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
2504 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
2505 { 0, 0x00424ef6 }, /* UDMA 3 */
2506 { 0, 0x004127f3 }, /* UDMA 4 */
2507 { 0, 0x004127f3 } /* UDMA 5 */
2510 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2512 switch (ctlr->chip->cfg1) {
2515 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
2516 (ch->unit ? 1 << 11 : 1 << 10))) {
2517 ata_print_cable(dev, "controller");
2520 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
2521 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
2525 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
2526 if (mode > ATA_UDMA2 &&
2527 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
2528 ata_print_cable(dev, "controller");
2534 if (mode > ATA_UDMA2 &&
2535 (ATA_INL(ctlr->r_res2,
2536 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
2537 (ch->unit << 7)) & 0x01000000)) {
2538 ata_print_cable(dev, "controller");
2544 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2547 device_printf(dev, "%ssetting %s on %s chip\n",
2548 (error) ? "FAILURE " : "",
2549 ata_mode2str(mode), ctlr->chip->text);
2551 if (ctlr->chip->cfg1 < PRTX)
2552 pci_write_config(gparent, 0x60 + (devno << 2),
2553 timings33[ctlr->chip->cfg1][ata_mode2idx(mode)],4);
2554 atadev->mode = mode;
2561 * ServerWorks chipset support functions
2564 ata_serverworks_ident(device_t dev)
2566 struct ata_pci_controller *ctlr = device_get_softc(dev);
2567 struct ata_chip_id *idx;
2568 static struct ata_chip_id ids[] =
2569 {{ ATA_ROSB4, 0x00, SWKS33, 0x00, ATA_UDMA2, "ServerWorks ROSB4" },
2570 { ATA_CSB5, 0x92, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB5" },
2571 { ATA_CSB5, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB5" },
2572 { ATA_CSB6, 0x00, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB6" },
2573 { ATA_CSB6_1, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB6" },
2574 { 0, 0, 0, 0, 0, 0}};
2577 if (!(idx = ata_match_chip(dev, ids)))
2580 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
2581 device_set_desc_copy(dev, buffer);
2583 ctlr->chipinit = ata_serverworks_chipinit;
2588 ata_serverworks_chipinit(device_t dev)
2590 struct ata_pci_controller *ctlr = device_get_softc(dev);
2592 if (ata_setup_interrupt(dev))
2595 if (ctlr->chip->cfg1 == SWKS33) {
2599 /* locate the ISA part in the southbridge and enable UDMA33 */
2600 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
2601 for (i = 0; i < nchildren; i++) {
2602 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
2603 pci_write_config(children[i], 0x64,
2604 (pci_read_config(children[i], 0x64, 4) &
2605 ~0x00002000) | 0x00004000, 4);
2609 free(children, M_TEMP);
2613 pci_write_config(dev, 0x5a,
2614 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
2615 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
2617 ctlr->setmode = ata_serverworks_setmode;
2622 ata_serverworks_setmode(device_t dev, int mode)
2624 device_t gparent = GRANDPARENT(dev);
2625 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2626 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2627 struct ata_device *atadev = device_get_softc(dev);
2628 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2629 int offset = (devno ^ 0x01) << 3;
2631 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
2632 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
2633 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
2635 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2637 mode = ata_check_80pin(dev, mode);
2639 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2642 device_printf(dev, "%ssetting %s on %s chip\n",
2643 (error) ? "FAILURE " : "",
2644 ata_mode2str(mode), ctlr->chip->text);
2646 if (mode >= ATA_UDMA0) {
2647 pci_write_config(gparent, 0x56,
2648 (pci_read_config(gparent, 0x56, 2) &
2649 ~(0xf << (devno << 2))) |
2650 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
2651 pci_write_config(gparent, 0x54,
2652 pci_read_config(gparent, 0x54, 1) |
2653 (0x01 << devno), 1);
2654 pci_write_config(gparent, 0x44,
2655 (pci_read_config(gparent, 0x44, 4) &
2656 ~(0xff << offset)) |
2657 (dmatimings[2] << offset), 4);
2659 else if (mode >= ATA_WDMA0) {
2660 pci_write_config(gparent, 0x54,
2661 pci_read_config(gparent, 0x54, 1) &
2662 ~(0x01 << devno), 1);
2663 pci_write_config(gparent, 0x44,
2664 (pci_read_config(gparent, 0x44, 4) &
2665 ~(0xff << offset)) |
2666 (dmatimings[mode & ATA_MODE_MASK] << offset),4);
2669 pci_write_config(gparent, 0x54,
2670 pci_read_config(gparent, 0x54, 1) &
2671 ~(0x01 << devno), 1);
2673 pci_write_config(gparent, 0x40,
2674 (pci_read_config(gparent, 0x40, 4) &
2675 ~(0xff << offset)) |
2676 (piotimings[ata_mode2idx(mode)] << offset), 4);
2677 atadev->mode = mode;
2683 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
2686 ata_sii_ident(device_t dev)
2688 struct ata_pci_controller *ctlr = device_get_softc(dev);
2689 struct ata_chip_id *idx;
2690 static struct ata_chip_id ids[] =
2691 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
2692 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
2693 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
2694 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
2695 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
2696 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
2697 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
2698 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
2699 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
2700 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
2701 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
2702 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
2703 { 0, 0, 0, 0, 0, 0}};
2706 if (!(idx = ata_match_chip(dev, ids)))
2709 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
2710 device_set_desc_copy(dev, buffer);
2712 ctlr->chipinit = ata_sii_chipinit;
2717 ata_sii_chipinit(device_t dev)
2719 struct ata_pci_controller *ctlr = device_get_softc(dev);
2720 int rid = ATA_IRQ_RID;
2722 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2723 RF_SHAREABLE | RF_ACTIVE))) {
2724 device_printf(dev, "unable to map interrupt\n");
2728 if (ctlr->chip->cfg1 == SIIMEMIO) {
2729 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2730 ata_sii_intr, ctlr, &ctlr->handle))) {
2731 device_printf(dev, "unable to setup interrupt\n");
2735 ctlr->r_type2 = SYS_RES_MEMORY;
2736 ctlr->r_rid2 = PCIR_BAR(5);
2737 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2738 &ctlr->r_rid2, RF_ACTIVE)))
2741 if (ctlr->chip->cfg2 & SIISETCLK) {
2742 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
2743 pci_write_config(dev, 0x8a,
2744 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
2745 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
2746 device_printf(dev, "%s could not set ATA133 clock\n",
2750 /* if we have 4 channels enable the second set */
2751 if (ctlr->chip->cfg2 & SII4CH) {
2752 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
2756 /* enable PCI interrupt as BIOS might not */
2757 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
2759 /* dont block interrupts from any channel */
2760 pci_write_config(dev, 0x48,
2761 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
2763 ctlr->allocate = ata_sii_allocate;
2764 if (ctlr->chip->max_dma >= ATA_SA150) {
2765 ctlr->reset = ata_sii_reset;
2766 ctlr->setmode = ata_sata_setmode;
2769 ctlr->setmode = ata_sii_setmode;
2772 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2773 ctlr->chip->cfg2 & SIIINTR ?
2774 ata_cmd_intr : ata_cmd_old_intr,
2775 ctlr, &ctlr->handle))) {
2776 device_printf(dev, "unable to setup interrupt\n");
2780 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
2781 device_printf(dev, "HW has secondary channel disabled\n");
2785 /* enable interrupt as BIOS might not */
2786 pci_write_config(dev, 0x71, 0x01, 1);
2788 ctlr->setmode = ata_cmd_setmode;
2794 ata_sii_allocate(device_t dev)
2796 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2797 struct ata_channel *ch = device_get_softc(dev);
2798 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
2801 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2802 ch->r_io[i].res = ctlr->r_res2;
2803 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
2805 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
2806 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
2807 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
2808 ata_default_registers(dev);
2809 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
2810 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
2811 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
2812 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
2813 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
2814 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
2815 ch->r_io[ATA_BMDEVSPEC_0].res = ctlr->r_res2;
2816 ch->r_io[ATA_BMDEVSPEC_0].offset = 0xa1 + (unit01 << 6) + (unit10 << 8);
2818 if (ctlr->chip->max_dma >= ATA_SA150) {
2819 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2820 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
2821 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2822 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
2823 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2824 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
2825 ch->flags |= ATA_NO_SLAVE;
2827 /* enable PHY state change interrupt */
2828 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
2831 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
2832 /* work around errata in early chips */
2833 ch->dma->boundary = 16 * DEV_BSIZE;
2834 ch->dma->max_iosize = 15 * DEV_BSIZE;
2837 ata_generic_hw(dev);
2842 ata_sii_intr(void *data)
2844 struct ata_pci_controller *ctlr = data;
2845 struct ata_channel *ch;
2848 /* implement this as a toggle instead to balance load XXX */
2849 for (unit = 0; unit < ctlr->channels; unit++) {
2850 if (!(ch = ctlr->interrupt[unit].argument))
2853 /* check for PHY related interrupts on SATA capable HW */
2854 if (ctlr->chip->max_dma >= ATA_SA150) {
2855 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
2856 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
2857 struct ata_connect_task *tp;
2860 /* clear error bits/interrupt */
2861 ATA_IDX_OUTL(ch, ATA_SERROR, error);
2863 /* if we have a connection event deal with it */
2864 if ((error & ATA_SE_PHY_CHANGED) &&
2865 (tp = (struct ata_connect_task *)
2866 malloc(sizeof(struct ata_connect_task),
2867 M_ATA, M_NOWAIT | M_ZERO))) {
2869 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
2870 device_printf(ch->dev, "CONNECT requested\n");
2871 tp->action = ATA_C_ATTACH;
2874 device_printf(ch->dev, "DISCONNECT requested\n");
2875 tp->action = ATA_C_DETACH;
2878 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2879 taskqueue_enqueue(taskqueue_thread, &tp->task);
2884 /* any drive action to take care of ? */
2885 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_0) & 0x08) {
2886 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2887 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2889 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
2891 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2894 ctlr->interrupt[unit].function(ch);
2901 ata_cmd_intr(void *data)
2903 struct ata_pci_controller *ctlr = data;
2904 struct ata_channel *ch;
2908 /* implement this as a toggle instead to balance load XXX */
2909 for (unit = 0; unit < ctlr->channels; unit++) {
2910 if (!(ch = ctlr->interrupt[unit].argument))
2912 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
2913 (ch->unit ? 0x08 : 0x04))) {
2914 pci_write_config(device_get_parent(ch->dev), 0x71,
2915 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
2916 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2917 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2919 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2920 ATA_BMSTAT_INTERRUPT)
2922 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2925 ctlr->interrupt[unit].function(ch);
2931 ata_cmd_old_intr(void *data)
2933 struct ata_pci_controller *ctlr = data;
2934 struct ata_channel *ch;
2937 /* implement this as a toggle instead to balance load XXX */
2938 for (unit = 0; unit < ctlr->channels; unit++) {
2939 if (!(ch = ctlr->interrupt[unit].argument))
2941 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2942 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2944 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2945 ATA_BMSTAT_INTERRUPT)
2947 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2950 ctlr->interrupt[unit].function(ch);
2955 ata_sii_reset(device_t dev)
2957 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2958 struct ata_channel *ch = device_get_softc(dev);
2959 int offset = ((ch->unit & 1) << 7) + ((ch->unit & 2) << 8);
2961 /* disable PHY state change interrupt */
2962 ATA_OUTL(ctlr->r_res2, 0x148 + offset, ~(1 << 16));
2964 ata_sata_phy_enable(ch);
2966 /* enable PHY state change interrupt */
2967 ATA_OUTL(ctlr->r_res2, 0x148 + offset, (1 << 16));
2971 ata_sii_setmode(device_t dev, int mode)
2973 device_t gparent = GRANDPARENT(dev);
2974 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2975 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2976 struct ata_device *atadev = device_get_softc(dev);
2977 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
2978 int mreg = ch->unit ? 0x84 : 0x80;
2979 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
2980 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
2983 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2985 if (ctlr->chip->cfg2 & SIISETCLK) {
2986 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
2987 (ch->unit ? 0x02 : 0x01))) {
2988 ata_print_cable(dev, "controller");
2993 mode = ata_check_80pin(dev, mode);
2995 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2998 device_printf(dev, "%ssetting %s on %s chip\n",
2999 (error) ? "FAILURE " : "",
3000 ata_mode2str(mode), ctlr->chip->text);
3004 if (mode >= ATA_UDMA0) {
3005 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
3006 u_int8_t ureg = 0xac + rego;
3008 pci_write_config(gparent, mreg,
3009 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
3010 pci_write_config(gparent, ureg,
3011 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
3012 udmatimings[mode & ATA_MODE_MASK], 1);
3015 else if (mode >= ATA_WDMA0) {
3016 u_int8_t dreg = 0xa8 + rego;
3017 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
3019 pci_write_config(gparent, mreg,
3020 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
3021 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
3025 u_int8_t preg = 0xa4 + rego;
3026 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
3028 pci_write_config(gparent, mreg,
3029 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
3030 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
3032 atadev->mode = mode;
3036 ata_cmd_setmode(device_t dev, int mode)
3038 device_t gparent = GRANDPARENT(dev);
3039 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3040 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3041 struct ata_device *atadev = device_get_softc(dev);
3042 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3045 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3047 mode = ata_check_80pin(dev, mode);
3049 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3052 device_printf(dev, "%ssetting %s on %s chip\n",
3053 (error) ? "FAILURE " : "",
3054 ata_mode2str(mode), ctlr->chip->text);
3056 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
3057 int ureg = ch->unit ? 0x7b : 0x73;
3059 if (mode >= ATA_UDMA0) {
3060 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
3061 { 0x11, 0x42 }, { 0x25, 0x8a },
3062 { 0x15, 0x4a }, { 0x05, 0x0a } };
3064 u_int8_t umode = pci_read_config(gparent, ureg, 1);
3066 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
3067 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
3068 pci_write_config(gparent, ureg, umode, 1);
3070 else if (mode >= ATA_WDMA0) {
3071 int dmatimings[] = { 0x87, 0x32, 0x3f };
3073 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
3074 pci_write_config(gparent, ureg,
3075 pci_read_config(gparent, ureg, 1) &
3076 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3079 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
3080 pci_write_config(gparent, treg,
3081 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
3082 pci_write_config(gparent, ureg,
3083 pci_read_config(gparent, ureg, 1) &
3084 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3086 atadev->mode = mode;
3092 * Silicon Integrated Systems Corp. (SiS) chipset support functions
3095 ata_sis_ident(device_t dev)
3097 struct ata_pci_controller *ctlr = device_get_softc(dev);
3098 struct ata_chip_id *idx;
3099 static struct ata_chip_id ids[] =
3100 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "SiS 182" }, /* south */
3101 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "SiS 181" }, /* south */
3102 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "SiS 180" }, /* south */
3103 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 965" }, /* south */
3104 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 964" }, /* south */
3105 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 963" }, /* south */
3106 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 962" }, /* south */
3108 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 745" }, /* 1chip */
3109 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 735" }, /* 1chip */
3110 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 733" }, /* 1chip */
3111 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "SiS 730" }, /* 1chip */
3113 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 635" }, /* 1chip */
3114 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 633" }, /* unknown */
3115 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "SiS 630S"}, /* 1chip */
3116 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "SiS 630" }, /* 1chip */
3117 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "SiS 620" }, /* 1chip */
3119 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "SiS 550" },
3120 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "SiS 540" },
3121 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "SiS 530" },
3123 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "SiS 5513" },
3124 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "SiS 5513" },
3125 { 0, 0, 0, 0, 0, 0 }};
3129 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
3132 if (idx->cfg2 && !found) {
3133 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
3135 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
3136 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
3138 idx->cfg1 = SIS133NEW;
3139 idx->max_dma = ATA_UDMA6;
3140 sprintf(buffer, "SiS 962/963 %s controller",
3141 ata_mode2str(idx->max_dma));
3143 pci_write_config(dev, 0x57, reg57, 1);
3145 if (idx->cfg2 && !found) {
3146 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
3148 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
3149 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
3150 struct ata_chip_id id[] =
3151 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
3154 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
3155 idx->cfg1 = SIS133OLD;
3156 idx->max_dma = ATA_UDMA6;
3159 idx->cfg1 = SIS100NEW;
3160 idx->max_dma = ATA_UDMA5;
3162 sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
3164 pci_write_config(dev, 0x4a, reg4a, 1);
3167 sprintf(buffer,"%s %s controller",idx->text,ata_mode2str(idx->max_dma));
3169 device_set_desc_copy(dev, buffer);
3171 ctlr->chipinit = ata_sis_chipinit;
3176 ata_sis_chipinit(device_t dev)
3178 struct ata_pci_controller *ctlr = device_get_softc(dev);
3180 if (ata_setup_interrupt(dev))
3183 switch (ctlr->chip->cfg1) {
3188 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
3192 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
3195 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
3196 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
3199 ctlr->r_type2 = SYS_RES_IOPORT;
3200 ctlr->r_rid2 = PCIR_BAR(5);
3201 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3202 &ctlr->r_rid2, RF_ACTIVE))) {
3203 pci_write_config(dev, PCIR_COMMAND,
3204 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3205 ctlr->allocate = ata_sis_allocate;
3206 ctlr->reset = ata_sis_reset;
3208 ctlr->setmode = ata_sata_setmode;
3213 ctlr->setmode = ata_sis_setmode;
3218 ata_sis_allocate(device_t dev)
3220 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3221 struct ata_channel *ch = device_get_softc(dev);
3223 /* setup the usual register normal pci style */
3224 ata_pci_allocate(dev);
3226 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3227 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 4);
3228 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3229 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 4);
3230 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3231 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 4);
3232 ch->flags |= ATA_NO_SLAVE;
3234 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
3235 /* XXX SOS unknown how to enable PHY state change interrupt */
3240 ata_sis_reset(device_t dev)
3242 struct ata_channel *ch = device_get_softc(dev);
3244 ata_sata_phy_enable(ch);
3249 ata_sis_setmode(device_t dev, int mode)
3251 device_t gparent = GRANDPARENT(dev);
3252 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3253 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3254 struct ata_device *atadev = device_get_softc(dev);
3255 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3258 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3260 if (ctlr->chip->cfg1 == SIS133NEW) {
3261 if (mode > ATA_UDMA2 &&
3262 pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
3263 ata_print_cable(dev, "controller");
3268 if (mode > ATA_UDMA2 &&
3269 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
3270 ata_print_cable(dev, "controller");
3275 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3278 device_printf(dev, "%ssetting %s on %s chip\n",
3279 (error) ? "FAILURE " : "",
3280 ata_mode2str(mode), ctlr->chip->text);
3282 switch (ctlr->chip->cfg1) {
3284 u_int32_t timings[] =
3285 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
3286 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
3287 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
3290 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
3291 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
3295 u_int16_t timings[] =
3296 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
3297 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
3299 u_int16_t reg = 0x40 + (devno << 1);
3301 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3305 u_int16_t timings[] =
3306 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
3307 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
3308 u_int16_t reg = 0x40 + (devno << 1);
3310 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3316 u_int16_t timings[] =
3317 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
3318 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
3319 u_int16_t reg = 0x40 + (devno << 1);
3321 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3325 atadev->mode = mode;
3330 /* VIA Technologies Inc. chipset support functions */
3332 ata_via_ident(device_t dev)
3334 struct ata_pci_controller *ctlr = device_get_softc(dev);
3335 struct ata_chip_id *idx;
3336 static struct ata_chip_id ids[] =
3337 {{ ATA_VIA82C586, 0x02, VIA33, 0x00, ATA_UDMA2, "VIA 82C586B" },
3338 { ATA_VIA82C586, 0x00, VIA33, 0x00, ATA_WDMA2, "VIA 82C586" },
3339 { ATA_VIA82C596, 0x12, VIA66, VIACLK, ATA_UDMA4, "VIA 82C596B" },
3340 { ATA_VIA82C596, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C596" },
3341 { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "VIA 82C686B"},
3342 { ATA_VIA82C686, 0x10, VIA66, VIACLK, ATA_UDMA4, "VIA 82C686A" },
3343 { ATA_VIA82C686, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C686" },
3344 { ATA_VIA8231, 0x00, VIA100, VIABUG, ATA_UDMA5, "VIA 8231" },
3345 { ATA_VIA8233, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233" },
3346 { ATA_VIA8233C, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233C" },
3347 { ATA_VIA8233A, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8233A" },
3348 { ATA_VIA8235, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8235" },
3349 { ATA_VIA8237, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8237" },
3350 { 0, 0, 0, 0, 0, 0 }};
3351 static struct ata_chip_id new_ids[] =
3352 {{ ATA_VIA6410, 0x00, 0, 0x00, ATA_UDMA6, "VIA 6410" },
3353 { ATA_VIA6420, 0x00, 7, 0x00, ATA_SA150, "VIA 6420" },
3354 { ATA_VIA6421, 0x00, 6, 0x00, ATA_SA150, "VIA 6421" },
3355 { 0, 0, 0, 0, 0, 0 }};
3358 if (pci_get_devid(dev) == ATA_VIA82C571) {
3359 if (!(idx = ata_find_chip(dev, ids, -99)))
3363 if (!(idx = ata_match_chip(dev, new_ids)))
3367 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3368 device_set_desc_copy(dev, buffer);
3370 ctlr->chipinit = ata_via_chipinit;
3375 ata_via_chipinit(device_t dev)
3377 struct ata_pci_controller *ctlr = device_get_softc(dev);
3379 if (ata_setup_interrupt(dev))
3382 if (ctlr->chip->max_dma >= ATA_SA150) {
3383 ctlr->r_type2 = SYS_RES_IOPORT;
3384 ctlr->r_rid2 = PCIR_BAR(5);
3385 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3386 &ctlr->r_rid2, RF_ACTIVE))) {
3387 pci_write_config(dev, PCIR_COMMAND,
3388 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3389 ctlr->allocate = ata_via_allocate;
3390 ctlr->reset = ata_via_reset;
3392 ctlr->setmode = ata_sata_setmode;
3396 /* prepare for ATA-66 on the 82C686a and 82C596b */
3397 if (ctlr->chip->cfg2 & VIACLK)
3398 pci_write_config(dev, 0x50, 0x030b030b, 4);
3400 /* the southbridge might need the data corruption fix */
3401 if (ctlr->chip->cfg2 & VIABUG)
3402 ata_via_southbridge_fixup(dev);
3404 /* set fifo configuration half'n'half */
3405 pci_write_config(dev, 0x43,
3406 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
3408 /* set status register read retry */
3409 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
3411 /* set DMA read & end-of-sector fifo flush */
3412 pci_write_config(dev, 0x46,
3413 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
3415 /* set sector size */
3416 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
3417 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
3419 ctlr->setmode = ata_via_family_setmode;
3424 ata_via_allocate(device_t dev)
3426 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3427 struct ata_channel *ch = device_get_softc(dev);
3429 /* setup the usual register normal pci style */
3430 ata_pci_allocate(dev);
3432 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3433 ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
3434 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3435 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
3436 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3437 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
3438 ch->flags |= ATA_NO_SLAVE;
3440 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
3441 /* XXX SOS unknown how to enable PHY state change interrupt */
3446 ata_via_reset(device_t dev)
3448 struct ata_channel *ch = device_get_softc(dev);
3450 ata_sata_phy_enable(ch);
3454 ata_via_southbridge_fixup(device_t dev)
3459 if (device_get_children(device_get_parent(dev), &children, &nchildren))
3462 for (i = 0; i < nchildren; i++) {
3463 if (pci_get_devid(children[i]) == ATA_VIA8363 ||
3464 pci_get_devid(children[i]) == ATA_VIA8371 ||
3465 pci_get_devid(children[i]) == ATA_VIA8662 ||
3466 pci_get_devid(children[i]) == ATA_VIA8361) {
3467 u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
3469 if ((reg76 & 0xf0) != 0xd0) {
3471 "Correcting VIA config for southbridge data corruption bug\n");
3472 pci_write_config(children[i], 0x75, 0x80, 1);
3473 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
3478 free(children, M_TEMP);
3482 /* common code for VIA, AMD & nVidia */
3484 ata_via_family_setmode(device_t dev, int mode)
3486 device_t gparent = GRANDPARENT(dev);
3487 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3488 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3489 struct ata_device *atadev = device_get_softc(dev);
3490 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
3491 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
3493 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* VIA ATA33 */
3494 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* VIA ATA66 */
3495 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* VIA ATA100 */
3496 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
3497 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }}; /* AMD/nVIDIA */
3498 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3499 int reg = 0x53 - devno;
3502 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3504 if (ctlr->chip->cfg2 & AMDCABLE) {
3505 if (mode > ATA_UDMA2 &&
3506 !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
3507 ata_print_cable(dev, "controller");
3512 mode = ata_check_80pin(dev, mode);
3514 if (ctlr->chip->cfg2 & NVIDIA)
3517 if (ctlr->chip->cfg1 != VIA133)
3518 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
3520 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3523 device_printf(dev, "%ssetting %s on %s chip\n",
3524 (error) ? "FAILURE " : "", ata_mode2str(mode),
3527 if (mode >= ATA_UDMA0)
3528 pci_write_config(gparent, reg,
3529 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
3531 pci_write_config(gparent, reg, 0x8b, 1);
3532 atadev->mode = mode;
3537 /* misc functions */
3538 struct ata_chip_id *
3539 ata_match_chip(device_t dev, struct ata_chip_id *index)
3541 while (index->chipid != 0) {
3542 if (pci_get_devid(dev) == index->chipid &&
3543 pci_get_revid(dev) >= index->chiprev)
3550 static struct ata_chip_id *
3551 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
3556 if (device_get_children(device_get_parent(dev), &children, &nchildren))
3559 while (index->chipid != 0) {
3560 for (i = 0; i < nchildren; i++) {
3561 if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
3562 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
3563 pci_get_devid(children[i]) == index->chipid &&
3564 pci_get_revid(children[i]) >= index->chiprev) {
3565 free(children, M_TEMP);
3571 free(children, M_TEMP);
3576 ata_setup_interrupt(device_t dev)
3578 struct ata_pci_controller *ctlr = device_get_softc(dev);
3579 int rid = ATA_IRQ_RID;
3581 if (!ata_legacy(dev)) {
3582 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3583 RF_SHAREABLE | RF_ACTIVE))) {
3584 device_printf(dev, "unable to map interrupt\n");
3587 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3588 ata_generic_intr, ctlr, &ctlr->handle))) {
3589 device_printf(dev, "unable to setup interrupt\n");
3596 struct ata_serialize {
3597 struct mtx locked_mtx;
3603 ata_serialize(device_t dev, int flags)
3605 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3606 struct ata_channel *ch = device_get_softc(dev);
3607 struct ata_serialize *serial;
3608 static int inited = 0;
3612 serial = malloc(sizeof(struct ata_serialize),
3613 M_TEMP, M_NOWAIT | M_ZERO);
3614 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
3615 serial->locked_ch = -1;
3616 serial->restart_ch = -1;
3617 device_set_ivars(ctlr->dev, serial);
3621 serial = device_get_ivars(ctlr->dev);
3623 mtx_lock(&serial->locked_mtx);
3626 if (serial->locked_ch == -1)
3627 serial->locked_ch = ch->unit;
3628 if (serial->locked_ch != ch->unit)
3629 serial->restart_ch = ch->unit;
3633 if (serial->locked_ch == ch->unit) {
3634 serial->locked_ch = -1;
3635 if (serial->restart_ch != -1) {
3636 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
3637 serial->restart_ch = -1;
3638 mtx_unlock(&serial->locked_mtx);
3649 res = serial->locked_ch;
3650 mtx_unlock(&serial->locked_mtx);
3655 ata_print_cable(device_t dev, u_int8_t *who)
3658 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
3662 ata_atapi(device_t dev)
3664 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3665 struct ata_device *atadev = device_get_softc(dev);
3667 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
3668 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
3672 ata_check_80pin(device_t dev, int mode)
3674 struct ata_device *atadev = device_get_softc(dev);
3676 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
3677 ata_print_cable(dev, "device");
3684 ata_mode2idx(int mode)
3686 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
3687 return (mode & ATA_MODE_MASK) + 8;
3688 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
3689 return (mode & ATA_MODE_MASK) + 5;
3690 return (mode & ATA_MODE_MASK) - ATA_PIO0;