blob: cc59b21f9f48193c8b28938f64021bac8608d006 [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>
J. German Riverab940ca62014-06-23 15:15:55 -07008#include <errno.h>
Masahiro Yamada84b8bf62016-01-24 23:27:48 +09009#include <linux/bug.h>
J. German Riverab940ca62014-06-23 15:15:55 -070010#include <asm/io.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090011#include <linux/libfdt.h>
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +000012#include <net.h>
Stuart Yoder21c69872015-07-02 11:29:03 +053013#include <fdt_support.h>
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080014#include <fsl-mc/fsl_mc.h>
15#include <fsl-mc/fsl_mc_sys.h>
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070016#include <fsl-mc/fsl_mc_private.h>
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080017#include <fsl-mc/fsl_dpmng.h>
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070018#include <fsl-mc/fsl_dprc.h>
19#include <fsl-mc/fsl_dpio.h>
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053020#include <fsl-mc/fsl_dpni.h>
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070021#include <fsl-mc/fsl_qbman_portal.h>
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053022#include <fsl-mc/ldpaa_wriop.h>
J. German Riverab940ca62014-06-23 15:15:55 -070023
J. German Rivera125e2bc2015-03-20 19:28:18 -070024#define MC_RAM_BASE_ADDR_ALIGNMENT (512UL * 1024 * 1024)
25#define MC_RAM_BASE_ADDR_ALIGNMENT_MASK (~(MC_RAM_BASE_ADDR_ALIGNMENT - 1))
26#define MC_RAM_SIZE_ALIGNMENT (256UL * 1024 * 1024)
27
28#define MC_MEM_SIZE_ENV_VAR "mcmemsize"
29#define MC_BOOT_TIMEOUT_ENV_VAR "mcboottimeout"
Bogdan Purcareata33a89912017-05-24 16:40:21 +000030#define MC_BOOT_ENV_VAR "mcinitcmd"
Meenakshi Aggarwal43ad41e2019-02-27 14:41:02 +053031#define MC_DRAM_BLOCK_DEFAULT_SIZE (512UL * 1024 * 1024)
J. German Rivera125e2bc2015-03-20 19:28:18 -070032
J. German Riverab940ca62014-06-23 15:15:55 -070033DECLARE_GLOBAL_DATA_PTR;
Prabhakar Kushwaha87519a92018-08-27 12:58:52 +053034static int mc_memset_resv_ram;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053035static int mc_boot_status = -1;
36static int mc_dpl_applied = -1;
37#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
38static int mc_aiop_applied = -1;
39#endif
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +053040struct fsl_mc_io *root_mc_io = NULL;
41struct fsl_mc_io *dflt_mc_io = NULL; /* child container */
42uint16_t root_dprc_handle = 0;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070043uint16_t dflt_dprc_handle = 0;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +053044int child_dprc_id;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070045struct fsl_dpbp_obj *dflt_dpbp = NULL;
46struct fsl_dpio_obj *dflt_dpio = NULL;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +053047struct fsl_dpni_obj *dflt_dpni = NULL;
Alexander Grafb7b84102016-11-17 01:02:57 +010048static u64 mc_lazy_dpl_addr;
J. German Riverab940ca62014-06-23 15:15:55 -070049
J. German Rivera125e2bc2015-03-20 19:28:18 -070050#ifdef DEBUG
51void dump_ram_words(const char *title, void *addr)
52{
53 int i;
54 uint32_t *words = addr;
55
56 printf("Dumping beginning of %s (%p):\n", title, addr);
57 for (i = 0; i < 16; i++)
58 printf("%#x ", words[i]);
59
60 printf("\n");
61}
62
63void dump_mc_ccsr_regs(struct mc_ccsr_registers __iomem *mc_ccsr_regs)
64{
65 printf("MC CCSR registers:\n"
66 "reg_gcr1 %#x\n"
67 "reg_gsr %#x\n"
68 "reg_sicbalr %#x\n"
69 "reg_sicbahr %#x\n"
70 "reg_sicapr %#x\n"
71 "reg_mcfbalr %#x\n"
72 "reg_mcfbahr %#x\n"
73 "reg_mcfapr %#x\n"
74 "reg_psr %#x\n",
75 mc_ccsr_regs->reg_gcr1,
76 mc_ccsr_regs->reg_gsr,
77 mc_ccsr_regs->reg_sicbalr,
78 mc_ccsr_regs->reg_sicbahr,
79 mc_ccsr_regs->reg_sicapr,
80 mc_ccsr_regs->reg_mcfbalr,
81 mc_ccsr_regs->reg_mcfbahr,
82 mc_ccsr_regs->reg_mcfapr,
83 mc_ccsr_regs->reg_psr);
84}
85#else
86
87#define dump_ram_words(title, addr)
88#define dump_mc_ccsr_regs(mc_ccsr_regs)
89
90#endif /* DEBUG */
91
92#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Riverab940ca62014-06-23 15:15:55 -070093/**
94 * Copying MC firmware or DPL image to DDR
95 */
96static int mc_copy_image(const char *title,
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080097 u64 image_addr, u32 image_size, u64 mc_ram_addr)
J. German Riverab940ca62014-06-23 15:15:55 -070098{
99 debug("%s copied to address %p\n", title, (void *)mc_ram_addr);
100 memcpy((void *)mc_ram_addr, (void *)image_addr, image_size);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700101 flush_dcache_range(mc_ram_addr, mc_ram_addr + image_size);
J. German Riverab940ca62014-06-23 15:15:55 -0700102 return 0;
103}
104
105/**
106 * MC firmware FIT image parser checks if the image is in FIT
107 * format, verifies integrity of the image and calculates
108 * raw image address and size values.
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800109 * Returns 0 on success and a negative errno on error.
J. German Riverab940ca62014-06-23 15:15:55 -0700110 * task fail.
111 **/
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530112int parse_mc_firmware_fit_image(u64 mc_fw_addr,
113 const void **raw_image_addr,
J. German Riverab940ca62014-06-23 15:15:55 -0700114 size_t *raw_image_size)
115{
116 int format;
117 void *fit_hdr;
118 int node_offset;
119 const void *data;
120 size_t size;
121 const char *uname = "firmware";
122
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530123 fit_hdr = (void *)mc_fw_addr;
J. German Riverab940ca62014-06-23 15:15:55 -0700124
125 /* Check if Image is in FIT format */
126 format = genimg_get_format(fit_hdr);
127
128 if (format != IMAGE_FORMAT_FIT) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530129 printf("fsl-mc: ERR: Bad firmware image (not a FIT image)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800130 return -EINVAL;
J. German Riverab940ca62014-06-23 15:15:55 -0700131 }
132
133 if (!fit_check_format(fit_hdr)) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530134 printf("fsl-mc: ERR: Bad firmware image (bad FIT header)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800135 return -EINVAL;
J. German Riverab940ca62014-06-23 15:15:55 -0700136 }
137
138 node_offset = fit_image_get_node(fit_hdr, uname);
139
140 if (node_offset < 0) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530141 printf("fsl-mc: ERR: Bad firmware image (missing subimage)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800142 return -ENOENT;
J. German Riverab940ca62014-06-23 15:15:55 -0700143 }
144
145 /* Verify MC firmware image */
146 if (!(fit_image_verify(fit_hdr, node_offset))) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530147 printf("fsl-mc: ERR: Bad firmware image (bad CRC)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800148 return -EINVAL;
J. German Riverab940ca62014-06-23 15:15:55 -0700149 }
150
151 /* Get address and size of raw image */
152 fit_image_get_data(fit_hdr, node_offset, &data, &size);
153
154 *raw_image_addr = data;
155 *raw_image_size = size;
156
157 return 0;
158}
J. German Rivera125e2bc2015-03-20 19:28:18 -0700159#endif
160
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000161#define MC_DT_INCREASE_SIZE 64
162
163enum mc_fixup_type {
164 MC_FIXUP_DPL,
165 MC_FIXUP_DPC
166};
167
168static int mc_fixup_mac_addr(void *blob, int nodeoffset,
169 const char *propname, struct eth_device *eth_dev,
170 enum mc_fixup_type type)
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000171{
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000172 int err = 0, len = 0, size, i;
173 unsigned char env_enetaddr[ARP_HLEN];
174 unsigned int enetaddr_32[ARP_HLEN];
175 void *val = NULL;
176
177 switch (type) {
178 case MC_FIXUP_DPL:
179 /* DPL likes its addresses on 32 * ARP_HLEN bits */
180 for (i = 0; i < ARP_HLEN; i++)
181 enetaddr_32[i] = cpu_to_fdt32(eth_dev->enetaddr[i]);
182 val = enetaddr_32;
183 len = sizeof(enetaddr_32);
184 break;
185
186 case MC_FIXUP_DPC:
187 val = eth_dev->enetaddr;
188 len = ARP_HLEN;
189 break;
190 }
191
192 /* MAC address property present */
193 if (fdt_get_property(blob, nodeoffset, propname, NULL)) {
194 /* u-boot MAC addr randomly assigned - leave the present one */
Simon Glass35affd72017-08-03 12:22:14 -0600195 if (!eth_env_get_enetaddr_by_index("eth", eth_dev->index,
196 env_enetaddr))
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000197 return err;
198 } else {
199 size = MC_DT_INCREASE_SIZE + strlen(propname) + len;
200 /* make room for mac address property */
201 err = fdt_increase_size(blob, size);
202 if (err) {
203 printf("fdt_increase_size: err=%s\n",
204 fdt_strerror(err));
205 return err;
206 }
207 }
208
209 err = fdt_setprop(blob, nodeoffset, propname, val, len);
210 if (err) {
211 printf("fdt_setprop: err=%s\n", fdt_strerror(err));
212 return err;
213 }
214
215 return err;
216}
217
218#define is_dpni(s) (s != NULL ? !strncmp(s, "dpni@", 5) : 0)
219
220const char *dpl_get_connection_endpoint(void *blob, char *endpoint)
221{
222 int connoffset = fdt_path_offset(blob, "/connections"), off;
223 const char *s1, *s2;
224
225 for (off = fdt_first_subnode(blob, connoffset);
226 off >= 0;
227 off = fdt_next_subnode(blob, off)) {
228 s1 = fdt_stringlist_get(blob, off, "endpoint1", 0, NULL);
229 s2 = fdt_stringlist_get(blob, off, "endpoint2", 0, NULL);
230
231 if (!s1 || !s2)
232 continue;
233
234 if (strcmp(endpoint, s1) == 0)
235 return s2;
236
237 if (strcmp(endpoint, s2) == 0)
238 return s1;
239 }
240
241 return NULL;
242}
243
244static int mc_fixup_dpl_mac_addr(void *blob, int dpmac_id,
245 struct eth_device *eth_dev)
246{
247 int objoff = fdt_path_offset(blob, "/objects");
248 int dpmacoff = -1, dpnioff = -1;
249 const char *endpoint;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000250 char mac_name[10];
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000251 int err;
252
253 sprintf(mac_name, "dpmac@%d", dpmac_id);
254 dpmacoff = fdt_subnode_offset(blob, objoff, mac_name);
255 if (dpmacoff < 0)
256 /* dpmac not defined in DPL, so skip it. */
257 return 0;
258
259 err = mc_fixup_mac_addr(blob, dpmacoff, "mac_addr", eth_dev,
260 MC_FIXUP_DPL);
261 if (err) {
262 printf("Error fixing up dpmac mac_addr in DPL\n");
263 return err;
264 }
265
266 /* now we need to figure out if there is any
267 * DPNI connected to this MAC, so we walk the
268 * connection list
269 */
270 endpoint = dpl_get_connection_endpoint(blob, mac_name);
271 if (!is_dpni(endpoint))
272 return 0;
273
274 /* let's see if we can fixup the DPNI as well */
275 dpnioff = fdt_subnode_offset(blob, objoff, endpoint);
276 if (dpnioff < 0)
277 /* DPNI not defined in DPL in the objects area */
278 return 0;
279
280 return mc_fixup_mac_addr(blob, dpnioff, "mac_addr", eth_dev,
281 MC_FIXUP_DPL);
282}
283
Nipun Guptaa78df402018-08-20 16:01:14 +0530284void fdt_fsl_mc_fixup_iommu_map_entry(void *blob)
285{
286 u32 *prop;
287 u32 iommu_map[4];
288 int offset;
289 int lenp;
290
291 /* find fsl-mc node */
292 offset = fdt_path_offset(blob, "/soc/fsl-mc");
293 if (offset < 0)
294 offset = fdt_path_offset(blob, "/fsl-mc");
295 if (offset < 0) {
296 printf("%s: fsl-mc: ERR: fsl-mc node not found in DT, err %d\n",
297 __func__, offset);
298 return;
299 }
300
301 prop = fdt_getprop_w(blob, offset, "iommu-map", &lenp);
302 if (!prop) {
303 debug("%s: fsl-mc: ERR: missing iommu-map in fsl-mc bus node\n",
304 __func__);
305 return;
306 }
307
308 iommu_map[0] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_START);
309 iommu_map[1] = *++prop;
310 iommu_map[2] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_START);
311 iommu_map[3] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_END -
312 FSL_DPAA2_STREAM_ID_START + 1);
313
314 fdt_setprop_inplace(blob, offset, "iommu-map",
315 iommu_map, sizeof(iommu_map));
316}
317
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000318static int mc_fixup_dpc_mac_addr(void *blob, int dpmac_id,
319 struct eth_device *eth_dev)
320{
321 int nodeoffset = fdt_path_offset(blob, "/board_info/ports"), noff;
322 int err = 0;
323 char mac_name[10];
324 const char link_type_mode[] = "MAC_LINK_TYPE_FIXED";
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000325
326 sprintf(mac_name, "mac@%d", dpmac_id);
327
328 /* node not found - create it */
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000329 noff = fdt_subnode_offset(blob, nodeoffset, (const char *)mac_name);
330 if (noff < 0) {
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000331 err = fdt_increase_size(blob, 200);
332 if (err) {
333 printf("fdt_increase_size: err=%s\n",
334 fdt_strerror(err));
335 return err;
336 }
337
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000338 noff = fdt_add_subnode(blob, nodeoffset, mac_name);
339 if (noff < 0) {
340 printf("fdt_add_subnode: err=%s\n",
341 fdt_strerror(err));
342 return err;
343 }
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000344
345 /* add default property of fixed link */
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000346 err = fdt_appendprop_string(blob, noff,
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000347 "link_type", link_type_mode);
348 if (err) {
349 printf("fdt_appendprop_string: err=%s\n",
350 fdt_strerror(err));
351 return err;
352 }
353 }
354
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000355 return mc_fixup_mac_addr(blob, noff, "port_mac_address", eth_dev,
356 MC_FIXUP_DPC);
357}
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000358
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000359static int mc_fixup_mac_addrs(void *blob, enum mc_fixup_type type)
360{
361 int i, err = 0, ret = 0;
Pankaj Bansalc022ec02018-08-02 16:31:28 +0530362 char ethname[ETH_NAME_LEN];
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000363 struct eth_device *eth_dev;
364
365 for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++) {
366 /* port not enabled */
Pankaj Bansal1ed19a12018-10-10 14:08:33 +0530367 if (wriop_is_enabled_dpmac(i) != 1)
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000368 continue;
369
Pankaj Bansalc022ec02018-08-02 16:31:28 +0530370 snprintf(ethname, ETH_NAME_LEN, "DPMAC%d@%s", i,
371 phy_interface_strings[wriop_get_enet_if(i)]);
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000372
373 eth_dev = eth_get_dev_by_name(ethname);
374 if (eth_dev == NULL)
375 continue;
376
377 switch (type) {
378 case MC_FIXUP_DPL:
379 err = mc_fixup_dpl_mac_addr(blob, i, eth_dev);
380 break;
381 case MC_FIXUP_DPC:
382 err = mc_fixup_dpc_mac_addr(blob, i, eth_dev);
383 break;
384 default:
385 break;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000386 }
387
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000388 if (err)
389 printf("fsl-mc: ERROR fixing mac address for %s\n",
390 ethname);
391 ret |= err;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000392 }
393
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000394 return ret;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000395}
396
Stuart Yoder21c69872015-07-02 11:29:03 +0530397static int mc_fixup_dpc(u64 dpc_addr)
398{
399 void *blob = (void *)dpc_addr;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000400 int nodeoffset, err = 0;
Stuart Yoder21c69872015-07-02 11:29:03 +0530401
402 /* delete any existing ICID pools */
403 nodeoffset = fdt_path_offset(blob, "/resources/icid_pools");
404 if (fdt_del_node(blob, nodeoffset) < 0)
405 printf("\nfsl-mc: WARNING: could not delete ICID pool\n");
406
407 /* add a new pool */
408 nodeoffset = fdt_path_offset(blob, "/resources");
409 if (nodeoffset < 0) {
410 printf("\nfsl-mc: ERROR: DPC is missing /resources\n");
411 return -EINVAL;
412 }
413 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pools");
414 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pool@0");
415 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
416 "base_icid", FSL_DPAA2_STREAM_ID_START, 1);
417 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
418 "num",
419 FSL_DPAA2_STREAM_ID_END -
420 FSL_DPAA2_STREAM_ID_START + 1, 1);
421
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000422 /* fixup MAC addresses for dpmac ports */
423 nodeoffset = fdt_path_offset(blob, "/board_info/ports");
424 if (nodeoffset < 0)
Ioana Ciocoi Radulescu2e9f1bf2019-02-26 15:50:07 +0000425 goto out;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000426
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000427 err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPC);
Ioana Ciocoi Radulescu2e9f1bf2019-02-26 15:50:07 +0000428
429out:
Stuart Yoder21c69872015-07-02 11:29:03 +0530430 flush_dcache_range(dpc_addr, dpc_addr + fdt_totalsize(blob));
431
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000432 return err;
Stuart Yoder21c69872015-07-02 11:29:03 +0530433}
434
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530435static 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 -0700436{
437 u64 mc_dpc_offset;
438#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
439 int error;
440 void *dpc_fdt_hdr;
441 int dpc_size;
442#endif
443
444#ifdef CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET
445 BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET & 0x3) != 0 ||
446 CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET > 0xffffffff);
447
448 mc_dpc_offset = CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET;
449#else
450#error "CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET not defined"
451#endif
452
453 /*
454 * Load the MC DPC blob in the MC private DRAM block:
455 */
456#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
457 printf("MC DPC is preloaded to %#llx\n", mc_ram_addr + mc_dpc_offset);
458#else
459 /*
460 * Get address and size of the DPC blob stored in flash:
461 */
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530462 dpc_fdt_hdr = (void *)mc_dpc_addr;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700463
464 error = fdt_check_header(dpc_fdt_hdr);
465 if (error != 0) {
466 /*
467 * Don't return with error here, since the MC firmware can
468 * still boot without a DPC
469 */
J. German Riveracc088c32015-07-02 11:28:56 +0530470 printf("\nfsl-mc: WARNING: No DPC image found");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700471 return 0;
472 }
473
474 dpc_size = fdt_totalsize(dpc_fdt_hdr);
475 if (dpc_size > CONFIG_SYS_LS_MC_DPC_MAX_LENGTH) {
J. German Riveracc088c32015-07-02 11:28:56 +0530476 printf("\nfsl-mc: ERROR: Bad DPC image (too large: %d)\n",
J. German Rivera125e2bc2015-03-20 19:28:18 -0700477 dpc_size);
478 return -EINVAL;
479 }
480
481 mc_copy_image("MC DPC blob",
482 (u64)dpc_fdt_hdr, dpc_size, mc_ram_addr + mc_dpc_offset);
483#endif /* not defined CONFIG_SYS_LS_MC_DPC_IN_DDR */
484
Stuart Yoder21c69872015-07-02 11:29:03 +0530485 if (mc_fixup_dpc(mc_ram_addr + mc_dpc_offset))
486 return -EINVAL;
487
J. German Rivera125e2bc2015-03-20 19:28:18 -0700488 dump_ram_words("DPC", (void *)(mc_ram_addr + mc_dpc_offset));
489 return 0;
490}
491
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000492
493static int mc_fixup_dpl(u64 dpl_addr)
494{
495 void *blob = (void *)dpl_addr;
496 u32 ver = fdt_getprop_u32_default(blob, "/", "dpl-version", 0);
497 int err = 0;
498
499 /* The DPL fixup for mac addresses is only relevant
500 * for old-style DPLs
501 */
502 if (ver >= 10)
503 return 0;
504
505 err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPL);
506 flush_dcache_range(dpl_addr, dpl_addr + fdt_totalsize(blob));
507
508 return err;
509}
510
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530511static 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 -0700512{
513 u64 mc_dpl_offset;
514#ifndef CONFIG_SYS_LS_MC_DPL_IN_DDR
515 int error;
516 void *dpl_fdt_hdr;
517 int dpl_size;
518#endif
519
520#ifdef CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET
521 BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET & 0x3) != 0 ||
522 CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET > 0xffffffff);
523
524 mc_dpl_offset = CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET;
525#else
526#error "CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET not defined"
527#endif
528
529 /*
530 * Load the MC DPL blob in the MC private DRAM block:
531 */
532#ifdef CONFIG_SYS_LS_MC_DPL_IN_DDR
533 printf("MC DPL is preloaded to %#llx\n", mc_ram_addr + mc_dpl_offset);
534#else
535 /*
536 * Get address and size of the DPL blob stored in flash:
537 */
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530538 dpl_fdt_hdr = (void *)mc_dpl_addr;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700539
540 error = fdt_check_header(dpl_fdt_hdr);
541 if (error != 0) {
J. German Riveracc088c32015-07-02 11:28:56 +0530542 printf("\nfsl-mc: ERROR: Bad DPL image (bad header)\n");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700543 return error;
544 }
545
546 dpl_size = fdt_totalsize(dpl_fdt_hdr);
547 if (dpl_size > CONFIG_SYS_LS_MC_DPL_MAX_LENGTH) {
J. German Riveracc088c32015-07-02 11:28:56 +0530548 printf("\nfsl-mc: ERROR: Bad DPL image (too large: %d)\n",
J. German Rivera125e2bc2015-03-20 19:28:18 -0700549 dpl_size);
550 return -EINVAL;
551 }
552
553 mc_copy_image("MC DPL blob",
554 (u64)dpl_fdt_hdr, dpl_size, mc_ram_addr + mc_dpl_offset);
555#endif /* not defined CONFIG_SYS_LS_MC_DPL_IN_DDR */
556
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000557 if (mc_fixup_dpl(mc_ram_addr + mc_dpl_offset))
558 return -EINVAL;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700559 dump_ram_words("DPL", (void *)(mc_ram_addr + mc_dpl_offset));
560 return 0;
561}
562
563/**
564 * Return the MC boot timeout value in milliseconds
565 */
566static unsigned long get_mc_boot_timeout_ms(void)
567{
568 unsigned long timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
569
Simon Glass00caae62017-08-03 12:22:12 -0600570 char *timeout_ms_env_var = env_get(MC_BOOT_TIMEOUT_ENV_VAR);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700571
572 if (timeout_ms_env_var) {
573 timeout_ms = simple_strtoul(timeout_ms_env_var, NULL, 10);
574 if (timeout_ms == 0) {
575 printf("fsl-mc: WARNING: Invalid value for \'"
576 MC_BOOT_TIMEOUT_ENV_VAR
577 "\' environment variable: %lu\n",
578 timeout_ms);
579
580 timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
581 }
582 }
583
584 return timeout_ms;
585}
586
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530587#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
York Sun3c1d2182016-04-04 11:41:26 -0700588
589__weak bool soc_has_aiop(void)
590{
591 return false;
592}
593
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530594static int load_mc_aiop_img(u64 aiop_fw_addr)
J. German Riverac1000c12015-07-02 11:28:58 +0530595{
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530596 u64 mc_ram_addr = mc_get_dram_addr();
597#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
J. German Riverac1000c12015-07-02 11:28:58 +0530598 void *aiop_img;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530599#endif
J. German Riverac1000c12015-07-02 11:28:58 +0530600
York Sun3c1d2182016-04-04 11:41:26 -0700601 /* Check if AIOP is available */
602 if (!soc_has_aiop())
603 return -ENODEV;
J. German Riverac1000c12015-07-02 11:28:58 +0530604 /*
605 * Load the MC AIOP image in the MC private DRAM block:
606 */
607
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530608#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
609 printf("MC AIOP is preloaded to %#llx\n", mc_ram_addr +
610 CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
611#else
612 aiop_img = (void *)aiop_fw_addr;
J. German Riverac1000c12015-07-02 11:28:58 +0530613 mc_copy_image("MC AIOP image",
614 (u64)aiop_img, CONFIG_SYS_LS_MC_AIOP_IMG_MAX_LENGTH,
615 mc_ram_addr + CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530616#endif
617 mc_aiop_applied = 0;
J. German Riverac1000c12015-07-02 11:28:58 +0530618
619 return 0;
620}
621#endif
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530622
J. German Rivera125e2bc2015-03-20 19:28:18 -0700623static int wait_for_mc(bool booting_mc, u32 *final_reg_gsr)
624{
625 u32 reg_gsr;
626 u32 mc_fw_boot_status;
627 unsigned long timeout_ms = get_mc_boot_timeout_ms();
628 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
629
630 dmb();
J. German Rivera125e2bc2015-03-20 19:28:18 -0700631 assert(timeout_ms > 0);
632 for (;;) {
633 udelay(1000); /* throttle polling */
634 reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
635 mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
636 if (mc_fw_boot_status & 0x1)
637 break;
638
639 timeout_ms--;
640 if (timeout_ms == 0)
641 break;
642 }
643
644 if (timeout_ms == 0) {
J. German Riveracc088c32015-07-02 11:28:56 +0530645 printf("ERROR: timeout\n");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700646
647 /* TODO: Get an error status from an MC CCSR register */
648 return -ETIMEDOUT;
649 }
650
651 if (mc_fw_boot_status != 0x1) {
652 /*
653 * TODO: Identify critical errors from the GSR register's FS
654 * field and for those errors, set error to -ENODEV or other
655 * appropriate errno, so that the status property is set to
656 * failure in the fsl,dprc device tree node.
657 */
J. German Riveracc088c32015-07-02 11:28:56 +0530658 printf("WARNING: Firmware returned an error (GSR: %#x)\n",
659 reg_gsr);
660 } else {
661 printf("SUCCESS\n");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700662 }
663
J. German Riveracc088c32015-07-02 11:28:56 +0530664
J. German Rivera125e2bc2015-03-20 19:28:18 -0700665 *final_reg_gsr = reg_gsr;
666 return 0;
667}
J. German Riverab940ca62014-06-23 15:15:55 -0700668
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530669int mc_init(u64 mc_fw_addr, u64 mc_dpc_addr)
J. German Riverab940ca62014-06-23 15:15:55 -0700670{
671 int error = 0;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700672 int portal_id = 0;
J. German Riverab940ca62014-06-23 15:15:55 -0700673 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530674 u64 mc_ram_addr = mc_get_dram_addr();
J. German Riverab940ca62014-06-23 15:15:55 -0700675 u32 reg_gsr;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700676 u32 reg_mcfbalr;
677#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Riverab940ca62014-06-23 15:15:55 -0700678 const void *raw_image_addr;
679 size_t raw_image_size = 0;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700680#endif
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800681 struct mc_version mc_ver_info;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700682 u8 mc_ram_num_256mb_blocks;
683 size_t mc_ram_size = mc_get_dram_block_size();
J. German Riverab940ca62014-06-23 15:15:55 -0700684
York Sun437858b62017-03-06 09:02:29 -0800685 mc_ram_num_256mb_blocks = mc_ram_size / MC_RAM_SIZE_ALIGNMENT;
Meenakshi Aggarwal43ad41e2019-02-27 14:41:02 +0530686
687 if (mc_ram_num_256mb_blocks >= 0xff) {
York Sun437858b62017-03-06 09:02:29 -0800688 error = -EINVAL;
689 printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
690 mc_ram_size);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700691 goto out;
York Sun437858b62017-03-06 09:02:29 -0800692 }
J. German Rivera125e2bc2015-03-20 19:28:18 -0700693
J. German Riverab940ca62014-06-23 15:15:55 -0700694 /*
Meenakshi Aggarwal43ad41e2019-02-27 14:41:02 +0530695 * To support 128 MB DDR Size for MC
696 */
697 if (mc_ram_num_256mb_blocks == 0)
698 mc_ram_num_256mb_blocks = 0xFF;
699
700 /*
J. German Riverab940ca62014-06-23 15:15:55 -0700701 * Management Complex cores should be held at reset out of POR.
Bin Menga1875592016-02-05 19:30:11 -0800702 * U-Boot should be the first software to touch MC. To be safe,
J. German Riverab940ca62014-06-23 15:15:55 -0700703 * we reset all cores again by setting GCR1 to 0. It doesn't do
704 * anything if they are held at reset. After we setup the firmware
705 * we kick off MC by deasserting the reset bit for core 0, and
706 * deasserting the reset bits for Command Portal Managers.
707 * The stop bits are not touched here. They are used to stop the
708 * cores when they are active. Setting stop bits doesn't stop the
709 * cores from fetching instructions when they are released from
710 * reset.
711 */
712 out_le32(&mc_ccsr_regs->reg_gcr1, 0);
713 dmb();
714
J. German Rivera125e2bc2015-03-20 19:28:18 -0700715#ifdef CONFIG_SYS_LS_MC_FW_IN_DDR
716 printf("MC firmware is preloaded to %#llx\n", mc_ram_addr);
717#else
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530718 error = parse_mc_firmware_fit_image(mc_fw_addr, &raw_image_addr,
719 &raw_image_size);
J. German Riverab940ca62014-06-23 15:15:55 -0700720 if (error != 0)
721 goto out;
722 /*
723 * Load the MC FW at the beginning of the MC private DRAM block:
724 */
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800725 mc_copy_image("MC Firmware",
726 (u64)raw_image_addr, raw_image_size, mc_ram_addr);
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800727#endif
J. German Rivera125e2bc2015-03-20 19:28:18 -0700728 dump_ram_words("firmware", (void *)mc_ram_addr);
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800729
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530730 error = load_mc_dpc(mc_ram_addr, mc_ram_size, mc_dpc_addr);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700731 if (error != 0)
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800732 goto out;
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800733
J. German Riverab940ca62014-06-23 15:15:55 -0700734 debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700735 dump_mc_ccsr_regs(mc_ccsr_regs);
J. German Riverab940ca62014-06-23 15:15:55 -0700736
737 /*
J. German Rivera125e2bc2015-03-20 19:28:18 -0700738 * Tell MC what is the address range of the DRAM block assigned to it:
J. German Riverab940ca62014-06-23 15:15:55 -0700739 */
Meenakshi Aggarwal43ad41e2019-02-27 14:41:02 +0530740 if (mc_ram_num_256mb_blocks < 0xFF) {
741 reg_mcfbalr = (u32)mc_ram_addr |
742 (mc_ram_num_256mb_blocks - 1);
743 } else {
744 reg_mcfbalr = (u32)mc_ram_addr |
745 (mc_ram_num_256mb_blocks);
746 }
747
J. German Rivera125e2bc2015-03-20 19:28:18 -0700748 out_le32(&mc_ccsr_regs->reg_mcfbalr, reg_mcfbalr);
749 out_le32(&mc_ccsr_regs->reg_mcfbahr,
York Sun437858b62017-03-06 09:02:29 -0800750 (u32)(mc_ram_addr >> 32));
Stuart Yoder39da6442015-07-02 11:29:02 +0530751 out_le32(&mc_ccsr_regs->reg_mcfapr, FSL_BYPASS_AMQ);
J. German Riverab940ca62014-06-23 15:15:55 -0700752
753 /*
J. German Rivera125e2bc2015-03-20 19:28:18 -0700754 * Tell the MC that we want delayed DPL deployment.
J. German Riverab940ca62014-06-23 15:15:55 -0700755 */
J. German Rivera125e2bc2015-03-20 19:28:18 -0700756 out_le32(&mc_ccsr_regs->reg_gsr, 0xDD00);
J. German Riverab940ca62014-06-23 15:15:55 -0700757
J. German Riveracc088c32015-07-02 11:28:56 +0530758 printf("\nfsl-mc: Booting Management Complex ... ");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800759
J. German Riverab940ca62014-06-23 15:15:55 -0700760 /*
761 * Deassert reset and release MC core 0 to run
762 */
763 out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700764 error = wait_for_mc(true, &reg_gsr);
765 if (error != 0)
J. German Riverab940ca62014-06-23 15:15:55 -0700766 goto out;
J. German Riverab940ca62014-06-23 15:15:55 -0700767
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800768 /*
769 * TODO: need to obtain the portal_id for the root container from the
770 * DPL
771 */
772 portal_id = 0;
773
774 /*
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700775 * Initialize the global default MC portal
776 * And check that the MC firmware is responding portal commands:
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800777 */
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530778 root_mc_io = (struct fsl_mc_io *)calloc(sizeof(struct fsl_mc_io), 1);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530779 if (!root_mc_io) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530780 printf(" No memory: calloc() failed\n");
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700781 return -ENOMEM;
782 }
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800783
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530784 root_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(portal_id);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700785 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 +0530786 portal_id, root_mc_io->mmio_regs);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700787
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530788 error = mc_get_version(root_mc_io, MC_CMD_NO_FLAGS, &mc_ver_info);
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800789 if (error != 0) {
790 printf("fsl-mc: ERROR: Firmware version check failed (error: %d)\n",
791 error);
792 goto out;
793 }
794
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800795 printf("fsl-mc: Management Complex booted (version: %d.%d.%d, boot status: %#x)\n",
796 mc_ver_info.major, mc_ver_info.minor, mc_ver_info.revision,
J. German Rivera125e2bc2015-03-20 19:28:18 -0700797 reg_gsr & GSR_FS_MASK);
798
J. German Riverab940ca62014-06-23 15:15:55 -0700799out:
800 if (error != 0)
Prabhakar Kushwaha2b7c4a12015-07-02 11:29:01 +0530801 mc_boot_status = error;
J. German Riverab940ca62014-06-23 15:15:55 -0700802 else
803 mc_boot_status = 0;
804
805 return error;
806}
807
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530808int mc_apply_dpl(u64 mc_dpl_addr)
809{
810 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
811 int error = 0;
812 u32 reg_gsr;
813 u64 mc_ram_addr = mc_get_dram_addr();
814 size_t mc_ram_size = mc_get_dram_block_size();
815
Alexander Grafb7b84102016-11-17 01:02:57 +0100816 if (!mc_dpl_addr)
817 return -1;
818
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530819 error = load_mc_dpl(mc_ram_addr, mc_ram_size, mc_dpl_addr);
820 if (error != 0)
821 return error;
822
823 /*
824 * Tell the MC to deploy the DPL:
825 */
826 out_le32(&mc_ccsr_regs->reg_gsr, 0x0);
827 printf("fsl-mc: Deploying data path layout ... ");
828 error = wait_for_mc(false, &reg_gsr);
829
830 if (!error)
831 mc_dpl_applied = 0;
832
833 return error;
834}
835
J. German Riverab940ca62014-06-23 15:15:55 -0700836int get_mc_boot_status(void)
837{
838 return mc_boot_status;
839}
840
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530841#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
842int get_aiop_apply_status(void)
843{
844 return mc_aiop_applied;
845}
846#endif
847
848int get_dpl_apply_status(void)
849{
850 return mc_dpl_applied;
851}
852
Mian Yousaf Kaukab7e968042018-12-18 14:01:17 +0100853int is_lazy_dpl_addr_valid(void)
854{
855 return !!mc_lazy_dpl_addr;
856}
857
Priyanka Jain033c5382017-08-24 16:42:43 +0530858/*
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530859 * Return the MC address of private DRAM block.
Priyanka Jain033c5382017-08-24 16:42:43 +0530860 * As per MC design document, MC initial base address
861 * should be least significant 512MB address of MC private
862 * memory, i.e. address should point to end address masked
863 * with 512MB offset in private DRAM block.
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530864 */
865u64 mc_get_dram_addr(void)
866{
Priyanka Jain033c5382017-08-24 16:42:43 +0530867 size_t mc_ram_size = mc_get_dram_block_size();
868
Prabhakar Kushwaha87519a92018-08-27 12:58:52 +0530869 if (!mc_memset_resv_ram || (get_mc_boot_status() < 0)) {
870 mc_memset_resv_ram = 1;
871 memset((void *)gd->arch.resv_ram, 0, mc_ram_size);
872 }
873
Priyanka Jain033c5382017-08-24 16:42:43 +0530874 return (gd->arch.resv_ram + mc_ram_size - 1) &
875 MC_RAM_BASE_ADDR_ALIGNMENT_MASK;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530876}
877
J. German Riverab940ca62014-06-23 15:15:55 -0700878/**
879 * Return the actual size of the MC private DRAM block.
J. German Riverab940ca62014-06-23 15:15:55 -0700880 */
881unsigned long mc_get_dram_block_size(void)
882{
J. German Rivera125e2bc2015-03-20 19:28:18 -0700883 unsigned long dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
884
Simon Glass00caae62017-08-03 12:22:12 -0600885 char *dram_block_size_env_var = env_get(MC_MEM_SIZE_ENV_VAR);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700886
887 if (dram_block_size_env_var) {
888 dram_block_size = simple_strtoul(dram_block_size_env_var, NULL,
Prabhakar Kushwahaf53e12d2017-11-09 14:45:41 +0530889 16);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700890
891 if (dram_block_size < CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE) {
892 printf("fsl-mc: WARNING: Invalid value for \'"
893 MC_MEM_SIZE_ENV_VAR
894 "\' environment variable: %lu\n",
895 dram_block_size);
896
Meenakshi Aggarwal43ad41e2019-02-27 14:41:02 +0530897 dram_block_size = MC_DRAM_BLOCK_DEFAULT_SIZE;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700898 }
899 }
900
901 return dram_block_size;
J. German Riverab940ca62014-06-23 15:15:55 -0700902}
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700903
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530904int fsl_mc_ldpaa_init(bd_t *bis)
905{
Prabhakar Kushwahac919ab92015-11-04 12:26:00 +0530906 int i;
907
908 for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++)
Pankaj Bansal1ed19a12018-10-10 14:08:33 +0530909 if (wriop_is_enabled_dpmac(i) == 1)
Prabhakar Kushwahac919ab92015-11-04 12:26:00 +0530910 ldpaa_eth_init(i, wriop_get_enet_if(i));
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530911 return 0;
912}
913
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530914static int dprc_version_check(struct fsl_mc_io *mc_io, uint16_t handle)
915{
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530916 int error;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530917 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530918
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530919 error = dprc_get_api_version(mc_io, 0,
920 &major_ver,
921 &minor_ver);
922 if (error < 0) {
923 printf("dprc_get_api_version() failed: %d\n", error);
924 return error;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530925 }
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530926
927 if (major_ver < DPRC_VER_MAJOR || (major_ver == DPRC_VER_MAJOR &&
928 minor_ver < DPRC_VER_MINOR)) {
929 printf("DPRC version mismatch found %u.%u,",
930 major_ver, minor_ver);
931 printf("supported version is %u.%u\n",
932 DPRC_VER_MAJOR, DPRC_VER_MINOR);
933 }
934
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530935 return error;
936}
937
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530938static int dpio_init(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700939{
940 struct qbman_swp_desc p_des;
941 struct dpio_attr attr;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530942 struct dpio_cfg dpio_cfg;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700943 int err = 0;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530944 uint16_t major_ver, minor_ver;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700945
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530946 dflt_dpio = (struct fsl_dpio_obj *)calloc(
947 sizeof(struct fsl_dpio_obj), 1);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700948 if (!dflt_dpio) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530949 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530950 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530951 goto err_calloc;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700952 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530953 dpio_cfg.channel_mode = DPIO_LOCAL_CHANNEL;
954 dpio_cfg.num_priorities = 8;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700955
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530956 err = dpio_create(dflt_mc_io,
957 dflt_dprc_handle,
958 MC_CMD_NO_FLAGS,
959 &dpio_cfg,
960 &dflt_dpio->dpio_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530961 if (err < 0) {
962 printf("dpio_create() failed: %d\n", err);
963 err = -ENODEV;
964 goto err_create;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700965 }
966
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530967 err = dpio_get_api_version(dflt_mc_io, 0,
968 &major_ver,
969 &minor_ver);
970 if (err < 0) {
971 printf("dpio_get_api_version() failed: %d\n", err);
972 goto err_get_api_ver;
973 }
974
975 if (major_ver < DPIO_VER_MAJOR || (major_ver == DPIO_VER_MAJOR &&
976 minor_ver < DPIO_VER_MINOR)) {
977 printf("DPRC version mismatch found %u.%u,",
978 major_ver,
979 minor_ver);
980 }
981
982 err = dpio_open(dflt_mc_io,
983 MC_CMD_NO_FLAGS,
984 dflt_dpio->dpio_id,
985 &dflt_dpio->dpio_handle);
986 if (err) {
987 printf("dpio_open() failed\n");
988 goto err_open;
989 }
990
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530991 memset(&attr, 0, sizeof(struct dpio_attr));
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +0530992 err = dpio_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530993 dflt_dpio->dpio_handle, &attr);
994 if (err < 0) {
995 printf("dpio_get_attributes() failed: %d\n", err);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700996 goto err_get_attr;
997 }
998
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530999 if (dflt_dpio->dpio_id != attr.id) {
1000 printf("dnpi object id and attribute id are not same\n");
1001 goto err_attr_not_same;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301002 }
1003
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301004#ifdef DEBUG
1005 printf("Init: DPIO id=0x%d\n", dflt_dpio->dpio_id);
1006#endif
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301007 err = dpio_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1008 if (err < 0) {
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001009 printf("dpio_enable() failed %d\n", err);
1010 goto err_get_enable;
1011 }
Prabhakar Kushwaha1f1c25c2015-07-02 11:28:59 +05301012 debug("ce_offset=0x%llx, ci_offset=0x%llx, portalid=%d, prios=%d\n",
1013 attr.qbman_portal_ce_offset,
1014 attr.qbman_portal_ci_offset,
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001015 attr.qbman_portal_id,
1016 attr.num_priorities);
1017
Prabhakar Kushwaha1f1c25c2015-07-02 11:28:59 +05301018 p_des.cena_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
1019 + attr.qbman_portal_ce_offset);
1020 p_des.cinh_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
1021 + attr.qbman_portal_ci_offset);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001022
1023 dflt_dpio->sw_portal = qbman_swp_init(&p_des);
1024 if (dflt_dpio->sw_portal == NULL) {
1025 printf("qbman_swp_init() failed\n");
1026 goto err_get_swp_init;
1027 }
1028 return 0;
1029
1030err_get_swp_init:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301031 dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001032err_get_enable:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301033err_get_attr:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301034err_attr_not_same:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301035 dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301036err_open:
1037err_get_api_ver:
1038 dpio_destroy(dflt_mc_io,
1039 dflt_dprc_handle,
1040 MC_CMD_NO_FLAGS,
1041 dflt_dpio->dpio_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301042err_create:
Prabhakar Kushwahacd7b3fb2016-03-18 16:15:29 +05301043 free(dflt_dpio);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301044err_calloc:
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001045 return err;
1046}
1047
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301048static int dpio_exit(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001049{
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301050 int err;
1051
1052 err = dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1053 if (err < 0) {
1054 printf("dpio_disable() failed: %d\n", err);
1055 goto err;
1056 }
1057
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301058 dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1059 if (err < 0) {
1060 printf("dpio_close() failed: %d\n", err);
1061 goto err;
1062 }
1063
1064 err = dpio_destroy(dflt_mc_io,
1065 dflt_dprc_handle,
1066 MC_CMD_NO_FLAGS,
1067 dflt_dpio->dpio_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301068 if (err < 0) {
1069 printf("dpio_destroy() failed: %d\n", err);
1070 goto err;
1071 }
1072
1073#ifdef DEBUG
1074 printf("Exit: DPIO id=0x%d\n", dflt_dpio->dpio_id);
1075#endif
1076
1077 if (dflt_dpio)
1078 free(dflt_dpio);
1079
1080 return 0;
1081err:
1082 return err;
1083}
1084
1085static int dprc_init(void)
1086{
1087 int err, child_portal_id, container_id;
1088 struct dprc_cfg cfg;
1089 uint64_t mc_portal_offset;
1090
1091 /* Open root container */
1092 err = dprc_get_container_id(root_mc_io, MC_CMD_NO_FLAGS, &container_id);
1093 if (err < 0) {
1094 printf("dprc_get_container_id(): Root failed: %d\n", err);
1095 goto err_root_container_id;
1096 }
1097
1098#ifdef DEBUG
1099 printf("Root container id = %d\n", container_id);
1100#endif
1101 err = dprc_open(root_mc_io, MC_CMD_NO_FLAGS, container_id,
1102 &root_dprc_handle);
1103 if (err < 0) {
1104 printf("dprc_open(): Root Container failed: %d\n", err);
1105 goto err_root_open;
1106 }
1107
1108 if (!root_dprc_handle) {
1109 printf("dprc_open(): Root Container Handle is not valid\n");
1110 goto err_root_open;
1111 }
1112
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301113 err = dprc_version_check(root_mc_io, root_dprc_handle);
1114 if (err < 0) {
1115 printf("dprc_version_check() failed: %d\n", err);
1116 goto err_root_open;
1117 }
1118
Prabhakar Kushwaha5373b202016-01-20 12:04:19 +05301119 memset(&cfg, 0, sizeof(struct dprc_cfg));
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301120 cfg.options = DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED |
1121 DPRC_CFG_OPT_OBJ_CREATE_ALLOWED |
1122 DPRC_CFG_OPT_ALLOC_ALLOWED;
1123 cfg.icid = DPRC_GET_ICID_FROM_POOL;
Prabhakar Kushwaha335b1932015-12-24 15:33:49 +05301124 cfg.portal_id = DPRC_GET_PORTAL_ID_FROM_POOL;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301125 err = dprc_create_container(root_mc_io, MC_CMD_NO_FLAGS,
1126 root_dprc_handle,
1127 &cfg,
1128 &child_dprc_id,
1129 &mc_portal_offset);
1130 if (err < 0) {
1131 printf("dprc_create_container() failed: %d\n", err);
1132 goto err_create;
1133 }
1134
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301135 dflt_mc_io = (struct fsl_mc_io *)calloc(sizeof(struct fsl_mc_io), 1);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301136 if (!dflt_mc_io) {
1137 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301138 printf(" No memory: calloc() failed\n");
1139 goto err_calloc;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301140 }
1141
1142 child_portal_id = MC_PORTAL_OFFSET_TO_PORTAL_ID(mc_portal_offset);
1143 dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(child_portal_id);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301144
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301145#ifdef DEBUG
1146 printf("MC portal of child DPRC container: %d, physical addr %p)\n",
1147 child_dprc_id, dflt_mc_io->mmio_regs);
1148#endif
1149
1150 err = dprc_open(dflt_mc_io, MC_CMD_NO_FLAGS, child_dprc_id,
1151 &dflt_dprc_handle);
1152 if (err < 0) {
1153 printf("dprc_open(): Child container failed: %d\n", err);
1154 goto err_child_open;
1155 }
1156
1157 if (!dflt_dprc_handle) {
1158 printf("dprc_open(): Child container Handle is not valid\n");
1159 goto err_child_open;
1160 }
1161
1162 return 0;
1163err_child_open:
1164 free(dflt_mc_io);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301165err_calloc:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301166 dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1167 root_dprc_handle, child_dprc_id);
1168err_create:
1169 dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1170err_root_open:
1171err_root_container_id:
1172 return err;
1173}
1174
1175static int dprc_exit(void)
1176{
1177 int err;
1178
1179 err = dprc_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dprc_handle);
1180 if (err < 0) {
1181 printf("dprc_close(): Child failed: %d\n", err);
1182 goto err;
1183 }
1184
1185 err = dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1186 root_dprc_handle, child_dprc_id);
1187 if (err < 0) {
1188 printf("dprc_destroy_container() failed: %d\n", err);
1189 goto err;
1190 }
1191
1192 err = dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1193 if (err < 0) {
1194 printf("dprc_close(): Root failed: %d\n", err);
1195 goto err;
1196 }
1197
1198 if (dflt_mc_io)
1199 free(dflt_mc_io);
1200
1201 if (root_mc_io)
1202 free(root_mc_io);
1203
1204 return 0;
1205
1206err:
1207 return err;
1208}
1209
1210static int dpbp_init(void)
1211{
1212 int err;
1213 struct dpbp_attr dpbp_attr;
1214 struct dpbp_cfg dpbp_cfg;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301215 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301216
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301217 dflt_dpbp = (struct fsl_dpbp_obj *)calloc(
1218 sizeof(struct fsl_dpbp_obj), 1);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001219 if (!dflt_dpbp) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301220 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301221 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301222 goto err_calloc;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001223 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301224
1225 dpbp_cfg.options = 512;
1226
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301227 err = dpbp_create(dflt_mc_io,
1228 dflt_dprc_handle,
1229 MC_CMD_NO_FLAGS,
1230 &dpbp_cfg,
1231 &dflt_dpbp->dpbp_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301232
1233 if (err < 0) {
1234 err = -ENODEV;
1235 printf("dpbp_create() failed: %d\n", err);
1236 goto err_create;
1237 }
1238
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301239 err = dpbp_get_api_version(dflt_mc_io, 0,
1240 &major_ver,
1241 &minor_ver);
1242 if (err < 0) {
1243 printf("dpbp_get_api_version() failed: %d\n", err);
1244 goto err_get_api_ver;
1245 }
1246
1247 if (major_ver < DPBP_VER_MAJOR || (major_ver == DPBP_VER_MAJOR &&
1248 minor_ver < DPBP_VER_MINOR)) {
1249 printf("DPBP version mismatch found %u.%u,",
1250 major_ver, minor_ver);
1251 printf("supported version is %u.%u\n",
1252 DPBP_VER_MAJOR, DPBP_VER_MINOR);
1253 }
1254
1255 err = dpbp_open(dflt_mc_io,
1256 MC_CMD_NO_FLAGS,
1257 dflt_dpbp->dpbp_id,
1258 &dflt_dpbp->dpbp_handle);
1259 if (err) {
1260 printf("dpbp_open() failed\n");
1261 goto err_open;
1262 }
1263
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301264 memset(&dpbp_attr, 0, sizeof(struct dpbp_attr));
1265 err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
1266 dflt_dpbp->dpbp_handle,
1267 &dpbp_attr);
1268 if (err < 0) {
1269 printf("dpbp_get_attributes() failed: %d\n", err);
1270 goto err_get_attr;
1271 }
1272
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301273 if (dflt_dpbp->dpbp_id != dpbp_attr.id) {
1274 printf("dpbp object id and attribute id are not same\n");
1275 goto err_attr_not_same;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301276 }
1277
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301278#ifdef DEBUG
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301279 printf("Init: DPBP id=0x%x\n", dflt_dpbp->dpbp_attr.id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301280#endif
1281
1282 err = dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
1283 if (err < 0) {
1284 printf("dpbp_close() failed: %d\n", err);
1285 goto err_close;
1286 }
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001287
1288 return 0;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301289
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301290err_get_attr:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301291err_attr_not_same:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301292 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301293 dpbp_destroy(dflt_mc_io,
1294 dflt_dprc_handle,
1295 MC_CMD_NO_FLAGS,
1296 dflt_dpbp->dpbp_id);
1297err_get_api_ver:
1298err_close:
1299err_open:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301300err_create:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301301 free(dflt_dpbp);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301302err_calloc:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301303 return err;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001304}
1305
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301306static int dpbp_exit(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001307{
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301308 int err;
1309
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301310 err = dpbp_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1311 dflt_dpbp->dpbp_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301312 if (err < 0) {
1313 printf("dpbp_destroy() failed: %d\n", err);
1314 goto err;
1315 }
1316
1317#ifdef DEBUG
1318 printf("Exit: DPBP id=0x%d\n", dflt_dpbp->dpbp_attr.id);
1319#endif
1320
1321 if (dflt_dpbp)
1322 free(dflt_dpbp);
1323 return 0;
1324
1325err:
1326 return err;
1327}
1328
1329static int dpni_init(void)
1330{
1331 int err;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301332 uint8_t cfg_buf[256] = {0};
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301333 struct dpni_cfg dpni_cfg;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301334 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301335
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301336 dflt_dpni = (struct fsl_dpni_obj *)calloc(
1337 sizeof(struct fsl_dpni_obj), 1);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301338 if (!dflt_dpni) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301339 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301340 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301341 goto err_calloc;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301342 }
1343
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301344 memset(&dpni_cfg, 0, sizeof(dpni_cfg));
1345 err = dpni_prepare_cfg(&dpni_cfg, &cfg_buf[0]);
Prabhakar Kushwaha879a59a2015-12-24 15:33:01 +05301346 if (err < 0) {
1347 err = -ENODEV;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301348 printf("dpni_prepare_cfg() failed: %d\n", err);
1349 goto err_prepare_cfg;
Prabhakar Kushwaha879a59a2015-12-24 15:33:01 +05301350 }
1351
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301352 err = dpni_create(dflt_mc_io,
1353 dflt_dprc_handle,
1354 MC_CMD_NO_FLAGS,
1355 &dpni_cfg,
1356 &dflt_dpni->dpni_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301357 if (err < 0) {
1358 err = -ENODEV;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301359 printf("dpni create() failed: %d\n", err);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301360 goto err_create;
1361 }
1362
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301363 err = dpni_get_api_version(dflt_mc_io, 0,
1364 &major_ver,
1365 &minor_ver);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301366 if (err < 0) {
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301367 printf("dpni_get_api_version() failed: %d\n", err);
1368 goto err_get_version;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301369 }
1370
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301371 if (major_ver < DPNI_VER_MAJOR || (major_ver == DPNI_VER_MAJOR &&
1372 minor_ver < DPNI_VER_MINOR)) {
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301373 printf("DPNI version mismatch found %u.%u,",
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301374 major_ver, minor_ver);
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301375 printf("supported version is %u.%u\n",
1376 DPNI_VER_MAJOR, DPNI_VER_MINOR);
1377 }
1378
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301379 err = dpni_open(dflt_mc_io,
1380 MC_CMD_NO_FLAGS,
1381 dflt_dpni->dpni_id,
1382 &dflt_dpni->dpni_handle);
1383 if (err) {
1384 printf("dpni_open() failed\n");
1385 goto err_open;
1386 }
1387
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301388#ifdef DEBUG
1389 printf("Init: DPNI id=0x%d\n", dflt_dpni->dpni_id);
1390#endif
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301391 err = dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
1392 if (err < 0) {
1393 printf("dpni_close() failed: %d\n", err);
1394 goto err_close;
1395 }
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001396
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301397 return 0;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301398
1399err_close:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301400 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301401err_open:
1402err_get_version:
1403 dpni_destroy(dflt_mc_io,
1404 dflt_dprc_handle,
1405 MC_CMD_NO_FLAGS,
1406 dflt_dpni->dpni_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301407err_create:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301408err_prepare_cfg:
Prabhakar Kushwaha879a59a2015-12-24 15:33:01 +05301409 free(dflt_dpni);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301410err_calloc:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301411 return err;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001412}
1413
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301414static int dpni_exit(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001415{
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301416 int err;
1417
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301418 err = dpni_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1419 dflt_dpni->dpni_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301420 if (err < 0) {
1421 printf("dpni_destroy() failed: %d\n", err);
1422 goto err;
1423 }
1424
1425#ifdef DEBUG
1426 printf("Exit: DPNI id=0x%d\n", dflt_dpni->dpni_id);
1427#endif
1428
1429 if (dflt_dpni)
1430 free(dflt_dpni);
1431 return 0;
1432
1433err:
1434 return err;
1435}
1436
1437static int mc_init_object(void)
1438{
1439 int err = 0;
1440
1441 err = dprc_init();
1442 if (err < 0) {
1443 printf("dprc_init() failed: %d\n", err);
1444 goto err;
1445 }
1446
1447 err = dpbp_init();
1448 if (err < 0) {
1449 printf("dpbp_init() failed: %d\n", err);
1450 goto err;
1451 }
1452
1453 err = dpio_init();
1454 if (err < 0) {
1455 printf("dpio_init() failed: %d\n", err);
1456 goto err;
1457 }
1458
1459 err = dpni_init();
1460 if (err < 0) {
1461 printf("dpni_init() failed: %d\n", err);
1462 goto err;
1463 }
1464
1465 return 0;
1466err:
1467 return err;
1468}
1469
1470int fsl_mc_ldpaa_exit(bd_t *bd)
1471{
1472 int err = 0;
Yogesh Gaur42e81792017-04-27 10:14:16 +05301473 bool is_dpl_apply_status = false;
Santan Kumar06651b92017-06-29 11:19:34 +05301474 bool mc_boot_status = false;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301475
Alexander Grafb7b84102016-11-17 01:02:57 +01001476 if (bd && mc_lazy_dpl_addr && !fsl_mc_ldpaa_exit(NULL)) {
Yogesh Gaurf9747a52018-01-16 10:08:24 +05301477 err = mc_apply_dpl(mc_lazy_dpl_addr);
1478 if (!err)
1479 fdt_fixup_board_enet(working_fdt);
Alexander Grafb7b84102016-11-17 01:02:57 +01001480 mc_lazy_dpl_addr = 0;
1481 }
1482
Santan Kumar06651b92017-06-29 11:19:34 +05301483 if (!get_mc_boot_status())
1484 mc_boot_status = true;
1485
Prabhakar Kushwaha6dedced2016-03-21 14:19:39 +05301486 /* MC is not loaded intentionally, So return success. */
Santan Kumar06651b92017-06-29 11:19:34 +05301487 if (bd && !mc_boot_status)
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301488 return 0;
1489
Yogesh Gaur42e81792017-04-27 10:14:16 +05301490 /* If DPL is deployed, set is_dpl_apply_status as TRUE. */
1491 if (!get_dpl_apply_status())
1492 is_dpl_apply_status = true;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301493
Yogesh Gaur42e81792017-04-27 10:14:16 +05301494 /*
1495 * For case MC is loaded but DPL is not deployed, return success and
1496 * print message on console. Else FDT fix-up code execution hanged.
1497 */
Santan Kumar06651b92017-06-29 11:19:34 +05301498 if (bd && mc_boot_status && !is_dpl_apply_status) {
Yogesh Gaur42e81792017-04-27 10:14:16 +05301499 printf("fsl-mc: DPL not deployed, DPAA2 ethernet not work\n");
Yogesh Gaur73fa2062017-11-28 10:11:14 +05301500 goto mc_obj_cleanup;
Yogesh Gaur42e81792017-04-27 10:14:16 +05301501 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301502
Santan Kumar06651b92017-06-29 11:19:34 +05301503 if (bd && mc_boot_status && is_dpl_apply_status)
1504 return 0;
1505
Yogesh Gaur73fa2062017-11-28 10:11:14 +05301506mc_obj_cleanup:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301507 err = dpbp_exit();
1508 if (err < 0) {
Prabhakar Kushwahaa2a4dc52016-01-20 12:04:37 +05301509 printf("dpbp_exit() failed: %d\n", err);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301510 goto err;
1511 }
1512
1513 err = dpio_exit();
1514 if (err < 0) {
1515 printf("dpio_exit() failed: %d\n", err);
1516 goto err;
1517 }
1518
1519 err = dpni_exit();
1520 if (err < 0) {
1521 printf("dpni_exit() failed: %d\n", err);
1522 goto err;
1523 }
1524
1525 err = dprc_exit();
1526 if (err < 0) {
1527 printf("dprc_exit() failed: %d\n", err);
1528 goto err;
1529 }
1530
1531 return 0;
1532err:
1533 return err;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001534}
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301535
1536static int do_fsl_mc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1537{
1538 int err = 0;
1539 if (argc < 3)
1540 goto usage;
1541
1542 switch (argv[1][0]) {
1543 case 's': {
1544 char sub_cmd;
Prabhakar Kushwaha44937212015-11-09 16:42:07 +05301545 u64 mc_fw_addr, mc_dpc_addr;
1546#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
1547 u64 aiop_fw_addr;
1548#endif
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301549
1550 sub_cmd = argv[2][0];
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301551
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301552 switch (sub_cmd) {
1553 case 'm':
1554 if (argc < 5)
1555 goto usage;
1556
1557 if (get_mc_boot_status() == 0) {
1558 printf("fsl-mc: MC is already booted");
1559 printf("\n");
1560 return err;
1561 }
1562 mc_fw_addr = simple_strtoull(argv[3], NULL, 16);
1563 mc_dpc_addr = simple_strtoull(argv[4], NULL,
1564 16);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301565
1566 if (!mc_init(mc_fw_addr, mc_dpc_addr))
1567 err = mc_init_object();
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301568 break;
1569
1570#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
1571 case 'a':
1572 if (argc < 4)
1573 goto usage;
1574 if (get_aiop_apply_status() == 0) {
1575 printf("fsl-mc: AIOP FW is already");
1576 printf(" applied\n");
1577 return err;
1578 }
1579
1580 aiop_fw_addr = simple_strtoull(argv[3], NULL,
1581 16);
1582
York Sun3c1d2182016-04-04 11:41:26 -07001583 /* if SoC doesn't have AIOP, err = -ENODEV */
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301584 err = load_mc_aiop_img(aiop_fw_addr);
1585 if (!err)
1586 printf("fsl-mc: AIOP FW applied\n");
1587 break;
1588#endif
1589 default:
1590 printf("Invalid option: %s\n", argv[2]);
1591 goto usage;
1592
1593 break;
1594 }
1595 }
1596 break;
1597
Alexander Grafb7b84102016-11-17 01:02:57 +01001598 case 'l':
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301599 case 'a': {
1600 u64 mc_dpl_addr;
1601
1602 if (argc < 4)
1603 goto usage;
1604
1605 if (get_dpl_apply_status() == 0) {
1606 printf("fsl-mc: DPL already applied\n");
1607 return err;
1608 }
1609
1610 mc_dpl_addr = simple_strtoull(argv[3], NULL,
1611 16);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301612
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301613 if (get_mc_boot_status() != 0) {
1614 printf("fsl-mc: Deploying data path layout ..");
1615 printf("ERROR (MC is not booted)\n");
1616 return -ENODEV;
1617 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301618
Alexander Grafb7b84102016-11-17 01:02:57 +01001619 if (argv[1][0] == 'l') {
1620 /*
1621 * We will do the actual dpaa exit and dpl apply
1622 * later from announce_and_cleanup().
1623 */
1624 mc_lazy_dpl_addr = mc_dpl_addr;
1625 } else {
1626 /* The user wants it applied now */
1627 if (!fsl_mc_ldpaa_exit(NULL))
1628 err = mc_apply_dpl(mc_dpl_addr);
1629 }
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301630 break;
1631 }
1632 default:
1633 printf("Invalid option: %s\n", argv[1]);
1634 goto usage;
1635 break;
1636 }
1637 return err;
1638 usage:
1639 return CMD_RET_USAGE;
1640}
1641
1642U_BOOT_CMD(
1643 fsl_mc, CONFIG_SYS_MAXARGS, 1, do_fsl_mc,
1644 "DPAA2 command to manage Management Complex (MC)",
1645 "start mc [FW_addr] [DPC_addr] - Start Management Complex\n"
1646 "fsl_mc apply DPL [DPL_addr] - Apply DPL file\n"
Alexander Grafb7b84102016-11-17 01:02:57 +01001647 "fsl_mc lazyapply DPL [DPL_addr] - Apply DPL file on exit\n"
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301648 "fsl_mc start aiop [FW_addr] - Start AIOP\n"
1649);
Bogdan Purcareata33a89912017-05-24 16:40:21 +00001650
1651void mc_env_boot(void)
1652{
1653#if defined(CONFIG_FSL_MC_ENET)
1654 char *mc_boot_env_var;
1655 /* The MC may only be initialized in the reset PHY function
1656 * because otherwise U-Boot has not yet set up all the MAC
1657 * address info properly. Without MAC addresses, the MC code
1658 * can not properly initialize the DPC.
1659 */
Simon Glass00caae62017-08-03 12:22:12 -06001660 mc_boot_env_var = env_get(MC_BOOT_ENV_VAR);
Bogdan Purcareata33a89912017-05-24 16:40:21 +00001661 if (mc_boot_env_var)
1662 run_command_list(mc_boot_env_var, -1, 0);
1663#endif /* CONFIG_FSL_MC_ENET */
1664}