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