mc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /*
  2. * Copyright (C) 2014 Freescale Semiconductor
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <errno.h>
  7. #include <asm/io.h>
  8. #include <fsl-mc/fsl_mc.h>
  9. #include <fsl-mc/fsl_mc_sys.h>
  10. #include <fsl-mc/fsl_mc_private.h>
  11. #include <fsl-mc/fsl_dpmng.h>
  12. #include <fsl-mc/fsl_dprc.h>
  13. #include <fsl-mc/fsl_dpio.h>
  14. #include <fsl-mc/fsl_qbman_portal.h>
  15. #define MC_RAM_BASE_ADDR_ALIGNMENT (512UL * 1024 * 1024)
  16. #define MC_RAM_BASE_ADDR_ALIGNMENT_MASK (~(MC_RAM_BASE_ADDR_ALIGNMENT - 1))
  17. #define MC_RAM_SIZE_ALIGNMENT (256UL * 1024 * 1024)
  18. #define MC_MEM_SIZE_ENV_VAR "mcmemsize"
  19. #define MC_BOOT_TIMEOUT_ENV_VAR "mcboottimeout"
  20. DECLARE_GLOBAL_DATA_PTR;
  21. static int mc_boot_status;
  22. struct fsl_mc_io *dflt_mc_io = NULL;
  23. uint16_t dflt_dprc_handle = 0;
  24. struct fsl_dpbp_obj *dflt_dpbp = NULL;
  25. struct fsl_dpio_obj *dflt_dpio = NULL;
  26. uint16_t dflt_dpio_handle = 0;
  27. #ifdef DEBUG
  28. void dump_ram_words(const char *title, void *addr)
  29. {
  30. int i;
  31. uint32_t *words = addr;
  32. printf("Dumping beginning of %s (%p):\n", title, addr);
  33. for (i = 0; i < 16; i++)
  34. printf("%#x ", words[i]);
  35. printf("\n");
  36. }
  37. void dump_mc_ccsr_regs(struct mc_ccsr_registers __iomem *mc_ccsr_regs)
  38. {
  39. printf("MC CCSR registers:\n"
  40. "reg_gcr1 %#x\n"
  41. "reg_gsr %#x\n"
  42. "reg_sicbalr %#x\n"
  43. "reg_sicbahr %#x\n"
  44. "reg_sicapr %#x\n"
  45. "reg_mcfbalr %#x\n"
  46. "reg_mcfbahr %#x\n"
  47. "reg_mcfapr %#x\n"
  48. "reg_psr %#x\n",
  49. mc_ccsr_regs->reg_gcr1,
  50. mc_ccsr_regs->reg_gsr,
  51. mc_ccsr_regs->reg_sicbalr,
  52. mc_ccsr_regs->reg_sicbahr,
  53. mc_ccsr_regs->reg_sicapr,
  54. mc_ccsr_regs->reg_mcfbalr,
  55. mc_ccsr_regs->reg_mcfbahr,
  56. mc_ccsr_regs->reg_mcfapr,
  57. mc_ccsr_regs->reg_psr);
  58. }
  59. #else
  60. #define dump_ram_words(title, addr)
  61. #define dump_mc_ccsr_regs(mc_ccsr_regs)
  62. #endif /* DEBUG */
  63. #ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
  64. /**
  65. * Copying MC firmware or DPL image to DDR
  66. */
  67. static int mc_copy_image(const char *title,
  68. u64 image_addr, u32 image_size, u64 mc_ram_addr)
  69. {
  70. debug("%s copied to address %p\n", title, (void *)mc_ram_addr);
  71. memcpy((void *)mc_ram_addr, (void *)image_addr, image_size);
  72. flush_dcache_range(mc_ram_addr, mc_ram_addr + image_size);
  73. return 0;
  74. }
  75. /**
  76. * MC firmware FIT image parser checks if the image is in FIT
  77. * format, verifies integrity of the image and calculates
  78. * raw image address and size values.
  79. * Returns 0 on success and a negative errno on error.
  80. * task fail.
  81. **/
  82. int parse_mc_firmware_fit_image(const void **raw_image_addr,
  83. size_t *raw_image_size)
  84. {
  85. int format;
  86. void *fit_hdr;
  87. int node_offset;
  88. const void *data;
  89. size_t size;
  90. const char *uname = "firmware";
  91. /* Check if the image is in NOR flash */
  92. #ifdef CONFIG_SYS_LS_MC_FW_IN_NOR
  93. fit_hdr = (void *)CONFIG_SYS_LS_MC_FW_ADDR;
  94. #else
  95. #error "No CONFIG_SYS_LS_MC_FW_IN_xxx defined"
  96. #endif
  97. /* Check if Image is in FIT format */
  98. format = genimg_get_format(fit_hdr);
  99. if (format != IMAGE_FORMAT_FIT) {
  100. printf("fsl-mc: ERROR: Bad firmware image (not a FIT image)\n");
  101. return -EINVAL;
  102. }
  103. if (!fit_check_format(fit_hdr)) {
  104. printf("fsl-mc: ERROR: Bad firmware image (bad FIT header)\n");
  105. return -EINVAL;
  106. }
  107. node_offset = fit_image_get_node(fit_hdr, uname);
  108. if (node_offset < 0) {
  109. printf("fsl-mc: ERROR: Bad firmware image (missing subimage)\n");
  110. return -ENOENT;
  111. }
  112. /* Verify MC firmware image */
  113. if (!(fit_image_verify(fit_hdr, node_offset))) {
  114. printf("fsl-mc: ERROR: Bad firmware image (bad CRC)\n");
  115. return -EINVAL;
  116. }
  117. /* Get address and size of raw image */
  118. fit_image_get_data(fit_hdr, node_offset, &data, &size);
  119. *raw_image_addr = data;
  120. *raw_image_size = size;
  121. return 0;
  122. }
  123. #endif
  124. /*
  125. * Calculates the values to be used to specify the address range
  126. * for the MC private DRAM block, in the MCFBALR/MCFBAHR registers.
  127. * It returns the highest 512MB-aligned address within the given
  128. * address range, in '*aligned_base_addr', and the number of 256 MiB
  129. * blocks in it, in 'num_256mb_blocks'.
  130. */
  131. static int calculate_mc_private_ram_params(u64 mc_private_ram_start_addr,
  132. size_t mc_ram_size,
  133. u64 *aligned_base_addr,
  134. u8 *num_256mb_blocks)
  135. {
  136. u64 addr;
  137. u16 num_blocks;
  138. if (mc_ram_size % MC_RAM_SIZE_ALIGNMENT != 0) {
  139. printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
  140. mc_ram_size);
  141. return -EINVAL;
  142. }
  143. num_blocks = mc_ram_size / MC_RAM_SIZE_ALIGNMENT;
  144. if (num_blocks < 1 || num_blocks > 0xff) {
  145. printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
  146. mc_ram_size);
  147. return -EINVAL;
  148. }
  149. addr = (mc_private_ram_start_addr + mc_ram_size - 1) &
  150. MC_RAM_BASE_ADDR_ALIGNMENT_MASK;
  151. if (addr < mc_private_ram_start_addr) {
  152. printf("fsl-mc: ERROR: bad start address %#llx\n",
  153. mc_private_ram_start_addr);
  154. return -EFAULT;
  155. }
  156. *aligned_base_addr = addr;
  157. *num_256mb_blocks = num_blocks;
  158. return 0;
  159. }
  160. static int load_mc_dpc(u64 mc_ram_addr, size_t mc_ram_size)
  161. {
  162. u64 mc_dpc_offset;
  163. #ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
  164. int error;
  165. void *dpc_fdt_hdr;
  166. int dpc_size;
  167. #endif
  168. #ifdef CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET
  169. BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET & 0x3) != 0 ||
  170. CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET > 0xffffffff);
  171. mc_dpc_offset = CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET;
  172. #else
  173. #error "CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET not defined"
  174. #endif
  175. /*
  176. * Load the MC DPC blob in the MC private DRAM block:
  177. */
  178. #ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
  179. printf("MC DPC is preloaded to %#llx\n", mc_ram_addr + mc_dpc_offset);
  180. #else
  181. /*
  182. * Get address and size of the DPC blob stored in flash:
  183. */
  184. #ifdef CONFIG_SYS_LS_MC_DPC_IN_NOR
  185. dpc_fdt_hdr = (void *)CONFIG_SYS_LS_MC_DPC_ADDR;
  186. #else
  187. #error "No CONFIG_SYS_LS_MC_DPC_IN_xxx defined"
  188. #endif
  189. error = fdt_check_header(dpc_fdt_hdr);
  190. if (error != 0) {
  191. /*
  192. * Don't return with error here, since the MC firmware can
  193. * still boot without a DPC
  194. */
  195. printf("fsl-mc: WARNING: No DPC image found\n");
  196. return 0;
  197. }
  198. dpc_size = fdt_totalsize(dpc_fdt_hdr);
  199. if (dpc_size > CONFIG_SYS_LS_MC_DPC_MAX_LENGTH) {
  200. printf("fsl-mc: ERROR: Bad DPC image (too large: %d)\n",
  201. dpc_size);
  202. return -EINVAL;
  203. }
  204. mc_copy_image("MC DPC blob",
  205. (u64)dpc_fdt_hdr, dpc_size, mc_ram_addr + mc_dpc_offset);
  206. #endif /* not defined CONFIG_SYS_LS_MC_DPC_IN_DDR */
  207. dump_ram_words("DPC", (void *)(mc_ram_addr + mc_dpc_offset));
  208. return 0;
  209. }
  210. static int load_mc_dpl(u64 mc_ram_addr, size_t mc_ram_size)
  211. {
  212. u64 mc_dpl_offset;
  213. #ifndef CONFIG_SYS_LS_MC_DPL_IN_DDR
  214. int error;
  215. void *dpl_fdt_hdr;
  216. int dpl_size;
  217. #endif
  218. #ifdef CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET
  219. BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET & 0x3) != 0 ||
  220. CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET > 0xffffffff);
  221. mc_dpl_offset = CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET;
  222. #else
  223. #error "CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET not defined"
  224. #endif
  225. /*
  226. * Load the MC DPL blob in the MC private DRAM block:
  227. */
  228. #ifdef CONFIG_SYS_LS_MC_DPL_IN_DDR
  229. printf("MC DPL is preloaded to %#llx\n", mc_ram_addr + mc_dpl_offset);
  230. #else
  231. /*
  232. * Get address and size of the DPL blob stored in flash:
  233. */
  234. #ifdef CONFIG_SYS_LS_MC_DPL_IN_NOR
  235. dpl_fdt_hdr = (void *)CONFIG_SYS_LS_MC_DPL_ADDR;
  236. #else
  237. #error "No CONFIG_SYS_LS_MC_DPL_IN_xxx defined"
  238. #endif
  239. error = fdt_check_header(dpl_fdt_hdr);
  240. if (error != 0) {
  241. printf("fsl-mc: ERROR: Bad DPL image (bad header)\n");
  242. return error;
  243. }
  244. dpl_size = fdt_totalsize(dpl_fdt_hdr);
  245. if (dpl_size > CONFIG_SYS_LS_MC_DPL_MAX_LENGTH) {
  246. printf("fsl-mc: ERROR: Bad DPL image (too large: %d)\n",
  247. dpl_size);
  248. return -EINVAL;
  249. }
  250. mc_copy_image("MC DPL blob",
  251. (u64)dpl_fdt_hdr, dpl_size, mc_ram_addr + mc_dpl_offset);
  252. #endif /* not defined CONFIG_SYS_LS_MC_DPL_IN_DDR */
  253. dump_ram_words("DPL", (void *)(mc_ram_addr + mc_dpl_offset));
  254. return 0;
  255. }
  256. /**
  257. * Return the MC boot timeout value in milliseconds
  258. */
  259. static unsigned long get_mc_boot_timeout_ms(void)
  260. {
  261. unsigned long timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
  262. char *timeout_ms_env_var = getenv(MC_BOOT_TIMEOUT_ENV_VAR);
  263. if (timeout_ms_env_var) {
  264. timeout_ms = simple_strtoul(timeout_ms_env_var, NULL, 10);
  265. if (timeout_ms == 0) {
  266. printf("fsl-mc: WARNING: Invalid value for \'"
  267. MC_BOOT_TIMEOUT_ENV_VAR
  268. "\' environment variable: %lu\n",
  269. timeout_ms);
  270. timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
  271. }
  272. }
  273. return timeout_ms;
  274. }
  275. static int wait_for_mc(bool booting_mc, u32 *final_reg_gsr)
  276. {
  277. u32 reg_gsr;
  278. u32 mc_fw_boot_status;
  279. unsigned long timeout_ms = get_mc_boot_timeout_ms();
  280. struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
  281. dmb();
  282. debug("Polling mc_ccsr_regs->reg_gsr ...\n");
  283. assert(timeout_ms > 0);
  284. for (;;) {
  285. udelay(1000); /* throttle polling */
  286. reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
  287. mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
  288. if (mc_fw_boot_status & 0x1)
  289. break;
  290. timeout_ms--;
  291. if (timeout_ms == 0)
  292. break;
  293. }
  294. if (timeout_ms == 0) {
  295. if (booting_mc)
  296. printf("fsl-mc: timeout booting management complex firmware\n");
  297. else
  298. printf("fsl-mc: timeout deploying data path layout\n");
  299. /* TODO: Get an error status from an MC CCSR register */
  300. return -ETIMEDOUT;
  301. }
  302. if (mc_fw_boot_status != 0x1) {
  303. /*
  304. * TODO: Identify critical errors from the GSR register's FS
  305. * field and for those errors, set error to -ENODEV or other
  306. * appropriate errno, so that the status property is set to
  307. * failure in the fsl,dprc device tree node.
  308. */
  309. if (booting_mc) {
  310. printf("fsl-mc: WARNING: Firmware booted with error (GSR: %#x)\n",
  311. reg_gsr);
  312. } else {
  313. printf("fsl-mc: WARNING: Data path layout deployed with error (GSR: %#x)\n",
  314. reg_gsr);
  315. }
  316. }
  317. *final_reg_gsr = reg_gsr;
  318. return 0;
  319. }
  320. int mc_init(void)
  321. {
  322. int error = 0;
  323. int portal_id = 0;
  324. struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
  325. u64 mc_ram_addr;
  326. u32 reg_gsr;
  327. u32 reg_mcfbalr;
  328. #ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
  329. const void *raw_image_addr;
  330. size_t raw_image_size = 0;
  331. #endif
  332. struct mc_version mc_ver_info;
  333. u64 mc_ram_aligned_base_addr;
  334. u8 mc_ram_num_256mb_blocks;
  335. size_t mc_ram_size = mc_get_dram_block_size();
  336. /*
  337. * The MC private DRAM block was already carved at the end of DRAM
  338. * by board_init_f() using CONFIG_SYS_MEM_TOP_HIDE:
  339. */
  340. if (gd->bd->bi_dram[1].start) {
  341. mc_ram_addr =
  342. gd->bd->bi_dram[1].start + gd->bd->bi_dram[1].size;
  343. } else {
  344. mc_ram_addr =
  345. gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size;
  346. }
  347. error = calculate_mc_private_ram_params(mc_ram_addr,
  348. mc_ram_size,
  349. &mc_ram_aligned_base_addr,
  350. &mc_ram_num_256mb_blocks);
  351. if (error != 0)
  352. goto out;
  353. /*
  354. * Management Complex cores should be held at reset out of POR.
  355. * U-boot should be the first software to touch MC. To be safe,
  356. * we reset all cores again by setting GCR1 to 0. It doesn't do
  357. * anything if they are held at reset. After we setup the firmware
  358. * we kick off MC by deasserting the reset bit for core 0, and
  359. * deasserting the reset bits for Command Portal Managers.
  360. * The stop bits are not touched here. They are used to stop the
  361. * cores when they are active. Setting stop bits doesn't stop the
  362. * cores from fetching instructions when they are released from
  363. * reset.
  364. */
  365. out_le32(&mc_ccsr_regs->reg_gcr1, 0);
  366. dmb();
  367. #ifdef CONFIG_SYS_LS_MC_FW_IN_DDR
  368. printf("MC firmware is preloaded to %#llx\n", mc_ram_addr);
  369. #else
  370. error = parse_mc_firmware_fit_image(&raw_image_addr, &raw_image_size);
  371. if (error != 0)
  372. goto out;
  373. /*
  374. * Load the MC FW at the beginning of the MC private DRAM block:
  375. */
  376. mc_copy_image("MC Firmware",
  377. (u64)raw_image_addr, raw_image_size, mc_ram_addr);
  378. #endif
  379. dump_ram_words("firmware", (void *)mc_ram_addr);
  380. error = load_mc_dpc(mc_ram_addr, mc_ram_size);
  381. if (error != 0)
  382. goto out;
  383. error = load_mc_dpl(mc_ram_addr, mc_ram_size);
  384. if (error != 0)
  385. goto out;
  386. debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
  387. dump_mc_ccsr_regs(mc_ccsr_regs);
  388. /*
  389. * Tell MC what is the address range of the DRAM block assigned to it:
  390. */
  391. reg_mcfbalr = (u32)mc_ram_aligned_base_addr |
  392. (mc_ram_num_256mb_blocks - 1);
  393. out_le32(&mc_ccsr_regs->reg_mcfbalr, reg_mcfbalr);
  394. out_le32(&mc_ccsr_regs->reg_mcfbahr,
  395. (u32)(mc_ram_aligned_base_addr >> 32));
  396. out_le32(&mc_ccsr_regs->reg_mcfapr, MCFAPR_BYPASS_ICID_MASK);
  397. /*
  398. * Tell the MC that we want delayed DPL deployment.
  399. */
  400. out_le32(&mc_ccsr_regs->reg_gsr, 0xDD00);
  401. printf("\nfsl-mc: Booting Management Complex ...\n");
  402. /*
  403. * Deassert reset and release MC core 0 to run
  404. */
  405. out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
  406. error = wait_for_mc(true, &reg_gsr);
  407. if (error != 0)
  408. goto out;
  409. /*
  410. * TODO: need to obtain the portal_id for the root container from the
  411. * DPL
  412. */
  413. portal_id = 0;
  414. /*
  415. * Initialize the global default MC portal
  416. * And check that the MC firmware is responding portal commands:
  417. */
  418. dflt_mc_io = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
  419. if (!dflt_mc_io) {
  420. printf(" No memory: malloc() failed\n");
  421. return -ENOMEM;
  422. }
  423. dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(portal_id);
  424. debug("Checking access to MC portal of root DPRC container (portal_id %d, portal physical addr %p)\n",
  425. portal_id, dflt_mc_io->mmio_regs);
  426. error = mc_get_version(dflt_mc_io, &mc_ver_info);
  427. if (error != 0) {
  428. printf("fsl-mc: ERROR: Firmware version check failed (error: %d)\n",
  429. error);
  430. goto out;
  431. }
  432. if (MC_VER_MAJOR != mc_ver_info.major)
  433. printf("fsl-mc: ERROR: Firmware major version mismatch (found: %d, expected: %d)\n",
  434. mc_ver_info.major, MC_VER_MAJOR);
  435. if (MC_VER_MINOR != mc_ver_info.minor)
  436. printf("fsl-mc: WARNING: Firmware minor version mismatch (found: %d, expected: %d)\n",
  437. mc_ver_info.minor, MC_VER_MINOR);
  438. printf("fsl-mc: Management Complex booted (version: %d.%d.%d, boot status: %#x)\n",
  439. mc_ver_info.major, mc_ver_info.minor, mc_ver_info.revision,
  440. reg_gsr & GSR_FS_MASK);
  441. /*
  442. * Tell the MC to deploy the DPL:
  443. */
  444. out_le32(&mc_ccsr_regs->reg_gsr, 0x0);
  445. printf("\nfsl-mc: Deploying data path layout ...\n");
  446. error = wait_for_mc(false, &reg_gsr);
  447. if (error != 0)
  448. goto out;
  449. out:
  450. if (error != 0)
  451. mc_boot_status = -error;
  452. else
  453. mc_boot_status = 0;
  454. return error;
  455. }
  456. int get_mc_boot_status(void)
  457. {
  458. return mc_boot_status;
  459. }
  460. /**
  461. * Return the actual size of the MC private DRAM block.
  462. */
  463. unsigned long mc_get_dram_block_size(void)
  464. {
  465. unsigned long dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
  466. char *dram_block_size_env_var = getenv(MC_MEM_SIZE_ENV_VAR);
  467. if (dram_block_size_env_var) {
  468. dram_block_size = simple_strtoul(dram_block_size_env_var, NULL,
  469. 10);
  470. if (dram_block_size < CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE) {
  471. printf("fsl-mc: WARNING: Invalid value for \'"
  472. MC_MEM_SIZE_ENV_VAR
  473. "\' environment variable: %lu\n",
  474. dram_block_size);
  475. dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
  476. }
  477. }
  478. return dram_block_size;
  479. }
  480. int dpio_init(struct dprc_obj_desc obj_desc)
  481. {
  482. struct qbman_swp_desc p_des;
  483. struct dpio_attr attr;
  484. int err = 0;
  485. dflt_dpio = (struct fsl_dpio_obj *)malloc(sizeof(struct fsl_dpio_obj));
  486. if (!dflt_dpio) {
  487. printf(" No memory: malloc() failed\n");
  488. return -ENOMEM;
  489. }
  490. dflt_dpio->dpio_id = obj_desc.id;
  491. err = dpio_open(dflt_mc_io, obj_desc.id, &dflt_dpio_handle);
  492. if (err) {
  493. printf("dpio_open() failed\n");
  494. goto err_open;
  495. }
  496. err = dpio_get_attributes(dflt_mc_io, dflt_dpio_handle, &attr);
  497. if (err) {
  498. printf("dpio_get_attributes() failed %d\n", err);
  499. goto err_get_attr;
  500. }
  501. err = dpio_enable(dflt_mc_io, dflt_dpio_handle);
  502. if (err) {
  503. printf("dpio_enable() failed %d\n", err);
  504. goto err_get_enable;
  505. }
  506. debug("ce_paddr=0x%llx, ci_paddr=0x%llx, portalid=%d, prios=%d\n",
  507. attr.qbman_portal_ce_paddr,
  508. attr.qbman_portal_ci_paddr,
  509. attr.qbman_portal_id,
  510. attr.num_priorities);
  511. p_des.cena_bar = (void *)attr.qbman_portal_ce_paddr;
  512. p_des.cinh_bar = (void *)attr.qbman_portal_ci_paddr;
  513. dflt_dpio->sw_portal = qbman_swp_init(&p_des);
  514. if (dflt_dpio->sw_portal == NULL) {
  515. printf("qbman_swp_init() failed\n");
  516. goto err_get_swp_init;
  517. }
  518. return 0;
  519. err_get_swp_init:
  520. err_get_enable:
  521. dpio_disable(dflt_mc_io, dflt_dpio_handle);
  522. err_get_attr:
  523. dpio_close(dflt_mc_io, dflt_dpio_handle);
  524. err_open:
  525. free(dflt_dpio);
  526. return err;
  527. }
  528. int dpbp_init(struct dprc_obj_desc obj_desc)
  529. {
  530. dflt_dpbp = (struct fsl_dpbp_obj *)malloc(sizeof(struct fsl_dpbp_obj));
  531. if (!dflt_dpbp) {
  532. printf(" No memory: malloc() failed\n");
  533. return -ENOMEM;
  534. }
  535. dflt_dpbp->dpbp_attr.id = obj_desc.id;
  536. return 0;
  537. }
  538. int dprc_init_container_obj(struct dprc_obj_desc obj_desc, uint16_t dprc_handle)
  539. {
  540. int error = 0, state = 0;
  541. struct dprc_endpoint dpni_endpoint, dpmac_endpoint;
  542. if (!strcmp(obj_desc.type, "dpbp")) {
  543. if (!dflt_dpbp) {
  544. error = dpbp_init(obj_desc);
  545. if (error < 0)
  546. printf("dpbp_init failed\n");
  547. }
  548. } else if (!strcmp(obj_desc.type, "dpio")) {
  549. if (!dflt_dpio) {
  550. error = dpio_init(obj_desc);
  551. if (error < 0)
  552. printf("dpio_init failed\n");
  553. }
  554. } else if (!strcmp(obj_desc.type, "dpni")) {
  555. strcpy(dpni_endpoint.type, obj_desc.type);
  556. dpni_endpoint.id = obj_desc.id;
  557. error = dprc_get_connection(dflt_mc_io, dprc_handle,
  558. &dpni_endpoint, &dpmac_endpoint, &state);
  559. if (!strcmp(dpmac_endpoint.type, "dpmac"))
  560. error = ldpaa_eth_init(obj_desc);
  561. if (error < 0)
  562. printf("ldpaa_eth_init failed\n");
  563. }
  564. return error;
  565. }
  566. int dprc_scan_container_obj(uint16_t dprc_handle, char *obj_type, int i)
  567. {
  568. int error = 0;
  569. struct dprc_obj_desc obj_desc;
  570. memset((void *)&obj_desc, 0x00, sizeof(struct dprc_obj_desc));
  571. error = dprc_get_obj(dflt_mc_io, dprc_handle,
  572. i, &obj_desc);
  573. if (error < 0) {
  574. printf("dprc_get_obj(i=%d) failed: %d\n",
  575. i, error);
  576. return error;
  577. }
  578. if (!strcmp(obj_desc.type, obj_type)) {
  579. debug("Discovered object: type %s, id %d, req %s\n",
  580. obj_desc.type, obj_desc.id, obj_type);
  581. error = dprc_init_container_obj(obj_desc, dprc_handle);
  582. if (error < 0) {
  583. printf("dprc_init_container_obj(i=%d) failed: %d\n",
  584. i, error);
  585. return error;
  586. }
  587. }
  588. return error;
  589. }
  590. int fsl_mc_ldpaa_init(bd_t *bis)
  591. {
  592. int i, error = 0;
  593. int dprc_opened = 0, container_id;
  594. int num_child_objects = 0;
  595. error = mc_init();
  596. if (error < 0)
  597. goto error;
  598. error = dprc_get_container_id(dflt_mc_io, &container_id);
  599. if (error < 0) {
  600. printf("dprc_get_container_id() failed: %d\n", error);
  601. goto error;
  602. }
  603. debug("fsl-mc: Container id=0x%x\n", container_id);
  604. error = dprc_open(dflt_mc_io, container_id, &dflt_dprc_handle);
  605. if (error < 0) {
  606. printf("dprc_open() failed: %d\n", error);
  607. goto error;
  608. }
  609. dprc_opened = true;
  610. error = dprc_get_obj_count(dflt_mc_io,
  611. dflt_dprc_handle,
  612. &num_child_objects);
  613. if (error < 0) {
  614. printf("dprc_get_obj_count() failed: %d\n", error);
  615. goto error;
  616. }
  617. debug("Total child in container %d = %d\n", container_id,
  618. num_child_objects);
  619. if (num_child_objects != 0) {
  620. /*
  621. * Discover objects currently in the DPRC container in the MC:
  622. */
  623. for (i = 0; i < num_child_objects; i++)
  624. error = dprc_scan_container_obj(dflt_dprc_handle,
  625. "dpbp", i);
  626. for (i = 0; i < num_child_objects; i++)
  627. error = dprc_scan_container_obj(dflt_dprc_handle,
  628. "dpio", i);
  629. for (i = 0; i < num_child_objects; i++)
  630. error = dprc_scan_container_obj(dflt_dprc_handle,
  631. "dpni", i);
  632. }
  633. error:
  634. if (dprc_opened)
  635. dprc_close(dflt_mc_io, dflt_dprc_handle);
  636. return error;
  637. }
  638. void fsl_mc_ldpaa_exit(bd_t *bis)
  639. {
  640. int err;
  641. if (get_mc_boot_status() == 0) {
  642. err = dpio_disable(dflt_mc_io, dflt_dpio_handle);
  643. if (err < 0) {
  644. printf("dpio_disable() failed: %d\n", err);
  645. return;
  646. }
  647. err = dpio_reset(dflt_mc_io, dflt_dpio_handle);
  648. if (err < 0) {
  649. printf("dpio_reset() failed: %d\n", err);
  650. return;
  651. }
  652. err = dpio_close(dflt_mc_io, dflt_dpio_handle);
  653. if (err < 0) {
  654. printf("dpio_close() failed: %d\n", err);
  655. return;
  656. }
  657. free(dflt_dpio);
  658. free(dflt_dpbp);
  659. }
  660. if (dflt_mc_io)
  661. free(dflt_mc_io);
  662. }