]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/atheros/ar71xx_machdep.c
Merge upstream patch to unbreak tunnel forwarding.
[FreeBSD/FreeBSD.git] / sys / mips / atheros / ar71xx_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Oleksandr Tymoshenko
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_ddb.h"
33 #include "opt_ar71xx.h"
34
35 #include <sys/param.h>
36 #include <sys/conf.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40 #include <sys/cons.h>
41 #include <sys/kdb.h>
42 #include <sys/reboot.h>
43
44 #include <vm/vm.h>
45 #include <vm/vm_page.h>
46
47 #include <net/ethernet.h>
48
49 #include <machine/clock.h>
50 #include <machine/cpu.h>
51 #include <machine/cpuregs.h>
52 #include <machine/hwfunc.h>
53 #include <machine/md_var.h>
54 #include <machine/trap.h>
55 #include <machine/vmparam.h>
56
57 #include <mips/atheros/ar71xxreg.h>
58
59 #include <mips/atheros/ar71xx_setup.h>
60 #include <mips/atheros/ar71xx_cpudef.h>
61 #include <mips/atheros/ar71xx_macaddr.h>
62
63 extern char edata[], end[];
64
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 int
121 ar71xx_redboot_get_macaddr(void)
122 {
123         char *var;
124         int count = 0, i;
125         uint32_t macaddr[ETHER_ADDR_LEN];
126         uint8_t tmpmac[ETHER_ADDR_LEN];
127
128         /*
129          * "ethaddr" is passed via envp on RedBoot platforms
130          * "kmac" is passed via argv on RouterBOOT platforms
131          */
132         if ((var = kern_getenv("ethaddr")) != NULL ||
133             (var = kern_getenv("kmac")) != NULL) {
134                 count = sscanf(var, "%x%*c%x%*c%x%*c%x%*c%x%*c%x",
135                     &macaddr[0], &macaddr[1],
136                     &macaddr[2], &macaddr[3],
137                     &macaddr[4], &macaddr[5]);
138
139                 if (count < 6) {
140                         memset(macaddr, 0,
141                             sizeof(macaddr));
142                 } else {
143                         for (i = 0; i < ETHER_ADDR_LEN; i++)
144                                 tmpmac[i] = macaddr[i] & 0xff;
145                         (void) ar71xx_mac_addr_init(ar71xx_board_mac_addr,
146                             tmpmac,
147                             0, /* offset */
148                             0); /* is_local */
149                 }
150                 freeenv(var);
151                 return (0);
152         }
153         return (-1);
154 }
155
156 #ifdef  AR71XX_ENV_ROUTERBOOT
157 /*
158  * RouterBoot gives us the board memory in a command line argument.
159  */
160 static int
161 ar71xx_routerboot_get_mem(int argc, char **argv)
162 {
163         int i, board_mem;
164
165         /*
166          * Protect ourselves from garbage in registers.
167          */
168         if (!MIPS_IS_VALID_PTR(argv))
169                 return (0);
170
171         for (i = 0; i < argc; i++) {
172                 if (argv[i] == NULL)
173                         continue;
174                 if (strncmp(argv[i], "mem=", 4) == 0) {
175                         if (sscanf(argv[i] + 4, "%dM", &board_mem) == 1)
176                                 return (btoc(board_mem * 1024 * 1024));
177                 }
178         }
179
180         return (0);
181 }
182 #endif
183
184 /*
185  * Handle initialising the MAC address from a specific EEPROM
186  * offset.
187  *
188  * This is done during (very) early boot.
189  *
190  * hint.ar71xx.0.eeprom_mac_addr=<address to read from>
191  * hint.ar71xx.0.eeprom_mac_isascii=<0|1>
192  */
193 static int
194 ar71xx_platform_read_eeprom_mac(void)
195 {
196         long eeprom_mac_addr = 0;
197         const char *mac;
198         int i, readascii = 0;
199         uint8_t macaddr[ETHER_ADDR_LEN];
200
201         if (resource_long_value("ar71xx", 0, "eeprom_mac_addr",
202             &eeprom_mac_addr) != 0)
203                 return (-1);
204
205         /* get a pointer to the EEPROM MAC address */
206
207         mac = (const char *) MIPS_PHYS_TO_KSEG1(eeprom_mac_addr);
208
209         /* Check if it's ASCII or not */
210         if (resource_int_value("ar71xx", 0, "eeprom_mac_isascii",
211             &readascii) == 0 && readascii == 1) {
212                 printf("ar71xx: Overriding MAC from EEPROM (ascii)\n");
213                 for (i = 0; i < 6; i++) {
214                         macaddr[i] = strtol(&(mac[i*3]), NULL, 16);
215                 }
216         } else {
217                 printf("ar71xx: Overriding MAC from EEPROM\n");
218                 for (i = 0; i < 6; i++) {
219                         macaddr[i] = mac[i];
220                 }
221         }
222
223         /* Set the default board MAC */
224         (void) ar71xx_mac_addr_init(ar71xx_board_mac_addr,
225             macaddr,
226             0, /* offset */
227             0); /* is_local */
228         printf("ar71xx: Board MAC: %6D\n", ar71xx_board_mac_addr, ":");
229         return (0);
230 }
231
232 /*
233  * Populate a kenv hint for the given device based on the given
234  * MAC address and offset.
235  *
236  * Returns 0 if ok, < 0 on error.
237  */
238 static int
239 ar71xx_platform_set_mac_hint(const char *dev, int unit,
240     const uint8_t *macaddr, int offset, int islocal)
241 {
242         char macstr[32];
243         uint8_t lclmac[ETHER_ADDR_LEN];
244         char devstr[32];
245
246         /* Initialise the MAC address, plus/minus the offset */
247         if (ar71xx_mac_addr_init(lclmac, macaddr, offset, islocal) != 0) {
248                 return (-1);
249         }
250
251         /* Turn it into a string */
252         snprintf(macstr, 32, "%6D", lclmac, ":");
253         snprintf(devstr, 32, "hint.%s.%d.macaddr", dev, unit);
254
255         printf("  %s => %s\n", devstr, macstr);
256
257         /* Call setenv */
258         if (kern_setenv(devstr, macstr) != 0) {
259                 printf("%s: failed to set hint (%s => %s)\n",
260                     __func__,
261                     devstr,
262                     macstr);
263                 return (-1);
264         }
265
266         return (0);
267 }
268
269 /*
270  * Iterate through the list of boot time hints that populate
271  * a device MAC address hint based on the "board" MAC address.
272  *
273  * ar71xx_mac_map.X.devid=<device id, eg ath>
274  * ar71xx_mac_map.X.unitid=<unit id, eg 0>
275  * ar71xx_mac_map.X.offset=<mac address value offset>
276  * ar71xx_mac_map.X.is_local=<1 or 0>
277  */
278 static int
279 ar71xx_platform_check_mac_hints(void)
280 {
281         int i;
282         const char *devid;
283         int offset, is_local, unitid;
284
285         for (i = 0; i < 8; i++) {
286                 if (resource_string_value("ar71xx_mac_map", i, "devid",
287                     &devid) != 0)
288                         break;
289                 if (resource_int_value("ar71xx_mac_map", i, "unitid",
290                     &unitid) != 0)
291                         break;
292                 if (resource_int_value("ar71xx_mac_map", i, "offset",
293                     &offset) != 0)
294                         break;
295                 if (resource_int_value("ar71xx_mac_map", i, "is_local",
296                     &is_local) != 0)
297                         break;
298                 printf("ar71xx: devid '%s.%d', MAC offset '%d'\n",
299                     devid, unitid, offset);
300                 (void) ar71xx_platform_set_mac_hint(devid, unitid,
301                     ar71xx_board_mac_addr, offset, is_local);
302         }
303
304         return (0);
305 }
306
307 extern char cpu_model[];
308
309 void
310 platform_start(__register_t a0 __unused, __register_t a1 __unused, 
311     __register_t a2 __unused, __register_t a3 __unused)
312 {
313         uint64_t platform_counter_freq;
314         int argc = 0, i;
315         char **argv = NULL, **envp = NULL;
316         vm_offset_t kernend;
317
318         /* 
319          * clear the BSS and SBSS segments, this should be first call in
320          * the function
321          */
322         kernend = (vm_offset_t)&end;
323         memset(&edata, 0, kernend - (vm_offset_t)(&edata));
324
325         mips_postboot_fixup();
326
327         /* Initialize pcpu stuff */
328         mips_pcpu0_init();
329
330         /*
331          * Until some more sensible abstractions for uboot/redboot
332          * environment handling, we have to make this a compile-time
333          * hack.  The existing code handles the uboot environment
334          * very incorrectly so we should just ignore initialising
335          * the relevant pointers.
336          */
337 #ifndef AR71XX_ENV_UBOOT
338         argc = a0;
339         argv = (char**)a1;
340         envp = (char**)a2;
341 #endif
342         /* 
343          * Protect ourselves from garbage in registers 
344          */
345         if (MIPS_IS_VALID_PTR(envp)) {
346                 for (i = 0; envp[i]; i += 2) {
347                         if (strcmp(envp[i], "memsize") == 0)
348                                 realmem = btoc(strtoul(envp[i+1], NULL, 16));
349                         else if (strcmp(envp[i], "bootverbose") == 0)
350                                 bootverbose = btoc(strtoul(envp[i+1], NULL, 10));
351                 }
352         }
353         bootverbose = 1;
354
355 #ifdef  AR71XX_ENV_ROUTERBOOT
356         /*
357          * RouterBoot informs the board memory as a command line argument.
358          */
359         if (realmem == 0)
360                 realmem = ar71xx_routerboot_get_mem(argc, argv);
361 #endif
362
363         /*
364          * Just wild guess. RedBoot let us down and didn't reported 
365          * memory size
366          */
367         if (realmem == 0)
368                 realmem = btoc(32*1024*1024);
369
370         /*
371          * Allow build-time override in case Redboot lies
372          * or in other situations (eg where there's u-boot)
373          * where there isn't (yet) a convienent method of
374          * being told how much RAM is available.
375          *
376          * This happens on at least the Ubiquiti LS-SR71A
377          * board, where redboot says there's 16mb of RAM
378          * but in fact there's 32mb.
379          */
380 #if     defined(AR71XX_REALMEM)
381                 realmem = btoc(AR71XX_REALMEM);
382 #endif
383
384         /* phys_avail regions are in bytes */
385         phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
386         phys_avail[1] = ctob(realmem);
387
388         dump_avail[0] = 0;
389         dump_avail[1] = phys_avail[1];
390
391         physmem = realmem;
392
393         /*
394          * ns8250 uart code uses DELAY so ticker should be inititalized 
395          * before cninit. And tick_init_params refers to hz, so * init_param1 
396          * should be called first.
397          */
398         init_param1();
399
400         /* Detect the system type - this is needed for subsequent chipset-specific calls */
401         ar71xx_detect_sys_type();
402         ar71xx_detect_sys_frequency();
403
404         platform_counter_freq = ar71xx_cpu_freq();
405         mips_timer_init_params(platform_counter_freq, 1);
406         cninit();
407         init_static_kenv(boot1_env, sizeof(boot1_env));
408
409         printf("CPU platform: %s\n", ar71xx_get_system_type());
410         printf("CPU Frequency=%d MHz\n", u_ar71xx_cpu_freq / 1000000);
411         printf("CPU DDR Frequency=%d MHz\n", u_ar71xx_ddr_freq / 1000000);
412         printf("CPU AHB Frequency=%d MHz\n", u_ar71xx_ahb_freq / 1000000);
413         printf("platform frequency: %lld MHz\n", platform_counter_freq / 1000000);
414         printf("CPU reference clock: %d MHz\n", u_ar71xx_refclk / 1000000);
415         printf("CPU MDIO clock: %d MHz\n", u_ar71xx_mdio_freq / 1000000);
416         printf("arguments: \n");
417         printf("  a0 = %08x\n", a0);
418         printf("  a1 = %08x\n", a1);
419         printf("  a2 = %08x\n", a2);
420         printf("  a3 = %08x\n", a3);
421
422         strcpy(cpu_model, ar71xx_get_system_type());
423
424         /*
425          * XXX this code is very redboot specific.
426          */
427         printf("Cmd line:");
428         if (MIPS_IS_VALID_PTR(argv)) {
429                 for (i = 0; i < argc; i++) {
430                         printf(" %s", argv[i]);
431                         parse_argv(argv[i]);
432                 }
433         }
434         else
435                 printf ("argv is invalid");
436         printf("\n");
437
438         printf("Environment:\n");
439         if (MIPS_IS_VALID_PTR(envp)) {
440                 for (i = 0; envp[i]; i+=2) {
441                         printf("  %s = %s\n", envp[i], envp[i+1]);
442                         kern_setenv(envp[i], envp[i+1]);
443                 }
444         }
445         else 
446                 printf ("envp is invalid\n");
447
448         /* Platform setup */
449         init_param2(physmem);
450         mips_cpu_init();
451         pmap_bootstrap();
452         mips_proc0_init();
453         mutex_init();
454
455         /*
456          * Reset USB devices 
457          */
458         ar71xx_init_usb_peripheral();
459
460         /*
461          * Reset internal ethernet switch, if one exists
462          */
463         ar71xx_reset_ethernet_switch();
464
465         /*
466          * Initialise the gmac driver.
467          */
468         ar71xx_init_gmac();
469
470         /* Redboot if_arge MAC address is in the environment */
471         (void) ar71xx_redboot_get_macaddr();
472
473         /* Various other boards need things to come out of EEPROM */
474         (void) ar71xx_platform_read_eeprom_mac();
475
476         /* Initialise the MAC address hint map */
477         ar71xx_platform_check_mac_hints();
478
479         kdb_init();
480 #ifdef KDB
481         if (boothowto & RB_KDB)
482                 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
483 #endif
484 }