xref: /freebsd/sys/arm/ti/am335x/am3359_cppi41.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.org>
3  *
4  * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 /* Based on sys/arm/ti/ti_sysc.c */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/resource.h>
36 #include <sys/rman.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42 
43 #include <dev/fdt/simplebus.h>
44 
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 
49 #include <arm/ti/ti_sysc.h>
50 
51 #if 0
52 #define DPRINTF(dev, msg...) device_printf(dev, msg)
53 #else
54 #define DPRINTF(dev, msg...)
55 #endif
56 
57 struct ti_am3359_cppi41_softc {
58 	device_t		dev;
59 	struct syscon *		syscon;
60 	struct resource *	res[4];
61 	bus_space_tag_t		bst;
62 	bus_space_handle_t	bsh;
63 	struct mtx		mtx;
64 };
65 
66 static struct resource_spec ti_am3359_cppi41_res_spec[] = {
67 	{ SYS_RES_MEMORY, 0, RF_ACTIVE | RF_SHAREABLE },
68 	{ SYS_RES_MEMORY, 1, RF_ACTIVE | RF_SHAREABLE },
69 	{ SYS_RES_MEMORY, 2, RF_ACTIVE | RF_SHAREABLE },
70 	{ SYS_RES_MEMORY, 3, RF_ACTIVE | RF_SHAREABLE },
71 	{ -1, 0 }
72 };
73 
74 /* Device */
75 static struct ofw_compat_data compat_data[] = {
76 	{ "ti,am3359-cppi41",	1 },
77 	{ NULL,		0 }
78 };
79 
80 static int
81 ti_am3359_cppi41_write_4(device_t dev, bus_addr_t addr, uint32_t val)
82 {
83 	struct ti_am3359_cppi41_softc *sc;
84 
85 	sc = device_get_softc(dev);
86 	DPRINTF(sc->dev, "offset=%lx write %x\n", addr, val);
87 	mtx_lock(&sc->mtx);
88 	bus_space_write_4(sc->bst, sc->bsh, addr, val);
89 	mtx_unlock(&sc->mtx);
90 	return (0);
91 }
92 
93 static uint32_t
94 ti_am3359_cppi41_read_4(device_t dev, bus_addr_t addr)
95 {
96 	struct ti_am3359_cppi41_softc *sc;
97 	uint32_t val;
98 
99 	sc = device_get_softc(dev);
100 
101 	mtx_lock(&sc->mtx);
102 	val = bus_space_read_4(sc->bst, sc->bsh, addr);
103 	mtx_unlock(&sc->mtx);
104 	DPRINTF(sc->dev, "offset=%lx Read %x\n", addr, val);
105 	return (val);
106 }
107 
108 /* device interface */
109 static int
110 ti_am3359_cppi41_probe(device_t dev)
111 {
112 	if (!ofw_bus_status_okay(dev))
113 		return (ENXIO);
114 
115 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
116 		return (ENXIO);
117 
118 	device_set_desc(dev, "TI AM3359 CPPI 41");
119 	return(BUS_PROBE_DEFAULT);
120 }
121 
122 static int
123 ti_am3359_cppi41_attach(device_t dev)
124 {
125 	struct ti_am3359_cppi41_softc *sc;
126 	uint32_t reg, reset_bit, timeout=10;
127 	uint64_t sysc_address;
128 	device_t parent;
129 
130 	sc = device_get_softc(dev);
131 	sc->dev = dev;
132 
133 	if (bus_alloc_resources(dev, ti_am3359_cppi41_res_spec, sc->res)) {
134 		device_printf(sc->dev, "Cant allocate resources\n");
135 		return (ENXIO);
136 	}
137 
138 	sc->dev = dev;
139 	sc->bst = rman_get_bustag(sc->res[0]);
140 	sc->bsh = rman_get_bushandle(sc->res[0]);
141 
142 	mtx_init(&sc->mtx, device_get_nameunit(sc->dev), NULL, MTX_DEF);
143 
144 	/* variant of am335x_usbss.c */
145 	DPRINTF(dev, "-- RESET USB --\n");
146 	parent = device_get_parent(dev);
147 	reset_bit = ti_sysc_get_soft_reset_bit(parent);
148 	if (reset_bit == 0) {
149 		DPRINTF(dev, "Dont have reset bit\n");
150 		return (0);
151 	}
152 	sysc_address = ti_sysc_get_sysc_address_offset_host(parent);
153 	DPRINTF(dev, "sysc_address %x\n", sysc_address);
154 	ti_am3359_cppi41_write_4(dev, sysc_address, reset_bit);
155 	DELAY(100);
156 	reg = ti_am3359_cppi41_read_4(dev, sysc_address);
157 	if ((reg & reset_bit) && timeout--) {
158 		DPRINTF(dev, "Reset still ongoing - wait a little bit longer\n");
159 		DELAY(100);
160 		reg = ti_am3359_cppi41_read_4(dev, sysc_address);
161 	}
162 	if (timeout == 0)
163 		device_printf(dev, "USB Reset timeout\n");
164 
165 	return (0);
166 }
167 
168 static device_method_t ti_am3359_cppi41_methods[] = {
169 	DEVMETHOD(device_probe,		ti_am3359_cppi41_probe),
170 	DEVMETHOD(device_attach,	ti_am3359_cppi41_attach),
171 
172 	DEVMETHOD_END
173 };
174 
175 DEFINE_CLASS_1(ti_am3359_cppi41, ti_am3359_cppi41_driver,
176     ti_am3359_cppi41_methods,sizeof(struct ti_am3359_cppi41_softc),
177     simplebus_driver);
178 
179 EARLY_DRIVER_MODULE(ti_am3359_cppi41, simplebus, ti_am3359_cppi41_driver, 0, 0,
180     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
181 MODULE_VERSION(ti_am3359_cppi41, 1);
182 MODULE_DEPEND(ti_am3359_cppi41, ti_sysc, 1, 1, 1);
183