mkclock.c revision 1.1.2.3 1 /* $NetBSD: mkclock.c,v 1.1.2.3 2000/12/08 09:28:26 bouyer 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 struct cfattach mkclock_ca = {
64 sizeof(struct mkclock_softc), mkclock_match, mkclock_attach
65 };
66
67 static struct mkclock_softc *mk0;
68
69 void mkclock_read (struct clock_ymdhms *);
70 void mkclock_write (struct clock_ymdhms *);
71
72 static int mk_read (struct mkclock_softc *, int);
73 static void mk_write (struct mkclock_softc *, int, int);
74
75 int
76 mkclock_match(parent, cf, aux)
77 struct device *parent;
78 struct cfdata *cf;
79 void *aux;
80 {
81 return 1;
82 }
83
84 void
85 mkclock_attach(parent, self, aux)
86 struct device *parent, *self;
87 void *aux;
88 {
89 struct mkclock_softc *sc = (void *)self;
90 struct confargs *ca = aux;
91
92 sc->sc_bst = ca->ca_bustag;
93 if (bus_space_map(ca->ca_bustag, ca->ca_addr,
94 0x10000,
95 BUS_SPACE_MAP_LINEAR,
96 &sc->sc_bsh) != 0) {
97 printf(": cannot map registers\n");
98 return;
99 }
100
101 platform.write_todr = mkclock_write;
102 platform.read_todr = mkclock_read;
103 mk0 = sc;
104 printf("\n");
105 }
106
107 static int
108 mk_read(sc, reg)
109 struct mkclock_softc *sc;
110 int reg;
111 {
112 u_int8_t val;
113
114 val = bus_space_read_1(sc->sc_bst, sc->sc_bsh, DATA_PORT + reg*4);
115 return FROMBCD(val);
116 }
117
118 static void
119 mk_write(sc, reg, val)
120 struct mkclock_softc *sc;
121 int reg, val;
122 {
123 bus_space_write_1(sc->sc_bst, sc->sc_bsh,
124 DATA_PORT + reg*4, TOBCD(val));
125 }
126
127 void
128 mkclock_read(dt)
129 struct clock_ymdhms *dt;
130 {
131 struct mkclock_softc *sc = mk0;
132 int s = splclock();
133
134 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, READ_CLOCK);
135 dt->dt_sec = mk_read(sc, 0);
136 dt->dt_min = mk_read(sc, 1);
137 dt->dt_hour = mk_read(sc, 2);
138 dt->dt_wday = mk_read(sc, 3);
139 dt->dt_day = mk_read(sc, 4);
140 dt->dt_mon = mk_read(sc, 5);
141 dt->dt_year = mk_read(sc, 6);
142 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
143 splx(s);
144
145 dt->dt_year = dt->dt_year + (dt->dt_year >= 70 ? 1900 : 2000);
146 }
147
148 void
149 mkclock_write(dt)
150 struct clock_ymdhms *dt;
151 {
152 struct mkclock_softc *sc = mk0;
153 int year, s;
154
155 year = dt->dt_year % 100;
156
157 s = splclock();
158 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, SET_CLOCK);
159 mk_write(sc, 0, dt->dt_sec);
160 mk_write(sc, 1, dt->dt_min);
161 mk_write(sc, 2, dt->dt_hour);
162 /* week day not set */
163 mk_write(sc, 4, dt->dt_day);
164 mk_write(sc, 5, dt->dt_mon);
165 mk_write(sc, 6, year);
166 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
167 splx(s);
168 }
169