1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved. 4 * 5 * Description: CoreSight Trace Port Interface Unit driver 6 */ 7 8 #include <linux/acpi.h> 9 #include <linux/amba/bus.h> 10 #include <linux/atomic.h> 11 #include <linux/clk.h> 12 #include <linux/coresight.h> 13 #include <linux/device.h> 14 #include <linux/err.h> 15 #include <linux/init.h> 16 #include <linux/io.h> 17 #include <linux/kernel.h> 18 #include <linux/platform_device.h> 19 #include <linux/pm_runtime.h> 20 #include <linux/slab.h> 21 22 #include "coresight-priv.h" 23 24 #define TPIU_SUPP_PORTSZ 0x000 25 #define TPIU_CURR_PORTSZ 0x004 26 #define TPIU_SUPP_TRIGMODES 0x100 27 #define TPIU_TRIG_CNTRVAL 0x104 28 #define TPIU_TRIG_MULT 0x108 29 #define TPIU_SUPP_TESTPATM 0x200 30 #define TPIU_CURR_TESTPATM 0x204 31 #define TPIU_TEST_PATREPCNTR 0x208 32 #define TPIU_FFSR 0x300 33 #define TPIU_FFCR 0x304 34 #define TPIU_FSYNC_CNTR 0x308 35 #define TPIU_EXTCTL_INPORT 0x400 36 #define TPIU_EXTCTL_OUTPORT 0x404 37 #define TPIU_ITTRFLINACK 0xee4 38 #define TPIU_ITTRFLIN 0xee8 39 #define TPIU_ITATBDATA0 0xeec 40 #define TPIU_ITATBCTR2 0xef0 41 #define TPIU_ITATBCTR1 0xef4 42 #define TPIU_ITATBCTR0 0xef8 43 44 /** register definition **/ 45 /* FFSR - 0x300 */ 46 #define FFSR_FT_STOPPED_BIT 1 47 /* FFCR - 0x304 */ 48 #define FFCR_FON_MAN_BIT 6 49 #define FFCR_FON_MAN BIT(6) 50 #define FFCR_STOP_FI BIT(12) 51 52 /* 53 * @base: memory mapped base address for this component. 54 * @atclk: optional clock for the core parts of the TPIU. 55 * @pclk: APB clock if present, otherwise NULL 56 * @csdev: component vitals needed by the framework. 57 */ 58 struct tpiu_drvdata { 59 void __iomem *base; 60 struct clk *atclk; 61 struct clk *pclk; 62 struct coresight_device *csdev; 63 spinlock_t spinlock; 64 }; 65 66 static void tpiu_enable_hw(struct csdev_access *csa) 67 { 68 CS_UNLOCK(csa->base); 69 70 /* TODO: fill this up */ 71 72 CS_LOCK(csa->base); 73 } 74 75 static int tpiu_enable(struct coresight_device *csdev, enum cs_mode mode, 76 struct coresight_path *path) 77 { 78 struct tpiu_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); 79 80 guard(spinlock)(&drvdata->spinlock); 81 tpiu_enable_hw(&csdev->access); 82 csdev->refcnt++; 83 dev_dbg(&csdev->dev, "TPIU enabled\n"); 84 return 0; 85 } 86 87 static void tpiu_disable_hw(struct csdev_access *csa) 88 { 89 CS_UNLOCK(csa->base); 90 91 /* Clear formatter and stop on flush */ 92 csdev_access_relaxed_write32(csa, FFCR_STOP_FI, TPIU_FFCR); 93 /* Generate manual flush */ 94 csdev_access_relaxed_write32(csa, FFCR_STOP_FI | FFCR_FON_MAN, TPIU_FFCR); 95 /* Wait for flush to complete */ 96 coresight_timeout(csa, TPIU_FFCR, FFCR_FON_MAN_BIT, 0); 97 /* Wait for formatter to stop */ 98 coresight_timeout(csa, TPIU_FFSR, FFSR_FT_STOPPED_BIT, 1); 99 100 CS_LOCK(csa->base); 101 } 102 103 static int tpiu_disable(struct coresight_device *csdev) 104 { 105 struct tpiu_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); 106 107 guard(spinlock)(&drvdata->spinlock); 108 csdev->refcnt--; 109 if (csdev->refcnt) 110 return -EBUSY; 111 112 tpiu_disable_hw(&csdev->access); 113 114 dev_dbg(&csdev->dev, "TPIU disabled\n"); 115 return 0; 116 } 117 118 static const struct coresight_ops_sink tpiu_sink_ops = { 119 .enable = tpiu_enable, 120 .disable = tpiu_disable, 121 }; 122 123 static const struct coresight_ops tpiu_cs_ops = { 124 .sink_ops = &tpiu_sink_ops, 125 }; 126 127 static int __tpiu_probe(struct device *dev, struct resource *res) 128 { 129 void __iomem *base; 130 struct coresight_platform_data *pdata = NULL; 131 struct tpiu_drvdata *drvdata; 132 struct coresight_desc desc = { 0 }; 133 int ret; 134 135 desc.name = coresight_alloc_device_name("tpiu", dev); 136 if (!desc.name) 137 return -ENOMEM; 138 139 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); 140 if (!drvdata) 141 return -ENOMEM; 142 143 spin_lock_init(&drvdata->spinlock); 144 145 ret = coresight_get_enable_clocks(dev, &drvdata->pclk, &drvdata->atclk); 146 if (ret) 147 return ret; 148 149 dev_set_drvdata(dev, drvdata); 150 151 /* Validity for the resource is already checked by the AMBA core */ 152 base = devm_ioremap_resource(dev, res); 153 if (IS_ERR(base)) 154 return PTR_ERR(base); 155 156 drvdata->base = base; 157 desc.access = CSDEV_ACCESS_IOMEM(base); 158 159 /* Disable tpiu to support older devices */ 160 tpiu_disable_hw(&desc.access); 161 162 pdata = coresight_get_platform_data(dev); 163 if (IS_ERR(pdata)) 164 return PTR_ERR(pdata); 165 dev->platform_data = pdata; 166 167 desc.type = CORESIGHT_DEV_TYPE_SINK; 168 desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_PORT; 169 desc.ops = &tpiu_cs_ops; 170 desc.pdata = pdata; 171 desc.dev = dev; 172 drvdata->csdev = coresight_register(&desc); 173 174 if (!IS_ERR(drvdata->csdev)) 175 return 0; 176 177 return PTR_ERR(drvdata->csdev); 178 } 179 180 static int tpiu_probe(struct amba_device *adev, const struct amba_id *id) 181 { 182 int ret; 183 184 ret = __tpiu_probe(&adev->dev, &adev->res); 185 if (!ret) 186 pm_runtime_put(&adev->dev); 187 return ret; 188 } 189 190 static void __tpiu_remove(struct device *dev) 191 { 192 struct tpiu_drvdata *drvdata = dev_get_drvdata(dev); 193 194 coresight_unregister(drvdata->csdev); 195 } 196 197 static void tpiu_remove(struct amba_device *adev) 198 { 199 __tpiu_remove(&adev->dev); 200 } 201 202 #ifdef CONFIG_PM 203 static int tpiu_runtime_suspend(struct device *dev) 204 { 205 struct tpiu_drvdata *drvdata = dev_get_drvdata(dev); 206 207 clk_disable_unprepare(drvdata->atclk); 208 clk_disable_unprepare(drvdata->pclk); 209 210 return 0; 211 } 212 213 static int tpiu_runtime_resume(struct device *dev) 214 { 215 struct tpiu_drvdata *drvdata = dev_get_drvdata(dev); 216 int ret; 217 218 ret = clk_prepare_enable(drvdata->pclk); 219 if (ret) 220 return ret; 221 222 ret = clk_prepare_enable(drvdata->atclk); 223 if (ret) 224 clk_disable_unprepare(drvdata->pclk); 225 226 return ret; 227 } 228 #endif 229 230 static const struct dev_pm_ops tpiu_dev_pm_ops = { 231 SET_RUNTIME_PM_OPS(tpiu_runtime_suspend, tpiu_runtime_resume, NULL) 232 }; 233 234 static const struct amba_id tpiu_ids[] = { 235 { 236 .id = 0x000bb912, 237 .mask = 0x000fffff, 238 }, 239 { 240 .id = 0x0004b912, 241 .mask = 0x0007ffff, 242 }, 243 { 244 /* Coresight SoC-600 */ 245 .id = 0x000bb9e7, 246 .mask = 0x000fffff, 247 }, 248 { 0, 0, NULL }, 249 }; 250 251 MODULE_DEVICE_TABLE(amba, tpiu_ids); 252 253 static struct amba_driver tpiu_driver = { 254 .drv = { 255 .name = "coresight-tpiu", 256 .pm = &tpiu_dev_pm_ops, 257 .suppress_bind_attrs = true, 258 }, 259 .probe = tpiu_probe, 260 .remove = tpiu_remove, 261 .id_table = tpiu_ids, 262 }; 263 264 static int tpiu_platform_probe(struct platform_device *pdev) 265 { 266 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 267 int ret; 268 269 pm_runtime_get_noresume(&pdev->dev); 270 pm_runtime_set_active(&pdev->dev); 271 pm_runtime_enable(&pdev->dev); 272 273 ret = __tpiu_probe(&pdev->dev, res); 274 pm_runtime_put(&pdev->dev); 275 if (ret) 276 pm_runtime_disable(&pdev->dev); 277 278 return ret; 279 } 280 281 static void tpiu_platform_remove(struct platform_device *pdev) 282 { 283 struct tpiu_drvdata *drvdata = dev_get_drvdata(&pdev->dev); 284 285 if (WARN_ON(!drvdata)) 286 return; 287 288 __tpiu_remove(&pdev->dev); 289 pm_runtime_disable(&pdev->dev); 290 } 291 292 #ifdef CONFIG_ACPI 293 static const struct acpi_device_id tpiu_acpi_ids[] = { 294 {"ARMHC979", 0, 0, 0}, /* ARM CoreSight TPIU */ 295 {} 296 }; 297 MODULE_DEVICE_TABLE(acpi, tpiu_acpi_ids); 298 #endif 299 300 static struct platform_driver tpiu_platform_driver = { 301 .probe = tpiu_platform_probe, 302 .remove = tpiu_platform_remove, 303 .driver = { 304 .name = "coresight-tpiu-platform", 305 .acpi_match_table = ACPI_PTR(tpiu_acpi_ids), 306 .suppress_bind_attrs = true, 307 .pm = &tpiu_dev_pm_ops, 308 }, 309 }; 310 311 static int __init tpiu_init(void) 312 { 313 return coresight_init_driver("tpiu", &tpiu_driver, &tpiu_platform_driver); 314 } 315 316 static void __exit tpiu_exit(void) 317 { 318 coresight_remove_driver(&tpiu_driver, &tpiu_platform_driver); 319 } 320 module_init(tpiu_init); 321 module_exit(tpiu_exit); 322 323 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>"); 324 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>"); 325 MODULE_DESCRIPTION("Arm CoreSight TPIU (Trace Port Interface Unit) driver"); 326 MODULE_LICENSE("GPL v2"); 327