Channagoud Kadabi | 571193a | 2014-02-05 13:58:49 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012-2014, 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> |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 38 | #include <list.h> |
| 39 | #include <kernel/thread.h> |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 40 | |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 41 | struct dt_entry_v1 |
| 42 | { |
| 43 | uint32_t platform_id; |
| 44 | uint32_t variant_id; |
| 45 | uint32_t soc_rev; |
| 46 | uint32_t offset; |
| 47 | uint32_t size; |
| 48 | }; |
| 49 | |
Sundarajan Srinivasan | 44de434 | 2013-07-08 14:47:13 -0700 | [diff] [blame] | 50 | static struct dt_mem_node_info mem_node; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 51 | static int platform_dt_absolute_match(struct dt_entry *cur_dt_entry, struct dt_entry_node *dt_list); |
| 52 | static struct dt_entry *platform_dt_match_best(struct dt_entry_node *dt_list); |
| 53 | static int update_dtb_entry_node(struct dt_entry_node *dt_list, uint32_t dtb_info); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 54 | extern int target_is_emmc_boot(void); |
| 55 | 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] | 56 | /* TODO: This function needs to be moved to target layer to check violations |
| 57 | * against all the other regions as well. |
| 58 | */ |
| 59 | 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] | 60 | |
Shashank Mittal | c0f1028 | 2013-07-15 14:53:31 -0700 | [diff] [blame] | 61 | /* Returns soc version if platform id and hardware id matches |
| 62 | otherwise return 0xFFFFFFFF */ |
| 63 | #define INVALID_SOC_REV_ID 0XFFFFFFFF |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 64 | |
| 65 | /* Add function to allocate dt entry list, used for recording |
| 66 | * the entry which conform to platform_dt_absolute_match() |
| 67 | */ |
| 68 | static struct dt_entry_node *dt_entry_list_init(void) |
| 69 | { |
| 70 | struct dt_entry_node *dt_node_member = NULL; |
| 71 | |
| 72 | dt_node_member = (struct dt_entry_node *) |
| 73 | malloc(sizeof(struct dt_entry_node)); |
| 74 | |
| 75 | ASSERT(dt_node_member); |
| 76 | |
| 77 | list_clear_node(&dt_node_member->node); |
| 78 | dt_node_member->dt_entry_m = (struct dt_entry *) |
| 79 | malloc(sizeof(struct dt_entry)); |
| 80 | ASSERT(dt_node_member->dt_entry_m); |
| 81 | |
| 82 | memset(dt_node_member->dt_entry_m ,0 ,sizeof(struct dt_entry)); |
| 83 | return dt_node_member; |
| 84 | } |
| 85 | |
| 86 | static void insert_dt_entry_in_queue(struct dt_entry_node *dt_list, struct dt_entry_node *dt_node_member) |
| 87 | { |
| 88 | list_add_tail(&dt_list->node, &dt_node_member->node); |
| 89 | } |
| 90 | |
| 91 | static void dt_entry_list_delete(struct dt_entry_node *dt_node_member) |
| 92 | { |
| 93 | if (list_in_list(&dt_node_member->node)) { |
| 94 | list_delete(&dt_node_member->node); |
| 95 | free(dt_node_member->dt_entry_m); |
| 96 | free(dt_node_member); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static int dev_tree_compatible(void *dtb, uint32_t dtb_size, struct dt_entry_node *dtb_list) |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 101 | { |
| 102 | int root_offset; |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 103 | const void *prop = NULL; |
| 104 | const char *plat_prop = NULL; |
| 105 | const char *board_prop = NULL; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 106 | const char *pmic_prop = NULL; |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 107 | char *model = NULL; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 108 | struct dt_entry *cur_dt_entry; |
| 109 | struct dt_entry *dt_entry_array = NULL; |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 110 | struct board_id *board_data = NULL; |
| 111 | struct plat_id *platform_data = NULL; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 112 | struct pmic_id *pmic_data = NULL; |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 113 | int len; |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 114 | int len_board_id; |
| 115 | int len_plat_id; |
| 116 | int min_plat_id_len = 0; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 117 | int len_pmic_id; |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 118 | uint32_t dtb_ver; |
| 119 | uint32_t num_entries = 0; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 120 | uint32_t i, j, k, n; |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 121 | uint32_t msm_data_count; |
| 122 | uint32_t board_data_count; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 123 | uint32_t pmic_data_count; |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 124 | |
| 125 | root_offset = fdt_path_offset(dtb, "/"); |
| 126 | if (root_offset < 0) |
| 127 | return false; |
| 128 | |
| 129 | prop = fdt_getprop(dtb, root_offset, "model", &len); |
| 130 | if (prop && len > 0) { |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 131 | model = (char *) malloc(sizeof(char) * len); |
| 132 | ASSERT(model); |
| 133 | strlcpy(model, prop, len); |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 134 | } else { |
vijay kumar | 89d36d8 | 2014-06-30 19:32:18 +0530 | [diff] [blame] | 135 | dprintf(INFO, "model does not exist in device tree\n"); |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 136 | } |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 137 | /* Find the pmic-id prop from DTB , if pmic-id is present then |
| 138 | * the DTB is version 3, otherwise find the board-id prop from DTB , |
| 139 | * if board-id is present then the DTB is version 2 */ |
| 140 | pmic_prop = (const char *)fdt_getprop(dtb, root_offset, "qcom,pmic-id", &len_pmic_id); |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 141 | board_prop = (const char *)fdt_getprop(dtb, root_offset, "qcom,board-id", &len_board_id); |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 142 | if (pmic_prop && (len_pmic_id > 0) && board_prop && (len_board_id > 0)) { |
| 143 | if ((len_pmic_id % PMIC_ID_SIZE) || (len_board_id % BOARD_ID_SIZE)) |
| 144 | { |
| 145 | dprintf(CRITICAL, "qcom,pmic-id(%d) or qcom,board-id(%d) in device tree is not a multiple of (%d %d)\n", |
| 146 | len_pmic_id, len_board_id, PMIC_ID_SIZE, BOARD_ID_SIZE); |
| 147 | return false; |
| 148 | } |
| 149 | dtb_ver = DEV_TREE_VERSION_V3; |
| 150 | min_plat_id_len = PLAT_ID_SIZE; |
| 151 | } else if (board_prop && len_board_id > 0) { |
vijay kumar | 89d36d8 | 2014-06-30 19:32:18 +0530 | [diff] [blame] | 152 | if (len_board_id % BOARD_ID_SIZE) |
| 153 | { |
| 154 | dprintf(CRITICAL, "qcom,board-id in device tree is (%d) not a multiple of (%d)\n", |
| 155 | len_board_id, BOARD_ID_SIZE); |
| 156 | return false; |
| 157 | } |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 158 | dtb_ver = DEV_TREE_VERSION_V2; |
| 159 | min_plat_id_len = PLAT_ID_SIZE; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 160 | } else { |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 161 | dtb_ver = DEV_TREE_VERSION_V1; |
| 162 | min_plat_id_len = DT_ENTRY_V1_SIZE; |
| 163 | } |
| 164 | |
| 165 | /* Get the msm-id prop from DTB */ |
| 166 | plat_prop = (const char *)fdt_getprop(dtb, root_offset, "qcom,msm-id", &len_plat_id); |
| 167 | if (!plat_prop || len_plat_id <= 0) { |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 168 | dprintf(INFO, "qcom,msm-id entry not found\n"); |
| 169 | return false; |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 170 | } else if (len_plat_id % min_plat_id_len) { |
| 171 | dprintf(INFO, "qcom,msm-id in device tree is (%d) not a multiple of (%d)\n", |
| 172 | len_plat_id, min_plat_id_len); |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 173 | return false; |
| 174 | } |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 175 | |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 176 | /* |
| 177 | * If DTB version is '1' look for <x y z> pair in the DTB |
| 178 | * x: platform_id |
| 179 | * y: variant_id |
| 180 | * z: SOC rev |
| 181 | */ |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 182 | if (dtb_ver == DEV_TREE_VERSION_V1) { |
| 183 | cur_dt_entry = (struct dt_entry *) |
| 184 | malloc(sizeof(struct dt_entry)); |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 185 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 186 | if (!cur_dt_entry) { |
| 187 | dprintf(CRITICAL, "Out of memory\n"); |
| 188 | return false; |
| 189 | } |
| 190 | memset(cur_dt_entry, 0, sizeof(struct dt_entry)); |
| 191 | |
| 192 | while (len_plat_id) { |
| 193 | cur_dt_entry->platform_id = fdt32_to_cpu(((const struct dt_entry_v1 *)plat_prop)->platform_id); |
| 194 | cur_dt_entry->variant_id = fdt32_to_cpu(((const struct dt_entry_v1 *)plat_prop)->variant_id); |
| 195 | cur_dt_entry->soc_rev = fdt32_to_cpu(((const struct dt_entry_v1 *)plat_prop)->soc_rev); |
| 196 | cur_dt_entry->board_hw_subtype = |
| 197 | fdt32_to_cpu(((const struct dt_entry_v1 *)plat_prop)->variant_id) >> 0x18; |
| 198 | cur_dt_entry->pmic_rev[0] = board_pmic_target(0); |
| 199 | cur_dt_entry->pmic_rev[1] = board_pmic_target(1); |
| 200 | cur_dt_entry->pmic_rev[2] = board_pmic_target(2); |
| 201 | cur_dt_entry->pmic_rev[3] = board_pmic_target(3); |
| 202 | cur_dt_entry->offset = (uint32_t)dtb; |
| 203 | cur_dt_entry->size = dtb_size; |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 204 | |
| 205 | dprintf(SPEW, "Found an appended flattened device tree (%s - %u %u 0x%x)\n", |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 206 | *model ? model : "unknown", |
| 207 | cur_dt_entry->platform_id, cur_dt_entry->variant_id, cur_dt_entry->soc_rev); |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 208 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 209 | if (platform_dt_absolute_match(cur_dt_entry, dtb_list)) { |
| 210 | dprintf(SPEW, "Device tree exact match the board: <%u %u 0x%x> != <%u %u 0x%x>\n", |
| 211 | cur_dt_entry->platform_id, |
| 212 | cur_dt_entry->variant_id, |
| 213 | cur_dt_entry->soc_rev, |
| 214 | board_platform_id(), |
| 215 | board_hardware_id(), |
| 216 | board_soc_version()); |
| 217 | |
| 218 | } else { |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 219 | dprintf(SPEW, "Device tree's msm_id doesn't match the board: <%u %u 0x%x> != <%u %u 0x%x>\n", |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 220 | cur_dt_entry->platform_id, |
| 221 | cur_dt_entry->variant_id, |
| 222 | cur_dt_entry->soc_rev, |
| 223 | board_platform_id(), |
| 224 | board_hardware_id(), |
| 225 | board_soc_version()); |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 226 | plat_prop += DT_ENTRY_V1_SIZE; |
| 227 | len_plat_id -= DT_ENTRY_V1_SIZE; |
| 228 | continue; |
| 229 | } |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 230 | } |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 231 | free(cur_dt_entry); |
| 232 | |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 233 | } |
| 234 | /* |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 235 | * If DTB Version is '3' then we have split DTB with board & msm data & pmic |
| 236 | * populated saperately in board-id & msm-id & pmic-id prop respectively. |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 237 | * Extract the data & prepare a look up table |
| 238 | */ |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 239 | else if (dtb_ver == DEV_TREE_VERSION_V2 || dtb_ver == DEV_TREE_VERSION_V3) { |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 240 | board_data_count = (len_board_id / BOARD_ID_SIZE); |
| 241 | msm_data_count = (len_plat_id / PLAT_ID_SIZE); |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 242 | /* If dtb version is v2.0, the pmic_data_count will be <= 0 */ |
| 243 | pmic_data_count = (len_pmic_id / PMIC_ID_SIZE); |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 244 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 245 | /* If we are using dtb v3.0, then we have split board, msm & pmic data in the DTB |
| 246 | * If we are using dtb v2.0, then we have split board & msmdata in the DTB |
| 247 | */ |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 248 | board_data = (struct board_id *) malloc(sizeof(struct board_id) * (len_board_id / BOARD_ID_SIZE)); |
| 249 | ASSERT(board_data); |
| 250 | platform_data = (struct plat_id *) malloc(sizeof(struct plat_id) * (len_plat_id / PLAT_ID_SIZE)); |
| 251 | ASSERT(platform_data); |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 252 | if (dtb_ver == DEV_TREE_VERSION_V3) { |
| 253 | pmic_data = (struct pmic_id *) malloc(sizeof(struct pmic_id) * (len_pmic_id / PMIC_ID_SIZE)); |
| 254 | ASSERT(pmic_data); |
| 255 | } |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 256 | i = 0; |
| 257 | |
| 258 | /* Extract board data from DTB */ |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 259 | for(i = 0 ; i < board_data_count; i++) { |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 260 | board_data[i].variant_id = fdt32_to_cpu(((struct board_id *)board_prop)->variant_id); |
| 261 | board_data[i].platform_subtype = fdt32_to_cpu(((struct board_id *)board_prop)->platform_subtype); |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 262 | /* For V2/V3 version of DTBs we have platform version field as part |
| 263 | * of variant ID, in such case the subtype will be mentioned as 0x0 |
| 264 | * As the qcom, board-id = <0xSSPMPmPH, 0x0> |
| 265 | * SS -- Subtype |
| 266 | * PM -- Platform major version |
| 267 | * Pm -- Platform minor version |
| 268 | * PH -- Platform hardware CDP/MTP |
| 269 | * In such case to make it compatible with LK algorithm move the subtype |
| 270 | * from variant_id to subtype field |
| 271 | */ |
| 272 | if (board_data[i].platform_subtype == 0) |
| 273 | board_data[i].platform_subtype = |
| 274 | fdt32_to_cpu(((struct board_id *)board_prop)->variant_id) >> 0x18; |
| 275 | |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 276 | len_board_id -= sizeof(struct board_id); |
| 277 | board_prop += sizeof(struct board_id); |
| 278 | } |
| 279 | |
| 280 | /* Extract platform data from DTB */ |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 281 | for(i = 0 ; i < msm_data_count; i++) { |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 282 | platform_data[i].platform_id = fdt32_to_cpu(((struct plat_id *)plat_prop)->platform_id); |
| 283 | platform_data[i].soc_rev = fdt32_to_cpu(((struct plat_id *)plat_prop)->soc_rev); |
| 284 | len_plat_id -= sizeof(struct plat_id); |
| 285 | plat_prop += sizeof(struct plat_id); |
| 286 | } |
| 287 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 288 | if (dtb_ver == DEV_TREE_VERSION_V3 && pmic_prop) { |
| 289 | /* Extract pmic data from DTB */ |
| 290 | for(i = 0 ; i < pmic_data_count; i++) { |
| 291 | pmic_data[i].pmic_version[0]= fdt32_to_cpu(((struct pmic_id *)pmic_prop)->pmic_version[0]); |
| 292 | pmic_data[i].pmic_version[1]= fdt32_to_cpu(((struct pmic_id *)pmic_prop)->pmic_version[1]); |
| 293 | pmic_data[i].pmic_version[2]= fdt32_to_cpu(((struct pmic_id *)pmic_prop)->pmic_version[2]); |
| 294 | pmic_data[i].pmic_version[3]= fdt32_to_cpu(((struct pmic_id *)pmic_prop)->pmic_version[3]); |
| 295 | len_pmic_id -= sizeof(struct pmic_id); |
| 296 | pmic_prop += sizeof(struct pmic_id); |
| 297 | } |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 298 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 299 | /* We need to merge board & platform data into dt entry structure */ |
| 300 | num_entries = msm_data_count * board_data_count * pmic_data_count; |
| 301 | } else { |
| 302 | /* We need to merge board & platform data into dt entry structure */ |
| 303 | num_entries = msm_data_count * board_data_count; |
| 304 | } |
vijay kumar | 89d36d8 | 2014-06-30 19:32:18 +0530 | [diff] [blame] | 305 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 306 | if ((((uint64_t)msm_data_count * (uint64_t)board_data_count * (uint64_t)pmic_data_count) != |
| 307 | msm_data_count * board_data_count * pmic_data_count) || |
| 308 | (((uint64_t)msm_data_count * (uint64_t)board_data_count) != msm_data_count * board_data_count)) { |
| 309 | |
| 310 | free(board_data); |
| 311 | free(platform_data); |
| 312 | if (pmic_data) |
| 313 | free(pmic_data); |
| 314 | if (model) |
| 315 | free(model); |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | dt_entry_array = (struct dt_entry*) malloc(sizeof(struct dt_entry) * num_entries); |
| 320 | ASSERT(dt_entry_array); |
| 321 | |
| 322 | /* If we have '<X>; <Y>; <Z>' as platform data & '<A>; <B>; <C>' as board data. |
| 323 | * Then dt entry should look like |
| 324 | * <X ,A >;<X, B>;<X, C>; |
| 325 | * <Y ,A >;<Y, B>;<Y, C>; |
| 326 | * <Z ,A >;<Z, B>;<Z, C>; |
| 327 | */ |
| 328 | i = 0; |
| 329 | k = 0; |
| 330 | n = 0; |
| 331 | for (i = 0; i < msm_data_count; i++) { |
| 332 | for (j = 0; j < board_data_count; j++) { |
| 333 | if (dtb_ver == DEV_TREE_VERSION_V3 && pmic_prop) { |
| 334 | for (n = 0; n < pmic_data_count; n++) { |
| 335 | dt_entry_array[k].platform_id = platform_data[i].platform_id; |
| 336 | dt_entry_array[k].soc_rev = platform_data[i].soc_rev; |
| 337 | dt_entry_array[k].variant_id = board_data[j].variant_id; |
| 338 | dt_entry_array[k].board_hw_subtype = board_data[j].platform_subtype; |
| 339 | dt_entry_array[k].pmic_rev[0]= pmic_data[n].pmic_version[0]; |
| 340 | dt_entry_array[k].pmic_rev[1]= pmic_data[n].pmic_version[1]; |
| 341 | dt_entry_array[k].pmic_rev[2]= pmic_data[n].pmic_version[2]; |
| 342 | dt_entry_array[k].pmic_rev[3]= pmic_data[n].pmic_version[3]; |
| 343 | dt_entry_array[k].offset = (uint32_t)dtb; |
| 344 | dt_entry_array[k].size = dtb_size; |
| 345 | k++; |
| 346 | } |
| 347 | |
| 348 | } else { |
| 349 | dt_entry_array[k].platform_id = platform_data[i].platform_id; |
| 350 | dt_entry_array[k].soc_rev = platform_data[i].soc_rev; |
| 351 | dt_entry_array[k].variant_id = board_data[j].variant_id; |
| 352 | dt_entry_array[k].board_hw_subtype = board_data[j].platform_subtype; |
| 353 | dt_entry_array[k].pmic_rev[0]= board_pmic_target(0); |
| 354 | dt_entry_array[k].pmic_rev[1]= board_pmic_target(1); |
| 355 | dt_entry_array[k].pmic_rev[2]= board_pmic_target(2); |
| 356 | dt_entry_array[k].pmic_rev[3]= board_pmic_target(3); |
| 357 | dt_entry_array[k].offset = (uint32_t)dtb; |
| 358 | dt_entry_array[k].size = dtb_size; |
vijay kumar | 89d36d8 | 2014-06-30 19:32:18 +0530 | [diff] [blame] | 359 | k++; |
| 360 | } |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 361 | } |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 362 | } |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 363 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 364 | for (i=0 ;i < num_entries; i++) { |
| 365 | dprintf(SPEW, "Found an appended flattened device tree (%s - %u %u %u 0x%x)\n", |
| 366 | *model ? model : "unknown", |
| 367 | dt_entry_array[i].platform_id, dt_entry_array[i].variant_id, dt_entry_array[i].board_hw_subtype, dt_entry_array[i].soc_rev); |
vijay kumar | 89d36d8 | 2014-06-30 19:32:18 +0530 | [diff] [blame] | 368 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 369 | if (platform_dt_absolute_match(&(dt_entry_array[i]), dtb_list)) { |
| 370 | dprintf(SPEW, "Device tree exact match the board: <%u %u %u 0x%x> == <%u %u %u 0x%x>\n", |
| 371 | dt_entry_array[i].platform_id, |
| 372 | dt_entry_array[i].variant_id, |
| 373 | dt_entry_array[i].soc_rev, |
| 374 | dt_entry_array[i].board_hw_subtype, |
| 375 | board_platform_id(), |
| 376 | board_hardware_id(), |
| 377 | board_hardware_subtype(), |
| 378 | board_soc_version()); |
vijay kumar | 89d36d8 | 2014-06-30 19:32:18 +0530 | [diff] [blame] | 379 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 380 | } else { |
| 381 | dprintf(SPEW, "Device tree's msm_id doesn't match the board: <%u %u %u 0x%x> != <%u %u %u 0x%x>\n", |
| 382 | dt_entry_array[i].platform_id, |
| 383 | dt_entry_array[i].variant_id, |
| 384 | dt_entry_array[i].soc_rev, |
| 385 | dt_entry_array[i].board_hw_subtype, |
| 386 | board_platform_id(), |
| 387 | board_hardware_id(), |
| 388 | board_hardware_subtype(), |
| 389 | board_soc_version()); |
Channagoud Kadabi | a4dbe33 | 2013-09-05 17:44:11 -0700 | [diff] [blame] | 390 | } |
| 391 | } |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 392 | |
| 393 | free(board_data); |
| 394 | free(platform_data); |
| 395 | if (pmic_data) |
| 396 | free(pmic_data); |
| 397 | free(dt_entry_array); |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 398 | } |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 399 | if (model) |
vijay kumar | 89d36d8 | 2014-06-30 19:32:18 +0530 | [diff] [blame] | 400 | free(model); |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 401 | return true; |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 404 | /* |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 405 | * Will relocate the DTB to the tags addr if the device tree is found and return |
| 406 | * its address |
| 407 | * |
| 408 | * Arguments: kernel - Start address of the kernel loaded in RAM |
| 409 | * tags - Start address of the tags loaded in RAM |
Channagoud Kadabi | 704cd56 | 2013-04-25 15:19:59 -0700 | [diff] [blame] | 410 | * kernel_size - Size of the kernel in bytes |
| 411 | * |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 412 | * Return Value: DTB address : If appended device tree is found |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 413 | * 'NULL' : Otherwise |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 414 | */ |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 415 | void *dev_tree_appended(void *kernel, uint32_t kernel_size, void *tags) |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 416 | { |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 417 | void *kernel_end = kernel + kernel_size; |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 418 | uint32_t app_dtb_offset = 0; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 419 | void *dtb = NULL; |
Shashank Mittal | c0f1028 | 2013-07-15 14:53:31 -0700 | [diff] [blame] | 420 | void *bestmatch_tag = NULL; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 421 | struct dt_entry *best_match_dt_entry = NULL; |
Shashank Mittal | c0f1028 | 2013-07-15 14:53:31 -0700 | [diff] [blame] | 422 | uint32_t bestmatch_tag_size; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 423 | struct dt_entry_node *dt_entry_queue = NULL; |
| 424 | struct dt_entry_node *dt_node_tmp1 = NULL; |
| 425 | struct dt_entry_node *dt_node_tmp2 = NULL; |
| 426 | |
| 427 | |
| 428 | /* Initialize the dtb entry node*/ |
| 429 | dt_entry_queue = (struct dt_entry_node *) |
| 430 | malloc(sizeof(struct dt_entry_node)); |
| 431 | |
| 432 | if (!dt_entry_queue) { |
| 433 | dprintf(CRITICAL, "Out of memory\n"); |
| 434 | return NULL; |
| 435 | } |
| 436 | list_initialize(&dt_entry_queue->node); |
| 437 | |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 438 | |
| 439 | memcpy((void*) &app_dtb_offset, (void*) (kernel + DTB_OFFSET), sizeof(uint32_t)); |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 440 | |
vijay kumar | e2a5ea8 | 2014-06-25 12:24:14 +0530 | [diff] [blame] | 441 | if (((uintptr_t)kernel + (uintptr_t)app_dtb_offset) < (uintptr_t)kernel) { |
| 442 | return NULL; |
| 443 | } |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 444 | dtb = kernel + app_dtb_offset; |
vijay kumar | e2a5ea8 | 2014-06-25 12:24:14 +0530 | [diff] [blame] | 445 | while (((uintptr_t)dtb + sizeof(struct fdt_header)) < (uintptr_t)kernel_end) { |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 446 | struct fdt_header dtb_hdr; |
| 447 | uint32_t dtb_size; |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 448 | |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 449 | /* the DTB could be unaligned, so extract the header, |
| 450 | * and operate on it separately */ |
| 451 | memcpy(&dtb_hdr, dtb, sizeof(struct fdt_header)); |
| 452 | if (fdt_check_header((const void *)&dtb_hdr) != 0 || |
vijay kumar | e2a5ea8 | 2014-06-25 12:24:14 +0530 | [diff] [blame] | 453 | ((uintptr_t)dtb + (uintptr_t)fdt_totalsize((const void *)&dtb_hdr) < (uintptr_t)dtb) || |
| 454 | ((uintptr_t)dtb + (uintptr_t)fdt_totalsize((const void *)&dtb_hdr) > (uintptr_t)kernel_end)) |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 455 | break; |
| 456 | dtb_size = fdt_totalsize(&dtb_hdr); |
| 457 | |
vijay kumar | b05eed2 | 2014-06-24 16:30:18 +0530 | [diff] [blame] | 458 | if (check_aboot_addr_range_overlap(tags, dtb_size)) { |
| 459 | dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n"); |
| 460 | return NULL; |
| 461 | } |
| 462 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 463 | dev_tree_compatible(dtb, dtb_size, dt_entry_queue); |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 464 | |
| 465 | /* goto the next device tree if any */ |
| 466 | dtb += dtb_size; |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 467 | } |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 468 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 469 | best_match_dt_entry = platform_dt_match_best(dt_entry_queue); |
| 470 | if (best_match_dt_entry){ |
| 471 | bestmatch_tag = (void *)best_match_dt_entry->offset; |
| 472 | bestmatch_tag_size = best_match_dt_entry->size; |
| 473 | dprintf(INFO, "Best match DTB tags %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n", |
| 474 | best_match_dt_entry->platform_id, best_match_dt_entry->variant_id, |
| 475 | best_match_dt_entry->board_hw_subtype, best_match_dt_entry->soc_rev, |
| 476 | best_match_dt_entry->pmic_rev[0], best_match_dt_entry->pmic_rev[1], |
| 477 | best_match_dt_entry->pmic_rev[2], best_match_dt_entry->pmic_rev[3], |
| 478 | best_match_dt_entry->offset, best_match_dt_entry->size); |
| 479 | dprintf(INFO, "Using pmic info 0x%0x/0x%x/0x%x/0x%0x for device 0x%0x/0x%x/0x%x/0x%0x\n", |
| 480 | best_match_dt_entry->pmic_rev[0], best_match_dt_entry->pmic_rev[1], |
| 481 | best_match_dt_entry->pmic_rev[2], best_match_dt_entry->pmic_rev[3], |
| 482 | board_pmic_target(0), board_pmic_target(1), |
| 483 | board_pmic_target(2), board_pmic_target(3)); |
| 484 | } |
| 485 | /* free queue's memory */ |
| 486 | list_for_every_entry(&dt_entry_queue->node, dt_node_tmp1, dt_node, node) { |
| 487 | dt_node_tmp2 = dt_node_tmp1->node.prev; |
| 488 | dt_entry_list_delete(dt_node_tmp1); |
| 489 | dt_node_tmp1 = dt_node_tmp2; |
| 490 | } |
| 491 | |
Shashank Mittal | c0f1028 | 2013-07-15 14:53:31 -0700 | [diff] [blame] | 492 | if(bestmatch_tag) { |
Shashank Mittal | c0f1028 | 2013-07-15 14:53:31 -0700 | [diff] [blame] | 493 | memcpy(tags, bestmatch_tag, bestmatch_tag_size); |
| 494 | /* clear out the old DTB magic so kernel doesn't find it */ |
| 495 | *((uint32_t *)(kernel + app_dtb_offset)) = 0; |
| 496 | return tags; |
| 497 | } |
| 498 | |
Dima Zavin | c46f838 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 499 | 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] | 500 | |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 501 | return NULL; |
Channagoud Kadabi | 11682e9 | 2013-02-28 11:21:46 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 504 | /* Returns 0 if the device tree is valid. */ |
Deepa Dinamani | 8725295 | 2013-09-09 13:58:27 -0700 | [diff] [blame] | 505 | int dev_tree_validate(struct dt_table *table, unsigned int page_size, uint32_t *dt_hdr_size) |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 506 | { |
| 507 | int dt_entry_size; |
Channagoud Kadabi | d87c277 | 2014-06-20 15:41:55 -0700 | [diff] [blame] | 508 | uint64_t hdr_size; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 509 | |
| 510 | /* Validate the device tree table header */ |
| 511 | if(table->magic != DEV_TREE_MAGIC) { |
| 512 | dprintf(CRITICAL, "ERROR: Bad magic in device tree table \n"); |
| 513 | return -1; |
| 514 | } |
| 515 | |
| 516 | if (table->version == DEV_TREE_VERSION_V1) { |
| 517 | dt_entry_size = sizeof(struct dt_entry_v1); |
| 518 | } else if (table->version == DEV_TREE_VERSION_V2) { |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 519 | dt_entry_size = sizeof(struct dt_entry_v2); |
| 520 | } else if (table->version == DEV_TREE_VERSION_V3) { |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 521 | dt_entry_size = sizeof(struct dt_entry); |
| 522 | } else { |
| 523 | dprintf(CRITICAL, "ERROR: Unsupported version (%d) in DT table \n", |
| 524 | table->version); |
| 525 | return -1; |
| 526 | } |
| 527 | |
Channagoud Kadabi | d87c277 | 2014-06-20 15:41:55 -0700 | [diff] [blame] | 528 | hdr_size = (uint64_t)table->num_entries * dt_entry_size + DEV_TREE_HEADER_SIZE; |
| 529 | |
Deepa Dinamani | 8725295 | 2013-09-09 13:58:27 -0700 | [diff] [blame] | 530 | /* Roundup to page_size. */ |
| 531 | hdr_size = ROUNDUP(hdr_size, page_size); |
| 532 | |
Channagoud Kadabi | d87c277 | 2014-06-20 15:41:55 -0700 | [diff] [blame] | 533 | if (hdr_size > UINT_MAX) |
| 534 | return -1; |
| 535 | else |
| 536 | *dt_hdr_size = hdr_size & UINT_MAX; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 541 | static int platform_dt_absolute_match(struct dt_entry *cur_dt_entry, struct dt_entry_node *dt_list) |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 542 | { |
Aparna Mallavarapu | 6eb3090 | 2014-05-13 17:03:10 +0530 | [diff] [blame] | 543 | uint32_t cur_dt_hlos_subtype; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 544 | uint32_t cur_dt_hw_platform; |
| 545 | uint32_t cur_dt_hw_subtype; |
| 546 | uint32_t cur_dt_msm_id; |
| 547 | dt_node *dt_node_tmp = NULL; |
Channagoud Kadabi | 571193a | 2014-02-05 13:58:49 -0800 | [diff] [blame] | 548 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 549 | /* Platform-id |
| 550 | * bit no |31 24|23 16|15 0| |
| 551 | * |reserved|foundry-id|msm-id| |
| 552 | */ |
| 553 | cur_dt_msm_id = (cur_dt_entry->platform_id & 0x0000ffff); |
| 554 | cur_dt_hw_platform = (cur_dt_entry->variant_id & 0x000000ff); |
Lijuan Gao | 0621b86 | 2014-09-02 10:02:52 +0800 | [diff] [blame^] | 555 | cur_dt_hw_subtype = (cur_dt_entry->board_hw_subtype & 0xff); |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 556 | |
| 557 | |
Aparna Mallavarapu | 6eb3090 | 2014-05-13 17:03:10 +0530 | [diff] [blame] | 558 | /* Determine the bits 23:8 to check the DT with the DDR Size */ |
| 559 | cur_dt_hlos_subtype = (cur_dt_entry->board_hw_subtype & 0xffff00); |
Channagoud Kadabi | 571193a | 2014-02-05 13:58:49 -0800 | [diff] [blame] | 560 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 561 | /* 1. must match the msm_id, platform_hw_id, platform_subtype and DDR size |
| 562 | * soc, board major/minor, pmic major/minor must less than board info |
| 563 | * 2. find the matched DTB then return 1 |
| 564 | * 3. otherwise return 0 |
Maria Yu | 2e2d2c2 | 2013-07-03 19:20:33 +0800 | [diff] [blame] | 565 | */ |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 566 | if((cur_dt_msm_id == (board_platform_id() & 0x0000ffff)) && |
| 567 | (cur_dt_hw_platform == board_hardware_id()) && |
| 568 | (cur_dt_hw_subtype == board_hardware_subtype()) && |
| 569 | (cur_dt_hlos_subtype == target_get_hlos_subtype()) && |
| 570 | (cur_dt_entry->soc_rev <= board_soc_version()) && |
| 571 | ((cur_dt_entry->variant_id & 0x00ffff00) <= (board_target_id() & 0x00ffff00)) && |
| 572 | ((cur_dt_entry->pmic_rev[0] & 0x00ffff00) <= (board_pmic_target(0) & 0x00ffff00)) && |
| 573 | ((cur_dt_entry->pmic_rev[1] & 0x00ffff00) <= (board_pmic_target(1) & 0x00ffff00)) && |
| 574 | ((cur_dt_entry->pmic_rev[2] & 0x00ffff00) <= (board_pmic_target(2) & 0x00ffff00)) && |
| 575 | ((cur_dt_entry->pmic_rev[3] & 0x00ffff00) <= (board_pmic_target(3) & 0x00ffff00))) { |
Maria Yu | 2e2d2c2 | 2013-07-03 19:20:33 +0800 | [diff] [blame] | 576 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 577 | dt_node_tmp = dt_entry_list_init(); |
| 578 | memcpy((char*)dt_node_tmp->dt_entry_m,(char*)cur_dt_entry, sizeof(struct dt_entry)); |
| 579 | |
| 580 | dprintf(SPEW, "Add DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n", |
| 581 | dt_node_tmp->dt_entry_m->platform_id, dt_node_tmp->dt_entry_m->variant_id, |
| 582 | dt_node_tmp->dt_entry_m->board_hw_subtype, dt_node_tmp->dt_entry_m->soc_rev, |
| 583 | dt_node_tmp->dt_entry_m->pmic_rev[0], dt_node_tmp->dt_entry_m->pmic_rev[1], |
| 584 | dt_node_tmp->dt_entry_m->pmic_rev[2], dt_node_tmp->dt_entry_m->pmic_rev[3], |
| 585 | dt_node_tmp->dt_entry_m->offset, dt_node_tmp->dt_entry_m->size); |
| 586 | |
| 587 | insert_dt_entry_in_queue(dt_list, dt_node_tmp); |
| 588 | return 1; |
| 589 | } |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | static int platform_dt_absolute_compat_match(struct dt_entry_node *dt_list, uint32_t dtb_info) { |
| 594 | struct dt_entry_node *dt_node_tmp1 = NULL; |
| 595 | struct dt_entry_node *dt_node_tmp2 = NULL; |
| 596 | uint32_t current_info = 0; |
| 597 | uint32_t board_info = 0; |
| 598 | uint32_t best_info = 0; |
| 599 | uint32_t current_pmic_model[4] = {0, 0, 0, 0}; |
| 600 | uint32_t board_pmic_model[4] = {0, 0, 0, 0}; |
| 601 | uint32_t best_pmic_model[4] = {0, 0, 0, 0}; |
| 602 | uint32_t delete_current_dt = 0; |
| 603 | uint32_t i; |
| 604 | |
| 605 | /* start to select the exact entry |
| 606 | * default to exact match 0, if find current DTB entry info is the same as board info, |
| 607 | * then exact match board info. |
| 608 | */ |
| 609 | list_for_every_entry(&dt_list->node, dt_node_tmp1, dt_node, node) { |
| 610 | if (!dt_node_tmp1){ |
| 611 | dprintf(SPEW, "Current node is the end\n"); |
| 612 | break; |
| 613 | } |
| 614 | switch(dtb_info) { |
| 615 | case DTB_FOUNDRY: |
| 616 | current_info = ((dt_node_tmp1->dt_entry_m->platform_id) & 0x00ff0000); |
| 617 | board_info = board_foundry_id(); |
| 618 | break; |
| 619 | case DTB_PMIC_MODEL: |
| 620 | for (i = 0; i < 4; i++) { |
| 621 | current_pmic_model[i] = (dt_node_tmp1->dt_entry_m->pmic_rev[i] & 0xff); |
| 622 | board_pmic_model[i] = (board_pmic_target(i) & 0xff); |
| 623 | } |
| 624 | break; |
| 625 | default: |
| 626 | dprintf(CRITICAL, "ERROR: Unsupported version (%d) in dt node check \n", |
| 627 | dtb_info); |
Maria Yu | ca51ee2 | 2013-06-27 21:45:24 +0800 | [diff] [blame] | 628 | return 0; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | if (dtb_info == DTB_PMIC_MODEL) { |
| 632 | if ((current_pmic_model[0] == board_pmic_model[0]) && |
| 633 | (current_pmic_model[1] == board_pmic_model[1]) && |
| 634 | (current_pmic_model[2] == board_pmic_model[2]) && |
| 635 | (current_pmic_model[3] == board_pmic_model[3])) { |
| 636 | |
| 637 | for (i = 0; i < 4; i++) { |
| 638 | best_pmic_model[i] = current_pmic_model[i]; |
| 639 | } |
| 640 | break; |
| 641 | } |
| 642 | } else { |
| 643 | if (current_info == board_info) { |
| 644 | best_info = current_info; |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | list_for_every_entry(&dt_list->node, dt_node_tmp1, dt_node, node) { |
| 651 | if (!dt_node_tmp1){ |
| 652 | dprintf(SPEW, "Current node is the end\n"); |
| 653 | break; |
| 654 | } |
| 655 | switch(dtb_info) { |
| 656 | case DTB_FOUNDRY: |
| 657 | current_info = ((dt_node_tmp1->dt_entry_m->platform_id) & 0x00ff0000); |
| 658 | break; |
| 659 | case DTB_PMIC_MODEL: |
| 660 | for (i = 0; i < 4; i++) { |
| 661 | current_pmic_model[i] = (dt_node_tmp1->dt_entry_m->pmic_rev[i] & 0xff); |
| 662 | } |
| 663 | break; |
| 664 | default: |
| 665 | dprintf(CRITICAL, "ERROR: Unsupported version (%d) in dt node check \n", |
| 666 | dtb_info); |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | if (dtb_info == DTB_PMIC_MODEL) { |
| 671 | if ((current_pmic_model[0] != best_pmic_model[0]) || |
| 672 | (current_pmic_model[1] != best_pmic_model[1]) || |
| 673 | (current_pmic_model[2] != best_pmic_model[2]) || |
| 674 | (current_pmic_model[3] != best_pmic_model[3])) { |
| 675 | |
| 676 | delete_current_dt = 1; |
| 677 | } |
| 678 | } else { |
| 679 | if (current_info != best_info) { |
| 680 | delete_current_dt = 1; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | if (delete_current_dt) { |
| 685 | dprintf(SPEW, "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n", |
| 686 | dt_node_tmp1->dt_entry_m->platform_id, dt_node_tmp1->dt_entry_m->variant_id, |
| 687 | dt_node_tmp1->dt_entry_m->board_hw_subtype, dt_node_tmp1->dt_entry_m->soc_rev, |
| 688 | dt_node_tmp1->dt_entry_m->pmic_rev[0], dt_node_tmp1->dt_entry_m->pmic_rev[1], |
| 689 | dt_node_tmp1->dt_entry_m->pmic_rev[2], dt_node_tmp1->dt_entry_m->pmic_rev[3], |
| 690 | dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size); |
| 691 | |
| 692 | dt_node_tmp2 = dt_node_tmp1->node.prev; |
| 693 | dt_entry_list_delete(dt_node_tmp1); |
| 694 | dt_node_tmp1 = dt_node_tmp2; |
| 695 | delete_current_dt = 0; |
Maria Yu | ca51ee2 | 2013-06-27 21:45:24 +0800 | [diff] [blame] | 696 | } |
| 697 | } |
Maria Yu | ca51ee2 | 2013-06-27 21:45:24 +0800 | [diff] [blame] | 698 | |
Maria Yu | 2e2d2c2 | 2013-07-03 19:20:33 +0800 | [diff] [blame] | 699 | return 1; |
Maria Yu | ca51ee2 | 2013-06-27 21:45:24 +0800 | [diff] [blame] | 700 | } |
| 701 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 702 | static int update_dtb_entry_node(struct dt_entry_node *dt_list, uint32_t dtb_info) { |
| 703 | struct dt_entry_node *dt_node_tmp1 = NULL; |
| 704 | struct dt_entry_node *dt_node_tmp2 = NULL; |
| 705 | uint32_t current_info = 0; |
| 706 | uint32_t board_info = 0; |
| 707 | uint32_t best_info = 0; |
| 708 | |
| 709 | /* start to select the best entry*/ |
| 710 | list_for_every_entry(&dt_list->node, dt_node_tmp1, dt_node, node) { |
| 711 | if (!dt_node_tmp1){ |
| 712 | dprintf(SPEW, "Current node is the end\n"); |
| 713 | break; |
| 714 | } |
| 715 | switch(dtb_info) { |
| 716 | case DTB_SOC: |
| 717 | current_info = dt_node_tmp1->dt_entry_m->soc_rev; |
| 718 | board_info = board_soc_version(); |
| 719 | break; |
| 720 | case DTB_MAJOR_MINOR: |
| 721 | current_info = ((dt_node_tmp1->dt_entry_m->variant_id) & 0x00ffff00); |
| 722 | board_info = (board_target_id() & 0x00ffff00); |
| 723 | break; |
| 724 | case DTB_PMIC0: |
| 725 | current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[0]) & 0x00ffff00); |
| 726 | board_info = (board_pmic_target(0) & 0x00ffff00); |
| 727 | break; |
| 728 | case DTB_PMIC1: |
| 729 | current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[1]) & 0x00ffff00); |
| 730 | board_info = (board_pmic_target(1) & 0x00ffff00); |
| 731 | break; |
| 732 | case DTB_PMIC2: |
| 733 | current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[2]) & 0x00ffff00); |
| 734 | board_info = (board_pmic_target(2) & 0x00ffff00); |
| 735 | break; |
| 736 | case DTB_PMIC3: |
| 737 | current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[3]) & 0x00ffff00); |
| 738 | board_info = (board_pmic_target(3) & 0x00ffff00); |
| 739 | break; |
| 740 | default: |
| 741 | dprintf(CRITICAL, "ERROR: Unsupported version (%d) in dt node check \n", |
| 742 | dtb_info); |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | if (current_info == board_info) { |
| 747 | best_info = current_info; |
| 748 | break; |
| 749 | } |
| 750 | if ((current_info < board_info) && (current_info > best_info)) { |
| 751 | best_info = current_info; |
| 752 | } |
| 753 | if (current_info < best_info) { |
| 754 | dprintf(SPEW, "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n", |
| 755 | dt_node_tmp1->dt_entry_m->platform_id, dt_node_tmp1->dt_entry_m->variant_id, |
| 756 | dt_node_tmp1->dt_entry_m->board_hw_subtype, dt_node_tmp1->dt_entry_m->soc_rev, |
| 757 | dt_node_tmp1->dt_entry_m->pmic_rev[0], dt_node_tmp1->dt_entry_m->pmic_rev[1], |
| 758 | dt_node_tmp1->dt_entry_m->pmic_rev[2], dt_node_tmp1->dt_entry_m->pmic_rev[3], |
| 759 | dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size); |
| 760 | |
| 761 | dt_node_tmp2 = dt_node_tmp1->node.prev; |
| 762 | dt_entry_list_delete(dt_node_tmp1); |
| 763 | dt_node_tmp1 = dt_node_tmp2; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | list_for_every_entry(&dt_list->node, dt_node_tmp1, dt_node, node) { |
| 768 | if (!dt_node_tmp1){ |
| 769 | dprintf(SPEW, "Current node is the end\n"); |
| 770 | break; |
| 771 | } |
| 772 | switch(dtb_info) { |
| 773 | case DTB_SOC: |
| 774 | current_info = dt_node_tmp1->dt_entry_m->soc_rev; |
| 775 | break; |
| 776 | case DTB_MAJOR_MINOR: |
| 777 | current_info = ((dt_node_tmp1->dt_entry_m->variant_id) & 0x00ffff00); |
| 778 | break; |
| 779 | case DTB_PMIC0: |
| 780 | current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[0]) & 0x00ffff00); |
| 781 | break; |
| 782 | case DTB_PMIC1: |
| 783 | current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[1]) & 0x00ffff00); |
| 784 | break; |
| 785 | case DTB_PMIC2: |
| 786 | current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[2]) & 0x00ffff00); |
| 787 | break; |
| 788 | case DTB_PMIC3: |
| 789 | current_info = ((dt_node_tmp1->dt_entry_m->pmic_rev[3]) & 0x00ffff00); |
| 790 | break; |
| 791 | default: |
| 792 | dprintf(CRITICAL, "ERROR: Unsupported version (%d) in dt node check \n", |
| 793 | dtb_info); |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | if (current_info != best_info) { |
| 798 | dprintf(SPEW, "Delete don't fit DTB entry %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n", |
| 799 | dt_node_tmp1->dt_entry_m->platform_id, dt_node_tmp1->dt_entry_m->variant_id, |
| 800 | dt_node_tmp1->dt_entry_m->board_hw_subtype, dt_node_tmp1->dt_entry_m->soc_rev, |
| 801 | dt_node_tmp1->dt_entry_m->pmic_rev[0], dt_node_tmp1->dt_entry_m->pmic_rev[1], |
| 802 | dt_node_tmp1->dt_entry_m->pmic_rev[2], dt_node_tmp1->dt_entry_m->pmic_rev[3], |
| 803 | dt_node_tmp1->dt_entry_m->offset, dt_node_tmp1->dt_entry_m->size); |
| 804 | |
| 805 | dt_node_tmp2 = dt_node_tmp1->node.prev; |
| 806 | dt_entry_list_delete(dt_node_tmp1); |
| 807 | dt_node_tmp1 = dt_node_tmp2; |
| 808 | } |
| 809 | } |
| 810 | return 1; |
| 811 | } |
| 812 | |
| 813 | static struct dt_entry *platform_dt_match_best(struct dt_entry_node *dt_list) |
| 814 | { |
| 815 | struct dt_entry_node *dt_node_tmp1 = NULL; |
| 816 | |
| 817 | /* check Foundry id |
| 818 | * the foundry id must exact match board founddry id, this is compatibility check, |
| 819 | * if couldn't find the exact match from DTB, will exact match 0x0. |
| 820 | */ |
| 821 | if (!platform_dt_absolute_compat_match(dt_list, DTB_FOUNDRY)) |
| 822 | return NULL; |
| 823 | |
| 824 | /* check PMIC model |
| 825 | * the PMIC model must exact match board PMIC model, this is compatibility check, |
| 826 | * if couldn't find the exact match from DTB, will exact match 0x0. |
| 827 | */ |
| 828 | if (!platform_dt_absolute_compat_match(dt_list, DTB_PMIC_MODEL)) |
| 829 | return NULL; |
| 830 | |
| 831 | /* check soc version |
| 832 | * the suitable soc version must less than or equal to board soc version |
| 833 | */ |
| 834 | if (!update_dtb_entry_node(dt_list, DTB_SOC)) |
| 835 | return NULL; |
| 836 | |
| 837 | /*check major and minor version |
| 838 | * the suitable major&minor version must less than or equal to board major&minor version |
| 839 | */ |
| 840 | if (!update_dtb_entry_node(dt_list, DTB_MAJOR_MINOR)) |
| 841 | return NULL; |
| 842 | |
| 843 | /*check pmic info |
| 844 | * the suitable pmic major&minor info must less than or equal to board pmic major&minor version |
| 845 | */ |
| 846 | if (!update_dtb_entry_node(dt_list, DTB_PMIC0)) |
| 847 | return NULL; |
| 848 | if (!update_dtb_entry_node(dt_list, DTB_PMIC1)) |
| 849 | return NULL; |
| 850 | if (!update_dtb_entry_node(dt_list, DTB_PMIC2)) |
| 851 | return NULL; |
| 852 | if (!update_dtb_entry_node(dt_list, DTB_PMIC3)) |
| 853 | return NULL; |
| 854 | |
| 855 | list_for_every_entry(&dt_list->node, dt_node_tmp1, dt_node, node) { |
| 856 | if (!dt_node_tmp1) { |
| 857 | dprintf(CRITICAL, "ERROR: Couldn't find the suitable DTB!\n"); |
| 858 | return NULL; |
| 859 | } |
| 860 | if (dt_node_tmp1->dt_entry_m) |
| 861 | return dt_node_tmp1->dt_entry_m; |
| 862 | } |
| 863 | |
| 864 | return NULL; |
| 865 | } |
| 866 | |
| 867 | /* Function to obtain the index information for the correct device tree |
| 868 | * based on the platform data. |
| 869 | * If a matching device tree is found, the information is returned in the |
| 870 | * "dt_entry_info" out parameter and a function value of 0 is returned, otherwise |
| 871 | * a non-zero function value is returned. |
| 872 | */ |
| 873 | int dev_tree_get_entry_info(struct dt_table *table, struct dt_entry *dt_entry_info) |
Maria Yu | ca51ee2 | 2013-06-27 21:45:24 +0800 | [diff] [blame] | 874 | { |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 875 | uint32_t i; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 876 | unsigned char *table_ptr = NULL; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 877 | struct dt_entry dt_entry_buf_1; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 878 | struct dt_entry *cur_dt_entry = NULL; |
| 879 | struct dt_entry *best_match_dt_entry = NULL; |
| 880 | struct dt_entry_v1 *dt_entry_v1 = NULL; |
| 881 | struct dt_entry_v2 *dt_entry_v2 = NULL; |
| 882 | struct dt_entry_node *dt_entry_queue = NULL; |
| 883 | struct dt_entry_node *dt_node_tmp1 = NULL; |
| 884 | struct dt_entry_node *dt_node_tmp2 = NULL; |
Maria Yu | 2e2d2c2 | 2013-07-03 19:20:33 +0800 | [diff] [blame] | 885 | uint32_t found = 0; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 886 | |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 887 | if (!dt_entry_info) { |
| 888 | dprintf(CRITICAL, "ERROR: Bad parameter passed to %s \n", |
| 889 | __func__); |
| 890 | return -1; |
| 891 | } |
| 892 | |
| 893 | table_ptr = (unsigned char *)table + DEV_TREE_HEADER_SIZE; |
| 894 | cur_dt_entry = &dt_entry_buf_1; |
| 895 | best_match_dt_entry = NULL; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 896 | dt_entry_queue = (struct dt_entry_node *) |
| 897 | malloc(sizeof(struct dt_entry_node)); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 898 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 899 | if (!dt_entry_queue) { |
| 900 | dprintf(CRITICAL, "Out of memory\n"); |
| 901 | return -1; |
| 902 | } |
| 903 | |
| 904 | list_initialize(&dt_entry_queue->node); |
| 905 | dprintf(INFO, "DTB Total entry: %d, DTB version: %d\n", table->num_entries, table->version); |
Maria Yu | 2e2d2c2 | 2013-07-03 19:20:33 +0800 | [diff] [blame] | 906 | for(i = 0; found == 0 && i < table->num_entries; i++) |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 907 | { |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 908 | memset(cur_dt_entry, 0, sizeof(struct dt_entry)); |
| 909 | switch(table->version) { |
| 910 | case DEV_TREE_VERSION_V1: |
| 911 | dt_entry_v1 = (struct dt_entry_v1 *)table_ptr; |
| 912 | cur_dt_entry->platform_id = dt_entry_v1->platform_id; |
| 913 | cur_dt_entry->variant_id = dt_entry_v1->variant_id; |
| 914 | cur_dt_entry->soc_rev = dt_entry_v1->soc_rev; |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 915 | cur_dt_entry->board_hw_subtype = (dt_entry_v1->variant_id >> 0x18); |
| 916 | cur_dt_entry->pmic_rev[0] = board_pmic_target(0); |
| 917 | cur_dt_entry->pmic_rev[1] = board_pmic_target(1); |
| 918 | cur_dt_entry->pmic_rev[2] = board_pmic_target(2); |
| 919 | cur_dt_entry->pmic_rev[3] = board_pmic_target(3); |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 920 | cur_dt_entry->offset = dt_entry_v1->offset; |
| 921 | cur_dt_entry->size = dt_entry_v1->size; |
| 922 | table_ptr += sizeof(struct dt_entry_v1); |
| 923 | break; |
| 924 | case DEV_TREE_VERSION_V2: |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 925 | dt_entry_v2 = (struct dt_entry_v2*)table_ptr; |
| 926 | cur_dt_entry->platform_id = dt_entry_v2->platform_id; |
| 927 | cur_dt_entry->variant_id = dt_entry_v2->variant_id; |
| 928 | cur_dt_entry->soc_rev = dt_entry_v2->soc_rev; |
| 929 | /* For V2 version of DTBs we have platform version field as part |
| 930 | * of variant ID, in such case the subtype will be mentioned as 0x0 |
| 931 | * As the qcom, board-id = <0xSSPMPmPH, 0x0> |
| 932 | * SS -- Subtype |
| 933 | * PM -- Platform major version |
| 934 | * Pm -- Platform minor version |
| 935 | * PH -- Platform hardware CDP/MTP |
| 936 | * In such case to make it compatible with LK algorithm move the subtype |
| 937 | * from variant_id to subtype field |
| 938 | */ |
| 939 | if (dt_entry_v2->board_hw_subtype == 0) |
| 940 | cur_dt_entry->board_hw_subtype = (cur_dt_entry->variant_id >> 0x18); |
| 941 | else |
| 942 | cur_dt_entry->board_hw_subtype = dt_entry_v2->board_hw_subtype; |
| 943 | cur_dt_entry->pmic_rev[0] = board_pmic_target(0); |
| 944 | cur_dt_entry->pmic_rev[1] = board_pmic_target(1); |
| 945 | cur_dt_entry->pmic_rev[2] = board_pmic_target(2); |
| 946 | cur_dt_entry->pmic_rev[3] = board_pmic_target(3); |
| 947 | cur_dt_entry->offset = dt_entry_v2->offset; |
| 948 | cur_dt_entry->size = dt_entry_v2->size; |
| 949 | table_ptr += sizeof(struct dt_entry_v2); |
| 950 | break; |
| 951 | case DEV_TREE_VERSION_V3: |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 952 | memcpy(cur_dt_entry, (struct dt_entry *)table_ptr, |
| 953 | sizeof(struct dt_entry)); |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 954 | /* For V3 version of DTBs we have platform version field as part |
| 955 | * of variant ID, in such case the subtype will be mentioned as 0x0 |
| 956 | * As the qcom, board-id = <0xSSPMPmPH, 0x0> |
| 957 | * SS -- Subtype |
| 958 | * PM -- Platform major version |
| 959 | * Pm -- Platform minor version |
| 960 | * PH -- Platform hardware CDP/MTP |
| 961 | * In such case to make it compatible with LK algorithm move the subtype |
| 962 | * from variant_id to subtype field |
| 963 | */ |
| 964 | if (cur_dt_entry->board_hw_subtype == 0) |
| 965 | cur_dt_entry->board_hw_subtype = (cur_dt_entry->variant_id >> 0x18); |
| 966 | |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 967 | table_ptr += sizeof(struct dt_entry); |
| 968 | break; |
| 969 | default: |
| 970 | dprintf(CRITICAL, "ERROR: Unsupported version (%d) in DT table \n", |
| 971 | table->version); |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 972 | free(dt_entry_queue); |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 973 | return -1; |
| 974 | } |
| 975 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 976 | /* DTBs must match the platform_id, platform_hw_id, platform_subtype and DDR size. |
| 977 | * The satisfactory DTBs are stored in dt_entry_queue |
| 978 | */ |
| 979 | platform_dt_absolute_match(cur_dt_entry, dt_entry_queue); |
Channagoud Kadabi | afd62bf | 2013-01-08 20:32:52 -0800 | [diff] [blame] | 980 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 981 | } |
| 982 | best_match_dt_entry = platform_dt_match_best(dt_entry_queue); |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 983 | if (best_match_dt_entry) { |
| 984 | *dt_entry_info = *best_match_dt_entry; |
Maria Yu | 2e2d2c2 | 2013-07-03 19:20:33 +0800 | [diff] [blame] | 985 | found = 1; |
| 986 | } |
| 987 | |
| 988 | if (found != 0) { |
Sundarajan Srinivasan | 763c0db | 2014-05-20 17:08:36 -0700 | [diff] [blame] | 989 | dprintf(INFO, "Using DTB entry 0x%08x/%08x/0x%08x/%u for device 0x%08x/%08x/0x%08x/%u\n", |
David Ng | 618293a | 2013-06-25 12:29:03 -0700 | [diff] [blame] | 990 | dt_entry_info->platform_id, dt_entry_info->soc_rev, |
| 991 | dt_entry_info->variant_id, dt_entry_info->board_hw_subtype, |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 992 | board_platform_id(), board_soc_version(), |
| 993 | board_target_id(), board_hardware_subtype()); |
| 994 | if (dt_entry_info->pmic_rev[0] == 0 && dt_entry_info->pmic_rev[0] == 0 && |
| 995 | dt_entry_info->pmic_rev[0] == 0 && dt_entry_info->pmic_rev[0] == 0) { |
| 996 | dprintf(SPEW, "No maintain pmic info in DTB, device pmic info is 0x%0x/0x%x/0x%x/0x%0x\n", |
| 997 | board_pmic_target(0), board_pmic_target(1), |
| 998 | board_pmic_target(2), board_pmic_target(3)); |
| 999 | } else { |
| 1000 | dprintf(INFO, "Using pmic info 0x%0x/0x%x/0x%x/0x%0x for device 0x%0x/0x%x/0x%x/0x%0x\n", |
| 1001 | dt_entry_info->pmic_rev[0], dt_entry_info->pmic_rev[1], |
| 1002 | dt_entry_info->pmic_rev[2], dt_entry_info->pmic_rev[3], |
| 1003 | board_pmic_target(0), board_pmic_target(1), |
| 1004 | board_pmic_target(2), board_pmic_target(3)); |
| 1005 | } |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1006 | return 0; |
Channagoud Kadabi | afd62bf | 2013-01-08 20:32:52 -0800 | [diff] [blame] | 1007 | } |
| 1008 | |
Lijuan Gao | 9f15286 | 2014-08-18 13:45:24 +0800 | [diff] [blame] | 1009 | dprintf(CRITICAL, "ERROR: Unable to find suitable device tree for device (%u/0x%08x/0x%08x/%u)\n", |
| 1010 | board_platform_id(), board_soc_version(), |
| 1011 | board_target_id(), board_hardware_subtype()); |
| 1012 | |
| 1013 | list_for_every_entry(&dt_entry_queue->node, dt_node_tmp1, dt_node, node) { |
| 1014 | /* free node memory */ |
| 1015 | dt_node_tmp2 = dt_node_tmp1->node.prev; |
| 1016 | dt_entry_list_delete(dt_node_tmp1); |
| 1017 | dt_node_tmp1 = dt_node_tmp2; |
| 1018 | } |
| 1019 | free(dt_entry_queue); |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1020 | return -1; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1023 | /* Function to add the first RAM partition info to the device tree. |
| 1024 | * Note: The function replaces the reg property in the "/memory" node |
| 1025 | * with the addr and size provided. |
| 1026 | */ |
| 1027 | int dev_tree_add_first_mem_info(uint32_t *fdt, uint32_t offset, uint32_t addr, uint32_t size) |
| 1028 | { |
| 1029 | int ret; |
| 1030 | |
| 1031 | ret = fdt_setprop_u32(fdt, offset, "reg", addr); |
| 1032 | |
| 1033 | if (ret) |
| 1034 | { |
| 1035 | dprintf(CRITICAL, "Failed to add the memory information addr: %d\n", |
| 1036 | ret); |
| 1037 | } |
| 1038 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1039 | ret = fdt_appendprop_u32(fdt, offset, "reg", size); |
| 1040 | |
| 1041 | if (ret) |
| 1042 | { |
| 1043 | dprintf(CRITICAL, "Failed to add the memory information size: %d\n", |
| 1044 | ret); |
| 1045 | } |
| 1046 | |
| 1047 | return ret; |
| 1048 | } |
| 1049 | |
Sundarajan Srinivasan | 44de434 | 2013-07-08 14:47:13 -0700 | [diff] [blame] | 1050 | static int dev_tree_query_memory_cell_sizes(void *fdt, struct dt_mem_node_info *mem_node, uint32_t mem_node_offset) |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1051 | { |
Sundarajan Srinivasan | 44de434 | 2013-07-08 14:47:13 -0700 | [diff] [blame] | 1052 | int len; |
| 1053 | uint32_t *valp; |
| 1054 | int ret; |
| 1055 | uint32_t offset; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1056 | |
Sundarajan Srinivasan | 44de434 | 2013-07-08 14:47:13 -0700 | [diff] [blame] | 1057 | mem_node->offset = mem_node_offset; |
| 1058 | |
| 1059 | /* Get offset of the root node */ |
| 1060 | ret = fdt_path_offset(fdt, "/"); |
| 1061 | if (ret < 0) |
| 1062 | { |
| 1063 | dprintf(CRITICAL, "Could not find memory node.\n"); |
| 1064 | return ret; |
| 1065 | } |
| 1066 | |
| 1067 | offset = ret; |
| 1068 | |
| 1069 | /* Find the #address-cells size. */ |
| 1070 | valp = (uint32_t*)fdt_getprop(fdt, offset, "#address-cells", &len); |
Lijuan Gao | ae0927b | 2014-07-18 17:16:16 +0800 | [diff] [blame] | 1071 | if (len <= 0 || !valp) |
Sundarajan Srinivasan | 44de434 | 2013-07-08 14:47:13 -0700 | [diff] [blame] | 1072 | { |
| 1073 | if (len == -FDT_ERR_NOTFOUND) |
| 1074 | { |
| 1075 | /* Property not found. |
| 1076 | * Assume standard sizes. |
| 1077 | */ |
| 1078 | mem_node->addr_cell_size = 2; |
| 1079 | dprintf(CRITICAL, "Using default #addr_cell_size: %u\n", mem_node->addr_cell_size); |
| 1080 | } |
| 1081 | else |
| 1082 | { |
| 1083 | dprintf(CRITICAL, "Error finding the #address-cells property\n"); |
| 1084 | return len; |
| 1085 | } |
| 1086 | } |
| 1087 | else |
| 1088 | mem_node->addr_cell_size = fdt32_to_cpu(*valp); |
| 1089 | |
| 1090 | /* Find the #size-cells size. */ |
| 1091 | valp = (uint32_t*)fdt_getprop(fdt, offset, "#size-cells", &len); |
Lijuan Gao | ae0927b | 2014-07-18 17:16:16 +0800 | [diff] [blame] | 1092 | if (len <= 0 || !valp) |
Sundarajan Srinivasan | 44de434 | 2013-07-08 14:47:13 -0700 | [diff] [blame] | 1093 | { |
| 1094 | if (len == -FDT_ERR_NOTFOUND) |
| 1095 | { |
| 1096 | /* Property not found. |
| 1097 | * Assume standard sizes. |
| 1098 | */ |
| 1099 | mem_node->size_cell_size = 1; |
| 1100 | dprintf(CRITICAL, "Using default #size_cell_size: %u\n", mem_node->size_cell_size); |
| 1101 | } |
| 1102 | else |
| 1103 | { |
| 1104 | dprintf(CRITICAL, "Error finding the #size-cells property\n"); |
| 1105 | return len; |
| 1106 | } |
| 1107 | } |
| 1108 | else |
| 1109 | mem_node->size_cell_size = fdt32_to_cpu(*valp); |
| 1110 | |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | static void dev_tree_update_memory_node(uint32_t offset) |
| 1115 | { |
| 1116 | mem_node.offset = offset; |
| 1117 | mem_node.addr_cell_size = 1; |
| 1118 | mem_node.size_cell_size = 1; |
| 1119 | } |
| 1120 | |
| 1121 | /* Function to add the subsequent RAM partition info to the device tree. */ |
| 1122 | int dev_tree_add_mem_info(void *fdt, uint32_t offset, uint64_t addr, uint64_t size) |
| 1123 | { |
| 1124 | int ret = 0; |
| 1125 | |
| 1126 | if(smem_get_ram_ptable_version() >= 1) |
| 1127 | { |
| 1128 | ret = dev_tree_query_memory_cell_sizes(fdt, &mem_node, offset); |
| 1129 | if (ret < 0) |
| 1130 | { |
| 1131 | dprintf(CRITICAL, "Could not find #address-cells and #size-cells properties: ret %d\n", ret); |
| 1132 | return ret; |
| 1133 | } |
| 1134 | |
| 1135 | } |
| 1136 | else |
| 1137 | { |
| 1138 | dev_tree_update_memory_node(offset); |
| 1139 | } |
| 1140 | |
| 1141 | if (!(mem_node.mem_info_cnt)) |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1142 | { |
| 1143 | /* Replace any other reg prop in the memory node. */ |
Sundarajan Srinivasan | 44de434 | 2013-07-08 14:47:13 -0700 | [diff] [blame] | 1144 | |
| 1145 | /* cell_size is the number of 32 bit words used to represent an address/length in the device tree. |
| 1146 | * memory node in DT can be either 32-bit(cell-size = 1) or 64-bit(cell-size = 2).So when updating |
| 1147 | * the memory node in the device tree, we write one word or two words based on cell_size = 1 or 2. |
| 1148 | */ |
| 1149 | |
| 1150 | if(mem_node.addr_cell_size == 2) |
| 1151 | { |
| 1152 | ret = fdt_setprop_u32(fdt, mem_node.offset, "reg", addr >> 32); |
| 1153 | if(ret) |
| 1154 | { |
| 1155 | dprintf(CRITICAL, "ERROR: Could not set prop reg for memory node\n"); |
| 1156 | return ret; |
| 1157 | } |
| 1158 | |
| 1159 | ret = fdt_appendprop_u32(fdt, mem_node.offset, "reg", (uint32_t)addr); |
| 1160 | if(ret) |
| 1161 | { |
| 1162 | dprintf(CRITICAL, "ERROR: Could not append prop reg for memory node\n"); |
| 1163 | return ret; |
| 1164 | } |
| 1165 | } |
| 1166 | else |
| 1167 | { |
| 1168 | ret = fdt_setprop_u32(fdt, mem_node.offset, "reg", (uint32_t)addr); |
| 1169 | if(ret) |
| 1170 | { |
| 1171 | dprintf(CRITICAL, "ERROR: Could not set prop reg for memory node\n"); |
| 1172 | return ret; |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | mem_node.mem_info_cnt = 1; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1177 | } |
| 1178 | else |
| 1179 | { |
| 1180 | /* Append the mem info to the reg prop for subsequent nodes. */ |
Sundarajan Srinivasan | 44de434 | 2013-07-08 14:47:13 -0700 | [diff] [blame] | 1181 | if(mem_node.addr_cell_size == 2) |
| 1182 | { |
| 1183 | ret = fdt_appendprop_u32(fdt, mem_node.offset, "reg", addr >> 32); |
| 1184 | if(ret) |
| 1185 | { |
| 1186 | dprintf(CRITICAL, "ERROR: Could not append prop reg for memory node\n"); |
| 1187 | return ret; |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | ret = fdt_appendprop_u32(fdt, mem_node.offset, "reg", (uint32_t)addr); |
| 1192 | if(ret) |
| 1193 | { |
| 1194 | dprintf(CRITICAL, "ERROR: Could not append prop reg for memory node\n"); |
| 1195 | return ret; |
| 1196 | } |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
Sundarajan Srinivasan | 44de434 | 2013-07-08 14:47:13 -0700 | [diff] [blame] | 1199 | if(mem_node.size_cell_size == 2) |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1200 | { |
Sundarajan Srinivasan | 44de434 | 2013-07-08 14:47:13 -0700 | [diff] [blame] | 1201 | ret = fdt_appendprop_u32(fdt, mem_node.offset, "reg", size>>32); |
| 1202 | if(ret) |
| 1203 | { |
| 1204 | dprintf(CRITICAL, "ERROR: Could not append prop reg for memory node\n"); |
| 1205 | return ret; |
| 1206 | } |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Sundarajan Srinivasan | 44de434 | 2013-07-08 14:47:13 -0700 | [diff] [blame] | 1209 | ret = fdt_appendprop_u32(fdt, mem_node.offset, "reg", (uint32_t)size); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1210 | |
| 1211 | if (ret) |
| 1212 | { |
| 1213 | dprintf(CRITICAL, "Failed to add the memory information size: %d\n", |
| 1214 | ret); |
Sundarajan Srinivasan | 44de434 | 2013-07-08 14:47:13 -0700 | [diff] [blame] | 1215 | return ret; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | return ret; |
| 1219 | } |
| 1220 | |
| 1221 | /* Top level function that updates the device tree. */ |
| 1222 | int update_device_tree(void *fdt, const char *cmdline, |
| 1223 | void *ramdisk, uint32_t ramdisk_size) |
| 1224 | { |
| 1225 | int ret = 0; |
| 1226 | uint32_t offset; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1227 | |
| 1228 | /* Check the device tree header */ |
| 1229 | ret = fdt_check_header(fdt); |
| 1230 | if (ret) |
| 1231 | { |
| 1232 | dprintf(CRITICAL, "Invalid device tree header \n"); |
| 1233 | return ret; |
| 1234 | } |
| 1235 | |
Deepa Dinamani | 1c97073 | 2013-04-19 14:23:01 -0700 | [diff] [blame] | 1236 | /* Add padding to make space for new nodes and properties. */ |
| 1237 | ret = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + DTB_PAD_SIZE); |
| 1238 | if (ret!= 0) |
| 1239 | { |
| 1240 | dprintf(CRITICAL, "Failed to move/resize dtb buffer: %d\n", ret); |
| 1241 | return ret; |
| 1242 | } |
| 1243 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1244 | /* Get offset of the memory node */ |
| 1245 | ret = fdt_path_offset(fdt, "/memory"); |
| 1246 | if (ret < 0) |
| 1247 | { |
| 1248 | dprintf(CRITICAL, "Could not find memory node.\n"); |
| 1249 | return ret; |
| 1250 | } |
| 1251 | |
| 1252 | offset = ret; |
| 1253 | |
| 1254 | ret = target_dev_tree_mem(fdt, offset); |
| 1255 | if(ret) |
| 1256 | { |
| 1257 | dprintf(CRITICAL, "ERROR: Cannot update memory node\n"); |
| 1258 | return ret; |
| 1259 | } |
| 1260 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1261 | /* Get offset of the chosen node */ |
| 1262 | ret = fdt_path_offset(fdt, "/chosen"); |
| 1263 | if (ret < 0) |
| 1264 | { |
| 1265 | dprintf(CRITICAL, "Could not find chosen node.\n"); |
| 1266 | return ret; |
| 1267 | } |
| 1268 | |
| 1269 | offset = ret; |
Maria Yu | abde954 | 2014-07-07 14:49:34 +0800 | [diff] [blame] | 1270 | if (cmdline) |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1271 | { |
Maria Yu | abde954 | 2014-07-07 14:49:34 +0800 | [diff] [blame] | 1272 | /* Adding the cmdline to the chosen node */ |
| 1273 | ret = fdt_appendprop_string(fdt, offset, (const char*)"bootargs", (const void*)cmdline); |
| 1274 | if (ret) |
| 1275 | { |
| 1276 | dprintf(CRITICAL, "ERROR: Cannot update chosen node [bootargs]\n"); |
| 1277 | return ret; |
| 1278 | } |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
Joonwoo Park | a5b5f49 | 2014-02-13 18:24:48 -0800 | [diff] [blame] | 1281 | if (ramdisk_size) { |
| 1282 | /* Adding the initrd-start to the chosen node */ |
| 1283 | ret = fdt_setprop_u32(fdt, offset, "linux,initrd-start", |
| 1284 | (uint32_t)ramdisk); |
| 1285 | if (ret) |
| 1286 | { |
| 1287 | dprintf(CRITICAL, "ERROR: Cannot update chosen node [linux,initrd-start]\n"); |
| 1288 | return ret; |
| 1289 | } |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1290 | |
Joonwoo Park | a5b5f49 | 2014-02-13 18:24:48 -0800 | [diff] [blame] | 1291 | /* Adding the initrd-end to the chosen node */ |
| 1292 | ret = fdt_setprop_u32(fdt, offset, "linux,initrd-end", |
| 1293 | ((uint32_t)ramdisk + ramdisk_size)); |
| 1294 | if (ret) |
| 1295 | { |
| 1296 | dprintf(CRITICAL, "ERROR: Cannot update chosen node [linux,initrd-end]\n"); |
| 1297 | return ret; |
| 1298 | } |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | fdt_pack(fdt); |
| 1302 | |
| 1303 | return ret; |
| 1304 | } |
Maria Yu | ca51ee2 | 2013-06-27 21:45:24 +0800 | [diff] [blame] | 1305 | |