blob: 4efc2d1da0228a777ebf648fb6f188729d22e31a [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;
75};
76
77static struct etb_ctx etb;
78
79static 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
98void etb_enable(void)
99{
Pratik Patel61de7302012-03-07 12:06:10 -0800100 mutex_lock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700101 __etb_enable();
102 etb.enabled = true;
Pratik Patel61de7302012-03-07 12:06:10 -0800103 dev_info(etb.dev, "ETB enabled\n");
104 mutex_unlock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700105}
106
107static 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 Patel61de7302012-03-07 12:06:10 -0800119 WARN(count == 0, "timeout while disabling ETB, ETB_FFSR: %#x\n",
120 etb_readl(etb, ETB_FFSR));
Pratik Patel7831c082011-06-08 21:44:37 -0700121
122 ETB_LOCK();
123}
124
125void etb_disable(void)
126{
Pratik Patel61de7302012-03-07 12:06:10 -0800127 mutex_lock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700128 __etb_disable();
129 etb.enabled = false;
Pratik Patel61de7302012-03-07 12:06:10 -0800130 dev_info(etb.dev, "ETB disabled\n");
131 mutex_unlock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700132}
133
134static 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 Patel70f5e392011-09-24 21:23:27 -0700141 uint32_t frame_off;
142 uint32_t frame_endoff;
Pratik Patel7831c082011-06-08 21:44:37 -0700143
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 Patel70f5e392011-09-24 21:23:27 -0700149 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 Patel7831c082011-06-08 21:44:37 -0700159 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 Patel70f5e392011-09-24 21:23:27 -0700167 *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 Patel7831c082011-06-08 21:44:37 -0700181 }
182
183 etb_writel(etb, read_ptr, ETB_RAM_READ_POINTER);
184
185 ETB_LOCK();
186}
187
188void etb_dump(void)
189{
Pratik Patel61de7302012-03-07 12:06:10 -0800190 mutex_lock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700191 if (etb.enabled) {
192 __etb_disable();
193 __etb_dump();
194 __etb_enable();
195
Pratik Patel61de7302012-03-07 12:06:10 -0800196 dev_info(etb.dev, "ETB dumped\n");
Pratik Patel7831c082011-06-08 21:44:37 -0700197 }
Pratik Patel61de7302012-03-07 12:06:10 -0800198 mutex_unlock(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700199}
200
201static 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
210static 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
234static 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
245static const struct file_operations etb_fops = {
246 .owner = THIS_MODULE,
247 .open = etb_open,
248 .read = etb_read,
249 .release = etb_release,
250};
251
252static struct miscdevice etb_misc = {
253 .name = "msm_etb",
254 .minor = MISC_DYNAMIC_MINOR,
255 .fops = &etb_fops,
256};
257
258static 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 Patel61de7302012-03-07 12:06:10 -0800287 mutex_init(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700288
Pratik Patel61de7302012-03-07 12:06:10 -0800289 dev_info(etb.dev, "ETB initialized\n");
Pratik Patel7831c082011-06-08 21:44:37 -0700290 return 0;
291
292err_alloc:
293 misc_deregister(&etb_misc);
294err_misc:
295 iounmap(etb.base);
296err_ioremap:
297err_res:
Pratik Patel61de7302012-03-07 12:06:10 -0800298 dev_err(etb.dev, "ETB init failed\n");
Pratik Patel7831c082011-06-08 21:44:37 -0700299 return ret;
300}
301
Pratik Patel59e29942011-12-27 10:31:33 -0800302static int etb_remove(struct platform_device *pdev)
Pratik Patel7831c082011-06-08 21:44:37 -0700303{
304 if (etb.enabled)
305 etb_disable();
Pratik Patel61de7302012-03-07 12:06:10 -0800306 mutex_destroy(&etb.mutex);
Pratik Patel7831c082011-06-08 21:44:37 -0700307 kfree(etb.buf);
308 misc_deregister(&etb_misc);
309 iounmap(etb.base);
310
311 return 0;
312}
313
314static struct platform_driver etb_driver = {
315 .probe = etb_probe,
Pratik Patel59e29942011-12-27 10:31:33 -0800316 .remove = etb_remove,
Pratik Patel7831c082011-06-08 21:44:37 -0700317 .driver = {
318 .name = "msm_etb",
319 },
320};
321
Pratik Patel59e29942011-12-27 10:31:33 -0800322int __init etb_init(void)
Pratik Patel7831c082011-06-08 21:44:37 -0700323{
324 return platform_driver_register(&etb_driver);
325}
Pratik Patel7831c082011-06-08 21:44:37 -0700326
Pratik Patel59e29942011-12-27 10:31:33 -0800327void etb_exit(void)
Pratik Patel7831c082011-06-08 21:44:37 -0700328{
329 platform_driver_unregister(&etb_driver);
330}