1 1.9 thorpej /* $NetBSD: clock_ebus.c,v 1.9 2025/09/07 21:45:12 thorpej Exp $ */ 2 1.1 pooka 3 1.1 pooka /*- 4 1.1 pooka * Copyright (c) 2010 The NetBSD Foundation, Inc. 5 1.1 pooka * All rights reserved. 6 1.1 pooka * 7 1.1 pooka * This code was written by Alessandro Forin and Neil Pittman 8 1.1 pooka * at Microsoft Research and contributed to The NetBSD Foundation 9 1.1 pooka * by Microsoft Corporation. 10 1.1 pooka * 11 1.1 pooka * Redistribution and use in source and binary forms, with or without 12 1.1 pooka * modification, are permitted provided that the following conditions 13 1.1 pooka * are met: 14 1.1 pooka * 1. Redistributions of source code must retain the above copyright 15 1.1 pooka * notice, this list of conditions and the following disclaimer. 16 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright 17 1.1 pooka * notice, this list of conditions and the following disclaimer in the 18 1.1 pooka * documentation and/or other materials provided with the distribution. 19 1.1 pooka * 20 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 1.1 pooka * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 1.1 pooka * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 1.1 pooka * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 1.1 pooka * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 1.1 pooka * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 1.1 pooka * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 1.1 pooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 1.1 pooka * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 1.1 pooka * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 1.1 pooka * POSSIBILITY OF SUCH DAMAGE. 31 1.1 pooka */ 32 1.1 pooka 33 1.1 pooka #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 34 1.9 thorpej __KERNEL_RCSID(0, "$NetBSD: clock_ebus.c,v 1.9 2025/09/07 21:45:12 thorpej Exp $"); 35 1.1 pooka 36 1.1 pooka #include <sys/param.h> 37 1.1 pooka #include <sys/kernel.h> 38 1.1 pooka #include <sys/device.h> 39 1.1 pooka #include <sys/systm.h> 40 1.1 pooka #include <sys/timetc.h> 41 1.1 pooka 42 1.1 pooka #include <dev/clock_subr.h> 43 1.1 pooka 44 1.1 pooka #include <emips/ebus/ebusvar.h> 45 1.1 pooka #include <emips/emips/machdep.h> 46 1.1 pooka #include <machine/emipsreg.h> 47 1.1 pooka 48 1.1 pooka /* 49 1.1 pooka * Device softc 50 1.1 pooka */ 51 1.1 pooka struct eclock_softc { 52 1.6 tsutsui device_t sc_dev; 53 1.1 pooka struct _Tc *sc_dp; 54 1.4 tsutsui uint32_t sc_reload; 55 1.2 tsutsui struct timecounter sc_tc; 56 1.2 tsutsui struct todr_chip_handle sc_todr; 57 1.1 pooka }; 58 1.1 pooka 59 1.2 tsutsui static int eclock_ebus_match(device_t, cfdata_t, void *); 60 1.2 tsutsui static void eclock_ebus_attach(device_t, device_t, void *); 61 1.1 pooka 62 1.6 tsutsui CFATTACH_DECL_NEW(eclock_ebus, sizeof (struct eclock_softc), 63 1.1 pooka eclock_ebus_match, eclock_ebus_attach, NULL, NULL); 64 1.1 pooka 65 1.2 tsutsui void eclock_init(device_t); 66 1.1 pooka 67 1.2 tsutsui static void __eclock_init(device_t); 68 1.2 tsutsui static int eclock_gettime(struct todr_chip_handle *, struct timeval *); 69 1.2 tsutsui static int eclock_settime(struct todr_chip_handle *, struct timeval *); 70 1.2 tsutsui static int eclock_ebus_intr(void *, void *); 71 1.2 tsutsui static u_int eclock_counter(struct timecounter *); 72 1.2 tsutsui 73 1.2 tsutsui /* BUGBUG resolve the gap between cpu_initclocks() and eclock_init(x) */ 74 1.2 tsutsui device_t clockdev = NULL; 75 1.1 pooka 76 1.1 pooka void 77 1.2 tsutsui eclock_init(device_t dev) 78 1.1 pooka { 79 1.2 tsutsui 80 1.2 tsutsui if (dev == NULL) 81 1.2 tsutsui dev = clockdev; 82 1.2 tsutsui if (dev == NULL) 83 1.2 tsutsui panic("eclock_init"); 84 1.2 tsutsui __eclock_init(dev); 85 1.1 pooka } 86 1.1 pooka 87 1.1 pooka static void 88 1.2 tsutsui __eclock_init(device_t dev) 89 1.1 pooka { 90 1.6 tsutsui struct eclock_softc *sc = device_private(dev); 91 1.2 tsutsui struct _Tc *tc = sc->sc_dp; 92 1.4 tsutsui uint32_t reload = 10 * 1000000; /* 1sec in 100ns units (10MHz clock) */ 93 1.1 pooka 94 1.2 tsutsui /* 95 1.2 tsutsui * Compute reload according to whatever value passed in, 96 1.2 tsutsui * Warn if fractional 97 1.2 tsutsui */ 98 1.2 tsutsui if (hz > 1) { 99 1.2 tsutsui uint32_t r = reload / hz; 100 1.2 tsutsui if ((r * hz) != reload) 101 1.2 tsutsui printf("%s: %d Hz clock will cause roundoffs" 102 1.2 tsutsui " with 10MHz xtal (%d)\n", 103 1.6 tsutsui device_xname(sc->sc_dev), hz, reload - (r * hz)); 104 1.1 pooka reload = r; 105 1.1 pooka } 106 1.1 pooka 107 1.4 tsutsui sc->sc_reload = reload; 108 1.1 pooka 109 1.2 tsutsui /* Start the counter */ 110 1.2 tsutsui tc->DownCounterHigh = 0; 111 1.4 tsutsui tc->DownCounter = sc->sc_reload; 112 1.2 tsutsui tc->Control = TCCT_ENABLE | TCCT_INT_ENABLE; 113 1.1 pooka } 114 1.1 pooka 115 1.1 pooka /* 116 1.1 pooka * Get the time of day, based on the clock's value and/or the base value. 117 1.1 pooka * NB: At 10MHz, our 64bits FreeRunning is worth 58,426 years. 118 1.1 pooka */ 119 1.1 pooka 120 1.1 pooka 121 1.1 pooka static int 122 1.1 pooka eclock_gettime(struct todr_chip_handle *todr, struct timeval *tv) 123 1.1 pooka { 124 1.9 thorpej struct eclock_softc *sc = device_private(todr->todr_dev); 125 1.2 tsutsui struct _Tc *tc = sc->sc_dp; 126 1.2 tsutsui uint64_t free; 127 1.2 tsutsui int s; 128 1.2 tsutsui 129 1.2 tsutsui /* 130 1.2 tsutsui * 32bit processor, guard against interrupts in the middle of 131 1.2 tsutsui * reading this 64bit entity 132 1.2 tsutsui */ 133 1.2 tsutsui /* BUGBUG Should read it "twice" to guard against rollover too. */ 134 1.1 pooka s = splhigh(); 135 1.2 tsutsui free = tc->FreeRunning; 136 1.1 pooka splx(s); 137 1.1 pooka 138 1.2 tsutsui /* 139 1.2 tsutsui * Big fight with the compiler here, it gets very confused by 64bits. 140 1.2 tsutsui */ 141 1.8 martin #if 1 142 1.2 tsutsui /* 143 1.2 tsutsui * This is in C: 144 1.2 tsutsui */ 145 1.2 tsutsui { 146 1.2 tsutsui uint64_t freeS, freeU; 147 1.8 martin freeS = free / 10000000UL; 148 1.8 martin freeU = free % 10000000UL; 149 1.2 tsutsui tv->tv_sec = freeS; 150 1.2 tsutsui tv->tv_usec = freeU / 10; 151 1.2 tsutsui #if 0 152 1.2 tsutsui printf("egt: s x%" PRIx64 " u x%lx (fs %" PRId64 153 1.2 tsutsui " fu %" PRId64 " f %" PRId64 ")\n", 154 1.2 tsutsui tv->tv_sec, tv->tv_usec, freeS, freeU, free); 155 1.2 tsutsui #endif 156 1.2 tsutsui } 157 1.1 pooka #else 158 1.2 tsutsui /* 159 1.2 tsutsui * And this is in assembly :-) 160 1.2 tsutsui */ 161 1.2 tsutsui { 162 1.2 tsutsui u_quad_t r; 163 1.2 tsutsui u_quad_t d = __qdivrem(free,(u_quad_t)10000000,&r); 164 1.2 tsutsui uint32_t su, uu; 165 1.2 tsutsui su = (uint32_t)d; 166 1.2 tsutsui uu = (uint32_t)r; 167 1.2 tsutsui uu = uu / 10; /* in usecs */ 168 1.2 tsutsui tv->tv_sec = su; 169 1.2 tsutsui tv->tv_usec = uu; 170 1.2 tsutsui #if 0 171 1.2 tsutsui printf("egt: s x%" PRIx64 " u x%lx (fs %" PRId64 172 1.2 tsutsui " fu %" PRId64 " f %" PRId64 ")\n", 173 1.2 tsutsui tv->tv_sec, tv->tv_usec, d, r, free); 174 1.2 tsutsui #endif 175 1.2 tsutsui } 176 1.1 pooka #endif 177 1.1 pooka 178 1.2 tsutsui return 0; 179 1.1 pooka } 180 1.1 pooka 181 1.1 pooka /* 182 1.1 pooka * Reset the TODR based on the time value. 183 1.1 pooka */ 184 1.1 pooka static int 185 1.1 pooka eclock_settime(struct todr_chip_handle *todr, struct timeval *tv) 186 1.1 pooka { 187 1.9 thorpej struct eclock_softc *sc = device_private(todr->todr_dev); 188 1.2 tsutsui struct _Tc *tc = sc->sc_dp; 189 1.4 tsutsui uint64_t free, su; 190 1.4 tsutsui uint32_t uu; 191 1.2 tsutsui int s; 192 1.1 pooka 193 1.2 tsutsui /* Careful with what we do here, else the compilerbugs hit hard */ 194 1.1 pooka s = splhigh(); 195 1.1 pooka 196 1.4 tsutsui su = (uint64_t)tv->tv_sec; /* 0(tv) */ 197 1.4 tsutsui uu = (uint32_t)tv->tv_usec; /* 8(tv) */ 198 1.1 pooka 199 1.1 pooka 200 1.4 tsutsui free = su * 10 * 1000 * 1000; 201 1.2 tsutsui free += uu * 10; 202 1.1 pooka 203 1.2 tsutsui tc->FreeRunning = free; 204 1.1 pooka splx(s); 205 1.1 pooka 206 1.1 pooka #if 0 207 1.2 tsutsui /* 208 1.1 pooka Should compile to something like this: 209 1.1 pooka 80260c84 <eclock_settime>: 210 1.1 pooka 80260c84: 27bdffc0 addiu sp,sp,-64 211 1.1 pooka 80260c88: afbf0038 sw ra,56(sp) 212 1.1 pooka 80260c8c: afb40030 sw s4,48(sp) 213 1.1 pooka 80260c90: afb3002c sw s3,44(sp) 214 1.1 pooka 80260c94: afb20028 sw s2,40(sp) 215 1.1 pooka 80260c98: afb10024 sw s1,36(sp) 216 1.1 pooka 80260c9c: afb00020 sw s0,32(sp) 217 1.1 pooka 80260ca0: afb50034 sw s5,52(sp) 218 1.1 pooka 80260ca4: 8c820000 lw v0,0(a0) 219 1.1 pooka 80260ca8: 00a09021 move s2,a1 220 1.1 pooka 80260cac: 8c55003c lw s5,60(v0) //s5=tc 221 1.1 pooka 80260cb0: 0c004122 jal 80010488 <_splraise> 222 1.1 pooka 80260cb4: 3404ff00 li a0,0xff00 223 1.1 pooka 80260cb8: 8e540000 lw s4,0(s2) //s4=tv->tv_sec=us 224 1.1 pooka 80260cbc: 3c060098 lui a2,0x98 225 1.1 pooka 80260cc0: 34c69680 ori a2,a2,0x9680 //a2=10000000 226 1.1 pooka 80260cc4: 02860019 multu s4,a2 //free=us*10000000 227 1.1 pooka 80260cc8: 8e530004 lw s3,4(s2) //s3=uu 228 1.1 pooka 80260ccc: 00402021 move a0,v0 //s=splhigh() 229 1.1 pooka 80260cd0: 001328c0 sll a1,s3,0x3 230 1.1 pooka 80260cd4: 00131040 sll v0,s3,0x1 231 1.1 pooka 80260cd8: 00451021 addu v0,v0,a1 232 1.1 pooka 80260cdc: 00401821 move v1,v0 //v1 = uu*10 233 1.1 pooka 80260ce0: 00001021 move v0,zero 234 1.1 pooka 80260ce4: 00003812 mflo a3 //a3=low(free) 235 1.1 pooka 80260ce8: 00e38821 addu s1,a3,v1 //s1=low(free)+(uu*10) 236 1.1 pooka 80260cec: 0227282b sltu a1,s1,a3 //a1=overflow bit 237 1.1 pooka 80260cf0: 00003010 mfhi a2 //a2=high(free) 238 1.1 pooka 80260cf4: 00c28021 addu s0,a2,v0 //s0=a2=high(free) [useless, v0=0] 239 1.1 pooka 80260cf8: 00b08021 addu s0,a1,s0 //s0+=overflow bit 240 1.1 pooka 80260cfc: aeb1000c sw s1,12(s5) 241 1.1 pooka 80260d00: aeb00008 sw s0,8(s5) 242 1.1 pooka 80260d04: 0c00413f jal 800104fc <_splset> 243 1.1 pooka 80260d08: 00000000 nop 244 1.2 tsutsui */ 245 1.2 tsutsui #endif 246 1.1 pooka 247 1.2 tsutsui #if 0 248 1.2 tsutsui printf("est: s x%" PRIx64 " u x%lx (%d %d), free %" PRId64 "\n", 249 1.2 tsutsui tv->tv_sec, tv->tv_usec, su, uu, free); 250 1.1 pooka #endif 251 1.1 pooka 252 1.2 tsutsui return 0; 253 1.1 pooka } 254 1.1 pooka 255 1.1 pooka static int 256 1.1 pooka eclock_ebus_intr(void *cookie, void *f) 257 1.1 pooka { 258 1.1 pooka struct eclock_softc *sc = cookie; 259 1.2 tsutsui struct _Tc *tc = sc->sc_dp; 260 1.2 tsutsui struct clockframe *cf = f; 261 1.7 christos volatile uint32_t x __unused; 262 1.2 tsutsui 263 1.2 tsutsui x = tc->Control; 264 1.2 tsutsui tc->DownCounterHigh = 0; 265 1.4 tsutsui tc->DownCounter = sc->sc_reload; 266 1.1 pooka 267 1.2 tsutsui hardclock(cf); 268 1.2 tsutsui emips_clock_evcnt.ev_count++; 269 1.1 pooka 270 1.2 tsutsui return 0; 271 1.1 pooka } 272 1.1 pooka 273 1.1 pooka static u_int 274 1.1 pooka eclock_counter(struct timecounter *tc) 275 1.1 pooka { 276 1.1 pooka struct eclock_softc *sc = tc->tc_priv; 277 1.2 tsutsui struct _Tc *Tc = sc->sc_dp; 278 1.2 tsutsui 279 1.2 tsutsui return (u_int)Tc->FreeRunning; /* NB: chops to 32bits */ 280 1.1 pooka } 281 1.1 pooka 282 1.1 pooka 283 1.1 pooka static int 284 1.2 tsutsui eclock_ebus_match(device_t parent, cfdata_t cf, void *aux) 285 1.1 pooka { 286 1.1 pooka struct ebus_attach_args *ia = aux; 287 1.2 tsutsui struct _Tc *mc = (struct _Tc *)ia->ia_vaddr; 288 1.1 pooka 289 1.1 pooka if (strcmp("eclock", ia->ia_name) != 0) 290 1.2 tsutsui return 0; 291 1.2 tsutsui if ((mc == NULL) || 292 1.2 tsutsui (mc->Tag != PMTTAG_TIMER)) 293 1.2 tsutsui return 0; 294 1.1 pooka 295 1.2 tsutsui return 1; 296 1.1 pooka } 297 1.1 pooka 298 1.1 pooka static void 299 1.2 tsutsui eclock_ebus_attach(device_t parent, device_t self, void *aux) 300 1.1 pooka { 301 1.6 tsutsui struct eclock_softc *sc = device_private(self); 302 1.2 tsutsui struct ebus_attach_args *ia = aux; 303 1.1 pooka 304 1.6 tsutsui sc->sc_dev = self; 305 1.2 tsutsui sc->sc_dp = (struct _Tc *)ia->ia_vaddr; 306 1.1 pooka 307 1.2 tsutsui /* NB: We are chopping our 64bit free-running down to 32bits */ 308 1.2 tsutsui sc->sc_tc.tc_get_timecount = eclock_counter; 309 1.2 tsutsui sc->sc_tc.tc_poll_pps = 0; 310 1.2 tsutsui sc->sc_tc.tc_counter_mask = 0xffffffff; 311 1.5 tsutsui sc->sc_tc.tc_frequency = 10 * 1000 * 1000; /* 10 MHz */ 312 1.2 tsutsui sc->sc_tc.tc_name = "eclock"; /* BUGBUG is it unique per instance?? */ 313 1.2 tsutsui sc->sc_tc.tc_quality = 2000; /* uhu? */ 314 1.2 tsutsui sc->sc_tc.tc_priv = sc; 315 1.2 tsutsui sc->sc_tc.tc_next = NULL; 316 1.1 pooka 317 1.1 pooka #if DEBUG 318 1.2 tsutsui printf(" virt=%p ", (void *)sc->sc_dp); 319 1.1 pooka #endif 320 1.1 pooka printf(": eMIPS clock\n"); 321 1.1 pooka 322 1.1 pooka /* Turn interrupts off, just in case. */ 323 1.2 tsutsui sc->sc_dp->Control &= ~(TCCT_INT_ENABLE|TCCT_INTERRUPT); 324 1.1 pooka 325 1.1 pooka ebus_intr_establish(parent, (void *)ia->ia_cookie, IPL_CLOCK, 326 1.1 pooka eclock_ebus_intr, sc); 327 1.1 pooka 328 1.1 pooka #ifdef EVCNT_COUNTERS 329 1.1 pooka evcnt_attach_dynamic(&clock_intr_evcnt, EVCNT_TYPE_INTR, NULL, 330 1.6 tsutsui device_xname(self), "intr"); 331 1.1 pooka #endif 332 1.1 pooka 333 1.2 tsutsui clockdev = self; 334 1.5 tsutsui memset(&sc->sc_todr, 0, sizeof sc->sc_todr); 335 1.9 thorpej sc->sc_todr.todr_dev = self; 336 1.2 tsutsui sc->sc_todr.todr_gettime = eclock_gettime; 337 1.2 tsutsui sc->sc_todr.todr_settime = eclock_settime; 338 1.2 tsutsui todr_attach(&sc->sc_todr); 339 1.1 pooka 340 1.2 tsutsui tc_init(&sc->sc_tc); 341 1.1 pooka } 342