Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 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> |
Pratik Patel | cf41862 | 2011-09-22 11:15:11 -0700 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/device.h> |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 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/delay.h> |
Pratik Patel | cf41862 | 2011-09-22 11:15:11 -0700 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 27 | |
| 28 | #include "qdss.h" |
| 29 | |
| 30 | #define etb_writel(etb, val, off) __raw_writel((val), etb.base + off) |
| 31 | #define etb_readl(etb, off) __raw_readl(etb.base + off) |
| 32 | |
| 33 | #define ETB_RAM_DEPTH_REG (0x004) |
| 34 | #define ETB_STATUS_REG (0x00C) |
| 35 | #define ETB_RAM_READ_DATA_REG (0x010) |
| 36 | #define ETB_RAM_READ_POINTER (0x014) |
| 37 | #define ETB_RAM_WRITE_POINTER (0x018) |
| 38 | #define ETB_TRG (0x01C) |
| 39 | #define ETB_CTL_REG (0x020) |
| 40 | #define ETB_RWD_REG (0x024) |
| 41 | #define ETB_FFSR (0x300) |
| 42 | #define ETB_FFCR (0x304) |
| 43 | #define ETB_ITMISCOP0 (0xEE0) |
| 44 | #define ETB_ITTRFLINACK (0xEE4) |
| 45 | #define ETB_ITTRFLIN (0xEE8) |
| 46 | #define ETB_ITATBDATA0 (0xEEC) |
| 47 | #define ETB_ITATBCTR2 (0xEF0) |
| 48 | #define ETB_ITATBCTR1 (0xEF4) |
| 49 | #define ETB_ITATBCTR0 (0xEF8) |
| 50 | |
| 51 | |
| 52 | #define BYTES_PER_WORD 4 |
| 53 | #define ETB_SIZE_WORDS 4096 |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 54 | #define FRAME_SIZE_WORDS 4 |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 55 | |
| 56 | #define ETB_LOCK() \ |
| 57 | do { \ |
| 58 | mb(); \ |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 59 | etb_writel(etb, 0x0, CS_LAR); \ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 60 | } while (0) |
| 61 | #define ETB_UNLOCK() \ |
| 62 | do { \ |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 63 | etb_writel(etb, CS_UNLOCK_MAGIC, CS_LAR); \ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 64 | mb(); \ |
| 65 | } while (0) |
| 66 | |
| 67 | struct etb_ctx { |
| 68 | uint8_t *buf; |
| 69 | void __iomem *base; |
| 70 | bool enabled; |
| 71 | bool reading; |
| 72 | struct mutex lock; |
| 73 | atomic_t in_use; |
| 74 | struct device *dev; |
| 75 | }; |
| 76 | |
| 77 | static struct etb_ctx etb; |
| 78 | |
| 79 | static void __etb_enable(void) |
| 80 | { |
| 81 | int i; |
| 82 | |
| 83 | ETB_UNLOCK(); |
| 84 | |
| 85 | etb_writel(etb, 0x0, ETB_RAM_WRITE_POINTER); |
| 86 | for (i = 0; i < ETB_SIZE_WORDS; i++) |
| 87 | etb_writel(etb, 0x0, ETB_RWD_REG); |
| 88 | |
| 89 | etb_writel(etb, 0x0, ETB_RAM_WRITE_POINTER); |
| 90 | etb_writel(etb, 0x0, ETB_RAM_READ_POINTER); |
| 91 | |
| 92 | etb_writel(etb, BIT(13) | BIT(0), ETB_FFCR); |
| 93 | etb_writel(etb, BIT(0), ETB_CTL_REG); |
| 94 | |
| 95 | ETB_LOCK(); |
| 96 | } |
| 97 | |
| 98 | void etb_enable(void) |
| 99 | { |
| 100 | mutex_lock(&etb.lock); |
| 101 | __etb_enable(); |
| 102 | etb.enabled = true; |
| 103 | dev_info(etb.dev, "etb enabled\n"); |
| 104 | mutex_unlock(&etb.lock); |
| 105 | } |
| 106 | |
| 107 | static void __etb_disable(void) |
| 108 | { |
| 109 | int count; |
| 110 | |
| 111 | ETB_UNLOCK(); |
| 112 | |
| 113 | etb_writel(etb, BIT(12) | BIT(13), ETB_FFCR); |
| 114 | etb_writel(etb, 0x0, ETB_CTL_REG); |
| 115 | |
| 116 | for (count = TIMEOUT_US; BVAL(etb_readl(etb, ETB_FFSR), 1) != 1 |
| 117 | && count > 0; count--) |
| 118 | udelay(1); |
| 119 | WARN(count == 0, "timeout while disabling etb\n"); |
| 120 | |
| 121 | ETB_LOCK(); |
| 122 | } |
| 123 | |
| 124 | void etb_disable(void) |
| 125 | { |
| 126 | mutex_lock(&etb.lock); |
| 127 | __etb_disable(); |
| 128 | etb.enabled = false; |
| 129 | dev_info(etb.dev, "etb disabled\n"); |
| 130 | mutex_unlock(&etb.lock); |
| 131 | } |
| 132 | |
| 133 | static void __etb_dump(void) |
| 134 | { |
| 135 | int i; |
| 136 | uint8_t *buf_ptr; |
| 137 | uint32_t read_data; |
| 138 | uint32_t read_ptr; |
| 139 | uint32_t write_ptr; |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 140 | uint32_t frame_off; |
| 141 | uint32_t frame_endoff; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 142 | |
| 143 | ETB_UNLOCK(); |
| 144 | |
| 145 | read_ptr = etb_readl(etb, ETB_RAM_READ_POINTER); |
| 146 | write_ptr = etb_readl(etb, ETB_RAM_WRITE_POINTER); |
| 147 | |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 148 | frame_off = write_ptr % FRAME_SIZE_WORDS; |
| 149 | frame_endoff = FRAME_SIZE_WORDS - frame_off; |
| 150 | if (frame_off) { |
| 151 | dev_err(etb.dev, "write_ptr: %lu not aligned to formatter " |
| 152 | "frame size\n", (unsigned long)write_ptr); |
| 153 | dev_err(etb.dev, "frameoff: %lu, frame_endoff: %lu\n", |
| 154 | (unsigned long)frame_off, (unsigned long)frame_endoff); |
| 155 | write_ptr += frame_endoff; |
| 156 | } |
| 157 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 158 | if ((etb_readl(etb, ETB_STATUS_REG) & BIT(0)) == 0) |
| 159 | etb_writel(etb, 0x0, ETB_RAM_READ_POINTER); |
| 160 | else |
| 161 | etb_writel(etb, write_ptr, ETB_RAM_READ_POINTER); |
| 162 | |
| 163 | buf_ptr = etb.buf; |
| 164 | for (i = 0; i < ETB_SIZE_WORDS; i++) { |
| 165 | read_data = etb_readl(etb, ETB_RAM_READ_DATA_REG); |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 166 | *buf_ptr++ = read_data >> 0; |
| 167 | *buf_ptr++ = read_data >> 8; |
| 168 | *buf_ptr++ = read_data >> 16; |
| 169 | *buf_ptr++ = read_data >> 24; |
| 170 | } |
| 171 | |
| 172 | if (frame_off) { |
| 173 | buf_ptr -= (frame_endoff * BYTES_PER_WORD); |
| 174 | for (i = 0; i < frame_endoff; i++) { |
| 175 | *buf_ptr++ = 0x0; |
| 176 | *buf_ptr++ = 0x0; |
| 177 | *buf_ptr++ = 0x0; |
| 178 | *buf_ptr++ = 0x0; |
| 179 | } |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | etb_writel(etb, read_ptr, ETB_RAM_READ_POINTER); |
| 183 | |
| 184 | ETB_LOCK(); |
| 185 | } |
| 186 | |
| 187 | void etb_dump(void) |
| 188 | { |
| 189 | mutex_lock(&etb.lock); |
| 190 | if (etb.enabled) { |
| 191 | __etb_disable(); |
| 192 | __etb_dump(); |
| 193 | __etb_enable(); |
| 194 | |
| 195 | dev_info(etb.dev, "etb dumped\n"); |
| 196 | } |
| 197 | mutex_unlock(&etb.lock); |
| 198 | } |
| 199 | |
| 200 | static int etb_open(struct inode *inode, struct file *file) |
| 201 | { |
| 202 | if (atomic_cmpxchg(&etb.in_use, 0, 1)) |
| 203 | return -EBUSY; |
| 204 | |
| 205 | dev_dbg(etb.dev, "%s: successfully opened\n", __func__); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static ssize_t etb_read(struct file *file, char __user *data, |
| 210 | size_t len, loff_t *ppos) |
| 211 | { |
| 212 | if (etb.reading == false) { |
| 213 | etb_dump(); |
| 214 | etb.reading = true; |
| 215 | } |
| 216 | |
| 217 | if (*ppos + len > ETB_SIZE_WORDS * BYTES_PER_WORD) |
| 218 | len = ETB_SIZE_WORDS * BYTES_PER_WORD - *ppos; |
| 219 | |
| 220 | if (copy_to_user(data, etb.buf + *ppos, len)) { |
| 221 | dev_dbg(etb.dev, "%s: copy_to_user failed\n", __func__); |
| 222 | return -EFAULT; |
| 223 | } |
| 224 | |
| 225 | *ppos += len; |
| 226 | |
| 227 | dev_dbg(etb.dev, "%s: %d bytes copied, %d bytes left\n", |
| 228 | __func__, len, (int) (ETB_SIZE_WORDS * BYTES_PER_WORD - *ppos)); |
| 229 | |
| 230 | return len; |
| 231 | } |
| 232 | |
| 233 | static int etb_release(struct inode *inode, struct file *file) |
| 234 | { |
| 235 | etb.reading = false; |
| 236 | |
| 237 | atomic_set(&etb.in_use, 0); |
| 238 | |
| 239 | dev_dbg(etb.dev, "%s: released\n", __func__); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static const struct file_operations etb_fops = { |
| 245 | .owner = THIS_MODULE, |
| 246 | .open = etb_open, |
| 247 | .read = etb_read, |
| 248 | .release = etb_release, |
| 249 | }; |
| 250 | |
| 251 | static struct miscdevice etb_misc = { |
| 252 | .name = "msm_etb", |
| 253 | .minor = MISC_DYNAMIC_MINOR, |
| 254 | .fops = &etb_fops, |
| 255 | }; |
| 256 | |
| 257 | static int __devinit etb_probe(struct platform_device *pdev) |
| 258 | { |
| 259 | int ret; |
| 260 | struct resource *res; |
| 261 | |
| 262 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 263 | if (!res) { |
| 264 | ret = -EINVAL; |
| 265 | goto err_res; |
| 266 | } |
| 267 | |
| 268 | etb.base = ioremap_nocache(res->start, resource_size(res)); |
| 269 | if (!etb.base) { |
| 270 | ret = -EINVAL; |
| 271 | goto err_ioremap; |
| 272 | } |
| 273 | |
| 274 | etb.dev = &pdev->dev; |
| 275 | |
| 276 | ret = misc_register(&etb_misc); |
| 277 | if (ret) |
| 278 | goto err_misc; |
| 279 | |
| 280 | etb.buf = kzalloc(ETB_SIZE_WORDS * BYTES_PER_WORD, GFP_KERNEL); |
| 281 | if (!etb.buf) { |
| 282 | ret = -ENOMEM; |
| 283 | goto err_alloc; |
| 284 | } |
| 285 | |
| 286 | mutex_init(&etb.lock); |
| 287 | |
| 288 | return 0; |
| 289 | |
| 290 | err_alloc: |
| 291 | misc_deregister(&etb_misc); |
| 292 | err_misc: |
| 293 | iounmap(etb.base); |
| 294 | err_ioremap: |
| 295 | err_res: |
| 296 | return ret; |
| 297 | } |
| 298 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 299 | static int etb_remove(struct platform_device *pdev) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 300 | { |
| 301 | if (etb.enabled) |
| 302 | etb_disable(); |
| 303 | mutex_destroy(&etb.lock); |
| 304 | kfree(etb.buf); |
| 305 | misc_deregister(&etb_misc); |
| 306 | iounmap(etb.base); |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static struct platform_driver etb_driver = { |
| 312 | .probe = etb_probe, |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 313 | .remove = etb_remove, |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 314 | .driver = { |
| 315 | .name = "msm_etb", |
| 316 | }, |
| 317 | }; |
| 318 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 319 | int __init etb_init(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 320 | { |
| 321 | return platform_driver_register(&etb_driver); |
| 322 | } |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 323 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 324 | void etb_exit(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 325 | { |
| 326 | platform_driver_unregister(&etb_driver); |
| 327 | } |