sdram.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * (C) Copyright 2003-2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2004
  6. * Mark Jonas, Freescale Semiconductor, mark.jonas@freescale.com.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <mpc5xxx.h>
  28. #include "sdram.h"
  29. #ifndef CFG_RAMBOOT
  30. static void mpc5xxx_sdram_start (sdram_conf_t *sdram_conf, int hi_addr)
  31. {
  32. long hi_addr_bit = hi_addr ? 0x01000000 : 0;
  33. /* unlock mode register */
  34. *(vu_long *)MPC5XXX_SDRAM_CTRL = sdram_conf->control | 0x80000000 | hi_addr_bit;
  35. __asm__ volatile ("sync");
  36. /* precharge all banks */
  37. *(vu_long *)MPC5XXX_SDRAM_CTRL = sdram_conf->control | 0x80000002 | hi_addr_bit;
  38. __asm__ volatile ("sync");
  39. if (sdram_conf->ddr) {
  40. /* set mode register: extended mode */
  41. *(vu_long *)MPC5XXX_SDRAM_MODE = sdram_conf->emode;
  42. __asm__ volatile ("sync");
  43. /* set mode register: reset DLL */
  44. *(vu_long *)MPC5XXX_SDRAM_MODE = sdram_conf->mode | 0x04000000;
  45. __asm__ volatile ("sync");
  46. }
  47. /* precharge all banks */
  48. *(vu_long *)MPC5XXX_SDRAM_CTRL = sdram_conf->control | 0x80000002 | hi_addr_bit;
  49. __asm__ volatile ("sync");
  50. /* auto refresh */
  51. *(vu_long *)MPC5XXX_SDRAM_CTRL = sdram_conf->control | 0x80000004 | hi_addr_bit;
  52. __asm__ volatile ("sync");
  53. /* set mode register */
  54. *(vu_long *)MPC5XXX_SDRAM_MODE = sdram_conf->mode;
  55. __asm__ volatile ("sync");
  56. /* normal operation */
  57. *(vu_long *)MPC5XXX_SDRAM_CTRL = sdram_conf->control | hi_addr_bit;
  58. __asm__ volatile ("sync");
  59. }
  60. #endif
  61. /*
  62. * ATTENTION: Although partially referenced initdram does NOT make real use
  63. * use of CFG_SDRAM_BASE. The code does not work if CFG_SDRAM_BASE
  64. * is something else than 0x00000000.
  65. */
  66. #if defined(CONFIG_MPC5200)
  67. long int mpc5xxx_sdram_init (sdram_conf_t *sdram_conf)
  68. {
  69. ulong dramsize = 0;
  70. ulong dramsize2 = 0;
  71. #ifndef CFG_RAMBOOT
  72. ulong test1, test2;
  73. /* setup SDRAM chip selects */
  74. *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0x0000001e;/* 2G at 0x0 */
  75. *(vu_long *)MPC5XXX_SDRAM_CS1CFG = 0x80000000;/* disabled */
  76. __asm__ volatile ("sync");
  77. /* setup config registers */
  78. *(vu_long *)MPC5XXX_SDRAM_CONFIG1 = sdram_conf->config1;
  79. *(vu_long *)MPC5XXX_SDRAM_CONFIG2 = sdram_conf->config2;
  80. __asm__ volatile ("sync");
  81. if (sdram_conf->ddr) {
  82. /* set tap delay */
  83. *(vu_long *)MPC5XXX_CDM_PORCFG = sdram_conf->tapdelay;
  84. __asm__ volatile ("sync");
  85. }
  86. /* find RAM size using SDRAM CS0 only */
  87. mpc5xxx_sdram_start(sdram_conf, 0);
  88. test1 = get_ram_size((long *)CFG_SDRAM_BASE, 0x80000000);
  89. mpc5xxx_sdram_start(sdram_conf, 1);
  90. test2 = get_ram_size((long *)CFG_SDRAM_BASE, 0x80000000);
  91. if (test1 > test2) {
  92. mpc5xxx_sdram_start(sdram_conf, 0);
  93. dramsize = test1;
  94. } else {
  95. dramsize = test2;
  96. }
  97. /* memory smaller than 1MB is impossible */
  98. if (dramsize < (1 << 20)) {
  99. dramsize = 0;
  100. }
  101. /* set SDRAM CS0 size according to the amount of RAM found */
  102. if (dramsize > 0) {
  103. *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0x13 + __builtin_ffs(dramsize >> 20) - 1;
  104. } else {
  105. *(vu_long *)MPC5XXX_SDRAM_CS0CFG = 0; /* disabled */
  106. }
  107. /* let SDRAM CS1 start right after CS0 */
  108. *(vu_long *)MPC5XXX_SDRAM_CS1CFG = dramsize + 0x0000001e;/* 2G */
  109. /* find RAM size using SDRAM CS1 only */
  110. mpc5xxx_sdram_start(sdram_conf, 0);
  111. test1 = get_ram_size((long *)(CFG_SDRAM_BASE + dramsize), 0x80000000);
  112. mpc5xxx_sdram_start(sdram_conf, 1);
  113. test2 = get_ram_size((long *)(CFG_SDRAM_BASE + dramsize), 0x80000000);
  114. if (test1 > test2) {
  115. mpc5xxx_sdram_start(sdram_conf, 0);
  116. dramsize2 = test1;
  117. } else {
  118. dramsize2 = test2;
  119. }
  120. /* memory smaller than 1MB is impossible */
  121. if (dramsize2 < (1 << 20)) {
  122. dramsize2 = 0;
  123. }
  124. /* set SDRAM CS1 size according to the amount of RAM found */
  125. if (dramsize2 > 0) {
  126. *(vu_long *)MPC5XXX_SDRAM_CS1CFG = dramsize
  127. | (0x13 + __builtin_ffs(dramsize2 >> 20) - 1);
  128. } else {
  129. *(vu_long *)MPC5XXX_SDRAM_CS1CFG = dramsize; /* disabled */
  130. }
  131. #else /* CFG_RAMBOOT */
  132. /* retrieve size of memory connected to SDRAM CS0 */
  133. dramsize = *(vu_long *)MPC5XXX_SDRAM_CS0CFG & 0xFF;
  134. if (dramsize >= 0x13) {
  135. dramsize = (1 << (dramsize - 0x13)) << 20;
  136. } else {
  137. dramsize = 0;
  138. }
  139. /* retrieve size of memory connected to SDRAM CS1 */
  140. dramsize2 = *(vu_long *)MPC5XXX_SDRAM_CS1CFG & 0xFF;
  141. if (dramsize2 >= 0x13) {
  142. dramsize2 = (1 << (dramsize2 - 0x13)) << 20;
  143. } else {
  144. dramsize2 = 0;
  145. }
  146. #endif /* CFG_RAMBOOT */
  147. return dramsize + dramsize2;
  148. }
  149. #elif defined(CONFIG_MGT5100)
  150. long int mpc5xxx_sdram_init (sdram_conf_t *sdram_conf)
  151. {
  152. ulong dramsize = 0;
  153. #ifndef CFG_RAMBOOT
  154. ulong test1, test2;
  155. /* setup and enable SDRAM chip selects */
  156. *(vu_long *)MPC5XXX_SDRAM_START = 0x00000000;
  157. *(vu_long *)MPC5XXX_SDRAM_STOP = 0x0000ffff;/* 2G */
  158. *(vu_long *)MPC5XXX_ADDECR |= (1 << 22); /* Enable SDRAM */
  159. __asm__ volatile ("sync");
  160. /* setup config registers */
  161. *(vu_long *)MPC5XXX_SDRAM_CONFIG1 = sdram_conf->config1;
  162. *(vu_long *)MPC5XXX_SDRAM_CONFIG2 = sdram_conf->config2;
  163. /* address select register */
  164. *(vu_long *)MPC5XXX_SDRAM_XLBSEL = sdram_conf->addrsel;
  165. __asm__ volatile ("sync");
  166. /* find RAM size */
  167. mpc5xxx_sdram_start(sdram_conf, 0);
  168. test1 = get_ram_size((long *)CFG_SDRAM_BASE, 0x80000000);
  169. mpc5xxx_sdram_start(sdram_conf, 1);
  170. test2 = get_ram_size((long *)CFG_SDRAM_BASE, 0x80000000);
  171. if (test1 > test2) {
  172. mpc5xxx_sdram_start(sdram_conf, 0);
  173. dramsize = test1;
  174. } else {
  175. dramsize = test2;
  176. }
  177. /* set SDRAM end address according to size */
  178. *(vu_long *)MPC5XXX_SDRAM_STOP = ((dramsize - 1) >> 15);
  179. #else /* CFG_RAMBOOT */
  180. /* Retrieve amount of SDRAM available */
  181. dramsize = ((*(vu_long *)MPC5XXX_SDRAM_STOP + 1) << 15);
  182. #endif /* CFG_RAMBOOT */
  183. return dramsize;
  184. }
  185. #else
  186. #error Neither CONFIG_MPC5200 or CONFIG_MGT5100 defined
  187. #endif