1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2006-2009 Freescale Semiconductor, Inc.
5 * Dave Liu <daveliu@freescale.com>
6 * based on source code of Shlomi Gridish
12 #include <asm/global_data.h>
13 #include <linux/errno.h>
15 #include <linux/immap_qe.h>
18 #include <u-boot/crc.h>
20 #ifdef CONFIG_ARCH_LS1021A
21 #include <asm/arch/immap_ls102xa.h>
24 #include <asm/armv8/mmu.h>
25 #include <asm/arch/cpu.h>
28 #define MPC85xx_DEVDISR_QE_DISABLE 0x1
32 static qe_snum_t snums[QE_NUM_OF_SNUM];
35 DECLARE_GLOBAL_DATA_PTR;
37 void qe_issue_cmd(uint cmd, uint sbc, u8 mcn, u32 cmd_data)
41 if (cmd == QE_RESET) {
42 out_be32(&qe_immr->cp.cecr, (u32)(cmd | QE_CR_FLG));
44 out_be32(&qe_immr->cp.cecdr, cmd_data);
45 out_be32(&qe_immr->cp.cecr, (sbc | QE_CR_FLG |
46 ((u32)mcn << QE_CR_PROTOCOL_SHIFT) | cmd));
48 /* Wait for the QE_CR_FLG to clear */
50 cecr = in_be32(&qe_immr->cp.cecr);
51 } while (cecr & QE_CR_FLG);
55 uint qe_muram_alloc(uint size, uint align)
61 align_mask = align - 1;
62 savebase = gd->arch.mp_alloc_base;
64 off = gd->arch.mp_alloc_base & align_mask;
66 gd->arch.mp_alloc_base += (align - off);
68 off = size & align_mask;
70 size += (align - off);
72 if ((gd->arch.mp_alloc_base + size) >= gd->arch.mp_alloc_top) {
73 gd->arch.mp_alloc_base = savebase;
74 printf("%s: ran out of ram.\n", __func__);
77 retloc = gd->arch.mp_alloc_base;
78 gd->arch.mp_alloc_base += size;
80 memset((void *)&qe_immr->muram[retloc], 0, size);
82 __asm__ __volatile__("sync");
88 void *qe_muram_addr(uint offset)
90 return (void *)&qe_immr->muram[offset];
94 static void qe_sdma_init(void)
97 uint sdma_buffer_base;
99 p = (sdma_t *)&qe_immr->sdma;
101 /* All of DMA transaction in bus 1 */
102 out_be32(&p->sdaqr, 0);
103 out_be32(&p->sdaqmr, 0);
105 /* Allocate 2KB temporary buffer for sdma */
106 sdma_buffer_base = qe_muram_alloc(2048, 4096);
107 out_be32(&p->sdwbcr, sdma_buffer_base & QE_SDEBCR_BA_MASK);
109 /* Clear sdma status */
110 out_be32(&p->sdsr, 0x03000000);
112 /* Enable global mode on bus 1, and 2KB buffer size */
113 out_be32(&p->sdmr, QE_SDMR_GLB_1_MSK | (0x3 << QE_SDMR_CEN_SHIFT));
116 /* This table is a list of the serial numbers of the Threads, taken from the
117 * "SNUM Table" chart in the QE Reference Manual. The order is not important,
118 * we just need to know what the SNUMs are for the threads.
120 static u8 thread_snum[] = {
121 /* Evthreads 16-29 are not supported in MPC8309 */
122 #if !defined(CONFIG_ARCH_MPC8309)
123 0x04, 0x05, 0x0c, 0x0d,
124 0x14, 0x15, 0x1c, 0x1d,
125 0x24, 0x25, 0x2c, 0x2d,
128 0x88, 0x89, 0x98, 0x99,
129 0xa8, 0xa9, 0xb8, 0xb9,
130 0xc8, 0xc9, 0xd8, 0xd9,
131 0xe8, 0xe9, 0x08, 0x09,
132 0x18, 0x19, 0x28, 0x29,
133 0x38, 0x39, 0x48, 0x49,
134 0x58, 0x59, 0x68, 0x69,
135 0x78, 0x79, 0x80, 0x81
138 static void qe_snums_init(void)
142 for (i = 0; i < QE_NUM_OF_SNUM; i++) {
143 snums[i].state = QE_SNUM_STATE_FREE;
144 snums[i].num = thread_snum[i];
148 int qe_get_snum(void)
153 for (i = 0; i < QE_NUM_OF_SNUM; i++) {
154 if (snums[i].state == QE_SNUM_STATE_FREE) {
155 snums[i].state = QE_SNUM_STATE_USED;
164 void qe_put_snum(u8 snum)
168 for (i = 0; i < QE_NUM_OF_SNUM; i++) {
169 if (snums[i].num == snum) {
170 snums[i].state = QE_SNUM_STATE_FREE;
176 #ifdef CONFIG_TFABOOT
177 void qe_init(uint qe_base)
179 enum boot_src src = get_boot_src();
181 /* Init the QE IMMR base */
182 qe_immr = (qe_map_t *)qe_base;
184 if (src == BOOT_SOURCE_IFC_NOR) {
186 * Upload microcode to IRAM for those SOCs
187 * which do not have ROM in QE.
189 qe_upload_firmware((const void *)(CONFIG_SYS_QE_FW_ADDR +
190 CONFIG_SYS_FSL_IFC_BASE));
192 /* enable the microcode in IRAM */
193 out_be32(&qe_immr->iram.iready, QE_IRAM_READY);
196 gd->arch.mp_alloc_base = QE_DATAONLY_BASE;
197 gd->arch.mp_alloc_top = gd->arch.mp_alloc_base + QE_DATAONLY_SIZE;
203 void qe_init(uint qe_base)
205 /* Init the QE IMMR base */
206 qe_immr = (qe_map_t *)qe_base;
208 #ifdef CONFIG_SYS_QE_FMAN_FW_IN_NOR
210 * Upload microcode to IRAM for those SOCs which do not have ROM in QE.
212 qe_upload_firmware((const void *)CONFIG_SYS_QE_FW_ADDR);
214 /* enable the microcode in IRAM */
215 out_be32(&qe_immr->iram.iready, QE_IRAM_READY);
218 gd->arch.mp_alloc_base = QE_DATAONLY_BASE;
219 gd->arch.mp_alloc_top = gd->arch.mp_alloc_base + QE_DATAONLY_SIZE;
228 #ifdef CONFIG_TFABOOT
231 enum boot_src src = get_boot_src();
233 qe_immr = (qe_map_t *)(CONFIG_SYS_IMMR + QE_IMMR_OFFSET);
235 void *addr = (void *)CONFIG_SYS_QE_FW_ADDR;
237 if (src == BOOT_SOURCE_IFC_NOR)
238 addr = (void *)(CONFIG_SYS_QE_FW_ADDR +
239 CONFIG_SYS_FSL_IFC_BASE);
241 if (src == BOOT_SOURCE_QSPI_NOR)
242 addr = (void *)(CONFIG_SYS_QE_FW_ADDR +
243 CONFIG_SYS_FSL_QSPI_BASE);
245 if (src == BOOT_SOURCE_SD_MMC) {
246 int dev = CONFIG_SYS_MMC_ENV_DEV;
247 u32 cnt = CONFIG_SYS_QE_FMAN_FW_LENGTH / 512;
248 u32 blk = CONFIG_SYS_QE_FW_ADDR / 512;
250 if (mmc_initialize(gd->bd)) {
251 printf("%s: mmc_initialize() failed\n", __func__);
254 addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH);
255 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
259 printf("\nMMC cannot find device for ucode\n");
261 printf("\nMMC read: dev # %u, block # %u, count %u ...\n",
264 (void)blk_dread(mmc_get_blk_desc(mmc), blk, cnt,
268 if (!u_qe_upload_firmware(addr))
269 out_be32(&qe_immr->iram.iready, QE_IRAM_READY);
270 if (src == BOOT_SOURCE_SD_MMC)
276 qe_immr = (qe_map_t *)(CONFIG_SYS_IMMR + QE_IMMR_OFFSET);
278 void *addr = (void *)CONFIG_SYS_QE_FW_ADDR;
279 #ifdef CONFIG_SYS_QE_FMAN_FW_IN_MMC
280 int dev = CONFIG_SYS_MMC_ENV_DEV;
281 u32 cnt = CONFIG_SYS_QE_FMAN_FW_LENGTH / 512;
282 u32 blk = CONFIG_SYS_QE_FW_ADDR / 512;
284 if (mmc_initialize(gd->bd)) {
285 printf("%s: mmc_initialize() failed\n", __func__);
288 addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH);
289 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
292 printf("\nMMC cannot find device for ucode\n");
294 printf("\nMMC read: dev # %u, block # %u, count %u ...\n",
297 (void)blk_dread(mmc_get_blk_desc(mmc), blk, cnt,
301 if (!u_qe_upload_firmware(addr))
302 out_be32(&qe_immr->iram.iready, QE_IRAM_READY);
303 #ifdef CONFIG_SYS_QE_FMAN_FW_IN_MMC
311 void u_qe_resume(void)
315 qe_immrr = (qe_map_t *)(CONFIG_SYS_IMMR + QE_IMMR_OFFSET);
316 u_qe_firmware_resume((const void *)CONFIG_SYS_QE_FW_ADDR, qe_immrr);
317 out_be32(&qe_immrr->iram.iready, QE_IRAM_READY);
323 qe_issue_cmd(QE_RESET, QE_CR_SUBBLOCK_INVALID,
324 (u8)QE_CR_PROTOCOL_UNSPECIFIED, 0);
328 void qe_assign_page(uint snum, uint para_ram_base)
332 out_be32(&qe_immr->cp.cecdr, para_ram_base);
333 out_be32(&qe_immr->cp.cecr, ((u32)snum << QE_CR_ASSIGN_PAGE_SNUM_SHIFT)
334 | QE_CR_FLG | QE_ASSIGN_PAGE);
336 /* Wait for the QE_CR_FLG to clear */
338 cecr = in_be32(&qe_immr->cp.cecr);
339 } while (cecr & QE_CR_FLG);
344 * brg: 0~15 as BRG1~BRG16
346 * BRG input clock comes from the BRGCLK (internal clock generated from
347 * the QE clock, it is one-half of the QE clock), If need the clock source
348 * from CLKn pin, we have te change the function.
351 #define BRG_CLK (gd->arch.brg_clk)
354 int qe_set_brg(uint brg, uint rate)
361 if (brg >= QE_NUM_OF_BRGS)
364 bp = (uint *)&qe_immr->brg.brgc1;
367 divisor = (BRG_CLK / rate);
368 if (divisor > QE_BRGC_DIVISOR_MAX + 1) {
376 * *bp = ((divisor - 1) << QE_BRGC_DIVISOR_SHIFT) | QE_BRGC_ENABLE;
377 * __asm__ __volatile__("sync");
380 val = ((divisor - 1) << QE_BRGC_DIVISOR_SHIFT) | QE_BRGC_ENABLE;
382 val |= QE_BRGC_DIV16;
390 /* Set ethernet MII clock master */
391 int qe_set_mii_clk_src(int ucc_num)
395 /* check if the UCC number is in range. */
396 if ((ucc_num > UCC_MAX_NUM - 1) || ucc_num < 0) {
397 printf("%s: ucc num not in ranges\n", __func__);
401 cmxgcr = in_be32(&qe_immr->qmx.cmxgcr);
402 cmxgcr &= ~QE_CMXGCR_MII_ENET_MNG_MASK;
403 cmxgcr |= (ucc_num << QE_CMXGCR_MII_ENET_MNG_SHIFT);
404 out_be32(&qe_immr->qmx.cmxgcr, cmxgcr);
409 /* Firmware information stored here for qe_get_firmware_info() */
410 static struct qe_firmware_info qe_firmware_info;
413 * Set to 1 if QE firmware has been uploaded, and therefore
414 * qe_firmware_info contains valid data.
416 static int qe_firmware_uploaded;
419 * Upload a QE microcode
421 * This function is a worker function for qe_upload_firmware(). It does
422 * the actual uploading of the microcode.
424 static void qe_upload_microcode(const void *base,
425 const struct qe_microcode *ucode)
427 const u32 *code = base + be32_to_cpu(ucode->code_offset);
430 if (ucode->major || ucode->minor || ucode->revision)
431 printf("QE: uploading microcode '%s' version %u.%u.%u\n",
432 (char *)ucode->id, (u16)ucode->major, (u16)ucode->minor,
433 (u16)ucode->revision);
435 printf("QE: uploading microcode '%s'\n", (char *)ucode->id);
437 /* Use auto-increment */
438 out_be32(&qe_immr->iram.iadd, be32_to_cpu(ucode->iram_offset) |
439 QE_IRAM_IADD_AIE | QE_IRAM_IADD_BADDR);
441 for (i = 0; i < be32_to_cpu(ucode->count); i++)
442 out_be32(&qe_immr->iram.idata, be32_to_cpu(code[i]));
446 * Upload a microcode to the I-RAM at a specific address.
448 * See Documentation/powerpc/qe_firmware.rst in the Linux kernel tree for
449 * information on QE microcode uploading.
451 * Currently, only version 1 is supported, so the 'version' field must be
454 * The SOC model and revision are not validated, they are only displayed for
455 * informational purposes.
457 * 'calc_size' is the calculated size, in bytes, of the firmware structure and
458 * all of the microcode structures, minus the CRC.
460 * 'length' is the size that the structure says it is, including the CRC.
462 int qe_upload_firmware(const struct qe_firmware *firmware)
467 size_t calc_size = sizeof(struct qe_firmware);
469 const struct qe_header *hdr;
470 #ifdef CONFIG_DEEP_SLEEP
471 #ifdef CONFIG_ARCH_LS1021A
472 struct ccsr_gur __iomem *gur = (void *)CONFIG_SYS_FSL_GUTS_ADDR;
474 ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
478 printf("Invalid address\n");
482 hdr = &firmware->header;
483 length = be32_to_cpu(hdr->length);
485 /* Check the magic */
486 if ((hdr->magic[0] != 'Q') || (hdr->magic[1] != 'E') ||
487 (hdr->magic[2] != 'F')) {
488 printf("QE microcode not found\n");
489 #ifdef CONFIG_DEEP_SLEEP
490 setbits_be32(&gur->devdisr, MPC85xx_DEVDISR_QE_DISABLE);
495 /* Check the version */
496 if (hdr->version != 1) {
497 printf("Unsupported version\n");
501 /* Validate some of the fields */
502 if (firmware->count < 1 || firmware->count > MAX_QE_RISC) {
503 printf("Invalid data\n");
507 /* Validate the length and check if there's a CRC */
508 calc_size += (firmware->count - 1) * sizeof(struct qe_microcode);
510 for (i = 0; i < firmware->count; i++)
512 * For situations where the second RISC uses the same microcode
513 * as the first, the 'code_offset' and 'count' fields will be
514 * zero, so it's okay to add those.
516 calc_size += sizeof(u32) *
517 be32_to_cpu(firmware->microcode[i].count);
519 /* Validate the length */
520 if (length != calc_size + sizeof(u32)) {
521 printf("Invalid length\n");
526 * Validate the CRC. We would normally call crc32_no_comp(), but that
527 * function isn't available unless you turn on JFFS support.
529 crc = be32_to_cpu(*(u32 *)((void *)firmware + calc_size));
530 if (crc != (crc32(-1, (const void *)firmware, calc_size) ^ -1)) {
531 printf("Firmware CRC is invalid\n");
536 * If the microcode calls for it, split the I-RAM.
538 if (!firmware->split) {
539 out_be16(&qe_immr->cp.cercr,
540 in_be16(&qe_immr->cp.cercr) | QE_CP_CERCR_CIR);
543 if (firmware->soc.model)
544 printf("Firmware '%s' for %u V%u.%u\n",
545 firmware->id, be16_to_cpu(firmware->soc.model),
546 firmware->soc.major, firmware->soc.minor);
548 printf("Firmware '%s'\n", firmware->id);
551 * The QE only supports one microcode per RISC, so clear out all the
552 * saved microcode information and put in the new.
554 memset(&qe_firmware_info, 0, sizeof(qe_firmware_info));
555 strncpy(qe_firmware_info.id, (char *)firmware->id, 62);
556 qe_firmware_info.extended_modes = firmware->extended_modes;
557 memcpy(qe_firmware_info.vtraps, firmware->vtraps,
558 sizeof(firmware->vtraps));
559 qe_firmware_uploaded = 1;
561 /* Loop through each microcode. */
562 for (i = 0; i < firmware->count; i++) {
563 const struct qe_microcode *ucode = &firmware->microcode[i];
565 /* Upload a microcode if it's present */
566 if (ucode->code_offset)
567 qe_upload_microcode(firmware, ucode);
569 /* Program the traps for this processor */
570 for (j = 0; j < 16; j++) {
571 u32 trap = be32_to_cpu(ucode->traps[j]);
574 out_be32(&qe_immr->rsp[i].tibcr[j], trap);
578 out_be32(&qe_immr->rsp[i].eccr, be32_to_cpu(ucode->eccr));
586 * Upload a microcode to the I-RAM at a specific address.
588 * See Documentation/powerpc/qe_firmware.rst in the Linux kernel tree for
589 * information on QE microcode uploading.
591 * Currently, only version 1 is supported, so the 'version' field must be
594 * The SOC model and revision are not validated, they are only displayed for
595 * informational purposes.
597 * 'calc_size' is the calculated size, in bytes, of the firmware structure and
598 * all of the microcode structures, minus the CRC.
600 * 'length' is the size that the structure says it is, including the CRC.
602 int u_qe_upload_firmware(const struct qe_firmware *firmware)
607 size_t calc_size = sizeof(struct qe_firmware);
609 const struct qe_header *hdr;
610 #ifdef CONFIG_DEEP_SLEEP
611 #ifdef CONFIG_ARCH_LS1021A
612 struct ccsr_gur __iomem *gur = (void *)CONFIG_SYS_FSL_GUTS_ADDR;
614 ccsr_gur_t __iomem *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
618 printf("Invalid address\n");
622 hdr = &firmware->header;
623 length = be32_to_cpu(hdr->length);
625 /* Check the magic */
626 if ((hdr->magic[0] != 'Q') || (hdr->magic[1] != 'E') ||
627 (hdr->magic[2] != 'F')) {
628 printf("Not a microcode\n");
629 #ifdef CONFIG_DEEP_SLEEP
630 setbits_be32(&gur->devdisr, MPC85xx_DEVDISR_QE_DISABLE);
635 /* Check the version */
636 if (hdr->version != 1) {
637 printf("Unsupported version\n");
641 /* Validate some of the fields */
642 if (firmware->count < 1 || firmware->count > MAX_QE_RISC) {
643 printf("Invalid data\n");
647 /* Validate the length and check if there's a CRC */
648 calc_size += (firmware->count - 1) * sizeof(struct qe_microcode);
650 for (i = 0; i < firmware->count; i++)
652 * For situations where the second RISC uses the same microcode
653 * as the first, the 'code_offset' and 'count' fields will be
654 * zero, so it's okay to add those.
656 calc_size += sizeof(u32) *
657 be32_to_cpu(firmware->microcode[i].count);
659 /* Validate the length */
660 if (length != calc_size + sizeof(u32)) {
661 printf("Invalid length\n");
666 * Validate the CRC. We would normally call crc32_no_comp(), but that
667 * function isn't available unless you turn on JFFS support.
669 crc = be32_to_cpu(*(u32 *)((void *)firmware + calc_size));
670 if (crc != (crc32(-1, (const void *)firmware, calc_size) ^ -1)) {
671 printf("Firmware CRC is invalid\n");
676 * If the microcode calls for it, split the I-RAM.
678 if (!firmware->split) {
679 out_be16(&qe_immr->cp.cercr,
680 in_be16(&qe_immr->cp.cercr) | QE_CP_CERCR_CIR);
683 if (firmware->soc.model)
684 printf("Firmware '%s' for %u V%u.%u\n",
685 firmware->id, be16_to_cpu(firmware->soc.model),
686 firmware->soc.major, firmware->soc.minor);
688 printf("Firmware '%s'\n", firmware->id);
690 /* Loop through each microcode. */
691 for (i = 0; i < firmware->count; i++) {
692 const struct qe_microcode *ucode = &firmware->microcode[i];
694 /* Upload a microcode if it's present */
695 if (ucode->code_offset)
696 qe_upload_microcode(firmware, ucode);
698 /* Program the traps for this processor */
699 for (j = 0; j < 16; j++) {
700 u32 trap = be32_to_cpu(ucode->traps[j]);
703 out_be32(&qe_immr->rsp[i].tibcr[j], trap);
707 out_be32(&qe_immr->rsp[i].eccr, be32_to_cpu(ucode->eccr));
715 int u_qe_firmware_resume(const struct qe_firmware *firmware, qe_map_t *qe_immrr)
719 const struct qe_header *hdr;
721 #ifdef CONFIG_DEEP_SLEEP
723 ccsr_gur_t __iomem *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
725 struct ccsr_gur __iomem *gur = (void *)CONFIG_SYS_FSL_GUTS_ADDR;
732 hdr = &firmware->header;
734 /* Check the magic */
735 if ((hdr->magic[0] != 'Q') || (hdr->magic[1] != 'E') ||
736 (hdr->magic[2] != 'F')) {
737 #ifdef CONFIG_DEEP_SLEEP
738 setbits_be32(&gur->devdisr, MPC85xx_DEVDISR_QE_DISABLE);
744 * If the microcode calls for it, split the I-RAM.
746 if (!firmware->split) {
747 out_be16(&qe_immrr->cp.cercr,
748 in_be16(&qe_immrr->cp.cercr) | QE_CP_CERCR_CIR);
751 /* Loop through each microcode. */
752 for (i = 0; i < firmware->count; i++) {
753 const struct qe_microcode *ucode = &firmware->microcode[i];
755 /* Upload a microcode if it's present */
756 if (!ucode->code_offset)
759 code = (const void *)firmware + be32_to_cpu(ucode->code_offset);
761 /* Use auto-increment */
762 out_be32(&qe_immrr->iram.iadd, be32_to_cpu(ucode->iram_offset) |
763 QE_IRAM_IADD_AIE | QE_IRAM_IADD_BADDR);
765 for (i = 0; i < be32_to_cpu(ucode->count); i++)
766 out_be32(&qe_immrr->iram.idata, be32_to_cpu(code[i]));
768 /* Program the traps for this processor */
769 for (j = 0; j < 16; j++) {
770 u32 trap = be32_to_cpu(ucode->traps[j]);
773 out_be32(&qe_immrr->rsp[i].tibcr[j], trap);
777 out_be32(&qe_immrr->rsp[i].eccr, be32_to_cpu(ucode->eccr));
784 struct qe_firmware_info *qe_get_firmware_info(void)
786 return qe_firmware_uploaded ? &qe_firmware_info : NULL;
789 static int qe_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
794 return cmd_usage(cmdtp);
796 if (strcmp(argv[1], "fw") == 0) {
797 addr = hextoul(argv[2], NULL);
800 printf("Invalid address\n");
805 * If a length was supplied, compare that with the 'length'
810 ulong length = hextoul(argv[3], NULL);
811 struct qe_firmware *firmware = (void *)addr;
813 if (length != be32_to_cpu(firmware->header.length)) {
814 printf("Length mismatch\n");
819 return qe_upload_firmware((const struct qe_firmware *)addr);
822 return cmd_usage(cmdtp);
827 "QUICC Engine commands",
828 "fw <addr> [<length>] - Upload firmware binary at address <addr> to the QE,\n"
829 "\twith optional length <length> verification."