blob: 440273a1756298cc2173e4ac2aa1b8be456cdb2f [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) {
359 printf("fdt_increase_size: err=%s\n",
360 fdt_strerror(err));
361 return err;
362 }
363
Bogdan Purcareata72669142017-05-24 16:40:22 +0000364 noff = fdt_add_subnode(blob, nodeoffset, mac_name);
365 if (noff < 0) {
366 printf("fdt_add_subnode: err=%s\n",
367 fdt_strerror(err));
368 return err;
369 }
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000370
371 /* add default property of fixed link */
Bogdan Purcareata72669142017-05-24 16:40:22 +0000372 err = fdt_appendprop_string(blob, noff,
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000373 "link_type", link_type_mode);
374 if (err) {
375 printf("fdt_appendprop_string: err=%s\n",
376 fdt_strerror(err));
377 return err;
378 }
379 }
380
Bogdan Purcareata72669142017-05-24 16:40:22 +0000381 return mc_fixup_mac_addr(blob, noff, "port_mac_address", eth_dev,
382 MC_FIXUP_DPC);
383}
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000384
Bogdan Purcareata72669142017-05-24 16:40:22 +0000385static int mc_fixup_mac_addrs(void *blob, enum mc_fixup_type type)
386{
387 int i, err = 0, ret = 0;
Ioana Ciornei25f7cf72020-03-18 16:47:38 +0200388#define ETH_NAME_LEN 20
389 struct udevice *eth_dev;
Ioana Ciornei25f7cf72020-03-18 16:47:38 +0200390 char ethname[ETH_NAME_LEN];
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000391
Bogdan Purcareata72669142017-05-24 16:40:22 +0000392 for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++) {
393 /* port not enabled */
Pankaj Bansal7987ab72018-10-10 14:08:33 +0530394 if (wriop_is_enabled_dpmac(i) != 1)
Bogdan Purcareata72669142017-05-24 16:40:22 +0000395 continue;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000396
Pankaj Bansal8a921702018-08-02 16:31:28 +0530397 snprintf(ethname, ETH_NAME_LEN, "DPMAC%d@%s", i,
398 phy_interface_strings[wriop_get_enet_if(i)]);
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000399
Bogdan Purcareata72669142017-05-24 16:40:22 +0000400 eth_dev = eth_get_dev_by_name(ethname);
401 if (eth_dev == NULL)
402 continue;
403
404 switch (type) {
405 case MC_FIXUP_DPL:
406 err = mc_fixup_dpl_mac_addr(blob, i, eth_dev);
407 break;
408 case MC_FIXUP_DPC:
409 err = mc_fixup_dpc_mac_addr(blob, i, eth_dev);
410 break;
411 default:
412 break;
413 }
414
415 if (err)
416 printf("fsl-mc: ERROR fixing mac address for %s\n",
417 ethname);
418 ret |= err;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000419 }
420
Bogdan Purcareata72669142017-05-24 16:40:22 +0000421 return ret;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000422}
423
Stuart Yodera2bdb772015-07-02 11:29:03 +0530424static int mc_fixup_dpc(u64 dpc_addr)
425{
426 void *blob = (void *)dpc_addr;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000427 int nodeoffset, err = 0;
Stuart Yodera2bdb772015-07-02 11:29:03 +0530428
429 /* delete any existing ICID pools */
430 nodeoffset = fdt_path_offset(blob, "/resources/icid_pools");
431 if (fdt_del_node(blob, nodeoffset) < 0)
432 printf("\nfsl-mc: WARNING: could not delete ICID pool\n");
433
434 /* add a new pool */
435 nodeoffset = fdt_path_offset(blob, "/resources");
436 if (nodeoffset < 0) {
437 printf("\nfsl-mc: ERROR: DPC is missing /resources\n");
438 return -EINVAL;
439 }
440 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pools");
441 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pool@0");
442 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
443 "base_icid", FSL_DPAA2_STREAM_ID_START, 1);
444 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
445 "num",
446 FSL_DPAA2_STREAM_ID_END -
447 FSL_DPAA2_STREAM_ID_START + 1, 1);
448
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000449 /* fixup MAC addresses for dpmac ports */
450 nodeoffset = fdt_path_offset(blob, "/board_info/ports");
Razvan Ionut Cirjana3bbc3b2020-04-28 16:09:59 +0300451 if (nodeoffset < 0) {
452 err = fdt_increase_size(blob, 512);
453 if (err) {
454 printf("fdt_increase_size: err=%s\n",
455 fdt_strerror(err));
456 goto out;
457 }
458 nodeoffset = fdt_path_offset(blob, "/board_info");
459 if (nodeoffset < 0)
460 nodeoffset = fdt_add_subnode(blob, 0, "board_info");
461
462 nodeoffset = fdt_add_subnode(blob, nodeoffset, "ports");
463 }
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000464
Bogdan Purcareata72669142017-05-24 16:40:22 +0000465 err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPC);
Ioana Ciocoi Radulescud7697f42019-02-26 15:50:07 +0000466
467out:
Stuart Yodera2bdb772015-07-02 11:29:03 +0530468 flush_dcache_range(dpc_addr, dpc_addr + fdt_totalsize(blob));
469
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000470 return err;
Stuart Yodera2bdb772015-07-02 11:29:03 +0530471}
472
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530473static 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 -0700474{
475 u64 mc_dpc_offset;
476#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
477 int error;
478 void *dpc_fdt_hdr;
479 int dpc_size;
480#endif
481
Tom Rini6a5dccc2022-11-16 13:10:41 -0500482#ifdef CFG_SYS_LS_MC_DRAM_DPC_OFFSET
483 BUILD_BUG_ON((CFG_SYS_LS_MC_DRAM_DPC_OFFSET & 0x3) != 0 ||
484 CFG_SYS_LS_MC_DRAM_DPC_OFFSET > 0xffffffff);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700485
Tom Rini6a5dccc2022-11-16 13:10:41 -0500486 mc_dpc_offset = CFG_SYS_LS_MC_DRAM_DPC_OFFSET;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700487#else
Tom Rini6a5dccc2022-11-16 13:10:41 -0500488#error "CFG_SYS_LS_MC_DRAM_DPC_OFFSET not defined"
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700489#endif
490
491 /*
492 * Load the MC DPC blob in the MC private DRAM block:
493 */
494#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
495 printf("MC DPC is preloaded to %#llx\n", mc_ram_addr + mc_dpc_offset);
496#else
497 /*
498 * Get address and size of the DPC blob stored in flash:
499 */
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530500 dpc_fdt_hdr = (void *)mc_dpc_addr;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700501
502 error = fdt_check_header(dpc_fdt_hdr);
503 if (error != 0) {
504 /*
505 * Don't return with error here, since the MC firmware can
506 * still boot without a DPC
507 */
J. German Rivera75d7ace2015-07-02 11:28:56 +0530508 printf("\nfsl-mc: WARNING: No DPC image found");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700509 return 0;
510 }
511
512 dpc_size = fdt_totalsize(dpc_fdt_hdr);
Tom Rini6a5dccc2022-11-16 13:10:41 -0500513 if (dpc_size > CFG_SYS_LS_MC_DPC_MAX_LENGTH) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530514 printf("\nfsl-mc: ERROR: Bad DPC image (too large: %d)\n",
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700515 dpc_size);
516 return -EINVAL;
517 }
518
519 mc_copy_image("MC DPC blob",
520 (u64)dpc_fdt_hdr, dpc_size, mc_ram_addr + mc_dpc_offset);
521#endif /* not defined CONFIG_SYS_LS_MC_DPC_IN_DDR */
522
Stuart Yodera2bdb772015-07-02 11:29:03 +0530523 if (mc_fixup_dpc(mc_ram_addr + mc_dpc_offset))
524 return -EINVAL;
525
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700526 dump_ram_words("DPC", (void *)(mc_ram_addr + mc_dpc_offset));
527 return 0;
528}
529
Bogdan Purcareata72669142017-05-24 16:40:22 +0000530
531static int mc_fixup_dpl(u64 dpl_addr)
532{
533 void *blob = (void *)dpl_addr;
534 u32 ver = fdt_getprop_u32_default(blob, "/", "dpl-version", 0);
535 int err = 0;
536
537 /* The DPL fixup for mac addresses is only relevant
538 * for old-style DPLs
539 */
540 if (ver >= 10)
541 return 0;
542
543 err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPL);
544 flush_dcache_range(dpl_addr, dpl_addr + fdt_totalsize(blob));
545
546 return err;
547}
548
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530549static 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 -0700550{
551 u64 mc_dpl_offset;
552#ifndef CONFIG_SYS_LS_MC_DPL_IN_DDR
553 int error;
554 void *dpl_fdt_hdr;
555 int dpl_size;
556#endif
557
Tom Rini6a5dccc2022-11-16 13:10:41 -0500558#ifdef CFG_SYS_LS_MC_DRAM_DPL_OFFSET
559 BUILD_BUG_ON((CFG_SYS_LS_MC_DRAM_DPL_OFFSET & 0x3) != 0 ||
560 CFG_SYS_LS_MC_DRAM_DPL_OFFSET > 0xffffffff);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700561
Tom Rini6a5dccc2022-11-16 13:10:41 -0500562 mc_dpl_offset = CFG_SYS_LS_MC_DRAM_DPL_OFFSET;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700563#else
Tom Rini6a5dccc2022-11-16 13:10:41 -0500564#error "CFG_SYS_LS_MC_DRAM_DPL_OFFSET not defined"
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700565#endif
566
567 /*
568 * Load the MC DPL blob in the MC private DRAM block:
569 */
570#ifdef CONFIG_SYS_LS_MC_DPL_IN_DDR
571 printf("MC DPL is preloaded to %#llx\n", mc_ram_addr + mc_dpl_offset);
572#else
573 /*
574 * Get address and size of the DPL blob stored in flash:
575 */
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530576 dpl_fdt_hdr = (void *)mc_dpl_addr;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700577
578 error = fdt_check_header(dpl_fdt_hdr);
579 if (error != 0) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530580 printf("\nfsl-mc: ERROR: Bad DPL image (bad header)\n");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700581 return error;
582 }
583
584 dpl_size = fdt_totalsize(dpl_fdt_hdr);
Tom Rini6a5dccc2022-11-16 13:10:41 -0500585 if (dpl_size > CFG_SYS_LS_MC_DPL_MAX_LENGTH) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530586 printf("\nfsl-mc: ERROR: Bad DPL image (too large: %d)\n",
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700587 dpl_size);
588 return -EINVAL;
589 }
590
591 mc_copy_image("MC DPL blob",
592 (u64)dpl_fdt_hdr, dpl_size, mc_ram_addr + mc_dpl_offset);
593#endif /* not defined CONFIG_SYS_LS_MC_DPL_IN_DDR */
594
Bogdan Purcareata72669142017-05-24 16:40:22 +0000595 if (mc_fixup_dpl(mc_ram_addr + mc_dpl_offset))
596 return -EINVAL;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700597 dump_ram_words("DPL", (void *)(mc_ram_addr + mc_dpl_offset));
598 return 0;
599}
600
601/**
602 * Return the MC boot timeout value in milliseconds
603 */
604static unsigned long get_mc_boot_timeout_ms(void)
605{
Tom Rini6a5dccc2022-11-16 13:10:41 -0500606 unsigned long timeout_ms = CFG_SYS_LS_MC_BOOT_TIMEOUT_MS;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700607
Simon Glass64b723f2017-08-03 12:22:12 -0600608 char *timeout_ms_env_var = env_get(MC_BOOT_TIMEOUT_ENV_VAR);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700609
610 if (timeout_ms_env_var) {
Simon Glassff9b9032021-07-24 09:03:30 -0600611 timeout_ms = dectoul(timeout_ms_env_var, NULL);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700612 if (timeout_ms == 0) {
613 printf("fsl-mc: WARNING: Invalid value for \'"
614 MC_BOOT_TIMEOUT_ENV_VAR
615 "\' environment variable: %lu\n",
616 timeout_ms);
617
Tom Rini6a5dccc2022-11-16 13:10:41 -0500618 timeout_ms = CFG_SYS_LS_MC_BOOT_TIMEOUT_MS;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700619 }
620 }
621
622 return timeout_ms;
623}
624
Tom Rini6a5dccc2022-11-16 13:10:41 -0500625#ifdef CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
York Suncbe8e1c2016-04-04 11:41:26 -0700626
627__weak bool soc_has_aiop(void)
628{
629 return false;
630}
631
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530632static int load_mc_aiop_img(u64 aiop_fw_addr)
J. German Riverac3b505f2015-07-02 11:28:58 +0530633{
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530634 u64 mc_ram_addr = mc_get_dram_addr();
635#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
J. German Riverac3b505f2015-07-02 11:28:58 +0530636 void *aiop_img;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530637#endif
J. German Riverac3b505f2015-07-02 11:28:58 +0530638
York Suncbe8e1c2016-04-04 11:41:26 -0700639 /* Check if AIOP is available */
640 if (!soc_has_aiop())
641 return -ENODEV;
J. German Riverac3b505f2015-07-02 11:28:58 +0530642 /*
643 * Load the MC AIOP image in the MC private DRAM block:
644 */
645
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530646#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
647 printf("MC AIOP is preloaded to %#llx\n", mc_ram_addr +
Tom Rini6a5dccc2022-11-16 13:10:41 -0500648 CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530649#else
650 aiop_img = (void *)aiop_fw_addr;
J. German Riverac3b505f2015-07-02 11:28:58 +0530651 mc_copy_image("MC AIOP image",
Tom Rini6a5dccc2022-11-16 13:10:41 -0500652 (u64)aiop_img, CFG_SYS_LS_MC_AIOP_IMG_MAX_LENGTH,
653 mc_ram_addr + CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530654#endif
655 mc_aiop_applied = 0;
J. German Riverac3b505f2015-07-02 11:28:58 +0530656
657 return 0;
658}
659#endif
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530660
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700661static int wait_for_mc(bool booting_mc, u32 *final_reg_gsr)
662{
663 u32 reg_gsr;
664 u32 mc_fw_boot_status;
665 unsigned long timeout_ms = get_mc_boot_timeout_ms();
666 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
667
668 dmb();
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700669 assert(timeout_ms > 0);
670 for (;;) {
671 udelay(1000); /* throttle polling */
672 reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
673 mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
674 if (mc_fw_boot_status & 0x1)
675 break;
676
677 timeout_ms--;
678 if (timeout_ms == 0)
679 break;
680 }
681
682 if (timeout_ms == 0) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530683 printf("ERROR: timeout\n");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700684
685 /* TODO: Get an error status from an MC CCSR register */
686 return -ETIMEDOUT;
687 }
688
689 if (mc_fw_boot_status != 0x1) {
690 /*
691 * TODO: Identify critical errors from the GSR register's FS
692 * field and for those errors, set error to -ENODEV or other
693 * appropriate errno, so that the status property is set to
694 * failure in the fsl,dprc device tree node.
695 */
J. German Rivera75d7ace2015-07-02 11:28:56 +0530696 printf("WARNING: Firmware returned an error (GSR: %#x)\n",
697 reg_gsr);
698 } else {
699 printf("SUCCESS\n");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700700 }
701
J. German Rivera75d7ace2015-07-02 11:28:56 +0530702
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700703 *final_reg_gsr = reg_gsr;
704 return 0;
705}
J. German Rivera8ff14b72014-06-23 15:15:55 -0700706
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530707int mc_init(u64 mc_fw_addr, u64 mc_dpc_addr)
J. German Rivera8ff14b72014-06-23 15:15:55 -0700708{
709 int error = 0;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700710 int portal_id = 0;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700711 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530712 u64 mc_ram_addr = mc_get_dram_addr();
J. German Rivera8ff14b72014-06-23 15:15:55 -0700713 u32 reg_gsr;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700714 u32 reg_mcfbalr;
715#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Rivera8ff14b72014-06-23 15:15:55 -0700716 const void *raw_image_addr;
717 size_t raw_image_size = 0;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700718#endif
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700719 u8 mc_ram_num_256mb_blocks;
720 size_t mc_ram_size = mc_get_dram_block_size();
J. German Rivera8ff14b72014-06-23 15:15:55 -0700721
York Sunc55d7312017-03-06 09:02:29 -0800722 mc_ram_num_256mb_blocks = mc_ram_size / MC_RAM_SIZE_ALIGNMENT;
Meenakshi Aggarwal67f195c2019-02-27 14:41:02 +0530723
724 if (mc_ram_num_256mb_blocks >= 0xff) {
York Sunc55d7312017-03-06 09:02:29 -0800725 error = -EINVAL;
726 printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
727 mc_ram_size);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700728 goto out;
York Sunc55d7312017-03-06 09:02:29 -0800729 }
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700730
J. German Rivera8ff14b72014-06-23 15:15:55 -0700731 /*
Meenakshi Aggarwal67f195c2019-02-27 14:41:02 +0530732 * To support 128 MB DDR Size for MC
733 */
734 if (mc_ram_num_256mb_blocks == 0)
735 mc_ram_num_256mb_blocks = 0xFF;
736
737 /*
J. German Rivera8ff14b72014-06-23 15:15:55 -0700738 * Management Complex cores should be held at reset out of POR.
Bin Meng75574052016-02-05 19:30:11 -0800739 * U-Boot should be the first software to touch MC. To be safe,
J. German Rivera8ff14b72014-06-23 15:15:55 -0700740 * we reset all cores again by setting GCR1 to 0. It doesn't do
741 * anything if they are held at reset. After we setup the firmware
742 * we kick off MC by deasserting the reset bit for core 0, and
743 * deasserting the reset bits for Command Portal Managers.
744 * The stop bits are not touched here. They are used to stop the
745 * cores when they are active. Setting stop bits doesn't stop the
746 * cores from fetching instructions when they are released from
747 * reset.
748 */
749 out_le32(&mc_ccsr_regs->reg_gcr1, 0);
750 dmb();
751
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700752#ifdef CONFIG_SYS_LS_MC_FW_IN_DDR
753 printf("MC firmware is preloaded to %#llx\n", mc_ram_addr);
754#else
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530755 error = parse_mc_firmware_fit_image(mc_fw_addr, &raw_image_addr,
756 &raw_image_size);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700757 if (error != 0)
758 goto out;
759 /*
760 * Load the MC FW at the beginning of the MC private DRAM block:
761 */
J. German Rivera43e4ae32015-01-06 13:19:02 -0800762 mc_copy_image("MC Firmware",
763 (u64)raw_image_addr, raw_image_size, mc_ram_addr);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800764#endif
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700765 dump_ram_words("firmware", (void *)mc_ram_addr);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800766
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530767 error = load_mc_dpc(mc_ram_addr, mc_ram_size, mc_dpc_addr);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700768 if (error != 0)
J. German Rivera8ff14b72014-06-23 15:15:55 -0700769 goto out;
J. German Riverac3b505f2015-07-02 11:28:58 +0530770
J. German Rivera8ff14b72014-06-23 15:15:55 -0700771 debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700772 dump_mc_ccsr_regs(mc_ccsr_regs);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700773
774 /*
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700775 * Tell MC what is the address range of the DRAM block assigned to it:
J. German Rivera8ff14b72014-06-23 15:15:55 -0700776 */
Meenakshi Aggarwal67f195c2019-02-27 14:41:02 +0530777 if (mc_ram_num_256mb_blocks < 0xFF) {
778 reg_mcfbalr = (u32)mc_ram_addr |
779 (mc_ram_num_256mb_blocks - 1);
780 } else {
781 reg_mcfbalr = (u32)mc_ram_addr |
782 (mc_ram_num_256mb_blocks);
783 }
784
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700785 out_le32(&mc_ccsr_regs->reg_mcfbalr, reg_mcfbalr);
786 out_le32(&mc_ccsr_regs->reg_mcfbahr,
York Sunc55d7312017-03-06 09:02:29 -0800787 (u32)(mc_ram_addr >> 32));
Stuart Yoderd1970352015-07-02 11:29:02 +0530788 out_le32(&mc_ccsr_regs->reg_mcfapr, FSL_BYPASS_AMQ);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700789
790 /*
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700791 * Tell the MC that we want delayed DPL deployment.
J. German Rivera8ff14b72014-06-23 15:15:55 -0700792 */
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700793 out_le32(&mc_ccsr_regs->reg_gsr, 0xDD00);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700794
J. German Rivera75d7ace2015-07-02 11:28:56 +0530795 printf("\nfsl-mc: Booting Management Complex ... ");
J. German Rivera43e4ae32015-01-06 13:19:02 -0800796
J. German Rivera8ff14b72014-06-23 15:15:55 -0700797 /*
798 * Deassert reset and release MC core 0 to run
799 */
800 out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700801 error = wait_for_mc(true, &reg_gsr);
802 if (error != 0)
J. German Rivera8ff14b72014-06-23 15:15:55 -0700803 goto out;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700804
J. German Rivera43e4ae32015-01-06 13:19:02 -0800805 /*
806 * TODO: need to obtain the portal_id for the root container from the
807 * DPL
808 */
809 portal_id = 0;
810
811 /*
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700812 * Initialize the global default MC portal
813 * And check that the MC firmware is responding portal commands:
J. German Rivera43e4ae32015-01-06 13:19:02 -0800814 */
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +0530815 root_mc_io = (struct fsl_mc_io *)calloc(sizeof(struct fsl_mc_io), 1);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530816 if (!root_mc_io) {
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +0530817 printf(" No memory: calloc() failed\n");
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700818 return -ENOMEM;
819 }
820
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530821 root_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(portal_id);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800822 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 +0530823 portal_id, root_mc_io->mmio_regs);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800824
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530825 error = mc_get_version(root_mc_io, MC_CMD_NO_FLAGS, &mc_ver_info);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800826 if (error != 0) {
827 printf("fsl-mc: ERROR: Firmware version check failed (error: %d)\n",
828 error);
829 goto out;
830 }
831
J. German Rivera43e4ae32015-01-06 13:19:02 -0800832 printf("fsl-mc: Management Complex booted (version: %d.%d.%d, boot status: %#x)\n",
833 mc_ver_info.major, mc_ver_info.minor, mc_ver_info.revision,
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700834 reg_gsr & GSR_FS_MASK);
835
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530836out:
837 if (error != 0)
838 mc_boot_status = error;
839 else
840 mc_boot_status = 0;
841
842 return error;
843}
844
845int mc_apply_dpl(u64 mc_dpl_addr)
846{
847 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
848 int error = 0;
849 u32 reg_gsr;
850 u64 mc_ram_addr = mc_get_dram_addr();
851 size_t mc_ram_size = mc_get_dram_block_size();
852
Alexander Graf2ebeb442016-11-17 01:02:57 +0100853 if (!mc_dpl_addr)
854 return -1;
855
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530856 error = load_mc_dpl(mc_ram_addr, mc_ram_size, mc_dpl_addr);
857 if (error != 0)
858 return error;
859
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700860 /*
861 * Tell the MC to deploy the DPL:
862 */
863 out_le32(&mc_ccsr_regs->reg_gsr, 0x0);
J. German Rivera75d7ace2015-07-02 11:28:56 +0530864 printf("fsl-mc: Deploying data path layout ... ");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700865 error = wait_for_mc(false, &reg_gsr);
J. German Rivera75d7ace2015-07-02 11:28:56 +0530866
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530867 if (!error)
868 mc_dpl_applied = 0;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700869
870 return error;
871}
872
873int get_mc_boot_status(void)
874{
875 return mc_boot_status;
876}
877
Tom Rini6a5dccc2022-11-16 13:10:41 -0500878#ifdef CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530879int get_aiop_apply_status(void)
880{
881 return mc_aiop_applied;
882}
883#endif
884
885int get_dpl_apply_status(void)
886{
887 return mc_dpl_applied;
888}
889
Mian Yousaf Kaukab97124652018-12-18 14:01:17 +0100890int is_lazy_dpl_addr_valid(void)
891{
892 return !!mc_lazy_dpl_addr;
893}
894
Priyanka Jaine0e0be52017-08-24 16:42:43 +0530895/*
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530896 * Return the MC address of private DRAM block.
Priyanka Jaine0e0be52017-08-24 16:42:43 +0530897 * As per MC design document, MC initial base address
898 * should be least significant 512MB address of MC private
899 * memory, i.e. address should point to end address masked
900 * with 512MB offset in private DRAM block.
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530901 */
902u64 mc_get_dram_addr(void)
903{
Priyanka Jaine0e0be52017-08-24 16:42:43 +0530904 size_t mc_ram_size = mc_get_dram_block_size();
905
Prabhakar Kushwaha145e4cd2018-08-27 12:58:52 +0530906 if (!mc_memset_resv_ram || (get_mc_boot_status() < 0)) {
907 mc_memset_resv_ram = 1;
908 memset((void *)gd->arch.resv_ram, 0, mc_ram_size);
909 }
910
Priyanka Jaine0e0be52017-08-24 16:42:43 +0530911 return (gd->arch.resv_ram + mc_ram_size - 1) &
912 MC_RAM_BASE_ADDR_ALIGNMENT_MASK;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530913}
914
915/**
J. German Rivera8ff14b72014-06-23 15:15:55 -0700916 * Return the actual size of the MC private DRAM block.
J. German Rivera8ff14b72014-06-23 15:15:55 -0700917 */
918unsigned long mc_get_dram_block_size(void)
919{
Tom Rini6a5dccc2022-11-16 13:10:41 -0500920 unsigned long dram_block_size = CFG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700921
Simon Glass64b723f2017-08-03 12:22:12 -0600922 char *dram_block_size_env_var = env_get(MC_MEM_SIZE_ENV_VAR);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700923
924 if (dram_block_size_env_var) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600925 dram_block_size = hextoul(dram_block_size_env_var, NULL);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700926
Tom Rini6a5dccc2022-11-16 13:10:41 -0500927 if (dram_block_size < CFG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE) {
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700928 printf("fsl-mc: WARNING: Invalid value for \'"
929 MC_MEM_SIZE_ENV_VAR
930 "\' environment variable: %lu\n",
931 dram_block_size);
932
Meenakshi Aggarwal67f195c2019-02-27 14:41:02 +0530933 dram_block_size = MC_DRAM_BLOCK_DEFAULT_SIZE;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700934 }
935 }
936
937 return dram_block_size;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700938}
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700939
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900940int fsl_mc_ldpaa_init(struct bd_info *bis)
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530941{
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530942 int i;
943
944 for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++)
Pankaj Bansal7987ab72018-10-10 14:08:33 +0530945 if (wriop_is_enabled_dpmac(i) == 1)
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530946 ldpaa_eth_init(i, wriop_get_enet_if(i));
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530947 return 0;
948}
949
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530950static int dprc_version_check(struct fsl_mc_io *mc_io, uint16_t handle)
951{
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530952 int error;
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530953 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530954
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530955 error = dprc_get_api_version(mc_io, 0,
956 &major_ver,
957 &minor_ver);
958 if (error < 0) {
959 printf("dprc_get_api_version() failed: %d\n", error);
960 return error;
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530961 }
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530962
963 if (major_ver < DPRC_VER_MAJOR || (major_ver == DPRC_VER_MAJOR &&
964 minor_ver < DPRC_VER_MINOR)) {
965 printf("DPRC version mismatch found %u.%u,",
966 major_ver, minor_ver);
967 printf("supported version is %u.%u\n",
968 DPRC_VER_MAJOR, DPRC_VER_MINOR);
969 }
970
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530971 return error;
972}
973
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530974static int dpio_init(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700975{
976 struct qbman_swp_desc p_des;
977 struct dpio_attr attr;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530978 struct dpio_cfg dpio_cfg;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700979 int err = 0;
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530980 uint16_t major_ver, minor_ver;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700981
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +0530982 dflt_dpio = (struct fsl_dpio_obj *)calloc(
983 sizeof(struct fsl_dpio_obj), 1);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700984 if (!dflt_dpio) {
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +0530985 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530986 err = -ENOMEM;
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +0530987 goto err_calloc;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700988 }
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530989 dpio_cfg.channel_mode = DPIO_LOCAL_CHANNEL;
990 dpio_cfg.num_priorities = 8;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700991
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530992 err = dpio_create(dflt_mc_io,
993 dflt_dprc_handle,
994 MC_CMD_NO_FLAGS,
995 &dpio_cfg,
996 &dflt_dpio->dpio_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530997 if (err < 0) {
998 printf("dpio_create() failed: %d\n", err);
999 err = -ENODEV;
1000 goto err_create;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001001 }
1002
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301003 err = dpio_get_api_version(dflt_mc_io, 0,
1004 &major_ver,
1005 &minor_ver);
1006 if (err < 0) {
1007 printf("dpio_get_api_version() failed: %d\n", err);
1008 goto err_get_api_ver;
1009 }
1010
1011 if (major_ver < DPIO_VER_MAJOR || (major_ver == DPIO_VER_MAJOR &&
1012 minor_ver < DPIO_VER_MINOR)) {
1013 printf("DPRC version mismatch found %u.%u,",
1014 major_ver,
1015 minor_ver);
1016 }
1017
1018 err = dpio_open(dflt_mc_io,
1019 MC_CMD_NO_FLAGS,
1020 dflt_dpio->dpio_id,
1021 &dflt_dpio->dpio_handle);
1022 if (err) {
1023 printf("dpio_open() failed\n");
1024 goto err_open;
1025 }
1026
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301027 memset(&attr, 0, sizeof(struct dpio_attr));
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +05301028 err = dpio_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301029 dflt_dpio->dpio_handle, &attr);
1030 if (err < 0) {
1031 printf("dpio_get_attributes() failed: %d\n", err);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001032 goto err_get_attr;
1033 }
1034
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301035 if (dflt_dpio->dpio_id != attr.id) {
1036 printf("dnpi object id and attribute id are not same\n");
1037 goto err_attr_not_same;
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301038 }
1039
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301040#ifdef DEBUG
Ioana Ciorneib1ba07a2023-01-05 17:03:17 +02001041 printf("Init: DPIO.%d\n", dflt_dpio->dpio_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301042#endif
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301043 err = dpio_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1044 if (err < 0) {
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001045 printf("dpio_enable() failed %d\n", err);
1046 goto err_get_enable;
1047 }
Prabhakar Kushwahafd5d1272015-07-02 11:28:59 +05301048 debug("ce_offset=0x%llx, ci_offset=0x%llx, portalid=%d, prios=%d\n",
1049 attr.qbman_portal_ce_offset,
1050 attr.qbman_portal_ci_offset,
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001051 attr.qbman_portal_id,
1052 attr.num_priorities);
1053
Prabhakar Kushwahafd5d1272015-07-02 11:28:59 +05301054 p_des.cena_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
1055 + attr.qbman_portal_ce_offset);
1056 p_des.cinh_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
1057 + attr.qbman_portal_ci_offset);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001058
1059 dflt_dpio->sw_portal = qbman_swp_init(&p_des);
1060 if (dflt_dpio->sw_portal == NULL) {
1061 printf("qbman_swp_init() failed\n");
1062 goto err_get_swp_init;
1063 }
1064 return 0;
1065
1066err_get_swp_init:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301067 dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001068err_get_enable:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301069err_get_attr:
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301070err_attr_not_same:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301071 dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301072err_open:
1073err_get_api_ver:
1074 dpio_destroy(dflt_mc_io,
1075 dflt_dprc_handle,
1076 MC_CMD_NO_FLAGS,
1077 dflt_dpio->dpio_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301078err_create:
Prabhakar Kushwahac6018bd2016-03-18 16:15:29 +05301079 free(dflt_dpio);
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301080err_calloc:
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001081 return err;
1082}
1083
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301084static int dpio_exit(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001085{
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301086 int err;
1087
1088 err = dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1089 if (err < 0) {
1090 printf("dpio_disable() failed: %d\n", err);
1091 goto err;
1092 }
1093
Cosmin-Florin Aluchenesei971dfd32021-07-21 19:13:33 +03001094 err = dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301095 if (err < 0) {
1096 printf("dpio_close() failed: %d\n", err);
1097 goto err;
1098 }
1099
1100 err = dpio_destroy(dflt_mc_io,
1101 dflt_dprc_handle,
1102 MC_CMD_NO_FLAGS,
1103 dflt_dpio->dpio_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301104 if (err < 0) {
1105 printf("dpio_destroy() failed: %d\n", err);
1106 goto err;
1107 }
1108
1109#ifdef DEBUG
Ioana Ciorneib1ba07a2023-01-05 17:03:17 +02001110 printf("Exit: DPIO.%d\n", dflt_dpio->dpio_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301111#endif
1112
1113 if (dflt_dpio)
1114 free(dflt_dpio);
1115
1116 return 0;
1117err:
1118 return err;
1119}
1120
1121static int dprc_init(void)
1122{
1123 int err, child_portal_id, container_id;
1124 struct dprc_cfg cfg;
1125 uint64_t mc_portal_offset;
1126
1127 /* Open root container */
1128 err = dprc_get_container_id(root_mc_io, MC_CMD_NO_FLAGS, &container_id);
1129 if (err < 0) {
1130 printf("dprc_get_container_id(): Root failed: %d\n", err);
1131 goto err_root_container_id;
1132 }
1133
1134#ifdef DEBUG
1135 printf("Root container id = %d\n", container_id);
1136#endif
1137 err = dprc_open(root_mc_io, MC_CMD_NO_FLAGS, container_id,
1138 &root_dprc_handle);
1139 if (err < 0) {
1140 printf("dprc_open(): Root Container failed: %d\n", err);
1141 goto err_root_open;
1142 }
1143
1144 if (!root_dprc_handle) {
1145 printf("dprc_open(): Root Container Handle is not valid\n");
1146 goto err_root_open;
1147 }
1148
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301149 err = dprc_version_check(root_mc_io, root_dprc_handle);
1150 if (err < 0) {
1151 printf("dprc_version_check() failed: %d\n", err);
1152 goto err_root_open;
1153 }
1154
Prabhakar Kushwaha901a88e2016-01-20 12:04:19 +05301155 memset(&cfg, 0, sizeof(struct dprc_cfg));
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301156 cfg.options = DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED |
1157 DPRC_CFG_OPT_OBJ_CREATE_ALLOWED |
1158 DPRC_CFG_OPT_ALLOC_ALLOWED;
1159 cfg.icid = DPRC_GET_ICID_FROM_POOL;
Prabhakar Kushwaha7fd59d92015-12-24 15:33:49 +05301160 cfg.portal_id = DPRC_GET_PORTAL_ID_FROM_POOL;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301161 err = dprc_create_container(root_mc_io, MC_CMD_NO_FLAGS,
1162 root_dprc_handle,
1163 &cfg,
1164 &child_dprc_id,
1165 &mc_portal_offset);
1166 if (err < 0) {
1167 printf("dprc_create_container() failed: %d\n", err);
1168 goto err_create;
1169 }
1170
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301171 dflt_mc_io = (struct fsl_mc_io *)calloc(sizeof(struct fsl_mc_io), 1);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301172 if (!dflt_mc_io) {
1173 err = -ENOMEM;
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301174 printf(" No memory: calloc() failed\n");
1175 goto err_calloc;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301176 }
1177
1178 child_portal_id = MC_PORTAL_OFFSET_TO_PORTAL_ID(mc_portal_offset);
1179 dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(child_portal_id);
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301180
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301181#ifdef DEBUG
1182 printf("MC portal of child DPRC container: %d, physical addr %p)\n",
1183 child_dprc_id, dflt_mc_io->mmio_regs);
1184#endif
1185
1186 err = dprc_open(dflt_mc_io, MC_CMD_NO_FLAGS, child_dprc_id,
1187 &dflt_dprc_handle);
1188 if (err < 0) {
1189 printf("dprc_open(): Child container failed: %d\n", err);
1190 goto err_child_open;
1191 }
1192
1193 if (!dflt_dprc_handle) {
1194 printf("dprc_open(): Child container Handle is not valid\n");
1195 goto err_child_open;
1196 }
1197
1198 return 0;
1199err_child_open:
1200 free(dflt_mc_io);
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301201err_calloc:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301202 dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1203 root_dprc_handle, child_dprc_id);
1204err_create:
1205 dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1206err_root_open:
1207err_root_container_id:
1208 return err;
1209}
1210
1211static int dprc_exit(void)
1212{
1213 int err;
1214
1215 err = dprc_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dprc_handle);
1216 if (err < 0) {
1217 printf("dprc_close(): Child failed: %d\n", err);
1218 goto err;
1219 }
1220
1221 err = dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1222 root_dprc_handle, child_dprc_id);
1223 if (err < 0) {
1224 printf("dprc_destroy_container() failed: %d\n", err);
1225 goto err;
1226 }
1227
1228 err = dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1229 if (err < 0) {
1230 printf("dprc_close(): Root failed: %d\n", err);
1231 goto err;
1232 }
1233
1234 if (dflt_mc_io)
1235 free(dflt_mc_io);
1236
1237 if (root_mc_io)
1238 free(root_mc_io);
1239
1240 return 0;
1241
1242err:
1243 return err;
1244}
1245
1246static int dpbp_init(void)
1247{
1248 int err;
1249 struct dpbp_attr dpbp_attr;
1250 struct dpbp_cfg dpbp_cfg;
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301251 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301252
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301253 dflt_dpbp = (struct fsl_dpbp_obj *)calloc(
1254 sizeof(struct fsl_dpbp_obj), 1);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001255 if (!dflt_dpbp) {
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301256 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301257 err = -ENOMEM;
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301258 goto err_calloc;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301259 }
1260
1261 dpbp_cfg.options = 512;
1262
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301263 err = dpbp_create(dflt_mc_io,
1264 dflt_dprc_handle,
1265 MC_CMD_NO_FLAGS,
1266 &dpbp_cfg,
1267 &dflt_dpbp->dpbp_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301268
1269 if (err < 0) {
1270 err = -ENODEV;
1271 printf("dpbp_create() failed: %d\n", err);
1272 goto err_create;
1273 }
1274
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301275 err = dpbp_get_api_version(dflt_mc_io, 0,
1276 &major_ver,
1277 &minor_ver);
1278 if (err < 0) {
1279 printf("dpbp_get_api_version() failed: %d\n", err);
1280 goto err_get_api_ver;
1281 }
1282
1283 if (major_ver < DPBP_VER_MAJOR || (major_ver == DPBP_VER_MAJOR &&
1284 minor_ver < DPBP_VER_MINOR)) {
1285 printf("DPBP version mismatch found %u.%u,",
1286 major_ver, minor_ver);
1287 printf("supported version is %u.%u\n",
1288 DPBP_VER_MAJOR, DPBP_VER_MINOR);
1289 }
1290
1291 err = dpbp_open(dflt_mc_io,
1292 MC_CMD_NO_FLAGS,
1293 dflt_dpbp->dpbp_id,
1294 &dflt_dpbp->dpbp_handle);
1295 if (err) {
1296 printf("dpbp_open() failed\n");
1297 goto err_open;
1298 }
1299
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301300 memset(&dpbp_attr, 0, sizeof(struct dpbp_attr));
1301 err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
1302 dflt_dpbp->dpbp_handle,
1303 &dpbp_attr);
1304 if (err < 0) {
1305 printf("dpbp_get_attributes() failed: %d\n", err);
1306 goto err_get_attr;
1307 }
1308
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301309 if (dflt_dpbp->dpbp_id != dpbp_attr.id) {
1310 printf("dpbp object id and attribute id are not same\n");
1311 goto err_attr_not_same;
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301312 }
1313
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301314#ifdef DEBUG
Ioana Ciorneib1ba07a2023-01-05 17:03:17 +02001315 printf("Init: DPBP.%d\n", dflt_dpbp->dpbp_attr.id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301316#endif
1317
1318 err = dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
1319 if (err < 0) {
1320 printf("dpbp_close() failed: %d\n", err);
1321 goto err_close;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001322 }
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001323
1324 return 0;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301325
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301326err_get_attr:
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301327err_attr_not_same:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301328 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301329 dpbp_destroy(dflt_mc_io,
1330 dflt_dprc_handle,
1331 MC_CMD_NO_FLAGS,
1332 dflt_dpbp->dpbp_id);
1333err_get_api_ver:
1334err_close:
1335err_open:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301336err_create:
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301337 free(dflt_dpbp);
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301338err_calloc:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301339 return err;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001340}
1341
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301342static int dpbp_exit(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001343{
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301344 int err;
1345
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301346 err = dpbp_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1347 dflt_dpbp->dpbp_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301348 if (err < 0) {
1349 printf("dpbp_destroy() failed: %d\n", err);
1350 goto err;
1351 }
1352
1353#ifdef DEBUG
Ioana Ciorneib1ba07a2023-01-05 17:03:17 +02001354 printf("Exit: DPBP.%d\n", dflt_dpbp->dpbp_attr.id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301355#endif
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001356
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301357 if (dflt_dpbp)
1358 free(dflt_dpbp);
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301359 return 0;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301360
1361err:
1362 return err;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001363}
1364
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301365static int dpni_init(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001366{
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301367 int err;
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301368 uint8_t cfg_buf[256] = {0};
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301369 struct dpni_cfg dpni_cfg;
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301370 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301371
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301372 dflt_dpni = (struct fsl_dpni_obj *)calloc(
1373 sizeof(struct fsl_dpni_obj), 1);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301374 if (!dflt_dpni) {
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301375 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301376 err = -ENOMEM;
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301377 goto err_calloc;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301378 }
1379
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301380 memset(&dpni_cfg, 0, sizeof(dpni_cfg));
1381 err = dpni_prepare_cfg(&dpni_cfg, &cfg_buf[0]);
Prabhakar Kushwahae26e2852015-12-24 15:33:01 +05301382 if (err < 0) {
1383 err = -ENODEV;
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301384 printf("dpni_prepare_cfg() failed: %d\n", err);
1385 goto err_prepare_cfg;
Prabhakar Kushwahae26e2852015-12-24 15:33:01 +05301386 }
1387
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301388 err = dpni_create(dflt_mc_io,
1389 dflt_dprc_handle,
1390 MC_CMD_NO_FLAGS,
1391 &dpni_cfg,
1392 &dflt_dpni->dpni_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301393 if (err < 0) {
1394 err = -ENODEV;
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301395 printf("dpni create() failed: %d\n", err);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301396 goto err_create;
1397 }
1398
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301399 err = dpni_get_api_version(dflt_mc_io, 0,
1400 &major_ver,
1401 &minor_ver);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301402 if (err < 0) {
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301403 printf("dpni_get_api_version() failed: %d\n", err);
1404 goto err_get_version;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301405 }
1406
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301407 if (major_ver < DPNI_VER_MAJOR || (major_ver == DPNI_VER_MAJOR &&
1408 minor_ver < DPNI_VER_MINOR)) {
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301409 printf("DPNI version mismatch found %u.%u,",
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301410 major_ver, minor_ver);
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301411 printf("supported version is %u.%u\n",
1412 DPNI_VER_MAJOR, DPNI_VER_MINOR);
1413 }
1414
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301415 err = dpni_open(dflt_mc_io,
1416 MC_CMD_NO_FLAGS,
1417 dflt_dpni->dpni_id,
1418 &dflt_dpni->dpni_handle);
1419 if (err) {
1420 printf("dpni_open() failed\n");
1421 goto err_open;
1422 }
1423
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301424#ifdef DEBUG
Ioana Ciorneib1ba07a2023-01-05 17:03:17 +02001425 printf("Init: DPNI.%d\n", dflt_dpni->dpni_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301426#endif
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301427 err = dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
1428 if (err < 0) {
1429 printf("dpni_close() failed: %d\n", err);
1430 goto err_close;
1431 }
1432
1433 return 0;
1434
1435err_close:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301436 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301437err_open:
1438err_get_version:
1439 dpni_destroy(dflt_mc_io,
1440 dflt_dprc_handle,
1441 MC_CMD_NO_FLAGS,
1442 dflt_dpni->dpni_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301443err_create:
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301444err_prepare_cfg:
Prabhakar Kushwahae26e2852015-12-24 15:33:01 +05301445 free(dflt_dpni);
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301446err_calloc:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301447 return err;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001448}
1449
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301450static int dpni_exit(void)
1451{
1452 int err;
1453
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301454 err = dpni_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1455 dflt_dpni->dpni_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301456 if (err < 0) {
1457 printf("dpni_destroy() failed: %d\n", err);
1458 goto err;
1459 }
1460
1461#ifdef DEBUG
Ioana Ciorneib1ba07a2023-01-05 17:03:17 +02001462 printf("Exit: DPNI.%d\n", dflt_dpni->dpni_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301463#endif
1464
1465 if (dflt_dpni)
1466 free(dflt_dpni);
1467 return 0;
1468
1469err:
1470 return err;
1471}
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001472
1473static bool is_dpsparser_supported(void)
1474{
1475 /* dpsparser support was first introduced in MC version: 10.12.0 */
1476 if (mc_ver_info.major < 10)
1477 return false;
1478 if (mc_ver_info.major == 10)
1479 return (mc_ver_info.minor >= 12);
1480 return true;
1481}
1482
1483static int dpsparser_version_check(struct fsl_mc_io *mc_io)
1484{
1485 int error;
1486 u16 major_ver, minor_ver;
1487
1488 if (!is_dpsparser_supported())
1489 return 0;
1490
1491 error = dpsparser_get_api_version(mc_io, 0,
1492 &major_ver,
1493 &minor_ver);
1494 if (error < 0) {
1495 printf("dpsparser_get_api_version() failed: %d\n", error);
1496 return error;
1497 }
1498
1499 if (major_ver < DPSPARSER_VER_MAJOR || (major_ver ==
1500 DPSPARSER_VER_MAJOR && minor_ver < DPSPARSER_VER_MINOR)) {
1501 printf("DPSPARSER version mismatch found %u.%u,",
1502 major_ver, minor_ver);
1503 printf("supported version is %u.%u\n",
1504 DPSPARSER_VER_MAJOR, DPSPARSER_VER_MINOR);
1505 }
1506
1507 return error;
1508}
1509
1510static int dpsparser_init(void)
1511{
1512 int err = 0;
1513
1514 if (!is_dpsparser_supported())
1515 return 0;
1516
1517 err = dpsparser_create(dflt_mc_io,
1518 dflt_dprc_handle,
1519 MC_CMD_NO_FLAGS,
1520 &dpsparser_obj_id);
1521 if (err)
1522 printf("dpsparser_create() failed\n");
1523
1524 err = dpsparser_version_check(dflt_mc_io);
1525 if (err < 0) {
1526 printf("dpsparser_version_check() failed: %d\n", err);
1527 goto err_version_check;
1528 }
1529
1530 err = dpsparser_open(dflt_mc_io,
1531 MC_CMD_NO_FLAGS,
1532 &dpsparser_handle);
1533 if (err < 0) {
1534 printf("dpsparser_open() failed: %d\n", err);
1535 goto err_open;
1536 }
1537
1538 return err;
1539
1540err_open:
1541err_version_check:
1542 dpsparser_destroy(dflt_mc_io,
1543 dflt_dprc_handle,
1544 MC_CMD_NO_FLAGS, dpsparser_obj_id);
1545
1546 return err;
1547}
1548
1549#ifdef DPSPARSER_DESTROY
1550/* TODO: refactoring needed in the future to allow DPSPARSER object destroy
1551 * Workaround: DO NOT destroy DPSPARSER object because it needs to be available
1552 * on Apply DPL
1553 */
1554static int dpsparser_exit(void)
1555{
1556 int err;
1557
1558 if (!is_dpsparser_supported())
1559 return 0;
1560
1561 dpsparser_close(dflt_mc_io, MC_CMD_NO_FLAGS, dpsparser_handle);
1562 if (err < 0) {
1563 printf("dpsparser_close() failed: %d\n", err);
1564 goto err;
1565 }
1566
1567 err = dpsparser_destroy(dflt_mc_io, dflt_dprc_handle,
1568 MC_CMD_NO_FLAGS, dpsparser_obj_id);
1569 if (err < 0) {
1570 printf("dpsparser_destroy() failed: %d\n", err);
1571 goto err;
1572 }
1573 return 0;
1574
1575err:
1576 return err;
1577}
1578#endif
1579
1580int mc_apply_spb(u64 mc_spb_addr)
1581{
1582 int err = 0;
1583 u16 error, err_arr_size;
1584 u64 mc_spb_offset;
1585 u32 spb_size;
1586 struct sp_blob_header *sp_blob;
1587 u64 mc_ram_addr = mc_get_dram_addr();
1588
1589 if (!is_dpsparser_supported())
1590 return 0;
1591
1592 if (!mc_spb_addr) {
1593 printf("fsl-mc: Invalid Blob address\n");
1594 return -1;
1595 }
1596
1597#ifdef CONFIG_MC_DRAM_SPB_OFFSET
1598 mc_spb_offset = CONFIG_MC_DRAM_SPB_OFFSET;
1599#else
1600#error "CONFIG_MC_DRAM_SPB_OFFSET not defined"
1601#endif
1602
1603 // Read blob header and get size of SPB blob
1604 sp_blob = (struct sp_blob_header *)mc_spb_addr;
1605 spb_size = le32_to_cpu(sp_blob->length);
1606 if (spb_size > CONFIG_MC_SPB_MAX_SIZE) {
1607 printf("\nfsl-mc: ERROR: Bad SPB image (too large: %d)\n",
1608 spb_size);
1609 return -EINVAL;
1610 }
1611
1612 mc_copy_image("MC SP Blob", mc_spb_addr, spb_size,
1613 mc_ram_addr + mc_spb_offset);
1614
1615 //Invoke MC command to apply SPB blob
1616 printf("fsl-mc: Applying soft parser blob... ");
1617 err = dpsparser_apply_spb(dflt_mc_io, MC_CMD_NO_FLAGS, dpsparser_handle,
1618 mc_spb_offset, &error);
1619 if (err)
1620 return err;
1621
1622 if (error == 0) {
1623 printf("SUCCESS\n");
1624 } else {
1625 printf("FAILED with error code = %d:\n", error);
1626 err_arr_size = (u16)ARRAY_SIZE(mc_err_msg_apply_spb);
1627
1628 if (error > 0 && error < err_arr_size)
1629 printf(mc_err_msg_apply_spb[error]);
1630 else
1631 printf(MC_ERROR_MSG_SPB_UNKNOWN);
1632 }
1633
1634 return err;
1635}
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301636
1637static int mc_init_object(void)
1638{
1639 int err = 0;
1640
1641 err = dprc_init();
1642 if (err < 0) {
1643 printf("dprc_init() failed: %d\n", err);
1644 goto err;
1645 }
1646
1647 err = dpbp_init();
1648 if (err < 0) {
1649 printf("dpbp_init() failed: %d\n", err);
1650 goto err;
1651 }
1652
1653 err = dpio_init();
1654 if (err < 0) {
1655 printf("dpio_init() failed: %d\n", err);
1656 goto err;
1657 }
1658
1659 err = dpni_init();
1660 if (err < 0) {
1661 printf("dpni_init() failed: %d\n", err);
1662 goto err;
1663 }
1664
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001665 err = dpsparser_init();
1666 if (err < 0) {
1667 printf("dpsparser_init() failed: %d\n", err);
1668 goto err;
1669 }
1670
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301671 return 0;
1672err:
1673 return err;
1674}
1675
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001676int fsl_mc_ldpaa_exit(struct bd_info *bd)
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301677{
1678 int err = 0;
Yogesh Gaur4a81bbe2017-04-27 10:14:16 +05301679 bool is_dpl_apply_status = false;
Santan Kumar0a6e5ba2017-06-29 11:19:34 +05301680 bool mc_boot_status = false;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301681
Alexander Graf2ebeb442016-11-17 01:02:57 +01001682 if (bd && mc_lazy_dpl_addr && !fsl_mc_ldpaa_exit(NULL)) {
Yogesh Gaur8dadfb42018-01-16 10:08:24 +05301683 err = mc_apply_dpl(mc_lazy_dpl_addr);
1684 if (!err)
1685 fdt_fixup_board_enet(working_fdt);
Alexander Graf2ebeb442016-11-17 01:02:57 +01001686 mc_lazy_dpl_addr = 0;
1687 }
1688
Santan Kumar0a6e5ba2017-06-29 11:19:34 +05301689 if (!get_mc_boot_status())
1690 mc_boot_status = true;
1691
Prabhakar Kushwaha878d3ec2016-03-21 14:19:39 +05301692 /* MC is not loaded intentionally, So return success. */
Santan Kumar0a6e5ba2017-06-29 11:19:34 +05301693 if (bd && !mc_boot_status)
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301694 return 0;
1695
Yogesh Gaur4a81bbe2017-04-27 10:14:16 +05301696 /* If DPL is deployed, set is_dpl_apply_status as TRUE. */
1697 if (!get_dpl_apply_status())
1698 is_dpl_apply_status = true;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301699
Yogesh Gaur4a81bbe2017-04-27 10:14:16 +05301700 /*
1701 * For case MC is loaded but DPL is not deployed, return success and
1702 * print message on console. Else FDT fix-up code execution hanged.
1703 */
Santan Kumar0a6e5ba2017-06-29 11:19:34 +05301704 if (bd && mc_boot_status && !is_dpl_apply_status) {
Yogesh Gaur4a81bbe2017-04-27 10:14:16 +05301705 printf("fsl-mc: DPL not deployed, DPAA2 ethernet not work\n");
Yogesh Gaur82709e52017-11-28 10:11:14 +05301706 goto mc_obj_cleanup;
Yogesh Gaur4a81bbe2017-04-27 10:14:16 +05301707 }
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301708
Santan Kumar0a6e5ba2017-06-29 11:19:34 +05301709 if (bd && mc_boot_status && is_dpl_apply_status)
1710 return 0;
1711
Yogesh Gaur82709e52017-11-28 10:11:14 +05301712mc_obj_cleanup:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301713 err = dpbp_exit();
1714 if (err < 0) {
Prabhakar Kushwaha88ba4d62016-01-20 12:04:37 +05301715 printf("dpbp_exit() failed: %d\n", err);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301716 goto err;
1717 }
1718
1719 err = dpio_exit();
1720 if (err < 0) {
1721 printf("dpio_exit() failed: %d\n", err);
1722 goto err;
1723 }
1724
1725 err = dpni_exit();
1726 if (err < 0) {
1727 printf("dpni_exit() failed: %d\n", err);
1728 goto err;
1729 }
1730
1731 err = dprc_exit();
1732 if (err < 0) {
1733 printf("dprc_exit() failed: %d\n", err);
1734 goto err;
1735 }
1736
1737 return 0;
1738err:
1739 return err;
1740}
1741
Cosmin-Florin Alucheneseiaa280142021-10-07 13:07:44 +03001742static void print_k_bytes(const void *buf, ssize_t *size)
1743{
1744 while (*size > 0) {
1745 int count = printf("%s", (char *)buf);
1746
1747 buf += count;
1748 *size -= count;
1749 }
1750}
1751
1752static void mc_dump_log(void)
1753{
1754 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
1755 u64 high = in_le64(&mc_ccsr_regs->reg_mcfbahr) & MC_FW_ADDR_MASK_HIGH;
1756 u64 low = in_le64(&mc_ccsr_regs->reg_mcfbalr) & MC_FW_ADDR_MASK_LOW;
1757 u32 buf_len, wrapped, last_byte, magic, buf_start;
1758 u64 mc_addr = (high << 32) | low;
1759 struct log_header *header;
1760 ssize_t size, bytes_end;
1761 const void *end_of_data;
1762 const void *map_addr;
1763 const void *end_addr;
1764 const void *cur_ptr;
1765 const void *buf;
1766
1767 map_addr = map_sysmem(mc_addr + MC_STRUCT_BUFFER_OFFSET,
1768 MC_BUFFER_SIZE);
1769 header = (struct log_header *)map_addr;
1770 last_byte = in_le32(&header->last_byte);
1771 buf_len = in_le32(&header->buf_length);
1772 magic = in_le32(&header->magic_word);
1773 buf_start = in_le32(&header->buf_start);
1774 buf = map_addr + buf_start - MC_OFFSET_DELTA;
1775 end_addr = buf + buf_len;
1776 wrapped = last_byte & LOG_HEADER_FLAG_BUFFER_WRAPAROUND;
1777 end_of_data = buf + LAST_BYTE(last_byte);
1778
1779 if (magic != MAGIC_MC) {
1780 puts("Magic number is not valid\n");
1781 printf("expected = %08x, received = %08x\n", MAGIC_MC, magic);
1782 goto err_magic;
1783 }
1784
1785 if (wrapped && end_of_data != end_addr)
1786 cur_ptr = end_of_data + 1;
1787 else
1788 cur_ptr = buf;
1789
1790 if (cur_ptr <= end_of_data)
1791 size = end_of_data - cur_ptr;
1792 else
1793 size = (end_addr - cur_ptr) + (end_of_data - buf);
1794
1795 bytes_end = end_addr - cur_ptr;
1796 if (size > bytes_end) {
1797 print_k_bytes(cur_ptr, &bytes_end);
1798
Cosmin-Florin Alucheneseiaa280142021-10-07 13:07:44 +03001799 size -= bytes_end;
1800 }
1801
1802 print_k_bytes(buf, &size);
1803
1804err_magic:
1805 unmap_sysmem(map_addr);
1806}
1807
Simon Glassed38aef2020-05-10 11:40:03 -06001808static int do_fsl_mc(struct cmd_tbl *cmdtp, int flag, int argc,
1809 char *const argv[])
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001810{
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301811 int err = 0;
Cosmin-Florin Alucheneseiaa280142021-10-07 13:07:44 +03001812 if (argc < 2)
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301813 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001814
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301815 switch (argv[1][0]) {
1816 case 's': {
1817 char sub_cmd;
Prabhakar Kushwaha122bcfd2015-11-09 16:42:07 +05301818 u64 mc_fw_addr, mc_dpc_addr;
Tom Rini6a5dccc2022-11-16 13:10:41 -05001819#ifdef CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
Prabhakar Kushwaha122bcfd2015-11-09 16:42:07 +05301820 u64 aiop_fw_addr;
1821#endif
Cosmin-Florin Alucheneseiaa280142021-10-07 13:07:44 +03001822 if (argc < 3)
1823 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001824
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301825 sub_cmd = argv[2][0];
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301826
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301827 switch (sub_cmd) {
1828 case 'm':
1829 if (argc < 5)
1830 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001831
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301832 if (get_mc_boot_status() == 0) {
1833 printf("fsl-mc: MC is already booted");
1834 printf("\n");
1835 return err;
1836 }
1837 mc_fw_addr = simple_strtoull(argv[3], NULL, 16);
1838 mc_dpc_addr = simple_strtoull(argv[4], NULL,
1839 16);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301840
1841 if (!mc_init(mc_fw_addr, mc_dpc_addr))
1842 err = mc_init_object();
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301843 break;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001844
Tom Rini6a5dccc2022-11-16 13:10:41 -05001845#ifdef CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301846 case 'a':
1847 if (argc < 4)
1848 goto usage;
1849 if (get_aiop_apply_status() == 0) {
1850 printf("fsl-mc: AIOP FW is already");
1851 printf(" applied\n");
1852 return err;
1853 }
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001854
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301855 aiop_fw_addr = simple_strtoull(argv[3], NULL,
1856 16);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001857
York Suncbe8e1c2016-04-04 11:41:26 -07001858 /* if SoC doesn't have AIOP, err = -ENODEV */
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301859 err = load_mc_aiop_img(aiop_fw_addr);
1860 if (!err)
1861 printf("fsl-mc: AIOP FW applied\n");
1862 break;
1863#endif
1864 default:
1865 printf("Invalid option: %s\n", argv[2]);
1866 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001867
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301868 break;
1869 }
1870 }
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001871 break;
1872
1873 case 'l': {
1874 /* lazyapply */
1875 u64 mc_dpl_addr;
1876
1877 if (argc < 4)
1878 goto usage;
1879
1880 if (get_dpl_apply_status() == 0) {
1881 printf("fsl-mc: DPL already applied\n");
1882 return err;
1883 }
1884
1885 mc_dpl_addr = simple_strtoull(argv[3], NULL, 16);
1886
1887 if (get_mc_boot_status() != 0) {
1888 printf("fsl-mc: Deploying data path layout ..");
1889 printf("ERROR (MC is not booted)\n");
1890 return -ENODEV;
1891 }
1892
1893 /*
1894 * We will do the actual dpaa exit and dpl apply
1895 * later from announce_and_cleanup().
1896 */
1897 mc_lazy_dpl_addr = mc_dpl_addr;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301898 break;
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001899 }
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001900
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301901 case 'a': {
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001902 /* apply */
1903 char sub_cmd;
1904 u64 mc_apply_addr;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001905
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001906 if (argc < 4)
1907 goto usage;
1908
1909 sub_cmd = argv[2][0];
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001910
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001911 switch (sub_cmd) {
1912 case 'd':
1913 case 'D':
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301914 if (get_dpl_apply_status() == 0) {
1915 printf("fsl-mc: DPL already applied\n");
1916 return err;
1917 }
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301918 if (get_mc_boot_status() != 0) {
1919 printf("fsl-mc: Deploying data path layout ..");
1920 printf("ERROR (MC is not booted)\n");
1921 return -ENODEV;
1922 }
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301923
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001924 mc_apply_addr = simple_strtoull(argv[3], NULL, 16);
1925
1926 /* The user wants DPL applied now */
1927 if (!fsl_mc_ldpaa_exit(NULL))
1928 err = mc_apply_dpl(mc_apply_addr);
1929 break;
1930
1931 case 's':
1932 if (!is_dpsparser_supported()) {
1933 printf("fsl-mc: apply spb command .. ");
1934 printf("ERROR: requires at least MC 10.12.0\n");
1935 return err;
Alexander Graf2ebeb442016-11-17 01:02:57 +01001936 }
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001937 if (get_mc_boot_status() != 0) {
1938 printf("fsl-mc: Deploying Soft Parser Blob...");
1939 printf("ERROR (MC is not booted)\n");
1940 return err;
1941 }
1942
1943 mc_apply_addr = simple_strtoull(argv[3], NULL, 16);
1944
1945 /* Apply spb (Soft Parser Blob) */
1946 err = mc_apply_spb(mc_apply_addr);
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301947 break;
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001948
1949 default:
1950 printf("Invalid option: %s\n", argv[2]);
1951 goto usage;
1952 }
1953 break;
J. German Riveraf4fed4b2015-03-20 19:28:18 -07001954 }
Cosmin-Florin Alucheneseiaa280142021-10-07 13:07:44 +03001955 case 'd':
1956 if (argc > 2)
1957 goto usage;
1958
1959 mc_dump_log();
1960 break;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301961 default:
1962 printf("Invalid option: %s\n", argv[1]);
1963 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001964 }
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301965 return err;
1966 usage:
1967 return CMD_RET_USAGE;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001968}
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301969
1970U_BOOT_CMD(
1971 fsl_mc, CONFIG_SYS_MAXARGS, 1, do_fsl_mc,
1972 "DPAA2 command to manage Management Complex (MC)",
1973 "start mc [FW_addr] [DPC_addr] - Start Management Complex\n"
1974 "fsl_mc apply DPL [DPL_addr] - Apply DPL file\n"
Alexander Graf2ebeb442016-11-17 01:02:57 +01001975 "fsl_mc lazyapply DPL [DPL_addr] - Apply DPL file on exit\n"
Florinel Iordacheae6d8522019-11-19 10:28:17 +00001976 "fsl_mc apply spb [spb_addr] - Apply SPB Soft Parser Blob\n"
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301977 "fsl_mc start aiop [FW_addr] - Start AIOP\n"
Cosmin-Florin Alucheneseiaa280142021-10-07 13:07:44 +03001978 "fsl_mc dump_log - Dump MC Log\n"
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301979);
Bogdan Purcareata08bc0142017-05-24 16:40:21 +00001980
1981void mc_env_boot(void)
1982{
1983#if defined(CONFIG_FSL_MC_ENET)
1984 char *mc_boot_env_var;
1985 /* The MC may only be initialized in the reset PHY function
1986 * because otherwise U-Boot has not yet set up all the MAC
1987 * address info properly. Without MAC addresses, the MC code
1988 * can not properly initialize the DPC.
1989 */
Simon Glass64b723f2017-08-03 12:22:12 -06001990 mc_boot_env_var = env_get(MC_BOOT_ENV_VAR);
Bogdan Purcareata08bc0142017-05-24 16:40:21 +00001991 if (mc_boot_env_var)
1992 run_command_list(mc_boot_env_var, -1, 0);
1993#endif /* CONFIG_FSL_MC_ENET */
1994}