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