1 // SPDX-License-Identifier: GPL-2.0-only
3 * PRU-ICSS platform driver for various TI SoCs
5 * Copyright (C) 2014-2020 Texas Instruments Incorporated - http://www.ti.com/
7 * Suman Anna <s-anna@ti.com>
8 * Andrew F. Davis <afd@ti.com>
11 #include <linux/clk-provider.h>
12 #include <linux/dma-mapping.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/of_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/pruss_driver.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
24 * struct pruss_private_data - PRUSS driver private data
25 * @has_no_sharedram: flag to indicate the absence of PRUSS Shared Data RAM
26 * @has_core_mux_clock: flag to indicate the presence of PRUSS core clock
28 struct pruss_private_data {
29 bool has_no_sharedram;
30 bool has_core_mux_clock;
33 static void pruss_of_free_clk_provider(void *data)
35 struct device_node *clk_mux_np = data;
37 of_clk_del_provider(clk_mux_np);
38 of_node_put(clk_mux_np);
41 static int pruss_clk_mux_setup(struct pruss *pruss, struct clk *clk_mux,
42 char *mux_name, struct device_node *clks_np)
44 struct device_node *clk_mux_np;
45 struct device *dev = pruss->dev;
47 unsigned int num_parents;
48 const char **parent_names;
53 clk_mux_np = of_get_child_by_name(clks_np, mux_name);
55 dev_err(dev, "%pOF is missing its '%s' node\n", clks_np,
60 num_parents = of_clk_get_parent_count(clk_mux_np);
61 if (num_parents < 1) {
62 dev_err(dev, "mux-clock %pOF must have parents\n", clk_mux_np);
67 parent_names = devm_kcalloc(dev, sizeof(*parent_names), num_parents,
74 of_clk_parent_fill(clk_mux_np, parent_names, num_parents);
76 clk_mux_name = devm_kasprintf(dev, GFP_KERNEL, "%s.%pOFn",
77 dev_name(dev), clk_mux_np);
83 ret = of_property_read_u32(clk_mux_np, "reg", ®_offset);
87 reg = pruss->cfg_base + reg_offset;
89 clk_mux = clk_register_mux(NULL, clk_mux_name, parent_names,
90 num_parents, 0, reg, 0, 1, 0, NULL);
91 if (IS_ERR(clk_mux)) {
92 ret = PTR_ERR(clk_mux);
96 ret = devm_add_action_or_reset(dev, (void(*)(void *))clk_unregister_mux,
99 dev_err(dev, "failed to add clkmux unregister action %d", ret);
103 ret = of_clk_add_provider(clk_mux_np, of_clk_src_simple_get, clk_mux);
107 ret = devm_add_action_or_reset(dev, pruss_of_free_clk_provider,
110 dev_err(dev, "failed to add clkmux free action %d", ret);
117 of_node_put(clk_mux_np);
121 static int pruss_clk_init(struct pruss *pruss, struct device_node *cfg_node)
123 const struct pruss_private_data *data;
124 struct device_node *clks_np;
125 struct device *dev = pruss->dev;
128 data = of_device_get_match_data(dev);
132 clks_np = of_get_child_by_name(cfg_node, "clocks");
134 dev_err(dev, "%pOF is missing its 'clocks' node\n", cfg_node);
138 if (data && data->has_core_mux_clock) {
139 ret = pruss_clk_mux_setup(pruss, pruss->core_clk_mux,
140 "coreclk-mux", clks_np);
142 dev_err(dev, "failed to setup coreclk-mux\n");
147 ret = pruss_clk_mux_setup(pruss, pruss->iep_clk_mux, "iepclk-mux",
150 dev_err(dev, "failed to setup iepclk-mux\n");
155 of_node_put(clks_np);
160 static struct regmap_config regmap_conf = {
166 static int pruss_probe(struct platform_device *pdev)
168 struct device *dev = &pdev->dev;
169 struct device_node *np = dev_of_node(dev);
170 struct device_node *child;
174 const struct pruss_private_data *data;
175 const char *mem_names[PRUSS_MEM_MAX] = { "dram0", "dram1", "shrdram2" };
177 data = of_device_get_match_data(&pdev->dev);
179 dev_err(dev, "missing private data\n");
183 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
185 dev_err(dev, "failed to set the DMA coherent mask");
189 pruss = devm_kzalloc(dev, sizeof(*pruss), GFP_KERNEL);
195 child = of_get_child_by_name(np, "memories");
197 dev_err(dev, "%pOF is missing its 'memories' node\n", child);
201 for (i = 0; i < PRUSS_MEM_MAX; i++) {
203 * On AM437x one of two PRUSS units don't contain Shared RAM,
206 if (data && data->has_no_sharedram && i == PRUSS_MEM_SHRD_RAM2)
209 index = of_property_match_string(child, "reg-names",
216 if (of_address_to_resource(child, index, &res)) {
221 pruss->mem_regions[i].va = devm_ioremap(dev, res.start,
222 resource_size(&res));
223 if (!pruss->mem_regions[i].va) {
224 dev_err(dev, "failed to parse and map memory resource %d %s\n",
229 pruss->mem_regions[i].pa = res.start;
230 pruss->mem_regions[i].size = resource_size(&res);
232 dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %pK\n",
233 mem_names[i], &pruss->mem_regions[i].pa,
234 pruss->mem_regions[i].size, pruss->mem_regions[i].va);
238 platform_set_drvdata(pdev, pruss);
240 pm_runtime_enable(dev);
241 ret = pm_runtime_get_sync(dev);
243 dev_err(dev, "couldn't enable module\n");
244 pm_runtime_put_noidle(dev);
248 child = of_get_child_by_name(np, "cfg");
250 dev_err(dev, "%pOF is missing its 'cfg' node\n", child);
255 if (of_address_to_resource(child, 0, &res)) {
260 pruss->cfg_base = devm_ioremap(dev, res.start, resource_size(&res));
261 if (!pruss->cfg_base) {
266 regmap_conf.name = kasprintf(GFP_KERNEL, "%pOFn@%llx", child,
268 regmap_conf.max_register = resource_size(&res) - 4;
270 pruss->cfg_regmap = devm_regmap_init_mmio(dev, pruss->cfg_base,
272 kfree(regmap_conf.name);
273 if (IS_ERR(pruss->cfg_regmap)) {
274 dev_err(dev, "regmap_init_mmio failed for cfg, ret = %ld\n",
275 PTR_ERR(pruss->cfg_regmap));
276 ret = PTR_ERR(pruss->cfg_regmap);
280 ret = pruss_clk_init(pruss, child);
282 dev_err(dev, "failed to setup coreclk-mux\n");
286 ret = devm_of_platform_populate(dev);
288 dev_err(dev, "failed to register child devices\n");
299 pm_runtime_put_sync(dev);
301 pm_runtime_disable(dev);
305 static int pruss_remove(struct platform_device *pdev)
307 struct device *dev = &pdev->dev;
309 devm_of_platform_depopulate(dev);
311 pm_runtime_put_sync(dev);
312 pm_runtime_disable(dev);
317 /* instance-specific driver private data */
318 static const struct pruss_private_data am437x_pruss1_data = {
319 .has_no_sharedram = false,
322 static const struct pruss_private_data am437x_pruss0_data = {
323 .has_no_sharedram = true,
326 static const struct pruss_private_data am65x_j721e_pruss_data = {
327 .has_core_mux_clock = true,
330 static const struct of_device_id pruss_of_match[] = {
331 { .compatible = "ti,am3356-pruss" },
332 { .compatible = "ti,am4376-pruss0", .data = &am437x_pruss0_data, },
333 { .compatible = "ti,am4376-pruss1", .data = &am437x_pruss1_data, },
334 { .compatible = "ti,am5728-pruss" },
335 { .compatible = "ti,k2g-pruss" },
336 { .compatible = "ti,am654-icssg", .data = &am65x_j721e_pruss_data, },
337 { .compatible = "ti,j721e-icssg", .data = &am65x_j721e_pruss_data, },
340 MODULE_DEVICE_TABLE(of, pruss_of_match);
342 static struct platform_driver pruss_driver = {
345 .of_match_table = pruss_of_match,
347 .probe = pruss_probe,
348 .remove = pruss_remove,
350 module_platform_driver(pruss_driver);
352 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
353 MODULE_DESCRIPTION("PRU-ICSS Subsystem Driver");
354 MODULE_LICENSE("GPL v2");