Home | History | Annotate | Line # | Download | only in nvidia
tegra_fuse.c revision 1.4.2.3
      1  1.4.2.3  skrll /* $NetBSD: tegra_fuse.c,v 1.4.2.3 2017/08/28 17:51:31 skrll Exp $ */
      2  1.4.2.2  skrll 
      3  1.4.2.2  skrll /*-
      4  1.4.2.2  skrll  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.4.2.2  skrll  * All rights reserved.
      6  1.4.2.2  skrll  *
      7  1.4.2.2  skrll  * Redistribution and use in source and binary forms, with or without
      8  1.4.2.2  skrll  * modification, are permitted provided that the following conditions
      9  1.4.2.2  skrll  * are met:
     10  1.4.2.2  skrll  * 1. Redistributions of source code must retain the above copyright
     11  1.4.2.2  skrll  *    notice, this list of conditions and the following disclaimer.
     12  1.4.2.2  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.4.2.2  skrll  *    notice, this list of conditions and the following disclaimer in the
     14  1.4.2.2  skrll  *    documentation and/or other materials provided with the distribution.
     15  1.4.2.2  skrll  *
     16  1.4.2.2  skrll  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.4.2.2  skrll  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.4.2.2  skrll  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.4.2.2  skrll  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.4.2.2  skrll  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.4.2.2  skrll  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.4.2.2  skrll  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.4.2.2  skrll  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.4.2.2  skrll  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.4.2.2  skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.4.2.2  skrll  * SUCH DAMAGE.
     27  1.4.2.2  skrll  */
     28  1.4.2.2  skrll 
     29  1.4.2.2  skrll #include <sys/cdefs.h>
     30  1.4.2.3  skrll __KERNEL_RCSID(0, "$NetBSD: tegra_fuse.c,v 1.4.2.3 2017/08/28 17:51:31 skrll Exp $");
     31  1.4.2.2  skrll 
     32  1.4.2.2  skrll #include <sys/param.h>
     33  1.4.2.2  skrll #include <sys/bus.h>
     34  1.4.2.2  skrll #include <sys/device.h>
     35  1.4.2.2  skrll #include <sys/intr.h>
     36  1.4.2.2  skrll #include <sys/systm.h>
     37  1.4.2.2  skrll #include <sys/kernel.h>
     38  1.4.2.2  skrll 
     39  1.4.2.2  skrll #include <arm/nvidia/tegra_reg.h>
     40  1.4.2.2  skrll #include <arm/nvidia/tegra_var.h>
     41  1.4.2.2  skrll 
     42  1.4.2.2  skrll #include <dev/fdt/fdtvar.h>
     43  1.4.2.2  skrll 
     44  1.4.2.2  skrll static int	tegra_fuse_match(device_t, cfdata_t, void *);
     45  1.4.2.2  skrll static void	tegra_fuse_attach(device_t, device_t, void *);
     46  1.4.2.2  skrll 
     47  1.4.2.2  skrll struct tegra_fuse_softc {
     48  1.4.2.2  skrll 	device_t		sc_dev;
     49  1.4.2.2  skrll 	bus_space_tag_t		sc_bst;
     50  1.4.2.2  skrll 	bus_space_handle_t	sc_bsh;
     51  1.4.2.2  skrll 
     52  1.4.2.2  skrll 	struct clk		*sc_clk;
     53  1.4.2.2  skrll 	struct fdtbus_reset	*sc_rst;
     54  1.4.2.2  skrll };
     55  1.4.2.2  skrll 
     56  1.4.2.2  skrll static struct tegra_fuse_softc *fuse_softc = NULL;
     57  1.4.2.2  skrll 
     58  1.4.2.2  skrll CFATTACH_DECL_NEW(tegra_fuse, sizeof(struct tegra_fuse_softc),
     59  1.4.2.2  skrll 	tegra_fuse_match, tegra_fuse_attach, NULL, NULL);
     60  1.4.2.2  skrll 
     61  1.4.2.2  skrll static int
     62  1.4.2.2  skrll tegra_fuse_match(device_t parent, cfdata_t cf, void *aux)
     63  1.4.2.2  skrll {
     64  1.4.2.3  skrll 	const char * const compatible[] = {
     65  1.4.2.3  skrll 		"nvidia,tegra210-efuse",
     66  1.4.2.3  skrll 		"nvidia,tegra124-efuse",
     67  1.4.2.3  skrll 		NULL
     68  1.4.2.3  skrll 	};
     69  1.4.2.2  skrll 	struct fdt_attach_args * const faa = aux;
     70  1.4.2.2  skrll 
     71  1.4.2.2  skrll 	return of_match_compatible(faa->faa_phandle, compatible);
     72  1.4.2.2  skrll }
     73  1.4.2.2  skrll 
     74  1.4.2.2  skrll static void
     75  1.4.2.2  skrll tegra_fuse_attach(device_t parent, device_t self, void *aux)
     76  1.4.2.2  skrll {
     77  1.4.2.2  skrll 	struct tegra_fuse_softc * const sc = device_private(self);
     78  1.4.2.2  skrll 	struct fdt_attach_args * const faa = aux;
     79  1.4.2.2  skrll 	bus_addr_t addr;
     80  1.4.2.2  skrll 	bus_size_t size;
     81  1.4.2.2  skrll 	int error;
     82  1.4.2.2  skrll 
     83  1.4.2.2  skrll 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
     84  1.4.2.2  skrll 		aprint_error(": couldn't get registers\n");
     85  1.4.2.2  skrll 		return;
     86  1.4.2.2  skrll 	}
     87  1.4.2.2  skrll 	sc->sc_clk = fdtbus_clock_get(faa->faa_phandle, "fuse");
     88  1.4.2.2  skrll 	if (sc->sc_clk == NULL) {
     89  1.4.2.2  skrll 		aprint_error(": couldn't get clock fuse\n");
     90  1.4.2.2  skrll 		return;
     91  1.4.2.2  skrll 	}
     92  1.4.2.2  skrll 	sc->sc_rst = fdtbus_reset_get(faa->faa_phandle, "fuse");
     93  1.4.2.2  skrll 	if (sc->sc_rst == NULL) {
     94  1.4.2.2  skrll 		aprint_error(": couldn't get reset fuse\n");
     95  1.4.2.2  skrll 		return;
     96  1.4.2.2  skrll 	}
     97  1.4.2.2  skrll 
     98  1.4.2.2  skrll 	sc->sc_dev = self;
     99  1.4.2.2  skrll 	sc->sc_bst = faa->faa_bst;
    100  1.4.2.2  skrll 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    101  1.4.2.2  skrll 	if (error) {
    102  1.4.2.2  skrll 		aprint_error(": couldn't map %#llx: %d", (uint64_t)addr, error);
    103  1.4.2.2  skrll 		return;
    104  1.4.2.2  skrll 	}
    105  1.4.2.2  skrll 
    106  1.4.2.2  skrll 	KASSERT(fuse_softc == NULL);
    107  1.4.2.2  skrll 	fuse_softc = sc;
    108  1.4.2.2  skrll 
    109  1.4.2.2  skrll 	aprint_naive("\n");
    110  1.4.2.2  skrll 	aprint_normal(": FUSE\n");
    111  1.4.2.2  skrll }
    112  1.4.2.2  skrll 
    113  1.4.2.2  skrll uint32_t
    114  1.4.2.2  skrll tegra_fuse_read(u_int offset)
    115  1.4.2.2  skrll {
    116  1.4.2.2  skrll 	bus_space_tag_t bst;
    117  1.4.2.2  skrll 	bus_space_handle_t bsh;
    118  1.4.2.2  skrll 
    119  1.4.2.2  skrll 	KASSERT(fuse_softc != NULL);
    120  1.4.2.2  skrll 
    121  1.4.2.2  skrll 	bst = fuse_softc->sc_bst;
    122  1.4.2.2  skrll 	bsh = fuse_softc->sc_bsh;
    123  1.4.2.2  skrll 
    124  1.4.2.2  skrll 	clk_enable(fuse_softc->sc_clk);
    125  1.4.2.2  skrll 	const uint32_t v = bus_space_read_4(bst, bsh, 0x100 + offset);
    126  1.4.2.2  skrll 	clk_disable(fuse_softc->sc_clk);
    127  1.4.2.2  skrll 
    128  1.4.2.2  skrll 	return v;
    129  1.4.2.2  skrll }
    130