memory.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. /*
  268. * #o CRAM Card
  269. * # - CRAMCRE @reg16 = 1; for CRAM to use
  270. * # - CRAMCRE @reg16 = 0; for CRAM to program
  271. *
  272. * # enable CRAM SEL, move from setEPLD.cmd
  273. * set EPLD0_MUX_CTL.OECRAM = 0
  274. * set EPLD0_MUX_CTL.CRAMCR = 1
  275. * set EPLD0_ETHRSTBOOT.SLCRAM = 0
  276. * #end
  277. */
  278. /*
  279. * #1. EBC need to program READY, CLK, ADV for ASync mode
  280. * # config output
  281. */
  282. /*
  283. * # CRAM CLOCK
  284. * set GPIO0_TCR.G8 = 1
  285. * set GPIO0_OSRL.G8 = 0
  286. * set GPIO0_OR.G8 = 0
  287. */
  288. tmp_reg = in32(GPIO0_TCR);
  289. out32(GPIO0_TCR, tmp_reg | 0x00800000);
  290. tmp_reg = in32(GPIO0_OSRL);
  291. out32(GPIO0_OSRL, tmp_reg & 0xffffbfff);
  292. tmp_reg = in32(GPIO0_OR);
  293. out32(GPIO0_OR, tmp_reg & 0xff7fffff);
  294. /*
  295. * # CRAM Addreaa Valid
  296. * set GPIO0_TCR.G10 = 1
  297. * set GPIO0_OSRL.G10 = 0
  298. * set GPIO0_OR.G10 = 0
  299. */
  300. tmp_reg = in32(GPIO0_TCR);
  301. out32(GPIO0_TCR, tmp_reg | 0x00200000);
  302. tmp_reg = in32(GPIO0_OSRL);
  303. out32(GPIO0_OSRL, tmp_reg & 0xfffffbff);
  304. tmp_reg = in32(GPIO0_OR);
  305. out32(GPIO0_OR, tmp_reg & 0xffdfffff);
  306. /*
  307. * # config input (EBC_WAIT)
  308. * set GPIO0_ISR1L.G9 = 1
  309. * set GPIO0_TCR.G9 = 0
  310. */
  311. tmp_reg = in32(GPIO0_ISR1L);
  312. out32(GPIO0_ISR1L, tmp_reg | 0x00001000);
  313. tmp_reg = in32(GPIO0_TCR);
  314. out32(GPIO0_TCR, tmp_reg & 0xffbfffff);
  315. /*
  316. * # config CS4 from GPIO
  317. * set GPIO0_TCR.G0 = 1
  318. * set GPIO0_OSRL.G0 = 1
  319. */
  320. tmp_reg = in32(GPIO0_TCR);
  321. out32(GPIO0_TCR, tmp_reg | 0x80000000);
  322. tmp_reg = in32(GPIO0_OSRL);
  323. out32(GPIO0_OSRL, tmp_reg | 0x40000000);
  324. /*
  325. * #2. EBC in Async mode
  326. * # set EBC0_PB1AP = 0x078f0ec0
  327. * set EBC0_PB1AP = 0x078f1ec0
  328. * set EBC0_PB2AP = 0x078f1ec0
  329. */
  330. mtebc(pb1ap, 0x078F1EC0);
  331. mtebc(pb2ap, 0x078F1EC0);
  332. /*
  333. * #set EBC0_PB1CR = 0x000bc000
  334. * #enable CS2 for CRAM
  335. * set EBC0_PB2CR = 0x020bc000
  336. */
  337. mtebc(pb1cr, 0x000BC000);
  338. mtebc(pb2cr, 0x020BC000);
  339. /*
  340. * #3. set CRAM in Sync mode
  341. * #exec cm_bcr_write.cmd { 0x701f }
  342. * #3. set CRAM in Sync mode (full drv strength)
  343. * exec cm_bcr_write.cmd { 0x701F }
  344. */
  345. cram_wr_val = 0x7012; /* CRAM burst setting */
  346. cram_bcr_write(cram_wr_val);
  347. /*
  348. * #4. EBC in Sync mode
  349. * #set EBC0_PB1AP = 0x9f800fc0
  350. * #set EBC0_PB1AP = 0x900001c0
  351. * set EBC0_PB2AP = 0x9C0201c0
  352. * set EBC0_PB2AP = 0x9C0201c0
  353. */
  354. mtebc(pb1ap, 0x9C0201C0);
  355. mtebc(pb2ap, 0x9C0201C0);
  356. /*
  357. * #5. EBC need to program READY, CLK, ADV for Sync mode
  358. * # config output
  359. * set GPIO0_TCR.G8 = 1
  360. * set GPIO0_OSRL.G8 = 1
  361. * set GPIO0_TCR.G10 = 1
  362. * set GPIO0_OSRL.G10 = 1
  363. */
  364. tmp_reg = in32(GPIO0_TCR);
  365. out32(GPIO0_TCR, tmp_reg | 0x00800000);
  366. tmp_reg = in32(GPIO0_OSRL);
  367. out32(GPIO0_OSRL, tmp_reg | 0x00004000);
  368. tmp_reg = in32(GPIO0_TCR);
  369. out32(GPIO0_TCR, tmp_reg | 0x00200000);
  370. tmp_reg = in32(GPIO0_OSRL);
  371. out32(GPIO0_OSRL, tmp_reg | 0x00000400);
  372. /*
  373. * # config input
  374. * set GPIO0_ISR1L.G9 = 1
  375. * set GPIO0_TCR.G9 = 0
  376. */
  377. tmp_reg = in32(GPIO0_ISR1L);
  378. out32(GPIO0_ISR1L, tmp_reg | 0x00001000);
  379. tmp_reg = in32(GPIO0_TCR);
  380. out32(GPIO0_TCR, tmp_reg & 0xffbfffff);
  381. /*
  382. * # config EBC to use RDY
  383. * set SDR0_ULTRA0.EBCREN = 1
  384. */
  385. mfsdr(sdrultra0, tmp_reg);
  386. mtsdr(sdrultra0, tmp_reg | 0x04000000);
  387. /*
  388. * set EPLD0_MUX_CTL.OESPR3 = 0
  389. */
  390. mtspr(SPRG7, LOAK_CRAM); /* "CRAM" */
  391. } /* if (already_inited != 1) */
  392. return (64 * 1024 * 1024);
  393. }
  394. /******
  395. * return 0 if not PSRAM
  396. * return 1 if is PSRAM
  397. ******/
  398. static int is_psram(u32 addr)
  399. {
  400. u32 test_pattern = 0xdeadbeef;
  401. volatile u32 readback;
  402. if (addr == CFG_SDRAM_BASE) {
  403. /* This is to temp enable OE for PSRAM */
  404. out16(EPLD_BASE+EPLD_MUXOE, 0x7f0f);
  405. udelay(10000);
  406. }
  407. out32(addr, test_pattern);
  408. asm volatile(" sync");
  409. asm volatile(" eieio");
  410. readback = (volatile u32) in32(addr);
  411. asm volatile(" sync");
  412. asm volatile(" eieio");
  413. if (readback == test_pattern) {
  414. return 1;
  415. } else {
  416. return 0;
  417. }
  418. }
  419. static long int psram_init(void)
  420. {
  421. u32 readback;
  422. long psramsize = 0;
  423. int i;
  424. /* This is to temp enable OE for PSRAM */
  425. out16(EPLD_BASE+EPLD_MUXOE, 0x7f0f);
  426. udelay(10000);
  427. /*
  428. * PSRAM bank 1: read then write to address 0x00000000
  429. */
  430. for (i = 0; i < 100; i++) {
  431. if (is_psram(CFG_SDRAM_BASE + (i*256)) == 1) {
  432. readback = PSRAM_PASS;
  433. } else {
  434. readback = PSRAM_FAIL;
  435. break;
  436. }
  437. }
  438. if (readback == PSRAM_PASS) {
  439. debug("psram_init(bank0): pass\n");
  440. psramsize = (16 * 1024 * 1024);
  441. } else {
  442. debug("psram_init(bank0): fail\n");
  443. return 0;
  444. }
  445. #if 0
  446. /*
  447. * PSRAM bank 1: read then write to address 0x01000000
  448. */
  449. for (i = 0; i < 100; i++) {
  450. if (is_psram((1 << 24) + (i*256)) == 1) {
  451. readback = PSRAM_PASS;
  452. } else {
  453. readback = PSRAM_FAIL;
  454. break;
  455. }
  456. }
  457. if (readback == PSRAM_PASS) {
  458. debug("psram_init(bank1): pass\n");
  459. psramsize = psramsize + (16 * 1024 * 1024);
  460. }
  461. #endif
  462. mtspr(SPRG7, LOAK_PSRAM); /* "PSRA" - PSRAM */
  463. return psramsize;
  464. }
  465. long int initdram(int board_type)
  466. {
  467. long int sram_size;
  468. u32 cram_inited;
  469. /* Determine Attached Memory Expansion Card*/
  470. cram_inited = is_cram();
  471. if (cram_inited != 0) { /* CRAM */
  472. debug("CRAM Expansion Card attached\n");
  473. sram_size = cram_init(cram_inited);
  474. } else if (is_psram(CFG_SDRAM_BASE+4) == 1) { /* PSRAM */
  475. debug("PSRAM Expansion Card attached\n");
  476. sram_size = psram_init();
  477. } else { /* no SRAM */
  478. debug("No Memory Card Attached!!\n");
  479. sram_size = 0;
  480. }
  481. return sram_size;
  482. }
  483. int testdram(void)
  484. {
  485. return (0);
  486. }