emc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  4. */
  5. #include <common.h>
  6. #include <fdtdec.h>
  7. #include <asm/io.h>
  8. #include <asm/arch-tegra/ap.h>
  9. #include <asm/arch-tegra/apb_misc.h>
  10. #include <asm/arch/clock.h>
  11. #include <asm/arch/emc.h>
  12. #include <asm/arch/tegra.h>
  13. /*
  14. * The EMC registers have shadow registers. When the EMC clock is updated
  15. * in the clock controller, the shadow registers are copied to the active
  16. * registers, allowing glitchless memory bus frequency changes.
  17. * This function updates the shadow registers for a new clock frequency,
  18. * and relies on the clock lock on the emc clock to avoid races between
  19. * multiple frequency changes
  20. */
  21. /*
  22. * This table defines the ordering of the registers provided to
  23. * tegra_set_mmc()
  24. * TODO: Convert to fdt version once available
  25. */
  26. static const unsigned long emc_reg_addr[TEGRA_EMC_NUM_REGS] = {
  27. 0x2c, /* RC */
  28. 0x30, /* RFC */
  29. 0x34, /* RAS */
  30. 0x38, /* RP */
  31. 0x3c, /* R2W */
  32. 0x40, /* W2R */
  33. 0x44, /* R2P */
  34. 0x48, /* W2P */
  35. 0x4c, /* RD_RCD */
  36. 0x50, /* WR_RCD */
  37. 0x54, /* RRD */
  38. 0x58, /* REXT */
  39. 0x5c, /* WDV */
  40. 0x60, /* QUSE */
  41. 0x64, /* QRST */
  42. 0x68, /* QSAFE */
  43. 0x6c, /* RDV */
  44. 0x70, /* REFRESH */
  45. 0x74, /* BURST_REFRESH_NUM */
  46. 0x78, /* PDEX2WR */
  47. 0x7c, /* PDEX2RD */
  48. 0x80, /* PCHG2PDEN */
  49. 0x84, /* ACT2PDEN */
  50. 0x88, /* AR2PDEN */
  51. 0x8c, /* RW2PDEN */
  52. 0x90, /* TXSR */
  53. 0x94, /* TCKE */
  54. 0x98, /* TFAW */
  55. 0x9c, /* TRPAB */
  56. 0xa0, /* TCLKSTABLE */
  57. 0xa4, /* TCLKSTOP */
  58. 0xa8, /* TREFBW */
  59. 0xac, /* QUSE_EXTRA */
  60. 0x114, /* FBIO_CFG6 */
  61. 0xb0, /* ODT_WRITE */
  62. 0xb4, /* ODT_READ */
  63. 0x104, /* FBIO_CFG5 */
  64. 0x2bc, /* CFG_DIG_DLL */
  65. 0x2c0, /* DLL_XFORM_DQS */
  66. 0x2c4, /* DLL_XFORM_QUSE */
  67. 0x2e0, /* ZCAL_REF_CNT */
  68. 0x2e4, /* ZCAL_WAIT_CNT */
  69. 0x2a8, /* AUTO_CAL_INTERVAL */
  70. 0x2d0, /* CFG_CLKTRIM_0 */
  71. 0x2d4, /* CFG_CLKTRIM_1 */
  72. 0x2d8, /* CFG_CLKTRIM_2 */
  73. };
  74. struct emc_ctlr *emc_get_controller(const void *blob)
  75. {
  76. fdt_addr_t addr;
  77. int node;
  78. node = fdtdec_next_compatible(blob, 0, COMPAT_NVIDIA_TEGRA20_EMC);
  79. if (node > 0) {
  80. addr = fdtdec_get_addr(blob, node, "reg");
  81. if (addr != FDT_ADDR_T_NONE)
  82. return (struct emc_ctlr *)addr;
  83. }
  84. return NULL;
  85. }
  86. /* Error codes we use */
  87. enum {
  88. ERR_NO_EMC_NODE = -10,
  89. ERR_NO_EMC_REG,
  90. ERR_NO_FREQ,
  91. ERR_FREQ_NOT_FOUND,
  92. ERR_BAD_REGS,
  93. ERR_NO_RAM_CODE,
  94. ERR_RAM_CODE_NOT_FOUND,
  95. };
  96. /**
  97. * Find EMC tables for the given ram code.
  98. *
  99. * The tegra EMC binding has two options, one using the ram code and one not.
  100. * We detect which is in use by looking for the nvidia,use-ram-code property.
  101. * If this is not present, then the EMC tables are directly below 'node',
  102. * otherwise we select the correct emc-tables subnode based on the 'ram_code'
  103. * value.
  104. *
  105. * @param blob Device tree blob
  106. * @param node EMC node (nvidia,tegra20-emc compatible string)
  107. * @param ram_code RAM code to select (0-3, or -1 if unknown)
  108. * @return 0 if ok, otherwise a -ve ERR_ code (see enum above)
  109. */
  110. static int find_emc_tables(const void *blob, int node, int ram_code)
  111. {
  112. int need_ram_code;
  113. int depth;
  114. int offset;
  115. /* If we are using RAM codes, scan through the tables for our code */
  116. need_ram_code = fdtdec_get_bool(blob, node, "nvidia,use-ram-code");
  117. if (!need_ram_code)
  118. return node;
  119. if (ram_code == -1) {
  120. debug("%s: RAM code required but not supplied\n", __func__);
  121. return ERR_NO_RAM_CODE;
  122. }
  123. offset = node;
  124. depth = 0;
  125. do {
  126. /*
  127. * Sadly there is no compatible string so we cannot use
  128. * fdtdec_next_compatible_subnode().
  129. */
  130. offset = fdt_next_node(blob, offset, &depth);
  131. if (depth <= 0)
  132. break;
  133. /* Make sure this is a direct subnode */
  134. if (depth != 1)
  135. continue;
  136. if (strcmp("emc-tables", fdt_get_name(blob, offset, NULL)))
  137. continue;
  138. if (fdtdec_get_int(blob, offset, "nvidia,ram-code", -1)
  139. == ram_code)
  140. return offset;
  141. } while (1);
  142. debug("%s: Could not find tables for RAM code %d\n", __func__,
  143. ram_code);
  144. return ERR_RAM_CODE_NOT_FOUND;
  145. }
  146. /**
  147. * Decode the EMC node of the device tree, returning a pointer to the emc
  148. * controller and the table to be used for the given rate.
  149. *
  150. * @param blob Device tree blob
  151. * @param rate Clock speed of memory controller in Hz (=2x memory bus rate)
  152. * @param emcp Returns address of EMC controller registers
  153. * @param tablep Returns pointer to table to program into EMC. There are
  154. * TEGRA_EMC_NUM_REGS entries, destined for offsets as per the
  155. * emc_reg_addr array.
  156. * @return 0 if ok, otherwise a -ve error code which will allow someone to
  157. * figure out roughly what went wrong by looking at this code.
  158. */
  159. static int decode_emc(const void *blob, unsigned rate, struct emc_ctlr **emcp,
  160. const u32 **tablep)
  161. {
  162. struct apb_misc_pp_ctlr *pp =
  163. (struct apb_misc_pp_ctlr *)NV_PA_APB_MISC_BASE;
  164. int ram_code;
  165. int depth;
  166. int node;
  167. ram_code = (readl(&pp->strapping_opt_a) & RAM_CODE_MASK)
  168. >> RAM_CODE_SHIFT;
  169. /*
  170. * The EMC clock rate is twice the bus rate, and the bus rate is
  171. * measured in kHz
  172. */
  173. rate = rate / 2 / 1000;
  174. node = fdtdec_next_compatible(blob, 0, COMPAT_NVIDIA_TEGRA20_EMC);
  175. if (node < 0) {
  176. debug("%s: No EMC node found in FDT\n", __func__);
  177. return ERR_NO_EMC_NODE;
  178. }
  179. *emcp = (struct emc_ctlr *)fdtdec_get_addr(blob, node, "reg");
  180. if (*emcp == (struct emc_ctlr *)FDT_ADDR_T_NONE) {
  181. debug("%s: No EMC node reg property\n", __func__);
  182. return ERR_NO_EMC_REG;
  183. }
  184. /* Work out the parent node which contains our EMC tables */
  185. node = find_emc_tables(blob, node, ram_code & 3);
  186. if (node < 0)
  187. return node;
  188. depth = 0;
  189. for (;;) {
  190. int node_rate;
  191. node = fdtdec_next_compatible_subnode(blob, node,
  192. COMPAT_NVIDIA_TEGRA20_EMC_TABLE, &depth);
  193. if (node < 0)
  194. break;
  195. node_rate = fdtdec_get_int(blob, node, "clock-frequency", -1);
  196. if (node_rate == -1) {
  197. debug("%s: Missing clock-frequency\n", __func__);
  198. return ERR_NO_FREQ; /* we expect this property */
  199. }
  200. if (node_rate == rate)
  201. break;
  202. }
  203. if (node < 0) {
  204. debug("%s: No node found for clock frequency %d\n", __func__,
  205. rate);
  206. return ERR_FREQ_NOT_FOUND;
  207. }
  208. *tablep = fdtdec_locate_array(blob, node, "nvidia,emc-registers",
  209. TEGRA_EMC_NUM_REGS);
  210. if (!*tablep) {
  211. debug("%s: node '%s' array missing / wrong size\n", __func__,
  212. fdt_get_name(blob, node, NULL));
  213. return ERR_BAD_REGS;
  214. }
  215. /* All seems well */
  216. return 0;
  217. }
  218. int tegra_set_emc(const void *blob, unsigned rate)
  219. {
  220. struct emc_ctlr *emc;
  221. const u32 *table = NULL;
  222. int err, i;
  223. err = decode_emc(blob, rate, &emc, &table);
  224. if (err) {
  225. debug("Warning: no valid EMC (%d), memory timings unset\n",
  226. err);
  227. return err;
  228. }
  229. debug("%s: Table found, setting EMC values as follows:\n", __func__);
  230. for (i = 0; i < TEGRA_EMC_NUM_REGS; i++) {
  231. u32 value = fdt32_to_cpu(table[i]);
  232. u32 addr = (uintptr_t)emc + emc_reg_addr[i];
  233. debug(" %#x: %#x\n", addr, value);
  234. writel(value, addr);
  235. }
  236. /* trigger emc with new settings */
  237. clock_adjust_periph_pll_div(PERIPH_ID_EMC, CLOCK_ID_MEMORY,
  238. clock_get_rate(CLOCK_ID_MEMORY), NULL);
  239. debug("EMC clock set to %lu\n",
  240. clock_get_periph_rate(PERIPH_ID_EMC, CLOCK_ID_MEMORY));
  241. return 0;
  242. }