fdt_support.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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 enet[16], *tmp, *end;
  412. char mac[16];
  413. const char *path;
  414. unsigned char mac_addr[6];
  415. node = fdt_path_offset(fdt, "/aliases");
  416. if (node < 0)
  417. return;
  418. if (!getenv("ethaddr")) {
  419. if (getenv("usbethaddr")) {
  420. strcpy(mac, "usbethaddr");
  421. } else {
  422. debug("No ethernet MAC Address defined\n");
  423. return;
  424. }
  425. } else {
  426. strcpy(mac, "ethaddr");
  427. }
  428. i = 0;
  429. while ((tmp = getenv(mac)) != NULL) {
  430. sprintf(enet, "ethernet%d", i);
  431. path = fdt_getprop(fdt, node, enet, NULL);
  432. if (!path) {
  433. debug("No alias for %s\n", enet);
  434. sprintf(mac, "eth%daddr", ++i);
  435. continue;
  436. }
  437. for (j = 0; j < 6; j++) {
  438. mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  439. if (tmp)
  440. tmp = (*end) ? end+1 : end;
  441. }
  442. do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
  443. do_fixup_by_path(fdt, path, "local-mac-address",
  444. &mac_addr, 6, 1);
  445. sprintf(mac, "eth%daddr", ++i);
  446. }
  447. }
  448. /* Resize the fdt to its actual size + a bit of padding */
  449. int fdt_shrink_to_minimum(void *blob)
  450. {
  451. int i;
  452. uint64_t addr, size;
  453. int total, ret;
  454. uint actualsize;
  455. if (!blob)
  456. return 0;
  457. total = fdt_num_mem_rsv(blob);
  458. for (i = 0; i < total; i++) {
  459. fdt_get_mem_rsv(blob, i, &addr, &size);
  460. if (addr == (uintptr_t)blob) {
  461. fdt_del_mem_rsv(blob, i);
  462. break;
  463. }
  464. }
  465. /*
  466. * Calculate the actual size of the fdt
  467. * plus the size needed for 5 fdt_add_mem_rsv, one
  468. * for the fdt itself and 4 for a possible initrd
  469. * ((initrd-start + initrd-end) * 2 (name & value))
  470. */
  471. actualsize = fdt_off_dt_strings(blob) +
  472. fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
  473. /* Make it so the fdt ends on a page boundary */
  474. actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
  475. actualsize = actualsize - ((uintptr_t)blob & 0xfff);
  476. /* Change the fdt header to reflect the correct size */
  477. fdt_set_totalsize(blob, actualsize);
  478. /* Add the new reservation */
  479. ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
  480. if (ret < 0)
  481. return ret;
  482. return actualsize;
  483. }
  484. #ifdef CONFIG_PCI
  485. #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
  486. #define FDT_PCI_PREFETCH (0x40000000)
  487. #define FDT_PCI_MEM32 (0x02000000)
  488. #define FDT_PCI_IO (0x01000000)
  489. #define FDT_PCI_MEM64 (0x03000000)
  490. int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
  491. int addrcell, sizecell, len, r;
  492. u32 *dma_range;
  493. /* sized based on pci addr cells, size-cells, & address-cells */
  494. u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
  495. addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
  496. sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
  497. dma_range = &dma_ranges[0];
  498. for (r = 0; r < hose->region_count; r++) {
  499. u64 bus_start, phys_start, size;
  500. /* skip if !PCI_REGION_SYS_MEMORY */
  501. if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
  502. continue;
  503. bus_start = (u64)hose->regions[r].bus_start;
  504. phys_start = (u64)hose->regions[r].phys_start;
  505. size = (u64)hose->regions[r].size;
  506. dma_range[0] = 0;
  507. if (size >= 0x100000000ull)
  508. dma_range[0] |= FDT_PCI_MEM64;
  509. else
  510. dma_range[0] |= FDT_PCI_MEM32;
  511. if (hose->regions[r].flags & PCI_REGION_PREFETCH)
  512. dma_range[0] |= FDT_PCI_PREFETCH;
  513. #ifdef CONFIG_SYS_PCI_64BIT
  514. dma_range[1] = bus_start >> 32;
  515. #else
  516. dma_range[1] = 0;
  517. #endif
  518. dma_range[2] = bus_start & 0xffffffff;
  519. if (addrcell == 2) {
  520. dma_range[3] = phys_start >> 32;
  521. dma_range[4] = phys_start & 0xffffffff;
  522. } else {
  523. dma_range[3] = phys_start & 0xffffffff;
  524. }
  525. if (sizecell == 2) {
  526. dma_range[3 + addrcell + 0] = size >> 32;
  527. dma_range[3 + addrcell + 1] = size & 0xffffffff;
  528. } else {
  529. dma_range[3 + addrcell + 0] = size & 0xffffffff;
  530. }
  531. dma_range += (3 + addrcell + sizecell);
  532. }
  533. len = dma_range - &dma_ranges[0];
  534. if (len)
  535. fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
  536. return 0;
  537. }
  538. #endif
  539. #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
  540. /*
  541. * Provide a weak default function to return the flash bank size.
  542. * There might be multiple non-identical flash chips connected to one
  543. * chip-select, so we need to pass an index as well.
  544. */
  545. u32 __flash_get_bank_size(int cs, int idx)
  546. {
  547. extern flash_info_t flash_info[];
  548. /*
  549. * As default, a simple 1:1 mapping is provided. Boards with
  550. * a different mapping need to supply a board specific mapping
  551. * routine.
  552. */
  553. return flash_info[cs].size;
  554. }
  555. u32 flash_get_bank_size(int cs, int idx)
  556. __attribute__((weak, alias("__flash_get_bank_size")));
  557. /*
  558. * This function can be used to update the size in the "reg" property
  559. * of all NOR FLASH device nodes. This is necessary for boards with
  560. * non-fixed NOR FLASH sizes.
  561. */
  562. int fdt_fixup_nor_flash_size(void *blob)
  563. {
  564. char compat[][16] = { "cfi-flash", "jedec-flash" };
  565. int off;
  566. int len;
  567. struct fdt_property *prop;
  568. u32 *reg, *reg2;
  569. int i;
  570. for (i = 0; i < 2; i++) {
  571. off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
  572. while (off != -FDT_ERR_NOTFOUND) {
  573. int idx;
  574. /*
  575. * Found one compatible node, so fixup the size
  576. * int its reg properties
  577. */
  578. prop = fdt_get_property_w(blob, off, "reg", &len);
  579. if (prop) {
  580. int tuple_size = 3 * sizeof(reg);
  581. /*
  582. * There might be multiple reg-tuples,
  583. * so loop through them all
  584. */
  585. reg = reg2 = (u32 *)&prop->data[0];
  586. for (idx = 0; idx < (len / tuple_size); idx++) {
  587. /*
  588. * Update size in reg property
  589. */
  590. reg[2] = flash_get_bank_size(reg[0],
  591. idx);
  592. /*
  593. * Point to next reg tuple
  594. */
  595. reg += 3;
  596. }
  597. fdt_setprop(blob, off, "reg", reg2, len);
  598. }
  599. /* Move to next compatible node */
  600. off = fdt_node_offset_by_compatible(blob, off,
  601. compat[i]);
  602. }
  603. }
  604. return 0;
  605. }
  606. #endif
  607. int fdt_increase_size(void *fdt, int add_len)
  608. {
  609. int newlen;
  610. newlen = fdt_totalsize(fdt) + add_len;
  611. /* Open in place with a new len */
  612. return fdt_open_into(fdt, fdt, newlen);
  613. }
  614. #ifdef CONFIG_FDT_FIXUP_PARTITIONS
  615. #include <jffs2/load_kernel.h>
  616. #include <mtd_node.h>
  617. struct reg_cell {
  618. unsigned int r0;
  619. unsigned int r1;
  620. };
  621. int fdt_del_subnodes(const void *blob, int parent_offset)
  622. {
  623. int off, ndepth;
  624. int ret;
  625. for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
  626. (off >= 0) && (ndepth > 0);
  627. off = fdt_next_node(blob, off, &ndepth)) {
  628. if (ndepth == 1) {
  629. debug("delete %s: offset: %x\n",
  630. fdt_get_name(blob, off, 0), off);
  631. ret = fdt_del_node((void *)blob, off);
  632. if (ret < 0) {
  633. printf("Can't delete node: %s\n",
  634. fdt_strerror(ret));
  635. return ret;
  636. } else {
  637. ndepth = 0;
  638. off = parent_offset;
  639. }
  640. }
  641. }
  642. return 0;
  643. }
  644. int fdt_del_partitions(void *blob, int parent_offset)
  645. {
  646. const void *prop;
  647. int ndepth = 0;
  648. int off;
  649. int ret;
  650. off = fdt_next_node(blob, parent_offset, &ndepth);
  651. if (off > 0 && ndepth == 1) {
  652. prop = fdt_getprop(blob, off, "label", NULL);
  653. if (prop == NULL) {
  654. /*
  655. * Could not find label property, nand {}; node?
  656. * Check subnode, delete partitions there if any.
  657. */
  658. return fdt_del_partitions(blob, off);
  659. } else {
  660. ret = fdt_del_subnodes(blob, parent_offset);
  661. if (ret < 0) {
  662. printf("Can't remove subnodes: %s\n",
  663. fdt_strerror(ret));
  664. return ret;
  665. }
  666. }
  667. }
  668. return 0;
  669. }
  670. int fdt_node_set_part_info(void *blob, int parent_offset,
  671. struct mtd_device *dev)
  672. {
  673. struct list_head *pentry;
  674. struct part_info *part;
  675. struct reg_cell cell;
  676. int off, ndepth = 0;
  677. int part_num, ret;
  678. char buf[64];
  679. ret = fdt_del_partitions(blob, parent_offset);
  680. if (ret < 0)
  681. return ret;
  682. /*
  683. * Check if it is nand {}; subnode, adjust
  684. * the offset in this case
  685. */
  686. off = fdt_next_node(blob, parent_offset, &ndepth);
  687. if (off > 0 && ndepth == 1)
  688. parent_offset = off;
  689. part_num = 0;
  690. list_for_each_prev(pentry, &dev->parts) {
  691. int newoff;
  692. part = list_entry(pentry, struct part_info, link);
  693. debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
  694. part_num, part->name, part->size,
  695. part->offset, part->mask_flags);
  696. sprintf(buf, "partition@%llx", part->offset);
  697. add_sub:
  698. ret = fdt_add_subnode(blob, parent_offset, buf);
  699. if (ret == -FDT_ERR_NOSPACE) {
  700. ret = fdt_increase_size(blob, 512);
  701. if (!ret)
  702. goto add_sub;
  703. else
  704. goto err_size;
  705. } else if (ret < 0) {
  706. printf("Can't add partition node: %s\n",
  707. fdt_strerror(ret));
  708. return ret;
  709. }
  710. newoff = ret;
  711. /* Check MTD_WRITEABLE_CMD flag */
  712. if (part->mask_flags & 1) {
  713. add_ro:
  714. ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
  715. if (ret == -FDT_ERR_NOSPACE) {
  716. ret = fdt_increase_size(blob, 512);
  717. if (!ret)
  718. goto add_ro;
  719. else
  720. goto err_size;
  721. } else if (ret < 0)
  722. goto err_prop;
  723. }
  724. cell.r0 = cpu_to_fdt32(part->offset);
  725. cell.r1 = cpu_to_fdt32(part->size);
  726. add_reg:
  727. ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
  728. if (ret == -FDT_ERR_NOSPACE) {
  729. ret = fdt_increase_size(blob, 512);
  730. if (!ret)
  731. goto add_reg;
  732. else
  733. goto err_size;
  734. } else if (ret < 0)
  735. goto err_prop;
  736. add_label:
  737. ret = fdt_setprop_string(blob, newoff, "label", part->name);
  738. if (ret == -FDT_ERR_NOSPACE) {
  739. ret = fdt_increase_size(blob, 512);
  740. if (!ret)
  741. goto add_label;
  742. else
  743. goto err_size;
  744. } else if (ret < 0)
  745. goto err_prop;
  746. part_num++;
  747. }
  748. return 0;
  749. err_size:
  750. printf("Can't increase blob size: %s\n", fdt_strerror(ret));
  751. return ret;
  752. err_prop:
  753. printf("Can't add property: %s\n", fdt_strerror(ret));
  754. return ret;
  755. }
  756. /*
  757. * Update partitions in nor/nand nodes using info from
  758. * mtdparts environment variable. The nodes to update are
  759. * specified by node_info structure which contains mtd device
  760. * type and compatible string: E. g. the board code in
  761. * ft_board_setup() could use:
  762. *
  763. * struct node_info nodes[] = {
  764. * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
  765. * { "cfi-flash", MTD_DEV_TYPE_NOR, },
  766. * };
  767. *
  768. * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
  769. */
  770. void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
  771. {
  772. struct node_info *ni = node_info;
  773. struct mtd_device *dev;
  774. char *parts;
  775. int i, idx;
  776. int noff;
  777. parts = getenv("mtdparts");
  778. if (!parts)
  779. return;
  780. if (mtdparts_init() != 0)
  781. return;
  782. for (i = 0; i < node_info_size; i++) {
  783. idx = 0;
  784. noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
  785. while (noff != -FDT_ERR_NOTFOUND) {
  786. debug("%s: %s, mtd dev type %d\n",
  787. fdt_get_name(blob, noff, 0),
  788. ni[i].compat, ni[i].type);
  789. dev = device_find(ni[i].type, idx++);
  790. if (dev) {
  791. if (fdt_node_set_part_info(blob, noff, dev))
  792. return; /* return on error */
  793. }
  794. /* Jump to next flash node */
  795. noff = fdt_node_offset_by_compatible(blob, noff,
  796. ni[i].compat);
  797. }
  798. }
  799. }
  800. #endif
  801. void fdt_del_node_and_alias(void *blob, const char *alias)
  802. {
  803. int off = fdt_path_offset(blob, alias);
  804. if (off < 0)
  805. return;
  806. fdt_del_node(blob, off);
  807. off = fdt_path_offset(blob, "/aliases");
  808. fdt_delprop(blob, off, alias);
  809. }
  810. /* Max address size we deal with */
  811. #define OF_MAX_ADDR_CELLS 4
  812. #define OF_BAD_ADDR FDT_ADDR_T_NONE
  813. #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
  814. (ns) > 0)
  815. /* Debug utility */
  816. #ifdef DEBUG
  817. static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
  818. {
  819. printf("%s", s);
  820. while(na--)
  821. printf(" %08x", *(addr++));
  822. printf("\n");
  823. }
  824. #else
  825. static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
  826. #endif
  827. /* Callbacks for bus specific translators */
  828. struct of_bus {
  829. const char *name;
  830. const char *addresses;
  831. void (*count_cells)(void *blob, int parentoffset,
  832. int *addrc, int *sizec);
  833. u64 (*map)(fdt32_t *addr, const fdt32_t *range,
  834. int na, int ns, int pna);
  835. int (*translate)(fdt32_t *addr, u64 offset, int na);
  836. };
  837. /* Default translator (generic bus) */
  838. void of_bus_default_count_cells(void *blob, int parentoffset,
  839. int *addrc, int *sizec)
  840. {
  841. const fdt32_t *prop;
  842. if (addrc)
  843. *addrc = fdt_address_cells(blob, parentoffset);
  844. if (sizec) {
  845. prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
  846. if (prop)
  847. *sizec = be32_to_cpup(prop);
  848. else
  849. *sizec = 1;
  850. }
  851. }
  852. static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
  853. int na, int ns, int pna)
  854. {
  855. u64 cp, s, da;
  856. cp = of_read_number(range, na);
  857. s = of_read_number(range + na + pna, ns);
  858. da = of_read_number(addr, na);
  859. debug("OF: default map, cp=%" PRIu64 ", s=%" PRIu64
  860. ", da=%" PRIu64 "\n", cp, s, da);
  861. if (da < cp || da >= (cp + s))
  862. return OF_BAD_ADDR;
  863. return da - cp;
  864. }
  865. static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
  866. {
  867. u64 a = of_read_number(addr, na);
  868. memset(addr, 0, na * 4);
  869. a += offset;
  870. if (na > 1)
  871. addr[na - 2] = cpu_to_fdt32(a >> 32);
  872. addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
  873. return 0;
  874. }
  875. /* Array of bus specific translators */
  876. static struct of_bus of_busses[] = {
  877. /* Default */
  878. {
  879. .name = "default",
  880. .addresses = "reg",
  881. .count_cells = of_bus_default_count_cells,
  882. .map = of_bus_default_map,
  883. .translate = of_bus_default_translate,
  884. },
  885. };
  886. static int of_translate_one(void * blob, int parent, struct of_bus *bus,
  887. struct of_bus *pbus, fdt32_t *addr,
  888. int na, int ns, int pna, const char *rprop)
  889. {
  890. const fdt32_t *ranges;
  891. int rlen;
  892. int rone;
  893. u64 offset = OF_BAD_ADDR;
  894. /* Normally, an absence of a "ranges" property means we are
  895. * crossing a non-translatable boundary, and thus the addresses
  896. * below the current not cannot be converted to CPU physical ones.
  897. * Unfortunately, while this is very clear in the spec, it's not
  898. * what Apple understood, and they do have things like /uni-n or
  899. * /ht nodes with no "ranges" property and a lot of perfectly
  900. * useable mapped devices below them. Thus we treat the absence of
  901. * "ranges" as equivalent to an empty "ranges" property which means
  902. * a 1:1 translation at that level. It's up to the caller not to try
  903. * to translate addresses that aren't supposed to be translated in
  904. * the first place. --BenH.
  905. */
  906. ranges = fdt_getprop(blob, parent, rprop, &rlen);
  907. if (ranges == NULL || rlen == 0) {
  908. offset = of_read_number(addr, na);
  909. memset(addr, 0, pna * 4);
  910. debug("OF: no ranges, 1:1 translation\n");
  911. goto finish;
  912. }
  913. debug("OF: walking ranges...\n");
  914. /* Now walk through the ranges */
  915. rlen /= 4;
  916. rone = na + pna + ns;
  917. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  918. offset = bus->map(addr, ranges, na, ns, pna);
  919. if (offset != OF_BAD_ADDR)
  920. break;
  921. }
  922. if (offset == OF_BAD_ADDR) {
  923. debug("OF: not found !\n");
  924. return 1;
  925. }
  926. memcpy(addr, ranges + na, 4 * pna);
  927. finish:
  928. of_dump_addr("OF: parent translation for:", addr, pna);
  929. debug("OF: with offset: %" PRIu64 "\n", offset);
  930. /* Translate it into parent bus space */
  931. return pbus->translate(addr, offset, pna);
  932. }
  933. /*
  934. * Translate an address from the device-tree into a CPU physical address,
  935. * this walks up the tree and applies the various bus mappings on the
  936. * way.
  937. *
  938. * Note: We consider that crossing any level with #size-cells == 0 to mean
  939. * that translation is impossible (that is we are not dealing with a value
  940. * that can be mapped to a cpu physical address). This is not really specified
  941. * that way, but this is traditionally the way IBM at least do things
  942. */
  943. static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr,
  944. const char *rprop)
  945. {
  946. int parent;
  947. struct of_bus *bus, *pbus;
  948. fdt32_t addr[OF_MAX_ADDR_CELLS];
  949. int na, ns, pna, pns;
  950. u64 result = OF_BAD_ADDR;
  951. debug("OF: ** translation for device %s **\n",
  952. fdt_get_name(blob, node_offset, NULL));
  953. /* Get parent & match bus type */
  954. parent = fdt_parent_offset(blob, node_offset);
  955. if (parent < 0)
  956. goto bail;
  957. bus = &of_busses[0];
  958. /* Cound address cells & copy address locally */
  959. bus->count_cells(blob, parent, &na, &ns);
  960. if (!OF_CHECK_COUNTS(na, ns)) {
  961. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  962. fdt_get_name(blob, node_offset, NULL));
  963. goto bail;
  964. }
  965. memcpy(addr, in_addr, na * 4);
  966. debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  967. bus->name, na, ns, fdt_get_name(blob, parent, NULL));
  968. of_dump_addr("OF: translating address:", addr, na);
  969. /* Translate */
  970. for (;;) {
  971. /* Switch to parent bus */
  972. node_offset = parent;
  973. parent = fdt_parent_offset(blob, node_offset);
  974. /* If root, we have finished */
  975. if (parent < 0) {
  976. debug("OF: reached root node\n");
  977. result = of_read_number(addr, na);
  978. break;
  979. }
  980. /* Get new parent bus and counts */
  981. pbus = &of_busses[0];
  982. pbus->count_cells(blob, parent, &pna, &pns);
  983. if (!OF_CHECK_COUNTS(pna, pns)) {
  984. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  985. fdt_get_name(blob, node_offset, NULL));
  986. break;
  987. }
  988. debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  989. pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
  990. /* Apply bus translation */
  991. if (of_translate_one(blob, node_offset, bus, pbus,
  992. addr, na, ns, pna, rprop))
  993. break;
  994. /* Complete the move up one level */
  995. na = pna;
  996. ns = pns;
  997. bus = pbus;
  998. of_dump_addr("OF: one level translation:", addr, na);
  999. }
  1000. bail:
  1001. return result;
  1002. }
  1003. u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr)
  1004. {
  1005. return __of_translate_address(blob, node_offset, in_addr, "ranges");
  1006. }
  1007. /**
  1008. * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
  1009. * who's reg property matches a physical cpu address
  1010. *
  1011. * @blob: ptr to device tree
  1012. * @compat: compatiable string to match
  1013. * @compat_off: property name
  1014. *
  1015. */
  1016. int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
  1017. phys_addr_t compat_off)
  1018. {
  1019. int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
  1020. while (off != -FDT_ERR_NOTFOUND) {
  1021. const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
  1022. if (reg) {
  1023. if (compat_off == fdt_translate_address(blob, off, reg))
  1024. return off;
  1025. }
  1026. off = fdt_node_offset_by_compatible(blob, off, compat);
  1027. }
  1028. return -FDT_ERR_NOTFOUND;
  1029. }
  1030. /**
  1031. * fdt_alloc_phandle: Return next free phandle value
  1032. *
  1033. * @blob: ptr to device tree
  1034. */
  1035. int fdt_alloc_phandle(void *blob)
  1036. {
  1037. int offset;
  1038. uint32_t phandle = 0;
  1039. for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
  1040. offset = fdt_next_node(blob, offset, NULL)) {
  1041. phandle = max(phandle, fdt_get_phandle(blob, offset));
  1042. }
  1043. return phandle + 1;
  1044. }
  1045. /*
  1046. * fdt_set_phandle: Create a phandle property for the given node
  1047. *
  1048. * @fdt: ptr to device tree
  1049. * @nodeoffset: node to update
  1050. * @phandle: phandle value to set (must be unique)
  1051. */
  1052. int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
  1053. {
  1054. int ret;
  1055. #ifdef DEBUG
  1056. int off = fdt_node_offset_by_phandle(fdt, phandle);
  1057. if ((off >= 0) && (off != nodeoffset)) {
  1058. char buf[64];
  1059. fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
  1060. printf("Trying to update node %s with phandle %u ",
  1061. buf, phandle);
  1062. fdt_get_path(fdt, off, buf, sizeof(buf));
  1063. printf("that already exists in node %s.\n", buf);
  1064. return -FDT_ERR_BADPHANDLE;
  1065. }
  1066. #endif
  1067. ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
  1068. if (ret < 0)
  1069. return ret;
  1070. /*
  1071. * For now, also set the deprecated "linux,phandle" property, so that we
  1072. * don't break older kernels.
  1073. */
  1074. ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
  1075. return ret;
  1076. }
  1077. /*
  1078. * fdt_create_phandle: Create a phandle property for the given node
  1079. *
  1080. * @fdt: ptr to device tree
  1081. * @nodeoffset: node to update
  1082. */
  1083. unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
  1084. {
  1085. /* see if there is a phandle already */
  1086. int phandle = fdt_get_phandle(fdt, nodeoffset);
  1087. /* if we got 0, means no phandle so create one */
  1088. if (phandle == 0) {
  1089. int ret;
  1090. phandle = fdt_alloc_phandle(fdt);
  1091. ret = fdt_set_phandle(fdt, nodeoffset, phandle);
  1092. if (ret < 0) {
  1093. printf("Can't set phandle %u: %s\n", phandle,
  1094. fdt_strerror(ret));
  1095. return 0;
  1096. }
  1097. }
  1098. return phandle;
  1099. }
  1100. /*
  1101. * fdt_set_node_status: Set status for the given node
  1102. *
  1103. * @fdt: ptr to device tree
  1104. * @nodeoffset: node to update
  1105. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
  1106. * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
  1107. * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
  1108. */
  1109. int fdt_set_node_status(void *fdt, int nodeoffset,
  1110. enum fdt_status status, unsigned int error_code)
  1111. {
  1112. char buf[16];
  1113. int ret = 0;
  1114. if (nodeoffset < 0)
  1115. return nodeoffset;
  1116. switch (status) {
  1117. case FDT_STATUS_OKAY:
  1118. ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
  1119. break;
  1120. case FDT_STATUS_DISABLED:
  1121. ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
  1122. break;
  1123. case FDT_STATUS_FAIL:
  1124. ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
  1125. break;
  1126. case FDT_STATUS_FAIL_ERROR_CODE:
  1127. sprintf(buf, "fail-%d", error_code);
  1128. ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
  1129. break;
  1130. default:
  1131. printf("Invalid fdt status: %x\n", status);
  1132. ret = -1;
  1133. break;
  1134. }
  1135. return ret;
  1136. }
  1137. /*
  1138. * fdt_set_status_by_alias: Set status for the given node given an alias
  1139. *
  1140. * @fdt: ptr to device tree
  1141. * @alias: alias of node to update
  1142. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
  1143. * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
  1144. * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
  1145. */
  1146. int fdt_set_status_by_alias(void *fdt, const char* alias,
  1147. enum fdt_status status, unsigned int error_code)
  1148. {
  1149. int offset = fdt_path_offset(fdt, alias);
  1150. return fdt_set_node_status(fdt, offset, status, error_code);
  1151. }
  1152. #if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
  1153. int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
  1154. {
  1155. int noff;
  1156. int ret;
  1157. noff = fdt_node_offset_by_compatible(blob, -1, compat);
  1158. if (noff != -FDT_ERR_NOTFOUND) {
  1159. debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
  1160. add_edid:
  1161. ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
  1162. if (ret == -FDT_ERR_NOSPACE) {
  1163. ret = fdt_increase_size(blob, 512);
  1164. if (!ret)
  1165. goto add_edid;
  1166. else
  1167. goto err_size;
  1168. } else if (ret < 0) {
  1169. printf("Can't add property: %s\n", fdt_strerror(ret));
  1170. return ret;
  1171. }
  1172. }
  1173. return 0;
  1174. err_size:
  1175. printf("Can't increase blob size: %s\n", fdt_strerror(ret));
  1176. return ret;
  1177. }
  1178. #endif
  1179. /*
  1180. * Verify the physical address of device tree node for a given alias
  1181. *
  1182. * This function locates the device tree node of a given alias, and then
  1183. * verifies that the physical address of that device matches the given
  1184. * parameter. It displays a message if there is a mismatch.
  1185. *
  1186. * Returns 1 on success, 0 on failure
  1187. */
  1188. int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
  1189. {
  1190. const char *path;
  1191. const fdt32_t *reg;
  1192. int node, len;
  1193. u64 dt_addr;
  1194. path = fdt_getprop(fdt, anode, alias, NULL);
  1195. if (!path) {
  1196. /* If there's no such alias, then it's not a failure */
  1197. return 1;
  1198. }
  1199. node = fdt_path_offset(fdt, path);
  1200. if (node < 0) {
  1201. printf("Warning: device tree alias '%s' points to invalid "
  1202. "node %s.\n", alias, path);
  1203. return 0;
  1204. }
  1205. reg = fdt_getprop(fdt, node, "reg", &len);
  1206. if (!reg) {
  1207. printf("Warning: device tree node '%s' has no address.\n",
  1208. path);
  1209. return 0;
  1210. }
  1211. dt_addr = fdt_translate_address(fdt, node, reg);
  1212. if (addr != dt_addr) {
  1213. printf("Warning: U-Boot configured device %s at address %"
  1214. PRIx64 ",\n but the device tree has it address %"
  1215. PRIx64 ".\n", alias, addr, dt_addr);
  1216. return 0;
  1217. }
  1218. return 1;
  1219. }
  1220. /*
  1221. * Returns the base address of an SOC or PCI node
  1222. */
  1223. u64 fdt_get_base_address(void *fdt, int node)
  1224. {
  1225. int size;
  1226. u32 naddr;
  1227. const fdt32_t *prop;
  1228. naddr = fdt_address_cells(fdt, node);
  1229. prop = fdt_getprop(fdt, node, "ranges", &size);
  1230. return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
  1231. }
  1232. /*
  1233. * Read a property of size <prop_len>. Currently only supports 1 or 2 cells.
  1234. */
  1235. static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
  1236. uint64_t *val, int cells)
  1237. {
  1238. const fdt32_t *prop32 = &prop[cell_off];
  1239. const fdt64_t *prop64 = (const fdt64_t *)&prop[cell_off];
  1240. if ((cell_off + cells) > prop_len)
  1241. return -FDT_ERR_NOSPACE;
  1242. switch (cells) {
  1243. case 1:
  1244. *val = fdt32_to_cpu(*prop32);
  1245. break;
  1246. case 2:
  1247. *val = fdt64_to_cpu(*prop64);
  1248. break;
  1249. default:
  1250. return -FDT_ERR_NOSPACE;
  1251. }
  1252. return 0;
  1253. }
  1254. /**
  1255. * fdt_read_range - Read a node's n'th range property
  1256. *
  1257. * @fdt: ptr to device tree
  1258. * @node: offset of node
  1259. * @n: range index
  1260. * @child_addr: pointer to storage for the "child address" field
  1261. * @addr: pointer to storage for the CPU view translated physical start
  1262. * @len: pointer to storage for the range length
  1263. *
  1264. * Convenience function that reads and interprets a specific range out of
  1265. * a number of the "ranges" property array.
  1266. */
  1267. int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
  1268. uint64_t *addr, uint64_t *len)
  1269. {
  1270. int pnode = fdt_parent_offset(fdt, node);
  1271. const fdt32_t *ranges;
  1272. int pacells;
  1273. int acells;
  1274. int scells;
  1275. int ranges_len;
  1276. int cell = 0;
  1277. int r = 0;
  1278. /*
  1279. * The "ranges" property is an array of
  1280. * { <child address> <parent address> <size in child address space> }
  1281. *
  1282. * All 3 elements can span a diffent number of cells. Fetch their size.
  1283. */
  1284. pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
  1285. acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
  1286. scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
  1287. /* Now try to get the ranges property */
  1288. ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
  1289. if (!ranges)
  1290. return -FDT_ERR_NOTFOUND;
  1291. ranges_len /= sizeof(uint32_t);
  1292. /* Jump to the n'th entry */
  1293. cell = n * (pacells + acells + scells);
  1294. /* Read <child address> */
  1295. if (child_addr) {
  1296. r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
  1297. acells);
  1298. if (r)
  1299. return r;
  1300. }
  1301. cell += acells;
  1302. /* Read <parent address> */
  1303. if (addr)
  1304. *addr = fdt_translate_address(fdt, node, ranges + cell);
  1305. cell += pacells;
  1306. /* Read <size in child address space> */
  1307. if (len) {
  1308. r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
  1309. if (r)
  1310. return r;
  1311. }
  1312. return 0;
  1313. }
  1314. /**
  1315. * fdt_setup_simplefb_node - Fill and enable a simplefb node
  1316. *
  1317. * @fdt: ptr to device tree
  1318. * @node: offset of the simplefb node
  1319. * @base_address: framebuffer base address
  1320. * @width: width in pixels
  1321. * @height: height in pixels
  1322. * @stride: bytes per line
  1323. * @format: pixel format string
  1324. *
  1325. * Convenience function to fill and enable a simplefb node.
  1326. */
  1327. int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
  1328. u32 height, u32 stride, const char *format)
  1329. {
  1330. char name[32];
  1331. fdt32_t cells[4];
  1332. int i, addrc, sizec, ret;
  1333. of_bus_default_count_cells(fdt, fdt_parent_offset(fdt, node),
  1334. &addrc, &sizec);
  1335. i = 0;
  1336. if (addrc == 2)
  1337. cells[i++] = cpu_to_fdt32(base_address >> 32);
  1338. cells[i++] = cpu_to_fdt32(base_address);
  1339. if (sizec == 2)
  1340. cells[i++] = 0;
  1341. cells[i++] = cpu_to_fdt32(height * stride);
  1342. ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
  1343. if (ret < 0)
  1344. return ret;
  1345. snprintf(name, sizeof(name), "framebuffer@%" PRIx64, base_address);
  1346. ret = fdt_set_name(fdt, node, name);
  1347. if (ret < 0)
  1348. return ret;
  1349. ret = fdt_setprop_u32(fdt, node, "width", width);
  1350. if (ret < 0)
  1351. return ret;
  1352. ret = fdt_setprop_u32(fdt, node, "height", height);
  1353. if (ret < 0)
  1354. return ret;
  1355. ret = fdt_setprop_u32(fdt, node, "stride", stride);
  1356. if (ret < 0)
  1357. return ret;
  1358. ret = fdt_setprop_string(fdt, node, "format", format);
  1359. if (ret < 0)
  1360. return ret;
  1361. ret = fdt_setprop_string(fdt, node, "status", "okay");
  1362. if (ret < 0)
  1363. return ret;
  1364. return 0;
  1365. }
  1366. /*
  1367. * Update native-mode in display-timings from display environment variable.
  1368. * The node to update are specified by path.
  1369. */
  1370. int fdt_fixup_display(void *blob, const char *path, const char *display)
  1371. {
  1372. int off, toff;
  1373. if (!display || !path)
  1374. return -FDT_ERR_NOTFOUND;
  1375. toff = fdt_path_offset(blob, path);
  1376. if (toff >= 0)
  1377. toff = fdt_subnode_offset(blob, toff, "display-timings");
  1378. if (toff < 0)
  1379. return toff;
  1380. for (off = fdt_first_subnode(blob, toff);
  1381. off >= 0;
  1382. off = fdt_next_subnode(blob, off)) {
  1383. uint32_t h = fdt_get_phandle(blob, off);
  1384. debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
  1385. fdt32_to_cpu(h));
  1386. if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
  1387. return fdt_setprop_u32(blob, toff, "native-mode", h);
  1388. }
  1389. return toff;
  1390. }