mc.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  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. int error;
  719. uint16_t major_ver, minor_ver;
  720. error = dprc_get_api_version(mc_io, 0,
  721. &major_ver,
  722. &minor_ver);
  723. if (error < 0) {
  724. printf("dprc_get_api_version() failed: %d\n", error);
  725. return error;
  726. }
  727. if (major_ver < DPRC_VER_MAJOR || (major_ver == DPRC_VER_MAJOR &&
  728. minor_ver < DPRC_VER_MINOR)) {
  729. printf("DPRC version mismatch found %u.%u,",
  730. major_ver, minor_ver);
  731. printf("supported version is %u.%u\n",
  732. DPRC_VER_MAJOR, DPRC_VER_MINOR);
  733. }
  734. return error;
  735. }
  736. static int dpio_init(void)
  737. {
  738. struct qbman_swp_desc p_des;
  739. struct dpio_attr attr;
  740. struct dpio_cfg dpio_cfg;
  741. int err = 0;
  742. uint16_t major_ver, minor_ver;
  743. dflt_dpio = (struct fsl_dpio_obj *)calloc(
  744. sizeof(struct fsl_dpio_obj), 1);
  745. if (!dflt_dpio) {
  746. printf("No memory: calloc() failed\n");
  747. err = -ENOMEM;
  748. goto err_calloc;
  749. }
  750. dpio_cfg.channel_mode = DPIO_LOCAL_CHANNEL;
  751. dpio_cfg.num_priorities = 8;
  752. err = dpio_create(dflt_mc_io,
  753. dflt_dprc_handle,
  754. MC_CMD_NO_FLAGS,
  755. &dpio_cfg,
  756. &dflt_dpio->dpio_id);
  757. if (err < 0) {
  758. printf("dpio_create() failed: %d\n", err);
  759. err = -ENODEV;
  760. goto err_create;
  761. }
  762. err = dpio_get_api_version(dflt_mc_io, 0,
  763. &major_ver,
  764. &minor_ver);
  765. if (err < 0) {
  766. printf("dpio_get_api_version() failed: %d\n", err);
  767. goto err_get_api_ver;
  768. }
  769. if (major_ver < DPIO_VER_MAJOR || (major_ver == DPIO_VER_MAJOR &&
  770. minor_ver < DPIO_VER_MINOR)) {
  771. printf("DPRC version mismatch found %u.%u,",
  772. major_ver,
  773. minor_ver);
  774. }
  775. err = dpio_open(dflt_mc_io,
  776. MC_CMD_NO_FLAGS,
  777. dflt_dpio->dpio_id,
  778. &dflt_dpio->dpio_handle);
  779. if (err) {
  780. printf("dpio_open() failed\n");
  781. goto err_open;
  782. }
  783. memset(&attr, 0, sizeof(struct dpio_attr));
  784. err = dpio_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
  785. dflt_dpio->dpio_handle, &attr);
  786. if (err < 0) {
  787. printf("dpio_get_attributes() failed: %d\n", err);
  788. goto err_get_attr;
  789. }
  790. if (dflt_dpio->dpio_id != attr.id) {
  791. printf("dnpi object id and attribute id are not same\n");
  792. goto err_attr_not_same;
  793. }
  794. #ifdef DEBUG
  795. printf("Init: DPIO id=0x%d\n", dflt_dpio->dpio_id);
  796. #endif
  797. err = dpio_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  798. if (err < 0) {
  799. printf("dpio_enable() failed %d\n", err);
  800. goto err_get_enable;
  801. }
  802. debug("ce_offset=0x%llx, ci_offset=0x%llx, portalid=%d, prios=%d\n",
  803. attr.qbman_portal_ce_offset,
  804. attr.qbman_portal_ci_offset,
  805. attr.qbman_portal_id,
  806. attr.num_priorities);
  807. p_des.cena_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
  808. + attr.qbman_portal_ce_offset);
  809. p_des.cinh_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
  810. + attr.qbman_portal_ci_offset);
  811. dflt_dpio->sw_portal = qbman_swp_init(&p_des);
  812. if (dflt_dpio->sw_portal == NULL) {
  813. printf("qbman_swp_init() failed\n");
  814. goto err_get_swp_init;
  815. }
  816. return 0;
  817. err_get_swp_init:
  818. dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  819. err_get_enable:
  820. err_get_attr:
  821. err_attr_not_same:
  822. dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  823. err_open:
  824. err_get_api_ver:
  825. dpio_destroy(dflt_mc_io,
  826. dflt_dprc_handle,
  827. MC_CMD_NO_FLAGS,
  828. dflt_dpio->dpio_id);
  829. err_create:
  830. free(dflt_dpio);
  831. err_calloc:
  832. return err;
  833. }
  834. static int dpio_exit(void)
  835. {
  836. int err;
  837. err = dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  838. if (err < 0) {
  839. printf("dpio_disable() failed: %d\n", err);
  840. goto err;
  841. }
  842. dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
  843. if (err < 0) {
  844. printf("dpio_close() failed: %d\n", err);
  845. goto err;
  846. }
  847. err = dpio_destroy(dflt_mc_io,
  848. dflt_dprc_handle,
  849. MC_CMD_NO_FLAGS,
  850. dflt_dpio->dpio_id);
  851. if (err < 0) {
  852. printf("dpio_destroy() failed: %d\n", err);
  853. goto err;
  854. }
  855. #ifdef DEBUG
  856. printf("Exit: DPIO id=0x%d\n", dflt_dpio->dpio_id);
  857. #endif
  858. if (dflt_dpio)
  859. free(dflt_dpio);
  860. return 0;
  861. err:
  862. return err;
  863. }
  864. static int dprc_init(void)
  865. {
  866. int err, child_portal_id, container_id;
  867. struct dprc_cfg cfg;
  868. uint64_t mc_portal_offset;
  869. /* Open root container */
  870. err = dprc_get_container_id(root_mc_io, MC_CMD_NO_FLAGS, &container_id);
  871. if (err < 0) {
  872. printf("dprc_get_container_id(): Root failed: %d\n", err);
  873. goto err_root_container_id;
  874. }
  875. #ifdef DEBUG
  876. printf("Root container id = %d\n", container_id);
  877. #endif
  878. err = dprc_open(root_mc_io, MC_CMD_NO_FLAGS, container_id,
  879. &root_dprc_handle);
  880. if (err < 0) {
  881. printf("dprc_open(): Root Container failed: %d\n", err);
  882. goto err_root_open;
  883. }
  884. if (!root_dprc_handle) {
  885. printf("dprc_open(): Root Container Handle is not valid\n");
  886. goto err_root_open;
  887. }
  888. err = dprc_version_check(root_mc_io, root_dprc_handle);
  889. if (err < 0) {
  890. printf("dprc_version_check() failed: %d\n", err);
  891. goto err_root_open;
  892. }
  893. memset(&cfg, 0, sizeof(struct dprc_cfg));
  894. cfg.options = DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED |
  895. DPRC_CFG_OPT_OBJ_CREATE_ALLOWED |
  896. DPRC_CFG_OPT_ALLOC_ALLOWED;
  897. cfg.icid = DPRC_GET_ICID_FROM_POOL;
  898. cfg.portal_id = DPRC_GET_PORTAL_ID_FROM_POOL;
  899. err = dprc_create_container(root_mc_io, MC_CMD_NO_FLAGS,
  900. root_dprc_handle,
  901. &cfg,
  902. &child_dprc_id,
  903. &mc_portal_offset);
  904. if (err < 0) {
  905. printf("dprc_create_container() failed: %d\n", err);
  906. goto err_create;
  907. }
  908. dflt_mc_io = (struct fsl_mc_io *)calloc(sizeof(struct fsl_mc_io), 1);
  909. if (!dflt_mc_io) {
  910. err = -ENOMEM;
  911. printf(" No memory: calloc() failed\n");
  912. goto err_calloc;
  913. }
  914. child_portal_id = MC_PORTAL_OFFSET_TO_PORTAL_ID(mc_portal_offset);
  915. dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(child_portal_id);
  916. #ifdef DEBUG
  917. printf("MC portal of child DPRC container: %d, physical addr %p)\n",
  918. child_dprc_id, dflt_mc_io->mmio_regs);
  919. #endif
  920. err = dprc_open(dflt_mc_io, MC_CMD_NO_FLAGS, child_dprc_id,
  921. &dflt_dprc_handle);
  922. if (err < 0) {
  923. printf("dprc_open(): Child container failed: %d\n", err);
  924. goto err_child_open;
  925. }
  926. if (!dflt_dprc_handle) {
  927. printf("dprc_open(): Child container Handle is not valid\n");
  928. goto err_child_open;
  929. }
  930. return 0;
  931. err_child_open:
  932. free(dflt_mc_io);
  933. err_calloc:
  934. dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
  935. root_dprc_handle, child_dprc_id);
  936. err_create:
  937. dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
  938. err_root_open:
  939. err_root_container_id:
  940. return err;
  941. }
  942. static int dprc_exit(void)
  943. {
  944. int err;
  945. err = dprc_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dprc_handle);
  946. if (err < 0) {
  947. printf("dprc_close(): Child failed: %d\n", err);
  948. goto err;
  949. }
  950. err = dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
  951. root_dprc_handle, child_dprc_id);
  952. if (err < 0) {
  953. printf("dprc_destroy_container() failed: %d\n", err);
  954. goto err;
  955. }
  956. err = dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
  957. if (err < 0) {
  958. printf("dprc_close(): Root failed: %d\n", err);
  959. goto err;
  960. }
  961. if (dflt_mc_io)
  962. free(dflt_mc_io);
  963. if (root_mc_io)
  964. free(root_mc_io);
  965. return 0;
  966. err:
  967. return err;
  968. }
  969. static int dpbp_init(void)
  970. {
  971. int err;
  972. struct dpbp_attr dpbp_attr;
  973. struct dpbp_cfg dpbp_cfg;
  974. uint16_t major_ver, minor_ver;
  975. dflt_dpbp = (struct fsl_dpbp_obj *)calloc(
  976. sizeof(struct fsl_dpbp_obj), 1);
  977. if (!dflt_dpbp) {
  978. printf("No memory: calloc() failed\n");
  979. err = -ENOMEM;
  980. goto err_calloc;
  981. }
  982. dpbp_cfg.options = 512;
  983. err = dpbp_create(dflt_mc_io,
  984. dflt_dprc_handle,
  985. MC_CMD_NO_FLAGS,
  986. &dpbp_cfg,
  987. &dflt_dpbp->dpbp_id);
  988. if (err < 0) {
  989. err = -ENODEV;
  990. printf("dpbp_create() failed: %d\n", err);
  991. goto err_create;
  992. }
  993. err = dpbp_get_api_version(dflt_mc_io, 0,
  994. &major_ver,
  995. &minor_ver);
  996. if (err < 0) {
  997. printf("dpbp_get_api_version() failed: %d\n", err);
  998. goto err_get_api_ver;
  999. }
  1000. if (major_ver < DPBP_VER_MAJOR || (major_ver == DPBP_VER_MAJOR &&
  1001. minor_ver < DPBP_VER_MINOR)) {
  1002. printf("DPBP version mismatch found %u.%u,",
  1003. major_ver, minor_ver);
  1004. printf("supported version is %u.%u\n",
  1005. DPBP_VER_MAJOR, DPBP_VER_MINOR);
  1006. }
  1007. err = dpbp_open(dflt_mc_io,
  1008. MC_CMD_NO_FLAGS,
  1009. dflt_dpbp->dpbp_id,
  1010. &dflt_dpbp->dpbp_handle);
  1011. if (err) {
  1012. printf("dpbp_open() failed\n");
  1013. goto err_open;
  1014. }
  1015. memset(&dpbp_attr, 0, sizeof(struct dpbp_attr));
  1016. err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
  1017. dflt_dpbp->dpbp_handle,
  1018. &dpbp_attr);
  1019. if (err < 0) {
  1020. printf("dpbp_get_attributes() failed: %d\n", err);
  1021. goto err_get_attr;
  1022. }
  1023. if (dflt_dpbp->dpbp_id != dpbp_attr.id) {
  1024. printf("dpbp object id and attribute id are not same\n");
  1025. goto err_attr_not_same;
  1026. }
  1027. #ifdef DEBUG
  1028. printf("Init: DPBP id=0x%x\n", dflt_dpbp->dpbp_attr.id);
  1029. #endif
  1030. err = dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
  1031. if (err < 0) {
  1032. printf("dpbp_close() failed: %d\n", err);
  1033. goto err_close;
  1034. }
  1035. return 0;
  1036. err_get_attr:
  1037. err_attr_not_same:
  1038. dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
  1039. dpbp_destroy(dflt_mc_io,
  1040. dflt_dprc_handle,
  1041. MC_CMD_NO_FLAGS,
  1042. dflt_dpbp->dpbp_id);
  1043. err_get_api_ver:
  1044. err_close:
  1045. err_open:
  1046. err_create:
  1047. free(dflt_dpbp);
  1048. err_calloc:
  1049. return err;
  1050. }
  1051. static int dpbp_exit(void)
  1052. {
  1053. int err;
  1054. err = dpbp_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
  1055. dflt_dpbp->dpbp_id);
  1056. if (err < 0) {
  1057. printf("dpbp_destroy() failed: %d\n", err);
  1058. goto err;
  1059. }
  1060. #ifdef DEBUG
  1061. printf("Exit: DPBP id=0x%d\n", dflt_dpbp->dpbp_attr.id);
  1062. #endif
  1063. if (dflt_dpbp)
  1064. free(dflt_dpbp);
  1065. return 0;
  1066. err:
  1067. return err;
  1068. }
  1069. static int dpni_init(void)
  1070. {
  1071. int err;
  1072. uint8_t cfg_buf[256] = {0};
  1073. struct dpni_cfg dpni_cfg;
  1074. uint16_t major_ver, minor_ver;
  1075. dflt_dpni = (struct fsl_dpni_obj *)calloc(
  1076. sizeof(struct fsl_dpni_obj), 1);
  1077. if (!dflt_dpni) {
  1078. printf("No memory: calloc() failed\n");
  1079. err = -ENOMEM;
  1080. goto err_calloc;
  1081. }
  1082. memset(&dpni_cfg, 0, sizeof(dpni_cfg));
  1083. err = dpni_prepare_cfg(&dpni_cfg, &cfg_buf[0]);
  1084. if (err < 0) {
  1085. err = -ENODEV;
  1086. printf("dpni_prepare_cfg() failed: %d\n", err);
  1087. goto err_prepare_cfg;
  1088. }
  1089. err = dpni_create(dflt_mc_io,
  1090. dflt_dprc_handle,
  1091. MC_CMD_NO_FLAGS,
  1092. &dpni_cfg,
  1093. &dflt_dpni->dpni_id);
  1094. if (err < 0) {
  1095. err = -ENODEV;
  1096. printf("dpni create() failed: %d\n", err);
  1097. goto err_create;
  1098. }
  1099. err = dpni_get_api_version(dflt_mc_io, 0,
  1100. &major_ver,
  1101. &minor_ver);
  1102. if (err < 0) {
  1103. printf("dpni_get_api_version() failed: %d\n", err);
  1104. goto err_get_version;
  1105. }
  1106. if (major_ver < DPNI_VER_MAJOR || (major_ver == DPNI_VER_MAJOR &&
  1107. minor_ver < DPNI_VER_MINOR)) {
  1108. printf("DPNI version mismatch found %u.%u,",
  1109. major_ver, minor_ver);
  1110. printf("supported version is %u.%u\n",
  1111. DPNI_VER_MAJOR, DPNI_VER_MINOR);
  1112. }
  1113. err = dpni_open(dflt_mc_io,
  1114. MC_CMD_NO_FLAGS,
  1115. dflt_dpni->dpni_id,
  1116. &dflt_dpni->dpni_handle);
  1117. if (err) {
  1118. printf("dpni_open() failed\n");
  1119. goto err_open;
  1120. }
  1121. #ifdef DEBUG
  1122. printf("Init: DPNI id=0x%d\n", dflt_dpni->dpni_id);
  1123. #endif
  1124. err = dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
  1125. if (err < 0) {
  1126. printf("dpni_close() failed: %d\n", err);
  1127. goto err_close;
  1128. }
  1129. return 0;
  1130. err_close:
  1131. dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
  1132. err_open:
  1133. err_get_version:
  1134. dpni_destroy(dflt_mc_io,
  1135. dflt_dprc_handle,
  1136. MC_CMD_NO_FLAGS,
  1137. dflt_dpni->dpni_id);
  1138. err_create:
  1139. err_prepare_cfg:
  1140. free(dflt_dpni);
  1141. err_calloc:
  1142. return err;
  1143. }
  1144. static int dpni_exit(void)
  1145. {
  1146. int err;
  1147. err = dpni_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
  1148. dflt_dpni->dpni_id);
  1149. if (err < 0) {
  1150. printf("dpni_destroy() failed: %d\n", err);
  1151. goto err;
  1152. }
  1153. #ifdef DEBUG
  1154. printf("Exit: DPNI id=0x%d\n", dflt_dpni->dpni_id);
  1155. #endif
  1156. if (dflt_dpni)
  1157. free(dflt_dpni);
  1158. return 0;
  1159. err:
  1160. return err;
  1161. }
  1162. static int mc_init_object(void)
  1163. {
  1164. int err = 0;
  1165. err = dprc_init();
  1166. if (err < 0) {
  1167. printf("dprc_init() failed: %d\n", err);
  1168. goto err;
  1169. }
  1170. err = dpbp_init();
  1171. if (err < 0) {
  1172. printf("dpbp_init() failed: %d\n", err);
  1173. goto err;
  1174. }
  1175. err = dpio_init();
  1176. if (err < 0) {
  1177. printf("dpio_init() failed: %d\n", err);
  1178. goto err;
  1179. }
  1180. err = dpni_init();
  1181. if (err < 0) {
  1182. printf("dpni_init() failed: %d\n", err);
  1183. goto err;
  1184. }
  1185. return 0;
  1186. err:
  1187. return err;
  1188. }
  1189. int fsl_mc_ldpaa_exit(bd_t *bd)
  1190. {
  1191. int err = 0;
  1192. bool is_dpl_apply_status = false;
  1193. bool mc_boot_status = false;
  1194. if (bd && mc_lazy_dpl_addr && !fsl_mc_ldpaa_exit(NULL)) {
  1195. err = mc_apply_dpl(mc_lazy_dpl_addr);
  1196. if (!err)
  1197. fdt_fixup_board_enet(working_fdt);
  1198. mc_lazy_dpl_addr = 0;
  1199. }
  1200. if (!get_mc_boot_status())
  1201. mc_boot_status = true;
  1202. /* MC is not loaded intentionally, So return success. */
  1203. if (bd && !mc_boot_status)
  1204. return 0;
  1205. /* If DPL is deployed, set is_dpl_apply_status as TRUE. */
  1206. if (!get_dpl_apply_status())
  1207. is_dpl_apply_status = true;
  1208. /*
  1209. * For case MC is loaded but DPL is not deployed, return success and
  1210. * print message on console. Else FDT fix-up code execution hanged.
  1211. */
  1212. if (bd && mc_boot_status && !is_dpl_apply_status) {
  1213. printf("fsl-mc: DPL not deployed, DPAA2 ethernet not work\n");
  1214. goto mc_obj_cleanup;
  1215. }
  1216. if (bd && mc_boot_status && is_dpl_apply_status)
  1217. return 0;
  1218. mc_obj_cleanup:
  1219. err = dpbp_exit();
  1220. if (err < 0) {
  1221. printf("dpbp_exit() failed: %d\n", err);
  1222. goto err;
  1223. }
  1224. err = dpio_exit();
  1225. if (err < 0) {
  1226. printf("dpio_exit() failed: %d\n", err);
  1227. goto err;
  1228. }
  1229. err = dpni_exit();
  1230. if (err < 0) {
  1231. printf("dpni_exit() failed: %d\n", err);
  1232. goto err;
  1233. }
  1234. err = dprc_exit();
  1235. if (err < 0) {
  1236. printf("dprc_exit() failed: %d\n", err);
  1237. goto err;
  1238. }
  1239. return 0;
  1240. err:
  1241. return err;
  1242. }
  1243. static int do_fsl_mc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1244. {
  1245. int err = 0;
  1246. if (argc < 3)
  1247. goto usage;
  1248. switch (argv[1][0]) {
  1249. case 's': {
  1250. char sub_cmd;
  1251. u64 mc_fw_addr, mc_dpc_addr;
  1252. #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
  1253. u64 aiop_fw_addr;
  1254. #endif
  1255. sub_cmd = argv[2][0];
  1256. switch (sub_cmd) {
  1257. case 'm':
  1258. if (argc < 5)
  1259. goto usage;
  1260. if (get_mc_boot_status() == 0) {
  1261. printf("fsl-mc: MC is already booted");
  1262. printf("\n");
  1263. return err;
  1264. }
  1265. mc_fw_addr = simple_strtoull(argv[3], NULL, 16);
  1266. mc_dpc_addr = simple_strtoull(argv[4], NULL,
  1267. 16);
  1268. if (!mc_init(mc_fw_addr, mc_dpc_addr))
  1269. err = mc_init_object();
  1270. break;
  1271. #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
  1272. case 'a':
  1273. if (argc < 4)
  1274. goto usage;
  1275. if (get_aiop_apply_status() == 0) {
  1276. printf("fsl-mc: AIOP FW is already");
  1277. printf(" applied\n");
  1278. return err;
  1279. }
  1280. aiop_fw_addr = simple_strtoull(argv[3], NULL,
  1281. 16);
  1282. /* if SoC doesn't have AIOP, err = -ENODEV */
  1283. err = load_mc_aiop_img(aiop_fw_addr);
  1284. if (!err)
  1285. printf("fsl-mc: AIOP FW applied\n");
  1286. break;
  1287. #endif
  1288. default:
  1289. printf("Invalid option: %s\n", argv[2]);
  1290. goto usage;
  1291. break;
  1292. }
  1293. }
  1294. break;
  1295. case 'l':
  1296. case 'a': {
  1297. u64 mc_dpl_addr;
  1298. if (argc < 4)
  1299. goto usage;
  1300. if (get_dpl_apply_status() == 0) {
  1301. printf("fsl-mc: DPL already applied\n");
  1302. return err;
  1303. }
  1304. mc_dpl_addr = simple_strtoull(argv[3], NULL,
  1305. 16);
  1306. if (get_mc_boot_status() != 0) {
  1307. printf("fsl-mc: Deploying data path layout ..");
  1308. printf("ERROR (MC is not booted)\n");
  1309. return -ENODEV;
  1310. }
  1311. if (argv[1][0] == 'l') {
  1312. /*
  1313. * We will do the actual dpaa exit and dpl apply
  1314. * later from announce_and_cleanup().
  1315. */
  1316. mc_lazy_dpl_addr = mc_dpl_addr;
  1317. } else {
  1318. /* The user wants it applied now */
  1319. if (!fsl_mc_ldpaa_exit(NULL))
  1320. err = mc_apply_dpl(mc_dpl_addr);
  1321. }
  1322. break;
  1323. }
  1324. default:
  1325. printf("Invalid option: %s\n", argv[1]);
  1326. goto usage;
  1327. break;
  1328. }
  1329. return err;
  1330. usage:
  1331. return CMD_RET_USAGE;
  1332. }
  1333. U_BOOT_CMD(
  1334. fsl_mc, CONFIG_SYS_MAXARGS, 1, do_fsl_mc,
  1335. "DPAA2 command to manage Management Complex (MC)",
  1336. "start mc [FW_addr] [DPC_addr] - Start Management Complex\n"
  1337. "fsl_mc apply DPL [DPL_addr] - Apply DPL file\n"
  1338. "fsl_mc lazyapply DPL [DPL_addr] - Apply DPL file on exit\n"
  1339. "fsl_mc start aiop [FW_addr] - Start AIOP\n"
  1340. );
  1341. void mc_env_boot(void)
  1342. {
  1343. #if defined(CONFIG_FSL_MC_ENET)
  1344. char *mc_boot_env_var;
  1345. /* The MC may only be initialized in the reset PHY function
  1346. * because otherwise U-Boot has not yet set up all the MAC
  1347. * address info properly. Without MAC addresses, the MC code
  1348. * can not properly initialize the DPC.
  1349. */
  1350. mc_boot_env_var = env_get(MC_BOOT_ENV_VAR);
  1351. if (mc_boot_env_var)
  1352. run_command_list(mc_boot_env_var, -1, 0);
  1353. #endif /* CONFIG_FSL_MC_ENET */
  1354. }