tsrtc.c revision 1.1 1 1.1 joff /* $NetBSD: tsrtc.c,v 1.1 2004/12/27 02:41:54 joff Exp $ */
2 1.1 joff
3 1.1 joff /*
4 1.1 joff * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 1.1 joff * All rights reserved.
6 1.1 joff *
7 1.1 joff * Author: Chris G. Demetriou
8 1.1 joff *
9 1.1 joff * Permission to use, copy, modify and distribute this software and
10 1.1 joff * its documentation is hereby granted, provided that both the copyright
11 1.1 joff * notice and this permission notice appear in all copies of the
12 1.1 joff * software, derivative works or modified versions, and any portions
13 1.1 joff * thereof, and that both notices appear in supporting documentation.
14 1.1 joff *
15 1.1 joff * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 1.1 joff * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 1.1 joff * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 1.1 joff *
19 1.1 joff * Carnegie Mellon requests users of this software to return to
20 1.1 joff *
21 1.1 joff * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 1.1 joff * School of Computer Science
23 1.1 joff * Carnegie Mellon University
24 1.1 joff * Pittsburgh PA 15213-3890
25 1.1 joff *
26 1.1 joff * any improvements or extensions that they make and grant Carnegie the
27 1.1 joff * rights to redistribute these changes.
28 1.1 joff */
29 1.1 joff
30 1.1 joff #include <sys/cdefs.h>
31 1.1 joff __KERNEL_RCSID(0, "$NetBSD: tsrtc.c,v 1.1 2004/12/27 02:41:54 joff Exp $");
32 1.1 joff
33 1.1 joff #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
34 1.1 joff
35 1.1 joff #include <sys/param.h>
36 1.1 joff #include <sys/kernel.h>
37 1.1 joff #include <sys/systm.h>
38 1.1 joff #include <sys/device.h>
39 1.1 joff
40 1.1 joff #include <machine/bus.h>
41 1.1 joff
42 1.1 joff #include <dev/clock_subr.h>
43 1.1 joff #include <dev/ic/mc146818reg.h>
44 1.1 joff #include <dev/ic/mc146818var.h>
45 1.1 joff
46 1.1 joff #include <evbarm/tsarm/tspldvar.h>
47 1.1 joff #include <evbarm/tsarm/tsarmreg.h>
48 1.1 joff
49 1.1 joff int tsrtc_match __P((struct device *, struct cfdata *, void *));
50 1.1 joff void tsrtc_attach __P((struct device *, struct device *, void *));
51 1.1 joff
52 1.1 joff struct tsrtc_softc {
53 1.1 joff struct mc146818_softc sc_mc;
54 1.1 joff bus_space_tag_t sc_iot;
55 1.1 joff bus_space_handle_t sc_dath;
56 1.1 joff bus_space_handle_t sc_idxh;
57 1.1 joff };
58 1.1 joff
59 1.1 joff CFATTACH_DECL(tsrtc, sizeof (struct tsrtc_softc),
60 1.1 joff tsrtc_match, tsrtc_attach, NULL, NULL);
61 1.1 joff
62 1.1 joff void tsrtc_write __P((struct mc146818_softc *, u_int, u_int));
63 1.1 joff u_int tsrtc_read __P((struct mc146818_softc *, u_int));
64 1.1 joff
65 1.1 joff
66 1.1 joff int
67 1.1 joff tsrtc_match(parent, match, aux)
68 1.1 joff struct device *parent;
69 1.1 joff struct cfdata *match;
70 1.1 joff void *aux;
71 1.1 joff {
72 1.1 joff struct tspld_attach_args *aa = aux;
73 1.1 joff struct tsrtc_softc tsrtc, *sc;
74 1.1 joff unsigned int t1, t2;
75 1.1 joff static int found = -1;
76 1.1 joff
77 1.1 joff if (found != -1) return found;
78 1.1 joff
79 1.1 joff sc = &tsrtc;
80 1.1 joff sc->sc_iot = aa->ta_iot;
81 1.1 joff if (bus_space_map(sc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCIDX, 1, 0,
82 1.1 joff &sc->sc_idxh))
83 1.1 joff return (0);
84 1.1 joff if (bus_space_map(sc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCDAT, 1, 0,
85 1.1 joff &sc->sc_dath))
86 1.1 joff return (0);
87 1.1 joff
88 1.1 joff /* Read from the seconds counter. */
89 1.1 joff t1 = FROMBCD(tsrtc_read((struct mc146818_softc *)sc, MC_SEC));
90 1.1 joff if (t1 > 59)
91 1.1 joff goto unmap;
92 1.1 joff
93 1.1 joff /* Wait, then look again. */
94 1.1 joff DELAY(1100000);
95 1.1 joff t2 = FROMBCD(tsrtc_read((struct mc146818_softc *)sc, MC_SEC));
96 1.1 joff if (t2 > 59)
97 1.1 joff goto unmap;
98 1.1 joff
99 1.1 joff /* If [1,2) seconds have passed since, call it a clock. */
100 1.1 joff if ((t1 + 1) % 60 == t2 || (t1 + 2) % 60 == t2)
101 1.1 joff found = 1;
102 1.1 joff
103 1.1 joff unmap:
104 1.1 joff bus_space_unmap(sc->sc_iot, sc->sc_idxh, 1);
105 1.1 joff bus_space_unmap(sc->sc_iot, sc->sc_dath, 1);
106 1.1 joff
107 1.1 joff return (found);
108 1.1 joff }
109 1.1 joff
110 1.1 joff void
111 1.1 joff tsrtc_attach(parent, self, aux)
112 1.1 joff struct device *parent, *self;
113 1.1 joff void *aux;
114 1.1 joff {
115 1.1 joff struct tsrtc_softc *sc = (void *)self;
116 1.1 joff struct tspld_attach_args *aa = aux;
117 1.1 joff
118 1.1 joff sc->sc_iot = aa->ta_iot;
119 1.1 joff if (bus_space_map(sc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCIDX,
120 1.1 joff 1, 0, &sc->sc_idxh))
121 1.1 joff panic("tsrtc_attach: couldn't map clock I/O space");
122 1.1 joff if (bus_space_map(sc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCDAT,
123 1.1 joff 1, 0, &sc->sc_dath))
124 1.1 joff panic("tsrtc_attach: couldn't map clock I/O space");
125 1.1 joff
126 1.1 joff sc->sc_mc.sc_year0 = 2000;
127 1.1 joff sc->sc_mc.sc_mcread = tsrtc_read;
128 1.1 joff sc->sc_mc.sc_mcwrite = tsrtc_write;
129 1.1 joff sc->sc_mc.sc_flag = MC146818_BCD;
130 1.1 joff mc146818_attach(&sc->sc_mc);
131 1.1 joff
132 1.1 joff printf("\n");
133 1.1 joff
134 1.1 joff (*sc->sc_mc.sc_mcwrite)((struct mc146818_softc *)sc, MC_REGB,
135 1.1 joff MC_REGB_24HR);
136 1.1 joff
137 1.1 joff todr_attach(&sc->sc_mc.sc_handle);
138 1.1 joff }
139 1.1 joff
140 1.1 joff void
141 1.1 joff tsrtc_write(mc_sc, reg, datum)
142 1.1 joff struct mc146818_softc *mc_sc;
143 1.1 joff u_int reg, datum;
144 1.1 joff {
145 1.1 joff struct tsrtc_softc *sc = (struct tsrtc_softc *)mc_sc;
146 1.1 joff
147 1.1 joff bus_space_write_1(sc->sc_iot, sc->sc_idxh, 0, reg);
148 1.1 joff bus_space_write_1(sc->sc_iot, sc->sc_dath, 0, datum);
149 1.1 joff }
150 1.1 joff
151 1.1 joff u_int
152 1.1 joff tsrtc_read(mc_sc, reg)
153 1.1 joff struct mc146818_softc *mc_sc;
154 1.1 joff u_int reg;
155 1.1 joff {
156 1.1 joff struct tsrtc_softc *sc = (struct tsrtc_softc *)mc_sc;
157 1.1 joff u_int datum;
158 1.1 joff
159 1.1 joff bus_space_write_1(sc->sc_iot, sc->sc_idxh, 0, reg);
160 1.1 joff datum = bus_space_read_1(sc->sc_iot, sc->sc_dath, 0);
161 1.1 joff
162 1.1 joff return (datum);
163 1.1 joff }
164