qe.c 15 KB

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