xref: /freebsd/sys/arm/freescale/vybrid/vf_iomuxc.c (revision 7263c8c0998402e9860a0865013fbec4ece98cd4)
1 /*-
2  * Copyright (c) 2013-2014 Ruslan Bukin <br@bsdpad.com>
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 
27 /*
28  * Vybrid Family Input/Output Multiplexer Controller (IOMUXC)
29  * Chapter 5, Vybrid Reference Manual, Rev. 5, 07/2013
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/malloc.h>
41 #include <sys/rman.h>
42 #include <sys/timeet.h>
43 #include <sys/timetc.h>
44 #include <sys/watchdog.h>
45 
46 #include <dev/fdt/fdt_common.h>
47 #include <dev/ofw/openfirm.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 
51 #include <machine/bus.h>
52 #include <machine/cpu.h>
53 #include <machine/intr.h>
54 
55 #include <arm/freescale/vybrid/vf_iomuxc.h>
56 #include <arm/freescale/vybrid/vf_common.h>
57 
58 #define	MUX_MODE_MASK		7
59 #define	MUX_MODE_SHIFT		20
60 #define	MUX_MODE_GPIO		0
61 #define	MUX_MODE_VBUS_EN_OTG	2
62 
63 #define	IBE		(1 << 0)	/* Input Buffer Enable Field */
64 #define	OBE		(1 << 1)	/* Output Buffer Enable Field. */
65 #define	PUE		(1 << 2)	/* Pull / Keep Select Field. */
66 #define	PKE		(1 << 3)	/* Pull / Keep Enable Field. */
67 #define	HYS		(1 << 9)	/* Hysteresis Enable Field */
68 #define	ODE		(1 << 10)	/* Open Drain Enable Field. */
69 #define	SRE		(1 << 11)	/* Slew Rate Field. */
70 
71 #define	SPEED_SHIFT		12
72 #define	SPEED_MASK		0x3
73 #define	SPEED_LOW		0	/* 50 MHz */
74 #define	SPEED_MEDIUM		0x1	/* 100 MHz */
75 #define	SPEED_HIGH		0x3	/* 200 MHz */
76 
77 #define	PUS_SHIFT		4	/* Pull Up / Down Config Field Shift */
78 #define	PUS_MASK		0x3
79 #define	PUS_100_KOHM_PULL_DOWN	0
80 #define	PUS_47_KOHM_PULL_UP	0x1
81 #define	PUS_100_KOHM_PULL_UP	0x2
82 #define	PUS_22_KOHM_PULL_UP	0x3
83 
84 #define	DSE_SHIFT		6	/* Drive Strength Field Shift */
85 #define	DSE_MASK		0x7
86 #define	DSE_DISABLED		0	/* Output driver disabled */
87 #define	DSE_150_OHM		0x1
88 #define	DSE_75_OHM		0x2
89 #define	DSE_50_OHM		0x3
90 #define	DSE_37_OHM		0x4
91 #define	DSE_30_OHM		0x5
92 #define	DSE_25_OHM		0x6
93 #define	DSE_20_OHM		0x7
94 
95 #define	MAX_MUX_LEN		1024
96 
97 struct iomuxc_softc {
98 	struct resource		*tmr_res[1];
99 	bus_space_tag_t		bst;
100 	bus_space_handle_t	bsh;
101 	device_t		dev;
102 };
103 
104 static struct resource_spec iomuxc_spec[] = {
105 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
106 	{ -1, 0 }
107 };
108 
109 static int
110 iomuxc_probe(device_t dev)
111 {
112 
113 	if (!ofw_bus_status_okay(dev))
114 		return (ENXIO);
115 
116 	if (!ofw_bus_is_compatible(dev, "fsl,mvf600-iomuxc"))
117 		return (ENXIO);
118 
119 	device_set_desc(dev, "Vybrid Family IOMUXC Unit");
120 	return (BUS_PROBE_DEFAULT);
121 }
122 
123 static int
124 pinmux_set(struct iomuxc_softc *sc)
125 {
126 	phandle_t child, parent, root;
127 	pcell_t iomux_config[MAX_MUX_LEN];
128 	int len;
129 	int values;
130 	int pin;
131 	int pin_cfg;
132 	int i;
133 
134 	root = OF_finddevice("/");
135 	len = 0;
136 	parent = root;
137 
138 	/* Find 'iomux_config' prop in the nodes */
139 	for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
140 
141 		/* Find a 'leaf'. Start the search from this node. */
142 		while (OF_child(child)) {
143 			parent = child;
144 			child = OF_child(child);
145 		}
146 
147 		if (!fdt_is_enabled(child))
148 			continue;
149 
150 		if ((len = OF_getproplen(child, "iomux_config")) > 0) {
151 			OF_getprop(child, "iomux_config", &iomux_config, len);
152 
153 			values = len / (sizeof(uint32_t));
154 			for (i = 0; i < values; i += 2) {
155 				pin = fdt32_to_cpu(iomux_config[i]);
156 				pin_cfg = fdt32_to_cpu(iomux_config[i+1]);
157 #if 0
158 				device_printf(sc->dev, "Set pin %d to 0x%08x\n",
159 				    pin, pin_cfg);
160 #endif
161 				WRITE4(sc, IOMUXC(pin), pin_cfg);
162 			}
163 		}
164 
165 		if (OF_peer(child) == 0) {
166 			/* No more siblings. */
167 			child = parent;
168 			parent = OF_parent(child);
169 		}
170 	}
171 
172 	return (0);
173 }
174 
175 static int
176 iomuxc_attach(device_t dev)
177 {
178 	struct iomuxc_softc *sc;
179 
180 	sc = device_get_softc(dev);
181 	sc->dev = dev;
182 
183 	if (bus_alloc_resources(dev, iomuxc_spec, sc->tmr_res)) {
184 		device_printf(dev, "could not allocate resources\n");
185 		return (ENXIO);
186 	}
187 
188 	/* Memory interface */
189 	sc->bst = rman_get_bustag(sc->tmr_res[0]);
190 	sc->bsh = rman_get_bushandle(sc->tmr_res[0]);
191 
192 	pinmux_set(sc);
193 
194 	return (0);
195 }
196 
197 static device_method_t iomuxc_methods[] = {
198 	DEVMETHOD(device_probe,		iomuxc_probe),
199 	DEVMETHOD(device_attach,	iomuxc_attach),
200 	{ 0, 0 }
201 };
202 
203 static driver_t iomuxc_driver = {
204 	"iomuxc",
205 	iomuxc_methods,
206 	sizeof(struct iomuxc_softc),
207 };
208 
209 static devclass_t iomuxc_devclass;
210 
211 DRIVER_MODULE(iomuxc, simplebus, iomuxc_driver, iomuxc_devclass, 0, 0);
212