1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * PRU-ICSS platform driver for various TI SoCs 4 * 5 * Copyright (C) 2014-2020 Texas Instruments Incorporated - http://www.ti.com/ 6 * Author(s): 7 * Suman Anna <s-anna@ti.com> 8 * Andrew F. Davis <afd@ti.com> 9 * Tero Kristo <t-kristo@ti.com> 10 */ 11 12 #include <linux/clk-provider.h> 13 #include <linux/dma-mapping.h> 14 #include <linux/io.h> 15 #include <linux/mfd/syscon.h> 16 #include <linux/module.h> 17 #include <linux/of.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> 26 #include "pruss.h" 27 28 /** 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 32 */ 33 struct pruss_private_data { 34 bool has_no_sharedram; 35 bool has_core_mux_clock; 36 }; 37 38 /** 39 * pruss_get() - get the pruss for a given PRU remoteproc 40 * @rproc: remoteproc handle of a PRU instance 41 * 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 45 * anymore. 46 * 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. 50 * 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 55 */ 56 struct pruss *pruss_get(struct rproc *rproc) 57 { 58 struct pruss *pruss; 59 struct device *dev; 60 struct platform_device *ppdev; 61 62 if (IS_ERR_OR_NULL(rproc)) 63 return ERR_PTR(-EINVAL); 64 65 dev = &rproc->dev; 66 67 /* make sure it is PRU rproc */ 68 if (!dev->parent || !is_pru_rproc(dev->parent)) 69 return ERR_PTR(-ENODEV); 70 71 ppdev = to_platform_device(dev->parent->parent); 72 pruss = platform_get_drvdata(ppdev); 73 if (!pruss) 74 return ERR_PTR(-ENODEV); 75 76 get_device(pruss->dev); 77 78 return pruss; 79 } 80 EXPORT_SYMBOL_GPL(pruss_get); 81 82 /** 83 * pruss_put() - decrement pruss device's usecount 84 * @pruss: pruss handle 85 * 86 * Complimentary function for pruss_get(). Needs to be called 87 * after the PRUSS is used, and only if the pruss_get() succeeds. 88 */ 89 void pruss_put(struct pruss *pruss) 90 { 91 if (IS_ERR_OR_NULL(pruss)) 92 return; 93 94 put_device(pruss->dev); 95 } 96 EXPORT_SYMBOL_GPL(pruss_put); 97 98 /** 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 103 * 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() 107 * API. 108 * 109 * Return: 0 if requested memory region is available (in such case pointer to 110 * memory region is returned via @region), an error otherwise 111 */ 112 int pruss_request_mem_region(struct pruss *pruss, enum pruss_mem mem_id, 113 struct pruss_mem_region *region) 114 { 115 if (!pruss || !region || mem_id >= PRUSS_MEM_MAX) 116 return -EINVAL; 117 118 mutex_lock(&pruss->lock); 119 120 if (pruss->mem_in_use[mem_id]) { 121 mutex_unlock(&pruss->lock); 122 return -EBUSY; 123 } 124 125 *region = pruss->mem_regions[mem_id]; 126 pruss->mem_in_use[mem_id] = region; 127 128 mutex_unlock(&pruss->lock); 129 130 return 0; 131 } 132 EXPORT_SYMBOL_GPL(pruss_request_mem_region); 133 134 /** 135 * pruss_release_mem_region() - release a memory resource 136 * @pruss: the pruss instance 137 * @region: the memory region to release 138 * 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. 142 * 143 * Return: 0 on success, an error code otherwise 144 */ 145 int pruss_release_mem_region(struct pruss *pruss, 146 struct pruss_mem_region *region) 147 { 148 int id; 149 150 if (!pruss || !region) 151 return -EINVAL; 152 153 mutex_lock(&pruss->lock); 154 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) 158 break; 159 } 160 161 if (id == PRUSS_MEM_MAX) { 162 mutex_unlock(&pruss->lock); 163 return -EINVAL; 164 } 165 166 pruss->mem_in_use[id] = NULL; 167 168 mutex_unlock(&pruss->lock); 169 170 return 0; 171 } 172 EXPORT_SYMBOL_GPL(pruss_release_mem_region); 173 174 /** 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 179 * 180 * Return: 0 on success, or an error code otherwise 181 */ 182 int pruss_cfg_get_gpmux(struct pruss *pruss, enum pruss_pru_id pru_id, u8 *mux) 183 { 184 int ret; 185 u32 val; 186 187 if (pru_id >= PRUSS_NUM_PRUS || !mux) 188 return -EINVAL; 189 190 ret = pruss_cfg_read(pruss, PRUSS_CFG_GPCFG(pru_id), &val); 191 if (!ret) 192 *mux = (u8)((val & PRUSS_GPCFG_PRU_MUX_SEL_MASK) >> 193 PRUSS_GPCFG_PRU_MUX_SEL_SHIFT); 194 return ret; 195 } 196 EXPORT_SYMBOL_GPL(pruss_cfg_get_gpmux); 197 198 /** 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 203 * 204 * Return: 0 on success, or an error code otherwise 205 */ 206 int pruss_cfg_set_gpmux(struct pruss *pruss, enum pruss_pru_id pru_id, u8 mux) 207 { 208 if (mux >= PRUSS_GP_MUX_SEL_MAX || 209 pru_id >= PRUSS_NUM_PRUS) 210 return -EINVAL; 211 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); 215 } 216 EXPORT_SYMBOL_GPL(pruss_cfg_set_gpmux); 217 218 /** 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 223 * 224 * Sets the GPI mode for a given PRU by programming the 225 * corresponding PRUSS_CFG_GPCFGx register 226 * 227 * Return: 0 on success, or an error code otherwise 228 */ 229 int pruss_cfg_gpimode(struct pruss *pruss, enum pruss_pru_id pru_id, 230 enum pruss_gpi_mode mode) 231 { 232 if (pru_id >= PRUSS_NUM_PRUS || mode >= PRUSS_GPI_MODE_MAX) 233 return -EINVAL; 234 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); 238 } 239 EXPORT_SYMBOL_GPL(pruss_cfg_gpimode); 240 241 /** 242 * pruss_cfg_miirt_enable() - Enable/disable MII RT Events 243 * @pruss: the pruss instance 244 * @enable: enable/disable 245 * 246 * Enable/disable the MII RT Events for the PRUSS. 247 * 248 * Return: 0 on success, or an error code otherwise 249 */ 250 int pruss_cfg_miirt_enable(struct pruss *pruss, bool enable) 251 { 252 u32 set = enable ? PRUSS_MII_RT_EVENT_EN : 0; 253 254 return pruss_cfg_update(pruss, PRUSS_CFG_MII_RT, 255 PRUSS_MII_RT_EVENT_EN, set); 256 } 257 EXPORT_SYMBOL_GPL(pruss_cfg_miirt_enable); 258 259 /** 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 264 * 265 * Return: 0 on success, or an error code otherwise 266 */ 267 int pruss_cfg_xfr_enable(struct pruss *pruss, enum pru_type pru_type, 268 bool enable) 269 { 270 u32 mask, set; 271 272 switch (pru_type) { 273 case PRU_TYPE_PRU: 274 mask = PRUSS_SPP_XFER_SHIFT_EN; 275 break; 276 case PRU_TYPE_RTU: 277 mask = PRUSS_SPP_RTU_XFR_SHIFT_EN; 278 break; 279 default: 280 return -EINVAL; 281 } 282 283 set = enable ? mask : 0; 284 285 return pruss_cfg_update(pruss, PRUSS_CFG_SPP, mask, set); 286 } 287 EXPORT_SYMBOL_GPL(pruss_cfg_xfr_enable); 288 289 static void pruss_of_free_clk_provider(void *data) 290 { 291 struct device_node *clk_mux_np = data; 292 293 of_clk_del_provider(clk_mux_np); 294 of_node_put(clk_mux_np); 295 } 296 297 static void pruss_clk_unregister_mux(void *data) 298 { 299 clk_unregister_mux(data); 300 } 301 302 static int pruss_clk_mux_setup(struct pruss *pruss, struct clk *clk_mux, 303 char *mux_name, struct device_node *clks_np) 304 { 305 struct device_node *clk_mux_np; 306 struct device *dev = pruss->dev; 307 char *clk_mux_name; 308 unsigned int num_parents; 309 const char **parent_names; 310 void __iomem *reg; 311 u32 reg_offset; 312 int ret; 313 314 clk_mux_np = of_get_child_by_name(clks_np, mux_name); 315 if (!clk_mux_np) { 316 dev_err(dev, "%pOF is missing its '%s' node\n", clks_np, 317 mux_name); 318 return -ENODEV; 319 } 320 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); 324 ret = -EINVAL; 325 goto put_clk_mux_np; 326 } 327 328 parent_names = devm_kcalloc(dev, sizeof(*parent_names), num_parents, 329 GFP_KERNEL); 330 if (!parent_names) { 331 ret = -ENOMEM; 332 goto put_clk_mux_np; 333 } 334 335 of_clk_parent_fill(clk_mux_np, parent_names, num_parents); 336 337 clk_mux_name = devm_kasprintf(dev, GFP_KERNEL, "%s.%pOFn", 338 dev_name(dev), clk_mux_np); 339 if (!clk_mux_name) { 340 ret = -ENOMEM; 341 goto put_clk_mux_np; 342 } 343 344 ret = of_property_read_u32(clk_mux_np, "reg", ®_offset); 345 if (ret) 346 goto put_clk_mux_np; 347 348 reg = pruss->cfg_base + reg_offset; 349 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); 354 goto put_clk_mux_np; 355 } 356 357 ret = devm_add_action_or_reset(dev, pruss_clk_unregister_mux, clk_mux); 358 if (ret) { 359 dev_err(dev, "failed to add clkmux unregister action %d", ret); 360 goto put_clk_mux_np; 361 } 362 363 ret = of_clk_add_provider(clk_mux_np, of_clk_src_simple_get, clk_mux); 364 if (ret) 365 goto put_clk_mux_np; 366 367 ret = devm_add_action_or_reset(dev, pruss_of_free_clk_provider, 368 clk_mux_np); 369 if (ret) 370 dev_err(dev, "failed to add clkmux free action %d", ret); 371 372 return ret; 373 374 put_clk_mux_np: 375 of_node_put(clk_mux_np); 376 return ret; 377 } 378 379 static int pruss_clk_init(struct pruss *pruss, struct device_node *cfg_node) 380 { 381 struct device *dev = pruss->dev; 382 struct device_node *clks_np __free(device_node) = 383 of_get_child_by_name(cfg_node, "clocks"); 384 const struct pruss_private_data *data = of_device_get_match_data(dev); 385 int ret; 386 387 if (!clks_np) 388 return dev_err_probe(dev, -ENODEV, 389 "%pOF is missing its 'clocks' node\n", 390 cfg_node); 391 392 if (data && data->has_core_mux_clock) { 393 ret = pruss_clk_mux_setup(pruss, pruss->core_clk_mux, 394 "coreclk-mux", clks_np); 395 if (ret) 396 return dev_err_probe(dev, ret, 397 "failed to setup coreclk-mux\n"); 398 } 399 400 ret = pruss_clk_mux_setup(pruss, pruss->iep_clk_mux, "iepclk-mux", 401 clks_np); 402 if (ret) 403 return dev_err_probe(dev, ret, "failed to setup iepclk-mux\n"); 404 405 return 0; 406 } 407 408 static int pruss_of_setup_memories(struct device *dev, struct pruss *pruss) 409 { 410 struct device_node *np = dev_of_node(dev); 411 struct device_node *child __free(device_node) = 412 of_get_child_by_name(np, "memories"); 413 const struct pruss_private_data *data = of_device_get_match_data(dev); 414 const char *mem_names[PRUSS_MEM_MAX] = { "dram0", "dram1", "shrdram2" }; 415 int i; 416 417 if (!child) 418 return dev_err_probe(dev, -ENODEV, 419 "%pOF is missing its 'memories' node\n", 420 child); 421 422 for (i = 0; i < PRUSS_MEM_MAX; i++) { 423 struct resource res; 424 int index; 425 426 /* 427 * On AM437x one of two PRUSS units don't contain Shared RAM, 428 * skip it 429 */ 430 if (data && data->has_no_sharedram && i == PRUSS_MEM_SHRD_RAM2) 431 continue; 432 433 index = of_property_match_string(child, "reg-names", 434 mem_names[i]); 435 if (index < 0) 436 return index; 437 438 if (of_address_to_resource(child, index, &res)) 439 return -EINVAL; 440 441 pruss->mem_regions[i].va = devm_ioremap(dev, res.start, 442 resource_size(&res)); 443 if (!pruss->mem_regions[i].va) 444 return dev_err_probe(dev, -ENOMEM, 445 "failed to parse and map memory resource %d %s\n", 446 i, mem_names[i]); 447 pruss->mem_regions[i].pa = res.start; 448 pruss->mem_regions[i].size = resource_size(&res); 449 450 dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %p\n", 451 mem_names[i], &pruss->mem_regions[i].pa, 452 pruss->mem_regions[i].size, pruss->mem_regions[i].va); 453 } 454 455 return 0; 456 } 457 458 static struct regmap_config regmap_conf = { 459 .reg_bits = 32, 460 .val_bits = 32, 461 .reg_stride = 4, 462 }; 463 464 static int pruss_cfg_of_init(struct device *dev, struct pruss *pruss) 465 { 466 struct device_node *np = dev_of_node(dev); 467 struct device_node *child __free(device_node) = 468 of_get_child_by_name(np, "cfg"); 469 struct resource res; 470 int ret; 471 472 if (!child) 473 return dev_err_probe(dev, -ENODEV, 474 "%pOF is missing its 'cfg' node\n", child); 475 476 if (of_address_to_resource(child, 0, &res)) 477 return -ENOMEM; 478 479 pruss->cfg_base = devm_ioremap(dev, res.start, resource_size(&res)); 480 if (!pruss->cfg_base) 481 return -ENOMEM; 482 483 regmap_conf.name = kasprintf(GFP_KERNEL, "%pOFn@%llx", child, 484 (u64)res.start); 485 regmap_conf.max_register = resource_size(&res) - 4; 486 487 pruss->cfg_regmap = devm_regmap_init_mmio(dev, pruss->cfg_base, 488 ®map_conf); 489 kfree(regmap_conf.name); 490 if (IS_ERR(pruss->cfg_regmap)) 491 return dev_err_probe(dev, PTR_ERR(pruss->cfg_regmap), 492 "regmap_init_mmio failed for cfg\n"); 493 494 ret = pruss_clk_init(pruss, child); 495 if (ret) 496 return dev_err_probe(dev, ret, "pruss_clk_init failed\n"); 497 498 return 0; 499 } 500 501 static int pruss_probe(struct platform_device *pdev) 502 { 503 struct device *dev = &pdev->dev; 504 struct pruss *pruss; 505 int ret; 506 507 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32)); 508 if (ret) { 509 dev_err(dev, "failed to set the DMA coherent mask"); 510 return ret; 511 } 512 513 pruss = devm_kzalloc(dev, sizeof(*pruss), GFP_KERNEL); 514 if (!pruss) 515 return -ENOMEM; 516 517 pruss->dev = dev; 518 mutex_init(&pruss->lock); 519 520 ret = pruss_of_setup_memories(dev, pruss); 521 if (ret < 0) 522 return ret; 523 524 platform_set_drvdata(pdev, pruss); 525 526 pm_runtime_enable(dev); 527 ret = pm_runtime_resume_and_get(dev); 528 if (ret < 0) { 529 dev_err(dev, "couldn't enable module\n"); 530 goto rpm_disable; 531 } 532 533 ret = pruss_cfg_of_init(dev, pruss); 534 if (ret < 0) 535 goto rpm_put; 536 537 ret = devm_of_platform_populate(dev); 538 if (ret) { 539 dev_err(dev, "failed to register child devices\n"); 540 goto rpm_put; 541 } 542 543 return 0; 544 545 rpm_put: 546 pm_runtime_put_sync(dev); 547 rpm_disable: 548 pm_runtime_disable(dev); 549 return ret; 550 } 551 552 static void pruss_remove(struct platform_device *pdev) 553 { 554 struct device *dev = &pdev->dev; 555 556 devm_of_platform_depopulate(dev); 557 558 pm_runtime_put_sync(dev); 559 pm_runtime_disable(dev); 560 } 561 562 /* instance-specific driver private data */ 563 static const struct pruss_private_data am437x_pruss1_data = { 564 .has_no_sharedram = false, 565 }; 566 567 static const struct pruss_private_data am437x_pruss0_data = { 568 .has_no_sharedram = true, 569 }; 570 571 static const struct pruss_private_data am65x_j721e_pruss_data = { 572 .has_core_mux_clock = true, 573 }; 574 575 static const struct of_device_id pruss_of_match[] = { 576 { .compatible = "ti,am3356-pruss" }, 577 { .compatible = "ti,am4376-pruss0", .data = &am437x_pruss0_data, }, 578 { .compatible = "ti,am4376-pruss1", .data = &am437x_pruss1_data, }, 579 { .compatible = "ti,am5728-pruss" }, 580 { .compatible = "ti,k2g-pruss" }, 581 { .compatible = "ti,am654-icssg", .data = &am65x_j721e_pruss_data, }, 582 { .compatible = "ti,j721e-icssg", .data = &am65x_j721e_pruss_data, }, 583 { .compatible = "ti,am642-icssg", .data = &am65x_j721e_pruss_data, }, 584 { .compatible = "ti,am625-pruss", .data = &am65x_j721e_pruss_data, }, 585 {}, 586 }; 587 MODULE_DEVICE_TABLE(of, pruss_of_match); 588 589 static struct platform_driver pruss_driver = { 590 .driver = { 591 .name = "pruss", 592 .of_match_table = pruss_of_match, 593 }, 594 .probe = pruss_probe, 595 .remove = pruss_remove, 596 }; 597 module_platform_driver(pruss_driver); 598 599 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>"); 600 MODULE_DESCRIPTION("PRU-ICSS Subsystem Driver"); 601 MODULE_LICENSE("GPL v2"); 602