xref: /netbsd-src/sys/arch/arm/amlogic/meson_resets.c (revision 8ecbf5f02b752fcb7debe1a8fab1dc82602bc760)
1 /* $NetBSD: meson_resets.c,v 1.2 2019/02/25 19:30:17 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2017-2019 Jared McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: meson_resets.c,v 1.2 2019/02/25 19:30:17 jmcneill Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/cpu.h>
35 #include <sys/device.h>
36 
37 #include <dev/fdt/fdtvar.h>
38 
39 #include <dev/clk/clk_backend.h>
40 
41 #define	RESET_REG(index)	(((index) / 32) * 4)
42 #define	RESET_MASK(index)	__BIT((index) % 32)
43 
44 #define	LEVEL_OFFSET		0x7c
45 
46 static const char * compatible[] = {
47 	"amlogic,meson8b-reset",
48 	"amlogic,meson-gxbb-reset",
49 	NULL
50 };
51 
52 struct meson_resets_softc {
53 	device_t		sc_dev;
54 	bus_space_tag_t		sc_bst;
55 	bus_space_handle_t	sc_bsh;
56 };
57 
58 #define	RESET_READ(sc, reg)		\
59 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
60 #define	RESET_WRITE(sc, reg, val)	\
61 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
62 
63 static void *
64 meson_resets_acquire(device_t dev, const void *data, size_t len)
65 {
66 	if (len != 4)
67 		return NULL;
68 
69 	/* Specifier is an index. Just return it. */
70 	return (void *)(uintptr_t)be32dec(data);
71 }
72 
73 static void
74 meson_resets_release(device_t dev, void *priv)
75 {
76 }
77 
78 static int
79 meson_resets_assert(device_t dev, void *priv)
80 {
81 	struct meson_resets_softc * const sc = device_private(dev);
82 	const uintptr_t index = (uintptr_t)priv;
83 
84 	const bus_size_t reset_reg = RESET_REG(index) + LEVEL_OFFSET;
85 	const uint32_t reset_mask = RESET_MASK(index);
86 
87 	const uint32_t val = RESET_READ(sc, reset_reg);
88 	RESET_WRITE(sc, reset_reg, val & ~reset_mask);
89 
90 	return 0;
91 }
92 
93 static int
94 meson_resets_deassert(device_t dev, void *priv)
95 {
96 	struct meson_resets_softc * const sc = device_private(dev);
97 	const uintptr_t index = (uintptr_t)priv;
98 
99 	const bus_size_t reset_reg = RESET_REG(index) + LEVEL_OFFSET;
100 	const uint32_t reset_mask = RESET_MASK(index);
101 
102 	const uint32_t val = RESET_READ(sc, reset_reg);
103 	RESET_WRITE(sc, reset_reg, val | reset_mask);
104 
105 	return 0;
106 }
107 
108 static const struct fdtbus_reset_controller_func meson_fdtreset_funcs = {
109 	.acquire = meson_resets_acquire,
110 	.release = meson_resets_release,
111 	.reset_assert = meson_resets_assert,
112 	.reset_deassert = meson_resets_deassert,
113 };
114 
115 static int
116 meson_resets_match(device_t parent, cfdata_t cf, void *aux)
117 {
118 	struct fdt_attach_args * const faa = aux;
119 
120 	return of_match_compatible(faa->faa_phandle, compatible);
121 }
122 
123 static void
124 meson_resets_attach(device_t parent, device_t self, void *aux)
125 {
126 	struct meson_resets_softc * const sc = device_private(self);
127 	struct fdt_attach_args * const faa = aux;
128 	const int phandle = faa->faa_phandle;
129 	bus_addr_t addr;
130 	bus_size_t size;
131 
132 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
133 		aprint_error(": couldn't get registers\n");
134 		return;
135 	}
136 
137 	sc->sc_dev = self;
138 	sc->sc_bst = faa->faa_bst;
139 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
140 		aprint_error(": couldn't map registers\n");
141 		return;
142 	}
143 
144 	aprint_naive("\n");
145 	aprint_normal("\n");
146 
147 	fdtbus_register_reset_controller(sc->sc_dev, phandle,
148 	    &meson_fdtreset_funcs);
149 }
150 
151 CFATTACH_DECL_NEW(meson_resets, sizeof(struct meson_resets_softc),
152     meson_resets_match, meson_resets_attach, NULL, NULL);
153