Path: blob/main/sys/arm/ti/am335x/am3359_cppi41.c
105585 views
/*-1* Copyright (c) 2019 Emmanuel Vadot <[email protected]>2*3* Copyright (c) 2020 Oskar Holmlund <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,19* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;20* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED21* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,22* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/26/* Based on sys/arm/ti/ti_sysc.c */2728#include <sys/param.h>29#include <sys/systm.h>30#include <sys/kernel.h>31#include <sys/module.h>32#include <sys/bus.h>33#include <sys/resource.h>34#include <sys/rman.h>35#include <sys/lock.h>36#include <sys/mutex.h>3738#include <machine/bus.h>39#include <machine/resource.h>4041#include <dev/fdt/simplebus.h>4243#include <dev/ofw/openfirm.h>44#include <dev/ofw/ofw_bus.h>45#include <dev/ofw/ofw_bus_subr.h>4647#include <arm/ti/ti_sysc.h>4849#if 050#define DPRINTF(dev, msg...) device_printf(dev, msg)51#else52#define DPRINTF(dev, msg...)53#endif5455struct ti_am3359_cppi41_softc {56device_t dev;57struct syscon * syscon;58struct resource * res[4];59bus_space_tag_t bst;60bus_space_handle_t bsh;61struct mtx mtx;62};6364static struct resource_spec ti_am3359_cppi41_res_spec[] = {65{ SYS_RES_MEMORY, 0, RF_ACTIVE | RF_SHAREABLE },66{ SYS_RES_MEMORY, 1, RF_ACTIVE | RF_SHAREABLE },67{ SYS_RES_MEMORY, 2, RF_ACTIVE | RF_SHAREABLE },68{ SYS_RES_MEMORY, 3, RF_ACTIVE | RF_SHAREABLE },69{ -1, 0 }70};7172/* Device */73static struct ofw_compat_data compat_data[] = {74{ "ti,am3359-cppi41", 1 },75{ NULL, 0 }76};7778static int79ti_am3359_cppi41_write_4(device_t dev, bus_addr_t addr, uint32_t val)80{81struct ti_am3359_cppi41_softc *sc;8283sc = device_get_softc(dev);84DPRINTF(sc->dev, "offset=%lx write %x\n", addr, val);85mtx_lock(&sc->mtx);86bus_space_write_4(sc->bst, sc->bsh, addr, val);87mtx_unlock(&sc->mtx);88return (0);89}9091static uint32_t92ti_am3359_cppi41_read_4(device_t dev, bus_addr_t addr)93{94struct ti_am3359_cppi41_softc *sc;95uint32_t val;9697sc = device_get_softc(dev);9899mtx_lock(&sc->mtx);100val = bus_space_read_4(sc->bst, sc->bsh, addr);101mtx_unlock(&sc->mtx);102DPRINTF(sc->dev, "offset=%lx Read %x\n", addr, val);103return (val);104}105106/* device interface */107static int108ti_am3359_cppi41_probe(device_t dev)109{110if (!ofw_bus_status_okay(dev))111return (ENXIO);112113if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)114return (ENXIO);115116device_set_desc(dev, "TI AM3359 CPPI 41");117return(BUS_PROBE_DEFAULT);118}119120static int121ti_am3359_cppi41_attach(device_t dev)122{123struct ti_am3359_cppi41_softc *sc;124uint32_t reg, reset_bit, timeout=10;125uint64_t sysc_address;126device_t parent;127128sc = device_get_softc(dev);129sc->dev = dev;130131if (bus_alloc_resources(dev, ti_am3359_cppi41_res_spec, sc->res)) {132device_printf(sc->dev, "Cant allocate resources\n");133return (ENXIO);134}135136sc->dev = dev;137sc->bst = rman_get_bustag(sc->res[0]);138sc->bsh = rman_get_bushandle(sc->res[0]);139140mtx_init(&sc->mtx, device_get_nameunit(sc->dev), NULL, MTX_DEF);141142/* variant of am335x_usbss.c */143DPRINTF(dev, "-- RESET USB --\n");144parent = device_get_parent(dev);145reset_bit = ti_sysc_get_soft_reset_bit(parent);146if (reset_bit == 0) {147DPRINTF(dev, "Dont have reset bit\n");148return (0);149}150sysc_address = ti_sysc_get_sysc_address_offset_host(parent);151DPRINTF(dev, "sysc_address %x\n", sysc_address);152ti_am3359_cppi41_write_4(dev, sysc_address, reset_bit);153DELAY(100);154reg = ti_am3359_cppi41_read_4(dev, sysc_address);155if ((reg & reset_bit) && timeout--) {156DPRINTF(dev, "Reset still ongoing - wait a little bit longer\n");157DELAY(100);158reg = ti_am3359_cppi41_read_4(dev, sysc_address);159}160if (timeout == 0)161device_printf(dev, "USB Reset timeout\n");162163return (0);164}165166static device_method_t ti_am3359_cppi41_methods[] = {167DEVMETHOD(device_probe, ti_am3359_cppi41_probe),168DEVMETHOD(device_attach, ti_am3359_cppi41_attach),169170DEVMETHOD_END171};172173DEFINE_CLASS_1(ti_am3359_cppi41, ti_am3359_cppi41_driver,174ti_am3359_cppi41_methods,sizeof(struct ti_am3359_cppi41_softc),175simplebus_driver);176177EARLY_DRIVER_MODULE(ti_am3359_cppi41, simplebus, ti_am3359_cppi41_driver, 0, 0,178BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);179MODULE_VERSION(ti_am3359_cppi41, 1);180MODULE_DEPEND(ti_am3359_cppi41, ti_sysc, 1, 1, 1);181182183