mcclock.c revision 1.1 1 /* $NetBSD: mcclock.c,v 1.1 2006/04/15 13:33:05 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: mcclock.c,v 1.1 2006/04/15 13:33:05 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/autoconf.h>
41 #include <machine/bus.h>
42
43 #include <dev/clock_subr.h>
44 #include <dev/ic/mc146818reg.h>
45 #include <dev/ic/mc146818var.h>
46
47 #define MCCLOCK_NPORTS 2
48
49 static int mcclock_match(struct device *, struct cfdata *, void *);
50 static void mcclock_attach(struct device *, struct device *, void *);
51
52 CFATTACH_DECL(mcclock, sizeof (struct mc146818_softc),
53 mcclock_match, mcclock_attach, NULL, NULL);
54
55 static void mcclock_write(struct mc146818_softc *, u_int, u_int);
56 static u_int mcclock_read(struct mc146818_softc *, u_int);
57
58
59 static int
60 mcclock_match(struct device *parent, struct cfdata *match, void *aux)
61 {
62 static int mcclock_found;
63
64 if (mcclock_found)
65 return 0;
66
67 mcclock_found = 1;
68
69 return 1;
70 }
71
72 static void
73 mcclock_attach(struct device *parent, struct device *self, void *aux)
74 {
75 struct mc146818_softc *sc = (void *)self;
76 struct mainbus_attach_args *ma = aux;
77
78 sc->sc_bst = ma->ma_iot;
79 if (bus_space_map(sc->sc_bst, ma->ma_addr, MCCLOCK_NPORTS,
80 0, &sc->sc_bsh)) {
81 aprint_error("mcclock_attach: unable to map registers\n");
82 return;
83 }
84
85 sc->sc_year0 = 2000;
86 sc->sc_mcread = mcclock_read;
87 sc->sc_mcwrite = mcclock_write;
88 sc->sc_flag = MC146818_BCD;
89 mc146818_attach(sc);
90
91 printf("\n");
92
93 (*sc->sc_mcwrite)(sc, MC_REGB, MC_REGB_24HR);
94
95 todr_attach(&sc->sc_handle);
96 }
97
98 static void
99 mcclock_write(struct mc146818_softc *sc, u_int reg, u_int datum)
100 {
101 bus_space_tag_t iot;
102 bus_space_handle_t ioh;
103
104 iot = sc->sc_bst;
105 ioh = sc->sc_bsh;
106
107 bus_space_write_1(iot, ioh, 0, reg);
108 bus_space_write_1(iot, ioh, 1, datum);
109 }
110
111 static u_int
112 mcclock_read(struct mc146818_softc *sc, u_int reg)
113 {
114 bus_space_tag_t iot;
115 bus_space_handle_t ioh;
116 u_int datum;
117
118 iot = sc->sc_bst;
119 ioh = sc->sc_bsh;
120
121 bus_space_write_1(iot, ioh, 0, reg);
122 datum = bus_space_read_1(iot, ioh, 1);
123
124 return datum;
125 }
126