1
2
3
4#include <linux/debugfs.h>
5#include <linux/kernel.h>
6#include <linux/module.h>
7#include <linux/of.h>
8#include <linux/of_address.h>
9#include <linux/of_reserved_mem.h>
10#include <linux/platform_device.h>
11#include <linux/seq_file.h>
12#include <linux/types.h>
13
14#include <soc/qcom/cmd-db.h>
15
16#define NUM_PRIORITY 2
17#define MAX_SLV_ID 8
18#define SLAVE_ID_MASK 0x7
19#define SLAVE_ID_SHIFT 16
20
21
22
23
24
25
26
27
28
29
30struct entry_header {
31 u8 id[8];
32 __le32 priority[NUM_PRIORITY];
33 __le32 addr;
34 __le16 len;
35 __le16 offset;
36};
37
38
39
40
41
42
43
44
45
46
47
48struct rsc_hdr {
49 __le16 slv_id;
50 __le16 header_offset;
51 __le16 data_offset;
52 __le16 cnt;
53 __le16 version;
54 __le16 reserved[3];
55};
56
57
58
59
60
61
62
63
64
65
66
67struct cmd_db_header {
68 __le32 version;
69 u8 magic[4];
70 struct rsc_hdr header[MAX_SLV_ID];
71 __le32 checksum;
72 __le32 reserved;
73 u8 data[];
74};
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95static const u8 CMD_DB_MAGIC[] = { 0xdb, 0x30, 0x03, 0x0c };
96
97static bool cmd_db_magic_matches(const struct cmd_db_header *header)
98{
99 const u8 *magic = header->magic;
100
101 return memcmp(magic, CMD_DB_MAGIC, ARRAY_SIZE(CMD_DB_MAGIC)) == 0;
102}
103
104static struct cmd_db_header *cmd_db_header;
105
106static inline const void *rsc_to_entry_header(const struct rsc_hdr *hdr)
107{
108 u16 offset = le16_to_cpu(hdr->header_offset);
109
110 return cmd_db_header->data + offset;
111}
112
113static inline void *
114rsc_offset(const struct rsc_hdr *hdr, const struct entry_header *ent)
115{
116 u16 offset = le16_to_cpu(hdr->data_offset);
117 u16 loffset = le16_to_cpu(ent->offset);
118
119 return cmd_db_header->data + offset + loffset;
120}
121
122
123
124
125
126
127int cmd_db_ready(void)
128{
129 if (cmd_db_header == NULL)
130 return -EPROBE_DEFER;
131 else if (!cmd_db_magic_matches(cmd_db_header))
132 return -EINVAL;
133
134 return 0;
135}
136EXPORT_SYMBOL(cmd_db_ready);
137
138static int cmd_db_get_header(const char *id, const struct entry_header **eh,
139 const struct rsc_hdr **rh)
140{
141 const struct rsc_hdr *rsc_hdr;
142 const struct entry_header *ent;
143 int ret, i, j;
144 u8 query[8];
145
146 ret = cmd_db_ready();
147 if (ret)
148 return ret;
149
150
151 strncpy(query, id, sizeof(query));
152
153 for (i = 0; i < MAX_SLV_ID; i++) {
154 rsc_hdr = &cmd_db_header->header[i];
155 if (!rsc_hdr->slv_id)
156 break;
157
158 ent = rsc_to_entry_header(rsc_hdr);
159 for (j = 0; j < le16_to_cpu(rsc_hdr->cnt); j++, ent++) {
160 if (memcmp(ent->id, query, sizeof(ent->id)) == 0) {
161 if (eh)
162 *eh = ent;
163 if (rh)
164 *rh = rsc_hdr;
165 return 0;
166 }
167 }
168 }
169
170 return -ENODEV;
171}
172
173
174
175
176
177
178
179
180
181
182
183u32 cmd_db_read_addr(const char *id)
184{
185 int ret;
186 const struct entry_header *ent;
187
188 ret = cmd_db_get_header(id, &ent, NULL);
189
190 return ret < 0 ? 0 : le32_to_cpu(ent->addr);
191}
192EXPORT_SYMBOL(cmd_db_read_addr);
193
194
195
196
197
198
199
200
201
202const void *cmd_db_read_aux_data(const char *id, size_t *len)
203{
204 int ret;
205 const struct entry_header *ent;
206 const struct rsc_hdr *rsc_hdr;
207
208 ret = cmd_db_get_header(id, &ent, &rsc_hdr);
209 if (ret)
210 return ERR_PTR(ret);
211
212 if (len)
213 *len = le16_to_cpu(ent->len);
214
215 return rsc_offset(rsc_hdr, ent);
216}
217EXPORT_SYMBOL(cmd_db_read_aux_data);
218
219
220
221
222
223
224
225
226enum cmd_db_hw_type cmd_db_read_slave_id(const char *id)
227{
228 int ret;
229 const struct entry_header *ent;
230 u32 addr;
231
232 ret = cmd_db_get_header(id, &ent, NULL);
233 if (ret < 0)
234 return CMD_DB_HW_INVALID;
235
236 addr = le32_to_cpu(ent->addr);
237 return (addr >> SLAVE_ID_SHIFT) & SLAVE_ID_MASK;
238}
239EXPORT_SYMBOL(cmd_db_read_slave_id);
240
241#ifdef CONFIG_DEBUG_FS
242static int cmd_db_debugfs_dump(struct seq_file *seq, void *p)
243{
244 int i, j;
245 const struct rsc_hdr *rsc;
246 const struct entry_header *ent;
247 const char *name;
248 u16 len, version;
249 u8 major, minor;
250
251 seq_puts(seq, "Command DB DUMP\n");
252
253 for (i = 0; i < MAX_SLV_ID; i++) {
254 rsc = &cmd_db_header->header[i];
255 if (!rsc->slv_id)
256 break;
257
258 switch (le16_to_cpu(rsc->slv_id)) {
259 case CMD_DB_HW_ARC:
260 name = "ARC";
261 break;
262 case CMD_DB_HW_VRM:
263 name = "VRM";
264 break;
265 case CMD_DB_HW_BCM:
266 name = "BCM";
267 break;
268 default:
269 name = "Unknown";
270 break;
271 }
272
273 version = le16_to_cpu(rsc->version);
274 major = version >> 8;
275 minor = version;
276
277 seq_printf(seq, "Slave %s (v%u.%u)\n", name, major, minor);
278 seq_puts(seq, "-------------------------\n");
279
280 ent = rsc_to_entry_header(rsc);
281 for (j = 0; j < le16_to_cpu(rsc->cnt); j++, ent++) {
282 seq_printf(seq, "0x%05x: %*pEp", le32_to_cpu(ent->addr),
283 (int)sizeof(ent->id), ent->id);
284
285 len = le16_to_cpu(ent->len);
286 if (len) {
287 seq_printf(seq, " [%*ph]",
288 len, rsc_offset(rsc, ent));
289 }
290 seq_putc(seq, '\n');
291 }
292 }
293
294 return 0;
295}
296
297static int open_cmd_db_debugfs(struct inode *inode, struct file *file)
298{
299 return single_open(file, cmd_db_debugfs_dump, inode->i_private);
300}
301#endif
302
303static const struct file_operations cmd_db_debugfs_ops = {
304#ifdef CONFIG_DEBUG_FS
305 .open = open_cmd_db_debugfs,
306#endif
307 .read = seq_read,
308 .llseek = seq_lseek,
309 .release = single_release,
310};
311
312static int cmd_db_dev_probe(struct platform_device *pdev)
313{
314 struct reserved_mem *rmem;
315 int ret = 0;
316
317 rmem = of_reserved_mem_lookup(pdev->dev.of_node);
318 if (!rmem) {
319 dev_err(&pdev->dev, "failed to acquire memory region\n");
320 return -EINVAL;
321 }
322
323 cmd_db_header = memremap(rmem->base, rmem->size, MEMREMAP_WB);
324 if (!cmd_db_header) {
325 ret = -ENOMEM;
326 cmd_db_header = NULL;
327 return ret;
328 }
329
330 if (!cmd_db_magic_matches(cmd_db_header)) {
331 dev_err(&pdev->dev, "Invalid Command DB Magic\n");
332 return -EINVAL;
333 }
334
335 debugfs_create_file("cmd-db", 0400, NULL, NULL, &cmd_db_debugfs_ops);
336
337 return 0;
338}
339
340static const struct of_device_id cmd_db_match_table[] = {
341 { .compatible = "qcom,cmd-db" },
342 { }
343};
344MODULE_DEVICE_TABLE(of, cmd_db_match_table);
345
346static struct platform_driver cmd_db_dev_driver = {
347 .probe = cmd_db_dev_probe,
348 .driver = {
349 .name = "cmd-db",
350 .of_match_table = cmd_db_match_table,
351 .suppress_bind_attrs = true,
352 },
353};
354
355static int __init cmd_db_device_init(void)
356{
357 return platform_driver_register(&cmd_db_dev_driver);
358}
359arch_initcall(cmd_db_device_init);
360
361MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Command DB Driver");
362MODULE_LICENSE("GPL v2");
363