blob: ffc408e3a4accc3dbd3a11c0c439ca41a18cac83 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
J. German Rivera8ff14b72014-06-23 15:15:55 -07002/*
Yogesh Gaur1a0c4ae2018-05-09 10:52:17 +05303 * Copyright 2014 Freescale Semiconductor, Inc.
4 * Copyright 2017 NXP
Prabhakar Kushwaha145e4cd2018-08-27 12:58:52 +05305 * Copyright 2017-2018 NXP
J. German Rivera8ff14b72014-06-23 15:15:55 -07006 */
Stuart Yodera2bdb772015-07-02 11:29:03 +05307#include <common.h>
Simon Glassadaaa482019-11-14 12:57:43 -07008#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -07009#include <cpu_func.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060010#include <env.h>
J. German Rivera8ff14b72014-06-23 15:15:55 -070011#include <errno.h>
Masahiro Yamada78eeb912016-01-24 23:27:48 +090012#include <linux/bug.h>
J. German Rivera8ff14b72014-06-23 15:15:55 -070013#include <asm/io.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090014#include <linux/libfdt.h>
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +000015#include <net.h>
Stuart Yodera2bdb772015-07-02 11:29:03 +053016#include <fdt_support.h>
J. German Rivera43e4ae32015-01-06 13:19:02 -080017#include <fsl-mc/fsl_mc.h>
18#include <fsl-mc/fsl_mc_sys.h>
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070019#include <fsl-mc/fsl_mc_private.h>
J. German Rivera43e4ae32015-01-06 13:19:02 -080020#include <fsl-mc/fsl_dpmng.h>
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070021#include <fsl-mc/fsl_dprc.h>
22#include <fsl-mc/fsl_dpio.h>
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +053023#include <fsl-mc/fsl_dpni.h>
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070024#include <fsl-mc/fsl_qbman_portal.h>
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +053025#include <fsl-mc/ldpaa_wriop.h>
J. German Rivera8ff14b72014-06-23 15:15:55 -070026
J. German Riveraf4fed4b2015-03-20 19:28:18 -070027#define MC_RAM_BASE_ADDR_ALIGNMENT (512UL * 1024 * 1024)
28#define MC_RAM_BASE_ADDR_ALIGNMENT_MASK (~(MC_RAM_BASE_ADDR_ALIGNMENT - 1))
29#define MC_RAM_SIZE_ALIGNMENT (256UL * 1024 * 1024)
30
31#define MC_MEM_SIZE_ENV_VAR "mcmemsize"
32#define MC_BOOT_TIMEOUT_ENV_VAR "mcboottimeout"
Bogdan Purcareata08bc0142017-05-24 16:40:21 +000033#define MC_BOOT_ENV_VAR "mcinitcmd"
Meenakshi Aggarwal67f195c2019-02-27 14:41:02 +053034#define MC_DRAM_BLOCK_DEFAULT_SIZE (512UL * 1024 * 1024)
J. German Riveraf4fed4b2015-03-20 19:28:18 -070035
J. German Rivera8ff14b72014-06-23 15:15:55 -070036DECLARE_GLOBAL_DATA_PTR;
Prabhakar Kushwaha145e4cd2018-08-27 12:58:52 +053037static int mc_memset_resv_ram;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +053038static int mc_boot_status = -1;
39static int mc_dpl_applied = -1;
40#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
41static int mc_aiop_applied = -1;
42#endif
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +053043struct fsl_mc_io *root_mc_io = NULL;
44struct fsl_mc_io *dflt_mc_io = NULL; /* child container */
45uint16_t root_dprc_handle = 0;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070046uint16_t dflt_dprc_handle = 0;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +053047int child_dprc_id;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070048struct fsl_dpbp_obj *dflt_dpbp = NULL;
49struct fsl_dpio_obj *dflt_dpio = NULL;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +053050struct fsl_dpni_obj *dflt_dpni = NULL;
Alexander Graf2ebeb442016-11-17 01:02:57 +010051static u64 mc_lazy_dpl_addr;
J. German Riveraf4fed4b2015-03-20 19:28:18 -070052
53#ifdef DEBUG
54void dump_ram_words(const char *title, void *addr)
55{
56 int i;
57 uint32_t *words = addr;
58
59 printf("Dumping beginning of %s (%p):\n", title, addr);
60 for (i = 0; i < 16; i++)
61 printf("%#x ", words[i]);
62
63 printf("\n");
64}
J. German Rivera8ff14b72014-06-23 15:15:55 -070065
J. German Riveraf4fed4b2015-03-20 19:28:18 -070066void dump_mc_ccsr_regs(struct mc_ccsr_registers __iomem *mc_ccsr_regs)
67{
68 printf("MC CCSR registers:\n"
69 "reg_gcr1 %#x\n"
70 "reg_gsr %#x\n"
71 "reg_sicbalr %#x\n"
72 "reg_sicbahr %#x\n"
73 "reg_sicapr %#x\n"
74 "reg_mcfbalr %#x\n"
75 "reg_mcfbahr %#x\n"
76 "reg_mcfapr %#x\n"
77 "reg_psr %#x\n",
78 mc_ccsr_regs->reg_gcr1,
79 mc_ccsr_regs->reg_gsr,
80 mc_ccsr_regs->reg_sicbalr,
81 mc_ccsr_regs->reg_sicbahr,
82 mc_ccsr_regs->reg_sicapr,
83 mc_ccsr_regs->reg_mcfbalr,
84 mc_ccsr_regs->reg_mcfbahr,
85 mc_ccsr_regs->reg_mcfapr,
86 mc_ccsr_regs->reg_psr);
87}
88#else
89
90#define dump_ram_words(title, addr)
91#define dump_mc_ccsr_regs(mc_ccsr_regs)
92
93#endif /* DEBUG */
94
95#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Rivera8ff14b72014-06-23 15:15:55 -070096/**
97 * Copying MC firmware or DPL image to DDR
98 */
99static int mc_copy_image(const char *title,
J. German Rivera43e4ae32015-01-06 13:19:02 -0800100 u64 image_addr, u32 image_size, u64 mc_ram_addr)
J. German Rivera8ff14b72014-06-23 15:15:55 -0700101{
102 debug("%s copied to address %p\n", title, (void *)mc_ram_addr);
103 memcpy((void *)mc_ram_addr, (void *)image_addr, image_size);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700104 flush_dcache_range(mc_ram_addr, mc_ram_addr + image_size);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700105 return 0;
106}
107
108/**
109 * MC firmware FIT image parser checks if the image is in FIT
110 * format, verifies integrity of the image and calculates
111 * raw image address and size values.
J. German Rivera43e4ae32015-01-06 13:19:02 -0800112 * Returns 0 on success and a negative errno on error.
J. German Rivera8ff14b72014-06-23 15:15:55 -0700113 * task fail.
114 **/
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530115int parse_mc_firmware_fit_image(u64 mc_fw_addr,
116 const void **raw_image_addr,
J. German Rivera8ff14b72014-06-23 15:15:55 -0700117 size_t *raw_image_size)
118{
119 int format;
120 void *fit_hdr;
121 int node_offset;
122 const void *data;
123 size_t size;
124 const char *uname = "firmware";
125
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530126 fit_hdr = (void *)mc_fw_addr;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700127
128 /* Check if Image is in FIT format */
129 format = genimg_get_format(fit_hdr);
130
131 if (format != IMAGE_FORMAT_FIT) {
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530132 printf("fsl-mc: ERR: Bad firmware image (not a FIT image)\n");
J. German Rivera43e4ae32015-01-06 13:19:02 -0800133 return -EINVAL;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700134 }
135
136 if (!fit_check_format(fit_hdr)) {
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530137 printf("fsl-mc: ERR: Bad firmware image (bad FIT header)\n");
J. German Rivera43e4ae32015-01-06 13:19:02 -0800138 return -EINVAL;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700139 }
140
141 node_offset = fit_image_get_node(fit_hdr, uname);
142
143 if (node_offset < 0) {
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530144 printf("fsl-mc: ERR: Bad firmware image (missing subimage)\n");
J. German Rivera43e4ae32015-01-06 13:19:02 -0800145 return -ENOENT;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700146 }
147
148 /* Verify MC firmware image */
149 if (!(fit_image_verify(fit_hdr, node_offset))) {
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530150 printf("fsl-mc: ERR: Bad firmware image (bad CRC)\n");
J. German Rivera43e4ae32015-01-06 13:19:02 -0800151 return -EINVAL;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700152 }
153
154 /* Get address and size of raw image */
155 fit_image_get_data(fit_hdr, node_offset, &data, &size);
156
157 *raw_image_addr = data;
158 *raw_image_size = size;
159
160 return 0;
161}
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700162#endif
163
Bogdan Purcareata72669142017-05-24 16:40:22 +0000164#define MC_DT_INCREASE_SIZE 64
165
166enum mc_fixup_type {
167 MC_FIXUP_DPL,
168 MC_FIXUP_DPC
169};
170
171static int mc_fixup_mac_addr(void *blob, int nodeoffset,
172 const char *propname, struct eth_device *eth_dev,
173 enum mc_fixup_type type)
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000174{
Bogdan Purcareata72669142017-05-24 16:40:22 +0000175 int err = 0, len = 0, size, i;
176 unsigned char env_enetaddr[ARP_HLEN];
177 unsigned int enetaddr_32[ARP_HLEN];
178 void *val = NULL;
179
180 switch (type) {
181 case MC_FIXUP_DPL:
182 /* DPL likes its addresses on 32 * ARP_HLEN bits */
183 for (i = 0; i < ARP_HLEN; i++)
184 enetaddr_32[i] = cpu_to_fdt32(eth_dev->enetaddr[i]);
185 val = enetaddr_32;
186 len = sizeof(enetaddr_32);
187 break;
188
189 case MC_FIXUP_DPC:
190 val = eth_dev->enetaddr;
191 len = ARP_HLEN;
192 break;
193 }
194
195 /* MAC address property present */
196 if (fdt_get_property(blob, nodeoffset, propname, NULL)) {
197 /* u-boot MAC addr randomly assigned - leave the present one */
Simon Glass399a9ce2017-08-03 12:22:14 -0600198 if (!eth_env_get_enetaddr_by_index("eth", eth_dev->index,
199 env_enetaddr))
Bogdan Purcareata72669142017-05-24 16:40:22 +0000200 return err;
201 } else {
202 size = MC_DT_INCREASE_SIZE + strlen(propname) + len;
203 /* make room for mac address property */
204 err = fdt_increase_size(blob, size);
205 if (err) {
206 printf("fdt_increase_size: err=%s\n",
207 fdt_strerror(err));
208 return err;
209 }
210 }
211
212 err = fdt_setprop(blob, nodeoffset, propname, val, len);
213 if (err) {
214 printf("fdt_setprop: err=%s\n", fdt_strerror(err));
215 return err;
216 }
217
218 return err;
219}
220
221#define is_dpni(s) (s != NULL ? !strncmp(s, "dpni@", 5) : 0)
222
223const char *dpl_get_connection_endpoint(void *blob, char *endpoint)
224{
225 int connoffset = fdt_path_offset(blob, "/connections"), off;
226 const char *s1, *s2;
227
228 for (off = fdt_first_subnode(blob, connoffset);
229 off >= 0;
230 off = fdt_next_subnode(blob, off)) {
231 s1 = fdt_stringlist_get(blob, off, "endpoint1", 0, NULL);
232 s2 = fdt_stringlist_get(blob, off, "endpoint2", 0, NULL);
233
234 if (!s1 || !s2)
235 continue;
236
237 if (strcmp(endpoint, s1) == 0)
238 return s2;
239
240 if (strcmp(endpoint, s2) == 0)
241 return s1;
242 }
243
244 return NULL;
245}
246
247static int mc_fixup_dpl_mac_addr(void *blob, int dpmac_id,
248 struct eth_device *eth_dev)
249{
250 int objoff = fdt_path_offset(blob, "/objects");
251 int dpmacoff = -1, dpnioff = -1;
252 const char *endpoint;
253 char mac_name[10];
254 int err;
255
256 sprintf(mac_name, "dpmac@%d", dpmac_id);
257 dpmacoff = fdt_subnode_offset(blob, objoff, mac_name);
258 if (dpmacoff < 0)
259 /* dpmac not defined in DPL, so skip it. */
260 return 0;
261
262 err = mc_fixup_mac_addr(blob, dpmacoff, "mac_addr", eth_dev,
263 MC_FIXUP_DPL);
264 if (err) {
265 printf("Error fixing up dpmac mac_addr in DPL\n");
266 return err;
267 }
268
269 /* now we need to figure out if there is any
270 * DPNI connected to this MAC, so we walk the
271 * connection list
272 */
273 endpoint = dpl_get_connection_endpoint(blob, mac_name);
274 if (!is_dpni(endpoint))
275 return 0;
276
277 /* let's see if we can fixup the DPNI as well */
278 dpnioff = fdt_subnode_offset(blob, objoff, endpoint);
279 if (dpnioff < 0)
280 /* DPNI not defined in DPL in the objects area */
281 return 0;
282
283 return mc_fixup_mac_addr(blob, dpnioff, "mac_addr", eth_dev,
284 MC_FIXUP_DPL);
285}
286
Meenakshi Aggarwald67ae482019-05-23 15:13:43 +0530287void fdt_fixup_mc_ddr(u64 *base, u64 *size)
288{
289 u64 mc_size = mc_get_dram_block_size();
290
291 if (mc_size < MC_DRAM_BLOCK_DEFAULT_SIZE) {
292 *base = mc_get_dram_addr() + mc_size;
293 *size = MC_DRAM_BLOCK_DEFAULT_SIZE - mc_size;
294 }
295}
296
Nipun Guptad6912642018-08-20 16:01:14 +0530297void fdt_fsl_mc_fixup_iommu_map_entry(void *blob)
298{
299 u32 *prop;
300 u32 iommu_map[4];
301 int offset;
302 int lenp;
303
304 /* find fsl-mc node */
305 offset = fdt_path_offset(blob, "/soc/fsl-mc");
306 if (offset < 0)
307 offset = fdt_path_offset(blob, "/fsl-mc");
308 if (offset < 0) {
309 printf("%s: fsl-mc: ERR: fsl-mc node not found in DT, err %d\n",
310 __func__, offset);
311 return;
312 }
313
314 prop = fdt_getprop_w(blob, offset, "iommu-map", &lenp);
315 if (!prop) {
316 debug("%s: fsl-mc: ERR: missing iommu-map in fsl-mc bus node\n",
317 __func__);
318 return;
319 }
320
321 iommu_map[0] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_START);
322 iommu_map[1] = *++prop;
323 iommu_map[2] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_START);
324 iommu_map[3] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_END -
325 FSL_DPAA2_STREAM_ID_START + 1);
326
327 fdt_setprop_inplace(blob, offset, "iommu-map",
328 iommu_map, sizeof(iommu_map));
329}
330
Bogdan Purcareata72669142017-05-24 16:40:22 +0000331static int mc_fixup_dpc_mac_addr(void *blob, int dpmac_id,
332 struct eth_device *eth_dev)
333{
334 int nodeoffset = fdt_path_offset(blob, "/board_info/ports"), noff;
335 int err = 0;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000336 char mac_name[10];
Bogdan Purcareata72669142017-05-24 16:40:22 +0000337 const char link_type_mode[] = "MAC_LINK_TYPE_FIXED";
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000338
339 sprintf(mac_name, "mac@%d", dpmac_id);
340
341 /* node not found - create it */
Bogdan Purcareata72669142017-05-24 16:40:22 +0000342 noff = fdt_subnode_offset(blob, nodeoffset, (const char *)mac_name);
343 if (noff < 0) {
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000344 err = fdt_increase_size(blob, 200);
345 if (err) {
346 printf("fdt_increase_size: err=%s\n",
347 fdt_strerror(err));
348 return err;
349 }
350
Bogdan Purcareata72669142017-05-24 16:40:22 +0000351 noff = fdt_add_subnode(blob, nodeoffset, mac_name);
352 if (noff < 0) {
353 printf("fdt_add_subnode: err=%s\n",
354 fdt_strerror(err));
355 return err;
356 }
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000357
358 /* add default property of fixed link */
Bogdan Purcareata72669142017-05-24 16:40:22 +0000359 err = fdt_appendprop_string(blob, noff,
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000360 "link_type", link_type_mode);
361 if (err) {
362 printf("fdt_appendprop_string: err=%s\n",
363 fdt_strerror(err));
364 return err;
365 }
366 }
367
Bogdan Purcareata72669142017-05-24 16:40:22 +0000368 return mc_fixup_mac_addr(blob, noff, "port_mac_address", eth_dev,
369 MC_FIXUP_DPC);
370}
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000371
Bogdan Purcareata72669142017-05-24 16:40:22 +0000372static int mc_fixup_mac_addrs(void *blob, enum mc_fixup_type type)
373{
374 int i, err = 0, ret = 0;
Pankaj Bansal8a921702018-08-02 16:31:28 +0530375 char ethname[ETH_NAME_LEN];
Bogdan Purcareata72669142017-05-24 16:40:22 +0000376 struct eth_device *eth_dev;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000377
Bogdan Purcareata72669142017-05-24 16:40:22 +0000378 for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++) {
379 /* port not enabled */
Pankaj Bansal7987ab72018-10-10 14:08:33 +0530380 if (wriop_is_enabled_dpmac(i) != 1)
Bogdan Purcareata72669142017-05-24 16:40:22 +0000381 continue;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000382
Pankaj Bansal8a921702018-08-02 16:31:28 +0530383 snprintf(ethname, ETH_NAME_LEN, "DPMAC%d@%s", i,
384 phy_interface_strings[wriop_get_enet_if(i)]);
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000385
Bogdan Purcareata72669142017-05-24 16:40:22 +0000386 eth_dev = eth_get_dev_by_name(ethname);
387 if (eth_dev == NULL)
388 continue;
389
390 switch (type) {
391 case MC_FIXUP_DPL:
392 err = mc_fixup_dpl_mac_addr(blob, i, eth_dev);
393 break;
394 case MC_FIXUP_DPC:
395 err = mc_fixup_dpc_mac_addr(blob, i, eth_dev);
396 break;
397 default:
398 break;
399 }
400
401 if (err)
402 printf("fsl-mc: ERROR fixing mac address for %s\n",
403 ethname);
404 ret |= err;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000405 }
406
Bogdan Purcareata72669142017-05-24 16:40:22 +0000407 return ret;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000408}
409
Stuart Yodera2bdb772015-07-02 11:29:03 +0530410static int mc_fixup_dpc(u64 dpc_addr)
411{
412 void *blob = (void *)dpc_addr;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000413 int nodeoffset, err = 0;
Stuart Yodera2bdb772015-07-02 11:29:03 +0530414
415 /* delete any existing ICID pools */
416 nodeoffset = fdt_path_offset(blob, "/resources/icid_pools");
417 if (fdt_del_node(blob, nodeoffset) < 0)
418 printf("\nfsl-mc: WARNING: could not delete ICID pool\n");
419
420 /* add a new pool */
421 nodeoffset = fdt_path_offset(blob, "/resources");
422 if (nodeoffset < 0) {
423 printf("\nfsl-mc: ERROR: DPC is missing /resources\n");
424 return -EINVAL;
425 }
426 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pools");
427 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pool@0");
428 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
429 "base_icid", FSL_DPAA2_STREAM_ID_START, 1);
430 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
431 "num",
432 FSL_DPAA2_STREAM_ID_END -
433 FSL_DPAA2_STREAM_ID_START + 1, 1);
434
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000435 /* fixup MAC addresses for dpmac ports */
436 nodeoffset = fdt_path_offset(blob, "/board_info/ports");
437 if (nodeoffset < 0)
Ioana Ciocoi Radulescud7697f42019-02-26 15:50:07 +0000438 goto out;
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000439
Bogdan Purcareata72669142017-05-24 16:40:22 +0000440 err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPC);
Ioana Ciocoi Radulescud7697f42019-02-26 15:50:07 +0000441
442out:
Stuart Yodera2bdb772015-07-02 11:29:03 +0530443 flush_dcache_range(dpc_addr, dpc_addr + fdt_totalsize(blob));
444
Bogdan Purcareatabea8abd2017-01-11 15:58:36 +0000445 return err;
Stuart Yodera2bdb772015-07-02 11:29:03 +0530446}
447
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530448static int load_mc_dpc(u64 mc_ram_addr, size_t mc_ram_size, u64 mc_dpc_addr)
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700449{
450 u64 mc_dpc_offset;
451#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
452 int error;
453 void *dpc_fdt_hdr;
454 int dpc_size;
455#endif
456
457#ifdef CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET
458 BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET & 0x3) != 0 ||
459 CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET > 0xffffffff);
460
461 mc_dpc_offset = CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET;
462#else
463#error "CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET not defined"
464#endif
465
466 /*
467 * Load the MC DPC blob in the MC private DRAM block:
468 */
469#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
470 printf("MC DPC is preloaded to %#llx\n", mc_ram_addr + mc_dpc_offset);
471#else
472 /*
473 * Get address and size of the DPC blob stored in flash:
474 */
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530475 dpc_fdt_hdr = (void *)mc_dpc_addr;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700476
477 error = fdt_check_header(dpc_fdt_hdr);
478 if (error != 0) {
479 /*
480 * Don't return with error here, since the MC firmware can
481 * still boot without a DPC
482 */
J. German Rivera75d7ace2015-07-02 11:28:56 +0530483 printf("\nfsl-mc: WARNING: No DPC image found");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700484 return 0;
485 }
486
487 dpc_size = fdt_totalsize(dpc_fdt_hdr);
488 if (dpc_size > CONFIG_SYS_LS_MC_DPC_MAX_LENGTH) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530489 printf("\nfsl-mc: ERROR: Bad DPC image (too large: %d)\n",
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700490 dpc_size);
491 return -EINVAL;
492 }
493
494 mc_copy_image("MC DPC blob",
495 (u64)dpc_fdt_hdr, dpc_size, mc_ram_addr + mc_dpc_offset);
496#endif /* not defined CONFIG_SYS_LS_MC_DPC_IN_DDR */
497
Stuart Yodera2bdb772015-07-02 11:29:03 +0530498 if (mc_fixup_dpc(mc_ram_addr + mc_dpc_offset))
499 return -EINVAL;
500
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700501 dump_ram_words("DPC", (void *)(mc_ram_addr + mc_dpc_offset));
502 return 0;
503}
504
Bogdan Purcareata72669142017-05-24 16:40:22 +0000505
506static int mc_fixup_dpl(u64 dpl_addr)
507{
508 void *blob = (void *)dpl_addr;
509 u32 ver = fdt_getprop_u32_default(blob, "/", "dpl-version", 0);
510 int err = 0;
511
512 /* The DPL fixup for mac addresses is only relevant
513 * for old-style DPLs
514 */
515 if (ver >= 10)
516 return 0;
517
518 err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPL);
519 flush_dcache_range(dpl_addr, dpl_addr + fdt_totalsize(blob));
520
521 return err;
522}
523
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530524static int load_mc_dpl(u64 mc_ram_addr, size_t mc_ram_size, u64 mc_dpl_addr)
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700525{
526 u64 mc_dpl_offset;
527#ifndef CONFIG_SYS_LS_MC_DPL_IN_DDR
528 int error;
529 void *dpl_fdt_hdr;
530 int dpl_size;
531#endif
532
533#ifdef CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET
534 BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET & 0x3) != 0 ||
535 CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET > 0xffffffff);
536
537 mc_dpl_offset = CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET;
538#else
539#error "CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET not defined"
540#endif
541
542 /*
543 * Load the MC DPL blob in the MC private DRAM block:
544 */
545#ifdef CONFIG_SYS_LS_MC_DPL_IN_DDR
546 printf("MC DPL is preloaded to %#llx\n", mc_ram_addr + mc_dpl_offset);
547#else
548 /*
549 * Get address and size of the DPL blob stored in flash:
550 */
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530551 dpl_fdt_hdr = (void *)mc_dpl_addr;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700552
553 error = fdt_check_header(dpl_fdt_hdr);
554 if (error != 0) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530555 printf("\nfsl-mc: ERROR: Bad DPL image (bad header)\n");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700556 return error;
557 }
558
559 dpl_size = fdt_totalsize(dpl_fdt_hdr);
560 if (dpl_size > CONFIG_SYS_LS_MC_DPL_MAX_LENGTH) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530561 printf("\nfsl-mc: ERROR: Bad DPL image (too large: %d)\n",
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700562 dpl_size);
563 return -EINVAL;
564 }
565
566 mc_copy_image("MC DPL blob",
567 (u64)dpl_fdt_hdr, dpl_size, mc_ram_addr + mc_dpl_offset);
568#endif /* not defined CONFIG_SYS_LS_MC_DPL_IN_DDR */
569
Bogdan Purcareata72669142017-05-24 16:40:22 +0000570 if (mc_fixup_dpl(mc_ram_addr + mc_dpl_offset))
571 return -EINVAL;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700572 dump_ram_words("DPL", (void *)(mc_ram_addr + mc_dpl_offset));
573 return 0;
574}
575
576/**
577 * Return the MC boot timeout value in milliseconds
578 */
579static unsigned long get_mc_boot_timeout_ms(void)
580{
581 unsigned long timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
582
Simon Glass64b723f2017-08-03 12:22:12 -0600583 char *timeout_ms_env_var = env_get(MC_BOOT_TIMEOUT_ENV_VAR);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700584
585 if (timeout_ms_env_var) {
586 timeout_ms = simple_strtoul(timeout_ms_env_var, NULL, 10);
587 if (timeout_ms == 0) {
588 printf("fsl-mc: WARNING: Invalid value for \'"
589 MC_BOOT_TIMEOUT_ENV_VAR
590 "\' environment variable: %lu\n",
591 timeout_ms);
592
593 timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
594 }
595 }
596
597 return timeout_ms;
598}
599
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530600#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
York Suncbe8e1c2016-04-04 11:41:26 -0700601
602__weak bool soc_has_aiop(void)
603{
604 return false;
605}
606
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530607static int load_mc_aiop_img(u64 aiop_fw_addr)
J. German Riverac3b505f2015-07-02 11:28:58 +0530608{
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530609 u64 mc_ram_addr = mc_get_dram_addr();
610#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
J. German Riverac3b505f2015-07-02 11:28:58 +0530611 void *aiop_img;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530612#endif
J. German Riverac3b505f2015-07-02 11:28:58 +0530613
York Suncbe8e1c2016-04-04 11:41:26 -0700614 /* Check if AIOP is available */
615 if (!soc_has_aiop())
616 return -ENODEV;
J. German Riverac3b505f2015-07-02 11:28:58 +0530617 /*
618 * Load the MC AIOP image in the MC private DRAM block:
619 */
620
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530621#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
622 printf("MC AIOP is preloaded to %#llx\n", mc_ram_addr +
623 CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
624#else
625 aiop_img = (void *)aiop_fw_addr;
J. German Riverac3b505f2015-07-02 11:28:58 +0530626 mc_copy_image("MC AIOP image",
627 (u64)aiop_img, CONFIG_SYS_LS_MC_AIOP_IMG_MAX_LENGTH,
628 mc_ram_addr + CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530629#endif
630 mc_aiop_applied = 0;
J. German Riverac3b505f2015-07-02 11:28:58 +0530631
632 return 0;
633}
634#endif
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530635
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700636static int wait_for_mc(bool booting_mc, u32 *final_reg_gsr)
637{
638 u32 reg_gsr;
639 u32 mc_fw_boot_status;
640 unsigned long timeout_ms = get_mc_boot_timeout_ms();
641 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
642
643 dmb();
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700644 assert(timeout_ms > 0);
645 for (;;) {
646 udelay(1000); /* throttle polling */
647 reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
648 mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
649 if (mc_fw_boot_status & 0x1)
650 break;
651
652 timeout_ms--;
653 if (timeout_ms == 0)
654 break;
655 }
656
657 if (timeout_ms == 0) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530658 printf("ERROR: timeout\n");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700659
660 /* TODO: Get an error status from an MC CCSR register */
661 return -ETIMEDOUT;
662 }
663
664 if (mc_fw_boot_status != 0x1) {
665 /*
666 * TODO: Identify critical errors from the GSR register's FS
667 * field and for those errors, set error to -ENODEV or other
668 * appropriate errno, so that the status property is set to
669 * failure in the fsl,dprc device tree node.
670 */
J. German Rivera75d7ace2015-07-02 11:28:56 +0530671 printf("WARNING: Firmware returned an error (GSR: %#x)\n",
672 reg_gsr);
673 } else {
674 printf("SUCCESS\n");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700675 }
676
J. German Rivera75d7ace2015-07-02 11:28:56 +0530677
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700678 *final_reg_gsr = reg_gsr;
679 return 0;
680}
J. German Rivera8ff14b72014-06-23 15:15:55 -0700681
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530682int mc_init(u64 mc_fw_addr, u64 mc_dpc_addr)
J. German Rivera8ff14b72014-06-23 15:15:55 -0700683{
684 int error = 0;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700685 int portal_id = 0;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700686 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530687 u64 mc_ram_addr = mc_get_dram_addr();
J. German Rivera8ff14b72014-06-23 15:15:55 -0700688 u32 reg_gsr;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700689 u32 reg_mcfbalr;
690#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Rivera8ff14b72014-06-23 15:15:55 -0700691 const void *raw_image_addr;
692 size_t raw_image_size = 0;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700693#endif
J. German Rivera43e4ae32015-01-06 13:19:02 -0800694 struct mc_version mc_ver_info;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700695 u8 mc_ram_num_256mb_blocks;
696 size_t mc_ram_size = mc_get_dram_block_size();
J. German Rivera8ff14b72014-06-23 15:15:55 -0700697
York Sunc55d7312017-03-06 09:02:29 -0800698 mc_ram_num_256mb_blocks = mc_ram_size / MC_RAM_SIZE_ALIGNMENT;
Meenakshi Aggarwal67f195c2019-02-27 14:41:02 +0530699
700 if (mc_ram_num_256mb_blocks >= 0xff) {
York Sunc55d7312017-03-06 09:02:29 -0800701 error = -EINVAL;
702 printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
703 mc_ram_size);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700704 goto out;
York Sunc55d7312017-03-06 09:02:29 -0800705 }
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700706
J. German Rivera8ff14b72014-06-23 15:15:55 -0700707 /*
Meenakshi Aggarwal67f195c2019-02-27 14:41:02 +0530708 * To support 128 MB DDR Size for MC
709 */
710 if (mc_ram_num_256mb_blocks == 0)
711 mc_ram_num_256mb_blocks = 0xFF;
712
713 /*
J. German Rivera8ff14b72014-06-23 15:15:55 -0700714 * Management Complex cores should be held at reset out of POR.
Bin Meng75574052016-02-05 19:30:11 -0800715 * U-Boot should be the first software to touch MC. To be safe,
J. German Rivera8ff14b72014-06-23 15:15:55 -0700716 * we reset all cores again by setting GCR1 to 0. It doesn't do
717 * anything if they are held at reset. After we setup the firmware
718 * we kick off MC by deasserting the reset bit for core 0, and
719 * deasserting the reset bits for Command Portal Managers.
720 * The stop bits are not touched here. They are used to stop the
721 * cores when they are active. Setting stop bits doesn't stop the
722 * cores from fetching instructions when they are released from
723 * reset.
724 */
725 out_le32(&mc_ccsr_regs->reg_gcr1, 0);
726 dmb();
727
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700728#ifdef CONFIG_SYS_LS_MC_FW_IN_DDR
729 printf("MC firmware is preloaded to %#llx\n", mc_ram_addr);
730#else
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530731 error = parse_mc_firmware_fit_image(mc_fw_addr, &raw_image_addr,
732 &raw_image_size);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700733 if (error != 0)
734 goto out;
735 /*
736 * Load the MC FW at the beginning of the MC private DRAM block:
737 */
J. German Rivera43e4ae32015-01-06 13:19:02 -0800738 mc_copy_image("MC Firmware",
739 (u64)raw_image_addr, raw_image_size, mc_ram_addr);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800740#endif
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700741 dump_ram_words("firmware", (void *)mc_ram_addr);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800742
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530743 error = load_mc_dpc(mc_ram_addr, mc_ram_size, mc_dpc_addr);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700744 if (error != 0)
J. German Rivera8ff14b72014-06-23 15:15:55 -0700745 goto out;
J. German Riverac3b505f2015-07-02 11:28:58 +0530746
J. German Rivera8ff14b72014-06-23 15:15:55 -0700747 debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700748 dump_mc_ccsr_regs(mc_ccsr_regs);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700749
750 /*
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700751 * Tell MC what is the address range of the DRAM block assigned to it:
J. German Rivera8ff14b72014-06-23 15:15:55 -0700752 */
Meenakshi Aggarwal67f195c2019-02-27 14:41:02 +0530753 if (mc_ram_num_256mb_blocks < 0xFF) {
754 reg_mcfbalr = (u32)mc_ram_addr |
755 (mc_ram_num_256mb_blocks - 1);
756 } else {
757 reg_mcfbalr = (u32)mc_ram_addr |
758 (mc_ram_num_256mb_blocks);
759 }
760
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700761 out_le32(&mc_ccsr_regs->reg_mcfbalr, reg_mcfbalr);
762 out_le32(&mc_ccsr_regs->reg_mcfbahr,
York Sunc55d7312017-03-06 09:02:29 -0800763 (u32)(mc_ram_addr >> 32));
Stuart Yoderd1970352015-07-02 11:29:02 +0530764 out_le32(&mc_ccsr_regs->reg_mcfapr, FSL_BYPASS_AMQ);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700765
766 /*
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700767 * Tell the MC that we want delayed DPL deployment.
J. German Rivera8ff14b72014-06-23 15:15:55 -0700768 */
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700769 out_le32(&mc_ccsr_regs->reg_gsr, 0xDD00);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700770
J. German Rivera75d7ace2015-07-02 11:28:56 +0530771 printf("\nfsl-mc: Booting Management Complex ... ");
J. German Rivera43e4ae32015-01-06 13:19:02 -0800772
J. German Rivera8ff14b72014-06-23 15:15:55 -0700773 /*
774 * Deassert reset and release MC core 0 to run
775 */
776 out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700777 error = wait_for_mc(true, &reg_gsr);
778 if (error != 0)
J. German Rivera8ff14b72014-06-23 15:15:55 -0700779 goto out;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700780
J. German Rivera43e4ae32015-01-06 13:19:02 -0800781 /*
782 * TODO: need to obtain the portal_id for the root container from the
783 * DPL
784 */
785 portal_id = 0;
786
787 /*
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700788 * Initialize the global default MC portal
789 * And check that the MC firmware is responding portal commands:
J. German Rivera43e4ae32015-01-06 13:19:02 -0800790 */
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +0530791 root_mc_io = (struct fsl_mc_io *)calloc(sizeof(struct fsl_mc_io), 1);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530792 if (!root_mc_io) {
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +0530793 printf(" No memory: calloc() failed\n");
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700794 return -ENOMEM;
795 }
796
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530797 root_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(portal_id);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800798 debug("Checking access to MC portal of root DPRC container (portal_id %d, portal physical addr %p)\n",
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530799 portal_id, root_mc_io->mmio_regs);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800800
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530801 error = mc_get_version(root_mc_io, MC_CMD_NO_FLAGS, &mc_ver_info);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800802 if (error != 0) {
803 printf("fsl-mc: ERROR: Firmware version check failed (error: %d)\n",
804 error);
805 goto out;
806 }
807
J. German Rivera43e4ae32015-01-06 13:19:02 -0800808 printf("fsl-mc: Management Complex booted (version: %d.%d.%d, boot status: %#x)\n",
809 mc_ver_info.major, mc_ver_info.minor, mc_ver_info.revision,
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700810 reg_gsr & GSR_FS_MASK);
811
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530812out:
813 if (error != 0)
814 mc_boot_status = error;
815 else
816 mc_boot_status = 0;
817
818 return error;
819}
820
821int mc_apply_dpl(u64 mc_dpl_addr)
822{
823 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
824 int error = 0;
825 u32 reg_gsr;
826 u64 mc_ram_addr = mc_get_dram_addr();
827 size_t mc_ram_size = mc_get_dram_block_size();
828
Alexander Graf2ebeb442016-11-17 01:02:57 +0100829 if (!mc_dpl_addr)
830 return -1;
831
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530832 error = load_mc_dpl(mc_ram_addr, mc_ram_size, mc_dpl_addr);
833 if (error != 0)
834 return error;
835
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700836 /*
837 * Tell the MC to deploy the DPL:
838 */
839 out_le32(&mc_ccsr_regs->reg_gsr, 0x0);
J. German Rivera75d7ace2015-07-02 11:28:56 +0530840 printf("fsl-mc: Deploying data path layout ... ");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700841 error = wait_for_mc(false, &reg_gsr);
J. German Rivera75d7ace2015-07-02 11:28:56 +0530842
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530843 if (!error)
844 mc_dpl_applied = 0;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700845
846 return error;
847}
848
849int get_mc_boot_status(void)
850{
851 return mc_boot_status;
852}
853
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530854#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
855int get_aiop_apply_status(void)
856{
857 return mc_aiop_applied;
858}
859#endif
860
861int get_dpl_apply_status(void)
862{
863 return mc_dpl_applied;
864}
865
Mian Yousaf Kaukab97124652018-12-18 14:01:17 +0100866int is_lazy_dpl_addr_valid(void)
867{
868 return !!mc_lazy_dpl_addr;
869}
870
Priyanka Jaine0e0be52017-08-24 16:42:43 +0530871/*
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530872 * Return the MC address of private DRAM block.
Priyanka Jaine0e0be52017-08-24 16:42:43 +0530873 * As per MC design document, MC initial base address
874 * should be least significant 512MB address of MC private
875 * memory, i.e. address should point to end address masked
876 * with 512MB offset in private DRAM block.
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530877 */
878u64 mc_get_dram_addr(void)
879{
Priyanka Jaine0e0be52017-08-24 16:42:43 +0530880 size_t mc_ram_size = mc_get_dram_block_size();
881
Prabhakar Kushwaha145e4cd2018-08-27 12:58:52 +0530882 if (!mc_memset_resv_ram || (get_mc_boot_status() < 0)) {
883 mc_memset_resv_ram = 1;
884 memset((void *)gd->arch.resv_ram, 0, mc_ram_size);
885 }
886
Priyanka Jaine0e0be52017-08-24 16:42:43 +0530887 return (gd->arch.resv_ram + mc_ram_size - 1) &
888 MC_RAM_BASE_ADDR_ALIGNMENT_MASK;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530889}
890
891/**
J. German Rivera8ff14b72014-06-23 15:15:55 -0700892 * Return the actual size of the MC private DRAM block.
J. German Rivera8ff14b72014-06-23 15:15:55 -0700893 */
894unsigned long mc_get_dram_block_size(void)
895{
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700896 unsigned long dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
897
Simon Glass64b723f2017-08-03 12:22:12 -0600898 char *dram_block_size_env_var = env_get(MC_MEM_SIZE_ENV_VAR);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700899
900 if (dram_block_size_env_var) {
901 dram_block_size = simple_strtoul(dram_block_size_env_var, NULL,
Prabhakar Kushwahab166d6a2017-11-09 14:45:41 +0530902 16);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700903
904 if (dram_block_size < CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE) {
905 printf("fsl-mc: WARNING: Invalid value for \'"
906 MC_MEM_SIZE_ENV_VAR
907 "\' environment variable: %lu\n",
908 dram_block_size);
909
Meenakshi Aggarwal67f195c2019-02-27 14:41:02 +0530910 dram_block_size = MC_DRAM_BLOCK_DEFAULT_SIZE;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700911 }
912 }
913
914 return dram_block_size;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700915}
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700916
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530917int fsl_mc_ldpaa_init(bd_t *bis)
918{
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530919 int i;
920
921 for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++)
Pankaj Bansal7987ab72018-10-10 14:08:33 +0530922 if (wriop_is_enabled_dpmac(i) == 1)
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530923 ldpaa_eth_init(i, wriop_get_enet_if(i));
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530924 return 0;
925}
926
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530927static int dprc_version_check(struct fsl_mc_io *mc_io, uint16_t handle)
928{
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530929 int error;
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530930 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530931
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530932 error = dprc_get_api_version(mc_io, 0,
933 &major_ver,
934 &minor_ver);
935 if (error < 0) {
936 printf("dprc_get_api_version() failed: %d\n", error);
937 return error;
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530938 }
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530939
940 if (major_ver < DPRC_VER_MAJOR || (major_ver == DPRC_VER_MAJOR &&
941 minor_ver < DPRC_VER_MINOR)) {
942 printf("DPRC version mismatch found %u.%u,",
943 major_ver, minor_ver);
944 printf("supported version is %u.%u\n",
945 DPRC_VER_MAJOR, DPRC_VER_MINOR);
946 }
947
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530948 return error;
949}
950
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530951static int dpio_init(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700952{
953 struct qbman_swp_desc p_des;
954 struct dpio_attr attr;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530955 struct dpio_cfg dpio_cfg;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700956 int err = 0;
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530957 uint16_t major_ver, minor_ver;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700958
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +0530959 dflt_dpio = (struct fsl_dpio_obj *)calloc(
960 sizeof(struct fsl_dpio_obj), 1);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700961 if (!dflt_dpio) {
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +0530962 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530963 err = -ENOMEM;
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +0530964 goto err_calloc;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700965 }
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530966 dpio_cfg.channel_mode = DPIO_LOCAL_CHANNEL;
967 dpio_cfg.num_priorities = 8;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700968
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530969 err = dpio_create(dflt_mc_io,
970 dflt_dprc_handle,
971 MC_CMD_NO_FLAGS,
972 &dpio_cfg,
973 &dflt_dpio->dpio_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530974 if (err < 0) {
975 printf("dpio_create() failed: %d\n", err);
976 err = -ENODEV;
977 goto err_create;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700978 }
979
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530980 err = dpio_get_api_version(dflt_mc_io, 0,
981 &major_ver,
982 &minor_ver);
983 if (err < 0) {
984 printf("dpio_get_api_version() failed: %d\n", err);
985 goto err_get_api_ver;
986 }
987
988 if (major_ver < DPIO_VER_MAJOR || (major_ver == DPIO_VER_MAJOR &&
989 minor_ver < DPIO_VER_MINOR)) {
990 printf("DPRC version mismatch found %u.%u,",
991 major_ver,
992 minor_ver);
993 }
994
995 err = dpio_open(dflt_mc_io,
996 MC_CMD_NO_FLAGS,
997 dflt_dpio->dpio_id,
998 &dflt_dpio->dpio_handle);
999 if (err) {
1000 printf("dpio_open() failed\n");
1001 goto err_open;
1002 }
1003
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301004 memset(&attr, 0, sizeof(struct dpio_attr));
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +05301005 err = dpio_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301006 dflt_dpio->dpio_handle, &attr);
1007 if (err < 0) {
1008 printf("dpio_get_attributes() failed: %d\n", err);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001009 goto err_get_attr;
1010 }
1011
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301012 if (dflt_dpio->dpio_id != attr.id) {
1013 printf("dnpi object id and attribute id are not same\n");
1014 goto err_attr_not_same;
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301015 }
1016
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301017#ifdef DEBUG
1018 printf("Init: DPIO id=0x%d\n", dflt_dpio->dpio_id);
1019#endif
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301020 err = dpio_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1021 if (err < 0) {
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001022 printf("dpio_enable() failed %d\n", err);
1023 goto err_get_enable;
1024 }
Prabhakar Kushwahafd5d1272015-07-02 11:28:59 +05301025 debug("ce_offset=0x%llx, ci_offset=0x%llx, portalid=%d, prios=%d\n",
1026 attr.qbman_portal_ce_offset,
1027 attr.qbman_portal_ci_offset,
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001028 attr.qbman_portal_id,
1029 attr.num_priorities);
1030
Prabhakar Kushwahafd5d1272015-07-02 11:28:59 +05301031 p_des.cena_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
1032 + attr.qbman_portal_ce_offset);
1033 p_des.cinh_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
1034 + attr.qbman_portal_ci_offset);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001035
1036 dflt_dpio->sw_portal = qbman_swp_init(&p_des);
1037 if (dflt_dpio->sw_portal == NULL) {
1038 printf("qbman_swp_init() failed\n");
1039 goto err_get_swp_init;
1040 }
1041 return 0;
1042
1043err_get_swp_init:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301044 dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001045err_get_enable:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301046err_get_attr:
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301047err_attr_not_same:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301048 dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301049err_open:
1050err_get_api_ver:
1051 dpio_destroy(dflt_mc_io,
1052 dflt_dprc_handle,
1053 MC_CMD_NO_FLAGS,
1054 dflt_dpio->dpio_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301055err_create:
Prabhakar Kushwahac6018bd2016-03-18 16:15:29 +05301056 free(dflt_dpio);
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301057err_calloc:
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001058 return err;
1059}
1060
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301061static int dpio_exit(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001062{
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301063 int err;
1064
1065 err = dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1066 if (err < 0) {
1067 printf("dpio_disable() failed: %d\n", err);
1068 goto err;
1069 }
1070
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301071 dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1072 if (err < 0) {
1073 printf("dpio_close() failed: %d\n", err);
1074 goto err;
1075 }
1076
1077 err = dpio_destroy(dflt_mc_io,
1078 dflt_dprc_handle,
1079 MC_CMD_NO_FLAGS,
1080 dflt_dpio->dpio_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301081 if (err < 0) {
1082 printf("dpio_destroy() failed: %d\n", err);
1083 goto err;
1084 }
1085
1086#ifdef DEBUG
1087 printf("Exit: DPIO id=0x%d\n", dflt_dpio->dpio_id);
1088#endif
1089
1090 if (dflt_dpio)
1091 free(dflt_dpio);
1092
1093 return 0;
1094err:
1095 return err;
1096}
1097
1098static int dprc_init(void)
1099{
1100 int err, child_portal_id, container_id;
1101 struct dprc_cfg cfg;
1102 uint64_t mc_portal_offset;
1103
1104 /* Open root container */
1105 err = dprc_get_container_id(root_mc_io, MC_CMD_NO_FLAGS, &container_id);
1106 if (err < 0) {
1107 printf("dprc_get_container_id(): Root failed: %d\n", err);
1108 goto err_root_container_id;
1109 }
1110
1111#ifdef DEBUG
1112 printf("Root container id = %d\n", container_id);
1113#endif
1114 err = dprc_open(root_mc_io, MC_CMD_NO_FLAGS, container_id,
1115 &root_dprc_handle);
1116 if (err < 0) {
1117 printf("dprc_open(): Root Container failed: %d\n", err);
1118 goto err_root_open;
1119 }
1120
1121 if (!root_dprc_handle) {
1122 printf("dprc_open(): Root Container Handle is not valid\n");
1123 goto err_root_open;
1124 }
1125
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301126 err = dprc_version_check(root_mc_io, root_dprc_handle);
1127 if (err < 0) {
1128 printf("dprc_version_check() failed: %d\n", err);
1129 goto err_root_open;
1130 }
1131
Prabhakar Kushwaha901a88e2016-01-20 12:04:19 +05301132 memset(&cfg, 0, sizeof(struct dprc_cfg));
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301133 cfg.options = DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED |
1134 DPRC_CFG_OPT_OBJ_CREATE_ALLOWED |
1135 DPRC_CFG_OPT_ALLOC_ALLOWED;
1136 cfg.icid = DPRC_GET_ICID_FROM_POOL;
Prabhakar Kushwaha7fd59d92015-12-24 15:33:49 +05301137 cfg.portal_id = DPRC_GET_PORTAL_ID_FROM_POOL;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301138 err = dprc_create_container(root_mc_io, MC_CMD_NO_FLAGS,
1139 root_dprc_handle,
1140 &cfg,
1141 &child_dprc_id,
1142 &mc_portal_offset);
1143 if (err < 0) {
1144 printf("dprc_create_container() failed: %d\n", err);
1145 goto err_create;
1146 }
1147
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301148 dflt_mc_io = (struct fsl_mc_io *)calloc(sizeof(struct fsl_mc_io), 1);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301149 if (!dflt_mc_io) {
1150 err = -ENOMEM;
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301151 printf(" No memory: calloc() failed\n");
1152 goto err_calloc;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301153 }
1154
1155 child_portal_id = MC_PORTAL_OFFSET_TO_PORTAL_ID(mc_portal_offset);
1156 dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(child_portal_id);
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301157
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301158#ifdef DEBUG
1159 printf("MC portal of child DPRC container: %d, physical addr %p)\n",
1160 child_dprc_id, dflt_mc_io->mmio_regs);
1161#endif
1162
1163 err = dprc_open(dflt_mc_io, MC_CMD_NO_FLAGS, child_dprc_id,
1164 &dflt_dprc_handle);
1165 if (err < 0) {
1166 printf("dprc_open(): Child container failed: %d\n", err);
1167 goto err_child_open;
1168 }
1169
1170 if (!dflt_dprc_handle) {
1171 printf("dprc_open(): Child container Handle is not valid\n");
1172 goto err_child_open;
1173 }
1174
1175 return 0;
1176err_child_open:
1177 free(dflt_mc_io);
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301178err_calloc:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301179 dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1180 root_dprc_handle, child_dprc_id);
1181err_create:
1182 dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1183err_root_open:
1184err_root_container_id:
1185 return err;
1186}
1187
1188static int dprc_exit(void)
1189{
1190 int err;
1191
1192 err = dprc_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dprc_handle);
1193 if (err < 0) {
1194 printf("dprc_close(): Child failed: %d\n", err);
1195 goto err;
1196 }
1197
1198 err = dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1199 root_dprc_handle, child_dprc_id);
1200 if (err < 0) {
1201 printf("dprc_destroy_container() failed: %d\n", err);
1202 goto err;
1203 }
1204
1205 err = dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1206 if (err < 0) {
1207 printf("dprc_close(): Root failed: %d\n", err);
1208 goto err;
1209 }
1210
1211 if (dflt_mc_io)
1212 free(dflt_mc_io);
1213
1214 if (root_mc_io)
1215 free(root_mc_io);
1216
1217 return 0;
1218
1219err:
1220 return err;
1221}
1222
1223static int dpbp_init(void)
1224{
1225 int err;
1226 struct dpbp_attr dpbp_attr;
1227 struct dpbp_cfg dpbp_cfg;
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301228 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301229
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301230 dflt_dpbp = (struct fsl_dpbp_obj *)calloc(
1231 sizeof(struct fsl_dpbp_obj), 1);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001232 if (!dflt_dpbp) {
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301233 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301234 err = -ENOMEM;
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301235 goto err_calloc;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301236 }
1237
1238 dpbp_cfg.options = 512;
1239
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301240 err = dpbp_create(dflt_mc_io,
1241 dflt_dprc_handle,
1242 MC_CMD_NO_FLAGS,
1243 &dpbp_cfg,
1244 &dflt_dpbp->dpbp_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301245
1246 if (err < 0) {
1247 err = -ENODEV;
1248 printf("dpbp_create() failed: %d\n", err);
1249 goto err_create;
1250 }
1251
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301252 err = dpbp_get_api_version(dflt_mc_io, 0,
1253 &major_ver,
1254 &minor_ver);
1255 if (err < 0) {
1256 printf("dpbp_get_api_version() failed: %d\n", err);
1257 goto err_get_api_ver;
1258 }
1259
1260 if (major_ver < DPBP_VER_MAJOR || (major_ver == DPBP_VER_MAJOR &&
1261 minor_ver < DPBP_VER_MINOR)) {
1262 printf("DPBP version mismatch found %u.%u,",
1263 major_ver, minor_ver);
1264 printf("supported version is %u.%u\n",
1265 DPBP_VER_MAJOR, DPBP_VER_MINOR);
1266 }
1267
1268 err = dpbp_open(dflt_mc_io,
1269 MC_CMD_NO_FLAGS,
1270 dflt_dpbp->dpbp_id,
1271 &dflt_dpbp->dpbp_handle);
1272 if (err) {
1273 printf("dpbp_open() failed\n");
1274 goto err_open;
1275 }
1276
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301277 memset(&dpbp_attr, 0, sizeof(struct dpbp_attr));
1278 err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
1279 dflt_dpbp->dpbp_handle,
1280 &dpbp_attr);
1281 if (err < 0) {
1282 printf("dpbp_get_attributes() failed: %d\n", err);
1283 goto err_get_attr;
1284 }
1285
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301286 if (dflt_dpbp->dpbp_id != dpbp_attr.id) {
1287 printf("dpbp object id and attribute id are not same\n");
1288 goto err_attr_not_same;
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301289 }
1290
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301291#ifdef DEBUG
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301292 printf("Init: DPBP id=0x%x\n", dflt_dpbp->dpbp_attr.id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301293#endif
1294
1295 err = dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
1296 if (err < 0) {
1297 printf("dpbp_close() failed: %d\n", err);
1298 goto err_close;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001299 }
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001300
1301 return 0;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301302
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301303err_get_attr:
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301304err_attr_not_same:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301305 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301306 dpbp_destroy(dflt_mc_io,
1307 dflt_dprc_handle,
1308 MC_CMD_NO_FLAGS,
1309 dflt_dpbp->dpbp_id);
1310err_get_api_ver:
1311err_close:
1312err_open:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301313err_create:
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301314 free(dflt_dpbp);
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301315err_calloc:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301316 return err;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001317}
1318
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301319static int dpbp_exit(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001320{
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301321 int err;
1322
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301323 err = dpbp_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1324 dflt_dpbp->dpbp_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301325 if (err < 0) {
1326 printf("dpbp_destroy() failed: %d\n", err);
1327 goto err;
1328 }
1329
1330#ifdef DEBUG
1331 printf("Exit: DPBP id=0x%d\n", dflt_dpbp->dpbp_attr.id);
1332#endif
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001333
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301334 if (dflt_dpbp)
1335 free(dflt_dpbp);
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301336 return 0;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301337
1338err:
1339 return err;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001340}
1341
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301342static int dpni_init(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001343{
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301344 int err;
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301345 uint8_t cfg_buf[256] = {0};
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301346 struct dpni_cfg dpni_cfg;
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301347 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301348
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301349 dflt_dpni = (struct fsl_dpni_obj *)calloc(
1350 sizeof(struct fsl_dpni_obj), 1);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301351 if (!dflt_dpni) {
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301352 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301353 err = -ENOMEM;
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301354 goto err_calloc;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301355 }
1356
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301357 memset(&dpni_cfg, 0, sizeof(dpni_cfg));
1358 err = dpni_prepare_cfg(&dpni_cfg, &cfg_buf[0]);
Prabhakar Kushwahae26e2852015-12-24 15:33:01 +05301359 if (err < 0) {
1360 err = -ENODEV;
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301361 printf("dpni_prepare_cfg() failed: %d\n", err);
1362 goto err_prepare_cfg;
Prabhakar Kushwahae26e2852015-12-24 15:33:01 +05301363 }
1364
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301365 err = dpni_create(dflt_mc_io,
1366 dflt_dprc_handle,
1367 MC_CMD_NO_FLAGS,
1368 &dpni_cfg,
1369 &dflt_dpni->dpni_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301370 if (err < 0) {
1371 err = -ENODEV;
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301372 printf("dpni create() failed: %d\n", err);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301373 goto err_create;
1374 }
1375
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301376 err = dpni_get_api_version(dflt_mc_io, 0,
1377 &major_ver,
1378 &minor_ver);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301379 if (err < 0) {
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301380 printf("dpni_get_api_version() failed: %d\n", err);
1381 goto err_get_version;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301382 }
1383
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301384 if (major_ver < DPNI_VER_MAJOR || (major_ver == DPNI_VER_MAJOR &&
1385 minor_ver < DPNI_VER_MINOR)) {
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301386 printf("DPNI version mismatch found %u.%u,",
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301387 major_ver, minor_ver);
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301388 printf("supported version is %u.%u\n",
1389 DPNI_VER_MAJOR, DPNI_VER_MINOR);
1390 }
1391
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301392 err = dpni_open(dflt_mc_io,
1393 MC_CMD_NO_FLAGS,
1394 dflt_dpni->dpni_id,
1395 &dflt_dpni->dpni_handle);
1396 if (err) {
1397 printf("dpni_open() failed\n");
1398 goto err_open;
1399 }
1400
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301401#ifdef DEBUG
1402 printf("Init: DPNI id=0x%d\n", dflt_dpni->dpni_id);
1403#endif
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301404 err = dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
1405 if (err < 0) {
1406 printf("dpni_close() failed: %d\n", err);
1407 goto err_close;
1408 }
1409
1410 return 0;
1411
1412err_close:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301413 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301414err_open:
1415err_get_version:
1416 dpni_destroy(dflt_mc_io,
1417 dflt_dprc_handle,
1418 MC_CMD_NO_FLAGS,
1419 dflt_dpni->dpni_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301420err_create:
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301421err_prepare_cfg:
Prabhakar Kushwahae26e2852015-12-24 15:33:01 +05301422 free(dflt_dpni);
Prabhakar Kushwaha790c1a42017-10-11 08:51:18 +05301423err_calloc:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301424 return err;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001425}
1426
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301427static int dpni_exit(void)
1428{
1429 int err;
1430
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301431 err = dpni_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1432 dflt_dpni->dpni_id);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301433 if (err < 0) {
1434 printf("dpni_destroy() failed: %d\n", err);
1435 goto err;
1436 }
1437
1438#ifdef DEBUG
1439 printf("Exit: DPNI id=0x%d\n", dflt_dpni->dpni_id);
1440#endif
1441
1442 if (dflt_dpni)
1443 free(dflt_dpni);
1444 return 0;
1445
1446err:
1447 return err;
1448}
1449
1450static int mc_init_object(void)
1451{
1452 int err = 0;
1453
1454 err = dprc_init();
1455 if (err < 0) {
1456 printf("dprc_init() failed: %d\n", err);
1457 goto err;
1458 }
1459
1460 err = dpbp_init();
1461 if (err < 0) {
1462 printf("dpbp_init() failed: %d\n", err);
1463 goto err;
1464 }
1465
1466 err = dpio_init();
1467 if (err < 0) {
1468 printf("dpio_init() failed: %d\n", err);
1469 goto err;
1470 }
1471
1472 err = dpni_init();
1473 if (err < 0) {
1474 printf("dpni_init() failed: %d\n", err);
1475 goto err;
1476 }
1477
1478 return 0;
1479err:
1480 return err;
1481}
1482
1483int fsl_mc_ldpaa_exit(bd_t *bd)
1484{
1485 int err = 0;
Yogesh Gaur4a81bbe2017-04-27 10:14:16 +05301486 bool is_dpl_apply_status = false;
Santan Kumar0a6e5ba2017-06-29 11:19:34 +05301487 bool mc_boot_status = false;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301488
Alexander Graf2ebeb442016-11-17 01:02:57 +01001489 if (bd && mc_lazy_dpl_addr && !fsl_mc_ldpaa_exit(NULL)) {
Yogesh Gaur8dadfb42018-01-16 10:08:24 +05301490 err = mc_apply_dpl(mc_lazy_dpl_addr);
1491 if (!err)
1492 fdt_fixup_board_enet(working_fdt);
Alexander Graf2ebeb442016-11-17 01:02:57 +01001493 mc_lazy_dpl_addr = 0;
1494 }
1495
Santan Kumar0a6e5ba2017-06-29 11:19:34 +05301496 if (!get_mc_boot_status())
1497 mc_boot_status = true;
1498
Prabhakar Kushwaha878d3ec2016-03-21 14:19:39 +05301499 /* MC is not loaded intentionally, So return success. */
Santan Kumar0a6e5ba2017-06-29 11:19:34 +05301500 if (bd && !mc_boot_status)
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301501 return 0;
1502
Yogesh Gaur4a81bbe2017-04-27 10:14:16 +05301503 /* If DPL is deployed, set is_dpl_apply_status as TRUE. */
1504 if (!get_dpl_apply_status())
1505 is_dpl_apply_status = true;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301506
Yogesh Gaur4a81bbe2017-04-27 10:14:16 +05301507 /*
1508 * For case MC is loaded but DPL is not deployed, return success and
1509 * print message on console. Else FDT fix-up code execution hanged.
1510 */
Santan Kumar0a6e5ba2017-06-29 11:19:34 +05301511 if (bd && mc_boot_status && !is_dpl_apply_status) {
Yogesh Gaur4a81bbe2017-04-27 10:14:16 +05301512 printf("fsl-mc: DPL not deployed, DPAA2 ethernet not work\n");
Yogesh Gaur82709e52017-11-28 10:11:14 +05301513 goto mc_obj_cleanup;
Yogesh Gaur4a81bbe2017-04-27 10:14:16 +05301514 }
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301515
Santan Kumar0a6e5ba2017-06-29 11:19:34 +05301516 if (bd && mc_boot_status && is_dpl_apply_status)
1517 return 0;
1518
Yogesh Gaur82709e52017-11-28 10:11:14 +05301519mc_obj_cleanup:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301520 err = dpbp_exit();
1521 if (err < 0) {
Prabhakar Kushwaha88ba4d62016-01-20 12:04:37 +05301522 printf("dpbp_exit() failed: %d\n", err);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301523 goto err;
1524 }
1525
1526 err = dpio_exit();
1527 if (err < 0) {
1528 printf("dpio_exit() failed: %d\n", err);
1529 goto err;
1530 }
1531
1532 err = dpni_exit();
1533 if (err < 0) {
1534 printf("dpni_exit() failed: %d\n", err);
1535 goto err;
1536 }
1537
1538 err = dprc_exit();
1539 if (err < 0) {
1540 printf("dprc_exit() failed: %d\n", err);
1541 goto err;
1542 }
1543
1544 return 0;
1545err:
1546 return err;
1547}
1548
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301549static int do_fsl_mc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001550{
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301551 int err = 0;
1552 if (argc < 3)
1553 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001554
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301555 switch (argv[1][0]) {
1556 case 's': {
1557 char sub_cmd;
Prabhakar Kushwaha122bcfd2015-11-09 16:42:07 +05301558 u64 mc_fw_addr, mc_dpc_addr;
1559#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
1560 u64 aiop_fw_addr;
1561#endif
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001562
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301563 sub_cmd = argv[2][0];
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301564
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301565 switch (sub_cmd) {
1566 case 'm':
1567 if (argc < 5)
1568 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001569
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301570 if (get_mc_boot_status() == 0) {
1571 printf("fsl-mc: MC is already booted");
1572 printf("\n");
1573 return err;
1574 }
1575 mc_fw_addr = simple_strtoull(argv[3], NULL, 16);
1576 mc_dpc_addr = simple_strtoull(argv[4], NULL,
1577 16);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301578
1579 if (!mc_init(mc_fw_addr, mc_dpc_addr))
1580 err = mc_init_object();
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301581 break;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001582
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301583#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
1584 case 'a':
1585 if (argc < 4)
1586 goto usage;
1587 if (get_aiop_apply_status() == 0) {
1588 printf("fsl-mc: AIOP FW is already");
1589 printf(" applied\n");
1590 return err;
1591 }
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001592
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301593 aiop_fw_addr = simple_strtoull(argv[3], NULL,
1594 16);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001595
York Suncbe8e1c2016-04-04 11:41:26 -07001596 /* if SoC doesn't have AIOP, err = -ENODEV */
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301597 err = load_mc_aiop_img(aiop_fw_addr);
1598 if (!err)
1599 printf("fsl-mc: AIOP FW applied\n");
1600 break;
1601#endif
1602 default:
1603 printf("Invalid option: %s\n", argv[2]);
1604 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001605
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301606 break;
1607 }
1608 }
1609 break;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001610
Alexander Graf2ebeb442016-11-17 01:02:57 +01001611 case 'l':
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301612 case 'a': {
1613 u64 mc_dpl_addr;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001614
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301615 if (argc < 4)
1616 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001617
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301618 if (get_dpl_apply_status() == 0) {
1619 printf("fsl-mc: DPL already applied\n");
1620 return err;
1621 }
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001622
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301623 mc_dpl_addr = simple_strtoull(argv[3], NULL,
1624 16);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301625
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301626 if (get_mc_boot_status() != 0) {
1627 printf("fsl-mc: Deploying data path layout ..");
1628 printf("ERROR (MC is not booted)\n");
1629 return -ENODEV;
1630 }
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301631
Alexander Graf2ebeb442016-11-17 01:02:57 +01001632 if (argv[1][0] == 'l') {
1633 /*
1634 * We will do the actual dpaa exit and dpl apply
1635 * later from announce_and_cleanup().
1636 */
1637 mc_lazy_dpl_addr = mc_dpl_addr;
1638 } else {
1639 /* The user wants it applied now */
1640 if (!fsl_mc_ldpaa_exit(NULL))
1641 err = mc_apply_dpl(mc_dpl_addr);
1642 }
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301643 break;
J. German Riveraf4fed4b2015-03-20 19:28:18 -07001644 }
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301645 default:
1646 printf("Invalid option: %s\n", argv[1]);
1647 goto usage;
1648 break;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001649 }
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301650 return err;
1651 usage:
1652 return CMD_RET_USAGE;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001653}
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301654
1655U_BOOT_CMD(
1656 fsl_mc, CONFIG_SYS_MAXARGS, 1, do_fsl_mc,
1657 "DPAA2 command to manage Management Complex (MC)",
1658 "start mc [FW_addr] [DPC_addr] - Start Management Complex\n"
1659 "fsl_mc apply DPL [DPL_addr] - Apply DPL file\n"
Alexander Graf2ebeb442016-11-17 01:02:57 +01001660 "fsl_mc lazyapply DPL [DPL_addr] - Apply DPL file on exit\n"
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301661 "fsl_mc start aiop [FW_addr] - Start AIOP\n"
1662);
Bogdan Purcareata08bc0142017-05-24 16:40:21 +00001663
1664void mc_env_boot(void)
1665{
1666#if defined(CONFIG_FSL_MC_ENET)
1667 char *mc_boot_env_var;
1668 /* The MC may only be initialized in the reset PHY function
1669 * because otherwise U-Boot has not yet set up all the MAC
1670 * address info properly. Without MAC addresses, the MC code
1671 * can not properly initialize the DPC.
1672 */
Simon Glass64b723f2017-08-03 12:22:12 -06001673 mc_boot_env_var = env_get(MC_BOOT_ENV_VAR);
Bogdan Purcareata08bc0142017-05-24 16:40:21 +00001674 if (mc_boot_env_var)
1675 run_command_list(mc_boot_env_var, -1, 0);
1676#endif /* CONFIG_FSL_MC_ENET */
1677}