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