tegra2_emc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (C) 2011 Google, Inc.
  3. *
  4. * Author:
  5. * Colin Cross <ccross@android.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  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. */
  17. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #include <linux/clk.h>
  20. #include <linux/err.h>
  21. #include <linux/io.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/platform_data/tegra_emc.h>
  26. #include "tegra2_emc.h"
  27. #include "fuse.h"
  28. #ifdef CONFIG_TEGRA_EMC_SCALING_ENABLE
  29. static bool emc_enable = true;
  30. #else
  31. static bool emc_enable;
  32. #endif
  33. module_param(emc_enable, bool, 0644);
  34. static struct platform_device *emc_pdev;
  35. static void __iomem *emc_regbase;
  36. static inline void emc_writel(u32 val, unsigned long addr)
  37. {
  38. writel(val, emc_regbase + addr);
  39. }
  40. static inline u32 emc_readl(unsigned long addr)
  41. {
  42. return readl(emc_regbase + addr);
  43. }
  44. static const unsigned long emc_reg_addr[TEGRA_EMC_NUM_REGS] = {
  45. 0x2c, /* RC */
  46. 0x30, /* RFC */
  47. 0x34, /* RAS */
  48. 0x38, /* RP */
  49. 0x3c, /* R2W */
  50. 0x40, /* W2R */
  51. 0x44, /* R2P */
  52. 0x48, /* W2P */
  53. 0x4c, /* RD_RCD */
  54. 0x50, /* WR_RCD */
  55. 0x54, /* RRD */
  56. 0x58, /* REXT */
  57. 0x5c, /* WDV */
  58. 0x60, /* QUSE */
  59. 0x64, /* QRST */
  60. 0x68, /* QSAFE */
  61. 0x6c, /* RDV */
  62. 0x70, /* REFRESH */
  63. 0x74, /* BURST_REFRESH_NUM */
  64. 0x78, /* PDEX2WR */
  65. 0x7c, /* PDEX2RD */
  66. 0x80, /* PCHG2PDEN */
  67. 0x84, /* ACT2PDEN */
  68. 0x88, /* AR2PDEN */
  69. 0x8c, /* RW2PDEN */
  70. 0x90, /* TXSR */
  71. 0x94, /* TCKE */
  72. 0x98, /* TFAW */
  73. 0x9c, /* TRPAB */
  74. 0xa0, /* TCLKSTABLE */
  75. 0xa4, /* TCLKSTOP */
  76. 0xa8, /* TREFBW */
  77. 0xac, /* QUSE_EXTRA */
  78. 0x114, /* FBIO_CFG6 */
  79. 0xb0, /* ODT_WRITE */
  80. 0xb4, /* ODT_READ */
  81. 0x104, /* FBIO_CFG5 */
  82. 0x2bc, /* CFG_DIG_DLL */
  83. 0x2c0, /* DLL_XFORM_DQS */
  84. 0x2c4, /* DLL_XFORM_QUSE */
  85. 0x2e0, /* ZCAL_REF_CNT */
  86. 0x2e4, /* ZCAL_WAIT_CNT */
  87. 0x2a8, /* AUTO_CAL_INTERVAL */
  88. 0x2d0, /* CFG_CLKTRIM_0 */
  89. 0x2d4, /* CFG_CLKTRIM_1 */
  90. 0x2d8, /* CFG_CLKTRIM_2 */
  91. };
  92. /* Select the closest EMC rate that is higher than the requested rate */
  93. long tegra_emc_round_rate(unsigned long rate)
  94. {
  95. struct tegra_emc_pdata *pdata;
  96. int i;
  97. int best = -1;
  98. unsigned long distance = ULONG_MAX;
  99. if (!emc_pdev)
  100. return -EINVAL;
  101. pdata = emc_pdev->dev.platform_data;
  102. pr_debug("%s: %lu\n", __func__, rate);
  103. /*
  104. * The EMC clock rate is twice the bus rate, and the bus rate is
  105. * measured in kHz
  106. */
  107. rate = rate / 2 / 1000;
  108. for (i = 0; i < pdata->num_tables; i++) {
  109. if (pdata->tables[i].rate >= rate &&
  110. (pdata->tables[i].rate - rate) < distance) {
  111. distance = pdata->tables[i].rate - rate;
  112. best = i;
  113. }
  114. }
  115. if (best < 0)
  116. return -EINVAL;
  117. pr_debug("%s: using %lu\n", __func__, pdata->tables[best].rate);
  118. return pdata->tables[best].rate * 2 * 1000;
  119. }
  120. /*
  121. * The EMC registers have shadow registers. When the EMC clock is updated
  122. * in the clock controller, the shadow registers are copied to the active
  123. * registers, allowing glitchless memory bus frequency changes.
  124. * This function updates the shadow registers for a new clock frequency,
  125. * and relies on the clock lock on the emc clock to avoid races between
  126. * multiple frequency changes
  127. */
  128. int tegra_emc_set_rate(unsigned long rate)
  129. {
  130. struct tegra_emc_pdata *pdata;
  131. int i;
  132. int j;
  133. if (!emc_pdev)
  134. return -EINVAL;
  135. pdata = emc_pdev->dev.platform_data;
  136. /*
  137. * The EMC clock rate is twice the bus rate, and the bus rate is
  138. * measured in kHz
  139. */
  140. rate = rate / 2 / 1000;
  141. for (i = 0; i < pdata->num_tables; i++)
  142. if (pdata->tables[i].rate == rate)
  143. break;
  144. if (i >= pdata->num_tables)
  145. return -EINVAL;
  146. pr_debug("%s: setting to %lu\n", __func__, rate);
  147. for (j = 0; j < TEGRA_EMC_NUM_REGS; j++)
  148. emc_writel(pdata->tables[i].regs[j], emc_reg_addr[j]);
  149. emc_readl(pdata->tables[i].regs[TEGRA_EMC_NUM_REGS - 1]);
  150. return 0;
  151. }
  152. #ifdef CONFIG_OF
  153. static struct device_node *tegra_emc_ramcode_devnode(struct device_node *np)
  154. {
  155. struct device_node *iter;
  156. u32 reg;
  157. for_each_child_of_node(np, iter) {
  158. if (of_property_read_u32(np, "nvidia,ram-code", &reg))
  159. continue;
  160. if (reg == tegra_bct_strapping)
  161. return of_node_get(iter);
  162. }
  163. return NULL;
  164. }
  165. static struct tegra_emc_pdata *tegra_emc_dt_parse_pdata(
  166. struct platform_device *pdev)
  167. {
  168. struct device_node *np = pdev->dev.of_node;
  169. struct device_node *tnp, *iter;
  170. struct tegra_emc_pdata *pdata;
  171. int ret, i, num_tables;
  172. if (!np)
  173. return NULL;
  174. if (of_find_property(np, "nvidia,use-ram-code", NULL)) {
  175. tnp = tegra_emc_ramcode_devnode(np);
  176. if (!tnp)
  177. dev_warn(&pdev->dev,
  178. "can't find emc table for ram-code 0x%02x\n",
  179. tegra_bct_strapping);
  180. } else
  181. tnp = of_node_get(np);
  182. if (!tnp)
  183. return NULL;
  184. num_tables = 0;
  185. for_each_child_of_node(tnp, iter)
  186. if (of_device_is_compatible(iter, "nvidia,tegra20-emc-table"))
  187. num_tables++;
  188. if (!num_tables) {
  189. pdata = NULL;
  190. goto out;
  191. }
  192. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  193. pdata->tables = devm_kzalloc(&pdev->dev,
  194. sizeof(*pdata->tables) * num_tables,
  195. GFP_KERNEL);
  196. i = 0;
  197. for_each_child_of_node(tnp, iter) {
  198. u32 prop;
  199. ret = of_property_read_u32(iter, "clock-frequency", &prop);
  200. if (ret) {
  201. dev_err(&pdev->dev, "no clock-frequency in %s\n",
  202. iter->full_name);
  203. continue;
  204. }
  205. pdata->tables[i].rate = prop;
  206. ret = of_property_read_u32_array(iter, "nvidia,emc-registers",
  207. pdata->tables[i].regs,
  208. TEGRA_EMC_NUM_REGS);
  209. if (ret) {
  210. dev_err(&pdev->dev,
  211. "malformed emc-registers property in %s\n",
  212. iter->full_name);
  213. continue;
  214. }
  215. i++;
  216. }
  217. pdata->num_tables = i;
  218. out:
  219. of_node_put(tnp);
  220. return pdata;
  221. }
  222. #else
  223. static struct tegra_emc_pdata *tegra_emc_dt_parse_pdata(
  224. struct platform_device *pdev)
  225. {
  226. return NULL;
  227. }
  228. #endif
  229. static struct tegra_emc_pdata *tegra_emc_fill_pdata(struct platform_device *pdev)
  230. {
  231. struct clk *c = clk_get_sys(NULL, "emc");
  232. struct tegra_emc_pdata *pdata;
  233. unsigned long khz;
  234. int i;
  235. WARN_ON(pdev->dev.platform_data);
  236. BUG_ON(IS_ERR_OR_NULL(c));
  237. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  238. pdata->tables = devm_kzalloc(&pdev->dev, sizeof(*pdata->tables),
  239. GFP_KERNEL);
  240. pdata->tables[0].rate = clk_get_rate(c) / 2 / 1000;
  241. for (i = 0; i < TEGRA_EMC_NUM_REGS; i++)
  242. pdata->tables[0].regs[i] = emc_readl(emc_reg_addr[i]);
  243. pdata->num_tables = 1;
  244. khz = pdata->tables[0].rate;
  245. dev_info(&pdev->dev, "no tables provided, using %ld kHz emc, "
  246. "%ld kHz mem\n", khz * 2, khz);
  247. return pdata;
  248. }
  249. static int tegra_emc_probe(struct platform_device *pdev)
  250. {
  251. struct tegra_emc_pdata *pdata;
  252. struct resource *res;
  253. if (!emc_enable) {
  254. dev_err(&pdev->dev, "disabled per module parameter\n");
  255. return -ENODEV;
  256. }
  257. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  258. if (!res) {
  259. dev_err(&pdev->dev, "missing register base\n");
  260. return -ENOMEM;
  261. }
  262. emc_regbase = devm_request_and_ioremap(&pdev->dev, res);
  263. if (!emc_regbase) {
  264. dev_err(&pdev->dev, "failed to remap registers\n");
  265. return -ENOMEM;
  266. }
  267. pdata = pdev->dev.platform_data;
  268. if (!pdata)
  269. pdata = tegra_emc_dt_parse_pdata(pdev);
  270. if (!pdata)
  271. pdata = tegra_emc_fill_pdata(pdev);
  272. pdev->dev.platform_data = pdata;
  273. emc_pdev = pdev;
  274. return 0;
  275. }
  276. static struct of_device_id tegra_emc_of_match[] = {
  277. { .compatible = "nvidia,tegra20-emc", },
  278. { },
  279. };
  280. static struct platform_driver tegra_emc_driver = {
  281. .driver = {
  282. .name = "tegra-emc",
  283. .owner = THIS_MODULE,
  284. .of_match_table = tegra_emc_of_match,
  285. },
  286. .probe = tegra_emc_probe,
  287. };
  288. static int __init tegra_emc_init(void)
  289. {
  290. return platform_driver_register(&tegra_emc_driver);
  291. }
  292. device_initcall(tegra_emc_init);