sdram.c 4.9 KB

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