40x_spd_sdram.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * 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. * See file CREDITS for list of people who contributed to this
  27. * project.
  28. *
  29. * This program is free software; you can redistribute it and/or
  30. * modify it under the terms of the GNU General Public License as
  31. * published by the Free Software Foundation; either version 2 of
  32. * the License, or (at your option) any later version.
  33. *
  34. * This program is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with this program; if not, write to the Free Software
  41. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  42. * MA 02111-1307 USA
  43. */
  44. #include <common.h>
  45. #include <asm/processor.h>
  46. #include <i2c.h>
  47. #include <ppc4xx.h>
  48. #if defined(CONFIG_SPD_EEPROM) && !defined(CONFIG_440)
  49. /*
  50. * Set default values
  51. */
  52. #ifndef CONFIG_SYS_I2C_SPEED
  53. #define CONFIG_SYS_I2C_SPEED 50000
  54. #endif
  55. #define ONE_BILLION 1000000000
  56. #define SDRAM0_CFG_DCE 0x80000000
  57. #define SDRAM0_CFG_SRE 0x40000000
  58. #define SDRAM0_CFG_PME 0x20000000
  59. #define SDRAM0_CFG_MEMCHK 0x10000000
  60. #define SDRAM0_CFG_REGEN 0x08000000
  61. #define SDRAM0_CFG_ECCDD 0x00400000
  62. #define SDRAM0_CFG_EMDULR 0x00200000
  63. #define SDRAM0_CFG_DRW_SHIFT (31-6)
  64. #define SDRAM0_CFG_BRPF_SHIFT (31-8)
  65. #define SDRAM0_TR_CASL_SHIFT (31-8)
  66. #define SDRAM0_TR_PTA_SHIFT (31-13)
  67. #define SDRAM0_TR_CTP_SHIFT (31-15)
  68. #define SDRAM0_TR_LDF_SHIFT (31-17)
  69. #define SDRAM0_TR_RFTA_SHIFT (31-29)
  70. #define SDRAM0_TR_RCD_SHIFT (31-31)
  71. #define SDRAM0_RTR_SHIFT (31-15)
  72. #define SDRAM0_ECCCFG_SHIFT (31-11)
  73. /* SDRAM0_CFG enable macro */
  74. #define SDRAM0_CFG_BRPF(x) ( ( x & 0x3)<< SDRAM0_CFG_BRPF_SHIFT )
  75. #define SDRAM0_BXCR_SZ_MASK 0x000e0000
  76. #define SDRAM0_BXCR_AM_MASK 0x0000e000
  77. #define SDRAM0_BXCR_SZ_SHIFT (31-14)
  78. #define SDRAM0_BXCR_AM_SHIFT (31-18)
  79. #define SDRAM0_BXCR_SZ(x) ( (( x << SDRAM0_BXCR_SZ_SHIFT) & SDRAM0_BXCR_SZ_MASK) )
  80. #define SDRAM0_BXCR_AM(x) ( (( x << SDRAM0_BXCR_AM_SHIFT) & SDRAM0_BXCR_AM_MASK) )
  81. #ifdef CONFIG_SPDDRAM_SILENT
  82. # define SPD_ERR(x) do { return 0; } while (0)
  83. #else
  84. # define SPD_ERR(x) do { printf(x); return(0); } while (0)
  85. #endif
  86. #define sdram_HZ_to_ns(hertz) (1000000000/(hertz))
  87. /* function prototypes */
  88. int spd_read(uint addr);
  89. /*
  90. * This function is reading data from the DIMM module EEPROM over the SPD bus
  91. * and uses that to program the sdram controller.
  92. *
  93. * This works on boards that has the same schematics that the AMCC walnut has.
  94. *
  95. * Input: null for default I2C spd functions or a pointer to a custom function
  96. * returning spd_data.
  97. */
  98. long int spd_sdram(int(read_spd)(uint addr))
  99. {
  100. int tmp,row,col;
  101. int total_size,bank_size,bank_code;
  102. int ecc_on;
  103. int mode;
  104. int bank_cnt;
  105. int sdram0_pmit=0x07c00000;
  106. #ifndef CONFIG_405EP /* not on PPC405EP */
  107. int sdram0_besr0 = -1;
  108. int sdram0_besr1 = -1;
  109. int sdram0_eccesr = -1;
  110. #endif
  111. int sdram0_ecccfg;
  112. int sdram0_rtr=0;
  113. int sdram0_tr=0;
  114. int sdram0_b0cr;
  115. int sdram0_b1cr;
  116. int sdram0_b2cr;
  117. int sdram0_b3cr;
  118. int sdram0_cfg=0;
  119. int t_rp;
  120. int t_rcd;
  121. int t_ras;
  122. int t_rc;
  123. int min_cas;
  124. PPC4xx_SYS_INFO sys_info;
  125. unsigned long bus_period_x_10;
  126. /*
  127. * get the board info
  128. */
  129. get_sys_info(&sys_info);
  130. bus_period_x_10 = ONE_BILLION / (sys_info.freqPLB / 10);
  131. if (read_spd == 0){
  132. read_spd=spd_read;
  133. /*
  134. * Make sure I2C controller is initialized
  135. * before continuing.
  136. */
  137. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  138. }
  139. /* Make shure we are using SDRAM */
  140. if (read_spd(2) != 0x04) {
  141. SPD_ERR("SDRAM - non SDRAM memory module found\n");
  142. }
  143. /* ------------------------------------------------------------------
  144. * configure memory timing register
  145. *
  146. * data from DIMM:
  147. * 27 IN Row Precharge Time ( t RP)
  148. * 29 MIN RAS to CAS Delay ( t RCD)
  149. * 127 Component and Clock Detail ,clk0-clk3, junction temp, CAS
  150. * -------------------------------------------------------------------*/
  151. /*
  152. * first figure out which cas latency mode to use
  153. * use the min supported mode
  154. */
  155. tmp = read_spd(127) & 0x6;
  156. if (tmp == 0x02) { /* only cas = 2 supported */
  157. min_cas = 2;
  158. /* t_ck = read_spd(9); */
  159. /* t_ac = read_spd(10); */
  160. } else if (tmp == 0x04) { /* only cas = 3 supported */
  161. min_cas = 3;
  162. /* t_ck = read_spd(9); */
  163. /* t_ac = read_spd(10); */
  164. } else if (tmp == 0x06) { /* 2,3 supported, so use 2 */
  165. min_cas = 2;
  166. /* t_ck = read_spd(23); */
  167. /* t_ac = read_spd(24); */
  168. } else {
  169. SPD_ERR("SDRAM - unsupported CAS latency \n");
  170. }
  171. /* get some timing values, t_rp,t_rcd,t_ras,t_rc
  172. */
  173. t_rp = read_spd(27);
  174. t_rcd = read_spd(29);
  175. t_ras = read_spd(30);
  176. t_rc = t_ras + t_rp;
  177. /* The following timing calcs subtract 1 before deviding.
  178. * this has effect of using ceiling instead of floor rounding,
  179. * and also subtracting 1 to convert number to reg value
  180. */
  181. /* set up CASL */
  182. sdram0_tr = (min_cas - 1) << SDRAM0_TR_CASL_SHIFT;
  183. /* set up PTA */
  184. sdram0_tr |= ((((t_rp - 1) * 10)/bus_period_x_10) & 0x3) << SDRAM0_TR_PTA_SHIFT;
  185. /* set up CTP */
  186. tmp = (((t_rc - t_rcd - t_rp -1) * 10) / bus_period_x_10) & 0x3;
  187. if (tmp < 1)
  188. tmp = 1;
  189. sdram0_tr |= tmp << SDRAM0_TR_CTP_SHIFT;
  190. /* set LDF = 2 cycles, reg value = 1 */
  191. sdram0_tr |= 1 << SDRAM0_TR_LDF_SHIFT;
  192. /* set RFTA = t_rfc/bus_period, use t_rfc = t_rc */
  193. tmp = (((t_rc - 1) * 10) / bus_period_x_10) - 3;
  194. if (tmp < 0)
  195. tmp = 0;
  196. if (tmp > 6)
  197. tmp = 6;
  198. sdram0_tr |= tmp << SDRAM0_TR_RFTA_SHIFT;
  199. /* set RCD = t_rcd/bus_period*/
  200. sdram0_tr |= ((((t_rcd - 1) * 10) / bus_period_x_10) &0x3) << SDRAM0_TR_RCD_SHIFT ;
  201. /*------------------------------------------------------------------
  202. * configure RTR register
  203. * -------------------------------------------------------------------*/
  204. row = read_spd(3);
  205. col = read_spd(4);
  206. tmp = read_spd(12) & 0x7f ; /* refresh type less self refresh bit */
  207. switch (tmp) {
  208. case 0x00:
  209. tmp = 15625;
  210. break;
  211. case 0x01:
  212. tmp = 15625 / 4;
  213. break;
  214. case 0x02:
  215. tmp = 15625 / 2;
  216. break;
  217. case 0x03:
  218. tmp = 15625 * 2;
  219. break;
  220. case 0x04:
  221. tmp = 15625 * 4;
  222. break;
  223. case 0x05:
  224. tmp = 15625 * 8;
  225. break;
  226. default:
  227. SPD_ERR("SDRAM - Bad refresh period \n");
  228. }
  229. /* convert from nsec to bus cycles */
  230. tmp = (tmp * 10) / bus_period_x_10;
  231. sdram0_rtr = (tmp & 0x3ff8) << SDRAM0_RTR_SHIFT;
  232. /*------------------------------------------------------------------
  233. * determine the number of banks used
  234. * -------------------------------------------------------------------*/
  235. /* byte 7:6 is module data width */
  236. if (read_spd(7) != 0)
  237. SPD_ERR("SDRAM - unsupported module width\n");
  238. tmp = read_spd(6);
  239. if (tmp < 32)
  240. SPD_ERR("SDRAM - unsupported module width\n");
  241. else if (tmp < 64)
  242. bank_cnt = 1; /* one bank per sdram side */
  243. else if (tmp < 73)
  244. bank_cnt = 2; /* need two banks per side */
  245. else if (tmp < 161)
  246. bank_cnt = 4; /* need four banks per side */
  247. else
  248. SPD_ERR("SDRAM - unsupported module width\n");
  249. /* byte 5 is the module row count (refered to as dimm "sides") */
  250. tmp = read_spd(5);
  251. if (tmp == 1)
  252. ;
  253. else if (tmp==2)
  254. bank_cnt *= 2;
  255. else if (tmp==4)
  256. bank_cnt *= 4;
  257. else
  258. bank_cnt = 8; /* 8 is an error code */
  259. if (bank_cnt > 4) /* we only have 4 banks to work with */
  260. SPD_ERR("SDRAM - unsupported module rows for this width\n");
  261. /* now check for ECC ability of module. We only support ECC
  262. * on 32 bit wide devices with 8 bit ECC.
  263. */
  264. if ((read_spd(11)==2) && (read_spd(6)==40) && (read_spd(14)==8)) {
  265. sdram0_ecccfg = 0xf << SDRAM0_ECCCFG_SHIFT;
  266. ecc_on = 1;
  267. } else {
  268. sdram0_ecccfg = 0;
  269. ecc_on = 0;
  270. }
  271. /*------------------------------------------------------------------
  272. * calculate total size
  273. * -------------------------------------------------------------------*/
  274. /* calculate total size and do sanity check */
  275. tmp = read_spd(31);
  276. total_size = 1 << 22; /* total_size = 4MB */
  277. /* now multiply 4M by the smallest device row density */
  278. /* note that we don't support asymetric rows */
  279. while (((tmp & 0x0001) == 0) && (tmp != 0)) {
  280. total_size = total_size << 1;
  281. tmp = tmp >> 1;
  282. }
  283. total_size *= read_spd(5); /* mult by module rows (dimm sides) */
  284. /*------------------------------------------------------------------
  285. * map rows * cols * banks to a mode
  286. * -------------------------------------------------------------------*/
  287. switch (row) {
  288. case 11:
  289. switch (col) {
  290. case 8:
  291. mode=4; /* mode 5 */
  292. break;
  293. case 9:
  294. case 10:
  295. mode=0; /* mode 1 */
  296. break;
  297. default:
  298. SPD_ERR("SDRAM - unsupported mode\n");
  299. }
  300. break;
  301. case 12:
  302. switch (col) {
  303. case 8:
  304. mode=3; /* mode 4 */
  305. break;
  306. case 9:
  307. case 10:
  308. mode=1; /* mode 2 */
  309. break;
  310. default:
  311. SPD_ERR("SDRAM - unsupported mode\n");
  312. }
  313. break;
  314. case 13:
  315. switch (col) {
  316. case 8:
  317. mode=5; /* mode 6 */
  318. break;
  319. case 9:
  320. case 10:
  321. if (read_spd(17) == 2)
  322. mode = 6; /* mode 7 */
  323. else
  324. mode = 2; /* mode 3 */
  325. break;
  326. case 11:
  327. mode = 2; /* mode 3 */
  328. break;
  329. default:
  330. SPD_ERR("SDRAM - unsupported mode\n");
  331. }
  332. break;
  333. default:
  334. SPD_ERR("SDRAM - unsupported mode\n");
  335. }
  336. /*------------------------------------------------------------------
  337. * using the calculated values, compute the bank
  338. * config register values.
  339. * -------------------------------------------------------------------*/
  340. sdram0_b1cr = 0;
  341. sdram0_b2cr = 0;
  342. sdram0_b3cr = 0;
  343. /* compute the size of each bank */
  344. bank_size = total_size / bank_cnt;
  345. /* convert bank size to bank size code for ppc4xx
  346. by takeing log2(bank_size) - 22 */
  347. tmp = bank_size; /* start with tmp = bank_size */
  348. bank_code = 0; /* and bank_code = 0 */
  349. while (tmp > 1) { /* this takes log2 of tmp */
  350. bank_code++; /* and stores result in bank_code */
  351. tmp = tmp >> 1;
  352. } /* bank_code is now log2(bank_size) */
  353. bank_code -= 22; /* subtract 22 to get the code */
  354. tmp = SDRAM0_BXCR_SZ(bank_code) | SDRAM0_BXCR_AM(mode) | 1;
  355. sdram0_b0cr = (bank_size * 0) | tmp;
  356. #ifndef CONFIG_405EP /* not on PPC405EP */
  357. if (bank_cnt > 1)
  358. sdram0_b2cr = (bank_size * 1) | tmp;
  359. if (bank_cnt > 2)
  360. sdram0_b1cr = (bank_size * 2) | tmp;
  361. if (bank_cnt > 3)
  362. sdram0_b3cr = (bank_size * 3) | tmp;
  363. #else
  364. /* PPC405EP chip only supports two SDRAM banks */
  365. if (bank_cnt > 1)
  366. sdram0_b1cr = (bank_size * 1) | tmp;
  367. if (bank_cnt > 2)
  368. total_size = 2 * bank_size;
  369. #endif
  370. /*
  371. * enable sdram controller DCE=1
  372. * enable burst read prefetch to 32 bytes BRPF=2
  373. * leave other functions off
  374. */
  375. /*------------------------------------------------------------------
  376. * now that we've done our calculations, we are ready to
  377. * program all the registers.
  378. * -------------------------------------------------------------------*/
  379. #define mtsdram0(reg, data) mtdcr(memcfga,reg);mtdcr(memcfgd,data)
  380. /* disable memcontroller so updates work */
  381. mtsdram0( mem_mcopt1, 0 );
  382. #ifndef CONFIG_405EP /* not on PPC405EP */
  383. mtsdram0( mem_besra , sdram0_besr0 );
  384. mtsdram0( mem_besrb , sdram0_besr1 );
  385. mtsdram0( mem_ecccf , sdram0_ecccfg );
  386. mtsdram0( mem_eccerr, sdram0_eccesr );
  387. #endif
  388. mtsdram0( mem_rtr , sdram0_rtr );
  389. mtsdram0( mem_pmit , sdram0_pmit );
  390. mtsdram0( mem_mb0cf , sdram0_b0cr );
  391. mtsdram0( mem_mb1cf , sdram0_b1cr );
  392. #ifndef CONFIG_405EP /* not on PPC405EP */
  393. mtsdram0( mem_mb2cf , sdram0_b2cr );
  394. mtsdram0( mem_mb3cf , sdram0_b3cr );
  395. #endif
  396. mtsdram0( mem_sdtr1 , sdram0_tr );
  397. /* SDRAM have a power on delay, 500 micro should do */
  398. udelay(500);
  399. sdram0_cfg = SDRAM0_CFG_DCE | SDRAM0_CFG_BRPF(1) | SDRAM0_CFG_ECCDD | SDRAM0_CFG_EMDULR;
  400. if (ecc_on)
  401. sdram0_cfg |= SDRAM0_CFG_MEMCHK;
  402. mtsdram0(mem_mcopt1, sdram0_cfg);
  403. return (total_size);
  404. }
  405. int spd_read(uint addr)
  406. {
  407. uchar data[2];
  408. if (i2c_read(SPD_EEPROM_ADDRESS, addr, 1, data, 1) == 0)
  409. return (int)data[0];
  410. else
  411. return 0;
  412. }
  413. #endif /* CONFIG_SPD_EEPROM */