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