xref: /freebsd/sys/arm/mv/mvebu_pinctrl.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2018 Rubicon Communications, LLC (Netgate)
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/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/rman.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 #include <machine/intr.h>
44 
45 #include <dev/extres/syscon/syscon.h>
46 
47 #include <dev/fdt/fdt_pinctrl.h>
48 
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 
52 #include "syscon_if.h"
53 
54 #define	PINS_PER_REG	8
55 #define	BITS_PER_PIN	4
56 #define	PINS_MASK	0xf
57 #define	MAX_PIN_FUNC	5
58 
59 struct mv_pins {
60 	const char	*name;
61 	const char	*functions[MAX_PIN_FUNC];
62 };
63 
64 struct mv_padconf {
65 	const struct mv_pins	*pins;
66 	size_t		npins;
67 };
68 
69 const static struct mv_pins ap806_pins[] = {
70 	{"mpp0", {"gpio", "sdio", NULL, "spi0"}},
71 	{"mpp1", {"gpio", "sdio", NULL, "spi0"}},
72 	{"mpp2", {"gpio", "sdio", NULL, "spi0"}},
73 	{"mpp3", {"gpio", "sdio", NULL, "spi0"}},
74 	{"mpp4", {"gpio", "sdio", NULL, "i2c0"}},
75 	{"mpp5", {"gpio", "sdio", NULL, "i2c0"}},
76 	{"mpp6", {"gpio", "sdio", NULL, NULL}},
77 	{"mpp7", {"gpio", "sdio", NULL, "uart1"}},
78 	{"mpp8", {"gpio", "sdio", NULL, "uart1"}},
79 	{"mpp9", {"gpio", "sdio", NULL, "spi0"}},
80 	{"mpp10", {"gpio", "sdio", NULL, NULL}},
81 	{"mpp11", {"gpio", NULL, NULL, "uart0"}},
82 	{"mpp12", {"gpio", "sdio", "sdio", NULL}},
83 	{"mpp13", {"gpio", NULL, NULL}},
84 	{"mpp14", {"gpio", NULL, NULL}},
85 	{"mpp15", {"gpio", NULL, NULL}},
86 	{"mpp16", {"gpio", NULL, NULL}},
87 	{"mpp17", {"gpio", NULL, NULL}},
88 	{"mpp18", {"gpio", NULL, NULL}},
89 	{"mpp19", {"gpio", NULL, NULL, "uart0", "sdio"}},
90 };
91 
92 const struct mv_padconf ap806_padconf = {
93 	.npins = nitems(ap806_pins),
94 	.pins = ap806_pins,
95 };
96 
97 struct mv_pinctrl_softc {
98 	device_t		dev;
99 	struct syscon		*syscon;
100 
101 	struct mv_padconf	*padconf;
102 };
103 
104 static struct ofw_compat_data compat_data[] = {
105 	{"marvell,ap806-pinctrl", (uintptr_t)&ap806_padconf},
106 	{NULL,             0}
107 };
108 
109 #define	RD4(sc, reg)		SYSCON_READ_4((sc)->syscon, (reg))
110 #define	WR4(sc, reg, val)	SYSCON_WRITE_4((sc)->syscon, (reg), (val))
111 
112 static void
113 mv_pinctrl_configure_pin(struct mv_pinctrl_softc *sc, uint32_t pin,
114     uint32_t function)
115 {
116 	uint32_t offset, shift, reg;
117 
118 	offset = (pin / PINS_PER_REG) * BITS_PER_PIN;
119 	shift = (pin % PINS_PER_REG) * BITS_PER_PIN;
120 	reg = RD4(sc, offset);
121 	reg &= ~(PINS_MASK << shift);
122 	reg |= function << shift;
123 	WR4(sc, offset, reg);
124 }
125 
126 static int
127 mv_pinctrl_configure_pins(device_t dev, phandle_t cfgxref)
128 {
129 	struct mv_pinctrl_softc *sc;
130 	phandle_t node;
131 	char *function;
132 	const char **pins;
133 	int i, pin_num, pin_func, npins;
134 
135 	sc = device_get_softc(dev);
136 	node = OF_node_from_xref(cfgxref);
137 
138 	if (OF_getprop_alloc(node, "marvell,function",
139 	    (void **)&function) == -1)
140 		return (ENOMEM);
141 
142 	npins = ofw_bus_string_list_to_array(node, "marvell,pins", &pins);
143 	if (npins == -1)
144 		return (ENOMEM);
145 
146 	for (i = 0; i < npins; i++) {
147 		for (pin_num = 0; pin_num < sc->padconf->npins; pin_num++) {
148 			if (strcmp(pins[i], sc->padconf->pins[pin_num].name) == 0)
149 				break;
150 		}
151 		if (pin_num == sc->padconf->npins)
152 			continue;
153 
154 		for (pin_func = 0; pin_func < MAX_PIN_FUNC; pin_func++)
155 			if (sc->padconf->pins[pin_num].functions[pin_func] &&
156 			    strcmp(function, sc->padconf->pins[pin_num].functions[pin_func]) == 0)
157 				break;
158 
159 		if (pin_func == MAX_PIN_FUNC)
160 			continue;
161 
162 		mv_pinctrl_configure_pin(sc, pin_num, pin_func);
163 	}
164 
165 	OF_prop_free(pins);
166 
167 	return (0);
168 }
169 
170 static int
171 mv_pinctrl_probe(device_t dev)
172 {
173 
174 	if (!ofw_bus_status_okay(dev))
175 		return (ENXIO);
176 
177 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
178 		return (ENXIO);
179 
180 	device_set_desc(dev, "Marvell Pinctrl controller");
181 	return (BUS_PROBE_DEFAULT);
182 }
183 
184 static int
185 mv_pinctrl_attach(device_t dev)
186 {
187 	struct mv_pinctrl_softc *sc;
188 
189 	sc = device_get_softc(dev);
190 	sc->dev = dev;
191 	sc->padconf = (struct mv_padconf *)
192 	    ofw_bus_search_compatible(dev,compat_data)->ocd_data;
193 
194 	if (SYSCON_GET_HANDLE(sc->dev, &sc->syscon) != 0 ||
195 	    sc->syscon == NULL) {
196 		device_printf(dev, "cannot get syscon for device\n");
197 		return (ENXIO);
198 	}
199 
200 	fdt_pinctrl_register(dev, "marvell,pins");
201 	fdt_pinctrl_configure_tree(dev);
202 
203 	return (0);
204 }
205 
206 static int
207 mv_pinctrl_detach(device_t dev)
208 {
209 
210 	return (EBUSY);
211 }
212 
213 static device_method_t mv_pinctrl_methods[] = {
214 	/* Device interface */
215 	DEVMETHOD(device_probe,		mv_pinctrl_probe),
216 	DEVMETHOD(device_attach,	mv_pinctrl_attach),
217 	DEVMETHOD(device_detach,	mv_pinctrl_detach),
218 
219         /* fdt_pinctrl interface */
220 	DEVMETHOD(fdt_pinctrl_configure,mv_pinctrl_configure_pins),
221 
222 	DEVMETHOD_END
223 };
224 
225 static driver_t mv_pinctrl_driver = {
226 	"mv_pinctrl",
227 	mv_pinctrl_methods,
228 	sizeof(struct mv_pinctrl_softc),
229 };
230 
231 EARLY_DRIVER_MODULE(mv_pinctrl, simplebus, mv_pinctrl_driver, 0, 0,
232     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
233