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