sdram.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #include <spd.h>
  29. struct sdram_conf_s {
  30. unsigned long size;
  31. unsigned long reg;
  32. };
  33. typedef struct sdram_conf_s sdram_conf_t;
  34. sdram_conf_t ddr_cs_conf[] = {
  35. {(512 << 20), 0x80000202}, /* 512MB, 14x10(4) */
  36. {(256 << 20), 0x80000102}, /* 256MB, 13x10(4) */
  37. {(128 << 20), 0x80000101}, /* 128MB, 13x9(4) */
  38. {(64 << 20), 0x80000001}, /* 64MB, 12x9(4) */
  39. };
  40. #define N_DDR_CS_CONF (sizeof(ddr_cs_conf) / sizeof(ddr_cs_conf[0]))
  41. int cas_latency(void);
  42. /*
  43. * Autodetect onboard DDR SDRAM on 85xx platforms
  44. *
  45. * NOTE: Some of the hardcoded values are hardware dependant,
  46. * so this should be extended for other future boards
  47. * using this routine!
  48. */
  49. long int sdram_setup(int casl)
  50. {
  51. int i;
  52. volatile ccsr_ddr_t *ddr = (void *)(CFG_MPC85xx_DDR_ADDR);
  53. unsigned long cfg_ddr_timing1;
  54. unsigned long cfg_ddr_mode;
  55. /*
  56. * Disable memory controller.
  57. */
  58. ddr->cs0_config = 0;
  59. ddr->sdram_cfg = 0;
  60. switch (casl) {
  61. case 20:
  62. cfg_ddr_timing1 = 0x47405331 | (3 << 16);
  63. cfg_ddr_mode = 0x40020002 | (2 << 4);
  64. break;
  65. case 25:
  66. cfg_ddr_timing1 = 0x47405331 | (4 << 16);
  67. cfg_ddr_mode = 0x40020002 | (6 << 4);
  68. break;
  69. case 30:
  70. default:
  71. cfg_ddr_timing1 = 0x47405331 | (5 << 16);
  72. cfg_ddr_mode = 0x40020002 | (3 << 4);
  73. break;
  74. }
  75. ddr->cs0_bnds = (ddr_cs_conf[0].size - 1) >> 24;
  76. ddr->cs0_config = ddr_cs_conf[0].reg;
  77. ddr->timing_cfg_1 = cfg_ddr_timing1;
  78. ddr->timing_cfg_2 = 0x00000800; /* P9-45,may need tuning */
  79. ddr->sdram_mode = cfg_ddr_mode;
  80. ddr->sdram_interval = 0x05160100; /* autocharge,no open page */
  81. ddr->err_disable = 0x0000000D;
  82. asm ("sync;isync;msync");
  83. udelay(1000);
  84. ddr->sdram_cfg = 0xc2000000; /* unbuffered,no DYN_PWR */
  85. asm ("sync; isync; msync");
  86. udelay(1000);
  87. for (i=0; i<N_DDR_CS_CONF; i++) {
  88. ddr->cs0_config = ddr_cs_conf[i].reg;
  89. if (get_ram_size(0, ddr_cs_conf[i].size) == ddr_cs_conf[i].size) {
  90. /*
  91. * OK, size detected -> all done
  92. */
  93. return ddr_cs_conf[i].size;
  94. }
  95. }
  96. return 0; /* nothing found ! */
  97. }
  98. void board_add_ram_info(int use_default)
  99. {
  100. int casl;
  101. if (use_default)
  102. casl = CONFIG_DDR_DEFAULT_CL;
  103. else
  104. casl = cas_latency();
  105. puts(" (CL=");
  106. switch (casl) {
  107. case 20:
  108. puts("2)");
  109. break;
  110. case 25:
  111. puts("2.5)");
  112. break;
  113. case 30:
  114. puts("3)");
  115. break;
  116. }
  117. }
  118. long int initdram (int board_type)
  119. {
  120. long dram_size = 0;
  121. int casl;
  122. #if defined(CONFIG_DDR_DLL)
  123. /*
  124. * This DLL-Override only used on TQM8540 and TQM8560
  125. */
  126. {
  127. volatile ccsr_gur_t *gur = (void *)(CFG_MPC85xx_GUTS_ADDR);
  128. int i,x;
  129. x = 10;
  130. /*
  131. * Work around to stabilize DDR DLL
  132. */
  133. gur->ddrdllcr = 0x81000000;
  134. asm("sync;isync;msync");
  135. udelay (200);
  136. while (gur->ddrdllcr != 0x81000100) {
  137. gur->devdisr = gur->devdisr | 0x00010000;
  138. asm("sync;isync;msync");
  139. for (i=0; i<x; i++)
  140. ;
  141. gur->devdisr = gur->devdisr & 0xfff7ffff;
  142. asm("sync;isync;msync");
  143. x++;
  144. }
  145. }
  146. #endif
  147. casl = cas_latency();
  148. dram_size = sdram_setup(casl);
  149. if ((dram_size == 0) && (casl != CONFIG_DDR_DEFAULT_CL)) {
  150. /*
  151. * Try again with default CAS latency
  152. */
  153. puts("Problem with CAS lantency");
  154. board_add_ram_info(1);
  155. puts(", using default CL!\n");
  156. casl = CONFIG_DDR_DEFAULT_CL;
  157. dram_size = sdram_setup(casl);
  158. puts(" ");
  159. }
  160. return dram_size;
  161. }
  162. #if defined(CFG_DRAM_TEST)
  163. int testdram (void)
  164. {
  165. uint *pstart = (uint *) CFG_MEMTEST_START;
  166. uint *pend = (uint *) CFG_MEMTEST_END;
  167. uint *p;
  168. printf ("SDRAM test phase 1:\n");
  169. for (p = pstart; p < pend; p++)
  170. *p = 0xaaaaaaaa;
  171. for (p = pstart; p < pend; p++) {
  172. if (*p != 0xaaaaaaaa) {
  173. printf ("SDRAM test fails at: %08x\n", (uint) p);
  174. return 1;
  175. }
  176. }
  177. printf ("SDRAM test phase 2:\n");
  178. for (p = pstart; p < pend; p++)
  179. *p = 0x55555555;
  180. for (p = pstart; p < pend; p++) {
  181. if (*p != 0x55555555) {
  182. printf ("SDRAM test fails at: %08x\n", (uint) p);
  183. return 1;
  184. }
  185. }
  186. printf ("SDRAM test passed.\n");
  187. return 0;
  188. }
  189. #endif