xref: /netbsd-src/sys/arch/epoc32/windermere/windermere.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /*	$NetBSD: windermere.c,v 1.3 2021/08/07 16:18:48 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.3 2021/08/07 16:18:48 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
windermere_match(device_t parent,cfdata_t match,void * aux)109 windermere_match(device_t parent, cfdata_t match, void *aux)
110 {
111 
112 	return 1;
113 }
114 
115 /* ARGSUSED */
116 static void
windermere_attach(device_t parent,device_t self,void * aux)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 		    CFARGS(.submatch = windermere_submatch));
148 	}
149 }
150 
151 static int
windermere_print(void * aux,const char * pnp)152 windermere_print(void *aux, const char *pnp)
153 {
154 	struct windermere_attach_args *aa = aux;
155 
156 	if (pnp)
157 		aprint_normal("%s at %s", aa->aa_name, pnp);
158 	else {
159 		if (aa->aa_offset != WINDERMERECF_OFFSET_DEFAULT) {
160 			aprint_normal(" offset 0x%04lx", aa->aa_offset);
161 			if (aa->aa_size > 0)
162 				aprint_normal("-0x%04lx",
163 				    aa->aa_offset + aa->aa_size - 1);
164 		}
165 		if (aa->aa_irq != WINDERMERECF_IRQ_DEFAULT)
166 			aprint_normal(" irq %d", aa->aa_irq);
167 	}
168 
169 	return UNCONF;
170 }
171 
172 /* ARGSUSED */
173 static int
windermere_submatch(device_t parent,cfdata_t cf,const int * ldesc,void * aux)174 windermere_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
175 {
176 	struct windermere_attach_args *aa = aux;
177 
178 	/*
179 	 * special case. match *kbd@windermere.
180 	 * Windermere helps implement to machine-dependent keyboard.
181 	 */
182 	if (strcmp(aa->aa_name, "wmkbd") == 0 &&
183 	    strcmp(cf->cf_name + strlen(cf->cf_name) - 3, "kbd") == 0)
184 		return config_match(parent, cf, aux);
185 
186 	if (strcmp(cf->cf_name, aa->aa_name) != 0)
187 		return 0;
188 	return config_match(parent, cf, aux);
189 }
190 
191 
192 static int
windermere_find_pending_irqs(void)193 windermere_find_pending_irqs(void)
194 {
195 	volatile uint32_t *intr =
196 	    (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_INTR_OFFSET);
197 	uint16_t pending;
198 
199 	pending = *(intr + INTSR);
200 	if (pending & ~pic_mask) {
201 		*(intr + INTENC) = pending & ~pic_mask;
202 		printf("stray interrupt pending: 0x%04x\n",
203 		    pending & ~pic_mask);
204 		pending &= pic_mask;
205 	}
206 	if (pending == 0)
207 		return 0;
208 
209 	return pic_mark_pending_sources(&windermere_pic, 0, pending);
210 }
211 
212 /* ARGSUSED */
213 static void
windermere_pic_unblock_irqs(struct pic_softc * pic,size_t irqbase,uint32_t irq_mask)214 windermere_pic_unblock_irqs(struct pic_softc *pic, size_t irqbase,
215 			    uint32_t irq_mask)
216 {
217 	volatile uint32_t *intr =
218 	    (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_INTR_OFFSET);
219 
220 	*(intr + INTENS) = irq_mask;
221 	pic_mask |= irq_mask;
222 }
223 
224 /* ARGSUSED */
225 static void
windermere_pic_block_irqs(struct pic_softc * pic,size_t irqbase,uint32_t irq_mask)226 windermere_pic_block_irqs(struct pic_softc *pic, size_t irqbase,
227 			  uint32_t irq_mask)
228 {
229 	volatile uint32_t *intr =
230 	    (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_INTR_OFFSET);
231 
232 	*(intr + INTENC) = irq_mask;
233 	pic_mask &= ~irq_mask;
234 }
235 
236 static void
windermere_pic_establish_irq(struct pic_softc * pic,struct intrsource * is)237 windermere_pic_establish_irq(struct pic_softc *pic, struct intrsource *is)
238 {
239 	/* Nothing */
240 }
241 
242 static void
windermere_initclocks(void)243 windermere_initclocks(void)
244 {
245 	volatile uint32_t *tc1, *tc2;
246 	uint32_t ctrl;
247 
248 	tc1 =
249 	 (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_TC_OFFSET + TC1_OFFSET);
250 	tc2 =
251 	 (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_TC_OFFSET + TC2_OFFSET);
252 
253 	/* set clock to TC1 */
254 	ctrl = TC_READ(tc1, TC_CTRL);
255 	ctrl |= (CTRL_CLKSEL | CTRL_MODE | CTRL_ENABLE); /* select 512kHz */
256 	TC_WRITE(tc1, TC_CTRL, ctrl);
257 	TC_WRITE(tc1, TC_LOAD, 512 * 1000 / hz - 1);	/* 512kHz / hz - 1 */
258 	intr_establish(8, IPL_CLOCK, 0, windermere_clockintr, NULL);
259 
260 	/* set delay counter */
261 	ctrl = TC_READ(tc2, TC_CTRL);
262 	ctrl |= (CTRL_CLKSEL | CTRL_MODE | CTRL_ENABLE); /* select 512kHz */
263 	TC_WRITE(tc2, TC_CTRL, ctrl);
264 	TC_WRITE(tc2, TC_LOAD, TC_MAX);
265 
266 	windermere_tc_init();
267 }
268 
269 static int
windermere_clockintr(void * arg)270 windermere_clockintr(void *arg)
271 {
272 	volatile uint32_t *tc1;
273 
274 	tc1 =
275 	 (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_TC_OFFSET + TC1_OFFSET);
276 	TC_WRITE(tc1, TC_EOI, EOI_EOI);
277 
278 	hardclock(arg);
279 
280 	return 1;
281 }
282 
283 static void
windermere_tc_init(void)284 windermere_tc_init(void)
285 {
286 	static struct timecounter windermere_tc = {
287 		.tc_get_timecount = windermere_get_timecount,
288 		.tc_counter_mask = TC_MASK,
289 		.tc_frequency = 512000,
290 		.tc_name = "windermere",
291 		.tc_quality = 100,
292 	};
293 
294 	tc_init(&windermere_tc);
295 }
296 
297 static u_int
windermere_get_timecount(struct timecounter * tc)298 windermere_get_timecount(struct timecounter *tc)
299 {
300 	volatile uint32_t *tc2;
301 
302 	tc2 =
303 	 (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_TC_OFFSET + TC2_OFFSET);
304 
305 	return TC_READ(tc2, TC_VAL) ^ TC_MASK;	/* It is decremental counter */
306 }
307 
308 static void
windermere_delay(unsigned int us)309 windermere_delay(unsigned int us)
310 {
311 	volatile uint32_t *tc2;
312 	int prev, now, remaining;
313 
314 	tc2 =
315 	 (uint32_t *)(ARM7XX_INTRREG_VBASE + WINDERMERE_TC_OFFSET + TC2_OFFSET);
316 
317 	prev = TC_READ(tc2, TC_VAL) & TC_MASK;
318 	remaining = us * 512 / 1000 + 1;
319 
320 	while (remaining > 0) {
321 		now = TC_READ(tc2, TC_VAL) & TC_MASK;
322 		if (now >= prev)
323 			remaining -= (now - prev);
324 		else
325 			remaining -= (USHRT_MAX - now + prev + 1);
326 		prev = now;
327 	}
328 }
329