qe.c 17 KB

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