qe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include "common.h"
  23. #include <command.h>
  24. #include "asm/errno.h"
  25. #include "asm/io.h"
  26. #include "asm/immap_qe.h"
  27. #include "qe.h"
  28. qe_map_t *qe_immr = NULL;
  29. static qe_snum_t snums[QE_NUM_OF_SNUM];
  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. return;
  46. }
  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->mp_alloc_base;
  54. if ((off = (gd->mp_alloc_base & align_mask)) != 0)
  55. gd->mp_alloc_base += (align - off);
  56. if ((off = size & align_mask) != 0)
  57. size += (align - off);
  58. if ((gd->mp_alloc_base + size) >= gd->mp_alloc_top) {
  59. gd->mp_alloc_base = savebase;
  60. printf("%s: ran out of ram.\n", __FUNCTION__);
  61. }
  62. retloc = gd->mp_alloc_base;
  63. gd->mp_alloc_base += size;
  64. memset((void *)&qe_immr->muram[retloc], 0, size);
  65. __asm__ __volatile__("sync");
  66. return retloc;
  67. }
  68. void *qe_muram_addr(uint offset)
  69. {
  70. return (void *)&qe_immr->muram[offset];
  71. }
  72. static void qe_sdma_init(void)
  73. {
  74. volatile sdma_t *p;
  75. uint sdma_buffer_base;
  76. p = (volatile sdma_t *)&qe_immr->sdma;
  77. /* All of DMA transaction in bus 1 */
  78. out_be32(&p->sdaqr, 0);
  79. out_be32(&p->sdaqmr, 0);
  80. /* Allocate 2KB temporary buffer for sdma */
  81. sdma_buffer_base = qe_muram_alloc(2048, 4096);
  82. out_be32(&p->sdwbcr, sdma_buffer_base & QE_SDEBCR_BA_MASK);
  83. /* Clear sdma status */
  84. out_be32(&p->sdsr, 0x03000000);
  85. /* Enable global mode on bus 1, and 2KB buffer size */
  86. out_be32(&p->sdmr, QE_SDMR_GLB_1_MSK | (0x3 << QE_SDMR_CEN_SHIFT));
  87. }
  88. /* This table is a list of the serial numbers of the Threads, taken from the
  89. * "SNUM Table" chart in the QE Reference Manual. The order is not important,
  90. * we just need to know what the SNUMs are for the threads.
  91. */
  92. static u8 thread_snum[] = {
  93. 0x04, 0x05, 0x0c, 0x0d,
  94. 0x14, 0x15, 0x1c, 0x1d,
  95. 0x24, 0x25, 0x2c, 0x2d,
  96. 0x34, 0x35, 0x88, 0x89,
  97. 0x98, 0x99, 0xa8, 0xa9,
  98. 0xb8, 0xb9, 0xc8, 0xc9,
  99. 0xd8, 0xd9, 0xe8, 0xe9,
  100. 0x08, 0x09, 0x18, 0x19,
  101. 0x28, 0x29, 0x38, 0x39,
  102. 0x48, 0x49, 0x58, 0x59,
  103. 0x68, 0x69, 0x78, 0x79,
  104. 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_FW_ADDR
  142. /*
  143. * Upload microcode to IRAM for those SOCs which do not have ROM in QE.
  144. */
  145. qe_upload_firmware((const struct qe_firmware *) 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->mp_alloc_base = QE_DATAONLY_BASE;
  150. gd->mp_alloc_top = gd->mp_alloc_base + QE_DATAONLY_SIZE;
  151. qe_sdma_init();
  152. qe_snums_init();
  153. }
  154. void qe_reset(void)
  155. {
  156. qe_issue_cmd(QE_RESET, QE_CR_SUBBLOCK_INVALID,
  157. (u8) QE_CR_PROTOCOL_UNSPECIFIED, 0);
  158. }
  159. void qe_assign_page(uint snum, uint para_ram_base)
  160. {
  161. u32 cecr;
  162. out_be32(&qe_immr->cp.cecdr, para_ram_base);
  163. out_be32(&qe_immr->cp.cecr, ((u32) snum<<QE_CR_ASSIGN_PAGE_SNUM_SHIFT)
  164. | QE_CR_FLG | QE_ASSIGN_PAGE);
  165. /* Wait for the QE_CR_FLG to clear */
  166. do {
  167. cecr = in_be32(&qe_immr->cp.cecr);
  168. } while (cecr & QE_CR_FLG );
  169. return;
  170. }
  171. /*
  172. * brg: 0~15 as BRG1~BRG16
  173. rate: baud rate
  174. * BRG input clock comes from the BRGCLK (internal clock generated from
  175. the QE clock, it is one-half of the QE clock), If need the clock source
  176. from CLKn pin, we have te change the function.
  177. */
  178. #define BRG_CLK (gd->brg_clk)
  179. int qe_set_brg(uint brg, uint rate)
  180. {
  181. volatile uint *bp;
  182. u32 divisor;
  183. int div16 = 0;
  184. if (brg >= QE_NUM_OF_BRGS)
  185. return -EINVAL;
  186. bp = (uint *)&qe_immr->brg.brgc1;
  187. bp += brg;
  188. divisor = (BRG_CLK / rate);
  189. if (divisor > QE_BRGC_DIVISOR_MAX + 1) {
  190. div16 = 1;
  191. divisor /= 16;
  192. }
  193. *bp = ((divisor - 1) << QE_BRGC_DIVISOR_SHIFT) | QE_BRGC_ENABLE;
  194. __asm__ __volatile__("sync");
  195. if (div16) {
  196. *bp |= QE_BRGC_DIV16;
  197. __asm__ __volatile__("sync");
  198. }
  199. return 0;
  200. }
  201. /* Set ethernet MII clock master
  202. */
  203. int qe_set_mii_clk_src(int ucc_num)
  204. {
  205. u32 cmxgcr;
  206. /* check if the UCC number is in range. */
  207. if ((ucc_num > UCC_MAX_NUM - 1) || (ucc_num < 0)) {
  208. printf("%s: ucc num not in ranges\n", __FUNCTION__);
  209. return -EINVAL;
  210. }
  211. cmxgcr = in_be32(&qe_immr->qmx.cmxgcr);
  212. cmxgcr &= ~QE_CMXGCR_MII_ENET_MNG_MASK;
  213. cmxgcr |= (ucc_num <<QE_CMXGCR_MII_ENET_MNG_SHIFT);
  214. out_be32(&qe_immr->qmx.cmxgcr, cmxgcr);
  215. return 0;
  216. }
  217. /* Firmware information stored here for qe_get_firmware_info() */
  218. static struct qe_firmware_info qe_firmware_info;
  219. /*
  220. * Set to 1 if QE firmware has been uploaded, and therefore
  221. * qe_firmware_info contains valid data.
  222. */
  223. static int qe_firmware_uploaded;
  224. /*
  225. * Upload a QE microcode
  226. *
  227. * This function is a worker function for qe_upload_firmware(). It does
  228. * the actual uploading of the microcode.
  229. */
  230. static void qe_upload_microcode(const void *base,
  231. const struct qe_microcode *ucode)
  232. {
  233. const u32 *code = base + be32_to_cpu(ucode->code_offset);
  234. unsigned int i;
  235. if (ucode->major || ucode->minor || ucode->revision)
  236. printf("QE: uploading microcode '%s' version %u.%u.%u\n",
  237. ucode->id, ucode->major, ucode->minor, ucode->revision);
  238. else
  239. printf("QE: uploading microcode '%s'\n", ucode->id);
  240. /* Use auto-increment */
  241. out_be32(&qe_immr->iram.iadd, be32_to_cpu(ucode->iram_offset) |
  242. QE_IRAM_IADD_AIE | QE_IRAM_IADD_BADDR);
  243. for (i = 0; i < be32_to_cpu(ucode->count); i++)
  244. out_be32(&qe_immr->iram.idata, be32_to_cpu(code[i]));
  245. }
  246. /*
  247. * Upload a microcode to the I-RAM at a specific address.
  248. *
  249. * See docs/README.qe_firmware for information on QE microcode uploading.
  250. *
  251. * Currently, only version 1 is supported, so the 'version' field must be
  252. * set to 1.
  253. *
  254. * The SOC model and revision are not validated, they are only displayed for
  255. * informational purposes.
  256. *
  257. * 'calc_size' is the calculated size, in bytes, of the firmware structure and
  258. * all of the microcode structures, minus the CRC.
  259. *
  260. * 'length' is the size that the structure says it is, including the CRC.
  261. */
  262. int qe_upload_firmware(const struct qe_firmware *firmware)
  263. {
  264. unsigned int i;
  265. unsigned int j;
  266. u32 crc;
  267. size_t calc_size = sizeof(struct qe_firmware);
  268. size_t length;
  269. const struct qe_header *hdr;
  270. if (!firmware) {
  271. printf("Invalid address\n");
  272. return -EINVAL;
  273. }
  274. hdr = &firmware->header;
  275. length = be32_to_cpu(hdr->length);
  276. /* Check the magic */
  277. if ((hdr->magic[0] != 'Q') || (hdr->magic[1] != 'E') ||
  278. (hdr->magic[2] != 'F')) {
  279. printf("Not a microcode\n");
  280. return -EPERM;
  281. }
  282. /* Check the version */
  283. if (hdr->version != 1) {
  284. printf("Unsupported version\n");
  285. return -EPERM;
  286. }
  287. /* Validate some of the fields */
  288. if ((firmware->count < 1) || (firmware->count > MAX_QE_RISC)) {
  289. printf("Invalid data\n");
  290. return -EINVAL;
  291. }
  292. /* Validate the length and check if there's a CRC */
  293. calc_size += (firmware->count - 1) * sizeof(struct qe_microcode);
  294. for (i = 0; i < firmware->count; i++)
  295. /*
  296. * For situations where the second RISC uses the same microcode
  297. * as the first, the 'code_offset' and 'count' fields will be
  298. * zero, so it's okay to add those.
  299. */
  300. calc_size += sizeof(u32) *
  301. be32_to_cpu(firmware->microcode[i].count);
  302. /* Validate the length */
  303. if (length != calc_size + sizeof(u32)) {
  304. printf("Invalid length\n");
  305. return -EPERM;
  306. }
  307. /*
  308. * Validate the CRC. We would normally call crc32_no_comp(), but that
  309. * function isn't available unless you turn on JFFS support.
  310. */
  311. crc = be32_to_cpu(*(u32 *)((void *)firmware + calc_size));
  312. if (crc != (crc32(-1, (const void *) firmware, calc_size) ^ -1)) {
  313. printf("Firmware CRC is invalid\n");
  314. return -EIO;
  315. }
  316. /*
  317. * If the microcode calls for it, split the I-RAM.
  318. */
  319. if (!firmware->split) {
  320. out_be16(&qe_immr->cp.cercr,
  321. in_be16(&qe_immr->cp.cercr) | QE_CP_CERCR_CIR);
  322. }
  323. if (firmware->soc.model)
  324. printf("Firmware '%s' for %u V%u.%u\n",
  325. firmware->id, be16_to_cpu(firmware->soc.model),
  326. firmware->soc.major, firmware->soc.minor);
  327. else
  328. printf("Firmware '%s'\n", firmware->id);
  329. /*
  330. * The QE only supports one microcode per RISC, so clear out all the
  331. * saved microcode information and put in the new.
  332. */
  333. memset(&qe_firmware_info, 0, sizeof(qe_firmware_info));
  334. strcpy(qe_firmware_info.id, (char *)firmware->id);
  335. qe_firmware_info.extended_modes = firmware->extended_modes;
  336. memcpy(qe_firmware_info.vtraps, firmware->vtraps,
  337. sizeof(firmware->vtraps));
  338. qe_firmware_uploaded = 1;
  339. /* Loop through each microcode. */
  340. for (i = 0; i < firmware->count; i++) {
  341. const struct qe_microcode *ucode = &firmware->microcode[i];
  342. /* Upload a microcode if it's present */
  343. if (ucode->code_offset)
  344. qe_upload_microcode(firmware, ucode);
  345. /* Program the traps for this processor */
  346. for (j = 0; j < 16; j++) {
  347. u32 trap = be32_to_cpu(ucode->traps[j]);
  348. if (trap)
  349. out_be32(&qe_immr->rsp[i].tibcr[j], trap);
  350. }
  351. /* Enable traps */
  352. out_be32(&qe_immr->rsp[i].eccr, be32_to_cpu(ucode->eccr));
  353. }
  354. return 0;
  355. }
  356. struct qe_firmware_info *qe_get_firmware_info(void)
  357. {
  358. return qe_firmware_uploaded ? &qe_firmware_info : NULL;
  359. }
  360. static int qe_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  361. {
  362. ulong addr;
  363. if (argc < 3) {
  364. cmd_usage(cmdtp);
  365. return 1;
  366. }
  367. if (strcmp(argv[1], "fw") == 0) {
  368. addr = simple_strtoul(argv[2], NULL, 16);
  369. if (!addr) {
  370. printf("Invalid address\n");
  371. return -EINVAL;
  372. }
  373. /*
  374. * If a length was supplied, compare that with the 'length'
  375. * field.
  376. */
  377. if (argc > 3) {
  378. ulong length = simple_strtoul(argv[3], NULL, 16);
  379. struct qe_firmware *firmware = (void *) addr;
  380. if (length != be32_to_cpu(firmware->header.length)) {
  381. printf("Length mismatch\n");
  382. return -EINVAL;
  383. }
  384. }
  385. return qe_upload_firmware((const struct qe_firmware *) addr);
  386. }
  387. cmd_usage(cmdtp);
  388. return 1;
  389. }
  390. U_BOOT_CMD(
  391. qe, 4, 0, qe_cmd,
  392. "QUICC Engine commands",
  393. "fw <addr> [<length>] - Upload firmware binary at address <addr> to "
  394. "the QE,\n"
  395. "\twith optional length <length> verification."
  396. );