tegra_hdaudio.c revision 1.6 1 /* $NetBSD: tegra_hdaudio.c,v 1.6 2015/12/22 22:10:36 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
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 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: tegra_hdaudio.c,v 1.6 2015/12/22 22:10:36 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/intr.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38
39 #include <dev/hdaudio/hdaudioreg.h>
40 #include <dev/hdaudio/hdaudiovar.h>
41
42 #include <arm/nvidia/tegra_var.h>
43 #include <arm/nvidia/tegra_pmcreg.h>
44 #include <arm/nvidia/tegra_hdaudioreg.h>
45
46 #include <dev/fdt/fdtvar.h>
47
48 #define TEGRA_HDAUDIO_OFFSET 0x8000
49
50 #define TEGRA_HDA_IFPS_BAR0_REG 0x0080
51 #define TEGRA_HDA_IFPS_CONFIG_REG 0x0180
52 #define TEGRA_HDA_IFPS_INTR_REG 0x0188
53 #define TEGRA_HDA_CFG_CMD_REG 0x1004
54 #define TEGRA_HDA_CFG_BAR0_REG 0x1010
55
56 static int tegra_hdaudio_match(device_t, cfdata_t, void *);
57 static void tegra_hdaudio_attach(device_t, device_t, void *);
58 static int tegra_hdaudio_detach(device_t, int);
59 static int tegra_hdaudio_rescan(device_t, const char *, const int *);
60 static void tegra_hdaudio_childdet(device_t, device_t);
61
62 static int tegra_hdaudio_intr(void *);
63
64 struct tegra_hdaudio_softc {
65 struct hdaudio_softc sc;
66 bus_space_tag_t sc_bst;
67 bus_space_handle_t sc_bsh;
68 void *sc_ih;
69 int sc_phandle;
70 struct clk *sc_clk_hda;
71 struct clk *sc_clk_hda2hdmi;
72 struct clk *sc_clk_hda2codec_2x;
73 struct fdtbus_reset *sc_rst_hda;
74 struct fdtbus_reset *sc_rst_hda2hdmi;
75 struct fdtbus_reset *sc_rst_hda2codec_2x;
76 };
77
78 static int tegra_hdaudio_init_clocks(struct tegra_hdaudio_softc *);
79 static void tegra_hdaudio_init(struct tegra_hdaudio_softc *);
80
81 CFATTACH_DECL2_NEW(tegra_hdaudio, sizeof(struct tegra_hdaudio_softc),
82 tegra_hdaudio_match, tegra_hdaudio_attach, tegra_hdaudio_detach, NULL,
83 tegra_hdaudio_rescan, tegra_hdaudio_childdet);
84
85 static int
86 tegra_hdaudio_match(device_t parent, cfdata_t cf, void *aux)
87 {
88 const char * const compatible[] = { "nvidia,tegra124-hda", NULL };
89 struct fdt_attach_args * const faa = aux;
90
91 return of_match_compatible(faa->faa_phandle, compatible);
92 }
93
94 static void
95 tegra_hdaudio_attach(device_t parent, device_t self, void *aux)
96 {
97 struct tegra_hdaudio_softc * const sc = device_private(self);
98 struct fdt_attach_args * const faa = aux;
99 const int phandle = faa->faa_phandle;
100 char intrstr[128];
101 bus_addr_t addr;
102 bus_size_t size;
103 int error;
104
105 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
106 aprint_error(": couldn't get registers\n");
107 return;
108 }
109 sc->sc_clk_hda = fdtbus_clock_get(phandle, "hda");
110 if (sc->sc_clk_hda == NULL) {
111 aprint_error(": couldn't get clock hda\n");
112 return;
113 }
114 sc->sc_clk_hda2hdmi = fdtbus_clock_get(phandle, "hda2hdmi");
115 if (sc->sc_clk_hda2hdmi == NULL) {
116 aprint_error(": couldn't get clock hda2hdmi\n");
117 return;
118 }
119 sc->sc_clk_hda2codec_2x = fdtbus_clock_get(phandle, "hda2codec_2x");
120 if (sc->sc_clk_hda2codec_2x == NULL) {
121 aprint_error(": couldn't get clock hda2codec_2x\n");
122 return;
123 }
124 sc->sc_rst_hda = fdtbus_reset_get(phandle, "hda");
125 if (sc->sc_rst_hda == NULL) {
126 aprint_error(": couldn't get reset hda\n");
127 return;
128 }
129 sc->sc_rst_hda2hdmi = fdtbus_reset_get(phandle, "hda2hdmi");
130 if (sc->sc_rst_hda2hdmi == NULL) {
131 aprint_error(": couldn't get reset hda2hdmi\n");
132 return;
133 }
134 sc->sc_rst_hda2codec_2x = fdtbus_reset_get(phandle, "hda2codec_2x");
135 if (sc->sc_rst_hda2codec_2x == NULL) {
136 aprint_error(": couldn't get reset hda2codec_2x\n");
137 return;
138 }
139
140 sc->sc_phandle = phandle;
141 sc->sc_bst = faa->faa_bst;
142 error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
143 if (error) {
144 aprint_error(": couldn't map %#llx: %d", (uint64_t)addr, error);
145 return;
146 }
147
148 sc->sc.sc_dev = self;
149 sc->sc.sc_memt = faa->faa_bst;
150 bus_space_subregion(sc->sc.sc_memt, sc->sc_bsh, TEGRA_HDAUDIO_OFFSET,
151 size - TEGRA_HDAUDIO_OFFSET, &sc->sc.sc_memh);
152 sc->sc.sc_memvalid = true;
153 sc->sc.sc_dmat = faa->faa_dmat;
154 sc->sc.sc_flags = HDAUDIO_FLAG_NO_STREAM_RESET;
155
156 aprint_naive("\n");
157 aprint_normal(": HDA\n");
158
159 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
160 aprint_error_dev(self, "failed to decode interrupt\n");
161 return;
162 }
163
164 sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_AUDIO, 0,
165 tegra_hdaudio_intr, sc);
166 if (sc->sc_ih == NULL) {
167 aprint_error_dev(self, "couldn't establish interrupt on %s\n",
168 intrstr);
169 return;
170 }
171 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
172
173 tegra_pmc_power(PMC_PARTID_DISB, true);
174
175 if (tegra_hdaudio_init_clocks(sc) != 0)
176 return;
177
178 tegra_hdaudio_init(sc);
179
180 hdaudio_attach(self, &sc->sc);
181 }
182
183 static int
184 tegra_hdaudio_init_clocks(struct tegra_hdaudio_softc *sc)
185 {
186 device_t self = sc->sc.sc_dev;
187 struct clk *pll_p_out0;
188 int error;
189
190 pll_p_out0 = clk_get("pll_p_out0");
191 if (pll_p_out0 == NULL) {
192 aprint_error_dev(self, "couldn't find pll_p_out0\n");
193 return ENOENT;
194 }
195
196 /* Assert resets */
197 fdtbus_reset_assert(sc->sc_rst_hda);
198 fdtbus_reset_assert(sc->sc_rst_hda2hdmi);
199 fdtbus_reset_assert(sc->sc_rst_hda2codec_2x);
200
201 /* Set hda to 48MHz and enable it */
202 error = clk_set_parent(sc->sc_clk_hda, pll_p_out0);
203 if (error) {
204 aprint_error_dev(self, "coulnd't set hda parent: %d\n", error);
205 return error;
206 }
207 error = clk_set_rate(sc->sc_clk_hda, 48000000);
208 if (error) {
209 aprint_error_dev(self, "couldn't set hda frequency: %d\n",
210 error);
211 return error;
212 }
213 error = clk_enable(sc->sc_clk_hda);
214 if (error) {
215 aprint_error_dev(self, "couldn't enable clock hda: %d\n",
216 error);
217 return error;
218 }
219
220 /* Enable hda2hdmi clock */
221 error = clk_enable(sc->sc_clk_hda2hdmi);
222 if (error) {
223 aprint_error_dev(self, "couldn't enable clock hda2hdmi: %d\n",
224 error);
225 return error;
226 }
227
228 /* Set hda2codec_2x to 48MHz and enable it */
229 error = clk_set_parent(sc->sc_clk_hda2codec_2x, pll_p_out0);
230 if (error) {
231 aprint_error_dev(self, "couldn't set hda2codec_2x parent: %d\n",
232 error);
233 return error;
234 }
235 error = clk_set_rate(sc->sc_clk_hda2codec_2x, 48000000);
236 if (error) {
237 aprint_error_dev(self,
238 "couldn't set clock hda2codec_2x frequency: %d\n", error);
239 return error;
240 }
241 error = clk_enable(sc->sc_clk_hda2codec_2x);
242 if (error) {
243 aprint_error_dev(self,
244 "couldn't enable clock hda2codec_2x: %d\n", error);
245 return error;
246 }
247
248 /* De-assert resets */
249 fdtbus_reset_deassert(sc->sc_rst_hda);
250 fdtbus_reset_deassert(sc->sc_rst_hda2hdmi);
251 fdtbus_reset_deassert(sc->sc_rst_hda2codec_2x);
252
253 return 0;
254 }
255
256 static void
257 tegra_hdaudio_init(struct tegra_hdaudio_softc *sc)
258 {
259 tegra_reg_set_clear(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_IFPS_CONFIG_REG,
260 TEGRA_HDA_IFPS_CONFIG_FPCI_EN, 0);
261 tegra_reg_set_clear(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_CFG_CMD_REG,
262 TEGRA_HDA_CFG_CMD_ENABLE_SERR |
263 TEGRA_HDA_CFG_CMD_BUS_MASTER |
264 TEGRA_HDA_CFG_CMD_MEM_SPACE |
265 TEGRA_HDA_CFG_CMD_IO_SPACE,
266 TEGRA_HDA_CFG_CMD_DISABLE_INTR);
267 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_CFG_BAR0_REG,
268 0xffffffff);
269 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_CFG_BAR0_REG,
270 0x00004000);
271 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_IFPS_BAR0_REG,
272 TEGRA_HDA_CFG_BAR0_START);
273 tegra_reg_set_clear(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_IFPS_INTR_REG,
274 TEGRA_HDA_IFPS_INTR_EN, 0);
275 }
276
277 static int
278 tegra_hdaudio_detach(device_t self, int flags)
279 {
280 struct tegra_hdaudio_softc * const sc = device_private(self);
281
282 hdaudio_detach(&sc->sc, flags);
283
284 if (sc->sc_ih) {
285 fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
286 sc->sc_ih = NULL;
287 }
288
289 sc->sc.sc_memvalid = false;
290
291 return 0;
292 }
293
294 static int
295 tegra_hdaudio_rescan(device_t self, const char *ifattr, const int *locs)
296 {
297 struct tegra_hdaudio_softc * const sc = device_private(self);
298
299 return hdaudio_rescan(&sc->sc, ifattr, locs);
300 }
301
302 static void
303 tegra_hdaudio_childdet(device_t self, device_t child)
304 {
305 struct tegra_hdaudio_softc * const sc = device_private(self);
306
307 hdaudio_childdet(&sc->sc, child);
308 }
309
310 static int
311 tegra_hdaudio_intr(void *priv)
312 {
313 struct tegra_hdaudio_softc * const sc = priv;
314
315 return hdaudio_intr(&sc->sc);
316 }
317