xref: /netbsd-src/sys/arch/mipsco/obio/mkclock.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: mkclock.c,v 1.6 2005/12/11 12:18:13 christos 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/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: mkclock.c,v 1.6 2005/12/11 12:18:13 christos Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/device.h>
45 #include <sys/systm.h>
46 
47 #include <dev/clock_subr.h>
48 
49 #include <machine/cpu.h>
50 #include <machine/mainboard.h>
51 #include <machine/autoconf.h>
52 #include <machine/sysconf.h>
53 #include <machine/bus.h>
54 
55 #include <mipsco/obio/clockreg.h>
56 
57 struct	mkclock_softc {
58         struct  device dev;
59 	bus_space_tag_t	sc_bst;
60 	bus_space_handle_t sc_bsh;
61 };
62 
63 static int mkclock_match (struct device *, struct cfdata *, void *);
64 static void mkclock_attach (struct device *, struct device *, void *);
65 
66 CFATTACH_DECL(mkclock, sizeof(struct mkclock_softc),
67     mkclock_match, mkclock_attach, NULL, NULL);
68 
69 static struct mkclock_softc *mk0;
70 
71 void mkclock_read (struct clock_ymdhms *);
72 void mkclock_write (struct clock_ymdhms *);
73 
74 static int mk_read (struct mkclock_softc *, int);
75 static void mk_write (struct mkclock_softc *, int, int);
76 
77 int
78 mkclock_match(parent, cf, aux)
79 	struct device *parent;
80 	struct cfdata *cf;
81 	void *aux;
82 {
83 	return 1;
84 }
85 
86 void
87 mkclock_attach(parent, self, aux)
88 	struct device *parent, *self;
89 	void *aux;
90 {
91         struct mkclock_softc *sc = (void *)self;
92 	struct confargs *ca = aux;
93 
94 	sc->sc_bst = ca->ca_bustag;
95 	if (bus_space_map(ca->ca_bustag, ca->ca_addr,
96 			  0x10000,
97 			  BUS_SPACE_MAP_LINEAR,
98 			  &sc->sc_bsh) != 0) {
99 		printf(": cannot map registers\n");
100 		return;
101 	}
102 
103 	platform.write_todr = mkclock_write;
104 	platform.read_todr  = mkclock_read;
105 	mk0 = sc;
106 	printf("\n");
107 }
108 
109 static int
110 mk_read(sc, reg)
111 	struct mkclock_softc *sc;
112 	int reg;
113 {
114 	u_int8_t val;
115 
116 	val = bus_space_read_1(sc->sc_bst, sc->sc_bsh, DATA_PORT + reg*4);
117 	return FROMBCD(val);
118 }
119 
120 static void
121 mk_write(sc, reg, val)
122 	struct mkclock_softc *sc;
123 	int reg, val;
124 {
125 	bus_space_write_1(sc->sc_bst, sc->sc_bsh,
126 			  DATA_PORT + reg*4, TOBCD(val));
127 }
128 
129 void
130 mkclock_read(dt)
131 	struct clock_ymdhms *dt;
132 {
133 	struct mkclock_softc *sc = mk0;
134 	int s = splclock();
135 
136 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, READ_CLOCK);
137 	dt->dt_sec  = mk_read(sc, 0);
138 	dt->dt_min  = mk_read(sc, 1);
139 	dt->dt_hour = mk_read(sc, 2);
140 	dt->dt_wday = mk_read(sc, 3);
141 	dt->dt_day  = mk_read(sc, 4);
142 	dt->dt_mon  = mk_read(sc, 5);
143 	dt->dt_year = mk_read(sc, 6);
144 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
145 	splx(s);
146 
147 	dt->dt_year = dt->dt_year + (dt->dt_year >= 70 ? 1900 : 2000);
148 }
149 
150 void
151 mkclock_write(dt)
152 	struct clock_ymdhms *dt;
153 {
154 	struct mkclock_softc *sc = mk0;
155 	int year, s;
156 
157 	year = dt->dt_year % 100;
158 
159 	s = splclock();
160 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, SET_CLOCK);
161 	mk_write(sc, 0, dt->dt_sec);
162 	mk_write(sc, 1, dt->dt_min);
163 	mk_write(sc, 2, dt->dt_hour);
164 	/* week day not set */
165 	mk_write(sc, 4, dt->dt_day);
166 	mk_write(sc, 5, dt->dt_mon);
167 	mk_write(sc, 6, year);
168 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
169 	splx(s);
170 }
171