1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for NVIDIA Generic Memory Interface
5 * Copyright (C) 2016 Host Mobility AB. All rights reserved.
9 #include <linux/delay.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/reset.h>
16 #include <soc/tegra/common.h>
18 #define TEGRA_GMI_CONFIG 0x00
19 #define TEGRA_GMI_CONFIG_GO BIT(31)
20 #define TEGRA_GMI_BUS_WIDTH_32BIT BIT(30)
21 #define TEGRA_GMI_MUX_MODE BIT(28)
22 #define TEGRA_GMI_RDY_BEFORE_DATA BIT(24)
23 #define TEGRA_GMI_RDY_ACTIVE_HIGH BIT(23)
24 #define TEGRA_GMI_ADV_ACTIVE_HIGH BIT(22)
25 #define TEGRA_GMI_OE_ACTIVE_HIGH BIT(21)
26 #define TEGRA_GMI_CS_ACTIVE_HIGH BIT(20)
27 #define TEGRA_GMI_CS_SELECT(x) ((x & 0x7) << 4)
29 #define TEGRA_GMI_TIMING0 0x10
30 #define TEGRA_GMI_MUXED_WIDTH(x) ((x & 0xf) << 12)
31 #define TEGRA_GMI_HOLD_WIDTH(x) ((x & 0xf) << 8)
32 #define TEGRA_GMI_ADV_WIDTH(x) ((x & 0xf) << 4)
33 #define TEGRA_GMI_CE_WIDTH(x) (x & 0xf)
35 #define TEGRA_GMI_TIMING1 0x14
36 #define TEGRA_GMI_WE_WIDTH(x) ((x & 0xff) << 16)
37 #define TEGRA_GMI_OE_WIDTH(x) ((x & 0xff) << 8)
38 #define TEGRA_GMI_WAIT_WIDTH(x) (x & 0xff)
40 #define TEGRA_GMI_MAX_CHIP_SELECT 8
46 struct reset_control *rst;
53 static int tegra_gmi_enable(struct tegra_gmi *gmi)
57 pm_runtime_enable(gmi->dev);
58 err = pm_runtime_resume_and_get(gmi->dev);
60 pm_runtime_disable(gmi->dev);
64 reset_control_assert(gmi->rst);
65 usleep_range(2000, 4000);
66 reset_control_deassert(gmi->rst);
68 writel(gmi->snor_timing0, gmi->base + TEGRA_GMI_TIMING0);
69 writel(gmi->snor_timing1, gmi->base + TEGRA_GMI_TIMING1);
71 gmi->snor_config |= TEGRA_GMI_CONFIG_GO;
72 writel(gmi->snor_config, gmi->base + TEGRA_GMI_CONFIG);
77 static void tegra_gmi_disable(struct tegra_gmi *gmi)
81 /* stop GMI operation */
82 config = readl(gmi->base + TEGRA_GMI_CONFIG);
83 config &= ~TEGRA_GMI_CONFIG_GO;
84 writel(config, gmi->base + TEGRA_GMI_CONFIG);
86 reset_control_assert(gmi->rst);
88 pm_runtime_put_sync_suspend(gmi->dev);
89 pm_runtime_force_suspend(gmi->dev);
92 static int tegra_gmi_parse_dt(struct tegra_gmi *gmi)
94 struct device_node *child;
95 u32 property, ranges[4];
98 child = of_get_next_available_child(gmi->dev->of_node, NULL);
100 dev_err(gmi->dev, "no child nodes found\n");
105 * We currently only support one child device due to lack of
106 * chip-select address decoding. Which means that we only have one
107 * chip-select line from the GMI controller.
109 if (of_get_child_count(gmi->dev->of_node) > 1)
110 dev_warn(gmi->dev, "only one child device is supported.");
112 if (of_property_read_bool(child, "nvidia,snor-data-width-32bit"))
113 gmi->snor_config |= TEGRA_GMI_BUS_WIDTH_32BIT;
115 if (of_property_read_bool(child, "nvidia,snor-mux-mode"))
116 gmi->snor_config |= TEGRA_GMI_MUX_MODE;
118 if (of_property_read_bool(child, "nvidia,snor-rdy-active-before-data"))
119 gmi->snor_config |= TEGRA_GMI_RDY_BEFORE_DATA;
121 if (of_property_read_bool(child, "nvidia,snor-rdy-active-high"))
122 gmi->snor_config |= TEGRA_GMI_RDY_ACTIVE_HIGH;
124 if (of_property_read_bool(child, "nvidia,snor-adv-active-high"))
125 gmi->snor_config |= TEGRA_GMI_ADV_ACTIVE_HIGH;
127 if (of_property_read_bool(child, "nvidia,snor-oe-active-high"))
128 gmi->snor_config |= TEGRA_GMI_OE_ACTIVE_HIGH;
130 if (of_property_read_bool(child, "nvidia,snor-cs-active-high"))
131 gmi->snor_config |= TEGRA_GMI_CS_ACTIVE_HIGH;
134 err = of_property_read_u32_array(child, "ranges", ranges, 4);
136 /* Invalid binding */
137 if (err == -EOVERFLOW) {
139 "failed to decode CS: invalid ranges length\n");
144 * If we reach here it means that the child node has an empty
145 * ranges or it does not exist at all. Attempt to decode the
146 * CS# from the reg property instead.
148 err = of_property_read_u32(child, "reg", &property);
151 "failed to decode CS: no reg property found\n");
155 property = ranges[1];
158 /* Valid chip selects are CS0-CS7 */
159 if (property >= TEGRA_GMI_MAX_CHIP_SELECT) {
160 dev_err(gmi->dev, "invalid chip select: %d", property);
165 gmi->snor_config |= TEGRA_GMI_CS_SELECT(property);
167 /* The default values that are provided below are reset values */
168 if (!of_property_read_u32(child, "nvidia,snor-muxed-width", &property))
169 gmi->snor_timing0 |= TEGRA_GMI_MUXED_WIDTH(property);
171 gmi->snor_timing0 |= TEGRA_GMI_MUXED_WIDTH(1);
173 if (!of_property_read_u32(child, "nvidia,snor-hold-width", &property))
174 gmi->snor_timing0 |= TEGRA_GMI_HOLD_WIDTH(property);
176 gmi->snor_timing0 |= TEGRA_GMI_HOLD_WIDTH(1);
178 if (!of_property_read_u32(child, "nvidia,snor-adv-width", &property))
179 gmi->snor_timing0 |= TEGRA_GMI_ADV_WIDTH(property);
181 gmi->snor_timing0 |= TEGRA_GMI_ADV_WIDTH(1);
183 if (!of_property_read_u32(child, "nvidia,snor-ce-width", &property))
184 gmi->snor_timing0 |= TEGRA_GMI_CE_WIDTH(property);
186 gmi->snor_timing0 |= TEGRA_GMI_CE_WIDTH(4);
188 if (!of_property_read_u32(child, "nvidia,snor-we-width", &property))
189 gmi->snor_timing1 |= TEGRA_GMI_WE_WIDTH(property);
191 gmi->snor_timing1 |= TEGRA_GMI_WE_WIDTH(1);
193 if (!of_property_read_u32(child, "nvidia,snor-oe-width", &property))
194 gmi->snor_timing1 |= TEGRA_GMI_OE_WIDTH(property);
196 gmi->snor_timing1 |= TEGRA_GMI_OE_WIDTH(1);
198 if (!of_property_read_u32(child, "nvidia,snor-wait-width", &property))
199 gmi->snor_timing1 |= TEGRA_GMI_WAIT_WIDTH(property);
201 gmi->snor_timing1 |= TEGRA_GMI_WAIT_WIDTH(3);
208 static int tegra_gmi_probe(struct platform_device *pdev)
210 struct device *dev = &pdev->dev;
211 struct tegra_gmi *gmi;
212 struct resource *res;
215 gmi = devm_kzalloc(dev, sizeof(*gmi), GFP_KERNEL);
219 platform_set_drvdata(pdev, gmi);
222 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
223 gmi->base = devm_ioremap_resource(dev, res);
224 if (IS_ERR(gmi->base))
225 return PTR_ERR(gmi->base);
227 gmi->clk = devm_clk_get(dev, "gmi");
228 if (IS_ERR(gmi->clk)) {
229 dev_err(dev, "can not get clock\n");
230 return PTR_ERR(gmi->clk);
233 gmi->rst = devm_reset_control_get(dev, "gmi");
234 if (IS_ERR(gmi->rst)) {
235 dev_err(dev, "can not get reset\n");
236 return PTR_ERR(gmi->rst);
239 err = devm_tegra_core_dev_init_opp_table_common(&pdev->dev);
243 err = tegra_gmi_parse_dt(gmi);
247 err = tegra_gmi_enable(gmi);
251 err = of_platform_default_populate(dev->of_node, NULL, dev);
253 dev_err(dev, "fail to create devices.\n");
254 tegra_gmi_disable(gmi);
261 static int tegra_gmi_remove(struct platform_device *pdev)
263 struct tegra_gmi *gmi = platform_get_drvdata(pdev);
265 of_platform_depopulate(gmi->dev);
266 tegra_gmi_disable(gmi);
271 static int __maybe_unused tegra_gmi_runtime_resume(struct device *dev)
273 struct tegra_gmi *gmi = dev_get_drvdata(dev);
276 err = clk_prepare_enable(gmi->clk);
278 dev_err(gmi->dev, "failed to enable clock: %d\n", err);
285 static int __maybe_unused tegra_gmi_runtime_suspend(struct device *dev)
287 struct tegra_gmi *gmi = dev_get_drvdata(dev);
289 clk_disable_unprepare(gmi->clk);
294 static const struct dev_pm_ops tegra_gmi_pm = {
295 SET_RUNTIME_PM_OPS(tegra_gmi_runtime_suspend, tegra_gmi_runtime_resume,
299 static const struct of_device_id tegra_gmi_id_table[] = {
300 { .compatible = "nvidia,tegra20-gmi", },
301 { .compatible = "nvidia,tegra30-gmi", },
304 MODULE_DEVICE_TABLE(of, tegra_gmi_id_table);
306 static struct platform_driver tegra_gmi_driver = {
307 .probe = tegra_gmi_probe,
308 .remove = tegra_gmi_remove,
311 .of_match_table = tegra_gmi_id_table,
315 module_platform_driver(tegra_gmi_driver);
317 MODULE_AUTHOR("Mirza Krak <mirza.krak@gmail.com");
318 MODULE_DESCRIPTION("NVIDIA Tegra GMI Bus Driver");
319 MODULE_LICENSE("GPL v2");