mct.c revision 1.1 1 1.1 matt /* $NetBSD: mct.c,v 1.1 2014/04/13 02:26:26 matt Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.1 matt * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.1 matt * by Reinoud Zandijk.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt *
19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
30 1.1 matt */
31 1.1 matt
32 1.1 matt #include <sys/cdefs.h>
33 1.1 matt
34 1.1 matt __KERNEL_RCSID(1, "$NetBSD: mct.c,v 1.1 2014/04/13 02:26:26 matt Exp $");
35 1.1 matt
36 1.1 matt #include <sys/param.h>
37 1.1 matt #include <sys/bus.h>
38 1.1 matt #include <sys/device.h>
39 1.1 matt #include <sys/intr.h>
40 1.1 matt #include <sys/kernel.h>
41 1.1 matt #include <sys/proc.h>
42 1.1 matt #include <sys/systm.h>
43 1.1 matt #include <sys/timetc.h>
44 1.1 matt
45 1.1 matt #include <prop/proplib.h>
46 1.1 matt
47 1.1 matt #include <arm/samsung/exynos_reg.h>
48 1.1 matt #include <arm/samsung/exynos_var.h>
49 1.1 matt #include <arm/samsung/mct_reg.h>
50 1.1 matt #include <arm/samsung/mct_var.h>
51 1.1 matt
52 1.1 matt
53 1.1 matt static int mct_match(device_t, cfdata_t, void *);
54 1.1 matt static void mct_attach(device_t, device_t, void *);
55 1.1 matt
56 1.1 matt static int clockhandler(void *);
57 1.1 matt static u_int mct_get_timecount(struct timecounter *);
58 1.1 matt
59 1.1 matt
60 1.1 matt CFATTACH_DECL_NEW(exyo_mct, 0, mct_match, mct_attach, NULL, NULL);
61 1.1 matt
62 1.1 matt
63 1.1 matt static struct timecounter mct_timecounter = {
64 1.1 matt .tc_get_timecount = mct_get_timecount,
65 1.1 matt .tc_poll_pps = 0,
66 1.1 matt .tc_counter_mask = ~0u,
67 1.1 matt .tc_frequency = 0, /* set by cpu_initclocks() */
68 1.1 matt .tc_name = NULL, /* set by cpu_initclocks() */
69 1.1 matt .tc_quality = 500, /* why 500? */
70 1.1 matt .tc_priv = &mct_sc,
71 1.1 matt .tc_next = NULL,
72 1.1 matt };
73 1.1 matt
74 1.1 matt
75 1.1 matt static inline uint32_t
76 1.1 matt mct_read_global(struct mct_softc *sc, bus_size_t o)
77 1.1 matt {
78 1.1 matt return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
79 1.1 matt }
80 1.1 matt
81 1.1 matt
82 1.1 matt static inline void
83 1.1 matt mct_write_global(struct mct_softc *sc, bus_size_t o, uint32_t v)
84 1.1 matt {
85 1.1 matt bus_size_t wreg;
86 1.1 matt uint32_t bit;
87 1.1 matt int i;
88 1.1 matt
89 1.1 matt /* do the write */
90 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
91 1.1 matt // printf("%s: write %#x at %#x\n",
92 1.1 matt // __func__, ((uint32_t) sc->sc_bsh + (uint32_t) o), v);
93 1.1 matt
94 1.1 matt /* dependent on the write address, do the ack dance */
95 1.1 matt if (o == MCT_G_CNT_L || o == MCT_G_CNT_U) {
96 1.1 matt wreg = MCT_G_CNT_WSTAT;
97 1.1 matt bit = (o == MCT_G_CNT_L) ? G_CNT_WSTAT_L : G_CNT_WSTAT_U;
98 1.1 matt } else {
99 1.1 matt wreg = MCT_G_WSTAT;
100 1.1 matt switch (o) {
101 1.1 matt case MCT_G_COMP0_L:
102 1.1 matt bit = G_WSTAT_COMP0_L;
103 1.1 matt break;
104 1.1 matt case MCT_G_COMP0_U:
105 1.1 matt bit = G_WSTAT_COMP0_U;
106 1.1 matt break;
107 1.1 matt case MCT_G_COMP0_ADD_INCR:
108 1.1 matt bit = G_WSTAT_ADD_INCR;
109 1.1 matt break;
110 1.1 matt case MCT_G_TCON:
111 1.1 matt bit = G_WSTAT_TCON;
112 1.1 matt break;
113 1.1 matt default:
114 1.1 matt /* all other registers */
115 1.1 matt return;
116 1.1 matt }
117 1.1 matt }
118 1.1 matt
119 1.1 matt /* wait for ack */
120 1.1 matt for (i = 0; i < 10000000; i++) {
121 1.1 matt /* value accepted by the hardware/hal ? */
122 1.1 matt if (mct_read_global(sc, wreg) & bit) {
123 1.1 matt /* ack */
124 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh, wreg, bit);
125 1.1 matt return;
126 1.1 matt }
127 1.1 matt }
128 1.1 matt panic("MCT hangs after writing %#x at %#x", v, (uint32_t) o);
129 1.1 matt }
130 1.1 matt
131 1.1 matt
132 1.1 matt static int
133 1.1 matt mct_match(device_t parent, cfdata_t cf, void *aux)
134 1.1 matt {
135 1.1 matt /* not used if Generic Timer is Available */
136 1.1 matt if (armreg_pfr1_read() & ARM_PFR1_GTIMER_MASK)
137 1.1 matt return 0;
138 1.1 matt
139 1.1 matt /* sanity check, something is mixed up! */
140 1.1 matt if (!device_is_a(parent, "exyo"))
141 1.1 matt return 1;
142 1.1 matt
143 1.1 matt /* there can only be one */
144 1.1 matt if (mct_sc.sc_dev != NULL)
145 1.1 matt return 0;
146 1.1 matt
147 1.1 matt return 1;
148 1.1 matt }
149 1.1 matt
150 1.1 matt
151 1.1 matt static void
152 1.1 matt mct_attach(device_t parent, device_t self, void *aux)
153 1.1 matt {
154 1.1 matt struct exyo_attach_args *exyo = (struct exyo_attach_args *) aux;
155 1.1 matt struct mct_softc * const sc = &mct_sc;
156 1.1 matt prop_dictionary_t dict = device_properties(self);
157 1.1 matt char freqbuf[sizeof("XXX SHz")];
158 1.1 matt
159 1.1 matt self->dv_private = sc;
160 1.1 matt sc->sc_dev = self;
161 1.1 matt sc->sc_bst = exyo->exyo_core_bst;
162 1.1 matt sc->sc_irq = exyo->exyo_loc.loc_intr;
163 1.1 matt
164 1.1 matt bus_space_subregion(sc->sc_bst, exyo->exyo_core_bsh,
165 1.1 matt exyo->exyo_loc.loc_offset, exyo->exyo_loc.loc_size, &sc->sc_bsh);
166 1.1 matt
167 1.1 matt KASSERTMSG(sc->sc_bsh,
168 1.1 matt "%s: can't map in registers for %#x + %#x for device %s\n",
169 1.1 matt __func__,
170 1.1 matt (uint32_t) exyo->exyo_loc.loc_offset,
171 1.1 matt (uint32_t) exyo->exyo_loc.loc_size,
172 1.1 matt device_xname(sc->sc_dev));
173 1.1 matt
174 1.1 matt prop_dictionary_get_uint32(dict, "frequency", &sc->sc_freq);
175 1.1 matt
176 1.1 matt humanize_number(freqbuf, sizeof(freqbuf), sc->sc_freq, "Hz", 1000);
177 1.1 matt
178 1.1 matt aprint_naive("\n");
179 1.1 matt aprint_normal(": Exynos SoC multi core timer (64 bits) (%s)\n", freqbuf);
180 1.1 matt
181 1.1 matt evcnt_attach_dynamic(&sc->sc_ev_missing_ticks, EVCNT_TYPE_MISC, NULL,
182 1.1 matt device_xname(self), "missing interrupts");
183 1.1 matt
184 1.1 matt sc->sc_global_ih = intr_establish(sc->sc_irq, IPL_CLOCK, IST_EDGE,
185 1.1 matt clockhandler, NULL);
186 1.1 matt if (sc->sc_global_ih == NULL)
187 1.1 matt panic("%s: unable to register timer interrupt", __func__);
188 1.1 matt aprint_normal_dev(sc->sc_dev, "interrupting on irq %d\n", sc->sc_irq);
189 1.1 matt }
190 1.1 matt
191 1.1 matt
192 1.1 matt static inline uint64_t
193 1.1 matt mct_gettime(struct mct_softc *sc)
194 1.1 matt {
195 1.1 matt uint32_t lo, hi;
196 1.1 matt do {
197 1.1 matt hi = mct_read_global(sc, MCT_G_CNT_U);
198 1.1 matt lo = mct_read_global(sc, MCT_G_CNT_L);
199 1.1 matt } while (hi != mct_read_global(sc, MCT_G_CNT_U));
200 1.1 matt return ((uint64_t) hi << 32) | lo;
201 1.1 matt }
202 1.1 matt
203 1.1 matt
204 1.1 matt static u_int
205 1.1 matt mct_get_timecount(struct timecounter *tc)
206 1.1 matt {
207 1.1 matt struct mct_softc * const sc = tc->tc_priv;
208 1.1 matt return (u_int) (mct_gettime(sc));
209 1.1 matt }
210 1.1 matt
211 1.1 matt
212 1.1 matt /* interrupt handler */
213 1.1 matt static int
214 1.1 matt clockhandler(void *arg)
215 1.1 matt {
216 1.1 matt struct clockframe * const cf = arg;
217 1.1 matt struct mct_softc * const sc = &mct_sc;
218 1.1 matt const uint64_t now = mct_gettime(sc);
219 1.1 matt uint64_t delta = now - sc->sc_lastintr;
220 1.1 matt
221 1.1 matt /* ack the interrupt */
222 1.1 matt mct_write_global(sc, MCT_G_INT_CSTAT, G_INT_CSTAT_CLEAR);
223 1.1 matt
224 1.1 matt /* check if we missed clock interrupts */
225 1.1 matt if (delta > sc->sc_autoinc)
226 1.1 matt sc->sc_ev_missing_ticks.ev_count += delta / sc->sc_autoinc;
227 1.1 matt
228 1.1 matt sc->sc_lastintr = now;
229 1.1 matt hardclock(cf);
230 1.1 matt
231 1.1 matt /* handled */
232 1.1 matt return 1;
233 1.1 matt }
234 1.1 matt
235 1.1 matt
236 1.1 matt void
237 1.1 matt mct_init_cpu_clock(struct cpu_info *ci)
238 1.1 matt {
239 1.1 matt struct mct_softc * const sc = &mct_sc;
240 1.1 matt uint64_t now = mct_gettime(sc);
241 1.1 matt uint64_t then;
242 1.1 matt uint32_t tcon;
243 1.1 matt
244 1.1 matt KASSERT(ci == curcpu());
245 1.1 matt
246 1.1 matt sc->sc_lastintr = now;
247 1.1 matt
248 1.1 matt /* get current config */
249 1.1 matt tcon = mct_read_global(sc, MCT_G_TCON);
250 1.1 matt
251 1.1 matt /* setup auto increment */
252 1.1 matt mct_write_global(sc, MCT_G_COMP0_ADD_INCR, sc->sc_autoinc);
253 1.1 matt
254 1.1 matt /* (re)setup comparator */
255 1.1 matt then = now + sc->sc_autoinc;
256 1.1 matt mct_write_global(sc, MCT_G_COMP0_L, (uint32_t) then);
257 1.1 matt mct_write_global(sc, MCT_G_COMP0_U, (uint32_t) (then >> 32));
258 1.1 matt tcon |= G_TCON_COMP0_AUTOINC;
259 1.1 matt tcon |= G_TCON_COMP0_ENABLE;
260 1.1 matt
261 1.1 matt /* start timer */
262 1.1 matt tcon |= G_TCON_START;
263 1.1 matt
264 1.1 matt /* enable interrupt */
265 1.1 matt mct_write_global(sc, MCT_G_INT_ENB, G_INT_ENB_ENABLE);
266 1.1 matt
267 1.1 matt /* update config, starting the thing */
268 1.1 matt mct_write_global(sc, MCT_G_TCON, tcon);
269 1.1 matt }
270 1.1 matt
271 1.1 matt
272 1.1 matt void
273 1.1 matt cpu_initclocks(void)
274 1.1 matt {
275 1.1 matt struct mct_softc * const sc = &mct_sc;
276 1.1 matt
277 1.1 matt sc->sc_autoinc = sc->sc_freq / hz;
278 1.1 matt mct_init_cpu_clock(curcpu());
279 1.1 matt
280 1.1 matt mct_timecounter.tc_name = device_xname(sc->sc_dev);
281 1.1 matt mct_timecounter.tc_frequency = sc->sc_freq;
282 1.1 matt
283 1.1 matt tc_init(&mct_timecounter);
284 1.1 matt
285 1.1 matt #if 0
286 1.1 matt {
287 1.1 matt uint64_t then, now;
288 1.1 matt
289 1.1 matt printf("testing timer\n");
290 1.1 matt for (int i = 0; i < 200; i++) {
291 1.1 matt printf("cstat %d\n", mct_read_global(sc, MCT_G_INT_CSTAT));
292 1.1 matt then = mct_get_timecount(&mct_timecounter);
293 1.1 matt do {
294 1.1 matt now = mct_get_timecount(&mct_timecounter);
295 1.1 matt } while (now == then);
296 1.1 matt printf("\tgot %"PRIu64"\n", now);
297 1.1 matt for (int j = 0; j < 90000; j++);
298 1.1 matt }
299 1.1 matt printf("passed\n");
300 1.1 matt }
301 1.1 matt #endif
302 1.1 matt }
303 1.1 matt
304 1.1 matt
305 1.1 matt void
306 1.1 matt setstatclockrate(int newhz)
307 1.1 matt {
308 1.1 matt }
309 1.1 matt
310