mc.c 20 KB

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