mct.c revision 1.7 1 1.7 marty /* $NetBSD: mct.c,v 1.7 2015/12/21 00:54:35 marty 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.7 marty __KERNEL_RCSID(1, "$NetBSD: mct.c,v 1.7 2015/12/21 00:54:35 marty 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.7 marty #include <dev/fdt/fdtvar.h>
53 1.1 matt
54 1.1 matt static int mct_match(device_t, cfdata_t, void *);
55 1.1 matt static void mct_attach(device_t, device_t, void *);
56 1.1 matt
57 1.1 matt static int clockhandler(void *);
58 1.1 matt
59 1.1 matt CFATTACH_DECL_NEW(exyo_mct, 0, mct_match, mct_attach, NULL, NULL);
60 1.1 matt
61 1.1 matt
62 1.6 marty #if 0
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.6 marty #endif
74 1.1 matt
75 1.1 matt
76 1.1 matt static inline uint32_t
77 1.1 matt mct_read_global(struct mct_softc *sc, bus_size_t o)
78 1.1 matt {
79 1.1 matt return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
80 1.1 matt }
81 1.1 matt
82 1.1 matt
83 1.1 matt static inline void
84 1.1 matt mct_write_global(struct mct_softc *sc, bus_size_t o, uint32_t v)
85 1.1 matt {
86 1.1 matt bus_size_t wreg;
87 1.1 matt uint32_t bit;
88 1.1 matt int i;
89 1.1 matt
90 1.1 matt /* do the write */
91 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
92 1.1 matt // printf("%s: write %#x at %#x\n",
93 1.1 matt // __func__, ((uint32_t) sc->sc_bsh + (uint32_t) o), v);
94 1.1 matt
95 1.1 matt /* dependent on the write address, do the ack dance */
96 1.1 matt if (o == MCT_G_CNT_L || o == MCT_G_CNT_U) {
97 1.1 matt wreg = MCT_G_CNT_WSTAT;
98 1.1 matt bit = (o == MCT_G_CNT_L) ? G_CNT_WSTAT_L : G_CNT_WSTAT_U;
99 1.1 matt } else {
100 1.1 matt wreg = MCT_G_WSTAT;
101 1.1 matt switch (o) {
102 1.1 matt case MCT_G_COMP0_L:
103 1.1 matt bit = G_WSTAT_COMP0_L;
104 1.1 matt break;
105 1.1 matt case MCT_G_COMP0_U:
106 1.1 matt bit = G_WSTAT_COMP0_U;
107 1.1 matt break;
108 1.1 matt case MCT_G_COMP0_ADD_INCR:
109 1.1 matt bit = G_WSTAT_ADD_INCR;
110 1.1 matt break;
111 1.1 matt case MCT_G_TCON:
112 1.1 matt bit = G_WSTAT_TCON;
113 1.1 matt break;
114 1.1 matt default:
115 1.1 matt /* all other registers */
116 1.1 matt return;
117 1.1 matt }
118 1.1 matt }
119 1.1 matt
120 1.1 matt /* wait for ack */
121 1.1 matt for (i = 0; i < 10000000; i++) {
122 1.1 matt /* value accepted by the hardware/hal ? */
123 1.1 matt if (mct_read_global(sc, wreg) & bit) {
124 1.1 matt /* ack */
125 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh, wreg, bit);
126 1.1 matt return;
127 1.1 matt }
128 1.1 matt }
129 1.1 matt panic("MCT hangs after writing %#x at %#x", v, (uint32_t) o);
130 1.1 matt }
131 1.1 matt
132 1.1 matt
133 1.1 matt static int
134 1.1 matt mct_match(device_t parent, cfdata_t cf, void *aux)
135 1.1 matt {
136 1.7 marty const char * const compatible[] = { "samsung,exynos4210-mct",
137 1.7 marty NULL };
138 1.1 matt
139 1.7 marty struct fdt_attach_args * const faa = aux;
140 1.7 marty return of_match_compatible(faa->faa_phandle, compatible);
141 1.1 matt }
142 1.1 matt
143 1.1 matt
144 1.1 matt static void
145 1.1 matt mct_attach(device_t parent, device_t self, void *aux)
146 1.1 matt {
147 1.1 matt struct mct_softc * const sc = &mct_sc;
148 1.7 marty // prop_dictionary_t dict = device_properties(self);
149 1.7 marty // char freqbuf[sizeof("XXX SHz")];
150 1.7 marty struct fdt_attach_args * const faa = aux;
151 1.7 marty bus_addr_t addr;
152 1.7 marty bus_size_t size;
153 1.7 marty int error;
154 1.7 marty
155 1.7 marty if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
156 1.7 marty aprint_error(": couldn't get registers\n");
157 1.7 marty return;
158 1.7 marty }
159 1.1 matt
160 1.1 matt self->dv_private = sc;
161 1.1 matt sc->sc_dev = self;
162 1.7 marty sc->sc_bst = faa->faa_bst;
163 1.7 marty /* MJF: Need to get irq from the dtd */
164 1.7 marty // sc->sc_irq = exyo->exyo_loc.loc_intr;
165 1.7 marty
166 1.7 marty error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
167 1.7 marty if (error) {
168 1.7 marty aprint_error(": couldn't map %#llx: %d",
169 1.7 marty (uint64_t)addr, error);
170 1.7 marty return;
171 1.7 marty }
172 1.1 matt
173 1.1 matt aprint_naive("\n");
174 1.7 marty aprint_normal(": Exynos SoC multi core timer (64 bits)\n");
175 1.1 matt
176 1.1 matt evcnt_attach_dynamic(&sc->sc_ev_missing_ticks, EVCNT_TYPE_MISC, NULL,
177 1.1 matt device_xname(self), "missing interrupts");
178 1.1 matt
179 1.1 matt sc->sc_global_ih = intr_establish(sc->sc_irq, IPL_CLOCK, IST_EDGE,
180 1.1 matt clockhandler, NULL);
181 1.1 matt if (sc->sc_global_ih == NULL)
182 1.1 matt panic("%s: unable to register timer interrupt", __func__);
183 1.1 matt aprint_normal_dev(sc->sc_dev, "interrupting on irq %d\n", sc->sc_irq);
184 1.1 matt }
185 1.1 matt
186 1.1 matt
187 1.1 matt static inline uint64_t
188 1.1 matt mct_gettime(struct mct_softc *sc)
189 1.1 matt {
190 1.1 matt uint32_t lo, hi;
191 1.1 matt do {
192 1.1 matt hi = mct_read_global(sc, MCT_G_CNT_U);
193 1.1 matt lo = mct_read_global(sc, MCT_G_CNT_L);
194 1.1 matt } while (hi != mct_read_global(sc, MCT_G_CNT_U));
195 1.1 matt return ((uint64_t) hi << 32) | lo;
196 1.1 matt }
197 1.1 matt
198 1.1 matt
199 1.1 matt /* interrupt handler */
200 1.1 matt static int
201 1.1 matt clockhandler(void *arg)
202 1.1 matt {
203 1.1 matt struct clockframe * const cf = arg;
204 1.1 matt struct mct_softc * const sc = &mct_sc;
205 1.1 matt const uint64_t now = mct_gettime(sc);
206 1.3 reinoud int64_t delta = now - sc->sc_lastintr;
207 1.3 reinoud int64_t periods = delta / sc->sc_autoinc;
208 1.3 reinoud
209 1.3 reinoud KASSERT(delta >= 0);
210 1.3 reinoud KASSERT(periods >= 0);
211 1.1 matt
212 1.1 matt /* ack the interrupt */
213 1.1 matt mct_write_global(sc, MCT_G_INT_CSTAT, G_INT_CSTAT_CLEAR);
214 1.1 matt
215 1.4 reinoud /* check if we missed clock interrupts */
216 1.3 reinoud if (periods > 1)
217 1.3 reinoud sc->sc_ev_missing_ticks.ev_count += periods - 1;
218 1.1 matt
219 1.1 matt sc->sc_lastintr = now;
220 1.1 matt hardclock(cf);
221 1.1 matt
222 1.1 matt /* handled */
223 1.1 matt return 1;
224 1.1 matt }
225 1.1 matt
226 1.1 matt
227 1.1 matt void
228 1.1 matt mct_init_cpu_clock(struct cpu_info *ci)
229 1.1 matt {
230 1.1 matt struct mct_softc * const sc = &mct_sc;
231 1.1 matt uint64_t now = mct_gettime(sc);
232 1.1 matt uint64_t then;
233 1.1 matt uint32_t tcon;
234 1.1 matt
235 1.1 matt KASSERT(ci == curcpu());
236 1.1 matt
237 1.1 matt sc->sc_lastintr = now;
238 1.1 matt
239 1.1 matt /* get current config */
240 1.1 matt tcon = mct_read_global(sc, MCT_G_TCON);
241 1.1 matt
242 1.1 matt /* setup auto increment */
243 1.1 matt mct_write_global(sc, MCT_G_COMP0_ADD_INCR, sc->sc_autoinc);
244 1.1 matt
245 1.1 matt /* (re)setup comparator */
246 1.1 matt then = now + sc->sc_autoinc;
247 1.1 matt mct_write_global(sc, MCT_G_COMP0_L, (uint32_t) then);
248 1.1 matt mct_write_global(sc, MCT_G_COMP0_U, (uint32_t) (then >> 32));
249 1.1 matt tcon |= G_TCON_COMP0_AUTOINC;
250 1.1 matt tcon |= G_TCON_COMP0_ENABLE;
251 1.1 matt
252 1.1 matt /* start timer */
253 1.1 matt tcon |= G_TCON_START;
254 1.1 matt
255 1.1 matt /* enable interrupt */
256 1.1 matt mct_write_global(sc, MCT_G_INT_ENB, G_INT_ENB_ENABLE);
257 1.1 matt
258 1.1 matt /* update config, starting the thing */
259 1.1 matt mct_write_global(sc, MCT_G_TCON, tcon);
260 1.1 matt }
261 1.1 matt
262 1.1 matt
263 1.6 marty #if 0
264 1.1 matt void
265 1.1 matt cpu_initclocks(void)
266 1.1 matt {
267 1.1 matt struct mct_softc * const sc = &mct_sc;
268 1.1 matt
269 1.1 matt sc->sc_autoinc = sc->sc_freq / hz;
270 1.1 matt mct_init_cpu_clock(curcpu());
271 1.1 matt
272 1.1 matt mct_timecounter.tc_name = device_xname(sc->sc_dev);
273 1.1 matt mct_timecounter.tc_frequency = sc->sc_freq;
274 1.1 matt
275 1.1 matt tc_init(&mct_timecounter);
276 1.1 matt
277 1.1 matt #if 0
278 1.1 matt {
279 1.1 matt uint64_t then, now;
280 1.1 matt
281 1.1 matt printf("testing timer\n");
282 1.1 matt for (int i = 0; i < 200; i++) {
283 1.1 matt printf("cstat %d\n", mct_read_global(sc, MCT_G_INT_CSTAT));
284 1.1 matt then = mct_get_timecount(&mct_timecounter);
285 1.1 matt do {
286 1.1 matt now = mct_get_timecount(&mct_timecounter);
287 1.1 matt } while (now == then);
288 1.1 matt printf("\tgot %"PRIu64"\n", now);
289 1.1 matt for (int j = 0; j < 90000; j++);
290 1.1 matt }
291 1.1 matt printf("passed\n");
292 1.1 matt }
293 1.1 matt #endif
294 1.1 matt }
295 1.1 matt
296 1.6 marty #endif
297