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