mem.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * (C) Copyright 2004
  3. * Texas Instruments, <www.ti.com>
  4. * Richard Woodruff <r-woodruff2@ti.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. #include <common.h>
  22. #include <asm/arch/omap2420.h>
  23. #include <asm/io.h>
  24. #include <asm/arch/bits.h>
  25. #include <asm/arch/mux.h>
  26. #include <asm/arch/mem.h>
  27. #include <asm/arch/clocks.h>
  28. #include <asm/arch/sys_proto.h>
  29. #include <asm/arch/sys_info.h>
  30. /************************************************************
  31. * sdelay() - simple spin loop. Will be constant time as
  32. * its generally used in 12MHz bypass conditions only. This
  33. * is necessary until timers are accessible.
  34. *
  35. * not inline to increase chances its in cache when called
  36. *************************************************************/
  37. void sdelay (unsigned long loops)
  38. {
  39. __asm__ volatile ("1:\n" "subs %0, %1, #1\n"
  40. "bne 1b":"=r" (loops):"0" (loops));
  41. }
  42. /*********************************************************************************
  43. * prcm_init() - inits clocks for PRCM as defined in clocks.h (config II default).
  44. * -- called from SRAM, or Flash (using temp SRAM stack).
  45. *********************************************************************************/
  46. void prcm_init(void)
  47. {
  48. u32 div;
  49. void (*f_lock_pll) (u32, u32, u32, u32);
  50. extern void *_end_vect, *_start;
  51. f_lock_pll = (void *)((u32)&_end_vect - (u32)&_start + SRAM_VECT_CODE);
  52. __raw_writel(0, CM_FCLKEN1_CORE); /* stop all clocks to reduce ringing */
  53. __raw_writel(0, CM_FCLKEN2_CORE); /* may not be necessary */
  54. __raw_writel(0, CM_ICLKEN1_CORE);
  55. __raw_writel(0, CM_ICLKEN2_CORE);
  56. __raw_writel(DPLL_OUT, CM_CLKSEL2_PLL); /* set DPLL out */
  57. __raw_writel(MPU_DIV, CM_CLKSEL_MPU); /* set MPU divider */
  58. __raw_writel(DSP_DIV, CM_CLKSEL_DSP); /* set dsp and iva dividers */
  59. __raw_writel(GFX_DIV, CM_CLKSEL_GFX); /* set gfx dividers */
  60. div = BUS_DIV;
  61. __raw_writel(div, CM_CLKSEL1_CORE);/* set L3/L4/USB/Display/Vlnc/SSi dividers */
  62. sdelay(1000);
  63. if(running_in_sram()){
  64. /* If running fully from SRAM this is OK. The Flash bus drops out for just a little.
  65. * but then comes back. If running from Flash this sequence kills you, thus you need
  66. * to run it using CONFIG_PARTIAL_SRAM.
  67. */
  68. __raw_writel(MODE_BYPASS_FAST, CM_CLKEN_PLL); /* go to bypass, fast relock */
  69. wait_on_value(BIT0|BIT1, BIT0, CM_IDLEST_CKGEN, LDELAY); /* wait till in bypass */
  70. sdelay(1000);
  71. /* set clock selection and dpll dividers. */
  72. __raw_writel(DPLL_VAL, CM_CLKSEL1_PLL); /* set pll for target rate */
  73. __raw_writel(COMMIT_DIVIDERS, PRCM_CLKCFG_CTRL); /* commit dividers */
  74. sdelay(10000);
  75. __raw_writel(DPLL_LOCK, CM_CLKEN_PLL); /* enable dpll */
  76. sdelay(10000);
  77. wait_on_value(BIT0|BIT1, BIT1, CM_IDLEST_CKGEN, LDELAY); /*wait for dpll lock */
  78. }else if(running_in_flash()){
  79. /* if running from flash, need to jump to small relocated code area in SRAM.
  80. * This is the only safe spot to do configurations from.
  81. */
  82. (*f_lock_pll)(PRCM_CLKCFG_CTRL, CM_CLKEN_PLL, DPLL_LOCK, CM_IDLEST_CKGEN);
  83. }
  84. __raw_writel(DPLL_LOCK|APLL_LOCK, CM_CLKEN_PLL); /* enable apll */
  85. wait_on_value(BIT8, BIT8, CM_IDLEST_CKGEN, LDELAY); /* wait for apll lock */
  86. sdelay(1000);
  87. }
  88. /**************************************************************************
  89. * make_cs1_contiguous() - for es2 and above remap cs1 behind cs0 to allow
  90. * command line mem=xyz use all memory with out discontigious support
  91. * compiled in. Could do it at the ATAG, but there really is two banks...
  92. * Called as part of 2nd phase DDR init.
  93. **************************************************************************/
  94. void make_cs1_contiguous(void)
  95. {
  96. u32 size, a_add_low, a_add_high;
  97. size = get_sdr_cs_size(SDRC_CS0_OSET);
  98. size /= SZ_32M; /* find size to offset CS1 */
  99. a_add_high = (size & 3) << 8; /* set up low field */
  100. a_add_low = (size & 0x3C) >> 2; /* set up high field */
  101. __raw_writel((a_add_high|a_add_low),SDRC_CS_CFG);
  102. }
  103. /********************************************************
  104. * mem_ok() - test used to see if timings are correct
  105. * for a part. Helps in gussing which part
  106. * we are currently using.
  107. *******************************************************/
  108. u32 mem_ok(void)
  109. {
  110. u32 val1, val2;
  111. u32 pattern = 0x12345678;
  112. __raw_writel(0x0,OMAP2420_SDRC_CS0+0x400); /* clear pos A */
  113. __raw_writel(pattern, OMAP2420_SDRC_CS0); /* pattern to pos B */
  114. __raw_writel(0x0,OMAP2420_SDRC_CS0+4); /* remove pattern off the bus */
  115. val1 = __raw_readl(OMAP2420_SDRC_CS0+0x400); /* get pos A value */
  116. val2 = __raw_readl(OMAP2420_SDRC_CS0); /* get val2 */
  117. if ((val1 != 0) || (val2 != pattern)) /* see if pos A value changed*/
  118. return(0);
  119. else
  120. return(1);
  121. }
  122. /********************************************************
  123. * sdrc_init() - init the sdrc chip selects CS0 and CS1
  124. * - early init routines, called from flash or
  125. * SRAM.
  126. *******************************************************/
  127. void sdrc_init(void)
  128. {
  129. #define EARLY_INIT 1
  130. do_sdrc_init(SDRC_CS0_OSET, EARLY_INIT); /* only init up first bank here */
  131. }
  132. /*************************************************************************
  133. * do_sdrc_init(): initialize the SDRAM for use.
  134. * -called from low level code with stack only.
  135. * -code sets up SDRAM timing and muxing for 2422 or 2420.
  136. * -optimal settings can be placed here, or redone after i2c
  137. * inspection of board info
  138. *
  139. * This is a bit ugly, but should handle all memory moduels
  140. * used with the H4. The first time though this code from s_init()
  141. * we configure the first chip select. Later on we come back and
  142. * will configure the 2nd chip select if it exists.
  143. *
  144. **************************************************************************/
  145. void do_sdrc_init(u32 offset, u32 early)
  146. {
  147. u32 cpu, dllen=0, rev, common=0, cs0=0, pmask=0, pass_type, mtype;
  148. sdrc_data_t *sdata; /* do not change type */
  149. u32 a, b, r;
  150. static const sdrc_data_t sdrc_2422 =
  151. {
  152. H4_2422_SDRC_SHARING, H4_2422_SDRC_MDCFG_0_DDR, 0 , H4_2422_SDRC_ACTIM_CTRLA_0,
  153. H4_2422_SDRC_ACTIM_CTRLB_0, H4_2422_SDRC_RFR_CTRL, H4_2422_SDRC_MR_0_DDR,
  154. 0, H4_2422_SDRC_DLLAB_CTRL
  155. };
  156. static const sdrc_data_t sdrc_2420 =
  157. {
  158. H4_2420_SDRC_SHARING, H4_2420_SDRC_MDCFG_0_DDR, H4_2420_SDRC_MDCFG_0_SDR,
  159. H4_2420_SDRC_ACTIM_CTRLA_0, H4_2420_SDRC_ACTIM_CTRLB_0,
  160. H4_2420_SDRC_RFR_CTRL, H4_2420_SDRC_MR_0_DDR, H4_2420_SDRC_MR_0_SDR,
  161. H4_2420_SDRC_DLLAB_CTRL
  162. };
  163. if (offset == SDRC_CS0_OSET)
  164. cs0 = common = 1; /* int regs shared between both chip select */
  165. cpu = get_cpu_type();
  166. rev = get_cpu_rev();
  167. /* warning generated, though code generation is correct. this may bite later,
  168. * but is ok for now. there is only so much C code you can do on stack only
  169. * operation.
  170. */
  171. if (cpu == CPU_2422){
  172. sdata = (sdrc_data_t *)&sdrc_2422;
  173. pass_type = STACKED;
  174. } else{
  175. sdata = (sdrc_data_t *)&sdrc_2420;
  176. pass_type = IP_DDR;
  177. }
  178. __asm__ __volatile__("": : :"memory"); /* limit compiler scope */
  179. /* u-boot is compiled to run in DDR or SRAM at 8xxxxxxx or 4xxxxxxx.
  180. * If we are running in flash prior to relocation and we use data
  181. * here which is not pc relative we need to get the address correct.
  182. * We need to find the current flash mapping to dress up the initial
  183. * pointer load. As long as this is const data we should be ok.
  184. */
  185. if((early) && running_in_flash()){
  186. sdata = (sdrc_data_t *)(((u32)sdata & 0x0003FFFF) | get_gpmc0_base());
  187. /* NOR internal boot offset is 0x4000 from xloader signature */
  188. if(running_from_internal_boot())
  189. sdata = (sdrc_data_t *)((u32)sdata + 0x4000);
  190. }
  191. if (!early && (((mtype = get_mem_type()) == DDR_COMBO)||(mtype == DDR_STACKED))) {
  192. if(mtype == DDR_COMBO){
  193. pmask = BIT2;/* combo part has a shared CKE signal, can't use feature */
  194. pass_type = COMBO_DDR; /* CS1 config */
  195. __raw_writel((__raw_readl(SDRC_POWER)) & ~pmask, SDRC_POWER);
  196. }
  197. if(rev != CPU_2420_2422_ES1) /* for es2 and above smooth things out */
  198. make_cs1_contiguous();
  199. }
  200. next_mem_type:
  201. if (common) { /* do a SDRC reset between types to clear regs*/
  202. __raw_writel(SOFTRESET, SDRC_SYSCONFIG); /* reset sdrc */
  203. wait_on_value(BIT0, BIT0, SDRC_STATUS, 12000000);/* wait till reset done set */
  204. __raw_writel(0, SDRC_SYSCONFIG); /* clear soft reset */
  205. __raw_writel(sdata->sdrc_sharing, SDRC_SHARING);
  206. #ifdef POWER_SAVE
  207. __raw_writel(__raw_readl(SMS_SYSCONFIG)|SMART_IDLE, SMS_SYSCONFIG);
  208. __raw_writel(sdata->sdrc_sharing|SMART_IDLE, SDRC_SHARING);
  209. __raw_writel((__raw_readl(SDRC_POWER)|BIT6), SDRC_POWER);
  210. #endif
  211. }
  212. if ((pass_type == IP_DDR) || (pass_type == STACKED)) /* (IP ddr-CS0),(2422-CS0/CS1) */
  213. __raw_writel(sdata->sdrc_mdcfg_0_ddr, SDRC_MCFG_0+offset);
  214. else if (pass_type == COMBO_DDR){ /* (combo-CS0/CS1) */
  215. __raw_writel(H4_2420_COMBO_MDCFG_0_DDR,SDRC_MCFG_0+offset);
  216. } else if (pass_type == IP_SDR){ /* ip sdr-CS0 */
  217. __raw_writel(sdata->sdrc_mdcfg_0_sdr, SDRC_MCFG_0+offset);
  218. }
  219. a = sdata->sdrc_actim_ctrla_0;
  220. b = sdata->sdrc_actim_ctrlb_0;
  221. r = sdata->sdrc_dllab_ctrl;
  222. /* work around ES1 DDR issues */
  223. if((pass_type != IP_SDR) && (rev == CPU_2420_2422_ES1)){
  224. a = H4_242x_SDRC_ACTIM_CTRLA_0_ES1;
  225. b = H4_242x_SDRC_ACTIM_CTRLB_0_ES1;
  226. r = H4_242x_SDRC_RFR_CTRL_ES1;
  227. }
  228. if (cs0) {
  229. __raw_writel(a, SDRC_ACTIM_CTRLA_0);
  230. __raw_writel(b, SDRC_ACTIM_CTRLB_0);
  231. } else {
  232. __raw_writel(a, SDRC_ACTIM_CTRLA_1);
  233. __raw_writel(b, SDRC_ACTIM_CTRLB_1);
  234. }
  235. __raw_writel(r, SDRC_RFR_CTRL+offset);
  236. /* init sequence for mDDR/mSDR using manual commands (DDR is a bit different) */
  237. __raw_writel(CMD_NOP, SDRC_MANUAL_0+offset);
  238. sdelay(5000); /* susposed to be 100us per design spec for mddr/msdr */
  239. __raw_writel(CMD_PRECHARGE, SDRC_MANUAL_0+offset);
  240. __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_0+offset);
  241. __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_0+offset);
  242. /*
  243. * CSx SDRC Mode Register
  244. * Burst length = (4 - DDR) (2-SDR)
  245. * Serial mode
  246. * CAS latency = x
  247. */
  248. if(pass_type == IP_SDR)
  249. __raw_writel(sdata->sdrc_mr_0_sdr, SDRC_MR_0+offset);
  250. else
  251. __raw_writel(sdata->sdrc_mr_0_ddr, SDRC_MR_0+offset);
  252. /* NOTE: ES1 242x _BUG_ DLL + External Bandwidth fix*/
  253. if (rev == CPU_2420_2422_ES1){
  254. dllen = (BIT0|BIT3); /* es1 clear both bit0 and bit3 */
  255. __raw_writel((__raw_readl(SMS_CLASS_ARB0)|BURSTCOMPLETE_GROUP7)
  256. ,SMS_CLASS_ARB0);/* enable bust complete for lcd */
  257. }
  258. else
  259. dllen = BIT0|BIT1; /* es2, clear bit0, and 1 (set phase to 72) */
  260. /* enable & load up DLL with good value for 75MHz, and set phase to 90
  261. * ES1 recommends 90 phase, ES2 recommends 72 phase.
  262. */
  263. if (common && (pass_type != IP_SDR)) {
  264. __raw_writel(sdata->sdrc_dllab_ctrl, SDRC_DLLA_CTRL);
  265. __raw_writel(sdata->sdrc_dllab_ctrl & ~(BIT2|dllen), SDRC_DLLA_CTRL);
  266. __raw_writel(sdata->sdrc_dllab_ctrl, SDRC_DLLB_CTRL);
  267. __raw_writel(sdata->sdrc_dllab_ctrl & ~(BIT2|dllen) , SDRC_DLLB_CTRL);
  268. }
  269. sdelay(90000);
  270. if(mem_ok())
  271. return; /* STACKED, other configued type */
  272. ++pass_type; /* IPDDR->COMBODDR->IPSDR for CS0 */
  273. goto next_mem_type;
  274. }
  275. /*****************************************************
  276. * gpmc_init(): init gpmc bus
  277. * Init GPMC for x16, MuxMode (SDRAM in x32).
  278. * This code can only be executed from SRAM or SDRAM.
  279. *****************************************************/
  280. void gpmc_init(void)
  281. {
  282. u32 mux=0, mtype, mwidth, rev, tval;
  283. rev = get_cpu_rev();
  284. if (rev == CPU_2420_2422_ES1)
  285. tval = 1;
  286. else
  287. tval = 0; /* disable bit switched meaning */
  288. /* global settings */
  289. __raw_writel(0x10, GPMC_SYSCONFIG); /* smart idle */
  290. __raw_writel(0x0, GPMC_IRQENABLE); /* isr's sources masked */
  291. __raw_writel(tval, GPMC_TIMEOUT_CONTROL);/* timeout disable */
  292. #ifdef CONFIG_SYS_NAND_BOOT
  293. __raw_writel(0x001, GPMC_CONFIG); /* set nWP, disable limited addr */
  294. #else
  295. __raw_writel(0x111, GPMC_CONFIG); /* set nWP, disable limited addr */
  296. #endif
  297. /* discover bus connection from sysboot */
  298. if (is_gpmc_muxed() == GPMC_MUXED)
  299. mux = BIT9;
  300. mtype = get_gpmc0_type();
  301. mwidth = get_gpmc0_width();
  302. /* setup cs0 */
  303. __raw_writel(0x0, GPMC_CONFIG7_0); /* disable current map */
  304. sdelay(1000);
  305. #ifdef CONFIG_SYS_NAND_BOOT
  306. __raw_writel(H4_24XX_GPMC_CONFIG1_0|mtype|mwidth, GPMC_CONFIG1_0);
  307. #else
  308. __raw_writel(H4_24XX_GPMC_CONFIG1_0|mux|mtype|mwidth, GPMC_CONFIG1_0);
  309. #endif
  310. #ifdef PRCM_CONFIG_III
  311. __raw_writel(H4_24XX_GPMC_CONFIG2_0, GPMC_CONFIG2_0);
  312. #endif
  313. __raw_writel(H4_24XX_GPMC_CONFIG3_0, GPMC_CONFIG3_0);
  314. __raw_writel(H4_24XX_GPMC_CONFIG4_0, GPMC_CONFIG4_0);
  315. #ifdef PRCM_CONFIG_III
  316. __raw_writel(H4_24XX_GPMC_CONFIG5_0, GPMC_CONFIG5_0);
  317. __raw_writel(H4_24XX_GPMC_CONFIG6_0, GPMC_CONFIG6_0);
  318. #endif
  319. __raw_writel(H4_24XX_GPMC_CONFIG7_0, GPMC_CONFIG7_0);/* enable new mapping */
  320. sdelay(2000);
  321. /* setup cs1 */
  322. __raw_writel(0, GPMC_CONFIG7_1); /* disable any mapping */
  323. sdelay(1000);
  324. __raw_writel(H4_24XX_GPMC_CONFIG1_1|mux, GPMC_CONFIG1_1);
  325. __raw_writel(H4_24XX_GPMC_CONFIG2_1, GPMC_CONFIG2_1);
  326. __raw_writel(H4_24XX_GPMC_CONFIG3_1, GPMC_CONFIG3_1);
  327. __raw_writel(H4_24XX_GPMC_CONFIG4_1, GPMC_CONFIG4_1);
  328. __raw_writel(H4_24XX_GPMC_CONFIG5_1, GPMC_CONFIG5_1);
  329. __raw_writel(H4_24XX_GPMC_CONFIG6_1, GPMC_CONFIG6_1);
  330. __raw_writel(H4_24XX_GPMC_CONFIG7_1, GPMC_CONFIG7_1); /* enable mapping */
  331. sdelay(2000);
  332. }