2 * Driver for NVIDIA Generic Memory Interface
4 * Copyright (C) 2016 Host Mobility AB. All rights reserved.
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.
11 #include <linux/clk.h>
12 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/reset.h>
19 #include <soc/tegra/common.h>
21 #define TEGRA_GMI_CONFIG 0x00
22 #define TEGRA_GMI_CONFIG_GO BIT(31)
23 #define TEGRA_GMI_BUS_WIDTH_32BIT BIT(30)
24 #define TEGRA_GMI_MUX_MODE BIT(28)
25 #define TEGRA_GMI_RDY_BEFORE_DATA BIT(24)
26 #define TEGRA_GMI_RDY_ACTIVE_HIGH BIT(23)
27 #define TEGRA_GMI_ADV_ACTIVE_HIGH BIT(22)
28 #define TEGRA_GMI_OE_ACTIVE_HIGH BIT(21)
29 #define TEGRA_GMI_CS_ACTIVE_HIGH BIT(20)
30 #define TEGRA_GMI_CS_SELECT(x) ((x & 0x7) << 4)
32 #define TEGRA_GMI_TIMING0 0x10
33 #define TEGRA_GMI_MUXED_WIDTH(x) ((x & 0xf) << 12)
34 #define TEGRA_GMI_HOLD_WIDTH(x) ((x & 0xf) << 8)
35 #define TEGRA_GMI_ADV_WIDTH(x) ((x & 0xf) << 4)
36 #define TEGRA_GMI_CE_WIDTH(x) (x & 0xf)
38 #define TEGRA_GMI_TIMING1 0x14
39 #define TEGRA_GMI_WE_WIDTH(x) ((x & 0xff) << 16)
40 #define TEGRA_GMI_OE_WIDTH(x) ((x & 0xff) << 8)
41 #define TEGRA_GMI_WAIT_WIDTH(x) (x & 0xff)
43 #define TEGRA_GMI_MAX_CHIP_SELECT 8
49 struct reset_control *rst;
56 static int tegra_gmi_enable(struct tegra_gmi *gmi)
60 pm_runtime_enable(gmi->dev);
61 err = pm_runtime_resume_and_get(gmi->dev);
63 pm_runtime_disable(gmi->dev);
67 reset_control_assert(gmi->rst);
68 usleep_range(2000, 4000);
69 reset_control_deassert(gmi->rst);
71 writel(gmi->snor_timing0, gmi->base + TEGRA_GMI_TIMING0);
72 writel(gmi->snor_timing1, gmi->base + TEGRA_GMI_TIMING1);
74 gmi->snor_config |= TEGRA_GMI_CONFIG_GO;
75 writel(gmi->snor_config, gmi->base + TEGRA_GMI_CONFIG);
80 static void tegra_gmi_disable(struct tegra_gmi *gmi)
84 /* stop GMI operation */
85 config = readl(gmi->base + TEGRA_GMI_CONFIG);
86 config &= ~TEGRA_GMI_CONFIG_GO;
87 writel(config, gmi->base + TEGRA_GMI_CONFIG);
89 reset_control_assert(gmi->rst);
91 pm_runtime_put_sync_suspend(gmi->dev);
92 pm_runtime_force_suspend(gmi->dev);
95 static int tegra_gmi_parse_dt(struct tegra_gmi *gmi)
97 struct device_node *child;
98 u32 property, ranges[4];
101 child = of_get_next_available_child(gmi->dev->of_node, NULL);
103 dev_err(gmi->dev, "no child nodes found\n");
108 * We currently only support one child device due to lack of
109 * chip-select address decoding. Which means that we only have one
110 * chip-select line from the GMI controller.
112 if (of_get_child_count(gmi->dev->of_node) > 1)
113 dev_warn(gmi->dev, "only one child device is supported.");
115 if (of_property_read_bool(child, "nvidia,snor-data-width-32bit"))
116 gmi->snor_config |= TEGRA_GMI_BUS_WIDTH_32BIT;
118 if (of_property_read_bool(child, "nvidia,snor-mux-mode"))
119 gmi->snor_config |= TEGRA_GMI_MUX_MODE;
121 if (of_property_read_bool(child, "nvidia,snor-rdy-active-before-data"))
122 gmi->snor_config |= TEGRA_GMI_RDY_BEFORE_DATA;
124 if (of_property_read_bool(child, "nvidia,snor-rdy-active-high"))
125 gmi->snor_config |= TEGRA_GMI_RDY_ACTIVE_HIGH;
127 if (of_property_read_bool(child, "nvidia,snor-adv-active-high"))
128 gmi->snor_config |= TEGRA_GMI_ADV_ACTIVE_HIGH;
130 if (of_property_read_bool(child, "nvidia,snor-oe-active-high"))
131 gmi->snor_config |= TEGRA_GMI_OE_ACTIVE_HIGH;
133 if (of_property_read_bool(child, "nvidia,snor-cs-active-high"))
134 gmi->snor_config |= TEGRA_GMI_CS_ACTIVE_HIGH;
137 err = of_property_read_u32_array(child, "ranges", ranges, 4);
139 /* Invalid binding */
140 if (err == -EOVERFLOW) {
142 "failed to decode CS: invalid ranges length\n");
147 * If we reach here it means that the child node has an empty
148 * ranges or it does not exist at all. Attempt to decode the
149 * CS# from the reg property instead.
151 err = of_property_read_u32(child, "reg", &property);
154 "failed to decode CS: no reg property found\n");
158 property = ranges[1];
161 /* Valid chip selects are CS0-CS7 */
162 if (property >= TEGRA_GMI_MAX_CHIP_SELECT) {
163 dev_err(gmi->dev, "invalid chip select: %d", property);
168 gmi->snor_config |= TEGRA_GMI_CS_SELECT(property);
170 /* The default values that are provided below are reset values */
171 if (!of_property_read_u32(child, "nvidia,snor-muxed-width", &property))
172 gmi->snor_timing0 |= TEGRA_GMI_MUXED_WIDTH(property);
174 gmi->snor_timing0 |= TEGRA_GMI_MUXED_WIDTH(1);
176 if (!of_property_read_u32(child, "nvidia,snor-hold-width", &property))
177 gmi->snor_timing0 |= TEGRA_GMI_HOLD_WIDTH(property);
179 gmi->snor_timing0 |= TEGRA_GMI_HOLD_WIDTH(1);
181 if (!of_property_read_u32(child, "nvidia,snor-adv-width", &property))
182 gmi->snor_timing0 |= TEGRA_GMI_ADV_WIDTH(property);
184 gmi->snor_timing0 |= TEGRA_GMI_ADV_WIDTH(1);
186 if (!of_property_read_u32(child, "nvidia,snor-ce-width", &property))
187 gmi->snor_timing0 |= TEGRA_GMI_CE_WIDTH(property);
189 gmi->snor_timing0 |= TEGRA_GMI_CE_WIDTH(4);
191 if (!of_property_read_u32(child, "nvidia,snor-we-width", &property))
192 gmi->snor_timing1 |= TEGRA_GMI_WE_WIDTH(property);
194 gmi->snor_timing1 |= TEGRA_GMI_WE_WIDTH(1);
196 if (!of_property_read_u32(child, "nvidia,snor-oe-width", &property))
197 gmi->snor_timing1 |= TEGRA_GMI_OE_WIDTH(property);
199 gmi->snor_timing1 |= TEGRA_GMI_OE_WIDTH(1);
201 if (!of_property_read_u32(child, "nvidia,snor-wait-width", &property))
202 gmi->snor_timing1 |= TEGRA_GMI_WAIT_WIDTH(property);
204 gmi->snor_timing1 |= TEGRA_GMI_WAIT_WIDTH(3);
211 static int tegra_gmi_probe(struct platform_device *pdev)
213 struct device *dev = &pdev->dev;
214 struct tegra_gmi *gmi;
215 struct resource *res;
218 gmi = devm_kzalloc(dev, sizeof(*gmi), GFP_KERNEL);
222 platform_set_drvdata(pdev, gmi);
225 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
226 gmi->base = devm_ioremap_resource(dev, res);
227 if (IS_ERR(gmi->base))
228 return PTR_ERR(gmi->base);
230 gmi->clk = devm_clk_get(dev, "gmi");
231 if (IS_ERR(gmi->clk)) {
232 dev_err(dev, "can not get clock\n");
233 return PTR_ERR(gmi->clk);
236 gmi->rst = devm_reset_control_get(dev, "gmi");
237 if (IS_ERR(gmi->rst)) {
238 dev_err(dev, "can not get reset\n");
239 return PTR_ERR(gmi->rst);
242 err = devm_tegra_core_dev_init_opp_table_common(&pdev->dev);
246 err = tegra_gmi_parse_dt(gmi);
250 err = tegra_gmi_enable(gmi);
254 err = of_platform_default_populate(dev->of_node, NULL, dev);
256 dev_err(dev, "fail to create devices.\n");
257 tegra_gmi_disable(gmi);
264 static int tegra_gmi_remove(struct platform_device *pdev)
266 struct tegra_gmi *gmi = platform_get_drvdata(pdev);
268 of_platform_depopulate(gmi->dev);
269 tegra_gmi_disable(gmi);
274 static int __maybe_unused tegra_gmi_runtime_resume(struct device *dev)
276 struct tegra_gmi *gmi = dev_get_drvdata(dev);
279 err = clk_prepare_enable(gmi->clk);
281 dev_err(gmi->dev, "failed to enable clock: %d\n", err);
288 static int __maybe_unused tegra_gmi_runtime_suspend(struct device *dev)
290 struct tegra_gmi *gmi = dev_get_drvdata(dev);
292 clk_disable_unprepare(gmi->clk);
297 static const struct dev_pm_ops tegra_gmi_pm = {
298 SET_RUNTIME_PM_OPS(tegra_gmi_runtime_suspend, tegra_gmi_runtime_resume,
302 static const struct of_device_id tegra_gmi_id_table[] = {
303 { .compatible = "nvidia,tegra20-gmi", },
304 { .compatible = "nvidia,tegra30-gmi", },
307 MODULE_DEVICE_TABLE(of, tegra_gmi_id_table);
309 static struct platform_driver tegra_gmi_driver = {
310 .probe = tegra_gmi_probe,
311 .remove = tegra_gmi_remove,
314 .of_match_table = tegra_gmi_id_table,
318 module_platform_driver(tegra_gmi_driver);
320 MODULE_AUTHOR("Mirza Krak <mirza.krak@gmail.com");
321 MODULE_DESCRIPTION("NVIDIA Tegra GMI Bus Driver");
322 MODULE_LICENSE("GPL v2");