Home | History | Annotate | Line # | Download | only in tx
tx39clock.c revision 1.5
      1  1.5  uch /*	$NetBSD: tx39clock.c,v 1.5 2000/01/03 18:24:04 uch Exp $ */
      2  1.1  uch 
      3  1.1  uch /*
      4  1.5  uch  * Copyright (c) 1999, 2000 by UCHIYAMA Yasushi
      5  1.1  uch  * All rights reserved.
      6  1.1  uch  *
      7  1.1  uch  * Redistribution and use in source and binary forms, with or without
      8  1.1  uch  * modification, are permitted provided that the following conditions
      9  1.1  uch  * are met:
     10  1.1  uch  * 1. Redistributions of source code must retain the above copyright
     11  1.1  uch  *    notice, this list of conditions and the following disclaimer.
     12  1.1  uch  * 2. The name of the developer may NOT be used to endorse or promote products
     13  1.1  uch  *    derived from this software without specific prior written permission.
     14  1.1  uch  *
     15  1.1  uch  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  1.1  uch  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  1.1  uch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  1.1  uch  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1  uch  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1  uch  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  1.1  uch  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  uch  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1  uch  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  uch  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  uch  * SUCH DAMAGE.
     26  1.1  uch  *
     27  1.1  uch  */
     28  1.1  uch #include "opt_tx39_debug.h"
     29  1.1  uch 
     30  1.1  uch #include <sys/param.h>
     31  1.3  uch #include <sys/kernel.h>
     32  1.1  uch #include <sys/systm.h>
     33  1.1  uch #include <sys/device.h>
     34  1.1  uch 
     35  1.3  uch #include <dev/clock_subr.h>
     36  1.3  uch 
     37  1.1  uch #include <machine/bus.h>
     38  1.1  uch #include <machine/clock_machdep.h>
     39  1.1  uch #include <machine/cpu.h>
     40  1.1  uch 
     41  1.1  uch #include <hpcmips/tx/tx39var.h>
     42  1.3  uch #include <hpcmips/tx/tx39icureg.h>
     43  1.5  uch #include <hpcmips/tx/tx39clockvar.h>
     44  1.1  uch #include <hpcmips/tx/tx39clockreg.h>
     45  1.1  uch #include <hpcmips/tx/tx39timerreg.h>
     46  1.3  uch 
     47  1.1  uch #include <dev/dec/clockvar.h>
     48  1.1  uch 
     49  1.3  uch #ifdef TX39CLKDEBUG
     50  1.3  uch #define	DPRINTF(arg) printf arg
     51  1.3  uch #else
     52  1.3  uch #define	DPRINTF(arg)
     53  1.3  uch #endif
     54  1.3  uch 
     55  1.1  uch #define ISSETPRINT(r, m) __is_set_print(r, TX39_CLOCK_EN##m##CLK, #m)
     56  1.1  uch 
     57  1.3  uch void	tx39clock_init __P((struct device*));
     58  1.3  uch void	tx39clock_get __P((struct device*, time_t, struct clocktime*));
     59  1.3  uch void	tx39clock_set __P((struct device*, struct clocktime*));
     60  1.3  uch 
     61  1.3  uch const struct clockfns tx39clockfns = {
     62  1.3  uch 	tx39clock_init, tx39clock_get, tx39clock_set,
     63  1.3  uch };
     64  1.1  uch 
     65  1.3  uch struct txtime {
     66  1.3  uch 	u_int32_t t_hi;
     67  1.3  uch 	u_int32_t t_lo;
     68  1.3  uch };
     69  1.3  uch 
     70  1.3  uch struct tx39clock_softc {
     71  1.3  uch 	struct	device sc_dev;
     72  1.3  uch 	tx_chipset_tag_t sc_tc;
     73  1.3  uch 
     74  1.5  uch 	int sc_alarm;
     75  1.5  uch 
     76  1.3  uch 	int sc_enabled;
     77  1.3  uch 	int sc_year;
     78  1.3  uch 	struct clocktime sc_epoch;
     79  1.1  uch };
     80  1.1  uch 
     81  1.1  uch int	tx39clock_match __P((struct device*, struct cfdata*, void*));
     82  1.1  uch void	tx39clock_attach __P((struct device*, struct device*, void*));
     83  1.1  uch void	tx39clock_dump __P((tx_chipset_tag_t));
     84  1.1  uch 
     85  1.3  uch void	tx39clock_cpuspeed __P((int*, int*));
     86  1.1  uch 
     87  1.3  uch void	__tx39timer_rtcfreeze __P((tx_chipset_tag_t));
     88  1.3  uch void	__tx39timer_rtcreset __P((tx_chipset_tag_t));
     89  1.3  uch __inline void	__tx39timer_rtcget __P((struct txtime*));
     90  1.3  uch __inline time_t	__tx39timer_rtc2sec __P((struct txtime*));
     91  1.1  uch 
     92  1.1  uch struct cfattach tx39clock_ca = {
     93  1.1  uch 	sizeof(struct tx39clock_softc), tx39clock_match, tx39clock_attach
     94  1.1  uch };
     95  1.1  uch 
     96  1.1  uch int
     97  1.1  uch tx39clock_match(parent, cf, aux)
     98  1.1  uch 	struct device *parent;
     99  1.1  uch 	struct cfdata *cf;
    100  1.1  uch 	void *aux;
    101  1.1  uch {
    102  1.1  uch 	return 2; /* 1st attach group of txsim */
    103  1.1  uch }
    104  1.1  uch 
    105  1.1  uch void
    106  1.1  uch tx39clock_attach(parent, self, aux)
    107  1.1  uch 	struct device *parent;
    108  1.1  uch 	struct device *self;
    109  1.1  uch 	void *aux;
    110  1.1  uch {
    111  1.1  uch 	struct txsim_attach_args *ta = aux;
    112  1.1  uch 	struct tx39clock_softc *sc = (void*)self;
    113  1.1  uch 	tx_chipset_tag_t tc;
    114  1.1  uch 	txreg_t reg;
    115  1.1  uch 
    116  1.1  uch 	tc = sc->sc_tc = ta->ta_tc;
    117  1.5  uch 	tx_conf_register_clock(tc, self);
    118  1.1  uch 
    119  1.3  uch 	/* Reset timer module */
    120  1.3  uch 	tx_conf_write(tc, TX39_TIMERCONTROL_REG, 0);
    121  1.3  uch 
    122  1.3  uch 	/* Enable periodic timer */
    123  1.1  uch 	reg = tx_conf_read(tc, TX39_TIMERCONTROL_REG);
    124  1.1  uch 	reg |= TX39_TIMERCONTROL_ENPERTIMER;
    125  1.1  uch 	tx_conf_write(tc, TX39_TIMERCONTROL_REG, reg);
    126  1.3  uch 
    127  1.3  uch 	sc->sc_enabled = 0;
    128  1.3  uch 	/*
    129  1.3  uch 	 * RTC and ALARM
    130  1.3  uch 	 *    RTCINT    ... INTR5 bit 31  (roll over)
    131  1.3  uch 	 *    ALARMINT  ... INTR5 bit 30
    132  1.3  uch 	 *    PERINT    ... INTR5 bit 29
    133  1.3  uch 	 */
    134  1.3  uch 
    135  1.3  uch 	clockattach(self, &tx39clockfns);
    136  1.1  uch 
    137  1.2  uch #ifdef TX39CLKDEBUG
    138  1.1  uch 	tx39clock_dump(tc);
    139  1.2  uch #endif /* TX39CLKDEBUG */
    140  1.1  uch }
    141  1.1  uch 
    142  1.3  uch /*
    143  1.3  uch  * cpuclock ... CPU clock (Hz)
    144  1.3  uch  * cpuspeed ... instructions-per-microsecond
    145  1.1  uch  */
    146  1.1  uch void
    147  1.3  uch tx39clock_cpuspeed(cpuclock, cpuspeed)
    148  1.3  uch 	int *cpuclock;
    149  1.3  uch 	int *cpuspeed;
    150  1.3  uch {
    151  1.3  uch 	struct txtime t0, t1;
    152  1.3  uch 	int elapsed;
    153  1.3  uch 
    154  1.3  uch 	__tx39timer_rtcget(&t0);
    155  1.3  uch 	__asm __volatile("
    156  1.3  uch 		.set	noreorder;
    157  1.3  uch 		li	$8, 10000000;
    158  1.3  uch 	1:	nop;
    159  1.3  uch 		nop;
    160  1.3  uch 		nop;
    161  1.3  uch 		nop;
    162  1.3  uch 		nop;
    163  1.3  uch 		nop;
    164  1.3  uch 		nop;
    165  1.3  uch 		add	$8, $8, -1;
    166  1.3  uch 		bnez	$8, 1b;
    167  1.3  uch 		nop;
    168  1.3  uch 		.set	reorder;
    169  1.3  uch 	");
    170  1.3  uch 	__tx39timer_rtcget(&t1);
    171  1.3  uch 
    172  1.3  uch 	elapsed = t1.t_lo - t0.t_lo;
    173  1.3  uch 
    174  1.3  uch 	*cpuclock = (100000000 / elapsed) * TX39_RTCLOCK;
    175  1.3  uch 	*cpuspeed = *cpuclock / 1000000;
    176  1.3  uch 
    177  1.3  uch }
    178  1.3  uch 
    179  1.3  uch void
    180  1.3  uch __tx39timer_rtcfreeze(tc)
    181  1.1  uch 	tx_chipset_tag_t tc;
    182  1.1  uch {
    183  1.1  uch 	txreg_t reg;
    184  1.1  uch 
    185  1.1  uch 	reg = tx_conf_read(tc, TX39_TIMERCONTROL_REG);
    186  1.3  uch 
    187  1.1  uch 	/* Freeze RTC */
    188  1.1  uch 	reg |= TX39_TIMERCONTROL_FREEZEPRE; /* Upper 8bit */
    189  1.1  uch 	reg |= TX39_TIMERCONTROL_FREEZERTC; /* Lower 32bit */
    190  1.3  uch 
    191  1.1  uch 	/* Freeze periodic timer */
    192  1.1  uch 	reg |= TX39_TIMERCONTROL_FREEZETIMER;
    193  1.1  uch 	reg &= ~TX39_TIMERCONTROL_ENPERTIMER;
    194  1.1  uch 	tx_conf_write(tc, TX39_TIMERCONTROL_REG, reg);
    195  1.1  uch }
    196  1.1  uch 
    197  1.3  uch __inline time_t
    198  1.3  uch __tx39timer_rtc2sec(t)
    199  1.3  uch 	struct txtime *t;
    200  1.3  uch {
    201  1.3  uch 	/* This rely on RTC is 32.768kHz */
    202  1.3  uch 	return (t->t_lo >> 15) | (t->t_hi << 17);
    203  1.3  uch }
    204  1.3  uch 
    205  1.3  uch __inline void
    206  1.3  uch __tx39timer_rtcget(t)
    207  1.3  uch 	struct txtime *t;
    208  1.3  uch {
    209  1.3  uch 	tx_chipset_tag_t tc;
    210  1.3  uch 	txreg_t reghi, reglo, oreghi, oreglo;
    211  1.3  uch 	int retry;
    212  1.3  uch 
    213  1.3  uch 	tc = tx_conf_get_tag();
    214  1.3  uch 
    215  1.3  uch 	retry = 10;
    216  1.3  uch 
    217  1.3  uch 	do {
    218  1.3  uch 		oreglo = tx_conf_read(tc, TX39_TIMERRTCLO_REG);
    219  1.3  uch 		reglo = tx_conf_read(tc, TX39_TIMERRTCLO_REG);
    220  1.3  uch 
    221  1.3  uch 		oreghi = tx_conf_read(tc, TX39_TIMERRTCHI_REG);
    222  1.3  uch 		reghi = tx_conf_read(tc, TX39_TIMERRTCHI_REG);
    223  1.3  uch 	} while ((reghi != oreghi || reglo != oreglo) && (--retry > 0));
    224  1.3  uch 
    225  1.3  uch 	if (retry < 0) {
    226  1.3  uch 		printf("RTC timer read error.\n");
    227  1.3  uch 	}
    228  1.3  uch 
    229  1.3  uch 	t->t_hi = TX39_TIMERRTCHI(reghi);
    230  1.3  uch 	t->t_lo = reglo;
    231  1.3  uch }
    232  1.3  uch 
    233  1.1  uch void
    234  1.3  uch __tx39timer_rtcreset(tc)
    235  1.1  uch  	tx_chipset_tag_t tc;
    236  1.1  uch {
    237  1.1  uch 	txreg_t reg;
    238  1.1  uch 
    239  1.1  uch 	reg = tx_conf_read(tc, TX39_TIMERCONTROL_REG);
    240  1.3  uch 
    241  1.1  uch 	/* Reset counter and stop */
    242  1.1  uch 	reg |= TX39_TIMERCONTROL_RTCCLR;
    243  1.1  uch 	tx_conf_write(tc, TX39_TIMERCONTROL_REG, reg);
    244  1.3  uch 
    245  1.1  uch 	/* Count again */
    246  1.1  uch 	reg &= ~TX39_TIMERCONTROL_RTCCLR;
    247  1.1  uch 	tx_conf_write(tc, TX39_TIMERCONTROL_REG, reg);
    248  1.1  uch }
    249  1.1  uch 
    250  1.5  uch 
    251  1.1  uch void
    252  1.3  uch tx39clock_init(dev)
    253  1.1  uch 	struct device *dev;
    254  1.1  uch {
    255  1.5  uch 	struct tx39clock_softc *sc = (void*)dev;
    256  1.5  uch 	tx_chipset_tag_t tc = sc->sc_tc;
    257  1.1  uch 	txreg_t reg;
    258  1.3  uch 	int pcnt;
    259  1.1  uch 
    260  1.3  uch 	/*
    261  1.3  uch 	 * Setup periodic timer (interrupting hz times per second.)
    262  1.3  uch 	 */
    263  1.3  uch 	pcnt = TX39_TIMERCLK / hz - 1;
    264  1.3  uch 	reg = tx_conf_read(tc, TX39_TIMERPERIODIC_REG);
    265  1.3  uch 	TX39_TIMERPERIODIC_PERVAL_CLR(reg);
    266  1.3  uch 	reg = TX39_TIMERPERIODIC_PERVAL_SET(reg, pcnt);
    267  1.3  uch 	tx_conf_write(tc, TX39_TIMERPERIODIC_REG, reg);
    268  1.3  uch 
    269  1.3  uch 	/*
    270  1.3  uch 	 * Enable periodic timer
    271  1.3  uch 	 */
    272  1.1  uch 	reg = tx_conf_read(tc, TX39_INTRENABLE6_REG);
    273  1.1  uch 	reg |= TX39_INTRPRI13_TIMER_PERIODIC_BIT;
    274  1.1  uch 	tx_conf_write(tc, TX39_INTRENABLE6_REG, reg);
    275  1.1  uch 
    276  1.3  uch 	/*
    277  1.3  uch 	 * number of microseconds between interrupts
    278  1.3  uch 	 */
    279  1.3  uch 	tick = 1000000 / hz;
    280  1.1  uch }
    281  1.1  uch 
    282  1.1  uch void
    283  1.3  uch tx39clock_get(dev, base, ct)
    284  1.1  uch 	struct device *dev;
    285  1.1  uch 	time_t base;
    286  1.1  uch 	struct clocktime *ct;
    287  1.1  uch {
    288  1.3  uch 	struct clock_ymdhms dt;
    289  1.3  uch 	struct tx39clock_softc *sc = (void*)dev;
    290  1.3  uch 	struct txtime tt;
    291  1.3  uch 	time_t sec;
    292  1.3  uch 
    293  1.3  uch 	__tx39timer_rtcget(&tt);
    294  1.3  uch 	sec = __tx39timer_rtc2sec(&tt);
    295  1.3  uch 
    296  1.3  uch 	if (!sc->sc_enabled) {
    297  1.3  uch 		DPRINTF(("bootstrap: %d sec from previous reboot\n",
    298  1.3  uch 			 (int)sec));
    299  1.3  uch 
    300  1.3  uch 		sc->sc_enabled = 1;
    301  1.3  uch 		base += sec;
    302  1.3  uch 	} else {
    303  1.3  uch 		dt.dt_year = sc->sc_year;
    304  1.3  uch 		dt.dt_mon = sc->sc_epoch.mon;
    305  1.3  uch 		dt.dt_day = sc->sc_epoch.day;
    306  1.3  uch 		dt.dt_hour = sc->sc_epoch.hour;
    307  1.3  uch 		dt.dt_min = sc->sc_epoch.min;
    308  1.3  uch 		dt.dt_sec = sc->sc_epoch.sec;
    309  1.3  uch 		dt.dt_wday = sc->sc_epoch.dow;
    310  1.4  uch 		base = sec + clock_ymdhms_to_secs(&dt);
    311  1.3  uch 	}
    312  1.3  uch 
    313  1.3  uch 	clock_secs_to_ymdhms(base, &dt);
    314  1.3  uch 
    315  1.3  uch 	ct->year = dt.dt_year % 100;
    316  1.3  uch 	ct->mon = dt.dt_mon;
    317  1.3  uch 	ct->day = dt.dt_day;
    318  1.3  uch 	ct->hour = dt.dt_hour;
    319  1.3  uch 	ct->min = dt.dt_min;
    320  1.3  uch 	ct->sec = dt.dt_sec;
    321  1.3  uch 	ct->dow = dt.dt_wday;
    322  1.1  uch 
    323  1.3  uch 	sc->sc_year = dt.dt_year;
    324  1.1  uch }
    325  1.1  uch 
    326  1.1  uch void
    327  1.3  uch tx39clock_set(dev, ct)
    328  1.1  uch 	struct device *dev;
    329  1.1  uch 	struct clocktime *ct;
    330  1.1  uch {
    331  1.3  uch 	struct tx39clock_softc *sc = (void*)dev;
    332  1.3  uch 
    333  1.3  uch 	if (sc->sc_enabled) {
    334  1.3  uch 		sc->sc_epoch = *ct;
    335  1.3  uch 		/* Reset RTC counter */
    336  1.3  uch 		__tx39timer_rtcreset(sc->sc_tc);
    337  1.3  uch 	}
    338  1.5  uch }
    339  1.5  uch 
    340  1.5  uch int
    341  1.5  uch tx39clock_alarm_set(tc, msec)
    342  1.5  uch 	tx_chipset_tag_t tc;
    343  1.5  uch 	int msec;
    344  1.5  uch {
    345  1.5  uch 	struct tx39clock_softc *sc = tc->tc_clockt;
    346  1.5  uch 
    347  1.5  uch 	sc->sc_alarm = TX39_MSEC2RTC(msec);
    348  1.5  uch 	tx39clock_alarm_refill(tc);
    349  1.5  uch 
    350  1.5  uch 	return 0;
    351  1.5  uch }
    352  1.5  uch 
    353  1.5  uch void
    354  1.5  uch tx39clock_alarm_refill(tc)
    355  1.5  uch 	tx_chipset_tag_t tc;
    356  1.5  uch {
    357  1.5  uch 	struct tx39clock_softc *sc = tc->tc_clockt;
    358  1.5  uch 	struct txtime t;
    359  1.5  uch 
    360  1.5  uch 	__tx39timer_rtcget(&t);
    361  1.5  uch 
    362  1.5  uch 	tx_conf_write(tc, TX39_TIMERALARMHI_REG, t.t_hi); /* XXX */
    363  1.5  uch 	tx_conf_write(tc, TX39_TIMERALARMLO_REG, t.t_lo + sc->sc_alarm);
    364  1.5  uch 
    365  1.1  uch }
    366  1.1  uch 
    367  1.1  uch void
    368  1.1  uch tx39clock_dump(tc)
    369  1.1  uch 	tx_chipset_tag_t tc;
    370  1.1  uch {
    371  1.1  uch 	txreg_t reg;
    372  1.1  uch 
    373  1.1  uch 	reg = tx_conf_read(tc, TX39_CLOCKCTRL_REG);
    374  1.3  uch 
    375  1.1  uch 	printf(" ");
    376  1.1  uch 	ISSETPRINT(reg, CHIM);
    377  1.1  uch #ifdef TX391X
    378  1.1  uch 	ISSETPRINT(reg, VID);
    379  1.1  uch 	ISSETPRINT(reg, MBUS);
    380  1.1  uch #endif /* TX391X */
    381  1.1  uch #ifdef TX392X
    382  1.1  uch 	ISSETPRINT(reg, IRDA);
    383  1.1  uch #endif /* TX392X */
    384  1.1  uch 	ISSETPRINT(reg, SPI);
    385  1.1  uch 	ISSETPRINT(reg, TIMER);
    386  1.1  uch 	ISSETPRINT(reg, FASTTIMER);
    387  1.1  uch #ifdef TX392X
    388  1.1  uch 	ISSETPRINT(reg, C48MOUT);
    389  1.1  uch #endif /* TX392X */
    390  1.1  uch 	ISSETPRINT(reg, SIBM);
    391  1.1  uch 	ISSETPRINT(reg, CSER);
    392  1.1  uch 	ISSETPRINT(reg, IR);
    393  1.1  uch 	ISSETPRINT(reg, UARTA);
    394  1.1  uch 	ISSETPRINT(reg, UARTB);
    395  1.1  uch 	printf("\n");
    396  1.1  uch }
    397