bcm2835_systimer.c revision 243523
1/* 2 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org> 3 * Copyright (c) 2012 Damjan Marion <dmarion@freebsd.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: head/sys/arm/broadcom/bcm2835/bcm2835_systimer.c 243523 2012-11-25 16:19:12Z kientzle $"); 30 31#include <sys/param.h> 32#include <sys/systm.h> 33#include <sys/bus.h> 34#include <sys/kernel.h> 35#include <sys/module.h> 36#include <sys/malloc.h> 37#include <sys/rman.h> 38#include <sys/timeet.h> 39#include <sys/timetc.h> 40#include <sys/watchdog.h> 41#include <machine/bus.h> 42#include <machine/cpu.h> 43#include <machine/frame.h> 44#include <machine/intr.h> 45 46#include <dev/fdt/fdt_common.h> 47#include <dev/ofw/openfirm.h> 48#include <dev/ofw/ofw_bus.h> 49#include <dev/ofw/ofw_bus_subr.h> 50 51#include <machine/bus.h> 52#include <machine/fdt.h> 53 54#define BCM2835_NUM_TIMERS 4 55 56#define DEFAULT_TIMER 3 57#define DEFAULT_FREQUENCY 1000000 58 59#define SYSTIMER_CS 0x00 60#define SYSTIMER_CLO 0x04 61#define SYSTIMER_CHI 0x08 62#define SYSTIMER_C0 0x0C 63#define SYSTIMER_C1 0x10 64#define SYSTIMER_C2 0x14 65#define SYSTIMER_C3 0x18 66 67struct systimer { 68 int index; 69 bool enabled; 70 struct eventtimer et; 71}; 72 73struct bcm_systimer_softc { 74 struct resource* mem_res; 75 struct resource* irq_res[BCM2835_NUM_TIMERS]; 76 void* intr_hl[BCM2835_NUM_TIMERS]; 77 uint32_t sysclk_freq; 78 bus_space_tag_t bst; 79 bus_space_handle_t bsh; 80 struct systimer st[BCM2835_NUM_TIMERS]; 81}; 82 83static struct resource_spec bcm_systimer_irq_spec[] = { 84 { SYS_RES_IRQ, 0, RF_ACTIVE }, 85 { SYS_RES_IRQ, 1, RF_ACTIVE }, 86 { SYS_RES_IRQ, 2, RF_ACTIVE }, 87 { SYS_RES_IRQ, 3, RF_ACTIVE }, 88 { -1, 0, 0 } 89}; 90 91static struct bcm_systimer_softc *bcm_systimer_sc = NULL; 92 93/* Read/Write macros for Timer used as timecounter */ 94#define bcm_systimer_tc_read_4(reg) \ 95 bus_space_read_4(bcm_systimer_sc->bst, \ 96 bcm_systimer_sc->bsh, reg) 97 98#define bcm_systimer_tc_write_4(reg, val) \ 99 bus_space_write_4(bcm_systimer_sc->bst, \ 100 bcm_systimer_sc->bsh, reg, val) 101 102static unsigned bcm_systimer_tc_get_timecount(struct timecounter *); 103 104static struct timecounter bcm_systimer_tc = { 105 .tc_name = "BCM2835 Timecounter", 106 .tc_get_timecount = bcm_systimer_tc_get_timecount, 107 .tc_poll_pps = NULL, 108 .tc_counter_mask = ~0u, 109 .tc_frequency = 0, 110 .tc_quality = 1000, 111}; 112 113static unsigned 114bcm_systimer_tc_get_timecount(struct timecounter *tc) 115{ 116 return bcm_systimer_tc_read_4(SYSTIMER_CLO); 117} 118 119static int 120bcm_systimer_start(struct eventtimer *et, struct bintime *first, 121 struct bintime *period) 122{ 123 struct systimer *st = et->et_priv; 124 uint32_t clo; 125 uint32_t count; 126 127 if (first != NULL) { 128 st->enabled = 1; 129 130 count = (st->et.et_frequency * (first->frac >> 32)) >> 32; 131 if (first->sec != 0) 132 count += st->et.et_frequency * first->sec; 133 134 clo = bcm_systimer_tc_read_4(SYSTIMER_CLO); 135 clo += count; 136 bcm_systimer_tc_write_4(SYSTIMER_C0 + st->index*4, clo); 137 138 return (0); 139 } 140 141 return (EINVAL); 142} 143 144static int 145bcm_systimer_stop(struct eventtimer *et) 146{ 147 struct systimer *st = et->et_priv; 148 st->enabled = 0; 149 150 return (0); 151} 152 153static int 154bcm_systimer_intr(void *arg) 155{ 156 struct systimer *st = (struct systimer *)arg; 157 158 bcm_systimer_tc_write_4(SYSTIMER_CS, (1 << st->index)); 159 if (st->enabled) { 160 if (st->et.et_active) { 161 st->et.et_event_cb(&st->et, st->et.et_arg); 162 } 163 } 164 165 return (FILTER_HANDLED); 166} 167 168static int 169bcm_systimer_probe(device_t dev) 170{ 171 172 if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-system-timer")) { 173 device_set_desc(dev, "BCM2835 System Timer"); 174 return (BUS_PROBE_DEFAULT); 175 } 176 177 return (ENXIO); 178} 179 180static int 181bcm_systimer_attach(device_t dev) 182{ 183 struct bcm_systimer_softc *sc = device_get_softc(dev); 184 int err; 185 int rid = 0; 186 187 if (bcm_systimer_sc != NULL) 188 return (EINVAL); 189 190 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 191 if (sc->mem_res == NULL) { 192 device_printf(dev, "could not allocate memory resource\n"); 193 return (ENXIO); 194 } 195 196 sc->bst = rman_get_bustag(sc->mem_res); 197 sc->bsh = rman_get_bushandle(sc->mem_res); 198 199 /* Request the IRQ resources */ 200 err = bus_alloc_resources(dev, bcm_systimer_irq_spec, 201 sc->irq_res); 202 if (err) { 203 device_printf(dev, "Error: could not allocate irq resources\n"); 204 return (ENXIO); 205 } 206 207 /* TODO: get frequency from FDT */ 208 sc->sysclk_freq = DEFAULT_FREQUENCY; 209 210 /* Setup and enable the timer */ 211 if (bus_setup_intr(dev, sc->irq_res[DEFAULT_TIMER], INTR_TYPE_CLK, 212 bcm_systimer_intr, NULL, &sc->st[DEFAULT_TIMER], 213 &sc->intr_hl[DEFAULT_TIMER]) != 0) { 214 bus_release_resources(dev, bcm_systimer_irq_spec, 215 sc->irq_res); 216 device_printf(dev, "Unable to setup the clock irq handler.\n"); 217 return (ENXIO); 218 } 219 220 sc->st[DEFAULT_TIMER].index = DEFAULT_TIMER; 221 sc->st[DEFAULT_TIMER].enabled = 0; 222 sc->st[DEFAULT_TIMER].et.et_name = malloc(64, M_DEVBUF, M_NOWAIT | M_ZERO); 223 sprintf(sc->st[DEFAULT_TIMER].et.et_name, "BCM2835 Event Timer %d", DEFAULT_TIMER); 224 sc->st[DEFAULT_TIMER].et.et_flags = ET_FLAGS_ONESHOT; 225 sc->st[DEFAULT_TIMER].et.et_quality = 1000; 226 sc->st[DEFAULT_TIMER].et.et_frequency = sc->sysclk_freq; 227 sc->st[DEFAULT_TIMER].et.et_min_period.sec = 0; 228 sc->st[DEFAULT_TIMER].et.et_min_period.frac = 229 ((0x00000002LLU << 32) / sc->st[DEFAULT_TIMER].et.et_frequency) << 32; 230 sc->st[DEFAULT_TIMER].et.et_max_period.sec = 0xfffffff0U / sc->st[DEFAULT_TIMER].et.et_frequency; 231 sc->st[DEFAULT_TIMER].et.et_max_period.frac = 232 ((0xfffffffeLLU << 32) / sc->st[DEFAULT_TIMER].et.et_frequency) << 32; 233 sc->st[DEFAULT_TIMER].et.et_start = bcm_systimer_start; 234 sc->st[DEFAULT_TIMER].et.et_stop = bcm_systimer_stop; 235 sc->st[DEFAULT_TIMER].et.et_priv = &sc->st[DEFAULT_TIMER]; 236 et_register(&sc->st[DEFAULT_TIMER].et); 237 238 bcm_systimer_sc = sc; 239 240 bcm_systimer_tc.tc_frequency = DEFAULT_FREQUENCY; 241 tc_init(&bcm_systimer_tc); 242 243 return (0); 244} 245 246static device_method_t bcm_systimer_methods[] = { 247 DEVMETHOD(device_probe, bcm_systimer_probe), 248 DEVMETHOD(device_attach, bcm_systimer_attach), 249 { 0, 0 } 250}; 251 252static driver_t bcm_systimer_driver = { 253 "systimer", 254 bcm_systimer_methods, 255 sizeof(struct bcm_systimer_softc), 256}; 257 258static devclass_t bcm_systimer_devclass; 259 260DRIVER_MODULE(bcm_systimer, simplebus, bcm_systimer_driver, bcm_systimer_devclass, 0, 0); 261 262void 263cpu_initclocks(void) 264{ 265 cpu_initclocks_bsp(); 266} 267 268void 269DELAY(int usec) 270{ 271 int32_t counts; 272 uint32_t first, last; 273 274 if (bcm_systimer_sc == NULL) { 275 for (; usec > 0; usec--) 276 for (counts = 200; counts > 0; counts--) 277 /* Prevent gcc from optimizing out the loop */ 278 cpufunc_nullop(); 279 return; 280 } 281 282 /* Get the number of times to count */ 283 counts = usec * ((bcm_systimer_tc.tc_frequency / 1000000) + 1); 284 285 first = bcm_systimer_tc_read_4(SYSTIMER_CLO); 286 287 while (counts > 0) { 288 last = bcm_systimer_tc_read_4(SYSTIMER_CLO); 289 if (last == first) 290 continue; 291 if (last>first) { 292 counts -= (int32_t)(last - first); 293 } else { 294 counts -= (int32_t)((0xFFFFFFFF - first) + last); 295 } 296 first = last; 297 } 298} 299