blob: a8caa0f0927dbc60cc6f4bc367ece39d1678efb1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kumar Gala2683c532011-04-13 08:37:44 -05002/*
3 * Copyright 2009-2011 Freescale Semiconductor, Inc.
4 * Dave Liu <daveliu@freescale.com>
Kumar Gala2683c532011-04-13 08:37:44 -05005 */
Simon Glass313112a2019-08-01 09:46:46 -06006#include <env.h>
Sean Andersonb55b7502022-12-29 11:53:01 -05007#include <fs_loader.h>
Sean Anderson9a2c7732022-09-07 13:44:55 +08008#include <image.h>
Kumar Gala2683c532011-04-13 08:37:44 -05009#include <malloc.h>
10#include <asm/io.h>
Sean Anderson394b4ef2022-12-29 11:53:00 -050011#include <dm/device_compat.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090012#include <linux/errno.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070013#include <u-boot/crc.h>
Madalin Bucurb76b0a62020-04-23 16:25:19 +030014#include <dm.h>
Kumar Gala2683c532011-04-13 08:37:44 -050015
16#include "fm.h"
Qianyu Gongae6a7582016-02-18 13:01:59 +080017#include <fsl_qe.h> /* For struct qe_firmware */
Kumar Gala2683c532011-04-13 08:37:44 -050018
Kumar Gala2683c532011-04-13 08:37:44 -050019#include <nand.h>
Kumar Gala2683c532011-04-13 08:37:44 -050020#include <spi_flash.h>
Kumar Gala2683c532011-04-13 08:37:44 -050021#include <mmc.h>
Rajesh Bhagat1dec9612018-11-05 18:02:23 +000022
23#ifdef CONFIG_ARM64
24#include <asm/armv8/mmu.h>
25#include <asm/arch/cpu.h>
Kumar Gala2683c532011-04-13 08:37:44 -050026#endif
27
Tom Rini0a2bac72022-11-16 13:10:29 -050028struct fm_muram muram[CFG_SYS_NUM_FMAN];
Kumar Gala2683c532011-04-13 08:37:44 -050029
Hou Zhiqiangea52d332015-10-26 19:47:44 +080030void *fm_muram_base(int fm_idx)
Kumar Gala2683c532011-04-13 08:37:44 -050031{
32 return muram[fm_idx].base;
33}
34
Hou Zhiqiangea52d332015-10-26 19:47:44 +080035void *fm_muram_alloc(int fm_idx, size_t size, ulong align)
Kumar Gala2683c532011-04-13 08:37:44 -050036{
Hou Zhiqiangea52d332015-10-26 19:47:44 +080037 void *ret;
38 ulong align_mask;
39 size_t off;
40 void *save;
Kumar Gala2683c532011-04-13 08:37:44 -050041
42 align_mask = align - 1;
43 save = muram[fm_idx].alloc;
44
Hou Zhiqiangea52d332015-10-26 19:47:44 +080045 off = (ulong)save & align_mask;
Kumar Gala2683c532011-04-13 08:37:44 -050046 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__);
Hou Zhiqiangea52d332015-10-26 19:47:44 +080054 return NULL;
Kumar Gala2683c532011-04-13 08:37:44 -050055 }
56
57 ret = muram[fm_idx].alloc;
58 muram[fm_idx].alloc += size;
59 memset((void *)ret, 0, size);
60
61 return ret;
62}
63
64static void fm_init_muram(int fm_idx, void *reg)
65{
Hou Zhiqiangea52d332015-10-26 19:47:44 +080066 void *base = reg;
Kumar Gala2683c532011-04-13 08:37:44 -050067
68 muram[fm_idx].base = base;
Tom Rini6a5dccc2022-11-16 13:10:41 -050069 muram[fm_idx].size = CFG_SYS_FM_MURAM_SIZE;
Kumar Gala2683c532011-04-13 08:37:44 -050070 muram[fm_idx].alloc = base + FM_MURAM_RES_SIZE;
Tom Rini6a5dccc2022-11-16 13:10:41 -050071 muram[fm_idx].top = base + CFG_SYS_FM_MURAM_SIZE;
Kumar Gala2683c532011-04-13 08:37:44 -050072}
73
74/*
75 * fm_upload_ucode - Fman microcode upload worker function
76 *
77 * This function does the actual uploading of an Fman microcode
78 * to an Fman.
79 */
80static void fm_upload_ucode(int fm_idx, struct fm_imem *imem,
81 u32 *ucode, unsigned int size)
82{
83 unsigned int i;
84 unsigned int timeout = 1000000;
85
86 /* enable address auto increase */
87 out_be32(&imem->iadd, IRAM_IADD_AIE);
88 /* write microcode to IRAM */
89 for (i = 0; i < size / 4; i++)
Hou Zhiqiang3a25ece2015-10-26 19:47:43 +080090 out_be32(&imem->idata, (be32_to_cpu(ucode[i])));
Kumar Gala2683c532011-04-13 08:37:44 -050091
92 /* verify if the writing is over */
93 out_be32(&imem->iadd, 0);
Hou Zhiqiang3a25ece2015-10-26 19:47:43 +080094 while ((in_be32(&imem->idata) != be32_to_cpu(ucode[0])) && --timeout)
Kumar Gala2683c532011-04-13 08:37:44 -050095 ;
96 if (!timeout)
97 printf("Fman%u: microcode upload timeout\n", fm_idx + 1);
98
99 /* enable microcode from IRAM */
100 out_be32(&imem->iready, IRAM_READY);
101}
102
103/*
104 * Upload an Fman firmware
105 *
106 * This function is similar to qe_upload_firmware(), exception that it uploads
107 * a microcode to the Fman instead of the QE.
108 *
109 * Because the process for uploading a microcode to the Fman is similar for
110 * that of the QE, the QE firmware binary format is used for Fman microcode.
111 * It should be possible to unify these two functions, but for now we keep them
112 * separate.
113 */
114static int fman_upload_firmware(int fm_idx,
115 struct fm_imem *fm_imem,
116 const struct qe_firmware *firmware)
117{
118 unsigned int i;
119 u32 crc;
120 size_t calc_size = sizeof(struct qe_firmware);
121 size_t length;
122 const struct qe_header *hdr;
123
124 if (!firmware) {
125 printf("Fman%u: Invalid address for firmware\n", fm_idx + 1);
126 return -EINVAL;
127 }
128
129 hdr = &firmware->header;
130 length = be32_to_cpu(hdr->length);
131
132 /* Check the magic */
133 if ((hdr->magic[0] != 'Q') || (hdr->magic[1] != 'E') ||
134 (hdr->magic[2] != 'F')) {
135 printf("Fman%u: Data at %p is not a firmware\n", fm_idx + 1,
136 firmware);
137 return -EPERM;
138 }
139
140 /* Check the version */
141 if (hdr->version != 1) {
142 printf("Fman%u: Unsupported firmware version %u\n", fm_idx + 1,
143 hdr->version);
144 return -EPERM;
145 }
146
147 /* Validate some of the fields */
148 if ((firmware->count != 1)) {
149 printf("Fman%u: Invalid data in firmware header\n", fm_idx + 1);
150 return -EINVAL;
151 }
152
153 /* Validate the length and check if there's a CRC */
154 calc_size += (firmware->count - 1) * sizeof(struct qe_microcode);
155
156 for (i = 0; i < firmware->count; i++)
157 /*
158 * For situations where the second RISC uses the same microcode
159 * as the first, the 'code_offset' and 'count' fields will be
160 * zero, so it's okay to add those.
161 */
162 calc_size += sizeof(u32) *
163 be32_to_cpu(firmware->microcode[i].count);
164
165 /* Validate the length */
166 if (length != calc_size + sizeof(u32)) {
167 printf("Fman%u: Invalid length in firmware header\n",
168 fm_idx + 1);
169 return -EPERM;
170 }
171
172 /*
173 * Validate the CRC. We would normally call crc32_no_comp(), but that
174 * function isn't available unless you turn on JFFS support.
175 */
176 crc = be32_to_cpu(*(u32 *)((void *)firmware + calc_size));
177 if (crc != (crc32(-1, (const void *)firmware, calc_size) ^ -1)) {
178 printf("Fman%u: Firmware CRC is invalid\n", fm_idx + 1);
179 return -EIO;
180 }
181
182 /* Loop through each microcode. */
183 for (i = 0; i < firmware->count; i++) {
184 const struct qe_microcode *ucode = &firmware->microcode[i];
185
186 /* Upload a microcode if it's present */
Hou Zhiqiang3a25ece2015-10-26 19:47:43 +0800187 if (be32_to_cpu(ucode->code_offset)) {
Kumar Gala2683c532011-04-13 08:37:44 -0500188 u32 ucode_size;
189 u32 *code;
190 printf("Fman%u: Uploading microcode version %u.%u.%u\n",
191 fm_idx + 1, ucode->major, ucode->minor,
192 ucode->revision);
Hou Zhiqiang3a25ece2015-10-26 19:47:43 +0800193 code = (void *)firmware +
194 be32_to_cpu(ucode->code_offset);
195 ucode_size = sizeof(u32) * be32_to_cpu(ucode->count);
Kumar Gala2683c532011-04-13 08:37:44 -0500196 fm_upload_ucode(fm_idx, fm_imem, code, ucode_size);
197 }
198 }
199
200 return 0;
201}
202
203static u32 fm_assign_risc(int port_id)
204{
205 u32 risc_sel, val;
206 risc_sel = (port_id & 0x1) ? FMFPPRC_RISC2 : FMFPPRC_RISC1;
207 val = (port_id << FMFPPRC_PORTID_SHIFT) & FMFPPRC_PORTID_MASK;
208 val |= ((risc_sel << FMFPPRC_ORA_SHIFT) | risc_sel);
209
210 return val;
211}
212
213static void fm_init_fpm(struct fm_fpm *fpm)
214{
215 int i, port_id;
216 u32 val;
217
218 setbits_be32(&fpm->fmfpee, FMFPEE_EHM | FMFPEE_UEC |
219 FMFPEE_CER | FMFPEE_DER);
220
221 /* IM mode, each even port ID to RISC#1, each odd port ID to RISC#2 */
222
223 /* offline/parser port */
224 for (i = 0; i < MAX_NUM_OH_PORT; i++) {
225 port_id = OH_PORT_ID_BASE + i;
226 val = fm_assign_risc(port_id);
227 out_be32(&fpm->fpmprc, val);
228 }
229 /* Rx 1G port */
230 for (i = 0; i < MAX_NUM_RX_PORT_1G; i++) {
231 port_id = RX_PORT_1G_BASE + i;
232 val = fm_assign_risc(port_id);
233 out_be32(&fpm->fpmprc, val);
234 }
235 /* Tx 1G port */
236 for (i = 0; i < MAX_NUM_TX_PORT_1G; i++) {
237 port_id = TX_PORT_1G_BASE + i;
238 val = fm_assign_risc(port_id);
239 out_be32(&fpm->fpmprc, val);
240 }
241 /* Rx 10G port */
242 port_id = RX_PORT_10G_BASE;
243 val = fm_assign_risc(port_id);
244 out_be32(&fpm->fpmprc, val);
245 /* Tx 10G port */
246 port_id = TX_PORT_10G_BASE;
247 val = fm_assign_risc(port_id);
248 out_be32(&fpm->fpmprc, val);
249
250 /* disable the dispatch limit in IM case */
251 out_be32(&fpm->fpmflc, FMFP_FLC_DISP_LIM_NONE);
252 /* clear events */
253 out_be32(&fpm->fmfpee, FMFPEE_CLEAR_EVENT);
254
255 /* clear risc events */
256 for (i = 0; i < 4; i++)
257 out_be32(&fpm->fpmcev[i], 0xffffffff);
258
259 /* clear error */
260 out_be32(&fpm->fpmrcr, FMFP_RCR_MDEC | FMFP_RCR_IDEC);
261}
262
263static int fm_init_bmi(int fm_idx, struct fm_bmi_common *bmi)
264{
265 int blk, i, port_id;
Hou Zhiqiangea52d332015-10-26 19:47:44 +0800266 u32 val;
267 size_t offset;
268 void *base;
Kumar Gala2683c532011-04-13 08:37:44 -0500269
270 /* alloc free buffer pool in MURAM */
271 base = fm_muram_alloc(fm_idx, FM_FREE_POOL_SIZE, FM_FREE_POOL_ALIGN);
272 if (!base) {
273 printf("%s: no muram for free buffer pool\n", __func__);
274 return -ENOMEM;
275 }
276 offset = base - fm_muram_base(fm_idx);
277
278 /* Need 128KB total free buffer pool size */
279 val = offset / 256;
280 blk = FM_FREE_POOL_SIZE / 256;
281 /* in IM, we must not begin from offset 0 in MURAM */
282 val |= ((blk - 1) << FMBM_CFG1_FBPS_SHIFT);
283 out_be32(&bmi->fmbm_cfg1, val);
284
285 /* disable all BMI interrupt */
286 out_be32(&bmi->fmbm_ier, FMBM_IER_DISABLE_ALL);
287
288 /* clear all events */
289 out_be32(&bmi->fmbm_ievr, FMBM_IEVR_CLEAR_ALL);
290
291 /*
292 * set port parameters - FMBM_PP_x
293 * max tasks 10G Rx/Tx=12, 1G Rx/Tx 4, others is 1
294 * max dma 10G Rx/Tx=3, others is 1
295 * set port FIFO size - FMBM_PFS_x
296 * 4KB for all Rx and Tx ports
297 */
298 /* offline/parser port */
299 for (i = 0; i < MAX_NUM_OH_PORT; i++) {
300 port_id = OH_PORT_ID_BASE + i - 1;
301 /* max tasks=1, max dma=1, no extra */
302 out_be32(&bmi->fmbm_pp[port_id], 0);
303 /* port FIFO size - 256 bytes, no extra */
304 out_be32(&bmi->fmbm_pfs[port_id], 0);
305 }
306 /* Rx 1G port */
307 for (i = 0; i < MAX_NUM_RX_PORT_1G; i++) {
308 port_id = RX_PORT_1G_BASE + i - 1;
309 /* max tasks=4, max dma=1, no extra */
310 out_be32(&bmi->fmbm_pp[port_id], FMBM_PP_MXT(4));
311 /* FIFO size - 4KB, no extra */
312 out_be32(&bmi->fmbm_pfs[port_id], FMBM_PFS_IFSZ(0xf));
313 }
314 /* Tx 1G port FIFO size - 4KB, no extra */
315 for (i = 0; i < MAX_NUM_TX_PORT_1G; i++) {
316 port_id = TX_PORT_1G_BASE + i - 1;
317 /* max tasks=4, max dma=1, no extra */
318 out_be32(&bmi->fmbm_pp[port_id], FMBM_PP_MXT(4));
319 /* FIFO size - 4KB, no extra */
320 out_be32(&bmi->fmbm_pfs[port_id], FMBM_PFS_IFSZ(0xf));
321 }
322 /* Rx 10G port */
323 port_id = RX_PORT_10G_BASE - 1;
324 /* max tasks=12, max dma=3, no extra */
325 out_be32(&bmi->fmbm_pp[port_id], FMBM_PP_MXT(12) | FMBM_PP_MXD(3));
326 /* FIFO size - 4KB, no extra */
327 out_be32(&bmi->fmbm_pfs[port_id], FMBM_PFS_IFSZ(0xf));
328
329 /* Tx 10G port */
330 port_id = TX_PORT_10G_BASE - 1;
331 /* max tasks=12, max dma=3, no extra */
332 out_be32(&bmi->fmbm_pp[port_id], FMBM_PP_MXT(12) | FMBM_PP_MXD(3));
333 /* FIFO size - 4KB, no extra */
334 out_be32(&bmi->fmbm_pfs[port_id], FMBM_PFS_IFSZ(0xf));
335
336 /* initialize internal buffers data base (linked list) */
337 out_be32(&bmi->fmbm_init, FMBM_INIT_START);
338
339 return 0;
340}
341
342static void fm_init_qmi(struct fm_qmi_common *qmi)
343{
Kumar Gala2683c532011-04-13 08:37:44 -0500344 /* disable all error interrupts */
345 out_be32(&qmi->fmqm_eien, FMQM_EIEN_DISABLE_ALL);
346 /* clear all error events */
347 out_be32(&qmi->fmqm_eie, FMQM_EIE_CLEAR_ALL);
348
349 /* disable all interrupts */
350 out_be32(&qmi->fmqm_ien, FMQM_IEN_DISABLE_ALL);
351 /* clear all interrupts */
352 out_be32(&qmi->fmqm_ie, FMQM_IE_CLEAR_ALL);
353}
354
355/* Init common part of FM, index is fm num# like fm as above */
Rajesh Bhagat1dec9612018-11-05 18:02:23 +0000356#ifdef CONFIG_TFABOOT
Sean Anderson394b4ef2022-12-29 11:53:00 -0500357int fm_init_common(int index, struct ccsr_fman *reg, const char *firmware_name)
Kumar Gala2683c532011-04-13 08:37:44 -0500358{
359 int rc;
Rajesh Bhagat1dec9612018-11-05 18:02:23 +0000360 void *addr = NULL;
361 enum boot_src src = get_boot_src();
362
363 if (src == BOOT_SOURCE_IFC_NOR) {
364 addr = (void *)(CONFIG_SYS_FMAN_FW_ADDR +
Tom Rini364d0022023-01-10 11:19:45 -0500365 CFG_SYS_FSL_IFC_BASE);
Francois Gervais28ff2ef2020-04-08 09:48:12 -0400366#ifdef CONFIG_CMD_NAND
Rajesh Bhagat1dec9612018-11-05 18:02:23 +0000367 } else if (src == BOOT_SOURCE_IFC_NAND) {
368 size_t fw_length = CONFIG_SYS_QE_FMAN_FW_LENGTH;
369
370 addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH);
371
372 rc = nand_read(get_nand_dev_by_index(0),
373 (loff_t)CONFIG_SYS_FMAN_FW_ADDR,
374 &fw_length, (u_char *)addr);
375 if (rc == -EUCLEAN) {
376 printf("NAND read of FMAN firmware at offset 0x%x failed %d\n",
377 CONFIG_SYS_FMAN_FW_ADDR, rc);
378 }
Francois Gervais28ff2ef2020-04-08 09:48:12 -0400379#endif
Rajesh Bhagat1dec9612018-11-05 18:02:23 +0000380 } else if (src == BOOT_SOURCE_QSPI_NOR) {
381 struct spi_flash *ucode_flash;
382
383 addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH);
384 int ret = 0;
385
Lukasz Majewski76f442982020-06-04 23:11:53 +0800386#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
Rajesh Bhagat1dec9612018-11-05 18:02:23 +0000387 struct udevice *new;
388
389 /* speed and mode will be read from DT */
Tom Rini119d2fb2021-12-11 14:55:48 -0500390 ret = spi_flash_probe_bus_cs(CONFIG_SF_DEFAULT_BUS,
Patrice Chotard05b1aa62022-03-30 09:33:14 +0200391 CONFIG_SF_DEFAULT_CS, &new);
Rajesh Bhagat1dec9612018-11-05 18:02:23 +0000392
393 ucode_flash = dev_get_uclass_priv(new);
394#else
395 ucode_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
396 CONFIG_ENV_SPI_CS,
397 CONFIG_ENV_SPI_MAX_HZ,
398 CONFIG_ENV_SPI_MODE);
399#endif
400 if (!ucode_flash) {
401 printf("SF: probe for ucode failed\n");
402 } else {
403 ret = spi_flash_read(ucode_flash,
404 CONFIG_SYS_FMAN_FW_ADDR +
Tom Rini376b88a2022-10-28 20:27:13 -0400405 CFG_SYS_FSL_QSPI_BASE,
Rajesh Bhagat1dec9612018-11-05 18:02:23 +0000406 CONFIG_SYS_QE_FMAN_FW_LENGTH,
407 addr);
408 if (ret)
409 printf("SF: read for ucode failed\n");
410 spi_flash_free(ucode_flash);
411 }
412 } else if (src == BOOT_SOURCE_SD_MMC) {
413 int dev = CONFIG_SYS_MMC_ENV_DEV;
414
415 addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH);
416 u32 cnt = CONFIG_SYS_QE_FMAN_FW_LENGTH / 512;
417 u32 blk = CONFIG_SYS_FMAN_FW_ADDR / 512;
418 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
419
420 if (!mmc) {
421 printf("\nMMC cannot find device for ucode\n");
422 } else {
423 printf("\nMMC read: dev # %u, block # %u, count %u ...\n",
424 dev, blk, cnt);
425 mmc_init(mmc);
426 (void)blk_dread(mmc_get_blk_desc(mmc), blk, cnt,
427 addr);
428 }
429 } else {
430 addr = NULL;
431 }
432
433 /* Upload the Fman microcode if it's present */
434 rc = fman_upload_firmware(index, &reg->fm_imem, addr);
435 if (rc)
436 return rc;
437 env_set_addr("fman_ucode", addr);
438
439 fm_init_muram(index, &reg->muram);
440 fm_init_qmi(&reg->fm_qmi_common);
441 fm_init_fpm(&reg->fm_fpm);
442
443 /* clear DMA status */
444 setbits_be32(&reg->fm_dma.fmdmsr, FMDMSR_CLEAR_ALL);
445
446 /* set DMA mode */
447 setbits_be32(&reg->fm_dma.fmdmmr, FMDMMR_SBER);
448
449 return fm_init_bmi(index, &reg->fm_bmi_common);
450}
451#else
Sean Anderson394b4ef2022-12-29 11:53:00 -0500452int fm_init_common(int index, struct ccsr_fman *reg, const char *firmware_name)
Rajesh Bhagat1dec9612018-11-05 18:02:23 +0000453{
454 int rc;
Sean Andersonb55b7502022-12-29 11:53:01 -0500455#if defined(CONFIG_SYS_QE_FMAN_FW_IN_FS)
456 struct udevice *fs_loader;
457 void *addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH);
458
459 if (!addr)
460 return -ENOMEM;
461
462 rc = get_fs_loader(&fs_loader);
463 if (rc) {
464 debug("could not get fs loader: %d\n", rc);
465 return rc;
466 }
467
468 if (!firmware_name)
469 firmware_name = "fman.itb";
470
471 rc = request_firmware_into_buf(fs_loader, firmware_name, addr,
472 CONFIG_SYS_QE_FMAN_FW_LENGTH, 0);
473 if (rc < 0) {
474 debug("could not request %s: %d\n", firmware_name, rc);
475 return rc;
476 }
477#elif defined(CONFIG_SYS_QE_FMAN_FW_IN_NOR)
Zhao Qiang83a90842014-03-21 16:21:44 +0800478 void *addr = (void *)CONFIG_SYS_FMAN_FW_ADDR;
Timur Tabi275f4bb2011-11-22 09:21:25 -0600479#elif defined(CONFIG_SYS_QE_FMAN_FW_IN_NAND)
480 size_t fw_length = CONFIG_SYS_QE_FMAN_FW_LENGTH;
481 void *addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH);
Kumar Gala2683c532011-04-13 08:37:44 -0500482
Grygorii Strashkoaa942fe2017-06-26 19:13:00 -0500483 rc = nand_read(get_nand_dev_by_index(0),
484 (loff_t)CONFIG_SYS_FMAN_FW_ADDR,
Kumar Gala2683c532011-04-13 08:37:44 -0500485 &fw_length, (u_char *)addr);
486 if (rc == -EUCLEAN) {
487 printf("NAND read of FMAN firmware at offset 0x%x failed %d\n",
Zhao Qiang83a90842014-03-21 16:21:44 +0800488 CONFIG_SYS_FMAN_FW_ADDR, rc);
Kumar Gala2683c532011-04-13 08:37:44 -0500489 }
Tom Rinifa911f82019-05-12 07:59:12 -0400490#elif defined(CONFIG_SYS_QE_FMAN_FW_IN_SPIFLASH)
Kumar Gala2683c532011-04-13 08:37:44 -0500491 struct spi_flash *ucode_flash;
Timur Tabi275f4bb2011-11-22 09:21:25 -0600492 void *addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH);
Kumar Gala2683c532011-04-13 08:37:44 -0500493 int ret = 0;
494
Lukasz Majewski76f442982020-06-04 23:11:53 +0800495#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
Qianyu Gongea4923e2016-08-03 11:04:25 +0800496 struct udevice *new;
497
498 /* speed and mode will be read from DT */
Tom Rini119d2fb2021-12-11 14:55:48 -0500499 ret = spi_flash_probe_bus_cs(CONFIG_SF_DEFAULT_BUS, CONFIG_SF_DEFAULT_CS,
Patrice Chotard05b1aa62022-03-30 09:33:14 +0200500 &new);
Qianyu Gongea4923e2016-08-03 11:04:25 +0800501
502 ucode_flash = dev_get_uclass_priv(new);
503#else
Kumar Gala2683c532011-04-13 08:37:44 -0500504 ucode_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
505 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Qianyu Gongea4923e2016-08-03 11:04:25 +0800506#endif
Kumar Gala2683c532011-04-13 08:37:44 -0500507 if (!ucode_flash)
508 printf("SF: probe for ucode failed\n");
509 else {
Zhao Qiang83a90842014-03-21 16:21:44 +0800510 ret = spi_flash_read(ucode_flash, CONFIG_SYS_FMAN_FW_ADDR,
Timur Tabi275f4bb2011-11-22 09:21:25 -0600511 CONFIG_SYS_QE_FMAN_FW_LENGTH, addr);
Kumar Gala2683c532011-04-13 08:37:44 -0500512 if (ret)
513 printf("SF: read for ucode failed\n");
514 spi_flash_free(ucode_flash);
515 }
Timur Tabi275f4bb2011-11-22 09:21:25 -0600516#elif defined(CONFIG_SYS_QE_FMAN_FW_IN_MMC)
Kumar Gala2683c532011-04-13 08:37:44 -0500517 int dev = CONFIG_SYS_MMC_ENV_DEV;
Timur Tabi275f4bb2011-11-22 09:21:25 -0600518 void *addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH);
519 u32 cnt = CONFIG_SYS_QE_FMAN_FW_LENGTH / 512;
Zhao Qiang83a90842014-03-21 16:21:44 +0800520 u32 blk = CONFIG_SYS_FMAN_FW_ADDR / 512;
Kumar Gala2683c532011-04-13 08:37:44 -0500521 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
522
523 if (!mmc)
524 printf("\nMMC cannot find device for ucode\n");
525 else {
526 printf("\nMMC read: dev # %u, block # %u, count %u ...\n",
527 dev, blk, cnt);
528 mmc_init(mmc);
Yinbo Zhu45c20bd2018-09-25 14:47:06 +0800529 (void)blk_dread(mmc_get_blk_desc(mmc), blk, cnt,
Stephen Warrene73f2962015-12-07 11:38:48 -0700530 addr);
Kumar Gala2683c532011-04-13 08:37:44 -0500531 }
Liu Gang1e084582012-03-08 00:33:18 +0000532#elif defined(CONFIG_SYS_QE_FMAN_FW_IN_REMOTE)
Zhao Qiang83a90842014-03-21 16:21:44 +0800533 void *addr = (void *)CONFIG_SYS_FMAN_FW_ADDR;
York Sun5f321a32013-06-25 11:37:40 -0700534#else
535 void *addr = NULL;
Kumar Gala2683c532011-04-13 08:37:44 -0500536#endif
537
Sean Anderson9a2c7732022-09-07 13:44:55 +0800538 rc = fit_check_format(addr, CONFIG_SYS_QE_FMAN_FW_LENGTH);
539 if (!rc) {
540 size_t unused;
541 const void *new_addr;
542
543 rc = fit_get_data_conf_prop(addr, "fman", &new_addr, &unused);
544 if (rc)
545 return rc;
546 addr = (void *)new_addr;
547 } else if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
548 /*
549 * Using a (signed) FIT wrapper is mandatory if we are
550 * doing verified boot.
551 */
552 return rc;
553 }
554
Kumar Gala2683c532011-04-13 08:37:44 -0500555 /* Upload the Fman microcode if it's present */
556 rc = fman_upload_firmware(index, &reg->fm_imem, addr);
557 if (rc)
558 return rc;
Simon Glass4d949a22017-08-03 12:22:10 -0600559 env_set_addr("fman_ucode", addr);
Kumar Gala2683c532011-04-13 08:37:44 -0500560
561 fm_init_muram(index, &reg->muram);
562 fm_init_qmi(&reg->fm_qmi_common);
563 fm_init_fpm(&reg->fm_fpm);
564
565 /* clear DMA status */
566 setbits_be32(&reg->fm_dma.fmdmsr, FMDMSR_CLEAR_ALL);
567
568 /* set DMA mode */
569 setbits_be32(&reg->fm_dma.fmdmmr, FMDMMR_SBER);
570
571 return fm_init_bmi(index, &reg->fm_bmi_common);
572}
Rajesh Bhagat1dec9612018-11-05 18:02:23 +0000573#endif
Madalin Bucurb76b0a62020-04-23 16:25:19 +0300574
Madalin Bucurb76b0a62020-04-23 16:25:19 +0300575struct fman_priv {
576 struct ccsr_fman *reg;
577 unsigned int fman_id;
578};
579
580static const struct udevice_id fman_ids[] = {
581 { .compatible = "fsl,fman" },
582 {}
583};
584
585static int fman_probe(struct udevice *dev)
586{
Sean Anderson394b4ef2022-12-29 11:53:00 -0500587 const char *firmware_name = NULL;
588 int ret;
Madalin Bucurb76b0a62020-04-23 16:25:19 +0300589 struct fman_priv *priv = dev_get_priv(dev);
590
591 priv->reg = (struct ccsr_fman *)(uintptr_t)dev_read_addr(dev);
592
593 if (dev_read_u32(dev, "cell-index", &priv->fman_id)) {
594 printf("FMan node property cell-index missing\n");
595 return -EINVAL;
596 }
597
Sean Anderson394b4ef2022-12-29 11:53:00 -0500598 ret = dev_read_string_index(dev, "firmware-name", 0, &firmware_name);
599 if (ret && ret != -EINVAL) {
600 dev_dbg(dev, "Could not read firmware-name\n");
601 return ret;
602 }
603
604 return fm_init_common(priv->fman_id, priv->reg, firmware_name);
Madalin Bucurb76b0a62020-04-23 16:25:19 +0300605}
606
607static int fman_remove(struct udevice *dev)
608{
609 return 0;
610}
611
612int fman_id(struct udevice *dev)
613{
614 struct fman_priv *priv = dev_get_priv(dev);
615
616 return priv->fman_id;
617}
618
619void *fman_port(struct udevice *dev, int num)
620{
621 struct fman_priv *priv = dev_get_priv(dev);
622
623 return &priv->reg->port[num - 1].fm_bmi;
624}
625
626void *fman_mdio(struct udevice *dev, enum fm_mac_type type, int num)
627{
628 struct fman_priv *priv = dev_get_priv(dev);
629 void *res = NULL;
630
631 switch (type) {
632#ifdef CONFIG_SYS_FMAN_V3
633 case FM_MEMAC:
634 res = &priv->reg->memac[num].fm_memac_mdio;
635 break;
636#else
637 case FM_DTSEC:
638 res = &priv->reg->mac_1g[num].fm_mdio.miimcfg;
639 break;
640 case FM_TGEC:
641 res = &priv->reg->mac_10g[num].fm_10gec_mdio;
642 break;
643#endif
644 }
645 return res;
646}
647
648U_BOOT_DRIVER(fman) = {
649 .name = "fman",
650 .id = UCLASS_SIMPLE_BUS,
651 .of_match = fman_ids,
652 .probe = fman_probe,
653 .remove = fman_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700654 .priv_auto = sizeof(struct fman_priv),
Madalin Bucurb76b0a62020-04-23 16:25:19 +0300655 .flags = DM_FLAG_ALLOC_PRIV_DMA,
656};