1*56816e68SRuslan Bukin /*-
2*56816e68SRuslan Bukin * SPDX-License-Identifier: BSD-2-Clause
3*56816e68SRuslan Bukin *
4*56816e68SRuslan Bukin * Copyright (c) 2024 Ruslan Bukin <br@bsdpad.com>
5*56816e68SRuslan Bukin *
6*56816e68SRuslan Bukin * Redistribution and use in source and binary forms, with or without
7*56816e68SRuslan Bukin * modification, are permitted provided that the following conditions
8*56816e68SRuslan Bukin * are met:
9*56816e68SRuslan Bukin * 1. Redistributions of source code must retain the above copyright
10*56816e68SRuslan Bukin * notice, this list of conditions and the following disclaimer.
11*56816e68SRuslan Bukin * 2. Redistributions in binary form must reproduce the above copyright
12*56816e68SRuslan Bukin * notice, this list of conditions and the following disclaimer in the
13*56816e68SRuslan Bukin * documentation and/or other materials provided with the distribution.
14*56816e68SRuslan Bukin *
15*56816e68SRuslan Bukin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*56816e68SRuslan Bukin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*56816e68SRuslan Bukin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*56816e68SRuslan Bukin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*56816e68SRuslan Bukin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*56816e68SRuslan Bukin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*56816e68SRuslan Bukin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*56816e68SRuslan Bukin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*56816e68SRuslan Bukin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*56816e68SRuslan Bukin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*56816e68SRuslan Bukin * SUCH DAMAGE.
26*56816e68SRuslan Bukin */
27*56816e68SRuslan Bukin
28*56816e68SRuslan Bukin #include <sys/param.h>
29*56816e68SRuslan Bukin #include <sys/systm.h>
30*56816e68SRuslan Bukin #include <sys/bus.h>
31*56816e68SRuslan Bukin #include <sys/kernel.h>
32*56816e68SRuslan Bukin #include <sys/lock.h>
33*56816e68SRuslan Bukin #include <sys/module.h>
34*56816e68SRuslan Bukin #include <sys/mutex.h>
35*56816e68SRuslan Bukin
36*56816e68SRuslan Bukin #include <machine/bus.h>
37*56816e68SRuslan Bukin #include <machine/cpu.h>
38*56816e68SRuslan Bukin
39*56816e68SRuslan Bukin #include <dev/syscon/syscon.h>
40*56816e68SRuslan Bukin #include <dev/hwreset/hwreset.h>
41*56816e68SRuslan Bukin
42*56816e68SRuslan Bukin #include <dev/ofw/ofw_bus.h>
43*56816e68SRuslan Bukin #include <dev/ofw/ofw_bus_subr.h>
44*56816e68SRuslan Bukin #include <dev/ofw/openfirm.h>
45*56816e68SRuslan Bukin
46*56816e68SRuslan Bukin #include "syscon_if.h"
47*56816e68SRuslan Bukin #include "hwreset_if.h"
48*56816e68SRuslan Bukin
49*56816e68SRuslan Bukin struct eswin_rst_softc {
50*56816e68SRuslan Bukin device_t dev;
51*56816e68SRuslan Bukin struct mtx mtx;
52*56816e68SRuslan Bukin struct syscon *syscon;
53*56816e68SRuslan Bukin };
54*56816e68SRuslan Bukin
55*56816e68SRuslan Bukin #define RESET_BLOCK 0x400
56*56816e68SRuslan Bukin #define RESET_ID_TO_REG(x) (RESET_BLOCK + (x) * 4)
57*56816e68SRuslan Bukin
58*56816e68SRuslan Bukin #define ERST_LOCK(sc) mtx_lock(&(sc)->mtx)
59*56816e68SRuslan Bukin #define ERST_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
60*56816e68SRuslan Bukin #define ERST_ASSERT_LOCKED(sc) mtx_assert(&(sc)->mtx, MA_OWNED);
61*56816e68SRuslan Bukin #define ERST_ASSERT_UNLOCKED(sc) mtx_assert(&(sc)->mtx, MA_NOTOWNED);
62*56816e68SRuslan Bukin
63*56816e68SRuslan Bukin #define ERST_READ(_sc, _reg) \
64*56816e68SRuslan Bukin SYSCON_READ_4(sc->syscon, (_reg))
65*56816e68SRuslan Bukin #define ERST_WRITE(_sc, _reg, _val) \
66*56816e68SRuslan Bukin SYSCON_WRITE_4(sc->syscon, (_reg), (_val))
67*56816e68SRuslan Bukin
68*56816e68SRuslan Bukin static struct ofw_compat_data compat_data[] = {
69*56816e68SRuslan Bukin { "eswin,eic7700-reset", 1 },
70*56816e68SRuslan Bukin { NULL, 0 },
71*56816e68SRuslan Bukin };
72*56816e68SRuslan Bukin
73*56816e68SRuslan Bukin static int
eswin_rst_probe(device_t dev)74*56816e68SRuslan Bukin eswin_rst_probe(device_t dev)
75*56816e68SRuslan Bukin {
76*56816e68SRuslan Bukin
77*56816e68SRuslan Bukin if (!ofw_bus_status_okay(dev))
78*56816e68SRuslan Bukin return (ENXIO);
79*56816e68SRuslan Bukin
80*56816e68SRuslan Bukin if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
81*56816e68SRuslan Bukin return (ENXIO);
82*56816e68SRuslan Bukin
83*56816e68SRuslan Bukin device_set_desc(dev, "Eswin Reset");
84*56816e68SRuslan Bukin
85*56816e68SRuslan Bukin return (BUS_PROBE_DEFAULT);
86*56816e68SRuslan Bukin }
87*56816e68SRuslan Bukin
88*56816e68SRuslan Bukin static int
eswin_rst_attach(device_t dev)89*56816e68SRuslan Bukin eswin_rst_attach(device_t dev)
90*56816e68SRuslan Bukin {
91*56816e68SRuslan Bukin struct eswin_rst_softc *sc;
92*56816e68SRuslan Bukin int error;
93*56816e68SRuslan Bukin
94*56816e68SRuslan Bukin sc = device_get_softc(dev);
95*56816e68SRuslan Bukin sc->dev = dev;
96*56816e68SRuslan Bukin
97*56816e68SRuslan Bukin error = syscon_get_by_ofw_node(dev, OF_parent(ofw_bus_get_node(dev)),
98*56816e68SRuslan Bukin &sc->syscon);
99*56816e68SRuslan Bukin if (error != 0) {
100*56816e68SRuslan Bukin device_printf(dev, "Couldn't get syscon handle of parent\n");
101*56816e68SRuslan Bukin return (error);
102*56816e68SRuslan Bukin }
103*56816e68SRuslan Bukin
104*56816e68SRuslan Bukin mtx_init(&sc->mtx, device_get_nameunit(sc->dev), NULL, MTX_DEF);
105*56816e68SRuslan Bukin
106*56816e68SRuslan Bukin hwreset_register_ofw_provider(dev);
107*56816e68SRuslan Bukin
108*56816e68SRuslan Bukin return (0);
109*56816e68SRuslan Bukin }
110*56816e68SRuslan Bukin
111*56816e68SRuslan Bukin static int
eswin_rst_reset_assert(device_t dev,intptr_t id,bool reset)112*56816e68SRuslan Bukin eswin_rst_reset_assert(device_t dev, intptr_t id, bool reset)
113*56816e68SRuslan Bukin {
114*56816e68SRuslan Bukin struct eswin_rst_softc *sc;
115*56816e68SRuslan Bukin uint32_t reg;
116*56816e68SRuslan Bukin uint32_t base;
117*56816e68SRuslan Bukin uint32_t bit;
118*56816e68SRuslan Bukin
119*56816e68SRuslan Bukin sc = device_get_softc(dev);
120*56816e68SRuslan Bukin
121*56816e68SRuslan Bukin base = RESET_ID_TO_REG(id >> 5);
122*56816e68SRuslan Bukin bit = id & 0x1f;
123*56816e68SRuslan Bukin
124*56816e68SRuslan Bukin ERST_LOCK(sc);
125*56816e68SRuslan Bukin reg = ERST_READ(sc, base);
126*56816e68SRuslan Bukin if (reset)
127*56816e68SRuslan Bukin reg &= ~(1 << bit);
128*56816e68SRuslan Bukin else
129*56816e68SRuslan Bukin reg |= (1 << bit);
130*56816e68SRuslan Bukin ERST_WRITE(sc, base, reg);
131*56816e68SRuslan Bukin ERST_UNLOCK(sc);
132*56816e68SRuslan Bukin
133*56816e68SRuslan Bukin return (0);
134*56816e68SRuslan Bukin }
135*56816e68SRuslan Bukin
136*56816e68SRuslan Bukin static int
eswin_rst_reset_is_asserted(device_t dev,intptr_t id,bool * reset)137*56816e68SRuslan Bukin eswin_rst_reset_is_asserted(device_t dev, intptr_t id, bool *reset)
138*56816e68SRuslan Bukin {
139*56816e68SRuslan Bukin struct eswin_rst_softc *sc;
140*56816e68SRuslan Bukin uint32_t reg;
141*56816e68SRuslan Bukin uint32_t base;
142*56816e68SRuslan Bukin uint32_t bit;
143*56816e68SRuslan Bukin
144*56816e68SRuslan Bukin sc = device_get_softc(dev);
145*56816e68SRuslan Bukin
146*56816e68SRuslan Bukin base = RESET_ID_TO_REG(id >> 5);
147*56816e68SRuslan Bukin bit = id & 0x1f;
148*56816e68SRuslan Bukin
149*56816e68SRuslan Bukin ERST_LOCK(sc);
150*56816e68SRuslan Bukin reg = ERST_READ(sc, base);
151*56816e68SRuslan Bukin *reset = (reg & (1 << bit)) == 0;
152*56816e68SRuslan Bukin ERST_UNLOCK(sc);
153*56816e68SRuslan Bukin
154*56816e68SRuslan Bukin return (0);
155*56816e68SRuslan Bukin }
156*56816e68SRuslan Bukin
157*56816e68SRuslan Bukin static int
eswin_rst_map(device_t provider_dev,phandle_t xref,int ncells,pcell_t * cells,intptr_t * id)158*56816e68SRuslan Bukin eswin_rst_map(device_t provider_dev, phandle_t xref, int ncells,
159*56816e68SRuslan Bukin pcell_t *cells, intptr_t *id)
160*56816e68SRuslan Bukin {
161*56816e68SRuslan Bukin
162*56816e68SRuslan Bukin KASSERT(ncells == 2, ("wrong ncells"));
163*56816e68SRuslan Bukin
164*56816e68SRuslan Bukin *id = cells[0] << 5;
165*56816e68SRuslan Bukin *id |= ilog2(cells[1]);
166*56816e68SRuslan Bukin
167*56816e68SRuslan Bukin return (0);
168*56816e68SRuslan Bukin }
169*56816e68SRuslan Bukin
170*56816e68SRuslan Bukin static device_method_t eswin_rst_methods[] = {
171*56816e68SRuslan Bukin /* Device interface. */
172*56816e68SRuslan Bukin DEVMETHOD(device_probe, eswin_rst_probe),
173*56816e68SRuslan Bukin DEVMETHOD(device_attach, eswin_rst_attach),
174*56816e68SRuslan Bukin
175*56816e68SRuslan Bukin /* Reset interface. */
176*56816e68SRuslan Bukin DEVMETHOD(hwreset_assert, eswin_rst_reset_assert),
177*56816e68SRuslan Bukin DEVMETHOD(hwreset_is_asserted, eswin_rst_reset_is_asserted),
178*56816e68SRuslan Bukin DEVMETHOD(hwreset_map, eswin_rst_map),
179*56816e68SRuslan Bukin
180*56816e68SRuslan Bukin DEVMETHOD_END
181*56816e68SRuslan Bukin };
182*56816e68SRuslan Bukin
183*56816e68SRuslan Bukin static driver_t eswin_rst_driver = {
184*56816e68SRuslan Bukin "eswin_rst",
185*56816e68SRuslan Bukin eswin_rst_methods,
186*56816e68SRuslan Bukin sizeof(struct eswin_rst_softc)
187*56816e68SRuslan Bukin };
188*56816e68SRuslan Bukin
189*56816e68SRuslan Bukin EARLY_DRIVER_MODULE(eswin_rst, simplebus, eswin_rst_driver, 0, 0,
190*56816e68SRuslan Bukin BUS_PASS_BUS + BUS_PASS_ORDER_LATE);
191