cycv_rstmgr.c revision 1.3 1 1.3 skrll /* $NetBSD: cycv_rstmgr.c,v 1.3 2019/10/18 06:50:08 skrll Exp $ */
2 1.1 aymeric
3 1.1 aymeric /* This file is in the public domain. */
4 1.1 aymeric
5 1.1 aymeric #include <sys/cdefs.h>
6 1.3 skrll __KERNEL_RCSID(0, "$NetBSD: cycv_rstmgr.c,v 1.3 2019/10/18 06:50:08 skrll Exp $");
7 1.1 aymeric
8 1.1 aymeric #include <sys/param.h>
9 1.1 aymeric #include <sys/bus.h>
10 1.1 aymeric #include <sys/device.h>
11 1.1 aymeric #include <sys/intr.h>
12 1.1 aymeric #include <sys/systm.h>
13 1.1 aymeric #include <sys/kernel.h>
14 1.1 aymeric #include <sys/atomic.h>
15 1.1 aymeric #include <sys/kmem.h>
16 1.1 aymeric
17 1.1 aymeric #include <arm/altera/cycv_reg.h>
18 1.1 aymeric #include <arm/altera/cycv_var.h>
19 1.1 aymeric
20 1.1 aymeric #include <dev/fdt/fdtvar.h>
21 1.1 aymeric
22 1.1 aymeric static int cycv_rstmgr_match(device_t, cfdata_t, void *);
23 1.1 aymeric static void cycv_rstmgr_attach(device_t, device_t, void *);
24 1.1 aymeric
25 1.1 aymeric static void *cycv_rst_acquire(device_t, const void *, size_t);
26 1.1 aymeric static void cycv_rst_release(device_t, void *);
27 1.1 aymeric static int cycv_rst_reset_assert(device_t, void *);
28 1.1 aymeric static int cycv_rst_reset_deassert(device_t, void *);
29 1.1 aymeric
30 1.1 aymeric static const struct fdtbus_reset_controller_func cycv_rstmgr_funcs = {
31 1.1 aymeric cycv_rst_acquire, cycv_rst_release,
32 1.1 aymeric cycv_rst_reset_assert, cycv_rst_reset_deassert
33 1.1 aymeric };
34 1.1 aymeric
35 1.1 aymeric struct cycv_rstmgr_softc {
36 1.1 aymeric device_t sc_dev;
37 1.1 aymeric
38 1.1 aymeric bus_space_tag_t sc_bst;
39 1.1 aymeric bus_space_handle_t sc_bsh;
40 1.1 aymeric };
41 1.1 aymeric
42 1.1 aymeric struct cycv_reset {
43 1.1 aymeric bus_addr_t address;
44 1.1 aymeric uint32_t mask;
45 1.1 aymeric };
46 1.1 aymeric
47 1.1 aymeric CFATTACH_DECL_NEW(cycvrstmgr, sizeof (struct cycv_rstmgr_softc),
48 1.1 aymeric cycv_rstmgr_match, cycv_rstmgr_attach, NULL, NULL);
49 1.1 aymeric
50 1.1 aymeric static struct cycv_rstmgr_softc *cycv_rstmgr_sc;
51 1.1 aymeric
52 1.1 aymeric static int
53 1.1 aymeric cycv_rstmgr_match(device_t parent, cfdata_t cf, void *aux)
54 1.1 aymeric {
55 1.1 aymeric const char *compatible[] = { "altr,rst-mgr", NULL };
56 1.1 aymeric struct fdt_attach_args *faa = aux;
57 1.1 aymeric
58 1.1 aymeric return of_match_compatible(faa->faa_phandle, compatible);
59 1.1 aymeric }
60 1.1 aymeric
61 1.1 aymeric static void
62 1.1 aymeric cycv_rstmgr_attach(device_t parent, device_t self, void *aux)
63 1.1 aymeric {
64 1.1 aymeric struct cycv_rstmgr_softc *sc = device_private(self);
65 1.1 aymeric struct fdt_attach_args *faa = aux;
66 1.1 aymeric int phandle = faa->faa_phandle;
67 1.1 aymeric bus_addr_t addr;
68 1.1 aymeric bus_size_t size;
69 1.1 aymeric int error;
70 1.1 aymeric
71 1.1 aymeric if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
72 1.1 aymeric aprint_error(": couldn't get registers\n");
73 1.1 aymeric return;
74 1.1 aymeric }
75 1.1 aymeric
76 1.1 aymeric sc->sc_dev = self;
77 1.1 aymeric sc->sc_bst = faa->faa_bst;
78 1.1 aymeric error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
79 1.1 aymeric if (error) {
80 1.3 skrll aprint_error(": couldn't map %#" PRIxBUSADDR ": %d",
81 1.3 skrll addr, error);
82 1.1 aymeric return;
83 1.1 aymeric }
84 1.1 aymeric
85 1.1 aymeric aprint_normal(": reset manager\n");
86 1.1 aymeric
87 1.1 aymeric fdtbus_register_reset_controller(self, phandle, &cycv_rstmgr_funcs);
88 1.1 aymeric cycv_rstmgr_sc = sc;
89 1.1 aymeric }
90 1.1 aymeric
91 1.1 aymeric static void *
92 1.1 aymeric cycv_rst_acquire(device_t dev, const void *data, size_t len) {
93 1.1 aymeric struct cycv_reset *reset = NULL;
94 1.1 aymeric uint32_t value;
95 1.1 aymeric
96 1.1 aymeric if (len != sizeof value)
97 1.1 aymeric goto err_decode;
98 1.1 aymeric
99 1.1 aymeric value = of_decode_int(data);
100 1.1 aymeric if (value < 0 || value >
101 1.2 aymeric (CYCV_RSTMGR_MISCMODRST - CYCV_RSTMGR_MPUMODRST + 4) / 4 * 32)
102 1.1 aymeric goto err_decode;
103 1.1 aymeric
104 1.1 aymeric reset = kmem_alloc(sizeof *reset, KM_SLEEP);
105 1.2 aymeric reset->address = CYCV_RSTMGR_MPUMODRST + value / 32 * 4;
106 1.1 aymeric reset->mask = 1 << (value % 32);
107 1.1 aymeric
108 1.1 aymeric if (0) {
109 1.1 aymeric err_decode:
110 1.1 aymeric aprint_debug_dev(dev, "couln't decode reset\n");
111 1.1 aymeric }
112 1.1 aymeric return reset;
113 1.1 aymeric }
114 1.1 aymeric
115 1.1 aymeric static void
116 1.1 aymeric cycv_rst_release(device_t dev, void *r) {
117 1.1 aymeric kmem_free(r, sizeof (struct cycv_reset));
118 1.1 aymeric }
119 1.1 aymeric
120 1.1 aymeric static void
121 1.1 aymeric cycv_rst_reset_set(device_t dev, struct cycv_reset *reset, int set) {
122 1.1 aymeric struct cycv_rstmgr_softc *sc = device_private(dev);
123 1.1 aymeric uint32_t val = bus_space_read_4(sc->sc_bst, sc->sc_bsh, reset->address);
124 1.1 aymeric
125 1.1 aymeric if (set)
126 1.1 aymeric val |= reset->mask;
127 1.1 aymeric else
128 1.1 aymeric val &= ~reset->mask;
129 1.1 aymeric
130 1.1 aymeric bus_space_write_4(sc->sc_bst, sc->sc_bsh, reset->address, val);
131 1.1 aymeric }
132 1.1 aymeric
133 1.1 aymeric static int
134 1.1 aymeric cycv_rst_reset_assert(device_t dev, void *r) {
135 1.1 aymeric
136 1.1 aymeric cycv_rst_reset_set(dev, r, 1);
137 1.1 aymeric
138 1.1 aymeric return 0;
139 1.1 aymeric }
140 1.1 aymeric
141 1.1 aymeric static int
142 1.1 aymeric cycv_rst_reset_deassert(device_t dev, void *r) {
143 1.1 aymeric
144 1.1 aymeric cycv_rst_reset_set(dev, r, 0);
145 1.1 aymeric
146 1.1 aymeric return 0;
147 1.1 aymeric }
148