Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2009-2011 Freescale Semiconductor, Inc. |
| 4 | * Dave Liu <daveliu@freescale.com> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 5 | */ |
| 6 | #include <common.h> |
Simon Glass | 313112a | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 7 | #include <env.h> |
Sean Anderson | b55b750 | 2022-12-29 11:53:01 -0500 | [diff] [blame] | 8 | #include <fs_loader.h> |
Sean Anderson | 9a2c773 | 2022-09-07 13:44:55 +0800 | [diff] [blame] | 9 | #include <image.h> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include <asm/io.h> |
Sean Anderson | 394b4ef | 2022-12-29 11:53:00 -0500 | [diff] [blame] | 12 | #include <dm/device_compat.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 13 | #include <linux/errno.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 14 | #include <u-boot/crc.h> |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 15 | #include <dm.h> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 16 | |
| 17 | #include "fm.h" |
Qianyu Gong | ae6a758 | 2016-02-18 13:01:59 +0800 | [diff] [blame] | 18 | #include <fsl_qe.h> /* For struct qe_firmware */ |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 19 | |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 20 | #include <nand.h> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 21 | #include <spi_flash.h> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 22 | #include <mmc.h> |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 23 | |
| 24 | #ifdef CONFIG_ARM64 |
| 25 | #include <asm/armv8/mmu.h> |
| 26 | #include <asm/arch/cpu.h> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 27 | #endif |
| 28 | |
Tom Rini | 0a2bac7 | 2022-11-16 13:10:29 -0500 | [diff] [blame] | 29 | struct fm_muram muram[CFG_SYS_NUM_FMAN]; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 30 | |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 31 | void *fm_muram_base(int fm_idx) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 32 | { |
| 33 | return muram[fm_idx].base; |
| 34 | } |
| 35 | |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 36 | void *fm_muram_alloc(int fm_idx, size_t size, ulong align) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 37 | { |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 38 | void *ret; |
| 39 | ulong align_mask; |
| 40 | size_t off; |
| 41 | void *save; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 42 | |
| 43 | align_mask = align - 1; |
| 44 | save = muram[fm_idx].alloc; |
| 45 | |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 46 | off = (ulong)save & align_mask; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 47 | if (off != 0) |
| 48 | muram[fm_idx].alloc += (align - off); |
| 49 | off = size & align_mask; |
| 50 | if (off != 0) |
| 51 | size += (align - off); |
| 52 | if ((muram[fm_idx].alloc + size) >= muram[fm_idx].top) { |
| 53 | muram[fm_idx].alloc = save; |
| 54 | printf("%s: run out of ram.\n", __func__); |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 55 | return NULL; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | ret = muram[fm_idx].alloc; |
| 59 | muram[fm_idx].alloc += size; |
| 60 | memset((void *)ret, 0, size); |
| 61 | |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | static void fm_init_muram(int fm_idx, void *reg) |
| 66 | { |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 67 | void *base = reg; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 68 | |
| 69 | muram[fm_idx].base = base; |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 70 | muram[fm_idx].size = CFG_SYS_FM_MURAM_SIZE; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 71 | muram[fm_idx].alloc = base + FM_MURAM_RES_SIZE; |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 72 | muram[fm_idx].top = base + CFG_SYS_FM_MURAM_SIZE; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* |
| 76 | * fm_upload_ucode - Fman microcode upload worker function |
| 77 | * |
| 78 | * This function does the actual uploading of an Fman microcode |
| 79 | * to an Fman. |
| 80 | */ |
| 81 | static void fm_upload_ucode(int fm_idx, struct fm_imem *imem, |
| 82 | u32 *ucode, unsigned int size) |
| 83 | { |
| 84 | unsigned int i; |
| 85 | unsigned int timeout = 1000000; |
| 86 | |
| 87 | /* enable address auto increase */ |
| 88 | out_be32(&imem->iadd, IRAM_IADD_AIE); |
| 89 | /* write microcode to IRAM */ |
| 90 | for (i = 0; i < size / 4; i++) |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 91 | out_be32(&imem->idata, (be32_to_cpu(ucode[i]))); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 92 | |
| 93 | /* verify if the writing is over */ |
| 94 | out_be32(&imem->iadd, 0); |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 95 | while ((in_be32(&imem->idata) != be32_to_cpu(ucode[0])) && --timeout) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 96 | ; |
| 97 | if (!timeout) |
| 98 | printf("Fman%u: microcode upload timeout\n", fm_idx + 1); |
| 99 | |
| 100 | /* enable microcode from IRAM */ |
| 101 | out_be32(&imem->iready, IRAM_READY); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Upload an Fman firmware |
| 106 | * |
| 107 | * This function is similar to qe_upload_firmware(), exception that it uploads |
| 108 | * a microcode to the Fman instead of the QE. |
| 109 | * |
| 110 | * Because the process for uploading a microcode to the Fman is similar for |
| 111 | * that of the QE, the QE firmware binary format is used for Fman microcode. |
| 112 | * It should be possible to unify these two functions, but for now we keep them |
| 113 | * separate. |
| 114 | */ |
| 115 | static int fman_upload_firmware(int fm_idx, |
| 116 | struct fm_imem *fm_imem, |
| 117 | const struct qe_firmware *firmware) |
| 118 | { |
| 119 | unsigned int i; |
| 120 | u32 crc; |
| 121 | size_t calc_size = sizeof(struct qe_firmware); |
| 122 | size_t length; |
| 123 | const struct qe_header *hdr; |
| 124 | |
| 125 | if (!firmware) { |
| 126 | printf("Fman%u: Invalid address for firmware\n", fm_idx + 1); |
| 127 | return -EINVAL; |
| 128 | } |
| 129 | |
| 130 | hdr = &firmware->header; |
| 131 | length = be32_to_cpu(hdr->length); |
| 132 | |
| 133 | /* Check the magic */ |
| 134 | if ((hdr->magic[0] != 'Q') || (hdr->magic[1] != 'E') || |
| 135 | (hdr->magic[2] != 'F')) { |
| 136 | printf("Fman%u: Data at %p is not a firmware\n", fm_idx + 1, |
| 137 | firmware); |
| 138 | return -EPERM; |
| 139 | } |
| 140 | |
| 141 | /* Check the version */ |
| 142 | if (hdr->version != 1) { |
| 143 | printf("Fman%u: Unsupported firmware version %u\n", fm_idx + 1, |
| 144 | hdr->version); |
| 145 | return -EPERM; |
| 146 | } |
| 147 | |
| 148 | /* Validate some of the fields */ |
| 149 | if ((firmware->count != 1)) { |
| 150 | printf("Fman%u: Invalid data in firmware header\n", fm_idx + 1); |
| 151 | return -EINVAL; |
| 152 | } |
| 153 | |
| 154 | /* Validate the length and check if there's a CRC */ |
| 155 | calc_size += (firmware->count - 1) * sizeof(struct qe_microcode); |
| 156 | |
| 157 | for (i = 0; i < firmware->count; i++) |
| 158 | /* |
| 159 | * For situations where the second RISC uses the same microcode |
| 160 | * as the first, the 'code_offset' and 'count' fields will be |
| 161 | * zero, so it's okay to add those. |
| 162 | */ |
| 163 | calc_size += sizeof(u32) * |
| 164 | be32_to_cpu(firmware->microcode[i].count); |
| 165 | |
| 166 | /* Validate the length */ |
| 167 | if (length != calc_size + sizeof(u32)) { |
| 168 | printf("Fman%u: Invalid length in firmware header\n", |
| 169 | fm_idx + 1); |
| 170 | return -EPERM; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Validate the CRC. We would normally call crc32_no_comp(), but that |
| 175 | * function isn't available unless you turn on JFFS support. |
| 176 | */ |
| 177 | crc = be32_to_cpu(*(u32 *)((void *)firmware + calc_size)); |
| 178 | if (crc != (crc32(-1, (const void *)firmware, calc_size) ^ -1)) { |
| 179 | printf("Fman%u: Firmware CRC is invalid\n", fm_idx + 1); |
| 180 | return -EIO; |
| 181 | } |
| 182 | |
| 183 | /* Loop through each microcode. */ |
| 184 | for (i = 0; i < firmware->count; i++) { |
| 185 | const struct qe_microcode *ucode = &firmware->microcode[i]; |
| 186 | |
| 187 | /* Upload a microcode if it's present */ |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 188 | if (be32_to_cpu(ucode->code_offset)) { |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 189 | u32 ucode_size; |
| 190 | u32 *code; |
| 191 | printf("Fman%u: Uploading microcode version %u.%u.%u\n", |
| 192 | fm_idx + 1, ucode->major, ucode->minor, |
| 193 | ucode->revision); |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 194 | code = (void *)firmware + |
| 195 | be32_to_cpu(ucode->code_offset); |
| 196 | ucode_size = sizeof(u32) * be32_to_cpu(ucode->count); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 197 | fm_upload_ucode(fm_idx, fm_imem, code, ucode_size); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static u32 fm_assign_risc(int port_id) |
| 205 | { |
| 206 | u32 risc_sel, val; |
| 207 | risc_sel = (port_id & 0x1) ? FMFPPRC_RISC2 : FMFPPRC_RISC1; |
| 208 | val = (port_id << FMFPPRC_PORTID_SHIFT) & FMFPPRC_PORTID_MASK; |
| 209 | val |= ((risc_sel << FMFPPRC_ORA_SHIFT) | risc_sel); |
| 210 | |
| 211 | return val; |
| 212 | } |
| 213 | |
| 214 | static void fm_init_fpm(struct fm_fpm *fpm) |
| 215 | { |
| 216 | int i, port_id; |
| 217 | u32 val; |
| 218 | |
| 219 | setbits_be32(&fpm->fmfpee, FMFPEE_EHM | FMFPEE_UEC | |
| 220 | FMFPEE_CER | FMFPEE_DER); |
| 221 | |
| 222 | /* IM mode, each even port ID to RISC#1, each odd port ID to RISC#2 */ |
| 223 | |
| 224 | /* offline/parser port */ |
| 225 | for (i = 0; i < MAX_NUM_OH_PORT; i++) { |
| 226 | port_id = OH_PORT_ID_BASE + i; |
| 227 | val = fm_assign_risc(port_id); |
| 228 | out_be32(&fpm->fpmprc, val); |
| 229 | } |
| 230 | /* Rx 1G port */ |
| 231 | for (i = 0; i < MAX_NUM_RX_PORT_1G; i++) { |
| 232 | port_id = RX_PORT_1G_BASE + i; |
| 233 | val = fm_assign_risc(port_id); |
| 234 | out_be32(&fpm->fpmprc, val); |
| 235 | } |
| 236 | /* Tx 1G port */ |
| 237 | for (i = 0; i < MAX_NUM_TX_PORT_1G; i++) { |
| 238 | port_id = TX_PORT_1G_BASE + i; |
| 239 | val = fm_assign_risc(port_id); |
| 240 | out_be32(&fpm->fpmprc, val); |
| 241 | } |
| 242 | /* Rx 10G port */ |
| 243 | port_id = RX_PORT_10G_BASE; |
| 244 | val = fm_assign_risc(port_id); |
| 245 | out_be32(&fpm->fpmprc, val); |
| 246 | /* Tx 10G port */ |
| 247 | port_id = TX_PORT_10G_BASE; |
| 248 | val = fm_assign_risc(port_id); |
| 249 | out_be32(&fpm->fpmprc, val); |
| 250 | |
| 251 | /* disable the dispatch limit in IM case */ |
| 252 | out_be32(&fpm->fpmflc, FMFP_FLC_DISP_LIM_NONE); |
| 253 | /* clear events */ |
| 254 | out_be32(&fpm->fmfpee, FMFPEE_CLEAR_EVENT); |
| 255 | |
| 256 | /* clear risc events */ |
| 257 | for (i = 0; i < 4; i++) |
| 258 | out_be32(&fpm->fpmcev[i], 0xffffffff); |
| 259 | |
| 260 | /* clear error */ |
| 261 | out_be32(&fpm->fpmrcr, FMFP_RCR_MDEC | FMFP_RCR_IDEC); |
| 262 | } |
| 263 | |
| 264 | static int fm_init_bmi(int fm_idx, struct fm_bmi_common *bmi) |
| 265 | { |
| 266 | int blk, i, port_id; |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 267 | u32 val; |
| 268 | size_t offset; |
| 269 | void *base; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 270 | |
| 271 | /* alloc free buffer pool in MURAM */ |
| 272 | base = fm_muram_alloc(fm_idx, FM_FREE_POOL_SIZE, FM_FREE_POOL_ALIGN); |
| 273 | if (!base) { |
| 274 | printf("%s: no muram for free buffer pool\n", __func__); |
| 275 | return -ENOMEM; |
| 276 | } |
| 277 | offset = base - fm_muram_base(fm_idx); |
| 278 | |
| 279 | /* Need 128KB total free buffer pool size */ |
| 280 | val = offset / 256; |
| 281 | blk = FM_FREE_POOL_SIZE / 256; |
| 282 | /* in IM, we must not begin from offset 0 in MURAM */ |
| 283 | val |= ((blk - 1) << FMBM_CFG1_FBPS_SHIFT); |
| 284 | out_be32(&bmi->fmbm_cfg1, val); |
| 285 | |
| 286 | /* disable all BMI interrupt */ |
| 287 | out_be32(&bmi->fmbm_ier, FMBM_IER_DISABLE_ALL); |
| 288 | |
| 289 | /* clear all events */ |
| 290 | out_be32(&bmi->fmbm_ievr, FMBM_IEVR_CLEAR_ALL); |
| 291 | |
| 292 | /* |
| 293 | * set port parameters - FMBM_PP_x |
| 294 | * max tasks 10G Rx/Tx=12, 1G Rx/Tx 4, others is 1 |
| 295 | * max dma 10G Rx/Tx=3, others is 1 |
| 296 | * set port FIFO size - FMBM_PFS_x |
| 297 | * 4KB for all Rx and Tx ports |
| 298 | */ |
| 299 | /* offline/parser port */ |
| 300 | for (i = 0; i < MAX_NUM_OH_PORT; i++) { |
| 301 | port_id = OH_PORT_ID_BASE + i - 1; |
| 302 | /* max tasks=1, max dma=1, no extra */ |
| 303 | out_be32(&bmi->fmbm_pp[port_id], 0); |
| 304 | /* port FIFO size - 256 bytes, no extra */ |
| 305 | out_be32(&bmi->fmbm_pfs[port_id], 0); |
| 306 | } |
| 307 | /* Rx 1G port */ |
| 308 | for (i = 0; i < MAX_NUM_RX_PORT_1G; i++) { |
| 309 | port_id = RX_PORT_1G_BASE + i - 1; |
| 310 | /* max tasks=4, max dma=1, no extra */ |
| 311 | out_be32(&bmi->fmbm_pp[port_id], FMBM_PP_MXT(4)); |
| 312 | /* FIFO size - 4KB, no extra */ |
| 313 | out_be32(&bmi->fmbm_pfs[port_id], FMBM_PFS_IFSZ(0xf)); |
| 314 | } |
| 315 | /* Tx 1G port FIFO size - 4KB, no extra */ |
| 316 | for (i = 0; i < MAX_NUM_TX_PORT_1G; i++) { |
| 317 | port_id = TX_PORT_1G_BASE + i - 1; |
| 318 | /* max tasks=4, max dma=1, no extra */ |
| 319 | out_be32(&bmi->fmbm_pp[port_id], FMBM_PP_MXT(4)); |
| 320 | /* FIFO size - 4KB, no extra */ |
| 321 | out_be32(&bmi->fmbm_pfs[port_id], FMBM_PFS_IFSZ(0xf)); |
| 322 | } |
| 323 | /* Rx 10G port */ |
| 324 | port_id = RX_PORT_10G_BASE - 1; |
| 325 | /* max tasks=12, max dma=3, no extra */ |
| 326 | out_be32(&bmi->fmbm_pp[port_id], FMBM_PP_MXT(12) | FMBM_PP_MXD(3)); |
| 327 | /* FIFO size - 4KB, no extra */ |
| 328 | out_be32(&bmi->fmbm_pfs[port_id], FMBM_PFS_IFSZ(0xf)); |
| 329 | |
| 330 | /* Tx 10G port */ |
| 331 | port_id = TX_PORT_10G_BASE - 1; |
| 332 | /* max tasks=12, max dma=3, no extra */ |
| 333 | out_be32(&bmi->fmbm_pp[port_id], FMBM_PP_MXT(12) | FMBM_PP_MXD(3)); |
| 334 | /* FIFO size - 4KB, no extra */ |
| 335 | out_be32(&bmi->fmbm_pfs[port_id], FMBM_PFS_IFSZ(0xf)); |
| 336 | |
| 337 | /* initialize internal buffers data base (linked list) */ |
| 338 | out_be32(&bmi->fmbm_init, FMBM_INIT_START); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static void fm_init_qmi(struct fm_qmi_common *qmi) |
| 344 | { |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 345 | /* disable all error interrupts */ |
| 346 | out_be32(&qmi->fmqm_eien, FMQM_EIEN_DISABLE_ALL); |
| 347 | /* clear all error events */ |
| 348 | out_be32(&qmi->fmqm_eie, FMQM_EIE_CLEAR_ALL); |
| 349 | |
| 350 | /* disable all interrupts */ |
| 351 | out_be32(&qmi->fmqm_ien, FMQM_IEN_DISABLE_ALL); |
| 352 | /* clear all interrupts */ |
| 353 | out_be32(&qmi->fmqm_ie, FMQM_IE_CLEAR_ALL); |
| 354 | } |
| 355 | |
| 356 | /* Init common part of FM, index is fm num# like fm as above */ |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 357 | #ifdef CONFIG_TFABOOT |
Sean Anderson | 394b4ef | 2022-12-29 11:53:00 -0500 | [diff] [blame] | 358 | int fm_init_common(int index, struct ccsr_fman *reg, const char *firmware_name) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 359 | { |
| 360 | int rc; |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 361 | void *addr = NULL; |
| 362 | enum boot_src src = get_boot_src(); |
| 363 | |
| 364 | if (src == BOOT_SOURCE_IFC_NOR) { |
| 365 | addr = (void *)(CONFIG_SYS_FMAN_FW_ADDR + |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame^] | 366 | CFG_SYS_FSL_IFC_BASE); |
Francois Gervais | 28ff2ef | 2020-04-08 09:48:12 -0400 | [diff] [blame] | 367 | #ifdef CONFIG_CMD_NAND |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 368 | } else if (src == BOOT_SOURCE_IFC_NAND) { |
| 369 | size_t fw_length = CONFIG_SYS_QE_FMAN_FW_LENGTH; |
| 370 | |
| 371 | addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH); |
| 372 | |
| 373 | rc = nand_read(get_nand_dev_by_index(0), |
| 374 | (loff_t)CONFIG_SYS_FMAN_FW_ADDR, |
| 375 | &fw_length, (u_char *)addr); |
| 376 | if (rc == -EUCLEAN) { |
| 377 | printf("NAND read of FMAN firmware at offset 0x%x failed %d\n", |
| 378 | CONFIG_SYS_FMAN_FW_ADDR, rc); |
| 379 | } |
Francois Gervais | 28ff2ef | 2020-04-08 09:48:12 -0400 | [diff] [blame] | 380 | #endif |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 381 | } else if (src == BOOT_SOURCE_QSPI_NOR) { |
| 382 | struct spi_flash *ucode_flash; |
| 383 | |
| 384 | addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH); |
| 385 | int ret = 0; |
| 386 | |
Lukasz Majewski | 76f44298 | 2020-06-04 23:11:53 +0800 | [diff] [blame] | 387 | #if CONFIG_IS_ENABLED(DM_SPI_FLASH) |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 388 | struct udevice *new; |
| 389 | |
| 390 | /* speed and mode will be read from DT */ |
Tom Rini | 119d2fb | 2021-12-11 14:55:48 -0500 | [diff] [blame] | 391 | ret = spi_flash_probe_bus_cs(CONFIG_SF_DEFAULT_BUS, |
Patrice Chotard | 05b1aa6 | 2022-03-30 09:33:14 +0200 | [diff] [blame] | 392 | CONFIG_SF_DEFAULT_CS, &new); |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 393 | |
| 394 | ucode_flash = dev_get_uclass_priv(new); |
| 395 | #else |
| 396 | ucode_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
| 397 | CONFIG_ENV_SPI_CS, |
| 398 | CONFIG_ENV_SPI_MAX_HZ, |
| 399 | CONFIG_ENV_SPI_MODE); |
| 400 | #endif |
| 401 | if (!ucode_flash) { |
| 402 | printf("SF: probe for ucode failed\n"); |
| 403 | } else { |
| 404 | ret = spi_flash_read(ucode_flash, |
| 405 | CONFIG_SYS_FMAN_FW_ADDR + |
Tom Rini | 376b88a | 2022-10-28 20:27:13 -0400 | [diff] [blame] | 406 | CFG_SYS_FSL_QSPI_BASE, |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 407 | CONFIG_SYS_QE_FMAN_FW_LENGTH, |
| 408 | addr); |
| 409 | if (ret) |
| 410 | printf("SF: read for ucode failed\n"); |
| 411 | spi_flash_free(ucode_flash); |
| 412 | } |
| 413 | } else if (src == BOOT_SOURCE_SD_MMC) { |
| 414 | int dev = CONFIG_SYS_MMC_ENV_DEV; |
| 415 | |
| 416 | addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH); |
| 417 | u32 cnt = CONFIG_SYS_QE_FMAN_FW_LENGTH / 512; |
| 418 | u32 blk = CONFIG_SYS_FMAN_FW_ADDR / 512; |
| 419 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
| 420 | |
| 421 | if (!mmc) { |
| 422 | printf("\nMMC cannot find device for ucode\n"); |
| 423 | } else { |
| 424 | printf("\nMMC read: dev # %u, block # %u, count %u ...\n", |
| 425 | dev, blk, cnt); |
| 426 | mmc_init(mmc); |
| 427 | (void)blk_dread(mmc_get_blk_desc(mmc), blk, cnt, |
| 428 | addr); |
| 429 | } |
| 430 | } else { |
| 431 | addr = NULL; |
| 432 | } |
| 433 | |
| 434 | /* Upload the Fman microcode if it's present */ |
| 435 | rc = fman_upload_firmware(index, ®->fm_imem, addr); |
| 436 | if (rc) |
| 437 | return rc; |
| 438 | env_set_addr("fman_ucode", addr); |
| 439 | |
| 440 | fm_init_muram(index, ®->muram); |
| 441 | fm_init_qmi(®->fm_qmi_common); |
| 442 | fm_init_fpm(®->fm_fpm); |
| 443 | |
| 444 | /* clear DMA status */ |
| 445 | setbits_be32(®->fm_dma.fmdmsr, FMDMSR_CLEAR_ALL); |
| 446 | |
| 447 | /* set DMA mode */ |
| 448 | setbits_be32(®->fm_dma.fmdmmr, FMDMMR_SBER); |
| 449 | |
| 450 | return fm_init_bmi(index, ®->fm_bmi_common); |
| 451 | } |
| 452 | #else |
Sean Anderson | 394b4ef | 2022-12-29 11:53:00 -0500 | [diff] [blame] | 453 | int fm_init_common(int index, struct ccsr_fman *reg, const char *firmware_name) |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 454 | { |
| 455 | int rc; |
Sean Anderson | b55b750 | 2022-12-29 11:53:01 -0500 | [diff] [blame] | 456 | #if defined(CONFIG_SYS_QE_FMAN_FW_IN_FS) |
| 457 | struct udevice *fs_loader; |
| 458 | void *addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH); |
| 459 | |
| 460 | if (!addr) |
| 461 | return -ENOMEM; |
| 462 | |
| 463 | rc = get_fs_loader(&fs_loader); |
| 464 | if (rc) { |
| 465 | debug("could not get fs loader: %d\n", rc); |
| 466 | return rc; |
| 467 | } |
| 468 | |
| 469 | if (!firmware_name) |
| 470 | firmware_name = "fman.itb"; |
| 471 | |
| 472 | rc = request_firmware_into_buf(fs_loader, firmware_name, addr, |
| 473 | CONFIG_SYS_QE_FMAN_FW_LENGTH, 0); |
| 474 | if (rc < 0) { |
| 475 | debug("could not request %s: %d\n", firmware_name, rc); |
| 476 | return rc; |
| 477 | } |
| 478 | #elif defined(CONFIG_SYS_QE_FMAN_FW_IN_NOR) |
Zhao Qiang | 83a9084 | 2014-03-21 16:21:44 +0800 | [diff] [blame] | 479 | void *addr = (void *)CONFIG_SYS_FMAN_FW_ADDR; |
Timur Tabi | 275f4bb | 2011-11-22 09:21:25 -0600 | [diff] [blame] | 480 | #elif defined(CONFIG_SYS_QE_FMAN_FW_IN_NAND) |
| 481 | size_t fw_length = CONFIG_SYS_QE_FMAN_FW_LENGTH; |
| 482 | void *addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 483 | |
Grygorii Strashko | aa942fe | 2017-06-26 19:13:00 -0500 | [diff] [blame] | 484 | rc = nand_read(get_nand_dev_by_index(0), |
| 485 | (loff_t)CONFIG_SYS_FMAN_FW_ADDR, |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 486 | &fw_length, (u_char *)addr); |
| 487 | if (rc == -EUCLEAN) { |
| 488 | printf("NAND read of FMAN firmware at offset 0x%x failed %d\n", |
Zhao Qiang | 83a9084 | 2014-03-21 16:21:44 +0800 | [diff] [blame] | 489 | CONFIG_SYS_FMAN_FW_ADDR, rc); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 490 | } |
Tom Rini | fa911f8 | 2019-05-12 07:59:12 -0400 | [diff] [blame] | 491 | #elif defined(CONFIG_SYS_QE_FMAN_FW_IN_SPIFLASH) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 492 | struct spi_flash *ucode_flash; |
Timur Tabi | 275f4bb | 2011-11-22 09:21:25 -0600 | [diff] [blame] | 493 | void *addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 494 | int ret = 0; |
| 495 | |
Lukasz Majewski | 76f44298 | 2020-06-04 23:11:53 +0800 | [diff] [blame] | 496 | #if CONFIG_IS_ENABLED(DM_SPI_FLASH) |
Qianyu Gong | ea4923e | 2016-08-03 11:04:25 +0800 | [diff] [blame] | 497 | struct udevice *new; |
| 498 | |
| 499 | /* speed and mode will be read from DT */ |
Tom Rini | 119d2fb | 2021-12-11 14:55:48 -0500 | [diff] [blame] | 500 | ret = spi_flash_probe_bus_cs(CONFIG_SF_DEFAULT_BUS, CONFIG_SF_DEFAULT_CS, |
Patrice Chotard | 05b1aa6 | 2022-03-30 09:33:14 +0200 | [diff] [blame] | 501 | &new); |
Qianyu Gong | ea4923e | 2016-08-03 11:04:25 +0800 | [diff] [blame] | 502 | |
| 503 | ucode_flash = dev_get_uclass_priv(new); |
| 504 | #else |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 505 | ucode_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 506 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Qianyu Gong | ea4923e | 2016-08-03 11:04:25 +0800 | [diff] [blame] | 507 | #endif |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 508 | if (!ucode_flash) |
| 509 | printf("SF: probe for ucode failed\n"); |
| 510 | else { |
Zhao Qiang | 83a9084 | 2014-03-21 16:21:44 +0800 | [diff] [blame] | 511 | ret = spi_flash_read(ucode_flash, CONFIG_SYS_FMAN_FW_ADDR, |
Timur Tabi | 275f4bb | 2011-11-22 09:21:25 -0600 | [diff] [blame] | 512 | CONFIG_SYS_QE_FMAN_FW_LENGTH, addr); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 513 | if (ret) |
| 514 | printf("SF: read for ucode failed\n"); |
| 515 | spi_flash_free(ucode_flash); |
| 516 | } |
Timur Tabi | 275f4bb | 2011-11-22 09:21:25 -0600 | [diff] [blame] | 517 | #elif defined(CONFIG_SYS_QE_FMAN_FW_IN_MMC) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 518 | int dev = CONFIG_SYS_MMC_ENV_DEV; |
Timur Tabi | 275f4bb | 2011-11-22 09:21:25 -0600 | [diff] [blame] | 519 | void *addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH); |
| 520 | u32 cnt = CONFIG_SYS_QE_FMAN_FW_LENGTH / 512; |
Zhao Qiang | 83a9084 | 2014-03-21 16:21:44 +0800 | [diff] [blame] | 521 | u32 blk = CONFIG_SYS_FMAN_FW_ADDR / 512; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 522 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
| 523 | |
| 524 | if (!mmc) |
| 525 | printf("\nMMC cannot find device for ucode\n"); |
| 526 | else { |
| 527 | printf("\nMMC read: dev # %u, block # %u, count %u ...\n", |
| 528 | dev, blk, cnt); |
| 529 | mmc_init(mmc); |
Yinbo Zhu | 45c20bd | 2018-09-25 14:47:06 +0800 | [diff] [blame] | 530 | (void)blk_dread(mmc_get_blk_desc(mmc), blk, cnt, |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 531 | addr); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 532 | } |
Liu Gang | 1e08458 | 2012-03-08 00:33:18 +0000 | [diff] [blame] | 533 | #elif defined(CONFIG_SYS_QE_FMAN_FW_IN_REMOTE) |
Zhao Qiang | 83a9084 | 2014-03-21 16:21:44 +0800 | [diff] [blame] | 534 | void *addr = (void *)CONFIG_SYS_FMAN_FW_ADDR; |
York Sun | 5f321a3 | 2013-06-25 11:37:40 -0700 | [diff] [blame] | 535 | #else |
| 536 | void *addr = NULL; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 537 | #endif |
| 538 | |
Sean Anderson | 9a2c773 | 2022-09-07 13:44:55 +0800 | [diff] [blame] | 539 | rc = fit_check_format(addr, CONFIG_SYS_QE_FMAN_FW_LENGTH); |
| 540 | if (!rc) { |
| 541 | size_t unused; |
| 542 | const void *new_addr; |
| 543 | |
| 544 | rc = fit_get_data_conf_prop(addr, "fman", &new_addr, &unused); |
| 545 | if (rc) |
| 546 | return rc; |
| 547 | addr = (void *)new_addr; |
| 548 | } else if (CONFIG_IS_ENABLED(FIT_SIGNATURE)) { |
| 549 | /* |
| 550 | * Using a (signed) FIT wrapper is mandatory if we are |
| 551 | * doing verified boot. |
| 552 | */ |
| 553 | return rc; |
| 554 | } |
| 555 | |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 556 | /* Upload the Fman microcode if it's present */ |
| 557 | rc = fman_upload_firmware(index, ®->fm_imem, addr); |
| 558 | if (rc) |
| 559 | return rc; |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 560 | env_set_addr("fman_ucode", addr); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 561 | |
| 562 | fm_init_muram(index, ®->muram); |
| 563 | fm_init_qmi(®->fm_qmi_common); |
| 564 | fm_init_fpm(®->fm_fpm); |
| 565 | |
| 566 | /* clear DMA status */ |
| 567 | setbits_be32(®->fm_dma.fmdmsr, FMDMSR_CLEAR_ALL); |
| 568 | |
| 569 | /* set DMA mode */ |
| 570 | setbits_be32(®->fm_dma.fmdmmr, FMDMMR_SBER); |
| 571 | |
| 572 | return fm_init_bmi(index, ®->fm_bmi_common); |
| 573 | } |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 574 | #endif |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 575 | |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 576 | struct fman_priv { |
| 577 | struct ccsr_fman *reg; |
| 578 | unsigned int fman_id; |
| 579 | }; |
| 580 | |
| 581 | static const struct udevice_id fman_ids[] = { |
| 582 | { .compatible = "fsl,fman" }, |
| 583 | {} |
| 584 | }; |
| 585 | |
| 586 | static int fman_probe(struct udevice *dev) |
| 587 | { |
Sean Anderson | 394b4ef | 2022-12-29 11:53:00 -0500 | [diff] [blame] | 588 | const char *firmware_name = NULL; |
| 589 | int ret; |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 590 | struct fman_priv *priv = dev_get_priv(dev); |
| 591 | |
| 592 | priv->reg = (struct ccsr_fman *)(uintptr_t)dev_read_addr(dev); |
| 593 | |
| 594 | if (dev_read_u32(dev, "cell-index", &priv->fman_id)) { |
| 595 | printf("FMan node property cell-index missing\n"); |
| 596 | return -EINVAL; |
| 597 | } |
| 598 | |
Sean Anderson | 394b4ef | 2022-12-29 11:53:00 -0500 | [diff] [blame] | 599 | ret = dev_read_string_index(dev, "firmware-name", 0, &firmware_name); |
| 600 | if (ret && ret != -EINVAL) { |
| 601 | dev_dbg(dev, "Could not read firmware-name\n"); |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | return fm_init_common(priv->fman_id, priv->reg, firmware_name); |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | static int fman_remove(struct udevice *dev) |
| 609 | { |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | int fman_id(struct udevice *dev) |
| 614 | { |
| 615 | struct fman_priv *priv = dev_get_priv(dev); |
| 616 | |
| 617 | return priv->fman_id; |
| 618 | } |
| 619 | |
| 620 | void *fman_port(struct udevice *dev, int num) |
| 621 | { |
| 622 | struct fman_priv *priv = dev_get_priv(dev); |
| 623 | |
| 624 | return &priv->reg->port[num - 1].fm_bmi; |
| 625 | } |
| 626 | |
| 627 | void *fman_mdio(struct udevice *dev, enum fm_mac_type type, int num) |
| 628 | { |
| 629 | struct fman_priv *priv = dev_get_priv(dev); |
| 630 | void *res = NULL; |
| 631 | |
| 632 | switch (type) { |
| 633 | #ifdef CONFIG_SYS_FMAN_V3 |
| 634 | case FM_MEMAC: |
| 635 | res = &priv->reg->memac[num].fm_memac_mdio; |
| 636 | break; |
| 637 | #else |
| 638 | case FM_DTSEC: |
| 639 | res = &priv->reg->mac_1g[num].fm_mdio.miimcfg; |
| 640 | break; |
| 641 | case FM_TGEC: |
| 642 | res = &priv->reg->mac_10g[num].fm_10gec_mdio; |
| 643 | break; |
| 644 | #endif |
| 645 | } |
| 646 | return res; |
| 647 | } |
| 648 | |
| 649 | U_BOOT_DRIVER(fman) = { |
| 650 | .name = "fman", |
| 651 | .id = UCLASS_SIMPLE_BUS, |
| 652 | .of_match = fman_ids, |
| 653 | .probe = fman_probe, |
| 654 | .remove = fman_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 655 | .priv_auto = sizeof(struct fman_priv), |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 656 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 657 | }; |