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