1 /* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
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.
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.
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>
19 #include <linux/err.h>
21 #include <linux/miscdevice.h>
22 #include <linux/uaccess.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/seq_file.h>
27 #include <linux/coresight.h>
28 #include <linux/amba/bus.h>
29 #include <linux/clk.h>
31 #include "coresight-priv.h"
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
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
51 /* register description */
53 #define ETB_STATUS_RAM_FULL BIT(0)
55 #define ETB_CTL_CAPT_EN BIT(0)
57 #define ETB_FFCR_EN_FTC BIT(0)
58 #define ETB_FFCR_FON_MAN BIT(6)
59 #define ETB_FFCR_STOP_FI BIT(12)
60 #define ETB_FFCR_STOP_TRIGGER BIT(13)
62 #define ETB_FFCR_BIT 6
63 #define ETB_FFSR_BIT 1
64 #define ETB_FRAME_SIZE_WORDS 4
67 * struct etb_drvdata - specifics associated to an ETB component
68 * @base: memory mapped base address for this component.
69 * @dev: the device entity associated to this component.
70 * @atclk: optional clock for the core parts of the ETB.
71 * @csdev: component vitals needed by the framework.
72 * @miscdev: specifics to handle "/dev/xyz.etb" entry.
73 * @spinlock: only one at a time pls.
74 * @in_use: synchronise user space access to etb buffer.
75 * @buf: area of memory where ETB buffer content gets sent.
76 * @buffer_depth: size of @buf.
77 * @enable: this ETB is being used.
78 * @trigger_cntr: amount of words to store after a trigger.
84 struct coresight_device
*csdev
;
85 struct miscdevice miscdev
;
94 static unsigned int etb_get_buffer_depth(struct etb_drvdata
*drvdata
)
98 pm_runtime_get_sync(drvdata
->dev
);
100 /* RO registers don't need locking */
101 depth
= readl_relaxed(drvdata
->base
+ ETB_RAM_DEPTH_REG
);
103 pm_runtime_put(drvdata
->dev
);
107 static void etb_enable_hw(struct etb_drvdata
*drvdata
)
112 CS_UNLOCK(drvdata
->base
);
114 depth
= drvdata
->buffer_depth
;
115 /* reset write RAM pointer address */
116 writel_relaxed(0x0, drvdata
->base
+ ETB_RAM_WRITE_POINTER
);
117 /* clear entire RAM buffer */
118 for (i
= 0; i
< depth
; i
++)
119 writel_relaxed(0x0, drvdata
->base
+ ETB_RWD_REG
);
121 /* reset write RAM pointer address */
122 writel_relaxed(0x0, drvdata
->base
+ ETB_RAM_WRITE_POINTER
);
123 /* reset read RAM pointer address */
124 writel_relaxed(0x0, drvdata
->base
+ ETB_RAM_READ_POINTER
);
126 writel_relaxed(drvdata
->trigger_cntr
, drvdata
->base
+ ETB_TRG
);
127 writel_relaxed(ETB_FFCR_EN_FTC
| ETB_FFCR_STOP_TRIGGER
,
128 drvdata
->base
+ ETB_FFCR
);
129 /* ETB trace capture enable */
130 writel_relaxed(ETB_CTL_CAPT_EN
, drvdata
->base
+ ETB_CTL_REG
);
132 CS_LOCK(drvdata
->base
);
135 static int etb_enable(struct coresight_device
*csdev
)
137 struct etb_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
140 pm_runtime_get_sync(drvdata
->dev
);
142 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
143 etb_enable_hw(drvdata
);
144 drvdata
->enable
= true;
145 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
147 dev_info(drvdata
->dev
, "ETB enabled\n");
151 static void etb_disable_hw(struct etb_drvdata
*drvdata
)
155 CS_UNLOCK(drvdata
->base
);
157 ffcr
= readl_relaxed(drvdata
->base
+ ETB_FFCR
);
158 /* stop formatter when a stop has completed */
159 ffcr
|= ETB_FFCR_STOP_FI
;
160 writel_relaxed(ffcr
, drvdata
->base
+ ETB_FFCR
);
161 /* manually generate a flush of the system */
162 ffcr
|= ETB_FFCR_FON_MAN
;
163 writel_relaxed(ffcr
, drvdata
->base
+ ETB_FFCR
);
165 if (coresight_timeout(drvdata
->base
, ETB_FFCR
, ETB_FFCR_BIT
, 0)) {
166 dev_err(drvdata
->dev
,
167 "timeout observed when probing at offset %#x\n",
171 /* disable trace capture */
172 writel_relaxed(0x0, drvdata
->base
+ ETB_CTL_REG
);
174 if (coresight_timeout(drvdata
->base
, ETB_FFSR
, ETB_FFSR_BIT
, 1)) {
175 dev_err(drvdata
->dev
,
176 "timeout observed when probing at offset %#x\n",
180 CS_LOCK(drvdata
->base
);
183 static void etb_dump_hw(struct etb_drvdata
*drvdata
)
187 u32 read_data
, depth
;
188 u32 read_ptr
, write_ptr
;
189 u32 frame_off
, frame_endoff
;
191 CS_UNLOCK(drvdata
->base
);
193 read_ptr
= readl_relaxed(drvdata
->base
+ ETB_RAM_READ_POINTER
);
194 write_ptr
= readl_relaxed(drvdata
->base
+ ETB_RAM_WRITE_POINTER
);
196 frame_off
= write_ptr
% ETB_FRAME_SIZE_WORDS
;
197 frame_endoff
= ETB_FRAME_SIZE_WORDS
- frame_off
;
199 dev_err(drvdata
->dev
,
200 "write_ptr: %lu not aligned to formatter frame size\n",
201 (unsigned long)write_ptr
);
202 dev_err(drvdata
->dev
, "frameoff: %lu, frame_endoff: %lu\n",
203 (unsigned long)frame_off
, (unsigned long)frame_endoff
);
204 write_ptr
+= frame_endoff
;
207 if ((readl_relaxed(drvdata
->base
+ ETB_STATUS_REG
)
208 & ETB_STATUS_RAM_FULL
) == 0)
209 writel_relaxed(0x0, drvdata
->base
+ ETB_RAM_READ_POINTER
);
211 writel_relaxed(write_ptr
, drvdata
->base
+ ETB_RAM_READ_POINTER
);
213 depth
= drvdata
->buffer_depth
;
214 buf_ptr
= drvdata
->buf
;
215 for (i
= 0; i
< depth
; i
++) {
216 read_data
= readl_relaxed(drvdata
->base
+
217 ETB_RAM_READ_DATA_REG
);
218 *buf_ptr
++ = read_data
>> 0;
219 *buf_ptr
++ = read_data
>> 8;
220 *buf_ptr
++ = read_data
>> 16;
221 *buf_ptr
++ = read_data
>> 24;
225 buf_ptr
-= (frame_endoff
* 4);
226 for (i
= 0; i
< frame_endoff
; i
++) {
234 writel_relaxed(read_ptr
, drvdata
->base
+ ETB_RAM_READ_POINTER
);
236 CS_LOCK(drvdata
->base
);
239 static void etb_disable(struct coresight_device
*csdev
)
241 struct etb_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
244 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
245 etb_disable_hw(drvdata
);
246 etb_dump_hw(drvdata
);
247 drvdata
->enable
= false;
248 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
250 pm_runtime_put(drvdata
->dev
);
252 dev_info(drvdata
->dev
, "ETB disabled\n");
255 static const struct coresight_ops_sink etb_sink_ops
= {
256 .enable
= etb_enable
,
257 .disable
= etb_disable
,
260 static const struct coresight_ops etb_cs_ops
= {
261 .sink_ops
= &etb_sink_ops
,
264 static void etb_dump(struct etb_drvdata
*drvdata
)
268 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
269 if (drvdata
->enable
) {
270 etb_disable_hw(drvdata
);
271 etb_dump_hw(drvdata
);
272 etb_enable_hw(drvdata
);
274 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
276 dev_info(drvdata
->dev
, "ETB dumped\n");
279 static int etb_open(struct inode
*inode
, struct file
*file
)
281 struct etb_drvdata
*drvdata
= container_of(file
->private_data
,
282 struct etb_drvdata
, miscdev
);
284 if (atomic_cmpxchg(&drvdata
->in_use
, 0, 1))
287 dev_dbg(drvdata
->dev
, "%s: successfully opened\n", __func__
);
291 static ssize_t
etb_read(struct file
*file
, char __user
*data
,
292 size_t len
, loff_t
*ppos
)
295 struct etb_drvdata
*drvdata
= container_of(file
->private_data
,
296 struct etb_drvdata
, miscdev
);
300 depth
= drvdata
->buffer_depth
;
301 if (*ppos
+ len
> depth
* 4)
302 len
= depth
* 4 - *ppos
;
304 if (copy_to_user(data
, drvdata
->buf
+ *ppos
, len
)) {
305 dev_dbg(drvdata
->dev
, "%s: copy_to_user failed\n", __func__
);
311 dev_dbg(drvdata
->dev
, "%s: %zu bytes copied, %d bytes left\n",
312 __func__
, len
, (int)(depth
* 4 - *ppos
));
316 static int etb_release(struct inode
*inode
, struct file
*file
)
318 struct etb_drvdata
*drvdata
= container_of(file
->private_data
,
319 struct etb_drvdata
, miscdev
);
320 atomic_set(&drvdata
->in_use
, 0);
322 dev_dbg(drvdata
->dev
, "%s: released\n", __func__
);
326 static const struct file_operations etb_fops
= {
327 .owner
= THIS_MODULE
,
330 .release
= etb_release
,
334 static ssize_t
status_show(struct device
*dev
,
335 struct device_attribute
*attr
, char *buf
)
338 u32 etb_rdr
, etb_sr
, etb_rrp
, etb_rwp
;
339 u32 etb_trg
, etb_cr
, etb_ffsr
, etb_ffcr
;
340 struct etb_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
342 pm_runtime_get_sync(drvdata
->dev
);
343 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
344 CS_UNLOCK(drvdata
->base
);
346 etb_rdr
= readl_relaxed(drvdata
->base
+ ETB_RAM_DEPTH_REG
);
347 etb_sr
= readl_relaxed(drvdata
->base
+ ETB_STATUS_REG
);
348 etb_rrp
= readl_relaxed(drvdata
->base
+ ETB_RAM_READ_POINTER
);
349 etb_rwp
= readl_relaxed(drvdata
->base
+ ETB_RAM_WRITE_POINTER
);
350 etb_trg
= readl_relaxed(drvdata
->base
+ ETB_TRG
);
351 etb_cr
= readl_relaxed(drvdata
->base
+ ETB_CTL_REG
);
352 etb_ffsr
= readl_relaxed(drvdata
->base
+ ETB_FFSR
);
353 etb_ffcr
= readl_relaxed(drvdata
->base
+ ETB_FFCR
);
355 CS_LOCK(drvdata
->base
);
356 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
358 pm_runtime_put(drvdata
->dev
);
363 "RAM read ptr:\t0x%x\n"
364 "RAM wrt ptr:\t0x%x\n"
365 "Trigger cnt:\t0x%x\n"
367 "Flush status:\t0x%x\n"
368 "Flush ctrl:\t0x%x\n",
369 etb_rdr
, etb_sr
, etb_rrp
, etb_rwp
,
370 etb_trg
, etb_cr
, etb_ffsr
, etb_ffcr
);
374 static DEVICE_ATTR_RO(status
);
376 static ssize_t
trigger_cntr_show(struct device
*dev
,
377 struct device_attribute
*attr
, char *buf
)
379 struct etb_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
380 unsigned long val
= drvdata
->trigger_cntr
;
382 return sprintf(buf
, "%#lx\n", val
);
385 static ssize_t
trigger_cntr_store(struct device
*dev
,
386 struct device_attribute
*attr
,
387 const char *buf
, size_t size
)
391 struct etb_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
393 ret
= kstrtoul(buf
, 16, &val
);
397 drvdata
->trigger_cntr
= val
;
400 static DEVICE_ATTR_RW(trigger_cntr
);
402 static struct attribute
*coresight_etb_attrs
[] = {
403 &dev_attr_trigger_cntr
.attr
,
404 &dev_attr_status
.attr
,
407 ATTRIBUTE_GROUPS(coresight_etb
);
409 static int etb_probe(struct amba_device
*adev
, const struct amba_id
*id
)
413 struct device
*dev
= &adev
->dev
;
414 struct coresight_platform_data
*pdata
= NULL
;
415 struct etb_drvdata
*drvdata
;
416 struct resource
*res
= &adev
->res
;
417 struct coresight_desc
*desc
;
418 struct device_node
*np
= adev
->dev
.of_node
;
421 pdata
= of_get_coresight_platform_data(dev
, np
);
423 return PTR_ERR(pdata
);
424 adev
->dev
.platform_data
= pdata
;
427 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
431 drvdata
->dev
= &adev
->dev
;
432 drvdata
->atclk
= devm_clk_get(&adev
->dev
, "atclk"); /* optional */
433 if (!IS_ERR(drvdata
->atclk
)) {
434 ret
= clk_prepare_enable(drvdata
->atclk
);
438 dev_set_drvdata(dev
, drvdata
);
440 /* validity for the resource is already checked by the AMBA core */
441 base
= devm_ioremap_resource(dev
, res
);
443 return PTR_ERR(base
);
445 drvdata
->base
= base
;
447 spin_lock_init(&drvdata
->spinlock
);
449 drvdata
->buffer_depth
= etb_get_buffer_depth(drvdata
);
450 pm_runtime_put(&adev
->dev
);
452 if (drvdata
->buffer_depth
& 0x80000000)
455 drvdata
->buf
= devm_kzalloc(dev
,
456 drvdata
->buffer_depth
* 4, GFP_KERNEL
);
458 dev_err(dev
, "Failed to allocate %u bytes for buffer data\n",
459 drvdata
->buffer_depth
* 4);
463 desc
= devm_kzalloc(dev
, sizeof(*desc
), GFP_KERNEL
);
467 desc
->type
= CORESIGHT_DEV_TYPE_SINK
;
468 desc
->subtype
.sink_subtype
= CORESIGHT_DEV_SUBTYPE_SINK_BUFFER
;
469 desc
->ops
= &etb_cs_ops
;
472 desc
->groups
= coresight_etb_groups
;
473 drvdata
->csdev
= coresight_register(desc
);
474 if (IS_ERR(drvdata
->csdev
))
475 return PTR_ERR(drvdata
->csdev
);
477 drvdata
->miscdev
.name
= pdata
->name
;
478 drvdata
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
479 drvdata
->miscdev
.fops
= &etb_fops
;
480 ret
= misc_register(&drvdata
->miscdev
);
482 goto err_misc_register
;
484 dev_info(dev
, "ETB initialized\n");
488 coresight_unregister(drvdata
->csdev
);
492 static int etb_remove(struct amba_device
*adev
)
494 struct etb_drvdata
*drvdata
= amba_get_drvdata(adev
);
496 misc_deregister(&drvdata
->miscdev
);
497 coresight_unregister(drvdata
->csdev
);
502 static int etb_runtime_suspend(struct device
*dev
)
504 struct etb_drvdata
*drvdata
= dev_get_drvdata(dev
);
506 if (drvdata
&& !IS_ERR(drvdata
->atclk
))
507 clk_disable_unprepare(drvdata
->atclk
);
512 static int etb_runtime_resume(struct device
*dev
)
514 struct etb_drvdata
*drvdata
= dev_get_drvdata(dev
);
516 if (drvdata
&& !IS_ERR(drvdata
->atclk
))
517 clk_prepare_enable(drvdata
->atclk
);
523 static const struct dev_pm_ops etb_dev_pm_ops
= {
524 SET_RUNTIME_PM_OPS(etb_runtime_suspend
, etb_runtime_resume
, NULL
)
527 static struct amba_id etb_ids
[] = {
535 static struct amba_driver etb_driver
= {
537 .name
= "coresight-etb10",
538 .owner
= THIS_MODULE
,
539 .pm
= &etb_dev_pm_ops
,
543 .remove
= etb_remove
,
547 module_amba_driver(etb_driver
);
549 MODULE_LICENSE("GPL v2");
550 MODULE_DESCRIPTION("CoreSight Embedded Trace Buffer driver");