164dbbd40SGerald Van Baren /* 264dbbd40SGerald Van Baren * (C) Copyright 2007 364dbbd40SGerald Van Baren * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com 464dbbd40SGerald Van Baren * 52a523f52SShengzhou Liu * Copyright 2010-2011 Freescale Semiconductor, Inc. 6a0342c08SKumar Gala * 7*1a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 864dbbd40SGerald Van Baren */ 964dbbd40SGerald Van Baren 1064dbbd40SGerald Van Baren #include <common.h> 113e303f74SAnton Vorontsov #include <stdio_dev.h> 1264dbbd40SGerald Van Baren #include <linux/ctype.h> 1364dbbd40SGerald Van Baren #include <linux/types.h> 1464dbbd40SGerald Van Baren #include <asm/global_data.h> 1564dbbd40SGerald Van Baren #include <libfdt.h> 1664dbbd40SGerald Van Baren #include <fdt_support.h> 17151c8b09SKumar Gala #include <exports.h> 1864dbbd40SGerald Van Baren 1964dbbd40SGerald Van Baren /* 2064dbbd40SGerald Van Baren * Global data (for the gd->bd) 2164dbbd40SGerald Van Baren */ 2264dbbd40SGerald Van Baren DECLARE_GLOBAL_DATA_PTR; 2364dbbd40SGerald Van Baren 243bed2aafSKumar Gala /** 253bed2aafSKumar Gala * fdt_getprop_u32_default - Find a node and return it's property or a default 263bed2aafSKumar Gala * 273bed2aafSKumar Gala * @fdt: ptr to device tree 283bed2aafSKumar Gala * @path: path of node 293bed2aafSKumar Gala * @prop: property name 303bed2aafSKumar Gala * @dflt: default value if the property isn't found 313bed2aafSKumar Gala * 323bed2aafSKumar Gala * Convenience function to find a node and return it's property or a 333bed2aafSKumar Gala * default value if it doesn't exist. 343bed2aafSKumar Gala */ 3507e12784SGabe Black u32 fdt_getprop_u32_default(const void *fdt, const char *path, 3607e12784SGabe Black const char *prop, const u32 dflt) 373bed2aafSKumar Gala { 388aa5ec6eSKim Phillips const fdt32_t *val; 393bed2aafSKumar Gala int off; 403bed2aafSKumar Gala 413bed2aafSKumar Gala off = fdt_path_offset(fdt, path); 423bed2aafSKumar Gala if (off < 0) 433bed2aafSKumar Gala return dflt; 443bed2aafSKumar Gala 453bed2aafSKumar Gala val = fdt_getprop(fdt, off, prop, NULL); 463bed2aafSKumar Gala if (val) 47de16606aSGabe Black return fdt32_to_cpu(*val); 483bed2aafSKumar Gala else 493bed2aafSKumar Gala return dflt; 503bed2aafSKumar Gala } 5164dbbd40SGerald Van Baren 52a3c2933eSKumar Gala /** 53a3c2933eSKumar Gala * fdt_find_and_setprop: Find a node and set it's property 54a3c2933eSKumar Gala * 55a3c2933eSKumar Gala * @fdt: ptr to device tree 56a3c2933eSKumar Gala * @node: path of node 57a3c2933eSKumar Gala * @prop: property name 58a3c2933eSKumar Gala * @val: ptr to new value 59a3c2933eSKumar Gala * @len: length of new property value 60a3c2933eSKumar Gala * @create: flag to create the property if it doesn't exist 61a3c2933eSKumar Gala * 62a3c2933eSKumar Gala * Convenience function to directly set a property given the path to the node. 63a3c2933eSKumar Gala */ 64a3c2933eSKumar Gala int fdt_find_and_setprop(void *fdt, const char *node, const char *prop, 65a3c2933eSKumar Gala const void *val, int len, int create) 66a3c2933eSKumar Gala { 678d04f02fSKumar Gala int nodeoff = fdt_path_offset(fdt, node); 68a3c2933eSKumar Gala 69a3c2933eSKumar Gala if (nodeoff < 0) 70a3c2933eSKumar Gala return nodeoff; 71a3c2933eSKumar Gala 728aa5ec6eSKim Phillips if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL)) 73a3c2933eSKumar Gala return 0; /* create flag not set; so exit quietly */ 74a3c2933eSKumar Gala 75a3c2933eSKumar Gala return fdt_setprop(fdt, nodeoff, prop, val, len); 76a3c2933eSKumar Gala } 77a3c2933eSKumar Gala 78151c8b09SKumar Gala #ifdef CONFIG_OF_STDOUT_VIA_ALIAS 793e303f74SAnton Vorontsov 80036036d7SMarek Vasut #ifdef CONFIG_CONS_INDEX 813e303f74SAnton Vorontsov static void fdt_fill_multisername(char *sername, size_t maxlen) 823e303f74SAnton Vorontsov { 833e303f74SAnton Vorontsov const char *outname = stdio_devices[stdout]->name; 843e303f74SAnton Vorontsov 853e303f74SAnton Vorontsov if (strcmp(outname, "serial") > 0) 863e303f74SAnton Vorontsov strncpy(sername, outname, maxlen); 873e303f74SAnton Vorontsov 883e303f74SAnton Vorontsov /* eserial? */ 893e303f74SAnton Vorontsov if (strcmp(outname + 1, "serial") > 0) 903e303f74SAnton Vorontsov strncpy(sername, outname + 1, maxlen); 913e303f74SAnton Vorontsov } 92036036d7SMarek Vasut #endif 933e303f74SAnton Vorontsov 9440777812SDetlev Zundel static int fdt_fixup_stdout(void *fdt, int chosenoff) 95151c8b09SKumar Gala { 96151c8b09SKumar Gala int err = 0; 97151c8b09SKumar Gala #ifdef CONFIG_CONS_INDEX 98151c8b09SKumar Gala int node; 99151c8b09SKumar Gala char sername[9] = { 0 }; 100151c8b09SKumar Gala const char *path; 101151c8b09SKumar Gala 1023e303f74SAnton Vorontsov fdt_fill_multisername(sername, sizeof(sername) - 1); 1033e303f74SAnton Vorontsov if (!sername[0]) 104151c8b09SKumar Gala sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1); 105151c8b09SKumar Gala 106151c8b09SKumar Gala err = node = fdt_path_offset(fdt, "/aliases"); 107151c8b09SKumar Gala if (node >= 0) { 108151c8b09SKumar Gala int len; 109151c8b09SKumar Gala path = fdt_getprop(fdt, node, sername, &len); 110151c8b09SKumar Gala if (path) { 111151c8b09SKumar Gala char *p = malloc(len); 112151c8b09SKumar Gala err = -FDT_ERR_NOSPACE; 113151c8b09SKumar Gala if (p) { 114151c8b09SKumar Gala memcpy(p, path, len); 11540777812SDetlev Zundel err = fdt_setprop(fdt, chosenoff, 116151c8b09SKumar Gala "linux,stdout-path", p, len); 117151c8b09SKumar Gala free(p); 118151c8b09SKumar Gala } 119151c8b09SKumar Gala } else { 120151c8b09SKumar Gala err = len; 121151c8b09SKumar Gala } 122151c8b09SKumar Gala } 123151c8b09SKumar Gala #endif 124151c8b09SKumar Gala if (err < 0) 125151c8b09SKumar Gala printf("WARNING: could not set linux,stdout-path %s.\n", 126151c8b09SKumar Gala fdt_strerror(err)); 127151c8b09SKumar Gala 128151c8b09SKumar Gala return err; 129151c8b09SKumar Gala } 130151c8b09SKumar Gala #endif 131151c8b09SKumar Gala 1322a1a2cb6SKumar Gala int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force) 13364dbbd40SGerald Van Baren { 13464dbbd40SGerald Van Baren int nodeoffset; 1352a1a2cb6SKumar Gala int err, j, total; 1368aa5ec6eSKim Phillips fdt32_t tmp; 137b60af3d4SGerald Van Baren const char *path; 1382a1a2cb6SKumar Gala uint64_t addr, size; 13964dbbd40SGerald Van Baren 1402a1a2cb6SKumar Gala /* Find the "chosen" node. */ 1412a1a2cb6SKumar Gala nodeoffset = fdt_path_offset (fdt, "/chosen"); 1422a1a2cb6SKumar Gala 1432a1a2cb6SKumar Gala /* If there is no "chosen" node in the blob return */ 1442a1a2cb6SKumar Gala if (nodeoffset < 0) { 1452a1a2cb6SKumar Gala printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset)); 1462a1a2cb6SKumar Gala return nodeoffset; 14764dbbd40SGerald Van Baren } 14864dbbd40SGerald Van Baren 1492a1a2cb6SKumar Gala /* just return if initrd_start/end aren't valid */ 1502a1a2cb6SKumar Gala if ((initrd_start == 0) || (initrd_end == 0)) 1512a1a2cb6SKumar Gala return 0; 1522a1a2cb6SKumar Gala 1532a1a2cb6SKumar Gala total = fdt_num_mem_rsv(fdt); 154c28abb9cSGerald Van Baren 155c28abb9cSGerald Van Baren /* 156c28abb9cSGerald Van Baren * Look for an existing entry and update it. If we don't find 157c28abb9cSGerald Van Baren * the entry, we will j be the next available slot. 158c28abb9cSGerald Van Baren */ 1598d04f02fSKumar Gala for (j = 0; j < total; j++) { 1608d04f02fSKumar Gala err = fdt_get_mem_rsv(fdt, j, &addr, &size); 1618d04f02fSKumar Gala if (addr == initrd_start) { 1628d04f02fSKumar Gala fdt_del_mem_rsv(fdt, j); 163c28abb9cSGerald Van Baren break; 164c28abb9cSGerald Van Baren } 165c28abb9cSGerald Van Baren } 1668d04f02fSKumar Gala 167ce6b27a8SGrant Likely err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start); 16864dbbd40SGerald Van Baren if (err < 0) { 1692a1a2cb6SKumar Gala printf("fdt_initrd: %s\n", fdt_strerror(err)); 17064dbbd40SGerald Van Baren return err; 17164dbbd40SGerald Van Baren } 1722a1a2cb6SKumar Gala 1732a1a2cb6SKumar Gala path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL); 1742a1a2cb6SKumar Gala if ((path == NULL) || force) { 1758aa5ec6eSKim Phillips tmp = cpu_to_fdt32(initrd_start); 1762a1a2cb6SKumar Gala err = fdt_setprop(fdt, nodeoffset, 1772a1a2cb6SKumar Gala "linux,initrd-start", &tmp, sizeof(tmp)); 1782a1a2cb6SKumar Gala if (err < 0) { 1792a1a2cb6SKumar Gala printf("WARNING: " 1802a1a2cb6SKumar Gala "could not set linux,initrd-start %s.\n", 1812a1a2cb6SKumar Gala fdt_strerror(err)); 1822a1a2cb6SKumar Gala return err; 1832a1a2cb6SKumar Gala } 1848aa5ec6eSKim Phillips tmp = cpu_to_fdt32(initrd_end); 1852a1a2cb6SKumar Gala err = fdt_setprop(fdt, nodeoffset, 1862a1a2cb6SKumar Gala "linux,initrd-end", &tmp, sizeof(tmp)); 1872a1a2cb6SKumar Gala if (err < 0) { 1882a1a2cb6SKumar Gala printf("WARNING: could not set linux,initrd-end %s.\n", 1892a1a2cb6SKumar Gala fdt_strerror(err)); 1902a1a2cb6SKumar Gala 1912a1a2cb6SKumar Gala return err; 1922a1a2cb6SKumar Gala } 1932a1a2cb6SKumar Gala } 1942a1a2cb6SKumar Gala 1952a1a2cb6SKumar Gala return 0; 1962a1a2cb6SKumar Gala } 1972a1a2cb6SKumar Gala 19856844a22SHeiko Schocher int fdt_chosen(void *fdt, int force) 1992a1a2cb6SKumar Gala { 2002a1a2cb6SKumar Gala int nodeoffset; 2012a1a2cb6SKumar Gala int err; 2022a1a2cb6SKumar Gala char *str; /* used to set string properties */ 2032a1a2cb6SKumar Gala const char *path; 2042a1a2cb6SKumar Gala 2052a1a2cb6SKumar Gala err = fdt_check_header(fdt); 2062a1a2cb6SKumar Gala if (err < 0) { 2072a1a2cb6SKumar Gala printf("fdt_chosen: %s\n", fdt_strerror(err)); 2082a1a2cb6SKumar Gala return err; 20964dbbd40SGerald Van Baren } 21064dbbd40SGerald Van Baren 21164dbbd40SGerald Van Baren /* 21264dbbd40SGerald Van Baren * Find the "chosen" node. 21364dbbd40SGerald Van Baren */ 2148d04f02fSKumar Gala nodeoffset = fdt_path_offset (fdt, "/chosen"); 21564dbbd40SGerald Van Baren 21664dbbd40SGerald Van Baren /* 217b60af3d4SGerald Van Baren * If there is no "chosen" node in the blob, create it. 21864dbbd40SGerald Van Baren */ 21964dbbd40SGerald Van Baren if (nodeoffset < 0) { 22064dbbd40SGerald Van Baren /* 22164dbbd40SGerald Van Baren * Create a new node "/chosen" (offset 0 is root level) 22264dbbd40SGerald Van Baren */ 22364dbbd40SGerald Van Baren nodeoffset = fdt_add_subnode(fdt, 0, "chosen"); 22464dbbd40SGerald Van Baren if (nodeoffset < 0) { 2255fe6be62SGerald Van Baren printf("WARNING: could not create /chosen %s.\n", 22635ec398fSGerald Van Baren fdt_strerror(nodeoffset)); 22764dbbd40SGerald Van Baren return nodeoffset; 22864dbbd40SGerald Van Baren } 22964dbbd40SGerald Van Baren } 23064dbbd40SGerald Van Baren 23164dbbd40SGerald Van Baren /* 232b60af3d4SGerald Van Baren * Create /chosen properites that don't exist in the fdt. 233b60af3d4SGerald Van Baren * If the property exists, update it only if the "force" parameter 234b60af3d4SGerald Van Baren * is true. 23564dbbd40SGerald Van Baren */ 23664dbbd40SGerald Van Baren str = getenv("bootargs"); 23764dbbd40SGerald Van Baren if (str != NULL) { 238b60af3d4SGerald Van Baren path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL); 239b60af3d4SGerald Van Baren if ((path == NULL) || force) { 24035ec398fSGerald Van Baren err = fdt_setprop(fdt, nodeoffset, 24135ec398fSGerald Van Baren "bootargs", str, strlen(str)+1); 24264dbbd40SGerald Van Baren if (err < 0) 2435fe6be62SGerald Van Baren printf("WARNING: could not set bootargs %s.\n", 24435ec398fSGerald Van Baren fdt_strerror(err)); 24564dbbd40SGerald Van Baren } 246b60af3d4SGerald Van Baren } 2472a1a2cb6SKumar Gala 248151c8b09SKumar Gala #ifdef CONFIG_OF_STDOUT_VIA_ALIAS 249b60af3d4SGerald Van Baren path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL); 250b60af3d4SGerald Van Baren if ((path == NULL) || force) 251151c8b09SKumar Gala err = fdt_fixup_stdout(fdt, nodeoffset); 252151c8b09SKumar Gala #endif 253151c8b09SKumar Gala 25464dbbd40SGerald Van Baren #ifdef OF_STDOUT_PATH 255b60af3d4SGerald Van Baren path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL); 256b60af3d4SGerald Van Baren if ((path == NULL) || force) { 25735ec398fSGerald Van Baren err = fdt_setprop(fdt, nodeoffset, 25835ec398fSGerald Van Baren "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1); 25964dbbd40SGerald Van Baren if (err < 0) 2605fe6be62SGerald Van Baren printf("WARNING: could not set linux,stdout-path %s.\n", 26135ec398fSGerald Van Baren fdt_strerror(err)); 262b60af3d4SGerald Van Baren } 26364dbbd40SGerald Van Baren #endif 26464dbbd40SGerald Van Baren 26564dbbd40SGerald Van Baren return err; 26664dbbd40SGerald Van Baren } 26764dbbd40SGerald Van Baren 268e93becf8SKumar Gala void do_fixup_by_path(void *fdt, const char *path, const char *prop, 269e93becf8SKumar Gala const void *val, int len, int create) 270e93becf8SKumar Gala { 271e93becf8SKumar Gala #if defined(DEBUG) 272e93becf8SKumar Gala int i; 273d9ad115bSKumar Gala debug("Updating property '%s/%s' = ", path, prop); 274e93becf8SKumar Gala for (i = 0; i < len; i++) 275e93becf8SKumar Gala debug(" %.2x", *(u8*)(val+i)); 276e93becf8SKumar Gala debug("\n"); 277e93becf8SKumar Gala #endif 278e93becf8SKumar Gala int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create); 279e93becf8SKumar Gala if (rc) 280e93becf8SKumar Gala printf("Unable to update property %s:%s, err=%s\n", 281e93becf8SKumar Gala path, prop, fdt_strerror(rc)); 282e93becf8SKumar Gala } 283e93becf8SKumar Gala 284e93becf8SKumar Gala void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop, 285e93becf8SKumar Gala u32 val, int create) 286e93becf8SKumar Gala { 2878aa5ec6eSKim Phillips fdt32_t tmp = cpu_to_fdt32(val); 2888aa5ec6eSKim Phillips do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create); 289e93becf8SKumar Gala } 290e93becf8SKumar Gala 2919eb77ceaSKumar Gala void do_fixup_by_prop(void *fdt, 2929eb77ceaSKumar Gala const char *pname, const void *pval, int plen, 2939eb77ceaSKumar Gala const char *prop, const void *val, int len, 2949eb77ceaSKumar Gala int create) 2959eb77ceaSKumar Gala { 2969eb77ceaSKumar Gala int off; 2979eb77ceaSKumar Gala #if defined(DEBUG) 2989eb77ceaSKumar Gala int i; 299d9ad115bSKumar Gala debug("Updating property '%s' = ", prop); 3009eb77ceaSKumar Gala for (i = 0; i < len; i++) 3019eb77ceaSKumar Gala debug(" %.2x", *(u8*)(val+i)); 3029eb77ceaSKumar Gala debug("\n"); 3039eb77ceaSKumar Gala #endif 3049eb77ceaSKumar Gala off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen); 3059eb77ceaSKumar Gala while (off != -FDT_ERR_NOTFOUND) { 3068aa5ec6eSKim Phillips if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL)) 3079eb77ceaSKumar Gala fdt_setprop(fdt, off, prop, val, len); 3089eb77ceaSKumar Gala off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen); 3099eb77ceaSKumar Gala } 3109eb77ceaSKumar Gala } 3119eb77ceaSKumar Gala 3129eb77ceaSKumar Gala void do_fixup_by_prop_u32(void *fdt, 3139eb77ceaSKumar Gala const char *pname, const void *pval, int plen, 3149eb77ceaSKumar Gala const char *prop, u32 val, int create) 3159eb77ceaSKumar Gala { 3168aa5ec6eSKim Phillips fdt32_t tmp = cpu_to_fdt32(val); 3178aa5ec6eSKim Phillips do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create); 3189eb77ceaSKumar Gala } 3199eb77ceaSKumar Gala 3209eb77ceaSKumar Gala void do_fixup_by_compat(void *fdt, const char *compat, 3219eb77ceaSKumar Gala const char *prop, const void *val, int len, int create) 3229eb77ceaSKumar Gala { 3239eb77ceaSKumar Gala int off = -1; 3249eb77ceaSKumar Gala #if defined(DEBUG) 3259eb77ceaSKumar Gala int i; 326d9ad115bSKumar Gala debug("Updating property '%s' = ", prop); 3279eb77ceaSKumar Gala for (i = 0; i < len; i++) 3289eb77ceaSKumar Gala debug(" %.2x", *(u8*)(val+i)); 3299eb77ceaSKumar Gala debug("\n"); 3309eb77ceaSKumar Gala #endif 3319eb77ceaSKumar Gala off = fdt_node_offset_by_compatible(fdt, -1, compat); 3329eb77ceaSKumar Gala while (off != -FDT_ERR_NOTFOUND) { 3338aa5ec6eSKim Phillips if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL)) 3349eb77ceaSKumar Gala fdt_setprop(fdt, off, prop, val, len); 3359eb77ceaSKumar Gala off = fdt_node_offset_by_compatible(fdt, off, compat); 3369eb77ceaSKumar Gala } 3379eb77ceaSKumar Gala } 3389eb77ceaSKumar Gala 3399eb77ceaSKumar Gala void do_fixup_by_compat_u32(void *fdt, const char *compat, 3409eb77ceaSKumar Gala const char *prop, u32 val, int create) 3419eb77ceaSKumar Gala { 3428aa5ec6eSKim Phillips fdt32_t tmp = cpu_to_fdt32(val); 3438aa5ec6eSKim Phillips do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create); 3449eb77ceaSKumar Gala } 3459eb77ceaSKumar Gala 346a6bd9e83SJohn Rigby /* 347a6bd9e83SJohn Rigby * Get cells len in bytes 348a6bd9e83SJohn Rigby * if #NNNN-cells property is 2 then len is 8 349a6bd9e83SJohn Rigby * otherwise len is 4 350a6bd9e83SJohn Rigby */ 351a6bd9e83SJohn Rigby static int get_cells_len(void *blob, char *nr_cells_name) 3523c927281SKumar Gala { 3538aa5ec6eSKim Phillips const fdt32_t *cell; 354a6bd9e83SJohn Rigby 355a6bd9e83SJohn Rigby cell = fdt_getprop(blob, 0, nr_cells_name, NULL); 356de16606aSGabe Black if (cell && fdt32_to_cpu(*cell) == 2) 357a6bd9e83SJohn Rigby return 8; 358a6bd9e83SJohn Rigby 359a6bd9e83SJohn Rigby return 4; 360a6bd9e83SJohn Rigby } 361a6bd9e83SJohn Rigby 362a6bd9e83SJohn Rigby /* 363a6bd9e83SJohn Rigby * Write a 4 or 8 byte big endian cell 364a6bd9e83SJohn Rigby */ 365a6bd9e83SJohn Rigby static void write_cell(u8 *addr, u64 val, int size) 366a6bd9e83SJohn Rigby { 367a6bd9e83SJohn Rigby int shift = (size - 1) * 8; 368a6bd9e83SJohn Rigby while (size-- > 0) { 369a6bd9e83SJohn Rigby *addr++ = (val >> shift) & 0xff; 370a6bd9e83SJohn Rigby shift -= 8; 371a6bd9e83SJohn Rigby } 372a6bd9e83SJohn Rigby } 373a6bd9e83SJohn Rigby 3745e574546SDoug Anderson #ifdef CONFIG_NR_DRAM_BANKS 3755e574546SDoug Anderson #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS 3765e574546SDoug Anderson #else 3778aa5ec6eSKim Phillips #define MEMORY_BANKS_MAX 4 3785e574546SDoug Anderson #endif 379a6bd9e83SJohn Rigby int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks) 380a6bd9e83SJohn Rigby { 381a6bd9e83SJohn Rigby int err, nodeoffset; 382a6bd9e83SJohn Rigby int addr_cell_len, size_cell_len, len; 3838aa5ec6eSKim Phillips u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */ 384a6bd9e83SJohn Rigby int bank; 3853c927281SKumar Gala 3868aa5ec6eSKim Phillips if (banks > MEMORY_BANKS_MAX) { 3878aa5ec6eSKim Phillips printf("%s: num banks %d exceeds hardcoded limit %d." 3888aa5ec6eSKim Phillips " Recompile with higher MEMORY_BANKS_MAX?\n", 3898aa5ec6eSKim Phillips __FUNCTION__, banks, MEMORY_BANKS_MAX); 3908aa5ec6eSKim Phillips return -1; 3918aa5ec6eSKim Phillips } 3928aa5ec6eSKim Phillips 3933c927281SKumar Gala err = fdt_check_header(blob); 3943c927281SKumar Gala if (err < 0) { 3953c927281SKumar Gala printf("%s: %s\n", __FUNCTION__, fdt_strerror(err)); 3963c927281SKumar Gala return err; 3973c927281SKumar Gala } 3983c927281SKumar Gala 3993c927281SKumar Gala /* update, or add and update /memory node */ 4003c927281SKumar Gala nodeoffset = fdt_path_offset(blob, "/memory"); 4013c927281SKumar Gala if (nodeoffset < 0) { 4023c927281SKumar Gala nodeoffset = fdt_add_subnode(blob, 0, "memory"); 4033c927281SKumar Gala if (nodeoffset < 0) 4043c927281SKumar Gala printf("WARNING: could not create /memory: %s.\n", 4053c927281SKumar Gala fdt_strerror(nodeoffset)); 4063c927281SKumar Gala return nodeoffset; 4073c927281SKumar Gala } 4083c927281SKumar Gala err = fdt_setprop(blob, nodeoffset, "device_type", "memory", 4093c927281SKumar Gala sizeof("memory")); 4103c927281SKumar Gala if (err < 0) { 4113c927281SKumar Gala printf("WARNING: could not set %s %s.\n", "device_type", 4123c927281SKumar Gala fdt_strerror(err)); 4133c927281SKumar Gala return err; 4143c927281SKumar Gala } 4153c927281SKumar Gala 416a6bd9e83SJohn Rigby addr_cell_len = get_cells_len(blob, "#address-cells"); 417a6bd9e83SJohn Rigby size_cell_len = get_cells_len(blob, "#size-cells"); 4183c927281SKumar Gala 419a6bd9e83SJohn Rigby for (bank = 0, len = 0; bank < banks; bank++) { 420a6bd9e83SJohn Rigby write_cell(tmp + len, start[bank], addr_cell_len); 421a6bd9e83SJohn Rigby len += addr_cell_len; 422a6bd9e83SJohn Rigby 423a6bd9e83SJohn Rigby write_cell(tmp + len, size[bank], size_cell_len); 424a6bd9e83SJohn Rigby len += size_cell_len; 4253c927281SKumar Gala } 4263c927281SKumar Gala 4273c927281SKumar Gala err = fdt_setprop(blob, nodeoffset, "reg", tmp, len); 4283c927281SKumar Gala if (err < 0) { 4293c927281SKumar Gala printf("WARNING: could not set %s %s.\n", 4303c927281SKumar Gala "reg", fdt_strerror(err)); 4313c927281SKumar Gala return err; 4323c927281SKumar Gala } 4333c927281SKumar Gala return 0; 4343c927281SKumar Gala } 4353c927281SKumar Gala 436a6bd9e83SJohn Rigby int fdt_fixup_memory(void *blob, u64 start, u64 size) 437a6bd9e83SJohn Rigby { 438a6bd9e83SJohn Rigby return fdt_fixup_memory_banks(blob, &start, &size, 1); 439a6bd9e83SJohn Rigby } 440a6bd9e83SJohn Rigby 441ba37aa03SKumar Gala void fdt_fixup_ethernet(void *fdt) 442ab544633SKumar Gala { 443ba37aa03SKumar Gala int node, i, j; 444ba37aa03SKumar Gala char enet[16], *tmp, *end; 445064d55f8SStephen Warren char mac[16]; 446ab544633SKumar Gala const char *path; 447ba37aa03SKumar Gala unsigned char mac_addr[6]; 448ab544633SKumar Gala 449ab544633SKumar Gala node = fdt_path_offset(fdt, "/aliases"); 450ba37aa03SKumar Gala if (node < 0) 451ba37aa03SKumar Gala return; 452ba37aa03SKumar Gala 453ba37aa03SKumar Gala i = 0; 454064d55f8SStephen Warren strcpy(mac, "ethaddr"); 455ba37aa03SKumar Gala while ((tmp = getenv(mac)) != NULL) { 456ba37aa03SKumar Gala sprintf(enet, "ethernet%d", i); 457ba37aa03SKumar Gala path = fdt_getprop(fdt, node, enet, NULL); 458ba37aa03SKumar Gala if (!path) { 459ba37aa03SKumar Gala debug("No alias for %s\n", enet); 460ba37aa03SKumar Gala sprintf(mac, "eth%daddr", ++i); 461ba37aa03SKumar Gala continue; 462ba37aa03SKumar Gala } 463ba37aa03SKumar Gala 464ba37aa03SKumar Gala for (j = 0; j < 6; j++) { 465ba37aa03SKumar Gala mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0; 466ba37aa03SKumar Gala if (tmp) 467ba37aa03SKumar Gala tmp = (*end) ? end+1 : end; 468ba37aa03SKumar Gala } 469ba37aa03SKumar Gala 470ba37aa03SKumar Gala do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0); 471ab544633SKumar Gala do_fixup_by_path(fdt, path, "local-mac-address", 472ba37aa03SKumar Gala &mac_addr, 6, 1); 473ba37aa03SKumar Gala 474ba37aa03SKumar Gala sprintf(mac, "eth%daddr", ++i); 475ab544633SKumar Gala } 476ab544633SKumar Gala } 47718e69a35SAnton Vorontsov 4783082d234SKumar Gala /* Resize the fdt to its actual size + a bit of padding */ 4793082d234SKumar Gala int fdt_resize(void *blob) 4803082d234SKumar Gala { 4813082d234SKumar Gala int i; 4823082d234SKumar Gala uint64_t addr, size; 4833082d234SKumar Gala int total, ret; 4843082d234SKumar Gala uint actualsize; 4853082d234SKumar Gala 4863082d234SKumar Gala if (!blob) 4873082d234SKumar Gala return 0; 4883082d234SKumar Gala 4893082d234SKumar Gala total = fdt_num_mem_rsv(blob); 4903082d234SKumar Gala for (i = 0; i < total; i++) { 4913082d234SKumar Gala fdt_get_mem_rsv(blob, i, &addr, &size); 49292549358SSimon Glass if (addr == (uintptr_t)blob) { 4933082d234SKumar Gala fdt_del_mem_rsv(blob, i); 4943082d234SKumar Gala break; 4953082d234SKumar Gala } 4963082d234SKumar Gala } 4973082d234SKumar Gala 498f242a088SPeter Korsgaard /* 499f242a088SPeter Korsgaard * Calculate the actual size of the fdt 5003840ebfaSFeng Wang * plus the size needed for 5 fdt_add_mem_rsv, one 5013840ebfaSFeng Wang * for the fdt itself and 4 for a possible initrd 5023840ebfaSFeng Wang * ((initrd-start + initrd-end) * 2 (name & value)) 503f242a088SPeter Korsgaard */ 5043082d234SKumar Gala actualsize = fdt_off_dt_strings(blob) + 5053840ebfaSFeng Wang fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry); 5063082d234SKumar Gala 5073082d234SKumar Gala /* Make it so the fdt ends on a page boundary */ 50892549358SSimon Glass actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000); 50992549358SSimon Glass actualsize = actualsize - ((uintptr_t)blob & 0xfff); 5103082d234SKumar Gala 5113082d234SKumar Gala /* Change the fdt header to reflect the correct size */ 5123082d234SKumar Gala fdt_set_totalsize(blob, actualsize); 5133082d234SKumar Gala 5143082d234SKumar Gala /* Add the new reservation */ 51592549358SSimon Glass ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize); 5163082d234SKumar Gala if (ret < 0) 5173082d234SKumar Gala return ret; 5183082d234SKumar Gala 5193082d234SKumar Gala return actualsize; 5203082d234SKumar Gala } 5218ab451c4SKumar Gala 5228ab451c4SKumar Gala #ifdef CONFIG_PCI 523cfd700beSKumar Gala #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4 5248ab451c4SKumar Gala 5258ab451c4SKumar Gala #define FDT_PCI_PREFETCH (0x40000000) 5268ab451c4SKumar Gala #define FDT_PCI_MEM32 (0x02000000) 5278ab451c4SKumar Gala #define FDT_PCI_IO (0x01000000) 5288ab451c4SKumar Gala #define FDT_PCI_MEM64 (0x03000000) 5298ab451c4SKumar Gala 5308ab451c4SKumar Gala int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) { 5318ab451c4SKumar Gala 5328ab451c4SKumar Gala int addrcell, sizecell, len, r; 5338ab451c4SKumar Gala u32 *dma_range; 5348ab451c4SKumar Gala /* sized based on pci addr cells, size-cells, & address-cells */ 5358ab451c4SKumar Gala u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN]; 5368ab451c4SKumar Gala 5378ab451c4SKumar Gala addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1); 5388ab451c4SKumar Gala sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1); 5398ab451c4SKumar Gala 5408ab451c4SKumar Gala dma_range = &dma_ranges[0]; 5418ab451c4SKumar Gala for (r = 0; r < hose->region_count; r++) { 5428ab451c4SKumar Gala u64 bus_start, phys_start, size; 5438ab451c4SKumar Gala 544ff4e66e9SKumar Gala /* skip if !PCI_REGION_SYS_MEMORY */ 545ff4e66e9SKumar Gala if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY)) 5468ab451c4SKumar Gala continue; 5478ab451c4SKumar Gala 5488ab451c4SKumar Gala bus_start = (u64)hose->regions[r].bus_start; 5498ab451c4SKumar Gala phys_start = (u64)hose->regions[r].phys_start; 5508ab451c4SKumar Gala size = (u64)hose->regions[r].size; 5518ab451c4SKumar Gala 5528ab451c4SKumar Gala dma_range[0] = 0; 553cfd700beSKumar Gala if (size >= 0x100000000ull) 5548ab451c4SKumar Gala dma_range[0] |= FDT_PCI_MEM64; 5558ab451c4SKumar Gala else 5568ab451c4SKumar Gala dma_range[0] |= FDT_PCI_MEM32; 5578ab451c4SKumar Gala if (hose->regions[r].flags & PCI_REGION_PREFETCH) 5588ab451c4SKumar Gala dma_range[0] |= FDT_PCI_PREFETCH; 5598ab451c4SKumar Gala #ifdef CONFIG_SYS_PCI_64BIT 5608ab451c4SKumar Gala dma_range[1] = bus_start >> 32; 5618ab451c4SKumar Gala #else 5628ab451c4SKumar Gala dma_range[1] = 0; 5638ab451c4SKumar Gala #endif 5648ab451c4SKumar Gala dma_range[2] = bus_start & 0xffffffff; 5658ab451c4SKumar Gala 5668ab451c4SKumar Gala if (addrcell == 2) { 5678ab451c4SKumar Gala dma_range[3] = phys_start >> 32; 5688ab451c4SKumar Gala dma_range[4] = phys_start & 0xffffffff; 5698ab451c4SKumar Gala } else { 5708ab451c4SKumar Gala dma_range[3] = phys_start & 0xffffffff; 5718ab451c4SKumar Gala } 5728ab451c4SKumar Gala 5738ab451c4SKumar Gala if (sizecell == 2) { 5748ab451c4SKumar Gala dma_range[3 + addrcell + 0] = size >> 32; 5758ab451c4SKumar Gala dma_range[3 + addrcell + 1] = size & 0xffffffff; 5768ab451c4SKumar Gala } else { 5778ab451c4SKumar Gala dma_range[3 + addrcell + 0] = size & 0xffffffff; 5788ab451c4SKumar Gala } 5798ab451c4SKumar Gala 5808ab451c4SKumar Gala dma_range += (3 + addrcell + sizecell); 5818ab451c4SKumar Gala } 5828ab451c4SKumar Gala 5838ab451c4SKumar Gala len = dma_range - &dma_ranges[0]; 5848ab451c4SKumar Gala if (len) 5858ab451c4SKumar Gala fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4); 5868ab451c4SKumar Gala 5878ab451c4SKumar Gala return 0; 5888ab451c4SKumar Gala } 5898ab451c4SKumar Gala #endif 59030d45c0dSStefan Roese 59130d45c0dSStefan Roese #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE 59230d45c0dSStefan Roese /* 5938a805df1SStefan Roese * Provide a weak default function to return the flash bank size. 5948a805df1SStefan Roese * There might be multiple non-identical flash chips connected to one 5958a805df1SStefan Roese * chip-select, so we need to pass an index as well. 5968a805df1SStefan Roese */ 5978a805df1SStefan Roese u32 __flash_get_bank_size(int cs, int idx) 5988a805df1SStefan Roese { 5998a805df1SStefan Roese extern flash_info_t flash_info[]; 6008a805df1SStefan Roese 6018a805df1SStefan Roese /* 6028a805df1SStefan Roese * As default, a simple 1:1 mapping is provided. Boards with 6038a805df1SStefan Roese * a different mapping need to supply a board specific mapping 6048a805df1SStefan Roese * routine. 6058a805df1SStefan Roese */ 6068a805df1SStefan Roese return flash_info[cs].size; 6078a805df1SStefan Roese } 6088a805df1SStefan Roese u32 flash_get_bank_size(int cs, int idx) 6098a805df1SStefan Roese __attribute__((weak, alias("__flash_get_bank_size"))); 6108a805df1SStefan Roese 6118a805df1SStefan Roese /* 61230d45c0dSStefan Roese * This function can be used to update the size in the "reg" property 6138a805df1SStefan Roese * of all NOR FLASH device nodes. This is necessary for boards with 61430d45c0dSStefan Roese * non-fixed NOR FLASH sizes. 61530d45c0dSStefan Roese */ 6168a805df1SStefan Roese int fdt_fixup_nor_flash_size(void *blob) 61730d45c0dSStefan Roese { 61830d45c0dSStefan Roese char compat[][16] = { "cfi-flash", "jedec-flash" }; 61930d45c0dSStefan Roese int off; 62030d45c0dSStefan Roese int len; 62130d45c0dSStefan Roese struct fdt_property *prop; 6222778a014SStefan Roese u32 *reg, *reg2; 62330d45c0dSStefan Roese int i; 62430d45c0dSStefan Roese 62530d45c0dSStefan Roese for (i = 0; i < 2; i++) { 62630d45c0dSStefan Roese off = fdt_node_offset_by_compatible(blob, -1, compat[i]); 62730d45c0dSStefan Roese while (off != -FDT_ERR_NOTFOUND) { 6288a805df1SStefan Roese int idx; 6298a805df1SStefan Roese 63030d45c0dSStefan Roese /* 6318a805df1SStefan Roese * Found one compatible node, so fixup the size 6328a805df1SStefan Roese * int its reg properties 63330d45c0dSStefan Roese */ 63430d45c0dSStefan Roese prop = fdt_get_property_w(blob, off, "reg", &len); 63530d45c0dSStefan Roese if (prop) { 6368a805df1SStefan Roese int tuple_size = 3 * sizeof(reg); 63730d45c0dSStefan Roese 6388a805df1SStefan Roese /* 6398a805df1SStefan Roese * There might be multiple reg-tuples, 6408a805df1SStefan Roese * so loop through them all 6418a805df1SStefan Roese */ 6422778a014SStefan Roese reg = reg2 = (u32 *)&prop->data[0]; 6432778a014SStefan Roese for (idx = 0; idx < (len / tuple_size); idx++) { 6448a805df1SStefan Roese /* 6458a805df1SStefan Roese * Update size in reg property 6468a805df1SStefan Roese */ 6478a805df1SStefan Roese reg[2] = flash_get_bank_size(reg[0], 6488a805df1SStefan Roese idx); 6492778a014SStefan Roese 6502778a014SStefan Roese /* 6512778a014SStefan Roese * Point to next reg tuple 6522778a014SStefan Roese */ 6532778a014SStefan Roese reg += 3; 65430d45c0dSStefan Roese } 6552778a014SStefan Roese 6562778a014SStefan Roese fdt_setprop(blob, off, "reg", reg2, len); 65730d45c0dSStefan Roese } 65830d45c0dSStefan Roese 65930d45c0dSStefan Roese /* Move to next compatible node */ 66030d45c0dSStefan Roese off = fdt_node_offset_by_compatible(blob, off, 66130d45c0dSStefan Roese compat[i]); 66230d45c0dSStefan Roese } 66330d45c0dSStefan Roese } 66430d45c0dSStefan Roese 6658a805df1SStefan Roese return 0; 66630d45c0dSStefan Roese } 66730d45c0dSStefan Roese #endif 6683c950e2eSAnatolij Gustschin 669cb2707afSMatthew McClintock int fdt_increase_size(void *fdt, int add_len) 670cb2707afSMatthew McClintock { 671cb2707afSMatthew McClintock int newlen; 672cb2707afSMatthew McClintock 673cb2707afSMatthew McClintock newlen = fdt_totalsize(fdt) + add_len; 674cb2707afSMatthew McClintock 675cb2707afSMatthew McClintock /* Open in place with a new len */ 676cb2707afSMatthew McClintock return fdt_open_into(fdt, fdt, newlen); 677cb2707afSMatthew McClintock } 678cb2707afSMatthew McClintock 6793c950e2eSAnatolij Gustschin #ifdef CONFIG_FDT_FIXUP_PARTITIONS 6803c950e2eSAnatolij Gustschin #include <jffs2/load_kernel.h> 6813c950e2eSAnatolij Gustschin #include <mtd_node.h> 6823c950e2eSAnatolij Gustschin 6833c950e2eSAnatolij Gustschin struct reg_cell { 6843c950e2eSAnatolij Gustschin unsigned int r0; 6853c950e2eSAnatolij Gustschin unsigned int r1; 6863c950e2eSAnatolij Gustschin }; 6873c950e2eSAnatolij Gustschin 6883c950e2eSAnatolij Gustschin int fdt_del_subnodes(const void *blob, int parent_offset) 6893c950e2eSAnatolij Gustschin { 6903c950e2eSAnatolij Gustschin int off, ndepth; 6913c950e2eSAnatolij Gustschin int ret; 6923c950e2eSAnatolij Gustschin 6933c950e2eSAnatolij Gustschin for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth); 6943c950e2eSAnatolij Gustschin (off >= 0) && (ndepth > 0); 6953c950e2eSAnatolij Gustschin off = fdt_next_node(blob, off, &ndepth)) { 6963c950e2eSAnatolij Gustschin if (ndepth == 1) { 6973c950e2eSAnatolij Gustschin debug("delete %s: offset: %x\n", 6983c950e2eSAnatolij Gustschin fdt_get_name(blob, off, 0), off); 6993c950e2eSAnatolij Gustschin ret = fdt_del_node((void *)blob, off); 7003c950e2eSAnatolij Gustschin if (ret < 0) { 7013c950e2eSAnatolij Gustschin printf("Can't delete node: %s\n", 7023c950e2eSAnatolij Gustschin fdt_strerror(ret)); 7033c950e2eSAnatolij Gustschin return ret; 7043c950e2eSAnatolij Gustschin } else { 7053c950e2eSAnatolij Gustschin ndepth = 0; 7063c950e2eSAnatolij Gustschin off = parent_offset; 7073c950e2eSAnatolij Gustschin } 7083c950e2eSAnatolij Gustschin } 7093c950e2eSAnatolij Gustschin } 7103c950e2eSAnatolij Gustschin return 0; 7113c950e2eSAnatolij Gustschin } 7123c950e2eSAnatolij Gustschin 7133c950e2eSAnatolij Gustschin int fdt_del_partitions(void *blob, int parent_offset) 7143c950e2eSAnatolij Gustschin { 7153c950e2eSAnatolij Gustschin const void *prop; 7163c950e2eSAnatolij Gustschin int ndepth = 0; 7173c950e2eSAnatolij Gustschin int off; 7183c950e2eSAnatolij Gustschin int ret; 7193c950e2eSAnatolij Gustschin 7203c950e2eSAnatolij Gustschin off = fdt_next_node(blob, parent_offset, &ndepth); 7213c950e2eSAnatolij Gustschin if (off > 0 && ndepth == 1) { 7223c950e2eSAnatolij Gustschin prop = fdt_getprop(blob, off, "label", NULL); 7233c950e2eSAnatolij Gustschin if (prop == NULL) { 7243c950e2eSAnatolij Gustschin /* 7253c950e2eSAnatolij Gustschin * Could not find label property, nand {}; node? 7263c950e2eSAnatolij Gustschin * Check subnode, delete partitions there if any. 7273c950e2eSAnatolij Gustschin */ 7283c950e2eSAnatolij Gustschin return fdt_del_partitions(blob, off); 7293c950e2eSAnatolij Gustschin } else { 7303c950e2eSAnatolij Gustschin ret = fdt_del_subnodes(blob, parent_offset); 7313c950e2eSAnatolij Gustschin if (ret < 0) { 7323c950e2eSAnatolij Gustschin printf("Can't remove subnodes: %s\n", 7333c950e2eSAnatolij Gustschin fdt_strerror(ret)); 7343c950e2eSAnatolij Gustschin return ret; 7353c950e2eSAnatolij Gustschin } 7363c950e2eSAnatolij Gustschin } 7373c950e2eSAnatolij Gustschin } 7383c950e2eSAnatolij Gustschin return 0; 7393c950e2eSAnatolij Gustschin } 7403c950e2eSAnatolij Gustschin 7413c950e2eSAnatolij Gustschin int fdt_node_set_part_info(void *blob, int parent_offset, 7423c950e2eSAnatolij Gustschin struct mtd_device *dev) 7433c950e2eSAnatolij Gustschin { 7443c950e2eSAnatolij Gustschin struct list_head *pentry; 7453c950e2eSAnatolij Gustschin struct part_info *part; 7463c950e2eSAnatolij Gustschin struct reg_cell cell; 7473c950e2eSAnatolij Gustschin int off, ndepth = 0; 7483c950e2eSAnatolij Gustschin int part_num, ret; 7493c950e2eSAnatolij Gustschin char buf[64]; 7503c950e2eSAnatolij Gustschin 7513c950e2eSAnatolij Gustschin ret = fdt_del_partitions(blob, parent_offset); 7523c950e2eSAnatolij Gustschin if (ret < 0) 7533c950e2eSAnatolij Gustschin return ret; 7543c950e2eSAnatolij Gustschin 7553c950e2eSAnatolij Gustschin /* 7563c950e2eSAnatolij Gustschin * Check if it is nand {}; subnode, adjust 7573c950e2eSAnatolij Gustschin * the offset in this case 7583c950e2eSAnatolij Gustschin */ 7593c950e2eSAnatolij Gustschin off = fdt_next_node(blob, parent_offset, &ndepth); 7603c950e2eSAnatolij Gustschin if (off > 0 && ndepth == 1) 7613c950e2eSAnatolij Gustschin parent_offset = off; 7623c950e2eSAnatolij Gustschin 7633c950e2eSAnatolij Gustschin part_num = 0; 7643c950e2eSAnatolij Gustschin list_for_each_prev(pentry, &dev->parts) { 7653c950e2eSAnatolij Gustschin int newoff; 7663c950e2eSAnatolij Gustschin 7673c950e2eSAnatolij Gustschin part = list_entry(pentry, struct part_info, link); 7683c950e2eSAnatolij Gustschin 7693c950e2eSAnatolij Gustschin debug("%2d: %-20s0x%08x\t0x%08x\t%d\n", 7703c950e2eSAnatolij Gustschin part_num, part->name, part->size, 7713c950e2eSAnatolij Gustschin part->offset, part->mask_flags); 7723c950e2eSAnatolij Gustschin 7733c950e2eSAnatolij Gustschin sprintf(buf, "partition@%x", part->offset); 7743c950e2eSAnatolij Gustschin add_sub: 7753c950e2eSAnatolij Gustschin ret = fdt_add_subnode(blob, parent_offset, buf); 7763c950e2eSAnatolij Gustschin if (ret == -FDT_ERR_NOSPACE) { 7773c950e2eSAnatolij Gustschin ret = fdt_increase_size(blob, 512); 7783c950e2eSAnatolij Gustschin if (!ret) 7793c950e2eSAnatolij Gustschin goto add_sub; 7803c950e2eSAnatolij Gustschin else 7813c950e2eSAnatolij Gustschin goto err_size; 7823c950e2eSAnatolij Gustschin } else if (ret < 0) { 7833c950e2eSAnatolij Gustschin printf("Can't add partition node: %s\n", 7843c950e2eSAnatolij Gustschin fdt_strerror(ret)); 7853c950e2eSAnatolij Gustschin return ret; 7863c950e2eSAnatolij Gustschin } 7873c950e2eSAnatolij Gustschin newoff = ret; 7883c950e2eSAnatolij Gustschin 7893c950e2eSAnatolij Gustschin /* Check MTD_WRITEABLE_CMD flag */ 7903c950e2eSAnatolij Gustschin if (part->mask_flags & 1) { 7913c950e2eSAnatolij Gustschin add_ro: 7923c950e2eSAnatolij Gustschin ret = fdt_setprop(blob, newoff, "read_only", NULL, 0); 7933c950e2eSAnatolij Gustschin if (ret == -FDT_ERR_NOSPACE) { 7943c950e2eSAnatolij Gustschin ret = fdt_increase_size(blob, 512); 7953c950e2eSAnatolij Gustschin if (!ret) 7963c950e2eSAnatolij Gustschin goto add_ro; 7973c950e2eSAnatolij Gustschin else 7983c950e2eSAnatolij Gustschin goto err_size; 7993c950e2eSAnatolij Gustschin } else if (ret < 0) 8003c950e2eSAnatolij Gustschin goto err_prop; 8013c950e2eSAnatolij Gustschin } 8023c950e2eSAnatolij Gustschin 8033c950e2eSAnatolij Gustschin cell.r0 = cpu_to_fdt32(part->offset); 8043c950e2eSAnatolij Gustschin cell.r1 = cpu_to_fdt32(part->size); 8053c950e2eSAnatolij Gustschin add_reg: 8063c950e2eSAnatolij Gustschin ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell)); 8073c950e2eSAnatolij Gustschin if (ret == -FDT_ERR_NOSPACE) { 8083c950e2eSAnatolij Gustschin ret = fdt_increase_size(blob, 512); 8093c950e2eSAnatolij Gustschin if (!ret) 8103c950e2eSAnatolij Gustschin goto add_reg; 8113c950e2eSAnatolij Gustschin else 8123c950e2eSAnatolij Gustschin goto err_size; 8133c950e2eSAnatolij Gustschin } else if (ret < 0) 8143c950e2eSAnatolij Gustschin goto err_prop; 8153c950e2eSAnatolij Gustschin 8163c950e2eSAnatolij Gustschin add_label: 8173c950e2eSAnatolij Gustschin ret = fdt_setprop_string(blob, newoff, "label", part->name); 8183c950e2eSAnatolij Gustschin if (ret == -FDT_ERR_NOSPACE) { 8193c950e2eSAnatolij Gustschin ret = fdt_increase_size(blob, 512); 8203c950e2eSAnatolij Gustschin if (!ret) 8213c950e2eSAnatolij Gustschin goto add_label; 8223c950e2eSAnatolij Gustschin else 8233c950e2eSAnatolij Gustschin goto err_size; 8243c950e2eSAnatolij Gustschin } else if (ret < 0) 8253c950e2eSAnatolij Gustschin goto err_prop; 8263c950e2eSAnatolij Gustschin 8273c950e2eSAnatolij Gustschin part_num++; 8283c950e2eSAnatolij Gustschin } 8293c950e2eSAnatolij Gustschin return 0; 8303c950e2eSAnatolij Gustschin err_size: 8313c950e2eSAnatolij Gustschin printf("Can't increase blob size: %s\n", fdt_strerror(ret)); 8323c950e2eSAnatolij Gustschin return ret; 8333c950e2eSAnatolij Gustschin err_prop: 8343c950e2eSAnatolij Gustschin printf("Can't add property: %s\n", fdt_strerror(ret)); 8353c950e2eSAnatolij Gustschin return ret; 8363c950e2eSAnatolij Gustschin } 8373c950e2eSAnatolij Gustschin 8383c950e2eSAnatolij Gustschin /* 8393c950e2eSAnatolij Gustschin * Update partitions in nor/nand nodes using info from 8403c950e2eSAnatolij Gustschin * mtdparts environment variable. The nodes to update are 8413c950e2eSAnatolij Gustschin * specified by node_info structure which contains mtd device 8423c950e2eSAnatolij Gustschin * type and compatible string: E. g. the board code in 8433c950e2eSAnatolij Gustschin * ft_board_setup() could use: 8443c950e2eSAnatolij Gustschin * 8453c950e2eSAnatolij Gustschin * struct node_info nodes[] = { 8463c950e2eSAnatolij Gustschin * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, }, 8473c950e2eSAnatolij Gustschin * { "cfi-flash", MTD_DEV_TYPE_NOR, }, 8483c950e2eSAnatolij Gustschin * }; 8493c950e2eSAnatolij Gustschin * 8503c950e2eSAnatolij Gustschin * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes)); 8513c950e2eSAnatolij Gustschin */ 8523c950e2eSAnatolij Gustschin void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size) 8533c950e2eSAnatolij Gustschin { 8543c950e2eSAnatolij Gustschin struct node_info *ni = node_info; 8553c950e2eSAnatolij Gustschin struct mtd_device *dev; 8563c950e2eSAnatolij Gustschin char *parts; 8573c950e2eSAnatolij Gustschin int i, idx; 8583c950e2eSAnatolij Gustschin int noff; 8593c950e2eSAnatolij Gustschin 8603c950e2eSAnatolij Gustschin parts = getenv("mtdparts"); 8613c950e2eSAnatolij Gustschin if (!parts) 8623c950e2eSAnatolij Gustschin return; 8633c950e2eSAnatolij Gustschin 8643c950e2eSAnatolij Gustschin if (mtdparts_init() != 0) 8653c950e2eSAnatolij Gustschin return; 8663c950e2eSAnatolij Gustschin 8673c950e2eSAnatolij Gustschin for (i = 0; i < node_info_size; i++) { 8683c950e2eSAnatolij Gustschin idx = 0; 8693c950e2eSAnatolij Gustschin noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat); 8703c950e2eSAnatolij Gustschin while (noff != -FDT_ERR_NOTFOUND) { 8713c950e2eSAnatolij Gustschin debug("%s: %s, mtd dev type %d\n", 8723c950e2eSAnatolij Gustschin fdt_get_name(blob, noff, 0), 8733c950e2eSAnatolij Gustschin ni[i].compat, ni[i].type); 8743c950e2eSAnatolij Gustschin dev = device_find(ni[i].type, idx++); 8753c950e2eSAnatolij Gustschin if (dev) { 8763c950e2eSAnatolij Gustschin if (fdt_node_set_part_info(blob, noff, dev)) 8773c950e2eSAnatolij Gustschin return; /* return on error */ 8783c950e2eSAnatolij Gustschin } 8793c950e2eSAnatolij Gustschin 8803c950e2eSAnatolij Gustschin /* Jump to next flash node */ 8813c950e2eSAnatolij Gustschin noff = fdt_node_offset_by_compatible(blob, noff, 8823c950e2eSAnatolij Gustschin ni[i].compat); 8833c950e2eSAnatolij Gustschin } 8843c950e2eSAnatolij Gustschin } 8853c950e2eSAnatolij Gustschin } 8863c950e2eSAnatolij Gustschin #endif 88749b97d9cSKumar Gala 88849b97d9cSKumar Gala void fdt_del_node_and_alias(void *blob, const char *alias) 88949b97d9cSKumar Gala { 89049b97d9cSKumar Gala int off = fdt_path_offset(blob, alias); 89149b97d9cSKumar Gala 89249b97d9cSKumar Gala if (off < 0) 89349b97d9cSKumar Gala return; 89449b97d9cSKumar Gala 89549b97d9cSKumar Gala fdt_del_node(blob, off); 89649b97d9cSKumar Gala 89749b97d9cSKumar Gala off = fdt_path_offset(blob, "/aliases"); 89849b97d9cSKumar Gala fdt_delprop(blob, off, alias); 89949b97d9cSKumar Gala } 900a0342c08SKumar Gala 901a0342c08SKumar Gala /* Helper to read a big number; size is in cells (not bytes) */ 9028aa5ec6eSKim Phillips static inline u64 of_read_number(const fdt32_t *cell, int size) 903a0342c08SKumar Gala { 904a0342c08SKumar Gala u64 r = 0; 905a0342c08SKumar Gala while (size--) 9068aa5ec6eSKim Phillips r = (r << 32) | fdt32_to_cpu(*(cell++)); 907a0342c08SKumar Gala return r; 908a0342c08SKumar Gala } 909a0342c08SKumar Gala 910a0342c08SKumar Gala #define PRu64 "%llx" 911a0342c08SKumar Gala 912a0342c08SKumar Gala /* Max address size we deal with */ 913a0342c08SKumar Gala #define OF_MAX_ADDR_CELLS 4 914a0342c08SKumar Gala #define OF_BAD_ADDR ((u64)-1) 915a0342c08SKumar Gala #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \ 916a0342c08SKumar Gala (ns) > 0) 917a0342c08SKumar Gala 918a0342c08SKumar Gala /* Debug utility */ 919a0342c08SKumar Gala #ifdef DEBUG 9208aa5ec6eSKim Phillips static void of_dump_addr(const char *s, const fdt32_t *addr, int na) 921a0342c08SKumar Gala { 922a0342c08SKumar Gala printf("%s", s); 923a0342c08SKumar Gala while(na--) 924a0342c08SKumar Gala printf(" %08x", *(addr++)); 925a0342c08SKumar Gala printf("\n"); 926a0342c08SKumar Gala } 927a0342c08SKumar Gala #else 9288aa5ec6eSKim Phillips static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { } 929a0342c08SKumar Gala #endif 930a0342c08SKumar Gala 931a0342c08SKumar Gala /* Callbacks for bus specific translators */ 932a0342c08SKumar Gala struct of_bus { 933a0342c08SKumar Gala const char *name; 934a0342c08SKumar Gala const char *addresses; 9356395f318SScott Wood void (*count_cells)(void *blob, int parentoffset, 936a0342c08SKumar Gala int *addrc, int *sizec); 9378aa5ec6eSKim Phillips u64 (*map)(fdt32_t *addr, const fdt32_t *range, 938a0342c08SKumar Gala int na, int ns, int pna); 9398aa5ec6eSKim Phillips int (*translate)(fdt32_t *addr, u64 offset, int na); 940a0342c08SKumar Gala }; 941a0342c08SKumar Gala 942a0342c08SKumar Gala /* Default translator (generic bus) */ 9436395f318SScott Wood static void of_bus_default_count_cells(void *blob, int parentoffset, 944a0342c08SKumar Gala int *addrc, int *sizec) 945a0342c08SKumar Gala { 9468aa5ec6eSKim Phillips const fdt32_t *prop; 9476395f318SScott Wood 9486395f318SScott Wood if (addrc) { 9496395f318SScott Wood prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL); 9506395f318SScott Wood if (prop) 9518aa5ec6eSKim Phillips *addrc = be32_to_cpup(prop); 9526395f318SScott Wood else 9536395f318SScott Wood *addrc = 2; 9546395f318SScott Wood } 9556395f318SScott Wood 9566395f318SScott Wood if (sizec) { 9576395f318SScott Wood prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL); 9586395f318SScott Wood if (prop) 9598aa5ec6eSKim Phillips *sizec = be32_to_cpup(prop); 9606395f318SScott Wood else 9616395f318SScott Wood *sizec = 1; 9626395f318SScott Wood } 963a0342c08SKumar Gala } 964a0342c08SKumar Gala 9658aa5ec6eSKim Phillips static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range, 966a0342c08SKumar Gala int na, int ns, int pna) 967a0342c08SKumar Gala { 968a0342c08SKumar Gala u64 cp, s, da; 969a0342c08SKumar Gala 970a0342c08SKumar Gala cp = of_read_number(range, na); 971a0342c08SKumar Gala s = of_read_number(range + na + pna, ns); 972a0342c08SKumar Gala da = of_read_number(addr, na); 973a0342c08SKumar Gala 974a0342c08SKumar Gala debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n", 975a0342c08SKumar Gala cp, s, da); 976a0342c08SKumar Gala 977a0342c08SKumar Gala if (da < cp || da >= (cp + s)) 978a0342c08SKumar Gala return OF_BAD_ADDR; 979a0342c08SKumar Gala return da - cp; 980a0342c08SKumar Gala } 981a0342c08SKumar Gala 9828aa5ec6eSKim Phillips static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na) 983a0342c08SKumar Gala { 984a0342c08SKumar Gala u64 a = of_read_number(addr, na); 985a0342c08SKumar Gala memset(addr, 0, na * 4); 986a0342c08SKumar Gala a += offset; 987a0342c08SKumar Gala if (na > 1) 9888aa5ec6eSKim Phillips addr[na - 2] = cpu_to_fdt32(a >> 32); 9898aa5ec6eSKim Phillips addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu); 990a0342c08SKumar Gala 991a0342c08SKumar Gala return 0; 992a0342c08SKumar Gala } 993a0342c08SKumar Gala 994a0342c08SKumar Gala /* Array of bus specific translators */ 995a0342c08SKumar Gala static struct of_bus of_busses[] = { 996a0342c08SKumar Gala /* Default */ 997a0342c08SKumar Gala { 998a0342c08SKumar Gala .name = "default", 999a0342c08SKumar Gala .addresses = "reg", 1000a0342c08SKumar Gala .count_cells = of_bus_default_count_cells, 1001a0342c08SKumar Gala .map = of_bus_default_map, 1002a0342c08SKumar Gala .translate = of_bus_default_translate, 1003a0342c08SKumar Gala }, 1004a0342c08SKumar Gala }; 1005a0342c08SKumar Gala 1006a0342c08SKumar Gala static int of_translate_one(void * blob, int parent, struct of_bus *bus, 10078aa5ec6eSKim Phillips struct of_bus *pbus, fdt32_t *addr, 1008a0342c08SKumar Gala int na, int ns, int pna, const char *rprop) 1009a0342c08SKumar Gala { 10108aa5ec6eSKim Phillips const fdt32_t *ranges; 1011a0342c08SKumar Gala int rlen; 1012a0342c08SKumar Gala int rone; 1013a0342c08SKumar Gala u64 offset = OF_BAD_ADDR; 1014a0342c08SKumar Gala 1015a0342c08SKumar Gala /* Normally, an absence of a "ranges" property means we are 1016a0342c08SKumar Gala * crossing a non-translatable boundary, and thus the addresses 1017a0342c08SKumar Gala * below the current not cannot be converted to CPU physical ones. 1018a0342c08SKumar Gala * Unfortunately, while this is very clear in the spec, it's not 1019a0342c08SKumar Gala * what Apple understood, and they do have things like /uni-n or 1020a0342c08SKumar Gala * /ht nodes with no "ranges" property and a lot of perfectly 1021a0342c08SKumar Gala * useable mapped devices below them. Thus we treat the absence of 1022a0342c08SKumar Gala * "ranges" as equivalent to an empty "ranges" property which means 1023a0342c08SKumar Gala * a 1:1 translation at that level. It's up to the caller not to try 1024a0342c08SKumar Gala * to translate addresses that aren't supposed to be translated in 1025a0342c08SKumar Gala * the first place. --BenH. 1026a0342c08SKumar Gala */ 10278aa5ec6eSKim Phillips ranges = fdt_getprop(blob, parent, rprop, &rlen); 1028a0342c08SKumar Gala if (ranges == NULL || rlen == 0) { 1029a0342c08SKumar Gala offset = of_read_number(addr, na); 1030a0342c08SKumar Gala memset(addr, 0, pna * 4); 1031a0342c08SKumar Gala debug("OF: no ranges, 1:1 translation\n"); 1032a0342c08SKumar Gala goto finish; 1033a0342c08SKumar Gala } 1034a0342c08SKumar Gala 1035a0342c08SKumar Gala debug("OF: walking ranges...\n"); 1036a0342c08SKumar Gala 1037a0342c08SKumar Gala /* Now walk through the ranges */ 1038a0342c08SKumar Gala rlen /= 4; 1039a0342c08SKumar Gala rone = na + pna + ns; 1040a0342c08SKumar Gala for (; rlen >= rone; rlen -= rone, ranges += rone) { 1041a0342c08SKumar Gala offset = bus->map(addr, ranges, na, ns, pna); 1042a0342c08SKumar Gala if (offset != OF_BAD_ADDR) 1043a0342c08SKumar Gala break; 1044a0342c08SKumar Gala } 1045a0342c08SKumar Gala if (offset == OF_BAD_ADDR) { 1046a0342c08SKumar Gala debug("OF: not found !\n"); 1047a0342c08SKumar Gala return 1; 1048a0342c08SKumar Gala } 1049a0342c08SKumar Gala memcpy(addr, ranges + na, 4 * pna); 1050a0342c08SKumar Gala 1051a0342c08SKumar Gala finish: 1052a0342c08SKumar Gala of_dump_addr("OF: parent translation for:", addr, pna); 1053a0342c08SKumar Gala debug("OF: with offset: "PRu64"\n", offset); 1054a0342c08SKumar Gala 1055a0342c08SKumar Gala /* Translate it into parent bus space */ 1056a0342c08SKumar Gala return pbus->translate(addr, offset, pna); 1057a0342c08SKumar Gala } 1058a0342c08SKumar Gala 1059a0342c08SKumar Gala /* 1060a0342c08SKumar Gala * Translate an address from the device-tree into a CPU physical address, 1061a0342c08SKumar Gala * this walks up the tree and applies the various bus mappings on the 1062a0342c08SKumar Gala * way. 1063a0342c08SKumar Gala * 1064a0342c08SKumar Gala * Note: We consider that crossing any level with #size-cells == 0 to mean 1065a0342c08SKumar Gala * that translation is impossible (that is we are not dealing with a value 1066a0342c08SKumar Gala * that can be mapped to a cpu physical address). This is not really specified 1067a0342c08SKumar Gala * that way, but this is traditionally the way IBM at least do things 1068a0342c08SKumar Gala */ 10698aa5ec6eSKim Phillips static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr, 1070a0342c08SKumar Gala const char *rprop) 1071a0342c08SKumar Gala { 1072a0342c08SKumar Gala int parent; 1073a0342c08SKumar Gala struct of_bus *bus, *pbus; 10748aa5ec6eSKim Phillips fdt32_t addr[OF_MAX_ADDR_CELLS]; 1075a0342c08SKumar Gala int na, ns, pna, pns; 1076a0342c08SKumar Gala u64 result = OF_BAD_ADDR; 1077a0342c08SKumar Gala 1078a0342c08SKumar Gala debug("OF: ** translation for device %s **\n", 1079a0342c08SKumar Gala fdt_get_name(blob, node_offset, NULL)); 1080a0342c08SKumar Gala 1081a0342c08SKumar Gala /* Get parent & match bus type */ 1082a0342c08SKumar Gala parent = fdt_parent_offset(blob, node_offset); 1083a0342c08SKumar Gala if (parent < 0) 1084a0342c08SKumar Gala goto bail; 1085a0342c08SKumar Gala bus = &of_busses[0]; 1086a0342c08SKumar Gala 1087a0342c08SKumar Gala /* Cound address cells & copy address locally */ 10886395f318SScott Wood bus->count_cells(blob, parent, &na, &ns); 1089a0342c08SKumar Gala if (!OF_CHECK_COUNTS(na, ns)) { 1090a0342c08SKumar Gala printf("%s: Bad cell count for %s\n", __FUNCTION__, 1091a0342c08SKumar Gala fdt_get_name(blob, node_offset, NULL)); 1092a0342c08SKumar Gala goto bail; 1093a0342c08SKumar Gala } 1094a0342c08SKumar Gala memcpy(addr, in_addr, na * 4); 1095a0342c08SKumar Gala 1096a0342c08SKumar Gala debug("OF: bus is %s (na=%d, ns=%d) on %s\n", 1097a0342c08SKumar Gala bus->name, na, ns, fdt_get_name(blob, parent, NULL)); 1098a0342c08SKumar Gala of_dump_addr("OF: translating address:", addr, na); 1099a0342c08SKumar Gala 1100a0342c08SKumar Gala /* Translate */ 1101a0342c08SKumar Gala for (;;) { 1102a0342c08SKumar Gala /* Switch to parent bus */ 1103a0342c08SKumar Gala node_offset = parent; 1104a0342c08SKumar Gala parent = fdt_parent_offset(blob, node_offset); 1105a0342c08SKumar Gala 1106a0342c08SKumar Gala /* If root, we have finished */ 1107a0342c08SKumar Gala if (parent < 0) { 1108a0342c08SKumar Gala debug("OF: reached root node\n"); 1109a0342c08SKumar Gala result = of_read_number(addr, na); 1110a0342c08SKumar Gala break; 1111a0342c08SKumar Gala } 1112a0342c08SKumar Gala 1113a0342c08SKumar Gala /* Get new parent bus and counts */ 1114a0342c08SKumar Gala pbus = &of_busses[0]; 11156395f318SScott Wood pbus->count_cells(blob, parent, &pna, &pns); 1116a0342c08SKumar Gala if (!OF_CHECK_COUNTS(pna, pns)) { 1117a0342c08SKumar Gala printf("%s: Bad cell count for %s\n", __FUNCTION__, 1118a0342c08SKumar Gala fdt_get_name(blob, node_offset, NULL)); 1119a0342c08SKumar Gala break; 1120a0342c08SKumar Gala } 1121a0342c08SKumar Gala 1122a0342c08SKumar Gala debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n", 1123a0342c08SKumar Gala pbus->name, pna, pns, fdt_get_name(blob, parent, NULL)); 1124a0342c08SKumar Gala 1125a0342c08SKumar Gala /* Apply bus translation */ 1126a0342c08SKumar Gala if (of_translate_one(blob, node_offset, bus, pbus, 1127a0342c08SKumar Gala addr, na, ns, pna, rprop)) 1128a0342c08SKumar Gala break; 1129a0342c08SKumar Gala 1130a0342c08SKumar Gala /* Complete the move up one level */ 1131a0342c08SKumar Gala na = pna; 1132a0342c08SKumar Gala ns = pns; 1133a0342c08SKumar Gala bus = pbus; 1134a0342c08SKumar Gala 1135a0342c08SKumar Gala of_dump_addr("OF: one level translation:", addr, na); 1136a0342c08SKumar Gala } 1137a0342c08SKumar Gala bail: 1138a0342c08SKumar Gala 1139a0342c08SKumar Gala return result; 1140a0342c08SKumar Gala } 1141a0342c08SKumar Gala 11428aa5ec6eSKim Phillips u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr) 1143a0342c08SKumar Gala { 1144a0342c08SKumar Gala return __of_translate_address(blob, node_offset, in_addr, "ranges"); 1145a0342c08SKumar Gala } 114675e73afdSKumar Gala 114775e73afdSKumar Gala /** 114875e73afdSKumar Gala * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and 114975e73afdSKumar Gala * who's reg property matches a physical cpu address 115075e73afdSKumar Gala * 115175e73afdSKumar Gala * @blob: ptr to device tree 115275e73afdSKumar Gala * @compat: compatiable string to match 115375e73afdSKumar Gala * @compat_off: property name 115475e73afdSKumar Gala * 115575e73afdSKumar Gala */ 115675e73afdSKumar Gala int fdt_node_offset_by_compat_reg(void *blob, const char *compat, 115775e73afdSKumar Gala phys_addr_t compat_off) 115875e73afdSKumar Gala { 115975e73afdSKumar Gala int len, off = fdt_node_offset_by_compatible(blob, -1, compat); 116075e73afdSKumar Gala while (off != -FDT_ERR_NOTFOUND) { 11618aa5ec6eSKim Phillips const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len); 116275e73afdSKumar Gala if (reg) { 116375e73afdSKumar Gala if (compat_off == fdt_translate_address(blob, off, reg)) 116475e73afdSKumar Gala return off; 116575e73afdSKumar Gala } 116675e73afdSKumar Gala off = fdt_node_offset_by_compatible(blob, off, compat); 116775e73afdSKumar Gala } 116875e73afdSKumar Gala 116975e73afdSKumar Gala return -FDT_ERR_NOTFOUND; 117075e73afdSKumar Gala } 117175e73afdSKumar Gala 1172b4b847e9SKumar Gala /** 1173b4b847e9SKumar Gala * fdt_alloc_phandle: Return next free phandle value 1174b4b847e9SKumar Gala * 1175b4b847e9SKumar Gala * @blob: ptr to device tree 1176b4b847e9SKumar Gala */ 1177b4b847e9SKumar Gala int fdt_alloc_phandle(void *blob) 1178b4b847e9SKumar Gala { 117950bf17bdSTimur Tabi int offset, phandle = 0; 118075e73afdSKumar Gala 1181b4b847e9SKumar Gala for (offset = fdt_next_node(blob, -1, NULL); offset >= 0; 1182b4b847e9SKumar Gala offset = fdt_next_node(blob, offset, NULL)) { 118350bf17bdSTimur Tabi phandle = max(phandle, fdt_get_phandle(blob, offset)); 1184b4b847e9SKumar Gala } 1185b4b847e9SKumar Gala 1186b4b847e9SKumar Gala return phandle + 1; 1187b4b847e9SKumar Gala } 1188beca5a5fSAnatolij Gustschin 1189a8d2a75dSGerald Van Baren /* 1190f117c0f0SKumar Gala * fdt_set_phandle: Create a phandle property for the given node 1191a8d2a75dSGerald Van Baren * 1192a8d2a75dSGerald Van Baren * @fdt: ptr to device tree 1193a8d2a75dSGerald Van Baren * @nodeoffset: node to update 1194a8d2a75dSGerald Van Baren * @phandle: phandle value to set (must be unique) 1195a8d2a75dSGerald Van Baren */ 1196f117c0f0SKumar Gala int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle) 1197a8d2a75dSGerald Van Baren { 1198a8d2a75dSGerald Van Baren int ret; 1199a8d2a75dSGerald Van Baren 1200a8d2a75dSGerald Van Baren #ifdef DEBUG 1201a8d2a75dSGerald Van Baren int off = fdt_node_offset_by_phandle(fdt, phandle); 1202a8d2a75dSGerald Van Baren 1203a8d2a75dSGerald Van Baren if ((off >= 0) && (off != nodeoffset)) { 1204a8d2a75dSGerald Van Baren char buf[64]; 1205a8d2a75dSGerald Van Baren 1206a8d2a75dSGerald Van Baren fdt_get_path(fdt, nodeoffset, buf, sizeof(buf)); 1207a8d2a75dSGerald Van Baren printf("Trying to update node %s with phandle %u ", 1208a8d2a75dSGerald Van Baren buf, phandle); 1209a8d2a75dSGerald Van Baren 1210a8d2a75dSGerald Van Baren fdt_get_path(fdt, off, buf, sizeof(buf)); 1211a8d2a75dSGerald Van Baren printf("that already exists in node %s.\n", buf); 1212a8d2a75dSGerald Van Baren return -FDT_ERR_BADPHANDLE; 1213a8d2a75dSGerald Van Baren } 1214a8d2a75dSGerald Van Baren #endif 1215a8d2a75dSGerald Van Baren 1216a8d2a75dSGerald Van Baren ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle); 1217a8d2a75dSGerald Van Baren if (ret < 0) 1218a8d2a75dSGerald Van Baren return ret; 1219a8d2a75dSGerald Van Baren 1220a8d2a75dSGerald Van Baren /* 1221a8d2a75dSGerald Van Baren * For now, also set the deprecated "linux,phandle" property, so that we 1222a8d2a75dSGerald Van Baren * don't break older kernels. 1223a8d2a75dSGerald Van Baren */ 1224a8d2a75dSGerald Van Baren ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle); 1225a8d2a75dSGerald Van Baren 1226a8d2a75dSGerald Van Baren return ret; 1227a8d2a75dSGerald Van Baren } 1228a8d2a75dSGerald Van Baren 122910aeabd1SKumar Gala /* 123010aeabd1SKumar Gala * fdt_create_phandle: Create a phandle property for the given node 123110aeabd1SKumar Gala * 123210aeabd1SKumar Gala * @fdt: ptr to device tree 123310aeabd1SKumar Gala * @nodeoffset: node to update 123410aeabd1SKumar Gala */ 12353c927cccSTimur Tabi unsigned int fdt_create_phandle(void *fdt, int nodeoffset) 123610aeabd1SKumar Gala { 123710aeabd1SKumar Gala /* see if there is a phandle already */ 123810aeabd1SKumar Gala int phandle = fdt_get_phandle(fdt, nodeoffset); 123910aeabd1SKumar Gala 124010aeabd1SKumar Gala /* if we got 0, means no phandle so create one */ 124110aeabd1SKumar Gala if (phandle == 0) { 12423c927cccSTimur Tabi int ret; 12433c927cccSTimur Tabi 124410aeabd1SKumar Gala phandle = fdt_alloc_phandle(fdt); 12453c927cccSTimur Tabi ret = fdt_set_phandle(fdt, nodeoffset, phandle); 12463c927cccSTimur Tabi if (ret < 0) { 12473c927cccSTimur Tabi printf("Can't set phandle %u: %s\n", phandle, 12483c927cccSTimur Tabi fdt_strerror(ret)); 12493c927cccSTimur Tabi return 0; 12503c927cccSTimur Tabi } 125110aeabd1SKumar Gala } 125210aeabd1SKumar Gala 125310aeabd1SKumar Gala return phandle; 125410aeabd1SKumar Gala } 125510aeabd1SKumar Gala 12562a523f52SShengzhou Liu /* 12572a523f52SShengzhou Liu * fdt_set_node_status: Set status for the given node 12582a523f52SShengzhou Liu * 12592a523f52SShengzhou Liu * @fdt: ptr to device tree 12602a523f52SShengzhou Liu * @nodeoffset: node to update 12612a523f52SShengzhou Liu * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, 12622a523f52SShengzhou Liu * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE 12632a523f52SShengzhou Liu * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE 12642a523f52SShengzhou Liu */ 12652a523f52SShengzhou Liu int fdt_set_node_status(void *fdt, int nodeoffset, 12662a523f52SShengzhou Liu enum fdt_status status, unsigned int error_code) 12672a523f52SShengzhou Liu { 12682a523f52SShengzhou Liu char buf[16]; 12692a523f52SShengzhou Liu int ret = 0; 12702a523f52SShengzhou Liu 12712a523f52SShengzhou Liu if (nodeoffset < 0) 12722a523f52SShengzhou Liu return nodeoffset; 12732a523f52SShengzhou Liu 12742a523f52SShengzhou Liu switch (status) { 12752a523f52SShengzhou Liu case FDT_STATUS_OKAY: 12762a523f52SShengzhou Liu ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay"); 12772a523f52SShengzhou Liu break; 12782a523f52SShengzhou Liu case FDT_STATUS_DISABLED: 12792a523f52SShengzhou Liu ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled"); 12802a523f52SShengzhou Liu break; 12812a523f52SShengzhou Liu case FDT_STATUS_FAIL: 12822a523f52SShengzhou Liu ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail"); 12832a523f52SShengzhou Liu break; 12842a523f52SShengzhou Liu case FDT_STATUS_FAIL_ERROR_CODE: 12852a523f52SShengzhou Liu sprintf(buf, "fail-%d", error_code); 12862a523f52SShengzhou Liu ret = fdt_setprop_string(fdt, nodeoffset, "status", buf); 12872a523f52SShengzhou Liu break; 12882a523f52SShengzhou Liu default: 12892a523f52SShengzhou Liu printf("Invalid fdt status: %x\n", status); 12902a523f52SShengzhou Liu ret = -1; 12912a523f52SShengzhou Liu break; 12922a523f52SShengzhou Liu } 12932a523f52SShengzhou Liu 12942a523f52SShengzhou Liu return ret; 12952a523f52SShengzhou Liu } 12962a523f52SShengzhou Liu 12972a523f52SShengzhou Liu /* 12982a523f52SShengzhou Liu * fdt_set_status_by_alias: Set status for the given node given an alias 12992a523f52SShengzhou Liu * 13002a523f52SShengzhou Liu * @fdt: ptr to device tree 13012a523f52SShengzhou Liu * @alias: alias of node to update 13022a523f52SShengzhou Liu * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, 13032a523f52SShengzhou Liu * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE 13042a523f52SShengzhou Liu * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE 13052a523f52SShengzhou Liu */ 13062a523f52SShengzhou Liu int fdt_set_status_by_alias(void *fdt, const char* alias, 13072a523f52SShengzhou Liu enum fdt_status status, unsigned int error_code) 13082a523f52SShengzhou Liu { 13092a523f52SShengzhou Liu int offset = fdt_path_offset(fdt, alias); 13102a523f52SShengzhou Liu 13112a523f52SShengzhou Liu return fdt_set_node_status(fdt, offset, status, error_code); 13122a523f52SShengzhou Liu } 13132a523f52SShengzhou Liu 1314096eb3f5STom Wai-Hong Tam #if defined(CONFIG_VIDEO) || defined(CONFIG_LCD) 1315beca5a5fSAnatolij Gustschin int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf) 1316beca5a5fSAnatolij Gustschin { 1317beca5a5fSAnatolij Gustschin int noff; 1318beca5a5fSAnatolij Gustschin int ret; 1319beca5a5fSAnatolij Gustschin 1320beca5a5fSAnatolij Gustschin noff = fdt_node_offset_by_compatible(blob, -1, compat); 1321beca5a5fSAnatolij Gustschin if (noff != -FDT_ERR_NOTFOUND) { 1322beca5a5fSAnatolij Gustschin debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat); 1323beca5a5fSAnatolij Gustschin add_edid: 1324beca5a5fSAnatolij Gustschin ret = fdt_setprop(blob, noff, "edid", edid_buf, 128); 1325beca5a5fSAnatolij Gustschin if (ret == -FDT_ERR_NOSPACE) { 1326beca5a5fSAnatolij Gustschin ret = fdt_increase_size(blob, 512); 1327beca5a5fSAnatolij Gustschin if (!ret) 1328beca5a5fSAnatolij Gustschin goto add_edid; 1329beca5a5fSAnatolij Gustschin else 1330beca5a5fSAnatolij Gustschin goto err_size; 1331beca5a5fSAnatolij Gustschin } else if (ret < 0) { 1332beca5a5fSAnatolij Gustschin printf("Can't add property: %s\n", fdt_strerror(ret)); 1333beca5a5fSAnatolij Gustschin return ret; 1334beca5a5fSAnatolij Gustschin } 1335beca5a5fSAnatolij Gustschin } 1336beca5a5fSAnatolij Gustschin return 0; 1337beca5a5fSAnatolij Gustschin err_size: 1338beca5a5fSAnatolij Gustschin printf("Can't increase blob size: %s\n", fdt_strerror(ret)); 1339beca5a5fSAnatolij Gustschin return ret; 1340beca5a5fSAnatolij Gustschin } 1341beca5a5fSAnatolij Gustschin #endif 1342bb682001STimur Tabi 1343bb682001STimur Tabi /* 1344bb682001STimur Tabi * Verify the physical address of device tree node for a given alias 1345bb682001STimur Tabi * 1346bb682001STimur Tabi * This function locates the device tree node of a given alias, and then 1347bb682001STimur Tabi * verifies that the physical address of that device matches the given 1348bb682001STimur Tabi * parameter. It displays a message if there is a mismatch. 1349bb682001STimur Tabi * 1350bb682001STimur Tabi * Returns 1 on success, 0 on failure 1351bb682001STimur Tabi */ 1352bb682001STimur Tabi int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr) 1353bb682001STimur Tabi { 1354bb682001STimur Tabi const char *path; 13558aa5ec6eSKim Phillips const fdt32_t *reg; 1356bb682001STimur Tabi int node, len; 1357bb682001STimur Tabi u64 dt_addr; 1358bb682001STimur Tabi 1359bb682001STimur Tabi path = fdt_getprop(fdt, anode, alias, NULL); 1360bb682001STimur Tabi if (!path) { 1361bb682001STimur Tabi /* If there's no such alias, then it's not a failure */ 1362bb682001STimur Tabi return 1; 1363bb682001STimur Tabi } 1364bb682001STimur Tabi 1365bb682001STimur Tabi node = fdt_path_offset(fdt, path); 1366bb682001STimur Tabi if (node < 0) { 1367bb682001STimur Tabi printf("Warning: device tree alias '%s' points to invalid " 1368bb682001STimur Tabi "node %s.\n", alias, path); 1369bb682001STimur Tabi return 0; 1370bb682001STimur Tabi } 1371bb682001STimur Tabi 1372bb682001STimur Tabi reg = fdt_getprop(fdt, node, "reg", &len); 1373bb682001STimur Tabi if (!reg) { 1374bb682001STimur Tabi printf("Warning: device tree node '%s' has no address.\n", 1375bb682001STimur Tabi path); 1376bb682001STimur Tabi return 0; 1377bb682001STimur Tabi } 1378bb682001STimur Tabi 1379bb682001STimur Tabi dt_addr = fdt_translate_address(fdt, node, reg); 1380bb682001STimur Tabi if (addr != dt_addr) { 1381bb682001STimur Tabi printf("Warning: U-Boot configured device %s at address %llx,\n" 1382bb682001STimur Tabi " but the device tree has it address %llx.\n", 1383bb682001STimur Tabi alias, addr, dt_addr); 1384bb682001STimur Tabi return 0; 1385bb682001STimur Tabi } 1386bb682001STimur Tabi 1387bb682001STimur Tabi return 1; 1388bb682001STimur Tabi } 1389bb682001STimur Tabi 1390bb682001STimur Tabi /* 1391bb682001STimur Tabi * Returns the base address of an SOC or PCI node 1392bb682001STimur Tabi */ 1393bb682001STimur Tabi u64 fdt_get_base_address(void *fdt, int node) 1394bb682001STimur Tabi { 1395bb682001STimur Tabi int size; 1396bb682001STimur Tabi u32 naddr; 13978aa5ec6eSKim Phillips const fdt32_t *prop; 1398bb682001STimur Tabi 1399bb682001STimur Tabi prop = fdt_getprop(fdt, node, "#address-cells", &size); 1400bb682001STimur Tabi if (prop && size == 4) 14018aa5ec6eSKim Phillips naddr = be32_to_cpup(prop); 1402bb682001STimur Tabi else 1403bb682001STimur Tabi naddr = 2; 1404bb682001STimur Tabi 1405bb682001STimur Tabi prop = fdt_getprop(fdt, node, "ranges", &size); 1406bb682001STimur Tabi 1407bb682001STimur Tabi return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0; 1408bb682001STimur Tabi } 1409