blob: 4a9a97a15ff2945d1113b70f8f69ba45797d9c32 [file] [log] [blame]
Pratik Patel8c86adf2012-12-07 13:42:12 -08001/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Pratik Patel22911492012-06-25 21:38:02 -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>
15#include <linux/init.h>
16#include <linux/types.h>
17#include <linux/device.h>
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/memory_alloc.h>
26#include <linux/delay.h>
27#include <linux/spinlock.h>
28#include <linux/clk.h>
Pratik Patel8c86adf2012-12-07 13:42:12 -080029#include <linux/of.h>
Pratik Patel22911492012-06-25 21:38:02 -070030#include <linux/of_coresight.h>
31#include <linux/coresight.h>
Pratik Patel7c0b3722013-02-08 11:57:27 -080032#include <linux/coresight-cti.h>
Pratik Patelb46038f2012-07-22 23:09:11 -070033#include <linux/usb/usb_qdss.h>
Pratik Patel22911492012-06-25 21:38:02 -070034#include <mach/memory.h>
Pratik Patelb46038f2012-07-22 23:09:11 -070035#include <mach/sps.h>
36#include <mach/usb_bam.h>
Pratik Patel26477792012-09-07 01:35:36 -070037#include <mach/msm_memory_dump.h>
Pratik Patel22911492012-06-25 21:38:02 -070038
39#include "coresight-priv.h"
40
Pratik Patel22911492012-06-25 21:38:02 -070041#define tmc_writel(drvdata, val, off) __raw_writel((val), drvdata->base + off)
42#define tmc_readl(drvdata, off) __raw_readl(drvdata->base + off)
43
44#define TMC_LOCK(drvdata) \
45do { \
46 mb(); \
47 tmc_writel(drvdata, 0x0, CORESIGHT_LAR); \
48} while (0)
49#define TMC_UNLOCK(drvdata) \
50do { \
51 tmc_writel(drvdata, CORESIGHT_UNLOCK, CORESIGHT_LAR); \
52 mb(); \
53} while (0)
54
Pratik Patel84750b22012-12-11 21:56:19 -080055#define TMC_RSZ (0x004)
56#define TMC_STS (0x00C)
57#define TMC_RRD (0x010)
58#define TMC_RRP (0x014)
59#define TMC_RWP (0x018)
60#define TMC_TRG (0x01C)
61#define TMC_CTL (0x020)
62#define TMC_RWD (0x024)
63#define TMC_MODE (0x028)
64#define TMC_LBUFLEVEL (0x02C)
65#define TMC_CBUFLEVEL (0x030)
66#define TMC_BUFWM (0x034)
67#define TMC_RRPHI (0x038)
68#define TMC_RWPHI (0x03C)
69#define TMC_AXICTL (0x110)
70#define TMC_DBALO (0x118)
71#define TMC_DBAHI (0x11C)
72#define TMC_FFSR (0x300)
73#define TMC_FFCR (0x304)
74#define TMC_PSCR (0x308)
75#define TMC_ITMISCOP0 (0xEE0)
76#define TMC_ITTRFLIN (0xEE8)
77#define TMC_ITATBDATA0 (0xEEC)
78#define TMC_ITATBCTR2 (0xEF0)
79#define TMC_ITATBCTR1 (0xEF4)
80#define TMC_ITATBCTR0 (0xEF8)
Pratik Patel22911492012-06-25 21:38:02 -070081
Pratik Patel84750b22012-12-11 21:56:19 -080082#define BYTES_PER_WORD 4
83#define TMC_ETR_BAM_PIPE_INDEX 0
84#define TMC_ETR_BAM_NR_PIPES 2
Pratik Patel22911492012-06-25 21:38:02 -070085
Pratik Patel84750b22012-12-11 21:56:19 -080086#define TMC_ETFETB_DUMP_MAGIC_OFF (0)
87#define TMC_ETFETB_DUMP_MAGIC (0x5D1DB1BF)
88#define TMC_ETFETB_DUMP_VER_OFF (4)
89#define TMC_ETFETB_DUMP_VER (1)
90#define TMC_REG_DUMP_MAGIC_OFF (0)
91#define TMC_REG_DUMP_MAGIC (0x5D1DB1BF)
92#define TMC_REG_DUMP_VER_OFF (4)
93#define TMC_REG_DUMP_VER (1)
Pratik Patel26477792012-09-07 01:35:36 -070094
Pratik Patel22911492012-06-25 21:38:02 -070095enum tmc_config_type {
96 TMC_CONFIG_TYPE_ETB,
97 TMC_CONFIG_TYPE_ETR,
98 TMC_CONFIG_TYPE_ETF,
99};
100
101enum tmc_mode {
102 TMC_MODE_CIRCULAR_BUFFER,
103 TMC_MODE_SOFTWARE_FIFO,
104 TMC_MODE_HARDWARE_FIFO,
105};
106
Pratik Patelb46038f2012-07-22 23:09:11 -0700107enum tmc_etr_out_mode {
108 TMC_ETR_OUT_MODE_NONE,
109 TMC_ETR_OUT_MODE_MEM,
110 TMC_ETR_OUT_MODE_USB,
111};
112
Pratik Patel22911492012-06-25 21:38:02 -0700113enum tmc_mem_intf_width {
114 TMC_MEM_INTF_WIDTH_32BITS = 0x2,
115 TMC_MEM_INTF_WIDTH_64BITS = 0x3,
116 TMC_MEM_INTF_WIDTH_128BITS = 0x4,
117 TMC_MEM_INTF_WIDTH_256BITS = 0x5,
118};
119
Pratik Patelb46038f2012-07-22 23:09:11 -0700120struct tmc_etr_bam_data {
121 struct sps_bam_props props;
122 uint32_t handle;
123 struct sps_pipe *pipe;
124 struct sps_connect connect;
125 uint32_t src_pipe_idx;
126 uint32_t dest;
127 uint32_t dest_pipe_idx;
128 struct sps_mem_buffer desc_fifo;
129 struct sps_mem_buffer data_fifo;
130 bool enable;
131};
132
Pratik Patel22911492012-06-25 21:38:02 -0700133struct tmc_drvdata {
134 void __iomem *base;
135 struct device *dev;
136 struct coresight_device *csdev;
137 struct miscdevice miscdev;
138 struct clk *clk;
139 spinlock_t spinlock;
Pratik Patela5544162013-02-08 11:57:27 -0800140 bool reset_flush_race;
Pratik Patel7c0b3722013-02-08 11:57:27 -0800141 struct coresight_cti *cti_flush;
142 struct coresight_cti *cti_reset;
Pratik Patelb46038f2012-07-22 23:09:11 -0700143 struct mutex read_lock;
Pratik Patel22911492012-06-25 21:38:02 -0700144 int read_count;
145 bool reading;
Pratik Patel84750b22012-12-11 21:56:19 -0800146 bool aborting;
147 char *reg_buf;
Pratik Patel22911492012-06-25 21:38:02 -0700148 char *buf;
149 unsigned long paddr;
150 void __iomem *vaddr;
151 uint32_t size;
Pratik Patelb46038f2012-07-22 23:09:11 -0700152 struct mutex usb_lock;
153 struct usb_qdss_ch *usbch;
154 struct tmc_etr_bam_data *bamdata;
155 enum tmc_etr_out_mode out_mode;
156 bool enable_to_bam;
Pratik Patel22911492012-06-25 21:38:02 -0700157 bool enable;
158 enum tmc_config_type config_type;
159 uint32_t trigger_cntr;
160};
161
Pratik Patelbfd2de62013-01-26 12:14:59 -0800162static void tmc_wait_for_flush(struct tmc_drvdata *drvdata)
163{
164 int count;
165
166 /* Ensure no flush is in progress */
167 for (count = TIMEOUT_US; BVAL(tmc_readl(drvdata, TMC_FFSR), 0) != 0
168 && count > 0; count--)
169 udelay(1);
170 WARN(count == 0, "timeout while waiting for TMC flush, TMC_FFSR: %#x\n",
171 tmc_readl(drvdata, TMC_FFSR));
172}
173
Pratik Patel22911492012-06-25 21:38:02 -0700174static void tmc_wait_for_ready(struct tmc_drvdata *drvdata)
175{
176 int count;
177
178 /* Ensure formatter, unformatter and hardware fifo are empty */
179 for (count = TIMEOUT_US; BVAL(tmc_readl(drvdata, TMC_STS), 2) != 1
180 && count > 0; count--)
181 udelay(1);
182 WARN(count == 0, "timeout while waiting for TMC ready, TMC_STS: %#x\n",
183 tmc_readl(drvdata, TMC_STS));
184}
185
186static void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
187{
188 int count;
189 uint32_t ffcr;
190
191 ffcr = tmc_readl(drvdata, TMC_FFCR);
192 ffcr |= BIT(12);
193 tmc_writel(drvdata, ffcr, TMC_FFCR);
194 ffcr |= BIT(6);
195 tmc_writel(drvdata, ffcr, TMC_FFCR);
196 /* Ensure flush completes */
197 for (count = TIMEOUT_US; BVAL(tmc_readl(drvdata, TMC_FFCR), 6) != 0
198 && count > 0; count--)
199 udelay(1);
200 WARN(count == 0, "timeout while flushing TMC, TMC_FFCR: %#x\n",
201 tmc_readl(drvdata, TMC_FFCR));
202
203 tmc_wait_for_ready(drvdata);
204}
205
206static void __tmc_enable(struct tmc_drvdata *drvdata)
207{
208 tmc_writel(drvdata, 0x1, TMC_CTL);
209}
210
211static void __tmc_disable(struct tmc_drvdata *drvdata)
212{
213 tmc_writel(drvdata, 0x0, TMC_CTL);
214}
215
Pratik Patelb46038f2012-07-22 23:09:11 -0700216static void tmc_etr_fill_usb_bam_data(struct tmc_drvdata *drvdata)
217{
218 struct tmc_etr_bam_data *bamdata = drvdata->bamdata;
219
Shimrit Malichidbf43d72013-03-16 03:32:27 +0200220 get_bam2bam_connection_info(usb_bam_get_qdss_idx(0),
Pratik Patelb46038f2012-07-22 23:09:11 -0700221 &bamdata->dest,
222 &bamdata->dest_pipe_idx,
223 &bamdata->src_pipe_idx,
224 &bamdata->desc_fifo,
225 &bamdata->data_fifo);
226}
227
228static void __tmc_etr_enable_to_bam(struct tmc_drvdata *drvdata)
229{
230 struct tmc_etr_bam_data *bamdata = drvdata->bamdata;
231 uint32_t axictl;
232
233 if (drvdata->enable_to_bam)
234 return;
235
236 /* Configure and enable required CSR registers */
237 msm_qdss_csr_enable_bam_to_usb();
238
239 /* Configure and enable ETR for usb bam output */
240
241 TMC_UNLOCK(drvdata);
242
243 tmc_writel(drvdata, bamdata->data_fifo.size / BYTES_PER_WORD,
244 TMC_RSZ);
245 tmc_writel(drvdata, TMC_MODE_CIRCULAR_BUFFER, TMC_MODE);
246
247 axictl = tmc_readl(drvdata, TMC_AXICTL);
248 axictl |= (0xF << 8);
249 tmc_writel(drvdata, axictl, TMC_AXICTL);
250 axictl &= ~(0x1 << 7);
251 tmc_writel(drvdata, axictl, TMC_AXICTL);
252 axictl = (axictl & ~0x3) | 0x2;
253 tmc_writel(drvdata, axictl, TMC_AXICTL);
254
255 tmc_writel(drvdata, bamdata->data_fifo.phys_base, TMC_DBALO);
256 tmc_writel(drvdata, 0x0, TMC_DBAHI);
Pratik Patelbfd2de62013-01-26 12:14:59 -0800257 tmc_writel(drvdata, 0x103, TMC_FFCR);
Pratik Patelb46038f2012-07-22 23:09:11 -0700258 tmc_writel(drvdata, drvdata->trigger_cntr, TMC_TRG);
259 __tmc_enable(drvdata);
260
261 TMC_LOCK(drvdata);
262
263 drvdata->enable_to_bam = true;
264}
265
266static int tmc_etr_bam_enable(struct tmc_drvdata *drvdata)
267{
268 struct tmc_etr_bam_data *bamdata = drvdata->bamdata;
269 int ret;
270
271 if (bamdata->enable)
272 return 0;
273
Pratik Patelaa0e6f92013-01-26 11:21:03 -0800274 /* Reset bam to start with */
275 ret = sps_device_reset(bamdata->handle);
276 if (ret)
277 goto err0;
278
279 /* Now configure and enable bam */
Pratik Patelb46038f2012-07-22 23:09:11 -0700280
281 bamdata->pipe = sps_alloc_endpoint();
282 if (!bamdata->pipe)
283 return -ENOMEM;
284
285 ret = sps_get_config(bamdata->pipe, &bamdata->connect);
286 if (ret)
Pratik Patelaa0e6f92013-01-26 11:21:03 -0800287 goto err1;
Pratik Patelb46038f2012-07-22 23:09:11 -0700288
289 bamdata->connect.mode = SPS_MODE_SRC;
290 bamdata->connect.source = bamdata->handle;
291 bamdata->connect.event_thresh = 0x4;
292 bamdata->connect.src_pipe_index = TMC_ETR_BAM_PIPE_INDEX;
293 bamdata->connect.options = SPS_O_AUTO_ENABLE;
294
295 bamdata->connect.destination = bamdata->dest;
296 bamdata->connect.dest_pipe_index = bamdata->dest_pipe_idx;
297 bamdata->connect.desc = bamdata->desc_fifo;
298 bamdata->connect.data = bamdata->data_fifo;
299
300 ret = sps_connect(bamdata->pipe, &bamdata->connect);
301 if (ret)
Pratik Patelaa0e6f92013-01-26 11:21:03 -0800302 goto err1;
Pratik Patelb46038f2012-07-22 23:09:11 -0700303
304 bamdata->enable = true;
305 return 0;
Pratik Patelaa0e6f92013-01-26 11:21:03 -0800306err1:
Pratik Patelb46038f2012-07-22 23:09:11 -0700307 sps_free_endpoint(bamdata->pipe);
Pratik Patelaa0e6f92013-01-26 11:21:03 -0800308err0:
Pratik Patelb46038f2012-07-22 23:09:11 -0700309 return ret;
310}
311
312static void __tmc_etr_disable_to_bam(struct tmc_drvdata *drvdata)
313{
314 if (!drvdata->enable_to_bam)
315 return;
316
Pratik Patelbfd2de62013-01-26 12:14:59 -0800317 /* Ensure periodic flush is disabled in CSR block */
318 msm_qdss_csr_disable_flush();
319
Pratik Patelb46038f2012-07-22 23:09:11 -0700320 TMC_UNLOCK(drvdata);
321
Pratik Patelbfd2de62013-01-26 12:14:59 -0800322 tmc_wait_for_flush(drvdata);
Pratik Patelb46038f2012-07-22 23:09:11 -0700323 tmc_flush_and_stop(drvdata);
324 __tmc_disable(drvdata);
325
326 TMC_LOCK(drvdata);
327
Pratik Patelbfd2de62013-01-26 12:14:59 -0800328 /* Disable CSR configuration */
Pratik Patelb46038f2012-07-22 23:09:11 -0700329 msm_qdss_csr_disable_bam_to_usb();
330 drvdata->enable_to_bam = false;
331}
332
333static void tmc_etr_bam_disable(struct tmc_drvdata *drvdata)
334{
335 struct tmc_etr_bam_data *bamdata = drvdata->bamdata;
336
337 if (!bamdata->enable)
338 return;
339
340 sps_disconnect(bamdata->pipe);
341 sps_free_endpoint(bamdata->pipe);
342 bamdata->enable = false;
343}
344
345static void usb_notifier(void *priv, unsigned int event,
346 struct qdss_request *d_req, struct usb_qdss_ch *ch)
347{
348 struct tmc_drvdata *drvdata = priv;
349 unsigned long flags;
350 int ret = 0;
351
352 mutex_lock(&drvdata->usb_lock);
353 if (event == USB_QDSS_CONNECT) {
354 tmc_etr_fill_usb_bam_data(drvdata);
355 ret = tmc_etr_bam_enable(drvdata);
356 if (ret)
357 dev_err(drvdata->dev, "ETR BAM enable failed\n");
358
359 spin_lock_irqsave(&drvdata->spinlock, flags);
360 __tmc_etr_enable_to_bam(drvdata);
361 spin_unlock_irqrestore(&drvdata->spinlock, flags);
362 } else if (event == USB_QDSS_DISCONNECT) {
363 spin_lock_irqsave(&drvdata->spinlock, flags);
364 __tmc_etr_disable_to_bam(drvdata);
365 spin_unlock_irqrestore(&drvdata->spinlock, flags);
366 tmc_etr_bam_disable(drvdata);
367 }
368 mutex_unlock(&drvdata->usb_lock);
369}
370
Pratik Patel22911492012-06-25 21:38:02 -0700371static void __tmc_etb_enable(struct tmc_drvdata *drvdata)
372{
373 /* Zero out the memory to help with debug */
374 memset(drvdata->buf, 0, drvdata->size);
375
376 TMC_UNLOCK(drvdata);
377
378 tmc_writel(drvdata, TMC_MODE_CIRCULAR_BUFFER, TMC_MODE);
Pratik Patel7c0b3722013-02-08 11:57:27 -0800379 tmc_writel(drvdata, 0x1133, TMC_FFCR);
Pratik Patel22911492012-06-25 21:38:02 -0700380 tmc_writel(drvdata, drvdata->trigger_cntr, TMC_TRG);
381 __tmc_enable(drvdata);
382
383 TMC_LOCK(drvdata);
384}
385
Pratik Patelb46038f2012-07-22 23:09:11 -0700386static void __tmc_etr_enable_to_mem(struct tmc_drvdata *drvdata)
Pratik Patel22911492012-06-25 21:38:02 -0700387{
388 uint32_t axictl;
389
390 /* Zero out the memory to help with debug */
391 memset(drvdata->vaddr, 0, drvdata->size);
392
393 TMC_UNLOCK(drvdata);
394
395 tmc_writel(drvdata, drvdata->size / BYTES_PER_WORD, TMC_RSZ);
396 tmc_writel(drvdata, TMC_MODE_CIRCULAR_BUFFER, TMC_MODE);
397
398 axictl = tmc_readl(drvdata, TMC_AXICTL);
399 axictl |= (0xF << 8);
400 tmc_writel(drvdata, axictl, TMC_AXICTL);
401 axictl &= ~(0x1 << 7);
402 tmc_writel(drvdata, axictl, TMC_AXICTL);
403 axictl = (axictl & ~0x3) | 0x2;
404 tmc_writel(drvdata, axictl, TMC_AXICTL);
405
406 tmc_writel(drvdata, drvdata->paddr, TMC_DBALO);
407 tmc_writel(drvdata, 0x0, TMC_DBAHI);
Pratik Patela5544162013-02-08 11:57:27 -0800408 tmc_writel(drvdata, 0x1133, TMC_FFCR);
Pratik Patelb46038f2012-07-22 23:09:11 -0700409 tmc_writel(drvdata, drvdata->trigger_cntr, TMC_TRG);
Pratik Patel22911492012-06-25 21:38:02 -0700410 __tmc_enable(drvdata);
411
412 TMC_LOCK(drvdata);
413}
414
415static void __tmc_etf_enable(struct tmc_drvdata *drvdata)
416{
417 TMC_UNLOCK(drvdata);
418
419 tmc_writel(drvdata, TMC_MODE_HARDWARE_FIFO, TMC_MODE);
420 tmc_writel(drvdata, 0x3, TMC_FFCR);
421 tmc_writel(drvdata, 0x0, TMC_BUFWM);
422 __tmc_enable(drvdata);
423
424 TMC_LOCK(drvdata);
425}
426
427static int tmc_enable(struct tmc_drvdata *drvdata, enum tmc_mode mode)
428{
429 int ret;
430 unsigned long flags;
431
432 ret = clk_prepare_enable(drvdata->clk);
433 if (ret)
434 return ret;
435
Pratik Patelb46038f2012-07-22 23:09:11 -0700436 mutex_lock(&drvdata->usb_lock);
Pratik Patel7c0b3722013-02-08 11:57:27 -0800437 if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
438 coresight_cti_map_trigout(drvdata->cti_flush, 1, 0);
439 coresight_cti_map_trigin(drvdata->cti_reset, 0, 0);
440 } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
Pratik Patela5544162013-02-08 11:57:27 -0800441 if (drvdata->out_mode == TMC_ETR_OUT_MODE_MEM &&
442 !drvdata->reset_flush_race) {
443 coresight_cti_map_trigout(drvdata->cti_flush, 3, 0);
444 coresight_cti_map_trigin(drvdata->cti_reset, 0, 0);
445 } else if (drvdata->out_mode == TMC_ETR_OUT_MODE_USB) {
Pratik Patelb46038f2012-07-22 23:09:11 -0700446 drvdata->usbch = usb_qdss_open("qdss", drvdata,
447 usb_notifier);
448 if (IS_ERR(drvdata->usbch)) {
449 dev_err(drvdata->dev, "usb_qdss_open failed\n");
450 ret = PTR_ERR(drvdata->usbch);
451 goto err0;
452 }
453 }
Pratik Patel7c0b3722013-02-08 11:57:27 -0800454 } else {
455 if (mode == TMC_MODE_CIRCULAR_BUFFER) {
456 coresight_cti_map_trigout(drvdata->cti_flush, 1, 0);
457 coresight_cti_map_trigin(drvdata->cti_reset, 0, 0);
458 }
Pratik Patelb46038f2012-07-22 23:09:11 -0700459 }
460
Pratik Patel22911492012-06-25 21:38:02 -0700461 spin_lock_irqsave(&drvdata->spinlock, flags);
462 if (drvdata->reading) {
Pratik Patelb46038f2012-07-22 23:09:11 -0700463 ret = -EBUSY;
464 goto err1;
Pratik Patel22911492012-06-25 21:38:02 -0700465 }
466
467 if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
468 __tmc_etb_enable(drvdata);
469 } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
Pratik Patelb46038f2012-07-22 23:09:11 -0700470 if (drvdata->out_mode == TMC_ETR_OUT_MODE_MEM)
471 __tmc_etr_enable_to_mem(drvdata);
Pratik Patel22911492012-06-25 21:38:02 -0700472 } else {
473 if (mode == TMC_MODE_CIRCULAR_BUFFER)
474 __tmc_etb_enable(drvdata);
475 else
476 __tmc_etf_enable(drvdata);
477 }
478 drvdata->enable = true;
479 spin_unlock_irqrestore(&drvdata->spinlock, flags);
Pratik Patelb46038f2012-07-22 23:09:11 -0700480 mutex_unlock(&drvdata->usb_lock);
Pratik Patel22911492012-06-25 21:38:02 -0700481
482 dev_info(drvdata->dev, "TMC enabled\n");
483 return 0;
Pratik Patelb46038f2012-07-22 23:09:11 -0700484err1:
485 spin_unlock_irqrestore(&drvdata->spinlock, flags);
486 if (drvdata->config_type == TMC_CONFIG_TYPE_ETR)
487 if (drvdata->out_mode == TMC_ETR_OUT_MODE_USB)
488 usb_qdss_close(drvdata->usbch);
489err0:
490 mutex_unlock(&drvdata->usb_lock);
491 clk_disable_unprepare(drvdata->clk);
492 return ret;
Pratik Patel22911492012-06-25 21:38:02 -0700493}
494
495static int tmc_enable_sink(struct coresight_device *csdev)
496{
497 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
498
499 return tmc_enable(drvdata, TMC_MODE_CIRCULAR_BUFFER);
500}
501
502static int tmc_enable_link(struct coresight_device *csdev, int inport,
503 int outport)
504{
505 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
506
507 return tmc_enable(drvdata, TMC_MODE_HARDWARE_FIFO);
508}
509
Pratik Patel84750b22012-12-11 21:56:19 -0800510static void __tmc_reg_dump(struct tmc_drvdata *drvdata)
511{
512 char *reg_hdr;
513 uint32_t *reg_buf;
514
515 if (!drvdata->reg_buf || !drvdata->aborting)
516 return;
517
518 reg_hdr = drvdata->reg_buf - PAGE_SIZE;
519 reg_buf = (uint32_t *)drvdata->reg_buf;
520
521 reg_buf[1] = tmc_readl(drvdata, TMC_RSZ);
522 reg_buf[3] = tmc_readl(drvdata, TMC_STS);
523 reg_buf[5] = tmc_readl(drvdata, TMC_RRP);
524 reg_buf[6] = tmc_readl(drvdata, TMC_RWP);
525 reg_buf[7] = tmc_readl(drvdata, TMC_TRG);
526 reg_buf[8] = tmc_readl(drvdata, TMC_CTL);
527 reg_buf[10] = tmc_readl(drvdata, TMC_MODE);
528 reg_buf[11] = tmc_readl(drvdata, TMC_LBUFLEVEL);
529 reg_buf[12] = tmc_readl(drvdata, TMC_CBUFLEVEL);
530 reg_buf[13] = tmc_readl(drvdata, TMC_BUFWM);
531 if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
532 reg_buf[14] = tmc_readl(drvdata, TMC_RRPHI);
533 reg_buf[15] = tmc_readl(drvdata, TMC_RWPHI);
534 reg_buf[68] = tmc_readl(drvdata, TMC_AXICTL);
535 reg_buf[70] = tmc_readl(drvdata, TMC_DBALO);
536 reg_buf[71] = tmc_readl(drvdata, TMC_DBAHI);
537 }
538 reg_buf[192] = tmc_readl(drvdata, TMC_FFSR);
539 reg_buf[193] = tmc_readl(drvdata, TMC_FFCR);
540 reg_buf[194] = tmc_readl(drvdata, TMC_PSCR);
541 reg_buf[1000] = tmc_readl(drvdata, CORESIGHT_CLAIMSET);
542 reg_buf[1001] = tmc_readl(drvdata, CORESIGHT_CLAIMCLR);
543 reg_buf[1005] = tmc_readl(drvdata, CORESIGHT_LSR);
544 reg_buf[1006] = tmc_readl(drvdata, CORESIGHT_AUTHSTATUS);
545 reg_buf[1010] = tmc_readl(drvdata, CORESIGHT_DEVID);
546 reg_buf[1011] = tmc_readl(drvdata, CORESIGHT_DEVTYPE);
547 reg_buf[1012] = tmc_readl(drvdata, CORESIGHT_PERIPHIDR4);
548 reg_buf[1013] = tmc_readl(drvdata, CORESIGHT_PERIPHIDR5);
549 reg_buf[1014] = tmc_readl(drvdata, CORESIGHT_PERIPHIDR6);
550 reg_buf[1015] = tmc_readl(drvdata, CORESIGHT_PERIPHIDR7);
551 reg_buf[1016] = tmc_readl(drvdata, CORESIGHT_PERIPHIDR0);
552 reg_buf[1017] = tmc_readl(drvdata, CORESIGHT_PERIPHIDR1);
553 reg_buf[1018] = tmc_readl(drvdata, CORESIGHT_PERIPHIDR2);
554 reg_buf[1019] = tmc_readl(drvdata, CORESIGHT_PERIPHIDR3);
555 reg_buf[1020] = tmc_readl(drvdata, CORESIGHT_COMPIDR0);
556 reg_buf[1021] = tmc_readl(drvdata, CORESIGHT_COMPIDR1);
557 reg_buf[1022] = tmc_readl(drvdata, CORESIGHT_COMPIDR2);
558 reg_buf[1023] = tmc_readl(drvdata, CORESIGHT_COMPIDR3);
559
560 *(uint32_t *)(reg_hdr + TMC_REG_DUMP_MAGIC_OFF) = TMC_REG_DUMP_MAGIC;
561}
562
Pratik Patel22911492012-06-25 21:38:02 -0700563static void __tmc_etb_dump(struct tmc_drvdata *drvdata)
564{
565 enum tmc_mem_intf_width memwidth;
566 uint8_t memwords;
Pratik Patel84750b22012-12-11 21:56:19 -0800567 char *hdr;
Pratik Patel22911492012-06-25 21:38:02 -0700568 char *bufp;
569 uint32_t read_data;
570 int i;
571
572 memwidth = BMVAL(tmc_readl(drvdata, CORESIGHT_DEVID), 8, 10);
573 if (memwidth == TMC_MEM_INTF_WIDTH_32BITS)
574 memwords = 1;
575 else if (memwidth == TMC_MEM_INTF_WIDTH_64BITS)
576 memwords = 2;
577 else if (memwidth == TMC_MEM_INTF_WIDTH_128BITS)
578 memwords = 4;
579 else
580 memwords = 8;
581
582 bufp = drvdata->buf;
583 while (1) {
584 for (i = 0; i < memwords; i++) {
585 read_data = tmc_readl(drvdata, TMC_RRD);
586 if (read_data == 0xFFFFFFFF)
Pratik Patel84750b22012-12-11 21:56:19 -0800587 goto out;
Pratik Patel22911492012-06-25 21:38:02 -0700588 memcpy(bufp, &read_data, BYTES_PER_WORD);
589 bufp += BYTES_PER_WORD;
590 }
591 }
Pratik Patel84750b22012-12-11 21:56:19 -0800592
593out:
594 if (drvdata->aborting) {
595 hdr = drvdata->buf - PAGE_SIZE;
596 *(uint32_t *)(hdr + TMC_ETFETB_DUMP_MAGIC_OFF) =
597 TMC_ETFETB_DUMP_MAGIC;
598 }
Pratik Patel22911492012-06-25 21:38:02 -0700599}
600
601static void __tmc_etb_disable(struct tmc_drvdata *drvdata)
602{
603 TMC_UNLOCK(drvdata);
604
605 tmc_flush_and_stop(drvdata);
606 __tmc_etb_dump(drvdata);
Pratik Patel84750b22012-12-11 21:56:19 -0800607 __tmc_reg_dump(drvdata);
Pratik Patel22911492012-06-25 21:38:02 -0700608 __tmc_disable(drvdata);
609
610 TMC_LOCK(drvdata);
611}
612
613static void __tmc_etr_dump(struct tmc_drvdata *drvdata)
614{
615 uint32_t rwp, rwphi;
616
617 rwp = tmc_readl(drvdata, TMC_RWP);
618 rwphi = tmc_readl(drvdata, TMC_RWPHI);
619
620 if (BVAL(tmc_readl(drvdata, TMC_STS), 0))
Pratik Patel05fe91e2012-08-10 10:47:08 -0700621 drvdata->buf = drvdata->vaddr + rwp - drvdata->paddr;
Pratik Patel22911492012-06-25 21:38:02 -0700622 else
623 drvdata->buf = drvdata->vaddr;
624}
625
Pratik Patelb46038f2012-07-22 23:09:11 -0700626static void __tmc_etr_disable_to_mem(struct tmc_drvdata *drvdata)
Pratik Patel22911492012-06-25 21:38:02 -0700627{
628 TMC_UNLOCK(drvdata);
629
630 tmc_flush_and_stop(drvdata);
631 __tmc_etr_dump(drvdata);
Pratik Patel84750b22012-12-11 21:56:19 -0800632 __tmc_reg_dump(drvdata);
Pratik Patel22911492012-06-25 21:38:02 -0700633 __tmc_disable(drvdata);
634
635 TMC_LOCK(drvdata);
636}
637
638static void __tmc_etf_disable(struct tmc_drvdata *drvdata)
639{
640 TMC_UNLOCK(drvdata);
641
642 tmc_flush_and_stop(drvdata);
643 __tmc_disable(drvdata);
644
645 TMC_LOCK(drvdata);
646}
647
648static void tmc_disable(struct tmc_drvdata *drvdata, enum tmc_mode mode)
649{
650 unsigned long flags;
651
Pratik Patelb46038f2012-07-22 23:09:11 -0700652 mutex_lock(&drvdata->usb_lock);
Pratik Patel22911492012-06-25 21:38:02 -0700653 spin_lock_irqsave(&drvdata->spinlock, flags);
654 if (drvdata->reading)
655 goto out;
656
657 if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
658 __tmc_etb_disable(drvdata);
659 } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
Pratik Patelb46038f2012-07-22 23:09:11 -0700660 if (drvdata->out_mode == TMC_ETR_OUT_MODE_MEM)
661 __tmc_etr_disable_to_mem(drvdata);
662 else if (drvdata->out_mode == TMC_ETR_OUT_MODE_USB)
Pratik Patel7c0b3722013-02-08 11:57:27 -0800663 __tmc_etr_disable_to_bam(drvdata);
Pratik Patel22911492012-06-25 21:38:02 -0700664 } else {
665 if (mode == TMC_MODE_CIRCULAR_BUFFER)
666 __tmc_etb_disable(drvdata);
667 else
668 __tmc_etf_disable(drvdata);
669 }
Pratik Patel22911492012-06-25 21:38:02 -0700670 drvdata->enable = false;
671 spin_unlock_irqrestore(&drvdata->spinlock, flags);
672
Pratik Patel7c0b3722013-02-08 11:57:27 -0800673 if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
674 coresight_cti_unmap_trigin(drvdata->cti_reset, 0, 0);
675 coresight_cti_unmap_trigout(drvdata->cti_flush, 1, 0);
676 } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
Pratik Patela5544162013-02-08 11:57:27 -0800677 if (drvdata->out_mode == TMC_ETR_OUT_MODE_MEM &&
678 !drvdata->reset_flush_race) {
679 coresight_cti_unmap_trigin(drvdata->cti_reset, 0, 0);
680 coresight_cti_unmap_trigout(drvdata->cti_flush, 3, 0);
681 } else if (drvdata->out_mode == TMC_ETR_OUT_MODE_USB) {
Pratik Patel7c0b3722013-02-08 11:57:27 -0800682 tmc_etr_bam_disable(drvdata);
683 usb_qdss_close(drvdata->usbch);
684 }
685 } else {
686 if (mode == TMC_MODE_CIRCULAR_BUFFER) {
687 coresight_cti_unmap_trigin(drvdata->cti_reset, 0, 0);
688 coresight_cti_unmap_trigout(drvdata->cti_flush, 1, 0);
Pratik Patelb46038f2012-07-22 23:09:11 -0700689 }
690 }
691 mutex_unlock(&drvdata->usb_lock);
692
Pratik Patel22911492012-06-25 21:38:02 -0700693 clk_disable_unprepare(drvdata->clk);
694
695 dev_info(drvdata->dev, "TMC disabled\n");
Pratik Patel7c0b3722013-02-08 11:57:27 -0800696 return;
697out:
698 drvdata->enable = false;
699 spin_unlock_irqrestore(&drvdata->spinlock, flags);
700 mutex_unlock(&drvdata->usb_lock);
701
702 clk_disable_unprepare(drvdata->clk);
703
704 dev_info(drvdata->dev, "TMC disabled\n");
Pratik Patel22911492012-06-25 21:38:02 -0700705}
706
707static void tmc_disable_sink(struct coresight_device *csdev)
708{
709 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
710
711 tmc_disable(drvdata, TMC_MODE_CIRCULAR_BUFFER);
712}
713
714static void tmc_disable_link(struct coresight_device *csdev, int inport,
715 int outport)
716{
717 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
718
719 tmc_disable(drvdata, TMC_MODE_HARDWARE_FIFO);
720}
721
Pratik Patelcf7d0452012-07-02 13:57:20 -0700722static void tmc_abort(struct coresight_device *csdev)
723{
724 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
725 unsigned long flags;
726 enum tmc_mode mode;
727
Pratik Patel84750b22012-12-11 21:56:19 -0800728 drvdata->aborting = true;
729
Pratik Patelcf7d0452012-07-02 13:57:20 -0700730 spin_lock_irqsave(&drvdata->spinlock, flags);
731 if (drvdata->reading)
732 goto out0;
733
734 if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
735 __tmc_etb_disable(drvdata);
736 } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
Pratik Patelb46038f2012-07-22 23:09:11 -0700737 if (drvdata->out_mode == TMC_ETR_OUT_MODE_MEM)
738 __tmc_etr_disable_to_mem(drvdata);
Pratik Patel89a76622013-02-09 21:34:41 -0800739 else if (drvdata->out_mode == TMC_ETR_OUT_MODE_USB)
740 __tmc_etr_disable_to_bam(drvdata);
Pratik Patelcf7d0452012-07-02 13:57:20 -0700741 } else {
742 mode = tmc_readl(drvdata, TMC_MODE);
743 if (mode == TMC_MODE_CIRCULAR_BUFFER)
744 __tmc_etb_disable(drvdata);
745 else
746 goto out1;
747 }
748out0:
749 drvdata->enable = false;
750 spin_unlock_irqrestore(&drvdata->spinlock, flags);
751
752 dev_info(drvdata->dev, "TMC aborted\n");
753 return;
754out1:
755 spin_unlock_irqrestore(&drvdata->spinlock, flags);
756}
757
Pratik Patel22911492012-06-25 21:38:02 -0700758static const struct coresight_ops_sink tmc_sink_ops = {
759 .enable = tmc_enable_sink,
760 .disable = tmc_disable_sink,
Pratik Patelcf7d0452012-07-02 13:57:20 -0700761 .abort = tmc_abort,
Pratik Patel22911492012-06-25 21:38:02 -0700762};
763
764static const struct coresight_ops_link tmc_link_ops = {
765 .enable = tmc_enable_link,
766 .disable = tmc_disable_link,
767};
768
769static const struct coresight_ops tmc_etb_cs_ops = {
770 .sink_ops = &tmc_sink_ops,
771};
772
773static const struct coresight_ops tmc_etr_cs_ops = {
774 .sink_ops = &tmc_sink_ops,
775};
776
777static const struct coresight_ops tmc_etf_cs_ops = {
778 .sink_ops = &tmc_sink_ops,
779 .link_ops = &tmc_link_ops,
780};
781
782static int tmc_read_prepare(struct tmc_drvdata *drvdata)
783{
784 int ret;
785 unsigned long flags;
786 enum tmc_mode mode;
787
788 spin_lock_irqsave(&drvdata->spinlock, flags);
789 if (!drvdata->enable)
790 goto out;
791
792 if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
793 __tmc_etb_disable(drvdata);
794 } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
Pratik Patelb46038f2012-07-22 23:09:11 -0700795 if (drvdata->out_mode == TMC_ETR_OUT_MODE_MEM) {
796 __tmc_etr_disable_to_mem(drvdata);
797 } else {
798 ret = -ENODEV;
799 goto err;
800 }
Pratik Patel22911492012-06-25 21:38:02 -0700801 } else {
802 mode = tmc_readl(drvdata, TMC_MODE);
803 if (mode == TMC_MODE_CIRCULAR_BUFFER) {
804 __tmc_etb_disable(drvdata);
805 } else {
806 ret = -ENODEV;
807 goto err;
808 }
809 }
810out:
811 drvdata->reading = true;
812 spin_unlock_irqrestore(&drvdata->spinlock, flags);
813
814 dev_info(drvdata->dev, "TMC read start\n");
815 return 0;
816err:
817 spin_unlock_irqrestore(&drvdata->spinlock, flags);
818 return ret;
819}
820
821static void tmc_read_unprepare(struct tmc_drvdata *drvdata)
822{
823 unsigned long flags;
824 enum tmc_mode mode;
825
826 spin_lock_irqsave(&drvdata->spinlock, flags);
827 if (!drvdata->enable)
828 goto out;
829
830 if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
831 __tmc_etb_enable(drvdata);
832 } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
Pratik Patelb46038f2012-07-22 23:09:11 -0700833 if (drvdata->out_mode == TMC_ETR_OUT_MODE_MEM)
834 __tmc_etr_enable_to_mem(drvdata);
Pratik Patel22911492012-06-25 21:38:02 -0700835 } else {
836 mode = tmc_readl(drvdata, TMC_MODE);
837 if (mode == TMC_MODE_CIRCULAR_BUFFER)
838 __tmc_etb_enable(drvdata);
839 }
840out:
841 drvdata->reading = false;
842 spin_unlock_irqrestore(&drvdata->spinlock, flags);
843
844 dev_info(drvdata->dev, "TMC read end\n");
845}
846
847static int tmc_open(struct inode *inode, struct file *file)
848{
849 struct tmc_drvdata *drvdata = container_of(file->private_data,
850 struct tmc_drvdata, miscdev);
851 int ret = 0;
852
Pratik Patelb46038f2012-07-22 23:09:11 -0700853 mutex_lock(&drvdata->read_lock);
Pratik Patel22911492012-06-25 21:38:02 -0700854 if (drvdata->read_count++)
855 goto out;
856
857 ret = tmc_read_prepare(drvdata);
858 if (ret)
Pratik Patelb46038f2012-07-22 23:09:11 -0700859 goto err;
Pratik Patel22911492012-06-25 21:38:02 -0700860out:
Pratik Patelb46038f2012-07-22 23:09:11 -0700861 mutex_unlock(&drvdata->read_lock);
Pratik Patel22911492012-06-25 21:38:02 -0700862 nonseekable_open(inode, file);
863
864 dev_dbg(drvdata->dev, "%s: successfully opened\n", __func__);
865 return 0;
Pratik Patelb46038f2012-07-22 23:09:11 -0700866err:
867 drvdata->read_count--;
868 mutex_unlock(&drvdata->read_lock);
869 return ret;
Pratik Patel22911492012-06-25 21:38:02 -0700870}
871
872static ssize_t tmc_read(struct file *file, char __user *data, size_t len,
873 loff_t *ppos)
874{
875 struct tmc_drvdata *drvdata = container_of(file->private_data,
876 struct tmc_drvdata, miscdev);
877 char *bufp = drvdata->buf + *ppos;
878
879 if (*ppos + len > drvdata->size)
880 len = drvdata->size - *ppos;
881
882 if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
883 if (bufp == (char *)(drvdata->vaddr + drvdata->size))
884 bufp = drvdata->vaddr;
Pratik Patel05fe91e2012-08-10 10:47:08 -0700885 else if (bufp > (char *)(drvdata->vaddr + drvdata->size))
886 bufp -= drvdata->size;
Pratik Patel22911492012-06-25 21:38:02 -0700887 if ((bufp + len) > (char *)(drvdata->vaddr + drvdata->size))
888 len = (char *)(drvdata->vaddr + drvdata->size) - bufp;
889 }
890
891 if (copy_to_user(data, bufp, len)) {
892 dev_dbg(drvdata->dev, "%s: copy_to_user failed\n", __func__);
893 return -EFAULT;
894 }
895
896 *ppos += len;
897
898 dev_dbg(drvdata->dev, "%s: %d bytes copied, %d bytes left\n",
899 __func__, len, (int) (drvdata->size - *ppos));
900 return len;
901}
902
903static int tmc_release(struct inode *inode, struct file *file)
904{
905 struct tmc_drvdata *drvdata = container_of(file->private_data,
906 struct tmc_drvdata, miscdev);
907
Pratik Patelb46038f2012-07-22 23:09:11 -0700908 mutex_lock(&drvdata->read_lock);
Pratik Patel22911492012-06-25 21:38:02 -0700909 if (--drvdata->read_count) {
910 if (drvdata->read_count < 0) {
911 WARN_ONCE(1, "mismatched close\n");
912 drvdata->read_count = 0;
913 }
914 goto out;
915 }
916
917 tmc_read_unprepare(drvdata);
918out:
Pratik Patelb46038f2012-07-22 23:09:11 -0700919 mutex_unlock(&drvdata->read_lock);
Pratik Patel22911492012-06-25 21:38:02 -0700920 dev_dbg(drvdata->dev, "%s: released\n", __func__);
921 return 0;
922}
923
924static const struct file_operations tmc_fops = {
925 .owner = THIS_MODULE,
926 .open = tmc_open,
927 .read = tmc_read,
928 .release = tmc_release,
929 .llseek = no_llseek,
930};
931
932static ssize_t tmc_show_trigger_cntr(struct device *dev,
933 struct device_attribute *attr, char *buf)
934{
935 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
936 unsigned long val = drvdata->trigger_cntr;
937
938 return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
939}
940
941static ssize_t tmc_store_trigger_cntr(struct device *dev,
942 struct device_attribute *attr,
943 const char *buf, size_t size)
944{
945 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
946 unsigned long val;
947
948 if (sscanf(buf, "%lx", &val) != 1)
949 return -EINVAL;
950
951 drvdata->trigger_cntr = val;
952 return size;
953}
954static DEVICE_ATTR(trigger_cntr, S_IRUGO | S_IWUSR, tmc_show_trigger_cntr,
955 tmc_store_trigger_cntr);
956
Pratik Patelb46038f2012-07-22 23:09:11 -0700957static ssize_t tmc_etr_show_out_mode(struct device *dev,
958 struct device_attribute *attr, char *buf)
959{
960 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
961
962 return scnprintf(buf, PAGE_SIZE, "%s\n",
963 drvdata->out_mode == TMC_ETR_OUT_MODE_MEM ?
964 "mem" : "usb");
965}
966
967static ssize_t tmc_etr_store_out_mode(struct device *dev,
968 struct device_attribute *attr,
969 const char *buf, size_t size)
970{
971 struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
972 char str[10] = "";
973 unsigned long flags;
Pratik Patelb46038f2012-07-22 23:09:11 -0700974 int ret;
975
976 if (strlen(buf) >= 10)
977 return -EINVAL;
978 if (sscanf(buf, "%s", str) != 1)
979 return -EINVAL;
980
981 mutex_lock(&drvdata->usb_lock);
982 if (!strcmp(str, "mem")) {
983 if (drvdata->out_mode == TMC_ETR_OUT_MODE_MEM)
984 goto out;
985
986 spin_lock_irqsave(&drvdata->spinlock, flags);
Pratik Patela5544162013-02-08 11:57:27 -0800987 if (!drvdata->enable) {
988 drvdata->out_mode = TMC_ETR_OUT_MODE_MEM;
989 spin_unlock_irqrestore(&drvdata->spinlock, flags);
990 goto out;
Pratik Patelb46038f2012-07-22 23:09:11 -0700991 }
Pratik Patela5544162013-02-08 11:57:27 -0800992 __tmc_etr_disable_to_bam(drvdata);
993 __tmc_etr_enable_to_mem(drvdata);
Pratik Patelb46038f2012-07-22 23:09:11 -0700994 drvdata->out_mode = TMC_ETR_OUT_MODE_MEM;
995 spin_unlock_irqrestore(&drvdata->spinlock, flags);
996
Pratik Patela5544162013-02-08 11:57:27 -0800997 if (!drvdata->reset_flush_race) {
998 coresight_cti_map_trigout(drvdata->cti_flush, 3, 0);
999 coresight_cti_map_trigin(drvdata->cti_reset, 0, 0);
Pratik Patelb46038f2012-07-22 23:09:11 -07001000 }
Pratik Patela5544162013-02-08 11:57:27 -08001001
1002 tmc_etr_bam_disable(drvdata);
1003 usb_qdss_close(drvdata->usbch);
Pratik Patelb46038f2012-07-22 23:09:11 -07001004 } else if (!strcmp(str, "usb")) {
1005 if (drvdata->out_mode == TMC_ETR_OUT_MODE_USB)
1006 goto out;
1007
1008 spin_lock_irqsave(&drvdata->spinlock, flags);
Pratik Patela5544162013-02-08 11:57:27 -08001009 if (!drvdata->enable) {
1010 drvdata->out_mode = TMC_ETR_OUT_MODE_USB;
1011 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1012 goto out;
Pratik Patelb46038f2012-07-22 23:09:11 -07001013 }
Pratik Patela5544162013-02-08 11:57:27 -08001014 if (drvdata->reading) {
1015 ret = -EBUSY;
1016 goto err1;
1017 }
1018 __tmc_etr_disable_to_mem(drvdata);
Pratik Patelb46038f2012-07-22 23:09:11 -07001019 drvdata->out_mode = TMC_ETR_OUT_MODE_USB;
1020 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1021
Pratik Patela5544162013-02-08 11:57:27 -08001022 if (!drvdata->reset_flush_race) {
1023 coresight_cti_unmap_trigin(drvdata->cti_reset, 0, 0);
1024 coresight_cti_unmap_trigout(drvdata->cti_flush, 3, 0);
1025 }
1026
1027 drvdata->usbch = usb_qdss_open("qdss", drvdata,
1028 usb_notifier);
1029 if (IS_ERR(drvdata->usbch)) {
1030 dev_err(drvdata->dev, "usb_qdss_open failed\n");
1031 ret = PTR_ERR(drvdata->usbch);
1032 goto err0;
Pratik Patelb46038f2012-07-22 23:09:11 -07001033 }
1034 }
1035out:
1036 mutex_unlock(&drvdata->usb_lock);
1037 return size;
1038err1:
1039 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1040err0:
1041 mutex_unlock(&drvdata->usb_lock);
1042 return ret;
1043}
1044static DEVICE_ATTR(out_mode, S_IRUGO | S_IWUSR, tmc_etr_show_out_mode,
1045 tmc_etr_store_out_mode);
1046
Pratik Patel22911492012-06-25 21:38:02 -07001047static struct attribute *tmc_attrs[] = {
1048 &dev_attr_trigger_cntr.attr,
1049 NULL,
1050};
1051
1052static struct attribute_group tmc_attr_grp = {
1053 .attrs = tmc_attrs,
1054};
1055
Pratik Patelb46038f2012-07-22 23:09:11 -07001056static struct attribute *tmc_etr_attrs[] = {
1057 &dev_attr_out_mode.attr,
1058 NULL,
1059};
1060
1061static struct attribute_group tmc_etr_attr_grp = {
1062 .attrs = tmc_etr_attrs,
1063};
1064
Pratik Patel22911492012-06-25 21:38:02 -07001065static const struct attribute_group *tmc_etb_attr_grps[] = {
1066 &tmc_attr_grp,
1067 NULL,
1068};
1069
1070static const struct attribute_group *tmc_etr_attr_grps[] = {
1071 &tmc_attr_grp,
Pratik Patelb46038f2012-07-22 23:09:11 -07001072 &tmc_etr_attr_grp,
Pratik Patel22911492012-06-25 21:38:02 -07001073 NULL,
1074};
1075
1076static const struct attribute_group *tmc_etf_attr_grps[] = {
1077 &tmc_attr_grp,
1078 NULL,
1079};
1080
Pratik Patelb46038f2012-07-22 23:09:11 -07001081static int __devinit tmc_etr_bam_init(struct platform_device *pdev,
1082 struct tmc_drvdata *drvdata)
1083{
1084 struct device *dev = &pdev->dev;
1085 struct resource *res;
1086 struct tmc_etr_bam_data *bamdata;
1087
1088 bamdata = devm_kzalloc(dev, sizeof(*bamdata), GFP_KERNEL);
1089 if (!bamdata)
1090 return -ENOMEM;
1091 drvdata->bamdata = bamdata;
1092
Aparna Dasc9907512013-03-08 10:20:52 -08001093 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "bam-base");
Pratik Patelb46038f2012-07-22 23:09:11 -07001094 if (!res)
1095 return -ENODEV;
1096
1097 bamdata->props.phys_addr = res->start;
1098 bamdata->props.virt_addr = devm_ioremap(dev, res->start,
1099 resource_size(res));
1100 if (!bamdata->props.virt_addr)
1101 return -ENOMEM;
1102 bamdata->props.virt_size = resource_size(res);
1103
1104 bamdata->props.event_threshold = 0x4; /* Pipe event threshold */
1105 bamdata->props.summing_threshold = 0x10; /* BAM event threshold */
1106 bamdata->props.irq = 0;
1107 bamdata->props.num_pipes = TMC_ETR_BAM_NR_PIPES;
1108
1109 return sps_register_bam_device(&bamdata->props, &bamdata->handle);
1110}
1111
1112static void tmc_etr_bam_exit(struct tmc_drvdata *drvdata)
1113{
1114 struct tmc_etr_bam_data *bamdata = drvdata->bamdata;
1115
1116 if (!bamdata->handle)
1117 return;
1118 sps_deregister_bam_device(bamdata->handle);
1119}
1120
Pratik Patel22911492012-06-25 21:38:02 -07001121static int __devinit tmc_probe(struct platform_device *pdev)
1122{
1123 int ret;
1124 uint32_t devid;
1125 struct device *dev = &pdev->dev;
1126 struct coresight_platform_data *pdata;
1127 struct tmc_drvdata *drvdata;
1128 struct resource *res;
Pratik Patel26477792012-09-07 01:35:36 -07001129 uint32_t reg_size;
1130 static int etfetb_count;
1131 static int count;
1132 void *baddr;
1133 struct msm_client_dump dump;
Pratik Patel7c0b3722013-02-08 11:57:27 -08001134 struct coresight_cti_data *ctidata;
Pratik Patel22911492012-06-25 21:38:02 -07001135 struct coresight_desc *desc;
1136
1137 if (pdev->dev.of_node) {
1138 pdata = of_get_coresight_platform_data(dev, pdev->dev.of_node);
1139 if (IS_ERR(pdata))
1140 return PTR_ERR(pdata);
1141 pdev->dev.platform_data = pdata;
1142 }
1143
1144 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
1145 if (!drvdata)
1146 return -ENOMEM;
1147 drvdata->dev = &pdev->dev;
1148 platform_set_drvdata(pdev, drvdata);
1149
Aparna Dasc9907512013-03-08 10:20:52 -08001150 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tmc-base");
Pratik Patel22911492012-06-25 21:38:02 -07001151 if (!res)
1152 return -ENODEV;
Pratik Patel26477792012-09-07 01:35:36 -07001153 reg_size = resource_size(res);
Pratik Patel2c09b762012-07-21 15:54:54 -07001154
Pratik Patel22911492012-06-25 21:38:02 -07001155 drvdata->base = devm_ioremap(dev, res->start, resource_size(res));
1156 if (!drvdata->base)
1157 return -ENOMEM;
1158
1159 spin_lock_init(&drvdata->spinlock);
Pratik Patelb46038f2012-07-22 23:09:11 -07001160 mutex_init(&drvdata->read_lock);
1161 mutex_init(&drvdata->usb_lock);
Pratik Patel22911492012-06-25 21:38:02 -07001162
1163 drvdata->clk = devm_clk_get(dev, "core_clk");
1164 if (IS_ERR(drvdata->clk))
1165 return PTR_ERR(drvdata->clk);
Pratik Patel2c09b762012-07-21 15:54:54 -07001166
Pratik Patel22911492012-06-25 21:38:02 -07001167 ret = clk_set_rate(drvdata->clk, CORESIGHT_CLK_RATE_TRACE);
1168 if (ret)
1169 return ret;
1170
1171 ret = clk_prepare_enable(drvdata->clk);
1172 if (ret)
1173 return ret;
Pratik Patel2c09b762012-07-21 15:54:54 -07001174
Pratik Patel22911492012-06-25 21:38:02 -07001175 devid = tmc_readl(drvdata, CORESIGHT_DEVID);
1176 drvdata->config_type = BMVAL(devid, 6, 7);
Pratik Patel2c09b762012-07-21 15:54:54 -07001177
Pratik Patel8c86adf2012-12-07 13:42:12 -08001178 if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
1179 if (pdev->dev.of_node) {
1180 ret = of_property_read_u32(pdev->dev.of_node,
1181 "qcom,memory-reservation-size", &drvdata->size);
1182 if (ret) {
1183 clk_disable_unprepare(drvdata->clk);
1184 return ret;
1185 }
1186 }
1187 } else {
Pratik Patele411ac72012-10-31 16:38:05 -07001188 drvdata->size = tmc_readl(drvdata, TMC_RSZ) * BYTES_PER_WORD;
Pratik Patel8c86adf2012-12-07 13:42:12 -08001189 }
Pratik Patel2c09b762012-07-21 15:54:54 -07001190
Pratik Patel22911492012-06-25 21:38:02 -07001191 clk_disable_unprepare(drvdata->clk);
1192
1193 if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
1194 drvdata->paddr = allocate_contiguous_ebi_nomap(drvdata->size,
1195 SZ_4K);
1196 if (!drvdata->paddr)
1197 return -ENOMEM;
1198 drvdata->vaddr = devm_ioremap(dev, drvdata->paddr,
1199 drvdata->size);
1200 if (!drvdata->vaddr) {
1201 ret = -ENOMEM;
1202 goto err0;
1203 }
1204 memset(drvdata->vaddr, 0, drvdata->size);
Pratik Patelcd8ac7a2012-10-01 16:05:55 -07001205 drvdata->buf = drvdata->vaddr;
Pratik Patelb46038f2012-07-22 23:09:11 -07001206 drvdata->out_mode = TMC_ETR_OUT_MODE_MEM;
1207
1208 ret = tmc_etr_bam_init(pdev, drvdata);
1209 if (ret)
1210 goto err0;
Pratik Patel22911492012-06-25 21:38:02 -07001211 } else {
Pratik Patele411ac72012-10-31 16:38:05 -07001212 baddr = devm_kzalloc(dev, PAGE_SIZE + drvdata->size,
1213 GFP_KERNEL);
Pratik Patel26477792012-09-07 01:35:36 -07001214 if (!baddr)
Pratik Patel22911492012-06-25 21:38:02 -07001215 return -ENOMEM;
Pratik Patel26477792012-09-07 01:35:36 -07001216 drvdata->buf = baddr + PAGE_SIZE;
1217 *(uint32_t *)(baddr + TMC_ETFETB_DUMP_VER_OFF) =
1218 TMC_ETFETB_DUMP_VER;
1219 dump.id = MSM_TMC_ETFETB + etfetb_count;
1220 dump.start_addr = virt_to_phys(baddr);
Pratik Patele411ac72012-10-31 16:38:05 -07001221 dump.end_addr = dump.start_addr + PAGE_SIZE + drvdata->size;
Pratik Patel26477792012-09-07 01:35:36 -07001222 ret = msm_dump_table_register(&dump);
Pratik Patel84750b22012-12-11 21:56:19 -08001223 /*
1224 * Don't free the buffer in case of error since it can still
1225 * be used to provide dump collection via the device node or
1226 * as part of abort.
Pratik Patel26477792012-09-07 01:35:36 -07001227 */
1228 if (ret)
1229 dev_info(dev, "TMC ETF-ETB dump setup failed\n");
1230 etfetb_count++;
Pratik Patel22911492012-06-25 21:38:02 -07001231 }
1232
Pratik Patel26477792012-09-07 01:35:36 -07001233 baddr = devm_kzalloc(dev, PAGE_SIZE + reg_size, GFP_KERNEL);
1234 if (baddr) {
Pratik Patel84750b22012-12-11 21:56:19 -08001235 drvdata->reg_buf = baddr + PAGE_SIZE;
Pratik Patel26477792012-09-07 01:35:36 -07001236 *(uint32_t *)(baddr + TMC_REG_DUMP_VER_OFF) = TMC_REG_DUMP_VER;
1237 dump.id = MSM_TMC0_REG + count;
1238 dump.start_addr = virt_to_phys(baddr);
1239 dump.end_addr = dump.start_addr + PAGE_SIZE + reg_size;
1240 ret = msm_dump_table_register(&dump);
Pratik Patel84750b22012-12-11 21:56:19 -08001241 /*
1242 * Don't free the buffer in case of error since it can still
1243 * be used to dump registers as part of abort to aid post crash
1244 * parsing.
1245 */
1246 if (ret)
Pratik Patel26477792012-09-07 01:35:36 -07001247 dev_info(dev, "TMC REG dump setup failed\n");
Pratik Patel26477792012-09-07 01:35:36 -07001248 } else {
1249 dev_info(dev, "TMC REG dump space allocation failed\n");
1250 }
1251 count++;
1252
Pratik Patel7c0b3722013-02-08 11:57:27 -08001253 if (pdev->dev.of_node) {
Pratik Patela5544162013-02-08 11:57:27 -08001254 drvdata->reset_flush_race = of_property_read_bool(
1255 pdev->dev.of_node,
1256 "qcom,reset-flush-race");
1257
Pratik Patel7c0b3722013-02-08 11:57:27 -08001258 ctidata = of_get_coresight_cti_data(dev, pdev->dev.of_node);
1259 if (IS_ERR(ctidata)) {
1260 dev_err(dev, "invalid cti data\n");
1261 } else if (ctidata && ctidata->nr_ctis == 2) {
1262 drvdata->cti_flush = coresight_cti_get(
1263 ctidata->names[0]);
1264 if (IS_ERR(drvdata->cti_flush))
1265 dev_err(dev, "failed to get flush cti\n");
1266
1267 drvdata->cti_reset = coresight_cti_get(
1268 ctidata->names[1]);
1269 if (IS_ERR(drvdata->cti_reset))
1270 dev_err(dev, "failed to get reset cti\n");
1271 }
1272 }
1273
Pratik Patel22911492012-06-25 21:38:02 -07001274 desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
1275 if (!desc) {
1276 ret = -ENOMEM;
Pratik Patelb46038f2012-07-22 23:09:11 -07001277 goto err1;
Pratik Patel22911492012-06-25 21:38:02 -07001278 }
1279 if (drvdata->config_type == TMC_CONFIG_TYPE_ETB) {
1280 desc->type = CORESIGHT_DEV_TYPE_SINK;
1281 desc->subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
1282 desc->ops = &tmc_etb_cs_ops;
1283 desc->pdata = pdev->dev.platform_data;
1284 desc->dev = &pdev->dev;
1285 desc->groups = tmc_etb_attr_grps;
1286 desc->owner = THIS_MODULE;
1287 drvdata->csdev = coresight_register(desc);
1288 if (IS_ERR(drvdata->csdev)) {
1289 ret = PTR_ERR(drvdata->csdev);
Pratik Patelb46038f2012-07-22 23:09:11 -07001290 goto err1;
Pratik Patel22911492012-06-25 21:38:02 -07001291 }
1292 } else if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
1293 desc->type = CORESIGHT_DEV_TYPE_SINK;
1294 desc->subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
1295 desc->ops = &tmc_etr_cs_ops;
1296 desc->pdata = pdev->dev.platform_data;
1297 desc->dev = &pdev->dev;
1298 desc->groups = tmc_etr_attr_grps;
1299 desc->owner = THIS_MODULE;
1300 drvdata->csdev = coresight_register(desc);
1301 if (IS_ERR(drvdata->csdev)) {
1302 ret = PTR_ERR(drvdata->csdev);
Pratik Patelb46038f2012-07-22 23:09:11 -07001303 goto err1;
Pratik Patel22911492012-06-25 21:38:02 -07001304 }
1305 } else {
1306 desc->type = CORESIGHT_DEV_TYPE_LINKSINK;
1307 desc->subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
1308 desc->subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_FIFO;
1309 desc->ops = &tmc_etf_cs_ops;
1310 desc->pdata = pdev->dev.platform_data;
1311 desc->dev = &pdev->dev;
1312 desc->groups = tmc_etf_attr_grps;
1313 desc->owner = THIS_MODULE;
1314 drvdata->csdev = coresight_register(desc);
1315 if (IS_ERR(drvdata->csdev)) {
1316 ret = PTR_ERR(drvdata->csdev);
Pratik Patelb46038f2012-07-22 23:09:11 -07001317 goto err1;
Pratik Patel22911492012-06-25 21:38:02 -07001318 }
1319 }
1320
1321 drvdata->miscdev.name = ((struct coresight_platform_data *)
1322 (pdev->dev.platform_data))->name;
1323 drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
1324 drvdata->miscdev.fops = &tmc_fops;
1325 ret = misc_register(&drvdata->miscdev);
1326 if (ret)
Pratik Patelb46038f2012-07-22 23:09:11 -07001327 goto err2;
Pratik Patel22911492012-06-25 21:38:02 -07001328
1329 dev_info(dev, "TMC initialized\n");
1330 return 0;
Pratik Patelb46038f2012-07-22 23:09:11 -07001331err2:
Pratik Patel22911492012-06-25 21:38:02 -07001332 coresight_unregister(drvdata->csdev);
Pratik Patelb46038f2012-07-22 23:09:11 -07001333err1:
1334 tmc_etr_bam_exit(drvdata);
Pratik Patel22911492012-06-25 21:38:02 -07001335err0:
1336 free_contiguous_memory_by_paddr(drvdata->paddr);
1337 return ret;
1338}
1339
1340static int __devexit tmc_remove(struct platform_device *pdev)
1341{
1342 struct tmc_drvdata *drvdata = platform_get_drvdata(pdev);
1343
1344 misc_deregister(&drvdata->miscdev);
1345 coresight_unregister(drvdata->csdev);
Pratik Patelb46038f2012-07-22 23:09:11 -07001346 tmc_etr_bam_exit(drvdata);
Pratik Patel22911492012-06-25 21:38:02 -07001347 free_contiguous_memory_by_paddr(drvdata->paddr);
1348 return 0;
1349}
1350
1351static struct of_device_id tmc_match[] = {
1352 {.compatible = "arm,coresight-tmc"},
1353 {}
1354};
Pratik Patelfa896c92012-08-21 09:24:51 -07001355EXPORT_COMPAT("arm,coresight-tmc");
Pratik Patel22911492012-06-25 21:38:02 -07001356
1357static struct platform_driver tmc_driver = {
1358 .probe = tmc_probe,
1359 .remove = __devexit_p(tmc_remove),
1360 .driver = {
1361 .name = "coresight-tmc",
1362 .owner = THIS_MODULE,
1363 .of_match_table = tmc_match,
1364 },
1365};
1366
1367static int __init tmc_init(void)
1368{
1369 return platform_driver_register(&tmc_driver);
1370}
1371module_init(tmc_init);
1372
1373static void __exit tmc_exit(void)
1374{
1375 platform_driver_unregister(&tmc_driver);
1376}
1377module_exit(tmc_exit);
1378
1379MODULE_LICENSE("GPL v2");
1380MODULE_DESCRIPTION("CoreSight Trace Memory Controller driver");