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(struct ata_request *);
95 static int ata_promise_sx4_command(struct ata_request *);
96 static int ata_promise_apkt(u_int8_t *, struct ata_request *);
97 static void ata_promise_queue_hpkt(struct ata_pci_controller *, u_int32_t);
98 static void ata_promise_next_hpkt(struct ata_pci_controller *);
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, u_int8_t *);
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 /* XXX SOS 10 secs for now as I have little patience */
240 for (timeout = 0; timeout < 1000; 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 (timeout < 1000) {
249 if ((ATA_IDX_INB(ch, ATA_CYL_LSB) == ATAPI_MAGIC_LSB) &&
250 (ATA_IDX_INB(ch, ATA_CYL_MSB) == ATAPI_MAGIC_MSB))
251 ch->devices = ATA_ATAPI_MASTER;
252 else /*if ((ATA_IDX_INB(ch, ATA_COUNT) == 0x01) &&
253 (ATA_IDX_INB(ch, ATA_CYL_LSB) == 0x01)) */
254 ch->devices = ATA_ATA_MASTER;
257 device_printf(ch->dev, "sata_connect devices=0x%b\n",
258 ch->devices, "\20\3ATAPI_MASTER\1ATA_MASTER");
263 ata_sata_phy_enable(struct ata_channel *ch)
267 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE) {
268 ata_sata_connect(ch);
272 for (retry = 0; retry < 10; retry++) {
273 for (loop = 0; loop < 10; loop++) {
274 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_RESET);
276 if ((ATA_IDX_INL(ch, ATA_SCONTROL) &
277 ATA_SC_DET_MASK) == ATA_SC_DET_RESET)
281 for (loop = 0; loop < 10; loop++) {
282 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_IDLE);
284 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == 0) {
285 ata_sata_connect(ch);
293 ata_sata_phy_event(void *context, int dummy)
295 struct ata_connect_task *tp = (struct ata_connect_task *)context;
299 mtx_lock(&Giant); /* newbus suckage it needs Giant */
300 if (tp->action == ATA_C_ATTACH) {
301 struct ata_channel *ch = device_get_softc(tp->dev);
303 device_printf(tp->dev, "CONNECTED\n");
304 ata_sata_connect(ch);
305 ata_identify(tp->dev);
307 if (tp->action == ATA_C_DETACH) {
308 if (!device_get_children(tp->dev, &children, &nchildren)) {
309 for (i = 0; i < nchildren; i++)
311 device_delete_child(tp->dev, children[i]);
312 free(children, M_TEMP);
314 device_printf(tp->dev, "DISCONNECTED\n");
316 mtx_unlock(&Giant); /* suckage code dealt with, release Giant */
322 * Acard chipset support functions
325 ata_acard_ident(device_t dev)
327 struct ata_pci_controller *ctlr = device_get_softc(dev);
328 struct ata_chip_id *idx;
329 static struct ata_chip_id ids[] =
330 {{ ATA_ATP850R, 0, ATPOLD, 0x00, ATA_UDMA2, "Acard ATP850" },
331 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "Acard ATP860A" },
332 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "Acard ATP860R" },
333 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "Acard ATP865A" },
334 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "Acard ATP865R" },
335 { 0, 0, 0, 0, 0, 0}};
338 if (!(idx = ata_match_chip(dev, ids)))
341 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
342 device_set_desc_copy(dev, buffer);
344 ctlr->chipinit = ata_acard_chipinit;
349 ata_acard_chipinit(device_t dev)
351 struct ata_pci_controller *ctlr = device_get_softc(dev);
352 int rid = ATA_IRQ_RID;
354 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
355 RF_SHAREABLE | RF_ACTIVE))) {
356 device_printf(dev, "unable to map interrupt\n");
359 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
360 ata_acard_intr, ctlr, &ctlr->handle))) {
361 device_printf(dev, "unable to setup interrupt\n");
364 if (ctlr->chip->cfg1 == ATPOLD) {
365 ctlr->setmode = ata_acard_850_setmode;
366 ctlr->locking = ata_serialize;
369 ctlr->setmode = ata_acard_86X_setmode;
374 ata_acard_intr(void *data)
376 struct ata_pci_controller *ctlr = data;
377 struct ata_channel *ch;
380 /* implement this as a toggle instead to balance load XXX */
381 for (unit = 0; unit < ctlr->channels; unit++) {
382 if (!(ch = ctlr->interrupt[unit].argument))
384 if (ctlr->chip->cfg1 == ATPOLD &&
385 ATA_LOCKING(ch->dev, ATA_LF_WHICH) != unit)
387 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
388 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
390 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
391 ATA_BMSTAT_INTERRUPT)
393 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
395 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
396 ATA_IDX_INB(ch, ATA_BMCMD_PORT)&~ATA_BMCMD_START_STOP);
399 ctlr->interrupt[unit].function(ch);
404 ata_acard_850_setmode(device_t dev, int mode)
406 device_t gparent = GRANDPARENT(dev);
407 struct ata_pci_controller *ctlr = device_get_softc(gparent);
408 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
409 struct ata_device *atadev = device_get_softc(dev);
410 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
413 mode = ata_limit_mode(dev, mode,
414 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
416 /* XXX SOS missing WDMA0+1 + PIO modes */
417 if (mode >= ATA_WDMA2) {
418 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
420 device_printf(dev, "%ssetting %s on %s chip\n",
421 (error) ? "FAILURE " : "",
422 ata_mode2str(mode), ctlr->chip->text);
424 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
426 reg54 &= ~(0x03 << (devno << 1));
427 if (mode >= ATA_UDMA0)
428 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
429 pci_write_config(gparent, 0x54, reg54, 1);
430 pci_write_config(gparent, 0x4a, 0xa6, 1);
431 pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
436 /* we could set PIO mode timings, but we assume the BIOS did that */
440 ata_acard_86X_setmode(device_t dev, int mode)
442 device_t gparent = GRANDPARENT(dev);
443 struct ata_pci_controller *ctlr = device_get_softc(gparent);
444 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
445 struct ata_device *atadev = device_get_softc(dev);
446 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
450 mode = ata_limit_mode(dev, mode,
451 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
453 mode = ata_check_80pin(dev, mode);
455 /* XXX SOS missing WDMA0+1 + PIO modes */
456 if (mode >= ATA_WDMA2) {
457 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
459 device_printf(dev, "%ssetting %s on %s chip\n",
460 (error) ? "FAILURE " : "",
461 ata_mode2str(mode), ctlr->chip->text);
463 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
465 reg44 &= ~(0x000f << (devno << 2));
466 if (mode >= ATA_UDMA0)
467 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
468 pci_write_config(gparent, 0x44, reg44, 2);
469 pci_write_config(gparent, 0x4a, 0xa6, 1);
470 pci_write_config(gparent, 0x40 + devno, 0x31, 1);
475 /* we could set PIO mode timings, but we assume the BIOS did that */
480 * Acer Labs Inc (ALI) chipset support functions
483 ata_ali_ident(device_t dev)
485 struct ata_pci_controller *ctlr = device_get_softc(dev);
486 struct ata_chip_id *idx;
487 static struct ata_chip_id ids[] =
488 {{ ATA_ALI_5289, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5289" },
489 { ATA_ALI_5287, 0x00, 4, ALISATA, ATA_SA150, "AcerLabs M5287" },
490 { ATA_ALI_5281, 0x00, 2, ALISATA, ATA_SA150, "AcerLabs M5281" },
491 { ATA_ALI_5229, 0xc5, 0, ALINEW, ATA_UDMA6, "AcerLabs M5229" },
492 { ATA_ALI_5229, 0xc4, 0, ALINEW, ATA_UDMA5, "AcerLabs M5229" },
493 { ATA_ALI_5229, 0xc2, 0, ALINEW, ATA_UDMA4, "AcerLabs M5229" },
494 { ATA_ALI_5229, 0x20, 0, ALIOLD, ATA_UDMA2, "AcerLabs M5229" },
495 { ATA_ALI_5229, 0x00, 0, ALIOLD, ATA_WDMA2, "AcerLabs M5229" },
496 { 0, 0, 0, 0, 0, 0}};
499 if (!(idx = ata_match_chip(dev, ids)))
502 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
503 device_set_desc_copy(dev, buffer);
505 ctlr->chipinit = ata_ali_chipinit;
510 ata_ali_chipinit(device_t dev)
512 struct ata_pci_controller *ctlr = device_get_softc(dev);
514 if (ata_setup_interrupt(dev))
517 switch (ctlr->chip->cfg2) {
519 pci_write_config(dev, PCIR_COMMAND,
520 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
521 ctlr->channels = ctlr->chip->cfg1;
522 ctlr->allocate = ata_ali_allocate;
523 ctlr->setmode = ata_sata_setmode;
527 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
528 pci_write_config(dev, 0x53,
529 pci_read_config(dev, 0x53, 1) | 0x01, 1);
531 /* enable cable detection and UDMA support on newer chips */
532 pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
533 ctlr->setmode = ata_ali_setmode;
537 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
538 pci_write_config(dev, 0x53,
539 (pci_read_config(dev, 0x53, 1) & ~0x02) | 0x03, 1);
540 ctlr->setmode = ata_ali_setmode;
547 ata_ali_allocate(device_t dev)
549 device_t parent = device_get_parent(dev);
550 struct ata_pci_controller *ctlr = device_get_softc(parent);
551 struct ata_channel *ch = device_get_softc(dev);
552 struct resource *io = NULL, *ctlio = NULL;
553 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
556 rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
557 io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
561 rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
562 ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
564 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
568 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
569 ch->r_io[i].res = io;
570 ch->r_io[i].offset = i + (unit10 ? 8 : 0);
572 ch->r_io[ATA_CONTROL].res = ctlio;
573 ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
574 ch->r_io[ATA_IDX_ADDR].res = io;
575 ata_default_registers(dev);
577 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
578 ch->r_io[i].res = ctlr->r_res1;
579 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
582 ch->flags |= ATA_NO_SLAVE;
584 /* XXX SOS PHY handling awkward in ALI chip not supported yet */
590 ata_ali_setmode(device_t dev, int mode)
592 device_t gparent = GRANDPARENT(dev);
593 struct ata_pci_controller *ctlr = device_get_softc(gparent);
594 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
595 struct ata_device *atadev = device_get_softc(dev);
596 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
599 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
601 if (ctlr->chip->cfg2 & ALINEW) {
602 if (mode > ATA_UDMA2 &&
603 pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
604 ata_print_cable(dev, "controller");
609 mode = ata_check_80pin(dev, mode);
611 if (ctlr->chip->cfg2 & ALIOLD) {
612 /* doesn't support ATAPI DMA on write */
613 ch->flags |= ATA_ATAPI_DMA_RO;
614 if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
615 /* doesn't support ATAPI DMA on two ATAPI devices */
616 device_printf(dev, "two atapi devices on this channel, no DMA\n");
617 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
621 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
624 device_printf(dev, "%ssetting %s on %s chip\n",
625 (error) ? "FAILURE " : "",
626 ata_mode2str(mode), ctlr->chip->text);
628 if (mode >= ATA_UDMA0) {
629 u_int8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
630 u_int32_t word54 = pci_read_config(gparent, 0x54, 4);
632 word54 &= ~(0x000f000f << (devno << 2));
633 word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
634 pci_write_config(gparent, 0x54, word54, 4);
635 pci_write_config(gparent, 0x58 + (ch->unit << 2),
639 u_int32_t piotimings[] =
640 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
641 0x00310001, 0x00440001, 0x00330001, 0x00310001};
643 pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
644 ~(0x0008000f << (devno << 2)), 4);
645 pci_write_config(gparent, 0x58 + (ch->unit << 2),
646 piotimings[ata_mode2idx(mode)], 4);
654 * American Micro Devices (AMD) chipset support functions
657 ata_amd_ident(device_t dev)
659 struct ata_pci_controller *ctlr = device_get_softc(dev);
660 struct ata_chip_id *idx;
661 static struct ata_chip_id ids[] =
662 {{ ATA_AMD756, 0x00, AMDNVIDIA, 0x00, ATA_UDMA4, "AMD 756" },
663 { ATA_AMD766, 0x00, AMDNVIDIA, AMDCABLE|AMDBUG, ATA_UDMA5, "AMD 766" },
664 { ATA_AMD768, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA5, "AMD 768" },
665 { ATA_AMD8111, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA6, "AMD 8111" },
666 { 0, 0, 0, 0, 0, 0}};
669 if (!(idx = ata_match_chip(dev, ids)))
672 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
673 device_set_desc_copy(dev, buffer);
675 ctlr->chipinit = ata_amd_chipinit;
680 ata_amd_chipinit(device_t dev)
682 struct ata_pci_controller *ctlr = device_get_softc(dev);
684 if (ata_setup_interrupt(dev))
687 /* disable/set prefetch, postwrite */
688 if (ctlr->chip->cfg2 & AMDBUG)
689 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
691 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
693 ctlr->setmode = ata_via_family_setmode;
699 * Cyrix chipset support functions
702 ata_cyrix_ident(device_t dev)
704 struct ata_pci_controller *ctlr = device_get_softc(dev);
706 if (pci_get_devid(dev) == ATA_CYRIX_5530) {
707 device_set_desc(dev, "Cyrix 5530 ATA33 controller");
708 ctlr->chipinit = ata_cyrix_chipinit;
715 ata_cyrix_chipinit(device_t dev)
717 struct ata_pci_controller *ctlr = device_get_softc(dev);
719 if (ata_setup_interrupt(dev))
723 ctlr->setmode = ata_cyrix_setmode;
725 ctlr->setmode = ata_generic_setmode;
730 ata_cyrix_setmode(device_t dev, int mode)
732 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
733 struct ata_device *atadev = device_get_softc(dev);
734 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
735 u_int32_t piotiming[] =
736 { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
737 u_int32_t dmatiming[] = { 0x00077771, 0x00012121, 0x00002020 };
738 u_int32_t udmatiming[] = { 0x00921250, 0x00911140, 0x00911030 };
741 ch->dma->alignment = 16;
742 ch->dma->max_iosize = 126 * DEV_BSIZE;
744 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
746 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
749 device_printf(dev, "%ssetting %s on Cyrix chip\n",
750 (error) ? "FAILURE " : "", ata_mode2str(mode));
752 if (mode >= ATA_UDMA0) {
753 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
754 0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
756 else if (mode >= ATA_WDMA0) {
757 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
758 0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
761 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
762 0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
770 * Cypress chipset support functions
773 ata_cypress_ident(device_t dev)
775 struct ata_pci_controller *ctlr = device_get_softc(dev);
778 * the Cypress chip is a mess, it contains two ATA functions, but
779 * both channels are visible on the first one.
780 * simply ignore the second function for now, as the right
781 * solution (ignoring the second channel on the first function)
782 * doesn't work with the crappy ATA interrupt setup on the alpha.
784 if (pci_get_devid(dev) == ATA_CYPRESS_82C693 &&
785 pci_get_function(dev) == 1 &&
786 pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
787 device_set_desc(dev, "Cypress 82C693 ATA controller");
788 ctlr->chipinit = ata_cypress_chipinit;
795 ata_cypress_chipinit(device_t dev)
797 struct ata_pci_controller *ctlr = device_get_softc(dev);
799 if (ata_setup_interrupt(dev))
802 ctlr->setmode = ata_cypress_setmode;
807 ata_cypress_setmode(device_t dev, int mode)
809 device_t gparent = GRANDPARENT(dev);
810 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
811 struct ata_device *atadev = device_get_softc(dev);
814 mode = ata_limit_mode(dev, mode, ATA_WDMA2);
816 /* XXX missing WDMA0+1 + PIO modes */
817 if (mode == ATA_WDMA2) {
818 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
820 device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
821 error ? "FAILURE " : "");
823 pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
828 /* we could set PIO mode timings, but we assume the BIOS did that */
833 * HighPoint chipset support functions
836 ata_highpoint_ident(device_t dev)
838 struct ata_pci_controller *ctlr = device_get_softc(dev);
839 struct ata_chip_id *idx;
840 static struct ata_chip_id ids[] =
841 {{ ATA_HPT374, 0x07, HPT374, 0x00, ATA_UDMA6, "HighPoint HPT374" },
842 { ATA_HPT372, 0x02, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372N" },
843 { ATA_HPT372, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372" },
844 { ATA_HPT371, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT371" },
845 { ATA_HPT366, 0x05, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT372" },
846 { ATA_HPT366, 0x03, HPT370, 0x00, ATA_UDMA5, "HighPoint HPT370" },
847 { ATA_HPT366, 0x02, HPT366, 0x00, ATA_UDMA4, "HighPoint HPT368" },
848 { ATA_HPT366, 0x00, HPT366, HPTOLD, ATA_UDMA4, "HighPoint HPT366" },
849 { ATA_HPT302, 0x01, HPT372, 0x00, ATA_UDMA6, "HighPoint HPT302" },
850 { 0, 0, 0, 0, 0, 0}};
853 if (!(idx = ata_match_chip(dev, ids)))
856 strcpy(buffer, idx->text);
857 if (idx->cfg1 == HPT374) {
858 if (pci_get_function(dev) == 0)
859 strcat(buffer, " (channel 0+1)");
860 else if (pci_get_function(dev) == 1)
861 strcat(buffer, " (channel 2+3)");
863 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
864 device_set_desc_copy(dev, buffer);
866 ctlr->chipinit = ata_highpoint_chipinit;
871 ata_highpoint_chipinit(device_t dev)
873 struct ata_pci_controller *ctlr = device_get_softc(dev);
874 int rid = ATA_IRQ_RID;
876 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
877 RF_SHAREABLE | RF_ACTIVE))) {
878 device_printf(dev, "unable to map interrupt\n");
881 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
882 ata_highpoint_intr, ctlr, &ctlr->handle))) {
883 device_printf(dev, "unable to setup interrupt\n");
887 if (ctlr->chip->cfg2 == HPTOLD) {
888 /* disable interrupt prediction */
889 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
892 /* disable interrupt prediction */
893 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
894 pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
896 /* enable interrupts */
897 pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
900 if (ctlr->chip->cfg1 < HPT372)
901 pci_write_config(dev, 0x5b, 0x22, 1);
903 pci_write_config(dev, 0x5b,
904 (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
906 ctlr->setmode = ata_highpoint_setmode;
911 ata_highpoint_intr(void *data)
913 struct ata_pci_controller *ctlr = data;
914 struct ata_channel *ch;
917 /* implement this as a toggle instead to balance load XXX */
918 for (unit = 0; unit < ctlr->channels; unit++) {
919 if (!(ch = ctlr->interrupt[unit].argument))
922 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
924 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
925 ATA_BMSTAT_INTERRUPT)
927 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
930 ctlr->interrupt[unit].function(ch);
935 ata_highpoint_setmode(device_t dev, int mode)
937 device_t gparent = GRANDPARENT(dev);
938 struct ata_pci_controller *ctlr = device_get_softc(gparent);
939 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
940 struct ata_device *atadev = device_get_softc(dev);
941 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
943 u_int32_t timings33[][4] = {
944 /* HPT366 HPT370 HPT372 HPT374 mode */
945 { 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a }, /* PIO 0 */
946 { 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 }, /* PIO 1 */
947 { 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 }, /* PIO 2 */
948 { 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 }, /* PIO 3 */
949 { 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 }, /* PIO 4 */
950 { 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea }, /* MWDMA 0 */
951 { 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 }, /* MWDMA 1 */
952 { 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 }, /* MWDMA 2 */
953 { 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea }, /* UDMA 0 */
954 { 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 }, /* UDMA 1 */
955 { 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 }, /* UDMA 2 */
956 { 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 }, /* UDMA 3 */
957 { 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 }, /* UDMA 4 */
958 { 0, 0x16454e31, 0x1c8a9c62, 0x12848242 }, /* UDMA 5 */
959 { 0, 0, 0x1c869c62, 0x12808242 } /* UDMA 6 */
962 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
964 if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
965 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
967 mode = ata_highpoint_check_80pin(dev, mode);
970 * most if not all HPT chips cant really handle that the device is
971 * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
972 * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
974 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
975 ata_limit_mode(dev, mode, ATA_UDMA5));
977 device_printf(dev, "%ssetting %s on HighPoint chip\n",
978 (error) ? "FAILURE " : "", ata_mode2str(mode));
980 pci_write_config(gparent, 0x40 + (devno << 2),
981 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
986 ata_highpoint_check_80pin(device_t dev, int mode)
988 device_t gparent = GRANDPARENT(dev);
989 struct ata_pci_controller *ctlr = device_get_softc(gparent);
990 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
991 u_int8_t reg, val, res;
993 if (ctlr->chip->cfg1 == HPT374 && pci_get_function(gparent) == 1) {
994 reg = ch->unit ? 0x57 : 0x53;
995 val = pci_read_config(gparent, reg, 1);
996 pci_write_config(gparent, reg, val | 0x80, 1);
1000 val = pci_read_config(gparent, reg, 1);
1001 pci_write_config(gparent, reg, val & 0xfe, 1);
1003 res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1004 pci_write_config(gparent, reg, val, 1);
1006 if (mode > ATA_UDMA2 && res) {
1007 ata_print_cable(dev, "controller");
1015 * Intel chipset support functions
1018 ata_intel_ident(device_t dev)
1020 struct ata_pci_controller *ctlr = device_get_softc(dev);
1021 struct ata_chip_id *idx;
1022 static struct ata_chip_id ids[] =
1023 {{ ATA_I82371FB, 0, 0, 0x00, ATA_WDMA2, "Intel PIIX" },
1024 { ATA_I82371SB, 0, 0, 0x00, ATA_WDMA2, "Intel PIIX3" },
1025 { ATA_I82371AB, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1026 { ATA_I82443MX, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1027 { ATA_I82451NX, 0, 0, 0x00, ATA_UDMA2, "Intel PIIX4" },
1028 { ATA_I82801AB, 0, 0, 0x00, ATA_UDMA2, "Intel ICH0" },
1029 { ATA_I82801AA, 0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1030 { ATA_I82372FB, 0, 0, 0x00, ATA_UDMA4, "Intel ICH" },
1031 { ATA_I82801BA, 0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1032 { ATA_I82801BA_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH2" },
1033 { ATA_I82801CA, 0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1034 { ATA_I82801CA_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH3" },
1035 { ATA_I82801DB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1036 { ATA_I82801DB_1, 0, 0, 0x00, ATA_UDMA5, "Intel ICH4" },
1037 { ATA_I82801EB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH5" },
1038 { ATA_I82801EB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1039 { ATA_I82801EB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH5" },
1040 { ATA_I6300ESB, 0, 0, 0x00, ATA_UDMA5, "Intel 6300ESB" },
1041 { ATA_I6300ESB_S1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1042 { ATA_I6300ESB_R1, 0, 0, 0x00, ATA_SA150, "Intel 6300ESB" },
1043 { ATA_I82801FB, 0, 0, 0x00, ATA_UDMA5, "Intel ICH6" },
1044 { ATA_I82801FB_S1, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1045 { ATA_I82801FB_R1, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1046 { ATA_I82801FBM, 0, 0, 0x00, ATA_SA150, "Intel ICH6" },
1047 { 0, 0, 0, 0, 0, 0}};
1050 if (!(idx = ata_match_chip(dev, ids)))
1053 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1054 device_set_desc_copy(dev, buffer);
1056 ctlr->chipinit = ata_intel_chipinit;
1061 ata_intel_chipinit(device_t dev)
1063 struct ata_pci_controller *ctlr = device_get_softc(dev);
1064 int rid = ATA_IRQ_RID;
1066 if (!ata_legacy(dev)) {
1067 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1068 RF_SHAREABLE | RF_ACTIVE))) {
1069 device_printf(dev, "unable to map interrupt\n");
1072 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1073 ata_intel_intr, ctlr, &ctlr->handle))) {
1074 device_printf(dev, "unable to setup interrupt\n");
1079 if (ctlr->chip->chipid == ATA_I82371FB) {
1080 ctlr->setmode = ata_intel_old_setmode;
1082 else if (ctlr->chip->max_dma < ATA_SA150) {
1083 ctlr->setmode = ata_intel_new_setmode;
1086 pci_write_config(dev, PCIR_COMMAND,
1087 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1088 ctlr->reset = ata_intel_reset;
1089 ctlr->setmode = ata_sata_setmode;
1095 ata_intel_intr(void *data)
1097 struct ata_pci_controller *ctlr = data;
1098 struct ata_channel *ch;
1101 /* implement this as a toggle instead to balance load XXX */
1102 for (unit = 0; unit < ctlr->channels; unit++) {
1103 if (!(ch = ctlr->interrupt[unit].argument))
1106 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1108 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
1109 ATA_BMSTAT_INTERRUPT)
1111 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
1114 ctlr->interrupt[unit].function(ch);
1119 ata_intel_old_setmode(device_t dev, int mode)
1125 ata_intel_reset(device_t dev)
1127 device_t parent = device_get_parent(dev);
1128 struct ata_pci_controller *ctlr = device_get_softc(parent);
1129 struct ata_channel *ch = device_get_softc(dev);
1132 /* ICH6 has 4 SATA ports as master/slave on 2 channels so deal with pairs */
1133 if (ctlr->chip->chipid == ATA_I82801FB_S1 ||
1134 ctlr->chip->chipid == ATA_I82801FB_R1) {
1135 mask = (0x0005 << ch->unit);
1138 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1139 if (pci_read_config(parent, 0x90, 1) & 0x04)
1142 mask = (0x0001 << ch->unit);
1143 /* XXX SOS should be in intel_allocate when we grow it */
1144 ch->flags |= ATA_NO_SLAVE;
1147 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1149 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1151 /* wait up to 1 sec for "connect well" */
1152 for (timeout = 0; timeout < 100 ; timeout++) {
1153 if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1154 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1158 ata_generic_reset(dev);
1162 ata_intel_new_setmode(device_t dev, int mode)
1164 device_t gparent = GRANDPARENT(dev);
1165 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1166 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1167 struct ata_device *atadev = device_get_softc(dev);
1168 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1169 u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
1170 u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
1171 u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
1172 u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
1173 u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
1174 u_int32_t mask40 = 0, new40 = 0;
1175 u_int8_t mask44 = 0, new44 = 0;
1177 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1178 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1180 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1182 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1183 ata_print_cable(dev, "controller");
1187 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1190 device_printf(dev, "%ssetting %s on %s chip\n",
1191 (error) ? "FAILURE " : "",
1192 ata_mode2str(mode), ctlr->chip->text);
1196 if (mode >= ATA_UDMA0) {
1197 pci_write_config(gparent, 0x48, reg48 | (0x0001 << devno), 2);
1198 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno<<2))) |
1199 (0x01 + !(mode & 0x01)), 2);
1202 pci_write_config(gparent, 0x48, reg48 & ~(0x0001 << devno), 2);
1203 pci_write_config(gparent, 0x4a, (reg4a & ~(0x3 << (devno << 2))), 2);
1206 if (mode >= ATA_UDMA2)
1207 pci_write_config(gparent, 0x54, reg54 | (0x1 << devno), 2);
1209 pci_write_config(gparent, 0x54, reg54 & ~(0x1 << devno), 2);
1211 if (mode >= ATA_UDMA5)
1212 pci_write_config(gparent, 0x54, reg54 | (0x1000 << devno), 2);
1214 pci_write_config(gparent, 0x54, reg54 & ~(0x1000 << devno), 2);
1216 reg40 &= ~0x00ff00ff;
1217 reg40 |= 0x40774077;
1219 if (atadev->unit == ATA_MASTER) {
1221 new40 = timings[ata_mode2idx(mode)] << 8;
1225 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
1226 (timings[ata_mode2idx(mode)] & 0x03);
1234 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
1235 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
1237 atadev->mode = mode;
1242 * Integrated Technology Express Inc. (ITE) chipset support functions
1245 ata_ite_ident(device_t dev)
1247 struct ata_pci_controller *ctlr = device_get_softc(dev);
1249 if (pci_get_devid(dev) == ATA_IT8212F) {
1250 device_set_desc(dev, "ITE IT8212F ATA133 controller");
1251 ctlr->chipinit = ata_ite_chipinit;
1258 ata_ite_chipinit(device_t dev)
1260 struct ata_pci_controller *ctlr = device_get_softc(dev);
1262 if (ata_setup_interrupt(dev))
1265 ctlr->setmode = ata_ite_setmode;
1267 /* set PCI mode and 66Mhz reference clock */
1268 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
1270 /* set default active & recover timings */
1271 pci_write_config(dev, 0x54, 0x31, 1);
1272 pci_write_config(dev, 0x56, 0x31, 1);
1277 ata_ite_setmode(device_t dev, int mode)
1279 device_t gparent = GRANDPARENT(dev);
1280 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1281 struct ata_device *atadev = device_get_softc(dev);
1282 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1285 /* correct the mode for what the HW supports */
1286 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
1288 /* check the CBLID bits for 80 conductor cable detection */
1289 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
1290 (ch->unit ? (1<<3) : (1<<2)))) {
1291 ata_print_cable(dev, "controller");
1295 /* set the wanted mode on the device */
1296 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1299 device_printf(dev, "%s setting %s on ITE8212F chip\n",
1300 (error) ? "failed" : "success", ata_mode2str(mode));
1302 /* if the device accepted the mode change, setup the HW accordingly */
1304 if (mode >= ATA_UDMA0) {
1305 u_int8_t udmatiming[] =
1306 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
1308 /* enable UDMA mode */
1309 pci_write_config(gparent, 0x50,
1310 pci_read_config(gparent, 0x50, 1) &
1311 ~(1 << (devno + 3)), 1);
1313 /* set UDMA timing */
1314 pci_write_config(gparent,
1315 0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
1316 udmatiming[mode & ATA_MODE_MASK], 1);
1319 u_int8_t chtiming[] =
1320 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
1322 /* disable UDMA mode */
1323 pci_write_config(gparent, 0x50,
1324 pci_read_config(gparent, 0x50, 1) |
1325 (1 << (devno + 3)), 1);
1327 /* set active and recover timing (shared between master & slave) */
1328 if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
1329 chtiming[ata_mode2idx(mode)])
1330 pci_write_config(gparent, 0x54 + (ch->unit << 2),
1331 chtiming[ata_mode2idx(mode)], 1);
1333 atadev->mode = mode;
1339 * National chipset support functions
1342 ata_national_ident(device_t dev)
1344 struct ata_pci_controller *ctlr = device_get_softc(dev);
1346 /* this chip is a clone of the Cyrix chip, bugs and all */
1347 if (pci_get_devid(dev) == ATA_SC1100) {
1348 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
1349 ctlr->chipinit = ata_national_chipinit;
1355 static device_t nat_host = NULL;
1358 ata_national_chipinit(device_t dev)
1360 struct ata_pci_controller *ctlr = device_get_softc(dev);
1364 if (ata_setup_interrupt(dev))
1367 /* locate the ISA part in the southbridge and enable UDMA33 */
1368 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
1369 for (i = 0; i < nchildren; i++) {
1370 if (pci_get_devid(children[i]) == 0x0510100b) {
1371 nat_host = children[i];
1375 free(children, M_TEMP);
1377 ctlr->setmode = ata_national_setmode;
1382 ata_national_setmode(device_t dev, int mode)
1384 device_t gparent = GRANDPARENT(dev);
1385 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1386 struct ata_device *atadev = device_get_softc(dev);
1387 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1388 u_int32_t piotiming[] =
1389 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
1390 0x00803020, 0x20102010, 0x00100010,
1391 0x00100010, 0x00100010, 0x00100010 };
1392 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
1393 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
1396 ch->dma->alignment = 16;
1397 ch->dma->max_iosize = 126 * DEV_BSIZE;
1399 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1401 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1404 device_printf(dev, "%s setting %s on National chip\n",
1405 (error) ? "failed" : "success", ata_mode2str(mode));
1407 if (mode >= ATA_UDMA0) {
1408 pci_write_config(gparent, 0x44 + (devno << 3),
1409 udmatiming[mode & ATA_MODE_MASK], 4);
1411 else if (mode >= ATA_WDMA0) {
1412 pci_write_config(gparent, 0x44 + (devno << 3),
1413 dmatiming[mode & ATA_MODE_MASK], 4);
1416 pci_write_config(gparent, 0x44 + (devno << 3),
1417 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
1420 pci_write_config(gparent, 0x40 + (devno << 3),
1421 piotiming[ata_mode2idx(mode)], 4);
1422 atadev->mode = mode;
1428 * nVidia chipset support functions
1431 ata_nvidia_ident(device_t dev)
1433 struct ata_pci_controller *ctlr = device_get_softc(dev);
1434 struct ata_chip_id *idx;
1435 static struct ata_chip_id ids[] =
1436 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nVidia nForce" },
1437 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2" },
1438 { ATA_NFORCE2_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce2 MCP" },
1439 { ATA_NFORCE2_MCP_S1, 0, 0, 0, ATA_SA150, "nVidia nForce2 MCP" },
1440 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3" },
1441 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 Pro" },
1442 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
1443 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nVidia nForce3 Pro" },
1444 { ATA_NFORCE3_MCP, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce3 MCP" },
1445 { ATA_NFORCE3_MCP_S1, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
1446 { ATA_NFORCE3_MCP_S2, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce3 MCP" },
1447 { ATA_NFORCE4, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nVidia nForce4" },
1448 { ATA_NFORCE4_S1, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce4" },
1449 { ATA_NFORCE4_S2, 0, 0, NV4OFF, ATA_SA150, "nVidia nForce4" },
1450 { 0, 0, 0, 0, 0, 0}};
1453 if (!(idx = ata_match_chip(dev, ids)))
1456 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
1457 device_set_desc_copy(dev, buffer);
1459 ctlr->chipinit = ata_nvidia_chipinit;
1464 ata_nvidia_chipinit(device_t dev)
1466 struct ata_pci_controller *ctlr = device_get_softc(dev);
1468 if (ata_setup_interrupt(dev))
1471 if (ctlr->chip->max_dma >= ATA_SA150) {
1472 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
1473 ctlr->r_type2 = SYS_RES_IOPORT;
1475 ctlr->r_type2 = SYS_RES_MEMORY;
1476 ctlr->r_rid2 = PCIR_BAR(5);
1477 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1478 &ctlr->r_rid2, RF_ACTIVE))) {
1479 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
1480 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1481 ata_nvidia_intr, ctlr, &ctlr->handle)) {
1482 device_printf(dev, "unable to setup interrupt\n");
1485 pci_write_config(dev, PCIR_COMMAND,
1486 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
1487 ctlr->allocate = ata_nvidia_allocate;
1488 ctlr->reset = ata_nvidia_reset;
1490 ctlr->setmode = ata_sata_setmode;
1493 /* disable prefetch, postwrite */
1494 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
1495 ctlr->setmode = ata_via_family_setmode;
1501 ata_nvidia_allocate(device_t dev)
1503 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1504 struct ata_channel *ch = device_get_softc(dev);
1505 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0441 : 0x0011;
1507 /* setup the usual register normal pci style */
1508 ata_pci_allocate(dev);
1510 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
1511 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
1512 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
1513 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
1514 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
1515 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
1516 ch->flags |= ATA_NO_SLAVE;
1518 /* enable PHY state change interrupts */
1519 ATA_OUTB(ctlr->r_res2, offset,
1520 ATA_INB(ctlr->r_res2, offset) | (0x0c << (ch->unit << 2)));
1525 ata_nvidia_intr(void *data)
1527 struct ata_pci_controller *ctlr = data;
1528 struct ata_channel *ch;
1529 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0440 : 0x0010;
1533 /* implement this as a toggle instead to balance load XXX */
1534 for (unit = 0; unit < ctlr->channels; unit++) {
1535 if (!(ch = ctlr->interrupt[unit].argument))
1538 if ((status = ATA_INB(ctlr->r_res2, offset))) {
1539 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
1540 struct ata_connect_task *tp;
1542 /* clear error bits/interrupt */
1543 ATA_IDX_OUTL(ch, ATA_SERROR, error);
1544 ATA_OUTB(ctlr->r_res2, offset, status);
1546 /* check for and handle connect events */
1547 if ((status & (0x04 << (ch->unit << 2))) &&
1548 (tp = (struct ata_connect_task *)
1549 malloc(sizeof(struct ata_connect_task),
1550 M_ATA, M_NOWAIT | M_ZERO))) {
1552 device_printf(ch->dev, "CONNECT requested\n");
1553 tp->action = ATA_C_ATTACH;
1555 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1556 taskqueue_enqueue(taskqueue_thread, &tp->task);
1560 /* check for and handle disconnect events */
1561 if ((status & (0x08 << (ch->unit << 2))) &&
1562 (tp = (struct ata_connect_task *)
1563 malloc(sizeof(struct ata_connect_task),
1564 M_ATA, M_NOWAIT | M_ZERO))) {
1566 device_printf(ch->dev, "DISCONNECT requested\n");
1567 tp->action = ATA_C_DETACH;
1569 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1570 taskqueue_enqueue(taskqueue_thread, &tp->task);
1573 /* any drive action to take care of ? */
1574 if (status & (0x01 << (ch->unit << 2))) {
1575 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
1577 ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
1579 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
1581 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat&~ATA_BMSTAT_ERROR);
1584 ctlr->interrupt[unit].function(ch);
1591 ata_nvidia_reset(device_t dev)
1593 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1594 struct ata_channel *ch = device_get_softc(dev);
1595 int offset = ctlr->chip->cfg2 & NV4OFF ? 0x0441 : 0x0011;
1597 /* disable PHY state change interrupt */
1598 ATA_OUTB(ctlr->r_res2, offset,
1599 ATA_INB(ctlr->r_res2, offset) & (~0x0c << (ch->unit << 2)));
1601 ata_sata_phy_enable(ch);
1603 /* enable PHY state change interrupt */
1604 ATA_OUTB(ctlr->r_res2, offset,
1605 ATA_INB(ctlr->r_res2, offset) | (0x0c << (ch->unit << 2)));
1610 * Promise chipset support functions
1612 #define ATA_PDC_APKT_OFFSET 0x00000010
1613 #define ATA_PDC_HPKT_OFFSET 0x00000040
1614 #define ATA_PDC_ASG_OFFSET 0x00000080
1615 #define ATA_PDC_LSG_OFFSET 0x000000c0
1616 #define ATA_PDC_HSG_OFFSET 0x00000100
1617 #define ATA_PDC_CHN_OFFSET 0x00000400
1618 #define ATA_PDC_BUF_BASE 0x00400000
1619 #define ATA_PDC_BUF_OFFSET 0x00100000
1620 #define ATA_PDC_MAX_HPKT 8
1621 #define ATA_PDC_WRITE_REG 0x00
1622 #define ATA_PDC_WRITE_CTL 0x0e
1623 #define ATA_PDC_WRITE_END 0x08
1624 #define ATA_PDC_WAIT_NBUSY 0x10
1625 #define ATA_PDC_WAIT_READY 0x18
1626 #define ATA_PDC_1B 0x20
1627 #define ATA_PDC_2B 0x40
1629 struct host_packet {
1631 TAILQ_ENTRY(host_packet) chain;
1634 struct ata_promise_sx4 {
1636 TAILQ_HEAD(, host_packet) queue;
1641 ata_promise_ident(device_t dev)
1643 struct ata_pci_controller *ctlr = device_get_softc(dev);
1644 struct ata_chip_id *idx;
1645 static struct ata_chip_id ids[] =
1646 {{ ATA_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "Promise PDC20246" },
1647 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20262" },
1648 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "Promise PDC20263" },
1649 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20265" },
1650 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "Promise PDC20267" },
1651 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20268" },
1652 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20269" },
1653 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "Promise PDC20270" },
1654 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20271" },
1655 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20275" },
1656 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "Promise PDC20276" },
1657 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "Promise PDC20277" },
1658 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20318" },
1659 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "Promise PDC20319" },
1660 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20371" },
1661 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20375" },
1662 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20376" },
1663 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20377" },
1664 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20378" },
1665 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "Promise PDC20379" },
1666 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20571" },
1667 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20575" },
1668 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20579" },
1669 { ATA_PDC20580, 0, PRMIO, PRCMBO2, ATA_SA150, "Promise PDC20580" },
1670 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20617" },
1671 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20618" },
1672 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20619" },
1673 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "Promise PDC20620" },
1674 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "Promise PDC20621" },
1675 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "Promise PDC20622" },
1676 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40518" },
1677 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "Promise PDC40519" },
1678 { 0, 0, 0, 0, 0, 0}};
1680 uintptr_t devid = 0;
1682 if (!(idx = ata_match_chip(dev, ids)))
1685 /* if we are on a SuperTrak SX6000 dont attach */
1686 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
1687 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
1688 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
1689 devid == ATA_I960RM)
1692 strcpy(buffer, idx->text);
1694 /* if we are on a FastTrak TX4, adjust the interrupt resource */
1695 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
1696 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
1697 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
1698 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
1699 static long start = 0, end = 0;
1701 if (pci_get_slot(dev) == 1) {
1702 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
1703 strcat(buffer, " (channel 0+1)");
1705 else if (pci_get_slot(dev) == 2 && start && end) {
1706 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
1708 strcat(buffer, " (channel 2+3)");
1714 sprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
1715 device_set_desc_copy(dev, buffer);
1717 ctlr->chipinit = ata_promise_chipinit;
1722 ata_promise_chipinit(device_t dev)
1724 struct ata_pci_controller *ctlr = device_get_softc(dev);
1725 int rid = ATA_IRQ_RID;
1727 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1728 RF_SHAREABLE | RF_ACTIVE))) {
1729 device_printf(dev, "unable to map interrupt\n");
1733 if (ctlr->chip->max_dma >= ATA_SA150)
1734 ctlr->setmode = ata_sata_setmode;
1736 ctlr->setmode = ata_promise_setmode;
1738 switch (ctlr->chip->cfg1) {
1741 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
1743 ctlr->dmainit = ata_promise_new_dmainit;
1747 /* enable burst mode */
1748 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
1750 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1751 ata_promise_old_intr, ctlr, &ctlr->handle))) {
1752 device_printf(dev, "unable to setup interrupt\n");
1758 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1759 ata_promise_tx2_intr, ctlr, &ctlr->handle))) {
1760 device_printf(dev, "unable to setup interrupt\n");
1766 // if (ctlr->r_res1)
1767 // bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
1768 ctlr->r_type1 = SYS_RES_MEMORY;
1769 ctlr->r_rid1 = PCIR_BAR(4);
1770 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
1771 &ctlr->r_rid1, RF_ACTIVE)))
1774 ctlr->r_type2 = SYS_RES_MEMORY;
1775 ctlr->r_rid2 = PCIR_BAR(3);
1776 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1777 &ctlr->r_rid2, RF_ACTIVE))){
1778 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
1781 ctlr->reset = ata_promise_mio_reset;
1782 ctlr->dmainit = ata_promise_mio_dmainit;
1783 ctlr->allocate = ata_promise_mio_allocate;
1785 if (ctlr->chip->cfg2 == PRSX4X) {
1786 struct ata_promise_sx4 *hpkt;
1787 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
1789 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1790 ata_promise_sx4_intr, ctlr, &ctlr->handle))) {
1791 device_printf(dev, "unable to setup interrupt\n");
1792 /* XXX SOS release resources */
1796 /* print info about cache memory */
1797 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
1798 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
1799 ((dimm >> 24) & 0xff),
1800 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
1801 " ECC enabled" : "" );
1803 /* adjust cache memory parameters */
1804 ATA_OUTL(ctlr->r_res2, 0x000c000c,
1805 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
1807 /* setup host packet controls */
1808 hpkt = malloc(sizeof(struct ata_promise_sx4),
1809 M_TEMP, M_NOWAIT | M_ZERO);
1810 mtx_init(&hpkt->mtx, "ATA promise HPKT lock", NULL, MTX_DEF);
1811 TAILQ_INIT(&hpkt->queue);
1812 hpkt->busy = 0; //hpkt->head = hpkt->tail = 0;
1813 device_set_ivars(dev, hpkt);
1818 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
1819 ata_promise_mio_intr, ctlr, &ctlr->handle))) {
1820 device_printf(dev, "unable to setup interrupt\n");
1821 /* XXX SOS release resources */
1825 switch (ctlr->chip->cfg2) {
1827 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
1828 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
1832 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
1833 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 3;
1837 ATA_OUTL(ctlr->r_res2, 0x06c, 0x000000ff);
1842 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
1847 ATA_OUTL(ctlr->r_res2, 0x060, 0x000000ff);
1852 /* XXX SOS release resources */
1856 /* XXX SOS release resources */
1863 ata_promise_mio_allocate(device_t dev)
1865 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1866 struct ata_channel *ch = device_get_softc(dev);
1867 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
1870 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
1871 ch->r_io[i].res = ctlr->r_res2;
1872 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
1874 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
1875 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
1876 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
1877 ata_default_registers(dev);
1878 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
1879 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
1880 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
1881 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
1882 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
1883 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
1884 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
1885 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
1886 ch->flags |= ATA_NO_SLAVE;
1888 ch->flags |= ATA_USE_16BIT;
1890 ata_generic_hw(dev);
1892 ch->hw.command = ata_promise_sx4_command;
1894 ch->hw.command = ata_promise_mio_command;
1899 ata_promise_mio_intr(void *data)
1901 struct ata_pci_controller *ctlr = data;
1902 struct ata_channel *ch;
1903 u_int32_t vector = 0, status = 0;
1906 switch (ctlr->chip->cfg2) {
1909 /* read and acknowledge interrupt(s) */
1910 vector = ATA_INL(ctlr->r_res2, 0x040);
1912 /* read and clear interface status */
1913 status = ATA_INL(ctlr->r_res2, 0x06c);
1914 ATA_OUTL(ctlr->r_res2, 0x06c, status & 0x000000ff);
1919 /* read and acknowledge interrupt(s) */
1920 vector = ATA_INL(ctlr->r_res2, 0x040);
1921 ATA_OUTL(ctlr->r_res2, 0x040, vector & 0x0000ffff);
1923 /* read and clear interface status */
1924 status = ATA_INL(ctlr->r_res2, 0x060);
1925 ATA_OUTL(ctlr->r_res2, 0x060, status & 0x000000ff);
1929 for (unit = 0; unit < ctlr->channels; unit++) {
1931 if ((ch = ctlr->interrupt[unit].argument)) {
1932 struct ata_connect_task *tp;
1934 /* check for and handle disconnect events */
1935 if ((status & (0x00000001 << unit)) &&
1936 (tp = (struct ata_connect_task *)
1937 malloc(sizeof(struct ata_connect_task),
1938 M_ATA, M_NOWAIT | M_ZERO))) {
1941 device_printf(ch->dev, "DISCONNECT requested\n");
1942 tp->action = ATA_C_DETACH;
1944 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1945 taskqueue_enqueue(taskqueue_thread, &tp->task);
1948 /* check for and handle connect events */
1949 if ((status & (0x00000010 << unit)) &&
1950 (tp = (struct ata_connect_task *)
1951 malloc(sizeof(struct ata_connect_task),
1952 M_ATA, M_NOWAIT | M_ZERO))) {
1955 device_printf(ch->dev, "CONNECT requested\n");
1956 tp->action = ATA_C_ATTACH;
1958 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
1959 taskqueue_enqueue(taskqueue_thread, &tp->task);
1962 /* active interrupt(s) need to call the interrupt handler */
1963 if (vector & (1 << (unit + 1)))
1964 if ((ch = ctlr->interrupt[unit].argument))
1965 ctlr->interrupt[unit].function(ch);
1971 ata_promise_sx4_intr(void *data)
1973 struct ata_pci_controller *ctlr = data;
1974 struct ata_channel *ch;
1975 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
1978 for (unit = 0; unit < ctlr->channels; unit++) {
1979 if (vector & (1 << (unit + 1)))
1980 if ((ch = ctlr->interrupt[unit].argument))
1981 ctlr->interrupt[unit].function(ch);
1982 if (vector & (1 << (unit + 5)))
1983 if ((ch = ctlr->interrupt[unit].argument))
1984 ata_promise_queue_hpkt(ctlr,
1985 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
1986 ATA_PDC_HPKT_OFFSET));
1987 if (vector & (1 << (unit + 9))) {
1988 ata_promise_next_hpkt(ctlr);
1989 if ((ch = ctlr->interrupt[unit].argument))
1990 ctlr->interrupt[unit].function(ch);
1992 if (vector & (1 << (unit + 13))) {
1993 ata_promise_next_hpkt(ctlr);
1994 if ((ch = ctlr->interrupt[unit].argument))
1995 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
1996 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
1997 ATA_PDC_APKT_OFFSET));
2003 ata_promise_mio_dmainit(device_t dev)
2005 struct ata_channel *ch = device_get_softc(dev);
2009 /* note start and stop are not used here */
2014 ata_promise_mio_reset(device_t dev)
2016 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2017 struct ata_channel *ch = device_get_softc(dev);
2018 struct ata_promise_sx4 *hpktp;
2020 switch (ctlr->chip->cfg2) {
2023 /* softreset channel ATA module */
2024 hpktp = device_get_ivars(ctlr->dev);
2025 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
2027 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
2028 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
2029 ~0x00003f9f) | (ch->unit + 1));
2031 /* softreset HOST module XXX SOS what about other outstandings */
2032 mtx_lock(&hpktp->mtx);
2033 ATA_OUTL(ctlr->r_res2, 0xc012c,
2034 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
2036 ATA_OUTL(ctlr->r_res2, 0xc012c,
2037 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
2039 mtx_unlock(&hpktp->mtx);
2040 ata_generic_reset(dev);
2045 if ((ctlr->chip->cfg2 == PRSATA) ||
2046 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2048 /* mask plug/unplug intr */
2049 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
2052 /* softreset channels ATA module */
2053 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2055 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2056 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2057 ~0x00003f9f) | (ch->unit + 1));
2059 if ((ctlr->chip->cfg2 == PRSATA) ||
2060 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
2062 ata_sata_phy_enable(ch);
2064 /* reset and enable plug/unplug intr */
2065 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
2068 ata_generic_reset(dev);
2073 if ((ctlr->chip->cfg2 == PRSATA2) ||
2074 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2075 /* set portmultiplier port */
2076 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
2078 /* mask plug/unplug intr */
2079 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
2082 /* softreset channels ATA module */
2083 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
2085 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
2086 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
2087 ~0x00003f9f) | (ch->unit + 1));
2089 if ((ctlr->chip->cfg2 == PRSATA2) ||
2090 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
2092 /* set PHY mode to "improved" */
2093 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
2094 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
2095 ~0x00000003) | 0x00000001);
2097 ata_sata_phy_enable(ch);
2099 /* reset and enable plug/unplug intr */
2100 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
2102 /* set portmultiplier port */
2103 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
2106 ata_generic_reset(dev);
2113 ata_promise_mio_command(struct ata_request *request)
2115 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2116 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2117 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
2119 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
2121 /* XXX SOS add ATAPI commands support later */
2122 switch (request->u.ata.command) {
2124 return ata_generic_command(request);
2127 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
2131 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
2134 wordp[1] = htole32(ch->dma->sg_bus);
2136 ata_promise_apkt((u_int8_t*)wordp, request);
2138 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
2143 ata_promise_sx4_command(struct ata_request *request)
2145 device_t gparent = GRANDPARENT(request->dev);
2146 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2147 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2148 struct ata_dma_prdentry *prd = ch->dma->sg;
2149 caddr_t window = rman_get_virtual(ctlr->r_res1);
2151 int i, idx, length = 0;
2153 /* XXX SOS add ATAPI commands support later */
2154 switch (request->u.ata.command) {
2159 case ATA_ATA_IDENTIFY:
2164 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2165 return ata_generic_command(request);
2167 case ATA_SETFEATURES:
2168 case ATA_FLUSHCACHE:
2171 wordp = (u_int32_t *)
2172 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2173 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
2176 ata_promise_apkt((u_int8_t *)wordp, request);
2177 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2178 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
2179 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2180 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
2185 wordp = (u_int32_t *)
2186 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
2189 wordp[idx++] = prd[i].addr;
2190 wordp[idx++] = prd[i].count;
2191 length += (prd[i].count & ~ATA_DMA_EOT);
2192 } while (!(prd[i++].count & ATA_DMA_EOT));
2194 wordp = (u_int32_t *)
2195 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
2196 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2197 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
2199 wordp = (u_int32_t *)
2200 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
2201 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
2202 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
2204 wordp = (u_int32_t *)
2205 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
2206 if (request->flags & ATA_R_READ)
2207 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
2208 if (request->flags & ATA_R_WRITE)
2209 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
2210 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
2211 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
2214 wordp = (u_int32_t *)
2215 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
2216 if (request->flags & ATA_R_READ)
2217 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
2218 if (request->flags & ATA_R_WRITE)
2219 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
2220 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
2222 ata_promise_apkt((u_int8_t *)wordp, request);
2223 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
2225 if (request->flags & ATA_R_READ) {
2226 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
2227 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
2228 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
2229 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
2231 if (request->flags & ATA_R_WRITE) {
2232 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
2233 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
2234 ata_promise_queue_hpkt(ctlr,
2235 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
2242 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
2244 struct ata_device *atadev = device_get_softc(request->dev);
2248 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
2249 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
2250 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
2251 bytep[i++] = ATA_A_4BIT;
2253 command = ata_modify_if_48bit(request);
2255 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2256 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
2257 bytep[i++] = request->u.ata.feature >> 8;
2258 bytep[i++] = request->u.ata.feature;
2259 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
2260 bytep[i++] = request->u.ata.count >> 8;
2261 bytep[i++] = request->u.ata.count;
2262 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
2263 bytep[i++] = request->u.ata.lba >> 24;
2264 bytep[i++] = request->u.ata.lba;
2265 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
2266 bytep[i++] = request->u.ata.lba >> 32;
2267 bytep[i++] = request->u.ata.lba >> 8;
2268 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
2269 bytep[i++] = request->u.ata.lba >> 40;
2270 bytep[i++] = request->u.ata.lba >> 16;
2271 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
2272 bytep[i++] = ATA_D_LBA | atadev->unit;
2275 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
2276 bytep[i++] = request->u.ata.feature;
2277 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
2278 bytep[i++] = request->u.ata.count;
2279 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
2280 bytep[i++] = request->u.ata.lba;
2281 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
2282 bytep[i++] = request->u.ata.lba >> 8;
2283 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
2284 bytep[i++] = request->u.ata.lba >> 16;
2285 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
2286 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
2287 ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
2289 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
2290 bytep[i++] = command;
2295 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
2297 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
2299 mtx_lock(&hpktp->mtx);
2301 struct host_packet *hp =
2302 malloc(sizeof(struct host_packet), M_TEMP, M_NOWAIT | M_ZERO);
2304 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
2308 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
2310 mtx_unlock(&hpktp->mtx);
2314 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
2316 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
2317 struct host_packet *hp;
2319 mtx_lock(&hpktp->mtx);
2320 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
2321 TAILQ_REMOVE(&hpktp->queue, hp, chain);
2322 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
2327 mtx_unlock(&hpktp->mtx);
2331 ata_promise_tx2_intr(void *data)
2333 struct ata_pci_controller *ctlr = data;
2334 struct ata_channel *ch;
2337 /* implement this as a toggle instead to balance load XXX */
2338 for (unit = 0; unit < ctlr->channels; unit++) {
2339 if (!(ch = ctlr->interrupt[unit].argument))
2341 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
2342 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
2343 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2344 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2346 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2347 ATA_BMSTAT_INTERRUPT)
2349 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2352 ctlr->interrupt[unit].function(ch);
2358 ata_promise_old_intr(void *data)
2360 struct ata_pci_controller *ctlr = data;
2361 struct ata_channel *ch;
2364 /* implement this as a toggle instead to balance load XXX */
2365 for (unit = 0; unit < ctlr->channels; unit++) {
2366 if (!(ch = ctlr->interrupt[unit].argument))
2368 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)){
2369 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2370 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2372 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2373 ATA_BMSTAT_INTERRUPT)
2375 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2378 ctlr->interrupt[unit].function(ch);
2384 ata_promise_new_dmastart(device_t dev)
2386 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2387 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2388 struct ata_device *atadev = device_get_softc(dev);
2390 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2391 ATA_OUTB(ctlr->r_res1, 0x11,
2392 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
2393 ATA_OUTL(ctlr->r_res1, 0x20,
2394 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
2395 (ch->dma->cur_iosize >> 1));
2397 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
2398 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
2399 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
2400 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2401 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
2402 ATA_BMCMD_START_STOP);
2403 ch->flags |= ATA_DMA_ACTIVE;
2408 ata_promise_new_dmastop(device_t dev)
2410 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2411 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2412 struct ata_device *atadev = device_get_softc(dev);
2415 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
2416 ATA_OUTB(ctlr->r_res1, 0x11,
2417 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
2418 ATA_OUTL(ctlr->r_res1, 0x20, 0);
2420 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
2421 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2422 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
2423 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
2424 ch->flags &= ~ATA_DMA_ACTIVE;
2429 ata_promise_new_dmareset(device_t dev)
2431 struct ata_channel *ch = device_get_softc(dev);
2433 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
2434 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
2435 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
2436 ch->flags &= ~ATA_DMA_ACTIVE;
2440 ata_promise_new_dmainit(device_t dev)
2442 struct ata_channel *ch = device_get_softc(dev);
2446 ch->dma->start = ata_promise_new_dmastart;
2447 ch->dma->stop = ata_promise_new_dmastop;
2448 ch->dma->reset = ata_promise_new_dmareset;
2453 ata_promise_setmode(device_t dev, int mode)
2455 device_t gparent = GRANDPARENT(dev);
2456 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2457 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2458 struct ata_device *atadev = device_get_softc(dev);
2459 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2461 u_int32_t timings33[][2] = {
2462 /* PROLD PRNEW mode */
2463 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
2464 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
2465 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
2466 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
2467 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
2468 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
2469 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
2470 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
2471 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
2472 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
2473 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
2474 { 0, 0x00424ef6 }, /* UDMA 3 */
2475 { 0, 0x004127f3 }, /* UDMA 4 */
2476 { 0, 0x004127f3 } /* UDMA 5 */
2479 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2481 switch (ctlr->chip->cfg1) {
2484 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
2485 (ch->unit ? 1 << 11 : 1 << 10))) {
2486 ata_print_cable(dev, "controller");
2489 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
2490 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
2494 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
2495 if (mode > ATA_UDMA2 &&
2496 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
2497 ata_print_cable(dev, "controller");
2503 if (mode > ATA_UDMA2 &&
2504 (ATA_INL(ctlr->r_res2,
2505 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
2506 (ch->unit << 7)) & 0x01000000)) {
2507 ata_print_cable(dev, "controller");
2513 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2516 device_printf(dev, "%ssetting %s on %s chip\n",
2517 (error) ? "FAILURE " : "",
2518 ata_mode2str(mode), ctlr->chip->text);
2520 if (ctlr->chip->cfg1 < PRTX)
2521 pci_write_config(gparent, 0x60 + (devno << 2),
2522 timings33[ctlr->chip->cfg1][ata_mode2idx(mode)],4);
2523 atadev->mode = mode;
2530 * ServerWorks chipset support functions
2533 ata_serverworks_ident(device_t dev)
2535 struct ata_pci_controller *ctlr = device_get_softc(dev);
2536 struct ata_chip_id *idx;
2537 static struct ata_chip_id ids[] =
2538 {{ ATA_ROSB4, 0x00, SWKS33, 0x00, ATA_UDMA2, "ServerWorks ROSB4" },
2539 { ATA_CSB5, 0x92, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB5" },
2540 { ATA_CSB5, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB5" },
2541 { ATA_CSB6, 0x00, SWKS100, 0x00, ATA_UDMA5, "ServerWorks CSB6" },
2542 { ATA_CSB6_1, 0x00, SWKS66, 0x00, ATA_UDMA4, "ServerWorks CSB6" },
2543 { 0, 0, 0, 0, 0, 0}};
2546 if (!(idx = ata_match_chip(dev, ids)))
2549 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
2550 device_set_desc_copy(dev, buffer);
2552 ctlr->chipinit = ata_serverworks_chipinit;
2557 ata_serverworks_chipinit(device_t dev)
2559 struct ata_pci_controller *ctlr = device_get_softc(dev);
2561 if (ata_setup_interrupt(dev))
2564 if (ctlr->chip->cfg1 == SWKS33) {
2568 /* locate the ISA part in the southbridge and enable UDMA33 */
2569 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
2570 for (i = 0; i < nchildren; i++) {
2571 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
2572 pci_write_config(children[i], 0x64,
2573 (pci_read_config(children[i], 0x64, 4) &
2574 ~0x00002000) | 0x00004000, 4);
2578 free(children, M_TEMP);
2582 pci_write_config(dev, 0x5a,
2583 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
2584 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
2586 ctlr->setmode = ata_serverworks_setmode;
2591 ata_serverworks_setmode(device_t dev, int mode)
2593 device_t gparent = GRANDPARENT(dev);
2594 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2595 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2596 struct ata_device *atadev = device_get_softc(dev);
2597 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2598 int offset = (devno ^ 0x01) << 3;
2600 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
2601 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
2602 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
2604 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2606 mode = ata_check_80pin(dev, mode);
2608 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2611 device_printf(dev, "%ssetting %s on %s chip\n",
2612 (error) ? "FAILURE " : "",
2613 ata_mode2str(mode), ctlr->chip->text);
2615 if (mode >= ATA_UDMA0) {
2616 pci_write_config(gparent, 0x56,
2617 (pci_read_config(gparent, 0x56, 2) &
2618 ~(0xf << (devno << 2))) |
2619 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
2620 pci_write_config(gparent, 0x54,
2621 pci_read_config(gparent, 0x54, 1) |
2622 (0x01 << devno), 1);
2623 pci_write_config(gparent, 0x44,
2624 (pci_read_config(gparent, 0x44, 4) &
2625 ~(0xff << offset)) |
2626 (dmatimings[2] << offset), 4);
2628 else if (mode >= ATA_WDMA0) {
2629 pci_write_config(gparent, 0x54,
2630 pci_read_config(gparent, 0x54, 1) &
2631 ~(0x01 << devno), 1);
2632 pci_write_config(gparent, 0x44,
2633 (pci_read_config(gparent, 0x44, 4) &
2634 ~(0xff << offset)) |
2635 (dmatimings[mode & ATA_MODE_MASK] << offset),4);
2638 pci_write_config(gparent, 0x54,
2639 pci_read_config(gparent, 0x54, 1) &
2640 ~(0x01 << devno), 1);
2642 pci_write_config(gparent, 0x40,
2643 (pci_read_config(gparent, 0x40, 4) &
2644 ~(0xff << offset)) |
2645 (piotimings[ata_mode2idx(mode)] << offset), 4);
2646 atadev->mode = mode;
2652 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
2655 ata_sii_ident(device_t dev)
2657 struct ata_pci_controller *ctlr = device_get_softc(dev);
2658 struct ata_chip_id *idx;
2659 static struct ata_chip_id ids[] =
2660 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
2661 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
2662 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
2663 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
2664 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
2665 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
2666 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
2667 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
2668 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
2669 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
2670 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
2671 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
2672 { 0, 0, 0, 0, 0, 0}};
2675 if (!(idx = ata_match_chip(dev, ids)))
2678 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
2679 device_set_desc_copy(dev, buffer);
2681 ctlr->chipinit = ata_sii_chipinit;
2686 ata_sii_chipinit(device_t dev)
2688 struct ata_pci_controller *ctlr = device_get_softc(dev);
2689 int rid = ATA_IRQ_RID;
2691 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2692 RF_SHAREABLE | RF_ACTIVE))) {
2693 device_printf(dev, "unable to map interrupt\n");
2697 if (ctlr->chip->cfg1 == SIIMEMIO) {
2698 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2699 ata_sii_intr, ctlr, &ctlr->handle))) {
2700 device_printf(dev, "unable to setup interrupt\n");
2704 ctlr->r_type2 = SYS_RES_MEMORY;
2705 ctlr->r_rid2 = PCIR_BAR(5);
2706 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2707 &ctlr->r_rid2, RF_ACTIVE)))
2710 if (ctlr->chip->cfg2 & SIISETCLK) {
2711 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
2712 pci_write_config(dev, 0x8a,
2713 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
2714 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
2715 device_printf(dev, "%s could not set ATA133 clock\n",
2719 /* if we have 4 channels enable the second set */
2720 if (ctlr->chip->cfg2 & SII4CH) {
2721 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
2725 /* enable PCI interrupt as BIOS might not */
2726 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
2728 /* dont block interrupts from any channel */
2729 pci_write_config(dev, 0x48,
2730 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
2732 ctlr->allocate = ata_sii_allocate;
2733 if (ctlr->chip->max_dma >= ATA_SA150) {
2734 ctlr->reset = ata_sii_reset;
2735 ctlr->setmode = ata_sata_setmode;
2738 ctlr->setmode = ata_sii_setmode;
2741 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
2742 ctlr->chip->cfg2 & SIIINTR ?
2743 ata_cmd_intr : ata_cmd_old_intr,
2744 ctlr, &ctlr->handle))) {
2745 device_printf(dev, "unable to setup interrupt\n");
2749 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
2750 device_printf(dev, "HW has secondary channel disabled\n");
2754 /* enable interrupt as BIOS might not */
2755 pci_write_config(dev, 0x71, 0x01, 1);
2757 ctlr->setmode = ata_cmd_setmode;
2763 ata_sii_allocate(device_t dev)
2765 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2766 struct ata_channel *ch = device_get_softc(dev);
2767 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
2770 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2771 ch->r_io[i].res = ctlr->r_res2;
2772 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
2774 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
2775 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
2776 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
2777 ata_default_registers(dev);
2778 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
2779 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
2780 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
2781 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
2782 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
2783 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
2784 ch->r_io[ATA_BMDEVSPEC_0].res = ctlr->r_res2;
2785 ch->r_io[ATA_BMDEVSPEC_0].offset = 0xa1 + (unit01 << 6) + (unit10 << 8);
2787 if (ctlr->chip->max_dma >= ATA_SA150) {
2788 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
2789 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
2790 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
2791 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
2792 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
2793 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
2794 ch->flags |= ATA_NO_SLAVE;
2796 /* enable PHY state change interrupt */
2797 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
2800 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
2801 /* work around errata in early chips */
2802 ch->dma->boundary = 16 * DEV_BSIZE;
2803 ch->dma->max_iosize = 15 * DEV_BSIZE;
2806 ata_generic_hw(dev);
2811 ata_sii_intr(void *data)
2813 struct ata_pci_controller *ctlr = data;
2814 struct ata_channel *ch;
2817 /* implement this as a toggle instead to balance load XXX */
2818 for (unit = 0; unit < ctlr->channels; unit++) {
2819 if (!(ch = ctlr->interrupt[unit].argument))
2822 /* check for PHY related interrupts on SATA capable HW */
2823 if (ctlr->chip->max_dma >= ATA_SA150) {
2824 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
2825 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
2826 struct ata_connect_task *tp;
2829 /* clear error bits/interrupt */
2830 ATA_IDX_OUTL(ch, ATA_SERROR, error);
2832 /* if we have a connection event deal with it */
2833 if ((error & ATA_SE_PHY_CHANGED) &&
2834 (tp = (struct ata_connect_task *)
2835 malloc(sizeof(struct ata_connect_task),
2836 M_ATA, M_NOWAIT | M_ZERO))) {
2838 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) {
2839 device_printf(ch->dev, "CONNECT requested\n");
2840 tp->action = ATA_C_ATTACH;
2843 device_printf(ch->dev, "DISCONNECT requested\n");
2844 tp->action = ATA_C_DETACH;
2847 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
2848 taskqueue_enqueue(taskqueue_thread, &tp->task);
2853 /* any drive action to take care of ? */
2854 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_0) & 0x08) {
2855 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2856 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2858 if (!(bmstat & ATA_BMSTAT_INTERRUPT))
2860 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2863 ctlr->interrupt[unit].function(ch);
2870 ata_cmd_intr(void *data)
2872 struct ata_pci_controller *ctlr = data;
2873 struct ata_channel *ch;
2877 /* implement this as a toggle instead to balance load XXX */
2878 for (unit = 0; unit < ctlr->channels; unit++) {
2879 if (!(ch = ctlr->interrupt[unit].argument))
2881 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
2882 (ch->unit ? 0x08 : 0x04))) {
2883 pci_write_config(device_get_parent(ch->dev), 0x71,
2884 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
2885 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2886 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2888 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2889 ATA_BMSTAT_INTERRUPT)
2891 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2894 ctlr->interrupt[unit].function(ch);
2900 ata_cmd_old_intr(void *data)
2902 struct ata_pci_controller *ctlr = data;
2903 struct ata_channel *ch;
2906 /* implement this as a toggle instead to balance load XXX */
2907 for (unit = 0; unit < ctlr->channels; unit++) {
2908 if (!(ch = ctlr->interrupt[unit].argument))
2910 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
2911 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
2913 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
2914 ATA_BMSTAT_INTERRUPT)
2916 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
2919 ctlr->interrupt[unit].function(ch);
2924 ata_sii_reset(device_t dev)
2926 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2927 struct ata_channel *ch = device_get_softc(dev);
2928 int offset = ((ch->unit & 1) << 7) + ((ch->unit & 2) << 8);
2930 /* disable PHY state change interrupt */
2931 ATA_OUTL(ctlr->r_res2, 0x148 + offset, ~(1 << 16));
2933 ata_sata_phy_enable(ch);
2935 /* enable PHY state change interrupt */
2936 ATA_OUTL(ctlr->r_res2, 0x148 + offset, (1 << 16));
2940 ata_sii_setmode(device_t dev, int mode)
2942 device_t gparent = GRANDPARENT(dev);
2943 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2944 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2945 struct ata_device *atadev = device_get_softc(dev);
2946 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
2947 int mreg = ch->unit ? 0x84 : 0x80;
2948 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
2949 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
2952 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2954 if (ctlr->chip->cfg2 & SIISETCLK) {
2955 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
2956 (ch->unit ? 0x02 : 0x01))) {
2957 ata_print_cable(dev, "controller");
2962 mode = ata_check_80pin(dev, mode);
2964 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2967 device_printf(dev, "%ssetting %s on %s chip\n",
2968 (error) ? "FAILURE " : "",
2969 ata_mode2str(mode), ctlr->chip->text);
2973 if (mode >= ATA_UDMA0) {
2974 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
2975 u_int8_t ureg = 0xac + rego;
2977 pci_write_config(gparent, mreg,
2978 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
2979 pci_write_config(gparent, ureg,
2980 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
2981 udmatimings[mode & ATA_MODE_MASK], 1);
2984 else if (mode >= ATA_WDMA0) {
2985 u_int8_t dreg = 0xa8 + rego;
2986 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
2988 pci_write_config(gparent, mreg,
2989 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
2990 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
2994 u_int8_t preg = 0xa4 + rego;
2995 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
2997 pci_write_config(gparent, mreg,
2998 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
2999 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
3001 atadev->mode = mode;
3005 ata_cmd_setmode(device_t dev, int mode)
3007 device_t gparent = GRANDPARENT(dev);
3008 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3009 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3010 struct ata_device *atadev = device_get_softc(dev);
3011 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3014 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3016 mode = ata_check_80pin(dev, mode);
3018 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3021 device_printf(dev, "%ssetting %s on %s chip\n",
3022 (error) ? "FAILURE " : "",
3023 ata_mode2str(mode), ctlr->chip->text);
3025 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
3026 int ureg = ch->unit ? 0x7b : 0x73;
3028 if (mode >= ATA_UDMA0) {
3029 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
3030 { 0x11, 0x42 }, { 0x25, 0x8a },
3031 { 0x15, 0x4a }, { 0x05, 0x0a } };
3033 u_int8_t umode = pci_read_config(gparent, ureg, 1);
3035 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
3036 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
3037 pci_write_config(gparent, ureg, umode, 1);
3039 else if (mode >= ATA_WDMA0) {
3040 int dmatimings[] = { 0x87, 0x32, 0x3f };
3042 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
3043 pci_write_config(gparent, ureg,
3044 pci_read_config(gparent, ureg, 1) &
3045 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3048 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
3049 pci_write_config(gparent, treg,
3050 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
3051 pci_write_config(gparent, ureg,
3052 pci_read_config(gparent, ureg, 1) &
3053 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
3055 atadev->mode = mode;
3061 * Silicon Integrated Systems Corp. (SiS) chipset support functions
3064 ata_sis_ident(device_t dev)
3066 struct ata_pci_controller *ctlr = device_get_softc(dev);
3067 struct ata_chip_id *idx;
3068 static struct ata_chip_id ids[] =
3069 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "SiS 182" }, /* south */
3070 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "SiS 181" }, /* south */
3071 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "SiS 180" }, /* south */
3072 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 965" }, /* south */
3073 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 964" }, /* south */
3074 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 963" }, /* south */
3075 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "SiS 962" }, /* south */
3077 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 745" }, /* 1chip */
3078 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 735" }, /* 1chip */
3079 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 733" }, /* 1chip */
3080 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "SiS 730" }, /* 1chip */
3082 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 635" }, /* 1chip */
3083 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "SiS 633" }, /* unknown */
3084 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "SiS 630S"}, /* 1chip */
3085 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "SiS 630" }, /* 1chip */
3086 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "SiS 620" }, /* 1chip */
3088 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "SiS 550" },
3089 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "SiS 540" },
3090 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "SiS 530" },
3092 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "SiS 5513" },
3093 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "SiS 5513" },
3094 { 0, 0, 0, 0, 0, 0 }};
3098 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
3101 if (idx->cfg2 && !found) {
3102 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
3104 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
3105 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
3107 idx->cfg1 = SIS133NEW;
3108 idx->max_dma = ATA_UDMA6;
3109 sprintf(buffer, "SiS 962/963 %s controller",
3110 ata_mode2str(idx->max_dma));
3112 pci_write_config(dev, 0x57, reg57, 1);
3114 if (idx->cfg2 && !found) {
3115 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
3117 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
3118 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
3119 struct ata_chip_id id[] =
3120 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
3123 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
3124 idx->cfg1 = SIS133OLD;
3125 idx->max_dma = ATA_UDMA6;
3128 idx->cfg1 = SIS100NEW;
3129 idx->max_dma = ATA_UDMA5;
3131 sprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
3133 pci_write_config(dev, 0x4a, reg4a, 1);
3136 sprintf(buffer,"%s %s controller",idx->text,ata_mode2str(idx->max_dma));
3138 device_set_desc_copy(dev, buffer);
3140 ctlr->chipinit = ata_sis_chipinit;
3145 ata_sis_chipinit(device_t dev)
3147 struct ata_pci_controller *ctlr = device_get_softc(dev);
3149 if (ata_setup_interrupt(dev))
3152 switch (ctlr->chip->cfg1) {
3157 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
3161 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
3164 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
3165 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
3168 ctlr->r_type2 = SYS_RES_IOPORT;
3169 ctlr->r_rid2 = PCIR_BAR(5);
3170 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3171 &ctlr->r_rid2, RF_ACTIVE))) {
3172 pci_write_config(dev, PCIR_COMMAND,
3173 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3174 ctlr->allocate = ata_sis_allocate;
3175 ctlr->reset = ata_sis_reset;
3177 ctlr->setmode = ata_sata_setmode;
3182 ctlr->setmode = ata_sis_setmode;
3187 ata_sis_allocate(device_t dev)
3189 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3190 struct ata_channel *ch = device_get_softc(dev);
3192 /* setup the usual register normal pci style */
3193 ata_pci_allocate(dev);
3195 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3196 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 4);
3197 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3198 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 4);
3199 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3200 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 4);
3201 ch->flags |= ATA_NO_SLAVE;
3203 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
3204 /* XXX SOS unknown how to enable PHY state change interrupt */
3209 ata_sis_reset(device_t dev)
3211 struct ata_channel *ch = device_get_softc(dev);
3213 ata_sata_phy_enable(ch);
3218 ata_sis_setmode(device_t dev, int mode)
3220 device_t gparent = GRANDPARENT(dev);
3221 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3222 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3223 struct ata_device *atadev = device_get_softc(dev);
3224 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3227 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3229 if (ctlr->chip->cfg1 == SIS133NEW) {
3230 if (mode > ATA_UDMA2 &&
3231 pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
3232 ata_print_cable(dev, "controller");
3237 if (mode > ATA_UDMA2 &&
3238 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
3239 ata_print_cable(dev, "controller");
3244 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3247 device_printf(dev, "%ssetting %s on %s chip\n",
3248 (error) ? "FAILURE " : "",
3249 ata_mode2str(mode), ctlr->chip->text);
3251 switch (ctlr->chip->cfg1) {
3253 u_int32_t timings[] =
3254 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
3255 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
3256 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
3259 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
3260 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
3264 u_int16_t timings[] =
3265 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
3266 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
3268 u_int16_t reg = 0x40 + (devno << 1);
3270 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3274 u_int16_t timings[] =
3275 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
3276 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
3277 u_int16_t reg = 0x40 + (devno << 1);
3279 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3285 u_int16_t timings[] =
3286 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
3287 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
3288 u_int16_t reg = 0x40 + (devno << 1);
3290 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
3294 atadev->mode = mode;
3299 /* VIA Technologies Inc. chipset support functions */
3301 ata_via_ident(device_t dev)
3303 struct ata_pci_controller *ctlr = device_get_softc(dev);
3304 struct ata_chip_id *idx;
3305 static struct ata_chip_id ids[] =
3306 {{ ATA_VIA82C586, 0x02, VIA33, 0x00, ATA_UDMA2, "VIA 82C586B" },
3307 { ATA_VIA82C586, 0x00, VIA33, 0x00, ATA_WDMA2, "VIA 82C586" },
3308 { ATA_VIA82C596, 0x12, VIA66, VIACLK, ATA_UDMA4, "VIA 82C596B" },
3309 { ATA_VIA82C596, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C596" },
3310 { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "VIA 82C686B"},
3311 { ATA_VIA82C686, 0x10, VIA66, VIACLK, ATA_UDMA4, "VIA 82C686A" },
3312 { ATA_VIA82C686, 0x00, VIA33, 0x00, ATA_UDMA2, "VIA 82C686" },
3313 { ATA_VIA8231, 0x00, VIA100, VIABUG, ATA_UDMA5, "VIA 8231" },
3314 { ATA_VIA8233, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233" },
3315 { ATA_VIA8233C, 0x00, VIA100, 0x00, ATA_UDMA5, "VIA 8233C" },
3316 { ATA_VIA8233A, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8233A" },
3317 { ATA_VIA8235, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8235" },
3318 { ATA_VIA8237, 0x00, VIA133, 0x00, ATA_UDMA6, "VIA 8237" },
3319 { 0, 0, 0, 0, 0, 0 }};
3320 static struct ata_chip_id new_ids[] =
3321 {{ ATA_VIA6410, 0x00, 0, 0x00, ATA_UDMA6, "VIA 6410" },
3322 { ATA_VIA6420, 0x00, 7, 0x00, ATA_SA150, "VIA 6420" },
3323 { ATA_VIA6421, 0x00, 6, 0x00, ATA_SA150, "VIA 6421" },
3324 { 0, 0, 0, 0, 0, 0 }};
3327 if (pci_get_devid(dev) == ATA_VIA82C571) {
3328 if (!(idx = ata_find_chip(dev, ids, -99)))
3332 if (!(idx = ata_match_chip(dev, new_ids)))
3336 sprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
3337 device_set_desc_copy(dev, buffer);
3339 ctlr->chipinit = ata_via_chipinit;
3344 ata_via_chipinit(device_t dev)
3346 struct ata_pci_controller *ctlr = device_get_softc(dev);
3348 if (ata_setup_interrupt(dev))
3351 if (ctlr->chip->max_dma >= ATA_SA150) {
3352 ctlr->r_type2 = SYS_RES_IOPORT;
3353 ctlr->r_rid2 = PCIR_BAR(5);
3354 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3355 &ctlr->r_rid2, RF_ACTIVE))) {
3356 pci_write_config(dev, PCIR_COMMAND,
3357 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3358 ctlr->allocate = ata_via_allocate;
3359 ctlr->reset = ata_via_reset;
3361 ctlr->setmode = ata_sata_setmode;
3365 /* prepare for ATA-66 on the 82C686a and 82C596b */
3366 if (ctlr->chip->cfg2 & VIACLK)
3367 pci_write_config(dev, 0x50, 0x030b030b, 4);
3369 /* the southbridge might need the data corruption fix */
3370 if (ctlr->chip->cfg2 & VIABUG)
3371 ata_via_southbridge_fixup(dev);
3373 /* set fifo configuration half'n'half */
3374 pci_write_config(dev, 0x43,
3375 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
3377 /* set status register read retry */
3378 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
3380 /* set DMA read & end-of-sector fifo flush */
3381 pci_write_config(dev, 0x46,
3382 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
3384 /* set sector size */
3385 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
3386 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
3388 ctlr->setmode = ata_via_family_setmode;
3393 ata_via_allocate(device_t dev)
3395 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3396 struct ata_channel *ch = device_get_softc(dev);
3398 /* setup the usual register normal pci style */
3399 ata_pci_allocate(dev);
3401 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3402 ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
3403 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3404 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
3405 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3406 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
3407 ch->flags |= ATA_NO_SLAVE;
3409 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
3410 /* XXX SOS unknown how to enable PHY state change interrupt */
3415 ata_via_reset(device_t dev)
3417 struct ata_channel *ch = device_get_softc(dev);
3419 ata_sata_phy_enable(ch);
3423 ata_via_southbridge_fixup(device_t dev)
3428 if (device_get_children(device_get_parent(dev), &children, &nchildren))
3431 for (i = 0; i < nchildren; i++) {
3432 if (pci_get_devid(children[i]) == ATA_VIA8363 ||
3433 pci_get_devid(children[i]) == ATA_VIA8371 ||
3434 pci_get_devid(children[i]) == ATA_VIA8662 ||
3435 pci_get_devid(children[i]) == ATA_VIA8361) {
3436 u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
3438 if ((reg76 & 0xf0) != 0xd0) {
3440 "Correcting VIA config for southbridge data corruption bug\n");
3441 pci_write_config(children[i], 0x75, 0x80, 1);
3442 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
3447 free(children, M_TEMP);
3451 /* common code for VIA, AMD & nVidia */
3453 ata_via_family_setmode(device_t dev, int mode)
3455 device_t gparent = GRANDPARENT(dev);
3456 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3457 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3458 struct ata_device *atadev = device_get_softc(dev);
3459 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
3460 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
3462 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* VIA ATA33 */
3463 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* VIA ATA66 */
3464 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* VIA ATA100 */
3465 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
3466 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }}; /* AMD/nVIDIA */
3467 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3468 int reg = 0x53 - devno;
3471 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3473 if (ctlr->chip->cfg2 & AMDCABLE) {
3474 if (mode > ATA_UDMA2 &&
3475 !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
3476 ata_print_cable(dev, "controller");
3481 mode = ata_check_80pin(dev, mode);
3483 if (ctlr->chip->cfg2 & NVIDIA)
3486 if (ctlr->chip->cfg1 != VIA133)
3487 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
3489 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3492 device_printf(dev, "%ssetting %s on %s chip\n",
3493 (error) ? "FAILURE " : "", ata_mode2str(mode),
3496 if (mode >= ATA_UDMA0)
3497 pci_write_config(gparent, reg,
3498 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
3500 pci_write_config(gparent, reg, 0x8b, 1);
3501 atadev->mode = mode;
3506 /* misc functions */
3507 struct ata_chip_id *
3508 ata_match_chip(device_t dev, struct ata_chip_id *index)
3510 while (index->chipid != 0) {
3511 if (pci_get_devid(dev) == index->chipid &&
3512 pci_get_revid(dev) >= index->chiprev)
3519 static struct ata_chip_id *
3520 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
3525 if (device_get_children(device_get_parent(dev), &children, &nchildren))
3528 while (index->chipid != 0) {
3529 for (i = 0; i < nchildren; i++) {
3530 if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
3531 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
3532 pci_get_devid(children[i]) == index->chipid &&
3533 pci_get_revid(children[i]) >= index->chiprev) {
3534 free(children, M_TEMP);
3540 free(children, M_TEMP);
3545 ata_setup_interrupt(device_t dev)
3547 struct ata_pci_controller *ctlr = device_get_softc(dev);
3548 int rid = ATA_IRQ_RID;
3550 if (!ata_legacy(dev)) {
3551 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3552 RF_SHAREABLE | RF_ACTIVE))) {
3553 device_printf(dev, "unable to map interrupt\n");
3556 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3557 ata_generic_intr, ctlr, &ctlr->handle))) {
3558 device_printf(dev, "unable to setup interrupt\n");
3565 struct ata_serialize {
3566 struct mtx locked_mtx;
3572 ata_serialize(device_t dev, int flags)
3574 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3575 struct ata_channel *ch = device_get_softc(dev);
3576 struct ata_serialize *serial;
3577 static int inited = 0;
3581 serial = malloc(sizeof(struct ata_serialize),
3582 M_TEMP, M_NOWAIT | M_ZERO);
3583 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
3584 serial->locked_ch = -1;
3585 serial->restart_ch = -1;
3586 device_set_ivars(ctlr->dev, serial);
3590 serial = device_get_ivars(ctlr->dev);
3592 mtx_lock(&serial->locked_mtx);
3595 if (serial->locked_ch == -1)
3596 serial->locked_ch = ch->unit;
3597 if (serial->locked_ch != ch->unit)
3598 serial->restart_ch = ch->unit;
3602 if (serial->locked_ch == ch->unit) {
3603 serial->locked_ch = -1;
3604 if (serial->restart_ch != -1) {
3605 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
3606 serial->restart_ch = -1;
3607 mtx_unlock(&serial->locked_mtx);
3618 res = serial->locked_ch;
3619 mtx_unlock(&serial->locked_mtx);
3624 ata_print_cable(device_t dev, u_int8_t *who)
3627 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
3631 ata_atapi(device_t dev)
3633 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3634 struct ata_device *atadev = device_get_softc(dev);
3636 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
3637 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
3641 ata_check_80pin(device_t dev, int mode)
3643 struct ata_device *atadev = device_get_softc(dev);
3645 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
3646 ata_print_cable(dev, "device");
3653 ata_mode2idx(int mode)
3655 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
3656 return (mode & ATA_MODE_MASK) + 8;
3657 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
3658 return (mode & ATA_MODE_MASK) + 5;
3659 return (mode & ATA_MODE_MASK) - ATA_PIO0;