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);
76 * Try to get FDT filename first from loader env and then from u-boot env
78 s = getenv("fdt_file");
80 s = ub_env_get("fdtfile");
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);
93 fdt_load_dtb_overlays(ub_env_get("fdt_overlays"));
98 fdt_platform_fixups(void)
100 static struct fdt_mem_region regions[UB_MAX_MR];
101 const char *env, *str;
103 int eth_no, i, len, n;
110 /* Apply overlays before anything else */
111 fdt_apply_overlays();
113 /* Acquire sys_info */
114 si = ub_get_sys_info();
116 while ((env = ub_env_enum(env)) != NULL) {
117 if (strncmp(env, "eth", 3) == 0 &&
118 strncmp(env + (strlen(env) - 4), "addr", 4) == 0) {
120 * Handle Ethernet addrs: parse uboot env eth%daddr
125 * Check how many chars we will need to store
126 * maximal eth iface number.
128 len = strlen(STRINGIFY(TMP_MAX_ETH)) +
129 strlen("ethernet") + 1;
132 * Reserve mem for string "ethernet" and len
133 * chars for iface no.
135 ethstr = (char *)malloc(len * sizeof(char));
136 bzero(ethstr, len * sizeof(char));
137 strcpy(ethstr, "ethernet0");
140 /* Extract interface number */
141 i = strtol(env + 3, &end, 10);
142 if (end == (env + 3))
143 /* 'ethaddr' means interface 0 address */
151 str = ub_env_get(env);
155 * Find the length of the interface id by
156 * taking in to account the first 3 and
160 strncpy(ethstr + 8, env + 3, i);
164 fdt_fixup_ethernet(str, ethstr, len);
166 /* Clear ethernet..XXXX.. string */
167 bzero(ethstr + 8, len - 8);
171 } else if (strcmp(env, "consoledev") == 0) {
172 str = ub_env_get(env);
173 fdt_fixup_stdout(str);
177 /* Modify cpu(s) and bus clock frequenties in /cpus node [Hz] */
178 fdt_fixup_cpubusfreqs(si->clk_cpu, si->clk_bus);
180 /* Extract the DRAM regions into fdt_mem_region format. */
181 for (i = 0, n = 0; i < si->mr_no && n < nitems(regions); i++) {
182 if (si->mr[i].flags == MR_ATTR_DRAM) {
183 regions[n].start = si->mr[i].start;
184 regions[n].size = si->mr[i].size;
189 /* Fixup memory regions */
190 fdt_fixup_memory(regions, n);