2 * Copyright (c) 1998 - 2008 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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/module.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
37 #include <sys/endian.h>
38 #include <sys/malloc.h>
40 #include <sys/mutex.h>
42 #include <sys/taskqueue.h>
44 #include <machine/stdarg.h>
45 #include <machine/resource.h>
46 #include <machine/bus.h>
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/ata/ata-all.h>
51 #include <dev/ata/ata-pci.h>
54 /* local prototypes */
55 static int ata_intel_chipinit(device_t dev);
56 static int ata_intel_ch_attach(device_t dev);
57 static void ata_intel_reset(device_t dev);
58 static int ata_intel_old_setmode(device_t dev, int target, int mode);
59 static int ata_intel_new_setmode(device_t dev, int target, int mode);
60 static int ata_intel_sch_setmode(device_t dev, int target, int mode);
61 static int ata_intel_sata_getrev(device_t dev, int target);
62 static int ata_intel_sata_status(device_t dev);
63 static int ata_intel_sata_ahci_read(device_t dev, int port,
64 int reg, u_int32_t *result);
65 static int ata_intel_sata_cscr_read(device_t dev, int port,
66 int reg, u_int32_t *result);
67 static int ata_intel_sata_sidpr_read(device_t dev, int port,
68 int reg, u_int32_t *result);
69 static int ata_intel_sata_ahci_write(device_t dev, int port,
70 int reg, u_int32_t result);
71 static int ata_intel_sata_cscr_write(device_t dev, int port,
72 int reg, u_int32_t result);
73 static int ata_intel_sata_sidpr_write(device_t dev, int port,
74 int reg, u_int32_t result);
75 static int ata_intel_31244_ch_attach(device_t dev);
76 static int ata_intel_31244_ch_detach(device_t dev);
77 static int ata_intel_31244_status(device_t dev);
78 static void ata_intel_31244_tf_write(struct ata_request *request);
79 static void ata_intel_31244_reset(device_t dev);
89 * Intel chipset support functions
92 ata_intel_probe(device_t dev)
94 struct ata_pci_controller *ctlr = device_get_softc(dev);
95 static struct ata_chip_id ids[] =
96 {{ ATA_I82371FB, 0, 0, 2, ATA_WDMA2, "PIIX" },
97 { ATA_I82371SB, 0, 0, 2, ATA_WDMA2, "PIIX3" },
98 { ATA_I82371AB, 0, 0, 2, ATA_UDMA2, "PIIX4" },
99 { ATA_I82443MX, 0, 0, 2, ATA_UDMA2, "PIIX4" },
100 { ATA_I82451NX, 0, 0, 2, ATA_UDMA2, "PIIX4" },
101 { ATA_I82801AB, 0, 0, 2, ATA_UDMA2, "ICH0" },
102 { ATA_I82801AA, 0, 0, 2, ATA_UDMA4, "ICH" },
103 { ATA_I82372FB, 0, 0, 2, ATA_UDMA4, "ICH" },
104 { ATA_I82801BA, 0, 0, 2, ATA_UDMA5, "ICH2" },
105 { ATA_I82801BA_1, 0, 0, 2, ATA_UDMA5, "ICH2" },
106 { ATA_I82801CA, 0, 0, 2, ATA_UDMA5, "ICH3" },
107 { ATA_I82801CA_1, 0, 0, 2, ATA_UDMA5, "ICH3" },
108 { ATA_I82801DB, 0, 0, 2, ATA_UDMA5, "ICH4" },
109 { ATA_I82801DB_1, 0, 0, 2, ATA_UDMA5, "ICH4" },
110 { ATA_I82801EB, 0, 0, 2, ATA_UDMA5, "ICH5" },
111 { ATA_I82801EB_S1, 0, INTEL_ICH5, 2, ATA_SA150, "ICH5" },
112 { ATA_I82801EB_R1, 0, INTEL_ICH5, 2, ATA_SA150, "ICH5" },
113 { ATA_I6300ESB, 0, 0, 2, ATA_UDMA5, "6300ESB" },
114 { ATA_I6300ESB_S1, 0, INTEL_ICH5, 2, ATA_SA150, "6300ESB" },
115 { ATA_I6300ESB_R1, 0, INTEL_ICH5, 2, ATA_SA150, "6300ESB" },
116 { ATA_I82801FB, 0, 0, 2, ATA_UDMA5, "ICH6" },
117 { ATA_I82801FB_S1, 0, INTEL_AHCI, 0, ATA_SA150, "ICH6" },
118 { ATA_I82801FB_R1, 0, INTEL_AHCI, 0, ATA_SA150, "ICH6" },
119 { ATA_I82801FBM, 0, INTEL_AHCI, 0, ATA_SA150, "ICH6M" },
120 { ATA_I82801GB, 0, 0, 1, ATA_UDMA5, "ICH7" },
121 { ATA_I82801GB_S1, 0, INTEL_ICH7, 0, ATA_SA300, "ICH7" },
122 { ATA_I82801GB_R1, 0, INTEL_AHCI, 0, ATA_SA300, "ICH7" },
123 { ATA_I82801GB_AH, 0, INTEL_AHCI, 0, ATA_SA300, "ICH7" },
124 { ATA_I82801GBM_S1, 0, INTEL_ICH7, 0, ATA_SA150, "ICH7M" },
125 { ATA_I82801GBM_R1, 0, INTEL_AHCI, 0, ATA_SA150, "ICH7M" },
126 { ATA_I82801GBM_AH, 0, INTEL_AHCI, 0, ATA_SA150, "ICH7M" },
127 { ATA_I63XXESB2, 0, 0, 1, ATA_UDMA5, "63XXESB2" },
128 { ATA_I63XXESB2_S1, 0, 0, 0, ATA_SA300, "63XXESB2" },
129 { ATA_I63XXESB2_S2, 0, INTEL_AHCI, 0, ATA_SA300, "63XXESB2" },
130 { ATA_I63XXESB2_R1, 0, INTEL_AHCI, 0, ATA_SA300, "63XXESB2" },
131 { ATA_I63XXESB2_R2, 0, INTEL_AHCI, 0, ATA_SA300, "63XXESB2" },
132 { ATA_I82801HB_S1, 0, INTEL_6CH, 0, ATA_SA300, "ICH8" },
133 { ATA_I82801HB_S2, 0, INTEL_6CH2, 0, ATA_SA300, "ICH8" },
134 { ATA_I82801HB_R1, 0, INTEL_AHCI, 0, ATA_SA300, "ICH8" },
135 { ATA_I82801HB_AH4, 0, INTEL_AHCI, 0, ATA_SA300, "ICH8" },
136 { ATA_I82801HB_AH6, 0, INTEL_AHCI, 0, ATA_SA300, "ICH8" },
137 { ATA_I82801HBM, 0, 0, 1, ATA_UDMA5, "ICH8M" },
138 { ATA_I82801HBM_S1, 0, INTEL_6CH, 0, ATA_SA300, "ICH8M" },
139 { ATA_I82801HBM_S2, 0, INTEL_AHCI, 0, ATA_SA300, "ICH8M" },
140 { ATA_I82801HBM_S3, 0, INTEL_AHCI, 0, ATA_SA300, "ICH8M" },
141 { ATA_I82801IB_S1, 0, INTEL_6CH, 0, ATA_SA300, "ICH9" },
142 { ATA_I82801IB_S2, 0, INTEL_6CH2, 0, ATA_SA300, "ICH9" },
143 { ATA_I82801IB_AH2, 0, INTEL_AHCI, 0, ATA_SA300, "ICH9" },
144 { ATA_I82801IB_AH4, 0, INTEL_AHCI, 0, ATA_SA300, "ICH9" },
145 { ATA_I82801IB_AH6, 0, INTEL_AHCI, 0, ATA_SA300, "ICH9" },
146 { ATA_I82801IB_R1, 0, INTEL_AHCI, 0, ATA_SA300, "ICH9" },
147 { ATA_I82801JIB_S1, 0, INTEL_6CH, 0, ATA_SA300, "ICH10" },
148 { ATA_I82801JIB_AH, 0, INTEL_AHCI, 0, ATA_SA300, "ICH10" },
149 { ATA_I82801JIB_R1, 0, INTEL_AHCI, 0, ATA_SA300, "ICH10" },
150 { ATA_I82801JIB_S2, 0, INTEL_6CH2, 0, ATA_SA300, "ICH10" },
151 { ATA_I82801JD_S1, 0, INTEL_6CH, 0, ATA_SA300, "ICH10" },
152 { ATA_I82801JD_AH, 0, INTEL_AHCI, 0, ATA_SA300, "ICH10" },
153 { ATA_I82801JD_R1, 0, INTEL_AHCI, 0, ATA_SA300, "ICH10" },
154 { ATA_I82801JD_S2, 0, INTEL_6CH2, 0, ATA_SA300, "ICH10" },
155 { ATA_I82801JI_S1, 0, INTEL_6CH, 0, ATA_SA300, "ICH10" },
156 { ATA_I82801JI_AH, 0, INTEL_AHCI, 0, ATA_SA300, "ICH10" },
157 { ATA_I82801JI_R1, 0, INTEL_AHCI, 0, ATA_SA300, "ICH10" },
158 { ATA_I82801JI_S2, 0, INTEL_6CH2, 0, ATA_SA300, "ICH10" },
159 { ATA_5Series_S1, 0, INTEL_6CH, 0, ATA_SA300, "5 Series/3400 Series PCH" },
160 { ATA_5Series_S2, 0, INTEL_6CH2, 0, ATA_SA300, "5 Series/3400 Series PCH" },
161 { ATA_5Series_AH1, 0, INTEL_AHCI, 0, ATA_SA300, "5 Series/3400 Series PCH" },
162 { ATA_5Series_AH2, 0, INTEL_AHCI, 0, ATA_SA300, "5 Series/3400 Series PCH" },
163 { ATA_5Series_R1, 0, INTEL_AHCI, 0, ATA_SA300, "5 Series/3400 Series PCH" },
164 { ATA_5Series_S3, 0, INTEL_6CH2, 0, ATA_SA300, "5 Series/3400 Series PCH" },
165 { ATA_5Series_S4, 0, INTEL_6CH, 0, ATA_SA300, "5 Series/3400 Series PCH" },
166 { ATA_5Series_AH3, 0, INTEL_AHCI, 0, ATA_SA300, "5 Series/3400 Series PCH" },
167 { ATA_5Series_R2, 0, INTEL_AHCI, 0, ATA_SA300, "5 Series/3400 Series PCH" },
168 { ATA_5Series_S5, 0, INTEL_6CH2, 0, ATA_SA300, "5 Series/3400 Series PCH" },
169 { ATA_5Series_S6, 0, INTEL_6CH, 0, ATA_SA300, "5 Series/3400 Series PCH" },
170 { ATA_5Series_AH4, 0, INTEL_AHCI, 0, ATA_SA300, "5 Series/3400 Series PCH" },
171 { ATA_CPT_S1, 0, INTEL_6CH, 0, ATA_SA300, "Cougar Point" },
172 { ATA_CPT_S2, 0, INTEL_6CH, 0, ATA_SA300, "Cougar Point" },
173 { ATA_CPT_AH1, 0, INTEL_AHCI, 0, ATA_SA300, "Cougar Point" },
174 { ATA_CPT_AH2, 0, INTEL_AHCI, 0, ATA_SA300, "Cougar Point" },
175 { ATA_CPT_R1, 0, INTEL_AHCI, 0, ATA_SA300, "Cougar Point" },
176 { ATA_CPT_R2, 0, INTEL_AHCI, 0, ATA_SA300, "Cougar Point" },
177 { ATA_CPT_S3, 0, INTEL_6CH2, 0, ATA_SA300, "Cougar Point" },
178 { ATA_CPT_S4, 0, INTEL_6CH2, 0, ATA_SA300, "Cougar Point" },
179 { ATA_I31244, 0, 0, 2, ATA_SA150, "31244" },
180 { ATA_ISCH, 0, 0, 1, ATA_UDMA5, "SCH" },
181 { ATA_DH89XXCC, 0, INTEL_AHCI, 0, ATA_SA300, "DH89xxCC" },
182 { 0, 0, 0, 0, 0, 0}};
184 if (pci_get_vendor(dev) != ATA_INTEL_ID)
187 if (!(ctlr->chip = ata_match_chip(dev, ids)))
191 ctlr->chipinit = ata_intel_chipinit;
192 return (BUS_PROBE_DEFAULT);
196 ata_intel_chipinit(device_t dev)
198 struct ata_pci_controller *ctlr = device_get_softc(dev);
200 if (ata_setup_interrupt(dev, ata_generic_intr))
203 ctlr->chipset_data = NULL;
205 /* good old PIIX needs special treatment (not implemented) */
206 if (ctlr->chip->chipid == ATA_I82371FB) {
207 ctlr->setmode = ata_intel_old_setmode;
210 /* the intel 31244 needs special care if in DPA mode */
211 else if (ctlr->chip->chipid == ATA_I31244) {
212 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE) {
213 ctlr->r_type2 = SYS_RES_MEMORY;
214 ctlr->r_rid2 = PCIR_BAR(0);
215 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
220 ctlr->ch_attach = ata_intel_31244_ch_attach;
221 ctlr->ch_detach = ata_intel_31244_ch_detach;
222 ctlr->reset = ata_intel_31244_reset;
224 ctlr->setmode = ata_sata_setmode;
225 ctlr->getrev = ata_sata_getrev;
228 else if (ctlr->chip->chipid == ATA_ISCH) {
230 ctlr->ch_attach = ata_intel_ch_attach;
231 ctlr->ch_detach = ata_pci_ch_detach;
232 ctlr->setmode = ata_intel_sch_setmode;
234 /* non SATA intel chips goes here */
235 else if (ctlr->chip->max_dma < ATA_SA150) {
236 ctlr->channels = ctlr->chip->cfg2;
237 ctlr->ch_attach = ata_intel_ch_attach;
238 ctlr->ch_detach = ata_pci_ch_detach;
239 ctlr->setmode = ata_intel_new_setmode;
242 /* SATA parts can be either compat or AHCI */
244 /* force all ports active "the legacy way" */
245 pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f, 2);
247 ctlr->ch_attach = ata_intel_ch_attach;
248 ctlr->ch_detach = ata_pci_ch_detach;
249 ctlr->reset = ata_intel_reset;
252 * if we have AHCI capability and AHCI or RAID mode enabled
253 * in BIOS we try for AHCI mode
255 if ((ctlr->chip->cfg1 & INTEL_AHCI) &&
256 (pci_read_config(dev, 0x90, 1) & 0xc0) &&
257 (ata_ahci_chipinit(dev) != ENXIO))
260 /* BAR(5) may point to SATA interface registers */
261 if ((ctlr->chip->cfg1 & INTEL_ICH7)) {
262 ctlr->r_type2 = SYS_RES_MEMORY;
263 ctlr->r_rid2 = PCIR_BAR(5);
264 ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
265 &ctlr->r_rid2, RF_ACTIVE);
266 if (ctlr->r_res2 != NULL) {
267 /* Set SCRAE bit to enable registers access. */
268 pci_write_config(dev, 0x94,
269 pci_read_config(dev, 0x94, 4) | (1 << 9), 4);
270 /* Set Ports Implemented register bits. */
271 ATA_OUTL(ctlr->r_res2, 0x0C,
272 ATA_INL(ctlr->r_res2, 0x0C) | 0xf);
275 ctlr->r_type2 = SYS_RES_IOPORT;
276 ctlr->r_rid2 = PCIR_BAR(5);
277 ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
278 &ctlr->r_rid2, RF_ACTIVE);
280 if (ctlr->r_res2 != NULL ||
281 (ctlr->chip->cfg1 & INTEL_ICH5))
282 ctlr->getrev = ata_intel_sata_getrev;
283 ctlr->setmode = ata_sata_setmode;
289 ata_intel_ch_attach(device_t dev)
291 struct ata_pci_controller *ctlr;
292 struct ata_channel *ch;
296 /* setup the usual register normal pci style */
297 if (ata_pci_ch_attach(dev))
300 ctlr = device_get_softc(device_get_parent(dev));
301 ch = device_get_softc(dev);
303 /* if r_res2 is valid it points to SATA interface registers */
305 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
306 ch->r_io[ATA_IDX_ADDR].offset = 0x00;
307 ch->r_io[ATA_IDX_DATA].res = ctlr->r_res2;
308 ch->r_io[ATA_IDX_DATA].offset = 0x04;
311 ch->flags |= ATA_ALWAYS_DMASTAT;
312 if (ctlr->chip->max_dma >= ATA_SA150) {
313 smap = (u_char *)&ctlr->chipset_data + ch->unit * 2;
314 map = pci_read_config(device_get_parent(dev), 0x90, 1);
315 if (ctlr->chip->cfg1 & INTEL_ICH5) {
317 if ((map & 0x04) == 0) {
318 ch->flags |= ATA_SATA;
319 ch->flags |= ATA_NO_SLAVE;
320 smap[0] = (map & 0x01) ^ ch->unit;
322 } else if ((map & 0x02) == 0 && ch->unit == 0) {
323 ch->flags |= ATA_SATA;
324 smap[0] = (map & 0x01) ? 1 : 0;
325 smap[1] = (map & 0x01) ? 0 : 1;
326 } else if ((map & 0x02) != 0 && ch->unit == 1) {
327 ch->flags |= ATA_SATA;
328 smap[0] = (map & 0x01) ? 1 : 0;
329 smap[1] = (map & 0x01) ? 0 : 1;
331 } else if (ctlr->chip->cfg1 & INTEL_6CH2) {
332 ch->flags |= ATA_SATA;
333 ch->flags |= ATA_NO_SLAVE;
334 smap[0] = (ch->unit == 0) ? 4 : 5;
339 ch->flags |= ATA_SATA;
340 smap[0] = (ch->unit == 0) ? 0 : 1;
341 smap[1] = (ch->unit == 0) ? 2 : 3;
342 } else if (map == 0x02 && ch->unit == 0) {
343 ch->flags |= ATA_SATA;
346 } else if (map == 0x01 && ch->unit == 1) {
347 ch->flags |= ATA_SATA;
352 if (ch->flags & ATA_SATA) {
353 if ((ctlr->chip->cfg1 & INTEL_ICH5)) {
354 ch->flags |= ATA_PERIODIC_POLL;
355 ch->hw.status = ata_intel_sata_status;
356 ch->hw.pm_read = ata_intel_sata_cscr_read;
357 ch->hw.pm_write = ata_intel_sata_cscr_write;
358 } else if (ctlr->r_res2) {
359 ch->flags |= ATA_PERIODIC_POLL;
360 ch->hw.status = ata_intel_sata_status;
361 if ((ctlr->chip->cfg1 & INTEL_ICH7)) {
362 ch->hw.pm_read = ata_intel_sata_ahci_read;
363 ch->hw.pm_write = ata_intel_sata_ahci_write;
365 ch->hw.pm_read = ata_intel_sata_sidpr_read;
366 ch->hw.pm_write = ata_intel_sata_sidpr_write;
369 if (ch->hw.pm_write != NULL) {
370 ata_sata_scr_write(ch, 0,
371 ATA_SERROR, 0xffffffff);
372 if ((ch->flags & ATA_NO_SLAVE) == 0) {
373 ata_sata_scr_write(ch, 1,
374 ATA_SERROR, 0xffffffff);
378 ctlr->setmode = ata_intel_new_setmode;
379 } else if (ctlr->chip->chipid != ATA_ISCH)
380 ch->flags |= ATA_CHECKS_CABLE;
385 ata_intel_reset(device_t dev)
387 device_t parent = device_get_parent(dev);
388 struct ata_pci_controller *ctlr = device_get_softc(parent);
389 struct ata_channel *ch = device_get_softc(dev);
390 int mask, pmask, timeout, devs;
393 /* In combined mode, skip SATA stuff for PATA channel. */
394 if ((ch->flags & ATA_SATA) == 0)
395 return (ata_generic_reset(dev));
397 /* Do hard-reset on respective SATA ports. */
398 smap = (u_char *)&ctlr->chipset_data + ch->unit * 2;
400 if ((ch->flags & ATA_NO_SLAVE) == 0)
401 mask |= (1 << smap[1]);
402 pci_write_config(parent, 0x92,
403 pci_read_config(parent, 0x92, 2) & ~mask, 2);
405 pci_write_config(parent, 0x92,
406 pci_read_config(parent, 0x92, 2) | mask, 2);
408 /* Wait up to 1 sec for "connect well". */
409 if (ctlr->chip->cfg1 & (INTEL_6CH | INTEL_6CH2))
413 for (timeout = 0; timeout < 100 ; timeout++) {
414 if (((pci_read_config(parent, 0x92, 2) & pmask) == pmask) &&
415 (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
420 /* If any device found, do soft-reset. */
421 if (ch->hw.pm_read != NULL) {
422 devs = ata_sata_phy_reset(dev, 0, 2);
423 if ((ch->flags & ATA_NO_SLAVE) == 0)
424 devs += ata_sata_phy_reset(dev, 1, 2);
428 ata_generic_reset(dev);
434 ata_intel_old_setmode(device_t dev, int target, int mode)
436 device_t parent = device_get_parent(dev);
437 struct ata_pci_controller *ctlr = device_get_softc(parent);
439 mode = min(mode, ctlr->chip->max_dma);
444 ata_intel_new_setmode(device_t dev, int target, int mode)
446 device_t parent = device_get_parent(dev);
447 struct ata_pci_controller *ctlr = device_get_softc(parent);
448 struct ata_channel *ch = device_get_softc(dev);
449 int devno = (ch->unit << 1) + target;
451 u_int32_t reg40 = pci_read_config(parent, 0x40, 4);
452 u_int8_t reg44 = pci_read_config(parent, 0x44, 1);
453 u_int8_t reg48 = pci_read_config(parent, 0x48, 1);
454 u_int16_t reg4a = pci_read_config(parent, 0x4a, 2);
455 u_int16_t reg54 = pci_read_config(parent, 0x54, 2);
456 u_int32_t mask40 = 0, new40 = 0;
457 u_int8_t mask44 = 0, new44 = 0;
458 u_int8_t timings[] = { 0x00, 0x00, 0x10, 0x21, 0x23, 0x00, 0x21, 0x23 };
459 u_int8_t utimings[] = { 0x00, 0x01, 0x02, 0x01, 0x02, 0x01, 0x02 };
461 /* In combined mode, skip PATA stuff for SATA channel. */
462 if (ch->flags & ATA_SATA)
463 return (ata_sata_setmode(dev, target, mode));
465 mode = min(mode, ctlr->chip->max_dma);
466 if (ata_dma_check_80pin && mode > ATA_UDMA2 &&
467 !(reg54 & (0x10 << devno))) {
468 ata_print_cable(dev, "controller");
471 /* Enable/disable UDMA and set timings. */
472 if (mode >= ATA_UDMA0) {
473 pci_write_config(parent, 0x48, reg48 | (0x0001 << devno), 2);
474 pci_write_config(parent, 0x4a,
475 (reg4a & ~(0x3 << (devno << 2))) |
476 (utimings[mode & ATA_MODE_MASK] << (devno<<2)), 2);
479 pci_write_config(parent, 0x48, reg48 & ~(0x0001 << devno), 2);
480 pci_write_config(parent, 0x4a, (reg4a & ~(0x3 << (devno << 2))),2);
484 /* Set UDMA reference clock (33/66/133MHz). */
485 reg54 &= ~(0x1001 << devno);
486 if (mode >= ATA_UDMA5)
487 reg54 |= (0x1000 << devno);
488 else if (mode >= ATA_UDMA3)
489 reg54 |= (0x1 << devno);
490 pci_write_config(parent, 0x54, reg54, 2);
491 /* Allow PIO/WDMA timing controls. */
492 reg40 &= ~0x00ff00ff;
494 /* Set PIO/WDMA timings. */
497 new40 = timings[ata_mode2idx(piomode)] << 8;
500 new44 = ((timings[ata_mode2idx(piomode)] & 0x30) >> 2) |
501 (timings[ata_mode2idx(piomode)] & 0x03);
509 pci_write_config(parent, 0x40, (reg40 & ~mask40) | new40, 4);
510 pci_write_config(parent, 0x44, (reg44 & ~mask44) | new44, 1);
515 ata_intel_sch_setmode(device_t dev, int target, int mode)
517 device_t parent = device_get_parent(dev);
518 struct ata_pci_controller *ctlr = device_get_softc(parent);
519 u_int8_t dtim = 0x80 + (target << 2);
520 u_int32_t tim = pci_read_config(parent, dtim, 4);
523 mode = min(mode, ctlr->chip->max_dma);
524 if (mode >= ATA_UDMA0) {
527 tim |= ((mode & ATA_MODE_MASK) << 16);
529 } else if (mode >= ATA_WDMA0) {
532 tim |= ((mode & ATA_MODE_MASK) << 8);
533 piomode = (mode == ATA_WDMA0) ? ATA_PIO0 :
534 (mode == ATA_WDMA1) ? ATA_PIO3 : ATA_PIO4;
538 tim |= (piomode & 0x7);
539 pci_write_config(parent, dtim, tim, 4);
544 ata_intel_sata_getrev(device_t dev, int target)
546 struct ata_channel *ch = device_get_softc(dev);
549 if (ata_sata_scr_read(ch, target, ATA_SSTATUS, &status) == 0)
550 return ((status & 0x0f0) >> 4);
555 ata_intel_sata_status(device_t dev)
557 struct ata_channel *ch = device_get_softc(dev);
559 ata_sata_phy_check_events(dev, 0);
560 if ((ch->flags & ATA_NO_SLAVE) == 0)
561 ata_sata_phy_check_events(dev, 1);
563 return ata_pci_status(dev);
567 ata_intel_sata_ahci_read(device_t dev, int port, int reg, u_int32_t *result)
569 struct ata_pci_controller *ctlr;
570 struct ata_channel *ch;
575 parent = device_get_parent(dev);
576 ctlr = device_get_softc(parent);
577 ch = device_get_softc(dev);
578 port = (port == 1) ? 1 : 0;
579 smap = (u_char *)&ctlr->chipset_data + ch->unit * 2;
580 offset = 0x100 + smap[port] * 0x80;
594 *result = ATA_INL(ctlr->r_res2, offset + reg);
599 ata_intel_sata_cscr_read(device_t dev, int port, int reg, u_int32_t *result)
601 struct ata_pci_controller *ctlr;
602 struct ata_channel *ch;
606 parent = device_get_parent(dev);
607 ctlr = device_get_softc(parent);
608 ch = device_get_softc(dev);
609 smap = (u_char *)&ctlr->chipset_data + ch->unit * 2;
610 port = (port == 1) ? 1 : 0;
624 pci_write_config(parent, 0xa0,
625 0x50 + smap[port] * 0x10 + reg * 4, 4);
626 *result = pci_read_config(parent, 0xa4, 4);
631 ata_intel_sata_sidpr_read(device_t dev, int port, int reg, u_int32_t *result)
633 struct ata_pci_controller *ctlr;
634 struct ata_channel *ch;
637 parent = device_get_parent(dev);
638 ctlr = device_get_softc(parent);
639 ch = device_get_softc(dev);
640 port = (port == 1) ? 1 : 0;
654 ATA_IDX_OUTL(ch, ATA_IDX_ADDR, ((ch->unit * 2 + port) << 8) + reg);
655 *result = ATA_IDX_INL(ch, ATA_IDX_DATA);
660 ata_intel_sata_ahci_write(device_t dev, int port, int reg, u_int32_t value)
662 struct ata_pci_controller *ctlr;
663 struct ata_channel *ch;
668 parent = device_get_parent(dev);
669 ctlr = device_get_softc(parent);
670 ch = device_get_softc(dev);
671 port = (port == 1) ? 1 : 0;
672 smap = (u_char *)&ctlr->chipset_data + ch->unit * 2;
673 offset = 0x100 + smap[port] * 0x80;
687 ATA_OUTL(ctlr->r_res2, offset + reg, value);
692 ata_intel_sata_cscr_write(device_t dev, int port, int reg, u_int32_t value)
694 struct ata_pci_controller *ctlr;
695 struct ata_channel *ch;
699 parent = device_get_parent(dev);
700 ctlr = device_get_softc(parent);
701 ch = device_get_softc(dev);
702 smap = (u_char *)&ctlr->chipset_data + ch->unit * 2;
703 port = (port == 1) ? 1 : 0;
717 pci_write_config(parent, 0xa0,
718 0x50 + smap[port] * 0x10 + reg * 4, 4);
719 pci_write_config(parent, 0xa4, value, 4);
724 ata_intel_sata_sidpr_write(device_t dev, int port, int reg, u_int32_t value)
726 struct ata_pci_controller *ctlr;
727 struct ata_channel *ch;
730 parent = device_get_parent(dev);
731 ctlr = device_get_softc(parent);
732 ch = device_get_softc(dev);
733 port = (port == 1) ? 1 : 0;
747 ATA_IDX_OUTL(ch, ATA_IDX_ADDR, ((ch->unit * 2 + port) << 8) + reg);
748 ATA_IDX_OUTL(ch, ATA_IDX_DATA, value);
753 ata_intel_31244_ch_attach(device_t dev)
755 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
756 struct ata_channel *ch = device_get_softc(dev);
760 ata_pci_dmainit(dev);
762 ch_offset = 0x200 + ch->unit * 0x200;
764 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
765 ch->r_io[i].res = ctlr->r_res2;
767 /* setup ATA registers */
768 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
769 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x06;
770 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
771 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
772 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
773 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
774 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
775 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1d;
776 ch->r_io[ATA_ERROR].offset = ch_offset + 0x04;
777 ch->r_io[ATA_STATUS].offset = ch_offset + 0x1c;
778 ch->r_io[ATA_ALTSTAT].offset = ch_offset + 0x28;
779 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x29;
781 /* setup DMA registers */
782 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x100;
783 ch->r_io[ATA_SERROR].offset = ch_offset + 0x104;
784 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x108;
786 /* setup SATA registers */
787 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x70;
788 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x72;
789 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x74;
791 ch->flags |= ATA_NO_SLAVE;
792 ch->flags |= ATA_SATA;
794 ch->hw.status = ata_intel_31244_status;
795 ch->hw.tf_write = ata_intel_31244_tf_write;
797 /* enable PHY state change interrupt */
798 ATA_OUTL(ctlr->r_res2, 0x4,
799 ATA_INL(ctlr->r_res2, 0x04) | (0x01 << (ch->unit << 3)));
804 ata_intel_31244_ch_detach(device_t dev)
807 ata_pci_dmafini(dev);
812 ata_intel_31244_status(device_t dev)
814 /* do we have any PHY events ? */
815 ata_sata_phy_check_events(dev, -1);
817 /* any drive action to take care of ? */
818 return ata_pci_status(dev);
822 ata_intel_31244_tf_write(struct ata_request *request)
824 struct ata_channel *ch = device_get_softc(request->parent);
826 struct ata_device *atadev = device_get_softc(request->dev);
829 if (request->flags & ATA_R_48BIT) {
830 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
831 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
832 ATA_IDX_OUTW(ch, ATA_SECTOR, ((request->u.ata.lba >> 16) & 0xff00) |
833 (request->u.ata.lba & 0x00ff));
834 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((request->u.ata.lba >> 24) & 0xff00) |
835 ((request->u.ata.lba >> 8) & 0x00ff));
836 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((request->u.ata.lba >> 32) & 0xff00) |
837 ((request->u.ata.lba >> 16) & 0x00ff));
838 ATA_IDX_OUTW(ch, ATA_DRIVE, ATA_D_LBA | ATA_DEV(request->unit));
841 ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature);
842 ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count);
844 if (atadev->flags & ATA_D_USE_CHS) {
847 if (atadev->param.atavalid & ATA_FLAG_54_58) {
848 heads = atadev->param.current_heads;
849 sectors = atadev->param.current_sectors;
852 heads = atadev->param.heads;
853 sectors = atadev->param.sectors;
855 ATA_IDX_OUTB(ch, ATA_SECTOR, (request->u.ata.lba % sectors)+1);
856 ATA_IDX_OUTB(ch, ATA_CYL_LSB,
857 (request->u.ata.lba / (sectors * heads)));
858 ATA_IDX_OUTB(ch, ATA_CYL_MSB,
859 (request->u.ata.lba / (sectors * heads)) >> 8);
860 ATA_IDX_OUTB(ch, ATA_DRIVE, ATA_D_IBM | ATA_DEV(request->unit) |
861 (((request->u.ata.lba% (sectors * heads)) /
866 ATA_IDX_OUTB(ch, ATA_SECTOR, request->u.ata.lba);
867 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->u.ata.lba >> 8);
868 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->u.ata.lba >> 16);
869 ATA_IDX_OUTB(ch, ATA_DRIVE,
870 ATA_D_IBM | ATA_D_LBA | ATA_DEV(request->unit) |
871 ((request->u.ata.lba >> 24) & 0x0f));
879 ata_intel_31244_reset(device_t dev)
881 struct ata_channel *ch = device_get_softc(dev);
883 if (ata_sata_phy_reset(dev, -1, 1))
884 ata_generic_reset(dev);
889 ATA_DECLARE_DRIVER(ata_intel);
890 MODULE_DEPEND(ata_intel, ata_ahci, 1, 1, 1);