xref: /rk3399_rockchip-uboot/drivers/net/fm/fdt.c (revision 075affb1ac0cc72e4d599df5f39bd40389312d6a)
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 
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 
28 	/* The first Fman we find will contain the actual firmware. */
29 	fmnode = fdt_node_offset_by_compatible(blob, -1, "fsl,fman");
30 	if (fmnode < 0)
31 		/* Exit silently if there are no Fman devices */
32 		return;
33 
34 	/* If we already have a firmware node, then also exit silently. */
35 	if (fdt_node_offset_by_compatible(blob, -1, "fsl,fman-firmware") > 0)
36 		return;
37 
38 	/* If the environment variable is not set, then exit silently */
39 	p = getenv("fman_ucode");
40 	if (!p)
41 		return;
42 
43 	fmanfw = (struct qe_firmware *)simple_strtoul(p, NULL, 16);
44 	if (!fmanfw)
45 		return;
46 
47 	hdr = &fmanfw->header;
48 	length = be32_to_cpu(hdr->length);
49 
50 	/* Verify the firmware. */
51 	if ((hdr->magic[0] != 'Q') || (hdr->magic[1] != 'E') ||
52 	    (hdr->magic[2] != 'F')) {
53 		printf("Data at %p is not an Fman firmware\n", fmanfw);
54 		return;
55 	}
56 
57 	if (length > CONFIG_SYS_QE_FMAN_FW_LENGTH) {
58 		printf("Fman firmware at %p is too large (size=%u)\n",
59 		       fmanfw, length);
60 		return;
61 	}
62 
63 	length -= sizeof(u32);	/* Subtract the size of the CRC */
64 	crc = be32_to_cpu(*(u32 *)((void *)fmanfw + length));
65 	if (crc != crc32_no_comp(0, (void *)fmanfw, length)) {
66 		printf("Fman firmware at %p has invalid CRC\n", fmanfw);
67 		return;
68 	}
69 
70 	/* Increase the size of the fdt to make room for the node. */
71 	rc = fdt_increase_size(blob, fmanfw->header.length);
72 	if (rc < 0) {
73 		printf("Unable to make room for Fman firmware: %s\n",
74 		       fdt_strerror(rc));
75 		return;
76 	}
77 
78 	/* Create the firmware node. */
79 	fwnode = fdt_add_subnode(blob, fmnode, "fman-firmware");
80 	if (fwnode < 0) {
81 		char s[64];
82 		fdt_get_path(blob, fmnode, s, sizeof(s));
83 		printf("Could not add firmware node to %s: %s\n", s,
84 		       fdt_strerror(fwnode));
85 		return;
86 	}
87 	rc = fdt_setprop_string(blob, fwnode, "compatible",
88 					"fsl,fman-firmware");
89 	if (rc < 0) {
90 		char s[64];
91 		fdt_get_path(blob, fwnode, s, sizeof(s));
92 		printf("Could not add compatible property to node %s: %s\n", s,
93 		       fdt_strerror(rc));
94 		return;
95 	}
96 	phandle = fdt_create_phandle(blob, fwnode);
97 	if (!phandle) {
98 		char s[64];
99 		fdt_get_path(blob, fwnode, s, sizeof(s));
100 		printf("Could not add phandle property to node %s: %s\n", s,
101 		       fdt_strerror(rc));
102 		return;
103 	}
104 	rc = fdt_setprop(blob, fwnode, "fsl,firmware", fmanfw,
105 			 fmanfw->header.length);
106 	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
129