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.15 2008/07/12 16:38:10 mneumann 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/systm.h>
46 #include <sys/taskqueue.h>
47 #include <sys/machintr.h>
49 #include <sys/spinlock2.h>
50 #include <sys/mplock2.h>
52 #include <machine/bus_dma.h>
54 #include <bus/pci/pcireg.h>
55 #include <bus/pci/pcivar.h>
61 /* local prototypes */
63 static int ata_generic_chipinit(device_t dev);
64 static void ata_generic_intr(void *data);
65 static void ata_generic_setmode(device_t dev, int mode);
66 static void ata_sata_phy_check_events(device_t dev);
67 static void ata_sata_phy_event(void *context, int dummy);
68 static int ata_sata_phy_reset(device_t dev);
69 static int ata_sata_connect(struct ata_channel *ch);
70 static void ata_sata_setmode(device_t dev, int mode);
71 static int ata_request2fis_h2d(struct ata_request *request, u_int8_t *fis);
72 static int ata_ahci_chipinit(device_t dev);
73 static int ata_ahci_allocate(device_t dev);
74 static int ata_ahci_status(device_t dev);
75 static int ata_ahci_begin_transaction(struct ata_request *request);
76 static int ata_ahci_end_transaction(struct ata_request *request);
77 static void ata_ahci_reset(device_t dev);
78 static void ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
79 static void ata_ahci_dmainit(device_t dev);
80 static int ata_ahci_setup_fis(struct ata_ahci_cmd_tab *ctp, struct ata_request *request);
81 static int ata_acard_chipinit(device_t dev);
82 static int ata_acard_allocate(device_t dev);
83 static int ata_acard_status(device_t dev);
84 static void ata_acard_850_setmode(device_t dev, int mode);
85 static void ata_acard_86X_setmode(device_t dev, int mode);
86 static int ata_ali_chipinit(device_t dev);
87 static int ata_ali_allocate(device_t dev);
88 static int ata_ali_sata_allocate(device_t dev);
89 static void ata_ali_reset(device_t dev);
90 static void ata_ali_setmode(device_t dev, int mode);
91 static int ata_amd_chipinit(device_t dev);
92 static int ata_ati_chipinit(device_t dev);
93 static void ata_ati_setmode(device_t dev, int mode);
94 static int ata_cyrix_chipinit(device_t dev);
95 static void ata_cyrix_setmode(device_t dev, int mode);
96 static int ata_cypress_chipinit(device_t dev);
97 static void ata_cypress_setmode(device_t dev, int mode);
98 static int ata_highpoint_chipinit(device_t dev);
99 static int ata_highpoint_allocate(device_t dev);
100 static void ata_highpoint_setmode(device_t dev, int mode);
101 static int ata_highpoint_check_80pin(device_t dev, int mode);
102 static int ata_intel_chipinit(device_t dev);
103 static int ata_intel_allocate(device_t dev);
104 static void ata_intel_reset(device_t dev);
105 static void ata_intel_old_setmode(device_t dev, int mode);
106 static void ata_intel_new_setmode(device_t dev, int mode);
107 static void ata_intel_sata_setmode(device_t dev, int mode);
108 static int ata_intel_31244_allocate(device_t dev);
109 static int ata_intel_31244_status(device_t dev);
110 static int ata_intel_31244_command(struct ata_request *request);
111 static void ata_intel_31244_reset(device_t dev);
112 static int ata_ite_chipinit(device_t dev);
113 static void ata_ite_setmode(device_t dev, int mode);
114 static int ata_jmicron_chipinit(device_t dev);
115 static int ata_jmicron_allocate(device_t dev);
116 static void ata_jmicron_reset(device_t dev);
117 static void ata_jmicron_dmainit(device_t dev);
118 static void ata_jmicron_setmode(device_t dev, int mode);
119 static int ata_marvell_pata_chipinit(device_t dev);
120 static int ata_marvell_pata_allocate(device_t dev);
121 static void ata_marvell_pata_setmode(device_t dev, int mode);
122 static int ata_marvell_edma_chipinit(device_t dev);
123 static int ata_marvell_edma_allocate(device_t dev);
124 static int ata_marvell_edma_status(device_t dev);
125 static int ata_marvell_edma_begin_transaction(struct ata_request *request);
126 static int ata_marvell_edma_end_transaction(struct ata_request *request);
127 static void ata_marvell_edma_reset(device_t dev);
128 static void ata_marvell_edma_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
129 static void ata_marvell_edma_dmainit(device_t dev);
130 static int ata_national_chipinit(device_t dev);
131 static void ata_national_setmode(device_t dev, int mode);
132 static int ata_netcell_chipinit(device_t dev);
133 static int ata_netcell_allocate(device_t dev);
134 static int ata_nvidia_chipinit(device_t dev);
135 static int ata_nvidia_allocate(device_t dev);
136 static int ata_nvidia_status(device_t dev);
137 static void ata_nvidia_reset(device_t dev);
138 static int ata_promise_chipinit(device_t dev);
139 static int ata_promise_allocate(device_t dev);
140 static int ata_promise_status(device_t dev);
141 static int ata_promise_dmastart(device_t dev);
142 static int ata_promise_dmastop(device_t dev);
143 static void ata_promise_dmareset(device_t dev);
144 static void ata_promise_dmainit(device_t dev);
145 static void ata_promise_setmode(device_t dev, int mode);
146 static int ata_promise_tx2_allocate(device_t dev);
147 static int ata_promise_tx2_status(device_t dev);
148 static int ata_promise_mio_allocate(device_t dev);
149 static void ata_promise_mio_intr(void *data);
150 static int ata_promise_mio_status(device_t dev);
151 static int ata_promise_mio_command(struct ata_request *request);
152 static void ata_promise_mio_reset(device_t dev);
153 static void ata_promise_mio_dmainit(device_t dev);
154 static void ata_promise_mio_setmode(device_t dev, int mode);
155 static void ata_promise_sx4_intr(void *data);
156 static int ata_promise_sx4_command(struct ata_request *request);
157 static int ata_promise_apkt(u_int8_t *bytep, struct ata_request *request);
158 static void ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt);
159 static void ata_promise_next_hpkt(struct ata_pci_controller *ctlr);
160 static int ata_serverworks_chipinit(device_t dev);
161 static int ata_serverworks_allocate(device_t dev);
162 static void ata_serverworks_setmode(device_t dev, int mode);
163 static int ata_sii_chipinit(device_t dev);
164 static int ata_cmd_allocate(device_t dev);
165 static int ata_cmd_status(device_t dev);
166 static void ata_cmd_setmode(device_t dev, int mode);
167 static int ata_sii_allocate(device_t dev);
168 static int ata_sii_status(device_t dev);
169 static void ata_sii_reset(device_t dev);
170 static void ata_sii_setmode(device_t dev, int mode);
171 static int ata_siiprb_allocate(device_t dev);
172 static int ata_siiprb_status(device_t dev);
173 static int ata_siiprb_begin_transaction(struct ata_request *request);
174 static int ata_siiprb_end_transaction(struct ata_request *request);
175 static void ata_siiprb_reset(device_t dev);
176 static void ata_siiprb_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
177 static void ata_siiprb_dmainit(device_t dev);
178 static int ata_sis_chipinit(device_t dev);
179 static int ata_sis_allocate(device_t dev);
180 static void ata_sis_reset(device_t dev);
181 static void ata_sis_setmode(device_t dev, int mode);
182 static int ata_via_chipinit(device_t dev);
183 static int ata_via_allocate(device_t dev);
184 static void ata_via_reset(device_t dev);
185 static void ata_via_setmode(device_t dev, int mode);
186 static void ata_via_southbridge_fixup(device_t dev);
187 static void ata_via_family_setmode(device_t dev, int mode);
188 static struct ata_chip_id *ata_match_chip(device_t dev, struct ata_chip_id *index);
189 static struct ata_chip_id *ata_find_chip(device_t dev, struct ata_chip_id *index, int slot);
190 static int ata_setup_interrupt(device_t dev);
191 static void ata_teardown_interrupt(device_t dev);
192 static int ata_serialize(device_t dev, int flags);
193 static void ata_print_cable(device_t dev, u_int8_t *who);
194 static int ata_atapi(device_t dev);
195 static int ata_check_80pin(device_t dev, int mode);
196 static int ata_mode2idx(int mode);
200 * generic ATA support functions
203 ata_generic_ident(device_t dev)
205 struct ata_pci_controller *ctlr = device_get_softc(dev);
208 ksnprintf(buffer, sizeof(buffer),
209 "%s ATA controller", ata_pcivendor2str(dev));
210 device_set_desc_copy(dev, buffer);
211 ctlr->chipinit = ata_generic_chipinit;
216 ata_generic_chipinit(device_t dev)
218 struct ata_pci_controller *ctlr = device_get_softc(dev);
220 if (ata_setup_interrupt(dev))
222 ctlr->setmode = ata_generic_setmode;
227 ata_generic_intr(void *data)
229 struct ata_pci_controller *ctlr = data;
230 struct ata_channel *ch;
233 for (unit = 0; unit < ctlr->channels; unit++) {
234 if ((ch = ctlr->interrupt[unit].argument))
235 ctlr->interrupt[unit].function(ch);
240 ata_generic_setmode(device_t dev, int mode)
242 struct ata_device *atadev = device_get_softc(dev);
244 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
245 mode = ata_check_80pin(dev, mode);
246 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
252 * SATA support functions
255 ata_sata_phy_check_events(device_t dev)
257 struct ata_channel *ch = device_get_softc(dev);
258 u_int32_t error = ATA_IDX_INL(ch, ATA_SERROR);
260 /* clear error bits/interrupt */
261 ATA_IDX_OUTL(ch, ATA_SERROR, error);
263 /* do we have any events flagged ? */
265 struct ata_connect_task *tp;
266 u_int32_t status = ATA_IDX_INL(ch, ATA_SSTATUS);
268 /* if we have a connection event deal with it */
269 if ((error & ATA_SE_PHY_CHANGED) &&
270 (tp = (struct ata_connect_task *)
271 kmalloc(sizeof(struct ata_connect_task),
272 M_ATA, M_INTWAIT | M_ZERO))) {
274 if (((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1) ||
275 ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)) {
277 device_printf(ch->dev, "CONNECT requested\n");
278 tp->action = ATA_C_ATTACH;
282 device_printf(ch->dev, "DISCONNECT requested\n");
283 tp->action = ATA_C_DETACH;
286 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
287 taskqueue_enqueue(taskqueue_thread[mycpuid], &tp->task);
293 ata_sata_phy_event(void *context, int dummy)
295 struct ata_connect_task *tp = (struct ata_connect_task *)context;
296 struct ata_channel *ch = device_get_softc(tp->dev);
301 if (tp->action == ATA_C_ATTACH) {
303 device_printf(tp->dev, "CONNECTED\n");
305 ata_identify(tp->dev);
307 if (tp->action == ATA_C_DETACH) {
308 if (!device_get_children(tp->dev, &children, &nchildren)) {
309 for (i = 0; i < nchildren; i++)
311 device_delete_child(tp->dev, children[i]);
312 kfree(children, M_TEMP);
314 spin_lock(&ch->state_mtx);
315 ch->state = ATA_IDLE;
316 spin_unlock(&ch->state_mtx);
318 device_printf(tp->dev, "DISCONNECTED\n");
325 ata_sata_phy_reset(device_t dev)
327 struct ata_channel *ch = device_get_softc(dev);
330 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == ATA_SC_DET_IDLE)
331 return ata_sata_connect(ch);
333 for (retry = 0; retry < 10; retry++) {
334 for (loop = 0; loop < 10; loop++) {
335 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_RESET);
337 if ((ATA_IDX_INL(ch, ATA_SCONTROL) &
338 ATA_SC_DET_MASK) == ATA_SC_DET_RESET)
342 for (loop = 0; loop < 10; loop++) {
343 ATA_IDX_OUTL(ch, ATA_SCONTROL, ATA_SC_DET_IDLE |
344 ATA_SC_IPM_DIS_PARTIAL |
345 ATA_SC_IPM_DIS_SLUMBER);
347 if ((ATA_IDX_INL(ch, ATA_SCONTROL) & ATA_SC_DET_MASK) == 0)
348 return ata_sata_connect(ch);
355 ata_sata_connect(struct ata_channel *ch)
360 /* wait up to 1 second for "connect well" */
361 for (timeout = 0; timeout < 100 ; timeout++) {
362 status = ATA_IDX_INL(ch, ATA_SSTATUS);
363 if ((status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN1 ||
364 (status & ATA_SS_CONWELL_MASK) == ATA_SS_CONWELL_GEN2)
368 if (timeout >= 100) {
370 device_printf(ch->dev, "SATA connect status=%08x\n", status);
375 device_printf(ch->dev, "SATA connect time=%dms\n", timeout * 10);
377 /* clear SATA error register */
378 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
384 ata_sata_setmode(device_t dev, int mode)
386 struct ata_device *atadev = device_get_softc(dev);
389 * if we detect that the device isn't a real SATA device we limit
390 * the transfer mode to UDMA5/ATA100.
391 * this works around the problems some devices has with the
392 * Marvell 88SX8030 SATA->PATA converters and UDMA6/ATA133.
394 if (atadev->param.satacapabilities != 0x0000 &&
395 atadev->param.satacapabilities != 0xffff) {
396 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
398 /* on some drives we need to set the transfer mode */
399 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
400 ata_limit_mode(dev, mode, ATA_UDMA6));
402 /* query SATA STATUS for the speed */
403 if (ch->r_io[ATA_SSTATUS].res &&
404 ((ATA_IDX_INL(ch, ATA_SSTATUS) & ATA_SS_CONWELL_MASK) ==
405 ATA_SS_CONWELL_GEN2))
406 atadev->mode = ATA_SA300;
408 atadev->mode = ATA_SA150;
411 mode = ata_limit_mode(dev, mode, ATA_UDMA5);
412 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
418 ata_request2fis_h2d(struct ata_request *request, u_int8_t *fis)
420 struct ata_device *atadev = device_get_softc(request->dev);
422 if (request->flags & ATA_R_ATAPI) {
423 fis[0] = 0x27; /* host to device */
424 fis[1] = 0x80; /* command FIS (note PM goes here) */
425 fis[2] = ATA_PACKET_CMD;
426 if (request->flags & (ATA_R_READ | ATA_R_WRITE))
429 fis[5] = request->transfersize;
430 fis[6] = request->transfersize >> 8;
432 fis[7] = ATA_D_LBA | atadev->unit;
433 fis[15] = ATA_A_4BIT;
437 ata_modify_if_48bit(request);
438 fis[0] = 0x27; /* host to device */
439 fis[1] = 0x80; /* command FIS (note PM goes here) */
440 fis[2] = request->u.ata.command;
441 fis[3] = request->u.ata.feature;
442 fis[4] = request->u.ata.lba;
443 fis[5] = request->u.ata.lba >> 8;
444 fis[6] = request->u.ata.lba >> 16;
445 fis[7] = ATA_D_LBA | atadev->unit;
446 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
447 fis[7] |= (request->u.ata.lba >> 24 & 0x0f);
448 fis[8] = request->u.ata.lba >> 24;
449 fis[9] = request->u.ata.lba >> 32;
450 fis[10] = request->u.ata.lba >> 40;
451 fis[11] = request->u.ata.feature >> 8;
452 fis[12] = request->u.ata.count;
453 fis[13] = request->u.ata.count >> 8;
454 fis[15] = ATA_A_4BIT;
461 * AHCI v1.x compliant SATA chipset support functions
464 ata_ahci_ident(device_t dev)
466 struct ata_pci_controller *ctlr = device_get_softc(dev);
467 static struct ata_chip_id id = {0, 0, 0, 0x00, ATA_SA300, "AHCI"};
470 if (pci_read_config(dev, PCIR_PROGIF, 1) != PCIP_STORAGE_SATA_AHCI_1_0)
474 ksnprintf(buffer, sizeof(buffer), "%s (ID=%08x) AHCI controller",
475 ata_pcivendor2str(dev), pci_get_devid(dev));
477 ksnprintf(buffer, sizeof(buffer), "%s AHCI controller",
478 ata_pcivendor2str(dev));
479 device_set_desc_copy(dev, buffer);
481 ctlr->chipinit = ata_ahci_chipinit;
487 * AHCI v1.x compliant SATA chipset support functions
490 ata_ahci_chipinit(device_t dev)
492 struct ata_pci_controller *ctlr = device_get_softc(dev);
496 /* if we have a memory BAR(5) we are likely on an AHCI part */
497 ctlr->r_type2 = SYS_RES_MEMORY;
498 ctlr->r_rid2 = PCIR_BAR(5);
499 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
500 &ctlr->r_rid2, RF_ACTIVE)))
503 /* setup interrupt delivery if not done allready by a vendor driver */
505 if (ata_setup_interrupt(dev))
509 device_printf(dev, "AHCI called from vendor specific driver\n");
511 /* enable AHCI mode */
512 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC, ATA_AHCI_GHC_AE);
514 /* reset AHCI controller */
515 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC, ATA_AHCI_GHC_HR);
517 if (ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) & ATA_AHCI_GHC_HR) {
518 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
519 device_printf(dev, "AHCI controller reset failure\n");
523 /* reenable AHCI mode */
524 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC, ATA_AHCI_GHC_AE);
526 /* get the number of HW channels */
528 MAX(flsl(ATA_INL(ctlr->r_res2, ATA_AHCI_PI)),
529 (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_NPMASK) + 1);
531 /* disable interrupt sources and clear interrupts */
532 for (unit = 0; unit < ctlr->channels; unit++) {
533 int offset = unit << 7;
534 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset, 0);
535 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset, -1);
537 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, ATA_INL(ctlr->r_res2, ATA_AHCI_IS));
539 /* enable AHCI interrupts */
540 ATA_OUTL(ctlr->r_res2, ATA_AHCI_GHC,
541 ATA_INL(ctlr->r_res2, ATA_AHCI_GHC) | ATA_AHCI_GHC_IE);
543 ctlr->reset = ata_ahci_reset;
544 ctlr->dmainit = ata_ahci_dmainit;
545 ctlr->allocate = ata_ahci_allocate;
546 ctlr->setmode = ata_sata_setmode;
548 /* enable PCI interrupt */
549 pci_write_config(dev, PCIR_COMMAND,
550 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
552 /* announce we support the HW */
553 version = ATA_INL(ctlr->r_res2, ATA_AHCI_VS);
555 "AHCI Version %x%x.%x%x controller with %d ports detected\n",
556 (version >> 24) & 0xff, (version >> 16) & 0xff,
557 (version >> 8) & 0xff, version & 0xff,
558 (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_NPMASK) + 1);
563 ata_ahci_allocate(device_t dev)
565 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
566 struct ata_channel *ch = device_get_softc(dev);
568 int offset = ch->unit << 7;
570 /* set the SATA resources */
571 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
572 ch->r_io[ATA_SSTATUS].offset = ATA_AHCI_P_SSTS + offset;
573 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
574 ch->r_io[ATA_SERROR].offset = ATA_AHCI_P_SERR + offset;
575 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
576 ch->r_io[ATA_SCONTROL].offset = ATA_AHCI_P_SCTL + offset;
577 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
578 ch->r_io[ATA_SACTIVE].offset = ATA_AHCI_P_SACT + offset;
580 ch->hw.status = ata_ahci_status;
581 ch->hw.begin_transaction = ata_ahci_begin_transaction;
582 ch->hw.end_transaction = ata_ahci_end_transaction;
583 ch->hw.command = NULL; /* not used here */
585 /* setup work areas */
586 work = ch->dma->work_bus + ATA_AHCI_CL_OFFSET;
587 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLB + offset, work & 0xffffffff);
588 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CLBU + offset, work >> 32);
590 work = ch->dma->work_bus + ATA_AHCI_FB_OFFSET;
591 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FB + offset, work & 0xffffffff);
592 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_FBU + offset, work >> 32);
594 /* enable wanted port interrupts */
595 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IE + offset,
596 (ATA_AHCI_P_IX_CPD | ATA_AHCI_P_IX_TFE | ATA_AHCI_P_IX_HBF |
597 ATA_AHCI_P_IX_HBD | ATA_AHCI_P_IX_IF | ATA_AHCI_P_IX_OF |
598 ATA_AHCI_P_IX_PRC | ATA_AHCI_P_IX_PC | ATA_AHCI_P_IX_DP |
599 ATA_AHCI_P_IX_UF | ATA_AHCI_P_IX_SDB | ATA_AHCI_P_IX_DS |
600 ATA_AHCI_P_IX_PS | ATA_AHCI_P_IX_DHR));
602 /* start operations on this channel */
603 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
604 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
605 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
610 ata_ahci_status(device_t dev)
612 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
613 struct ata_channel *ch = device_get_softc(dev);
614 u_int32_t action = ATA_INL(ctlr->r_res2, ATA_AHCI_IS);
615 int offset = ch->unit << 7;
618 if (action & (1 << ch->unit)) {
619 u_int32_t istatus = ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset);
620 u_int32_t cstatus = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CI + offset);
622 /* clear interrupt(s) */
623 ATA_OUTL(ctlr->r_res2, ATA_AHCI_IS, action & (1 << ch->unit));
624 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset, istatus);
626 /* do we have any PHY events ? */
627 /* XXX SOS check istatus phy bits */
628 ata_sata_phy_check_events(dev);
630 /* do we have a potentially hanging engine to take care of? */
631 if ((istatus & 0x78400050) && (cstatus & (1 << tag))) {
633 u_int32_t cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
636 /* kill off all activity on this channel */
637 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
638 cmd & ~(ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
640 /* XXX SOS this is not entirely wrong */
643 if (timeout++ > 500) {
644 device_printf(dev, "stopping AHCI engine failed\n");
647 } while (ATA_INL(ctlr->r_res2,
648 ATA_AHCI_P_CMD + offset) & ATA_AHCI_P_CMD_CR);
650 /* start operations on this channel */
651 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
652 cmd | (ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
657 return (!(cstatus & (1 << tag)));
662 /* must be called with ATA channel locked and state_mtx held */
664 ata_ahci_begin_transaction(struct ata_request *request)
666 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
667 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
668 struct ata_ahci_cmd_tab *ctp;
669 struct ata_ahci_cmd_list *clp;
670 int offset = ch->unit << 7;
671 int tag = 0, entries = 0;
674 /* get a piece of the workspace for this request */
675 ctp = (struct ata_ahci_cmd_tab *)
676 (ch->dma->work + ATA_AHCI_CT_OFFSET + (ATA_AHCI_CT_SIZE * tag));
678 /* setup the FIS for this request */
679 if (!(fis_size = ata_ahci_setup_fis(ctp, request))) {
680 device_printf(request->dev, "setting up SATA FIS failed\n");
681 request->result = EIO;
682 return ATA_OP_FINISHED;
685 /* if request moves data setup and load SG list */
686 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
687 if (ch->dma->load(ch->dev, request->data, request->bytecount,
688 request->flags & ATA_R_READ,
689 ctp->prd_tab, &entries)) {
690 device_printf(request->dev, "setting up DMA failed\n");
691 request->result = EIO;
692 return ATA_OP_FINISHED;
696 /* setup the command list entry */
697 clp = (struct ata_ahci_cmd_list *)
698 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
700 clp->prd_length = entries;
701 clp->cmd_flags = (request->flags & ATA_R_WRITE ? (1<<6) : 0) |
702 (request->flags & ATA_R_ATAPI ? ((1<<5) | (1<<7)) : 0) |
703 (fis_size / sizeof(u_int32_t));
705 clp->cmd_table_phys = htole64(ch->dma->work_bus + ATA_AHCI_CT_OFFSET +
706 (ATA_AHCI_CT_SIZE * tag));
708 /* clear eventual ACTIVE bit */
709 ATA_IDX_OUTL(ch, ATA_SACTIVE, ATA_IDX_INL(ch, ATA_SACTIVE) & (1 << tag));
711 /* set command type bit */
712 if (request->flags & ATA_R_ATAPI)
713 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
714 ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) |
715 ATA_AHCI_P_CMD_ATAPI);
717 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
718 ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) &
719 ~ATA_AHCI_P_CMD_ATAPI);
721 /* issue command to controller */
722 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CI + offset, (1 << tag));
724 if (!(request->flags & ATA_R_ATAPI)) {
725 /* device reset doesn't interrupt */
726 if (request->u.ata.command == ATA_DEVICE_RESET) {
728 int timeout = 1000000;
732 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + (ch->unit<<7));
733 } while ((tf_data & ATA_S_BUSY) && timeout--);
735 device_printf(ch->dev, "device_reset timeout=%dus\n",
736 (1000000-timeout)*10);
737 request->status = tf_data;
738 if (request->status & ATA_S_ERROR)
739 request->error = tf_data >> 8;
740 return ATA_OP_FINISHED;
744 /* start the timeout */
745 callout_reset(&request->callout, request->timeout * hz,
746 (timeout_t*)ata_timeout, request);
747 return ATA_OP_CONTINUES;
750 /* must be called with ATA channel locked and state_mtx held */
752 ata_ahci_end_transaction(struct ata_request *request)
754 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
755 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
756 struct ata_ahci_cmd_list *clp;
758 int offset = ch->unit << 7;
761 /* kill the timeout */
762 callout_stop(&request->callout);
765 tf_data = ATA_INL(ctlr->r_res2, ATA_AHCI_P_TFD + offset);
766 request->status = tf_data;
768 /* if error status get details */
769 if (request->status & ATA_S_ERROR)
770 request->error = tf_data >> 8;
772 /* record how much data we actually moved */
773 clp = (struct ata_ahci_cmd_list *)
774 (ch->dma->work + ATA_AHCI_CL_OFFSET + (ATA_AHCI_CL_SIZE * tag));
775 request->donecount = clp->bytecount;
777 /* release SG list etc */
778 ch->dma->unload(ch->dev);
780 return ATA_OP_FINISHED;
784 ata_ahci_reset(device_t dev)
786 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
787 struct ata_channel *ch = device_get_softc(dev);
788 u_int32_t cmd, signature;
789 int offset = ch->unit << 7;
792 if (!(ATA_INL(ctlr->r_res2, ATA_AHCI_PI) & (1 << ch->unit))) {
793 device_printf(dev, "port not implemented\n");
798 /* kill off all activity on this channel */
799 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
800 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
801 cmd & ~(ATA_AHCI_P_CMD_FRE | ATA_AHCI_P_CMD_ST));
803 /* XXX SOS this is not entirely wrong */
807 if (timeout++ > 500) {
808 device_printf(dev, "stopping AHCI engine failed\n");
812 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset) & ATA_AHCI_P_CMD_CR);
814 /* issue Command List Override if supported */
815 if (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_CAP_CLO) {
816 cmd = ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD + offset);
817 cmd |= ATA_AHCI_P_CMD_CLO;
818 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset, cmd);
822 if (timeout++ > 500) {
823 device_printf(dev, "executing CLO failed\n");
827 while (ATA_INL(ctlr->r_res2, ATA_AHCI_P_CMD+offset)&ATA_AHCI_P_CMD_CLO);
830 /* reset PHY and decide what is present */
831 if (ata_sata_phy_reset(dev)) {
833 /* clear any interrupts pending on this channel */
834 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_IS + offset,
835 ATA_INL(ctlr->r_res2, ATA_AHCI_P_IS + offset));
837 /* clear SATA error register */
838 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
840 /* start operations on this channel */
841 ATA_OUTL(ctlr->r_res2, ATA_AHCI_P_CMD + offset,
842 (ATA_AHCI_P_CMD_ACTIVE | ATA_AHCI_P_CMD_FRE |
843 ATA_AHCI_P_CMD_POD | ATA_AHCI_P_CMD_SUD | ATA_AHCI_P_CMD_ST));
845 signature = ATA_INL(ctlr->r_res2, ATA_AHCI_P_SIG + offset);
848 ch->devices = ATA_ATA_MASTER;
851 ch->devices = ATA_PORTMULTIPLIER;
852 device_printf(ch->dev, "Portmultipliers not supported yet\n");
856 ch->devices = ATA_ATAPI_MASTER;
858 default: /* SOS XXX */
860 device_printf(ch->dev, "No signature, assuming disk device\n");
861 ch->devices = ATA_ATA_MASTER;
865 device_printf(dev, "ahci_reset devices=0x%b\n", ch->devices,
866 "\20\4ATAPI_SLAVE\3ATAPI_MASTER\2ATA_SLAVE\1ATA_MASTER");
870 ata_ahci_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
872 struct ata_dmasetprd_args *args = xsc;
873 struct ata_ahci_dma_prd *prd = args->dmatab;
876 if (!(args->error = error)) {
877 for (i = 0; i < nsegs; i++) {
878 prd[i].dba = htole64(segs[i].ds_addr);
879 prd[i].dbc = htole32((segs[i].ds_len - 1) & ATA_AHCI_PRD_MASK);
886 ata_ahci_dmainit(device_t dev)
888 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
889 struct ata_channel *ch = device_get_softc(dev);
893 /* note start and stop are not used here */
894 ch->dma->setprd = ata_ahci_dmasetprd;
895 ch->dma->max_iosize = 8192 * DEV_BSIZE;
896 if (ATA_INL(ctlr->r_res2, ATA_AHCI_CAP) & ATA_AHCI_CAP_64BIT)
897 ch->dma->max_address = BUS_SPACE_MAXADDR;
902 ata_ahci_setup_fis(struct ata_ahci_cmd_tab *ctp, struct ata_request *request)
904 bzero(ctp->cfis, 64);
905 if (request->flags & ATA_R_ATAPI) {
906 bzero(ctp->acmd, 32);
907 bcopy(request->u.atapi.ccb, ctp->acmd, 16);
909 return ata_request2fis_h2d(request, &ctp->cfis[0]);
913 * Acard chipset support functions
916 ata_acard_ident(device_t dev)
918 struct ata_pci_controller *ctlr = device_get_softc(dev);
919 struct ata_chip_id *idx;
920 static struct ata_chip_id ids[] =
921 {{ ATA_ATP850R, 0, ATPOLD, 0x00, ATA_UDMA2, "ATP850" },
922 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "ATP860A" },
923 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "ATP860R" },
924 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "ATP865A" },
925 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "ATP865R" },
926 { 0, 0, 0, 0, 0, 0}};
929 if (!(idx = ata_match_chip(dev, ids)))
932 ksprintf(buffer, "Acard %s %s controller",
933 idx->text, ata_mode2str(idx->max_dma));
934 device_set_desc_copy(dev, buffer);
936 ctlr->chipinit = ata_acard_chipinit;
941 ata_acard_chipinit(device_t dev)
943 struct ata_pci_controller *ctlr = device_get_softc(dev);
945 if (ata_setup_interrupt(dev))
948 ctlr->allocate = ata_acard_allocate;
949 if (ctlr->chip->cfg1 == ATPOLD) {
950 ctlr->setmode = ata_acard_850_setmode;
951 ctlr->locking = ata_serialize;
954 ctlr->setmode = ata_acard_86X_setmode;
959 ata_acard_allocate(device_t dev)
961 struct ata_channel *ch = device_get_softc(dev);
963 /* setup the usual register normal pci style */
964 if (ata_pci_allocate(dev))
967 ch->hw.status = ata_acard_status;
972 ata_acard_status(device_t dev)
974 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
975 struct ata_channel *ch = device_get_softc(dev);
977 if (ctlr->chip->cfg1 == ATPOLD &&
978 ATA_LOCKING(ch->dev, ATA_LF_WHICH) != ch->unit)
980 if (ch->dma && (ch->dma->flags & ATA_DMA_ACTIVE)) {
981 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
983 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
984 ATA_BMSTAT_INTERRUPT)
986 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
988 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
989 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
992 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
994 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
1001 ata_acard_850_setmode(device_t dev, int mode)
1003 device_t gparent = GRANDPARENT(dev);
1004 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1005 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1006 struct ata_device *atadev = device_get_softc(dev);
1007 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1010 mode = ata_limit_mode(dev, mode,
1011 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
1013 /* XXX SOS missing WDMA0+1 + PIO modes */
1014 if (mode >= ATA_WDMA2) {
1015 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1017 device_printf(dev, "%ssetting %s on %s chip\n",
1018 (error) ? "FAILURE " : "",
1019 ata_mode2str(mode), ctlr->chip->text);
1021 u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
1023 reg54 &= ~(0x03 << (devno << 1));
1024 if (mode >= ATA_UDMA0)
1025 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
1026 pci_write_config(gparent, 0x54, reg54, 1);
1027 pci_write_config(gparent, 0x4a, 0xa6, 1);
1028 pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
1029 atadev->mode = mode;
1033 /* we could set PIO mode timings, but we assume the BIOS did that */
1037 ata_acard_86X_setmode(device_t dev, int mode)
1039 device_t gparent = GRANDPARENT(dev);
1040 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1041 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1042 struct ata_device *atadev = device_get_softc(dev);
1043 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1047 mode = ata_limit_mode(dev, mode,
1048 ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
1050 mode = ata_check_80pin(dev, mode);
1052 /* XXX SOS missing WDMA0+1 + PIO modes */
1053 if (mode >= ATA_WDMA2) {
1054 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1056 device_printf(dev, "%ssetting %s on %s chip\n",
1057 (error) ? "FAILURE " : "",
1058 ata_mode2str(mode), ctlr->chip->text);
1060 u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
1062 reg44 &= ~(0x000f << (devno << 2));
1063 if (mode >= ATA_UDMA0)
1064 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
1065 pci_write_config(gparent, 0x44, reg44, 2);
1066 pci_write_config(gparent, 0x4a, 0xa6, 1);
1067 pci_write_config(gparent, 0x40 + devno, 0x31, 1);
1068 atadev->mode = mode;
1072 /* we could set PIO mode timings, but we assume the BIOS did that */
1077 * Acer Labs Inc (ALI) chipset support functions
1080 ata_ali_ident(device_t dev)
1082 struct ata_pci_controller *ctlr = device_get_softc(dev);
1083 struct ata_chip_id *idx;
1084 static struct ata_chip_id ids[] =
1085 {{ ATA_ALI_5289, 0x00, 2, ALISATA, ATA_SA150, "M5289" },
1086 { ATA_ALI_5288, 0x00, 4, ALISATA, ATA_SA300, "M5288" },
1087 { ATA_ALI_5287, 0x00, 4, ALISATA, ATA_SA150, "M5287" },
1088 { ATA_ALI_5281, 0x00, 2, ALISATA, ATA_SA150, "M5281" },
1089 { ATA_ALI_5229, 0xc5, 0, ALINEW, ATA_UDMA6, "M5229" },
1090 { ATA_ALI_5229, 0xc4, 0, ALINEW, ATA_UDMA5, "M5229" },
1091 { ATA_ALI_5229, 0xc2, 0, ALINEW, ATA_UDMA4, "M5229" },
1092 { ATA_ALI_5229, 0x20, 0, ALIOLD, ATA_UDMA2, "M5229" },
1093 { ATA_ALI_5229, 0x00, 0, ALIOLD, ATA_WDMA2, "M5229" },
1094 { 0, 0, 0, 0, 0, 0}};
1097 if (!(idx = ata_match_chip(dev, ids)))
1100 ksprintf(buffer, "AcerLabs %s %s controller",
1101 idx->text, ata_mode2str(idx->max_dma));
1102 device_set_desc_copy(dev, buffer);
1104 ctlr->chipinit = ata_ali_chipinit;
1109 ata_ali_chipinit(device_t dev)
1111 struct ata_pci_controller *ctlr = device_get_softc(dev);
1113 if (ata_setup_interrupt(dev))
1116 switch (ctlr->chip->cfg2) {
1118 ctlr->channels = ctlr->chip->cfg1;
1119 ctlr->allocate = ata_ali_sata_allocate;
1120 ctlr->setmode = ata_sata_setmode;
1122 /* AHCI mode is correctly supported only on the ALi 5288. */
1123 if ((ctlr->chip->chipid == ATA_ALI_5288) &&
1124 (ata_ahci_chipinit(dev) != ENXIO))
1127 /* enable PCI interrupt */
1128 pci_write_config(dev, PCIR_COMMAND,
1129 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1133 /* use device interrupt as byte count end */
1134 pci_write_config(dev, 0x4a, pci_read_config(dev, 0x4a, 1) | 0x20, 1);
1136 /* enable cable detection and UDMA support on newer chips */
1137 pci_write_config(dev, 0x4b, pci_read_config(dev, 0x4b, 1) | 0x09, 1);
1139 /* enable ATAPI UDMA mode */
1140 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x01, 1);
1142 /* only chips with revision > 0xc4 can do 48bit DMA */
1143 if (ctlr->chip->chiprev <= 0xc4)
1145 "using PIO transfers above 137GB as workaround for "
1146 "48bit DMA access bug, expect reduced performance\n");
1147 ctlr->allocate = ata_ali_allocate;
1148 ctlr->reset = ata_ali_reset;
1149 ctlr->setmode = ata_ali_setmode;
1153 /* deactivate the ATAPI FIFO and enable ATAPI UDMA */
1154 pci_write_config(dev, 0x53, pci_read_config(dev, 0x53, 1) | 0x03, 1);
1155 ctlr->setmode = ata_ali_setmode;
1162 ata_ali_allocate(device_t dev)
1164 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1165 struct ata_channel *ch = device_get_softc(dev);
1167 /* setup the usual register normal pci style */
1168 if (ata_pci_allocate(dev))
1171 /* older chips can't do 48bit DMA transfers */
1172 if (ctlr->chip->chiprev <= 0xc4)
1173 ch->flags |= ATA_NO_48BIT_DMA;
1179 ata_ali_sata_allocate(device_t dev)
1181 device_t parent = device_get_parent(dev);
1182 struct ata_pci_controller *ctlr = device_get_softc(parent);
1183 struct ata_channel *ch = device_get_softc(dev);
1184 struct resource *io = NULL, *ctlio = NULL;
1185 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
1188 rid = PCIR_BAR(0) + (unit01 ? 8 : 0);
1189 io = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
1193 rid = PCIR_BAR(1) + (unit01 ? 8 : 0);
1194 ctlio = bus_alloc_resource_any(parent, SYS_RES_IOPORT, &rid, RF_ACTIVE);
1196 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
1200 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
1201 ch->r_io[i].res = io;
1202 ch->r_io[i].offset = i + (unit10 ? 8 : 0);
1204 ch->r_io[ATA_CONTROL].res = ctlio;
1205 ch->r_io[ATA_CONTROL].offset = 2 + (unit10 ? 4 : 0);
1206 ch->r_io[ATA_IDX_ADDR].res = io;
1207 ata_default_registers(dev);
1209 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
1210 ch->r_io[i].res = ctlr->r_res1;
1211 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
1214 ch->flags |= ATA_NO_SLAVE;
1216 /* XXX SOS PHY handling awkward in ALI chip not supported yet */
1222 ata_ali_reset(device_t dev)
1224 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1225 struct ata_channel *ch = device_get_softc(dev);
1229 ata_generic_reset(dev);
1232 * workaround for datacorruption bug found on at least SUN Blade-100
1233 * find the ISA function on the southbridge and disable then enable
1234 * the ATA channel tristate buffer
1236 if (ctlr->chip->chiprev == 0xc3 || ctlr->chip->chiprev == 0xc2) {
1237 if (!device_get_children(GRANDPARENT(dev), &children, &nchildren)) {
1238 for (i = 0; i < nchildren; i++) {
1239 if (pci_get_devid(children[i]) == ATA_ALI_1533) {
1240 pci_write_config(children[i], 0x58,
1241 pci_read_config(children[i], 0x58, 1) &
1242 ~(0x04 << ch->unit), 1);
1243 pci_write_config(children[i], 0x58,
1244 pci_read_config(children[i], 0x58, 1) |
1245 (0x04 << ch->unit), 1);
1249 kfree(children, M_TEMP);
1255 ata_ali_setmode(device_t dev, int mode)
1257 device_t gparent = GRANDPARENT(dev);
1258 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1259 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1260 struct ata_device *atadev = device_get_softc(dev);
1261 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1264 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1266 if (ctlr->chip->cfg2 & ALINEW) {
1267 if (mode > ATA_UDMA2 &&
1268 pci_read_config(gparent, 0x4a, 1) & (1 << ch->unit)) {
1269 ata_print_cable(dev, "controller");
1274 mode = ata_check_80pin(dev, mode);
1276 if (ctlr->chip->cfg2 & ALIOLD) {
1277 /* doesn't support ATAPI DMA on write */
1278 ch->flags |= ATA_ATAPI_DMA_RO;
1279 if (ch->devices & ATA_ATAPI_MASTER && ch->devices & ATA_ATAPI_SLAVE) {
1280 /* doesn't support ATAPI DMA on two ATAPI devices */
1281 device_printf(dev, "two atapi devices on this channel, no DMA\n");
1282 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1286 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1289 device_printf(dev, "%ssetting %s on %s chip\n",
1290 (error) ? "FAILURE " : "",
1291 ata_mode2str(mode), ctlr->chip->text);
1293 if (mode >= ATA_UDMA0) {
1294 u_int8_t udma[] = {0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0d};
1295 u_int32_t word54 = pci_read_config(gparent, 0x54, 4);
1297 word54 &= ~(0x000f000f << (devno << 2));
1298 word54 |= (((udma[mode&ATA_MODE_MASK]<<16)|0x05)<<(devno<<2));
1299 pci_write_config(gparent, 0x54, word54, 4);
1300 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1304 u_int32_t piotimings[] =
1305 { 0x006d0003, 0x00580002, 0x00440001, 0x00330001,
1306 0x00310001, 0x00440001, 0x00330001, 0x00310001};
1308 pci_write_config(gparent, 0x54, pci_read_config(gparent, 0x54, 4) &
1309 ~(0x0008000f << (devno << 2)), 4);
1310 pci_write_config(gparent, 0x58 + (ch->unit << 2),
1311 piotimings[ata_mode2idx(mode)], 4);
1313 atadev->mode = mode;
1319 * American Micro Devices (AMD) chipset support functions
1322 ata_amd_ident(device_t dev)
1324 struct ata_pci_controller *ctlr = device_get_softc(dev);
1325 struct ata_chip_id *idx;
1326 static struct ata_chip_id ids[] =
1327 {{ ATA_AMD756, 0x00, AMDNVIDIA, 0x00, ATA_UDMA4, "756" },
1328 { ATA_AMD766, 0x00, AMDNVIDIA, AMDCABLE|AMDBUG, ATA_UDMA5, "766" },
1329 { ATA_AMD768, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA5, "768" },
1330 { ATA_AMD8111, 0x00, AMDNVIDIA, AMDCABLE, ATA_UDMA6, "8111" },
1331 { 0, 0, 0, 0, 0, 0}};
1334 if (!(idx = ata_match_chip(dev, ids)))
1337 ksprintf(buffer, "AMD %s %s controller",
1338 idx->text, ata_mode2str(idx->max_dma));
1339 device_set_desc_copy(dev, buffer);
1341 ctlr->chipinit = ata_amd_chipinit;
1346 ata_amd_chipinit(device_t dev)
1348 struct ata_pci_controller *ctlr = device_get_softc(dev);
1350 if (ata_setup_interrupt(dev))
1353 /* disable/set prefetch, postwrite */
1354 if (ctlr->chip->cfg2 & AMDBUG)
1355 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) & 0x0f, 1);
1357 pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
1359 ctlr->setmode = ata_via_family_setmode;
1365 * ATI chipset support functions
1368 ata_ati_ident(device_t dev)
1370 struct ata_pci_controller *ctlr = device_get_softc(dev);
1371 struct ata_chip_id *idx;
1372 static struct ata_chip_id ids[] =
1373 {{ ATA_ATI_IXP200, 0x00, 0, 0, ATA_UDMA5, "IXP200" },
1374 { ATA_ATI_IXP300, 0x00, 0, 0, ATA_UDMA6, "IXP300" },
1375 { ATA_ATI_IXP400, 0x00, 0, 0, ATA_UDMA6, "IXP400" },
1376 { ATA_ATI_SB600, 0x00, 0, 0, ATA_UDMA6, "SB600" },
1377 { ATA_ATI_IXP300_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP300" },
1378 { ATA_ATI_IXP400_S1, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP400" },
1379 { ATA_ATI_IXP400_S2, 0x00, SIIMEMIO, 0, ATA_SA150, "IXP400" },
1380 { ATA_ATI_SB600_S1, 0x00, ATIAHCI, 0, ATA_SA300, "SB600" },
1381 { ATA_ATI_SB600_S2, 0x00, ATIAHCI, 0, ATA_SA300, "SB600" },
1382 { 0, 0, 0, 0, 0, 0}};
1385 if (!(idx = ata_match_chip(dev, ids)))
1388 ksprintf(buffer, "ATI %s %s controller",
1389 idx->text, ata_mode2str(idx->max_dma));
1390 device_set_desc_copy(dev, buffer);
1394 * The ATI SATA controllers are actually a SiI 3112 controller, except
1397 if (ctlr->chip->cfg1 & SIIMEMIO)
1398 ctlr->chipinit = ata_sii_chipinit;
1400 ctlr->chipinit = ata_ati_chipinit;
1405 ata_ati_chipinit(device_t dev)
1407 struct ata_pci_controller *ctlr = device_get_softc(dev);
1409 if (ata_setup_interrupt(dev))
1412 /* The SB600 needs special treatment. */
1413 if (ctlr->chip->cfg1 & ATIAHCI) {
1414 /* Check if the chip is configured as an AHCI part. */
1415 if ((pci_get_subclass(dev) == PCIS_STORAGE_SATA) &&
1416 (pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_STORAGE_SATA_AHCI_1_0)) {
1417 if (ata_ahci_chipinit(dev) != ENXIO)
1422 ctlr->setmode = ata_ati_setmode;
1427 ata_ati_setmode(device_t dev, int mode)
1429 device_t gparent = GRANDPARENT(dev);
1430 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1431 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1432 struct ata_device *atadev = device_get_softc(dev);
1433 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1434 int offset = (devno ^ 0x01) << 3;
1436 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
1437 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
1438 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
1440 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1442 mode = ata_check_80pin(dev, mode);
1444 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1447 device_printf(dev, "%ssetting %s on %s chip\n",
1448 (error) ? "FAILURE " : "",
1449 ata_mode2str(mode), ctlr->chip->text);
1451 if (mode >= ATA_UDMA0) {
1452 pci_write_config(gparent, 0x56,
1453 (pci_read_config(gparent, 0x56, 2) &
1454 ~(0xf << (devno << 2))) |
1455 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
1456 pci_write_config(gparent, 0x54,
1457 pci_read_config(gparent, 0x54, 1) |
1458 (0x01 << devno), 1);
1459 pci_write_config(gparent, 0x44,
1460 (pci_read_config(gparent, 0x44, 4) &
1461 ~(0xff << offset)) |
1462 (dmatimings[2] << offset), 4);
1464 else if (mode >= ATA_WDMA0) {
1465 pci_write_config(gparent, 0x54,
1466 pci_read_config(gparent, 0x54, 1) &
1467 ~(0x01 << devno), 1);
1468 pci_write_config(gparent, 0x44,
1469 (pci_read_config(gparent, 0x44, 4) &
1470 ~(0xff << offset)) |
1471 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
1474 pci_write_config(gparent, 0x54,
1475 pci_read_config(gparent, 0x54, 1) &
1476 ~(0x01 << devno), 1);
1478 pci_write_config(gparent, 0x4a,
1479 (pci_read_config(gparent, 0x4a, 2) &
1480 ~(0xf << (devno << 2))) |
1481 (((mode - ATA_PIO0) & ATA_MODE_MASK) << (devno<<2)),2);
1482 pci_write_config(gparent, 0x40,
1483 (pci_read_config(gparent, 0x40, 4) &
1484 ~(0xff << offset)) |
1485 (piotimings[ata_mode2idx(mode)] << offset), 4);
1486 atadev->mode = mode;
1491 * Cyrix chipset support functions
1494 ata_cyrix_ident(device_t dev)
1496 struct ata_pci_controller *ctlr = device_get_softc(dev);
1498 if (pci_get_devid(dev) == ATA_CYRIX_5530) {
1499 device_set_desc(dev, "Cyrix 5530 ATA33 controller");
1500 ctlr->chipinit = ata_cyrix_chipinit;
1507 ata_cyrix_chipinit(device_t dev)
1509 struct ata_pci_controller *ctlr = device_get_softc(dev);
1511 if (ata_setup_interrupt(dev))
1515 ctlr->setmode = ata_cyrix_setmode;
1517 ctlr->setmode = ata_generic_setmode;
1522 ata_cyrix_setmode(device_t dev, int mode)
1524 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1525 struct ata_device *atadev = device_get_softc(dev);
1526 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1527 u_int32_t piotiming[] =
1528 { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
1529 u_int32_t dmatiming[] = { 0x00077771, 0x00012121, 0x00002020 };
1530 u_int32_t udmatiming[] = { 0x00921250, 0x00911140, 0x00911030 };
1533 ch->dma->alignment = 16;
1534 ch->dma->max_iosize = 126 * DEV_BSIZE;
1536 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
1538 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1541 device_printf(dev, "%ssetting %s on Cyrix chip\n",
1542 (error) ? "FAILURE " : "", ata_mode2str(mode));
1544 if (mode >= ATA_UDMA0) {
1545 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1546 0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
1548 else if (mode >= ATA_WDMA0) {
1549 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1550 0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
1553 ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
1554 0x20 + (devno << 3), piotiming[mode & ATA_MODE_MASK]);
1556 atadev->mode = mode;
1562 * Cypress chipset support functions
1565 ata_cypress_ident(device_t dev)
1567 struct ata_pci_controller *ctlr = device_get_softc(dev);
1570 * the Cypress chip is a mess, it contains two ATA functions, but
1571 * both channels are visible on the first one.
1572 * simply ignore the second function for now, as the right
1573 * solution (ignoring the second channel on the first function)
1574 * doesn't work with the crappy ATA interrupt setup on the alpha.
1576 if (pci_get_devid(dev) == ATA_CYPRESS_82C693 &&
1577 pci_get_function(dev) == 1 &&
1578 pci_get_subclass(dev) == PCIS_STORAGE_IDE) {
1579 device_set_desc(dev, "Cypress 82C693 ATA controller");
1580 ctlr->chipinit = ata_cypress_chipinit;
1587 ata_cypress_chipinit(device_t dev)
1589 struct ata_pci_controller *ctlr = device_get_softc(dev);
1591 if (ata_setup_interrupt(dev))
1594 ctlr->setmode = ata_cypress_setmode;
1599 ata_cypress_setmode(device_t dev, int mode)
1601 device_t gparent = GRANDPARENT(dev);
1602 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1603 struct ata_device *atadev = device_get_softc(dev);
1606 mode = ata_limit_mode(dev, mode, ATA_WDMA2);
1608 /* XXX SOS missing WDMA0+1 + PIO modes */
1609 if (mode == ATA_WDMA2) {
1610 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
1612 device_printf(dev, "%ssetting WDMA2 on Cypress chip\n",
1613 error ? "FAILURE " : "");
1615 pci_write_config(gparent, ch->unit ? 0x4e : 0x4c, 0x2020, 2);
1616 atadev->mode = mode;
1620 /* we could set PIO mode timings, but we assume the BIOS did that */
1625 * HighPoint chipset support functions
1628 ata_highpoint_ident(device_t dev)
1630 struct ata_pci_controller *ctlr = device_get_softc(dev);
1631 struct ata_chip_id *idx;
1632 static struct ata_chip_id ids[] =
1633 {{ ATA_HPT374, 0x07, HPT374, 0x00, ATA_UDMA6, "HPT374" },
1634 { ATA_HPT372, 0x02, HPT372, 0x00, ATA_UDMA6, "HPT372N" },
1635 { ATA_HPT372, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT372" },
1636 { ATA_HPT371, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT371" },
1637 { ATA_HPT366, 0x05, HPT372, 0x00, ATA_UDMA6, "HPT372" },
1638 { ATA_HPT366, 0x03, HPT370, 0x00, ATA_UDMA5, "HPT370" },
1639 { ATA_HPT366, 0x02, HPT366, 0x00, ATA_UDMA4, "HPT368" },
1640 { ATA_HPT366, 0x00, HPT366, HPTOLD, ATA_UDMA4, "HPT366" },
1641 { ATA_HPT302, 0x01, HPT372, 0x00, ATA_UDMA6, "HPT302" },
1642 { 0, 0, 0, 0, 0, 0}};
1645 if (!(idx = ata_match_chip(dev, ids)))
1648 strcpy(buffer, "HighPoint ");
1649 strcat(buffer, idx->text);
1650 if (idx->cfg1 == HPT374) {
1651 if (pci_get_function(dev) == 0)
1652 strcat(buffer, " (channel 0+1)");
1653 if (pci_get_function(dev) == 1)
1654 strcat(buffer, " (channel 2+3)");
1656 ksprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
1657 device_set_desc_copy(dev, buffer);
1659 ctlr->chipinit = ata_highpoint_chipinit;
1664 ata_highpoint_chipinit(device_t dev)
1666 struct ata_pci_controller *ctlr = device_get_softc(dev);
1668 if (ata_setup_interrupt(dev))
1671 if (ctlr->chip->cfg2 == HPTOLD) {
1672 /* disable interrupt prediction */
1673 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
1676 /* disable interrupt prediction */
1677 pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
1678 pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
1680 /* enable interrupts */
1681 pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
1683 /* set clocks etc */
1684 if (ctlr->chip->cfg1 < HPT372)
1685 pci_write_config(dev, 0x5b, 0x22, 1);
1687 pci_write_config(dev, 0x5b,
1688 (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
1690 ctlr->allocate = ata_highpoint_allocate;
1691 ctlr->setmode = ata_highpoint_setmode;
1696 ata_highpoint_allocate(device_t dev)
1698 struct ata_channel *ch = device_get_softc(dev);
1700 /* setup the usual register normal pci style */
1701 if (ata_pci_allocate(dev))
1704 ch->flags |= ATA_ALWAYS_DMASTAT;
1709 ata_highpoint_setmode(device_t dev, int mode)
1711 device_t gparent = GRANDPARENT(dev);
1712 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1713 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1714 struct ata_device *atadev = device_get_softc(dev);
1715 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
1717 u_int32_t timings33[][4] = {
1718 /* HPT366 HPT370 HPT372 HPT374 mode */
1719 { 0x40d0a7aa, 0x06914e57, 0x0d029d5e, 0x0ac1f48a }, /* PIO 0 */
1720 { 0x40d0a7a3, 0x06914e43, 0x0d029d26, 0x0ac1f465 }, /* PIO 1 */
1721 { 0x40d0a753, 0x06514e33, 0x0c829ca6, 0x0a81f454 }, /* PIO 2 */
1722 { 0x40c8a742, 0x06514e22, 0x0c829c84, 0x0a81f443 }, /* PIO 3 */
1723 { 0x40c8a731, 0x06514e21, 0x0c829c62, 0x0a81f442 }, /* PIO 4 */
1724 { 0x20c8a797, 0x26514e97, 0x2c82922e, 0x228082ea }, /* MWDMA 0 */
1725 { 0x20c8a732, 0x26514e33, 0x2c829266, 0x22808254 }, /* MWDMA 1 */
1726 { 0x20c8a731, 0x26514e21, 0x2c829262, 0x22808242 }, /* MWDMA 2 */
1727 { 0x10c8a731, 0x16514e31, 0x1c829c62, 0x121882ea }, /* UDMA 0 */
1728 { 0x10cba731, 0x164d4e31, 0x1c9a9c62, 0x12148254 }, /* UDMA 1 */
1729 { 0x10caa731, 0x16494e31, 0x1c929c62, 0x120c8242 }, /* UDMA 2 */
1730 { 0x10cfa731, 0x166d4e31, 0x1c8e9c62, 0x128c8242 }, /* UDMA 3 */
1731 { 0x10c9a731, 0x16454e31, 0x1c8a9c62, 0x12ac8242 }, /* UDMA 4 */
1732 { 0, 0x16454e31, 0x1c8a9c62, 0x12848242 }, /* UDMA 5 */
1733 { 0, 0, 0x1c869c62, 0x12808242 } /* UDMA 6 */
1736 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
1738 if (ctlr->chip->cfg1 == HPT366 && ata_atapi(dev))
1739 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
1741 mode = ata_highpoint_check_80pin(dev, mode);
1744 * most if not all HPT chips cant really handle that the device is
1745 * running at ATA_UDMA6/ATA133 speed, so we cheat at set the device to
1746 * a max of ATA_UDMA5/ATA100 to guard against suboptimal performance
1748 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
1749 ata_limit_mode(dev, mode, ATA_UDMA5));
1751 device_printf(dev, "%ssetting %s on HighPoint chip\n",
1752 (error) ? "FAILURE " : "", ata_mode2str(mode));
1754 pci_write_config(gparent, 0x40 + (devno << 2),
1755 timings33[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
1756 atadev->mode = mode;
1760 ata_highpoint_check_80pin(device_t dev, int mode)
1762 device_t gparent = GRANDPARENT(dev);
1763 struct ata_pci_controller *ctlr = device_get_softc(gparent);
1764 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
1765 u_int8_t reg, val, res;
1767 if (ctlr->chip->cfg1 == HPT374 && pci_get_function(gparent) == 1) {
1768 reg = ch->unit ? 0x57 : 0x53;
1769 val = pci_read_config(gparent, reg, 1);
1770 pci_write_config(gparent, reg, val | 0x80, 1);
1774 val = pci_read_config(gparent, reg, 1);
1775 pci_write_config(gparent, reg, val & 0xfe, 1);
1777 res = pci_read_config(gparent, 0x5a, 1) & (ch->unit ? 0x1:0x2);
1778 pci_write_config(gparent, reg, val, 1);
1780 if (mode > ATA_UDMA2 && res) {
1781 ata_print_cable(dev, "controller");
1789 * Intel chipset support functions
1792 ata_intel_ident(device_t dev)
1794 struct ata_pci_controller *ctlr = device_get_softc(dev);
1795 struct ata_chip_id *idx;
1796 static struct ata_chip_id ids[] =
1797 {{ ATA_I82371FB, 0, 0, 0x00, ATA_WDMA2, "PIIX" },
1798 { ATA_I82371SB, 0, 0, 0x00, ATA_WDMA2, "PIIX3" },
1799 { ATA_I82371AB, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1800 { ATA_I82443MX, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1801 { ATA_I82451NX, 0, 0, 0x00, ATA_UDMA2, "PIIX4" },
1802 { ATA_I82801AB, 0, 0, 0x00, ATA_UDMA2, "ICH0" },
1803 { ATA_I82801AA, 0, 0, 0x00, ATA_UDMA4, "ICH" },
1804 { ATA_I82372FB, 0, 0, 0x00, ATA_UDMA4, "ICH" },
1805 { ATA_I82801BA, 0, 0, 0x00, ATA_UDMA5, "ICH2" },
1806 { ATA_I82801BA_1, 0, 0, 0x00, ATA_UDMA5, "ICH2" },
1807 { ATA_I82801CA, 0, 0, 0x00, ATA_UDMA5, "ICH3" },
1808 { ATA_I82801CA_1, 0, 0, 0x00, ATA_UDMA5, "ICH3" },
1809 { ATA_I82801DB, 0, 0, 0x00, ATA_UDMA5, "ICH4" },
1810 { ATA_I82801DB_1, 0, 0, 0x00, ATA_UDMA5, "ICH4" },
1811 { ATA_I82801EB, 0, 0, 0x00, ATA_UDMA5, "ICH5" },
1812 { ATA_I82801EB_S1, 0, 0, 0x00, ATA_SA150, "ICH5" },
1813 { ATA_I82801EB_R1, 0, 0, 0x00, ATA_SA150, "ICH5" },
1814 { ATA_I6300ESB, 0, 0, 0x00, ATA_UDMA5, "6300ESB" },
1815 { ATA_I6300ESB_S1, 0, 0, 0x00, ATA_SA150, "6300ESB" },
1816 { ATA_I6300ESB_R1, 0, 0, 0x00, ATA_SA150, "6300ESB" },
1817 { ATA_I82801FB, 0, 0, 0x00, ATA_UDMA5, "ICH6" },
1818 { ATA_I82801FB_S1, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1819 { ATA_I82801FB_R1, 0, AHCI, 0x00, ATA_SA150, "ICH6" },
1820 { ATA_I82801FBM, 0, AHCI, 0x00, ATA_SA150, "ICH6M" },
1821 { ATA_I82801GB, 0, 0, 0x00, ATA_UDMA5, "ICH7" },
1822 { ATA_I82801GB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1823 { ATA_I82801GB_R1, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1824 { ATA_I82801GB_AH, 0, AHCI, 0x00, ATA_SA300, "ICH7" },
1825 { ATA_I82801GBM_S1, 0, AHCI, 0x00, ATA_SA300, "ICH7M" },
1826 { ATA_I82801GBM_R1, 0, AHCI, 0x00, ATA_SA300, "ICH7M" },
1827 { ATA_I82801GBM_AH, 0, AHCI, 0x00, ATA_SA300, "ICH7M" },
1828 { ATA_I63XXESB2, 0, 0, 0x00, ATA_UDMA5, "63XXESB2" },
1829 { ATA_I63XXESB2_S1, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1830 { ATA_I63XXESB2_S2, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1831 { ATA_I63XXESB2_R1, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1832 { ATA_I63XXESB2_R2, 0, AHCI, 0x00, ATA_SA300, "63XXESB2" },
1833 { ATA_I82801HB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1834 { ATA_I82801HB_S2, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1835 { ATA_I82801HB_R1, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1836 { ATA_I82801HB_AH4, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1837 { ATA_I82801HB_AH6, 0, AHCI, 0x00, ATA_SA300, "ICH8" },
1838 { ATA_I82801HBM_S1, 0, 0, 0x00, ATA_SA300, "ICH8M" },
1839 { ATA_I82801HBM_S2, 0, AHCI, 0x00, ATA_SA300, "ICH8M" },
1840 { ATA_I82801HBM_S3, 0, AHCI, 0x00, ATA_SA300, "ICH8M" },
1841 { ATA_I82801IB_S1, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1842 { ATA_I82801IB_S2, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1843 { ATA_I82801IB_AH2, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1844 { ATA_I82801IB_AH4, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1845 { ATA_I82801IB_AH6, 0, AHCI, 0x00, ATA_SA300, "ICH9" },
1846 { ATA_I31244, 0, 0, 0x00, ATA_SA150, "31244" },
1847 { 0, 0, 0, 0, 0, 0}};
1850 if (!(idx = ata_match_chip(dev, ids)))
1853 ksprintf(buffer, "Intel %s %s controller",
1854 idx->text, ata_mode2str(idx->max_dma));
1855 device_set_desc_copy(dev, buffer);
1857 ctlr->chipinit = ata_intel_chipinit;
1862 ata_intel_chipinit(device_t dev)
1864 struct ata_pci_controller *ctlr = device_get_softc(dev);
1866 if (ata_setup_interrupt(dev))
1869 /* good old PIIX needs special treatment (not implemented) */
1870 if (ctlr->chip->chipid == ATA_I82371FB) {
1871 ctlr->setmode = ata_intel_old_setmode;
1874 /* the intel 31244 needs special care if in DPA mode */
1875 else if (ctlr->chip->chipid == ATA_I31244) {
1876 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE) {
1877 ctlr->r_type2 = SYS_RES_MEMORY;
1878 ctlr->r_rid2 = PCIR_BAR(0);
1879 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1882 ata_teardown_interrupt(dev);
1886 ctlr->allocate = ata_intel_31244_allocate;
1887 ctlr->reset = ata_intel_31244_reset;
1889 ctlr->setmode = ata_sata_setmode;
1892 /* non SATA intel chips goes here */
1893 else if (ctlr->chip->max_dma < ATA_SA150) {
1894 ctlr->allocate = ata_intel_allocate;
1895 ctlr->setmode = ata_intel_new_setmode;
1898 /* SATA parts can be either compat or AHCI */
1900 /* force all ports active "the legacy way" */
1901 pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f,2);
1903 ctlr->allocate = ata_intel_allocate;
1904 ctlr->reset = ata_intel_reset;
1907 * if we have AHCI capability and BAR(5) as a memory resource
1908 * and AHCI or RAID mode enabled in BIOS we go for AHCI mode
1910 if ((ctlr->chip->cfg1 == AHCI) &&
1911 (pci_read_config(dev, 0x90, 1) & 0xc0) &&
1912 (ata_ahci_chipinit(dev) != ENXIO))
1915 /* if BAR(5) is IO it should point to SATA interface registers */
1916 ctlr->r_type2 = SYS_RES_IOPORT;
1917 ctlr->r_rid2 = PCIR_BAR(5);
1918 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
1919 &ctlr->r_rid2, RF_ACTIVE)))
1920 ctlr->setmode = ata_intel_sata_setmode;
1922 ctlr->setmode = ata_sata_setmode;
1924 /* enable PCI interrupt */
1925 pci_write_config(dev, PCIR_COMMAND,
1926 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
1932 ata_intel_allocate(device_t dev)
1934 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
1935 struct ata_channel *ch = device_get_softc(dev);
1937 /* setup the usual register normal pci style */
1938 if (ata_pci_allocate(dev))
1941 /* if r_res2 is valid it points to SATA interface registers */
1943 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
1944 ch->r_io[ATA_IDX_ADDR].offset = 0x00;
1945 ch->r_io[ATA_IDX_DATA].res = ctlr->r_res2;
1946 ch->r_io[ATA_IDX_DATA].offset = 0x04;
1949 ch->flags |= ATA_ALWAYS_DMASTAT;
1954 ata_intel_reset(device_t dev)
1956 device_t parent = device_get_parent(dev);
1957 struct ata_pci_controller *ctlr = device_get_softc(parent);
1958 struct ata_channel *ch = device_get_softc(dev);
1961 /* ICH6 & ICH7 in compat mode has 4 SATA ports as master/slave on 2 ch's */
1962 if (ctlr->chip->cfg1) {
1963 mask = (0x0005 << ch->unit);
1966 /* ICH5 in compat mode has SATA ports as master/slave on 1 channel */
1967 if (pci_read_config(parent, 0x90, 1) & 0x04)
1970 mask = (0x0001 << ch->unit);
1971 /* XXX SOS should be in intel_allocate if we grow it */
1972 ch->flags |= ATA_NO_SLAVE;
1975 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) & ~mask, 2);
1977 pci_write_config(parent, 0x92, pci_read_config(parent, 0x92, 2) | mask, 2);
1979 /* wait up to 1 sec for "connect well" */
1980 for (timeout = 0; timeout < 100 ; timeout++) {
1981 if (((pci_read_config(parent, 0x92, 2) & (mask << 4)) == (mask << 4)) &&
1982 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
1986 ata_generic_reset(dev);
1990 ata_intel_old_setmode(device_t dev, int mode)
1996 ata_intel_new_setmode(device_t dev, int mode)
1998 device_t gparent = GRANDPARENT(dev);
1999 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2000 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2001 struct ata_device *atadev = device_get_softc(dev);
2002 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2003 u_int32_t reg40 = pci_read_config(gparent, 0x40, 4);
2004 u_int8_t reg44 = pci_read_config(gparent, 0x44, 1);
2005 u_int8_t reg48 = pci_read_config(gparent, 0x48, 1);
2006 u_int16_t reg4a = pci_read_config(gparent, 0x4a, 2);
2007 u_int16_t reg54 = pci_read_config(gparent, 0x54, 2);
2008 u_int32_t mask40 = 0, new40 = 0;
2009 u_int8_t mask44 = 0, new44 = 0;
2011 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x10, 0x21, 0x23,
2012 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
2013 /* PIO0 PIO1 PIO2 PIO3 PIO4 WDMA0 WDMA1 WDMA2 */
2014 /* UDMA0 UDMA1 UDMA2 UDMA3 UDMA4 UDMA5 UDMA6 */
2016 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2018 if ( mode > ATA_UDMA2 && !(reg54 & (0x10 << devno))) {
2019 ata_print_cable(dev, "controller");
2023 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2026 device_printf(dev, "%ssetting %s on %s chip\n",
2027 (error) ? "FAILURE " : "",
2028 ata_mode2str(mode), ctlr->chip->text);
2033 * reg48: 1 bit per (primary drive 0, primary drive 1, secondary
2034 * drive 0, secondary drive 1)
2036 * 0 Disable Ultra DMA mode
2037 * 1 Enable Ultra DMA mode
2039 * reg4a: 4 bits per (primary drive 0, primary drive 1, secondary
2040 * drive 0, secondary drive 1).
2042 * 0001 UDMA mode 1, 3, 5
2043 * 0010 UDMA mode 2, 4, reserved
2045 * (top two bits for each drive reserved)
2049 "regs before 40=%08x 44=%02x 48=%02x 4a=%04x 54=%04x\n",
2050 reg40, reg44, reg48 ,reg4a, reg54);
2052 reg48 &= ~(0x0001 << devno);
2053 reg4a &= ~(0x3 << (devno << 2));
2054 if (mode >= ATA_UDMA0) {
2055 reg48 |= 0x0001 << devno;
2056 if (mode > ATA_UDMA0)
2057 reg4a |= (1 + !(mode & 0x01)) << (devno << 2);
2059 pci_write_config(gparent, 0x48, reg48, 2);
2060 pci_write_config(gparent, 0x4a, reg4a, 2);
2066 * 19:18 Secondary ATA signal mode
2067 * 17:16 Primary ATA signal mode
2068 * 00 = Normal (enabled)
2069 * 01 = Tri-state (disabled)
2070 * 10 = Drive Low (disabled)
2073 * 15 Secondary drive 1 - Base Clock
2074 * 14 Secondary drive 0 - Base Clock
2075 * 13 Primary drive 1 - Base Clock
2076 * 12 Primary drive 0 - Base Clock
2077 * 0 = Select 33 MHz clock
2078 * 1 = Select 100 Mhz clock
2081 * 10 Vendor specific (set by BIOS?)
2084 * 07 Secondary drive 1 - Cable Type
2085 * 06 Secondary drive 0 - Cable Type
2086 * 05 Primary drive 1 - Cable Type
2087 * 04 Primary drive 0 - Cable Type
2089 * 1 = 80 Conductor (or high speed cable)
2091 * 03 Secondary drive 1 - Select 33/66 clock
2092 * 02 Secondary drive 0 - Select 33/66 clock
2093 * 01 Primary drive 1 - Select 33/66 clock
2094 * 00 Primary drive 0 - Select 33/66 clock
2098 * It is unclear what this should be set to when operating
2101 * NOTE: UDMA2 = 33 MHz
2102 * UDMA3 = 40 MHz (?) - unsupported
2107 reg54 |= 0x0400; /* set vendor specific bit */
2108 reg54 &= ~((0x1 << devno) | (0x1000 << devno));
2110 if (mode >= ATA_UDMA5)
2111 reg54 |= (0x1000 << devno);
2112 else if (mode >= ATA_UDMA3) /* XXX should this be ATA_UDMA3 or 4? */
2113 reg54 |= (0x1 << devno);
2115 pci_write_config(gparent, 0x54, reg54, 2);
2118 * Reg40 (32 bits... well, actually two 16 bit registers)
2120 * Primary channel bits 15:00, Secondary channel bits 31:00. Note
2121 * that slave timings are handled in register 44.
2123 * 15 ATA Decode Enable (R/W) 1 = enable decoding of I/O ranges
2125 * 14 Slave ATA Timing Register Enable (R/W)
2127 * 13:12 IORDY Sample Mode
2130 * 10 PIO-3, PIO-4, MW-1, MW-2
2135 * 09:08 Recovery Mode
2136 * 00 PIO-0, PIO-2, SW-2
2141 * 07:04 Secondary Device Control Bits
2142 * 03:00 Primary Device Control Bits
2144 * bit 3 DMA Timing Enable
2146 * bit 2 Indicate Presence of ATA(1) or ATAPI(0) device
2148 * bit 1 Enable IORDY sample point capability for PIO
2149 * xfers. Always enabled for PIO4 and PIO3, enabled
2150 * for PIO2 if indicated by the device, and otherwise
2151 * probably should be 0.
2153 * bit 0 Fast Drive Timing Enable. Enables faster then PIO-0
2158 * Modify reg40 according to the table
2160 if (atadev->unit == ATA_MASTER) {
2162 new40 = timings[ata_mode2idx(mode)] << 8;
2166 new44 = ((timings[ata_mode2idx(mode)] & 0x30) >> 2) |
2167 (timings[ata_mode2idx(mode)] & 0x03);
2171 * Slave ATA timing register enable
2177 * Device control bits 3:0 for master, 7:4 for slave.
2179 * bit3 DMA Timing enable.
2180 * bit2 Indicate presence of ATA(1) or ATAPI(0) device, set accordingly
2181 * bit1 Enable IORDY sample point capability for PIO xfers. Always
2182 * enabled for PIO4 and PIO3, enabled for PIO2 if indicated by
2183 * the device, and otherwise should be 0.
2184 * bit0 Fast Drive Timing Enable. Enable faster then PIO-0 timing modes.
2189 if (atadev->unit == ATA_MASTER) {
2192 if (!ata_atapi(dev))
2197 if (!ata_atapi(dev))
2201 reg40 &= ~0x00ff00ff;
2202 reg40 |= 0x40774077;
2206 * Primary or Secondary controller
2214 pci_write_config(gparent, 0x40, (reg40 & ~mask40) | new40, 4);
2215 pci_write_config(gparent, 0x44, (reg44 & ~mask44) | new44, 1);
2218 reg40 = pci_read_config(gparent, 0x40, 4);
2219 reg44 = pci_read_config(gparent, 0x44, 1);
2220 reg48 = pci_read_config(gparent, 0x48, 1);
2221 reg4a = pci_read_config(gparent, 0x4a, 2);
2222 reg54 = pci_read_config(gparent, 0x54, 2);
2224 "regs after 40=%08x 44=%02x 48=%02x 4a=%04x 54=%04x\n",
2225 reg40, reg44, reg48 ,reg4a, reg54);
2228 atadev->mode = mode;
2232 ata_intel_sata_setmode(device_t dev, int mode)
2234 struct ata_device *atadev = device_get_softc(dev);
2236 if (atadev->param.satacapabilities != 0x0000 &&
2237 atadev->param.satacapabilities != 0xffff) {
2239 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2240 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2242 /* on some drives we need to set the transfer mode */
2243 ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0,
2244 ata_limit_mode(dev, mode, ATA_UDMA6));
2246 /* set ATA_SSTATUS register offset */
2247 ATA_IDX_OUTL(ch, ATA_IDX_ADDR, devno * 0x100);
2249 /* query SATA STATUS for the speed */
2250 if ((ATA_IDX_INL(ch, ATA_IDX_DATA) & ATA_SS_CONWELL_MASK) ==
2251 ATA_SS_CONWELL_GEN2)
2252 atadev->mode = ATA_SA300;
2254 atadev->mode = ATA_SA150;
2257 mode = ata_limit_mode(dev, mode, ATA_UDMA5);
2258 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
2259 atadev->mode = mode;
2264 ata_intel_31244_allocate(device_t dev)
2266 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2267 struct ata_channel *ch = device_get_softc(dev);
2271 ch_offset = 0x200 + ch->unit * 0x200;
2273 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
2274 ch->r_io[i].res = ctlr->r_res2;
2276 /* setup ATA registers */
2277 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
2278 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x06;
2279 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
2280 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
2281 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
2282 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
2283 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
2284 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1d;
2285 ch->r_io[ATA_ERROR].offset = ch_offset + 0x04;
2286 ch->r_io[ATA_STATUS].offset = ch_offset + 0x1c;
2287 ch->r_io[ATA_ALTSTAT].offset = ch_offset + 0x28;
2288 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x29;
2290 /* setup DMA registers */
2291 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x100;
2292 ch->r_io[ATA_SERROR].offset = ch_offset + 0x104;
2293 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x108;
2295 /* setup SATA registers */
2296 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x70;
2297 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x72;
2298 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x74;
2300 ch->flags |= ATA_NO_SLAVE;
2302 ch->hw.status = ata_intel_31244_status;
2303 ch->hw.command = ata_intel_31244_command;
2305 /* enable PHY state change interrupt */
2306 ATA_OUTL(ctlr->r_res2, 0x4,
2307 ATA_INL(ctlr->r_res2, 0x04) | (0x01 << (ch->unit << 3)));
2312 ata_intel_31244_status(device_t dev)
2314 /* do we have any PHY events ? */
2315 ata_sata_phy_check_events(dev);
2317 /* any drive action to take care of ? */
2318 return ata_pci_status(dev);
2322 ata_intel_31244_command(struct ata_request *request)
2324 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2325 struct ata_device *atadev = device_get_softc(request->dev);
2328 if (!(atadev->flags & ATA_D_48BIT_ACTIVE))
2329 return (ata_generic_command(request));
2331 lba = request->u.ata.lba;
2332 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_D_LBA | atadev->unit);
2333 /* enable interrupt */
2334 ATA_IDX_OUTB(ch, ATA_CONTROL, ATA_A_4BIT);
2335 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
2336 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
2337 ATA_IDX_OUTW(ch, ATA_SECTOR, ((lba >> 16) & 0xff00) | (lba & 0x00ff));
2338 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((lba >> 24) & 0xff00) |
2339 ((lba >> 8) & 0x00ff));
2340 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((lba >> 32) & 0xff00) |
2341 ((lba >> 16) & 0x00ff));
2343 /* issue command to controller */
2344 ATA_IDX_OUTB(ch, ATA_COMMAND, request->u.ata.command);
2350 ata_intel_31244_reset(device_t dev)
2352 if (ata_sata_phy_reset(dev))
2353 ata_generic_reset(dev);
2358 * Integrated Technology Express Inc. (ITE) chipset support functions
2361 ata_ite_ident(device_t dev)
2363 struct ata_pci_controller *ctlr = device_get_softc(dev);
2364 struct ata_chip_id *idx;
2365 static struct ata_chip_id ids[] =
2366 {{ ATA_IT8212F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8212F" },
2367 { ATA_IT8211F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8211F" },
2368 { 0, 0, 0, 0, 0, 0}};
2371 if (!(idx = ata_match_chip(dev, ids)))
2374 ksprintf(buffer, "ITE %s %s controller",
2375 idx->text, ata_mode2str(idx->max_dma));
2376 device_set_desc_copy(dev, buffer);
2378 ctlr->chipinit = ata_ite_chipinit;
2383 ata_ite_chipinit(device_t dev)
2385 struct ata_pci_controller *ctlr = device_get_softc(dev);
2387 if (ata_setup_interrupt(dev))
2390 ctlr->setmode = ata_ite_setmode;
2392 /* set PCI mode and 66Mhz reference clock */
2393 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
2395 /* set default active & recover timings */
2396 pci_write_config(dev, 0x54, 0x31, 1);
2397 pci_write_config(dev, 0x56, 0x31, 1);
2402 ata_ite_setmode(device_t dev, int mode)
2404 device_t gparent = GRANDPARENT(dev);
2405 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2406 struct ata_device *atadev = device_get_softc(dev);
2407 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
2410 /* correct the mode for what the HW supports */
2411 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2413 /* check the CBLID bits for 80 conductor cable detection */
2414 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
2415 (ch->unit ? (1<<3) : (1<<2)))) {
2416 ata_print_cable(dev, "controller");
2420 /* set the wanted mode on the device */
2421 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
2424 device_printf(dev, "%s setting %s on ITE8212F chip\n",
2425 (error) ? "failed" : "success", ata_mode2str(mode));
2427 /* if the device accepted the mode change, setup the HW accordingly */
2429 if (mode >= ATA_UDMA0) {
2430 u_int8_t udmatiming[] =
2431 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
2433 /* enable UDMA mode */
2434 pci_write_config(gparent, 0x50,
2435 pci_read_config(gparent, 0x50, 1) &
2436 ~(1 << (devno + 3)), 1);
2438 /* set UDMA timing */
2439 pci_write_config(gparent,
2440 0x56 + (ch->unit << 2) + ATA_DEV(atadev->unit),
2441 udmatiming[mode & ATA_MODE_MASK], 1);
2444 u_int8_t chtiming[] =
2445 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
2447 /* disable UDMA mode */
2448 pci_write_config(gparent, 0x50,
2449 pci_read_config(gparent, 0x50, 1) |
2450 (1 << (devno + 3)), 1);
2452 /* set active and recover timing (shared between master & slave) */
2453 if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
2454 chtiming[ata_mode2idx(mode)])
2455 pci_write_config(gparent, 0x54 + (ch->unit << 2),
2456 chtiming[ata_mode2idx(mode)], 1);
2458 atadev->mode = mode;
2464 * JMicron chipset support functions
2467 ata_jmicron_ident(device_t dev)
2469 struct ata_pci_controller *ctlr = device_get_softc(dev);
2470 struct ata_chip_id *idx;
2471 static struct ata_chip_id ids[] =
2472 {{ ATA_JMB360, 0, 1, 0, ATA_SA300, "JMB360" },
2473 { ATA_JMB361, 0, 1, 1, ATA_SA300, "JMB361" },
2474 { ATA_JMB363, 0, 2, 1, ATA_SA300, "JMB363" },
2475 { ATA_JMB365, 0, 1, 2, ATA_SA300, "JMB365" },
2476 { ATA_JMB366, 0, 2, 2, ATA_SA300, "JMB366" },
2477 { ATA_JMB368, 0, 0, 1, ATA_UDMA6, "JMB368" },
2478 { 0, 0, 0, 0, 0, 0}};
2481 if (!(idx = ata_match_chip(dev, ids)))
2484 if ((pci_read_config(dev, 0xdf, 1) & 0x40) &&
2485 (pci_get_function(dev) == (pci_read_config(dev, 0x40, 1) & 0x02 >> 1)))
2486 ksnprintf(buffer, sizeof(buffer), "JMicron %s %s controller",
2487 idx->text, ata_mode2str(ATA_UDMA6));
2489 ksnprintf(buffer, sizeof(buffer), "JMicron %s %s controller",
2490 idx->text, ata_mode2str(idx->max_dma));
2491 device_set_desc_copy(dev, buffer);
2493 ctlr->chipinit = ata_jmicron_chipinit;
2498 ata_jmicron_chipinit(device_t dev)
2500 struct ata_pci_controller *ctlr = device_get_softc(dev);
2503 if (ata_setup_interrupt(dev))
2506 /* do we have multiple PCI functions ? */
2507 if (pci_read_config(dev, 0xdf, 1) & 0x40) {
2508 /* are we on the AHCI part ? */
2509 if (ata_ahci_chipinit(dev) != ENXIO)
2512 /* otherwise we are on the PATA part */
2513 ctlr->allocate = ata_pci_allocate;
2514 ctlr->reset = ata_generic_reset;
2515 ctlr->dmainit = ata_pci_dmainit;
2516 ctlr->setmode = ata_jmicron_setmode;
2517 ctlr->channels = ctlr->chip->cfg2;
2520 /* set controller configuration to a combined setup we support */
2521 pci_write_config(dev, 0x40, 0x80c0a131, 4);
2522 pci_write_config(dev, 0x80, 0x01200000, 4);
2524 if ((error = ata_ahci_chipinit(dev))) {
2525 ata_teardown_interrupt(dev);
2529 ctlr->allocate = ata_jmicron_allocate;
2530 ctlr->reset = ata_jmicron_reset;
2531 ctlr->dmainit = ata_jmicron_dmainit;
2532 ctlr->setmode = ata_jmicron_setmode;
2534 /* set the number of HW channels */
2535 ctlr->channels = ctlr->chip->cfg1 + ctlr->chip->cfg2;
2541 ata_jmicron_allocate(device_t dev)
2543 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2544 struct ata_channel *ch = device_get_softc(dev);
2547 if (ch->unit >= ctlr->chip->cfg1) {
2548 ch->unit -= ctlr->chip->cfg1;
2549 error = ata_pci_allocate(dev);
2550 ch->unit += ctlr->chip->cfg1;
2553 error = ata_ahci_allocate(dev);
2558 ata_jmicron_reset(device_t dev)
2560 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2561 struct ata_channel *ch = device_get_softc(dev);
2563 if (ch->unit >= ctlr->chip->cfg1)
2564 ata_generic_reset(dev);
2566 ata_ahci_reset(dev);
2570 ata_jmicron_dmainit(device_t dev)
2572 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2573 struct ata_channel *ch = device_get_softc(dev);
2575 if (ch->unit >= ctlr->chip->cfg1)
2576 ata_pci_dmainit(dev);
2578 ata_ahci_dmainit(dev);
2582 ata_jmicron_setmode(device_t dev, int mode)
2584 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
2585 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
2587 if (pci_read_config(dev, 0xdf, 1) & 0x40 || ch->unit >= ctlr->chip->cfg1) {
2588 struct ata_device *atadev = device_get_softc(dev);
2590 /* check for 80pin cable present */
2591 if (pci_read_config(dev, 0x40, 1) & 0x08)
2592 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
2594 mode = ata_limit_mode(dev, mode, ATA_UDMA6);
2596 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
2597 atadev->mode = mode;
2600 ata_sata_setmode(dev, mode);
2605 * Marvell chipset support functions
2607 #define ATA_MV_HOST_BASE(ch) \
2608 ((ch->unit & 3) * 0x0100) + (ch->unit > 3 ? 0x30000 : 0x20000)
2609 #define ATA_MV_EDMA_BASE(ch) \
2610 ((ch->unit & 3) * 0x2000) + (ch->unit > 3 ? 0x30000 : 0x20000)
2612 struct ata_marvell_response {
2614 u_int8_t edma_status;
2615 u_int8_t dev_status;
2616 u_int32_t timestamp;
2619 struct ata_marvell_dma_prdentry {
2627 ata_marvell_ident(device_t dev)
2629 struct ata_pci_controller *ctlr = device_get_softc(dev);
2630 struct ata_chip_id *idx;
2631 static struct ata_chip_id ids[] =
2632 {{ ATA_M88SX5040, 0, 4, MV50XX, ATA_SA150, "88SX5040" },
2633 { ATA_M88SX5041, 0, 4, MV50XX, ATA_SA150, "88SX5041" },
2634 { ATA_M88SX5080, 0, 8, MV50XX, ATA_SA150, "88SX5080" },
2635 { ATA_M88SX5081, 0, 8, MV50XX, ATA_SA150, "88SX5081" },
2636 { ATA_M88SX6041, 0, 4, MV60XX, ATA_SA300, "88SX6041" },
2637 { ATA_M88SX6081, 0, 8, MV60XX, ATA_SA300, "88SX6081" },
2638 { ATA_M88SX6101, 0, 1, MV61XX, ATA_UDMA6, "88SX6101" },
2639 { ATA_M88SX6145, 0, 2, MV61XX, ATA_UDMA6, "88SX6145" },
2640 { 0, 0, 0, 0, 0, 0}};
2643 if (!(idx = ata_match_chip(dev, ids)))
2646 ksprintf(buffer, "Marvell %s %s controller",
2647 idx->text, ata_mode2str(idx->max_dma));
2648 device_set_desc_copy(dev, buffer);
2650 switch (ctlr->chip->cfg2) {
2653 ctlr->chipinit = ata_marvell_edma_chipinit;
2656 ctlr->chipinit = ata_marvell_pata_chipinit;
2663 ata_marvell_pata_chipinit(device_t dev)
2665 struct ata_pci_controller *ctlr = device_get_softc(dev);
2667 if (ata_setup_interrupt(dev))
2670 ctlr->allocate = ata_marvell_pata_allocate;
2671 ctlr->setmode = ata_marvell_pata_setmode;
2672 ctlr->channels = ctlr->chip->cfg1;
2677 ata_marvell_pata_allocate(device_t dev)
2679 struct ata_channel *ch = device_get_softc(dev);
2681 /* setup the usual register normal pci style */
2682 if (ata_pci_allocate(dev))
2685 /* dont use 32 bit PIO transfers */
2686 ch->flags |= ATA_USE_16BIT;
2692 ata_marvell_pata_setmode(device_t dev, int mode)
2694 device_t gparent = GRANDPARENT(dev);
2695 struct ata_pci_controller *ctlr = device_get_softc(gparent);
2696 struct ata_device *atadev = device_get_softc(dev);
2698 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
2699 mode = ata_check_80pin(dev, mode);
2700 if (!ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode))
2701 atadev->mode = mode;
2705 ata_marvell_edma_chipinit(device_t dev)
2707 struct ata_pci_controller *ctlr = device_get_softc(dev);
2709 if (ata_setup_interrupt(dev))
2712 ctlr->r_type1 = SYS_RES_MEMORY;
2713 ctlr->r_rid1 = PCIR_BAR(0);
2714 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
2715 &ctlr->r_rid1, RF_ACTIVE))) {
2716 ata_teardown_interrupt(dev);
2720 /* mask all host controller interrupts */
2721 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x00000000);
2723 /* mask all PCI interrupts */
2724 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x00000000);
2726 ctlr->allocate = ata_marvell_edma_allocate;
2727 ctlr->reset = ata_marvell_edma_reset;
2728 ctlr->dmainit = ata_marvell_edma_dmainit;
2729 ctlr->setmode = ata_sata_setmode;
2730 ctlr->channels = ctlr->chip->cfg1;
2732 /* clear host controller interrupts */
2733 ATA_OUTL(ctlr->r_res1, 0x20014, 0x00000000);
2734 if (ctlr->chip->cfg1 > 4)
2735 ATA_OUTL(ctlr->r_res1, 0x30014, 0x00000000);
2737 /* clear PCI interrupts */
2738 ATA_OUTL(ctlr->r_res1, 0x01d58, 0x00000000);
2740 /* unmask PCI interrupts we want */
2741 ATA_OUTL(ctlr->r_res1, 0x01d5c, 0x007fffff);
2743 /* unmask host controller interrupts we want */
2744 ATA_OUTL(ctlr->r_res1, 0x01d64, 0x000000ff/*HC0*/ | 0x0001fe00/*HC1*/ |
2745 /*(1<<19) | (1<<20) | (1<<21) |*/(1<<22) | (1<<24) | (0x7f << 25));
2747 /* enable PCI interrupt */
2748 pci_write_config(dev, PCIR_COMMAND,
2749 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
2754 ata_marvell_edma_allocate(device_t dev)
2756 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2757 struct ata_channel *ch = device_get_softc(dev);
2758 u_int64_t work = ch->dma->work_bus;
2761 /* clear work area */
2762 bzero(ch->dma->work, 1024+256);
2764 /* set legacy ATA resources */
2765 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
2766 ch->r_io[i].res = ctlr->r_res1;
2767 ch->r_io[i].offset = 0x02100 + (i << 2) + ATA_MV_EDMA_BASE(ch);
2769 ch->r_io[ATA_CONTROL].res = ctlr->r_res1;
2770 ch->r_io[ATA_CONTROL].offset = 0x02120 + ATA_MV_EDMA_BASE(ch);
2771 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res1;
2772 ata_default_registers(dev);
2774 /* set SATA resources */
2775 switch (ctlr->chip->cfg2) {
2777 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2778 ch->r_io[ATA_SSTATUS].offset = 0x00100 + ATA_MV_HOST_BASE(ch);
2779 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2780 ch->r_io[ATA_SERROR].offset = 0x00104 + ATA_MV_HOST_BASE(ch);
2781 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2782 ch->r_io[ATA_SCONTROL].offset = 0x00108 + ATA_MV_HOST_BASE(ch);
2785 ch->r_io[ATA_SSTATUS].res = ctlr->r_res1;
2786 ch->r_io[ATA_SSTATUS].offset = 0x02300 + ATA_MV_EDMA_BASE(ch);
2787 ch->r_io[ATA_SERROR].res = ctlr->r_res1;
2788 ch->r_io[ATA_SERROR].offset = 0x02304 + ATA_MV_EDMA_BASE(ch);
2789 ch->r_io[ATA_SCONTROL].res = ctlr->r_res1;
2790 ch->r_io[ATA_SCONTROL].offset = 0x02308 + ATA_MV_EDMA_BASE(ch);
2791 ch->r_io[ATA_SACTIVE].res = ctlr->r_res1;
2792 ch->r_io[ATA_SACTIVE].offset = 0x02350 + ATA_MV_EDMA_BASE(ch);
2796 ch->flags |= ATA_NO_SLAVE;
2797 ch->flags |= ATA_USE_16BIT; /* XXX SOS needed ? */
2798 ata_generic_hw(dev);
2799 ch->hw.begin_transaction = ata_marvell_edma_begin_transaction;
2800 ch->hw.end_transaction = ata_marvell_edma_end_transaction;
2801 ch->hw.status = ata_marvell_edma_status;
2803 /* disable the EDMA machinery */
2804 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2805 DELAY(100000); /* SOS should poll for disabled */
2807 /* set configuration to non-queued 128b read transfers stop on error */
2808 ATA_OUTL(ctlr->r_res1, 0x02000 + ATA_MV_EDMA_BASE(ch), (1<<11) | (1<<13));
2810 /* request queue base high */
2811 ATA_OUTL(ctlr->r_res1, 0x02010 + ATA_MV_EDMA_BASE(ch), work >> 32);
2813 /* request queue in ptr */
2814 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2816 /* request queue out ptr */
2817 ATA_OUTL(ctlr->r_res1, 0x02018 + ATA_MV_EDMA_BASE(ch), 0x0);
2819 /* response queue base high */
2821 ATA_OUTL(ctlr->r_res1, 0x0201c + ATA_MV_EDMA_BASE(ch), work >> 32);
2823 /* response queue in ptr */
2824 ATA_OUTL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch), 0x0);
2826 /* response queue out ptr */
2827 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), work & 0xffffffff);
2829 /* clear SATA error register */
2830 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
2832 /* clear any outstanding error interrupts */
2833 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2835 /* unmask all error interrupts */
2836 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
2838 /* enable EDMA machinery */
2839 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2844 ata_marvell_edma_status(device_t dev)
2846 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
2847 struct ata_channel *ch = device_get_softc(dev);
2848 u_int32_t cause = ATA_INL(ctlr->r_res1, 0x01d60);
2849 int shift = (ch->unit << 1) + (ch->unit > 3);
2851 if (cause & (1 << shift)) {
2853 /* clear interrupt(s) */
2854 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
2856 /* do we have any PHY events ? */
2857 ata_sata_phy_check_events(dev);
2860 /* do we have any device action ? */
2861 return (cause & (2 << shift));
2864 /* must be called with ATA channel locked and state_mtx held */
2866 ata_marvell_edma_begin_transaction(struct ata_request *request)
2868 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2869 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2875 int dummy, error, slot;
2877 /* only DMA R/W goes through the EMDA machine */
2878 if (request->u.ata.command != ATA_READ_DMA &&
2879 request->u.ata.command != ATA_WRITE_DMA) {
2881 /* disable the EDMA machinery */
2882 if (ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)
2883 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
2884 return ata_begin_transaction(request);
2887 /* check for 48 bit access and convert if needed */
2888 ata_modify_if_48bit(request);
2890 /* check sanity, setup SG list and DMA engine */
2891 if ((error = ch->dma->load(ch->dev, request->data, request->bytecount,
2892 request->flags & ATA_R_READ, ch->dma->sg,
2894 device_printf(request->dev, "setting up DMA failed\n");
2895 request->result = error;
2896 return ATA_OP_FINISHED;
2899 /* get next free request queue slot */
2900 req_in = ATA_INL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch));
2901 slot = (((req_in & ~0xfffffc00) >> 5) + 0) & 0x1f;
2902 bytep = (u_int8_t *)(ch->dma->work);
2903 bytep += (slot << 5);
2904 wordp = (u_int16_t *)bytep;
2905 quadp = (u_int32_t *)bytep;
2907 /* fill in this request */
2908 quadp[0] = (long)ch->dma->sg_bus & 0xffffffff;
2909 quadp[1] = (u_int64_t)ch->dma->sg_bus >> 32;
2910 wordp[4] = (request->flags & ATA_R_READ ? 0x01 : 0x00) | (tag<<1);
2913 bytep[i++] = (request->u.ata.count >> 8) & 0xff;
2914 bytep[i++] = 0x10 | ATA_COUNT;
2915 bytep[i++] = request->u.ata.count & 0xff;
2916 bytep[i++] = 0x10 | ATA_COUNT;
2918 bytep[i++] = (request->u.ata.lba >> 24) & 0xff;
2919 bytep[i++] = 0x10 | ATA_SECTOR;
2920 bytep[i++] = request->u.ata.lba & 0xff;
2921 bytep[i++] = 0x10 | ATA_SECTOR;
2923 bytep[i++] = (request->u.ata.lba >> 32) & 0xff;
2924 bytep[i++] = 0x10 | ATA_CYL_LSB;
2925 bytep[i++] = (request->u.ata.lba >> 8) & 0xff;
2926 bytep[i++] = 0x10 | ATA_CYL_LSB;
2928 bytep[i++] = (request->u.ata.lba >> 40) & 0xff;
2929 bytep[i++] = 0x10 | ATA_CYL_MSB;
2930 bytep[i++] = (request->u.ata.lba >> 16) & 0xff;
2931 bytep[i++] = 0x10 | ATA_CYL_MSB;
2933 bytep[i++] = ATA_D_LBA | ATA_D_IBM | ((request->u.ata.lba >> 24) & 0xf);
2934 bytep[i++] = 0x10 | ATA_DRIVE;
2936 bytep[i++] = request->u.ata.command;
2937 bytep[i++] = 0x90 | ATA_COMMAND;
2939 /* enable EDMA machinery if needed */
2940 if (!(ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001)) {
2941 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
2942 while (!(ATA_INL(ctlr->r_res1,
2943 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
2947 /* tell EDMA it has a new request */
2948 slot = (((req_in & ~0xfffffc00) >> 5) + 1) & 0x1f;
2949 req_in &= 0xfffffc00;
2950 req_in += (slot << 5);
2951 ATA_OUTL(ctlr->r_res1, 0x02014 + ATA_MV_EDMA_BASE(ch), req_in);
2953 return ATA_OP_CONTINUES;
2956 /* must be called with ATA channel locked and state_mtx held */
2958 ata_marvell_edma_end_transaction(struct ata_request *request)
2960 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
2961 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
2962 int offset = (ch->unit > 3 ? 0x30014 : 0x20014);
2963 u_int32_t icr = ATA_INL(ctlr->r_res1, offset);
2966 /* EDMA interrupt */
2967 if ((icr & (0x0001 << (ch->unit & 3)))) {
2968 struct ata_marvell_response *response;
2969 u_int32_t rsp_in, rsp_out;
2973 callout_stop(&request->callout);
2975 /* get response ptr's */
2976 rsp_in = ATA_INL(ctlr->r_res1, 0x02020 + ATA_MV_EDMA_BASE(ch));
2977 rsp_out = ATA_INL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch));
2978 slot = (((rsp_in & ~0xffffff00) >> 3)) & 0x1f;
2979 rsp_out &= 0xffffff00;
2980 rsp_out += (slot << 3);
2981 response = (struct ata_marvell_response *)
2982 (ch->dma->work + 1024 + (slot << 3));
2984 /* record status for this request */
2985 request->status = response->dev_status;
2989 ATA_OUTL(ctlr->r_res1, 0x02024 + ATA_MV_EDMA_BASE(ch), rsp_out);
2991 /* update progress */
2992 if (!(request->status & ATA_S_ERROR) &&
2993 !(request->flags & ATA_R_TIMEOUT))
2994 request->donecount = request->bytecount;
2996 /* unload SG list */
2997 ch->dma->unload(ch->dev);
2999 res = ATA_OP_FINISHED;
3002 /* legacy ATA interrupt */
3004 res = ata_end_transaction(request);
3008 ATA_OUTL(ctlr->r_res1, offset, ~(icr & (0x0101 << (ch->unit & 3))));
3013 ata_marvell_edma_reset(device_t dev)
3015 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3016 struct ata_channel *ch = device_get_softc(dev);
3018 /* disable the EDMA machinery */
3019 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000002);
3020 while ((ATA_INL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch)) & 0x00000001))
3023 /* clear SATA error register */
3024 ATA_IDX_OUTL(ch, ATA_SERROR, ATA_IDX_INL(ch, ATA_SERROR));
3026 /* clear any outstanding error interrupts */
3027 ATA_OUTL(ctlr->r_res1, 0x02008 + ATA_MV_EDMA_BASE(ch), 0x0);
3029 /* unmask all error interrupts */
3030 ATA_OUTL(ctlr->r_res1, 0x0200c + ATA_MV_EDMA_BASE(ch), ~0x0);
3032 /* enable channel and test for devices */
3033 if (ata_sata_phy_reset(dev))
3034 ata_generic_reset(dev);
3036 /* enable EDMA machinery */
3037 ATA_OUTL(ctlr->r_res1, 0x02028 + ATA_MV_EDMA_BASE(ch), 0x00000001);
3041 ata_marvell_edma_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs,
3044 struct ata_dmasetprd_args *args = xsc;
3045 struct ata_marvell_dma_prdentry *prd = args->dmatab;
3048 if ((args->error = error))
3051 for (i = 0; i < nsegs; i++) {
3052 prd[i].addrlo = htole32(segs[i].ds_addr);
3053 prd[i].count = htole32(segs[i].ds_len);
3054 prd[i].addrhi = htole32((u_int64_t)segs[i].ds_addr >> 32);
3056 prd[i - 1].count |= htole32(ATA_DMA_EOT);
3060 ata_marvell_edma_dmainit(device_t dev)
3062 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3063 struct ata_channel *ch = device_get_softc(dev);
3067 /* note start and stop are not used here */
3068 ch->dma->setprd = ata_marvell_edma_dmasetprd;
3070 if (ATA_INL(ctlr->r_res1, 0x00d00) & 0x00000004)
3071 ch->dma->max_address = BUS_SPACE_MAXADDR;
3073 /* chip does not reliably do 64K DMA transfers */
3074 ch->dma->max_iosize = 126 * DEV_BSIZE;
3080 * National chipset support functions
3083 ata_national_ident(device_t dev)
3085 struct ata_pci_controller *ctlr = device_get_softc(dev);
3087 /* this chip is a clone of the Cyrix chip, bugs and all */
3088 if (pci_get_devid(dev) == ATA_SC1100) {
3089 device_set_desc(dev, "National Geode SC1100 ATA33 controller");
3090 ctlr->chipinit = ata_national_chipinit;
3097 ata_national_chipinit(device_t dev)
3099 struct ata_pci_controller *ctlr = device_get_softc(dev);
3101 if (ata_setup_interrupt(dev))
3104 ctlr->setmode = ata_national_setmode;
3109 ata_national_setmode(device_t dev, int mode)
3111 device_t gparent = GRANDPARENT(dev);
3112 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3113 struct ata_device *atadev = device_get_softc(dev);
3114 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3115 u_int32_t piotiming[] =
3116 { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
3117 0x00803020, 0x20102010, 0x00100010,
3118 0x00100010, 0x00100010, 0x00100010 };
3119 u_int32_t dmatiming[] = { 0x80077771, 0x80012121, 0x80002020 };
3120 u_int32_t udmatiming[] = { 0x80921250, 0x80911140, 0x80911030 };
3123 ch->dma->alignment = 16;
3124 ch->dma->max_iosize = 126 * DEV_BSIZE;
3126 mode = ata_limit_mode(dev, mode, ATA_UDMA2);
3128 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3131 device_printf(dev, "%s setting %s on National chip\n",
3132 (error) ? "failed" : "success", ata_mode2str(mode));
3134 if (mode >= ATA_UDMA0) {
3135 pci_write_config(gparent, 0x44 + (devno << 3),
3136 udmatiming[mode & ATA_MODE_MASK], 4);
3138 else if (mode >= ATA_WDMA0) {
3139 pci_write_config(gparent, 0x44 + (devno << 3),
3140 dmatiming[mode & ATA_MODE_MASK], 4);
3143 pci_write_config(gparent, 0x44 + (devno << 3),
3144 pci_read_config(gparent, 0x44 + (devno << 3), 4) |
3147 pci_write_config(gparent, 0x40 + (devno << 3),
3148 piotiming[ata_mode2idx(mode)], 4);
3149 atadev->mode = mode;
3154 * NetCell chipset support functions
3157 ata_netcell_ident(device_t dev)
3159 struct ata_pci_controller *ctlr = device_get_softc(dev);
3161 if (pci_get_devid(dev) == ATA_NETCELL_SR) {
3162 device_set_desc(dev, "Netcell SyncRAID SR3000/5000 RAID Controller");
3163 ctlr->chipinit = ata_netcell_chipinit;
3170 ata_netcell_chipinit(device_t dev)
3172 struct ata_pci_controller *ctlr = device_get_softc(dev);
3174 if (ata_generic_chipinit(dev))
3177 ctlr->allocate = ata_netcell_allocate;
3182 ata_netcell_allocate(device_t dev)
3184 struct ata_channel *ch = device_get_softc(dev);
3186 /* setup the usual register normal pci style */
3187 if (ata_pci_allocate(dev))
3190 /* the NetCell only supports 16 bit PIO transfers */
3191 ch->flags |= ATA_USE_16BIT;
3198 * nVidia chipset support functions
3201 ata_nvidia_ident(device_t dev)
3203 struct ata_pci_controller *ctlr = device_get_softc(dev);
3204 struct ata_chip_id *idx;
3205 static struct ata_chip_id ids[] =
3206 {{ ATA_NFORCE1, 0, AMDNVIDIA, NVIDIA, ATA_UDMA5, "nForce" },
3207 { ATA_NFORCE2, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2" },
3208 { ATA_NFORCE2_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce2 Pro" },
3209 { ATA_NFORCE2_PRO_S1, 0, 0, 0, ATA_SA150, "nForce2 Pro" },
3210 { ATA_NFORCE3, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3" },
3211 { ATA_NFORCE3_PRO, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce3 Pro" },
3212 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
3213 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
3214 { ATA_NFORCE_MCP04, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP" },
3215 { ATA_NFORCE_MCP04_S1, 0, 0, NV4, ATA_SA150, "nForce MCP" },
3216 { ATA_NFORCE_MCP04_S2, 0, 0, NV4, ATA_SA150, "nForce MCP" },
3217 { ATA_NFORCE_CK804, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce CK804" },
3218 { ATA_NFORCE_CK804_S1, 0, 0, NV4, ATA_SA300, "nForce CK804" },
3219 { ATA_NFORCE_CK804_S2, 0, 0, NV4, ATA_SA300, "nForce CK804" },
3220 { ATA_NFORCE_MCP51, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP51" },
3221 { ATA_NFORCE_MCP51_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP51" },
3222 { ATA_NFORCE_MCP51_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP51" },
3223 { ATA_NFORCE_MCP55, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP55" },
3224 { ATA_NFORCE_MCP55_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP55" },
3225 { ATA_NFORCE_MCP55_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP55" },
3226 { ATA_NFORCE_MCP61, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP61" },
3227 { ATA_NFORCE_MCP61_S1, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP61" },
3228 { ATA_NFORCE_MCP61_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP61" },
3229 { ATA_NFORCE_MCP61_S3, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP61" },
3230 { ATA_NFORCE_MCP65, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP65" },
3231 { ATA_NFORCE_MCP67, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP67" },
3232 { ATA_NFORCE_MCP67_S2, 0, 0, NV4|NVQ, ATA_SA300, "nForce MCP67" },
3233 { ATA_NFORCE_MCP73, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP73" },
3234 { ATA_NFORCE_MCP77, 0, AMDNVIDIA, NVIDIA, ATA_UDMA6, "nForce MCP77" },
3235 { 0, 0, 0, 0, 0, 0}} ;
3238 if (!(idx = ata_match_chip(dev, ids)))
3241 ksprintf(buffer, "nVidia %s %s controller",
3242 idx->text, ata_mode2str(idx->max_dma));
3243 device_set_desc_copy(dev, buffer);
3245 ctlr->chipinit = ata_nvidia_chipinit;
3250 ata_nvidia_chipinit(device_t dev)
3252 struct ata_pci_controller *ctlr = device_get_softc(dev);
3254 if (ata_setup_interrupt(dev))
3257 if (ctlr->chip->max_dma >= ATA_SA150) {
3258 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
3259 ctlr->r_type2 = SYS_RES_IOPORT;
3261 ctlr->r_type2 = SYS_RES_MEMORY;
3262 ctlr->r_rid2 = PCIR_BAR(5);
3263 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3264 &ctlr->r_rid2, RF_ACTIVE))) {
3265 int offset = ctlr->chip->cfg2 & NV4 ? 0x0440 : 0x0010;
3267 ctlr->allocate = ata_nvidia_allocate;
3268 ctlr->reset = ata_nvidia_reset;
3270 /* enable control access */
3271 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
3273 if (ctlr->chip->cfg2 & NVQ) {
3274 /* clear interrupt status */
3275 ATA_OUTL(ctlr->r_res2, offset, 0x00ff00ff);
3277 /* enable device and PHY state change interrupts */
3278 ATA_OUTL(ctlr->r_res2, offset + 4, 0x000d000d);
3280 /* disable NCQ support */
3281 ATA_OUTL(ctlr->r_res2, 0x0400,
3282 ATA_INL(ctlr->r_res2, 0x0400) & 0xfffffff9);
3285 /* clear interrupt status */
3286 ATA_OUTB(ctlr->r_res2, offset, 0xff);
3288 /* enable device and PHY state change interrupts */
3289 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
3292 /* enable PCI interrupt */
3293 pci_write_config(dev, PCIR_COMMAND,
3294 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
3297 ctlr->setmode = ata_sata_setmode;
3300 /* disable prefetch, postwrite */
3301 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
3302 ctlr->setmode = ata_via_family_setmode;
3308 ata_nvidia_allocate(device_t dev)
3310 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3311 struct ata_channel *ch = device_get_softc(dev);
3313 /* setup the usual register normal pci style */
3314 if (ata_pci_allocate(dev))
3317 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3318 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
3319 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3320 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
3321 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3322 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
3324 ch->hw.status = ata_nvidia_status;
3325 ch->flags |= ATA_NO_SLAVE;
3331 ata_nvidia_status(device_t dev)
3333 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3334 struct ata_channel *ch = device_get_softc(dev);
3335 int offset = ctlr->chip->cfg2 & NV4 ? 0x0440 : 0x0010;
3336 int shift = ch->unit << (ctlr->chip->cfg2 & NVQ ? 4 : 2);
3339 /* get interrupt status */
3340 if (ctlr->chip->cfg2 & NVQ)
3341 istatus = ATA_INL(ctlr->r_res2, offset);
3343 istatus = ATA_INB(ctlr->r_res2, offset);
3345 /* do we have any PHY events ? */
3346 if (istatus & (0x0c << shift))
3347 ata_sata_phy_check_events(dev);
3349 /* clear interrupt(s) */
3350 if (ctlr->chip->cfg2 & NVQ)
3351 ATA_OUTL(ctlr->r_res2, offset, (0x0f << shift) | 0x00f000f0);
3353 ATA_OUTB(ctlr->r_res2, offset, (0x0f << shift));
3355 /* do we have any device action ? */
3356 return (istatus & (0x01 << shift));
3360 ata_nvidia_reset(device_t dev)
3362 if (ata_sata_phy_reset(dev))
3363 ata_generic_reset(dev);
3368 * Promise chipset support functions
3370 #define ATA_PDC_APKT_OFFSET 0x00000010
3371 #define ATA_PDC_HPKT_OFFSET 0x00000040
3372 #define ATA_PDC_ASG_OFFSET 0x00000080
3373 #define ATA_PDC_LSG_OFFSET 0x000000c0
3374 #define ATA_PDC_HSG_OFFSET 0x00000100
3375 #define ATA_PDC_CHN_OFFSET 0x00000400
3376 #define ATA_PDC_BUF_BASE 0x00400000
3377 #define ATA_PDC_BUF_OFFSET 0x00100000
3378 #define ATA_PDC_MAX_HPKT 8
3379 #define ATA_PDC_WRITE_REG 0x00
3380 #define ATA_PDC_WRITE_CTL 0x0e
3381 #define ATA_PDC_WRITE_END 0x08
3382 #define ATA_PDC_WAIT_NBUSY 0x10
3383 #define ATA_PDC_WAIT_READY 0x18
3384 #define ATA_PDC_1B 0x20
3385 #define ATA_PDC_2B 0x40
3387 struct host_packet {
3389 TAILQ_ENTRY(host_packet) chain;
3392 struct ata_promise_sx4 {
3393 struct spinlock mtx;
3394 TAILQ_HEAD(, host_packet) queue;
3399 ata_promise_ident(device_t dev)
3401 struct ata_pci_controller *ctlr = device_get_softc(dev);
3402 struct ata_chip_id *idx;
3403 static struct ata_chip_id ids[] =
3404 {{ ATA_PDC20246, 0, PROLD, 0x00, ATA_UDMA2, "PDC20246" },
3405 { ATA_PDC20262, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20262" },
3406 { ATA_PDC20263, 0, PRNEW, 0x00, ATA_UDMA4, "PDC20263" },
3407 { ATA_PDC20265, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20265" },
3408 { ATA_PDC20267, 0, PRNEW, 0x00, ATA_UDMA5, "PDC20267" },
3409 { ATA_PDC20268, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20268" },
3410 { ATA_PDC20269, 0, PRTX, 0x00, ATA_UDMA6, "PDC20269" },
3411 { ATA_PDC20270, 0, PRTX, PRTX4, ATA_UDMA5, "PDC20270" },
3412 { ATA_PDC20271, 0, PRTX, 0x00, ATA_UDMA6, "PDC20271" },
3413 { ATA_PDC20275, 0, PRTX, 0x00, ATA_UDMA6, "PDC20275" },
3414 { ATA_PDC20276, 0, PRTX, PRSX6K, ATA_UDMA6, "PDC20276" },
3415 { ATA_PDC20277, 0, PRTX, 0x00, ATA_UDMA6, "PDC20277" },
3416 { ATA_PDC20318, 0, PRMIO, PRSATA, ATA_SA150, "PDC20318" },
3417 { ATA_PDC20319, 0, PRMIO, PRSATA, ATA_SA150, "PDC20319" },
3418 { ATA_PDC20371, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20371" },
3419 { ATA_PDC20375, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20375" },
3420 { ATA_PDC20376, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20376" },
3421 { ATA_PDC20377, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20377" },
3422 { ATA_PDC20378, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20378" },
3423 { ATA_PDC20379, 0, PRMIO, PRCMBO, ATA_SA150, "PDC20379" },
3424 { ATA_PDC20571, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20571" },
3425 { ATA_PDC20575, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20575" },
3426 { ATA_PDC20579, 0, PRMIO, PRCMBO2, ATA_SA150, "PDC20579" },
3427 { ATA_PDC20771, 0, PRMIO, PRCMBO2, ATA_SA300, "PDC20771" },
3428 { ATA_PDC40775, 0, PRMIO, PRCMBO2, ATA_SA300, "PDC40775" },
3429 { ATA_PDC20617, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20617" },
3430 { ATA_PDC20618, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20618" },
3431 { ATA_PDC20619, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20619" },
3432 { ATA_PDC20620, 0, PRMIO, PRPATA, ATA_UDMA6, "PDC20620" },
3433 { ATA_PDC20621, 0, PRMIO, PRSX4X, ATA_UDMA5, "PDC20621" },
3434 { ATA_PDC20622, 0, PRMIO, PRSX4X, ATA_SA150, "PDC20622" },
3435 { ATA_PDC40518, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40518" },
3436 { ATA_PDC40519, 0, PRMIO, PRSATA2, ATA_SA150, "PDC40519" },
3437 { ATA_PDC40718, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40718" },
3438 { ATA_PDC40719, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40719" },
3439 { ATA_PDC40779, 0, PRMIO, PRSATA2, ATA_SA300, "PDC40779" },
3440 { 0, 0, 0, 0, 0, 0}};
3442 uintptr_t devid = 0;
3444 if (!(idx = ata_match_chip(dev, ids)))
3447 /* if we are on a SuperTrak SX6000 dont attach */
3448 if ((idx->cfg2 & PRSX6K) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
3449 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
3450 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
3451 devid == ATA_I960RM)
3454 strcpy(buffer, "Promise ");
3455 strcat(buffer, idx->text);
3457 /* if we are on a FastTrak TX4, adjust the interrupt resource */
3458 if ((idx->cfg2 & PRTX4) && pci_get_class(GRANDPARENT(dev))==PCIC_BRIDGE &&
3459 !BUS_READ_IVAR(device_get_parent(GRANDPARENT(dev)),
3460 GRANDPARENT(dev), PCI_IVAR_DEVID, &devid) &&
3461 ((devid == ATA_DEC_21150) || (devid == ATA_DEC_21150_1))) {
3462 static long start = 0, end = 0;
3464 if (pci_get_slot(dev) == 1) {
3465 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
3466 strcat(buffer, " (channel 0+1)");
3468 else if (pci_get_slot(dev) == 2 && start && end) {
3469 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end,
3470 machintr_legacy_intr_cpuid(start));
3471 strcat(buffer, " (channel 2+3)");
3477 ksprintf(buffer, "%s %s controller", buffer, ata_mode2str(idx->max_dma));
3478 device_set_desc_copy(dev, buffer);
3480 ctlr->chipinit = ata_promise_chipinit;
3485 ata_promise_chipinit(device_t dev)
3487 struct ata_pci_controller *ctlr = device_get_softc(dev);
3488 int fake_reg, stat_reg;
3490 if (ata_setup_interrupt(dev))
3493 switch (ctlr->chip->cfg1) {
3496 ATA_OUTB(ctlr->r_res1, 0x11, ATA_INB(ctlr->r_res1, 0x11) | 0x0a);
3498 ctlr->dmainit = ata_promise_dmainit;
3502 /* enable burst mode */
3503 ATA_OUTB(ctlr->r_res1, 0x1f, ATA_INB(ctlr->r_res1, 0x1f) | 0x01);
3504 ctlr->allocate = ata_promise_allocate;
3505 ctlr->setmode = ata_promise_setmode;
3509 ctlr->allocate = ata_promise_tx2_allocate;
3510 ctlr->setmode = ata_promise_setmode;
3514 ctlr->r_type1 = SYS_RES_MEMORY;
3515 ctlr->r_rid1 = PCIR_BAR(4);
3516 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
3517 &ctlr->r_rid1, RF_ACTIVE)))
3520 ctlr->r_type2 = SYS_RES_MEMORY;
3521 ctlr->r_rid2 = PCIR_BAR(3);
3522 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
3523 &ctlr->r_rid2, RF_ACTIVE)))
3526 if (ctlr->chip->cfg2 == PRSX4X) {
3527 struct ata_promise_sx4 *hpkt;
3528 u_int32_t dimm = ATA_INL(ctlr->r_res2, 0x000c0080);
3530 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
3531 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3532 ata_promise_sx4_intr, ctlr, &ctlr->handle, NULL)) {
3533 device_printf(dev, "unable to setup interrupt\n");
3537 /* print info about cache memory */
3538 device_printf(dev, "DIMM size %dMB @ 0x%08x%s\n",
3539 (((dimm >> 16) & 0xff)-((dimm >> 24) & 0xff)+1) << 4,
3540 ((dimm >> 24) & 0xff),
3541 ATA_INL(ctlr->r_res2, 0x000c0088) & (1<<16) ?
3542 " ECC enabled" : "" );
3544 /* adjust cache memory parameters */
3545 ATA_OUTL(ctlr->r_res2, 0x000c000c,
3546 (ATA_INL(ctlr->r_res2, 0x000c000c) & 0xffff0000));
3548 /* setup host packet controls */
3549 hpkt = kmalloc(sizeof(struct ata_promise_sx4),
3550 M_TEMP, M_INTWAIT | M_ZERO);
3551 spin_init(&hpkt->mtx);
3552 TAILQ_INIT(&hpkt->queue);
3554 device_set_ivars(dev, hpkt);
3555 ctlr->allocate = ata_promise_mio_allocate;
3556 ctlr->reset = ata_promise_mio_reset;
3557 ctlr->dmainit = ata_promise_mio_dmainit;
3558 ctlr->setmode = ata_promise_setmode;
3563 /* mio type controllers need an interrupt intercept */
3564 if (bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle) ||
3565 bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
3566 ata_promise_mio_intr, ctlr, &ctlr->handle, NULL)) {
3567 device_printf(dev, "unable to setup interrupt\n");
3571 switch (ctlr->chip->cfg2) {
3573 ctlr->channels = ((ATA_INL(ctlr->r_res2, 0x48) & 0x01) > 0) +
3574 ((ATA_INL(ctlr->r_res2, 0x48) & 0x02) > 0) + 2;
3598 /* prime fake interrupt register */
3599 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3601 /* clear SATA status */
3602 ATA_OUTL(ctlr->r_res2, stat_reg, 0x000000ff);
3604 ctlr->allocate = ata_promise_mio_allocate;
3605 ctlr->reset = ata_promise_mio_reset;
3606 ctlr->dmainit = ata_promise_mio_dmainit;
3607 ctlr->setmode = ata_promise_mio_setmode;
3614 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
3616 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
3621 ata_promise_allocate(device_t dev)
3623 struct ata_channel *ch = device_get_softc(dev);
3625 if (ata_pci_allocate(dev))
3628 ch->hw.status = ata_promise_status;
3633 ata_promise_status(device_t dev)
3635 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3636 struct ata_channel *ch = device_get_softc(dev);
3638 if (ATA_INL(ctlr->r_res1, 0x1c) & (ch->unit ? 0x00004000 : 0x00000400)) {
3639 return ata_pci_status(dev);
3645 ata_promise_dmastart(device_t dev)
3647 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3648 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3649 struct ata_device *atadev = device_get_softc(dev);
3651 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3652 ATA_OUTB(ctlr->r_res1, 0x11,
3653 ATA_INB(ctlr->r_res1, 0x11) | (ch->unit ? 0x08 : 0x02));
3654 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20,
3655 ((ch->dma->flags & ATA_DMA_READ) ? 0x05000000 : 0x06000000) |
3656 (ch->dma->cur_iosize >> 1));
3658 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
3659 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
3660 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, ch->dma->sg_bus);
3661 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3662 ((ch->dma->flags & ATA_DMA_READ) ? ATA_BMCMD_WRITE_READ : 0) |
3663 ATA_BMCMD_START_STOP);
3664 ch->flags |= ATA_DMA_ACTIVE;
3669 ata_promise_dmastop(device_t dev)
3671 struct ata_pci_controller *ctlr = device_get_softc(GRANDPARENT(dev));
3672 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3673 struct ata_device *atadev = device_get_softc(dev);
3676 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
3677 ATA_OUTB(ctlr->r_res1, 0x11,
3678 ATA_INB(ctlr->r_res1, 0x11) & ~(ch->unit ? 0x08 : 0x02));
3679 ATA_OUTL(ctlr->r_res1, ch->unit ? 0x24 : 0x20, 0);
3681 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT);
3682 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3683 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3684 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3685 ch->flags &= ~ATA_DMA_ACTIVE;
3690 ata_promise_dmareset(device_t dev)
3692 struct ata_channel *ch = device_get_softc(dev);
3694 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
3695 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
3696 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
3697 ch->flags &= ~ATA_DMA_ACTIVE;
3701 ata_promise_dmainit(device_t dev)
3703 struct ata_channel *ch = device_get_softc(dev);
3707 ch->dma->start = ata_promise_dmastart;
3708 ch->dma->stop = ata_promise_dmastop;
3709 ch->dma->reset = ata_promise_dmareset;
3714 ata_promise_setmode(device_t dev, int mode)
3716 device_t gparent = GRANDPARENT(dev);
3717 struct ata_pci_controller *ctlr = device_get_softc(gparent);
3718 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
3719 struct ata_device *atadev = device_get_softc(dev);
3720 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
3722 u_int32_t timings[][2] = {
3723 /* PROLD PRNEW mode */
3724 { 0x004ff329, 0x004fff2f }, /* PIO 0 */
3725 { 0x004fec25, 0x004ff82a }, /* PIO 1 */
3726 { 0x004fe823, 0x004ff026 }, /* PIO 2 */
3727 { 0x004fe622, 0x004fec24 }, /* PIO 3 */
3728 { 0x004fe421, 0x004fe822 }, /* PIO 4 */
3729 { 0x004567f3, 0x004acef6 }, /* MWDMA 0 */
3730 { 0x004467f3, 0x0048cef6 }, /* MWDMA 1 */
3731 { 0x004367f3, 0x0046cef6 }, /* MWDMA 2 */
3732 { 0x004367f3, 0x0046cef6 }, /* UDMA 0 */
3733 { 0x004247f3, 0x00448ef6 }, /* UDMA 1 */
3734 { 0x004127f3, 0x00436ef6 }, /* UDMA 2 */
3735 { 0, 0x00424ef6 }, /* UDMA 3 */
3736 { 0, 0x004127f3 }, /* UDMA 4 */
3737 { 0, 0x004127f3 } /* UDMA 5 */
3740 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
3742 switch (ctlr->chip->cfg1) {
3745 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x50, 2) &
3746 (ch->unit ? 1 << 11 : 1 << 10))) {
3747 ata_print_cable(dev, "controller");
3750 if (ata_atapi(dev) && mode > ATA_PIO_MAX)
3751 mode = ata_limit_mode(dev, mode, ATA_PIO_MAX);
3755 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3756 if (mode > ATA_UDMA2 &&
3757 ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x04) {
3758 ata_print_cable(dev, "controller");
3764 if (mode > ATA_UDMA2 &&
3765 (ATA_INL(ctlr->r_res2,
3766 (ctlr->chip->cfg2 & PRSX4X ? 0x000c0260 : 0x0260) +
3767 (ch->unit << 7)) & 0x01000000)) {
3768 ata_print_cable(dev, "controller");
3774 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
3777 device_printf(dev, "%ssetting %s on %s chip\n",
3778 (error) ? "FAILURE " : "",
3779 ata_mode2str(mode), ctlr->chip->text);
3781 if (ctlr->chip->cfg1 < PRTX)
3782 pci_write_config(gparent, 0x60 + (devno << 2),
3783 timings[ata_mode2idx(mode)][ctlr->chip->cfg1], 4);
3784 atadev->mode = mode;
3790 ata_promise_tx2_allocate(device_t dev)
3792 struct ata_channel *ch = device_get_softc(dev);
3794 if (ata_pci_allocate(dev))
3797 ch->hw.status = ata_promise_tx2_status;
3802 ata_promise_tx2_status(device_t dev)
3804 struct ata_channel *ch = device_get_softc(dev);
3806 ATA_IDX_OUTB(ch, ATA_BMDEVSPEC_0, 0x0b);
3807 if (ATA_IDX_INB(ch, ATA_BMDEVSPEC_1) & 0x20) {
3808 return ata_pci_status(dev);
3814 ata_promise_mio_allocate(device_t dev)
3816 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3817 struct ata_channel *ch = device_get_softc(dev);
3818 int offset = (ctlr->chip->cfg2 & PRSX4X) ? 0x000c0000 : 0;
3821 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
3822 ch->r_io[i].res = ctlr->r_res2;
3823 ch->r_io[i].offset = offset + 0x0200 + (i << 2) + (ch->unit << 7);
3825 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
3826 ch->r_io[ATA_CONTROL].offset = offset + 0x0238 + (ch->unit << 7);
3827 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
3828 ata_default_registers(dev);
3829 if ((ctlr->chip->cfg2 & (PRSATA | PRSATA2)) ||
3830 ((ctlr->chip->cfg2 & (PRCMBO | PRCMBO2)) && ch->unit < 2)) {
3831 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
3832 ch->r_io[ATA_SSTATUS].offset = 0x400 + (ch->unit << 8);
3833 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
3834 ch->r_io[ATA_SERROR].offset = 0x404 + (ch->unit << 8);
3835 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
3836 ch->r_io[ATA_SCONTROL].offset = 0x408 + (ch->unit << 8);
3837 ch->flags |= ATA_NO_SLAVE;
3839 ch->flags |= ATA_USE_16BIT;
3841 ata_generic_hw(dev);
3842 if (ctlr->chip->cfg2 & PRSX4X) {
3843 ch->hw.command = ata_promise_sx4_command;
3846 ch->hw.command = ata_promise_mio_command;
3847 ch->hw.status = ata_promise_mio_status;
3853 ata_promise_mio_intr(void *data)
3855 struct ata_pci_controller *ctlr = data;
3856 struct ata_channel *ch;
3860 switch (ctlr->chip->cfg2) {
3874 * since reading interrupt status register on early "mio" chips
3875 * clears the status bits we cannot read it for each channel later on
3876 * in the generic interrupt routine.
3877 * store the bits in an unused register in the chip so we can read
3878 * it from there safely to get around this "feature".
3880 vector = ATA_INL(ctlr->r_res2, 0x040);
3881 ATA_OUTL(ctlr->r_res2, 0x040, vector);
3882 ATA_OUTL(ctlr->r_res2, fake_reg, vector);
3884 for (unit = 0; unit < ctlr->channels; unit++) {
3885 if ((ch = ctlr->interrupt[unit].argument))
3886 ctlr->interrupt[unit].function(ch);
3889 ATA_OUTL(ctlr->r_res2, fake_reg, 0xffffffff);
3893 ata_promise_mio_status(device_t dev)
3895 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3896 struct ata_channel *ch = device_get_softc(dev);
3897 struct ata_connect_task *tp;
3898 u_int32_t fake_reg, stat_reg, vector, status;
3900 switch (ctlr->chip->cfg2) {
3915 /* read and acknowledge interrupt */
3916 vector = ATA_INL(ctlr->r_res2, fake_reg);
3918 /* read and clear interface status */
3919 status = ATA_INL(ctlr->r_res2, stat_reg);
3920 ATA_OUTL(ctlr->r_res2, stat_reg, status & (0x00000011 << ch->unit));
3922 /* check for and handle disconnect events */
3923 if ((status & (0x00000001 << ch->unit)) &&
3924 (tp = (struct ata_connect_task *)
3925 kmalloc(sizeof(struct ata_connect_task),
3926 M_ATA, M_INTWAIT | M_ZERO))) {
3929 device_printf(ch->dev, "DISCONNECT requested\n");
3930 tp->action = ATA_C_DETACH;
3932 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3933 taskqueue_enqueue(taskqueue_thread[mycpuid], &tp->task);
3936 /* check for and handle connect events */
3937 if ((status & (0x00000010 << ch->unit)) &&
3938 (tp = (struct ata_connect_task *)
3939 kmalloc(sizeof(struct ata_connect_task),
3940 M_ATA, M_INTWAIT | M_ZERO))) {
3943 device_printf(ch->dev, "CONNECT requested\n");
3944 tp->action = ATA_C_ATTACH;
3946 TASK_INIT(&tp->task, 0, ata_sata_phy_event, tp);
3947 taskqueue_enqueue(taskqueue_thread[mycpuid], &tp->task);
3950 /* do we have any device action ? */
3951 return (vector & (1 << (ch->unit + 1)));
3955 ata_promise_mio_command(struct ata_request *request)
3957 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
3958 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
3959 u_int32_t *wordp = (u_int32_t *)ch->dma->work;
3961 ATA_OUTL(ctlr->r_res2, (ch->unit + 1) << 2, 0x00000001);
3963 /* XXX SOS add ATAPI commands support later */
3964 switch (request->u.ata.command) {
3966 return ata_generic_command(request);
3969 case ATA_READ_DMA48:
3970 wordp[0] = htole32(0x04 | ((ch->unit + 1) << 16) | (0x00 << 24));
3974 case ATA_WRITE_DMA48:
3975 wordp[0] = htole32(0x00 | ((ch->unit + 1) << 16) | (0x00 << 24));
3978 wordp[1] = htole32(ch->dma->sg_bus);
3980 ata_promise_apkt((u_int8_t*)wordp, request);
3982 ATA_OUTL(ctlr->r_res2, 0x0240 + (ch->unit << 7), ch->dma->work_bus);
3987 ata_promise_mio_reset(device_t dev)
3989 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
3990 struct ata_channel *ch = device_get_softc(dev);
3991 struct ata_promise_sx4 *hpktp;
3993 switch (ctlr->chip->cfg2) {
3996 /* softreset channel ATA module */
3997 hpktp = device_get_ivars(ctlr->dev);
3998 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7), ch->unit + 1);
4000 ATA_OUTL(ctlr->r_res2, 0xc0260 + (ch->unit << 7),
4001 (ATA_INL(ctlr->r_res2, 0xc0260 + (ch->unit << 7)) &
4002 ~0x00003f9f) | (ch->unit + 1));
4004 /* softreset HOST module */ /* XXX SOS what about other outstandings */
4005 spin_lock(&hpktp->mtx);
4006 ATA_OUTL(ctlr->r_res2, 0xc012c,
4007 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f) | (1 << 11));
4009 ATA_OUTL(ctlr->r_res2, 0xc012c,
4010 (ATA_INL(ctlr->r_res2, 0xc012c) & ~0x00000f9f));
4012 spin_unlock(&hpktp->mtx);
4013 ata_generic_reset(dev);
4019 if ((ctlr->chip->cfg2 == PRSATA) ||
4020 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
4022 /* mask plug/unplug intr */
4023 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00110000 << ch->unit));
4026 /* softreset channels ATA module */
4027 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
4029 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
4030 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
4031 ~0x00003f9f) | (ch->unit + 1));
4033 if ((ctlr->chip->cfg2 == PRSATA) ||
4034 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2))) {
4036 if (ata_sata_phy_reset(dev))
4037 ata_generic_reset(dev);
4039 /* reset and enable plug/unplug intr */
4040 ATA_OUTL(ctlr->r_res2, 0x06c, (0x00000011 << ch->unit));
4043 ata_generic_reset(dev);
4048 if ((ctlr->chip->cfg2 == PRSATA2) ||
4049 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
4050 /* set portmultiplier port */
4051 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x0f);
4053 /* mask plug/unplug intr */
4054 ATA_OUTL(ctlr->r_res2, 0x060, (0x00110000 << ch->unit));
4057 /* softreset channels ATA module */
4058 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7), (1 << 11));
4060 ATA_OUTL(ctlr->r_res2, 0x0260 + (ch->unit << 7),
4061 (ATA_INL(ctlr->r_res2, 0x0260 + (ch->unit << 7)) &
4062 ~0x00003f9f) | (ch->unit + 1));
4064 if ((ctlr->chip->cfg2 == PRSATA2) ||
4065 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2))) {
4067 /* set PHY mode to "improved" */
4068 ATA_OUTL(ctlr->r_res2, 0x414 + (ch->unit << 8),
4069 (ATA_INL(ctlr->r_res2, 0x414 + (ch->unit << 8)) &
4070 ~0x00000003) | 0x00000001);
4072 if (ata_sata_phy_reset(dev))
4073 ata_generic_reset(dev);
4075 /* reset and enable plug/unplug intr */
4076 ATA_OUTL(ctlr->r_res2, 0x060, (0x00000011 << ch->unit));
4078 /* set portmultiplier port */
4079 ATA_OUTL(ctlr->r_res2, 0x4e8 + (ch->unit << 8), 0x00);
4082 ata_generic_reset(dev);
4089 ata_promise_mio_dmainit(device_t dev)
4091 /* note start and stop are not used here */
4096 ata_promise_mio_setmode(device_t dev, int mode)
4098 device_t gparent = GRANDPARENT(dev);
4099 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4100 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4102 if ( (ctlr->chip->cfg2 == PRSATA) ||
4103 ((ctlr->chip->cfg2 == PRCMBO) && (ch->unit < 2)) ||
4104 (ctlr->chip->cfg2 == PRSATA2) ||
4105 ((ctlr->chip->cfg2 == PRCMBO2) && (ch->unit < 2)))
4106 ata_sata_setmode(dev, mode);
4108 ata_promise_setmode(dev, mode);
4112 ata_promise_sx4_intr(void *data)
4114 struct ata_pci_controller *ctlr = data;
4115 struct ata_channel *ch;
4116 u_int32_t vector = ATA_INL(ctlr->r_res2, 0x000c0480);
4119 for (unit = 0; unit < ctlr->channels; unit++) {
4120 if (vector & (1 << (unit + 1)))
4121 if ((ch = ctlr->interrupt[unit].argument))
4122 ctlr->interrupt[unit].function(ch);
4123 if (vector & (1 << (unit + 5)))
4124 if ((ch = ctlr->interrupt[unit].argument))
4125 ata_promise_queue_hpkt(ctlr,
4126 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
4127 ATA_PDC_HPKT_OFFSET));
4128 if (vector & (1 << (unit + 9))) {
4129 ata_promise_next_hpkt(ctlr);
4130 if ((ch = ctlr->interrupt[unit].argument))
4131 ctlr->interrupt[unit].function(ch);
4133 if (vector & (1 << (unit + 13))) {
4134 ata_promise_next_hpkt(ctlr);
4135 if ((ch = ctlr->interrupt[unit].argument))
4136 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
4137 htole32((ch->unit * ATA_PDC_CHN_OFFSET) +
4138 ATA_PDC_APKT_OFFSET));
4144 ata_promise_sx4_command(struct ata_request *request)
4146 device_t gparent = GRANDPARENT(request->dev);
4147 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4148 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4149 struct ata_dma_prdentry *prd = ch->dma->sg;
4150 caddr_t window = rman_get_virtual(ctlr->r_res1);
4152 int i, idx, length = 0;
4154 /* XXX SOS add ATAPI commands support later */
4155 switch (request->u.ata.command) {
4160 case ATA_ATA_IDENTIFY:
4164 case ATA_READ_MUL48:
4168 case ATA_WRITE_MUL48:
4169 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
4170 return ata_generic_command(request);
4172 case ATA_SETFEATURES:
4173 case ATA_FLUSHCACHE:
4174 case ATA_FLUSHCACHE48:
4177 wordp = (u_int32_t *)
4178 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
4179 wordp[0] = htole32(0x08 | ((ch->unit + 1)<<16) | (0x00 << 24));
4182 ata_promise_apkt((u_int8_t *)wordp, request);
4183 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
4184 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit + 1) << 2), 0x00000001);
4185 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
4186 htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_APKT_OFFSET));
4190 case ATA_READ_DMA48:
4192 case ATA_WRITE_DMA48:
4193 wordp = (u_int32_t *)
4194 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HSG_OFFSET);
4197 wordp[idx++] = prd[i].addr;
4198 wordp[idx++] = prd[i].count;
4199 length += (prd[i].count & ~ATA_DMA_EOT);
4200 } while (!(prd[i++].count & ATA_DMA_EOT));
4202 wordp = (u_int32_t *)
4203 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_LSG_OFFSET);
4204 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
4205 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
4207 wordp = (u_int32_t *)
4208 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_ASG_OFFSET);
4209 wordp[0] = htole32((ch->unit * ATA_PDC_BUF_OFFSET) + ATA_PDC_BUF_BASE);
4210 wordp[1] = htole32(request->bytecount | ATA_DMA_EOT);
4212 wordp = (u_int32_t *)
4213 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET);
4214 if (request->flags & ATA_R_READ)
4215 wordp[0] = htole32(0x14 | ((ch->unit+9)<<16) | ((ch->unit+5)<<24));
4216 if (request->flags & ATA_R_WRITE)
4217 wordp[0] = htole32(0x00 | ((ch->unit+13)<<16) | (0x00<<24));
4218 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_HSG_OFFSET);
4219 wordp[2] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_LSG_OFFSET);
4222 wordp = (u_int32_t *)
4223 (window + (ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET);
4224 if (request->flags & ATA_R_READ)
4225 wordp[0] = htole32(0x04 | ((ch->unit+5)<<16) | (0x00<<24));
4226 if (request->flags & ATA_R_WRITE)
4227 wordp[0] = htole32(0x10 | ((ch->unit+1)<<16) | ((ch->unit+13)<<24));
4228 wordp[1] = htole32((ch->unit * ATA_PDC_CHN_OFFSET)+ATA_PDC_ASG_OFFSET);
4230 ata_promise_apkt((u_int8_t *)wordp, request);
4231 ATA_OUTL(ctlr->r_res2, 0x000c0484, 0x00000001);
4233 if (request->flags & ATA_R_READ) {
4234 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+5)<<2), 0x00000001);
4235 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+9)<<2), 0x00000001);
4236 ATA_OUTL(ctlr->r_res2, 0x000c0240 + (ch->unit << 7),
4237 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_APKT_OFFSET));
4239 if (request->flags & ATA_R_WRITE) {
4240 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+1)<<2), 0x00000001);
4241 ATA_OUTL(ctlr->r_res2, 0x000c0400 + ((ch->unit+13)<<2), 0x00000001);
4242 ata_promise_queue_hpkt(ctlr,
4243 htole32((ch->unit * ATA_PDC_CHN_OFFSET) + ATA_PDC_HPKT_OFFSET));
4250 ata_promise_apkt(u_int8_t *bytep, struct ata_request *request)
4252 struct ata_device *atadev = device_get_softc(request->dev);
4255 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_PDC_WAIT_NBUSY|ATA_DRIVE;
4256 bytep[i++] = ATA_D_IBM | ATA_D_LBA | atadev->unit;
4257 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_CTL;
4258 bytep[i++] = ATA_A_4BIT;
4260 if (atadev->flags & ATA_D_48BIT_ACTIVE) {
4261 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_FEATURE;
4262 bytep[i++] = request->u.ata.feature >> 8;
4263 bytep[i++] = request->u.ata.feature;
4264 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_COUNT;
4265 bytep[i++] = request->u.ata.count >> 8;
4266 bytep[i++] = request->u.ata.count;
4267 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_SECTOR;
4268 bytep[i++] = request->u.ata.lba >> 24;
4269 bytep[i++] = request->u.ata.lba;
4270 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
4271 bytep[i++] = request->u.ata.lba >> 32;
4272 bytep[i++] = request->u.ata.lba >> 8;
4273 bytep[i++] = ATA_PDC_2B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
4274 bytep[i++] = request->u.ata.lba >> 40;
4275 bytep[i++] = request->u.ata.lba >> 16;
4276 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
4277 bytep[i++] = ATA_D_LBA | atadev->unit;
4280 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_FEATURE;
4281 bytep[i++] = request->u.ata.feature;
4282 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_COUNT;
4283 bytep[i++] = request->u.ata.count;
4284 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_SECTOR;
4285 bytep[i++] = request->u.ata.lba;
4286 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_LSB;
4287 bytep[i++] = request->u.ata.lba >> 8;
4288 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_CYL_MSB;
4289 bytep[i++] = request->u.ata.lba >> 16;
4290 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_REG | ATA_DRIVE;
4291 bytep[i++] = (atadev->flags & ATA_D_USE_CHS ? 0 : ATA_D_LBA) |
4292 ATA_D_IBM | atadev->unit | ((request->u.ata.lba >> 24)&0xf);
4294 bytep[i++] = ATA_PDC_1B | ATA_PDC_WRITE_END | ATA_COMMAND;
4295 bytep[i++] = request->u.ata.command;
4300 ata_promise_queue_hpkt(struct ata_pci_controller *ctlr, u_int32_t hpkt)
4302 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
4304 spin_lock(&hpktp->mtx);
4306 struct host_packet *hp =
4307 kmalloc(sizeof(struct host_packet), M_TEMP, M_INTWAIT | M_ZERO);
4309 TAILQ_INSERT_TAIL(&hpktp->queue, hp, chain);
4313 ATA_OUTL(ctlr->r_res2, 0x000c0100, hpkt);
4315 spin_unlock(&hpktp->mtx);
4319 ata_promise_next_hpkt(struct ata_pci_controller *ctlr)
4321 struct ata_promise_sx4 *hpktp = device_get_ivars(ctlr->dev);
4322 struct host_packet *hp;
4324 spin_lock(&hpktp->mtx);
4325 if ((hp = TAILQ_FIRST(&hpktp->queue))) {
4326 TAILQ_REMOVE(&hpktp->queue, hp, chain);
4327 ATA_OUTL(ctlr->r_res2, 0x000c0100, hp->addr);
4332 spin_unlock(&hpktp->mtx);
4337 * ServerWorks chipset support functions
4340 ata_serverworks_ident(device_t dev)
4342 struct ata_pci_controller *ctlr = device_get_softc(dev);
4343 struct ata_chip_id *idx;
4344 static struct ata_chip_id ids[] =
4345 {{ ATA_ROSB4, 0x00, SWKS33, 0, ATA_UDMA2, "ROSB4" },
4346 { ATA_CSB5, 0x92, SWKS100, 0, ATA_UDMA5, "CSB5" },
4347 { ATA_CSB5, 0x00, SWKS66, 0, ATA_UDMA4, "CSB5" },
4348 { ATA_CSB6, 0x00, SWKS100, 0, ATA_UDMA5, "CSB6" },
4349 { ATA_CSB6_1, 0x00, SWKS66, 0, ATA_UDMA4, "CSB6" },
4350 { ATA_HT1000, 0x00, SWKS100, 0, ATA_UDMA5, "HT1000" },
4351 { ATA_HT1000_S1, 0x00, SWKS100, 4, ATA_SA150, "HT1000" },
4352 { ATA_HT1000_S2, 0x00, SWKSMIO, 4, ATA_SA150, "HT1000" },
4353 { ATA_K2, 0x00, SWKSMIO, 4, ATA_SA150, "K2" },
4354 { ATA_FRODO4, 0x00, SWKSMIO, 4, ATA_SA150, "Frodo4" },
4355 { ATA_FRODO8, 0x00, SWKSMIO, 8, ATA_SA150, "Frodo8" },
4356 { 0, 0, 0, 0, 0, 0}};
4359 if (!(idx = ata_match_chip(dev, ids)))
4362 ksprintf(buffer, "ServerWorks %s %s controller",
4363 idx->text, ata_mode2str(idx->max_dma));
4364 device_set_desc_copy(dev, buffer);
4366 ctlr->chipinit = ata_serverworks_chipinit;
4371 ata_serverworks_chipinit(device_t dev)
4373 struct ata_pci_controller *ctlr = device_get_softc(dev);
4375 if (ata_setup_interrupt(dev))
4378 if (ctlr->chip->cfg1 == SWKSMIO) {
4379 ctlr->r_type2 = SYS_RES_MEMORY;
4380 ctlr->r_rid2 = PCIR_BAR(5);
4381 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4382 &ctlr->r_rid2, RF_ACTIVE))){
4383 ata_teardown_interrupt(dev);
4387 ctlr->channels = ctlr->chip->cfg2;
4388 ctlr->allocate = ata_serverworks_allocate;
4389 ctlr->setmode = ata_sata_setmode;
4392 else if (ctlr->chip->cfg1 == SWKS33) {
4396 /* locate the ISA part in the southbridge and enable UDMA33 */
4397 if (!device_get_children(device_get_parent(dev), &children,&nchildren)){
4398 for (i = 0; i < nchildren; i++) {
4399 if (pci_get_devid(children[i]) == ATA_ROSB4_ISA) {
4400 pci_write_config(children[i], 0x64,
4401 (pci_read_config(children[i], 0x64, 4) &
4402 ~0x00002000) | 0x00004000, 4);
4406 kfree(children, M_TEMP);
4410 pci_write_config(dev, 0x5a,
4411 (pci_read_config(dev, 0x5a, 1) & ~0x40) |
4412 (ctlr->chip->cfg1 == SWKS100) ? 0x03 : 0x02, 1);
4414 ctlr->setmode = ata_serverworks_setmode;
4419 ata_serverworks_allocate(device_t dev)
4421 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4422 struct ata_channel *ch = device_get_softc(dev);
4426 ch_offset = ch->unit * 0x100;
4428 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
4429 ch->r_io[i].res = ctlr->r_res2;
4431 /* setup ATA registers */
4432 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
4433 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x04;
4434 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
4435 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
4436 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
4437 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
4438 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
4439 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1c;
4440 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x20;
4441 ata_default_registers(dev);
4443 /* setup DMA registers */
4444 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x30;
4445 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x32;
4446 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x34;
4448 /* setup SATA registers */
4449 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x40;
4450 ch->r_io[ATA_SERROR].offset = ch_offset + 0x44;
4451 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x48;
4453 ch->flags |= ATA_NO_SLAVE;
4456 /* chip does not reliably do 64K DMA transfers */
4458 ch->dma->max_iosize = 126 * DEV_BSIZE;
4464 ata_serverworks_setmode(device_t dev, int mode)
4466 device_t gparent = GRANDPARENT(dev);
4467 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4468 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4469 struct ata_device *atadev = device_get_softc(dev);
4470 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4471 int offset = (devno ^ 0x01) << 3;
4473 u_int8_t piotimings[] = { 0x5d, 0x47, 0x34, 0x22, 0x20, 0x34, 0x22, 0x20,
4474 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
4475 u_int8_t dmatimings[] = { 0x77, 0x21, 0x20 };
4477 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4479 mode = ata_check_80pin(dev, mode);
4481 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4484 device_printf(dev, "%ssetting %s on %s chip\n",
4485 (error) ? "FAILURE " : "",
4486 ata_mode2str(mode), ctlr->chip->text);
4488 if (mode >= ATA_UDMA0) {
4489 pci_write_config(gparent, 0x56,
4490 (pci_read_config(gparent, 0x56, 2) &
4491 ~(0xf << (devno << 2))) |
4492 ((mode & ATA_MODE_MASK) << (devno << 2)), 2);
4493 pci_write_config(gparent, 0x54,
4494 pci_read_config(gparent, 0x54, 1) |
4495 (0x01 << devno), 1);
4496 pci_write_config(gparent, 0x44,
4497 (pci_read_config(gparent, 0x44, 4) &
4498 ~(0xff << offset)) |
4499 (dmatimings[2] << offset), 4);
4501 else if (mode >= ATA_WDMA0) {
4502 pci_write_config(gparent, 0x54,
4503 pci_read_config(gparent, 0x54, 1) &
4504 ~(0x01 << devno), 1);
4505 pci_write_config(gparent, 0x44,
4506 (pci_read_config(gparent, 0x44, 4) &
4507 ~(0xff << offset)) |
4508 (dmatimings[mode & ATA_MODE_MASK] << offset), 4);
4511 pci_write_config(gparent, 0x54,
4512 pci_read_config(gparent, 0x54, 1) &
4513 ~(0x01 << devno), 1);
4515 pci_write_config(gparent, 0x40,
4516 (pci_read_config(gparent, 0x40, 4) &
4517 ~(0xff << offset)) |
4518 (piotimings[ata_mode2idx(mode)] << offset), 4);
4519 atadev->mode = mode;
4525 * Silicon Image Inc. (SiI) (former CMD) chipset support functions
4528 ata_sii_ident(device_t dev)
4530 struct ata_pci_controller *ctlr = device_get_softc(dev);
4531 struct ata_chip_id *idx;
4532 static struct ata_chip_id ids[] =
4533 {{ ATA_SII3114, 0x00, SIIMEMIO, SII4CH, ATA_SA150, "SiI 3114" },
4534 { ATA_SII3512, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3512" },
4535 { ATA_SII3112, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
4536 { ATA_SII3112_1, 0x02, SIIMEMIO, 0, ATA_SA150, "SiI 3112" },
4537 { ATA_SII3512, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3512" },
4538 { ATA_SII3112, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
4539 { ATA_SII3112_1, 0x00, SIIMEMIO, SIIBUG, ATA_SA150, "SiI 3112" },
4540 { ATA_SII3124, 0x00, SIIPRBIO, SII4CH, ATA_SA300, "SiI 3124" },
4541 { ATA_SII3132, 0x00, SIIPRBIO, 0, ATA_SA300, "SiI 3132" },
4542 { ATA_SII0680, 0x00, SIIMEMIO, SIISETCLK, ATA_UDMA6, "SiI 0680" },
4543 { ATA_CMD649, 0x00, 0, SIIINTR, ATA_UDMA5, "CMD 649" },
4544 { ATA_CMD648, 0x00, 0, SIIINTR, ATA_UDMA4, "CMD 648" },
4545 { ATA_CMD646, 0x07, 0, 0, ATA_UDMA2, "CMD 646U2" },
4546 { ATA_CMD646, 0x00, 0, 0, ATA_WDMA2, "CMD 646" },
4547 { 0, 0, 0, 0, 0, 0}};
4550 if (!(idx = ata_match_chip(dev, ids)))
4553 ksprintf(buffer, "%s %s controller", idx->text, ata_mode2str(idx->max_dma));
4554 device_set_desc_copy(dev, buffer);
4556 ctlr->chipinit = ata_sii_chipinit;
4561 ata_sii_chipinit(device_t dev)
4563 struct ata_pci_controller *ctlr = device_get_softc(dev);
4565 if (ata_setup_interrupt(dev))
4568 switch (ctlr->chip->cfg1) {
4570 ctlr->r_type1 = SYS_RES_MEMORY;
4571 ctlr->r_rid1 = PCIR_BAR(0);
4572 if (!(ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1,
4573 &ctlr->r_rid1, RF_ACTIVE))){
4574 ata_teardown_interrupt(dev);
4578 ctlr->r_rid2 = PCIR_BAR(2);
4579 ctlr->r_type2 = SYS_RES_MEMORY;
4580 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4581 &ctlr->r_rid2, RF_ACTIVE))){
4582 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1,ctlr->r_res1);
4583 ata_teardown_interrupt(dev);
4586 ctlr->allocate = ata_siiprb_allocate;
4587 ctlr->reset = ata_siiprb_reset;
4588 ctlr->dmainit = ata_siiprb_dmainit;
4589 ctlr->setmode = ata_sata_setmode;
4590 ctlr->channels = (ctlr->chip->cfg2 == SII4CH) ? 4 : 2;
4592 /* reset controller */
4593 ATA_OUTL(ctlr->r_res1, 0x0040, 0x80000000);
4595 ATA_OUTL(ctlr->r_res1, 0x0040, 0x0000000f);
4597 /* enable PCI interrupt */
4598 pci_write_config(dev, PCIR_COMMAND,
4599 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400, 2);
4603 ctlr->r_type2 = SYS_RES_MEMORY;
4604 ctlr->r_rid2 = PCIR_BAR(5);
4605 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
4606 &ctlr->r_rid2, RF_ACTIVE))){
4607 ata_teardown_interrupt(dev);
4611 if (ctlr->chip->cfg2 & SIISETCLK) {
4612 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
4613 pci_write_config(dev, 0x8a,
4614 (pci_read_config(dev, 0x8a, 1) & 0xcf)|0x10,1);
4615 if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
4616 device_printf(dev, "%s could not set ATA133 clock\n",
4620 /* if we have 4 channels enable the second set */
4621 if (ctlr->chip->cfg2 & SII4CH) {
4622 ATA_OUTL(ctlr->r_res2, 0x0200, 0x00000002);
4626 /* dont block interrupts from any channel */
4627 pci_write_config(dev, 0x48,
4628 (pci_read_config(dev, 0x48, 4) & ~0x03c00000), 4);
4630 /* enable PCI interrupt as BIOS might not */
4631 pci_write_config(dev, 0x8a, (pci_read_config(dev, 0x8a, 1) & 0x3f), 1);
4633 ctlr->allocate = ata_sii_allocate;
4634 if (ctlr->chip->max_dma >= ATA_SA150) {
4635 ctlr->reset = ata_sii_reset;
4636 ctlr->setmode = ata_sata_setmode;
4639 ctlr->setmode = ata_sii_setmode;
4643 if ((pci_read_config(dev, 0x51, 1) & 0x08) != 0x08) {
4644 device_printf(dev, "HW has secondary channel disabled\n");
4648 /* enable interrupt as BIOS might not */
4649 pci_write_config(dev, 0x71, 0x01, 1);
4651 ctlr->allocate = ata_cmd_allocate;
4652 ctlr->setmode = ata_cmd_setmode;
4659 ata_cmd_allocate(device_t dev)
4661 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4662 struct ata_channel *ch = device_get_softc(dev);
4664 /* setup the usual register normal pci style */
4665 if (ata_pci_allocate(dev))
4668 if (ctlr->chip->cfg2 & SIIINTR)
4669 ch->hw.status = ata_cmd_status;
4675 ata_cmd_status(device_t dev)
4677 struct ata_channel *ch = device_get_softc(dev);
4680 if (((reg71 = pci_read_config(device_get_parent(ch->dev), 0x71, 1)) &
4681 (ch->unit ? 0x08 : 0x04))) {
4682 pci_write_config(device_get_parent(ch->dev), 0x71,
4683 reg71 & ~(ch->unit ? 0x04 : 0x08), 1);
4684 return ata_pci_status(dev);
4690 ata_cmd_setmode(device_t dev, int mode)
4692 device_t gparent = GRANDPARENT(dev);
4693 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4694 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4695 struct ata_device *atadev = device_get_softc(dev);
4696 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
4699 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
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);
4710 int treg = 0x54 + ((devno < 3) ? (devno << 1) : 7);
4711 int ureg = ch->unit ? 0x7b : 0x73;
4713 if (mode >= ATA_UDMA0) {
4714 int udmatimings[][2] = { { 0x31, 0xc2 }, { 0x21, 0x82 },
4715 { 0x11, 0x42 }, { 0x25, 0x8a },
4716 { 0x15, 0x4a }, { 0x05, 0x0a } };
4718 u_int8_t umode = pci_read_config(gparent, ureg, 1);
4720 umode &= ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca);
4721 umode |= udmatimings[mode & ATA_MODE_MASK][ATA_DEV(atadev->unit)];
4722 pci_write_config(gparent, ureg, umode, 1);
4724 else if (mode >= ATA_WDMA0) {
4725 int dmatimings[] = { 0x87, 0x32, 0x3f };
4727 pci_write_config(gparent, treg, dmatimings[mode & ATA_MODE_MASK],1);
4728 pci_write_config(gparent, ureg,
4729 pci_read_config(gparent, ureg, 1) &
4730 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4733 int piotimings[] = { 0xa9, 0x57, 0x44, 0x32, 0x3f };
4734 pci_write_config(gparent, treg,
4735 piotimings[(mode & ATA_MODE_MASK) - ATA_PIO0], 1);
4736 pci_write_config(gparent, ureg,
4737 pci_read_config(gparent, ureg, 1) &
4738 ~(atadev->unit == ATA_MASTER ? 0x35 : 0xca), 1);
4740 atadev->mode = mode;
4745 ata_sii_allocate(device_t dev)
4747 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4748 struct ata_channel *ch = device_get_softc(dev);
4749 int unit01 = (ch->unit & 1), unit10 = (ch->unit & 2);
4752 for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
4753 ch->r_io[i].res = ctlr->r_res2;
4754 ch->r_io[i].offset = 0x80 + i + (unit01 << 6) + (unit10 << 8);
4756 ch->r_io[ATA_CONTROL].res = ctlr->r_res2;
4757 ch->r_io[ATA_CONTROL].offset = 0x8a + (unit01 << 6) + (unit10 << 8);
4758 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
4759 ata_default_registers(dev);
4761 ch->r_io[ATA_BMCMD_PORT].res = ctlr->r_res2;
4762 ch->r_io[ATA_BMCMD_PORT].offset = 0x00 + (unit01 << 3) + (unit10 << 8);
4763 ch->r_io[ATA_BMSTAT_PORT].res = ctlr->r_res2;
4764 ch->r_io[ATA_BMSTAT_PORT].offset = 0x02 + (unit01 << 3) + (unit10 << 8);
4765 ch->r_io[ATA_BMDTP_PORT].res = ctlr->r_res2;
4766 ch->r_io[ATA_BMDTP_PORT].offset = 0x04 + (unit01 << 3) + (unit10 << 8);
4768 if (ctlr->chip->max_dma >= ATA_SA150) {
4769 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4770 ch->r_io[ATA_SSTATUS].offset = 0x104 + (unit01 << 7) + (unit10 << 8);
4771 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4772 ch->r_io[ATA_SERROR].offset = 0x108 + (unit01 << 7) + (unit10 << 8);
4773 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4774 ch->r_io[ATA_SCONTROL].offset = 0x100 + (unit01 << 7) + (unit10 << 8);
4775 ch->flags |= ATA_NO_SLAVE;
4777 /* enable PHY state change interrupt */
4778 ATA_OUTL(ctlr->r_res2, 0x148 + (unit01 << 7) + (unit10 << 8),(1 << 16));
4781 if ((ctlr->chip->cfg2 & SIIBUG) && ch->dma) {
4782 /* work around errata in early chips */
4783 ch->dma->boundary = 16 * DEV_BSIZE;
4784 ch->dma->segsize = 15 * DEV_BSIZE;
4788 ch->hw.status = ata_sii_status;
4793 ata_sii_status(device_t dev)
4795 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4796 struct ata_channel *ch = device_get_softc(dev);
4797 int offset0 = ((ch->unit & 1) << 3) + ((ch->unit & 2) << 8);
4798 int offset1 = ((ch->unit & 1) << 6) + ((ch->unit & 2) << 8);
4800 /* do we have any PHY events ? */
4801 if (ctlr->chip->max_dma >= ATA_SA150 &&
4802 (ATA_INL(ctlr->r_res2, 0x10 + offset0) & 0x00000010))
4803 ata_sata_phy_check_events(dev);
4805 if (ATA_INL(ctlr->r_res2, 0xa0 + offset1) & 0x00000800)
4806 return ata_pci_status(dev);
4812 ata_sii_reset(device_t dev)
4814 if (ata_sata_phy_reset(dev))
4815 ata_generic_reset(dev);
4819 ata_sii_setmode(device_t dev, int mode)
4821 device_t gparent = GRANDPARENT(dev);
4822 struct ata_pci_controller *ctlr = device_get_softc(gparent);
4823 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
4824 struct ata_device *atadev = device_get_softc(dev);
4825 int rego = (ch->unit << 4) + (ATA_DEV(atadev->unit) << 1);
4826 int mreg = ch->unit ? 0x84 : 0x80;
4827 int mask = 0x03 << (ATA_DEV(atadev->unit) << 2);
4828 int mval = pci_read_config(gparent, mreg, 1) & ~mask;
4831 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
4833 if (ctlr->chip->cfg2 & SIISETCLK) {
4834 if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x79, 1) &
4835 (ch->unit ? 0x02 : 0x01))) {
4836 ata_print_cable(dev, "controller");
4841 mode = ata_check_80pin(dev, mode);
4843 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
4846 device_printf(dev, "%ssetting %s on %s chip\n",
4847 (error) ? "FAILURE " : "",
4848 ata_mode2str(mode), ctlr->chip->text);
4852 if (mode >= ATA_UDMA0) {
4853 u_int8_t udmatimings[] = { 0xf, 0xb, 0x7, 0x5, 0x3, 0x2, 0x1 };
4854 u_int8_t ureg = 0xac + rego;
4856 pci_write_config(gparent, mreg,
4857 mval | (0x03 << (ATA_DEV(atadev->unit) << 2)), 1);
4858 pci_write_config(gparent, ureg,
4859 (pci_read_config(gparent, ureg, 1) & ~0x3f) |
4860 udmatimings[mode & ATA_MODE_MASK], 1);
4863 else if (mode >= ATA_WDMA0) {
4864 u_int8_t dreg = 0xa8 + rego;
4865 u_int16_t dmatimings[] = { 0x2208, 0x10c2, 0x10c1 };
4867 pci_write_config(gparent, mreg,
4868 mval | (0x02 << (ATA_DEV(atadev->unit) << 2)), 1);
4869 pci_write_config(gparent, dreg, dmatimings[mode & ATA_MODE_MASK], 2);
4873 u_int8_t preg = 0xa4 + rego;
4874 u_int16_t piotimings[] = { 0x328a, 0x2283, 0x1104, 0x10c3, 0x10c1 };
4876 pci_write_config(gparent, mreg,
4877 mval | (0x01 << (ATA_DEV(atadev->unit) << 2)), 1);
4878 pci_write_config(gparent, preg, piotimings[mode & ATA_MODE_MASK], 2);
4880 atadev->mode = mode;
4883 struct ata_siiprb_dma_prdentry {
4889 struct ata_siiprb_ata_command {
4890 u_int32_t reserved0;
4891 struct ata_siiprb_dma_prdentry prd[126];
4894 struct ata_siiprb_atapi_command {
4896 struct ata_siiprb_dma_prdentry prd[125];
4899 struct ata_siiprb_command {
4901 u_int16_t protocol_override;
4902 u_int32_t transfer_count;
4905 struct ata_siiprb_ata_command ata;
4906 struct ata_siiprb_atapi_command atapi;
4911 ata_siiprb_allocate(device_t dev)
4913 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4914 struct ata_channel *ch = device_get_softc(dev);
4915 int offset = ch->unit * 0x2000;
4917 /* set the SATA resources */
4918 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
4919 ch->r_io[ATA_SSTATUS].offset = 0x1f04 + offset;
4920 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
4921 ch->r_io[ATA_SERROR].offset = 0x1f08 + offset;
4922 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
4923 ch->r_io[ATA_SCONTROL].offset = 0x1f00 + offset;
4924 ch->r_io[ATA_SACTIVE].res = ctlr->r_res2;
4925 ch->r_io[ATA_SACTIVE].offset = 0x1f0c + offset;
4927 ch->hw.begin_transaction = ata_siiprb_begin_transaction;
4928 ch->hw.end_transaction = ata_siiprb_end_transaction;
4929 ch->hw.status = ata_siiprb_status;
4930 ch->hw.command = NULL; /* not used here */
4935 ata_siiprb_status(device_t dev)
4937 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
4938 struct ata_channel *ch = device_get_softc(dev);
4939 int offset = ch->unit * 0x2000;
4941 if ((ATA_INL(ctlr->r_res1, 0x0044) & (1 << ch->unit))) {
4942 u_int32_t istatus = ATA_INL(ctlr->r_res2, 0x1008 + offset);
4944 /* do we have any PHY events ? */
4945 ata_sata_phy_check_events(dev);
4947 /* clear interrupt(s) */
4948 ATA_OUTL(ctlr->r_res2, 0x1008 + offset, istatus);
4950 /* do we have any device action ? */
4951 return (istatus & 0x00000001);
4957 ata_siiprb_begin_transaction(struct ata_request *request)
4959 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
4960 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
4961 struct ata_siiprb_command *prb;
4962 int offset = ch->unit * 0x2000;
4966 /* check for 48 bit access and convert if needed */
4967 ata_modify_if_48bit(request);
4969 /* get a piece of the workspace for this request */
4970 prb = (struct ata_siiprb_command *)
4971 (ch->dma->work + (sizeof(struct ata_siiprb_command) * tag));
4973 /* set basic prd options ata/atapi etc etc */
4974 bzero(prb, sizeof(struct ata_siiprb_command));
4976 /* setup the FIS for this request */
4977 if (!ata_request2fis_h2d(request, &prb->fis[0])) {
4978 device_printf(request->dev, "setting up SATA FIS failed\n");
4979 request->result = EIO;
4980 return ATA_OP_FINISHED;
4983 /* if request moves data setup and load SG list */
4984 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
4985 struct ata_siiprb_dma_prdentry *prd;
4987 if (request->flags & ATA_R_ATAPI)
4988 prd = &prb->u.atapi.prd[0];
4990 prd = &prb->u.ata.prd[0];
4991 if (ch->dma->load(ch->dev, request->data, request->bytecount,
4992 request->flags & ATA_R_READ, prd, &dummy)) {
4993 device_printf(request->dev, "setting up DMA failed\n");
4994 request->result = EIO;
4995 return ATA_OP_FINISHED;
4999 /* activate the prb */
5000 prb_bus = ch->dma->work_bus + (sizeof(struct ata_siiprb_command) * tag);
5001 ATA_OUTL(ctlr->r_res2,
5002 0x1c00 + offset + (tag * sizeof(u_int64_t)), prb_bus);
5003 ATA_OUTL(ctlr->r_res2,
5004 0x1c04 + offset + (tag * sizeof(u_int64_t)), prb_bus>>32);
5006 /* start the timeout */
5007 callout_reset(&request->callout, request->timeout * hz,
5008 (timeout_t*)ata_timeout, request);
5009 return ATA_OP_CONTINUES;
5013 ata_siiprb_end_transaction(struct ata_request *request)
5015 struct ata_pci_controller *ctlr=device_get_softc(GRANDPARENT(request->dev));
5016 struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
5017 struct ata_siiprb_command *prb;
5018 int offset = ch->unit * 0x2000;
5021 /* kill the timeout */
5022 callout_stop(&request->callout);
5024 prb = (struct ata_siiprb_command *)
5025 ((u_int8_t *)rman_get_virtual(ctlr->r_res2) + (tag << 7) + offset);
5027 /* if error status get details */
5028 request->status = prb->fis[2];
5029 if (request->status & ATA_S_ERROR)
5030 request->error = prb->fis[3];
5032 /* update progress */
5033 if (!(request->status & ATA_S_ERROR) && !(request->flags & ATA_R_TIMEOUT)) {
5034 if (request->flags & ATA_R_READ)
5035 request->donecount = prb->transfer_count;
5037 request->donecount = request->bytecount;
5040 /* any controller errors flagged ? */
5041 if ((error = ATA_INL(ctlr->r_res2, 0x1024 + offset))) {
5042 kprintf("ata_siiprb_end_transaction %s error=%08x\n",
5043 ata_cmd2str(request), error);
5046 /* release SG list etc */
5047 ch->dma->unload(ch->dev);
5049 return ATA_OP_FINISHED;
5053 ata_siiprb_reset(device_t dev)
5055 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5056 struct ata_channel *ch = device_get_softc(dev);
5057 int offset = ch->unit * 0x2000;
5058 struct ata_siiprb_command *prb;
5060 u_int32_t status, signature;
5061 int timeout, tag = 0;
5063 /* reset channel HW */
5064 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000001);
5066 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000001);
5069 /* poll for channel ready */
5070 for (timeout = 0; timeout < 1000; timeout++) {
5071 if ((status = ATA_INL(ctlr->r_res2, 0x1000 + offset)) & 0x00040000)
5075 if (timeout >= 1000) {
5076 device_printf(ch->dev, "channel HW reset timeout reset failure\n");
5081 device_printf(ch->dev, "channel HW reset time=%dms\n", timeout * 1);
5084 if (!ata_sata_phy_reset(dev)) {
5086 device_printf(ch->dev, "phy reset found no device\n");
5091 /* get a piece of the workspace for a soft reset request */
5092 prb = (struct ata_siiprb_command *)
5093 (ch->dma->work + (sizeof(struct ata_siiprb_command) * tag));
5094 bzero(prb, sizeof(struct ata_siiprb_command));
5095 prb->control = htole16(0x0080);
5097 /* activate the soft reset prb */
5098 prb_bus = ch->dma->work_bus + (sizeof(struct ata_siiprb_command) * tag);
5099 ATA_OUTL(ctlr->r_res2,
5100 0x1c00 + offset + (tag * sizeof(u_int64_t)), prb_bus);
5101 ATA_OUTL(ctlr->r_res2,
5102 0x1c04 + offset + (tag * sizeof(u_int64_t)), prb_bus>>32);
5104 /* poll for channel ready */
5105 for (timeout = 0; timeout < 1000; timeout++) {
5107 if ((status = ATA_INL(ctlr->r_res2, 0x1008 + offset)) & 0x00010000)
5110 if (timeout >= 1000) {
5111 device_printf(ch->dev, "reset timeout - no device found\n");
5116 device_printf(ch->dev, "soft reset exec time=%dms status=%08x\n",
5119 /* find out whats there */
5120 prb = (struct ata_siiprb_command *)
5121 ((u_int8_t *)rman_get_virtual(ctlr->r_res2) + (tag << 7) + offset);
5123 prb->fis[12]|(prb->fis[4]<<8)|(prb->fis[5]<<16)|(prb->fis[6]<<24);
5125 device_printf(ch->dev, "signature=%08x\n", signature);
5126 switch (signature) {
5128 ch->devices = ATA_ATAPI_MASTER;
5129 device_printf(ch->dev, "SATA ATAPI devices not supported yet\n");
5133 ch->devices = ATA_PORTMULTIPLIER;
5134 device_printf(ch->dev, "Portmultipliers not supported yet\n");
5138 ch->devices = ATA_ATA_MASTER;
5145 /* clear interrupt(s) */
5146 ATA_OUTL(ctlr->r_res2, 0x1008 + offset, 0x000008ff);
5148 /* require explicit interrupt ack */
5149 ATA_OUTL(ctlr->r_res2, 0x1000 + offset, 0x00000008);
5152 ATA_OUTL(ctlr->r_res2, 0x1004 + offset, 0x00000400);
5154 /* enable interrupts wanted */
5155 ATA_OUTL(ctlr->r_res2, 0x1010 + offset, 0x000000ff);
5159 ata_siiprb_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
5161 struct ata_dmasetprd_args *args = xsc;
5162 struct ata_siiprb_dma_prdentry *prd = args->dmatab;
5165 if ((args->error = error))
5168 for (i = 0; i < nsegs; i++) {
5169 prd[i].addr = htole64(segs[i].ds_addr);
5170 prd[i].count = htole32(segs[i].ds_len);
5172 prd[i - 1].control = htole32(ATA_DMA_EOT);
5176 ata_siiprb_dmainit(device_t dev)
5178 struct ata_channel *ch = device_get_softc(dev);
5182 /* note start and stop are not used here */
5183 ch->dma->setprd = ata_siiprb_dmasetprd;
5184 ch->dma->max_address = BUS_SPACE_MAXADDR;
5190 * Silicon Integrated Systems Corp. (SiS) chipset support functions
5193 ata_sis_ident(device_t dev)
5195 struct ata_pci_controller *ctlr = device_get_softc(dev);
5196 struct ata_chip_id *idx;
5197 static struct ata_chip_id ids[] =
5198 {{ ATA_SIS182, 0x00, SISSATA, 0, ATA_SA150, "182" }, /* south */
5199 { ATA_SIS181, 0x00, SISSATA, 0, ATA_SA150, "181" }, /* south */
5200 { ATA_SIS180, 0x00, SISSATA, 0, ATA_SA150, "180" }, /* south */
5201 { ATA_SIS965, 0x00, SIS133NEW, 0, ATA_UDMA6, "965" }, /* south */
5202 { ATA_SIS964, 0x00, SIS133NEW, 0, ATA_UDMA6, "964" }, /* south */
5203 { ATA_SIS963, 0x00, SIS133NEW, 0, ATA_UDMA6, "963" }, /* south */
5204 { ATA_SIS962, 0x00, SIS133NEW, 0, ATA_UDMA6, "962" }, /* south */
5206 { ATA_SIS745, 0x00, SIS100NEW, 0, ATA_UDMA5, "745" }, /* 1chip */
5207 { ATA_SIS735, 0x00, SIS100NEW, 0, ATA_UDMA5, "735" }, /* 1chip */
5208 { ATA_SIS733, 0x00, SIS100NEW, 0, ATA_UDMA5, "733" }, /* 1chip */
5209 { ATA_SIS730, 0x00, SIS100OLD, 0, ATA_UDMA5, "730" }, /* 1chip */
5211 { ATA_SIS635, 0x00, SIS100NEW, 0, ATA_UDMA5, "635" }, /* 1chip */
5212 { ATA_SIS633, 0x00, SIS100NEW, 0, ATA_UDMA5, "633" }, /* unknown */
5213 { ATA_SIS630, 0x30, SIS100OLD, 0, ATA_UDMA5, "630S"}, /* 1chip */
5214 { ATA_SIS630, 0x00, SIS66, 0, ATA_UDMA4, "630" }, /* 1chip */
5215 { ATA_SIS620, 0x00, SIS66, 0, ATA_UDMA4, "620" }, /* 1chip */
5217 { ATA_SIS550, 0x00, SIS66, 0, ATA_UDMA5, "550" },
5218 { ATA_SIS540, 0x00, SIS66, 0, ATA_UDMA4, "540" },
5219 { ATA_SIS530, 0x00, SIS66, 0, ATA_UDMA4, "530" },
5221 { ATA_SIS5513, 0xc2, SIS33, 1, ATA_UDMA2, "5513" },
5222 { ATA_SIS5513, 0x00, SIS33, 1, ATA_WDMA2, "5513" },
5223 { 0, 0, 0, 0, 0, 0 }};
5227 if (!(idx = ata_find_chip(dev, ids, -pci_get_slot(dev))))
5230 if (idx->cfg2 && !found) {
5231 u_int8_t reg57 = pci_read_config(dev, 0x57, 1);
5233 pci_write_config(dev, 0x57, (reg57 & 0x7f), 1);
5234 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5518) {
5236 idx->cfg1 = SIS133NEW;
5237 idx->max_dma = ATA_UDMA6;
5238 ksprintf(buffer, "SiS 962/963 %s controller",
5239 ata_mode2str(idx->max_dma));
5241 pci_write_config(dev, 0x57, reg57, 1);
5243 if (idx->cfg2 && !found) {
5244 u_int8_t reg4a = pci_read_config(dev, 0x4a, 1);
5246 pci_write_config(dev, 0x4a, (reg4a | 0x10), 1);
5247 if (pci_read_config(dev, PCIR_DEVVENDOR, 4) == ATA_SIS5517) {
5248 struct ata_chip_id id[] =
5249 {{ ATA_SISSOUTH, 0x10, 0, 0, 0, "" }, { 0, 0, 0, 0, 0, 0 }};
5252 if (ata_find_chip(dev, id, pci_get_slot(dev))) {
5253 idx->cfg1 = SIS133OLD;
5254 idx->max_dma = ATA_UDMA6;
5257 idx->cfg1 = SIS100NEW;
5258 idx->max_dma = ATA_UDMA5;
5260 ksprintf(buffer, "SiS 961 %s controller",ata_mode2str(idx->max_dma));
5262 pci_write_config(dev, 0x4a, reg4a, 1);
5265 ksprintf(buffer,"SiS %s %s controller",
5266 idx->text, ata_mode2str(idx->max_dma));
5268 device_set_desc_copy(dev, buffer);
5270 ctlr->chipinit = ata_sis_chipinit;
5275 ata_sis_chipinit(device_t dev)
5277 struct ata_pci_controller *ctlr = device_get_softc(dev);
5279 if (ata_setup_interrupt(dev))
5282 switch (ctlr->chip->cfg1) {
5287 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 1) & ~0x04, 1);
5291 pci_write_config(dev, 0x49, pci_read_config(dev, 0x49, 1) & ~0x01, 1);
5294 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 2) | 0x0008, 2);
5295 pci_write_config(dev, 0x52, pci_read_config(dev, 0x52, 2) | 0x0008, 2);
5298 ctlr->r_type2 = SYS_RES_IOPORT;
5299 ctlr->r_rid2 = PCIR_BAR(5);
5300 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
5301 &ctlr->r_rid2, RF_ACTIVE))) {
5302 ctlr->allocate = ata_sis_allocate;
5303 ctlr->reset = ata_sis_reset;
5305 /* enable PCI interrupt */
5306 pci_write_config(dev, PCIR_COMMAND,
5307 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
5309 ctlr->setmode = ata_sata_setmode;
5312 ata_teardown_interrupt(dev);
5315 ctlr->setmode = ata_sis_setmode;
5320 ata_sis_allocate(device_t dev)
5322 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5323 struct ata_channel *ch = device_get_softc(dev);
5324 int offset = ch->unit << ((ctlr->chip->chipid == ATA_SIS182) ? 5 : 6);
5326 /* setup the usual register normal pci style */
5327 if (ata_pci_allocate(dev))
5330 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
5331 ch->r_io[ATA_SSTATUS].offset = 0x00 + offset;
5332 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
5333 ch->r_io[ATA_SERROR].offset = 0x04 + offset;
5334 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
5335 ch->r_io[ATA_SCONTROL].offset = 0x08 + offset;
5336 ch->flags |= ATA_NO_SLAVE;
5338 /* XXX SOS PHY hotplug handling missing in SiS chip ?? */
5339 /* XXX SOS unknown how to enable PHY state change interrupt */
5344 ata_sis_reset(device_t dev)
5346 if (ata_sata_phy_reset(dev))
5347 ata_generic_reset(dev);
5351 ata_sis_setmode(device_t dev, int mode)
5353 device_t gparent = GRANDPARENT(dev);
5354 struct ata_pci_controller *ctlr = device_get_softc(gparent);
5355 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5356 struct ata_device *atadev = device_get_softc(dev);
5357 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
5360 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
5362 if (ctlr->chip->cfg1 == SIS133NEW) {
5363 if (mode > ATA_UDMA2 &&
5364 pci_read_config(gparent, ch->unit ? 0x52 : 0x50,2) & 0x8000) {
5365 ata_print_cable(dev, "controller");
5370 if (mode > ATA_UDMA2 &&
5371 pci_read_config(gparent, 0x48, 1)&(ch->unit ? 0x20 : 0x10)) {
5372 ata_print_cable(dev, "controller");
5377 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5380 device_printf(dev, "%ssetting %s on %s chip\n",
5381 (error) ? "FAILURE " : "",
5382 ata_mode2str(mode), ctlr->chip->text);
5384 switch (ctlr->chip->cfg1) {
5386 u_int32_t timings[] =
5387 { 0x28269008, 0x0c266008, 0x04263008, 0x0c0a3008, 0x05093008,
5388 0x22196008, 0x0c0a3008, 0x05093008, 0x050939fc, 0x050936ac,
5389 0x0509347c, 0x0509325c, 0x0509323c, 0x0509322c, 0x0509321c};
5392 reg = (pci_read_config(gparent, 0x57, 1)&0x40?0x70:0x40)+(devno<<2);
5393 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 4);
5397 u_int16_t timings[] =
5398 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033, 0x0031,
5399 0x8f31, 0x8a31, 0x8731, 0x8531, 0x8331, 0x8231, 0x8131 };
5401 u_int16_t reg = 0x40 + (devno << 1);
5403 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5407 u_int16_t timings[] =
5408 { 0x00cb, 0x0067, 0x0044, 0x0033, 0x0031, 0x0044, 0x0033,
5409 0x0031, 0x8b31, 0x8731, 0x8531, 0x8431, 0x8231, 0x8131 };
5410 u_int16_t reg = 0x40 + (devno << 1);
5412 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5418 u_int16_t timings[] =
5419 { 0x0c0b, 0x0607, 0x0404, 0x0303, 0x0301, 0x0404, 0x0303,
5420 0x0301, 0xf301, 0xd301, 0xb301, 0xa301, 0x9301, 0x8301 };
5421 u_int16_t reg = 0x40 + (devno << 1);
5423 pci_write_config(gparent, reg, timings[ata_mode2idx(mode)], 2);
5427 atadev->mode = mode;
5432 /* VIA Technologies Inc. chipset support functions */
5434 ata_via_ident(device_t dev)
5436 struct ata_pci_controller *ctlr = device_get_softc(dev);
5437 struct ata_chip_id *idx;
5438 static struct ata_chip_id ids[] =
5439 {{ ATA_VIA82C586, 0x02, VIA33, 0x00, ATA_UDMA2, "82C586B" },
5440 { ATA_VIA82C586, 0x00, VIA33, 0x00, ATA_WDMA2, "82C586" },
5441 { ATA_VIA82C596, 0x12, VIA66, VIACLK, ATA_UDMA4, "82C596B" },
5442 { ATA_VIA82C596, 0x00, VIA33, 0x00, ATA_UDMA2, "82C596" },
5443 { ATA_VIA82C686, 0x40, VIA100, VIABUG, ATA_UDMA5, "82C686B"},
5444 { ATA_VIA82C686, 0x10, VIA66, VIACLK, ATA_UDMA4, "82C686A" },
5445 { ATA_VIA82C686, 0x00, VIA33, 0x00, ATA_UDMA2, "82C686" },
5446 { ATA_VIA8231, 0x00, VIA100, VIABUG, ATA_UDMA5, "8231" },
5447 { ATA_VIA8233, 0x00, VIA100, 0x00, ATA_UDMA5, "8233" },
5448 { ATA_VIA8233C, 0x00, VIA100, 0x00, ATA_UDMA5, "8233C" },
5449 { ATA_VIA8233A, 0x00, VIA133, 0x00, ATA_UDMA6, "8233A" },
5450 { ATA_VIA8235, 0x00, VIA133, 0x00, ATA_UDMA6, "8235" },
5451 { ATA_VIA8237, 0x00, VIA133, 0x00, ATA_UDMA6, "8237" },
5452 { ATA_VIA8237A, 0x00, VIA133, 0x00, ATA_UDMA6, "8237A" },
5453 { ATA_VIA8251, 0x00, VIA133, 0x00, ATA_UDMA6, "8251" },
5454 { 0, 0, 0, 0, 0, 0 }};
5455 static struct ata_chip_id new_ids[] =
5456 {{ ATA_VIA6410, 0x00, 0, 0x00, ATA_UDMA6, "6410" },
5457 { ATA_VIA6420, 0x00, 7, 0x00, ATA_SA150, "6420" },
5458 { ATA_VIA6421, 0x00, 6, VIABAR, ATA_SA150, "6421" },
5459 { ATA_VIA8237A, 0x00, 7, 0x00, ATA_SA150, "8237A" },
5460 { ATA_VIA8237S, 0x00, 7, 0x00, ATA_SA150, "8237S" },
5461 { ATA_VIA8251, 0x00, 0, VIAAHCI, ATA_SA300, "8251" },
5462 { 0, 0, 0, 0, 0, 0 }};
5465 if (pci_get_devid(dev) == ATA_VIA82C571) {
5466 if (!(idx = ata_find_chip(dev, ids, -99)))
5470 if (!(idx = ata_match_chip(dev, new_ids)))
5474 ksprintf(buffer, "VIA %s %s controller",
5475 idx->text, ata_mode2str(idx->max_dma));
5476 device_set_desc_copy(dev, buffer);
5478 ctlr->chipinit = ata_via_chipinit;
5483 ata_via_chipinit(device_t dev)
5485 struct ata_pci_controller *ctlr = device_get_softc(dev);
5487 if (ata_setup_interrupt(dev))
5490 if (ctlr->chip->max_dma >= ATA_SA150) {
5491 /* do we have AHCI capability ? */
5492 if ((ctlr->chip->cfg2 == VIAAHCI) && ata_ahci_chipinit(dev) != ENXIO)
5495 ctlr->r_type2 = SYS_RES_IOPORT;
5496 ctlr->r_rid2 = PCIR_BAR(5);
5497 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
5498 &ctlr->r_rid2, RF_ACTIVE))) {
5499 ctlr->allocate = ata_via_allocate;
5500 ctlr->reset = ata_via_reset;
5502 /* enable PCI interrupt */
5503 pci_write_config(dev, PCIR_COMMAND,
5504 pci_read_config(dev, PCIR_COMMAND, 2) & ~0x0400,2);
5507 if (ctlr->chip->cfg2 & VIABAR) {
5509 ctlr->setmode = ata_via_setmode;
5512 ctlr->setmode = ata_sata_setmode;
5516 /* prepare for ATA-66 on the 82C686a and 82C596b */
5517 if (ctlr->chip->cfg2 & VIACLK)
5518 pci_write_config(dev, 0x50, 0x030b030b, 4);
5520 /* the southbridge might need the data corruption fix */
5521 if (ctlr->chip->cfg2 & VIABUG)
5522 ata_via_southbridge_fixup(dev);
5524 /* set fifo configuration half'n'half */
5525 pci_write_config(dev, 0x43,
5526 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
5528 /* set status register read retry */
5529 pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
5531 /* set DMA read & end-of-sector fifo flush */
5532 pci_write_config(dev, 0x46,
5533 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
5535 /* set sector size */
5536 pci_write_config(dev, 0x60, DEV_BSIZE, 2);
5537 pci_write_config(dev, 0x68, DEV_BSIZE, 2);
5539 ctlr->setmode = ata_via_family_setmode;
5544 ata_via_allocate(device_t dev)
5546 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5547 struct ata_channel *ch = device_get_softc(dev);
5549 /* newer SATA chips has resources in one BAR for each channel */
5550 if (ctlr->chip->cfg2 & VIABAR) {
5551 struct resource *r_io;
5554 rid = PCIR_BAR(ch->unit);
5555 if (!(r_io = bus_alloc_resource_any(device_get_parent(dev),
5560 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
5561 ch->r_io[i].res = r_io;
5562 ch->r_io[i].offset = i;
5564 ch->r_io[ATA_CONTROL].res = r_io;
5565 ch->r_io[ATA_CONTROL].offset = 2 + ATA_IOSIZE;
5566 ch->r_io[ATA_IDX_ADDR].res = r_io;
5567 ata_default_registers(dev);
5568 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
5569 ch->r_io[i].res = ctlr->r_res1;
5570 ch->r_io[i].offset = (i - ATA_BMCMD_PORT)+(ch->unit * ATA_BMIOSIZE);
5577 /* setup the usual register normal pci style */
5578 if (ata_pci_allocate(dev))
5582 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
5583 ch->r_io[ATA_SSTATUS].offset = (ch->unit << ctlr->chip->cfg1);
5584 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
5585 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << ctlr->chip->cfg1);
5586 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
5587 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << ctlr->chip->cfg1);
5588 ch->flags |= ATA_NO_SLAVE;
5590 /* XXX SOS PHY hotplug handling missing in VIA chip ?? */
5591 /* XXX SOS unknown how to enable PHY state change interrupt */
5596 ata_via_reset(device_t dev)
5598 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5599 struct ata_channel *ch = device_get_softc(dev);
5601 if ((ctlr->chip->cfg2 & VIABAR) && (ch->unit > 1))
5602 ata_generic_reset(dev);
5604 if (ata_sata_phy_reset(dev))
5605 ata_generic_reset(dev);
5609 ata_via_setmode(device_t dev, int mode)
5611 device_t gparent = GRANDPARENT(dev);
5612 struct ata_pci_controller *ctlr = device_get_softc(gparent);
5613 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5614 struct ata_device *atadev = device_get_softc(dev);
5617 if ((ctlr->chip->cfg2 & VIABAR) && (ch->unit > 1)) {
5618 u_int8_t pio_timings[] = { 0xa8, 0x65, 0x65, 0x32, 0x20,
5620 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
5621 u_int8_t dma_timings[] = { 0xee, 0xe8, 0xe6, 0xe4, 0xe2, 0xe1, 0xe0 };
5623 mode = ata_check_80pin(dev, ata_limit_mode(dev, mode, ATA_UDMA6));
5624 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5626 device_printf(dev, "%ssetting %s on %s chip\n",
5627 (error) ? "FAILURE " : "", ata_mode2str(mode),
5630 pci_write_config(gparent, 0xab, pio_timings[ata_mode2idx(mode)], 1);
5631 if (mode >= ATA_UDMA0)
5632 pci_write_config(gparent, 0xb3,
5633 dma_timings[mode & ATA_MODE_MASK], 1);
5634 atadev->mode = mode;
5638 ata_sata_setmode(dev, mode);
5642 ata_via_southbridge_fixup(device_t dev)
5647 if (device_get_children(device_get_parent(dev), &children, &nchildren))
5650 for (i = 0; i < nchildren; i++) {
5651 if (pci_get_devid(children[i]) == ATA_VIA8363 ||
5652 pci_get_devid(children[i]) == ATA_VIA8371 ||
5653 pci_get_devid(children[i]) == ATA_VIA8662 ||
5654 pci_get_devid(children[i]) == ATA_VIA8361) {
5655 u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
5657 if ((reg76 & 0xf0) != 0xd0) {
5659 "Correcting VIA config for southbridge data corruption bug\n");
5660 pci_write_config(children[i], 0x75, 0x80, 1);
5661 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
5666 kfree(children, M_TEMP);
5670 /* common code for VIA, AMD & nVidia */
5672 ata_via_family_setmode(device_t dev, int mode)
5674 device_t gparent = GRANDPARENT(dev);
5675 struct ata_pci_controller *ctlr = device_get_softc(gparent);
5676 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5677 struct ata_device *atadev = device_get_softc(dev);
5678 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
5679 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
5681 { 0xc2, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00 }, /* VIA ATA33 */
5682 { 0xee, 0xec, 0xea, 0xe9, 0xe8, 0x00, 0x00 }, /* VIA ATA66 */
5683 { 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0, 0x00 }, /* VIA ATA100 */
5684 { 0xf7, 0xf7, 0xf6, 0xf4, 0xf2, 0xf1, 0xf0 }, /* VIA ATA133 */
5685 { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 }}; /* AMD/nVIDIA */
5686 int devno = (ch->unit << 1) + ATA_DEV(atadev->unit);
5687 int reg = 0x53 - devno;
5690 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
5692 if (ctlr->chip->cfg2 & AMDCABLE) {
5693 if (mode > ATA_UDMA2 &&
5694 !(pci_read_config(gparent, 0x42, 1) & (1 << devno))) {
5695 ata_print_cable(dev, "controller");
5700 mode = ata_check_80pin(dev, mode);
5702 if (ctlr->chip->cfg2 & NVIDIA)
5705 if (ctlr->chip->cfg1 != VIA133)
5706 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
5708 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
5711 device_printf(dev, "%ssetting %s on %s chip\n",
5712 (error) ? "FAILURE " : "", ata_mode2str(mode),
5715 if (mode >= ATA_UDMA0)
5716 pci_write_config(gparent, reg,
5717 modes[ctlr->chip->cfg1][mode & ATA_MODE_MASK], 1);
5719 pci_write_config(gparent, reg, 0x8b, 1);
5720 atadev->mode = mode;
5725 /* misc functions */
5726 static struct ata_chip_id *
5727 ata_match_chip(device_t dev, struct ata_chip_id *index)
5729 while (index->chipid != 0) {
5730 if (pci_get_devid(dev) == index->chipid &&
5731 pci_get_revid(dev) >= index->chiprev)
5738 static struct ata_chip_id *
5739 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
5744 if (device_get_children(device_get_parent(dev), &children, &nchildren))
5747 while (index->chipid != 0) {
5748 for (i = 0; i < nchildren; i++) {
5749 if (((slot >= 0 && pci_get_slot(children[i]) == slot) ||
5750 (slot < 0 && pci_get_slot(children[i]) <= -slot)) &&
5751 pci_get_devid(children[i]) == index->chipid &&
5752 pci_get_revid(children[i]) >= index->chiprev) {
5753 kfree(children, M_TEMP);
5759 kfree(children, M_TEMP);
5764 ata_setup_interrupt(device_t dev)
5766 struct ata_pci_controller *ctlr = device_get_softc(dev);
5767 int rid = ATA_IRQ_RID;
5769 if (!ctlr->legacy) {
5770 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
5771 RF_SHAREABLE | RF_ACTIVE))) {
5772 device_printf(dev, "unable to map interrupt\n");
5775 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS,
5776 ata_generic_intr, ctlr, &ctlr->handle, NULL))) {
5777 device_printf(dev, "unable to setup interrupt\n");
5778 bus_release_resource(dev, SYS_RES_IRQ, rid, ctlr->r_irq);
5787 ata_teardown_interrupt(device_t dev)
5789 struct ata_pci_controller *ctlr = device_get_softc(dev);
5791 if (!ctlr->legacy) {
5793 bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
5794 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ctlr->r_irq);
5800 struct ata_serialize {
5801 struct spinlock locked_mtx;
5807 ata_serialize(device_t dev, int flags)
5809 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
5810 struct ata_channel *ch = device_get_softc(dev);
5811 struct ata_serialize *serial;
5812 static int inited = 0;
5816 serial = kmalloc(sizeof(struct ata_serialize),
5817 M_TEMP, M_INTWAIT | M_ZERO);
5818 spin_init(&serial->locked_mtx);
5819 serial->locked_ch = -1;
5820 serial->restart_ch = -1;
5821 device_set_ivars(ctlr->dev, serial);
5825 serial = device_get_ivars(ctlr->dev);
5827 spin_lock(&serial->locked_mtx);
5830 if (serial->locked_ch == -1)
5831 serial->locked_ch = ch->unit;
5832 if (serial->locked_ch != ch->unit)
5833 serial->restart_ch = ch->unit;
5837 if (serial->locked_ch == ch->unit) {
5838 serial->locked_ch = -1;
5839 if (serial->restart_ch != -1) {
5840 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
5841 serial->restart_ch = -1;
5842 spin_unlock(&serial->locked_mtx);
5853 res = serial->locked_ch;
5854 spin_unlock(&serial->locked_mtx);
5859 ata_print_cable(device_t dev, u_int8_t *who)
5862 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
5866 ata_atapi(device_t dev)
5868 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
5869 struct ata_device *atadev = device_get_softc(dev);
5871 return ((atadev->unit == ATA_MASTER && ch->devices & ATA_ATAPI_MASTER) ||
5872 (atadev->unit == ATA_SLAVE && ch->devices & ATA_ATAPI_SLAVE));
5876 ata_check_80pin(device_t dev, int mode)
5878 struct ata_device *atadev = device_get_softc(dev);
5880 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
5881 ata_print_cable(dev, "device");
5888 ata_mode2idx(int mode)
5890 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
5891 return (mode & ATA_MODE_MASK) + 8;
5892 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
5893 return (mode & ATA_MODE_MASK) + 5;
5894 return (mode & ATA_MODE_MASK) - ATA_PIO0;