xref: /freebsd/sys/arm/freescale/imx/imx_iomux.c (revision 7ddf24b3)
1 /*-
2  * Copyright (c) 2014 Ian Lepore
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/malloc.h>
35 #include <sys/rman.h>
36 
37 #include <machine/bus.h>
38 #include <machine/fdt.h>
39 
40 #include <dev/fdt/fdt_common.h>
41 #include <dev/fdt/fdt_pinctrl.h>
42 #include <dev/ofw/openfirm.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 
46 #include <arm/freescale/imx/imx_iomuxvar.h>
47 #include <arm/freescale/imx/imx_machdep.h>
48 
49 struct iomux_softc {
50 	device_t	dev;
51 	struct resource	*mem_res;
52 	u_int		last_gpreg;
53 };
54 
55 static struct iomux_softc *iomux_sc;
56 
57 static struct ofw_compat_data compat_data[] = {
58 	{"fsl,imx6dl-iomuxc",	true},
59 	{"fsl,imx6q-iomuxc",	true},
60 	{"fsl,imx6sl-iomuxc",	true},
61 	{"fsl,imx6sx-iomuxc",	true},
62 	{"fsl,imx53-iomuxc",	true},
63 	{"fsl,imx51-iomuxc",	true},
64 	{NULL,			false},
65 };
66 
67 /*
68  * Each tuple in an fsl,pins property contains these fields.
69  */
70 struct pincfg {
71 	uint32_t mux_reg;
72 	uint32_t padconf_reg;
73 	uint32_t input_reg;
74 	uint32_t mux_val;
75 	uint32_t input_val;
76 	uint32_t padconf_val;
77 };
78 
79 static inline uint32_t
80 RD4(struct iomux_softc *sc, bus_size_t off)
81 {
82 
83 	return (bus_read_4(sc->mem_res, off));
84 }
85 
86 static inline void
87 WR4(struct iomux_softc *sc, bus_size_t off, uint32_t val)
88 {
89 
90 	bus_write_4(sc->mem_res, off, val);
91 }
92 
93 static int
94 iomux_configure_pins(device_t dev, phandle_t cfgxref)
95 {
96 	struct iomux_softc * sc;
97 	struct pincfg *cfgtuples, *cfg;
98 	phandle_t cfgnode;
99 	int i, ntuples;
100 
101 	sc = device_get_softc(dev);
102 	cfgnode = OF_node_from_xref(cfgxref);
103 	ntuples = OF_getencprop_alloc(cfgnode, "fsl,pins", sizeof(*cfgtuples),
104 	    (void **)&cfgtuples);
105 #ifdef DEBUG
106 	{
107 		char name[32];
108 		OF_getprop(cfgnode, "name", &name, sizeof(name));
109 		printf("found %d tuples in fsl,pins for %s\n", ntuples, name);
110 	}
111 #endif
112 	if (ntuples < 0)
113 		return (ENOENT);
114 	if (ntuples == 0)
115 		return (0); /* Empty property is not an error. */
116 	for (i = 0, cfg = cfgtuples; i < ntuples; i++, cfg++) {
117 		WR4(sc, cfg->mux_reg, cfg->mux_val);
118 		WR4(sc, cfg->input_reg, cfg->input_val);
119 		WR4(sc, cfg->padconf_reg, cfg->padconf_val);
120 	}
121 	free(cfgtuples, M_OFWPROP);
122 	return (0);
123 }
124 
125 static int
126 iomux_probe(device_t dev)
127 {
128 
129 	if (!ofw_bus_status_okay(dev))
130 		return (ENXIO);
131 
132 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
133 		return (ENXIO);
134 
135 	device_set_desc(dev, "Freescale i.MX pin configuration");
136 	return (BUS_PROBE_DEFAULT);
137 }
138 
139 static int
140 iomux_detach(device_t dev)
141 {
142 
143         /* This device is always present. */
144 	return (EBUSY);
145 }
146 
147 static int
148 iomux_attach(device_t dev)
149 {
150 	struct iomux_softc * sc;
151 	int rid;
152 
153 	sc = device_get_softc(dev);
154 	sc->dev = dev;
155 
156 	switch (imx_soc_type()) {
157 	case IMXSOC_51:
158 		sc->last_gpreg = 1;
159 		break;
160 	case IMXSOC_53:
161 		sc->last_gpreg = 2;
162 		break;
163 	case IMXSOC_6DL:
164 	case IMXSOC_6S:
165 	case IMXSOC_6SL:
166 	case IMXSOC_6Q:
167 		sc->last_gpreg = 13;
168 		break;
169 	default:
170 		device_printf(dev, "Unknown SoC type\n");
171 		return (ENXIO);
172 	}
173 
174 	rid = 0;
175 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
176 	    RF_ACTIVE);
177 	if (sc->mem_res == NULL) {
178 		device_printf(dev, "Cannot allocate memory resources\n");
179 		return (ENXIO);
180 	}
181 
182 	iomux_sc = sc;
183 
184 	/*
185 	 * Register as a pinctrl device, and call the convenience function that
186 	 * walks the entire device tree invoking FDT_PINCTRL_CONFIGURE() on any
187 	 * pinctrl-0 property cells whose xref phandle refers to a configuration
188 	 * that is a child node of our node in the tree.
189 	 *
190 	 * The pinctrl bindings documentation specifically mentions that the
191 	 * pinctrl device itself may have a pinctrl-0 property which contains
192 	 * static configuration to be applied at device init time.  The tree
193 	 * walk will automatically handle this for us when it passes through our
194 	 * node in the tree.
195 	 */
196 	fdt_pinctrl_register(dev, "fsl,pins");
197 	fdt_pinctrl_configure_tree(dev);
198 
199 	return (0);
200 }
201 
202 uint32_t
203 imx_iomux_gpr_get(u_int regnum)
204 {
205 	struct iomux_softc * sc;
206 
207 	sc = iomux_sc;
208 	KASSERT(sc != NULL, ("%s called before attach", __FUNCTION__));
209 	KASSERT(regnum >= 0 && regnum <= sc->last_gpreg,
210 	    ("%s bad regnum %u, max %u", __FUNCTION__, regnum, sc->last_gpreg));
211 
212 	return (RD4(iomux_sc, regnum * 4));
213 }
214 
215 void
216 imx_iomux_gpr_set(u_int regnum, uint32_t val)
217 {
218 	struct iomux_softc * sc;
219 
220 	sc = iomux_sc;
221 	KASSERT(sc != NULL, ("%s called before attach", __FUNCTION__));
222 	KASSERT(regnum >= 0 && regnum <= sc->last_gpreg,
223 	    ("%s bad regnum %u, max %u", __FUNCTION__, regnum, sc->last_gpreg));
224 
225 	WR4(iomux_sc, regnum * 4, val);
226 }
227 
228 void
229 imx_iomux_gpr_set_masked(u_int regnum, uint32_t clrbits, uint32_t setbits)
230 {
231 	struct iomux_softc * sc;
232 	uint32_t val;
233 
234 	sc = iomux_sc;
235 	KASSERT(sc != NULL, ("%s called before attach", __FUNCTION__));
236 	KASSERT(regnum >= 0 && regnum <= sc->last_gpreg,
237 	    ("%s bad regnum %u, max %u", __FUNCTION__, regnum, sc->last_gpreg));
238 
239 	val = RD4(iomux_sc, regnum * 4);
240 	val = (val & ~clrbits) | setbits;
241 	WR4(iomux_sc, regnum * 4, val);
242 }
243 
244 static device_method_t imx_iomux_methods[] = {
245 	/* Device interface */
246 	DEVMETHOD(device_probe,         iomux_probe),
247 	DEVMETHOD(device_attach,        iomux_attach),
248 	DEVMETHOD(device_detach,        iomux_detach),
249 
250         /* fdt_pinctrl interface */
251 	DEVMETHOD(fdt_pinctrl_configure,iomux_configure_pins),
252 
253 	DEVMETHOD_END
254 };
255 
256 static driver_t imx_iomux_driver = {
257 	"imx_iomux",
258 	imx_iomux_methods,
259 	sizeof(struct iomux_softc),
260 };
261 
262 static devclass_t imx_iomux_devclass;
263 
264 EARLY_DRIVER_MODULE(imx_iomux, simplebus, imx_iomux_driver,
265     imx_iomux_devclass, 0, 0, BUS_PASS_CPU + BUS_PASS_ORDER_LATE);
266 
267