Pratik Patel | 2291149 | 2012-06-25 21:38:02 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012, Code Aurora Forum. 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/types.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/miscdevice.h> |
| 23 | #include <linux/uaccess.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/memory_alloc.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/spinlock.h> |
| 28 | #include <linux/clk.h> |
| 29 | #include <linux/of_coresight.h> |
| 30 | #include <linux/coresight.h> |
| 31 | #include <mach/memory.h> |
| 32 | |
| 33 | #include "coresight-priv.h" |
| 34 | |
Pratik Patel | 2291149 | 2012-06-25 21:38:02 -0700 | [diff] [blame] | 35 | #define tmc_writel(drvdata, val, off) __raw_writel((val), drvdata->base + off) |
| 36 | #define tmc_readl(drvdata, off) __raw_readl(drvdata->base + off) |
| 37 | |
| 38 | #define TMC_LOCK(drvdata) \ |
| 39 | do { \ |
| 40 | mb(); \ |
| 41 | tmc_writel(drvdata, 0x0, CORESIGHT_LAR); \ |
| 42 | } while (0) |
| 43 | #define TMC_UNLOCK(drvdata) \ |
| 44 | do { \ |
| 45 | tmc_writel(drvdata, CORESIGHT_UNLOCK, CORESIGHT_LAR); \ |
| 46 | mb(); \ |
| 47 | } while (0) |
| 48 | |
Pratik Patel | 2291149 | 2012-06-25 21:38:02 -0700 | [diff] [blame] | 49 | #define TMC_RSZ (0x004) |
| 50 | #define TMC_STS (0x00C) |
| 51 | #define TMC_RRD (0x010) |
| 52 | #define TMC_RRP (0x014) |
| 53 | #define TMC_RWP (0x018) |
| 54 | #define TMC_TRG (0x01C) |
| 55 | #define TMC_CTL (0x020) |
| 56 | #define TMC_RWD (0x024) |
| 57 | #define TMC_MODE (0x028) |
| 58 | #define TMC_LBUFLEVEL (0x02C) |
| 59 | #define TMC_CBUFLEVEL (0x030) |
| 60 | #define TMC_BUFWM (0x034) |
| 61 | #define TMC_RRPHI (0x038) |
| 62 | #define TMC_RWPHI (0x03C) |
| 63 | #define TMC_AXICTL (0x110) |
| 64 | #define TMC_DBALO (0x118) |
| 65 | #define TMC_DBAHI (0x11C) |
| 66 | #define TMC_FFSR (0x300) |
| 67 | #define TMC_FFCR (0x304) |
| 68 | #define TMC_PSCR (0x308) |
| 69 | #define TMC_ITMISCOP0 (0xEE0) |
| 70 | #define TMC_ITTRFLIN (0xEE8) |
| 71 | #define TMC_ITATBDATA0 (0xEEC) |
| 72 | #define TMC_ITATBCTR2 (0xEF0) |
| 73 | #define TMC_ITATBCTR1 (0xEF4) |
| 74 | #define TMC_ITATBCTR0 (0xEF8) |
| 75 | |
Pratik Patel | 2291149 | 2012-06-25 21:38:02 -0700 | [diff] [blame] | 76 | #define BYTES_PER_WORD 4 |
| 77 | |
| 78 | enum tmc_config_type { |
| 79 | TMC_CONFIG_TYPE_ETB, |
| 80 | TMC_CONFIG_TYPE_ETR, |
| 81 | TMC_CONFIG_TYPE_ETF, |
| 82 | }; |
| 83 | |
| 84 | enum tmc_mode { |
| 85 | TMC_MODE_CIRCULAR_BUFFER, |
| 86 | TMC_MODE_SOFTWARE_FIFO, |
| 87 | TMC_MODE_HARDWARE_FIFO, |
| 88 | }; |
| 89 | |
| 90 | enum tmc_mem_intf_width { |
| 91 | TMC_MEM_INTF_WIDTH_32BITS = 0x2, |
| 92 | TMC_MEM_INTF_WIDTH_64BITS = 0x3, |
| 93 | TMC_MEM_INTF_WIDTH_128BITS = 0x4, |
| 94 | TMC_MEM_INTF_WIDTH_256BITS = 0x5, |
| 95 | }; |
| 96 | |
Pratik Patel | 2291149 | 2012-06-25 21:38:02 -0700 | [diff] [blame] | 97 | struct tmc_drvdata { |
| 98 | void __iomem *base; |
| 99 | struct device *dev; |
| 100 | struct coresight_device *csdev; |
| 101 | struct miscdevice miscdev; |
| 102 | struct clk *clk; |
| 103 | spinlock_t spinlock; |
| 104 | int read_count; |
| 105 | bool reading; |
| 106 | char *buf; |
| 107 | unsigned long paddr; |
| 108 | void __iomem *vaddr; |
| 109 | uint32_t size; |
| 110 | bool enable; |
| 111 | enum tmc_config_type config_type; |
| 112 | uint32_t trigger_cntr; |
| 113 | }; |
| 114 | |
Pratik Patel | 2291149 | 2012-06-25 21:38:02 -0700 | [diff] [blame] | 115 | static void tmc_wait_for_ready(struct tmc_drvdata *drvdata) |
| 116 | { |
| 117 | int count; |
| 118 | |
| 119 | /* Ensure formatter, unformatter and hardware fifo are empty */ |
| 120 | for (count = TIMEOUT_US; BVAL(tmc_readl(drvdata, TMC_STS), 2) != 1 |
| 121 | && count > 0; count--) |
| 122 | udelay(1); |
| 123 | WARN(count == 0, "timeout while waiting for TMC ready, TMC_STS: %#x\n", |
| 124 | tmc_readl(drvdata, TMC_STS)); |
| 125 | } |
| 126 | |
| 127 | static void tmc_flush_and_stop(struct tmc_drvdata *drvdata) |
| 128 | { |
| 129 | int count; |
| 130 | uint32_t ffcr; |
| 131 | |
| 132 | ffcr = tmc_readl(drvdata, TMC_FFCR); |
| 133 | ffcr |= BIT(12); |
| 134 | tmc_writel(drvdata, ffcr, TMC_FFCR); |
| 135 | ffcr |= BIT(6); |
| 136 | tmc_writel(drvdata, ffcr, TMC_FFCR); |
| 137 | /* Ensure flush completes */ |
| 138 | for (count = TIMEOUT_US; BVAL(tmc_readl(drvdata, TMC_FFCR), 6) != 0 |
| 139 | && count > 0; count--) |
| 140 | udelay(1); |
| 141 | WARN(count == 0, "timeout while flushing TMC, TMC_FFCR: %#x\n", |
| 142 | tmc_readl(drvdata, TMC_FFCR)); |
| 143 | |
| 144 | tmc_wait_for_ready(drvdata); |
| 145 | } |
| 146 | |
| 147 | static void __tmc_enable(struct tmc_drvdata *drvdata) |
| 148 | { |
| 149 | tmc_writel(drvdata, 0x1, TMC_CTL); |
| 150 | } |
| 151 | |
| 152 | static void __tmc_disable(struct tmc_drvdata *drvdata) |
| 153 | { |
| 154 | tmc_writel(drvdata, 0x0, TMC_CTL); |
| 155 | } |
| 156 | |
| 157 | static void __tmc_etb_enable(struct tmc_drvdata *drvdata) |
| 158 | { |
| 159 | /* Zero out the memory to help with debug */ |
| 160 | memset(drvdata->buf, 0, drvdata->size); |
| 161 | |
| 162 | TMC_UNLOCK(drvdata); |
| 163 | |
| 164 | tmc_writel(drvdata, TMC_MODE_CIRCULAR_BUFFER, TMC_MODE); |
| 165 | tmc_writel(drvdata, 0x133, TMC_FFCR); |
| 166 | tmc_writel(drvdata, drvdata->trigger_cntr, TMC_TRG); |
| 167 | __tmc_enable(drvdata); |
| 168 | |
| 169 | TMC_LOCK(drvdata); |
| 170 | } |
| 171 | |
| 172 | static void __tmc_etr_enable(struct tmc_drvdata *drvdata) |
| 173 | { |
| 174 | uint32_t axictl; |
| 175 | |
| 176 | /* Zero out the memory to help with debug */ |
| 177 | memset(drvdata->vaddr, 0, drvdata->size); |
| 178 | |
| 179 | TMC_UNLOCK(drvdata); |
| 180 | |
| 181 | tmc_writel(drvdata, drvdata->size / BYTES_PER_WORD, TMC_RSZ); |
| 182 | tmc_writel(drvdata, TMC_MODE_CIRCULAR_BUFFER, TMC_MODE); |
| 183 | |
| 184 | axictl = tmc_readl(drvdata, TMC_AXICTL); |
| 185 | axictl |= (0xF << 8); |
| 186 | tmc_writel(drvdata, axictl, TMC_AXICTL); |
| 187 | axictl &= ~(0x1 << 7); |
| 188 | tmc_writel(drvdata, axictl, TMC_AXICTL); |
| 189 | axictl = (axictl & ~0x3) | 0x2; |
| 190 | tmc_writel(drvdata, axictl, TMC_AXICTL); |
| 191 | |
| 192 | tmc_writel(drvdata, drvdata->paddr, TMC_DBALO); |
| 193 | tmc_writel(drvdata, 0x0, TMC_DBAHI); |
| 194 | tmc_writel(drvdata, 0x133, TMC_FFCR); |
| 195 | __tmc_enable(drvdata); |
| 196 | |
| 197 | TMC_LOCK(drvdata); |
| 198 | } |
| 199 | |
| 200 | static void __tmc_etf_enable(struct tmc_drvdata *drvdata) |
| 201 | { |
| 202 | TMC_UNLOCK(drvdata); |
| 203 | |
| 204 | tmc_writel(drvdata, TMC_MODE_HARDWARE_FIFO, TMC_MODE); |
| 205 | tmc_writel(drvdata, 0x3, TMC_FFCR); |
| 206 | tmc_writel(drvdata, 0x0, TMC_BUFWM); |
| 207 | __tmc_enable(drvdata); |
| 208 | |
| 209 | TMC_LOCK(drvdata); |
| 210 | } |
| 211 | |
| 212 | static int tmc_enable(struct tmc_drvdata *drvdata, enum tmc_mode mode) |
| 213 | { |
| 214 | int ret; |
| 215 | unsigned long flags; |
| 216 | |
| 217 | ret = clk_prepare_enable(drvdata->clk); |
| 218 | if (ret) |
| 219 | return ret; |
| 220 | |
| 221 | spin_lock_irqsave(&drvdata->spinlock, flags); |
| 222 | if (drvdata->reading) { |
| 223 | spin_unlock_irqrestore(&drvdata->spinlock, flags); |
| 224 | clk_disable_unprepare(drvdata->clk); |
| 225 | return -EBUSY; |
| 226 | } |
| 227 | |
| 228 | if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) { |
| 229 | __tmc_etb_enable(drvdata); |
| 230 | } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) { |
| 231 | __tmc_etr_enable(drvdata); |
| 232 | } else { |
| 233 | if (mode == TMC_MODE_CIRCULAR_BUFFER) |
| 234 | __tmc_etb_enable(drvdata); |
| 235 | else |
| 236 | __tmc_etf_enable(drvdata); |
| 237 | } |
| 238 | drvdata->enable = true; |
| 239 | spin_unlock_irqrestore(&drvdata->spinlock, flags); |
| 240 | |
| 241 | dev_info(drvdata->dev, "TMC enabled\n"); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int tmc_enable_sink(struct coresight_device *csdev) |
| 246 | { |
| 247 | struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); |
| 248 | |
| 249 | return tmc_enable(drvdata, TMC_MODE_CIRCULAR_BUFFER); |
| 250 | } |
| 251 | |
| 252 | static int tmc_enable_link(struct coresight_device *csdev, int inport, |
| 253 | int outport) |
| 254 | { |
| 255 | struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); |
| 256 | |
| 257 | return tmc_enable(drvdata, TMC_MODE_HARDWARE_FIFO); |
| 258 | } |
| 259 | |
| 260 | static void __tmc_etb_dump(struct tmc_drvdata *drvdata) |
| 261 | { |
| 262 | enum tmc_mem_intf_width memwidth; |
| 263 | uint8_t memwords; |
| 264 | char *bufp; |
| 265 | uint32_t read_data; |
| 266 | int i; |
| 267 | |
| 268 | memwidth = BMVAL(tmc_readl(drvdata, CORESIGHT_DEVID), 8, 10); |
| 269 | if (memwidth == TMC_MEM_INTF_WIDTH_32BITS) |
| 270 | memwords = 1; |
| 271 | else if (memwidth == TMC_MEM_INTF_WIDTH_64BITS) |
| 272 | memwords = 2; |
| 273 | else if (memwidth == TMC_MEM_INTF_WIDTH_128BITS) |
| 274 | memwords = 4; |
| 275 | else |
| 276 | memwords = 8; |
| 277 | |
| 278 | bufp = drvdata->buf; |
| 279 | while (1) { |
| 280 | for (i = 0; i < memwords; i++) { |
| 281 | read_data = tmc_readl(drvdata, TMC_RRD); |
| 282 | if (read_data == 0xFFFFFFFF) |
| 283 | return; |
| 284 | memcpy(bufp, &read_data, BYTES_PER_WORD); |
| 285 | bufp += BYTES_PER_WORD; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | static void __tmc_etb_disable(struct tmc_drvdata *drvdata) |
| 291 | { |
| 292 | TMC_UNLOCK(drvdata); |
| 293 | |
| 294 | tmc_flush_and_stop(drvdata); |
| 295 | __tmc_etb_dump(drvdata); |
| 296 | __tmc_disable(drvdata); |
| 297 | |
| 298 | TMC_LOCK(drvdata); |
| 299 | } |
| 300 | |
| 301 | static void __tmc_etr_dump(struct tmc_drvdata *drvdata) |
| 302 | { |
| 303 | uint32_t rwp, rwphi; |
| 304 | |
| 305 | rwp = tmc_readl(drvdata, TMC_RWP); |
| 306 | rwphi = tmc_readl(drvdata, TMC_RWPHI); |
| 307 | |
| 308 | if (BVAL(tmc_readl(drvdata, TMC_STS), 0)) |
| 309 | drvdata->buf = drvdata->vaddr + rwp; |
| 310 | else |
| 311 | drvdata->buf = drvdata->vaddr; |
| 312 | } |
| 313 | |
| 314 | static void __tmc_etr_disable(struct tmc_drvdata *drvdata) |
| 315 | { |
| 316 | TMC_UNLOCK(drvdata); |
| 317 | |
| 318 | tmc_flush_and_stop(drvdata); |
| 319 | __tmc_etr_dump(drvdata); |
| 320 | __tmc_disable(drvdata); |
| 321 | |
| 322 | TMC_LOCK(drvdata); |
| 323 | } |
| 324 | |
| 325 | static void __tmc_etf_disable(struct tmc_drvdata *drvdata) |
| 326 | { |
| 327 | TMC_UNLOCK(drvdata); |
| 328 | |
| 329 | tmc_flush_and_stop(drvdata); |
| 330 | __tmc_disable(drvdata); |
| 331 | |
| 332 | TMC_LOCK(drvdata); |
| 333 | } |
| 334 | |
| 335 | static void tmc_disable(struct tmc_drvdata *drvdata, enum tmc_mode mode) |
| 336 | { |
| 337 | unsigned long flags; |
| 338 | |
| 339 | spin_lock_irqsave(&drvdata->spinlock, flags); |
| 340 | if (drvdata->reading) |
| 341 | goto out; |
| 342 | |
| 343 | if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) { |
| 344 | __tmc_etb_disable(drvdata); |
| 345 | } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) { |
| 346 | __tmc_etr_disable(drvdata); |
| 347 | } else { |
| 348 | if (mode == TMC_MODE_CIRCULAR_BUFFER) |
| 349 | __tmc_etb_disable(drvdata); |
| 350 | else |
| 351 | __tmc_etf_disable(drvdata); |
| 352 | } |
| 353 | out: |
| 354 | drvdata->enable = false; |
| 355 | spin_unlock_irqrestore(&drvdata->spinlock, flags); |
| 356 | |
| 357 | clk_disable_unprepare(drvdata->clk); |
| 358 | |
| 359 | dev_info(drvdata->dev, "TMC disabled\n"); |
| 360 | } |
| 361 | |
| 362 | static void tmc_disable_sink(struct coresight_device *csdev) |
| 363 | { |
| 364 | struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); |
| 365 | |
| 366 | tmc_disable(drvdata, TMC_MODE_CIRCULAR_BUFFER); |
| 367 | } |
| 368 | |
| 369 | static void tmc_disable_link(struct coresight_device *csdev, int inport, |
| 370 | int outport) |
| 371 | { |
| 372 | struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); |
| 373 | |
| 374 | tmc_disable(drvdata, TMC_MODE_HARDWARE_FIFO); |
| 375 | } |
| 376 | |
Pratik Patel | cf7d045 | 2012-07-02 13:57:20 -0700 | [diff] [blame] | 377 | static void tmc_abort(struct coresight_device *csdev) |
| 378 | { |
| 379 | struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); |
| 380 | unsigned long flags; |
| 381 | enum tmc_mode mode; |
| 382 | |
| 383 | spin_lock_irqsave(&drvdata->spinlock, flags); |
| 384 | if (drvdata->reading) |
| 385 | goto out0; |
| 386 | |
| 387 | if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) { |
| 388 | __tmc_etb_disable(drvdata); |
| 389 | } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) { |
| 390 | __tmc_etr_disable(drvdata); |
| 391 | } else { |
| 392 | mode = tmc_readl(drvdata, TMC_MODE); |
| 393 | if (mode == TMC_MODE_CIRCULAR_BUFFER) |
| 394 | __tmc_etb_disable(drvdata); |
| 395 | else |
| 396 | goto out1; |
| 397 | } |
| 398 | out0: |
| 399 | drvdata->enable = false; |
| 400 | spin_unlock_irqrestore(&drvdata->spinlock, flags); |
| 401 | |
| 402 | dev_info(drvdata->dev, "TMC aborted\n"); |
| 403 | return; |
| 404 | out1: |
| 405 | spin_unlock_irqrestore(&drvdata->spinlock, flags); |
| 406 | } |
| 407 | |
Pratik Patel | 2291149 | 2012-06-25 21:38:02 -0700 | [diff] [blame] | 408 | static const struct coresight_ops_sink tmc_sink_ops = { |
| 409 | .enable = tmc_enable_sink, |
| 410 | .disable = tmc_disable_sink, |
Pratik Patel | cf7d045 | 2012-07-02 13:57:20 -0700 | [diff] [blame] | 411 | .abort = tmc_abort, |
Pratik Patel | 2291149 | 2012-06-25 21:38:02 -0700 | [diff] [blame] | 412 | }; |
| 413 | |
| 414 | static const struct coresight_ops_link tmc_link_ops = { |
| 415 | .enable = tmc_enable_link, |
| 416 | .disable = tmc_disable_link, |
| 417 | }; |
| 418 | |
| 419 | static const struct coresight_ops tmc_etb_cs_ops = { |
| 420 | .sink_ops = &tmc_sink_ops, |
| 421 | }; |
| 422 | |
| 423 | static const struct coresight_ops tmc_etr_cs_ops = { |
| 424 | .sink_ops = &tmc_sink_ops, |
| 425 | }; |
| 426 | |
| 427 | static const struct coresight_ops tmc_etf_cs_ops = { |
| 428 | .sink_ops = &tmc_sink_ops, |
| 429 | .link_ops = &tmc_link_ops, |
| 430 | }; |
| 431 | |
| 432 | static int tmc_read_prepare(struct tmc_drvdata *drvdata) |
| 433 | { |
| 434 | int ret; |
| 435 | unsigned long flags; |
| 436 | enum tmc_mode mode; |
| 437 | |
| 438 | spin_lock_irqsave(&drvdata->spinlock, flags); |
| 439 | if (!drvdata->enable) |
| 440 | goto out; |
| 441 | |
| 442 | if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) { |
| 443 | __tmc_etb_disable(drvdata); |
| 444 | } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) { |
| 445 | __tmc_etr_disable(drvdata); |
| 446 | } else { |
| 447 | mode = tmc_readl(drvdata, TMC_MODE); |
| 448 | if (mode == TMC_MODE_CIRCULAR_BUFFER) { |
| 449 | __tmc_etb_disable(drvdata); |
| 450 | } else { |
| 451 | ret = -ENODEV; |
| 452 | goto err; |
| 453 | } |
| 454 | } |
| 455 | out: |
| 456 | drvdata->reading = true; |
| 457 | spin_unlock_irqrestore(&drvdata->spinlock, flags); |
| 458 | |
| 459 | dev_info(drvdata->dev, "TMC read start\n"); |
| 460 | return 0; |
| 461 | err: |
| 462 | spin_unlock_irqrestore(&drvdata->spinlock, flags); |
| 463 | return ret; |
| 464 | } |
| 465 | |
| 466 | static void tmc_read_unprepare(struct tmc_drvdata *drvdata) |
| 467 | { |
| 468 | unsigned long flags; |
| 469 | enum tmc_mode mode; |
| 470 | |
| 471 | spin_lock_irqsave(&drvdata->spinlock, flags); |
| 472 | if (!drvdata->enable) |
| 473 | goto out; |
| 474 | |
| 475 | if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) { |
| 476 | __tmc_etb_enable(drvdata); |
| 477 | } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) { |
| 478 | __tmc_etr_enable(drvdata); |
| 479 | } else { |
| 480 | mode = tmc_readl(drvdata, TMC_MODE); |
| 481 | if (mode == TMC_MODE_CIRCULAR_BUFFER) |
| 482 | __tmc_etb_enable(drvdata); |
| 483 | } |
| 484 | out: |
| 485 | drvdata->reading = false; |
| 486 | spin_unlock_irqrestore(&drvdata->spinlock, flags); |
| 487 | |
| 488 | dev_info(drvdata->dev, "TMC read end\n"); |
| 489 | } |
| 490 | |
| 491 | static int tmc_open(struct inode *inode, struct file *file) |
| 492 | { |
| 493 | struct tmc_drvdata *drvdata = container_of(file->private_data, |
| 494 | struct tmc_drvdata, miscdev); |
| 495 | int ret = 0; |
| 496 | |
| 497 | if (drvdata->read_count++) |
| 498 | goto out; |
| 499 | |
| 500 | ret = tmc_read_prepare(drvdata); |
| 501 | if (ret) |
| 502 | return ret; |
| 503 | out: |
| 504 | nonseekable_open(inode, file); |
| 505 | |
| 506 | dev_dbg(drvdata->dev, "%s: successfully opened\n", __func__); |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | static ssize_t tmc_read(struct file *file, char __user *data, size_t len, |
| 511 | loff_t *ppos) |
| 512 | { |
| 513 | struct tmc_drvdata *drvdata = container_of(file->private_data, |
| 514 | struct tmc_drvdata, miscdev); |
| 515 | char *bufp = drvdata->buf + *ppos; |
| 516 | |
| 517 | if (*ppos + len > drvdata->size) |
| 518 | len = drvdata->size - *ppos; |
| 519 | |
| 520 | if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) { |
| 521 | if (bufp == (char *)(drvdata->vaddr + drvdata->size)) |
| 522 | bufp = drvdata->vaddr; |
| 523 | if ((bufp + len) > (char *)(drvdata->vaddr + drvdata->size)) |
| 524 | len = (char *)(drvdata->vaddr + drvdata->size) - bufp; |
| 525 | } |
| 526 | |
| 527 | if (copy_to_user(data, bufp, len)) { |
| 528 | dev_dbg(drvdata->dev, "%s: copy_to_user failed\n", __func__); |
| 529 | return -EFAULT; |
| 530 | } |
| 531 | |
| 532 | *ppos += len; |
| 533 | |
| 534 | dev_dbg(drvdata->dev, "%s: %d bytes copied, %d bytes left\n", |
| 535 | __func__, len, (int) (drvdata->size - *ppos)); |
| 536 | return len; |
| 537 | } |
| 538 | |
| 539 | static int tmc_release(struct inode *inode, struct file *file) |
| 540 | { |
| 541 | struct tmc_drvdata *drvdata = container_of(file->private_data, |
| 542 | struct tmc_drvdata, miscdev); |
| 543 | |
| 544 | if (--drvdata->read_count) { |
| 545 | if (drvdata->read_count < 0) { |
| 546 | WARN_ONCE(1, "mismatched close\n"); |
| 547 | drvdata->read_count = 0; |
| 548 | } |
| 549 | goto out; |
| 550 | } |
| 551 | |
| 552 | tmc_read_unprepare(drvdata); |
| 553 | out: |
| 554 | dev_dbg(drvdata->dev, "%s: released\n", __func__); |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static const struct file_operations tmc_fops = { |
| 559 | .owner = THIS_MODULE, |
| 560 | .open = tmc_open, |
| 561 | .read = tmc_read, |
| 562 | .release = tmc_release, |
| 563 | .llseek = no_llseek, |
| 564 | }; |
| 565 | |
| 566 | static ssize_t tmc_show_trigger_cntr(struct device *dev, |
| 567 | struct device_attribute *attr, char *buf) |
| 568 | { |
| 569 | struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent); |
| 570 | unsigned long val = drvdata->trigger_cntr; |
| 571 | |
| 572 | return scnprintf(buf, PAGE_SIZE, "%#lx\n", val); |
| 573 | } |
| 574 | |
| 575 | static ssize_t tmc_store_trigger_cntr(struct device *dev, |
| 576 | struct device_attribute *attr, |
| 577 | const char *buf, size_t size) |
| 578 | { |
| 579 | struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent); |
| 580 | unsigned long val; |
| 581 | |
| 582 | if (sscanf(buf, "%lx", &val) != 1) |
| 583 | return -EINVAL; |
| 584 | |
| 585 | drvdata->trigger_cntr = val; |
| 586 | return size; |
| 587 | } |
| 588 | static DEVICE_ATTR(trigger_cntr, S_IRUGO | S_IWUSR, tmc_show_trigger_cntr, |
| 589 | tmc_store_trigger_cntr); |
| 590 | |
| 591 | static struct attribute *tmc_attrs[] = { |
| 592 | &dev_attr_trigger_cntr.attr, |
| 593 | NULL, |
| 594 | }; |
| 595 | |
| 596 | static struct attribute_group tmc_attr_grp = { |
| 597 | .attrs = tmc_attrs, |
| 598 | }; |
| 599 | |
| 600 | static const struct attribute_group *tmc_etb_attr_grps[] = { |
| 601 | &tmc_attr_grp, |
| 602 | NULL, |
| 603 | }; |
| 604 | |
| 605 | static const struct attribute_group *tmc_etr_attr_grps[] = { |
| 606 | &tmc_attr_grp, |
| 607 | NULL, |
| 608 | }; |
| 609 | |
| 610 | static const struct attribute_group *tmc_etf_attr_grps[] = { |
| 611 | &tmc_attr_grp, |
| 612 | NULL, |
| 613 | }; |
| 614 | |
| 615 | static int __devinit tmc_probe(struct platform_device *pdev) |
| 616 | { |
| 617 | int ret; |
| 618 | uint32_t devid; |
| 619 | struct device *dev = &pdev->dev; |
| 620 | struct coresight_platform_data *pdata; |
| 621 | struct tmc_drvdata *drvdata; |
| 622 | struct resource *res; |
| 623 | struct coresight_desc *desc; |
| 624 | |
| 625 | if (pdev->dev.of_node) { |
| 626 | pdata = of_get_coresight_platform_data(dev, pdev->dev.of_node); |
| 627 | if (IS_ERR(pdata)) |
| 628 | return PTR_ERR(pdata); |
| 629 | pdev->dev.platform_data = pdata; |
| 630 | } |
| 631 | |
| 632 | drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); |
| 633 | if (!drvdata) |
| 634 | return -ENOMEM; |
| 635 | drvdata->dev = &pdev->dev; |
| 636 | platform_set_drvdata(pdev, drvdata); |
| 637 | |
| 638 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 639 | if (!res) |
| 640 | return -ENODEV; |
Pratik Patel | 2c09b76 | 2012-07-21 15:54:54 -0700 | [diff] [blame] | 641 | |
Pratik Patel | 2291149 | 2012-06-25 21:38:02 -0700 | [diff] [blame] | 642 | drvdata->base = devm_ioremap(dev, res->start, resource_size(res)); |
| 643 | if (!drvdata->base) |
| 644 | return -ENOMEM; |
| 645 | |
| 646 | spin_lock_init(&drvdata->spinlock); |
| 647 | |
| 648 | drvdata->clk = devm_clk_get(dev, "core_clk"); |
| 649 | if (IS_ERR(drvdata->clk)) |
| 650 | return PTR_ERR(drvdata->clk); |
Pratik Patel | 2c09b76 | 2012-07-21 15:54:54 -0700 | [diff] [blame] | 651 | |
Pratik Patel | 2291149 | 2012-06-25 21:38:02 -0700 | [diff] [blame] | 652 | ret = clk_set_rate(drvdata->clk, CORESIGHT_CLK_RATE_TRACE); |
| 653 | if (ret) |
| 654 | return ret; |
| 655 | |
| 656 | ret = clk_prepare_enable(drvdata->clk); |
| 657 | if (ret) |
| 658 | return ret; |
Pratik Patel | 2c09b76 | 2012-07-21 15:54:54 -0700 | [diff] [blame] | 659 | |
Pratik Patel | 2291149 | 2012-06-25 21:38:02 -0700 | [diff] [blame] | 660 | devid = tmc_readl(drvdata, CORESIGHT_DEVID); |
| 661 | drvdata->config_type = BMVAL(devid, 6, 7); |
Pratik Patel | 2c09b76 | 2012-07-21 15:54:54 -0700 | [diff] [blame] | 662 | |
Pratik Patel | 2291149 | 2012-06-25 21:38:02 -0700 | [diff] [blame] | 663 | if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) |
| 664 | drvdata->size = SZ_1M; |
| 665 | else |
| 666 | drvdata->size = tmc_readl(drvdata, TMC_RSZ) * BYTES_PER_WORD; |
Pratik Patel | 2c09b76 | 2012-07-21 15:54:54 -0700 | [diff] [blame] | 667 | |
Pratik Patel | 2291149 | 2012-06-25 21:38:02 -0700 | [diff] [blame] | 668 | clk_disable_unprepare(drvdata->clk); |
| 669 | |
| 670 | if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) { |
| 671 | drvdata->paddr = allocate_contiguous_ebi_nomap(drvdata->size, |
| 672 | SZ_4K); |
| 673 | if (!drvdata->paddr) |
| 674 | return -ENOMEM; |
| 675 | drvdata->vaddr = devm_ioremap(dev, drvdata->paddr, |
| 676 | drvdata->size); |
| 677 | if (!drvdata->vaddr) { |
| 678 | ret = -ENOMEM; |
| 679 | goto err0; |
| 680 | } |
| 681 | memset(drvdata->vaddr, 0, drvdata->size); |
| 682 | } else { |
| 683 | drvdata->buf = devm_kzalloc(dev, drvdata->size, GFP_KERNEL); |
| 684 | if (!drvdata->buf) |
| 685 | return -ENOMEM; |
| 686 | } |
| 687 | |
| 688 | desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL); |
| 689 | if (!desc) { |
| 690 | ret = -ENOMEM; |
| 691 | goto err0; |
| 692 | } |
| 693 | if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) { |
| 694 | desc->type = CORESIGHT_DEV_TYPE_SINK; |
| 695 | desc->subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER; |
| 696 | desc->ops = &tmc_etb_cs_ops; |
| 697 | desc->pdata = pdev->dev.platform_data; |
| 698 | desc->dev = &pdev->dev; |
| 699 | desc->groups = tmc_etb_attr_grps; |
| 700 | desc->owner = THIS_MODULE; |
| 701 | drvdata->csdev = coresight_register(desc); |
| 702 | if (IS_ERR(drvdata->csdev)) { |
| 703 | ret = PTR_ERR(drvdata->csdev); |
| 704 | goto err0; |
| 705 | } |
| 706 | } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) { |
| 707 | desc->type = CORESIGHT_DEV_TYPE_SINK; |
| 708 | desc->subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER; |
| 709 | desc->ops = &tmc_etr_cs_ops; |
| 710 | desc->pdata = pdev->dev.platform_data; |
| 711 | desc->dev = &pdev->dev; |
| 712 | desc->groups = tmc_etr_attr_grps; |
| 713 | desc->owner = THIS_MODULE; |
| 714 | drvdata->csdev = coresight_register(desc); |
| 715 | if (IS_ERR(drvdata->csdev)) { |
| 716 | ret = PTR_ERR(drvdata->csdev); |
| 717 | goto err0; |
| 718 | } |
| 719 | } else { |
| 720 | desc->type = CORESIGHT_DEV_TYPE_LINKSINK; |
| 721 | desc->subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER; |
| 722 | desc->subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_FIFO; |
| 723 | desc->ops = &tmc_etf_cs_ops; |
| 724 | desc->pdata = pdev->dev.platform_data; |
| 725 | desc->dev = &pdev->dev; |
| 726 | desc->groups = tmc_etf_attr_grps; |
| 727 | desc->owner = THIS_MODULE; |
| 728 | drvdata->csdev = coresight_register(desc); |
| 729 | if (IS_ERR(drvdata->csdev)) { |
| 730 | ret = PTR_ERR(drvdata->csdev); |
| 731 | goto err0; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | drvdata->miscdev.name = ((struct coresight_platform_data *) |
| 736 | (pdev->dev.platform_data))->name; |
| 737 | drvdata->miscdev.minor = MISC_DYNAMIC_MINOR; |
| 738 | drvdata->miscdev.fops = &tmc_fops; |
| 739 | ret = misc_register(&drvdata->miscdev); |
| 740 | if (ret) |
| 741 | goto err1; |
| 742 | |
| 743 | dev_info(dev, "TMC initialized\n"); |
| 744 | return 0; |
| 745 | err1: |
| 746 | coresight_unregister(drvdata->csdev); |
| 747 | err0: |
| 748 | free_contiguous_memory_by_paddr(drvdata->paddr); |
| 749 | return ret; |
| 750 | } |
| 751 | |
| 752 | static int __devexit tmc_remove(struct platform_device *pdev) |
| 753 | { |
| 754 | struct tmc_drvdata *drvdata = platform_get_drvdata(pdev); |
| 755 | |
| 756 | misc_deregister(&drvdata->miscdev); |
| 757 | coresight_unregister(drvdata->csdev); |
| 758 | free_contiguous_memory_by_paddr(drvdata->paddr); |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static struct of_device_id tmc_match[] = { |
| 763 | {.compatible = "arm,coresight-tmc"}, |
| 764 | {} |
| 765 | }; |
| 766 | |
| 767 | static struct platform_driver tmc_driver = { |
| 768 | .probe = tmc_probe, |
| 769 | .remove = __devexit_p(tmc_remove), |
| 770 | .driver = { |
| 771 | .name = "coresight-tmc", |
| 772 | .owner = THIS_MODULE, |
| 773 | .of_match_table = tmc_match, |
| 774 | }, |
| 775 | }; |
| 776 | |
| 777 | static int __init tmc_init(void) |
| 778 | { |
| 779 | return platform_driver_register(&tmc_driver); |
| 780 | } |
| 781 | module_init(tmc_init); |
| 782 | |
| 783 | static void __exit tmc_exit(void) |
| 784 | { |
| 785 | platform_driver_unregister(&tmc_driver); |
| 786 | } |
| 787 | module_exit(tmc_exit); |
| 788 | |
| 789 | MODULE_LICENSE("GPL v2"); |
| 790 | MODULE_DESCRIPTION("CoreSight Trace Memory Controller driver"); |