fdt_support.c 34 KB

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