1 1.20 rin /* $NetBSD: ixp425_timer.c,v 1.20 2020/05/29 12:30:39 rin Exp $ */ 2 1.1 ichiro 3 1.1 ichiro /* 4 1.1 ichiro * Copyright (c) 2003 5 1.1 ichiro * Ichiro FUKUHARA <ichiro (at) ichiro.org>. 6 1.1 ichiro * All rights reserved. 7 1.1 ichiro * 8 1.1 ichiro * Redistribution and use in source and binary forms, with or without 9 1.1 ichiro * modification, are permitted provided that the following conditions 10 1.1 ichiro * are met: 11 1.1 ichiro * 1. Redistributions of source code must retain the above copyright 12 1.1 ichiro * notice, this list of conditions and the following disclaimer. 13 1.1 ichiro * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 ichiro * notice, this list of conditions and the following disclaimer in the 15 1.1 ichiro * documentation and/or other materials provided with the distribution. 16 1.1 ichiro * 17 1.1 ichiro * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR 18 1.1 ichiro * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 1.1 ichiro * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 1.1 ichiro * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR 21 1.1 ichiro * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 1.1 ichiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 1.1 ichiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 1.1 ichiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 1.1 ichiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 1.1 ichiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 1.1 ichiro * SUCH DAMAGE. 28 1.1 ichiro */ 29 1.1 ichiro 30 1.1 ichiro #include <sys/cdefs.h> 31 1.20 rin __KERNEL_RCSID(0, "$NetBSD: ixp425_timer.c,v 1.20 2020/05/29 12:30:39 rin Exp $"); 32 1.1 ichiro 33 1.10 scw #include "opt_ixp425.h" 34 1.1 ichiro 35 1.1 ichiro #include <sys/types.h> 36 1.1 ichiro #include <sys/param.h> 37 1.1 ichiro #include <sys/systm.h> 38 1.1 ichiro #include <sys/kernel.h> 39 1.14 joerg #include <sys/atomic.h> 40 1.1 ichiro #include <sys/time.h> 41 1.14 joerg #include <sys/timetc.h> 42 1.1 ichiro #include <sys/device.h> 43 1.1 ichiro 44 1.2 thorpej #include <dev/clock_subr.h> 45 1.2 thorpej 46 1.16 dyoung #include <sys/bus.h> 47 1.1 ichiro #include <machine/intr.h> 48 1.1 ichiro 49 1.1 ichiro #include <arm/cpufunc.h> 50 1.1 ichiro 51 1.1 ichiro #include <arm/xscale/ixp425reg.h> 52 1.1 ichiro #include <arm/xscale/ixp425var.h> 53 1.1 ichiro #include <arm/xscale/ixp425_sipvar.h> 54 1.1 ichiro 55 1.17 msaitoh static int ixpclk_match(device_t, cfdata_t, void *); 56 1.17 msaitoh static void ixpclk_attach(device_t, device_t, void *); 57 1.14 joerg static u_int ixpclk_get_timecount(struct timecounter *); 58 1.1 ichiro 59 1.1 ichiro static uint32_t counts_per_hz; 60 1.1 ichiro 61 1.1 ichiro static void *clock_ih; 62 1.1 ichiro 63 1.1 ichiro /* callback functions for intr_functions */ 64 1.1 ichiro int ixpclk_intr(void *); 65 1.1 ichiro 66 1.1 ichiro struct ixpclk_softc { 67 1.11 simonb bus_addr_t sc_baseaddr; 68 1.11 simonb bus_space_tag_t sc_iot; 69 1.11 simonb bus_space_handle_t sc_ioh; 70 1.1 ichiro }; 71 1.1 ichiro 72 1.10 scw #ifndef IXP425_CLOCK_FREQ 73 1.4 scw #define COUNTS_PER_SEC 66666600 /* 66MHz */ 74 1.10 scw #else 75 1.10 scw #define COUNTS_PER_SEC IXP425_CLOCK_FREQ 76 1.10 scw #endif 77 1.4 scw #define COUNTS_PER_USEC ((COUNTS_PER_SEC / 1000000) + 1) 78 1.1 ichiro 79 1.4 scw static struct ixpclk_softc *ixpclk_sc; 80 1.1 ichiro 81 1.14 joerg static struct timecounter ixpclk_timecounter = { 82 1.20 rin .tc_get_timecount = ixpclk_get_timecount, 83 1.20 rin .tc_counter_mask = 0xffffffff, 84 1.20 rin .tc_frequency = COUNTS_PER_SEC, 85 1.20 rin .tc_name = "ixpclk", 86 1.20 rin .tc_quality = 100, 87 1.14 joerg }; 88 1.14 joerg 89 1.14 joerg static volatile uint32_t ixpclk_base; 90 1.14 joerg 91 1.17 msaitoh CFATTACH_DECL_NEW(ixpclk, sizeof(struct ixpclk_softc), 92 1.1 ichiro ixpclk_match, ixpclk_attach, NULL, NULL); 93 1.1 ichiro 94 1.1 ichiro #define GET_TIMER_VALUE(sc) (bus_space_read_4((sc)->sc_iot, \ 95 1.1 ichiro (sc)->sc_ioh, \ 96 1.1 ichiro IXP425_OST_TIM0)) 97 1.1 ichiro 98 1.18 skrll #define GET_TS_VALUE(sc) (*(volatile uint32_t *) \ 99 1.5 scw (IXP425_TIMER_VBASE + IXP425_OST_TS)) 100 1.5 scw 101 1.1 ichiro static int 102 1.17 msaitoh ixpclk_match(device_t parent, cfdata_t match, void *aux) 103 1.1 ichiro { 104 1.11 simonb return 2; 105 1.1 ichiro } 106 1.1 ichiro 107 1.1 ichiro static void 108 1.17 msaitoh ixpclk_attach(device_t parent, device_t self, void *aux) 109 1.1 ichiro { 110 1.17 msaitoh struct ixpclk_softc *sc = device_private(self); 111 1.1 ichiro struct ixpsip_attach_args *sa = aux; 112 1.1 ichiro 113 1.1 ichiro printf("\n"); 114 1.1 ichiro 115 1.5 scw ixpclk_sc = sc; 116 1.5 scw 117 1.1 ichiro sc->sc_iot = sa->sa_iot; 118 1.1 ichiro sc->sc_baseaddr = sa->sa_addr; 119 1.1 ichiro 120 1.1 ichiro if (bus_space_map(sc->sc_iot, sa->sa_addr, sa->sa_size, 0, 121 1.1 ichiro &sc->sc_ioh)) 122 1.17 msaitoh panic("%s: Cannot map registers", device_xname(self)); 123 1.1 ichiro 124 1.17 msaitoh aprint_normal_dev(self, "IXP425 Interval Timer\n"); 125 1.1 ichiro } 126 1.1 ichiro 127 1.1 ichiro /* 128 1.1 ichiro * cpu_initclocks: 129 1.1 ichiro * 130 1.1 ichiro * Initialize the clock and get them going. 131 1.1 ichiro */ 132 1.1 ichiro void 133 1.1 ichiro cpu_initclocks(void) 134 1.1 ichiro { 135 1.17 msaitoh struct ixpclk_softc *sc = ixpclk_sc; 136 1.1 ichiro u_int oldirqstate; 137 1.1 ichiro 138 1.1 ichiro if (hz < 50 || COUNTS_PER_SEC % hz) { 139 1.1 ichiro aprint_error("Cannot get %d Hz clock; using 100 Hz\n", hz); 140 1.1 ichiro hz = 100; 141 1.1 ichiro } 142 1.1 ichiro 143 1.1 ichiro /* 144 1.1 ichiro * We only have one timer available; stathz and profhz are 145 1.1 ichiro * always left as 0 (the upper-layer clock code deals with 146 1.1 ichiro * this situation). 147 1.1 ichiro */ 148 1.1 ichiro if (stathz != 0) 149 1.1 ichiro aprint_error("Cannot get %d Hz statclock\n", stathz); 150 1.1 ichiro stathz = 0; 151 1.1 ichiro 152 1.1 ichiro if (profhz != 0) 153 1.1 ichiro aprint_error("Cannot get %d Hz profclock\n", profhz); 154 1.1 ichiro profhz = 0; 155 1.1 ichiro 156 1.1 ichiro /* Report the clock frequency. */ 157 1.1 ichiro aprint_normal("clock: hz=%d stathz=%d profhz=%d\n", hz, stathz, profhz); 158 1.1 ichiro 159 1.1 ichiro oldirqstate = disable_interrupts(I32_bit); 160 1.1 ichiro 161 1.1 ichiro /* Hook up the clock interrupt handler. */ 162 1.1 ichiro clock_ih = ixp425_intr_establish(IXP425_INT_TMR0, IPL_CLOCK, 163 1.1 ichiro ixpclk_intr, NULL); 164 1.1 ichiro if (clock_ih == NULL) 165 1.1 ichiro panic("cpu_initclocks: unable to register timer interrupt"); 166 1.1 ichiro 167 1.1 ichiro /* Set up the new clock parameters. */ 168 1.1 ichiro 169 1.1 ichiro /* clear interrupt */ 170 1.1 ichiro bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_STATUS, 171 1.1 ichiro OST_WARM_RESET | OST_WDOG_INT | OST_TS_INT | 172 1.1 ichiro OST_TIM1_INT | OST_TIM0_INT); 173 1.1 ichiro 174 1.1 ichiro counts_per_hz = COUNTS_PER_SEC / hz; 175 1.1 ichiro 176 1.1 ichiro /* reload value & Timer enable */ 177 1.1 ichiro bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_TIM0_RELOAD, 178 1.1 ichiro (counts_per_hz & TIMERRELOAD_MASK) | OST_TIMER_EN); 179 1.1 ichiro 180 1.1 ichiro restore_interrupts(oldirqstate); 181 1.14 joerg 182 1.14 joerg tc_init(&ixpclk_timecounter); 183 1.1 ichiro } 184 1.1 ichiro 185 1.1 ichiro /* 186 1.1 ichiro * setstatclockrate: 187 1.1 ichiro * 188 1.1 ichiro * Set the rate of the statistics clock. 189 1.1 ichiro * 190 1.1 ichiro * We assume that hz is either stathz or profhz, and that neither 191 1.1 ichiro * will change after being set by cpu_initclocks(). We could 192 1.1 ichiro * recalculate the intervals here, but that would be a pain. 193 1.1 ichiro */ 194 1.1 ichiro void 195 1.8 he setstatclockrate(int newhz) 196 1.1 ichiro { 197 1.1 ichiro 198 1.1 ichiro /* 199 1.1 ichiro * XXX Use TMR1? 200 1.1 ichiro */ 201 1.1 ichiro } 202 1.1 ichiro 203 1.14 joerg static u_int 204 1.14 joerg ixpclk_get_timecount(struct timecounter *tc) 205 1.1 ichiro { 206 1.14 joerg u_int savedints, base, counter; 207 1.1 ichiro 208 1.14 joerg savedints = disable_interrupts(I32_bit); 209 1.14 joerg base = ixpclk_base; 210 1.14 joerg counter = GET_TIMER_VALUE(ixpclk_sc); 211 1.14 joerg restore_interrupts(savedints); 212 1.1 ichiro 213 1.14 joerg return base - counter; 214 1.1 ichiro } 215 1.1 ichiro 216 1.1 ichiro /* 217 1.1 ichiro * delay: 218 1.1 ichiro * 219 1.1 ichiro * Delay for at least N microseconds. 220 1.1 ichiro */ 221 1.1 ichiro void 222 1.1 ichiro delay(u_int n) 223 1.1 ichiro { 224 1.18 skrll uint32_t first, last; 225 1.5 scw int usecs; 226 1.5 scw 227 1.5 scw if (n == 0) 228 1.5 scw return; 229 1.1 ichiro 230 1.1 ichiro /* 231 1.5 scw * Clamp the timeout at a maximum value (about 32 seconds with 232 1.5 scw * a 66MHz clock). *Nobody* should be delay()ing for anywhere 233 1.5 scw * near that length of time and if they are, they should be hung 234 1.5 scw * out to dry. 235 1.1 ichiro */ 236 1.5 scw if (n >= (0x80000000U / COUNTS_PER_USEC)) 237 1.5 scw usecs = (0x80000000U / COUNTS_PER_USEC) - 1; 238 1.5 scw else 239 1.5 scw usecs = n * COUNTS_PER_USEC; 240 1.5 scw 241 1.5 scw /* Note: Timestamp timer counts *up*, unlike the other timers */ 242 1.5 scw first = GET_TS_VALUE(); 243 1.5 scw 244 1.5 scw while (usecs > 0) { 245 1.5 scw last = GET_TS_VALUE(); 246 1.5 scw usecs -= (int)(last - first); 247 1.5 scw first = last; 248 1.1 ichiro } 249 1.1 ichiro } 250 1.1 ichiro 251 1.1 ichiro /* 252 1.1 ichiro * ixpclk_intr: 253 1.1 ichiro * 254 1.1 ichiro * Handle the hardclock interrupt. 255 1.1 ichiro */ 256 1.1 ichiro int 257 1.1 ichiro ixpclk_intr(void *arg) 258 1.1 ichiro { 259 1.17 msaitoh struct ixpclk_softc *sc = ixpclk_sc; 260 1.1 ichiro struct clockframe *frame = arg; 261 1.1 ichiro 262 1.1 ichiro bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_STATUS, 263 1.1 ichiro OST_TIM0_INT); 264 1.1 ichiro 265 1.14 joerg atomic_add_32(&ixpclk_base, counts_per_hz); 266 1.14 joerg 267 1.1 ichiro hardclock(frame); 268 1.1 ichiro 269 1.1 ichiro return (1); 270 1.1 ichiro } 271