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