xref: /netbsd-src/sys/arch/mips/alchemy/dev/aurtc.c (revision f58fcf6acfd26103cd885f6cab9a64560e20bfa9)
1*f58fcf6aSkiyohara /* $NetBSD: aurtc.c,v 1.14 2012/01/03 07:36:02 kiyohara Exp $ */
2d375c80eSgdamore 
3d375c80eSgdamore /*-
4d375c80eSgdamore  * Copyright (c) 2006 Itronix Inc.
5d375c80eSgdamore  * All rights reserved.
6d375c80eSgdamore  *
7d375c80eSgdamore  * Written by Garrett D'Amore for Itronix Inc.
8d375c80eSgdamore  *
9d375c80eSgdamore  * Redistribution and use in source and binary forms, with or without
10d375c80eSgdamore  * modification, are permitted provided that the following conditions
11d375c80eSgdamore  * are met:
12d375c80eSgdamore  * 1. Redistributions of source code must retain the above copyright
13d375c80eSgdamore  *    notice, this list of conditions and the following disclaimer.
14d375c80eSgdamore  * 2. Redistributions in binary form must reproduce the above copyright
15d375c80eSgdamore  *    notice, this list of conditions and the following disclaimer in the
16d375c80eSgdamore  *    documentation and/or other materials provided with the distribution.
17d375c80eSgdamore  * 3. The name of Itronix Inc. may not be used to endorse
18d375c80eSgdamore  *    or promote products derived from this software without specific
19d375c80eSgdamore  *    prior written permission.
20d375c80eSgdamore  *
21d375c80eSgdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22d375c80eSgdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23d375c80eSgdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24d375c80eSgdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25d375c80eSgdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26d375c80eSgdamore  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27d375c80eSgdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28d375c80eSgdamore  * ON ANY THEORY OF LIABILITY, WHETHER IN
29d375c80eSgdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30d375c80eSgdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31d375c80eSgdamore  * POSSIBILITY OF SUCH DAMAGE.
32d375c80eSgdamore  */
33ca42af5eSsimonb 
34ca42af5eSsimonb /*
35ca42af5eSsimonb  * Copyright 2002 Wasabi Systems, Inc.
36ca42af5eSsimonb  * All rights reserved.
37ca42af5eSsimonb  *
38ca42af5eSsimonb  * Written by Simon Burge for Wasabi Systems, Inc.
39ca42af5eSsimonb  *
40ca42af5eSsimonb  * Redistribution and use in source and binary forms, with or without
41ca42af5eSsimonb  * modification, are permitted provided that the following conditions
42ca42af5eSsimonb  * are met:
43ca42af5eSsimonb  * 1. Redistributions of source code must retain the above copyright
44ca42af5eSsimonb  *    notice, this list of conditions and the following disclaimer.
45ca42af5eSsimonb  * 2. Redistributions in binary form must reproduce the above copyright
46ca42af5eSsimonb  *    notice, this list of conditions and the following disclaimer in the
47ca42af5eSsimonb  *    documentation and/or other materials provided with the distribution.
48ca42af5eSsimonb  * 3. All advertising materials mentioning features or use of this software
49ca42af5eSsimonb  *    must display the following acknowledgement:
50ca42af5eSsimonb  *      This product includes software developed for the NetBSD Project by
51ca42af5eSsimonb  *      Wasabi Systems, Inc.
52ca42af5eSsimonb  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
53ca42af5eSsimonb  *    or promote products derived from this software without specific prior
54ca42af5eSsimonb  *    written permission.
55ca42af5eSsimonb  *
56ca42af5eSsimonb  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
57ca42af5eSsimonb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58ca42af5eSsimonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59ca42af5eSsimonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
60ca42af5eSsimonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61ca42af5eSsimonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62ca42af5eSsimonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63ca42af5eSsimonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64ca42af5eSsimonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65ca42af5eSsimonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66ca42af5eSsimonb  * POSSIBILITY OF SUCH DAMAGE.
67ca42af5eSsimonb  */
68ca42af5eSsimonb 
69d375c80eSgdamore 
704b2744bfSlukem #include <sys/cdefs.h>
71*f58fcf6aSkiyohara __KERNEL_RCSID(0, "$NetBSD: aurtc.c,v 1.14 2012/01/03 07:36:02 kiyohara Exp $");
724b2744bfSlukem 
73ca42af5eSsimonb #include <sys/param.h>
74ca42af5eSsimonb #include <sys/systm.h>
75d375c80eSgdamore #include <sys/errno.h>
76ca42af5eSsimonb #include <sys/device.h>
77d375c80eSgdamore #include <sys/proc.h>
78ca42af5eSsimonb 
79ca42af5eSsimonb #include <dev/clock_subr.h>
80ca42af5eSsimonb 
81e265f67bSdyoung #include <sys/bus.h>
82d375c80eSgdamore 
83ca42af5eSsimonb #include <mips/alchemy/include/aureg.h>
84d375c80eSgdamore #include <mips/alchemy/include/auvar.h>
85ca42af5eSsimonb #include <mips/alchemy/include/aubusvar.h>
86ca42af5eSsimonb 
87d375c80eSgdamore #define	REGVAL(x) (*(volatile uint32_t *)(MIPS_PHYS_TO_KSEG1(PC_BASE + (x))))
88d375c80eSgdamore #define	GETREG(x)	(REGVAL(x))
89d375c80eSgdamore #define	PUTREG(x,v)	(REGVAL(x) = (v))
90d375c80eSgdamore 
91d375c80eSgdamore struct aurtc_softc {
92*f58fcf6aSkiyohara 	device_t		sc_dev;
93d375c80eSgdamore 	struct todr_chip_handle	sc_tch;
94d375c80eSgdamore 	void			*sc_shutdownhook;
95d375c80eSgdamore };
96d375c80eSgdamore 
97*f58fcf6aSkiyohara static int	aurtc_match(device_t, struct cfdata *, void *);
98*f58fcf6aSkiyohara static void	aurtc_attach(device_t, device_t, void *);
99471e528bStsutsui static int	aurtc_gettime(todr_chip_handle_t, struct timeval *);
100471e528bStsutsui static int	aurtc_settime(todr_chip_handle_t, struct timeval *);
101d375c80eSgdamore static void	aurtc_shutdown(void *);
102ca42af5eSsimonb 
103*f58fcf6aSkiyohara CFATTACH_DECL_NEW(aurtc, sizeof (struct aurtc_softc),
10489bf5a8fSthorpej     aurtc_match, aurtc_attach, NULL, NULL);
105ca42af5eSsimonb 
106ca42af5eSsimonb int
aurtc_match(device_t parent,struct cfdata * match,void * aux)107*f58fcf6aSkiyohara aurtc_match(device_t parent, struct cfdata *match, void *aux)
108ca42af5eSsimonb {
109ca42af5eSsimonb 	struct aubus_attach_args *aa = aux;
110ca42af5eSsimonb 
111d1ad2ac4Sthorpej 	if (strcmp(aa->aa_name, match->cf_name) == 0)
112ca42af5eSsimonb 		return (1);
113ca42af5eSsimonb 
114ca42af5eSsimonb 	return (0);
115ca42af5eSsimonb }
116ca42af5eSsimonb 
117ca42af5eSsimonb void
aurtc_attach(device_t parent,device_t self,void * aux)118*f58fcf6aSkiyohara aurtc_attach(device_t parent, device_t self, void *aux)
119ca42af5eSsimonb {
120*f58fcf6aSkiyohara 	struct aurtc_softc *sc = device_private(self);
121ca42af5eSsimonb 
122d375c80eSgdamore 	printf(": Au1X00 programmable clock\n");
123ca42af5eSsimonb 
124*f58fcf6aSkiyohara 	sc->sc_dev = self;
125d375c80eSgdamore 	sc->sc_tch.cookie = sc;
126d375c80eSgdamore 	sc->sc_tch.bus_cookie = NULL;
127d375c80eSgdamore 	sc->sc_tch.todr_gettime = aurtc_gettime;
128d375c80eSgdamore 	sc->sc_tch.todr_settime = aurtc_settime;
129d375c80eSgdamore 	sc->sc_tch.todr_setwen = NULL;
130d375c80eSgdamore 
131d375c80eSgdamore 	sc->sc_shutdownhook = shutdownhook_establish(aurtc_shutdown, NULL);
132203fcf4dSgdamore 
133203fcf4dSgdamore 	todr_attach(&sc->sc_tch);
134ca42af5eSsimonb }
135ca42af5eSsimonb 
136ca42af5eSsimonb /*
137d375c80eSgdamore  * Note that our RTC only has second resolution.
138ca42af5eSsimonb  */
139ca42af5eSsimonb 
140d375c80eSgdamore int
aurtc_gettime(todr_chip_handle_t tch,struct timeval * tv)141471e528bStsutsui aurtc_gettime(todr_chip_handle_t tch, struct timeval *tv)
142ca42af5eSsimonb {
143d375c80eSgdamore 	int			s;
144ca42af5eSsimonb 
145d375c80eSgdamore 	s = splclock();
146d375c80eSgdamore 	tv->tv_sec = GETREG(PC_COUNTER_READ_0);
147d375c80eSgdamore 	splx(s);
148d375c80eSgdamore 	return 0;
149ca42af5eSsimonb }
150ca42af5eSsimonb 
151d375c80eSgdamore int
aurtc_settime(todr_chip_handle_t tch,struct timeval * tvp)152471e528bStsutsui aurtc_settime(todr_chip_handle_t tch, struct timeval *tvp)
153ca42af5eSsimonb {
154d375c80eSgdamore 	int			s;
155d375c80eSgdamore 	struct timeval		tv;
15608f478e5Sgdamore 
157d375c80eSgdamore 	s = splclock();
158d375c80eSgdamore 	tv = *tvp;
159d375c80eSgdamore 	splx(s);
160ca42af5eSsimonb 
161d375c80eSgdamore 	/* wait for the clock register to be idle */
162d375c80eSgdamore 	while (GETREG(PC_COUNTER_CONTROL) & CC_C0S) {
16308f478e5Sgdamore 		continue;
164d375c80eSgdamore 	}
165d375c80eSgdamore 
166d375c80eSgdamore 	PUTREG(PC_COUNTER_WRITE0, tv.tv_sec);
167d375c80eSgdamore 
168d375c80eSgdamore 	/*
169d375c80eSgdamore 	 * It could take a second or two for the clock change to take effect.
170d375c80eSgdamore 	 * We don't want to make settimeofday() take that long, so we don't
171d375c80eSgdamore 	 * wait here, but instead wait in flush.  This can have a bad effect
172d375c80eSgdamore 	 * for settimeofday() calls with a short window between them.
173d375c80eSgdamore 	 */
174d375c80eSgdamore 	return 0;
175d375c80eSgdamore }
176d375c80eSgdamore 
177d375c80eSgdamore void
aurtc_shutdown(void * arg)178d375c80eSgdamore aurtc_shutdown(void *arg)
179d375c80eSgdamore {
180d375c80eSgdamore 
181d375c80eSgdamore 	/* wait for the clock register to be idle */
182d375c80eSgdamore 	while (GETREG(PC_COUNTER_CONTROL) & CC_C0S) {
18308f478e5Sgdamore 		continue;
184ca42af5eSsimonb 	}
185d375c80eSgdamore }
186