Path: blob/main/sys/arm/freescale/vybrid/vf_port.c
39536 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2014 Ruslan Bukin <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728/*29* Vybrid Family Port control and interrupts (PORT)30* Chapter 6, Vybrid Reference Manual, Rev. 5, 07/201331*/3233#include <sys/param.h>34#include <sys/systm.h>35#include <sys/bus.h>36#include <sys/kernel.h>37#include <sys/module.h>38#include <sys/malloc.h>39#include <sys/rman.h>40#include <sys/timeet.h>41#include <sys/timetc.h>42#include <sys/watchdog.h>4344#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/cpu.h>50#include <machine/intr.h>5152#include <arm/freescale/vybrid/vf_port.h>53#include <arm/freescale/vybrid/vf_common.h>5455/* Pin Control Register */56#define PORT_PCR(n) (0x1000 * (n >> 5) + 0x4 * (n % 32))57#define PCR_IRQC_S 1658#define PCR_IRQC_M 0xF59#define PCR_DMA_RE 0x160#define PCR_DMA_FE 0x261#define PCR_DMA_EE 0x362#define PCR_INT_LZ 0x863#define PCR_INT_RE 0x964#define PCR_INT_FE 0xA65#define PCR_INT_EE 0xB66#define PCR_INT_LO 0xC67#define PCR_ISF (1 << 24)68#define PORT0_ISFR 0xA0 /* Interrupt Status Flag Register */69#define PORT0_DFER 0xC0 /* Digital Filter Enable Register */70#define PORT0_DFCR 0xC4 /* Digital Filter Clock Register */71#define PORT0_DFWR 0xC8 /* Digital Filter Width Register */7273struct port_event {74uint32_t enabled;75uint32_t mux_num;76uint32_t mux_src;77uint32_t mux_chn;78void (*ih) (void *);79void *ih_user;80enum ev_type pevt;81};8283static struct port_event event_map[NGPIO];8485struct port_softc {86struct resource *res[6];87bus_space_tag_t bst;88bus_space_handle_t bsh;89void *gpio_ih[NGPIO];90};9192struct port_softc *port_sc;9394static struct resource_spec port_spec[] = {95{ SYS_RES_MEMORY, 0, RF_ACTIVE },96{ SYS_RES_IRQ, 0, RF_ACTIVE },97{ SYS_RES_IRQ, 1, RF_ACTIVE },98{ SYS_RES_IRQ, 2, RF_ACTIVE },99{ SYS_RES_IRQ, 3, RF_ACTIVE },100{ SYS_RES_IRQ, 4, RF_ACTIVE },101{ -1, 0 }102};103104static int105port_intr(void *arg)106{107struct port_event *pev;108struct port_softc *sc;109int reg;110int i;111112sc = arg;113114for (i = 0; i < NGPIO; i++) {115reg = READ4(sc, PORT_PCR(i));116if (reg & PCR_ISF) {117/* Clear interrupt */118WRITE4(sc, PORT_PCR(i), reg);119120/* Handle event */121pev = &event_map[i];122if (pev->enabled == 1) {123if (pev->ih != NULL) {124pev->ih(pev->ih_user);125}126}127}128}129130return (FILTER_HANDLED);131}132133int134port_setup(int pnum, enum ev_type pevt, void (*ih)(void *), void *ih_user)135{136struct port_event *pev;137struct port_softc *sc;138int reg;139int val;140141sc = port_sc;142143switch (pevt) {144case DMA_RISING_EDGE:145val = PCR_DMA_RE;146break;147case DMA_FALLING_EDGE:148val = PCR_DMA_FE;149break;150case DMA_EITHER_EDGE:151val = PCR_DMA_EE;152break;153case INT_LOGIC_ZERO:154val = PCR_INT_LZ;155break;156case INT_RISING_EDGE:157val = PCR_INT_RE;158break;159case INT_FALLING_EDGE:160val = PCR_INT_FE;161break;162case INT_EITHER_EDGE:163val = PCR_INT_EE;164break;165case INT_LOGIC_ONE:166val = PCR_INT_LO;167break;168default:169return (-1);170}171172reg = READ4(sc, PORT_PCR(pnum));173reg &= ~(PCR_IRQC_M << PCR_IRQC_S);174reg |= (val << PCR_IRQC_S);175WRITE4(sc, PORT_PCR(pnum), reg);176177pev = &event_map[pnum];178pev->ih = ih;179pev->ih_user = ih_user;180pev->pevt = pevt;181pev->enabled = 1;182183return (0);184}185186static int187port_probe(device_t dev)188{189190if (!ofw_bus_status_okay(dev))191return (ENXIO);192193if (!ofw_bus_is_compatible(dev, "fsl,mvf600-port"))194return (ENXIO);195196device_set_desc(dev, "Vybrid Family Port control and interrupts");197return (BUS_PROBE_DEFAULT);198}199200static int201port_attach(device_t dev)202{203struct port_softc *sc;204int irq;205206sc = device_get_softc(dev);207208if (bus_alloc_resources(dev, port_spec, sc->res)) {209device_printf(dev, "could not allocate resources\n");210return (ENXIO);211}212213/* Memory interface */214sc->bst = rman_get_bustag(sc->res[0]);215sc->bsh = rman_get_bushandle(sc->res[0]);216217port_sc = sc;218219for (irq = 0; irq < NPORTS; irq ++) {220if ((bus_setup_intr(dev, sc->res[1 + irq], INTR_TYPE_MISC,221port_intr, NULL, sc, &sc->gpio_ih[irq]))) {222device_printf(dev,223"ERROR: Unable to register interrupt handler\n");224return (ENXIO);225}226}227228return (0);229}230231static device_method_t port_methods[] = {232DEVMETHOD(device_probe, port_probe),233DEVMETHOD(device_attach, port_attach),234{ 0, 0 }235};236237static driver_t port_driver = {238"port",239port_methods,240sizeof(struct port_softc),241};242243DRIVER_MODULE(port, simplebus, port_driver, 0, 0);244245246