40x_spd_sdram.c 12 KB

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