fdt_support.c 37 KB

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