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