1
2
3
4#include <linux/kernel.h>
5#include <linux/of.h>
6#include <linux/of_address.h>
7#include <linux/of_platform.h>
8#include <linux/of_reserved_mem.h>
9#include <linux/platform_device.h>
10#include <linux/types.h>
11
12#include <soc/qcom/cmd-db.h>
13
14#define NUM_PRIORITY 2
15#define MAX_SLV_ID 8
16#define SLAVE_ID_MASK 0x7
17#define SLAVE_ID_SHIFT 16
18
19
20
21
22
23
24
25
26
27
28struct entry_header {
29 u8 id[8];
30 __le32 priority[NUM_PRIORITY];
31 __le32 addr;
32 __le16 len;
33 __le16 offset;
34};
35
36
37
38
39
40
41
42
43
44
45
46struct rsc_hdr {
47 __le16 slv_id;
48 __le16 header_offset;
49 __le16 data_offset;
50 __le16 cnt;
51 __le16 version;
52 __le16 reserved[3];
53};
54
55
56
57
58
59
60
61
62
63
64
65struct cmd_db_header {
66 __le32 version;
67 u8 magic[4];
68 struct rsc_hdr header[MAX_SLV_ID];
69 __le32 checksum;
70 __le32 reserved;
71 u8 data[];
72};
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93static const u8 CMD_DB_MAGIC[] = { 0xdb, 0x30, 0x03, 0x0c };
94
95static bool cmd_db_magic_matches(const struct cmd_db_header *header)
96{
97 const u8 *magic = header->magic;
98
99 return memcmp(magic, CMD_DB_MAGIC, ARRAY_SIZE(CMD_DB_MAGIC)) == 0;
100}
101
102static struct cmd_db_header *cmd_db_header;
103
104static inline const void *rsc_to_entry_header(const struct rsc_hdr *hdr)
105{
106 u16 offset = le16_to_cpu(hdr->header_offset);
107
108 return cmd_db_header->data + offset;
109}
110
111static inline void *
112rsc_offset(const struct rsc_hdr *hdr, const struct entry_header *ent)
113{
114 u16 offset = le16_to_cpu(hdr->data_offset);
115 u16 loffset = le16_to_cpu(ent->offset);
116
117 return cmd_db_header->data + offset + loffset;
118}
119
120
121
122
123
124
125int cmd_db_ready(void)
126{
127 if (cmd_db_header == NULL)
128 return -EPROBE_DEFER;
129 else if (!cmd_db_magic_matches(cmd_db_header))
130 return -EINVAL;
131
132 return 0;
133}
134EXPORT_SYMBOL(cmd_db_ready);
135
136static int cmd_db_get_header(const char *id, const struct entry_header **eh,
137 const struct rsc_hdr **rh)
138{
139 const struct rsc_hdr *rsc_hdr;
140 const struct entry_header *ent;
141 int ret, i, j;
142 u8 query[8];
143
144 ret = cmd_db_ready();
145 if (ret)
146 return ret;
147
148
149 strncpy(query, id, sizeof(query));
150
151 for (i = 0; i < MAX_SLV_ID; i++) {
152 rsc_hdr = &cmd_db_header->header[i];
153 if (!rsc_hdr->slv_id)
154 break;
155
156 ent = rsc_to_entry_header(rsc_hdr);
157 for (j = 0; j < le16_to_cpu(rsc_hdr->cnt); j++, ent++) {
158 if (memcmp(ent->id, query, sizeof(ent->id)) == 0) {
159 if (eh)
160 *eh = ent;
161 if (rh)
162 *rh = rsc_hdr;
163 return 0;
164 }
165 }
166 }
167
168 return -ENODEV;
169}
170
171
172
173
174
175
176
177
178
179
180
181u32 cmd_db_read_addr(const char *id)
182{
183 int ret;
184 const struct entry_header *ent;
185
186 ret = cmd_db_get_header(id, &ent, NULL);
187
188 return ret < 0 ? 0 : le32_to_cpu(ent->addr);
189}
190EXPORT_SYMBOL(cmd_db_read_addr);
191
192
193
194
195
196
197
198
199
200const void *cmd_db_read_aux_data(const char *id, size_t *len)
201{
202 int ret;
203 const struct entry_header *ent;
204 const struct rsc_hdr *rsc_hdr;
205
206 ret = cmd_db_get_header(id, &ent, &rsc_hdr);
207 if (ret)
208 return ERR_PTR(ret);
209
210 if (len)
211 *len = le16_to_cpu(ent->len);
212
213 return rsc_offset(rsc_hdr, ent);
214}
215EXPORT_SYMBOL(cmd_db_read_aux_data);
216
217
218
219
220
221
222
223
224enum cmd_db_hw_type cmd_db_read_slave_id(const char *id)
225{
226 int ret;
227 const struct entry_header *ent;
228 u32 addr;
229
230 ret = cmd_db_get_header(id, &ent, NULL);
231 if (ret < 0)
232 return CMD_DB_HW_INVALID;
233
234 addr = le32_to_cpu(ent->addr);
235 return (addr >> SLAVE_ID_SHIFT) & SLAVE_ID_MASK;
236}
237EXPORT_SYMBOL(cmd_db_read_slave_id);
238
239static int cmd_db_dev_probe(struct platform_device *pdev)
240{
241 struct reserved_mem *rmem;
242 int ret = 0;
243
244 rmem = of_reserved_mem_lookup(pdev->dev.of_node);
245 if (!rmem) {
246 dev_err(&pdev->dev, "failed to acquire memory region\n");
247 return -EINVAL;
248 }
249
250 cmd_db_header = memremap(rmem->base, rmem->size, MEMREMAP_WB);
251 if (!cmd_db_header) {
252 ret = -ENOMEM;
253 cmd_db_header = NULL;
254 return ret;
255 }
256
257 if (!cmd_db_magic_matches(cmd_db_header)) {
258 dev_err(&pdev->dev, "Invalid Command DB Magic\n");
259 return -EINVAL;
260 }
261
262 return 0;
263}
264
265static const struct of_device_id cmd_db_match_table[] = {
266 { .compatible = "qcom,cmd-db" },
267 { },
268};
269
270static struct platform_driver cmd_db_dev_driver = {
271 .probe = cmd_db_dev_probe,
272 .driver = {
273 .name = "cmd-db",
274 .of_match_table = cmd_db_match_table,
275 },
276};
277
278static int __init cmd_db_device_init(void)
279{
280 return platform_driver_register(&cmd_db_dev_driver);
281}
282arch_initcall(cmd_db_device_init);
283