]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/uboot/fdt/uboot_fdt.c
Import tzdata 2018c
[FreeBSD/FreeBSD.git] / stand / uboot / fdt / uboot_fdt.c
1 /*-
2  * Copyright (c) 2009-2010 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Semihalf under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <stand.h>
35 #include <fdt_platform.h>
36
37 #include "glue.h"
38
39 #define STR(number) #number
40 #define STRINGIFY(number) STR(number)
41
42 int
43 fdt_platform_load_dtb(void)
44 {
45         struct fdt_header *hdr;
46         const char *s;
47         char *p;
48         int rv;
49
50         /*
51          * If the U-boot environment contains a variable giving the address of a
52          * valid blob in memory, use it.  The U-boot README says the right
53          * variable for fdt data loaded into ram is fdt_addr_r, so try that
54          * first.  Board vendors also use both fdtaddr and fdt_addr names.
55          */
56         s = ub_env_get("fdt_addr_r");
57         if (s == NULL)
58                 s = ub_env_get("fdtaddr");
59         if (s == NULL)
60                 s = ub_env_get("fdt_addr");
61         if (s != NULL && *s != '\0') {
62                 hdr = (struct fdt_header *)strtoul(s, &p, 16);
63                 if (*p == '\0') {
64                         if (fdt_load_dtb_addr(hdr) == 0) {
65                                 printf("Using DTB provided by U-Boot at "
66                                     "address %p.\n", hdr);
67                                 rv = 0;
68                                 goto exit;
69                         }
70                 }
71         }
72
73         rv = 1;
74
75         /*
76          * Try to get FDT filename first from loader env and then from u-boot env
77          */
78         s = getenv("fdt_file");
79         if (s == NULL)
80                 s = ub_env_get("fdtfile");
81         if (s == NULL)
82                 s = ub_env_get("fdt_file");
83         if (s != NULL && *s != '\0') {
84                 if (fdt_load_dtb_file(s) == 0) {
85                         printf("Loaded DTB from file '%s'.\n", s);
86                         rv = 0;
87                         goto exit;
88                 }
89         }
90
91 exit:
92         if (rv == 0) {
93                 s = getenv("fdt_overlays");
94                 if (s == NULL)
95                         s = ub_env_get("fdt_overlays");
96                 if (s != NULL && *s != '\0') {
97                         printf("Loading DTB overlays: '%s'\n", s);
98                         fdt_load_dtb_overlays(s);
99                 }
100         }
101
102         return (rv);
103 }
104
105 void
106 fdt_platform_fixups(void)
107 {
108         static struct fdt_mem_region regions[UB_MAX_MR];
109         const char *env, *str;
110         char *end, *ethstr;
111         int eth_no, i, len, n;
112         struct sys_info *si;
113
114         env = NULL;
115         eth_no = 0;
116         ethstr = NULL;
117
118         /* Apply overlays before anything else */
119         fdt_apply_overlays();
120
121         /* Acquire sys_info */
122         si = ub_get_sys_info();
123
124         while ((env = ub_env_enum(env)) != NULL) {
125                 if (strncmp(env, "eth", 3) == 0 &&
126                     strncmp(env + (strlen(env) - 4), "addr", 4) == 0) {
127                         /*
128                          * Handle Ethernet addrs: parse uboot env eth%daddr
129                          */
130
131                         if (!eth_no) {
132                                 /*
133                                  * Check how many chars we will need to store
134                                  * maximal eth iface number.
135                                  */
136                                 len = strlen(STRINGIFY(TMP_MAX_ETH)) +
137                                     strlen("ethernet") + 1;
138
139                                 /*
140                                  * Reserve mem for string "ethernet" and len
141                                  * chars for iface no.
142                                  */
143                                 ethstr = (char *)malloc(len * sizeof(char));
144                                 bzero(ethstr, len * sizeof(char));
145                                 strcpy(ethstr, "ethernet0");
146                         }
147
148                         /* Extract interface number */
149                         i = strtol(env + 3, &end, 10);
150                         if (end == (env + 3))
151                                 /* 'ethaddr' means interface 0 address */
152                                 n = 0;
153                         else
154                                 n = i;
155
156                         if (n > TMP_MAX_ETH)
157                                 continue;
158
159                         str = ub_env_get(env);
160
161                         if (n != 0) {
162                                 /*
163                                  * Find the length of the interface id by
164                                  * taking in to account the first 3 and
165                                  * last 4 characters.
166                                  */
167                                 i = strlen(env) - 7;
168                                 strncpy(ethstr + 8, env + 3, i);
169                         }
170
171                         /* Modify blob */
172                         fdt_fixup_ethernet(str, ethstr, len);
173
174                         /* Clear ethernet..XXXX.. string */
175                         bzero(ethstr + 8, len - 8);
176
177                         if (n + 1 > eth_no)
178                                 eth_no = n + 1;
179                 } else if (strcmp(env, "consoledev") == 0) {
180                         str = ub_env_get(env);
181                         fdt_fixup_stdout(str);
182                 }
183         }
184
185         /* Modify cpu(s) and bus clock frequenties in /cpus node [Hz] */
186         fdt_fixup_cpubusfreqs(si->clk_cpu, si->clk_bus);
187
188         /* Extract the DRAM regions into fdt_mem_region format. */
189         for (i = 0, n = 0; i < si->mr_no && n < nitems(regions); i++) {
190                 if (si->mr[i].flags == MR_ATTR_DRAM) {
191                         regions[n].start = si->mr[i].start;
192                         regions[n].size = si->mr[i].size;
193                         n++;
194                 }
195         }
196
197         /* Fixup memory regions */
198         fdt_fixup_memory(regions, n);
199 }