fdt_support.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. /*
  2. * (C) Copyright 2007
  3. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
  4. *
  5. * Copyright 2010-2011 Freescale Semiconductor, Inc.
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <inttypes.h>
  11. #include <stdio_dev.h>
  12. #include <linux/ctype.h>
  13. #include <linux/types.h>
  14. #include <asm/global_data.h>
  15. #include <libfdt.h>
  16. #include <fdt_support.h>
  17. #include <exports.h>
  18. #include <fdtdec.h>
  19. /**
  20. * fdt_getprop_u32_default_node - Return a node's property or a default
  21. *
  22. * @fdt: ptr to device tree
  23. * @off: offset of node
  24. * @cell: cell offset in property
  25. * @prop: property name
  26. * @dflt: default value if the property isn't found
  27. *
  28. * Convenience function to return a node's property or a default value if
  29. * the property doesn't exist.
  30. */
  31. u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
  32. const char *prop, const u32 dflt)
  33. {
  34. const fdt32_t *val;
  35. int len;
  36. val = fdt_getprop(fdt, off, prop, &len);
  37. /* Check if property exists */
  38. if (!val)
  39. return dflt;
  40. /* Check if property is long enough */
  41. if (len < ((cell + 1) * sizeof(uint32_t)))
  42. return dflt;
  43. return fdt32_to_cpu(*val);
  44. }
  45. /**
  46. * fdt_getprop_u32_default - Find a node and return it's property or a default
  47. *
  48. * @fdt: ptr to device tree
  49. * @path: path of node
  50. * @prop: property name
  51. * @dflt: default value if the property isn't found
  52. *
  53. * Convenience function to find a node and return it's property or a
  54. * default value if it doesn't exist.
  55. */
  56. u32 fdt_getprop_u32_default(const void *fdt, const char *path,
  57. const char *prop, const u32 dflt)
  58. {
  59. int off;
  60. off = fdt_path_offset(fdt, path);
  61. if (off < 0)
  62. return dflt;
  63. return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
  64. }
  65. /**
  66. * fdt_find_and_setprop: Find a node and set it's property
  67. *
  68. * @fdt: ptr to device tree
  69. * @node: path of node
  70. * @prop: property name
  71. * @val: ptr to new value
  72. * @len: length of new property value
  73. * @create: flag to create the property if it doesn't exist
  74. *
  75. * Convenience function to directly set a property given the path to the node.
  76. */
  77. int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
  78. const void *val, int len, int create)
  79. {
  80. int nodeoff = fdt_path_offset(fdt, node);
  81. if (nodeoff < 0)
  82. return nodeoff;
  83. if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
  84. return 0; /* create flag not set; so exit quietly */
  85. return fdt_setprop(fdt, nodeoff, prop, val, len);
  86. }
  87. /**
  88. * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node
  89. *
  90. * @fdt: pointer to the device tree blob
  91. * @parentoffset: structure block offset of a node
  92. * @name: name of the subnode to locate
  93. *
  94. * fdt_subnode_offset() finds a subnode of the node with a given name.
  95. * If the subnode does not exist, it will be created.
  96. */
  97. int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name)
  98. {
  99. int offset;
  100. offset = fdt_subnode_offset(fdt, parentoffset, name);
  101. if (offset == -FDT_ERR_NOTFOUND)
  102. offset = fdt_add_subnode(fdt, parentoffset, name);
  103. if (offset < 0)
  104. printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
  105. return offset;
  106. }
  107. /* rename to CONFIG_OF_STDOUT_PATH ? */
  108. #if defined(OF_STDOUT_PATH)
  109. static int fdt_fixup_stdout(void *fdt, int chosenoff)
  110. {
  111. return fdt_setprop(fdt, chosenoff, "linux,stdout-path",
  112. OF_STDOUT_PATH, strlen(OF_STDOUT_PATH) + 1);
  113. }
  114. #elif defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
  115. static void fdt_fill_multisername(char *sername, size_t maxlen)
  116. {
  117. const char *outname = stdio_devices[stdout]->name;
  118. if (strcmp(outname, "serial") > 0)
  119. strncpy(sername, outname, maxlen);
  120. /* eserial? */
  121. if (strcmp(outname + 1, "serial") > 0)
  122. strncpy(sername, outname + 1, maxlen);
  123. }
  124. static int fdt_fixup_stdout(void *fdt, int chosenoff)
  125. {
  126. int err;
  127. int aliasoff;
  128. char sername[9] = { 0 };
  129. const void *path;
  130. int len;
  131. char tmp[256]; /* long enough */
  132. fdt_fill_multisername(sername, sizeof(sername) - 1);
  133. if (!sername[0])
  134. sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
  135. aliasoff = fdt_path_offset(fdt, "/aliases");
  136. if (aliasoff < 0) {
  137. err = aliasoff;
  138. goto noalias;
  139. }
  140. path = fdt_getprop(fdt, aliasoff, sername, &len);
  141. if (!path) {
  142. err = len;
  143. goto noalias;
  144. }
  145. /* fdt_setprop may break "path" so we copy it to tmp buffer */
  146. memcpy(tmp, path, len);
  147. err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
  148. if (err < 0)
  149. printf("WARNING: could not set linux,stdout-path %s.\n",
  150. fdt_strerror(err));
  151. return err;
  152. noalias:
  153. printf("WARNING: %s: could not read %s alias: %s\n",
  154. __func__, sername, fdt_strerror(err));
  155. return 0;
  156. }
  157. #else
  158. static int fdt_fixup_stdout(void *fdt, int chosenoff)
  159. {
  160. return 0;
  161. }
  162. #endif
  163. static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
  164. uint64_t val, int is_u64)
  165. {
  166. if (is_u64)
  167. return fdt_setprop_u64(fdt, nodeoffset, name, val);
  168. else
  169. return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
  170. }
  171. int fdt_root(void *fdt)
  172. {
  173. char *serial;
  174. int err;
  175. err = fdt_check_header(fdt);
  176. if (err < 0) {
  177. printf("fdt_root: %s\n", fdt_strerror(err));
  178. return err;
  179. }
  180. serial = getenv("serial#");
  181. if (serial) {
  182. err = fdt_setprop(fdt, 0, "serial-number", serial,
  183. strlen(serial) + 1);
  184. if (err < 0) {
  185. printf("WARNING: could not set serial-number %s.\n",
  186. fdt_strerror(err));
  187. return err;
  188. }
  189. }
  190. return 0;
  191. }
  192. int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
  193. {
  194. int nodeoffset;
  195. int err, j, total;
  196. int is_u64;
  197. uint64_t addr, size;
  198. /* just return if the size of initrd is zero */
  199. if (initrd_start == initrd_end)
  200. return 0;
  201. /* find or create "/chosen" node. */
  202. nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
  203. if (nodeoffset < 0)
  204. return nodeoffset;
  205. total = fdt_num_mem_rsv(fdt);
  206. /*
  207. * Look for an existing entry and update it. If we don't find
  208. * the entry, we will j be the next available slot.
  209. */
  210. for (j = 0; j < total; j++) {
  211. err = fdt_get_mem_rsv(fdt, j, &addr, &size);
  212. if (addr == initrd_start) {
  213. fdt_del_mem_rsv(fdt, j);
  214. break;
  215. }
  216. }
  217. err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
  218. if (err < 0) {
  219. printf("fdt_initrd: %s\n", fdt_strerror(err));
  220. return err;
  221. }
  222. is_u64 = (fdt_address_cells(fdt, 0) == 2);
  223. err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
  224. (uint64_t)initrd_start, is_u64);
  225. if (err < 0) {
  226. printf("WARNING: could not set linux,initrd-start %s.\n",
  227. fdt_strerror(err));
  228. return err;
  229. }
  230. err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
  231. (uint64_t)initrd_end, is_u64);
  232. if (err < 0) {
  233. printf("WARNING: could not set linux,initrd-end %s.\n",
  234. fdt_strerror(err));
  235. return err;
  236. }
  237. return 0;
  238. }
  239. int fdt_chosen(void *fdt)
  240. {
  241. int nodeoffset;
  242. int err;
  243. char *str; /* used to set string properties */
  244. err = fdt_check_header(fdt);
  245. if (err < 0) {
  246. printf("fdt_chosen: %s\n", fdt_strerror(err));
  247. return err;
  248. }
  249. /* find or create "/chosen" node. */
  250. nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
  251. if (nodeoffset < 0)
  252. return nodeoffset;
  253. str = getenv("bootargs");
  254. if (str) {
  255. err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
  256. strlen(str) + 1);
  257. if (err < 0) {
  258. printf("WARNING: could not set bootargs %s.\n",
  259. fdt_strerror(err));
  260. return err;
  261. }
  262. }
  263. return fdt_fixup_stdout(fdt, nodeoffset);
  264. }
  265. void do_fixup_by_path(void *fdt, const char *path, const char *prop,
  266. const void *val, int len, int create)
  267. {
  268. #if defined(DEBUG)
  269. int i;
  270. debug("Updating property '%s/%s' = ", path, prop);
  271. for (i = 0; i < len; i++)
  272. debug(" %.2x", *(u8*)(val+i));
  273. debug("\n");
  274. #endif
  275. int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
  276. if (rc)
  277. printf("Unable to update property %s:%s, err=%s\n",
  278. path, prop, fdt_strerror(rc));
  279. }
  280. void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
  281. u32 val, int create)
  282. {
  283. fdt32_t tmp = cpu_to_fdt32(val);
  284. do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
  285. }
  286. void do_fixup_by_prop(void *fdt,
  287. const char *pname, const void *pval, int plen,
  288. const char *prop, const void *val, int len,
  289. int create)
  290. {
  291. int off;
  292. #if defined(DEBUG)
  293. int i;
  294. debug("Updating property '%s' = ", prop);
  295. for (i = 0; i < len; i++)
  296. debug(" %.2x", *(u8*)(val+i));
  297. debug("\n");
  298. #endif
  299. off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
  300. while (off != -FDT_ERR_NOTFOUND) {
  301. if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
  302. fdt_setprop(fdt, off, prop, val, len);
  303. off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
  304. }
  305. }
  306. void do_fixup_by_prop_u32(void *fdt,
  307. const char *pname, const void *pval, int plen,
  308. const char *prop, u32 val, int create)
  309. {
  310. fdt32_t tmp = cpu_to_fdt32(val);
  311. do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
  312. }
  313. void do_fixup_by_compat(void *fdt, const char *compat,
  314. const char *prop, const void *val, int len, int create)
  315. {
  316. int off = -1;
  317. #if defined(DEBUG)
  318. int i;
  319. debug("Updating property '%s' = ", prop);
  320. for (i = 0; i < len; i++)
  321. debug(" %.2x", *(u8*)(val+i));
  322. debug("\n");
  323. #endif
  324. off = fdt_node_offset_by_compatible(fdt, -1, compat);
  325. while (off != -FDT_ERR_NOTFOUND) {
  326. if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
  327. fdt_setprop(fdt, off, prop, val, len);
  328. off = fdt_node_offset_by_compatible(fdt, off, compat);
  329. }
  330. }
  331. void do_fixup_by_compat_u32(void *fdt, const char *compat,
  332. const char *prop, u32 val, int create)
  333. {
  334. fdt32_t tmp = cpu_to_fdt32(val);
  335. do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
  336. }
  337. /*
  338. * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
  339. */
  340. static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
  341. int n)
  342. {
  343. int i;
  344. int address_cells = fdt_address_cells(fdt, 0);
  345. int size_cells = fdt_size_cells(fdt, 0);
  346. char *p = buf;
  347. for (i = 0; i < n; i++) {
  348. if (address_cells == 2)
  349. *(fdt64_t *)p = cpu_to_fdt64(address[i]);
  350. else
  351. *(fdt32_t *)p = cpu_to_fdt32(address[i]);
  352. p += 4 * address_cells;
  353. if (size_cells == 2)
  354. *(fdt64_t *)p = cpu_to_fdt64(size[i]);
  355. else
  356. *(fdt32_t *)p = cpu_to_fdt32(size[i]);
  357. p += 4 * size_cells;
  358. }
  359. return p - (char *)buf;
  360. }
  361. #ifdef CONFIG_NR_DRAM_BANKS
  362. #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
  363. #else
  364. #define MEMORY_BANKS_MAX 4
  365. #endif
  366. int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
  367. {
  368. int err, nodeoffset;
  369. int len;
  370. u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
  371. if (banks > MEMORY_BANKS_MAX) {
  372. printf("%s: num banks %d exceeds hardcoded limit %d."
  373. " Recompile with higher MEMORY_BANKS_MAX?\n",
  374. __FUNCTION__, banks, MEMORY_BANKS_MAX);
  375. return -1;
  376. }
  377. err = fdt_check_header(blob);
  378. if (err < 0) {
  379. printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
  380. return err;
  381. }
  382. /* find or create "/memory" node. */
  383. nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
  384. if (nodeoffset < 0)
  385. return nodeoffset;
  386. err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
  387. sizeof("memory"));
  388. if (err < 0) {
  389. printf("WARNING: could not set %s %s.\n", "device_type",
  390. fdt_strerror(err));
  391. return err;
  392. }
  393. if (!banks)
  394. return 0;
  395. len = fdt_pack_reg(blob, tmp, start, size, banks);
  396. err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
  397. if (err < 0) {
  398. printf("WARNING: could not set %s %s.\n",
  399. "reg", fdt_strerror(err));
  400. return err;
  401. }
  402. return 0;
  403. }
  404. int fdt_fixup_memory(void *blob, u64 start, u64 size)
  405. {
  406. return fdt_fixup_memory_banks(blob, &start, &size, 1);
  407. }
  408. void fdt_fixup_ethernet(void *fdt)
  409. {
  410. int node, i, j;
  411. char *tmp, *end;
  412. char mac[16];
  413. const char *path;
  414. unsigned char mac_addr[6];
  415. int offset;
  416. node = fdt_path_offset(fdt, "/aliases");
  417. if (node < 0)
  418. return;
  419. for (offset = fdt_first_property_offset(fdt, node);
  420. offset > 0;
  421. offset = fdt_next_property_offset(fdt, offset)) {
  422. const char *name;
  423. int len = strlen("ethernet");
  424. path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
  425. if (!strncmp(name, "ethernet", len)) {
  426. i = trailing_strtol(name);
  427. if (i != -1) {
  428. if (i == 0)
  429. strcpy(mac, "ethaddr");
  430. else
  431. sprintf(mac, "eth%daddr", i);
  432. } else {
  433. continue;
  434. }
  435. tmp = getenv(mac);
  436. if (!tmp)
  437. continue;
  438. for (j = 0; j < 6; j++) {
  439. mac_addr[j] = tmp ?
  440. simple_strtoul(tmp, &end, 16) : 0;
  441. if (tmp)
  442. tmp = (*end) ? end + 1 : end;
  443. }
  444. do_fixup_by_path(fdt, path, "mac-address",
  445. &mac_addr, 6, 0);
  446. do_fixup_by_path(fdt, path, "local-mac-address",
  447. &mac_addr, 6, 1);
  448. }
  449. }
  450. }
  451. /* Resize the fdt to its actual size + a bit of padding */
  452. int fdt_shrink_to_minimum(void *blob)
  453. {
  454. int i;
  455. uint64_t addr, size;
  456. int total, ret;
  457. uint actualsize;
  458. if (!blob)
  459. return 0;
  460. total = fdt_num_mem_rsv(blob);
  461. for (i = 0; i < total; i++) {
  462. fdt_get_mem_rsv(blob, i, &addr, &size);
  463. if (addr == (uintptr_t)blob) {
  464. fdt_del_mem_rsv(blob, i);
  465. break;
  466. }
  467. }
  468. /*
  469. * Calculate the actual size of the fdt
  470. * plus the size needed for 5 fdt_add_mem_rsv, one
  471. * for the fdt itself and 4 for a possible initrd
  472. * ((initrd-start + initrd-end) * 2 (name & value))
  473. */
  474. actualsize = fdt_off_dt_strings(blob) +
  475. fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
  476. /* Make it so the fdt ends on a page boundary */
  477. actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
  478. actualsize = actualsize - ((uintptr_t)blob & 0xfff);
  479. /* Change the fdt header to reflect the correct size */
  480. fdt_set_totalsize(blob, actualsize);
  481. /* Add the new reservation */
  482. ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
  483. if (ret < 0)
  484. return ret;
  485. return actualsize;
  486. }
  487. #ifdef CONFIG_PCI
  488. #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
  489. #define FDT_PCI_PREFETCH (0x40000000)
  490. #define FDT_PCI_MEM32 (0x02000000)
  491. #define FDT_PCI_IO (0x01000000)
  492. #define FDT_PCI_MEM64 (0x03000000)
  493. int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
  494. int addrcell, sizecell, len, r;
  495. u32 *dma_range;
  496. /* sized based on pci addr cells, size-cells, & address-cells */
  497. u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
  498. addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
  499. sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
  500. dma_range = &dma_ranges[0];
  501. for (r = 0; r < hose->region_count; r++) {
  502. u64 bus_start, phys_start, size;
  503. /* skip if !PCI_REGION_SYS_MEMORY */
  504. if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
  505. continue;
  506. bus_start = (u64)hose->regions[r].bus_start;
  507. phys_start = (u64)hose->regions[r].phys_start;
  508. size = (u64)hose->regions[r].size;
  509. dma_range[0] = 0;
  510. if (size >= 0x100000000ull)
  511. dma_range[0] |= FDT_PCI_MEM64;
  512. else
  513. dma_range[0] |= FDT_PCI_MEM32;
  514. if (hose->regions[r].flags & PCI_REGION_PREFETCH)
  515. dma_range[0] |= FDT_PCI_PREFETCH;
  516. #ifdef CONFIG_SYS_PCI_64BIT
  517. dma_range[1] = bus_start >> 32;
  518. #else
  519. dma_range[1] = 0;
  520. #endif
  521. dma_range[2] = bus_start & 0xffffffff;
  522. if (addrcell == 2) {
  523. dma_range[3] = phys_start >> 32;
  524. dma_range[4] = phys_start & 0xffffffff;
  525. } else {
  526. dma_range[3] = phys_start & 0xffffffff;
  527. }
  528. if (sizecell == 2) {
  529. dma_range[3 + addrcell + 0] = size >> 32;
  530. dma_range[3 + addrcell + 1] = size & 0xffffffff;
  531. } else {
  532. dma_range[3 + addrcell + 0] = size & 0xffffffff;
  533. }
  534. dma_range += (3 + addrcell + sizecell);
  535. }
  536. len = dma_range - &dma_ranges[0];
  537. if (len)
  538. fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
  539. return 0;
  540. }
  541. #endif
  542. #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
  543. /*
  544. * Provide a weak default function to return the flash bank size.
  545. * There might be multiple non-identical flash chips connected to one
  546. * chip-select, so we need to pass an index as well.
  547. */
  548. u32 __flash_get_bank_size(int cs, int idx)
  549. {
  550. extern flash_info_t flash_info[];
  551. /*
  552. * As default, a simple 1:1 mapping is provided. Boards with
  553. * a different mapping need to supply a board specific mapping
  554. * routine.
  555. */
  556. return flash_info[cs].size;
  557. }
  558. u32 flash_get_bank_size(int cs, int idx)
  559. __attribute__((weak, alias("__flash_get_bank_size")));
  560. /*
  561. * This function can be used to update the size in the "reg" property
  562. * of all NOR FLASH device nodes. This is necessary for boards with
  563. * non-fixed NOR FLASH sizes.
  564. */
  565. int fdt_fixup_nor_flash_size(void *blob)
  566. {
  567. char compat[][16] = { "cfi-flash", "jedec-flash" };
  568. int off;
  569. int len;
  570. struct fdt_property *prop;
  571. u32 *reg, *reg2;
  572. int i;
  573. for (i = 0; i < 2; i++) {
  574. off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
  575. while (off != -FDT_ERR_NOTFOUND) {
  576. int idx;
  577. /*
  578. * Found one compatible node, so fixup the size
  579. * int its reg properties
  580. */
  581. prop = fdt_get_property_w(blob, off, "reg", &len);
  582. if (prop) {
  583. int tuple_size = 3 * sizeof(reg);
  584. /*
  585. * There might be multiple reg-tuples,
  586. * so loop through them all
  587. */
  588. reg = reg2 = (u32 *)&prop->data[0];
  589. for (idx = 0; idx < (len / tuple_size); idx++) {
  590. /*
  591. * Update size in reg property
  592. */
  593. reg[2] = flash_get_bank_size(reg[0],
  594. idx);
  595. /*
  596. * Point to next reg tuple
  597. */
  598. reg += 3;
  599. }
  600. fdt_setprop(blob, off, "reg", reg2, len);
  601. }
  602. /* Move to next compatible node */
  603. off = fdt_node_offset_by_compatible(blob, off,
  604. compat[i]);
  605. }
  606. }
  607. return 0;
  608. }
  609. #endif
  610. int fdt_increase_size(void *fdt, int add_len)
  611. {
  612. int newlen;
  613. newlen = fdt_totalsize(fdt) + add_len;
  614. /* Open in place with a new len */
  615. return fdt_open_into(fdt, fdt, newlen);
  616. }
  617. #ifdef CONFIG_FDT_FIXUP_PARTITIONS
  618. #include <jffs2/load_kernel.h>
  619. #include <mtd_node.h>
  620. struct reg_cell {
  621. unsigned int r0;
  622. unsigned int r1;
  623. };
  624. int fdt_del_subnodes(const void *blob, int parent_offset)
  625. {
  626. int off, ndepth;
  627. int ret;
  628. for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
  629. (off >= 0) && (ndepth > 0);
  630. off = fdt_next_node(blob, off, &ndepth)) {
  631. if (ndepth == 1) {
  632. debug("delete %s: offset: %x\n",
  633. fdt_get_name(blob, off, 0), off);
  634. ret = fdt_del_node((void *)blob, off);
  635. if (ret < 0) {
  636. printf("Can't delete node: %s\n",
  637. fdt_strerror(ret));
  638. return ret;
  639. } else {
  640. ndepth = 0;
  641. off = parent_offset;
  642. }
  643. }
  644. }
  645. return 0;
  646. }
  647. int fdt_del_partitions(void *blob, int parent_offset)
  648. {
  649. const void *prop;
  650. int ndepth = 0;
  651. int off;
  652. int ret;
  653. off = fdt_next_node(blob, parent_offset, &ndepth);
  654. if (off > 0 && ndepth == 1) {
  655. prop = fdt_getprop(blob, off, "label", NULL);
  656. if (prop == NULL) {
  657. /*
  658. * Could not find label property, nand {}; node?
  659. * Check subnode, delete partitions there if any.
  660. */
  661. return fdt_del_partitions(blob, off);
  662. } else {
  663. ret = fdt_del_subnodes(blob, parent_offset);
  664. if (ret < 0) {
  665. printf("Can't remove subnodes: %s\n",
  666. fdt_strerror(ret));
  667. return ret;
  668. }
  669. }
  670. }
  671. return 0;
  672. }
  673. int fdt_node_set_part_info(void *blob, int parent_offset,
  674. struct mtd_device *dev)
  675. {
  676. struct list_head *pentry;
  677. struct part_info *part;
  678. struct reg_cell cell;
  679. int off, ndepth = 0;
  680. int part_num, ret;
  681. char buf[64];
  682. ret = fdt_del_partitions(blob, parent_offset);
  683. if (ret < 0)
  684. return ret;
  685. /*
  686. * Check if it is nand {}; subnode, adjust
  687. * the offset in this case
  688. */
  689. off = fdt_next_node(blob, parent_offset, &ndepth);
  690. if (off > 0 && ndepth == 1)
  691. parent_offset = off;
  692. part_num = 0;
  693. list_for_each_prev(pentry, &dev->parts) {
  694. int newoff;
  695. part = list_entry(pentry, struct part_info, link);
  696. debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
  697. part_num, part->name, part->size,
  698. part->offset, part->mask_flags);
  699. sprintf(buf, "partition@%llx", part->offset);
  700. add_sub:
  701. ret = fdt_add_subnode(blob, parent_offset, buf);
  702. if (ret == -FDT_ERR_NOSPACE) {
  703. ret = fdt_increase_size(blob, 512);
  704. if (!ret)
  705. goto add_sub;
  706. else
  707. goto err_size;
  708. } else if (ret < 0) {
  709. printf("Can't add partition node: %s\n",
  710. fdt_strerror(ret));
  711. return ret;
  712. }
  713. newoff = ret;
  714. /* Check MTD_WRITEABLE_CMD flag */
  715. if (part->mask_flags & 1) {
  716. add_ro:
  717. ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
  718. if (ret == -FDT_ERR_NOSPACE) {
  719. ret = fdt_increase_size(blob, 512);
  720. if (!ret)
  721. goto add_ro;
  722. else
  723. goto err_size;
  724. } else if (ret < 0)
  725. goto err_prop;
  726. }
  727. cell.r0 = cpu_to_fdt32(part->offset);
  728. cell.r1 = cpu_to_fdt32(part->size);
  729. add_reg:
  730. ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
  731. if (ret == -FDT_ERR_NOSPACE) {
  732. ret = fdt_increase_size(blob, 512);
  733. if (!ret)
  734. goto add_reg;
  735. else
  736. goto err_size;
  737. } else if (ret < 0)
  738. goto err_prop;
  739. add_label:
  740. ret = fdt_setprop_string(blob, newoff, "label", part->name);
  741. if (ret == -FDT_ERR_NOSPACE) {
  742. ret = fdt_increase_size(blob, 512);
  743. if (!ret)
  744. goto add_label;
  745. else
  746. goto err_size;
  747. } else if (ret < 0)
  748. goto err_prop;
  749. part_num++;
  750. }
  751. return 0;
  752. err_size:
  753. printf("Can't increase blob size: %s\n", fdt_strerror(ret));
  754. return ret;
  755. err_prop:
  756. printf("Can't add property: %s\n", fdt_strerror(ret));
  757. return ret;
  758. }
  759. /*
  760. * Update partitions in nor/nand nodes using info from
  761. * mtdparts environment variable. The nodes to update are
  762. * specified by node_info structure which contains mtd device
  763. * type and compatible string: E. g. the board code in
  764. * ft_board_setup() could use:
  765. *
  766. * struct node_info nodes[] = {
  767. * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
  768. * { "cfi-flash", MTD_DEV_TYPE_NOR, },
  769. * };
  770. *
  771. * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
  772. */
  773. void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
  774. {
  775. struct node_info *ni = node_info;
  776. struct mtd_device *dev;
  777. char *parts;
  778. int i, idx;
  779. int noff;
  780. parts = getenv("mtdparts");
  781. if (!parts)
  782. return;
  783. if (mtdparts_init() != 0)
  784. return;
  785. for (i = 0; i < node_info_size; i++) {
  786. idx = 0;
  787. noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
  788. while (noff != -FDT_ERR_NOTFOUND) {
  789. debug("%s: %s, mtd dev type %d\n",
  790. fdt_get_name(blob, noff, 0),
  791. ni[i].compat, ni[i].type);
  792. dev = device_find(ni[i].type, idx++);
  793. if (dev) {
  794. if (fdt_node_set_part_info(blob, noff, dev))
  795. return; /* return on error */
  796. }
  797. /* Jump to next flash node */
  798. noff = fdt_node_offset_by_compatible(blob, noff,
  799. ni[i].compat);
  800. }
  801. }
  802. }
  803. #endif
  804. void fdt_del_node_and_alias(void *blob, const char *alias)
  805. {
  806. int off = fdt_path_offset(blob, alias);
  807. if (off < 0)
  808. return;
  809. fdt_del_node(blob, off);
  810. off = fdt_path_offset(blob, "/aliases");
  811. fdt_delprop(blob, off, alias);
  812. }
  813. /* Max address size we deal with */
  814. #define OF_MAX_ADDR_CELLS 4
  815. #define OF_BAD_ADDR FDT_ADDR_T_NONE
  816. #define OF_CHECK_COUNTS(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
  817. /* Debug utility */
  818. #ifdef DEBUG
  819. static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
  820. {
  821. printf("%s", s);
  822. while(na--)
  823. printf(" %08x", *(addr++));
  824. printf("\n");
  825. }
  826. #else
  827. static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
  828. #endif
  829. /* Callbacks for bus specific translators */
  830. struct of_bus {
  831. const char *name;
  832. const char *addresses;
  833. void (*count_cells)(void *blob, int parentoffset,
  834. int *addrc, int *sizec);
  835. u64 (*map)(fdt32_t *addr, const fdt32_t *range,
  836. int na, int ns, int pna);
  837. int (*translate)(fdt32_t *addr, u64 offset, int na);
  838. };
  839. /* Default translator (generic bus) */
  840. void of_bus_default_count_cells(void *blob, int parentoffset,
  841. int *addrc, int *sizec)
  842. {
  843. const fdt32_t *prop;
  844. if (addrc)
  845. *addrc = fdt_address_cells(blob, parentoffset);
  846. if (sizec) {
  847. prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
  848. if (prop)
  849. *sizec = be32_to_cpup(prop);
  850. else
  851. *sizec = 1;
  852. }
  853. }
  854. static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
  855. int na, int ns, int pna)
  856. {
  857. u64 cp, s, da;
  858. cp = of_read_number(range, na);
  859. s = of_read_number(range + na + pna, ns);
  860. da = of_read_number(addr, na);
  861. debug("OF: default map, cp=%" PRIu64 ", s=%" PRIu64
  862. ", da=%" PRIu64 "\n", cp, s, da);
  863. if (da < cp || da >= (cp + s))
  864. return OF_BAD_ADDR;
  865. return da - cp;
  866. }
  867. static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
  868. {
  869. u64 a = of_read_number(addr, na);
  870. memset(addr, 0, na * 4);
  871. a += offset;
  872. if (na > 1)
  873. addr[na - 2] = cpu_to_fdt32(a >> 32);
  874. addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
  875. return 0;
  876. }
  877. /* Array of bus specific translators */
  878. static struct of_bus of_busses[] = {
  879. /* Default */
  880. {
  881. .name = "default",
  882. .addresses = "reg",
  883. .count_cells = of_bus_default_count_cells,
  884. .map = of_bus_default_map,
  885. .translate = of_bus_default_translate,
  886. },
  887. };
  888. static int of_translate_one(void * blob, int parent, struct of_bus *bus,
  889. struct of_bus *pbus, fdt32_t *addr,
  890. int na, int ns, int pna, const char *rprop)
  891. {
  892. const fdt32_t *ranges;
  893. int rlen;
  894. int rone;
  895. u64 offset = OF_BAD_ADDR;
  896. /* Normally, an absence of a "ranges" property means we are
  897. * crossing a non-translatable boundary, and thus the addresses
  898. * below the current not cannot be converted to CPU physical ones.
  899. * Unfortunately, while this is very clear in the spec, it's not
  900. * what Apple understood, and they do have things like /uni-n or
  901. * /ht nodes with no "ranges" property and a lot of perfectly
  902. * useable mapped devices below them. Thus we treat the absence of
  903. * "ranges" as equivalent to an empty "ranges" property which means
  904. * a 1:1 translation at that level. It's up to the caller not to try
  905. * to translate addresses that aren't supposed to be translated in
  906. * the first place. --BenH.
  907. */
  908. ranges = fdt_getprop(blob, parent, rprop, &rlen);
  909. if (ranges == NULL || rlen == 0) {
  910. offset = of_read_number(addr, na);
  911. memset(addr, 0, pna * 4);
  912. debug("OF: no ranges, 1:1 translation\n");
  913. goto finish;
  914. }
  915. debug("OF: walking ranges...\n");
  916. /* Now walk through the ranges */
  917. rlen /= 4;
  918. rone = na + pna + ns;
  919. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  920. offset = bus->map(addr, ranges, na, ns, pna);
  921. if (offset != OF_BAD_ADDR)
  922. break;
  923. }
  924. if (offset == OF_BAD_ADDR) {
  925. debug("OF: not found !\n");
  926. return 1;
  927. }
  928. memcpy(addr, ranges + na, 4 * pna);
  929. finish:
  930. of_dump_addr("OF: parent translation for:", addr, pna);
  931. debug("OF: with offset: %" PRIu64 "\n", offset);
  932. /* Translate it into parent bus space */
  933. return pbus->translate(addr, offset, pna);
  934. }
  935. /*
  936. * Translate an address from the device-tree into a CPU physical address,
  937. * this walks up the tree and applies the various bus mappings on the
  938. * way.
  939. *
  940. * Note: We consider that crossing any level with #size-cells == 0 to mean
  941. * that translation is impossible (that is we are not dealing with a value
  942. * that can be mapped to a cpu physical address). This is not really specified
  943. * that way, but this is traditionally the way IBM at least do things
  944. */
  945. static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr,
  946. const char *rprop)
  947. {
  948. int parent;
  949. struct of_bus *bus, *pbus;
  950. fdt32_t addr[OF_MAX_ADDR_CELLS];
  951. int na, ns, pna, pns;
  952. u64 result = OF_BAD_ADDR;
  953. debug("OF: ** translation for device %s **\n",
  954. fdt_get_name(blob, node_offset, NULL));
  955. /* Get parent & match bus type */
  956. parent = fdt_parent_offset(blob, node_offset);
  957. if (parent < 0)
  958. goto bail;
  959. bus = &of_busses[0];
  960. /* Cound address cells & copy address locally */
  961. bus->count_cells(blob, parent, &na, &ns);
  962. if (!OF_CHECK_COUNTS(na)) {
  963. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  964. fdt_get_name(blob, node_offset, NULL));
  965. goto bail;
  966. }
  967. memcpy(addr, in_addr, na * 4);
  968. debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  969. bus->name, na, ns, fdt_get_name(blob, parent, NULL));
  970. of_dump_addr("OF: translating address:", addr, na);
  971. /* Translate */
  972. for (;;) {
  973. /* Switch to parent bus */
  974. node_offset = parent;
  975. parent = fdt_parent_offset(blob, node_offset);
  976. /* If root, we have finished */
  977. if (parent < 0) {
  978. debug("OF: reached root node\n");
  979. result = of_read_number(addr, na);
  980. break;
  981. }
  982. /* Get new parent bus and counts */
  983. pbus = &of_busses[0];
  984. pbus->count_cells(blob, parent, &pna, &pns);
  985. if (!OF_CHECK_COUNTS(pna)) {
  986. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  987. fdt_get_name(blob, node_offset, NULL));
  988. break;
  989. }
  990. debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  991. pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
  992. /* Apply bus translation */
  993. if (of_translate_one(blob, node_offset, bus, pbus,
  994. addr, na, ns, pna, rprop))
  995. break;
  996. /* Complete the move up one level */
  997. na = pna;
  998. ns = pns;
  999. bus = pbus;
  1000. of_dump_addr("OF: one level translation:", addr, na);
  1001. }
  1002. bail:
  1003. return result;
  1004. }
  1005. u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr)
  1006. {
  1007. return __of_translate_address(blob, node_offset, in_addr, "ranges");
  1008. }
  1009. /**
  1010. * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
  1011. * who's reg property matches a physical cpu address
  1012. *
  1013. * @blob: ptr to device tree
  1014. * @compat: compatiable string to match
  1015. * @compat_off: property name
  1016. *
  1017. */
  1018. int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
  1019. phys_addr_t compat_off)
  1020. {
  1021. int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
  1022. while (off != -FDT_ERR_NOTFOUND) {
  1023. const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
  1024. if (reg) {
  1025. if (compat_off == fdt_translate_address(blob, off, reg))
  1026. return off;
  1027. }
  1028. off = fdt_node_offset_by_compatible(blob, off, compat);
  1029. }
  1030. return -FDT_ERR_NOTFOUND;
  1031. }
  1032. /**
  1033. * fdt_alloc_phandle: Return next free phandle value
  1034. *
  1035. * @blob: ptr to device tree
  1036. */
  1037. int fdt_alloc_phandle(void *blob)
  1038. {
  1039. int offset;
  1040. uint32_t phandle = 0;
  1041. for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
  1042. offset = fdt_next_node(blob, offset, NULL)) {
  1043. phandle = max(phandle, fdt_get_phandle(blob, offset));
  1044. }
  1045. return phandle + 1;
  1046. }
  1047. /*
  1048. * fdt_set_phandle: Create a phandle property for the given node
  1049. *
  1050. * @fdt: ptr to device tree
  1051. * @nodeoffset: node to update
  1052. * @phandle: phandle value to set (must be unique)
  1053. */
  1054. int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
  1055. {
  1056. int ret;
  1057. #ifdef DEBUG
  1058. int off = fdt_node_offset_by_phandle(fdt, phandle);
  1059. if ((off >= 0) && (off != nodeoffset)) {
  1060. char buf[64];
  1061. fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
  1062. printf("Trying to update node %s with phandle %u ",
  1063. buf, phandle);
  1064. fdt_get_path(fdt, off, buf, sizeof(buf));
  1065. printf("that already exists in node %s.\n", buf);
  1066. return -FDT_ERR_BADPHANDLE;
  1067. }
  1068. #endif
  1069. ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
  1070. if (ret < 0)
  1071. return ret;
  1072. /*
  1073. * For now, also set the deprecated "linux,phandle" property, so that we
  1074. * don't break older kernels.
  1075. */
  1076. ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
  1077. return ret;
  1078. }
  1079. /*
  1080. * fdt_create_phandle: Create a phandle property for the given node
  1081. *
  1082. * @fdt: ptr to device tree
  1083. * @nodeoffset: node to update
  1084. */
  1085. unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
  1086. {
  1087. /* see if there is a phandle already */
  1088. int phandle = fdt_get_phandle(fdt, nodeoffset);
  1089. /* if we got 0, means no phandle so create one */
  1090. if (phandle == 0) {
  1091. int ret;
  1092. phandle = fdt_alloc_phandle(fdt);
  1093. ret = fdt_set_phandle(fdt, nodeoffset, phandle);
  1094. if (ret < 0) {
  1095. printf("Can't set phandle %u: %s\n", phandle,
  1096. fdt_strerror(ret));
  1097. return 0;
  1098. }
  1099. }
  1100. return phandle;
  1101. }
  1102. /*
  1103. * fdt_set_node_status: Set status for the given node
  1104. *
  1105. * @fdt: ptr to device tree
  1106. * @nodeoffset: node to update
  1107. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
  1108. * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
  1109. * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
  1110. */
  1111. int fdt_set_node_status(void *fdt, int nodeoffset,
  1112. enum fdt_status status, unsigned int error_code)
  1113. {
  1114. char buf[16];
  1115. int ret = 0;
  1116. if (nodeoffset < 0)
  1117. return nodeoffset;
  1118. switch (status) {
  1119. case FDT_STATUS_OKAY:
  1120. ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
  1121. break;
  1122. case FDT_STATUS_DISABLED:
  1123. ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
  1124. break;
  1125. case FDT_STATUS_FAIL:
  1126. ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
  1127. break;
  1128. case FDT_STATUS_FAIL_ERROR_CODE:
  1129. sprintf(buf, "fail-%d", error_code);
  1130. ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
  1131. break;
  1132. default:
  1133. printf("Invalid fdt status: %x\n", status);
  1134. ret = -1;
  1135. break;
  1136. }
  1137. return ret;
  1138. }
  1139. /*
  1140. * fdt_set_status_by_alias: Set status for the given node given an alias
  1141. *
  1142. * @fdt: ptr to device tree
  1143. * @alias: alias of node to update
  1144. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
  1145. * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
  1146. * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
  1147. */
  1148. int fdt_set_status_by_alias(void *fdt, const char* alias,
  1149. enum fdt_status status, unsigned int error_code)
  1150. {
  1151. int offset = fdt_path_offset(fdt, alias);
  1152. return fdt_set_node_status(fdt, offset, status, error_code);
  1153. }
  1154. #if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
  1155. int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
  1156. {
  1157. int noff;
  1158. int ret;
  1159. noff = fdt_node_offset_by_compatible(blob, -1, compat);
  1160. if (noff != -FDT_ERR_NOTFOUND) {
  1161. debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
  1162. add_edid:
  1163. ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
  1164. if (ret == -FDT_ERR_NOSPACE) {
  1165. ret = fdt_increase_size(blob, 512);
  1166. if (!ret)
  1167. goto add_edid;
  1168. else
  1169. goto err_size;
  1170. } else if (ret < 0) {
  1171. printf("Can't add property: %s\n", fdt_strerror(ret));
  1172. return ret;
  1173. }
  1174. }
  1175. return 0;
  1176. err_size:
  1177. printf("Can't increase blob size: %s\n", fdt_strerror(ret));
  1178. return ret;
  1179. }
  1180. #endif
  1181. /*
  1182. * Verify the physical address of device tree node for a given alias
  1183. *
  1184. * This function locates the device tree node of a given alias, and then
  1185. * verifies that the physical address of that device matches the given
  1186. * parameter. It displays a message if there is a mismatch.
  1187. *
  1188. * Returns 1 on success, 0 on failure
  1189. */
  1190. int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
  1191. {
  1192. const char *path;
  1193. const fdt32_t *reg;
  1194. int node, len;
  1195. u64 dt_addr;
  1196. path = fdt_getprop(fdt, anode, alias, NULL);
  1197. if (!path) {
  1198. /* If there's no such alias, then it's not a failure */
  1199. return 1;
  1200. }
  1201. node = fdt_path_offset(fdt, path);
  1202. if (node < 0) {
  1203. printf("Warning: device tree alias '%s' points to invalid "
  1204. "node %s.\n", alias, path);
  1205. return 0;
  1206. }
  1207. reg = fdt_getprop(fdt, node, "reg", &len);
  1208. if (!reg) {
  1209. printf("Warning: device tree node '%s' has no address.\n",
  1210. path);
  1211. return 0;
  1212. }
  1213. dt_addr = fdt_translate_address(fdt, node, reg);
  1214. if (addr != dt_addr) {
  1215. printf("Warning: U-Boot configured device %s at address %"
  1216. PRIx64 ",\n but the device tree has it address %"
  1217. PRIx64 ".\n", alias, addr, dt_addr);
  1218. return 0;
  1219. }
  1220. return 1;
  1221. }
  1222. /*
  1223. * Returns the base address of an SOC or PCI node
  1224. */
  1225. u64 fdt_get_base_address(void *fdt, int node)
  1226. {
  1227. int size;
  1228. u32 naddr;
  1229. const fdt32_t *prop;
  1230. naddr = fdt_address_cells(fdt, node);
  1231. prop = fdt_getprop(fdt, node, "ranges", &size);
  1232. return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
  1233. }
  1234. /*
  1235. * Read a property of size <prop_len>. Currently only supports 1 or 2 cells.
  1236. */
  1237. static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
  1238. uint64_t *val, int cells)
  1239. {
  1240. const fdt32_t *prop32 = &prop[cell_off];
  1241. const fdt64_t *prop64 = (const fdt64_t *)&prop[cell_off];
  1242. if ((cell_off + cells) > prop_len)
  1243. return -FDT_ERR_NOSPACE;
  1244. switch (cells) {
  1245. case 1:
  1246. *val = fdt32_to_cpu(*prop32);
  1247. break;
  1248. case 2:
  1249. *val = fdt64_to_cpu(*prop64);
  1250. break;
  1251. default:
  1252. return -FDT_ERR_NOSPACE;
  1253. }
  1254. return 0;
  1255. }
  1256. /**
  1257. * fdt_read_range - Read a node's n'th range property
  1258. *
  1259. * @fdt: ptr to device tree
  1260. * @node: offset of node
  1261. * @n: range index
  1262. * @child_addr: pointer to storage for the "child address" field
  1263. * @addr: pointer to storage for the CPU view translated physical start
  1264. * @len: pointer to storage for the range length
  1265. *
  1266. * Convenience function that reads and interprets a specific range out of
  1267. * a number of the "ranges" property array.
  1268. */
  1269. int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
  1270. uint64_t *addr, uint64_t *len)
  1271. {
  1272. int pnode = fdt_parent_offset(fdt, node);
  1273. const fdt32_t *ranges;
  1274. int pacells;
  1275. int acells;
  1276. int scells;
  1277. int ranges_len;
  1278. int cell = 0;
  1279. int r = 0;
  1280. /*
  1281. * The "ranges" property is an array of
  1282. * { <child address> <parent address> <size in child address space> }
  1283. *
  1284. * All 3 elements can span a diffent number of cells. Fetch their size.
  1285. */
  1286. pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
  1287. acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
  1288. scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
  1289. /* Now try to get the ranges property */
  1290. ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
  1291. if (!ranges)
  1292. return -FDT_ERR_NOTFOUND;
  1293. ranges_len /= sizeof(uint32_t);
  1294. /* Jump to the n'th entry */
  1295. cell = n * (pacells + acells + scells);
  1296. /* Read <child address> */
  1297. if (child_addr) {
  1298. r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
  1299. acells);
  1300. if (r)
  1301. return r;
  1302. }
  1303. cell += acells;
  1304. /* Read <parent address> */
  1305. if (addr)
  1306. *addr = fdt_translate_address(fdt, node, ranges + cell);
  1307. cell += pacells;
  1308. /* Read <size in child address space> */
  1309. if (len) {
  1310. r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
  1311. if (r)
  1312. return r;
  1313. }
  1314. return 0;
  1315. }
  1316. /**
  1317. * fdt_setup_simplefb_node - Fill and enable a simplefb node
  1318. *
  1319. * @fdt: ptr to device tree
  1320. * @node: offset of the simplefb node
  1321. * @base_address: framebuffer base address
  1322. * @width: width in pixels
  1323. * @height: height in pixels
  1324. * @stride: bytes per line
  1325. * @format: pixel format string
  1326. *
  1327. * Convenience function to fill and enable a simplefb node.
  1328. */
  1329. int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
  1330. u32 height, u32 stride, const char *format)
  1331. {
  1332. char name[32];
  1333. fdt32_t cells[4];
  1334. int i, addrc, sizec, ret;
  1335. of_bus_default_count_cells(fdt, fdt_parent_offset(fdt, node),
  1336. &addrc, &sizec);
  1337. i = 0;
  1338. if (addrc == 2)
  1339. cells[i++] = cpu_to_fdt32(base_address >> 32);
  1340. cells[i++] = cpu_to_fdt32(base_address);
  1341. if (sizec == 2)
  1342. cells[i++] = 0;
  1343. cells[i++] = cpu_to_fdt32(height * stride);
  1344. ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
  1345. if (ret < 0)
  1346. return ret;
  1347. snprintf(name, sizeof(name), "framebuffer@%" PRIx64, base_address);
  1348. ret = fdt_set_name(fdt, node, name);
  1349. if (ret < 0)
  1350. return ret;
  1351. ret = fdt_setprop_u32(fdt, node, "width", width);
  1352. if (ret < 0)
  1353. return ret;
  1354. ret = fdt_setprop_u32(fdt, node, "height", height);
  1355. if (ret < 0)
  1356. return ret;
  1357. ret = fdt_setprop_u32(fdt, node, "stride", stride);
  1358. if (ret < 0)
  1359. return ret;
  1360. ret = fdt_setprop_string(fdt, node, "format", format);
  1361. if (ret < 0)
  1362. return ret;
  1363. ret = fdt_setprop_string(fdt, node, "status", "okay");
  1364. if (ret < 0)
  1365. return ret;
  1366. return 0;
  1367. }
  1368. /*
  1369. * Update native-mode in display-timings from display environment variable.
  1370. * The node to update are specified by path.
  1371. */
  1372. int fdt_fixup_display(void *blob, const char *path, const char *display)
  1373. {
  1374. int off, toff;
  1375. if (!display || !path)
  1376. return -FDT_ERR_NOTFOUND;
  1377. toff = fdt_path_offset(blob, path);
  1378. if (toff >= 0)
  1379. toff = fdt_subnode_offset(blob, toff, "display-timings");
  1380. if (toff < 0)
  1381. return toff;
  1382. for (off = fdt_first_subnode(blob, toff);
  1383. off >= 0;
  1384. off = fdt_next_subnode(blob, off)) {
  1385. uint32_t h = fdt_get_phandle(blob, off);
  1386. debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
  1387. fdt32_to_cpu(h));
  1388. if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
  1389. return fdt_setprop_u32(blob, toff, "native-mode", h);
  1390. }
  1391. return toff;
  1392. }