tegra_hdaudio.c revision 1.7.2.1 1 1.7.2.1 pgoyette /* $NetBSD: tegra_hdaudio.c,v 1.7.2.1 2017/04/26 02:53:01 pgoyette 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.7.2.1 pgoyette __KERNEL_RCSID(0, "$NetBSD: tegra_hdaudio.c,v 1.7.2.1 2017/04/26 02:53:01 pgoyette 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.5 jmcneill const char * const compatible[] = { "nvidia,tegra124-hda", NULL };
89 1.5 jmcneill struct fdt_attach_args * const faa = aux;
90 1.5 jmcneill
91 1.5 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
92 1.1 jmcneill }
93 1.1 jmcneill
94 1.1 jmcneill static void
95 1.1 jmcneill tegra_hdaudio_attach(device_t parent, device_t self, void *aux)
96 1.1 jmcneill {
97 1.1 jmcneill struct tegra_hdaudio_softc * const sc = device_private(self);
98 1.5 jmcneill struct fdt_attach_args * const faa = aux;
99 1.6 jmcneill const int phandle = faa->faa_phandle;
100 1.5 jmcneill char intrstr[128];
101 1.5 jmcneill bus_addr_t addr;
102 1.5 jmcneill bus_size_t size;
103 1.5 jmcneill int error;
104 1.5 jmcneill
105 1.6 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
106 1.5 jmcneill aprint_error(": couldn't get registers\n");
107 1.5 jmcneill return;
108 1.5 jmcneill }
109 1.6 jmcneill sc->sc_clk_hda = fdtbus_clock_get(phandle, "hda");
110 1.6 jmcneill if (sc->sc_clk_hda == NULL) {
111 1.6 jmcneill aprint_error(": couldn't get clock hda\n");
112 1.6 jmcneill return;
113 1.6 jmcneill }
114 1.6 jmcneill sc->sc_clk_hda2hdmi = fdtbus_clock_get(phandle, "hda2hdmi");
115 1.6 jmcneill if (sc->sc_clk_hda2hdmi == NULL) {
116 1.6 jmcneill aprint_error(": couldn't get clock hda2hdmi\n");
117 1.6 jmcneill return;
118 1.6 jmcneill }
119 1.6 jmcneill sc->sc_clk_hda2codec_2x = fdtbus_clock_get(phandle, "hda2codec_2x");
120 1.6 jmcneill if (sc->sc_clk_hda2codec_2x == NULL) {
121 1.6 jmcneill aprint_error(": couldn't get clock hda2codec_2x\n");
122 1.6 jmcneill return;
123 1.6 jmcneill }
124 1.6 jmcneill sc->sc_rst_hda = fdtbus_reset_get(phandle, "hda");
125 1.6 jmcneill if (sc->sc_rst_hda == NULL) {
126 1.6 jmcneill aprint_error(": couldn't get reset hda\n");
127 1.6 jmcneill return;
128 1.6 jmcneill }
129 1.6 jmcneill sc->sc_rst_hda2hdmi = fdtbus_reset_get(phandle, "hda2hdmi");
130 1.6 jmcneill if (sc->sc_rst_hda2hdmi == NULL) {
131 1.6 jmcneill aprint_error(": couldn't get reset hda2hdmi\n");
132 1.6 jmcneill return;
133 1.6 jmcneill }
134 1.6 jmcneill sc->sc_rst_hda2codec_2x = fdtbus_reset_get(phandle, "hda2codec_2x");
135 1.6 jmcneill if (sc->sc_rst_hda2codec_2x == NULL) {
136 1.6 jmcneill aprint_error(": couldn't get reset hda2codec_2x\n");
137 1.6 jmcneill return;
138 1.6 jmcneill }
139 1.1 jmcneill
140 1.6 jmcneill sc->sc_phandle = phandle;
141 1.5 jmcneill sc->sc_bst = faa->faa_bst;
142 1.5 jmcneill error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
143 1.5 jmcneill if (error) {
144 1.5 jmcneill aprint_error(": couldn't map %#llx: %d", (uint64_t)addr, error);
145 1.5 jmcneill return;
146 1.5 jmcneill }
147 1.5 jmcneill
148 1.6 jmcneill sc->sc.sc_dev = self;
149 1.5 jmcneill sc->sc.sc_memt = faa->faa_bst;
150 1.5 jmcneill bus_space_subregion(sc->sc.sc_memt, sc->sc_bsh, TEGRA_HDAUDIO_OFFSET,
151 1.5 jmcneill size - TEGRA_HDAUDIO_OFFSET, &sc->sc.sc_memh);
152 1.1 jmcneill sc->sc.sc_memvalid = true;
153 1.5 jmcneill sc->sc.sc_dmat = faa->faa_dmat;
154 1.1 jmcneill
155 1.1 jmcneill aprint_naive("\n");
156 1.2 jmcneill aprint_normal(": HDA\n");
157 1.1 jmcneill
158 1.6 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
159 1.5 jmcneill aprint_error_dev(self, "failed to decode interrupt\n");
160 1.5 jmcneill return;
161 1.5 jmcneill }
162 1.5 jmcneill
163 1.6 jmcneill sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_AUDIO, 0,
164 1.1 jmcneill tegra_hdaudio_intr, sc);
165 1.1 jmcneill if (sc->sc_ih == NULL) {
166 1.5 jmcneill aprint_error_dev(self, "couldn't establish interrupt on %s\n",
167 1.5 jmcneill intrstr);
168 1.1 jmcneill return;
169 1.1 jmcneill }
170 1.5 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
171 1.1 jmcneill
172 1.3 jmcneill tegra_pmc_power(PMC_PARTID_DISB, true);
173 1.6 jmcneill
174 1.6 jmcneill if (tegra_hdaudio_init_clocks(sc) != 0)
175 1.6 jmcneill return;
176 1.6 jmcneill
177 1.3 jmcneill tegra_hdaudio_init(sc);
178 1.3 jmcneill
179 1.1 jmcneill hdaudio_attach(self, &sc->sc);
180 1.1 jmcneill }
181 1.1 jmcneill
182 1.6 jmcneill static int
183 1.6 jmcneill tegra_hdaudio_init_clocks(struct tegra_hdaudio_softc *sc)
184 1.6 jmcneill {
185 1.6 jmcneill device_t self = sc->sc.sc_dev;
186 1.6 jmcneill int error;
187 1.6 jmcneill
188 1.6 jmcneill /* Assert resets */
189 1.6 jmcneill fdtbus_reset_assert(sc->sc_rst_hda);
190 1.6 jmcneill fdtbus_reset_assert(sc->sc_rst_hda2hdmi);
191 1.6 jmcneill fdtbus_reset_assert(sc->sc_rst_hda2codec_2x);
192 1.6 jmcneill
193 1.6 jmcneill /* Set hda to 48MHz and enable it */
194 1.6 jmcneill error = clk_set_rate(sc->sc_clk_hda, 48000000);
195 1.6 jmcneill if (error) {
196 1.6 jmcneill aprint_error_dev(self, "couldn't set hda frequency: %d\n",
197 1.6 jmcneill error);
198 1.6 jmcneill return error;
199 1.6 jmcneill }
200 1.6 jmcneill error = clk_enable(sc->sc_clk_hda);
201 1.6 jmcneill if (error) {
202 1.6 jmcneill aprint_error_dev(self, "couldn't enable clock hda: %d\n",
203 1.6 jmcneill error);
204 1.6 jmcneill return error;
205 1.6 jmcneill }
206 1.6 jmcneill
207 1.6 jmcneill /* Enable hda2hdmi clock */
208 1.6 jmcneill error = clk_enable(sc->sc_clk_hda2hdmi);
209 1.6 jmcneill if (error) {
210 1.6 jmcneill aprint_error_dev(self, "couldn't enable clock hda2hdmi: %d\n",
211 1.6 jmcneill error);
212 1.6 jmcneill return error;
213 1.6 jmcneill }
214 1.6 jmcneill
215 1.6 jmcneill /* Set hda2codec_2x to 48MHz and enable it */
216 1.6 jmcneill error = clk_set_rate(sc->sc_clk_hda2codec_2x, 48000000);
217 1.6 jmcneill if (error) {
218 1.6 jmcneill aprint_error_dev(self,
219 1.6 jmcneill "couldn't set clock hda2codec_2x frequency: %d\n", error);
220 1.6 jmcneill return error;
221 1.6 jmcneill }
222 1.6 jmcneill error = clk_enable(sc->sc_clk_hda2codec_2x);
223 1.6 jmcneill if (error) {
224 1.6 jmcneill aprint_error_dev(self,
225 1.6 jmcneill "couldn't enable clock hda2codec_2x: %d\n", error);
226 1.6 jmcneill return error;
227 1.6 jmcneill }
228 1.6 jmcneill
229 1.6 jmcneill /* De-assert resets */
230 1.6 jmcneill fdtbus_reset_deassert(sc->sc_rst_hda);
231 1.6 jmcneill fdtbus_reset_deassert(sc->sc_rst_hda2hdmi);
232 1.6 jmcneill fdtbus_reset_deassert(sc->sc_rst_hda2codec_2x);
233 1.6 jmcneill
234 1.6 jmcneill return 0;
235 1.6 jmcneill }
236 1.6 jmcneill
237 1.3 jmcneill static void
238 1.3 jmcneill tegra_hdaudio_init(struct tegra_hdaudio_softc *sc)
239 1.3 jmcneill {
240 1.3 jmcneill tegra_reg_set_clear(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_IFPS_CONFIG_REG,
241 1.3 jmcneill TEGRA_HDA_IFPS_CONFIG_FPCI_EN, 0);
242 1.3 jmcneill tegra_reg_set_clear(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_CFG_CMD_REG,
243 1.3 jmcneill TEGRA_HDA_CFG_CMD_ENABLE_SERR |
244 1.3 jmcneill TEGRA_HDA_CFG_CMD_BUS_MASTER |
245 1.3 jmcneill TEGRA_HDA_CFG_CMD_MEM_SPACE |
246 1.3 jmcneill TEGRA_HDA_CFG_CMD_IO_SPACE,
247 1.3 jmcneill TEGRA_HDA_CFG_CMD_DISABLE_INTR);
248 1.3 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_CFG_BAR0_REG,
249 1.3 jmcneill 0xffffffff);
250 1.3 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_CFG_BAR0_REG,
251 1.3 jmcneill 0x00004000);
252 1.3 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_IFPS_BAR0_REG,
253 1.3 jmcneill TEGRA_HDA_CFG_BAR0_START);
254 1.3 jmcneill tegra_reg_set_clear(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_IFPS_INTR_REG,
255 1.3 jmcneill TEGRA_HDA_IFPS_INTR_EN, 0);
256 1.3 jmcneill }
257 1.3 jmcneill
258 1.1 jmcneill static int
259 1.1 jmcneill tegra_hdaudio_detach(device_t self, int flags)
260 1.1 jmcneill {
261 1.1 jmcneill struct tegra_hdaudio_softc * const sc = device_private(self);
262 1.1 jmcneill
263 1.1 jmcneill hdaudio_detach(&sc->sc, flags);
264 1.1 jmcneill
265 1.1 jmcneill if (sc->sc_ih) {
266 1.5 jmcneill fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
267 1.1 jmcneill sc->sc_ih = NULL;
268 1.1 jmcneill }
269 1.1 jmcneill
270 1.1 jmcneill sc->sc.sc_memvalid = false;
271 1.1 jmcneill
272 1.1 jmcneill return 0;
273 1.1 jmcneill }
274 1.1 jmcneill
275 1.1 jmcneill static int
276 1.1 jmcneill tegra_hdaudio_rescan(device_t self, const char *ifattr, const int *locs)
277 1.1 jmcneill {
278 1.1 jmcneill struct tegra_hdaudio_softc * const sc = device_private(self);
279 1.1 jmcneill
280 1.1 jmcneill return hdaudio_rescan(&sc->sc, ifattr, locs);
281 1.1 jmcneill }
282 1.1 jmcneill
283 1.1 jmcneill static void
284 1.1 jmcneill tegra_hdaudio_childdet(device_t self, device_t child)
285 1.1 jmcneill {
286 1.1 jmcneill struct tegra_hdaudio_softc * const sc = device_private(self);
287 1.1 jmcneill
288 1.1 jmcneill hdaudio_childdet(&sc->sc, child);
289 1.1 jmcneill }
290 1.1 jmcneill
291 1.1 jmcneill static int
292 1.1 jmcneill tegra_hdaudio_intr(void *priv)
293 1.1 jmcneill {
294 1.1 jmcneill struct tegra_hdaudio_softc * const sc = priv;
295 1.1 jmcneill
296 1.1 jmcneill return hdaudio_intr(&sc->sc);
297 1.1 jmcneill }
298