1 | /* $NetBSD: hdaudio_pci.c,v 1.4 2016/07/14 04:19:27 msaitoh Exp $ */ |
2 | |
3 | /* |
4 | * Copyright (c) 2009 Precedence Technologies Ltd <support@precedence.co.uk> |
5 | * Copyright (c) 2009 Jared D. McNeill <jmcneill@invisible.ca> |
6 | * All rights reserved. |
7 | * |
8 | * This code is derived from software contributed to The NetBSD Foundation |
9 | * by Precedence Technologies Ltd |
10 | * |
11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions |
13 | * are met: |
14 | * 1. Redistributions of source code must retain the above copyright |
15 | * notice, this list of conditions and the following disclaimer. |
16 | * 2. The name of the author may not be used to endorse or promote products |
17 | * derived from this software without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
26 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
27 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 | * SUCH DAMAGE. |
30 | */ |
31 | |
32 | /* |
33 | * Intel High Definition Audio (Revision 1.0a) device driver. |
34 | */ |
35 | |
36 | #include <sys/cdefs.h> |
37 | __KERNEL_RCSID(0, "$NetBSD: hdaudio_pci.c,v 1.4 2016/07/14 04:19:27 msaitoh Exp $" ); |
38 | |
39 | #include <sys/types.h> |
40 | #include <sys/param.h> |
41 | #include <sys/systm.h> |
42 | #include <sys/device.h> |
43 | #include <sys/conf.h> |
44 | #include <sys/bus.h> |
45 | #include <sys/intr.h> |
46 | #include <sys/module.h> |
47 | |
48 | #include <dev/pci/pcidevs.h> |
49 | #include <dev/pci/pcivar.h> |
50 | |
51 | #include <dev/hdaudio/hdaudioreg.h> |
52 | #include <dev/hdaudio/hdaudiovar.h> |
53 | #include <dev/pci/hdaudio_pci.h> |
54 | |
55 | struct hdaudio_pci_softc { |
56 | struct hdaudio_softc sc_hdaudio; /* must be first */ |
57 | pcitag_t sc_tag; |
58 | pci_chipset_tag_t sc_pc; |
59 | void *sc_ih; |
60 | pcireg_t sc_id; |
61 | }; |
62 | |
63 | static int hdaudio_pci_match(device_t, cfdata_t, void *); |
64 | static void hdaudio_pci_attach(device_t, device_t, void *); |
65 | static int hdaudio_pci_detach(device_t, int); |
66 | static int hdaudio_pci_rescan(device_t, const char *, const int *); |
67 | static void hdaudio_pci_childdet(device_t, device_t); |
68 | |
69 | static int hdaudio_pci_intr(void *); |
70 | static void hdaudio_pci_reinit(struct hdaudio_pci_softc *); |
71 | |
72 | /* power management */ |
73 | static bool hdaudio_pci_resume(device_t, const pmf_qual_t *); |
74 | |
75 | CFATTACH_DECL2_NEW( |
76 | hdaudio_pci, |
77 | sizeof(struct hdaudio_pci_softc), |
78 | hdaudio_pci_match, |
79 | hdaudio_pci_attach, |
80 | hdaudio_pci_detach, |
81 | NULL, |
82 | hdaudio_pci_rescan, |
83 | hdaudio_pci_childdet |
84 | ); |
85 | |
86 | /* |
87 | * NetBSD autoconfiguration |
88 | */ |
89 | |
90 | static int |
91 | hdaudio_pci_match(device_t parent, cfdata_t match, void *opaque) |
92 | { |
93 | struct pci_attach_args *pa = opaque; |
94 | |
95 | if (PCI_CLASS(pa->pa_class) != PCI_CLASS_MULTIMEDIA) |
96 | return 0; |
97 | if (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_MULTIMEDIA_HDAUDIO) |
98 | return 0; |
99 | |
100 | return 10; |
101 | } |
102 | |
103 | static void |
104 | hdaudio_pci_attach(device_t parent, device_t self, void *opaque) |
105 | { |
106 | struct hdaudio_pci_softc *sc = device_private(self); |
107 | struct pci_attach_args *pa = opaque; |
108 | pci_intr_handle_t ih; |
109 | const char *intrstr; |
110 | pcireg_t csr; |
111 | int err; |
112 | char intrbuf[PCI_INTRSTR_LEN]; |
113 | |
114 | aprint_naive("\n" ); |
115 | aprint_normal(": HD Audio Controller\n" ); |
116 | |
117 | sc->sc_pc = pa->pa_pc; |
118 | sc->sc_tag = pa->pa_tag; |
119 | sc->sc_id = pa->pa_id; |
120 | |
121 | sc->sc_hdaudio.sc_subsystem = pci_conf_read(sc->sc_pc, sc->sc_tag, |
122 | PCI_SUBSYS_ID_REG); |
123 | |
124 | /* Enable busmastering and MMIO access */ |
125 | csr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG); |
126 | csr |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_BACKTOBACK_ENABLE; |
127 | pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG, csr); |
128 | |
129 | /* Map MMIO registers */ |
130 | err = pci_mapreg_map(pa, HDAUDIO_PCI_AZBARL, PCI_MAPREG_TYPE_MEM, 0, |
131 | &sc->sc_hdaudio.sc_memt, |
132 | &sc->sc_hdaudio.sc_memh, |
133 | &sc->sc_hdaudio.sc_membase, |
134 | &sc->sc_hdaudio.sc_memsize); |
135 | if (err) { |
136 | aprint_error_dev(self, "couldn't map mmio space\n" ); |
137 | return; |
138 | } |
139 | sc->sc_hdaudio.sc_memvalid = true; |
140 | sc->sc_hdaudio.sc_dmat = pa->pa_dmat; |
141 | |
142 | /* Map interrupt and establish handler */ |
143 | err = pci_intr_map(pa, &ih); |
144 | if (err) { |
145 | aprint_error_dev(self, "couldn't map interrupt\n" ); |
146 | return; |
147 | } |
148 | intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf)); |
149 | sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_AUDIO, |
150 | hdaudio_pci_intr, sc); |
151 | if (sc->sc_ih == NULL) { |
152 | aprint_error_dev(self, "couldn't establish interrupt" ); |
153 | if (intrstr) |
154 | aprint_error(" at %s" , intrstr); |
155 | aprint_error("\n" ); |
156 | return; |
157 | } |
158 | aprint_normal_dev(self, "interrupting at %s\n" , intrstr); |
159 | |
160 | if (!pmf_device_register(self, NULL, hdaudio_pci_resume)) |
161 | aprint_error_dev(self, "couldn't establish power handler\n" ); |
162 | |
163 | hdaudio_pci_reinit(sc); |
164 | |
165 | /* Attach bus-independent HD audio layer */ |
166 | if (hdaudio_attach(self, &sc->sc_hdaudio)) { |
167 | pci_intr_disestablish(sc->sc_pc, sc->sc_ih); |
168 | sc->sc_ih = NULL; |
169 | bus_space_unmap(sc->sc_hdaudio.sc_memt, |
170 | sc->sc_hdaudio.sc_memh, |
171 | sc->sc_hdaudio.sc_memsize); |
172 | sc->sc_hdaudio.sc_memvalid = false; |
173 | csr = pci_conf_read(sc->sc_pc, sc->sc_tag, |
174 | PCI_COMMAND_STATUS_REG); |
175 | csr &= ~(PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_BACKTOBACK_ENABLE); |
176 | pci_conf_write(sc->sc_pc, sc->sc_tag, |
177 | PCI_COMMAND_STATUS_REG, csr); |
178 | pmf_device_deregister(self); |
179 | } |
180 | } |
181 | |
182 | static int |
183 | hdaudio_pci_rescan(device_t self, const char *ifattr, const int *locs) |
184 | { |
185 | struct hdaudio_pci_softc *sc = device_private(self); |
186 | |
187 | return hdaudio_rescan(&sc->sc_hdaudio, ifattr, locs); |
188 | } |
189 | |
190 | void |
191 | hdaudio_pci_childdet(device_t self, device_t child) |
192 | { |
193 | struct hdaudio_pci_softc *sc = device_private(self); |
194 | |
195 | hdaudio_childdet(&sc->sc_hdaudio, child); |
196 | } |
197 | |
198 | static int |
199 | hdaudio_pci_detach(device_t self, int flags) |
200 | { |
201 | struct hdaudio_pci_softc *sc = device_private(self); |
202 | pcireg_t csr; |
203 | |
204 | hdaudio_detach(&sc->sc_hdaudio, flags); |
205 | |
206 | if (sc->sc_ih != NULL) { |
207 | pci_intr_disestablish(sc->sc_pc, sc->sc_ih); |
208 | sc->sc_ih = NULL; |
209 | } |
210 | if (sc->sc_hdaudio.sc_memvalid == true) { |
211 | bus_space_unmap(sc->sc_hdaudio.sc_memt, |
212 | sc->sc_hdaudio.sc_memh, |
213 | sc->sc_hdaudio.sc_memsize); |
214 | sc->sc_hdaudio.sc_memvalid = false; |
215 | } |
216 | |
217 | /* Disable busmastering and MMIO access */ |
218 | csr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG); |
219 | csr &= ~(PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_BACKTOBACK_ENABLE); |
220 | pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG, csr); |
221 | |
222 | pmf_device_deregister(self); |
223 | |
224 | return 0; |
225 | } |
226 | |
227 | static int |
228 | hdaudio_pci_intr(void *opaque) |
229 | { |
230 | struct hdaudio_pci_softc *sc = opaque; |
231 | |
232 | return hdaudio_intr(&sc->sc_hdaudio); |
233 | } |
234 | |
235 | |
236 | static void |
237 | hdaudio_pci_reinit(struct hdaudio_pci_softc *sc) |
238 | { |
239 | pcireg_t val; |
240 | |
241 | /* stops playback static */ |
242 | val = pci_conf_read(sc->sc_pc, sc->sc_tag, HDAUDIO_PCI_TCSEL); |
243 | val &= ~7; |
244 | val |= 0; |
245 | pci_conf_write(sc->sc_pc, sc->sc_tag, HDAUDIO_PCI_TCSEL, val); |
246 | |
247 | switch (PCI_VENDOR(sc->sc_id)) { |
248 | case PCI_VENDOR_NVIDIA: |
249 | /* enable snooping */ |
250 | val = pci_conf_read(sc->sc_pc, sc->sc_tag, |
251 | HDAUDIO_NV_REG_SNOOP); |
252 | val &= ~HDAUDIO_NV_SNOOP_MASK; |
253 | val |= HDAUDIO_NV_SNOOP_ENABLE; |
254 | pci_conf_write(sc->sc_pc, sc->sc_tag, |
255 | HDAUDIO_NV_REG_SNOOP, val); |
256 | break; |
257 | } |
258 | } |
259 | |
260 | static bool |
261 | hdaudio_pci_resume(device_t self, const pmf_qual_t *qual) |
262 | { |
263 | struct hdaudio_pci_softc *sc = device_private(self); |
264 | |
265 | hdaudio_pci_reinit(sc); |
266 | return hdaudio_resume(&sc->sc_hdaudio); |
267 | } |
268 | |
269 | MODULE(MODULE_CLASS_DRIVER, hdaudio_pci, "hdaudio" ); |
270 | |
271 | #ifdef _MODULE |
272 | #include "ioconf.c" |
273 | #endif |
274 | |
275 | static int |
276 | hdaudio_pci_modcmd(modcmd_t cmd, void *opaque) |
277 | { |
278 | int error = 0; |
279 | |
280 | switch (cmd) { |
281 | case MODULE_CMD_INIT: |
282 | #ifdef _MODULE |
283 | error = config_init_component(cfdriver_ioconf_hdaudio_pci, |
284 | cfattach_ioconf_hdaudio_pci, cfdata_ioconf_hdaudio_pci); |
285 | #endif |
286 | return error; |
287 | case MODULE_CMD_FINI: |
288 | #ifdef _MODULE |
289 | error = config_fini_component(cfdriver_ioconf_hdaudio_pci, |
290 | cfattach_ioconf_hdaudio_pci, cfdata_ioconf_hdaudio_pci); |
291 | #endif |
292 | return error; |
293 | default: |
294 | return ENOTTY; |
295 | } |
296 | } |
297 | |