counter.c revision 223961
1/*- 2 * Copyright (c) 2002 by Thomas Moestl <tmm@FreeBSD.org>. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 23 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include <sys/cdefs.h> 27__FBSDID("$FreeBSD: head/sys/sparc64/sparc64/counter.c 223961 2011-07-12 18:02:37Z marius $"); 28 29#include <sys/param.h> 30#include <sys/bus.h> 31#include <sys/malloc.h> 32#include <sys/systm.h> 33#include <sys/time.h> 34#include <sys/timetc.h> 35 36#include <machine/bus.h> 37#include <machine/bus_common.h> 38 39#define COUNTER_MASK ((1 << 29) - 1) 40#define COUNTER_FREQ 1000000 41#define COUNTER_QUALITY 100 42 43/* Bits in the limit register. */ 44#define CTLR_INTEN (1U << 31) /* Enable timer interrupts */ 45#define CTLR_RELOAD (1U << 30) /* Zero counter on write to limit reg */ 46#define CTLR_PERIODIC (1U << 29) /* Wrap to 0 if limit is reached */ 47 48/* Offsets of the registers for the two counters. */ 49#define CTR_CT0 0x00 50#define CTR_CT1 0x10 51 52/* Register offsets from the base address. */ 53#define CTR_COUNT 0x00 54#define CTR_LIMIT 0x08 55 56 57static timecounter_get_t counter_get_timecount; 58 59struct ct_softc { 60 bus_space_tag_t sc_tag; 61 bus_space_handle_t sc_handle; 62 bus_addr_t sc_offset; 63}; 64 65 66/* 67 * This is called from the psycho and sbus drivers. It does not directly 68 * attach to the nexus because it shares register space with the bridge in 69 * question. 70 */ 71void 72sparc64_counter_init(const char *name, bus_space_tag_t tag, 73 bus_space_handle_t handle, bus_addr_t offset) 74{ 75 struct timecounter *tc; 76 struct ct_softc *sc; 77 78 printf("initializing counter-timer\n"); 79 /* 80 * Turn off interrupts from both counters. Set the limit to the 81 * maximum value (although that should not change anything with 82 * CTLR_INTEN and CTLR_PERIODIC off). 83 */ 84 bus_space_write_8(tag, handle, offset + CTR_CT0 + CTR_LIMIT, 85 COUNTER_MASK); 86 bus_space_write_8(tag, handle, offset + CTR_CT1 + CTR_LIMIT, 87 COUNTER_MASK); 88 /* Register as a time counter. */ 89 tc = malloc(sizeof(*tc), M_DEVBUF, M_WAITOK | M_ZERO); 90 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK); 91 sc->sc_tag = tag; 92 sc->sc_handle = handle; 93 sc->sc_offset = offset + CTR_CT0; 94 tc->tc_get_timecount = counter_get_timecount; 95 tc->tc_counter_mask = COUNTER_MASK; 96 tc->tc_frequency = COUNTER_FREQ; 97 tc->tc_name = strdup(name, M_DEVBUF); 98 tc->tc_priv = sc; 99 tc->tc_quality = COUNTER_QUALITY; 100 tc_init(tc); 101} 102 103static unsigned int 104counter_get_timecount(struct timecounter *tc) 105{ 106 struct ct_softc *sc; 107 108 sc = tc->tc_priv; 109 return (bus_space_read_8(sc->sc_tag, sc->sc_handle, sc->sc_offset)); 110} 111