1 /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //#include <lib/ptable.h>
34 //#include <qpic_nand.h>
37 //#include <platform.h>
43 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
45 #define debugf(fmt, args...)
48 //extern uint32_t target_dev_tree_mem(void *fdt, uint32_t memory_node_offset);
49 extern int hw_revision;
50 uint32_t target_dev_tree_mem(void *fdt, uint32_t memory_node_offset)
55 /* TODO: This function needs to be moved to target layer to check violations
56 * against all the other regions as well.
58 extern int check_aboot_addr_range_overlap(uint32_t start, uint32_t size);
61 * Will relocate the DTB to the tags addr if the device tree is found and return
64 * Arguments: kernel - Start address of the kernel loaded in RAM
65 * tags - Start address of the tags loaded in RAM
66 * kernel_size - Size of the kernel in bytes
68 * Return Value: DTB address : If appended device tree is found
71 void *dev_tree_appended(void *kernel, void *tags, uint32_t kernel_size)
73 uint32_t app_dtb_offset = 0;
76 memcpy((void*) &app_dtb_offset, (void*) (kernel + DTB_OFFSET), sizeof(uint32_t));
79 * Check if we have valid offset for the DTB, if not return error.
80 * If the kernel image does not have appeneded device tree, DTB offset
81 * might contain some random address which is not accessible & cause
82 * data abort. If kernel start + dtb offset address exceed the total
83 * size of the kernel, then we dont have an appeneded DTB.
85 if (app_dtb_offset < kernel_size)
87 if (!fdt_check_header((void*) (kernel + app_dtb_offset)))
92 debugf( "Found Appeneded Flattened Device tree\n");
93 dtb = kernel + app_dtb_offset;
94 size = fdt_totalsize(dtb);
95 if (check_aboot_addr_range_overlap(tags, size))
97 debugf("Appended dtb aboot overlap check failed.\n");
100 rc = fdt_open_into(dtb, tags, size);
103 /* clear out the old DTB magic so kernel doesn't find it */
104 *((uint32_t *)dtb) = 0;
110 debugf( "DTB offset is incorrect, kernel image does not have appended DTB\n");
115 /* Function to return the pointer to the start of the correct device tree
116 * based on the platform data.
118 struct dt_entry * dev_tree_get_entry_ptr(struct dt_table *table)
121 struct dt_entry *dt_entry_ptr;
122 struct dt_entry *latest_dt_entry = NULL;
124 dt_entry_ptr = (struct dt_entry *)((char *)table + DEV_TREE_HEADER_SIZE);
126 debugf("magic=%x,ver=%d,num=%d\n",table->magic,table->version,table->num_entries);
128 #ifndef CONFIG_MULTI_DTS
129 hw_revision = DT_HARDWARE_ID;
131 debugf("detected hw_rev=%x\n", hw_revision);
133 for(i = 0; i < table->num_entries; i++)
135 debugf("platform_id=%d, hw_rev=%x, soc_rev=%x\n",
136 dt_entry_ptr->platform_id,dt_entry_ptr->variant_id,
137 dt_entry_ptr->soc_rev);
139 /* DTBs are stored in the ascending order of soc revision.
140 * For eg: Rev0..Rev1..Rev2 & so on.
141 * we pickup the DTB with highest soc rev number which is less
142 * than or equal to actual hardware
144 if((dt_entry_ptr->platform_id == DT_PLATFROM_ID) &&
145 (dt_entry_ptr->variant_id == hw_revision) &&
146 (dt_entry_ptr->soc_rev == DT_SOC_VER))
148 debugf("Find matched DTB hw_rev=%x\n",
149 dt_entry_ptr->variant_id);
153 /* SLP kernel DT SOC VER check */
154 if((dt_entry_ptr->platform_id == DT_PLATFROM_ID) &&
155 (dt_entry_ptr->variant_id == hw_revision) &&
156 (dt_entry_ptr->soc_rev == DT_SLP_SOC_VER))
158 debugf("Find matched DTB hw_rev=%x\n",
159 dt_entry_ptr->variant_id);
164 /* if the exact match not found, return the closest match
165 * assuming it to be the nearest soc version
167 if((dt_entry_ptr->platform_id == DT_PLATFROM_ID) &&
168 (dt_entry_ptr->variant_id <= hw_revision) &&
169 (dt_entry_ptr->soc_rev <= DT_SOC_VER)) {
170 latest_dt_entry = dt_entry_ptr;
175 if (latest_dt_entry) {
176 debugf( "Loading DTB with SOC version:%x\n", latest_dt_entry->soc_rev);
177 return latest_dt_entry;
183 int load_dtb(int addr,void* dt_img_adr)
185 struct dt_table *table;
186 struct dt_entry *dt_entry_ptr;
188 /* offset now point to start of dt.img */
189 table = (struct dt_table*)dt_img_adr;
191 /* Restriction that the device tree entry table should be less than a page*/
192 //ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
194 /* Validate the device tree table header */
195 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
196 debugf("ERROR: Cannot validate Device Tree Table %x %u\n", table->magic, table->version);
200 /* Calculate the offset of device tree within device tree table */
201 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
202 debugf("ERROR: Getting device tree address failed\n");
206 /* Validate and Read device device tree in the "tags_add */
207 //if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
209 // debugf("Device tree addresses overlap with aboot addresses.\n");
213 memcpy((void*)addr,(const void*)(dt_img_adr + dt_entry_ptr->offset), dt_entry_ptr->size);
220 /* Function to add the first RAM partition info to the device tree.
221 * Note: The function replaces the reg property in the "/memory" node
222 * with the addr and size provided.
224 int dev_tree_add_first_mem_info(uint32_t *fdt, uint32_t offset, uint32_t addr, uint32_t size)
228 ret = fdt_setprop_u32(fdt, offset, "reg", addr);
232 debugf( "Failed to add the memory information addr: %d\n",
237 ret = fdt_appendprop_u32(fdt, offset, "reg", size);
241 debugf( "Failed to add the memory information size: %d\n",
248 /* Function to add the subsequent RAM partition info to the device tree. */
249 int dev_tree_add_mem_info(void *fdt, uint32_t offset, uint32_t addr, uint32_t size)
251 static int mem_info_cnt = 0;
256 /* Replace any other reg prop in the memory node. */
257 ret = fdt_setprop_u32(fdt, offset, "reg", addr);
262 /* Append the mem info to the reg prop for subsequent nodes. */
263 ret = fdt_appendprop_u32(fdt, offset, "reg", addr);
268 debugf( "Failed to add the memory information addr: %d\n",
273 ret = fdt_appendprop_u32(fdt, offset, "reg", size);
277 debugf( "Failed to add the memory information size: %d\n",
284 /* Top level function that updates the device tree. */
285 int update_device_tree(void *fdt, const char *cmdline,
286 void *ramdisk, uint32_t ramdisk_size)
291 /* Check the device tree header */
292 ret = fdt_check_header(fdt);
295 debugf( "Invalid device tree header \n");
299 /* Add padding to make space for new nodes and properties. */
300 ret = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + DTB_PAD_SIZE);
303 debugf( "Failed to move/resize dtb buffer: %d\n", ret);
307 /* Get offset of the memory node */
308 ret = fdt_path_offset(fdt, "/memory");
311 debugf( "Could not find memory node.\n");
317 ret = target_dev_tree_mem(fdt, offset);
320 debugf( "ERROR: Cannot update memory node\n");
324 /* Get offset of the chosen node */
325 ret = fdt_path_offset(fdt, "/chosen");
328 debugf("Could not find chosen node.\n");
333 /* Adding the cmdline to the chosen node */
334 ret = fdt_setprop_string(fdt, offset, (const char*)"bootargs", (const void*)cmdline);
337 debugf( "ERROR: Cannot update chosen node [bootargs]\n");
341 /* Adding the initrd-start to the chosen node */
342 ret = fdt_setprop_u32(fdt, offset, "linux,initrd-start", (uint32_t)ramdisk);
345 debugf( "ERROR: Cannot update chosen node [linux,initrd-start]\n");
349 /* Adding the initrd-end to the chosen node */
350 ret = fdt_setprop_u32(fdt, offset, "linux,initrd-end", ((uint32_t)ramdisk + ramdisk_size));
353 debugf( "ERROR: Cannot update chosen node [linux,initrd-end]\n");