xref: /freebsd/sys/dev/ata/chipsets/ata-nvidia.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
16  *
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.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/module.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/ata.h>
35 #include <sys/bus.h>
36 #include <sys/endian.h>
37 #include <sys/malloc.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/sema.h>
41 #include <sys/taskqueue.h>
42 #include <vm/uma.h>
43 #include <machine/stdarg.h>
44 #include <machine/resource.h>
45 #include <machine/bus.h>
46 #include <sys/rman.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/ata/ata-all.h>
50 #include <dev/ata/ata-pci.h>
51 #include <ata_if.h>
52 
53 /* local prototypes */
54 static int ata_nvidia_chipinit(device_t dev);
55 static int ata_nvidia_ch_attach(device_t dev);
56 static int ata_nvidia_ch_attach_dumb(device_t dev);
57 static int ata_nvidia_status(device_t dev);
58 static void ata_nvidia_reset(device_t dev);
59 static int ata_nvidia_setmode(device_t dev, int target, int mode);
60 
61 /* misc defines */
62 #define NV4             0x01
63 #define NVQ             0x02
64 #define NVAHCI          0x04
65 
66 /*
67  * nVidia chipset support functions
68  */
69 static int
70 ata_nvidia_probe(device_t dev)
71 {
72     struct ata_pci_controller *ctlr = device_get_softc(dev);
73     static const struct ata_chip_id ids[] =
74     {{ ATA_NFORCE1,         0, 0,       0, ATA_UDMA5, "nForce" },
75      { ATA_NFORCE2,         0, 0,       0, ATA_UDMA6, "nForce2" },
76      { ATA_NFORCE2_PRO,     0, 0,       0, ATA_UDMA6, "nForce2 Pro" },
77      { ATA_NFORCE2_PRO_S1,  0, 0,       0, ATA_SA150, "nForce2 Pro" },
78      { ATA_NFORCE3,         0, 0,       0, ATA_UDMA6, "nForce3" },
79      { ATA_NFORCE3_PRO,     0, 0,       0, ATA_UDMA6, "nForce3 Pro" },
80      { ATA_NFORCE3_PRO_S1,  0, 0,       0, ATA_SA150, "nForce3 Pro" },
81      { ATA_NFORCE3_PRO_S2,  0, 0,       0, ATA_SA150, "nForce3 Pro" },
82      { ATA_NFORCE_MCP04,    0, 0,       0, ATA_UDMA6, "nForce MCP" },
83      { ATA_NFORCE_MCP04_S1, 0, NV4,     0, ATA_SA150, "nForce MCP" },
84      { ATA_NFORCE_MCP04_S2, 0, NV4,     0, ATA_SA150, "nForce MCP" },
85      { ATA_NFORCE_CK804,    0, 0,       0, ATA_UDMA6, "nForce CK804" },
86      { ATA_NFORCE_CK804_S1, 0, NV4,     0, ATA_SA300, "nForce CK804" },
87      { ATA_NFORCE_CK804_S2, 0, NV4,     0, ATA_SA300, "nForce CK804" },
88      { ATA_NFORCE_MCP51,    0, 0,       0, ATA_UDMA6, "nForce MCP51" },
89      { ATA_NFORCE_MCP51_S1, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP51" },
90      { ATA_NFORCE_MCP51_S2, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP51" },
91      { ATA_NFORCE_MCP55,    0, 0,       0, ATA_UDMA6, "nForce MCP55" },
92      { ATA_NFORCE_MCP55_S1, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP55" },
93      { ATA_NFORCE_MCP55_S2, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP55" },
94      { ATA_NFORCE_MCP61,    0, 0,       0, ATA_UDMA6, "nForce MCP61" },
95      { ATA_NFORCE_MCP61_S1, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP61" },
96      { ATA_NFORCE_MCP61_S2, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP61" },
97      { ATA_NFORCE_MCP61_S3, 0, NV4|NVQ, 0, ATA_SA300, "nForce MCP61" },
98      { ATA_NFORCE_MCP65,    0, 0,       0, ATA_UDMA6, "nForce MCP65" },
99      { ATA_NFORCE_MCP65_A0, 0, NVAHCI,  0, ATA_SA300, "nForce MCP65" },
100      { ATA_NFORCE_MCP65_A1, 0, NVAHCI,  0, ATA_SA300, "nForce MCP65" },
101      { ATA_NFORCE_MCP65_A2, 0, NVAHCI,  0, ATA_SA300, "nForce MCP65" },
102      { ATA_NFORCE_MCP65_A3, 0, NVAHCI,  0, ATA_SA300, "nForce MCP65" },
103      { ATA_NFORCE_MCP65_A4, 0, NVAHCI,  0, ATA_SA300, "nForce MCP65" },
104      { ATA_NFORCE_MCP65_A5, 0, NVAHCI,  0, ATA_SA300, "nForce MCP65" },
105      { ATA_NFORCE_MCP65_A6, 0, NVAHCI,  0, ATA_SA300, "nForce MCP65" },
106      { ATA_NFORCE_MCP65_A7, 0, NVAHCI,  0, ATA_SA300, "nForce MCP65" },
107      { ATA_NFORCE_MCP67,    0, 0,       0, ATA_UDMA6, "nForce MCP67" },
108      { ATA_NFORCE_MCP67_A0, 0, NVAHCI,  0, ATA_SA300, "nForce MCP67" },
109      { ATA_NFORCE_MCP67_A1, 0, NVAHCI,  0, ATA_SA300, "nForce MCP67" },
110      { ATA_NFORCE_MCP67_A2, 0, NVAHCI,  0, ATA_SA300, "nForce MCP67" },
111      { ATA_NFORCE_MCP67_A3, 0, NVAHCI,  0, ATA_SA300, "nForce MCP67" },
112      { ATA_NFORCE_MCP67_A4, 0, NVAHCI,  0, ATA_SA300, "nForce MCP67" },
113      { ATA_NFORCE_MCP67_A5, 0, NVAHCI,  0, ATA_SA300, "nForce MCP67" },
114      { ATA_NFORCE_MCP67_A6, 0, NVAHCI,  0, ATA_SA300, "nForce MCP67" },
115      { ATA_NFORCE_MCP67_A7, 0, NVAHCI,  0, ATA_SA300, "nForce MCP67" },
116      { ATA_NFORCE_MCP67_A8, 0, NVAHCI,  0, ATA_SA300, "nForce MCP67" },
117      { ATA_NFORCE_MCP67_A9, 0, NVAHCI,  0, ATA_SA300, "nForce MCP67" },
118      { ATA_NFORCE_MCP67_AA, 0, NVAHCI,  0, ATA_SA300, "nForce MCP67" },
119      { ATA_NFORCE_MCP67_AB, 0, NVAHCI,  0, ATA_SA300, "nForce MCP67" },
120      { ATA_NFORCE_MCP67_AC, 0, NVAHCI,  0, ATA_SA300, "nForce MCP67" },
121      { ATA_NFORCE_MCP73,    0, 0,       0, ATA_UDMA6, "nForce MCP73" },
122      { ATA_NFORCE_MCP73_A0, 0, NVAHCI,  0, ATA_SA300, "nForce MCP73" },
123      { ATA_NFORCE_MCP73_A1, 0, NVAHCI,  0, ATA_SA300, "nForce MCP73" },
124      { ATA_NFORCE_MCP73_A2, 0, NVAHCI,  0, ATA_SA300, "nForce MCP73" },
125      { ATA_NFORCE_MCP73_A3, 0, NVAHCI,  0, ATA_SA300, "nForce MCP73" },
126      { ATA_NFORCE_MCP73_A4, 0, NVAHCI,  0, ATA_SA300, "nForce MCP73" },
127      { ATA_NFORCE_MCP73_A5, 0, NVAHCI,  0, ATA_SA300, "nForce MCP73" },
128      { ATA_NFORCE_MCP73_A6, 0, NVAHCI,  0, ATA_SA300, "nForce MCP73" },
129      { ATA_NFORCE_MCP73_A7, 0, NVAHCI,  0, ATA_SA300, "nForce MCP73" },
130      { ATA_NFORCE_MCP73_A8, 0, NVAHCI,  0, ATA_SA300, "nForce MCP73" },
131      { ATA_NFORCE_MCP73_A9, 0, NVAHCI,  0, ATA_SA300, "nForce MCP73" },
132      { ATA_NFORCE_MCP73_AA, 0, NVAHCI,  0, ATA_SA300, "nForce MCP73" },
133      { ATA_NFORCE_MCP73_AB, 0, NVAHCI,  0, ATA_SA300, "nForce MCP73" },
134      { ATA_NFORCE_MCP77,    0, 0,       0, ATA_UDMA6, "nForce MCP77" },
135      { ATA_NFORCE_MCP77_A0, 0, NVAHCI,  0, ATA_SA300, "nForce MCP77" },
136      { ATA_NFORCE_MCP77_A1, 0, NVAHCI,  0, ATA_SA300, "nForce MCP77" },
137      { ATA_NFORCE_MCP77_A2, 0, NVAHCI,  0, ATA_SA300, "nForce MCP77" },
138      { ATA_NFORCE_MCP77_A3, 0, NVAHCI,  0, ATA_SA300, "nForce MCP77" },
139      { ATA_NFORCE_MCP77_A4, 0, NVAHCI,  0, ATA_SA300, "nForce MCP77" },
140      { ATA_NFORCE_MCP77_A5, 0, NVAHCI,  0, ATA_SA300, "nForce MCP77" },
141      { ATA_NFORCE_MCP77_A6, 0, NVAHCI,  0, ATA_SA300, "nForce MCP77" },
142      { ATA_NFORCE_MCP77_A7, 0, NVAHCI,  0, ATA_SA300, "nForce MCP77" },
143      { ATA_NFORCE_MCP77_A8, 0, NVAHCI,  0, ATA_SA300, "nForce MCP77" },
144      { ATA_NFORCE_MCP77_A9, 0, NVAHCI,  0, ATA_SA300, "nForce MCP77" },
145      { ATA_NFORCE_MCP77_AA, 0, NVAHCI,  0, ATA_SA300, "nForce MCP77" },
146      { ATA_NFORCE_MCP77_AB, 0, NVAHCI,  0, ATA_SA300, "nForce MCP77" },
147      { ATA_NFORCE_MCP79_A0, 0, NVAHCI,  0, ATA_SA300, "nForce MCP79" },
148      { ATA_NFORCE_MCP79_A1, 0, NVAHCI,  0, ATA_SA300, "nForce MCP79" },
149      { ATA_NFORCE_MCP79_A2, 0, NVAHCI,  0, ATA_SA300, "nForce MCP79" },
150      { ATA_NFORCE_MCP79_A3, 0, NVAHCI,  0, ATA_SA300, "nForce MCP79" },
151      { ATA_NFORCE_MCP79_A4, 0, NVAHCI,  0, ATA_SA300, "nForce MCP79" },
152      { ATA_NFORCE_MCP79_A5, 0, NVAHCI,  0, ATA_SA300, "nForce MCP79" },
153      { ATA_NFORCE_MCP79_A6, 0, NVAHCI,  0, ATA_SA300, "nForce MCP79" },
154      { ATA_NFORCE_MCP79_A7, 0, NVAHCI,  0, ATA_SA300, "nForce MCP79" },
155      { ATA_NFORCE_MCP79_A8, 0, NVAHCI,  0, ATA_SA300, "nForce MCP79" },
156      { ATA_NFORCE_MCP79_A9, 0, NVAHCI,  0, ATA_SA300, "nForce MCP79" },
157      { ATA_NFORCE_MCP79_AA, 0, NVAHCI,  0, ATA_SA300, "nForce MCP79" },
158      { ATA_NFORCE_MCP79_AB, 0, NVAHCI,  0, ATA_SA300, "nForce MCP79" },
159      { ATA_NFORCE_MCP89_A0, 0, NVAHCI,  0, ATA_SA300, "nForce MCP89" },
160      { ATA_NFORCE_MCP89_A1, 0, NVAHCI,  0, ATA_SA300, "nForce MCP89" },
161      { ATA_NFORCE_MCP89_A2, 0, NVAHCI,  0, ATA_SA300, "nForce MCP89" },
162      { ATA_NFORCE_MCP89_A3, 0, NVAHCI,  0, ATA_SA300, "nForce MCP89" },
163      { ATA_NFORCE_MCP89_A4, 0, NVAHCI,  0, ATA_SA300, "nForce MCP89" },
164      { ATA_NFORCE_MCP89_A5, 0, NVAHCI,  0, ATA_SA300, "nForce MCP89" },
165      { ATA_NFORCE_MCP89_A6, 0, NVAHCI,  0, ATA_SA300, "nForce MCP89" },
166      { ATA_NFORCE_MCP89_A7, 0, NVAHCI,  0, ATA_SA300, "nForce MCP89" },
167      { ATA_NFORCE_MCP89_A8, 0, NVAHCI,  0, ATA_SA300, "nForce MCP89" },
168      { ATA_NFORCE_MCP89_A9, 0, NVAHCI,  0, ATA_SA300, "nForce MCP89" },
169      { ATA_NFORCE_MCP89_AA, 0, NVAHCI,  0, ATA_SA300, "nForce MCP89" },
170      { ATA_NFORCE_MCP89_AB, 0, NVAHCI,  0, ATA_SA300, "nForce MCP89" },
171      { 0, 0, 0, 0, 0, 0}} ;
172 
173     if (pci_get_vendor(dev) != ATA_NVIDIA_ID)
174 	return ENXIO;
175 
176     if (!(ctlr->chip = ata_match_chip(dev, ids)))
177 	return ENXIO;
178 
179     if ((ctlr->chip->cfg1 & NVAHCI) &&
180 	    pci_get_subclass(dev) != PCIS_STORAGE_IDE)
181 	return (ENXIO);
182 
183     ata_set_desc(dev);
184     ctlr->chipinit = ata_nvidia_chipinit;
185     return (BUS_PROBE_LOW_PRIORITY);
186 }
187 
188 static int
189 ata_nvidia_chipinit(device_t dev)
190 {
191     struct ata_pci_controller *ctlr = device_get_softc(dev);
192 
193     if (ata_setup_interrupt(dev, ata_generic_intr))
194 	return ENXIO;
195 
196     if (ctlr->chip->cfg1 & NVAHCI) {
197 	ctlr->ch_attach = ata_nvidia_ch_attach_dumb;
198 	ctlr->setmode = ata_sata_setmode;
199     } else if (ctlr->chip->max_dma >= ATA_SA150) {
200 	if (pci_read_config(dev, PCIR_BAR(5), 1) & 1)
201 	    ctlr->r_type2 = SYS_RES_IOPORT;
202 	else
203 	    ctlr->r_type2 = SYS_RES_MEMORY;
204 	ctlr->r_rid2 = PCIR_BAR(5);
205 	if ((ctlr->r_res2 = bus_alloc_resource_any(dev, ctlr->r_type2,
206 						   &ctlr->r_rid2, RF_ACTIVE))) {
207 	    int offset = ctlr->chip->cfg1 & NV4 ? 0x0440 : 0x0010;
208 
209 	    ctlr->ch_attach = ata_nvidia_ch_attach;
210 	    ctlr->ch_detach = ata_pci_ch_detach;
211 	    ctlr->reset = ata_nvidia_reset;
212 
213 	    /* enable control access */
214 	    pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) | 0x04,1);
215 	    /* MCP55 seems to need some time to allow r_res2 read. */
216 	    DELAY(10);
217 	    if (ctlr->chip->cfg1 & NVQ) {
218 		/* clear interrupt status */
219 		ATA_OUTL(ctlr->r_res2, offset, 0x00ff00ff);
220 
221 		/* enable device and PHY state change interrupts */
222 		ATA_OUTL(ctlr->r_res2, offset + 4, 0x000d000d);
223 
224 		/* disable NCQ support */
225 		ATA_OUTL(ctlr->r_res2, 0x0400,
226 			 ATA_INL(ctlr->r_res2, 0x0400) & 0xfffffff9);
227 	    }
228 	    else {
229 		/* clear interrupt status */
230 		ATA_OUTB(ctlr->r_res2, offset, 0xff);
231 
232 		/* enable device and PHY state change interrupts */
233 		ATA_OUTB(ctlr->r_res2, offset + 1, 0xdd);
234 	    }
235 	}
236 	ctlr->setmode = ata_sata_setmode;
237 	ctlr->getrev = ata_sata_getrev;
238     }
239     else {
240 	/* disable prefetch, postwrite */
241 	pci_write_config(dev, 0x51, pci_read_config(dev, 0x51, 1) & 0x0f, 1);
242 	ctlr->setmode = ata_nvidia_setmode;
243     }
244     return 0;
245 }
246 
247 static int
248 ata_nvidia_ch_attach(device_t dev)
249 {
250     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
251     struct ata_channel *ch = device_get_softc(dev);
252 
253     /* setup the usual register normal pci style */
254     if (ata_pci_ch_attach(dev))
255 	return ENXIO;
256 
257     ch->r_io[ATA_SSTATUS].res = ctlr->r_res2;
258     ch->r_io[ATA_SSTATUS].offset = (ch->unit << 6);
259     ch->r_io[ATA_SERROR].res = ctlr->r_res2;
260     ch->r_io[ATA_SERROR].offset = 0x04 + (ch->unit << 6);
261     ch->r_io[ATA_SCONTROL].res = ctlr->r_res2;
262     ch->r_io[ATA_SCONTROL].offset = 0x08 + (ch->unit << 6);
263 
264     ch->hw.status = ata_nvidia_status;
265     ch->flags |= ATA_NO_SLAVE;
266     ch->flags |= ATA_SATA;
267     return 0;
268 }
269 
270 static int
271 ata_nvidia_ch_attach_dumb(device_t dev)
272 {
273     struct ata_channel *ch = device_get_softc(dev);
274 
275     if (ata_pci_ch_attach(dev))
276 	return ENXIO;
277     ch->flags |= ATA_SATA;
278     return 0;
279 }
280 
281 static int
282 ata_nvidia_status(device_t dev)
283 {
284     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
285     struct ata_channel *ch = device_get_softc(dev);
286     int offset = ctlr->chip->cfg1 & NV4 ? 0x0440 : 0x0010;
287     int shift = ch->unit << (ctlr->chip->cfg1 & NVQ ? 4 : 2);
288     u_int32_t istatus;
289 
290     /* get interrupt status */
291     if (ctlr->chip->cfg1 & NVQ)
292 	istatus = ATA_INL(ctlr->r_res2, offset);
293     else
294 	istatus = ATA_INB(ctlr->r_res2, offset);
295 
296     /* do we have any PHY events ? */
297     if (istatus & (0x0c << shift))
298 	ata_sata_phy_check_events(dev, -1);
299 
300     /* clear interrupt(s) */
301     if (ctlr->chip->cfg1 & NVQ)
302 	ATA_OUTL(ctlr->r_res2, offset, (0x0f << shift) | 0x00f000f0);
303     else
304 	ATA_OUTB(ctlr->r_res2, offset, (0x0f << shift));
305 
306     /* do we have any device action ? */
307     return (istatus & (0x01 << shift));
308 }
309 
310 static void
311 ata_nvidia_reset(device_t dev)
312 {
313     struct ata_channel *ch = device_get_softc(dev);
314 
315     if (ata_sata_phy_reset(dev, -1, 1))
316 	ata_generic_reset(dev);
317     else
318 	ch->devices = 0;
319 }
320 
321 static int
322 ata_nvidia_setmode(device_t dev, int target, int mode)
323 {
324 	device_t parent = device_get_parent(dev);
325 	struct ata_pci_controller *ctlr = device_get_softc(parent);
326 	struct ata_channel *ch = device_get_softc(dev);
327 	int devno = (ch->unit << 1) + target;
328 	int piomode;
329 	static const uint8_t timings[] =
330 	    { 0xa8, 0x65, 0x42, 0x22, 0x20, 0xa8, 0x22, 0x20 };
331 	static const uint8_t modes[] =
332 	    { 0xc2, 0xc1, 0xc0, 0xc4, 0xc5, 0xc6, 0xc7 };
333 	int reg = 0x63 - devno;
334 
335 	mode = min(mode, ctlr->chip->max_dma);
336 
337 	if (mode >= ATA_UDMA0) {
338 	    pci_write_config(parent, reg, modes[mode & ATA_MODE_MASK], 1);
339 	    piomode = ATA_PIO4;
340 	} else {
341 	    pci_write_config(parent, reg, 0x8b, 1);
342 	    piomode = mode;
343 	}
344 	pci_write_config(parent, reg - 0x08, timings[ata_mode2idx(piomode)], 1);
345 	return (mode);
346 }
347 
348 ATA_DECLARE_DRIVER(ata_nvidia);
349