77fe7e243f9eed57a71d0f59c5d820bef4b81ff2
[oweals/u-boot.git] / board / tqc / tqm834x / tqm834x.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2005
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 #include <common.h>
8 #include <fdt_support.h>
9 #include <init.h>
10 #include <ioports.h>
11 #include <log.h>
12 #include <mpc83xx.h>
13 #include <asm/mpc8349_pci.h>
14 #include <i2c.h>
15 #include <miiphy.h>
16 #include <asm/mmu.h>
17 #include <pci.h>
18 #include <flash.h>
19 #include <mtd/cfi_flash.h>
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 #define IOSYNC                  asm("eieio")
24 #define ISYNC                   asm("isync")
25 #define SYNC                    asm("sync")
26 #define FPW                     FLASH_PORT_WIDTH
27 #define FPWV                    FLASH_PORT_WIDTHV
28
29 #define DDR_MAX_SIZE_PER_CS     0x20000000
30
31 #if defined(DDR_CASLAT_20)
32 #define TIMING_CASLAT           TIMING_CFG1_CASLAT_20
33 #define MODE_CASLAT             DDR_MODE_CASLAT_20
34 #else
35 #define TIMING_CASLAT           TIMING_CFG1_CASLAT_25
36 #define MODE_CASLAT             DDR_MODE_CASLAT_25
37 #endif
38
39 #define INITIAL_CS_CONFIG       (CSCONFIG_EN | CSCONFIG_ROW_BIT_12 | \
40                                 CSCONFIG_COL_BIT_9)
41
42 /* External definitions */
43 ulong flash_get_size (ulong base, int banknum);
44
45 /* Local functions */
46 static int detect_num_flash_banks(void);
47 static long int get_ddr_bank_size(short cs, long *base);
48 static void set_cs_bounds(short cs, ulong base, ulong size);
49 static void set_cs_config(short cs, long config);
50 static void set_ddr_config(void);
51
52 /* Local variable */
53 static volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
54
55 /**************************************************************************
56  * Board initialzation after relocation to RAM. Used to detect the number
57  * of Flash banks on TQM834x.
58  */
59 int board_early_init_r (void) {
60         /* sanity check, IMMARBAR should be mirrored at offset zero of IMMR */
61         if ((im->sysconf.immrbar & IMMRBAR_BASE_ADDR) != (u32)im)
62                 return 0;
63
64         /* detect the number of Flash banks */
65         return detect_num_flash_banks();
66 }
67
68 /**************************************************************************
69  * DRAM initalization and size detection
70  */
71 int dram_init(void)
72 {
73         long bank_size;
74         long size;
75         int cs;
76
77         /* during size detection, set up the max DDRLAW size */
78         im->sysconf.ddrlaw[0].bar = CONFIG_SYS_SDRAM_BASE;
79         im->sysconf.ddrlaw[0].ar = (LAWAR_EN | LAWAR_SIZE_2G);
80
81         /* set CS bounds to maximum size */
82         for(cs = 0; cs < 4; ++cs) {
83                 set_cs_bounds(cs,
84                         CONFIG_SYS_SDRAM_BASE + (cs * DDR_MAX_SIZE_PER_CS),
85                         DDR_MAX_SIZE_PER_CS);
86
87                 set_cs_config(cs, INITIAL_CS_CONFIG);
88         }
89
90         /* configure ddr controller */
91         set_ddr_config();
92
93         udelay(200);
94
95         /* enable DDR controller */
96         im->ddr.sdram_cfg = (SDRAM_CFG_MEM_EN |
97                 SDRAM_CFG_SREN |
98                 SDRAM_CFG_SDRAM_TYPE_DDR1);
99         SYNC;
100
101         /* size detection */
102         debug("\n");
103         size = 0;
104         for(cs = 0; cs < 4; ++cs) {
105                 debug("\nDetecting Bank%d\n", cs);
106
107                 bank_size = get_ddr_bank_size(cs,
108                         (long *)(CONFIG_SYS_SDRAM_BASE + size));
109                 size += bank_size;
110
111                 debug("DDR Bank%d size: %ld MiB\n\n", cs, bank_size >> 20);
112
113                 /* exit if less than one bank */
114                 if(size < DDR_MAX_SIZE_PER_CS) break;
115         }
116
117         gd->ram_size = size;
118
119         return 0;
120 }
121
122 /**************************************************************************
123  * checkboard()
124  */
125 int checkboard (void)
126 {
127         puts("Board: TQM834x\n");
128
129 #ifdef CONFIG_PCI
130         volatile immap_t * immr;
131         u32 w, f;
132
133         immr = (immap_t *)CONFIG_SYS_IMMR;
134         if (!(immr->reset.rcwh & HRCWH_PCI_HOST)) {
135                 printf("PCI:   NOT in host mode..?!\n");
136                 return 0;
137         }
138
139         /* get bus width */
140         w = 32;
141         if (immr->reset.rcwh & HRCWH_64_BIT_PCI)
142                 w = 64;
143
144         /* get clock */
145         f = gd->pci_clk;
146
147         printf("PCI1:  %d bit, %d MHz\n", w, f / 1000000);
148 #else
149         printf("PCI:   disabled\n");
150 #endif
151         return 0;
152 }
153
154
155 /**************************************************************************
156  *
157  * Local functions
158  *
159  *************************************************************************/
160
161 /**************************************************************************
162  * Detect the number of flash banks (1 or 2). Store it in
163  * a global variable tqm834x_num_flash_banks.
164  * Bank detection code based on the Monitor code.
165  */
166 static int detect_num_flash_banks(void)
167 {
168         typedef unsigned long FLASH_PORT_WIDTH;
169         typedef volatile unsigned long FLASH_PORT_WIDTHV;
170         FPWV *bank1_base;
171         FPWV *bank2_base;
172         FPW bank1_read;
173         FPW bank2_read;
174         ulong bank1_size;
175         ulong bank2_size;
176         ulong total_size;
177
178         cfi_flash_num_flash_banks = 2;  /* assume two banks */
179
180         /* Get bank 1 and 2 information */
181         bank1_size = flash_get_size(CONFIG_SYS_FLASH_BASE, 0);
182         debug("Bank1 size: %lu\n", bank1_size);
183         bank2_size = flash_get_size(CONFIG_SYS_FLASH_BASE + bank1_size, 1);
184         debug("Bank2 size: %lu\n", bank2_size);
185         total_size = bank1_size + bank2_size;
186
187         if (bank2_size > 0) {
188                 /* Seems like we've got bank 2, but maybe it's mirrored 1 */
189
190                 /* Set the base addresses */
191                 bank1_base = (FPWV *) (CONFIG_SYS_FLASH_BASE);
192                 bank2_base = (FPWV *) (CONFIG_SYS_FLASH_BASE + bank1_size);
193
194                 /* Put bank 2 into CFI command mode and read */
195                 bank2_base[0x55] = 0x00980098;
196                 IOSYNC;
197                 ISYNC;
198                 bank2_read = bank2_base[0x10];
199
200                 /* Read from bank 1 (it's in read mode) */
201                 bank1_read = bank1_base[0x10];
202
203                 /* Reset Flash */
204                 bank1_base[0] = 0x00F000F0;
205                 bank2_base[0] = 0x00F000F0;
206
207                 if (bank2_read == bank1_read) {
208                         /*
209                          * Looks like just one bank, but not sure yet. Let's
210                          * read from bank 2 in autosoelect mode.
211                          */
212                         bank2_base[0x0555] = 0x00AA00AA;
213                         bank2_base[0x02AA] = 0x00550055;
214                         bank2_base[0x0555] = 0x00900090;
215                         IOSYNC;
216                         ISYNC;
217                         bank2_read = bank2_base[0x10];
218
219                         /* Read from bank 1 (it's in read mode) */
220                         bank1_read = bank1_base[0x10];
221
222                         /* Reset Flash */
223                         bank1_base[0] = 0x00F000F0;
224                         bank2_base[0] = 0x00F000F0;
225
226                         if (bank2_read == bank1_read) {
227                                 /*
228                                  * In both CFI command and autoselect modes,
229                                  * we got the some data reading from Flash.
230                                  * There is only one mirrored bank.
231                                  */
232                                 cfi_flash_num_flash_banks = 1;
233                                 total_size = bank1_size;
234                         }
235                 }
236         }
237
238         debug("Number of flash banks detected: %d\n", cfi_flash_num_flash_banks);
239
240         /* set OR0 and BR0 */
241         set_lbc_or(0, OR_GPCM_CSNT | OR_GPCM_ACS_DIV4 | OR_GPCM_SCY_5 |
242                    OR_GPCM_TRLX | (-(total_size) & OR_GPCM_AM));
243         set_lbc_br(0, (CONFIG_SYS_FLASH_BASE & BR_BA) |
244                    (BR_MS_GPCM | BR_PS_32 | BR_V));
245
246         return (0);
247 }
248
249 /*************************************************************************
250  * Detect the size of a ddr bank. Sets CS bounds and CS config accordingly.
251  */
252 static long int get_ddr_bank_size(short cs, long *base)
253 {
254         /* This array lists all valid DDR SDRAM configurations, with
255          * Bank sizes in bytes. (Refer to Table 9-27 in the MPC8349E RM).
256          * The last entry has to to have size equal 0 and is igonred during
257          * autodection. Bank sizes must be in increasing order of size
258          */
259         struct {
260                 long row;
261                 long col;
262                 long size;
263         } conf[] = {
264                 {CSCONFIG_ROW_BIT_12,   CSCONFIG_COL_BIT_8,     32 << 20},
265                 {CSCONFIG_ROW_BIT_12,   CSCONFIG_COL_BIT_9,     64 << 20},
266                 {CSCONFIG_ROW_BIT_12,   CSCONFIG_COL_BIT_10,    128 << 20},
267                 {CSCONFIG_ROW_BIT_13,   CSCONFIG_COL_BIT_9,     128 << 20},
268                 {CSCONFIG_ROW_BIT_13,   CSCONFIG_COL_BIT_10,    256 << 20},
269                 {CSCONFIG_ROW_BIT_13,   CSCONFIG_COL_BIT_11,    512 << 20},
270                 {CSCONFIG_ROW_BIT_14,   CSCONFIG_COL_BIT_10,    512 << 20},
271                 {CSCONFIG_ROW_BIT_14,   CSCONFIG_COL_BIT_11,    1024 << 20},
272                 {0,                     0,                      0}
273         };
274
275         int i;
276         int detected;
277         long size;
278
279         detected = -1;
280         for(i = 0; conf[i].size != 0; ++i) {
281
282                 /* set sdram bank configuration */
283                 set_cs_config(cs, CSCONFIG_EN | conf[i].col | conf[i].row);
284
285                 debug("Getting RAM size...\n");
286                 size = get_ram_size(base, DDR_MAX_SIZE_PER_CS);
287
288                 if((size == conf[i].size) && (i == detected + 1))
289                         detected = i;
290
291                 debug("Trying %ld x %ld (%ld MiB) at addr %p, detected: %ld MiB\n",
292                         conf[i].row,
293                         conf[i].col,
294                         conf[i].size >> 20,
295                         base,
296                         size >> 20);
297         }
298
299         if(detected == -1){
300                 /* disable empty cs */
301                 debug("\nNo valid configurations for CS%d, disabling...\n", cs);
302                 set_cs_config(cs, 0);
303                 return 0;
304         }
305
306         debug("\nDetected configuration %ld x %ld (%ld MiB) at addr %p\n",
307                         conf[detected].row, conf[detected].col, conf[detected].size >> 20, base);
308
309         /* configure cs ro detected params */
310         set_cs_config(cs, CSCONFIG_EN | conf[detected].row |
311                         conf[detected].col);
312
313         set_cs_bounds(cs, (long)base, conf[detected].size);
314
315         return(conf[detected].size);
316 }
317
318 /**************************************************************************
319  * Sets DDR bank CS bounds.
320  */
321 static void set_cs_bounds(short cs, ulong base, ulong size)
322 {
323         debug("Setting bounds %08lx, %08lx for cs %d\n", base, size, cs);
324         if(size == 0){
325                 im->ddr.csbnds[cs].csbnds = 0x00000000;
326         } else {
327                 im->ddr.csbnds[cs].csbnds =
328                         ((base >> CSBNDS_SA_SHIFT) & CSBNDS_SA) |
329                         (((base + size - 1) >> CSBNDS_EA_SHIFT) &
330                                 CSBNDS_EA);
331         }
332         SYNC;
333 }
334
335 /**************************************************************************
336  * Sets DDR banks CS configuration.
337  * config == 0x00000000 disables the CS.
338  */
339 static void set_cs_config(short cs, long config)
340 {
341         debug("Setting config %08lx for cs %d\n", config, cs);
342         im->ddr.cs_config[cs] = config;
343         SYNC;
344 }
345
346 /**************************************************************************
347  * Sets DDR clocks, timings and configuration.
348  */
349 static void set_ddr_config(void) {
350         /* clock control */
351         im->ddr.sdram_clk_cntl = DDR_SDRAM_CLK_CNTL_SS_EN |
352                 DDR_SDRAM_CLK_CNTL_CLK_ADJUST_05;
353         SYNC;
354
355         /* timing configuration */
356         im->ddr.timing_cfg_1 =
357                 (4 << TIMING_CFG1_PRETOACT_SHIFT) |
358                 (7 << TIMING_CFG1_ACTTOPRE_SHIFT) |
359                 (4 << TIMING_CFG1_ACTTORW_SHIFT)  |
360                 (5 << TIMING_CFG1_REFREC_SHIFT)   |
361                 (3 << TIMING_CFG1_WRREC_SHIFT)    |
362                 (3 << TIMING_CFG1_ACTTOACT_SHIFT) |
363                 (1 << TIMING_CFG1_WRTORD_SHIFT)   |
364                 (TIMING_CFG1_CASLAT & TIMING_CASLAT);
365
366         im->ddr.timing_cfg_2 =
367                 TIMING_CFG2_CPO_DEF |
368                 (2 << TIMING_CFG2_WR_DATA_DELAY_SHIFT);
369         SYNC;
370
371         /* don't enable DDR controller yet */
372         im->ddr.sdram_cfg =
373                 SDRAM_CFG_SREN |
374                 SDRAM_CFG_SDRAM_TYPE_DDR1;
375         SYNC;
376
377         /* Set SDRAM mode */
378         im->ddr.sdram_mode =
379                 ((DDR_MODE_EXT_MODEREG | DDR_MODE_WEAK) <<
380                         SDRAM_MODE_ESD_SHIFT) |
381                 ((DDR_MODE_MODEREG | DDR_MODE_BLEN_4) <<
382                         SDRAM_MODE_SD_SHIFT) |
383                 ((DDR_MODE_CASLAT << SDRAM_MODE_SD_SHIFT) &
384                         MODE_CASLAT);
385         SYNC;
386
387         /* Set fast SDRAM refresh rate */
388         im->ddr.sdram_interval =
389                 (DDR_REFINT_166MHZ_7US << SDRAM_INTERVAL_REFINT_SHIFT) |
390                 (DDR_BSTOPRE << SDRAM_INTERVAL_BSTOPRE_SHIFT);
391         SYNC;
392
393         /* Workaround for DDR6 Erratum
394          * see MPC8349E Device Errata Rev.8, 2/2006
395          * This workaround influences the MPC internal "input enables"
396          * dependent on CAS latency and MPC revision. According to errata
397          * sheet the internal reserved registers for this workaround are
398          * not available from revision 2.0 and up.
399          */
400
401         /* Get REVID from register SPRIDR. Skip workaround if rev >= 2.0
402          * (0x200)
403          */
404         if ((im->sysconf.spridr & SPRIDR_REVID) < 0x200) {
405
406                 /* There is a internal reserved register at IMMRBAR+0x2F00
407                  * which has to be written with a certain value defined by
408                  * errata sheet.
409                  */
410                 u32 *reserved_p = (u32 *)((u8 *)im + 0x2f00);
411
412 #if defined(DDR_CASLAT_20)
413                 *reserved_p = 0x201c0000;
414 #else
415                 *reserved_p = 0x202c0000;
416 #endif
417         }
418 }
419
420 #ifdef CONFIG_OF_BOARD_SETUP
421 int ft_board_setup(void *blob, bd_t *bd)
422 {
423         ft_cpu_setup(blob, bd);
424
425 #ifdef CONFIG_PCI
426         ft_pci_setup(blob, bd);
427 #endif  /* CONFIG_PCI */
428
429         return 0;
430 }
431 #endif  /* CONFIG_OF_BOARD_SETUP */