xref: /freebsd/sys/arm/ti/am335x/am3359_cppi41.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
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 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/resource.h>
38 #include <sys/rman.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 
45 #include <dev/fdt/simplebus.h>
46 
47 #include <dev/ofw/openfirm.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 
51 #include <arm/ti/ti_sysc.h>
52 
53 #if 0
54 #define DPRINTF(dev, msg...) device_printf(dev, msg)
55 #else
56 #define DPRINTF(dev, msg...)
57 #endif
58 
59 struct ti_am3359_cppi41_softc {
60 	device_t		dev;
61 	struct syscon *		syscon;
62 	struct resource *	res[4];
63 	bus_space_tag_t		bst;
64 	bus_space_handle_t	bsh;
65 	struct mtx		mtx;
66 };
67 
68 static struct resource_spec ti_am3359_cppi41_res_spec[] = {
69 	{ SYS_RES_MEMORY, 0, RF_ACTIVE | RF_SHAREABLE },
70 	{ SYS_RES_MEMORY, 1, RF_ACTIVE | RF_SHAREABLE },
71 	{ SYS_RES_MEMORY, 2, RF_ACTIVE | RF_SHAREABLE },
72 	{ SYS_RES_MEMORY, 3, RF_ACTIVE | RF_SHAREABLE },
73 	{ -1, 0 }
74 };
75 
76 /* Device */
77 static struct ofw_compat_data compat_data[] = {
78 	{ "ti,am3359-cppi41",	1 },
79 	{ NULL,		0 }
80 };
81 
82 static int
83 ti_am3359_cppi41_write_4(device_t dev, bus_addr_t addr, uint32_t val)
84 {
85 	struct ti_am3359_cppi41_softc *sc;
86 
87 	sc = device_get_softc(dev);
88 	DPRINTF(sc->dev, "offset=%lx write %x\n", addr, val);
89 	mtx_lock(&sc->mtx);
90 	bus_space_write_4(sc->bst, sc->bsh, addr, val);
91 	mtx_unlock(&sc->mtx);
92 	return (0);
93 }
94 
95 static uint32_t
96 ti_am3359_cppi41_read_4(device_t dev, bus_addr_t addr)
97 {
98 	struct ti_am3359_cppi41_softc *sc;
99 	uint32_t val;
100 
101 	sc = device_get_softc(dev);
102 
103 	mtx_lock(&sc->mtx);
104 	val = bus_space_read_4(sc->bst, sc->bsh, addr);
105 	mtx_unlock(&sc->mtx);
106 	DPRINTF(sc->dev, "offset=%lx Read %x\n", addr, val);
107 	return (val);
108 }
109 
110 /* device interface */
111 static int
112 ti_am3359_cppi41_probe(device_t dev)
113 {
114 	if (!ofw_bus_status_okay(dev))
115 		return (ENXIO);
116 
117 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
118 		return (ENXIO);
119 
120 	device_set_desc(dev, "TI AM3359 CPPI 41");
121 	return(BUS_PROBE_DEFAULT);
122 }
123 
124 static int
125 ti_am3359_cppi41_attach(device_t dev)
126 {
127 	struct ti_am3359_cppi41_softc *sc;
128 	uint32_t reg, reset_bit, timeout=10;
129 	uint64_t sysc_address;
130 	device_t parent;
131 
132 	sc = device_get_softc(dev);
133 	sc->dev = dev;
134 
135 	if (bus_alloc_resources(dev, ti_am3359_cppi41_res_spec, sc->res)) {
136 		device_printf(sc->dev, "Cant allocate resources\n");
137 		return (ENXIO);
138 	}
139 
140 	sc->dev = dev;
141 	sc->bst = rman_get_bustag(sc->res[0]);
142 	sc->bsh = rman_get_bushandle(sc->res[0]);
143 
144 	mtx_init(&sc->mtx, device_get_nameunit(sc->dev), NULL, MTX_DEF);
145 
146 	/* variant of am335x_usbss.c */
147 	DPRINTF(dev, "-- RESET USB --\n");
148 	parent = device_get_parent(dev);
149 	reset_bit = ti_sysc_get_soft_reset_bit(parent);
150 	if (reset_bit == 0) {
151 		DPRINTF(dev, "Dont have reset bit\n");
152 		return (0);
153 	}
154 	sysc_address = ti_sysc_get_sysc_address_offset_host(parent);
155 	DPRINTF(dev, "sysc_address %x\n", sysc_address);
156 	ti_am3359_cppi41_write_4(dev, sysc_address, reset_bit);
157 	DELAY(100);
158 	reg = ti_am3359_cppi41_read_4(dev, sysc_address);
159 	if ((reg & reset_bit) && timeout--) {
160 		DPRINTF(dev, "Reset still ongoing - wait a little bit longer\n");
161 		DELAY(100);
162 		reg = ti_am3359_cppi41_read_4(dev, sysc_address);
163 	}
164 	if (timeout == 0)
165 		device_printf(dev, "USB Reset timeout\n");
166 
167 	return (0);
168 }
169 
170 static device_method_t ti_am3359_cppi41_methods[] = {
171 	DEVMETHOD(device_probe,		ti_am3359_cppi41_probe),
172 	DEVMETHOD(device_attach,	ti_am3359_cppi41_attach),
173 
174 	DEVMETHOD_END
175 };
176 
177 DEFINE_CLASS_1(ti_am3359_cppi41, ti_am3359_cppi41_driver,
178     ti_am3359_cppi41_methods,sizeof(struct ti_am3359_cppi41_softc),
179     simplebus_driver);
180 
181 EARLY_DRIVER_MODULE(ti_am3359_cppi41, simplebus, ti_am3359_cppi41_driver, 0, 0,
182     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
183 MODULE_VERSION(ti_am3359_cppi41, 1);
184 MODULE_DEPEND(ti_am3359_cppi41, ti_sysc, 1, 1, 1);
185