fdt_support.c 32 KB

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