1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved. 4 * 5 * Description: CoreSight Replicator driver 6 */ 7 8 #include <linux/acpi.h> 9 #include <linux/amba/bus.h> 10 #include <linux/kernel.h> 11 #include <linux/device.h> 12 #include <linux/platform_device.h> 13 #include <linux/io.h> 14 #include <linux/err.h> 15 #include <linux/slab.h> 16 #include <linux/pm_runtime.h> 17 #include <linux/property.h> 18 #include <linux/clk.h> 19 #include <linux/of.h> 20 #include <linux/coresight.h> 21 22 #include "coresight-priv.h" 23 24 #define REPLICATOR_IDFILTER0 0x000 25 #define REPLICATOR_IDFILTER1 0x004 26 27 DEFINE_CORESIGHT_DEVLIST(replicator_devs, "replicator"); 28 29 /** 30 * struct replicator_drvdata - specifics associated to a replicator component 31 * @base: memory mapped base address for this component. Also indicates 32 * whether this one is programmable or not. 33 * @atclk: optional clock for the core parts of the replicator. 34 * @pclk: APB clock if present, otherwise NULL 35 * @csdev: component vitals needed by the framework 36 * @spinlock: serialize enable/disable operations. 37 * @check_idfilter_val: check if the context is lost upon clock removal. 38 */ 39 struct replicator_drvdata { 40 void __iomem *base; 41 struct clk *atclk; 42 struct clk *pclk; 43 struct coresight_device *csdev; 44 raw_spinlock_t spinlock; 45 bool check_idfilter_val; 46 }; 47 48 static void dynamic_replicator_reset(struct replicator_drvdata *drvdata) 49 { 50 struct coresight_device *csdev = drvdata->csdev; 51 52 CS_UNLOCK(drvdata->base); 53 54 if (!coresight_claim_device_unlocked(csdev)) { 55 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0); 56 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1); 57 coresight_disclaim_device_unlocked(csdev); 58 } 59 60 CS_LOCK(drvdata->base); 61 } 62 63 /* 64 * replicator_reset : Reset the replicator configuration to sane values. 65 */ 66 static void replicator_reset(struct replicator_drvdata *drvdata) 67 { 68 if (drvdata->base) 69 dynamic_replicator_reset(drvdata); 70 } 71 72 static int dynamic_replicator_enable(struct replicator_drvdata *drvdata, 73 int inport, int outport) 74 { 75 int rc = 0; 76 u32 id0val, id1val; 77 struct coresight_device *csdev = drvdata->csdev; 78 79 CS_UNLOCK(drvdata->base); 80 81 id0val = readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0); 82 id1val = readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1); 83 84 /* 85 * Some replicator designs lose context when AMBA clocks are removed, 86 * so have a check for this. 87 */ 88 if (drvdata->check_idfilter_val && id0val == 0x0 && id1val == 0x0) 89 id0val = id1val = 0xff; 90 91 if (id0val == 0xff && id1val == 0xff) 92 rc = coresight_claim_device_unlocked(csdev); 93 94 if (!rc) { 95 switch (outport) { 96 case 0: 97 id0val = 0x0; 98 break; 99 case 1: 100 id1val = 0x0; 101 break; 102 default: 103 WARN_ON(1); 104 rc = -EINVAL; 105 } 106 } 107 108 /* Ensure that the outport is enabled. */ 109 if (!rc) { 110 writel_relaxed(id0val, drvdata->base + REPLICATOR_IDFILTER0); 111 writel_relaxed(id1val, drvdata->base + REPLICATOR_IDFILTER1); 112 } 113 114 CS_LOCK(drvdata->base); 115 116 return rc; 117 } 118 119 static int replicator_enable(struct coresight_device *csdev, 120 struct coresight_connection *in, 121 struct coresight_connection *out) 122 { 123 int rc = 0; 124 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); 125 unsigned long flags; 126 bool first_enable = false; 127 128 raw_spin_lock_irqsave(&drvdata->spinlock, flags); 129 if (out->src_refcnt == 0) { 130 if (drvdata->base) 131 rc = dynamic_replicator_enable(drvdata, in->dest_port, 132 out->src_port); 133 if (!rc) 134 first_enable = true; 135 } 136 if (!rc) 137 out->src_refcnt++; 138 raw_spin_unlock_irqrestore(&drvdata->spinlock, flags); 139 140 if (first_enable) 141 dev_dbg(&csdev->dev, "REPLICATOR enabled\n"); 142 return rc; 143 } 144 145 static void dynamic_replicator_disable(struct replicator_drvdata *drvdata, 146 int inport, int outport) 147 { 148 u32 reg; 149 struct coresight_device *csdev = drvdata->csdev; 150 151 switch (outport) { 152 case 0: 153 reg = REPLICATOR_IDFILTER0; 154 break; 155 case 1: 156 reg = REPLICATOR_IDFILTER1; 157 break; 158 default: 159 WARN_ON(1); 160 return; 161 } 162 163 CS_UNLOCK(drvdata->base); 164 165 /* disable the flow of ATB data through port */ 166 writel_relaxed(0xff, drvdata->base + reg); 167 168 if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) && 169 (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff)) 170 coresight_disclaim_device_unlocked(csdev); 171 CS_LOCK(drvdata->base); 172 } 173 174 static void replicator_disable(struct coresight_device *csdev, 175 struct coresight_connection *in, 176 struct coresight_connection *out) 177 { 178 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); 179 unsigned long flags; 180 bool last_disable = false; 181 182 raw_spin_lock_irqsave(&drvdata->spinlock, flags); 183 if (--out->src_refcnt == 0) { 184 if (drvdata->base) 185 dynamic_replicator_disable(drvdata, in->dest_port, 186 out->src_port); 187 last_disable = true; 188 } 189 raw_spin_unlock_irqrestore(&drvdata->spinlock, flags); 190 191 if (last_disable) 192 dev_dbg(&csdev->dev, "REPLICATOR disabled\n"); 193 } 194 195 static const struct coresight_ops_link replicator_link_ops = { 196 .enable = replicator_enable, 197 .disable = replicator_disable, 198 }; 199 200 static const struct coresight_ops replicator_cs_ops = { 201 .link_ops = &replicator_link_ops, 202 }; 203 204 static struct attribute *replicator_mgmt_attrs[] = { 205 coresight_simple_reg32(idfilter0, REPLICATOR_IDFILTER0), 206 coresight_simple_reg32(idfilter1, REPLICATOR_IDFILTER1), 207 NULL, 208 }; 209 210 static const struct attribute_group replicator_mgmt_group = { 211 .attrs = replicator_mgmt_attrs, 212 .name = "mgmt", 213 }; 214 215 static const struct attribute_group *replicator_groups[] = { 216 &replicator_mgmt_group, 217 NULL, 218 }; 219 220 static int replicator_probe(struct device *dev, struct resource *res) 221 { 222 struct coresight_platform_data *pdata = NULL; 223 struct replicator_drvdata *drvdata; 224 struct coresight_desc desc = { 0 }; 225 void __iomem *base; 226 int ret; 227 228 if (is_of_node(dev_fwnode(dev)) && 229 of_device_is_compatible(dev->of_node, "arm,coresight-replicator")) 230 dev_warn_once(dev, 231 "Uses OBSOLETE CoreSight replicator binding\n"); 232 233 desc.name = coresight_alloc_device_name(&replicator_devs, dev); 234 if (!desc.name) 235 return -ENOMEM; 236 237 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); 238 if (!drvdata) 239 return -ENOMEM; 240 241 ret = coresight_get_enable_clocks(dev, &drvdata->pclk, &drvdata->atclk); 242 if (ret) 243 return ret; 244 245 /* 246 * Map the device base for dynamic-replicator, which has been 247 * validated by AMBA core 248 */ 249 if (res) { 250 base = devm_ioremap_resource(dev, res); 251 if (IS_ERR(base)) 252 return PTR_ERR(base); 253 drvdata->base = base; 254 desc.groups = replicator_groups; 255 desc.access = CSDEV_ACCESS_IOMEM(base); 256 coresight_clear_self_claim_tag(&desc.access); 257 } 258 259 if (fwnode_property_present(dev_fwnode(dev), 260 "qcom,replicator-loses-context")) 261 drvdata->check_idfilter_val = true; 262 263 dev_set_drvdata(dev, drvdata); 264 265 pdata = coresight_get_platform_data(dev); 266 if (IS_ERR(pdata)) 267 return PTR_ERR(pdata); 268 dev->platform_data = pdata; 269 270 raw_spin_lock_init(&drvdata->spinlock); 271 desc.type = CORESIGHT_DEV_TYPE_LINK; 272 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT; 273 desc.ops = &replicator_cs_ops; 274 desc.pdata = dev->platform_data; 275 desc.dev = dev; 276 277 drvdata->csdev = coresight_register(&desc); 278 if (IS_ERR(drvdata->csdev)) 279 return PTR_ERR(drvdata->csdev); 280 281 replicator_reset(drvdata); 282 return 0; 283 } 284 285 static int replicator_remove(struct device *dev) 286 { 287 struct replicator_drvdata *drvdata = dev_get_drvdata(dev); 288 289 coresight_unregister(drvdata->csdev); 290 return 0; 291 } 292 293 static int replicator_platform_probe(struct platform_device *pdev) 294 { 295 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 296 int ret; 297 298 pm_runtime_get_noresume(&pdev->dev); 299 pm_runtime_set_active(&pdev->dev); 300 pm_runtime_enable(&pdev->dev); 301 302 ret = replicator_probe(&pdev->dev, res); 303 pm_runtime_put(&pdev->dev); 304 if (ret) 305 pm_runtime_disable(&pdev->dev); 306 307 return ret; 308 } 309 310 static void replicator_platform_remove(struct platform_device *pdev) 311 { 312 struct replicator_drvdata *drvdata = dev_get_drvdata(&pdev->dev); 313 314 if (WARN_ON(!drvdata)) 315 return; 316 317 replicator_remove(&pdev->dev); 318 pm_runtime_disable(&pdev->dev); 319 } 320 321 #ifdef CONFIG_PM 322 static int replicator_runtime_suspend(struct device *dev) 323 { 324 struct replicator_drvdata *drvdata = dev_get_drvdata(dev); 325 326 clk_disable_unprepare(drvdata->atclk); 327 clk_disable_unprepare(drvdata->pclk); 328 329 return 0; 330 } 331 332 static int replicator_runtime_resume(struct device *dev) 333 { 334 struct replicator_drvdata *drvdata = dev_get_drvdata(dev); 335 int ret; 336 337 ret = clk_prepare_enable(drvdata->pclk); 338 if (ret) 339 return ret; 340 341 ret = clk_prepare_enable(drvdata->atclk); 342 if (ret) 343 clk_disable_unprepare(drvdata->pclk); 344 345 return ret; 346 } 347 #endif 348 349 static const struct dev_pm_ops replicator_dev_pm_ops = { 350 SET_RUNTIME_PM_OPS(replicator_runtime_suspend, 351 replicator_runtime_resume, NULL) 352 }; 353 354 static const struct of_device_id replicator_match[] = { 355 {.compatible = "arm,coresight-replicator"}, 356 {.compatible = "arm,coresight-static-replicator"}, 357 {} 358 }; 359 360 MODULE_DEVICE_TABLE(of, replicator_match); 361 362 #ifdef CONFIG_ACPI 363 static const struct acpi_device_id replicator_acpi_ids[] = { 364 {"ARMHC985", 0, 0, 0}, /* ARM CoreSight Static Replicator */ 365 {"ARMHC98D", 0, 0, 0}, /* ARM CoreSight Dynamic Replicator */ 366 {} 367 }; 368 369 MODULE_DEVICE_TABLE(acpi, replicator_acpi_ids); 370 #endif 371 372 static struct platform_driver replicator_driver = { 373 .probe = replicator_platform_probe, 374 .remove = replicator_platform_remove, 375 .driver = { 376 .name = "coresight-replicator", 377 /* THIS_MODULE is taken care of by platform_driver_register() */ 378 .of_match_table = of_match_ptr(replicator_match), 379 .acpi_match_table = ACPI_PTR(replicator_acpi_ids), 380 .pm = &replicator_dev_pm_ops, 381 .suppress_bind_attrs = true, 382 }, 383 }; 384 385 static int dynamic_replicator_probe(struct amba_device *adev, 386 const struct amba_id *id) 387 { 388 int ret; 389 390 ret = replicator_probe(&adev->dev, &adev->res); 391 if (!ret) 392 pm_runtime_put(&adev->dev); 393 394 return ret; 395 } 396 397 static void dynamic_replicator_remove(struct amba_device *adev) 398 { 399 replicator_remove(&adev->dev); 400 } 401 402 static const struct amba_id dynamic_replicator_ids[] = { 403 CS_AMBA_ID(0x000bb909), 404 CS_AMBA_ID(0x000bb9ec), /* Coresight SoC-600 */ 405 {}, 406 }; 407 408 MODULE_DEVICE_TABLE(amba, dynamic_replicator_ids); 409 410 static struct amba_driver dynamic_replicator_driver = { 411 .drv = { 412 .name = "coresight-dynamic-replicator", 413 .pm = &replicator_dev_pm_ops, 414 .suppress_bind_attrs = true, 415 }, 416 .probe = dynamic_replicator_probe, 417 .remove = dynamic_replicator_remove, 418 .id_table = dynamic_replicator_ids, 419 }; 420 421 static int __init replicator_init(void) 422 { 423 return coresight_init_driver("replicator", &dynamic_replicator_driver, &replicator_driver, 424 THIS_MODULE); 425 } 426 427 static void __exit replicator_exit(void) 428 { 429 coresight_remove_driver(&dynamic_replicator_driver, &replicator_driver); 430 } 431 432 module_init(replicator_init); 433 module_exit(replicator_exit); 434 435 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>"); 436 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>"); 437 MODULE_DESCRIPTION("Arm CoreSight Replicator Driver"); 438 MODULE_LICENSE("GPL v2"); 439