summaryrefslogtreecommitdiffstats
path: root/sys/arm/freescale/vybrid/vf_iomuxc.c
blob: c4e29fe89ccdf09be09e5e897a9e1a3e14b96bbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*-
 * Copyright (c) 2013-2014 Ruslan Bukin <br@bsdpad.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Vybrid Family Input/Output Multiplexer Controller (IOMUXC)
 * Chapter 5, Vybrid Reference Manual, Rev. 5, 07/2013
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/malloc.h>
#include <sys/rman.h>
#include <sys/timeet.h>
#include <sys/timetc.h>
#include <sys/watchdog.h>

#include <dev/fdt/fdt_common.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

#include <machine/bus.h>
#include <machine/cpu.h>
#include <machine/intr.h>

#include <arm/freescale/vybrid/vf_iomuxc.h>
#include <arm/freescale/vybrid/vf_common.h>

#define	MUX_MODE_MASK		7
#define	MUX_MODE_SHIFT		20
#define	MUX_MODE_GPIO		0
#define	MUX_MODE_VBUS_EN_OTG	2

#define	IBE		(1 << 0)	/* Input Buffer Enable Field */
#define	OBE		(1 << 1)	/* Output Buffer Enable Field. */
#define	PUE		(1 << 2)	/* Pull / Keep Select Field. */
#define	PKE		(1 << 3)	/* Pull / Keep Enable Field. */
#define	HYS		(1 << 9)	/* Hysteresis Enable Field */
#define	ODE		(1 << 10)	/* Open Drain Enable Field. */
#define	SRE		(1 << 11)	/* Slew Rate Field. */

#define	SPEED_SHIFT		12
#define	SPEED_MASK		0x3
#define	SPEED_LOW		0	/* 50 MHz */
#define	SPEED_MEDIUM		0x1	/* 100 MHz */
#define	SPEED_HIGH		0x3	/* 200 MHz */

#define	PUS_SHIFT		4	/* Pull Up / Down Config Field Shift */
#define	PUS_MASK		0x3
#define	PUS_100_KOHM_PULL_DOWN	0
#define	PUS_47_KOHM_PULL_UP	0x1
#define	PUS_100_KOHM_PULL_UP	0x2
#define	PUS_22_KOHM_PULL_UP	0x3

#define	DSE_SHIFT		6	/* Drive Strength Field Shift */
#define	DSE_MASK		0x7
#define	DSE_DISABLED		0	/* Output driver disabled */
#define	DSE_150_OHM		0x1
#define	DSE_75_OHM		0x2
#define	DSE_50_OHM		0x3
#define	DSE_37_OHM		0x4
#define	DSE_30_OHM		0x5
#define	DSE_25_OHM		0x6
#define	DSE_20_OHM		0x7

#define	MAX_MUX_LEN		1024

struct iomuxc_softc {
	struct resource		*tmr_res[1];
	bus_space_tag_t		bst;
	bus_space_handle_t	bsh;
	device_t		dev;
};

static struct resource_spec iomuxc_spec[] = {
	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
	{ -1, 0 }
};

static int
iomuxc_probe(device_t dev)
{

	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	if (!ofw_bus_is_compatible(dev, "fsl,mvf600-iomuxc"))
		return (ENXIO);

	device_set_desc(dev, "Vybrid Family IOMUXC Unit");
	return (BUS_PROBE_DEFAULT);
}

static int
pinmux_set(struct iomuxc_softc *sc)
{
	phandle_t child, parent, root;
	pcell_t iomux_config[MAX_MUX_LEN];
	int len;
	int values;
	int pin;
	int pin_cfg;
	int i;

	root = OF_finddevice("/");
	len = 0;
	parent = root;

	/* Find 'iomux_config' prop in the nodes */
	for (child = OF_child(parent); child != 0; child = OF_peer(child)) {

		/* Find a 'leaf'. Start the search from this node. */
		while (OF_child(child)) {
			parent = child;
			child = OF_child(child);
		}

		if (!fdt_is_enabled(child))
			continue;

		if ((len = OF_getproplen(child, "iomux_config")) > 0) {
			OF_getencprop(child, "iomux_config", iomux_config, len);

			values = len / (sizeof(uint32_t));
			for (i = 0; i < values; i += 2) {
				pin = iomux_config[i];
				pin_cfg = iomux_config[i+1];
#if 0
				device_printf(sc->dev, "Set pin %d to 0x%08x\n",
				    pin, pin_cfg);
#endif
				WRITE4(sc, IOMUXC(pin), pin_cfg);
			}
		}

		if (OF_peer(child) == 0) {
			/* No more siblings. */
			child = parent;
			parent = OF_parent(child);
		}
	}

	return (0);
}

static int
iomuxc_attach(device_t dev)
{
	struct iomuxc_softc *sc;

	sc = device_get_softc(dev);
	sc->dev = dev;

	if (bus_alloc_resources(dev, iomuxc_spec, sc->tmr_res)) {
		device_printf(dev, "could not allocate resources\n");
		return (ENXIO);
	}

	/* Memory interface */
	sc->bst = rman_get_bustag(sc->tmr_res[0]);
	sc->bsh = rman_get_bushandle(sc->tmr_res[0]);

	pinmux_set(sc);

	return (0);
}

static device_method_t iomuxc_methods[] = {
	DEVMETHOD(device_probe,		iomuxc_probe),
	DEVMETHOD(device_attach,	iomuxc_attach),
	{ 0, 0 }
};

static driver_t iomuxc_driver = {
	"iomuxc",
	iomuxc_methods,
	sizeof(struct iomuxc_softc),
};

static devclass_t iomuxc_devclass;

DRIVER_MODULE(iomuxc, simplebus, iomuxc_driver, iomuxc_devclass, 0, 0);
OpenPOWER on IntegriCloud