mc.c 38 KB

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