fdt_support.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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 + 1);
  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. int fdt_fixup_memory(void *blob, u64 start, u64 size)
  324. {
  325. int err, nodeoffset, len = 0;
  326. u8 tmp[16];
  327. const u32 *addrcell, *sizecell;
  328. err = fdt_check_header(blob);
  329. if (err < 0) {
  330. printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
  331. return err;
  332. }
  333. /* update, or add and update /memory node */
  334. nodeoffset = fdt_path_offset(blob, "/memory");
  335. if (nodeoffset < 0) {
  336. nodeoffset = fdt_add_subnode(blob, 0, "memory");
  337. if (nodeoffset < 0)
  338. printf("WARNING: could not create /memory: %s.\n",
  339. fdt_strerror(nodeoffset));
  340. return nodeoffset;
  341. }
  342. err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
  343. sizeof("memory"));
  344. if (err < 0) {
  345. printf("WARNING: could not set %s %s.\n", "device_type",
  346. fdt_strerror(err));
  347. return err;
  348. }
  349. addrcell = fdt_getprop(blob, 0, "#address-cells", NULL);
  350. /* use shifts and mask to ensure endianness */
  351. if ((addrcell) && (*addrcell == 2)) {
  352. tmp[0] = (start >> 56) & 0xff;
  353. tmp[1] = (start >> 48) & 0xff;
  354. tmp[2] = (start >> 40) & 0xff;
  355. tmp[3] = (start >> 32) & 0xff;
  356. tmp[4] = (start >> 24) & 0xff;
  357. tmp[5] = (start >> 16) & 0xff;
  358. tmp[6] = (start >> 8) & 0xff;
  359. tmp[7] = (start ) & 0xff;
  360. len = 8;
  361. } else {
  362. tmp[0] = (start >> 24) & 0xff;
  363. tmp[1] = (start >> 16) & 0xff;
  364. tmp[2] = (start >> 8) & 0xff;
  365. tmp[3] = (start ) & 0xff;
  366. len = 4;
  367. }
  368. sizecell = fdt_getprop(blob, 0, "#size-cells", NULL);
  369. /* use shifts and mask to ensure endianness */
  370. if ((sizecell) && (*sizecell == 2)) {
  371. tmp[0+len] = (size >> 56) & 0xff;
  372. tmp[1+len] = (size >> 48) & 0xff;
  373. tmp[2+len] = (size >> 40) & 0xff;
  374. tmp[3+len] = (size >> 32) & 0xff;
  375. tmp[4+len] = (size >> 24) & 0xff;
  376. tmp[5+len] = (size >> 16) & 0xff;
  377. tmp[6+len] = (size >> 8) & 0xff;
  378. tmp[7+len] = (size ) & 0xff;
  379. len += 8;
  380. } else {
  381. tmp[0+len] = (size >> 24) & 0xff;
  382. tmp[1+len] = (size >> 16) & 0xff;
  383. tmp[2+len] = (size >> 8) & 0xff;
  384. tmp[3+len] = (size ) & 0xff;
  385. len += 4;
  386. }
  387. err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
  388. if (err < 0) {
  389. printf("WARNING: could not set %s %s.\n",
  390. "reg", fdt_strerror(err));
  391. return err;
  392. }
  393. return 0;
  394. }
  395. void fdt_fixup_ethernet(void *fdt)
  396. {
  397. int node, i, j;
  398. char enet[16], *tmp, *end;
  399. char mac[16] = "ethaddr";
  400. const char *path;
  401. unsigned char mac_addr[6];
  402. node = fdt_path_offset(fdt, "/aliases");
  403. if (node < 0)
  404. return;
  405. i = 0;
  406. while ((tmp = getenv(mac)) != NULL) {
  407. sprintf(enet, "ethernet%d", i);
  408. path = fdt_getprop(fdt, node, enet, NULL);
  409. if (!path) {
  410. debug("No alias for %s\n", enet);
  411. sprintf(mac, "eth%daddr", ++i);
  412. continue;
  413. }
  414. for (j = 0; j < 6; j++) {
  415. mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  416. if (tmp)
  417. tmp = (*end) ? end+1 : end;
  418. }
  419. do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
  420. do_fixup_by_path(fdt, path, "local-mac-address",
  421. &mac_addr, 6, 1);
  422. sprintf(mac, "eth%daddr", ++i);
  423. }
  424. }
  425. /* Resize the fdt to its actual size + a bit of padding */
  426. int fdt_resize(void *blob)
  427. {
  428. int i;
  429. uint64_t addr, size;
  430. int total, ret;
  431. uint actualsize;
  432. if (!blob)
  433. return 0;
  434. total = fdt_num_mem_rsv(blob);
  435. for (i = 0; i < total; i++) {
  436. fdt_get_mem_rsv(blob, i, &addr, &size);
  437. if (addr == (uint64_t)(u32)blob) {
  438. fdt_del_mem_rsv(blob, i);
  439. break;
  440. }
  441. }
  442. /*
  443. * Calculate the actual size of the fdt
  444. * plus the size needed for 5 fdt_add_mem_rsv, one
  445. * for the fdt itself and 4 for a possible initrd
  446. * ((initrd-start + initrd-end) * 2 (name & value))
  447. */
  448. actualsize = fdt_off_dt_strings(blob) +
  449. fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
  450. /* Make it so the fdt ends on a page boundary */
  451. actualsize = ALIGN(actualsize + ((uint)blob & 0xfff), 0x1000);
  452. actualsize = actualsize - ((uint)blob & 0xfff);
  453. /* Change the fdt header to reflect the correct size */
  454. fdt_set_totalsize(blob, actualsize);
  455. /* Add the new reservation */
  456. ret = fdt_add_mem_rsv(blob, (uint)blob, actualsize);
  457. if (ret < 0)
  458. return ret;
  459. return actualsize;
  460. }
  461. #ifdef CONFIG_PCI
  462. #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
  463. #define FDT_PCI_PREFETCH (0x40000000)
  464. #define FDT_PCI_MEM32 (0x02000000)
  465. #define FDT_PCI_IO (0x01000000)
  466. #define FDT_PCI_MEM64 (0x03000000)
  467. int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
  468. int addrcell, sizecell, len, r;
  469. u32 *dma_range;
  470. /* sized based on pci addr cells, size-cells, & address-cells */
  471. u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
  472. addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
  473. sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
  474. dma_range = &dma_ranges[0];
  475. for (r = 0; r < hose->region_count; r++) {
  476. u64 bus_start, phys_start, size;
  477. /* skip if !PCI_REGION_SYS_MEMORY */
  478. if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
  479. continue;
  480. bus_start = (u64)hose->regions[r].bus_start;
  481. phys_start = (u64)hose->regions[r].phys_start;
  482. size = (u64)hose->regions[r].size;
  483. dma_range[0] = 0;
  484. if (size >= 0x100000000ull)
  485. dma_range[0] |= FDT_PCI_MEM64;
  486. else
  487. dma_range[0] |= FDT_PCI_MEM32;
  488. if (hose->regions[r].flags & PCI_REGION_PREFETCH)
  489. dma_range[0] |= FDT_PCI_PREFETCH;
  490. #ifdef CONFIG_SYS_PCI_64BIT
  491. dma_range[1] = bus_start >> 32;
  492. #else
  493. dma_range[1] = 0;
  494. #endif
  495. dma_range[2] = bus_start & 0xffffffff;
  496. if (addrcell == 2) {
  497. dma_range[3] = phys_start >> 32;
  498. dma_range[4] = phys_start & 0xffffffff;
  499. } else {
  500. dma_range[3] = phys_start & 0xffffffff;
  501. }
  502. if (sizecell == 2) {
  503. dma_range[3 + addrcell + 0] = size >> 32;
  504. dma_range[3 + addrcell + 1] = size & 0xffffffff;
  505. } else {
  506. dma_range[3 + addrcell + 0] = size & 0xffffffff;
  507. }
  508. dma_range += (3 + addrcell + sizecell);
  509. }
  510. len = dma_range - &dma_ranges[0];
  511. if (len)
  512. fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
  513. return 0;
  514. }
  515. #endif
  516. #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
  517. /*
  518. * Provide a weak default function to return the flash bank size.
  519. * There might be multiple non-identical flash chips connected to one
  520. * chip-select, so we need to pass an index as well.
  521. */
  522. u32 __flash_get_bank_size(int cs, int idx)
  523. {
  524. extern flash_info_t flash_info[];
  525. /*
  526. * As default, a simple 1:1 mapping is provided. Boards with
  527. * a different mapping need to supply a board specific mapping
  528. * routine.
  529. */
  530. return flash_info[cs].size;
  531. }
  532. u32 flash_get_bank_size(int cs, int idx)
  533. __attribute__((weak, alias("__flash_get_bank_size")));
  534. /*
  535. * This function can be used to update the size in the "reg" property
  536. * of all NOR FLASH device nodes. This is necessary for boards with
  537. * non-fixed NOR FLASH sizes.
  538. */
  539. int fdt_fixup_nor_flash_size(void *blob)
  540. {
  541. char compat[][16] = { "cfi-flash", "jedec-flash" };
  542. int off;
  543. int len;
  544. struct fdt_property *prop;
  545. u32 *reg, *reg2;
  546. int i;
  547. for (i = 0; i < 2; i++) {
  548. off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
  549. while (off != -FDT_ERR_NOTFOUND) {
  550. int idx;
  551. /*
  552. * Found one compatible node, so fixup the size
  553. * int its reg properties
  554. */
  555. prop = fdt_get_property_w(blob, off, "reg", &len);
  556. if (prop) {
  557. int tuple_size = 3 * sizeof(reg);
  558. /*
  559. * There might be multiple reg-tuples,
  560. * so loop through them all
  561. */
  562. reg = reg2 = (u32 *)&prop->data[0];
  563. for (idx = 0; idx < (len / tuple_size); idx++) {
  564. /*
  565. * Update size in reg property
  566. */
  567. reg[2] = flash_get_bank_size(reg[0],
  568. idx);
  569. /*
  570. * Point to next reg tuple
  571. */
  572. reg += 3;
  573. }
  574. fdt_setprop(blob, off, "reg", reg2, len);
  575. }
  576. /* Move to next compatible node */
  577. off = fdt_node_offset_by_compatible(blob, off,
  578. compat[i]);
  579. }
  580. }
  581. return 0;
  582. }
  583. #endif
  584. #ifdef CONFIG_FDT_FIXUP_PARTITIONS
  585. #include <jffs2/load_kernel.h>
  586. #include <mtd_node.h>
  587. struct reg_cell {
  588. unsigned int r0;
  589. unsigned int r1;
  590. };
  591. int fdt_del_subnodes(const void *blob, int parent_offset)
  592. {
  593. int off, ndepth;
  594. int ret;
  595. for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
  596. (off >= 0) && (ndepth > 0);
  597. off = fdt_next_node(blob, off, &ndepth)) {
  598. if (ndepth == 1) {
  599. debug("delete %s: offset: %x\n",
  600. fdt_get_name(blob, off, 0), off);
  601. ret = fdt_del_node((void *)blob, off);
  602. if (ret < 0) {
  603. printf("Can't delete node: %s\n",
  604. fdt_strerror(ret));
  605. return ret;
  606. } else {
  607. ndepth = 0;
  608. off = parent_offset;
  609. }
  610. }
  611. }
  612. return 0;
  613. }
  614. int fdt_increase_size(void *fdt, int add_len)
  615. {
  616. int newlen;
  617. newlen = fdt_totalsize(fdt) + add_len;
  618. /* Open in place with a new len */
  619. return fdt_open_into(fdt, fdt, newlen);
  620. }
  621. int fdt_del_partitions(void *blob, int parent_offset)
  622. {
  623. const void *prop;
  624. int ndepth = 0;
  625. int off;
  626. int ret;
  627. off = fdt_next_node(blob, parent_offset, &ndepth);
  628. if (off > 0 && ndepth == 1) {
  629. prop = fdt_getprop(blob, off, "label", NULL);
  630. if (prop == NULL) {
  631. /*
  632. * Could not find label property, nand {}; node?
  633. * Check subnode, delete partitions there if any.
  634. */
  635. return fdt_del_partitions(blob, off);
  636. } else {
  637. ret = fdt_del_subnodes(blob, parent_offset);
  638. if (ret < 0) {
  639. printf("Can't remove subnodes: %s\n",
  640. fdt_strerror(ret));
  641. return ret;
  642. }
  643. }
  644. }
  645. return 0;
  646. }
  647. int fdt_node_set_part_info(void *blob, int parent_offset,
  648. struct mtd_device *dev)
  649. {
  650. struct list_head *pentry;
  651. struct part_info *part;
  652. struct reg_cell cell;
  653. int off, ndepth = 0;
  654. int part_num, ret;
  655. char buf[64];
  656. ret = fdt_del_partitions(blob, parent_offset);
  657. if (ret < 0)
  658. return ret;
  659. /*
  660. * Check if it is nand {}; subnode, adjust
  661. * the offset in this case
  662. */
  663. off = fdt_next_node(blob, parent_offset, &ndepth);
  664. if (off > 0 && ndepth == 1)
  665. parent_offset = off;
  666. part_num = 0;
  667. list_for_each_prev(pentry, &dev->parts) {
  668. int newoff;
  669. part = list_entry(pentry, struct part_info, link);
  670. debug("%2d: %-20s0x%08x\t0x%08x\t%d\n",
  671. part_num, part->name, part->size,
  672. part->offset, part->mask_flags);
  673. sprintf(buf, "partition@%x", part->offset);
  674. add_sub:
  675. ret = fdt_add_subnode(blob, parent_offset, buf);
  676. if (ret == -FDT_ERR_NOSPACE) {
  677. ret = fdt_increase_size(blob, 512);
  678. if (!ret)
  679. goto add_sub;
  680. else
  681. goto err_size;
  682. } else if (ret < 0) {
  683. printf("Can't add partition node: %s\n",
  684. fdt_strerror(ret));
  685. return ret;
  686. }
  687. newoff = ret;
  688. /* Check MTD_WRITEABLE_CMD flag */
  689. if (part->mask_flags & 1) {
  690. add_ro:
  691. ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
  692. if (ret == -FDT_ERR_NOSPACE) {
  693. ret = fdt_increase_size(blob, 512);
  694. if (!ret)
  695. goto add_ro;
  696. else
  697. goto err_size;
  698. } else if (ret < 0)
  699. goto err_prop;
  700. }
  701. cell.r0 = cpu_to_fdt32(part->offset);
  702. cell.r1 = cpu_to_fdt32(part->size);
  703. add_reg:
  704. ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
  705. if (ret == -FDT_ERR_NOSPACE) {
  706. ret = fdt_increase_size(blob, 512);
  707. if (!ret)
  708. goto add_reg;
  709. else
  710. goto err_size;
  711. } else if (ret < 0)
  712. goto err_prop;
  713. add_label:
  714. ret = fdt_setprop_string(blob, newoff, "label", part->name);
  715. if (ret == -FDT_ERR_NOSPACE) {
  716. ret = fdt_increase_size(blob, 512);
  717. if (!ret)
  718. goto add_label;
  719. else
  720. goto err_size;
  721. } else if (ret < 0)
  722. goto err_prop;
  723. part_num++;
  724. }
  725. return 0;
  726. err_size:
  727. printf("Can't increase blob size: %s\n", fdt_strerror(ret));
  728. return ret;
  729. err_prop:
  730. printf("Can't add property: %s\n", fdt_strerror(ret));
  731. return ret;
  732. }
  733. /*
  734. * Update partitions in nor/nand nodes using info from
  735. * mtdparts environment variable. The nodes to update are
  736. * specified by node_info structure which contains mtd device
  737. * type and compatible string: E. g. the board code in
  738. * ft_board_setup() could use:
  739. *
  740. * struct node_info nodes[] = {
  741. * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
  742. * { "cfi-flash", MTD_DEV_TYPE_NOR, },
  743. * };
  744. *
  745. * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
  746. */
  747. void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
  748. {
  749. struct node_info *ni = node_info;
  750. struct mtd_device *dev;
  751. char *parts;
  752. int i, idx;
  753. int noff;
  754. parts = getenv("mtdparts");
  755. if (!parts)
  756. return;
  757. if (mtdparts_init() != 0)
  758. return;
  759. for (i = 0; i < node_info_size; i++) {
  760. idx = 0;
  761. noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
  762. while (noff != -FDT_ERR_NOTFOUND) {
  763. debug("%s: %s, mtd dev type %d\n",
  764. fdt_get_name(blob, noff, 0),
  765. ni[i].compat, ni[i].type);
  766. dev = device_find(ni[i].type, idx++);
  767. if (dev) {
  768. if (fdt_node_set_part_info(blob, noff, dev))
  769. return; /* return on error */
  770. }
  771. /* Jump to next flash node */
  772. noff = fdt_node_offset_by_compatible(blob, noff,
  773. ni[i].compat);
  774. }
  775. }
  776. }
  777. #endif
  778. void fdt_del_node_and_alias(void *blob, const char *alias)
  779. {
  780. int off = fdt_path_offset(blob, alias);
  781. if (off < 0)
  782. return;
  783. fdt_del_node(blob, off);
  784. off = fdt_path_offset(blob, "/aliases");
  785. fdt_delprop(blob, off, alias);
  786. }
  787. /* Helper to read a big number; size is in cells (not bytes) */
  788. static inline u64 of_read_number(const __be32 *cell, int size)
  789. {
  790. u64 r = 0;
  791. while (size--)
  792. r = (r << 32) | be32_to_cpu(*(cell++));
  793. return r;
  794. }
  795. #define PRu64 "%llx"
  796. /* Max address size we deal with */
  797. #define OF_MAX_ADDR_CELLS 4
  798. #define OF_BAD_ADDR ((u64)-1)
  799. #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
  800. (ns) > 0)
  801. /* Debug utility */
  802. #ifdef DEBUG
  803. static void of_dump_addr(const char *s, const u32 *addr, int na)
  804. {
  805. printf("%s", s);
  806. while(na--)
  807. printf(" %08x", *(addr++));
  808. printf("\n");
  809. }
  810. #else
  811. static void of_dump_addr(const char *s, const u32 *addr, int na) { }
  812. #endif
  813. /* Callbacks for bus specific translators */
  814. struct of_bus {
  815. const char *name;
  816. const char *addresses;
  817. void (*count_cells)(void *blob, int parentoffset,
  818. int *addrc, int *sizec);
  819. u64 (*map)(u32 *addr, const u32 *range,
  820. int na, int ns, int pna);
  821. int (*translate)(u32 *addr, u64 offset, int na);
  822. };
  823. /* Default translator (generic bus) */
  824. static void of_bus_default_count_cells(void *blob, int parentoffset,
  825. int *addrc, int *sizec)
  826. {
  827. const u32 *prop;
  828. if (addrc) {
  829. prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
  830. if (prop)
  831. *addrc = be32_to_cpup(prop);
  832. else
  833. *addrc = 2;
  834. }
  835. if (sizec) {
  836. prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
  837. if (prop)
  838. *sizec = be32_to_cpup(prop);
  839. else
  840. *sizec = 1;
  841. }
  842. }
  843. static u64 of_bus_default_map(u32 *addr, const u32 *range,
  844. int na, int ns, int pna)
  845. {
  846. u64 cp, s, da;
  847. cp = of_read_number(range, na);
  848. s = of_read_number(range + na + pna, ns);
  849. da = of_read_number(addr, na);
  850. debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
  851. cp, s, da);
  852. if (da < cp || da >= (cp + s))
  853. return OF_BAD_ADDR;
  854. return da - cp;
  855. }
  856. static int of_bus_default_translate(u32 *addr, u64 offset, int na)
  857. {
  858. u64 a = of_read_number(addr, na);
  859. memset(addr, 0, na * 4);
  860. a += offset;
  861. if (na > 1)
  862. addr[na - 2] = a >> 32;
  863. addr[na - 1] = a & 0xffffffffu;
  864. return 0;
  865. }
  866. /* Array of bus specific translators */
  867. static struct of_bus of_busses[] = {
  868. /* Default */
  869. {
  870. .name = "default",
  871. .addresses = "reg",
  872. .count_cells = of_bus_default_count_cells,
  873. .map = of_bus_default_map,
  874. .translate = of_bus_default_translate,
  875. },
  876. };
  877. static int of_translate_one(void * blob, int parent, struct of_bus *bus,
  878. struct of_bus *pbus, u32 *addr,
  879. int na, int ns, int pna, const char *rprop)
  880. {
  881. const u32 *ranges;
  882. int rlen;
  883. int rone;
  884. u64 offset = OF_BAD_ADDR;
  885. /* Normally, an absence of a "ranges" property means we are
  886. * crossing a non-translatable boundary, and thus the addresses
  887. * below the current not cannot be converted to CPU physical ones.
  888. * Unfortunately, while this is very clear in the spec, it's not
  889. * what Apple understood, and they do have things like /uni-n or
  890. * /ht nodes with no "ranges" property and a lot of perfectly
  891. * useable mapped devices below them. Thus we treat the absence of
  892. * "ranges" as equivalent to an empty "ranges" property which means
  893. * a 1:1 translation at that level. It's up to the caller not to try
  894. * to translate addresses that aren't supposed to be translated in
  895. * the first place. --BenH.
  896. */
  897. ranges = (u32 *)fdt_getprop(blob, parent, rprop, &rlen);
  898. if (ranges == NULL || rlen == 0) {
  899. offset = of_read_number(addr, na);
  900. memset(addr, 0, pna * 4);
  901. debug("OF: no ranges, 1:1 translation\n");
  902. goto finish;
  903. }
  904. debug("OF: walking ranges...\n");
  905. /* Now walk through the ranges */
  906. rlen /= 4;
  907. rone = na + pna + ns;
  908. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  909. offset = bus->map(addr, ranges, na, ns, pna);
  910. if (offset != OF_BAD_ADDR)
  911. break;
  912. }
  913. if (offset == OF_BAD_ADDR) {
  914. debug("OF: not found !\n");
  915. return 1;
  916. }
  917. memcpy(addr, ranges + na, 4 * pna);
  918. finish:
  919. of_dump_addr("OF: parent translation for:", addr, pna);
  920. debug("OF: with offset: "PRu64"\n", offset);
  921. /* Translate it into parent bus space */
  922. return pbus->translate(addr, offset, pna);
  923. }
  924. /*
  925. * Translate an address from the device-tree into a CPU physical address,
  926. * this walks up the tree and applies the various bus mappings on the
  927. * way.
  928. *
  929. * Note: We consider that crossing any level with #size-cells == 0 to mean
  930. * that translation is impossible (that is we are not dealing with a value
  931. * that can be mapped to a cpu physical address). This is not really specified
  932. * that way, but this is traditionally the way IBM at least do things
  933. */
  934. u64 __of_translate_address(void *blob, int node_offset, const u32 *in_addr,
  935. const char *rprop)
  936. {
  937. int parent;
  938. struct of_bus *bus, *pbus;
  939. u32 addr[OF_MAX_ADDR_CELLS];
  940. int na, ns, pna, pns;
  941. u64 result = OF_BAD_ADDR;
  942. debug("OF: ** translation for device %s **\n",
  943. fdt_get_name(blob, node_offset, NULL));
  944. /* Get parent & match bus type */
  945. parent = fdt_parent_offset(blob, node_offset);
  946. if (parent < 0)
  947. goto bail;
  948. bus = &of_busses[0];
  949. /* Cound address cells & copy address locally */
  950. bus->count_cells(blob, parent, &na, &ns);
  951. if (!OF_CHECK_COUNTS(na, ns)) {
  952. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  953. fdt_get_name(blob, node_offset, NULL));
  954. goto bail;
  955. }
  956. memcpy(addr, in_addr, na * 4);
  957. debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  958. bus->name, na, ns, fdt_get_name(blob, parent, NULL));
  959. of_dump_addr("OF: translating address:", addr, na);
  960. /* Translate */
  961. for (;;) {
  962. /* Switch to parent bus */
  963. node_offset = parent;
  964. parent = fdt_parent_offset(blob, node_offset);
  965. /* If root, we have finished */
  966. if (parent < 0) {
  967. debug("OF: reached root node\n");
  968. result = of_read_number(addr, na);
  969. break;
  970. }
  971. /* Get new parent bus and counts */
  972. pbus = &of_busses[0];
  973. pbus->count_cells(blob, parent, &pna, &pns);
  974. if (!OF_CHECK_COUNTS(pna, pns)) {
  975. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  976. fdt_get_name(blob, node_offset, NULL));
  977. break;
  978. }
  979. debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  980. pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
  981. /* Apply bus translation */
  982. if (of_translate_one(blob, node_offset, bus, pbus,
  983. addr, na, ns, pna, rprop))
  984. break;
  985. /* Complete the move up one level */
  986. na = pna;
  987. ns = pns;
  988. bus = pbus;
  989. of_dump_addr("OF: one level translation:", addr, na);
  990. }
  991. bail:
  992. return result;
  993. }
  994. u64 fdt_translate_address(void *blob, int node_offset, const u32 *in_addr)
  995. {
  996. return __of_translate_address(blob, node_offset, in_addr, "ranges");
  997. }
  998. /**
  999. * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
  1000. * who's reg property matches a physical cpu address
  1001. *
  1002. * @blob: ptr to device tree
  1003. * @compat: compatiable string to match
  1004. * @compat_off: property name
  1005. *
  1006. */
  1007. int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
  1008. phys_addr_t compat_off)
  1009. {
  1010. int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
  1011. while (off != -FDT_ERR_NOTFOUND) {
  1012. u32 *reg = (u32 *)fdt_getprop(blob, off, "reg", &len);
  1013. if (reg) {
  1014. if (compat_off == fdt_translate_address(blob, off, reg))
  1015. return off;
  1016. }
  1017. off = fdt_node_offset_by_compatible(blob, off, compat);
  1018. }
  1019. return -FDT_ERR_NOTFOUND;
  1020. }
  1021. /**
  1022. * fdt_alloc_phandle: Return next free phandle value
  1023. *
  1024. * @blob: ptr to device tree
  1025. */
  1026. int fdt_alloc_phandle(void *blob)
  1027. {
  1028. int offset, len, phandle = 0;
  1029. const u32 *val;
  1030. for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
  1031. offset = fdt_next_node(blob, offset, NULL)) {
  1032. val = fdt_getprop(blob, offset, "linux,phandle", &len);
  1033. if (val)
  1034. phandle = max(*val, phandle);
  1035. }
  1036. return phandle + 1;
  1037. }