spd_sdram.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /*
  2. * Copyright 2004, 2007 Freescale Semiconductor.
  3. * (C) Copyright 2003 Motorola Inc.
  4. * Xianghua Xiao (X.Xiao@motorola.com)
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <asm/processor.h>
  26. #include <i2c.h>
  27. #include <spd.h>
  28. #include <asm/mmu.h>
  29. #include <asm/fsl_law.h>
  30. #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  31. extern void dma_init(void);
  32. extern uint dma_check(void);
  33. extern int dma_xfer(void *dest, uint count, void *src);
  34. #endif
  35. #ifdef CONFIG_SPD_EEPROM
  36. #ifndef CFG_READ_SPD
  37. #define CFG_READ_SPD i2c_read
  38. #endif
  39. static unsigned int setup_laws_and_tlbs(unsigned int memsize);
  40. /*
  41. * Convert picoseconds into clock cycles (rounding up if needed).
  42. */
  43. int
  44. picos_to_clk(int picos)
  45. {
  46. int clks;
  47. clks = picos / (2000000000 / (get_ddr_freq(0) / 1000));
  48. if (picos % (2000000000 / (get_ddr_freq(0) / 1000)) != 0) {
  49. clks++;
  50. }
  51. return clks;
  52. }
  53. /*
  54. * Calculate the Density of each Physical Rank.
  55. * Returned size is in bytes.
  56. *
  57. * Study these table from Byte 31 of JEDEC SPD Spec.
  58. *
  59. * DDR I DDR II
  60. * Bit Size Size
  61. * --- ----- ------
  62. * 7 high 512MB 512MB
  63. * 6 256MB 256MB
  64. * 5 128MB 128MB
  65. * 4 64MB 16GB
  66. * 3 32MB 8GB
  67. * 2 16MB 4GB
  68. * 1 2GB 2GB
  69. * 0 low 1GB 1GB
  70. *
  71. * Reorder Table to be linear by stripping the bottom
  72. * 2 or 5 bits off and shifting them up to the top.
  73. */
  74. unsigned int
  75. compute_banksize(unsigned int mem_type, unsigned char row_dens)
  76. {
  77. unsigned int bsize;
  78. if (mem_type == SPD_MEMTYPE_DDR) {
  79. /* Bottom 2 bits up to the top. */
  80. bsize = ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
  81. debug("DDR: DDR I rank density = 0x%08x\n", bsize);
  82. } else {
  83. /* Bottom 5 bits up to the top. */
  84. bsize = ((row_dens >> 5) | ((row_dens & 31) << 3)) << 27;
  85. debug("DDR: DDR II rank density = 0x%08x\n", bsize);
  86. }
  87. return bsize;
  88. }
  89. /*
  90. * Convert a two-nibble BCD value into a cycle time.
  91. * While the spec calls for nano-seconds, picos are returned.
  92. *
  93. * This implements the tables for bytes 9, 23 and 25 for both
  94. * DDR I and II. No allowance for distinguishing the invalid
  95. * fields absent for DDR I yet present in DDR II is made.
  96. * (That is, cycle times of .25, .33, .66 and .75 ns are
  97. * allowed for both DDR II and I.)
  98. */
  99. unsigned int
  100. convert_bcd_tenths_to_cycle_time_ps(unsigned int spd_val)
  101. {
  102. /*
  103. * Table look up the lower nibble, allow DDR I & II.
  104. */
  105. unsigned int tenths_ps[16] = {
  106. 0,
  107. 100,
  108. 200,
  109. 300,
  110. 400,
  111. 500,
  112. 600,
  113. 700,
  114. 800,
  115. 900,
  116. 250,
  117. 330,
  118. 660,
  119. 750,
  120. 0, /* undefined */
  121. 0 /* undefined */
  122. };
  123. unsigned int whole_ns = (spd_val & 0xF0) >> 4;
  124. unsigned int tenth_ns = spd_val & 0x0F;
  125. unsigned int ps = whole_ns * 1000 + tenths_ps[tenth_ns];
  126. return ps;
  127. }
  128. /*
  129. * Determine Refresh Rate. Ignore self refresh bit on DDR I.
  130. * Table from SPD Spec, Byte 12, converted to picoseconds and
  131. * filled in with "default" normal values.
  132. */
  133. unsigned int determine_refresh_rate(unsigned int spd_refresh)
  134. {
  135. unsigned int refresh_time_ns[8] = {
  136. 15625000, /* 0 Normal 1.00x */
  137. 3900000, /* 1 Reduced .25x */
  138. 7800000, /* 2 Extended .50x */
  139. 31300000, /* 3 Extended 2.00x */
  140. 62500000, /* 4 Extended 4.00x */
  141. 125000000, /* 5 Extended 8.00x */
  142. 15625000, /* 6 Normal 1.00x filler */
  143. 15625000, /* 7 Normal 1.00x filler */
  144. };
  145. return picos_to_clk(refresh_time_ns[spd_refresh & 0x7]);
  146. }
  147. long int
  148. spd_sdram(void)
  149. {
  150. volatile ccsr_ddr_t *ddr = (void *)(CFG_MPC85xx_DDR_ADDR);
  151. spd_eeprom_t spd;
  152. unsigned int n_ranks;
  153. unsigned int rank_density;
  154. unsigned int odt_rd_cfg, odt_wr_cfg, ba_bits;
  155. unsigned int odt_cfg, mode_odt_enable;
  156. unsigned int refresh_clk;
  157. #ifdef MPC85xx_DDR_SDRAM_CLK_CNTL
  158. unsigned char clk_adjust;
  159. #endif
  160. unsigned int dqs_cfg;
  161. unsigned char twr_clk, twtr_clk, twr_auto_clk;
  162. unsigned int tCKmin_ps, tCKmax_ps;
  163. unsigned int max_data_rate, effective_data_rate;
  164. unsigned int busfreq;
  165. unsigned sdram_cfg;
  166. unsigned int memsize = 0;
  167. unsigned char caslat, caslat_ctrl;
  168. unsigned int trfc, trfc_clk, trfc_low, trfc_high;
  169. unsigned int trcd_clk;
  170. unsigned int trtp_clk;
  171. unsigned char cke_min_clk;
  172. unsigned char add_lat;
  173. unsigned char wr_lat;
  174. unsigned char wr_data_delay;
  175. unsigned char four_act;
  176. unsigned char cpo;
  177. unsigned char burst_len;
  178. unsigned int mode_caslat;
  179. unsigned char sdram_type;
  180. unsigned char d_init;
  181. unsigned int bnds;
  182. /*
  183. * Skip configuration if already configured.
  184. * memsize is determined from last configured chip select.
  185. */
  186. if (ddr->cs0_config & 0x80000000) {
  187. debug(" cs0 already configured, bnds=%x\n",ddr->cs0_bnds);
  188. bnds = 0xfff & ddr->cs0_bnds;
  189. if (bnds < 0xff) { /* do not add if at top of 4G */
  190. memsize = (bnds + 1) << 4;
  191. }
  192. }
  193. if (ddr->cs1_config & 0x80000000) {
  194. debug(" cs1 already configured, bnds=%x\n",ddr->cs1_bnds);
  195. bnds = 0xfff & ddr->cs1_bnds;
  196. if (bnds < 0xff) { /* do not add if at top of 4G */
  197. memsize = (bnds + 1) << 4; /* assume ordered bnds */
  198. }
  199. }
  200. if (ddr->cs2_config & 0x80000000) {
  201. debug(" cs2 already configured, bnds=%x\n",ddr->cs2_bnds);
  202. bnds = 0xfff & ddr->cs2_bnds;
  203. if (bnds < 0xff) { /* do not add if at top of 4G */
  204. memsize = (bnds + 1) << 4;
  205. }
  206. }
  207. if (ddr->cs3_config & 0x80000000) {
  208. debug(" cs3 already configured, bnds=%x\n",ddr->cs3_bnds);
  209. bnds = 0xfff & ddr->cs3_bnds;
  210. if (bnds < 0xff) { /* do not add if at top of 4G */
  211. memsize = (bnds + 1) << 4;
  212. }
  213. }
  214. if (memsize) {
  215. printf(" Reusing current %dMB configuration\n",memsize);
  216. memsize = setup_laws_and_tlbs(memsize);
  217. return memsize << 20;
  218. }
  219. /*
  220. * Read SPD information.
  221. */
  222. CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) &spd, sizeof(spd));
  223. /*
  224. * Check for supported memory module types.
  225. */
  226. if (spd.mem_type != SPD_MEMTYPE_DDR &&
  227. spd.mem_type != SPD_MEMTYPE_DDR2) {
  228. printf("Unable to locate DDR I or DDR II module.\n"
  229. " Fundamental memory type is 0x%0x\n",
  230. spd.mem_type);
  231. return 0;
  232. }
  233. /*
  234. * These test gloss over DDR I and II differences in interpretation
  235. * of bytes 3 and 4, but irrelevantly. Multiple asymmetric banks
  236. * are not supported on DDR I; and not encoded on DDR II.
  237. *
  238. * Also note that the 8548 controller can support:
  239. * 12 <= nrow <= 16
  240. * and
  241. * 8 <= ncol <= 11 (still, for DDR)
  242. * 6 <= ncol <= 9 (for FCRAM)
  243. */
  244. if (spd.nrow_addr < 12 || spd.nrow_addr > 14) {
  245. printf("DDR: Unsupported number of Row Addr lines: %d.\n",
  246. spd.nrow_addr);
  247. return 0;
  248. }
  249. if (spd.ncol_addr < 8 || spd.ncol_addr > 11) {
  250. printf("DDR: Unsupported number of Column Addr lines: %d.\n",
  251. spd.ncol_addr);
  252. return 0;
  253. }
  254. /*
  255. * Determine the number of physical banks controlled by
  256. * different Chip Select signals. This is not quite the
  257. * same as the number of DIMM modules on the board. Feh.
  258. */
  259. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  260. n_ranks = spd.nrows;
  261. } else {
  262. n_ranks = (spd.nrows & 0x7) + 1;
  263. }
  264. debug("DDR: number of ranks = %d\n", n_ranks);
  265. if (n_ranks > 2) {
  266. printf("DDR: Only 2 chip selects are supported: %d\n",
  267. n_ranks);
  268. return 0;
  269. }
  270. #ifdef CONFIG_MPC8548
  271. /*
  272. * Adjust DDR II IO voltage biasing.
  273. * Only 8548 rev 1 needs the fix
  274. */
  275. if ((SVR_SOC_VER(get_svr()) == SVR_8548_E) &&
  276. (SVR_MJREV(get_svr()) == 1) &&
  277. (spd.mem_type == SPD_MEMTYPE_DDR2)) {
  278. volatile ccsr_gur_t *gur = (void *)(CFG_MPC85xx_GUTS_ADDR);
  279. gur->ddrioovcr = (0x80000000 /* Enable */
  280. | 0x10000000);/* VSEL to 1.8V */
  281. }
  282. #endif
  283. /*
  284. * Determine the size of each Rank in bytes.
  285. */
  286. rank_density = compute_banksize(spd.mem_type, spd.row_dens);
  287. /*
  288. * Eg: Bounds: 0x0000_0000 to 0x0f000_0000 first 256 Meg
  289. */
  290. ddr->cs0_bnds = (rank_density >> 24) - 1;
  291. /*
  292. * ODT configuration recommendation from DDR Controller Chapter.
  293. */
  294. odt_rd_cfg = 0; /* Never assert ODT */
  295. odt_wr_cfg = 0; /* Never assert ODT */
  296. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  297. odt_wr_cfg = 1; /* Assert ODT on writes to CS0 */
  298. #if 0
  299. /* FIXME: How to determine the number of dimm modules? */
  300. if (n_dimm_modules == 2) {
  301. odt_rd_cfg = 1; /* Assert ODT on reads to CS0 */
  302. }
  303. #endif
  304. }
  305. ba_bits = 0;
  306. if (spd.nbanks == 0x8)
  307. ba_bits = 1;
  308. ddr->cs0_config = ( 1 << 31
  309. | (odt_rd_cfg << 20)
  310. | (odt_wr_cfg << 16)
  311. | (ba_bits << 14)
  312. | (spd.nrow_addr - 12) << 8
  313. | (spd.ncol_addr - 8) );
  314. debug("\n");
  315. debug("DDR: cs0_bnds = 0x%08x\n", ddr->cs0_bnds);
  316. debug("DDR: cs0_config = 0x%08x\n", ddr->cs0_config);
  317. if (n_ranks == 2) {
  318. /*
  319. * Eg: Bounds: 0x0f00_0000 to 0x1e0000_0000, second 256 Meg
  320. */
  321. ddr->cs1_bnds = ( (rank_density >> 8)
  322. | ((rank_density >> (24 - 1)) - 1) );
  323. ddr->cs1_config = ( 1<<31
  324. | (odt_rd_cfg << 20)
  325. | (odt_wr_cfg << 16)
  326. | (spd.nrow_addr - 12) << 8
  327. | (spd.ncol_addr - 8) );
  328. debug("DDR: cs1_bnds = 0x%08x\n", ddr->cs1_bnds);
  329. debug("DDR: cs1_config = 0x%08x\n", ddr->cs1_config);
  330. }
  331. /*
  332. * Find the largest CAS by locating the highest 1 bit
  333. * in the spd.cas_lat field. Translate it to a DDR
  334. * controller field value:
  335. *
  336. * CAS Lat DDR I DDR II Ctrl
  337. * Clocks SPD Bit SPD Bit Value
  338. * ------- ------- ------- -----
  339. * 1.0 0 0001
  340. * 1.5 1 0010
  341. * 2.0 2 2 0011
  342. * 2.5 3 0100
  343. * 3.0 4 3 0101
  344. * 3.5 5 0110
  345. * 4.0 4 0111
  346. * 4.5 1000
  347. * 5.0 5 1001
  348. */
  349. caslat = __ilog2(spd.cas_lat);
  350. if ((spd.mem_type == SPD_MEMTYPE_DDR)
  351. && (caslat > 5)) {
  352. printf("DDR I: Invalid SPD CAS Latency: 0x%x.\n", spd.cas_lat);
  353. return 0;
  354. } else if (spd.mem_type == SPD_MEMTYPE_DDR2
  355. && (caslat < 2 || caslat > 5)) {
  356. printf("DDR II: Invalid SPD CAS Latency: 0x%x.\n",
  357. spd.cas_lat);
  358. return 0;
  359. }
  360. debug("DDR: caslat SPD bit is %d\n", caslat);
  361. /*
  362. * Calculate the Maximum Data Rate based on the Minimum Cycle time.
  363. * The SPD clk_cycle field (tCKmin) is measured in tenths of
  364. * nanoseconds and represented as BCD.
  365. */
  366. tCKmin_ps = convert_bcd_tenths_to_cycle_time_ps(spd.clk_cycle);
  367. debug("DDR: tCKmin = %d ps\n", tCKmin_ps);
  368. /*
  369. * Double-data rate, scaled 1000 to picoseconds, and back down to MHz.
  370. */
  371. max_data_rate = 2 * 1000 * 1000 / tCKmin_ps;
  372. debug("DDR: Module max data rate = %d Mhz\n", max_data_rate);
  373. /*
  374. * Adjust the CAS Latency to allow for bus speeds that
  375. * are slower than the DDR module.
  376. */
  377. busfreq = get_ddr_freq(0) / 1000000; /* MHz */
  378. effective_data_rate = max_data_rate;
  379. if (busfreq < 90) {
  380. /* DDR rate out-of-range */
  381. puts("DDR: platform frequency is not fit for DDR rate\n");
  382. return 0;
  383. } else if (90 <= busfreq && busfreq < 230 && max_data_rate >= 230) {
  384. /*
  385. * busfreq 90~230 range, treated as DDR 200.
  386. */
  387. effective_data_rate = 200;
  388. if (spd.clk_cycle3 == 0xa0) /* 10 ns */
  389. caslat -= 2;
  390. else if (spd.clk_cycle2 == 0xa0)
  391. caslat--;
  392. } else if (230 <= busfreq && busfreq < 280 && max_data_rate >= 280) {
  393. /*
  394. * busfreq 230~280 range, treated as DDR 266.
  395. */
  396. effective_data_rate = 266;
  397. if (spd.clk_cycle3 == 0x75) /* 7.5 ns */
  398. caslat -= 2;
  399. else if (spd.clk_cycle2 == 0x75)
  400. caslat--;
  401. } else if (280 <= busfreq && busfreq < 350 && max_data_rate >= 350) {
  402. /*
  403. * busfreq 280~350 range, treated as DDR 333.
  404. */
  405. effective_data_rate = 333;
  406. if (spd.clk_cycle3 == 0x60) /* 6.0 ns */
  407. caslat -= 2;
  408. else if (spd.clk_cycle2 == 0x60)
  409. caslat--;
  410. } else if (350 <= busfreq && busfreq < 460 && max_data_rate >= 460) {
  411. /*
  412. * busfreq 350~460 range, treated as DDR 400.
  413. */
  414. effective_data_rate = 400;
  415. if (spd.clk_cycle3 == 0x50) /* 5.0 ns */
  416. caslat -= 2;
  417. else if (spd.clk_cycle2 == 0x50)
  418. caslat--;
  419. } else if (460 <= busfreq && busfreq < 560 && max_data_rate >= 560) {
  420. /*
  421. * busfreq 460~560 range, treated as DDR 533.
  422. */
  423. effective_data_rate = 533;
  424. if (spd.clk_cycle3 == 0x3D) /* 3.75 ns */
  425. caslat -= 2;
  426. else if (spd.clk_cycle2 == 0x3D)
  427. caslat--;
  428. } else if (560 <= busfreq && busfreq < 700 && max_data_rate >= 700) {
  429. /*
  430. * busfreq 560~700 range, treated as DDR 667.
  431. */
  432. effective_data_rate = 667;
  433. if (spd.clk_cycle3 == 0x30) /* 3.0 ns */
  434. caslat -= 2;
  435. else if (spd.clk_cycle2 == 0x30)
  436. caslat--;
  437. } else if (700 <= busfreq) {
  438. /*
  439. * DDR rate out-of-range
  440. */
  441. printf("DDR: Bus freq %d MHz is not fit for DDR rate %d MHz\n",
  442. busfreq, max_data_rate);
  443. return 0;
  444. }
  445. /*
  446. * Convert caslat clocks to DDR controller value.
  447. * Force caslat_ctrl to be DDR Controller field-sized.
  448. */
  449. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  450. caslat_ctrl = (caslat + 1) & 0x07;
  451. } else {
  452. caslat_ctrl = (2 * caslat - 1) & 0x0f;
  453. }
  454. debug("DDR: effective data rate is %d MHz\n", effective_data_rate);
  455. debug("DDR: caslat SPD bit is %d, controller field is 0x%x\n",
  456. caslat, caslat_ctrl);
  457. /*
  458. * Timing Config 0.
  459. * Avoid writing for DDR I. The new PQ38 DDR controller
  460. * dreams up non-zero default values to be backwards compatible.
  461. */
  462. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  463. unsigned char taxpd_clk = 8; /* By the book. */
  464. unsigned char tmrd_clk = 2; /* By the book. */
  465. unsigned char act_pd_exit = 2; /* Empirical? */
  466. unsigned char pre_pd_exit = 6; /* Empirical? */
  467. ddr->timing_cfg_0 = (0
  468. | ((act_pd_exit & 0x7) << 20) /* ACT_PD_EXIT */
  469. | ((pre_pd_exit & 0x7) << 16) /* PRE_PD_EXIT */
  470. | ((taxpd_clk & 0xf) << 8) /* ODT_PD_EXIT */
  471. | ((tmrd_clk & 0xf) << 0) /* MRS_CYC */
  472. );
  473. #if 0
  474. ddr->timing_cfg_0 |= 0xaa000000; /* extra cycles */
  475. #endif
  476. debug("DDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
  477. } else {
  478. #if 0
  479. /*
  480. * Force extra cycles with 0xaa bits.
  481. * Incidentally supply the dreamt-up backwards compat value!
  482. */
  483. ddr->timing_cfg_0 = 0x00110105; /* backwards compat value */
  484. ddr->timing_cfg_0 |= 0xaa000000; /* extra cycles */
  485. debug("DDR: HACK timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
  486. #endif
  487. }
  488. /*
  489. * Some Timing Config 1 values now.
  490. * Sneak Extended Refresh Recovery in here too.
  491. */
  492. /*
  493. * For DDR I, WRREC(Twr) and WRTORD(Twtr) are not in SPD,
  494. * use conservative value.
  495. * For DDR II, they are bytes 36 and 37, in quarter nanos.
  496. */
  497. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  498. twr_clk = 3; /* Clocks */
  499. twtr_clk = 1; /* Clocks */
  500. } else {
  501. twr_clk = picos_to_clk(spd.twr * 250);
  502. twtr_clk = picos_to_clk(spd.twtr * 250);
  503. }
  504. /*
  505. * Calculate Trfc, in picos.
  506. * DDR I: Byte 42 straight up in ns.
  507. * DDR II: Byte 40 and 42 swizzled some, in ns.
  508. */
  509. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  510. trfc = spd.trfc * 1000; /* up to ps */
  511. } else {
  512. unsigned int byte40_table_ps[8] = {
  513. 0,
  514. 250,
  515. 330,
  516. 500,
  517. 660,
  518. 750,
  519. 0,
  520. 0
  521. };
  522. trfc = (((spd.trctrfc_ext & 0x1) * 256) + spd.trfc) * 1000
  523. + byte40_table_ps[(spd.trctrfc_ext >> 1) & 0x7];
  524. }
  525. trfc_clk = picos_to_clk(trfc);
  526. /*
  527. * Trcd, Byte 29, from quarter nanos to ps and clocks.
  528. */
  529. trcd_clk = picos_to_clk(spd.trcd * 250) & 0x7;
  530. /*
  531. * Convert trfc_clk to DDR controller fields. DDR I should
  532. * fit in the REFREC field (16-19) of TIMING_CFG_1, but the
  533. * 8548 controller has an extended REFREC field of three bits.
  534. * The controller automatically adds 8 clocks to this value,
  535. * so preadjust it down 8 first before splitting it up.
  536. */
  537. trfc_low = (trfc_clk - 8) & 0xf;
  538. trfc_high = ((trfc_clk - 8) >> 4) & 0x3;
  539. /*
  540. * Sneak in some Extended Refresh Recovery.
  541. */
  542. ddr->ext_refrec = (trfc_high << 16);
  543. debug("DDR: ext_refrec = 0x%08x\n", ddr->ext_refrec);
  544. ddr->timing_cfg_1 =
  545. (0
  546. | ((picos_to_clk(spd.trp * 250) & 0x07) << 28) /* PRETOACT */
  547. | ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24) /* ACTTOPRE */
  548. | (trcd_clk << 20) /* ACTTORW */
  549. | (caslat_ctrl << 16) /* CASLAT */
  550. | (trfc_low << 12) /* REFEC */
  551. | ((twr_clk & 0x07) << 8) /* WRRREC */
  552. | ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) /* ACTTOACT */
  553. | ((twtr_clk & 0x07) << 0) /* WRTORD */
  554. );
  555. debug("DDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
  556. /*
  557. * Timing_Config_2
  558. * Was: 0x00000800;
  559. */
  560. /*
  561. * Additive Latency
  562. * For DDR I, 0.
  563. * For DDR II, with ODT enabled, use "a value" less than ACTTORW,
  564. * which comes from Trcd, and also note that:
  565. * add_lat + caslat must be >= 4
  566. */
  567. add_lat = 0;
  568. if (spd.mem_type == SPD_MEMTYPE_DDR2
  569. && (odt_wr_cfg || odt_rd_cfg)
  570. && (caslat < 4)) {
  571. add_lat = 4 - caslat;
  572. if (add_lat > trcd_clk) {
  573. add_lat = trcd_clk - 1;
  574. }
  575. }
  576. /*
  577. * Write Data Delay
  578. * Historically 0x2 == 4/8 clock delay.
  579. * Empirically, 0x3 == 6/8 clock delay is suggested for DDR I 266.
  580. */
  581. wr_data_delay = 3;
  582. /*
  583. * Write Latency
  584. * Read to Precharge
  585. * Minimum CKE Pulse Width.
  586. * Four Activate Window
  587. */
  588. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  589. /*
  590. * This is a lie. It should really be 1, but if it is
  591. * set to 1, bits overlap into the old controller's
  592. * otherwise unused ACSM field. If we leave it 0, then
  593. * the HW will magically treat it as 1 for DDR 1. Oh Yea.
  594. */
  595. wr_lat = 0;
  596. trtp_clk = 2; /* By the book. */
  597. cke_min_clk = 1; /* By the book. */
  598. four_act = 1; /* By the book. */
  599. } else {
  600. wr_lat = caslat - 1;
  601. /* Convert SPD value from quarter nanos to picos. */
  602. trtp_clk = picos_to_clk(spd.trtp * 250);
  603. cke_min_clk = 3; /* By the book. */
  604. four_act = picos_to_clk(37500); /* By the book. 1k pages? */
  605. }
  606. /*
  607. * Empirically set ~MCAS-to-preamble override for DDR 2.
  608. * Your milage will vary.
  609. */
  610. cpo = 0;
  611. if (spd.mem_type == SPD_MEMTYPE_DDR2) {
  612. if (effective_data_rate <= 333) {
  613. cpo = 0x7; /* READ_LAT + 5/4 */
  614. } else {
  615. cpo = 0x9; /* READ_LAT + 7/4 */
  616. }
  617. }
  618. ddr->timing_cfg_2 = (0
  619. | ((add_lat & 0x7) << 28) /* ADD_LAT */
  620. | ((cpo & 0x1f) << 23) /* CPO */
  621. | ((wr_lat & 0x7) << 19) /* WR_LAT */
  622. | ((trtp_clk & 0x7) << 13) /* RD_TO_PRE */
  623. | ((wr_data_delay & 0x7) << 10) /* WR_DATA_DELAY */
  624. | ((cke_min_clk & 0x7) << 6) /* CKE_PLS */
  625. | ((four_act & 0x1f) << 0) /* FOUR_ACT */
  626. );
  627. debug("DDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
  628. /*
  629. * Determine the Mode Register Set.
  630. *
  631. * This is nominally part specific, but it appears to be
  632. * consistent for all DDR I devices, and for all DDR II devices.
  633. *
  634. * caslat must be programmed
  635. * burst length is always 4
  636. * burst type is sequential
  637. *
  638. * For DDR I:
  639. * operating mode is "normal"
  640. *
  641. * For DDR II:
  642. * other stuff
  643. */
  644. mode_caslat = 0;
  645. /*
  646. * Table lookup from DDR I or II Device Operation Specs.
  647. */
  648. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  649. if (1 <= caslat && caslat <= 4) {
  650. unsigned char mode_caslat_table[4] = {
  651. 0x5, /* 1.5 clocks */
  652. 0x2, /* 2.0 clocks */
  653. 0x6, /* 2.5 clocks */
  654. 0x3 /* 3.0 clocks */
  655. };
  656. mode_caslat = mode_caslat_table[caslat - 1];
  657. } else {
  658. puts("DDR I: Only CAS Latencies of 1.5, 2.0, "
  659. "2.5 and 3.0 clocks are supported.\n");
  660. return 0;
  661. }
  662. } else {
  663. if (2 <= caslat && caslat <= 5) {
  664. mode_caslat = caslat;
  665. } else {
  666. puts("DDR II: Only CAS Latencies of 2.0, 3.0, "
  667. "4.0 and 5.0 clocks are supported.\n");
  668. return 0;
  669. }
  670. }
  671. /*
  672. * Encoded Burst Lenght of 4.
  673. */
  674. burst_len = 2; /* Fiat. */
  675. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  676. twr_auto_clk = 0; /* Historical */
  677. } else {
  678. /*
  679. * Determine tCK max in picos. Grab tWR and convert to picos.
  680. * Auto-precharge write recovery is:
  681. * WR = roundup(tWR_ns/tCKmax_ns).
  682. *
  683. * Ponder: Is twr_auto_clk different than twr_clk?
  684. */
  685. tCKmax_ps = convert_bcd_tenths_to_cycle_time_ps(spd.tckmax);
  686. twr_auto_clk = (spd.twr * 250 + tCKmax_ps - 1) / tCKmax_ps;
  687. }
  688. /*
  689. * Mode Reg in bits 16 ~ 31,
  690. * Extended Mode Reg 1 in bits 0 ~ 15.
  691. */
  692. mode_odt_enable = 0x0; /* Default disabled */
  693. if (odt_wr_cfg || odt_rd_cfg) {
  694. /*
  695. * Bits 6 and 2 in Extended MRS(1)
  696. * Bit 2 == 0x04 == 75 Ohm, with 2 DIMM modules.
  697. * Bit 6 == 0x40 == 150 Ohm, with 1 DIMM module.
  698. */
  699. mode_odt_enable = 0x40; /* 150 Ohm */
  700. }
  701. ddr->sdram_mode =
  702. (0
  703. | (add_lat << (16 + 3)) /* Additive Latency in EMRS1 */
  704. | (mode_odt_enable << 16) /* ODT Enable in EMRS1 */
  705. | (twr_auto_clk << 9) /* Write Recovery Autopre */
  706. | (mode_caslat << 4) /* caslat */
  707. | (burst_len << 0) /* Burst length */
  708. );
  709. debug("DDR: sdram_mode = 0x%08x\n", ddr->sdram_mode);
  710. /*
  711. * Clear EMRS2 and EMRS3.
  712. */
  713. ddr->sdram_mode_2 = 0;
  714. debug("DDR: sdram_mode_2 = 0x%08x\n", ddr->sdram_mode_2);
  715. /*
  716. * Determine Refresh Rate.
  717. */
  718. refresh_clk = determine_refresh_rate(spd.refresh & 0x7);
  719. /*
  720. * Set BSTOPRE to 0x100 for page mode
  721. * If auto-charge is used, set BSTOPRE = 0
  722. */
  723. ddr->sdram_interval =
  724. (0
  725. | (refresh_clk & 0x3fff) << 16
  726. | 0x100
  727. );
  728. debug("DDR: sdram_interval = 0x%08x\n", ddr->sdram_interval);
  729. /*
  730. * Is this an ECC DDR chip?
  731. * But don't mess with it if the DDR controller will init mem.
  732. */
  733. #ifdef CONFIG_DDR_ECC
  734. if (spd.config == 0x02) {
  735. #ifndef CONFIG_ECC_INIT_VIA_DDRCONTROLLER
  736. ddr->err_disable = 0x0000000d;
  737. #endif
  738. ddr->err_sbe = 0x00ff0000;
  739. }
  740. debug("DDR: err_disable = 0x%08x\n", ddr->err_disable);
  741. debug("DDR: err_sbe = 0x%08x\n", ddr->err_sbe);
  742. #endif /* CONFIG_DDR_ECC */
  743. asm("sync;isync;msync");
  744. udelay(500);
  745. /*
  746. * SDRAM Cfg 2
  747. */
  748. /*
  749. * When ODT is enabled, Chap 9 suggests asserting ODT to
  750. * internal IOs only during reads.
  751. */
  752. odt_cfg = 0;
  753. if (odt_rd_cfg | odt_wr_cfg) {
  754. odt_cfg = 0x2; /* ODT to IOs during reads */
  755. }
  756. /*
  757. * Try to use differential DQS with DDR II.
  758. */
  759. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  760. dqs_cfg = 0; /* No Differential DQS for DDR I */
  761. } else {
  762. dqs_cfg = 0x1; /* Differential DQS for DDR II */
  763. }
  764. #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  765. /*
  766. * Use the DDR controller to auto initialize memory.
  767. */
  768. d_init = 1;
  769. ddr->sdram_data_init = CONFIG_MEM_INIT_VALUE;
  770. debug("DDR: ddr_data_init = 0x%08x\n", ddr->sdram_data_init);
  771. #else
  772. /*
  773. * Memory will be initialized via DMA, or not at all.
  774. */
  775. d_init = 0;
  776. #endif
  777. ddr->sdram_cfg_2 = (0
  778. | (dqs_cfg << 26) /* Differential DQS */
  779. | (odt_cfg << 21) /* ODT */
  780. | (d_init << 4) /* D_INIT auto init DDR */
  781. );
  782. debug("DDR: sdram_cfg_2 = 0x%08x\n", ddr->sdram_cfg_2);
  783. #ifdef MPC85xx_DDR_SDRAM_CLK_CNTL
  784. /*
  785. * Setup the clock control.
  786. * SDRAM_CLK_CNTL[0] = Source synchronous enable == 1
  787. * SDRAM_CLK_CNTL[5-7] = Clock Adjust
  788. * 0110 3/4 cycle late
  789. * 0111 7/8 cycle late
  790. */
  791. if (spd.mem_type == SPD_MEMTYPE_DDR)
  792. clk_adjust = 0x6;
  793. else
  794. #ifdef CONFIG_MPC8568
  795. /* Empirally setting clk_adjust */
  796. clk_adjust = 0x6;
  797. #else
  798. clk_adjust = 0x7;
  799. #endif
  800. ddr->sdram_clk_cntl = (0
  801. | 0x80000000
  802. | (clk_adjust << 23)
  803. );
  804. debug("DDR: sdram_clk_cntl = 0x%08x\n", ddr->sdram_clk_cntl);
  805. #endif
  806. /*
  807. * Figure out the settings for the sdram_cfg register.
  808. * Build up the entire register in 'sdram_cfg' before writing
  809. * since the write into the register will actually enable the
  810. * memory controller; all settings must be done before enabling.
  811. *
  812. * sdram_cfg[0] = 1 (ddr sdram logic enable)
  813. * sdram_cfg[1] = 1 (self-refresh-enable)
  814. * sdram_cfg[5:7] = (SDRAM type = DDR SDRAM)
  815. * 010 DDR 1 SDRAM
  816. * 011 DDR 2 SDRAM
  817. */
  818. sdram_type = (spd.mem_type == SPD_MEMTYPE_DDR) ? 2 : 3;
  819. sdram_cfg = (0
  820. | (1 << 31) /* Enable */
  821. | (1 << 30) /* Self refresh */
  822. | (sdram_type << 24) /* SDRAM type */
  823. );
  824. /*
  825. * sdram_cfg[3] = RD_EN - registered DIMM enable
  826. * A value of 0x26 indicates micron registered DIMMS (micron.com)
  827. */
  828. if (spd.mem_type == SPD_MEMTYPE_DDR && spd.mod_attr == 0x26) {
  829. sdram_cfg |= 0x10000000; /* RD_EN */
  830. }
  831. #if defined(CONFIG_DDR_ECC)
  832. /*
  833. * If the user wanted ECC (enabled via sdram_cfg[2])
  834. */
  835. if (spd.config == 0x02) {
  836. sdram_cfg |= 0x20000000; /* ECC_EN */
  837. }
  838. #endif
  839. /*
  840. * REV1 uses 1T timing.
  841. * REV2 may use 1T or 2T as configured by the user.
  842. */
  843. {
  844. uint pvr = get_pvr();
  845. if (pvr != PVR_85xx_REV1) {
  846. #if defined(CONFIG_DDR_2T_TIMING)
  847. /*
  848. * Enable 2T timing by setting sdram_cfg[16].
  849. */
  850. sdram_cfg |= 0x8000; /* 2T_EN */
  851. #endif
  852. }
  853. }
  854. /*
  855. * 200 painful micro-seconds must elapse between
  856. * the DDR clock setup and the DDR config enable.
  857. */
  858. udelay(200);
  859. /*
  860. * Go!
  861. */
  862. ddr->sdram_cfg = sdram_cfg;
  863. asm("sync;isync;msync");
  864. udelay(500);
  865. debug("DDR: sdram_cfg = 0x%08x\n", ddr->sdram_cfg);
  866. #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  867. /*
  868. * Poll until memory is initialized.
  869. * 512 Meg at 400 might hit this 200 times or so.
  870. */
  871. while ((ddr->sdram_cfg_2 & (d_init << 4)) != 0) {
  872. udelay(1000);
  873. }
  874. #endif
  875. /*
  876. * Figure out memory size in Megabytes.
  877. */
  878. memsize = n_ranks * rank_density / 0x100000;
  879. /*
  880. * Establish Local Access Window and TLB mappings for DDR memory.
  881. */
  882. memsize = setup_laws_and_tlbs(memsize);
  883. if (memsize == 0) {
  884. return 0;
  885. }
  886. return memsize * 1024 * 1024;
  887. }
  888. /*
  889. * Setup Local Access Window and TLB1 mappings for the requested
  890. * amount of memory. Returns the amount of memory actually mapped
  891. * (usually the original request size), or 0 on error.
  892. */
  893. static unsigned int
  894. setup_laws_and_tlbs(unsigned int memsize)
  895. {
  896. unsigned int tlb_size;
  897. unsigned int law_size;
  898. unsigned int ram_tlb_index;
  899. unsigned int ram_tlb_address;
  900. /*
  901. * Determine size of each TLB1 entry.
  902. */
  903. switch (memsize) {
  904. case 16:
  905. case 32:
  906. tlb_size = BOOKE_PAGESZ_16M;
  907. break;
  908. case 64:
  909. case 128:
  910. tlb_size = BOOKE_PAGESZ_64M;
  911. break;
  912. case 256:
  913. case 512:
  914. tlb_size = BOOKE_PAGESZ_256M;
  915. break;
  916. case 1024:
  917. case 2048:
  918. if (PVR_VER(get_pvr()) > PVR_VER(PVR_85xx))
  919. tlb_size = BOOKE_PAGESZ_1G;
  920. else
  921. tlb_size = BOOKE_PAGESZ_256M;
  922. break;
  923. default:
  924. puts("DDR: only 16M,32M,64M,128M,256M,512M,1G and 2G are supported.\n");
  925. /*
  926. * The memory was not able to be mapped.
  927. * Default to a small size.
  928. */
  929. tlb_size = BOOKE_PAGESZ_64M;
  930. memsize=64;
  931. break;
  932. }
  933. /*
  934. * Configure DDR TLB1 entries.
  935. * Starting at TLB1 8, use no more than 8 TLB1 entries.
  936. */
  937. ram_tlb_index = 8;
  938. ram_tlb_address = (unsigned int)CFG_DDR_SDRAM_BASE;
  939. while (ram_tlb_address < (memsize * 1024 * 1024)
  940. && ram_tlb_index < 16) {
  941. set_tlb(1, ram_tlb_address, ram_tlb_address,
  942. MAS3_SX|MAS3_SW|MAS3_SR, 0,
  943. 0, ram_tlb_index, tlb_size, 1);
  944. ram_tlb_address += (0x1000 << ((tlb_size - 1) * 2));
  945. ram_tlb_index++;
  946. }
  947. /*
  948. * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23. Fnord.
  949. */
  950. law_size = 19 + __ilog2(memsize);
  951. /*
  952. * Set up LAWBAR for all of DDR.
  953. */
  954. #ifdef CONFIG_FSL_LAW
  955. set_law(1, CFG_DDR_SDRAM_BASE, law_size, LAW_TRGT_IF_DDR);
  956. #endif
  957. /*
  958. * Confirm that the requested amount of memory was mapped.
  959. */
  960. return memsize;
  961. }
  962. #endif /* CONFIG_SPD_EEPROM */
  963. #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  964. /*
  965. * Initialize all of memory for ECC, then enable errors.
  966. */
  967. void
  968. ddr_enable_ecc(unsigned int dram_size)
  969. {
  970. uint *p = 0;
  971. uint i = 0;
  972. volatile ccsr_ddr_t *ddr= (void *)(CFG_MPC85xx_DDR_ADDR);
  973. dma_init();
  974. for (*p = 0; p < (uint *)(8 * 1024); p++) {
  975. if (((unsigned int)p & 0x1f) == 0) {
  976. ppcDcbz((unsigned long) p);
  977. }
  978. *p = (unsigned int)CONFIG_MEM_INIT_VALUE;
  979. if (((unsigned int)p & 0x1c) == 0x1c) {
  980. ppcDcbf((unsigned long) p);
  981. }
  982. }
  983. dma_xfer((uint *)0x002000, 0x002000, (uint *)0); /* 8K */
  984. dma_xfer((uint *)0x004000, 0x004000, (uint *)0); /* 16K */
  985. dma_xfer((uint *)0x008000, 0x008000, (uint *)0); /* 32K */
  986. dma_xfer((uint *)0x010000, 0x010000, (uint *)0); /* 64K */
  987. dma_xfer((uint *)0x020000, 0x020000, (uint *)0); /* 128k */
  988. dma_xfer((uint *)0x040000, 0x040000, (uint *)0); /* 256k */
  989. dma_xfer((uint *)0x080000, 0x080000, (uint *)0); /* 512k */
  990. dma_xfer((uint *)0x100000, 0x100000, (uint *)0); /* 1M */
  991. dma_xfer((uint *)0x200000, 0x200000, (uint *)0); /* 2M */
  992. dma_xfer((uint *)0x400000, 0x400000, (uint *)0); /* 4M */
  993. for (i = 1; i < dram_size / 0x800000; i++) {
  994. dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
  995. }
  996. /*
  997. * Enable errors for ECC.
  998. */
  999. debug("DMA DDR: err_disable = 0x%08x\n", ddr->err_disable);
  1000. ddr->err_disable = 0x00000000;
  1001. asm("sync;isync;msync");
  1002. debug("DMA DDR: err_disable = 0x%08x\n", ddr->err_disable);
  1003. }
  1004. #endif /* CONFIG_DDR_ECC && ! CONFIG_ECC_INIT_VIA_DDRCONTROLLER */