1 /* $NetBSD: tx39clock.c,v 1.22 2008/01/04 22:13:57 ad Exp $ */
4 * Copyright (c) 1999-2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: tx39clock.c,v 1.22 2008/01/04 22:13:57 ad Exp $");
35 #include "opt_tx39clock_debug.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/timetc.h>
40 #include <sys/device.h>
43 #include <dev/clock_subr.h>
45 #include <machine/sysconf.h>
47 #include <hpcmips/tx/tx39var.h>
48 #include <hpcmips/tx/tx39icureg.h>
49 #include <hpcmips/tx/tx39clockvar.h>
50 #include <hpcmips/tx/tx39clockreg.h>
51 #include <hpcmips/tx/tx39timerreg.h>
53 #ifdef TX39CLOCK_DEBUG
54 #define DPRINTF_ENABLE
55 #define DPRINTF_DEBUG tx39clock_debug
57 #include <machine/debug.h>
59 #define ISSETPRINT(r, m) \
60 dbg_bitmask_print(r, TX39_CLOCK_EN ## m ## CLK, #m)
62 void tx39clock_init(struct device
*);
64 struct platform_clock tx39_clock
= {
65 #define CLOCK_RATE 100
66 CLOCK_RATE
, tx39clock_init
,
74 struct tx39clock_softc
{
76 tx_chipset_tag_t sc_tc
;
81 struct clock_ymdhms sc_epoch
;
82 struct timecounter sc_tcounter
;
85 int tx39clock_match(struct device
*, struct cfdata
*, void *);
86 void tx39clock_attach(struct device
*, struct device
*, void *);
87 #ifdef TX39CLOCK_DEBUG
88 void tx39clock_dump(tx_chipset_tag_t
);
91 void tx39clock_cpuspeed(int *, int *);
93 void __tx39timer_rtcfreeze(tx_chipset_tag_t
);
94 void __tx39timer_rtcreset(tx_chipset_tag_t
);
95 inline void __tx39timer_rtcget(struct txtime
*);
96 inline time_t __tx39timer_rtc2sec(struct txtime
*);
97 uint32_t tx39_timecount(struct timecounter
*);
99 CFATTACH_DECL(tx39clock
, sizeof(struct tx39clock_softc
),
100 tx39clock_match
, tx39clock_attach
, NULL
, NULL
);
103 tx39clock_match(struct device
*parent
, struct cfdata
*cf
, void *aux
)
106 return (ATTACH_FIRST
);
110 tx39clock_attach(struct device
*parent
, struct device
*self
, void *aux
)
112 struct txsim_attach_args
*ta
= aux
;
113 struct tx39clock_softc
*sc
= (void*)self
;
117 tc
= sc
->sc_tc
= ta
->ta_tc
;
118 tx_conf_register_clock(tc
, self
);
120 /* Reset timer module */
121 tx_conf_write(tc
, TX39_TIMERCONTROL_REG
, 0);
123 /* Enable periodic timer */
124 reg
= tx_conf_read(tc
, TX39_TIMERCONTROL_REG
);
125 reg
|= TX39_TIMERCONTROL_ENPERTIMER
;
126 tx_conf_write(tc
, TX39_TIMERCONTROL_REG
, reg
);
131 * RTCINT ... INTR5 bit 31 (roll over)
132 * ALARMINT ... INTR5 bit 30
133 * PERINT ... INTR5 bit 29
136 platform_clock_attach(self
, &tx39_clock
);
138 #ifdef TX39CLOCK_DEBUG
140 #endif /* TX39CLOCK_DEBUG */
144 * cpuclock ... CPU clock (Hz)
145 * cpuspeed ... instructions-per-microsecond
148 tx39clock_cpuspeed(int *cpuclock
, int *cpu_speed
)
150 struct txtime t0
, t1
;
153 __tx39timer_rtcget(&t0
);
155 ".set noreorder; \n\t"
156 "li $8, 10000000; \n"
164 "add $8, $8, -1; \n\t"
168 __tx39timer_rtcget(&t1
);
170 elapsed
= t1
.t_lo
- t0
.t_lo
;
172 *cpuclock
= (100000000 / elapsed
) * TX39_RTCLOCK
;
173 *cpu_speed
= *cpuclock
/ 1000000;
177 __tx39timer_rtcfreeze(tx_chipset_tag_t tc
)
181 reg
= tx_conf_read(tc
, TX39_TIMERCONTROL_REG
);
184 reg
|= TX39_TIMERCONTROL_FREEZEPRE
; /* Upper 8bit */
185 reg
|= TX39_TIMERCONTROL_FREEZERTC
; /* Lower 32bit */
187 /* Freeze periodic timer */
188 reg
|= TX39_TIMERCONTROL_FREEZETIMER
;
189 reg
&= ~TX39_TIMERCONTROL_ENPERTIMER
;
190 tx_conf_write(tc
, TX39_TIMERCONTROL_REG
, reg
);
194 __tx39timer_rtcget(struct txtime
*t
)
197 txreg_t reghi
, reglo
, oreghi
, oreglo
;
200 tc
= tx_conf_get_tag();
205 oreglo
= tx_conf_read(tc
, TX39_TIMERRTCLO_REG
);
206 reglo
= tx_conf_read(tc
, TX39_TIMERRTCLO_REG
);
208 oreghi
= tx_conf_read(tc
, TX39_TIMERRTCHI_REG
);
209 reghi
= tx_conf_read(tc
, TX39_TIMERRTCHI_REG
);
210 } while ((reghi
!= oreghi
|| reglo
!= oreglo
) && (--retry
> 0));
213 printf("RTC timer read error.\n");
216 t
->t_hi
= TX39_TIMERRTCHI(reghi
);
221 __tx39timer_rtcreset(tx_chipset_tag_t tc
)
225 reg
= tx_conf_read(tc
, TX39_TIMERCONTROL_REG
);
227 /* Reset counter and stop */
228 reg
|= TX39_TIMERCONTROL_RTCCLR
;
229 tx_conf_write(tc
, TX39_TIMERCONTROL_REG
, reg
);
232 reg
&= ~TX39_TIMERCONTROL_RTCCLR
;
233 tx_conf_write(tc
, TX39_TIMERCONTROL_REG
, reg
);
237 tx39_timecount(struct timecounter
*tch
)
239 tx_chipset_tag_t tc
= tch
->tc_priv
;
242 * since we're only reading the low register, we don't care about
243 * if the chip increments it. we assume that the single read will
244 * always be consistent. This is much faster than the routine which
245 * has to get both values, improving the quality.
247 return (tx_conf_read(tc
, TX39_TIMERRTCLO_REG
));
251 tx39clock_init(struct device
*dev
)
253 struct tx39clock_softc
*sc
= (void*)dev
;
254 tx_chipset_tag_t tc
= sc
->sc_tc
;
259 * Setup periodic timer (interrupting hz times per second.)
261 pcnt
= TX39_TIMERCLK
/ CLOCK_RATE
- 1;
262 reg
= tx_conf_read(tc
, TX39_TIMERPERIODIC_REG
);
263 TX39_TIMERPERIODIC_PERVAL_CLR(reg
);
264 reg
= TX39_TIMERPERIODIC_PERVAL_SET(reg
, pcnt
);
265 tx_conf_write(tc
, TX39_TIMERPERIODIC_REG
, reg
);
268 * Enable periodic timer
270 reg
= tx_conf_read(tc
, TX39_INTRENABLE6_REG
);
271 reg
|= TX39_INTRPRI13_TIMER_PERIODIC_BIT
;
272 tx_conf_write(tc
, TX39_INTRENABLE6_REG
, reg
);
274 sc
->sc_tcounter
.tc_name
= "tx39rtc";
275 sc
->sc_tcounter
.tc_get_timecount
= tx39_timecount
;
276 sc
->sc_tcounter
.tc_priv
= tc
;
277 sc
->sc_tcounter
.tc_counter_mask
= 0xffffffff;
278 sc
->sc_tcounter
.tc_frequency
= TX39_RTCLOCK
;
279 sc
->sc_tcounter
.tc_quality
= 100;
280 tc_init(&sc
->sc_tcounter
);
284 tx39clock_alarm_set(tx_chipset_tag_t tc
, int msec
)
286 struct tx39clock_softc
*sc
= tc
->tc_clockt
;
288 sc
->sc_alarm
= TX39_MSEC2RTC(msec
);
289 tx39clock_alarm_refill(tc
);
295 tx39clock_alarm_refill(tx_chipset_tag_t tc
)
297 struct tx39clock_softc
*sc
= tc
->tc_clockt
;
301 __tx39timer_rtcget(&t
);
303 mytime
= ((u_int64_t
)t
.t_hi
<< 32) | (u_int64_t
)t
.t_lo
;
304 mytime
+= (u_int64_t
)sc
->sc_alarm
;
306 t
.t_hi
= (u_int32_t
)((mytime
>> 32) & TX39_TIMERALARMHI_MASK
);
307 t
.t_lo
= (u_int32_t
)(mytime
& 0xffffffff);
309 tx_conf_write(tc
, TX39_TIMERALARMHI_REG
, t
.t_hi
);
310 tx_conf_write(tc
, TX39_TIMERALARMLO_REG
, t
.t_lo
);
313 #ifdef TX39CLOCK_DEBUG
315 tx39clock_dump(tx_chipset_tag_t tc
)
319 reg
= tx_conf_read(tc
, TX39_CLOCKCTRL_REG
);
322 ISSETPRINT(reg
, CHIM
);
324 ISSETPRINT(reg
, VID
);
325 ISSETPRINT(reg
, MBUS
);
328 ISSETPRINT(reg
, IRDA
);
330 ISSETPRINT(reg
, SPI
);
331 ISSETPRINT(reg
, TIMER
);
332 ISSETPRINT(reg
, FASTTIMER
);
334 ISSETPRINT(reg
, C48MOUT
);
336 ISSETPRINT(reg
, SIBM
);
337 ISSETPRINT(reg
, CSER
);
339 ISSETPRINT(reg
, UARTA
);
340 ISSETPRINT(reg
, UARTB
);
343 #endif /* TX39CLOCK_DEBUG */