blob: 1d96e4bdc2f112c1e47a361925f9401291ad3660 [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.
4 * Copyright 2017 NXP
Prabhakar Kushwaha87519a92018-08-27 12:58:52 +05305 * Copyright 2017-2018 NXP
J. German Riverab940ca62014-06-23 15:15:55 -07006 */
Stuart Yoder21c69872015-07-02 11:29:03 +05307#include <common.h>
Simon Glass7b51b572019-08-01 09:46:52 -06008#include <env.h>
J. German Riverab940ca62014-06-23 15:15:55 -07009#include <errno.h>
Masahiro Yamada84b8bf62016-01-24 23:27:48 +090010#include <linux/bug.h>
J. German Riverab940ca62014-06-23 15:15:55 -070011#include <asm/io.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090012#include <linux/libfdt.h>
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +000013#include <net.h>
Stuart Yoder21c69872015-07-02 11:29:03 +053014#include <fdt_support.h>
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080015#include <fsl-mc/fsl_mc.h>
16#include <fsl-mc/fsl_mc_sys.h>
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070017#include <fsl-mc/fsl_mc_private.h>
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080018#include <fsl-mc/fsl_dpmng.h>
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070019#include <fsl-mc/fsl_dprc.h>
20#include <fsl-mc/fsl_dpio.h>
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053021#include <fsl-mc/fsl_dpni.h>
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070022#include <fsl-mc/fsl_qbman_portal.h>
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053023#include <fsl-mc/ldpaa_wriop.h>
J. German Riverab940ca62014-06-23 15:15:55 -070024
J. German Rivera125e2bc2015-03-20 19:28:18 -070025#define MC_RAM_BASE_ADDR_ALIGNMENT (512UL * 1024 * 1024)
26#define MC_RAM_BASE_ADDR_ALIGNMENT_MASK (~(MC_RAM_BASE_ADDR_ALIGNMENT - 1))
27#define MC_RAM_SIZE_ALIGNMENT (256UL * 1024 * 1024)
28
29#define MC_MEM_SIZE_ENV_VAR "mcmemsize"
30#define MC_BOOT_TIMEOUT_ENV_VAR "mcboottimeout"
Bogdan Purcareata33a89912017-05-24 16:40:21 +000031#define MC_BOOT_ENV_VAR "mcinitcmd"
Meenakshi Aggarwal43ad41e2019-02-27 14:41:02 +053032#define MC_DRAM_BLOCK_DEFAULT_SIZE (512UL * 1024 * 1024)
J. German Rivera125e2bc2015-03-20 19:28:18 -070033
J. German Riverab940ca62014-06-23 15:15:55 -070034DECLARE_GLOBAL_DATA_PTR;
Prabhakar Kushwaha87519a92018-08-27 12:58:52 +053035static int mc_memset_resv_ram;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053036static int mc_boot_status = -1;
37static int mc_dpl_applied = -1;
38#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
39static int mc_aiop_applied = -1;
40#endif
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +053041struct fsl_mc_io *root_mc_io = NULL;
42struct fsl_mc_io *dflt_mc_io = NULL; /* child container */
43uint16_t root_dprc_handle = 0;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070044uint16_t dflt_dprc_handle = 0;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +053045int child_dprc_id;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070046struct fsl_dpbp_obj *dflt_dpbp = NULL;
47struct fsl_dpio_obj *dflt_dpio = NULL;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +053048struct fsl_dpni_obj *dflt_dpni = NULL;
Alexander Grafb7b84102016-11-17 01:02:57 +010049static u64 mc_lazy_dpl_addr;
J. German Riverab940ca62014-06-23 15:15:55 -070050
J. German Rivera125e2bc2015-03-20 19:28:18 -070051#ifdef DEBUG
52void dump_ram_words(const char *title, void *addr)
53{
54 int i;
55 uint32_t *words = addr;
56
57 printf("Dumping beginning of %s (%p):\n", title, addr);
58 for (i = 0; i < 16; i++)
59 printf("%#x ", words[i]);
60
61 printf("\n");
62}
63
64void dump_mc_ccsr_regs(struct mc_ccsr_registers __iomem *mc_ccsr_regs)
65{
66 printf("MC CCSR registers:\n"
67 "reg_gcr1 %#x\n"
68 "reg_gsr %#x\n"
69 "reg_sicbalr %#x\n"
70 "reg_sicbahr %#x\n"
71 "reg_sicapr %#x\n"
72 "reg_mcfbalr %#x\n"
73 "reg_mcfbahr %#x\n"
74 "reg_mcfapr %#x\n"
75 "reg_psr %#x\n",
76 mc_ccsr_regs->reg_gcr1,
77 mc_ccsr_regs->reg_gsr,
78 mc_ccsr_regs->reg_sicbalr,
79 mc_ccsr_regs->reg_sicbahr,
80 mc_ccsr_regs->reg_sicapr,
81 mc_ccsr_regs->reg_mcfbalr,
82 mc_ccsr_regs->reg_mcfbahr,
83 mc_ccsr_regs->reg_mcfapr,
84 mc_ccsr_regs->reg_psr);
85}
86#else
87
88#define dump_ram_words(title, addr)
89#define dump_mc_ccsr_regs(mc_ccsr_regs)
90
91#endif /* DEBUG */
92
93#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Riverab940ca62014-06-23 15:15:55 -070094/**
95 * Copying MC firmware or DPL image to DDR
96 */
97static int mc_copy_image(const char *title,
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080098 u64 image_addr, u32 image_size, u64 mc_ram_addr)
J. German Riverab940ca62014-06-23 15:15:55 -070099{
100 debug("%s copied to address %p\n", title, (void *)mc_ram_addr);
101 memcpy((void *)mc_ram_addr, (void *)image_addr, image_size);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700102 flush_dcache_range(mc_ram_addr, mc_ram_addr + image_size);
J. German Riverab940ca62014-06-23 15:15:55 -0700103 return 0;
104}
105
106/**
107 * MC firmware FIT image parser checks if the image is in FIT
108 * format, verifies integrity of the image and calculates
109 * raw image address and size values.
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800110 * Returns 0 on success and a negative errno on error.
J. German Riverab940ca62014-06-23 15:15:55 -0700111 * task fail.
112 **/
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530113int parse_mc_firmware_fit_image(u64 mc_fw_addr,
114 const void **raw_image_addr,
J. German Riverab940ca62014-06-23 15:15:55 -0700115 size_t *raw_image_size)
116{
117 int format;
118 void *fit_hdr;
119 int node_offset;
120 const void *data;
121 size_t size;
122 const char *uname = "firmware";
123
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530124 fit_hdr = (void *)mc_fw_addr;
J. German Riverab940ca62014-06-23 15:15:55 -0700125
126 /* Check if Image is in FIT format */
127 format = genimg_get_format(fit_hdr);
128
129 if (format != IMAGE_FORMAT_FIT) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530130 printf("fsl-mc: ERR: Bad firmware image (not a FIT image)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800131 return -EINVAL;
J. German Riverab940ca62014-06-23 15:15:55 -0700132 }
133
134 if (!fit_check_format(fit_hdr)) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530135 printf("fsl-mc: ERR: Bad firmware image (bad FIT header)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800136 return -EINVAL;
J. German Riverab940ca62014-06-23 15:15:55 -0700137 }
138
139 node_offset = fit_image_get_node(fit_hdr, uname);
140
141 if (node_offset < 0) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530142 printf("fsl-mc: ERR: Bad firmware image (missing subimage)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800143 return -ENOENT;
J. German Riverab940ca62014-06-23 15:15:55 -0700144 }
145
146 /* Verify MC firmware image */
147 if (!(fit_image_verify(fit_hdr, node_offset))) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530148 printf("fsl-mc: ERR: Bad firmware image (bad CRC)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800149 return -EINVAL;
J. German Riverab940ca62014-06-23 15:15:55 -0700150 }
151
152 /* Get address and size of raw image */
153 fit_image_get_data(fit_hdr, node_offset, &data, &size);
154
155 *raw_image_addr = data;
156 *raw_image_size = size;
157
158 return 0;
159}
J. German Rivera125e2bc2015-03-20 19:28:18 -0700160#endif
161
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000162#define MC_DT_INCREASE_SIZE 64
163
164enum mc_fixup_type {
165 MC_FIXUP_DPL,
166 MC_FIXUP_DPC
167};
168
169static int mc_fixup_mac_addr(void *blob, int nodeoffset,
170 const char *propname, struct eth_device *eth_dev,
171 enum mc_fixup_type type)
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000172{
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000173 int err = 0, len = 0, size, i;
174 unsigned char env_enetaddr[ARP_HLEN];
175 unsigned int enetaddr_32[ARP_HLEN];
176 void *val = NULL;
177
178 switch (type) {
179 case MC_FIXUP_DPL:
180 /* DPL likes its addresses on 32 * ARP_HLEN bits */
181 for (i = 0; i < ARP_HLEN; i++)
182 enetaddr_32[i] = cpu_to_fdt32(eth_dev->enetaddr[i]);
183 val = enetaddr_32;
184 len = sizeof(enetaddr_32);
185 break;
186
187 case MC_FIXUP_DPC:
188 val = eth_dev->enetaddr;
189 len = ARP_HLEN;
190 break;
191 }
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 */
Simon Glass35affd72017-08-03 12:22:14 -0600196 if (!eth_env_get_enetaddr_by_index("eth", eth_dev->index,
197 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,
246 struct eth_device *eth_dev)
247{
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
Nipun Guptaa78df402018-08-20 16:01:14 +0530285void fdt_fsl_mc_fixup_iommu_map_entry(void *blob)
286{
287 u32 *prop;
288 u32 iommu_map[4];
289 int offset;
290 int lenp;
291
292 /* find fsl-mc node */
293 offset = fdt_path_offset(blob, "/soc/fsl-mc");
294 if (offset < 0)
295 offset = fdt_path_offset(blob, "/fsl-mc");
296 if (offset < 0) {
297 printf("%s: fsl-mc: ERR: fsl-mc node not found in DT, err %d\n",
298 __func__, offset);
299 return;
300 }
301
302 prop = fdt_getprop_w(blob, offset, "iommu-map", &lenp);
303 if (!prop) {
304 debug("%s: fsl-mc: ERR: missing iommu-map in fsl-mc bus node\n",
305 __func__);
306 return;
307 }
308
309 iommu_map[0] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_START);
310 iommu_map[1] = *++prop;
311 iommu_map[2] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_START);
312 iommu_map[3] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_END -
313 FSL_DPAA2_STREAM_ID_START + 1);
314
315 fdt_setprop_inplace(blob, offset, "iommu-map",
316 iommu_map, sizeof(iommu_map));
317}
318
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000319static int mc_fixup_dpc_mac_addr(void *blob, int dpmac_id,
320 struct eth_device *eth_dev)
321{
322 int nodeoffset = fdt_path_offset(blob, "/board_info/ports"), noff;
323 int err = 0;
324 char mac_name[10];
325 const char link_type_mode[] = "MAC_LINK_TYPE_FIXED";
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000326
327 sprintf(mac_name, "mac@%d", dpmac_id);
328
329 /* node not found - create it */
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000330 noff = fdt_subnode_offset(blob, nodeoffset, (const char *)mac_name);
331 if (noff < 0) {
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000332 err = fdt_increase_size(blob, 200);
333 if (err) {
334 printf("fdt_increase_size: err=%s\n",
335 fdt_strerror(err));
336 return err;
337 }
338
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000339 noff = fdt_add_subnode(blob, nodeoffset, mac_name);
340 if (noff < 0) {
341 printf("fdt_add_subnode: err=%s\n",
342 fdt_strerror(err));
343 return err;
344 }
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000345
346 /* add default property of fixed link */
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000347 err = fdt_appendprop_string(blob, noff,
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000348 "link_type", link_type_mode);
349 if (err) {
350 printf("fdt_appendprop_string: err=%s\n",
351 fdt_strerror(err));
352 return err;
353 }
354 }
355
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000356 return mc_fixup_mac_addr(blob, noff, "port_mac_address", eth_dev,
357 MC_FIXUP_DPC);
358}
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000359
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000360static int mc_fixup_mac_addrs(void *blob, enum mc_fixup_type type)
361{
362 int i, err = 0, ret = 0;
Pankaj Bansalc022ec02018-08-02 16:31:28 +0530363 char ethname[ETH_NAME_LEN];
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000364 struct eth_device *eth_dev;
365
366 for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++) {
367 /* port not enabled */
Pankaj Bansal1ed19a12018-10-10 14:08:33 +0530368 if (wriop_is_enabled_dpmac(i) != 1)
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000369 continue;
370
Pankaj Bansalc022ec02018-08-02 16:31:28 +0530371 snprintf(ethname, ETH_NAME_LEN, "DPMAC%d@%s", i,
372 phy_interface_strings[wriop_get_enet_if(i)]);
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000373
374 eth_dev = eth_get_dev_by_name(ethname);
375 if (eth_dev == NULL)
376 continue;
377
378 switch (type) {
379 case MC_FIXUP_DPL:
380 err = mc_fixup_dpl_mac_addr(blob, i, eth_dev);
381 break;
382 case MC_FIXUP_DPC:
383 err = mc_fixup_dpc_mac_addr(blob, i, eth_dev);
384 break;
385 default:
386 break;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000387 }
388
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000389 if (err)
390 printf("fsl-mc: ERROR fixing mac address for %s\n",
391 ethname);
392 ret |= err;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000393 }
394
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000395 return ret;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000396}
397
Stuart Yoder21c69872015-07-02 11:29:03 +0530398static int mc_fixup_dpc(u64 dpc_addr)
399{
400 void *blob = (void *)dpc_addr;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000401 int nodeoffset, err = 0;
Stuart Yoder21c69872015-07-02 11:29:03 +0530402
403 /* delete any existing ICID pools */
404 nodeoffset = fdt_path_offset(blob, "/resources/icid_pools");
405 if (fdt_del_node(blob, nodeoffset) < 0)
406 printf("\nfsl-mc: WARNING: could not delete ICID pool\n");
407
408 /* add a new pool */
409 nodeoffset = fdt_path_offset(blob, "/resources");
410 if (nodeoffset < 0) {
411 printf("\nfsl-mc: ERROR: DPC is missing /resources\n");
412 return -EINVAL;
413 }
414 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pools");
415 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pool@0");
416 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
417 "base_icid", FSL_DPAA2_STREAM_ID_START, 1);
418 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
419 "num",
420 FSL_DPAA2_STREAM_ID_END -
421 FSL_DPAA2_STREAM_ID_START + 1, 1);
422
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000423 /* fixup MAC addresses for dpmac ports */
424 nodeoffset = fdt_path_offset(blob, "/board_info/ports");
425 if (nodeoffset < 0)
Ioana Ciocoi Radulescu2e9f1bf2019-02-26 15:50:07 +0000426 goto out;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000427
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000428 err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPC);
Ioana Ciocoi Radulescu2e9f1bf2019-02-26 15:50:07 +0000429
430out:
Stuart Yoder21c69872015-07-02 11:29:03 +0530431 flush_dcache_range(dpc_addr, dpc_addr + fdt_totalsize(blob));
432
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000433 return err;
Stuart Yoder21c69872015-07-02 11:29:03 +0530434}
435
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530436static 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 -0700437{
438 u64 mc_dpc_offset;
439#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
440 int error;
441 void *dpc_fdt_hdr;
442 int dpc_size;
443#endif
444
445#ifdef CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET
446 BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET & 0x3) != 0 ||
447 CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET > 0xffffffff);
448
449 mc_dpc_offset = CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET;
450#else
451#error "CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET not defined"
452#endif
453
454 /*
455 * Load the MC DPC blob in the MC private DRAM block:
456 */
457#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
458 printf("MC DPC is preloaded to %#llx\n", mc_ram_addr + mc_dpc_offset);
459#else
460 /*
461 * Get address and size of the DPC blob stored in flash:
462 */
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530463 dpc_fdt_hdr = (void *)mc_dpc_addr;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700464
465 error = fdt_check_header(dpc_fdt_hdr);
466 if (error != 0) {
467 /*
468 * Don't return with error here, since the MC firmware can
469 * still boot without a DPC
470 */
J. German Riveracc088c32015-07-02 11:28:56 +0530471 printf("\nfsl-mc: WARNING: No DPC image found");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700472 return 0;
473 }
474
475 dpc_size = fdt_totalsize(dpc_fdt_hdr);
476 if (dpc_size > CONFIG_SYS_LS_MC_DPC_MAX_LENGTH) {
J. German Riveracc088c32015-07-02 11:28:56 +0530477 printf("\nfsl-mc: ERROR: Bad DPC image (too large: %d)\n",
J. German Rivera125e2bc2015-03-20 19:28:18 -0700478 dpc_size);
479 return -EINVAL;
480 }
481
482 mc_copy_image("MC DPC blob",
483 (u64)dpc_fdt_hdr, dpc_size, mc_ram_addr + mc_dpc_offset);
484#endif /* not defined CONFIG_SYS_LS_MC_DPC_IN_DDR */
485
Stuart Yoder21c69872015-07-02 11:29:03 +0530486 if (mc_fixup_dpc(mc_ram_addr + mc_dpc_offset))
487 return -EINVAL;
488
J. German Rivera125e2bc2015-03-20 19:28:18 -0700489 dump_ram_words("DPC", (void *)(mc_ram_addr + mc_dpc_offset));
490 return 0;
491}
492
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000493
494static int mc_fixup_dpl(u64 dpl_addr)
495{
496 void *blob = (void *)dpl_addr;
497 u32 ver = fdt_getprop_u32_default(blob, "/", "dpl-version", 0);
498 int err = 0;
499
500 /* The DPL fixup for mac addresses is only relevant
501 * for old-style DPLs
502 */
503 if (ver >= 10)
504 return 0;
505
506 err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPL);
507 flush_dcache_range(dpl_addr, dpl_addr + fdt_totalsize(blob));
508
509 return err;
510}
511
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530512static 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 -0700513{
514 u64 mc_dpl_offset;
515#ifndef CONFIG_SYS_LS_MC_DPL_IN_DDR
516 int error;
517 void *dpl_fdt_hdr;
518 int dpl_size;
519#endif
520
521#ifdef CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET
522 BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET & 0x3) != 0 ||
523 CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET > 0xffffffff);
524
525 mc_dpl_offset = CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET;
526#else
527#error "CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET not defined"
528#endif
529
530 /*
531 * Load the MC DPL blob in the MC private DRAM block:
532 */
533#ifdef CONFIG_SYS_LS_MC_DPL_IN_DDR
534 printf("MC DPL is preloaded to %#llx\n", mc_ram_addr + mc_dpl_offset);
535#else
536 /*
537 * Get address and size of the DPL blob stored in flash:
538 */
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530539 dpl_fdt_hdr = (void *)mc_dpl_addr;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700540
541 error = fdt_check_header(dpl_fdt_hdr);
542 if (error != 0) {
J. German Riveracc088c32015-07-02 11:28:56 +0530543 printf("\nfsl-mc: ERROR: Bad DPL image (bad header)\n");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700544 return error;
545 }
546
547 dpl_size = fdt_totalsize(dpl_fdt_hdr);
548 if (dpl_size > CONFIG_SYS_LS_MC_DPL_MAX_LENGTH) {
J. German Riveracc088c32015-07-02 11:28:56 +0530549 printf("\nfsl-mc: ERROR: Bad DPL image (too large: %d)\n",
J. German Rivera125e2bc2015-03-20 19:28:18 -0700550 dpl_size);
551 return -EINVAL;
552 }
553
554 mc_copy_image("MC DPL blob",
555 (u64)dpl_fdt_hdr, dpl_size, mc_ram_addr + mc_dpl_offset);
556#endif /* not defined CONFIG_SYS_LS_MC_DPL_IN_DDR */
557
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000558 if (mc_fixup_dpl(mc_ram_addr + mc_dpl_offset))
559 return -EINVAL;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700560 dump_ram_words("DPL", (void *)(mc_ram_addr + mc_dpl_offset));
561 return 0;
562}
563
564/**
565 * Return the MC boot timeout value in milliseconds
566 */
567static unsigned long get_mc_boot_timeout_ms(void)
568{
569 unsigned long timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
570
Simon Glass00caae62017-08-03 12:22:12 -0600571 char *timeout_ms_env_var = env_get(MC_BOOT_TIMEOUT_ENV_VAR);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700572
573 if (timeout_ms_env_var) {
574 timeout_ms = simple_strtoul(timeout_ms_env_var, NULL, 10);
575 if (timeout_ms == 0) {
576 printf("fsl-mc: WARNING: Invalid value for \'"
577 MC_BOOT_TIMEOUT_ENV_VAR
578 "\' environment variable: %lu\n",
579 timeout_ms);
580
581 timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
582 }
583 }
584
585 return timeout_ms;
586}
587
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530588#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
York Sun3c1d2182016-04-04 11:41:26 -0700589
590__weak bool soc_has_aiop(void)
591{
592 return false;
593}
594
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530595static int load_mc_aiop_img(u64 aiop_fw_addr)
J. German Riverac1000c12015-07-02 11:28:58 +0530596{
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530597 u64 mc_ram_addr = mc_get_dram_addr();
598#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
J. German Riverac1000c12015-07-02 11:28:58 +0530599 void *aiop_img;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530600#endif
J. German Riverac1000c12015-07-02 11:28:58 +0530601
York Sun3c1d2182016-04-04 11:41:26 -0700602 /* Check if AIOP is available */
603 if (!soc_has_aiop())
604 return -ENODEV;
J. German Riverac1000c12015-07-02 11:28:58 +0530605 /*
606 * Load the MC AIOP image in the MC private DRAM block:
607 */
608
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530609#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
610 printf("MC AIOP is preloaded to %#llx\n", mc_ram_addr +
611 CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
612#else
613 aiop_img = (void *)aiop_fw_addr;
J. German Riverac1000c12015-07-02 11:28:58 +0530614 mc_copy_image("MC AIOP image",
615 (u64)aiop_img, CONFIG_SYS_LS_MC_AIOP_IMG_MAX_LENGTH,
616 mc_ram_addr + CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530617#endif
618 mc_aiop_applied = 0;
J. German Riverac1000c12015-07-02 11:28:58 +0530619
620 return 0;
621}
622#endif
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530623
J. German Rivera125e2bc2015-03-20 19:28:18 -0700624static int wait_for_mc(bool booting_mc, u32 *final_reg_gsr)
625{
626 u32 reg_gsr;
627 u32 mc_fw_boot_status;
628 unsigned long timeout_ms = get_mc_boot_timeout_ms();
629 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
630
631 dmb();
J. German Rivera125e2bc2015-03-20 19:28:18 -0700632 assert(timeout_ms > 0);
633 for (;;) {
634 udelay(1000); /* throttle polling */
635 reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
636 mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
637 if (mc_fw_boot_status & 0x1)
638 break;
639
640 timeout_ms--;
641 if (timeout_ms == 0)
642 break;
643 }
644
645 if (timeout_ms == 0) {
J. German Riveracc088c32015-07-02 11:28:56 +0530646 printf("ERROR: timeout\n");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700647
648 /* TODO: Get an error status from an MC CCSR register */
649 return -ETIMEDOUT;
650 }
651
652 if (mc_fw_boot_status != 0x1) {
653 /*
654 * TODO: Identify critical errors from the GSR register's FS
655 * field and for those errors, set error to -ENODEV or other
656 * appropriate errno, so that the status property is set to
657 * failure in the fsl,dprc device tree node.
658 */
J. German Riveracc088c32015-07-02 11:28:56 +0530659 printf("WARNING: Firmware returned an error (GSR: %#x)\n",
660 reg_gsr);
661 } else {
662 printf("SUCCESS\n");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700663 }
664
J. German Riveracc088c32015-07-02 11:28:56 +0530665
J. German Rivera125e2bc2015-03-20 19:28:18 -0700666 *final_reg_gsr = reg_gsr;
667 return 0;
668}
J. German Riverab940ca62014-06-23 15:15:55 -0700669
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530670int mc_init(u64 mc_fw_addr, u64 mc_dpc_addr)
J. German Riverab940ca62014-06-23 15:15:55 -0700671{
672 int error = 0;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700673 int portal_id = 0;
J. German Riverab940ca62014-06-23 15:15:55 -0700674 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530675 u64 mc_ram_addr = mc_get_dram_addr();
J. German Riverab940ca62014-06-23 15:15:55 -0700676 u32 reg_gsr;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700677 u32 reg_mcfbalr;
678#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Riverab940ca62014-06-23 15:15:55 -0700679 const void *raw_image_addr;
680 size_t raw_image_size = 0;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700681#endif
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800682 struct mc_version mc_ver_info;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700683 u8 mc_ram_num_256mb_blocks;
684 size_t mc_ram_size = mc_get_dram_block_size();
J. German Riverab940ca62014-06-23 15:15:55 -0700685
York Sun437858b62017-03-06 09:02:29 -0800686 mc_ram_num_256mb_blocks = mc_ram_size / MC_RAM_SIZE_ALIGNMENT;
Meenakshi Aggarwal43ad41e2019-02-27 14:41:02 +0530687
688 if (mc_ram_num_256mb_blocks >= 0xff) {
York Sun437858b62017-03-06 09:02:29 -0800689 error = -EINVAL;
690 printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
691 mc_ram_size);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700692 goto out;
York Sun437858b62017-03-06 09:02:29 -0800693 }
J. German Rivera125e2bc2015-03-20 19:28:18 -0700694
J. German Riverab940ca62014-06-23 15:15:55 -0700695 /*
Meenakshi Aggarwal43ad41e2019-02-27 14:41:02 +0530696 * To support 128 MB DDR Size for MC
697 */
698 if (mc_ram_num_256mb_blocks == 0)
699 mc_ram_num_256mb_blocks = 0xFF;
700
701 /*
J. German Riverab940ca62014-06-23 15:15:55 -0700702 * Management Complex cores should be held at reset out of POR.
Bin Menga1875592016-02-05 19:30:11 -0800703 * U-Boot should be the first software to touch MC. To be safe,
J. German Riverab940ca62014-06-23 15:15:55 -0700704 * we reset all cores again by setting GCR1 to 0. It doesn't do
705 * anything if they are held at reset. After we setup the firmware
706 * we kick off MC by deasserting the reset bit for core 0, and
707 * deasserting the reset bits for Command Portal Managers.
708 * The stop bits are not touched here. They are used to stop the
709 * cores when they are active. Setting stop bits doesn't stop the
710 * cores from fetching instructions when they are released from
711 * reset.
712 */
713 out_le32(&mc_ccsr_regs->reg_gcr1, 0);
714 dmb();
715
J. German Rivera125e2bc2015-03-20 19:28:18 -0700716#ifdef CONFIG_SYS_LS_MC_FW_IN_DDR
717 printf("MC firmware is preloaded to %#llx\n", mc_ram_addr);
718#else
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530719 error = parse_mc_firmware_fit_image(mc_fw_addr, &raw_image_addr,
720 &raw_image_size);
J. German Riverab940ca62014-06-23 15:15:55 -0700721 if (error != 0)
722 goto out;
723 /*
724 * Load the MC FW at the beginning of the MC private DRAM block:
725 */
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800726 mc_copy_image("MC Firmware",
727 (u64)raw_image_addr, raw_image_size, mc_ram_addr);
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800728#endif
J. German Rivera125e2bc2015-03-20 19:28:18 -0700729 dump_ram_words("firmware", (void *)mc_ram_addr);
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800730
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530731 error = load_mc_dpc(mc_ram_addr, mc_ram_size, mc_dpc_addr);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700732 if (error != 0)
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800733 goto out;
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800734
J. German Riverab940ca62014-06-23 15:15:55 -0700735 debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700736 dump_mc_ccsr_regs(mc_ccsr_regs);
J. German Riverab940ca62014-06-23 15:15:55 -0700737
738 /*
J. German Rivera125e2bc2015-03-20 19:28:18 -0700739 * Tell MC what is the address range of the DRAM block assigned to it:
J. German Riverab940ca62014-06-23 15:15:55 -0700740 */
Meenakshi Aggarwal43ad41e2019-02-27 14:41:02 +0530741 if (mc_ram_num_256mb_blocks < 0xFF) {
742 reg_mcfbalr = (u32)mc_ram_addr |
743 (mc_ram_num_256mb_blocks - 1);
744 } else {
745 reg_mcfbalr = (u32)mc_ram_addr |
746 (mc_ram_num_256mb_blocks);
747 }
748
J. German Rivera125e2bc2015-03-20 19:28:18 -0700749 out_le32(&mc_ccsr_regs->reg_mcfbalr, reg_mcfbalr);
750 out_le32(&mc_ccsr_regs->reg_mcfbahr,
York Sun437858b62017-03-06 09:02:29 -0800751 (u32)(mc_ram_addr >> 32));
Stuart Yoder39da6442015-07-02 11:29:02 +0530752 out_le32(&mc_ccsr_regs->reg_mcfapr, FSL_BYPASS_AMQ);
J. German Riverab940ca62014-06-23 15:15:55 -0700753
754 /*
J. German Rivera125e2bc2015-03-20 19:28:18 -0700755 * Tell the MC that we want delayed DPL deployment.
J. German Riverab940ca62014-06-23 15:15:55 -0700756 */
J. German Rivera125e2bc2015-03-20 19:28:18 -0700757 out_le32(&mc_ccsr_regs->reg_gsr, 0xDD00);
J. German Riverab940ca62014-06-23 15:15:55 -0700758
J. German Riveracc088c32015-07-02 11:28:56 +0530759 printf("\nfsl-mc: Booting Management Complex ... ");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800760
J. German Riverab940ca62014-06-23 15:15:55 -0700761 /*
762 * Deassert reset and release MC core 0 to run
763 */
764 out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700765 error = wait_for_mc(true, &reg_gsr);
766 if (error != 0)
J. German Riverab940ca62014-06-23 15:15:55 -0700767 goto out;
J. German Riverab940ca62014-06-23 15:15:55 -0700768
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800769 /*
770 * TODO: need to obtain the portal_id for the root container from the
771 * DPL
772 */
773 portal_id = 0;
774
775 /*
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700776 * Initialize the global default MC portal
777 * And check that the MC firmware is responding portal commands:
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800778 */
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530779 root_mc_io = (struct fsl_mc_io *)calloc(sizeof(struct fsl_mc_io), 1);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530780 if (!root_mc_io) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530781 printf(" No memory: calloc() failed\n");
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700782 return -ENOMEM;
783 }
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800784
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530785 root_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(portal_id);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700786 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 +0530787 portal_id, root_mc_io->mmio_regs);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700788
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530789 error = mc_get_version(root_mc_io, MC_CMD_NO_FLAGS, &mc_ver_info);
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800790 if (error != 0) {
791 printf("fsl-mc: ERROR: Firmware version check failed (error: %d)\n",
792 error);
793 goto out;
794 }
795
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800796 printf("fsl-mc: Management Complex booted (version: %d.%d.%d, boot status: %#x)\n",
797 mc_ver_info.major, mc_ver_info.minor, mc_ver_info.revision,
J. German Rivera125e2bc2015-03-20 19:28:18 -0700798 reg_gsr & GSR_FS_MASK);
799
J. German Riverab940ca62014-06-23 15:15:55 -0700800out:
801 if (error != 0)
Prabhakar Kushwaha2b7c4a12015-07-02 11:29:01 +0530802 mc_boot_status = error;
J. German Riverab940ca62014-06-23 15:15:55 -0700803 else
804 mc_boot_status = 0;
805
806 return error;
807}
808
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530809int mc_apply_dpl(u64 mc_dpl_addr)
810{
811 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
812 int error = 0;
813 u32 reg_gsr;
814 u64 mc_ram_addr = mc_get_dram_addr();
815 size_t mc_ram_size = mc_get_dram_block_size();
816
Alexander Grafb7b84102016-11-17 01:02:57 +0100817 if (!mc_dpl_addr)
818 return -1;
819
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530820 error = load_mc_dpl(mc_ram_addr, mc_ram_size, mc_dpl_addr);
821 if (error != 0)
822 return error;
823
824 /*
825 * Tell the MC to deploy the DPL:
826 */
827 out_le32(&mc_ccsr_regs->reg_gsr, 0x0);
828 printf("fsl-mc: Deploying data path layout ... ");
829 error = wait_for_mc(false, &reg_gsr);
830
831 if (!error)
832 mc_dpl_applied = 0;
833
834 return error;
835}
836
J. German Riverab940ca62014-06-23 15:15:55 -0700837int get_mc_boot_status(void)
838{
839 return mc_boot_status;
840}
841
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530842#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
843int get_aiop_apply_status(void)
844{
845 return mc_aiop_applied;
846}
847#endif
848
849int get_dpl_apply_status(void)
850{
851 return mc_dpl_applied;
852}
853
Mian Yousaf Kaukab7e968042018-12-18 14:01:17 +0100854int is_lazy_dpl_addr_valid(void)
855{
856 return !!mc_lazy_dpl_addr;
857}
858
Priyanka Jain033c5382017-08-24 16:42:43 +0530859/*
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530860 * Return the MC address of private DRAM block.
Priyanka Jain033c5382017-08-24 16:42:43 +0530861 * As per MC design document, MC initial base address
862 * should be least significant 512MB address of MC private
863 * memory, i.e. address should point to end address masked
864 * with 512MB offset in private DRAM block.
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530865 */
866u64 mc_get_dram_addr(void)
867{
Priyanka Jain033c5382017-08-24 16:42:43 +0530868 size_t mc_ram_size = mc_get_dram_block_size();
869
Prabhakar Kushwaha87519a92018-08-27 12:58:52 +0530870 if (!mc_memset_resv_ram || (get_mc_boot_status() < 0)) {
871 mc_memset_resv_ram = 1;
872 memset((void *)gd->arch.resv_ram, 0, mc_ram_size);
873 }
874
Priyanka Jain033c5382017-08-24 16:42:43 +0530875 return (gd->arch.resv_ram + mc_ram_size - 1) &
876 MC_RAM_BASE_ADDR_ALIGNMENT_MASK;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530877}
878
J. German Riverab940ca62014-06-23 15:15:55 -0700879/**
880 * Return the actual size of the MC private DRAM block.
J. German Riverab940ca62014-06-23 15:15:55 -0700881 */
882unsigned long mc_get_dram_block_size(void)
883{
J. German Rivera125e2bc2015-03-20 19:28:18 -0700884 unsigned long dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
885
Simon Glass00caae62017-08-03 12:22:12 -0600886 char *dram_block_size_env_var = env_get(MC_MEM_SIZE_ENV_VAR);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700887
888 if (dram_block_size_env_var) {
889 dram_block_size = simple_strtoul(dram_block_size_env_var, NULL,
Prabhakar Kushwahaf53e12d2017-11-09 14:45:41 +0530890 16);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700891
892 if (dram_block_size < CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE) {
893 printf("fsl-mc: WARNING: Invalid value for \'"
894 MC_MEM_SIZE_ENV_VAR
895 "\' environment variable: %lu\n",
896 dram_block_size);
897
Meenakshi Aggarwal43ad41e2019-02-27 14:41:02 +0530898 dram_block_size = MC_DRAM_BLOCK_DEFAULT_SIZE;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700899 }
900 }
901
902 return dram_block_size;
J. German Riverab940ca62014-06-23 15:15:55 -0700903}
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700904
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530905int fsl_mc_ldpaa_init(bd_t *bis)
906{
Prabhakar Kushwahac919ab92015-11-04 12:26:00 +0530907 int i;
908
909 for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++)
Pankaj Bansal1ed19a12018-10-10 14:08:33 +0530910 if (wriop_is_enabled_dpmac(i) == 1)
Prabhakar Kushwahac919ab92015-11-04 12:26:00 +0530911 ldpaa_eth_init(i, wriop_get_enet_if(i));
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530912 return 0;
913}
914
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530915static int dprc_version_check(struct fsl_mc_io *mc_io, uint16_t handle)
916{
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530917 int error;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530918 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530919
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530920 error = dprc_get_api_version(mc_io, 0,
921 &major_ver,
922 &minor_ver);
923 if (error < 0) {
924 printf("dprc_get_api_version() failed: %d\n", error);
925 return error;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530926 }
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530927
928 if (major_ver < DPRC_VER_MAJOR || (major_ver == DPRC_VER_MAJOR &&
929 minor_ver < DPRC_VER_MINOR)) {
930 printf("DPRC version mismatch found %u.%u,",
931 major_ver, minor_ver);
932 printf("supported version is %u.%u\n",
933 DPRC_VER_MAJOR, DPRC_VER_MINOR);
934 }
935
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530936 return error;
937}
938
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530939static int dpio_init(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700940{
941 struct qbman_swp_desc p_des;
942 struct dpio_attr attr;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530943 struct dpio_cfg dpio_cfg;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700944 int err = 0;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530945 uint16_t major_ver, minor_ver;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700946
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530947 dflt_dpio = (struct fsl_dpio_obj *)calloc(
948 sizeof(struct fsl_dpio_obj), 1);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700949 if (!dflt_dpio) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530950 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530951 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530952 goto err_calloc;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700953 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530954 dpio_cfg.channel_mode = DPIO_LOCAL_CHANNEL;
955 dpio_cfg.num_priorities = 8;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700956
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530957 err = dpio_create(dflt_mc_io,
958 dflt_dprc_handle,
959 MC_CMD_NO_FLAGS,
960 &dpio_cfg,
961 &dflt_dpio->dpio_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530962 if (err < 0) {
963 printf("dpio_create() failed: %d\n", err);
964 err = -ENODEV;
965 goto err_create;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700966 }
967
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530968 err = dpio_get_api_version(dflt_mc_io, 0,
969 &major_ver,
970 &minor_ver);
971 if (err < 0) {
972 printf("dpio_get_api_version() failed: %d\n", err);
973 goto err_get_api_ver;
974 }
975
976 if (major_ver < DPIO_VER_MAJOR || (major_ver == DPIO_VER_MAJOR &&
977 minor_ver < DPIO_VER_MINOR)) {
978 printf("DPRC version mismatch found %u.%u,",
979 major_ver,
980 minor_ver);
981 }
982
983 err = dpio_open(dflt_mc_io,
984 MC_CMD_NO_FLAGS,
985 dflt_dpio->dpio_id,
986 &dflt_dpio->dpio_handle);
987 if (err) {
988 printf("dpio_open() failed\n");
989 goto err_open;
990 }
991
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530992 memset(&attr, 0, sizeof(struct dpio_attr));
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +0530993 err = dpio_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530994 dflt_dpio->dpio_handle, &attr);
995 if (err < 0) {
996 printf("dpio_get_attributes() failed: %d\n", err);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700997 goto err_get_attr;
998 }
999
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301000 if (dflt_dpio->dpio_id != attr.id) {
1001 printf("dnpi object id and attribute id are not same\n");
1002 goto err_attr_not_same;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301003 }
1004
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301005#ifdef DEBUG
1006 printf("Init: DPIO id=0x%d\n", dflt_dpio->dpio_id);
1007#endif
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301008 err = dpio_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1009 if (err < 0) {
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001010 printf("dpio_enable() failed %d\n", err);
1011 goto err_get_enable;
1012 }
Prabhakar Kushwaha1f1c25c2015-07-02 11:28:59 +05301013 debug("ce_offset=0x%llx, ci_offset=0x%llx, portalid=%d, prios=%d\n",
1014 attr.qbman_portal_ce_offset,
1015 attr.qbman_portal_ci_offset,
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001016 attr.qbman_portal_id,
1017 attr.num_priorities);
1018
Prabhakar Kushwaha1f1c25c2015-07-02 11:28:59 +05301019 p_des.cena_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
1020 + attr.qbman_portal_ce_offset);
1021 p_des.cinh_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
1022 + attr.qbman_portal_ci_offset);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001023
1024 dflt_dpio->sw_portal = qbman_swp_init(&p_des);
1025 if (dflt_dpio->sw_portal == NULL) {
1026 printf("qbman_swp_init() failed\n");
1027 goto err_get_swp_init;
1028 }
1029 return 0;
1030
1031err_get_swp_init:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301032 dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001033err_get_enable:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301034err_get_attr:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301035err_attr_not_same:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301036 dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301037err_open:
1038err_get_api_ver:
1039 dpio_destroy(dflt_mc_io,
1040 dflt_dprc_handle,
1041 MC_CMD_NO_FLAGS,
1042 dflt_dpio->dpio_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301043err_create:
Prabhakar Kushwahacd7b3fb2016-03-18 16:15:29 +05301044 free(dflt_dpio);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301045err_calloc:
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001046 return err;
1047}
1048
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301049static int dpio_exit(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001050{
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301051 int err;
1052
1053 err = dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1054 if (err < 0) {
1055 printf("dpio_disable() failed: %d\n", err);
1056 goto err;
1057 }
1058
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301059 dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1060 if (err < 0) {
1061 printf("dpio_close() failed: %d\n", err);
1062 goto err;
1063 }
1064
1065 err = dpio_destroy(dflt_mc_io,
1066 dflt_dprc_handle,
1067 MC_CMD_NO_FLAGS,
1068 dflt_dpio->dpio_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301069 if (err < 0) {
1070 printf("dpio_destroy() failed: %d\n", err);
1071 goto err;
1072 }
1073
1074#ifdef DEBUG
1075 printf("Exit: DPIO id=0x%d\n", dflt_dpio->dpio_id);
1076#endif
1077
1078 if (dflt_dpio)
1079 free(dflt_dpio);
1080
1081 return 0;
1082err:
1083 return err;
1084}
1085
1086static int dprc_init(void)
1087{
1088 int err, child_portal_id, container_id;
1089 struct dprc_cfg cfg;
1090 uint64_t mc_portal_offset;
1091
1092 /* Open root container */
1093 err = dprc_get_container_id(root_mc_io, MC_CMD_NO_FLAGS, &container_id);
1094 if (err < 0) {
1095 printf("dprc_get_container_id(): Root failed: %d\n", err);
1096 goto err_root_container_id;
1097 }
1098
1099#ifdef DEBUG
1100 printf("Root container id = %d\n", container_id);
1101#endif
1102 err = dprc_open(root_mc_io, MC_CMD_NO_FLAGS, container_id,
1103 &root_dprc_handle);
1104 if (err < 0) {
1105 printf("dprc_open(): Root Container failed: %d\n", err);
1106 goto err_root_open;
1107 }
1108
1109 if (!root_dprc_handle) {
1110 printf("dprc_open(): Root Container Handle is not valid\n");
1111 goto err_root_open;
1112 }
1113
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301114 err = dprc_version_check(root_mc_io, root_dprc_handle);
1115 if (err < 0) {
1116 printf("dprc_version_check() failed: %d\n", err);
1117 goto err_root_open;
1118 }
1119
Prabhakar Kushwaha5373b202016-01-20 12:04:19 +05301120 memset(&cfg, 0, sizeof(struct dprc_cfg));
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301121 cfg.options = DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED |
1122 DPRC_CFG_OPT_OBJ_CREATE_ALLOWED |
1123 DPRC_CFG_OPT_ALLOC_ALLOWED;
1124 cfg.icid = DPRC_GET_ICID_FROM_POOL;
Prabhakar Kushwaha335b1932015-12-24 15:33:49 +05301125 cfg.portal_id = DPRC_GET_PORTAL_ID_FROM_POOL;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301126 err = dprc_create_container(root_mc_io, MC_CMD_NO_FLAGS,
1127 root_dprc_handle,
1128 &cfg,
1129 &child_dprc_id,
1130 &mc_portal_offset);
1131 if (err < 0) {
1132 printf("dprc_create_container() failed: %d\n", err);
1133 goto err_create;
1134 }
1135
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301136 dflt_mc_io = (struct fsl_mc_io *)calloc(sizeof(struct fsl_mc_io), 1);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301137 if (!dflt_mc_io) {
1138 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301139 printf(" No memory: calloc() failed\n");
1140 goto err_calloc;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301141 }
1142
1143 child_portal_id = MC_PORTAL_OFFSET_TO_PORTAL_ID(mc_portal_offset);
1144 dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(child_portal_id);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301145
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301146#ifdef DEBUG
1147 printf("MC portal of child DPRC container: %d, physical addr %p)\n",
1148 child_dprc_id, dflt_mc_io->mmio_regs);
1149#endif
1150
1151 err = dprc_open(dflt_mc_io, MC_CMD_NO_FLAGS, child_dprc_id,
1152 &dflt_dprc_handle);
1153 if (err < 0) {
1154 printf("dprc_open(): Child container failed: %d\n", err);
1155 goto err_child_open;
1156 }
1157
1158 if (!dflt_dprc_handle) {
1159 printf("dprc_open(): Child container Handle is not valid\n");
1160 goto err_child_open;
1161 }
1162
1163 return 0;
1164err_child_open:
1165 free(dflt_mc_io);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301166err_calloc:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301167 dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1168 root_dprc_handle, child_dprc_id);
1169err_create:
1170 dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1171err_root_open:
1172err_root_container_id:
1173 return err;
1174}
1175
1176static int dprc_exit(void)
1177{
1178 int err;
1179
1180 err = dprc_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dprc_handle);
1181 if (err < 0) {
1182 printf("dprc_close(): Child failed: %d\n", err);
1183 goto err;
1184 }
1185
1186 err = dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1187 root_dprc_handle, child_dprc_id);
1188 if (err < 0) {
1189 printf("dprc_destroy_container() failed: %d\n", err);
1190 goto err;
1191 }
1192
1193 err = dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1194 if (err < 0) {
1195 printf("dprc_close(): Root failed: %d\n", err);
1196 goto err;
1197 }
1198
1199 if (dflt_mc_io)
1200 free(dflt_mc_io);
1201
1202 if (root_mc_io)
1203 free(root_mc_io);
1204
1205 return 0;
1206
1207err:
1208 return err;
1209}
1210
1211static int dpbp_init(void)
1212{
1213 int err;
1214 struct dpbp_attr dpbp_attr;
1215 struct dpbp_cfg dpbp_cfg;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301216 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301217
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301218 dflt_dpbp = (struct fsl_dpbp_obj *)calloc(
1219 sizeof(struct fsl_dpbp_obj), 1);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001220 if (!dflt_dpbp) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301221 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301222 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301223 goto err_calloc;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001224 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301225
1226 dpbp_cfg.options = 512;
1227
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301228 err = dpbp_create(dflt_mc_io,
1229 dflt_dprc_handle,
1230 MC_CMD_NO_FLAGS,
1231 &dpbp_cfg,
1232 &dflt_dpbp->dpbp_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301233
1234 if (err < 0) {
1235 err = -ENODEV;
1236 printf("dpbp_create() failed: %d\n", err);
1237 goto err_create;
1238 }
1239
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301240 err = dpbp_get_api_version(dflt_mc_io, 0,
1241 &major_ver,
1242 &minor_ver);
1243 if (err < 0) {
1244 printf("dpbp_get_api_version() failed: %d\n", err);
1245 goto err_get_api_ver;
1246 }
1247
1248 if (major_ver < DPBP_VER_MAJOR || (major_ver == DPBP_VER_MAJOR &&
1249 minor_ver < DPBP_VER_MINOR)) {
1250 printf("DPBP version mismatch found %u.%u,",
1251 major_ver, minor_ver);
1252 printf("supported version is %u.%u\n",
1253 DPBP_VER_MAJOR, DPBP_VER_MINOR);
1254 }
1255
1256 err = dpbp_open(dflt_mc_io,
1257 MC_CMD_NO_FLAGS,
1258 dflt_dpbp->dpbp_id,
1259 &dflt_dpbp->dpbp_handle);
1260 if (err) {
1261 printf("dpbp_open() failed\n");
1262 goto err_open;
1263 }
1264
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301265 memset(&dpbp_attr, 0, sizeof(struct dpbp_attr));
1266 err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
1267 dflt_dpbp->dpbp_handle,
1268 &dpbp_attr);
1269 if (err < 0) {
1270 printf("dpbp_get_attributes() failed: %d\n", err);
1271 goto err_get_attr;
1272 }
1273
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301274 if (dflt_dpbp->dpbp_id != dpbp_attr.id) {
1275 printf("dpbp object id and attribute id are not same\n");
1276 goto err_attr_not_same;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301277 }
1278
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301279#ifdef DEBUG
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301280 printf("Init: DPBP id=0x%x\n", dflt_dpbp->dpbp_attr.id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301281#endif
1282
1283 err = dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
1284 if (err < 0) {
1285 printf("dpbp_close() failed: %d\n", err);
1286 goto err_close;
1287 }
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001288
1289 return 0;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301290
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301291err_get_attr:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301292err_attr_not_same:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301293 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301294 dpbp_destroy(dflt_mc_io,
1295 dflt_dprc_handle,
1296 MC_CMD_NO_FLAGS,
1297 dflt_dpbp->dpbp_id);
1298err_get_api_ver:
1299err_close:
1300err_open:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301301err_create:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301302 free(dflt_dpbp);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301303err_calloc:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301304 return err;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001305}
1306
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301307static int dpbp_exit(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001308{
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301309 int err;
1310
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301311 err = dpbp_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1312 dflt_dpbp->dpbp_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301313 if (err < 0) {
1314 printf("dpbp_destroy() failed: %d\n", err);
1315 goto err;
1316 }
1317
1318#ifdef DEBUG
1319 printf("Exit: DPBP id=0x%d\n", dflt_dpbp->dpbp_attr.id);
1320#endif
1321
1322 if (dflt_dpbp)
1323 free(dflt_dpbp);
1324 return 0;
1325
1326err:
1327 return err;
1328}
1329
1330static int dpni_init(void)
1331{
1332 int err;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301333 uint8_t cfg_buf[256] = {0};
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301334 struct dpni_cfg dpni_cfg;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301335 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301336
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301337 dflt_dpni = (struct fsl_dpni_obj *)calloc(
1338 sizeof(struct fsl_dpni_obj), 1);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301339 if (!dflt_dpni) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301340 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301341 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301342 goto err_calloc;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301343 }
1344
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301345 memset(&dpni_cfg, 0, sizeof(dpni_cfg));
1346 err = dpni_prepare_cfg(&dpni_cfg, &cfg_buf[0]);
Prabhakar Kushwaha879a59a2015-12-24 15:33:01 +05301347 if (err < 0) {
1348 err = -ENODEV;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301349 printf("dpni_prepare_cfg() failed: %d\n", err);
1350 goto err_prepare_cfg;
Prabhakar Kushwaha879a59a2015-12-24 15:33:01 +05301351 }
1352
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301353 err = dpni_create(dflt_mc_io,
1354 dflt_dprc_handle,
1355 MC_CMD_NO_FLAGS,
1356 &dpni_cfg,
1357 &dflt_dpni->dpni_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301358 if (err < 0) {
1359 err = -ENODEV;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301360 printf("dpni create() failed: %d\n", err);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301361 goto err_create;
1362 }
1363
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301364 err = dpni_get_api_version(dflt_mc_io, 0,
1365 &major_ver,
1366 &minor_ver);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301367 if (err < 0) {
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301368 printf("dpni_get_api_version() failed: %d\n", err);
1369 goto err_get_version;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301370 }
1371
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301372 if (major_ver < DPNI_VER_MAJOR || (major_ver == DPNI_VER_MAJOR &&
1373 minor_ver < DPNI_VER_MINOR)) {
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301374 printf("DPNI version mismatch found %u.%u,",
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301375 major_ver, minor_ver);
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301376 printf("supported version is %u.%u\n",
1377 DPNI_VER_MAJOR, DPNI_VER_MINOR);
1378 }
1379
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301380 err = dpni_open(dflt_mc_io,
1381 MC_CMD_NO_FLAGS,
1382 dflt_dpni->dpni_id,
1383 &dflt_dpni->dpni_handle);
1384 if (err) {
1385 printf("dpni_open() failed\n");
1386 goto err_open;
1387 }
1388
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301389#ifdef DEBUG
1390 printf("Init: DPNI id=0x%d\n", dflt_dpni->dpni_id);
1391#endif
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301392 err = dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
1393 if (err < 0) {
1394 printf("dpni_close() failed: %d\n", err);
1395 goto err_close;
1396 }
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001397
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301398 return 0;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301399
1400err_close:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301401 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301402err_open:
1403err_get_version:
1404 dpni_destroy(dflt_mc_io,
1405 dflt_dprc_handle,
1406 MC_CMD_NO_FLAGS,
1407 dflt_dpni->dpni_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301408err_create:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301409err_prepare_cfg:
Prabhakar Kushwaha879a59a2015-12-24 15:33:01 +05301410 free(dflt_dpni);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301411err_calloc:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301412 return err;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001413}
1414
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301415static int dpni_exit(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001416{
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301417 int err;
1418
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301419 err = dpni_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1420 dflt_dpni->dpni_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301421 if (err < 0) {
1422 printf("dpni_destroy() failed: %d\n", err);
1423 goto err;
1424 }
1425
1426#ifdef DEBUG
1427 printf("Exit: DPNI id=0x%d\n", dflt_dpni->dpni_id);
1428#endif
1429
1430 if (dflt_dpni)
1431 free(dflt_dpni);
1432 return 0;
1433
1434err:
1435 return err;
1436}
1437
1438static int mc_init_object(void)
1439{
1440 int err = 0;
1441
1442 err = dprc_init();
1443 if (err < 0) {
1444 printf("dprc_init() failed: %d\n", err);
1445 goto err;
1446 }
1447
1448 err = dpbp_init();
1449 if (err < 0) {
1450 printf("dpbp_init() failed: %d\n", err);
1451 goto err;
1452 }
1453
1454 err = dpio_init();
1455 if (err < 0) {
1456 printf("dpio_init() failed: %d\n", err);
1457 goto err;
1458 }
1459
1460 err = dpni_init();
1461 if (err < 0) {
1462 printf("dpni_init() failed: %d\n", err);
1463 goto err;
1464 }
1465
1466 return 0;
1467err:
1468 return err;
1469}
1470
1471int fsl_mc_ldpaa_exit(bd_t *bd)
1472{
1473 int err = 0;
Yogesh Gaur42e81792017-04-27 10:14:16 +05301474 bool is_dpl_apply_status = false;
Santan Kumar06651b92017-06-29 11:19:34 +05301475 bool mc_boot_status = false;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301476
Alexander Grafb7b84102016-11-17 01:02:57 +01001477 if (bd && mc_lazy_dpl_addr && !fsl_mc_ldpaa_exit(NULL)) {
Yogesh Gaurf9747a52018-01-16 10:08:24 +05301478 err = mc_apply_dpl(mc_lazy_dpl_addr);
1479 if (!err)
1480 fdt_fixup_board_enet(working_fdt);
Alexander Grafb7b84102016-11-17 01:02:57 +01001481 mc_lazy_dpl_addr = 0;
1482 }
1483
Santan Kumar06651b92017-06-29 11:19:34 +05301484 if (!get_mc_boot_status())
1485 mc_boot_status = true;
1486
Prabhakar Kushwaha6dedced2016-03-21 14:19:39 +05301487 /* MC is not loaded intentionally, So return success. */
Santan Kumar06651b92017-06-29 11:19:34 +05301488 if (bd && !mc_boot_status)
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301489 return 0;
1490
Yogesh Gaur42e81792017-04-27 10:14:16 +05301491 /* If DPL is deployed, set is_dpl_apply_status as TRUE. */
1492 if (!get_dpl_apply_status())
1493 is_dpl_apply_status = true;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301494
Yogesh Gaur42e81792017-04-27 10:14:16 +05301495 /*
1496 * For case MC is loaded but DPL is not deployed, return success and
1497 * print message on console. Else FDT fix-up code execution hanged.
1498 */
Santan Kumar06651b92017-06-29 11:19:34 +05301499 if (bd && mc_boot_status && !is_dpl_apply_status) {
Yogesh Gaur42e81792017-04-27 10:14:16 +05301500 printf("fsl-mc: DPL not deployed, DPAA2 ethernet not work\n");
Yogesh Gaur73fa2062017-11-28 10:11:14 +05301501 goto mc_obj_cleanup;
Yogesh Gaur42e81792017-04-27 10:14:16 +05301502 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301503
Santan Kumar06651b92017-06-29 11:19:34 +05301504 if (bd && mc_boot_status && is_dpl_apply_status)
1505 return 0;
1506
Yogesh Gaur73fa2062017-11-28 10:11:14 +05301507mc_obj_cleanup:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301508 err = dpbp_exit();
1509 if (err < 0) {
Prabhakar Kushwahaa2a4dc52016-01-20 12:04:37 +05301510 printf("dpbp_exit() failed: %d\n", err);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301511 goto err;
1512 }
1513
1514 err = dpio_exit();
1515 if (err < 0) {
1516 printf("dpio_exit() failed: %d\n", err);
1517 goto err;
1518 }
1519
1520 err = dpni_exit();
1521 if (err < 0) {
1522 printf("dpni_exit() failed: %d\n", err);
1523 goto err;
1524 }
1525
1526 err = dprc_exit();
1527 if (err < 0) {
1528 printf("dprc_exit() failed: %d\n", err);
1529 goto err;
1530 }
1531
1532 return 0;
1533err:
1534 return err;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001535}
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301536
1537static int do_fsl_mc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1538{
1539 int err = 0;
1540 if (argc < 3)
1541 goto usage;
1542
1543 switch (argv[1][0]) {
1544 case 's': {
1545 char sub_cmd;
Prabhakar Kushwaha44937212015-11-09 16:42:07 +05301546 u64 mc_fw_addr, mc_dpc_addr;
1547#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
1548 u64 aiop_fw_addr;
1549#endif
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301550
1551 sub_cmd = argv[2][0];
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301552
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301553 switch (sub_cmd) {
1554 case 'm':
1555 if (argc < 5)
1556 goto usage;
1557
1558 if (get_mc_boot_status() == 0) {
1559 printf("fsl-mc: MC is already booted");
1560 printf("\n");
1561 return err;
1562 }
1563 mc_fw_addr = simple_strtoull(argv[3], NULL, 16);
1564 mc_dpc_addr = simple_strtoull(argv[4], NULL,
1565 16);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301566
1567 if (!mc_init(mc_fw_addr, mc_dpc_addr))
1568 err = mc_init_object();
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301569 break;
1570
1571#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
1572 case 'a':
1573 if (argc < 4)
1574 goto usage;
1575 if (get_aiop_apply_status() == 0) {
1576 printf("fsl-mc: AIOP FW is already");
1577 printf(" applied\n");
1578 return err;
1579 }
1580
1581 aiop_fw_addr = simple_strtoull(argv[3], NULL,
1582 16);
1583
York Sun3c1d2182016-04-04 11:41:26 -07001584 /* if SoC doesn't have AIOP, err = -ENODEV */
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301585 err = load_mc_aiop_img(aiop_fw_addr);
1586 if (!err)
1587 printf("fsl-mc: AIOP FW applied\n");
1588 break;
1589#endif
1590 default:
1591 printf("Invalid option: %s\n", argv[2]);
1592 goto usage;
1593
1594 break;
1595 }
1596 }
1597 break;
1598
Alexander Grafb7b84102016-11-17 01:02:57 +01001599 case 'l':
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301600 case 'a': {
1601 u64 mc_dpl_addr;
1602
1603 if (argc < 4)
1604 goto usage;
1605
1606 if (get_dpl_apply_status() == 0) {
1607 printf("fsl-mc: DPL already applied\n");
1608 return err;
1609 }
1610
1611 mc_dpl_addr = simple_strtoull(argv[3], NULL,
1612 16);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301613
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301614 if (get_mc_boot_status() != 0) {
1615 printf("fsl-mc: Deploying data path layout ..");
1616 printf("ERROR (MC is not booted)\n");
1617 return -ENODEV;
1618 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301619
Alexander Grafb7b84102016-11-17 01:02:57 +01001620 if (argv[1][0] == 'l') {
1621 /*
1622 * We will do the actual dpaa exit and dpl apply
1623 * later from announce_and_cleanup().
1624 */
1625 mc_lazy_dpl_addr = mc_dpl_addr;
1626 } else {
1627 /* The user wants it applied now */
1628 if (!fsl_mc_ldpaa_exit(NULL))
1629 err = mc_apply_dpl(mc_dpl_addr);
1630 }
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301631 break;
1632 }
1633 default:
1634 printf("Invalid option: %s\n", argv[1]);
1635 goto usage;
1636 break;
1637 }
1638 return err;
1639 usage:
1640 return CMD_RET_USAGE;
1641}
1642
1643U_BOOT_CMD(
1644 fsl_mc, CONFIG_SYS_MAXARGS, 1, do_fsl_mc,
1645 "DPAA2 command to manage Management Complex (MC)",
1646 "start mc [FW_addr] [DPC_addr] - Start Management Complex\n"
1647 "fsl_mc apply DPL [DPL_addr] - Apply DPL file\n"
Alexander Grafb7b84102016-11-17 01:02:57 +01001648 "fsl_mc lazyapply DPL [DPL_addr] - Apply DPL file on exit\n"
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301649 "fsl_mc start aiop [FW_addr] - Start AIOP\n"
1650);
Bogdan Purcareata33a89912017-05-24 16:40:21 +00001651
1652void mc_env_boot(void)
1653{
1654#if defined(CONFIG_FSL_MC_ENET)
1655 char *mc_boot_env_var;
1656 /* The MC may only be initialized in the reset PHY function
1657 * because otherwise U-Boot has not yet set up all the MAC
1658 * address info properly. Without MAC addresses, the MC code
1659 * can not properly initialize the DPC.
1660 */
Simon Glass00caae62017-08-03 12:22:12 -06001661 mc_boot_env_var = env_get(MC_BOOT_ENV_VAR);
Bogdan Purcareata33a89912017-05-24 16:40:21 +00001662 if (mc_boot_env_var)
1663 run_command_list(mc_boot_env_var, -1, 0);
1664#endif /* CONFIG_FSL_MC_ENET */
1665}