mc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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("\nfsl-mc: WARNING: No DPC image found");
  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("\nfsl-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("\nfsl-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("\nfsl-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. assert(timeout_ms > 0);
  283. for (;;) {
  284. udelay(1000); /* throttle polling */
  285. reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
  286. mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
  287. if (mc_fw_boot_status & 0x1)
  288. break;
  289. timeout_ms--;
  290. if (timeout_ms == 0)
  291. break;
  292. }
  293. if (timeout_ms == 0) {
  294. printf("ERROR: timeout\n");
  295. /* TODO: Get an error status from an MC CCSR register */
  296. return -ETIMEDOUT;
  297. }
  298. if (mc_fw_boot_status != 0x1) {
  299. /*
  300. * TODO: Identify critical errors from the GSR register's FS
  301. * field and for those errors, set error to -ENODEV or other
  302. * appropriate errno, so that the status property is set to
  303. * failure in the fsl,dprc device tree node.
  304. */
  305. printf("WARNING: Firmware returned an error (GSR: %#x)\n",
  306. reg_gsr);
  307. } else {
  308. printf("SUCCESS\n");
  309. }
  310. *final_reg_gsr = reg_gsr;
  311. return 0;
  312. }
  313. int mc_init(void)
  314. {
  315. int error = 0;
  316. int portal_id = 0;
  317. struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
  318. u64 mc_ram_addr;
  319. u32 reg_gsr;
  320. u32 reg_mcfbalr;
  321. #ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
  322. const void *raw_image_addr;
  323. size_t raw_image_size = 0;
  324. #endif
  325. struct mc_version mc_ver_info;
  326. u64 mc_ram_aligned_base_addr;
  327. u8 mc_ram_num_256mb_blocks;
  328. size_t mc_ram_size = mc_get_dram_block_size();
  329. /*
  330. * The MC private DRAM block was already carved at the end of DRAM
  331. * by board_init_f() using CONFIG_SYS_MEM_TOP_HIDE:
  332. */
  333. if (gd->bd->bi_dram[1].start) {
  334. mc_ram_addr =
  335. gd->bd->bi_dram[1].start + gd->bd->bi_dram[1].size;
  336. } else {
  337. mc_ram_addr =
  338. gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size;
  339. }
  340. error = calculate_mc_private_ram_params(mc_ram_addr,
  341. mc_ram_size,
  342. &mc_ram_aligned_base_addr,
  343. &mc_ram_num_256mb_blocks);
  344. if (error != 0)
  345. goto out;
  346. /*
  347. * Management Complex cores should be held at reset out of POR.
  348. * U-boot should be the first software to touch MC. To be safe,
  349. * we reset all cores again by setting GCR1 to 0. It doesn't do
  350. * anything if they are held at reset. After we setup the firmware
  351. * we kick off MC by deasserting the reset bit for core 0, and
  352. * deasserting the reset bits for Command Portal Managers.
  353. * The stop bits are not touched here. They are used to stop the
  354. * cores when they are active. Setting stop bits doesn't stop the
  355. * cores from fetching instructions when they are released from
  356. * reset.
  357. */
  358. out_le32(&mc_ccsr_regs->reg_gcr1, 0);
  359. dmb();
  360. #ifdef CONFIG_SYS_LS_MC_FW_IN_DDR
  361. printf("MC firmware is preloaded to %#llx\n", mc_ram_addr);
  362. #else
  363. error = parse_mc_firmware_fit_image(&raw_image_addr, &raw_image_size);
  364. if (error != 0)
  365. goto out;
  366. /*
  367. * Load the MC FW at the beginning of the MC private DRAM block:
  368. */
  369. mc_copy_image("MC Firmware",
  370. (u64)raw_image_addr, raw_image_size, mc_ram_addr);
  371. #endif
  372. dump_ram_words("firmware", (void *)mc_ram_addr);
  373. error = load_mc_dpc(mc_ram_addr, mc_ram_size);
  374. if (error != 0)
  375. goto out;
  376. error = load_mc_dpl(mc_ram_addr, mc_ram_size);
  377. if (error != 0)
  378. goto out;
  379. debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
  380. dump_mc_ccsr_regs(mc_ccsr_regs);
  381. /*
  382. * Tell MC what is the address range of the DRAM block assigned to it:
  383. */
  384. reg_mcfbalr = (u32)mc_ram_aligned_base_addr |
  385. (mc_ram_num_256mb_blocks - 1);
  386. out_le32(&mc_ccsr_regs->reg_mcfbalr, reg_mcfbalr);
  387. out_le32(&mc_ccsr_regs->reg_mcfbahr,
  388. (u32)(mc_ram_aligned_base_addr >> 32));
  389. out_le32(&mc_ccsr_regs->reg_mcfapr, MCFAPR_BYPASS_ICID_MASK);
  390. /*
  391. * Tell the MC that we want delayed DPL deployment.
  392. */
  393. out_le32(&mc_ccsr_regs->reg_gsr, 0xDD00);
  394. printf("\nfsl-mc: Booting Management Complex ... ");
  395. /*
  396. * Deassert reset and release MC core 0 to run
  397. */
  398. out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
  399. error = wait_for_mc(true, &reg_gsr);
  400. if (error != 0)
  401. goto out;
  402. /*
  403. * TODO: need to obtain the portal_id for the root container from the
  404. * DPL
  405. */
  406. portal_id = 0;
  407. /*
  408. * Initialize the global default MC portal
  409. * And check that the MC firmware is responding portal commands:
  410. */
  411. dflt_mc_io = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
  412. if (!dflt_mc_io) {
  413. printf(" No memory: malloc() failed\n");
  414. return -ENOMEM;
  415. }
  416. dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(portal_id);
  417. debug("Checking access to MC portal of root DPRC container (portal_id %d, portal physical addr %p)\n",
  418. portal_id, dflt_mc_io->mmio_regs);
  419. error = mc_get_version(dflt_mc_io, &mc_ver_info);
  420. if (error != 0) {
  421. printf("fsl-mc: ERROR: Firmware version check failed (error: %d)\n",
  422. error);
  423. goto out;
  424. }
  425. if (MC_VER_MAJOR != mc_ver_info.major)
  426. printf("fsl-mc: ERROR: Firmware major version mismatch (found: %d, expected: %d)\n",
  427. mc_ver_info.major, MC_VER_MAJOR);
  428. if (MC_VER_MINOR != mc_ver_info.minor)
  429. printf("fsl-mc: WARNING: Firmware minor version mismatch (found: %d, expected: %d)\n",
  430. mc_ver_info.minor, MC_VER_MINOR);
  431. printf("fsl-mc: Management Complex booted (version: %d.%d.%d, boot status: %#x)\n",
  432. mc_ver_info.major, mc_ver_info.minor, mc_ver_info.revision,
  433. reg_gsr & GSR_FS_MASK);
  434. /*
  435. * Tell the MC to deploy the DPL:
  436. */
  437. out_le32(&mc_ccsr_regs->reg_gsr, 0x0);
  438. printf("fsl-mc: Deploying data path layout ... ");
  439. error = wait_for_mc(false, &reg_gsr);
  440. if (error != 0)
  441. goto out;
  442. out:
  443. if (error != 0)
  444. mc_boot_status = -error;
  445. else
  446. mc_boot_status = 0;
  447. return error;
  448. }
  449. int get_mc_boot_status(void)
  450. {
  451. return mc_boot_status;
  452. }
  453. /**
  454. * Return the actual size of the MC private DRAM block.
  455. */
  456. unsigned long mc_get_dram_block_size(void)
  457. {
  458. unsigned long dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
  459. char *dram_block_size_env_var = getenv(MC_MEM_SIZE_ENV_VAR);
  460. if (dram_block_size_env_var) {
  461. dram_block_size = simple_strtoul(dram_block_size_env_var, NULL,
  462. 10);
  463. if (dram_block_size < CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE) {
  464. printf("fsl-mc: WARNING: Invalid value for \'"
  465. MC_MEM_SIZE_ENV_VAR
  466. "\' environment variable: %lu\n",
  467. dram_block_size);
  468. dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
  469. }
  470. }
  471. return dram_block_size;
  472. }
  473. int dpio_init(struct dprc_obj_desc obj_desc)
  474. {
  475. struct qbman_swp_desc p_des;
  476. struct dpio_attr attr;
  477. int err = 0;
  478. dflt_dpio = (struct fsl_dpio_obj *)malloc(sizeof(struct fsl_dpio_obj));
  479. if (!dflt_dpio) {
  480. printf(" No memory: malloc() failed\n");
  481. return -ENOMEM;
  482. }
  483. dflt_dpio->dpio_id = obj_desc.id;
  484. err = dpio_open(dflt_mc_io, obj_desc.id, &dflt_dpio_handle);
  485. if (err) {
  486. printf("dpio_open() failed\n");
  487. goto err_open;
  488. }
  489. err = dpio_get_attributes(dflt_mc_io, dflt_dpio_handle, &attr);
  490. if (err) {
  491. printf("dpio_get_attributes() failed %d\n", err);
  492. goto err_get_attr;
  493. }
  494. err = dpio_enable(dflt_mc_io, dflt_dpio_handle);
  495. if (err) {
  496. printf("dpio_enable() failed %d\n", err);
  497. goto err_get_enable;
  498. }
  499. debug("ce_paddr=0x%llx, ci_paddr=0x%llx, portalid=%d, prios=%d\n",
  500. attr.qbman_portal_ce_paddr,
  501. attr.qbman_portal_ci_paddr,
  502. attr.qbman_portal_id,
  503. attr.num_priorities);
  504. p_des.cena_bar = (void *)attr.qbman_portal_ce_paddr;
  505. p_des.cinh_bar = (void *)attr.qbman_portal_ci_paddr;
  506. dflt_dpio->sw_portal = qbman_swp_init(&p_des);
  507. if (dflt_dpio->sw_portal == NULL) {
  508. printf("qbman_swp_init() failed\n");
  509. goto err_get_swp_init;
  510. }
  511. return 0;
  512. err_get_swp_init:
  513. err_get_enable:
  514. dpio_disable(dflt_mc_io, dflt_dpio_handle);
  515. err_get_attr:
  516. dpio_close(dflt_mc_io, dflt_dpio_handle);
  517. err_open:
  518. free(dflt_dpio);
  519. return err;
  520. }
  521. int dpbp_init(struct dprc_obj_desc obj_desc)
  522. {
  523. dflt_dpbp = (struct fsl_dpbp_obj *)malloc(sizeof(struct fsl_dpbp_obj));
  524. if (!dflt_dpbp) {
  525. printf(" No memory: malloc() failed\n");
  526. return -ENOMEM;
  527. }
  528. dflt_dpbp->dpbp_attr.id = obj_desc.id;
  529. return 0;
  530. }
  531. int dprc_init_container_obj(struct dprc_obj_desc obj_desc, uint16_t dprc_handle)
  532. {
  533. int error = 0, state = 0;
  534. struct dprc_endpoint dpni_endpoint, dpmac_endpoint;
  535. if (!strcmp(obj_desc.type, "dpbp")) {
  536. if (!dflt_dpbp) {
  537. error = dpbp_init(obj_desc);
  538. if (error < 0)
  539. printf("dpbp_init failed\n");
  540. }
  541. } else if (!strcmp(obj_desc.type, "dpio")) {
  542. if (!dflt_dpio) {
  543. error = dpio_init(obj_desc);
  544. if (error < 0)
  545. printf("dpio_init failed\n");
  546. }
  547. } else if (!strcmp(obj_desc.type, "dpni")) {
  548. strcpy(dpni_endpoint.type, obj_desc.type);
  549. dpni_endpoint.id = obj_desc.id;
  550. error = dprc_get_connection(dflt_mc_io, dprc_handle,
  551. &dpni_endpoint, &dpmac_endpoint, &state);
  552. if (!strcmp(dpmac_endpoint.type, "dpmac"))
  553. error = ldpaa_eth_init(obj_desc);
  554. if (error < 0)
  555. printf("ldpaa_eth_init failed\n");
  556. }
  557. return error;
  558. }
  559. int dprc_scan_container_obj(uint16_t dprc_handle, char *obj_type, int i)
  560. {
  561. int error = 0;
  562. struct dprc_obj_desc obj_desc;
  563. memset((void *)&obj_desc, 0x00, sizeof(struct dprc_obj_desc));
  564. error = dprc_get_obj(dflt_mc_io, dprc_handle,
  565. i, &obj_desc);
  566. if (error < 0) {
  567. printf("dprc_get_obj(i=%d) failed: %d\n",
  568. i, error);
  569. return error;
  570. }
  571. if (!strcmp(obj_desc.type, obj_type)) {
  572. debug("Discovered object: type %s, id %d, req %s\n",
  573. obj_desc.type, obj_desc.id, obj_type);
  574. error = dprc_init_container_obj(obj_desc, dprc_handle);
  575. if (error < 0) {
  576. printf("dprc_init_container_obj(i=%d) failed: %d\n",
  577. i, error);
  578. return error;
  579. }
  580. }
  581. return error;
  582. }
  583. int fsl_mc_ldpaa_init(bd_t *bis)
  584. {
  585. int i, error = 0;
  586. int dprc_opened = 0, container_id;
  587. int num_child_objects = 0;
  588. error = mc_init();
  589. if (error < 0)
  590. goto error;
  591. error = dprc_get_container_id(dflt_mc_io, &container_id);
  592. if (error < 0) {
  593. printf("dprc_get_container_id() failed: %d\n", error);
  594. goto error;
  595. }
  596. debug("fsl-mc: Container id=0x%x\n", container_id);
  597. error = dprc_open(dflt_mc_io, container_id, &dflt_dprc_handle);
  598. if (error < 0) {
  599. printf("dprc_open() failed: %d\n", error);
  600. goto error;
  601. }
  602. dprc_opened = true;
  603. error = dprc_get_obj_count(dflt_mc_io,
  604. dflt_dprc_handle,
  605. &num_child_objects);
  606. if (error < 0) {
  607. printf("dprc_get_obj_count() failed: %d\n", error);
  608. goto error;
  609. }
  610. debug("Total child in container %d = %d\n", container_id,
  611. num_child_objects);
  612. if (num_child_objects != 0) {
  613. /*
  614. * Discover objects currently in the DPRC container in the MC:
  615. */
  616. for (i = 0; i < num_child_objects; i++)
  617. error = dprc_scan_container_obj(dflt_dprc_handle,
  618. "dpbp", i);
  619. for (i = 0; i < num_child_objects; i++)
  620. error = dprc_scan_container_obj(dflt_dprc_handle,
  621. "dpio", i);
  622. for (i = 0; i < num_child_objects; i++)
  623. error = dprc_scan_container_obj(dflt_dprc_handle,
  624. "dpni", i);
  625. }
  626. error:
  627. if (dprc_opened)
  628. dprc_close(dflt_mc_io, dflt_dprc_handle);
  629. return error;
  630. }
  631. void fsl_mc_ldpaa_exit(bd_t *bis)
  632. {
  633. int err;
  634. if (get_mc_boot_status() == 0) {
  635. err = dpio_disable(dflt_mc_io, dflt_dpio_handle);
  636. if (err < 0) {
  637. printf("dpio_disable() failed: %d\n", err);
  638. return;
  639. }
  640. err = dpio_reset(dflt_mc_io, dflt_dpio_handle);
  641. if (err < 0) {
  642. printf("dpio_reset() failed: %d\n", err);
  643. return;
  644. }
  645. err = dpio_close(dflt_mc_io, dflt_dpio_handle);
  646. if (err < 0) {
  647. printf("dpio_close() failed: %d\n", err);
  648. return;
  649. }
  650. free(dflt_dpio);
  651. free(dflt_dpbp);
  652. }
  653. if (dflt_mc_io)
  654. free(dflt_mc_io);
  655. }