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