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