Channagoud Kadabi | afd62bf | 2013-01-08 20:32:52 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 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. |
| 15 | * |
| 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. |
| 27 | */ |
| 28 | |
| 29 | #include <libfdt.h> |
| 30 | #include <dev_tree.h> |
| 31 | #include <lib/ptable.h> |
| 32 | #include <malloc.h> |
| 33 | #include <qpic_nand.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <platform.h> |
| 37 | #include <board.h> |
| 38 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 39 | extern int target_is_emmc_boot(void); |
| 40 | extern uint32_t target_dev_tree_mem(void *fdt, uint32_t memory_node_offset); |
Deepa Dinamani | c55f01b | 2013-05-30 14:05:56 -0700 | [diff] [blame^] | 41 | /* TODO: This function needs to be moved to target layer to check violations |
| 42 | * against all the other regions as well. |
| 43 | */ |
| 44 | extern int check_aboot_addr_range_overlap(uint32_t start, uint32_t size); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 45 | |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 46 | /* |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 47 | * Will relocate the DTB to the tags addr if the device tree is found and return |
| 48 | * its address |
| 49 | * |
| 50 | * Arguments: kernel - Start address of the kernel loaded in RAM |
| 51 | * tags - Start address of the tags loaded in RAM |
Channagoud Kadabi | 704cd56 | 2013-04-25 15:19:59 -0700 | [diff] [blame] | 52 | * kernel_size - Size of the kernel in bytes |
| 53 | * |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 54 | * Return Value: DTB address : If appended device tree is found |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 55 | * 'NULL' : Otherwise |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 56 | */ |
Channagoud Kadabi | 704cd56 | 2013-04-25 15:19:59 -0700 | [diff] [blame] | 57 | void *dev_tree_appended(void *kernel, void *tags, uint32_t kernel_size) |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 58 | { |
| 59 | uint32_t app_dtb_offset = 0; |
Deepa Dinamani | c55f01b | 2013-05-30 14:05:56 -0700 | [diff] [blame^] | 60 | uint32_t size; |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 61 | |
| 62 | memcpy((void*) &app_dtb_offset, (void*) (kernel + DTB_OFFSET), sizeof(uint32_t)); |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 63 | |
Channagoud Kadabi | 704cd56 | 2013-04-25 15:19:59 -0700 | [diff] [blame] | 64 | /* |
| 65 | * Check if we have valid offset for the DTB, if not return error. |
| 66 | * If the kernel image does not have appeneded device tree, DTB offset |
| 67 | * might contain some random address which is not accessible & cause |
| 68 | * data abort. If kernel start + dtb offset address exceed the total |
| 69 | * size of the kernel, then we dont have an appeneded DTB. |
| 70 | */ |
| 71 | if (app_dtb_offset < kernel_size) |
| 72 | { |
| 73 | if (!fdt_check_header((void*) (kernel + app_dtb_offset))) |
| 74 | { |
| 75 | void *dtb; |
| 76 | int rc; |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 77 | |
Channagoud Kadabi | 704cd56 | 2013-04-25 15:19:59 -0700 | [diff] [blame] | 78 | dprintf(INFO, "Found Appeneded Flattened Device tree\n"); |
| 79 | dtb = kernel + app_dtb_offset; |
Deepa Dinamani | c55f01b | 2013-05-30 14:05:56 -0700 | [diff] [blame^] | 80 | size = fdt_totalsize(dtb); |
| 81 | if (check_aboot_addr_range_overlap(tags, size)) |
| 82 | { |
| 83 | dprintf(CRITICAL, "Appended dtb aboot overlap check failed.\n"); |
| 84 | return NULL; |
| 85 | } |
| 86 | rc = fdt_open_into(dtb, tags, size); |
Channagoud Kadabi | 704cd56 | 2013-04-25 15:19:59 -0700 | [diff] [blame] | 87 | if (rc == 0) |
| 88 | { |
| 89 | /* clear out the old DTB magic so kernel doesn't find it */ |
| 90 | *((uint32_t *)dtb) = 0; |
| 91 | return tags; |
| 92 | } |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 93 | } |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 94 | } |
Channagoud Kadabi | 704cd56 | 2013-04-25 15:19:59 -0700 | [diff] [blame] | 95 | else |
| 96 | dprintf(CRITICAL, "DTB offset is incorrect, kernel image does not have appended DTB\n"); |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 97 | |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 98 | return NULL; |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 101 | /* Function to return the pointer to the start of the correct device tree |
| 102 | * based on the platform data. |
| 103 | */ |
| 104 | struct dt_entry * dev_tree_get_entry_ptr(struct dt_table *table) |
| 105 | { |
| 106 | uint32_t i; |
| 107 | struct dt_entry *dt_entry_ptr; |
Channagoud Kadabi | afd62bf | 2013-01-08 20:32:52 -0800 | [diff] [blame] | 108 | struct dt_entry *latest_dt_entry = NULL; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 109 | |
| 110 | dt_entry_ptr = (struct dt_entry *)((char *)table + DEV_TREE_HEADER_SIZE); |
| 111 | |
| 112 | for(i = 0; i < table->num_entries; i++) |
| 113 | { |
Channagoud Kadabi | afd62bf | 2013-01-08 20:32:52 -0800 | [diff] [blame] | 114 | /* DTBs are stored in the ascending order of soc revision. |
| 115 | * For eg: Rev0..Rev1..Rev2 & so on. |
| 116 | * we pickup the DTB with highest soc rev number which is less |
| 117 | * than or equal to actual hardware |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 118 | */ |
| 119 | if((dt_entry_ptr->platform_id == board_platform_id()) && |
| 120 | (dt_entry_ptr->variant_id == board_hardware_id()) && |
Channagoud Kadabi | afd62bf | 2013-01-08 20:32:52 -0800 | [diff] [blame] | 121 | (dt_entry_ptr->soc_rev == board_soc_version())) |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 122 | { |
| 123 | return dt_entry_ptr; |
| 124 | } |
Channagoud Kadabi | afd62bf | 2013-01-08 20:32:52 -0800 | [diff] [blame] | 125 | /* if the exact match not found, return the closest match |
| 126 | * assuming it to be the nearest soc version |
| 127 | */ |
| 128 | if((dt_entry_ptr->platform_id == board_platform_id()) && |
| 129 | (dt_entry_ptr->variant_id == board_hardware_id()) && |
| 130 | (dt_entry_ptr->soc_rev <= board_soc_version())) { |
| 131 | latest_dt_entry = dt_entry_ptr; |
| 132 | } |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 133 | dt_entry_ptr++; |
| 134 | } |
Channagoud Kadabi | afd62bf | 2013-01-08 20:32:52 -0800 | [diff] [blame] | 135 | |
| 136 | if (latest_dt_entry) { |
| 137 | dprintf(SPEW, "Loading DTB with SOC version:%x\n", latest_dt_entry->soc_rev); |
| 138 | return latest_dt_entry; |
| 139 | } |
| 140 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 141 | return NULL; |
| 142 | } |
| 143 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 144 | /* Function to add the first RAM partition info to the device tree. |
| 145 | * Note: The function replaces the reg property in the "/memory" node |
| 146 | * with the addr and size provided. |
| 147 | */ |
| 148 | int dev_tree_add_first_mem_info(uint32_t *fdt, uint32_t offset, uint32_t addr, uint32_t size) |
| 149 | { |
| 150 | int ret; |
| 151 | |
| 152 | ret = fdt_setprop_u32(fdt, offset, "reg", addr); |
| 153 | |
| 154 | if (ret) |
| 155 | { |
| 156 | dprintf(CRITICAL, "Failed to add the memory information addr: %d\n", |
| 157 | ret); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | ret = fdt_appendprop_u32(fdt, offset, "reg", size); |
| 162 | |
| 163 | if (ret) |
| 164 | { |
| 165 | dprintf(CRITICAL, "Failed to add the memory information size: %d\n", |
| 166 | ret); |
| 167 | } |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | /* Function to add the subsequent RAM partition info to the device tree. */ |
| 173 | int dev_tree_add_mem_info(void *fdt, uint32_t offset, uint32_t addr, uint32_t size) |
| 174 | { |
| 175 | static int mem_info_cnt = 0; |
| 176 | int ret; |
| 177 | |
| 178 | if (!mem_info_cnt) |
| 179 | { |
| 180 | /* Replace any other reg prop in the memory node. */ |
| 181 | ret = fdt_setprop_u32(fdt, offset, "reg", addr); |
| 182 | mem_info_cnt = 1; |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | /* Append the mem info to the reg prop for subsequent nodes. */ |
| 187 | ret = fdt_appendprop_u32(fdt, offset, "reg", addr); |
| 188 | } |
| 189 | |
| 190 | if (ret) |
| 191 | { |
| 192 | dprintf(CRITICAL, "Failed to add the memory information addr: %d\n", |
| 193 | ret); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | ret = fdt_appendprop_u32(fdt, offset, "reg", size); |
| 198 | |
| 199 | if (ret) |
| 200 | { |
| 201 | dprintf(CRITICAL, "Failed to add the memory information size: %d\n", |
| 202 | ret); |
| 203 | } |
| 204 | |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | /* Top level function that updates the device tree. */ |
| 209 | int update_device_tree(void *fdt, const char *cmdline, |
| 210 | void *ramdisk, uint32_t ramdisk_size) |
| 211 | { |
| 212 | int ret = 0; |
| 213 | uint32_t offset; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 214 | |
| 215 | /* Check the device tree header */ |
| 216 | ret = fdt_check_header(fdt); |
| 217 | if (ret) |
| 218 | { |
| 219 | dprintf(CRITICAL, "Invalid device tree header \n"); |
| 220 | return ret; |
| 221 | } |
| 222 | |
Deepa Dinamani | 1c97073 | 2013-04-19 14:23:01 -0700 | [diff] [blame] | 223 | /* Add padding to make space for new nodes and properties. */ |
| 224 | ret = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + DTB_PAD_SIZE); |
| 225 | if (ret!= 0) |
| 226 | { |
| 227 | dprintf(CRITICAL, "Failed to move/resize dtb buffer: %d\n", ret); |
| 228 | return ret; |
| 229 | } |
| 230 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 231 | /* Get offset of the memory node */ |
| 232 | ret = fdt_path_offset(fdt, "/memory"); |
| 233 | if (ret < 0) |
| 234 | { |
| 235 | dprintf(CRITICAL, "Could not find memory node.\n"); |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | offset = ret; |
| 240 | |
| 241 | ret = target_dev_tree_mem(fdt, offset); |
| 242 | if(ret) |
| 243 | { |
| 244 | dprintf(CRITICAL, "ERROR: Cannot update memory node\n"); |
| 245 | return ret; |
| 246 | } |
| 247 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 248 | /* Get offset of the chosen node */ |
| 249 | ret = fdt_path_offset(fdt, "/chosen"); |
| 250 | if (ret < 0) |
| 251 | { |
| 252 | dprintf(CRITICAL, "Could not find chosen node.\n"); |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | offset = ret; |
| 257 | /* Adding the cmdline to the chosen node */ |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 258 | ret = fdt_setprop_string(fdt, offset, (const char*)"bootargs", (const void*)cmdline); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 259 | if (ret) |
| 260 | { |
| 261 | dprintf(CRITICAL, "ERROR: Cannot update chosen node [bootargs]\n"); |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | /* Adding the initrd-start to the chosen node */ |
| 266 | ret = fdt_setprop_u32(fdt, offset, "linux,initrd-start", (uint32_t)ramdisk); |
| 267 | if (ret) |
| 268 | { |
| 269 | dprintf(CRITICAL, "ERROR: Cannot update chosen node [linux,initrd-start]\n"); |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | /* Adding the initrd-end to the chosen node */ |
| 274 | ret = fdt_setprop_u32(fdt, offset, "linux,initrd-end", ((uint32_t)ramdisk + ramdisk_size)); |
| 275 | if (ret) |
| 276 | { |
| 277 | dprintf(CRITICAL, "ERROR: Cannot update chosen node [linux,initrd-end]\n"); |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | fdt_pack(fdt); |
| 282 | |
| 283 | return ret; |
| 284 | } |