Pratik Patel | d944cc7 | 2013-02-08 11:52:12 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/mutex.h> |
| 22 | #include <linux/clk.h> |
| 23 | #include <linux/of_coresight.h> |
| 24 | #include <linux/coresight.h> |
| 25 | #include <linux/coresight-cti.h> |
| 26 | |
| 27 | #include "coresight-priv.h" |
| 28 | |
| 29 | #define cti_writel(drvdata, val, off) __raw_writel((val), drvdata->base + off) |
| 30 | #define cti_readl(drvdata, off) __raw_readl(drvdata->base + off) |
| 31 | |
| 32 | #define CTI_LOCK(drvdata) \ |
| 33 | do { \ |
| 34 | mb(); \ |
| 35 | cti_writel(drvdata, 0x0, CORESIGHT_LAR); \ |
| 36 | } while (0) |
| 37 | #define CTI_UNLOCK(drvdata) \ |
| 38 | do { \ |
| 39 | cti_writel(drvdata, CORESIGHT_UNLOCK, CORESIGHT_LAR); \ |
| 40 | mb(); \ |
| 41 | } while (0) |
| 42 | |
| 43 | #define CTICONTROL (0x000) |
| 44 | #define CTIINTACK (0x010) |
| 45 | #define CTIAPPSET (0x014) |
| 46 | #define CTIAPPCLEAR (0x018) |
| 47 | #define CTIAPPPULSE (0x01C) |
| 48 | #define CTIINEN(n) (0x020 + (n * 4)) |
| 49 | #define CTIOUTEN(n) (0x0A0 + (n * 4)) |
| 50 | #define CTITRIGINSTATUS (0x130) |
| 51 | #define CTITRIGOUTSTATUS (0x134) |
| 52 | #define CTICHINSTATUS (0x138) |
| 53 | #define CTICHOUTSTATUS (0x13C) |
| 54 | #define CTIGATE (0x140) |
| 55 | #define ASICCTL (0x144) |
| 56 | #define ITCHINACK (0xEDC) |
| 57 | #define ITTRIGINACK (0xEE0) |
| 58 | #define ITCHOUT (0xEE4) |
| 59 | #define ITTRIGOUT (0xEE8) |
| 60 | #define ITCHOUTACK (0xEEC) |
| 61 | #define ITTRIGOUTACK (0xEF0) |
| 62 | #define ITCHIN (0xEF4) |
| 63 | #define ITTRIGIN (0xEF8) |
| 64 | |
| 65 | #define CTI_MAX_TRIGGERS (8) |
| 66 | #define CTI_MAX_CHANNELS (4) |
| 67 | |
| 68 | #define to_cti_drvdata(c) container_of(c, struct cti_drvdata, cti) |
| 69 | |
| 70 | struct cti_drvdata { |
| 71 | void __iomem *base; |
| 72 | struct device *dev; |
| 73 | struct coresight_device *csdev; |
| 74 | struct clk *clk; |
| 75 | struct mutex mutex; |
| 76 | struct coresight_cti cti; |
| 77 | int refcnt; |
| 78 | }; |
| 79 | |
| 80 | static LIST_HEAD(cti_list); |
| 81 | static DEFINE_MUTEX(cti_lock); |
| 82 | |
| 83 | static int cti_verify_bounds(int trig, int ch) |
| 84 | { |
| 85 | if (trig >= CTI_MAX_TRIGGERS) |
| 86 | return -EINVAL; |
| 87 | |
| 88 | if (ch >= CTI_MAX_CHANNELS) |
| 89 | return -EINVAL; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int cti_enable(struct cti_drvdata *drvdata) |
| 95 | { |
| 96 | int ret; |
| 97 | |
| 98 | ret = clk_prepare_enable(drvdata->clk); |
| 99 | if (ret) |
| 100 | return ret; |
| 101 | |
| 102 | CTI_UNLOCK(drvdata); |
| 103 | |
| 104 | cti_writel(drvdata, 0x1, CTICONTROL); |
| 105 | |
| 106 | CTI_LOCK(drvdata); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static void __cti_map_trigin(struct cti_drvdata *drvdata, int trig, int ch) |
| 111 | { |
| 112 | uint32_t ctien; |
| 113 | |
| 114 | CTI_UNLOCK(drvdata); |
| 115 | |
| 116 | ctien = cti_readl(drvdata, CTIINEN(trig)); |
| 117 | cti_writel(drvdata, (ctien | 0x1 << ch), CTIINEN(trig)); |
| 118 | |
| 119 | CTI_LOCK(drvdata); |
| 120 | } |
| 121 | |
| 122 | int coresight_cti_map_trigin(struct coresight_cti *cti, int trig, int ch) |
| 123 | { |
| 124 | struct cti_drvdata *drvdata; |
| 125 | int ret = 0; |
| 126 | |
| 127 | if (IS_ERR_OR_NULL(cti)) |
| 128 | return -EINVAL; |
| 129 | |
| 130 | ret = cti_verify_bounds(trig, ch); |
| 131 | if (ret) |
| 132 | return ret; |
| 133 | |
| 134 | drvdata = to_cti_drvdata(cti); |
| 135 | |
| 136 | mutex_lock(&drvdata->mutex); |
| 137 | if (drvdata->refcnt == 0) { |
| 138 | ret = cti_enable(drvdata); |
| 139 | if (ret) |
| 140 | goto err; |
| 141 | } |
| 142 | drvdata->refcnt++; |
| 143 | |
| 144 | __cti_map_trigin(drvdata, trig, ch); |
| 145 | err: |
| 146 | mutex_unlock(&drvdata->mutex); |
| 147 | return ret; |
| 148 | } |
| 149 | EXPORT_SYMBOL(coresight_cti_map_trigin); |
| 150 | |
| 151 | static void __cti_map_trigout(struct cti_drvdata *drvdata, int trig, int ch) |
| 152 | { |
| 153 | uint32_t ctien; |
| 154 | |
| 155 | CTI_UNLOCK(drvdata); |
| 156 | |
| 157 | ctien = cti_readl(drvdata, CTIOUTEN(trig)); |
| 158 | cti_writel(drvdata, (ctien | 0x1 << ch), CTIOUTEN(trig)); |
| 159 | |
| 160 | CTI_LOCK(drvdata); |
| 161 | } |
| 162 | |
| 163 | int coresight_cti_map_trigout(struct coresight_cti *cti, int trig, int ch) |
| 164 | { |
| 165 | struct cti_drvdata *drvdata; |
| 166 | int ret = 0; |
| 167 | |
| 168 | if (IS_ERR_OR_NULL(cti)) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | ret = cti_verify_bounds(trig, ch); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | |
| 175 | drvdata = to_cti_drvdata(cti); |
| 176 | |
| 177 | mutex_lock(&drvdata->mutex); |
| 178 | if (drvdata->refcnt == 0) { |
| 179 | ret = cti_enable(drvdata); |
| 180 | if (ret) |
| 181 | goto err; |
| 182 | } |
| 183 | drvdata->refcnt++; |
| 184 | |
| 185 | __cti_map_trigout(drvdata, trig, ch); |
| 186 | err: |
| 187 | mutex_unlock(&drvdata->mutex); |
| 188 | return ret; |
| 189 | } |
| 190 | EXPORT_SYMBOL(coresight_cti_map_trigout); |
| 191 | |
| 192 | static void cti_disable(struct cti_drvdata *drvdata) |
| 193 | { |
| 194 | CTI_UNLOCK(drvdata); |
| 195 | |
| 196 | cti_writel(drvdata, 0x1, CTICONTROL); |
| 197 | |
| 198 | CTI_LOCK(drvdata); |
| 199 | } |
| 200 | |
| 201 | static void __cti_unmap_trigin(struct cti_drvdata *drvdata, int trig, int ch) |
| 202 | { |
| 203 | uint32_t ctien; |
| 204 | |
| 205 | CTI_UNLOCK(drvdata); |
| 206 | |
| 207 | ctien = cti_readl(drvdata, CTIINEN(trig)); |
| 208 | cti_writel(drvdata, (ctien & ~(0x1 << ch)), CTIINEN(trig)); |
| 209 | |
| 210 | CTI_LOCK(drvdata); |
| 211 | } |
| 212 | |
| 213 | void coresight_cti_unmap_trigin(struct coresight_cti *cti, int trig, int ch) |
| 214 | { |
| 215 | struct cti_drvdata *drvdata; |
| 216 | |
| 217 | if (IS_ERR_OR_NULL(cti)) |
| 218 | return; |
| 219 | |
| 220 | if (cti_verify_bounds(trig, ch)) |
| 221 | return; |
| 222 | |
| 223 | drvdata = to_cti_drvdata(cti); |
| 224 | |
| 225 | mutex_lock(&drvdata->mutex); |
| 226 | __cti_unmap_trigin(drvdata, trig, ch); |
| 227 | |
| 228 | if (drvdata->refcnt == 1) |
| 229 | cti_disable(drvdata); |
| 230 | drvdata->refcnt--; |
| 231 | mutex_unlock(&drvdata->mutex); |
| 232 | |
| 233 | clk_disable_unprepare(drvdata->clk); |
| 234 | } |
| 235 | EXPORT_SYMBOL(coresight_cti_unmap_trigin); |
| 236 | |
| 237 | static void __cti_unmap_trigout(struct cti_drvdata *drvdata, int trig, int ch) |
| 238 | { |
| 239 | uint32_t ctien; |
| 240 | |
| 241 | CTI_UNLOCK(drvdata); |
| 242 | |
| 243 | ctien = cti_readl(drvdata, CTIOUTEN(trig)); |
| 244 | cti_writel(drvdata, (ctien & ~(0x1 << ch)), CTIOUTEN(trig)); |
| 245 | |
| 246 | CTI_LOCK(drvdata); |
| 247 | } |
| 248 | |
| 249 | void coresight_cti_unmap_trigout(struct coresight_cti *cti, int trig, int ch) |
| 250 | { |
| 251 | struct cti_drvdata *drvdata; |
| 252 | |
| 253 | if (IS_ERR_OR_NULL(cti)) |
| 254 | return; |
| 255 | |
| 256 | if (cti_verify_bounds(trig, ch)) |
| 257 | return; |
| 258 | |
| 259 | drvdata = to_cti_drvdata(cti); |
| 260 | |
| 261 | mutex_lock(&drvdata->mutex); |
| 262 | __cti_unmap_trigout(drvdata, trig, ch); |
| 263 | |
| 264 | if (drvdata->refcnt == 1) |
| 265 | cti_disable(drvdata); |
| 266 | drvdata->refcnt--; |
| 267 | mutex_unlock(&drvdata->mutex); |
| 268 | |
| 269 | clk_disable_unprepare(drvdata->clk); |
| 270 | } |
| 271 | EXPORT_SYMBOL(coresight_cti_unmap_trigout); |
| 272 | |
| 273 | struct coresight_cti *coresight_cti_get(const char *name) |
| 274 | { |
| 275 | struct coresight_cti *cti; |
| 276 | |
| 277 | mutex_lock(&cti_lock); |
| 278 | list_for_each_entry(cti, &cti_list, link) { |
| 279 | if (!strncmp(cti->name, name, strlen(cti->name) + 1)) { |
| 280 | mutex_unlock(&cti_lock); |
| 281 | return cti; |
| 282 | } |
| 283 | } |
| 284 | mutex_unlock(&cti_lock); |
| 285 | |
| 286 | return ERR_PTR(-EINVAL); |
| 287 | } |
| 288 | EXPORT_SYMBOL(coresight_cti_get); |
| 289 | |
| 290 | void coresight_cti_put(struct coresight_cti *cti) |
| 291 | { |
| 292 | } |
| 293 | EXPORT_SYMBOL(coresight_cti_put); |
| 294 | |
| 295 | static ssize_t cti_store_map_trigin(struct device *dev, |
| 296 | struct device_attribute *attr, |
| 297 | const char *buf, size_t size) |
| 298 | { |
| 299 | struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent); |
| 300 | unsigned long val1, val2; |
| 301 | int ret; |
| 302 | |
| 303 | if (sscanf(buf, "%lx %lx", &val1, &val2) != 2) |
| 304 | return -EINVAL; |
| 305 | |
| 306 | ret = coresight_cti_map_trigin(&drvdata->cti, val1, val2); |
| 307 | |
| 308 | if (ret) |
| 309 | return ret; |
| 310 | return size; |
| 311 | } |
| 312 | static DEVICE_ATTR(map_trigin, S_IWUSR, NULL, cti_store_map_trigin); |
| 313 | |
| 314 | static ssize_t cti_store_map_trigout(struct device *dev, |
| 315 | struct device_attribute *attr, |
| 316 | const char *buf, size_t size) |
| 317 | { |
| 318 | struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent); |
| 319 | unsigned long val1, val2; |
| 320 | int ret; |
| 321 | |
| 322 | if (sscanf(buf, "%lx %lx", &val1, &val2) != 2) |
| 323 | return -EINVAL; |
| 324 | |
| 325 | ret = coresight_cti_map_trigout(&drvdata->cti, val1, val2); |
| 326 | |
| 327 | if (ret) |
| 328 | return ret; |
| 329 | return size; |
| 330 | } |
| 331 | static DEVICE_ATTR(map_trigout, S_IWUSR, NULL, cti_store_map_trigout); |
| 332 | |
| 333 | static ssize_t cti_store_unmap_trigin(struct device *dev, |
| 334 | struct device_attribute *attr, |
| 335 | const char *buf, size_t size) |
| 336 | { |
| 337 | struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent); |
| 338 | unsigned long val1, val2; |
| 339 | |
| 340 | if (sscanf(buf, "%lx %lx", &val1, &val2) != 2) |
| 341 | return -EINVAL; |
| 342 | |
| 343 | coresight_cti_unmap_trigin(&drvdata->cti, val1, val2); |
| 344 | |
| 345 | return size; |
| 346 | } |
| 347 | static DEVICE_ATTR(unmap_trigin, S_IWUSR, NULL, cti_store_unmap_trigin); |
| 348 | |
| 349 | static ssize_t cti_store_unmap_trigout(struct device *dev, |
| 350 | struct device_attribute *attr, |
| 351 | const char *buf, size_t size) |
| 352 | { |
| 353 | struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent); |
| 354 | unsigned long val1, val2; |
| 355 | |
| 356 | if (sscanf(buf, "%lx %lx", &val1, &val2) != 2) |
| 357 | return -EINVAL; |
| 358 | |
| 359 | coresight_cti_unmap_trigout(&drvdata->cti, val1, val2); |
| 360 | |
| 361 | return size; |
| 362 | } |
| 363 | static DEVICE_ATTR(unmap_trigout, S_IWUSR, NULL, cti_store_unmap_trigout); |
| 364 | |
| 365 | static struct attribute *cti_attrs[] = { |
| 366 | &dev_attr_map_trigin.attr, |
| 367 | &dev_attr_map_trigout.attr, |
| 368 | &dev_attr_unmap_trigin.attr, |
| 369 | &dev_attr_unmap_trigout.attr, |
| 370 | NULL, |
| 371 | }; |
| 372 | |
| 373 | static struct attribute_group cti_attr_grp = { |
| 374 | .attrs = cti_attrs, |
| 375 | }; |
| 376 | |
| 377 | static const struct attribute_group *cti_attr_grps[] = { |
| 378 | &cti_attr_grp, |
| 379 | NULL, |
| 380 | }; |
| 381 | |
| 382 | static int __devinit cti_probe(struct platform_device *pdev) |
| 383 | { |
| 384 | int ret; |
| 385 | struct device *dev = &pdev->dev; |
| 386 | struct coresight_platform_data *pdata; |
| 387 | struct cti_drvdata *drvdata; |
| 388 | struct resource *res; |
| 389 | struct coresight_desc *desc; |
| 390 | |
| 391 | if (pdev->dev.of_node) { |
| 392 | pdata = of_get_coresight_platform_data(dev, pdev->dev.of_node); |
| 393 | if (IS_ERR(pdata)) |
| 394 | return PTR_ERR(pdata); |
| 395 | pdev->dev.platform_data = pdata; |
| 396 | } |
| 397 | |
| 398 | drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); |
| 399 | if (!drvdata) |
| 400 | return -ENOMEM; |
| 401 | /* Store the driver data pointer for use in exported functions */ |
| 402 | drvdata->dev = &pdev->dev; |
| 403 | platform_set_drvdata(pdev, drvdata); |
| 404 | |
Aparna Das | c990751 | 2013-03-08 10:20:52 -0800 | [diff] [blame] | 405 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cti-base"); |
Pratik Patel | d944cc7 | 2013-02-08 11:52:12 -0800 | [diff] [blame] | 406 | if (!res) |
| 407 | return -ENODEV; |
| 408 | |
| 409 | drvdata->base = devm_ioremap(dev, res->start, resource_size(res)); |
| 410 | if (!drvdata->base) |
| 411 | return -ENOMEM; |
| 412 | |
| 413 | mutex_init(&drvdata->mutex); |
| 414 | |
| 415 | drvdata->clk = devm_clk_get(dev, "core_clk"); |
| 416 | if (IS_ERR(drvdata->clk)) |
| 417 | return PTR_ERR(drvdata->clk); |
| 418 | |
| 419 | ret = clk_set_rate(drvdata->clk, CORESIGHT_CLK_RATE_TRACE); |
| 420 | if (ret) |
| 421 | return ret; |
| 422 | |
| 423 | mutex_lock(&cti_lock); |
| 424 | drvdata->cti.name = ((struct coresight_platform_data *) |
| 425 | (pdev->dev.platform_data))->name; |
| 426 | list_add_tail(&drvdata->cti.link, &cti_list); |
| 427 | mutex_unlock(&cti_lock); |
| 428 | |
| 429 | desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL); |
| 430 | if (!desc) |
| 431 | return -ENOMEM; |
| 432 | desc->type = CORESIGHT_DEV_TYPE_NONE; |
| 433 | desc->pdata = pdev->dev.platform_data; |
| 434 | desc->dev = &pdev->dev; |
| 435 | desc->groups = cti_attr_grps; |
| 436 | desc->owner = THIS_MODULE; |
| 437 | drvdata->csdev = coresight_register(desc); |
| 438 | if (IS_ERR(drvdata->csdev)) |
| 439 | return PTR_ERR(drvdata->csdev); |
| 440 | |
| 441 | dev_info(dev, "CTI initialized\n"); |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | static int __devexit cti_remove(struct platform_device *pdev) |
| 446 | { |
| 447 | struct cti_drvdata *drvdata = platform_get_drvdata(pdev); |
| 448 | |
| 449 | coresight_unregister(drvdata->csdev); |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | static struct of_device_id cti_match[] = { |
| 454 | {.compatible = "arm,coresight-cti"}, |
| 455 | {} |
| 456 | }; |
| 457 | |
| 458 | static struct platform_driver cti_driver = { |
| 459 | .probe = cti_probe, |
| 460 | .remove = __devexit_p(cti_remove), |
| 461 | .driver = { |
| 462 | .name = "coresight-cti", |
| 463 | .owner = THIS_MODULE, |
| 464 | .of_match_table = cti_match, |
| 465 | }, |
| 466 | }; |
| 467 | |
| 468 | static int __init cti_init(void) |
| 469 | { |
| 470 | return platform_driver_register(&cti_driver); |
| 471 | } |
| 472 | module_init(cti_init); |
| 473 | |
| 474 | static void __exit cti_exit(void) |
| 475 | { |
| 476 | platform_driver_unregister(&cti_driver); |
| 477 | } |
| 478 | module_exit(cti_exit); |
| 479 | |
| 480 | MODULE_LICENSE("GPL v2"); |
| 481 | MODULE_DESCRIPTION("CoreSight CTI driver"); |