xref: /freebsd/sys/arm/ti/ti_mbox.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
15b8db1daSRui Paulo /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3af3dc4a7SPedro F. Giffuni  *
45b8db1daSRui Paulo  * Copyright (c) 2013 Rui Paulo <rpaulo@FreeBSD.org>
55b8db1daSRui Paulo  * All rights reserved.
65b8db1daSRui Paulo  *
75b8db1daSRui Paulo  * Redistribution and use in source and binary forms, with or without
85b8db1daSRui Paulo  * modification, are permitted provided that the following conditions
95b8db1daSRui Paulo  * are met:
105b8db1daSRui Paulo  * 1. Redistributions of source code must retain the above copyright
115b8db1daSRui Paulo  *    notice, this list of conditions and the following disclaimer.
125b8db1daSRui Paulo  * 2. Redistributions in binary form must reproduce the above copyright
135b8db1daSRui Paulo  *    notice, this list of conditions and the following disclaimer in the
145b8db1daSRui Paulo  *    documentation and/or other materials provided with the distribution.
155b8db1daSRui Paulo  *
165b8db1daSRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
175b8db1daSRui Paulo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
185b8db1daSRui Paulo  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
195b8db1daSRui Paulo  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
205b8db1daSRui Paulo  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
215b8db1daSRui Paulo  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
225b8db1daSRui Paulo  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
235b8db1daSRui Paulo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
245b8db1daSRui Paulo  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
255b8db1daSRui Paulo  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
265b8db1daSRui Paulo  * POSSIBILITY OF SUCH DAMAGE.
275b8db1daSRui Paulo  */
28*fdafd315SWarner Losh 
295b8db1daSRui Paulo #include <sys/param.h>
305b8db1daSRui Paulo #include <sys/systm.h>
315b8db1daSRui Paulo #include <sys/bus.h>
325b8db1daSRui Paulo #include <sys/kernel.h>
335b8db1daSRui Paulo #include <sys/module.h>
345b8db1daSRui Paulo #include <sys/malloc.h>
355b8db1daSRui Paulo #include <sys/rman.h>
365b8db1daSRui Paulo #include <sys/timeet.h>
375b8db1daSRui Paulo #include <sys/timetc.h>
385b8db1daSRui Paulo #include <sys/watchdog.h>
395b8db1daSRui Paulo #include <machine/bus.h>
405b8db1daSRui Paulo #include <machine/cpu.h>
415b8db1daSRui Paulo #include <machine/frame.h>
425b8db1daSRui Paulo #include <machine/intr.h>
435b8db1daSRui Paulo 
445b8db1daSRui Paulo #include <dev/ofw/openfirm.h>
455b8db1daSRui Paulo #include <dev/ofw/ofw_bus.h>
465b8db1daSRui Paulo #include <dev/ofw/ofw_bus_subr.h>
475b8db1daSRui Paulo 
485b8db1daSRui Paulo #include <machine/bus.h>
495b8db1daSRui Paulo 
505b8db1daSRui Paulo #include <arm/ti/ti_mbox.h>
510050ea24SMichal Meloun #include <arm/ti/ti_sysc.h>
525b8db1daSRui Paulo 
535b8db1daSRui Paulo #include "mbox_if.h"
545b8db1daSRui Paulo 
555b8db1daSRui Paulo #ifdef DEBUG
565b8db1daSRui Paulo #define	DPRINTF(fmt, ...)	do {	\
575b8db1daSRui Paulo 	printf("%s: ", __func__);	\
585b8db1daSRui Paulo 	printf(fmt, __VA_ARGS__);	\
595b8db1daSRui Paulo } while (0)
605b8db1daSRui Paulo #else
615b8db1daSRui Paulo #define	DPRINTF(fmt, ...)
625b8db1daSRui Paulo #endif
635b8db1daSRui Paulo 
645b8db1daSRui Paulo static device_probe_t		ti_mbox_probe;
655b8db1daSRui Paulo static device_attach_t		ti_mbox_attach;
665b8db1daSRui Paulo static device_detach_t		ti_mbox_detach;
675b8db1daSRui Paulo static void			ti_mbox_intr(void *);
685b8db1daSRui Paulo static int			ti_mbox_read(device_t, int, uint32_t *);
695b8db1daSRui Paulo static int			ti_mbox_write(device_t, int, uint32_t);
705b8db1daSRui Paulo 
715b8db1daSRui Paulo struct ti_mbox_softc {
725b8db1daSRui Paulo 	struct mtx		sc_mtx;
735b8db1daSRui Paulo 	struct resource		*sc_mem_res;
745b8db1daSRui Paulo 	struct resource		*sc_irq_res;
755b8db1daSRui Paulo 	void			*sc_intr;
765b8db1daSRui Paulo 	bus_space_tag_t		sc_bt;
775b8db1daSRui Paulo 	bus_space_handle_t	sc_bh;
785b8db1daSRui Paulo };
795b8db1daSRui Paulo 
805b8db1daSRui Paulo #define	TI_MBOX_LOCK(sc)	mtx_lock(&(sc)->sc_mtx)
815b8db1daSRui Paulo #define	TI_MBOX_UNLOCK(sc)	mtx_unlock(&(sc)->sc_mtx)
825b8db1daSRui Paulo 
835b8db1daSRui Paulo static device_method_t ti_mbox_methods[] = {
845b8db1daSRui Paulo 	DEVMETHOD(device_probe,		ti_mbox_probe),
855b8db1daSRui Paulo 	DEVMETHOD(device_attach,	ti_mbox_attach),
865b8db1daSRui Paulo 	DEVMETHOD(device_detach,	ti_mbox_detach),
875b8db1daSRui Paulo 
885b8db1daSRui Paulo 	DEVMETHOD(mbox_read,		ti_mbox_read),
895b8db1daSRui Paulo 	DEVMETHOD(mbox_write,		ti_mbox_write),
905b8db1daSRui Paulo 
915b8db1daSRui Paulo 	DEVMETHOD_END
925b8db1daSRui Paulo };
935b8db1daSRui Paulo 
945b8db1daSRui Paulo static driver_t ti_mbox_driver = {
955b8db1daSRui Paulo 	"ti_mbox",
965b8db1daSRui Paulo 	ti_mbox_methods,
975b8db1daSRui Paulo 	sizeof(struct ti_mbox_softc)
985b8db1daSRui Paulo };
995b8db1daSRui Paulo 
1008537e671SJohn Baldwin DRIVER_MODULE(ti_mbox, simplebus, ti_mbox_driver, 0, 0);
1010050ea24SMichal Meloun MODULE_DEPEND(ti_mbox, ti_sysc, 1, 1, 1);
1025b8db1daSRui Paulo 
1035b8db1daSRui Paulo static __inline uint32_t
ti_mbox_reg_read(struct ti_mbox_softc * sc,uint16_t reg)1045b8db1daSRui Paulo ti_mbox_reg_read(struct ti_mbox_softc *sc, uint16_t reg)
1055b8db1daSRui Paulo {
1065b8db1daSRui Paulo 	return (bus_space_read_4(sc->sc_bt, sc->sc_bh, reg));
1075b8db1daSRui Paulo }
1085b8db1daSRui Paulo 
1095b8db1daSRui Paulo static __inline void
ti_mbox_reg_write(struct ti_mbox_softc * sc,uint16_t reg,uint32_t val)1105b8db1daSRui Paulo ti_mbox_reg_write(struct ti_mbox_softc *sc, uint16_t reg, uint32_t val)
1115b8db1daSRui Paulo {
1125b8db1daSRui Paulo 	bus_space_write_4(sc->sc_bt, sc->sc_bh, reg, val);
1135b8db1daSRui Paulo }
1145b8db1daSRui Paulo 
1155b8db1daSRui Paulo static int
ti_mbox_probe(device_t dev)1165b8db1daSRui Paulo ti_mbox_probe(device_t dev)
1175b8db1daSRui Paulo {
118add35ed5SIan Lepore 
119add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
120add35ed5SIan Lepore 		return (ENXIO);
121add35ed5SIan Lepore 
1225b03aba6SOleksandr Tymoshenko 	if (ofw_bus_is_compatible(dev, "ti,omap4-mailbox")) {
1235b8db1daSRui Paulo 		device_set_desc(dev, "TI System Mailbox");
1245b8db1daSRui Paulo 		return (BUS_PROBE_DEFAULT);
1255b8db1daSRui Paulo 	}
1265b8db1daSRui Paulo 
1275b8db1daSRui Paulo 	return (ENXIO);
1285b8db1daSRui Paulo }
1295b8db1daSRui Paulo 
1305b8db1daSRui Paulo static int
ti_mbox_attach(device_t dev)1315b8db1daSRui Paulo ti_mbox_attach(device_t dev)
1325b8db1daSRui Paulo {
1335b8db1daSRui Paulo 	struct ti_mbox_softc *sc;
1345b8db1daSRui Paulo 	int rid, delay, chan;
1355b8db1daSRui Paulo 	uint32_t rev, sysconfig;
1365b8db1daSRui Paulo 
1370050ea24SMichal Meloun 	if (ti_sysc_clock_enable(device_get_parent(dev)) != 0) {
1385b8db1daSRui Paulo 		device_printf(dev, "could not enable MBOX clock\n");
1395b8db1daSRui Paulo 		return (ENXIO);
1405b8db1daSRui Paulo 	}
1410050ea24SMichal Meloun 
1425b8db1daSRui Paulo 	sc = device_get_softc(dev);
1435b8db1daSRui Paulo 	rid = 0;
1446eebe850SJohn-Mark Gurney 	mtx_init(&sc->sc_mtx, "TI mbox", NULL, MTX_DEF);
1455b8db1daSRui Paulo 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1465b8db1daSRui Paulo 	    RF_ACTIVE);
1475b8db1daSRui Paulo 	if (sc->sc_mem_res == NULL) {
1485b8db1daSRui Paulo 		device_printf(dev, "could not allocate memory resource\n");
1495b8db1daSRui Paulo 		return (ENXIO);
1505b8db1daSRui Paulo 	}
1515b8db1daSRui Paulo 	sc->sc_bt = rman_get_bustag(sc->sc_mem_res);
1525b8db1daSRui Paulo 	sc->sc_bh = rman_get_bushandle(sc->sc_mem_res);
1535b8db1daSRui Paulo 	rid = 0;
1545b8db1daSRui Paulo 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1555b8db1daSRui Paulo 	    RF_ACTIVE);
1565b8db1daSRui Paulo 	if (sc->sc_irq_res == NULL) {
1575b8db1daSRui Paulo 		device_printf(dev, "could not allocate interrupt resource\n");
1585b8db1daSRui Paulo 		ti_mbox_detach(dev);
1595b8db1daSRui Paulo 		return (ENXIO);
1605b8db1daSRui Paulo 	}
1615b8db1daSRui Paulo 	if (bus_setup_intr(dev, sc->sc_irq_res, INTR_MPSAFE | INTR_TYPE_MISC,
1625b8db1daSRui Paulo 	    NULL, ti_mbox_intr, sc, &sc->sc_intr) != 0) {
1635b8db1daSRui Paulo 		device_printf(dev, "unable to setup the interrupt handler\n");
1645b8db1daSRui Paulo 		ti_mbox_detach(dev);
1655b8db1daSRui Paulo 		return (ENXIO);
1665b8db1daSRui Paulo 	}
1675b8db1daSRui Paulo 	/*
1685b8db1daSRui Paulo 	 * Reset the controller.
1695b8db1daSRui Paulo 	 */
1705b8db1daSRui Paulo 	sysconfig = ti_mbox_reg_read(sc, TI_MBOX_SYSCONFIG);
1715b8db1daSRui Paulo 	DPRINTF("initial sysconfig %d\n", sysconfig);
1725b8db1daSRui Paulo 	sysconfig |= TI_MBOX_SYSCONFIG_SOFTRST;
173ecbe5044SJohn Baldwin 	ti_mbox_reg_write(sc, TI_MBOX_SYSCONFIG, sysconfig);
1745b8db1daSRui Paulo 	delay = 100;
1755b8db1daSRui Paulo 	while (ti_mbox_reg_read(sc, TI_MBOX_SYSCONFIG) &
1765b8db1daSRui Paulo 	    TI_MBOX_SYSCONFIG_SOFTRST) {
1775b8db1daSRui Paulo 		delay--;
1785b8db1daSRui Paulo 		DELAY(10);
1795b8db1daSRui Paulo 	}
1805b8db1daSRui Paulo 	if (delay == 0) {
1815b8db1daSRui Paulo 		device_printf(dev, "controller reset failed\n");
1825b8db1daSRui Paulo 		ti_mbox_detach(dev);
1835b8db1daSRui Paulo 		return (ENXIO);
1845b8db1daSRui Paulo 	}
1855b8db1daSRui Paulo 	/*
1865b8db1daSRui Paulo 	 * Enable smart idle mode.
1875b8db1daSRui Paulo 	 */
1885b8db1daSRui Paulo 	ti_mbox_reg_write(sc, TI_MBOX_SYSCONFIG,
1895b8db1daSRui Paulo 	    ti_mbox_reg_read(sc, TI_MBOX_SYSCONFIG) | TI_MBOX_SYSCONFIG_SMARTIDLE);
1900050ea24SMichal Meloun 	rev = ti_mbox_reg_read(sc,
1910050ea24SMichal Meloun 	    ti_sysc_get_rev_address_offset_host(device_get_parent(dev)));
1925b8db1daSRui Paulo 	DPRINTF("rev %d\n", rev);
1935b8db1daSRui Paulo 	device_printf(dev, "revision %d.%d\n", (rev >> 8) & 0x4, rev & 0x40);
1945b8db1daSRui Paulo 	/*
1955b8db1daSRui Paulo 	 * Enable message interrupts.
1965b8db1daSRui Paulo 	 */
1975b8db1daSRui Paulo 	for (chan = 0; chan < 8; chan++)
1985b8db1daSRui Paulo 		ti_mbox_reg_write(sc, TI_MBOX_IRQENABLE_SET(chan), 1);
1995b8db1daSRui Paulo 
2005b8db1daSRui Paulo 	return (0);
2015b8db1daSRui Paulo }
2025b8db1daSRui Paulo 
2035b8db1daSRui Paulo static int
ti_mbox_detach(device_t dev)2045b8db1daSRui Paulo ti_mbox_detach(device_t dev)
2055b8db1daSRui Paulo {
2065b8db1daSRui Paulo 	struct ti_mbox_softc *sc;
2075b8db1daSRui Paulo 
2085b8db1daSRui Paulo 	sc = device_get_softc(dev);
2095b8db1daSRui Paulo 	if (sc->sc_intr)
2105b8db1daSRui Paulo 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr);
2115b8db1daSRui Paulo 	if (sc->sc_irq_res)
2125b8db1daSRui Paulo 		bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->sc_irq_res),
2135b8db1daSRui Paulo 		    sc->sc_irq_res);
2145b8db1daSRui Paulo 	if (sc->sc_mem_res)
2155b8db1daSRui Paulo 		bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->sc_mem_res),
2165b8db1daSRui Paulo 		    sc->sc_mem_res);
2175b8db1daSRui Paulo 
2185b8db1daSRui Paulo 	return (0);
2195b8db1daSRui Paulo }
2205b8db1daSRui Paulo 
2215b8db1daSRui Paulo static void
ti_mbox_intr(void * arg)2225b8db1daSRui Paulo ti_mbox_intr(void *arg)
2235b8db1daSRui Paulo {
2240d46a96aSJohn Baldwin 	DPRINTF("interrupt %p", arg);
2255b8db1daSRui Paulo }
2265b8db1daSRui Paulo 
2275b8db1daSRui Paulo static int
ti_mbox_read(device_t dev,int chan,uint32_t * data)2285b8db1daSRui Paulo ti_mbox_read(device_t dev, int chan, uint32_t *data)
2295b8db1daSRui Paulo {
2305b8db1daSRui Paulo 	struct ti_mbox_softc *sc;
2315b8db1daSRui Paulo 
2325b8db1daSRui Paulo 	if (chan < 0 || chan > 7)
2335b8db1daSRui Paulo 		return (EINVAL);
2345b8db1daSRui Paulo 	sc = device_get_softc(dev);
2355b8db1daSRui Paulo 
2365b8db1daSRui Paulo 	return (ti_mbox_reg_read(sc, TI_MBOX_MESSAGE(chan)));
2375b8db1daSRui Paulo }
2385b8db1daSRui Paulo 
2395b8db1daSRui Paulo static int
ti_mbox_write(device_t dev,int chan,uint32_t data)2405b8db1daSRui Paulo ti_mbox_write(device_t dev, int chan, uint32_t data)
2415b8db1daSRui Paulo {
2425b8db1daSRui Paulo 	int limit = 500;
2435b8db1daSRui Paulo 	struct ti_mbox_softc *sc;
2445b8db1daSRui Paulo 
2455b8db1daSRui Paulo 	if (chan < 0 || chan > 7)
2465b8db1daSRui Paulo 		return (EINVAL);
2475b8db1daSRui Paulo 	sc = device_get_softc(dev);
2485b8db1daSRui Paulo 	TI_MBOX_LOCK(sc);
2495b8db1daSRui Paulo 	/* XXX implement interrupt method */
2505b8db1daSRui Paulo 	while (ti_mbox_reg_read(sc, TI_MBOX_FIFOSTATUS(chan)) == 1 &&
2515b8db1daSRui Paulo 	    limit--) {
2525b8db1daSRui Paulo 		DELAY(10);
2535b8db1daSRui Paulo 	}
2545b8db1daSRui Paulo 	if (limit == 0) {
2555b8db1daSRui Paulo 		device_printf(dev, "FIFOSTAUS%d stuck\n", chan);
2565b8db1daSRui Paulo 		TI_MBOX_UNLOCK(sc);
2575b8db1daSRui Paulo 		return (EAGAIN);
2585b8db1daSRui Paulo 	}
2595b8db1daSRui Paulo 	ti_mbox_reg_write(sc, TI_MBOX_MESSAGE(chan), data);
2605b8db1daSRui Paulo 
2615b8db1daSRui Paulo 	return (0);
2625b8db1daSRui Paulo }
263