2 * Copyright (c) 2009 Oleksandr Tymoshenko
3 * Copyright (c) 2015 Alexander Kabaev
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include "opt_platform.h"
34 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
41 #include <sys/reboot.h>
44 #include <dev/fdt/fdt_common.h>
45 #include <dev/ofw/openfirm.h>
49 #include <vm/vm_page.h>
51 #include <net/ethernet.h>
53 #include <machine/clock.h>
54 #include <machine/cpu.h>
55 #include <machine/cpufunc.h>
56 #include <machine/hwfunc.h>
57 #include <machine/md_var.h>
58 #include <machine/trap.h>
59 #include <machine/vmparam.h>
61 #include <mips/ingenic/jz4780_regs.h>
62 #include <mips/ingenic/jz4780_cpuregs.h>
64 uint32_t * const led = (uint32_t *)0xb0010548;
66 extern char edata[], end[];
67 static char boot1_env[4096];
70 platform_cpu_init(void)
75 * Do not expect mbox interrups while writing
78 reg = mips_rd_xburst_reim();
79 reg &= ~JZ_REIM_MIRQ0M;
80 mips_wr_xburst_reim(reg);
83 mips_wr_xburst_mbox0(0);
84 mips_wr_xburst_mbox1(0);
85 mips_wr_xburst_core_sts(~JZ_CORESTS_MIRQ0P);
87 /* Unmask mbox interrupts */
88 reg |= JZ_REIM_MIRQ0M;
89 mips_wr_xburst_reim(reg);
96 * For now, provoke a watchdog reset in about a second, so UART buffers
97 * have a fighting chance to flush before we pull the plug
99 writereg(JZ_TCU_BASE + JZ_WDOG_TCER, 0); /* disable watchdog */
100 writereg(JZ_TCU_BASE + JZ_WDOG_TCNT, 0); /* reset counter */
101 writereg(JZ_TCU_BASE + JZ_WDOG_TDR, 128); /* wait for ~1s */
102 writereg(JZ_TCU_BASE + JZ_WDOG_TCSR, TCSR_RTC_EN | TCSR_DIV_256);
103 writereg(JZ_TCU_BASE + JZ_WDOG_TCER, TCER_ENABLE); /* fire! */
115 struct mem_region mr[FDT_MEM_REGIONS];
121 for (i = 0; i < 10; i++) {
125 /* The minimal amount of memory Ingenic SoC can have. */
126 dump_avail[0] = phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
127 physmem = realmem = btoc(32 * 1024 * 1024);
130 * X1000 mips cpu special.
131 * TODO: do anyone know what is this ?
134 "li $2, 0xa9000000 \n\t"
135 "mtc0 $2, $5, 4 \n\t"
140 if (fdt_get_mem_regions(mr, &mr_cnt, &val) == 0) {
142 physmem = realmem = btoc(val);
144 KASSERT((phys_avail[0] >= mr[0].mr_start) && \
145 (phys_avail[0] < (mr[0].mr_start + mr[0].mr_size)),
146 ("First region is not within FDT memory range"));
148 /* Limit size of the first region */
149 phys_avail[1] = (mr[0].mr_start + MIN(mr[0].mr_size, ctob(realmem)));
150 dump_avail[1] = phys_avail[1];
152 /* Add the rest of regions */
153 for (i = 1, j = 2; i < mr_cnt; i++, j+=2) {
154 phys_avail[j] = mr[i].mr_start;
155 phys_avail[j+1] = (mr[i].mr_start + mr[i].mr_size);
156 dump_avail[j] = phys_avail[j];
157 dump_avail[j+1] = phys_avail[j+1];
163 init_param2(physmem);
171 if (boothowto & RB_KDB)
172 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
177 _parse_bootarg(char *v)
185 case 'a': boothowto |= RB_ASKNAME; break;
186 /* Someone should simulate that ;-) */
187 case 'C': boothowto |= RB_CDROM; break;
188 case 'd': boothowto |= RB_KDB; break;
189 case 'D': boothowto |= RB_MULTIPLE; break;
190 case 'm': boothowto |= RB_MUTE; break;
191 case 'g': boothowto |= RB_GDB; break;
192 case 'h': boothowto |= RB_SERIAL; break;
193 case 'p': boothowto |= RB_PAUSE; break;
194 case 'r': boothowto |= RB_DFLTROOT; break;
195 case 's': boothowto |= RB_SINGLE; break;
196 case 'v': boothowto |= RB_VERBOSE; break;
209 _parse_cmdline(int argc, char *argv[])
213 for (i = 1; i < argc; i++)
214 _parse_bootarg(argv[i]);
218 /* Parse cmd line args as env - copied from xlp_machdep. */
219 /* XXX-BZ this should really be centrally provided for all (boot) code. */
221 _parse_bootargs(char *cmdline)
225 while ((v = strsep(&cmdline, " \n")) != NULL) {
234 platform_start(__register_t a0, __register_t a1,
235 __register_t a2 __unused, __register_t a3 __unused)
243 char buf[2048]; /* early stack supposedly big enough */
246 * clear the BSS and SBSS segments, this should be first call in
249 kernend = (vm_offset_t)&end;
250 memset(&edata, 0, kernend - (vm_offset_t)(&edata));
252 mips_postboot_fixup();
254 /* Initialize pcpu stuff */
257 /* Something to hold kernel env until kmem is available */
258 init_static_kenv(boot1_env, sizeof(boot1_env));
261 * Find the dtb passed in by the boot loader (currently fictional).
263 dtbp = (vm_offset_t)NULL;
265 #if defined(FDT_DTB_STATIC)
267 * In case the device tree blob was not retrieved (from metadata) try
268 * to use the statically embedded one.
270 if (dtbp == (vm_offset_t)NULL)
271 dtbp = (vm_offset_t)&fdt_static_dtb;
273 #error "Non-static FDT not supported on JZ4780"
275 if (OF_install(OFW_FDT, 0) == FALSE)
277 if (OF_init((void *)dtbp) != 0)
284 * Get bootargs from FDT if specified.
286 chosen = OF_finddevice("/chosen");
287 if (OF_getprop(chosen, "bootargs", buf, sizeof(buf)) != -1)
288 _parse_bootargs(buf);
290 /* Parse cmdline from U-Boot */
293 _parse_cmdline(argc, argv);