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