vf_iomuxc.c revision 266146
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: stable/10/sys/arm/freescale/vybrid/vf_iomuxc.c 266146 2014-05-15 15:43:33Z ian $");
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/fdt.h>
53#include <machine/cpu.h>
54#include <machine/intr.h>
55
56#include <arm/freescale/vybrid/vf_iomuxc.h>
57#include <arm/freescale/vybrid/vf_common.h>
58
59#define	IBE		(1 << 0) /* Input Buffer Enable Field */
60#define	OBE		(1 << 1) /* Output Buffer Enable Field. */
61#define	PUE		(1 << 2) /* Pull / Keep Select Field. */
62#define	PKE		(1 << 3) /* Pull / Keep Enable Field. */
63#define	PUS_MASK	(3 << 4) /* Pull Up / Down Config Field. */
64#define	DSE_MASK	(7 << 6) /* Drive Strength Field. */
65#define	HYS		(1 << 9) /* Hysteresis Enable Field */
66
67#define	MUX_MODE_MASK		7
68#define	MUX_MODE_SHIFT		20
69#define	MUX_MODE_GPIO		0
70#define	MUX_MODE_VBUS_EN_OTG	2
71
72#define	PUS_22_KOHM_PULL_UP	(3 << 4)
73#define	DSE_25_OHM		(6 << 6)
74
75#define	MAX_MUX_LEN		1024
76
77struct iomuxc_softc {
78	struct resource		*tmr_res[1];
79	bus_space_tag_t		bst;
80	bus_space_handle_t	bsh;
81	device_t		dev;
82};
83
84static struct resource_spec iomuxc_spec[] = {
85	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
86	{ -1, 0 }
87};
88
89static int
90iomuxc_probe(device_t dev)
91{
92
93	if (!ofw_bus_is_compatible(dev, "fsl,mvf600-iomuxc"))
94		return (ENXIO);
95
96	device_set_desc(dev, "Vybrid Family IOMUXC Unit");
97	return (BUS_PROBE_DEFAULT);
98}
99
100static int
101configure_pad(struct iomuxc_softc *sc, int pad, int mux_mode)
102{
103	int reg;
104
105	reg = READ4(sc, pad);
106	reg &= ~(MUX_MODE_MASK << MUX_MODE_SHIFT);
107	reg |= (mux_mode << MUX_MODE_SHIFT);
108	WRITE4(sc, pad, reg);
109
110	return (0);
111}
112
113static int
114pinmux_set(struct iomuxc_softc *sc)
115{
116	phandle_t child, parent, root;
117	pcell_t iomux_config[MAX_MUX_LEN];
118	int len;
119	int values;
120	int pin;
121	int mux_mode;
122	int i;
123
124	root = OF_finddevice("/");
125	len = 0;
126	parent = root;
127
128	/* Find 'iomux_config' prop in the nodes */
129	for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
130
131		/* Find a 'leaf'. Start the search from this node. */
132		while (OF_child(child)) {
133			parent = child;
134			child = OF_child(child);
135		}
136
137		if (!fdt_is_enabled(child))
138			continue;
139
140		if ((len = OF_getproplen(child, "iomux_config")) > 0) {
141			OF_getprop(child, "iomux_config", &iomux_config, len);
142
143			values = len / (sizeof(uint32_t));
144			for (i = 0; i < values; i += 2) {
145				pin = fdt32_to_cpu(iomux_config[i]);
146				mux_mode = fdt32_to_cpu(iomux_config[i+1]);
147#if 0
148				device_printf(sc->dev, "Set pin %d to ALT%d\n",
149				    pin, mux_mode);
150#endif
151				configure_pad(sc, IOMUXC(pin), mux_mode);
152			}
153		}
154
155		if (OF_peer(child) == 0) {
156			/* No more siblings. */
157			child = parent;
158			parent = OF_parent(child);
159		}
160	}
161
162	return (0);
163}
164
165static int
166iomuxc_attach(device_t dev)
167{
168	struct iomuxc_softc *sc;
169	int reg;
170
171	sc = device_get_softc(dev);
172	sc->dev = dev;
173
174	if (bus_alloc_resources(dev, iomuxc_spec, sc->tmr_res)) {
175		device_printf(dev, "could not allocate resources\n");
176		return (ENXIO);
177	}
178
179	/* Memory interface */
180	sc->bst = rman_get_bustag(sc->tmr_res[0]);
181	sc->bsh = rman_get_bushandle(sc->tmr_res[0]);
182
183	/* USB */
184	configure_pad(sc, IOMUXC_PTA17, MUX_MODE_VBUS_EN_OTG);
185	reg = (PKE | PUE | PUS_22_KOHM_PULL_UP | DSE_25_OHM | OBE);
186	WRITE4(sc, IOMUXC_PTA7, reg);
187
188	pinmux_set(sc);
189
190	return (0);
191}
192
193static device_method_t iomuxc_methods[] = {
194	DEVMETHOD(device_probe,		iomuxc_probe),
195	DEVMETHOD(device_attach,	iomuxc_attach),
196	{ 0, 0 }
197};
198
199static driver_t iomuxc_driver = {
200	"iomuxc",
201	iomuxc_methods,
202	sizeof(struct iomuxc_softc),
203};
204
205static devclass_t iomuxc_devclass;
206
207DRIVER_MODULE(iomuxc, simplebus, iomuxc_driver, iomuxc_devclass, 0, 0);
208