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