mc.c 33 KB

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