xref: /netbsd-src/sys/arch/sgimips/dev/dsclock.c (revision eb488f671d3f62b03268182548a1bfc622ffc37b)
1*eb488f67Smacallan /*	$NetBSD: dsclock.c,v 1.7 2015/02/18 16:47:58 macallan Exp $	*/
285716c44Srumble 
385716c44Srumble /*
485716c44Srumble  * Copyright (c) 2001 Rafal K. Boni
585716c44Srumble  * Copyright (c) 2001 Christopher Sekiya
685716c44Srumble  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
785716c44Srumble  * All rights reserved.
885716c44Srumble  *
985716c44Srumble  * Portions of this code are derived from software contributed to The
1085716c44Srumble  * NetBSD Foundation by Jason R. Thorpe of the Numerical Aerospace
1185716c44Srumble  * Simulation Facility, NASA Ames Research Center.
1285716c44Srumble  *
1385716c44Srumble  * Redistribution and use in source and binary forms, with or without
1485716c44Srumble  * modification, are permitted provided that the following conditions
1585716c44Srumble  * are met:
1685716c44Srumble  * 1. Redistributions of source code must retain the above copyright
1785716c44Srumble  *    notice, this list of conditions and the following disclaimer.
1885716c44Srumble  * 2. Redistributions in binary form must reproduce the above copyright
1985716c44Srumble  *    notice, this list of conditions and the following disclaimer in the
2085716c44Srumble  *    documentation and/or other materials provided with the distribution.
2185716c44Srumble  * 3. The name of the author may not be used to endorse or promote products
2285716c44Srumble  *    derived from this software without specific prior written permission.
2385716c44Srumble  *
2485716c44Srumble  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2585716c44Srumble  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2685716c44Srumble  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2785716c44Srumble  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2885716c44Srumble  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2985716c44Srumble  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3085716c44Srumble  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3185716c44Srumble  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3285716c44Srumble  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3385716c44Srumble  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3485716c44Srumble  */
3585716c44Srumble 
3685716c44Srumble #include <sys/cdefs.h>
37*eb488f67Smacallan __KERNEL_RCSID(0, "$NetBSD: dsclock.c,v 1.7 2015/02/18 16:47:58 macallan Exp $");
3885716c44Srumble 
3985716c44Srumble #include <sys/param.h>
4085716c44Srumble #include <sys/kernel.h>
4185716c44Srumble #include <sys/systm.h>
4285716c44Srumble #include <sys/device.h>
4385716c44Srumble 
44cf10107dSdyoung #include <sys/bus.h>
4585716c44Srumble #include <machine/autoconf.h>
4685716c44Srumble #include <machine/sysconf.h>
4785716c44Srumble #include <machine/machtype.h>
4885716c44Srumble 
4985716c44Srumble #include <dev/clock_subr.h>
5085716c44Srumble #include <dev/ic/ds1286reg.h>
5185716c44Srumble 
5285716c44Srumble #include <sgimips/sgimips/clockvar.h>
5385716c44Srumble 
5485716c44Srumble struct dsclock_softc {
5523e6770bStsutsui 	device_t sc_dev;
5685716c44Srumble 
5785716c44Srumble 	struct todr_chip_handle sc_todrch;
5885716c44Srumble 
5985716c44Srumble 	/* RTC registers */
6085716c44Srumble 	bus_space_tag_t sc_rtct;
6185716c44Srumble 	bus_space_handle_t sc_rtch;
6285716c44Srumble };
6385716c44Srumble 
6423e6770bStsutsui static int	dsclock_match(device_t, cfdata_t, void *);
6523e6770bStsutsui static void	dsclock_attach(device_t, device_t, void *);
6659bb42b6Stsutsui static int	dsclock_gettime_ymdhms(struct todr_chip_handle *,
6759bb42b6Stsutsui 		    struct clock_ymdhms *);
6859bb42b6Stsutsui static int	dsclock_settime_ymdhms(struct todr_chip_handle *,
6959bb42b6Stsutsui 		    struct clock_ymdhms *);
7085716c44Srumble 
7123e6770bStsutsui CFATTACH_DECL_NEW(dsclock, sizeof(struct dsclock_softc),
7285716c44Srumble     dsclock_match, dsclock_attach, NULL, NULL);
7385716c44Srumble 
7423e6770bStsutsui #define	ds1286_write(sc, reg, datum)					\
75*eb488f67Smacallan     bus_space_write_1((sc)->sc_rtct, (sc)->sc_rtch, (reg << 2) + 3, (datum))
7623e6770bStsutsui #define	ds1286_read(sc, reg)						\
77*eb488f67Smacallan     bus_space_read_1((sc)->sc_rtct, (sc)->sc_rtch, (reg << 2) + 3)
7885716c44Srumble 
7985716c44Srumble static int
dsclock_match(device_t parent,cfdata_t cf,void * aux)8023e6770bStsutsui dsclock_match(device_t parent, cfdata_t cf, void *aux)
8185716c44Srumble {
8285716c44Srumble 	struct mainbus_attach_args *ma = aux;
8385716c44Srumble 
8485716c44Srumble 	if (mach_type == MACH_SGI_IP22 && ma->ma_addr == 0x1fbe0000)
8523e6770bStsutsui 		return 1;
8685716c44Srumble 
8723e6770bStsutsui 	return 0;
8885716c44Srumble }
8985716c44Srumble 
9085716c44Srumble static void
dsclock_attach(device_t parent,device_t self,void * aux)9123e6770bStsutsui dsclock_attach(device_t parent, device_t self, void *aux)
9285716c44Srumble {
9323e6770bStsutsui 	struct dsclock_softc *sc = device_private(self);
9485716c44Srumble 	struct mainbus_attach_args *ma = aux;
9585716c44Srumble 	int err;
9685716c44Srumble 
9723e6770bStsutsui 	aprint_normal("\n");
9885716c44Srumble 
99*eb488f67Smacallan 	sc->sc_rtct = normal_memt;
10085716c44Srumble 	if ((err = bus_space_map(sc->sc_rtct, ma->ma_addr, 0x1ffff,
10185716c44Srumble 	    BUS_SPACE_MAP_LINEAR, &sc->sc_rtch)) != 0) {
10223e6770bStsutsui 		aprint_error_dev(self,
10323e6770bStsutsui 		    "unable to map RTC registers, error = %d\n", err);
10485716c44Srumble 		return;
10585716c44Srumble 	}
10685716c44Srumble 
10785716c44Srumble 	sc->sc_todrch.cookie = sc;
10859bb42b6Stsutsui 	sc->sc_todrch.todr_gettime_ymdhms = dsclock_gettime_ymdhms;
10959bb42b6Stsutsui 	sc->sc_todrch.todr_settime_ymdhms = dsclock_settime_ymdhms;
11085716c44Srumble 	sc->sc_todrch.todr_setwen = NULL;
11185716c44Srumble 
11285716c44Srumble 	todr_attach(&sc->sc_todrch);
11385716c44Srumble }
11485716c44Srumble 
11585716c44Srumble /*
11685716c44Srumble  * Get the time of day, based on the clock's value and/or the base value.
11785716c44Srumble  */
11885716c44Srumble static int
dsclock_gettime_ymdhms(struct todr_chip_handle * todrch,struct clock_ymdhms * dt)11959bb42b6Stsutsui dsclock_gettime_ymdhms(struct todr_chip_handle *todrch, struct clock_ymdhms *dt)
12085716c44Srumble {
12159bb42b6Stsutsui 	struct dsclock_softc *sc = todrch->cookie;
12285716c44Srumble 	ds1286_todregs regs;
12385716c44Srumble 	int s;
12485716c44Srumble 
12585716c44Srumble 	s = splhigh();
12685716c44Srumble 	DS1286_GETTOD(sc, &regs)
12785716c44Srumble 	splx(s);
12885716c44Srumble 
129b59f66e1Schristos 	dt->dt_sec = bcdtobin(regs[DS1286_SEC]);
130b59f66e1Schristos 	dt->dt_min = bcdtobin(regs[DS1286_MIN]);
13185716c44Srumble 
13285716c44Srumble 	if (regs[DS1286_HOUR] & DS1286_HOUR_12MODE) {
13359bb42b6Stsutsui 		dt->dt_hour =
134b59f66e1Schristos 		    bcdtobin(regs[DS1286_HOUR] & DS1286_HOUR_12HR_MASK)
13585716c44Srumble 		    + ((regs[DS1286_HOUR] & DS1286_HOUR_12HR_PM) ? 12 : 0);
13685716c44Srumble 
13785716c44Srumble 		/*
13885716c44Srumble 		 * In AM/PM mode, hour range is 01-12, so adding in 12 hours
13985716c44Srumble 		 * for PM gives us 01-24, whereas we want 00-23, so map hour
14085716c44Srumble 		 * 24 to hour 0.
14185716c44Srumble 		 */
14259bb42b6Stsutsui 		if (dt->dt_hour == 24)
14359bb42b6Stsutsui 			dt->dt_hour = 0;
14485716c44Srumble 	} else {
14559bb42b6Stsutsui 		 dt->dt_hour =
146b59f66e1Schristos 		    bcdtobin(regs[DS1286_HOUR] & DS1286_HOUR_24HR_MASK);
14785716c44Srumble 	}
14885716c44Srumble 
149b59f66e1Schristos 	dt->dt_wday = bcdtobin(regs[DS1286_DOW]);
150b59f66e1Schristos 	dt->dt_day = bcdtobin(regs[DS1286_DOM]);
151b59f66e1Schristos 	dt->dt_mon = bcdtobin(regs[DS1286_MONTH] & DS1286_MONTH_MASK);
152b59f66e1Schristos 	dt->dt_year = FROM_IRIX_YEAR(bcdtobin(regs[DS1286_YEAR]));
15385716c44Srumble 
15423e6770bStsutsui 	return 0;
15585716c44Srumble }
15685716c44Srumble 
15785716c44Srumble /*
15885716c44Srumble  * Reset the TODR based on the time value.
15985716c44Srumble  */
16085716c44Srumble static int
dsclock_settime_ymdhms(struct todr_chip_handle * todrch,struct clock_ymdhms * dt)16159bb42b6Stsutsui dsclock_settime_ymdhms(struct todr_chip_handle *todrch, struct clock_ymdhms *dt)
16285716c44Srumble {
16359bb42b6Stsutsui 	struct dsclock_softc *sc = todrch->cookie;
16485716c44Srumble 	ds1286_todregs regs;
16585716c44Srumble 	int s;
16685716c44Srumble 
16785716c44Srumble 	s = splhigh();
16885716c44Srumble 	DS1286_GETTOD(sc, &regs);
16985716c44Srumble 	splx(s);
17085716c44Srumble 
17185716c44Srumble 	regs[DS1286_SUBSEC] = 0;
172b59f66e1Schristos 	regs[DS1286_SEC] = bintobcd(dt->dt_sec);
173b59f66e1Schristos 	regs[DS1286_MIN] = bintobcd(dt->dt_min);
174b59f66e1Schristos 	regs[DS1286_HOUR] = bintobcd(dt->dt_hour) & DS1286_HOUR_24HR_MASK;
175b59f66e1Schristos 	regs[DS1286_DOW] = bintobcd(dt->dt_wday);
176b59f66e1Schristos 	regs[DS1286_DOM] = bintobcd(dt->dt_day);
17785716c44Srumble 
17885716c44Srumble 	/* Leave wave-generator bits as set originally */
17985716c44Srumble 	regs[DS1286_MONTH] &=  ~DS1286_MONTH_MASK;
180b59f66e1Schristos 	regs[DS1286_MONTH] |=  bintobcd(dt->dt_mon) & DS1286_MONTH_MASK;
18185716c44Srumble 
182b59f66e1Schristos 	regs[DS1286_YEAR] = bintobcd(TO_IRIX_YEAR(dt->dt_year));
18385716c44Srumble 
18485716c44Srumble 	s = splhigh();
18585716c44Srumble 	DS1286_PUTTOD(sc, &regs);
18685716c44Srumble 	splx(s);
18785716c44Srumble 
18823e6770bStsutsui 	return 0;
18985716c44Srumble }
190