sdram.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * (C) Copyright 2005-2007
  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. #include "ecc.h"
  34. #ifdef CONFIG_SDRAM_BANK0
  35. #ifndef CONFIG_440
  36. #ifndef CONFIG_SYS_SDRAM_TABLE
  37. sdram_conf_t mb0cf[] = {
  38. {(128 << 20), 13, 0x000A4001}, /* (0-128MB) Address Mode 3, 13x10(4) */
  39. {(64 << 20), 13, 0x00084001}, /* (0-64MB) Address Mode 3, 13x9(4) */
  40. {(32 << 20), 12, 0x00062001}, /* (0-32MB) Address Mode 2, 12x9(4) */
  41. {(16 << 20), 12, 0x00046001}, /* (0-16MB) Address Mode 4, 12x8(4) */
  42. {(4 << 20), 11, 0x00008001}, /* (0-4MB) Address Mode 5, 11x8(2) */
  43. };
  44. #else
  45. sdram_conf_t mb0cf[] = CONFIG_SYS_SDRAM_TABLE;
  46. #endif
  47. #define N_MB0CF (sizeof(mb0cf) / sizeof(mb0cf[0]))
  48. #ifdef CONFIG_SYS_SDRAM_CASL
  49. static ulong ns2clks(ulong ns)
  50. {
  51. ulong bus_period_x_10 = ONE_BILLION / (get_bus_freq(0) / 10);
  52. return ((ns * 10) + bus_period_x_10) / bus_period_x_10;
  53. }
  54. #endif /* CONFIG_SYS_SDRAM_CASL */
  55. static ulong compute_sdtr1(ulong speed)
  56. {
  57. #ifdef CONFIG_SYS_SDRAM_CASL
  58. ulong tmp;
  59. ulong sdtr1 = 0;
  60. /* CASL */
  61. if (CONFIG_SYS_SDRAM_CASL < 2)
  62. sdtr1 |= (1 << SDRAM0_TR_CASL);
  63. else
  64. if (CONFIG_SYS_SDRAM_CASL > 4)
  65. sdtr1 |= (3 << SDRAM0_TR_CASL);
  66. else
  67. sdtr1 |= ((CONFIG_SYS_SDRAM_CASL-1) << SDRAM0_TR_CASL);
  68. /* PTA */
  69. tmp = ns2clks(CONFIG_SYS_SDRAM_PTA);
  70. if ((tmp >= 2) && (tmp <= 4))
  71. sdtr1 |= ((tmp-1) << SDRAM0_TR_PTA);
  72. else
  73. sdtr1 |= ((4-1) << SDRAM0_TR_PTA);
  74. /* CTP */
  75. tmp = ns2clks(CONFIG_SYS_SDRAM_CTP);
  76. if ((tmp >= 2) && (tmp <= 4))
  77. sdtr1 |= ((tmp-1) << SDRAM0_TR_CTP);
  78. else
  79. sdtr1 |= ((4-1) << SDRAM0_TR_CTP);
  80. /* LDF */
  81. tmp = ns2clks(CONFIG_SYS_SDRAM_LDF);
  82. if ((tmp >= 2) && (tmp <= 4))
  83. sdtr1 |= ((tmp-1) << SDRAM0_TR_LDF);
  84. else
  85. sdtr1 |= ((2-1) << SDRAM0_TR_LDF);
  86. /* RFTA */
  87. tmp = ns2clks(CONFIG_SYS_SDRAM_RFTA);
  88. if ((tmp >= 4) && (tmp <= 10))
  89. sdtr1 |= ((tmp-4) << SDRAM0_TR_RFTA);
  90. else
  91. sdtr1 |= ((10-4) << SDRAM0_TR_RFTA);
  92. /* RCD */
  93. tmp = ns2clks(CONFIG_SYS_SDRAM_RCD);
  94. if ((tmp >= 2) && (tmp <= 4))
  95. sdtr1 |= ((tmp-1) << SDRAM0_TR_RCD);
  96. else
  97. sdtr1 |= ((4-1) << SDRAM0_TR_RCD);
  98. return sdtr1;
  99. #else /* CONFIG_SYS_SDRAM_CASL */
  100. /*
  101. * If no values are configured in the board config file
  102. * use the default values, which seem to be ok for most
  103. * boards.
  104. *
  105. * REMARK:
  106. * For new board ports we strongly recommend to define the
  107. * correct values for the used SDRAM chips in your board
  108. * config file (see PPChameleonEVB.h)
  109. */
  110. if (speed > 100000000) {
  111. /*
  112. * 133 MHz SDRAM
  113. */
  114. return 0x01074015;
  115. } else {
  116. /*
  117. * default: 100 MHz SDRAM
  118. */
  119. return 0x0086400d;
  120. }
  121. #endif /* CONFIG_SYS_SDRAM_CASL */
  122. }
  123. /* refresh is expressed in ms */
  124. static ulong compute_rtr(ulong speed, ulong rows, ulong refresh)
  125. {
  126. #ifdef CONFIG_SYS_SDRAM_CASL
  127. ulong tmp;
  128. tmp = ((refresh*1000*1000) / (1 << rows)) * (speed / 1000);
  129. tmp /= 1000000;
  130. return ((tmp & 0x00003FF8) << 16);
  131. #else /* CONFIG_SYS_SDRAM_CASL */
  132. if (speed > 100000000) {
  133. /*
  134. * 133 MHz SDRAM
  135. */
  136. return 0x07f00000;
  137. } else {
  138. /*
  139. * default: 100 MHz SDRAM
  140. */
  141. return 0x05f00000;
  142. }
  143. #endif /* CONFIG_SYS_SDRAM_CASL */
  144. }
  145. /*
  146. * Autodetect onboard SDRAM on 405 platforms
  147. */
  148. phys_size_t initdram(int board_type)
  149. {
  150. ulong speed;
  151. ulong sdtr1;
  152. int i;
  153. /*
  154. * Determine SDRAM speed
  155. */
  156. speed = get_bus_freq(0); /* parameter not used on ppc4xx */
  157. /*
  158. * sdtr1 (register SDRAM0_TR) must take into account timings listed
  159. * in SDRAM chip datasheet. rtr (register SDRAM0_RTR) must take into
  160. * account actual SDRAM size. So we can set up sdtr1 according to what
  161. * is specified in board configuration file while rtr dependds on SDRAM
  162. * size we are assuming before detection.
  163. */
  164. sdtr1 = compute_sdtr1(speed);
  165. for (i=0; i<N_MB0CF; i++) {
  166. /*
  167. * Disable memory controller.
  168. */
  169. mtsdram(mem_mcopt1, 0x00000000);
  170. /*
  171. * Set MB0CF for bank 0.
  172. */
  173. mtsdram(mem_mb0cf, mb0cf[i].reg);
  174. mtsdram(mem_sdtr1, sdtr1);
  175. mtsdram(mem_rtr, compute_rtr(speed, mb0cf[i].rows, 64));
  176. udelay(200);
  177. /*
  178. * Set memory controller options reg, MCOPT1.
  179. * Set DC_EN to '1' and BRD_PRF to '01' for 16 byte PLB Burst
  180. * read/prefetch.
  181. */
  182. mtsdram(mem_mcopt1, 0x80800000);
  183. udelay(10000);
  184. if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) {
  185. phys_size_t size = mb0cf[i].size;
  186. /*
  187. * OK, size detected. Enable second bank if
  188. * defined (assumes same type as bank 0)
  189. */
  190. #ifdef CONFIG_SDRAM_BANK1
  191. mtsdram(mem_mcopt1, 0x00000000);
  192. mtsdram(mem_mb1cf, mb0cf[i].size | mb0cf[i].reg);
  193. mtsdram(mem_mcopt1, 0x80800000);
  194. udelay(10000);
  195. /*
  196. * Check if 2nd bank is really available.
  197. * If the size not equal to the size of the first
  198. * bank, then disable the 2nd bank completely.
  199. */
  200. if (get_ram_size((long *)mb0cf[i].size, mb0cf[i].size) !=
  201. mb0cf[i].size) {
  202. mtsdram(mem_mb1cf, 0);
  203. mtsdram(mem_mcopt1, 0);
  204. } else {
  205. /*
  206. * We have two identical banks, so the size
  207. * is twice the bank size
  208. */
  209. size = 2 * size;
  210. }
  211. #endif
  212. /*
  213. * OK, size detected -> all done
  214. */
  215. return size;
  216. }
  217. }
  218. return 0;
  219. }
  220. #else /* CONFIG_440 */
  221. /*
  222. * Define some default values. Those can be overwritten in the
  223. * board config file.
  224. */
  225. #ifndef CONFIG_SYS_SDRAM_TABLE
  226. sdram_conf_t mb0cf[] = {
  227. {(256 << 20), 13, 0x000C4001}, /* 256MB mode 3, 13x10(4) */
  228. {(128 << 20), 13, 0x000A4001}, /* 128MB mode 3, 13x10(4) */
  229. {(64 << 20), 12, 0x00082001} /* 64MB mode 2, 12x9(4) */
  230. };
  231. #else
  232. sdram_conf_t mb0cf[] = CONFIG_SYS_SDRAM_TABLE;
  233. #endif
  234. #ifndef CONFIG_SYS_SDRAM0_TR0
  235. #define CONFIG_SYS_SDRAM0_TR0 0x41094012
  236. #endif
  237. #ifndef CONFIG_SYS_SDRAM0_WDDCTR
  238. #define CONFIG_SYS_SDRAM0_WDDCTR 0x00000000 /* wrcp=0 dcd=0 */
  239. #endif
  240. #ifndef CONFIG_SYS_SDRAM0_RTR
  241. #define CONFIG_SYS_SDRAM0_RTR 0x04100000 /* 7.8us @ 133MHz PLB */
  242. #endif
  243. #ifndef CONFIG_SYS_SDRAM0_CFG0
  244. #define CONFIG_SYS_SDRAM0_CFG0 0x82000000 /* DCEN=1, PMUD=0, 64-bit */
  245. #endif
  246. #define N_MB0CF (sizeof(mb0cf) / sizeof(mb0cf[0]))
  247. #define NUM_TRIES 64
  248. #define NUM_READS 10
  249. static void sdram_tr1_set(int ram_address, int* tr1_value)
  250. {
  251. int i;
  252. int j, k;
  253. volatile unsigned int* ram_pointer = (unsigned int *)ram_address;
  254. int first_good = -1, last_bad = 0x1ff;
  255. unsigned long test[NUM_TRIES] = {
  256. 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
  257. 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
  258. 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
  259. 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
  260. 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
  261. 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
  262. 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
  263. 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
  264. 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
  265. 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
  266. 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
  267. 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
  268. 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
  269. 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
  270. 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
  271. 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55 };
  272. /* go through all possible SDRAM0_TR1[RDCT] values */
  273. for (i=0; i<=0x1ff; i++) {
  274. /* set the current value for TR1 */
  275. mtsdram(mem_tr1, (0x80800800 | i));
  276. /* write values */
  277. for (j=0; j<NUM_TRIES; j++) {
  278. ram_pointer[j] = test[j];
  279. /* clear any cache at ram location */
  280. __asm__("dcbf 0,%0": :"r" (&ram_pointer[j]));
  281. }
  282. /* read values back */
  283. for (j=0; j<NUM_TRIES; j++) {
  284. for (k=0; k<NUM_READS; k++) {
  285. /* clear any cache at ram location */
  286. __asm__("dcbf 0,%0": :"r" (&ram_pointer[j]));
  287. if (ram_pointer[j] != test[j])
  288. break;
  289. }
  290. /* read error */
  291. if (k != NUM_READS)
  292. break;
  293. }
  294. /* we have a SDRAM0_TR1[RDCT] that is part of the window */
  295. if (j == NUM_TRIES) {
  296. if (first_good == -1)
  297. first_good = i; /* found beginning of window */
  298. } else { /* bad read */
  299. /* if we have not had a good read then don't care */
  300. if (first_good != -1) {
  301. /* first failure after a good read */
  302. last_bad = i-1;
  303. break;
  304. }
  305. }
  306. }
  307. /* return the current value for TR1 */
  308. *tr1_value = (first_good + last_bad) / 2;
  309. }
  310. /*
  311. * Autodetect onboard DDR SDRAM on 440 platforms
  312. *
  313. * NOTE: Some of the hardcoded values are hardware dependant,
  314. * so this should be extended for other future boards
  315. * using this routine!
  316. */
  317. phys_size_t initdram(int board_type)
  318. {
  319. int i;
  320. int tr1_bank1;
  321. #if defined(CONFIG_440GX) || defined(CONFIG_440EP) || \
  322. defined(CONFIG_440GR) || defined(CONFIG_440SP)
  323. /*
  324. * Soft-reset SDRAM controller.
  325. */
  326. mtsdr(sdr_srst, SDR0_SRST_DMC);
  327. mtsdr(sdr_srst, 0x00000000);
  328. #endif
  329. for (i=0; i<N_MB0CF; i++) {
  330. /*
  331. * Disable memory controller.
  332. */
  333. mtsdram(mem_cfg0, 0x00000000);
  334. /*
  335. * Setup some default
  336. */
  337. mtsdram(mem_uabba, 0x00000000); /* ubba=0 (default) */
  338. mtsdram(mem_slio, 0x00000000); /* rdre=0 wrre=0 rarw=0 */
  339. mtsdram(mem_devopt, 0x00000000); /* dll=0 ds=0 (normal) */
  340. mtsdram(mem_wddctr, CONFIG_SYS_SDRAM0_WDDCTR);
  341. mtsdram(mem_clktr, 0x40000000); /* clkp=1 (90 deg wr) dcdt=0 */
  342. /*
  343. * Following for CAS Latency = 2.5 @ 133 MHz PLB
  344. */
  345. mtsdram(mem_b0cr, mb0cf[i].reg);
  346. mtsdram(mem_tr0, CONFIG_SYS_SDRAM0_TR0);
  347. mtsdram(mem_tr1, 0x80800800); /* SS=T2 SL=STAGE 3 CD=1 CT=0x00*/
  348. mtsdram(mem_rtr, CONFIG_SYS_SDRAM0_RTR);
  349. mtsdram(mem_cfg1, 0x00000000); /* Self-refresh exit, disable PM*/
  350. udelay(400); /* Delay 200 usecs (min) */
  351. /*
  352. * Enable the controller, then wait for DCEN to complete
  353. */
  354. mtsdram(mem_cfg0, CONFIG_SYS_SDRAM0_CFG0);
  355. udelay(10000);
  356. if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) {
  357. phys_size_t size = mb0cf[i].size;
  358. /*
  359. * Optimize TR1 to current hardware environment
  360. */
  361. sdram_tr1_set(0x00000000, &tr1_bank1);
  362. mtsdram(mem_tr1, (tr1_bank1 | 0x80800800));
  363. /*
  364. * OK, size detected. Enable second bank if
  365. * defined (assumes same type as bank 0)
  366. */
  367. #ifdef CONFIG_SDRAM_BANK1
  368. mtsdram(mem_cfg0, 0);
  369. mtsdram(mem_b1cr, mb0cf[i].size | mb0cf[i].reg);
  370. mtsdram(mem_cfg0, CONFIG_SYS_SDRAM0_CFG0);
  371. udelay(10000);
  372. /*
  373. * Check if 2nd bank is really available.
  374. * If the size not equal to the size of the first
  375. * bank, then disable the 2nd bank completely.
  376. */
  377. if (get_ram_size((long *)mb0cf[i].size, mb0cf[i].size)
  378. != mb0cf[i].size) {
  379. mtsdram(mem_cfg0, 0);
  380. mtsdram(mem_b1cr, 0);
  381. mtsdram(mem_cfg0, CONFIG_SYS_SDRAM0_CFG0);
  382. udelay(10000);
  383. } else {
  384. /*
  385. * We have two identical banks, so the size
  386. * is twice the bank size
  387. */
  388. size = 2 * size;
  389. }
  390. #endif
  391. #ifdef CONFIG_SDRAM_ECC
  392. ecc_init(0, size);
  393. #endif
  394. /*
  395. * OK, size detected -> all done
  396. */
  397. return size;
  398. }
  399. }
  400. return 0; /* nothing found ! */
  401. }
  402. #endif /* CONFIG_440 */
  403. #endif /* CONFIG_SDRAM_BANK0 */