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