cm5200.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * (C) Copyright 2003-2007
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2004
  6. * Mark Jonas, Freescale Semiconductor, mark.jonas@motorola.com.
  7. *
  8. * (C) Copyright 2004-2005
  9. * Martin Krause, TQ-Systems GmbH, martin.krause@tqs.de
  10. *
  11. * Adapted to U-Boot 1.2 by:
  12. * Bartlomiej Sieka <tur@semihalf.com>:
  13. * - HW ID readout from EEPROM
  14. * - module detection
  15. * Grzegorz Bernacki <gjb@semihalf.com>:
  16. * - run-time SDRAM controller configuration
  17. * - LIBFDT support
  18. *
  19. * SPDX-License-Identifier: GPL-2.0+
  20. */
  21. #include <common.h>
  22. #include <mpc5xxx.h>
  23. #include <pci.h>
  24. #include <asm/processor.h>
  25. #include <i2c.h>
  26. #include <linux/ctype.h>
  27. #ifdef CONFIG_OF_LIBFDT
  28. #include <libfdt.h>
  29. #include <fdt_support.h>
  30. #endif /* CONFIG_OF_LIBFDT */
  31. #include "cm5200.h"
  32. #include "fwupdate.h"
  33. DECLARE_GLOBAL_DATA_PTR;
  34. static hw_id_t hw_id;
  35. #ifndef CONFIG_SYS_RAMBOOT
  36. /*
  37. * Helper function to initialize SDRAM controller.
  38. */
  39. static void sdram_start(int hi_addr, mem_conf_t *mem_conf)
  40. {
  41. long hi_addr_bit = hi_addr ? 0x01000000 : 0;
  42. /* unlock mode register */
  43. *(vu_long *)MPC5XXX_SDRAM_CTRL = mem_conf->control | 0x80000000 |
  44. hi_addr_bit;
  45. /* precharge all banks */
  46. *(vu_long *)MPC5XXX_SDRAM_CTRL = mem_conf->control | 0x80000002 |
  47. hi_addr_bit;
  48. /* auto refresh */
  49. *(vu_long *)MPC5XXX_SDRAM_CTRL = mem_conf->control | 0x80000004 |
  50. hi_addr_bit;
  51. /* auto refresh, second time */
  52. *(vu_long *)MPC5XXX_SDRAM_CTRL = mem_conf->control | 0x80000004 |
  53. hi_addr_bit;
  54. /* set mode register */
  55. *(vu_long *)MPC5XXX_SDRAM_MODE = mem_conf->mode;
  56. /* normal operation */
  57. *(vu_long *)MPC5XXX_SDRAM_CTRL = mem_conf->control | hi_addr_bit;
  58. }
  59. #endif /* CONFIG_SYS_RAMBOOT */
  60. /*
  61. * Retrieve memory configuration for a given module. board_type is the index
  62. * in hw_id_list[] corresponding to the module we are executing on; we return
  63. * SDRAM controller settings approprate for this module.
  64. */
  65. static mem_conf_t* get_mem_config(int board_type)
  66. {
  67. switch(board_type){
  68. case CM1_QA:
  69. return memory_config[0];
  70. case CM11_QA:
  71. case CMU1_QA:
  72. return memory_config[1];
  73. default:
  74. printf("ERROR: Unknown module, using a default SDRAM "
  75. "configuration - things may not work!!!.\n");
  76. return memory_config[0];
  77. }
  78. }
  79. /*
  80. * Initalize SDRAM - configure SDRAM controller, detect memory size.
  81. */
  82. int dram_init(void)
  83. {
  84. ulong dramsize = 0;
  85. #ifndef CONFIG_SYS_RAMBOOT
  86. ulong test1, test2;
  87. mem_conf_t *mem_conf;
  88. mem_conf = get_mem_config(gd->board_type);
  89. /* configure SDRAM start/end for detection */
  90. *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0x0000001e; /* 2G at 0x0 */
  91. /* setup config registers */
  92. *(vu_long *)MPC5XXX_SDRAM_CONFIG1 = mem_conf->config1;
  93. *(vu_long *)MPC5XXX_SDRAM_CONFIG2 = mem_conf->config2;
  94. sdram_start(0, mem_conf);
  95. test1 = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE, 0x80000000);
  96. sdram_start(1, mem_conf);
  97. test2 = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE, 0x80000000);
  98. if (test1 > test2) {
  99. sdram_start(0, mem_conf);
  100. dramsize = test1;
  101. } else
  102. dramsize = test2;
  103. /* memory smaller than 1MB is impossible */
  104. if (dramsize < (1 << 20))
  105. dramsize = 0;
  106. /* set SDRAM CS0 size according to the amount of RAM found */
  107. if (dramsize > 0) {
  108. *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0x13 +
  109. __builtin_ffs(dramsize >> 20) - 1;
  110. } else
  111. *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0; /* disabled */
  112. #else /* CONFIG_SYS_RAMBOOT */
  113. /* retrieve size of memory connected to SDRAM CS0 */
  114. dramsize = *(vu_long *)MPC5XXX_SDRAM_CS0CFG & 0xFF;
  115. if (dramsize >= 0x13)
  116. dramsize = (1 << (dramsize - 0x13)) << 20;
  117. else
  118. dramsize = 0;
  119. #endif /* !CONFIG_SYS_RAMBOOT */
  120. /*
  121. * On MPC5200B we need to set the special configuration delay in the
  122. * DDR controller. Refer to chapter 8.7.5 SDelay--MBAR + 0x0190 of
  123. * the MPC5200B User's Manual.
  124. */
  125. *(vu_long *)MPC5XXX_SDRAM_SDELAY = 0x04;
  126. __asm__ volatile ("sync");
  127. gd->ram_size = dramsize;
  128. return 0;
  129. }
  130. /*
  131. * Read module hardware identification data from the I2C EEPROM.
  132. */
  133. static void read_hw_id(hw_id_t hw_id)
  134. {
  135. printf("ERROR: can't read HW ID from EEPROM\n");
  136. }
  137. /*
  138. * Identify module we are running on, set gd->board_type to the index in
  139. * hw_id_list[] corresponding to the module identifed, or to
  140. * CM5200_UNKNOWN_MODULE if we can't identify the module.
  141. */
  142. static void identify_module(hw_id_t hw_id)
  143. {
  144. int i, j, element;
  145. char match;
  146. gd->board_type = CM5200_UNKNOWN_MODULE;
  147. for (i = 0; i < sizeof (hw_id_list) / sizeof (char **); ++i) {
  148. match = 1;
  149. for (j = 0; j < sizeof (hw_id_identify) / sizeof (int); ++j) {
  150. element = hw_id_identify[j];
  151. if (strncmp(hw_id_list[i][element],
  152. &hw_id[element][0],
  153. hw_id_format[element].length) != 0) {
  154. match = 0;
  155. break;
  156. }
  157. }
  158. if (match) {
  159. gd->board_type = i;
  160. break;
  161. }
  162. }
  163. }
  164. /*
  165. * Compose string with module name.
  166. * buf is assumed to have enough space, and be null-terminated.
  167. */
  168. static void compose_module_name(hw_id_t hw_id, char *buf)
  169. {
  170. char tmp[MODULE_NAME_MAXLEN];
  171. strncat(buf, &hw_id[PCB_NAME][0], hw_id_format[PCB_NAME].length);
  172. strncat(buf, ".", 1);
  173. strncat(buf, &hw_id[FORM][0], hw_id_format[FORM].length);
  174. strncat(buf, &hw_id[VERSION][0], hw_id_format[VERSION].length);
  175. strncat(buf, " (", 2);
  176. strncat(buf, &hw_id[IDENTIFICATION_NUMBER][0],
  177. hw_id_format[IDENTIFICATION_NUMBER].length);
  178. sprintf(tmp, " / %u.%u)",
  179. hw_id[MAJOR_SW_VERSION][0],
  180. hw_id[MINOR_SW_VERSION][0]);
  181. strcat(buf, tmp);
  182. }
  183. #if defined(CONFIG_SYS_I2C_SOFT)
  184. /*
  185. * Compose string with hostname.
  186. * buf is assumed to have enough space, and be null-terminated.
  187. */
  188. static void compose_hostname(hw_id_t hw_id, char *buf)
  189. {
  190. char *p;
  191. strncat(buf, &hw_id[PCB_NAME][0], hw_id_format[PCB_NAME].length);
  192. strncat(buf, "_", 1);
  193. strncat(buf, &hw_id[FORM][0], hw_id_format[FORM].length);
  194. strncat(buf, &hw_id[VERSION][0], hw_id_format[VERSION].length);
  195. for (p = buf; *p; ++p)
  196. *p = tolower(*p);
  197. }
  198. #endif
  199. #ifdef CONFIG_OF_BOARD_SETUP
  200. /*
  201. * Update 'model' and 'memory' properties in the blob according to the module
  202. * that we are running on.
  203. */
  204. static void ft_blob_update(void *blob, bd_t *bd)
  205. {
  206. int len, ret, nodeoffset = 0;
  207. char module_name[MODULE_NAME_MAXLEN] = {0};
  208. compose_module_name(hw_id, module_name);
  209. len = strlen(module_name) + 1;
  210. ret = fdt_setprop(blob, nodeoffset, "model", module_name, len);
  211. if (ret < 0)
  212. printf("ft_blob_update(): cannot set /model property err:%s\n",
  213. fdt_strerror(ret));
  214. }
  215. #endif /* CONFIG_OF_BOARD_SETUP */
  216. /*
  217. * Read HW ID from I2C EEPROM and detect the modue we are running on. Note
  218. * that we need to use local variable for readout, because global data is not
  219. * writable yet (and we'll have to redo the readout later on).
  220. */
  221. int checkboard(void)
  222. {
  223. hw_id_t hw_id_tmp;
  224. char module_name_tmp[MODULE_NAME_MAXLEN] = "";
  225. read_hw_id(hw_id_tmp);
  226. identify_module(hw_id_tmp); /* this sets gd->board_type */
  227. compose_module_name(hw_id_tmp, module_name_tmp);
  228. if (gd->board_type != CM5200_UNKNOWN_MODULE)
  229. printf("Board: %s\n", module_name_tmp);
  230. else
  231. printf("Board: unrecognized cm5200 module (%s)\n",
  232. module_name_tmp);
  233. return 0;
  234. }
  235. int board_early_init_r(void)
  236. {
  237. /*
  238. * Now, when we are in RAM, enable flash write access for detection
  239. * process. Note that CS_BOOT cannot be cleared when executing in
  240. * flash.
  241. */
  242. *(vu_long *)MPC5XXX_BOOTCS_CFG &= ~0x1; /* clear RO */
  243. /* Now that we can write to global data, read HW ID again. */
  244. read_hw_id(hw_id);
  245. return 0;
  246. }
  247. #ifdef CONFIG_MISC_INIT_R
  248. int misc_init_r(void)
  249. {
  250. #if defined(CONFIG_SYS_I2C_SOFT)
  251. uchar buf[6];
  252. char str[18];
  253. char hostname[MODULE_NAME_MAXLEN];
  254. /* Read ethaddr from EEPROM */
  255. if (i2c_read(CONFIG_SYS_I2C_EEPROM, CONFIG_MAC_OFFSET, 2, buf, 6) == 0) {
  256. sprintf(str, "%02X:%02X:%02X:%02X:%02X:%02X",
  257. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
  258. /* Check if MAC addr is owned by Schindler */
  259. if (strstr(str, "00:06:C3") != str)
  260. printf(LOG_PREFIX "Warning - Illegal MAC address (%s)"
  261. " in EEPROM.\n", str);
  262. else {
  263. printf(LOG_PREFIX "Using MAC (%s) from I2C EEPROM\n",
  264. str);
  265. setenv("ethaddr", str);
  266. }
  267. } else {
  268. printf(LOG_PREFIX "Warning - Unable to read MAC from I2C"
  269. " device at address %02X:%04X\n", CONFIG_SYS_I2C_EEPROM,
  270. CONFIG_MAC_OFFSET);
  271. }
  272. hostname[0] = 0x00;
  273. /* set the hostname appropriate to the module we're running on */
  274. compose_hostname(hw_id, hostname);
  275. setenv("hostname", hostname);
  276. #endif /* defined(CONFIG_SYS_I2C_SOFT) */
  277. if (!getenv("ethaddr"))
  278. printf(LOG_PREFIX "MAC address not set, networking is not "
  279. "operational\n");
  280. return 0;
  281. }
  282. #endif /* CONFIG_MISC_INIT_R */
  283. #ifdef CONFIG_LAST_STAGE_INIT
  284. int last_stage_init(void)
  285. {
  286. #ifdef CONFIG_USB_STORAGE
  287. cm5200_fwupdate();
  288. #endif /* CONFIG_USB_STORAGE */
  289. return 0;
  290. }
  291. #endif /* CONFIG_LAST_STAGE_INIT */
  292. #ifdef CONFIG_OF_BOARD_SETUP
  293. int ft_board_setup(void *blob, bd_t *bd)
  294. {
  295. ft_cpu_setup(blob, bd);
  296. ft_blob_update(blob, bd);
  297. return 0;
  298. }
  299. #endif /* CONFIG_OF_BOARD_SETUP */