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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/* $NetBSD: mkclock.c,v 1.12 2012/10/27 17:18:12 chs Exp $ */
/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
* Copyright (c) 1994 Gordon W. Ross
* Copyright (c) 1993 Adam Glass
* Copyright (c) 1996 Paul Kranenburg
* Copyright (c) 1996
* The President and Fellows of Harvard College. All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Harvard University.
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratory.
*
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* This product includes software developed by Paul Kranenburg.
* This product includes software developed by Harvard University.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
* @(#)clock.c 8.1 (Berkeley) 6/11/93
*
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: mkclock.c,v 1.12 2012/10/27 17:18:12 chs Exp $");
/*
* Clock driver for 'mkclock' - Mostek MK48Txx TOD clock.
*/
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/proc.h>
#include <sys/resourcevar.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <uvm/uvm_extern.h>
#include <sys/bus.h>
#include <machine/autoconf.h>
#include <machine/eeprom.h>
#include <machine/cpu.h>
#include <dev/clock_subr.h>
#include <dev/ic/mk48txxreg.h>
#include <dev/ic/mk48txxvar.h>
#include <dev/sbus/sbusvar.h>
#include <dev/ebus/ebusreg.h>
#include <dev/ebus/ebusvar.h>
#include <sparc64/dev/fhcvar.h>
/*
* clock (eeprom) attaches at the sbus or the ebus (PCI)
*/
static int mkclock_sbus_match(device_t, cfdata_t, void *);
static void mkclock_sbus_attach(device_t, device_t, void *);
static int mkclock_ebus_match(device_t, cfdata_t, void *);
static void mkclock_ebus_attach(device_t, device_t, void *);
static int mkclock_fhc_match(device_t, cfdata_t, void *);
static void mkclock_fhc_attach(device_t, device_t, void *);
static void mkclock_attach(struct mk48txx_softc *, int);
static int mkclock_wenable(struct todr_chip_handle *, int);
CFATTACH_DECL_NEW(mkclock_sbus, sizeof(struct mk48txx_softc),
mkclock_sbus_match, mkclock_sbus_attach, NULL, NULL);
CFATTACH_DECL_NEW(mkclock_ebus, sizeof(struct mk48txx_softc),
mkclock_ebus_match, mkclock_ebus_attach, NULL, NULL);
CFATTACH_DECL_NEW(mkclock_fhc, sizeof(struct mk48txx_softc),
mkclock_fhc_match, mkclock_fhc_attach, NULL, NULL);
/*
* The OPENPROM calls the clock the "eeprom", so we have to have our
* own special match function to call it the "clock".
*/
static int
mkclock_sbus_match(device_t parent, cfdata_t cf, void *aux)
{
struct sbus_attach_args *sa = aux;
return (strcmp("eeprom", sa->sa_name) == 0);
}
static int
mkclock_ebus_match(device_t parent, cfdata_t cf, void *aux)
{
struct ebus_attach_args *ea = aux;
return (strcmp("eeprom", ea->ea_name) == 0);
}
static int
mkclock_fhc_match(device_t parent, cfdata_t cf, void *aux)
{
struct fhc_attach_args *fa = aux;
return (strcmp("eeprom", fa->fa_name) == 0);
}
/*
* Attach a clock (really `eeprom') to the sbus or ebus.
*
* We ignore any existing virtual address as we need to map
* this read-only and make it read-write only temporarily,
* whenever we read or write the clock chip. The clock also
* contains the ID ``PROM'', and I have already had the pleasure
* of reloading the CPU type, Ethernet address, etc, by hand from
* the console FORTH interpreter. I intend not to enjoy it again.
*
* the MK48T02 is 2K. the MK48T08 is 8K, and the MK48T59 is
* supposed to be identical to it.
*
* This is *UGLY*! We probably have multiple mappings. But I do
* know that this all fits inside an 8K page, so I'll just map in
* once.
*
* What we really need is some way to record the bus attach args
* so we can call *_bus_map() later with BUS_SPACE_MAP_READONLY
* or not to write enable/disable the device registers. This is
* a non-trivial operation.
*/
/* ARGSUSED */
static void
mkclock_sbus_attach(device_t parent, device_t self, void *aux)
{
struct mk48txx_softc *sc = device_private(self);
struct sbus_attach_args *sa = aux;
int sz;
sc->sc_dev = self;
sc->sc_bst = sa->sa_bustag;
/* use sa->sa_regs[0].size? */
sz = 8192;
if (sbus_bus_map(sc->sc_bst,
sa->sa_slot,
trunc_page(sa->sa_offset),
sz,
BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_READONLY,
&sc->sc_bsh) != 0) {
aprint_error(": can't map register\n");
return;
}
mkclock_attach(sc, sa->sa_node);
}
/* ARGSUSED */
static void
mkclock_ebus_attach(device_t parent, device_t self, void *aux)
{
struct mk48txx_softc *sc = device_private(self);
struct ebus_attach_args *ea = aux;
int sz;
sc->sc_dev = self;
sc->sc_bst = ea->ea_bustag;
/* hard code to 8K? */
sz = ea->ea_reg[0].size;
/* Use the PROM address if there. */
if (ea->ea_nvaddr)
sparc_promaddr_to_handle(sc->sc_bst, ea->ea_vaddr[0],
&sc->sc_bsh);
else if (bus_space_map(sc->sc_bst,
EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
sz,
BUS_SPACE_MAP_LINEAR,
&sc->sc_bsh) != 0) {
aprint_error(": can't map register\n");
return;
}
mkclock_attach(sc, ea->ea_node);
}
/* ARGSUSED */
static void
mkclock_fhc_attach(device_t parent, device_t self, void *aux)
{
struct mk48txx_softc *sc = device_private(self);
struct fhc_attach_args *fa = aux;
sc->sc_dev = self;
sc->sc_bst = fa->fa_bustag;
if (fhc_bus_map(sc->sc_bst,
fa->fa_reg[0].fbr_slot,
(fa->fa_reg[0].fbr_offset & ~NBPG),
fa->fa_reg[0].fbr_size,
BUS_SPACE_MAP_LINEAR,
&sc->sc_bsh) != 0) {
aprint_error(": can't map register\n");
return;
}
mkclock_attach(sc, fa->fa_node);
}
static void
mkclock_attach(struct mk48txx_softc *sc, int node)
{
sc->sc_model = prom_getpropstring(node, "model");
#ifdef DIAGNOSTIC
if (sc->sc_model == NULL)
panic("clockattach: no model property");
#endif
/* Our TOD clock year 0 is 1968 */
sc->sc_year0 = 1968;
/* Save info for the clock wenable call. */
sc->sc_handle.todr_setwen = mkclock_wenable;
mk48txx_attach(sc);
aprint_normal("\n");
}
/*
* Write en/dis-able clock registers. We coordinate so that several
* writers can run simultaneously.
*/
static int
mkclock_wenable(struct todr_chip_handle *handle, int onoff)
{
struct mk48txx_softc *sc;
vm_prot_t prot;
vaddr_t va;
int s, err = 0;
static int writers;
/* XXXSMP */
s = splhigh();
if (onoff)
prot = writers++ == 0 ? VM_PROT_READ|VM_PROT_WRITE : 0;
else
prot = --writers == 0 ? VM_PROT_READ : 0;
splx(s);
if (prot == VM_PROT_NONE) {
return 0;
}
sc = handle->cookie;
va = (vaddr_t)bus_space_vaddr(sc->sc_bst, sc->sc_bsh);
if (va == 0UL) {
printf("clock_wenable: WARNING -- cannot get va\n");
return EIO;
}
pmap_kprotect(va, prot);
return (err);
}
|