blob: d33a39dca9bd114088ea8a3db77e580afecf0726 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
J. German Riverab940ca62014-06-23 15:15:55 -07002/*
Yogesh Gaura6f2a6e2018-05-09 10:52:17 +05303 * Copyright 2014 Freescale Semiconductor, Inc.
Cosmin-Florin Aluchenesei669884e2021-07-21 19:13:33 +03004 * Copyright 2017-2018, 2020-2021 NXP
J. German Riverab940ca62014-06-23 15:15:55 -07005 */
Stuart Yoder21c69872015-07-02 11:29:03 +05306#include <common.h>
Simon Glass288b29e2019-11-14 12:57:43 -07007#include <command.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -07008#include <cpu_func.h>
Simon Glass7b51b572019-08-01 09:46:52 -06009#include <env.h>
J. German Riverab940ca62014-06-23 15:15:55 -070010#include <errno.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060011#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <malloc.h>
Cosmin-Florin Alucheneseied067722021-10-07 13:07:44 +030014#include <mapmem.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
Masahiro Yamada84b8bf62016-01-24 23:27:48 +090016#include <linux/bug.h>
J. German Riverab940ca62014-06-23 15:15:55 -070017#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060018#include <linux/delay.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090019#include <linux/libfdt.h>
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +000020#include <net.h>
Stuart Yoder21c69872015-07-02 11:29:03 +053021#include <fdt_support.h>
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080022#include <fsl-mc/fsl_mc.h>
23#include <fsl-mc/fsl_mc_sys.h>
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070024#include <fsl-mc/fsl_mc_private.h>
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080025#include <fsl-mc/fsl_dpmng.h>
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070026#include <fsl-mc/fsl_dprc.h>
27#include <fsl-mc/fsl_dpio.h>
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053028#include <fsl-mc/fsl_dpni.h>
Florinel Iordache1990cc72019-11-19 10:28:17 +000029#include <fsl-mc/fsl_dpsparser.h>
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070030#include <fsl-mc/fsl_qbman_portal.h>
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053031#include <fsl-mc/ldpaa_wriop.h>
J. German Riverab940ca62014-06-23 15:15:55 -070032
J. German Rivera125e2bc2015-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 Purcareata33a89912017-05-24 16:40:21 +000039#define MC_BOOT_ENV_VAR "mcinitcmd"
Meenakshi Aggarwal43ad41e2019-02-27 14:41:02 +053040#define MC_DRAM_BLOCK_DEFAULT_SIZE (512UL * 1024 * 1024)
J. German Rivera125e2bc2015-03-20 19:28:18 -070041
Cosmin-Florin Alucheneseied067722021-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 Riverab940ca62014-06-23 15:15:55 -070052DECLARE_GLOBAL_DATA_PTR;
Prabhakar Kushwaha87519a92018-08-27 12:58:52 +053053static int mc_memset_resv_ram;
Florinel Iordache1990cc72019-11-19 10:28:17 +000054static struct mc_version mc_ver_info;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053055static int mc_boot_status = -1;
56static int mc_dpl_applied = -1;
Tom Rini65cc0e22022-11-16 13:10:41 -050057#ifdef CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053058static int mc_aiop_applied = -1;
59#endif
Prabhakar Kushwaha1730a172015-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 Kushwahaa2a55e52015-03-19 09:20:45 -070063uint16_t dflt_dprc_handle = 0;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +053064int child_dprc_id;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070065struct fsl_dpbp_obj *dflt_dpbp = NULL;
66struct fsl_dpio_obj *dflt_dpio = NULL;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +053067struct fsl_dpni_obj *dflt_dpni = NULL;
Alexander Grafb7b84102016-11-17 01:02:57 +010068static u64 mc_lazy_dpl_addr;
Florinel Iordache1990cc72019-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 Riverab940ca62014-06-23 15:15:55 -070072
J. German Rivera125e2bc2015-03-20 19:28:18 -070073#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}
85
86void 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 Riverab940ca62014-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 Rivera7b3bd9a2015-01-06 13:19:02 -0800119 u64 image_addr, u32 image_size, u64 mc_ram_addr)
J. German Riverab940ca62014-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 Rivera125e2bc2015-03-20 19:28:18 -0700123 flush_dcache_range(mc_ram_addr, mc_ram_addr + image_size);
J. German Riverab940ca62014-06-23 15:15:55 -0700124 return 0;
125}
126
Florinel Iordache1990cc72019-11-19 10:28:17 +0000127#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Riverab940ca62014-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 Rivera7b3bd9a2015-01-06 13:19:02 -0800132 * Returns 0 on success and a negative errno on error.
J. German Riverab940ca62014-06-23 15:15:55 -0700133 * task fail.
134 **/
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530135int parse_mc_firmware_fit_image(u64 mc_fw_addr,
136 const void **raw_image_addr,
J. German Riverab940ca62014-06-23 15:15:55 -0700137 size_t *raw_image_size)
138{
139 int format;
Sean Andersonf1061c52022-08-16 11:16:06 -0400140 void *fit_hdr = (void *)mc_fw_addr;
J. German Riverab940ca62014-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 Kushwahafb4a87a2015-11-04 12:25:58 +0530146 printf("fsl-mc: ERR: Bad firmware image (not a FIT image)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800147 return -EINVAL;
J. German Riverab940ca62014-06-23 15:15:55 -0700148 }
149
Simon Glassc5819702021-02-15 17:08:09 -0700150 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530151 printf("fsl-mc: ERR: Bad firmware image (bad FIT header)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800152 return -EINVAL;
J. German Riverab940ca62014-06-23 15:15:55 -0700153 }
154
Sean Andersonf1061c52022-08-16 11:16:06 -0400155 return fit_get_data_node(fit_hdr, "firmware", raw_image_addr,
156 raw_image_size);
J. German Riverab940ca62014-06-23 15:15:55 -0700157}
J. German Rivera125e2bc2015-03-20 19:28:18 -0700158#endif
159
Bogdan Purcareata1161dbc2017-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 Ciornei988e33f2020-03-18 16:47:38 +0200168 const char *propname, struct udevice *eth_dev,
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000169 enum mc_fixup_type type)
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000170{
Simon Glassc69cda22020-12-03 16:55:20 -0700171 struct eth_pdata *plat = dev_get_plat(eth_dev);
Ioana Ciornei988e33f2020-03-18 16:47:38 +0200172 unsigned char *enetaddr = plat->enetaddr;
Simon Glass8b85dfc2020-12-16 21:20:07 -0700173 int eth_index = dev_seq(eth_dev);
Bogdan Purcareata1161dbc2017-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 Ciornei988e33f2020-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 Purcareata1161dbc2017-05-24 16:40:22 +0000187 case MC_FIXUP_DPC:
Ioana Ciornei988e33f2020-03-18 16:47:38 +0200188 val = enetaddr;
189 len = ARP_HLEN;
190 break;
Bogdan Purcareata1161dbc2017-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 Ciornei988e33f2020-03-18 16:47:38 +0200196 if (!eth_env_get_enetaddr_by_index("eth", eth_index,
Simon Glass35affd72017-08-03 12:22:14 -0600197 env_enetaddr))
Bogdan Purcareata1161dbc2017-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 Ciornei988e33f2020-03-18 16:47:38 +0200246 struct udevice *eth_dev)
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000247{
248 int objoff = fdt_path_offset(blob, "/objects");
249 int dpmacoff = -1, dpnioff = -1;
250 const char *endpoint;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000251 char mac_name[10];
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000252 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 Aggarwalcf0bbbd2019-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 Guptaa78df402018-08-20 16:01:14 +0530295void fdt_fsl_mc_fixup_iommu_map_entry(void *blob)
296{
297 u32 *prop;
Laurentiu Tudor21a00d12020-05-04 14:47:11 +0300298 u32 iommu_map[4], phandle;
Nipun Guptaa78df402018-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 Tudor21a00d12020-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 Guptaa78df402018-08-20 16:01:14 +0530342}
343
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000344static int mc_fixup_dpc_mac_addr(void *blob, int dpmac_id,
Ioana Ciornei988e33f2020-03-18 16:47:38 +0200345 struct udevice *eth_dev)
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000346{
347 int nodeoffset = fdt_path_offset(blob, "/board_info/ports"), noff;
348 int err = 0;
349 char mac_name[10];
350 const char link_type_mode[] = "MAC_LINK_TYPE_FIXED";
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000351
352 sprintf(mac_name, "mac@%d", dpmac_id);
353
354 /* node not found - create it */
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000355 noff = fdt_subnode_offset(blob, nodeoffset, (const char *)mac_name);
356 if (noff < 0) {
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000357 err = fdt_increase_size(blob, 200);
358 if (err) {
Ioana Ciorneif45ed0b2023-01-05 17:03:20 +0200359 printf("fdt_increase_size: err=%s\n", fdt_strerror(err));
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000360 return err;
361 }
362
Bogdan Purcareata1161dbc2017-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 Purcareata5707dfb2017-01-11 15:58:36 +0000369
370 /* add default property of fixed link */
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000371 err = fdt_appendprop_string(blob, noff,
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000372 "link_type", link_type_mode);
373 if (err) {
374 printf("fdt_appendprop_string: err=%s\n",
Ioana Ciorneif45ed0b2023-01-05 17:03:20 +0200375 fdt_strerror(err));
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000376 return err;
377 }
378 }
379
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000380 return mc_fixup_mac_addr(blob, noff, "port_mac_address", eth_dev,
381 MC_FIXUP_DPC);
382}
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000383
Bogdan Purcareata1161dbc2017-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 Ciornei988e33f2020-03-18 16:47:38 +0200387#define ETH_NAME_LEN 20
388 struct udevice *eth_dev;
Ioana Ciornei988e33f2020-03-18 16:47:38 +0200389 char ethname[ETH_NAME_LEN];
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000390
391 for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++) {
392 /* port not enabled */
Pankaj Bansal1ed19a12018-10-10 14:08:33 +0530393 if (wriop_is_enabled_dpmac(i) != 1)
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000394 continue;
395
Pankaj Bansalc022ec02018-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 Purcareata1161dbc2017-05-24 16:40:22 +0000398
399 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;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000412 }
413
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000414 if (err)
415 printf("fsl-mc: ERROR fixing mac address for %s\n",
416 ethname);
417 ret |= err;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000418 }
419
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000420 return ret;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000421}
422
Stuart Yoder21c69872015-07-02 11:29:03 +0530423static int mc_fixup_dpc(u64 dpc_addr)
424{
425 void *blob = (void *)dpc_addr;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000426 int nodeoffset, err = 0;
Stuart Yoder21c69872015-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 Purcareata5707dfb2017-01-11 15:58:36 +0000448 /* fixup MAC addresses for dpmac ports */
449 nodeoffset = fdt_path_offset(blob, "/board_info/ports");
Razvan Ionut Cirjand5b0af02020-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 Purcareata5707dfb2017-01-11 15:58:36 +0000463
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000464 err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPC);
Ioana Ciocoi Radulescu2e9f1bf2019-02-26 15:50:07 +0000465
466out:
Stuart Yoder21c69872015-07-02 11:29:03 +0530467 flush_dcache_range(dpc_addr, dpc_addr + fdt_totalsize(blob));
468
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000469 return err;
Stuart Yoder21c69872015-07-02 11:29:03 +0530470}
471
Prabhakar Kushwahafb4a87a2015-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 Rivera125e2bc2015-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 Rini65cc0e22022-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 Rivera125e2bc2015-03-20 19:28:18 -0700484
Tom Rini65cc0e22022-11-16 13:10:41 -0500485 mc_dpc_offset = CFG_SYS_LS_MC_DRAM_DPC_OFFSET;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700486#else
Tom Rini65cc0e22022-11-16 13:10:41 -0500487#error "CFG_SYS_LS_MC_DRAM_DPC_OFFSET not defined"
J. German Rivera125e2bc2015-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 Kushwahafb4a87a2015-11-04 12:25:58 +0530499 dpc_fdt_hdr = (void *)mc_dpc_addr;
J. German Rivera125e2bc2015-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 Riveracc088c32015-07-02 11:28:56 +0530507 printf("\nfsl-mc: WARNING: No DPC image found");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700508 return 0;
509 }
510
511 dpc_size = fdt_totalsize(dpc_fdt_hdr);
Tom Rini65cc0e22022-11-16 13:10:41 -0500512 if (dpc_size > CFG_SYS_LS_MC_DPC_MAX_LENGTH) {
J. German Riveracc088c32015-07-02 11:28:56 +0530513 printf("\nfsl-mc: ERROR: Bad DPC image (too large: %d)\n",
J. German Rivera125e2bc2015-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 Yoder21c69872015-07-02 11:29:03 +0530522 if (mc_fixup_dpc(mc_ram_addr + mc_dpc_offset))
523 return -EINVAL;
524
J. German Rivera125e2bc2015-03-20 19:28:18 -0700525 dump_ram_words("DPC", (void *)(mc_ram_addr + mc_dpc_offset));
526 return 0;
527}
528
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000529
530static int mc_fixup_dpl(u64 dpl_addr)
531{
532 void *blob = (void *)dpl_addr;
533 u32 ver = fdt_getprop_u32_default(blob, "/", "dpl-version", 0);
534 int err = 0;
535
536 /* The DPL fixup for mac addresses is only relevant
537 * for old-style DPLs
538 */
539 if (ver >= 10)
540 return 0;
541
542 err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPL);
543 flush_dcache_range(dpl_addr, dpl_addr + fdt_totalsize(blob));
544
545 return err;
546}
547
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530548static int load_mc_dpl(u64 mc_ram_addr, size_t mc_ram_size, u64 mc_dpl_addr)
J. German Rivera125e2bc2015-03-20 19:28:18 -0700549{
550 u64 mc_dpl_offset;
551#ifndef CONFIG_SYS_LS_MC_DPL_IN_DDR
552 int error;
553 void *dpl_fdt_hdr;
554 int dpl_size;
555#endif
556
Tom Rini65cc0e22022-11-16 13:10:41 -0500557#ifdef CFG_SYS_LS_MC_DRAM_DPL_OFFSET
558 BUILD_BUG_ON((CFG_SYS_LS_MC_DRAM_DPL_OFFSET & 0x3) != 0 ||
559 CFG_SYS_LS_MC_DRAM_DPL_OFFSET > 0xffffffff);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700560
Tom Rini65cc0e22022-11-16 13:10:41 -0500561 mc_dpl_offset = CFG_SYS_LS_MC_DRAM_DPL_OFFSET;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700562#else
Tom Rini65cc0e22022-11-16 13:10:41 -0500563#error "CFG_SYS_LS_MC_DRAM_DPL_OFFSET not defined"
J. German Rivera125e2bc2015-03-20 19:28:18 -0700564#endif
565
566 /*
567 * Load the MC DPL blob in the MC private DRAM block:
568 */
569#ifdef CONFIG_SYS_LS_MC_DPL_IN_DDR
570 printf("MC DPL is preloaded to %#llx\n", mc_ram_addr + mc_dpl_offset);
571#else
572 /*
573 * Get address and size of the DPL blob stored in flash:
574 */
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530575 dpl_fdt_hdr = (void *)mc_dpl_addr;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700576
577 error = fdt_check_header(dpl_fdt_hdr);
578 if (error != 0) {
J. German Riveracc088c32015-07-02 11:28:56 +0530579 printf("\nfsl-mc: ERROR: Bad DPL image (bad header)\n");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700580 return error;
581 }
582
583 dpl_size = fdt_totalsize(dpl_fdt_hdr);
Tom Rini65cc0e22022-11-16 13:10:41 -0500584 if (dpl_size > CFG_SYS_LS_MC_DPL_MAX_LENGTH) {
J. German Riveracc088c32015-07-02 11:28:56 +0530585 printf("\nfsl-mc: ERROR: Bad DPL image (too large: %d)\n",
J. German Rivera125e2bc2015-03-20 19:28:18 -0700586 dpl_size);
587 return -EINVAL;
588 }
589
590 mc_copy_image("MC DPL blob",
591 (u64)dpl_fdt_hdr, dpl_size, mc_ram_addr + mc_dpl_offset);
592#endif /* not defined CONFIG_SYS_LS_MC_DPL_IN_DDR */
593
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000594 if (mc_fixup_dpl(mc_ram_addr + mc_dpl_offset))
595 return -EINVAL;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700596 dump_ram_words("DPL", (void *)(mc_ram_addr + mc_dpl_offset));
597 return 0;
598}
599
600/**
601 * Return the MC boot timeout value in milliseconds
602 */
603static unsigned long get_mc_boot_timeout_ms(void)
604{
Tom Rini65cc0e22022-11-16 13:10:41 -0500605 unsigned long timeout_ms = CFG_SYS_LS_MC_BOOT_TIMEOUT_MS;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700606
Simon Glass00caae62017-08-03 12:22:12 -0600607 char *timeout_ms_env_var = env_get(MC_BOOT_TIMEOUT_ENV_VAR);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700608
609 if (timeout_ms_env_var) {
Simon Glass0b1284e2021-07-24 09:03:30 -0600610 timeout_ms = dectoul(timeout_ms_env_var, NULL);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700611 if (timeout_ms == 0) {
612 printf("fsl-mc: WARNING: Invalid value for \'"
613 MC_BOOT_TIMEOUT_ENV_VAR
614 "\' environment variable: %lu\n",
615 timeout_ms);
616
Tom Rini65cc0e22022-11-16 13:10:41 -0500617 timeout_ms = CFG_SYS_LS_MC_BOOT_TIMEOUT_MS;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700618 }
619 }
620
621 return timeout_ms;
622}
623
Tom Rini65cc0e22022-11-16 13:10:41 -0500624#ifdef CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
York Sun3c1d2182016-04-04 11:41:26 -0700625
626__weak bool soc_has_aiop(void)
627{
628 return false;
629}
630
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530631static int load_mc_aiop_img(u64 aiop_fw_addr)
J. German Riverac1000c12015-07-02 11:28:58 +0530632{
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530633 u64 mc_ram_addr = mc_get_dram_addr();
634#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
J. German Riverac1000c12015-07-02 11:28:58 +0530635 void *aiop_img;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530636#endif
J. German Riverac1000c12015-07-02 11:28:58 +0530637
York Sun3c1d2182016-04-04 11:41:26 -0700638 /* Check if AIOP is available */
639 if (!soc_has_aiop())
640 return -ENODEV;
J. German Riverac1000c12015-07-02 11:28:58 +0530641 /*
642 * Load the MC AIOP image in the MC private DRAM block:
643 */
644
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530645#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
646 printf("MC AIOP is preloaded to %#llx\n", mc_ram_addr +
Tom Rini65cc0e22022-11-16 13:10:41 -0500647 CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530648#else
649 aiop_img = (void *)aiop_fw_addr;
J. German Riverac1000c12015-07-02 11:28:58 +0530650 mc_copy_image("MC AIOP image",
Tom Rini65cc0e22022-11-16 13:10:41 -0500651 (u64)aiop_img, CFG_SYS_LS_MC_AIOP_IMG_MAX_LENGTH,
652 mc_ram_addr + CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530653#endif
654 mc_aiop_applied = 0;
J. German Riverac1000c12015-07-02 11:28:58 +0530655
656 return 0;
657}
658#endif
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530659
J. German Rivera125e2bc2015-03-20 19:28:18 -0700660static int wait_for_mc(bool booting_mc, u32 *final_reg_gsr)
661{
662 u32 reg_gsr;
663 u32 mc_fw_boot_status;
664 unsigned long timeout_ms = get_mc_boot_timeout_ms();
665 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
666
667 dmb();
J. German Rivera125e2bc2015-03-20 19:28:18 -0700668 assert(timeout_ms > 0);
669 for (;;) {
670 udelay(1000); /* throttle polling */
671 reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
672 mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
673 if (mc_fw_boot_status & 0x1)
674 break;
675
676 timeout_ms--;
677 if (timeout_ms == 0)
678 break;
679 }
680
681 if (timeout_ms == 0) {
J. German Riveracc088c32015-07-02 11:28:56 +0530682 printf("ERROR: timeout\n");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700683
684 /* TODO: Get an error status from an MC CCSR register */
685 return -ETIMEDOUT;
686 }
687
688 if (mc_fw_boot_status != 0x1) {
689 /*
690 * TODO: Identify critical errors from the GSR register's FS
691 * field and for those errors, set error to -ENODEV or other
692 * appropriate errno, so that the status property is set to
693 * failure in the fsl,dprc device tree node.
694 */
J. German Riveracc088c32015-07-02 11:28:56 +0530695 printf("WARNING: Firmware returned an error (GSR: %#x)\n",
696 reg_gsr);
697 } else {
698 printf("SUCCESS\n");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700699 }
700
J. German Riveracc088c32015-07-02 11:28:56 +0530701
J. German Rivera125e2bc2015-03-20 19:28:18 -0700702 *final_reg_gsr = reg_gsr;
703 return 0;
704}
J. German Riverab940ca62014-06-23 15:15:55 -0700705
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530706int mc_init(u64 mc_fw_addr, u64 mc_dpc_addr)
J. German Riverab940ca62014-06-23 15:15:55 -0700707{
708 int error = 0;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700709 int portal_id = 0;
J. German Riverab940ca62014-06-23 15:15:55 -0700710 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530711 u64 mc_ram_addr = mc_get_dram_addr();
J. German Riverab940ca62014-06-23 15:15:55 -0700712 u32 reg_gsr;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700713 u32 reg_mcfbalr;
714#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Riverab940ca62014-06-23 15:15:55 -0700715 const void *raw_image_addr;
716 size_t raw_image_size = 0;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700717#endif
J. German Rivera125e2bc2015-03-20 19:28:18 -0700718 u8 mc_ram_num_256mb_blocks;
719 size_t mc_ram_size = mc_get_dram_block_size();
J. German Riverab940ca62014-06-23 15:15:55 -0700720
York Sun437858b62017-03-06 09:02:29 -0800721 mc_ram_num_256mb_blocks = mc_ram_size / MC_RAM_SIZE_ALIGNMENT;
Meenakshi Aggarwal43ad41e2019-02-27 14:41:02 +0530722
723 if (mc_ram_num_256mb_blocks >= 0xff) {
York Sun437858b62017-03-06 09:02:29 -0800724 error = -EINVAL;
725 printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
726 mc_ram_size);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700727 goto out;
York Sun437858b62017-03-06 09:02:29 -0800728 }
J. German Rivera125e2bc2015-03-20 19:28:18 -0700729
J. German Riverab940ca62014-06-23 15:15:55 -0700730 /*
Meenakshi Aggarwal43ad41e2019-02-27 14:41:02 +0530731 * To support 128 MB DDR Size for MC
732 */
733 if (mc_ram_num_256mb_blocks == 0)
734 mc_ram_num_256mb_blocks = 0xFF;
735
736 /*
J. German Riverab940ca62014-06-23 15:15:55 -0700737 * Management Complex cores should be held at reset out of POR.
Bin Menga1875592016-02-05 19:30:11 -0800738 * U-Boot should be the first software to touch MC. To be safe,
J. German Riverab940ca62014-06-23 15:15:55 -0700739 * we reset all cores again by setting GCR1 to 0. It doesn't do
740 * anything if they are held at reset. After we setup the firmware
741 * we kick off MC by deasserting the reset bit for core 0, and
742 * deasserting the reset bits for Command Portal Managers.
743 * The stop bits are not touched here. They are used to stop the
744 * cores when they are active. Setting stop bits doesn't stop the
745 * cores from fetching instructions when they are released from
746 * reset.
747 */
748 out_le32(&mc_ccsr_regs->reg_gcr1, 0);
749 dmb();
750
J. German Rivera125e2bc2015-03-20 19:28:18 -0700751#ifdef CONFIG_SYS_LS_MC_FW_IN_DDR
752 printf("MC firmware is preloaded to %#llx\n", mc_ram_addr);
753#else
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530754 error = parse_mc_firmware_fit_image(mc_fw_addr, &raw_image_addr,
755 &raw_image_size);
J. German Riverab940ca62014-06-23 15:15:55 -0700756 if (error != 0)
757 goto out;
758 /*
759 * Load the MC FW at the beginning of the MC private DRAM block:
760 */
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800761 mc_copy_image("MC Firmware",
762 (u64)raw_image_addr, raw_image_size, mc_ram_addr);
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800763#endif
J. German Rivera125e2bc2015-03-20 19:28:18 -0700764 dump_ram_words("firmware", (void *)mc_ram_addr);
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800765
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530766 error = load_mc_dpc(mc_ram_addr, mc_ram_size, mc_dpc_addr);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700767 if (error != 0)
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800768 goto out;
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800769
J. German Riverab940ca62014-06-23 15:15:55 -0700770 debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700771 dump_mc_ccsr_regs(mc_ccsr_regs);
J. German Riverab940ca62014-06-23 15:15:55 -0700772
773 /*
J. German Rivera125e2bc2015-03-20 19:28:18 -0700774 * Tell MC what is the address range of the DRAM block assigned to it:
J. German Riverab940ca62014-06-23 15:15:55 -0700775 */
Meenakshi Aggarwal43ad41e2019-02-27 14:41:02 +0530776 if (mc_ram_num_256mb_blocks < 0xFF) {
777 reg_mcfbalr = (u32)mc_ram_addr |
778 (mc_ram_num_256mb_blocks - 1);
779 } else {
780 reg_mcfbalr = (u32)mc_ram_addr |
781 (mc_ram_num_256mb_blocks);
782 }
783
J. German Rivera125e2bc2015-03-20 19:28:18 -0700784 out_le32(&mc_ccsr_regs->reg_mcfbalr, reg_mcfbalr);
785 out_le32(&mc_ccsr_regs->reg_mcfbahr,
York Sun437858b62017-03-06 09:02:29 -0800786 (u32)(mc_ram_addr >> 32));
Stuart Yoder39da6442015-07-02 11:29:02 +0530787 out_le32(&mc_ccsr_regs->reg_mcfapr, FSL_BYPASS_AMQ);
J. German Riverab940ca62014-06-23 15:15:55 -0700788
789 /*
J. German Rivera125e2bc2015-03-20 19:28:18 -0700790 * Tell the MC that we want delayed DPL deployment.
J. German Riverab940ca62014-06-23 15:15:55 -0700791 */
J. German Rivera125e2bc2015-03-20 19:28:18 -0700792 out_le32(&mc_ccsr_regs->reg_gsr, 0xDD00);
J. German Riverab940ca62014-06-23 15:15:55 -0700793
J. German Riveracc088c32015-07-02 11:28:56 +0530794 printf("\nfsl-mc: Booting Management Complex ... ");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800795
J. German Riverab940ca62014-06-23 15:15:55 -0700796 /*
797 * Deassert reset and release MC core 0 to run
798 */
799 out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700800 error = wait_for_mc(true, &reg_gsr);
801 if (error != 0)
J. German Riverab940ca62014-06-23 15:15:55 -0700802 goto out;
J. German Riverab940ca62014-06-23 15:15:55 -0700803
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800804 /*
805 * TODO: need to obtain the portal_id for the root container from the
806 * DPL
807 */
808 portal_id = 0;
809
810 /*
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700811 * Initialize the global default MC portal
812 * And check that the MC firmware is responding portal commands:
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800813 */
Ioana Ciornei6dcf5e42023-01-05 17:03:19 +0200814 root_mc_io = calloc(sizeof(struct fsl_mc_io), 1);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530815 if (!root_mc_io) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530816 printf(" No memory: calloc() failed\n");
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700817 return -ENOMEM;
818 }
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800819
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530820 root_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(portal_id);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700821 debug("Checking access to MC portal of root DPRC container (portal_id %d, portal physical addr %p)\n",
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530822 portal_id, root_mc_io->mmio_regs);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700823
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530824 error = mc_get_version(root_mc_io, MC_CMD_NO_FLAGS, &mc_ver_info);
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800825 if (error != 0) {
826 printf("fsl-mc: ERROR: Firmware version check failed (error: %d)\n",
827 error);
828 goto out;
829 }
830
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800831 printf("fsl-mc: Management Complex booted (version: %d.%d.%d, boot status: %#x)\n",
832 mc_ver_info.major, mc_ver_info.minor, mc_ver_info.revision,
J. German Rivera125e2bc2015-03-20 19:28:18 -0700833 reg_gsr & GSR_FS_MASK);
834
J. German Riverab940ca62014-06-23 15:15:55 -0700835out:
836 if (error != 0)
Prabhakar Kushwaha2b7c4a12015-07-02 11:29:01 +0530837 mc_boot_status = error;
J. German Riverab940ca62014-06-23 15:15:55 -0700838 else
839 mc_boot_status = 0;
840
841 return error;
842}
843
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530844int mc_apply_dpl(u64 mc_dpl_addr)
845{
846 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
847 int error = 0;
848 u32 reg_gsr;
849 u64 mc_ram_addr = mc_get_dram_addr();
850 size_t mc_ram_size = mc_get_dram_block_size();
851
Alexander Grafb7b84102016-11-17 01:02:57 +0100852 if (!mc_dpl_addr)
853 return -1;
854
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530855 error = load_mc_dpl(mc_ram_addr, mc_ram_size, mc_dpl_addr);
856 if (error != 0)
857 return error;
858
859 /*
860 * Tell the MC to deploy the DPL:
861 */
862 out_le32(&mc_ccsr_regs->reg_gsr, 0x0);
863 printf("fsl-mc: Deploying data path layout ... ");
864 error = wait_for_mc(false, &reg_gsr);
865
866 if (!error)
867 mc_dpl_applied = 0;
868
869 return error;
870}
871
J. German Riverab940ca62014-06-23 15:15:55 -0700872int get_mc_boot_status(void)
873{
874 return mc_boot_status;
875}
876
Tom Rini65cc0e22022-11-16 13:10:41 -0500877#ifdef CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530878int get_aiop_apply_status(void)
879{
880 return mc_aiop_applied;
881}
882#endif
883
884int get_dpl_apply_status(void)
885{
886 return mc_dpl_applied;
887}
888
Mian Yousaf Kaukab7e968042018-12-18 14:01:17 +0100889int is_lazy_dpl_addr_valid(void)
890{
891 return !!mc_lazy_dpl_addr;
892}
893
Priyanka Jain033c5382017-08-24 16:42:43 +0530894/*
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530895 * Return the MC address of private DRAM block.
Priyanka Jain033c5382017-08-24 16:42:43 +0530896 * As per MC design document, MC initial base address
897 * should be least significant 512MB address of MC private
898 * memory, i.e. address should point to end address masked
899 * with 512MB offset in private DRAM block.
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530900 */
901u64 mc_get_dram_addr(void)
902{
Priyanka Jain033c5382017-08-24 16:42:43 +0530903 size_t mc_ram_size = mc_get_dram_block_size();
904
Prabhakar Kushwaha87519a92018-08-27 12:58:52 +0530905 if (!mc_memset_resv_ram || (get_mc_boot_status() < 0)) {
906 mc_memset_resv_ram = 1;
907 memset((void *)gd->arch.resv_ram, 0, mc_ram_size);
908 }
909
Priyanka Jain033c5382017-08-24 16:42:43 +0530910 return (gd->arch.resv_ram + mc_ram_size - 1) &
911 MC_RAM_BASE_ADDR_ALIGNMENT_MASK;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530912}
913
J. German Riverab940ca62014-06-23 15:15:55 -0700914/**
915 * Return the actual size of the MC private DRAM block.
J. German Riverab940ca62014-06-23 15:15:55 -0700916 */
917unsigned long mc_get_dram_block_size(void)
918{
Tom Rini65cc0e22022-11-16 13:10:41 -0500919 unsigned long dram_block_size = CFG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700920
Simon Glass00caae62017-08-03 12:22:12 -0600921 char *dram_block_size_env_var = env_get(MC_MEM_SIZE_ENV_VAR);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700922
923 if (dram_block_size_env_var) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600924 dram_block_size = hextoul(dram_block_size_env_var, NULL);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700925
Tom Rini65cc0e22022-11-16 13:10:41 -0500926 if (dram_block_size < CFG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE) {
J. German Rivera125e2bc2015-03-20 19:28:18 -0700927 printf("fsl-mc: WARNING: Invalid value for \'"
928 MC_MEM_SIZE_ENV_VAR
929 "\' environment variable: %lu\n",
930 dram_block_size);
931
Meenakshi Aggarwal43ad41e2019-02-27 14:41:02 +0530932 dram_block_size = MC_DRAM_BLOCK_DEFAULT_SIZE;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700933 }
934 }
935
936 return dram_block_size;
J. German Riverab940ca62014-06-23 15:15:55 -0700937}
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700938
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900939int fsl_mc_ldpaa_init(struct bd_info *bis)
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530940{
Prabhakar Kushwahac919ab92015-11-04 12:26:00 +0530941 int i;
942
943 for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++)
Pankaj Bansal1ed19a12018-10-10 14:08:33 +0530944 if (wriop_is_enabled_dpmac(i) == 1)
Prabhakar Kushwahac919ab92015-11-04 12:26:00 +0530945 ldpaa_eth_init(i, wriop_get_enet_if(i));
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530946 return 0;
947}
948
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530949static int dprc_version_check(struct fsl_mc_io *mc_io, uint16_t handle)
950{
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530951 int error;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530952 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530953
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530954 error = dprc_get_api_version(mc_io, 0,
955 &major_ver,
956 &minor_ver);
957 if (error < 0) {
958 printf("dprc_get_api_version() failed: %d\n", error);
959 return error;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530960 }
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530961
962 if (major_ver < DPRC_VER_MAJOR || (major_ver == DPRC_VER_MAJOR &&
963 minor_ver < DPRC_VER_MINOR)) {
964 printf("DPRC version mismatch found %u.%u,",
965 major_ver, minor_ver);
966 printf("supported version is %u.%u\n",
967 DPRC_VER_MAJOR, DPRC_VER_MINOR);
968 }
969
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530970 return error;
971}
972
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530973static int dpio_init(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700974{
975 struct qbman_swp_desc p_des;
976 struct dpio_attr attr;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530977 struct dpio_cfg dpio_cfg;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700978 int err = 0;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530979 uint16_t major_ver, minor_ver;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700980
Ioana Ciornei6dcf5e42023-01-05 17:03:19 +0200981 dflt_dpio = calloc(sizeof(struct fsl_dpio_obj), 1);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700982 if (!dflt_dpio) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530983 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530984 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530985 goto err_calloc;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700986 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530987 dpio_cfg.channel_mode = DPIO_LOCAL_CHANNEL;
988 dpio_cfg.num_priorities = 8;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700989
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530990 err = dpio_create(dflt_mc_io,
991 dflt_dprc_handle,
992 MC_CMD_NO_FLAGS,
993 &dpio_cfg,
994 &dflt_dpio->dpio_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530995 if (err < 0) {
996 printf("dpio_create() failed: %d\n", err);
997 err = -ENODEV;
998 goto err_create;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700999 }
1000
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301001 err = dpio_get_api_version(dflt_mc_io, 0,
1002 &major_ver,
1003 &minor_ver);
1004 if (err < 0) {
1005 printf("dpio_get_api_version() failed: %d\n", err);
1006 goto err_get_api_ver;
1007 }
1008
1009 if (major_ver < DPIO_VER_MAJOR || (major_ver == DPIO_VER_MAJOR &&
1010 minor_ver < DPIO_VER_MINOR)) {
1011 printf("DPRC version mismatch found %u.%u,",
1012 major_ver,
1013 minor_ver);
1014 }
1015
1016 err = dpio_open(dflt_mc_io,
1017 MC_CMD_NO_FLAGS,
1018 dflt_dpio->dpio_id,
1019 &dflt_dpio->dpio_handle);
1020 if (err) {
1021 printf("dpio_open() failed\n");
1022 goto err_open;
1023 }
1024
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301025 memset(&attr, 0, sizeof(struct dpio_attr));
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +05301026 err = dpio_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301027 dflt_dpio->dpio_handle, &attr);
1028 if (err < 0) {
1029 printf("dpio_get_attributes() failed: %d\n", err);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001030 goto err_get_attr;
1031 }
1032
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301033 if (dflt_dpio->dpio_id != attr.id) {
1034 printf("dnpi object id and attribute id are not same\n");
1035 goto err_attr_not_same;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301036 }
1037
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301038#ifdef DEBUG
Ioana Ciornei8ecf1ca2023-01-05 17:03:17 +02001039 printf("Init: DPIO.%d\n", dflt_dpio->dpio_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301040#endif
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301041 err = dpio_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1042 if (err < 0) {
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001043 printf("dpio_enable() failed %d\n", err);
1044 goto err_get_enable;
1045 }
Prabhakar Kushwaha1f1c25c2015-07-02 11:28:59 +05301046 debug("ce_offset=0x%llx, ci_offset=0x%llx, portalid=%d, prios=%d\n",
1047 attr.qbman_portal_ce_offset,
1048 attr.qbman_portal_ci_offset,
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001049 attr.qbman_portal_id,
1050 attr.num_priorities);
1051
Prabhakar Kushwaha1f1c25c2015-07-02 11:28:59 +05301052 p_des.cena_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
1053 + attr.qbman_portal_ce_offset);
1054 p_des.cinh_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
1055 + attr.qbman_portal_ci_offset);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001056
1057 dflt_dpio->sw_portal = qbman_swp_init(&p_des);
1058 if (dflt_dpio->sw_portal == NULL) {
1059 printf("qbman_swp_init() failed\n");
1060 goto err_get_swp_init;
1061 }
1062 return 0;
1063
1064err_get_swp_init:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301065 dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001066err_get_enable:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301067err_get_attr:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301068err_attr_not_same:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301069 dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301070err_open:
1071err_get_api_ver:
1072 dpio_destroy(dflt_mc_io,
1073 dflt_dprc_handle,
1074 MC_CMD_NO_FLAGS,
1075 dflt_dpio->dpio_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301076err_create:
Prabhakar Kushwahacd7b3fb2016-03-18 16:15:29 +05301077 free(dflt_dpio);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301078err_calloc:
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001079 return err;
1080}
1081
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301082static int dpio_exit(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001083{
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301084 int err;
1085
1086 err = dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1087 if (err < 0) {
1088 printf("dpio_disable() failed: %d\n", err);
1089 goto err;
1090 }
1091
Cosmin-Florin Aluchenesei669884e2021-07-21 19:13:33 +03001092 err = dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301093 if (err < 0) {
1094 printf("dpio_close() failed: %d\n", err);
1095 goto err;
1096 }
1097
1098 err = dpio_destroy(dflt_mc_io,
1099 dflt_dprc_handle,
1100 MC_CMD_NO_FLAGS,
1101 dflt_dpio->dpio_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301102 if (err < 0) {
1103 printf("dpio_destroy() failed: %d\n", err);
1104 goto err;
1105 }
1106
1107#ifdef DEBUG
Ioana Ciornei8ecf1ca2023-01-05 17:03:17 +02001108 printf("Exit: DPIO.%d\n", dflt_dpio->dpio_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301109#endif
1110
1111 if (dflt_dpio)
1112 free(dflt_dpio);
1113
1114 return 0;
1115err:
1116 return err;
1117}
1118
1119static int dprc_init(void)
1120{
1121 int err, child_portal_id, container_id;
1122 struct dprc_cfg cfg;
1123 uint64_t mc_portal_offset;
1124
1125 /* Open root container */
1126 err = dprc_get_container_id(root_mc_io, MC_CMD_NO_FLAGS, &container_id);
1127 if (err < 0) {
1128 printf("dprc_get_container_id(): Root failed: %d\n", err);
1129 goto err_root_container_id;
1130 }
1131
1132#ifdef DEBUG
1133 printf("Root container id = %d\n", container_id);
1134#endif
1135 err = dprc_open(root_mc_io, MC_CMD_NO_FLAGS, container_id,
1136 &root_dprc_handle);
1137 if (err < 0) {
1138 printf("dprc_open(): Root Container failed: %d\n", err);
1139 goto err_root_open;
1140 }
1141
1142 if (!root_dprc_handle) {
1143 printf("dprc_open(): Root Container Handle is not valid\n");
1144 goto err_root_open;
1145 }
1146
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301147 err = dprc_version_check(root_mc_io, root_dprc_handle);
1148 if (err < 0) {
1149 printf("dprc_version_check() failed: %d\n", err);
1150 goto err_root_open;
1151 }
1152
Prabhakar Kushwaha5373b202016-01-20 12:04:19 +05301153 memset(&cfg, 0, sizeof(struct dprc_cfg));
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301154 cfg.options = DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED |
1155 DPRC_CFG_OPT_OBJ_CREATE_ALLOWED |
1156 DPRC_CFG_OPT_ALLOC_ALLOWED;
1157 cfg.icid = DPRC_GET_ICID_FROM_POOL;
Prabhakar Kushwaha335b1932015-12-24 15:33:49 +05301158 cfg.portal_id = DPRC_GET_PORTAL_ID_FROM_POOL;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301159 err = dprc_create_container(root_mc_io, MC_CMD_NO_FLAGS,
Ioana Ciorneif45ed0b2023-01-05 17:03:20 +02001160 root_dprc_handle, &cfg,
1161 &child_dprc_id,
1162 &mc_portal_offset);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301163 if (err < 0) {
1164 printf("dprc_create_container() failed: %d\n", err);
1165 goto err_create;
1166 }
1167
Ioana Ciornei6dcf5e42023-01-05 17:03:19 +02001168 dflt_mc_io = calloc(sizeof(struct fsl_mc_io), 1);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301169 if (!dflt_mc_io) {
1170 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301171 printf(" No memory: calloc() failed\n");
1172 goto err_calloc;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301173 }
1174
1175 child_portal_id = MC_PORTAL_OFFSET_TO_PORTAL_ID(mc_portal_offset);
1176 dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(child_portal_id);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301177
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301178#ifdef DEBUG
1179 printf("MC portal of child DPRC container: %d, physical addr %p)\n",
1180 child_dprc_id, dflt_mc_io->mmio_regs);
1181#endif
1182
1183 err = dprc_open(dflt_mc_io, MC_CMD_NO_FLAGS, child_dprc_id,
1184 &dflt_dprc_handle);
1185 if (err < 0) {
1186 printf("dprc_open(): Child container failed: %d\n", err);
1187 goto err_child_open;
1188 }
1189
1190 if (!dflt_dprc_handle) {
1191 printf("dprc_open(): Child container Handle is not valid\n");
1192 goto err_child_open;
1193 }
1194
1195 return 0;
1196err_child_open:
1197 free(dflt_mc_io);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301198err_calloc:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301199 dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1200 root_dprc_handle, child_dprc_id);
1201err_create:
1202 dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1203err_root_open:
1204err_root_container_id:
1205 return err;
1206}
1207
1208static int dprc_exit(void)
1209{
1210 int err;
1211
1212 err = dprc_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dprc_handle);
1213 if (err < 0) {
1214 printf("dprc_close(): Child failed: %d\n", err);
1215 goto err;
1216 }
1217
1218 err = dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1219 root_dprc_handle, child_dprc_id);
1220 if (err < 0) {
1221 printf("dprc_destroy_container() failed: %d\n", err);
1222 goto err;
1223 }
1224
1225 err = dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1226 if (err < 0) {
1227 printf("dprc_close(): Root failed: %d\n", err);
1228 goto err;
1229 }
1230
1231 if (dflt_mc_io)
1232 free(dflt_mc_io);
1233
1234 if (root_mc_io)
1235 free(root_mc_io);
1236
1237 return 0;
1238
1239err:
1240 return err;
1241}
1242
1243static int dpbp_init(void)
1244{
1245 int err;
1246 struct dpbp_attr dpbp_attr;
1247 struct dpbp_cfg dpbp_cfg;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301248 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301249
Ioana Ciornei6dcf5e42023-01-05 17:03:19 +02001250 dflt_dpbp = calloc(sizeof(struct fsl_dpbp_obj), 1);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001251 if (!dflt_dpbp) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301252 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301253 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301254 goto err_calloc;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001255 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301256
1257 dpbp_cfg.options = 512;
1258
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301259 err = dpbp_create(dflt_mc_io,
1260 dflt_dprc_handle,
1261 MC_CMD_NO_FLAGS,
1262 &dpbp_cfg,
1263 &dflt_dpbp->dpbp_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301264
1265 if (err < 0) {
1266 err = -ENODEV;
1267 printf("dpbp_create() failed: %d\n", err);
1268 goto err_create;
1269 }
1270
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301271 err = dpbp_get_api_version(dflt_mc_io, 0,
1272 &major_ver,
1273 &minor_ver);
1274 if (err < 0) {
1275 printf("dpbp_get_api_version() failed: %d\n", err);
1276 goto err_get_api_ver;
1277 }
1278
1279 if (major_ver < DPBP_VER_MAJOR || (major_ver == DPBP_VER_MAJOR &&
1280 minor_ver < DPBP_VER_MINOR)) {
1281 printf("DPBP version mismatch found %u.%u,",
1282 major_ver, minor_ver);
1283 printf("supported version is %u.%u\n",
1284 DPBP_VER_MAJOR, DPBP_VER_MINOR);
1285 }
1286
1287 err = dpbp_open(dflt_mc_io,
1288 MC_CMD_NO_FLAGS,
1289 dflt_dpbp->dpbp_id,
1290 &dflt_dpbp->dpbp_handle);
1291 if (err) {
1292 printf("dpbp_open() failed\n");
1293 goto err_open;
1294 }
1295
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301296 memset(&dpbp_attr, 0, sizeof(struct dpbp_attr));
1297 err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
1298 dflt_dpbp->dpbp_handle,
1299 &dpbp_attr);
1300 if (err < 0) {
1301 printf("dpbp_get_attributes() failed: %d\n", err);
1302 goto err_get_attr;
1303 }
1304
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301305 if (dflt_dpbp->dpbp_id != dpbp_attr.id) {
1306 printf("dpbp object id and attribute id are not same\n");
1307 goto err_attr_not_same;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301308 }
1309
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301310#ifdef DEBUG
Ioana Ciornei8ecf1ca2023-01-05 17:03:17 +02001311 printf("Init: DPBP.%d\n", dflt_dpbp->dpbp_attr.id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301312#endif
1313
1314 err = dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
1315 if (err < 0) {
1316 printf("dpbp_close() failed: %d\n", err);
1317 goto err_close;
1318 }
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001319
1320 return 0;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301321
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301322err_get_attr:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301323err_attr_not_same:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301324 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301325 dpbp_destroy(dflt_mc_io,
1326 dflt_dprc_handle,
1327 MC_CMD_NO_FLAGS,
1328 dflt_dpbp->dpbp_id);
1329err_get_api_ver:
1330err_close:
1331err_open:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301332err_create:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301333 free(dflt_dpbp);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301334err_calloc:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301335 return err;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001336}
1337
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301338static int dpbp_exit(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001339{
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301340 int err;
1341
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301342 err = dpbp_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1343 dflt_dpbp->dpbp_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301344 if (err < 0) {
1345 printf("dpbp_destroy() failed: %d\n", err);
1346 goto err;
1347 }
1348
1349#ifdef DEBUG
Ioana Ciornei8ecf1ca2023-01-05 17:03:17 +02001350 printf("Exit: DPBP.%d\n", dflt_dpbp->dpbp_attr.id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301351#endif
1352
1353 if (dflt_dpbp)
1354 free(dflt_dpbp);
1355 return 0;
1356
1357err:
1358 return err;
1359}
1360
1361static int dpni_init(void)
1362{
1363 int err;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301364 uint8_t cfg_buf[256] = {0};
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301365 struct dpni_cfg dpni_cfg;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301366 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301367
Ioana Ciornei6dcf5e42023-01-05 17:03:19 +02001368 dflt_dpni = calloc(sizeof(struct fsl_dpni_obj), 1);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301369 if (!dflt_dpni) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301370 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301371 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301372 goto err_calloc;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301373 }
1374
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301375 memset(&dpni_cfg, 0, sizeof(dpni_cfg));
1376 err = dpni_prepare_cfg(&dpni_cfg, &cfg_buf[0]);
Prabhakar Kushwaha879a59a2015-12-24 15:33:01 +05301377 if (err < 0) {
1378 err = -ENODEV;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301379 printf("dpni_prepare_cfg() failed: %d\n", err);
1380 goto err_prepare_cfg;
Prabhakar Kushwaha879a59a2015-12-24 15:33:01 +05301381 }
1382
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301383 err = dpni_create(dflt_mc_io,
1384 dflt_dprc_handle,
1385 MC_CMD_NO_FLAGS,
1386 &dpni_cfg,
1387 &dflt_dpni->dpni_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301388 if (err < 0) {
1389 err = -ENODEV;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301390 printf("dpni create() failed: %d\n", err);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301391 goto err_create;
1392 }
1393
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301394 err = dpni_get_api_version(dflt_mc_io, 0,
1395 &major_ver,
1396 &minor_ver);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301397 if (err < 0) {
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301398 printf("dpni_get_api_version() failed: %d\n", err);
1399 goto err_get_version;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301400 }
1401
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301402 if (major_ver < DPNI_VER_MAJOR || (major_ver == DPNI_VER_MAJOR &&
1403 minor_ver < DPNI_VER_MINOR)) {
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301404 printf("DPNI version mismatch found %u.%u,",
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301405 major_ver, minor_ver);
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301406 printf("supported version is %u.%u\n",
1407 DPNI_VER_MAJOR, DPNI_VER_MINOR);
1408 }
1409
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301410 err = dpni_open(dflt_mc_io,
1411 MC_CMD_NO_FLAGS,
1412 dflt_dpni->dpni_id,
1413 &dflt_dpni->dpni_handle);
1414 if (err) {
1415 printf("dpni_open() failed\n");
1416 goto err_open;
1417 }
1418
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301419#ifdef DEBUG
Ioana Ciornei8ecf1ca2023-01-05 17:03:17 +02001420 printf("Init: DPNI.%d\n", dflt_dpni->dpni_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301421#endif
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301422 err = dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
1423 if (err < 0) {
1424 printf("dpni_close() failed: %d\n", err);
1425 goto err_close;
1426 }
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001427
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301428 return 0;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301429
1430err_close:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301431 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301432err_open:
1433err_get_version:
1434 dpni_destroy(dflt_mc_io,
1435 dflt_dprc_handle,
1436 MC_CMD_NO_FLAGS,
1437 dflt_dpni->dpni_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301438err_create:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301439err_prepare_cfg:
Prabhakar Kushwaha879a59a2015-12-24 15:33:01 +05301440 free(dflt_dpni);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301441err_calloc:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301442 return err;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001443}
1444
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301445static int dpni_exit(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001446{
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301447 int err;
1448
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301449 err = dpni_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1450 dflt_dpni->dpni_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301451 if (err < 0) {
1452 printf("dpni_destroy() failed: %d\n", err);
1453 goto err;
1454 }
1455
1456#ifdef DEBUG
Ioana Ciornei8ecf1ca2023-01-05 17:03:17 +02001457 printf("Exit: DPNI.%d\n", dflt_dpni->dpni_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301458#endif
1459
1460 if (dflt_dpni)
1461 free(dflt_dpni);
1462 return 0;
1463
1464err:
1465 return err;
1466}
1467
Florinel Iordache1990cc72019-11-19 10:28:17 +00001468static bool is_dpsparser_supported(void)
1469{
1470 /* dpsparser support was first introduced in MC version: 10.12.0 */
1471 if (mc_ver_info.major < 10)
1472 return false;
1473 if (mc_ver_info.major == 10)
1474 return (mc_ver_info.minor >= 12);
1475 return true;
1476}
1477
1478static int dpsparser_version_check(struct fsl_mc_io *mc_io)
1479{
1480 int error;
1481 u16 major_ver, minor_ver;
1482
1483 if (!is_dpsparser_supported())
1484 return 0;
1485
1486 error = dpsparser_get_api_version(mc_io, 0,
1487 &major_ver,
1488 &minor_ver);
1489 if (error < 0) {
1490 printf("dpsparser_get_api_version() failed: %d\n", error);
1491 return error;
1492 }
1493
1494 if (major_ver < DPSPARSER_VER_MAJOR || (major_ver ==
1495 DPSPARSER_VER_MAJOR && minor_ver < DPSPARSER_VER_MINOR)) {
1496 printf("DPSPARSER version mismatch found %u.%u,",
1497 major_ver, minor_ver);
1498 printf("supported version is %u.%u\n",
1499 DPSPARSER_VER_MAJOR, DPSPARSER_VER_MINOR);
1500 }
1501
1502 return error;
1503}
1504
1505static int dpsparser_init(void)
1506{
1507 int err = 0;
1508
1509 if (!is_dpsparser_supported())
1510 return 0;
1511
1512 err = dpsparser_create(dflt_mc_io,
1513 dflt_dprc_handle,
1514 MC_CMD_NO_FLAGS,
1515 &dpsparser_obj_id);
1516 if (err)
1517 printf("dpsparser_create() failed\n");
1518
1519 err = dpsparser_version_check(dflt_mc_io);
1520 if (err < 0) {
1521 printf("dpsparser_version_check() failed: %d\n", err);
1522 goto err_version_check;
1523 }
1524
1525 err = dpsparser_open(dflt_mc_io,
1526 MC_CMD_NO_FLAGS,
1527 &dpsparser_handle);
1528 if (err < 0) {
1529 printf("dpsparser_open() failed: %d\n", err);
1530 goto err_open;
1531 }
1532
1533 return err;
1534
1535err_open:
1536err_version_check:
1537 dpsparser_destroy(dflt_mc_io,
1538 dflt_dprc_handle,
1539 MC_CMD_NO_FLAGS, dpsparser_obj_id);
1540
1541 return err;
1542}
1543
1544#ifdef DPSPARSER_DESTROY
1545/* TODO: refactoring needed in the future to allow DPSPARSER object destroy
1546 * Workaround: DO NOT destroy DPSPARSER object because it needs to be available
1547 * on Apply DPL
1548 */
1549static int dpsparser_exit(void)
1550{
1551 int err;
1552
1553 if (!is_dpsparser_supported())
1554 return 0;
1555
1556 dpsparser_close(dflt_mc_io, MC_CMD_NO_FLAGS, dpsparser_handle);
1557 if (err < 0) {
1558 printf("dpsparser_close() failed: %d\n", err);
1559 goto err;
1560 }
1561
1562 err = dpsparser_destroy(dflt_mc_io, dflt_dprc_handle,
1563 MC_CMD_NO_FLAGS, dpsparser_obj_id);
1564 if (err < 0) {
1565 printf("dpsparser_destroy() failed: %d\n", err);
1566 goto err;
1567 }
1568 return 0;
1569
1570err:
1571 return err;
1572}
1573#endif
1574
1575int mc_apply_spb(u64 mc_spb_addr)
1576{
1577 int err = 0;
1578 u16 error, err_arr_size;
1579 u64 mc_spb_offset;
1580 u32 spb_size;
1581 struct sp_blob_header *sp_blob;
1582 u64 mc_ram_addr = mc_get_dram_addr();
1583
1584 if (!is_dpsparser_supported())
1585 return 0;
1586
1587 if (!mc_spb_addr) {
1588 printf("fsl-mc: Invalid Blob address\n");
1589 return -1;
1590 }
1591
1592#ifdef CONFIG_MC_DRAM_SPB_OFFSET
1593 mc_spb_offset = CONFIG_MC_DRAM_SPB_OFFSET;
1594#else
1595#error "CONFIG_MC_DRAM_SPB_OFFSET not defined"
1596#endif
1597
1598 // Read blob header and get size of SPB blob
1599 sp_blob = (struct sp_blob_header *)mc_spb_addr;
1600 spb_size = le32_to_cpu(sp_blob->length);
1601 if (spb_size > CONFIG_MC_SPB_MAX_SIZE) {
1602 printf("\nfsl-mc: ERROR: Bad SPB image (too large: %d)\n",
1603 spb_size);
1604 return -EINVAL;
1605 }
1606
1607 mc_copy_image("MC SP Blob", mc_spb_addr, spb_size,
1608 mc_ram_addr + mc_spb_offset);
1609
1610 //Invoke MC command to apply SPB blob
1611 printf("fsl-mc: Applying soft parser blob... ");
1612 err = dpsparser_apply_spb(dflt_mc_io, MC_CMD_NO_FLAGS, dpsparser_handle,
1613 mc_spb_offset, &error);
1614 if (err)
1615 return err;
1616
1617 if (error == 0) {
1618 printf("SUCCESS\n");
1619 } else {
1620 printf("FAILED with error code = %d:\n", error);
1621 err_arr_size = (u16)ARRAY_SIZE(mc_err_msg_apply_spb);
1622
1623 if (error > 0 && error < err_arr_size)
1624 printf(mc_err_msg_apply_spb[error]);
1625 else
1626 printf(MC_ERROR_MSG_SPB_UNKNOWN);
1627 }
1628
1629 return err;
1630}
1631
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301632static int mc_init_object(void)
1633{
1634 int err = 0;
1635
1636 err = dprc_init();
1637 if (err < 0) {
1638 printf("dprc_init() failed: %d\n", err);
1639 goto err;
1640 }
1641
1642 err = dpbp_init();
1643 if (err < 0) {
1644 printf("dpbp_init() failed: %d\n", err);
1645 goto err;
1646 }
1647
1648 err = dpio_init();
1649 if (err < 0) {
1650 printf("dpio_init() failed: %d\n", err);
1651 goto err;
1652 }
1653
1654 err = dpni_init();
1655 if (err < 0) {
1656 printf("dpni_init() failed: %d\n", err);
1657 goto err;
1658 }
1659
Florinel Iordache1990cc72019-11-19 10:28:17 +00001660 err = dpsparser_init();
1661 if (err < 0) {
1662 printf("dpsparser_init() failed: %d\n", err);
1663 goto err;
1664 }
1665
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301666 return 0;
1667err:
1668 return err;
1669}
1670
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09001671int fsl_mc_ldpaa_exit(struct bd_info *bd)
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301672{
1673 int err = 0;
Yogesh Gaur42e81792017-04-27 10:14:16 +05301674 bool is_dpl_apply_status = false;
Santan Kumar06651b92017-06-29 11:19:34 +05301675 bool mc_boot_status = false;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301676
Alexander Grafb7b84102016-11-17 01:02:57 +01001677 if (bd && mc_lazy_dpl_addr && !fsl_mc_ldpaa_exit(NULL)) {
Yogesh Gaurf9747a52018-01-16 10:08:24 +05301678 err = mc_apply_dpl(mc_lazy_dpl_addr);
1679 if (!err)
1680 fdt_fixup_board_enet(working_fdt);
Alexander Grafb7b84102016-11-17 01:02:57 +01001681 mc_lazy_dpl_addr = 0;
1682 }
1683
Santan Kumar06651b92017-06-29 11:19:34 +05301684 if (!get_mc_boot_status())
1685 mc_boot_status = true;
1686
Prabhakar Kushwaha6dedced2016-03-21 14:19:39 +05301687 /* MC is not loaded intentionally, So return success. */
Santan Kumar06651b92017-06-29 11:19:34 +05301688 if (bd && !mc_boot_status)
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301689 return 0;
1690
Yogesh Gaur42e81792017-04-27 10:14:16 +05301691 /* If DPL is deployed, set is_dpl_apply_status as TRUE. */
1692 if (!get_dpl_apply_status())
1693 is_dpl_apply_status = true;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301694
Yogesh Gaur42e81792017-04-27 10:14:16 +05301695 /*
1696 * For case MC is loaded but DPL is not deployed, return success and
1697 * print message on console. Else FDT fix-up code execution hanged.
1698 */
Santan Kumar06651b92017-06-29 11:19:34 +05301699 if (bd && mc_boot_status && !is_dpl_apply_status) {
Yogesh Gaur42e81792017-04-27 10:14:16 +05301700 printf("fsl-mc: DPL not deployed, DPAA2 ethernet not work\n");
Yogesh Gaur73fa2062017-11-28 10:11:14 +05301701 goto mc_obj_cleanup;
Yogesh Gaur42e81792017-04-27 10:14:16 +05301702 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301703
Santan Kumar06651b92017-06-29 11:19:34 +05301704 if (bd && mc_boot_status && is_dpl_apply_status)
1705 return 0;
1706
Yogesh Gaur73fa2062017-11-28 10:11:14 +05301707mc_obj_cleanup:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301708 err = dpbp_exit();
1709 if (err < 0) {
Prabhakar Kushwahaa2a4dc52016-01-20 12:04:37 +05301710 printf("dpbp_exit() failed: %d\n", err);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301711 goto err;
1712 }
1713
1714 err = dpio_exit();
1715 if (err < 0) {
1716 printf("dpio_exit() failed: %d\n", err);
1717 goto err;
1718 }
1719
1720 err = dpni_exit();
1721 if (err < 0) {
1722 printf("dpni_exit() failed: %d\n", err);
1723 goto err;
1724 }
1725
1726 err = dprc_exit();
1727 if (err < 0) {
1728 printf("dprc_exit() failed: %d\n", err);
1729 goto err;
1730 }
1731
1732 return 0;
1733err:
1734 return err;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001735}
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301736
Cosmin-Florin Alucheneseied067722021-10-07 13:07:44 +03001737static void print_k_bytes(const void *buf, ssize_t *size)
1738{
1739 while (*size > 0) {
1740 int count = printf("%s", (char *)buf);
1741
1742 buf += count;
1743 *size -= count;
1744 }
1745}
1746
1747static void mc_dump_log(void)
1748{
1749 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
1750 u64 high = in_le64(&mc_ccsr_regs->reg_mcfbahr) & MC_FW_ADDR_MASK_HIGH;
1751 u64 low = in_le64(&mc_ccsr_regs->reg_mcfbalr) & MC_FW_ADDR_MASK_LOW;
1752 u32 buf_len, wrapped, last_byte, magic, buf_start;
1753 u64 mc_addr = (high << 32) | low;
1754 struct log_header *header;
1755 ssize_t size, bytes_end;
1756 const void *end_of_data;
1757 const void *map_addr;
1758 const void *end_addr;
1759 const void *cur_ptr;
1760 const void *buf;
1761
1762 map_addr = map_sysmem(mc_addr + MC_STRUCT_BUFFER_OFFSET,
1763 MC_BUFFER_SIZE);
1764 header = (struct log_header *)map_addr;
1765 last_byte = in_le32(&header->last_byte);
1766 buf_len = in_le32(&header->buf_length);
1767 magic = in_le32(&header->magic_word);
1768 buf_start = in_le32(&header->buf_start);
1769 buf = map_addr + buf_start - MC_OFFSET_DELTA;
1770 end_addr = buf + buf_len;
1771 wrapped = last_byte & LOG_HEADER_FLAG_BUFFER_WRAPAROUND;
1772 end_of_data = buf + LAST_BYTE(last_byte);
1773
1774 if (magic != MAGIC_MC) {
1775 puts("Magic number is not valid\n");
1776 printf("expected = %08x, received = %08x\n", MAGIC_MC, magic);
1777 goto err_magic;
1778 }
1779
1780 if (wrapped && end_of_data != end_addr)
1781 cur_ptr = end_of_data + 1;
1782 else
1783 cur_ptr = buf;
1784
1785 if (cur_ptr <= end_of_data)
1786 size = end_of_data - cur_ptr;
1787 else
1788 size = (end_addr - cur_ptr) + (end_of_data - buf);
1789
1790 bytes_end = end_addr - cur_ptr;
1791 if (size > bytes_end) {
1792 print_k_bytes(cur_ptr, &bytes_end);
1793
Cosmin-Florin Alucheneseied067722021-10-07 13:07:44 +03001794 size -= bytes_end;
1795 }
1796
1797 print_k_bytes(buf, &size);
1798
1799err_magic:
1800 unmap_sysmem(map_addr);
1801}
1802
Simon Glass09140112020-05-10 11:40:03 -06001803static int do_fsl_mc(struct cmd_tbl *cmdtp, int flag, int argc,
1804 char *const argv[])
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301805{
1806 int err = 0;
Cosmin-Florin Alucheneseied067722021-10-07 13:07:44 +03001807 if (argc < 2)
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301808 goto usage;
1809
1810 switch (argv[1][0]) {
1811 case 's': {
1812 char sub_cmd;
Prabhakar Kushwaha44937212015-11-09 16:42:07 +05301813 u64 mc_fw_addr, mc_dpc_addr;
Tom Rini65cc0e22022-11-16 13:10:41 -05001814#ifdef CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
Prabhakar Kushwaha44937212015-11-09 16:42:07 +05301815 u64 aiop_fw_addr;
1816#endif
Cosmin-Florin Alucheneseied067722021-10-07 13:07:44 +03001817 if (argc < 3)
1818 goto usage;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301819
1820 sub_cmd = argv[2][0];
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301821
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301822 switch (sub_cmd) {
1823 case 'm':
1824 if (argc < 5)
1825 goto usage;
1826
1827 if (get_mc_boot_status() == 0) {
1828 printf("fsl-mc: MC is already booted");
1829 printf("\n");
1830 return err;
1831 }
1832 mc_fw_addr = simple_strtoull(argv[3], NULL, 16);
1833 mc_dpc_addr = simple_strtoull(argv[4], NULL,
1834 16);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301835
1836 if (!mc_init(mc_fw_addr, mc_dpc_addr))
1837 err = mc_init_object();
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301838 break;
1839
Tom Rini65cc0e22022-11-16 13:10:41 -05001840#ifdef CFG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301841 case 'a':
1842 if (argc < 4)
1843 goto usage;
1844 if (get_aiop_apply_status() == 0) {
1845 printf("fsl-mc: AIOP FW is already");
1846 printf(" applied\n");
1847 return err;
1848 }
1849
1850 aiop_fw_addr = simple_strtoull(argv[3], NULL,
1851 16);
1852
York Sun3c1d2182016-04-04 11:41:26 -07001853 /* if SoC doesn't have AIOP, err = -ENODEV */
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301854 err = load_mc_aiop_img(aiop_fw_addr);
1855 if (!err)
1856 printf("fsl-mc: AIOP FW applied\n");
1857 break;
1858#endif
1859 default:
1860 printf("Invalid option: %s\n", argv[2]);
1861 goto usage;
1862
1863 break;
1864 }
1865 }
1866 break;
1867
Florinel Iordache1990cc72019-11-19 10:28:17 +00001868 case 'l': {
1869 /* lazyapply */
1870 u64 mc_dpl_addr;
1871
1872 if (argc < 4)
1873 goto usage;
1874
1875 if (get_dpl_apply_status() == 0) {
1876 printf("fsl-mc: DPL already applied\n");
1877 return err;
1878 }
1879
1880 mc_dpl_addr = simple_strtoull(argv[3], NULL, 16);
1881
1882 if (get_mc_boot_status() != 0) {
1883 printf("fsl-mc: Deploying data path layout ..");
1884 printf("ERROR (MC is not booted)\n");
1885 return -ENODEV;
1886 }
1887
1888 /*
1889 * We will do the actual dpaa exit and dpl apply
1890 * later from announce_and_cleanup().
1891 */
1892 mc_lazy_dpl_addr = mc_dpl_addr;
1893 break;
1894 }
1895
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301896 case 'a': {
Florinel Iordache1990cc72019-11-19 10:28:17 +00001897 /* apply */
1898 char sub_cmd;
1899 u64 mc_apply_addr;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301900
Florinel Iordache1990cc72019-11-19 10:28:17 +00001901 if (argc < 4)
1902 goto usage;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301903
Florinel Iordache1990cc72019-11-19 10:28:17 +00001904 sub_cmd = argv[2][0];
1905
1906 switch (sub_cmd) {
1907 case 'd':
1908 case 'D':
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301909 if (get_dpl_apply_status() == 0) {
1910 printf("fsl-mc: DPL already applied\n");
1911 return err;
1912 }
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301913 if (get_mc_boot_status() != 0) {
1914 printf("fsl-mc: Deploying data path layout ..");
1915 printf("ERROR (MC is not booted)\n");
1916 return -ENODEV;
1917 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301918
Florinel Iordache1990cc72019-11-19 10:28:17 +00001919 mc_apply_addr = simple_strtoull(argv[3], NULL, 16);
1920
1921 /* The user wants DPL applied now */
1922 if (!fsl_mc_ldpaa_exit(NULL))
1923 err = mc_apply_dpl(mc_apply_addr);
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301924 break;
Florinel Iordache1990cc72019-11-19 10:28:17 +00001925
1926 case 's':
1927 if (!is_dpsparser_supported()) {
1928 printf("fsl-mc: apply spb command .. ");
1929 printf("ERROR: requires at least MC 10.12.0\n");
1930 return err;
1931 }
1932 if (get_mc_boot_status() != 0) {
1933 printf("fsl-mc: Deploying Soft Parser Blob...");
1934 printf("ERROR (MC is not booted)\n");
1935 return err;
1936 }
1937
1938 mc_apply_addr = simple_strtoull(argv[3], NULL, 16);
1939
1940 /* Apply spb (Soft Parser Blob) */
1941 err = mc_apply_spb(mc_apply_addr);
1942 break;
1943
1944 default:
1945 printf("Invalid option: %s\n", argv[2]);
1946 goto usage;
1947 }
1948 break;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301949 }
Cosmin-Florin Alucheneseied067722021-10-07 13:07:44 +03001950 case 'd':
1951 if (argc > 2)
1952 goto usage;
1953
1954 mc_dump_log();
1955 break;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301956 default:
1957 printf("Invalid option: %s\n", argv[1]);
1958 goto usage;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301959 }
1960 return err;
1961 usage:
1962 return CMD_RET_USAGE;
1963}
1964
1965U_BOOT_CMD(
1966 fsl_mc, CONFIG_SYS_MAXARGS, 1, do_fsl_mc,
1967 "DPAA2 command to manage Management Complex (MC)",
1968 "start mc [FW_addr] [DPC_addr] - Start Management Complex\n"
1969 "fsl_mc apply DPL [DPL_addr] - Apply DPL file\n"
Alexander Grafb7b84102016-11-17 01:02:57 +01001970 "fsl_mc lazyapply DPL [DPL_addr] - Apply DPL file on exit\n"
Florinel Iordache1990cc72019-11-19 10:28:17 +00001971 "fsl_mc apply spb [spb_addr] - Apply SPB Soft Parser Blob\n"
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301972 "fsl_mc start aiop [FW_addr] - Start AIOP\n"
Cosmin-Florin Alucheneseied067722021-10-07 13:07:44 +03001973 "fsl_mc dump_log - Dump MC Log\n"
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301974);
Bogdan Purcareata33a89912017-05-24 16:40:21 +00001975
1976void mc_env_boot(void)
1977{
1978#if defined(CONFIG_FSL_MC_ENET)
1979 char *mc_boot_env_var;
1980 /* The MC may only be initialized in the reset PHY function
1981 * because otherwise U-Boot has not yet set up all the MAC
1982 * address info properly. Without MAC addresses, the MC code
1983 * can not properly initialize the DPC.
1984 */
Simon Glass00caae62017-08-03 12:22:12 -06001985 mc_boot_env_var = env_get(MC_BOOT_ENV_VAR);
Bogdan Purcareata33a89912017-05-24 16:40:21 +00001986 if (mc_boot_env_var)
1987 run_command_list(mc_boot_env_var, -1, 0);
1988#endif /* CONFIG_FSL_MC_ENET */
1989}