qe.c 20 KB

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