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 | */ |
Simon Glass | 313112a | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 6 | #include <env.h> |
Sean Anderson | b55b750 | 2022-12-29 11:53:01 -0500 | [diff] [blame] | 7 | #include <fs_loader.h> |
Sean Anderson | 9a2c773 | 2022-09-07 13:44:55 +0800 | [diff] [blame] | 8 | #include <image.h> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 9 | #include <malloc.h> |
| 10 | #include <asm/io.h> |
Sean Anderson | 394b4ef | 2022-12-29 11:53:00 -0500 | [diff] [blame] | 11 | #include <dm/device_compat.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 12 | #include <linux/errno.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 13 | #include <u-boot/crc.h> |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 14 | #include <dm.h> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 15 | |
| 16 | #include "fm.h" |
Qianyu Gong | ae6a758 | 2016-02-18 13:01:59 +0800 | [diff] [blame] | 17 | #include <fsl_qe.h> /* For struct qe_firmware */ |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 18 | |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 19 | #include <nand.h> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 20 | #include <spi_flash.h> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 21 | #include <mmc.h> |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 22 | |
| 23 | #ifdef CONFIG_ARM64 |
| 24 | #include <asm/armv8/mmu.h> |
| 25 | #include <asm/arch/cpu.h> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 26 | #endif |
| 27 | |
Tom Rini | 0a2bac7 | 2022-11-16 13:10:29 -0500 | [diff] [blame] | 28 | struct fm_muram muram[CFG_SYS_NUM_FMAN]; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 29 | |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 30 | void *fm_muram_base(int fm_idx) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 31 | { |
| 32 | return muram[fm_idx].base; |
| 33 | } |
| 34 | |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 35 | void *fm_muram_alloc(int fm_idx, size_t size, ulong align) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 36 | { |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 37 | void *ret; |
| 38 | ulong align_mask; |
| 39 | size_t off; |
| 40 | void *save; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 41 | |
| 42 | align_mask = align - 1; |
| 43 | save = muram[fm_idx].alloc; |
| 44 | |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 45 | off = (ulong)save & align_mask; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 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__); |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 54 | return NULL; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 55 | } |
| 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 | |
| 64 | static void fm_init_muram(int fm_idx, void *reg) |
| 65 | { |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 66 | void *base = reg; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 67 | |
| 68 | muram[fm_idx].base = base; |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 69 | muram[fm_idx].size = CFG_SYS_FM_MURAM_SIZE; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 70 | muram[fm_idx].alloc = base + FM_MURAM_RES_SIZE; |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 71 | muram[fm_idx].top = base + CFG_SYS_FM_MURAM_SIZE; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 72 | } |
| 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 | */ |
| 80 | static 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 Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 90 | out_be32(&imem->idata, (be32_to_cpu(ucode[i]))); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 91 | |
| 92 | /* verify if the writing is over */ |
| 93 | out_be32(&imem->iadd, 0); |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 94 | while ((in_be32(&imem->idata) != be32_to_cpu(ucode[0])) && --timeout) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 95 | ; |
| 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 | */ |
| 114 | static 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 Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 187 | if (be32_to_cpu(ucode->code_offset)) { |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 188 | 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 Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 193 | code = (void *)firmware + |
| 194 | be32_to_cpu(ucode->code_offset); |
| 195 | ucode_size = sizeof(u32) * be32_to_cpu(ucode->count); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 196 | fm_upload_ucode(fm_idx, fm_imem, code, ucode_size); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static 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 | |
| 213 | static 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 | |
| 263 | static int fm_init_bmi(int fm_idx, struct fm_bmi_common *bmi) |
| 264 | { |
| 265 | int blk, i, port_id; |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 266 | u32 val; |
| 267 | size_t offset; |
| 268 | void *base; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 269 | |
| 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 | |
| 342 | static void fm_init_qmi(struct fm_qmi_common *qmi) |
| 343 | { |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 344 | /* 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 Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 356 | #ifdef CONFIG_TFABOOT |
Sean Anderson | 394b4ef | 2022-12-29 11:53:00 -0500 | [diff] [blame] | 357 | 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] | 358 | { |
| 359 | int rc; |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 360 | 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 Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 365 | CFG_SYS_FSL_IFC_BASE); |
Francois Gervais | 28ff2ef | 2020-04-08 09:48:12 -0400 | [diff] [blame] | 366 | #ifdef CONFIG_CMD_NAND |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 367 | } 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 Gervais | 28ff2ef | 2020-04-08 09:48:12 -0400 | [diff] [blame] | 379 | #endif |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 380 | } 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 Majewski | 76f44298 | 2020-06-04 23:11:53 +0800 | [diff] [blame] | 386 | #if CONFIG_IS_ENABLED(DM_SPI_FLASH) |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 387 | struct udevice *new; |
| 388 | |
| 389 | /* speed and mode will be read from DT */ |
Tom Rini | 119d2fb | 2021-12-11 14:55:48 -0500 | [diff] [blame] | 390 | ret = spi_flash_probe_bus_cs(CONFIG_SF_DEFAULT_BUS, |
Patrice Chotard | 05b1aa6 | 2022-03-30 09:33:14 +0200 | [diff] [blame] | 391 | CONFIG_SF_DEFAULT_CS, &new); |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 392 | |
| 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 Rini | 376b88a | 2022-10-28 20:27:13 -0400 | [diff] [blame] | 405 | CFG_SYS_FSL_QSPI_BASE, |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 406 | 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, ®->fm_imem, addr); |
| 435 | if (rc) |
| 436 | return rc; |
| 437 | env_set_addr("fman_ucode", addr); |
| 438 | |
| 439 | fm_init_muram(index, ®->muram); |
| 440 | fm_init_qmi(®->fm_qmi_common); |
| 441 | fm_init_fpm(®->fm_fpm); |
| 442 | |
| 443 | /* clear DMA status */ |
| 444 | setbits_be32(®->fm_dma.fmdmsr, FMDMSR_CLEAR_ALL); |
| 445 | |
| 446 | /* set DMA mode */ |
| 447 | setbits_be32(®->fm_dma.fmdmmr, FMDMMR_SBER); |
| 448 | |
| 449 | return fm_init_bmi(index, ®->fm_bmi_common); |
| 450 | } |
| 451 | #else |
Sean Anderson | 394b4ef | 2022-12-29 11:53:00 -0500 | [diff] [blame] | 452 | 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] | 453 | { |
| 454 | int rc; |
Sean Anderson | b55b750 | 2022-12-29 11:53:01 -0500 | [diff] [blame] | 455 | #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 Qiang | 83a9084 | 2014-03-21 16:21:44 +0800 | [diff] [blame] | 478 | void *addr = (void *)CONFIG_SYS_FMAN_FW_ADDR; |
Timur Tabi | 275f4bb | 2011-11-22 09:21:25 -0600 | [diff] [blame] | 479 | #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 Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 482 | |
Grygorii Strashko | aa942fe | 2017-06-26 19:13:00 -0500 | [diff] [blame] | 483 | rc = nand_read(get_nand_dev_by_index(0), |
| 484 | (loff_t)CONFIG_SYS_FMAN_FW_ADDR, |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 485 | &fw_length, (u_char *)addr); |
| 486 | if (rc == -EUCLEAN) { |
| 487 | 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] | 488 | CONFIG_SYS_FMAN_FW_ADDR, rc); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 489 | } |
Tom Rini | fa911f8 | 2019-05-12 07:59:12 -0400 | [diff] [blame] | 490 | #elif defined(CONFIG_SYS_QE_FMAN_FW_IN_SPIFLASH) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 491 | struct spi_flash *ucode_flash; |
Timur Tabi | 275f4bb | 2011-11-22 09:21:25 -0600 | [diff] [blame] | 492 | void *addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 493 | int ret = 0; |
| 494 | |
Lukasz Majewski | 76f44298 | 2020-06-04 23:11:53 +0800 | [diff] [blame] | 495 | #if CONFIG_IS_ENABLED(DM_SPI_FLASH) |
Qianyu Gong | ea4923e | 2016-08-03 11:04:25 +0800 | [diff] [blame] | 496 | struct udevice *new; |
| 497 | |
| 498 | /* speed and mode will be read from DT */ |
Tom Rini | 119d2fb | 2021-12-11 14:55:48 -0500 | [diff] [blame] | 499 | 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] | 500 | &new); |
Qianyu Gong | ea4923e | 2016-08-03 11:04:25 +0800 | [diff] [blame] | 501 | |
| 502 | ucode_flash = dev_get_uclass_priv(new); |
| 503 | #else |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 504 | ucode_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 505 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Qianyu Gong | ea4923e | 2016-08-03 11:04:25 +0800 | [diff] [blame] | 506 | #endif |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 507 | if (!ucode_flash) |
| 508 | printf("SF: probe for ucode failed\n"); |
| 509 | else { |
Zhao Qiang | 83a9084 | 2014-03-21 16:21:44 +0800 | [diff] [blame] | 510 | ret = spi_flash_read(ucode_flash, CONFIG_SYS_FMAN_FW_ADDR, |
Timur Tabi | 275f4bb | 2011-11-22 09:21:25 -0600 | [diff] [blame] | 511 | CONFIG_SYS_QE_FMAN_FW_LENGTH, addr); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 512 | if (ret) |
| 513 | printf("SF: read for ucode failed\n"); |
| 514 | spi_flash_free(ucode_flash); |
| 515 | } |
Timur Tabi | 275f4bb | 2011-11-22 09:21:25 -0600 | [diff] [blame] | 516 | #elif defined(CONFIG_SYS_QE_FMAN_FW_IN_MMC) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 517 | int dev = CONFIG_SYS_MMC_ENV_DEV; |
Timur Tabi | 275f4bb | 2011-11-22 09:21:25 -0600 | [diff] [blame] | 518 | void *addr = malloc(CONFIG_SYS_QE_FMAN_FW_LENGTH); |
| 519 | u32 cnt = CONFIG_SYS_QE_FMAN_FW_LENGTH / 512; |
Zhao Qiang | 83a9084 | 2014-03-21 16:21:44 +0800 | [diff] [blame] | 520 | u32 blk = CONFIG_SYS_FMAN_FW_ADDR / 512; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 521 | 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 Zhu | 45c20bd | 2018-09-25 14:47:06 +0800 | [diff] [blame] | 529 | (void)blk_dread(mmc_get_blk_desc(mmc), blk, cnt, |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 530 | addr); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 531 | } |
Liu Gang | 1e08458 | 2012-03-08 00:33:18 +0000 | [diff] [blame] | 532 | #elif defined(CONFIG_SYS_QE_FMAN_FW_IN_REMOTE) |
Zhao Qiang | 83a9084 | 2014-03-21 16:21:44 +0800 | [diff] [blame] | 533 | void *addr = (void *)CONFIG_SYS_FMAN_FW_ADDR; |
York Sun | 5f321a3 | 2013-06-25 11:37:40 -0700 | [diff] [blame] | 534 | #else |
| 535 | void *addr = NULL; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 536 | #endif |
| 537 | |
Sean Anderson | 9a2c773 | 2022-09-07 13:44:55 +0800 | [diff] [blame] | 538 | 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 Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 555 | /* Upload the Fman microcode if it's present */ |
| 556 | rc = fman_upload_firmware(index, ®->fm_imem, addr); |
| 557 | if (rc) |
| 558 | return rc; |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 559 | env_set_addr("fman_ucode", addr); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 560 | |
| 561 | fm_init_muram(index, ®->muram); |
| 562 | fm_init_qmi(®->fm_qmi_common); |
| 563 | fm_init_fpm(®->fm_fpm); |
| 564 | |
| 565 | /* clear DMA status */ |
| 566 | setbits_be32(®->fm_dma.fmdmsr, FMDMSR_CLEAR_ALL); |
| 567 | |
| 568 | /* set DMA mode */ |
| 569 | setbits_be32(®->fm_dma.fmdmmr, FMDMMR_SBER); |
| 570 | |
| 571 | return fm_init_bmi(index, ®->fm_bmi_common); |
| 572 | } |
Rajesh Bhagat | 1dec961 | 2018-11-05 18:02:23 +0000 | [diff] [blame] | 573 | #endif |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 574 | |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 575 | struct fman_priv { |
| 576 | struct ccsr_fman *reg; |
| 577 | unsigned int fman_id; |
| 578 | }; |
| 579 | |
| 580 | static const struct udevice_id fman_ids[] = { |
| 581 | { .compatible = "fsl,fman" }, |
| 582 | {} |
| 583 | }; |
| 584 | |
| 585 | static int fman_probe(struct udevice *dev) |
| 586 | { |
Sean Anderson | 394b4ef | 2022-12-29 11:53:00 -0500 | [diff] [blame] | 587 | const char *firmware_name = NULL; |
| 588 | int ret; |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 589 | 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 Anderson | 394b4ef | 2022-12-29 11:53:00 -0500 | [diff] [blame] | 598 | 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 Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static int fman_remove(struct udevice *dev) |
| 608 | { |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | int fman_id(struct udevice *dev) |
| 613 | { |
| 614 | struct fman_priv *priv = dev_get_priv(dev); |
| 615 | |
| 616 | return priv->fman_id; |
| 617 | } |
| 618 | |
| 619 | void *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 | |
| 626 | void *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 | |
| 648 | U_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 Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 654 | .priv_auto = sizeof(struct fman_priv), |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 655 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 656 | }; |