1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2025 Bojan Novković <bnovkov@FreeBSD.org
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/lock.h>
33 #include <sys/module.h>
34 #include <sys/mutex.h>
35 #include <sys/rman.h>
36
37 #include <machine/bus.h>
38 #include <machine/cpu.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41
42 #include <dev/hwreset/hwreset.h>
43 #include <dev/syscon/syscon.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/ofw/openfirm.h>
47
48 #include "syscon_if.h"
49 #include "hwreset_if.h"
50
51 struct cvitek_reset_softc {
52 device_t dev;
53 struct mtx mtx;
54 struct syscon *syscon;
55 };
56
57 static int
cvitek_reset_probe(device_t dev)58 cvitek_reset_probe(device_t dev)
59 {
60
61 if (!ofw_bus_status_okay(dev))
62 return (ENXIO);
63
64 if (ofw_bus_is_compatible(dev, "cvitek,reset")) {
65 device_set_desc(dev, "CVITEK reset controller");
66 return (BUS_PROBE_DEFAULT);
67 }
68
69 return (ENXIO);
70 }
71
72 static int
cvitek_reset_attach(device_t dev)73 cvitek_reset_attach(device_t dev)
74 {
75 struct cvitek_reset_softc *sc;
76 int error;
77
78 sc = device_get_softc(dev);
79 sc->dev = dev;
80
81 error = syscon_get_by_ofw_property(dev, ofw_bus_get_node(dev),
82 "syscon", &sc->syscon);
83 if (error != 0) {
84 device_printf(dev, "Couldn't get syscon handle\n");
85 return (error);
86 }
87 mtx_init(&sc->mtx, device_get_nameunit(sc->dev), NULL, MTX_DEF);
88
89 hwreset_register_ofw_provider(dev);
90
91 return (0);
92 }
93
94 static int
cvitek_reset_assert(device_t dev,intptr_t id,bool reset)95 cvitek_reset_assert(device_t dev, intptr_t id, bool reset)
96 {
97 struct cvitek_reset_softc *sc;
98 uint32_t offset, val;
99 uint32_t bit;
100
101 sc = device_get_softc(dev);
102 bit = id % 32;
103 offset = id / 32;
104
105 mtx_lock(&sc->mtx);
106 val = SYSCON_READ_4(sc->syscon, offset);
107 if (reset)
108 val &= ~(1 << bit);
109 else
110 val |= (1 << bit);
111 SYSCON_WRITE_4(sc->syscon, offset, val);
112 mtx_unlock(&sc->mtx);
113
114 return (0);
115 }
116
117 static device_method_t cvitek_reset_methods[] = {
118 DEVMETHOD(device_probe, cvitek_reset_probe),
119 DEVMETHOD(device_attach, cvitek_reset_attach),
120
121 DEVMETHOD(hwreset_assert, cvitek_reset_assert),
122
123 DEVMETHOD_END
124 };
125
126 static driver_t cvitek_reset_driver = {
127 "cvitek_reset",
128 cvitek_reset_methods,
129 sizeof(struct cvitek_reset_softc)
130 };
131
132 DRIVER_MODULE(cvitek_reset, simplebus, cvitek_reset_driver, 0, 0);
133