Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / board / esd / vme8349 / vme8349.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * vme8349.c -- esd VME8349 board support
4  *
5  * Copyright (c) 2008-2009 esd gmbh.
6  *
7  * (C) Copyright 2006
8  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9  *
10  * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
11  * Based on board/mpc8349emds/mpc8349emds.c (and previous 834x releases.)
12  */
13
14 #include <common.h>
15 #include <fdt_support.h>
16 #include <init.h>
17 #include <ioports.h>
18 #include <mpc83xx.h>
19 #include <net.h>
20 #include <asm/mpc8349_pci.h>
21 #if defined(CONFIG_OF_LIBFDT)
22 #include <linux/libfdt.h>
23 #endif
24 #include <asm/io.h>
25 #include <asm/mmu.h>
26 #include <spd.h>
27 #include <spd_sdram.h>
28 #include <i2c.h>
29 #include <netdev.h>
30
31 DECLARE_GLOBAL_DATA_PTR;
32
33 void ddr_enable_ecc(unsigned int dram_size);
34
35 int dram_init(void)
36 {
37         volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
38         u32 msize = 0;
39
40         if ((im->sysconf.immrbar & IMMRBAR_BASE_ADDR) != (u32)im)
41                 return -ENXIO;
42
43         /* DDR SDRAM - Main memory */
44         im->sysconf.ddrlaw[0].bar = CONFIG_SYS_SDRAM_BASE & LAWBAR_BAR;
45
46         msize = spd_sdram();
47
48 #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
49         /*
50          * Initialize and enable DDR ECC.
51          */
52         ddr_enable_ecc(msize * 1024 * 1024);
53 #endif
54
55         /* Now check memory size (after ECC is initialized) */
56         msize = get_ram_size(0, msize);
57
58         /* return total bus SDRAM size(bytes)  -- DDR */
59         gd->ram_size = msize * 1024 * 1024;
60
61         return 0;
62 }
63
64 int checkboard(void)
65 {
66 #ifdef CONFIG_TARGET_CADDY2
67         puts("Board: esd VME-CADDY/2\n");
68 #else
69         puts("Board: esd VME-CPU/8349\n");
70 #endif
71
72         return 0;
73 }
74
75 #ifdef CONFIG_TARGET_CADDY2
76 int board_eth_init(bd_t *bis)
77 {
78         return pci_eth_init(bis);
79 }
80 #endif
81
82 #if defined(CONFIG_OF_BOARD_SETUP)
83 int ft_board_setup(void *blob, bd_t *bd)
84 {
85         ft_cpu_setup(blob, bd);
86
87 #ifdef CONFIG_PCI
88         ft_pci_setup(blob, bd);
89 #endif
90
91         return 0;
92 }
93 #endif
94
95 int misc_init_r()
96 {
97         immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
98
99         clrsetbits_be32(&im->im_lbc.lcrr, LBCR_LDIS, 0);
100
101         return 0;
102 }
103
104 /*
105  * Provide SPD values for spd_sdram(). Both boards (VME-CADDY/2
106  * and VME-CADDY/2) have different SDRAM configurations.
107  */
108 #ifdef CONFIG_TARGET_CADDY2
109 #define SMALL_RAM       0xff
110 #define LARGE_RAM       0x00
111 #else
112 #define SMALL_RAM       0x00
113 #define LARGE_RAM       0xff
114 #endif
115
116 #define SPD_VAL(a, b)   (((a) & SMALL_RAM) | ((b) & LARGE_RAM))
117
118 static spd_eeprom_t default_spd_eeprom = {
119         SPD_VAL(0x80, 0x80),    /* 00 use 128 Bytes */
120         SPD_VAL(0x07, 0x07),    /* 01 use 128 Bytes */
121         SPD_MEMTYPE_DDR2,       /* 02 type is DDR2 */
122         SPD_VAL(0x0d, 0x0d),    /* 03 rows: 13 */
123         SPD_VAL(0x09, 0x0a),    /* 04 cols:  9 / 10 */
124         SPD_VAL(0x00, 0x00),    /* 05 */
125         SPD_VAL(0x40, 0x40),    /* 06 */
126         SPD_VAL(0x00, 0x00),    /* 07 */
127         SPD_VAL(0x05, 0x05),    /* 08 */
128         SPD_VAL(0x30, 0x30),    /* 09 */
129         SPD_VAL(0x45, 0x45),    /* 10 */
130         SPD_VAL(0x02, 0x02),    /* 11 ecc used */
131         SPD_VAL(0x82, 0x82),    /* 12 */
132         SPD_VAL(0x10, 0x10),    /* 13 */
133         SPD_VAL(0x08, 0x08),    /* 14 */
134         SPD_VAL(0x00, 0x00),    /* 15 */
135         SPD_VAL(0x0c, 0x0c),    /* 16 */
136         SPD_VAL(0x04, 0x08),    /* 17 banks: 4 / 8 */
137         SPD_VAL(0x38, 0x38),    /* 18 */
138         SPD_VAL(0x00, 0x00),    /* 19 */
139         SPD_VAL(0x02, 0x02),    /* 20 */
140         SPD_VAL(0x00, 0x00),    /* 21 */
141         SPD_VAL(0x03, 0x03),    /* 22 */
142         SPD_VAL(0x3d, 0x3d),    /* 23 */
143         SPD_VAL(0x45, 0x45),    /* 24 */
144         SPD_VAL(0x50, 0x50),    /* 25 */
145         SPD_VAL(0x45, 0x45),    /* 26 */
146         SPD_VAL(0x3c, 0x3c),    /* 27 */
147         SPD_VAL(0x28, 0x28),    /* 28 */
148         SPD_VAL(0x3c, 0x3c),    /* 29 */
149         SPD_VAL(0x2d, 0x2d),    /* 30 */
150         SPD_VAL(0x20, 0x80),    /* 31 */
151         SPD_VAL(0x20, 0x20),    /* 32 */
152         SPD_VAL(0x27, 0x27),    /* 33 */
153         SPD_VAL(0x10, 0x10),    /* 34 */
154         SPD_VAL(0x17, 0x17),    /* 35 */
155         SPD_VAL(0x3c, 0x3c),    /* 36 */
156         SPD_VAL(0x1e, 0x1e),    /* 37 */
157         SPD_VAL(0x1e, 0x1e),    /* 38 */
158         SPD_VAL(0x00, 0x00),    /* 39 */
159         SPD_VAL(0x00, 0x06),    /* 40 */
160         SPD_VAL(0x37, 0x37),    /* 41 */
161         SPD_VAL(0x4b, 0x7f),    /* 42 */
162         SPD_VAL(0x80, 0x80),    /* 43 */
163         SPD_VAL(0x18, 0x18),    /* 44 */
164         SPD_VAL(0x22, 0x22),    /* 45 */
165         SPD_VAL(0x00, 0x00),    /* 46 */
166         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
167         SPD_VAL(0x10, 0x10),    /* 62 */
168         SPD_VAL(0x7e, 0x1d),    /* 63 */
169         { 'e', 's', 'd', '-', 'g', 'm', 'b', 'h' },
170         SPD_VAL(0x00, 0x00),    /* 72 */
171 #ifdef CONFIG_TARGET_CADDY2
172         { "vme-caddy/2 ram   " }
173 #else
174         { "vme-cpu/2 ram     " }
175 #endif
176 };
177
178 int vme8349_read_spd(uchar chip, uint addr, int alen, uchar *buffer, int len)
179 {
180         int old_bus = i2c_get_bus_num();
181         unsigned int l, sum;
182         int valid = 0;
183
184         i2c_set_bus_num(0);
185
186         if (i2c_read(chip, addr, alen, buffer, len) == 0)
187                 if (memcmp(&buffer[64], &default_spd_eeprom.mid[0], 8) == 0) {
188                         sum = 0;
189                         for (l = 0; l < 63; l++)
190                                 sum = (sum + buffer[l]) & 0xff;
191                         if (sum == buffer[63])
192                                 valid = 1;
193                         else
194                                 printf("Invalid checksum in EEPROM %02x %02x\n",
195                                        sum, buffer[63]);
196                 }
197
198         if (valid == 0) {
199                 memcpy(buffer, (void *)&default_spd_eeprom, len);
200                 sum = 0;
201                 for (l = 0; l < 63; l++)
202                         sum = (sum + buffer[l]) & 0xff;
203                 if (sum != buffer[63])
204                         printf("Invalid checksum in FLASH %02x %02x\n",
205                                sum, buffer[63]);
206                 buffer[63] = sum;
207         }
208
209         i2c_set_bus_num(old_bus);
210
211         return 0;
212 }