sdram.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * (C) Copyright 2006
  3. * Sylvie Gohl, AMCC/IBM, gohl.sylvie@fr.ibm.com
  4. * Jacqueline Pira-Ferriol, AMCC/IBM, jpira-ferriol@fr.ibm.com
  5. * Thierry Roman, AMCC/IBM, thierry_roman@fr.ibm.com
  6. * Alain Saurel, AMCC/IBM, alain.saurel@fr.ibm.com
  7. * Robert Snyder, AMCC/IBM, rob.snyder@fr.ibm.com
  8. *
  9. * (C) Copyright 2007-2013
  10. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  11. *
  12. * SPDX-License-Identifier: GPL-2.0+
  13. */
  14. /* define DEBUG for debugging output (obviously ;-)) */
  15. #if 0
  16. #define DEBUG
  17. #endif
  18. #include <common.h>
  19. #include <asm/processor.h>
  20. #include <asm/mmu.h>
  21. #include <asm/io.h>
  22. #include <asm/cache.h>
  23. #include <asm/ppc440.h>
  24. #include <watchdog.h>
  25. /*
  26. * This DDR2 setup code can dynamically setup the TLB entries for the DDR2 memory
  27. * region. Right now the cache should still be disabled in U-Boot because of the
  28. * EMAC driver, that need it's buffer descriptor to be located in non cached
  29. * memory.
  30. *
  31. * If at some time this restriction doesn't apply anymore, just define
  32. * CONFIG_4xx_DCACHE in the board config file and this code should setup
  33. * everything correctly.
  34. */
  35. #ifdef CONFIG_4xx_DCACHE
  36. #define MY_TLB_WORD2_I_ENABLE 0 /* enable caching on SDRAM */
  37. #else
  38. #define MY_TLB_WORD2_I_ENABLE TLB_WORD2_I_ENABLE /* disable caching on SDRAM */
  39. #endif
  40. /*-----------------------------------------------------------------------------+
  41. * Prototypes
  42. *-----------------------------------------------------------------------------*/
  43. extern int denali_wait_for_dlllock(void);
  44. extern void denali_core_search_data_eye(void);
  45. extern void dcbz_area(u32 start_address, u32 num_bytes);
  46. static u32 is_ecc_enabled(void)
  47. {
  48. u32 val;
  49. mfsdram(DDR0_22, val);
  50. val &= DDR0_22_CTRL_RAW_MASK;
  51. if (val)
  52. return 1;
  53. else
  54. return 0;
  55. }
  56. void board_add_ram_info(int use_default)
  57. {
  58. PPC4xx_SYS_INFO board_cfg;
  59. u32 val;
  60. if (is_ecc_enabled())
  61. puts(" (ECC");
  62. else
  63. puts(" (ECC not");
  64. get_sys_info(&board_cfg);
  65. printf(" enabled, %ld MHz", (board_cfg.freqPLB * 2) / 1000000);
  66. mfsdram(DDR0_03, val);
  67. val = DDR0_03_CASLAT_DECODE(val);
  68. printf(", CL%d)", val);
  69. }
  70. #ifdef CONFIG_DDR_ECC
  71. static void wait_ddr_idle(void)
  72. {
  73. /*
  74. * Controller idle status cannot be determined for Denali
  75. * DDR2 code. Just return here.
  76. */
  77. }
  78. static void program_ecc(u32 start_address,
  79. u32 num_bytes,
  80. u32 tlb_word2_i_value)
  81. {
  82. u32 val;
  83. u32 current_addr = start_address;
  84. u32 size;
  85. int bytes_remaining;
  86. sync();
  87. wait_ddr_idle();
  88. /*
  89. * Because of 440EPx errata CHIP 11, we don't touch the last 256
  90. * bytes of SDRAM.
  91. */
  92. bytes_remaining = num_bytes - CONFIG_SYS_MEM_TOP_HIDE;
  93. /*
  94. * We have to write the ECC bytes by zeroing and flushing in smaller
  95. * steps, since the whole 256MByte takes too long for the external
  96. * watchdog.
  97. */
  98. while (bytes_remaining > 0) {
  99. size = min((64 << 20), bytes_remaining);
  100. /* Write zero's to SDRAM */
  101. dcbz_area(current_addr, size);
  102. /* Write modified dcache lines back to memory */
  103. clean_dcache_range(current_addr, current_addr + size);
  104. current_addr += 64 << 20;
  105. bytes_remaining -= 64 << 20;
  106. WATCHDOG_RESET();
  107. }
  108. sync();
  109. wait_ddr_idle();
  110. /* Clear error status */
  111. mfsdram(DDR0_00, val);
  112. mtsdram(DDR0_00, val | DDR0_00_INT_ACK_ALL);
  113. /* Set 'int_mask' parameter to functionnal value */
  114. mfsdram(DDR0_01, val);
  115. mtsdram(DDR0_01, ((val &~ DDR0_01_INT_MASK_MASK) | DDR0_01_INT_MASK_ALL_OFF));
  116. sync();
  117. wait_ddr_idle();
  118. }
  119. #endif
  120. /*************************************************************************
  121. *
  122. * initdram -- 440EPx's DDR controller is a DENALI Core
  123. *
  124. ************************************************************************/
  125. phys_size_t initdram (int board_type)
  126. {
  127. /* CL=4 */
  128. mtsdram(DDR0_02, 0x00000000);
  129. mtsdram(DDR0_00, 0x0000190A);
  130. mtsdram(DDR0_01, 0x01000000);
  131. mtsdram(DDR0_03, 0x02040803); /* A suitable burst length was taken. CAS is right for our board */
  132. mtsdram(DDR0_04, 0x0B030300);
  133. mtsdram(DDR0_05, 0x02020308);
  134. mtsdram(DDR0_06, 0x0003C812);
  135. mtsdram(DDR0_07, 0x00090100);
  136. mtsdram(DDR0_08, 0x03c80001);
  137. mtsdram(DDR0_09, 0x00011D5F);
  138. mtsdram(DDR0_10, 0x00000100);
  139. mtsdram(DDR0_11, 0x000CC800);
  140. mtsdram(DDR0_12, 0x00000003);
  141. mtsdram(DDR0_14, 0x00000000);
  142. mtsdram(DDR0_17, 0x1e000000);
  143. mtsdram(DDR0_18, 0x1e1e1e1e);
  144. mtsdram(DDR0_19, 0x1e1e1e1e);
  145. mtsdram(DDR0_20, 0x0B0B0B0B);
  146. mtsdram(DDR0_21, 0x0B0B0B0B);
  147. #ifdef CONFIG_DDR_ECC
  148. mtsdram(DDR0_22, 0x00267F0B | DDR0_22_CTRL_RAW_ECC_ENABLE); /* enable ECC */
  149. #else
  150. mtsdram(DDR0_22, 0x00267F0B);
  151. #endif
  152. mtsdram(DDR0_23, 0x01000000);
  153. mtsdram(DDR0_24, 0x01010001);
  154. mtsdram(DDR0_26, 0x2D93028A);
  155. mtsdram(DDR0_27, 0x0784682B);
  156. mtsdram(DDR0_28, 0x00000080);
  157. mtsdram(DDR0_31, 0x00000000);
  158. mtsdram(DDR0_42, 0x01000008);
  159. mtsdram(DDR0_43, 0x050A0200);
  160. mtsdram(DDR0_44, 0x00000005);
  161. mtsdram(DDR0_02, 0x00000001); /* Activate the denali core */
  162. denali_wait_for_dlllock();
  163. #if defined(CONFIG_DDR_DATA_EYE)
  164. /* -----------------------------------------------------------+
  165. * Perform data eye search if requested.
  166. * ----------------------------------------------------------*/
  167. program_tlb(0, CONFIG_SYS_SDRAM_BASE, CONFIG_SYS_MBYTES_SDRAM << 20,
  168. TLB_WORD2_I_ENABLE);
  169. denali_core_search_data_eye();
  170. remove_tlb(CONFIG_SYS_SDRAM_BASE, CONFIG_SYS_MBYTES_SDRAM << 20);
  171. #endif
  172. /*
  173. * Program tlb entries for this size (dynamic)
  174. */
  175. program_tlb(0, CONFIG_SYS_SDRAM_BASE, CONFIG_SYS_MBYTES_SDRAM << 20,
  176. MY_TLB_WORD2_I_ENABLE);
  177. #if defined(CONFIG_DDR_ECC)
  178. #if defined(CONFIG_4xx_DCACHE)
  179. /*
  180. * If ECC is enabled, initialize the parity bits.
  181. */
  182. program_ecc(0, CONFIG_SYS_MBYTES_SDRAM << 20, 0);
  183. #else /* CONFIG_4xx_DCACHE */
  184. /*
  185. * Setup 2nd TLB with same physical address but different virtual address
  186. * with cache enabled. This is done for fast ECC generation.
  187. */
  188. program_tlb(0, CONFIG_SYS_DDR_CACHED_ADDR, CONFIG_SYS_MBYTES_SDRAM << 20, 0);
  189. /*
  190. * If ECC is enabled, initialize the parity bits.
  191. */
  192. program_ecc(CONFIG_SYS_DDR_CACHED_ADDR, CONFIG_SYS_MBYTES_SDRAM << 20, 0);
  193. /*
  194. * Now after initialization (auto-calibration and ECC generation)
  195. * remove the TLB entries with caches enabled and program again with
  196. * desired cache functionality
  197. */
  198. remove_tlb(CONFIG_SYS_DDR_CACHED_ADDR, CONFIG_SYS_MBYTES_SDRAM << 20);
  199. #endif /* CONFIG_4xx_DCACHE */
  200. #endif /* CONFIG_DDR_ECC */
  201. /*
  202. * Clear possible errors resulting from data-eye-search.
  203. * If not done, then we could get an interrupt later on when
  204. * exceptions are enabled.
  205. */
  206. set_mcsr(get_mcsr());
  207. return (CONFIG_SYS_MBYTES_SDRAM << 20);
  208. }