xref: /freebsd/sys/arm/ti/ti_prm.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 /*-
2  * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 /*
28  * Power management - simple driver to handle reset and give access to
29  * memory space region for other drivers through prcm driver.
30  * Documentation/devicetree/binding/arm/omap/prm-inst.txt
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 
41 #include <dev/fdt/simplebus.h>
42 
43 #include <dev/ofw/ofw_bus_subr.h>
44 
45 #include <arm/ti/ti_prcm.h>
46 #include <arm/ti/ti_prm.h>
47 
48 #if 0
49 #define DPRINTF(dev, msg...) device_printf(dev, msg)
50 #else
51 #define DPRINTF(dev, msg...)
52 #endif
53 
54 /* relative to prcm address range */
55 #define TI_PRM_PER_RSTCTRL	0xC00
56 
57 struct ti_prm_softc {
58 	device_t		dev;
59 	uint8_t			type;
60 	bool			has_reset;
61 };
62 
63 /* Device */
64 #define TI_OMAP_PRM_INST	10
65 
66 #define TI_AM3_PRM_INST		5
67 #define TI_AM4_PRM_INST		4
68 #define TI_OMAP4_PRM_INST	3
69 #define TI_OMAP5_PRM_INST	2
70 #define TI_DRA7_PRM_INST	1
71 #define TI_END			0
72 
73 static struct ofw_compat_data compat_data[] = {
74 	{ "ti,am3-prm-inst",	TI_AM3_PRM_INST },
75 	{ "ti,am4-prm-inst",	TI_AM4_PRM_INST },
76 	{ "ti,omap4-prm-inst",	TI_OMAP4_PRM_INST },
77 	{ "ti,omap5-prm-inst",	TI_OMAP5_PRM_INST },
78 	{ "ti,dra7-prm-inst",	TI_DRA7_PRM_INST },
79 	{ NULL,		TI_END }
80 };
81 
82 static struct ofw_compat_data required_data[] = {
83 	{ "ti,omap-prm-inst", TI_OMAP_PRM_INST },
84 	{ NULL, TI_END }
85 };
86 
87 /* device interface */
88 static int
89 ti_prm_probe(device_t dev)
90 {
91 	if (!ofw_bus_status_okay(dev))
92 		return (ENXIO);
93 
94 	if (ofw_bus_search_compatible(dev, required_data)->ocd_data == 0)
95 		return (ENXIO);
96 
97 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
98 		return (ENXIO);
99 
100 	device_set_desc(dev, "TI OMAP Power Management");
101 	return(BUS_PROBE_DEFAULT);
102 }
103 
104 static int
105 ti_prm_attach(device_t dev)
106 {
107 	struct ti_prm_softc *sc;
108 	phandle_t node;
109 
110  	sc = device_get_softc(dev);
111 	sc->dev = dev;
112 	sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
113 
114 	node = ofw_bus_get_node(sc->dev);
115 
116 	if (OF_hasprop(node, "#reset-cells")) {
117 		sc->has_reset = true;
118 	} else
119 		sc->has_reset = false;
120 
121 	/* Make device visible for other drivers */
122 	OF_device_register_xref(OF_xref_from_node(node), sc->dev);
123 
124 	return (0);
125 }
126 
127 static int
128 ti_prm_detach(device_t dev) {
129 	return (EBUSY);
130 }
131 
132 int
133 ti_prm_reset(device_t dev)
134 {
135 	struct ti_prm_softc *sc;
136 	int err;
137 
138 	sc = device_get_softc(dev);
139 	if (sc->has_reset == false)
140 		return 1;
141 
142 	err = ti_prm_modify_4(dev, TI_PRM_PER_RSTCTRL, 0x2, 0x00);
143 	return (err);
144 }
145 
146 int
147 ti_prm_write_4(device_t dev, bus_addr_t addr, uint32_t val)
148 {
149 	struct ti_prm_softc *sc;
150 	device_t parent;
151 
152 	parent = device_get_parent(dev);
153 	sc = device_get_softc(dev);
154 	DPRINTF(sc->dev, "offset=%lx write %x\n", addr, val);
155 	ti_prcm_device_lock(parent);
156 	ti_prcm_write_4(parent, addr, val);
157 	ti_prcm_device_unlock(parent);
158 	return (0);
159 }
160 
161 int
162 ti_prm_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
163 {
164 	struct ti_prm_softc *sc;
165 	device_t parent;
166 
167 	parent = device_get_parent(dev);
168 	sc = device_get_softc(dev);
169 
170 	ti_prcm_device_lock(parent);
171 	ti_prcm_read_4(parent, addr, val);
172 	ti_prcm_device_unlock(parent);
173 	DPRINTF(sc->dev, "offset=%lx Read %x\n", addr, *val);
174 	return (0);
175 }
176 
177 int
178 ti_prm_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set)
179 {
180 	struct ti_prm_softc *sc;
181 	device_t parent;
182 
183 	parent = device_get_parent(dev);
184 	sc = device_get_softc(dev);
185 
186 	ti_prcm_device_lock(parent);
187 	ti_prcm_modify_4(parent, addr, clr, set);
188 	ti_prcm_device_unlock(parent);
189 	DPRINTF(sc->dev, "offset=%lx (clr %x set %x)\n", addr, clr, set);
190 
191 	return (0);
192 }
193 
194 static device_method_t ti_prm_methods[] = {
195 	DEVMETHOD(device_probe,		ti_prm_probe),
196 	DEVMETHOD(device_attach,	ti_prm_attach),
197 	DEVMETHOD(device_detach,	ti_prm_detach),
198 
199 	DEVMETHOD_END
200 };
201 
202 DEFINE_CLASS_1(ti_prm, ti_prm_driver, ti_prm_methods,
203     sizeof(struct ti_prm_softc), simplebus_driver);
204 
205 static devclass_t ti_prm_devclass;
206 
207 EARLY_DRIVER_MODULE(ti_prm, simplebus, ti_prm_driver,
208     ti_prm_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
209 MODULE_VERSION(ti_prm, 1);
210 MODULE_DEPEND(ti_prm, ti_sysc, 1, 1, 1);
211