xref: /netbsd-src/sys/arch/sgimips/mace/mcclock_mace.c (revision eb488f671d3f62b03268182548a1bfc622ffc37b)
1*eb488f67Smacallan /*	$NetBSD: mcclock_mace.c,v 1.17 2015/02/18 16:47:59 macallan Exp $	*/
263b59c4dSsekiya 
363b59c4dSsekiya /*
463b59c4dSsekiya  * Copyright (c) 2001 Antti Kantee.  All Rights Reserved.
563b59c4dSsekiya  *
663b59c4dSsekiya  * Redistribution and use in source and binary forms, with or without
763b59c4dSsekiya  * modification, are permitted provided that the following conditions
863b59c4dSsekiya  * are met:
963b59c4dSsekiya  * 1. Redistributions of source code must retain the above copyright
1063b59c4dSsekiya  *    notice, this list of conditions and the following disclaimer.
1163b59c4dSsekiya  * 2. Redistributions in binary form must reproduce the above copyright
1263b59c4dSsekiya  *    notice, this list of conditions and the following disclaimer in the
1363b59c4dSsekiya  *    documentation and/or other materials provided with the distribution.
14ac9042f5Spooka  * 3. The name of the company nor the name of the author may be used to
1563b59c4dSsekiya  *    endorse or promote products derived from this software without specific
1663b59c4dSsekiya  *    prior written permission.
1763b59c4dSsekiya  *
1863b59c4dSsekiya  * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS
1963b59c4dSsekiya  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2063b59c4dSsekiya  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2163b59c4dSsekiya  * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE
2263b59c4dSsekiya  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2363b59c4dSsekiya  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2463b59c4dSsekiya  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2563b59c4dSsekiya  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2663b59c4dSsekiya  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2763b59c4dSsekiya  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2863b59c4dSsekiya  * SUCH DAMAGE.
2963b59c4dSsekiya  */
3063b59c4dSsekiya 
3163b59c4dSsekiya /*
3263b59c4dSsekiya  * Copyright (c) 1998 Mark Brinicombe.
3363b59c4dSsekiya  * Copyright (c) 1998 Causality Limited.
3463b59c4dSsekiya  * All rights reserved.
3563b59c4dSsekiya  *
3663b59c4dSsekiya  * Written by Mark Brinicombe, Causality Limited
3763b59c4dSsekiya  *
3863b59c4dSsekiya  * Redistribution and use in source and binary forms, with or without
3963b59c4dSsekiya  * modification, are permitted provided that the following conditions
4063b59c4dSsekiya  * are met:
4163b59c4dSsekiya  * 1. Redistributions of source code must retain the above copyright
4263b59c4dSsekiya  *    notice, this list of conditions and the following disclaimer.
4363b59c4dSsekiya  * 2. Redistributions in binary form must reproduce the above copyright
4463b59c4dSsekiya  *    notice, this list of conditions and the following disclaimer in the
4563b59c4dSsekiya  *    documentation and/or other materials provided with the distribution.
4663b59c4dSsekiya  * 3. All advertising materials mentioning features or use of this software
4763b59c4dSsekiya  *    must display the following acknowledgement:
4863b59c4dSsekiya  *	This product includes software developed by Mark Brinicombe
4963b59c4dSsekiya  *	for the NetBSD Project.
5063b59c4dSsekiya  * 4. The name of the company nor the name of the author may be used to
5163b59c4dSsekiya  *    endorse or promote products derived from this software without specific
5263b59c4dSsekiya  *    prior written permission.
5363b59c4dSsekiya  *
5463b59c4dSsekiya  * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS
5563b59c4dSsekiya  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
5663b59c4dSsekiya  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
5763b59c4dSsekiya  * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE
5863b59c4dSsekiya  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5963b59c4dSsekiya  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
6063b59c4dSsekiya  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6163b59c4dSsekiya  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6263b59c4dSsekiya  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6363b59c4dSsekiya  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6463b59c4dSsekiya  * SUCH DAMAGE.
6563b59c4dSsekiya  */
6663b59c4dSsekiya 
6763b59c4dSsekiya #include <sys/cdefs.h>
68*eb488f67Smacallan __KERNEL_RCSID(0, "$NetBSD: mcclock_mace.c,v 1.17 2015/02/18 16:47:59 macallan Exp $");
6963b59c4dSsekiya 
7063b59c4dSsekiya #include <sys/param.h>
7163b59c4dSsekiya #include <sys/systm.h>
7258141c89Suebayasi #include <sys/device.h>
7363b59c4dSsekiya 
7463b59c4dSsekiya #include <machine/cpu.h>
7563b59c4dSsekiya #include <machine/autoconf.h>
76cf10107dSdyoung #include <sys/bus.h>
7763b59c4dSsekiya #include <machine/machtype.h>
7863b59c4dSsekiya 
7963b59c4dSsekiya #include <dev/clock_subr.h>
8063b59c4dSsekiya #include <dev/ic/ds1687reg.h>
8163b59c4dSsekiya 
8263b59c4dSsekiya #include <sgimips/mace/macevar.h>
8363b59c4dSsekiya 
8463b59c4dSsekiya #include <sgimips/sgimips/clockvar.h>
8563b59c4dSsekiya 
8663b59c4dSsekiya struct mcclock_mace_softc {
87e180a794Stsutsui 	device_t		sc_dev;
8863b59c4dSsekiya 
89f30a5205Srumble 	struct todr_chip_handle sc_todrch;
90f30a5205Srumble 
9163b59c4dSsekiya 	bus_space_tag_t		sc_st;
9263b59c4dSsekiya 	bus_space_handle_t	sc_sh;
9363b59c4dSsekiya };
9463b59c4dSsekiya 
954a60b0ecSmacallan static struct mcclock_mace_softc *mace0 = NULL;
964a60b0ecSmacallan 
97e180a794Stsutsui static int	mcclock_mace_match(device_t, cfdata_t, void *);
98e180a794Stsutsui static void	mcclock_mace_attach(device_t, device_t, void *);
9963b59c4dSsekiya 
100e180a794Stsutsui static int	mcclock_mace_gettime_ymdhms(todr_chip_handle_t,
10175b234b3Stsutsui     struct clock_ymdhms *);
102e180a794Stsutsui static int	mcclock_mace_settime_ymdhms(todr_chip_handle_t,
10375b234b3Stsutsui     struct clock_ymdhms *);
10463b59c4dSsekiya 
10563b59c4dSsekiya unsigned int	ds1687_read(void *arg, unsigned int addr);
10663b59c4dSsekiya void		ds1687_write(void *arg, unsigned int addr, unsigned int data);
10763b59c4dSsekiya 
1084a60b0ecSmacallan void		mcclock_poweroff(void);
1094a60b0ecSmacallan 
1104a60b0ecSmacallan 
111e180a794Stsutsui CFATTACH_DECL_NEW(mcclock_mace, sizeof(struct mcclock_mace_softc),
11263b59c4dSsekiya     mcclock_mace_match, mcclock_mace_attach, NULL, NULL);
11363b59c4dSsekiya 
11463b59c4dSsekiya static int
mcclock_mace_match(device_t parent,cfdata_t cf,void * aux)115e180a794Stsutsui mcclock_mace_match(device_t parent, cfdata_t cf, void *aux)
11663b59c4dSsekiya {
117e180a794Stsutsui 
11863b59c4dSsekiya 	return 1;
11963b59c4dSsekiya }
12063b59c4dSsekiya 
12163b59c4dSsekiya void
mcclock_mace_attach(device_t parent,device_t self,void * aux)122e180a794Stsutsui mcclock_mace_attach(device_t parent, device_t self, void *aux)
12363b59c4dSsekiya {
124e180a794Stsutsui 	struct mcclock_mace_softc *sc = device_private(self);
12563b59c4dSsekiya 	struct mace_attach_args *maa = aux;
12663b59c4dSsekiya 
127e180a794Stsutsui 	sc->sc_dev = self;
12863b59c4dSsekiya 	sc->sc_st = maa->maa_st;
12963b59c4dSsekiya 	/* XXX should be bus_space_map() */
13063b59c4dSsekiya 	if (bus_space_subregion(maa->maa_st, maa->maa_sh,
13163b59c4dSsekiya 	    maa->maa_offset, 0, &sc->sc_sh))
13263b59c4dSsekiya 		panic("mcclock_mace_attach: couldn't map");
13363b59c4dSsekiya 
13463b59c4dSsekiya 	/*
13563b59c4dSsekiya 	 * We want a fixed format: 24-hour, BCD data, so just force the
13663b59c4dSsekiya 	 * RTC to this mode; if there was junk there we'll be able to
13763b59c4dSsekiya 	 * fix things up later when we discover the time read back is
13863b59c4dSsekiya 	 * no good.
13963b59c4dSsekiya 	 */
14063b59c4dSsekiya 	ds1687_write(sc, DS1687_CONTROLA, DS1687_DV1 | DS1687_BANK1);
14163b59c4dSsekiya 	ds1687_write(sc, DS1687_CONTROLB, DS1687_24HRS);
14263b59c4dSsekiya 
14363b59c4dSsekiya 	/* XXXrkb: init kickstart/wakeup stuff */
14463b59c4dSsekiya 
14563b59c4dSsekiya 	if (!(ds1687_read(sc, DS1687_CONTROLD) & DS1687_VRT))
14663b59c4dSsekiya 		printf(": lithium cell is dead, RTC unreliable");
14763b59c4dSsekiya 
14863b59c4dSsekiya 	printf("\n");
149f30a5205Srumble 
150f30a5205Srumble 	sc->sc_todrch.cookie = sc;
15175b234b3Stsutsui 	sc->sc_todrch.todr_gettime_ymdhms = mcclock_mace_gettime_ymdhms;
15275b234b3Stsutsui 	sc->sc_todrch.todr_settime_ymdhms = mcclock_mace_settime_ymdhms;
153f30a5205Srumble 	sc->sc_todrch.todr_setwen = NULL;
154f30a5205Srumble 
155f30a5205Srumble 	todr_attach(&sc->sc_todrch);
1564a60b0ecSmacallan 	mace0 = sc;
15763b59c4dSsekiya }
15863b59c4dSsekiya 
15963b59c4dSsekiya /*
16063b59c4dSsekiya  * We need to use the following two functions from
16163b59c4dSsekiya  * DS1687_PUT/GETTOD(). Hence the names.
16263b59c4dSsekiya  */
16363b59c4dSsekiya unsigned int
ds1687_read(void * arg,unsigned int addr)16463b59c4dSsekiya ds1687_read(void *arg, unsigned int addr)
16563b59c4dSsekiya {
166e180a794Stsutsui 	struct mcclock_mace_softc *sc = arg;
16763b59c4dSsekiya 
168*eb488f67Smacallan 	return bus_space_read_1(sc->sc_st, sc->sc_sh, (addr << 8) + 7);
16963b59c4dSsekiya }
17063b59c4dSsekiya 
17163b59c4dSsekiya void
ds1687_write(void * arg,unsigned int addr,unsigned int data)17263b59c4dSsekiya ds1687_write(void *arg, unsigned int addr, unsigned int data)
17363b59c4dSsekiya {
174e180a794Stsutsui 	struct mcclock_mace_softc *sc = arg;
17563b59c4dSsekiya 
176*eb488f67Smacallan 	bus_space_write_1(sc->sc_st, sc->sc_sh, (addr << 8) + 7, data);
17763b59c4dSsekiya }
17863b59c4dSsekiya 
179f30a5205Srumble static int
mcclock_mace_gettime_ymdhms(todr_chip_handle_t todrch,struct clock_ymdhms * dt)180e180a794Stsutsui mcclock_mace_gettime_ymdhms(todr_chip_handle_t todrch, struct clock_ymdhms *dt)
18163b59c4dSsekiya {
182e180a794Stsutsui 	struct mcclock_mace_softc *sc = todrch->cookie;
18363b59c4dSsekiya 	ds1687_todregs regs;
18463b59c4dSsekiya 	int s;
18563b59c4dSsekiya 
18663b59c4dSsekiya 	s = splhigh();
18763b59c4dSsekiya 	DS1687_GETTOD(sc, &regs);
18863b59c4dSsekiya 	splx(s);
18963b59c4dSsekiya 
190b59f66e1Schristos 	dt->dt_sec = bcdtobin(regs[DS1687_SOFT_SEC]);
191b59f66e1Schristos 	dt->dt_min = bcdtobin(regs[DS1687_SOFT_MIN]);
192b59f66e1Schristos 	dt->dt_hour = bcdtobin(regs[DS1687_SOFT_HOUR]);
193b59f66e1Schristos 	dt->dt_wday = bcdtobin(regs[DS1687_SOFT_DOW]);
194b59f66e1Schristos 	dt->dt_day = bcdtobin(regs[DS1687_SOFT_DOM]);
195b59f66e1Schristos 	dt->dt_mon = bcdtobin(regs[DS1687_SOFT_MONTH]);
196b59f66e1Schristos 	dt->dt_year = bcdtobin(regs[DS1687_SOFT_YEAR]) +
197b59f66e1Schristos 	    (100 * bcdtobin(regs[DS1687_SOFT_CENTURY]));
198f30a5205Srumble 
199e180a794Stsutsui 	return 0;
20063b59c4dSsekiya }
20163b59c4dSsekiya 
202f30a5205Srumble static int
mcclock_mace_settime_ymdhms(todr_chip_handle_t todrch,struct clock_ymdhms * dt)203e180a794Stsutsui mcclock_mace_settime_ymdhms(todr_chip_handle_t todrch, struct clock_ymdhms *dt)
20463b59c4dSsekiya {
205e180a794Stsutsui 	struct mcclock_mace_softc *sc = todrch->cookie;
20663b59c4dSsekiya 	ds1687_todregs regs;
20763b59c4dSsekiya 	int s;
20863b59c4dSsekiya 
20963b59c4dSsekiya 	memset(&regs, 0, sizeof(regs));
21063b59c4dSsekiya 
211b59f66e1Schristos 	regs[DS1687_SOFT_SEC] = bintobcd(dt->dt_sec);
212b59f66e1Schristos 	regs[DS1687_SOFT_MIN] = bintobcd(dt->dt_min);
213b59f66e1Schristos 	regs[DS1687_SOFT_HOUR] = bintobcd(dt->dt_hour);
214b59f66e1Schristos 	regs[DS1687_SOFT_DOW] = bintobcd(dt->dt_wday);
215b59f66e1Schristos 	regs[DS1687_SOFT_DOM] = bintobcd(dt->dt_day);
216b59f66e1Schristos 	regs[DS1687_SOFT_MONTH] = bintobcd(dt->dt_mon);
217b59f66e1Schristos 	regs[DS1687_SOFT_YEAR] = bintobcd(dt->dt_year % 100);
218b59f66e1Schristos 	regs[DS1687_SOFT_CENTURY] = bintobcd(dt->dt_year / 100);
21963b59c4dSsekiya 	s = splhigh();
22063b59c4dSsekiya 	DS1687_PUTTOD(sc, &regs);
22163b59c4dSsekiya 	splx(s);
222f30a5205Srumble 
223e180a794Stsutsui 	return 0;
22463b59c4dSsekiya }
2254a60b0ecSmacallan 
2264a60b0ecSmacallan void
mcclock_poweroff(void)227df7f595eScegger mcclock_poweroff(void)
2284a60b0ecSmacallan {
2294a60b0ecSmacallan 	uint8_t a, xctl_a, xctl_b;
2304a60b0ecSmacallan 
2314a60b0ecSmacallan 	if (mace0 == NULL)
2324a60b0ecSmacallan 		return;
2334a60b0ecSmacallan 
234d07fd968Smrg 	(void)splhigh();
2354a60b0ecSmacallan 	a = ds1687_read(mace0, DS1687_CONTROLA);
2364a60b0ecSmacallan 	a &= ~DS1687_DV2;
2374a60b0ecSmacallan 	a |= DS1687_DV1;
2384a60b0ecSmacallan 	ds1687_write(mace0, DS1687_CONTROLA, a | DS1687_BANK1);
2394a60b0ecSmacallan 	wbflush();
2404a60b0ecSmacallan 
2414a60b0ecSmacallan 	xctl_b = ds1687_read(mace0, DS1687_BANK1_XCTRL4B);
2424a60b0ecSmacallan 	xctl_b |= DS1687_X4B_ABE | DS1687_X4B_KIE;
2434a60b0ecSmacallan 	ds1687_write(mace0, DS1687_BANK1_XCTRL4B, xctl_b);
2444a60b0ecSmacallan 
2454a60b0ecSmacallan 	xctl_a = ds1687_read(mace0, DS1687_BANK1_XCTRL4A);
2464a60b0ecSmacallan 	xctl_a &= ~(DS1687_X4A_RCF | DS1687_X4A_WAF | DS1687_X4A_KF);
2474a60b0ecSmacallan 	ds1687_write(mace0, DS1687_BANK1_XCTRL4A, xctl_a);
2484a60b0ecSmacallan 	wbflush();
2494a60b0ecSmacallan 
2504a60b0ecSmacallan 	/* and down we go */
2514a60b0ecSmacallan 	ds1687_write(mace0, DS1687_BANK1_XCTRL4A, xctl_a | DS1687_X4A_PAB);
2524a60b0ecSmacallan 	ds1687_write(mace0, DS1687_CONTROLA, a);
2534a60b0ecSmacallan 	wbflush();
254e180a794Stsutsui 	for (;;)
255e180a794Stsutsui 		;
2564a60b0ecSmacallan }
257