1 /* $NetBSD: tsrtc.c,v 1.5 2008/03/29 05:42:45 tsutsui Exp $ */
4 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
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 *);
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
);
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
;
73 static int found
= -1;
80 tsc
->sc_iot
= aa
->ta_iot
;
81 if (bus_space_map(tsc
->sc_iot
, TS7XXX_IO8_HWBASE
+ TS7XXX_RTCIDX
, 1, 0,
84 if (bus_space_map(tsc
->sc_iot
, TS7XXX_IO8_HWBASE
+ TS7XXX_RTCDAT
, 1, 0,
88 /* Read from the seconds counter. */
89 t1
= FROMBCD(tsrtc_read(sc
, MC_SEC
));
93 /* Wait, then look again. */
95 t2
= FROMBCD(tsrtc_read(sc
, MC_SEC
));
99 /* If [1,2) seconds have passed since, call it a clock. */
100 if ((t1
+ 1) % 60 == t2
|| (t1
+ 2) % 60 == t2
)
104 bus_space_unmap(tsc
->sc_iot
, tsc
->sc_idxh
, 1);
105 bus_space_unmap(tsc
->sc_iot
, tsc
->sc_dath
, 1);
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
;
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");
127 sc
->sc_mcread
= tsrtc_read
;
128 sc
->sc_mcwrite
= tsrtc_write
;
129 sc
->sc_flag
= MC146818_BCD
;
134 (*sc
->sc_mcwrite
)(sc
, MC_REGB
, MC_REGB_24HR
);
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
);
147 tsrtc_read(struct mc146818_softc
*mc_sc
, u_int reg
)
149 struct tsrtc_softc
*sc
= (struct tsrtc_softc
*)mc_sc
;
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);