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