2 * Copyright (c) 2009-2010 The FreeBSD Foundation
4 * This software was developed by Semihalf under sponsorship from
5 * the FreeBSD Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
31 #include <fdt_platform.h>
35 #define STR(number) #number
36 #define STRINGIFY(number) STR(number)
39 fdt_platform_load_from_ubenv(const char *var)
41 struct fdt_header *hdr;
46 if (s == NULL || *s == '\0')
49 hdr = (struct fdt_header *)strtoul(s, &p, 16);
53 if (fdt_load_dtb_addr(hdr) == 0) {
54 printf("Using DTB provided by U-Boot at "
55 "address %p.\n", hdr);
62 #define FDT_DTB_PADSZ 1024
65 fdt_platform_load_dtb(void)
67 struct fdt_header *hdr;
73 * If the U-boot environment contains a variable giving the address of a
74 * valid blob in memory, use it. The U-boot README says the right
75 * variable for fdt data loaded into ram is fdt_addr_r, so try that
76 * first. Board vendors also use both fdtaddr and fdt_addr names.
78 if ((rv = fdt_platform_load_from_ubenv("fdt_addr_r")) == 0)
80 if ((rv = fdt_platform_load_from_ubenv("fdt_addr")) == 0)
82 if ((rv = fdt_platform_load_from_ubenv("fdtaddr")) == 0)
88 * Try to get FDT filename first from loader env and then from u-boot env
90 s = getenv("fdt_file");
92 s = ub_env_get("fdtfile");
94 s = ub_env_get("fdt_file");
95 if (s != NULL && *s != '\0') {
96 if (fdt_load_dtb_file(s) == 0) {
97 printf("Loaded DTB from file '%s'.\n", s);
108 fdt_platform_load_overlays(void)
111 fdt_load_dtb_overlays(ub_env_get("fdt_overlays"));
115 fdt_platform_fixups(void)
117 static struct fdt_mem_region regions[UB_MAX_MR];
118 const char *env, *str;
120 int eth_no, i, len, n;
127 /* Apply overlays before anything else */
128 if (fdt_apply_overlays() > 0)
129 fdt_pad_dtb(FDT_DTB_PADSZ);
131 /* Acquire sys_info */
132 si = ub_get_sys_info();
134 while ((env = ub_env_enum(env)) != NULL) {
135 if (strncmp(env, "eth", 3) == 0 &&
136 strncmp(env + (strlen(env) - 4), "addr", 4) == 0) {
138 * Handle Ethernet addrs: parse uboot env eth%daddr
143 * Check how many chars we will need to store
144 * maximal eth iface number.
146 len = strlen(STRINGIFY(TMP_MAX_ETH)) +
147 strlen("ethernet") + 1;
150 * Reserve mem for string "ethernet" and len
151 * chars for iface no.
153 ethstr = (char *)malloc(len * sizeof(char));
154 bzero(ethstr, len * sizeof(char));
155 strcpy(ethstr, "ethernet0");
158 /* Extract interface number */
159 i = strtol(env + 3, &end, 10);
160 if (end == (env + 3))
161 /* 'ethaddr' means interface 0 address */
169 str = ub_env_get(env);
173 * Find the length of the interface id by
174 * taking in to account the first 3 and
178 strncpy(ethstr + 8, env + 3, i);
182 fdt_fixup_ethernet(str, ethstr, len);
184 /* Clear ethernet..XXXX.. string */
185 bzero(ethstr + 8, len - 8);
189 } else if (strcmp(env, "consoledev") == 0) {
190 str = ub_env_get(env);
191 fdt_fixup_stdout(str);
195 /* Modify cpu(s) and bus clock frequenties in /cpus node [Hz] */
196 fdt_fixup_cpubusfreqs(si->clk_cpu, si->clk_bus);
198 /* Extract the DRAM regions into fdt_mem_region format. */
199 for (i = 0, n = 0; i < si->mr_no && n < nitems(regions); i++) {
200 if (si->mr[i].flags == MR_ATTR_DRAM) {
201 regions[n].start = si->mr[i].start;
202 regions[n].size = si->mr[i].size;
207 /* Fixup memory regions */
208 fdt_fixup_memory(regions, n);