fm.c 12 KB

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