tegra2_emc.c 7.8 KB

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