tegra-gmi.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for NVIDIA Generic Memory Interface
  4. *
  5. * Copyright (C) 2016 Host Mobility AB. All rights reserved.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/reset.h>
  16. #include <soc/tegra/common.h>
  17. #define TEGRA_GMI_CONFIG 0x00
  18. #define TEGRA_GMI_CONFIG_GO BIT(31)
  19. #define TEGRA_GMI_BUS_WIDTH_32BIT BIT(30)
  20. #define TEGRA_GMI_MUX_MODE BIT(28)
  21. #define TEGRA_GMI_RDY_BEFORE_DATA BIT(24)
  22. #define TEGRA_GMI_RDY_ACTIVE_HIGH BIT(23)
  23. #define TEGRA_GMI_ADV_ACTIVE_HIGH BIT(22)
  24. #define TEGRA_GMI_OE_ACTIVE_HIGH BIT(21)
  25. #define TEGRA_GMI_CS_ACTIVE_HIGH BIT(20)
  26. #define TEGRA_GMI_CS_SELECT(x) ((x & 0x7) << 4)
  27. #define TEGRA_GMI_TIMING0 0x10
  28. #define TEGRA_GMI_MUXED_WIDTH(x) ((x & 0xf) << 12)
  29. #define TEGRA_GMI_HOLD_WIDTH(x) ((x & 0xf) << 8)
  30. #define TEGRA_GMI_ADV_WIDTH(x) ((x & 0xf) << 4)
  31. #define TEGRA_GMI_CE_WIDTH(x) (x & 0xf)
  32. #define TEGRA_GMI_TIMING1 0x14
  33. #define TEGRA_GMI_WE_WIDTH(x) ((x & 0xff) << 16)
  34. #define TEGRA_GMI_OE_WIDTH(x) ((x & 0xff) << 8)
  35. #define TEGRA_GMI_WAIT_WIDTH(x) (x & 0xff)
  36. #define TEGRA_GMI_MAX_CHIP_SELECT 8
  37. struct tegra_gmi {
  38. struct device *dev;
  39. void __iomem *base;
  40. struct clk *clk;
  41. struct reset_control *rst;
  42. u32 snor_config;
  43. u32 snor_timing0;
  44. u32 snor_timing1;
  45. };
  46. static int tegra_gmi_enable(struct tegra_gmi *gmi)
  47. {
  48. int err;
  49. pm_runtime_enable(gmi->dev);
  50. err = pm_runtime_resume_and_get(gmi->dev);
  51. if (err) {
  52. pm_runtime_disable(gmi->dev);
  53. return err;
  54. }
  55. reset_control_assert(gmi->rst);
  56. usleep_range(2000, 4000);
  57. reset_control_deassert(gmi->rst);
  58. writel(gmi->snor_timing0, gmi->base + TEGRA_GMI_TIMING0);
  59. writel(gmi->snor_timing1, gmi->base + TEGRA_GMI_TIMING1);
  60. gmi->snor_config |= TEGRA_GMI_CONFIG_GO;
  61. writel(gmi->snor_config, gmi->base + TEGRA_GMI_CONFIG);
  62. return 0;
  63. }
  64. static void tegra_gmi_disable(struct tegra_gmi *gmi)
  65. {
  66. u32 config;
  67. /* stop GMI operation */
  68. config = readl(gmi->base + TEGRA_GMI_CONFIG);
  69. config &= ~TEGRA_GMI_CONFIG_GO;
  70. writel(config, gmi->base + TEGRA_GMI_CONFIG);
  71. reset_control_assert(gmi->rst);
  72. pm_runtime_put_sync_suspend(gmi->dev);
  73. pm_runtime_force_suspend(gmi->dev);
  74. }
  75. static int tegra_gmi_parse_dt(struct tegra_gmi *gmi)
  76. {
  77. struct device_node *child;
  78. u32 property, ranges[4];
  79. int err;
  80. child = of_get_next_available_child(gmi->dev->of_node, NULL);
  81. if (!child) {
  82. dev_err(gmi->dev, "no child nodes found\n");
  83. return -ENODEV;
  84. }
  85. /*
  86. * We currently only support one child device due to lack of
  87. * chip-select address decoding. Which means that we only have one
  88. * chip-select line from the GMI controller.
  89. */
  90. if (of_get_child_count(gmi->dev->of_node) > 1)
  91. dev_warn(gmi->dev, "only one child device is supported.");
  92. if (of_property_read_bool(child, "nvidia,snor-data-width-32bit"))
  93. gmi->snor_config |= TEGRA_GMI_BUS_WIDTH_32BIT;
  94. if (of_property_read_bool(child, "nvidia,snor-mux-mode"))
  95. gmi->snor_config |= TEGRA_GMI_MUX_MODE;
  96. if (of_property_read_bool(child, "nvidia,snor-rdy-active-before-data"))
  97. gmi->snor_config |= TEGRA_GMI_RDY_BEFORE_DATA;
  98. if (of_property_read_bool(child, "nvidia,snor-rdy-active-high"))
  99. gmi->snor_config |= TEGRA_GMI_RDY_ACTIVE_HIGH;
  100. if (of_property_read_bool(child, "nvidia,snor-adv-active-high"))
  101. gmi->snor_config |= TEGRA_GMI_ADV_ACTIVE_HIGH;
  102. if (of_property_read_bool(child, "nvidia,snor-oe-active-high"))
  103. gmi->snor_config |= TEGRA_GMI_OE_ACTIVE_HIGH;
  104. if (of_property_read_bool(child, "nvidia,snor-cs-active-high"))
  105. gmi->snor_config |= TEGRA_GMI_CS_ACTIVE_HIGH;
  106. /* Decode the CS# */
  107. err = of_property_read_u32_array(child, "ranges", ranges, 4);
  108. if (err < 0) {
  109. /* Invalid binding */
  110. if (err == -EOVERFLOW) {
  111. dev_err(gmi->dev,
  112. "failed to decode CS: invalid ranges length\n");
  113. goto error_cs;
  114. }
  115. /*
  116. * If we reach here it means that the child node has an empty
  117. * ranges or it does not exist at all. Attempt to decode the
  118. * CS# from the reg property instead.
  119. */
  120. err = of_property_read_u32(child, "reg", &property);
  121. if (err < 0) {
  122. dev_err(gmi->dev,
  123. "failed to decode CS: no reg property found\n");
  124. goto error_cs;
  125. }
  126. } else {
  127. property = ranges[1];
  128. }
  129. /* Valid chip selects are CS0-CS7 */
  130. if (property >= TEGRA_GMI_MAX_CHIP_SELECT) {
  131. dev_err(gmi->dev, "invalid chip select: %d", property);
  132. err = -EINVAL;
  133. goto error_cs;
  134. }
  135. gmi->snor_config |= TEGRA_GMI_CS_SELECT(property);
  136. /* The default values that are provided below are reset values */
  137. if (!of_property_read_u32(child, "nvidia,snor-muxed-width", &property))
  138. gmi->snor_timing0 |= TEGRA_GMI_MUXED_WIDTH(property);
  139. else
  140. gmi->snor_timing0 |= TEGRA_GMI_MUXED_WIDTH(1);
  141. if (!of_property_read_u32(child, "nvidia,snor-hold-width", &property))
  142. gmi->snor_timing0 |= TEGRA_GMI_HOLD_WIDTH(property);
  143. else
  144. gmi->snor_timing0 |= TEGRA_GMI_HOLD_WIDTH(1);
  145. if (!of_property_read_u32(child, "nvidia,snor-adv-width", &property))
  146. gmi->snor_timing0 |= TEGRA_GMI_ADV_WIDTH(property);
  147. else
  148. gmi->snor_timing0 |= TEGRA_GMI_ADV_WIDTH(1);
  149. if (!of_property_read_u32(child, "nvidia,snor-ce-width", &property))
  150. gmi->snor_timing0 |= TEGRA_GMI_CE_WIDTH(property);
  151. else
  152. gmi->snor_timing0 |= TEGRA_GMI_CE_WIDTH(4);
  153. if (!of_property_read_u32(child, "nvidia,snor-we-width", &property))
  154. gmi->snor_timing1 |= TEGRA_GMI_WE_WIDTH(property);
  155. else
  156. gmi->snor_timing1 |= TEGRA_GMI_WE_WIDTH(1);
  157. if (!of_property_read_u32(child, "nvidia,snor-oe-width", &property))
  158. gmi->snor_timing1 |= TEGRA_GMI_OE_WIDTH(property);
  159. else
  160. gmi->snor_timing1 |= TEGRA_GMI_OE_WIDTH(1);
  161. if (!of_property_read_u32(child, "nvidia,snor-wait-width", &property))
  162. gmi->snor_timing1 |= TEGRA_GMI_WAIT_WIDTH(property);
  163. else
  164. gmi->snor_timing1 |= TEGRA_GMI_WAIT_WIDTH(3);
  165. error_cs:
  166. of_node_put(child);
  167. return err;
  168. }
  169. static int tegra_gmi_probe(struct platform_device *pdev)
  170. {
  171. struct device *dev = &pdev->dev;
  172. struct tegra_gmi *gmi;
  173. int err;
  174. gmi = devm_kzalloc(dev, sizeof(*gmi), GFP_KERNEL);
  175. if (!gmi)
  176. return -ENOMEM;
  177. platform_set_drvdata(pdev, gmi);
  178. gmi->dev = dev;
  179. gmi->base = devm_platform_ioremap_resource(pdev, 0);
  180. if (IS_ERR(gmi->base))
  181. return PTR_ERR(gmi->base);
  182. gmi->clk = devm_clk_get(dev, "gmi");
  183. if (IS_ERR(gmi->clk)) {
  184. dev_err(dev, "can not get clock\n");
  185. return PTR_ERR(gmi->clk);
  186. }
  187. gmi->rst = devm_reset_control_get(dev, "gmi");
  188. if (IS_ERR(gmi->rst)) {
  189. dev_err(dev, "can not get reset\n");
  190. return PTR_ERR(gmi->rst);
  191. }
  192. err = devm_tegra_core_dev_init_opp_table_common(&pdev->dev);
  193. if (err)
  194. return err;
  195. err = tegra_gmi_parse_dt(gmi);
  196. if (err)
  197. return err;
  198. err = tegra_gmi_enable(gmi);
  199. if (err < 0)
  200. return err;
  201. err = of_platform_default_populate(dev->of_node, NULL, dev);
  202. if (err < 0) {
  203. dev_err(dev, "fail to create devices.\n");
  204. tegra_gmi_disable(gmi);
  205. return err;
  206. }
  207. return 0;
  208. }
  209. static void tegra_gmi_remove(struct platform_device *pdev)
  210. {
  211. struct tegra_gmi *gmi = platform_get_drvdata(pdev);
  212. of_platform_depopulate(gmi->dev);
  213. tegra_gmi_disable(gmi);
  214. }
  215. static int __maybe_unused tegra_gmi_runtime_resume(struct device *dev)
  216. {
  217. struct tegra_gmi *gmi = dev_get_drvdata(dev);
  218. int err;
  219. err = clk_prepare_enable(gmi->clk);
  220. if (err < 0) {
  221. dev_err(gmi->dev, "failed to enable clock: %d\n", err);
  222. return err;
  223. }
  224. return 0;
  225. }
  226. static int __maybe_unused tegra_gmi_runtime_suspend(struct device *dev)
  227. {
  228. struct tegra_gmi *gmi = dev_get_drvdata(dev);
  229. clk_disable_unprepare(gmi->clk);
  230. return 0;
  231. }
  232. static const struct dev_pm_ops tegra_gmi_pm = {
  233. SET_RUNTIME_PM_OPS(tegra_gmi_runtime_suspend, tegra_gmi_runtime_resume,
  234. NULL)
  235. };
  236. static const struct of_device_id tegra_gmi_id_table[] = {
  237. { .compatible = "nvidia,tegra20-gmi", },
  238. { .compatible = "nvidia,tegra30-gmi", },
  239. { }
  240. };
  241. MODULE_DEVICE_TABLE(of, tegra_gmi_id_table);
  242. static struct platform_driver tegra_gmi_driver = {
  243. .probe = tegra_gmi_probe,
  244. .remove_new = tegra_gmi_remove,
  245. .driver = {
  246. .name = "tegra-gmi",
  247. .of_match_table = tegra_gmi_id_table,
  248. .pm = &tegra_gmi_pm,
  249. },
  250. };
  251. module_platform_driver(tegra_gmi_driver);
  252. MODULE_AUTHOR("Mirza Krak <mirza.krak@gmail.com");
  253. MODULE_DESCRIPTION("NVIDIA Tegra GMI Bus Driver");
  254. MODULE_LICENSE("GPL v2");