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