blob: c58c93ea20887c3e3ee6a5e739d2b2adb0949aed [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
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 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;
113
114 ETB_UNLOCK();
115
116 etb_writel(etb, BIT(12) | BIT(13), ETB_FFCR);
117 etb_writel(etb, 0x0, ETB_CTL_REG);
118
119 for (count = TIMEOUT_US; BVAL(etb_readl(etb, ETB_FFSR), 1) != 1
120 && count > 0; count--)
121 udelay(1);
Pratik Patel61de7302012-03-07 12:06:10 -0800122 WARN(count == 0, "timeout while disabling ETB, ETB_FFSR: %#x\n",
123 etb_readl(etb, ETB_FFSR));
Pratik Patel7831c082011-06-08 21:44:37 -0700124
125 ETB_LOCK();
126}
127
128void etb_disable(void)
129{
Pratik Patel61de7302012-03-07 12:06:10 -0800130 mutex_lock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700131 __etb_disable();
132 etb.enabled = false;
Pratik Patel61de7302012-03-07 12:06:10 -0800133 dev_info(etb.dev, "ETB disabled\n");
134 mutex_unlock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700135}
136
137static void __etb_dump(void)
138{
139 int i;
140 uint8_t *buf_ptr;
141 uint32_t read_data;
142 uint32_t read_ptr;
143 uint32_t write_ptr;
Pratik Patel70f5e392011-09-24 21:23:27 -0700144 uint32_t frame_off;
145 uint32_t frame_endoff;
Pratik Patel7831c082011-06-08 21:44:37 -0700146
147 ETB_UNLOCK();
148
149 read_ptr = etb_readl(etb, ETB_RAM_READ_POINTER);
150 write_ptr = etb_readl(etb, ETB_RAM_WRITE_POINTER);
151
Pratik Patel70f5e392011-09-24 21:23:27 -0700152 frame_off = write_ptr % FRAME_SIZE_WORDS;
153 frame_endoff = FRAME_SIZE_WORDS - frame_off;
154 if (frame_off) {
155 dev_err(etb.dev, "write_ptr: %lu not aligned to formatter "
156 "frame size\n", (unsigned long)write_ptr);
157 dev_err(etb.dev, "frameoff: %lu, frame_endoff: %lu\n",
158 (unsigned long)frame_off, (unsigned long)frame_endoff);
159 write_ptr += frame_endoff;
160 }
161
Pratik Patel7831c082011-06-08 21:44:37 -0700162 if ((etb_readl(etb, ETB_STATUS_REG) & BIT(0)) == 0)
163 etb_writel(etb, 0x0, ETB_RAM_READ_POINTER);
164 else
165 etb_writel(etb, write_ptr, ETB_RAM_READ_POINTER);
166
167 buf_ptr = etb.buf;
168 for (i = 0; i < ETB_SIZE_WORDS; i++) {
169 read_data = etb_readl(etb, ETB_RAM_READ_DATA_REG);
Pratik Patel70f5e392011-09-24 21:23:27 -0700170 *buf_ptr++ = read_data >> 0;
171 *buf_ptr++ = read_data >> 8;
172 *buf_ptr++ = read_data >> 16;
173 *buf_ptr++ = read_data >> 24;
174 }
175
176 if (frame_off) {
177 buf_ptr -= (frame_endoff * BYTES_PER_WORD);
178 for (i = 0; i < frame_endoff; i++) {
179 *buf_ptr++ = 0x0;
180 *buf_ptr++ = 0x0;
181 *buf_ptr++ = 0x0;
182 *buf_ptr++ = 0x0;
183 }
Pratik Patel7831c082011-06-08 21:44:37 -0700184 }
185
186 etb_writel(etb, read_ptr, ETB_RAM_READ_POINTER);
187
188 ETB_LOCK();
189}
190
191void etb_dump(void)
192{
Pratik Patel61de7302012-03-07 12:06:10 -0800193 mutex_lock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700194 if (etb.enabled) {
195 __etb_disable();
196 __etb_dump();
197 __etb_enable();
198
Pratik Patel61de7302012-03-07 12:06:10 -0800199 dev_info(etb.dev, "ETB dumped\n");
Pratik Patel7831c082011-06-08 21:44:37 -0700200 }
Pratik Patel61de7302012-03-07 12:06:10 -0800201 mutex_unlock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700202}
203
204static int etb_open(struct inode *inode, struct file *file)
205{
206 if (atomic_cmpxchg(&etb.in_use, 0, 1))
207 return -EBUSY;
208
209 dev_dbg(etb.dev, "%s: successfully opened\n", __func__);
210 return 0;
211}
212
213static ssize_t etb_read(struct file *file, char __user *data,
214 size_t len, loff_t *ppos)
215{
216 if (etb.reading == false) {
217 etb_dump();
218 etb.reading = true;
219 }
220
221 if (*ppos + len > ETB_SIZE_WORDS * BYTES_PER_WORD)
222 len = ETB_SIZE_WORDS * BYTES_PER_WORD - *ppos;
223
224 if (copy_to_user(data, etb.buf + *ppos, len)) {
225 dev_dbg(etb.dev, "%s: copy_to_user failed\n", __func__);
226 return -EFAULT;
227 }
228
229 *ppos += len;
230
231 dev_dbg(etb.dev, "%s: %d bytes copied, %d bytes left\n",
232 __func__, len, (int) (ETB_SIZE_WORDS * BYTES_PER_WORD - *ppos));
233
234 return len;
235}
236
237static int etb_release(struct inode *inode, struct file *file)
238{
239 etb.reading = false;
240
241 atomic_set(&etb.in_use, 0);
242
243 dev_dbg(etb.dev, "%s: released\n", __func__);
244
245 return 0;
246}
247
248static const struct file_operations etb_fops = {
249 .owner = THIS_MODULE,
250 .open = etb_open,
251 .read = etb_read,
252 .release = etb_release,
253};
254
255static struct miscdevice etb_misc = {
256 .name = "msm_etb",
257 .minor = MISC_DYNAMIC_MINOR,
258 .fops = &etb_fops,
259};
260
Pratik Patel6630ebe2012-03-06 16:44:22 -0800261#define ETB_ATTR(__name) \
262static struct kobj_attribute __name##_attr = \
263 __ATTR(__name, S_IRUGO | S_IWUSR, __name##_show, __name##_store)
264
265static ssize_t trigger_cntr_store(struct kobject *kobj,
266 struct kobj_attribute *attr,
267 const char *buf, size_t n)
268{
269 unsigned long val;
270
271 if (sscanf(buf, "%lx", &val) != 1)
272 return -EINVAL;
273
274 etb.trigger_cntr = val;
275 return n;
276}
277static ssize_t trigger_cntr_show(struct kobject *kobj,
278 struct kobj_attribute *attr,
279 char *buf)
280{
281 unsigned long val = etb.trigger_cntr;
282 return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
283}
284ETB_ATTR(trigger_cntr);
285
286static int __init etb_sysfs_init(void)
287{
288 int ret;
289
290 etb.kobj = kobject_create_and_add("etb", qdss_get_modulekobj());
291 if (!etb.kobj) {
292 dev_err(etb.dev, "failed to create ETB sysfs kobject\n");
293 ret = -ENOMEM;
294 goto err_create;
295 }
296
297 ret = sysfs_create_file(etb.kobj, &trigger_cntr_attr.attr);
298 if (ret) {
299 dev_err(etb.dev, "failed to create ETB sysfs trigger_cntr"
300 " attribute\n");
301 goto err_file;
302 }
303
304 return 0;
305err_file:
306 kobject_put(etb.kobj);
307err_create:
308 return ret;
309}
310
311static void etb_sysfs_exit(void)
312{
313 sysfs_remove_file(etb.kobj, &trigger_cntr_attr.attr);
314 kobject_put(etb.kobj);
315}
316
Pratik Patel7831c082011-06-08 21:44:37 -0700317static int __devinit etb_probe(struct platform_device *pdev)
318{
319 int ret;
320 struct resource *res;
321
322 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
323 if (!res) {
324 ret = -EINVAL;
325 goto err_res;
326 }
327
328 etb.base = ioremap_nocache(res->start, resource_size(res));
329 if (!etb.base) {
330 ret = -EINVAL;
331 goto err_ioremap;
332 }
333
334 etb.dev = &pdev->dev;
335
Pratik Patel9dc70222012-03-07 12:09:13 -0800336 mutex_init(&etb.mutex);
337
Pratik Patel7831c082011-06-08 21:44:37 -0700338 ret = misc_register(&etb_misc);
339 if (ret)
340 goto err_misc;
341
342 etb.buf = kzalloc(ETB_SIZE_WORDS * BYTES_PER_WORD, GFP_KERNEL);
343 if (!etb.buf) {
344 ret = -ENOMEM;
345 goto err_alloc;
346 }
347
Pratik Patel6630ebe2012-03-06 16:44:22 -0800348 etb_sysfs_init();
349
Pratik Patel61de7302012-03-07 12:06:10 -0800350 dev_info(etb.dev, "ETB initialized\n");
Pratik Patel7831c082011-06-08 21:44:37 -0700351 return 0;
352
353err_alloc:
354 misc_deregister(&etb_misc);
355err_misc:
Pratik Patel9dc70222012-03-07 12:09:13 -0800356 mutex_destroy(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700357 iounmap(etb.base);
358err_ioremap:
359err_res:
Pratik Patel61de7302012-03-07 12:06:10 -0800360 dev_err(etb.dev, "ETB init failed\n");
Pratik Patel7831c082011-06-08 21:44:37 -0700361 return ret;
362}
363
Pratik Patel59e29942011-12-27 10:31:33 -0800364static int etb_remove(struct platform_device *pdev)
Pratik Patel7831c082011-06-08 21:44:37 -0700365{
366 if (etb.enabled)
367 etb_disable();
Pratik Patel6630ebe2012-03-06 16:44:22 -0800368 etb_sysfs_exit();
Pratik Patel7831c082011-06-08 21:44:37 -0700369 kfree(etb.buf);
370 misc_deregister(&etb_misc);
Pratik Patel9dc70222012-03-07 12:09:13 -0800371 mutex_destroy(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700372 iounmap(etb.base);
373
374 return 0;
375}
376
377static struct platform_driver etb_driver = {
378 .probe = etb_probe,
Pratik Patel59e29942011-12-27 10:31:33 -0800379 .remove = etb_remove,
Pratik Patel7831c082011-06-08 21:44:37 -0700380 .driver = {
381 .name = "msm_etb",
382 },
383};
384
Pratik Patel59e29942011-12-27 10:31:33 -0800385int __init etb_init(void)
Pratik Patel7831c082011-06-08 21:44:37 -0700386{
387 return platform_driver_register(&etb_driver);
388}
Pratik Patel7831c082011-06-08 21:44:37 -0700389
Pratik Patel59e29942011-12-27 10:31:33 -0800390void etb_exit(void)
Pratik Patel7831c082011-06-08 21:44:37 -0700391{
392 platform_driver_unregister(&etb_driver);
393}