machdep.c revision 1.15 1 /* $NetBSD: machdep.c,v 1.15 2020/07/22 01:24:39 msaitoh Exp $ */
2
3 /*-
4 * Copyright (c) 2014 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.15 2020/07/22 01:24:39 msaitoh Exp $");
31
32 #include "opt_ddb.h"
33 #include "opt_kgdb.h"
34 #include "opt_modular.h"
35 #include "opt_multiprocessor.h"
36
37 #include <sys/param.h>
38 #include <sys/boot_flag.h>
39 #include <sys/device.h>
40 #include <sys/kernel.h>
41 #include <sys/kcore.h>
42 #include <sys/ksyms.h>
43 #include <sys/mount.h>
44 #include <sys/reboot.h>
45 #include <sys/cpu.h>
46 #include <sys/bus.h>
47 #include <sys/mutex.h>
48
49 #include <uvm/uvm_extern.h>
50
51 #include <dev/cons.h>
52
53 #include "ksyms.h"
54
55 #if NKSYMS || defined(DDB) || defined(MODULAR)
56 #include <mips/db_machdep.h>
57 #include <ddb/db_extern.h>
58 #endif
59
60 #include <mips/cache.h>
61 #include <mips/locore.h>
62 #include <mips/cpuregs.h>
63
64 #include <mips/ingenic/ingenic_coreregs.h>
65 #include <mips/ingenic/ingenic_regs.h>
66 #include <mips/ingenic/ingenic_var.h>
67
68 #include "opt_ingenic.h"
69
70 /* Maps for VM objects. */
71 struct vm_map *phys_map = NULL;
72
73 int maxmem; /* max memory per process */
74
75 int mem_cluster_cnt;
76 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
77
78 void mach_init(void); /* XXX */
79 void ingenic_reset(void);
80
81 void ingenic_putchar_init(void);
82 void ingenic_puts(const char *);
83 void ingenic_com_cnattach(void);
84
85 #ifdef MULTIPROCESSOR
86 kmutex_t ingenic_ipi_lock;
87 #endif
88
89 static void
90 cal_timer(void)
91 {
92 uint32_t cntfreq;
93 volatile uint32_t junk;
94
95 /*
96 * The manual seems to imply that EXCCLK is 12MHz, although in real
97 * life it appears to be 48MHz. Either way, we want a 12MHz counter.
98 */
99 curcpu()->ci_cpu_freq = 1200000000; /* for now */
100 cntfreq = 12000000; /* EXTCLK / 4 */
101
102 curcpu()->ci_cctr_freq = cntfreq;
103 curcpu()->ci_cycles_per_hz = (cntfreq + hz / 2) / hz;
104
105 /* Compute number of cycles per 1us (1/MHz). 0.5MHz is for roundup. */
106 curcpu()->ci_divisor_delay = ((cntfreq + 500000) / 1000000);
107
108 /* actually start the counter now */
109 /* stop OS timer */
110 writereg(JZ_TC_TECR, TESR_OST);
111 /* zero everything */
112 writereg(JZ_OST_CTRL, 0);
113 writereg(JZ_OST_CNT_LO, 0);
114 writereg(JZ_OST_CNT_HI, 0);
115 writereg(JZ_OST_DATA, 0xffffffff);
116 /* use EXTCLK, don't reset */
117 writereg(JZ_OST_CTRL, OSTC_EXT_EN | OSTC_MODE | OSTC_DIV_4);
118 /* start the timer */
119 writereg(JZ_TC_TESR, TESR_OST);
120 /* make sure the timer actually runs */
121 junk = readreg(JZ_OST_CNT_LO);
122 do {} while (junk == readreg(JZ_OST_CNT_LO));
123 }
124
125 #ifdef MULTIPROCESSOR
126 static void
127 ingenic_cpu_init(struct cpu_info *ci)
128 {
129 uint32_t reg;
130
131 /* enable IPIs for this core */
132 reg = mips_cp0_corereim_read();
133 if (cpu_index(ci) == 1) {
134 reg |= REIM_MIRQ1_M;
135 } else
136 reg |= REIM_MIRQ0_M;
137 mips_cp0_corereim_write(reg);
138 printf("%s %d %08x\n", __func__, cpu_index(ci), reg);
139 }
140
141 static int
142 ingenic_send_ipi(struct cpu_info *ci, int tag)
143 {
144 uint32_t msg;
145
146 msg = 1 << tag;
147
148 mutex_enter(&ingenic_ipi_lock);
149 if (kcpuset_isset(cpus_running, cpu_index(ci))) {
150 if (cpu_index(ci) == 0) {
151 mips_cp0_corembox_write(msg, 0);
152 } else {
153 mips_cp0_corembox_write(msg, 1);
154 }
155 }
156 mutex_exit(&ingenic_ipi_lock);
157 return 0;
158 }
159 #endif /* MULTIPROCESSOR */
160
161 void
162 mach_init(void)
163 {
164 void *kernend;
165 uint32_t memsize;
166 extern char edata[], end[]; /* XXX */
167
168 /* clear the BSS segment */
169 kernend = (void *)mips_round_page(end);
170
171 memset(edata, 0, (char *)kernend - edata);
172
173 /* setup early console */
174 ingenic_putchar_init();
175
176 /* set CPU model info for sysctl_hw */
177 cpu_setmodel("Ingenic XBurst");
178 mips_vector_init(NULL, false);
179 cal_timer();
180 uvm_md_init();
181 /*
182 * Look at arguments passed to us and compute boothowto.
183 */
184 boothowto = RB_AUTOBOOT;
185 #ifdef KADB
186 boothowto |= RB_KDB;
187 #endif
188
189 /*
190 * Determine the memory size.
191 *
192 * Note: Reserve the first page! That's where the trap
193 * vectors are located.
194 */
195 memsize = 0x40000000;
196
197 printf("Memory size: 0x%08x\n", memsize);
198 physmem = btoc(memsize);
199
200 /*
201 * memory is at 0x20000000 with first 256MB mirrored to 0x00000000 so
202 * we can see them through KSEG*
203 * assume 1GB for now, the SoC can theoretically support up to 3GB
204 */
205 mem_clusters[0].start = PAGE_SIZE;
206 mem_clusters[0].size = 0x10000000 - PAGE_SIZE;
207 mem_clusters[1].start = 0x30000000;
208 mem_clusters[1].size = 0x30000000;
209 mem_cluster_cnt = 2;
210
211 /*
212 * Load the available pages into the VM system.
213 */
214 mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
215 mem_clusters, mem_cluster_cnt, NULL, 0);
216
217 /*
218 * Initialize message buffer (at end of core).
219 */
220 mips_init_msgbuf();
221
222 /*
223 * Initialize the virtual memory system.
224 */
225 pmap_bootstrap();
226
227 /*
228 * Allocate uarea page for lwp0 and set it.
229 */
230 mips_init_lwp0_uarea();
231
232 #ifdef MULTIPROCESSOR
233 mutex_init(&ingenic_ipi_lock, MUTEX_DEFAULT, IPL_HIGH);
234 mips_locoresw.lsw_send_ipi = ingenic_send_ipi;
235 mips_locoresw.lsw_cpu_init = ingenic_cpu_init;
236 #endif
237
238 apbus_init();
239 /*
240 * Initialize debuggers, and break into them, if appropriate.
241 */
242 #ifdef DDB
243 if (boothowto & RB_KDB)
244 Debugger();
245 #endif
246 }
247
248 void
249 consinit(void)
250 {
251 /*
252 * Everything related to console initialization is done
253 * in mach_init().
254 */
255 apbus_init();
256 ingenic_com_cnattach();
257 }
258
259 void
260 cpu_startup(void)
261 {
262 cpu_startup_common();
263 }
264
265 void
266 cpu_reboot(int howto, char *bootstr)
267 {
268 static int waittime = -1;
269
270 /* Take a snapshot before clobbering any registers. */
271 savectx(curpcb);
272
273 /* If "always halt" was specified as a boot flag, obey. */
274 if (boothowto & RB_HALT)
275 howto |= RB_HALT;
276
277 boothowto = howto;
278
279 /* If system is cold, just halt. */
280 if (cold) {
281 boothowto |= RB_HALT;
282 goto haltsys;
283 }
284
285 if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
286 waittime = 0;
287
288 /*
289 * Synchronize the disks....
290 */
291 vfs_shutdown();
292
293 /*
294 * If we've been adjusting the clock, the todr
295 * will be out of synch; adjust it now.
296 */
297 resettodr();
298 }
299
300 /* Disable interrupts. */
301 splhigh();
302
303 if (boothowto & RB_DUMP)
304 dumpsys();
305
306 haltsys:
307 /* Run any shutdown hooks. */
308 doshutdownhooks();
309
310 pmf_system_shutdown(boothowto);
311
312 #if 0
313 if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN)
314 if (board && board->ab_poweroff)
315 board->ab_poweroff();
316 #endif
317
318 /*
319 * Firmware may autoboot (depending on settings), and we cannot pass
320 * flags to it (at least I haven't figured out how to yet), so
321 * we "pseudo-halt" now.
322 */
323 if (boothowto & RB_HALT) {
324 printf("\n");
325 printf("The operating system has halted.\n");
326 printf("Please press any key to reboot.\n\n");
327 cnpollc(1); /* For proper keyboard command handling */
328 cngetc();
329 cnpollc(0);
330 }
331
332 printf("resetting board...\n\n");
333 mips_icache_sync_all();
334 mips_dcache_wbinv_all();
335 ingenic_reset();
336 __asm volatile("jr %0" :: "r"(MIPS_RESET_EXC_VEC));
337 printf("Oops, back from reset\n\nSpinning...");
338 for (;;)
339 /* spin forever */ ; /* XXX */
340 /*NOTREACHED*/
341 }
342
343 void
344 ingenic_reset(void)
345 {
346 /*
347 * for now, provoke a watchdog reset in about a second, so UART buffers
348 * have a fighting chance to flush before we pull the plug
349 */
350 writereg(JZ_WDOG_TCER, 0); /* disable watchdog */
351 writereg(JZ_WDOG_TCNT, 0); /* reset counter */
352 writereg(JZ_WDOG_TDR, 128); /* wait for ~1s */
353 writereg(JZ_WDOG_TCSR, TCSR_RTC_EN | TCSR_DIV_256);
354 writereg(JZ_WDOG_TCER, TCER_ENABLE); /* fire! */
355 }
356