xref: /freebsd/sys/arm/freescale/vybrid/vf_iomuxc.c (revision 3494f7c0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013-2014 Ruslan Bukin <br@bsdpad.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Vybrid Family Input/Output Multiplexer Controller (IOMUXC)
31  * Chapter 5, Vybrid Reference Manual, Rev. 5, 07/2013
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/malloc.h>
40 #include <sys/rman.h>
41 #include <sys/timeet.h>
42 #include <sys/timetc.h>
43 #include <sys/watchdog.h>
44 
45 #include <dev/fdt/fdt_common.h>
46 #include <dev/ofw/openfirm.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 
50 #include <machine/bus.h>
51 #include <machine/cpu.h>
52 #include <machine/intr.h>
53 
54 #include <arm/freescale/vybrid/vf_iomuxc.h>
55 #include <arm/freescale/vybrid/vf_common.h>
56 
57 #define	MUX_MODE_MASK		7
58 #define	MUX_MODE_SHIFT		20
59 #define	MUX_MODE_GPIO		0
60 #define	MUX_MODE_VBUS_EN_OTG	2
61 
62 #define	IBE		(1 << 0)	/* Input Buffer Enable Field */
63 #define	OBE		(1 << 1)	/* Output Buffer Enable Field. */
64 #define	PUE		(1 << 2)	/* Pull / Keep Select Field. */
65 #define	PKE		(1 << 3)	/* Pull / Keep Enable Field. */
66 #define	HYS		(1 << 9)	/* Hysteresis Enable Field */
67 #define	ODE		(1 << 10)	/* Open Drain Enable Field. */
68 #define	SRE		(1 << 11)	/* Slew Rate Field. */
69 
70 #define	SPEED_SHIFT		12
71 #define	SPEED_MASK		0x3
72 #define	SPEED_LOW		0	/* 50 MHz */
73 #define	SPEED_MEDIUM		0x1	/* 100 MHz */
74 #define	SPEED_HIGH		0x3	/* 200 MHz */
75 
76 #define	PUS_SHIFT		4	/* Pull Up / Down Config Field Shift */
77 #define	PUS_MASK		0x3
78 #define	PUS_100_KOHM_PULL_DOWN	0
79 #define	PUS_47_KOHM_PULL_UP	0x1
80 #define	PUS_100_KOHM_PULL_UP	0x2
81 #define	PUS_22_KOHM_PULL_UP	0x3
82 
83 #define	DSE_SHIFT		6	/* Drive Strength Field Shift */
84 #define	DSE_MASK		0x7
85 #define	DSE_DISABLED		0	/* Output driver disabled */
86 #define	DSE_150_OHM		0x1
87 #define	DSE_75_OHM		0x2
88 #define	DSE_50_OHM		0x3
89 #define	DSE_37_OHM		0x4
90 #define	DSE_30_OHM		0x5
91 #define	DSE_25_OHM		0x6
92 #define	DSE_20_OHM		0x7
93 
94 #define	MAX_MUX_LEN		1024
95 
96 struct iomuxc_softc {
97 	struct resource		*tmr_res[1];
98 	bus_space_tag_t		bst;
99 	bus_space_handle_t	bsh;
100 	device_t		dev;
101 };
102 
103 static struct resource_spec iomuxc_spec[] = {
104 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
105 	{ -1, 0 }
106 };
107 
108 static int
109 iomuxc_probe(device_t dev)
110 {
111 
112 	if (!ofw_bus_status_okay(dev))
113 		return (ENXIO);
114 
115 	if (!ofw_bus_is_compatible(dev, "fsl,mvf600-iomuxc"))
116 		return (ENXIO);
117 
118 	device_set_desc(dev, "Vybrid Family IOMUXC Unit");
119 	return (BUS_PROBE_DEFAULT);
120 }
121 
122 static int
123 pinmux_set(struct iomuxc_softc *sc)
124 {
125 	phandle_t child, parent, root;
126 	pcell_t iomux_config[MAX_MUX_LEN];
127 	int len;
128 	int values;
129 	int pin;
130 	int pin_cfg;
131 	int i;
132 
133 	root = OF_finddevice("/");
134 	len = 0;
135 	parent = root;
136 
137 	/* Find 'iomux_config' prop in the nodes */
138 	for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
139 		/* Find a 'leaf'. Start the search from this node. */
140 		while (OF_child(child)) {
141 			parent = child;
142 			child = OF_child(child);
143 		}
144 
145 		if (!ofw_bus_node_status_okay(child))
146 			continue;
147 
148 		if ((len = OF_getproplen(child, "iomux_config")) > 0) {
149 			OF_getencprop(child, "iomux_config", iomux_config, len);
150 
151 			values = len / (sizeof(uint32_t));
152 			for (i = 0; i < values; i += 2) {
153 				pin = iomux_config[i];
154 				pin_cfg = iomux_config[i+1];
155 #if 0
156 				device_printf(sc->dev, "Set pin %d to 0x%08x\n",
157 				    pin, pin_cfg);
158 #endif
159 				WRITE4(sc, IOMUXC(pin), pin_cfg);
160 			}
161 		}
162 
163 		if (OF_peer(child) == 0) {
164 			/* No more siblings. */
165 			child = parent;
166 			parent = OF_parent(child);
167 		}
168 	}
169 
170 	return (0);
171 }
172 
173 static int
174 iomuxc_attach(device_t dev)
175 {
176 	struct iomuxc_softc *sc;
177 
178 	sc = device_get_softc(dev);
179 	sc->dev = dev;
180 
181 	if (bus_alloc_resources(dev, iomuxc_spec, sc->tmr_res)) {
182 		device_printf(dev, "could not allocate resources\n");
183 		return (ENXIO);
184 	}
185 
186 	/* Memory interface */
187 	sc->bst = rman_get_bustag(sc->tmr_res[0]);
188 	sc->bsh = rman_get_bushandle(sc->tmr_res[0]);
189 
190 	pinmux_set(sc);
191 
192 	return (0);
193 }
194 
195 static device_method_t iomuxc_methods[] = {
196 	DEVMETHOD(device_probe,		iomuxc_probe),
197 	DEVMETHOD(device_attach,	iomuxc_attach),
198 	{ 0, 0 }
199 };
200 
201 static driver_t iomuxc_driver = {
202 	"iomuxc",
203 	iomuxc_methods,
204 	sizeof(struct iomuxc_softc),
205 };
206 
207 DRIVER_MODULE(iomuxc, simplebus, iomuxc_driver, 0, 0);
208