qe.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2006-2009 Freescale Semiconductor, Inc.
  4. *
  5. * Dave Liu <daveliu@freescale.com>
  6. * based on source code of Shlomi Gridish
  7. */
  8. #include <common.h>
  9. #include <malloc.h>
  10. #include <command.h>
  11. #include <asm/global_data.h>
  12. #include <linux/errno.h>
  13. #include <asm/io.h>
  14. #include <linux/immap_qe.h>
  15. #include <fsl_qe.h>
  16. #include <mmc.h>
  17. #include <u-boot/crc.h>
  18. #ifdef CONFIG_ARCH_LS1021A
  19. #include <asm/arch/immap_ls102xa.h>
  20. #endif
  21. #ifdef CONFIG_ARM64
  22. #include <asm/armv8/mmu.h>
  23. #include <asm/arch/cpu.h>
  24. #endif
  25. #define MPC85xx_DEVDISR_QE_DISABLE 0x1
  26. qe_map_t *qe_immr;
  27. #ifdef CONFIG_QE
  28. static qe_snum_t snums[QE_NUM_OF_SNUM];
  29. #endif
  30. DECLARE_GLOBAL_DATA_PTR;
  31. void qe_issue_cmd(uint cmd, uint sbc, u8 mcn, u32 cmd_data)
  32. {
  33. u32 cecr;
  34. if (cmd == QE_RESET) {
  35. out_be32(&qe_immr->cp.cecr, (u32)(cmd | QE_CR_FLG));
  36. } else {
  37. out_be32(&qe_immr->cp.cecdr, cmd_data);
  38. out_be32(&qe_immr->cp.cecr, (sbc | QE_CR_FLG |
  39. ((u32)mcn << QE_CR_PROTOCOL_SHIFT) | cmd));
  40. }
  41. /* Wait for the QE_CR_FLG to clear */
  42. do {
  43. cecr = in_be32(&qe_immr->cp.cecr);
  44. } while (cecr & QE_CR_FLG);
  45. }
  46. #ifdef CONFIG_QE
  47. uint qe_muram_alloc(uint size, uint align)
  48. {
  49. uint retloc;
  50. uint align_mask, off;
  51. uint savebase;
  52. align_mask = align - 1;
  53. savebase = gd->arch.mp_alloc_base;
  54. off = gd->arch.mp_alloc_base & align_mask;
  55. if (off != 0)
  56. gd->arch.mp_alloc_base += (align - off);
  57. off = size & align_mask;
  58. if (off != 0)
  59. size += (align - off);
  60. if ((gd->arch.mp_alloc_base + size) >= gd->arch.mp_alloc_top) {
  61. gd->arch.mp_alloc_base = savebase;
  62. printf("%s: ran out of ram.\n", __func__);
  63. }
  64. retloc = gd->arch.mp_alloc_base;
  65. gd->arch.mp_alloc_base += size;
  66. memset((void *)&qe_immr->muram[retloc], 0, size);
  67. __asm__ __volatile__("sync");
  68. return retloc;
  69. }
  70. #endif
  71. void *qe_muram_addr(uint offset)
  72. {
  73. return (void *)&qe_immr->muram[offset];
  74. }
  75. #ifdef CONFIG_QE
  76. static void qe_sdma_init(void)
  77. {
  78. sdma_t *p;
  79. uint sdma_buffer_base;
  80. p = (sdma_t *)&qe_immr->sdma;
  81. /* All of DMA transaction in bus 1 */
  82. out_be32(&p->sdaqr, 0);
  83. out_be32(&p->sdaqmr, 0);
  84. /* Allocate 2KB temporary buffer for sdma */
  85. sdma_buffer_base = qe_muram_alloc(2048, 4096);
  86. out_be32(&p->sdwbcr, sdma_buffer_base & QE_SDEBCR_BA_MASK);
  87. /* Clear sdma status */
  88. out_be32(&p->sdsr, 0x03000000);
  89. /* Enable global mode on bus 1, and 2KB buffer size */
  90. out_be32(&p->sdmr, QE_SDMR_GLB_1_MSK | (0x3 << QE_SDMR_CEN_SHIFT));
  91. }
  92. /* This table is a list of the serial numbers of the Threads, taken from the
  93. * "SNUM Table" chart in the QE Reference Manual. The order is not important,
  94. * we just need to know what the SNUMs are for the threads.
  95. */
  96. static u8 thread_snum[] = {
  97. /* Evthreads 16-29 are not supported in MPC8309 */
  98. 0x04, 0x05, 0x0c, 0x0d,
  99. 0x14, 0x15, 0x1c, 0x1d,
  100. 0x24, 0x25, 0x2c, 0x2d,
  101. 0x34, 0x35,
  102. 0x88, 0x89, 0x98, 0x99,
  103. 0xa8, 0xa9, 0xb8, 0xb9,
  104. 0xc8, 0xc9, 0xd8, 0xd9,
  105. 0xe8, 0xe9, 0x08, 0x09,
  106. 0x18, 0x19, 0x28, 0x29,
  107. 0x38, 0x39, 0x48, 0x49,
  108. 0x58, 0x59, 0x68, 0x69,
  109. 0x78, 0x79, 0x80, 0x81
  110. };
  111. static void qe_snums_init(void)
  112. {
  113. int i;
  114. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  115. snums[i].state = QE_SNUM_STATE_FREE;
  116. snums[i].num = thread_snum[i];
  117. }
  118. }
  119. int qe_get_snum(void)
  120. {
  121. int snum = -EBUSY;
  122. int i;
  123. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  124. if (snums[i].state == QE_SNUM_STATE_FREE) {
  125. snums[i].state = QE_SNUM_STATE_USED;
  126. snum = snums[i].num;
  127. break;
  128. }
  129. }
  130. return snum;
  131. }
  132. void qe_put_snum(u8 snum)
  133. {
  134. int i;
  135. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  136. if (snums[i].num == snum) {
  137. snums[i].state = QE_SNUM_STATE_FREE;
  138. break;
  139. }
  140. }
  141. }
  142. #ifdef CONFIG_TFABOOT
  143. void qe_init(uint qe_base)
  144. {
  145. enum boot_src src = get_boot_src();
  146. /* Init the QE IMMR base */
  147. qe_immr = (qe_map_t *)qe_base;
  148. if (src == BOOT_SOURCE_IFC_NOR) {
  149. /*
  150. * Upload microcode to IRAM for those SOCs
  151. * which do not have ROM in QE.
  152. */
  153. qe_upload_firmware((const void *)(CONFIG_SYS_QE_FW_ADDR +
  154. CFG_SYS_FSL_IFC_BASE));
  155. /* enable the microcode in IRAM */
  156. out_be32(&qe_immr->iram.iready, QE_IRAM_READY);
  157. }
  158. gd->arch.mp_alloc_base = QE_DATAONLY_BASE;
  159. gd->arch.mp_alloc_top = gd->arch.mp_alloc_base + QE_DATAONLY_SIZE;
  160. qe_sdma_init();
  161. qe_snums_init();
  162. }
  163. #else
  164. void qe_init(uint qe_base)
  165. {
  166. /* Init the QE IMMR base */
  167. qe_immr = (qe_map_t *)qe_base;
  168. #ifdef CONFIG_SYS_QE_FMAN_FW_IN_NOR
  169. /*
  170. * Upload microcode to IRAM for those SOCs which do not have ROM in QE.
  171. */
  172. qe_upload_firmware((const void *)CONFIG_SYS_QE_FW_ADDR);
  173. /* enable the microcode in IRAM */
  174. out_be32(&qe_immr->iram.iready, QE_IRAM_READY);
  175. #endif
  176. gd->arch.mp_alloc_base = QE_DATAONLY_BASE;
  177. gd->arch.mp_alloc_top = gd->arch.mp_alloc_base + QE_DATAONLY_SIZE;
  178. qe_sdma_init();
  179. qe_snums_init();
  180. }
  181. #endif
  182. #endif
  183. #ifdef CONFIG_U_QE
  184. #ifdef CONFIG_TFABOOT
  185. void u_qe_init(void)
  186. {
  187. enum boot_src src = get_boot_src();
  188. qe_immr = (qe_map_t *)(CONFIG_SYS_IMMR + QE_IMMR_OFFSET);
  189. void *addr = (void *)CONFIG_SYS_QE_FW_ADDR;
  190. if (src == BOOT_SOURCE_IFC_NOR)
  191. addr = (void *)(CONFIG_SYS_QE_FW_ADDR +
  192. CFG_SYS_FSL_IFC_BASE);
  193. if (src == BOOT_SOURCE_QSPI_NOR)
  194. addr = (void *)(CONFIG_SYS_QE_FW_ADDR +
  195. CFG_SYS_FSL_QSPI_BASE);
  196. if (src == BOOT_SOURCE_SD_MMC) {
  197. int dev = CONFIG_SYS_MMC_ENV_DEV;
  198. u32 cnt = CONFIG_SYS_QE_FMAN_FW_LENGTH / 512;
  199. u32 blk = CONFIG_SYS_QE_FW_ADDR / 512;
  200. if (mmc_initialize(gd->bd)) {
  201. printf("%s: mmc_initialize() failed\n", __func__);
  202. return;
  203. }
  204. addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH);
  205. struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
  206. if (!mmc) {
  207. free(addr);
  208. printf("\nMMC cannot find device for ucode\n");
  209. } else {
  210. printf("\nMMC read: dev # %u, block # %u, count %u ...\n",
  211. dev, blk, cnt);
  212. mmc_init(mmc);
  213. (void)blk_dread(mmc_get_blk_desc(mmc), blk, cnt,
  214. addr);
  215. }
  216. }
  217. if (!u_qe_upload_firmware(addr))
  218. out_be32(&qe_immr->iram.iready, QE_IRAM_READY);
  219. if (src == BOOT_SOURCE_SD_MMC)
  220. free(addr);
  221. }
  222. #else
  223. void u_qe_init(void)
  224. {
  225. qe_immr = (qe_map_t *)(CONFIG_SYS_IMMR + QE_IMMR_OFFSET);
  226. void *addr = (void *)CONFIG_SYS_QE_FW_ADDR;
  227. #ifdef CONFIG_SYS_QE_FMAN_FW_IN_MMC
  228. int dev = CONFIG_SYS_MMC_ENV_DEV;
  229. u32 cnt = CONFIG_SYS_QE_FMAN_FW_LENGTH / 512;
  230. u32 blk = CONFIG_SYS_QE_FW_ADDR / 512;
  231. if (mmc_initialize(gd->bd)) {
  232. printf("%s: mmc_initialize() failed\n", __func__);
  233. return;
  234. }
  235. addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH);
  236. struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
  237. if (!mmc) {
  238. printf("\nMMC cannot find device for ucode\n");
  239. } else {
  240. printf("\nMMC read: dev # %u, block # %u, count %u ...\n",
  241. dev, blk, cnt);
  242. mmc_init(mmc);
  243. (void)blk_dread(mmc_get_blk_desc(mmc), blk, cnt,
  244. addr);
  245. }
  246. #endif
  247. if (!u_qe_upload_firmware(addr))
  248. out_be32(&qe_immr->iram.iready, QE_IRAM_READY);
  249. #ifdef CONFIG_SYS_QE_FMAN_FW_IN_MMC
  250. free(addr);
  251. #endif
  252. }
  253. #endif
  254. #endif
  255. #ifdef CONFIG_U_QE
  256. void u_qe_resume(void)
  257. {
  258. qe_map_t *qe_immrr;
  259. qe_immrr = (qe_map_t *)(CONFIG_SYS_IMMR + QE_IMMR_OFFSET);
  260. u_qe_firmware_resume((const void *)CONFIG_SYS_QE_FW_ADDR, qe_immrr);
  261. out_be32(&qe_immrr->iram.iready, QE_IRAM_READY);
  262. }
  263. #endif
  264. void qe_reset(void)
  265. {
  266. qe_issue_cmd(QE_RESET, QE_CR_SUBBLOCK_INVALID,
  267. (u8)QE_CR_PROTOCOL_UNSPECIFIED, 0);
  268. }
  269. #ifdef CONFIG_QE
  270. void qe_assign_page(uint snum, uint para_ram_base)
  271. {
  272. u32 cecr;
  273. out_be32(&qe_immr->cp.cecdr, para_ram_base);
  274. out_be32(&qe_immr->cp.cecr, ((u32)snum << QE_CR_ASSIGN_PAGE_SNUM_SHIFT)
  275. | QE_CR_FLG | QE_ASSIGN_PAGE);
  276. /* Wait for the QE_CR_FLG to clear */
  277. do {
  278. cecr = in_be32(&qe_immr->cp.cecr);
  279. } while (cecr & QE_CR_FLG);
  280. }
  281. #endif
  282. /*
  283. * brg: 0~15 as BRG1~BRG16
  284. * rate: baud rate
  285. * BRG input clock comes from the BRGCLK (internal clock generated from
  286. * the QE clock, it is one-half of the QE clock), If need the clock source
  287. * from CLKn pin, we have te change the function.
  288. */
  289. #define BRG_CLK (gd->arch.brg_clk)
  290. #ifdef CONFIG_QE
  291. int qe_set_brg(uint brg, uint rate)
  292. {
  293. uint *bp;
  294. u32 divisor;
  295. u32 val;
  296. int div16 = 0;
  297. if (brg >= QE_NUM_OF_BRGS)
  298. return -EINVAL;
  299. bp = (uint *)&qe_immr->brg.brgc1;
  300. bp += brg;
  301. divisor = (BRG_CLK / rate);
  302. if (divisor > QE_BRGC_DIVISOR_MAX + 1) {
  303. div16 = 1;
  304. divisor /= 16;
  305. }
  306. /* CHECK TODO */
  307. /*
  308. * was
  309. * *bp = ((divisor - 1) << QE_BRGC_DIVISOR_SHIFT) | QE_BRGC_ENABLE;
  310. * __asm__ __volatile__("sync");
  311. */
  312. val = ((divisor - 1) << QE_BRGC_DIVISOR_SHIFT) | QE_BRGC_ENABLE;
  313. if (div16)
  314. val |= QE_BRGC_DIV16;
  315. out_be32(bp, val);
  316. return 0;
  317. }
  318. #endif
  319. /* Set ethernet MII clock master */
  320. int qe_set_mii_clk_src(int ucc_num)
  321. {
  322. u32 cmxgcr;
  323. /* check if the UCC number is in range. */
  324. if ((ucc_num > UCC_MAX_NUM - 1) || ucc_num < 0) {
  325. printf("%s: ucc num not in ranges\n", __func__);
  326. return -EINVAL;
  327. }
  328. cmxgcr = in_be32(&qe_immr->qmx.cmxgcr);
  329. cmxgcr &= ~QE_CMXGCR_MII_ENET_MNG_MASK;
  330. cmxgcr |= (ucc_num << QE_CMXGCR_MII_ENET_MNG_SHIFT);
  331. out_be32(&qe_immr->qmx.cmxgcr, cmxgcr);
  332. return 0;
  333. }
  334. /* Firmware information stored here for qe_get_firmware_info() */
  335. static struct qe_firmware_info qe_firmware_info;
  336. /*
  337. * Set to 1 if QE firmware has been uploaded, and therefore
  338. * qe_firmware_info contains valid data.
  339. */
  340. static int qe_firmware_uploaded;
  341. /*
  342. * Upload a QE microcode
  343. *
  344. * This function is a worker function for qe_upload_firmware(). It does
  345. * the actual uploading of the microcode.
  346. */
  347. static void qe_upload_microcode(const void *base,
  348. const struct qe_microcode *ucode)
  349. {
  350. const u32 *code = base + be32_to_cpu(ucode->code_offset);
  351. unsigned int i;
  352. if (ucode->major || ucode->minor || ucode->revision)
  353. printf("QE: uploading microcode '%s' version %u.%u.%u\n",
  354. (char *)ucode->id, (u16)ucode->major, (u16)ucode->minor,
  355. (u16)ucode->revision);
  356. else
  357. printf("QE: uploading microcode '%s'\n", (char *)ucode->id);
  358. /* Use auto-increment */
  359. out_be32(&qe_immr->iram.iadd, be32_to_cpu(ucode->iram_offset) |
  360. QE_IRAM_IADD_AIE | QE_IRAM_IADD_BADDR);
  361. for (i = 0; i < be32_to_cpu(ucode->count); i++)
  362. out_be32(&qe_immr->iram.idata, be32_to_cpu(code[i]));
  363. }
  364. /*
  365. * Upload a microcode to the I-RAM at a specific address.
  366. *
  367. * See Documentation/powerpc/qe_firmware.rst in the Linux kernel tree for
  368. * information on QE microcode uploading.
  369. *
  370. * Currently, only version 1 is supported, so the 'version' field must be
  371. * set to 1.
  372. *
  373. * The SOC model and revision are not validated, they are only displayed for
  374. * informational purposes.
  375. *
  376. * 'calc_size' is the calculated size, in bytes, of the firmware structure and
  377. * all of the microcode structures, minus the CRC.
  378. *
  379. * 'length' is the size that the structure says it is, including the CRC.
  380. */
  381. int qe_upload_firmware(const struct qe_firmware *firmware)
  382. {
  383. unsigned int i;
  384. unsigned int j;
  385. u32 crc;
  386. size_t calc_size = sizeof(struct qe_firmware);
  387. size_t length;
  388. const struct qe_header *hdr;
  389. #ifdef CONFIG_DEEP_SLEEP
  390. #ifdef CONFIG_ARCH_LS1021A
  391. struct ccsr_gur __iomem *gur = (void *)CFG_SYS_FSL_GUTS_ADDR;
  392. #else
  393. ccsr_gur_t *gur = (void *)(CFG_SYS_MPC85xx_GUTS_ADDR);
  394. #endif
  395. #endif
  396. if (!firmware) {
  397. printf("Invalid address\n");
  398. return -EINVAL;
  399. }
  400. hdr = &firmware->header;
  401. length = be32_to_cpu(hdr->length);
  402. /* Check the magic */
  403. if ((hdr->magic[0] != 'Q') || (hdr->magic[1] != 'E') ||
  404. (hdr->magic[2] != 'F')) {
  405. printf("QE microcode not found\n");
  406. #ifdef CONFIG_DEEP_SLEEP
  407. setbits_be32(&gur->devdisr, MPC85xx_DEVDISR_QE_DISABLE);
  408. #endif
  409. return -EPERM;
  410. }
  411. /* Check the version */
  412. if (hdr->version != 1) {
  413. printf("Unsupported version\n");
  414. return -EPERM;
  415. }
  416. /* Validate some of the fields */
  417. if (firmware->count < 1 || firmware->count > MAX_QE_RISC) {
  418. printf("Invalid data\n");
  419. return -EINVAL;
  420. }
  421. /* Validate the length and check if there's a CRC */
  422. calc_size += (firmware->count - 1) * sizeof(struct qe_microcode);
  423. for (i = 0; i < firmware->count; i++)
  424. /*
  425. * For situations where the second RISC uses the same microcode
  426. * as the first, the 'code_offset' and 'count' fields will be
  427. * zero, so it's okay to add those.
  428. */
  429. calc_size += sizeof(u32) *
  430. be32_to_cpu(firmware->microcode[i].count);
  431. /* Validate the length */
  432. if (length != calc_size + sizeof(u32)) {
  433. printf("Invalid length\n");
  434. return -EPERM;
  435. }
  436. /*
  437. * Validate the CRC. We would normally call crc32_no_comp(), but that
  438. * function isn't available unless you turn on JFFS support.
  439. */
  440. crc = be32_to_cpu(*(u32 *)((void *)firmware + calc_size));
  441. if (crc != (crc32(-1, (const void *)firmware, calc_size) ^ -1)) {
  442. printf("Firmware CRC is invalid\n");
  443. return -EIO;
  444. }
  445. /*
  446. * If the microcode calls for it, split the I-RAM.
  447. */
  448. if (!firmware->split) {
  449. out_be16(&qe_immr->cp.cercr,
  450. in_be16(&qe_immr->cp.cercr) | QE_CP_CERCR_CIR);
  451. }
  452. if (firmware->soc.model)
  453. printf("Firmware '%s' for %u V%u.%u\n",
  454. firmware->id, be16_to_cpu(firmware->soc.model),
  455. firmware->soc.major, firmware->soc.minor);
  456. else
  457. printf("Firmware '%s'\n", firmware->id);
  458. /*
  459. * The QE only supports one microcode per RISC, so clear out all the
  460. * saved microcode information and put in the new.
  461. */
  462. memset(&qe_firmware_info, 0, sizeof(qe_firmware_info));
  463. strncpy(qe_firmware_info.id, (char *)firmware->id, 62);
  464. qe_firmware_info.extended_modes = firmware->extended_modes;
  465. memcpy(qe_firmware_info.vtraps, firmware->vtraps,
  466. sizeof(firmware->vtraps));
  467. qe_firmware_uploaded = 1;
  468. /* Loop through each microcode. */
  469. for (i = 0; i < firmware->count; i++) {
  470. const struct qe_microcode *ucode = &firmware->microcode[i];
  471. /* Upload a microcode if it's present */
  472. if (ucode->code_offset)
  473. qe_upload_microcode(firmware, ucode);
  474. /* Program the traps for this processor */
  475. for (j = 0; j < 16; j++) {
  476. u32 trap = be32_to_cpu(ucode->traps[j]);
  477. if (trap)
  478. out_be32(&qe_immr->rsp[i].tibcr[j], trap);
  479. }
  480. /* Enable traps */
  481. out_be32(&qe_immr->rsp[i].eccr, be32_to_cpu(ucode->eccr));
  482. }
  483. return 0;
  484. }
  485. #ifdef CONFIG_U_QE
  486. /*
  487. * Upload a microcode to the I-RAM at a specific address.
  488. *
  489. * See Documentation/powerpc/qe_firmware.rst in the Linux kernel tree for
  490. * information on QE microcode uploading.
  491. *
  492. * Currently, only version 1 is supported, so the 'version' field must be
  493. * set to 1.
  494. *
  495. * The SOC model and revision are not validated, they are only displayed for
  496. * informational purposes.
  497. *
  498. * 'calc_size' is the calculated size, in bytes, of the firmware structure and
  499. * all of the microcode structures, minus the CRC.
  500. *
  501. * 'length' is the size that the structure says it is, including the CRC.
  502. */
  503. int u_qe_upload_firmware(const struct qe_firmware *firmware)
  504. {
  505. unsigned int i;
  506. unsigned int j;
  507. u32 crc;
  508. size_t calc_size = sizeof(struct qe_firmware);
  509. size_t length;
  510. const struct qe_header *hdr;
  511. #ifdef CONFIG_DEEP_SLEEP
  512. #ifdef CONFIG_ARCH_LS1021A
  513. struct ccsr_gur __iomem *gur = (void *)CFG_SYS_FSL_GUTS_ADDR;
  514. #else
  515. ccsr_gur_t __iomem *gur = (void *)(CFG_SYS_MPC85xx_GUTS_ADDR);
  516. #endif
  517. #endif
  518. if (!firmware) {
  519. printf("Invalid address\n");
  520. return -EINVAL;
  521. }
  522. hdr = &firmware->header;
  523. length = be32_to_cpu(hdr->length);
  524. /* Check the magic */
  525. if ((hdr->magic[0] != 'Q') || (hdr->magic[1] != 'E') ||
  526. (hdr->magic[2] != 'F')) {
  527. printf("Not a microcode\n");
  528. #ifdef CONFIG_DEEP_SLEEP
  529. setbits_be32(&gur->devdisr, MPC85xx_DEVDISR_QE_DISABLE);
  530. #endif
  531. return -EPERM;
  532. }
  533. /* Check the version */
  534. if (hdr->version != 1) {
  535. printf("Unsupported version\n");
  536. return -EPERM;
  537. }
  538. /* Validate some of the fields */
  539. if (firmware->count < 1 || firmware->count > MAX_QE_RISC) {
  540. printf("Invalid data\n");
  541. return -EINVAL;
  542. }
  543. /* Validate the length and check if there's a CRC */
  544. calc_size += (firmware->count - 1) * sizeof(struct qe_microcode);
  545. for (i = 0; i < firmware->count; i++)
  546. /*
  547. * For situations where the second RISC uses the same microcode
  548. * as the first, the 'code_offset' and 'count' fields will be
  549. * zero, so it's okay to add those.
  550. */
  551. calc_size += sizeof(u32) *
  552. be32_to_cpu(firmware->microcode[i].count);
  553. /* Validate the length */
  554. if (length != calc_size + sizeof(u32)) {
  555. printf("Invalid length\n");
  556. return -EPERM;
  557. }
  558. /*
  559. * Validate the CRC. We would normally call crc32_no_comp(), but that
  560. * function isn't available unless you turn on JFFS support.
  561. */
  562. crc = be32_to_cpu(*(u32 *)((void *)firmware + calc_size));
  563. if (crc != (crc32(-1, (const void *)firmware, calc_size) ^ -1)) {
  564. printf("Firmware CRC is invalid\n");
  565. return -EIO;
  566. }
  567. /*
  568. * If the microcode calls for it, split the I-RAM.
  569. */
  570. if (!firmware->split) {
  571. out_be16(&qe_immr->cp.cercr,
  572. in_be16(&qe_immr->cp.cercr) | QE_CP_CERCR_CIR);
  573. }
  574. if (firmware->soc.model)
  575. printf("Firmware '%s' for %u V%u.%u\n",
  576. firmware->id, be16_to_cpu(firmware->soc.model),
  577. firmware->soc.major, firmware->soc.minor);
  578. else
  579. printf("Firmware '%s'\n", firmware->id);
  580. /* Loop through each microcode. */
  581. for (i = 0; i < firmware->count; i++) {
  582. const struct qe_microcode *ucode = &firmware->microcode[i];
  583. /* Upload a microcode if it's present */
  584. if (ucode->code_offset)
  585. qe_upload_microcode(firmware, ucode);
  586. /* Program the traps for this processor */
  587. for (j = 0; j < 16; j++) {
  588. u32 trap = be32_to_cpu(ucode->traps[j]);
  589. if (trap)
  590. out_be32(&qe_immr->rsp[i].tibcr[j], trap);
  591. }
  592. /* Enable traps */
  593. out_be32(&qe_immr->rsp[i].eccr, be32_to_cpu(ucode->eccr));
  594. }
  595. return 0;
  596. }
  597. #endif
  598. #ifdef CONFIG_U_QE
  599. int u_qe_firmware_resume(const struct qe_firmware *firmware, qe_map_t *qe_immrr)
  600. {
  601. unsigned int i;
  602. unsigned int j;
  603. const struct qe_header *hdr;
  604. const u32 *code;
  605. #ifdef CONFIG_DEEP_SLEEP
  606. #ifdef CONFIG_PPC
  607. ccsr_gur_t __iomem *gur = (void *)(CFG_SYS_MPC85xx_GUTS_ADDR);
  608. #else
  609. struct ccsr_gur __iomem *gur = (void *)CFG_SYS_FSL_GUTS_ADDR;
  610. #endif
  611. #endif
  612. if (!firmware)
  613. return -EINVAL;
  614. hdr = &firmware->header;
  615. /* Check the magic */
  616. if ((hdr->magic[0] != 'Q') || (hdr->magic[1] != 'E') ||
  617. (hdr->magic[2] != 'F')) {
  618. #ifdef CONFIG_DEEP_SLEEP
  619. setbits_be32(&gur->devdisr, MPC85xx_DEVDISR_QE_DISABLE);
  620. #endif
  621. return -EPERM;
  622. }
  623. /*
  624. * If the microcode calls for it, split the I-RAM.
  625. */
  626. if (!firmware->split) {
  627. out_be16(&qe_immrr->cp.cercr,
  628. in_be16(&qe_immrr->cp.cercr) | QE_CP_CERCR_CIR);
  629. }
  630. /* Loop through each microcode. */
  631. for (i = 0; i < firmware->count; i++) {
  632. const struct qe_microcode *ucode = &firmware->microcode[i];
  633. /* Upload a microcode if it's present */
  634. if (!ucode->code_offset)
  635. return 0;
  636. code = (const void *)firmware + be32_to_cpu(ucode->code_offset);
  637. /* Use auto-increment */
  638. out_be32(&qe_immrr->iram.iadd, be32_to_cpu(ucode->iram_offset) |
  639. QE_IRAM_IADD_AIE | QE_IRAM_IADD_BADDR);
  640. for (i = 0; i < be32_to_cpu(ucode->count); i++)
  641. out_be32(&qe_immrr->iram.idata, be32_to_cpu(code[i]));
  642. /* Program the traps for this processor */
  643. for (j = 0; j < 16; j++) {
  644. u32 trap = be32_to_cpu(ucode->traps[j]);
  645. if (trap)
  646. out_be32(&qe_immrr->rsp[i].tibcr[j], trap);
  647. }
  648. /* Enable traps */
  649. out_be32(&qe_immrr->rsp[i].eccr, be32_to_cpu(ucode->eccr));
  650. }
  651. return 0;
  652. }
  653. #endif
  654. struct qe_firmware_info *qe_get_firmware_info(void)
  655. {
  656. return qe_firmware_uploaded ? &qe_firmware_info : NULL;
  657. }
  658. static int qe_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  659. {
  660. ulong addr;
  661. if (argc < 3)
  662. return cmd_usage(cmdtp);
  663. if (strcmp(argv[1], "fw") == 0) {
  664. addr = hextoul(argv[2], NULL);
  665. if (!addr) {
  666. printf("Invalid address\n");
  667. return -EINVAL;
  668. }
  669. /*
  670. * If a length was supplied, compare that with the 'length'
  671. * field.
  672. */
  673. if (argc > 3) {
  674. ulong length = hextoul(argv[3], NULL);
  675. struct qe_firmware *firmware = (void *)addr;
  676. if (length != be32_to_cpu(firmware->header.length)) {
  677. printf("Length mismatch\n");
  678. return -EINVAL;
  679. }
  680. }
  681. return qe_upload_firmware((const struct qe_firmware *)addr);
  682. }
  683. return cmd_usage(cmdtp);
  684. }
  685. U_BOOT_CMD(
  686. qe, 4, 0, qe_cmd,
  687. "QUICC Engine commands",
  688. "fw <addr> [<length>] - Upload firmware binary at address <addr> to the QE,\n"
  689. "\twith optional length <length> verification."
  690. );