xref: /netbsd-src/sys/arch/arm/amlogic/meson_resets.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: meson_resets.c,v 1.4 2021/01/27 03:10:18 thorpej Exp $ */
2912cfa14Sjmcneill 
3912cfa14Sjmcneill /*-
4912cfa14Sjmcneill  * Copyright (c) 2017-2019 Jared McNeill <jmcneill@invisible.ca>
5912cfa14Sjmcneill  * All rights reserved.
6912cfa14Sjmcneill  *
7912cfa14Sjmcneill  * Redistribution and use in source and binary forms, with or without
8912cfa14Sjmcneill  * modification, are permitted provided that the following conditions
9912cfa14Sjmcneill  * are met:
10912cfa14Sjmcneill  * 1. Redistributions of source code must retain the above copyright
11912cfa14Sjmcneill  *    notice, this list of conditions and the following disclaimer.
12912cfa14Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13912cfa14Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
14912cfa14Sjmcneill  *    documentation and/or other materials provided with the distribution.
15912cfa14Sjmcneill  *
16912cfa14Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17912cfa14Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18912cfa14Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19912cfa14Sjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20912cfa14Sjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21912cfa14Sjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22912cfa14Sjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23912cfa14Sjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24912cfa14Sjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25912cfa14Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26912cfa14Sjmcneill  * SUCH DAMAGE.
27912cfa14Sjmcneill  */
28912cfa14Sjmcneill 
29912cfa14Sjmcneill #include <sys/cdefs.h>
30*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: meson_resets.c,v 1.4 2021/01/27 03:10:18 thorpej Exp $");
31912cfa14Sjmcneill 
32912cfa14Sjmcneill #include <sys/param.h>
33912cfa14Sjmcneill #include <sys/bus.h>
34912cfa14Sjmcneill #include <sys/cpu.h>
35912cfa14Sjmcneill #include <sys/device.h>
36912cfa14Sjmcneill 
37912cfa14Sjmcneill #include <dev/fdt/fdtvar.h>
38912cfa14Sjmcneill 
39912cfa14Sjmcneill #include <dev/clk/clk_backend.h>
40912cfa14Sjmcneill 
41912cfa14Sjmcneill #define	RESET_REG(index)	(((index) / 32) * 4)
42912cfa14Sjmcneill #define	RESET_MASK(index)	__BIT((index) % 32)
43912cfa14Sjmcneill 
44912cfa14Sjmcneill #define	LEVEL_OFFSET		0x7c
45912cfa14Sjmcneill 
46*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
47*6e54367aSthorpej 	{ .compat = "amlogic,meson8b-reset" },
48*6e54367aSthorpej 	{ .compat = "amlogic,meson-axg-reset" },
49*6e54367aSthorpej 	{ .compat = "amlogic,meson-gxbb-reset" },
50*6e54367aSthorpej 	DEVICE_COMPAT_EOL
51912cfa14Sjmcneill };
52912cfa14Sjmcneill 
53912cfa14Sjmcneill struct meson_resets_softc {
54912cfa14Sjmcneill 	device_t		sc_dev;
55912cfa14Sjmcneill 	bus_space_tag_t		sc_bst;
56912cfa14Sjmcneill 	bus_space_handle_t	sc_bsh;
57912cfa14Sjmcneill };
58912cfa14Sjmcneill 
59912cfa14Sjmcneill #define	RESET_READ(sc, reg)		\
60912cfa14Sjmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
61912cfa14Sjmcneill #define	RESET_WRITE(sc, reg, val)	\
62912cfa14Sjmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
63912cfa14Sjmcneill 
64912cfa14Sjmcneill static void *
meson_resets_acquire(device_t dev,const void * data,size_t len)65912cfa14Sjmcneill meson_resets_acquire(device_t dev, const void *data, size_t len)
66912cfa14Sjmcneill {
67912cfa14Sjmcneill 	if (len != 4)
68912cfa14Sjmcneill 		return NULL;
69912cfa14Sjmcneill 
70912cfa14Sjmcneill 	/* Specifier is an index. Just return it. */
71912cfa14Sjmcneill 	return (void *)(uintptr_t)be32dec(data);
72912cfa14Sjmcneill }
73912cfa14Sjmcneill 
74912cfa14Sjmcneill static void
meson_resets_release(device_t dev,void * priv)75912cfa14Sjmcneill meson_resets_release(device_t dev, void *priv)
76912cfa14Sjmcneill {
77912cfa14Sjmcneill }
78912cfa14Sjmcneill 
79912cfa14Sjmcneill static int
meson_resets_assert(device_t dev,void * priv)80912cfa14Sjmcneill meson_resets_assert(device_t dev, void *priv)
81912cfa14Sjmcneill {
82912cfa14Sjmcneill 	struct meson_resets_softc * const sc = device_private(dev);
83912cfa14Sjmcneill 	const uintptr_t index = (uintptr_t)priv;
84912cfa14Sjmcneill 
85912cfa14Sjmcneill 	const bus_size_t reset_reg = RESET_REG(index) + LEVEL_OFFSET;
86912cfa14Sjmcneill 	const uint32_t reset_mask = RESET_MASK(index);
87912cfa14Sjmcneill 
88912cfa14Sjmcneill 	const uint32_t val = RESET_READ(sc, reset_reg);
89912cfa14Sjmcneill 	RESET_WRITE(sc, reset_reg, val & ~reset_mask);
90912cfa14Sjmcneill 
91912cfa14Sjmcneill 	return 0;
92912cfa14Sjmcneill }
93912cfa14Sjmcneill 
94912cfa14Sjmcneill static int
meson_resets_deassert(device_t dev,void * priv)95912cfa14Sjmcneill meson_resets_deassert(device_t dev, void *priv)
96912cfa14Sjmcneill {
97912cfa14Sjmcneill 	struct meson_resets_softc * const sc = device_private(dev);
98912cfa14Sjmcneill 	const uintptr_t index = (uintptr_t)priv;
99912cfa14Sjmcneill 
100912cfa14Sjmcneill 	const bus_size_t reset_reg = RESET_REG(index) + LEVEL_OFFSET;
101912cfa14Sjmcneill 	const uint32_t reset_mask = RESET_MASK(index);
102912cfa14Sjmcneill 
103912cfa14Sjmcneill 	const uint32_t val = RESET_READ(sc, reset_reg);
104912cfa14Sjmcneill 	RESET_WRITE(sc, reset_reg, val | reset_mask);
105912cfa14Sjmcneill 
106912cfa14Sjmcneill 	return 0;
107912cfa14Sjmcneill }
108912cfa14Sjmcneill 
109912cfa14Sjmcneill static const struct fdtbus_reset_controller_func meson_fdtreset_funcs = {
110912cfa14Sjmcneill 	.acquire = meson_resets_acquire,
111912cfa14Sjmcneill 	.release = meson_resets_release,
112912cfa14Sjmcneill 	.reset_assert = meson_resets_assert,
113912cfa14Sjmcneill 	.reset_deassert = meson_resets_deassert,
114912cfa14Sjmcneill };
115912cfa14Sjmcneill 
116912cfa14Sjmcneill static int
meson_resets_match(device_t parent,cfdata_t cf,void * aux)117912cfa14Sjmcneill meson_resets_match(device_t parent, cfdata_t cf, void *aux)
118912cfa14Sjmcneill {
119912cfa14Sjmcneill 	struct fdt_attach_args * const faa = aux;
120912cfa14Sjmcneill 
121*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
122912cfa14Sjmcneill }
123912cfa14Sjmcneill 
124912cfa14Sjmcneill static void
meson_resets_attach(device_t parent,device_t self,void * aux)125912cfa14Sjmcneill meson_resets_attach(device_t parent, device_t self, void *aux)
126912cfa14Sjmcneill {
127912cfa14Sjmcneill 	struct meson_resets_softc * const sc = device_private(self);
128912cfa14Sjmcneill 	struct fdt_attach_args * const faa = aux;
129912cfa14Sjmcneill 	const int phandle = faa->faa_phandle;
130912cfa14Sjmcneill 	bus_addr_t addr;
131912cfa14Sjmcneill 	bus_size_t size;
132912cfa14Sjmcneill 
133912cfa14Sjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
134912cfa14Sjmcneill 		aprint_error(": couldn't get registers\n");
135912cfa14Sjmcneill 		return;
136912cfa14Sjmcneill 	}
137912cfa14Sjmcneill 
138912cfa14Sjmcneill 	sc->sc_dev = self;
139912cfa14Sjmcneill 	sc->sc_bst = faa->faa_bst;
140912cfa14Sjmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
141912cfa14Sjmcneill 		aprint_error(": couldn't map registers\n");
142912cfa14Sjmcneill 		return;
143912cfa14Sjmcneill 	}
144912cfa14Sjmcneill 
145912cfa14Sjmcneill 	aprint_naive("\n");
146912cfa14Sjmcneill 	aprint_normal("\n");
147912cfa14Sjmcneill 
148912cfa14Sjmcneill 	fdtbus_register_reset_controller(sc->sc_dev, phandle,
149912cfa14Sjmcneill 	    &meson_fdtreset_funcs);
150912cfa14Sjmcneill }
151912cfa14Sjmcneill 
152912cfa14Sjmcneill CFATTACH_DECL_NEW(meson_resets, sizeof(struct meson_resets_softc),
153912cfa14Sjmcneill     meson_resets_match, meson_resets_attach, NULL, NULL);
154