fm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Copyright 2009-2011 Freescale Semiconductor, Inc.
  3. * Dave Liu <daveliu@freescale.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <malloc.h>
  9. #include <asm/io.h>
  10. #include <asm/errno.h>
  11. #include "fm.h"
  12. #include "../../qe/qe.h" /* For struct qe_firmware */
  13. #ifdef CONFIG_SYS_QE_FMAN_FW_IN_NAND
  14. #include <nand.h>
  15. #elif defined(CONFIG_SYS_QE_FW_IN_SPIFLASH)
  16. #include <spi_flash.h>
  17. #elif defined(CONFIG_SYS_QE_FMAN_FW_IN_MMC)
  18. #include <mmc.h>
  19. #endif
  20. struct fm_muram muram[CONFIG_SYS_NUM_FMAN];
  21. u32 fm_muram_base(int fm_idx)
  22. {
  23. return muram[fm_idx].base;
  24. }
  25. u32 fm_muram_alloc(int fm_idx, u32 size, u32 align)
  26. {
  27. u32 ret;
  28. u32 align_mask, off;
  29. u32 save;
  30. align_mask = align - 1;
  31. save = muram[fm_idx].alloc;
  32. off = save & align_mask;
  33. if (off != 0)
  34. muram[fm_idx].alloc += (align - off);
  35. off = size & align_mask;
  36. if (off != 0)
  37. size += (align - off);
  38. if ((muram[fm_idx].alloc + size) >= muram[fm_idx].top) {
  39. muram[fm_idx].alloc = save;
  40. printf("%s: run out of ram.\n", __func__);
  41. }
  42. ret = muram[fm_idx].alloc;
  43. muram[fm_idx].alloc += size;
  44. memset((void *)ret, 0, size);
  45. return ret;
  46. }
  47. static void fm_init_muram(int fm_idx, void *reg)
  48. {
  49. u32 base = (u32)reg;
  50. muram[fm_idx].base = base;
  51. muram[fm_idx].size = CONFIG_SYS_FM_MURAM_SIZE;
  52. muram[fm_idx].alloc = base + FM_MURAM_RES_SIZE;
  53. muram[fm_idx].top = base + CONFIG_SYS_FM_MURAM_SIZE;
  54. }
  55. /*
  56. * fm_upload_ucode - Fman microcode upload worker function
  57. *
  58. * This function does the actual uploading of an Fman microcode
  59. * to an Fman.
  60. */
  61. static void fm_upload_ucode(int fm_idx, struct fm_imem *imem,
  62. u32 *ucode, unsigned int size)
  63. {
  64. unsigned int i;
  65. unsigned int timeout = 1000000;
  66. /* enable address auto increase */
  67. out_be32(&imem->iadd, IRAM_IADD_AIE);
  68. /* write microcode to IRAM */
  69. for (i = 0; i < size / 4; i++)
  70. out_be32(&imem->idata, ucode[i]);
  71. /* verify if the writing is over */
  72. out_be32(&imem->iadd, 0);
  73. while ((in_be32(&imem->idata) != ucode[0]) && --timeout)
  74. ;
  75. if (!timeout)
  76. printf("Fman%u: microcode upload timeout\n", fm_idx + 1);
  77. /* enable microcode from IRAM */
  78. out_be32(&imem->iready, IRAM_READY);
  79. }
  80. /*
  81. * Upload an Fman firmware
  82. *
  83. * This function is similar to qe_upload_firmware(), exception that it uploads
  84. * a microcode to the Fman instead of the QE.
  85. *
  86. * Because the process for uploading a microcode to the Fman is similar for
  87. * that of the QE, the QE firmware binary format is used for Fman microcode.
  88. * It should be possible to unify these two functions, but for now we keep them
  89. * separate.
  90. */
  91. static int fman_upload_firmware(int fm_idx,
  92. struct fm_imem *fm_imem,
  93. const struct qe_firmware *firmware)
  94. {
  95. unsigned int i;
  96. u32 crc;
  97. size_t calc_size = sizeof(struct qe_firmware);
  98. size_t length;
  99. const struct qe_header *hdr;
  100. if (!firmware) {
  101. printf("Fman%u: Invalid address for firmware\n", fm_idx + 1);
  102. return -EINVAL;
  103. }
  104. hdr = &firmware->header;
  105. length = be32_to_cpu(hdr->length);
  106. /* Check the magic */
  107. if ((hdr->magic[0] != 'Q') || (hdr->magic[1] != 'E') ||
  108. (hdr->magic[2] != 'F')) {
  109. printf("Fman%u: Data at %p is not a firmware\n", fm_idx + 1,
  110. firmware);
  111. return -EPERM;
  112. }
  113. /* Check the version */
  114. if (hdr->version != 1) {
  115. printf("Fman%u: Unsupported firmware version %u\n", fm_idx + 1,
  116. hdr->version);
  117. return -EPERM;
  118. }
  119. /* Validate some of the fields */
  120. if ((firmware->count != 1)) {
  121. printf("Fman%u: Invalid data in firmware header\n", fm_idx + 1);
  122. return -EINVAL;
  123. }
  124. /* Validate the length and check if there's a CRC */
  125. calc_size += (firmware->count - 1) * sizeof(struct qe_microcode);
  126. for (i = 0; i < firmware->count; i++)
  127. /*
  128. * For situations where the second RISC uses the same microcode
  129. * as the first, the 'code_offset' and 'count' fields will be
  130. * zero, so it's okay to add those.
  131. */
  132. calc_size += sizeof(u32) *
  133. be32_to_cpu(firmware->microcode[i].count);
  134. /* Validate the length */
  135. if (length != calc_size + sizeof(u32)) {
  136. printf("Fman%u: Invalid length in firmware header\n",
  137. fm_idx + 1);
  138. return -EPERM;
  139. }
  140. /*
  141. * Validate the CRC. We would normally call crc32_no_comp(), but that
  142. * function isn't available unless you turn on JFFS support.
  143. */
  144. crc = be32_to_cpu(*(u32 *)((void *)firmware + calc_size));
  145. if (crc != (crc32(-1, (const void *)firmware, calc_size) ^ -1)) {
  146. printf("Fman%u: Firmware CRC is invalid\n", fm_idx + 1);
  147. return -EIO;
  148. }
  149. /* Loop through each microcode. */
  150. for (i = 0; i < firmware->count; i++) {
  151. const struct qe_microcode *ucode = &firmware->microcode[i];
  152. /* Upload a microcode if it's present */
  153. if (ucode->code_offset) {
  154. u32 ucode_size;
  155. u32 *code;
  156. printf("Fman%u: Uploading microcode version %u.%u.%u\n",
  157. fm_idx + 1, ucode->major, ucode->minor,
  158. ucode->revision);
  159. code = (void *)firmware + ucode->code_offset;
  160. ucode_size = sizeof(u32) * ucode->count;
  161. fm_upload_ucode(fm_idx, fm_imem, code, ucode_size);
  162. }
  163. }
  164. return 0;
  165. }
  166. static u32 fm_assign_risc(int port_id)
  167. {
  168. u32 risc_sel, val;
  169. risc_sel = (port_id & 0x1) ? FMFPPRC_RISC2 : FMFPPRC_RISC1;
  170. val = (port_id << FMFPPRC_PORTID_SHIFT) & FMFPPRC_PORTID_MASK;
  171. val |= ((risc_sel << FMFPPRC_ORA_SHIFT) | risc_sel);
  172. return val;
  173. }
  174. static void fm_init_fpm(struct fm_fpm *fpm)
  175. {
  176. int i, port_id;
  177. u32 val;
  178. setbits_be32(&fpm->fmfpee, FMFPEE_EHM | FMFPEE_UEC |
  179. FMFPEE_CER | FMFPEE_DER);
  180. /* IM mode, each even port ID to RISC#1, each odd port ID to RISC#2 */
  181. /* offline/parser port */
  182. for (i = 0; i < MAX_NUM_OH_PORT; i++) {
  183. port_id = OH_PORT_ID_BASE + i;
  184. val = fm_assign_risc(port_id);
  185. out_be32(&fpm->fpmprc, val);
  186. }
  187. /* Rx 1G port */
  188. for (i = 0; i < MAX_NUM_RX_PORT_1G; i++) {
  189. port_id = RX_PORT_1G_BASE + i;
  190. val = fm_assign_risc(port_id);
  191. out_be32(&fpm->fpmprc, val);
  192. }
  193. /* Tx 1G port */
  194. for (i = 0; i < MAX_NUM_TX_PORT_1G; i++) {
  195. port_id = TX_PORT_1G_BASE + i;
  196. val = fm_assign_risc(port_id);
  197. out_be32(&fpm->fpmprc, val);
  198. }
  199. /* Rx 10G port */
  200. port_id = RX_PORT_10G_BASE;
  201. val = fm_assign_risc(port_id);
  202. out_be32(&fpm->fpmprc, val);
  203. /* Tx 10G port */
  204. port_id = TX_PORT_10G_BASE;
  205. val = fm_assign_risc(port_id);
  206. out_be32(&fpm->fpmprc, val);
  207. /* disable the dispatch limit in IM case */
  208. out_be32(&fpm->fpmflc, FMFP_FLC_DISP_LIM_NONE);
  209. /* clear events */
  210. out_be32(&fpm->fmfpee, FMFPEE_CLEAR_EVENT);
  211. /* clear risc events */
  212. for (i = 0; i < 4; i++)
  213. out_be32(&fpm->fpmcev[i], 0xffffffff);
  214. /* clear error */
  215. out_be32(&fpm->fpmrcr, FMFP_RCR_MDEC | FMFP_RCR_IDEC);
  216. }
  217. static int fm_init_bmi(int fm_idx, struct fm_bmi_common *bmi)
  218. {
  219. int blk, i, port_id;
  220. u32 val, offset, base;
  221. /* alloc free buffer pool in MURAM */
  222. base = fm_muram_alloc(fm_idx, FM_FREE_POOL_SIZE, FM_FREE_POOL_ALIGN);
  223. if (!base) {
  224. printf("%s: no muram for free buffer pool\n", __func__);
  225. return -ENOMEM;
  226. }
  227. offset = base - fm_muram_base(fm_idx);
  228. /* Need 128KB total free buffer pool size */
  229. val = offset / 256;
  230. blk = FM_FREE_POOL_SIZE / 256;
  231. /* in IM, we must not begin from offset 0 in MURAM */
  232. val |= ((blk - 1) << FMBM_CFG1_FBPS_SHIFT);
  233. out_be32(&bmi->fmbm_cfg1, val);
  234. /* disable all BMI interrupt */
  235. out_be32(&bmi->fmbm_ier, FMBM_IER_DISABLE_ALL);
  236. /* clear all events */
  237. out_be32(&bmi->fmbm_ievr, FMBM_IEVR_CLEAR_ALL);
  238. /*
  239. * set port parameters - FMBM_PP_x
  240. * max tasks 10G Rx/Tx=12, 1G Rx/Tx 4, others is 1
  241. * max dma 10G Rx/Tx=3, others is 1
  242. * set port FIFO size - FMBM_PFS_x
  243. * 4KB for all Rx and Tx ports
  244. */
  245. /* offline/parser port */
  246. for (i = 0; i < MAX_NUM_OH_PORT; i++) {
  247. port_id = OH_PORT_ID_BASE + i - 1;
  248. /* max tasks=1, max dma=1, no extra */
  249. out_be32(&bmi->fmbm_pp[port_id], 0);
  250. /* port FIFO size - 256 bytes, no extra */
  251. out_be32(&bmi->fmbm_pfs[port_id], 0);
  252. }
  253. /* Rx 1G port */
  254. for (i = 0; i < MAX_NUM_RX_PORT_1G; i++) {
  255. port_id = RX_PORT_1G_BASE + i - 1;
  256. /* max tasks=4, max dma=1, no extra */
  257. out_be32(&bmi->fmbm_pp[port_id], FMBM_PP_MXT(4));
  258. /* FIFO size - 4KB, no extra */
  259. out_be32(&bmi->fmbm_pfs[port_id], FMBM_PFS_IFSZ(0xf));
  260. }
  261. /* Tx 1G port FIFO size - 4KB, no extra */
  262. for (i = 0; i < MAX_NUM_TX_PORT_1G; i++) {
  263. port_id = TX_PORT_1G_BASE + i - 1;
  264. /* max tasks=4, max dma=1, no extra */
  265. out_be32(&bmi->fmbm_pp[port_id], FMBM_PP_MXT(4));
  266. /* FIFO size - 4KB, no extra */
  267. out_be32(&bmi->fmbm_pfs[port_id], FMBM_PFS_IFSZ(0xf));
  268. }
  269. /* Rx 10G port */
  270. port_id = RX_PORT_10G_BASE - 1;
  271. /* max tasks=12, max dma=3, no extra */
  272. out_be32(&bmi->fmbm_pp[port_id], FMBM_PP_MXT(12) | FMBM_PP_MXD(3));
  273. /* FIFO size - 4KB, no extra */
  274. out_be32(&bmi->fmbm_pfs[port_id], FMBM_PFS_IFSZ(0xf));
  275. /* Tx 10G port */
  276. port_id = TX_PORT_10G_BASE - 1;
  277. /* max tasks=12, max dma=3, no extra */
  278. out_be32(&bmi->fmbm_pp[port_id], FMBM_PP_MXT(12) | FMBM_PP_MXD(3));
  279. /* FIFO size - 4KB, no extra */
  280. out_be32(&bmi->fmbm_pfs[port_id], FMBM_PFS_IFSZ(0xf));
  281. /* initialize internal buffers data base (linked list) */
  282. out_be32(&bmi->fmbm_init, FMBM_INIT_START);
  283. return 0;
  284. }
  285. static void fm_init_qmi(struct fm_qmi_common *qmi)
  286. {
  287. /* disable enqueue and dequeue of QMI */
  288. clrbits_be32(&qmi->fmqm_gc, FMQM_GC_ENQ_EN | FMQM_GC_DEQ_EN);
  289. /* disable all error interrupts */
  290. out_be32(&qmi->fmqm_eien, FMQM_EIEN_DISABLE_ALL);
  291. /* clear all error events */
  292. out_be32(&qmi->fmqm_eie, FMQM_EIE_CLEAR_ALL);
  293. /* disable all interrupts */
  294. out_be32(&qmi->fmqm_ien, FMQM_IEN_DISABLE_ALL);
  295. /* clear all interrupts */
  296. out_be32(&qmi->fmqm_ie, FMQM_IE_CLEAR_ALL);
  297. }
  298. /* Init common part of FM, index is fm num# like fm as above */
  299. int fm_init_common(int index, struct ccsr_fman *reg)
  300. {
  301. int rc;
  302. #if defined(CONFIG_SYS_QE_FMAN_FW_IN_NOR)
  303. void *addr = (void *)CONFIG_SYS_QE_FMAN_FW_ADDR;
  304. #elif defined(CONFIG_SYS_QE_FMAN_FW_IN_NAND)
  305. size_t fw_length = CONFIG_SYS_QE_FMAN_FW_LENGTH;
  306. void *addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH);
  307. rc = nand_read(&nand_info[0], (loff_t)CONFIG_SYS_QE_FMAN_FW_ADDR,
  308. &fw_length, (u_char *)addr);
  309. if (rc == -EUCLEAN) {
  310. printf("NAND read of FMAN firmware at offset 0x%x failed %d\n",
  311. CONFIG_SYS_QE_FMAN_FW_ADDR, rc);
  312. }
  313. #elif defined(CONFIG_SYS_QE_FW_IN_SPIFLASH)
  314. struct spi_flash *ucode_flash;
  315. void *addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH);
  316. int ret = 0;
  317. ucode_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
  318. CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
  319. if (!ucode_flash)
  320. printf("SF: probe for ucode failed\n");
  321. else {
  322. ret = spi_flash_read(ucode_flash, CONFIG_SYS_QE_FMAN_FW_ADDR,
  323. CONFIG_SYS_QE_FMAN_FW_LENGTH, addr);
  324. if (ret)
  325. printf("SF: read for ucode failed\n");
  326. spi_flash_free(ucode_flash);
  327. }
  328. #elif defined(CONFIG_SYS_QE_FMAN_FW_IN_MMC)
  329. int dev = CONFIG_SYS_MMC_ENV_DEV;
  330. void *addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH);
  331. u32 cnt = CONFIG_SYS_QE_FMAN_FW_LENGTH / 512;
  332. u32 blk = CONFIG_SYS_QE_FMAN_FW_ADDR / 512;
  333. struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
  334. if (!mmc)
  335. printf("\nMMC cannot find device for ucode\n");
  336. else {
  337. printf("\nMMC read: dev # %u, block # %u, count %u ...\n",
  338. dev, blk, cnt);
  339. mmc_init(mmc);
  340. (void)mmc->block_dev.block_read(dev, blk, cnt, addr);
  341. /* flush cache after read */
  342. flush_cache((ulong)addr, cnt * 512);
  343. }
  344. #elif defined(CONFIG_SYS_QE_FMAN_FW_IN_REMOTE)
  345. void *addr = (void *)CONFIG_SYS_QE_FMAN_FW_ADDR;
  346. #else
  347. void *addr = NULL;
  348. #endif
  349. /* Upload the Fman microcode if it's present */
  350. rc = fman_upload_firmware(index, &reg->fm_imem, addr);
  351. if (rc)
  352. return rc;
  353. setenv_addr("fman_ucode", addr);
  354. fm_init_muram(index, &reg->muram);
  355. fm_init_qmi(&reg->fm_qmi_common);
  356. fm_init_fpm(&reg->fm_fpm);
  357. /* clear DMA status */
  358. setbits_be32(&reg->fm_dma.fmdmsr, FMDMSR_CLEAR_ALL);
  359. /* set DMA mode */
  360. setbits_be32(&reg->fm_dma.fmdmmr, FMDMMR_SBER);
  361. return fm_init_bmi(index, &reg->fm_bmi_common);
  362. }