mct.c revision 1.10 1 1.10 marty /* $NetBSD: mct.c,v 1.10 2016/01/07 04:45:10 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.10 marty __KERNEL_RCSID(1, "$NetBSD: mct.c,v 1.10 2016/01/07 04:45:10 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.1 matt
58 1.10 marty static int clockhandler(void *);
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.6 marty #if 0
64 1.1 matt static struct timecounter mct_timecounter = {
65 1.1 matt .tc_get_timecount = mct_get_timecount,
66 1.1 matt .tc_poll_pps = 0,
67 1.1 matt .tc_counter_mask = ~0u,
68 1.1 matt .tc_frequency = 0, /* set by cpu_initclocks() */
69 1.1 matt .tc_name = NULL, /* set by cpu_initclocks() */
70 1.1 matt .tc_quality = 500, /* why 500? */
71 1.1 matt .tc_priv = &mct_sc,
72 1.1 matt .tc_next = NULL,
73 1.1 matt };
74 1.6 marty #endif
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 static int
133 1.1 matt mct_match(device_t parent, cfdata_t cf, void *aux)
134 1.1 matt {
135 1.7 marty const char * const compatible[] = { "samsung,exynos4210-mct",
136 1.7 marty NULL };
137 1.1 matt
138 1.7 marty struct fdt_attach_args * const faa = aux;
139 1.7 marty return of_match_compatible(faa->faa_phandle, compatible);
140 1.1 matt }
141 1.1 matt
142 1.1 matt
143 1.1 matt static void
144 1.1 matt mct_attach(device_t parent, device_t self, void *aux)
145 1.1 matt {
146 1.1 matt struct mct_softc * const sc = &mct_sc;
147 1.7 marty struct fdt_attach_args * const faa = aux;
148 1.7 marty bus_addr_t addr;
149 1.7 marty bus_size_t size;
150 1.7 marty int error;
151 1.7 marty
152 1.7 marty if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
153 1.7 marty aprint_error(": couldn't get registers\n");
154 1.7 marty return;
155 1.7 marty }
156 1.1 matt
157 1.1 matt self->dv_private = sc;
158 1.1 matt sc->sc_dev = self;
159 1.7 marty sc->sc_bst = faa->faa_bst;
160 1.7 marty
161 1.7 marty error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
162 1.7 marty if (error) {
163 1.7 marty aprint_error(": couldn't map %#llx: %d",
164 1.7 marty (uint64_t)addr, error);
165 1.7 marty return;
166 1.7 marty }
167 1.1 matt
168 1.1 matt aprint_naive("\n");
169 1.9 marty aprint_normal(": Exynos SoC multi core timer (64 bits) - NOT IMPLEMENTED\n");
170 1.1 matt
171 1.1 matt evcnt_attach_dynamic(&sc->sc_ev_missing_ticks, EVCNT_TYPE_MISC, NULL,
172 1.1 matt device_xname(self), "missing interrupts");
173 1.1 matt
174 1.9 marty for (int i = 0; i < 12; i++)
175 1.10 marty fdtbus_intr_establish(faa->faa_phandle, i, 0, 0,
176 1.10 marty clockhandler, 0);
177 1.9 marty }
178 1.1 matt
179 1.1 matt static inline uint64_t
180 1.1 matt mct_gettime(struct mct_softc *sc)
181 1.1 matt {
182 1.1 matt uint32_t lo, hi;
183 1.1 matt do {
184 1.1 matt hi = mct_read_global(sc, MCT_G_CNT_U);
185 1.1 matt lo = mct_read_global(sc, MCT_G_CNT_L);
186 1.1 matt } while (hi != mct_read_global(sc, MCT_G_CNT_U));
187 1.1 matt return ((uint64_t) hi << 32) | lo;
188 1.1 matt }
189 1.1 matt
190 1.1 matt
191 1.1 matt /* interrupt handler */
192 1.1 matt static int
193 1.1 matt clockhandler(void *arg)
194 1.1 matt {
195 1.1 matt struct clockframe * const cf = arg;
196 1.1 matt struct mct_softc * const sc = &mct_sc;
197 1.1 matt const uint64_t now = mct_gettime(sc);
198 1.3 reinoud int64_t delta = now - sc->sc_lastintr;
199 1.3 reinoud int64_t periods = delta / sc->sc_autoinc;
200 1.3 reinoud
201 1.3 reinoud KASSERT(delta >= 0);
202 1.3 reinoud KASSERT(periods >= 0);
203 1.1 matt
204 1.1 matt /* ack the interrupt */
205 1.1 matt mct_write_global(sc, MCT_G_INT_CSTAT, G_INT_CSTAT_CLEAR);
206 1.1 matt
207 1.4 reinoud /* check if we missed clock interrupts */
208 1.3 reinoud if (periods > 1)
209 1.3 reinoud sc->sc_ev_missing_ticks.ev_count += periods - 1;
210 1.1 matt
211 1.1 matt sc->sc_lastintr = now;
212 1.1 matt hardclock(cf);
213 1.1 matt
214 1.1 matt /* handled */
215 1.1 matt return 1;
216 1.1 matt }
217 1.1 matt
218 1.1 matt void
219 1.1 matt mct_init_cpu_clock(struct cpu_info *ci)
220 1.1 matt {
221 1.1 matt struct mct_softc * const sc = &mct_sc;
222 1.1 matt uint64_t now = mct_gettime(sc);
223 1.1 matt uint64_t then;
224 1.1 matt uint32_t tcon;
225 1.1 matt
226 1.1 matt KASSERT(ci == curcpu());
227 1.1 matt
228 1.1 matt sc->sc_lastintr = now;
229 1.1 matt
230 1.1 matt /* get current config */
231 1.1 matt tcon = mct_read_global(sc, MCT_G_TCON);
232 1.1 matt
233 1.1 matt /* setup auto increment */
234 1.1 matt mct_write_global(sc, MCT_G_COMP0_ADD_INCR, sc->sc_autoinc);
235 1.1 matt
236 1.1 matt /* (re)setup comparator */
237 1.1 matt then = now + sc->sc_autoinc;
238 1.1 matt mct_write_global(sc, MCT_G_COMP0_L, (uint32_t) then);
239 1.1 matt mct_write_global(sc, MCT_G_COMP0_U, (uint32_t) (then >> 32));
240 1.1 matt tcon |= G_TCON_COMP0_AUTOINC;
241 1.1 matt tcon |= G_TCON_COMP0_ENABLE;
242 1.1 matt
243 1.1 matt /* start timer */
244 1.1 matt tcon |= G_TCON_START;
245 1.1 matt
246 1.1 matt /* enable interrupt */
247 1.1 matt mct_write_global(sc, MCT_G_INT_ENB, G_INT_ENB_ENABLE);
248 1.1 matt
249 1.1 matt /* update config, starting the thing */
250 1.1 matt mct_write_global(sc, MCT_G_TCON, tcon);
251 1.1 matt }
252 1.1 matt
253 1.1 matt
254 1.6 marty #if 0
255 1.1 matt void
256 1.1 matt cpu_initclocks(void)
257 1.1 matt {
258 1.1 matt struct mct_softc * const sc = &mct_sc;
259 1.1 matt
260 1.1 matt sc->sc_autoinc = sc->sc_freq / hz;
261 1.1 matt mct_init_cpu_clock(curcpu());
262 1.1 matt
263 1.1 matt mct_timecounter.tc_name = device_xname(sc->sc_dev);
264 1.1 matt mct_timecounter.tc_frequency = sc->sc_freq;
265 1.1 matt
266 1.1 matt tc_init(&mct_timecounter);
267 1.1 matt
268 1.1 matt #if 0
269 1.1 matt {
270 1.1 matt uint64_t then, now;
271 1.1 matt
272 1.1 matt printf("testing timer\n");
273 1.1 matt for (int i = 0; i < 200; i++) {
274 1.1 matt printf("cstat %d\n", mct_read_global(sc, MCT_G_INT_CSTAT));
275 1.1 matt then = mct_get_timecount(&mct_timecounter);
276 1.1 matt do {
277 1.1 matt now = mct_get_timecount(&mct_timecounter);
278 1.1 matt } while (now == then);
279 1.1 matt printf("\tgot %"PRIu64"\n", now);
280 1.1 matt for (int j = 0; j < 90000; j++);
281 1.1 matt }
282 1.1 matt printf("passed\n");
283 1.1 matt }
284 1.1 matt #endif
285 1.1 matt }
286 1.1 matt
287 1.6 marty #endif
288