sdram_init.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. #elif defined(CONFIG_P3G4)
  153. static int
  154. check_dimm(uchar slot, sdram_info_t *info)
  155. {
  156. memset(info, 0, sizeof(*info));
  157. if (slot)
  158. return 0;
  159. info->slot = slot;
  160. info->banks = 1;
  161. info->registered = 0;
  162. info->drb_size = 4;
  163. info->tpar = 3;
  164. info->tras_clocks = 6;
  165. info->burst_len = 4;
  166. #ifdef CONFIG_ECC
  167. info->ecc = 2;
  168. #endif
  169. return 0;
  170. }
  171. #else /* ! CONFIG_ZUMA_V2 && ! CONFIG_P3G4*/
  172. /* This code reads the SPD chip on the sdram and populates
  173. * the array which is passed in with the relevant information */
  174. static int
  175. check_dimm(uchar slot, sdram_info_t *info)
  176. {
  177. DECLARE_GLOBAL_DATA_PTR;
  178. uchar addr = slot == 0 ? DIMM0_I2C_ADDR : DIMM1_I2C_ADDR;
  179. int ret;
  180. uchar rows, cols, sdram_banks, supp_cal, width, cal_val;
  181. ulong tmemclk;
  182. uchar trp_clocks, trcd_clocks;
  183. uchar data[128];
  184. get_clocks ();
  185. tmemclk = 1000000000 / (gd->bus_clk / 100); /* in 10 ps units */
  186. #ifdef CONFIG_EVB64260_750CX
  187. if (0 != slot) {
  188. printf("check_dimm: The EVB-64260-750CX only has 1 DIMM,");
  189. printf(" called with slot=%d insetad!\n", slot);
  190. return 0;
  191. }
  192. #endif
  193. DP(puts("before i2c read\n"));
  194. ret = i2c_read(addr, 0, 128, data, 0);
  195. DP(puts("after i2c read\n"));
  196. /* zero all the values */
  197. memset(info, 0, sizeof(*info));
  198. if (ret) {
  199. DP(printf("No DIMM in slot %d [err = %x]\n", slot, ret));
  200. return 0;
  201. }
  202. /* first, do some sanity checks */
  203. if (data[2] != 0x4) {
  204. printf("Not SDRAM in slot %d\n", slot);
  205. return 0;
  206. }
  207. /* get various information */
  208. rows = data[3];
  209. cols = data[4];
  210. info->banks = data[5];
  211. sdram_banks = data[17];
  212. width = data[13] & 0x7f;
  213. DP(printf("sdram_banks: %d, banks: %d\n", sdram_banks, info->banks));
  214. /* check if the memory is registered */
  215. if (data[21] & (BIT1 | BIT4))
  216. info->registered = 1;
  217. #ifdef CONFIG_ECC
  218. /* check for ECC/parity [0 = none, 1 = parity, 2 = ecc] */
  219. info->ecc = (data[11] & 2) >> 1;
  220. #endif
  221. /* bit 1 is CL2, bit 2 is CL3 */
  222. supp_cal = (data[18] & 0x6) >> 1;
  223. /* compute the relevant clock values */
  224. trp_clocks = (NSto10PS(data[27])+(tmemclk-1)) / tmemclk;
  225. trcd_clocks = (NSto10PS(data[29])+(tmemclk-1)) / tmemclk;
  226. info->tras_clocks = (NSto10PS(data[30])+(tmemclk-1)) / tmemclk;
  227. DP(printf("trp = %d\ntrcd_clocks = %d\ntras_clocks = %d\n",
  228. trp_clocks, trcd_clocks, info->tras_clocks));
  229. /* try a CAS latency of 3 first... */
  230. cal_val = 0;
  231. if (supp_cal & 3) {
  232. if (NS10to10PS(data[9]) <= tmemclk)
  233. cal_val = 3;
  234. }
  235. /* then 2... */
  236. if (supp_cal & 2) {
  237. if (NS10to10PS(data[23]) <= tmemclk)
  238. cal_val = 2;
  239. }
  240. DP(printf("cal_val = %d\n", cal_val));
  241. /* bummer, did't work... */
  242. if (cal_val == 0) {
  243. DP(printf("Couldn't find a good CAS latency\n"));
  244. return 0;
  245. }
  246. /* get the largest delay -- these values need to all be the same
  247. * see Res#6 */
  248. info->tpar = cal_val;
  249. if (trp_clocks > info->tpar)
  250. info->tpar = trp_clocks;
  251. if (trcd_clocks > info->tpar)
  252. info->tpar = trcd_clocks;
  253. DP(printf("tpar set to: %d\n", info->tpar));
  254. #ifdef CFG_BROKEN_CL2
  255. if (info->tpar == 2){
  256. info->tpar = 3;
  257. DP(printf("tpar fixed-up to: %d\n", info->tpar));
  258. }
  259. #endif
  260. /* compute the module DRB size */
  261. info->drb_size = (((1 << (rows + cols)) * sdram_banks) * width) / _16M;
  262. DP(printf("drb_size set to: %d\n", info->drb_size));
  263. /* find the burst len */
  264. info->burst_len = data[16] & 0xf;
  265. if ((info->burst_len & 8) == 8) {
  266. info->burst_len = 1;
  267. } else if ((info->burst_len & 4) == 4) {
  268. info->burst_len = 0;
  269. } else {
  270. return 0;
  271. }
  272. info->slot = slot;
  273. return 0;
  274. }
  275. #endif /* ! CONFIG_ZUMA_V2 */
  276. static int
  277. setup_sdram_common(sdram_info_t info[2])
  278. {
  279. ulong tmp;
  280. int tpar=2, tras_clocks=5, registered=1, ecc=2;
  281. if(!info[0].banks && !info[1].banks) return 0;
  282. if(info[0].banks) {
  283. if(info[0].tpar>tpar) tpar=info[0].tpar;
  284. if(info[0].tras_clocks>tras_clocks) tras_clocks=info[0].tras_clocks;
  285. if(!info[0].registered) registered=0;
  286. if(info[0].ecc!=2) ecc=0;
  287. }
  288. if(info[1].banks) {
  289. if(info[1].tpar>tpar) tpar=info[1].tpar;
  290. if(info[1].tras_clocks>tras_clocks) tras_clocks=info[1].tras_clocks;
  291. if(!info[1].registered) registered=0;
  292. if(info[1].ecc!=2) ecc=0;
  293. }
  294. /* SDRAM configuration */
  295. tmp = GTREGREAD(SDRAM_CONFIGURATION);
  296. /* Turn on physical interleave if both DIMMs
  297. * have even numbers of banks. */
  298. if( (info[0].banks == 0 || info[0].banks == 2) &&
  299. (info[1].banks == 0 || info[1].banks == 2) ) {
  300. /* physical interleave on */
  301. tmp &= ~(1 << 15);
  302. } else {
  303. /* physical interleave off */
  304. tmp |= (1 << 15);
  305. }
  306. tmp |= (registered << 17);
  307. /* Use buffer 1 to return read data to the CPU
  308. * See Res #12 */
  309. tmp |= (1 << 26);
  310. GT_REG_WRITE(SDRAM_CONFIGURATION, tmp);
  311. DP(printf("SDRAM config: %08x\n",
  312. GTREGREAD(SDRAM_CONFIGURATION)));
  313. /* SDRAM timing */
  314. tmp = (((tpar == 3) ? 2 : 1) |
  315. (((tpar == 3) ? 2 : 1) << 2) |
  316. (((tpar == 3) ? 2 : 1) << 4) |
  317. (tras_clocks << 8));
  318. #ifdef CONFIG_ECC
  319. /* Setup ECC */
  320. if (ecc == 2) tmp |= 1<<13;
  321. #endif /* CONFIG_ECC */
  322. GT_REG_WRITE(SDRAM_TIMING, tmp);
  323. DP(printf("SDRAM timing: %08x (%d,%d,%d,%d)\n",
  324. GTREGREAD(SDRAM_TIMING), tpar,tpar,tpar,tras_clocks));
  325. /* SDRAM address decode register */
  326. /* program this with the default value */
  327. GT_REG_WRITE(SDRAM_ADDRESS_DECODE, 0x2);
  328. DP(printf("SDRAM decode: %08x\n",
  329. GTREGREAD(SDRAM_ADDRESS_DECODE)));
  330. return 0;
  331. }
  332. /* sets up the GT properly with information passed in */
  333. static int
  334. setup_sdram(sdram_info_t *info)
  335. {
  336. ulong tmp, check;
  337. ulong *addr = 0;
  338. int i;
  339. /* sanity checking */
  340. if (! info->banks) return 0;
  341. /* ---------------------------- */
  342. /* Program the GT with the discovered data */
  343. /* bank parameters */
  344. tmp = (0xf<<16); /* leave all virt bank pages open */
  345. DP(printf("drb_size: %d\n", info->drb_size));
  346. switch (info->drb_size) {
  347. case 1:
  348. tmp |= (1 << 14);
  349. break;
  350. case 4:
  351. case 8:
  352. tmp |= (2 << 14);
  353. break;
  354. case 16:
  355. case 32:
  356. tmp |= (3 << 14);
  357. break;
  358. default:
  359. printf("Error in dram size calculation\n");
  360. return 1;
  361. }
  362. /* SDRAM bank parameters */
  363. /* the param registers for slot 1 (banks 2+3) are offset by 0x8 */
  364. GT_REG_WRITE(SDRAM_BANK0PARAMETERS + (info->slot * 0x8), tmp);
  365. GT_REG_WRITE(SDRAM_BANK1PARAMETERS + (info->slot * 0x8), tmp);
  366. DP(printf("SDRAM bankparam slot %d (bank %d+%d): %08lx\n", info->slot, info->slot*2, (info->slot*2)+1, tmp));
  367. /* set the SDRAM configuration for each bank */
  368. for (i = info->slot * 2; i < ((info->slot * 2) + info->banks); i++) {
  369. DP(printf("*** Running a MRS cycle for bank %d ***\n", i));
  370. /* map the bank */
  371. memory_map_bank(i, 0, GB/4);
  372. /* set SDRAM mode */
  373. GT_REG_WRITE(SDRAM_OPERATION_MODE, 0x3);
  374. check = GTREGREAD(SDRAM_OPERATION_MODE);
  375. /* dummy write */
  376. *addr = 0;
  377. /* wait for the command to complete */
  378. while ((GTREGREAD(SDRAM_OPERATION_MODE) & (1 << 31)) == 0)
  379. ;
  380. /* switch back to normal operation mode */
  381. GT_REG_WRITE(SDRAM_OPERATION_MODE, 0);
  382. check = GTREGREAD(SDRAM_OPERATION_MODE);
  383. /* unmap the bank */
  384. memory_map_bank(i, 0, 0);
  385. DP(printf("*** MRS cycle for bank %d done ***\n", i));
  386. }
  387. return 0;
  388. }
  389. /*
  390. * Check memory range for valid RAM. A simple memory test determines
  391. * the actually available RAM size between addresses `base' and
  392. * `base + maxsize'. Some (not all) hardware errors are detected:
  393. * - short between address lines
  394. * - short between data lines
  395. */
  396. static long int
  397. dram_size(long int *base, long int maxsize)
  398. {
  399. volatile long int *addr, *b=base;
  400. long int cnt, val, save1, save2;
  401. #define STARTVAL (1<<20) /* start test at 1M */
  402. for (cnt = STARTVAL/sizeof(long); cnt < maxsize/sizeof(long); cnt <<= 1) {
  403. addr = base + cnt; /* pointer arith! */
  404. save1=*addr; /* save contents of addr */
  405. save2=*b; /* save contents of base */
  406. *addr=cnt; /* write cnt to addr */
  407. *b=0; /* put null at base */
  408. /* check at base address */
  409. if ((*b) != 0) {
  410. *addr=save1; /* restore *addr */
  411. *b=save2; /* restore *b */
  412. return (0);
  413. }
  414. val = *addr; /* read *addr */
  415. *addr=save1;
  416. *b=save2;
  417. if (val != cnt) {
  418. /* fix boundary condition.. STARTVAL means zero */
  419. if(cnt==STARTVAL/sizeof(long)) cnt=0;
  420. return (cnt * sizeof(long));
  421. }
  422. }
  423. return maxsize;
  424. }
  425. /* ------------------------------------------------------------------------- */
  426. /* U-Boot interface function to SDRAM init - this is where all the
  427. * controlling logic happens */
  428. long int
  429. initdram(int board_type)
  430. {
  431. ulong checkbank[4] = { [0 ... 3] = 0 };
  432. int bank_no;
  433. ulong total;
  434. int nhr;
  435. sdram_info_t dimm_info[2];
  436. /* first, use the SPD to get info about the SDRAM */
  437. /* check the NHR bit and skip mem init if it's already done */
  438. nhr = get_hid0() & (1 << 16);
  439. if (nhr) {
  440. printf("Skipping SDRAM setup due to NHR bit being set\n");
  441. } else {
  442. /* DIMM0 */
  443. check_dimm(0, &dimm_info[0]);
  444. /* DIMM1 */
  445. #ifndef CONFIG_EVB64260_750CX /* EVB64260_750CX has only 1 DIMM */
  446. check_dimm(1, &dimm_info[1]);
  447. #else /* CONFIG_EVB64260_750CX */
  448. memset(&dimm_info[1], 0, sizeof(sdram_info_t));
  449. #endif
  450. /* unmap all banks */
  451. memory_map_bank(0, 0, 0);
  452. memory_map_bank(1, 0, 0);
  453. memory_map_bank(2, 0, 0);
  454. memory_map_bank(3, 0, 0);
  455. /* Now, program the GT with the correct values */
  456. if (setup_sdram_common(dimm_info)) {
  457. printf("Setup common failed.\n");
  458. }
  459. if (setup_sdram(&dimm_info[0])) {
  460. printf("Setup for DIMM1 failed.\n");
  461. }
  462. if (setup_sdram(&dimm_info[1])) {
  463. printf("Setup for DIMM2 failed.\n");
  464. }
  465. /* set the NHR bit */
  466. set_hid0(get_hid0() | (1 << 16));
  467. }
  468. /* next, size the SDRAM banks */
  469. total = 0;
  470. if (dimm_info[0].banks > 0) checkbank[0] = 1;
  471. if (dimm_info[0].banks > 1) checkbank[1] = 1;
  472. if (dimm_info[0].banks > 2)
  473. printf("Error, SPD claims DIMM1 has >2 banks\n");
  474. if (dimm_info[1].banks > 0) checkbank[2] = 1;
  475. if (dimm_info[1].banks > 1) checkbank[3] = 1;
  476. if (dimm_info[1].banks > 2)
  477. printf("Error, SPD claims DIMM2 has >2 banks\n");
  478. /* Generic dram sizer: works even if we don't have i2c DIMMs,
  479. * as long as the timing settings are more or less correct */
  480. /*
  481. * pass 1: size all the banks, using first bat (0-256M)
  482. * limitation: we only support 256M per bank due to
  483. * us only having 1 BAT for all DRAM
  484. */
  485. for (bank_no = 0; bank_no < CFG_DRAM_BANKS; bank_no++) {
  486. /* skip over banks that are not populated */
  487. if (! checkbank[bank_no])
  488. continue;
  489. DP(printf("checking bank %d\n", bank_no));
  490. memory_map_bank(bank_no, 0, GB/4);
  491. checkbank[bank_no] = dram_size(NULL, GB/4);
  492. memory_map_bank(bank_no, 0, 0);
  493. DP(printf("bank %d %08lx\n", bank_no, checkbank[bank_no]));
  494. }
  495. /*
  496. * pass 2: contiguously map each bank into physical address
  497. * space.
  498. */
  499. dimm_info[0].banks=dimm_info[1].banks=0;
  500. for (bank_no = 0; bank_no < CFG_DRAM_BANKS; bank_no++) {
  501. if(!checkbank[bank_no]) continue;
  502. dimm_info[bank_no/2].banks++;
  503. dimm_info[bank_no/2].size+=checkbank[bank_no];
  504. memory_map_bank(bank_no, total, checkbank[bank_no]);
  505. #ifdef MAP_PCI
  506. memory_map_bank_pci(bank_no, total, checkbank[bank_no]);
  507. #endif
  508. total += checkbank[bank_no];
  509. }
  510. #ifdef CONFIG_ECC
  511. #ifdef CONFIG_ZUMA_V2
  512. /*
  513. * We always enable ECC when bank 2 and 3 are unpopulated
  514. * If we 2 or 3 are populated, we CAN'T support ECC.
  515. * (Zuma boards only support ECC in banks 0 and 1; assume that
  516. * in that configuration, ECC chips are mounted, even for stacked
  517. * chips)
  518. */
  519. if (checkbank[2]==0 && checkbank[3]==0) {
  520. dimm_info[0].ecc=2;
  521. GT_REG_WRITE(SDRAM_TIMING, GTREGREAD(SDRAM_TIMING) | (1 << 13));
  522. /* TODO: do we have to run MRS cycles again? */
  523. }
  524. #endif /* CONFIG_ZUMA_V2 */
  525. if (GTREGREAD(SDRAM_TIMING) & (1 << 13)) {
  526. puts("[ECC] ");
  527. }
  528. #endif /* CONFIG_ECC */
  529. #ifdef DEBUG
  530. dump_dimm_info(&dimm_info[0]);
  531. dump_dimm_info(&dimm_info[1]);
  532. #endif
  533. /* TODO: return at MOST 256M? */
  534. /* return total > GB/4 ? GB/4 : total; */
  535. return total;
  536. }