mct.c revision 1.14 1 1.14 jmcneill /* $NetBSD: mct.c,v 1.14 2018/07/02 12:49:37 jmcneill Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.14 jmcneill * Copyright (c) 2014-2018 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.14 jmcneill * by Reinoud Zandijk and Jared McNeill.
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.14 jmcneill __KERNEL_RCSID(1, "$NetBSD: mct.c,v 1.14 2018/07/02 12:49:37 jmcneill 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.12 jmcneill #include <arm/fdt/arm_fdtvar.h>
55 1.1 matt
56 1.14 jmcneill static struct mct_softc mct_sc;
57 1.14 jmcneill
58 1.1 matt static int mct_match(device_t, cfdata_t, void *);
59 1.1 matt static void mct_attach(device_t, device_t, void *);
60 1.1 matt
61 1.14 jmcneill static u_int mct_get_timecount(struct timecounter *);
62 1.14 jmcneill
63 1.14 jmcneill static struct timecounter mct_timecounter = {
64 1.14 jmcneill .tc_get_timecount = mct_get_timecount,
65 1.14 jmcneill .tc_counter_mask = ~0u,
66 1.14 jmcneill .tc_frequency = EXYNOS_F_IN_FREQ,
67 1.14 jmcneill .tc_name = "MCT",
68 1.14 jmcneill .tc_quality = 500,
69 1.14 jmcneill .tc_priv = &mct_sc,
70 1.14 jmcneill };
71 1.14 jmcneill
72 1.1 matt CFATTACH_DECL_NEW(exyo_mct, 0, mct_match, mct_attach, NULL, NULL);
73 1.1 matt
74 1.1 matt static inline uint32_t
75 1.1 matt mct_read_global(struct mct_softc *sc, bus_size_t o)
76 1.1 matt {
77 1.1 matt return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
78 1.1 matt }
79 1.1 matt
80 1.1 matt static inline void
81 1.1 matt mct_write_global(struct mct_softc *sc, bus_size_t o, uint32_t v)
82 1.1 matt {
83 1.1 matt bus_size_t wreg;
84 1.1 matt uint32_t bit;
85 1.1 matt int i;
86 1.1 matt
87 1.1 matt /* do the write */
88 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
89 1.1 matt // printf("%s: write %#x at %#x\n",
90 1.1 matt // __func__, ((uint32_t) sc->sc_bsh + (uint32_t) o), v);
91 1.1 matt
92 1.1 matt /* dependent on the write address, do the ack dance */
93 1.1 matt if (o == MCT_G_CNT_L || o == MCT_G_CNT_U) {
94 1.1 matt wreg = MCT_G_CNT_WSTAT;
95 1.1 matt bit = (o == MCT_G_CNT_L) ? G_CNT_WSTAT_L : G_CNT_WSTAT_U;
96 1.1 matt } else {
97 1.1 matt switch (o) {
98 1.1 matt case MCT_G_COMP0_L:
99 1.14 jmcneill wreg = MCT_G_WSTAT;
100 1.1 matt bit = G_WSTAT_COMP0_L;
101 1.1 matt break;
102 1.1 matt case MCT_G_COMP0_U:
103 1.14 jmcneill wreg = MCT_G_WSTAT;
104 1.1 matt bit = G_WSTAT_COMP0_U;
105 1.1 matt break;
106 1.1 matt case MCT_G_COMP0_ADD_INCR:
107 1.14 jmcneill wreg = MCT_G_WSTAT;
108 1.1 matt bit = G_WSTAT_ADD_INCR;
109 1.1 matt break;
110 1.1 matt case MCT_G_TCON:
111 1.14 jmcneill wreg = MCT_G_WSTAT;
112 1.1 matt bit = G_WSTAT_TCON;
113 1.1 matt break;
114 1.14 jmcneill case MCT_G_CNT_L:
115 1.14 jmcneill wreg = MCT_G_CNT_WSTAT;
116 1.14 jmcneill bit = G_CNT_WSTAT_L;
117 1.14 jmcneill break;
118 1.14 jmcneill case MCT_G_CNT_U:
119 1.14 jmcneill wreg = MCT_G_CNT_WSTAT;
120 1.14 jmcneill bit = G_CNT_WSTAT_U;
121 1.14 jmcneill break;
122 1.1 matt default:
123 1.1 matt /* all other registers */
124 1.1 matt return;
125 1.1 matt }
126 1.1 matt }
127 1.1 matt
128 1.1 matt /* wait for ack */
129 1.1 matt for (i = 0; i < 10000000; i++) {
130 1.1 matt /* value accepted by the hardware/hal ? */
131 1.1 matt if (mct_read_global(sc, wreg) & bit) {
132 1.1 matt /* ack */
133 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh, wreg, bit);
134 1.1 matt return;
135 1.1 matt }
136 1.1 matt }
137 1.1 matt panic("MCT hangs after writing %#x at %#x", v, (uint32_t) o);
138 1.1 matt }
139 1.1 matt
140 1.12 jmcneill static void
141 1.12 jmcneill mct_fdt_cpu_hatch(void *priv, struct cpu_info *ci)
142 1.12 jmcneill {
143 1.14 jmcneill panic("%s: not implemented", __func__);
144 1.14 jmcneill }
145 1.14 jmcneill
146 1.14 jmcneill static int
147 1.14 jmcneill mct_intr(void *arg)
148 1.14 jmcneill {
149 1.14 jmcneill struct mct_softc * const sc = &mct_sc;
150 1.14 jmcneill struct clockframe *frame = arg;
151 1.14 jmcneill
152 1.14 jmcneill mct_write_global(sc, MCT_G_INT_CSTAT, G_INT_CSTAT_CLEAR);
153 1.14 jmcneill
154 1.14 jmcneill hardclock(frame);
155 1.14 jmcneill
156 1.14 jmcneill return 1;
157 1.14 jmcneill }
158 1.14 jmcneill
159 1.14 jmcneill static u_int
160 1.14 jmcneill mct_get_timecount(struct timecounter *tc)
161 1.14 jmcneill {
162 1.14 jmcneill struct mct_softc * const sc = tc->tc_priv;
163 1.14 jmcneill
164 1.14 jmcneill return mct_read_global(sc, MCT_G_CNT_L);
165 1.14 jmcneill }
166 1.14 jmcneill
167 1.14 jmcneill static uint64_t
168 1.14 jmcneill mct_read_gcnt(struct mct_softc *sc)
169 1.14 jmcneill {
170 1.14 jmcneill uint32_t gcntl, gcntu;
171 1.14 jmcneill
172 1.14 jmcneill do {
173 1.14 jmcneill gcntu = mct_read_global(sc, MCT_G_CNT_U);
174 1.14 jmcneill gcntl = mct_read_global(sc, MCT_G_CNT_L);
175 1.14 jmcneill } while (gcntu != mct_read_global(sc, MCT_G_CNT_U));
176 1.14 jmcneill
177 1.14 jmcneill return ((uint64_t)gcntu << 32) | gcntl;
178 1.14 jmcneill }
179 1.14 jmcneill
180 1.14 jmcneill static void
181 1.14 jmcneill mct_cpu_initclocks(void)
182 1.14 jmcneill {
183 1.14 jmcneill struct mct_softc * const sc = &mct_sc;
184 1.14 jmcneill char intrstr[128];
185 1.14 jmcneill
186 1.14 jmcneill if (!fdtbus_intr_str(sc->sc_phandle, 0, intrstr, sizeof(intrstr)))
187 1.14 jmcneill panic("%s: failed to decode interrupt", __func__);
188 1.14 jmcneill
189 1.14 jmcneill sc->sc_global_ih = fdtbus_intr_establish(sc->sc_phandle, 0, IPL_CLOCK,
190 1.14 jmcneill FDT_INTR_MPSAFE, mct_intr, NULL);
191 1.14 jmcneill if (sc->sc_global_ih == NULL)
192 1.14 jmcneill panic("%s: failed to establish timer interrupt on %s", __func__, intrstr);
193 1.14 jmcneill
194 1.14 jmcneill aprint_normal_dev(sc->sc_dev, "interrupting on %s\n", intrstr);
195 1.14 jmcneill
196 1.14 jmcneill /* Start the timer */
197 1.14 jmcneill const u_int autoinc = sc->sc_freq / hz;
198 1.14 jmcneill const uint64_t comp0 = mct_read_gcnt(sc) + autoinc;
199 1.14 jmcneill
200 1.14 jmcneill mct_write_global(sc, MCT_G_TCON, G_TCON_START | G_TCON_COMP0_AUTOINC);
201 1.14 jmcneill mct_write_global(sc, MCT_G_COMP0_ADD_INCR, autoinc);
202 1.14 jmcneill mct_write_global(sc, MCT_G_COMP0_L, (uint32_t)comp0);
203 1.14 jmcneill mct_write_global(sc, MCT_G_COMP0_U, (uint32_t)(comp0 >> 32));
204 1.14 jmcneill mct_write_global(sc, MCT_G_INT_ENB, G_INT_ENB_ENABLE);
205 1.14 jmcneill mct_write_global(sc, MCT_G_TCON, G_TCON_START | G_TCON_COMP0_ENABLE | G_TCON_COMP0_AUTOINC);
206 1.12 jmcneill }
207 1.12 jmcneill
208 1.1 matt static int
209 1.1 matt mct_match(device_t parent, cfdata_t cf, void *aux)
210 1.1 matt {
211 1.7 marty const char * const compatible[] = { "samsung,exynos4210-mct",
212 1.7 marty NULL };
213 1.1 matt
214 1.7 marty struct fdt_attach_args * const faa = aux;
215 1.7 marty return of_match_compatible(faa->faa_phandle, compatible);
216 1.1 matt }
217 1.1 matt
218 1.1 matt static void
219 1.1 matt mct_attach(device_t parent, device_t self, void *aux)
220 1.1 matt {
221 1.1 matt struct mct_softc * const sc = &mct_sc;
222 1.7 marty struct fdt_attach_args * const faa = aux;
223 1.7 marty bus_addr_t addr;
224 1.7 marty bus_size_t size;
225 1.7 marty int error;
226 1.7 marty
227 1.7 marty if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
228 1.7 marty aprint_error(": couldn't get registers\n");
229 1.7 marty return;
230 1.7 marty }
231 1.1 matt
232 1.1 matt self->dv_private = sc;
233 1.1 matt sc->sc_dev = self;
234 1.14 jmcneill sc->sc_phandle = faa->faa_phandle;
235 1.7 marty sc->sc_bst = faa->faa_bst;
236 1.11 jmcneill sc->sc_freq = EXYNOS_F_IN_FREQ;
237 1.7 marty error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
238 1.7 marty if (error) {
239 1.7 marty aprint_error(": couldn't map %#llx: %d",
240 1.7 marty (uint64_t)addr, error);
241 1.7 marty return;
242 1.7 marty }
243 1.1 matt
244 1.1 matt aprint_naive("\n");
245 1.11 jmcneill aprint_normal(": Exynos SoC multi core timer (64 bits)\n");
246 1.1 matt
247 1.14 jmcneill tc_init(&mct_timecounter);
248 1.14 jmcneill
249 1.14 jmcneill arm_fdt_cpu_hatch_register(self, mct_fdt_cpu_hatch);
250 1.14 jmcneill arm_fdt_timer_register(mct_cpu_initclocks);
251 1.14 jmcneill }
252 1.14 jmcneill
253 1.14 jmcneill void
254 1.14 jmcneill mct_delay(u_int n)
255 1.14 jmcneill {
256 1.14 jmcneill struct mct_softc * const sc = &mct_sc;
257 1.14 jmcneill uint64_t cur, prev;
258 1.14 jmcneill
259 1.14 jmcneill if (sc->sc_bsh == 0)
260 1.14 jmcneill panic("%s: mct driver not attached", __func__);
261 1.1 matt
262 1.14 jmcneill const long incs_per_us = sc->sc_freq / 1000000;
263 1.14 jmcneill long ticks = n * incs_per_us;
264 1.1 matt
265 1.14 jmcneill prev = mct_read_gcnt(sc);
266 1.14 jmcneill while (ticks > 0) {
267 1.14 jmcneill cur = mct_read_gcnt(sc);
268 1.14 jmcneill if (cur > prev)
269 1.14 jmcneill ticks -= (cur - prev);
270 1.14 jmcneill else
271 1.14 jmcneill ticks -= (UINT64_MAX - cur + prev);
272 1.14 jmcneill prev = cur;
273 1.14 jmcneill }
274 1.11 jmcneill }
275