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