windermere.c revision 1.2 1 1.2 thorpej /* $NetBSD: windermere.c,v 1.2 2021/04/24 23:36:32 thorpej Exp $ */
2 1.1 kiyohara /*
3 1.1 kiyohara * Copyright (c) 2012 KIYOHARA Takashi
4 1.1 kiyohara * All rights reserved.
5 1.1 kiyohara *
6 1.1 kiyohara * Redistribution and use in source and binary forms, with or without
7 1.1 kiyohara * modification, are permitted provided that the following conditions
8 1.1 kiyohara * are met:
9 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright
10 1.1 kiyohara * notice, this list of conditions and the following disclaimer.
11 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the
13 1.1 kiyohara * documentation and/or other materials provided with the distribution.
14 1.1 kiyohara *
15 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
26 1.1 kiyohara */
27 1.1 kiyohara
28 1.1 kiyohara #include <sys/cdefs.h>
29 1.2 thorpej __KERNEL_RCSID(0, "$NetBSD: windermere.c,v 1.2 2021/04/24 23:36:32 thorpej Exp $");
30 1.1 kiyohara
31 1.1 kiyohara #include "opt_com.h"
32 1.1 kiyohara
33 1.1 kiyohara #define _INTR_PRIVATE
34 1.1 kiyohara
35 1.1 kiyohara #include <sys/param.h>
36 1.1 kiyohara #include <sys/bus.h>
37 1.1 kiyohara #include <sys/device.h>
38 1.1 kiyohara #include <sys/errno.h>
39 1.1 kiyohara #include <sys/kernel.h>
40 1.1 kiyohara #include <sys/systm.h>
41 1.1 kiyohara #include <sys/timetc.h>
42 1.1 kiyohara #include <sys/types.h>
43 1.1 kiyohara
44 1.1 kiyohara #include <arm/mainbus/mainbus.h>
45 1.1 kiyohara #include <arm/pic/picvar.h>
46 1.1 kiyohara
47 1.1 kiyohara #include <machine/epoc32.h>
48 1.1 kiyohara #include <machine/limits.h>
49 1.1 kiyohara
50 1.1 kiyohara #include <epoc32/windermere/windermerereg.h>
51 1.1 kiyohara #include <epoc32/windermere/windermerevar.h>
52 1.1 kiyohara
53 1.1 kiyohara #include "locators.h"
54 1.1 kiyohara
55 1.1 kiyohara
56 1.1 kiyohara static int windermere_match(device_t, cfdata_t, void *);
57 1.1 kiyohara static void windermere_attach(device_t parent, device_t self, void *aux);
58 1.1 kiyohara
59 1.1 kiyohara static int windermere_print(void *, const char *);
60 1.1 kiyohara static int windermere_submatch(device_t, cfdata_t, const int *, void *);
61 1.1 kiyohara
62 1.1 kiyohara static int windermere_find_pending_irqs(void);
63 1.1 kiyohara
64 1.1 kiyohara static void windermere_pic_unblock_irqs(struct pic_softc *, size_t, uint32_t);
65 1.1 kiyohara static void windermere_pic_block_irqs(struct pic_softc *, size_t, uint32_t);
66 1.1 kiyohara static void windermere_pic_establish_irq(struct pic_softc *,
67 1.1 kiyohara struct intrsource *);
68 1.1 kiyohara
69 1.1 kiyohara #define TC_READ(base, off) (*((base) + (off)))
70 1.1 kiyohara #define TC_WRITE(base, off, val) (*((base) + (off)) = (val))
71 1.1 kiyohara static void windermere_initclocks(void);
72 1.1 kiyohara static int windermere_clockintr(void *);
73 1.1 kiyohara static void windermere_tc_init(void);
74 1.1 kiyohara static u_int windermere_get_timecount(struct timecounter *);
75 1.1 kiyohara
76 1.1 kiyohara static void windermere_delay(unsigned int);
77 1.1 kiyohara
78 1.1 kiyohara CFATTACH_DECL_NEW(windermere, 0,
79 1.1 kiyohara windermere_match, windermere_attach, NULL, NULL);
80 1.1 kiyohara
81 1.1 kiyohara static const struct {
82 1.1 kiyohara const char *name;
83 1.1 kiyohara bus_addr_t offset;
84 1.1 kiyohara int irq;
85 1.1 kiyohara } windermere_periphs[] = {
86 1.1 kiyohara { "wmlcd", 0x200, WINDERMERECF_IRQ_DEFAULT },
87 1.1 kiyohara { "wmcom", 0x600, 12 },
88 1.1 kiyohara { "wmcom", 0x700, 13 },
89 1.1 kiyohara { "wmrtc", 0xd00, 10 },
90 1.1 kiyohara { "wmpm", WINDERMERECF_OFFSET_DEFAULT, WINDERMERECF_IRQ_DEFAULT },
91 1.1 kiyohara { "wmaudio", WINDERMERECF_OFFSET_DEFAULT, WINDERMERECF_IRQ_DEFAULT },
92 1.1 kiyohara { "wmkbd", 0xe28, WINDERMERECF_IRQ_DEFAULT },
93 1.1 kiyohara };
94 1.1 kiyohara
95 1.1 kiyohara static struct pic_ops windermere_picops = {
96 1.1 kiyohara .pic_unblock_irqs = windermere_pic_unblock_irqs,
97 1.1 kiyohara .pic_block_irqs = windermere_pic_block_irqs,
98 1.1 kiyohara .pic_establish_irq = windermere_pic_establish_irq,
99 1.1 kiyohara };
100 1.1 kiyohara static struct pic_softc windermere_pic = {
101 1.1 kiyohara .pic_ops = &windermere_picops,
102 1.1 kiyohara .pic_maxsources = 16,
103 1.1 kiyohara .pic_name = "windermere",
104 1.1 kiyohara };
105 1.1 kiyohara static uint16_t pic_mask = 0x0000;
106 1.1 kiyohara
107 1.1 kiyohara /* ARGSUSED */
108 1.1 kiyohara static int
109 1.1 kiyohara windermere_match(device_t parent, cfdata_t match, void *aux)
110 1.1 kiyohara {
111 1.1 kiyohara
112 1.1 kiyohara return 1;
113 1.1 kiyohara }
114 1.1 kiyohara
115 1.1 kiyohara /* ARGSUSED */
116 1.1 kiyohara static void
117 1.1 kiyohara windermere_attach(device_t parent, device_t self, void *aux)
118 1.1 kiyohara {
119 1.1 kiyohara struct mainbus_attach_args *maa = aux;
120 1.1 kiyohara struct windermere_attach_args aa;
121 1.1 kiyohara bus_space_handle_t ioh;
122 1.1 kiyohara int i;
123 1.1 kiyohara
124 1.1 kiyohara aprint_naive("\n");
125 1.1 kiyohara aprint_normal("\n");
126 1.1 kiyohara
127 1.1 kiyohara if (bus_space_map(maa->mb_iot, maa->mb_iobase, ARM7XX_INTRREG_SIZE, 0,
128 1.1 kiyohara &ioh) != 0) {
129 1.1 kiyohara aprint_error_dev(self, "can't map registers\n");
130 1.1 kiyohara return;
131 1.1 kiyohara }
132 1.1 kiyohara
133 1.1 kiyohara pic_add(&windermere_pic, 0);
134 1.1 kiyohara soc_find_pending_irqs = windermere_find_pending_irqs;
135 1.1 kiyohara soc_initclocks = windermere_initclocks;
136 1.1 kiyohara soc_delay = windermere_delay;
137 1.1 kiyohara
138 1.1 kiyohara for (i = 0; i < __arraycount(windermere_periphs); i++) {
139 1.1 kiyohara aa.aa_name = windermere_periphs[i].name;
140 1.1 kiyohara aa.aa_iot = maa->mb_iot;
141 1.1 kiyohara aa.aa_ioh = &ioh;
142 1.1 kiyohara aa.aa_offset = windermere_periphs[i].offset;
143 1.1 kiyohara aa.aa_size = 0;
144 1.1 kiyohara aa.aa_irq = windermere_periphs[i].irq;
145 1.1 kiyohara
146 1.2 thorpej config_found(self, &aa, windermere_print,
147 1.2 thorpej CFARG_SUBMATCH, windermere_submatch,
148 1.2 thorpej CFARG_EOL);
149 1.1 kiyohara }
150 1.1 kiyohara }
151 1.1 kiyohara
152 1.1 kiyohara static int
153 1.1 kiyohara windermere_print(void *aux, const char *pnp)
154 1.1 kiyohara {
155 1.1 kiyohara struct windermere_attach_args *aa = aux;
156 1.1 kiyohara
157 1.1 kiyohara if (pnp)
158 1.1 kiyohara aprint_normal("%s at %s", aa->aa_name, pnp);
159 1.1 kiyohara else {
160 1.1 kiyohara if (aa->aa_offset != WINDERMERECF_OFFSET_DEFAULT) {
161 1.1 kiyohara aprint_normal(" offset 0x%04lx", aa->aa_offset);
162 1.1 kiyohara if (aa->aa_size > 0)
163 1.1 kiyohara aprint_normal("-0x%04lx",
164 1.1 kiyohara aa->aa_offset + aa->aa_size - 1);
165 1.1 kiyohara }
166 1.1 kiyohara if (aa->aa_irq != WINDERMERECF_IRQ_DEFAULT)
167 1.1 kiyohara aprint_normal(" irq %d", aa->aa_irq);
168 1.1 kiyohara }
169 1.1 kiyohara
170 1.1 kiyohara return UNCONF;
171 1.1 kiyohara }
172 1.1 kiyohara
173 1.1 kiyohara /* ARGSUSED */
174 1.1 kiyohara static int
175 1.1 kiyohara windermere_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
176 1.1 kiyohara {
177 1.1 kiyohara struct windermere_attach_args *aa = aux;
178 1.1 kiyohara
179 1.1 kiyohara /*
180 1.1 kiyohara * special case. match *kbd@windermere.
181 1.1 kiyohara * Windermere helps implement to machine-dependent keyboard.
182 1.1 kiyohara */
183 1.1 kiyohara if (strcmp(aa->aa_name, "wmkbd") == 0 &&
184 1.1 kiyohara strcmp(cf->cf_name + strlen(cf->cf_name) - 3, "kbd") == 0)
185 1.1 kiyohara return config_match(parent, cf, aux);
186 1.1 kiyohara
187 1.1 kiyohara if (strcmp(cf->cf_name, aa->aa_name) != 0)
188 1.1 kiyohara return 0;
189 1.1 kiyohara return config_match(parent, cf, aux);
190 1.1 kiyohara }
191 1.1 kiyohara
192 1.1 kiyohara
193 1.1 kiyohara static int
194 1.1 kiyohara windermere_find_pending_irqs(void)
195 1.1 kiyohara {
196 1.1 kiyohara volatile uint32_t *intr =
197 1.1 kiyohara (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_INTR_OFFSET);
198 1.1 kiyohara uint16_t pending;
199 1.1 kiyohara
200 1.1 kiyohara pending = *(intr + INTSR);
201 1.1 kiyohara if (pending & ~pic_mask) {
202 1.1 kiyohara *(intr + INTENC) = pending & ~pic_mask;
203 1.1 kiyohara printf("stray interrupt pending: 0x%04x\n",
204 1.1 kiyohara pending & ~pic_mask);
205 1.1 kiyohara pending &= pic_mask;
206 1.1 kiyohara }
207 1.1 kiyohara if (pending == 0)
208 1.1 kiyohara return 0;
209 1.1 kiyohara
210 1.1 kiyohara return pic_mark_pending_sources(&windermere_pic, 0, pending);
211 1.1 kiyohara }
212 1.1 kiyohara
213 1.1 kiyohara /* ARGSUSED */
214 1.1 kiyohara static void
215 1.1 kiyohara windermere_pic_unblock_irqs(struct pic_softc *pic, size_t irqbase,
216 1.1 kiyohara uint32_t irq_mask)
217 1.1 kiyohara {
218 1.1 kiyohara volatile uint32_t *intr =
219 1.1 kiyohara (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_INTR_OFFSET);
220 1.1 kiyohara
221 1.1 kiyohara *(intr + INTENS) = irq_mask;
222 1.1 kiyohara pic_mask |= irq_mask;
223 1.1 kiyohara }
224 1.1 kiyohara
225 1.1 kiyohara /* ARGSUSED */
226 1.1 kiyohara static void
227 1.1 kiyohara windermere_pic_block_irqs(struct pic_softc *pic, size_t irqbase,
228 1.1 kiyohara uint32_t irq_mask)
229 1.1 kiyohara {
230 1.1 kiyohara volatile uint32_t *intr =
231 1.1 kiyohara (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_INTR_OFFSET);
232 1.1 kiyohara
233 1.1 kiyohara *(intr + INTENC) = irq_mask;
234 1.1 kiyohara pic_mask &= ~irq_mask;
235 1.1 kiyohara }
236 1.1 kiyohara
237 1.1 kiyohara static void
238 1.1 kiyohara windermere_pic_establish_irq(struct pic_softc *pic, struct intrsource *is)
239 1.1 kiyohara {
240 1.1 kiyohara /* Nothing */
241 1.1 kiyohara }
242 1.1 kiyohara
243 1.1 kiyohara static void
244 1.1 kiyohara windermere_initclocks(void)
245 1.1 kiyohara {
246 1.1 kiyohara volatile uint32_t *tc1, *tc2;
247 1.1 kiyohara uint32_t ctrl;
248 1.1 kiyohara
249 1.1 kiyohara tc1 =
250 1.1 kiyohara (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_TC_OFFSET + TC1_OFFSET);
251 1.1 kiyohara tc2 =
252 1.1 kiyohara (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_TC_OFFSET + TC2_OFFSET);
253 1.1 kiyohara
254 1.1 kiyohara /* set clock to TC1 */
255 1.1 kiyohara ctrl = TC_READ(tc1, TC_CTRL);
256 1.1 kiyohara ctrl |= (CTRL_CLKSEL | CTRL_MODE | CTRL_ENABLE); /* select 512kHz */
257 1.1 kiyohara TC_WRITE(tc1, TC_CTRL, ctrl);
258 1.1 kiyohara TC_WRITE(tc1, TC_LOAD, 512 * 1000 / hz - 1); /* 512kHz / hz - 1 */
259 1.1 kiyohara intr_establish(8, IPL_CLOCK, 0, windermere_clockintr, NULL);
260 1.1 kiyohara
261 1.1 kiyohara /* set delay counter */
262 1.1 kiyohara ctrl = TC_READ(tc2, TC_CTRL);
263 1.1 kiyohara ctrl |= (CTRL_CLKSEL | CTRL_MODE | CTRL_ENABLE); /* select 512kHz */
264 1.1 kiyohara TC_WRITE(tc2, TC_CTRL, ctrl);
265 1.1 kiyohara TC_WRITE(tc2, TC_LOAD, TC_MAX);
266 1.1 kiyohara
267 1.1 kiyohara windermere_tc_init();
268 1.1 kiyohara }
269 1.1 kiyohara
270 1.1 kiyohara static int
271 1.1 kiyohara windermere_clockintr(void *arg)
272 1.1 kiyohara {
273 1.1 kiyohara volatile uint32_t *tc1;
274 1.1 kiyohara
275 1.1 kiyohara tc1 =
276 1.1 kiyohara (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_TC_OFFSET + TC1_OFFSET);
277 1.1 kiyohara TC_WRITE(tc1, TC_EOI, EOI_EOI);
278 1.1 kiyohara
279 1.1 kiyohara hardclock(arg);
280 1.1 kiyohara
281 1.1 kiyohara return 1;
282 1.1 kiyohara }
283 1.1 kiyohara
284 1.1 kiyohara static void
285 1.1 kiyohara windermere_tc_init(void)
286 1.1 kiyohara {
287 1.1 kiyohara static struct timecounter windermere_tc = {
288 1.1 kiyohara .tc_get_timecount = windermere_get_timecount,
289 1.1 kiyohara .tc_counter_mask = TC_MASK,
290 1.1 kiyohara .tc_frequency = 512000,
291 1.1 kiyohara .tc_name = "windermere",
292 1.1 kiyohara .tc_quality = 100,
293 1.1 kiyohara };
294 1.1 kiyohara
295 1.1 kiyohara tc_init(&windermere_tc);
296 1.1 kiyohara }
297 1.1 kiyohara
298 1.1 kiyohara static u_int
299 1.1 kiyohara windermere_get_timecount(struct timecounter *tc)
300 1.1 kiyohara {
301 1.1 kiyohara volatile uint32_t *tc2;
302 1.1 kiyohara
303 1.1 kiyohara tc2 =
304 1.1 kiyohara (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_TC_OFFSET + TC2_OFFSET);
305 1.1 kiyohara
306 1.1 kiyohara return TC_READ(tc2, TC_VAL) ^ TC_MASK; /* It is decremental counter */
307 1.1 kiyohara }
308 1.1 kiyohara
309 1.1 kiyohara static void
310 1.1 kiyohara windermere_delay(unsigned int us)
311 1.1 kiyohara {
312 1.1 kiyohara volatile uint32_t *tc2;
313 1.1 kiyohara int prev, now, remaining;
314 1.1 kiyohara
315 1.1 kiyohara tc2 =
316 1.1 kiyohara (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_TC_OFFSET + TC2_OFFSET);
317 1.1 kiyohara
318 1.1 kiyohara prev = TC_READ(tc2, TC_VAL) & TC_MASK;
319 1.1 kiyohara remaining = us * 512 / 1000 + 1;
320 1.1 kiyohara
321 1.1 kiyohara while (remaining > 0) {
322 1.1 kiyohara now = TC_READ(tc2, TC_VAL) & TC_MASK;
323 1.1 kiyohara if (now >= prev)
324 1.1 kiyohara remaining -= (now - prev);
325 1.1 kiyohara else
326 1.1 kiyohara remaining -= (USHRT_MAX - now + prev + 1);
327 1.1 kiyohara prev = now;
328 1.1 kiyohara }
329 1.1 kiyohara }
330