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