sdram_init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * (C) Copyright 2001
  3. * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /* sdram_init.c - automatic memory sizing */
  24. #include <common.h>
  25. #include <74xx_7xx.h>
  26. #include <galileo/memory.h>
  27. #include <galileo/pci.h>
  28. #include <galileo/gt64260R.h>
  29. #include <net.h>
  30. #include "eth.h"
  31. #include "mpsc.h"
  32. #include "i2c.h"
  33. #include "64260.h"
  34. /* #define DEBUG */
  35. #define MAP_PCI
  36. #ifdef DEBUG
  37. #define DP(x) x
  38. #else
  39. #define DP(x)
  40. #endif
  41. #define GB (1 << 30)
  42. /* structure to store the relevant information about an sdram bank */
  43. typedef struct sdram_info {
  44. uchar drb_size;
  45. uchar registered, ecc;
  46. uchar tpar;
  47. uchar tras_clocks;
  48. uchar burst_len;
  49. uchar banks, slot;
  50. int size; /* detected size, not from I2C but from dram_size() */
  51. } sdram_info_t;
  52. #ifdef DEBUG
  53. void dump_dimm_info(struct sdram_info *d)
  54. {
  55. static const char *ecc_legend[]={""," Parity"," ECC"};
  56. printf("dimm%s %sDRAM: %dMibytes:\n",
  57. ecc_legend[d->ecc],
  58. d->registered?"R":"",
  59. (d->size>>20));
  60. printf(" drb=%d tpar=%d tras=%d burstlen=%d banks=%d slot=%d\n",
  61. d->drb_size, d->tpar, d->tras_clocks, d->burst_len,
  62. d->banks, d->slot);
  63. }
  64. #endif
  65. static int
  66. memory_map_bank(unsigned int bankNo,
  67. unsigned int bankBase,
  68. unsigned int bankLength)
  69. {
  70. #ifdef DEBUG
  71. if (bankLength > 0) {
  72. printf("mapping bank %d at %08x - %08x\n",
  73. bankNo, bankBase, bankBase + bankLength - 1);
  74. } else {
  75. printf("unmapping bank %d\n", bankNo);
  76. }
  77. #endif
  78. memoryMapBank(bankNo, bankBase, bankLength);
  79. return 0;
  80. }
  81. #ifdef MAP_PCI
  82. static int
  83. memory_map_bank_pci(unsigned int bankNo,
  84. unsigned int bankBase,
  85. unsigned int bankLength)
  86. {
  87. PCI_HOST host;
  88. for (host=PCI_HOST0;host<=PCI_HOST1;host++) {
  89. const int features=
  90. PREFETCH_ENABLE |
  91. DELAYED_READ_ENABLE |
  92. AGGRESSIVE_PREFETCH |
  93. READ_LINE_AGGRESSIVE_PREFETCH |
  94. READ_MULTI_AGGRESSIVE_PREFETCH |
  95. MAX_BURST_4 |
  96. PCI_NO_SWAP;
  97. pciMapMemoryBank(host, bankNo, bankBase, bankLength);
  98. pciSetRegionSnoopMode(host, bankNo, PCI_SNOOP_WB, bankBase,
  99. bankLength);
  100. pciSetRegionFeatures(host, bankNo, features, bankBase, bankLength);
  101. }
  102. return 0;
  103. }
  104. #endif
  105. /* ------------------------------------------------------------------------- */
  106. /* much of this code is based on (or is) the code in the pip405 port */
  107. /* thanks go to the authors of said port - Josh */
  108. /*
  109. * translate ns.ns/10 coding of SPD timing values
  110. * into 10 ps unit values
  111. */
  112. static inline unsigned short
  113. NS10to10PS(unsigned char spd_byte)
  114. {
  115. unsigned short ns, ns10;
  116. /* isolate upper nibble */
  117. ns = (spd_byte >> 4) & 0x0F;
  118. /* isolate lower nibble */
  119. ns10 = (spd_byte & 0x0F);
  120. return(ns*100 + ns10*10);
  121. }
  122. /*
  123. * translate ns coding of SPD timing values
  124. * into 10 ps unit values
  125. */
  126. static inline unsigned short
  127. NSto10PS(unsigned char spd_byte)
  128. {
  129. return(spd_byte*100);
  130. }
  131. #ifdef CONFIG_ZUMA_V2
  132. static int
  133. check_dimm(uchar slot, sdram_info_t *info)
  134. {
  135. /* assume 2 dimms, 2 banks each 256M - we dont have an
  136. * dimm i2c so rely on the detection routines later */
  137. memset(info, 0, sizeof(*info));
  138. info->slot = slot;
  139. info->banks = 2; /* Detect later */
  140. info->registered = 0;
  141. info->drb_size = 32; /* 16 - 256MBit, 32 - 512MBit
  142. but doesn't matter, both do same
  143. thing in setup_sdram() */
  144. info->tpar = 3;
  145. info->tras_clocks = 5;
  146. info->burst_len = 4;
  147. #ifdef CONFIG_ECC
  148. info->ecc = 0; /* Detect later */
  149. #endif /* CONFIG_ECC */
  150. return 0;
  151. }
  152. #else /* ! CONFIG_ZUMA_V2 */
  153. /* This code reads the SPD chip on the sdram and populates
  154. * the array which is passed in with the relevant information */
  155. static int
  156. check_dimm(uchar slot, sdram_info_t *info)
  157. {
  158. DECLARE_GLOBAL_DATA_PTR;
  159. uchar addr = slot == 0 ? DIMM0_I2C_ADDR : DIMM1_I2C_ADDR;
  160. int ret;
  161. uchar rows, cols, sdram_banks, supp_cal, width, cal_val;
  162. ulong tmemclk;
  163. uchar trp_clocks, trcd_clocks;
  164. uchar data[128];
  165. get_clocks ();
  166. tmemclk = 1000000000 / (gd->bus_clk / 100); /* in 10 ps units */
  167. #ifdef CONFIG_EVB64260_750CX
  168. if (0 != slot) {
  169. printf("check_dimm: The EVB-64260-750CX only has 1 DIMM,");
  170. printf(" called with slot=%d insetad!\n", slot);
  171. return 0;
  172. }
  173. #endif
  174. DP(puts("before i2c read\n"));
  175. ret = i2c_read(addr, 0, 128, data, 0);
  176. DP(puts("after i2c read\n"));
  177. /* zero all the values */
  178. memset(info, 0, sizeof(*info));
  179. if (ret) {
  180. DP(printf("No DIMM in slot %d [err = %x]\n", slot, ret));
  181. return 0;
  182. }
  183. /* first, do some sanity checks */
  184. if (data[2] != 0x4) {
  185. printf("Not SDRAM in slot %d\n", slot);
  186. return 0;
  187. }
  188. /* get various information */
  189. rows = data[3];
  190. cols = data[4];
  191. info->banks = data[5];
  192. sdram_banks = data[17];
  193. width = data[13] & 0x7f;
  194. DP(printf("sdram_banks: %d, banks: %d\n", sdram_banks, info->banks));
  195. /* check if the memory is registered */
  196. if (data[21] & (BIT1 | BIT4))
  197. info->registered = 1;
  198. #ifdef CONFIG_ECC
  199. /* check for ECC/parity [0 = none, 1 = parity, 2 = ecc] */
  200. info->ecc = (data[11] & 2) >> 1;
  201. #endif
  202. /* bit 1 is CL2, bit 2 is CL3 */
  203. supp_cal = (data[18] & 0x6) >> 1;
  204. /* compute the relevant clock values */
  205. trp_clocks = (NSto10PS(data[27])+(tmemclk-1)) / tmemclk;
  206. trcd_clocks = (NSto10PS(data[29])+(tmemclk-1)) / tmemclk;
  207. info->tras_clocks = (NSto10PS(data[30])+(tmemclk-1)) / tmemclk;
  208. DP(printf("trp = %d\ntrcd_clocks = %d\ntras_clocks = %d\n",
  209. trp_clocks, trcd_clocks, info->tras_clocks));
  210. /* try a CAS latency of 3 first... */
  211. cal_val = 0;
  212. if (supp_cal & 3) {
  213. if (NS10to10PS(data[9]) <= tmemclk)
  214. cal_val = 3;
  215. }
  216. /* then 2... */
  217. if (supp_cal & 2) {
  218. if (NS10to10PS(data[23]) <= tmemclk)
  219. cal_val = 2;
  220. }
  221. DP(printf("cal_val = %d\n", cal_val));
  222. /* bummer, did't work... */
  223. if (cal_val == 0) {
  224. DP(printf("Couldn't find a good CAS latency\n"));
  225. return 0;
  226. }
  227. /* get the largest delay -- these values need to all be the same
  228. * see Res#6 */
  229. info->tpar = cal_val;
  230. if (trp_clocks > info->tpar)
  231. info->tpar = trp_clocks;
  232. if (trcd_clocks > info->tpar)
  233. info->tpar = trcd_clocks;
  234. DP(printf("tpar set to: %d\n", info->tpar));
  235. #ifdef CFG_BROKEN_CL2
  236. if (info->tpar == 2){
  237. info->tpar = 3;
  238. DP(printf("tpar fixed-up to: %d\n", info->tpar));
  239. }
  240. #endif
  241. /* compute the module DRB size */
  242. info->drb_size = (((1 << (rows + cols)) * sdram_banks) * width) / _16M;
  243. DP(printf("drb_size set to: %d\n", info->drb_size));
  244. /* find the burst len */
  245. info->burst_len = data[16] & 0xf;
  246. if ((info->burst_len & 8) == 8) {
  247. info->burst_len = 1;
  248. } else if ((info->burst_len & 4) == 4) {
  249. info->burst_len = 0;
  250. } else {
  251. return 0;
  252. }
  253. info->slot = slot;
  254. return 0;
  255. }
  256. #endif /* ! CONFIG_ZUMA_V2 */
  257. static int
  258. setup_sdram_common(sdram_info_t info[2])
  259. {
  260. ulong tmp;
  261. int tpar=2, tras_clocks=5, registered=1, ecc=2;
  262. if(!info[0].banks && !info[1].banks) return 0;
  263. if(info[0].banks) {
  264. if(info[0].tpar>tpar) tpar=info[0].tpar;
  265. if(info[0].tras_clocks>tras_clocks) tras_clocks=info[0].tras_clocks;
  266. if(!info[0].registered) registered=0;
  267. if(info[0].ecc!=2) ecc=0;
  268. }
  269. if(info[1].banks) {
  270. if(info[1].tpar>tpar) tpar=info[1].tpar;
  271. if(info[1].tras_clocks>tras_clocks) tras_clocks=info[1].tras_clocks;
  272. if(!info[1].registered) registered=0;
  273. if(info[1].ecc!=2) ecc=0;
  274. }
  275. /* SDRAM configuration */
  276. tmp = GTREGREAD(SDRAM_CONFIGURATION);
  277. /* Turn on physical interleave if both DIMMs
  278. * have even numbers of banks. */
  279. if( (info[0].banks == 0 || info[0].banks == 2) &&
  280. (info[1].banks == 0 || info[1].banks == 2) ) {
  281. /* physical interleave on */
  282. tmp &= ~(1 << 15);
  283. } else {
  284. /* physical interleave off */
  285. tmp |= (1 << 15);
  286. }
  287. tmp |= (registered << 17);
  288. /* Use buffer 1 to return read data to the CPU
  289. * See Res #12 */
  290. tmp |= (1 << 26);
  291. GT_REG_WRITE(SDRAM_CONFIGURATION, tmp);
  292. DP(printf("SDRAM config: %08x\n",
  293. GTREGREAD(SDRAM_CONFIGURATION)));
  294. /* SDRAM timing */
  295. tmp = (((tpar == 3) ? 2 : 1) |
  296. (((tpar == 3) ? 2 : 1) << 2) |
  297. (((tpar == 3) ? 2 : 1) << 4) |
  298. (tras_clocks << 8));
  299. #ifdef CONFIG_ECC
  300. /* Setup ECC */
  301. if (ecc == 2) tmp |= 1<<13;
  302. #endif /* CONFIG_ECC */
  303. GT_REG_WRITE(SDRAM_TIMING, tmp);
  304. DP(printf("SDRAM timing: %08x (%d,%d,%d,%d)\n",
  305. GTREGREAD(SDRAM_TIMING), tpar,tpar,tpar,tras_clocks));
  306. /* SDRAM address decode register */
  307. /* program this with the default value */
  308. GT_REG_WRITE(SDRAM_ADDRESS_DECODE, 0x2);
  309. DP(printf("SDRAM decode: %08x\n",
  310. GTREGREAD(SDRAM_ADDRESS_DECODE)));
  311. return 0;
  312. }
  313. /* sets up the GT properly with information passed in */
  314. static int
  315. setup_sdram(sdram_info_t *info)
  316. {
  317. ulong tmp, check;
  318. ulong *addr = 0;
  319. int i;
  320. /* sanity checking */
  321. if (! info->banks) return 0;
  322. /* ---------------------------- */
  323. /* Program the GT with the discovered data */
  324. /* bank parameters */
  325. tmp = (0xf<<16); /* leave all virt bank pages open */
  326. DP(printf("drb_size: %d\n", info->drb_size));
  327. switch (info->drb_size) {
  328. case 1:
  329. tmp |= (1 << 14);
  330. break;
  331. case 4:
  332. case 8:
  333. tmp |= (2 << 14);
  334. break;
  335. case 16:
  336. case 32:
  337. tmp |= (3 << 14);
  338. break;
  339. default:
  340. printf("Error in dram size calculation\n");
  341. return 1;
  342. }
  343. /* SDRAM bank parameters */
  344. /* the param registers for slot 1 (banks 2+3) are offset by 0x8 */
  345. GT_REG_WRITE(SDRAM_BANK0PARAMETERS + (info->slot * 0x8), tmp);
  346. GT_REG_WRITE(SDRAM_BANK1PARAMETERS + (info->slot * 0x8), tmp);
  347. DP(printf("SDRAM bankparam slot %d (bank %d+%d): %08lx\n", info->slot, info->slot*2, (info->slot*2)+1, tmp));
  348. /* set the SDRAM configuration for each bank */
  349. for (i = info->slot * 2; i < ((info->slot * 2) + info->banks); i++) {
  350. DP(printf("*** Running a MRS cycle for bank %d ***\n", i));
  351. /* map the bank */
  352. memory_map_bank(i, 0, GB/4);
  353. /* set SDRAM mode */
  354. GT_REG_WRITE(SDRAM_OPERATION_MODE, 0x3);
  355. check = GTREGREAD(SDRAM_OPERATION_MODE);
  356. /* dummy write */
  357. *addr = 0;
  358. /* wait for the command to complete */
  359. while ((GTREGREAD(SDRAM_OPERATION_MODE) & (1 << 31)) == 0)
  360. ;
  361. /* switch back to normal operation mode */
  362. GT_REG_WRITE(SDRAM_OPERATION_MODE, 0);
  363. check = GTREGREAD(SDRAM_OPERATION_MODE);
  364. /* unmap the bank */
  365. memory_map_bank(i, 0, 0);
  366. DP(printf("*** MRS cycle for bank %d done ***\n", i));
  367. }
  368. return 0;
  369. }
  370. /*
  371. * Check memory range for valid RAM. A simple memory test determines
  372. * the actually available RAM size between addresses `base' and
  373. * `base + maxsize'. Some (not all) hardware errors are detected:
  374. * - short between address lines
  375. * - short between data lines
  376. */
  377. static long int
  378. dram_size(long int *base, long int maxsize)
  379. {
  380. volatile long int *addr, *b=base;
  381. long int cnt, val, save1, save2;
  382. #define STARTVAL (1<<20) /* start test at 1M */
  383. for (cnt = STARTVAL/sizeof(long); cnt < maxsize/sizeof(long); cnt <<= 1) {
  384. addr = base + cnt; /* pointer arith! */
  385. save1=*addr; /* save contents of addr */
  386. save2=*b; /* save contents of base */
  387. *addr=cnt; /* write cnt to addr */
  388. *b=0; /* put null at base */
  389. /* check at base address */
  390. if ((*b) != 0) {
  391. *addr=save1; /* restore *addr */
  392. *b=save2; /* restore *b */
  393. return (0);
  394. }
  395. val = *addr; /* read *addr */
  396. *addr=save1;
  397. *b=save2;
  398. if (val != cnt) {
  399. /* fix boundary condition.. STARTVAL means zero */
  400. if(cnt==STARTVAL/sizeof(long)) cnt=0;
  401. return (cnt * sizeof(long));
  402. }
  403. }
  404. return maxsize;
  405. }
  406. /* ------------------------------------------------------------------------- */
  407. /* U-Boot interface function to SDRAM init - this is where all the
  408. * controlling logic happens */
  409. long int
  410. initdram(int board_type)
  411. {
  412. ulong checkbank[4] = { [0 ... 3] = 0 };
  413. int bank_no;
  414. ulong total;
  415. int nhr;
  416. sdram_info_t dimm_info[2];
  417. /* first, use the SPD to get info about the SDRAM */
  418. /* check the NHR bit and skip mem init if it's already done */
  419. nhr = get_hid0() & (1 << 16);
  420. if (nhr) {
  421. printf("Skipping SDRAM setup due to NHR bit being set\n");
  422. } else {
  423. /* DIMM0 */
  424. check_dimm(0, &dimm_info[0]);
  425. /* DIMM1 */
  426. #ifndef CONFIG_EVB64260_750CX /* EVB64260_750CX has only 1 DIMM */
  427. check_dimm(1, &dimm_info[1]);
  428. #else /* CONFIG_EVB64260_750CX */
  429. memset(&dimm_info[1], 0, sizeof(sdram_info_t));
  430. #endif
  431. /* unmap all banks */
  432. memory_map_bank(0, 0, 0);
  433. memory_map_bank(1, 0, 0);
  434. memory_map_bank(2, 0, 0);
  435. memory_map_bank(3, 0, 0);
  436. /* Now, program the GT with the correct values */
  437. if (setup_sdram_common(dimm_info)) {
  438. printf("Setup common failed.\n");
  439. }
  440. if (setup_sdram(&dimm_info[0])) {
  441. printf("Setup for DIMM1 failed.\n");
  442. }
  443. if (setup_sdram(&dimm_info[1])) {
  444. printf("Setup for DIMM2 failed.\n");
  445. }
  446. /* set the NHR bit */
  447. set_hid0(get_hid0() | (1 << 16));
  448. }
  449. /* next, size the SDRAM banks */
  450. total = 0;
  451. if (dimm_info[0].banks > 0) checkbank[0] = 1;
  452. if (dimm_info[0].banks > 1) checkbank[1] = 1;
  453. if (dimm_info[0].banks > 2)
  454. printf("Error, SPD claims DIMM1 has >2 banks\n");
  455. if (dimm_info[1].banks > 0) checkbank[2] = 1;
  456. if (dimm_info[1].banks > 1) checkbank[3] = 1;
  457. if (dimm_info[1].banks > 2)
  458. printf("Error, SPD claims DIMM2 has >2 banks\n");
  459. /* Generic dram sizer: works even if we don't have i2c DIMMs,
  460. * as long as the timing settings are more or less correct */
  461. /*
  462. * pass 1: size all the banks, using first bat (0-256M)
  463. * limitation: we only support 256M per bank due to
  464. * us only having 1 BAT for all DRAM
  465. */
  466. for (bank_no = 0; bank_no < CFG_DRAM_BANKS; bank_no++) {
  467. /* skip over banks that are not populated */
  468. if (! checkbank[bank_no])
  469. continue;
  470. DP(printf("checking bank %d\n", bank_no));
  471. memory_map_bank(bank_no, 0, GB/4);
  472. checkbank[bank_no] = dram_size(NULL, GB/4);
  473. memory_map_bank(bank_no, 0, 0);
  474. DP(printf("bank %d %08lx\n", bank_no, checkbank[bank_no]));
  475. }
  476. /*
  477. * pass 2: contiguously map each bank into physical address
  478. * space.
  479. */
  480. dimm_info[0].banks=dimm_info[1].banks=0;
  481. for (bank_no = 0; bank_no < CFG_DRAM_BANKS; bank_no++) {
  482. if(!checkbank[bank_no]) continue;
  483. dimm_info[bank_no/2].banks++;
  484. dimm_info[bank_no/2].size+=checkbank[bank_no];
  485. memory_map_bank(bank_no, total, checkbank[bank_no]);
  486. #ifdef MAP_PCI
  487. memory_map_bank_pci(bank_no, total, checkbank[bank_no]);
  488. #endif
  489. total += checkbank[bank_no];
  490. }
  491. #ifdef CONFIG_ECC
  492. #ifdef CONFIG_ZUMA_V2
  493. /*
  494. * We always enable ECC when bank 2 and 3 are unpopulated
  495. * If we 2 or 3 are populated, we CAN'T support ECC.
  496. * (Zuma boards only support ECC in banks 0 and 1; assume that
  497. * in that configuration, ECC chips are mounted, even for stacked
  498. * chips)
  499. */
  500. if (checkbank[2]==0 && checkbank[3]==0) {
  501. dimm_info[0].ecc=2;
  502. GT_REG_WRITE(SDRAM_TIMING, GTREGREAD(SDRAM_TIMING) | (1 << 13));
  503. /* TODO: do we have to run MRS cycles again? */
  504. }
  505. #endif /* CONFIG_ZUMA_V2 */
  506. if (GTREGREAD(SDRAM_TIMING) & (1 << 13)) {
  507. puts("[ECC] ");
  508. }
  509. #endif /* CONFIG_ECC */
  510. #ifdef DEBUG
  511. dump_dimm_info(&dimm_info[0]);
  512. dump_dimm_info(&dimm_info[1]);
  513. #endif
  514. /* TODO: return at MOST 256M? */
  515. /* return total > GB/4 ? GB/4 : total; */
  516. return total;
  517. }