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