blob: 4f84403d956caea188cf3952f0afe9ca3fd8cd25 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
J. German Rivera8ff14b72014-06-23 15:15:55 -07002/*
Yogesh Gaur1a0c4ae2018-05-09 10:52:17 +05303 * Copyright 2014 Freescale Semiconductor, Inc.
Cosmin-Florin Aluchenesei971dfd32021-07-21 19:13:33 +03004 * Copyright 2017-2018, 2020-2021 NXP
J. German Rivera8ff14b72014-06-23 15:15:55 -07005 */
Stuart Yodera2bdb772015-07-02 11:29:03 +05306#include <common.h>
Simon Glassadaaa482019-11-14 12:57:43 -07007#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -07008#include <cpu_func.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -06009#include <env.h>
J. German Rivera8ff14b72014-06-23 15:15:55 -070010#include <errno.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060011#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <malloc.h>
Cosmin-Florin Alucheneseiaa280142021-10-07 13:07:44 +030014#include <mapmem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Masahiro Yamada78eeb912016-01-24 23:27:48 +090016#include <linux/bug.h>
J. German Rivera8ff14b72014-06-23 15:15:55 -070017#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090019#include <linux/libfdt.h>
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +000020#include <net.h>
Stuart Yodera2bdb772015-07-02 11:29:03 +053021#include <fdt_support.h>
J. German Rivera43e4ae32015-01-06 13:19:02 -080022#include <fsl-mc/fsl_mc.h>
23#include <fsl-mc/fsl_mc_sys.h>
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070024#include <fsl-mc/fsl_mc_private.h>
J. German Rivera43e4ae32015-01-06 13:19:02 -080025#include <fsl-mc/fsl_dpmng.h>
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070026#include <fsl-mc/fsl_dprc.h>
27#include <fsl-mc/fsl_dpio.h>
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +053028#include <fsl-mc/fsl_dpni.h>
Florinel Iordacheae6d8522019-11-19 10:28:17 +000029#include <fsl-mc/fsl_dpsparser.h>
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070030#include <fsl-mc/fsl_qbman_portal.h>
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +053031#include <fsl-mc/ldpaa_wriop.h>
J. German Rivera8ff14b72014-06-23 15:15:55 -070032
J. German Riveraf4fed4b2015-03-20 19:28:18 -070033#define MC_RAM_BASE_ADDR_ALIGNMENT (512UL * 1024 * 1024)
34#define MC_RAM_BASE_ADDR_ALIGNMENT_MASK (~(MC_RAM_BASE_ADDR_ALIGNMENT - 1))
35#define MC_RAM_SIZE_ALIGNMENT (256UL * 1024 * 1024)
36
37#define MC_MEM_SIZE_ENV_VAR "mcmemsize"
38#define MC_BOOT_TIMEOUT_ENV_VAR "mcboottimeout"
Bogdan Purcareata08bc0142017-05-24 16:40:21 +000039#define MC_BOOT_ENV_VAR "mcinitcmd"
Meenakshi Aggarwal67f195c2019-02-27 14:41:02 +053040#define MC_DRAM_BLOCK_DEFAULT_SIZE (512UL * 1024 * 1024)
J. German Riveraf4fed4b2015-03-20 19:28:18 -070041
Cosmin-Florin Alucheneseiaa280142021-10-07 13:07:44 +030042#define MC_BUFFER_SIZE (1024 * 1024 * 16)
43#define MAGIC_MC 0x4d430100
44#define MC_FW_ADDR_MASK_LOW 0xE0000000
45#define MC_FW_ADDR_MASK_HIGH 0X1FFFF
46#define MC_STRUCT_BUFFER_OFFSET 0x01000000
47#define MC_OFFSET_DELTA MC_STRUCT_BUFFER_OFFSET
48
49#define LOG_HEADER_FLAG_BUFFER_WRAPAROUND 0x80000000
50#define LAST_BYTE(a) ((a) & ~(LOG_HEADER_FLAG_BUFFER_WRAPAROUND))
51
J. German Rivera8ff14b72014-06-23 15:15:55 -070052DECLARE_GLOBAL_DATA_PTR;
Prabhakar Kushwaha145e4cd2018-08-27 12:58:52 +053053static int mc_memset_resv_ram;
Florinel Iordacheae6d8522019-11-19 10:28:17 +000054static struct mc_version mc_ver_info;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +053055static int mc_boot_status = -1;
56static int mc_dpl_applied = -1;
Tom Rini6a5dccc2022-11-16 13:10:41 -050057#ifdef CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +053058static int mc_aiop_applied = -1;
59#endif
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +053060struct fsl_mc_io *root_mc_io = NULL;
61struct fsl_mc_io *dflt_mc_io = NULL; /* child container */
62uint16_t root_dprc_handle = 0;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070063uint16_t dflt_dprc_handle = 0;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +053064int child_dprc_id;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070065struct fsl_dpbp_obj *dflt_dpbp = NULL;
66struct fsl_dpio_obj *dflt_dpio = NULL;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +053067struct fsl_dpni_obj *dflt_dpni = NULL;
Alexander Graf2ebeb442016-11-17 01:02:57 +010068static u64 mc_lazy_dpl_addr;
Florinel Iordacheae6d8522019-11-19 10:28:17 +000069static u32 dpsparser_obj_id;
70static u16 dpsparser_handle;
71static char *mc_err_msg_apply_spb[] = MC_ERROR_MSG_APPLY_SPB;
J. German Riveraf4fed4b2015-03-20 19:28:18 -070072
73#ifdef DEBUG
74void dump_ram_words(const char *title, void *addr)
75{
76 int i;
77 uint32_t *words = addr;
78
79 printf("Dumping beginning of %s (%p):\n", title, addr);
80 for (i = 0; i < 16; i++)
81 printf("%#x ", words[i]);
82
83 printf("\n");
84}
J. German Rivera8ff14b72014-06-23 15:15:55 -070085
J. German Riveraf4fed4b2015-03-20 19:28:18 -070086void dump_mc_ccsr_regs(struct mc_ccsr_registers __iomem *mc_ccsr_regs)
87{
88 printf("MC CCSR registers:\n"
89 "reg_gcr1 %#x\n"
90 "reg_gsr %#x\n"
91 "reg_sicbalr %#x\n"
92 "reg_sicbahr %#x\n"
93 "reg_sicapr %#x\n"
94 "reg_mcfbalr %#x\n"
95 "reg_mcfbahr %#x\n"
96 "reg_mcfapr %#x\n"
97 "reg_psr %#x\n",
98 mc_ccsr_regs->reg_gcr1,
99 mc_ccsr_regs->reg_gsr,
100 mc_ccsr_regs->reg_sicbalr,
101 mc_ccsr_regs->reg_sicbahr,
102 mc_ccsr_regs->reg_sicapr,
103 mc_ccsr_regs->reg_mcfbalr,
104 mc_ccsr_regs->reg_mcfbahr,
105 mc_ccsr_regs->reg_mcfapr,
106 mc_ccsr_regs->reg_psr);
107}
108#else
109
110#define dump_ram_words(title, addr)
111#define dump_mc_ccsr_regs(mc_ccsr_regs)
112
113#endif /* DEBUG */
114
J. German Rivera8ff14b72014-06-23 15:15:55 -0700115/**
116 * Copying MC firmware or DPL image to DDR
117 */
118static int mc_copy_image(const char *title,
J. German Rivera43e4ae32015-01-06 13:19:02 -0800119 u64 image_addr, u32 image_size, u64 mc_ram_addr)
J. German Rivera8ff14b72014-06-23 15:15:55 -0700120{
121 debug("%s copied to address %p\n", title, (void *)mc_ram_addr);
122 memcpy((void *)mc_ram_addr, (void *)image_addr, image_size);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700123 flush_dcache_range(mc_ram_addr, mc_ram_addr + image_size);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700124 return 0;
125}
126
Florinel Iordacheae6d8522019-11-19 10:28:17 +0000127#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Rivera8ff14b72014-06-23 15:15:55 -0700128/**
129 * MC firmware FIT image parser checks if the image is in FIT
130 * format, verifies integrity of the image and calculates
131 * raw image address and size values.
J. German Rivera43e4ae32015-01-06 13:19:02 -0800132 * Returns 0 on success and a negative errno on error.
J. German Rivera8ff14b72014-06-23 15:15:55 -0700133 * task fail.
134 **/
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530135int parse_mc_firmware_fit_image(u64 mc_fw_addr,
136 const void **raw_image_addr,
J. German Rivera8ff14b72014-06-23 15:15:55 -0700137 size_t *raw_image_size)
138{
139 int format;
Sean Anderson4b2c94f2022-08-16 11:16:06 -0400140 void *fit_hdr = (void *)mc_fw_addr;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700141
142 /* Check if Image is in FIT format */
143 format = genimg_get_format(fit_hdr);
144
145 if (format != IMAGE_FORMAT_FIT) {
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530146 printf("fsl-mc: ERR: Bad firmware image (not a FIT image)\n");
J. German Rivera43e4ae32015-01-06 13:19:02 -0800147 return -EINVAL;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700148 }
149
Simon Glassd563c252021-02-15 17:08:09 -0700150 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530151 printf("fsl-mc: ERR: Bad firmware image (bad FIT header)\n");
J. German Rivera43e4ae32015-01-06 13:19:02 -0800152 return -EINVAL;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700153 }
154
Sean Anderson4b2c94f2022-08-16 11:16:06 -0400155 return fit_get_data_node(fit_hdr, "firmware", raw_image_addr,
156 raw_image_size);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700157}
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700158#endif
159
Bogdan Purcareata72669142017-05-24 16:40:22 +0000160#define MC_DT_INCREASE_SIZE 64
161
162enum mc_fixup_type {
163 MC_FIXUP_DPL,
164 MC_FIXUP_DPC
165};
166
167static int mc_fixup_mac_addr(void *blob, int nodeoffset,
Ioana Ciornei25f7cf72020-03-18 16:47:38 +0200168 const char *propname, struct udevice *eth_dev,
Bogdan Purcareata72669142017-05-24 16:40:22 +0000169 enum mc_fixup_type type)
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000170{
Simon Glassfa20e932020-12-03 16:55:20 -0700171 struct eth_pdata *plat = dev_get_plat(eth_dev);
Ioana Ciornei25f7cf72020-03-18 16:47:38 +0200172 unsigned char *enetaddr = plat->enetaddr;
Simon Glass75e534b2020-12-16 21:20:07 -0700173 int eth_index = dev_seq(eth_dev);
Bogdan Purcareata72669142017-05-24 16:40:22 +0000174 int err = 0, len = 0, size, i;
175 unsigned char env_enetaddr[ARP_HLEN];
176 unsigned int enetaddr_32[ARP_HLEN];
177 void *val = NULL;
178
179 switch (type) {
180 case MC_FIXUP_DPL:
Ioana Ciornei25f7cf72020-03-18 16:47:38 +0200181 /* DPL likes its addresses on 32 * ARP_HLEN bits */
182 for (i = 0; i < ARP_HLEN; i++)
183 enetaddr_32[i] = cpu_to_fdt32(enetaddr[i]);
184 val = enetaddr_32;
185 len = sizeof(enetaddr_32);
186 break;
Bogdan Purcareata72669142017-05-24 16:40:22 +0000187 case MC_FIXUP_DPC:
Ioana Ciornei25f7cf72020-03-18 16:47:38 +0200188 val = enetaddr;
189 len = ARP_HLEN;
190 break;
Bogdan Purcareata72669142017-05-24 16:40:22 +0000191 }
192
193 /* MAC address property present */
194 if (fdt_get_property(blob, nodeoffset, propname, NULL)) {
195 /* u-boot MAC addr randomly assigned - leave the present one */
Ioana Ciornei25f7cf72020-03-18 16:47:38 +0200196 if (!eth_env_get_enetaddr_by_index("eth", eth_index,
Simon Glass399a9ce2017-08-03 12:22:14 -0600197 env_enetaddr))
Bogdan Purcareata72669142017-05-24 16:40:22 +0000198 return err;
199 } else {
200 size = MC_DT_INCREASE_SIZE + strlen(propname) + len;
201 /* make room for mac address property */
202 err = fdt_increase_size(blob, size);
203 if (err) {
204 printf("fdt_increase_size: err=%s\n",
205 fdt_strerror(err));
206 return err;
207 }
208 }
209
210 err = fdt_setprop(blob, nodeoffset, propname, val, len);
211 if (err) {
212 printf("fdt_setprop: err=%s\n", fdt_strerror(err));
213 return err;
214 }
215
216 return err;
217}
218
219#define is_dpni(s) (s != NULL ? !strncmp(s, "dpni@", 5) : 0)
220
221const char *dpl_get_connection_endpoint(void *blob, char *endpoint)
222{
223 int connoffset = fdt_path_offset(blob, "/connections"), off;
224 const char *s1, *s2;
225
226 for (off = fdt_first_subnode(blob, connoffset);
227 off >= 0;
228 off = fdt_next_subnode(blob, off)) {
229 s1 = fdt_stringlist_get(blob, off, "endpoint1", 0, NULL);
230 s2 = fdt_stringlist_get(blob, off, "endpoint2", 0, NULL);
231
232 if (!s1 || !s2)
233 continue;
234
235 if (strcmp(endpoint, s1) == 0)
236 return s2;
237
238 if (strcmp(endpoint, s2) == 0)
239 return s1;
240 }
241
242 return NULL;
243}
244
245static int mc_fixup_dpl_mac_addr(void *blob, int dpmac_id,
Ioana Ciornei25f7cf72020-03-18 16:47:38 +0200246 struct udevice *eth_dev)
Bogdan Purcareata72669142017-05-24 16:40:22 +0000247{
248 int objoff = fdt_path_offset(blob, "/objects");
249 int dpmacoff = -1, dpnioff = -1;
250 const char *endpoint;
251 char mac_name[10];
252 int err;
253
254 sprintf(mac_name, "dpmac@%d", dpmac_id);
255 dpmacoff = fdt_subnode_offset(blob, objoff, mac_name);
256 if (dpmacoff < 0)
257 /* dpmac not defined in DPL, so skip it. */
258 return 0;
259
260 err = mc_fixup_mac_addr(blob, dpmacoff, "mac_addr", eth_dev,
261 MC_FIXUP_DPL);
262 if (err) {
263 printf("Error fixing up dpmac mac_addr in DPL\n");
264 return err;
265 }
266
267 /* now we need to figure out if there is any
268 * DPNI connected to this MAC, so we walk the
269 * connection list
270 */
271 endpoint = dpl_get_connection_endpoint(blob, mac_name);
272 if (!is_dpni(endpoint))
273 return 0;
274
275 /* let's see if we can fixup the DPNI as well */
276 dpnioff = fdt_subnode_offset(blob, objoff, endpoint);
277 if (dpnioff < 0)
278 /* DPNI not defined in DPL in the objects area */
279 return 0;
280
281 return mc_fixup_mac_addr(blob, dpnioff, "mac_addr", eth_dev,
282 MC_FIXUP_DPL);
283}
284
Meenakshi Aggarwald67ae482019-05-23 15:13:43 +0530285void fdt_fixup_mc_ddr(u64 *base, u64 *size)
286{
287 u64 mc_size = mc_get_dram_block_size();
288
289 if (mc_size < MC_DRAM_BLOCK_DEFAULT_SIZE) {
290 *base = mc_get_dram_addr() + mc_size;
291 *size = MC_DRAM_BLOCK_DEFAULT_SIZE - mc_size;
292 }
293}
294
Nipun Guptad6912642018-08-20 16:01:14 +0530295void fdt_fsl_mc_fixup_iommu_map_entry(void *blob)
296{
297 u32 *prop;
Laurentiu Tudor9e352592020-05-04 14:47:11 +0300298 u32 iommu_map[4], phandle;
Nipun Guptad6912642018-08-20 16:01:14 +0530299 int offset;
300 int lenp;
301
302 /* find fsl-mc node */
303 offset = fdt_path_offset(blob, "/soc/fsl-mc");
304 if (offset < 0)
305 offset = fdt_path_offset(blob, "/fsl-mc");
306 if (offset < 0) {
307 printf("%s: fsl-mc: ERR: fsl-mc node not found in DT, err %d\n",
308 __func__, offset);
309 return;
310 }
311
312 prop = fdt_getprop_w(blob, offset, "iommu-map", &lenp);
313 if (!prop) {
314 debug("%s: fsl-mc: ERR: missing iommu-map in fsl-mc bus node\n",
315 __func__);
316 return;
317 }
318
319 iommu_map[0] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_START);
320 iommu_map[1] = *++prop;
321 iommu_map[2] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_START);
322 iommu_map[3] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_END -
323 FSL_DPAA2_STREAM_ID_START + 1);
324
325 fdt_setprop_inplace(blob, offset, "iommu-map",
326 iommu_map, sizeof(iommu_map));
Laurentiu Tudor9e352592020-05-04 14:47:11 +0300327
328 /* get phandle to MSI controller */
329 prop = (u32 *)fdt_getprop(blob, offset, "msi-parent", 0);
330 if (!prop) {
331 debug("\n%s: ERROR: missing msi-parent\n", __func__);
332 return;
333 }
334 phandle = fdt32_to_cpu(*prop);
335
336 /* also set msi-map property */
337 fdt_appendprop_u32(blob, offset, "msi-map", FSL_DPAA2_STREAM_ID_START);
338 fdt_appendprop_u32(blob, offset, "msi-map", phandle);
339 fdt_appendprop_u32(blob, offset, "msi-map", FSL_DPAA2_STREAM_ID_START);
340 fdt_appendprop_u32(blob, offset, "msi-map", FSL_DPAA2_STREAM_ID_END -
341 FSL_DPAA2_STREAM_ID_START + 1);
Nipun Guptad6912642018-08-20 16:01:14 +0530342}
343
Bogdan Purcareata72669142017-05-24 16:40:22 +0000344static int mc_fixup_dpc_mac_addr(void *blob, int dpmac_id,
Ioana Ciornei25f7cf72020-03-18 16:47:38 +0200345 struct udevice *eth_dev)
Bogdan Purcareata72669142017-05-24 16:40:22 +0000346{
347 int nodeoffset = fdt_path_offset(blob, "/board_info/ports"), noff;
348 int err = 0;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000349 char mac_name[10];
Bogdan Purcareata72669142017-05-24 16:40:22 +0000350 const char link_type_mode[] = "MAC_LINK_TYPE_FIXED";
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000351
352 sprintf(mac_name, "mac@%d", dpmac_id);
353
354 /* node not found - create it */
Bogdan Purcareata72669142017-05-24 16:40:22 +0000355 noff = fdt_subnode_offset(blob, nodeoffset, (const char *)mac_name);
356 if (noff < 0) {
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000357 err = fdt_increase_size(blob, 200);
358 if (err) {
Ioana Ciornei4563b9c2023-01-05 17:03:20 +0200359 printf("fdt_increase_size: err=%s\n", fdt_strerror(err));
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000360 return err;
361 }
362
Bogdan Purcareata72669142017-05-24 16:40:22 +0000363 noff = fdt_add_subnode(blob, nodeoffset, mac_name);
364 if (noff < 0) {
365 printf("fdt_add_subnode: err=%s\n",
366 fdt_strerror(err));
367 return err;
368 }
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000369
370 /* add default property of fixed link */
Bogdan Purcareata72669142017-05-24 16:40:22 +0000371 err = fdt_appendprop_string(blob, noff,
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000372 "link_type", link_type_mode);
373 if (err) {
374 printf("fdt_appendprop_string: err=%s\n",
Ioana Ciornei4563b9c2023-01-05 17:03:20 +0200375 fdt_strerror(err));
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000376 return err;
377 }
378 }
379
Bogdan Purcareata72669142017-05-24 16:40:22 +0000380 return mc_fixup_mac_addr(blob, noff, "port_mac_address", eth_dev,
381 MC_FIXUP_DPC);
382}
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000383
Bogdan Purcareata72669142017-05-24 16:40:22 +0000384static int mc_fixup_mac_addrs(void *blob, enum mc_fixup_type type)
385{
386 int i, err = 0, ret = 0;
Ioana Ciornei25f7cf72020-03-18 16:47:38 +0200387#define ETH_NAME_LEN 20
388 struct udevice *eth_dev;
Ioana Ciornei25f7cf72020-03-18 16:47:38 +0200389 char ethname[ETH_NAME_LEN];
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000390
Bogdan Purcareata72669142017-05-24 16:40:22 +0000391 for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++) {
392 /* port not enabled */
Pankaj Bansal7987ab72018-10-10 14:08:33 +0530393 if (wriop_is_enabled_dpmac(i) != 1)
Bogdan Purcareata72669142017-05-24 16:40:22 +0000394 continue;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000395
Pankaj Bansal8a921702018-08-02 16:31:28 +0530396 snprintf(ethname, ETH_NAME_LEN, "DPMAC%d@%s", i,
397 phy_interface_strings[wriop_get_enet_if(i)]);
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000398
Bogdan Purcareata72669142017-05-24 16:40:22 +0000399 eth_dev = eth_get_dev_by_name(ethname);
400 if (eth_dev == NULL)
401 continue;
402
403 switch (type) {
404 case MC_FIXUP_DPL:
405 err = mc_fixup_dpl_mac_addr(blob, i, eth_dev);
406 break;
407 case MC_FIXUP_DPC:
408 err = mc_fixup_dpc_mac_addr(blob, i, eth_dev);
409 break;
410 default:
411 break;
412 }
413
414 if (err)
415 printf("fsl-mc: ERROR fixing mac address for %s\n",
416 ethname);
417 ret |= err;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000418 }
419
Bogdan Purcareata72669142017-05-24 16:40:22 +0000420 return ret;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000421}
422
Stuart Yodera2bdb772015-07-02 11:29:03 +0530423static int mc_fixup_dpc(u64 dpc_addr)
424{
425 void *blob = (void *)dpc_addr;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000426 int nodeoffset, err = 0;
Stuart Yodera2bdb772015-07-02 11:29:03 +0530427
428 /* delete any existing ICID pools */
429 nodeoffset = fdt_path_offset(blob, "/resources/icid_pools");
430 if (fdt_del_node(blob, nodeoffset) < 0)
431 printf("\nfsl-mc: WARNING: could not delete ICID pool\n");
432
433 /* add a new pool */
434 nodeoffset = fdt_path_offset(blob, "/resources");
435 if (nodeoffset < 0) {
436 printf("\nfsl-mc: ERROR: DPC is missing /resources\n");
437 return -EINVAL;
438 }
439 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pools");
440 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pool@0");
441 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
442 "base_icid", FSL_DPAA2_STREAM_ID_START, 1);
443 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
444 "num",
445 FSL_DPAA2_STREAM_ID_END -
446 FSL_DPAA2_STREAM_ID_START + 1, 1);
447
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000448 /* fixup MAC addresses for dpmac ports */
449 nodeoffset = fdt_path_offset(blob, "/board_info/ports");
Razvan Ionut Cirjana3bbc3b2020-04-28 16:09:59 +0300450 if (nodeoffset < 0) {
451 err = fdt_increase_size(blob, 512);
452 if (err) {
453 printf("fdt_increase_size: err=%s\n",
454 fdt_strerror(err));
455 goto out;
456 }
457 nodeoffset = fdt_path_offset(blob, "/board_info");
458 if (nodeoffset < 0)
459 nodeoffset = fdt_add_subnode(blob, 0, "board_info");
460
461 nodeoffset = fdt_add_subnode(blob, nodeoffset, "ports");
462 }
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000463
Bogdan Purcareata72669142017-05-24 16:40:22 +0000464 err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPC);
Ioana Ciocoi Radulescud7697f42019-02-26 15:50:07 +0000465
466out:
Stuart Yodera2bdb772015-07-02 11:29:03 +0530467 flush_dcache_range(dpc_addr, dpc_addr + fdt_totalsize(blob));
468
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000469 return err;
Stuart Yodera2bdb772015-07-02 11:29:03 +0530470}
471
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530472static int load_mc_dpc(u64 mc_ram_addr, size_t mc_ram_size, u64 mc_dpc_addr)
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700473{
474 u64 mc_dpc_offset;
475#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
476 int error;
477 void *dpc_fdt_hdr;
478 int dpc_size;
479#endif
480
Tom Rini6a5dccc2022-11-16 13:10:41 -0500481#ifdef CFG_SYS_LS_MC_DRAM_DPC_OFFSET
482 BUILD_BUG_ON((CFG_SYS_LS_MC_DRAM_DPC_OFFSET & 0x3) != 0 ||
483 CFG_SYS_LS_MC_DRAM_DPC_OFFSET > 0xffffffff);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700484
Tom Rini6a5dccc2022-11-16 13:10:41 -0500485 mc_dpc_offset = CFG_SYS_LS_MC_DRAM_DPC_OFFSET;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700486#else
Tom Rini6a5dccc2022-11-16 13:10:41 -0500487#error "CFG_SYS_LS_MC_DRAM_DPC_OFFSET not defined"
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700488#endif
489
490 /*
491 * Load the MC DPC blob in the MC private DRAM block:
492 */
493#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
494 printf("MC DPC is preloaded to %#llx\n", mc_ram_addr + mc_dpc_offset);
495#else
496 /*
497 * Get address and size of the DPC blob stored in flash:
498 */
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530499 dpc_fdt_hdr = (void *)mc_dpc_addr;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700500
501 error = fdt_check_header(dpc_fdt_hdr);
502 if (error != 0) {
503 /*
504 * Don't return with error here, since the MC firmware can
505 * still boot without a DPC
506 */
J. German Rivera75d7ace2015-07-02 11:28:56 +0530507 printf("\nfsl-mc: WARNING: No DPC image found");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700508 return 0;
509 }
510
511 dpc_size = fdt_totalsize(dpc_fdt_hdr);
Tom Rini6a5dccc2022-11-16 13:10:41 -0500512 if (dpc_size > CFG_SYS_LS_MC_DPC_MAX_LENGTH) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530513 printf("\nfsl-mc: ERROR: Bad DPC image (too large: %d)\n",
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700514 dpc_size);
515 return -EINVAL;
516 }
517
518 mc_copy_image("MC DPC blob",
519 (u64)dpc_fdt_hdr, dpc_size, mc_ram_addr + mc_dpc_offset);
520#endif /* not defined CONFIG_SYS_LS_MC_DPC_IN_DDR */
521
Stuart Yodera2bdb772015-07-02 11:29:03 +0530522 if (mc_fixup_dpc(mc_ram_addr + mc_dpc_offset))
523 return -EINVAL;
524
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700525 dump_ram_words("DPC", (void *)(mc_ram_addr + mc_dpc_offset));
526 return 0;
527}
528
Bogdan Purcareata72669142017-05-24 16:40:22 +0000529static int mc_fixup_dpl(u64 dpl_addr)
530{
531 void *blob = (void *)dpl_addr;
532 u32 ver = fdt_getprop_u32_default(blob, "/", "dpl-version", 0);
533 int err = 0;
534
535 /* The DPL fixup for mac addresses is only relevant
536 * for old-style DPLs
537 */
538 if (ver >= 10)
539 return 0;
540
541 err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPL);
542 flush_dcache_range(dpl_addr, dpl_addr + fdt_totalsize(blob));
543
544 return err;
545}
546
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530547static int load_mc_dpl(u64 mc_ram_addr, size_t mc_ram_size, u64 mc_dpl_addr)
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700548{
549 u64 mc_dpl_offset;
550#ifndef CONFIG_SYS_LS_MC_DPL_IN_DDR
551 int error;
552 void *dpl_fdt_hdr;
553 int dpl_size;
554#endif
555
Tom Rini6a5dccc2022-11-16 13:10:41 -0500556#ifdef CFG_SYS_LS_MC_DRAM_DPL_OFFSET
557 BUILD_BUG_ON((CFG_SYS_LS_MC_DRAM_DPL_OFFSET & 0x3) != 0 ||
558 CFG_SYS_LS_MC_DRAM_DPL_OFFSET > 0xffffffff);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700559
Tom Rini6a5dccc2022-11-16 13:10:41 -0500560 mc_dpl_offset = CFG_SYS_LS_MC_DRAM_DPL_OFFSET;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700561#else
Tom Rini6a5dccc2022-11-16 13:10:41 -0500562#error "CFG_SYS_LS_MC_DRAM_DPL_OFFSET not defined"
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700563#endif
564
565 /*
566 * Load the MC DPL blob in the MC private DRAM block:
567 */
568#ifdef CONFIG_SYS_LS_MC_DPL_IN_DDR
569 printf("MC DPL is preloaded to %#llx\n", mc_ram_addr + mc_dpl_offset);
570#else
571 /*
572 * Get address and size of the DPL blob stored in flash:
573 */
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530574 dpl_fdt_hdr = (void *)mc_dpl_addr;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700575
576 error = fdt_check_header(dpl_fdt_hdr);
577 if (error != 0) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530578 printf("\nfsl-mc: ERROR: Bad DPL image (bad header)\n");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700579 return error;
580 }
581
582 dpl_size = fdt_totalsize(dpl_fdt_hdr);
Tom Rini6a5dccc2022-11-16 13:10:41 -0500583 if (dpl_size > CFG_SYS_LS_MC_DPL_MAX_LENGTH) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530584 printf("\nfsl-mc: ERROR: Bad DPL image (too large: %d)\n",
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700585 dpl_size);
586 return -EINVAL;
587 }
588
589 mc_copy_image("MC DPL blob",
590 (u64)dpl_fdt_hdr, dpl_size, mc_ram_addr + mc_dpl_offset);
591#endif /* not defined CONFIG_SYS_LS_MC_DPL_IN_DDR */
592
Bogdan Purcareata72669142017-05-24 16:40:22 +0000593 if (mc_fixup_dpl(mc_ram_addr + mc_dpl_offset))
594 return -EINVAL;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700595 dump_ram_words("DPL", (void *)(mc_ram_addr + mc_dpl_offset));
596 return 0;
597}
598
599/**
600 * Return the MC boot timeout value in milliseconds
601 */
602static unsigned long get_mc_boot_timeout_ms(void)
603{
Tom Rini6a5dccc2022-11-16 13:10:41 -0500604 unsigned long timeout_ms = CFG_SYS_LS_MC_BOOT_TIMEOUT_MS;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700605
Simon Glass64b723f2017-08-03 12:22:12 -0600606 char *timeout_ms_env_var = env_get(MC_BOOT_TIMEOUT_ENV_VAR);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700607
608 if (timeout_ms_env_var) {
Simon Glassff9b9032021-07-24 09:03:30 -0600609 timeout_ms = dectoul(timeout_ms_env_var, NULL);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700610 if (timeout_ms == 0) {
611 printf("fsl-mc: WARNING: Invalid value for \'"
612 MC_BOOT_TIMEOUT_ENV_VAR
613 "\' environment variable: %lu\n",
614 timeout_ms);
615
Tom Rini6a5dccc2022-11-16 13:10:41 -0500616 timeout_ms = CFG_SYS_LS_MC_BOOT_TIMEOUT_MS;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700617 }
618 }
619
620 return timeout_ms;
621}
622
Tom Rini6a5dccc2022-11-16 13:10:41 -0500623#ifdef CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
York Suncbe8e1c2016-04-04 11:41:26 -0700624
625__weak bool soc_has_aiop(void)
626{
627 return false;
628}
629
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530630static int load_mc_aiop_img(u64 aiop_fw_addr)
J. German Riverac3b505f2015-07-02 11:28:58 +0530631{
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530632 u64 mc_ram_addr = mc_get_dram_addr();
633#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
J. German Riverac3b505f2015-07-02 11:28:58 +0530634 void *aiop_img;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530635#endif
J. German Riverac3b505f2015-07-02 11:28:58 +0530636
York Suncbe8e1c2016-04-04 11:41:26 -0700637 /* Check if AIOP is available */
638 if (!soc_has_aiop())
639 return -ENODEV;
J. German Riverac3b505f2015-07-02 11:28:58 +0530640 /*
641 * Load the MC AIOP image in the MC private DRAM block:
642 */
643
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530644#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
645 printf("MC AIOP is preloaded to %#llx\n", mc_ram_addr +
Tom Rini6a5dccc2022-11-16 13:10:41 -0500646 CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530647#else
648 aiop_img = (void *)aiop_fw_addr;
J. German Riverac3b505f2015-07-02 11:28:58 +0530649 mc_copy_image("MC AIOP image",
Tom Rini6a5dccc2022-11-16 13:10:41 -0500650 (u64)aiop_img, CFG_SYS_LS_MC_AIOP_IMG_MAX_LENGTH,
651 mc_ram_addr + CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530652#endif
653 mc_aiop_applied = 0;
J. German Riverac3b505f2015-07-02 11:28:58 +0530654
655 return 0;
656}
657#endif
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530658
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700659static int wait_for_mc(bool booting_mc, u32 *final_reg_gsr)
660{
661 u32 reg_gsr;
662 u32 mc_fw_boot_status;
663 unsigned long timeout_ms = get_mc_boot_timeout_ms();
664 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
665
666 dmb();
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700667 assert(timeout_ms > 0);
668 for (;;) {
669 udelay(1000); /* throttle polling */
670 reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
671 mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
672 if (mc_fw_boot_status & 0x1)
673 break;
674
675 timeout_ms--;
676 if (timeout_ms == 0)
677 break;
678 }
679
680 if (timeout_ms == 0) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530681 printf("ERROR: timeout\n");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700682
683 /* TODO: Get an error status from an MC CCSR register */
684 return -ETIMEDOUT;
685 }
686
687 if (mc_fw_boot_status != 0x1) {
688 /*
689 * TODO: Identify critical errors from the GSR register's FS
690 * field and for those errors, set error to -ENODEV or other
691 * appropriate errno, so that the status property is set to
692 * failure in the fsl,dprc device tree node.
693 */
J. German Rivera75d7ace2015-07-02 11:28:56 +0530694 printf("WARNING: Firmware returned an error (GSR: %#x)\n",
695 reg_gsr);
696 } else {
697 printf("SUCCESS\n");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700698 }
699
700 *final_reg_gsr = reg_gsr;
701 return 0;
702}
J. German Rivera8ff14b72014-06-23 15:15:55 -0700703
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530704int mc_init(u64 mc_fw_addr, u64 mc_dpc_addr)
J. German Rivera8ff14b72014-06-23 15:15:55 -0700705{
706 int error = 0;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700707 int portal_id = 0;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700708 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530709 u64 mc_ram_addr = mc_get_dram_addr();
J. German Rivera8ff14b72014-06-23 15:15:55 -0700710 u32 reg_gsr;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700711 u32 reg_mcfbalr;
712#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Rivera8ff14b72014-06-23 15:15:55 -0700713 const void *raw_image_addr;
714 size_t raw_image_size = 0;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700715#endif
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700716 u8 mc_ram_num_256mb_blocks;
717 size_t mc_ram_size = mc_get_dram_block_size();
J. German Rivera8ff14b72014-06-23 15:15:55 -0700718
York Sunc55d7312017-03-06 09:02:29 -0800719 mc_ram_num_256mb_blocks = mc_ram_size / MC_RAM_SIZE_ALIGNMENT;
Meenakshi Aggarwal67f195c2019-02-27 14:41:02 +0530720
721 if (mc_ram_num_256mb_blocks >= 0xff) {
York Sunc55d7312017-03-06 09:02:29 -0800722 error = -EINVAL;
723 printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
724 mc_ram_size);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700725 goto out;
York Sunc55d7312017-03-06 09:02:29 -0800726 }
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700727
J. German Rivera8ff14b72014-06-23 15:15:55 -0700728 /*
Meenakshi Aggarwal67f195c2019-02-27 14:41:02 +0530729 * To support 128 MB DDR Size for MC
730 */
731 if (mc_ram_num_256mb_blocks == 0)
732 mc_ram_num_256mb_blocks = 0xFF;
733
734 /*
J. German Rivera8ff14b72014-06-23 15:15:55 -0700735 * Management Complex cores should be held at reset out of POR.
Bin Meng75574052016-02-05 19:30:11 -0800736 * U-Boot should be the first software to touch MC. To be safe,
J. German Rivera8ff14b72014-06-23 15:15:55 -0700737 * we reset all cores again by setting GCR1 to 0. It doesn't do
738 * anything if they are held at reset. After we setup the firmware
739 * we kick off MC by deasserting the reset bit for core 0, and
740 * deasserting the reset bits for Command Portal Managers.
741 * The stop bits are not touched here. They are used to stop the
742 * cores when they are active. Setting stop bits doesn't stop the
743 * cores from fetching instructions when they are released from
744 * reset.
745 */
746 out_le32(&mc_ccsr_regs->reg_gcr1, 0);
747 dmb();
748
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700749#ifdef CONFIG_SYS_LS_MC_FW_IN_DDR
750 printf("MC firmware is preloaded to %#llx\n", mc_ram_addr);
751#else
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530752 error = parse_mc_firmware_fit_image(mc_fw_addr, &raw_image_addr,
753 &raw_image_size);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700754 if (error != 0)
755 goto out;
756 /*
757 * Load the MC FW at the beginning of the MC private DRAM block:
758 */
J. German Rivera43e4ae32015-01-06 13:19:02 -0800759 mc_copy_image("MC Firmware",
760 (u64)raw_image_addr, raw_image_size, mc_ram_addr);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800761#endif
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700762 dump_ram_words("firmware", (void *)mc_ram_addr);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800763
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530764 error = load_mc_dpc(mc_ram_addr, mc_ram_size, mc_dpc_addr);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700765 if (error != 0)
J. German Rivera8ff14b72014-06-23 15:15:55 -0700766 goto out;
J. German Riverac3b505f2015-07-02 11:28:58 +0530767
J. German Rivera8ff14b72014-06-23 15:15:55 -0700768 debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700769 dump_mc_ccsr_regs(mc_ccsr_regs);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700770
771 /*
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700772 * Tell MC what is the address range of the DRAM block assigned to it:
J. German Rivera8ff14b72014-06-23 15:15:55 -0700773 */
Meenakshi Aggarwal67f195c2019-02-27 14:41:02 +0530774 if (mc_ram_num_256mb_blocks < 0xFF) {
775 reg_mcfbalr = (u32)mc_ram_addr |
776 (mc_ram_num_256mb_blocks - 1);
777 } else {
778 reg_mcfbalr = (u32)mc_ram_addr |
779 (mc_ram_num_256mb_blocks);
780 }
781
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700782 out_le32(&mc_ccsr_regs->reg_mcfbalr, reg_mcfbalr);
783 out_le32(&mc_ccsr_regs->reg_mcfbahr,
York Sunc55d7312017-03-06 09:02:29 -0800784 (u32)(mc_ram_addr >> 32));
Stuart Yoderd1970352015-07-02 11:29:02 +0530785 out_le32(&mc_ccsr_regs->reg_mcfapr, FSL_BYPASS_AMQ);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700786
787 /*
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700788 * Tell the MC that we want delayed DPL deployment.
J. German Rivera8ff14b72014-06-23 15:15:55 -0700789 */
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700790 out_le32(&mc_ccsr_regs->reg_gsr, 0xDD00);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700791
J. German Rivera75d7ace2015-07-02 11:28:56 +0530792 printf("\nfsl-mc: Booting Management Complex ... ");
J. German Rivera43e4ae32015-01-06 13:19:02 -0800793
J. German Rivera8ff14b72014-06-23 15:15:55 -0700794 /*
795 * Deassert reset and release MC core 0 to run
796 */
797 out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700798 error = wait_for_mc(true, &reg_gsr);
799 if (error != 0)
J. German Rivera8ff14b72014-06-23 15:15:55 -0700800 goto out;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700801
J. German Rivera43e4ae32015-01-06 13:19:02 -0800802 /*
803 * TODO: need to obtain the portal_id for the root container from the
804 * DPL
805 */
806 portal_id = 0;
807
808 /*
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700809 * Initialize the global default MC portal
810 * And check that the MC firmware is responding portal commands:
J. German Rivera43e4ae32015-01-06 13:19:02 -0800811 */
Ioana Ciorneie992c4d2023-01-05 17:03:19 +0200812 root_mc_io = calloc(sizeof(struct fsl_mc_io), 1);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530813 if (!root_mc_io) {
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +0530814 printf(" No memory: calloc() failed\n");
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700815 return -ENOMEM;
816 }
817
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530818 root_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(portal_id);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800819 debug("Checking access to MC portal of root DPRC container (portal_id %d, portal physical addr %p)\n",
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530820 portal_id, root_mc_io->mmio_regs);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800821
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530822 error = mc_get_version(root_mc_io, MC_CMD_NO_FLAGS, &mc_ver_info);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800823 if (error != 0) {
824 printf("fsl-mc: ERROR: Firmware version check failed (error: %d)\n",
825 error);
826 goto out;
827 }
828
J. German Rivera43e4ae32015-01-06 13:19:02 -0800829 printf("fsl-mc: Management Complex booted (version: %d.%d.%d, boot status: %#x)\n",
830 mc_ver_info.major, mc_ver_info.minor, mc_ver_info.revision,
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700831 reg_gsr & GSR_FS_MASK);
832
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530833out:
834 if (error != 0)
835 mc_boot_status = error;
836 else
837 mc_boot_status = 0;
838
839 return error;
840}
841
842int mc_apply_dpl(u64 mc_dpl_addr)
843{
844 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
845 int error = 0;
846 u32 reg_gsr;
847 u64 mc_ram_addr = mc_get_dram_addr();
848 size_t mc_ram_size = mc_get_dram_block_size();
849
Alexander Graf2ebeb442016-11-17 01:02:57 +0100850 if (!mc_dpl_addr)
851 return -1;
852
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530853 error = load_mc_dpl(mc_ram_addr, mc_ram_size, mc_dpl_addr);
854 if (error != 0)
855 return error;
856
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700857 /*
858 * Tell the MC to deploy the DPL:
859 */
860 out_le32(&mc_ccsr_regs->reg_gsr, 0x0);
J. German Rivera75d7ace2015-07-02 11:28:56 +0530861 printf("fsl-mc: Deploying data path layout ... ");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700862 error = wait_for_mc(false, &reg_gsr);
J. German Rivera75d7ace2015-07-02 11:28:56 +0530863
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530864 if (!error)
865 mc_dpl_applied = 0;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700866
867 return error;
868}
869
870int get_mc_boot_status(void)
871{
872 return mc_boot_status;
873}
874
Tom Rini6a5dccc2022-11-16 13:10:41 -0500875#ifdef CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530876int get_aiop_apply_status(void)
877{
878 return mc_aiop_applied;
879}
880#endif
881
882int get_dpl_apply_status(void)
883{
884 return mc_dpl_applied;
885}
886
Mian Yousaf Kaukab97124652018-12-18 14:01:17 +0100887int is_lazy_dpl_addr_valid(void)
888{
889 return !!mc_lazy_dpl_addr;
890}
891
Priyanka Jaine0e0be52017-08-24 16:42:43 +0530892/*
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530893 * Return the MC address of private DRAM block.
Priyanka Jaine0e0be52017-08-24 16:42:43 +0530894 * As per MC design document, MC initial base address
895 * should be least significant 512MB address of MC private
896 * memory, i.e. address should point to end address masked
897 * with 512MB offset in private DRAM block.
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530898 */
899u64 mc_get_dram_addr(void)
900{
Priyanka Jaine0e0be52017-08-24 16:42:43 +0530901 size_t mc_ram_size = mc_get_dram_block_size();
902
Prabhakar Kushwaha145e4cd2018-08-27 12:58:52 +0530903 if (!mc_memset_resv_ram || (get_mc_boot_status() < 0)) {
904 mc_memset_resv_ram = 1;
905 memset((void *)gd->arch.resv_ram, 0, mc_ram_size);
906 }
907
Priyanka Jaine0e0be52017-08-24 16:42:43 +0530908 return (gd->arch.resv_ram + mc_ram_size - 1) &
909 MC_RAM_BASE_ADDR_ALIGNMENT_MASK;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530910}
911
912/**
J. German Rivera8ff14b72014-06-23 15:15:55 -0700913 * Return the actual size of the MC private DRAM block.
J. German Rivera8ff14b72014-06-23 15:15:55 -0700914 */
915unsigned long mc_get_dram_block_size(void)
916{
Tom Rini6a5dccc2022-11-16 13:10:41 -0500917 unsigned long dram_block_size = CFG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700918
Simon Glass64b723f2017-08-03 12:22:12 -0600919 char *dram_block_size_env_var = env_get(MC_MEM_SIZE_ENV_VAR);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700920
921 if (dram_block_size_env_var) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600922 dram_block_size = hextoul(dram_block_size_env_var, NULL);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700923
Tom Rini6a5dccc2022-11-16 13:10:41 -0500924 if (dram_block_size < CFG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE) {
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700925 printf("fsl-mc: WARNING: Invalid value for \'"
926 MC_MEM_SIZE_ENV_VAR
927 "\' environment variable: %lu\n",
928 dram_block_size);
929
Meenakshi Aggarwal67f195c2019-02-27 14:41:02 +0530930 dram_block_size = MC_DRAM_BLOCK_DEFAULT_SIZE;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700931 }
932 }
933
934 return dram_block_size;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700935}
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700936
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900937int fsl_mc_ldpaa_init(struct bd_info *bis)
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530938{
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530939 int i;
940
941 for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++)
Pankaj Bansal7987ab72018-10-10 14:08:33 +0530942 if (wriop_is_enabled_dpmac(i) == 1)
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530943 ldpaa_eth_init(i, wriop_get_enet_if(i));
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530944 return 0;
945}
946
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530947static int dprc_version_check(struct fsl_mc_io *mc_io, uint16_t handle)
948{
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530949 int error;
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530950 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530951
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530952 error = dprc_get_api_version(mc_io, 0,
953 &major_ver,
954 &minor_ver);
955 if (error < 0) {
956 printf("dprc_get_api_version() failed: %d\n", error);
957 return error;
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530958 }
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530959
960 if (major_ver < DPRC_VER_MAJOR || (major_ver == DPRC_VER_MAJOR &&
961 minor_ver < DPRC_VER_MINOR)) {
962 printf("DPRC version mismatch found %u.%u,",
963 major_ver, minor_ver);
964 printf("supported version is %u.%u\n",
965 DPRC_VER_MAJOR, DPRC_VER_MINOR);
966 }
967
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530968 return error;
969}
970
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530971static int dpio_init(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700972{
973 struct qbman_swp_desc p_des;
974 struct dpio_attr attr;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530975 struct dpio_cfg dpio_cfg;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700976 int err = 0;
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530977 uint16_t major_ver, minor_ver;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700978
Ioana Ciorneie992c4d2023-01-05 17:03:19 +0200979 dflt_dpio = calloc(sizeof(struct fsl_dpio_obj), 1);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700980 if (!dflt_dpio) {
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +0530981 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530982 err = -ENOMEM;
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +0530983 goto err_calloc;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700984 }
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530985 dpio_cfg.channel_mode = DPIO_LOCAL_CHANNEL;
986 dpio_cfg.num_priorities = 8;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700987
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530988 err = dpio_create(dflt_mc_io,
989 dflt_dprc_handle,
990 MC_CMD_NO_FLAGS,
991 &dpio_cfg,
992 &dflt_dpio->dpio_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530993 if (err < 0) {
994 printf("dpio_create() failed: %d\n", err);
995 err = -ENODEV;
996 goto err_create;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700997 }
998
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530999 err = dpio_get_api_version(dflt_mc_io, 0,
1000 &major_ver,
1001 &minor_ver);
1002 if (err < 0) {
1003 printf("dpio_get_api_version() failed: %d\n", err);
1004 goto err_get_api_ver;
1005 }
1006
1007 if (major_ver < DPIO_VER_MAJOR || (major_ver == DPIO_VER_MAJOR &&
1008 minor_ver < DPIO_VER_MINOR)) {
1009 printf("DPRC version mismatch found %u.%u,",
1010 major_ver,
1011 minor_ver);
1012 }
1013
1014 err = dpio_open(dflt_mc_io,
1015 MC_CMD_NO_FLAGS,
1016 dflt_dpio->dpio_id,
1017 &dflt_dpio->dpio_handle);
1018 if (err) {
1019 printf("dpio_open() failed\n");
1020 goto err_open;
1021 }
1022
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301023 memset(&attr, 0, sizeof(struct dpio_attr));
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +05301024 err = dpio_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301025 dflt_dpio->dpio_handle, &attr);
1026 if (err < 0) {
1027 printf("dpio_get_attributes() failed: %d\n", err);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001028 goto err_get_attr;
1029 }
1030
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301031 if (dflt_dpio->dpio_id != attr.id) {
1032 printf("dnpi object id and attribute id are not same\n");
1033 goto err_attr_not_same;
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301034 }
1035
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301036#ifdef DEBUG
Ioana Ciorneib1ba07a2023-01-05 17:03:17 +02001037 printf("Init: DPIO.%d\n", dflt_dpio->dpio_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301038#endif
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301039 err = dpio_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1040 if (err < 0) {
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001041 printf("dpio_enable() failed %d\n", err);
1042 goto err_get_enable;
1043 }
Prabhakar Kushwahafd5d1272015-07-02 11:28:59 +05301044 debug("ce_offset=0x%llx, ci_offset=0x%llx, portalid=%d, prios=%d\n",
1045 attr.qbman_portal_ce_offset,
1046 attr.qbman_portal_ci_offset,
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001047 attr.qbman_portal_id,
1048 attr.num_priorities);
1049
Prabhakar Kushwahafd5d1272015-07-02 11:28:59 +05301050 p_des.cena_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
1051 + attr.qbman_portal_ce_offset);
1052 p_des.cinh_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
1053 + attr.qbman_portal_ci_offset);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001054
1055 dflt_dpio->sw_portal = qbman_swp_init(&p_des);
1056 if (dflt_dpio->sw_portal == NULL) {
1057 printf("qbman_swp_init() failed\n");
1058 goto err_get_swp_init;
1059 }
1060 return 0;
1061
1062err_get_swp_init:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301063 dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001064err_get_enable:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301065err_get_attr:
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301066err_attr_not_same:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301067 dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301068err_open:
1069err_get_api_ver:
1070 dpio_destroy(dflt_mc_io,
1071 dflt_dprc_handle,
1072 MC_CMD_NO_FLAGS,
1073 dflt_dpio->dpio_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301074err_create:
Prabhakar Kushwahac6018bd2016-03-18 16:15:29 +05301075 free(dflt_dpio);
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301076err_calloc:
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001077 return err;
1078}
1079
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301080static int dpio_exit(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001081{
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301082 int err;
1083
1084 err = dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1085 if (err < 0) {
1086 printf("dpio_disable() failed: %d\n", err);
1087 goto err;
1088 }
1089
Cosmin-Florin Aluchenesei971dfd32021-07-21 19:13:33 +03001090 err = dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301091 if (err < 0) {
1092 printf("dpio_close() failed: %d\n", err);
1093 goto err;
1094 }
1095
1096 err = dpio_destroy(dflt_mc_io,
1097 dflt_dprc_handle,
1098 MC_CMD_NO_FLAGS,
1099 dflt_dpio->dpio_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301100 if (err < 0) {
1101 printf("dpio_destroy() failed: %d\n", err);
1102 goto err;
1103 }
1104
1105#ifdef DEBUG
Ioana Ciorneib1ba07a2023-01-05 17:03:17 +02001106 printf("Exit: DPIO.%d\n", dflt_dpio->dpio_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301107#endif
1108
1109 if (dflt_dpio)
1110 free(dflt_dpio);
1111
1112 return 0;
1113err:
1114 return err;
1115}
1116
1117static int dprc_init(void)
1118{
1119 int err, child_portal_id, container_id;
1120 struct dprc_cfg cfg;
1121 uint64_t mc_portal_offset;
1122
1123 /* Open root container */
1124 err = dprc_get_container_id(root_mc_io, MC_CMD_NO_FLAGS, &container_id);
1125 if (err < 0) {
1126 printf("dprc_get_container_id(): Root failed: %d\n", err);
1127 goto err_root_container_id;
1128 }
1129
1130#ifdef DEBUG
1131 printf("Root container id = %d\n", container_id);
1132#endif
1133 err = dprc_open(root_mc_io, MC_CMD_NO_FLAGS, container_id,
1134 &root_dprc_handle);
1135 if (err < 0) {
1136 printf("dprc_open(): Root Container failed: %d\n", err);
1137 goto err_root_open;
1138 }
1139
1140 if (!root_dprc_handle) {
1141 printf("dprc_open(): Root Container Handle is not valid\n");
1142 goto err_root_open;
1143 }
1144
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301145 err = dprc_version_check(root_mc_io, root_dprc_handle);
1146 if (err < 0) {
1147 printf("dprc_version_check() failed: %d\n", err);
1148 goto err_root_open;
1149 }
1150
Prabhakar Kushwaha901a88e2016-01-20 12:04:19 +05301151 memset(&cfg, 0, sizeof(struct dprc_cfg));
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301152 cfg.options = DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED |
1153 DPRC_CFG_OPT_OBJ_CREATE_ALLOWED |
1154 DPRC_CFG_OPT_ALLOC_ALLOWED;
1155 cfg.icid = DPRC_GET_ICID_FROM_POOL;
Prabhakar Kushwaha7fd59d92015-12-24 15:33:49 +05301156 cfg.portal_id = DPRC_GET_PORTAL_ID_FROM_POOL;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301157 err = dprc_create_container(root_mc_io, MC_CMD_NO_FLAGS,
Ioana Ciornei4563b9c2023-01-05 17:03:20 +02001158 root_dprc_handle, &cfg,
1159 &child_dprc_id,
1160 &mc_portal_offset);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301161 if (err < 0) {
1162 printf("dprc_create_container() failed: %d\n", err);
1163 goto err_create;
1164 }
1165
Ioana Ciorneie992c4d2023-01-05 17:03:19 +02001166 dflt_mc_io = calloc(sizeof(struct fsl_mc_io), 1);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301167 if (!dflt_mc_io) {
1168 err = -ENOMEM;
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301169 printf(" No memory: calloc() failed\n");
1170 goto err_calloc;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301171 }
1172
1173 child_portal_id = MC_PORTAL_OFFSET_TO_PORTAL_ID(mc_portal_offset);
1174 dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(child_portal_id);
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301175
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301176#ifdef DEBUG
1177 printf("MC portal of child DPRC container: %d, physical addr %p)\n",
1178 child_dprc_id, dflt_mc_io->mmio_regs);
1179#endif
1180
1181 err = dprc_open(dflt_mc_io, MC_CMD_NO_FLAGS, child_dprc_id,
1182 &dflt_dprc_handle);
1183 if (err < 0) {
1184 printf("dprc_open(): Child container failed: %d\n", err);
1185 goto err_child_open;
1186 }
1187
1188 if (!dflt_dprc_handle) {
1189 printf("dprc_open(): Child container Handle is not valid\n");
1190 goto err_child_open;
1191 }
1192
1193 return 0;
1194err_child_open:
1195 free(dflt_mc_io);
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301196err_calloc:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301197 dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1198 root_dprc_handle, child_dprc_id);
1199err_create:
1200 dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1201err_root_open:
1202err_root_container_id:
1203 return err;
1204}
1205
1206static int dprc_exit(void)
1207{
1208 int err;
1209
1210 err = dprc_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dprc_handle);
1211 if (err < 0) {
1212 printf("dprc_close(): Child failed: %d\n", err);
1213 goto err;
1214 }
1215
1216 err = dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1217 root_dprc_handle, child_dprc_id);
1218 if (err < 0) {
1219 printf("dprc_destroy_container() failed: %d\n", err);
1220 goto err;
1221 }
1222
1223 err = dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1224 if (err < 0) {
1225 printf("dprc_close(): Root failed: %d\n", err);
1226 goto err;
1227 }
1228
1229 if (dflt_mc_io)
1230 free(dflt_mc_io);
1231
1232 if (root_mc_io)
1233 free(root_mc_io);
1234
1235 return 0;
1236
1237err:
1238 return err;
1239}
1240
1241static int dpbp_init(void)
1242{
1243 int err;
1244 struct dpbp_attr dpbp_attr;
1245 struct dpbp_cfg dpbp_cfg;
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301246 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301247
Ioana Ciorneie992c4d2023-01-05 17:03:19 +02001248 dflt_dpbp = calloc(sizeof(struct fsl_dpbp_obj), 1);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001249 if (!dflt_dpbp) {
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301250 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301251 err = -ENOMEM;
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301252 goto err_calloc;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301253 }
1254
1255 dpbp_cfg.options = 512;
1256
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301257 err = dpbp_create(dflt_mc_io,
1258 dflt_dprc_handle,
1259 MC_CMD_NO_FLAGS,
1260 &dpbp_cfg,
1261 &dflt_dpbp->dpbp_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301262
1263 if (err < 0) {
1264 err = -ENODEV;
1265 printf("dpbp_create() failed: %d\n", err);
1266 goto err_create;
1267 }
1268
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301269 err = dpbp_get_api_version(dflt_mc_io, 0,
1270 &major_ver,
1271 &minor_ver);
1272 if (err < 0) {
1273 printf("dpbp_get_api_version() failed: %d\n", err);
1274 goto err_get_api_ver;
1275 }
1276
1277 if (major_ver < DPBP_VER_MAJOR || (major_ver == DPBP_VER_MAJOR &&
1278 minor_ver < DPBP_VER_MINOR)) {
1279 printf("DPBP version mismatch found %u.%u,",
1280 major_ver, minor_ver);
1281 printf("supported version is %u.%u\n",
1282 DPBP_VER_MAJOR, DPBP_VER_MINOR);
1283 }
1284
1285 err = dpbp_open(dflt_mc_io,
1286 MC_CMD_NO_FLAGS,
1287 dflt_dpbp->dpbp_id,
1288 &dflt_dpbp->dpbp_handle);
1289 if (err) {
1290 printf("dpbp_open() failed\n");
1291 goto err_open;
1292 }
1293
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301294 memset(&dpbp_attr, 0, sizeof(struct dpbp_attr));
1295 err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
1296 dflt_dpbp->dpbp_handle,
1297 &dpbp_attr);
1298 if (err < 0) {
1299 printf("dpbp_get_attributes() failed: %d\n", err);
1300 goto err_get_attr;
1301 }
1302
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301303 if (dflt_dpbp->dpbp_id != dpbp_attr.id) {
1304 printf("dpbp object id and attribute id are not same\n");
1305 goto err_attr_not_same;
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301306 }
1307
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301308#ifdef DEBUG
Ioana Ciorneib1ba07a2023-01-05 17:03:17 +02001309 printf("Init: DPBP.%d\n", dflt_dpbp->dpbp_attr.id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301310#endif
1311
1312 err = dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
1313 if (err < 0) {
1314 printf("dpbp_close() failed: %d\n", err);
1315 goto err_close;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001316 }
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001317
1318 return 0;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301319
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301320err_get_attr:
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301321err_attr_not_same:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301322 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301323 dpbp_destroy(dflt_mc_io,
1324 dflt_dprc_handle,
1325 MC_CMD_NO_FLAGS,
1326 dflt_dpbp->dpbp_id);
1327err_get_api_ver:
1328err_close:
1329err_open:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301330err_create:
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301331 free(dflt_dpbp);
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301332err_calloc:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301333 return err;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001334}
1335
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301336static int dpbp_exit(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001337{
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301338 int err;
1339
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301340 err = dpbp_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1341 dflt_dpbp->dpbp_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301342 if (err < 0) {
1343 printf("dpbp_destroy() failed: %d\n", err);
1344 goto err;
1345 }
1346
1347#ifdef DEBUG
Ioana Ciorneib1ba07a2023-01-05 17:03:17 +02001348 printf("Exit: DPBP.%d\n", dflt_dpbp->dpbp_attr.id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301349#endif
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001350
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301351 if (dflt_dpbp)
1352 free(dflt_dpbp);
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301353 return 0;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301354
1355err:
1356 return err;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001357}
1358
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301359static int dpni_init(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001360{
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301361 int err;
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301362 uint8_t cfg_buf[256] = {0};
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301363 struct dpni_cfg dpni_cfg;
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301364 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301365
Ioana Ciorneie992c4d2023-01-05 17:03:19 +02001366 dflt_dpni = calloc(sizeof(struct fsl_dpni_obj), 1);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301367 if (!dflt_dpni) {
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301368 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301369 err = -ENOMEM;
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301370 goto err_calloc;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301371 }
1372
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301373 memset(&dpni_cfg, 0, sizeof(dpni_cfg));
1374 err = dpni_prepare_cfg(&dpni_cfg, &cfg_buf[0]);
Prabhakar Kushwahae26e2852015-12-24 15:33:01 +05301375 if (err < 0) {
1376 err = -ENODEV;
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301377 printf("dpni_prepare_cfg() failed: %d\n", err);
1378 goto err_prepare_cfg;
Prabhakar Kushwahae26e2852015-12-24 15:33:01 +05301379 }
1380
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301381 err = dpni_create(dflt_mc_io,
1382 dflt_dprc_handle,
1383 MC_CMD_NO_FLAGS,
1384 &dpni_cfg,
1385 &dflt_dpni->dpni_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301386 if (err < 0) {
1387 err = -ENODEV;
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301388 printf("dpni create() failed: %d\n", err);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301389 goto err_create;
1390 }
1391
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301392 err = dpni_get_api_version(dflt_mc_io, 0,
1393 &major_ver,
1394 &minor_ver);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301395 if (err < 0) {
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301396 printf("dpni_get_api_version() failed: %d\n", err);
1397 goto err_get_version;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301398 }
1399
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301400 if (major_ver < DPNI_VER_MAJOR || (major_ver == DPNI_VER_MAJOR &&
1401 minor_ver < DPNI_VER_MINOR)) {
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301402 printf("DPNI version mismatch found %u.%u,",
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301403 major_ver, minor_ver);
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301404 printf("supported version is %u.%u\n",
1405 DPNI_VER_MAJOR, DPNI_VER_MINOR);
1406 }
1407
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301408 err = dpni_open(dflt_mc_io,
1409 MC_CMD_NO_FLAGS,
1410 dflt_dpni->dpni_id,
1411 &dflt_dpni->dpni_handle);
1412 if (err) {
1413 printf("dpni_open() failed\n");
1414 goto err_open;
1415 }
1416
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301417#ifdef DEBUG
Ioana Ciorneib1ba07a2023-01-05 17:03:17 +02001418 printf("Init: DPNI.%d\n", dflt_dpni->dpni_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301419#endif
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301420 err = dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
1421 if (err < 0) {
1422 printf("dpni_close() failed: %d\n", err);
1423 goto err_close;
1424 }
1425
1426 return 0;
1427
1428err_close:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301429 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301430err_open:
1431err_get_version:
1432 dpni_destroy(dflt_mc_io,
1433 dflt_dprc_handle,
1434 MC_CMD_NO_FLAGS,
1435 dflt_dpni->dpni_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301436err_create:
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301437err_prepare_cfg:
Prabhakar Kushwahae26e2852015-12-24 15:33:01 +05301438 free(dflt_dpni);
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301439err_calloc:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301440 return err;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001441}
1442
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301443static int dpni_exit(void)
1444{
1445 int err;
1446
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301447 err = dpni_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1448 dflt_dpni->dpni_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301449 if (err < 0) {
1450 printf("dpni_destroy() failed: %d\n", err);
1451 goto err;
1452 }
1453
1454#ifdef DEBUG
Ioana Ciorneib1ba07a2023-01-05 17:03:17 +02001455 printf("Exit: DPNI.%d\n", dflt_dpni->dpni_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301456#endif
1457
1458 if (dflt_dpni)
1459 free(dflt_dpni);
1460 return 0;
1461
1462err:
1463 return err;
1464}
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001465
1466static bool is_dpsparser_supported(void)
1467{
1468 /* dpsparser support was first introduced in MC version: 10.12.0 */
1469 if (mc_ver_info.major < 10)
1470 return false;
1471 if (mc_ver_info.major == 10)
1472 return (mc_ver_info.minor >= 12);
1473 return true;
1474}
1475
1476static int dpsparser_version_check(struct fsl_mc_io *mc_io)
1477{
1478 int error;
1479 u16 major_ver, minor_ver;
1480
1481 if (!is_dpsparser_supported())
1482 return 0;
1483
1484 error = dpsparser_get_api_version(mc_io, 0,
1485 &major_ver,
1486 &minor_ver);
1487 if (error < 0) {
1488 printf("dpsparser_get_api_version() failed: %d\n", error);
1489 return error;
1490 }
1491
1492 if (major_ver < DPSPARSER_VER_MAJOR || (major_ver ==
1493 DPSPARSER_VER_MAJOR && minor_ver < DPSPARSER_VER_MINOR)) {
1494 printf("DPSPARSER version mismatch found %u.%u,",
1495 major_ver, minor_ver);
1496 printf("supported version is %u.%u\n",
1497 DPSPARSER_VER_MAJOR, DPSPARSER_VER_MINOR);
1498 }
1499
1500 return error;
1501}
1502
1503static int dpsparser_init(void)
1504{
1505 int err = 0;
1506
1507 if (!is_dpsparser_supported())
1508 return 0;
1509
1510 err = dpsparser_create(dflt_mc_io,
1511 dflt_dprc_handle,
1512 MC_CMD_NO_FLAGS,
1513 &dpsparser_obj_id);
1514 if (err)
1515 printf("dpsparser_create() failed\n");
1516
1517 err = dpsparser_version_check(dflt_mc_io);
1518 if (err < 0) {
1519 printf("dpsparser_version_check() failed: %d\n", err);
1520 goto err_version_check;
1521 }
1522
1523 err = dpsparser_open(dflt_mc_io,
1524 MC_CMD_NO_FLAGS,
1525 &dpsparser_handle);
1526 if (err < 0) {
1527 printf("dpsparser_open() failed: %d\n", err);
1528 goto err_open;
1529 }
1530
1531 return err;
1532
1533err_open:
1534err_version_check:
1535 dpsparser_destroy(dflt_mc_io,
1536 dflt_dprc_handle,
1537 MC_CMD_NO_FLAGS, dpsparser_obj_id);
1538
1539 return err;
1540}
1541
1542#ifdef DPSPARSER_DESTROY
1543/* TODO: refactoring needed in the future to allow DPSPARSER object destroy
1544 * Workaround: DO NOT destroy DPSPARSER object because it needs to be available
1545 * on Apply DPL
1546 */
1547static int dpsparser_exit(void)
1548{
1549 int err;
1550
1551 if (!is_dpsparser_supported())
1552 return 0;
1553
1554 dpsparser_close(dflt_mc_io, MC_CMD_NO_FLAGS, dpsparser_handle);
1555 if (err < 0) {
1556 printf("dpsparser_close() failed: %d\n", err);
1557 goto err;
1558 }
1559
1560 err = dpsparser_destroy(dflt_mc_io, dflt_dprc_handle,
1561 MC_CMD_NO_FLAGS, dpsparser_obj_id);
1562 if (err < 0) {
1563 printf("dpsparser_destroy() failed: %d\n", err);
1564 goto err;
1565 }
1566 return 0;
1567
1568err:
1569 return err;
1570}
1571#endif
1572
1573int mc_apply_spb(u64 mc_spb_addr)
1574{
1575 int err = 0;
1576 u16 error, err_arr_size;
1577 u64 mc_spb_offset;
1578 u32 spb_size;
1579 struct sp_blob_header *sp_blob;
1580 u64 mc_ram_addr = mc_get_dram_addr();
1581
1582 if (!is_dpsparser_supported())
1583 return 0;
1584
1585 if (!mc_spb_addr) {
1586 printf("fsl-mc: Invalid Blob address\n");
1587 return -1;
1588 }
1589
1590#ifdef CONFIG_MC_DRAM_SPB_OFFSET
1591 mc_spb_offset = CONFIG_MC_DRAM_SPB_OFFSET;
1592#else
1593#error "CONFIG_MC_DRAM_SPB_OFFSET not defined"
1594#endif
1595
1596 // Read blob header and get size of SPB blob
1597 sp_blob = (struct sp_blob_header *)mc_spb_addr;
1598 spb_size = le32_to_cpu(sp_blob->length);
1599 if (spb_size > CONFIG_MC_SPB_MAX_SIZE) {
1600 printf("\nfsl-mc: ERROR: Bad SPB image (too large: %d)\n",
1601 spb_size);
1602 return -EINVAL;
1603 }
1604
1605 mc_copy_image("MC SP Blob", mc_spb_addr, spb_size,
1606 mc_ram_addr + mc_spb_offset);
1607
1608 //Invoke MC command to apply SPB blob
1609 printf("fsl-mc: Applying soft parser blob... ");
1610 err = dpsparser_apply_spb(dflt_mc_io, MC_CMD_NO_FLAGS, dpsparser_handle,
1611 mc_spb_offset, &error);
1612 if (err)
1613 return err;
1614
1615 if (error == 0) {
1616 printf("SUCCESS\n");
1617 } else {
1618 printf("FAILED with error code = %d:\n", error);
1619 err_arr_size = (u16)ARRAY_SIZE(mc_err_msg_apply_spb);
1620
1621 if (error > 0 && error < err_arr_size)
1622 printf(mc_err_msg_apply_spb[error]);
1623 else
1624 printf(MC_ERROR_MSG_SPB_UNKNOWN);
1625 }
1626
1627 return err;
1628}
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301629
1630static int mc_init_object(void)
1631{
1632 int err = 0;
1633
1634 err = dprc_init();
1635 if (err < 0) {
1636 printf("dprc_init() failed: %d\n", err);
1637 goto err;
1638 }
1639
1640 err = dpbp_init();
1641 if (err < 0) {
1642 printf("dpbp_init() failed: %d\n", err);
1643 goto err;
1644 }
1645
1646 err = dpio_init();
1647 if (err < 0) {
1648 printf("dpio_init() failed: %d\n", err);
1649 goto err;
1650 }
1651
1652 err = dpni_init();
1653 if (err < 0) {
1654 printf("dpni_init() failed: %d\n", err);
1655 goto err;
1656 }
1657
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001658 err = dpsparser_init();
1659 if (err < 0) {
1660 printf("dpsparser_init() failed: %d\n", err);
1661 goto err;
1662 }
1663
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301664 return 0;
1665err:
1666 return err;
1667}
1668
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001669int fsl_mc_ldpaa_exit(struct bd_info *bd)
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301670{
1671 int err = 0;
Yogesh Gaur4a81bbe2017-04-27 10:14:16 +05301672 bool is_dpl_apply_status = false;
Santan Kumar0a6e5ba2017-06-29 11:19:34 +05301673 bool mc_boot_status = false;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301674
Alexander Graf2ebeb442016-11-17 01:02:57 +01001675 if (bd && mc_lazy_dpl_addr && !fsl_mc_ldpaa_exit(NULL)) {
Yogesh Gaur8dadfb42018-01-16 10:08:24 +05301676 err = mc_apply_dpl(mc_lazy_dpl_addr);
1677 if (!err)
1678 fdt_fixup_board_enet(working_fdt);
Alexander Graf2ebeb442016-11-17 01:02:57 +01001679 mc_lazy_dpl_addr = 0;
1680 }
1681
Santan Kumar0a6e5ba2017-06-29 11:19:34 +05301682 if (!get_mc_boot_status())
1683 mc_boot_status = true;
1684
Prabhakar Kushwaha878d3ec2016-03-21 14:19:39 +05301685 /* MC is not loaded intentionally, So return success. */
Santan Kumar0a6e5ba2017-06-29 11:19:34 +05301686 if (bd && !mc_boot_status)
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301687 return 0;
1688
Yogesh Gaur4a81bbe2017-04-27 10:14:16 +05301689 /* If DPL is deployed, set is_dpl_apply_status as TRUE. */
1690 if (!get_dpl_apply_status())
1691 is_dpl_apply_status = true;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301692
Yogesh Gaur4a81bbe2017-04-27 10:14:16 +05301693 /*
1694 * For case MC is loaded but DPL is not deployed, return success and
1695 * print message on console. Else FDT fix-up code execution hanged.
1696 */
Santan Kumar0a6e5ba2017-06-29 11:19:34 +05301697 if (bd && mc_boot_status && !is_dpl_apply_status) {
Yogesh Gaur4a81bbe2017-04-27 10:14:16 +05301698 printf("fsl-mc: DPL not deployed, DPAA2 ethernet not work\n");
Yogesh Gaur82709e52017-11-28 10:11:14 +05301699 goto mc_obj_cleanup;
Yogesh Gaur4a81bbe2017-04-27 10:14:16 +05301700 }
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301701
Santan Kumar0a6e5ba2017-06-29 11:19:34 +05301702 if (bd && mc_boot_status && is_dpl_apply_status)
1703 return 0;
1704
Yogesh Gaur82709e52017-11-28 10:11:14 +05301705mc_obj_cleanup:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301706 err = dpbp_exit();
1707 if (err < 0) {
Prabhakar Kushwaha88ba4d62016-01-20 12:04:37 +05301708 printf("dpbp_exit() failed: %d\n", err);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301709 goto err;
1710 }
1711
1712 err = dpio_exit();
1713 if (err < 0) {
1714 printf("dpio_exit() failed: %d\n", err);
1715 goto err;
1716 }
1717
1718 err = dpni_exit();
1719 if (err < 0) {
1720 printf("dpni_exit() failed: %d\n", err);
1721 goto err;
1722 }
1723
1724 err = dprc_exit();
1725 if (err < 0) {
1726 printf("dprc_exit() failed: %d\n", err);
1727 goto err;
1728 }
1729
1730 return 0;
1731err:
1732 return err;
1733}
1734
Cosmin-Florin Alucheneseiaa280142021-10-07 13:07:44 +03001735static void print_k_bytes(const void *buf, ssize_t *size)
1736{
1737 while (*size > 0) {
1738 int count = printf("%s", (char *)buf);
1739
1740 buf += count;
1741 *size -= count;
1742 }
1743}
1744
1745static void mc_dump_log(void)
1746{
1747 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
1748 u64 high = in_le64(&mc_ccsr_regs->reg_mcfbahr) & MC_FW_ADDR_MASK_HIGH;
1749 u64 low = in_le64(&mc_ccsr_regs->reg_mcfbalr) & MC_FW_ADDR_MASK_LOW;
1750 u32 buf_len, wrapped, last_byte, magic, buf_start;
1751 u64 mc_addr = (high << 32) | low;
1752 struct log_header *header;
1753 ssize_t size, bytes_end;
1754 const void *end_of_data;
1755 const void *map_addr;
1756 const void *end_addr;
1757 const void *cur_ptr;
1758 const void *buf;
1759
1760 map_addr = map_sysmem(mc_addr + MC_STRUCT_BUFFER_OFFSET,
1761 MC_BUFFER_SIZE);
1762 header = (struct log_header *)map_addr;
1763 last_byte = in_le32(&header->last_byte);
1764 buf_len = in_le32(&header->buf_length);
1765 magic = in_le32(&header->magic_word);
1766 buf_start = in_le32(&header->buf_start);
1767 buf = map_addr + buf_start - MC_OFFSET_DELTA;
1768 end_addr = buf + buf_len;
1769 wrapped = last_byte & LOG_HEADER_FLAG_BUFFER_WRAPAROUND;
1770 end_of_data = buf + LAST_BYTE(last_byte);
1771
1772 if (magic != MAGIC_MC) {
1773 puts("Magic number is not valid\n");
1774 printf("expected = %08x, received = %08x\n", MAGIC_MC, magic);
1775 goto err_magic;
1776 }
1777
1778 if (wrapped && end_of_data != end_addr)
1779 cur_ptr = end_of_data + 1;
1780 else
1781 cur_ptr = buf;
1782
1783 if (cur_ptr <= end_of_data)
1784 size = end_of_data - cur_ptr;
1785 else
1786 size = (end_addr - cur_ptr) + (end_of_data - buf);
1787
1788 bytes_end = end_addr - cur_ptr;
1789 if (size > bytes_end) {
1790 print_k_bytes(cur_ptr, &bytes_end);
1791
Cosmin-Florin Alucheneseiaa280142021-10-07 13:07:44 +03001792 size -= bytes_end;
1793 }
1794
1795 print_k_bytes(buf, &size);
1796
1797err_magic:
1798 unmap_sysmem(map_addr);
1799}
1800
Simon Glassed38aef2020-05-10 11:40:03 -06001801static int do_fsl_mc(struct cmd_tbl *cmdtp, int flag, int argc,
1802 char *const argv[])
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001803{
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301804 int err = 0;
Cosmin-Florin Alucheneseiaa280142021-10-07 13:07:44 +03001805 if (argc < 2)
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301806 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001807
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301808 switch (argv[1][0]) {
1809 case 's': {
1810 char sub_cmd;
Prabhakar Kushwaha122bcfd2015-11-09 16:42:07 +05301811 u64 mc_fw_addr, mc_dpc_addr;
Tom Rini6a5dccc2022-11-16 13:10:41 -05001812#ifdef CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
Prabhakar Kushwaha122bcfd2015-11-09 16:42:07 +05301813 u64 aiop_fw_addr;
1814#endif
Cosmin-Florin Alucheneseiaa280142021-10-07 13:07:44 +03001815 if (argc < 3)
1816 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001817
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301818 sub_cmd = argv[2][0];
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301819
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301820 switch (sub_cmd) {
1821 case 'm':
1822 if (argc < 5)
1823 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001824
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301825 if (get_mc_boot_status() == 0) {
1826 printf("fsl-mc: MC is already booted");
1827 printf("\n");
1828 return err;
1829 }
1830 mc_fw_addr = simple_strtoull(argv[3], NULL, 16);
1831 mc_dpc_addr = simple_strtoull(argv[4], NULL,
1832 16);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301833
1834 if (!mc_init(mc_fw_addr, mc_dpc_addr))
1835 err = mc_init_object();
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301836 break;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001837
Tom Rini6a5dccc2022-11-16 13:10:41 -05001838#ifdef CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301839 case 'a':
1840 if (argc < 4)
1841 goto usage;
1842 if (get_aiop_apply_status() == 0) {
1843 printf("fsl-mc: AIOP FW is already");
1844 printf(" applied\n");
1845 return err;
1846 }
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001847
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301848 aiop_fw_addr = simple_strtoull(argv[3], NULL,
1849 16);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001850
York Suncbe8e1c2016-04-04 11:41:26 -07001851 /* if SoC doesn't have AIOP, err = -ENODEV */
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301852 err = load_mc_aiop_img(aiop_fw_addr);
1853 if (!err)
1854 printf("fsl-mc: AIOP FW applied\n");
1855 break;
1856#endif
1857 default:
1858 printf("Invalid option: %s\n", argv[2]);
1859 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001860
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301861 break;
1862 }
1863 }
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001864 break;
1865
1866 case 'l': {
1867 /* lazyapply */
1868 u64 mc_dpl_addr;
1869
1870 if (argc < 4)
1871 goto usage;
1872
1873 if (get_dpl_apply_status() == 0) {
1874 printf("fsl-mc: DPL already applied\n");
1875 return err;
1876 }
1877
1878 mc_dpl_addr = simple_strtoull(argv[3], NULL, 16);
1879
1880 if (get_mc_boot_status() != 0) {
1881 printf("fsl-mc: Deploying data path layout ..");
1882 printf("ERROR (MC is not booted)\n");
1883 return -ENODEV;
1884 }
1885
1886 /*
1887 * We will do the actual dpaa exit and dpl apply
1888 * later from announce_and_cleanup().
1889 */
1890 mc_lazy_dpl_addr = mc_dpl_addr;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301891 break;
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001892 }
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001893
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301894 case 'a': {
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001895 /* apply */
1896 char sub_cmd;
1897 u64 mc_apply_addr;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001898
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001899 if (argc < 4)
1900 goto usage;
1901
1902 sub_cmd = argv[2][0];
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001903
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001904 switch (sub_cmd) {
1905 case 'd':
1906 case 'D':
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301907 if (get_dpl_apply_status() == 0) {
1908 printf("fsl-mc: DPL already applied\n");
1909 return err;
1910 }
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301911 if (get_mc_boot_status() != 0) {
1912 printf("fsl-mc: Deploying data path layout ..");
1913 printf("ERROR (MC is not booted)\n");
1914 return -ENODEV;
1915 }
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301916
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001917 mc_apply_addr = simple_strtoull(argv[3], NULL, 16);
1918
1919 /* The user wants DPL applied now */
1920 if (!fsl_mc_ldpaa_exit(NULL))
1921 err = mc_apply_dpl(mc_apply_addr);
1922 break;
1923
1924 case 's':
1925 if (!is_dpsparser_supported()) {
1926 printf("fsl-mc: apply spb command .. ");
1927 printf("ERROR: requires at least MC 10.12.0\n");
1928 return err;
Alexander Graf2ebeb442016-11-17 01:02:57 +01001929 }
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001930 if (get_mc_boot_status() != 0) {
1931 printf("fsl-mc: Deploying Soft Parser Blob...");
1932 printf("ERROR (MC is not booted)\n");
1933 return err;
1934 }
1935
1936 mc_apply_addr = simple_strtoull(argv[3], NULL, 16);
1937
1938 /* Apply spb (Soft Parser Blob) */
1939 err = mc_apply_spb(mc_apply_addr);
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301940 break;
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001941
1942 default:
1943 printf("Invalid option: %s\n", argv[2]);
1944 goto usage;
1945 }
1946 break;
J. German Riveraf4fed4b2015-03-20 19:28:18 -07001947 }
Cosmin-Florin Alucheneseiaa280142021-10-07 13:07:44 +03001948 case 'd':
1949 if (argc > 2)
1950 goto usage;
1951
1952 mc_dump_log();
1953 break;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301954 default:
1955 printf("Invalid option: %s\n", argv[1]);
1956 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001957 }
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301958 return err;
1959 usage:
1960 return CMD_RET_USAGE;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001961}
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301962
1963U_BOOT_CMD(
1964 fsl_mc, CONFIG_SYS_MAXARGS, 1, do_fsl_mc,
1965 "DPAA2 command to manage Management Complex (MC)",
1966 "start mc [FW_addr] [DPC_addr] - Start Management Complex\n"
1967 "fsl_mc apply DPL [DPL_addr] - Apply DPL file\n"
Alexander Graf2ebeb442016-11-17 01:02:57 +01001968 "fsl_mc lazyapply DPL [DPL_addr] - Apply DPL file on exit\n"
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001969 "fsl_mc apply spb [spb_addr] - Apply SPB Soft Parser Blob\n"
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301970 "fsl_mc start aiop [FW_addr] - Start AIOP\n"
Cosmin-Florin Alucheneseiaa280142021-10-07 13:07:44 +03001971 "fsl_mc dump_log - Dump MC Log\n"
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301972);
Bogdan Purcareata08bc0142017-05-24 16:40:21 +00001973
1974void mc_env_boot(void)
1975{
1976#if defined(CONFIG_FSL_MC_ENET)
1977 char *mc_boot_env_var;
1978 /* The MC may only be initialized in the reset PHY function
1979 * because otherwise U-Boot has not yet set up all the MAC
1980 * address info properly. Without MAC addresses, the MC code
1981 * can not properly initialize the DPC.
1982 */
Simon Glass64b723f2017-08-03 12:22:12 -06001983 mc_boot_env_var = env_get(MC_BOOT_ENV_VAR);
Bogdan Purcareata08bc0142017-05-24 16:40:21 +00001984 if (mc_boot_env_var)
1985 run_command_list(mc_boot_env_var, -1, 0);
1986#endif /* CONFIG_FSL_MC_ENET */
1987}