xref: /netbsd-src/sys/arch/mipsco/obio/mkclock.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: mkclock.c,v 1.8 2008/04/28 20:23:28 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: mkclock.c,v 1.8 2008/04/28 20:23:28 martin Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38 #include <sys/systm.h>
39 
40 #include <dev/clock_subr.h>
41 
42 #include <machine/cpu.h>
43 #include <machine/mainboard.h>
44 #include <machine/autoconf.h>
45 #include <machine/sysconf.h>
46 #include <machine/bus.h>
47 
48 #include <mipsco/obio/clockreg.h>
49 
50 struct	mkclock_softc {
51         struct  device dev;
52 	bus_space_tag_t	sc_bst;
53 	bus_space_handle_t sc_bsh;
54 	struct todr_chip_handle sc_todr;
55 };
56 
57 static int mkclock_match (struct device *, struct cfdata *, void *);
58 static void mkclock_attach (struct device *, struct device *, void *);
59 
60 CFATTACH_DECL(mkclock, sizeof(struct mkclock_softc),
61     mkclock_match, mkclock_attach, NULL, NULL);
62 
63 int mkclock_read (todr_chip_handle_t, struct clock_ymdhms *);
64 int mkclock_write (todr_chip_handle_t, struct clock_ymdhms *);
65 
66 static int mk_read (struct mkclock_softc *, int);
67 static void mk_write (struct mkclock_softc *, int, int);
68 
69 int
70 mkclock_match(parent, cf, aux)
71 	struct device *parent;
72 	struct cfdata *cf;
73 	void *aux;
74 {
75 	return 1;
76 }
77 
78 void
79 mkclock_attach(parent, self, aux)
80 	struct device *parent, *self;
81 	void *aux;
82 {
83         struct mkclock_softc *sc = (void *)self;
84 	struct confargs *ca = aux;
85 
86 	sc->sc_bst = ca->ca_bustag;
87 	if (bus_space_map(ca->ca_bustag, ca->ca_addr,
88 			  0x10000,
89 			  BUS_SPACE_MAP_LINEAR,
90 			  &sc->sc_bsh) != 0) {
91 		printf(": cannot map registers\n");
92 		return;
93 	}
94 
95 	sc->sc_todr.todr_settime_ymdhms = mkclock_write;
96 	sc->sc_todr.todr_gettime_ymdhms = mkclock_read;
97 	sc->sc_todr.cookie = sc;
98 	todr_attach(&sc->sc_todr);
99 
100 	printf("\n");
101 }
102 
103 static int
104 mk_read(sc, reg)
105 	struct mkclock_softc *sc;
106 	int reg;
107 {
108 	u_int8_t val;
109 
110 	val = bus_space_read_1(sc->sc_bst, sc->sc_bsh, DATA_PORT + reg*4);
111 	return FROMBCD(val);
112 }
113 
114 static void
115 mk_write(sc, reg, val)
116 	struct mkclock_softc *sc;
117 	int reg, val;
118 {
119 	bus_space_write_1(sc->sc_bst, sc->sc_bsh,
120 			  DATA_PORT + reg*4, TOBCD(val));
121 }
122 
123 int
124 mkclock_read(todr_chip_handle_t tch, struct clock_ymdhms *dt)
125 {
126 	struct mkclock_softc *sc = tch->cookie;
127 	int s = splclock();
128 
129 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, READ_CLOCK);
130 	dt->dt_sec  = mk_read(sc, 0);
131 	dt->dt_min  = mk_read(sc, 1);
132 	dt->dt_hour = mk_read(sc, 2);
133 	dt->dt_wday = mk_read(sc, 3);
134 	dt->dt_day  = mk_read(sc, 4);
135 	dt->dt_mon  = mk_read(sc, 5);
136 	dt->dt_year = mk_read(sc, 6);
137 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
138 	splx(s);
139 
140 	dt->dt_year = dt->dt_year + (dt->dt_year >= 70 ? 1900 : 2000);
141 	return 0;
142 }
143 
144 int
145 mkclock_write(todr_chip_handle_t tch, struct clock_ymdhms *dt)
146 {
147 	struct mkclock_softc *sc = tch->cookie;
148 	int year, s;
149 
150 	year = dt->dt_year % 100;
151 
152 	s = splclock();
153 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, SET_CLOCK);
154 	mk_write(sc, 0, dt->dt_sec);
155 	mk_write(sc, 1, dt->dt_min);
156 	mk_write(sc, 2, dt->dt_hour);
157 	/* week day not set */
158 	mk_write(sc, 4, dt->dt_day);
159 	mk_write(sc, 5, dt->dt_mon);
160 	mk_write(sc, 6, year);
161 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
162 	splx(s);
163 	return 0;
164 }
165