mc.c 36 KB

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