sdram.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * (C) Copyright 2005-2006
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  4. *
  5. * (C) Copyright 2006
  6. * DAVE Srl <www.dave-tech.it>
  7. *
  8. * (C) Copyright 2002-2004
  9. * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
  10. *
  11. * See file CREDITS for list of people who contributed to this
  12. * project.
  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. #include <common.h>
  30. #include <ppc4xx.h>
  31. #include <asm/processor.h>
  32. #include "sdram.h"
  33. #ifdef CONFIG_SDRAM_BANK0
  34. #ifndef CFG_SDRAM_TABLE
  35. sdram_conf_t mb0cf[] = {
  36. {(128 << 20), 13, 0x000A4001}, /* (0-128MB) Address Mode 3, 13x10(4) */
  37. {(64 << 20), 13, 0x00084001}, /* (0-64MB) Address Mode 3, 13x9(4) */
  38. {(32 << 20), 12, 0x00062001}, /* (0-32MB) Address Mode 2, 12x9(4) */
  39. {(16 << 20), 12, 0x00046001}, /* (0-16MB) Address Mode 4, 12x8(4) */
  40. {(4 << 20), 11, 0x00008001}, /* (0-4MB) Address Mode 5, 11x8(2) */
  41. };
  42. #else
  43. sdram_conf_t mb0cf[] = CFG_SDRAM_TABLE;
  44. #endif
  45. #define N_MB0CF (sizeof(mb0cf) / sizeof(mb0cf[0]))
  46. #ifndef CONFIG_440
  47. #ifdef CFG_SDRAM_CASL
  48. static ulong ns2clks(ulong ns)
  49. {
  50. ulong bus_period_x_10 = ONE_BILLION / (get_bus_freq(0) / 10);
  51. return ((ns * 10) + bus_period_x_10) / bus_period_x_10;
  52. }
  53. #endif /* CFG_SDRAM_CASL */
  54. static ulong compute_sdtr1(ulong speed)
  55. {
  56. #ifdef CFG_SDRAM_CASL
  57. ulong tmp;
  58. ulong sdtr1 = 0;
  59. /* CASL */
  60. if (CFG_SDRAM_CASL < 2)
  61. sdtr1 |= (1 << SDRAM0_TR_CASL);
  62. else
  63. if (CFG_SDRAM_CASL > 4)
  64. sdtr1 |= (3 << SDRAM0_TR_CASL);
  65. else
  66. sdtr1 |= ((CFG_SDRAM_CASL-1) << SDRAM0_TR_CASL);
  67. /* PTA */
  68. tmp = ns2clks(CFG_SDRAM_PTA);
  69. if ((tmp >= 2) && (tmp <= 4))
  70. sdtr1 |= ((tmp-1) << SDRAM0_TR_PTA);
  71. else
  72. sdtr1 |= ((4-1) << SDRAM0_TR_PTA);
  73. /* CTP */
  74. tmp = ns2clks(CFG_SDRAM_CTP);
  75. if ((tmp >= 2) && (tmp <= 4))
  76. sdtr1 |= ((tmp-1) << SDRAM0_TR_CTP);
  77. else
  78. sdtr1 |= ((4-1) << SDRAM0_TR_CTP);
  79. /* LDF */
  80. tmp = ns2clks(CFG_SDRAM_LDF);
  81. if ((tmp >= 2) && (tmp <= 4))
  82. sdtr1 |= ((tmp-1) << SDRAM0_TR_LDF);
  83. else
  84. sdtr1 |= ((2-1) << SDRAM0_TR_LDF);
  85. /* RFTA */
  86. tmp = ns2clks(CFG_SDRAM_RFTA);
  87. if ((tmp >= 4) && (tmp <= 10))
  88. sdtr1 |= ((tmp-4) << SDRAM0_TR_RFTA);
  89. else
  90. sdtr1 |= ((10-4) << SDRAM0_TR_RFTA);
  91. /* RCD */
  92. tmp = ns2clks(CFG_SDRAM_RCD);
  93. if ((tmp >= 2) && (tmp <= 4))
  94. sdtr1 |= ((tmp-1) << SDRAM0_TR_RCD);
  95. else
  96. sdtr1 |= ((4-1) << SDRAM0_TR_RCD);
  97. return sdtr1;
  98. #else /* CFG_SDRAM_CASL */
  99. /*
  100. * If no values are configured in the board config file
  101. * use the default values, which seem to be ok for most
  102. * boards.
  103. *
  104. * REMARK:
  105. * For new board ports we strongly recommend to define the
  106. * correct values for the used SDRAM chips in your board
  107. * config file (see PPChameleonEVB.h)
  108. */
  109. if (speed > 100000000) {
  110. /*
  111. * 133 MHz SDRAM
  112. */
  113. return 0x01074015;
  114. } else {
  115. /*
  116. * default: 100 MHz SDRAM
  117. */
  118. return 0x0086400d;
  119. }
  120. #endif /* CFG_SDRAM_CASL */
  121. }
  122. /* refresh is expressed in ms */
  123. static ulong compute_rtr(ulong speed, ulong rows, ulong refresh)
  124. {
  125. #ifdef CFG_SDRAM_CASL
  126. ulong tmp;
  127. tmp = ((refresh*1000*1000) / (1 << rows)) * (speed / 1000);
  128. tmp /= 1000000;
  129. return ((tmp & 0x00003FF8) << 16);
  130. #else /* CFG_SDRAM_CASL */
  131. if (speed > 100000000) {
  132. /*
  133. * 133 MHz SDRAM
  134. */
  135. return 0x07f00000;
  136. } else {
  137. /*
  138. * default: 100 MHz SDRAM
  139. */
  140. return 0x05f00000;
  141. }
  142. #endif /* CFG_SDRAM_CASL */
  143. }
  144. /*
  145. * Autodetect onboard SDRAM on 405 platforms
  146. */
  147. void sdram_init(void)
  148. {
  149. ulong speed;
  150. ulong sdtr1;
  151. int i;
  152. /*
  153. * Determine SDRAM speed
  154. */
  155. speed = get_bus_freq(0); /* parameter not used on ppc4xx */
  156. /*
  157. * sdtr1 (register SDRAM0_TR) must take into account timings listed
  158. * in SDRAM chip datasheet. rtr (register SDRAM0_RTR) must take into
  159. * account actual SDRAM size. So we can set up sdtr1 according to what
  160. * is specified in board configuration file while rtr dependds on SDRAM
  161. * size we are assuming before detection.
  162. */
  163. sdtr1 = compute_sdtr1(speed);
  164. for (i=0; i<N_MB0CF; i++) {
  165. /*
  166. * Disable memory controller.
  167. */
  168. mtsdram0(mem_mcopt1, 0x00000000);
  169. /*
  170. * Set MB0CF for bank 0.
  171. */
  172. mtsdram0(mem_mb0cf, mb0cf[i].reg);
  173. mtsdram0(mem_sdtr1, sdtr1);
  174. mtsdram0(mem_rtr, compute_rtr(speed, mb0cf[i].rows, 64));
  175. udelay(200);
  176. /*
  177. * Set memory controller options reg, MCOPT1.
  178. * Set DC_EN to '1' and BRD_PRF to '01' for 16 byte PLB Burst
  179. * read/prefetch.
  180. */
  181. mtsdram0(mem_mcopt1, 0x80800000);
  182. udelay(10000);
  183. if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) {
  184. /*
  185. * OK, size detected -> all done
  186. */
  187. return;
  188. }
  189. }
  190. }
  191. #else /* CONFIG_440 */
  192. #define NUM_TRIES 64
  193. #define NUM_READS 10
  194. static void sdram_tr1_set(int ram_address, int* tr1_value)
  195. {
  196. int i;
  197. int j, k;
  198. volatile unsigned int* ram_pointer = (unsigned int *)ram_address;
  199. int first_good = -1, last_bad = 0x1ff;
  200. unsigned long test[NUM_TRIES] = {
  201. 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
  202. 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
  203. 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
  204. 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
  205. 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
  206. 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
  207. 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
  208. 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
  209. 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
  210. 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
  211. 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
  212. 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
  213. 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
  214. 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
  215. 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
  216. 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55 };
  217. /* go through all possible SDRAM0_TR1[RDCT] values */
  218. for (i=0; i<=0x1ff; i++) {
  219. /* set the current value for TR1 */
  220. mtsdram(mem_tr1, (0x80800800 | i));
  221. /* write values */
  222. for (j=0; j<NUM_TRIES; j++) {
  223. ram_pointer[j] = test[j];
  224. /* clear any cache at ram location */
  225. __asm__("dcbf 0,%0": :"r" (&ram_pointer[j]));
  226. }
  227. /* read values back */
  228. for (j=0; j<NUM_TRIES; j++) {
  229. for (k=0; k<NUM_READS; k++) {
  230. /* clear any cache at ram location */
  231. __asm__("dcbf 0,%0": :"r" (&ram_pointer[j]));
  232. if (ram_pointer[j] != test[j])
  233. break;
  234. }
  235. /* read error */
  236. if (k != NUM_READS)
  237. break;
  238. }
  239. /* we have a SDRAM0_TR1[RDCT] that is part of the window */
  240. if (j == NUM_TRIES) {
  241. if (first_good == -1)
  242. first_good = i; /* found beginning of window */
  243. } else { /* bad read */
  244. /* if we have not had a good read then don't care */
  245. if (first_good != -1) {
  246. /* first failure after a good read */
  247. last_bad = i-1;
  248. break;
  249. }
  250. }
  251. }
  252. /* return the current value for TR1 */
  253. *tr1_value = (first_good + last_bad) / 2;
  254. }
  255. #ifdef CONFIG_SDRAM_ECC
  256. static void ecc_init(ulong start, ulong size)
  257. {
  258. ulong current_addr; /* current byte address */
  259. ulong end_addr; /* end of memory region */
  260. ulong addr_inc; /* address skip between writes */
  261. ulong cfg0_reg; /* for restoring ECC state */
  262. /*
  263. * TODO: Enable dcache before running this test (speedup)
  264. */
  265. mfsdram(mem_cfg0, cfg0_reg);
  266. mtsdram(mem_cfg0, (cfg0_reg & ~SDRAM_CFG0_MEMCHK) | SDRAM_CFG0_MEMCHK_GEN);
  267. /*
  268. * look at geometry of SDRAM (data width) to determine whether we
  269. * can skip words when writing
  270. */
  271. if ((cfg0_reg & SDRAM_CFG0_DRAMWDTH) == SDRAM_CFG0_DRAMWDTH_32)
  272. addr_inc = 4;
  273. else
  274. addr_inc = 8;
  275. current_addr = start;
  276. end_addr = start + size;
  277. while (current_addr < end_addr) {
  278. *((ulong *)current_addr) = 0x00000000;
  279. current_addr += addr_inc;
  280. }
  281. /*
  282. * TODO: Flush dcache and disable it again
  283. */
  284. /*
  285. * Enable ecc checking and parity errors
  286. */
  287. mtsdram(mem_cfg0, (cfg0_reg & ~SDRAM_CFG0_MEMCHK) | SDRAM_CFG0_MEMCHK_CHK);
  288. }
  289. #endif
  290. /*
  291. * Autodetect onboard DDR SDRAM on 440 platforms
  292. *
  293. * NOTE: Some of the hardcoded values are hardware dependant,
  294. * so this should be extended for other future boards
  295. * using this routine!
  296. */
  297. long int initdram(int board_type)
  298. {
  299. int i;
  300. int tr1_bank1;
  301. #if defined(CONFIG_440GX) || defined(CONFIG_440EP) || defined(CONFIG_440GR) || defined(CONFIG_440SP)
  302. /*
  303. * Soft-reset SDRAM controller.
  304. */
  305. mtsdr(sdr_srst, SDR0_SRST_DMC);
  306. mtsdr(sdr_srst, 0x00000000);
  307. #endif
  308. for (i=0; i<N_MB0CF; i++) {
  309. /*
  310. * Disable memory controller.
  311. */
  312. mtsdram(mem_cfg0, 0x00000000);
  313. /*
  314. * Setup some default
  315. */
  316. mtsdram(mem_uabba, 0x00000000); /* ubba=0 (default) */
  317. mtsdram(mem_slio, 0x00000000); /* rdre=0 wrre=0 rarw=0 */
  318. mtsdram(mem_devopt, 0x00000000); /* dll=0 ds=0 (normal) */
  319. mtsdram(mem_wddctr, 0x00000000); /* wrcp=0 dcd=0 */
  320. mtsdram(mem_clktr, 0x40000000); /* clkp=1 (90 deg wr) dcdt=0 */
  321. /*
  322. * Following for CAS Latency = 2.5 @ 133 MHz PLB
  323. */
  324. mtsdram(mem_b0cr, mb0cf[i].reg);
  325. mtsdram(mem_tr0, 0x41094012);
  326. mtsdram(mem_tr1, 0x80800800); /* SS=T2 SL=STAGE 3 CD=1 CT=0x00*/
  327. mtsdram(mem_rtr, 0x7e000000); /* Interval 15.20µs @ 133MHz PLB*/
  328. mtsdram(mem_cfg1, 0x00000000); /* Self-refresh exit, disable PM*/
  329. udelay(400); /* Delay 200 usecs (min) */
  330. /*
  331. * Enable the controller, then wait for DCEN to complete
  332. */
  333. mtsdram(mem_cfg0, 0x82000000); /* DCEN=1, PMUD=0, 64-bit */
  334. udelay(10000);
  335. if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) {
  336. /*
  337. * Optimize TR1 to current hardware environment
  338. */
  339. sdram_tr1_set(0x00000000, &tr1_bank1);
  340. mtsdram(mem_tr1, (tr1_bank1 | 0x80800800));
  341. #ifdef CONFIG_SDRAM_ECC
  342. ecc_init(0, mb0cf[i].size);
  343. #endif
  344. /*
  345. * OK, size detected -> all done
  346. */
  347. return mb0cf[i].size;
  348. }
  349. }
  350. return 0; /* nothing found ! */
  351. }
  352. #endif /* CONFIG_440 */
  353. #endif /* CONFIG_SDRAM_BANK0 */