]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/atheros/ar71xx_machdep.c
Extract eventfilter declarations to sys/_eventfilter.h
[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_page.h>
49
50 #include <net/ethernet.h>
51
52 #include <machine/clock.h>
53 #include <machine/cpu.h>
54 #include <machine/cpuregs.h>
55 #include <machine/hwfunc.h>
56 #include <machine/md_var.h>
57 #include <machine/trap.h>
58 #include <machine/vmparam.h>
59
60 #include <mips/atheros/ar71xxreg.h>
61
62 #include <mips/atheros/ar71xx_setup.h>
63 #include <mips/atheros/ar71xx_cpudef.h>
64 #include <mips/atheros/ar71xx_macaddr.h>
65
66 extern char edata[], end[];
67
68 /* 4KB static data aread to keep a copy of the bootload env until
69    the dynamic kenv is setup */
70 char boot1_env[4096];
71
72 void
73 platform_cpu_init()
74 {
75         /* Nothing special */
76 }
77
78 void
79 platform_reset(void)
80 {
81         ar71xx_device_stop(RST_RESET_FULL_CHIP);
82         /* Wait for reset */
83         while(1)
84                 ;
85 }
86
87 /*
88  * Obtain the MAC address via the Redboot environment.
89  */
90 static int
91 ar71xx_redboot_get_macaddr(void)
92 {
93         char *var;
94         int count = 0, i;
95         uint32_t macaddr[ETHER_ADDR_LEN];
96         uint8_t tmpmac[ETHER_ADDR_LEN];
97
98         /*
99          * "ethaddr" is passed via envp on RedBoot platforms
100          * "kmac" is passed via argv on RouterBOOT platforms
101          */
102         if ((var = kern_getenv("ethaddr")) != NULL ||
103             (var = kern_getenv("kmac")) != NULL) {
104                 count = sscanf(var, "%x%*c%x%*c%x%*c%x%*c%x%*c%x",
105                     &macaddr[0], &macaddr[1],
106                     &macaddr[2], &macaddr[3],
107                     &macaddr[4], &macaddr[5]);
108
109                 if (count < 6) {
110                         memset(macaddr, 0,
111                             sizeof(macaddr));
112                 } else {
113                         for (i = 0; i < ETHER_ADDR_LEN; i++)
114                                 tmpmac[i] = macaddr[i] & 0xff;
115                         (void) ar71xx_mac_addr_init(ar71xx_board_mac_addr,
116                             tmpmac,
117                             0, /* offset */
118                             0); /* is_local */
119                 }
120                 freeenv(var);
121                 return (0);
122         }
123         return (-1);
124 }
125
126 #ifdef  AR71XX_ENV_ROUTERBOOT
127 /*
128  * RouterBoot gives us the board memory in a command line argument.
129  */
130 static int
131 ar71xx_routerboot_get_mem(int argc, char **argv)
132 {
133         int i, board_mem;
134
135         /*
136          * Protect ourselves from garbage in registers.
137          */
138         if (!MIPS_IS_VALID_PTR(argv))
139                 return (0);
140
141         for (i = 0; i < argc; i++) {
142                 if (argv[i] == NULL)
143                         continue;
144                 if (strncmp(argv[i], "mem=", 4) == 0) {
145                         if (sscanf(argv[i] + 4, "%dM", &board_mem) == 1)
146                                 return (btoc(board_mem * 1024 * 1024));
147                 }
148         }
149
150         return (0);
151 }
152 #endif
153
154 /*
155  * Handle initialising the MAC address from a specific EEPROM
156  * offset.
157  *
158  * This is done during (very) early boot.
159  *
160  * hint.ar71xx.0.eeprom_mac_addr=<address to read from>
161  * hint.ar71xx.0.eeprom_mac_isascii=<0|1>
162  */
163 static int
164 ar71xx_platform_read_eeprom_mac(void)
165 {
166         long eeprom_mac_addr = 0;
167         const char *mac;
168         int i, readascii = 0;
169         uint8_t macaddr[ETHER_ADDR_LEN];
170
171         if (resource_long_value("ar71xx", 0, "eeprom_mac_addr",
172             &eeprom_mac_addr) != 0)
173                 return (-1);
174
175         /* get a pointer to the EEPROM MAC address */
176
177         mac = (const char *) MIPS_PHYS_TO_KSEG1(eeprom_mac_addr);
178
179         /* Check if it's ASCII or not */
180         if (resource_int_value("ar71xx", 0, "eeprom_mac_isascii",
181             &readascii) == 0 && readascii == 1) {
182                 printf("ar71xx: Overriding MAC from EEPROM (ascii)\n");
183                 for (i = 0; i < 6; i++) {
184                         macaddr[i] = strtol(&(mac[i*3]), NULL, 16);
185                 }
186         } else {
187                 printf("ar71xx: Overriding MAC from EEPROM\n");
188                 for (i = 0; i < 6; i++) {
189                         macaddr[i] = mac[i];
190                 }
191         }
192
193         /* Set the default board MAC */
194         (void) ar71xx_mac_addr_init(ar71xx_board_mac_addr,
195             macaddr,
196             0, /* offset */
197             0); /* is_local */
198         printf("ar71xx: Board MAC: %6D\n", ar71xx_board_mac_addr, ":");
199         return (0);
200 }
201
202 /*
203  * Populate a kenv hint for the given device based on the given
204  * MAC address and offset.
205  *
206  * Returns 0 if ok, < 0 on error.
207  */
208 static int
209 ar71xx_platform_set_mac_hint(const char *dev, int unit,
210     const uint8_t *macaddr, int offset, int islocal)
211 {
212         char macstr[32];
213         uint8_t lclmac[ETHER_ADDR_LEN];
214         char devstr[32];
215
216         /* Initialise the MAC address, plus/minus the offset */
217         if (ar71xx_mac_addr_init(lclmac, macaddr, offset, islocal) != 0) {
218                 return (-1);
219         }
220
221         /* Turn it into a string */
222         snprintf(macstr, 32, "%6D", lclmac, ":");
223         snprintf(devstr, 32, "hint.%s.%d.macaddr", dev, unit);
224
225         printf("  %s => %s\n", devstr, macstr);
226
227         /* Call setenv */
228         if (kern_setenv(devstr, macstr) != 0) {
229                 printf("%s: failed to set hint (%s => %s)\n",
230                     __func__,
231                     devstr,
232                     macstr);
233                 return (-1);
234         }
235
236         return (0);
237 }
238
239 /*
240  * Iterate through the list of boot time hints that populate
241  * a device MAC address hint based on the "board" MAC address.
242  *
243  * ar71xx_mac_map.X.devid=<device id, eg ath>
244  * ar71xx_mac_map.X.unitid=<unit id, eg 0>
245  * ar71xx_mac_map.X.offset=<mac address value offset>
246  * ar71xx_mac_map.X.is_local=<1 or 0>
247  */
248 static int
249 ar71xx_platform_check_mac_hints(void)
250 {
251         int i;
252         const char *devid;
253         int offset, is_local, unitid;
254
255         for (i = 0; i < 8; i++) {
256                 if (resource_string_value("ar71xx_mac_map", i, "devid",
257                     &devid) != 0)
258                         break;
259                 if (resource_int_value("ar71xx_mac_map", i, "unitid",
260                     &unitid) != 0)
261                         break;
262                 if (resource_int_value("ar71xx_mac_map", i, "offset",
263                     &offset) != 0)
264                         break;
265                 if (resource_int_value("ar71xx_mac_map", i, "is_local",
266                     &is_local) != 0)
267                         break;
268                 printf("ar71xx: devid '%s.%d', MAC offset '%d'\n",
269                     devid, unitid, offset);
270                 (void) ar71xx_platform_set_mac_hint(devid, unitid,
271                     ar71xx_board_mac_addr, offset, is_local);
272         }
273
274         return (0);
275 }
276
277 extern char cpu_model[];
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 }