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