]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/atheros/ar531x/ar5315_machdep.c
MFV r353623: 10473 zfs(1M) missing cross-reference to zfs-program(1M)
[FreeBSD/FreeBSD.git] / sys / mips / atheros / ar531x / ar5315_machdep.c
1 /*-
2  * Copyright (c) 2016, Hiroki Mori
3  * Copyright (c) 2009 Oleksandr Tymoshenko
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_ar531x.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/cons.h>
40 #include <sys/kdb.h>
41 #include <sys/reboot.h>
42 #include <sys/boot.h>
43
44 #include <vm/vm.h>
45 #include <vm/vm_param.h>
46 #include <vm/vm_page.h>
47 #include <vm/vm_phys.h>
48
49 #include <net/ethernet.h>
50
51 #include <machine/clock.h>
52 #include <machine/cpu.h>
53 #include <machine/cpuregs.h>
54 #include <machine/hwfunc.h>
55 #include <machine/md_var.h>
56 #include <machine/trap.h>
57
58 #include <mips/atheros/ar531x/ar5315reg.h>
59
60 #include <mips/atheros/ar531x/ar5315_setup.h>
61 #include <mips/atheros/ar531x/ar5315_cpudef.h>
62
63 extern char edata[], end[];
64
65 uint32_t ar711_base_mac[ETHER_ADDR_LEN];
66 /* 4KB static data aread to keep a copy of the bootload env until
67    the dynamic kenv is setup */
68 char boot1_env[4096];
69
70 void
71 platform_cpu_init()
72 {
73         /* Nothing special */
74 }
75
76 void
77 platform_reset(void)
78 {
79         ar531x_device_reset();
80         /* Wait for reset */
81         while(1)
82                 ;
83 }
84
85 /*
86  * Obtain the MAC address via the Redboot environment.
87  */
88 static void
89 ar5315_redboot_get_macaddr(void)
90 {
91         char *var;
92         int count = 0;
93
94         /*
95          * "ethaddr" is passed via envp on RedBoot platforms
96          * "kmac" is passed via argv on RouterBOOT platforms
97          */
98         if ((var = kern_getenv("ethaddr")) != NULL ||
99             (var = kern_getenv("kmac")) != NULL) {
100                 count = sscanf(var, "%x%*c%x%*c%x%*c%x%*c%x%*c%x",
101                     &ar711_base_mac[0], &ar711_base_mac[1],
102                     &ar711_base_mac[2], &ar711_base_mac[3],
103                     &ar711_base_mac[4], &ar711_base_mac[5]);
104                 if (count < 6)
105                         memset(ar711_base_mac, 0,
106                             sizeof(ar711_base_mac));
107                 freeenv(var);
108         }
109 }
110
111 #if defined(SOC_VENDOR) || defined(SOC_MODEL) || defined(SOC_REV)
112 static SYSCTL_NODE(_hw, OID_AUTO, soc, CTLFLAG_RD, 0,
113     "System on Chip information");
114 #endif
115 #if defined(SOC_VENDOR)
116 static char hw_soc_vendor[] = SOC_VENDOR;
117 SYSCTL_STRING(_hw_soc, OID_AUTO, vendor, CTLFLAG_RD, hw_soc_vendor, 0,
118            "SoC vendor");
119 #endif
120 #if defined(SOC_MODEL)
121 static char hw_soc_model[] = SOC_MODEL;
122 SYSCTL_STRING(_hw_soc, OID_AUTO, model, CTLFLAG_RD, hw_soc_model, 0,
123            "SoC model");
124 #endif
125 #if defined(SOC_REV)
126 static char hw_soc_revision[] = SOC_REV;
127 SYSCTL_STRING(_hw_soc, OID_AUTO, revision, CTLFLAG_RD, hw_soc_revision, 0,
128            "SoC revision");
129 #endif
130
131 #if defined(DEVICE_VENDOR) || defined(DEVICE_MODEL) || defined(DEVICE_REV)
132 static SYSCTL_NODE(_hw, OID_AUTO, device, CTLFLAG_RD, 0, "Board information");
133 #endif
134 #if defined(DEVICE_VENDOR)
135 static char hw_device_vendor[] = DEVICE_VENDOR;
136 SYSCTL_STRING(_hw_device, OID_AUTO, vendor, CTLFLAG_RD, hw_device_vendor, 0,
137            "Board vendor");
138 #endif
139 #if defined(DEVICE_MODEL)
140 static char hw_device_model[] = DEVICE_MODEL;
141 SYSCTL_STRING(_hw_device, OID_AUTO, model, CTLFLAG_RD, hw_device_model, 0,
142            "Board model");
143 #endif
144 #if defined(DEVICE_REV)
145 static char hw_device_revision[] = DEVICE_REV;
146 SYSCTL_STRING(_hw_device, OID_AUTO, revision, CTLFLAG_RD, hw_device_revision, 0,
147            "Board revision");
148 #endif
149
150 extern char cpu_model[];
151
152 void
153 platform_start(__register_t a0 __unused, __register_t a1 __unused, 
154     __register_t a2 __unused, __register_t a3 __unused)
155 {
156         uint64_t platform_counter_freq;
157         int argc = 0, i;
158         char **argv = NULL;
159 #ifndef AR531X_ENV_UBOOT
160         char **envp = NULL;
161 #endif
162         vm_offset_t kernend;
163
164         /* 
165          * clear the BSS and SBSS segments, this should be first call in
166          * the function
167          */
168         kernend = (vm_offset_t)&end;
169         memset(&edata, 0, kernend - (vm_offset_t)(&edata));
170
171         mips_postboot_fixup();
172
173         /* Initialize pcpu stuff */
174         mips_pcpu0_init();
175
176         /*
177          * Until some more sensible abstractions for uboot/redboot
178          * environment handling, we have to make this a compile-time
179          * hack.  The existing code handles the uboot environment
180          * very incorrectly so we should just ignore initialising
181          * the relevant pointers.
182          */
183 #ifndef AR531X_ENV_UBOOT
184         argc = a0;
185         argv = (char**)a1;
186         envp = (char**)a2;
187 #endif
188         /* 
189          * Protect ourselves from garbage in registers 
190          */
191         if (MIPS_IS_VALID_PTR(envp)) {
192                 for (i = 0; envp[i]; i += 2) {
193                         if (strcmp(envp[i], "memsize") == 0)
194                                 realmem = btoc(strtoul(envp[i+1], NULL, 16));
195                 }
196         }
197
198         ar5315_detect_sys_type();
199
200 // RedBoot SDRAM Detect is missing
201 //      ar531x_detect_mem_size();
202
203         /*
204          * Just wild guess. RedBoot let us down and didn't reported 
205          * memory size
206          */
207         if (realmem == 0)
208                 realmem = btoc(16*1024*1024);
209
210         /*
211          * Allow build-time override in case Redboot lies
212          * or in other situations (eg where there's u-boot)
213          * where there isn't (yet) a convienent method of
214          * being told how much RAM is available.
215          *
216          * This happens on at least the Ubiquiti LS-SR71A
217          * board, where redboot says there's 16mb of RAM
218          * but in fact there's 32mb.
219          */
220 #if     defined(AR531X_REALMEM)
221                 realmem = btoc(AR531X_REALMEM);
222 #endif
223
224         /* phys_avail regions are in bytes */
225         phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
226         phys_avail[1] = ctob(realmem);
227
228         dump_avail[0] = phys_avail[0];
229         dump_avail[1] = phys_avail[1] - phys_avail[0];
230
231         physmem = realmem;
232
233         /*
234          * ns8250 uart code uses DELAY so ticker should be inititalized 
235          * before cninit. And tick_init_params refers to hz, so * init_param1 
236          * should be called first.
237          */
238         init_param1();
239         boothowto |= (RB_SERIAL | RB_MULTIPLE); /* Use multiple consoles */
240 //      boothowto |= RB_VERBOSE;
241 //      boothowto |= (RB_SINGLE);
242
243         /* Detect the system type - this is needed for subsequent chipset-specific calls */
244
245
246         ar531x_device_soc_init();
247         ar531x_detect_sys_frequency();
248
249         platform_counter_freq = ar531x_cpu_freq();
250         mips_timer_init_params(platform_counter_freq, 1);
251         cninit();
252         init_static_kenv(boot1_env, sizeof(boot1_env));
253
254         printf("CPU platform: %s\n", ar5315_get_system_type());
255         printf("CPU Frequency=%d MHz\n", ar531x_cpu_freq() / 1000000);
256         printf("CPU DDR Frequency=%d MHz\n", ar531x_ddr_freq() / 1000000);
257         printf("CPU AHB Frequency=%d MHz\n", ar531x_ahb_freq() / 1000000); 
258
259         printf("platform frequency: %lld\n", platform_counter_freq);
260         printf("arguments: \n");
261         printf("  a0 = %08x\n", a0);
262         printf("  a1 = %08x\n", a1);
263         printf("  a2 = %08x\n", a2);
264         printf("  a3 = %08x\n", a3);
265
266         strcpy(cpu_model, ar5315_get_system_type());
267
268         /*
269          * XXX this code is very redboot specific.
270          */
271         printf("Cmd line:");
272         if (MIPS_IS_VALID_PTR(argv)) {
273                 for (i = 0; i < argc; i++) {
274                         printf(" %s", argv[i]);
275                         boothowto |= boot_parse_arg(argv[i]);
276                 }
277         }
278         else
279                 printf ("argv is invalid");
280         printf("\n");
281
282         printf("Environment:\n");
283 #if 0
284         if (MIPS_IS_VALID_PTR(envp)) {
285                 if (envp[0] && strchr(envp[0], '=') ) {
286                         char *env_val; //
287                         for (i = 0; envp[i]; i++) {
288                                 env_val = strchr(envp[i], '=');
289                                 /* Not sure if we correct to change data, but env in RAM */
290                                 *(env_val++) = '\0';
291                                 printf("=  %s = %s\n", envp[i], env_val);
292                                 kern_setenv(envp[i], env_val);
293                         }
294                 } else {
295                         for (i = 0; envp[i]; i+=2) {
296                                 printf("  %s = %s\n", envp[i], envp[i+1]);
297                                 kern_setenv(envp[i], envp[i+1]);
298                         }
299                 }
300         }
301         else 
302                 printf ("envp is invalid\n");
303 #else
304         printf ("envp skiped\n");
305 #endif
306
307         /* Redboot if_are MAC address is in the environment */
308         ar5315_redboot_get_macaddr();
309
310         init_param2(physmem);
311         mips_cpu_init();
312         pmap_bootstrap();
313         mips_proc0_init();
314         mutex_init();
315
316         ar531x_device_start();
317
318         kdb_init();
319 #ifdef KDB
320         if (boothowto & RB_KDB)
321                 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
322 #endif
323
324 }