xref: /freebsd-13-stable/sys/mips/ingenic/jz4780_machdep.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * Copyright (c) 2009 Oleksandr Tymoshenko
3  * Copyright (c) 2015 Alexander Kabaev
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 AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include "opt_ddb.h"
30 #include "opt_platform.h"
31 
32 #include <sys/param.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/boot.h>
38 #include <sys/cons.h>
39 #include <sys/kdb.h>
40 #include <sys/mutex.h>
41 #include <sys/reboot.h>
42 
43 #ifdef FDT
44 #include <dev/fdt/fdt_common.h>
45 #include <dev/ofw/openfirm.h>
46 #endif
47 
48 #include <vm/vm.h>
49 #include <vm/vm_param.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_phys.h>
52 #include <vm/vm_dumpset.h>
53 
54 #include <net/ethernet.h>
55 
56 #include <machine/clock.h>
57 #include <machine/cpu.h>
58 #include <machine/cpufunc.h>
59 #include <machine/hwfunc.h>
60 #include <machine/md_var.h>
61 #include <machine/trap.h>
62 
63 #include <mips/ingenic/jz4780_regs.h>
64 #include <mips/ingenic/jz4780_cpuregs.h>
65 
66 uint32_t * const led = (uint32_t *)0xb0010548;
67 
68 extern char edata[], end[];
69 static char boot1_env[4096];
70 
71 void
platform_cpu_init(void)72 platform_cpu_init(void)
73 {
74 	uint32_t reg;
75 
76 	/*
77 	 * Do not expect mbox interrups while writing
78 	 * mbox
79 	 */
80 	reg = mips_rd_xburst_reim();
81 	reg &= ~JZ_REIM_MIRQ0M;
82 	mips_wr_xburst_reim(reg);
83 
84 	/* Clean mailboxes */
85 	mips_wr_xburst_mbox0(0);
86 	mips_wr_xburst_mbox1(0);
87 	mips_wr_xburst_core_sts(~JZ_CORESTS_MIRQ0P);
88 
89 	/* Unmask mbox interrupts */
90 	reg |= JZ_REIM_MIRQ0M;
91 	mips_wr_xburst_reim(reg);
92 }
93 
94 void
platform_reset(void)95 platform_reset(void)
96 {
97 	/*
98 	 * For now, provoke a watchdog reset in about a second, so UART buffers
99 	 * have a fighting chance to flush before we pull the plug
100 	 */
101 	writereg(JZ_TCU_BASE + JZ_WDOG_TCER, 0);	/* disable watchdog */
102 	writereg(JZ_TCU_BASE + JZ_WDOG_TCNT, 0);	/* reset counter */
103 	writereg(JZ_TCU_BASE + JZ_WDOG_TDR, 128);	/* wait for ~1s */
104 	writereg(JZ_TCU_BASE + JZ_WDOG_TCSR, TCSR_RTC_EN | TCSR_DIV_256);
105 	writereg(JZ_TCU_BASE + JZ_WDOG_TCER, TCER_ENABLE);	/* fire! */
106 
107 	/* Wait for reset */
108 	while (1)
109 		;
110 }
111 
112 static void
mips_init(void)113 mips_init(void)
114 {
115 	int i;
116 #ifdef FDT
117 	struct mem_region mr[FDT_MEM_REGIONS];
118 	uint64_t val;
119 	int mr_cnt;
120 	int j;
121 #endif
122 
123 	for (i = 0; i < 10; i++) {
124 		phys_avail[i] = 0;
125 	}
126 
127 	/* The minimal amount of memory Ingenic SoC can have. */
128 	dump_avail[0] = phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
129 	physmem = realmem = btoc(32 * 1024 * 1024);
130 
131 	/*
132 	 * X1000 mips cpu special.
133 	 * TODO: do anyone know what is this ?
134 	 */
135 	__asm(
136 		"li	$2, 0xa9000000	\n\t"
137 		"mtc0	$2, $5, 4	\n\t"
138 		"nop			\n\t"
139 		::"r"(2));
140 
141 #ifdef FDT
142 	if (fdt_get_mem_regions(mr, &mr_cnt, &val) == 0) {
143 		physmem = realmem = btoc(val);
144 
145 		KASSERT((phys_avail[0] >= mr[0].mr_start) && \
146 			(phys_avail[0] < (mr[0].mr_start + mr[0].mr_size)),
147 			("First region is not within FDT memory range"));
148 
149 		/* Limit size of the first region */
150 		phys_avail[1] = (mr[0].mr_start + MIN(mr[0].mr_size, ctob(realmem)));
151 		dump_avail[1] = phys_avail[1];
152 
153 		/* Add the rest of regions */
154 		for (i = 1, j = 2; i < mr_cnt; i++, j+=2) {
155 			phys_avail[j] = mr[i].mr_start;
156 			phys_avail[j+1] = (mr[i].mr_start + mr[i].mr_size);
157 			dump_avail[j] = phys_avail[j];
158 			dump_avail[j+1] = phys_avail[j+1];
159 		}
160 	}
161 #endif
162 
163 	init_param1();
164 	init_param2(physmem);
165 	mips_cpu_init();
166 	pmap_bootstrap();
167 	mips_proc0_init();
168 	mutex_init();
169 	kdb_init();
170 	led[0] = 0x8000;
171 #ifdef KDB
172 	if (boothowto & RB_KDB)
173 		kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
174 #endif
175 }
176 
177 void
platform_start(__register_t a0,__register_t a1,__register_t a2 __unused,__register_t a3 __unused)178 platform_start(__register_t a0,  __register_t a1,
179     __register_t a2 __unused, __register_t a3 __unused)
180 {
181 	char **argv;
182 	int  argc;
183 	vm_offset_t kernend;
184 #ifdef FDT
185 	vm_offset_t dtbp;
186 	phandle_t chosen;
187 	char buf[2048];		/* early stack supposedly big enough */
188 #endif
189 	/*
190 	 * clear the BSS and SBSS segments, this should be first call in
191 	 * the function
192 	 */
193 	kernend = (vm_offset_t)&end;
194 	memset(&edata, 0, kernend - (vm_offset_t)(&edata));
195 
196 	mips_postboot_fixup();
197 
198 	/* Initialize pcpu stuff */
199 	mips_pcpu0_init();
200 
201 	/* Something to hold kernel env until kmem is available */
202 	init_static_kenv(boot1_env, sizeof(boot1_env));
203 #ifdef FDT
204 	/*
205 	 * Find the dtb passed in by the boot loader (currently fictional).
206 	 */
207 	dtbp = (vm_offset_t)NULL;
208 
209 #if defined(FDT_DTB_STATIC)
210 	/*
211 	 * In case the device tree blob was not retrieved (from metadata) try
212 	 * to use the statically embedded one.
213 	 */
214 	if (dtbp == (vm_offset_t)NULL)
215 		dtbp = (vm_offset_t)&fdt_static_dtb;
216 #else
217 #error	"Non-static FDT not supported on JZ4780"
218 #endif
219 	if (OF_install(OFW_FDT, 0) == FALSE)
220 		while (1);
221 	if (OF_init((void *)dtbp) != 0)
222 		while (1);
223 #endif
224 
225 	cninit();
226 #ifdef FDT
227 	/*
228 	 * Get bootargs from FDT if specified.
229 	 */
230 	chosen = OF_finddevice("/chosen");
231 	if (OF_getprop(chosen, "bootargs", buf, sizeof(buf)) != -1)
232 		boothowto |= boot_parse_cmdline(buf);
233 #endif
234 	/* Parse cmdline from U-Boot */
235 	argc = a0;
236 	argv = (char **)a1;
237 	boothowto |= boot_parse_args(argc, argv);
238 
239 	mips_init();
240 }
241