fix gcc6 build error
[profile/mobile/platform/kernel/u-boot-tm1.git] / property / dev_tree.c
1 /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
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 <config.h>
31 #include "dev_tree.h"
32 //#include <lib/ptable.h>
33 #include <malloc.h>
34 //#include <qpic_nand.h>
35 //#include <stdlib.h>
36 //#include <string.h>
37 //#include <platform.h>
38 //#include <board.h>
39
40 #define DEBUG
41
42 #ifdef DEBUG
43 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
44 #else
45 #define debugf(fmt, args...)
46 #endif
47
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)
51 {
52         return -1;
53 }
54 #if 0
55 /* TODO: This function needs to be moved to target layer to check violations
56  * against all the other regions as well.
57  */
58 extern int check_aboot_addr_range_overlap(uint32_t start, uint32_t size);
59
60 /*
61  * Will relocate the DTB to the tags addr if the device tree is found and return
62  * its address
63  *
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
67  *
68  * Return Value: DTB address : If appended device tree is found
69  *               'NULL'         : Otherwise
70  */
71 void *dev_tree_appended(void *kernel, void *tags, uint32_t kernel_size)
72 {
73         uint32_t app_dtb_offset = 0;
74         uint32_t size;
75
76         memcpy((void*) &app_dtb_offset, (void*) (kernel + DTB_OFFSET), sizeof(uint32_t));
77
78         /*
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.
84          */
85         if (app_dtb_offset < kernel_size)
86         {
87                 if (!fdt_check_header((void*) (kernel + app_dtb_offset)))
88                 {
89                         void *dtb;
90                         int rc;
91
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))
96                         {
97                                 debugf("Appended dtb aboot overlap check failed.\n");
98                                 return NULL;
99                         }
100                         rc = fdt_open_into(dtb, tags, size);
101                         if (rc == 0)
102                         {
103                                 /* clear out the old DTB magic so kernel doesn't find it */
104                                 *((uint32_t *)dtb) = 0;
105                                 return tags;
106                         }
107                 }
108         }
109         else
110                 debugf( "DTB offset is incorrect, kernel image does not have appended DTB\n");
111
112         return NULL;
113 }
114 #endif
115 /* Function to return the pointer to the start of the correct device tree
116  *  based on the platform data.
117  */
118 struct dt_entry * dev_tree_get_entry_ptr(struct dt_table *table)
119 {
120         uint32_t i;
121         struct dt_entry *dt_entry_ptr;
122         struct dt_entry *latest_dt_entry = NULL;
123
124         dt_entry_ptr = (struct dt_entry *)((char *)table + DEV_TREE_HEADER_SIZE);
125
126         debugf("magic=%x,ver=%d,num=%d\n",table->magic,table->version,table->num_entries);
127
128 #ifndef CONFIG_MULTI_DTS
129         hw_revision = DT_HARDWARE_ID;
130 #endif
131         debugf("detected hw_rev=%x\n", hw_revision);
132
133         for(i = 0; i < table->num_entries; i++)
134         {
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);
138
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
143                  */
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))
147                         {
148                                 debugf("Find matched DTB hw_rev=%x\n",
149                                         dt_entry_ptr->variant_id);
150                                 return dt_entry_ptr;
151                         }
152
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))
157                         {
158                                 debugf("Find matched DTB hw_rev=%x\n",
159                                         dt_entry_ptr->variant_id);
160                                 return dt_entry_ptr;
161                         }
162
163
164                 /* if the exact match not found, return the closest match
165                  * assuming it to be the nearest soc version
166                  */
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;
171                 }
172                 dt_entry_ptr++;
173         }
174
175         if (latest_dt_entry) {
176                 debugf( "Loading DTB with SOC version:%x\n", latest_dt_entry->soc_rev);
177                 return latest_dt_entry;
178         }
179
180         return NULL;
181 }
182
183 int load_dtb(int addr,void* dt_img_adr)
184 {
185         struct dt_table *table;
186         struct dt_entry *dt_entry_ptr;
187
188         /* offset now point to start of dt.img */
189         table = (struct dt_table*)dt_img_adr;
190
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);
193
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);
197                 return -1;
198         }
199
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");
203                 return -1;
204         }
205
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))
208         //{
209         //      debugf("Device tree addresses overlap with aboot addresses.\n");
210         //      return -1;
211         //}
212
213         memcpy((void*)addr,(const void*)(dt_img_adr +  dt_entry_ptr->offset), dt_entry_ptr->size);
214
215         return 0;
216 }
217
218
219 #if 0
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.
223  */
224 int dev_tree_add_first_mem_info(uint32_t *fdt, uint32_t offset, uint32_t addr, uint32_t size)
225 {
226         int ret;
227
228         ret = fdt_setprop_u32(fdt, offset, "reg", addr);
229
230         if (ret)
231         {
232                 debugf( "Failed to add the memory information addr: %d\n",
233                                 ret);
234         }
235
236
237         ret = fdt_appendprop_u32(fdt, offset, "reg", size);
238
239         if (ret)
240         {
241                 debugf( "Failed to add the memory information size: %d\n",
242                                 ret);
243         }
244
245         return ret;
246 }
247
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)
250 {
251         static int mem_info_cnt = 0;
252         int ret;
253
254         if (!mem_info_cnt)
255         {
256                 /* Replace any other reg prop in the memory node. */
257                 ret = fdt_setprop_u32(fdt, offset, "reg", addr);
258                 mem_info_cnt = 1;
259         }
260         else
261         {
262                 /* Append the mem info to the reg prop for subsequent nodes.  */
263                 ret = fdt_appendprop_u32(fdt, offset, "reg", addr);
264         }
265
266         if (ret)
267         {
268                 debugf( "Failed to add the memory information addr: %d\n",
269                                 ret);
270         }
271
272
273         ret = fdt_appendprop_u32(fdt, offset, "reg", size);
274
275         if (ret)
276         {
277                 debugf( "Failed to add the memory information size: %d\n",
278                                 ret);
279         }
280
281         return ret;
282 }
283
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)
287 {
288         int ret = 0;
289         uint32_t offset;
290
291         /* Check the device tree header */
292         ret = fdt_check_header(fdt);
293         if (ret)
294         {
295                 debugf( "Invalid device tree header \n");
296                 return ret;
297         }
298
299         /* Add padding to make space for new nodes and properties. */
300         ret = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + DTB_PAD_SIZE);
301         if (ret!= 0)
302         {
303                 debugf( "Failed to move/resize dtb buffer: %d\n", ret);
304                 return ret;
305         }
306
307         /* Get offset of the memory node */
308         ret = fdt_path_offset(fdt, "/memory");
309         if (ret < 0)
310         {
311                 debugf( "Could not find memory node.\n");
312                 return ret;
313         }
314
315         offset = ret;
316
317         ret = target_dev_tree_mem(fdt, offset);
318         if(ret)
319         {
320                 debugf( "ERROR: Cannot update memory node\n");
321                 return ret;
322         }
323
324         /* Get offset of the chosen node */
325         ret = fdt_path_offset(fdt, "/chosen");
326         if (ret < 0)
327         {
328                 debugf("Could not find chosen node.\n");
329                 return ret;
330         }
331
332         offset = ret;
333         /* Adding the cmdline to the chosen node */
334         ret = fdt_setprop_string(fdt, offset, (const char*)"bootargs", (const void*)cmdline);
335         if (ret)
336         {
337                 debugf( "ERROR: Cannot update chosen node [bootargs]\n");
338                 return ret;
339         }
340
341         /* Adding the initrd-start to the chosen node */
342         ret = fdt_setprop_u32(fdt, offset, "linux,initrd-start", (uint32_t)ramdisk);
343         if (ret)
344         {
345                 debugf( "ERROR: Cannot update chosen node [linux,initrd-start]\n");
346                 return ret;
347         }
348
349         /* Adding the initrd-end to the chosen node */
350         ret = fdt_setprop_u32(fdt, offset, "linux,initrd-end", ((uint32_t)ramdisk + ramdisk_size));
351         if (ret)
352         {
353                 debugf( "ERROR: Cannot update chosen node [linux,initrd-end]\n");
354                 return ret;
355         }
356
357         fdt_pack(fdt);
358
359         return ret;
360 }
361 #endif