fm.c 12 KB

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