tegra-gmi.c 7.5 KB

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