Path: blob/main/sys/arm/ti/am335x/am335x_dmtimer.c
39507 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2012 Damjan Marion <[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#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 <machine/bus.h>3839#include <machine/machdep.h> /* For arm_set_delay */4041#include <dev/clk/clk.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#include "am335x_dmtreg.h"5051struct am335x_dmtimer_softc {52device_t dev;53int tmr_mem_rid;54struct resource * tmr_mem_res;55int tmr_irq_rid;56struct resource * tmr_irq_res;57void *tmr_irq_handler;58clk_t clk_fck;59uint64_t sysclk_freq;60uint32_t tclr; /* Cached TCLR register. */61union {62struct timecounter tc;63struct eventtimer et;64} func;65int tmr_num; /* Hardware unit number. */66char tmr_name[12]; /* "DMTimerN", N = tmr_num */67};6869static struct am335x_dmtimer_softc *am335x_dmtimer_et_sc = NULL;70static struct am335x_dmtimer_softc *am335x_dmtimer_tc_sc = NULL;7172static void am335x_dmtimer_delay(int, void *);7374/*75* We use dmtimer2 for eventtimer and dmtimer3 for timecounter.76*/77#define ET_TMR_NUM 278#define TC_TMR_NUM 37980/* List of compatible strings for FDT tree */81static struct ofw_compat_data compat_data[] = {82{"ti,am335x-timer", 1},83{"ti,am335x-timer-1ms", 1},84{NULL, 0},85};8687#define DMTIMER_READ4(sc, reg) bus_read_4((sc)->tmr_mem_res, (reg))88#define DMTIMER_WRITE4(sc, reg, val) bus_write_4((sc)->tmr_mem_res, (reg), (val))8990static int91am335x_dmtimer_et_start(struct eventtimer *et, sbintime_t first, sbintime_t period)92{93struct am335x_dmtimer_softc *sc;94uint32_t initial_count, reload_count;9596sc = et->et_priv;9798/*99* Stop the timer before changing it. This routine will often be called100* while the timer is still running, to either lengthen or shorten the101* current event time. We need to ensure the timer doesn't expire while102* we're working with it.103*104* Also clear any pending interrupt status, because it's at least105* theoretically possible that we're running in a primary interrupt106* context now, and a timer interrupt could be pending even before we107* stopped the timer. The more likely case is that we're being called108* from the et_event_cb() routine dispatched from our own handler, but109* it's not clear to me that that's the only case possible.110*/111sc->tclr &= ~(DMT_TCLR_START | DMT_TCLR_AUTOLOAD);112DMTIMER_WRITE4(sc, DMT_TCLR, sc->tclr);113DMTIMER_WRITE4(sc, DMT_IRQSTATUS, DMT_IRQ_OVF);114115if (period != 0) {116reload_count = ((uint32_t)et->et_frequency * period) >> 32;117sc->tclr |= DMT_TCLR_AUTOLOAD;118} else {119reload_count = 0;120}121122if (first != 0)123initial_count = ((uint32_t)et->et_frequency * first) >> 32;124else125initial_count = reload_count;126127/*128* Set auto-reload and current-count values. This timer hardware counts129* up from the initial/reload value and interrupts on the zero rollover.130*/131DMTIMER_WRITE4(sc, DMT_TLDR, 0xFFFFFFFF - reload_count);132DMTIMER_WRITE4(sc, DMT_TCRR, 0xFFFFFFFF - initial_count);133134/* Enable overflow interrupt, and start the timer. */135DMTIMER_WRITE4(sc, DMT_IRQENABLE_SET, DMT_IRQ_OVF);136sc->tclr |= DMT_TCLR_START;137DMTIMER_WRITE4(sc, DMT_TCLR, sc->tclr);138139return (0);140}141142static int143am335x_dmtimer_et_stop(struct eventtimer *et)144{145struct am335x_dmtimer_softc *sc;146147sc = et->et_priv;148149/* Stop timer, disable and clear interrupt. */150sc->tclr &= ~(DMT_TCLR_START | DMT_TCLR_AUTOLOAD);151DMTIMER_WRITE4(sc, DMT_TCLR, sc->tclr);152DMTIMER_WRITE4(sc, DMT_IRQENABLE_CLR, DMT_IRQ_OVF);153DMTIMER_WRITE4(sc, DMT_IRQSTATUS, DMT_IRQ_OVF);154return (0);155}156157static int158am335x_dmtimer_et_intr(void *arg)159{160struct am335x_dmtimer_softc *sc;161162sc = arg;163164/* Ack the interrupt, and invoke the callback if it's still enabled. */165DMTIMER_WRITE4(sc, DMT_IRQSTATUS, DMT_IRQ_OVF);166if (sc->func.et.et_active)167sc->func.et.et_event_cb(&sc->func.et, sc->func.et.et_arg);168169return (FILTER_HANDLED);170}171172static int173am335x_dmtimer_et_init(struct am335x_dmtimer_softc *sc)174{175KASSERT(am335x_dmtimer_et_sc == NULL, ("already have an eventtimer"));176177/*178* Setup eventtimer interrupt handling. Panic if anything goes wrong,179* because the system just isn't going to run without an eventtimer.180*/181sc->tmr_irq_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ,182&sc->tmr_irq_rid, RF_ACTIVE);183if (sc->tmr_irq_res == NULL)184panic("am335x_dmtimer: could not allocate irq resources");185if (bus_setup_intr(sc->dev, sc->tmr_irq_res, INTR_TYPE_CLK,186am335x_dmtimer_et_intr, NULL, sc, &sc->tmr_irq_handler) != 0)187panic("am335x_dmtimer: count not setup irq handler");188189sc->func.et.et_name = sc->tmr_name;190sc->func.et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT;191sc->func.et.et_quality = 500;192sc->func.et.et_frequency = sc->sysclk_freq;193sc->func.et.et_min_period =194((0x00000005LLU << 32) / sc->func.et.et_frequency);195sc->func.et.et_max_period =196(0xfffffffeLLU << 32) / sc->func.et.et_frequency;197sc->func.et.et_start = am335x_dmtimer_et_start;198sc->func.et.et_stop = am335x_dmtimer_et_stop;199sc->func.et.et_priv = sc;200201am335x_dmtimer_et_sc = sc;202et_register(&sc->func.et);203204return (0);205}206207static unsigned208am335x_dmtimer_tc_get_timecount(struct timecounter *tc)209{210struct am335x_dmtimer_softc *sc;211212sc = tc->tc_priv;213214return (DMTIMER_READ4(sc, DMT_TCRR));215}216217static int218am335x_dmtimer_tc_init(struct am335x_dmtimer_softc *sc)219{220KASSERT(am335x_dmtimer_tc_sc == NULL, ("already have a timecounter"));221222/* Set up timecounter, start it, register it. */223DMTIMER_WRITE4(sc, DMT_TSICR, DMT_TSICR_RESET);224while (DMTIMER_READ4(sc, DMT_TIOCP_CFG) & DMT_TIOCP_RESET)225continue;226227sc->tclr |= DMT_TCLR_START | DMT_TCLR_AUTOLOAD;228DMTIMER_WRITE4(sc, DMT_TLDR, 0);229DMTIMER_WRITE4(sc, DMT_TCRR, 0);230DMTIMER_WRITE4(sc, DMT_TCLR, sc->tclr);231232sc->func.tc.tc_name = sc->tmr_name;233sc->func.tc.tc_get_timecount = am335x_dmtimer_tc_get_timecount;234sc->func.tc.tc_counter_mask = ~0u;235sc->func.tc.tc_frequency = sc->sysclk_freq;236sc->func.tc.tc_quality = 500;237sc->func.tc.tc_priv = sc;238239am335x_dmtimer_tc_sc = sc;240tc_init(&sc->func.tc);241242arm_set_delay(am335x_dmtimer_delay, sc);243244return (0);245}246247static int248am335x_dmtimer_probe(device_t dev)249{250int tmr_num;251uint64_t rev_address;252253if (!ofw_bus_status_okay(dev))254return (ENXIO);255256if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)257return (ENXIO);258259/*260* Get the hardware unit number from address of rev register.261* If this isn't the hardware unit we're going to use for either the262* eventtimer or the timecounter, no point in instantiating the device.263*/264rev_address = ti_sysc_get_rev_address(device_get_parent(dev));265switch (rev_address) {266case DMTIMER2_REV:267tmr_num = 2;268break;269case DMTIMER3_REV:270tmr_num = 3;271break;272default:273/* Not DMTIMER2 or DMTIMER3 */274return (ENXIO);275}276277device_set_descf(dev, "AM335x DMTimer%d", tmr_num);278279return(BUS_PROBE_DEFAULT);280}281282static int283am335x_dmtimer_attach(device_t dev)284{285struct am335x_dmtimer_softc *sc;286int err;287uint64_t rev_address;288clk_t sys_clkin;289290sc = device_get_softc(dev);291sc->dev = dev;292293/* expect one clock */294err = clk_get_by_ofw_index(dev, 0, 0, &sc->clk_fck);295if (err != 0) {296device_printf(dev, "Cant find clock index 0. err: %d\n", err);297return (ENXIO);298}299300err = clk_get_by_name(dev, "sys_clkin_ck@40", &sys_clkin);301if (err != 0) {302device_printf(dev, "Cant find sys_clkin_ck@40 err: %d\n", err);303return (ENXIO);304}305306/* Select M_OSC as DPLL parent */307err = clk_set_parent_by_clk(sc->clk_fck, sys_clkin);308if (err != 0) {309device_printf(dev, "Cant set mux to CLK_M_OSC\n");310return (ENXIO);311}312313/* Enable clocks and power on the device. */314err = ti_sysc_clock_enable(device_get_parent(dev));315if (err != 0) {316device_printf(dev, "Cant enable sysc clkctrl, err %d\n", err);317return (ENXIO);318}319320/* Get the base clock frequency. */321err = clk_get_freq(sc->clk_fck, &sc->sysclk_freq);322if (err != 0) {323device_printf(dev, "Cant get sysclk frequency, err %d\n", err);324return (ENXIO);325}326327/* Request the memory resources. */328sc->tmr_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,329&sc->tmr_mem_rid, RF_ACTIVE);330if (sc->tmr_mem_res == NULL) {331return (ENXIO);332}333334rev_address = ti_sysc_get_rev_address(device_get_parent(dev));335switch (rev_address) {336case DMTIMER2_REV:337sc->tmr_num = 2;338break;339case DMTIMER3_REV:340sc->tmr_num = 3;341break;342default:343device_printf(dev, "Not timer 2 or 3! %#jx\n",344rev_address);345return (ENXIO);346}347348snprintf(sc->tmr_name, sizeof(sc->tmr_name), "DMTimer%d", sc->tmr_num);349350/*351* Go set up either a timecounter or eventtimer. We wouldn't have352* attached if we weren't one or the other.353*/354if (sc->tmr_num == ET_TMR_NUM)355am335x_dmtimer_et_init(sc);356else if (sc->tmr_num == TC_TMR_NUM)357am335x_dmtimer_tc_init(sc);358else359panic("am335x_dmtimer: bad timer number %d", sc->tmr_num);360361return (0);362}363364static device_method_t am335x_dmtimer_methods[] = {365DEVMETHOD(device_probe, am335x_dmtimer_probe),366DEVMETHOD(device_attach, am335x_dmtimer_attach),367{ 0, 0 }368};369370static driver_t am335x_dmtimer_driver = {371"am335x_dmtimer",372am335x_dmtimer_methods,373sizeof(struct am335x_dmtimer_softc),374};375376DRIVER_MODULE(am335x_dmtimer, simplebus, am335x_dmtimer_driver, 0, 0);377MODULE_DEPEND(am335x_dmtimer, ti_sysc, 1, 1, 1);378379static void380am335x_dmtimer_delay(int usec, void *arg)381{382struct am335x_dmtimer_softc *sc = arg;383int32_t counts;384uint32_t first, last;385386/* Get the number of times to count */387counts = (usec + 1) * (sc->sysclk_freq / 1000000);388389first = DMTIMER_READ4(sc, DMT_TCRR);390391while (counts > 0) {392last = DMTIMER_READ4(sc, DMT_TCRR);393if (last > first) {394counts -= (int32_t)(last - first);395} else {396counts -= (int32_t)((0xFFFFFFFF - first) + last);397}398first = last;399}400}401402403