mkclock.c revision 1.2.8.1 1 /* $NetBSD: mkclock.c,v 1.2.8.1 2002/10/18 02:38:54 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Wayne Knowles
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/systm.h>
43
44 #include <dev/clock_subr.h>
45
46 #include <machine/cpu.h>
47 #include <machine/mainboard.h>
48 #include <machine/autoconf.h>
49 #include <machine/sysconf.h>
50 #include <machine/bus.h>
51
52 #include <mipsco/obio/clockreg.h>
53
54 struct mkclock_softc {
55 struct device dev;
56 bus_space_tag_t sc_bst;
57 bus_space_handle_t sc_bsh;
58 };
59
60 static int mkclock_match (struct device *, struct cfdata *, void *);
61 static void mkclock_attach (struct device *, struct device *, void *);
62
63 CFATTACH_DECL(mkclock, sizeof(struct mkclock_softc),
64 mkclock_match, mkclock_attach, NULL, NULL);
65
66 static struct mkclock_softc *mk0;
67
68 void mkclock_read (struct clock_ymdhms *);
69 void mkclock_write (struct clock_ymdhms *);
70
71 static int mk_read (struct mkclock_softc *, int);
72 static void mk_write (struct mkclock_softc *, int, int);
73
74 int
75 mkclock_match(parent, cf, aux)
76 struct device *parent;
77 struct cfdata *cf;
78 void *aux;
79 {
80 return 1;
81 }
82
83 void
84 mkclock_attach(parent, self, aux)
85 struct device *parent, *self;
86 void *aux;
87 {
88 struct mkclock_softc *sc = (void *)self;
89 struct confargs *ca = aux;
90
91 sc->sc_bst = ca->ca_bustag;
92 if (bus_space_map(ca->ca_bustag, ca->ca_addr,
93 0x10000,
94 BUS_SPACE_MAP_LINEAR,
95 &sc->sc_bsh) != 0) {
96 printf(": cannot map registers\n");
97 return;
98 }
99
100 platform.write_todr = mkclock_write;
101 platform.read_todr = mkclock_read;
102 mk0 = sc;
103 printf("\n");
104 }
105
106 static int
107 mk_read(sc, reg)
108 struct mkclock_softc *sc;
109 int reg;
110 {
111 u_int8_t val;
112
113 val = bus_space_read_1(sc->sc_bst, sc->sc_bsh, DATA_PORT + reg*4);
114 return FROMBCD(val);
115 }
116
117 static void
118 mk_write(sc, reg, val)
119 struct mkclock_softc *sc;
120 int reg, val;
121 {
122 bus_space_write_1(sc->sc_bst, sc->sc_bsh,
123 DATA_PORT + reg*4, TOBCD(val));
124 }
125
126 void
127 mkclock_read(dt)
128 struct clock_ymdhms *dt;
129 {
130 struct mkclock_softc *sc = mk0;
131 int s = splclock();
132
133 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, READ_CLOCK);
134 dt->dt_sec = mk_read(sc, 0);
135 dt->dt_min = mk_read(sc, 1);
136 dt->dt_hour = mk_read(sc, 2);
137 dt->dt_wday = mk_read(sc, 3);
138 dt->dt_day = mk_read(sc, 4);
139 dt->dt_mon = mk_read(sc, 5);
140 dt->dt_year = mk_read(sc, 6);
141 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
142 splx(s);
143
144 dt->dt_year = dt->dt_year + (dt->dt_year >= 70 ? 1900 : 2000);
145 }
146
147 void
148 mkclock_write(dt)
149 struct clock_ymdhms *dt;
150 {
151 struct mkclock_softc *sc = mk0;
152 int year, s;
153
154 year = dt->dt_year % 100;
155
156 s = splclock();
157 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, SET_CLOCK);
158 mk_write(sc, 0, dt->dt_sec);
159 mk_write(sc, 1, dt->dt_min);
160 mk_write(sc, 2, dt->dt_hour);
161 /* week day not set */
162 mk_write(sc, 4, dt->dt_day);
163 mk_write(sc, 5, dt->dt_mon);
164 mk_write(sc, 6, year);
165 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
166 splx(s);
167 }
168