spd_sdram.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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_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. 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_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 = &immap->im_gur;
  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_bus_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 == 266 || effective_data_rate == 333) {
  613. cpo = 0x7; /* READ_LAT + 5/4 */
  614. } else if (effective_data_rate == 400) {
  615. cpo = 0x9; /* READ_LAT + 7/4 */
  616. } else {
  617. /* Pure speculation */
  618. cpo = 0xb;
  619. }
  620. }
  621. ddr->timing_cfg_2 = (0
  622. | ((add_lat & 0x7) << 28) /* ADD_LAT */
  623. | ((cpo & 0x1f) << 23) /* CPO */
  624. | ((wr_lat & 0x7) << 19) /* WR_LAT */
  625. | ((trtp_clk & 0x7) << 13) /* RD_TO_PRE */
  626. | ((wr_data_delay & 0x7) << 10) /* WR_DATA_DELAY */
  627. | ((cke_min_clk & 0x7) << 6) /* CKE_PLS */
  628. | ((four_act & 0x1f) << 0) /* FOUR_ACT */
  629. );
  630. debug("DDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
  631. /*
  632. * Determine the Mode Register Set.
  633. *
  634. * This is nominally part specific, but it appears to be
  635. * consistent for all DDR I devices, and for all DDR II devices.
  636. *
  637. * caslat must be programmed
  638. * burst length is always 4
  639. * burst type is sequential
  640. *
  641. * For DDR I:
  642. * operating mode is "normal"
  643. *
  644. * For DDR II:
  645. * other stuff
  646. */
  647. mode_caslat = 0;
  648. /*
  649. * Table lookup from DDR I or II Device Operation Specs.
  650. */
  651. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  652. if (1 <= caslat && caslat <= 4) {
  653. unsigned char mode_caslat_table[4] = {
  654. 0x5, /* 1.5 clocks */
  655. 0x2, /* 2.0 clocks */
  656. 0x6, /* 2.5 clocks */
  657. 0x3 /* 3.0 clocks */
  658. };
  659. mode_caslat = mode_caslat_table[caslat - 1];
  660. } else {
  661. puts("DDR I: Only CAS Latencies of 1.5, 2.0, "
  662. "2.5 and 3.0 clocks are supported.\n");
  663. return 0;
  664. }
  665. } else {
  666. if (2 <= caslat && caslat <= 5) {
  667. mode_caslat = caslat;
  668. } else {
  669. puts("DDR II: Only CAS Latencies of 2.0, 3.0, "
  670. "4.0 and 5.0 clocks are supported.\n");
  671. return 0;
  672. }
  673. }
  674. /*
  675. * Encoded Burst Lenght of 4.
  676. */
  677. burst_len = 2; /* Fiat. */
  678. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  679. twr_auto_clk = 0; /* Historical */
  680. } else {
  681. /*
  682. * Determine tCK max in picos. Grab tWR and convert to picos.
  683. * Auto-precharge write recovery is:
  684. * WR = roundup(tWR_ns/tCKmax_ns).
  685. *
  686. * Ponder: Is twr_auto_clk different than twr_clk?
  687. */
  688. tCKmax_ps = convert_bcd_tenths_to_cycle_time_ps(spd.tckmax);
  689. twr_auto_clk = (spd.twr * 250 + tCKmax_ps - 1) / tCKmax_ps;
  690. }
  691. /*
  692. * Mode Reg in bits 16 ~ 31,
  693. * Extended Mode Reg 1 in bits 0 ~ 15.
  694. */
  695. mode_odt_enable = 0x0; /* Default disabled */
  696. if (odt_wr_cfg || odt_rd_cfg) {
  697. /*
  698. * Bits 6 and 2 in Extended MRS(1)
  699. * Bit 2 == 0x04 == 75 Ohm, with 2 DIMM modules.
  700. * Bit 6 == 0x40 == 150 Ohm, with 1 DIMM module.
  701. */
  702. mode_odt_enable = 0x40; /* 150 Ohm */
  703. }
  704. ddr->sdram_mode =
  705. (0
  706. | (add_lat << (16 + 3)) /* Additive Latency in EMRS1 */
  707. | (mode_odt_enable << 16) /* ODT Enable in EMRS1 */
  708. | (twr_auto_clk << 9) /* Write Recovery Autopre */
  709. | (mode_caslat << 4) /* caslat */
  710. | (burst_len << 0) /* Burst length */
  711. );
  712. debug("DDR: sdram_mode = 0x%08x\n", ddr->sdram_mode);
  713. /*
  714. * Clear EMRS2 and EMRS3.
  715. */
  716. ddr->sdram_mode_2 = 0;
  717. debug("DDR: sdram_mode_2 = 0x%08x\n", ddr->sdram_mode_2);
  718. /*
  719. * Determine Refresh Rate.
  720. */
  721. refresh_clk = determine_refresh_rate(spd.refresh & 0x7);
  722. /*
  723. * Set BSTOPRE to 0x100 for page mode
  724. * If auto-charge is used, set BSTOPRE = 0
  725. */
  726. ddr->sdram_interval =
  727. (0
  728. | (refresh_clk & 0x3fff) << 16
  729. | 0x100
  730. );
  731. debug("DDR: sdram_interval = 0x%08x\n", ddr->sdram_interval);
  732. /*
  733. * Is this an ECC DDR chip?
  734. * But don't mess with it if the DDR controller will init mem.
  735. */
  736. #ifdef CONFIG_DDR_ECC
  737. if (spd.config == 0x02) {
  738. #ifndef CONFIG_ECC_INIT_VIA_DDRCONTROLLER
  739. ddr->err_disable = 0x0000000d;
  740. #endif
  741. ddr->err_sbe = 0x00ff0000;
  742. }
  743. debug("DDR: err_disable = 0x%08x\n", ddr->err_disable);
  744. debug("DDR: err_sbe = 0x%08x\n", ddr->err_sbe);
  745. #endif /* CONFIG_DDR_ECC */
  746. asm("sync;isync;msync");
  747. udelay(500);
  748. /*
  749. * SDRAM Cfg 2
  750. */
  751. /*
  752. * When ODT is enabled, Chap 9 suggests asserting ODT to
  753. * internal IOs only during reads.
  754. */
  755. odt_cfg = 0;
  756. if (odt_rd_cfg | odt_wr_cfg) {
  757. odt_cfg = 0x2; /* ODT to IOs during reads */
  758. }
  759. /*
  760. * Try to use differential DQS with DDR II.
  761. */
  762. if (spd.mem_type == SPD_MEMTYPE_DDR) {
  763. dqs_cfg = 0; /* No Differential DQS for DDR I */
  764. } else {
  765. dqs_cfg = 0x1; /* Differential DQS for DDR II */
  766. }
  767. #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  768. /*
  769. * Use the DDR controller to auto initialize memory.
  770. */
  771. d_init = 1;
  772. ddr->sdram_data_init = CONFIG_MEM_INIT_VALUE;
  773. debug("DDR: ddr_data_init = 0x%08x\n", ddr->sdram_data_init);
  774. #else
  775. /*
  776. * Memory will be initialized via DMA, or not at all.
  777. */
  778. d_init = 0;
  779. #endif
  780. ddr->sdram_cfg_2 = (0
  781. | (dqs_cfg << 26) /* Differential DQS */
  782. | (odt_cfg << 21) /* ODT */
  783. | (d_init << 4) /* D_INIT auto init DDR */
  784. );
  785. debug("DDR: sdram_cfg_2 = 0x%08x\n", ddr->sdram_cfg_2);
  786. #ifdef MPC85xx_DDR_SDRAM_CLK_CNTL
  787. /*
  788. * Setup the clock control.
  789. * SDRAM_CLK_CNTL[0] = Source synchronous enable == 1
  790. * SDRAM_CLK_CNTL[5-7] = Clock Adjust
  791. * 0110 3/4 cycle late
  792. * 0111 7/8 cycle late
  793. */
  794. if (spd.mem_type == SPD_MEMTYPE_DDR)
  795. clk_adjust = 0x6;
  796. else
  797. clk_adjust = 0x7;
  798. ddr->sdram_clk_cntl = (0
  799. | 0x80000000
  800. | (clk_adjust << 23)
  801. );
  802. debug("DDR: sdram_clk_cntl = 0x%08x\n", ddr->sdram_clk_cntl);
  803. #endif
  804. /*
  805. * Figure out the settings for the sdram_cfg register.
  806. * Build up the entire register in 'sdram_cfg' before writing
  807. * since the write into the register will actually enable the
  808. * memory controller; all settings must be done before enabling.
  809. *
  810. * sdram_cfg[0] = 1 (ddr sdram logic enable)
  811. * sdram_cfg[1] = 1 (self-refresh-enable)
  812. * sdram_cfg[5:7] = (SDRAM type = DDR SDRAM)
  813. * 010 DDR 1 SDRAM
  814. * 011 DDR 2 SDRAM
  815. */
  816. sdram_type = (spd.mem_type == SPD_MEMTYPE_DDR) ? 2 : 3;
  817. sdram_cfg = (0
  818. | (1 << 31) /* Enable */
  819. | (1 << 30) /* Self refresh */
  820. | (sdram_type << 24) /* SDRAM type */
  821. );
  822. /*
  823. * sdram_cfg[3] = RD_EN - registered DIMM enable
  824. * A value of 0x26 indicates micron registered DIMMS (micron.com)
  825. */
  826. if (spd.mem_type == SPD_MEMTYPE_DDR && spd.mod_attr == 0x26) {
  827. sdram_cfg |= 0x10000000; /* RD_EN */
  828. }
  829. #if defined(CONFIG_DDR_ECC)
  830. /*
  831. * If the user wanted ECC (enabled via sdram_cfg[2])
  832. */
  833. if (spd.config == 0x02) {
  834. sdram_cfg |= 0x20000000; /* ECC_EN */
  835. }
  836. #endif
  837. /*
  838. * REV1 uses 1T timing.
  839. * REV2 may use 1T or 2T as configured by the user.
  840. */
  841. {
  842. uint pvr = get_pvr();
  843. if (pvr != PVR_85xx_REV1) {
  844. #if defined(CONFIG_DDR_2T_TIMING)
  845. /*
  846. * Enable 2T timing by setting sdram_cfg[16].
  847. */
  848. sdram_cfg |= 0x8000; /* 2T_EN */
  849. #endif
  850. }
  851. }
  852. /*
  853. * 200 painful micro-seconds must elapse between
  854. * the DDR clock setup and the DDR config enable.
  855. */
  856. udelay(200);
  857. /*
  858. * Go!
  859. */
  860. ddr->sdram_cfg = sdram_cfg;
  861. asm("sync;isync;msync");
  862. udelay(500);
  863. debug("DDR: sdram_cfg = 0x%08x\n", ddr->sdram_cfg);
  864. #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
  865. /*
  866. * Poll until memory is initialized.
  867. * 512 Meg at 400 might hit this 200 times or so.
  868. */
  869. while ((ddr->sdram_cfg_2 & (d_init << 4)) != 0) {
  870. udelay(1000);
  871. }
  872. #endif
  873. /*
  874. * Figure out memory size in Megabytes.
  875. */
  876. memsize = n_ranks * rank_density / 0x100000;
  877. /*
  878. * Establish Local Access Window and TLB mappings for DDR memory.
  879. */
  880. memsize = setup_laws_and_tlbs(memsize);
  881. if (memsize == 0) {
  882. return 0;
  883. }
  884. return memsize * 1024 * 1024;
  885. }
  886. /*
  887. * Setup Local Access Window and TLB1 mappings for the requested
  888. * amount of memory. Returns the amount of memory actually mapped
  889. * (usually the original request size), or 0 on error.
  890. */
  891. static unsigned int
  892. setup_laws_and_tlbs(unsigned int memsize)
  893. {
  894. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  895. volatile ccsr_local_ecm_t *ecm = &immap->im_local_ecm;
  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 immap_t *immap = (immap_t *)CFG_IMMR;
  988. volatile ccsr_ddr_t *ddr= &immap->im_ddr;
  989. dma_init();
  990. for (*p = 0; p < (uint *)(8 * 1024); p++) {
  991. if (((unsigned int)p & 0x1f) == 0) {
  992. ppcDcbz((unsigned long) p);
  993. }
  994. *p = (unsigned int)CONFIG_MEM_INIT_VALUE;
  995. if (((unsigned int)p & 0x1c) == 0x1c) {
  996. ppcDcbf((unsigned long) p);
  997. }
  998. }
  999. dma_xfer((uint *)0x002000, 0x002000, (uint *)0); /* 8K */
  1000. dma_xfer((uint *)0x004000, 0x004000, (uint *)0); /* 16K */
  1001. dma_xfer((uint *)0x008000, 0x008000, (uint *)0); /* 32K */
  1002. dma_xfer((uint *)0x010000, 0x010000, (uint *)0); /* 64K */
  1003. dma_xfer((uint *)0x020000, 0x020000, (uint *)0); /* 128k */
  1004. dma_xfer((uint *)0x040000, 0x040000, (uint *)0); /* 256k */
  1005. dma_xfer((uint *)0x080000, 0x080000, (uint *)0); /* 512k */
  1006. dma_xfer((uint *)0x100000, 0x100000, (uint *)0); /* 1M */
  1007. dma_xfer((uint *)0x200000, 0x200000, (uint *)0); /* 2M */
  1008. dma_xfer((uint *)0x400000, 0x400000, (uint *)0); /* 4M */
  1009. for (i = 1; i < dram_size / 0x800000; i++) {
  1010. dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
  1011. }
  1012. /*
  1013. * Enable errors for ECC.
  1014. */
  1015. debug("DMA DDR: err_disable = 0x%08x\n", ddr->err_disable);
  1016. ddr->err_disable = 0x00000000;
  1017. asm("sync;isync;msync");
  1018. debug("DMA DDR: err_disable = 0x%08x\n", ddr->err_disable);
  1019. }
  1020. #endif /* CONFIG_DDR_ECC && ! CONFIG_ECC_INIT_VIA_DDRCONTROLLER */