xref: /netbsd-src/sys/arch/sgimips/dev/dpclock.c (revision 07519201ac566a9687e99c64d68b8f406c448934)
1*07519201Sthorpej /*	$NetBSD: dpclock.c,v 1.7 2020/01/02 23:51:48 thorpej Exp $	*/
285716c44Srumble 
385716c44Srumble /*
485716c44Srumble  * Copyright (c) 2001 Erik Reid
585716c44Srumble  * Copyright (c) 2001 Rafal K. Boni
685716c44Srumble  * Copyright (c) 2001 Christopher Sekiya
785716c44Srumble  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
885716c44Srumble  * All rights reserved.
985716c44Srumble  *
1085716c44Srumble  * Portions of this code are derived from software contributed to The
1185716c44Srumble  * NetBSD Foundation by Jason R. Thorpe of the Numerical Aerospace
1285716c44Srumble  * Simulation Facility, NASA Ames Research Center.
1385716c44Srumble  *
1485716c44Srumble  * Redistribution and use in source and binary forms, with or without
1585716c44Srumble  * modification, are permitted provided that the following conditions
1685716c44Srumble  * are met:
1785716c44Srumble  * 1. Redistributions of source code must retain the above copyright
1885716c44Srumble  *    notice, this list of conditions and the following disclaimer.
1985716c44Srumble  * 2. Redistributions in binary form must reproduce the above copyright
2085716c44Srumble  *    notice, this list of conditions and the following disclaimer in the
2185716c44Srumble  *    documentation and/or other materials provided with the distribution.
2285716c44Srumble  * 3. The name of the author may not be used to endorse or promote products
2385716c44Srumble  *    derived from this software without specific prior written permission.
2485716c44Srumble  *
2585716c44Srumble  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2685716c44Srumble  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2785716c44Srumble  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2885716c44Srumble  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2985716c44Srumble  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
3085716c44Srumble  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3185716c44Srumble  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3285716c44Srumble  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3385716c44Srumble  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3485716c44Srumble  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3585716c44Srumble  */
3685716c44Srumble 
3785716c44Srumble #include <sys/param.h>
3885716c44Srumble #include <sys/kernel.h>
3985716c44Srumble #include <sys/systm.h>
4085716c44Srumble #include <sys/device.h>
4185716c44Srumble 
42cf10107dSdyoung #include <sys/bus.h>
4385716c44Srumble #include <machine/autoconf.h>
4485716c44Srumble #include <machine/sysconf.h>
4585716c44Srumble #include <machine/machtype.h>
4685716c44Srumble 
4785716c44Srumble #include <dev/clock_subr.h>
4885716c44Srumble #include <sgimips/dev/dp8573areg.h>
4985716c44Srumble 
5085716c44Srumble #include <sgimips/sgimips/clockvar.h>
5185716c44Srumble 
5285716c44Srumble struct dpclock_softc {
5385716c44Srumble 	struct todr_chip_handle sc_todrch;
5485716c44Srumble 
5585716c44Srumble 	/* RTC registers */
5685716c44Srumble 	bus_space_tag_t		sc_rtct;
5785716c44Srumble 	bus_space_handle_t	sc_rtch;
58eb488f67Smacallan 	int			sc_offset;
5985716c44Srumble };
6085716c44Srumble 
61cbab9cadSchs static int	dpclock_match(device_t, cfdata_t, void *);
62cbab9cadSchs static void	dpclock_attach(device_t, device_t, void *);
63*07519201Sthorpej static int	dpclock_gettime_ymdhms(struct todr_chip_handle *,
64*07519201Sthorpej 				       struct clock_ymdhms *);
65*07519201Sthorpej static int	dpclock_settime_ymdhms(struct todr_chip_handle *,
66*07519201Sthorpej 				       struct clock_ymdhms *);
6785716c44Srumble 
68cbab9cadSchs CFATTACH_DECL_NEW(dpclock, sizeof(struct dpclock_softc),
6985716c44Srumble     dpclock_match, dpclock_attach, NULL, NULL);
7085716c44Srumble 
7185716c44Srumble static int
dpclock_match(device_t parent,cfdata_t cf,void * aux)72cbab9cadSchs dpclock_match(device_t parent, cfdata_t cf, void *aux)
7385716c44Srumble {
7485716c44Srumble 	struct mainbus_attach_args *ma = aux;
7585716c44Srumble 
7685716c44Srumble 	switch (mach_type) {
7785716c44Srumble 	case MACH_SGI_IP6 | MACH_SGI_IP10:
7885716c44Srumble 		if (ma->ma_addr == 0x1fbc0000)
7985716c44Srumble 			return (1);
8085716c44Srumble 		break;
8185716c44Srumble 
8285716c44Srumble 	case MACH_SGI_IP12:
8385716c44Srumble 	case MACH_SGI_IP20:
8485716c44Srumble 		if (ma->ma_addr == 0x1fb80e00)
8585716c44Srumble 			return (1);
8685716c44Srumble 		break;
8785716c44Srumble 	}
8885716c44Srumble 
8985716c44Srumble 	return (0);
9085716c44Srumble }
9185716c44Srumble 
9285716c44Srumble static void
writereg(struct dpclock_softc * sc,uint32_t reg,uint8_t val)93eb488f67Smacallan writereg(struct dpclock_softc *sc, uint32_t reg, uint8_t val)
94eb488f67Smacallan {
95eb488f67Smacallan 	bus_space_write_1(sc->sc_rtct, sc->sc_rtch,
96eb488f67Smacallan 	    (reg << 2) + sc->sc_offset, val);
97eb488f67Smacallan }
98eb488f67Smacallan 
99eb488f67Smacallan static uint8_t
readreg(struct dpclock_softc * sc,uint32_t reg)100eb488f67Smacallan readreg(struct dpclock_softc *sc, uint32_t reg)
101eb488f67Smacallan {
102eb488f67Smacallan 	return bus_space_read_1(sc->sc_rtct, sc->sc_rtch,
103eb488f67Smacallan 	    (reg << 2) + sc->sc_offset);
104eb488f67Smacallan }
105eb488f67Smacallan 
106eb488f67Smacallan static void
dpclock_attach(device_t parent,device_t self,void * aux)107cbab9cadSchs dpclock_attach(device_t parent, device_t self, void *aux)
10885716c44Srumble {
109cbab9cadSchs 	struct dpclock_softc *sc = device_private(self);
11085716c44Srumble 	struct mainbus_attach_args *ma = aux;
11185716c44Srumble 	int err;
11285716c44Srumble 
11385716c44Srumble 	printf("\n");
11485716c44Srumble 
115eb488f67Smacallan 	sc->sc_rtct = normal_memt;
11685716c44Srumble 	/*
11785716c44Srumble 	 * All machines have one byte register per word. IP6/IP10 use
11885716c44Srumble 	 * the MSB, others the LSB.
11985716c44Srumble 	 */
12085716c44Srumble 	if (mach_type == MACH_SGI_IP12 || mach_type == MACH_SGI_IP20)
121eb488f67Smacallan 		sc->sc_offset = 3;
12285716c44Srumble 	else
123eb488f67Smacallan 		sc->sc_offset = 0;
12485716c44Srumble 
12585716c44Srumble 	if ((err = bus_space_map(sc->sc_rtct, ma->ma_addr, 0x1ffff,
12685716c44Srumble 	    BUS_SPACE_MAP_LINEAR, &sc->sc_rtch)) != 0) {
12785716c44Srumble 		printf(": unable to map RTC registers, error = %d\n", err);
12885716c44Srumble 		return;
12985716c44Srumble 	}
13085716c44Srumble 
13185716c44Srumble 	sc->sc_todrch.cookie = sc;
132*07519201Sthorpej 	sc->sc_todrch.todr_gettime = NULL;
133*07519201Sthorpej 	sc->sc_todrch.todr_settime = NULL;
134*07519201Sthorpej 	sc->sc_todrch.todr_gettime_ymdhms = dpclock_gettime_ymdhms;
135*07519201Sthorpej 	sc->sc_todrch.todr_settime_ymdhms = dpclock_settime_ymdhms;
13685716c44Srumble 	sc->sc_todrch.todr_setwen = NULL;
13785716c44Srumble 
13885716c44Srumble 	todr_attach(&sc->sc_todrch);
13985716c44Srumble }
14085716c44Srumble 
14185716c44Srumble /*
14285716c44Srumble  * Get the time of day, based on the clock's value and/or the base value.
14385716c44Srumble  */
14485716c44Srumble static int
dpclock_gettime_ymdhms(struct todr_chip_handle * todrch,struct clock_ymdhms * dt)145*07519201Sthorpej dpclock_gettime_ymdhms(struct todr_chip_handle *todrch, struct clock_ymdhms *dt)
14685716c44Srumble {
14785716c44Srumble 	struct dpclock_softc *sc = (struct dpclock_softc *)todrch->cookie;
14885716c44Srumble 	int s;
14985716c44Srumble 	u_int8_t i, j;
15085716c44Srumble 	u_int8_t regs[32];
15185716c44Srumble 
15285716c44Srumble 	s = splhigh();
153eb488f67Smacallan 	i = readreg(sc, DP8573A_TIMESAVE_CTL);
15485716c44Srumble 	j = i | DP8573A_TIMESAVE_CTL_EN;
155eb488f67Smacallan 	writereg(sc, DP8573A_TIMESAVE_CTL, j);
156eb488f67Smacallan 	writereg(sc, DP8573A_TIMESAVE_CTL, i);
15785716c44Srumble 	splx(s);
15885716c44Srumble 
15985716c44Srumble 	for (i = 0; i < 32; i++)
160eb488f67Smacallan 		regs[i] = readreg(sc, i);
16185716c44Srumble 
162*07519201Sthorpej 	dt->dt_sec = bcdtobin(regs[DP8573A_SAVE_SEC]);
163*07519201Sthorpej 	dt->dt_min = bcdtobin(regs[DP8573A_SAVE_MIN]);
16485716c44Srumble 
16585716c44Srumble 	if (regs[DP8573A_RT_MODE] & DP8573A_RT_MODE_1224) {
166*07519201Sthorpej 		dt->dt_hour = bcdtobin(regs[DP8573A_SAVE_HOUR] &
16785716c44Srumble 						DP8573A_HOUR_12HR_MASK) +
16885716c44Srumble 		    ((regs[DP8573A_SAVE_HOUR] & DP8573A_RT_MODE_1224) ? 0 : 12);
16985716c44Srumble 
17085716c44Srumble 		/*
17185716c44Srumble 		 * In AM/PM mode, hour range is 01-12, so adding in 12 hours
17285716c44Srumble 		 * for PM gives us 01-24, whereas we want 00-23, so map hour
17385716c44Srumble 		 * 24 to hour 0.
17485716c44Srumble 		 */
17585716c44Srumble 
176*07519201Sthorpej 		if (dt->dt_hour == 24)
177*07519201Sthorpej 			dt->dt_hour = 0;
17885716c44Srumble 	} else {
179*07519201Sthorpej 		dt->dt_hour = bcdtobin(regs[DP8573A_SAVE_HOUR] &
18085716c44Srumble 							DP8573A_HOUR_24HR_MASK);
18185716c44Srumble 	}
18285716c44Srumble 
183*07519201Sthorpej 	dt->dt_wday = bcdtobin(regs[DP8573A_DOW]);    /* Not from time saved */
184*07519201Sthorpej 	dt->dt_day = bcdtobin(regs[DP8573A_SAVE_DOM]);
185*07519201Sthorpej 	dt->dt_mon = bcdtobin(regs[DP8573A_SAVE_MONTH]);
186*07519201Sthorpej 	dt->dt_year = FROM_IRIX_YEAR(bcdtobin(regs[DP8573A_YEAR]));
18785716c44Srumble 
18885716c44Srumble 	return (0);
18985716c44Srumble }
19085716c44Srumble 
19185716c44Srumble /*
19285716c44Srumble  * Reset the TODR based on the time value.
19385716c44Srumble  */
19485716c44Srumble static int
dpclock_settime_ymdhms(struct todr_chip_handle * todrch,struct clock_ymdhms * dt)195*07519201Sthorpej dpclock_settime_ymdhms(struct todr_chip_handle *todrch, struct clock_ymdhms *dt)
19685716c44Srumble {
19785716c44Srumble 	struct dpclock_softc *sc = (struct dpclock_softc *)todrch->cookie;
19885716c44Srumble 	int s;
19985716c44Srumble 	u_int8_t i, j;
20085716c44Srumble 	u_int8_t regs[32];
20185716c44Srumble 
20285716c44Srumble 	s = splhigh();
203eb488f67Smacallan 	i = readreg(sc, DP8573A_TIMESAVE_CTL);
20485716c44Srumble 	j = i | DP8573A_TIMESAVE_CTL_EN;
205eb488f67Smacallan 	writereg(sc, DP8573A_TIMESAVE_CTL, j);
206eb488f67Smacallan 	writereg(sc, DP8573A_TIMESAVE_CTL, i);
20785716c44Srumble 	splx(s);
20885716c44Srumble 
20985716c44Srumble 	for (i = 0; i < 32; i++)
210eb488f67Smacallan 		regs[i] = readreg(sc, i);
21185716c44Srumble 
21285716c44Srumble 	regs[DP8573A_SUBSECOND] = 0;
213*07519201Sthorpej 	regs[DP8573A_SECOND] = bintobcd(dt->dt_sec);
214*07519201Sthorpej 	regs[DP8573A_MINUTE] = bintobcd(dt->dt_min);
215*07519201Sthorpej 	regs[DP8573A_HOUR] = bintobcd(dt->dt_hour) & DP8573A_HOUR_24HR_MASK;
216*07519201Sthorpej 	regs[DP8573A_DOW] = bintobcd(dt->dt_wday);
217*07519201Sthorpej 	regs[DP8573A_DOM] = bintobcd(dt->dt_day);
218*07519201Sthorpej 	regs[DP8573A_MONTH] = bintobcd(dt->dt_mon);
219*07519201Sthorpej 	regs[DP8573A_YEAR] = bintobcd(TO_IRIX_YEAR(dt->dt_year));
22085716c44Srumble 
22185716c44Srumble 	s = splhigh();
222eb488f67Smacallan 	i = readreg(sc, DP8573A_RT_MODE);
22385716c44Srumble 	j = i & ~DP8573A_RT_MODE_CLKSS;
224eb488f67Smacallan 	writereg(sc, DP8573A_RT_MODE, j);
22585716c44Srumble 
22685716c44Srumble 	for (i = 0; i < 10; i++)
227eb488f67Smacallan 		writereg(sc, DP8573A_COUNTERS +i, regs[DP8573A_COUNTERS + i]);
22885716c44Srumble 
229eb488f67Smacallan 	writereg(sc, DP8573A_RT_MODE, i);
23085716c44Srumble 	splx(s);
23185716c44Srumble 
23285716c44Srumble 	return (0);
23385716c44Srumble }
234