tx39clock.c revision 1.22.8.1       1  1.22.8.1     yamt /*	$NetBSD: tx39clock.c,v 1.22.8.1 2008/05/18 12:32:04 yamt Exp $ */
      2       1.1      uch 
      3       1.6      uch /*-
      4      1.11      uch  * Copyright (c) 1999-2002 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.1      uch  *
     19       1.6      uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.6      uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.6      uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.6      uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.6      uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.6      uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.6      uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.6      uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.6      uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.6      uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.6      uch  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1      uch  */
     31      1.14    lukem 
     32      1.14    lukem #include <sys/cdefs.h>
     33  1.22.8.1     yamt __KERNEL_RCSID(0, "$NetBSD: tx39clock.c,v 1.22.8.1 2008/05/18 12:32:04 yamt Exp $");
     34       1.6      uch 
     35      1.11      uch #include "opt_tx39clock_debug.h"
     36       1.1      uch 
     37       1.1      uch #include <sys/param.h>
     38       1.1      uch #include <sys/systm.h>
     39      1.21  gdamore #include <sys/timetc.h>
     40      1.22       ad #include <sys/device.h>
     41      1.22       ad #include <sys/bus.h>
     42       1.1      uch 
     43       1.3      uch #include <dev/clock_subr.h>
     44       1.3      uch 
     45      1.10      uch #include <machine/sysconf.h>
     46       1.1      uch 
     47       1.1      uch #include <hpcmips/tx/tx39var.h>
     48       1.3      uch #include <hpcmips/tx/tx39icureg.h>
     49       1.5      uch #include <hpcmips/tx/tx39clockvar.h>
     50       1.1      uch #include <hpcmips/tx/tx39clockreg.h>
     51       1.1      uch #include <hpcmips/tx/tx39timerreg.h>
     52       1.3      uch 
     53      1.11      uch #ifdef	TX39CLOCK_DEBUG
     54      1.11      uch #define DPRINTF_ENABLE
     55      1.11      uch #define DPRINTF_DEBUG	tx39clock_debug
     56       1.3      uch #endif
     57      1.11      uch #include <machine/debug.h>
     58       1.3      uch 
     59      1.11      uch #define ISSETPRINT(r, m)						\
     60      1.11      uch 	dbg_bitmask_print(r, TX39_CLOCK_EN ## m ## CLK, #m)
     61       1.1      uch 
     62       1.6      uch void	tx39clock_init(struct device *);
     63       1.3      uch 
     64      1.10      uch struct platform_clock tx39_clock = {
     65      1.10      uch #define CLOCK_RATE	100
     66      1.21  gdamore 	CLOCK_RATE, tx39clock_init,
     67       1.3      uch };
     68       1.1      uch 
     69       1.3      uch struct txtime {
     70       1.3      uch 	u_int32_t t_hi;
     71       1.3      uch 	u_int32_t t_lo;
     72       1.3      uch };
     73       1.3      uch 
     74       1.3      uch struct tx39clock_softc {
     75       1.3      uch 	struct	device sc_dev;
     76       1.3      uch 	tx_chipset_tag_t sc_tc;
     77       1.3      uch 
     78       1.5      uch 	int sc_alarm;
     79       1.3      uch 	int sc_enabled;
     80       1.3      uch 	int sc_year;
     81      1.10      uch 	struct clock_ymdhms sc_epoch;
     82      1.21  gdamore 	struct timecounter sc_tcounter;
     83       1.1      uch };
     84       1.1      uch 
     85       1.6      uch int	tx39clock_match(struct device *, struct cfdata *, void *);
     86       1.6      uch void	tx39clock_attach(struct device *, struct device *, void *);
     87      1.11      uch #ifdef TX39CLOCK_DEBUG
     88       1.6      uch void	tx39clock_dump(tx_chipset_tag_t);
     89      1.11      uch #endif
     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.19    perry inline void	__tx39timer_rtcget(struct txtime *);
     96      1.19    perry inline time_t __tx39timer_rtc2sec(struct txtime *);
     97      1.21  gdamore uint32_t tx39_timecount(struct timecounter *);
     98       1.1      uch 
     99      1.13  thorpej CFATTACH_DECL(tx39clock, sizeof(struct tx39clock_softc),
    100      1.13  thorpej     tx39clock_match, tx39clock_attach, NULL, NULL);
    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.10      uch 
    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.10      uch 	platform_clock_attach(self, &tx39_clock);
    137       1.1      uch 
    138      1.11      uch #ifdef TX39CLOCK_DEBUG
    139       1.1      uch 	tx39clock_dump(tc);
    140      1.11      uch #endif /* TX39CLOCK_DEBUG */
    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.17       he tx39clock_cpuspeed(int *cpuclock, int *cpu_speed)
    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.19    perry 	__asm volatile(
    155      1.15   simonb 		".set	noreorder;		\n\t"
    156      1.15   simonb 		"li	$8, 10000000;		\n"
    157      1.15   simonb 	"1:	nop;				\n\t"
    158      1.15   simonb 		"nop;				\n\t"
    159      1.15   simonb 		"nop;				\n\t"
    160      1.15   simonb 		"nop;				\n\t"
    161      1.15   simonb 		"nop;				\n\t"
    162      1.15   simonb 		"nop;				\n\t"
    163      1.15   simonb 		"nop;				\n\t"
    164      1.15   simonb 		"add	$8, $8, -1;		\n\t"
    165      1.15   simonb 		"bnez	$8, 1b;			\n\t"
    166      1.15   simonb 		"nop;				\n\t"
    167      1.15   simonb 		".set	reorder;");
    168       1.3      uch 	__tx39timer_rtcget(&t1);
    169       1.3      uch 
    170       1.3      uch 	elapsed = t1.t_lo - t0.t_lo;
    171       1.3      uch 
    172       1.3      uch 	*cpuclock = (100000000 / elapsed) * TX39_RTCLOCK;
    173      1.17       he 	*cpu_speed = *cpuclock / 1000000;
    174       1.3      uch }
    175       1.3      uch 
    176       1.3      uch void
    177       1.6      uch __tx39timer_rtcfreeze(tx_chipset_tag_t tc)
    178       1.1      uch {
    179       1.1      uch 	txreg_t reg;
    180       1.1      uch 
    181       1.1      uch 	reg = tx_conf_read(tc, TX39_TIMERCONTROL_REG);
    182       1.3      uch 
    183       1.1      uch 	/* Freeze RTC */
    184       1.1      uch 	reg |= TX39_TIMERCONTROL_FREEZEPRE; /* Upper 8bit */
    185       1.1      uch 	reg |= TX39_TIMERCONTROL_FREEZERTC; /* Lower 32bit */
    186       1.3      uch 
    187       1.1      uch 	/* Freeze periodic timer */
    188       1.1      uch 	reg |= TX39_TIMERCONTROL_FREEZETIMER;
    189       1.1      uch 	reg &= ~TX39_TIMERCONTROL_ENPERTIMER;
    190       1.1      uch 	tx_conf_write(tc, TX39_TIMERCONTROL_REG, reg);
    191       1.1      uch }
    192       1.1      uch 
    193      1.19    perry inline void
    194       1.6      uch __tx39timer_rtcget(struct txtime *t)
    195       1.3      uch {
    196       1.3      uch 	tx_chipset_tag_t tc;
    197       1.3      uch 	txreg_t reghi, reglo, oreghi, oreglo;
    198       1.3      uch 	int retry;
    199       1.3      uch 
    200       1.3      uch 	tc = tx_conf_get_tag();
    201       1.3      uch 
    202       1.3      uch 	retry = 10;
    203       1.3      uch 
    204       1.3      uch 	do {
    205       1.3      uch 		oreglo = tx_conf_read(tc, TX39_TIMERRTCLO_REG);
    206       1.3      uch 		reglo = tx_conf_read(tc, TX39_TIMERRTCLO_REG);
    207       1.3      uch 
    208       1.3      uch 		oreghi = tx_conf_read(tc, TX39_TIMERRTCHI_REG);
    209       1.3      uch 		reghi = tx_conf_read(tc, TX39_TIMERRTCHI_REG);
    210       1.3      uch 	} while ((reghi != oreghi || reglo != oreglo) && (--retry > 0));
    211       1.3      uch 
    212       1.3      uch 	if (retry < 0) {
    213       1.3      uch 		printf("RTC timer read error.\n");
    214       1.3      uch 	}
    215       1.3      uch 
    216       1.3      uch 	t->t_hi = TX39_TIMERRTCHI(reghi);
    217       1.3      uch 	t->t_lo = reglo;
    218       1.3      uch }
    219       1.3      uch 
    220       1.1      uch void
    221       1.6      uch __tx39timer_rtcreset(tx_chipset_tag_t tc)
    222       1.1      uch {
    223       1.1      uch 	txreg_t reg;
    224       1.1      uch 
    225       1.1      uch 	reg = tx_conf_read(tc, TX39_TIMERCONTROL_REG);
    226       1.3      uch 
    227       1.1      uch 	/* Reset counter and stop */
    228       1.1      uch 	reg |= TX39_TIMERCONTROL_RTCCLR;
    229       1.1      uch 	tx_conf_write(tc, TX39_TIMERCONTROL_REG, reg);
    230       1.3      uch 
    231       1.1      uch 	/* Count again */
    232       1.1      uch 	reg &= ~TX39_TIMERCONTROL_RTCCLR;
    233       1.1      uch 	tx_conf_write(tc, TX39_TIMERCONTROL_REG, reg);
    234       1.1      uch }
    235       1.1      uch 
    236      1.21  gdamore uint32_t
    237      1.21  gdamore tx39_timecount(struct timecounter *tch)
    238      1.21  gdamore {
    239      1.21  gdamore 	tx_chipset_tag_t tc = tch->tc_priv;
    240      1.21  gdamore 
    241      1.21  gdamore 	/*
    242      1.21  gdamore 	 * since we're only reading the low register, we don't care about
    243      1.21  gdamore 	 * if the chip increments it.  we assume that the single read will
    244      1.21  gdamore 	 * always be consistent.  This is much faster than the routine which
    245      1.21  gdamore 	 * has to get both values, improving the quality.
    246      1.21  gdamore 	 */
    247      1.21  gdamore 	return (tx_conf_read(tc, TX39_TIMERRTCLO_REG));
    248      1.21  gdamore }
    249      1.21  gdamore 
    250       1.1      uch void
    251       1.6      uch tx39clock_init(struct device *dev)
    252       1.1      uch {
    253       1.5      uch 	struct tx39clock_softc *sc = (void*)dev;
    254       1.5      uch 	tx_chipset_tag_t tc = sc->sc_tc;
    255       1.1      uch 	txreg_t reg;
    256       1.3      uch 	int pcnt;
    257       1.1      uch 
    258       1.3      uch 	/*
    259       1.3      uch 	 * Setup periodic timer (interrupting hz times per second.)
    260       1.3      uch 	 */
    261      1.10      uch 	pcnt = TX39_TIMERCLK / CLOCK_RATE - 1;
    262       1.3      uch 	reg = tx_conf_read(tc, TX39_TIMERPERIODIC_REG);
    263       1.3      uch 	TX39_TIMERPERIODIC_PERVAL_CLR(reg);
    264       1.3      uch 	reg = TX39_TIMERPERIODIC_PERVAL_SET(reg, pcnt);
    265       1.3      uch 	tx_conf_write(tc, TX39_TIMERPERIODIC_REG, reg);
    266       1.3      uch 
    267       1.3      uch 	/*
    268       1.3      uch 	 * Enable periodic timer
    269       1.3      uch 	 */
    270       1.1      uch 	reg = tx_conf_read(tc, TX39_INTRENABLE6_REG);
    271       1.1      uch 	reg |= TX39_INTRPRI13_TIMER_PERIODIC_BIT;
    272       1.1      uch 	tx_conf_write(tc, TX39_INTRENABLE6_REG, reg);
    273       1.1      uch 
    274      1.21  gdamore 	sc->sc_tcounter.tc_name = "tx39rtc";
    275      1.21  gdamore 	sc->sc_tcounter.tc_get_timecount = tx39_timecount;
    276      1.21  gdamore 	sc->sc_tcounter.tc_priv = tc;
    277      1.21  gdamore 	sc->sc_tcounter.tc_counter_mask = 0xffffffff;
    278      1.21  gdamore 	sc->sc_tcounter.tc_frequency = TX39_RTCLOCK;
    279      1.21  gdamore 	sc->sc_tcounter.tc_quality = 100;
    280      1.21  gdamore 	tc_init(&sc->sc_tcounter);
    281       1.5      uch }
    282       1.5      uch 
    283       1.5      uch int
    284       1.6      uch tx39clock_alarm_set(tx_chipset_tag_t tc, int msec)
    285       1.5      uch {
    286       1.5      uch 	struct tx39clock_softc *sc = tc->tc_clockt;
    287       1.5      uch 
    288       1.5      uch 	sc->sc_alarm = TX39_MSEC2RTC(msec);
    289       1.5      uch 	tx39clock_alarm_refill(tc);
    290       1.5      uch 
    291       1.9      uch 	return (0);
    292       1.5      uch }
    293       1.5      uch 
    294       1.5      uch void
    295       1.6      uch tx39clock_alarm_refill(tx_chipset_tag_t tc)
    296       1.5      uch {
    297       1.5      uch 	struct tx39clock_softc *sc = tc->tc_clockt;
    298       1.5      uch 	struct txtime t;
    299      1.20  gdamore 	u_int64_t mytime;
    300       1.5      uch 
    301       1.5      uch 	__tx39timer_rtcget(&t);
    302       1.5      uch 
    303      1.20  gdamore 	mytime = ((u_int64_t)t.t_hi << 32) | (u_int64_t)t.t_lo;
    304      1.20  gdamore 	mytime += (u_int64_t)sc->sc_alarm;
    305       1.6      uch 
    306      1.20  gdamore 	t.t_hi = (u_int32_t)((mytime >> 32) & TX39_TIMERALARMHI_MASK);
    307      1.20  gdamore 	t.t_lo = (u_int32_t)(mytime & 0xffffffff);
    308       1.5      uch 
    309       1.6      uch 	tx_conf_write(tc, TX39_TIMERALARMHI_REG, t.t_hi);
    310       1.6      uch 	tx_conf_write(tc, TX39_TIMERALARMLO_REG, t.t_lo);
    311       1.1      uch }
    312       1.1      uch 
    313      1.11      uch #ifdef TX39CLOCK_DEBUG
    314       1.1      uch void
    315       1.6      uch tx39clock_dump(tx_chipset_tag_t tc)
    316       1.1      uch {
    317       1.1      uch 	txreg_t reg;
    318       1.1      uch 
    319       1.1      uch 	reg = tx_conf_read(tc, TX39_CLOCKCTRL_REG);
    320       1.3      uch 
    321       1.1      uch 	printf(" ");
    322       1.1      uch 	ISSETPRINT(reg, CHIM);
    323       1.1      uch #ifdef TX391X
    324       1.1      uch 	ISSETPRINT(reg, VID);
    325       1.1      uch 	ISSETPRINT(reg, MBUS);
    326       1.1      uch #endif /* TX391X */
    327       1.1      uch #ifdef TX392X
    328       1.1      uch 	ISSETPRINT(reg, IRDA);
    329       1.1      uch #endif /* TX392X */
    330       1.1      uch 	ISSETPRINT(reg, SPI);
    331       1.1      uch 	ISSETPRINT(reg, TIMER);
    332       1.1      uch 	ISSETPRINT(reg, FASTTIMER);
    333       1.1      uch #ifdef TX392X
    334       1.1      uch 	ISSETPRINT(reg, C48MOUT);
    335       1.1      uch #endif /* TX392X */
    336       1.1      uch 	ISSETPRINT(reg, SIBM);
    337       1.1      uch 	ISSETPRINT(reg, CSER);
    338       1.1      uch 	ISSETPRINT(reg, IR);
    339       1.1      uch 	ISSETPRINT(reg, UARTA);
    340       1.1      uch 	ISSETPRINT(reg, UARTB);
    341       1.1      uch 	printf("\n");
    342       1.1      uch }
    343      1.11      uch #endif /* TX39CLOCK_DEBUG */
    344