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