tsrtc.c revision 1.4 1 /* $NetBSD: tsrtc.c,v 1.4 2008/03/28 19:05:49 tsutsui Exp $ */
2
3 /*
4 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Author: Chris G. Demetriou
8 *
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.
14 *
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.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: tsrtc.c,v 1.4 2008/03/28 19:05:49 tsutsui Exp $");
32
33 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
34
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39
40 #include <machine/bus.h>
41
42 #include <dev/clock_subr.h>
43 #include <dev/ic/mc146818reg.h>
44 #include <dev/ic/mc146818var.h>
45
46 #include <evbarm/tsarm/tspldvar.h>
47 #include <evbarm/tsarm/tsarmreg.h>
48
49 int tsrtc_match(device_t, cfdata_t, void *);
50 void tsrtc_attach(device_t, device_t, void *);
51
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;
57 };
58
59 CFATTACH_DECL_NEW(tsrtc, sizeof (struct tsrtc_softc),
60 tsrtc_match, tsrtc_attach, NULL, NULL);
61
62 void tsrtc_write(struct mc146818_softc *, u_int, u_int);
63 u_int tsrtc_read(struct mc146818_softc *, u_int);
64
65
66 int
67 tsrtc_match(device_t parent, cfdata_t cf, void *aux)
68 {
69 struct tspld_attach_args *aa = aux;
70 struct tsrtc_softc tsrtc, *sc;
71 unsigned int t1, t2;
72 static int found = -1;
73
74 if (found != -1)
75 return found;
76
77 sc = &tsrtc;
78 sc->sc_iot = aa->ta_iot;
79 if (bus_space_map(sc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCIDX, 1, 0,
80 &sc->sc_idxh))
81 return (0);
82 if (bus_space_map(sc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCDAT, 1, 0,
83 &sc->sc_dath))
84 return (0);
85
86 /* Read from the seconds counter. */
87 t1 = FROMBCD(tsrtc_read((struct mc146818_softc *)sc, MC_SEC));
88 if (t1 > 59)
89 goto unmap;
90
91 /* Wait, then look again. */
92 DELAY(1100000);
93 t2 = FROMBCD(tsrtc_read((struct mc146818_softc *)sc, MC_SEC));
94 if (t2 > 59)
95 goto unmap;
96
97 /* If [1,2) seconds have passed since, call it a clock. */
98 if ((t1 + 1) % 60 == t2 || (t1 + 2) % 60 == t2)
99 found = 1;
100
101 unmap:
102 bus_space_unmap(sc->sc_iot, sc->sc_idxh, 1);
103 bus_space_unmap(sc->sc_iot, sc->sc_dath, 1);
104
105 return (found);
106 }
107
108 void
109 tsrtc_attach(device_t parent, device_t self, void *aux)
110 {
111 struct tsrtc_softc *sc = device_private(self);
112 struct tspld_attach_args *aa = aux;
113
114 sc->sc_iot = aa->ta_iot;
115 if (bus_space_map(sc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCIDX,
116 1, 0, &sc->sc_idxh))
117 panic("tsrtc_attach: couldn't map clock I/O space");
118 if (bus_space_map(sc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCDAT,
119 1, 0, &sc->sc_dath))
120 panic("tsrtc_attach: couldn't map clock I/O space");
121
122 sc->sc_mc.sc_year0 = 2000;
123 sc->sc_mc.sc_mcread = tsrtc_read;
124 sc->sc_mc.sc_mcwrite = tsrtc_write;
125 sc->sc_mc.sc_flag = MC146818_BCD;
126 mc146818_attach(&sc->sc_mc);
127
128 aprint_error("\n");
129
130 (*sc->sc_mc.sc_mcwrite)((struct mc146818_softc *)sc, MC_REGB,
131 MC_REGB_24HR);
132 }
133
134 void
135 tsrtc_write(struct mc146818_softc *mc_sc, u_int reg, u_int datum)
136 {
137 struct tsrtc_softc *sc = (struct tsrtc_softc *)mc_sc;
138
139 bus_space_write_1(sc->sc_iot, sc->sc_idxh, 0, reg);
140 bus_space_write_1(sc->sc_iot, sc->sc_dath, 0, datum);
141 }
142
143 u_int
144 tsrtc_read(struct mc146818_softc *mc_sc, u_int reg)
145 {
146 struct tsrtc_softc *sc = (struct tsrtc_softc *)mc_sc;
147 u_int datum;
148
149 bus_space_write_1(sc->sc_iot, sc->sc_idxh, 0, reg);
150 datum = bus_space_read_1(sc->sc_iot, sc->sc_dath, 0);
151
152 return (datum);
153 }
154