epclk.c revision 1.11.10.1 1 /* $NetBSD: epclk.c,v 1.11.10.1 2009/05/04 08:10:40 yamt Exp $ */
2
3 /*
4 * Copyright (c) 2004 Jesse Off
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the NetBSD
18 * Foundation, Inc. and its contributors.
19 * 4. Neither the name of The NetBSD Foundation nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*
37 * Driver for the ep93xx clock tick.
38 *
39 * We use the 64Hz RTC interrupt as its the only thing that allows for timekeeping
40 * of a second (crystal error only). There are two general purpose timers
41 * on the ep93xx, but they run at a frequency that makes a perfect integer
42 * number of ticks per second impossible. Note that there was an errata with
43 * the ep93xx processor and many early boards (including the Cirrus eval board) have
44 * a broken crystal oscillator input that may make this 64Hz unreliable. However,
45 * not all boards are susceptible, the Technologic Systems TS-7200 is a notable
46 * exception that is immune to this errata. --joff
47 */
48
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: epclk.c,v 1.11.10.1 2009/05/04 08:10:40 yamt Exp $");
51
52 #include <sys/types.h>
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/time.h>
57 #include <sys/timetc.h>
58 #include <sys/device.h>
59
60 #include <machine/bus.h>
61 #include <machine/intr.h>
62
63 #include <arm/cpufunc.h>
64
65 #include <arm/ep93xx/epsocvar.h>
66 #include <arm/ep93xx/epclkreg.h>
67 #include <arm/ep93xx/ep93xxreg.h>
68 #include <arm/ep93xx/ep93xxvar.h>
69 #include <dev/clock_subr.h>
70
71 #include "opt_hz.h"
72
73 #define TIMER_FREQ 983040
74
75 static int epclk_match(struct device *, struct cfdata *, void *);
76 static void epclk_attach(struct device *, struct device *, void *);
77 static u_int epclk_get_timecount(struct timecounter *);
78
79 void rtcinit(void);
80
81 /* callback functions for intr_functions */
82 static int epclk_intr(void* arg);
83
84 struct epclk_softc {
85 struct device sc_dev;
86 bus_addr_t sc_baseaddr;
87 bus_space_tag_t sc_iot;
88 bus_space_handle_t sc_ioh;
89 #if defined(HZ) && (HZ == 64)
90 bus_space_handle_t sc_teoi_ioh;
91 #endif
92 int sc_intr;
93 };
94
95 static struct timecounter epclk_timecounter = {
96 epclk_get_timecount, /* get_timecount */
97 0, /* no poll_pps */
98 ~0u, /* counter_mask */
99 TIMER_FREQ, /* frequency */
100 "epclk", /* name */
101 100, /* quality */
102 NULL, /* prev */
103 NULL, /* next */
104 };
105
106 static struct epclk_softc *epclk_sc = NULL;
107
108 CFATTACH_DECL(epclk, sizeof(struct epclk_softc),
109 epclk_match, epclk_attach, NULL, NULL);
110
111 /* This is a quick ARM way to multiply by 983040/1000000 (w/o overflow) */
112 #define US_TO_TIMER4VAL(x, y) { \
113 u_int32_t hi, lo, scalar = 4222124650UL; \
114 __asm volatile ( \
115 "umull %0, %1, %2, %3;" \
116 : "=&r"(lo), "=&r"(hi) \
117 : "r"((x)), "r"(scalar) \
118 ); \
119 (y) = hi; \
120 }
121
122 #define TIMER4VAL() (*(volatile u_int32_t *)(EP93XX_APB_VBASE + \
123 EP93XX_APB_TIMERS + EP93XX_TIMERS_Timer4ValueLow))
124
125 static int
126 epclk_match(struct device *parent, struct cfdata *match, void *aux)
127 {
128
129 return 2;
130 }
131
132 static void
133 epclk_attach(struct device *parent, struct device *self, void *aux)
134 {
135 struct epclk_softc *sc;
136 struct epsoc_attach_args *sa;
137 bool first_run;
138
139 printf("\n");
140
141 sc = (struct epclk_softc*) self;
142 sa = aux;
143 sc->sc_iot = sa->sa_iot;
144 sc->sc_baseaddr = sa->sa_addr;
145 sc->sc_intr = sa->sa_intr;
146
147 if (epclk_sc == NULL) {
148 first_run = true;
149 epclk_sc = sc;
150 }
151
152 if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size,
153 0, &sc->sc_ioh))
154 panic("%s: Cannot map registers", self->dv_xname);
155 #if defined(HZ) && (HZ == 64)
156 if (bus_space_map(sa->sa_iot, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON +
157 EP93XX_SYSCON_TEOI, 4, 0, &sc->sc_teoi_ioh))
158 panic("%s: Cannot map registers", self->dv_xname);
159 #endif
160
161 /* clear and start the debug timer (Timer4) */
162 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EP93XX_TIMERS_Timer4Enable, 0);
163 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EP93XX_TIMERS_Timer4Enable, 0x100);
164
165 if (first_run)
166 tc_init(&epclk_timecounter);
167 }
168
169 /*
170 * epclk_intr:
171 *
172 * Handle the hardclock interrupt.
173 */
174 static int
175 epclk_intr(void *arg)
176 {
177 struct epclk_softc* sc;
178
179 sc = epclk_sc;
180
181 #if defined(HZ) && (HZ == 64)
182 bus_space_write_4(sc->sc_iot, sc->sc_teoi_ioh, 0, 1);
183 #else
184 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EP93XX_TIMERS_Timer1Clear, 1);
185 #endif
186 hardclock((struct clockframe*) arg);
187 return (1);
188 }
189
190 /*
191 * setstatclockrate:
192 *
193 * Set the rate of the statistics clock.
194 *
195 * We assume that hz is either stathz or profhz, and that neither
196 * will change after being set by cpu_initclocks(). We could
197 * recalculate the intervals here, but that would be a pain.
198 */
199 void
200 setstatclockrate(int newhz)
201 {
202
203 /* use hardclock */
204 }
205
206 /*
207 * cpu_initclocks:
208 *
209 * Initialize the clock and get them going.
210 */
211 void
212 cpu_initclocks(void)
213 {
214 struct epclk_softc* sc;
215
216 sc = epclk_sc;
217 stathz = profhz = 0;
218
219 #if defined(HZ) && (HZ == 64)
220 if (hz != 64) panic("HZ must be 64!");
221
222 /* clear 64Hz interrupt status */
223 bus_space_write_4(sc->sc_iot, sc->sc_teoi_ioh, 0, 1);
224 #else
225 #define CLOCK_SOURCE_RATE 14745600UL
226 #define CLOCK_TICK_DIV 29
227 #define CLOCK_TICK_RATE \
228 (((CLOCK_SOURCE_RATE+(CLOCK_TICK_DIV*hz-1))/(CLOCK_TICK_DIV*hz))*hz)
229 #define LATCH ((CLOCK_TICK_RATE + hz/2) / hz)
230 /* setup and start the 16bit timer (Timer1) */
231 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
232 EP93XX_TIMERS_Timer1Control,
233 (TimerControl_MODE)|(TimerControl_CLKSEL));
234 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
235 EP93XX_TIMERS_Timer1Load, LATCH-1);
236 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
237 EP93XX_TIMERS_Timer1Control,
238 (TimerControl_ENABLE)|(TimerControl_MODE)|(TimerControl_CLKSEL));
239 #endif
240
241 ep93xx_intr_establish(sc->sc_intr, IPL_CLOCK, epclk_intr, NULL);
242 }
243
244 /*
245 * delay:
246 *
247 * Delay for at least N microseconds.
248 */
249 void
250 delay(unsigned int n)
251 {
252 unsigned int cur_tick, initial_tick;
253 int remaining;
254
255 #ifdef DEBUG
256 if (epclk_sc == NULL) {
257 printf("delay: called before start epclk\n");
258 return;
259 }
260 #endif
261
262 /*
263 * Read the counter first, so that the rest of the setup overhead is
264 * counted.
265 */
266 initial_tick = TIMER4VAL();
267
268 US_TO_TIMER4VAL(n, remaining);
269
270 while (remaining > 0) {
271 cur_tick = TIMER4VAL();
272 if (cur_tick >= initial_tick)
273 remaining -= cur_tick - initial_tick;
274 else
275 remaining -= UINT_MAX - initial_tick + cur_tick + 1;
276 initial_tick = cur_tick;
277 }
278 }
279
280 static u_int
281 epclk_get_timecount(struct timecounter *tc)
282 {
283 return TIMER4VAL();
284 }
285