blob: cb2fbe9e2f1d3572da087a9b3580606d5b368ce0 [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
J. German Riverab940ca62014-06-23 15:15:55 -07005 */
Stuart Yoder21c69872015-07-02 11:29:03 +05306#include <common.h>
J. German Riverab940ca62014-06-23 15:15:55 -07007#include <errno.h>
Masahiro Yamada84b8bf62016-01-24 23:27:48 +09008#include <linux/bug.h>
J. German Riverab940ca62014-06-23 15:15:55 -07009#include <asm/io.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +000011#include <net.h>
Stuart Yoder21c69872015-07-02 11:29:03 +053012#include <fdt_support.h>
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080013#include <fsl-mc/fsl_mc.h>
14#include <fsl-mc/fsl_mc_sys.h>
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070015#include <fsl-mc/fsl_mc_private.h>
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080016#include <fsl-mc/fsl_dpmng.h>
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070017#include <fsl-mc/fsl_dprc.h>
18#include <fsl-mc/fsl_dpio.h>
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053019#include <fsl-mc/fsl_dpni.h>
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070020#include <fsl-mc/fsl_qbman_portal.h>
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053021#include <fsl-mc/ldpaa_wriop.h>
J. German Riverab940ca62014-06-23 15:15:55 -070022
J. German Rivera125e2bc2015-03-20 19:28:18 -070023#define MC_RAM_BASE_ADDR_ALIGNMENT (512UL * 1024 * 1024)
24#define MC_RAM_BASE_ADDR_ALIGNMENT_MASK (~(MC_RAM_BASE_ADDR_ALIGNMENT - 1))
25#define MC_RAM_SIZE_ALIGNMENT (256UL * 1024 * 1024)
26
27#define MC_MEM_SIZE_ENV_VAR "mcmemsize"
28#define MC_BOOT_TIMEOUT_ENV_VAR "mcboottimeout"
Bogdan Purcareata33a89912017-05-24 16:40:21 +000029#define MC_BOOT_ENV_VAR "mcinitcmd"
J. German Rivera125e2bc2015-03-20 19:28:18 -070030
J. German Riverab940ca62014-06-23 15:15:55 -070031DECLARE_GLOBAL_DATA_PTR;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053032static int mc_boot_status = -1;
33static int mc_dpl_applied = -1;
34#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
35static int mc_aiop_applied = -1;
36#endif
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +053037struct fsl_mc_io *root_mc_io = NULL;
38struct fsl_mc_io *dflt_mc_io = NULL; /* child container */
39uint16_t root_dprc_handle = 0;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070040uint16_t dflt_dprc_handle = 0;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +053041int child_dprc_id;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070042struct fsl_dpbp_obj *dflt_dpbp = NULL;
43struct fsl_dpio_obj *dflt_dpio = NULL;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +053044struct fsl_dpni_obj *dflt_dpni = NULL;
Alexander Grafb7b84102016-11-17 01:02:57 +010045static u64 mc_lazy_dpl_addr;
J. German Riverab940ca62014-06-23 15:15:55 -070046
J. German Rivera125e2bc2015-03-20 19:28:18 -070047#ifdef DEBUG
48void dump_ram_words(const char *title, void *addr)
49{
50 int i;
51 uint32_t *words = addr;
52
53 printf("Dumping beginning of %s (%p):\n", title, addr);
54 for (i = 0; i < 16; i++)
55 printf("%#x ", words[i]);
56
57 printf("\n");
58}
59
60void dump_mc_ccsr_regs(struct mc_ccsr_registers __iomem *mc_ccsr_regs)
61{
62 printf("MC CCSR registers:\n"
63 "reg_gcr1 %#x\n"
64 "reg_gsr %#x\n"
65 "reg_sicbalr %#x\n"
66 "reg_sicbahr %#x\n"
67 "reg_sicapr %#x\n"
68 "reg_mcfbalr %#x\n"
69 "reg_mcfbahr %#x\n"
70 "reg_mcfapr %#x\n"
71 "reg_psr %#x\n",
72 mc_ccsr_regs->reg_gcr1,
73 mc_ccsr_regs->reg_gsr,
74 mc_ccsr_regs->reg_sicbalr,
75 mc_ccsr_regs->reg_sicbahr,
76 mc_ccsr_regs->reg_sicapr,
77 mc_ccsr_regs->reg_mcfbalr,
78 mc_ccsr_regs->reg_mcfbahr,
79 mc_ccsr_regs->reg_mcfapr,
80 mc_ccsr_regs->reg_psr);
81}
82#else
83
84#define dump_ram_words(title, addr)
85#define dump_mc_ccsr_regs(mc_ccsr_regs)
86
87#endif /* DEBUG */
88
89#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Riverab940ca62014-06-23 15:15:55 -070090/**
91 * Copying MC firmware or DPL image to DDR
92 */
93static int mc_copy_image(const char *title,
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080094 u64 image_addr, u32 image_size, u64 mc_ram_addr)
J. German Riverab940ca62014-06-23 15:15:55 -070095{
96 debug("%s copied to address %p\n", title, (void *)mc_ram_addr);
97 memcpy((void *)mc_ram_addr, (void *)image_addr, image_size);
J. German Rivera125e2bc2015-03-20 19:28:18 -070098 flush_dcache_range(mc_ram_addr, mc_ram_addr + image_size);
J. German Riverab940ca62014-06-23 15:15:55 -070099 return 0;
100}
101
102/**
103 * MC firmware FIT image parser checks if the image is in FIT
104 * format, verifies integrity of the image and calculates
105 * raw image address and size values.
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800106 * Returns 0 on success and a negative errno on error.
J. German Riverab940ca62014-06-23 15:15:55 -0700107 * task fail.
108 **/
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530109int parse_mc_firmware_fit_image(u64 mc_fw_addr,
110 const void **raw_image_addr,
J. German Riverab940ca62014-06-23 15:15:55 -0700111 size_t *raw_image_size)
112{
113 int format;
114 void *fit_hdr;
115 int node_offset;
116 const void *data;
117 size_t size;
118 const char *uname = "firmware";
119
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530120 fit_hdr = (void *)mc_fw_addr;
J. German Riverab940ca62014-06-23 15:15:55 -0700121
122 /* Check if Image is in FIT format */
123 format = genimg_get_format(fit_hdr);
124
125 if (format != IMAGE_FORMAT_FIT) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530126 printf("fsl-mc: ERR: Bad firmware image (not a FIT image)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800127 return -EINVAL;
J. German Riverab940ca62014-06-23 15:15:55 -0700128 }
129
130 if (!fit_check_format(fit_hdr)) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530131 printf("fsl-mc: ERR: Bad firmware image (bad FIT header)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800132 return -EINVAL;
J. German Riverab940ca62014-06-23 15:15:55 -0700133 }
134
135 node_offset = fit_image_get_node(fit_hdr, uname);
136
137 if (node_offset < 0) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530138 printf("fsl-mc: ERR: Bad firmware image (missing subimage)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800139 return -ENOENT;
J. German Riverab940ca62014-06-23 15:15:55 -0700140 }
141
142 /* Verify MC firmware image */
143 if (!(fit_image_verify(fit_hdr, node_offset))) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530144 printf("fsl-mc: ERR: Bad firmware image (bad CRC)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800145 return -EINVAL;
J. German Riverab940ca62014-06-23 15:15:55 -0700146 }
147
148 /* Get address and size of raw image */
149 fit_image_get_data(fit_hdr, node_offset, &data, &size);
150
151 *raw_image_addr = data;
152 *raw_image_size = size;
153
154 return 0;
155}
J. German Rivera125e2bc2015-03-20 19:28:18 -0700156#endif
157
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000158#define MC_DT_INCREASE_SIZE 64
159
160enum mc_fixup_type {
161 MC_FIXUP_DPL,
162 MC_FIXUP_DPC
163};
164
165static int mc_fixup_mac_addr(void *blob, int nodeoffset,
166 const char *propname, struct eth_device *eth_dev,
167 enum mc_fixup_type type)
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000168{
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000169 int err = 0, len = 0, size, i;
170 unsigned char env_enetaddr[ARP_HLEN];
171 unsigned int enetaddr_32[ARP_HLEN];
172 void *val = NULL;
173
174 switch (type) {
175 case MC_FIXUP_DPL:
176 /* DPL likes its addresses on 32 * ARP_HLEN bits */
177 for (i = 0; i < ARP_HLEN; i++)
178 enetaddr_32[i] = cpu_to_fdt32(eth_dev->enetaddr[i]);
179 val = enetaddr_32;
180 len = sizeof(enetaddr_32);
181 break;
182
183 case MC_FIXUP_DPC:
184 val = eth_dev->enetaddr;
185 len = ARP_HLEN;
186 break;
187 }
188
189 /* MAC address property present */
190 if (fdt_get_property(blob, nodeoffset, propname, NULL)) {
191 /* u-boot MAC addr randomly assigned - leave the present one */
Simon Glass35affd72017-08-03 12:22:14 -0600192 if (!eth_env_get_enetaddr_by_index("eth", eth_dev->index,
193 env_enetaddr))
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000194 return err;
195 } else {
196 size = MC_DT_INCREASE_SIZE + strlen(propname) + len;
197 /* make room for mac address property */
198 err = fdt_increase_size(blob, size);
199 if (err) {
200 printf("fdt_increase_size: err=%s\n",
201 fdt_strerror(err));
202 return err;
203 }
204 }
205
206 err = fdt_setprop(blob, nodeoffset, propname, val, len);
207 if (err) {
208 printf("fdt_setprop: err=%s\n", fdt_strerror(err));
209 return err;
210 }
211
212 return err;
213}
214
215#define is_dpni(s) (s != NULL ? !strncmp(s, "dpni@", 5) : 0)
216
217const char *dpl_get_connection_endpoint(void *blob, char *endpoint)
218{
219 int connoffset = fdt_path_offset(blob, "/connections"), off;
220 const char *s1, *s2;
221
222 for (off = fdt_first_subnode(blob, connoffset);
223 off >= 0;
224 off = fdt_next_subnode(blob, off)) {
225 s1 = fdt_stringlist_get(blob, off, "endpoint1", 0, NULL);
226 s2 = fdt_stringlist_get(blob, off, "endpoint2", 0, NULL);
227
228 if (!s1 || !s2)
229 continue;
230
231 if (strcmp(endpoint, s1) == 0)
232 return s2;
233
234 if (strcmp(endpoint, s2) == 0)
235 return s1;
236 }
237
238 return NULL;
239}
240
241static int mc_fixup_dpl_mac_addr(void *blob, int dpmac_id,
242 struct eth_device *eth_dev)
243{
244 int objoff = fdt_path_offset(blob, "/objects");
245 int dpmacoff = -1, dpnioff = -1;
246 const char *endpoint;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000247 char mac_name[10];
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000248 int err;
249
250 sprintf(mac_name, "dpmac@%d", dpmac_id);
251 dpmacoff = fdt_subnode_offset(blob, objoff, mac_name);
252 if (dpmacoff < 0)
253 /* dpmac not defined in DPL, so skip it. */
254 return 0;
255
256 err = mc_fixup_mac_addr(blob, dpmacoff, "mac_addr", eth_dev,
257 MC_FIXUP_DPL);
258 if (err) {
259 printf("Error fixing up dpmac mac_addr in DPL\n");
260 return err;
261 }
262
263 /* now we need to figure out if there is any
264 * DPNI connected to this MAC, so we walk the
265 * connection list
266 */
267 endpoint = dpl_get_connection_endpoint(blob, mac_name);
268 if (!is_dpni(endpoint))
269 return 0;
270
271 /* let's see if we can fixup the DPNI as well */
272 dpnioff = fdt_subnode_offset(blob, objoff, endpoint);
273 if (dpnioff < 0)
274 /* DPNI not defined in DPL in the objects area */
275 return 0;
276
277 return mc_fixup_mac_addr(blob, dpnioff, "mac_addr", eth_dev,
278 MC_FIXUP_DPL);
279}
280
Nipun Guptaa78df402018-08-20 16:01:14 +0530281void fdt_fsl_mc_fixup_iommu_map_entry(void *blob)
282{
283 u32 *prop;
284 u32 iommu_map[4];
285 int offset;
286 int lenp;
287
288 /* find fsl-mc node */
289 offset = fdt_path_offset(blob, "/soc/fsl-mc");
290 if (offset < 0)
291 offset = fdt_path_offset(blob, "/fsl-mc");
292 if (offset < 0) {
293 printf("%s: fsl-mc: ERR: fsl-mc node not found in DT, err %d\n",
294 __func__, offset);
295 return;
296 }
297
298 prop = fdt_getprop_w(blob, offset, "iommu-map", &lenp);
299 if (!prop) {
300 debug("%s: fsl-mc: ERR: missing iommu-map in fsl-mc bus node\n",
301 __func__);
302 return;
303 }
304
305 iommu_map[0] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_START);
306 iommu_map[1] = *++prop;
307 iommu_map[2] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_START);
308 iommu_map[3] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_END -
309 FSL_DPAA2_STREAM_ID_START + 1);
310
311 fdt_setprop_inplace(blob, offset, "iommu-map",
312 iommu_map, sizeof(iommu_map));
313}
314
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000315static int mc_fixup_dpc_mac_addr(void *blob, int dpmac_id,
316 struct eth_device *eth_dev)
317{
318 int nodeoffset = fdt_path_offset(blob, "/board_info/ports"), noff;
319 int err = 0;
320 char mac_name[10];
321 const char link_type_mode[] = "MAC_LINK_TYPE_FIXED";
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000322
323 sprintf(mac_name, "mac@%d", dpmac_id);
324
325 /* node not found - create it */
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000326 noff = fdt_subnode_offset(blob, nodeoffset, (const char *)mac_name);
327 if (noff < 0) {
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000328 err = fdt_increase_size(blob, 200);
329 if (err) {
330 printf("fdt_increase_size: err=%s\n",
331 fdt_strerror(err));
332 return err;
333 }
334
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000335 noff = fdt_add_subnode(blob, nodeoffset, mac_name);
336 if (noff < 0) {
337 printf("fdt_add_subnode: err=%s\n",
338 fdt_strerror(err));
339 return err;
340 }
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000341
342 /* add default property of fixed link */
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000343 err = fdt_appendprop_string(blob, noff,
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000344 "link_type", link_type_mode);
345 if (err) {
346 printf("fdt_appendprop_string: err=%s\n",
347 fdt_strerror(err));
348 return err;
349 }
350 }
351
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000352 return mc_fixup_mac_addr(blob, noff, "port_mac_address", eth_dev,
353 MC_FIXUP_DPC);
354}
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000355
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000356static int mc_fixup_mac_addrs(void *blob, enum mc_fixup_type type)
357{
358 int i, err = 0, ret = 0;
Pankaj Bansalc022ec02018-08-02 16:31:28 +0530359 char ethname[ETH_NAME_LEN];
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000360 struct eth_device *eth_dev;
361
362 for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++) {
363 /* port not enabled */
364 if ((wriop_is_enabled_dpmac(i) != 1) ||
365 (wriop_get_phy_address(i) == -1))
366 continue;
367
Pankaj Bansalc022ec02018-08-02 16:31:28 +0530368 snprintf(ethname, ETH_NAME_LEN, "DPMAC%d@%s", i,
369 phy_interface_strings[wriop_get_enet_if(i)]);
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000370
371 eth_dev = eth_get_dev_by_name(ethname);
372 if (eth_dev == NULL)
373 continue;
374
375 switch (type) {
376 case MC_FIXUP_DPL:
377 err = mc_fixup_dpl_mac_addr(blob, i, eth_dev);
378 break;
379 case MC_FIXUP_DPC:
380 err = mc_fixup_dpc_mac_addr(blob, i, eth_dev);
381 break;
382 default:
383 break;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000384 }
385
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000386 if (err)
387 printf("fsl-mc: ERROR fixing mac address for %s\n",
388 ethname);
389 ret |= err;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000390 }
391
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000392 return ret;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000393}
394
Stuart Yoder21c69872015-07-02 11:29:03 +0530395static int mc_fixup_dpc(u64 dpc_addr)
396{
397 void *blob = (void *)dpc_addr;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000398 int nodeoffset, err = 0;
Stuart Yoder21c69872015-07-02 11:29:03 +0530399
400 /* delete any existing ICID pools */
401 nodeoffset = fdt_path_offset(blob, "/resources/icid_pools");
402 if (fdt_del_node(blob, nodeoffset) < 0)
403 printf("\nfsl-mc: WARNING: could not delete ICID pool\n");
404
405 /* add a new pool */
406 nodeoffset = fdt_path_offset(blob, "/resources");
407 if (nodeoffset < 0) {
408 printf("\nfsl-mc: ERROR: DPC is missing /resources\n");
409 return -EINVAL;
410 }
411 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pools");
412 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pool@0");
413 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
414 "base_icid", FSL_DPAA2_STREAM_ID_START, 1);
415 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
416 "num",
417 FSL_DPAA2_STREAM_ID_END -
418 FSL_DPAA2_STREAM_ID_START + 1, 1);
419
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000420 /* fixup MAC addresses for dpmac ports */
421 nodeoffset = fdt_path_offset(blob, "/board_info/ports");
422 if (nodeoffset < 0)
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000423 return 0;
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000424
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000425 err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPC);
Stuart Yoder21c69872015-07-02 11:29:03 +0530426 flush_dcache_range(dpc_addr, dpc_addr + fdt_totalsize(blob));
427
Bogdan Purcareata5707dfb2017-01-11 15:58:36 +0000428 return err;
Stuart Yoder21c69872015-07-02 11:29:03 +0530429}
430
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530431static 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 -0700432{
433 u64 mc_dpc_offset;
434#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
435 int error;
436 void *dpc_fdt_hdr;
437 int dpc_size;
438#endif
439
440#ifdef CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET
441 BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET & 0x3) != 0 ||
442 CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET > 0xffffffff);
443
444 mc_dpc_offset = CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET;
445#else
446#error "CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET not defined"
447#endif
448
449 /*
450 * Load the MC DPC blob in the MC private DRAM block:
451 */
452#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
453 printf("MC DPC is preloaded to %#llx\n", mc_ram_addr + mc_dpc_offset);
454#else
455 /*
456 * Get address and size of the DPC blob stored in flash:
457 */
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530458 dpc_fdt_hdr = (void *)mc_dpc_addr;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700459
460 error = fdt_check_header(dpc_fdt_hdr);
461 if (error != 0) {
462 /*
463 * Don't return with error here, since the MC firmware can
464 * still boot without a DPC
465 */
J. German Riveracc088c32015-07-02 11:28:56 +0530466 printf("\nfsl-mc: WARNING: No DPC image found");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700467 return 0;
468 }
469
470 dpc_size = fdt_totalsize(dpc_fdt_hdr);
471 if (dpc_size > CONFIG_SYS_LS_MC_DPC_MAX_LENGTH) {
J. German Riveracc088c32015-07-02 11:28:56 +0530472 printf("\nfsl-mc: ERROR: Bad DPC image (too large: %d)\n",
J. German Rivera125e2bc2015-03-20 19:28:18 -0700473 dpc_size);
474 return -EINVAL;
475 }
476
477 mc_copy_image("MC DPC blob",
478 (u64)dpc_fdt_hdr, dpc_size, mc_ram_addr + mc_dpc_offset);
479#endif /* not defined CONFIG_SYS_LS_MC_DPC_IN_DDR */
480
Stuart Yoder21c69872015-07-02 11:29:03 +0530481 if (mc_fixup_dpc(mc_ram_addr + mc_dpc_offset))
482 return -EINVAL;
483
J. German Rivera125e2bc2015-03-20 19:28:18 -0700484 dump_ram_words("DPC", (void *)(mc_ram_addr + mc_dpc_offset));
485 return 0;
486}
487
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000488
489static int mc_fixup_dpl(u64 dpl_addr)
490{
491 void *blob = (void *)dpl_addr;
492 u32 ver = fdt_getprop_u32_default(blob, "/", "dpl-version", 0);
493 int err = 0;
494
495 /* The DPL fixup for mac addresses is only relevant
496 * for old-style DPLs
497 */
498 if (ver >= 10)
499 return 0;
500
501 err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPL);
502 flush_dcache_range(dpl_addr, dpl_addr + fdt_totalsize(blob));
503
504 return err;
505}
506
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530507static 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 -0700508{
509 u64 mc_dpl_offset;
510#ifndef CONFIG_SYS_LS_MC_DPL_IN_DDR
511 int error;
512 void *dpl_fdt_hdr;
513 int dpl_size;
514#endif
515
516#ifdef CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET
517 BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET & 0x3) != 0 ||
518 CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET > 0xffffffff);
519
520 mc_dpl_offset = CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET;
521#else
522#error "CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET not defined"
523#endif
524
525 /*
526 * Load the MC DPL blob in the MC private DRAM block:
527 */
528#ifdef CONFIG_SYS_LS_MC_DPL_IN_DDR
529 printf("MC DPL is preloaded to %#llx\n", mc_ram_addr + mc_dpl_offset);
530#else
531 /*
532 * Get address and size of the DPL blob stored in flash:
533 */
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530534 dpl_fdt_hdr = (void *)mc_dpl_addr;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700535
536 error = fdt_check_header(dpl_fdt_hdr);
537 if (error != 0) {
J. German Riveracc088c32015-07-02 11:28:56 +0530538 printf("\nfsl-mc: ERROR: Bad DPL image (bad header)\n");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700539 return error;
540 }
541
542 dpl_size = fdt_totalsize(dpl_fdt_hdr);
543 if (dpl_size > CONFIG_SYS_LS_MC_DPL_MAX_LENGTH) {
J. German Riveracc088c32015-07-02 11:28:56 +0530544 printf("\nfsl-mc: ERROR: Bad DPL image (too large: %d)\n",
J. German Rivera125e2bc2015-03-20 19:28:18 -0700545 dpl_size);
546 return -EINVAL;
547 }
548
549 mc_copy_image("MC DPL blob",
550 (u64)dpl_fdt_hdr, dpl_size, mc_ram_addr + mc_dpl_offset);
551#endif /* not defined CONFIG_SYS_LS_MC_DPL_IN_DDR */
552
Bogdan Purcareata1161dbc2017-05-24 16:40:22 +0000553 if (mc_fixup_dpl(mc_ram_addr + mc_dpl_offset))
554 return -EINVAL;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700555 dump_ram_words("DPL", (void *)(mc_ram_addr + mc_dpl_offset));
556 return 0;
557}
558
559/**
560 * Return the MC boot timeout value in milliseconds
561 */
562static unsigned long get_mc_boot_timeout_ms(void)
563{
564 unsigned long timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
565
Simon Glass00caae62017-08-03 12:22:12 -0600566 char *timeout_ms_env_var = env_get(MC_BOOT_TIMEOUT_ENV_VAR);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700567
568 if (timeout_ms_env_var) {
569 timeout_ms = simple_strtoul(timeout_ms_env_var, NULL, 10);
570 if (timeout_ms == 0) {
571 printf("fsl-mc: WARNING: Invalid value for \'"
572 MC_BOOT_TIMEOUT_ENV_VAR
573 "\' environment variable: %lu\n",
574 timeout_ms);
575
576 timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
577 }
578 }
579
580 return timeout_ms;
581}
582
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530583#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
York Sun3c1d2182016-04-04 11:41:26 -0700584
585__weak bool soc_has_aiop(void)
586{
587 return false;
588}
589
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530590static int load_mc_aiop_img(u64 aiop_fw_addr)
J. German Riverac1000c12015-07-02 11:28:58 +0530591{
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530592 u64 mc_ram_addr = mc_get_dram_addr();
593#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
J. German Riverac1000c12015-07-02 11:28:58 +0530594 void *aiop_img;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530595#endif
J. German Riverac1000c12015-07-02 11:28:58 +0530596
York Sun3c1d2182016-04-04 11:41:26 -0700597 /* Check if AIOP is available */
598 if (!soc_has_aiop())
599 return -ENODEV;
J. German Riverac1000c12015-07-02 11:28:58 +0530600 /*
601 * Load the MC AIOP image in the MC private DRAM block:
602 */
603
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530604#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
605 printf("MC AIOP is preloaded to %#llx\n", mc_ram_addr +
606 CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
607#else
608 aiop_img = (void *)aiop_fw_addr;
J. German Riverac1000c12015-07-02 11:28:58 +0530609 mc_copy_image("MC AIOP image",
610 (u64)aiop_img, CONFIG_SYS_LS_MC_AIOP_IMG_MAX_LENGTH,
611 mc_ram_addr + CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530612#endif
613 mc_aiop_applied = 0;
J. German Riverac1000c12015-07-02 11:28:58 +0530614
615 return 0;
616}
617#endif
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530618
J. German Rivera125e2bc2015-03-20 19:28:18 -0700619static int wait_for_mc(bool booting_mc, u32 *final_reg_gsr)
620{
621 u32 reg_gsr;
622 u32 mc_fw_boot_status;
623 unsigned long timeout_ms = get_mc_boot_timeout_ms();
624 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
625
626 dmb();
J. German Rivera125e2bc2015-03-20 19:28:18 -0700627 assert(timeout_ms > 0);
628 for (;;) {
629 udelay(1000); /* throttle polling */
630 reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
631 mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
632 if (mc_fw_boot_status & 0x1)
633 break;
634
635 timeout_ms--;
636 if (timeout_ms == 0)
637 break;
638 }
639
640 if (timeout_ms == 0) {
J. German Riveracc088c32015-07-02 11:28:56 +0530641 printf("ERROR: timeout\n");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700642
643 /* TODO: Get an error status from an MC CCSR register */
644 return -ETIMEDOUT;
645 }
646
647 if (mc_fw_boot_status != 0x1) {
648 /*
649 * TODO: Identify critical errors from the GSR register's FS
650 * field and for those errors, set error to -ENODEV or other
651 * appropriate errno, so that the status property is set to
652 * failure in the fsl,dprc device tree node.
653 */
J. German Riveracc088c32015-07-02 11:28:56 +0530654 printf("WARNING: Firmware returned an error (GSR: %#x)\n",
655 reg_gsr);
656 } else {
657 printf("SUCCESS\n");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700658 }
659
J. German Riveracc088c32015-07-02 11:28:56 +0530660
J. German Rivera125e2bc2015-03-20 19:28:18 -0700661 *final_reg_gsr = reg_gsr;
662 return 0;
663}
J. German Riverab940ca62014-06-23 15:15:55 -0700664
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530665int mc_init(u64 mc_fw_addr, u64 mc_dpc_addr)
J. German Riverab940ca62014-06-23 15:15:55 -0700666{
667 int error = 0;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700668 int portal_id = 0;
J. German Riverab940ca62014-06-23 15:15:55 -0700669 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530670 u64 mc_ram_addr = mc_get_dram_addr();
J. German Riverab940ca62014-06-23 15:15:55 -0700671 u32 reg_gsr;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700672 u32 reg_mcfbalr;
673#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Riverab940ca62014-06-23 15:15:55 -0700674 const void *raw_image_addr;
675 size_t raw_image_size = 0;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700676#endif
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800677 struct mc_version mc_ver_info;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700678 u8 mc_ram_num_256mb_blocks;
679 size_t mc_ram_size = mc_get_dram_block_size();
J. German Riverab940ca62014-06-23 15:15:55 -0700680
York Sun437858b62017-03-06 09:02:29 -0800681 mc_ram_num_256mb_blocks = mc_ram_size / MC_RAM_SIZE_ALIGNMENT;
682 if (mc_ram_num_256mb_blocks < 1 || mc_ram_num_256mb_blocks > 0xff) {
683 error = -EINVAL;
684 printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
685 mc_ram_size);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700686 goto out;
York Sun437858b62017-03-06 09:02:29 -0800687 }
J. German Rivera125e2bc2015-03-20 19:28:18 -0700688
J. German Riverab940ca62014-06-23 15:15:55 -0700689 /*
690 * Management Complex cores should be held at reset out of POR.
Bin Menga1875592016-02-05 19:30:11 -0800691 * U-Boot should be the first software to touch MC. To be safe,
J. German Riverab940ca62014-06-23 15:15:55 -0700692 * we reset all cores again by setting GCR1 to 0. It doesn't do
693 * anything if they are held at reset. After we setup the firmware
694 * we kick off MC by deasserting the reset bit for core 0, and
695 * deasserting the reset bits for Command Portal Managers.
696 * The stop bits are not touched here. They are used to stop the
697 * cores when they are active. Setting stop bits doesn't stop the
698 * cores from fetching instructions when they are released from
699 * reset.
700 */
701 out_le32(&mc_ccsr_regs->reg_gcr1, 0);
702 dmb();
703
J. German Rivera125e2bc2015-03-20 19:28:18 -0700704#ifdef CONFIG_SYS_LS_MC_FW_IN_DDR
705 printf("MC firmware is preloaded to %#llx\n", mc_ram_addr);
706#else
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530707 error = parse_mc_firmware_fit_image(mc_fw_addr, &raw_image_addr,
708 &raw_image_size);
J. German Riverab940ca62014-06-23 15:15:55 -0700709 if (error != 0)
710 goto out;
711 /*
712 * Load the MC FW at the beginning of the MC private DRAM block:
713 */
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800714 mc_copy_image("MC Firmware",
715 (u64)raw_image_addr, raw_image_size, mc_ram_addr);
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800716#endif
J. German Rivera125e2bc2015-03-20 19:28:18 -0700717 dump_ram_words("firmware", (void *)mc_ram_addr);
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800718
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530719 error = load_mc_dpc(mc_ram_addr, mc_ram_size, mc_dpc_addr);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700720 if (error != 0)
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800721 goto out;
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800722
J. German Riverab940ca62014-06-23 15:15:55 -0700723 debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700724 dump_mc_ccsr_regs(mc_ccsr_regs);
J. German Riverab940ca62014-06-23 15:15:55 -0700725
726 /*
J. German Rivera125e2bc2015-03-20 19:28:18 -0700727 * Tell MC what is the address range of the DRAM block assigned to it:
J. German Riverab940ca62014-06-23 15:15:55 -0700728 */
York Sun437858b62017-03-06 09:02:29 -0800729 reg_mcfbalr = (u32)mc_ram_addr |
J. German Rivera125e2bc2015-03-20 19:28:18 -0700730 (mc_ram_num_256mb_blocks - 1);
731 out_le32(&mc_ccsr_regs->reg_mcfbalr, reg_mcfbalr);
732 out_le32(&mc_ccsr_regs->reg_mcfbahr,
York Sun437858b62017-03-06 09:02:29 -0800733 (u32)(mc_ram_addr >> 32));
Stuart Yoder39da6442015-07-02 11:29:02 +0530734 out_le32(&mc_ccsr_regs->reg_mcfapr, FSL_BYPASS_AMQ);
J. German Riverab940ca62014-06-23 15:15:55 -0700735
736 /*
J. German Rivera125e2bc2015-03-20 19:28:18 -0700737 * Tell the MC that we want delayed DPL deployment.
J. German Riverab940ca62014-06-23 15:15:55 -0700738 */
J. German Rivera125e2bc2015-03-20 19:28:18 -0700739 out_le32(&mc_ccsr_regs->reg_gsr, 0xDD00);
J. German Riverab940ca62014-06-23 15:15:55 -0700740
J. German Riveracc088c32015-07-02 11:28:56 +0530741 printf("\nfsl-mc: Booting Management Complex ... ");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800742
J. German Riverab940ca62014-06-23 15:15:55 -0700743 /*
744 * Deassert reset and release MC core 0 to run
745 */
746 out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700747 error = wait_for_mc(true, &reg_gsr);
748 if (error != 0)
J. German Riverab940ca62014-06-23 15:15:55 -0700749 goto out;
J. German Riverab940ca62014-06-23 15:15:55 -0700750
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800751 /*
752 * TODO: need to obtain the portal_id for the root container from the
753 * DPL
754 */
755 portal_id = 0;
756
757 /*
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700758 * Initialize the global default MC portal
759 * And check that the MC firmware is responding portal commands:
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800760 */
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530761 root_mc_io = (struct fsl_mc_io *)calloc(sizeof(struct fsl_mc_io), 1);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530762 if (!root_mc_io) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530763 printf(" No memory: calloc() failed\n");
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700764 return -ENOMEM;
765 }
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800766
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530767 root_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(portal_id);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700768 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 +0530769 portal_id, root_mc_io->mmio_regs);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700770
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530771 error = mc_get_version(root_mc_io, MC_CMD_NO_FLAGS, &mc_ver_info);
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800772 if (error != 0) {
773 printf("fsl-mc: ERROR: Firmware version check failed (error: %d)\n",
774 error);
775 goto out;
776 }
777
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800778 printf("fsl-mc: Management Complex booted (version: %d.%d.%d, boot status: %#x)\n",
779 mc_ver_info.major, mc_ver_info.minor, mc_ver_info.revision,
J. German Rivera125e2bc2015-03-20 19:28:18 -0700780 reg_gsr & GSR_FS_MASK);
781
J. German Riverab940ca62014-06-23 15:15:55 -0700782out:
783 if (error != 0)
Prabhakar Kushwaha2b7c4a12015-07-02 11:29:01 +0530784 mc_boot_status = error;
J. German Riverab940ca62014-06-23 15:15:55 -0700785 else
786 mc_boot_status = 0;
787
788 return error;
789}
790
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530791int mc_apply_dpl(u64 mc_dpl_addr)
792{
793 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
794 int error = 0;
795 u32 reg_gsr;
796 u64 mc_ram_addr = mc_get_dram_addr();
797 size_t mc_ram_size = mc_get_dram_block_size();
798
Alexander Grafb7b84102016-11-17 01:02:57 +0100799 if (!mc_dpl_addr)
800 return -1;
801
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530802 error = load_mc_dpl(mc_ram_addr, mc_ram_size, mc_dpl_addr);
803 if (error != 0)
804 return error;
805
806 /*
807 * Tell the MC to deploy the DPL:
808 */
809 out_le32(&mc_ccsr_regs->reg_gsr, 0x0);
810 printf("fsl-mc: Deploying data path layout ... ");
811 error = wait_for_mc(false, &reg_gsr);
812
813 if (!error)
814 mc_dpl_applied = 0;
815
816 return error;
817}
818
J. German Riverab940ca62014-06-23 15:15:55 -0700819int get_mc_boot_status(void)
820{
821 return mc_boot_status;
822}
823
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530824#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
825int get_aiop_apply_status(void)
826{
827 return mc_aiop_applied;
828}
829#endif
830
831int get_dpl_apply_status(void)
832{
833 return mc_dpl_applied;
834}
835
Priyanka Jain033c5382017-08-24 16:42:43 +0530836/*
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530837 * Return the MC address of private DRAM block.
Priyanka Jain033c5382017-08-24 16:42:43 +0530838 * As per MC design document, MC initial base address
839 * should be least significant 512MB address of MC private
840 * memory, i.e. address should point to end address masked
841 * with 512MB offset in private DRAM block.
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530842 */
843u64 mc_get_dram_addr(void)
844{
Priyanka Jain033c5382017-08-24 16:42:43 +0530845 size_t mc_ram_size = mc_get_dram_block_size();
846
847 return (gd->arch.resv_ram + mc_ram_size - 1) &
848 MC_RAM_BASE_ADDR_ALIGNMENT_MASK;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530849}
850
J. German Riverab940ca62014-06-23 15:15:55 -0700851/**
852 * Return the actual size of the MC private DRAM block.
J. German Riverab940ca62014-06-23 15:15:55 -0700853 */
854unsigned long mc_get_dram_block_size(void)
855{
J. German Rivera125e2bc2015-03-20 19:28:18 -0700856 unsigned long dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
857
Simon Glass00caae62017-08-03 12:22:12 -0600858 char *dram_block_size_env_var = env_get(MC_MEM_SIZE_ENV_VAR);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700859
860 if (dram_block_size_env_var) {
861 dram_block_size = simple_strtoul(dram_block_size_env_var, NULL,
Prabhakar Kushwahaf53e12d2017-11-09 14:45:41 +0530862 16);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700863
864 if (dram_block_size < CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE) {
865 printf("fsl-mc: WARNING: Invalid value for \'"
866 MC_MEM_SIZE_ENV_VAR
867 "\' environment variable: %lu\n",
868 dram_block_size);
869
870 dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
871 }
872 }
873
874 return dram_block_size;
J. German Riverab940ca62014-06-23 15:15:55 -0700875}
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700876
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530877int fsl_mc_ldpaa_init(bd_t *bis)
878{
Prabhakar Kushwahac919ab92015-11-04 12:26:00 +0530879 int i;
880
881 for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++)
882 if ((wriop_is_enabled_dpmac(i) == 1) &&
883 (wriop_get_phy_address(i) != -1))
884 ldpaa_eth_init(i, wriop_get_enet_if(i));
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530885 return 0;
886}
887
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530888static int dprc_version_check(struct fsl_mc_io *mc_io, uint16_t handle)
889{
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530890 int error;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530891 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530892
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530893 error = dprc_get_api_version(mc_io, 0,
894 &major_ver,
895 &minor_ver);
896 if (error < 0) {
897 printf("dprc_get_api_version() failed: %d\n", error);
898 return error;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530899 }
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530900
901 if (major_ver < DPRC_VER_MAJOR || (major_ver == DPRC_VER_MAJOR &&
902 minor_ver < DPRC_VER_MINOR)) {
903 printf("DPRC version mismatch found %u.%u,",
904 major_ver, minor_ver);
905 printf("supported version is %u.%u\n",
906 DPRC_VER_MAJOR, DPRC_VER_MINOR);
907 }
908
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530909 return error;
910}
911
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530912static int dpio_init(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700913{
914 struct qbman_swp_desc p_des;
915 struct dpio_attr attr;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530916 struct dpio_cfg dpio_cfg;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700917 int err = 0;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530918 uint16_t major_ver, minor_ver;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700919
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530920 dflt_dpio = (struct fsl_dpio_obj *)calloc(
921 sizeof(struct fsl_dpio_obj), 1);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700922 if (!dflt_dpio) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530923 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530924 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +0530925 goto err_calloc;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700926 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530927 dpio_cfg.channel_mode = DPIO_LOCAL_CHANNEL;
928 dpio_cfg.num_priorities = 8;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700929
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530930 err = dpio_create(dflt_mc_io,
931 dflt_dprc_handle,
932 MC_CMD_NO_FLAGS,
933 &dpio_cfg,
934 &dflt_dpio->dpio_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530935 if (err < 0) {
936 printf("dpio_create() failed: %d\n", err);
937 err = -ENODEV;
938 goto err_create;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700939 }
940
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530941 err = dpio_get_api_version(dflt_mc_io, 0,
942 &major_ver,
943 &minor_ver);
944 if (err < 0) {
945 printf("dpio_get_api_version() failed: %d\n", err);
946 goto err_get_api_ver;
947 }
948
949 if (major_ver < DPIO_VER_MAJOR || (major_ver == DPIO_VER_MAJOR &&
950 minor_ver < DPIO_VER_MINOR)) {
951 printf("DPRC version mismatch found %u.%u,",
952 major_ver,
953 minor_ver);
954 }
955
956 err = dpio_open(dflt_mc_io,
957 MC_CMD_NO_FLAGS,
958 dflt_dpio->dpio_id,
959 &dflt_dpio->dpio_handle);
960 if (err) {
961 printf("dpio_open() failed\n");
962 goto err_open;
963 }
964
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530965 memset(&attr, 0, sizeof(struct dpio_attr));
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +0530966 err = dpio_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530967 dflt_dpio->dpio_handle, &attr);
968 if (err < 0) {
969 printf("dpio_get_attributes() failed: %d\n", err);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700970 goto err_get_attr;
971 }
972
Yogesh Gaur2557c5a2017-11-15 11:59:31 +0530973 if (dflt_dpio->dpio_id != attr.id) {
974 printf("dnpi object id and attribute id are not same\n");
975 goto err_attr_not_same;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +0530976 }
977
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530978#ifdef DEBUG
979 printf("Init: DPIO id=0x%d\n", dflt_dpio->dpio_id);
980#endif
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +0530981 err = dpio_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
982 if (err < 0) {
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700983 printf("dpio_enable() failed %d\n", err);
984 goto err_get_enable;
985 }
Prabhakar Kushwaha1f1c25c2015-07-02 11:28:59 +0530986 debug("ce_offset=0x%llx, ci_offset=0x%llx, portalid=%d, prios=%d\n",
987 attr.qbman_portal_ce_offset,
988 attr.qbman_portal_ci_offset,
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700989 attr.qbman_portal_id,
990 attr.num_priorities);
991
Prabhakar Kushwaha1f1c25c2015-07-02 11:28:59 +0530992 p_des.cena_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
993 + attr.qbman_portal_ce_offset);
994 p_des.cinh_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
995 + attr.qbman_portal_ci_offset);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700996
997 dflt_dpio->sw_portal = qbman_swp_init(&p_des);
998 if (dflt_dpio->sw_portal == NULL) {
999 printf("qbman_swp_init() failed\n");
1000 goto err_get_swp_init;
1001 }
1002 return 0;
1003
1004err_get_swp_init:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301005 dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001006err_get_enable:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301007err_get_attr:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301008err_attr_not_same:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301009 dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301010err_open:
1011err_get_api_ver:
1012 dpio_destroy(dflt_mc_io,
1013 dflt_dprc_handle,
1014 MC_CMD_NO_FLAGS,
1015 dflt_dpio->dpio_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301016err_create:
Prabhakar Kushwahacd7b3fb2016-03-18 16:15:29 +05301017 free(dflt_dpio);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301018err_calloc:
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001019 return err;
1020}
1021
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301022static int dpio_exit(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001023{
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301024 int err;
1025
1026 err = dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1027 if (err < 0) {
1028 printf("dpio_disable() failed: %d\n", err);
1029 goto err;
1030 }
1031
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301032 dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1033 if (err < 0) {
1034 printf("dpio_close() failed: %d\n", err);
1035 goto err;
1036 }
1037
1038 err = 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 +05301042 if (err < 0) {
1043 printf("dpio_destroy() failed: %d\n", err);
1044 goto err;
1045 }
1046
1047#ifdef DEBUG
1048 printf("Exit: DPIO id=0x%d\n", dflt_dpio->dpio_id);
1049#endif
1050
1051 if (dflt_dpio)
1052 free(dflt_dpio);
1053
1054 return 0;
1055err:
1056 return err;
1057}
1058
1059static int dprc_init(void)
1060{
1061 int err, child_portal_id, container_id;
1062 struct dprc_cfg cfg;
1063 uint64_t mc_portal_offset;
1064
1065 /* Open root container */
1066 err = dprc_get_container_id(root_mc_io, MC_CMD_NO_FLAGS, &container_id);
1067 if (err < 0) {
1068 printf("dprc_get_container_id(): Root failed: %d\n", err);
1069 goto err_root_container_id;
1070 }
1071
1072#ifdef DEBUG
1073 printf("Root container id = %d\n", container_id);
1074#endif
1075 err = dprc_open(root_mc_io, MC_CMD_NO_FLAGS, container_id,
1076 &root_dprc_handle);
1077 if (err < 0) {
1078 printf("dprc_open(): Root Container failed: %d\n", err);
1079 goto err_root_open;
1080 }
1081
1082 if (!root_dprc_handle) {
1083 printf("dprc_open(): Root Container Handle is not valid\n");
1084 goto err_root_open;
1085 }
1086
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301087 err = dprc_version_check(root_mc_io, root_dprc_handle);
1088 if (err < 0) {
1089 printf("dprc_version_check() failed: %d\n", err);
1090 goto err_root_open;
1091 }
1092
Prabhakar Kushwaha5373b202016-01-20 12:04:19 +05301093 memset(&cfg, 0, sizeof(struct dprc_cfg));
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301094 cfg.options = DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED |
1095 DPRC_CFG_OPT_OBJ_CREATE_ALLOWED |
1096 DPRC_CFG_OPT_ALLOC_ALLOWED;
1097 cfg.icid = DPRC_GET_ICID_FROM_POOL;
Prabhakar Kushwaha335b1932015-12-24 15:33:49 +05301098 cfg.portal_id = DPRC_GET_PORTAL_ID_FROM_POOL;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301099 err = dprc_create_container(root_mc_io, MC_CMD_NO_FLAGS,
1100 root_dprc_handle,
1101 &cfg,
1102 &child_dprc_id,
1103 &mc_portal_offset);
1104 if (err < 0) {
1105 printf("dprc_create_container() failed: %d\n", err);
1106 goto err_create;
1107 }
1108
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301109 dflt_mc_io = (struct fsl_mc_io *)calloc(sizeof(struct fsl_mc_io), 1);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301110 if (!dflt_mc_io) {
1111 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301112 printf(" No memory: calloc() failed\n");
1113 goto err_calloc;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301114 }
1115
1116 child_portal_id = MC_PORTAL_OFFSET_TO_PORTAL_ID(mc_portal_offset);
1117 dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(child_portal_id);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301118
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301119#ifdef DEBUG
1120 printf("MC portal of child DPRC container: %d, physical addr %p)\n",
1121 child_dprc_id, dflt_mc_io->mmio_regs);
1122#endif
1123
1124 err = dprc_open(dflt_mc_io, MC_CMD_NO_FLAGS, child_dprc_id,
1125 &dflt_dprc_handle);
1126 if (err < 0) {
1127 printf("dprc_open(): Child container failed: %d\n", err);
1128 goto err_child_open;
1129 }
1130
1131 if (!dflt_dprc_handle) {
1132 printf("dprc_open(): Child container Handle is not valid\n");
1133 goto err_child_open;
1134 }
1135
1136 return 0;
1137err_child_open:
1138 free(dflt_mc_io);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301139err_calloc:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301140 dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1141 root_dprc_handle, child_dprc_id);
1142err_create:
1143 dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1144err_root_open:
1145err_root_container_id:
1146 return err;
1147}
1148
1149static int dprc_exit(void)
1150{
1151 int err;
1152
1153 err = dprc_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dprc_handle);
1154 if (err < 0) {
1155 printf("dprc_close(): Child failed: %d\n", err);
1156 goto err;
1157 }
1158
1159 err = dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1160 root_dprc_handle, child_dprc_id);
1161 if (err < 0) {
1162 printf("dprc_destroy_container() failed: %d\n", err);
1163 goto err;
1164 }
1165
1166 err = dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1167 if (err < 0) {
1168 printf("dprc_close(): Root failed: %d\n", err);
1169 goto err;
1170 }
1171
1172 if (dflt_mc_io)
1173 free(dflt_mc_io);
1174
1175 if (root_mc_io)
1176 free(root_mc_io);
1177
1178 return 0;
1179
1180err:
1181 return err;
1182}
1183
1184static int dpbp_init(void)
1185{
1186 int err;
1187 struct dpbp_attr dpbp_attr;
1188 struct dpbp_cfg dpbp_cfg;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301189 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301190
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301191 dflt_dpbp = (struct fsl_dpbp_obj *)calloc(
1192 sizeof(struct fsl_dpbp_obj), 1);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001193 if (!dflt_dpbp) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301194 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301195 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301196 goto err_calloc;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001197 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301198
1199 dpbp_cfg.options = 512;
1200
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301201 err = dpbp_create(dflt_mc_io,
1202 dflt_dprc_handle,
1203 MC_CMD_NO_FLAGS,
1204 &dpbp_cfg,
1205 &dflt_dpbp->dpbp_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301206
1207 if (err < 0) {
1208 err = -ENODEV;
1209 printf("dpbp_create() failed: %d\n", err);
1210 goto err_create;
1211 }
1212
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301213 err = dpbp_get_api_version(dflt_mc_io, 0,
1214 &major_ver,
1215 &minor_ver);
1216 if (err < 0) {
1217 printf("dpbp_get_api_version() failed: %d\n", err);
1218 goto err_get_api_ver;
1219 }
1220
1221 if (major_ver < DPBP_VER_MAJOR || (major_ver == DPBP_VER_MAJOR &&
1222 minor_ver < DPBP_VER_MINOR)) {
1223 printf("DPBP version mismatch found %u.%u,",
1224 major_ver, minor_ver);
1225 printf("supported version is %u.%u\n",
1226 DPBP_VER_MAJOR, DPBP_VER_MINOR);
1227 }
1228
1229 err = dpbp_open(dflt_mc_io,
1230 MC_CMD_NO_FLAGS,
1231 dflt_dpbp->dpbp_id,
1232 &dflt_dpbp->dpbp_handle);
1233 if (err) {
1234 printf("dpbp_open() failed\n");
1235 goto err_open;
1236 }
1237
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301238 memset(&dpbp_attr, 0, sizeof(struct dpbp_attr));
1239 err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
1240 dflt_dpbp->dpbp_handle,
1241 &dpbp_attr);
1242 if (err < 0) {
1243 printf("dpbp_get_attributes() failed: %d\n", err);
1244 goto err_get_attr;
1245 }
1246
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301247 if (dflt_dpbp->dpbp_id != dpbp_attr.id) {
1248 printf("dpbp object id and attribute id are not same\n");
1249 goto err_attr_not_same;
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301250 }
1251
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301252#ifdef DEBUG
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301253 printf("Init: DPBP id=0x%x\n", dflt_dpbp->dpbp_attr.id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301254#endif
1255
1256 err = dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
1257 if (err < 0) {
1258 printf("dpbp_close() failed: %d\n", err);
1259 goto err_close;
1260 }
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001261
1262 return 0;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301263
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301264err_get_attr:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301265err_attr_not_same:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301266 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301267 dpbp_destroy(dflt_mc_io,
1268 dflt_dprc_handle,
1269 MC_CMD_NO_FLAGS,
1270 dflt_dpbp->dpbp_id);
1271err_get_api_ver:
1272err_close:
1273err_open:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301274err_create:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301275 free(dflt_dpbp);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301276err_calloc:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301277 return err;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001278}
1279
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301280static int dpbp_exit(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001281{
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301282 int err;
1283
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301284 err = dpbp_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1285 dflt_dpbp->dpbp_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301286 if (err < 0) {
1287 printf("dpbp_destroy() failed: %d\n", err);
1288 goto err;
1289 }
1290
1291#ifdef DEBUG
1292 printf("Exit: DPBP id=0x%d\n", dflt_dpbp->dpbp_attr.id);
1293#endif
1294
1295 if (dflt_dpbp)
1296 free(dflt_dpbp);
1297 return 0;
1298
1299err:
1300 return err;
1301}
1302
1303static int dpni_init(void)
1304{
1305 int err;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301306 uint8_t cfg_buf[256] = {0};
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301307 struct dpni_cfg dpni_cfg;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301308 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301309
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301310 dflt_dpni = (struct fsl_dpni_obj *)calloc(
1311 sizeof(struct fsl_dpni_obj), 1);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301312 if (!dflt_dpni) {
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301313 printf("No memory: calloc() failed\n");
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301314 err = -ENOMEM;
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301315 goto err_calloc;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301316 }
1317
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301318 memset(&dpni_cfg, 0, sizeof(dpni_cfg));
1319 err = dpni_prepare_cfg(&dpni_cfg, &cfg_buf[0]);
Prabhakar Kushwaha879a59a2015-12-24 15:33:01 +05301320 if (err < 0) {
1321 err = -ENODEV;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301322 printf("dpni_prepare_cfg() failed: %d\n", err);
1323 goto err_prepare_cfg;
Prabhakar Kushwaha879a59a2015-12-24 15:33:01 +05301324 }
1325
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301326 err = dpni_create(dflt_mc_io,
1327 dflt_dprc_handle,
1328 MC_CMD_NO_FLAGS,
1329 &dpni_cfg,
1330 &dflt_dpni->dpni_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301331 if (err < 0) {
1332 err = -ENODEV;
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301333 printf("dpni create() failed: %d\n", err);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301334 goto err_create;
1335 }
1336
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301337 err = dpni_get_api_version(dflt_mc_io, 0,
1338 &major_ver,
1339 &minor_ver);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301340 if (err < 0) {
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301341 printf("dpni_get_api_version() failed: %d\n", err);
1342 goto err_get_version;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301343 }
1344
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301345 if (major_ver < DPNI_VER_MAJOR || (major_ver == DPNI_VER_MAJOR &&
1346 minor_ver < DPNI_VER_MINOR)) {
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301347 printf("DPNI version mismatch found %u.%u,",
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301348 major_ver, minor_ver);
Prabhakar Kushwaha9a696f52015-12-24 15:32:37 +05301349 printf("supported version is %u.%u\n",
1350 DPNI_VER_MAJOR, DPNI_VER_MINOR);
1351 }
1352
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301353 err = dpni_open(dflt_mc_io,
1354 MC_CMD_NO_FLAGS,
1355 dflt_dpni->dpni_id,
1356 &dflt_dpni->dpni_handle);
1357 if (err) {
1358 printf("dpni_open() failed\n");
1359 goto err_open;
1360 }
1361
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301362#ifdef DEBUG
1363 printf("Init: DPNI id=0x%d\n", dflt_dpni->dpni_id);
1364#endif
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301365 err = dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
1366 if (err < 0) {
1367 printf("dpni_close() failed: %d\n", err);
1368 goto err_close;
1369 }
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001370
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301371 return 0;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301372
1373err_close:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301374 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301375err_open:
1376err_get_version:
1377 dpni_destroy(dflt_mc_io,
1378 dflt_dprc_handle,
1379 MC_CMD_NO_FLAGS,
1380 dflt_dpni->dpni_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301381err_create:
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301382err_prepare_cfg:
Prabhakar Kushwaha879a59a2015-12-24 15:33:01 +05301383 free(dflt_dpni);
Prabhakar Kushwahaa572fb62017-10-11 08:51:18 +05301384err_calloc:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301385 return err;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001386}
1387
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301388static int dpni_exit(void)
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001389{
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301390 int err;
1391
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301392 err = dpni_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1393 dflt_dpni->dpni_id);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301394 if (err < 0) {
1395 printf("dpni_destroy() failed: %d\n", err);
1396 goto err;
1397 }
1398
1399#ifdef DEBUG
1400 printf("Exit: DPNI id=0x%d\n", dflt_dpni->dpni_id);
1401#endif
1402
1403 if (dflt_dpni)
1404 free(dflt_dpni);
1405 return 0;
1406
1407err:
1408 return err;
1409}
1410
1411static int mc_init_object(void)
1412{
1413 int err = 0;
1414
1415 err = dprc_init();
1416 if (err < 0) {
1417 printf("dprc_init() failed: %d\n", err);
1418 goto err;
1419 }
1420
1421 err = dpbp_init();
1422 if (err < 0) {
1423 printf("dpbp_init() failed: %d\n", err);
1424 goto err;
1425 }
1426
1427 err = dpio_init();
1428 if (err < 0) {
1429 printf("dpio_init() failed: %d\n", err);
1430 goto err;
1431 }
1432
1433 err = dpni_init();
1434 if (err < 0) {
1435 printf("dpni_init() failed: %d\n", err);
1436 goto err;
1437 }
1438
1439 return 0;
1440err:
1441 return err;
1442}
1443
1444int fsl_mc_ldpaa_exit(bd_t *bd)
1445{
1446 int err = 0;
Yogesh Gaur42e81792017-04-27 10:14:16 +05301447 bool is_dpl_apply_status = false;
Santan Kumar06651b92017-06-29 11:19:34 +05301448 bool mc_boot_status = false;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301449
Alexander Grafb7b84102016-11-17 01:02:57 +01001450 if (bd && mc_lazy_dpl_addr && !fsl_mc_ldpaa_exit(NULL)) {
Yogesh Gaurf9747a52018-01-16 10:08:24 +05301451 err = mc_apply_dpl(mc_lazy_dpl_addr);
1452 if (!err)
1453 fdt_fixup_board_enet(working_fdt);
Alexander Grafb7b84102016-11-17 01:02:57 +01001454 mc_lazy_dpl_addr = 0;
1455 }
1456
Santan Kumar06651b92017-06-29 11:19:34 +05301457 if (!get_mc_boot_status())
1458 mc_boot_status = true;
1459
Prabhakar Kushwaha6dedced2016-03-21 14:19:39 +05301460 /* MC is not loaded intentionally, So return success. */
Santan Kumar06651b92017-06-29 11:19:34 +05301461 if (bd && !mc_boot_status)
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301462 return 0;
1463
Yogesh Gaur42e81792017-04-27 10:14:16 +05301464 /* If DPL is deployed, set is_dpl_apply_status as TRUE. */
1465 if (!get_dpl_apply_status())
1466 is_dpl_apply_status = true;
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301467
Yogesh Gaur42e81792017-04-27 10:14:16 +05301468 /*
1469 * For case MC is loaded but DPL is not deployed, return success and
1470 * print message on console. Else FDT fix-up code execution hanged.
1471 */
Santan Kumar06651b92017-06-29 11:19:34 +05301472 if (bd && mc_boot_status && !is_dpl_apply_status) {
Yogesh Gaur42e81792017-04-27 10:14:16 +05301473 printf("fsl-mc: DPL not deployed, DPAA2 ethernet not work\n");
Yogesh Gaur73fa2062017-11-28 10:11:14 +05301474 goto mc_obj_cleanup;
Yogesh Gaur42e81792017-04-27 10:14:16 +05301475 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301476
Santan Kumar06651b92017-06-29 11:19:34 +05301477 if (bd && mc_boot_status && is_dpl_apply_status)
1478 return 0;
1479
Yogesh Gaur73fa2062017-11-28 10:11:14 +05301480mc_obj_cleanup:
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301481 err = dpbp_exit();
1482 if (err < 0) {
Prabhakar Kushwahaa2a4dc52016-01-20 12:04:37 +05301483 printf("dpbp_exit() failed: %d\n", err);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301484 goto err;
1485 }
1486
1487 err = dpio_exit();
1488 if (err < 0) {
1489 printf("dpio_exit() failed: %d\n", err);
1490 goto err;
1491 }
1492
1493 err = dpni_exit();
1494 if (err < 0) {
1495 printf("dpni_exit() failed: %d\n", err);
1496 goto err;
1497 }
1498
1499 err = dprc_exit();
1500 if (err < 0) {
1501 printf("dprc_exit() failed: %d\n", err);
1502 goto err;
1503 }
1504
1505 return 0;
1506err:
1507 return err;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -07001508}
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301509
1510static int do_fsl_mc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1511{
1512 int err = 0;
1513 if (argc < 3)
1514 goto usage;
1515
1516 switch (argv[1][0]) {
1517 case 's': {
1518 char sub_cmd;
Prabhakar Kushwaha44937212015-11-09 16:42:07 +05301519 u64 mc_fw_addr, mc_dpc_addr;
1520#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
1521 u64 aiop_fw_addr;
1522#endif
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301523
1524 sub_cmd = argv[2][0];
Yogesh Gaur2557c5a2017-11-15 11:59:31 +05301525
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301526 switch (sub_cmd) {
1527 case 'm':
1528 if (argc < 5)
1529 goto usage;
1530
1531 if (get_mc_boot_status() == 0) {
1532 printf("fsl-mc: MC is already booted");
1533 printf("\n");
1534 return err;
1535 }
1536 mc_fw_addr = simple_strtoull(argv[3], NULL, 16);
1537 mc_dpc_addr = simple_strtoull(argv[4], NULL,
1538 16);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301539
1540 if (!mc_init(mc_fw_addr, mc_dpc_addr))
1541 err = mc_init_object();
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301542 break;
1543
1544#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
1545 case 'a':
1546 if (argc < 4)
1547 goto usage;
1548 if (get_aiop_apply_status() == 0) {
1549 printf("fsl-mc: AIOP FW is already");
1550 printf(" applied\n");
1551 return err;
1552 }
1553
1554 aiop_fw_addr = simple_strtoull(argv[3], NULL,
1555 16);
1556
York Sun3c1d2182016-04-04 11:41:26 -07001557 /* if SoC doesn't have AIOP, err = -ENODEV */
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301558 err = load_mc_aiop_img(aiop_fw_addr);
1559 if (!err)
1560 printf("fsl-mc: AIOP FW applied\n");
1561 break;
1562#endif
1563 default:
1564 printf("Invalid option: %s\n", argv[2]);
1565 goto usage;
1566
1567 break;
1568 }
1569 }
1570 break;
1571
Alexander Grafb7b84102016-11-17 01:02:57 +01001572 case 'l':
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301573 case 'a': {
1574 u64 mc_dpl_addr;
1575
1576 if (argc < 4)
1577 goto usage;
1578
1579 if (get_dpl_apply_status() == 0) {
1580 printf("fsl-mc: DPL already applied\n");
1581 return err;
1582 }
1583
1584 mc_dpl_addr = simple_strtoull(argv[3], NULL,
1585 16);
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301586
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301587 if (get_mc_boot_status() != 0) {
1588 printf("fsl-mc: Deploying data path layout ..");
1589 printf("ERROR (MC is not booted)\n");
1590 return -ENODEV;
1591 }
Prabhakar Kushwaha1730a172015-11-04 12:25:59 +05301592
Alexander Grafb7b84102016-11-17 01:02:57 +01001593 if (argv[1][0] == 'l') {
1594 /*
1595 * We will do the actual dpaa exit and dpl apply
1596 * later from announce_and_cleanup().
1597 */
1598 mc_lazy_dpl_addr = mc_dpl_addr;
1599 } else {
1600 /* The user wants it applied now */
1601 if (!fsl_mc_ldpaa_exit(NULL))
1602 err = mc_apply_dpl(mc_dpl_addr);
1603 }
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301604 break;
1605 }
1606 default:
1607 printf("Invalid option: %s\n", argv[1]);
1608 goto usage;
1609 break;
1610 }
1611 return err;
1612 usage:
1613 return CMD_RET_USAGE;
1614}
1615
1616U_BOOT_CMD(
1617 fsl_mc, CONFIG_SYS_MAXARGS, 1, do_fsl_mc,
1618 "DPAA2 command to manage Management Complex (MC)",
1619 "start mc [FW_addr] [DPC_addr] - Start Management Complex\n"
1620 "fsl_mc apply DPL [DPL_addr] - Apply DPL file\n"
Alexander Grafb7b84102016-11-17 01:02:57 +01001621 "fsl_mc lazyapply DPL [DPL_addr] - Apply DPL file on exit\n"
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +05301622 "fsl_mc start aiop [FW_addr] - Start AIOP\n"
1623);
Bogdan Purcareata33a89912017-05-24 16:40:21 +00001624
1625void mc_env_boot(void)
1626{
1627#if defined(CONFIG_FSL_MC_ENET)
1628 char *mc_boot_env_var;
1629 /* The MC may only be initialized in the reset PHY function
1630 * because otherwise U-Boot has not yet set up all the MAC
1631 * address info properly. Without MAC addresses, the MC code
1632 * can not properly initialize the DPC.
1633 */
Simon Glass00caae62017-08-03 12:22:12 -06001634 mc_boot_env_var = env_get(MC_BOOT_ENV_VAR);
Bogdan Purcareata33a89912017-05-24 16:40:21 +00001635 if (mc_boot_env_var)
1636 run_command_list(mc_boot_env_var, -1, 0);
1637#endif /* CONFIG_FSL_MC_ENET */
1638}