2 * (C) Copyright 2003-2007
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Mark Jonas, Freescale Semiconductor, mark.jonas@motorola.com.
8 * (C) Copyright 2004-2005
9 * Martin Krause, TQ-Systems GmbH, martin.krause@tqs.de
11 * Adapted to U-Boot 1.2 by:
12 * Bartlomiej Sieka <tur@semihalf.com>:
13 * - HW ID readout from EEPROM
15 * Grzegorz Bernacki <gjb@semihalf.com>:
16 * - run-time SDRAM controller configuration
19 * SPDX-License-Identifier: GPL-2.0+
25 #include <asm/processor.h>
27 #include <linux/ctype.h>
29 #ifdef CONFIG_OF_LIBFDT
31 #include <fdt_support.h>
32 #endif /* CONFIG_OF_LIBFDT */
38 DECLARE_GLOBAL_DATA_PTR;
43 #ifndef CONFIG_SYS_RAMBOOT
45 * Helper function to initialize SDRAM controller.
47 static void sdram_start(int hi_addr, mem_conf_t *mem_conf)
49 long hi_addr_bit = hi_addr ? 0x01000000 : 0;
51 /* unlock mode register */
52 *(vu_long *)MPC5XXX_SDRAM_CTRL = mem_conf->control | 0x80000000 |
55 /* precharge all banks */
56 *(vu_long *)MPC5XXX_SDRAM_CTRL = mem_conf->control | 0x80000002 |
60 *(vu_long *)MPC5XXX_SDRAM_CTRL = mem_conf->control | 0x80000004 |
63 /* auto refresh, second time */
64 *(vu_long *)MPC5XXX_SDRAM_CTRL = mem_conf->control | 0x80000004 |
67 /* set mode register */
68 *(vu_long *)MPC5XXX_SDRAM_MODE = mem_conf->mode;
70 /* normal operation */
71 *(vu_long *)MPC5XXX_SDRAM_CTRL = mem_conf->control | hi_addr_bit;
73 #endif /* CONFIG_SYS_RAMBOOT */
77 * Retrieve memory configuration for a given module. board_type is the index
78 * in hw_id_list[] corresponding to the module we are executing on; we return
79 * SDRAM controller settings approprate for this module.
81 static mem_conf_t* get_mem_config(int board_type)
85 return memory_config[0];
88 return memory_config[1];
90 printf("ERROR: Unknown module, using a default SDRAM "
91 "configuration - things may not work!!!.\n");
92 return memory_config[0];
98 * Initalize SDRAM - configure SDRAM controller, detect memory size.
103 #ifndef CONFIG_SYS_RAMBOOT
105 mem_conf_t *mem_conf;
107 mem_conf = get_mem_config(gd->board_type);
109 /* configure SDRAM start/end for detection */
110 *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0x0000001e; /* 2G at 0x0 */
112 /* setup config registers */
113 *(vu_long *)MPC5XXX_SDRAM_CONFIG1 = mem_conf->config1;
114 *(vu_long *)MPC5XXX_SDRAM_CONFIG2 = mem_conf->config2;
116 sdram_start(0, mem_conf);
117 test1 = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE, 0x80000000);
118 sdram_start(1, mem_conf);
119 test2 = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE, 0x80000000);
121 sdram_start(0, mem_conf);
126 /* memory smaller than 1MB is impossible */
127 if (dramsize < (1 << 20))
130 /* set SDRAM CS0 size according to the amount of RAM found */
132 *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0x13 +
133 __builtin_ffs(dramsize >> 20) - 1;
135 *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0; /* disabled */
136 #else /* CONFIG_SYS_RAMBOOT */
137 /* retrieve size of memory connected to SDRAM CS0 */
138 dramsize = *(vu_long *)MPC5XXX_SDRAM_CS0CFG & 0xFF;
139 if (dramsize >= 0x13)
140 dramsize = (1 << (dramsize - 0x13)) << 20;
143 #endif /* !CONFIG_SYS_RAMBOOT */
146 * On MPC5200B we need to set the special configuration delay in the
147 * DDR controller. Refer to chapter 8.7.5 SDelay--MBAR + 0x0190 of
148 * the MPC5200B User's Manual.
150 *(vu_long *)MPC5XXX_SDRAM_SDELAY = 0x04;
151 __asm__ volatile ("sync");
153 gd->ram_size = dramsize;
160 * Read module hardware identification data from the I2C EEPROM.
162 static void read_hw_id(hw_id_t hw_id)
165 for (i = 0; i < HW_ID_ELEM_COUNT; ++i)
166 if (i2c_read(CONFIG_SYS_I2C_EEPROM,
167 hw_id_format[i].offset,
169 (uchar *)&hw_id[i][0],
170 hw_id_format[i].length) != 0)
171 printf("ERROR: can't read HW ID from EEPROM\n");
176 * Identify module we are running on, set gd->board_type to the index in
177 * hw_id_list[] corresponding to the module identifed, or to
178 * CM5200_UNKNOWN_MODULE if we can't identify the module.
180 static void identify_module(hw_id_t hw_id)
184 gd->board_type = CM5200_UNKNOWN_MODULE;
185 for (i = 0; i < sizeof (hw_id_list) / sizeof (char **); ++i) {
187 for (j = 0; j < sizeof (hw_id_identify) / sizeof (int); ++j) {
188 element = hw_id_identify[j];
189 if (strncmp(hw_id_list[i][element],
191 hw_id_format[element].length) != 0) {
205 * Compose string with module name.
206 * buf is assumed to have enough space, and be null-terminated.
208 static void compose_module_name(hw_id_t hw_id, char *buf)
210 char tmp[MODULE_NAME_MAXLEN];
211 strncat(buf, &hw_id[PCB_NAME][0], hw_id_format[PCB_NAME].length);
212 strncat(buf, ".", 1);
213 strncat(buf, &hw_id[FORM][0], hw_id_format[FORM].length);
214 strncat(buf, &hw_id[VERSION][0], hw_id_format[VERSION].length);
215 strncat(buf, " (", 2);
216 strncat(buf, &hw_id[IDENTIFICATION_NUMBER][0],
217 hw_id_format[IDENTIFICATION_NUMBER].length);
218 sprintf(tmp, " / %u.%u)",
219 hw_id[MAJOR_SW_VERSION][0],
220 hw_id[MINOR_SW_VERSION][0]);
226 * Compose string with hostname.
227 * buf is assumed to have enough space, and be null-terminated.
229 static void compose_hostname(hw_id_t hw_id, char *buf)
232 strncat(buf, &hw_id[PCB_NAME][0], hw_id_format[PCB_NAME].length);
233 strncat(buf, "_", 1);
234 strncat(buf, &hw_id[FORM][0], hw_id_format[FORM].length);
235 strncat(buf, &hw_id[VERSION][0], hw_id_format[VERSION].length);
236 for (p = buf; *p; ++p)
242 #ifdef CONFIG_OF_BOARD_SETUP
244 * Update 'model' and 'memory' properties in the blob according to the module
245 * that we are running on.
247 static void ft_blob_update(void *blob, bd_t *bd)
249 int len, ret, nodeoffset = 0;
250 char module_name[MODULE_NAME_MAXLEN] = {0};
252 compose_module_name(hw_id, module_name);
253 len = strlen(module_name) + 1;
255 ret = fdt_setprop(blob, nodeoffset, "model", module_name, len);
257 printf("ft_blob_update(): cannot set /model property err:%s\n",
260 #endif /* CONFIG_OF_BOARD_SETUP */
264 * Read HW ID from I2C EEPROM and detect the modue we are running on. Note
265 * that we need to use local variable for readout, because global data is not
266 * writable yet (and we'll have to redo the readout later on).
271 char module_name_tmp[MODULE_NAME_MAXLEN] = "";
274 * We need I2C to access HW ID data from EEPROM, so we call i2c_init()
275 * here despite the fact that it will be called again later on. We
276 * also use a little trick to silence I2C-related output.
278 gd->flags |= GD_FLG_SILENT;
279 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
280 gd->flags &= ~GD_FLG_SILENT;
282 read_hw_id(hw_id_tmp);
283 identify_module(hw_id_tmp); /* this sets gd->board_type */
284 compose_module_name(hw_id_tmp, module_name_tmp);
286 if (gd->board_type != CM5200_UNKNOWN_MODULE)
287 printf("Board: %s\n", module_name_tmp);
289 printf("Board: unrecognized cm5200 module (%s)\n",
296 int board_early_init_r(void)
299 * Now, when we are in RAM, enable flash write access for detection
300 * process. Note that CS_BOOT cannot be cleared when executing in
303 *(vu_long *)MPC5XXX_BOOTCS_CFG &= ~0x1; /* clear RO */
305 /* Now that we can write to global data, read HW ID again. */
311 #ifdef CONFIG_MISC_INIT_R
312 int misc_init_r(void)
314 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C_SOFT)
317 char hostname[MODULE_NAME_MAXLEN];
319 /* Read ethaddr from EEPROM */
320 if (i2c_read(CONFIG_SYS_I2C_EEPROM, CONFIG_MAC_OFFSET, 2, buf, 6) == 0) {
321 sprintf(str, "%02X:%02X:%02X:%02X:%02X:%02X",
322 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
323 /* Check if MAC addr is owned by Schindler */
324 if (strstr(str, "00:06:C3") != str)
325 printf(LOG_PREFIX "Warning - Illegal MAC address (%s)"
326 " in EEPROM.\n", str);
328 printf(LOG_PREFIX "Using MAC (%s) from I2C EEPROM\n",
330 setenv("ethaddr", str);
333 printf(LOG_PREFIX "Warning - Unable to read MAC from I2C"
334 " device at address %02X:%04X\n", CONFIG_SYS_I2C_EEPROM,
337 #endif /* defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C_SOFT) */
338 if (!getenv("ethaddr"))
339 printf(LOG_PREFIX "MAC address not set, networking is not "
342 /* set the hostname appropriate to the module we're running on */
344 compose_hostname(hw_id, hostname);
345 setenv("hostname", hostname);
349 #endif /* CONFIG_MISC_INIT_R */
352 #ifdef CONFIG_LAST_STAGE_INIT
353 int last_stage_init(void)
355 #ifdef CONFIG_USB_STORAGE
357 #endif /* CONFIG_USB_STORAGE */
360 #endif /* CONFIG_LAST_STAGE_INIT */
363 #ifdef CONFIG_OF_BOARD_SETUP
364 int ft_board_setup(void *blob, bd_t *bd)
366 ft_cpu_setup(blob, bd);
367 ft_blob_update(blob, bd);
371 #endif /* CONFIG_OF_BOARD_SETUP */