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_nvidia_chipinit(device_t dev);
56 static int ata_nvidia_ch_attach(device_t dev);
57 static int ata_nvidia_status(device_t dev);
58 static void ata_nvidia_reset(device_t dev);
59 static void ata_nvidia_setmode(device_t dev, int mode);
68 * nVidia chipset support functions
71 ata_nvidia_probe(device_t dev)
73 struct ata_pci_controller *ctlr = device_get_softc(dev);
74 static struct ata_chip_id ids[] =
75 {{ ATA_NFORCE1, 0, 0, 0, ATA_UDMA5, "nForce" },
76 { ATA_NFORCE2, 0, 0, 0, ATA_UDMA6, "nForce2" },
77 { ATA_NFORCE2_PRO, 0, 0, 0, ATA_UDMA6, "nForce2 Pro" },
78 { ATA_NFORCE2_PRO_S1, 0, 0, 0, ATA_SA150, "nForce2 Pro" },
79 { ATA_NFORCE3, 0, 0, 0, ATA_UDMA6, "nForce3" },
80 { ATA_NFORCE3_PRO, 0, 0, 0, ATA_UDMA6, "nForce3 Pro" },
81 { ATA_NFORCE3_PRO_S1, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
82 { ATA_NFORCE3_PRO_S2, 0, 0, 0, ATA_SA150, "nForce3 Pro" },
83 { ATA_NFORCE_MCP04, 0, 0, 0, ATA_UDMA6, "nForce MCP" },
84 { ATA_NFORCE_MCP04_S1, 0, NV4, 0, ATA_SA150, "nForce MCP" },
85 { ATA_NFORCE_MCP04_S2, 0, NV4, 0, ATA_SA150, "nForce MCP" },
86 { ATA_NFORCE_CK804, 0, 0, 0, ATA_UDMA6, "nForce CK804" },
87 { ATA_NFORCE_CK804_S1, 0, NV4, 0, ATA_SA300, "nForce CK804" },
88 { ATA_NFORCE_CK804_S2, 0, NV4, 0, ATA_SA300, "nForce CK804" },
89 { ATA_NFORCE_MCP51, 0, 0, 0, ATA_UDMA6, "nForce MCP51" },
90 { ATA_NFORCE_MCP51_S1, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP51" },
91 { ATA_NFORCE_MCP51_S2, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP51" },
92 { ATA_NFORCE_MCP55, 0, 0, 0, ATA_UDMA6, "nForce MCP55" },
93 { ATA_NFORCE_MCP55_S1, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP55" },
94 { ATA_NFORCE_MCP55_S2, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP55" },
95 { ATA_NFORCE_MCP61, 0, 0, 0, ATA_UDMA6, "nForce MCP61" },
96 { ATA_NFORCE_MCP61_S1, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP61" },
97 { ATA_NFORCE_MCP61_S2, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP61" },
98 { ATA_NFORCE_MCP61_S3, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP61" },
99 { ATA_NFORCE_MCP65, 0, 0, 0, ATA_UDMA6, "nForce MCP65" },
100 { ATA_NFORCE_MCP67, 0, 0, 0, ATA_UDMA6, "nForce MCP67" },
101 { ATA_NFORCE_MCP67_A0, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" },
102 { ATA_NFORCE_MCP67_A1, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" },
103 { ATA_NFORCE_MCP67_A2, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" },
104 { ATA_NFORCE_MCP67_A3, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" },
105 { ATA_NFORCE_MCP67_A4, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" },
106 { ATA_NFORCE_MCP67_A5, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" },
107 { ATA_NFORCE_MCP67_A6, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" },
108 { ATA_NFORCE_MCP67_A7, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" },
109 { ATA_NFORCE_MCP67_A8, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" },
110 { ATA_NFORCE_MCP67_A9, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" },
111 { ATA_NFORCE_MCP67_AA, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" },
112 { ATA_NFORCE_MCP67_AB, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" },
113 { ATA_NFORCE_MCP67_AC, 0, NVAHCI, 0, ATA_SA300, "nForce MCP67" },
114 { ATA_NFORCE_MCP73, 0, 0, 0, ATA_UDMA6, "nForce MCP73" },
115 { ATA_NFORCE_MCP73_A0, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" },
116 { ATA_NFORCE_MCP73_A1, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" },
117 { ATA_NFORCE_MCP73_A2, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" },
118 { ATA_NFORCE_MCP73_A3, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" },
119 { ATA_NFORCE_MCP73_A4, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" },
120 { ATA_NFORCE_MCP73_A5, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" },
121 { ATA_NFORCE_MCP73_A6, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" },
122 { ATA_NFORCE_MCP73_A7, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" },
123 { ATA_NFORCE_MCP73_A8, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" },
124 { ATA_NFORCE_MCP73_A9, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" },
125 { ATA_NFORCE_MCP73_AA, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" },
126 { ATA_NFORCE_MCP73_AB, 0, NVAHCI, 0, ATA_SA300, "nForce MCP73" },
127 { ATA_NFORCE_MCP77, 0, 0, 0, ATA_UDMA6, "nForce MCP77" },
128 { 0, 0, 0, 0, 0, 0}} ;
130 if (pci_get_vendor(dev) != ATA_NVIDIA_ID)
133 if (!(ctlr->chip = ata_match_chip(dev, ids)))
137 if (ctlr->chip->cfg1 & NVAHCI)
138 ctlr->chipinit = ata_ahci_chipinit;
140 ctlr->chipinit = ata_nvidia_chipinit;
141 return (BUS_PROBE_DEFAULT);
145 ata_nvidia_chipinit(device_t dev)
147 struct ata_pci_controller *ctlr = device_get_softc(dev);
149 if (ata_setup_interrupt(dev, ata_generic_intr))
152 if (ctlr->chip->max_dma >= ATA_SA150) {
153 if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
154 ctlr->r_type2 = SYS_RES_IOPORT;
156 ctlr->r_type2 = SYS_RES_MEMORY;
157 ctlr->r_rid2 = PCIR_BAR(5);
158 if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
159 &ctlr->r_rid2, RF_ACTIVE))) {
160 int offset = ctlr->chip->cfg1 & NV4 ? 0x0440 : 0x0010;
162 ctlr->ch_attach = ata_nvidia_ch_attach;
163 ctlr->ch_detach = ata_pci_ch_detach;
164 ctlr->reset = ata_nvidia_reset;
166 /* enable control access */
167 pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
168 /* MCP55 seems to need some time to allow r_res2 read. */
170 if (ctlr->chip->cfg1 & NVQ) {
171 /* clear interrupt status */
172 ATA_OUTL(ctlr->r_res2, offset, 0x00ff00ff);
174 /* enable device and PHY state change interrupts */
175 ATA_OUTL(ctlr->r_res2, offset + 4, 0x000d000d);
177 /* disable NCQ support */
178 ATA_OUTL(ctlr->r_res2, 0x0400,
179 ATA_INL(ctlr->r_res2, 0x0400) & 0xfffffff9);
182 /* clear interrupt status */
183 ATA_OUTB(ctlr->r_res2, offset, 0xff);
185 /* enable device and PHY state change interrupts */
186 ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
189 ctlr->setmode = ata_sata_setmode;
192 /* disable prefetch, postwrite */
193 pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
194 ctlr->setmode = ata_nvidia_setmode;
200 ata_nvidia_ch_attach(device_t dev)
202 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
203 struct ata_channel *ch = device_get_softc(dev);
205 /* setup the usual register normal pci style */
206 if (ata_pci_ch_attach(dev))
209 ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
210 ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
211 ch->r_io[ATA_SERROR].res = ctlr->r_res2;
212 ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
213 ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
214 ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
216 ch->hw.status = ata_nvidia_status;
217 ch->flags |= ATA_NO_SLAVE;
223 ata_nvidia_status(device_t dev)
225 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
226 struct ata_channel *ch = device_get_softc(dev);
227 int offset = ctlr->chip->cfg1 & NV4 ? 0x0440 : 0x0010;
228 int shift = ch->unit << (ctlr->chip->cfg1 & NVQ ? 4 : 2);
231 /* get interrupt status */
232 if (ctlr->chip->cfg1 & NVQ)
233 istatus = ATA_INL(ctlr->r_res2, offset);
235 istatus = ATA_INB(ctlr->r_res2, offset);
237 /* do we have any PHY events ? */
238 if (istatus & (0x0c << shift))
239 ata_sata_phy_check_events(dev);
241 /* clear interrupt(s) */
242 if (ctlr->chip->cfg1 & NVQ)
243 ATA_OUTL(ctlr->r_res2, offset, (0x0f << shift) | 0x00f000f0);
245 ATA_OUTB(ctlr->r_res2, offset, (0x0f << shift));
247 /* do we have any device action ? */
248 return (istatus & (0x01 << shift));
252 ata_nvidia_reset(device_t dev)
254 if (ata_sata_phy_reset(dev, -1, 1))
255 ata_generic_reset(dev);
259 ata_nvidia_setmode(device_t dev, int mode)
261 device_t gparent = GRANDPARENT(dev);
262 struct ata_pci_controller *ctlr = device_get_softc(gparent);
263 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
264 struct ata_device *atadev = device_get_softc(dev);
265 u_int8_t timings[] = { 0xa8, 0x65, 0x42, 0x22, 0x20, 0x42, 0x22, 0x20,
266 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
267 int modes[7] = { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 };
268 int devno = (ch->unit << 1) + atadev->unit;
269 int reg = 0x63 - devno;
272 mode = ata_limit_mode(dev, mode, ctlr->chip->max_dma);
273 mode = ata_check_80pin(dev, mode);
275 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
277 device_printf(dev, "%ssetting %s on %s chip\n",
278 (error) ? "FAILURE " : "", ata_mode2str(mode),
281 pci_write_config(gparent, reg - 0x08, timings[ata_mode2idx(mode)], 1);
282 if (mode >= ATA_UDMA0)
283 pci_write_config(gparent, reg, modes[mode & ATA_MODE_MASK], 1);
285 pci_write_config(gparent, reg, 0x8b, 1);
290 ATA_DECLARE_DRIVER(ata_nvidia);
291 MODULE_DEPEND(ata_nvidia, ata_ahci, 1, 1, 1);