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