mem.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 rev,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. rev = get_cpu_rev();
  61. if (rev == CPU_2420_ES1 || rev == CPU_2422_ES1)
  62. div = BUS_DIV_ES1;
  63. else
  64. div = BUS_DIV;
  65. __raw_writel(div, CM_CLKSEL1_CORE);/* set L3/L4/USB/Display/Vlnc/SSi dividers */
  66. sdelay(1000);
  67. if(running_in_sram()){
  68. /* If running fully from SRAM this is OK. The Flash bus drops out for just a little.
  69. * but then comes back. If running from Flash this sequence kills you, thus you need
  70. * to run it using CONFIG_PARTIAL_SRAM.
  71. */
  72. __raw_writel(MODE_BYPASS_FAST, CM_CLKEN_PLL); /* go to bypass, fast relock */
  73. wait_on_value(BIT0|BIT1, BIT0, CM_IDLEST_CKGEN, LDELAY); /* wait till in bypass */
  74. sdelay(1000);
  75. /* set clock selection and dpll dividers. */
  76. __raw_writel(DPLL_VAL, CM_CLKSEL1_PLL); /* set pll for target rate */
  77. __raw_writel(COMMIT_DIVIDERS, PRCM_CLKCFG_CTRL); /* commit dividers */
  78. sdelay(10000);
  79. __raw_writel(DPLL_LOCK, CM_CLKEN_PLL); /* enable dpll */
  80. sdelay(10000);
  81. wait_on_value(BIT0|BIT1, BIT1, CM_IDLEST_CKGEN, LDELAY); /*wait for dpll lock */
  82. }else if(running_in_flash()){
  83. /* if running from flash, need to jump to small relocated code area in SRAM.
  84. * This is the only safe spot to do configurations from.
  85. */
  86. (*f_lock_pll)(PRCM_CLKCFG_CTRL, CM_CLKEN_PLL, DPLL_LOCK, CM_IDLEST_CKGEN);
  87. }
  88. __raw_writel(DPLL_LOCK|APLL_LOCK, CM_CLKEN_PLL); /* enable apll */
  89. wait_on_value(BIT8, BIT8, CM_IDLEST_CKGEN, LDELAY); /* wait for apll lock */
  90. sdelay(1000);
  91. }
  92. /********************************************************
  93. * mem_ok() - test used to see if timings are correct
  94. * for a part. Helps in gussing which part
  95. * we are currently using.
  96. *******************************************************/
  97. u32 mem_ok(void)
  98. {
  99. u32 val1, val2;
  100. u32 pattern = 0x12345678;
  101. __raw_writel(0x0,OMAP2420_SDRC_CS0+0x400); /* clear pos A */
  102. __raw_writel(pattern, OMAP2420_SDRC_CS0); /* pattern to pos B */
  103. __raw_writel(0x0,OMAP2420_SDRC_CS0+4); /* remove pattern off the bus */
  104. val1 = __raw_readl(OMAP2420_SDRC_CS0+0x400); /* get pos A value */
  105. val2 = __raw_readl(OMAP2420_SDRC_CS0); /* get val2 */
  106. if ((val1 != 0) || (val2 != pattern)) /* see if pos A value changed*/
  107. return(0);
  108. else
  109. return(1);
  110. }
  111. /********************************************************
  112. * sdrc_init() - init the sdrc chip selects CS0 and CS1
  113. * - early init routines, called from flash or
  114. * SRAM.
  115. *******************************************************/
  116. void sdrc_init(void)
  117. {
  118. #define EARLY_INIT 1
  119. do_sdrc_init(SDRC_CS0_OSET, EARLY_INIT); /* only init up first bank here */
  120. }
  121. /*************************************************************************
  122. * do_sdrc_init(): initialize the SDRAM for use.
  123. * -called from low level code with stack only.
  124. * -code sets up SDRAM timing and muxing for 2422 or 2420.
  125. * -optimal settings can be placed here, or redone after i2c
  126. * inspection of board info
  127. *
  128. * This is a bit ugly, but should handle all memory moduels
  129. * used with the H4. The first time though this code from s_init()
  130. * we configure the first chip select. Later on we come back and
  131. * will configure the 2nd chip select if it exists.
  132. *
  133. **************************************************************************/
  134. void do_sdrc_init(u32 offset, u32 early)
  135. {
  136. u32 cpu, bug=0, rev, common=0, cs0=0, pmask=0, pass_type;
  137. sdrc_data_t *sdata; /* do not change type */
  138. u32 a, b, r;
  139. static const sdrc_data_t sdrc_2422 =
  140. {
  141. H4_2422_SDRC_SHARING, H4_2422_SDRC_MDCFG_0_DDR, 0 , H4_2422_SDRC_ACTIM_CTRLA_0,
  142. H4_2422_SDRC_ACTIM_CTRLB_0, H4_2422_SDRC_RFR_CTRL_ES1, H4_2422_SDRC_MR_0_DDR,
  143. 0, H4_2422_SDRC_DLLA_CTRL, H4_2422_SDRC_DLLB_CTRL
  144. };
  145. static const sdrc_data_t sdrc_2420 =
  146. {
  147. H4_2420_SDRC_SHARING, H4_2420_SDRC_MDCFG_0_DDR, H4_2420_SDRC_MDCFG_0_SDR,
  148. H4_2420_SDRC_ACTIM_CTRLA_0, H4_2420_SDRC_ACTIM_CTRLB_0,
  149. H4_2420_SDRC_RFR_CTRL_ES1, H4_2420_SDRC_MR_0_DDR, H4_2420_SDRC_MR_0_SDR,
  150. H4_2420_SDRC_DLLA_CTRL, H4_2420_SDRC_DLLB_CTRL
  151. };
  152. if (offset == SDRC_CS0_OSET)
  153. cs0 = common = 1; /* int regs shared between both chip select */
  154. cpu = get_cpu_type();
  155. /* warning generated, though code generation is correct. this may bite later,
  156. * but is ok for now. there is only so much C code you can do on stack only
  157. * operation.
  158. */
  159. if (cpu == CPU_2422){
  160. sdata = (sdrc_data_t *)&sdrc_2422;
  161. pass_type = STACKED;
  162. } else{
  163. sdata = (sdrc_data_t *)&sdrc_2420;
  164. pass_type = IP_DDR;
  165. }
  166. __asm__ __volatile__("": : :"memory"); /* limit compiler scope */
  167. /* u-boot is compiled to run in DDR or SRAM at 8xxxxxxx or 4xxxxxxx.
  168. * If we are running in flash prior to relocation and we use data
  169. * here which is not pc relative we need to get the address correct.
  170. * We need to find the current flash mapping to dress up the initial
  171. * pointer load. As long as this is const data we should be ok.
  172. */
  173. if((early) && running_in_flash()){
  174. sdata = (sdrc_data_t *)(((u32)sdata & 0x0003FFFF) | get_gpmc0_base());
  175. /* NOR internal boot offset is 0x4000 from xloader signature */
  176. if(running_from_internal_boot())
  177. sdata = (sdrc_data_t *)((u32)sdata + 0x4000);
  178. }
  179. if (!early && (get_mem_type() == DDR_COMBO)) {/* combo part has a shared CKE signal, can't use feature */
  180. pmask = BIT2;
  181. pass_type = COMBO_DDR; /* CS1 config */
  182. }
  183. next_mem_type:
  184. if (common) { /* do a SDRC reset between types to clear regs*/
  185. __raw_writel(SOFTRESET, SDRC_SYSCONFIG); /* reset sdrc */
  186. wait_on_value(BIT0, BIT0, SDRC_STATUS, 12000000);/* wait till reset done set */
  187. __raw_writel(0, SDRC_SYSCONFIG); /* clear soft reset */
  188. __raw_writel(sdata->sdrc_sharing, SDRC_SHARING);
  189. __raw_writel((__raw_readl(SDRC_POWER)) & ~pmask, SDRC_POWER);
  190. #ifdef POWER_SAVE
  191. __raw_writel(__raw_readl(SMS_SYSCONFIG)|SMART_IDLE, SMS_SYSCONFIG);
  192. __raw_writel(sdata->sdrc_sharing|SMART_IDLE, SDRC_SHARING);
  193. __raw_writel((__raw_readl(SDRC_POWER)|BIT6) & ~pmask, SDRC_POWER);
  194. #endif
  195. }
  196. if ((pass_type == IP_DDR) || (pass_type == STACKED)) /* (IP ddr-CS0),(2422-CS0/CS1) */
  197. __raw_writel(sdata->sdrc_mdcfg_0_ddr, SDRC_MCFG_0+offset);
  198. else if (pass_type == COMBO_DDR){ /* (combo-CS0/CS1) */
  199. __raw_writel(H4_2420_COMBO_MDCFG_0_DDR,SDRC_MCFG_0+offset);
  200. } else if (pass_type == IP_SDR){ /* ip sdr-CS0 */
  201. __raw_writel(sdata->sdrc_mdcfg_0_sdr, SDRC_MCFG_0+offset);
  202. }
  203. if(pass_type == IP_SDR){ /* SDRAM can run full speed only rated for 105MHz*/
  204. a = H4_242X_SDRC_ACTIM_CTRLA_0_100MHz;
  205. b = H4_242X_SDRC_ACTIM_CTRLB_0_100MHz;
  206. r = H4_2420_SDRC_RFR_CTRL;
  207. } else {
  208. a = sdata->sdrc_actim_ctrla_0;
  209. b = sdata->sdrc_actim_ctrlb_0;
  210. r = sdata->sdrc_rfr_ctrl;
  211. }
  212. if (cs0) {
  213. __raw_writel(a, SDRC_ACTIM_CTRLA_0);
  214. __raw_writel(b, SDRC_ACTIM_CTRLB_0);
  215. } else {
  216. __raw_writel(a, SDRC_ACTIM_CTRLA_1);
  217. __raw_writel(b, SDRC_ACTIM_CTRLB_1);
  218. }
  219. __raw_writel(r, SDRC_RFR_CTRL+offset);
  220. /* init sequence for mDDR/mSDR using manual commands (DDR is a bit different) */
  221. __raw_writel(CMD_NOP, SDRC_MANUAL_0+offset);
  222. sdelay(5000); /* susposed to be 100us per design spec for mddr/msdr */
  223. __raw_writel(CMD_PRECHARGE, SDRC_MANUAL_0+offset);
  224. __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_0+offset);
  225. __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_0+offset);
  226. /*
  227. * CSx SDRC Mode Register
  228. * Burst length = (4 - DDR) (2-SDR)
  229. * Serial mode
  230. * CAS latency = x
  231. */
  232. if(pass_type == IP_SDR)
  233. __raw_writel(sdata->sdrc_mr_0_sdr, SDRC_MR_0+offset);
  234. else
  235. __raw_writel(sdata->sdrc_mr_0_ddr, SDRC_MR_0+offset);
  236. /* NOTE: ES1 242x _BUG_ DLL + External Bandwidth fix*/
  237. rev = get_cpu_rev();
  238. if (rev == CPU_2420_ES1 || rev == CPU_2422_ES1){
  239. bug = BIT0;
  240. __raw_writel((__raw_readl(SMS_CLASS_ARB0)|BURSTCOMPLETE_GROUP7)
  241. ,SMS_CLASS_ARB0);/* enable bust complete for lcd */
  242. }
  243. /* enable & load up DLL with good value for 75MHz, and set phase to 90% */
  244. if (common && (pass_type != IP_SDR)) {
  245. __raw_writel(sdata->sdrc_dlla_ctrl, SDRC_DLLA_CTRL);
  246. __raw_writel(sdata->sdrc_dlla_ctrl & ~(BIT2|bug), SDRC_DLLA_CTRL);
  247. __raw_writel(sdata->sdrc_dllb_ctrl, SDRC_DLLB_CTRL);
  248. __raw_writel(sdata->sdrc_dllb_ctrl & ~(BIT2|bug) , SDRC_DLLB_CTRL);
  249. }
  250. sdelay(90000);
  251. if(mem_ok())
  252. return; /* STACKED, other configued type */
  253. ++pass_type; /* IPDDR->COMBODDR->IPSDR for CS0 */
  254. goto next_mem_type;
  255. }
  256. /*****************************************************
  257. * gpmc_init(): init gpmc bus
  258. * Init GPMC for x16, MuxMode (SDRAM in x32).
  259. * This code can only be executed from SRAM or SDRAM.
  260. *****************************************************/
  261. void gpmc_init(void)
  262. {
  263. u32 mux=0, mtype, mwidth;
  264. /* global settings */
  265. __raw_writel(0x10, GPMC_SYSCONFIG); /* smart idle */
  266. __raw_writel(0x0, GPMC_IRQENABLE); /* isr's sources masked */
  267. __raw_writel(0x1, GPMC_TIMEOUT_CONTROL);/* timeout disable */
  268. #ifdef CFG_NAND_BOOT
  269. __raw_writel(0x001, GPMC_CONFIG); /* set nWP, disable limited addr */
  270. #else
  271. __raw_writel(0x111, GPMC_CONFIG); /* set nWP, disable limited addr */
  272. #endif
  273. /* discover bus connection from sysboot */
  274. if (is_gpmc_muxed() == GPMC_MUXED)
  275. mux = BIT9;
  276. mtype = get_gpmc0_type();
  277. mwidth = get_gpmc0_width();
  278. /* setup cs0 */
  279. __raw_writel(0x0, GPMC_CONFIG7_0); /* disable current map */
  280. sdelay(1000);
  281. #ifdef CFG_NAND_BOOT
  282. __raw_writel(H4_24XX_GPMC_CONFIG1_0|mtype|mwidth, GPMC_CONFIG1_0);
  283. #else
  284. __raw_writel(H4_24XX_GPMC_CONFIG1_0|mux|mtype|mwidth, GPMC_CONFIG1_0);
  285. #endif
  286. #ifdef PRCM_CONFIG_III
  287. __raw_writel(H4_24XX_GPMC_CONFIG2_0, GPMC_CONFIG2_0);
  288. #endif
  289. __raw_writel(H4_24XX_GPMC_CONFIG3_0, GPMC_CONFIG3_0);
  290. __raw_writel(H4_24XX_GPMC_CONFIG4_0, GPMC_CONFIG4_0);
  291. #ifdef PRCM_CONFIG_III
  292. __raw_writel(H4_24XX_GPMC_CONFIG5_0, GPMC_CONFIG5_0);
  293. __raw_writel(H4_24XX_GPMC_CONFIG6_0, GPMC_CONFIG6_0);
  294. #endif
  295. __raw_writel(H4_24XX_GPMC_CONFIG7_0, GPMC_CONFIG7_0);/* enable new mapping */
  296. sdelay(2000);
  297. /* setup cs1 */
  298. __raw_writel(0, GPMC_CONFIG7_1); /* disable any mapping */
  299. sdelay(1000);
  300. __raw_writel(H4_24XX_GPMC_CONFIG1_1|mux, GPMC_CONFIG1_1);
  301. __raw_writel(H4_24XX_GPMC_CONFIG2_1, GPMC_CONFIG2_1);
  302. __raw_writel(H4_24XX_GPMC_CONFIG3_1, GPMC_CONFIG3_1);
  303. __raw_writel(H4_24XX_GPMC_CONFIG4_1, GPMC_CONFIG4_1);
  304. __raw_writel(H4_24XX_GPMC_CONFIG5_1, GPMC_CONFIG5_1);
  305. __raw_writel(H4_24XX_GPMC_CONFIG6_1, GPMC_CONFIG6_1);
  306. __raw_writel(H4_24XX_GPMC_CONFIG7_1, GPMC_CONFIG7_1); /* enable mapping */
  307. sdelay(2000);
  308. }