40x_spd_sdram.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * arch/powerpc/cpu/ppc4xx/40x_spd_sdram.c
  3. * This SPD SDRAM detection code supports IBM/AMCC PPC44x cpu with a
  4. * SDRAM controller. Those are all current 405 PPC's.
  5. *
  6. * (C) Copyright 2001
  7. * Bill Hunter, Wave 7 Optics, williamhunter@attbi.com
  8. *
  9. * Based on code by:
  10. *
  11. * Kenneth Johansson ,Ericsson AB.
  12. * kenneth.johansson@etx.ericsson.se
  13. *
  14. * hacked up by bill hunter. fixed so we could run before
  15. * serial_init and console_init. previous version avoided this by
  16. * running out of cache memory during serial/console init, then running
  17. * this code later.
  18. *
  19. * (C) Copyright 2002
  20. * Jun Gu, Artesyn Technology, jung@artesyncp.com
  21. * Support for AMCC 440 based on OpenBIOS draminit.c from IBM.
  22. *
  23. * (C) Copyright 2005
  24. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  25. *
  26. * SPDX-License-Identifier: GPL-2.0+
  27. */
  28. #include <common.h>
  29. #include <asm/processor.h>
  30. #include <i2c.h>
  31. #include <asm/ppc4xx.h>
  32. #if defined(CONFIG_SPD_EEPROM) && !defined(CONFIG_440)
  33. /*
  34. * Set default values
  35. */
  36. #define ONE_BILLION 1000000000
  37. #define SDRAM0_CFG_DCE 0x80000000
  38. #define SDRAM0_CFG_SRE 0x40000000
  39. #define SDRAM0_CFG_PME 0x20000000
  40. #define SDRAM0_CFG_MEMCHK 0x10000000
  41. #define SDRAM0_CFG_REGEN 0x08000000
  42. #define SDRAM0_CFG_ECCDD 0x00400000
  43. #define SDRAM0_CFG_EMDULR 0x00200000
  44. #define SDRAM0_CFG_DRW_SHIFT (31-6)
  45. #define SDRAM0_CFG_BRPF_SHIFT (31-8)
  46. #define SDRAM0_TR_CASL_SHIFT (31-8)
  47. #define SDRAM0_TR_PTA_SHIFT (31-13)
  48. #define SDRAM0_TR_CTP_SHIFT (31-15)
  49. #define SDRAM0_TR_LDF_SHIFT (31-17)
  50. #define SDRAM0_TR_RFTA_SHIFT (31-29)
  51. #define SDRAM0_TR_RCD_SHIFT (31-31)
  52. #define SDRAM0_RTR_SHIFT (31-15)
  53. #define SDRAM0_ECCCFG_SHIFT (31-11)
  54. /* SDRAM0_CFG enable macro */
  55. #define SDRAM0_CFG_BRPF(x) ( ( x & 0x3)<< SDRAM0_CFG_BRPF_SHIFT )
  56. #define SDRAM0_BXCR_SZ_MASK 0x000e0000
  57. #define SDRAM0_BXCR_AM_MASK 0x0000e000
  58. #define SDRAM0_BXCR_SZ_SHIFT (31-14)
  59. #define SDRAM0_BXCR_AM_SHIFT (31-18)
  60. #define SDRAM0_BXCR_SZ(x) ( (( x << SDRAM0_BXCR_SZ_SHIFT) & SDRAM0_BXCR_SZ_MASK) )
  61. #define SDRAM0_BXCR_AM(x) ( (( x << SDRAM0_BXCR_AM_SHIFT) & SDRAM0_BXCR_AM_MASK) )
  62. #ifdef CONFIG_SPDDRAM_SILENT
  63. # define SPD_ERR(x) do { return 0; } while (0)
  64. #else
  65. # define SPD_ERR(x) do { printf(x); return(0); } while (0)
  66. #endif
  67. #define sdram_HZ_to_ns(hertz) (1000000000/(hertz))
  68. /* function prototypes */
  69. int spd_read(uint addr);
  70. /*
  71. * This function is reading data from the DIMM module EEPROM over the SPD bus
  72. * and uses that to program the sdram controller.
  73. *
  74. * This works on boards that has the same schematics that the AMCC walnut has.
  75. *
  76. * Input: null for default I2C spd functions or a pointer to a custom function
  77. * returning spd_data.
  78. */
  79. long int spd_sdram(int(read_spd)(uint addr))
  80. {
  81. int tmp,row,col;
  82. int total_size,bank_size,bank_code;
  83. int mode;
  84. int bank_cnt;
  85. int sdram0_pmit=0x07c00000;
  86. int sdram0_b0cr;
  87. int sdram0_b1cr = 0;
  88. #ifndef CONFIG_405EP /* not on PPC405EP */
  89. int sdram0_b2cr = 0;
  90. int sdram0_b3cr = 0;
  91. int sdram0_besr0 = -1;
  92. int sdram0_besr1 = -1;
  93. int sdram0_eccesr = -1;
  94. int sdram0_ecccfg;
  95. int ecc_on;
  96. #endif
  97. int sdram0_rtr=0;
  98. int sdram0_tr=0;
  99. int sdram0_cfg=0;
  100. int t_rp;
  101. int t_rcd;
  102. int t_ras;
  103. int t_rc;
  104. int min_cas;
  105. PPC4xx_SYS_INFO sys_info;
  106. unsigned long bus_period_x_10;
  107. /*
  108. * get the board info
  109. */
  110. get_sys_info(&sys_info);
  111. bus_period_x_10 = ONE_BILLION / (sys_info.freqPLB / 10);
  112. if (read_spd == 0){
  113. read_spd=spd_read;
  114. /*
  115. * Make sure I2C controller is initialized
  116. * before continuing.
  117. */
  118. i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
  119. }
  120. /* Make shure we are using SDRAM */
  121. if (read_spd(2) != 0x04) {
  122. SPD_ERR("SDRAM - non SDRAM memory module found\n");
  123. }
  124. /* ------------------------------------------------------------------
  125. * configure memory timing register
  126. *
  127. * data from DIMM:
  128. * 27 IN Row Precharge Time ( t RP)
  129. * 29 MIN RAS to CAS Delay ( t RCD)
  130. * 127 Component and Clock Detail ,clk0-clk3, junction temp, CAS
  131. * -------------------------------------------------------------------*/
  132. /*
  133. * first figure out which cas latency mode to use
  134. * use the min supported mode
  135. */
  136. tmp = read_spd(127) & 0x6;
  137. if (tmp == 0x02) { /* only cas = 2 supported */
  138. min_cas = 2;
  139. /* t_ck = read_spd(9); */
  140. /* t_ac = read_spd(10); */
  141. } else if (tmp == 0x04) { /* only cas = 3 supported */
  142. min_cas = 3;
  143. /* t_ck = read_spd(9); */
  144. /* t_ac = read_spd(10); */
  145. } else if (tmp == 0x06) { /* 2,3 supported, so use 2 */
  146. min_cas = 2;
  147. /* t_ck = read_spd(23); */
  148. /* t_ac = read_spd(24); */
  149. } else {
  150. SPD_ERR("SDRAM - unsupported CAS latency \n");
  151. }
  152. /* get some timing values, t_rp,t_rcd,t_ras,t_rc
  153. */
  154. t_rp = read_spd(27);
  155. t_rcd = read_spd(29);
  156. t_ras = read_spd(30);
  157. t_rc = t_ras + t_rp;
  158. /* The following timing calcs subtract 1 before deviding.
  159. * this has effect of using ceiling instead of floor rounding,
  160. * and also subtracting 1 to convert number to reg value
  161. */
  162. /* set up CASL */
  163. sdram0_tr = (min_cas - 1) << SDRAM0_TR_CASL_SHIFT;
  164. /* set up PTA */
  165. sdram0_tr |= ((((t_rp - 1) * 10)/bus_period_x_10) & 0x3) << SDRAM0_TR_PTA_SHIFT;
  166. /* set up CTP */
  167. tmp = (((t_rc - t_rcd - t_rp -1) * 10) / bus_period_x_10) & 0x3;
  168. if (tmp < 1)
  169. tmp = 1;
  170. sdram0_tr |= tmp << SDRAM0_TR_CTP_SHIFT;
  171. /* set LDF = 2 cycles, reg value = 1 */
  172. sdram0_tr |= 1 << SDRAM0_TR_LDF_SHIFT;
  173. /* set RFTA = t_rfc/bus_period, use t_rfc = t_rc */
  174. tmp = (((t_rc - 1) * 10) / bus_period_x_10) - 3;
  175. if (tmp < 0)
  176. tmp = 0;
  177. if (tmp > 6)
  178. tmp = 6;
  179. sdram0_tr |= tmp << SDRAM0_TR_RFTA_SHIFT;
  180. /* set RCD = t_rcd/bus_period*/
  181. sdram0_tr |= ((((t_rcd - 1) * 10) / bus_period_x_10) &0x3) << SDRAM0_TR_RCD_SHIFT ;
  182. /*------------------------------------------------------------------
  183. * configure RTR register
  184. * -------------------------------------------------------------------*/
  185. row = read_spd(3);
  186. col = read_spd(4);
  187. tmp = read_spd(12) & 0x7f ; /* refresh type less self refresh bit */
  188. switch (tmp) {
  189. case 0x00:
  190. tmp = 15625;
  191. break;
  192. case 0x01:
  193. tmp = 15625 / 4;
  194. break;
  195. case 0x02:
  196. tmp = 15625 / 2;
  197. break;
  198. case 0x03:
  199. tmp = 15625 * 2;
  200. break;
  201. case 0x04:
  202. tmp = 15625 * 4;
  203. break;
  204. case 0x05:
  205. tmp = 15625 * 8;
  206. break;
  207. default:
  208. SPD_ERR("SDRAM - Bad refresh period \n");
  209. }
  210. /* convert from nsec to bus cycles */
  211. tmp = (tmp * 10) / bus_period_x_10;
  212. sdram0_rtr = (tmp & 0x3ff8) << SDRAM0_RTR_SHIFT;
  213. /*------------------------------------------------------------------
  214. * determine the number of banks used
  215. * -------------------------------------------------------------------*/
  216. /* byte 7:6 is module data width */
  217. if (read_spd(7) != 0)
  218. SPD_ERR("SDRAM - unsupported module width\n");
  219. tmp = read_spd(6);
  220. if (tmp < 32)
  221. SPD_ERR("SDRAM - unsupported module width\n");
  222. else if (tmp < 64)
  223. bank_cnt = 1; /* one bank per sdram side */
  224. else if (tmp < 73)
  225. bank_cnt = 2; /* need two banks per side */
  226. else if (tmp < 161)
  227. bank_cnt = 4; /* need four banks per side */
  228. else
  229. SPD_ERR("SDRAM - unsupported module width\n");
  230. /* byte 5 is the module row count (refered to as dimm "sides") */
  231. tmp = read_spd(5);
  232. if (tmp == 1)
  233. ;
  234. else if (tmp==2)
  235. bank_cnt *= 2;
  236. else if (tmp==4)
  237. bank_cnt *= 4;
  238. else
  239. bank_cnt = 8; /* 8 is an error code */
  240. if (bank_cnt > 4) /* we only have 4 banks to work with */
  241. SPD_ERR("SDRAM - unsupported module rows for this width\n");
  242. #ifndef CONFIG_405EP /* not on PPC405EP */
  243. /* now check for ECC ability of module. We only support ECC
  244. * on 32 bit wide devices with 8 bit ECC.
  245. */
  246. if ((read_spd(11)==2) && (read_spd(6)==40) && (read_spd(14)==8)) {
  247. sdram0_ecccfg = 0xf << SDRAM0_ECCCFG_SHIFT;
  248. ecc_on = 1;
  249. } else {
  250. sdram0_ecccfg = 0;
  251. ecc_on = 0;
  252. }
  253. #endif
  254. /*------------------------------------------------------------------
  255. * calculate total size
  256. * -------------------------------------------------------------------*/
  257. /* calculate total size and do sanity check */
  258. tmp = read_spd(31);
  259. total_size = 1 << 22; /* total_size = 4MB */
  260. /* now multiply 4M by the smallest device row density */
  261. /* note that we don't support asymetric rows */
  262. while (((tmp & 0x0001) == 0) && (tmp != 0)) {
  263. total_size = total_size << 1;
  264. tmp = tmp >> 1;
  265. }
  266. total_size *= read_spd(5); /* mult by module rows (dimm sides) */
  267. /*------------------------------------------------------------------
  268. * map rows * cols * banks to a mode
  269. * -------------------------------------------------------------------*/
  270. switch (row) {
  271. case 11:
  272. switch (col) {
  273. case 8:
  274. mode=4; /* mode 5 */
  275. break;
  276. case 9:
  277. case 10:
  278. mode=0; /* mode 1 */
  279. break;
  280. default:
  281. SPD_ERR("SDRAM - unsupported mode\n");
  282. }
  283. break;
  284. case 12:
  285. switch (col) {
  286. case 8:
  287. mode=3; /* mode 4 */
  288. break;
  289. case 9:
  290. case 10:
  291. mode=1; /* mode 2 */
  292. break;
  293. default:
  294. SPD_ERR("SDRAM - unsupported mode\n");
  295. }
  296. break;
  297. case 13:
  298. switch (col) {
  299. case 8:
  300. mode=5; /* mode 6 */
  301. break;
  302. case 9:
  303. case 10:
  304. if (read_spd(17) == 2)
  305. mode = 6; /* mode 7 */
  306. else
  307. mode = 2; /* mode 3 */
  308. break;
  309. case 11:
  310. mode = 2; /* mode 3 */
  311. break;
  312. default:
  313. SPD_ERR("SDRAM - unsupported mode\n");
  314. }
  315. break;
  316. default:
  317. SPD_ERR("SDRAM - unsupported mode\n");
  318. }
  319. /*------------------------------------------------------------------
  320. * using the calculated values, compute the bank
  321. * config register values.
  322. * -------------------------------------------------------------------*/
  323. /* compute the size of each bank */
  324. bank_size = total_size / bank_cnt;
  325. /* convert bank size to bank size code for ppc4xx
  326. by takeing log2(bank_size) - 22 */
  327. tmp = bank_size; /* start with tmp = bank_size */
  328. bank_code = 0; /* and bank_code = 0 */
  329. while (tmp > 1) { /* this takes log2 of tmp */
  330. bank_code++; /* and stores result in bank_code */
  331. tmp = tmp >> 1;
  332. } /* bank_code is now log2(bank_size) */
  333. bank_code -= 22; /* subtract 22 to get the code */
  334. tmp = SDRAM0_BXCR_SZ(bank_code) | SDRAM0_BXCR_AM(mode) | 1;
  335. sdram0_b0cr = (bank_size * 0) | tmp;
  336. #ifndef CONFIG_405EP /* not on PPC405EP */
  337. if (bank_cnt > 1)
  338. sdram0_b2cr = (bank_size * 1) | tmp;
  339. if (bank_cnt > 2)
  340. sdram0_b1cr = (bank_size * 2) | tmp;
  341. if (bank_cnt > 3)
  342. sdram0_b3cr = (bank_size * 3) | tmp;
  343. #else
  344. /* PPC405EP chip only supports two SDRAM banks */
  345. if (bank_cnt > 1)
  346. sdram0_b1cr = (bank_size * 1) | tmp;
  347. if (bank_cnt > 2)
  348. total_size = 2 * bank_size;
  349. #endif
  350. /*
  351. * enable sdram controller DCE=1
  352. * enable burst read prefetch to 32 bytes BRPF=2
  353. * leave other functions off
  354. */
  355. /*------------------------------------------------------------------
  356. * now that we've done our calculations, we are ready to
  357. * program all the registers.
  358. * -------------------------------------------------------------------*/
  359. /* disable memcontroller so updates work */
  360. mtsdram(SDRAM0_CFG, 0);
  361. #ifndef CONFIG_405EP /* not on PPC405EP */
  362. mtsdram(SDRAM0_BESR0, sdram0_besr0);
  363. mtsdram(SDRAM0_BESR1, sdram0_besr1);
  364. mtsdram(SDRAM0_ECCCFG, sdram0_ecccfg);
  365. mtsdram(SDRAM0_ECCESR, sdram0_eccesr);
  366. #endif
  367. mtsdram(SDRAM0_RTR, sdram0_rtr);
  368. mtsdram(SDRAM0_PMIT, sdram0_pmit);
  369. mtsdram(SDRAM0_B0CR, sdram0_b0cr);
  370. mtsdram(SDRAM0_B1CR, sdram0_b1cr);
  371. #ifndef CONFIG_405EP /* not on PPC405EP */
  372. mtsdram(SDRAM0_B2CR, sdram0_b2cr);
  373. mtsdram(SDRAM0_B3CR, sdram0_b3cr);
  374. #endif
  375. mtsdram(SDRAM0_TR, sdram0_tr);
  376. /* SDRAM have a power on delay, 500 micro should do */
  377. udelay(500);
  378. sdram0_cfg = SDRAM0_CFG_DCE | SDRAM0_CFG_BRPF(1) | SDRAM0_CFG_ECCDD | SDRAM0_CFG_EMDULR;
  379. #ifndef CONFIG_405EP /* not on PPC405EP */
  380. if (ecc_on)
  381. sdram0_cfg |= SDRAM0_CFG_MEMCHK;
  382. #endif
  383. mtsdram(SDRAM0_CFG, sdram0_cfg);
  384. return (total_size);
  385. }
  386. int spd_read(uint addr)
  387. {
  388. uchar data[2];
  389. if (i2c_read(SPD_EEPROM_ADDRESS, addr, 1, data, 1) == 0)
  390. return (int)data[0];
  391. else
  392. return 0;
  393. }
  394. #endif /* CONFIG_SPD_EEPROM */