No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / evbarm / tsarm / tsrtc.c
blob16600f22d8f1675638c6c8739148707d6ac30165
1 /* $NetBSD: tsrtc.c,v 1.5 2008/03/29 05:42:45 tsutsui Exp $ */
3 /*
4 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 * All rights reserved.
7 * Author: Chris G. Demetriou
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19 * Carnegie Mellon requests users of this software to return to
21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: tsrtc.c,v 1.5 2008/03/29 05:42:45 tsutsui Exp $");
33 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
40 #include <machine/bus.h>
42 #include <dev/clock_subr.h>
43 #include <dev/ic/mc146818reg.h>
44 #include <dev/ic/mc146818var.h>
46 #include <evbarm/tsarm/tspldvar.h>
47 #include <evbarm/tsarm/tsarmreg.h>
49 int tsrtc_match(device_t, cfdata_t, void *);
50 void tsrtc_attach(device_t, device_t, void *);
52 struct tsrtc_softc {
53 struct mc146818_softc sc_mc;
54 bus_space_tag_t sc_iot;
55 bus_space_handle_t sc_dath;
56 bus_space_handle_t sc_idxh;
59 CFATTACH_DECL_NEW(tsrtc, sizeof (struct tsrtc_softc),
60 tsrtc_match, tsrtc_attach, NULL, NULL);
62 void tsrtc_write(struct mc146818_softc *, u_int, u_int);
63 u_int tsrtc_read(struct mc146818_softc *, u_int);
66 int
67 tsrtc_match(device_t parent, cfdata_t cf, void *aux)
69 struct tspld_attach_args *aa = aux;
70 struct tsrtc_softc tsrtc, *tsc;
71 struct mc146818_softc *sc;
72 unsigned int t1, t2;
73 static int found = -1;
75 if (found != -1)
76 return found;
78 tsc = &tsrtc;
79 sc = &tsc->sc_mc;
80 tsc->sc_iot = aa->ta_iot;
81 if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCIDX, 1, 0,
82 &tsc->sc_idxh))
83 return (0);
84 if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCDAT, 1, 0,
85 &tsc->sc_dath))
86 return (0);
88 /* Read from the seconds counter. */
89 t1 = FROMBCD(tsrtc_read(sc, MC_SEC));
90 if (t1 > 59)
91 goto unmap;
93 /* Wait, then look again. */
94 DELAY(1100000);
95 t2 = FROMBCD(tsrtc_read(sc, MC_SEC));
96 if (t2 > 59)
97 goto unmap;
99 /* If [1,2) seconds have passed since, call it a clock. */
100 if ((t1 + 1) % 60 == t2 || (t1 + 2) % 60 == t2)
101 found = 1;
103 unmap:
104 bus_space_unmap(tsc->sc_iot, tsc->sc_idxh, 1);
105 bus_space_unmap(tsc->sc_iot, tsc->sc_dath, 1);
107 return (found);
110 void
111 tsrtc_attach(device_t parent, device_t self, void *aux)
113 struct tsrtc_softc *tsc = device_private(self);
114 struct mc146818_softc *sc = &tsc->sc_mc;
115 struct tspld_attach_args *aa = aux;
117 sc->sc_dev = self;
118 tsc->sc_iot = aa->ta_iot;
119 if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCIDX,
120 1, 0, &tsc->sc_idxh))
121 panic("tsrtc_attach: couldn't map clock I/O space");
122 if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCDAT,
123 1, 0, &tsc->sc_dath))
124 panic("tsrtc_attach: couldn't map clock I/O space");
126 sc->sc_year0 = 2000;
127 sc->sc_mcread = tsrtc_read;
128 sc->sc_mcwrite = tsrtc_write;
129 sc->sc_flag = MC146818_BCD;
130 mc146818_attach(sc);
132 aprint_normal("\n");
134 (*sc->sc_mcwrite)(sc, MC_REGB, MC_REGB_24HR);
137 void
138 tsrtc_write(struct mc146818_softc *mc_sc, u_int reg, u_int datum)
140 struct tsrtc_softc *sc = (struct tsrtc_softc *)mc_sc;
142 bus_space_write_1(sc->sc_iot, sc->sc_idxh, 0, reg);
143 bus_space_write_1(sc->sc_iot, sc->sc_dath, 0, datum);
146 u_int
147 tsrtc_read(struct mc146818_softc *mc_sc, u_int reg)
149 struct tsrtc_softc *sc = (struct tsrtc_softc *)mc_sc;
150 u_int datum;
152 bus_space_write_1(sc->sc_iot, sc->sc_idxh, 0, reg);
153 datum = bus_space_read_1(sc->sc_iot, sc->sc_dath, 0);
155 return (datum);