]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/atheros/ar71xx_machdep.c
Merge llvm 3.5.0 release from ^/vendor/llvm/dist, resolve conflicts, and
[FreeBSD/FreeBSD.git] / sys / mips / atheros / ar71xx_machdep.c
1 /*-
2  * Copyright (c) 2009 Oleksandr Tymoshenko
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_ddb.h"
31 #include "opt_ar71xx.h"
32
33 #include <sys/param.h>
34 #include <sys/conf.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/cons.h>
39 #include <sys/kdb.h>
40 #include <sys/reboot.h>
41
42 #include <vm/vm.h>
43 #include <vm/vm_page.h>
44
45 #include <net/ethernet.h>
46
47 #include <machine/clock.h>
48 #include <machine/cpu.h>
49 #include <machine/cpuregs.h>
50 #include <machine/hwfunc.h>
51 #include <machine/md_var.h>
52 #include <machine/trap.h>
53 #include <machine/vmparam.h>
54
55 #include <mips/atheros/ar71xxreg.h>
56
57 #include <mips/atheros/ar71xx_setup.h>
58 #include <mips/atheros/ar71xx_cpudef.h>
59
60 #include <mips/sentry5/s5reg.h>
61
62 extern char edata[], end[];
63
64 uint32_t ar711_base_mac[ETHER_ADDR_LEN];
65 /* 4KB static data aread to keep a copy of the bootload env until
66    the dynamic kenv is setup */
67 char boot1_env[4096];
68
69 /*
70  * We get a string in from Redboot with the all the arguments together,
71  * "foo=bar bar=baz". Split them up and save in kenv.
72  */
73 static void
74 parse_argv(char *str)
75 {
76         char *n, *v;
77
78         while ((v = strsep(&str, " ")) != NULL) {
79                 if (*v == '\0')
80                         continue;
81                 if (*v == '-') {
82                         while (*v != '\0') {
83                                 v++;
84                                 switch (*v) {
85                                 case 'a': boothowto |= RB_ASKNAME; break;
86                                 case 'd': boothowto |= RB_KDB; break;
87                                 case 'g': boothowto |= RB_GDB; break;
88                                 case 's': boothowto |= RB_SINGLE; break;
89                                 case 'v': boothowto |= RB_VERBOSE; break;
90                                 }
91                         }
92                 } else {
93                         n = strsep(&v, "=");
94                         if (v == NULL)
95                                 kern_setenv(n, "1");
96                         else
97                                 kern_setenv(n, v);
98                 }
99         }
100 }
101
102 void
103 platform_cpu_init()
104 {
105         /* Nothing special */
106 }
107
108 void
109 platform_reset(void)
110 {
111         ar71xx_device_stop(RST_RESET_FULL_CHIP);
112         /* Wait for reset */
113         while(1)
114                 ;
115 }
116
117 /*
118  * Obtain the MAC address via the Redboot environment.
119  */
120 static void
121 ar71xx_redboot_get_macaddr(void)
122 {
123         char *var;
124         int count = 0;
125
126         /*
127          * "ethaddr" is passed via envp on RedBoot platforms
128          * "kmac" is passed via argv on RouterBOOT platforms
129          */
130         if ((var = kern_getenv("ethaddr")) != NULL ||
131             (var = kern_getenv("kmac")) != NULL) {
132                 count = sscanf(var, "%x%*c%x%*c%x%*c%x%*c%x%*c%x",
133                     &ar711_base_mac[0], &ar711_base_mac[1],
134                     &ar711_base_mac[2], &ar711_base_mac[3],
135                     &ar711_base_mac[4], &ar711_base_mac[5]);
136                 if (count < 6)
137                         memset(ar711_base_mac, 0,
138                             sizeof(ar711_base_mac));
139                 freeenv(var);
140         }
141 }
142
143 #ifdef  AR71XX_ENV_ROUTERBOOT
144 /*
145  * RouterBoot gives us the board memory in a command line argument.
146  */
147 static int
148 ar71xx_routerboot_get_mem(int argc, char **argv)
149 {
150         int i, board_mem;
151
152         /*
153          * Protect ourselves from garbage in registers.
154          */
155         if (!MIPS_IS_VALID_PTR(argv))
156                 return (0);
157
158         for (i = 0; i < argc; i++) {
159                 if (argv[i] == NULL)
160                         continue;
161                 if (strncmp(argv[i], "mem=", 4) == 0) {
162                         if (sscanf(argv[i] + 4, "%dM", &board_mem) == 1)
163                                 return (btoc(board_mem * 1024 * 1024));
164                 }
165         }
166
167         return (0);
168 }
169 #endif
170
171 void
172 platform_start(__register_t a0 __unused, __register_t a1 __unused, 
173     __register_t a2 __unused, __register_t a3 __unused)
174 {
175         uint64_t platform_counter_freq;
176         int argc = 0, i;
177         char **argv = NULL, **envp = NULL;
178         vm_offset_t kernend;
179
180         /* 
181          * clear the BSS and SBSS segments, this should be first call in
182          * the function
183          */
184         kernend = (vm_offset_t)&end;
185         memset(&edata, 0, kernend - (vm_offset_t)(&edata));
186
187         mips_postboot_fixup();
188
189         /* Initialize pcpu stuff */
190         mips_pcpu0_init();
191
192         /*
193          * Until some more sensible abstractions for uboot/redboot
194          * environment handling, we have to make this a compile-time
195          * hack.  The existing code handles the uboot environment
196          * very incorrectly so we should just ignore initialising
197          * the relevant pointers.
198          */
199 #ifndef AR71XX_ENV_UBOOT
200         argc = a0;
201         argv = (char**)a1;
202         envp = (char**)a2;
203 #endif
204         /* 
205          * Protect ourselves from garbage in registers 
206          */
207         if (MIPS_IS_VALID_PTR(envp)) {
208                 for (i = 0; envp[i]; i += 2) {
209                         if (strcmp(envp[i], "memsize") == 0)
210                                 realmem = btoc(strtoul(envp[i+1], NULL, 16));
211                 }
212         }
213
214 #ifdef  AR71XX_ENV_ROUTERBOOT
215         /*
216          * RouterBoot informs the board memory as a command line argument.
217          */
218         if (realmem == 0)
219                 realmem = ar71xx_routerboot_get_mem(argc, argv);
220 #endif
221
222         /*
223          * Just wild guess. RedBoot let us down and didn't reported 
224          * memory size
225          */
226         if (realmem == 0)
227                 realmem = btoc(32*1024*1024);
228
229         /*
230          * Allow build-time override in case Redboot lies
231          * or in other situations (eg where there's u-boot)
232          * where there isn't (yet) a convienent method of
233          * being told how much RAM is available.
234          *
235          * This happens on at least the Ubiquiti LS-SR71A
236          * board, where redboot says there's 16mb of RAM
237          * but in fact there's 32mb.
238          */
239 #if     defined(AR71XX_REALMEM)
240                 realmem = btoc(AR71XX_REALMEM);
241 #endif
242
243         /* phys_avail regions are in bytes */
244         phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
245         phys_avail[1] = ctob(realmem);
246
247         dump_avail[0] = phys_avail[0];
248         dump_avail[1] = phys_avail[1] - phys_avail[0];
249
250         physmem = realmem;
251
252         /*
253          * ns8250 uart code uses DELAY so ticker should be inititalized 
254          * before cninit. And tick_init_params refers to hz, so * init_param1 
255          * should be called first.
256          */
257         init_param1();
258
259         /* Detect the system type - this is needed for subsequent chipset-specific calls */
260         ar71xx_detect_sys_type();
261         ar71xx_detect_sys_frequency();
262
263         platform_counter_freq = ar71xx_cpu_freq();
264         mips_timer_init_params(platform_counter_freq, 1);
265         cninit();
266         init_static_kenv(boot1_env, sizeof(boot1_env));
267
268         printf("CPU platform: %s\n", ar71xx_get_system_type());
269         printf("CPU Frequency=%d MHz\n", u_ar71xx_cpu_freq / 1000000);
270         printf("CPU DDR Frequency=%d MHz\n", u_ar71xx_ddr_freq / 1000000);
271         printf("CPU AHB Frequency=%d MHz\n", u_ar71xx_ahb_freq / 1000000);
272         printf("platform frequency: %lld MHz\n", platform_counter_freq / 1000000);
273         printf("CPU reference clock: %d MHz\n", u_ar71xx_refclk / 1000000);
274         printf("CPU MDIO clock: %d MHz\n", u_ar71xx_mdio_freq / 1000000);
275         printf("arguments: \n");
276         printf("  a0 = %08x\n", a0);
277         printf("  a1 = %08x\n", a1);
278         printf("  a2 = %08x\n", a2);
279         printf("  a3 = %08x\n", a3);
280
281         /*
282          * XXX this code is very redboot specific.
283          */
284         printf("Cmd line:");
285         if (MIPS_IS_VALID_PTR(argv)) {
286                 for (i = 0; i < argc; i++) {
287                         printf(" %s", argv[i]);
288                         parse_argv(argv[i]);
289                 }
290         }
291         else
292                 printf ("argv is invalid");
293         printf("\n");
294
295         printf("Environment:\n");
296         if (MIPS_IS_VALID_PTR(envp)) {
297                 for (i = 0; envp[i]; i+=2) {
298                         printf("  %s = %s\n", envp[i], envp[i+1]);
299                         kern_setenv(envp[i], envp[i+1]);
300                 }
301         }
302         else 
303                 printf ("envp is invalid\n");
304
305         /* Redboot if_arge MAC address is in the environment */
306         ar71xx_redboot_get_macaddr();
307
308         init_param2(physmem);
309         mips_cpu_init();
310         pmap_bootstrap();
311         mips_proc0_init();
312         mutex_init();
313
314         /*
315          * Reset USB devices 
316          */
317         ar71xx_init_usb_peripheral();
318
319         /*
320          * Reset internal ethernet switch, if one exists
321          */
322         ar71xx_reset_ethernet_switch();
323
324         /*
325          * Initialise the gmac driver.
326          */
327         ar71xx_init_gmac();
328
329         kdb_init();
330 #ifdef KDB
331         if (boothowto & RB_KDB)
332                 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
333 #endif
334 }