]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/uboot/fdt/uboot_fdt.c
Merge compiler-rt trunk r321017 to contrib/compiler-rt.
[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                                 return (0);
68                         }
69                 }
70         }
71
72         rv = 1;
73
74         /*
75          * Try to get FDT filename first from loader env and then from u-boot env
76          */
77         s = getenv("fdt_file");
78         if (s == NULL)
79                 s = ub_env_get("fdtfile");
80         if (s == NULL)
81                 s = ub_env_get("fdt_file");
82         if (s != NULL && *s != '\0') {
83                 if (fdt_load_dtb_file(s) == 0) {
84                         printf("Loaded DTB from file '%s'.\n", s);
85                         rv = 0;
86                 }
87         }
88
89         if (rv == 0) {
90                 s = getenv("fdt_overlays");
91                 if (s == NULL)
92                         s = ub_env_get("fdt_overlays");
93                 if (s != NULL && *s != '\0') {
94                         printf("Loading DTB overlays: '%s'\n", s);
95                         fdt_load_dtb_overlays(s);
96                 }
97         }
98
99         return (rv);
100 }
101
102 void
103 fdt_platform_fixups(void)
104 {
105         static struct fdt_mem_region regions[UB_MAX_MR];
106         const char *env, *str;
107         char *end, *ethstr;
108         int eth_no, i, len, n;
109         struct sys_info *si;
110
111         env = NULL;
112         eth_no = 0;
113         ethstr = NULL;
114
115         /* Apply overlays before anything else */
116         fdt_apply_overlays();
117
118         /* Acquire sys_info */
119         si = ub_get_sys_info();
120
121         while ((env = ub_env_enum(env)) != NULL) {
122                 if (strncmp(env, "eth", 3) == 0 &&
123                     strncmp(env + (strlen(env) - 4), "addr", 4) == 0) {
124                         /*
125                          * Handle Ethernet addrs: parse uboot env eth%daddr
126                          */
127
128                         if (!eth_no) {
129                                 /*
130                                  * Check how many chars we will need to store
131                                  * maximal eth iface number.
132                                  */
133                                 len = strlen(STRINGIFY(TMP_MAX_ETH)) +
134                                     strlen("ethernet") + 1;
135
136                                 /*
137                                  * Reserve mem for string "ethernet" and len
138                                  * chars for iface no.
139                                  */
140                                 ethstr = (char *)malloc(len * sizeof(char));
141                                 bzero(ethstr, len * sizeof(char));
142                                 strcpy(ethstr, "ethernet0");
143                         }
144
145                         /* Extract interface number */
146                         i = strtol(env + 3, &end, 10);
147                         if (end == (env + 3))
148                                 /* 'ethaddr' means interface 0 address */
149                                 n = 0;
150                         else
151                                 n = i;
152
153                         if (n > TMP_MAX_ETH)
154                                 continue;
155
156                         str = ub_env_get(env);
157
158                         if (n != 0) {
159                                 /*
160                                  * Find the length of the interface id by
161                                  * taking in to account the first 3 and
162                                  * last 4 characters.
163                                  */
164                                 i = strlen(env) - 7;
165                                 strncpy(ethstr + 8, env + 3, i);
166                         }
167
168                         /* Modify blob */
169                         fdt_fixup_ethernet(str, ethstr, len);
170
171                         /* Clear ethernet..XXXX.. string */
172                         bzero(ethstr + 8, len - 8);
173
174                         if (n + 1 > eth_no)
175                                 eth_no = n + 1;
176                 } else if (strcmp(env, "consoledev") == 0) {
177                         str = ub_env_get(env);
178                         fdt_fixup_stdout(str);
179                 }
180         }
181
182         /* Modify cpu(s) and bus clock frequenties in /cpus node [Hz] */
183         fdt_fixup_cpubusfreqs(si->clk_cpu, si->clk_bus);
184
185         /* Extract the DRAM regions into fdt_mem_region format. */
186         for (i = 0, n = 0; i < si->mr_no && n < nitems(regions); i++) {
187                 if (si->mr[i].flags == MR_ATTR_DRAM) {
188                         regions[n].start = si->mr[i].start;
189                         regions[n].size = si->mr[i].size;
190                         n++;
191                 }
192         }
193
194         /* Fixup memory regions */
195         fdt_fixup_memory(regions, n);
196 }