xref: /freebsd-13-stable/sys/arm/ti/ti_mbox.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013 Rui Paulo <rpaulo@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/malloc.h>
35 #include <sys/rman.h>
36 #include <sys/timeet.h>
37 #include <sys/timetc.h>
38 #include <sys/watchdog.h>
39 #include <machine/bus.h>
40 #include <machine/cpu.h>
41 #include <machine/frame.h>
42 #include <machine/intr.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 <machine/bus.h>
49 
50 #include <arm/ti/ti_mbox.h>
51 #include <arm/ti/ti_sysc.h>
52 
53 #include "mbox_if.h"
54 
55 #ifdef DEBUG
56 #define	DPRINTF(fmt, ...)	do {	\
57 	printf("%s: ", __func__);	\
58 	printf(fmt, __VA_ARGS__);	\
59 } while (0)
60 #else
61 #define	DPRINTF(fmt, ...)
62 #endif
63 
64 static device_probe_t		ti_mbox_probe;
65 static device_attach_t		ti_mbox_attach;
66 static device_detach_t		ti_mbox_detach;
67 static void			ti_mbox_intr(void *);
68 static int			ti_mbox_read(device_t, int, uint32_t *);
69 static int			ti_mbox_write(device_t, int, uint32_t);
70 
71 struct ti_mbox_softc {
72 	struct mtx		sc_mtx;
73 	struct resource		*sc_mem_res;
74 	struct resource		*sc_irq_res;
75 	void			*sc_intr;
76 	bus_space_tag_t		sc_bt;
77 	bus_space_handle_t	sc_bh;
78 };
79 
80 #define	TI_MBOX_LOCK(sc)	mtx_lock(&(sc)->sc_mtx)
81 #define	TI_MBOX_UNLOCK(sc)	mtx_unlock(&(sc)->sc_mtx)
82 
83 static device_method_t ti_mbox_methods[] = {
84 	DEVMETHOD(device_probe,		ti_mbox_probe),
85 	DEVMETHOD(device_attach,	ti_mbox_attach),
86 	DEVMETHOD(device_detach,	ti_mbox_detach),
87 
88 	DEVMETHOD(mbox_read,		ti_mbox_read),
89 	DEVMETHOD(mbox_write,		ti_mbox_write),
90 
91 	DEVMETHOD_END
92 };
93 
94 static driver_t ti_mbox_driver = {
95 	"ti_mbox",
96 	ti_mbox_methods,
97 	sizeof(struct ti_mbox_softc)
98 };
99 
100 static devclass_t ti_mbox_devclass;
101 
102 DRIVER_MODULE(ti_mbox, simplebus, ti_mbox_driver, ti_mbox_devclass, 0, 0);
103 MODULE_DEPEND(ti_mbox, ti_sysc, 1, 1, 1);
104 
105 static __inline uint32_t
ti_mbox_reg_read(struct ti_mbox_softc * sc,uint16_t reg)106 ti_mbox_reg_read(struct ti_mbox_softc *sc, uint16_t reg)
107 {
108 	return (bus_space_read_4(sc->sc_bt, sc->sc_bh, reg));
109 }
110 
111 static __inline void
ti_mbox_reg_write(struct ti_mbox_softc * sc,uint16_t reg,uint32_t val)112 ti_mbox_reg_write(struct ti_mbox_softc *sc, uint16_t reg, uint32_t val)
113 {
114 	bus_space_write_4(sc->sc_bt, sc->sc_bh, reg, val);
115 }
116 
117 static int
ti_mbox_probe(device_t dev)118 ti_mbox_probe(device_t dev)
119 {
120 
121 	if (!ofw_bus_status_okay(dev))
122 		return (ENXIO);
123 
124 	if (ofw_bus_is_compatible(dev, "ti,omap4-mailbox")) {
125 		device_set_desc(dev, "TI System Mailbox");
126 		return (BUS_PROBE_DEFAULT);
127 	}
128 
129 	return (ENXIO);
130 }
131 
132 static int
ti_mbox_attach(device_t dev)133 ti_mbox_attach(device_t dev)
134 {
135 	struct ti_mbox_softc *sc;
136 	int rid, delay, chan;
137 	uint32_t rev, sysconfig;
138 
139 	if (ti_sysc_clock_enable(device_get_parent(dev)) != 0) {
140 		device_printf(dev, "could not enable MBOX clock\n");
141 		return (ENXIO);
142 	}
143 
144 	sc = device_get_softc(dev);
145 	rid = 0;
146 	mtx_init(&sc->sc_mtx, "TI mbox", NULL, MTX_DEF);
147 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
148 	    RF_ACTIVE);
149 	if (sc->sc_mem_res == NULL) {
150 		device_printf(dev, "could not allocate memory resource\n");
151 		return (ENXIO);
152 	}
153 	sc->sc_bt = rman_get_bustag(sc->sc_mem_res);
154 	sc->sc_bh = rman_get_bushandle(sc->sc_mem_res);
155 	rid = 0;
156 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
157 	    RF_ACTIVE);
158 	if (sc->sc_irq_res == NULL) {
159 		device_printf(dev, "could not allocate interrupt resource\n");
160 		ti_mbox_detach(dev);
161 		return (ENXIO);
162 	}
163 	if (bus_setup_intr(dev, sc->sc_irq_res, INTR_MPSAFE | INTR_TYPE_MISC,
164 	    NULL, ti_mbox_intr, sc, &sc->sc_intr) != 0) {
165 		device_printf(dev, "unable to setup the interrupt handler\n");
166 		ti_mbox_detach(dev);
167 		return (ENXIO);
168 	}
169 	/*
170 	 * Reset the controller.
171 	 */
172 	sysconfig = ti_mbox_reg_read(sc, TI_MBOX_SYSCONFIG);
173 	DPRINTF("initial sysconfig %d\n", sysconfig);
174 	sysconfig |= TI_MBOX_SYSCONFIG_SOFTRST;
175 	ti_mbox_reg_write(sc, TI_MBOX_SYSCONFIG, sysconfig);
176 	delay = 100;
177 	while (ti_mbox_reg_read(sc, TI_MBOX_SYSCONFIG) &
178 	    TI_MBOX_SYSCONFIG_SOFTRST) {
179 		delay--;
180 		DELAY(10);
181 	}
182 	if (delay == 0) {
183 		device_printf(dev, "controller reset failed\n");
184 		ti_mbox_detach(dev);
185 		return (ENXIO);
186 	}
187 	/*
188 	 * Enable smart idle mode.
189 	 */
190 	ti_mbox_reg_write(sc, TI_MBOX_SYSCONFIG,
191 	    ti_mbox_reg_read(sc, TI_MBOX_SYSCONFIG) | TI_MBOX_SYSCONFIG_SMARTIDLE);
192 	rev = ti_mbox_reg_read(sc,
193 	    ti_sysc_get_rev_address_offset_host(device_get_parent(dev)));
194 	DPRINTF("rev %d\n", rev);
195 	device_printf(dev, "revision %d.%d\n", (rev >> 8) & 0x4, rev & 0x40);
196 	/*
197 	 * Enable message interrupts.
198 	 */
199 	for (chan = 0; chan < 8; chan++)
200 		ti_mbox_reg_write(sc, TI_MBOX_IRQENABLE_SET(chan), 1);
201 
202 	return (0);
203 }
204 
205 static int
ti_mbox_detach(device_t dev)206 ti_mbox_detach(device_t dev)
207 {
208 	struct ti_mbox_softc *sc;
209 
210 	sc = device_get_softc(dev);
211 	if (sc->sc_intr)
212 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr);
213 	if (sc->sc_irq_res)
214 		bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->sc_irq_res),
215 		    sc->sc_irq_res);
216 	if (sc->sc_mem_res)
217 		bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->sc_mem_res),
218 		    sc->sc_mem_res);
219 
220 	return (0);
221 }
222 
223 static void
ti_mbox_intr(void * arg)224 ti_mbox_intr(void *arg)
225 {
226 	struct ti_mbox_softc *sc;
227 
228 	sc = arg;
229 	DPRINTF("interrupt %p", sc);
230 }
231 
232 static int
ti_mbox_read(device_t dev,int chan,uint32_t * data)233 ti_mbox_read(device_t dev, int chan, uint32_t *data)
234 {
235 	struct ti_mbox_softc *sc;
236 
237 	if (chan < 0 || chan > 7)
238 		return (EINVAL);
239 	sc = device_get_softc(dev);
240 
241 	return (ti_mbox_reg_read(sc, TI_MBOX_MESSAGE(chan)));
242 }
243 
244 static int
ti_mbox_write(device_t dev,int chan,uint32_t data)245 ti_mbox_write(device_t dev, int chan, uint32_t data)
246 {
247 	int limit = 500;
248 	struct ti_mbox_softc *sc;
249 
250 	if (chan < 0 || chan > 7)
251 		return (EINVAL);
252 	sc = device_get_softc(dev);
253 	TI_MBOX_LOCK(sc);
254 	/* XXX implement interrupt method */
255 	while (ti_mbox_reg_read(sc, TI_MBOX_FIFOSTATUS(chan)) == 1 &&
256 	    limit--) {
257 		DELAY(10);
258 	}
259 	if (limit == 0) {
260 		device_printf(dev, "FIFOSTAUS%d stuck\n", chan);
261 		TI_MBOX_UNLOCK(sc);
262 		return (EAGAIN);
263 	}
264 	ti_mbox_reg_write(sc, TI_MBOX_MESSAGE(chan), data);
265 
266 	return (0);
267 }
268