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>
17 #include <linux/of_address.h>
18 #include <linux/of_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/pruss_driver.h>
21 #include <linux/regmap.h>
22 #include <linux/remoteproc.h>
23 #include <linux/slab.h>
27 * struct pruss_private_data - PRUSS driver private data
28 * @has_no_sharedram: flag to indicate the absence of PRUSS Shared Data RAM
29 * @has_core_mux_clock: flag to indicate the presence of PRUSS core clock
31 struct pruss_private_data {
32 bool has_no_sharedram;
33 bool has_core_mux_clock;
37 * pruss_get() - get the pruss for a given PRU remoteproc
38 * @rproc: remoteproc handle of a PRU instance
40 * Finds the parent pruss device for a PRU given the @rproc handle of the
41 * PRU remote processor. This function increments the pruss device's refcount,
42 * so always use pruss_put() to decrement it back once pruss isn't needed
45 * This API doesn't check if @rproc is valid or not. It is expected the caller
46 * will have done a pru_rproc_get() on @rproc, before calling this API to make
47 * sure that @rproc is valid.
49 * Return: pruss handle on success, and an ERR_PTR on failure using one
50 * of the following error values
51 * -EINVAL if invalid parameter
52 * -ENODEV if PRU device or PRUSS device is not found
54 struct pruss *pruss_get(struct rproc *rproc)
58 struct platform_device *ppdev;
60 if (IS_ERR_OR_NULL(rproc))
61 return ERR_PTR(-EINVAL);
65 /* make sure it is PRU rproc */
66 if (!dev->parent || !is_pru_rproc(dev->parent))
67 return ERR_PTR(-ENODEV);
69 ppdev = to_platform_device(dev->parent->parent);
70 pruss = platform_get_drvdata(ppdev);
72 return ERR_PTR(-ENODEV);
74 get_device(pruss->dev);
78 EXPORT_SYMBOL_GPL(pruss_get);
81 * pruss_put() - decrement pruss device's usecount
82 * @pruss: pruss handle
84 * Complimentary function for pruss_get(). Needs to be called
85 * after the PRUSS is used, and only if the pruss_get() succeeds.
87 void pruss_put(struct pruss *pruss)
89 if (IS_ERR_OR_NULL(pruss))
92 put_device(pruss->dev);
94 EXPORT_SYMBOL_GPL(pruss_put);
97 * pruss_request_mem_region() - request a memory resource
98 * @pruss: the pruss instance
99 * @mem_id: the memory resource id
100 * @region: pointer to memory region structure to be filled in
102 * This function allows a client driver to request a memory resource,
103 * and if successful, will let the client driver own the particular
104 * memory region until released using the pruss_release_mem_region()
107 * Return: 0 if requested memory region is available (in such case pointer to
108 * memory region is returned via @region), an error otherwise
110 int pruss_request_mem_region(struct pruss *pruss, enum pruss_mem mem_id,
111 struct pruss_mem_region *region)
113 if (!pruss || !region || mem_id >= PRUSS_MEM_MAX)
116 mutex_lock(&pruss->lock);
118 if (pruss->mem_in_use[mem_id]) {
119 mutex_unlock(&pruss->lock);
123 *region = pruss->mem_regions[mem_id];
124 pruss->mem_in_use[mem_id] = region;
126 mutex_unlock(&pruss->lock);
130 EXPORT_SYMBOL_GPL(pruss_request_mem_region);
133 * pruss_release_mem_region() - release a memory resource
134 * @pruss: the pruss instance
135 * @region: the memory region to release
137 * This function is the complimentary function to
138 * pruss_request_mem_region(), and allows the client drivers to
139 * release back a memory resource.
141 * Return: 0 on success, an error code otherwise
143 int pruss_release_mem_region(struct pruss *pruss,
144 struct pruss_mem_region *region)
148 if (!pruss || !region)
151 mutex_lock(&pruss->lock);
153 /* find out the memory region being released */
154 for (id = 0; id < PRUSS_MEM_MAX; id++) {
155 if (pruss->mem_in_use[id] == region)
159 if (id == PRUSS_MEM_MAX) {
160 mutex_unlock(&pruss->lock);
164 pruss->mem_in_use[id] = NULL;
166 mutex_unlock(&pruss->lock);
170 EXPORT_SYMBOL_GPL(pruss_release_mem_region);
173 * pruss_cfg_get_gpmux() - get the current GPMUX value for a PRU device
174 * @pruss: pruss instance
175 * @pru_id: PRU identifier (0-1)
176 * @mux: pointer to store the current mux value into
178 * Return: 0 on success, or an error code otherwise
180 int pruss_cfg_get_gpmux(struct pruss *pruss, enum pruss_pru_id pru_id, u8 *mux)
185 if (pru_id >= PRUSS_NUM_PRUS || !mux)
188 ret = pruss_cfg_read(pruss, PRUSS_CFG_GPCFG(pru_id), &val);
190 *mux = (u8)((val & PRUSS_GPCFG_PRU_MUX_SEL_MASK) >>
191 PRUSS_GPCFG_PRU_MUX_SEL_SHIFT);
194 EXPORT_SYMBOL_GPL(pruss_cfg_get_gpmux);
197 * pruss_cfg_set_gpmux() - set the GPMUX value for a PRU device
198 * @pruss: pruss instance
199 * @pru_id: PRU identifier (0-1)
200 * @mux: new mux value for PRU
202 * Return: 0 on success, or an error code otherwise
204 int pruss_cfg_set_gpmux(struct pruss *pruss, enum pruss_pru_id pru_id, u8 mux)
206 if (mux >= PRUSS_GP_MUX_SEL_MAX ||
207 pru_id >= PRUSS_NUM_PRUS)
210 return pruss_cfg_update(pruss, PRUSS_CFG_GPCFG(pru_id),
211 PRUSS_GPCFG_PRU_MUX_SEL_MASK,
212 (u32)mux << PRUSS_GPCFG_PRU_MUX_SEL_SHIFT);
214 EXPORT_SYMBOL_GPL(pruss_cfg_set_gpmux);
217 * pruss_cfg_gpimode() - set the GPI mode of the PRU
218 * @pruss: the pruss instance handle
219 * @pru_id: id of the PRU core within the PRUSS
220 * @mode: GPI mode to set
222 * Sets the GPI mode for a given PRU by programming the
223 * corresponding PRUSS_CFG_GPCFGx register
225 * Return: 0 on success, or an error code otherwise
227 int pruss_cfg_gpimode(struct pruss *pruss, enum pruss_pru_id pru_id,
228 enum pruss_gpi_mode mode)
230 if (pru_id >= PRUSS_NUM_PRUS || mode >= PRUSS_GPI_MODE_MAX)
233 return pruss_cfg_update(pruss, PRUSS_CFG_GPCFG(pru_id),
234 PRUSS_GPCFG_PRU_GPI_MODE_MASK,
235 mode << PRUSS_GPCFG_PRU_GPI_MODE_SHIFT);
237 EXPORT_SYMBOL_GPL(pruss_cfg_gpimode);
240 * pruss_cfg_miirt_enable() - Enable/disable MII RT Events
241 * @pruss: the pruss instance
242 * @enable: enable/disable
244 * Enable/disable the MII RT Events for the PRUSS.
246 * Return: 0 on success, or an error code otherwise
248 int pruss_cfg_miirt_enable(struct pruss *pruss, bool enable)
250 u32 set = enable ? PRUSS_MII_RT_EVENT_EN : 0;
252 return pruss_cfg_update(pruss, PRUSS_CFG_MII_RT,
253 PRUSS_MII_RT_EVENT_EN, set);
255 EXPORT_SYMBOL_GPL(pruss_cfg_miirt_enable);
258 * pruss_cfg_xfr_enable() - Enable/disable XIN XOUT shift functionality
259 * @pruss: the pruss instance
260 * @pru_type: PRU core type identifier
261 * @enable: enable/disable
263 * Return: 0 on success, or an error code otherwise
265 int pruss_cfg_xfr_enable(struct pruss *pruss, enum pru_type pru_type,
272 mask = PRUSS_SPP_XFER_SHIFT_EN;
275 mask = PRUSS_SPP_RTU_XFR_SHIFT_EN;
281 set = enable ? mask : 0;
283 return pruss_cfg_update(pruss, PRUSS_CFG_SPP, mask, set);
285 EXPORT_SYMBOL_GPL(pruss_cfg_xfr_enable);
287 static void pruss_of_free_clk_provider(void *data)
289 struct device_node *clk_mux_np = data;
291 of_clk_del_provider(clk_mux_np);
292 of_node_put(clk_mux_np);
295 static void pruss_clk_unregister_mux(void *data)
297 clk_unregister_mux(data);
300 static int pruss_clk_mux_setup(struct pruss *pruss, struct clk *clk_mux,
301 char *mux_name, struct device_node *clks_np)
303 struct device_node *clk_mux_np;
304 struct device *dev = pruss->dev;
306 unsigned int num_parents;
307 const char **parent_names;
312 clk_mux_np = of_get_child_by_name(clks_np, mux_name);
314 dev_err(dev, "%pOF is missing its '%s' node\n", clks_np,
319 num_parents = of_clk_get_parent_count(clk_mux_np);
320 if (num_parents < 1) {
321 dev_err(dev, "mux-clock %pOF must have parents\n", clk_mux_np);
326 parent_names = devm_kcalloc(dev, sizeof(*parent_names), num_parents,
333 of_clk_parent_fill(clk_mux_np, parent_names, num_parents);
335 clk_mux_name = devm_kasprintf(dev, GFP_KERNEL, "%s.%pOFn",
336 dev_name(dev), clk_mux_np);
342 ret = of_property_read_u32(clk_mux_np, "reg", ®_offset);
346 reg = pruss->cfg_base + reg_offset;
348 clk_mux = clk_register_mux(NULL, clk_mux_name, parent_names,
349 num_parents, 0, reg, 0, 1, 0, NULL);
350 if (IS_ERR(clk_mux)) {
351 ret = PTR_ERR(clk_mux);
355 ret = devm_add_action_or_reset(dev, pruss_clk_unregister_mux, clk_mux);
357 dev_err(dev, "failed to add clkmux unregister action %d", ret);
361 ret = of_clk_add_provider(clk_mux_np, of_clk_src_simple_get, clk_mux);
365 ret = devm_add_action_or_reset(dev, pruss_of_free_clk_provider,
368 dev_err(dev, "failed to add clkmux free action %d", ret);
375 of_node_put(clk_mux_np);
379 static int pruss_clk_init(struct pruss *pruss, struct device_node *cfg_node)
381 const struct pruss_private_data *data;
382 struct device_node *clks_np;
383 struct device *dev = pruss->dev;
386 data = of_device_get_match_data(dev);
388 clks_np = of_get_child_by_name(cfg_node, "clocks");
390 dev_err(dev, "%pOF is missing its 'clocks' node\n", cfg_node);
394 if (data && data->has_core_mux_clock) {
395 ret = pruss_clk_mux_setup(pruss, pruss->core_clk_mux,
396 "coreclk-mux", clks_np);
398 dev_err(dev, "failed to setup coreclk-mux\n");
403 ret = pruss_clk_mux_setup(pruss, pruss->iep_clk_mux, "iepclk-mux",
406 dev_err(dev, "failed to setup iepclk-mux\n");
411 of_node_put(clks_np);
416 static struct regmap_config regmap_conf = {
422 static int pruss_cfg_of_init(struct device *dev, struct pruss *pruss)
424 struct device_node *np = dev_of_node(dev);
425 struct device_node *child;
429 child = of_get_child_by_name(np, "cfg");
431 dev_err(dev, "%pOF is missing its 'cfg' node\n", child);
435 if (of_address_to_resource(child, 0, &res)) {
440 pruss->cfg_base = devm_ioremap(dev, res.start, resource_size(&res));
441 if (!pruss->cfg_base) {
446 regmap_conf.name = kasprintf(GFP_KERNEL, "%pOFn@%llx", child,
448 regmap_conf.max_register = resource_size(&res) - 4;
450 pruss->cfg_regmap = devm_regmap_init_mmio(dev, pruss->cfg_base,
452 kfree(regmap_conf.name);
453 if (IS_ERR(pruss->cfg_regmap)) {
454 dev_err(dev, "regmap_init_mmio failed for cfg, ret = %ld\n",
455 PTR_ERR(pruss->cfg_regmap));
456 ret = PTR_ERR(pruss->cfg_regmap);
460 ret = pruss_clk_init(pruss, child);
462 dev_err(dev, "pruss_clk_init failed, ret = %d\n", ret);
469 static int pruss_probe(struct platform_device *pdev)
471 struct device *dev = &pdev->dev;
472 struct device_node *np = dev_of_node(dev);
473 struct device_node *child;
477 const struct pruss_private_data *data;
478 const char *mem_names[PRUSS_MEM_MAX] = { "dram0", "dram1", "shrdram2" };
480 data = of_device_get_match_data(&pdev->dev);
482 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
484 dev_err(dev, "failed to set the DMA coherent mask");
488 pruss = devm_kzalloc(dev, sizeof(*pruss), GFP_KERNEL);
493 mutex_init(&pruss->lock);
495 child = of_get_child_by_name(np, "memories");
497 dev_err(dev, "%pOF is missing its 'memories' node\n", child);
501 for (i = 0; i < PRUSS_MEM_MAX; i++) {
503 * On AM437x one of two PRUSS units don't contain Shared RAM,
506 if (data && data->has_no_sharedram && i == PRUSS_MEM_SHRD_RAM2)
509 index = of_property_match_string(child, "reg-names",
516 if (of_address_to_resource(child, index, &res)) {
521 pruss->mem_regions[i].va = devm_ioremap(dev, res.start,
522 resource_size(&res));
523 if (!pruss->mem_regions[i].va) {
524 dev_err(dev, "failed to parse and map memory resource %d %s\n",
529 pruss->mem_regions[i].pa = res.start;
530 pruss->mem_regions[i].size = resource_size(&res);
532 dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %pK\n",
533 mem_names[i], &pruss->mem_regions[i].pa,
534 pruss->mem_regions[i].size, pruss->mem_regions[i].va);
538 platform_set_drvdata(pdev, pruss);
540 pm_runtime_enable(dev);
541 ret = pm_runtime_resume_and_get(dev);
543 dev_err(dev, "couldn't enable module\n");
547 ret = pruss_cfg_of_init(dev, pruss);
551 ret = devm_of_platform_populate(dev);
553 dev_err(dev, "failed to register child devices\n");
560 pm_runtime_put_sync(dev);
562 pm_runtime_disable(dev);
566 static int pruss_remove(struct platform_device *pdev)
568 struct device *dev = &pdev->dev;
570 devm_of_platform_depopulate(dev);
572 pm_runtime_put_sync(dev);
573 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 = pruss_remove,
613 module_platform_driver(pruss_driver);
616 MODULE_DESCRIPTION("PRU-ICSS Subsystem Driver");
617 MODULE_LICENSE("GPL v2");