fdt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2016 Freescale Semiconductor, Inc.
  4. */
  5. #include <asm/io.h>
  6. #include <env.h>
  7. #include <fsl_qe.h> /* For struct qe_firmware */
  8. #include <u-boot/crc.h>
  9. #ifdef CONFIG_SYS_DPAA_FMAN
  10. /**
  11. * fdt_fixup_fman_firmware -- insert the Fman firmware into the device tree
  12. *
  13. * The binding for an Fman firmware node is documented in
  14. * Documentation/powerpc/dts-bindings/fsl/dpaa/fman.txt. This node contains
  15. * the actual Fman firmware binary data. The operating system is expected to
  16. * be able to parse the binary data to determine any attributes it needs.
  17. */
  18. void fdt_fixup_fman_firmware(void *blob)
  19. {
  20. int rc, fmnode, fwnode = -1;
  21. uint32_t phandle;
  22. struct qe_firmware *fmanfw;
  23. const struct qe_header *hdr;
  24. unsigned int length;
  25. uint32_t crc;
  26. const char *p;
  27. /* The first Fman we find will contain the actual firmware. */
  28. fmnode = fdt_node_offset_by_compatible(blob, -1, "fsl,fman");
  29. if (fmnode < 0)
  30. /* Exit silently if there are no Fman devices */
  31. return;
  32. /* If we already have a firmware node, then also exit silently. */
  33. if (fdt_node_offset_by_compatible(blob, -1, "fsl,fman-firmware") > 0)
  34. return;
  35. /* If the environment variable is not set, then exit silently */
  36. p = env_get("fman_ucode");
  37. if (!p)
  38. return;
  39. fmanfw = (struct qe_firmware *)simple_strtoul(p, NULL, 16);
  40. if (!fmanfw)
  41. return;
  42. hdr = &fmanfw->header;
  43. length = fdt32_to_cpu(hdr->length);
  44. /* Verify the firmware. */
  45. if ((hdr->magic[0] != 'Q') || (hdr->magic[1] != 'E') ||
  46. (hdr->magic[2] != 'F')) {
  47. printf("Data at %p is not an Fman firmware\n", fmanfw);
  48. return;
  49. }
  50. if (length > CONFIG_SYS_QE_FMAN_FW_LENGTH) {
  51. printf("Fman firmware at %p is too large (size=%u)\n",
  52. fmanfw, length);
  53. return;
  54. }
  55. length -= sizeof(u32); /* Subtract the size of the CRC */
  56. crc = fdt32_to_cpu(*(u32 *)((void *)fmanfw + length));
  57. if (crc != crc32_no_comp(0, (void *)fmanfw, length)) {
  58. printf("Fman firmware at %p has invalid CRC\n", fmanfw);
  59. return;
  60. }
  61. length += sizeof(u32);
  62. /* Increase the size of the fdt to make room for the node. */
  63. rc = fdt_increase_size(blob, length);
  64. if (rc < 0) {
  65. printf("Unable to make room for Fman firmware: %s\n",
  66. fdt_strerror(rc));
  67. return;
  68. }
  69. /* Create the firmware node. */
  70. fwnode = fdt_add_subnode(blob, fmnode, "fman-firmware");
  71. if (fwnode < 0) {
  72. char s[64];
  73. fdt_get_path(blob, fmnode, s, sizeof(s));
  74. printf("Could not add firmware node to %s: %s\n", s,
  75. fdt_strerror(fwnode));
  76. return;
  77. }
  78. rc = fdt_setprop_string(blob, fwnode, "compatible",
  79. "fsl,fman-firmware");
  80. if (rc < 0) {
  81. char s[64];
  82. fdt_get_path(blob, fwnode, s, sizeof(s));
  83. printf("Could not add compatible property to node %s: %s\n", s,
  84. fdt_strerror(rc));
  85. return;
  86. }
  87. phandle = fdt_create_phandle(blob, fwnode);
  88. if (!phandle) {
  89. char s[64];
  90. fdt_get_path(blob, fwnode, s, sizeof(s));
  91. printf("Could not add phandle property to node %s: %s\n", s,
  92. fdt_strerror(rc));
  93. return;
  94. }
  95. rc = fdt_setprop(blob, fwnode, "fsl,firmware", fmanfw, length);
  96. if (rc < 0) {
  97. char s[64];
  98. fdt_get_path(blob, fwnode, s, sizeof(s));
  99. printf("Could not add firmware property to node %s: %s\n", s,
  100. fdt_strerror(rc));
  101. return;
  102. }
  103. /* Find all other Fman nodes and point them to the firmware node. */
  104. while ((fmnode = fdt_node_offset_by_compatible(blob, fmnode,
  105. "fsl,fman")) > 0) {
  106. rc = fdt_setprop_cell(blob, fmnode, "fsl,firmware-phandle",
  107. phandle);
  108. if (rc < 0) {
  109. char s[64];
  110. fdt_get_path(blob, fmnode, s, sizeof(s));
  111. printf("Could not add pointer property to node %s: %s\n",
  112. s, fdt_strerror(rc));
  113. return;
  114. }
  115. }
  116. }
  117. #endif