blob: 8b5f8dbe3e5640cef36692719b5ee329ad17d027 [file] [log] [blame]
Pratik Patel17f3b822011-11-21 12:41:47 -08001/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Pratik Patel7831c082011-06-08 21:44:37 -07002 *
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 Patelcf418622011-09-22 11:15:11 -070015#include <linux/init.h>
16#include <linux/types.h>
17#include <linux/device.h>
Pratik Patel7831c082011-06-08 21:44:37 -070018#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 Patelcf418622011-09-22 11:15:11 -070026#include <linux/mutex.h>
Pratik Patel7831c082011-06-08 21:44:37 -070027
Pratik Patelb27a9352012-03-17 22:37:21 -070028#include "qdss-priv.h"
Pratik Patel7831c082011-06-08 21:44:37 -070029
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 Patel70f5e392011-09-24 21:23:27 -070054#define FRAME_SIZE_WORDS 4
Pratik Patel7831c082011-06-08 21:44:37 -070055
56#define ETB_LOCK() \
57do { \
58 mb(); \
Pratik Patel17f3b822011-11-21 12:41:47 -080059 etb_writel(etb, 0x0, CS_LAR); \
Pratik Patel7831c082011-06-08 21:44:37 -070060} while (0)
61#define ETB_UNLOCK() \
62do { \
Pratik Patel17f3b822011-11-21 12:41:47 -080063 etb_writel(etb, CS_UNLOCK_MAGIC, CS_LAR); \
Pratik Patel7831c082011-06-08 21:44:37 -070064 mb(); \
65} while (0)
66
67struct etb_ctx {
68 uint8_t *buf;
69 void __iomem *base;
70 bool enabled;
71 bool reading;
Pratik Patel61de7302012-03-07 12:06:10 -080072 struct mutex mutex;
Pratik Patel7831c082011-06-08 21:44:37 -070073 atomic_t in_use;
74 struct device *dev;
Pratik Patel6630ebe2012-03-06 16:44:22 -080075 struct kobject *kobj;
76 uint32_t trigger_cntr;
Pratik Patel7831c082011-06-08 21:44:37 -070077};
78
79static struct etb_ctx etb;
80
81static void __etb_enable(void)
82{
83 int i;
84
85 ETB_UNLOCK();
86
87 etb_writel(etb, 0x0, ETB_RAM_WRITE_POINTER);
88 for (i = 0; i < ETB_SIZE_WORDS; i++)
89 etb_writel(etb, 0x0, ETB_RWD_REG);
90
91 etb_writel(etb, 0x0, ETB_RAM_WRITE_POINTER);
92 etb_writel(etb, 0x0, ETB_RAM_READ_POINTER);
93
Pratik Patel6630ebe2012-03-06 16:44:22 -080094 etb_writel(etb, etb.trigger_cntr, ETB_TRG);
Pratik Patel7831c082011-06-08 21:44:37 -070095 etb_writel(etb, BIT(13) | BIT(0), ETB_FFCR);
96 etb_writel(etb, BIT(0), ETB_CTL_REG);
97
98 ETB_LOCK();
99}
100
101void etb_enable(void)
102{
Pratik Patel61de7302012-03-07 12:06:10 -0800103 mutex_lock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700104 __etb_enable();
105 etb.enabled = true;
Pratik Patel61de7302012-03-07 12:06:10 -0800106 dev_info(etb.dev, "ETB enabled\n");
107 mutex_unlock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700108}
109
110static void __etb_disable(void)
111{
112 int count;
Pratik Patel6a32d262012-01-20 15:21:31 -0800113 uint32_t ffcr;
Pratik Patel7831c082011-06-08 21:44:37 -0700114
115 ETB_UNLOCK();
116
Pratik Patel6a32d262012-01-20 15:21:31 -0800117 ffcr = etb_readl(etb, ETB_FFCR);
118 ffcr |= (BIT(12) | BIT(6));
119 etb_writel(etb, ffcr, ETB_FFCR);
120
121 for (count = TIMEOUT_US; BVAL(etb_readl(etb, ETB_FFCR), 6) != 0
122 && count > 0; count--)
123 udelay(1);
124 WARN(count == 0, "timeout while flushing ETB, ETB_FFCR: %#x\n",
125 etb_readl(etb, ETB_FFCR));
126
Pratik Patel7831c082011-06-08 21:44:37 -0700127 etb_writel(etb, 0x0, ETB_CTL_REG);
128
129 for (count = TIMEOUT_US; BVAL(etb_readl(etb, ETB_FFSR), 1) != 1
130 && count > 0; count--)
131 udelay(1);
Pratik Patel61de7302012-03-07 12:06:10 -0800132 WARN(count == 0, "timeout while disabling ETB, ETB_FFSR: %#x\n",
133 etb_readl(etb, ETB_FFSR));
Pratik Patel7831c082011-06-08 21:44:37 -0700134
135 ETB_LOCK();
136}
137
138void etb_disable(void)
139{
Pratik Patel61de7302012-03-07 12:06:10 -0800140 mutex_lock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700141 __etb_disable();
142 etb.enabled = false;
Pratik Patel61de7302012-03-07 12:06:10 -0800143 dev_info(etb.dev, "ETB disabled\n");
144 mutex_unlock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700145}
146
147static void __etb_dump(void)
148{
149 int i;
150 uint8_t *buf_ptr;
151 uint32_t read_data;
152 uint32_t read_ptr;
153 uint32_t write_ptr;
Pratik Patel70f5e392011-09-24 21:23:27 -0700154 uint32_t frame_off;
155 uint32_t frame_endoff;
Pratik Patel7831c082011-06-08 21:44:37 -0700156
157 ETB_UNLOCK();
158
159 read_ptr = etb_readl(etb, ETB_RAM_READ_POINTER);
160 write_ptr = etb_readl(etb, ETB_RAM_WRITE_POINTER);
161
Pratik Patel70f5e392011-09-24 21:23:27 -0700162 frame_off = write_ptr % FRAME_SIZE_WORDS;
163 frame_endoff = FRAME_SIZE_WORDS - frame_off;
164 if (frame_off) {
165 dev_err(etb.dev, "write_ptr: %lu not aligned to formatter "
166 "frame size\n", (unsigned long)write_ptr);
167 dev_err(etb.dev, "frameoff: %lu, frame_endoff: %lu\n",
168 (unsigned long)frame_off, (unsigned long)frame_endoff);
169 write_ptr += frame_endoff;
170 }
171
Pratik Patel7831c082011-06-08 21:44:37 -0700172 if ((etb_readl(etb, ETB_STATUS_REG) & BIT(0)) == 0)
173 etb_writel(etb, 0x0, ETB_RAM_READ_POINTER);
174 else
175 etb_writel(etb, write_ptr, ETB_RAM_READ_POINTER);
176
177 buf_ptr = etb.buf;
178 for (i = 0; i < ETB_SIZE_WORDS; i++) {
179 read_data = etb_readl(etb, ETB_RAM_READ_DATA_REG);
Pratik Patel70f5e392011-09-24 21:23:27 -0700180 *buf_ptr++ = read_data >> 0;
181 *buf_ptr++ = read_data >> 8;
182 *buf_ptr++ = read_data >> 16;
183 *buf_ptr++ = read_data >> 24;
184 }
185
186 if (frame_off) {
187 buf_ptr -= (frame_endoff * BYTES_PER_WORD);
188 for (i = 0; i < frame_endoff; i++) {
189 *buf_ptr++ = 0x0;
190 *buf_ptr++ = 0x0;
191 *buf_ptr++ = 0x0;
192 *buf_ptr++ = 0x0;
193 }
Pratik Patel7831c082011-06-08 21:44:37 -0700194 }
195
196 etb_writel(etb, read_ptr, ETB_RAM_READ_POINTER);
197
198 ETB_LOCK();
199}
200
201void etb_dump(void)
202{
Pratik Patel61de7302012-03-07 12:06:10 -0800203 mutex_lock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700204 if (etb.enabled) {
205 __etb_disable();
206 __etb_dump();
207 __etb_enable();
208
Pratik Patel61de7302012-03-07 12:06:10 -0800209 dev_info(etb.dev, "ETB dumped\n");
Pratik Patel7831c082011-06-08 21:44:37 -0700210 }
Pratik Patel61de7302012-03-07 12:06:10 -0800211 mutex_unlock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700212}
213
214static int etb_open(struct inode *inode, struct file *file)
215{
216 if (atomic_cmpxchg(&etb.in_use, 0, 1))
217 return -EBUSY;
218
219 dev_dbg(etb.dev, "%s: successfully opened\n", __func__);
220 return 0;
221}
222
223static ssize_t etb_read(struct file *file, char __user *data,
224 size_t len, loff_t *ppos)
225{
226 if (etb.reading == false) {
227 etb_dump();
228 etb.reading = true;
229 }
230
231 if (*ppos + len > ETB_SIZE_WORDS * BYTES_PER_WORD)
232 len = ETB_SIZE_WORDS * BYTES_PER_WORD - *ppos;
233
234 if (copy_to_user(data, etb.buf + *ppos, len)) {
235 dev_dbg(etb.dev, "%s: copy_to_user failed\n", __func__);
236 return -EFAULT;
237 }
238
239 *ppos += len;
240
241 dev_dbg(etb.dev, "%s: %d bytes copied, %d bytes left\n",
242 __func__, len, (int) (ETB_SIZE_WORDS * BYTES_PER_WORD - *ppos));
243
244 return len;
245}
246
247static int etb_release(struct inode *inode, struct file *file)
248{
249 etb.reading = false;
250
251 atomic_set(&etb.in_use, 0);
252
253 dev_dbg(etb.dev, "%s: released\n", __func__);
254
255 return 0;
256}
257
258static const struct file_operations etb_fops = {
259 .owner = THIS_MODULE,
260 .open = etb_open,
261 .read = etb_read,
262 .release = etb_release,
263};
264
265static struct miscdevice etb_misc = {
266 .name = "msm_etb",
267 .minor = MISC_DYNAMIC_MINOR,
268 .fops = &etb_fops,
269};
270
Pratik Patel6630ebe2012-03-06 16:44:22 -0800271#define ETB_ATTR(__name) \
272static struct kobj_attribute __name##_attr = \
273 __ATTR(__name, S_IRUGO | S_IWUSR, __name##_show, __name##_store)
274
275static ssize_t trigger_cntr_store(struct kobject *kobj,
276 struct kobj_attribute *attr,
277 const char *buf, size_t n)
278{
279 unsigned long val;
280
281 if (sscanf(buf, "%lx", &val) != 1)
282 return -EINVAL;
283
284 etb.trigger_cntr = val;
285 return n;
286}
287static ssize_t trigger_cntr_show(struct kobject *kobj,
288 struct kobj_attribute *attr,
289 char *buf)
290{
291 unsigned long val = etb.trigger_cntr;
292 return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
293}
294ETB_ATTR(trigger_cntr);
295
296static int __init etb_sysfs_init(void)
297{
298 int ret;
299
300 etb.kobj = kobject_create_and_add("etb", qdss_get_modulekobj());
301 if (!etb.kobj) {
302 dev_err(etb.dev, "failed to create ETB sysfs kobject\n");
303 ret = -ENOMEM;
304 goto err_create;
305 }
306
307 ret = sysfs_create_file(etb.kobj, &trigger_cntr_attr.attr);
308 if (ret) {
309 dev_err(etb.dev, "failed to create ETB sysfs trigger_cntr"
310 " attribute\n");
311 goto err_file;
312 }
313
314 return 0;
315err_file:
316 kobject_put(etb.kobj);
317err_create:
318 return ret;
319}
320
321static void etb_sysfs_exit(void)
322{
323 sysfs_remove_file(etb.kobj, &trigger_cntr_attr.attr);
324 kobject_put(etb.kobj);
325}
326
Pratik Patel7831c082011-06-08 21:44:37 -0700327static int __devinit etb_probe(struct platform_device *pdev)
328{
329 int ret;
330 struct resource *res;
331
332 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
333 if (!res) {
334 ret = -EINVAL;
335 goto err_res;
336 }
337
338 etb.base = ioremap_nocache(res->start, resource_size(res));
339 if (!etb.base) {
340 ret = -EINVAL;
341 goto err_ioremap;
342 }
343
344 etb.dev = &pdev->dev;
345
Pratik Patel9dc70222012-03-07 12:09:13 -0800346 mutex_init(&etb.mutex);
347
Pratik Patel7831c082011-06-08 21:44:37 -0700348 ret = misc_register(&etb_misc);
349 if (ret)
350 goto err_misc;
351
352 etb.buf = kzalloc(ETB_SIZE_WORDS * BYTES_PER_WORD, GFP_KERNEL);
353 if (!etb.buf) {
354 ret = -ENOMEM;
355 goto err_alloc;
356 }
357
Pratik Patel6630ebe2012-03-06 16:44:22 -0800358 etb_sysfs_init();
359
Pratik Patel61de7302012-03-07 12:06:10 -0800360 dev_info(etb.dev, "ETB initialized\n");
Pratik Patel7831c082011-06-08 21:44:37 -0700361 return 0;
362
363err_alloc:
364 misc_deregister(&etb_misc);
365err_misc:
Pratik Patel9dc70222012-03-07 12:09:13 -0800366 mutex_destroy(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700367 iounmap(etb.base);
368err_ioremap:
369err_res:
Pratik Patel61de7302012-03-07 12:06:10 -0800370 dev_err(etb.dev, "ETB init failed\n");
Pratik Patel7831c082011-06-08 21:44:37 -0700371 return ret;
372}
373
Pratik Patel59e29942011-12-27 10:31:33 -0800374static int etb_remove(struct platform_device *pdev)
Pratik Patel7831c082011-06-08 21:44:37 -0700375{
376 if (etb.enabled)
377 etb_disable();
Pratik Patel6630ebe2012-03-06 16:44:22 -0800378 etb_sysfs_exit();
Pratik Patel7831c082011-06-08 21:44:37 -0700379 kfree(etb.buf);
380 misc_deregister(&etb_misc);
Pratik Patel9dc70222012-03-07 12:09:13 -0800381 mutex_destroy(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700382 iounmap(etb.base);
383
384 return 0;
385}
386
387static struct platform_driver etb_driver = {
388 .probe = etb_probe,
Pratik Patel59e29942011-12-27 10:31:33 -0800389 .remove = etb_remove,
Pratik Patel7831c082011-06-08 21:44:37 -0700390 .driver = {
391 .name = "msm_etb",
392 },
393};
394
Pratik Patel59e29942011-12-27 10:31:33 -0800395int __init etb_init(void)
Pratik Patel7831c082011-06-08 21:44:37 -0700396{
397 return platform_driver_register(&etb_driver);
398}
Pratik Patel7831c082011-06-08 21:44:37 -0700399
Pratik Patel59e29942011-12-27 10:31:33 -0800400void etb_exit(void)
Pratik Patel7831c082011-06-08 21:44:37 -0700401{
402 platform_driver_unregister(&etb_driver);
403}