2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Oleksandr Tymoshenko
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include "opt_ar71xx.h"
35 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
43 #include <sys/mutex.h>
45 #include <sys/reboot.h>
48 #include <vm/vm_param.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_phys.h>
52 #include <net/ethernet.h>
54 #include <machine/clock.h>
55 #include <machine/cpu.h>
56 #include <machine/cpuregs.h>
57 #include <machine/hwfunc.h>
58 #include <machine/md_var.h>
59 #include <machine/trap.h>
61 #include <mips/atheros/ar71xxreg.h>
63 #include <mips/atheros/ar71xx_setup.h>
64 #include <mips/atheros/ar71xx_cpudef.h>
65 #include <mips/atheros/ar71xx_macaddr.h>
67 extern char edata[], end[];
69 /* 4KB static data aread to keep a copy of the bootload env until
70 the dynamic kenv is setup */
82 ar71xx_device_stop(RST_RESET_FULL_CHIP);
89 * Obtain the MAC address via the Redboot environment.
92 ar71xx_redboot_get_macaddr(void)
96 uint32_t macaddr[ETHER_ADDR_LEN];
97 uint8_t tmpmac[ETHER_ADDR_LEN];
100 * "ethaddr" is passed via envp on RedBoot platforms
101 * "kmac" is passed via argv on RouterBOOT platforms
103 if ((var = kern_getenv("ethaddr")) != NULL ||
104 (var = kern_getenv("kmac")) != NULL) {
105 count = sscanf(var, "%x%*c%x%*c%x%*c%x%*c%x%*c%x",
106 &macaddr[0], &macaddr[1],
107 &macaddr[2], &macaddr[3],
108 &macaddr[4], &macaddr[5]);
114 for (i = 0; i < ETHER_ADDR_LEN; i++)
115 tmpmac[i] = macaddr[i] & 0xff;
116 (void) ar71xx_mac_addr_init(ar71xx_board_mac_addr,
127 #ifdef AR71XX_ENV_ROUTERBOOT
129 * RouterBoot gives us the board memory in a command line argument.
132 ar71xx_routerboot_get_mem(int argc, char **argv)
137 * Protect ourselves from garbage in registers.
139 if (!MIPS_IS_VALID_PTR(argv))
142 for (i = 0; i < argc; i++) {
145 if (strncmp(argv[i], "mem=", 4) == 0) {
146 if (sscanf(argv[i] + 4, "%dM", &board_mem) == 1)
147 return (btoc(board_mem * 1024 * 1024));
156 * Handle initialising the MAC address from a specific EEPROM
159 * This is done during (very) early boot.
161 * hint.ar71xx.0.eeprom_mac_addr=<address to read from>
162 * hint.ar71xx.0.eeprom_mac_isascii=<0|1>
165 ar71xx_platform_read_eeprom_mac(void)
167 long eeprom_mac_addr = 0;
169 int i, readascii = 0;
170 uint8_t macaddr[ETHER_ADDR_LEN];
172 if (resource_long_value("ar71xx", 0, "eeprom_mac_addr",
173 &eeprom_mac_addr) != 0)
176 /* get a pointer to the EEPROM MAC address */
178 mac = (const char *) MIPS_PHYS_TO_KSEG1(eeprom_mac_addr);
180 /* Check if it's ASCII or not */
181 if (resource_int_value("ar71xx", 0, "eeprom_mac_isascii",
182 &readascii) == 0 && readascii == 1) {
183 printf("ar71xx: Overriding MAC from EEPROM (ascii)\n");
184 for (i = 0; i < 6; i++) {
185 macaddr[i] = strtol(&(mac[i*3]), NULL, 16);
188 printf("ar71xx: Overriding MAC from EEPROM\n");
189 for (i = 0; i < 6; i++) {
194 /* Set the default board MAC */
195 (void) ar71xx_mac_addr_init(ar71xx_board_mac_addr,
199 printf("ar71xx: Board MAC: %6D\n", ar71xx_board_mac_addr, ":");
204 * Populate a kenv hint for the given device based on the given
205 * MAC address and offset.
207 * Returns 0 if ok, < 0 on error.
210 ar71xx_platform_set_mac_hint(const char *dev, int unit,
211 const uint8_t *macaddr, int offset, int islocal)
214 uint8_t lclmac[ETHER_ADDR_LEN];
217 /* Initialise the MAC address, plus/minus the offset */
218 if (ar71xx_mac_addr_init(lclmac, macaddr, offset, islocal) != 0) {
222 /* Turn it into a string */
223 snprintf(macstr, 32, "%6D", lclmac, ":");
224 snprintf(devstr, 32, "hint.%s.%d.macaddr", dev, unit);
226 printf(" %s => %s\n", devstr, macstr);
229 if (kern_setenv(devstr, macstr) != 0) {
230 printf("%s: failed to set hint (%s => %s)\n",
241 * Iterate through the list of boot time hints that populate
242 * a device MAC address hint based on the "board" MAC address.
244 * ar71xx_mac_map.X.devid=<device id, eg ath>
245 * ar71xx_mac_map.X.unitid=<unit id, eg 0>
246 * ar71xx_mac_map.X.offset=<mac address value offset>
247 * ar71xx_mac_map.X.is_local=<1 or 0>
250 ar71xx_platform_check_mac_hints(void)
254 int offset, is_local, unitid;
256 for (i = 0; i < 8; i++) {
257 if (resource_string_value("ar71xx_mac_map", i, "devid",
260 if (resource_int_value("ar71xx_mac_map", i, "unitid",
263 if (resource_int_value("ar71xx_mac_map", i, "offset",
266 if (resource_int_value("ar71xx_mac_map", i, "is_local",
269 printf("ar71xx: devid '%s.%d', MAC offset '%d'\n",
270 devid, unitid, offset);
271 (void) ar71xx_platform_set_mac_hint(devid, unitid,
272 ar71xx_board_mac_addr, offset, is_local);
279 platform_start(__register_t a0 __unused, __register_t a1 __unused,
280 __register_t a2 __unused, __register_t a3 __unused)
282 uint64_t platform_counter_freq;
284 char **argv = NULL, **envp = NULL;
288 * clear the BSS and SBSS segments, this should be first call in
291 kernend = (vm_offset_t)&end;
292 memset(&edata, 0, kernend - (vm_offset_t)(&edata));
294 mips_postboot_fixup();
296 /* Initialize pcpu stuff */
300 * Until some more sensible abstractions for uboot/redboot
301 * environment handling, we have to make this a compile-time
302 * hack. The existing code handles the uboot environment
303 * very incorrectly so we should just ignore initialising
304 * the relevant pointers.
306 #ifndef AR71XX_ENV_UBOOT
312 * Protect ourselves from garbage in registers
314 if (MIPS_IS_VALID_PTR(envp)) {
315 for (i = 0; envp[i]; i += 2) {
316 if (strcmp(envp[i], "memsize") == 0)
317 realmem = btoc(strtoul(envp[i+1], NULL, 16));
318 else if (strcmp(envp[i], "bootverbose") == 0)
319 bootverbose = btoc(strtoul(envp[i+1], NULL, 10));
324 #ifdef AR71XX_ENV_ROUTERBOOT
326 * RouterBoot informs the board memory as a command line argument.
329 realmem = ar71xx_routerboot_get_mem(argc, argv);
333 * Just wild guess. RedBoot let us down and didn't reported
337 realmem = btoc(32*1024*1024);
340 * Allow build-time override in case Redboot lies
341 * or in other situations (eg where there's u-boot)
342 * where there isn't (yet) a convienent method of
343 * being told how much RAM is available.
345 * This happens on at least the Ubiquiti LS-SR71A
346 * board, where redboot says there's 16mb of RAM
347 * but in fact there's 32mb.
349 #if defined(AR71XX_REALMEM)
350 realmem = btoc(AR71XX_REALMEM);
353 /* phys_avail regions are in bytes */
354 phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
355 phys_avail[1] = ctob(realmem);
358 dump_avail[1] = phys_avail[1];
363 * ns8250 uart code uses DELAY so ticker should be inititalized
364 * before cninit. And tick_init_params refers to hz, so * init_param1
365 * should be called first.
369 /* Detect the system type - this is needed for subsequent chipset-specific calls */
370 ar71xx_detect_sys_type();
371 ar71xx_detect_sys_frequency();
373 platform_counter_freq = ar71xx_cpu_freq();
374 mips_timer_init_params(platform_counter_freq, 1);
376 init_static_kenv(boot1_env, sizeof(boot1_env));
378 printf("CPU platform: %s\n", ar71xx_get_system_type());
379 printf("CPU Frequency=%d MHz\n", u_ar71xx_cpu_freq / 1000000);
380 printf("CPU DDR Frequency=%d MHz\n", u_ar71xx_ddr_freq / 1000000);
381 printf("CPU AHB Frequency=%d MHz\n", u_ar71xx_ahb_freq / 1000000);
382 printf("platform frequency: %lld MHz\n", platform_counter_freq / 1000000);
383 printf("CPU reference clock: %d MHz\n", u_ar71xx_refclk / 1000000);
384 printf("CPU MDIO clock: %d MHz\n", u_ar71xx_mdio_freq / 1000000);
385 printf("arguments: \n");
386 printf(" a0 = %08x\n", a0);
387 printf(" a1 = %08x\n", a1);
388 printf(" a2 = %08x\n", a2);
389 printf(" a3 = %08x\n", a3);
391 strcpy(cpu_model, ar71xx_get_system_type());
394 * XXX this code is very redboot specific.
397 if (MIPS_IS_VALID_PTR(argv)) {
398 for (i = 0; i < argc; i++) {
399 printf(" %s", argv[i]);
400 boothowto |= boot_parse_arg(argv[i]);
404 printf ("argv is invalid");
407 printf("Environment:\n");
408 if (MIPS_IS_VALID_PTR(envp)) {
409 for (i = 0; envp[i]; i+=2) {
410 printf(" %s = %s\n", envp[i], envp[i+1]);
411 kern_setenv(envp[i], envp[i+1]);
415 printf ("envp is invalid\n");
418 init_param2(physmem);
427 ar71xx_init_usb_peripheral();
430 * Reset internal ethernet switch, if one exists
432 ar71xx_reset_ethernet_switch();
435 * Initialise the gmac driver.
439 /* Redboot if_arge MAC address is in the environment */
440 (void) ar71xx_redboot_get_macaddr();
442 /* Various other boards need things to come out of EEPROM */
443 (void) ar71xx_platform_read_eeprom_mac();
445 /* Initialise the MAC address hint map */
446 ar71xx_platform_check_mac_hints();
450 if (boothowto & RB_KDB)
451 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");