blob: 61257971258fb23b7fbf7cfbc44ff454d78f3f1b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Qianyu Gong8868a642016-02-18 13:02:00 +08002/*
3 * Copyright 2016 Freescale Semiconductor, Inc.
Qianyu Gong8868a642016-02-18 13:02:00 +08004 */
5#include <asm/io.h>
6#include <fsl_qe.h> /* For struct qe_firmware */
7
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 */
17void 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
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
33 /* If we already have a firmware node, then also exit silently. */
34 if (fdt_node_offset_by_compatible(blob, -1, "fsl,fman-firmware") > 0)
35 return;
36
37 /* If the environment variable is not set, then exit silently */
Simon Glass64b723f2017-08-03 12:22:12 -060038 p = env_get("fman_ucode");
Qianyu Gong8868a642016-02-18 13:02:00 +080039 if (!p)
40 return;
41
42 fmanfw = (struct qe_firmware *)simple_strtoul(p, NULL, 16);
43 if (!fmanfw)
44 return;
45
46 hdr = &fmanfw->header;
Qianyu Gong0f331d82016-02-18 13:02:01 +080047 length = fdt32_to_cpu(hdr->length);
Qianyu Gong8868a642016-02-18 13:02:00 +080048
49 /* Verify the firmware. */
50 if ((hdr->magic[0] != 'Q') || (hdr->magic[1] != 'E') ||
51 (hdr->magic[2] != 'F')) {
52 printf("Data at %p is not an Fman firmware\n", fmanfw);
53 return;
54 }
55
56 if (length > CONFIG_SYS_QE_FMAN_FW_LENGTH) {
57 printf("Fman firmware at %p is too large (size=%u)\n",
58 fmanfw, length);
59 return;
60 }
61
62 length -= sizeof(u32); /* Subtract the size of the CRC */
Qianyu Gong0f331d82016-02-18 13:02:01 +080063 crc = fdt32_to_cpu(*(u32 *)((void *)fmanfw + length));
Qianyu Gong8868a642016-02-18 13:02:00 +080064 if (crc != crc32_no_comp(0, (void *)fmanfw, length)) {
65 printf("Fman firmware at %p has invalid CRC\n", fmanfw);
66 return;
67 }
68
Qianyu Gong0f331d82016-02-18 13:02:01 +080069 length += sizeof(u32);
70
Qianyu Gong8868a642016-02-18 13:02:00 +080071 /* Increase the size of the fdt to make room for the node. */
Qianyu Gong0f331d82016-02-18 13:02:01 +080072 rc = fdt_increase_size(blob, length);
Qianyu Gong8868a642016-02-18 13:02:00 +080073 if (rc < 0) {
74 printf("Unable to make room for Fman firmware: %s\n",
75 fdt_strerror(rc));
76 return;
77 }
78
79 /* Create the firmware node. */
80 fwnode = fdt_add_subnode(blob, fmnode, "fman-firmware");
81 if (fwnode < 0) {
82 char s[64];
83 fdt_get_path(blob, fmnode, s, sizeof(s));
84 printf("Could not add firmware node to %s: %s\n", s,
85 fdt_strerror(fwnode));
86 return;
87 }
88 rc = fdt_setprop_string(blob, fwnode, "compatible",
89 "fsl,fman-firmware");
90 if (rc < 0) {
91 char s[64];
92 fdt_get_path(blob, fwnode, s, sizeof(s));
93 printf("Could not add compatible property to node %s: %s\n", s,
94 fdt_strerror(rc));
95 return;
96 }
97 phandle = fdt_create_phandle(blob, fwnode);
98 if (!phandle) {
99 char s[64];
100 fdt_get_path(blob, fwnode, s, sizeof(s));
101 printf("Could not add phandle property to node %s: %s\n", s,
102 fdt_strerror(rc));
103 return;
104 }
Qianyu Gong0f331d82016-02-18 13:02:01 +0800105 rc = fdt_setprop(blob, fwnode, "fsl,firmware", fmanfw, length);
Qianyu Gong8868a642016-02-18 13:02:00 +0800106 if (rc < 0) {
107 char s[64];
108 fdt_get_path(blob, fwnode, s, sizeof(s));
109 printf("Could not add firmware property to node %s: %s\n", s,
110 fdt_strerror(rc));
111 return;
112 }
113
114 /* Find all other Fman nodes and point them to the firmware node. */
115 while ((fmnode = fdt_node_offset_by_compatible(blob, fmnode,
116 "fsl,fman")) > 0) {
117 rc = fdt_setprop_cell(blob, fmnode, "fsl,firmware-phandle",
118 phandle);
119 if (rc < 0) {
120 char s[64];
121 fdt_get_path(blob, fmnode, s, sizeof(s));
122 printf("Could not add pointer property to node %s: %s\n",
123 s, fdt_strerror(rc));
124 return;
125 }
126 }
127}
128#endif