memory.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * (C) Copyright 2007
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  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. #include <common.h>
  24. #include <asm/processor.h>
  25. #define CRAM_BANK0_BASE 0x0
  26. #define CRAM_DIDR 0x00100000
  27. #define MICRON_MT45W8MW16BGX_CRAM_ID 0x1b431b43
  28. #define MICRON_MT45W8MW16BGX_CRAM_ID2 0x13431343
  29. #define MICRON_DIDR_VENDOR_ID 0x00030003 /* 00011b */
  30. #define CRAM_DIDR_VENDOR_ID_MASK 0x001f001f /* DIDR[4:0] */
  31. #define CRAM_DEVID_NOT_SUPPORTED 0x00000000
  32. #define PSRAM_PASS 0x50415353 /* "PASS" */
  33. #define PSRAM_FAIL 0x4641494C /* "FAIL" */
  34. static u32 is_cram_inited(void);
  35. static u32 is_cram(void);
  36. static long int cram_init(u32);
  37. static void cram_bcr_write(u32);
  38. void udelay (unsigned long);
  39. void sdram_init(void)
  40. {
  41. volatile unsigned long spr_reg;
  42. /*
  43. * If CRAM not initialized or CRAM looks initialized because this
  44. * is after a warm reboot then set SPRG7 to indicate CRAM needs
  45. * initialization. Note that CRAM is initialized by the SPI and
  46. * NAND preloader.
  47. */
  48. spr_reg = (volatile unsigned long) mfspr(SPRG6);
  49. if ((is_cram_inited() != 1) || (spr_reg != LOAK_SPL)) {
  50. mtspr(SPRG7, LOAK_NONE); /* "NONE" */
  51. }
  52. #if 1
  53. /*
  54. * When running the NAND SPL, the normal EBC configuration is not
  55. * done, so We need to enable EPLD access on EBC_CS_2 and the memory
  56. * on EBC_CS_3
  57. */
  58. /* Enable CPLD - Needed for PSRAM Access */
  59. /* Init SDRAM by setting EBC Bank 3 for PSRAM */
  60. mtebc(pb1ap, CFG_EBC_PB1AP);
  61. mtebc(pb1cr, CFG_EBC_PB1CR);
  62. mtebc(pb2ap, CFG_EBC_PB2AP);
  63. mtebc(pb2cr, CFG_EBC_PB2CR);
  64. /* pre-boot loader code: we are in OCM */
  65. mtspr(SPRG6, LOAK_SPL); /* "SPL " */
  66. mtspr(SPRG7, LOAK_OCM); /* "OCM " */
  67. #endif
  68. return;
  69. }
  70. static void cram_bcr_write(u32 wr_val)
  71. {
  72. u32 tmp_reg;
  73. u32 val;
  74. volatile u32 gpio_reg;
  75. /* # Program CRAM write */
  76. /*
  77. * set CRAM_CRE = 0x1
  78. * set wr_val = wr_val << 2
  79. */
  80. gpio_reg = in32(GPIO1_OR);
  81. out32(GPIO1_OR, gpio_reg | 0x00000400);
  82. wr_val = wr_val << 2;
  83. /* wr_val = 0x1c048; */
  84. /*
  85. * # stop PLL clock before programming CRAM
  86. * set EPLD0_MUX_CTL.OESPR3 = 1
  87. * delay 2
  88. */
  89. /*
  90. * # CS1
  91. * read 0x00200000
  92. * #shift 2 bit left before write
  93. * set val = wr_val + 0x00200000
  94. * write dmem val 0
  95. * read 0x00200000 val
  96. * print val/8x
  97. */
  98. tmp_reg = in32(0x00200000);
  99. val = wr_val + 0x00200000;
  100. /* val = 0x0021c048; */
  101. out32(val, 0x0000);
  102. udelay(100000);
  103. val = in32(0x00200000);
  104. debug("CRAM VAL: %x for CS1 ", val);
  105. /*
  106. * # CS2
  107. * read 0x02200000
  108. * #shift 2 bit left before write
  109. * set val = wr_val + 0x02200000
  110. * write dmem val 0
  111. * read 0x02200000 val
  112. * print val/8x
  113. */
  114. tmp_reg = in32(0x02200000);
  115. val = wr_val + 0x02200000;
  116. /* val = 0x0221c048; */
  117. out32(val, 0x0000);
  118. udelay(100000);
  119. val = in32(0x02200000);
  120. debug("CRAM VAL: %x for CS2 ", val);
  121. /*
  122. * # Start PLL clock before programming CRAM
  123. * set EPLD0_MUX_CTL.OESPR3 = 0
  124. */
  125. /*
  126. * set CRAMCR = 0x1
  127. */
  128. gpio_reg = in32(GPIO1_OR);
  129. out32(GPIO1_OR, gpio_reg | 0x00000400);
  130. /*
  131. * # read CRAM config BCR ( bit19:18 = 10b )
  132. * #read 0x00200000
  133. * # 1001_1001_0001_1111 ( 991f ) =>
  134. * #10_0110_0100_0111_1100 => 2647c => 0022647c
  135. * #0011_0010_0011_1110 (323e)
  136. * #
  137. */
  138. /*
  139. * set EPLD0_MUX_CTL.CRAMCR = 0x0
  140. */
  141. gpio_reg = in32(GPIO1_OR);
  142. out32(GPIO1_OR, gpio_reg & 0xFFFFFBFF);
  143. return;
  144. }
  145. static u32 is_cram_inited()
  146. {
  147. volatile unsigned long spr_reg;
  148. /*
  149. * If CRAM is initialized already, then don't reinitialize it again.
  150. * In the case of NAND boot and SPI boot, CRAM will already be
  151. * initialized by the pre-loader
  152. */
  153. spr_reg = (volatile unsigned long) mfspr(SPRG7);
  154. if (spr_reg == LOAK_CRAM) {
  155. return 1;
  156. } else {
  157. return 0;
  158. }
  159. }
  160. /******
  161. * return 0 if not CRAM
  162. * return 1 if CRAM and it's already inited by preloader
  163. * else return cram_id (CRAM Device Identification Register)
  164. ******/
  165. static u32 is_cram(void)
  166. {
  167. u32 gpio_TCR, gpio_OSRL, gpio_OR, gpio_ISR1L;
  168. volatile u32 gpio_reg;
  169. volatile u32 cram_id = 0;
  170. if (is_cram_inited() == 1) {
  171. /* this is CRAM and it is already inited (by preloader) */
  172. cram_id = 1;
  173. } else {
  174. /*
  175. * # CRAM CLOCK
  176. * set GPIO0_TCR.G8 = 1
  177. * set GPIO0_OSRL.G8 = 0
  178. * set GPIO0_OR.G8 = 0
  179. */
  180. gpio_reg = in32(GPIO0_TCR);
  181. gpio_TCR = gpio_reg;
  182. out32(GPIO0_TCR, gpio_reg | 0x00800000);
  183. gpio_reg = in32(GPIO0_OSRL);
  184. gpio_OSRL = gpio_reg;
  185. out32(GPIO0_OSRL, gpio_reg & 0xffffbfff);
  186. gpio_reg = in32(GPIO0_OR);
  187. gpio_OR = gpio_reg;
  188. out32(GPIO0_OR, gpio_reg & 0xff7fffff);
  189. /*
  190. * # CRAM Addreaa Valid
  191. * set GPIO0_TCR.G10 = 1
  192. * set GPIO0_OSRL.G10 = 0
  193. * set GPIO0_OR.G10 = 0
  194. */
  195. gpio_reg = in32(GPIO0_TCR);
  196. out32(GPIO0_TCR, gpio_reg | 0x00200000);
  197. gpio_reg = in32(GPIO0_OSRL);
  198. out32(GPIO0_OSRL, gpio_reg & 0xfffffbff);
  199. gpio_reg = in32(GPIO0_OR);
  200. out32(GPIO0_OR, gpio_reg & 0xffdfffff);
  201. /*
  202. * # config input (EBC_WAIT)
  203. * set GPIO0_ISR1L.G9 = 1
  204. * set GPIO0_TCR.G9 = 0
  205. */
  206. gpio_reg = in32(GPIO0_ISR1L);
  207. gpio_ISR1L = gpio_reg;
  208. out32(GPIO0_ISR1L, gpio_reg | 0x00001000);
  209. gpio_reg = in32(GPIO0_TCR);
  210. out32(GPIO0_TCR, gpio_reg & 0xffbfffff);
  211. /*
  212. * Enable CRE to read Registers
  213. * set GPIO0_TCR.21 = 1
  214. * set GPIO1_OR.21 = 1
  215. */
  216. gpio_reg = in32(GPIO1_TCR);
  217. out32(GPIO1_TCR, gpio_reg | 0x00000400);
  218. gpio_reg = in32(GPIO1_OR);
  219. out32(GPIO1_OR, gpio_reg | 0x00000400);
  220. /* Read Version ID */
  221. cram_id = (volatile u32) in32(CRAM_BANK0_BASE+CRAM_DIDR);
  222. udelay(100000);
  223. asm volatile(" sync");
  224. asm volatile(" eieio");
  225. debug("Cram ID: %X ", cram_id);
  226. switch (cram_id) {
  227. case MICRON_MT45W8MW16BGX_CRAM_ID:
  228. case MICRON_MT45W8MW16BGX_CRAM_ID2:
  229. /* supported CRAM vendor/part */
  230. break;
  231. case CRAM_DEVID_NOT_SUPPORTED:
  232. default:
  233. /* check for DIDR Vendor ID of Micron */
  234. if ((cram_id & CRAM_DIDR_VENDOR_ID_MASK) ==
  235. MICRON_DIDR_VENDOR_ID)
  236. {
  237. /* supported CRAM vendor */
  238. break;
  239. }
  240. /* this is not CRAM or not supported CRAM vendor/part */
  241. cram_id = 0;
  242. /*
  243. * reset the GPIO registers to the values that were
  244. * there before this routine
  245. */
  246. out32(GPIO0_TCR, gpio_TCR);
  247. out32(GPIO0_OSRL, gpio_OSRL);
  248. out32(GPIO0_OR, gpio_OR);
  249. out32(GPIO0_ISR1L, gpio_ISR1L);
  250. break;
  251. }
  252. }
  253. return cram_id;
  254. }
  255. static long int cram_init(u32 already_inited)
  256. {
  257. volatile u32 tmp_reg;
  258. u32 cram_wr_val;
  259. if (already_inited == 0) return 0;
  260. /*
  261. * If CRAM is initialized already, then don't reinitialize it again.
  262. * In the case of NAND boot and SPI boot, CRAM will already be
  263. * initialized by the pre-loader
  264. */
  265. if (already_inited != 1) {
  266. /*
  267. * #o CRAM Card
  268. * # - CRAMCRE @reg16 = 1; for CRAM to use
  269. * # - CRAMCRE @reg16 = 0; for CRAM to program
  270. *
  271. * # enable CRAM SEL, move from setEPLD.cmd
  272. * set EPLD0_MUX_CTL.OECRAM = 0
  273. * set EPLD0_MUX_CTL.CRAMCR = 1
  274. * set EPLD0_ETHRSTBOOT.SLCRAM = 0
  275. * #end
  276. */
  277. /*
  278. * #1. EBC need to program READY, CLK, ADV for ASync mode
  279. * # config output
  280. */
  281. /*
  282. * # CRAM CLOCK
  283. * set GPIO0_TCR.G8 = 1
  284. * set GPIO0_OSRL.G8 = 0
  285. * set GPIO0_OR.G8 = 0
  286. */
  287. tmp_reg = in32(GPIO0_TCR);
  288. out32(GPIO0_TCR, tmp_reg | 0x00800000);
  289. tmp_reg = in32(GPIO0_OSRL);
  290. out32(GPIO0_OSRL, tmp_reg & 0xffffbfff);
  291. tmp_reg = in32(GPIO0_OR);
  292. out32(GPIO0_OR, tmp_reg & 0xff7fffff);
  293. /*
  294. * # CRAM Addreaa Valid
  295. * set GPIO0_TCR.G10 = 1
  296. * set GPIO0_OSRL.G10 = 0
  297. * set GPIO0_OR.G10 = 0
  298. */
  299. tmp_reg = in32(GPIO0_TCR);
  300. out32(GPIO0_TCR, tmp_reg | 0x00200000);
  301. tmp_reg = in32(GPIO0_OSRL);
  302. out32(GPIO0_OSRL, tmp_reg & 0xfffffbff);
  303. tmp_reg = in32(GPIO0_OR);
  304. out32(GPIO0_OR, tmp_reg & 0xffdfffff);
  305. /*
  306. * # config input (EBC_WAIT)
  307. * set GPIO0_ISR1L.G9 = 1
  308. * set GPIO0_TCR.G9 = 0
  309. */
  310. tmp_reg = in32(GPIO0_ISR1L);
  311. out32(GPIO0_ISR1L, tmp_reg | 0x00001000);
  312. tmp_reg = in32(GPIO0_TCR);
  313. out32(GPIO0_TCR, tmp_reg & 0xffbfffff);
  314. /*
  315. * # config CS4 from GPIO
  316. * set GPIO0_TCR.G0 = 1
  317. * set GPIO0_OSRL.G0 = 1
  318. */
  319. tmp_reg = in32(GPIO0_TCR);
  320. out32(GPIO0_TCR, tmp_reg | 0x80000000);
  321. tmp_reg = in32(GPIO0_OSRL);
  322. out32(GPIO0_OSRL, tmp_reg | 0x40000000);
  323. /*
  324. * #2. EBC in Async mode
  325. * # set EBC0_PB1AP = 0x078f0ec0
  326. * set EBC0_PB1AP = 0x078f1ec0
  327. * set EBC0_PB2AP = 0x078f1ec0
  328. */
  329. mtebc(pb1ap, 0x078F1EC0);
  330. mtebc(pb2ap, 0x078F1EC0);
  331. /*
  332. * #set EBC0_PB1CR = 0x000bc000
  333. * #enable CS2 for CRAM
  334. * set EBC0_PB2CR = 0x020bc000
  335. */
  336. mtebc(pb1cr, 0x000BC000);
  337. mtebc(pb2cr, 0x020BC000);
  338. /*
  339. * #3. set CRAM in Sync mode
  340. * #exec cm_bcr_write.cmd { 0x701f }
  341. * #3. set CRAM in Sync mode (full drv strength)
  342. * exec cm_bcr_write.cmd { 0x701F }
  343. */
  344. cram_wr_val = 0x7012; /* CRAM burst setting */
  345. cram_bcr_write(cram_wr_val);
  346. /*
  347. * #4. EBC in Sync mode
  348. * #set EBC0_PB1AP = 0x9f800fc0
  349. * #set EBC0_PB1AP = 0x900001c0
  350. * set EBC0_PB2AP = 0x9C0201c0
  351. * set EBC0_PB2AP = 0x9C0201c0
  352. */
  353. mtebc(pb1ap, 0x9C0201C0);
  354. mtebc(pb2ap, 0x9C0201C0);
  355. /*
  356. * #5. EBC need to program READY, CLK, ADV for Sync mode
  357. * # config output
  358. * set GPIO0_TCR.G8 = 1
  359. * set GPIO0_OSRL.G8 = 1
  360. * set GPIO0_TCR.G10 = 1
  361. * set GPIO0_OSRL.G10 = 1
  362. */
  363. tmp_reg = in32(GPIO0_TCR);
  364. out32(GPIO0_TCR, tmp_reg | 0x00800000);
  365. tmp_reg = in32(GPIO0_OSRL);
  366. out32(GPIO0_OSRL, tmp_reg | 0x00004000);
  367. tmp_reg = in32(GPIO0_TCR);
  368. out32(GPIO0_TCR, tmp_reg | 0x00200000);
  369. tmp_reg = in32(GPIO0_OSRL);
  370. out32(GPIO0_OSRL, tmp_reg | 0x00000400);
  371. /*
  372. * # config input
  373. * set GPIO0_ISR1L.G9 = 1
  374. * set GPIO0_TCR.G9 = 0
  375. */
  376. tmp_reg = in32(GPIO0_ISR1L);
  377. out32(GPIO0_ISR1L, tmp_reg | 0x00001000);
  378. tmp_reg = in32(GPIO0_TCR);
  379. out32(GPIO0_TCR, tmp_reg & 0xffbfffff);
  380. /*
  381. * # config EBC to use RDY
  382. * set SDR0_ULTRA0.EBCREN = 1
  383. */
  384. mfsdr(sdrultra0, tmp_reg);
  385. mtsdr(sdrultra0, tmp_reg | 0x04000000);
  386. /*
  387. * set EPLD0_MUX_CTL.OESPR3 = 0
  388. */
  389. mtspr(SPRG7, LOAK_CRAM); /* "CRAM" */
  390. } /* if (already_inited != 1) */
  391. return (64 * 1024 * 1024);
  392. }
  393. /******
  394. * return 0 if not PSRAM
  395. * return 1 if is PSRAM
  396. ******/
  397. static int is_psram(u32 addr)
  398. {
  399. u32 test_pattern = 0xdeadbeef;
  400. volatile u32 readback;
  401. if (addr == CFG_SDRAM_BASE) {
  402. /* This is to temp enable OE for PSRAM */
  403. out16(EPLD_BASE+EPLD_MUXOE, 0x7f0f);
  404. udelay(10000);
  405. }
  406. out32(addr, test_pattern);
  407. asm volatile(" sync");
  408. asm volatile(" eieio");
  409. readback = (volatile u32) in32(addr);
  410. asm volatile(" sync");
  411. asm volatile(" eieio");
  412. if (readback == test_pattern) {
  413. return 1;
  414. } else {
  415. return 0;
  416. }
  417. }
  418. static long int psram_init(void)
  419. {
  420. u32 readback;
  421. long psramsize = 0;
  422. int i;
  423. /* This is to temp enable OE for PSRAM */
  424. out16(EPLD_BASE+EPLD_MUXOE, 0x7f0f);
  425. udelay(10000);
  426. /*
  427. * PSRAM bank 1: read then write to address 0x00000000
  428. */
  429. for (i = 0; i < 100; i++) {
  430. if (is_psram(CFG_SDRAM_BASE + (i*256)) == 1) {
  431. readback = PSRAM_PASS;
  432. } else {
  433. readback = PSRAM_FAIL;
  434. break;
  435. }
  436. }
  437. if (readback == PSRAM_PASS) {
  438. debug("psram_init(bank0): pass\n");
  439. psramsize = (16 * 1024 * 1024);
  440. } else {
  441. debug("psram_init(bank0): fail\n");
  442. return 0;
  443. }
  444. #if 0
  445. /*
  446. * PSRAM bank 1: read then write to address 0x01000000
  447. */
  448. for (i = 0; i < 100; i++) {
  449. if (is_psram((1 << 24) + (i*256)) == 1) {
  450. readback = PSRAM_PASS;
  451. } else {
  452. readback = PSRAM_FAIL;
  453. break;
  454. }
  455. }
  456. if (readback == PSRAM_PASS) {
  457. debug("psram_init(bank1): pass\n");
  458. psramsize = psramsize + (16 * 1024 * 1024);
  459. }
  460. #endif
  461. mtspr(SPRG7, LOAK_PSRAM); /* "PSRA" - PSRAM */
  462. return psramsize;
  463. }
  464. long int initdram(int board_type)
  465. {
  466. long int sram_size;
  467. u32 cram_inited;
  468. /* Determine Attached Memory Expansion Card*/
  469. cram_inited = is_cram();
  470. if (cram_inited != 0) { /* CRAM */
  471. debug("CRAM Expansion Card attached\n");
  472. sram_size = cram_init(cram_inited);
  473. } else if (is_psram(CFG_SDRAM_BASE+4) == 1) { /* PSRAM */
  474. debug("PSRAM Expansion Card attached\n");
  475. sram_size = psram_init();
  476. } else { /* no SRAM */
  477. debug("No Memory Card Attached!!\n");
  478. sram_size = 0;
  479. }
  480. return sram_size;
  481. }
  482. int testdram(void)
  483. {
  484. return (0);
  485. }