spd_sdram.c 29 KB

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