2 * Copyright (c) 1998 - 2006 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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/sys/dev/ata/ata-chipset.c,v 1.196 2007/04/08 19:18:51 sos Exp $
27 * $DragonFly: src/sys/dev/disk/nata/ata-chipset.c,v 1.9 2007/10/19 11:53:14 tgen Exp $
32 #include <sys/param.h>
34 #include <sys/bus_dma.h>
35 #include <sys/bus_resource.h>
36 #include <sys/callout.h>
37 #include <sys/endian.h>
38 #include <sys/libkern.h>
39 #include <sys/lock.h> /* for {get,rel}_mplock() */
40 #include <sys/malloc.h>
42 #include <sys/queue.h>
44 #include <sys/spinlock.h>
45 #include <sys/spinlock2.h>
46 #include <sys/systm.h>
47 #include <sys/taskqueue.h>
49 #include <machine/bus_dma.h>
51 #include <bus/pci/pcireg.h>
52 #include <bus/pci/pcivar.h>
58 /* local prototypes */
60 static int ata_generic_chipinit(device_t dev);
61 static void ata_generic_intr(void *data);
62 static void ata_generic_setmode(device_t dev, int mode);
63 static void ata_sata_phy_check_events(device_t dev);
64 static void ata_sata_phy_event(void *context, int dummy);
65 static int ata_sata_phy_reset(device_t dev);
66 static int ata_sata_connect(struct ata_channel *ch);
67 static void ata_sata_setmode(device_t dev, int mode);
68 static int ata_request2fis_h2d(struct ata_request *request, u_int8_t *fis);
69 static int ata_ahci_chipinit(device_t dev);
70 static int ata_ahci_allocate(device_t dev);
71 static int ata_ahci_status(device_t dev);
72 static int ata_ahci_begin_transaction(struct ata_request *request);
73 static int ata_ahci_end_transaction(struct ata_request *request);
74 static void ata_ahci_reset(device_t dev);
75 static void ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
76 static void ata_ahci_dmainit(device_t dev);
77 static int ata_ahci_setup_fis(struct ata_ahci_cmd_tab *ctp, struct ata_request *request);
78 static int ata_genahci_chipinit(device_t dev);
79 static int ata_acard_chipinit(device_t dev);
80 static int ata_acard_allocate(device_t dev);
81 static int ata_acard_status(device_t dev);
82 static void ata_acard_850_setmode(device_t dev, int mode);
83 static void ata_acard_86X_setmode(device_t dev, int mode);
84 static int ata_ali_chipinit(device_t dev);
85 static int ata_ali_allocate(device_t dev);
86 static int ata_ali_sata_allocate(device_t dev);
87 static void ata_ali_reset(device_t dev);
88 static void ata_ali_setmode(device_t dev, int mode);
89 static int ata_amd_chipinit(device_t dev);
90 static int ata_ati_chipinit(device_t dev);
91 static void ata_ati_setmode(device_t dev, int mode);
92 static int ata_cyrix_chipinit(device_t dev);
93 static void ata_cyrix_setmode(device_t dev, int mode);
94 static int ata_cypress_chipinit(device_t dev);
95 static void ata_cypress_setmode(device_t dev, int mode);
96 static int ata_highpoint_chipinit(device_t dev);
97 static int ata_highpoint_allocate(device_t dev);
98 static void ata_highpoint_setmode(device_t dev, int mode);
99 static int ata_highpoint_check_80pin(device_t dev, int mode);
100 static int ata_intel_chipinit(device_t dev);
101 static int ata_intel_allocate(device_t dev);
102 static void ata_intel_reset(device_t dev);
103 static void ata_intel_old_setmode(device_t dev, int mode);
104 static void ata_intel_new_setmode(device_t dev, int mode);
105 static int ata_intel_31244_allocate(device_t dev);
106 static int ata_intel_31244_status(device_t dev);
107 static int ata_intel_31244_command(struct ata_request *request);
108 static void ata_intel_31244_reset(device_t dev);
109 static int ata_ite_chipinit(device_t dev);
110 static void ata_ite_setmode(device_t dev, int mode);
111 static int ata_jmicron_chipinit(device_t dev);
112 static int ata_jmicron_allocate(device_t dev);
113 static void ata_jmicron_reset(device_t dev);
114 static void ata_jmicron_dmainit(device_t dev);
115 static void ata_jmicron_setmode(device_t dev, int mode);
116 static int ata_marvell_pata_chipinit(device_t dev);
117 static int ata_marvell_pata_allocate(device_t dev);
118 static void ata_marvell_pata_setmode(device_t dev, int mode);
119 static int ata_marvell_edma_chipinit(device_t dev);
120 static int ata_marvell_edma_allocate(device_t dev);
121 static int ata_marvell_edma_status(device_t dev);
122 static int ata_marvell_edma_begin_transaction(struct ata_request *request);
123 static int ata_marvell_edma_end_transaction(struct ata_request *request);
124 static void ata_marvell_edma_reset(device_t dev);
125 static void ata_marvell_edma_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
126 static void ata_marvell_edma_dmainit(device_t dev);
127 static int ata_national_chipinit(device_t dev);
128 static void ata_national_setmode(device_t dev, int mode);
129 static int ata_netcell_chipinit(device_t dev);
130 static int ata_netcell_allocate(device_t dev);
131 static int ata_nvidia_chipinit(device_t dev);
132 static int ata_nvidia_allocate(device_t dev);
133 static int ata_nvidia_status(device_t dev);
134 static void ata_nvidia_reset(device_t dev);
135 static int ata_promise_chipinit(device_t dev);
136 static int ata_promise_allocate(device_t dev);
137 static int ata_promise_status(device_t dev);
138 static int ata_promise_dmastart(device_t dev);
139 static int ata_promise_dmastop(device_t dev);
140 static void ata_promise_dmareset(device_t dev);
141 static void ata_promise_dmainit(device_t dev);
142 static void ata_promise_setmode(device_t dev, int mode);
143 static int ata_promise_tx2_allocate(device_t dev);
144 static int ata_promise_tx2_status(device_t dev);
145 static int ata_promise_mio_allocate(device_t dev);
146 static void ata_promise_mio_intr(void *data);
147 static int ata_promise_mio_status(device_t dev);
148 static int ata_promise_mio_command(struct ata_request *request);
149 static void ata_promise_mio_reset(device_t dev);
150 static void ata_promise_mio_dmainit(device_t dev);
151 static void ata_promise_mio_setmode(device_t dev, int mode);
152 static void ata_promise_sx4_intr(void *data);
153 static int ata_promise_sx4_command(struct ata_request *request);
154 static int ata_promise_apkt(u_int8_t *bytep, struct ata_request *request);
155 static void ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt);
156 static void ata_promise_next_hpkt(struct ata_pci_controller *ctlr);
157 static int ata_serverworks_chipinit(device_t dev);
158 static int ata_serverworks_allocate(device_t dev);
159 static void ata_serverworks_setmode(device_t dev, int mode);
160 static int ata_sii_chipinit(device_t dev);
161 static int ata_cmd_allocate(device_t dev);
162 static int ata_cmd_status(device_t dev);
163 static void ata_cmd_setmode(device_t dev, int mode);
164 static int ata_sii_allocate(device_t dev);
165 static int ata_sii_status(device_t dev);
166 static void ata_sii_reset(device_t dev);
167 static void ata_sii_setmode(device_t dev, int mode);
168 static int ata_siiprb_allocate(device_t dev);
169 static int ata_siiprb_status(device_t dev);
170 static int ata_siiprb_begin_transaction(struct ata_request *request);
171 static int ata_siiprb_end_transaction(struct ata_request *request);
172 static void ata_siiprb_reset(device_t dev);
173 static void ata_siiprb_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
174 static void ata_siiprb_dmainit(device_t dev);
175 static int ata_sis_chipinit(device_t dev);
176 static int ata_sis_allocate(device_t dev);
177 static void ata_sis_reset(device_t dev);
178 static void ata_sis_setmode(device_t dev, int mode);
179 static int ata_via_chipinit(device_t dev);
180 static int ata_via_allocate(device_t dev);
181 static void ata_via_reset(device_t dev);
182 static void ata_via_setmode(device_t dev, int mode);
183 static void ata_via_southbridge_fixup(device_t dev);
184 static void ata_via_family_setmode(device_t dev, int mode);
185 static struct ata_chip_id *ata_match_chip(device_t dev, struct ata_chip_id *index);
186 static struct ata_chip_id *ata_find_chip(device_t dev, struct ata_chip_id *index, int slot);
187 static int ata_setup_interrupt(device_t dev);
188 static int ata_serialize(device_t dev, int flags);
189 static void ata_print_cable(device_t dev, u_int8_t *who);
190 static int ata_atapi(device_t dev);
191 static int ata_check_80pin(device_t dev, int mode);
192 static int ata_mode2idx(int mode);
196 * generic ATA support functions
199 ata_generic_ident(device_t dev)
201 struct ata_pci_controller *ctlr = device_get_softc(dev);
203 device_set_desc(dev, "GENERIC ATA controller");
204 ctlr->chipinit = ata_generic_chipinit;
209 ata_generic_chipinit(device_t dev)
211 struct ata_pci_controller *ctlr = device_get_softc(dev);
213 if (ata_setup_interrupt(dev))
215 ctlr->setmode = ata_generic_setmode;
220 ata_generic_intr(void *data)
222 struct ata_pci_controller *ctlr = data;
223 struct ata_channel *ch;
226 for (unit = 0; unit < ctlr->channels; unit++) {
227 if ((ch = ctlr->interrupt[unit].argument))
228 ctlr->interrupt[unit].function(ch);
233 ata_generic_setmode(device_t dev, int mode)
235 struct ata_device *atadev = device_get_softc(dev);
237 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
238 mode = ata_check_80pin(dev, mode);
239 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
245 * SATA support functions
248 ata_sata_phy_check_events(device_t dev)
250 struct ata_channel *ch = device_get_softc(dev);
251 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
253 /* clear error bits/interrupt */
254 ATA_IDX_OUTL(ch, ATA_SERROR, error);
256 /* do we have any events flagged ? */
258 struct ata_connect_task *tp;
259 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
261 /* if we have a connection event deal with it */
262 if ((error & ATA_SE_PHY_CHANGED) &&
263 (tp = (struct ata_connect_task *)
264 kmalloc(sizeof(struct ata_connect_task),
265 M_ATA, M_INTWAIT | M_ZERO))) {
267 if (((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) ||
268 ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)) {
270 device_printf(ch->dev, "CONNECT requested\n");
271 tp->action = ATA_C_ATTACH;
275 device_printf(ch->dev, "DISCONNECT requested\n");
276 tp->action = ATA_C_DETACH;
279 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
280 taskqueue_enqueue(taskqueue_thread[mycpuid], &tp->task);
286 ata_sata_phy_event(void *context, int dummy)
288 struct ata_connect_task *tp = (struct ata_connect_task *)context;
289 struct ata_channel *ch = device_get_softc(tp->dev);
294 if (tp->action == ATA_C_ATTACH) {
296 device_printf(tp->dev, "CONNECTED\n");
298 ata_identify(tp->dev);
300 if (tp->action == ATA_C_DETACH) {
301 if (!device_get_children(tp->dev, &children, &nchildren)) {
302 for (i = 0; i < nchildren; i++)
304 device_delete_child(tp->dev, children[i]);
305 kfree(children, M_TEMP);
307 spin_lock_wr(&ch->state_mtx);
308 ch->state = ATA_IDLE;
309 spin_unlock_wr(&ch->state_mtx);
311 device_printf(tp->dev, "DISCONNECTED\n");
318 ata_sata_phy_reset(device_t dev)
320 struct ata_channel *ch = device_get_softc(dev);
323 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE)
324 return ata_sata_connect(ch);
326 for (retry = 0; retry < 10; retry++) {
327 for (loop = 0; loop < 10; loop++) {
328 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_RESET);
330 if ((ATA_IDX_INL(ch, ATA_SCONTROL) &
331 ATA_SC_DET_MASK) == ATA_SC_DET_RESET)
335 for (loop = 0; loop < 10; loop++) {
336 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_IDLE |
337 ATA_SC_IPM_DIS_PARTIAL |
338 ATA_SC_IPM_DIS_SLUMBER);
340 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == 0)
341 return ata_sata_connect(ch);
348 ata_sata_connect(struct ata_channel *ch)
353 /* wait up to 1 second for "connect well" */
354 for (timeout = 0; timeout < 100 ; timeout++) {
355 status = ATA_IDX_INL(ch, ATA_SSTATUS);
356 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1 ||
357 (status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)
361 if (timeout >= 100) {
363 device_printf(ch->dev, "SATA connect status=%08x\n", status);
368 device_printf(ch->dev, "SATA connect time=%dms\n", timeout * 10);
370 /* clear SATA error register */
371 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
377 ata_sata_setmode(device_t dev, int mode)
379 struct ata_device *atadev = device_get_softc(dev);
382 * if we detect that the device isn't a real SATA device we limit
383 * the transfer mode to UDMA5/ATA100.
384 * this works around the problems some devices has with the
385 * Marvell 88SX8030 SATA->PATA converters and UDMA6/ATA133.
387 if (atadev->param.satacapabilities != 0x0000 &&
388 atadev->param.satacapabilities != 0xffff) {
389 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
391 /* on some drives we need to set the transfer mode */
392 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
393 ata_limit_mode(dev, mode, ATA_UDMA6));
395 /* query SATA STATUS for the speed */
396 if (ch->r_io[ATA_SSTATUS].res &&
397 ((ATA_IDX_INL(ch, ATA_SSTATUS) & ATA_SS_CONWELL_MASK) ==
398 ATA_SS_CONWELL_GEN2))
399 atadev->mode = ATA_SA300;
401 atadev->mode = ATA_SA150;
404 mode = ata_limit_mode(dev, mode, ATA_UDMA5);
405 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
411 ata_request2fis_h2d(struct ata_request *request, u_int8_t *fis)
413 struct ata_device *atadev = device_get_softc(request->dev);
415 if (request->flags & ATA_R_ATAPI) {
416 fis[0] = 0x27; /* host to device */
417 fis[1] = 0x80; /* command FIS (note PM goes here) */
418 fis[2] = ATA_PACKET_CMD;
419 if (request->flags & ATA_R_DMA)
422 fis[5] = request->transfersize;
423 fis[6] = request->transfersize >> 8;
425 fis[7] = ATA_D_LBA | atadev->unit;
426 fis[15] = ATA_A_4BIT;
430 ata_modify_if_48bit(request);
431 fis[0] = 0x27; /* host to device */
432 fis[1] = 0x80; /* command FIS (note PM goes here) */
433 fis[2] = request->u.ata.command;
434 fis[3] = request->u.ata.feature;
435 fis[4] = request->u.ata.lba;
436 fis[5] = request->u.ata.lba >> 8;
437 fis[6] = request->u.ata.lba >> 16;
438 fis[7] = ATA_D_LBA | atadev->unit;
439 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
440 fis[7] |= (request->u.ata.lba >> 24 & 0x0f);
441 fis[8] = request->u.ata.lba >> 24;
442 fis[9] = request->u.ata.lba >> 32;
443 fis[10] = request->u.ata.lba >> 40;
444 fis[11] = request->u.ata.feature >> 8;
445 fis[12] = request->u.ata.count;
446 fis[13] = request->u.ata.count >> 8;
447 fis[15] = ATA_A_4BIT;
454 * AHCI v1.x compliant SATA chipset support functions
457 ata_ahci_chipinit(device_t dev)
459 struct ata_pci_controller *ctlr = device_get_softc(dev);
463 /* reset AHCI controller */
464 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
465 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) | ATA_AHCI_GHC_HR);
467 if (ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) & ATA_AHCI_GHC_HR) {
468 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
469 device_printf(dev, "AHCI controller reset failure\n");
473 /* enable AHCI mode */
474 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
475 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) | ATA_AHCI_GHC_AE);
477 /* get the number of HW channels */
479 MAX(flsl(ATA_INL(ctlr->r_res2, ATA_AHCI_PI)),
480 (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_NPMASK) + 1);
482 /* disable interrupt sources and clear interrupts */
483 for (unit = 0; unit < ctlr->channels; unit++) {
484 int offset = unit << 7;
485 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset, 0);
486 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset, -1);
488 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, ATA_INL(ctlr->r_res2, ATA_AHCI_IS));
490 /* enable AHCI interrupts */
491 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
492 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) | ATA_AHCI_GHC_IE);
494 ctlr->reset = ata_ahci_reset;
495 ctlr->dmainit = ata_ahci_dmainit;
496 ctlr->allocate = ata_ahci_allocate;
497 ctlr->setmode = ata_sata_setmode;
499 /* enable PCI interrupt */
500 pci_write_config(dev, PCIR_COMMAND,
501 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
503 /* announce we support the HW */
504 version = ATA_INL(ctlr->r_res2, ATA_AHCI_VS);
506 "AHCI Version %x%x.%x%x controller with %d ports detected\n",
507 (version >> 24) & 0xff, (version >> 16) & 0xff,
508 (version >> 8) & 0xff, version & 0xff,
509 (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_NPMASK) + 1);
514 ata_ahci_allocate(device_t dev)
516 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
517 struct ata_channel *ch = device_get_softc(dev);
519 int offset = ch->unit << 7;
521 /* set the SATA resources */
522 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
523 ch->r_io[ATA_SSTATUS].offset = ATA_AHCI_P_SSTS + offset;
524 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
525 ch->r_io[ATA_SERROR].offset = ATA_AHCI_P_SERR + offset;
526 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
527 ch->r_io[ATA_SCONTROL].offset = ATA_AHCI_P_SCTL + offset;
528 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
529 ch->r_io[ATA_SACTIVE].offset = ATA_AHCI_P_SACT + offset;
531 ch->hw.status = ata_ahci_status;
532 ch->hw.begin_transaction = ata_ahci_begin_transaction;
533 ch->hw.end_transaction = ata_ahci_end_transaction;
534 ch->hw.command = NULL; /* not used here */
536 /* setup work areas */
537 work = ch->dma->work_bus + ATA_AHCI_CL_OFFSET;
538 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLB + offset, work & 0xffffffff);
539 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLBU + offset, work >> 32);
541 work = ch->dma->work_bus + ATA_AHCI_FB_OFFSET;
542 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FB + offset, work & 0xffffffff);
543 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FBU + offset, work >> 32);
545 /* enable wanted port interrupts */
546 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset,
547 (ATA_AHCI_P_IX_CPD | ATA_AHCI_P_IX_TFE | ATA_AHCI_P_IX_HBF |
548 ATA_AHCI_P_IX_HBD | ATA_AHCI_P_IX_IF | ATA_AHCI_P_IX_OF |
549 ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC | ATA_AHCI_P_IX_DP |
550 ATA_AHCI_P_IX_UF | ATA_AHCI_P_IX_SDB | ATA_AHCI_P_IX_DS |
551 ATA_AHCI_P_IX_PS | ATA_AHCI_P_IX_DHR));
553 /* start operations on this channel */
554 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
555 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
556 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
561 ata_ahci_status(device_t dev)
563 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
564 struct ata_channel *ch = device_get_softc(dev);
565 u_int32_t action = ATA_INL(ctlr->r_res2, ATA_AHCI_IS);
566 int offset = ch->unit << 7;
569 if (action & (1 << ch->unit)) {
570 u_int32_t istatus = ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset);
572 /* clear interrupt(s) */
573 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, action & (1 << ch->unit));
574 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset, istatus);
576 /* do we have any PHY events ? */
577 ata_sata_phy_check_events(dev);
579 /* do we have any device action ? */
580 return (!(ATA_INL(ctlr->r_res2, ATA_AHCI_P_CI + offset) & (1 << tag)));
585 /* must be called with ATA channel locked and state_mtx held */
587 ata_ahci_begin_transaction(struct ata_request *request)
589 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
590 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
591 struct ata_ahci_cmd_tab *ctp;
592 struct ata_ahci_cmd_list *clp;
593 int offset = ch->unit << 7;
594 int tag = 0, entries = 0;
597 /* get a piece of the workspace for this request */
598 ctp = (struct ata_ahci_cmd_tab *)
599 (ch->dma->work + ATA_AHCI_CT_OFFSET + (ATA_AHCI_CT_SIZE * tag));
601 /* setup the FIS for this request */
602 if (!(fis_size = ata_ahci_setup_fis(ctp, request))) {
603 device_printf(request->dev, "setting up SATA FIS failed\n");
604 request->result = EIO;
605 return ATA_OP_FINISHED;
608 /* if request moves data setup and load SG list */
609 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
610 if (ch->dma->load(ch->dev, request->data, request->bytecount,
611 request->flags & ATA_R_READ,
612 ctp->prd_tab, &entries)) {
613 device_printf(request->dev, "setting up DMA failed\n");
614 request->result = EIO;
615 return ATA_OP_FINISHED;
619 /* setup the command list entry */
620 clp = (struct ata_ahci_cmd_list *)
621 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
623 clp->prd_length = entries;
624 clp->cmd_flags = (request->flags & ATA_R_WRITE ? (1<<6) : 0) |
625 (request->flags & ATA_R_ATAPI ? ((1<<5) | (1<<7)) : 0) |
626 (fis_size / sizeof(u_int32_t));
628 clp->cmd_table_phys = htole64(ch->dma->work_bus + ATA_AHCI_CT_OFFSET +
629 (ATA_AHCI_CT_SIZE * tag));
631 /* clear eventual ACTIVE bit */
632 ATA_IDX_OUTL(ch, ATA_SACTIVE, ATA_IDX_INL(ch, ATA_SACTIVE) & (1 << tag));
634 /* set command type bit */
635 if (request->flags & ATA_R_ATAPI)
636 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
637 ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) |
638 ATA_AHCI_P_CMD_ATAPI);
640 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
641 ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) &
642 ~ATA_AHCI_P_CMD_ATAPI);
644 /* issue the command */
645 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CI + offset, (1 << tag));
647 /* start the timeout */
648 callout_reset(&request->callout, request->timeout * hz,
649 (timeout_t*)ata_timeout, request);
650 return ATA_OP_CONTINUES;
653 /* must be called with ATA channel locked and state_mtx held */
655 ata_ahci_end_transaction(struct ata_request *request)
657 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
658 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
659 struct ata_ahci_cmd_list *clp;
661 int offset = ch->unit << 7;
664 /* kill the timeout */
665 callout_stop(&request->callout);
668 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + offset);
669 request->status = tf_data;
671 /* if error status get details */
672 if (request->status & ATA_S_ERROR)
673 request->error = tf_data >> 8;
675 /* record how much data we actually moved */
676 clp = (struct ata_ahci_cmd_list *)
677 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
678 request->donecount = clp->bytecount;
680 /* release SG list etc */
681 ch->dma->unload(ch->dev);
683 return ATA_OP_FINISHED;
687 ata_ahci_reset(device_t dev)
689 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
690 struct ata_channel *ch = device_get_softc(dev);
692 int offset = ch->unit << 7;
695 if (!(ATA_INL(ctlr->r_res2, ATA_AHCI_PI) & (1 << ch->unit))) {
696 device_printf(dev, "port not implemented\n");
701 /* kill off all activity on this channel */
702 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
703 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
704 cmd & ~(ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
706 /* XXX SOS this is not entirely wrong */
711 device_printf(dev, "stopping AHCI engine failed\n");
714 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) & ATA_AHCI_P_CMD_CR);
716 /* issue Command List Override if supported */
717 if (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_CAP_CLO) {
718 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
719 cmd |= ATA_AHCI_P_CMD_CLO;
720 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, cmd);
725 device_printf(dev, "executing CLO failed\n");
728 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD+offset)&ATA_AHCI_P_CMD_CLO);
732 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, ATA_AHCI_P_CMD_SUD);
734 /* enable interface */
735 if (ata_sata_phy_reset(dev)) {
736 switch (ATA_INL(ctlr->r_res2, ATA_AHCI_P_SIG + offset)) {
738 ch->devices = ATA_ATAPI_MASTER;
739 device_printf(ch->dev, "SATA ATAPI devices not supported yet\n");
743 ch->devices = ATA_PORTMULTIPLIER;
744 device_printf(ch->dev, "Portmultipliers not supported yet\n");
748 ch->devices = ATA_ATA_MASTER;
753 /* clear any interrupts pending on this channel */
754 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset,
755 ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset));
757 /* start operations on this channel */
758 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
759 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
760 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
764 ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
766 struct ata_dmasetprd_args *args = xsc;
767 struct ata_ahci_dma_prd *prd = args->dmatab;
770 if (!(args->error = error)) {
771 for (i = 0; i < nsegs; i++) {
772 prd[i].dba = htole64(segs[i].ds_addr);
773 prd[i].dbc = htole32((segs[i].ds_len - 1) & ATA_AHCI_PRD_MASK);
780 ata_ahci_dmainit(device_t dev)
782 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
783 struct ata_channel *ch = device_get_softc(dev);
787 /* note start and stop are not used here */
788 ch->dma->setprd = ata_ahci_dmasetprd;
789 ch->dma->max_iosize = 8192 * DEV_BSIZE;
790 if (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_CAP_64BIT)
791 ch->dma->max_address = BUS_SPACE_MAXADDR;
796 ata_ahci_setup_fis(struct ata_ahci_cmd_tab *ctp, struct ata_request *request)
798 bzero(ctp->cfis, 64);
799 if (request->flags & ATA_R_ATAPI) {
800 bzero(ctp->acmd, 32);
801 bcopy(request->u.atapi.ccb, ctp->acmd, 12);
803 return ata_request2fis_h2d(request, &ctp->cfis[0]);
807 * Generic AHCI part support functions.
810 ata_genahci_ident(device_t dev)
812 struct ata_pci_controller *ctlr = device_get_softc(dev);
813 static struct ata_chip_id id = {0, 0, 0, 0x00, ATA_SA300, "AHCI"};
816 if(!(pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_STORAGE_SATA_AHCI)) {
820 ksprintf(buffer, "GENERIC %s %s controller", id.text, ata_mode2str(id.max_dma));
821 device_set_desc_copy(dev, buffer);
823 ctlr->chipinit = ata_genahci_chipinit;
828 ata_genahci_chipinit(device_t dev)
830 struct ata_pci_controller *ctlr = device_get_softc(dev);
832 if (ata_setup_interrupt(dev))
835 /* Check if the chip has PCI BAR 5 as memory resource. */
836 ctlr->r_type2 = SYS_RES_MEMORY;
837 ctlr->r_rid2 = PCIR_BAR(5); /* 0x24 */
838 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
839 &ctlr->r_rid2, RF_ACTIVE))) {
840 return ata_ahci_chipinit(dev);
846 * Acard chipset support functions
849 ata_acard_ident(device_t dev)
851 struct ata_pci_controller *ctlr = device_get_softc(dev);
852 struct ata_chip_id *idx;
853 static struct ata_chip_id ids[] =
854 {{ ATA_ATP850R, 0, ATPOLD, 0x00, ATA_UDMA2, "ATP850" },
855 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "ATP860A" },
856 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "ATP860R" },
857 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "ATP865A" },
858 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "ATP865R" },
859 { 0, 0, 0, 0, 0, 0}};
862 if (!(idx = ata_match_chip(dev, ids)))
865 ksprintf(buffer, "Acard %s %s controller",
866 idx->text, ata_mode2str(idx->max_dma));
867 device_set_desc_copy(dev, buffer);
869 ctlr->chipinit = ata_acard_chipinit;
874 ata_acard_chipinit(device_t dev)
876 struct ata_pci_controller *ctlr = device_get_softc(dev);
878 if (ata_setup_interrupt(dev))
881 ctlr->allocate = ata_acard_allocate;
882 if (ctlr->chip->cfg1 == ATPOLD) {
883 ctlr->setmode = ata_acard_850_setmode;
884 ctlr->locking = ata_serialize;
887 ctlr->setmode = ata_acard_86X_setmode;
892 ata_acard_allocate(device_t dev)
894 struct ata_channel *ch = device_get_softc(dev);
896 /* setup the usual register normal pci style */
897 if (ata_pci_allocate(dev))
900 ch->hw.status = ata_acard_status;
905 ata_acard_status(device_t dev)
907 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
908 struct ata_channel *ch = device_get_softc(dev);
910 if (ctlr->chip->cfg1 == ATPOLD &&
911 ATA_LOCKING(ch->dev, ATA_LF_WHICH) != ch->unit)
913 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
914 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
916 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
917 ATA_BMSTAT_INTERRUPT)
919 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
921 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
922 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
925 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
927 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
934 ata_acard_850_setmode(device_t dev, int mode)
936 device_t gparent = GRANDPARENT(dev);
937 struct ata_pci_controller *ctlr = device_get_softc(gparent);
938 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
939 struct ata_device *atadev = device_get_softc(dev);
940 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
943 mode = ata_limit_mode(dev, mode,
944 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
946 /* XXX SOS missing WDMA0+1 + PIO modes */
947 if (mode >= ATA_WDMA2) {
948 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
950 device_printf(dev, "%ssetting %s on %s chip\n",
951 (error) ? "FAILURE " : "",
952 ata_mode2str(mode), ctlr->chip->text);
954 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
956 reg54 &= ~(0x03 << (devno << 1));
957 if (mode >= ATA_UDMA0)
958 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
959 pci_write_config(gparent, 0x54, reg54, 1);
960 pci_write_config(gparent, 0x4a, 0xa6, 1);
961 pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
966 /* we could set PIO mode timings, but we assume the BIOS did that */
970 ata_acard_86X_setmode(device_t dev, int mode)
972 device_t gparent = GRANDPARENT(dev);
973 struct ata_pci_controller *ctlr = device_get_softc(gparent);
974 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
975 struct ata_device *atadev = device_get_softc(dev);
976 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
980 mode = ata_limit_mode(dev, mode,
981 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
983 mode = ata_check_80pin(dev, mode);
985 /* XXX SOS missing WDMA0+1 + PIO modes */
986 if (mode >= ATA_WDMA2) {
987 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
989 device_printf(dev, "%ssetting %s on %s chip\n",
990 (error) ? "FAILURE " : "",
991 ata_mode2str(mode), ctlr->chip->text);
993 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
995 reg44 &= ~(0x000f << (devno << 2));
996 if (mode >= ATA_UDMA0)
997 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
998 pci_write_config(gparent, 0x44, reg44, 2);
999 pci_write_config(gparent, 0x4a, 0xa6, 1);
1000 pci_write_config(gparent, 0x40 + devno, 0x31, 1);
1001 atadev->mode = mode;
1005 /* we could set PIO mode timings, but we assume the BIOS did that */
1010 * Acer Labs Inc (ALI) chipset support functions
1013 ata_ali_ident(device_t dev)
1015 struct ata_pci_controller *ctlr = device_get_softc(dev);
1016 struct ata_chip_id *idx;
1017 static struct ata_chip_id ids[] =
1018 {{ ATA_ALI_5289, 0x00, 2, ALISATA, ATA_SA150, "M5289" },
1019 { ATA_ALI_5288, 0x00, 4, ALISATA, ATA_SA300, "M5288" },
1020 { ATA_ALI_5287, 0x00, 4, ALISATA, ATA_SA150, "M5287" },
1021 { ATA_ALI_5281, 0x00, 2, ALISATA, ATA_SA150, "M5281" },
1022 { ATA_ALI_5229, 0xc5, 0, ALINEW, ATA_UDMA6, "M5229" },
1023 { ATA_ALI_5229, 0xc4, 0, ALINEW, ATA_UDMA5, "M5229" },
1024 { ATA_ALI_5229, 0xc2, 0, ALINEW, ATA_UDMA4, "M5229" },
1025 { ATA_ALI_5229, 0x20, 0, ALIOLD, ATA_UDMA2, "M5229" },
1026 { ATA_ALI_5229, 0x00, 0, ALIOLD, ATA_WDMA2, "M5229" },
1027 { 0, 0, 0, 0, 0, 0}};
1030 if (!(idx = ata_match_chip(dev, ids)))
1033 ksprintf(buffer, "AcerLabs %s %s controller",
1034 idx->text, ata_mode2str(idx->max_dma));
1035 device_set_desc_copy(dev, buffer);
1037 ctlr->chipinit = ata_ali_chipinit;
1042 ata_ali_chipinit(device_t dev)
1044 struct ata_pci_controller *ctlr = device_get_softc(dev);
1046 if (ata_setup_interrupt(dev))
1049 switch (ctlr->chip->cfg2) {
1051 ctlr->channels = ctlr->chip->cfg1;
1052 ctlr->allocate = ata_ali_sata_allocate;
1053 ctlr->setmode = ata_sata_setmode;
1055 /* if we have a memory resource we can likely do AHCI */
1056 ctlr->r_type2 = SYS_RES_MEMORY;
1057 ctlr->r_rid2 = PCIR_BAR(5);
1058 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1059 &ctlr->r_rid2, RF_ACTIVE)))
1060 return ata_ahci_chipinit(dev);
1062 /* enable PCI interrupt */
1063 pci_write_config(dev, PCIR_COMMAND,
1064 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1068 /* use device interrupt as byte count end */
1069 pci_write_config(dev, 0x4a, pci_read_config(dev, 0x4a, 1) | 0x20, 1);
1071 /* enable cable detection and UDMA support on newer chips */
1072 pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
1074 /* enable ATAPI UDMA mode */
1075 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x01, 1);
1077 /* only chips with revision > 0xc4 can do 48bit DMA */
1078 if (ctlr->chip->chiprev <= 0xc4)
1080 "using PIO transfers above 137GB as workaround for "
1081 "48bit DMA access bug, expect reduced performance\n");
1082 ctlr->allocate = ata_ali_allocate;
1083 ctlr->reset = ata_ali_reset;
1084 ctlr->setmode = ata_ali_setmode;
1088 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
1089 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x03, 1);
1090 ctlr->setmode = ata_ali_setmode;
1097 ata_ali_allocate(device_t dev)
1099 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1100 struct ata_channel *ch = device_get_softc(dev);
1102 /* setup the usual register normal pci style */
1103 if (ata_pci_allocate(dev))
1106 /* older chips can't do 48bit DMA transfers */
1107 if (ctlr->chip->chiprev <= 0xc4)
1108 ch->flags |= ATA_NO_48BIT_DMA;
1114 ata_ali_sata_allocate(device_t dev)
1116 device_t parent = device_get_parent(dev);
1117 struct ata_pci_controller *ctlr = device_get_softc(parent);
1118 struct ata_channel *ch = device_get_softc(dev);
1119 struct resource *io = NULL, *ctlio = NULL;
1120 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
1123 rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
1124 io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
1128 rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
1129 ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
1131 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
1135 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
1136 ch->r_io[i].res = io;
1137 ch->r_io[i].offset = i + (unit10 ? 8 : 0);
1139 ch->r_io[ATA_CONTROL].res = ctlio;
1140 ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
1141 ch->r_io[ATA_IDX_ADDR].res = io;
1142 ata_default_registers(dev);
1144 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
1145 ch->r_io[i].res = ctlr->r_res1;
1146 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
1149 ch->flags |= ATA_NO_SLAVE;
1151 /* XXX SOS PHY handling awkward in ALI chip not supported yet */
1157 ata_ali_reset(device_t dev)
1159 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1160 struct ata_channel *ch = device_get_softc(dev);
1164 ata_generic_reset(dev);
1167 * workaround for datacorruption bug found on at least SUN Blade-100
1168 * find the ISA function on the southbridge and disable then enable
1169 * the ATA channel tristate buffer
1171 if (ctlr->chip->chiprev == 0xc3 || ctlr->chip->chiprev == 0xc2) {
1172 if (!device_get_children(GRANDPARENT(dev), &children, &nchildren)) {
1173 for (i = 0; i < nchildren; i++) {
1174 if (pci_get_devid(children[i]) == ATA_ALI_1533) {
1175 pci_write_config(children[i], 0x58,
1176 pci_read_config(children[i], 0x58, 1) &
1177 ~(0x04 << ch->unit), 1);
1178 pci_write_config(children[i], 0x58,
1179 pci_read_config(children[i], 0x58, 1) |
1180 (0x04 << ch->unit), 1);
1184 kfree(children, M_TEMP);
1190 ata_ali_setmode(device_t dev, int mode)
1192 device_t gparent = GRANDPARENT(dev);
1193 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1194 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1195 struct ata_device *atadev = device_get_softc(dev);
1196 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1199 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1201 if (ctlr->chip->cfg2 & ALINEW) {
1202 if (mode > ATA_UDMA2 &&
1203 pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
1204 ata_print_cable(dev, "controller");
1209 mode = ata_check_80pin(dev, mode);
1211 if (ctlr->chip->cfg2 & ALIOLD) {
1212 /* doesn't support ATAPI DMA on write */
1213 ch->flags |= ATA_ATAPI_DMA_RO;
1214 if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
1215 /* doesn't support ATAPI DMA on two ATAPI devices */
1216 device_printf(dev, "two atapi devices on this channel, no DMA\n");
1217 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1221 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1224 device_printf(dev, "%ssetting %s on %s chip\n",
1225 (error) ? "FAILURE " : "",
1226 ata_mode2str(mode), ctlr->chip->text);
1228 if (mode >= ATA_UDMA0) {
1229 u_int8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
1230 u_int32_t word54 = pci_read_config(gparent, 0x54, 4);
1232 word54 &= ~(0x000f000f << (devno << 2));
1233 word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
1234 pci_write_config(gparent, 0x54, word54, 4);
1235 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1239 u_int32_t piotimings[] =
1240 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
1241 0x00310001, 0x00440001, 0x00330001, 0x00310001};
1243 pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
1244 ~(0x0008000f << (devno << 2)), 4);
1245 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1246 piotimings[ata_mode2idx(mode)], 4);
1248 atadev->mode = mode;
1254 * American Micro Devices (AMD) chipset support functions
1257 ata_amd_ident(device_t dev)
1259 struct ata_pci_controller *ctlr = device_get_softc(dev);
1260 struct ata_chip_id *idx;
1261 static struct ata_chip_id ids[] =
1262 {{ ATA_AMD756, 0x00, AMDNVIDIA, 0x00, ATA_UDMA4, "756" },
1263 { ATA_AMD766, 0x00, AMDNVIDIA, AMDCABLE|AMDBUG, ATA_UDMA5, "766" },
1264 { ATA_AMD768, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA5, "768" },
1265 { ATA_AMD8111, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA6, "8111" },
1266 { 0, 0, 0, 0, 0, 0}};
1269 if (!(idx = ata_match_chip(dev, ids)))
1272 ksprintf(buffer, "AMD %s %s controller",
1273 idx->text, ata_mode2str(idx->max_dma));
1274 device_set_desc_copy(dev, buffer);
1276 ctlr->chipinit = ata_amd_chipinit;
1281 ata_amd_chipinit(device_t dev)
1283 struct ata_pci_controller *ctlr = device_get_softc(dev);
1285 if (ata_setup_interrupt(dev))
1288 /* disable/set prefetch, postwrite */
1289 if (ctlr->chip->cfg2 & AMDBUG)
1290 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
1292 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
1294 ctlr->setmode = ata_via_family_setmode;
1300 * ATI chipset support functions
1303 ata_ati_ident(device_t dev)
1305 struct ata_pci_controller *ctlr = device_get_softc(dev);
1306 struct ata_chip_id *idx;
1307 static struct ata_chip_id ids[] =
1308 {{ ATA_ATI_IXP200, 0x00, 0, 0, ATA_UDMA5, "IXP200" },
1309 { ATA_ATI_IXP300, 0x00, 0, 0, ATA_UDMA6, "IXP300" },
1310 { ATA_ATI_IXP400, 0x00, 0, 0, ATA_UDMA6, "IXP400" },
1311 { ATA_ATI_SB600, 0x00, 0, 0, ATA_UDMA6, "SB600" },
1312 { ATA_ATI_IXP300_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP300" },
1313 { ATA_ATI_IXP400_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP400" },
1314 { ATA_ATI_IXP400_S2, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP400" },
1315 { ATA_ATI_SB600_S1, 0x00, ATIAHCI, 0, ATA_SA300, "SB600" },
1316 { ATA_ATI_SB600_S2, 0x00, ATIAHCI, 0, ATA_SA300, "SB600" },
1317 { 0, 0, 0, 0, 0, 0}};
1320 if (!(idx = ata_match_chip(dev, ids)))
1323 ksprintf(buffer, "ATI %s %s controller",
1324 idx->text, ata_mode2str(idx->max_dma));
1325 device_set_desc_copy(dev, buffer);
1329 * The ATI SATA controllers are actually a SiI 3112 controller, except
1332 if (ctlr->chip->cfg1 & SIIMEMIO)
1333 ctlr->chipinit = ata_sii_chipinit;
1335 ctlr->chipinit = ata_ati_chipinit;
1340 ata_ati_chipinit(device_t dev)
1342 struct ata_pci_controller *ctlr = device_get_softc(dev);
1344 if (ata_setup_interrupt(dev))
1347 /* The SB600 needs special treatment. */
1348 if (ctlr->chip->cfg1 & ATIAHCI) {
1349 /* Check if the chip is configured as an AHCI part. */
1350 if ((pci_get_subclass(dev) == PCIS_STORAGE_SATA) &&
1351 (pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_STORAGE_SATA_AHCI)) {
1352 /* Check if the chip has PCI BAR 5 as memory resource. */
1353 ctlr->r_type2 = SYS_RES_MEMORY;
1354 ctlr->r_rid2 = PCIR_BAR(5); /* 0x24 */
1355 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1358 return ata_ahci_chipinit(dev);
1363 ctlr->setmode = ata_ati_setmode;
1368 ata_ati_setmode(device_t dev, int mode)
1370 device_t gparent = GRANDPARENT(dev);
1371 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1372 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1373 struct ata_device *atadev = device_get_softc(dev);
1374 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1375 int offset = (devno ^ 0x01) << 3;
1377 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
1378 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
1379 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
1381 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1383 mode = ata_check_80pin(dev, mode);
1385 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1388 device_printf(dev, "%ssetting %s on %s chip\n",
1389 (error) ? "FAILURE " : "",
1390 ata_mode2str(mode), ctlr->chip->text);
1392 if (mode >= ATA_UDMA0) {
1393 pci_write_config(gparent, 0x56,
1394 (pci_read_config(gparent, 0x56, 2) &
1395 ~(0xf << (devno << 2))) |
1396 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
1397 pci_write_config(gparent, 0x54,
1398 pci_read_config(gparent, 0x54, 1) |
1399 (0x01 << devno), 1);
1400 pci_write_config(gparent, 0x44,
1401 (pci_read_config(gparent, 0x44, 4) &
1402 ~(0xff << offset)) |
1403 (dmatimings[2] << offset), 4);
1405 else if (mode >= ATA_WDMA0) {
1406 pci_write_config(gparent, 0x54,
1407 pci_read_config(gparent, 0x54, 1) &
1408 ~(0x01 << devno), 1);
1409 pci_write_config(gparent, 0x44,
1410 (pci_read_config(gparent, 0x44, 4) &
1411 ~(0xff << offset)) |
1412 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
1415 pci_write_config(gparent, 0x54,
1416 pci_read_config(gparent, 0x54, 1) &
1417 ~(0x01 << devno), 1);
1419 pci_write_config(gparent, 0x4a,
1420 (pci_read_config(gparent, 0x4a, 2) &
1421 ~(0xf << (devno << 2))) |
1422 (((mode - ATA_PIO0) & ATA_MODE_MASK) << (devno<<2)),2);
1423 pci_write_config(gparent, 0x40,
1424 (pci_read_config(gparent, 0x40, 4) &
1425 ~(0xff << offset)) |
1426 (piotimings[ata_mode2idx(mode)] << offset), 4);
1427 atadev->mode = mode;
1432 * Cyrix chipset support functions
1435 ata_cyrix_ident(device_t dev)
1437 struct ata_pci_controller *ctlr = device_get_softc(dev);
1439 if (pci_get_devid(dev) == ATA_CYRIX_5530) {
1440 device_set_desc(dev, "Cyrix 5530 ATA33 controller");
1441 ctlr->chipinit = ata_cyrix_chipinit;
1448 ata_cyrix_chipinit(device_t dev)
1450 struct ata_pci_controller *ctlr = device_get_softc(dev);
1452 if (ata_setup_interrupt(dev))
1456 ctlr->setmode = ata_cyrix_setmode;
1458 ctlr->setmode = ata_generic_setmode;
1463 ata_cyrix_setmode(device_t dev, int mode)
1465 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1466 struct ata_device *atadev = device_get_softc(dev);
1467 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1468 u_int32_t piotiming[] =
1469 { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
1470 u_int32_t dmatiming[] = { 0x00077771, 0x00012121, 0x00002020 };
1471 u_int32_t udmatiming[] = { 0x00921250, 0x00911140, 0x00911030 };
1474 ch->dma->alignment = 16;
1475 ch->dma->max_iosize = 126 * DEV_BSIZE;
1477 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1479 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1482 device_printf(dev, "%ssetting %s on Cyrix chip\n",
1483 (error) ? "FAILURE " : "", ata_mode2str(mode));
1485 if (mode >= ATA_UDMA0) {
1486 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1487 0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
1489 else if (mode >= ATA_WDMA0) {
1490 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1491 0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
1494 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1495 0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
1497 atadev->mode = mode;
1503 * Cypress chipset support functions
1506 ata_cypress_ident(device_t dev)
1508 struct ata_pci_controller *ctlr = device_get_softc(dev);
1511 * the Cypress chip is a mess, it contains two ATA functions, but
1512 * both channels are visible on the first one.
1513 * simply ignore the second function for now, as the right
1514 * solution (ignoring the second channel on the first function)
1515 * doesn't work with the crappy ATA interrupt setup on the alpha.
1517 if (pci_get_devid(dev) == ATA_CYPRESS_82C693 &&
1518 pci_get_function(dev) == 1 &&
1519 pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
1520 device_set_desc(dev, "Cypress 82C693 ATA controller");
1521 ctlr->chipinit = ata_cypress_chipinit;
1528 ata_cypress_chipinit(device_t dev)
1530 struct ata_pci_controller *ctlr = device_get_softc(dev);
1532 if (ata_setup_interrupt(dev))
1535 ctlr->setmode = ata_cypress_setmode;
1540 ata_cypress_setmode(device_t dev, int mode)
1542 device_t gparent = GRANDPARENT(dev);
1543 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1544 struct ata_device *atadev = device_get_softc(dev);
1547 mode = ata_limit_mode(dev, mode, ATA_WDMA2);
1549 /* XXX SOS missing WDMA0+1 + PIO modes */
1550 if (mode == ATA_WDMA2) {
1551 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1553 device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
1554 error ? "FAILURE " : "");
1556 pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
1557 atadev->mode = mode;
1561 /* we could set PIO mode timings, but we assume the BIOS did that */
1566 * HighPoint chipset support functions
1569 ata_highpoint_ident(device_t dev)
1571 struct ata_pci_controller *ctlr = device_get_softc(dev);
1572 struct ata_chip_id *idx;
1573 static struct ata_chip_id ids[] =
1574 {{ ATA_HPT374, 0x07, HPT374, 0x00, ATA_UDMA6, "HPT374" },
1575 { ATA_HPT372, 0x02, HPT372, 0x00, ATA_UDMA6, "HPT372N" },
1576 { ATA_HPT372, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT372" },
1577 { ATA_HPT371, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT371" },
1578 { ATA_HPT366, 0x05, HPT372, 0x00, ATA_UDMA6, "HPT372" },
1579 { ATA_HPT366, 0x03, HPT370, 0x00, ATA_UDMA5, "HPT370" },
1580 { ATA_HPT366, 0x02, HPT366, 0x00, ATA_UDMA4, "HPT368" },
1581 { ATA_HPT366, 0x00, HPT366, HPTOLD, ATA_UDMA4, "HPT366" },
1582 { ATA_HPT302, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT302" },
1583 { 0, 0, 0, 0, 0, 0}};
1586 if (!(idx = ata_match_chip(dev, ids)))
1589 strcpy(buffer, "HighPoint ");
1590 strcat(buffer, idx->text);
1591 if (idx->cfg1 == HPT374) {
1592 if (pci_get_function(dev) == 0)
1593 strcat(buffer, " (channel 0+1)");
1594 if (pci_get_function(dev) == 1)
1595 strcat(buffer, " (channel 2+3)");
1597 ksprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
1598 device_set_desc_copy(dev, buffer);
1600 ctlr->chipinit = ata_highpoint_chipinit;
1605 ata_highpoint_chipinit(device_t dev)
1607 struct ata_pci_controller *ctlr = device_get_softc(dev);
1609 if (ata_setup_interrupt(dev))
1612 if (ctlr->chip->cfg2 == HPTOLD) {
1613 /* disable interrupt prediction */
1614 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
1617 /* disable interrupt prediction */
1618 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
1619 pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
1621 /* enable interrupts */
1622 pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
1624 /* set clocks etc */
1625 if (ctlr->chip->cfg1 < HPT372)
1626 pci_write_config(dev, 0x5b, 0x22, 1);
1628 pci_write_config(dev, 0x5b,
1629 (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
1631 ctlr->allocate = ata_highpoint_allocate;
1632 ctlr->setmode = ata_highpoint_setmode;
1637 ata_highpoint_allocate(device_t dev)
1639 struct ata_channel *ch = device_get_softc(dev);
1641 /* setup the usual register normal pci style */
1642 if (ata_pci_allocate(dev))
1645 ch->flags |= ATA_ALWAYS_DMASTAT;
1650 ata_highpoint_setmode(device_t dev, int mode)
1652 device_t gparent = GRANDPARENT(dev);
1653 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1654 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1655 struct ata_device *atadev = device_get_softc(dev);
1656 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1658 u_int32_t timings33[][4] = {
1659 /* HPT366 HPT370 HPT372 HPT374 mode */
1660 { 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a }, /* PIO 0 */
1661 { 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 }, /* PIO 1 */
1662 { 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 }, /* PIO 2 */
1663 { 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 }, /* PIO 3 */
1664 { 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 }, /* PIO 4 */
1665 { 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea }, /* MWDMA 0 */
1666 { 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 }, /* MWDMA 1 */
1667 { 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 }, /* MWDMA 2 */
1668 { 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea }, /* UDMA 0 */
1669 { 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 }, /* UDMA 1 */
1670 { 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 }, /* UDMA 2 */
1671 { 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 }, /* UDMA 3 */
1672 { 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 }, /* UDMA 4 */
1673 { 0, 0x16454e31, 0x1c8a9c62, 0x12848242 }, /* UDMA 5 */
1674 { 0, 0, 0x1c869c62, 0x12808242 } /* UDMA 6 */
1677 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1679 if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
1680 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1682 mode = ata_highpoint_check_80pin(dev, mode);
1685 * most if not all HPT chips cant really handle that the device is
1686 * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
1687 * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
1689 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
1690 ata_limit_mode(dev, mode, ATA_UDMA5));
1692 device_printf(dev, "%ssetting %s on HighPoint chip\n",
1693 (error) ? "FAILURE " : "", ata_mode2str(mode));
1695 pci_write_config(gparent, 0x40 + (devno << 2),
1696 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
1697 atadev->mode = mode;
1701 ata_highpoint_check_80pin(device_t dev, int mode)
1703 device_t gparent = GRANDPARENT(dev);
1704 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1705 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1706 u_int8_t reg, val, res;
1708 if (ctlr->chip->cfg1 == HPT374 && pci_get_function(gparent) == 1) {
1709 reg = ch->unit ? 0x57 : 0x53;
1710 val = pci_read_config(gparent, reg, 1);
1711 pci_write_config(gparent, reg, val | 0x80, 1);
1715 val = pci_read_config(gparent, reg, 1);
1716 pci_write_config(gparent, reg, val & 0xfe, 1);
1718 res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1719 pci_write_config(gparent, reg, val, 1);
1721 if (mode > ATA_UDMA2 && res) {
1722 ata_print_cable(dev, "controller");
1730 * Intel chipset support functions
1733 ata_intel_ident(device_t dev)
1735 struct ata_pci_controller *ctlr = device_get_softc(dev);
1736 struct ata_chip_id *idx;
1737 static struct ata_chip_id ids[] =
1738 {{ ATA_I82371FB, 0, 0, 0x00, ATA_WDMA2, "PIIX" },
1739 { ATA_I82371SB, 0, 0, 0x00, ATA_WDMA2, "PIIX3" },
1740 { ATA_I82371AB, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1741 { ATA_I82443MX, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1742 { ATA_I82451NX, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1743 { ATA_I82801AB, 0, 0, 0x00, ATA_UDMA2, "ICH0" },
1744 { ATA_I82801AA, 0, 0, 0x00, ATA_UDMA4, "ICH" },
1745 { ATA_I82372FB, 0, 0, 0x00, ATA_UDMA4, "ICH" },
1746 { ATA_I82801BA, 0, 0, 0x00, ATA_UDMA5, "ICH2" },
1747 { ATA_I82801BA_1, 0, 0, 0x00, ATA_UDMA5, "ICH2" },
1748 { ATA_I82801CA, 0, 0, 0x00, ATA_UDMA5, "ICH3" },
1749 { ATA_I82801CA_1, 0, 0, 0x00, ATA_UDMA5, "ICH3" },
1750 { ATA_I82801DB, 0, 0, 0x00, ATA_UDMA5, "ICH4" },
1751 { ATA_I82801DB_1, 0, 0, 0x00, ATA_UDMA5, "ICH4" },
1752 { ATA_I82801EB, 0, 0, 0x00, ATA_UDMA5, "ICH5" },
1753 { ATA_I82801EB_S1, 0, 0, 0x00, ATA_SA150, "ICH5" },
1754 { ATA_I82801EB_R1, 0, 0, 0x00, ATA_SA150, "ICH5" },
1755 { ATA_I6300ESB, 0, 0, 0x00, ATA_UDMA5, "6300ESB" },
1756 { ATA_I6300ESB_S1, 0, 0, 0x00, ATA_SA150, "6300ESB" },
1757 { ATA_I6300ESB_R1, 0, 0, 0x00, ATA_SA150, "6300ESB" },
1758 { ATA_I82801FB, 0, 0, 0x00, ATA_UDMA5, "ICH6" },
1759 { ATA_I82801FB_S1, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1760 { ATA_I82801FB_R1, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1761 { ATA_I82801FBM, 0, AHCI, 0x00, ATA_SA150, "ICH6M" },
1762 { ATA_I82801GB, 0, 0, 0x00, ATA_UDMA5, "ICH7" },
1763 { ATA_I82801GB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1764 { ATA_I82801GB_R1, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1765 { ATA_I82801GB_AH, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1766 { ATA_I82801GBM_S1, 0, AHCI, 0x00, ATA_SA300, "ICH7M" },
1767 { ATA_I82801GBM_R1, 0, AHCI, 0x00, ATA_SA300, "ICH7M" },
1768 { ATA_I82801GBM_AH, 0, AHCI, 0x00, ATA_SA300, "ICH7M" },
1769 { ATA_I63XXESB2, 0, 0, 0x00, ATA_UDMA5, "63XXESB2" },
1770 { ATA_I63XXESB2_S1, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1771 { ATA_I63XXESB2_S2, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1772 { ATA_I63XXESB2_R1, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1773 { ATA_I63XXESB2_R2, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1774 { ATA_I82801HB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1775 { ATA_I82801HB_S2, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1776 { ATA_I82801HB_R1, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1777 { ATA_I82801HB_AH4, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1778 { ATA_I82801HB_AH6, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1779 { ATA_I82801HBM_S1, 0, AHCI, 0x00, ATA_SA300, "ICH8M" },
1780 { ATA_I82801HBM_S2, 0, AHCI, 0x00, ATA_SA300, "ICH8M" },
1781 { ATA_I31244, 0, 0, 0x00, ATA_SA150, "31244" },
1782 { 0, 0, 0, 0, 0, 0}};
1785 if (!(idx = ata_match_chip(dev, ids)))
1788 ksprintf(buffer, "Intel %s %s controller",
1789 idx->text, ata_mode2str(idx->max_dma));
1790 device_set_desc_copy(dev, buffer);
1792 ctlr->chipinit = ata_intel_chipinit;
1797 ata_intel_chipinit(device_t dev)
1799 struct ata_pci_controller *ctlr = device_get_softc(dev);
1801 if (ata_setup_interrupt(dev))
1804 /* good old PIIX needs special treatment (not implemented) */
1805 if (ctlr->chip->chipid == ATA_I82371FB) {
1806 ctlr->setmode = ata_intel_old_setmode;
1809 /* the intel 31244 needs special care if in DPA mode */
1810 else if (ctlr->chip->chipid == ATA_I31244) {
1811 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE) {
1812 ctlr->r_type2 = SYS_RES_MEMORY;
1813 ctlr->r_rid2 = PCIR_BAR(0);
1814 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1819 ctlr->allocate = ata_intel_31244_allocate;
1820 ctlr->reset = ata_intel_31244_reset;
1822 ctlr->setmode = ata_sata_setmode;
1825 /* non SATA intel chips goes here */
1826 else if (ctlr->chip->max_dma < ATA_SA150) {
1827 ctlr->allocate = ata_intel_allocate;
1828 ctlr->setmode = ata_intel_new_setmode;
1831 /* SATA parts can be either compat or AHCI */
1833 /* force all ports active "the legacy way" */
1834 pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f,2);
1836 ctlr->allocate = ata_intel_allocate;
1837 ctlr->reset = ata_intel_reset;
1840 * if we have AHCI capability and BAR(5) as a memory resource
1841 * and AHCI or RAID mode enabled in BIOS we go for AHCI mode
1843 if ((ctlr->chip->cfg1 == AHCI) &&
1844 (pci_read_config(dev, 0x90, 1) & 0xc0)) {
1845 ctlr->r_type2 = SYS_RES_MEMORY;
1846 ctlr->r_rid2 = PCIR_BAR(5);
1847 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1850 return ata_ahci_chipinit(dev);
1852 ctlr->setmode = ata_sata_setmode;
1854 /* enable PCI interrupt */
1855 pci_write_config(dev, PCIR_COMMAND,
1856 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1862 ata_intel_allocate(device_t dev)
1864 struct ata_channel *ch = device_get_softc(dev);
1866 /* setup the usual register normal pci style */
1867 if (ata_pci_allocate(dev))
1870 ch->flags |= ATA_ALWAYS_DMASTAT;
1875 ata_intel_reset(device_t dev)
1877 device_t parent = device_get_parent(dev);
1878 struct ata_pci_controller *ctlr = device_get_softc(parent);
1879 struct ata_channel *ch = device_get_softc(dev);
1882 /* ICH6 & ICH7 in compat mode has 4 SATA ports as master/slave on 2 ch's */
1883 if (ctlr->chip->cfg1) {
1884 mask = (0x0005 << ch->unit);
1887 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1888 if (pci_read_config(parent, 0x90, 1) & 0x04)
1891 mask = (0x0001 << ch->unit);
1892 /* XXX SOS should be in intel_allocate if we grow it */
1893 ch->flags |= ATA_NO_SLAVE;
1896 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1898 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1900 /* wait up to 1 sec for "connect well" */
1901 for (timeout = 0; timeout < 100 ; timeout++) {
1902 if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1903 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1907 ata_generic_reset(dev);
1911 ata_intel_old_setmode(device_t dev, int mode)
1917 ata_intel_new_setmode(device_t dev, int mode)
1919 device_t gparent = GRANDPARENT(dev);
1920 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1921 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1922 struct ata_device *atadev = device_get_softc(dev);
1923 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1924 u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
1925 u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
1926 u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
1927 u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
1928 u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
1929 u_int32_t mask40 = 0, new40 = 0;
1930 u_int8_t mask44 = 0, new44 = 0;
1932 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
1933 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
1934 /* PIO0 PIO1 PIO2 PIO3 PIO4 WDMA0 WDMA1 WDMA2 */
1935 /* UDMA0 UDMA1 UDMA2 UDMA3 UDMA4 UDMA5 UDMA6 */
1937 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1939 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
1940 ata_print_cable(dev, "controller");
1944 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1947 device_printf(dev, "%ssetting %s on %s chip\n",
1948 (error) ? "FAILURE " : "",
1949 ata_mode2str(mode), ctlr->chip->text);
1954 * reg48: 1 bit per (primary drive 0, primary drive 1, secondary
1955 * drive 0, secondary drive 1)
1957 * 0 Disable Ultra DMA mode
1958 * 1 Enable Ultra DMA mode
1960 * reg4a: 4 bits per (primary drive 0, primary drive 1, secondary
1961 * drive 0, secondary drive 1).
1963 * 0001 UDMA mode 1, 3, 5
1964 * 0010 UDMA mode 2, 4, reserved
1966 * (top two bits for each drive reserved)
1970 "regs before 40=%08x 44=%02x 48=%02x 4a=%04x 54=%04x\n",
1971 reg40, reg44, reg48 ,reg4a, reg54);
1973 reg48 &= ~(0x0001 << devno);
1974 reg4a &= ~(0x3 << (devno << 2));
1975 if (mode >= ATA_UDMA0) {
1976 reg48 |= 0x0001 << devno;
1977 if (mode > ATA_UDMA0)
1978 reg4a |= (1 + !(mode & 0x01)) << (devno << 2);
1980 pci_write_config(gparent, 0x48, reg48, 2);
1981 pci_write_config(gparent, 0x4a, reg4a, 2);
1987 * 19:18 Secondary ATA signal mode
1988 * 17:16 Primary ATA signal mode
1989 * 00 = Normal (enabled)
1990 * 01 = Tri-state (disabled)
1991 * 10 = Drive Low (disabled)
1994 * 15 Secondary drive 1 - Base Clock
1995 * 14 Secondary drive 0 - Base Clock
1996 * 13 Primary drive 1 - Base Clock
1997 * 12 Primary drive 0 - Base Clock
1998 * 0 = Select 33 MHz clock
1999 * 1 = Select 100 Mhz clock
2002 * 10 Vendor specific (set by BIOS?)
2005 * 07 Secondary drive 1 - Cable Type
2006 * 06 Secondary drive 0 - Cable Type
2007 * 05 Primary drive 1 - Cable Type
2008 * 04 Primary drive 0 - Cable Type
2010 * 1 = 80 Conductor (or high speed cable)
2012 * 03 Secondary drive 1 - Select 33/66 clock
2013 * 02 Secondary drive 0 - Select 33/66 clock
2014 * 01 Primary drive 1 - Select 33/66 clock
2015 * 00 Primary drive 0 - Select 33/66 clock
2019 * It is unclear what this should be set to when operating
2022 * NOTE: UDMA2 = 33 MHz
2023 * UDMA3 = 40 MHz (?) - unsupported
2028 reg54 |= 0x0400; /* set vendor specific bit */
2029 reg54 &= ~((0x1 << devno) | (0x1000 << devno));
2031 if (mode >= ATA_UDMA5)
2032 reg54 |= (0x1000 << devno);
2033 else if (mode >= ATA_UDMA3) /* XXX should this be ATA_UDMA3 or 4? */
2034 reg54 |= (0x1 << devno);
2036 pci_write_config(gparent, 0x54, reg54, 2);
2039 * Reg40 (32 bits... well, actually two 16 bit registers)
2041 * Primary channel bits 15:00, Secondary channel bits 31:00. Note
2042 * that slave timings are handled in register 44.
2044 * 15 ATA Decode Enable (R/W) 1 = enable decoding of I/O ranges
2046 * 14 Slave ATA Timing Register Enable (R/W)
2048 * 13:12 IORDY Sample Mode
2051 * 10 PIO-3, PIO-4, MW-1, MW-2
2056 * 09:08 Recovery Mode
2057 * 00 PIO-0, PIO-2, SW-2
2062 * 07:04 Secondary Device Control Bits
2063 * 03:00 Primary Device Control Bits
2065 * bit 3 DMA Timing Enable
2067 * bit 2 Indicate Presence of ATA(1) or ATAPI(0) device
2069 * bit 1 Enable IORDY sample point capability for PIO
2070 * xfers. Always enabled for PIO4 and PIO3, enabled
2071 * for PIO2 if indicated by the device, and otherwise
2072 * probably should be 0.
2074 * bit 0 Fast Drive Timing Enable. Enables faster then PIO-0
2079 * Modify reg40 according to the table
2081 if (atadev->unit == ATA_MASTER) {
2083 new40 = timings[ata_mode2idx(mode)] << 8;
2087 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
2088 (timings[ata_mode2idx(mode)] & 0x03);
2092 * Slave ATA timing register enable
2098 * Device control bits 3:0 for master, 7:4 for slave.
2100 * bit3 DMA Timing enable.
2101 * bit2 Indicate presence of ATA(1) or ATAPI(0) device, set accordingly
2102 * bit1 Enable IORDY sample point capability for PIO xfers. Always
2103 * enabled for PIO4 and PIO3, enabled for PIO2 if indicated by
2104 * the device, and otherwise should be 0.
2105 * bit0 Fast Drive Timing Enable. Enable faster then PIO-0 timing modes.
2110 if (atadev->unit == ATA_MASTER) {
2113 if (!ata_atapi(dev))
2118 if (!ata_atapi(dev))
2122 reg40 &= ~0x00ff00ff;
2123 reg40 |= 0x40774077;
2127 * Primary or Secondary controller
2135 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
2136 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
2139 reg40 = pci_read_config(gparent, 0x40, 4);
2140 reg44 = pci_read_config(gparent, 0x44, 1);
2141 reg48 = pci_read_config(gparent, 0x48, 1);
2142 reg4a = pci_read_config(gparent, 0x4a, 2);
2143 reg54 = pci_read_config(gparent, 0x54, 2);
2145 "regs after 40=%08x 44=%02x 48=%02x 4a=%04x 54=%04x\n",
2146 reg40, reg44, reg48 ,reg4a, reg54);
2149 atadev->mode = mode;
2153 ata_intel_31244_allocate(device_t dev)
2155 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2156 struct ata_channel *ch = device_get_softc(dev);
2160 ch_offset = 0x200 + ch->unit * 0x200;
2162 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
2163 ch->r_io[i].res = ctlr->r_res2;
2165 /* setup ATA registers */
2166 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
2167 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x06;
2168 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
2169 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
2170 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
2171 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
2172 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
2173 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1d;
2174 ch->r_io[ATA_ERROR].offset = ch_offset + 0x04;
2175 ch->r_io[ATA_STATUS].offset = ch_offset + 0x1c;
2176 ch->r_io[ATA_ALTSTAT].offset = ch_offset + 0x28;
2177 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x29;
2179 /* setup DMA registers */
2180 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x100;
2181 ch->r_io[ATA_SERROR].offset = ch_offset + 0x104;
2182 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x108;
2184 /* setup SATA registers */
2185 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x70;
2186 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x72;
2187 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x74;
2189 ch->flags |= ATA_NO_SLAVE;
2191 ch->hw.status = ata_intel_31244_status;
2192 ch->hw.command = ata_intel_31244_command;
2194 /* enable PHY state change interrupt */
2195 ATA_OUTL(ctlr->r_res2, 0x4,
2196 ATA_INL(ctlr->r_res2, 0x04) | (0x01 << (ch->unit << 3)));
2201 ata_intel_31244_status(device_t dev)
2203 /* do we have any PHY events ? */
2204 ata_sata_phy_check_events(dev);
2206 /* any drive action to take care of ? */
2207 return ata_pci_status(dev);
2211 ata_intel_31244_command(struct ata_request *request)
2213 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2214 struct ata_device *atadev = device_get_softc(request->dev);
2217 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
2218 return (ata_generic_command(request));
2220 lba = request->u.ata.lba;
2221 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | atadev->unit);
2222 /* enable interrupt */
2223 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
2224 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
2225 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
2226 ATA_IDX_OUTW(ch, ATA_SECTOR, ((lba >> 16) & 0xff00) | (lba & 0x00ff));
2227 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((lba >> 24) & 0xff00) |
2228 ((lba >> 8) & 0x00ff));
2229 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((lba >> 32) & 0xff00) |
2230 ((lba >> 16) & 0x00ff));
2232 /* issue command to controller */
2233 ATA_IDX_OUTB(ch, ATA_COMMAND, request->u.ata.command);
2239 ata_intel_31244_reset(device_t dev)
2241 if (ata_sata_phy_reset(dev))
2242 ata_generic_reset(dev);
2247 * Integrated Technology Express Inc. (ITE) chipset support functions
2250 ata_ite_ident(device_t dev)
2252 struct ata_pci_controller *ctlr = device_get_softc(dev);
2253 struct ata_chip_id *idx;
2254 static struct ata_chip_id ids[] =
2255 {{ ATA_IT8212F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8212F" },
2256 { ATA_IT8211F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8211F" },
2257 { 0, 0, 0, 0, 0, 0}};
2260 if (!(idx = ata_match_chip(dev, ids)))
2263 ksprintf(buffer, "ITE %s %s controller",
2264 idx->text, ata_mode2str(idx->max_dma));
2265 device_set_desc_copy(dev, buffer);
2267 ctlr->chipinit = ata_ite_chipinit;
2272 ata_ite_chipinit(device_t dev)
2274 struct ata_pci_controller *ctlr = device_get_softc(dev);
2276 if (ata_setup_interrupt(dev))
2279 ctlr->setmode = ata_ite_setmode;
2281 /* set PCI mode and 66Mhz reference clock */
2282 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
2284 /* set default active & recover timings */
2285 pci_write_config(dev, 0x54, 0x31, 1);
2286 pci_write_config(dev, 0x56, 0x31, 1);
2291 ata_ite_setmode(device_t dev, int mode)
2293 device_t gparent = GRANDPARENT(dev);
2294 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2295 struct ata_device *atadev = device_get_softc(dev);
2296 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2299 /* correct the mode for what the HW supports */
2300 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2302 /* check the CBLID bits for 80 conductor cable detection */
2303 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
2304 (ch->unit ? (1<<3) : (1<<2)))) {
2305 ata_print_cable(dev, "controller");
2309 /* set the wanted mode on the device */
2310 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2313 device_printf(dev, "%s setting %s on ITE8212F chip\n",
2314 (error) ? "failed" : "success", ata_mode2str(mode));
2316 /* if the device accepted the mode change, setup the HW accordingly */
2318 if (mode >= ATA_UDMA0) {
2319 u_int8_t udmatiming[] =
2320 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
2322 /* enable UDMA mode */
2323 pci_write_config(gparent, 0x50,
2324 pci_read_config(gparent, 0x50, 1) &
2325 ~(1 << (devno + 3)), 1);
2327 /* set UDMA timing */
2328 pci_write_config(gparent,
2329 0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
2330 udmatiming[mode & ATA_MODE_MASK], 1);
2333 u_int8_t chtiming[] =
2334 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
2336 /* disable UDMA mode */
2337 pci_write_config(gparent, 0x50,
2338 pci_read_config(gparent, 0x50, 1) |
2339 (1 << (devno + 3)), 1);
2341 /* set active and recover timing (shared between master & slave) */
2342 if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
2343 chtiming[ata_mode2idx(mode)])
2344 pci_write_config(gparent, 0x54 + (ch->unit << 2),
2345 chtiming[ata_mode2idx(mode)], 1);
2347 atadev->mode = mode;
2353 * JMicron chipset support functions
2356 ata_jmicron_ident(device_t dev)
2358 struct ata_pci_controller *ctlr = device_get_softc(dev);
2359 struct ata_chip_id *idx;
2360 static struct ata_chip_id ids[] =
2361 {{ ATA_JMB360, 0, 1, 0, ATA_SA300, "JMB360" },
2362 { ATA_JMB361, 0, 1, 1, ATA_SA300, "JMB361" },
2363 { ATA_JMB363, 0, 2, 1, ATA_SA300, "JMB363" },
2364 { ATA_JMB365, 0, 1, 2, ATA_SA300, "JMB365" },
2365 { ATA_JMB366, 0, 2, 2, ATA_SA300, "JMB366" },
2366 { ATA_JMB368, 0, 0, 1, ATA_UDMA6, "JMB368" },
2367 { 0, 0, 0, 0, 0, 0}};
2370 if (!(idx = ata_match_chip(dev, ids)))
2373 if ((pci_read_config(dev, 0xdf, 1) & 0x40) &&
2374 (pci_get_function(dev) == (pci_read_config(dev, 0x40, 1) & 0x02 >> 1)))
2375 ksnprintf(buffer, sizeof(buffer), "JMicron %s %s controller",
2376 idx->text, ata_mode2str(ATA_UDMA6));
2378 ksnprintf(buffer, sizeof(buffer), "JMicron %s %s controller",
2379 idx->text, ata_mode2str(idx->max_dma));
2380 device_set_desc_copy(dev, buffer);
2382 ctlr->chipinit = ata_jmicron_chipinit;
2387 ata_jmicron_chipinit(device_t dev)
2389 struct ata_pci_controller *ctlr = device_get_softc(dev);
2392 if (ata_setup_interrupt(dev))
2395 /* do we have multiple PCI functions ? */
2396 if (pci_read_config(dev, 0xdf, 1) & 0x40) {
2397 /* if we have a memory BAR(5) we are on the AHCI part */
2398 ctlr->r_type2 = SYS_RES_MEMORY;
2399 ctlr->r_rid2 = PCIR_BAR(5);
2400 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2401 &ctlr->r_rid2, RF_ACTIVE)))
2402 return ata_ahci_chipinit(dev);
2404 /* otherwise we are on the PATA part */
2405 ctlr->allocate = ata_pci_allocate;
2406 ctlr->reset = ata_generic_reset;
2407 ctlr->dmainit = ata_pci_dmainit;
2408 ctlr->setmode = ata_jmicron_setmode;
2409 ctlr->channels = ctlr->chip->cfg2;
2412 /* set controller configuration to a combined setup we support */
2413 pci_write_config(dev, 0x40, 0x80c0a131, 4);
2414 pci_write_config(dev, 0x80, 0x01200000, 4);
2416 ctlr->r_type2 = SYS_RES_MEMORY;
2417 ctlr->r_rid2 = PCIR_BAR(5);
2418 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
2419 &ctlr->r_rid2, RF_ACTIVE))) {
2420 if ((error = ata_ahci_chipinit(dev)))
2424 ctlr->allocate = ata_jmicron_allocate;
2425 ctlr->reset = ata_jmicron_reset;
2426 ctlr->dmainit = ata_jmicron_dmainit;
2427 ctlr->setmode = ata_jmicron_setmode;
2429 /* set the number of HW channels */
2430 ctlr->channels = ctlr->chip->cfg1 + ctlr->chip->cfg2;
2436 ata_jmicron_allocate(device_t dev)
2438 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2439 struct ata_channel *ch = device_get_softc(dev);
2442 if (ch->unit >= ctlr->chip->cfg1) {
2443 ch->unit -= ctlr->chip->cfg1;
2444 error = ata_pci_allocate(dev);
2445 ch->unit += ctlr->chip->cfg1;
2448 error = ata_ahci_allocate(dev);
2453 ata_jmicron_reset(device_t dev)
2455 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2456 struct ata_channel *ch = device_get_softc(dev);
2458 if (ch->unit >= ctlr->chip->cfg1)
2459 ata_generic_reset(dev);
2461 ata_ahci_reset(dev);
2465 ata_jmicron_dmainit(device_t dev)
2467 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2468 struct ata_channel *ch = device_get_softc(dev);
2470 if (ch->unit >= ctlr->chip->cfg1)
2471 ata_pci_dmainit(dev);
2473 ata_ahci_dmainit(dev);
2477 ata_jmicron_setmode(device_t dev, int mode)
2479 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2480 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2482 if (pci_read_config(dev, 0xdf, 1) & 0x40 || ch->unit >= ctlr->chip->cfg1) {
2483 struct ata_device *atadev = device_get_softc(dev);
2485 /* check for 80pin cable present */
2486 if (pci_read_config(dev, 0x40, 1) & 0x08)
2487 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2489 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2491 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
2492 atadev->mode = mode;
2495 ata_sata_setmode(dev, mode);
2500 * Marvell chipset support functions
2502 #define ATA_MV_HOST_BASE(ch) \
2503 ((ch->unit & 3) * 0x0100) + (ch->unit > 3 ? 0x30000 : 0x20000)
2504 #define ATA_MV_EDMA_BASE(ch) \
2505 ((ch->unit & 3) * 0x2000) + (ch->unit > 3 ? 0x30000 : 0x20000)
2507 struct ata_marvell_response {
2509 u_int8_t edma_status;
2510 u_int8_t dev_status;
2511 u_int32_t timestamp;
2514 struct ata_marvell_dma_prdentry {
2522 ata_marvell_ident(device_t dev)
2524 struct ata_pci_controller *ctlr = device_get_softc(dev);
2525 struct ata_chip_id *idx;
2526 static struct ata_chip_id ids[] =
2527 {{ ATA_M88SX5040, 0, 4, MV50XX, ATA_SA150, "88SX5040" },
2528 { ATA_M88SX5041, 0, 4, MV50XX, ATA_SA150, "88SX5041" },
2529 { ATA_M88SX5080, 0, 8, MV50XX, ATA_SA150, "88SX5080" },
2530 { ATA_M88SX5081, 0, 8, MV50XX, ATA_SA150, "88SX5081" },
2531 { ATA_M88SX6041, 0, 4, MV60XX, ATA_SA300, "88SX6041" },
2532 { ATA_M88SX6081, 0, 8, MV60XX, ATA_SA300, "88SX6081" },
2533 { ATA_M88SX6101, 0, 1, MV61XX, ATA_UDMA6, "88SX6101" },
2534 { ATA_M88SX6145, 0, 2, MV61XX, ATA_UDMA6, "88SX6145" },
2535 { 0, 0, 0, 0, 0, 0}};
2538 if (!(idx = ata_match_chip(dev, ids)))
2541 ksprintf(buffer, "Marvell %s %s controller",
2542 idx->text, ata_mode2str(idx->max_dma));
2543 device_set_desc_copy(dev, buffer);
2545 switch (ctlr->chip->cfg2) {
2548 ctlr->chipinit = ata_marvell_edma_chipinit;
2551 ctlr->chipinit = ata_marvell_pata_chipinit;
2558 ata_marvell_pata_chipinit(device_t dev)
2560 struct ata_pci_controller *ctlr = device_get_softc(dev);
2562 if (ata_setup_interrupt(dev))
2565 ctlr->allocate = ata_marvell_pata_allocate;
2566 ctlr->setmode = ata_marvell_pata_setmode;
2567 ctlr->channels = ctlr->chip->cfg1;
2572 ata_marvell_pata_allocate(device_t dev)
2574 struct ata_channel *ch = device_get_softc(dev);
2576 /* setup the usual register normal pci style */
2577 if (ata_pci_allocate(dev))
2580 /* dont use 32 bit PIO transfers */
2581 ch->flags |= ATA_USE_16BIT;
2587 ata_marvell_pata_setmode(device_t dev, int mode)
2589 device_t gparent = GRANDPARENT(dev);
2590 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2591 struct ata_device *atadev = device_get_softc(dev);
2593 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2594 mode = ata_check_80pin(dev, mode);
2595 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
2596 atadev->mode = mode;
2600 ata_marvell_edma_chipinit(device_t dev)
2602 struct ata_pci_controller *ctlr = device_get_softc(dev);
2604 if (ata_setup_interrupt(dev))
2607 ctlr->r_type1 = SYS_RES_MEMORY;
2608 ctlr->r_rid1 = PCIR_BAR(0);
2609 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
2610 &ctlr->r_rid1, RF_ACTIVE)))
2613 /* mask all host controller interrupts */
2614 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x00000000);
2616 /* mask all PCI interrupts */
2617 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x00000000);
2619 ctlr->allocate = ata_marvell_edma_allocate;
2620 ctlr->reset = ata_marvell_edma_reset;
2621 ctlr->dmainit = ata_marvell_edma_dmainit;
2622 ctlr->setmode = ata_sata_setmode;
2623 ctlr->channels = ctlr->chip->cfg1;
2625 /* clear host controller interrupts */
2626 ATA_OUTL(ctlr->r_res1, 0x20014, 0x00000000);
2627 if (ctlr->chip->cfg1 > 4)
2628 ATA_OUTL(ctlr->r_res1, 0x30014, 0x00000000);
2630 /* clear PCI interrupts */
2631 ATA_OUTL(ctlr->r_res1, 0x01d58, 0x00000000);
2633 /* unmask PCI interrupts we want */
2634 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x007fffff);
2636 /* unmask host controller interrupts we want */
2637 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x000000ff/*HC0*/ | 0x0001fe00/*HC1*/ |
2638 /*(1<<19) | (1<<20) | (1<<21) |*/(1<<22) | (1<<24) | (0x7f << 25));
2640 /* enable PCI interrupt */
2641 pci_write_config(dev, PCIR_COMMAND,
2642 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
2647 ata_marvell_edma_allocate(device_t dev)
2649 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2650 struct ata_channel *ch = device_get_softc(dev);
2651 u_int64_t work = ch->dma->work_bus;
2654 /* clear work area */
2655 bzero(ch->dma->work, 1024+256);
2657 /* set legacy ATA resources */
2658 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2659 ch->r_io[i].res = ctlr->r_res1;
2660 ch->r_io[i].offset = 0x02100 + (i << 2) + ATA_MV_EDMA_BASE(ch);
2662 ch->r_io[ATA_CONTROL].res = ctlr->r_res1;
2663 ch->r_io[ATA_CONTROL].offset = 0x02120 + ATA_MV_EDMA_BASE(ch);
2664 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res1;
2665 ata_default_registers(dev);
2667 /* set SATA resources */
2668 switch (ctlr->chip->cfg2) {
2670 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2671 ch->r_io[ATA_SSTATUS].offset = 0x00100 + ATA_MV_HOST_BASE(ch);
2672 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2673 ch->r_io[ATA_SERROR].offset = 0x00104 + ATA_MV_HOST_BASE(ch);
2674 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2675 ch->r_io[ATA_SCONTROL].offset = 0x00108 + ATA_MV_HOST_BASE(ch);
2678 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2679 ch->r_io[ATA_SSTATUS].offset = 0x02300 + ATA_MV_EDMA_BASE(ch);
2680 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2681 ch->r_io[ATA_SERROR].offset = 0x02304 + ATA_MV_EDMA_BASE(ch);
2682 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2683 ch->r_io[ATA_SCONTROL].offset = 0x02308 + ATA_MV_EDMA_BASE(ch);
2684 ch->r_io[ATA_SACTIVE].res = ctlr->r_res1;
2685 ch->r_io[ATA_SACTIVE].offset = 0x02350 + ATA_MV_EDMA_BASE(ch);
2689 ch->flags |= ATA_NO_SLAVE;
2690 ch->flags |= ATA_USE_16BIT; /* XXX SOS needed ? */
2691 ata_generic_hw(dev);
2692 ch->hw.begin_transaction = ata_marvell_edma_begin_transaction;
2693 ch->hw.end_transaction = ata_marvell_edma_end_transaction;
2694 ch->hw.status = ata_marvell_edma_status;
2696 /* disable the EDMA machinery */
2697 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2698 DELAY(100000); /* SOS should poll for disabled */
2700 /* set configuration to non-queued 128b read transfers stop on error */
2701 ATA_OUTL(ctlr->r_res1, 0x02000 + ATA_MV_EDMA_BASE(ch), (1<<11) | (1<<13));
2703 /* request queue base high */
2704 ATA_OUTL(ctlr->r_res1, 0x02010 + ATA_MV_EDMA_BASE(ch), work >> 32);
2706 /* request queue in ptr */
2707 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2709 /* request queue out ptr */
2710 ATA_OUTL(ctlr->r_res1, 0x02018 + ATA_MV_EDMA_BASE(ch), 0x0);
2712 /* response queue base high */
2714 ATA_OUTL(ctlr->r_res1, 0x0201c + ATA_MV_EDMA_BASE(ch), work >> 32);
2716 /* response queue in ptr */
2717 ATA_OUTL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch), 0x0);
2719 /* response queue out ptr */
2720 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2722 /* clear SATA error register */
2723 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2725 /* clear any outstanding error interrupts */
2726 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2728 /* unmask all error interrupts */
2729 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2731 /* enable EDMA machinery */
2732 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2737 ata_marvell_edma_status(device_t dev)
2739 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2740 struct ata_channel *ch = device_get_softc(dev);
2741 u_int32_t cause = ATA_INL(ctlr->r_res1, 0x01d60);
2742 int shift = (ch->unit << 1) + (ch->unit > 3);
2744 if (cause & (1 << shift)) {
2746 /* clear interrupt(s) */
2747 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2749 /* do we have any PHY events ? */
2750 ata_sata_phy_check_events(dev);
2753 /* do we have any device action ? */
2754 return (cause & (2 << shift));
2757 /* must be called with ATA channel locked and state_mtx held */
2759 ata_marvell_edma_begin_transaction(struct ata_request *request)
2761 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2762 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2768 int dummy, error, slot;
2770 /* only DMA R/W goes through the EMDA machine */
2771 if (request->u.ata.command != ATA_READ_DMA &&
2772 request->u.ata.command != ATA_WRITE_DMA) {
2774 /* disable the EDMA machinery */
2775 if (ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)
2776 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2777 return ata_begin_transaction(request);
2780 /* check for 48 bit access and convert if needed */
2781 ata_modify_if_48bit(request);
2783 /* check sanity, setup SG list and DMA engine */
2784 if ((error = ch->dma->load(ch->dev, request->data, request->bytecount,
2785 request->flags & ATA_R_READ, ch->dma->sg,
2787 device_printf(request->dev, "setting up DMA failed\n");
2788 request->result = error;
2789 return ATA_OP_FINISHED;
2792 /* get next free request queue slot */
2793 req_in = ATA_INL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch));
2794 slot = (((req_in & ~0xfffffc00) >> 5) + 0) & 0x1f;
2795 bytep = (u_int8_t *)(ch->dma->work);
2796 bytep += (slot << 5);
2797 wordp = (u_int16_t *)bytep;
2798 quadp = (u_int32_t *)bytep;
2800 /* fill in this request */
2801 quadp[0] = (long)ch->dma->sg_bus & 0xffffffff;
2802 quadp[1] = (u_int64_t)ch->dma->sg_bus >> 32;
2803 wordp[4] = (request->flags & ATA_R_READ ? 0x01 : 0x00) | (tag<<1);
2806 bytep[i++] = (request->u.ata.count >> 8) & 0xff;
2807 bytep[i++] = 0x10 | ATA_COUNT;
2808 bytep[i++] = request->u.ata.count & 0xff;
2809 bytep[i++] = 0x10 | ATA_COUNT;
2811 bytep[i++] = (request->u.ata.lba >> 24) & 0xff;
2812 bytep[i++] = 0x10 | ATA_SECTOR;
2813 bytep[i++] = request->u.ata.lba & 0xff;
2814 bytep[i++] = 0x10 | ATA_SECTOR;
2816 bytep[i++] = (request->u.ata.lba >> 32) & 0xff;
2817 bytep[i++] = 0x10 | ATA_CYL_LSB;
2818 bytep[i++] = (request->u.ata.lba >> 8) & 0xff;
2819 bytep[i++] = 0x10 | ATA_CYL_LSB;
2821 bytep[i++] = (request->u.ata.lba >> 40) & 0xff;
2822 bytep[i++] = 0x10 | ATA_CYL_MSB;
2823 bytep[i++] = (request->u.ata.lba >> 16) & 0xff;
2824 bytep[i++] = 0x10 | ATA_CYL_MSB;
2826 bytep[i++] = ATA_D_LBA | ATA_D_IBM | ((request->u.ata.lba >> 24) & 0xf);
2827 bytep[i++] = 0x10 | ATA_DRIVE;
2829 bytep[i++] = request->u.ata.command;
2830 bytep[i++] = 0x90 | ATA_COMMAND;
2832 /* enable EDMA machinery if needed */
2833 if (!(ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)) {
2834 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2835 while (!(ATA_INL(ctlr->r_res1,
2836 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
2840 /* tell EDMA it has a new request */
2841 slot = (((req_in & ~0xfffffc00) >> 5) + 1) & 0x1f;
2842 req_in &= 0xfffffc00;
2843 req_in += (slot << 5);
2844 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), req_in);
2846 return ATA_OP_CONTINUES;
2849 /* must be called with ATA channel locked and state_mtx held */
2851 ata_marvell_edma_end_transaction(struct ata_request *request)
2853 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2854 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2855 int offset = (ch->unit > 3 ? 0x30014 : 0x20014);
2856 u_int32_t icr = ATA_INL(ctlr->r_res1, offset);
2859 /* EDMA interrupt */
2860 if ((icr & (0x0001 << (ch->unit & 3)))) {
2861 struct ata_marvell_response *response;
2862 u_int32_t rsp_in, rsp_out;
2866 callout_stop(&request->callout);
2868 /* get response ptr's */
2869 rsp_in = ATA_INL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch));
2870 rsp_out = ATA_INL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch));
2871 slot = (((rsp_in & ~0xffffff00) >> 3)) & 0x1f;
2872 rsp_out &= 0xffffff00;
2873 rsp_out += (slot << 3);
2874 response = (struct ata_marvell_response *)
2875 (ch->dma->work + 1024 + (slot << 3));
2877 /* record status for this request */
2878 request->status = response->dev_status;
2882 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), rsp_out);
2884 /* update progress */
2885 if (!(request->status & ATA_S_ERROR) &&
2886 !(request->flags & ATA_R_TIMEOUT))
2887 request->donecount = request->bytecount;
2889 /* unload SG list */
2890 ch->dma->unload(ch->dev);
2892 res = ATA_OP_FINISHED;
2895 /* legacy ATA interrupt */
2897 res = ata_end_transaction(request);
2901 ATA_OUTL(ctlr->r_res1, offset, ~(icr & (0x0101 << (ch->unit & 3))));
2906 ata_marvell_edma_reset(device_t dev)
2908 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2909 struct ata_channel *ch = device_get_softc(dev);
2911 /* disable the EDMA machinery */
2912 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2913 while ((ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
2916 /* clear SATA error register */
2917 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2919 /* clear any outstanding error interrupts */
2920 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2922 /* unmask all error interrupts */
2923 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2925 /* enable channel and test for devices */
2926 if (ata_sata_phy_reset(dev))
2927 ata_generic_reset(dev);
2929 /* enable EDMA machinery */
2930 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2934 ata_marvell_edma_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs,
2937 struct ata_dmasetprd_args *args = xsc;
2938 struct ata_marvell_dma_prdentry *prd = args->dmatab;
2941 if ((args->error = error))
2944 for (i = 0; i < nsegs; i++) {
2945 prd[i].addrlo = htole32(segs[i].ds_addr);
2946 prd[i].count = htole32(segs[i].ds_len);
2947 prd[i].addrhi = htole32((u_int64_t)segs[i].ds_addr >> 32);
2949 prd[i - 1].count |= htole32(ATA_DMA_EOT);
2953 ata_marvell_edma_dmainit(device_t dev)
2955 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2956 struct ata_channel *ch = device_get_softc(dev);
2960 /* note start and stop are not used here */
2961 ch->dma->setprd = ata_marvell_edma_dmasetprd;
2963 if (ATA_INL(ctlr->r_res1, 0x00d00) & 0x00000004)
2964 ch->dma->max_address = BUS_SPACE_MAXADDR;
2970 * National chipset support functions
2973 ata_national_ident(device_t dev)
2975 struct ata_pci_controller *ctlr = device_get_softc(dev);
2977 /* this chip is a clone of the Cyrix chip, bugs and all */
2978 if (pci_get_devid(dev) == ATA_SC1100) {
2979 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
2980 ctlr->chipinit = ata_national_chipinit;
2987 ata_national_chipinit(device_t dev)
2989 struct ata_pci_controller *ctlr = device_get_softc(dev);
2991 if (ata_setup_interrupt(dev))
2994 ctlr->setmode = ata_national_setmode;
2999 ata_national_setmode(device_t dev, int mode)
3001 device_t gparent = GRANDPARENT(dev);
3002 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3003 struct ata_device *atadev = device_get_softc(dev);
3004 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3005 u_int32_t piotiming[] =
3006 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
3007 0x00803020, 0x20102010, 0x00100010,
3008 0x00100010, 0x00100010, 0x00100010 };
3009 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
3010 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
3013 ch->dma->alignment = 16;
3014 ch->dma->max_iosize = 126 * DEV_BSIZE;
3016 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
3018 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3021 device_printf(dev, "%s setting %s on National chip\n",
3022 (error) ? "failed" : "success", ata_mode2str(mode));
3024 if (mode >= ATA_UDMA0) {
3025 pci_write_config(gparent, 0x44 + (devno << 3),
3026 udmatiming[mode & ATA_MODE_MASK], 4);
3028 else if (mode >= ATA_WDMA0) {
3029 pci_write_config(gparent, 0x44 + (devno << 3),
3030 dmatiming[mode & ATA_MODE_MASK], 4);
3033 pci_write_config(gparent, 0x44 + (devno << 3),
3034 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
3037 pci_write_config(gparent, 0x40 + (devno << 3),
3038 piotiming[ata_mode2idx(mode)], 4);
3039 atadev->mode = mode;
3044 * NetCell chipset support functions
3047 ata_netcell_ident(device_t dev)
3049 struct ata_pci_controller *ctlr = device_get_softc(dev);
3051 if (pci_get_devid(dev) == ATA_NETCELL_SR) {
3052 device_set_desc(dev, "Netcell SyncRAID SR3000/5000 RAID Controller");
3053 ctlr->chipinit = ata_netcell_chipinit;
3060 ata_netcell_chipinit(device_t dev)
3062 struct ata_pci_controller *ctlr = device_get_softc(dev);
3064 if (ata_generic_chipinit(dev))
3067 ctlr->allocate = ata_netcell_allocate;
3072 ata_netcell_allocate(device_t dev)
3074 struct ata_channel *ch = device_get_softc(dev);
3076 /* setup the usual register normal pci style */
3077 if (ata_pci_allocate(dev))
3080 /* the NetCell only supports 16 bit PIO transfers */
3081 ch->flags |= ATA_USE_16BIT;
3088 * nVidia chipset support functions
3091 ata_nvidia_ident(device_t dev)
3093 struct ata_pci_controller *ctlr = device_get_softc(dev);
3094 struct ata_chip_id *idx;
3095 static struct ata_chip_id ids[] =
3096 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nForce" },
3097 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2" },
3098 { ATA_NFORCE2_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2 Pro" },
3099 { ATA_NFORCE2_PRO_S1, 0, 0, 0, ATA_SA150, "nForce2 Pro" },
3100 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3" },
3101 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3 Pro" },
3102 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
3103 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
3104 { ATA_NFORCE_MCP04, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP" },
3105 { ATA_NFORCE_MCP04_S1, 0, 0, NV4, ATA_SA150, "nForce MCP" },
3106 { ATA_NFORCE_MCP04_S2, 0, 0, NV4, ATA_SA150, "nForce MCP" },
3107 { ATA_NFORCE_CK804, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce CK804" },
3108 { ATA_NFORCE_CK804_S1, 0, 0, NV4, ATA_SA300, "nForce CK804" },
3109 { ATA_NFORCE_CK804_S2, 0, 0, NV4, ATA_SA300, "nForce CK804" },
3110 { ATA_NFORCE_MCP51, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP51" },
3111 { ATA_NFORCE_MCP51_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP51" },
3112 { ATA_NFORCE_MCP51_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP51" },
3113 { ATA_NFORCE_MCP55, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP55" },
3114 { ATA_NFORCE_MCP55_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP55" },
3115 { ATA_NFORCE_MCP55_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP55" },
3116 { 0, 0, 0, 0, 0, 0}} ;
3119 if (!(idx = ata_match_chip(dev, ids)))
3122 ksprintf(buffer, "nVidia %s %s controller",
3123 idx->text, ata_mode2str(idx->max_dma));
3124 device_set_desc_copy(dev, buffer);
3126 ctlr->chipinit = ata_nvidia_chipinit;
3131 ata_nvidia_chipinit(device_t dev)
3133 struct ata_pci_controller *ctlr = device_get_softc(dev);
3135 if (ata_setup_interrupt(dev))
3138 if (ctlr->chip->max_dma >= ATA_SA150) {
3139 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
3140 ctlr->r_type2 = SYS_RES_IOPORT;
3142 ctlr->r_type2 = SYS_RES_MEMORY;
3143 ctlr->r_rid2 = PCIR_BAR(5);
3144 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3145 &ctlr->r_rid2, RF_ACTIVE))) {
3146 int offset = ctlr->chip->cfg2 & NV4 ? 0x0440 : 0x0010;
3148 ctlr->allocate = ata_nvidia_allocate;
3149 ctlr->reset = ata_nvidia_reset;
3151 /* enable control access */
3152 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
3154 if (ctlr->chip->cfg2 & NVQ) {
3155 /* clear interrupt status */
3156 ATA_OUTL(ctlr->r_res2, offset, 0x00ff00ff);
3158 /* enable device and PHY state change interrupts */
3159 ATA_OUTL(ctlr->r_res2, offset + 4, 0x000d000d);
3161 /* disable NCQ support */
3162 ATA_OUTL(ctlr->r_res2, 0x0400,
3163 ATA_INL(ctlr->r_res2, 0x0400) & 0xfffffff9);
3166 /* clear interrupt status */
3167 ATA_OUTB(ctlr->r_res2, offset, 0xff);
3169 /* enable device and PHY state change interrupts */
3170 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
3173 /* enable PCI interrupt */
3174 pci_write_config(dev, PCIR_COMMAND,
3175 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3178 ctlr->setmode = ata_sata_setmode;
3181 /* disable prefetch, postwrite */
3182 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
3183 ctlr->setmode = ata_via_family_setmode;
3189 ata_nvidia_allocate(device_t dev)
3191 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3192 struct ata_channel *ch = device_get_softc(dev);
3194 /* setup the usual register normal pci style */
3195 if (ata_pci_allocate(dev))
3198 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3199 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
3200 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3201 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
3202 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3203 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
3205 ch->hw.status = ata_nvidia_status;
3206 ch->flags |= ATA_NO_SLAVE;
3212 ata_nvidia_status(device_t dev)
3214 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3215 struct ata_channel *ch = device_get_softc(dev);
3216 int offset = ctlr->chip->cfg2 & NV4 ? 0x0440 : 0x0010;
3217 int shift = ch->unit << (ctlr->chip->cfg2 & NVQ ? 4 : 2);
3218 u_int32_t istatus = ATA_INL(ctlr->r_res2, offset);
3220 /* do we have any PHY events ? */
3221 if (istatus & (0x0c << shift))
3222 ata_sata_phy_check_events(dev);
3224 /* clear interrupt(s) */
3225 ATA_OUTB(ctlr->r_res2, offset,
3226 (0x0f << shift) | (ctlr->chip->cfg2 & NVQ ? 0x00f000f0 : 0));
3228 /* do we have any device action ? */
3229 return (istatus & (0x01 << shift));
3233 ata_nvidia_reset(device_t dev)
3235 if (ata_sata_phy_reset(dev))
3236 ata_generic_reset(dev);
3241 * Promise chipset support functions
3243 #define ATA_PDC_APKT_OFFSET 0x00000010
3244 #define ATA_PDC_HPKT_OFFSET 0x00000040
3245 #define ATA_PDC_ASG_OFFSET 0x00000080
3246 #define ATA_PDC_LSG_OFFSET 0x000000c0
3247 #define ATA_PDC_HSG_OFFSET 0x00000100
3248 #define ATA_PDC_CHN_OFFSET 0x00000400
3249 #define ATA_PDC_BUF_BASE 0x00400000
3250 #define ATA_PDC_BUF_OFFSET 0x00100000
3251 #define ATA_PDC_MAX_HPKT 8
3252 #define ATA_PDC_WRITE_REG 0x00
3253 #define ATA_PDC_WRITE_CTL 0x0e
3254 #define ATA_PDC_WRITE_END 0x08
3255 #define ATA_PDC_WAIT_NBUSY 0x10
3256 #define ATA_PDC_WAIT_READY 0x18
3257 #define ATA_PDC_1B 0x20
3258 #define ATA_PDC_2B 0x40
3260 struct host_packet {
3262 TAILQ_ENTRY(host_packet) chain;
3265 struct ata_promise_sx4 {
3266 struct spinlock mtx;
3267 TAILQ_HEAD(, host_packet) queue;
3272 ata_promise_ident(device_t dev)
3274 struct ata_pci_controller *ctlr = device_get_softc(dev);
3275 struct ata_chip_id *idx;
3276 static struct ata_chip_id ids[] =
3277 {{ ATA_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "PDC20246" },
3278 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20262" },
3279 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20263" },
3280 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20265" },
3281 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20267" },
3282 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20268" },
3283 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "PDC20269" },
3284 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20270" },
3285 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "PDC20271" },
3286 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "PDC20275" },
3287 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "PDC20276" },
3288 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "PDC20277" },
3289 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "PDC20318" },
3290 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "PDC20319" },
3291 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20371" },
3292 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20375" },
3293 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20376" },
3294 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20377" },
3295 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20378" },
3296 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20379" },
3297 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20571" },
3298 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20575" },
3299 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20579" },
3300 { ATA_PDC20771, 0, PRMIO, PRCMBO2, ATA_SA300, "PDC20771" },
3301 { ATA_PDC40775, 0, PRMIO, PRCMBO2, ATA_SA300, "PDC40775" },
3302 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20617" },
3303 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20618" },
3304 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20619" },
3305 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20620" },
3306 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "PDC20621" },
3307 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "PDC20622" },
3308 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40518" },
3309 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40519" },
3310 { ATA_PDC40718, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40718" },
3311 { ATA_PDC40719, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40719" },
3312 { ATA_PDC40779, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40779" },
3313 { 0, 0, 0, 0, 0, 0}};
3315 uintptr_t devid = 0;
3317 if (!(idx = ata_match_chip(dev, ids)))
3320 /* if we are on a SuperTrak SX6000 dont attach */
3321 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
3322 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
3323 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
3324 devid == ATA_I960RM)
3327 strcpy(buffer, "Promise ");
3328 strcat(buffer, idx->text);
3330 /* if we are on a FastTrak TX4, adjust the interrupt resource */
3331 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
3332 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
3333 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
3334 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
3335 static long start = 0, end = 0;
3337 if (pci_get_slot(dev) == 1) {
3338 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
3339 strcat(buffer, " (channel 0+1)");
3341 else if (pci_get_slot(dev) == 2 && start && end) {
3342 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
3343 strcat(buffer, " (channel 2+3)");
3349 ksprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
3350 device_set_desc_copy(dev, buffer);
3352 ctlr->chipinit = ata_promise_chipinit;
3357 ata_promise_chipinit(device_t dev)
3359 struct ata_pci_controller *ctlr = device_get_softc(dev);
3360 int fake_reg, stat_reg;
3362 if (ata_setup_interrupt(dev))
3365 switch (ctlr->chip->cfg1) {
3368 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
3370 ctlr->dmainit = ata_promise_dmainit;
3374 /* enable burst mode */
3375 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
3376 ctlr->allocate = ata_promise_allocate;
3377 ctlr->setmode = ata_promise_setmode;
3381 ctlr->allocate = ata_promise_tx2_allocate;
3382 ctlr->setmode = ata_promise_setmode;
3386 ctlr->r_type1 = SYS_RES_MEMORY;
3387 ctlr->r_rid1 = PCIR_BAR(4);
3388 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
3389 &ctlr->r_rid1, RF_ACTIVE)))
3392 ctlr->r_type2 = SYS_RES_MEMORY;
3393 ctlr->r_rid2 = PCIR_BAR(3);
3394 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3395 &ctlr->r_rid2, RF_ACTIVE)))
3398 if (ctlr->chip->cfg2 == PRSX4X) {
3399 struct ata_promise_sx4 *hpkt;
3400 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
3402 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
3403 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3404 ata_promise_sx4_intr, ctlr, &ctlr->handle, NULL)) {
3405 device_printf(dev, "unable to setup interrupt\n");
3409 /* print info about cache memory */
3410 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
3411 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
3412 ((dimm >> 24) & 0xff),
3413 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
3414 " ECC enabled" : "" );
3416 /* adjust cache memory parameters */
3417 ATA_OUTL(ctlr->r_res2, 0x000c000c,
3418 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
3420 /* setup host packet controls */
3421 hpkt = kmalloc(sizeof(struct ata_promise_sx4),
3422 M_TEMP, M_INTWAIT | M_ZERO);
3423 spin_init(&hpkt->mtx);
3424 TAILQ_INIT(&hpkt->queue);
3426 device_set_ivars(dev, hpkt);
3427 ctlr->allocate = ata_promise_mio_allocate;
3428 ctlr->reset = ata_promise_mio_reset;
3429 ctlr->dmainit = ata_promise_mio_dmainit;
3430 ctlr->setmode = ata_promise_setmode;
3435 /* mio type controllers need an interrupt intercept */
3436 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
3437 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3438 ata_promise_mio_intr, ctlr, &ctlr->handle, NULL)) {
3439 device_printf(dev, "unable to setup interrupt\n");
3443 switch (ctlr->chip->cfg2) {
3445 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
3446 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
3470 /* prime fake interrupt register */
3471 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3473 /* clear SATA status */
3474 ATA_OUTL(ctlr->r_res2, stat_reg, 0x000000ff);
3476 ctlr->allocate = ata_promise_mio_allocate;
3477 ctlr->reset = ata_promise_mio_reset;
3478 ctlr->dmainit = ata_promise_mio_dmainit;
3479 ctlr->setmode = ata_promise_mio_setmode;
3486 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
3488 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
3493 ata_promise_allocate(device_t dev)
3495 struct ata_channel *ch = device_get_softc(dev);
3497 if (ata_pci_allocate(dev))
3500 ch->hw.status = ata_promise_status;
3505 ata_promise_status(device_t dev)
3507 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3508 struct ata_channel *ch = device_get_softc(dev);
3510 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)) {
3511 return ata_pci_status(dev);
3517 ata_promise_dmastart(device_t dev)
3519 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3520 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3521 struct ata_device *atadev = device_get_softc(dev);
3523 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3524 ATA_OUTB(ctlr->r_res1, 0x11,
3525 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
3526 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20,
3527 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
3528 (ch->dma->cur_iosize >> 1));
3530 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
3531 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
3532 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
3533 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3534 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
3535 ATA_BMCMD_START_STOP);
3536 ch->flags |= ATA_DMA_ACTIVE;
3541 ata_promise_dmastop(device_t dev)
3543 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3544 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3545 struct ata_device *atadev = device_get_softc(dev);
3548 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3549 ATA_OUTB(ctlr->r_res1, 0x11,
3550 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
3551 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20, 0);
3553 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
3554 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3555 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3556 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3557 ch->flags &= ~ATA_DMA_ACTIVE;
3562 ata_promise_dmareset(device_t dev)
3564 struct ata_channel *ch = device_get_softc(dev);
3566 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3567 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3568 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3569 ch->flags &= ~ATA_DMA_ACTIVE;
3573 ata_promise_dmainit(device_t dev)
3575 struct ata_channel *ch = device_get_softc(dev);
3579 ch->dma->start = ata_promise_dmastart;
3580 ch->dma->stop = ata_promise_dmastop;
3581 ch->dma->reset = ata_promise_dmareset;
3586 ata_promise_setmode(device_t dev, int mode)
3588 device_t gparent = GRANDPARENT(dev);
3589 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3590 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3591 struct ata_device *atadev = device_get_softc(dev);
3592 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3594 u_int32_t timings[][2] = {
3595 /* PROLD PRNEW mode */
3596 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
3597 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
3598 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
3599 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
3600 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
3601 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
3602 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
3603 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
3604 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
3605 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
3606 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
3607 { 0, 0x00424ef6 }, /* UDMA 3 */
3608 { 0, 0x004127f3 }, /* UDMA 4 */
3609 { 0, 0x004127f3 } /* UDMA 5 */
3612 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3614 switch (ctlr->chip->cfg1) {
3617 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
3618 (ch->unit ? 1 << 11 : 1 << 10))) {
3619 ata_print_cable(dev, "controller");
3622 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
3623 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
3627 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3628 if (mode > ATA_UDMA2 &&
3629 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
3630 ata_print_cable(dev, "controller");
3636 if (mode > ATA_UDMA2 &&
3637 (ATA_INL(ctlr->r_res2,
3638 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
3639 (ch->unit << 7)) & 0x01000000)) {
3640 ata_print_cable(dev, "controller");
3646 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3649 device_printf(dev, "%ssetting %s on %s chip\n",
3650 (error) ? "FAILURE " : "",
3651 ata_mode2str(mode), ctlr->chip->text);
3653 if (ctlr->chip->cfg1 < PRTX)
3654 pci_write_config(gparent, 0x60 + (devno << 2),
3655 timings[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
3656 atadev->mode = mode;
3662 ata_promise_tx2_allocate(device_t dev)
3664 struct ata_channel *ch = device_get_softc(dev);
3666 if (ata_pci_allocate(dev))
3669 ch->hw.status = ata_promise_tx2_status;
3674 ata_promise_tx2_status(device_t dev)
3676 struct ata_channel *ch = device_get_softc(dev);
3678 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3679 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
3680 return ata_pci_status(dev);
3686 ata_promise_mio_allocate(device_t dev)
3688 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3689 struct ata_channel *ch = device_get_softc(dev);
3690 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
3693 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
3694 ch->r_io[i].res = ctlr->r_res2;
3695 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
3697 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
3698 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
3699 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
3700 ata_default_registers(dev);
3701 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
3702 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
3703 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3704 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
3705 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3706 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
3707 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3708 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
3709 ch->flags |= ATA_NO_SLAVE;
3711 ch->flags |= ATA_USE_16BIT;
3713 ata_generic_hw(dev);
3714 if (ctlr->chip->cfg2 & PRSX4X) {
3715 ch->hw.command = ata_promise_sx4_command;
3718 ch->hw.command = ata_promise_mio_command;
3719 ch->hw.status = ata_promise_mio_status;
3725 ata_promise_mio_intr(void *data)
3727 struct ata_pci_controller *ctlr = data;
3728 struct ata_channel *ch;
3732 switch (ctlr->chip->cfg2) {
3746 * since reading interrupt status register on early "mio" chips
3747 * clears the status bits we cannot read it for each channel later on
3748 * in the generic interrupt routine.
3749 * store the bits in an unused register in the chip so we can read
3750 * it from there safely to get around this "feature".
3752 vector = ATA_INL(ctlr->r_res2, 0x040);
3753 ATA_OUTL(ctlr->r_res2, 0x040, vector);
3754 ATA_OUTL(ctlr->r_res2, fake_reg, vector);
3756 for (unit = 0; unit < ctlr->channels; unit++) {
3757 if ((ch = ctlr->interrupt[unit].argument))
3758 ctlr->interrupt[unit].function(ch);
3761 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3765 ata_promise_mio_status(device_t dev)
3767 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3768 struct ata_channel *ch = device_get_softc(dev);
3769 struct ata_connect_task *tp;
3770 u_int32_t fake_reg, stat_reg, vector, status;
3772 switch (ctlr->chip->cfg2) {
3787 /* read and acknowledge interrupt */
3788 vector = ATA_INL(ctlr->r_res2, fake_reg);
3790 /* read and clear interface status */
3791 status = ATA_INL(ctlr->r_res2, stat_reg);
3792 ATA_OUTL(ctlr->r_res2, stat_reg, status & (0x00000011 << ch->unit));
3794 /* check for and handle disconnect events */
3795 if ((status & (0x00000001 << ch->unit)) &&
3796 (tp = (struct ata_connect_task *)
3797 kmalloc(sizeof(struct ata_connect_task),
3798 M_ATA, M_INTWAIT | M_ZERO))) {
3801 device_printf(ch->dev, "DISCONNECT requested\n");
3802 tp->action = ATA_C_DETACH;
3804 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3805 taskqueue_enqueue(taskqueue_thread[mycpuid], &tp->task);
3808 /* check for and handle connect events */
3809 if ((status & (0x00000010 << ch->unit)) &&
3810 (tp = (struct ata_connect_task *)
3811 kmalloc(sizeof(struct ata_connect_task),
3812 M_ATA, M_INTWAIT | M_ZERO))) {
3815 device_printf(ch->dev, "CONNECT requested\n");
3816 tp->action = ATA_C_ATTACH;
3818 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3819 taskqueue_enqueue(taskqueue_thread[mycpuid], &tp->task);
3822 /* do we have any device action ? */
3823 return (vector & (1 << (ch->unit + 1)));
3827 ata_promise_mio_command(struct ata_request *request)
3829 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
3830 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
3831 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
3833 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
3835 /* XXX SOS add ATAPI commands support later */
3836 switch (request->u.ata.command) {
3838 return ata_generic_command(request);
3841 case ATA_READ_DMA48:
3842 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
3846 case ATA_WRITE_DMA48:
3847 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
3850 wordp[1] = htole32(ch->dma->sg_bus);
3852 ata_promise_apkt((u_int8_t*)wordp, request);
3854 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
3859 ata_promise_mio_reset(device_t dev)
3861 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3862 struct ata_channel *ch = device_get_softc(dev);
3863 struct ata_promise_sx4 *hpktp;
3865 switch (ctlr->chip->cfg2) {
3868 /* softreset channel ATA module */
3869 hpktp = device_get_ivars(ctlr->dev);
3870 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
3872 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
3873 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
3874 ~0x00003f9f) | (ch->unit + 1));
3876 /* softreset HOST module */ /* XXX SOS what about other outstandings */
3877 spin_lock_wr(&hpktp->mtx);
3878 ATA_OUTL(ctlr->r_res2, 0xc012c,
3879 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
3881 ATA_OUTL(ctlr->r_res2, 0xc012c,
3882 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
3884 spin_unlock_wr(&hpktp->mtx);
3885 ata_generic_reset(dev);
3891 if ((ctlr->chip->cfg2 == PRSATA) ||
3892 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3894 /* mask plug/unplug intr */
3895 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
3898 /* softreset channels ATA module */
3899 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
3901 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
3902 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
3903 ~0x00003f9f) | (ch->unit + 1));
3905 if ((ctlr->chip->cfg2 == PRSATA) ||
3906 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
3908 if (ata_sata_phy_reset(dev))
3909 ata_generic_reset(dev);
3911 /* reset and enable plug/unplug intr */
3912 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
3915 ata_generic_reset(dev);
3920 if ((ctlr->chip->cfg2 == PRSATA2) ||
3921 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
3922 /* set portmultiplier port */
3923 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
3925 /* mask plug/unplug intr */
3926 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
3929 /* softreset channels ATA module */
3930 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
3932 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
3933 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
3934 ~0x00003f9f) | (ch->unit + 1));
3936 if ((ctlr->chip->cfg2 == PRSATA2) ||
3937 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
3939 /* set PHY mode to "improved" */
3940 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
3941 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
3942 ~0x00000003) | 0x00000001);
3944 if (ata_sata_phy_reset(dev))
3945 ata_generic_reset(dev);
3947 /* reset and enable plug/unplug intr */
3948 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
3950 /* set portmultiplier port */
3951 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
3954 ata_generic_reset(dev);
3961 ata_promise_mio_dmainit(device_t dev)
3963 /* note start and stop are not used here */
3968 ata_promise_mio_setmode(device_t dev, int mode)
3970 device_t gparent = GRANDPARENT(dev);
3971 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3972 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3974 if ( (ctlr->chip->cfg2 == PRSATA) ||
3975 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2)) ||
3976 (ctlr->chip->cfg2 == PRSATA2) ||
3977 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2)))
3978 ata_sata_setmode(dev, mode);
3980 ata_promise_setmode(dev, mode);
3984 ata_promise_sx4_intr(void *data)
3986 struct ata_pci_controller *ctlr = data;
3987 struct ata_channel *ch;
3988 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
3991 for (unit = 0; unit < ctlr->channels; unit++) {
3992 if (vector & (1 << (unit + 1)))
3993 if ((ch = ctlr->interrupt[unit].argument))
3994 ctlr->interrupt[unit].function(ch);
3995 if (vector & (1 << (unit + 5)))
3996 if ((ch = ctlr->interrupt[unit].argument))
3997 ata_promise_queue_hpkt(ctlr,
3998 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
3999 ATA_PDC_HPKT_OFFSET));
4000 if (vector & (1 << (unit + 9))) {
4001 ata_promise_next_hpkt(ctlr);
4002 if ((ch = ctlr->interrupt[unit].argument))
4003 ctlr->interrupt[unit].function(ch);
4005 if (vector & (1 << (unit + 13))) {
4006 ata_promise_next_hpkt(ctlr);
4007 if ((ch = ctlr->interrupt[unit].argument))
4008 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
4009 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
4010 ATA_PDC_APKT_OFFSET));
4016 ata_promise_sx4_command(struct ata_request *request)
4018 device_t gparent = GRANDPARENT(request->dev);
4019 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4020 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4021 struct ata_dma_prdentry *prd = ch->dma->sg;
4022 caddr_t window = rman_get_virtual(ctlr->r_res1);
4024 int i, idx, length = 0;
4026 /* XXX SOS add ATAPI commands support later */
4027 switch (request->u.ata.command) {
4032 case ATA_ATA_IDENTIFY:
4036 case ATA_READ_MUL48:
4040 case ATA_WRITE_MUL48:
4041 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
4042 return ata_generic_command(request);
4044 case ATA_SETFEATURES:
4045 case ATA_FLUSHCACHE:
4046 case ATA_FLUSHCACHE48:
4049 wordp = (u_int32_t *)
4050 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
4051 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
4054 ata_promise_apkt((u_int8_t *)wordp, request);
4055 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
4056 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
4057 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
4058 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
4062 case ATA_READ_DMA48:
4064 case ATA_WRITE_DMA48:
4065 wordp = (u_int32_t *)
4066 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
4069 wordp[idx++] = prd[i].addr;
4070 wordp[idx++] = prd[i].count;
4071 length += (prd[i].count & ~ATA_DMA_EOT);
4072 } while (!(prd[i++].count & ATA_DMA_EOT));
4074 wordp = (u_int32_t *)
4075 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
4076 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
4077 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
4079 wordp = (u_int32_t *)
4080 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
4081 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
4082 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
4084 wordp = (u_int32_t *)
4085 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
4086 if (request->flags & ATA_R_READ)
4087 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
4088 if (request->flags & ATA_R_WRITE)
4089 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
4090 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
4091 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
4094 wordp = (u_int32_t *)
4095 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
4096 if (request->flags & ATA_R_READ)
4097 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
4098 if (request->flags & ATA_R_WRITE)
4099 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
4100 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
4102 ata_promise_apkt((u_int8_t *)wordp, request);
4103 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
4105 if (request->flags & ATA_R_READ) {
4106 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
4107 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
4108 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
4109 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
4111 if (request->flags & ATA_R_WRITE) {
4112 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
4113 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
4114 ata_promise_queue_hpkt(ctlr,
4115 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
4122 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
4124 struct ata_device *atadev = device_get_softc(request->dev);
4127 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
4128 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
4129 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
4130 bytep[i++] = ATA_A_4BIT;
4132 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
4133 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
4134 bytep[i++] = request->u.ata.feature >> 8;
4135 bytep[i++] = request->u.ata.feature;
4136 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
4137 bytep[i++] = request->u.ata.count >> 8;
4138 bytep[i++] = request->u.ata.count;
4139 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
4140 bytep[i++] = request->u.ata.lba >> 24;
4141 bytep[i++] = request->u.ata.lba;
4142 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
4143 bytep[i++] = request->u.ata.lba >> 32;
4144 bytep[i++] = request->u.ata.lba >> 8;
4145 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
4146 bytep[i++] = request->u.ata.lba >> 40;
4147 bytep[i++] = request->u.ata.lba >> 16;
4148 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
4149 bytep[i++] = ATA_D_LBA | atadev->unit;
4152 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
4153 bytep[i++] = request->u.ata.feature;
4154 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
4155 bytep[i++] = request->u.ata.count;
4156 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
4157 bytep[i++] = request->u.ata.lba;
4158 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
4159 bytep[i++] = request->u.ata.lba >> 8;
4160 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
4161 bytep[i++] = request->u.ata.lba >> 16;
4162 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
4163 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
4164 ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
4166 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
4167 bytep[i++] = request->u.ata.command;
4172 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
4174 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
4176 spin_lock_wr(&hpktp->mtx);
4178 struct host_packet *hp =
4179 kmalloc(sizeof(struct host_packet), M_TEMP, M_INTWAIT | M_ZERO);
4181 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
4185 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
4187 spin_unlock_wr(&hpktp->mtx);
4191 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
4193 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
4194 struct host_packet *hp;
4196 spin_lock_wr(&hpktp->mtx);
4197 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
4198 TAILQ_REMOVE(&hpktp->queue, hp, chain);
4199 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
4204 spin_unlock_wr(&hpktp->mtx);
4209 * ServerWorks chipset support functions
4212 ata_serverworks_ident(device_t dev)
4214 struct ata_pci_controller *ctlr = device_get_softc(dev);
4215 struct ata_chip_id *idx;
4216 static struct ata_chip_id ids[] =
4217 {{ ATA_ROSB4, 0x00, SWKS33, 0, ATA_UDMA2, "ROSB4" },
4218 { ATA_CSB5, 0x92, SWKS100, 0, ATA_UDMA5, "CSB5" },
4219 { ATA_CSB5, 0x00, SWKS66, 0, ATA_UDMA4, "CSB5" },
4220 { ATA_CSB6, 0x00, SWKS100, 0, ATA_UDMA5, "CSB6" },
4221 { ATA_CSB6_1, 0x00, SWKS66, 0, ATA_UDMA4, "CSB6" },
4222 { ATA_HT1000, 0x00, SWKS100, 0, ATA_UDMA5, "HT1000" },
4223 { ATA_HT1000_S1, 0x00, SWKS100, 4, ATA_SA150, "HT1000" },
4224 { ATA_HT1000_S2, 0x00, SWKSMIO, 4, ATA_SA150, "HT1000" },
4225 { ATA_K2, 0x00, SWKSMIO, 4, ATA_SA150, "K2" },
4226 { ATA_FRODO4, 0x00, SWKSMIO, 4, ATA_SA150, "Frodo4" },
4227 { ATA_FRODO8, 0x00, SWKSMIO, 8, ATA_SA150, "Frodo8" },
4228 { 0, 0, 0, 0, 0, 0}};
4231 if (!(idx = ata_match_chip(dev, ids)))
4234 ksprintf(buffer, "ServerWorks %s %s controller",
4235 idx->text, ata_mode2str(idx->max_dma));
4236 device_set_desc_copy(dev, buffer);
4238 ctlr->chipinit = ata_serverworks_chipinit;
4243 ata_serverworks_chipinit(device_t dev)
4245 struct ata_pci_controller *ctlr = device_get_softc(dev);
4247 if (ata_setup_interrupt(dev))
4250 if (ctlr->chip->cfg1 == SWKSMIO) {
4251 ctlr->r_type2 = SYS_RES_MEMORY;
4252 ctlr->r_rid2 = PCIR_BAR(5);
4253 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4254 &ctlr->r_rid2, RF_ACTIVE)))
4257 ctlr->channels = ctlr->chip->cfg2;
4258 ctlr->allocate = ata_serverworks_allocate;
4259 ctlr->setmode = ata_sata_setmode;
4262 else if (ctlr->chip->cfg1 == SWKS33) {
4266 /* locate the ISA part in the southbridge and enable UDMA33 */
4267 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
4268 for (i = 0; i < nchildren; i++) {
4269 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
4270 pci_write_config(children[i], 0x64,
4271 (pci_read_config(children[i], 0x64, 4) &
4272 ~0x00002000) | 0x00004000, 4);
4276 kfree(children, M_TEMP);
4280 pci_write_config(dev, 0x5a,
4281 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
4282 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
4284 ctlr->setmode = ata_serverworks_setmode;
4289 ata_serverworks_allocate(device_t dev)
4291 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4292 struct ata_channel *ch = device_get_softc(dev);
4296 ch_offset = ch->unit * 0x100;
4298 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
4299 ch->r_io[i].res = ctlr->r_res2;
4301 /* setup ATA registers */
4302 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
4303 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x04;
4304 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
4305 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
4306 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
4307 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
4308 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
4309 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1c;
4310 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x20;
4311 ata_default_registers(dev);
4313 /* setup DMA registers */
4314 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x30;
4315 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x32;
4316 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x34;
4318 /* setup SATA registers */
4319 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x40;
4320 ch->r_io[ATA_SERROR].offset = ch_offset + 0x44;
4321 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x48;
4323 ch->flags |= ATA_NO_SLAVE;
4329 ata_serverworks_setmode(device_t dev, int mode)
4331 device_t gparent = GRANDPARENT(dev);
4332 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4333 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4334 struct ata_device *atadev = device_get_softc(dev);
4335 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4336 int offset = (devno ^ 0x01) << 3;
4338 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
4339 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
4340 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
4342 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4344 mode = ata_check_80pin(dev, mode);
4346 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4349 device_printf(dev, "%ssetting %s on %s chip\n",
4350 (error) ? "FAILURE " : "",
4351 ata_mode2str(mode), ctlr->chip->text);
4353 if (mode >= ATA_UDMA0) {
4354 pci_write_config(gparent, 0x56,
4355 (pci_read_config(gparent, 0x56, 2) &
4356 ~(0xf << (devno << 2))) |
4357 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
4358 pci_write_config(gparent, 0x54,
4359 pci_read_config(gparent, 0x54, 1) |
4360 (0x01 << devno), 1);
4361 pci_write_config(gparent, 0x44,
4362 (pci_read_config(gparent, 0x44, 4) &
4363 ~(0xff << offset)) |
4364 (dmatimings[2] << offset), 4);
4366 else if (mode >= ATA_WDMA0) {
4367 pci_write_config(gparent, 0x54,
4368 pci_read_config(gparent, 0x54, 1) &
4369 ~(0x01 << devno), 1);
4370 pci_write_config(gparent, 0x44,
4371 (pci_read_config(gparent, 0x44, 4) &
4372 ~(0xff << offset)) |
4373 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
4376 pci_write_config(gparent, 0x54,
4377 pci_read_config(gparent, 0x54, 1) &
4378 ~(0x01 << devno), 1);
4380 pci_write_config(gparent, 0x40,
4381 (pci_read_config(gparent, 0x40, 4) &
4382 ~(0xff << offset)) |
4383 (piotimings[ata_mode2idx(mode)] << offset), 4);
4384 atadev->mode = mode;
4390 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
4393 ata_sii_ident(device_t dev)
4395 struct ata_pci_controller *ctlr = device_get_softc(dev);
4396 struct ata_chip_id *idx;
4397 static struct ata_chip_id ids[] =
4398 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
4399 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
4400 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
4401 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
4402 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
4403 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
4404 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
4405 { ATA_SII3124, 0x00, SIIPRBIO, SII4CH, ATA_SA300, "SiI 3124" },
4406 { ATA_SII3132, 0x00, SIIPRBIO, 0, ATA_SA300, "SiI 3132" },
4407 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
4408 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
4409 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
4410 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
4411 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
4412 { 0, 0, 0, 0, 0, 0}};
4415 if (!(idx = ata_match_chip(dev, ids)))
4418 ksprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
4419 device_set_desc_copy(dev, buffer);
4421 ctlr->chipinit = ata_sii_chipinit;
4426 ata_sii_chipinit(device_t dev)
4428 struct ata_pci_controller *ctlr = device_get_softc(dev);
4430 if (ata_setup_interrupt(dev))
4433 switch (ctlr->chip->cfg1) {
4435 ctlr->r_type1 = SYS_RES_MEMORY;
4436 ctlr->r_rid1 = PCIR_BAR(0);
4437 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
4438 &ctlr->r_rid1, RF_ACTIVE)))
4441 ctlr->r_rid2 = PCIR_BAR(2);
4442 ctlr->r_type2 = SYS_RES_MEMORY;
4443 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4444 &ctlr->r_rid2, RF_ACTIVE))){
4445 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
4448 ctlr->allocate = ata_siiprb_allocate;
4449 ctlr->reset = ata_siiprb_reset;
4450 ctlr->dmainit = ata_siiprb_dmainit;
4451 ctlr->setmode = ata_sata_setmode;
4452 ctlr->channels = (ctlr->chip->cfg2 == SII4CH) ? 4 : 2;
4454 /* reset controller */
4455 ATA_OUTL(ctlr->r_res1, 0x0040, 0x80000000);
4457 ATA_OUTL(ctlr->r_res1, 0x0040, 0x0000000f);
4459 /* enable PCI interrupt */
4460 pci_write_config(dev, PCIR_COMMAND,
4461 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
4465 ctlr->r_type2 = SYS_RES_MEMORY;
4466 ctlr->r_rid2 = PCIR_BAR(5);
4467 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4468 &ctlr->r_rid2, RF_ACTIVE)))
4471 if (ctlr->chip->cfg2 & SIISETCLK) {
4472 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
4473 pci_write_config(dev, 0x8a,
4474 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
4475 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
4476 device_printf(dev, "%s could not set ATA133 clock\n",
4480 /* if we have 4 channels enable the second set */
4481 if (ctlr->chip->cfg2 & SII4CH) {
4482 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
4486 /* dont block interrupts from any channel */
4487 pci_write_config(dev, 0x48,
4488 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
4490 /* enable PCI interrupt as BIOS might not */
4491 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
4493 ctlr->allocate = ata_sii_allocate;
4494 if (ctlr->chip->max_dma >= ATA_SA150) {
4495 ctlr->reset = ata_sii_reset;
4496 ctlr->setmode = ata_sata_setmode;
4499 ctlr->setmode = ata_sii_setmode;
4503 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
4504 device_printf(dev, "HW has secondary channel disabled\n");
4508 /* enable interrupt as BIOS might not */
4509 pci_write_config(dev, 0x71, 0x01, 1);
4511 ctlr->allocate = ata_cmd_allocate;
4512 ctlr->setmode = ata_cmd_setmode;
4519 ata_cmd_allocate(device_t dev)
4521 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4522 struct ata_channel *ch = device_get_softc(dev);
4524 /* setup the usual register normal pci style */
4525 if (ata_pci_allocate(dev))
4528 if (ctlr->chip->cfg2 & SIIINTR)
4529 ch->hw.status = ata_cmd_status;
4535 ata_cmd_status(device_t dev)
4537 struct ata_channel *ch = device_get_softc(dev);
4540 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
4541 (ch->unit ? 0x08 : 0x04))) {
4542 pci_write_config(device_get_parent(ch->dev), 0x71,
4543 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
4544 return ata_pci_status(dev);
4550 ata_cmd_setmode(device_t dev, int mode)
4552 device_t gparent = GRANDPARENT(dev);
4553 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4554 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4555 struct ata_device *atadev = device_get_softc(dev);
4556 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4559 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4561 mode = ata_check_80pin(dev, mode);
4563 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4566 device_printf(dev, "%ssetting %s on %s chip\n",
4567 (error) ? "FAILURE " : "",
4568 ata_mode2str(mode), ctlr->chip->text);
4570 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
4571 int ureg = ch->unit ? 0x7b : 0x73;
4573 if (mode >= ATA_UDMA0) {
4574 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
4575 { 0x11, 0x42 }, { 0x25, 0x8a },
4576 { 0x15, 0x4a }, { 0x05, 0x0a } };
4578 u_int8_t umode = pci_read_config(gparent, ureg, 1);
4580 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
4581 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
4582 pci_write_config(gparent, ureg, umode, 1);
4584 else if (mode >= ATA_WDMA0) {
4585 int dmatimings[] = { 0x87, 0x32, 0x3f };
4587 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
4588 pci_write_config(gparent, ureg,
4589 pci_read_config(gparent, ureg, 1) &
4590 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4593 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
4594 pci_write_config(gparent, treg,
4595 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
4596 pci_write_config(gparent, ureg,
4597 pci_read_config(gparent, ureg, 1) &
4598 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4600 atadev->mode = mode;
4605 ata_sii_allocate(device_t dev)
4607 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4608 struct ata_channel *ch = device_get_softc(dev);
4609 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
4612 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
4613 ch->r_io[i].res = ctlr->r_res2;
4614 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
4616 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
4617 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
4618 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
4619 ata_default_registers(dev);
4621 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
4622 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
4623 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
4624 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
4625 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
4626 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
4628 if (ctlr->chip->max_dma >= ATA_SA150) {
4629 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4630 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
4631 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4632 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
4633 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4634 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
4635 ch->flags |= ATA_NO_SLAVE;
4637 /* enable PHY state change interrupt */
4638 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
4641 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
4642 /* work around errata in early chips */
4643 ch->dma->boundary = 16 * DEV_BSIZE;
4644 ch->dma->segsize = 15 * DEV_BSIZE;
4648 ch->hw.status = ata_sii_status;
4653 ata_sii_status(device_t dev)
4655 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4656 struct ata_channel *ch = device_get_softc(dev);
4657 int offset0 = ((ch->unit & 1) << 3) + ((ch->unit & 2) << 8);
4658 int offset1 = ((ch->unit & 1) << 6) + ((ch->unit & 2) << 8);
4660 /* do we have any PHY events ? */
4661 if (ctlr->chip->max_dma >= ATA_SA150 &&
4662 (ATA_INL(ctlr->r_res2, 0x10 + offset0) & 0x00000010))
4663 ata_sata_phy_check_events(dev);
4665 if (ATA_INL(ctlr->r_res2, 0xa0 + offset1) & 0x00000800)
4666 return ata_pci_status(dev);
4672 ata_sii_reset(device_t dev)
4674 if (ata_sata_phy_reset(dev))
4675 ata_generic_reset(dev);
4679 ata_sii_setmode(device_t dev, int mode)
4681 device_t gparent = GRANDPARENT(dev);
4682 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4683 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4684 struct ata_device *atadev = device_get_softc(dev);
4685 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
4686 int mreg = ch->unit ? 0x84 : 0x80;
4687 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
4688 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
4691 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4693 if (ctlr->chip->cfg2 & SIISETCLK) {
4694 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
4695 (ch->unit ? 0x02 : 0x01))) {
4696 ata_print_cable(dev, "controller");
4701 mode = ata_check_80pin(dev, mode);
4703 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4706 device_printf(dev, "%ssetting %s on %s chip\n",
4707 (error) ? "FAILURE " : "",
4708 ata_mode2str(mode), ctlr->chip->text);
4712 if (mode >= ATA_UDMA0) {
4713 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
4714 u_int8_t ureg = 0xac + rego;
4716 pci_write_config(gparent, mreg,
4717 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
4718 pci_write_config(gparent, ureg,
4719 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
4720 udmatimings[mode & ATA_MODE_MASK], 1);
4723 else if (mode >= ATA_WDMA0) {
4724 u_int8_t dreg = 0xa8 + rego;
4725 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
4727 pci_write_config(gparent, mreg,
4728 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
4729 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
4733 u_int8_t preg = 0xa4 + rego;
4734 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
4736 pci_write_config(gparent, mreg,
4737 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
4738 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
4740 atadev->mode = mode;
4743 struct ata_siiprb_dma_prdentry {
4749 struct ata_siiprb_ata_command {
4750 u_int32_t reserved0;
4751 struct ata_siiprb_dma_prdentry prd[126];
4754 struct ata_siiprb_atapi_command {
4756 struct ata_siiprb_dma_prdentry prd[125];
4759 struct ata_siiprb_command {
4761 u_int16_t protocol_override;
4762 u_int32_t transfer_count;
4765 struct ata_siiprb_ata_command ata;
4766 struct ata_siiprb_atapi_command atapi;
4771 ata_siiprb_allocate(device_t dev)
4773 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4774 struct ata_channel *ch = device_get_softc(dev);
4775 int offset = ch->unit * 0x2000;
4777 /* set the SATA resources */
4778 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4779 ch->r_io[ATA_SSTATUS].offset = 0x1f04 + offset;
4780 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4781 ch->r_io[ATA_SERROR].offset = 0x1f08 + offset;
4782 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4783 ch->r_io[ATA_SCONTROL].offset = 0x1f00 + offset;
4784 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
4785 ch->r_io[ATA_SACTIVE].offset = 0x1f0c + offset;
4787 ch->hw.begin_transaction = ata_siiprb_begin_transaction;
4788 ch->hw.end_transaction = ata_siiprb_end_transaction;
4789 ch->hw.status = ata_siiprb_status;
4790 ch->hw.command = NULL; /* not used here */
4795 ata_siiprb_status(device_t dev)
4797 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4798 struct ata_channel *ch = device_get_softc(dev);
4799 int offset = ch->unit * 0x2000;
4801 if ((ATA_INL(ctlr->r_res1, 0x0044) & (1 << ch->unit))) {
4802 u_int32_t istatus = ATA_INL(ctlr->r_res2, 0x1008 + offset);
4804 /* do we have any PHY events ? */
4805 ata_sata_phy_check_events(dev);
4807 /* clear interrupt(s) */
4808 ATA_OUTL(ctlr->r_res2, 0x1008 + offset, istatus);
4810 /* do we have any device action ? */
4811 return (istatus & 0x00000001);
4817 ata_siiprb_begin_transaction(struct ata_request *request)
4819 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
4820 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4821 struct ata_siiprb_command *prb;
4822 int offset = ch->unit * 0x2000;
4826 /* check for 48 bit access and convert if needed */
4827 ata_modify_if_48bit(request);
4829 /* get a piece of the workspace for this request */
4830 prb = (struct ata_siiprb_command *)
4831 (ch->dma->work + (sizeof(struct ata_siiprb_command) * tag));
4833 /* set basic prd options ata/atapi etc etc */
4834 bzero(prb, sizeof(struct ata_siiprb_command));
4836 /* setup the FIS for this request */
4837 if (!ata_request2fis_h2d(request, &prb->fis[0])) {
4838 device_printf(request->dev, "setting up SATA FIS failed\n");
4839 request->result = EIO;
4840 return ATA_OP_FINISHED;
4843 /* if request moves data setup and load SG list */
4844 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
4845 struct ata_siiprb_dma_prdentry *prd;
4847 if (request->flags & ATA_R_ATAPI)
4848 prd = &prb->u.atapi.prd[0];
4850 prd = &prb->u.ata.prd[0];
4851 if (ch->dma->load(ch->dev, request->data, request->bytecount,
4852 request->flags & ATA_R_READ, prd, &dummy)) {
4853 device_printf(request->dev, "setting up DMA failed\n");
4854 request->result = EIO;
4855 return ATA_OP_FINISHED;
4859 /* activate the prb */
4860 prb_bus = ch->dma->work_bus + (sizeof(struct ata_siiprb_command) * tag);
4861 ATA_OUTL(ctlr->r_res2,
4862 0x1c00 + offset + (tag * sizeof(u_int64_t)), prb_bus);
4863 ATA_OUTL(ctlr->r_res2,
4864 0x1c04 + offset + (tag * sizeof(u_int64_t)), prb_bus>>32);
4866 /* start the timeout */
4867 callout_reset(&request->callout, request->timeout * hz,
4868 (timeout_t*)ata_timeout, request);
4869 return ATA_OP_CONTINUES;
4873 ata_siiprb_end_transaction(struct ata_request *request)
4875 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
4876 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4877 struct ata_siiprb_command *prb;
4878 int offset = ch->unit * 0x2000;
4881 /* kill the timeout */
4882 callout_stop(&request->callout);
4884 prb = (struct ata_siiprb_command *)
4885 ((u_int8_t *)rman_get_virtual(ctlr->r_res2) + (tag << 7) + offset);
4887 /* if error status get details */
4888 request->status = prb->fis[2];
4889 if (request->status & ATA_S_ERROR)
4890 request->error = prb->fis[3];
4892 /* update progress */
4893 if (!(request->status & ATA_S_ERROR) && !(request->flags & ATA_R_TIMEOUT)) {
4894 if (request->flags & ATA_R_READ)
4895 request->donecount = prb->transfer_count;
4897 request->donecount = request->bytecount;
4900 /* any controller errors flagged ? */
4901 if ((error = ATA_INL(ctlr->r_res2, 0x1024 + offset))) {
4902 kprintf("ata_siiprb_end_transaction %s error=%08x\n",
4903 ata_cmd2str(request), error);
4906 /* release SG list etc */
4907 ch->dma->unload(ch->dev);
4909 return ATA_OP_FINISHED;
4913 ata_siiprb_reset(device_t dev)
4915 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4916 struct ata_channel *ch = device_get_softc(dev);
4917 int offset = ch->unit * 0x2000;
4918 struct ata_siiprb_command *prb;
4920 u_int32_t status, signature;
4921 int timeout, tag = 0;
4923 /* reset channel HW */
4924 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000001);
4926 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000001);
4929 /* poll for channel ready */
4930 for (timeout = 0; timeout < 1000; timeout++) {
4931 if ((status = ATA_INL(ctlr->r_res2, 0x1000 + offset)) & 0x00040000)
4935 if (timeout >= 1000) {
4936 device_printf(ch->dev, "channel HW reset timeout reset failure\n");
4941 device_printf(ch->dev, "channel HW reset time=%dms\n", timeout * 1);
4944 if (!ata_sata_phy_reset(dev)) {
4946 device_printf(ch->dev, "phy reset found no device\n");
4951 /* get a piece of the workspace for a soft reset request */
4952 prb = (struct ata_siiprb_command *)
4953 (ch->dma->work + (sizeof(struct ata_siiprb_command) * tag));
4954 bzero(prb, sizeof(struct ata_siiprb_command));
4955 prb->control = htole16(0x0080);
4957 /* activate the soft reset prb */
4958 prb_bus = ch->dma->work_bus + (sizeof(struct ata_siiprb_command) * tag);
4959 ATA_OUTL(ctlr->r_res2,
4960 0x1c00 + offset + (tag * sizeof(u_int64_t)), prb_bus);
4961 ATA_OUTL(ctlr->r_res2,
4962 0x1c04 + offset + (tag * sizeof(u_int64_t)), prb_bus>>32);
4964 /* poll for channel ready */
4965 for (timeout = 0; timeout < 1000; timeout++) {
4967 if ((status = ATA_INL(ctlr->r_res2, 0x1008 + offset)) & 0x00010000)
4970 if (timeout >= 1000) {
4971 device_printf(ch->dev, "reset timeout - no device found\n");
4976 device_printf(ch->dev, "soft reset exec time=%dms status=%08x\n",
4979 /* find out whats there */
4980 prb = (struct ata_siiprb_command *)
4981 ((u_int8_t *)rman_get_virtual(ctlr->r_res2) + (tag << 7) + offset);
4983 prb->fis[12]|(prb->fis[4]<<8)|(prb->fis[5]<<16)|(prb->fis[6]<<24);
4985 device_printf(ch->dev, "signature=%08x\n", signature);
4986 switch (signature) {
4988 ch->devices = ATA_ATAPI_MASTER;
4989 device_printf(ch->dev, "SATA ATAPI devices not supported yet\n");
4993 ch->devices = ATA_PORTMULTIPLIER;
4994 device_printf(ch->dev, "Portmultipliers not supported yet\n");
4998 ch->devices = ATA_ATA_MASTER;
5005 /* clear interrupt(s) */
5006 ATA_OUTL(ctlr->r_res2, 0x1008 + offset, 0x000008ff);
5008 /* require explicit interrupt ack */
5009 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000008);
5012 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000400);
5014 /* enable interrupts wanted */
5015 ATA_OUTL(ctlr->r_res2, 0x1010 + offset, 0x000000ff);
5019 ata_siiprb_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
5021 struct ata_dmasetprd_args *args = xsc;
5022 struct ata_siiprb_dma_prdentry *prd = args->dmatab;
5025 if ((args->error = error))
5028 for (i = 0; i < nsegs; i++) {
5029 prd[i].addr = htole64(segs[i].ds_addr);
5030 prd[i].count = htole32(segs[i].ds_len);
5032 prd[i - 1].control = htole32(ATA_DMA_EOT);
5036 ata_siiprb_dmainit(device_t dev)
5038 struct ata_channel *ch = device_get_softc(dev);
5042 /* note start and stop are not used here */
5043 ch->dma->setprd = ata_siiprb_dmasetprd;
5044 ch->dma->max_address = BUS_SPACE_MAXADDR;
5050 * Silicon Integrated Systems Corp. (SiS) chipset support functions
5053 ata_sis_ident(device_t dev)
5055 struct ata_pci_controller *ctlr = device_get_softc(dev);
5056 struct ata_chip_id *idx;
5057 static struct ata_chip_id ids[] =
5058 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "182" }, /* south */
5059 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "181" }, /* south */
5060 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "180" }, /* south */
5061 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "965" }, /* south */
5062 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "964" }, /* south */
5063 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "963" }, /* south */
5064 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "962" }, /* south */
5066 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "745" }, /* 1chip */
5067 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "735" }, /* 1chip */
5068 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "733" }, /* 1chip */
5069 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "730" }, /* 1chip */
5071 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "635" }, /* 1chip */
5072 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "633" }, /* unknown */
5073 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "630S"}, /* 1chip */
5074 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "630" }, /* 1chip */
5075 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "620" }, /* 1chip */
5077 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "550" },
5078 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "540" },
5079 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "530" },
5081 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "5513" },
5082 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "5513" },
5083 { 0, 0, 0, 0, 0, 0 }};
5087 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
5090 if (idx->cfg2 && !found) {
5091 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
5093 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
5094 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
5096 idx->cfg1 = SIS133NEW;
5097 idx->max_dma = ATA_UDMA6;
5098 ksprintf(buffer, "SiS 962/963 %s controller",
5099 ata_mode2str(idx->max_dma));
5101 pci_write_config(dev, 0x57, reg57, 1);
5103 if (idx->cfg2 && !found) {
5104 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
5106 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
5107 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
5108 struct ata_chip_id id[] =
5109 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
5112 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
5113 idx->cfg1 = SIS133OLD;
5114 idx->max_dma = ATA_UDMA6;
5117 idx->cfg1 = SIS100NEW;
5118 idx->max_dma = ATA_UDMA5;
5120 ksprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
5122 pci_write_config(dev, 0x4a, reg4a, 1);
5125 ksprintf(buffer,"SiS %s %s controller",
5126 idx->text, ata_mode2str(idx->max_dma));
5128 device_set_desc_copy(dev, buffer);
5130 ctlr->chipinit = ata_sis_chipinit;
5135 ata_sis_chipinit(device_t dev)
5137 struct ata_pci_controller *ctlr = device_get_softc(dev);
5139 if (ata_setup_interrupt(dev))
5142 switch (ctlr->chip->cfg1) {
5147 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
5151 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
5154 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
5155 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
5158 ctlr->r_type2 = SYS_RES_IOPORT;
5159 ctlr->r_rid2 = PCIR_BAR(5);
5160 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
5161 &ctlr->r_rid2, RF_ACTIVE))) {
5162 ctlr->allocate = ata_sis_allocate;
5163 ctlr->reset = ata_sis_reset;
5165 /* enable PCI interrupt */
5166 pci_write_config(dev, PCIR_COMMAND,
5167 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
5169 ctlr->setmode = ata_sata_setmode;
5174 ctlr->setmode = ata_sis_setmode;
5179 ata_sis_allocate(device_t dev)
5181 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5182 struct ata_channel *ch = device_get_softc(dev);
5183 int offset = ch->unit << ((ctlr->chip->chipid == ATA_SIS182) ? 5 : 6);
5185 /* setup the usual register normal pci style */
5186 if (ata_pci_allocate(dev))
5189 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
5190 ch->r_io[ATA_SSTATUS].offset = 0x00 + offset;
5191 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
5192 ch->r_io[ATA_SERROR].offset = 0x04 + offset;
5193 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
5194 ch->r_io[ATA_SCONTROL].offset = 0x08 + offset;
5195 ch->flags |= ATA_NO_SLAVE;
5197 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
5198 /* XXX SOS unknown how to enable PHY state change interrupt */
5203 ata_sis_reset(device_t dev)
5205 if (ata_sata_phy_reset(dev))
5206 ata_generic_reset(dev);
5210 ata_sis_setmode(device_t dev, int mode)
5212 device_t gparent = GRANDPARENT(dev);
5213 struct ata_pci_controller *ctlr = device_get_softc(gparent);
5214 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5215 struct ata_device *atadev = device_get_softc(dev);
5216 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
5219 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
5221 if (ctlr->chip->cfg1 == SIS133NEW) {
5222 if (mode > ATA_UDMA2 &&
5223 pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
5224 ata_print_cable(dev, "controller");
5229 if (mode > ATA_UDMA2 &&
5230 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
5231 ata_print_cable(dev, "controller");
5236 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5239 device_printf(dev, "%ssetting %s on %s chip\n",
5240 (error) ? "FAILURE " : "",
5241 ata_mode2str(mode), ctlr->chip->text);
5243 switch (ctlr->chip->cfg1) {
5245 u_int32_t timings[] =
5246 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
5247 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
5248 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
5251 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
5252 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
5256 u_int16_t timings[] =
5257 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
5258 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
5260 u_int16_t reg = 0x40 + (devno << 1);
5262 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5266 u_int16_t timings[] =
5267 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
5268 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
5269 u_int16_t reg = 0x40 + (devno << 1);
5271 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5277 u_int16_t timings[] =
5278 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
5279 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
5280 u_int16_t reg = 0x40 + (devno << 1);
5282 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5286 atadev->mode = mode;
5291 /* VIA Technologies Inc. chipset support functions */
5293 ata_via_ident(device_t dev)
5295 struct ata_pci_controller *ctlr = device_get_softc(dev);
5296 struct ata_chip_id *idx;
5297 static struct ata_chip_id ids[] =
5298 {{ ATA_VIA82C586, 0x02, VIA33, 0x00, ATA_UDMA2, "82C586B" },
5299 { ATA_VIA82C586, 0x00, VIA33, 0x00, ATA_WDMA2, "82C586" },
5300 { ATA_VIA82C596, 0x12, VIA66, VIACLK, ATA_UDMA4, "82C596B" },
5301 { ATA_VIA82C596, 0x00, VIA33, 0x00, ATA_UDMA2, "82C596" },
5302 { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "82C686B"},
5303 { ATA_VIA82C686, 0x10, VIA66, VIACLK, ATA_UDMA4, "82C686A" },
5304 { ATA_VIA82C686, 0x00, VIA33, 0x00, ATA_UDMA2, "82C686" },
5305 { ATA_VIA8231, 0x00, VIA100, VIABUG, ATA_UDMA5, "8231" },
5306 { ATA_VIA8233, 0x00, VIA100, 0x00, ATA_UDMA5, "8233" },
5307 { ATA_VIA8233C, 0x00, VIA100, 0x00, ATA_UDMA5, "8233C" },
5308 { ATA_VIA8233A, 0x00, VIA133, 0x00, ATA_UDMA6, "8233A" },
5309 { ATA_VIA8235, 0x00, VIA133, 0x00, ATA_UDMA6, "8235" },
5310 { ATA_VIA8237, 0x00, VIA133, 0x00, ATA_UDMA6, "8237" },
5311 { ATA_VIA8237A, 0x00, VIA133, 0x00, ATA_UDMA6, "8237A" },
5312 { ATA_VIA8251, 0x00, VIA133, 0x00, ATA_UDMA6, "8251" },
5313 { 0, 0, 0, 0, 0, 0 }};
5314 static struct ata_chip_id new_ids[] =
5315 {{ ATA_VIA6410, 0x00, 0, 0x00, ATA_UDMA6, "6410" },
5316 { ATA_VIA6420, 0x00, 7, 0x00, ATA_SA150, "6420" },
5317 { ATA_VIA6421, 0x00, 6, VIABAR, ATA_SA150, "6421" },
5318 { ATA_VIA8237A, 0x00, 7, 0x00, ATA_SA150, "8237A" },
5319 { ATA_VIA8251, 0x00, 0, VIAAHCI, ATA_SA300, "8251" },
5320 { 0, 0, 0, 0, 0, 0 }};
5323 if (pci_get_devid(dev) == ATA_VIA82C571) {
5324 if (!(idx = ata_find_chip(dev, ids, -99)))
5328 if (!(idx = ata_match_chip(dev, new_ids)))
5332 ksprintf(buffer, "VIA %s %s controller",
5333 idx->text, ata_mode2str(idx->max_dma));
5334 device_set_desc_copy(dev, buffer);
5336 ctlr->chipinit = ata_via_chipinit;
5341 ata_via_chipinit(device_t dev)
5343 struct ata_pci_controller *ctlr = device_get_softc(dev);
5345 if (ata_setup_interrupt(dev))
5348 if (ctlr->chip->max_dma >= ATA_SA150) {
5349 if (ctlr->chip->cfg2 == VIAAHCI) {
5350 ctlr->r_type2 = SYS_RES_MEMORY;
5351 ctlr->r_rid2 = PCIR_BAR(5);
5352 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
5355 return ata_ahci_chipinit(dev);
5358 ctlr->r_type2 = SYS_RES_IOPORT;
5359 ctlr->r_rid2 = PCIR_BAR(5);
5360 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
5361 &ctlr->r_rid2, RF_ACTIVE))) {
5362 ctlr->allocate = ata_via_allocate;
5363 ctlr->reset = ata_via_reset;
5365 /* enable PCI interrupt */
5366 pci_write_config(dev, PCIR_COMMAND,
5367 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
5370 if (ctlr->chip->cfg2 & VIABAR) {
5372 ctlr->setmode = ata_via_setmode;
5375 ctlr->setmode = ata_sata_setmode;
5379 /* prepare for ATA-66 on the 82C686a and 82C596b */
5380 if (ctlr->chip->cfg2 & VIACLK)
5381 pci_write_config(dev, 0x50, 0x030b030b, 4);
5383 /* the southbridge might need the data corruption fix */
5384 if (ctlr->chip->cfg2 & VIABUG)
5385 ata_via_southbridge_fixup(dev);
5387 /* set fifo configuration half'n'half */
5388 pci_write_config(dev, 0x43,
5389 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
5391 /* set status register read retry */
5392 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
5394 /* set DMA read & end-of-sector fifo flush */
5395 pci_write_config(dev, 0x46,
5396 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
5398 /* set sector size */
5399 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
5400 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
5402 ctlr->setmode = ata_via_family_setmode;
5407 ata_via_allocate(device_t dev)
5409 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5410 struct ata_channel *ch = device_get_softc(dev);
5412 /* newer SATA chips has resources in one BAR for each channel */
5413 if (ctlr->chip->cfg2 & VIABAR) {
5414 struct resource *r_io;
5417 rid = PCIR_BAR(ch->unit);
5418 if (!(r_io = bus_alloc_resource_any(device_get_parent(dev),
5423 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
5424 ch->r_io[i].res = r_io;
5425 ch->r_io[i].offset = i;
5427 ch->r_io[ATA_CONTROL].res = r_io;
5428 ch->r_io[ATA_CONTROL].offset = 2 + ATA_IOSIZE;
5429 ch->r_io[ATA_IDX_ADDR].res = r_io;
5430 ata_default_registers(dev);
5431 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
5432 ch->r_io[i].res = ctlr->r_res1;
5433 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
5440 /* setup the usual register normal pci style */
5441 if (ata_pci_allocate(dev))
5445 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
5446 ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
5447 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
5448 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
5449 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
5450 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
5451 ch->flags |= ATA_NO_SLAVE;
5453 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
5454 /* XXX SOS unknown how to enable PHY state change interrupt */
5459 ata_via_reset(device_t dev)
5461 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5462 struct ata_channel *ch = device_get_softc(dev);
5464 if ((ctlr->chip->cfg2 & VIABAR) && (ch->unit > 1))
5465 ata_generic_reset(dev);
5467 if (ata_sata_phy_reset(dev))
5468 ata_generic_reset(dev);
5472 ata_via_setmode(device_t dev, int mode)
5474 device_t gparent = GRANDPARENT(dev);
5475 struct ata_pci_controller *ctlr = device_get_softc(gparent);
5476 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5477 struct ata_device *atadev = device_get_softc(dev);
5480 if ((ctlr->chip->cfg2 & VIABAR) && (ch->unit > 1)) {
5481 u_int8_t pio_timings[] = { 0xa8, 0x65, 0x65, 0x32, 0x20,
5483 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
5484 u_int8_t dma_timings[] = { 0xee, 0xe8, 0xe6, 0xe4, 0xe2, 0xe1, 0xe0 };
5486 mode = ata_check_80pin(dev, ata_limit_mode(dev, mode, ATA_UDMA6));
5487 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5489 device_printf(dev, "%ssetting %s on %s chip\n",
5490 (error) ? "FAILURE " : "", ata_mode2str(mode),
5493 pci_write_config(gparent, 0xab, pio_timings[ata_mode2idx(mode)], 1);
5494 if (mode >= ATA_UDMA0)
5495 pci_write_config(gparent, 0xb3,
5496 dma_timings[mode & ATA_MODE_MASK], 1);
5497 atadev->mode = mode;
5501 ata_sata_setmode(dev, mode);
5505 ata_via_southbridge_fixup(device_t dev)
5510 if (device_get_children(device_get_parent(dev), &children, &nchildren))
5513 for (i = 0; i < nchildren; i++) {
5514 if (pci_get_devid(children[i]) == ATA_VIA8363 ||
5515 pci_get_devid(children[i]) == ATA_VIA8371 ||
5516 pci_get_devid(children[i]) == ATA_VIA8662 ||
5517 pci_get_devid(children[i]) == ATA_VIA8361) {
5518 u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
5520 if ((reg76 & 0xf0) != 0xd0) {
5522 "Correcting VIA config for southbridge data corruption bug\n");
5523 pci_write_config(children[i], 0x75, 0x80, 1);
5524 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
5529 kfree(children, M_TEMP);
5533 /* common code for VIA, AMD & nVidia */
5535 ata_via_family_setmode(device_t dev, int mode)
5537 device_t gparent = GRANDPARENT(dev);
5538 struct ata_pci_controller *ctlr = device_get_softc(gparent);
5539 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5540 struct ata_device *atadev = device_get_softc(dev);
5541 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
5542 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
5544 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* VIA ATA33 */
5545 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* VIA ATA66 */
5546 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* VIA ATA100 */
5547 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
5548 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }}; /* AMD/nVIDIA */
5549 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
5550 int reg = 0x53 - devno;
5553 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
5555 if (ctlr->chip->cfg2 & AMDCABLE) {
5556 if (mode > ATA_UDMA2 &&
5557 !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
5558 ata_print_cable(dev, "controller");
5563 mode = ata_check_80pin(dev, mode);
5565 if (ctlr->chip->cfg2 & NVIDIA)
5568 if (ctlr->chip->cfg1 != VIA133)
5569 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
5571 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5574 device_printf(dev, "%ssetting %s on %s chip\n",
5575 (error) ? "FAILURE " : "", ata_mode2str(mode),
5578 if (mode >= ATA_UDMA0)
5579 pci_write_config(gparent, reg,
5580 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
5582 pci_write_config(gparent, reg, 0x8b, 1);
5583 atadev->mode = mode;
5588 /* misc functions */
5589 static struct ata_chip_id *
5590 ata_match_chip(device_t dev, struct ata_chip_id *index)
5592 while (index->chipid != 0) {
5593 if (pci_get_devid(dev) == index->chipid &&
5594 pci_get_revid(dev) >= index->chiprev)
5601 static struct ata_chip_id *
5602 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
5607 if (device_get_children(device_get_parent(dev), &children, &nchildren))
5610 while (index->chipid != 0) {
5611 for (i = 0; i < nchildren; i++) {
5612 if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
5613 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
5614 pci_get_devid(children[i]) == index->chipid &&
5615 pci_get_revid(children[i]) >= index->chiprev) {
5616 kfree(children, M_TEMP);
5622 kfree(children, M_TEMP);
5627 ata_setup_interrupt(device_t dev)
5629 struct ata_pci_controller *ctlr = device_get_softc(dev);
5630 int rid = ATA_IRQ_RID;
5632 if (!ata_legacy(dev)) {
5633 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
5634 RF_SHAREABLE | RF_ACTIVE))) {
5635 device_printf(dev, "unable to map interrupt\n");
5638 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
5639 ata_generic_intr, ctlr, &ctlr->handle, NULL))) {
5640 device_printf(dev, "unable to setup interrupt\n");
5647 struct ata_serialize {
5648 struct spinlock locked_mtx;
5654 ata_serialize(device_t dev, int flags)
5656 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5657 struct ata_channel *ch = device_get_softc(dev);
5658 struct ata_serialize *serial;
5659 static int inited = 0;
5663 serial = kmalloc(sizeof(struct ata_serialize),
5664 M_TEMP, M_INTWAIT | M_ZERO);
5665 spin_init(&serial->locked_mtx);
5666 serial->locked_ch = -1;
5667 serial->restart_ch = -1;
5668 device_set_ivars(ctlr->dev, serial);
5672 serial = device_get_ivars(ctlr->dev);
5674 spin_lock_wr(&serial->locked_mtx);
5677 if (serial->locked_ch == -1)
5678 serial->locked_ch = ch->unit;
5679 if (serial->locked_ch != ch->unit)
5680 serial->restart_ch = ch->unit;
5684 if (serial->locked_ch == ch->unit) {
5685 serial->locked_ch = -1;
5686 if (serial->restart_ch != -1) {
5687 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
5688 serial->restart_ch = -1;
5689 spin_unlock_wr(&serial->locked_mtx);
5700 res = serial->locked_ch;
5701 spin_unlock_wr(&serial->locked_mtx);
5706 ata_print_cable(device_t dev, u_int8_t *who)
5709 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
5713 ata_atapi(device_t dev)
5715 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5716 struct ata_device *atadev = device_get_softc(dev);
5718 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
5719 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
5723 ata_check_80pin(device_t dev, int mode)
5725 struct ata_device *atadev = device_get_softc(dev);
5727 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
5728 ata_print_cable(dev, "device");
5735 ata_mode2idx(int mode)
5737 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
5738 return (mode & ATA_MODE_MASK) + 8;
5739 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
5740 return (mode & ATA_MODE_MASK) + 5;
5741 return (mode & ATA_MODE_MASK) - ATA_PIO0;