sdrc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Functions related to OMAP3 SDRC.
  4. *
  5. * This file has been created after exctracting and consolidating
  6. * the SDRC related content from mem.c and board.c, also created
  7. * generic init function (mem_init).
  8. *
  9. * Copyright (C) 2004-2010
  10. * Texas Instruments Incorporated - http://www.ti.com/
  11. *
  12. * Copyright (C) 2011
  13. * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
  14. *
  15. * Author :
  16. * Vaibhav Hiremath <hvaibhav@ti.com>
  17. *
  18. * Original implementation by (mem.c, board.c) :
  19. * Sunil Kumar <sunilsaini05@gmail.com>
  20. * Shashi Ranjan <shashiranjanmca05@gmail.com>
  21. * Manikandan Pillai <mani.pillai@ti.com>
  22. */
  23. #include <common.h>
  24. #include <asm/io.h>
  25. #include <asm/arch/mem.h>
  26. #include <asm/arch/sys_proto.h>
  27. DECLARE_GLOBAL_DATA_PTR;
  28. extern omap3_sysinfo sysinfo;
  29. static struct sdrc *sdrc_base = (struct sdrc *)OMAP34XX_SDRC_BASE;
  30. /*
  31. * is_mem_sdr -
  32. * - Return 1 if mem type in use is SDR
  33. */
  34. u32 is_mem_sdr(void)
  35. {
  36. if (readl(&sdrc_base->cs[CS0].mr) == SDRC_MR_0_SDR)
  37. return 1;
  38. return 0;
  39. }
  40. /*
  41. * make_cs1_contiguous -
  42. * - When we have CS1 populated we want to have it mapped after cs0 to allow
  43. * command line mem=xyz use all memory with out discontinuous support
  44. * compiled in. We could do it in the ATAG, but there really is two banks...
  45. */
  46. void make_cs1_contiguous(void)
  47. {
  48. u32 size, a_add_low, a_add_high;
  49. size = get_sdr_cs_size(CS0);
  50. size >>= 25; /* divide by 32 MiB to find size to offset CS1 */
  51. a_add_high = (size & 3) << 8; /* set up low field */
  52. a_add_low = (size & 0x3C) >> 2; /* set up high field */
  53. writel((a_add_high | a_add_low), &sdrc_base->cs_cfg);
  54. }
  55. /*
  56. * get_sdr_cs_size -
  57. * - Get size of chip select 0/1
  58. */
  59. u32 get_sdr_cs_size(u32 cs)
  60. {
  61. u32 size;
  62. /* get ram size field */
  63. size = readl(&sdrc_base->cs[cs].mcfg) >> 8;
  64. size &= 0x3FF; /* remove unwanted bits */
  65. size <<= 21; /* multiply by 2 MiB to find size in MB */
  66. return size;
  67. }
  68. /*
  69. * get_sdr_cs_offset -
  70. * - Get offset of cs from cs0 start
  71. */
  72. u32 get_sdr_cs_offset(u32 cs)
  73. {
  74. u32 offset;
  75. if (!cs)
  76. return 0;
  77. offset = readl(&sdrc_base->cs_cfg);
  78. offset = (offset & 15) << 27 | (offset & 0x300) << 17;
  79. return offset;
  80. }
  81. /*
  82. * write_sdrc_timings -
  83. * - Takes CS and associated timings and initalize SDRAM
  84. * - Test CS to make sure it's OK for use
  85. */
  86. static void write_sdrc_timings(u32 cs, struct sdrc_actim *sdrc_actim_base,
  87. struct board_sdrc_timings *timings)
  88. {
  89. /* Setup timings we got from the board. */
  90. writel(timings->mcfg, &sdrc_base->cs[cs].mcfg);
  91. writel(timings->ctrla, &sdrc_actim_base->ctrla);
  92. writel(timings->ctrlb, &sdrc_actim_base->ctrlb);
  93. writel(timings->rfr_ctrl, &sdrc_base->cs[cs].rfr_ctrl);
  94. writel(CMD_NOP, &sdrc_base->cs[cs].manual);
  95. writel(CMD_PRECHARGE, &sdrc_base->cs[cs].manual);
  96. writel(CMD_AUTOREFRESH, &sdrc_base->cs[cs].manual);
  97. writel(CMD_AUTOREFRESH, &sdrc_base->cs[cs].manual);
  98. writel(timings->mr, &sdrc_base->cs[cs].mr);
  99. /*
  100. * Test ram in this bank
  101. * Disable if bad or not present
  102. */
  103. if (!mem_ok(cs))
  104. writel(0, &sdrc_base->cs[cs].mcfg);
  105. }
  106. /*
  107. * do_sdrc_init -
  108. * - Code called once in C-Stack only context for CS0 and with early being
  109. * true and a possible 2nd time depending on memory configuration from
  110. * stack+global context.
  111. */
  112. void do_sdrc_init(u32 cs, u32 early)
  113. {
  114. struct sdrc_actim *sdrc_actim_base0, *sdrc_actim_base1;
  115. struct board_sdrc_timings timings;
  116. sdrc_actim_base0 = (struct sdrc_actim *)SDRC_ACTIM_CTRL0_BASE;
  117. sdrc_actim_base1 = (struct sdrc_actim *)SDRC_ACTIM_CTRL1_BASE;
  118. /* set some default timings */
  119. timings.sharing = SDRC_SHARING;
  120. /*
  121. * When called in the early context this may be SPL and we will
  122. * need to set all of the timings. This ends up being board
  123. * specific so we call a helper function to take care of this
  124. * for us. Otherwise, to be safe, we need to copy the settings
  125. * from the first bank to the second. We will setup CS0,
  126. * then set cs_cfg to the appropriate value then try and
  127. * setup CS1.
  128. */
  129. #ifdef CONFIG_SPL_BUILD
  130. /* set/modify board-specific timings */
  131. get_board_mem_timings(&timings);
  132. #endif
  133. if (early) {
  134. /* reset sdrc controller */
  135. writel(SOFTRESET, &sdrc_base->sysconfig);
  136. wait_on_value(RESETDONE, RESETDONE, &sdrc_base->status,
  137. 12000000);
  138. writel(0, &sdrc_base->sysconfig);
  139. /* setup sdrc to ball mux */
  140. writel(timings.sharing, &sdrc_base->sharing);
  141. /* Disable Power Down of CKE because of 1 CKE on combo part */
  142. writel(WAKEUPPROC | SRFRONRESET | PAGEPOLICY_HIGH,
  143. &sdrc_base->power);
  144. writel(ENADLL | DLLPHASE_90, &sdrc_base->dlla_ctrl);
  145. sdelay(0x20000);
  146. #ifdef CONFIG_SPL_BUILD
  147. write_sdrc_timings(CS0, sdrc_actim_base0, &timings);
  148. make_cs1_contiguous();
  149. write_sdrc_timings(CS1, sdrc_actim_base1, &timings);
  150. #endif
  151. }
  152. /*
  153. * If we aren't using SPL we have been loaded by some
  154. * other means which may not have correctly initialized
  155. * both CS0 and CS1 (such as some older versions of x-loader)
  156. * so we may be asked now to setup CS1.
  157. */
  158. if (cs == CS1) {
  159. timings.mcfg = readl(&sdrc_base->cs[CS0].mcfg),
  160. timings.rfr_ctrl = readl(&sdrc_base->cs[CS0].rfr_ctrl);
  161. timings.ctrla = readl(&sdrc_actim_base0->ctrla);
  162. timings.ctrlb = readl(&sdrc_actim_base0->ctrlb);
  163. timings.mr = readl(&sdrc_base->cs[CS0].mr);
  164. write_sdrc_timings(cs, sdrc_actim_base1, &timings);
  165. }
  166. }
  167. /*
  168. * dram_init -
  169. * - Sets uboots idea of sdram size
  170. */
  171. int dram_init(void)
  172. {
  173. unsigned int size0 = 0, size1 = 0;
  174. size0 = get_sdr_cs_size(CS0);
  175. /*
  176. * We always need to have cs_cfg point at where the second
  177. * bank would be, if present. Failure to do so can lead to
  178. * strange situations where memory isn't detected and
  179. * configured correctly. CS0 will already have been setup
  180. * at this point.
  181. */
  182. make_cs1_contiguous();
  183. do_sdrc_init(CS1, NOT_EARLY);
  184. size1 = get_sdr_cs_size(CS1);
  185. gd->ram_size = size0 + size1;
  186. return 0;
  187. }
  188. int dram_init_banksize(void)
  189. {
  190. unsigned int size0 = 0, size1 = 0;
  191. size0 = get_sdr_cs_size(CS0);
  192. size1 = get_sdr_cs_size(CS1);
  193. gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
  194. gd->bd->bi_dram[0].size = size0;
  195. gd->bd->bi_dram[1].start = PHYS_SDRAM_1 + get_sdr_cs_offset(CS1);
  196. gd->bd->bi_dram[1].size = size1;
  197. return 0;
  198. }
  199. /*
  200. * mem_init -
  201. * - Init the sdrc chip,
  202. * - Selects CS0 and CS1,
  203. */
  204. void mem_init(void)
  205. {
  206. /* only init up first bank here */
  207. do_sdrc_init(CS0, EARLY_INIT);
  208. }