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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/* $OpenBSD: pxa2x0_clock.c,v 1.4 2006/03/07 22:12:46 uwe Exp $ */
/*
* Copyright (c) 2005 Dale Rahn <drahn@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/time.h>
#include <sys/device.h>
#include <machine/bus.h>
#include <machine/intr.h>
#include <arm/cpufunc.h>
#include <arm/sa11x0/sa11x0_reg.h>
#include <arm/sa11x0/sa11x0_var.h>
#include <arm/sa11x0/sa11x0_ostreg.h>
#include <arm/xscale/pxa2x0reg.h>
int pxaost_match(struct device *, void *, void *);
void pxaost_attach(struct device *, struct device *, void *);
int doclockintr(void *);
int clockintr(void *);
int statintr(void *);
void rtcinit(void);
struct pxaost_softc {
struct device sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
u_int32_t sc_clock_count;
u_int32_t sc_statclock_count;
u_int32_t sc_statclock_step;
u_int32_t sc_clock_step;
u_int32_t sc_clock_step_err_cnt;
u_int32_t sc_clock_step_error;
};
static struct pxaost_softc *pxaost_sc = NULL;
#define CLK4_TIMER_FREQUENCY 32768 /* 32.768KHz */
#define CLK0_TIMER_FREQUENCY 3250000 /* 3.2500MHz */
#ifndef STATHZ
#define STATHZ 64
#endif
struct cfattach pxaost_ca = {
sizeof (struct pxaost_softc), pxaost_match, pxaost_attach
};
struct cfdriver pxaost_cd = {
NULL, "pxaost", DV_DULL
};
int
pxaost_match(parent, match, aux)
struct device *parent;
void *match;
void *aux;
{
return (1);
}
void
pxaost_attach(parent, self, aux)
struct device *parent;
struct device *self;
void *aux;
{
struct pxaost_softc *sc = (struct pxaost_softc*)self;
struct sa11x0_attach_args *sa = aux;
printf("\n");
sc->sc_iot = sa->sa_iot;
pxaost_sc = sc;
if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
&sc->sc_ioh))
panic("%s: Cannot map registers", self->dv_xname);
/* disable all channel and clear interrupt status */
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, SAOST_IR, 0);
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, SAOST_SR, 0x3f);
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, OST_OMCR4, 0xc1);
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, OST_OMCR5, 0x41);
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, OST_OSMR4,
pxaost_sc->sc_clock_count);
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, OST_OSMR5,
pxaost_sc->sc_statclock_count);
/* Zero the counter value */
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, OST_OSCR4, 0);
}
int
clockintr(arg)
void *arg;
{
struct clockframe *frame = arg;
u_int32_t oscr, match;
u_int32_t match_error;
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, SAOST_SR, 0x10);
match = pxaost_sc->sc_clock_count;
do {
match += pxaost_sc->sc_clock_step;
pxaost_sc->sc_clock_step_error +=
pxaost_sc->sc_clock_step_err_cnt;
if (pxaost_sc->sc_clock_count > hz) {
match_error = pxaost_sc->sc_clock_step_error / hz;
pxaost_sc->sc_clock_step_error -= (match_error * hz);
match += match_error;
}
pxaost_sc->sc_clock_count = match;
hardclock(frame);
oscr = bus_space_read_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh,
OST_OSCR4);
} while ((signed)(oscr - match) > 0);
/* prevent missed interrupts */
if (oscr - match < 5)
match += 5;
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, OST_OSMR4,
match);
return(1);
}
int
statintr(arg)
void *arg;
{
struct clockframe *frame = arg;
u_int32_t oscr, match;
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, SAOST_SR, 0x20);
/* schedule next clock intr */
match = pxaost_sc->sc_statclock_count;
do {
match += pxaost_sc->sc_statclock_step;
pxaost_sc->sc_statclock_count = match;
statclock(frame);
oscr = bus_space_read_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh,
OST_OSCR4);
} while ((signed)(oscr - match) > 0);
/* prevent missed interrupts */
if (oscr - match < 5)
match += 5;
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, OST_OSMR5,
match);
return(1);
}
void
setstatclockrate(int newstathz)
{
u_int32_t count;
pxaost_sc->sc_statclock_step = CLK4_TIMER_FREQUENCY / newstathz;
count = bus_space_read_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, OST_OSCR4);
count += pxaost_sc->sc_statclock_step;
pxaost_sc->sc_statclock_count = count;
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh,
OST_OSMR5, count);
}
int
doclockintr(void *arg)
{
u_int32_t status;
int result = 0;
status = bus_space_read_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, SAOST_SR);
if (status & 0x10)
result |= clockintr(arg);
if (status & 0x20)
result |= statintr(arg);
return (result);
}
void
cpu_initclocks()
{
u_int32_t clk;
stathz = STATHZ;
profhz = stathz;
pxaost_sc->sc_statclock_step = CLK4_TIMER_FREQUENCY / stathz;
pxaost_sc->sc_clock_step = CLK4_TIMER_FREQUENCY / hz;
pxaost_sc->sc_clock_step_err_cnt = CLK4_TIMER_FREQUENCY % hz;
pxaost_sc->sc_clock_step_error = 0;
printf("clock: hz=%d stathz=%d\n", hz, stathz);
/* Use the channels 0 and 1 for hardclock and statclock, respectively */
pxaost_sc->sc_clock_count = pxaost_sc->sc_clock_step;
pxaost_sc->sc_statclock_count = CLK4_TIMER_FREQUENCY / stathz;
pxa2x0_intr_establish(PXA2X0_INT_OST, IPL_CLOCK, doclockintr, 0, "clock");
clk = bus_space_read_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, OST_OSCR4);
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, SAOST_SR, 0x3f);
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, SAOST_IR, 0x30);
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, OST_OSMR4,
clk + pxaost_sc->sc_clock_count);
bus_space_write_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh, OST_OSMR5,
clk + pxaost_sc->sc_statclock_count);
}
void
microtime(tvp)
register struct timeval *tvp;
{
int s, deltacnt;
u_int32_t counter, expected;
if (pxaost_sc == NULL) {
tvp->tv_sec = 0;
tvp->tv_usec = 0;
return;
}
s = splhigh();
counter = bus_space_read_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh,
OST_OSCR4);
expected = pxaost_sc->sc_clock_count;
*tvp = time;
splx(s);
/* number of CLK4_TIMER_FREQUENCY ticks past time */
deltacnt = counter - expected + pxaost_sc->sc_clock_step;
tvp->tv_usec += deltacnt * 1000000ULL / CLK4_TIMER_FREQUENCY;
while (tvp->tv_usec >= 1000000) {
tvp->tv_sec++;
tvp->tv_usec -= 1000000;
}
}
void
delay(usecs)
u_int usecs;
{
u_int32_t clock, oclock, delta, delaycnt;
volatile int j;
int csec, usec;
if (usecs > (0x80000000 / (CLK4_TIMER_FREQUENCY))) {
csec = usecs / 10000;
usec = usecs % 10000;
delaycnt = (CLK4_TIMER_FREQUENCY / 100) * csec +
(CLK4_TIMER_FREQUENCY / 100) * usec / 10000;
} else {
delaycnt = CLK4_TIMER_FREQUENCY * usecs / 1000000;
}
if (delaycnt <= 1)
for (j = 100; j > 0; j--)
;
if (!pxaost_sc) {
/* clock isn't initialized yet */
for (; usecs > 0; usecs--)
for (j = 100; j > 0; j--)
;
return;
}
oclock = bus_space_read_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh,
OST_OSCR4);
while (1) {
for (j = 100; j > 0; j--)
;
clock = bus_space_read_4(pxaost_sc->sc_iot, pxaost_sc->sc_ioh,
OST_OSCR4);
delta = clock - oclock;
if (delta > delaycnt)
break;
}
}
|