sdram.c 11 KB

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