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