2 * Copyright (c) 2002 by Thomas Moestl <tmm@FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/param.h>
30 #include <sys/malloc.h>
31 #include <sys/systm.h>
33 #include <sys/timetc.h>
35 #include <machine/bus.h>
36 #include <machine/bus_common.h>
38 #define COUNTER_MASK ((1 << 29) - 1)
39 #define COUNTER_FREQ 1000000
40 #define COUNTER_QUALITY 100
42 /* Bits in the limit register. */
43 #define CTLR_INTEN (1U << 31) /* Enable timer interrupts */
44 #define CTLR_RELOAD (1U << 30) /* Zero counter on write to limit reg */
45 #define CTLR_PERIODIC (1U << 29) /* Wrap to 0 if limit is reached */
47 /* Offsets of the registers for the two counters. */
51 /* Register offsets from the base address. */
52 #define CTR_COUNT 0x00
53 #define CTR_LIMIT 0x08
56 static unsigned int counter_get_timecount(struct timecounter *tc);
59 bus_space_tag_t sc_tag;
60 bus_space_handle_t sc_handle;
66 * This is called from the psycho and sbus drivers. It does not directly attach
67 * to the nexus because it shares register space with the bridge in question.
70 sparc64_counter_init(bus_space_tag_t tag, bus_space_handle_t handle,
73 struct timecounter *tc;
76 printf("initializing counter-timer\n");
78 * Turn off interrupts from both counters. Set the limit to the maximum
79 * value (although that should not change anything with CTLR_INTEN and
82 bus_space_write_8(tag, handle, offset + CTR_CT0 + CTR_LIMIT,
84 bus_space_write_8(tag, handle, offset + CTR_CT1 + CTR_LIMIT,
86 /* Register as a time counter. */
87 tc = malloc(sizeof(*tc), M_DEVBUF, M_WAITOK);
88 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK);
90 sc->sc_handle = handle;
91 sc->sc_offset = offset + CTR_CT0;
92 tc->tc_get_timecount = counter_get_timecount;
93 tc->tc_poll_pps = NULL;
94 tc->tc_counter_mask = COUNTER_MASK;
95 tc->tc_frequency = COUNTER_FREQ;
96 tc->tc_name = "counter-timer";
98 tc->tc_quality = COUNTER_QUALITY;
103 counter_get_timecount(struct timecounter *tc)
108 return (bus_space_read_8(sc->sc_tag, sc->sc_handle, sc->sc_offset) &