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; |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame^] | 72 | struct mutex mutex; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 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 | { |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame^] | 100 | mutex_lock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 101 | __etb_enable(); |
| 102 | etb.enabled = true; |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame^] | 103 | dev_info(etb.dev, "ETB enabled\n"); |
| 104 | mutex_unlock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 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); |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame^] | 119 | WARN(count == 0, "timeout while disabling ETB, ETB_FFSR: %#x\n", |
| 120 | etb_readl(etb, ETB_FFSR)); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 121 | |
| 122 | ETB_LOCK(); |
| 123 | } |
| 124 | |
| 125 | void etb_disable(void) |
| 126 | { |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame^] | 127 | mutex_lock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 128 | __etb_disable(); |
| 129 | etb.enabled = false; |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame^] | 130 | dev_info(etb.dev, "ETB disabled\n"); |
| 131 | mutex_unlock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void __etb_dump(void) |
| 135 | { |
| 136 | int i; |
| 137 | uint8_t *buf_ptr; |
| 138 | uint32_t read_data; |
| 139 | uint32_t read_ptr; |
| 140 | uint32_t write_ptr; |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 141 | uint32_t frame_off; |
| 142 | uint32_t frame_endoff; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 143 | |
| 144 | ETB_UNLOCK(); |
| 145 | |
| 146 | read_ptr = etb_readl(etb, ETB_RAM_READ_POINTER); |
| 147 | write_ptr = etb_readl(etb, ETB_RAM_WRITE_POINTER); |
| 148 | |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 149 | frame_off = write_ptr % FRAME_SIZE_WORDS; |
| 150 | frame_endoff = FRAME_SIZE_WORDS - frame_off; |
| 151 | if (frame_off) { |
| 152 | dev_err(etb.dev, "write_ptr: %lu not aligned to formatter " |
| 153 | "frame size\n", (unsigned long)write_ptr); |
| 154 | dev_err(etb.dev, "frameoff: %lu, frame_endoff: %lu\n", |
| 155 | (unsigned long)frame_off, (unsigned long)frame_endoff); |
| 156 | write_ptr += frame_endoff; |
| 157 | } |
| 158 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 159 | if ((etb_readl(etb, ETB_STATUS_REG) & BIT(0)) == 0) |
| 160 | etb_writel(etb, 0x0, ETB_RAM_READ_POINTER); |
| 161 | else |
| 162 | etb_writel(etb, write_ptr, ETB_RAM_READ_POINTER); |
| 163 | |
| 164 | buf_ptr = etb.buf; |
| 165 | for (i = 0; i < ETB_SIZE_WORDS; i++) { |
| 166 | read_data = etb_readl(etb, ETB_RAM_READ_DATA_REG); |
Pratik Patel | 70f5e39 | 2011-09-24 21:23:27 -0700 | [diff] [blame] | 167 | *buf_ptr++ = read_data >> 0; |
| 168 | *buf_ptr++ = read_data >> 8; |
| 169 | *buf_ptr++ = read_data >> 16; |
| 170 | *buf_ptr++ = read_data >> 24; |
| 171 | } |
| 172 | |
| 173 | if (frame_off) { |
| 174 | buf_ptr -= (frame_endoff * BYTES_PER_WORD); |
| 175 | for (i = 0; i < frame_endoff; i++) { |
| 176 | *buf_ptr++ = 0x0; |
| 177 | *buf_ptr++ = 0x0; |
| 178 | *buf_ptr++ = 0x0; |
| 179 | *buf_ptr++ = 0x0; |
| 180 | } |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | etb_writel(etb, read_ptr, ETB_RAM_READ_POINTER); |
| 184 | |
| 185 | ETB_LOCK(); |
| 186 | } |
| 187 | |
| 188 | void etb_dump(void) |
| 189 | { |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame^] | 190 | mutex_lock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 191 | if (etb.enabled) { |
| 192 | __etb_disable(); |
| 193 | __etb_dump(); |
| 194 | __etb_enable(); |
| 195 | |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame^] | 196 | dev_info(etb.dev, "ETB dumped\n"); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 197 | } |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame^] | 198 | mutex_unlock(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static int etb_open(struct inode *inode, struct file *file) |
| 202 | { |
| 203 | if (atomic_cmpxchg(&etb.in_use, 0, 1)) |
| 204 | return -EBUSY; |
| 205 | |
| 206 | dev_dbg(etb.dev, "%s: successfully opened\n", __func__); |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static ssize_t etb_read(struct file *file, char __user *data, |
| 211 | size_t len, loff_t *ppos) |
| 212 | { |
| 213 | if (etb.reading == false) { |
| 214 | etb_dump(); |
| 215 | etb.reading = true; |
| 216 | } |
| 217 | |
| 218 | if (*ppos + len > ETB_SIZE_WORDS * BYTES_PER_WORD) |
| 219 | len = ETB_SIZE_WORDS * BYTES_PER_WORD - *ppos; |
| 220 | |
| 221 | if (copy_to_user(data, etb.buf + *ppos, len)) { |
| 222 | dev_dbg(etb.dev, "%s: copy_to_user failed\n", __func__); |
| 223 | return -EFAULT; |
| 224 | } |
| 225 | |
| 226 | *ppos += len; |
| 227 | |
| 228 | dev_dbg(etb.dev, "%s: %d bytes copied, %d bytes left\n", |
| 229 | __func__, len, (int) (ETB_SIZE_WORDS * BYTES_PER_WORD - *ppos)); |
| 230 | |
| 231 | return len; |
| 232 | } |
| 233 | |
| 234 | static int etb_release(struct inode *inode, struct file *file) |
| 235 | { |
| 236 | etb.reading = false; |
| 237 | |
| 238 | atomic_set(&etb.in_use, 0); |
| 239 | |
| 240 | dev_dbg(etb.dev, "%s: released\n", __func__); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static const struct file_operations etb_fops = { |
| 246 | .owner = THIS_MODULE, |
| 247 | .open = etb_open, |
| 248 | .read = etb_read, |
| 249 | .release = etb_release, |
| 250 | }; |
| 251 | |
| 252 | static struct miscdevice etb_misc = { |
| 253 | .name = "msm_etb", |
| 254 | .minor = MISC_DYNAMIC_MINOR, |
| 255 | .fops = &etb_fops, |
| 256 | }; |
| 257 | |
| 258 | static int __devinit etb_probe(struct platform_device *pdev) |
| 259 | { |
| 260 | int ret; |
| 261 | struct resource *res; |
| 262 | |
| 263 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 264 | if (!res) { |
| 265 | ret = -EINVAL; |
| 266 | goto err_res; |
| 267 | } |
| 268 | |
| 269 | etb.base = ioremap_nocache(res->start, resource_size(res)); |
| 270 | if (!etb.base) { |
| 271 | ret = -EINVAL; |
| 272 | goto err_ioremap; |
| 273 | } |
| 274 | |
| 275 | etb.dev = &pdev->dev; |
| 276 | |
| 277 | ret = misc_register(&etb_misc); |
| 278 | if (ret) |
| 279 | goto err_misc; |
| 280 | |
| 281 | etb.buf = kzalloc(ETB_SIZE_WORDS * BYTES_PER_WORD, GFP_KERNEL); |
| 282 | if (!etb.buf) { |
| 283 | ret = -ENOMEM; |
| 284 | goto err_alloc; |
| 285 | } |
| 286 | |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame^] | 287 | mutex_init(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 288 | |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame^] | 289 | dev_info(etb.dev, "ETB initialized\n"); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 290 | return 0; |
| 291 | |
| 292 | err_alloc: |
| 293 | misc_deregister(&etb_misc); |
| 294 | err_misc: |
| 295 | iounmap(etb.base); |
| 296 | err_ioremap: |
| 297 | err_res: |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame^] | 298 | dev_err(etb.dev, "ETB init failed\n"); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 299 | return ret; |
| 300 | } |
| 301 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 302 | static int etb_remove(struct platform_device *pdev) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 303 | { |
| 304 | if (etb.enabled) |
| 305 | etb_disable(); |
Pratik Patel | 61de730 | 2012-03-07 12:06:10 -0800 | [diff] [blame^] | 306 | mutex_destroy(&etb.mutex); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 307 | kfree(etb.buf); |
| 308 | misc_deregister(&etb_misc); |
| 309 | iounmap(etb.base); |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static struct platform_driver etb_driver = { |
| 315 | .probe = etb_probe, |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 316 | .remove = etb_remove, |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 317 | .driver = { |
| 318 | .name = "msm_etb", |
| 319 | }, |
| 320 | }; |
| 321 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 322 | int __init etb_init(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 323 | { |
| 324 | return platform_driver_register(&etb_driver); |
| 325 | } |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 326 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 327 | void etb_exit(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 328 | { |
| 329 | platform_driver_unregister(&etb_driver); |
| 330 | } |