fdt_support.c 41 KB

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