1*7ed56e8cSmrg /* $NetBSD: sbtimer.c,v 1.21 2017/07/24 09:56:46 mrg Exp $ */
21b5ddfe4Ssimonb
31b5ddfe4Ssimonb /*
41b5ddfe4Ssimonb * Copyright 2000, 2001
51b5ddfe4Ssimonb * Broadcom Corporation. All rights reserved.
61b5ddfe4Ssimonb *
71b5ddfe4Ssimonb * This software is furnished under license and may be used and copied only
81b5ddfe4Ssimonb * in accordance with the following terms and conditions. Subject to these
91b5ddfe4Ssimonb * conditions, you may download, copy, install, use, modify and distribute
101b5ddfe4Ssimonb * modified or unmodified copies of this software in source and/or binary
111b5ddfe4Ssimonb * form. No title or ownership is transferred hereby.
121b5ddfe4Ssimonb *
131b5ddfe4Ssimonb * 1) Any source code used, modified or distributed must reproduce and
141b5ddfe4Ssimonb * retain this copyright notice and list of conditions as they appear in
151b5ddfe4Ssimonb * the source file.
161b5ddfe4Ssimonb *
171b5ddfe4Ssimonb * 2) No right is granted to use any trade name, trademark, or logo of
188a6b8c3bScgd * Broadcom Corporation. The "Broadcom Corporation" name may not be
198a6b8c3bScgd * used to endorse or promote products derived from this software
208a6b8c3bScgd * without the prior written permission of Broadcom Corporation.
211b5ddfe4Ssimonb *
221b5ddfe4Ssimonb * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED
231b5ddfe4Ssimonb * WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
241b5ddfe4Ssimonb * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
251b5ddfe4Ssimonb * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE
261b5ddfe4Ssimonb * FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE
271b5ddfe4Ssimonb * LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
281b5ddfe4Ssimonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
291b5ddfe4Ssimonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
301b5ddfe4Ssimonb * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
311b5ddfe4Ssimonb * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
321b5ddfe4Ssimonb * OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
331b5ddfe4Ssimonb */
341b5ddfe4Ssimonb
354b2744bfSlukem #include <sys/cdefs.h>
36*7ed56e8cSmrg __KERNEL_RCSID(0, "$NetBSD: sbtimer.c,v 1.21 2017/07/24 09:56:46 mrg Exp $");
374b2744bfSlukem
381b5ddfe4Ssimonb #include <sys/param.h>
391b5ddfe4Ssimonb #include <sys/device.h>
401b5ddfe4Ssimonb #include <sys/systm.h>
411b5ddfe4Ssimonb #include <sys/kernel.h>
42a04ed687Stsutsui #include <sys/cpu.h>
431b5ddfe4Ssimonb
441b5ddfe4Ssimonb #include <mips/locore.h>
451b5ddfe4Ssimonb
461b5ddfe4Ssimonb #include <mips/sibyte/include/sb1250_regs.h>
471b5ddfe4Ssimonb #include <mips/sibyte/include/sb1250_scd.h>
481b5ddfe4Ssimonb #include <mips/sibyte/dev/sbscdvar.h>
491b5ddfe4Ssimonb
50*7ed56e8cSmrg #include <evbmips/sbmips/systemsw.h>
51*7ed56e8cSmrg
521b5ddfe4Ssimonb struct sbtimer_softc {
53916ee5b6Smatt device_t sc_dev;
541b5ddfe4Ssimonb void *sc_intrhand;
551b5ddfe4Ssimonb int sc_flags;
561b5ddfe4Ssimonb void *sc_addr_icnt, *sc_addr_cnt, *sc_addr_cfg;
571b5ddfe4Ssimonb };
581b5ddfe4Ssimonb #define SBTIMER_CLOCK 1
591b5ddfe4Ssimonb #define SBTIMER_STATCLOCK 2
601b5ddfe4Ssimonb
6167c90d24Schristos #define READ_REG(rp) mips3_ld((register_t)(rp))
6267c90d24Schristos #define WRITE_REG(rp, val) mips3_sd((register_t)(rp), (val))
631b5ddfe4Ssimonb
64916ee5b6Smatt static int sbtimer_match(device_t, cfdata_t, void *);
65916ee5b6Smatt static void sbtimer_attach(device_t, device_t, void *);
661b5ddfe4Ssimonb
67916ee5b6Smatt CFATTACH_DECL_NEW(sbtimer, sizeof(struct sbtimer_softc),
6889bf5a8fSthorpej sbtimer_match, sbtimer_attach, NULL, NULL);
691b5ddfe4Ssimonb
70290a34a0Smatt static void sbtimer_clockintr(void *arg, uint32_t status, vaddr_t pc);
71290a34a0Smatt static void sbtimer_statclockintr(void *arg, uint32_t status, vaddr_t pc);
72290a34a0Smatt static void sbtimer_miscintr(void *arg, uint32_t status, vaddr_t pc);
731b5ddfe4Ssimonb
741b5ddfe4Ssimonb static void sbtimer_clock_init(void *arg);
751b5ddfe4Ssimonb
761b5ddfe4Ssimonb static int
sbtimer_match(device_t parent,cfdata_t match,void * aux)77916ee5b6Smatt sbtimer_match(device_t parent, cfdata_t match, void *aux)
781b5ddfe4Ssimonb {
791b5ddfe4Ssimonb struct sbscd_attach_args *sap = aux;
801b5ddfe4Ssimonb
811b5ddfe4Ssimonb if (sap->sa_locs.sa_type != SBSCD_DEVTYPE_TIMER)
821b5ddfe4Ssimonb return (0);
831b5ddfe4Ssimonb
841b5ddfe4Ssimonb return 1;
851b5ddfe4Ssimonb }
861b5ddfe4Ssimonb
871b5ddfe4Ssimonb static void
sbtimer_attach(device_t parent,device_t self,void * aux)88916ee5b6Smatt sbtimer_attach(device_t parent, device_t self, void *aux)
891b5ddfe4Ssimonb {
901b5ddfe4Ssimonb struct sbscd_attach_args *sa = aux;
91916ee5b6Smatt struct sbtimer_softc *sc = device_private(self);
92290a34a0Smatt void (*fun)(void *, uint32_t, vaddr_t);
931b5ddfe4Ssimonb int ipl;
941b5ddfe4Ssimonb const char *comment = "";
951b5ddfe4Ssimonb
96916ee5b6Smatt sc->sc_dev = self;
97916ee5b6Smatt
98916ee5b6Smatt sc->sc_flags = device_cfdata(sc->sc_dev)->cf_flags;
996f35b2ecSmatt sc->sc_addr_icnt = (uint64_t *)MIPS_PHYS_TO_KSEG1(sa->sa_base +
1006f35b2ecSmatt sa->sa_locs.sa_offset + R_SCD_TIMER_INIT);
1016f35b2ecSmatt sc->sc_addr_cnt = (uint64_t *)MIPS_PHYS_TO_KSEG1(sa->sa_base +
1026f35b2ecSmatt sa->sa_locs.sa_offset + R_SCD_TIMER_CNT);
1036f35b2ecSmatt sc->sc_addr_cfg = (uint64_t *)MIPS_PHYS_TO_KSEG1(sa->sa_base +
1046f35b2ecSmatt sa->sa_locs.sa_offset + R_SCD_TIMER_CFG);
1051b5ddfe4Ssimonb
10682bf97dcSmatt aprint_normal(": ");
1071b5ddfe4Ssimonb if ((sc->sc_flags & SBTIMER_CLOCK) != 0) {
1081b5ddfe4Ssimonb ipl = IPL_CLOCK;
1091b5ddfe4Ssimonb fun = sbtimer_clockintr;
1101b5ddfe4Ssimonb
1111b5ddfe4Ssimonb if (system_set_clockfns(sc, sbtimer_clock_init)) {
1121b5ddfe4Ssimonb /* not really the clock */
1131b5ddfe4Ssimonb sc->sc_flags &= ~SBTIMER_CLOCK;
1141b5ddfe4Ssimonb comment = " (not system timer)";
1151b5ddfe4Ssimonb goto not_really;
1161b5ddfe4Ssimonb }
11782bf97dcSmatt aprint_normal("system timer");
1181b5ddfe4Ssimonb } else if ((sc->sc_flags & SBTIMER_STATCLOCK) != 0) {
119afa1151eSad ipl = IPL_HIGH;
1201b5ddfe4Ssimonb fun = sbtimer_statclockintr;
1211b5ddfe4Ssimonb
1221b5ddfe4Ssimonb /* XXX make sure it's the statclock */
1231b5ddfe4Ssimonb if (1) {
1241b5ddfe4Ssimonb /* not really the statclock */
1251b5ddfe4Ssimonb sc->sc_flags &= ~SBTIMER_STATCLOCK;
1261b5ddfe4Ssimonb comment = " (not system statistics timer)";
1271b5ddfe4Ssimonb goto not_really;
1281b5ddfe4Ssimonb }
12982bf97dcSmatt aprint_normal("system statistics timer");
1301b5ddfe4Ssimonb } else {
1311b5ddfe4Ssimonb not_really:
1321b5ddfe4Ssimonb ipl = IPL_BIO; /* XXX -- pretty low */
1331b5ddfe4Ssimonb fun = sbtimer_miscintr;
13482bf97dcSmatt aprint_normal("general-purpose timer%s", comment);
1351b5ddfe4Ssimonb }
13682bf97dcSmatt aprint_normal("\n");
1371b5ddfe4Ssimonb
1381b5ddfe4Ssimonb /* clear intr & disable timer. */
1391b5ddfe4Ssimonb WRITE_REG(sc->sc_addr_cfg, 0x00); /* XXX */
1401b5ddfe4Ssimonb
1411b5ddfe4Ssimonb sc->sc_intrhand = cpu_intr_establish(sa->sa_locs.sa_intr[0], ipl,
1421b5ddfe4Ssimonb fun, sc);
1431b5ddfe4Ssimonb }
1441b5ddfe4Ssimonb
1451b5ddfe4Ssimonb static void
sbtimer_clock_init(void * arg)1461b5ddfe4Ssimonb sbtimer_clock_init(void *arg)
1471b5ddfe4Ssimonb {
1481b5ddfe4Ssimonb struct sbtimer_softc *sc = arg;
1491b5ddfe4Ssimonb
15082bf97dcSmatt if ((1000000 % hz) == 0) {
15182bf97dcSmatt aprint_normal_dev(sc->sc_dev, "%dHz system timer\n", hz);
15282bf97dcSmatt } else {
15382bf97dcSmatt aprint_error_dev(sc->sc_dev,
15482bf97dcSmatt "cannot get %dHz clock; using 1000Hz\n", hz);
1551b5ddfe4Ssimonb hz = 1000;
1561b5ddfe4Ssimonb tick = 1000000 / hz;
1571b5ddfe4Ssimonb }
1581b5ddfe4Ssimonb
1591b5ddfe4Ssimonb WRITE_REG(sc->sc_addr_cfg, 0x00); /* XXX */
1601b5ddfe4Ssimonb if (G_SYS_PLL_DIV(READ_REG(MIPS_PHYS_TO_KSEG1(A_SCD_SYSTEM_CFG))) == 0) {
16182bf97dcSmatt aprint_debug_dev(sc->sc_dev,
16282bf97dcSmatt "PLL_DIV == 0; speeding up clock ticks for simulator\n");
1631b5ddfe4Ssimonb WRITE_REG(sc->sc_addr_icnt, (tick/100) - 1); /* XXX */
1641b5ddfe4Ssimonb } else {
1651b5ddfe4Ssimonb WRITE_REG(sc->sc_addr_icnt, tick - 1); /* XXX */
1661b5ddfe4Ssimonb }
1671b5ddfe4Ssimonb WRITE_REG(sc->sc_addr_cfg, 0x03); /* XXX */
1681b5ddfe4Ssimonb }
1691b5ddfe4Ssimonb
1701b5ddfe4Ssimonb static void
sbtimer_clockintr(void * arg,uint32_t status,vaddr_t pc)171290a34a0Smatt sbtimer_clockintr(void *arg, uint32_t status, vaddr_t pc)
1721b5ddfe4Ssimonb {
1731b5ddfe4Ssimonb struct sbtimer_softc *sc = arg;
1741b5ddfe4Ssimonb struct clockframe cf;
1751b5ddfe4Ssimonb
1761b5ddfe4Ssimonb /* clear interrupt, but leave timer enabled and in repeating mode */
1771b5ddfe4Ssimonb WRITE_REG(sc->sc_addr_cfg, 0x03); /* XXX */
1781b5ddfe4Ssimonb
1791b5ddfe4Ssimonb cf.pc = pc;
1801b5ddfe4Ssimonb cf.sr = status;
181a04ed687Stsutsui cf.intr = (curcpu()->ci_idepth > 1);
1821b5ddfe4Ssimonb
1831b5ddfe4Ssimonb hardclock(&cf);
184c4bf5468Ssimonb
185c4bf5468Ssimonb /*
186c4bf5468Ssimonb * We never want a CPU core clock interrupt, so adjust the CP0
187c4bf5468Ssimonb * compare register to just before the CP0 clock register's value
188c4bf5468Ssimonb * each time.
189c4bf5468Ssimonb */
190c4bf5468Ssimonb mips3_cp0_compare_write(mips3_cp0_count_read() - 1);
1911b5ddfe4Ssimonb }
1921b5ddfe4Ssimonb
1931b5ddfe4Ssimonb static void
sbtimer_statclockintr(void * arg,uint32_t status,vaddr_t pc)194290a34a0Smatt sbtimer_statclockintr(void *arg, uint32_t status, vaddr_t pc)
1951b5ddfe4Ssimonb {
1961b5ddfe4Ssimonb struct sbtimer_softc *sc = arg;
1971b5ddfe4Ssimonb struct clockframe cf;
1981b5ddfe4Ssimonb
1991b5ddfe4Ssimonb /* clear intr & disable timer, reset initial count, re-enable timer */
2001b5ddfe4Ssimonb WRITE_REG(sc->sc_addr_cfg, 0x00); /* XXX */
2011b5ddfe4Ssimonb /* XXX more to do */
2021b5ddfe4Ssimonb
2031b5ddfe4Ssimonb cf.pc = pc;
2041b5ddfe4Ssimonb cf.sr = status;
2051b5ddfe4Ssimonb
2061b5ddfe4Ssimonb statclock(&cf);
2071b5ddfe4Ssimonb }
2081b5ddfe4Ssimonb
2091b5ddfe4Ssimonb static void
sbtimer_miscintr(void * arg,uint32_t status,vaddr_t pc)210290a34a0Smatt sbtimer_miscintr(void *arg, uint32_t status, vaddr_t pc)
2111b5ddfe4Ssimonb {
2121b5ddfe4Ssimonb struct sbtimer_softc *sc = arg;
2131b5ddfe4Ssimonb
2141b5ddfe4Ssimonb /* disable timer */
2151b5ddfe4Ssimonb WRITE_REG(sc->sc_addr_cfg, 0x00); /* XXX */
2161b5ddfe4Ssimonb
2171b5ddfe4Ssimonb /* XXX more to do */
2181b5ddfe4Ssimonb }
219