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: releng/11.2/sys/mips/atheros/ar71xx_machdep.c 331722 2018-03-29 02:50:57Z eadler $");
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 #include <mips/atheros/ar71xx_macaddr.h>
60
61 extern char edata[], end[];
62
63 /* 4KB static data aread to keep a copy of the bootload env until
64 the dynamic kenv is setup */
65 char boot1_env[4096];
66
67 /*
68 * We get a string in from Redboot with the all the arguments together,
69 * "foo=bar bar=baz". Split them up and save in kenv.
70 */
71 static void
72 parse_argv(char *str)
73 {
74 char *n, *v;
75
76 while ((v = strsep(&str, " ")) != NULL) {
77 if (*v == '\0')
78 continue;
79 if (*v == '-') {
80 while (*v != '\0') {
81 v++;
82 switch (*v) {
83 case 'a': boothowto |= RB_ASKNAME; break;
84 case 'd': boothowto |= RB_KDB; break;
85 case 'g': boothowto |= RB_GDB; break;
86 case 's': boothowto |= RB_SINGLE; break;
87 case 'v': boothowto |= RB_VERBOSE; break;
88 }
89 }
90 } else {
91 n = strsep(&v, "=");
92 if (v == NULL)
93 kern_setenv(n, "1");
94 else
95 kern_setenv(n, v);
96 }
97 }
98 }
99
100 void
101 platform_cpu_init()
102 {
103 /* Nothing special */
104 }
105
106 void
107 platform_reset(void)
108 {
109 ar71xx_device_stop(RST_RESET_FULL_CHIP);
110 /* Wait for reset */
111 while(1)
112 ;
113 }
114
115 /*
116 * Obtain the MAC address via the Redboot environment.
117 */
118 static int
119 ar71xx_redboot_get_macaddr(void)
120 {
121 char *var;
122 int count = 0, i;
123 uint32_t macaddr[ETHER_ADDR_LEN];
124 uint8_t tmpmac[ETHER_ADDR_LEN];
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 &macaddr[0], &macaddr[1],
134 &macaddr[2], &macaddr[3],
135 &macaddr[4], &macaddr[5]);
136
137 if (count < 6) {
138 memset(macaddr, 0,
139 sizeof(macaddr));
140 } else {
141 for (i = 0; i < ETHER_ADDR_LEN; i++)
142 tmpmac[i] = macaddr[i] & 0xff;
143 (void) ar71xx_mac_addr_init(ar71xx_board_mac_addr,
144 tmpmac,
145 0, /* offset */
146 0); /* is_local */
147 }
148 freeenv(var);
149 return (0);
150 }
151 return (-1);
152 }
153
154 #ifdef AR71XX_ENV_ROUTERBOOT
155 /*
156 * RouterBoot gives us the board memory in a command line argument.
157 */
158 static int
159 ar71xx_routerboot_get_mem(int argc, char **argv)
160 {
161 int i, board_mem;
162
163 /*
164 * Protect ourselves from garbage in registers.
165 */
166 if (!MIPS_IS_VALID_PTR(argv))
167 return (0);
168
169 for (i = 0; i < argc; i++) {
170 if (argv[i] == NULL)
171 continue;
172 if (strncmp(argv[i], "mem=", 4) == 0) {
173 if (sscanf(argv[i] + 4, "%dM", &board_mem) == 1)
174 return (btoc(board_mem * 1024 * 1024));
175 }
176 }
177
178 return (0);
179 }
180 #endif
181
182 /*
183 * Handle initialising the MAC address from a specific EEPROM
184 * offset.
185 *
186 * This is done during (very) early boot.
187 *
188 * hint.ar71xx.0.eeprom_mac_addr=<address to read from>
189 * hint.ar71xx.0.eeprom_mac_isascii=<0|1>
190 */
191 static int
192 ar71xx_platform_read_eeprom_mac(void)
193 {
194 long eeprom_mac_addr = 0;
195 const char *mac;
196 int i, readascii = 0;
197 uint8_t macaddr[ETHER_ADDR_LEN];
198
199 if (resource_long_value("ar71xx", 0, "eeprom_mac_addr",
200 &eeprom_mac_addr) != 0)
201 return (-1);
202
203 /* get a pointer to the EEPROM MAC address */
204
205 mac = (const char *) MIPS_PHYS_TO_KSEG1(eeprom_mac_addr);
206
207 /* Check if it's ASCII or not */
208 if (resource_int_value("ar71xx", 0, "eeprom_mac_isascii",
209 &readascii) == 0 && readascii == 1) {
210 printf("ar71xx: Overriding MAC from EEPROM (ascii)\n");
211 for (i = 0; i < 6; i++) {
212 macaddr[i] = strtol(&(mac[i*3]), NULL, 16);
213 }
214 } else {
215 printf("ar71xx: Overriding MAC from EEPROM\n");
216 for (i = 0; i < 6; i++) {
217 macaddr[i] = mac[i];
218 }
219 }
220
221 /* Set the default board MAC */
222 (void) ar71xx_mac_addr_init(ar71xx_board_mac_addr,
223 macaddr,
224 0, /* offset */
225 0); /* is_local */
226 printf("ar71xx: Board MAC: %6D\n", ar71xx_board_mac_addr, ":");
227 return (0);
228 }
229
230 /*
231 * Populate a kenv hint for the given device based on the given
232 * MAC address and offset.
233 *
234 * Returns 0 if ok, < 0 on error.
235 */
236 static int
237 ar71xx_platform_set_mac_hint(const char *dev, int unit,
238 const uint8_t *macaddr, int offset, int islocal)
239 {
240 char macstr[32];
241 uint8_t lclmac[ETHER_ADDR_LEN];
242 char devstr[32];
243
244 /* Initialise the MAC address, plus/minus the offset */
245 if (ar71xx_mac_addr_init(lclmac, macaddr, offset, islocal) != 0) {
246 return (-1);
247 }
248
249 /* Turn it into a string */
250 snprintf(macstr, 32, "%6D", lclmac, ":");
251 snprintf(devstr, 32, "hint.%s.%d.macaddr", dev, unit);
252
253 printf(" %s => %s\n", devstr, macstr);
254
255 /* Call setenv */
256 if (kern_setenv(devstr, macstr) != 0) {
257 printf("%s: failed to set hint (%s => %s)\n",
258 __func__,
259 devstr,
260 macstr);
261 return (-1);
262 }
263
264 return (0);
265 }
266
267 /*
268 * Iterate through the list of boot time hints that populate
269 * a device MAC address hint based on the "board" MAC address.
270 *
271 * ar71xx_mac_map.X.devid=<device id, eg ath>
272 * ar71xx_mac_map.X.unitid=<unit id, eg 0>
273 * ar71xx_mac_map.X.offset=<mac address value offset>
274 * ar71xx_mac_map.X.is_local=<1 or 0>
275 */
276 static int
277 ar71xx_platform_check_mac_hints(void)
278 {
279 int i;
280 const char *devid;
281 int offset, is_local, unitid;
282
283 for (i = 0; i < 8; i++) {
284 if (resource_string_value("ar71xx_mac_map", i, "devid",
285 &devid) != 0)
286 break;
287 if (resource_int_value("ar71xx_mac_map", i, "unitid",
288 &unitid) != 0)
289 break;
290 if (resource_int_value("ar71xx_mac_map", i, "offset",
291 &offset) != 0)
292 break;
293 if (resource_int_value("ar71xx_mac_map", i, "is_local",
294 &is_local) != 0)
295 break;
296 printf("ar71xx: devid '%s.%d', MAC offset '%d'\n",
297 devid, unitid, offset);
298 (void) ar71xx_platform_set_mac_hint(devid, unitid,
299 ar71xx_board_mac_addr, offset, is_local);
300 }
301
302 return (0);
303 }
304
305 extern char cpu_model[];
306
307 void
308 platform_start(__register_t a0 __unused, __register_t a1 __unused,
309 __register_t a2 __unused, __register_t a3 __unused)
310 {
311 uint64_t platform_counter_freq;
312 int argc = 0, i;
313 char **argv = NULL, **envp = NULL;
314 vm_offset_t kernend;
315
316 /*
317 * clear the BSS and SBSS segments, this should be first call in
318 * the function
319 */
320 kernend = (vm_offset_t)&end;
321 memset(&edata, 0, kernend - (vm_offset_t)(&edata));
322
323 mips_postboot_fixup();
324
325 /* Initialize pcpu stuff */
326 mips_pcpu0_init();
327
328 /*
329 * Until some more sensible abstractions for uboot/redboot
330 * environment handling, we have to make this a compile-time
331 * hack. The existing code handles the uboot environment
332 * very incorrectly so we should just ignore initialising
333 * the relevant pointers.
334 */
335 #ifndef AR71XX_ENV_UBOOT
336 argc = a0;
337 argv = (char**)a1;
338 envp = (char**)a2;
339 #endif
340 /*
341 * Protect ourselves from garbage in registers
342 */
343 if (MIPS_IS_VALID_PTR(envp)) {
344 for (i = 0; envp[i]; i += 2) {
345 if (strcmp(envp[i], "memsize") == 0)
346 realmem = btoc(strtoul(envp[i+1], NULL, 16));
347 else if (strcmp(envp[i], "bootverbose") == 0)
348 bootverbose = btoc(strtoul(envp[i+1], NULL, 10));
349 }
350 }
351 bootverbose = 1;
352
353 #ifdef AR71XX_ENV_ROUTERBOOT
354 /*
355 * RouterBoot informs the board memory as a command line argument.
356 */
357 if (realmem == 0)
358 realmem = ar71xx_routerboot_get_mem(argc, argv);
359 #endif
360
361 /*
362 * Just wild guess. RedBoot let us down and didn't reported
363 * memory size
364 */
365 if (realmem == 0)
366 realmem = btoc(32*1024*1024);
367
368 /*
369 * Allow build-time override in case Redboot lies
370 * or in other situations (eg where there's u-boot)
371 * where there isn't (yet) a convienent method of
372 * being told how much RAM is available.
373 *
374 * This happens on at least the Ubiquiti LS-SR71A
375 * board, where redboot says there's 16mb of RAM
376 * but in fact there's 32mb.
377 */
378 #if defined(AR71XX_REALMEM)
379 realmem = btoc(AR71XX_REALMEM);
380 #endif
381
382 /* phys_avail regions are in bytes */
383 phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
384 phys_avail[1] = ctob(realmem);
385
386 dump_avail[0] = phys_avail[0];
387 dump_avail[1] = phys_avail[1] - phys_avail[0];
388
389 physmem = realmem;
390
391 /*
392 * ns8250 uart code uses DELAY so ticker should be inititalized
393 * before cninit. And tick_init_params refers to hz, so * init_param1
394 * should be called first.
395 */
396 init_param1();
397
398 /* Detect the system type - this is needed for subsequent chipset-specific calls */
399 ar71xx_detect_sys_type();
400 ar71xx_detect_sys_frequency();
401
402 platform_counter_freq = ar71xx_cpu_freq();
403 mips_timer_init_params(platform_counter_freq, 1);
404 cninit();
405 init_static_kenv(boot1_env, sizeof(boot1_env));
406
407 printf("CPU platform: %s\n", ar71xx_get_system_type());
408 printf("CPU Frequency=%d MHz\n", u_ar71xx_cpu_freq / 1000000);
409 printf("CPU DDR Frequency=%d MHz\n", u_ar71xx_ddr_freq / 1000000);
410 printf("CPU AHB Frequency=%d MHz\n", u_ar71xx_ahb_freq / 1000000);
411 printf("platform frequency: %lld MHz\n", platform_counter_freq / 1000000);
412 printf("CPU reference clock: %d MHz\n", u_ar71xx_refclk / 1000000);
413 printf("CPU MDIO clock: %d MHz\n", u_ar71xx_mdio_freq / 1000000);
414 printf("arguments: \n");
415 printf(" a0 = %08x\n", a0);
416 printf(" a1 = %08x\n", a1);
417 printf(" a2 = %08x\n", a2);
418 printf(" a3 = %08x\n", a3);
419
420 strcpy(cpu_model, ar71xx_get_system_type());
421
422 /*
423 * XXX this code is very redboot specific.
424 */
425 printf("Cmd line:");
426 if (MIPS_IS_VALID_PTR(argv)) {
427 for (i = 0; i < argc; i++) {
428 printf(" %s", argv[i]);
429 parse_argv(argv[i]);
430 }
431 }
432 else
433 printf ("argv is invalid");
434 printf("\n");
435
436 printf("Environment:\n");
437 if (MIPS_IS_VALID_PTR(envp)) {
438 for (i = 0; envp[i]; i+=2) {
439 printf(" %s = %s\n", envp[i], envp[i+1]);
440 kern_setenv(envp[i], envp[i+1]);
441 }
442 }
443 else
444 printf ("envp is invalid\n");
445
446 /* Platform setup */
447 init_param2(physmem);
448 mips_cpu_init();
449 pmap_bootstrap();
450 mips_proc0_init();
451 mutex_init();
452
453 /*
454 * Reset USB devices
455 */
456 ar71xx_init_usb_peripheral();
457
458 /*
459 * Reset internal ethernet switch, if one exists
460 */
461 ar71xx_reset_ethernet_switch();
462
463 /*
464 * Initialise the gmac driver.
465 */
466 ar71xx_init_gmac();
467
468 /* Redboot if_arge MAC address is in the environment */
469 (void) ar71xx_redboot_get_macaddr();
470
471 /* Various other boards need things to come out of EEPROM */
472 (void) ar71xx_platform_read_eeprom_mac();
473
474 /* Initialise the MAC address hint map */
475 ar71xx_platform_check_mac_hints();
476
477 kdb_init();
478 #ifdef KDB
479 if (boothowto & RB_KDB)
480 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
481 #endif
482 }
Cache object: 796191d9b206af4f427bf23dc37e64c6
|