sdram.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 <ppc440.h>
  36. void hcu_led_set(u32 value);
  37. void dcbz_area(u32 start_address, u32 num_bytes);
  38. void dflush(void);
  39. #define DDR_DCR_BASE 0x10
  40. #define ddrcfga (DDR_DCR_BASE+0x0) /* DDR configuration address reg */
  41. #define ddrcfgd (DDR_DCR_BASE+0x1) /* DDR configuration data reg */
  42. #define DDR0_01_INT_MASK_MASK 0x000000FF
  43. #define DDR0_00_INT_ACK_ALL 0x7F000000
  44. #define DDR0_01_INT_MASK_ALL_ON 0x000000FF
  45. #define DDR0_01_INT_MASK_ALL_OFF 0x00000000
  46. #define DDR0_17_DLLLOCKREG_MASK 0x00010000 /* Read only */
  47. #define DDR0_17_DLLLOCKREG_UNLOCKED 0x00000000
  48. #define DDR0_17_DLLLOCKREG_LOCKED 0x00010000
  49. #define DDR0_22 0x16
  50. /* ECC */
  51. #define DDR0_22_CTRL_RAW_MASK 0x03000000
  52. #define DDR0_22_CTRL_RAW_ECC_DISABLE 0x00000000 /* ECC not enabled */
  53. #define DDR0_22_CTRL_RAW_ECC_CHECK_ONLY 0x01000000 /* ECC no correction */
  54. #define DDR0_22_CTRL_RAW_NO_ECC_RAM 0x02000000 /* Not a ECC RAM*/
  55. #define DDR0_22_CTRL_RAW_ECC_ENABLE 0x03000000 /* ECC correcting on */
  56. #define DDR0_03_CASLAT_DECODE(n) ((((unsigned long)(n))>>16)&0x7)
  57. #ifdef CFG_ENABLE_SDRAM_CACHE
  58. #define MY_TLB_WORD2_I_ENABLE 0 /* enable caching on DDR2 */
  59. #else
  60. #define MY_TLB_WORD2_I_ENABLE TLB_WORD2_I_ENABLE /* disable caching on DDR2 */
  61. #endif
  62. void program_tlb(u32 phys_addr, u32 virt_addr, u32 size, u32 tlb_word2_i_value);
  63. void board_add_ram_info(int use_default)
  64. {
  65. PPC4xx_SYS_INFO board_cfg;
  66. u32 val;
  67. mfsdram(DDR0_22, val);
  68. val &= DDR0_22_CTRL_RAW_MASK;
  69. switch (val) {
  70. case DDR0_22_CTRL_RAW_ECC_DISABLE:
  71. puts(" (ECC disabled");
  72. break;
  73. case DDR0_22_CTRL_RAW_ECC_CHECK_ONLY:
  74. puts(" (ECC check only");
  75. break;
  76. case DDR0_22_CTRL_RAW_NO_ECC_RAM:
  77. puts(" (no ECC ram");
  78. break;
  79. case DDR0_22_CTRL_RAW_ECC_ENABLE:
  80. puts(" (ECC enabled");
  81. break;
  82. }
  83. get_sys_info(&board_cfg);
  84. printf(", %d MHz", (board_cfg.freqPLB * 2) / 1000000);
  85. mfsdram(DDR0_03, val);
  86. val = DDR0_03_CASLAT_DECODE(val);
  87. printf(", CL%d)", val);
  88. }
  89. /*--------------------------------------------------------------------
  90. * wait_for_dlllock.
  91. *--------------------------------------------------------------------*/
  92. static int wait_for_dlllock(void)
  93. {
  94. unsigned long val;
  95. int wait = 0;
  96. /* -----------------------------------------------------------+
  97. * Wait for the DCC master delay line to finish calibration
  98. * ----------------------------------------------------------*/
  99. mtdcr(ddrcfga, DDR0_17);
  100. val = DDR0_17_DLLLOCKREG_UNLOCKED;
  101. while (wait != 0xffff) {
  102. val = mfdcr(ddrcfgd);
  103. if ((val & DDR0_17_DLLLOCKREG_MASK) ==
  104. DDR0_17_DLLLOCKREG_LOCKED)
  105. /* dlllockreg bit on */
  106. return 0;
  107. else
  108. wait++;
  109. }
  110. debug("0x%04x: DDR0_17 Value (dlllockreg bit): 0x%08x\n", wait, val);
  111. debug("Waiting for dlllockreg bit to raise\n");
  112. return -1;
  113. }
  114. /***********************************************************************
  115. *
  116. * sdram_panic -- Panic if we cannot configure the sdram correctly
  117. *
  118. ************************************************************************/
  119. void sdram_panic(const char *reason)
  120. {
  121. printf("\n%s: reason %s", __FUNCTION__, reason);
  122. hcu_led_set(0xff);
  123. while (1) {
  124. }
  125. /* Never return */
  126. }
  127. #ifdef CONFIG_DDR_ECC
  128. static void blank_string(int size)
  129. {
  130. int i;
  131. for (i=0; i<size; i++)
  132. putc('\b');
  133. for (i=0; i<size; i++)
  134. putc(' ');
  135. for (i=0; i<size; i++)
  136. putc('\b');
  137. }
  138. /*---------------------------------------------------------------------------+
  139. * program_ecc.
  140. *---------------------------------------------------------------------------*/
  141. static void program_ecc(unsigned long start_address, unsigned long num_bytes,
  142. unsigned long tlb_word2_i_value)
  143. {
  144. unsigned long current_address= start_address;
  145. int loopi = 0;
  146. u32 val;
  147. char str[] = "ECC generation -";
  148. char slash[] = "\\|/-\\|/-";
  149. sync();
  150. eieio();
  151. puts(str);
  152. if (tlb_word2_i_value == TLB_WORD2_I_ENABLE) {
  153. /* ECC bit set method for non-cached memory */
  154. /* This takes various seconds */
  155. for(current_address = 0; current_address < num_bytes;
  156. current_address += sizeof(u32)) {
  157. *(u32 *)current_address = 0;
  158. if ((current_address % (2 << 20)) == 0) {
  159. putc('\b');
  160. putc(slash[loopi++ % 8]);
  161. }
  162. }
  163. } else {
  164. /* ECC bit set method for cached memory */
  165. /* Fast method, no noticeable delay */
  166. dcbz_area(start_address, num_bytes);
  167. dflush();
  168. }
  169. blank_string(strlen(str));
  170. /* Clear error status */
  171. mfsdram(DDR0_00, val);
  172. mtsdram(DDR0_00, val | DDR0_00_INT_ACK_ALL);
  173. /*
  174. * Clear possible errors
  175. * If not done, then we could get an interrupt later on when
  176. * exceptions are enabled.
  177. */
  178. mtspr(mcsr, mfspr(mcsr));
  179. /* Set 'int_mask' parameter to functionnal value */
  180. mfsdram(DDR0_01, val);
  181. mtsdram(DDR0_01, ((val &~ DDR0_01_INT_MASK_MASK) |
  182. DDR0_01_INT_MASK_ALL_OFF));
  183. return;
  184. }
  185. #endif
  186. /***********************************************************************
  187. *
  188. * initdram -- 440EPx's DDR controller is a DENALI Core
  189. *
  190. ************************************************************************/
  191. long int initdram (int board_type)
  192. {
  193. #define HCU_HW_SDRAM_CONFIG_MASK 0x7
  194. #define INVALID_HW_CONFIG "Invalid HW-Config"
  195. u16 *hwVersReg = (u16 *) HCU_HW_VERSION_REGISTER;
  196. unsigned int dram_size = 0;
  197. mtsdram(DDR0_02, 0x00000000);
  198. /* Values must be kept in sync with Excel-table <<A0001492.>> ! */
  199. mtsdram(DDR0_00, 0x0000190A);
  200. mtsdram(DDR0_01, 0x01000000);
  201. mtsdram(DDR0_03, 0x02030602);
  202. mtsdram(DDR0_04, 0x0A020200);
  203. mtsdram(DDR0_05, 0x02020307);
  204. switch (*hwVersReg & HCU_HW_SDRAM_CONFIG_MASK) {
  205. case 0:
  206. dram_size = 128 * 1024 * 1024 ;
  207. mtsdram(DDR0_06, 0x0102C80D); /* 128MB RAM */
  208. mtsdram(DDR0_11, 0x000FC800); /* 128MB RAM */
  209. mtsdram(DDR0_43, 0x030A0300); /* 128MB RAM */
  210. break;
  211. case 1:
  212. dram_size = 256 * 1024 * 1024 ;
  213. mtsdram(DDR0_06, 0x0102C812); /* 256MB RAM */
  214. mtsdram(DDR0_11, 0x0014C800); /* 256MB RAM */
  215. mtsdram(DDR0_43, 0x030A0200); /* 256MB RAM */
  216. break;
  217. default:
  218. sdram_panic(INVALID_HW_CONFIG);
  219. break;
  220. }
  221. mtsdram(DDR0_07, 0x00090100);
  222. /*
  223. * TCPD=200 cycles of clock input is required to lock the DLL.
  224. * CKE must be HIGH the entire time.mtsdram(DDR0_08, 0x02C80001);
  225. */
  226. mtsdram(DDR0_08, 0x02C80001);
  227. mtsdram(DDR0_09, 0x00011D5F);
  228. mtsdram(DDR0_10, 0x00000100);
  229. mtsdram(DDR0_12, 0x00000003);
  230. mtsdram(DDR0_14, 0x00000000);
  231. mtsdram(DDR0_17, 0x1D000000);
  232. mtsdram(DDR0_18, 0x1D1D1D1D);
  233. mtsdram(DDR0_19, 0x1D1D1D1D);
  234. mtsdram(DDR0_20, 0x0B0B0B0B);
  235. mtsdram(DDR0_21, 0x0B0B0B0B);
  236. #define ECC_RAM 0x03267F0B
  237. #define NO_ECC_RAM 0x00267F0B
  238. #ifdef CONFIG_DDR_ECC
  239. mtsdram(DDR0_22, ECC_RAM);
  240. #else
  241. mtsdram(DDR0_22, NO_ECC_RAM);
  242. #endif
  243. mtsdram(DDR0_23, 0x00000000);
  244. mtsdram(DDR0_24, 0x01020001);
  245. mtsdram(DDR0_26, 0x2D930517);
  246. mtsdram(DDR0_27, 0x00008236);
  247. mtsdram(DDR0_28, 0x00000000);
  248. mtsdram(DDR0_31, 0x00000000);
  249. mtsdram(DDR0_42, 0x01000006);
  250. mtsdram(DDR0_44, 0x00000003);
  251. mtsdram(DDR0_02, 0x00000001);
  252. wait_for_dlllock();
  253. mtsdram(DDR0_00, 0x40000000); /* Zero init bit */
  254. /*
  255. * Program tlb entries for this size (dynamic)
  256. */
  257. remove_tlb(CFG_SDRAM_BASE, 256 << 20);
  258. program_tlb(0, 0, dram_size, MY_TLB_WORD2_I_ENABLE);
  259. /*
  260. * Setup 2nd TLB with same physical address but different virtual
  261. * address with cache enabled. This is done for fast ECC generation.
  262. */
  263. program_tlb(0, CFG_DDR_CACHED_ADDR, dram_size, 0);
  264. /* Diminish RAM to initialize */
  265. dram_size = dram_size - 32 ;
  266. #ifdef CONFIG_DDR_ECC
  267. /*
  268. * If ECC is enabled, initialize the parity bits.
  269. */
  270. program_ecc(CFG_DDR_CACHED_ADDR, dram_size, 0);
  271. #endif
  272. return (dram_size);
  273. }