tegra_hdaudio.c revision 1.11 1 1.11 jmcneill /* $NetBSD: tegra_hdaudio.c,v 1.11 2019/07/25 00:30:45 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.11 jmcneill __KERNEL_RCSID(0, "$NetBSD: tegra_hdaudio.c,v 1.11 2019/07/25 00:30:45 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.10 christos aprint_error(": couldn't map %#" PRIx64 ": %d",
149 1.10 christos (uint64_t)addr, error);
150 1.5 jmcneill return;
151 1.5 jmcneill }
152 1.5 jmcneill
153 1.6 jmcneill sc->sc.sc_dev = self;
154 1.5 jmcneill sc->sc.sc_memt = faa->faa_bst;
155 1.5 jmcneill bus_space_subregion(sc->sc.sc_memt, sc->sc_bsh, TEGRA_HDAUDIO_OFFSET,
156 1.5 jmcneill size - TEGRA_HDAUDIO_OFFSET, &sc->sc.sc_memh);
157 1.1 jmcneill sc->sc.sc_memvalid = true;
158 1.5 jmcneill sc->sc.sc_dmat = faa->faa_dmat;
159 1.11 jmcneill sc->sc.sc_flags = HDAUDIO_FLAG_32BIT;
160 1.1 jmcneill
161 1.1 jmcneill aprint_naive("\n");
162 1.2 jmcneill aprint_normal(": HDA\n");
163 1.1 jmcneill
164 1.6 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
165 1.5 jmcneill aprint_error_dev(self, "failed to decode interrupt\n");
166 1.5 jmcneill return;
167 1.5 jmcneill }
168 1.5 jmcneill
169 1.6 jmcneill sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_AUDIO, 0,
170 1.1 jmcneill tegra_hdaudio_intr, sc);
171 1.1 jmcneill if (sc->sc_ih == NULL) {
172 1.5 jmcneill aprint_error_dev(self, "couldn't establish interrupt on %s\n",
173 1.5 jmcneill intrstr);
174 1.1 jmcneill return;
175 1.1 jmcneill }
176 1.5 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
177 1.1 jmcneill
178 1.3 jmcneill tegra_pmc_power(PMC_PARTID_DISB, true);
179 1.6 jmcneill
180 1.6 jmcneill if (tegra_hdaudio_init_clocks(sc) != 0)
181 1.6 jmcneill return;
182 1.6 jmcneill
183 1.3 jmcneill tegra_hdaudio_init(sc);
184 1.3 jmcneill
185 1.1 jmcneill hdaudio_attach(self, &sc->sc);
186 1.1 jmcneill }
187 1.1 jmcneill
188 1.6 jmcneill static int
189 1.6 jmcneill tegra_hdaudio_init_clocks(struct tegra_hdaudio_softc *sc)
190 1.6 jmcneill {
191 1.6 jmcneill device_t self = sc->sc.sc_dev;
192 1.6 jmcneill int error;
193 1.6 jmcneill
194 1.6 jmcneill /* Assert resets */
195 1.6 jmcneill fdtbus_reset_assert(sc->sc_rst_hda);
196 1.6 jmcneill fdtbus_reset_assert(sc->sc_rst_hda2hdmi);
197 1.6 jmcneill fdtbus_reset_assert(sc->sc_rst_hda2codec_2x);
198 1.6 jmcneill
199 1.6 jmcneill /* Set hda to 48MHz and enable it */
200 1.6 jmcneill error = clk_set_rate(sc->sc_clk_hda, 48000000);
201 1.6 jmcneill if (error) {
202 1.6 jmcneill aprint_error_dev(self, "couldn't set hda frequency: %d\n",
203 1.6 jmcneill error);
204 1.6 jmcneill return error;
205 1.6 jmcneill }
206 1.6 jmcneill error = clk_enable(sc->sc_clk_hda);
207 1.6 jmcneill if (error) {
208 1.6 jmcneill aprint_error_dev(self, "couldn't enable clock hda: %d\n",
209 1.6 jmcneill error);
210 1.6 jmcneill return error;
211 1.6 jmcneill }
212 1.6 jmcneill
213 1.6 jmcneill /* Enable hda2hdmi clock */
214 1.6 jmcneill error = clk_enable(sc->sc_clk_hda2hdmi);
215 1.6 jmcneill if (error) {
216 1.6 jmcneill aprint_error_dev(self, "couldn't enable clock hda2hdmi: %d\n",
217 1.6 jmcneill error);
218 1.6 jmcneill return error;
219 1.6 jmcneill }
220 1.6 jmcneill
221 1.6 jmcneill /* Set hda2codec_2x to 48MHz and enable it */
222 1.6 jmcneill error = clk_set_rate(sc->sc_clk_hda2codec_2x, 48000000);
223 1.6 jmcneill if (error) {
224 1.6 jmcneill aprint_error_dev(self,
225 1.6 jmcneill "couldn't set clock hda2codec_2x frequency: %d\n", error);
226 1.6 jmcneill return error;
227 1.6 jmcneill }
228 1.6 jmcneill error = clk_enable(sc->sc_clk_hda2codec_2x);
229 1.6 jmcneill if (error) {
230 1.6 jmcneill aprint_error_dev(self,
231 1.6 jmcneill "couldn't enable clock hda2codec_2x: %d\n", error);
232 1.6 jmcneill return error;
233 1.6 jmcneill }
234 1.6 jmcneill
235 1.6 jmcneill /* De-assert resets */
236 1.6 jmcneill fdtbus_reset_deassert(sc->sc_rst_hda);
237 1.6 jmcneill fdtbus_reset_deassert(sc->sc_rst_hda2hdmi);
238 1.6 jmcneill fdtbus_reset_deassert(sc->sc_rst_hda2codec_2x);
239 1.6 jmcneill
240 1.6 jmcneill return 0;
241 1.6 jmcneill }
242 1.6 jmcneill
243 1.3 jmcneill static void
244 1.3 jmcneill tegra_hdaudio_init(struct tegra_hdaudio_softc *sc)
245 1.3 jmcneill {
246 1.3 jmcneill tegra_reg_set_clear(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_IFPS_CONFIG_REG,
247 1.3 jmcneill TEGRA_HDA_IFPS_CONFIG_FPCI_EN, 0);
248 1.3 jmcneill tegra_reg_set_clear(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_CFG_CMD_REG,
249 1.3 jmcneill TEGRA_HDA_CFG_CMD_ENABLE_SERR |
250 1.3 jmcneill TEGRA_HDA_CFG_CMD_BUS_MASTER |
251 1.3 jmcneill TEGRA_HDA_CFG_CMD_MEM_SPACE |
252 1.3 jmcneill TEGRA_HDA_CFG_CMD_IO_SPACE,
253 1.3 jmcneill TEGRA_HDA_CFG_CMD_DISABLE_INTR);
254 1.3 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_CFG_BAR0_REG,
255 1.3 jmcneill 0xffffffff);
256 1.3 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_CFG_BAR0_REG,
257 1.3 jmcneill 0x00004000);
258 1.3 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_IFPS_BAR0_REG,
259 1.3 jmcneill TEGRA_HDA_CFG_BAR0_START);
260 1.3 jmcneill tegra_reg_set_clear(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_IFPS_INTR_REG,
261 1.3 jmcneill TEGRA_HDA_IFPS_INTR_EN, 0);
262 1.3 jmcneill }
263 1.3 jmcneill
264 1.1 jmcneill static int
265 1.1 jmcneill tegra_hdaudio_detach(device_t self, int flags)
266 1.1 jmcneill {
267 1.1 jmcneill struct tegra_hdaudio_softc * const sc = device_private(self);
268 1.1 jmcneill
269 1.1 jmcneill hdaudio_detach(&sc->sc, flags);
270 1.1 jmcneill
271 1.1 jmcneill if (sc->sc_ih) {
272 1.5 jmcneill fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
273 1.1 jmcneill sc->sc_ih = NULL;
274 1.1 jmcneill }
275 1.1 jmcneill
276 1.1 jmcneill sc->sc.sc_memvalid = false;
277 1.1 jmcneill
278 1.1 jmcneill return 0;
279 1.1 jmcneill }
280 1.1 jmcneill
281 1.1 jmcneill static int
282 1.1 jmcneill tegra_hdaudio_rescan(device_t self, const char *ifattr, const int *locs)
283 1.1 jmcneill {
284 1.1 jmcneill struct tegra_hdaudio_softc * const sc = device_private(self);
285 1.1 jmcneill
286 1.1 jmcneill return hdaudio_rescan(&sc->sc, ifattr, locs);
287 1.1 jmcneill }
288 1.1 jmcneill
289 1.1 jmcneill static void
290 1.1 jmcneill tegra_hdaudio_childdet(device_t self, device_t child)
291 1.1 jmcneill {
292 1.1 jmcneill struct tegra_hdaudio_softc * const sc = device_private(self);
293 1.1 jmcneill
294 1.1 jmcneill hdaudio_childdet(&sc->sc, child);
295 1.1 jmcneill }
296 1.1 jmcneill
297 1.1 jmcneill static int
298 1.1 jmcneill tegra_hdaudio_intr(void *priv)
299 1.1 jmcneill {
300 1.1 jmcneill struct tegra_hdaudio_softc * const sc = priv;
301 1.1 jmcneill
302 1.1 jmcneill return hdaudio_intr(&sc->sc);
303 1.1 jmcneill }
304