1 1.9 maxv /*$NetBSD: at91tctmr.c,v 1.9 2020/07/03 16:23:02 maxv Exp $*/ 2 1.2 matt 3 1.2 matt /* 4 1.2 matt * AT91 Timer Counter (TC) based clock functions 5 1.2 matt * Copyright (c) 2007, Embedtronics Oy 6 1.2 matt * All rights reserved. 7 1.2 matt * 8 1.2 matt * Based on vx115_clk.c, 9 1.2 matt * Copyright (c) 2006, Jon Sevy <jsevy (at) cs.drexel.edu> 10 1.2 matt * 11 1.2 matt * Based on epclk.c 12 1.2 matt * Copyright (c) 2004 Jesse Off 13 1.2 matt * All rights reserved. 14 1.2 matt * 15 1.2 matt * Redistribution and use in source and binary forms, with or without 16 1.2 matt * modification, are permitted provided that the following conditions 17 1.2 matt * are met: 18 1.2 matt * 1. Redistributions of source code must retain the above copyright 19 1.2 matt * notice, this list of conditions and the following disclaimer. 20 1.2 matt * 2. Redistributions in binary form must reproduce the above copyright 21 1.2 matt * notice, this list of conditions and the following disclaimer in the 22 1.2 matt * documentation and/or other materials provided with the distribution. 23 1.2 matt * 24 1.2 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 25 1.2 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 26 1.2 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 27 1.2 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 28 1.2 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 1.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 1.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 1.2 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 1.2 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 1.2 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 1.2 matt * POSSIBILITY OF SUCH DAMAGE. 35 1.2 matt */ 36 1.2 matt 37 1.2 matt /* 38 1.2 matt * Driver for the AT91RM9200 clock tick. 39 1.2 matt * We use Timer 1 for the system clock 40 1.2 matt */ 41 1.2 matt 42 1.2 matt #include <sys/cdefs.h> 43 1.9 maxv __KERNEL_RCSID(0, "$NetBSD: at91tctmr.c,v 1.9 2020/07/03 16:23:02 maxv Exp $"); 44 1.2 matt 45 1.2 matt #include <sys/types.h> 46 1.2 matt #include <sys/param.h> 47 1.2 matt #include <sys/systm.h> 48 1.2 matt #include <sys/kernel.h> 49 1.2 matt #include <sys/time.h> 50 1.2 matt #include <sys/timetc.h> 51 1.2 matt #include <sys/device.h> 52 1.2 matt 53 1.2 matt #include <dev/clock_subr.h> 54 1.2 matt 55 1.5 dyoung #include <sys/bus.h> 56 1.2 matt #include <machine/intr.h> 57 1.2 matt 58 1.2 matt #include <arm/cpufunc.h> 59 1.2 matt #include <arm/at91/at91reg.h> 60 1.2 matt #include <arm/at91/at91var.h> 61 1.2 matt #include <arm/at91/at91tcreg.h> 62 1.2 matt 63 1.2 matt #include <opt_hz.h> /* for HZ */ 64 1.2 matt 65 1.2 matt 66 1.2 matt #define DEBUG_CLK 67 1.2 matt #ifdef DEBUG_CLK 68 1.2 matt #define DPRINTF(fmt...) printf(fmt) 69 1.2 matt #else 70 1.2 matt #define DPRINTF(fmt...) 71 1.2 matt #endif 72 1.2 matt 73 1.2 matt 74 1.2 matt static int at91tctmr_match(device_t, cfdata_t, void *); 75 1.2 matt static void at91tctmr_attach(device_t, device_t, void *); 76 1.2 matt 77 1.2 matt void rtcinit(void); 78 1.2 matt 79 1.2 matt /* callback functions for intr_functions */ 80 1.2 matt static int at91tctmr_intr(void* arg); 81 1.2 matt 82 1.2 matt struct at91tctmr_softc { 83 1.2 matt device_t sc_dev; 84 1.2 matt u_char *sc_addr; 85 1.2 matt int sc_pid; 86 1.2 matt int sc_initialized; 87 1.2 matt uint32_t sc_timerclock; 88 1.2 matt uint32_t sc_divider; 89 1.2 matt uint32_t sc_usec_per_tick; 90 1.2 matt }; 91 1.2 matt 92 1.2 matt static struct at91tctmr_softc *at91tctmr_sc = NULL; 93 1.6 aymeric #if 0 94 1.2 matt static struct timeval lasttv; 95 1.6 aymeric #endif 96 1.2 matt 97 1.2 matt 98 1.2 matt 99 1.2 matt /* Match value for clock timer; running at master clock, want HZ ticks per second */ 100 1.2 matt /* NOTE: don't change there without visiting the functions below which */ 101 1.2 matt /* convert between timer counts and microseconds */ 102 1.2 matt 103 1.2 matt static inline uint32_t 104 1.2 matt at91tctmr_count_to_usec(struct at91tctmr_softc *sc, uint32_t count) 105 1.2 matt { 106 1.2 matt uint64_t tmp; 107 1.2 matt 108 1.2 matt tmp = count; 109 1.2 matt tmp *= 1000000U; 110 1.2 matt 111 1.2 matt return (tmp / sc->sc_timerclock); 112 1.2 matt } 113 1.2 matt 114 1.2 matt #if 0 115 1.2 matt /* This may only be called when overflow is avoided; typically, */ 116 1.2 matt /* it will be used when usec < USEC_PER_TICK */ 117 1.2 matt static uint32_t 118 1.2 matt usec_to_timer_count(uint32_t usec) 119 1.2 matt { 120 1.2 matt uint32_t result; 121 1.2 matt 122 1.2 matt /* convert specified number of usec to timer ticks, and round up */ 123 1.2 matt result = (AT91_SCLK * usec) / 1000000; 124 1.2 matt 125 1.2 matt if ((result * 1000000) != (usec * AT91_SCLK)) 126 1.2 matt { 127 1.2 matt /* round up */ 128 1.2 matt result += 1; 129 1.2 matt } 130 1.2 matt 131 1.2 matt return result; 132 1.2 matt 133 1.2 matt } 134 1.2 matt #endif 135 1.2 matt 136 1.2 matt /* macros to simplify writing to the timer controller */ 137 1.7 skrll static inline uint32_t 138 1.2 matt READ_TC(struct at91tctmr_softc *sc, uint offset) 139 1.2 matt { 140 1.7 skrll volatile uint32_t *addr = (void*)(sc->sc_addr + offset); 141 1.2 matt return *addr; 142 1.2 matt } 143 1.2 matt 144 1.2 matt //bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset) 145 1.2 matt static inline void 146 1.7 skrll WRITE_TC(struct at91tctmr_softc *sc, uint offset, uint32_t value) 147 1.2 matt { 148 1.7 skrll volatile uint32_t *addr = (void*)(sc->sc_addr + offset); 149 1.2 matt *addr = value; 150 1.2 matt } 151 1.2 matt 152 1.2 matt 153 1.2 matt CFATTACH_DECL_NEW(at91tctmr, sizeof(struct at91tctmr_softc), 154 1.2 matt at91tctmr_match, at91tctmr_attach, NULL, NULL); 155 1.2 matt 156 1.6 aymeric #if 0 157 1.2 matt static u_int at91tctmr_get_timecount(struct timecounter *); 158 1.2 matt 159 1.2 matt static struct timecounter at91tctmr_timecounter = { 160 1.8 rin .tc_get_timecount = at91tctmr_get_timecount, 161 1.8 rin .tc_counter_mask = 0xffffffff, 162 1.8 rin .tc_frequency = COUNTS_PER_SEC, 163 1.8 rin .tc_name = "at91tctmr", 164 1.8 rin .tc_quality = 100, 165 1.2 matt }; 166 1.6 aymeric #endif 167 1.2 matt 168 1.2 matt static int 169 1.2 matt at91tctmr_match(device_t parent, cfdata_t match, void *aux) 170 1.2 matt { 171 1.2 matt if (strcmp(match->cf_name, "at91tctmr") == 0) 172 1.2 matt return 2; 173 1.2 matt return 0; 174 1.2 matt } 175 1.2 matt 176 1.2 matt static void 177 1.2 matt at91tctmr_attach(device_t parent, device_t self, void *aux) 178 1.2 matt { 179 1.2 matt struct at91tctmr_softc *sc = device_private(self); 180 1.2 matt struct at91bus_attach_args *sa = aux; 181 1.2 matt 182 1.2 matt aprint_normal("\n"); 183 1.2 matt 184 1.2 matt sc->sc_dev = self; 185 1.2 matt sc->sc_addr = (void*)sa->sa_addr; 186 1.2 matt sc->sc_pid = sa->sa_pid; 187 1.2 matt 188 1.2 matt if (at91tctmr_sc == NULL) 189 1.2 matt at91tctmr_sc = sc; 190 1.2 matt 191 1.2 matt at91_peripheral_clock(sc->sc_pid, 1); 192 1.2 matt 193 1.2 matt WRITE_TC(sc, TC_CCR, TC_CCR_CLKDIS); 194 1.2 matt WRITE_TC(sc, TC_IDR, -1); /* make sure interrupts are disabled */ 195 1.2 matt 196 1.2 matt /* find divider */ 197 1.7 skrll uint32_t cmr = 0; 198 1.2 matt if (AT91_MSTCLK / 2U / HZ <= 65536) { 199 1.2 matt sc->sc_timerclock = AT91_MSTCLK / 2U; 200 1.2 matt cmr = TC_CMR_TCCLKS_MCK_DIV_2; 201 1.2 matt } else if (AT91_MSTCLK / 8U / HZ <= 65536) { 202 1.2 matt sc->sc_timerclock = AT91_MSTCLK / 8U; 203 1.2 matt cmr = TC_CMR_TCCLKS_MCK_DIV_8; 204 1.2 matt } else if (AT91_MSTCLK / 32U / HZ <= 65536) { 205 1.2 matt sc->sc_timerclock = AT91_MSTCLK / 32U; 206 1.2 matt cmr = TC_CMR_TCCLKS_MCK_DIV_32; 207 1.2 matt } else if (AT91_MSTCLK / 128U / HZ <= 65536) { 208 1.2 matt sc->sc_timerclock = AT91_MSTCLK / 128U; 209 1.2 matt cmr = TC_CMR_TCCLKS_MCK_DIV_128; 210 1.2 matt } else 211 1.4 matt panic("%s: cannot setup timer to reach HZ", device_xname(sc->sc_dev)); 212 1.2 matt 213 1.2 matt sc->sc_divider = (sc->sc_timerclock + HZ - 1) / HZ; /* round up */ 214 1.2 matt sc->sc_usec_per_tick = 1000000UL / (sc->sc_timerclock / sc->sc_divider); 215 1.2 matt 216 1.2 matt WRITE_TC(sc, TC_CMR, TC_CMR_WAVE | cmr | TC_CMR_WAVSEL_UP_RC); 217 1.2 matt WRITE_TC(sc, TC_CCR, TC_CCR_CLKEN); 218 1.2 matt WRITE_TC(sc, TC_RC, sc->sc_divider - 1); 219 1.2 matt WRITE_TC(sc, TC_CCR, TC_CCR_SWTRG); 220 1.2 matt 221 1.2 matt sc->sc_initialized = 1; 222 1.2 matt 223 1.2 matt DPRINTF("%s: done, tclock=%"PRIu32" div=%"PRIu32" uspertick=%"PRIu32"\n", __FUNCTION__, sc->sc_timerclock, sc->sc_divider, sc->sc_usec_per_tick); 224 1.2 matt 225 1.2 matt } 226 1.2 matt 227 1.2 matt /* 228 1.2 matt * at91tctmr_intr: 229 1.2 matt * 230 1.2 matt *Handle the hardclock interrupt. 231 1.2 matt */ 232 1.2 matt static int 233 1.2 matt at91tctmr_intr(void *arg) 234 1.2 matt { 235 1.2 matt struct at91tctmr_softc *sc = arg; 236 1.2 matt 237 1.2 matt /* make sure it's the kernel timer that generated the interrupt */ 238 1.2 matt /* need to do this since the interrupt line is shared by the */ 239 1.2 matt /* other interval and PWM timers */ 240 1.2 matt if (READ_TC(sc, TC_SR) & TC_SR_CPCS) { 241 1.2 matt /* call the kernel timer handler */ 242 1.2 matt hardclock((struct clockframe*) arg); 243 1.2 matt return 1; 244 1.2 matt } else { 245 1.2 matt /* it's one of the other timers; just pass it on */ 246 1.2 matt return 0; 247 1.2 matt } 248 1.2 matt } 249 1.2 matt 250 1.2 matt /* 251 1.2 matt * setstatclockrate: 252 1.2 matt * 253 1.2 matt *Set the rate of the statistics clock. 254 1.2 matt * 255 1.2 matt *We assume that hz is either stathz or profhz, and that neither 256 1.2 matt *will change after being set by cpu_initclocks(). We could 257 1.2 matt *recalculate the intervals here, but that would be a pain. 258 1.2 matt */ 259 1.2 matt void 260 1.2 matt setstatclockrate(int hzz) 261 1.2 matt { 262 1.2 matt /* use hardclock */ 263 1.2 matt (void)hzz; 264 1.2 matt } 265 1.2 matt 266 1.2 matt /* 267 1.2 matt * cpu_initclocks: 268 1.2 matt * 269 1.2 matt *Initialize the clock and get it going. 270 1.2 matt */ 271 1.2 matt static void udelay(unsigned int usec); 272 1.2 matt 273 1.2 matt void 274 1.2 matt cpu_initclocks(void) 275 1.2 matt { 276 1.2 matt struct at91tctmr_softc *sc = at91tctmr_sc; 277 1.2 matt 278 1.2 matt if (!sc || !sc->sc_initialized) 279 1.2 matt panic("%s: driver has not been initialized! (sc=%p)", __FUNCTION__, sc); 280 1.2 matt 281 1.2 matt hz = sc->sc_timerclock / sc->sc_divider; 282 1.2 matt stathz = profhz = 0; 283 1.2 matt 284 1.2 matt /* set up and enable interval timer 1 as kernel timer, */ 285 1.2 matt /* using 32kHz clock source */ 286 1.2 matt 287 1.2 matt /* register interrupt handler */ 288 1.2 matt at91_intr_establish(sc->sc_pid, IPL_CLOCK, INTR_HIGH_LEVEL, at91tctmr_intr, sc); 289 1.2 matt 290 1.2 matt /* enable interrupts from timer */ 291 1.2 matt WRITE_TC(sc, TC_IER, TC_SR_CPCS); 292 1.2 matt } 293 1.2 matt 294 1.2 matt 295 1.2 matt 296 1.2 matt 297 1.2 matt static void udelay(unsigned int usec) 298 1.2 matt { 299 1.2 matt struct at91tctmr_softc *sc = at91tctmr_sc; 300 1.7 skrll uint32_t prev_cvr, cvr, divi = READ_TC(sc, TC_RC), diff; 301 1.2 matt int prev_ticks, ticks, ticks2; 302 1.2 matt unsigned footick = (sc->sc_timerclock * 64ULL / 1000000UL); 303 1.2 matt 304 1.2 matt if (usec > 0) { 305 1.9 maxv prev_ticks = getticks(); 306 1.2 matt __insn_barrier(); 307 1.2 matt prev_cvr = READ_TC(sc, TC_CV); 308 1.9 maxv ticks = getticks(); 309 1.2 matt __insn_barrier(); 310 1.2 matt if (ticks != prev_ticks) { 311 1.2 matt prev_cvr = READ_TC(sc, TC_CV); 312 1.2 matt prev_ticks = ticks; 313 1.2 matt } 314 1.2 matt for (;;) { 315 1.9 maxv ticks = getticks(); 316 1.2 matt __insn_barrier(); 317 1.2 matt cvr = READ_TC(sc, TC_CV); 318 1.9 maxv ticks2 = getticks(); 319 1.2 matt __insn_barrier(); 320 1.2 matt if (ticks2 != ticks) { 321 1.2 matt cvr = READ_TC(sc, TC_CV); 322 1.2 matt } 323 1.2 matt diff = (ticks2 - prev_ticks) * divi; 324 1.2 matt if (cvr < prev_cvr) { 325 1.2 matt if (!diff) 326 1.2 matt diff = divi; 327 1.2 matt diff -= prev_cvr - cvr; 328 1.2 matt } else 329 1.2 matt diff += cvr - prev_cvr; 330 1.2 matt diff = diff * 64 / footick; 331 1.2 matt if (diff) { 332 1.2 matt if (usec <= diff) 333 1.2 matt break; 334 1.2 matt prev_ticks = ticks2; 335 1.2 matt prev_cvr = (prev_cvr + footick * diff / 64) % divi; 336 1.2 matt usec -= diff; 337 1.2 matt } 338 1.2 matt } 339 1.2 matt } 340 1.2 matt } 341 1.2 matt 342 1.2 matt 343 1.2 matt 344 1.2 matt /* 345 1.2 matt * delay: 346 1.2 matt * 347 1.2 matt *Delay for at least N microseconds. Note that due to our coarse clock, 348 1.2 matt * our resolution is 61 us. But we round up so we'll wait at least as 349 1.2 matt * long as requested. 350 1.2 matt */ 351 1.2 matt void 352 1.2 matt delay(unsigned int usec) 353 1.2 matt { 354 1.2 matt struct at91tctmr_softc *sc = at91tctmr_sc; 355 1.2 matt 356 1.2 matt #ifdef DEBUG 357 1.2 matt if (sc == NULL) { 358 1.2 matt printf("delay: called before start at91tc\n"); 359 1.2 matt return; 360 1.2 matt } 361 1.2 matt #endif 362 1.2 matt 363 1.2 matt if (usec >= sc->sc_usec_per_tick) { 364 1.2 matt /* have more than 1 tick; just do in ticks */ 365 1.2 matt unsigned int ticks = (usec + sc->sc_usec_per_tick - 1) / sc->sc_usec_per_tick; 366 1.2 matt while (ticks-- > 0) { 367 1.2 matt udelay(sc->sc_usec_per_tick); 368 1.2 matt } 369 1.2 matt } else { 370 1.2 matt /* less than 1 tick; can do as usec */ 371 1.2 matt udelay(usec); 372 1.2 matt } 373 1.2 matt 374 1.2 matt } 375 1.2 matt 376