/* $NetBSD$ */ /* * TI Dual-mode timers */ /*- * Copyright (c) 2012, 2017 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Taylor R. Campbell. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include __KERNEL_RCSID(0, "$NetBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include "ioconf.h" typedef uint8_t dmt_reg_t; typedef uint16_t dmt_timer_reg_t; static int ti_dmtimer_match(device_t, cfdata_t, void *); static void ti_dmtimer_attach(device_t, device_t, void *); static int ti_dmtimer_init(void); static void ti_dmtimer_initclocks(void); static unsigned int dmt_tc_get_timecount(struct timecounter *); static int dmt_hardintr(void *); static int dmt_statintr(void *); static void dmt_start_periodic_intr(struct ti_dmtimer_softc *, int, unsigned int, int (*)(void *)); static void dmt_set_periodic_intr_frequency(struct ti_dmtimer_softc *, unsigned int); static void dmt_start_timecounter(struct ti_dmtimer_softc *); static unsigned int dmt_get_timecount(struct ti_dmtimer_softc *); static void dmt_start(struct ti_dmtimer_softc *, unsigned int); static void dmt_reset(struct ti_dmtimer_softc *); static void dmt_enable(struct ti_dmtimer_softc *); static void dmt_intr_enable(struct ti_dmtimer_softc *, uint32_t); static void dmt_intr_ack(struct ti_dmtimer_softc *, uint32_t); static dmt_reg_t dmt_timer_base(struct ti_dmtimer_softc *); static uint32_t dmt_timer_read_4(struct ti_dmtimer_softc *, dmt_timer_reg_t); static void dmt_timer_write_4(struct ti_dmtimer_softc *, dmt_timer_reg_t, uint32_t); static void dmt_timer_write_post_wait(struct ti_dmtimer_softc *, dmt_timer_reg_t); static uint32_t dmt_read_4(struct ti_dmtimer_softc *, dmt_reg_t); static void dmt_write_4(struct ti_dmtimer_softc *, dmt_reg_t, uint32_t); CFATTACH_DECL_NEW(ti_dmtimer, sizeof(struct ti_dmtimer_softc), ti_dmtimer_match, ti_dmtimer_attach, NULL, NULL); static const char *const compatible[] = { /* XXX possibly also omap3430-timer but not sure */ "ti,am335x-timer", "ti,am335x-timer-1ms", NULL, }; static const char *const compatible_v1[] = { /* XXX possibly also omap3430-timer but not sure */ "ti,am335x-timer1ms", NULL, }; static const char *const compatible_v2[] = { "ti,am335x-timer", NULL, }; static const struct timecounter dmt_timecounter_template = { .tc_get_timecount = dmt_tc_get_timecount, .tc_counter_mask = 0xffffffff, /* XXXMAGIC Make sense? */ #ifdef TI_SYSTEM_CLOCK_FREQ .tc_frequency = TI_SYSTEM_CLOCK_FREQ, /* XXXPOWER */ #endif .tc_name = "ti_dmtimerN", .tc_quality = 100, /* XXXMAGIC? */ .tc_priv = NULL, }; static ONCE_DECL(ti_dmtimer_init_once); static struct ti_dmtimer_softc *hardclock_sc = NULL; static struct ti_dmtimer_softc *statclock_sc = NULL; static int ti_dmtimer_match(device_t parent, cfdata_t match, void *aux) { struct fdt_attach_args *faa = aux; return of_match_compatible(faa->faa_phandle, compatible); } static void ti_dmtimer_attach(device_t parent, device_t self, void *aux) { struct ti_dmtimer_softc *sc = device_private(self); struct fdt_attach_args *faa = aux; bus_addr_t addr; bus_size_t size; if (RUN_ONCE(&ti_dmtimer_init_once, ti_dmtimer_init)) panic("ti dmtimer setup failed"); sc->sc_dev = self; sc->sc_phandle = faa->faa_phandle; sc->sc_iot = faa->faa_bst; if (of_match_compatible(faa->faa_phandle, compatible_v1) != 0) sc->sc_version = 1; else if (of_match_compatible(faa->faa_phandle, compatible_v2) != 0) sc->sc_version = 2; else panic("%s: incompatible?", device_xname(self)); if (fdtbus_get_reg(sc->sc_phandle, 0, &addr, &size) != 0) { aprint_error(": couldn't get registers\n"); return; } if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) panic("%s: unable to map bus space", device_xname(self)); sc->sc_tc = dmt_timecounter_template; sc->sc_tc.tc_name = device_xname(self); #ifndef TI_SYSTEM_CLOCK_FREQ sc->sc_tc.tc_frequency = ti_sys_clk; #endif if (sc->sc_version == 2) { if (hardclock_sc == NULL) hardclock_sc = sc; else if (statclock_sc == NULL) statclock_sc = sc; } } static int ti_dmtimer_init(void) { arm_fdt_timer_register(ti_dmtimer_initclocks); return 0; } static void ti_dmtimer_initclocks(void) { struct ti_dmtimer_softc *sc; unsigned int i = 0; if (hardclock_sc == NULL) panic("ti dmtimer hardclock not initialized"); dmt_enable(hardclock_sc); dmt_start_periodic_intr(hardclock_sc, IPL_CLOCK, hz, &dmt_hardintr); if (statclock_sc == NULL) panic("ti dmtimer statclock not initialized"); dmt_enable(statclock_sc); dmt_start_periodic_intr(statclock_sc, IPL_HIGH, stathz, &dmt_statintr); while ((sc = device_lookup_private(&ti_dmtimer_cd, i++)) != NULL) { if (sc == hardclock_sc) continue; if (sc == statclock_sc) continue; dmt_enable(sc); dmt_start_timecounter(sc); tc_init(&sc->sc_tc); } } #if 0 /* XXX setstatclockrate */ void setstatclockrate(int rate) { struct ti_dmtimer_softc *sc = statclock_sc; if (rate < 0) panic("I can't run the statistics clock backward!"); if (sc == NULL) panic("There is no statclock timer!\n"); dmt_set_periodic_intr_frequency(sc, rate); } #endif static unsigned int dmt_tc_get_timecount(struct timecounter *tc) { struct ti_dmtimer_softc *sc = container_of(tc, struct ti_dmtimer_softc, sc_tc); return dmt_get_timecount(sc); } static int dmt_hardintr(void *frame) { struct ti_dmtimer_softc *sc = hardclock_sc; KASSERT(sc != NULL); dmt_intr_ack(sc, TI_DMTIMER_INTR_ALL); hardclock(frame); return 1; } static int dmt_statintr(void *frame) { struct ti_dmtimer_softc *sc = statclock_sc; KASSERT(sc != NULL); dmt_intr_ack(sc, TI_DMTIMER_INTR_ALL); statclock(frame); return 1; } static void dmt_start_periodic_intr(struct ti_dmtimer_softc *sc, int ipl, unsigned int frequency, int (*func)(void *)) { char intrstr[128]; dmt_reset(sc); dmt_start(sc, frequency); if (!fdtbus_intr_str(sc->sc_phandle, 0, intrstr, sizeof intrstr)) panic("%s: failed to decode interrupt", device_xname(sc->sc_dev)); /* * Null argument means func gets the interrupt frame. For * whatever reason it's not an option to pass an argument (such * as sc) and the interrupt frame both, which is why we have * the global hardclock_sc and statclock_sc. */ if (fdtbus_intr_establish(sc->sc_phandle, 0, ipl, FDT_INTR_MPSAFE, func, NULL) == NULL) panic("%s: failed to establish timer interrupt: %s", device_xname(sc->sc_dev), intrstr); dmt_intr_enable(sc, TI_DMTIMER_INTR_OVERFLOW); } static void dmt_set_periodic_intr_frequency(struct ti_dmtimer_softc *sc, unsigned int frequency) { dmt_reset(sc); dmt_start(sc, frequency); dmt_intr_enable(sc, TI_DMTIMER_INTR_OVERFLOW); } static void dmt_start_timecounter(struct ti_dmtimer_softc *sc) { /* * XXXPOWER On reset, the timer uses the system clock. For * low-power operation, we can configure timers to use less * frequent clocks, but there's currently no abstraction for * doing this. */ dmt_reset(sc); dmt_timer_write_4(sc, TI_DMTIMER_TIMER_LOAD, 0); dmt_timer_write_4(sc, TI_DMTIMER_TIMER_COUNTER, 0); dmt_timer_write_4(sc, TI_DMTIMER_TIMER_CTRL, (TI_DMTIMER_TIMER_CTRL_START | TI_DMTIMER_TIMER_CTRL_AUTORELOAD)); } static unsigned int dmt_get_timecount(struct ti_dmtimer_softc *sc) { return dmt_timer_read_4(sc, TI_DMTIMER_TIMER_COUNTER); } static void dmt_start(struct ti_dmtimer_softc *sc, unsigned int frequency) { uint32_t value; /* * XXXPOWER Should do something clever with prescaling and * clock selection to save power. */ /* * XXX The dmtimer doesn't even necessarily run at the system * clock frequency by default. On the AM335x, the system clock * frequency is 24 MHz, but dmtimer0 runs at 32 kHz. */ value = (0xffffffff - ((ti_sys_clk / frequency) - 1)); dmt_timer_write_4(sc, TI_DMTIMER_TIMER_LOAD, value); dmt_timer_write_4(sc, TI_DMTIMER_TIMER_COUNTER, value); dmt_timer_write_4(sc, TI_DMTIMER_TIMER_CTRL, (TI_DMTIMER_TIMER_CTRL_START | TI_DMTIMER_TIMER_CTRL_AUTORELOAD)); } static void dmt_reset(struct ti_dmtimer_softc *sc) { uint32_t reset_mask; unsigned int tries = 1000; /* XXXMAGIC */ if (sc->sc_version == 1) reset_mask = TI_DMTIMER_V1_OCP_CFG_SOFTRESET_MASK; else reset_mask = TI_DMTIMER_V2_OCP_CFG_SOFTRESET_MASK; dmt_write_4(sc, TI_DMTIMER_OCP_CFG, reset_mask); while (dmt_read_4(sc, TI_DMTIMER_OCP_CFG) & reset_mask) { if (--tries == 0) panic("unable to reset dmtimer %p", sc); DELAY(10); /* XXXMAGIC */ } /* * Posted mode is enabled on reset on the OMAP35x but disabled * on reset on the AM335x, so handle both cases. * * XXXPOWER Does enabling this reduce power consumption? */ sc->sc_posted = (0 != (dmt_read_4(sc, TI_DMTIMER_TIMER_SYNC_INT_CTRL) & TI_DMTIMER_TIMER_SYNC_INT_CTRL_POSTED)); } static void dmt_enable(struct ti_dmtimer_softc *sc) { if (!sc->sc_enabled) { ti_prcm_module_enable(sc->sc_phandle); sc->sc_enabled = 1; } } static void dmt_intr_enable(struct ti_dmtimer_softc *sc, uint32_t intr) { if (sc->sc_version == 1) { dmt_write_4(sc, TI_DMTIMER_V1_INTR_ENABLE, intr); } else { dmt_write_4(sc, TI_DMTIMER_V2_INTR_ENABLE_CLEAR, (TI_DMTIMER_INTR_ALL & ~intr)); dmt_write_4(sc, TI_DMTIMER_V2_INTR_ENABLE_SET, intr); } } static void dmt_intr_ack(struct ti_dmtimer_softc *sc, uint32_t intr) { if (sc->sc_version == 1) dmt_write_4(sc, TI_DMTIMER_V1_INTR_STATUS, intr); else dmt_write_4(sc, TI_DMTIMER_V2_INTR_STATUS, intr); } static dmt_reg_t dmt_timer_base(struct ti_dmtimer_softc *sc) { if (sc->sc_version == 1) return TI_DMTIMER_V1_TIMER_REGS; else return TI_DMTIMER_V2_TIMER_REGS; } static uint32_t dmt_timer_read_4(struct ti_dmtimer_softc *sc, dmt_timer_reg_t reg) { dmt_timer_write_post_wait(sc, reg); return dmt_read_4(sc, dmt_timer_base(sc) + reg); } static void dmt_timer_write_4(struct ti_dmtimer_softc *sc, dmt_timer_reg_t reg, uint32_t value) { dmt_timer_write_post_wait(sc, reg); dmt_write_4(sc, dmt_timer_base(sc) + reg, value); } static void dmt_timer_write_post_wait(struct ti_dmtimer_softc *sc, dmt_timer_reg_t reg) { /* * Make sure we can read the TWPS (TI_DMTIMER_TIMER_WRITE_POST) * register with vanilla dmt_read_4. */ CTASSERT(TI_DMTIMER_TIMER_WRITE_POST == TI_DMTIMER_REG_POSTED_INDEX(TI_DMTIMER_TIMER_WRITE_POST)); if (sc->sc_posted && TI_DMTIMER_REG_POSTED_P(reg)) { unsigned int tries = 1000; /* XXXMAGIC */ const dmt_reg_t write_post_reg = dmt_timer_base(sc) + TI_DMTIMER_TIMER_WRITE_POST; while (dmt_read_4(sc, write_post_reg) & TI_DMTIMER_REG_POSTED_MASK(reg)) { if (--tries == 0) panic("dmtimer %p failed to complete write", sc); DELAY(10); } } } static uint32_t dmt_read_4(struct ti_dmtimer_softc *sc, dmt_reg_t reg) { return bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg); } static void dmt_write_4(struct ti_dmtimer_softc *sc, dmt_reg_t reg, uint32_t value) { bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, value); }