mc.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  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 *)calloc(sizeof(struct fsl_mc_io), 1);
  615. if (!root_mc_io) {
  616. printf(" No memory: calloc() 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. * As per MC design document, MC initial base address
  677. * should be least significant 512MB address of MC private
  678. * memory, i.e. address should point to end address masked
  679. * with 512MB offset in private DRAM block.
  680. */
  681. u64 mc_get_dram_addr(void)
  682. {
  683. size_t mc_ram_size = mc_get_dram_block_size();
  684. return (gd->arch.resv_ram + mc_ram_size - 1) &
  685. MC_RAM_BASE_ADDR_ALIGNMENT_MASK;
  686. }
  687. /**
  688. * Return the actual size of the MC private DRAM block.
  689. */
  690. unsigned long mc_get_dram_block_size(void)
  691. {
  692. unsigned long dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
  693. char *dram_block_size_env_var = env_get(MC_MEM_SIZE_ENV_VAR);
  694. if (dram_block_size_env_var) {
  695. dram_block_size = simple_strtoul(dram_block_size_env_var, NULL,
  696. 16);
  697. if (dram_block_size < CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE) {
  698. printf("fsl-mc: WARNING: Invalid value for \'"
  699. MC_MEM_SIZE_ENV_VAR
  700. "\' environment variable: %lu\n",
  701. dram_block_size);
  702. dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
  703. }
  704. }
  705. return dram_block_size;
  706. }
  707. int fsl_mc_ldpaa_init(bd_t *bis)
  708. {
  709. int i;
  710. for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++)
  711. if ((wriop_is_enabled_dpmac(i) == 1) &&
  712. (wriop_get_phy_address(i) != -1))
  713. ldpaa_eth_init(i, wriop_get_enet_if(i));
  714. return 0;
  715. }
  716. static int dprc_version_check(struct fsl_mc_io *mc_io, uint16_t handle)
  717. {
  718. int error;
  719. uint16_t major_ver, minor_ver;
  720. error = dprc_get_api_version(mc_io, 0,
  721. &major_ver,
  722. &minor_ver);
  723. if (error < 0) {
  724. printf("dprc_get_api_version() failed: %d\n", error);
  725. return error;
  726. }
  727. if (major_ver < DPRC_VER_MAJOR || (major_ver == DPRC_VER_MAJOR &&
  728. minor_ver < DPRC_VER_MINOR)) {
  729. printf("DPRC version mismatch found %u.%u,",
  730. major_ver, minor_ver);
  731. printf("supported version is %u.%u\n",
  732. DPRC_VER_MAJOR, DPRC_VER_MINOR);
  733. }
  734. return error;
  735. }
  736. static int dpio_init(void)
  737. {
  738. struct qbman_swp_desc p_des;
  739. struct dpio_attr attr;
  740. struct dpio_cfg dpio_cfg;
  741. int err = 0;
  742. uint16_t major_ver, minor_ver;
  743. dflt_dpio = (struct fsl_dpio_obj *)calloc(
  744. sizeof(struct fsl_dpio_obj), 1);
  745. if (!dflt_dpio) {
  746. printf("No memory: calloc() failed\n");
  747. err = -ENOMEM;
  748. goto err_calloc;
  749. }
  750. dpio_cfg.channel_mode = DPIO_LOCAL_CHANNEL;
  751. dpio_cfg.num_priorities = 8;
  752. err = dpio_create(dflt_mc_io,
  753. dflt_dprc_handle,
  754. MC_CMD_NO_FLAGS,
  755. &dpio_cfg,
  756. &dflt_dpio->dpio_id);
  757. if (err < 0) {
  758. printf("dpio_create() failed: %d\n", err);
  759. err = -ENODEV;
  760. goto err_create;
  761. }
  762. err = dpio_get_api_version(dflt_mc_io, 0,
  763. &major_ver,
  764. &minor_ver);
  765. if (err < 0) {
  766. printf("dpio_get_api_version() failed: %d\n", err);
  767. goto err_get_api_ver;
  768. }
  769. if (major_ver < DPIO_VER_MAJOR || (major_ver == DPIO_VER_MAJOR &&
  770. minor_ver < DPIO_VER_MINOR)) {
  771. printf("DPRC version mismatch found %u.%u,",
  772. major_ver,
  773. minor_ver);
  774. }
  775. err = dpio_open(dflt_mc_io,
  776. MC_CMD_NO_FLAGS,
  777. dflt_dpio->dpio_id,
  778. &dflt_dpio->dpio_handle);
  779. if (err) {
  780. printf("dpio_open() failed\n");
  781. goto err_open;
  782. }
  783. memset(&attr, 0, sizeof(struct dpio_attr));
  784. err = dpio_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
  785. dflt_dpio->dpio_handle, &attr);
  786. if (err < 0) {
  787. printf("dpio_get_attributes() failed: %d\n", err);
  788. goto err_get_attr;
  789. }
  790. if (dflt_dpio->dpio_id != attr.id) {
  791. printf("dnpi object id and attribute id are not same\n");
  792. goto err_attr_not_same;
  793. }
  794. #ifdef DEBUG
  795. printf("Init: DPIO id=0x%d\n", dflt_dpio->dpio_id);
  796. #endif
  797. err = dpio_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  798. if (err < 0) {
  799. printf("dpio_enable() failed %d\n", err);
  800. goto err_get_enable;
  801. }
  802. debug("ce_offset=0x%llx, ci_offset=0x%llx, portalid=%d, prios=%d\n",
  803. attr.qbman_portal_ce_offset,
  804. attr.qbman_portal_ci_offset,
  805. attr.qbman_portal_id,
  806. attr.num_priorities);
  807. p_des.cena_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
  808. + attr.qbman_portal_ce_offset);
  809. p_des.cinh_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
  810. + attr.qbman_portal_ci_offset);
  811. dflt_dpio->sw_portal = qbman_swp_init(&p_des);
  812. if (dflt_dpio->sw_portal == NULL) {
  813. printf("qbman_swp_init() failed\n");
  814. goto err_get_swp_init;
  815. }
  816. return 0;
  817. err_get_swp_init:
  818. dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  819. err_get_enable:
  820. err_get_attr:
  821. err_attr_not_same:
  822. dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  823. err_open:
  824. err_get_api_ver:
  825. dpio_destroy(dflt_mc_io,
  826. dflt_dprc_handle,
  827. MC_CMD_NO_FLAGS,
  828. dflt_dpio->dpio_id);
  829. err_create:
  830. free(dflt_dpio);
  831. err_calloc:
  832. return err;
  833. }
  834. static int dpio_exit(void)
  835. {
  836. int err;
  837. err = dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  838. if (err < 0) {
  839. printf("dpio_disable() failed: %d\n", err);
  840. goto err;
  841. }
  842. dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  843. if (err < 0) {
  844. printf("dpio_close() failed: %d\n", err);
  845. goto err;
  846. }
  847. err = dpio_destroy(dflt_mc_io,
  848. dflt_dprc_handle,
  849. MC_CMD_NO_FLAGS,
  850. dflt_dpio->dpio_id);
  851. if (err < 0) {
  852. printf("dpio_destroy() failed: %d\n", err);
  853. goto err;
  854. }
  855. #ifdef DEBUG
  856. printf("Exit: DPIO id=0x%d\n", dflt_dpio->dpio_id);
  857. #endif
  858. if (dflt_dpio)
  859. free(dflt_dpio);
  860. return 0;
  861. err:
  862. return err;
  863. }
  864. static int dprc_init(void)
  865. {
  866. int err, child_portal_id, container_id;
  867. struct dprc_cfg cfg;
  868. uint64_t mc_portal_offset;
  869. /* Open root container */
  870. err = dprc_get_container_id(root_mc_io, MC_CMD_NO_FLAGS, &container_id);
  871. if (err < 0) {
  872. printf("dprc_get_container_id(): Root failed: %d\n", err);
  873. goto err_root_container_id;
  874. }
  875. #ifdef DEBUG
  876. printf("Root container id = %d\n", container_id);
  877. #endif
  878. err = dprc_open(root_mc_io, MC_CMD_NO_FLAGS, container_id,
  879. &root_dprc_handle);
  880. if (err < 0) {
  881. printf("dprc_open(): Root Container failed: %d\n", err);
  882. goto err_root_open;
  883. }
  884. if (!root_dprc_handle) {
  885. printf("dprc_open(): Root Container Handle is not valid\n");
  886. goto err_root_open;
  887. }
  888. err = dprc_version_check(root_mc_io, root_dprc_handle);
  889. if (err < 0) {
  890. printf("dprc_version_check() failed: %d\n", err);
  891. goto err_root_open;
  892. }
  893. memset(&cfg, 0, sizeof(struct dprc_cfg));
  894. cfg.options = DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED |
  895. DPRC_CFG_OPT_OBJ_CREATE_ALLOWED |
  896. DPRC_CFG_OPT_ALLOC_ALLOWED;
  897. cfg.icid = DPRC_GET_ICID_FROM_POOL;
  898. cfg.portal_id = DPRC_GET_PORTAL_ID_FROM_POOL;
  899. err = dprc_create_container(root_mc_io, MC_CMD_NO_FLAGS,
  900. root_dprc_handle,
  901. &cfg,
  902. &child_dprc_id,
  903. &mc_portal_offset);
  904. if (err < 0) {
  905. printf("dprc_create_container() failed: %d\n", err);
  906. goto err_create;
  907. }
  908. dflt_mc_io = (struct fsl_mc_io *)calloc(sizeof(struct fsl_mc_io), 1);
  909. if (!dflt_mc_io) {
  910. err = -ENOMEM;
  911. printf(" No memory: calloc() failed\n");
  912. goto err_calloc;
  913. }
  914. child_portal_id = MC_PORTAL_OFFSET_TO_PORTAL_ID(mc_portal_offset);
  915. dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(child_portal_id);
  916. #ifdef DEBUG
  917. printf("MC portal of child DPRC container: %d, physical addr %p)\n",
  918. child_dprc_id, dflt_mc_io->mmio_regs);
  919. #endif
  920. err = dprc_open(dflt_mc_io, MC_CMD_NO_FLAGS, child_dprc_id,
  921. &dflt_dprc_handle);
  922. if (err < 0) {
  923. printf("dprc_open(): Child container failed: %d\n", err);
  924. goto err_child_open;
  925. }
  926. if (!dflt_dprc_handle) {
  927. printf("dprc_open(): Child container Handle is not valid\n");
  928. goto err_child_open;
  929. }
  930. return 0;
  931. err_child_open:
  932. free(dflt_mc_io);
  933. err_calloc:
  934. dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
  935. root_dprc_handle, child_dprc_id);
  936. err_create:
  937. dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
  938. err_root_open:
  939. err_root_container_id:
  940. return err;
  941. }
  942. static int dprc_exit(void)
  943. {
  944. int err;
  945. err = dprc_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dprc_handle);
  946. if (err < 0) {
  947. printf("dprc_close(): Child failed: %d\n", err);
  948. goto err;
  949. }
  950. err = dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
  951. root_dprc_handle, child_dprc_id);
  952. if (err < 0) {
  953. printf("dprc_destroy_container() failed: %d\n", err);
  954. goto err;
  955. }
  956. err = dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
  957. if (err < 0) {
  958. printf("dprc_close(): Root failed: %d\n", err);
  959. goto err;
  960. }
  961. if (dflt_mc_io)
  962. free(dflt_mc_io);
  963. if (root_mc_io)
  964. free(root_mc_io);
  965. return 0;
  966. err:
  967. return err;
  968. }
  969. static int dpbp_init(void)
  970. {
  971. int err;
  972. struct dpbp_attr dpbp_attr;
  973. struct dpbp_cfg dpbp_cfg;
  974. uint16_t major_ver, minor_ver;
  975. dflt_dpbp = (struct fsl_dpbp_obj *)calloc(
  976. sizeof(struct fsl_dpbp_obj), 1);
  977. if (!dflt_dpbp) {
  978. printf("No memory: calloc() failed\n");
  979. err = -ENOMEM;
  980. goto err_calloc;
  981. }
  982. dpbp_cfg.options = 512;
  983. err = dpbp_create(dflt_mc_io,
  984. dflt_dprc_handle,
  985. MC_CMD_NO_FLAGS,
  986. &dpbp_cfg,
  987. &dflt_dpbp->dpbp_id);
  988. if (err < 0) {
  989. err = -ENODEV;
  990. printf("dpbp_create() failed: %d\n", err);
  991. goto err_create;
  992. }
  993. err = dpbp_get_api_version(dflt_mc_io, 0,
  994. &major_ver,
  995. &minor_ver);
  996. if (err < 0) {
  997. printf("dpbp_get_api_version() failed: %d\n", err);
  998. goto err_get_api_ver;
  999. }
  1000. if (major_ver < DPBP_VER_MAJOR || (major_ver == DPBP_VER_MAJOR &&
  1001. minor_ver < DPBP_VER_MINOR)) {
  1002. printf("DPBP version mismatch found %u.%u,",
  1003. major_ver, minor_ver);
  1004. printf("supported version is %u.%u\n",
  1005. DPBP_VER_MAJOR, DPBP_VER_MINOR);
  1006. }
  1007. err = dpbp_open(dflt_mc_io,
  1008. MC_CMD_NO_FLAGS,
  1009. dflt_dpbp->dpbp_id,
  1010. &dflt_dpbp->dpbp_handle);
  1011. if (err) {
  1012. printf("dpbp_open() failed\n");
  1013. goto err_open;
  1014. }
  1015. memset(&dpbp_attr, 0, sizeof(struct dpbp_attr));
  1016. err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
  1017. dflt_dpbp->dpbp_handle,
  1018. &dpbp_attr);
  1019. if (err < 0) {
  1020. printf("dpbp_get_attributes() failed: %d\n", err);
  1021. goto err_get_attr;
  1022. }
  1023. if (dflt_dpbp->dpbp_id != dpbp_attr.id) {
  1024. printf("dpbp object id and attribute id are not same\n");
  1025. goto err_attr_not_same;
  1026. }
  1027. #ifdef DEBUG
  1028. printf("Init: DPBP id=0x%x\n", dflt_dpbp->dpbp_attr.id);
  1029. #endif
  1030. err = dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
  1031. if (err < 0) {
  1032. printf("dpbp_close() failed: %d\n", err);
  1033. goto err_close;
  1034. }
  1035. return 0;
  1036. err_get_attr:
  1037. err_attr_not_same:
  1038. dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
  1039. dpbp_destroy(dflt_mc_io,
  1040. dflt_dprc_handle,
  1041. MC_CMD_NO_FLAGS,
  1042. dflt_dpbp->dpbp_id);
  1043. err_get_api_ver:
  1044. err_close:
  1045. err_open:
  1046. err_create:
  1047. free(dflt_dpbp);
  1048. err_calloc:
  1049. return err;
  1050. }
  1051. static int dpbp_exit(void)
  1052. {
  1053. int err;
  1054. err = dpbp_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
  1055. dflt_dpbp->dpbp_id);
  1056. if (err < 0) {
  1057. printf("dpbp_destroy() failed: %d\n", err);
  1058. goto err;
  1059. }
  1060. #ifdef DEBUG
  1061. printf("Exit: DPBP id=0x%d\n", dflt_dpbp->dpbp_attr.id);
  1062. #endif
  1063. if (dflt_dpbp)
  1064. free(dflt_dpbp);
  1065. return 0;
  1066. err:
  1067. return err;
  1068. }
  1069. static int dpni_init(void)
  1070. {
  1071. int err;
  1072. uint8_t cfg_buf[256] = {0};
  1073. struct dpni_cfg dpni_cfg;
  1074. uint16_t major_ver, minor_ver;
  1075. dflt_dpni = (struct fsl_dpni_obj *)calloc(
  1076. sizeof(struct fsl_dpni_obj), 1);
  1077. if (!dflt_dpni) {
  1078. printf("No memory: calloc() failed\n");
  1079. err = -ENOMEM;
  1080. goto err_calloc;
  1081. }
  1082. memset(&dpni_cfg, 0, sizeof(dpni_cfg));
  1083. err = dpni_prepare_cfg(&dpni_cfg, &cfg_buf[0]);
  1084. if (err < 0) {
  1085. err = -ENODEV;
  1086. printf("dpni_prepare_cfg() failed: %d\n", err);
  1087. goto err_prepare_cfg;
  1088. }
  1089. err = dpni_create(dflt_mc_io,
  1090. dflt_dprc_handle,
  1091. MC_CMD_NO_FLAGS,
  1092. &dpni_cfg,
  1093. &dflt_dpni->dpni_id);
  1094. if (err < 0) {
  1095. err = -ENODEV;
  1096. printf("dpni create() failed: %d\n", err);
  1097. goto err_create;
  1098. }
  1099. err = dpni_get_api_version(dflt_mc_io, 0,
  1100. &major_ver,
  1101. &minor_ver);
  1102. if (err < 0) {
  1103. printf("dpni_get_api_version() failed: %d\n", err);
  1104. goto err_get_version;
  1105. }
  1106. if (major_ver < DPNI_VER_MAJOR || (major_ver == DPNI_VER_MAJOR &&
  1107. minor_ver < DPNI_VER_MINOR)) {
  1108. printf("DPNI version mismatch found %u.%u,",
  1109. major_ver, minor_ver);
  1110. printf("supported version is %u.%u\n",
  1111. DPNI_VER_MAJOR, DPNI_VER_MINOR);
  1112. }
  1113. err = dpni_open(dflt_mc_io,
  1114. MC_CMD_NO_FLAGS,
  1115. dflt_dpni->dpni_id,
  1116. &dflt_dpni->dpni_handle);
  1117. if (err) {
  1118. printf("dpni_open() failed\n");
  1119. goto err_open;
  1120. }
  1121. #ifdef DEBUG
  1122. printf("Init: DPNI id=0x%d\n", dflt_dpni->dpni_id);
  1123. #endif
  1124. err = dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
  1125. if (err < 0) {
  1126. printf("dpni_close() failed: %d\n", err);
  1127. goto err_close;
  1128. }
  1129. return 0;
  1130. err_close:
  1131. dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
  1132. err_open:
  1133. err_get_version:
  1134. dpni_destroy(dflt_mc_io,
  1135. dflt_dprc_handle,
  1136. MC_CMD_NO_FLAGS,
  1137. dflt_dpni->dpni_id);
  1138. err_create:
  1139. err_prepare_cfg:
  1140. free(dflt_dpni);
  1141. err_calloc:
  1142. return err;
  1143. }
  1144. static int dpni_exit(void)
  1145. {
  1146. int err;
  1147. err = dpni_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
  1148. dflt_dpni->dpni_id);
  1149. if (err < 0) {
  1150. printf("dpni_destroy() failed: %d\n", err);
  1151. goto err;
  1152. }
  1153. #ifdef DEBUG
  1154. printf("Exit: DPNI id=0x%d\n", dflt_dpni->dpni_id);
  1155. #endif
  1156. if (dflt_dpni)
  1157. free(dflt_dpni);
  1158. return 0;
  1159. err:
  1160. return err;
  1161. }
  1162. static int mc_init_object(void)
  1163. {
  1164. int err = 0;
  1165. err = dprc_init();
  1166. if (err < 0) {
  1167. printf("dprc_init() failed: %d\n", err);
  1168. goto err;
  1169. }
  1170. err = dpbp_init();
  1171. if (err < 0) {
  1172. printf("dpbp_init() failed: %d\n", err);
  1173. goto err;
  1174. }
  1175. err = dpio_init();
  1176. if (err < 0) {
  1177. printf("dpio_init() failed: %d\n", err);
  1178. goto err;
  1179. }
  1180. err = dpni_init();
  1181. if (err < 0) {
  1182. printf("dpni_init() failed: %d\n", err);
  1183. goto err;
  1184. }
  1185. return 0;
  1186. err:
  1187. return err;
  1188. }
  1189. int fsl_mc_ldpaa_exit(bd_t *bd)
  1190. {
  1191. int err = 0;
  1192. bool is_dpl_apply_status = false;
  1193. bool mc_boot_status = false;
  1194. if (bd && mc_lazy_dpl_addr && !fsl_mc_ldpaa_exit(NULL)) {
  1195. mc_apply_dpl(mc_lazy_dpl_addr);
  1196. mc_lazy_dpl_addr = 0;
  1197. }
  1198. if (!get_mc_boot_status())
  1199. mc_boot_status = true;
  1200. /* MC is not loaded intentionally, So return success. */
  1201. if (bd && !mc_boot_status)
  1202. return 0;
  1203. /* If DPL is deployed, set is_dpl_apply_status as TRUE. */
  1204. if (!get_dpl_apply_status())
  1205. is_dpl_apply_status = true;
  1206. /*
  1207. * For case MC is loaded but DPL is not deployed, return success and
  1208. * print message on console. Else FDT fix-up code execution hanged.
  1209. */
  1210. if (bd && mc_boot_status && !is_dpl_apply_status) {
  1211. printf("fsl-mc: DPL not deployed, DPAA2 ethernet not work\n");
  1212. goto mc_obj_cleanup;
  1213. }
  1214. if (bd && mc_boot_status && is_dpl_apply_status)
  1215. return 0;
  1216. mc_obj_cleanup:
  1217. err = dpbp_exit();
  1218. if (err < 0) {
  1219. printf("dpbp_exit() failed: %d\n", err);
  1220. goto err;
  1221. }
  1222. err = dpio_exit();
  1223. if (err < 0) {
  1224. printf("dpio_exit() failed: %d\n", err);
  1225. goto err;
  1226. }
  1227. err = dpni_exit();
  1228. if (err < 0) {
  1229. printf("dpni_exit() failed: %d\n", err);
  1230. goto err;
  1231. }
  1232. err = dprc_exit();
  1233. if (err < 0) {
  1234. printf("dprc_exit() failed: %d\n", err);
  1235. goto err;
  1236. }
  1237. return 0;
  1238. err:
  1239. return err;
  1240. }
  1241. static int do_fsl_mc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1242. {
  1243. int err = 0;
  1244. if (argc < 3)
  1245. goto usage;
  1246. switch (argv[1][0]) {
  1247. case 's': {
  1248. char sub_cmd;
  1249. u64 mc_fw_addr, mc_dpc_addr;
  1250. #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
  1251. u64 aiop_fw_addr;
  1252. #endif
  1253. sub_cmd = argv[2][0];
  1254. switch (sub_cmd) {
  1255. case 'm':
  1256. if (argc < 5)
  1257. goto usage;
  1258. if (get_mc_boot_status() == 0) {
  1259. printf("fsl-mc: MC is already booted");
  1260. printf("\n");
  1261. return err;
  1262. }
  1263. mc_fw_addr = simple_strtoull(argv[3], NULL, 16);
  1264. mc_dpc_addr = simple_strtoull(argv[4], NULL,
  1265. 16);
  1266. if (!mc_init(mc_fw_addr, mc_dpc_addr))
  1267. err = mc_init_object();
  1268. break;
  1269. #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
  1270. case 'a':
  1271. if (argc < 4)
  1272. goto usage;
  1273. if (get_aiop_apply_status() == 0) {
  1274. printf("fsl-mc: AIOP FW is already");
  1275. printf(" applied\n");
  1276. return err;
  1277. }
  1278. aiop_fw_addr = simple_strtoull(argv[3], NULL,
  1279. 16);
  1280. /* if SoC doesn't have AIOP, err = -ENODEV */
  1281. err = load_mc_aiop_img(aiop_fw_addr);
  1282. if (!err)
  1283. printf("fsl-mc: AIOP FW applied\n");
  1284. break;
  1285. #endif
  1286. default:
  1287. printf("Invalid option: %s\n", argv[2]);
  1288. goto usage;
  1289. break;
  1290. }
  1291. }
  1292. break;
  1293. case 'l':
  1294. case 'a': {
  1295. u64 mc_dpl_addr;
  1296. if (argc < 4)
  1297. goto usage;
  1298. if (get_dpl_apply_status() == 0) {
  1299. printf("fsl-mc: DPL already applied\n");
  1300. return err;
  1301. }
  1302. mc_dpl_addr = simple_strtoull(argv[3], NULL,
  1303. 16);
  1304. if (get_mc_boot_status() != 0) {
  1305. printf("fsl-mc: Deploying data path layout ..");
  1306. printf("ERROR (MC is not booted)\n");
  1307. return -ENODEV;
  1308. }
  1309. if (argv[1][0] == 'l') {
  1310. /*
  1311. * We will do the actual dpaa exit and dpl apply
  1312. * later from announce_and_cleanup().
  1313. */
  1314. mc_lazy_dpl_addr = mc_dpl_addr;
  1315. } else {
  1316. /* The user wants it applied now */
  1317. if (!fsl_mc_ldpaa_exit(NULL))
  1318. err = mc_apply_dpl(mc_dpl_addr);
  1319. }
  1320. break;
  1321. }
  1322. default:
  1323. printf("Invalid option: %s\n", argv[1]);
  1324. goto usage;
  1325. break;
  1326. }
  1327. return err;
  1328. usage:
  1329. return CMD_RET_USAGE;
  1330. }
  1331. U_BOOT_CMD(
  1332. fsl_mc, CONFIG_SYS_MAXARGS, 1, do_fsl_mc,
  1333. "DPAA2 command to manage Management Complex (MC)",
  1334. "start mc [FW_addr] [DPC_addr] - Start Management Complex\n"
  1335. "fsl_mc apply DPL [DPL_addr] - Apply DPL file\n"
  1336. "fsl_mc lazyapply DPL [DPL_addr] - Apply DPL file on exit\n"
  1337. "fsl_mc start aiop [FW_addr] - Start AIOP\n"
  1338. );
  1339. void mc_env_boot(void)
  1340. {
  1341. #if defined(CONFIG_FSL_MC_ENET)
  1342. char *mc_boot_env_var;
  1343. /* The MC may only be initialized in the reset PHY function
  1344. * because otherwise U-Boot has not yet set up all the MAC
  1345. * address info properly. Without MAC addresses, the MC code
  1346. * can not properly initialize the DPC.
  1347. */
  1348. mc_boot_env_var = env_get(MC_BOOT_ENV_VAR);
  1349. if (mc_boot_env_var)
  1350. run_command_list(mc_boot_env_var, -1, 0);
  1351. #endif /* CONFIG_FSL_MC_ENET */
  1352. }