Path: blob/main/sys/contrib/vchiq/interface/vchiq_arm/vchiq_kmod.c
48383 views
/*-1* Copyright (c) 2012-2015 Oleksandr Tymoshenko <[email protected]>2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND13* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE15* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE16* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS18* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)19* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT20* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY21* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF22* SUCH DAMAGE.23*/2425#include <sys/cdefs.h>26__FBSDID("$FreeBSD$");2728#include <sys/param.h>29#include <sys/systm.h>30#include <sys/bus.h>31#include <sys/kernel.h>32#include <sys/module.h>33#include <sys/malloc.h>34#include <sys/rman.h>35#include <sys/timeet.h>36#include <sys/timetc.h>37#include <sys/watchdog.h>38#include <machine/bus.h>39#include <machine/cpu.h>40#include <machine/frame.h>41#include <machine/intr.h>4243#include <dev/fdt/fdt_common.h>44#include <dev/ofw/openfirm.h>45#include <dev/ofw/ofw_bus.h>46#include <dev/ofw/ofw_bus_subr.h>4748#include <machine/bus.h>49#include <machine/fdt.h>5051#include "vchiq_arm.h"52#include "vchiq_2835.h"5354#define VCHIQ_LOCK do { \55mtx_lock(&bcm_vchiq_sc->lock); \56} while(0)5758#define VCHIQ_UNLOCK do { \59mtx_unlock(&bcm_vchiq_sc->lock); \60} while(0)6162#ifdef DEBUG63#define dprintf(fmt, args...) printf(fmt, ##args)64#else65#define dprintf(fmt, args...)66#endif6768struct bcm_vchiq_softc {69struct mtx lock;70struct resource * mem_res;71struct resource * irq_res;72void* intr_hl;73bus_space_tag_t bst;74bus_space_handle_t bsh;75int regs_offset;76};7778static struct bcm_vchiq_softc *bcm_vchiq_sc = NULL;7980#define BSD_DTB 181#define UPSTREAM_DTB 282static struct ofw_compat_data compat_data[] = {83{"broadcom,bcm2835-vchiq", BSD_DTB},84{"brcm,bcm2835-vchiq", UPSTREAM_DTB},85{"brcm,bcm2711-vchiq", UPSTREAM_DTB},86{NULL, 0}87};8889#define vchiq_read_4(reg) \90bus_space_read_4(bcm_vchiq_sc->bst, bcm_vchiq_sc->bsh, (reg) + \91bcm_vchiq_sc->regs_offset)92#define vchiq_write_4(reg, val) \93bus_space_write_4(bcm_vchiq_sc->bst, bcm_vchiq_sc->bsh, (reg) + \94bcm_vchiq_sc->regs_offset, val)9596/*97* Extern functions */98void vchiq_exit(void);99int vchiq_init(void);100101extern VCHIQ_STATE_T g_state;102extern int g_cache_line_size;103104static void105bcm_vchiq_intr(void *arg)106{107VCHIQ_STATE_T *state = &g_state;108unsigned int status;109110/* Read (and clear) the doorbell */111status = vchiq_read_4(0x40);112113if (status & 0x4) { /* Was the doorbell rung? */114remote_event_pollall(state);115}116}117118void119remote_event_signal(REMOTE_EVENT_T *event)120{121event->fired = 1;122123/* The test on the next line also ensures the write on the previous line124has completed */125if (event->armed) {126/* trigger vc interrupt */127dsb();128vchiq_write_4(0x48, 0);129}130}131132static int133bcm_vchiq_probe(device_t dev)134{135136if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)137return (ENXIO);138139device_set_desc(dev, "BCM2835 VCHIQ");140return (BUS_PROBE_DEFAULT);141}142143static int144bcm_vchiq_attach(device_t dev)145{146struct bcm_vchiq_softc *sc = device_get_softc(dev);147phandle_t node;148pcell_t cell;149int rid = 0;150151if (bcm_vchiq_sc != NULL)152return (EINVAL);153154sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);155if (sc->mem_res == NULL) {156device_printf(dev, "could not allocate memory resource\n");157return (ENXIO);158}159160sc->bst = rman_get_bustag(sc->mem_res);161sc->bsh = rman_get_bushandle(sc->mem_res);162163rid = 0;164sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);165if (sc->irq_res == NULL) {166device_printf(dev, "could not allocate interrupt resource\n");167return (ENXIO);168}169170if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == UPSTREAM_DTB)171sc->regs_offset = -0x40;172173node = ofw_bus_get_node(dev);174if ((OF_getencprop(node, "cache-line-size", &cell, sizeof(cell))) > 0)175g_cache_line_size = cell;176177vchiq_core_initialize();178179/* Setup and enable the timer */180if (bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,181NULL, bcm_vchiq_intr, sc,182&sc->intr_hl) != 0) {183bus_release_resource(dev, SYS_RES_IRQ, rid,184sc->irq_res);185device_printf(dev, "Unable to setup the clock irq handler.\n");186return (ENXIO);187}188189mtx_init(&sc->lock, "vchiq", 0, MTX_DEF);190bcm_vchiq_sc = sc;191192vchiq_init();193194bus_identify_children(dev);195bus_attach_children(dev);196197return (0);198}199200static int201bcm_vchiq_detach(device_t dev)202{203struct bcm_vchiq_softc *sc = device_get_softc(dev);204205vchiq_exit();206207if (sc->intr_hl)208bus_teardown_intr(dev, sc->irq_res, sc->intr_hl);209bus_release_resource(dev, SYS_RES_IRQ, 0,210sc->irq_res);211bus_release_resource(dev, SYS_RES_MEMORY, 0,212sc->mem_res);213214mtx_destroy(&sc->lock);215216return (0);217}218219220static device_method_t bcm_vchiq_methods[] = {221DEVMETHOD(device_probe, bcm_vchiq_probe),222DEVMETHOD(device_attach, bcm_vchiq_attach),223DEVMETHOD(device_detach, bcm_vchiq_detach),224225/* Bus interface */226DEVMETHOD(bus_add_child, bus_generic_add_child),227228{ 0, 0 }229};230231static driver_t bcm_vchiq_driver = {232"vchiq",233bcm_vchiq_methods,234sizeof(struct bcm_vchiq_softc),235};236237DRIVER_MODULE(vchiq, simplebus, bcm_vchiq_driver, 0, 0);238MODULE_VERSION(vchiq, 1);239240241