fdt.c 3.5 KB

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