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