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