tx39clock.c revision 1.2 1 1.2 uch /* $NetBSD: tx39clock.c,v 1.2 1999/12/07 17:08:10 uch Exp $ */
2 1.1 uch
3 1.1 uch /*
4 1.1 uch * Copyright (c) 1999, by UCHIYAMA Yasushi
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * Redistribution and use in source and binary forms, with or without
8 1.1 uch * modification, are permitted provided that the following conditions
9 1.1 uch * are met:
10 1.1 uch * 1. Redistributions of source code must retain the above copyright
11 1.1 uch * notice, this list of conditions and the following disclaimer.
12 1.1 uch * 2. The name of the developer may NOT be used to endorse or promote products
13 1.1 uch * derived from this software without specific prior written permission.
14 1.1 uch *
15 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.1 uch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1 uch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1 uch * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 uch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 uch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1 uch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 uch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 uch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 uch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 uch * SUCH DAMAGE.
26 1.1 uch *
27 1.1 uch */
28 1.1 uch #include "opt_tx39_debug.h"
29 1.1 uch
30 1.1 uch #include <sys/param.h>
31 1.1 uch #include <sys/systm.h>
32 1.1 uch #include <sys/device.h>
33 1.1 uch
34 1.1 uch #include <machine/bus.h>
35 1.1 uch #include <machine/clock_machdep.h>
36 1.1 uch #include <machine/cpu.h>
37 1.1 uch
38 1.1 uch #include <hpcmips/tx/tx39var.h>
39 1.1 uch #include <hpcmips/tx/tx39icureg.h> /* XXX */
40 1.1 uch #include <hpcmips/tx/tx39clockreg.h>
41 1.1 uch #include <hpcmips/tx/tx39timerreg.h>
42 1.1 uch #include <dev/dec/clockvar.h>
43 1.1 uch
44 1.1 uch #define ISSETPRINT(r, m) __is_set_print(r, TX39_CLOCK_EN##m##CLK, #m)
45 1.1 uch
46 1.1 uch void clock_init __P((struct device*));
47 1.1 uch void clock_get __P((struct device*, time_t, struct clocktime*));
48 1.1 uch void clock_set __P((struct device*, struct clocktime*));
49 1.1 uch
50 1.1 uch static const struct clockfns clockfns = {
51 1.1 uch clock_init, clock_get, clock_set,
52 1.1 uch };
53 1.1 uch
54 1.1 uch int tx39clock_match __P((struct device*, struct cfdata*, void*));
55 1.1 uch void tx39clock_attach __P((struct device*, struct device*, void*));
56 1.1 uch void tx39clock_dump __P((tx_chipset_tag_t));
57 1.1 uch
58 1.1 uch void tx39timer_freeze __P((tx_chipset_tag_t));
59 1.1 uch void tx39timer_rtcreset __P((tx_chipset_tag_t));
60 1.1 uch
61 1.1 uch struct tx39clock_softc {
62 1.1 uch struct device sc_dev;
63 1.1 uch tx_chipset_tag_t sc_tc;
64 1.1 uch };
65 1.1 uch
66 1.1 uch struct cfattach tx39clock_ca = {
67 1.1 uch sizeof(struct tx39clock_softc), tx39clock_match, tx39clock_attach
68 1.1 uch };
69 1.1 uch
70 1.1 uch int
71 1.1 uch tx39clock_match(parent, cf, aux)
72 1.1 uch struct device *parent;
73 1.1 uch struct cfdata *cf;
74 1.1 uch void *aux;
75 1.1 uch {
76 1.1 uch return 2; /* 1st attach group of txsim */
77 1.1 uch }
78 1.1 uch
79 1.1 uch void
80 1.1 uch tx39clock_attach(parent, self, aux)
81 1.1 uch struct device *parent;
82 1.1 uch struct device *self;
83 1.1 uch void *aux;
84 1.1 uch {
85 1.1 uch struct txsim_attach_args *ta = aux;
86 1.1 uch struct tx39clock_softc *sc = (void*)self;
87 1.1 uch tx_chipset_tag_t tc;
88 1.1 uch txreg_t reg;
89 1.1 uch
90 1.1 uch tc = sc->sc_tc = ta->ta_tc;
91 1.1 uch
92 1.1 uch /*
93 1.1 uch * Enable periodic timer
94 1.1 uch * but interrupt don't arise yet. see clock_init().
95 1.1 uch */
96 1.1 uch reg = tx_conf_read(tc, TX39_TIMERCONTROL_REG);
97 1.1 uch reg |= TX39_TIMERCONTROL_ENPERTIMER;
98 1.1 uch tx_conf_write(tc, TX39_TIMERCONTROL_REG, reg);
99 1.1 uch
100 1.1 uch /* Set counter */
101 1.1 uch #if 0
102 1.1 uch {
103 1.1 uch int cnt = 0xffff; /* XXX the most slower. */
104 1.1 uch reg = tx_conf_read(tc, TX39_TIMERPERIODIC_REG);
105 1.1 uch reg = TX39_TIMERPERIODIC_PERVAL_SET(reg, cnt);
106 1.1 uch tx_conf_write(tc, TX39_TIMERPERIODIC_REG, reg);
107 1.1 uch }
108 1.1 uch #endif
109 1.1 uch clockattach(self, &clockfns);
110 1.1 uch
111 1.2 uch #ifdef TX39CLKDEBUG
112 1.1 uch tx39clock_dump(tc);
113 1.2 uch #endif /* TX39CLKDEBUG */
114 1.1 uch }
115 1.1 uch
116 1.1 uch /*
117 1.1 uch * RTC and ALARM
118 1.1 uch * RTCINT ... INTR5 bit 31 (roll over)
119 1.1 uch * ALARMINT ... INTR5 bit 30
120 1.1 uch * PERINT ... INTR5 bit 29
121 1.1 uch */
122 1.1 uch void
123 1.1 uch tx39timer_freeze(tc)
124 1.1 uch tx_chipset_tag_t tc;
125 1.1 uch {
126 1.1 uch txreg_t reg;
127 1.1 uch
128 1.1 uch reg = tx_conf_read(tc, TX39_TIMERCONTROL_REG);
129 1.1 uch /* Freeze RTC */
130 1.1 uch reg |= TX39_TIMERCONTROL_FREEZEPRE; /* Upper 8bit */
131 1.1 uch reg |= TX39_TIMERCONTROL_FREEZERTC; /* Lower 32bit */
132 1.1 uch /* Freeze periodic timer */
133 1.1 uch reg |= TX39_TIMERCONTROL_FREEZETIMER;
134 1.1 uch reg &= ~TX39_TIMERCONTROL_ENPERTIMER;
135 1.1 uch tx_conf_write(tc, TX39_TIMERCONTROL_REG, reg);
136 1.1 uch }
137 1.1 uch
138 1.1 uch void
139 1.1 uch tx39timer_rtcreset(tc)
140 1.1 uch tx_chipset_tag_t tc;
141 1.1 uch {
142 1.1 uch txreg_t reg;
143 1.1 uch
144 1.1 uch reg = tx_conf_read(tc, TX39_TIMERCONTROL_REG);
145 1.1 uch /* Reset counter and stop */
146 1.1 uch reg |= TX39_TIMERCONTROL_RTCCLR;
147 1.1 uch tx_conf_write(tc, TX39_TIMERCONTROL_REG, reg);
148 1.1 uch /* Count again */
149 1.1 uch reg &= ~TX39_TIMERCONTROL_RTCCLR;
150 1.1 uch tx_conf_write(tc, TX39_TIMERCONTROL_REG, reg);
151 1.1 uch }
152 1.1 uch
153 1.1 uch void
154 1.1 uch clock_init(dev)
155 1.1 uch struct device *dev;
156 1.1 uch {
157 1.1 uch tx_chipset_tag_t tc;
158 1.1 uch txreg_t reg;
159 1.1 uch
160 1.1 uch tc = tx_conf_get_tag();
161 1.1 uch /* Enable periodic timer */
162 1.1 uch reg = tx_conf_read(tc, TX39_INTRENABLE6_REG);
163 1.1 uch reg |= TX39_INTRPRI13_TIMER_PERIODIC_BIT;
164 1.1 uch tx_conf_write(tc, TX39_INTRENABLE6_REG, reg);
165 1.1 uch
166 1.1 uch }
167 1.1 uch
168 1.1 uch void
169 1.1 uch clock_get(dev, base, ct)
170 1.1 uch struct device *dev;
171 1.1 uch time_t base;
172 1.1 uch struct clocktime *ct;
173 1.1 uch {
174 1.1 uch tx_chipset_tag_t tc;
175 1.1 uch txreg_t reghi, reglo, oreghi, oreglo;
176 1.1 uch int i;
177 1.1 uch
178 1.1 uch tc = tx_conf_get_tag();
179 1.1 uch i = 10;
180 1.1 uch do {
181 1.1 uch reghi = tx_conf_read(tc, TX39_TIMERRTCHI_REG);
182 1.1 uch reglo = tx_conf_read(tc, TX39_TIMERRTCLO_REG);
183 1.1 uch oreghi = tx_conf_read(tc, TX39_TIMERRTCHI_REG);
184 1.1 uch oreglo = tx_conf_read(tc, TX39_TIMERRTCLO_REG);
185 1.1 uch } while ((reghi != oreghi || reglo != oreglo) && (--i > 0));
186 1.1 uch if (i < 0) {
187 1.1 uch panic("RTC timer read error.\n");
188 1.1 uch }
189 1.1 uch /* XXX not coded yet */
190 1.1 uch }
191 1.1 uch
192 1.1 uch void
193 1.1 uch clock_set(dev, ct)
194 1.1 uch struct device *dev;
195 1.1 uch struct clocktime *ct;
196 1.1 uch {
197 1.1 uch /* XXX not coded yet */
198 1.1 uch }
199 1.1 uch
200 1.1 uch void
201 1.1 uch tx39clock_dump(tc)
202 1.1 uch tx_chipset_tag_t tc;
203 1.1 uch {
204 1.1 uch txreg_t reg;
205 1.1 uch
206 1.1 uch reg = tx_conf_read(tc, TX39_CLOCKCTRL_REG);
207 1.1 uch printf(" ");
208 1.1 uch ISSETPRINT(reg, CHIM);
209 1.1 uch #ifdef TX391X
210 1.1 uch ISSETPRINT(reg, VID);
211 1.1 uch ISSETPRINT(reg, MBUS);
212 1.1 uch #endif /* TX391X */
213 1.1 uch #ifdef TX392X
214 1.1 uch ISSETPRINT(reg, IRDA);
215 1.1 uch #endif /* TX392X */
216 1.1 uch ISSETPRINT(reg, SPI);
217 1.1 uch ISSETPRINT(reg, TIMER);
218 1.1 uch ISSETPRINT(reg, FASTTIMER);
219 1.1 uch #ifdef TX392X
220 1.1 uch ISSETPRINT(reg, C48MOUT);
221 1.1 uch #endif /* TX392X */
222 1.1 uch ISSETPRINT(reg, SIBM);
223 1.1 uch ISSETPRINT(reg, CSER);
224 1.1 uch ISSETPRINT(reg, IR);
225 1.1 uch ISSETPRINT(reg, UARTA);
226 1.1 uch ISSETPRINT(reg, UARTB);
227 1.1 uch printf("\n");
228 1.1 uch }
229 1.1 uch
230 1.1 uch
231 1.1 uch
232 1.1 uch
233