Christophe Leroy | f6469c2 | 2023-04-04 13:09:36 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2006-2023 CS GROUP France |
| 4 | */ |
| 5 | |
| 6 | #include <command.h> |
Christophe Leroy | f6469c2 | 2023-04-04 13:09:36 +0200 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <env.h> |
| 9 | #include <env_internal.h> |
| 10 | #include <eeprom.h> |
| 11 | #include <fdt_support.h> |
| 12 | #include <hang.h> |
| 13 | #include <ioports.h> |
| 14 | #include <mpc83xx.h> |
| 15 | #include <netdev.h> |
| 16 | #include <spi.h> |
| 17 | #include <stdarg.h> |
| 18 | #include <stdlib.h> |
| 19 | |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/immap_qe.h> |
| 22 | #include <linux/libfdt.h> |
| 23 | #include <linux/log2.h> |
| 24 | #include <linux/sizes.h> |
| 25 | |
| 26 | #include <asm/io.h> |
| 27 | #include <asm/global_data.h> |
| 28 | #include <asm/mmu.h> |
| 29 | |
| 30 | #include <u-boot/crc.h> |
| 31 | |
| 32 | #include "../common/common.h" |
| 33 | |
| 34 | DECLARE_GLOBAL_DATA_PTR; |
| 35 | |
| 36 | #define ADDR_FPGA_BASE ((unsigned char __iomem *)CONFIG_CPLD_BASE) |
| 37 | #define ADDR_FPGA_RESET_G (ADDR_FPGA_BASE + 0x40) |
| 38 | #define ADDR_FPGA_REG_ETAT (ADDR_FPGA_BASE + 0x42) |
| 39 | |
| 40 | #define R_ETAT_PRES_BASE 0x01 |
| 41 | #define RESET_G_OK 0x08 |
| 42 | |
| 43 | /* SPI EEPROM parameters */ |
| 44 | #define MAX_SPI_BYTES 0x28 |
| 45 | #define EE_OFF_MAC1 0x10 |
| 46 | #define EE_OFF_MAC2 0x16 |
| 47 | #define EE_OFF_MAC3 0x1C |
| 48 | |
| 49 | static uint upma_table[] = { |
| 50 | /* Read Single-Beat (RSS) */ |
| 51 | 0x00AC0C00, 0x00FC1C40, 0x30FCE045, 0xFFFF0C00, |
| 52 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 53 | /* Read Burst (RBS) */ |
| 54 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 55 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 56 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 57 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 58 | /* Write Single-Beat (WSS) */ |
| 59 | 0x00A30C00, 0x00F31C40, 0x3FF3C045, 0xFFFF0C00, |
| 60 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 61 | /* Write Burst (WBS) */ |
| 62 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 63 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 64 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 65 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 66 | /* Refresh Timer (RTS) */ |
| 67 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 68 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 69 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 70 | /* Exception Condition (EXS) */ |
| 71 | 0xFFFF0C01, 0xFFFF0C01, 0xFFFF0C01, 0xFFFF0C01, |
| 72 | }; |
| 73 | |
| 74 | const qe_iop_conf_t qe_iop_conf_tab[] = { |
| 75 | /* ETH3 */ |
| 76 | {1, 0, 1, 0, 1}, /* TxD0 */ |
| 77 | {1, 1, 1, 0, 1}, /* TxD1 */ |
| 78 | {1, 2, 1, 0, 1}, /* TxD2 */ |
| 79 | {1, 3, 1, 0, 1}, /* TxD3 */ |
| 80 | {1, 9, 1, 0, 1}, /* TxER */ |
| 81 | {1, 12, 1, 0, 1}, /* TxEN */ |
| 82 | {3, 24, 2, 0, 1}, /* TxCLK->CLK10 */ |
| 83 | |
| 84 | {1, 4, 2, 0, 1}, /* RxD0 */ |
| 85 | {1, 5, 2, 0, 1}, /* RxD1 */ |
| 86 | {1, 6, 2, 0, 1}, /* RxD2 */ |
| 87 | {1, 7, 2, 0, 1}, /* RxD3 */ |
| 88 | {1, 8, 2, 0, 1}, /* RxER */ |
| 89 | {1, 10, 2, 0, 1}, /* RxDV */ |
| 90 | {0, 13, 2, 0, 1}, /* RxCLK->CLK9 */ |
| 91 | {1, 11, 2, 0, 1}, /* COL */ |
| 92 | {1, 13, 2, 0, 1}, /* CRS */ |
| 93 | |
| 94 | /* ETH4 */ |
| 95 | {1, 18, 1, 0, 1}, /* TxD0 */ |
| 96 | {1, 19, 1, 0, 1}, /* TxD1 */ |
| 97 | {1, 20, 1, 0, 1}, /* TxD2 */ |
| 98 | {1, 21, 1, 0, 1}, /* TxD3 */ |
| 99 | {1, 27, 1, 0, 1}, /* TxER */ |
| 100 | {1, 30, 1, 0, 1}, /* TxEN */ |
| 101 | {3, 6, 2, 0, 1}, /* TxCLK->CLK8 */ |
| 102 | |
| 103 | {1, 22, 2, 0, 1}, /* RxD0 */ |
| 104 | {1, 23, 2, 0, 1}, /* RxD1 */ |
| 105 | {1, 24, 2, 0, 1}, /* RxD2 */ |
| 106 | {1, 25, 2, 0, 1}, /* RxD3 */ |
| 107 | {1, 26, 1, 0, 1}, /* RxER */ |
| 108 | {1, 28, 2, 0, 1}, /* Rx_DV */ |
| 109 | {3, 31, 2, 0, 1}, /* RxCLK->CLK7 */ |
| 110 | {1, 29, 2, 0, 1}, /* COL */ |
| 111 | {1, 31, 2, 0, 1}, /* CRS */ |
| 112 | |
| 113 | {3, 4, 3, 0, 2}, /* MDIO */ |
| 114 | {3, 5, 1, 0, 2}, /* MDC */ |
| 115 | |
| 116 | {0, 0, 0, 0, QE_IOP_TAB_END}, /* END of table */ |
| 117 | }; |
| 118 | |
| 119 | void iop_setup_miae(void) |
| 120 | { |
| 121 | immap_t __iomem *im = (immap_t *)CONFIG_SYS_IMMR; |
| 122 | |
| 123 | /* PORTA configuration */ |
| 124 | out_be32(&im->qepio.ioport[0].pdat, 0x00808000); |
| 125 | out_be32(&im->qepio.ioport[0].podr, 0x00008000); |
| 126 | out_be32(&im->qepio.ioport[0].dir1, 0x40800968); |
| 127 | out_be32(&im->qepio.ioport[0].dir2, 0x650A0896); |
| 128 | out_be32(&im->qepio.ioport[0].ppar1, 0x40400204); |
| 129 | out_be32(&im->qepio.ioport[0].ppar2, 0x05050464); |
| 130 | |
| 131 | /* PORTB configuration */ |
| 132 | out_be32(&im->qepio.ioport[1].pdat, 0x00018000); |
| 133 | out_be32(&im->qepio.ioport[1].podr, 0x00000000); |
| 134 | out_be32(&im->qepio.ioport[1].dir1, 0x50A08949); |
| 135 | out_be32(&im->qepio.ioport[1].dir2, 0x5C0C6890); |
| 136 | out_be32(&im->qepio.ioport[1].ppar1, 0x50504644); |
| 137 | out_be32(&im->qepio.ioport[1].ppar2, 0x080800A0); |
| 138 | |
| 139 | /* PORTC configuration */ |
| 140 | out_be32(&im->qepio.ioport[2].pdat, 0x3D000108); |
| 141 | out_be32(&im->qepio.ioport[2].podr, 0x00000000); |
| 142 | out_be32(&im->qepio.ioport[2].dir1, 0x45518000); |
| 143 | out_be32(&im->qepio.ioport[2].dir2, 0xA8119561); |
| 144 | out_be32(&im->qepio.ioport[2].ppar1, 0x80008000); |
| 145 | out_be32(&im->qepio.ioport[2].ppar2, 0x00000000); |
| 146 | |
| 147 | /* PORTD configuration */ |
| 148 | out_be32(&im->qepio.ioport[3].pdat, 0x1000E000); |
| 149 | out_be32(&im->qepio.ioport[3].podr, 0x0000E000); |
| 150 | out_be32(&im->qepio.ioport[3].dir1, 0xFDD20800); |
| 151 | out_be32(&im->qepio.ioport[3].dir2, 0x54155228); |
| 152 | out_be32(&im->qepio.ioport[3].ppar1, 0x54A30C00); |
| 153 | out_be32(&im->qepio.ioport[3].ppar2, 0x00000100); |
| 154 | } |
| 155 | |
| 156 | void iop_setup_mcr(void) |
| 157 | { |
| 158 | immap_t __iomem *im = (immap_t *)CONFIG_SYS_IMMR; |
| 159 | |
| 160 | /* PORTA configuration */ |
| 161 | out_be32(&im->qepio.ioport[0].pdat, 0x00808004); |
| 162 | out_be32(&im->qepio.ioport[0].podr, 0x00000000); |
| 163 | out_be32(&im->qepio.ioport[0].dir1, 0x40800A68); |
| 164 | out_be32(&im->qepio.ioport[0].dir2, 0x650A0896); |
| 165 | out_be32(&im->qepio.ioport[0].ppar1, 0x40400004); |
| 166 | out_be32(&im->qepio.ioport[0].ppar2, 0x05050444); |
| 167 | |
| 168 | /* PORTB configuration */ |
| 169 | out_be32(&im->qepio.ioport[1].pdat, 0x00008000); |
| 170 | out_be32(&im->qepio.ioport[1].podr, 0x00000004); |
| 171 | out_be32(&im->qepio.ioport[1].dir1, 0x50A08A4A); |
| 172 | out_be32(&im->qepio.ioport[1].dir2, 0x5C0C6890); |
| 173 | out_be32(&im->qepio.ioport[1].ppar1, 0x50504444); |
| 174 | out_be32(&im->qepio.ioport[1].ppar2, 0x08080080); |
| 175 | |
| 176 | /* PORTC configuration */ |
| 177 | out_be32(&im->qepio.ioport[2].pdat, 0x3D000018); |
| 178 | out_be32(&im->qepio.ioport[2].podr, 0x00000400); |
| 179 | out_be32(&im->qepio.ioport[2].dir1, 0x45518000); |
| 180 | out_be32(&im->qepio.ioport[2].dir2, 0xA8129561); |
| 181 | out_be32(&im->qepio.ioport[2].ppar1, 0x80008000); |
| 182 | out_be32(&im->qepio.ioport[2].ppar2, 0x00000000); |
| 183 | |
| 184 | /* PORTD configuration */ |
| 185 | out_be32(&im->qepio.ioport[3].pdat, 0x1000E000); |
| 186 | out_be32(&im->qepio.ioport[3].podr, 0x0000E000); |
| 187 | out_be32(&im->qepio.ioport[3].dir1, 0xFDD20800); |
| 188 | out_be32(&im->qepio.ioport[3].dir2, 0x54155228); |
| 189 | out_be32(&im->qepio.ioport[3].ppar1, 0x54A30C00); |
| 190 | out_be32(&im->qepio.ioport[3].ppar2, 0x00000100); |
| 191 | } |
| 192 | |
| 193 | static void iop_setup_cmpcpro(void) |
| 194 | { |
| 195 | immap_t __iomem *im = (immap_t *)CONFIG_SYS_IMMR; |
| 196 | |
| 197 | /* PORTA configuration */ |
| 198 | out_be32(&im->qepio.ioport[0].pdat, 0x00000000); |
| 199 | out_be32(&im->qepio.ioport[0].podr, 0x00000000); |
| 200 | out_be32(&im->qepio.ioport[0].dir1, 0x50A84020); |
| 201 | out_be32(&im->qepio.ioport[0].dir2, 0x00000000); |
| 202 | out_be32(&im->qepio.ioport[0].ppar1, 0xF0FCC000); |
| 203 | out_be32(&im->qepio.ioport[0].ppar2, 0x00000000); |
| 204 | |
| 205 | /* PORTB configuration */ |
| 206 | out_be32(&im->qepio.ioport[1].pdat, 0x00000000); |
| 207 | out_be32(&im->qepio.ioport[1].podr, 0x00000000); |
| 208 | out_be32(&im->qepio.ioport[1].dir1, 0x00000000); |
| 209 | out_be32(&im->qepio.ioport[1].dir2, 0x00006800); |
| 210 | out_be32(&im->qepio.ioport[1].ppar1, 0x00000000); |
| 211 | out_be32(&im->qepio.ioport[1].ppar2, 0x00000000); |
| 212 | |
| 213 | /* PORTC configuration */ |
| 214 | out_be32(&im->qepio.ioport[2].pdat, 0x19000000); |
| 215 | out_be32(&im->qepio.ioport[2].podr, 0x00000000); |
| 216 | out_be32(&im->qepio.ioport[2].dir1, 0x01410000); |
| 217 | out_be32(&im->qepio.ioport[2].dir2, 0xA8009400); |
| 218 | out_be32(&im->qepio.ioport[2].ppar1, 0x00000000); |
| 219 | out_be32(&im->qepio.ioport[2].ppar2, 0x00000000); |
| 220 | |
| 221 | /* PORTD configuration */ |
| 222 | out_be32(&im->qepio.ioport[3].pdat, 0x1000E000); |
| 223 | out_be32(&im->qepio.ioport[3].podr, 0x0000E000); |
| 224 | out_be32(&im->qepio.ioport[3].dir1, 0xFD020000); |
| 225 | out_be32(&im->qepio.ioport[3].dir2, 0x54055000); |
| 226 | out_be32(&im->qepio.ioport[3].ppar1, 0x54030000); |
| 227 | out_be32(&im->qepio.ioport[3].ppar2, 0x00000000); |
| 228 | } |
| 229 | |
| 230 | int board_early_init_r(void) |
| 231 | { |
| 232 | immap_t __iomem *im = (immap_t *)CONFIG_SYS_IMMR; |
| 233 | fsl_lbc_t *lbus = &im->im_lbc; |
| 234 | |
| 235 | upmconfig(UPMA, upma_table, ARRAY_SIZE(upma_table)); |
| 236 | |
| 237 | out_be32(&lbus->mamr, 0x00044440); |
| 238 | |
| 239 | /* configure LBCR register */ |
| 240 | out_be32(&lbus->lbcr, 0x00000500); |
| 241 | sync(); |
| 242 | |
| 243 | if (in_8(ADDR_FPGA_REG_ETAT) & R_ETAT_PRES_BASE) { |
| 244 | int i; |
| 245 | |
| 246 | /* Initialize signal PROG_FPGA_FIRMWARE */ |
| 247 | setbits_be32(&im->qepio.ioport[0].pdat, 0x00008000); |
| 248 | setbits_be32(&im->qepio.ioport[0].dir2, 0x60000002); |
| 249 | setbits_be32(&im->qepio.ioport[0].podr, 0x00008000); |
| 250 | |
| 251 | mdelay(1); |
| 252 | |
| 253 | /* Now read CPDATA[31] to check if FPGA is loaded */ |
| 254 | if (!in_be32(&im->qepio.ioport[0].pdat) & 0x00000001) { |
| 255 | printf("Reloading FPGA firmware.\n"); |
| 256 | |
| 257 | clrbits_be32(&im->qepio.ioport[0].pdat, 0x00008000); |
| 258 | udelay(1); |
| 259 | setbits_be32(&im->qepio.ioport[0].pdat, 0x00008000); |
| 260 | |
| 261 | /* Wait 200 msec and check DONE_FPGA_FIRMWARE */ |
| 262 | mdelay(200); |
| 263 | if (!(in_be32(&im->qepio.ioport[0].pdat) & 0x00000001)) { |
| 264 | for (;;) { |
| 265 | printf("error loading firmware.\n"); |
| 266 | mdelay(500); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /* Send a reset signal and wait for 20 msec */ |
| 271 | out_8(ADDR_FPGA_RESET_G, in_8(ADDR_FPGA_RESET_G) | RESET_G_OK); |
| 272 | mdelay(20); |
| 273 | out_8(ADDR_FPGA_RESET_G, in_8(ADDR_FPGA_RESET_G) & ~RESET_G_OK); |
| 274 | } |
| 275 | |
| 276 | /* Wait 300 msec and check the reset state */ |
| 277 | mdelay(300); |
| 278 | for (i = 0; !(in_8(ADDR_FPGA_REG_ETAT) & RESET_G_OK); i++) { |
| 279 | for (;;) { |
| 280 | printf("Could not reset FPGA.\n"); |
| 281 | mdelay(500); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | iop_setup_common(); |
| 286 | |
| 287 | /* clocks configuration */ |
| 288 | out_be32(&qe_immr->qmx.cmxsi1cr_l, 0x00040004); |
| 289 | out_be32(&qe_immr->qmx.cmxsi1syr, 0x00000000); |
| 290 | } else { |
| 291 | iop_setup_cmpcpro(); |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | int dram_init(int board_type) |
| 298 | { |
| 299 | immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR; |
| 300 | |
| 301 | out_be32(&im->sysconf.ddrlaw[0].bar, CFG_SYS_DDR_SDRAM_BASE & LAWBAR_BAR); |
| 302 | out_be32(&im->sysconf.ddrlaw[0].ar, LAWAR_EN | ((ilog2(SZ_512M) - 1) & LAWAR_SIZE)); |
| 303 | |
| 304 | out_be32(&im->ddr.sdram_clk_cntl, CFG_SYS_DDR_CLK_CNTL); |
| 305 | out_be32(&im->ddr.csbnds[0].csbnds, CFG_SYS_DDR_CS0_BNDS); |
| 306 | out_be32(&im->ddr.cs_config[0], CFG_SYS_DDR_CS0_CONFIG); |
| 307 | |
| 308 | out_be32(&im->ddr.timing_cfg_0, CFG_SYS_DDR_TIMING_0); |
| 309 | out_be32(&im->ddr.timing_cfg_1, CFG_SYS_DDR_TIMING_1); |
| 310 | out_be32(&im->ddr.timing_cfg_2, CFG_SYS_DDR_TIMING_2); |
| 311 | out_be32(&im->ddr.timing_cfg_3, CFG_SYS_DDR_TIMING_3); |
| 312 | out_be32(&im->ddr.sdram_cfg, CFG_SYS_DDR_SDRAM_CFG); |
| 313 | out_be32(&im->ddr.sdram_cfg2, CFG_SYS_DDR_SDRAM_CFG2); |
| 314 | out_be32(&im->ddr.sdram_mode, CFG_SYS_DDR_MODE); |
| 315 | out_be32(&im->ddr.sdram_mode2, CFG_SYS_DDR_MODE2); |
| 316 | out_be32(&im->ddr.sdram_interval, CFG_SYS_DDR_INTERVAL); |
| 317 | udelay(200); |
| 318 | |
| 319 | setbits_be32(&im->ddr.sdram_cfg, SDRAM_CFG_MEM_EN); |
| 320 | |
| 321 | gd->ram_size = SZ_512M; |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | int checkboard(void) |
| 327 | { |
| 328 | printf("Board: "); |
| 329 | |
| 330 | /* Is a motherboard present ? */ |
| 331 | if (in_8(ADDR_FPGA_REG_ETAT) & R_ETAT_PRES_BASE) |
| 332 | return checkboard_common(); |
| 333 | |
| 334 | printf("CMPCPRO (CS GROUP)\n"); |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | /* Reads MAC addresses from SPI EEPROM */ |
| 340 | static int setup_mac(void) |
| 341 | { |
| 342 | uchar din[MAX_SPI_BYTES]; |
| 343 | int ret; |
| 344 | unsigned long ident = 0x08005120; |
| 345 | |
| 346 | ret = read_eeprom(din, sizeof(din)); |
| 347 | if (ret) |
| 348 | return ret; |
| 349 | |
| 350 | if (memcmp(din + EE_OFF_MAC1, &ident, sizeof(ident)) == 0) { |
| 351 | eth_env_set_enetaddr("ethaddr", din + EE_OFF_MAC1); |
| 352 | eth_env_set_enetaddr("eth3addr", din + EE_OFF_MAC1); |
| 353 | } |
| 354 | |
| 355 | if (memcmp(din + EE_OFF_MAC2, &ident, sizeof(ident)) == 0) |
| 356 | eth_env_set_enetaddr("eth1addr", din + EE_OFF_MAC2); |
| 357 | |
| 358 | if (memcmp(din + EE_OFF_MAC3, &ident, sizeof(ident)) == 0) |
| 359 | eth_env_set_enetaddr("eth2addr", din + EE_OFF_MAC3); |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | int misc_init_r(void) |
| 365 | { |
| 366 | /* we do not modify environment variable area if CRC is false */ |
| 367 | /* Verify if mother board is present */ |
| 368 | if (in_8(ADDR_FPGA_REG_ETAT) & R_ETAT_PRES_BASE) { |
| 369 | misc_init_r_common(); |
| 370 | } else { |
| 371 | env_set("config", CFG_BOARD_CMPCXXX); |
| 372 | env_set("hostname", CFG_BOARD_CMPCXXX); |
| 373 | } |
| 374 | |
| 375 | if (setup_mac()) |
| 376 | printf("Error retrieving mac addresses\n"); |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | int ft_board_setup(void *blob, struct bd_info *bd) |
| 382 | { |
| 383 | ft_cpu_setup(blob, bd); |
| 384 | |
| 385 | /* MIAE only */ |
| 386 | if (!(in_8(ADDR_FPGA_REG_ETAT) & R_ETAT_PRES_BASE)) |
| 387 | return 0; |
| 388 | |
| 389 | return ft_board_setup_common(blob); |
| 390 | } |
| 391 | |
| 392 | void ft_board_setup_phy3(void) |
| 393 | { |
| 394 | /* switch to phy3 with gpio, we'll only use phy3 */ |
| 395 | immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
| 396 | |
| 397 | setbits_be32(&immr->qepio.ioport[2].pdat, 0x00000400); |
| 398 | } |