1/*	$NetBSD: rtc.c,v 1.7 2010/03/11 03:54:56 mrg Exp $	*/
2
3/*
4 * Copyright (c) 1992, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 * Copyright (c) 1994 Gordon W. Ross
7 * Copyright (c) 1993 Adam Glass
8 * Copyright (c) 1996 Paul Kranenburg
9 * Copyright (c) 1996
10 * 	The President and Fellows of Harvard College. All rights reserved.
11 *
12 * This software was developed by the Computer Systems Engineering group
13 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
14 * contributed to Berkeley.
15 *
16 * All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 *	This product includes software developed by Harvard University.
19 *	This product includes software developed by the University of
20 *	California, Lawrence Berkeley Laboratory.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 *
26 * 1. Redistributions of source code must retain the above copyright
27 *    notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 *    notice, this list of conditions and the following disclaimer in the
30 *    documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 *    must display the following acknowledgement:
33 *	This product includes software developed by the University of
34 *	California, Berkeley and its contributors.
35 *	This product includes software developed by Paul Kranenburg.
36 *	This product includes software developed by Harvard University.
37 * 4. Neither the name of the University nor the names of its contributors
38 *    may be used to endorse or promote products derived from this software
39 *    without specific prior written permission.
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 *	@(#)clock.c	8.1 (Berkeley) 6/11/93
54 * from: NetBSD: clock.c,v 1.80 2006/09/03 22:27:45 gdamore Exp
55 *
56 */
57
58#include <sys/cdefs.h>
59__KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.7 2010/03/11 03:54:56 mrg Exp $");
60
61/*
62 * Clock driver for 'rtc' - mc146818 driver.
63 */
64
65#include <sys/param.h>
66#include <sys/kernel.h>
67#include <sys/device.h>
68#include <sys/proc.h>
69
70#include <sys/bus.h>
71#include <machine/autoconf.h>
72
73#include <dev/clock_subr.h>
74#include <dev/ic/mc146818reg.h>
75#include <dev/ic/mc146818var.h>
76
77#include <dev/ebus/ebusreg.h>
78#include <dev/ebus/ebusvar.h>
79
80static int	rtc_ebus_match(device_t, cfdata_t, void *);
81static void	rtc_ebus_attach(device_t, device_t, void *);
82
83CFATTACH_DECL_NEW(rtc_ebus, sizeof(struct mc146818_softc),
84    rtc_ebus_match, rtc_ebus_attach, NULL, NULL);
85
86u_int rtc_read_reg(struct mc146818_softc *, u_int);
87void rtc_write_reg(struct mc146818_softc *, u_int, u_int);
88u_int rtc_getcent(struct mc146818_softc *);
89void rtc_setcent(struct mc146818_softc *, u_int);
90
91static int
92rtc_ebus_match(device_t parent, cfdata_t cf, void *aux)
93{
94	struct ebus_attach_args *ea = aux;
95
96	return (strcmp("rtc", ea->ea_name) == 0);
97}
98
99/*
100 * `rtc' is a ds1287 on an ebus (actually an isa bus, but we use the
101 * ebus driver for isa.)  So we can use ebus_wenable() but need to do
102 * different attach work and use different todr routines.  It does not
103 * incorporate an IDPROM.
104 */
105
106/*
107 * XXX the stupid ds1287 is not mapped directly but uses an address
108 * and a data reg so we cannot access the stuuupid thing w/o having
109 * write access to the registers.
110 *
111 * XXXX We really need to mutex register access!
112 */
113#define	RTC_ADDR	0
114#define	RTC_DATA	1
115u_int
116rtc_read_reg(struct mc146818_softc *sc, u_int reg)
117{
118
119	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_ADDR, reg);
120	return (bus_space_read_1(sc->sc_bst, sc->sc_bsh, RTC_DATA));
121}
122void
123rtc_write_reg(struct mc146818_softc *sc, u_int reg, u_int val)
124{
125
126	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_ADDR, reg);
127	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_DATA, val);
128}
129
130/* ARGSUSED */
131static void
132rtc_ebus_attach(device_t parent, device_t self, void *aux)
133{
134	struct mc146818_softc *sc = device_private(self);
135	struct ebus_attach_args *ea = aux;
136	char *model;
137	int sz;
138
139	sc->sc_dev = self;
140	sc->sc_bst = ea->ea_bustag;
141
142	/* hard code to 8K? */
143	sz = ea->ea_reg[0].size;
144
145	if (bus_space_map(sc->sc_bst,
146			 EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
147			 sz, 0,
148			 &sc->sc_bsh) != 0) {
149		aprint_error(": can't map register\n");
150		return;
151	}
152
153	model = prom_getpropstring(ea->ea_node, "model");
154#ifdef DIAGNOSTIC
155	if (model == NULL)
156		panic("clockattach_rtc: no model property");
157#endif
158
159	/* Our TOD clock year 0 is 0 */
160	sc->sc_year0 = 0;
161	sc->sc_flag = MC146818_NO_CENT_ADJUST;
162	sc->sc_mcread = rtc_read_reg;
163	sc->sc_mcwrite = rtc_write_reg;
164	sc->sc_getcent = rtc_getcent;
165	sc->sc_setcent = rtc_setcent;
166	mc146818_attach(sc);
167
168	aprint_normal(": %s\n", model);
169	aprint_naive(": Clock\n");
170
171	/*
172	 * Turn interrupts off, just in case. (Although they shouldn't
173	 * be wired to an interrupt controller on sparcs).
174	 */
175	rtc_write_reg(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR);
176
177	/*
178	 * Apparently on some machines the TOD registers are on the same
179	 * physical page as the COM registers.  So we won't protect them.
180	 */
181	/*sc->sc_handle.todr_setwen = NULL;*/
182}
183
184/*
185 * MD mc146818 RTC todr routines.
186 */
187
188/* Loooks like Sun stores the century info somewhere in CMOS RAM */
189#define MC_CENT 0x32
190
191u_int
192rtc_getcent(struct mc146818_softc *sc)
193{
194
195	return rtc_read_reg(sc, MC_CENT);
196}
197void
198rtc_setcent(struct mc146818_softc *sc, u_int cent)
199{
200
201	rtc_write_reg(sc, MC_CENT, cent);
202}
203