sdram.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * (C) Copyright 2005
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/processor.h>
  25. #include <asm/immap_85xx.h>
  26. #include <asm/processor.h>
  27. #include <asm/mmu.h>
  28. struct sdram_conf_s {
  29. unsigned long size;
  30. unsigned long reg;
  31. };
  32. typedef struct sdram_conf_s sdram_conf_t;
  33. sdram_conf_t ddr_cs_conf[] = {
  34. {(512 << 20), 0x80000202}, /* 512MB, 14x10(4) */
  35. {(256 << 20), 0x80000102}, /* 256MB, 13x10(4) */
  36. {(128 << 20), 0x80000101}, /* 128MB, 13x9(4) */
  37. {(64 << 20), 0x80000001}, /* 64MB, 12x9(4) */
  38. };
  39. #define N_DDR_CS_CONF (sizeof(ddr_cs_conf) / sizeof(ddr_cs_conf[0]))
  40. int cas_latency(void);
  41. /*
  42. * Autodetect onboard DDR SDRAM on 85xx platforms
  43. *
  44. * NOTE: Some of the hardcoded values are hardware dependant,
  45. * so this should be extended for other future boards
  46. * using this routine!
  47. */
  48. long int sdram_setup(int casl)
  49. {
  50. int i;
  51. volatile ccsr_ddr_t *ddr = (void *)(CFG_MPC85xx_DDR_ADDR);
  52. unsigned long cfg_ddr_timing1;
  53. unsigned long cfg_ddr_mode;
  54. /*
  55. * Disable memory controller.
  56. */
  57. ddr->cs0_config = 0;
  58. ddr->sdram_cfg = 0;
  59. switch (casl) {
  60. case 20:
  61. cfg_ddr_timing1 = 0x47405331 | (3 << 16);
  62. cfg_ddr_mode = 0x40020002 | (2 << 4);
  63. break;
  64. case 25:
  65. cfg_ddr_timing1 = 0x47405331 | (4 << 16);
  66. cfg_ddr_mode = 0x40020002 | (6 << 4);
  67. break;
  68. case 30:
  69. default:
  70. cfg_ddr_timing1 = 0x47405331 | (5 << 16);
  71. cfg_ddr_mode = 0x40020002 | (3 << 4);
  72. break;
  73. }
  74. ddr->cs0_bnds = (ddr_cs_conf[0].size - 1) >> 24;
  75. ddr->cs0_config = ddr_cs_conf[0].reg;
  76. ddr->timing_cfg_1 = cfg_ddr_timing1;
  77. ddr->timing_cfg_2 = 0x00000800; /* P9-45,may need tuning */
  78. ddr->sdram_mode = cfg_ddr_mode;
  79. ddr->sdram_interval = 0x05160100; /* autocharge,no open page */
  80. ddr->err_disable = 0x0000000D;
  81. asm ("sync;isync;msync");
  82. udelay(1000);
  83. ddr->sdram_cfg = 0xc2000000; /* unbuffered,no DYN_PWR */
  84. asm ("sync; isync; msync");
  85. udelay(1000);
  86. for (i=0; i<N_DDR_CS_CONF; i++) {
  87. ddr->cs0_config = ddr_cs_conf[i].reg;
  88. if (get_ram_size(0, ddr_cs_conf[i].size) == ddr_cs_conf[i].size) {
  89. /*
  90. * OK, size detected -> all done
  91. */
  92. return ddr_cs_conf[i].size;
  93. }
  94. }
  95. return 0; /* nothing found ! */
  96. }
  97. void board_add_ram_info(int use_default)
  98. {
  99. int casl;
  100. if (use_default)
  101. casl = CONFIG_DDR_DEFAULT_CL;
  102. else
  103. casl = cas_latency();
  104. puts(" (CL=");
  105. switch (casl) {
  106. case 20:
  107. puts("2)");
  108. break;
  109. case 25:
  110. puts("2.5)");
  111. break;
  112. case 30:
  113. puts("3)");
  114. break;
  115. }
  116. }
  117. long int initdram (int board_type)
  118. {
  119. long dram_size = 0;
  120. int casl;
  121. #if defined(CONFIG_DDR_DLL)
  122. /*
  123. * This DLL-Override only used on TQM8540 and TQM8560
  124. */
  125. {
  126. volatile ccsr_gur_t *gur = (void *)(CFG_MPC85xx_GUTS_ADDR);
  127. int i,x;
  128. x = 10;
  129. /*
  130. * Work around to stabilize DDR DLL
  131. */
  132. gur->ddrdllcr = 0x81000000;
  133. asm("sync;isync;msync");
  134. udelay (200);
  135. while (gur->ddrdllcr != 0x81000100) {
  136. gur->devdisr = gur->devdisr | 0x00010000;
  137. asm("sync;isync;msync");
  138. for (i=0; i<x; i++)
  139. ;
  140. gur->devdisr = gur->devdisr & 0xfff7ffff;
  141. asm("sync;isync;msync");
  142. x++;
  143. }
  144. }
  145. #endif
  146. casl = cas_latency();
  147. dram_size = sdram_setup(casl);
  148. if ((dram_size == 0) && (casl != CONFIG_DDR_DEFAULT_CL)) {
  149. /*
  150. * Try again with default CAS latency
  151. */
  152. puts("Problem with CAS lantency");
  153. board_add_ram_info(1);
  154. puts(", using default CL!\n");
  155. casl = CONFIG_DDR_DEFAULT_CL;
  156. dram_size = sdram_setup(casl);
  157. puts(" ");
  158. }
  159. return dram_size;
  160. }
  161. #if defined(CFG_DRAM_TEST)
  162. int testdram (void)
  163. {
  164. uint *pstart = (uint *) CFG_MEMTEST_START;
  165. uint *pend = (uint *) CFG_MEMTEST_END;
  166. uint *p;
  167. printf ("SDRAM test phase 1:\n");
  168. for (p = pstart; p < pend; p++)
  169. *p = 0xaaaaaaaa;
  170. for (p = pstart; p < pend; p++) {
  171. if (*p != 0xaaaaaaaa) {
  172. printf ("SDRAM test fails at: %08x\n", (uint) p);
  173. return 1;
  174. }
  175. }
  176. printf ("SDRAM test phase 2:\n");
  177. for (p = pstart; p < pend; p++)
  178. *p = 0x55555555;
  179. for (p = pstart; p < pend; p++) {
  180. if (*p != 0x55555555) {
  181. printf ("SDRAM test fails at: %08x\n", (uint) p);
  182. return 1;
  183. }
  184. }
  185. printf ("SDRAM test passed.\n");
  186. return 0;
  187. }
  188. #endif