Home | History | Annotate | Line # | Download | only in vr
rtc.c revision 1.8
      1 /*	$NetBSD: rtc.c,v 1.8 2001/09/18 17:37:29 uch Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 Shin Takemura. All rights reserved.
      5  * Copyright (c) 1999 SATO Kazumi. All rights reserved.
      6  * Copyright (c) 1999 PocketBSD Project. All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the PocketBSD project
     19  *	and its contributors.
     20  * 4. Neither the name of the project nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  */
     37 
     38 #include "opt_vr41xx.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 
     43 #include <machine/sysconf.h>
     44 #include <machine/bus.h>
     45 
     46 #include <dev/clock_subr.h>
     47 
     48 #include <hpcmips/vr/vr.h>
     49 #include <hpcmips/vr/vrcpudef.h>
     50 #include <hpcmips/vr/vripvar.h>
     51 #include <hpcmips/vr/rtcreg.h>
     52 
     53 /*
     54  * for debugging definitions
     55  * 	VRRTCDEBUG	print rtc debugging information
     56  *	VRRTC_HEARTBEAT	print HEARTBEAT (too many print...)
     57  */
     58 #ifdef VRRTCDEBUG
     59 #ifndef VRRTCDEBUG_CONF
     60 #define VRRTCDEBUG_CONF 0
     61 #endif
     62 int vrrtc_debug = VRRTCDEBUG_CONF;
     63 #define DPRINTF(arg) if (vrrtc_debug) printf arg;
     64 #define DDUMP_REGS(arg) if (vrrtc_debug) vrrtc_dump_regs(arg);
     65 #else /* VRRTCDEBUG */
     66 #define DPRINTF(arg)
     67 #define DDUMP_REGS(arg)
     68 #endif /* VRRTCDEBUG */
     69 
     70 struct vrrtc_softc {
     71 	struct device sc_dev;
     72 	bus_space_tag_t sc_iot;
     73 	bus_space_handle_t sc_ioh;
     74 	void *sc_ih;
     75 };
     76 
     77 void	clock_init(struct device *);
     78 void	clock_get(struct device *, time_t, struct clock_ymdhms *);
     79 void	clock_set(struct device *, struct clock_ymdhms *);
     80 
     81 struct platform_clock vr_clock = {
     82 #define CLOCK_RATE	128
     83 	CLOCK_RATE, clock_init, clock_get, clock_set,
     84 };
     85 
     86 int	vrrtc_match(struct device *, struct cfdata *, void *);
     87 void	vrrtc_attach(struct device *, struct device *, void *);
     88 int	vrrtc_intr(void*, u_int32_t, u_int32_t);
     89 void	vrrtc_dump_regs(struct vrrtc_softc *);
     90 
     91 struct cfattach vrrtc_ca = {
     92 	sizeof(struct vrrtc_softc), vrrtc_match, vrrtc_attach
     93 };
     94 
     95 void	vrrtc_write(struct vrrtc_softc *, int, u_int16_t);
     96 u_int16_t vrrtc_read(struct vrrtc_softc *, int);
     97 void	cvt_timehl_ymdhms(u_int32_t, u_int32_t, struct clock_ymdhms *);
     98 
     99 extern int rtc_offset;
    100 static int m2d[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    101 
    102 static __inline__ void
    103 vrrtc_write(struct vrrtc_softc *sc, int port, u_int16_t val)
    104 {
    105 
    106 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
    107 }
    108 
    109 static __inline__ u_int16_t
    110 vrrtc_read(struct vrrtc_softc *sc, int port)
    111 {
    112 
    113 	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, port));
    114 }
    115 
    116 int
    117 vrrtc_match(struct device *parent, struct cfdata *cf, void *aux)
    118 {
    119 
    120 	return (1);
    121 }
    122 
    123 void
    124 vrrtc_attach(struct device *parent, struct device *self, void *aux)
    125 {
    126 	struct vrip_attach_args *va = aux;
    127 	struct vrrtc_softc *sc = (void *)self;
    128 
    129 	sc->sc_iot = va->va_iot;
    130 	if (bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
    131 	    0 /* no flags */, &sc->sc_ioh)) {
    132 		printf("vrrtc_attach: can't map i/o space\n");
    133 		return;
    134 	}
    135 	/* RTC interrupt handler is directly dispatched from CPU intr */
    136 	vr_intr_establish(VR_INTR1, vrrtc_intr, sc);
    137 	/* But need to set level 1 interupt mask register,
    138 	 * so regsiter fake interrurpt handler
    139 	 */
    140 	if (!(sc->sc_ih = vrip_intr_establish(va->va_vc, va->va_intr,
    141 	    IPL_CLOCK, 0, 0))) {
    142 		printf (":can't map interrupt.\n");
    143 		return;
    144 	}
    145 	/*
    146 	 *  Rtc is attached to call this routine
    147 	 *  before cpu_initclock() calls clock_init().
    148 	 *  So we must disable all interrupt for now.
    149 	 */
    150 	/*
    151 	 * Disable all rtc interrupts
    152 	 */
    153 	/* Disable Elapse compare intr */
    154 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECMP_H_REG_W, 0);
    155 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECMP_M_REG_W, 0);
    156 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECMP_L_REG_W, 0);
    157 	/* Disable RTC Long1 intr */
    158 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL1_H_REG_W, 0);
    159 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL1_L_REG_W, 0);
    160 	/* Disable RTC Long2 intr */
    161 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL2_H_REG_W, 0);
    162 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL2_L_REG_W, 0);
    163 	/* Disable RTC TCLK intr */
    164 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, TCLK_H_REG_W, 0);
    165 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, TCLK_L_REG_W, 0);
    166 	/*
    167 	 * Clear all rtc intrrupts.
    168 	 */
    169 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCINT_REG_W, RTCINT_ALL);
    170 
    171 	platform_clock_attach(sc, &vr_clock);
    172 }
    173 
    174 int
    175 vrrtc_intr(void *arg, u_int32_t pc, u_int32_t statusReg)
    176 {
    177 	struct vrrtc_softc *sc = arg;
    178 	struct clockframe cf;
    179 
    180 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCINT_REG_W, RTCINT_ALL);
    181 	cf.pc = pc;
    182 	cf.sr = statusReg;
    183 	hardclock(&cf);
    184 	intrcnt[HARDCLOCK]++;
    185 
    186 #ifdef VRRTC_HEARTBEAT
    187 	if ((intrcnt[HARDCLOCK] % (CLOCK_RATE * 5)) == 0) {
    188 		struct clocktime ct;
    189 		clock_get((struct device *)sc, NULL, &ct);
    190 		printf("%s(%d): rtc_intr: %2d.%2d.%2d %02d:%02d:%02d\n",
    191 		    __FILE__, __LINE__,
    192 		    ct.year, ct.mon, ct.day,
    193 		    ct.hour, ct.min, ct.sec);
    194 	}
    195 #endif
    196 	return 0;
    197 }
    198 
    199 void
    200 clock_init(struct device *dev)
    201 {
    202 	struct vrrtc_softc *sc = (struct vrrtc_softc *)dev;
    203 
    204 	DDUMP_REGS(sc);
    205 	/*
    206 	 * Set tick (CLOCK_RATE)
    207 	 */
    208 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL1_H_REG_W, 0);
    209 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, RTCL1_L_REG_W,
    210 	    RTCL1_L_HZ/CLOCK_RATE);
    211 }
    212 
    213 void
    214 clock_get(struct device *dev, time_t base, struct clock_ymdhms *dt)
    215 {
    216 
    217 	struct vrrtc_softc *sc = (struct vrrtc_softc *)dev;
    218 	bus_space_tag_t iot = sc->sc_iot;
    219 	bus_space_handle_t ioh = sc->sc_ioh;
    220 	u_int32_t timeh;	/* elapse time (2*timeh sec) */
    221 	u_int32_t timel;	/* timel/32768 sec */
    222 
    223 	timeh = bus_space_read_2(iot, ioh, ETIME_H_REG_W);
    224 	timeh = (timeh << 16) | bus_space_read_2(iot, ioh, ETIME_M_REG_W);
    225 	timel = bus_space_read_2(iot, ioh, ETIME_L_REG_W);
    226 
    227 	DPRINTF(("clock_get: timeh %08lx timel %08lx\n", timeh, timel));
    228 
    229 	cvt_timehl_ymdhms(timeh, timel, dt);
    230 
    231 	DPRINTF(("clock_get: %d/%d/%d/%d/%d/%d\n", dt->dt_year, dt->dt_mon,
    232 	    dt->dt_day, dt->dt_hour, dt->dt_min, dt->dt_sec));
    233 }
    234 
    235 void
    236 clock_set(struct device *dev, struct clock_ymdhms *dt)
    237 {
    238 	struct vrrtc_softc *sc = (struct vrrtc_softc *)dev;
    239 	bus_space_tag_t iot = sc->sc_iot;
    240 	bus_space_handle_t ioh = sc->sc_ioh;
    241 	u_int32_t timeh;	/* elapse time (2*timeh sec) */
    242 	u_int32_t timel;	/* timel/32768 sec */
    243 	int year, month, sec2;
    244 
    245 	timeh = 0;
    246 	timel = 0;
    247 
    248 	DPRINTF(("clock_set: %d/%d/%d/%d/%d/%d\n", dt->dt_year, dt->dt_mon,
    249 	    dt->dt_day, dt->dt_hour, dt->dt_min, dt->dt_sec));
    250 
    251 	dt->dt_year += YBASE;
    252 
    253 	DPRINTF(("clock_set: %d/%d/%d/%d/%d/%d\n", dt->dt_year, dt->dt_mon,
    254 	    dt->dt_day, dt->dt_hour, dt->dt_min, dt->dt_sec));
    255 
    256 	year = EPOCHYEAR;
    257 	sec2 = LEAPYEAR4(year)?SEC2YR+SEC2DAY:SEC2YR;
    258 	while (year < dt->dt_year) {
    259 		year++;
    260 		timeh += sec2;
    261 		sec2 = LEAPYEAR4(year)?SEC2YR+SEC2DAY:SEC2YR;
    262 	}
    263 	month = 1; /* now month is 1..12 */
    264 	sec2 = SEC2DAY * m2d[month-1];
    265 	while (month < dt->dt_mon) {
    266 		month++;
    267 		timeh += sec2;
    268 		sec2 = SEC2DAY * m2d[month-1];
    269 		if (month == 2 && LEAPYEAR4(year)) /* feb. and leapyear */
    270 			sec2 += SEC2DAY;
    271 	}
    272 
    273 	timeh += (dt->dt_day - 1)*SEC2DAY;
    274 
    275 	timeh += dt->dt_hour*SEC2HOUR;
    276 
    277 	timeh += dt->dt_min*SEC2MIN;
    278 
    279 	timeh += dt->dt_sec/2;
    280 	timel += (dt->dt_sec%2)*ETIME_L_HZ;
    281 
    282 	timeh += EPOCHOFF;
    283 	timeh -= (rtc_offset*SEC2MIN);
    284 
    285 #ifdef VRRTCDEBUG
    286 	cvt_timehl_ymdhms(timeh, timel, NULL);
    287 #endif /* RTCDEBUG */
    288 
    289 	bus_space_write_2(iot, ioh, ETIME_H_REG_W, (timeh >> 16) & 0xffff);
    290 	bus_space_write_2(iot, ioh, ETIME_M_REG_W, timeh & 0xffff);
    291 	bus_space_write_2(iot, ioh, ETIME_L_REG_W, timel);
    292 }
    293 
    294 void
    295 cvt_timehl_ymdhms(
    296 	u_int32_t timeh, /* 2 sec */
    297 	u_int32_t timel, /* 1/32768 sec */
    298 	struct clock_ymdhms *dt)
    299 {
    300 	u_int32_t year, month, date, hour, min, sec, sec2;
    301 
    302 	timeh -= EPOCHOFF;
    303 
    304 	timeh += (rtc_offset*SEC2MIN);
    305 
    306 	year = EPOCHYEAR;
    307 	sec2 = LEAPYEAR4(year)?SEC2YR+SEC2DAY:SEC2YR;
    308 	while (timeh > sec2) {
    309 		year++;
    310 		timeh -= sec2;
    311 		sec2 = LEAPYEAR4(year)?SEC2YR+SEC2DAY:SEC2YR;
    312 	}
    313 
    314 	DPRINTF(("cvt_timehl_ymdhms: timeh %08lx year %ld yrref %ld\n",
    315 	    timeh, year, sec2));
    316 
    317 	month = 0; /* now month is 0..11 */
    318 	sec2 = SEC2DAY * m2d[month];
    319 	while (timeh > sec2) {
    320 		timeh -= sec2;
    321 		month++;
    322 		sec2 = SEC2DAY * m2d[month];
    323 		if (month == 1 && LEAPYEAR4(year)) /* feb. and leapyear */
    324 			sec2 += SEC2DAY;
    325 	}
    326 	month +=1; /* now month is 1..12 */
    327 
    328 	DPRINTF(("cvt_timehl_ymdhms: timeh %08lx month %ld mref %ld\n",
    329 	    timeh, month, sec2));
    330 
    331 	sec2 = SEC2DAY;
    332 	date = timeh/sec2+1; /* date is 1..31 */
    333 	timeh -= (date-1)*sec2;
    334 
    335 	DPRINTF(("cvt_timehl_ymdhms: timeh %08lx date %ld dref %ld\n",
    336 	    timeh, date, sec2));
    337 
    338 	sec2 = SEC2HOUR;
    339 	hour = timeh/sec2;
    340 	timeh -= hour*sec2;
    341 
    342 	sec2 = SEC2MIN;
    343 	min = timeh/sec2;
    344 	timeh -= min*sec2;
    345 
    346 	sec = timeh*2 + timel/ETIME_L_HZ;
    347 
    348 	DPRINTF(("cvt_timehl_ymdhms: hour %ld min %ld sec %ld\n", hour, min, sec));
    349 
    350 	if (dt) {
    351 		dt->dt_year	= year - YBASE; /* base 1900 */
    352 		dt->dt_mon	= month;
    353 		dt->dt_day	= date;
    354 		dt->dt_hour	= hour;
    355 		dt->dt_min	= min;
    356 		dt->dt_sec	= sec;
    357 	}
    358 }
    359 
    360 void
    361 vrrtc_dump_regs(struct vrrtc_softc *sc)
    362 {
    363 	int timeh;
    364 	int timel;
    365 
    366 	timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_H_REG_W);
    367 	timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_M_REG_W);
    368 	timel = (timel << 16)
    369 	    | bus_space_read_2(sc->sc_iot, sc->sc_ioh, ETIME_L_REG_W);
    370 	printf("clock_init()  Elapse Time %04x%04x\n", timeh, timel);
    371 
    372 	timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ECMP_H_REG_W);
    373 	timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ECMP_M_REG_W);
    374 	timel = (timel << 16)
    375 	    | bus_space_read_2(sc->sc_iot, sc->sc_ioh, ECMP_L_REG_W);
    376 	printf("clock_init()  Elapse Compare %04x%04x\n", timeh, timel);
    377 
    378 	timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL1_H_REG_W);
    379 	timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL1_L_REG_W);
    380 	printf("clock_init()  LONG1 %04x%04x\n", timeh, timel);
    381 
    382 	timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL1_CNT_H_REG_W);
    383 	timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL1_CNT_L_REG_W);
    384 	printf("clock_init()  LONG1 CNTL %04x%04x\n", timeh, timel);
    385 
    386 	timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL2_H_REG_W);
    387 	timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL2_L_REG_W);
    388 	printf("clock_init()  LONG2 %04x%04x\n", timeh, timel);
    389 
    390 	timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL2_CNT_H_REG_W);
    391 	timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, RTCL2_CNT_L_REG_W);
    392 	printf("clock_init()  LONG2 CNTL %04x%04x\n", timeh, timel);
    393 
    394 	timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, TCLK_H_REG_W);
    395 	timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, TCLK_L_REG_W);
    396 	printf("clock_init()  TCLK %04x%04x\n", timeh, timel);
    397 
    398 	timeh = bus_space_read_2(sc->sc_iot, sc->sc_ioh, TCLK_CNT_H_REG_W);
    399 	timel = bus_space_read_2(sc->sc_iot, sc->sc_ioh, TCLK_CNT_L_REG_W);
    400 	printf("clock_init()  TCLK CNTL %04x%04x\n", timeh, timel);
    401 }
    402