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/
12 #include <linux/clk-provider.h>
13 #include <linux/dma-mapping.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/module.h>
18 #include <linux/of_address.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/pruss_driver.h>
23 #include <linux/regmap.h>
24 #include <linux/remoteproc.h>
25 #include <linux/slab.h>
29 * struct pruss_private_data - PRUSS driver private data
30 * @has_no_sharedram: flag to indicate the absence of PRUSS Shared Data RAM
31 * @has_core_mux_clock: flag to indicate the presence of PRUSS core clock
33 struct pruss_private_data {
34 bool has_no_sharedram;
35 bool has_core_mux_clock;
39 * pruss_get() - get the pruss for a given PRU remoteproc
40 * @rproc: remoteproc handle of a PRU instance
42 * Finds the parent pruss device for a PRU given the @rproc handle of the
43 * PRU remote processor. This function increments the pruss device's refcount,
44 * so always use pruss_put() to decrement it back once pruss isn't needed
47 * This API doesn't check if @rproc is valid or not. It is expected the caller
48 * will have done a pru_rproc_get() on @rproc, before calling this API to make
49 * sure that @rproc is valid.
51 * Return: pruss handle on success, and an ERR_PTR on failure using one
52 * of the following error values
53 * -EINVAL if invalid parameter
54 * -ENODEV if PRU device or PRUSS device is not found
56 struct pruss *pruss_get(struct rproc *rproc)
60 struct platform_device *ppdev;
62 if (IS_ERR_OR_NULL(rproc))
63 return ERR_PTR(-EINVAL);
67 /* make sure it is PRU rproc */
68 if (!dev->parent || !is_pru_rproc(dev->parent))
69 return ERR_PTR(-ENODEV);
71 ppdev = to_platform_device(dev->parent->parent);
72 pruss = platform_get_drvdata(ppdev);
74 return ERR_PTR(-ENODEV);
76 get_device(pruss->dev);
80 EXPORT_SYMBOL_GPL(pruss_get);
83 * pruss_put() - decrement pruss device's usecount
84 * @pruss: pruss handle
86 * Complimentary function for pruss_get(). Needs to be called
87 * after the PRUSS is used, and only if the pruss_get() succeeds.
89 void pruss_put(struct pruss *pruss)
91 if (IS_ERR_OR_NULL(pruss))
94 put_device(pruss->dev);
96 EXPORT_SYMBOL_GPL(pruss_put);
99 * pruss_request_mem_region() - request a memory resource
100 * @pruss: the pruss instance
101 * @mem_id: the memory resource id
102 * @region: pointer to memory region structure to be filled in
104 * This function allows a client driver to request a memory resource,
105 * and if successful, will let the client driver own the particular
106 * memory region until released using the pruss_release_mem_region()
109 * Return: 0 if requested memory region is available (in such case pointer to
110 * memory region is returned via @region), an error otherwise
112 int pruss_request_mem_region(struct pruss *pruss, enum pruss_mem mem_id,
113 struct pruss_mem_region *region)
115 if (!pruss || !region || mem_id >= PRUSS_MEM_MAX)
118 mutex_lock(&pruss->lock);
120 if (pruss->mem_in_use[mem_id]) {
121 mutex_unlock(&pruss->lock);
125 *region = pruss->mem_regions[mem_id];
126 pruss->mem_in_use[mem_id] = region;
128 mutex_unlock(&pruss->lock);
132 EXPORT_SYMBOL_GPL(pruss_request_mem_region);
135 * pruss_release_mem_region() - release a memory resource
136 * @pruss: the pruss instance
137 * @region: the memory region to release
139 * This function is the complimentary function to
140 * pruss_request_mem_region(), and allows the client drivers to
141 * release back a memory resource.
143 * Return: 0 on success, an error code otherwise
145 int pruss_release_mem_region(struct pruss *pruss,
146 struct pruss_mem_region *region)
150 if (!pruss || !region)
153 mutex_lock(&pruss->lock);
155 /* find out the memory region being released */
156 for (id = 0; id < PRUSS_MEM_MAX; id++) {
157 if (pruss->mem_in_use[id] == region)
161 if (id == PRUSS_MEM_MAX) {
162 mutex_unlock(&pruss->lock);
166 pruss->mem_in_use[id] = NULL;
168 mutex_unlock(&pruss->lock);
172 EXPORT_SYMBOL_GPL(pruss_release_mem_region);
175 * pruss_cfg_get_gpmux() - get the current GPMUX value for a PRU device
176 * @pruss: pruss instance
177 * @pru_id: PRU identifier (0-1)
178 * @mux: pointer to store the current mux value into
180 * Return: 0 on success, or an error code otherwise
182 int pruss_cfg_get_gpmux(struct pruss *pruss, enum pruss_pru_id pru_id, u8 *mux)
187 if (pru_id >= PRUSS_NUM_PRUS || !mux)
190 ret = pruss_cfg_read(pruss, PRUSS_CFG_GPCFG(pru_id), &val);
192 *mux = (u8)((val & PRUSS_GPCFG_PRU_MUX_SEL_MASK) >>
193 PRUSS_GPCFG_PRU_MUX_SEL_SHIFT);
196 EXPORT_SYMBOL_GPL(pruss_cfg_get_gpmux);
199 * pruss_cfg_set_gpmux() - set the GPMUX value for a PRU device
200 * @pruss: pruss instance
201 * @pru_id: PRU identifier (0-1)
202 * @mux: new mux value for PRU
204 * Return: 0 on success, or an error code otherwise
206 int pruss_cfg_set_gpmux(struct pruss *pruss, enum pruss_pru_id pru_id, u8 mux)
208 if (mux >= PRUSS_GP_MUX_SEL_MAX ||
209 pru_id >= PRUSS_NUM_PRUS)
212 return pruss_cfg_update(pruss, PRUSS_CFG_GPCFG(pru_id),
213 PRUSS_GPCFG_PRU_MUX_SEL_MASK,
214 (u32)mux << PRUSS_GPCFG_PRU_MUX_SEL_SHIFT);
216 EXPORT_SYMBOL_GPL(pruss_cfg_set_gpmux);
219 * pruss_cfg_gpimode() - set the GPI mode of the PRU
220 * @pruss: the pruss instance handle
221 * @pru_id: id of the PRU core within the PRUSS
222 * @mode: GPI mode to set
224 * Sets the GPI mode for a given PRU by programming the
225 * corresponding PRUSS_CFG_GPCFGx register
227 * Return: 0 on success, or an error code otherwise
229 int pruss_cfg_gpimode(struct pruss *pruss, enum pruss_pru_id pru_id,
230 enum pruss_gpi_mode mode)
232 if (pru_id >= PRUSS_NUM_PRUS || mode >= PRUSS_GPI_MODE_MAX)
235 return pruss_cfg_update(pruss, PRUSS_CFG_GPCFG(pru_id),
236 PRUSS_GPCFG_PRU_GPI_MODE_MASK,
237 mode << PRUSS_GPCFG_PRU_GPI_MODE_SHIFT);
239 EXPORT_SYMBOL_GPL(pruss_cfg_gpimode);
242 * pruss_cfg_miirt_enable() - Enable/disable MII RT Events
243 * @pruss: the pruss instance
244 * @enable: enable/disable
246 * Enable/disable the MII RT Events for the PRUSS.
248 * Return: 0 on success, or an error code otherwise
250 int pruss_cfg_miirt_enable(struct pruss *pruss, bool enable)
252 u32 set = enable ? PRUSS_MII_RT_EVENT_EN : 0;
254 return pruss_cfg_update(pruss, PRUSS_CFG_MII_RT,
255 PRUSS_MII_RT_EVENT_EN, set);
257 EXPORT_SYMBOL_GPL(pruss_cfg_miirt_enable);
260 * pruss_cfg_xfr_enable() - Enable/disable XIN XOUT shift functionality
261 * @pruss: the pruss instance
262 * @pru_type: PRU core type identifier
263 * @enable: enable/disable
265 * Return: 0 on success, or an error code otherwise
267 int pruss_cfg_xfr_enable(struct pruss *pruss, enum pru_type pru_type,
274 mask = PRUSS_SPP_XFER_SHIFT_EN;
277 mask = PRUSS_SPP_RTU_XFR_SHIFT_EN;
283 set = enable ? mask : 0;
285 return pruss_cfg_update(pruss, PRUSS_CFG_SPP, mask, set);
287 EXPORT_SYMBOL_GPL(pruss_cfg_xfr_enable);
289 static void pruss_of_free_clk_provider(void *data)
291 struct device_node *clk_mux_np = data;
293 of_clk_del_provider(clk_mux_np);
294 of_node_put(clk_mux_np);
297 static void pruss_clk_unregister_mux(void *data)
299 clk_unregister_mux(data);
302 static int pruss_clk_mux_setup(struct pruss *pruss, struct clk *clk_mux,
303 char *mux_name, struct device_node *clks_np)
305 struct device_node *clk_mux_np;
306 struct device *dev = pruss->dev;
308 unsigned int num_parents;
309 const char **parent_names;
314 clk_mux_np = of_get_child_by_name(clks_np, mux_name);
316 dev_err(dev, "%pOF is missing its '%s' node\n", clks_np,
321 num_parents = of_clk_get_parent_count(clk_mux_np);
322 if (num_parents < 1) {
323 dev_err(dev, "mux-clock %pOF must have parents\n", clk_mux_np);
328 parent_names = devm_kcalloc(dev, sizeof(*parent_names), num_parents,
335 of_clk_parent_fill(clk_mux_np, parent_names, num_parents);
337 clk_mux_name = devm_kasprintf(dev, GFP_KERNEL, "%s.%pOFn",
338 dev_name(dev), clk_mux_np);
344 ret = of_property_read_u32(clk_mux_np, "reg", ®_offset);
348 reg = pruss->cfg_base + reg_offset;
350 clk_mux = clk_register_mux(NULL, clk_mux_name, parent_names,
351 num_parents, 0, reg, 0, 1, 0, NULL);
352 if (IS_ERR(clk_mux)) {
353 ret = PTR_ERR(clk_mux);
357 ret = devm_add_action_or_reset(dev, pruss_clk_unregister_mux, clk_mux);
359 dev_err(dev, "failed to add clkmux unregister action %d", ret);
363 ret = of_clk_add_provider(clk_mux_np, of_clk_src_simple_get, clk_mux);
367 ret = devm_add_action_or_reset(dev, pruss_of_free_clk_provider,
370 dev_err(dev, "failed to add clkmux free action %d", ret);
377 of_node_put(clk_mux_np);
381 static int pruss_clk_init(struct pruss *pruss, struct device_node *cfg_node)
383 const struct pruss_private_data *data;
384 struct device_node *clks_np;
385 struct device *dev = pruss->dev;
388 data = of_device_get_match_data(dev);
390 clks_np = of_get_child_by_name(cfg_node, "clocks");
392 dev_err(dev, "%pOF is missing its 'clocks' node\n", cfg_node);
396 if (data && data->has_core_mux_clock) {
397 ret = pruss_clk_mux_setup(pruss, pruss->core_clk_mux,
398 "coreclk-mux", clks_np);
400 dev_err(dev, "failed to setup coreclk-mux\n");
405 ret = pruss_clk_mux_setup(pruss, pruss->iep_clk_mux, "iepclk-mux",
408 dev_err(dev, "failed to setup iepclk-mux\n");
413 of_node_put(clks_np);
418 static struct regmap_config regmap_conf = {
424 static int pruss_cfg_of_init(struct device *dev, struct pruss *pruss)
426 struct device_node *np = dev_of_node(dev);
427 struct device_node *child;
431 child = of_get_child_by_name(np, "cfg");
433 dev_err(dev, "%pOF is missing its 'cfg' node\n", child);
437 if (of_address_to_resource(child, 0, &res)) {
442 pruss->cfg_base = devm_ioremap(dev, res.start, resource_size(&res));
443 if (!pruss->cfg_base) {
448 regmap_conf.name = kasprintf(GFP_KERNEL, "%pOFn@%llx", child,
450 regmap_conf.max_register = resource_size(&res) - 4;
452 pruss->cfg_regmap = devm_regmap_init_mmio(dev, pruss->cfg_base,
454 kfree(regmap_conf.name);
455 if (IS_ERR(pruss->cfg_regmap)) {
456 dev_err(dev, "regmap_init_mmio failed for cfg, ret = %ld\n",
457 PTR_ERR(pruss->cfg_regmap));
458 ret = PTR_ERR(pruss->cfg_regmap);
462 ret = pruss_clk_init(pruss, child);
464 dev_err(dev, "pruss_clk_init failed, ret = %d\n", ret);
471 static int pruss_probe(struct platform_device *pdev)
473 struct device *dev = &pdev->dev;
474 struct device_node *np = dev_of_node(dev);
475 struct device_node *child;
479 const struct pruss_private_data *data;
480 const char *mem_names[PRUSS_MEM_MAX] = { "dram0", "dram1", "shrdram2" };
482 data = of_device_get_match_data(&pdev->dev);
484 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
486 dev_err(dev, "failed to set the DMA coherent mask");
490 pruss = devm_kzalloc(dev, sizeof(*pruss), GFP_KERNEL);
495 mutex_init(&pruss->lock);
497 child = of_get_child_by_name(np, "memories");
499 dev_err(dev, "%pOF is missing its 'memories' node\n", child);
503 for (i = 0; i < PRUSS_MEM_MAX; i++) {
505 * On AM437x one of two PRUSS units don't contain Shared RAM,
508 if (data && data->has_no_sharedram && i == PRUSS_MEM_SHRD_RAM2)
511 index = of_property_match_string(child, "reg-names",
518 if (of_address_to_resource(child, index, &res)) {
523 pruss->mem_regions[i].va = devm_ioremap(dev, res.start,
524 resource_size(&res));
525 if (!pruss->mem_regions[i].va) {
526 dev_err(dev, "failed to parse and map memory resource %d %s\n",
531 pruss->mem_regions[i].pa = res.start;
532 pruss->mem_regions[i].size = resource_size(&res);
534 dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %pK\n",
535 mem_names[i], &pruss->mem_regions[i].pa,
536 pruss->mem_regions[i].size, pruss->mem_regions[i].va);
540 platform_set_drvdata(pdev, pruss);
542 pm_runtime_enable(dev);
543 ret = pm_runtime_resume_and_get(dev);
545 dev_err(dev, "couldn't enable module\n");
549 ret = pruss_cfg_of_init(dev, pruss);
553 ret = devm_of_platform_populate(dev);
555 dev_err(dev, "failed to register child devices\n");
562 pm_runtime_put_sync(dev);
564 pm_runtime_disable(dev);
568 static void pruss_remove(struct platform_device *pdev)
570 struct device *dev = &pdev->dev;
572 devm_of_platform_depopulate(dev);
574 pm_runtime_put_sync(dev);
575 pm_runtime_disable(dev);
578 /* instance-specific driver private data */
579 static const struct pruss_private_data am437x_pruss1_data = {
580 .has_no_sharedram = false,
583 static const struct pruss_private_data am437x_pruss0_data = {
584 .has_no_sharedram = true,
587 static const struct pruss_private_data am65x_j721e_pruss_data = {
588 .has_core_mux_clock = true,
591 static const struct of_device_id pruss_of_match[] = {
592 { .compatible = "ti,am3356-pruss" },
593 { .compatible = "ti,am4376-pruss0", .data = &am437x_pruss0_data, },
594 { .compatible = "ti,am4376-pruss1", .data = &am437x_pruss1_data, },
595 { .compatible = "ti,am5728-pruss" },
596 { .compatible = "ti,k2g-pruss" },
597 { .compatible = "ti,am654-icssg", .data = &am65x_j721e_pruss_data, },
598 { .compatible = "ti,j721e-icssg", .data = &am65x_j721e_pruss_data, },
599 { .compatible = "ti,am642-icssg", .data = &am65x_j721e_pruss_data, },
600 { .compatible = "ti,am625-pruss", .data = &am65x_j721e_pruss_data, },
603 MODULE_DEVICE_TABLE(of, pruss_of_match);
605 static struct platform_driver pruss_driver = {
608 .of_match_table = pruss_of_match,
610 .probe = pruss_probe,
611 .remove_new = pruss_remove,
613 module_platform_driver(pruss_driver);
616 MODULE_DESCRIPTION("PRU-ICSS Subsystem Driver");
617 MODULE_LICENSE("GPL v2");