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