xref: /freebsd/sys/arm/ti/ti_scm.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2010
5  *	Ben Gray <ben.r.gray@gmail.com>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Ben Gray.
19  * 4. The name of the company nor the name of the author may be used to
20  *    endorse or promote products derived from this software without specific
21  *    prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY BEN GRAY ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL BEN GRAY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /**
36  *	SCM - System Control Module
37  *
38  *	Hopefully in the end this module will contain a bunch of utility functions
39  *	for configuring and querying the general system control registers, but for
40  *	now it only does pin(pad) multiplexing.
41  *
42  *	This is different from the GPIO module in that it is used to configure the
43  *	pins between modules not just GPIO input/output.
44  *
45  *	This file contains the generic top level driver, however it relies on chip
46  *	specific settings and therefore expects an array of ti_scm_padconf structs
47  *	call ti_padconf_devmap to be located somewhere in the kernel.
48  *
49  */
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/module.h>
57 #include <sys/bus.h>
58 #include <sys/resource.h>
59 #include <sys/rman.h>
60 #include <sys/lock.h>
61 #include <sys/mutex.h>
62 
63 #include <machine/bus.h>
64 #include <machine/resource.h>
65 
66 #include <dev/ofw/openfirm.h>
67 #include <dev/ofw/ofw_bus.h>
68 #include <dev/ofw/ofw_bus_subr.h>
69 #include <dev/fdt/fdt_pinctrl.h>
70 
71 #include "ti_scm.h"
72 #include "ti_cpuid.h"
73 
74 static struct resource_spec ti_scm_res_spec[] = {
75 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },	/* Control memory window */
76 	{ -1, 0 }
77 };
78 
79 static struct ti_scm_softc *ti_scm_sc;
80 
81 #define	ti_scm_read_4(sc, reg)		\
82     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
83 #define	ti_scm_write_4(sc, reg, val)		\
84     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
85 
86 /*
87  * Device part of OMAP SCM driver
88  */
89 static int
90 ti_scm_probe(device_t dev)
91 {
92 
93 	if (!ti_soc_is_supported())
94 		return (ENXIO);
95 
96 	if (!ofw_bus_status_okay(dev))
97 		return (ENXIO);
98 
99 	if (!ofw_bus_is_compatible(dev, "syscon"))
100 		return (ENXIO);
101 
102 	if (ti_scm_sc) {
103 		return (EEXIST);
104 	}
105 
106 	device_set_desc(dev, "TI Control Module");
107 	return (BUS_PROBE_DEFAULT);
108 }
109 
110 /**
111  *	ti_scm_attach - attaches the timer to the simplebus
112  *	@dev: new device
113  *
114  *	Reserves memory and interrupt resources, stores the softc structure
115  *	globally and registers both the timecount and eventtimer objects.
116  *
117  *	RETURNS
118  *	Zero on success or ENXIO if an error occuried.
119  */
120 static int
121 ti_scm_attach(device_t dev)
122 {
123 	struct ti_scm_softc *sc = device_get_softc(dev);
124 
125 	sc->sc_dev = dev;
126 
127 	if (bus_alloc_resources(dev, ti_scm_res_spec, sc->sc_res)) {
128 		device_printf(dev, "could not allocate resources\n");
129 		return (ENXIO);
130 	}
131 
132 	/* Global timer interface */
133 	sc->sc_bst = rman_get_bustag(sc->sc_res[0]);
134 	sc->sc_bsh = rman_get_bushandle(sc->sc_res[0]);
135 
136 	ti_scm_sc = sc;
137 
138 	/* Attach platform extensions, if any. */
139 	bus_generic_probe(dev);
140 
141 	return (bus_generic_attach(dev));
142 }
143 
144 int
145 ti_scm_reg_read_4(uint32_t reg, uint32_t *val)
146 {
147 	if (!ti_scm_sc)
148 		return (ENXIO);
149 
150 	*val = ti_scm_read_4(ti_scm_sc, reg);
151 	return (0);
152 }
153 
154 int
155 ti_scm_reg_write_4(uint32_t reg, uint32_t val)
156 {
157 	if (!ti_scm_sc)
158 		return (ENXIO);
159 
160 	ti_scm_write_4(ti_scm_sc, reg, val);
161 	return (0);
162 }
163 
164 
165 static device_method_t ti_scm_methods[] = {
166 	DEVMETHOD(device_probe,		ti_scm_probe),
167 	DEVMETHOD(device_attach,	ti_scm_attach),
168 
169 	{ 0, 0 }
170 };
171 
172 static driver_t ti_scm_driver = {
173 	"ti_scm",
174 	ti_scm_methods,
175 	sizeof(struct ti_scm_softc),
176 };
177 
178 static devclass_t ti_scm_devclass;
179 
180 EARLY_DRIVER_MODULE(ti_scm, simplebus, ti_scm_driver, ti_scm_devclass, 0, 0,
181     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
182