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