xref: /openbmc/u-boot/drivers/misc/fsl_portals.c (revision 87a62bce)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2008-2011 Freescale Semiconductor, Inc.
4  * Copyright 2017 NXP
5  */
6 
7 #include <common.h>
8 #include <linux/libfdt.h>
9 #include <fdt_support.h>
10 
11 #include <asm/processor.h>
12 #include <asm/io.h>
13 #ifdef CONFIG_PPC
14 #include <asm/fsl_portals.h>
15 #include <asm/fsl_liodn.h>
16 #endif
17 #include <fsl_qbman.h>
18 
19 #define MAX_BPORTALS (CONFIG_SYS_BMAN_CINH_SIZE / CONFIG_SYS_BMAN_SP_CINH_SIZE)
20 #define MAX_QPORTALS (CONFIG_SYS_QMAN_CINH_SIZE / CONFIG_SYS_QMAN_SP_CINH_SIZE)
21 void setup_qbman_portals(void)
22 {
23 	void __iomem *bpaddr = (void *)CONFIG_SYS_BMAN_CINH_BASE +
24 				CONFIG_SYS_BMAN_SWP_ISDR_REG;
25 	void __iomem *qpaddr = (void *)CONFIG_SYS_QMAN_CINH_BASE +
26 				CONFIG_SYS_QMAN_SWP_ISDR_REG;
27 #ifdef CONFIG_PPC
28 	struct ccsr_qman *qman = (void *)CONFIG_SYS_FSL_QMAN_ADDR;
29 
30 	/* Set the Qman initiator BAR to match the LAW (for DQRR stashing) */
31 #ifdef CONFIG_PHYS_64BIT
32 	out_be32(&qman->qcsp_bare, (u32)(CONFIG_SYS_QMAN_MEM_PHYS >> 32));
33 #endif
34 	out_be32(&qman->qcsp_bar, (u32)CONFIG_SYS_QMAN_MEM_PHYS);
35 #endif
36 #ifdef CONFIG_FSL_CORENET
37 	int i;
38 
39 	for (i = 0; i < CONFIG_SYS_QMAN_NUM_PORTALS; i++) {
40 		u8 sdest = qp_info[i].sdest;
41 		u16 fliodn = qp_info[i].fliodn;
42 		u16 dliodn = qp_info[i].dliodn;
43 		u16 liodn_off = qp_info[i].liodn_offset;
44 
45 		out_be32(&qman->qcsp[i].qcsp_lio_cfg, (liodn_off << 16) |
46 					dliodn);
47 		/* set frame liodn */
48 		out_be32(&qman->qcsp[i].qcsp_io_cfg, (sdest << 16) | fliodn);
49 	}
50 #endif
51 
52 	/* Change default state of BMan ISDR portals to all 1s */
53 	inhibit_portals(bpaddr, CONFIG_SYS_BMAN_NUM_PORTALS, MAX_BPORTALS,
54 			CONFIG_SYS_BMAN_SP_CINH_SIZE);
55 	inhibit_portals(qpaddr, CONFIG_SYS_QMAN_NUM_PORTALS, MAX_QPORTALS,
56 			CONFIG_SYS_QMAN_SP_CINH_SIZE);
57 }
58 
59 void inhibit_portals(void __iomem *addr, int max_portals,
60 		     int arch_max_portals, int portal_cinh_size)
61 {
62 	u32 val;
63 	int i;
64 
65 	/* arch_max_portals is the maximum based on memory size. This includes
66 	 * the reserved memory in the SoC.  max_portals the number of physical
67 	 * portals in the SoC
68 	 */
69 	if (max_portals > arch_max_portals) {
70 		printf("ERROR: portal config error\n");
71 		max_portals = arch_max_portals;
72 	}
73 
74 	for (i = 0; i < max_portals; i++) {
75 		out_be32(addr, -1);
76 		val = in_be32(addr);
77 		if (!val) {
78 			printf("ERROR: Stopped after %d portals\n", i);
79 			return;
80 		}
81 		addr += portal_cinh_size;
82 	}
83 	debug("Cleared %d portals\n", i);
84 }
85 
86 #ifdef CONFIG_PPC
87 static int fdt_qportal(void *blob, int off, int id, char *name,
88 		       enum fsl_dpaa_dev dev, int create)
89 {
90 	int childoff, dev_off, ret = 0;
91 	u32 dev_handle;
92 #ifdef CONFIG_FSL_CORENET
93 	int num;
94 	u32 liodns[2];
95 #endif
96 
97 	childoff = fdt_subnode_offset(blob, off, name);
98 	if (create) {
99 		char handle[64], *p;
100 
101 		strncpy(handle, name, sizeof(handle));
102 		p = strchr(handle, '@');
103 		if (!strncmp(name, "fman", 4)) {
104 			*p = *(p + 1);
105 			p++;
106 		}
107 		*p = '\0';
108 
109 		dev_off = fdt_path_offset(blob, handle);
110 		/* skip this node if alias is not found */
111 		if (dev_off == -FDT_ERR_BADPATH)
112 			return 0;
113 		if (dev_off < 0)
114 			return dev_off;
115 
116 		if (childoff <= 0)
117 			childoff = fdt_add_subnode(blob, off, name);
118 
119 		/* need to update the dev_off after adding a subnode */
120 		dev_off = fdt_path_offset(blob, handle);
121 		if (dev_off < 0)
122 			return dev_off;
123 
124 		if (childoff > 0) {
125 			dev_handle = fdt_get_phandle(blob, dev_off);
126 			if (dev_handle <= 0) {
127 				dev_handle = fdt_alloc_phandle(blob);
128 				ret = fdt_set_phandle(blob, dev_off,
129 						      dev_handle);
130 				if (ret < 0)
131 					return ret;
132 			}
133 
134 			ret = fdt_setprop(blob, childoff, "dev-handle",
135 					  &dev_handle, sizeof(dev_handle));
136 			if (ret < 0)
137 				return ret;
138 
139 #ifdef CONFIG_FSL_CORENET
140 			num = get_dpaa_liodn(dev, &liodns[0], id);
141 			ret = fdt_setprop(blob, childoff, "fsl,liodn",
142 					  &liodns[0], sizeof(u32) * num);
143 			if (!strncmp(name, "pme", 3)) {
144 				u32 pme_rev1, pme_rev2;
145 				ccsr_pme_t *pme_regs =
146 					(void *)CONFIG_SYS_FSL_CORENET_PME_ADDR;
147 
148 				pme_rev1 = in_be32(&pme_regs->pm_ip_rev_1);
149 				pme_rev2 = in_be32(&pme_regs->pm_ip_rev_2);
150 				ret = fdt_setprop(blob, childoff,
151 						  "fsl,pme-rev1", &pme_rev1,
152 						  sizeof(u32));
153 				if (ret < 0)
154 					return ret;
155 				ret = fdt_setprop(blob, childoff,
156 						  "fsl,pme-rev2", &pme_rev2,
157 						  sizeof(u32));
158 			}
159 #endif
160 		} else {
161 			return childoff;
162 		}
163 	} else {
164 		if (childoff > 0)
165 			ret = fdt_del_node(blob, childoff);
166 	}
167 
168 	return ret;
169 }
170 #endif /* CONFIG_PPC */
171 
172 void fdt_fixup_qportals(void *blob)
173 {
174 	int off, err;
175 	unsigned int maj, min;
176 	unsigned int ip_cfg;
177 	struct ccsr_qman *qman = (void *)CONFIG_SYS_FSL_QMAN_ADDR;
178 	u32 rev_1 = in_be32(&qman->ip_rev_1);
179 	u32 rev_2 = in_be32(&qman->ip_rev_2);
180 	char compat[64];
181 	int compat_len;
182 
183 	maj = (rev_1 >> 8) & 0xff;
184 	min = rev_1 & 0xff;
185 	ip_cfg = rev_2 & 0xff;
186 
187 	compat_len = sprintf(compat, "fsl,qman-portal-%u.%u.%u",
188 			     maj, min, ip_cfg) + 1;
189 	compat_len += sprintf(compat + compat_len, "fsl,qman-portal") + 1;
190 
191 	off = fdt_node_offset_by_compatible(blob, -1, "fsl,qman-portal");
192 	while (off != -FDT_ERR_NOTFOUND) {
193 #ifdef CONFIG_PPC
194 #ifdef CONFIG_FSL_CORENET
195 		u32 liodns[2];
196 #endif
197 		const int *ci = fdt_getprop(blob, off, "cell-index", &err);
198 		int i;
199 
200 		if (!ci)
201 			goto err;
202 
203 		i = *ci;
204 #ifdef CONFIG_SYS_DPAA_FMAN
205 		int j;
206 #endif
207 
208 #endif /* CONFIG_PPC */
209 		err = fdt_setprop(blob, off, "compatible", compat, compat_len);
210 		if (err < 0)
211 			goto err;
212 #ifdef CONFIG_PPC
213 #ifdef CONFIG_FSL_CORENET
214 		liodns[0] = qp_info[i].dliodn;
215 		liodns[1] = qp_info[i].fliodn;
216 		err = fdt_setprop(blob, off, "fsl,liodn",
217 				  &liodns, sizeof(u32) * 2);
218 		if (err < 0)
219 			goto err;
220 #endif
221 
222 		i++;
223 
224 		err = fdt_qportal(blob, off, i, "crypto@0", FSL_HW_PORTAL_SEC,
225 				  IS_E_PROCESSOR(get_svr()));
226 		if (err < 0)
227 			goto err;
228 
229 #ifdef CONFIG_FSL_CORENET
230 #ifdef CONFIG_SYS_DPAA_PME
231 		err = fdt_qportal(blob, off, i, "pme@0", FSL_HW_PORTAL_PME, 1);
232 		if (err < 0)
233 			goto err;
234 #else
235 		fdt_qportal(blob, off, i, "pme@0", FSL_HW_PORTAL_PME, 0);
236 #endif
237 #endif
238 
239 #ifdef CONFIG_SYS_DPAA_FMAN
240 		for (j = 0; j < CONFIG_SYS_NUM_FMAN; j++) {
241 			char name[] = "fman@0";
242 
243 			name[sizeof(name) - 2] = '0' + j;
244 			err = fdt_qportal(blob, off, i, name,
245 					  FSL_HW_PORTAL_FMAN1 + j, 1);
246 			if (err < 0)
247 				goto err;
248 		}
249 #endif
250 #ifdef CONFIG_SYS_DPAA_RMAN
251 		err = fdt_qportal(blob, off, i, "rman@0",
252 				  FSL_HW_PORTAL_RMAN, 1);
253 		if (err < 0)
254 			goto err;
255 #endif
256 #endif /* CONFIG_PPC */
257 
258 err:
259 		if (err < 0) {
260 			printf("ERROR: unable to create props for %s: %s\n",
261 			       fdt_get_name(blob, off, NULL),
262 			       fdt_strerror(err));
263 			return;
264 		}
265 
266 		off = fdt_node_offset_by_compatible(blob, off,
267 						    "fsl,qman-portal");
268 	}
269 }
270 
271 void fdt_fixup_bportals(void *blob)
272 {
273 	int off, err;
274 	unsigned int maj, min;
275 	unsigned int ip_cfg;
276 	struct ccsr_bman *bman = (void *)CONFIG_SYS_FSL_BMAN_ADDR;
277 	u32 rev_1 = in_be32(&bman->ip_rev_1);
278 	u32 rev_2 = in_be32(&bman->ip_rev_2);
279 	char compat[64];
280 	int compat_len;
281 
282 	maj = (rev_1 >> 8) & 0xff;
283 	min = rev_1 & 0xff;
284 
285 	ip_cfg = rev_2 & 0xff;
286 
287 	compat_len = sprintf(compat, "fsl,bman-portal-%u.%u.%u",
288 			     maj, min, ip_cfg) + 1;
289 	compat_len += sprintf(compat + compat_len, "fsl,bman-portal") + 1;
290 
291 	off = fdt_node_offset_by_compatible(blob, -1, "fsl,bman-portal");
292 	while (off != -FDT_ERR_NOTFOUND) {
293 		err = fdt_setprop(blob, off, "compatible", compat, compat_len);
294 		if (err < 0) {
295 			printf("ERROR: unable to create props for %s: %s\n",
296 			       fdt_get_name(blob, off, NULL),
297 			       fdt_strerror(err));
298 			return;
299 		}
300 
301 		off = fdt_node_offset_by_compatible(blob, off,
302 						    "fsl,bman-portal");
303 	}
304 }
305