2 * Copyright (c) 2009-2010 The FreeBSD Foundation
5 * This software was developed by Semihalf under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <fdt_platform.h>
39 #define STR(number) #number
40 #define STRINGIFY(number) STR(number)
43 fdt_platform_load_dtb(void)
45 struct fdt_header *hdr;
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.
56 s = ub_env_get("fdt_addr_r");
58 s = ub_env_get("fdtaddr");
60 s = ub_env_get("fdt_addr");
61 if (s != NULL && *s != '\0') {
62 hdr = (struct fdt_header *)strtoul(s, &p, 16);
64 if (fdt_load_dtb_addr(hdr) == 0) {
65 printf("Using DTB provided by U-Boot at "
66 "address %p.\n", hdr);
75 * Try to get FDT filename first from loader env and then from u-boot env
77 s = getenv("fdt_file");
79 s = ub_env_get("fdtfile");
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);
90 s = getenv("fdt_overlays");
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);
103 fdt_platform_fixups(void)
105 static struct fdt_mem_region regions[UB_MAX_MR];
106 const char *env, *str;
108 int eth_no, i, len, n;
115 /* Apply overlays before anything else */
116 fdt_apply_overlays();
118 /* Acquire sys_info */
119 si = ub_get_sys_info();
121 while ((env = ub_env_enum(env)) != NULL) {
122 if (strncmp(env, "eth", 3) == 0 &&
123 strncmp(env + (strlen(env) - 4), "addr", 4) == 0) {
125 * Handle Ethernet addrs: parse uboot env eth%daddr
130 * Check how many chars we will need to store
131 * maximal eth iface number.
133 len = strlen(STRINGIFY(TMP_MAX_ETH)) +
134 strlen("ethernet") + 1;
137 * Reserve mem for string "ethernet" and len
138 * chars for iface no.
140 ethstr = (char *)malloc(len * sizeof(char));
141 bzero(ethstr, len * sizeof(char));
142 strcpy(ethstr, "ethernet0");
145 /* Extract interface number */
146 i = strtol(env + 3, &end, 10);
147 if (end == (env + 3))
148 /* 'ethaddr' means interface 0 address */
156 str = ub_env_get(env);
160 * Find the length of the interface id by
161 * taking in to account the first 3 and
165 strncpy(ethstr + 8, env + 3, i);
169 fdt_fixup_ethernet(str, ethstr, len);
171 /* Clear ethernet..XXXX.. string */
172 bzero(ethstr + 8, len - 8);
176 } else if (strcmp(env, "consoledev") == 0) {
177 str = ub_env_get(env);
178 fdt_fixup_stdout(str);
182 /* Modify cpu(s) and bus clock frequenties in /cpus node [Hz] */
183 fdt_fixup_cpubusfreqs(si->clk_cpu, si->clk_bus);
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;
194 /* Fixup memory regions */
195 fdt_fixup_memory(regions, n);