spd_sdram.c 28 KB

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