sdram.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * (C) Copyright 2007
  3. * Niklaus Giger (Niklaus.Giger@netstal.com)
  4. * (C) Copyright 2006
  5. * Sylvie Gohl, AMCC/IBM, gohl.sylvie@fr.ibm.com
  6. * Jacqueline Pira-Ferriol, AMCC/IBM, jpira-ferriol@fr.ibm.com
  7. * Thierry Roman, AMCC/IBM, thierry_roman@fr.ibm.com
  8. * Alain Saurel, AMCC/IBM, alain.saurel@fr.ibm.com
  9. * Robert Snyder, AMCC/IBM, rob.snyder@fr.ibm.com
  10. *
  11. * (C) Copyright 2006
  12. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. /* define DEBUG for debug output */
  30. #undef DEBUG
  31. #include <common.h>
  32. #include <asm/processor.h>
  33. #include <asm/io.h>
  34. #include <asm/mmu.h>
  35. #include <asm/cache.h>
  36. #include <ppc440.h>
  37. void hcu_led_set(u32 value);
  38. void dcbz_area(u32 start_address, u32 num_bytes);
  39. #define ECC_RAM 0x03267F0B
  40. #define NO_ECC_RAM 0x00267F0B
  41. #define HCU_HW_SDRAM_CONFIG_MASK 0x7
  42. #define MY_TLB_WORD2_I_ENABLE TLB_WORD2_I_ENABLE
  43. /* disable caching on DDR2 */
  44. void board_add_ram_info(int use_default)
  45. {
  46. PPC4xx_SYS_INFO board_cfg;
  47. u32 val;
  48. mfsdram(DDR0_22, val);
  49. val &= DDR0_22_CTRL_RAW_MASK;
  50. switch (val) {
  51. case DDR0_22_CTRL_RAW_ECC_DISABLE:
  52. puts(" (ECC disabled");
  53. break;
  54. case DDR0_22_CTRL_RAW_ECC_CHECK_ONLY:
  55. puts(" (ECC check only");
  56. break;
  57. case DDR0_22_CTRL_RAW_NO_ECC_RAM:
  58. puts(" (no ECC ram");
  59. break;
  60. case DDR0_22_CTRL_RAW_ECC_ENABLE:
  61. puts(" (ECC enabled");
  62. break;
  63. }
  64. get_sys_info(&board_cfg);
  65. printf(", %d 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. /*--------------------------------------------------------------------
  71. * wait_for_dlllock.
  72. *--------------------------------------------------------------------*/
  73. static int wait_for_dlllock(void)
  74. {
  75. unsigned long val;
  76. int wait = 0;
  77. /* -----------------------------------------------------------+
  78. * Wait for the DCC master delay line to finish calibration
  79. * ----------------------------------------------------------*/
  80. mtdcr(memcfga, DDR0_17);
  81. val = DDR0_17_DLLLOCKREG_UNLOCKED;
  82. while (wait != 0xffff) {
  83. val = mfdcr(memcfgd);
  84. if ((val & DDR0_17_DLLLOCKREG_MASK) ==
  85. DDR0_17_DLLLOCKREG_LOCKED)
  86. /* dlllockreg bit on */
  87. return 0;
  88. else
  89. wait++;
  90. }
  91. debug("0x%04x: DDR0_17 Value (dlllockreg bit): 0x%08x\n", wait, val);
  92. debug("Waiting for dlllockreg bit to raise\n");
  93. return -1;
  94. }
  95. /***********************************************************************
  96. *
  97. * sdram_panic -- Panic if we cannot configure the sdram correctly
  98. *
  99. ************************************************************************/
  100. void sdram_panic(const char *reason)
  101. {
  102. printf("\n%s: reason %s", __FUNCTION__, reason);
  103. hcu_led_set(0xff);
  104. while (1) {
  105. }
  106. /* Never return */
  107. }
  108. #ifdef CONFIG_DDR_ECC
  109. static void blank_string(int size)
  110. {
  111. int i;
  112. for (i=0; i<size; i++)
  113. putc('\b');
  114. for (i=0; i<size; i++)
  115. putc(' ');
  116. for (i=0; i<size; i++)
  117. putc('\b');
  118. }
  119. /*---------------------------------------------------------------------------+
  120. * program_ecc.
  121. *---------------------------------------------------------------------------*/
  122. static void program_ecc(unsigned long start_address, unsigned long num_bytes)
  123. {
  124. u32 val;
  125. char str[] = "ECC generation -";
  126. #if defined(CONFIG_PRAM)
  127. u32 *magicPtr;
  128. u32 magic;
  129. if ((mfspr(dbcr0) & 0x80000000) == 0) {
  130. /* only if no external debugger is alive!
  131. * Check whether vxWorks is using EDR logging, if yes zero
  132. * also PostMortem and user reserved memory
  133. */
  134. magicPtr = (u32 *)(start_address + num_bytes -
  135. (CONFIG_PRAM*1024) + sizeof(u32));
  136. magic = in_be32(magicPtr);
  137. debug("%s: CONFIG_PRAM %d kB magic 0x%x 0x%p\n",
  138. __FUNCTION__, CONFIG_PRAM,
  139. magicPtr, magic);
  140. if (magic == 0xbeefbabe) {
  141. printf("%s: preserving at %p\n", __FUNCTION__, magicPtr);
  142. num_bytes -= (CONFIG_PRAM*1024) - PM_RESERVED_MEM;
  143. }
  144. }
  145. #endif
  146. sync();
  147. puts(str);
  148. /* ECC bit set method for cached memory */
  149. /* Fast method, no noticeable delay */
  150. dcbz_area(start_address, num_bytes);
  151. /* Write modified dcache lines back to memory */
  152. clean_dcache_range(start_address, start_address + num_bytes);
  153. blank_string(strlen(str));
  154. /* Clear error status */
  155. mfsdram(DDR0_00, val);
  156. mtsdram(DDR0_00, val | DDR0_00_INT_ACK_ALL);
  157. /*
  158. * Clear possible ECC errors
  159. * If not done, then we could get an interrupt later on when
  160. * exceptions are enabled.
  161. */
  162. mtspr(mcsr, mfspr(mcsr));
  163. /* Set 'int_mask' parameter to functionnal value */
  164. mfsdram(DDR0_01, val);
  165. mtsdram(DDR0_01, ((val &~ DDR0_01_INT_MASK_MASK) |
  166. DDR0_01_INT_MASK_ALL_OFF));
  167. return;
  168. }
  169. #endif
  170. /***********************************************************************
  171. *
  172. * initdram -- 440EPx's DDR controller is a DENALI Core
  173. *
  174. ************************************************************************/
  175. long int initdram (int board_type)
  176. {
  177. unsigned int dram_size = 0;
  178. mtsdram(DDR0_02, 0x00000000);
  179. /* Values must be kept in sync with Excel-table <<A0001492.>> ! */
  180. mtsdram(DDR0_00, 0x0000190A);
  181. mtsdram(DDR0_01, 0x01000000);
  182. mtsdram(DDR0_03, 0x02030602);
  183. mtsdram(DDR0_04, 0x0A020200);
  184. mtsdram(DDR0_05, 0x02020307);
  185. switch (in_be16((u16 *)HCU_HW_VERSION_REGISTER) & HCU_HW_SDRAM_CONFIG_MASK) {
  186. case 1:
  187. dram_size = 256 * 1024 * 1024 ;
  188. mtsdram(DDR0_06, 0x0102C812); /* 256MB RAM */
  189. mtsdram(DDR0_11, 0x0014C800); /* 256MB RAM */
  190. mtsdram(DDR0_43, 0x030A0200); /* 256MB RAM */
  191. break;
  192. case 0:
  193. default:
  194. dram_size = 128 * 1024 * 1024 ;
  195. mtsdram(DDR0_06, 0x0102C80D); /* 128MB RAM */
  196. mtsdram(DDR0_11, 0x000FC800); /* 128MB RAM */
  197. mtsdram(DDR0_43, 0x030A0300); /* 128MB RAM */
  198. break;
  199. }
  200. mtsdram(DDR0_07, 0x00090100);
  201. /*
  202. * TCPD=200 cycles of clock input is required to lock the DLL.
  203. * CKE must be HIGH the entire time.mtsdram(DDR0_08, 0x02C80001);
  204. */
  205. mtsdram(DDR0_08, 0x02C80001);
  206. mtsdram(DDR0_09, 0x00011D5F);
  207. mtsdram(DDR0_10, 0x00000100);
  208. mtsdram(DDR0_12, 0x00000003);
  209. mtsdram(DDR0_14, 0x00000000);
  210. mtsdram(DDR0_17, 0x1D000000);
  211. mtsdram(DDR0_18, 0x1D1D1D1D);
  212. mtsdram(DDR0_19, 0x1D1D1D1D);
  213. mtsdram(DDR0_20, 0x0B0B0B0B);
  214. mtsdram(DDR0_21, 0x0B0B0B0B);
  215. #ifdef CONFIG_DDR_ECC
  216. mtsdram(DDR0_22, ECC_RAM);
  217. #else
  218. mtsdram(DDR0_22, NO_ECC_RAM);
  219. #endif
  220. mtsdram(DDR0_23, 0x00000000);
  221. mtsdram(DDR0_24, 0x01020001);
  222. mtsdram(DDR0_26, 0x2D930517);
  223. mtsdram(DDR0_27, 0x00008236);
  224. mtsdram(DDR0_28, 0x00000000);
  225. mtsdram(DDR0_31, 0x00000000);
  226. mtsdram(DDR0_42, 0x01000006);
  227. mtsdram(DDR0_44, 0x00000003);
  228. mtsdram(DDR0_02, 0x00000001);
  229. wait_for_dlllock();
  230. mtsdram(DDR0_00, 0x40000000); /* Zero init bit */
  231. /*
  232. * Program tlb entries for this size (dynamic)
  233. */
  234. remove_tlb(CFG_SDRAM_BASE, 256 << 20);
  235. program_tlb(0, 0, dram_size, TLB_WORD2_W_ENABLE | TLB_WORD2_I_ENABLE);
  236. /*
  237. * Setup 2nd TLB with same physical address but different virtual
  238. * address with cache enabled. This is done for fast ECC generation.
  239. */
  240. program_tlb(0, CFG_DDR_CACHED_ADDR, dram_size, 0);
  241. #ifdef CONFIG_DDR_ECC
  242. /*
  243. * If ECC is enabled, initialize the parity bits.
  244. */
  245. program_ecc(CFG_DDR_CACHED_ADDR, dram_size);
  246. #endif
  247. return (dram_size);
  248. }