2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/module.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
38 #include <sys/endian.h>
39 #include <sys/malloc.h>
41 #include <sys/mutex.h>
43 #include <sys/taskqueue.h>
45 #include <machine/stdarg.h>
46 #include <machine/resource.h>
47 #include <machine/bus.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcireg.h>
51 #include <dev/ata/ata-all.h>
52 #include <dev/ata/ata-pci.h>
55 /* local prototypes */
56 static int ata_intel_chipinit(device_t dev);
57 static int ata_intel_chipdeinit(device_t dev);
58 static int ata_intel_ch_attach(device_t dev);
59 static void ata_intel_reset(device_t dev);
60 static int ata_intel_old_setmode(device_t dev, int target, int mode);
61 static int ata_intel_new_setmode(device_t dev, int target, int mode);
62 static int ata_intel_sch_setmode(device_t dev, int target, int mode);
63 static int ata_intel_sata_getrev(device_t dev, int target);
64 static int ata_intel_sata_status(device_t dev);
65 static int ata_intel_sata_ahci_read(device_t dev, int port,
66 int reg, u_int32_t *result);
67 static int ata_intel_sata_cscr_read(device_t dev, int port,
68 int reg, u_int32_t *result);
69 static int ata_intel_sata_sidpr_read(device_t dev, int port,
70 int reg, u_int32_t *result);
71 static int ata_intel_sata_ahci_write(device_t dev, int port,
72 int reg, u_int32_t result);
73 static int ata_intel_sata_cscr_write(device_t dev, int port,
74 int reg, u_int32_t result);
75 static int ata_intel_sata_sidpr_write(device_t dev, int port,
76 int reg, u_int32_t result);
77 static int ata_intel_sata_sidpr_test(device_t dev);
78 static int ata_intel_31244_ch_attach(device_t dev);
79 static int ata_intel_31244_ch_detach(device_t dev);
80 static int ata_intel_31244_status(device_t dev);
81 static void ata_intel_31244_tf_write(struct ata_request *request);
82 static void ata_intel_31244_reset(device_t dev);
90 struct ata_intel_data {
95 #define ATA_INTEL_SMAP(ctlr, ch) \
96 &((struct ata_intel_data *)((ctlr)->chipset_data))->smap[(ch)->unit * 2]
97 #define ATA_INTEL_LOCK(ctlr) \
98 mtx_lock(&((struct ata_intel_data *)((ctlr)->chipset_data))->lock)
99 #define ATA_INTEL_UNLOCK(ctlr) \
100 mtx_unlock(&((struct ata_intel_data *)((ctlr)->chipset_data))->lock)
103 * Intel chipset support functions
106 ata_intel_probe(device_t dev)
108 struct ata_pci_controller *ctlr = device_get_softc(dev);
109 static const struct ata_chip_id ids[] =
110 {{ ATA_I82371FB, 0, 0, 2, ATA_WDMA2, "PIIX" },
111 { ATA_I82371SB, 0, 0, 2, ATA_WDMA2, "PIIX3" },
112 { ATA_I82371AB, 0, 0, 2, ATA_UDMA2, "PIIX4" },
113 { ATA_I82443MX, 0, 0, 2, ATA_UDMA2, "PIIX4" },
114 { ATA_I82451NX, 0, 0, 2, ATA_UDMA2, "PIIX4" },
115 { ATA_I82801AB, 0, 0, 2, ATA_UDMA2, "ICH0" },
116 { ATA_I82801AA, 0, 0, 2, ATA_UDMA4, "ICH" },
117 { ATA_I82372FB, 0, 0, 2, ATA_UDMA4, "ICH" },
118 { ATA_I82801BA, 0, 0, 2, ATA_UDMA5, "ICH2" },
119 { ATA_I82801BA_1, 0, 0, 2, ATA_UDMA5, "ICH2" },
120 { ATA_I82801CA, 0, 0, 2, ATA_UDMA5, "ICH3" },
121 { ATA_I82801CA_1, 0, 0, 2, ATA_UDMA5, "ICH3" },
122 { ATA_I82801DB, 0, 0, 2, ATA_UDMA5, "ICH4" },
123 { ATA_I82801DB_1, 0, 0, 2, ATA_UDMA5, "ICH4" },
124 { ATA_I82801EB, 0, 0, 2, ATA_UDMA5, "ICH5" },
125 { ATA_I82801EB_S1, 0, INTEL_ICH5, 2, ATA_SA150, "ICH5" },
126 { ATA_I82801EB_R1, 0, INTEL_ICH5, 2, ATA_SA150, "ICH5" },
127 { ATA_I6300ESB, 0, 0, 2, ATA_UDMA5, "6300ESB" },
128 { ATA_I6300ESB_S1, 0, INTEL_ICH5, 2, ATA_SA150, "6300ESB" },
129 { ATA_I6300ESB_R1, 0, INTEL_ICH5, 2, ATA_SA150, "6300ESB" },
130 { ATA_I82801FB, 0, 0, 2, ATA_UDMA5, "ICH6" },
131 { ATA_I82801FB_S1, 0, 0, 0, ATA_SA150, "ICH6" },
132 { ATA_I82801FB_R1, 0, 0, 0, ATA_SA150, "ICH6" },
133 { ATA_I82801FBM, 0, 0, 0, ATA_SA150, "ICH6M" },
134 { ATA_I82801GB, 0, 0, 1, ATA_UDMA5, "ICH7" },
135 { ATA_I82801GB_S1, 0, INTEL_ICH7, 0, ATA_SA300, "ICH7" },
136 { ATA_I82801GBM_S1, 0, INTEL_ICH7, 0, ATA_SA150, "ICH7M" },
137 { ATA_I63XXESB2, 0, 0, 1, ATA_UDMA5, "63XXESB2" },
138 { ATA_I63XXESB2_S1, 0, 0, 0, ATA_SA300, "63XXESB2" },
139 { ATA_I82801HB_S1, 0, INTEL_6CH, 0, ATA_SA300, "ICH8" },
140 { ATA_I82801HB_S2, 0, INTEL_6CH2, 0, ATA_SA300, "ICH8" },
141 { ATA_I82801HBM, 0, 0, 1, ATA_UDMA5, "ICH8M" },
142 { ATA_I82801HBM_S1, 0, INTEL_6CH, 0, ATA_SA300, "ICH8M" },
143 { ATA_I82801IB_S1, 0, INTEL_6CH, 0, ATA_SA300, "ICH9" },
144 { ATA_I82801IB_S2, 0, INTEL_6CH2, 0, ATA_SA300, "ICH9" },
145 { ATA_I82801IB_S3, 0, INTEL_6CH2, 0, ATA_SA300, "ICH9" },
146 { ATA_I82801IBM_S1, 0, INTEL_6CH2, 0, ATA_SA300, "ICH9M" },
147 { ATA_I82801IBM_S2, 0, INTEL_6CH2, 0, ATA_SA300, "ICH9M" },
148 { ATA_I82801JIB_S1, 0, INTEL_6CH, 0, ATA_SA300, "ICH10" },
149 { ATA_I82801JIB_S2, 0, INTEL_6CH2, 0, ATA_SA300, "ICH10" },
150 { ATA_I82801JD_S1, 0, INTEL_6CH, 0, ATA_SA300, "ICH10" },
151 { ATA_I82801JD_S2, 0, INTEL_6CH2, 0, ATA_SA300, "ICH10" },
152 { ATA_I82801JI_S1, 0, INTEL_6CH, 0, ATA_SA300, "ICH10" },
153 { ATA_I82801JI_S2, 0, INTEL_6CH2, 0, ATA_SA300, "ICH10" },
154 { ATA_IBP_S1, 0, INTEL_6CH, 0, ATA_SA300, "Ibex Peak" },
155 { ATA_IBP_S2, 0, INTEL_6CH2, 0, ATA_SA300, "Ibex Peak" },
156 { ATA_IBP_S3, 0, INTEL_6CH2, 0, ATA_SA300, "Ibex Peak" },
157 { ATA_IBP_S4, 0, INTEL_6CH, 0, ATA_SA300, "Ibex Peak-M" },
158 { ATA_IBP_S5, 0, INTEL_6CH2, 0, ATA_SA300, "Ibex Peak-M" },
159 { ATA_IBP_S6, 0, INTEL_6CH, 0, ATA_SA300, "Ibex Peak-M" },
160 { ATA_CPT_S1, 0, INTEL_6CH, 0, ATA_SA600, "Cougar Point" },
161 { ATA_CPT_S2, 0, INTEL_6CH, 0, ATA_SA600, "Cougar Point" },
162 { ATA_CPT_S3, 0, INTEL_6CH2, 0, ATA_SA300, "Cougar Point" },
163 { ATA_CPT_S4, 0, INTEL_6CH2, 0, ATA_SA300, "Cougar Point" },
164 { ATA_PBG_S1, 0, INTEL_6CH, 0, ATA_SA600, "Patsburg" },
165 { ATA_PBG_S2, 0, INTEL_6CH2, 0, ATA_SA300, "Patsburg" },
166 { ATA_PPT_S1, 0, INTEL_6CH, 0, ATA_SA600, "Panther Point" },
167 { ATA_PPT_S2, 0, INTEL_6CH, 0, ATA_SA600, "Panther Point" },
168 { ATA_PPT_S3, 0, INTEL_6CH2, 0, ATA_SA300, "Panther Point" },
169 { ATA_PPT_S4, 0, INTEL_6CH2, 0, ATA_SA300, "Panther Point" },
170 { ATA_AVOTON_S1, 0, INTEL_6CH, 0, ATA_SA600, "Avoton" },
171 { ATA_AVOTON_S2, 0, INTEL_6CH, 0, ATA_SA600, "Avoton" },
172 { ATA_AVOTON_S3, 0, INTEL_6CH2, 0, ATA_SA300, "Avoton" },
173 { ATA_AVOTON_S4, 0, INTEL_6CH2, 0, ATA_SA300, "Avoton" },
174 { ATA_LPT_S1, 0, INTEL_6CH, 0, ATA_SA600, "Lynx Point" },
175 { ATA_LPT_S2, 0, INTEL_6CH, 0, ATA_SA600, "Lynx Point" },
176 { ATA_LPT_S3, 0, INTEL_6CH2, 0, ATA_SA600, "Lynx Point" },
177 { ATA_LPT_S4, 0, INTEL_6CH2, 0, ATA_SA600, "Lynx Point" },
178 { ATA_WCPT_S1, 0, INTEL_6CH, 0, ATA_SA600, "Wildcat Point" },
179 { ATA_WCPT_S2, 0, INTEL_6CH, 0, ATA_SA600, "Wildcat Point" },
180 { ATA_WCPT_S3, 0, INTEL_6CH2, 0, ATA_SA600, "Wildcat Point" },
181 { ATA_WCPT_S4, 0, INTEL_6CH2, 0, ATA_SA600, "Wildcat Point" },
182 { ATA_WELLS_S1, 0, INTEL_6CH, 0, ATA_SA600, "Wellsburg" },
183 { ATA_WELLS_S2, 0, INTEL_6CH2, 0, ATA_SA600, "Wellsburg" },
184 { ATA_WELLS_S3, 0, INTEL_6CH, 0, ATA_SA600, "Wellsburg" },
185 { ATA_WELLS_S4, 0, INTEL_6CH2, 0, ATA_SA600, "Wellsburg" },
186 { ATA_LPTLP_S1, 0, INTEL_6CH, 0, ATA_SA600, "Lynx Point-LP" },
187 { ATA_LPTLP_S2, 0, INTEL_6CH, 0, ATA_SA600, "Lynx Point-LP" },
188 { ATA_LPTLP_S3, 0, INTEL_6CH2, 0, ATA_SA300, "Lynx Point-LP" },
189 { ATA_LPTLP_S4, 0, INTEL_6CH2, 0, ATA_SA300, "Lynx Point-LP" },
190 { ATA_I31244, 0, 0, 2, ATA_SA150, "31244" },
191 { ATA_ISCH, 0, 0, 1, ATA_UDMA5, "SCH" },
192 { ATA_COLETOCRK_S1, 0, INTEL_6CH2, 0, ATA_SA300, "COLETOCRK" },
193 { ATA_COLETOCRK_S2, 0, INTEL_6CH2, 0, ATA_SA300, "COLETOCRK" },
194 { 0, 0, 0, 0, 0, 0}};
196 if (pci_get_vendor(dev) != ATA_INTEL_ID)
199 if (!(ctlr->chip = ata_match_chip(dev, ids)))
203 ctlr->chipinit = ata_intel_chipinit;
204 ctlr->chipdeinit = ata_intel_chipdeinit;
205 return (BUS_PROBE_LOW_PRIORITY);
209 ata_intel_chipinit(device_t dev)
211 struct ata_pci_controller *ctlr = device_get_softc(dev);
212 struct ata_intel_data *data;
214 if (ata_setup_interrupt(dev, ata_generic_intr))
217 data = malloc(sizeof(struct ata_intel_data), M_ATAPCI, M_WAITOK | M_ZERO);
218 mtx_init(&data->lock, "Intel SATA lock", NULL, MTX_DEF);
219 ctlr->chipset_data = (void *)data;
221 /* good old PIIX needs special treatment (not implemented) */
222 if (ctlr->chip->chipid == ATA_I82371FB) {
223 ctlr->setmode = ata_intel_old_setmode;
226 /* the intel 31244 needs special care if in DPA mode */
227 else if (ctlr->chip->chipid == ATA_I31244) {
228 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE) {
229 ctlr->r_type2 = SYS_RES_MEMORY;
230 ctlr->r_rid2 = PCIR_BAR(0);
231 if (!(ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
236 ctlr->ch_attach = ata_intel_31244_ch_attach;
237 ctlr->ch_detach = ata_intel_31244_ch_detach;
238 ctlr->reset = ata_intel_31244_reset;
240 ctlr->setmode = ata_sata_setmode;
241 ctlr->getrev = ata_sata_getrev;
244 else if (ctlr->chip->chipid == ATA_ISCH) {
246 ctlr->ch_attach = ata_intel_ch_attach;
247 ctlr->ch_detach = ata_pci_ch_detach;
248 ctlr->setmode = ata_intel_sch_setmode;
250 /* non SATA intel chips goes here */
251 else if (ctlr->chip->max_dma < ATA_SA150) {
252 ctlr->channels = ctlr->chip->cfg2;
253 ctlr->ch_attach = ata_intel_ch_attach;
254 ctlr->ch_detach = ata_pci_ch_detach;
255 ctlr->setmode = ata_intel_new_setmode;
258 /* SATA parts can be either compat or AHCI */
260 /* force all ports active "the legacy way" */
261 pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f, 2);
263 ctlr->ch_attach = ata_intel_ch_attach;
264 ctlr->ch_detach = ata_pci_ch_detach;
265 ctlr->reset = ata_intel_reset;
267 /* BAR(5) may point to SATA interface registers */
268 if ((ctlr->chip->cfg1 & INTEL_ICH7)) {
269 ctlr->r_type2 = SYS_RES_MEMORY;
270 ctlr->r_rid2 = PCIR_BAR(5);
271 ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
272 &ctlr->r_rid2, RF_ACTIVE);
273 if (ctlr->r_res2 != NULL) {
274 /* Set SCRAE bit to enable registers access. */
275 pci_write_config(dev, 0x94,
276 pci_read_config(dev, 0x94, 4) | (1 << 9), 4);
277 /* Set Ports Implemented register bits. */
278 ATA_OUTL(ctlr->r_res2, 0x0C,
279 ATA_INL(ctlr->r_res2, 0x0C) | 0xf);
281 /* Skip BAR(5) on ICH8M Apples, system locks up on access. */
282 } else if (ctlr->chip->chipid != ATA_I82801HBM_S1 ||
283 pci_get_subvendor(dev) != 0x106b) {
284 ctlr->r_type2 = SYS_RES_IOPORT;
285 ctlr->r_rid2 = PCIR_BAR(5);
286 ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
287 &ctlr->r_rid2, RF_ACTIVE);
289 if (ctlr->r_res2 != NULL ||
290 (ctlr->chip->cfg1 & INTEL_ICH5))
291 ctlr->getrev = ata_intel_sata_getrev;
292 ctlr->setmode = ata_sata_setmode;
298 ata_intel_chipdeinit(device_t dev)
300 struct ata_pci_controller *ctlr = device_get_softc(dev);
301 struct ata_intel_data *data;
303 data = ctlr->chipset_data;
304 mtx_destroy(&data->lock);
305 free(data, M_ATAPCI);
306 ctlr->chipset_data = NULL;
311 ata_intel_ch_attach(device_t dev)
313 struct ata_pci_controller *ctlr;
314 struct ata_channel *ch;
318 /* setup the usual register normal pci style */
319 if (ata_pci_ch_attach(dev))
322 ctlr = device_get_softc(device_get_parent(dev));
323 ch = device_get_softc(dev);
325 /* if r_res2 is valid it points to SATA interface registers */
327 ch->r_io[ATA_IDX_ADDR].res = ctlr->r_res2;
328 ch->r_io[ATA_IDX_ADDR].offset = 0x00;
329 ch->r_io[ATA_IDX_DATA].res = ctlr->r_res2;
330 ch->r_io[ATA_IDX_DATA].offset = 0x04;
333 ch->flags |= ATA_ALWAYS_DMASTAT;
334 if (ctlr->chip->max_dma >= ATA_SA150) {
335 smap = ATA_INTEL_SMAP(ctlr, ch);
336 map = pci_read_config(device_get_parent(dev), 0x90, 1);
337 if (ctlr->chip->cfg1 & INTEL_ICH5) {
339 if ((map & 0x04) == 0) {
340 ch->flags |= ATA_SATA;
341 ch->flags |= ATA_NO_SLAVE;
342 smap[0] = (map & 0x01) ^ ch->unit;
344 } else if ((map & 0x02) == 0 && ch->unit == 0) {
345 ch->flags |= ATA_SATA;
346 smap[0] = (map & 0x01) ? 1 : 0;
347 smap[1] = (map & 0x01) ? 0 : 1;
348 } else if ((map & 0x02) != 0 && ch->unit == 1) {
349 ch->flags |= ATA_SATA;
350 smap[0] = (map & 0x01) ? 1 : 0;
351 smap[1] = (map & 0x01) ? 0 : 1;
353 } else if (ctlr->chip->cfg1 & INTEL_6CH2) {
354 ch->flags |= ATA_SATA;
355 ch->flags |= ATA_NO_SLAVE;
356 smap[0] = (ch->unit == 0) ? 0 : 1;
361 ch->flags |= ATA_SATA;
362 smap[0] = (ch->unit == 0) ? 0 : 1;
363 smap[1] = (ch->unit == 0) ? 2 : 3;
364 } else if (map == 0x02 && ch->unit == 0) {
365 ch->flags |= ATA_SATA;
368 } else if (map == 0x01 && ch->unit == 1) {
369 ch->flags |= ATA_SATA;
374 if (ch->flags & ATA_SATA) {
375 if ((ctlr->chip->cfg1 & INTEL_ICH5)) {
376 ch->hw.pm_read = ata_intel_sata_cscr_read;
377 ch->hw.pm_write = ata_intel_sata_cscr_write;
378 } else if (ctlr->r_res2) {
379 if ((ctlr->chip->cfg1 & INTEL_ICH7)) {
380 ch->hw.pm_read = ata_intel_sata_ahci_read;
381 ch->hw.pm_write = ata_intel_sata_ahci_write;
382 } else if (ata_intel_sata_sidpr_test(dev)) {
383 ch->hw.pm_read = ata_intel_sata_sidpr_read;
384 ch->hw.pm_write = ata_intel_sata_sidpr_write;
387 if (ch->hw.pm_write != NULL) {
388 ch->flags |= ATA_PERIODIC_POLL;
389 ch->hw.status = ata_intel_sata_status;
390 ata_sata_scr_write(ch, 0,
391 ATA_SERROR, 0xffffffff);
392 if ((ch->flags & ATA_NO_SLAVE) == 0) {
393 ata_sata_scr_write(ch, 1,
394 ATA_SERROR, 0xffffffff);
398 ctlr->setmode = ata_intel_new_setmode;
399 if (ctlr->chip->max_dma >= ATA_SA600)
400 ch->flags |= ATA_USE_16BIT;
401 } else if (ctlr->chip->chipid != ATA_ISCH)
402 ch->flags |= ATA_CHECKS_CABLE;
407 ata_intel_reset(device_t dev)
409 device_t parent = device_get_parent(dev);
410 struct ata_pci_controller *ctlr = device_get_softc(parent);
411 struct ata_channel *ch = device_get_softc(dev);
412 int mask, pshift, timeout, devs;
416 /* In combined mode, skip SATA stuff for PATA channel. */
417 if ((ch->flags & ATA_SATA) == 0)
418 return (ata_generic_reset(dev));
420 /* Do hard-reset on respective SATA ports. */
421 smap = ATA_INTEL_SMAP(ctlr, ch);
423 if ((ch->flags & ATA_NO_SLAVE) == 0)
424 mask |= (1 << smap[1]);
425 pci_write_config(parent, 0x92,
426 pci_read_config(parent, 0x92, 2) & ~mask, 2);
428 pci_write_config(parent, 0x92,
429 pci_read_config(parent, 0x92, 2) | mask, 2);
431 /* Wait up to 1 sec for "connect well". */
432 if (ctlr->chip->cfg1 & (INTEL_6CH | INTEL_6CH2))
436 for (timeout = 0; timeout < 100 ; timeout++) {
437 pcs = (pci_read_config(parent, 0x92, 2) >> pshift) & mask;
438 if ((pcs == mask) && (ATA_IDX_INB(ch, ATA_STATUS) != 0xff))
444 device_printf(dev, "SATA reset: ports status=0x%02x\n", pcs);
445 /* If any device found, do soft-reset. */
446 if (ch->hw.pm_read != NULL) {
447 devs = ata_sata_phy_reset(dev, 0, 2) ? ATA_ATA_MASTER : 0;
448 if ((ch->flags & ATA_NO_SLAVE) == 0)
449 devs |= ata_sata_phy_reset(dev, 1, 2) ?
452 devs = (pcs & (1 << smap[0])) ? ATA_ATA_MASTER : 0;
453 if ((ch->flags & ATA_NO_SLAVE) == 0)
454 devs |= (pcs & (1 << smap[1])) ?
458 ata_generic_reset(dev);
459 /* Reset may give fake slave when only ATAPI master present. */
460 ch->devices &= (devs | (devs * ATA_ATAPI_MASTER));
466 ata_intel_old_setmode(device_t dev, int target, int mode)
468 device_t parent = device_get_parent(dev);
469 struct ata_pci_controller *ctlr = device_get_softc(parent);
471 mode = min(mode, ctlr->chip->max_dma);
476 ata_intel_new_setmode(device_t dev, int target, int mode)
478 device_t parent = device_get_parent(dev);
479 struct ata_pci_controller *ctlr = device_get_softc(parent);
480 struct ata_channel *ch = device_get_softc(dev);
481 int devno = (ch->unit << 1) + target;
483 u_int32_t reg40 = pci_read_config(parent, 0x40, 4);
484 u_int8_t reg44 = pci_read_config(parent, 0x44, 1);
485 u_int8_t reg48 = pci_read_config(parent, 0x48, 1);
486 u_int16_t reg4a = pci_read_config(parent, 0x4a, 2);
487 u_int16_t reg54 = pci_read_config(parent, 0x54, 2);
488 u_int32_t mask40 = 0, new40 = 0;
489 u_int8_t mask44 = 0, new44 = 0;
490 static const uint8_t timings[] =
491 { 0x00, 0x00, 0x10, 0x21, 0x23, 0x00, 0x21, 0x23 };
492 static const uint8_t utimings[] =
493 { 0x00, 0x01, 0x02, 0x01, 0x02, 0x01, 0x02 };
495 /* In combined mode, skip PATA stuff for SATA channel. */
496 if (ch->flags & ATA_SATA)
497 return (ata_sata_setmode(dev, target, mode));
499 mode = min(mode, ctlr->chip->max_dma);
500 if (ata_dma_check_80pin && mode > ATA_UDMA2 &&
501 !(reg54 & (0x10 << devno))) {
502 ata_print_cable(dev, "controller");
505 /* Enable/disable UDMA and set timings. */
506 if (mode >= ATA_UDMA0) {
507 pci_write_config(parent, 0x48, reg48 | (0x0001 << devno), 2);
508 pci_write_config(parent, 0x4a,
509 (reg4a & ~(0x3 << (devno << 2))) |
510 (utimings[mode & ATA_MODE_MASK] << (devno<<2)), 2);
513 pci_write_config(parent, 0x48, reg48 & ~(0x0001 << devno), 2);
514 pci_write_config(parent, 0x4a, (reg4a & ~(0x3 << (devno << 2))),2);
518 /* Set UDMA reference clock (33/66/133MHz). */
519 reg54 &= ~(0x1001 << devno);
520 if (mode >= ATA_UDMA5)
521 reg54 |= (0x1000 << devno);
522 else if (mode >= ATA_UDMA3)
523 reg54 |= (0x1 << devno);
524 pci_write_config(parent, 0x54, reg54, 2);
525 /* Allow PIO/WDMA timing controls. */
526 reg40 &= ~0x00ff00ff;
528 /* Set PIO/WDMA timings. */
531 new40 = timings[ata_mode2idx(piomode)] << 8;
534 new44 = ((timings[ata_mode2idx(piomode)] & 0x30) >> 2) |
535 (timings[ata_mode2idx(piomode)] & 0x03);
543 pci_write_config(parent, 0x40, (reg40 & ~mask40) | new40, 4);
544 pci_write_config(parent, 0x44, (reg44 & ~mask44) | new44, 1);
549 ata_intel_sch_setmode(device_t dev, int target, int mode)
551 device_t parent = device_get_parent(dev);
552 struct ata_pci_controller *ctlr = device_get_softc(parent);
553 u_int8_t dtim = 0x80 + (target << 2);
554 u_int32_t tim = pci_read_config(parent, dtim, 4);
557 mode = min(mode, ctlr->chip->max_dma);
558 if (mode >= ATA_UDMA0) {
561 tim |= ((mode & ATA_MODE_MASK) << 16);
563 } else if (mode >= ATA_WDMA0) {
566 tim |= ((mode & ATA_MODE_MASK) << 8);
567 piomode = (mode == ATA_WDMA0) ? ATA_PIO0 :
568 (mode == ATA_WDMA1) ? ATA_PIO3 : ATA_PIO4;
572 tim |= (piomode & 0x7);
573 pci_write_config(parent, dtim, tim, 4);
578 ata_intel_sata_getrev(device_t dev, int target)
580 struct ata_channel *ch = device_get_softc(dev);
583 if (ata_sata_scr_read(ch, target, ATA_SSTATUS, &status) == 0)
584 return ((status & 0x0f0) >> 4);
589 ata_intel_sata_status(device_t dev)
591 struct ata_channel *ch = device_get_softc(dev);
593 ata_sata_phy_check_events(dev, 0);
594 if ((ch->flags & ATA_NO_SLAVE) == 0)
595 ata_sata_phy_check_events(dev, 1);
597 return ata_pci_status(dev);
601 ata_intel_sata_ahci_read(device_t dev, int port, int reg, u_int32_t *result)
603 struct ata_pci_controller *ctlr;
604 struct ata_channel *ch;
609 parent = device_get_parent(dev);
610 ctlr = device_get_softc(parent);
611 ch = device_get_softc(dev);
612 port = (port == 1) ? 1 : 0;
613 smap = ATA_INTEL_SMAP(ctlr, ch);
614 offset = 0x100 + smap[port] * 0x80;
628 *result = ATA_INL(ctlr->r_res2, offset + reg);
633 ata_intel_sata_cscr_read(device_t dev, int port, int reg, u_int32_t *result)
635 struct ata_pci_controller *ctlr;
636 struct ata_channel *ch;
640 parent = device_get_parent(dev);
641 ctlr = device_get_softc(parent);
642 ch = device_get_softc(dev);
643 smap = ATA_INTEL_SMAP(ctlr, ch);
644 port = (port == 1) ? 1 : 0;
658 ATA_INTEL_LOCK(ctlr);
659 pci_write_config(parent, 0xa0,
660 0x50 + smap[port] * 0x10 + reg * 4, 4);
661 *result = pci_read_config(parent, 0xa4, 4);
662 ATA_INTEL_UNLOCK(ctlr);
667 ata_intel_sata_sidpr_read(device_t dev, int port, int reg, u_int32_t *result)
669 struct ata_pci_controller *ctlr;
670 struct ata_channel *ch;
673 parent = device_get_parent(dev);
674 ctlr = device_get_softc(parent);
675 ch = device_get_softc(dev);
676 port = (port == 1) ? 1 : 0;
690 ATA_INTEL_LOCK(ctlr);
691 ATA_IDX_OUTL(ch, ATA_IDX_ADDR, ((ch->unit * 2 + port) << 8) + reg);
692 *result = ATA_IDX_INL(ch, ATA_IDX_DATA);
693 ATA_INTEL_UNLOCK(ctlr);
698 ata_intel_sata_ahci_write(device_t dev, int port, int reg, u_int32_t value)
700 struct ata_pci_controller *ctlr;
701 struct ata_channel *ch;
706 parent = device_get_parent(dev);
707 ctlr = device_get_softc(parent);
708 ch = device_get_softc(dev);
709 port = (port == 1) ? 1 : 0;
710 smap = ATA_INTEL_SMAP(ctlr, ch);
711 offset = 0x100 + smap[port] * 0x80;
725 ATA_OUTL(ctlr->r_res2, offset + reg, value);
730 ata_intel_sata_cscr_write(device_t dev, int port, int reg, u_int32_t value)
732 struct ata_pci_controller *ctlr;
733 struct ata_channel *ch;
737 parent = device_get_parent(dev);
738 ctlr = device_get_softc(parent);
739 ch = device_get_softc(dev);
740 smap = ATA_INTEL_SMAP(ctlr, ch);
741 port = (port == 1) ? 1 : 0;
755 ATA_INTEL_LOCK(ctlr);
756 pci_write_config(parent, 0xa0,
757 0x50 + smap[port] * 0x10 + reg * 4, 4);
758 pci_write_config(parent, 0xa4, value, 4);
759 ATA_INTEL_UNLOCK(ctlr);
764 ata_intel_sata_sidpr_write(device_t dev, int port, int reg, u_int32_t value)
766 struct ata_pci_controller *ctlr;
767 struct ata_channel *ch;
770 parent = device_get_parent(dev);
771 ctlr = device_get_softc(parent);
772 ch = device_get_softc(dev);
773 port = (port == 1) ? 1 : 0;
787 ATA_INTEL_LOCK(ctlr);
788 ATA_IDX_OUTL(ch, ATA_IDX_ADDR, ((ch->unit * 2 + port) << 8) + reg);
789 ATA_IDX_OUTL(ch, ATA_IDX_DATA, value);
790 ATA_INTEL_UNLOCK(ctlr);
795 ata_intel_sata_sidpr_test(device_t dev)
797 struct ata_channel *ch = device_get_softc(dev);
801 port = (ch->flags & ATA_NO_SLAVE) ? 0 : 1;
802 for (; port >= 0; port--) {
803 ata_intel_sata_sidpr_read(dev, port, ATA_SCONTROL, &val);
804 if ((val & ATA_SC_IPM_MASK) ==
805 (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER))
807 val |= ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER;
808 ata_intel_sata_sidpr_write(dev, port, ATA_SCONTROL, val);
809 ata_intel_sata_sidpr_read(dev, port, ATA_SCONTROL, &val);
810 if ((val & ATA_SC_IPM_MASK) ==
811 (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER))
816 "SControl registers are not functional: %08x\n", val);
821 ata_intel_31244_ch_attach(device_t dev)
823 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
824 struct ata_channel *ch = device_get_softc(dev);
828 ata_pci_dmainit(dev);
830 ch_offset = 0x200 + ch->unit * 0x200;
832 for (i = ATA_DATA; i < ATA_MAX_RES; i++)
833 ch->r_io[i].res = ctlr->r_res2;
835 /* setup ATA registers */
836 ch->r_io[ATA_DATA].offset = ch_offset + 0x00;
837 ch->r_io[ATA_FEATURE].offset = ch_offset + 0x06;
838 ch->r_io[ATA_COUNT].offset = ch_offset + 0x08;
839 ch->r_io[ATA_SECTOR].offset = ch_offset + 0x0c;
840 ch->r_io[ATA_CYL_LSB].offset = ch_offset + 0x10;
841 ch->r_io[ATA_CYL_MSB].offset = ch_offset + 0x14;
842 ch->r_io[ATA_DRIVE].offset = ch_offset + 0x18;
843 ch->r_io[ATA_COMMAND].offset = ch_offset + 0x1d;
844 ch->r_io[ATA_ERROR].offset = ch_offset + 0x04;
845 ch->r_io[ATA_STATUS].offset = ch_offset + 0x1c;
846 ch->r_io[ATA_ALTSTAT].offset = ch_offset + 0x28;
847 ch->r_io[ATA_CONTROL].offset = ch_offset + 0x29;
849 /* setup DMA registers */
850 ch->r_io[ATA_SSTATUS].offset = ch_offset + 0x100;
851 ch->r_io[ATA_SERROR].offset = ch_offset + 0x104;
852 ch->r_io[ATA_SCONTROL].offset = ch_offset + 0x108;
854 /* setup SATA registers */
855 ch->r_io[ATA_BMCMD_PORT].offset = ch_offset + 0x70;
856 ch->r_io[ATA_BMSTAT_PORT].offset = ch_offset + 0x72;
857 ch->r_io[ATA_BMDTP_PORT].offset = ch_offset + 0x74;
859 ch->flags |= ATA_NO_SLAVE;
860 ch->flags |= ATA_SATA;
862 ch->hw.status = ata_intel_31244_status;
863 ch->hw.tf_write = ata_intel_31244_tf_write;
865 /* enable PHY state change interrupt */
866 ATA_OUTL(ctlr->r_res2, 0x4,
867 ATA_INL(ctlr->r_res2, 0x04) | (0x01 << (ch->unit << 3)));
872 ata_intel_31244_ch_detach(device_t dev)
875 ata_pci_dmafini(dev);
880 ata_intel_31244_status(device_t dev)
882 /* do we have any PHY events ? */
883 ata_sata_phy_check_events(dev, -1);
885 /* any drive action to take care of ? */
886 return ata_pci_status(dev);
890 ata_intel_31244_tf_write(struct ata_request *request)
892 struct ata_channel *ch = device_get_softc(request->parent);
894 if (request->flags & ATA_R_48BIT) {
895 ATA_IDX_OUTW(ch, ATA_FEATURE, request->u.ata.feature);
896 ATA_IDX_OUTW(ch, ATA_COUNT, request->u.ata.count);
897 ATA_IDX_OUTW(ch, ATA_SECTOR, ((request->u.ata.lba >> 16) & 0xff00) |
898 (request->u.ata.lba & 0x00ff));
899 ATA_IDX_OUTW(ch, ATA_CYL_LSB, ((request->u.ata.lba >> 24) & 0xff00) |
900 ((request->u.ata.lba >> 8) & 0x00ff));
901 ATA_IDX_OUTW(ch, ATA_CYL_MSB, ((request->u.ata.lba >> 32) & 0xff00) |
902 ((request->u.ata.lba >> 16) & 0x00ff));
903 ATA_IDX_OUTW(ch, ATA_DRIVE, ATA_D_LBA | ATA_DEV(request->unit));
906 ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature);
907 ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count);
908 ATA_IDX_OUTB(ch, ATA_SECTOR, request->u.ata.lba);
909 ATA_IDX_OUTB(ch, ATA_CYL_LSB, request->u.ata.lba >> 8);
910 ATA_IDX_OUTB(ch, ATA_CYL_MSB, request->u.ata.lba >> 16);
911 ATA_IDX_OUTB(ch, ATA_DRIVE,
912 ATA_D_IBM | ATA_D_LBA | ATA_DEV(request->unit) |
913 ((request->u.ata.lba >> 24) & 0x0f));
918 ata_intel_31244_reset(device_t dev)
920 struct ata_channel *ch = device_get_softc(dev);
922 if (ata_sata_phy_reset(dev, -1, 1))
923 ata_generic_reset(dev);
928 ATA_DECLARE_DRIVER(ata_intel);