mem.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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/clock.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. if (!early && (((mtype = get_mem_type()) == DDR_COMBO)||(mtype == DDR_STACKED))) {
  180. if(mtype == DDR_COMBO){
  181. pmask = BIT2;/* combo part has a shared CKE signal, can't use feature */
  182. pass_type = COMBO_DDR; /* CS1 config */
  183. __raw_writel((__raw_readl(SDRC_POWER)) & ~pmask, SDRC_POWER);
  184. }
  185. if(rev != CPU_2420_2422_ES1) /* for es2 and above smooth things out */
  186. make_cs1_contiguous();
  187. }
  188. next_mem_type:
  189. if (common) { /* do a SDRC reset between types to clear regs*/
  190. __raw_writel(SOFTRESET, SDRC_SYSCONFIG); /* reset sdrc */
  191. wait_on_value(BIT0, BIT0, SDRC_STATUS, 12000000);/* wait till reset done set */
  192. __raw_writel(0, SDRC_SYSCONFIG); /* clear soft reset */
  193. __raw_writel(sdata->sdrc_sharing, SDRC_SHARING);
  194. #ifdef POWER_SAVE
  195. __raw_writel(__raw_readl(SMS_SYSCONFIG)|SMART_IDLE, SMS_SYSCONFIG);
  196. __raw_writel(sdata->sdrc_sharing|SMART_IDLE, SDRC_SHARING);
  197. __raw_writel((__raw_readl(SDRC_POWER)|BIT6), SDRC_POWER);
  198. #endif
  199. }
  200. if ((pass_type == IP_DDR) || (pass_type == STACKED)) /* (IP ddr-CS0),(2422-CS0/CS1) */
  201. __raw_writel(sdata->sdrc_mdcfg_0_ddr, SDRC_MCFG_0+offset);
  202. else if (pass_type == COMBO_DDR){ /* (combo-CS0/CS1) */
  203. __raw_writel(H4_2420_COMBO_MDCFG_0_DDR,SDRC_MCFG_0+offset);
  204. } else if (pass_type == IP_SDR){ /* ip sdr-CS0 */
  205. __raw_writel(sdata->sdrc_mdcfg_0_sdr, SDRC_MCFG_0+offset);
  206. }
  207. a = sdata->sdrc_actim_ctrla_0;
  208. b = sdata->sdrc_actim_ctrlb_0;
  209. r = sdata->sdrc_dllab_ctrl;
  210. /* work around ES1 DDR issues */
  211. if((pass_type != IP_SDR) && (rev == CPU_2420_2422_ES1)){
  212. a = H4_242x_SDRC_ACTIM_CTRLA_0_ES1;
  213. b = H4_242x_SDRC_ACTIM_CTRLB_0_ES1;
  214. r = H4_242x_SDRC_RFR_CTRL_ES1;
  215. }
  216. if (cs0) {
  217. __raw_writel(a, SDRC_ACTIM_CTRLA_0);
  218. __raw_writel(b, SDRC_ACTIM_CTRLB_0);
  219. } else {
  220. __raw_writel(a, SDRC_ACTIM_CTRLA_1);
  221. __raw_writel(b, SDRC_ACTIM_CTRLB_1);
  222. }
  223. __raw_writel(r, SDRC_RFR_CTRL+offset);
  224. /* init sequence for mDDR/mSDR using manual commands (DDR is a bit different) */
  225. __raw_writel(CMD_NOP, SDRC_MANUAL_0+offset);
  226. sdelay(5000); /* susposed to be 100us per design spec for mddr/msdr */
  227. __raw_writel(CMD_PRECHARGE, SDRC_MANUAL_0+offset);
  228. __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_0+offset);
  229. __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_0+offset);
  230. /*
  231. * CSx SDRC Mode Register
  232. * Burst length = (4 - DDR) (2-SDR)
  233. * Serial mode
  234. * CAS latency = x
  235. */
  236. if(pass_type == IP_SDR)
  237. __raw_writel(sdata->sdrc_mr_0_sdr, SDRC_MR_0+offset);
  238. else
  239. __raw_writel(sdata->sdrc_mr_0_ddr, SDRC_MR_0+offset);
  240. /* NOTE: ES1 242x _BUG_ DLL + External Bandwidth fix*/
  241. if (rev == CPU_2420_2422_ES1){
  242. dllen = (BIT0|BIT3); /* es1 clear both bit0 and bit3 */
  243. __raw_writel((__raw_readl(SMS_CLASS_ARB0)|BURSTCOMPLETE_GROUP7)
  244. ,SMS_CLASS_ARB0);/* enable bust complete for lcd */
  245. }
  246. else
  247. dllen = BIT0|BIT1; /* es2, clear bit0, and 1 (set phase to 72) */
  248. /* enable & load up DLL with good value for 75MHz, and set phase to 90
  249. * ES1 recommends 90 phase, ES2 recommends 72 phase.
  250. */
  251. if (common && (pass_type != IP_SDR)) {
  252. __raw_writel(sdata->sdrc_dllab_ctrl, SDRC_DLLA_CTRL);
  253. __raw_writel(sdata->sdrc_dllab_ctrl & ~(BIT2|dllen), SDRC_DLLA_CTRL);
  254. __raw_writel(sdata->sdrc_dllab_ctrl, SDRC_DLLB_CTRL);
  255. __raw_writel(sdata->sdrc_dllab_ctrl & ~(BIT2|dllen) , SDRC_DLLB_CTRL);
  256. }
  257. sdelay(90000);
  258. if(mem_ok())
  259. return; /* STACKED, other configued type */
  260. ++pass_type; /* IPDDR->COMBODDR->IPSDR for CS0 */
  261. goto next_mem_type;
  262. }
  263. /*****************************************************
  264. * gpmc_init(): init gpmc bus
  265. * Init GPMC for x16, MuxMode (SDRAM in x32).
  266. * This code can only be executed from SRAM or SDRAM.
  267. *****************************************************/
  268. void gpmc_init(void)
  269. {
  270. u32 mux=0, mtype, mwidth, rev, tval;
  271. rev = get_cpu_rev();
  272. if (rev == CPU_2420_2422_ES1)
  273. tval = 1;
  274. else
  275. tval = 0; /* disable bit switched meaning */
  276. /* global settings */
  277. __raw_writel(0x10, GPMC_SYSCONFIG); /* smart idle */
  278. __raw_writel(0x0, GPMC_IRQENABLE); /* isr's sources masked */
  279. __raw_writel(tval, GPMC_TIMEOUT_CONTROL);/* timeout disable */
  280. #ifdef CONFIG_SYS_NAND_BOOT
  281. __raw_writel(0x001, GPMC_CONFIG); /* set nWP, disable limited addr */
  282. #else
  283. __raw_writel(0x111, GPMC_CONFIG); /* set nWP, disable limited addr */
  284. #endif
  285. /* discover bus connection from sysboot */
  286. if (is_gpmc_muxed() == GPMC_MUXED)
  287. mux = BIT9;
  288. mtype = get_gpmc0_type();
  289. mwidth = get_gpmc0_width();
  290. /* setup cs0 */
  291. __raw_writel(0x0, GPMC_CONFIG7_0); /* disable current map */
  292. sdelay(1000);
  293. #ifdef CONFIG_SYS_NAND_BOOT
  294. __raw_writel(H4_24XX_GPMC_CONFIG1_0|mtype|mwidth, GPMC_CONFIG1_0);
  295. #else
  296. __raw_writel(H4_24XX_GPMC_CONFIG1_0|mux|mtype|mwidth, GPMC_CONFIG1_0);
  297. #endif
  298. #ifdef PRCM_CONFIG_III
  299. __raw_writel(H4_24XX_GPMC_CONFIG2_0, GPMC_CONFIG2_0);
  300. #endif
  301. __raw_writel(H4_24XX_GPMC_CONFIG3_0, GPMC_CONFIG3_0);
  302. __raw_writel(H4_24XX_GPMC_CONFIG4_0, GPMC_CONFIG4_0);
  303. #ifdef PRCM_CONFIG_III
  304. __raw_writel(H4_24XX_GPMC_CONFIG5_0, GPMC_CONFIG5_0);
  305. __raw_writel(H4_24XX_GPMC_CONFIG6_0, GPMC_CONFIG6_0);
  306. #endif
  307. __raw_writel(H4_24XX_GPMC_CONFIG7_0, GPMC_CONFIG7_0);/* enable new mapping */
  308. sdelay(2000);
  309. /* setup cs1 */
  310. __raw_writel(0, GPMC_CONFIG7_1); /* disable any mapping */
  311. sdelay(1000);
  312. __raw_writel(H4_24XX_GPMC_CONFIG1_1|mux, GPMC_CONFIG1_1);
  313. __raw_writel(H4_24XX_GPMC_CONFIG2_1, GPMC_CONFIG2_1);
  314. __raw_writel(H4_24XX_GPMC_CONFIG3_1, GPMC_CONFIG3_1);
  315. __raw_writel(H4_24XX_GPMC_CONFIG4_1, GPMC_CONFIG4_1);
  316. __raw_writel(H4_24XX_GPMC_CONFIG5_1, GPMC_CONFIG5_1);
  317. __raw_writel(H4_24XX_GPMC_CONFIG6_1, GPMC_CONFIG6_1);
  318. __raw_writel(H4_24XX_GPMC_CONFIG7_1, GPMC_CONFIG7_1); /* enable mapping */
  319. sdelay(2000);
  320. }