fdt.c 3.5 KB

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