]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/ata/chipsets/ata-intel.c
MFC r218140 (jfv):
[FreeBSD/stable/8.git] / sys / dev / ata / chipsets / ata-intel.c
1 /*-
2  * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
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.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_ata.h"
31 #include <sys/param.h>
32 #include <sys/module.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/ata.h>
36 #include <sys/bus.h>
37 #include <sys/endian.h>
38 #include <sys/malloc.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/sema.h>
42 #include <sys/taskqueue.h>
43 #include <vm/uma.h>
44 #include <machine/stdarg.h>
45 #include <machine/resource.h>
46 #include <machine/bus.h>
47 #include <sys/rman.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>
52 #include <ata_if.h>
53
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);
80
81 /* misc defines */
82 #define INTEL_AHCI      1
83 #define INTEL_ICH5      2
84 #define INTEL_6CH       4
85 #define INTEL_6CH2      8
86 #define INTEL_ICH7      16
87
88 /*
89  * Intel chipset support functions
90  */
91 static int
92 ata_intel_probe(device_t dev)
93 {
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}};
183
184     if (pci_get_vendor(dev) != ATA_INTEL_ID)
185         return ENXIO;
186
187     if (!(ctlr->chip = ata_match_chip(dev, ids)))
188         return ENXIO;
189
190     ata_set_desc(dev);
191     ctlr->chipinit = ata_intel_chipinit;
192     return (BUS_PROBE_DEFAULT);
193 }
194
195 static int
196 ata_intel_chipinit(device_t dev)
197 {
198     struct ata_pci_controller *ctlr = device_get_softc(dev);
199
200     if (ata_setup_interrupt(dev, ata_generic_intr))
201         return ENXIO;
202
203     ctlr->chipset_data = NULL;
204
205     /* good old PIIX needs special treatment (not implemented) */
206     if (ctlr->chip->chipid == ATA_I82371FB) {
207         ctlr->setmode = ata_intel_old_setmode;
208     }
209
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,
216                                                         &ctlr->r_rid2,
217                                                         RF_ACTIVE)))
218                 return ENXIO;
219             ctlr->channels = 4;
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;
223         }
224         ctlr->setmode = ata_sata_setmode;
225         ctlr->getrev = ata_sata_getrev;
226     }
227     /* SCH */
228     else if (ctlr->chip->chipid == ATA_ISCH) {
229         ctlr->channels = 1;
230         ctlr->ch_attach = ata_intel_ch_attach;
231         ctlr->ch_detach = ata_pci_ch_detach;
232         ctlr->setmode = ata_intel_sch_setmode;
233     }
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;
240     }
241
242     /* SATA parts can be either compat or AHCI */
243     else {
244         /* force all ports active "the legacy way" */
245         pci_write_config(dev, 0x92, pci_read_config(dev, 0x92, 2) | 0x0f, 2);
246
247         ctlr->ch_attach = ata_intel_ch_attach;
248         ctlr->ch_detach = ata_pci_ch_detach;
249         ctlr->reset = ata_intel_reset;
250
251         /* 
252          * if we have AHCI capability and AHCI or RAID mode enabled
253          * in BIOS we try for AHCI mode
254          */ 
255         if ((ctlr->chip->cfg1 & INTEL_AHCI) &&
256             (pci_read_config(dev, 0x90, 1) & 0xc0) &&
257             (ata_ahci_chipinit(dev) != ENXIO))
258             return 0;
259
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);
273                 }
274         } else {
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);
279         }
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;
284     }
285     return 0;
286 }
287
288 static int
289 ata_intel_ch_attach(device_t dev)
290 {
291         struct ata_pci_controller *ctlr;
292         struct ata_channel *ch;
293         u_char *smap;
294         u_int map;
295
296         /* setup the usual register normal pci style */
297         if (ata_pci_ch_attach(dev))
298                 return (ENXIO);
299
300         ctlr = device_get_softc(device_get_parent(dev));
301         ch = device_get_softc(dev);
302
303         /* if r_res2 is valid it points to SATA interface registers */
304         if (ctlr->r_res2) {
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;
309         }
310
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) {
316                         map &= 0x07;
317                         if ((map & 0x04) == 0) {
318                                 ch->flags |= ATA_SATA;
319                                 ch->flags |= ATA_NO_SLAVE;
320                                 smap[0] = (map & 0x01) ^ ch->unit;
321                                 smap[1] = 0;
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;
330                         }
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;
335                         smap[1] = 0;
336                 } else {
337                         map &= 0x03;
338                         if (map == 0x00) {
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;
344                                 smap[0] = 0;
345                                 smap[1] = 2;
346                         } else if (map == 0x01 && ch->unit == 1) {
347                                 ch->flags |= ATA_SATA;
348                                 smap[0] = 1;
349                                 smap[1] = 3;
350                         }
351                 }
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;
364                                 } else {
365                                         ch->hw.pm_read = ata_intel_sata_sidpr_read;
366                                         ch->hw.pm_write = ata_intel_sata_sidpr_write;
367                                 };
368                         }
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);
375                                 }
376                         }
377                 } else
378                         ctlr->setmode = ata_intel_new_setmode;
379         } else if (ctlr->chip->chipid != ATA_ISCH)
380                 ch->flags |= ATA_CHECKS_CABLE;
381         return (0);
382 }
383
384 static void
385 ata_intel_reset(device_t dev)
386 {
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;
391         u_char *smap;
392
393         /* In combined mode, skip SATA stuff for PATA channel. */
394         if ((ch->flags & ATA_SATA) == 0)
395                 return (ata_generic_reset(dev));
396
397         /* Do hard-reset on respective SATA ports. */
398         smap = (u_char *)&ctlr->chipset_data + ch->unit * 2;
399         mask = 1 << smap[0];
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);
404         DELAY(10);
405         pci_write_config(parent, 0x92,
406             pci_read_config(parent, 0x92, 2) | mask, 2);
407
408         /* Wait up to 1 sec for "connect well". */
409         if (ctlr->chip->cfg1 & (INTEL_6CH | INTEL_6CH2))
410                 pmask = mask << 8;
411         else
412                 pmask = mask << 4;
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))
416                         break;
417                 ata_udelay(10000);
418         }
419
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);
425         } else
426                 devs = 1;
427         if (devs)
428                 ata_generic_reset(dev);
429         else
430                 ch->devices = 0;
431 }
432
433 static int
434 ata_intel_old_setmode(device_t dev, int target, int mode)
435 {
436         device_t parent = device_get_parent(dev);
437         struct ata_pci_controller *ctlr = device_get_softc(parent);
438
439         mode = min(mode, ctlr->chip->max_dma);
440         return (mode);
441 }
442
443 static int
444 ata_intel_new_setmode(device_t dev, int target, int mode)
445 {
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;
450         int piomode;
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 };
460
461         /* In combined mode, skip PATA stuff for SATA channel. */
462         if (ch->flags & ATA_SATA)
463                 return (ata_sata_setmode(dev, target, mode));
464
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");
469                 mode = ATA_UDMA2;
470         }
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);
477             piomode = ATA_PIO4;
478         } else {
479             pci_write_config(parent, 0x48, reg48 & ~(0x0001 << devno), 2);
480             pci_write_config(parent, 0x4a, (reg4a & ~(0x3 << (devno << 2))),2);
481             piomode = mode;
482         }
483         reg54 |= 0x0400;
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;
493         reg40 |= 0x40774077;
494         /* Set PIO/WDMA timings. */
495         if (target == 0) {
496             mask40 = 0x3300;
497             new40 = timings[ata_mode2idx(piomode)] << 8;
498         } else {
499             mask44 = 0x0f;
500             new44 = ((timings[ata_mode2idx(piomode)] & 0x30) >> 2) |
501                     (timings[ata_mode2idx(piomode)] & 0x03);
502         }
503         if (ch->unit) {
504             mask40 <<= 16;
505             new40 <<= 16;
506             mask44 <<= 4;
507             new44 <<= 4;
508         }
509         pci_write_config(parent, 0x40, (reg40 & ~mask40) | new40, 4);
510         pci_write_config(parent, 0x44, (reg44 & ~mask44) | new44, 1);
511         return (mode);
512 }
513
514 static int
515 ata_intel_sch_setmode(device_t dev, int target, int mode)
516 {
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);
521         int piomode;
522
523         mode = min(mode, ctlr->chip->max_dma);
524         if (mode >= ATA_UDMA0) {
525                 tim |= (0x1 << 31);
526                 tim &= ~(0x7 << 16);
527                 tim |= ((mode & ATA_MODE_MASK) << 16);
528                 piomode = ATA_PIO4;
529         } else if (mode >= ATA_WDMA0) {
530                 tim &= ~(0x1 << 31);
531                 tim &= ~(0x3 << 8);
532                 tim |= ((mode & ATA_MODE_MASK) << 8);
533                 piomode = (mode == ATA_WDMA0) ? ATA_PIO0 :
534                     (mode == ATA_WDMA1) ? ATA_PIO3 : ATA_PIO4;
535         } else
536                 piomode = mode;
537         tim &= ~(0x7);
538         tim |= (piomode & 0x7);
539         pci_write_config(parent, dtim, tim, 4);
540         return (mode);
541 }
542
543 static int
544 ata_intel_sata_getrev(device_t dev, int target)
545 {
546         struct ata_channel *ch = device_get_softc(dev);
547         uint32_t status;
548
549         if (ata_sata_scr_read(ch, target, ATA_SSTATUS, &status) == 0)
550                 return ((status & 0x0f0) >> 4);
551         return (0xff);
552 }
553
554 static int
555 ata_intel_sata_status(device_t dev)
556 {
557         struct ata_channel *ch = device_get_softc(dev);
558
559         ata_sata_phy_check_events(dev, 0);
560         if ((ch->flags & ATA_NO_SLAVE) == 0)
561                 ata_sata_phy_check_events(dev, 1);
562
563         return ata_pci_status(dev);
564 }
565
566 static int
567 ata_intel_sata_ahci_read(device_t dev, int port, int reg, u_int32_t *result)
568 {
569         struct ata_pci_controller *ctlr;
570         struct ata_channel *ch;
571         device_t parent;
572         u_char *smap;
573         int offset;
574
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;
581         switch (reg) {
582         case ATA_SSTATUS:
583             reg = 0x28;
584             break;
585         case ATA_SCONTROL:
586             reg = 0x2c;
587             break;
588         case ATA_SERROR:
589             reg = 0x30;
590             break;
591         default:
592             return (EINVAL);
593         }
594         *result = ATA_INL(ctlr->r_res2, offset + reg);
595         return (0);
596 }
597
598 static int
599 ata_intel_sata_cscr_read(device_t dev, int port, int reg, u_int32_t *result)
600 {
601         struct ata_pci_controller *ctlr;
602         struct ata_channel *ch;
603         device_t parent;
604         u_char *smap;
605
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;
611         switch (reg) {
612         case ATA_SSTATUS:
613             reg = 0;
614             break;
615         case ATA_SERROR:
616             reg = 1;
617             break;
618         case ATA_SCONTROL:
619             reg = 2;
620             break;
621         default:
622             return (EINVAL);
623         }
624         pci_write_config(parent, 0xa0,
625             0x50 + smap[port] * 0x10 + reg * 4, 4);
626         *result = pci_read_config(parent, 0xa4, 4);
627         return (0);
628 }
629
630 static int
631 ata_intel_sata_sidpr_read(device_t dev, int port, int reg, u_int32_t *result)
632 {
633         struct ata_pci_controller *ctlr;
634         struct ata_channel *ch;
635         device_t parent;
636
637         parent = device_get_parent(dev);
638         ctlr = device_get_softc(parent);
639         ch = device_get_softc(dev);
640         port = (port == 1) ? 1 : 0;
641         switch (reg) {
642         case ATA_SSTATUS:
643             reg = 0;
644             break;
645         case ATA_SCONTROL:
646             reg = 1;
647             break;
648         case ATA_SERROR:
649             reg = 2;
650             break;
651         default:
652             return (EINVAL);
653         }
654         ATA_IDX_OUTL(ch, ATA_IDX_ADDR, ((ch->unit * 2 + port) << 8) + reg);
655         *result = ATA_IDX_INL(ch, ATA_IDX_DATA);
656         return (0);
657 }
658
659 static int
660 ata_intel_sata_ahci_write(device_t dev, int port, int reg, u_int32_t value)
661 {
662         struct ata_pci_controller *ctlr;
663         struct ata_channel *ch;
664         device_t parent;
665         u_char *smap;
666         int offset;
667
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;
674         switch (reg) {
675         case ATA_SSTATUS:
676             reg = 0x28;
677             break;
678         case ATA_SCONTROL:
679             reg = 0x2c;
680             break;
681         case ATA_SERROR:
682             reg = 0x30;
683             break;
684         default:
685             return (EINVAL);
686         }
687         ATA_OUTL(ctlr->r_res2, offset + reg, value);
688         return (0);
689 }
690
691 static int
692 ata_intel_sata_cscr_write(device_t dev, int port, int reg, u_int32_t value)
693 {
694         struct ata_pci_controller *ctlr;
695         struct ata_channel *ch;
696         device_t parent;
697         u_char *smap;
698
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;
704         switch (reg) {
705         case ATA_SSTATUS:
706             reg = 0;
707             break;
708         case ATA_SERROR:
709             reg = 1;
710             break;
711         case ATA_SCONTROL:
712             reg = 2;
713             break;
714         default:
715             return (EINVAL);
716         }
717         pci_write_config(parent, 0xa0,
718             0x50 + smap[port] * 0x10 + reg * 4, 4);
719         pci_write_config(parent, 0xa4, value, 4);
720         return (0);
721 }
722
723 static int
724 ata_intel_sata_sidpr_write(device_t dev, int port, int reg, u_int32_t value)
725 {
726         struct ata_pci_controller *ctlr;
727         struct ata_channel *ch;
728         device_t parent;
729
730         parent = device_get_parent(dev);
731         ctlr = device_get_softc(parent);
732         ch = device_get_softc(dev);
733         port = (port == 1) ? 1 : 0;
734         switch (reg) {
735         case ATA_SSTATUS:
736             reg = 0;
737             break;
738         case ATA_SCONTROL:
739             reg = 1;
740             break;
741         case ATA_SERROR:
742             reg = 2;
743             break;
744         default:
745             return (EINVAL);
746         }
747         ATA_IDX_OUTL(ch, ATA_IDX_ADDR, ((ch->unit * 2 + port) << 8) + reg);
748         ATA_IDX_OUTL(ch, ATA_IDX_DATA, value);
749         return (0);
750 }
751
752 static int
753 ata_intel_31244_ch_attach(device_t dev)
754 {
755     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
756     struct ata_channel *ch = device_get_softc(dev);
757     int i;
758     int ch_offset;
759
760     ata_pci_dmainit(dev);
761
762     ch_offset = 0x200 + ch->unit * 0x200;
763
764     for (i = ATA_DATA; i < ATA_MAX_RES; i++)
765         ch->r_io[i].res = ctlr->r_res2;
766
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;
780
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;
785
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;
790
791     ch->flags |= ATA_NO_SLAVE;
792     ch->flags |= ATA_SATA;
793     ata_pci_hw(dev);
794     ch->hw.status = ata_intel_31244_status;
795     ch->hw.tf_write = ata_intel_31244_tf_write;
796
797     /* enable PHY state change interrupt */
798     ATA_OUTL(ctlr->r_res2, 0x4,
799              ATA_INL(ctlr->r_res2, 0x04) | (0x01 << (ch->unit << 3)));
800     return 0;
801 }
802
803 static int
804 ata_intel_31244_ch_detach(device_t dev)
805 {
806
807     ata_pci_dmafini(dev);
808     return (0);
809 }
810
811 static int
812 ata_intel_31244_status(device_t dev)
813 {
814     /* do we have any PHY events ? */
815     ata_sata_phy_check_events(dev, -1);
816
817     /* any drive action to take care of ? */
818     return ata_pci_status(dev);
819 }
820
821 static void
822 ata_intel_31244_tf_write(struct ata_request *request)
823 {
824     struct ata_channel *ch = device_get_softc(request->parent);
825 #ifndef ATA_CAM
826     struct ata_device *atadev = device_get_softc(request->dev);
827 #endif
828
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));
839     }
840     else {
841         ATA_IDX_OUTB(ch, ATA_FEATURE, request->u.ata.feature);
842         ATA_IDX_OUTB(ch, ATA_COUNT, request->u.ata.count);
843 #ifndef ATA_CAM
844         if (atadev->flags & ATA_D_USE_CHS) {
845             int heads, sectors;
846     
847             if (atadev->param.atavalid & ATA_FLAG_54_58) {
848                 heads = atadev->param.current_heads;
849                 sectors = atadev->param.current_sectors;
850             }
851             else {
852                 heads = atadev->param.heads;
853                 sectors = atadev->param.sectors;
854             }
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)) /
862                            sectors) & 0xf));
863         }
864         else {
865 #endif
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));
872 #ifndef ATA_CAM
873         }
874 #endif
875     }
876 }
877
878 static void
879 ata_intel_31244_reset(device_t dev)
880 {
881     struct ata_channel *ch = device_get_softc(dev);
882
883     if (ata_sata_phy_reset(dev, -1, 1))
884         ata_generic_reset(dev);
885     else
886         ch->devices = 0;
887 }
888
889 ATA_DECLARE_DRIVER(ata_intel);
890 MODULE_DEPEND(ata_intel, ata_ahci, 1, 1, 1);