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