xref: /freebsd/sys/arm/ti/ti_prm.c (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
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 /*
26  * Power management - simple driver to handle reset and give access to
27  * memory space region for other drivers through prcm driver.
28  * Documentation/devicetree/binding/arm/omap/prm-inst.txt
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 
39 #include <dev/fdt/simplebus.h>
40 
41 #include <dev/ofw/ofw_bus_subr.h>
42 
43 #include <arm/ti/ti_prcm.h>
44 #include <arm/ti/ti_prm.h>
45 
46 #if 0
47 #define DPRINTF(dev, msg...) device_printf(dev, msg)
48 #else
49 #define DPRINTF(dev, msg...)
50 #endif
51 
52 /* relative to prcm address range */
53 #define TI_PRM_PER_RSTCTRL	0xC00
54 
55 struct ti_prm_softc {
56 	device_t		dev;
57 	uint8_t			type;
58 	bool			has_reset;
59 };
60 
61 /* Device */
62 #define TI_OMAP_PRM_INST	10
63 
64 #define TI_AM3_PRM_INST		5
65 #define TI_AM4_PRM_INST		4
66 #define TI_OMAP4_PRM_INST	3
67 #define TI_OMAP5_PRM_INST	2
68 #define TI_DRA7_PRM_INST	1
69 #define TI_END			0
70 
71 static struct ofw_compat_data compat_data[] = {
72 	{ "ti,am3-prm-inst",	TI_AM3_PRM_INST },
73 	{ "ti,am4-prm-inst",	TI_AM4_PRM_INST },
74 	{ "ti,omap4-prm-inst",	TI_OMAP4_PRM_INST },
75 	{ "ti,omap5-prm-inst",	TI_OMAP5_PRM_INST },
76 	{ "ti,dra7-prm-inst",	TI_DRA7_PRM_INST },
77 	{ NULL,		TI_END }
78 };
79 
80 static struct ofw_compat_data required_data[] = {
81 	{ "ti,omap-prm-inst", TI_OMAP_PRM_INST },
82 	{ NULL, TI_END }
83 };
84 
85 /* device interface */
86 static int
87 ti_prm_probe(device_t dev)
88 {
89 	if (!ofw_bus_status_okay(dev))
90 		return (ENXIO);
91 
92 	if (ofw_bus_search_compatible(dev, required_data)->ocd_data == 0)
93 		return (ENXIO);
94 
95 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
96 		return (ENXIO);
97 
98 	device_set_desc(dev, "TI OMAP Power Management");
99 	return(BUS_PROBE_DEFAULT);
100 }
101 
102 static int
103 ti_prm_attach(device_t dev)
104 {
105 	struct ti_prm_softc *sc;
106 	phandle_t node;
107 
108  	sc = device_get_softc(dev);
109 	sc->dev = dev;
110 	sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
111 
112 	node = ofw_bus_get_node(sc->dev);
113 
114 	if (OF_hasprop(node, "#reset-cells")) {
115 		sc->has_reset = true;
116 	} else
117 		sc->has_reset = false;
118 
119 	/* Make device visible for other drivers */
120 	OF_device_register_xref(OF_xref_from_node(node), sc->dev);
121 
122 	return (0);
123 }
124 
125 static int
126 ti_prm_detach(device_t dev) {
127 	return (EBUSY);
128 }
129 
130 int
131 ti_prm_reset(device_t dev)
132 {
133 	struct ti_prm_softc *sc;
134 	int err;
135 
136 	sc = device_get_softc(dev);
137 	if (sc->has_reset == false)
138 		return 1;
139 
140 	err = ti_prm_modify_4(dev, TI_PRM_PER_RSTCTRL, 0x2, 0x00);
141 	return (err);
142 }
143 
144 int
145 ti_prm_write_4(device_t dev, bus_addr_t addr, uint32_t val)
146 {
147 	device_t parent;
148 
149 	parent = device_get_parent(dev);
150 	DPRINTF(dev, "offset=%lx write %x\n", addr, val);
151 	ti_prcm_device_lock(parent);
152 	ti_prcm_write_4(parent, addr, val);
153 	ti_prcm_device_unlock(parent);
154 	return (0);
155 }
156 
157 int
158 ti_prm_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
159 {
160 	device_t parent;
161 
162 	parent = device_get_parent(dev);
163 
164 	ti_prcm_device_lock(parent);
165 	ti_prcm_read_4(parent, addr, val);
166 	ti_prcm_device_unlock(parent);
167 	DPRINTF(dev, "offset=%lx Read %x\n", addr, *val);
168 	return (0);
169 }
170 
171 int
172 ti_prm_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set)
173 {
174 	device_t parent;
175 
176 	parent = device_get_parent(dev);
177 
178 	ti_prcm_device_lock(parent);
179 	ti_prcm_modify_4(parent, addr, clr, set);
180 	ti_prcm_device_unlock(parent);
181 	DPRINTF(dev, "offset=%lx (clr %x set %x)\n", addr, clr, set);
182 
183 	return (0);
184 }
185 
186 static device_method_t ti_prm_methods[] = {
187 	DEVMETHOD(device_probe,		ti_prm_probe),
188 	DEVMETHOD(device_attach,	ti_prm_attach),
189 	DEVMETHOD(device_detach,	ti_prm_detach),
190 
191 	DEVMETHOD_END
192 };
193 
194 DEFINE_CLASS_1(ti_prm, ti_prm_driver, ti_prm_methods,
195     sizeof(struct ti_prm_softc), simplebus_driver);
196 
197 EARLY_DRIVER_MODULE(ti_prm, simplebus, ti_prm_driver, 0, 0,
198     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
199 MODULE_VERSION(ti_prm, 1);
200 MODULE_DEPEND(ti_prm, ti_sysc, 1, 1, 1);
201