machdep.c revision 1.6 1 /* $NetBSD: machdep.c,v 1.6 2015/04/04 13:06:01 macallan 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.6 2015/04/04 13:06:01 macallan Exp $");
31
32 #include "opt_ddb.h"
33 #include "opt_kgdb.h"
34 #include "opt_modular.h"
35
36 #include <sys/param.h>
37 #include <sys/boot_flag.h>
38 #include <sys/device.h>
39 #include <sys/kernel.h>
40 #include <sys/kcore.h>
41 #include <sys/ksyms.h>
42 #include <sys/mount.h>
43 #include <sys/reboot.h>
44 #include <sys/cpu.h>
45 #include <sys/bus.h>
46
47 #include <uvm/uvm_extern.h>
48
49 #include <dev/cons.h>
50
51 #include "ksyms.h"
52
53 #if NKSYMS || defined(DDB) || defined(MODULAR)
54 #include <mips/db_machdep.h>
55 #include <ddb/db_extern.h>
56 #endif
57
58 #include <mips/cache.h>
59 #include <mips/locore.h>
60 #include <mips/cpuregs.h>
61
62 #include <mips/ingenic/ingenic_regs.h>
63 #include <mips/ingenic/ingenic_var.h>
64
65 #include "opt_ingenic.h"
66
67 /* Maps for VM objects. */
68 struct vm_map *phys_map = NULL;
69
70 int maxmem; /* max memory per process */
71
72 int mem_cluster_cnt;
73 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
74
75 void mach_init(void); /* XXX */
76 void ingenic_reset(void);
77
78 void ingenic_putchar_init(void);
79 void ingenic_puts(const char *);
80 void ingenic_com_cnattach(void);
81
82 static void
83 cal_timer(void)
84 {
85 uint32_t cntfreq;
86 volatile uint32_t junk;
87
88 /*
89 * The manual seems to imply that EXCCLK is 12MHz, although in real
90 * life it appears to be 48MHz. Either way, we want a 12MHz counter.
91 */
92 curcpu()->ci_cpu_freq = 1200000000; /* for now */
93 cntfreq = 12000000; /* EXTCLK / 4 */
94
95 curcpu()->ci_cctr_freq = cntfreq;
96 curcpu()->ci_cycles_per_hz = (cntfreq + hz / 2) / hz;
97
98 /* Compute number of cycles per 1us (1/MHz). 0.5MHz is for roundup. */
99 curcpu()->ci_divisor_delay = ((cntfreq + 500000) / 1000000);
100
101 /* actually start the counter now */
102 /* stop OS timer */
103 writereg(JZ_TC_TECR, TESR_OST);
104 /* zero everything */
105 writereg(JZ_OST_CTRL, 0);
106 writereg(JZ_OST_CNT_LO, 0);
107 writereg(JZ_OST_CNT_HI, 0);
108 writereg(JZ_OST_DATA, 0xffffffff);
109 /* use EXTCLK, don't reset */
110 writereg(JZ_OST_CTRL, OSTC_EXT_EN | OSTC_MODE | OSTC_DIV_4);
111 /* start the timer */
112 writereg(JZ_TC_TESR, TESR_OST);
113 /* make sure the timer actually runs */
114 junk = readreg(JZ_OST_CNT_LO);
115 do {} while (junk == readreg(JZ_OST_CNT_LO));
116 }
117
118 #ifdef MULTIPROCESSOR
119 static void
120 ingenic_cpu_init(struct cpu_info *ci)
121 {
122 uint32_t reg;
123
124 /* enable IPIs for this core */
125 reg = MFC0(12, 4); /* reset entry and interrupts */
126 reg &= 0xffff0000;
127 if (cpu_index(ci) == 1) {
128 reg |= REIM_MIRQ1_M;
129 } else
130 reg |= REIM_MIRQ0_M;
131 MTC0(reg, 12, 4);
132 }
133
134 static int
135 ingenic_send_ipi(struct cpu_info *ci, int tag)
136 {
137 uint32_t msg;
138
139 msg = 1 << tag;
140
141 if (cpus_running & (1 << cpu_index(ci))) {
142 if (cpu_index(ci) == 0) {
143 MTC0(msg, CP0_CORE_MBOX, 0);
144 } else {
145 MTC0(msg, CP0_CORE_MBOX, 1);
146 }
147 }
148 return 0;
149 }
150 #endif
151
152 void
153 mach_init(void)
154 {
155 void *kernend;
156 uint32_t memsize;
157 extern char edata[], end[]; /* XXX */
158
159 /* clear the BSS segment */
160 kernend = (void *)mips_round_page(end);
161
162 memset(edata, 0, (char *)kernend - edata);
163
164 /* setup early console */
165 ingenic_putchar_init();
166
167 /* set CPU model info for sysctl_hw */
168 cpu_setmodel("Ingenic XBurst");
169 mips_vector_init(NULL, false);
170 cal_timer();
171 uvm_setpagesize();
172 /*
173 * Look at arguments passed to us and compute boothowto.
174 */
175 boothowto = RB_AUTOBOOT;
176 #ifdef KADB
177 boothowto |= RB_KDB;
178 #endif
179
180 /*
181 * Determine the memory size.
182 *
183 * Note: Reserve the first page! That's where the trap
184 * vectors are located.
185 */
186 memsize = 0x40000000;
187
188 printf("Memory size: 0x%08x\n", memsize);
189 physmem = btoc(memsize);
190
191 /*
192 * memory is at 0x20000000 with first 256MB mirrored to 0x00000000 so
193 * we can see them through KSEG*
194 * assume 1GB for now, the SoC can theoretically support up to 3GB
195 */
196 mem_clusters[0].start = PAGE_SIZE;
197 mem_clusters[0].size = 0x10000000 - PAGE_SIZE;
198 mem_clusters[1].start = 0x30000000;
199 mem_clusters[1].size = 0x30000000;
200 mem_cluster_cnt = 2;
201
202 /*
203 * Load the available pages into the VM system.
204 */
205 mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
206 mem_clusters, mem_cluster_cnt, NULL, 0);
207
208 /*
209 * Initialize message buffer (at end of core).
210 */
211 mips_init_msgbuf();
212
213 /*
214 * Initialize the virtual memory system.
215 */
216 pmap_bootstrap();
217
218 /*
219 * Allocate uarea page for lwp0 and set it.
220 */
221 mips_init_lwp0_uarea();
222
223 #ifdef MULTIPROCESSOR
224 mips_locoresw.lsw_send_ipi = ingenic_send_ipi;
225 mips_locoresw.lsw_cpu_init = ingenic_cpu_init;
226 #endif
227
228 apbus_init();
229 /*
230 * Initialize debuggers, and break into them, if appropriate.
231 */
232 #ifdef DDB
233 if (boothowto & RB_KDB)
234 Debugger();
235 #endif
236 }
237
238 void
239 consinit(void)
240 {
241 /*
242 * Everything related to console initialization is done
243 * in mach_init().
244 */
245 ingenic_com_cnattach();
246 }
247
248 void
249 cpu_startup(void)
250 {
251 char pbuf[9];
252 vaddr_t minaddr, maxaddr;
253 #ifdef DEBUG
254 extern int pmapdebug; /* XXX */
255 int opmapdebug = pmapdebug;
256
257 pmapdebug = 0; /* Shut up pmap debug during bootstrap */
258 #endif
259
260 /*
261 * Good {morning,afternoon,evening,night}.
262 */
263 printf("%s%s", copyright, version);
264 printf("%s\n", cpu_getmodel());
265 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
266 printf("total memory = %s\n", pbuf);
267
268 minaddr = 0;
269 /*
270 * Allocate a submap for physio
271 */
272 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
273 VM_PHYS_SIZE, 0, FALSE, NULL);
274
275 /*
276 * No need to allocate an mbuf cluster submap. Mbuf clusters
277 * are allocated via the pool allocator, and we use KSEG to
278 * map those pages.
279 */
280
281 #ifdef DEBUG
282 pmapdebug = opmapdebug;
283 #endif
284 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
285 printf("avail memory = %s\n", pbuf);
286 }
287
288 void
289 cpu_reboot(int howto, char *bootstr)
290 {
291 static int waittime = -1;
292
293 /* Take a snapshot before clobbering any registers. */
294 savectx(curpcb);
295
296 /* If "always halt" was specified as a boot flag, obey. */
297 if (boothowto & RB_HALT)
298 howto |= RB_HALT;
299
300 boothowto = howto;
301
302 /* If system is cold, just halt. */
303 if (cold) {
304 boothowto |= RB_HALT;
305 goto haltsys;
306 }
307
308 if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
309 waittime = 0;
310
311 /*
312 * Synchronize the disks....
313 */
314 vfs_shutdown();
315
316 /*
317 * If we've been adjusting the clock, the todr
318 * will be out of synch; adjust it now.
319 */
320 resettodr();
321 }
322
323 /* Disable interrupts. */
324 splhigh();
325
326 if (boothowto & RB_DUMP)
327 dumpsys();
328
329 haltsys:
330 /* Run any shutdown hooks. */
331 doshutdownhooks();
332
333 pmf_system_shutdown(boothowto);
334
335 #if 0
336 if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN)
337 if (board && board->ab_poweroff)
338 board->ab_poweroff();
339 #endif
340
341 /*
342 * Firmware may autoboot (depending on settings), and we cannot pass
343 * flags to it (at least I haven't figured out how to yet), so
344 * we "pseudo-halt" now.
345 */
346 if (boothowto & RB_HALT) {
347 printf("\n");
348 printf("The operating system has halted.\n");
349 printf("Please press any key to reboot.\n\n");
350 cnpollc(1); /* For proper keyboard command handling */
351 cngetc();
352 cnpollc(0);
353 }
354
355 printf("reseting board...\n\n");
356 mips_icache_sync_all();
357 mips_dcache_wbinv_all();
358 ingenic_reset();
359 __asm volatile("jr %0" :: "r"(MIPS_RESET_EXC_VEC));
360 printf("Oops, back from reset\n\nSpinning...");
361 for (;;)
362 /* spin forever */ ; /* XXX */
363 /*NOTREACHED*/
364 }
365
366 void
367 ingenic_reset(void)
368 {
369 /*
370 * for now, provoke a watchdog reset in about a second, so UART buffers
371 * have a fighting chance to flush before we pull the plug
372 */
373 writereg(JZ_WDOG_TCER, 0); /* disable watchdog */
374 writereg(JZ_WDOG_TCNT, 0); /* reset counter */
375 writereg(JZ_WDOG_TDR, 128); /* wait for ~1s */
376 writereg(JZ_WDOG_TCSR, TCSR_RTC_EN | TCSR_DIV_256);
377 writereg(JZ_WDOG_TCER, TCER_ENABLE); /* fire! */
378 }
379