]> git.ipfire.org Git - thirdparty/u-boot.git/blame - common/iotrace.c
ARM: da850evm: Pinctrl for da850evm
[thirdparty/u-boot.git] / common / iotrace.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
aa53233a
SG
2/*
3 * Copyright (c) 2014 Google, Inc.
aa53233a
SG
4 */
5
6#define IOTRACE_IMPL
7
8#include <common.h>
0eb25b61 9#include <mapmem.h>
aa53233a
SG
10#include <asm/io.h>
11
12DECLARE_GLOBAL_DATA_PTR;
13
aa53233a
SG
14/**
15 * struct iotrace - current trace status and checksum
16 *
17 * @start: Start address of iotrace buffer
e0212dfa
RF
18 * @size: Actual size of iotrace buffer in bytes
19 * @needed_size: Needed of iotrace buffer in bytes
aa53233a 20 * @offset: Current write offset into iotrace buffer
a74440b2
RF
21 * @region_start: Address of IO region to trace
22 * @region_size: Size of region to trace. if 0 will trace all address space
aa53233a
SG
23 * @crc32: Current value of CRC chceksum of trace records
24 * @enabled: true if enabled, false if disabled
25 */
26static struct iotrace {
27 ulong start;
28 ulong size;
e0212dfa 29 ulong needed_size;
aa53233a 30 ulong offset;
a74440b2
RF
31 ulong region_start;
32 ulong region_size;
aa53233a
SG
33 u32 crc32;
34 bool enabled;
35} iotrace;
36
37static void add_record(int flags, const void *ptr, ulong value)
38{
39 struct iotrace_record srec, *rec = &srec;
40
41 /*
42 * We don't support iotrace before relocation. Since the trace buffer
43 * is set up by a command, it can't be enabled at present. To change
44 * this we would need to set the iotrace buffer at build-time. See
45 * lib/trace.c for how this might be done if you are interested.
46 */
47 if (!(gd->flags & GD_FLG_RELOC) || !iotrace.enabled)
48 return;
49
a74440b2
RF
50 if (iotrace.region_size)
51 if ((ulong)ptr < iotrace.region_start ||
52 (ulong)ptr > iotrace.region_start + iotrace.region_size)
53 return;
54
aa53233a
SG
55 /* Store it if there is room */
56 if (iotrace.offset + sizeof(*rec) < iotrace.size) {
57 rec = (struct iotrace_record *)map_sysmem(
58 iotrace.start + iotrace.offset,
59 sizeof(value));
e0212dfa
RF
60 } else {
61 WARN_ONCE(1, "WARNING: iotrace buffer exhausted, please check needed length using \"iotrace stats\"\n");
62 iotrace.needed_size += sizeof(struct iotrace_record);
63 return;
aa53233a 64 }
e0212dfa 65
b5e0e360 66 rec->timestamp = timer_get_us();
aa53233a
SG
67 rec->flags = flags;
68 rec->addr = map_to_sysmem(ptr);
69 rec->value = value;
70
71 /* Update our checksum */
72 iotrace.crc32 = crc32(iotrace.crc32, (unsigned char *)rec,
73 sizeof(*rec));
74
e0212dfa 75 iotrace.needed_size += sizeof(struct iotrace_record);
aa53233a
SG
76 iotrace.offset += sizeof(struct iotrace_record);
77}
78
79u32 iotrace_readl(const void *ptr)
80{
81 u32 v;
82
83 v = readl(ptr);
84 add_record(IOT_32 | IOT_READ, ptr, v);
85
86 return v;
87}
88
89void iotrace_writel(ulong value, const void *ptr)
90{
91 add_record(IOT_32 | IOT_WRITE, ptr, value);
92 writel(value, ptr);
93}
94
95u16 iotrace_readw(const void *ptr)
96{
97 u32 v;
98
99 v = readw(ptr);
100 add_record(IOT_16 | IOT_READ, ptr, v);
101
102 return v;
103}
104
105void iotrace_writew(ulong value, const void *ptr)
106{
107 add_record(IOT_16 | IOT_WRITE, ptr, value);
108 writew(value, ptr);
109}
110
111u8 iotrace_readb(const void *ptr)
112{
113 u32 v;
114
115 v = readb(ptr);
116 add_record(IOT_8 | IOT_READ, ptr, v);
117
118 return v;
119}
120
121void iotrace_writeb(ulong value, const void *ptr)
122{
123 add_record(IOT_8 | IOT_WRITE, ptr, value);
124 writeb(value, ptr);
125}
126
127void iotrace_reset_checksum(void)
128{
129 iotrace.crc32 = 0;
130}
131
132u32 iotrace_get_checksum(void)
133{
134 return iotrace.crc32;
135}
136
a74440b2
RF
137void iotrace_set_region(ulong start, ulong size)
138{
139 iotrace.region_start = start;
140 iotrace.region_size = size;
141}
142
143void iotrace_reset_region(void)
144{
145 iotrace.region_start = 0;
146 iotrace.region_size = 0;
147}
148
149void iotrace_get_region(ulong *start, ulong *size)
150{
151 *start = iotrace.region_start;
152 *size = iotrace.region_size;
153}
154
aa53233a
SG
155void iotrace_set_enabled(int enable)
156{
157 iotrace.enabled = enable;
158}
159
160int iotrace_get_enabled(void)
161{
162 return iotrace.enabled;
163}
164
165void iotrace_set_buffer(ulong start, ulong size)
166{
167 iotrace.start = start;
168 iotrace.size = size;
169 iotrace.offset = 0;
170 iotrace.crc32 = 0;
171}
172
e0212dfa 173void iotrace_get_buffer(ulong *start, ulong *size, ulong *needed_size, ulong *offset, ulong *count)
aa53233a
SG
174{
175 *start = iotrace.start;
176 *size = iotrace.size;
e0212dfa 177 *needed_size = iotrace.needed_size;
aa53233a
SG
178 *offset = iotrace.offset;
179 *count = iotrace.offset / sizeof(struct iotrace_record);
180}