Home | History | Annotate | Line # | Download | only in nvidia
tegra_hdaudio.c revision 1.5
      1  1.5  jmcneill /* $NetBSD: tegra_hdaudio.c,v 1.5 2015/12/13 17:39:19 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.5  jmcneill __KERNEL_RCSID(0, "$NetBSD: tegra_hdaudio.c,v 1.5 2015/12/13 17:39:19 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.1  jmcneill };
     71  1.1  jmcneill 
     72  1.3  jmcneill static void	tegra_hdaudio_init(struct tegra_hdaudio_softc *);
     73  1.3  jmcneill 
     74  1.1  jmcneill CFATTACH_DECL2_NEW(tegra_hdaudio, sizeof(struct tegra_hdaudio_softc),
     75  1.1  jmcneill 	tegra_hdaudio_match, tegra_hdaudio_attach, tegra_hdaudio_detach, NULL,
     76  1.1  jmcneill 	tegra_hdaudio_rescan, tegra_hdaudio_childdet);
     77  1.1  jmcneill 
     78  1.1  jmcneill static int
     79  1.1  jmcneill tegra_hdaudio_match(device_t parent, cfdata_t cf, void *aux)
     80  1.1  jmcneill {
     81  1.5  jmcneill 	const char * const compatible[] = { "nvidia,tegra124-hda", NULL };
     82  1.5  jmcneill 	struct fdt_attach_args * const faa = aux;
     83  1.5  jmcneill 
     84  1.5  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
     85  1.1  jmcneill }
     86  1.1  jmcneill 
     87  1.1  jmcneill static void
     88  1.1  jmcneill tegra_hdaudio_attach(device_t parent, device_t self, void *aux)
     89  1.1  jmcneill {
     90  1.1  jmcneill 	struct tegra_hdaudio_softc * const sc = device_private(self);
     91  1.5  jmcneill 	struct fdt_attach_args * const faa = aux;
     92  1.5  jmcneill 	char intrstr[128];
     93  1.5  jmcneill 	bus_addr_t addr;
     94  1.5  jmcneill 	bus_size_t size;
     95  1.5  jmcneill 	int error;
     96  1.5  jmcneill 
     97  1.5  jmcneill 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
     98  1.5  jmcneill 		aprint_error(": couldn't get registers\n");
     99  1.5  jmcneill 		return;
    100  1.5  jmcneill 	}
    101  1.1  jmcneill 
    102  1.5  jmcneill 	sc->sc_phandle = faa->faa_phandle;
    103  1.5  jmcneill 	sc->sc_bst = faa->faa_bst;
    104  1.5  jmcneill 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    105  1.5  jmcneill 	if (error) {
    106  1.5  jmcneill 		aprint_error(": couldn't map %#llx: %d", (uint64_t)addr, error);
    107  1.5  jmcneill 		return;
    108  1.5  jmcneill 	}
    109  1.5  jmcneill 
    110  1.5  jmcneill 	sc->sc.sc_memt = faa->faa_bst;
    111  1.5  jmcneill 	bus_space_subregion(sc->sc.sc_memt, sc->sc_bsh, TEGRA_HDAUDIO_OFFSET,
    112  1.5  jmcneill 	    size - TEGRA_HDAUDIO_OFFSET, &sc->sc.sc_memh);
    113  1.1  jmcneill 	sc->sc.sc_memvalid = true;
    114  1.5  jmcneill 	sc->sc.sc_dmat = faa->faa_dmat;
    115  1.4  jmcneill 	sc->sc.sc_flags = HDAUDIO_FLAG_NO_STREAM_RESET;
    116  1.1  jmcneill 
    117  1.1  jmcneill 	aprint_naive("\n");
    118  1.2  jmcneill 	aprint_normal(": HDA\n");
    119  1.1  jmcneill 
    120  1.5  jmcneill 	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
    121  1.5  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    122  1.5  jmcneill 		return;
    123  1.5  jmcneill 	}
    124  1.5  jmcneill 
    125  1.5  jmcneill 	sc->sc_ih = fdtbus_intr_establish(faa->faa_phandle, 0, IPL_AUDIO, 0,
    126  1.1  jmcneill 	    tegra_hdaudio_intr, sc);
    127  1.1  jmcneill 	if (sc->sc_ih == NULL) {
    128  1.5  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    129  1.5  jmcneill 		    intrstr);
    130  1.1  jmcneill 		return;
    131  1.1  jmcneill 	}
    132  1.5  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    133  1.1  jmcneill 
    134  1.3  jmcneill 	tegra_pmc_power(PMC_PARTID_DISB, true);
    135  1.3  jmcneill 	tegra_car_periph_hda_enable();
    136  1.3  jmcneill 	tegra_hdaudio_init(sc);
    137  1.3  jmcneill 
    138  1.1  jmcneill 	hdaudio_attach(self, &sc->sc);
    139  1.1  jmcneill }
    140  1.1  jmcneill 
    141  1.3  jmcneill static void
    142  1.3  jmcneill tegra_hdaudio_init(struct tegra_hdaudio_softc *sc)
    143  1.3  jmcneill {
    144  1.3  jmcneill 	tegra_reg_set_clear(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_IFPS_CONFIG_REG,
    145  1.3  jmcneill 	    TEGRA_HDA_IFPS_CONFIG_FPCI_EN, 0);
    146  1.3  jmcneill 	tegra_reg_set_clear(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_CFG_CMD_REG,
    147  1.3  jmcneill 	    TEGRA_HDA_CFG_CMD_ENABLE_SERR |
    148  1.3  jmcneill 	    TEGRA_HDA_CFG_CMD_BUS_MASTER |
    149  1.3  jmcneill 	    TEGRA_HDA_CFG_CMD_MEM_SPACE |
    150  1.3  jmcneill 	    TEGRA_HDA_CFG_CMD_IO_SPACE,
    151  1.3  jmcneill 	    TEGRA_HDA_CFG_CMD_DISABLE_INTR);
    152  1.3  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_CFG_BAR0_REG,
    153  1.3  jmcneill 	    0xffffffff);
    154  1.3  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_CFG_BAR0_REG,
    155  1.3  jmcneill 	    0x00004000);
    156  1.3  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_IFPS_BAR0_REG,
    157  1.3  jmcneill 	    TEGRA_HDA_CFG_BAR0_START);
    158  1.3  jmcneill 	tegra_reg_set_clear(sc->sc_bst, sc->sc_bsh, TEGRA_HDA_IFPS_INTR_REG,
    159  1.3  jmcneill 	    TEGRA_HDA_IFPS_INTR_EN, 0);
    160  1.3  jmcneill }
    161  1.3  jmcneill 
    162  1.1  jmcneill static int
    163  1.1  jmcneill tegra_hdaudio_detach(device_t self, int flags)
    164  1.1  jmcneill {
    165  1.1  jmcneill 	struct tegra_hdaudio_softc * const sc = device_private(self);
    166  1.1  jmcneill 
    167  1.1  jmcneill 	hdaudio_detach(&sc->sc, flags);
    168  1.1  jmcneill 
    169  1.1  jmcneill 	if (sc->sc_ih) {
    170  1.5  jmcneill 		fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
    171  1.1  jmcneill 		sc->sc_ih = NULL;
    172  1.1  jmcneill 	}
    173  1.1  jmcneill 
    174  1.1  jmcneill 	sc->sc.sc_memvalid = false;
    175  1.1  jmcneill 
    176  1.1  jmcneill 	return 0;
    177  1.1  jmcneill }
    178  1.1  jmcneill 
    179  1.1  jmcneill static int
    180  1.1  jmcneill tegra_hdaudio_rescan(device_t self, const char *ifattr, const int *locs)
    181  1.1  jmcneill {
    182  1.1  jmcneill 	struct tegra_hdaudio_softc * const sc = device_private(self);
    183  1.1  jmcneill 
    184  1.1  jmcneill 	return hdaudio_rescan(&sc->sc, ifattr, locs);
    185  1.1  jmcneill }
    186  1.1  jmcneill 
    187  1.1  jmcneill static void
    188  1.1  jmcneill tegra_hdaudio_childdet(device_t self, device_t child)
    189  1.1  jmcneill {
    190  1.1  jmcneill 	struct tegra_hdaudio_softc * const sc = device_private(self);
    191  1.1  jmcneill 
    192  1.1  jmcneill 	hdaudio_childdet(&sc->sc, child);
    193  1.1  jmcneill }
    194  1.1  jmcneill 
    195  1.1  jmcneill static int
    196  1.1  jmcneill tegra_hdaudio_intr(void *priv)
    197  1.1  jmcneill {
    198  1.1  jmcneill 	struct tegra_hdaudio_softc * const sc = priv;
    199  1.1  jmcneill 
    200  1.1  jmcneill 	return hdaudio_intr(&sc->sc);
    201  1.1  jmcneill }
    202