mc.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. /*
  2. * Copyright (C) 2017 NXP Semiconductors
  3. * Copyright (C) 2014 Freescale Semiconductor
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <errno.h>
  9. #include <linux/bug.h>
  10. #include <asm/io.h>
  11. #include <libfdt.h>
  12. #include <net.h>
  13. #include <fdt_support.h>
  14. #include <fsl-mc/fsl_mc.h>
  15. #include <fsl-mc/fsl_mc_sys.h>
  16. #include <fsl-mc/fsl_mc_private.h>
  17. #include <fsl-mc/fsl_dpmng.h>
  18. #include <fsl-mc/fsl_dprc.h>
  19. #include <fsl-mc/fsl_dpio.h>
  20. #include <fsl-mc/fsl_dpni.h>
  21. #include <fsl-mc/fsl_qbman_portal.h>
  22. #include <fsl-mc/ldpaa_wriop.h>
  23. #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. #define MC_MEM_SIZE_ENV_VAR "mcmemsize"
  27. #define MC_BOOT_TIMEOUT_ENV_VAR "mcboottimeout"
  28. #define MC_BOOT_ENV_VAR "mcinitcmd"
  29. DECLARE_GLOBAL_DATA_PTR;
  30. static int mc_boot_status = -1;
  31. static int mc_dpl_applied = -1;
  32. #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
  33. static int mc_aiop_applied = -1;
  34. #endif
  35. struct fsl_mc_io *root_mc_io = NULL;
  36. struct fsl_mc_io *dflt_mc_io = NULL; /* child container */
  37. uint16_t root_dprc_handle = 0;
  38. uint16_t dflt_dprc_handle = 0;
  39. int child_dprc_id;
  40. struct fsl_dpbp_obj *dflt_dpbp = NULL;
  41. struct fsl_dpio_obj *dflt_dpio = NULL;
  42. struct fsl_dpni_obj *dflt_dpni = NULL;
  43. static u64 mc_lazy_dpl_addr;
  44. #ifdef DEBUG
  45. void dump_ram_words(const char *title, void *addr)
  46. {
  47. int i;
  48. uint32_t *words = addr;
  49. printf("Dumping beginning of %s (%p):\n", title, addr);
  50. for (i = 0; i < 16; i++)
  51. printf("%#x ", words[i]);
  52. printf("\n");
  53. }
  54. void dump_mc_ccsr_regs(struct mc_ccsr_registers __iomem *mc_ccsr_regs)
  55. {
  56. printf("MC CCSR registers:\n"
  57. "reg_gcr1 %#x\n"
  58. "reg_gsr %#x\n"
  59. "reg_sicbalr %#x\n"
  60. "reg_sicbahr %#x\n"
  61. "reg_sicapr %#x\n"
  62. "reg_mcfbalr %#x\n"
  63. "reg_mcfbahr %#x\n"
  64. "reg_mcfapr %#x\n"
  65. "reg_psr %#x\n",
  66. mc_ccsr_regs->reg_gcr1,
  67. mc_ccsr_regs->reg_gsr,
  68. mc_ccsr_regs->reg_sicbalr,
  69. mc_ccsr_regs->reg_sicbahr,
  70. mc_ccsr_regs->reg_sicapr,
  71. mc_ccsr_regs->reg_mcfbalr,
  72. mc_ccsr_regs->reg_mcfbahr,
  73. mc_ccsr_regs->reg_mcfapr,
  74. mc_ccsr_regs->reg_psr);
  75. }
  76. #else
  77. #define dump_ram_words(title, addr)
  78. #define dump_mc_ccsr_regs(mc_ccsr_regs)
  79. #endif /* DEBUG */
  80. #ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
  81. /**
  82. * Copying MC firmware or DPL image to DDR
  83. */
  84. static int mc_copy_image(const char *title,
  85. u64 image_addr, u32 image_size, u64 mc_ram_addr)
  86. {
  87. debug("%s copied to address %p\n", title, (void *)mc_ram_addr);
  88. memcpy((void *)mc_ram_addr, (void *)image_addr, image_size);
  89. flush_dcache_range(mc_ram_addr, mc_ram_addr + image_size);
  90. return 0;
  91. }
  92. /**
  93. * MC firmware FIT image parser checks if the image is in FIT
  94. * format, verifies integrity of the image and calculates
  95. * raw image address and size values.
  96. * Returns 0 on success and a negative errno on error.
  97. * task fail.
  98. **/
  99. int parse_mc_firmware_fit_image(u64 mc_fw_addr,
  100. const void **raw_image_addr,
  101. size_t *raw_image_size)
  102. {
  103. int format;
  104. void *fit_hdr;
  105. int node_offset;
  106. const void *data;
  107. size_t size;
  108. const char *uname = "firmware";
  109. fit_hdr = (void *)mc_fw_addr;
  110. /* Check if Image is in FIT format */
  111. format = genimg_get_format(fit_hdr);
  112. if (format != IMAGE_FORMAT_FIT) {
  113. printf("fsl-mc: ERR: Bad firmware image (not a FIT image)\n");
  114. return -EINVAL;
  115. }
  116. if (!fit_check_format(fit_hdr)) {
  117. printf("fsl-mc: ERR: Bad firmware image (bad FIT header)\n");
  118. return -EINVAL;
  119. }
  120. node_offset = fit_image_get_node(fit_hdr, uname);
  121. if (node_offset < 0) {
  122. printf("fsl-mc: ERR: Bad firmware image (missing subimage)\n");
  123. return -ENOENT;
  124. }
  125. /* Verify MC firmware image */
  126. if (!(fit_image_verify(fit_hdr, node_offset))) {
  127. printf("fsl-mc: ERR: Bad firmware image (bad CRC)\n");
  128. return -EINVAL;
  129. }
  130. /* Get address and size of raw image */
  131. fit_image_get_data(fit_hdr, node_offset, &data, &size);
  132. *raw_image_addr = data;
  133. *raw_image_size = size;
  134. return 0;
  135. }
  136. #endif
  137. #define MC_DT_INCREASE_SIZE 64
  138. enum mc_fixup_type {
  139. MC_FIXUP_DPL,
  140. MC_FIXUP_DPC
  141. };
  142. static int mc_fixup_mac_addr(void *blob, int nodeoffset,
  143. const char *propname, struct eth_device *eth_dev,
  144. enum mc_fixup_type type)
  145. {
  146. int err = 0, len = 0, size, i;
  147. unsigned char env_enetaddr[ARP_HLEN];
  148. unsigned int enetaddr_32[ARP_HLEN];
  149. void *val = NULL;
  150. switch (type) {
  151. case MC_FIXUP_DPL:
  152. /* DPL likes its addresses on 32 * ARP_HLEN bits */
  153. for (i = 0; i < ARP_HLEN; i++)
  154. enetaddr_32[i] = cpu_to_fdt32(eth_dev->enetaddr[i]);
  155. val = enetaddr_32;
  156. len = sizeof(enetaddr_32);
  157. break;
  158. case MC_FIXUP_DPC:
  159. val = eth_dev->enetaddr;
  160. len = ARP_HLEN;
  161. break;
  162. }
  163. /* MAC address property present */
  164. if (fdt_get_property(blob, nodeoffset, propname, NULL)) {
  165. /* u-boot MAC addr randomly assigned - leave the present one */
  166. if (!eth_env_get_enetaddr_by_index("eth", eth_dev->index,
  167. env_enetaddr))
  168. return err;
  169. } else {
  170. size = MC_DT_INCREASE_SIZE + strlen(propname) + len;
  171. /* make room for mac address property */
  172. err = fdt_increase_size(blob, size);
  173. if (err) {
  174. printf("fdt_increase_size: err=%s\n",
  175. fdt_strerror(err));
  176. return err;
  177. }
  178. }
  179. err = fdt_setprop(blob, nodeoffset, propname, val, len);
  180. if (err) {
  181. printf("fdt_setprop: err=%s\n", fdt_strerror(err));
  182. return err;
  183. }
  184. return err;
  185. }
  186. #define is_dpni(s) (s != NULL ? !strncmp(s, "dpni@", 5) : 0)
  187. const char *dpl_get_connection_endpoint(void *blob, char *endpoint)
  188. {
  189. int connoffset = fdt_path_offset(blob, "/connections"), off;
  190. const char *s1, *s2;
  191. for (off = fdt_first_subnode(blob, connoffset);
  192. off >= 0;
  193. off = fdt_next_subnode(blob, off)) {
  194. s1 = fdt_stringlist_get(blob, off, "endpoint1", 0, NULL);
  195. s2 = fdt_stringlist_get(blob, off, "endpoint2", 0, NULL);
  196. if (!s1 || !s2)
  197. continue;
  198. if (strcmp(endpoint, s1) == 0)
  199. return s2;
  200. if (strcmp(endpoint, s2) == 0)
  201. return s1;
  202. }
  203. return NULL;
  204. }
  205. static int mc_fixup_dpl_mac_addr(void *blob, int dpmac_id,
  206. struct eth_device *eth_dev)
  207. {
  208. int objoff = fdt_path_offset(blob, "/objects");
  209. int dpmacoff = -1, dpnioff = -1;
  210. const char *endpoint;
  211. char mac_name[10];
  212. int err;
  213. sprintf(mac_name, "dpmac@%d", dpmac_id);
  214. dpmacoff = fdt_subnode_offset(blob, objoff, mac_name);
  215. if (dpmacoff < 0)
  216. /* dpmac not defined in DPL, so skip it. */
  217. return 0;
  218. err = mc_fixup_mac_addr(blob, dpmacoff, "mac_addr", eth_dev,
  219. MC_FIXUP_DPL);
  220. if (err) {
  221. printf("Error fixing up dpmac mac_addr in DPL\n");
  222. return err;
  223. }
  224. /* now we need to figure out if there is any
  225. * DPNI connected to this MAC, so we walk the
  226. * connection list
  227. */
  228. endpoint = dpl_get_connection_endpoint(blob, mac_name);
  229. if (!is_dpni(endpoint))
  230. return 0;
  231. /* let's see if we can fixup the DPNI as well */
  232. dpnioff = fdt_subnode_offset(blob, objoff, endpoint);
  233. if (dpnioff < 0)
  234. /* DPNI not defined in DPL in the objects area */
  235. return 0;
  236. return mc_fixup_mac_addr(blob, dpnioff, "mac_addr", eth_dev,
  237. MC_FIXUP_DPL);
  238. }
  239. static int mc_fixup_dpc_mac_addr(void *blob, int dpmac_id,
  240. struct eth_device *eth_dev)
  241. {
  242. int nodeoffset = fdt_path_offset(blob, "/board_info/ports"), noff;
  243. int err = 0;
  244. char mac_name[10];
  245. const char link_type_mode[] = "MAC_LINK_TYPE_FIXED";
  246. sprintf(mac_name, "mac@%d", dpmac_id);
  247. /* node not found - create it */
  248. noff = fdt_subnode_offset(blob, nodeoffset, (const char *)mac_name);
  249. if (noff < 0) {
  250. err = fdt_increase_size(blob, 200);
  251. if (err) {
  252. printf("fdt_increase_size: err=%s\n",
  253. fdt_strerror(err));
  254. return err;
  255. }
  256. noff = fdt_add_subnode(blob, nodeoffset, mac_name);
  257. if (noff < 0) {
  258. printf("fdt_add_subnode: err=%s\n",
  259. fdt_strerror(err));
  260. return err;
  261. }
  262. /* add default property of fixed link */
  263. err = fdt_appendprop_string(blob, noff,
  264. "link_type", link_type_mode);
  265. if (err) {
  266. printf("fdt_appendprop_string: err=%s\n",
  267. fdt_strerror(err));
  268. return err;
  269. }
  270. }
  271. return mc_fixup_mac_addr(blob, noff, "port_mac_address", eth_dev,
  272. MC_FIXUP_DPC);
  273. }
  274. static int mc_fixup_mac_addrs(void *blob, enum mc_fixup_type type)
  275. {
  276. int i, err = 0, ret = 0;
  277. char ethname[10];
  278. struct eth_device *eth_dev;
  279. for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++) {
  280. /* port not enabled */
  281. if ((wriop_is_enabled_dpmac(i) != 1) ||
  282. (wriop_get_phy_address(i) == -1))
  283. continue;
  284. sprintf(ethname, "DPMAC%d@%s", i,
  285. phy_interface_strings[wriop_get_enet_if(i)]);
  286. eth_dev = eth_get_dev_by_name(ethname);
  287. if (eth_dev == NULL)
  288. continue;
  289. switch (type) {
  290. case MC_FIXUP_DPL:
  291. err = mc_fixup_dpl_mac_addr(blob, i, eth_dev);
  292. break;
  293. case MC_FIXUP_DPC:
  294. err = mc_fixup_dpc_mac_addr(blob, i, eth_dev);
  295. break;
  296. default:
  297. break;
  298. }
  299. if (err)
  300. printf("fsl-mc: ERROR fixing mac address for %s\n",
  301. ethname);
  302. ret |= err;
  303. }
  304. return ret;
  305. }
  306. static int mc_fixup_dpc(u64 dpc_addr)
  307. {
  308. void *blob = (void *)dpc_addr;
  309. int nodeoffset, err = 0;
  310. /* delete any existing ICID pools */
  311. nodeoffset = fdt_path_offset(blob, "/resources/icid_pools");
  312. if (fdt_del_node(blob, nodeoffset) < 0)
  313. printf("\nfsl-mc: WARNING: could not delete ICID pool\n");
  314. /* add a new pool */
  315. nodeoffset = fdt_path_offset(blob, "/resources");
  316. if (nodeoffset < 0) {
  317. printf("\nfsl-mc: ERROR: DPC is missing /resources\n");
  318. return -EINVAL;
  319. }
  320. nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pools");
  321. nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pool@0");
  322. do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
  323. "base_icid", FSL_DPAA2_STREAM_ID_START, 1);
  324. do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
  325. "num",
  326. FSL_DPAA2_STREAM_ID_END -
  327. FSL_DPAA2_STREAM_ID_START + 1, 1);
  328. /* fixup MAC addresses for dpmac ports */
  329. nodeoffset = fdt_path_offset(blob, "/board_info/ports");
  330. if (nodeoffset < 0)
  331. return 0;
  332. err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPC);
  333. flush_dcache_range(dpc_addr, dpc_addr + fdt_totalsize(blob));
  334. return err;
  335. }
  336. static int load_mc_dpc(u64 mc_ram_addr, size_t mc_ram_size, u64 mc_dpc_addr)
  337. {
  338. u64 mc_dpc_offset;
  339. #ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
  340. int error;
  341. void *dpc_fdt_hdr;
  342. int dpc_size;
  343. #endif
  344. #ifdef CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET
  345. BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET & 0x3) != 0 ||
  346. CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET > 0xffffffff);
  347. mc_dpc_offset = CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET;
  348. #else
  349. #error "CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET not defined"
  350. #endif
  351. /*
  352. * Load the MC DPC blob in the MC private DRAM block:
  353. */
  354. #ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
  355. printf("MC DPC is preloaded to %#llx\n", mc_ram_addr + mc_dpc_offset);
  356. #else
  357. /*
  358. * Get address and size of the DPC blob stored in flash:
  359. */
  360. dpc_fdt_hdr = (void *)mc_dpc_addr;
  361. error = fdt_check_header(dpc_fdt_hdr);
  362. if (error != 0) {
  363. /*
  364. * Don't return with error here, since the MC firmware can
  365. * still boot without a DPC
  366. */
  367. printf("\nfsl-mc: WARNING: No DPC image found");
  368. return 0;
  369. }
  370. dpc_size = fdt_totalsize(dpc_fdt_hdr);
  371. if (dpc_size > CONFIG_SYS_LS_MC_DPC_MAX_LENGTH) {
  372. printf("\nfsl-mc: ERROR: Bad DPC image (too large: %d)\n",
  373. dpc_size);
  374. return -EINVAL;
  375. }
  376. mc_copy_image("MC DPC blob",
  377. (u64)dpc_fdt_hdr, dpc_size, mc_ram_addr + mc_dpc_offset);
  378. #endif /* not defined CONFIG_SYS_LS_MC_DPC_IN_DDR */
  379. if (mc_fixup_dpc(mc_ram_addr + mc_dpc_offset))
  380. return -EINVAL;
  381. dump_ram_words("DPC", (void *)(mc_ram_addr + mc_dpc_offset));
  382. return 0;
  383. }
  384. static int mc_fixup_dpl(u64 dpl_addr)
  385. {
  386. void *blob = (void *)dpl_addr;
  387. u32 ver = fdt_getprop_u32_default(blob, "/", "dpl-version", 0);
  388. int err = 0;
  389. /* The DPL fixup for mac addresses is only relevant
  390. * for old-style DPLs
  391. */
  392. if (ver >= 10)
  393. return 0;
  394. err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPL);
  395. flush_dcache_range(dpl_addr, dpl_addr + fdt_totalsize(blob));
  396. return err;
  397. }
  398. static int load_mc_dpl(u64 mc_ram_addr, size_t mc_ram_size, u64 mc_dpl_addr)
  399. {
  400. u64 mc_dpl_offset;
  401. #ifndef CONFIG_SYS_LS_MC_DPL_IN_DDR
  402. int error;
  403. void *dpl_fdt_hdr;
  404. int dpl_size;
  405. #endif
  406. #ifdef CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET
  407. BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET & 0x3) != 0 ||
  408. CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET > 0xffffffff);
  409. mc_dpl_offset = CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET;
  410. #else
  411. #error "CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET not defined"
  412. #endif
  413. /*
  414. * Load the MC DPL blob in the MC private DRAM block:
  415. */
  416. #ifdef CONFIG_SYS_LS_MC_DPL_IN_DDR
  417. printf("MC DPL is preloaded to %#llx\n", mc_ram_addr + mc_dpl_offset);
  418. #else
  419. /*
  420. * Get address and size of the DPL blob stored in flash:
  421. */
  422. dpl_fdt_hdr = (void *)mc_dpl_addr;
  423. error = fdt_check_header(dpl_fdt_hdr);
  424. if (error != 0) {
  425. printf("\nfsl-mc: ERROR: Bad DPL image (bad header)\n");
  426. return error;
  427. }
  428. dpl_size = fdt_totalsize(dpl_fdt_hdr);
  429. if (dpl_size > CONFIG_SYS_LS_MC_DPL_MAX_LENGTH) {
  430. printf("\nfsl-mc: ERROR: Bad DPL image (too large: %d)\n",
  431. dpl_size);
  432. return -EINVAL;
  433. }
  434. mc_copy_image("MC DPL blob",
  435. (u64)dpl_fdt_hdr, dpl_size, mc_ram_addr + mc_dpl_offset);
  436. #endif /* not defined CONFIG_SYS_LS_MC_DPL_IN_DDR */
  437. if (mc_fixup_dpl(mc_ram_addr + mc_dpl_offset))
  438. return -EINVAL;
  439. dump_ram_words("DPL", (void *)(mc_ram_addr + mc_dpl_offset));
  440. return 0;
  441. }
  442. /**
  443. * Return the MC boot timeout value in milliseconds
  444. */
  445. static unsigned long get_mc_boot_timeout_ms(void)
  446. {
  447. unsigned long timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
  448. char *timeout_ms_env_var = env_get(MC_BOOT_TIMEOUT_ENV_VAR);
  449. if (timeout_ms_env_var) {
  450. timeout_ms = simple_strtoul(timeout_ms_env_var, NULL, 10);
  451. if (timeout_ms == 0) {
  452. printf("fsl-mc: WARNING: Invalid value for \'"
  453. MC_BOOT_TIMEOUT_ENV_VAR
  454. "\' environment variable: %lu\n",
  455. timeout_ms);
  456. timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
  457. }
  458. }
  459. return timeout_ms;
  460. }
  461. #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
  462. __weak bool soc_has_aiop(void)
  463. {
  464. return false;
  465. }
  466. static int load_mc_aiop_img(u64 aiop_fw_addr)
  467. {
  468. u64 mc_ram_addr = mc_get_dram_addr();
  469. #ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
  470. void *aiop_img;
  471. #endif
  472. /* Check if AIOP is available */
  473. if (!soc_has_aiop())
  474. return -ENODEV;
  475. /*
  476. * Load the MC AIOP image in the MC private DRAM block:
  477. */
  478. #ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
  479. printf("MC AIOP is preloaded to %#llx\n", mc_ram_addr +
  480. CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
  481. #else
  482. aiop_img = (void *)aiop_fw_addr;
  483. mc_copy_image("MC AIOP image",
  484. (u64)aiop_img, CONFIG_SYS_LS_MC_AIOP_IMG_MAX_LENGTH,
  485. mc_ram_addr + CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
  486. #endif
  487. mc_aiop_applied = 0;
  488. return 0;
  489. }
  490. #endif
  491. static int wait_for_mc(bool booting_mc, u32 *final_reg_gsr)
  492. {
  493. u32 reg_gsr;
  494. u32 mc_fw_boot_status;
  495. unsigned long timeout_ms = get_mc_boot_timeout_ms();
  496. struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
  497. dmb();
  498. assert(timeout_ms > 0);
  499. for (;;) {
  500. udelay(1000); /* throttle polling */
  501. reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
  502. mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
  503. if (mc_fw_boot_status & 0x1)
  504. break;
  505. timeout_ms--;
  506. if (timeout_ms == 0)
  507. break;
  508. }
  509. if (timeout_ms == 0) {
  510. printf("ERROR: timeout\n");
  511. /* TODO: Get an error status from an MC CCSR register */
  512. return -ETIMEDOUT;
  513. }
  514. if (mc_fw_boot_status != 0x1) {
  515. /*
  516. * TODO: Identify critical errors from the GSR register's FS
  517. * field and for those errors, set error to -ENODEV or other
  518. * appropriate errno, so that the status property is set to
  519. * failure in the fsl,dprc device tree node.
  520. */
  521. printf("WARNING: Firmware returned an error (GSR: %#x)\n",
  522. reg_gsr);
  523. } else {
  524. printf("SUCCESS\n");
  525. }
  526. *final_reg_gsr = reg_gsr;
  527. return 0;
  528. }
  529. int mc_init(u64 mc_fw_addr, u64 mc_dpc_addr)
  530. {
  531. int error = 0;
  532. int portal_id = 0;
  533. struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
  534. u64 mc_ram_addr = mc_get_dram_addr();
  535. u32 reg_gsr;
  536. u32 reg_mcfbalr;
  537. #ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
  538. const void *raw_image_addr;
  539. size_t raw_image_size = 0;
  540. #endif
  541. struct mc_version mc_ver_info;
  542. u8 mc_ram_num_256mb_blocks;
  543. size_t mc_ram_size = mc_get_dram_block_size();
  544. mc_ram_num_256mb_blocks = mc_ram_size / MC_RAM_SIZE_ALIGNMENT;
  545. if (mc_ram_num_256mb_blocks < 1 || mc_ram_num_256mb_blocks > 0xff) {
  546. error = -EINVAL;
  547. printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
  548. mc_ram_size);
  549. goto out;
  550. }
  551. /*
  552. * Management Complex cores should be held at reset out of POR.
  553. * U-Boot should be the first software to touch MC. To be safe,
  554. * we reset all cores again by setting GCR1 to 0. It doesn't do
  555. * anything if they are held at reset. After we setup the firmware
  556. * we kick off MC by deasserting the reset bit for core 0, and
  557. * deasserting the reset bits for Command Portal Managers.
  558. * The stop bits are not touched here. They are used to stop the
  559. * cores when they are active. Setting stop bits doesn't stop the
  560. * cores from fetching instructions when they are released from
  561. * reset.
  562. */
  563. out_le32(&mc_ccsr_regs->reg_gcr1, 0);
  564. dmb();
  565. #ifdef CONFIG_SYS_LS_MC_FW_IN_DDR
  566. printf("MC firmware is preloaded to %#llx\n", mc_ram_addr);
  567. #else
  568. error = parse_mc_firmware_fit_image(mc_fw_addr, &raw_image_addr,
  569. &raw_image_size);
  570. if (error != 0)
  571. goto out;
  572. /*
  573. * Load the MC FW at the beginning of the MC private DRAM block:
  574. */
  575. mc_copy_image("MC Firmware",
  576. (u64)raw_image_addr, raw_image_size, mc_ram_addr);
  577. #endif
  578. dump_ram_words("firmware", (void *)mc_ram_addr);
  579. error = load_mc_dpc(mc_ram_addr, mc_ram_size, mc_dpc_addr);
  580. if (error != 0)
  581. goto out;
  582. debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
  583. dump_mc_ccsr_regs(mc_ccsr_regs);
  584. /*
  585. * Tell MC what is the address range of the DRAM block assigned to it:
  586. */
  587. reg_mcfbalr = (u32)mc_ram_addr |
  588. (mc_ram_num_256mb_blocks - 1);
  589. out_le32(&mc_ccsr_regs->reg_mcfbalr, reg_mcfbalr);
  590. out_le32(&mc_ccsr_regs->reg_mcfbahr,
  591. (u32)(mc_ram_addr >> 32));
  592. out_le32(&mc_ccsr_regs->reg_mcfapr, FSL_BYPASS_AMQ);
  593. /*
  594. * Tell the MC that we want delayed DPL deployment.
  595. */
  596. out_le32(&mc_ccsr_regs->reg_gsr, 0xDD00);
  597. printf("\nfsl-mc: Booting Management Complex ... ");
  598. /*
  599. * Deassert reset and release MC core 0 to run
  600. */
  601. out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
  602. error = wait_for_mc(true, &reg_gsr);
  603. if (error != 0)
  604. goto out;
  605. /*
  606. * TODO: need to obtain the portal_id for the root container from the
  607. * DPL
  608. */
  609. portal_id = 0;
  610. /*
  611. * Initialize the global default MC portal
  612. * And check that the MC firmware is responding portal commands:
  613. */
  614. root_mc_io = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
  615. if (!root_mc_io) {
  616. printf(" No memory: malloc() failed\n");
  617. return -ENOMEM;
  618. }
  619. root_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(portal_id);
  620. debug("Checking access to MC portal of root DPRC container (portal_id %d, portal physical addr %p)\n",
  621. portal_id, root_mc_io->mmio_regs);
  622. error = mc_get_version(root_mc_io, MC_CMD_NO_FLAGS, &mc_ver_info);
  623. if (error != 0) {
  624. printf("fsl-mc: ERROR: Firmware version check failed (error: %d)\n",
  625. error);
  626. goto out;
  627. }
  628. printf("fsl-mc: Management Complex booted (version: %d.%d.%d, boot status: %#x)\n",
  629. mc_ver_info.major, mc_ver_info.minor, mc_ver_info.revision,
  630. reg_gsr & GSR_FS_MASK);
  631. out:
  632. if (error != 0)
  633. mc_boot_status = error;
  634. else
  635. mc_boot_status = 0;
  636. return error;
  637. }
  638. int mc_apply_dpl(u64 mc_dpl_addr)
  639. {
  640. struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
  641. int error = 0;
  642. u32 reg_gsr;
  643. u64 mc_ram_addr = mc_get_dram_addr();
  644. size_t mc_ram_size = mc_get_dram_block_size();
  645. if (!mc_dpl_addr)
  646. return -1;
  647. error = load_mc_dpl(mc_ram_addr, mc_ram_size, mc_dpl_addr);
  648. if (error != 0)
  649. return error;
  650. /*
  651. * Tell the MC to deploy the DPL:
  652. */
  653. out_le32(&mc_ccsr_regs->reg_gsr, 0x0);
  654. printf("fsl-mc: Deploying data path layout ... ");
  655. error = wait_for_mc(false, &reg_gsr);
  656. if (!error)
  657. mc_dpl_applied = 0;
  658. return error;
  659. }
  660. int get_mc_boot_status(void)
  661. {
  662. return mc_boot_status;
  663. }
  664. #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
  665. int get_aiop_apply_status(void)
  666. {
  667. return mc_aiop_applied;
  668. }
  669. #endif
  670. int get_dpl_apply_status(void)
  671. {
  672. return mc_dpl_applied;
  673. }
  674. /**
  675. * Return the MC address of private DRAM block.
  676. */
  677. u64 mc_get_dram_addr(void)
  678. {
  679. return gd->arch.resv_ram;
  680. }
  681. /**
  682. * Return the actual size of the MC private DRAM block.
  683. */
  684. unsigned long mc_get_dram_block_size(void)
  685. {
  686. unsigned long dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
  687. char *dram_block_size_env_var = env_get(MC_MEM_SIZE_ENV_VAR);
  688. if (dram_block_size_env_var) {
  689. dram_block_size = simple_strtoul(dram_block_size_env_var, NULL,
  690. 10);
  691. if (dram_block_size < CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE) {
  692. printf("fsl-mc: WARNING: Invalid value for \'"
  693. MC_MEM_SIZE_ENV_VAR
  694. "\' environment variable: %lu\n",
  695. dram_block_size);
  696. dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
  697. }
  698. }
  699. return dram_block_size;
  700. }
  701. int fsl_mc_ldpaa_init(bd_t *bis)
  702. {
  703. int i;
  704. for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++)
  705. if ((wriop_is_enabled_dpmac(i) == 1) &&
  706. (wriop_get_phy_address(i) != -1))
  707. ldpaa_eth_init(i, wriop_get_enet_if(i));
  708. return 0;
  709. }
  710. static int dprc_version_check(struct fsl_mc_io *mc_io, uint16_t handle)
  711. {
  712. struct dprc_attributes attr;
  713. int error;
  714. memset(&attr, 0, sizeof(struct dprc_attributes));
  715. error = dprc_get_attributes(mc_io, MC_CMD_NO_FLAGS, handle, &attr);
  716. if (error == 0) {
  717. if ((attr.version.major != DPRC_VER_MAJOR) ||
  718. (attr.version.minor != DPRC_VER_MINOR)) {
  719. printf("DPRC version mismatch found %u.%u,",
  720. attr.version.major,
  721. attr.version.minor);
  722. printf("supported version is %u.%u\n",
  723. DPRC_VER_MAJOR, DPRC_VER_MINOR);
  724. }
  725. }
  726. return error;
  727. }
  728. static int dpio_init(void)
  729. {
  730. struct qbman_swp_desc p_des;
  731. struct dpio_attr attr;
  732. struct dpio_cfg dpio_cfg;
  733. int err = 0;
  734. dflt_dpio = (struct fsl_dpio_obj *)malloc(sizeof(struct fsl_dpio_obj));
  735. if (!dflt_dpio) {
  736. printf("No memory: malloc() failed\n");
  737. err = -ENOMEM;
  738. goto err_malloc;
  739. }
  740. dpio_cfg.channel_mode = DPIO_LOCAL_CHANNEL;
  741. dpio_cfg.num_priorities = 8;
  742. err = dpio_create(dflt_mc_io, MC_CMD_NO_FLAGS, &dpio_cfg,
  743. &dflt_dpio->dpio_handle);
  744. if (err < 0) {
  745. printf("dpio_create() failed: %d\n", err);
  746. err = -ENODEV;
  747. goto err_create;
  748. }
  749. memset(&attr, 0, sizeof(struct dpio_attr));
  750. err = dpio_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
  751. dflt_dpio->dpio_handle, &attr);
  752. if (err < 0) {
  753. printf("dpio_get_attributes() failed: %d\n", err);
  754. goto err_get_attr;
  755. }
  756. if ((attr.version.major != DPIO_VER_MAJOR) ||
  757. (attr.version.minor != DPIO_VER_MINOR)) {
  758. printf("DPIO version mismatch found %u.%u,",
  759. attr.version.major, attr.version.minor);
  760. printf("supported version is %u.%u\n",
  761. DPIO_VER_MAJOR, DPIO_VER_MINOR);
  762. }
  763. dflt_dpio->dpio_id = attr.id;
  764. #ifdef DEBUG
  765. printf("Init: DPIO id=0x%d\n", dflt_dpio->dpio_id);
  766. #endif
  767. err = dpio_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  768. if (err < 0) {
  769. printf("dpio_enable() failed %d\n", err);
  770. goto err_get_enable;
  771. }
  772. debug("ce_offset=0x%llx, ci_offset=0x%llx, portalid=%d, prios=%d\n",
  773. attr.qbman_portal_ce_offset,
  774. attr.qbman_portal_ci_offset,
  775. attr.qbman_portal_id,
  776. attr.num_priorities);
  777. p_des.cena_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
  778. + attr.qbman_portal_ce_offset);
  779. p_des.cinh_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
  780. + attr.qbman_portal_ci_offset);
  781. dflt_dpio->sw_portal = qbman_swp_init(&p_des);
  782. if (dflt_dpio->sw_portal == NULL) {
  783. printf("qbman_swp_init() failed\n");
  784. goto err_get_swp_init;
  785. }
  786. return 0;
  787. err_get_swp_init:
  788. dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  789. err_get_enable:
  790. err_get_attr:
  791. dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  792. dpio_destroy(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  793. err_create:
  794. free(dflt_dpio);
  795. err_malloc:
  796. return err;
  797. }
  798. static int dpio_exit(void)
  799. {
  800. int err;
  801. err = dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  802. if (err < 0) {
  803. printf("dpio_disable() failed: %d\n", err);
  804. goto err;
  805. }
  806. err = dpio_destroy(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  807. if (err < 0) {
  808. printf("dpio_destroy() failed: %d\n", err);
  809. goto err;
  810. }
  811. #ifdef DEBUG
  812. printf("Exit: DPIO id=0x%d\n", dflt_dpio->dpio_id);
  813. #endif
  814. if (dflt_dpio)
  815. free(dflt_dpio);
  816. return 0;
  817. err:
  818. return err;
  819. }
  820. static int dprc_init(void)
  821. {
  822. int err, child_portal_id, container_id;
  823. struct dprc_cfg cfg;
  824. uint64_t mc_portal_offset;
  825. /* Open root container */
  826. err = dprc_get_container_id(root_mc_io, MC_CMD_NO_FLAGS, &container_id);
  827. if (err < 0) {
  828. printf("dprc_get_container_id(): Root failed: %d\n", err);
  829. goto err_root_container_id;
  830. }
  831. #ifdef DEBUG
  832. printf("Root container id = %d\n", container_id);
  833. #endif
  834. err = dprc_open(root_mc_io, MC_CMD_NO_FLAGS, container_id,
  835. &root_dprc_handle);
  836. if (err < 0) {
  837. printf("dprc_open(): Root Container failed: %d\n", err);
  838. goto err_root_open;
  839. }
  840. if (!root_dprc_handle) {
  841. printf("dprc_open(): Root Container Handle is not valid\n");
  842. goto err_root_open;
  843. }
  844. err = dprc_version_check(root_mc_io, root_dprc_handle);
  845. if (err < 0) {
  846. printf("dprc_version_check() failed: %d\n", err);
  847. goto err_root_open;
  848. }
  849. memset(&cfg, 0, sizeof(struct dprc_cfg));
  850. cfg.options = DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED |
  851. DPRC_CFG_OPT_OBJ_CREATE_ALLOWED |
  852. DPRC_CFG_OPT_ALLOC_ALLOWED;
  853. cfg.icid = DPRC_GET_ICID_FROM_POOL;
  854. cfg.portal_id = DPRC_GET_PORTAL_ID_FROM_POOL;
  855. err = dprc_create_container(root_mc_io, MC_CMD_NO_FLAGS,
  856. root_dprc_handle,
  857. &cfg,
  858. &child_dprc_id,
  859. &mc_portal_offset);
  860. if (err < 0) {
  861. printf("dprc_create_container() failed: %d\n", err);
  862. goto err_create;
  863. }
  864. dflt_mc_io = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
  865. if (!dflt_mc_io) {
  866. err = -ENOMEM;
  867. printf(" No memory: malloc() failed\n");
  868. goto err_malloc;
  869. }
  870. child_portal_id = MC_PORTAL_OFFSET_TO_PORTAL_ID(mc_portal_offset);
  871. dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(child_portal_id);
  872. #ifdef DEBUG
  873. printf("MC portal of child DPRC container: %d, physical addr %p)\n",
  874. child_dprc_id, dflt_mc_io->mmio_regs);
  875. #endif
  876. err = dprc_open(dflt_mc_io, MC_CMD_NO_FLAGS, child_dprc_id,
  877. &dflt_dprc_handle);
  878. if (err < 0) {
  879. printf("dprc_open(): Child container failed: %d\n", err);
  880. goto err_child_open;
  881. }
  882. if (!dflt_dprc_handle) {
  883. printf("dprc_open(): Child container Handle is not valid\n");
  884. goto err_child_open;
  885. }
  886. return 0;
  887. err_child_open:
  888. free(dflt_mc_io);
  889. err_malloc:
  890. dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
  891. root_dprc_handle, child_dprc_id);
  892. err_create:
  893. dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
  894. err_root_open:
  895. err_root_container_id:
  896. return err;
  897. }
  898. static int dprc_exit(void)
  899. {
  900. int err;
  901. err = dprc_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dprc_handle);
  902. if (err < 0) {
  903. printf("dprc_close(): Child failed: %d\n", err);
  904. goto err;
  905. }
  906. err = dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
  907. root_dprc_handle, child_dprc_id);
  908. if (err < 0) {
  909. printf("dprc_destroy_container() failed: %d\n", err);
  910. goto err;
  911. }
  912. err = dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
  913. if (err < 0) {
  914. printf("dprc_close(): Root failed: %d\n", err);
  915. goto err;
  916. }
  917. if (dflt_mc_io)
  918. free(dflt_mc_io);
  919. if (root_mc_io)
  920. free(root_mc_io);
  921. return 0;
  922. err:
  923. return err;
  924. }
  925. static int dpbp_init(void)
  926. {
  927. int err;
  928. struct dpbp_attr dpbp_attr;
  929. struct dpbp_cfg dpbp_cfg;
  930. dflt_dpbp = (struct fsl_dpbp_obj *)malloc(sizeof(struct fsl_dpbp_obj));
  931. if (!dflt_dpbp) {
  932. printf("No memory: malloc() failed\n");
  933. err = -ENOMEM;
  934. goto err_malloc;
  935. }
  936. dpbp_cfg.options = 512;
  937. err = dpbp_create(dflt_mc_io, MC_CMD_NO_FLAGS, &dpbp_cfg,
  938. &dflt_dpbp->dpbp_handle);
  939. if (err < 0) {
  940. err = -ENODEV;
  941. printf("dpbp_create() failed: %d\n", err);
  942. goto err_create;
  943. }
  944. memset(&dpbp_attr, 0, sizeof(struct dpbp_attr));
  945. err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
  946. dflt_dpbp->dpbp_handle,
  947. &dpbp_attr);
  948. if (err < 0) {
  949. printf("dpbp_get_attributes() failed: %d\n", err);
  950. goto err_get_attr;
  951. }
  952. if ((dpbp_attr.version.major != DPBP_VER_MAJOR) ||
  953. (dpbp_attr.version.minor != DPBP_VER_MINOR)) {
  954. printf("DPBP version mismatch found %u.%u,",
  955. dpbp_attr.version.major, dpbp_attr.version.minor);
  956. printf("supported version is %u.%u\n",
  957. DPBP_VER_MAJOR, DPBP_VER_MINOR);
  958. }
  959. dflt_dpbp->dpbp_attr.id = dpbp_attr.id;
  960. #ifdef DEBUG
  961. printf("Init: DPBP id=0x%d\n", dflt_dpbp->dpbp_attr.id);
  962. #endif
  963. err = dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
  964. if (err < 0) {
  965. printf("dpbp_close() failed: %d\n", err);
  966. goto err_close;
  967. }
  968. return 0;
  969. err_close:
  970. free(dflt_dpbp);
  971. err_get_attr:
  972. dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
  973. dpbp_destroy(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
  974. err_create:
  975. err_malloc:
  976. return err;
  977. }
  978. static int dpbp_exit(void)
  979. {
  980. int err;
  981. err = dpbp_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_attr.id,
  982. &dflt_dpbp->dpbp_handle);
  983. if (err < 0) {
  984. printf("dpbp_open() failed: %d\n", err);
  985. goto err;
  986. }
  987. err = dpbp_destroy(dflt_mc_io, MC_CMD_NO_FLAGS,
  988. dflt_dpbp->dpbp_handle);
  989. if (err < 0) {
  990. printf("dpbp_destroy() failed: %d\n", err);
  991. goto err;
  992. }
  993. #ifdef DEBUG
  994. printf("Exit: DPBP id=0x%d\n", dflt_dpbp->dpbp_attr.id);
  995. #endif
  996. if (dflt_dpbp)
  997. free(dflt_dpbp);
  998. return 0;
  999. err:
  1000. return err;
  1001. }
  1002. static int dpni_init(void)
  1003. {
  1004. int err;
  1005. struct dpni_attr dpni_attr;
  1006. uint8_t ext_cfg_buf[256] = {0};
  1007. struct dpni_extended_cfg dpni_extended_cfg;
  1008. struct dpni_cfg dpni_cfg;
  1009. dflt_dpni = (struct fsl_dpni_obj *)malloc(sizeof(struct fsl_dpni_obj));
  1010. if (!dflt_dpni) {
  1011. printf("No memory: malloc() failed\n");
  1012. err = -ENOMEM;
  1013. goto err_malloc;
  1014. }
  1015. memset(&dpni_extended_cfg, 0, sizeof(dpni_extended_cfg));
  1016. err = dpni_prepare_extended_cfg(&dpni_extended_cfg, &ext_cfg_buf[0]);
  1017. if (err < 0) {
  1018. err = -ENODEV;
  1019. printf("dpni_prepare_extended_cfg() failed: %d\n", err);
  1020. goto err_prepare_extended_cfg;
  1021. }
  1022. memset(&dpni_cfg, 0, sizeof(dpni_cfg));
  1023. dpni_cfg.adv.options = DPNI_OPT_UNICAST_FILTER |
  1024. DPNI_OPT_MULTICAST_FILTER;
  1025. dpni_cfg.adv.ext_cfg_iova = (uint64_t)&ext_cfg_buf[0];
  1026. err = dpni_create(dflt_mc_io, MC_CMD_NO_FLAGS, &dpni_cfg,
  1027. &dflt_dpni->dpni_handle);
  1028. if (err < 0) {
  1029. err = -ENODEV;
  1030. printf("dpni_create() failed: %d\n", err);
  1031. goto err_create;
  1032. }
  1033. memset(&dpni_attr, 0, sizeof(struct dpni_attr));
  1034. err = dpni_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
  1035. dflt_dpni->dpni_handle,
  1036. &dpni_attr);
  1037. if (err < 0) {
  1038. printf("dpni_get_attributes() failed: %d\n", err);
  1039. goto err_get_attr;
  1040. }
  1041. if ((dpni_attr.version.major != DPNI_VER_MAJOR) ||
  1042. (dpni_attr.version.minor != DPNI_VER_MINOR)) {
  1043. printf("DPNI version mismatch found %u.%u,",
  1044. dpni_attr.version.major, dpni_attr.version.minor);
  1045. printf("supported version is %u.%u\n",
  1046. DPNI_VER_MAJOR, DPNI_VER_MINOR);
  1047. }
  1048. dflt_dpni->dpni_id = dpni_attr.id;
  1049. #ifdef DEBUG
  1050. printf("Init: DPNI id=0x%d\n", dflt_dpni->dpni_id);
  1051. #endif
  1052. err = dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
  1053. if (err < 0) {
  1054. printf("dpni_close() failed: %d\n", err);
  1055. goto err_close;
  1056. }
  1057. return 0;
  1058. err_close:
  1059. err_get_attr:
  1060. dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
  1061. dpni_destroy(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
  1062. err_create:
  1063. err_prepare_extended_cfg:
  1064. free(dflt_dpni);
  1065. err_malloc:
  1066. return err;
  1067. }
  1068. static int dpni_exit(void)
  1069. {
  1070. int err;
  1071. err = dpni_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_id,
  1072. &dflt_dpni->dpni_handle);
  1073. if (err < 0) {
  1074. printf("dpni_open() failed: %d\n", err);
  1075. goto err;
  1076. }
  1077. err = dpni_destroy(dflt_mc_io, MC_CMD_NO_FLAGS,
  1078. dflt_dpni->dpni_handle);
  1079. if (err < 0) {
  1080. printf("dpni_destroy() failed: %d\n", err);
  1081. goto err;
  1082. }
  1083. #ifdef DEBUG
  1084. printf("Exit: DPNI id=0x%d\n", dflt_dpni->dpni_id);
  1085. #endif
  1086. if (dflt_dpni)
  1087. free(dflt_dpni);
  1088. return 0;
  1089. err:
  1090. return err;
  1091. }
  1092. static int mc_init_object(void)
  1093. {
  1094. int err = 0;
  1095. err = dprc_init();
  1096. if (err < 0) {
  1097. printf("dprc_init() failed: %d\n", err);
  1098. goto err;
  1099. }
  1100. err = dpbp_init();
  1101. if (err < 0) {
  1102. printf("dpbp_init() failed: %d\n", err);
  1103. goto err;
  1104. }
  1105. err = dpio_init();
  1106. if (err < 0) {
  1107. printf("dpio_init() failed: %d\n", err);
  1108. goto err;
  1109. }
  1110. err = dpni_init();
  1111. if (err < 0) {
  1112. printf("dpni_init() failed: %d\n", err);
  1113. goto err;
  1114. }
  1115. return 0;
  1116. err:
  1117. return err;
  1118. }
  1119. int fsl_mc_ldpaa_exit(bd_t *bd)
  1120. {
  1121. int err = 0;
  1122. bool is_dpl_apply_status = false;
  1123. bool mc_boot_status = false;
  1124. if (bd && mc_lazy_dpl_addr && !fsl_mc_ldpaa_exit(NULL)) {
  1125. mc_apply_dpl(mc_lazy_dpl_addr);
  1126. mc_lazy_dpl_addr = 0;
  1127. }
  1128. if (!get_mc_boot_status())
  1129. mc_boot_status = true;
  1130. /* MC is not loaded intentionally, So return success. */
  1131. if (bd && !mc_boot_status)
  1132. return 0;
  1133. /* If DPL is deployed, set is_dpl_apply_status as TRUE. */
  1134. if (!get_dpl_apply_status())
  1135. is_dpl_apply_status = true;
  1136. /*
  1137. * For case MC is loaded but DPL is not deployed, return success and
  1138. * print message on console. Else FDT fix-up code execution hanged.
  1139. */
  1140. if (bd && mc_boot_status && !is_dpl_apply_status) {
  1141. printf("fsl-mc: DPL not deployed, DPAA2 ethernet not work\n");
  1142. return 0;
  1143. }
  1144. if (bd && mc_boot_status && is_dpl_apply_status)
  1145. return 0;
  1146. err = dpbp_exit();
  1147. if (err < 0) {
  1148. printf("dpbp_exit() failed: %d\n", err);
  1149. goto err;
  1150. }
  1151. err = dpio_exit();
  1152. if (err < 0) {
  1153. printf("dpio_exit() failed: %d\n", err);
  1154. goto err;
  1155. }
  1156. err = dpni_exit();
  1157. if (err < 0) {
  1158. printf("dpni_exit() failed: %d\n", err);
  1159. goto err;
  1160. }
  1161. err = dprc_exit();
  1162. if (err < 0) {
  1163. printf("dprc_exit() failed: %d\n", err);
  1164. goto err;
  1165. }
  1166. return 0;
  1167. err:
  1168. return err;
  1169. }
  1170. static int do_fsl_mc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1171. {
  1172. int err = 0;
  1173. if (argc < 3)
  1174. goto usage;
  1175. switch (argv[1][0]) {
  1176. case 's': {
  1177. char sub_cmd;
  1178. u64 mc_fw_addr, mc_dpc_addr;
  1179. #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
  1180. u64 aiop_fw_addr;
  1181. #endif
  1182. sub_cmd = argv[2][0];
  1183. switch (sub_cmd) {
  1184. case 'm':
  1185. if (argc < 5)
  1186. goto usage;
  1187. if (get_mc_boot_status() == 0) {
  1188. printf("fsl-mc: MC is already booted");
  1189. printf("\n");
  1190. return err;
  1191. }
  1192. mc_fw_addr = simple_strtoull(argv[3], NULL, 16);
  1193. mc_dpc_addr = simple_strtoull(argv[4], NULL,
  1194. 16);
  1195. if (!mc_init(mc_fw_addr, mc_dpc_addr))
  1196. err = mc_init_object();
  1197. break;
  1198. #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
  1199. case 'a':
  1200. if (argc < 4)
  1201. goto usage;
  1202. if (get_aiop_apply_status() == 0) {
  1203. printf("fsl-mc: AIOP FW is already");
  1204. printf(" applied\n");
  1205. return err;
  1206. }
  1207. aiop_fw_addr = simple_strtoull(argv[3], NULL,
  1208. 16);
  1209. /* if SoC doesn't have AIOP, err = -ENODEV */
  1210. err = load_mc_aiop_img(aiop_fw_addr);
  1211. if (!err)
  1212. printf("fsl-mc: AIOP FW applied\n");
  1213. break;
  1214. #endif
  1215. default:
  1216. printf("Invalid option: %s\n", argv[2]);
  1217. goto usage;
  1218. break;
  1219. }
  1220. }
  1221. break;
  1222. case 'l':
  1223. case 'a': {
  1224. u64 mc_dpl_addr;
  1225. if (argc < 4)
  1226. goto usage;
  1227. if (get_dpl_apply_status() == 0) {
  1228. printf("fsl-mc: DPL already applied\n");
  1229. return err;
  1230. }
  1231. mc_dpl_addr = simple_strtoull(argv[3], NULL,
  1232. 16);
  1233. if (get_mc_boot_status() != 0) {
  1234. printf("fsl-mc: Deploying data path layout ..");
  1235. printf("ERROR (MC is not booted)\n");
  1236. return -ENODEV;
  1237. }
  1238. if (argv[1][0] == 'l') {
  1239. /*
  1240. * We will do the actual dpaa exit and dpl apply
  1241. * later from announce_and_cleanup().
  1242. */
  1243. mc_lazy_dpl_addr = mc_dpl_addr;
  1244. } else {
  1245. /* The user wants it applied now */
  1246. if (!fsl_mc_ldpaa_exit(NULL))
  1247. err = mc_apply_dpl(mc_dpl_addr);
  1248. }
  1249. break;
  1250. }
  1251. default:
  1252. printf("Invalid option: %s\n", argv[1]);
  1253. goto usage;
  1254. break;
  1255. }
  1256. return err;
  1257. usage:
  1258. return CMD_RET_USAGE;
  1259. }
  1260. U_BOOT_CMD(
  1261. fsl_mc, CONFIG_SYS_MAXARGS, 1, do_fsl_mc,
  1262. "DPAA2 command to manage Management Complex (MC)",
  1263. "start mc [FW_addr] [DPC_addr] - Start Management Complex\n"
  1264. "fsl_mc apply DPL [DPL_addr] - Apply DPL file\n"
  1265. "fsl_mc lazyapply DPL [DPL_addr] - Apply DPL file on exit\n"
  1266. "fsl_mc start aiop [FW_addr] - Start AIOP\n"
  1267. );
  1268. void mc_env_boot(void)
  1269. {
  1270. #if defined(CONFIG_FSL_MC_ENET)
  1271. char *mc_boot_env_var;
  1272. /* The MC may only be initialized in the reset PHY function
  1273. * because otherwise U-Boot has not yet set up all the MAC
  1274. * address info properly. Without MAC addresses, the MC code
  1275. * can not properly initialize the DPC.
  1276. */
  1277. mc_boot_env_var = env_get(MC_BOOT_ENV_VAR);
  1278. if (mc_boot_env_var)
  1279. run_command_list(mc_boot_env_var, -1, 0);
  1280. #endif /* CONFIG_FSL_MC_ENET */
  1281. }