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