blob: 0c2ba4dfbb0b418ceaff4934a9f1906daf475a5c [file] [log] [blame]
Lina Iyer1c5ab792017-02-01 13:34:54 -07001/* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
Mahesh Sivasubramaniancb649522016-08-19 14:04:44 -06002 *
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/module.h>
14#include <linux/types.h>
15#include <linux/platform_device.h>
16#include <linux/slab.h>
17#include <linux/io.h>
18#include <linux/of.h>
19#include <linux/of_address.h>
20#include <linux/of_platform.h>
21#include <linux/kernel.h>
22#include <soc/qcom/cmd-db.h>
23
24#define RESOURCE_ID_LEN 8
25#define NUM_PRIORITY 2
26#define MAX_SLV_ID 8
27#define CMD_DB_MAGIC 0x0C0330DBUL
28#define SLAVE_ID_MASK 0x7
29#define SLAVE_ID_SHIFT 16
Lina Iyer1c5ab792017-02-01 13:34:54 -070030#define CMD_DB_STANDALONE_MASK BIT(0)
Mahesh Sivasubramaniancb649522016-08-19 14:04:44 -060031
32struct entry_header {
33 uint64_t res_id;
34 u32 priority[NUM_PRIORITY];
35 u32 addr;
36 u16 len;
37 u16 offset;
38};
39
40struct rsc_hdr {
41 u16 slv_id;
42 u16 header_offset; /* Entry header offset from data */
43 u16 data_offset; /* Entry offset for data location */
44 u16 cnt; /* Number of entries for HW type */
45 u16 version; /* MSB is Major and LSB is Minor version
46 * identifies the HW type of Aux Data
47 */
48 u16 reserved[3];
49};
50
51struct cmd_db_header {
52 u32 version;
53 u32 magic_num;
54 struct rsc_hdr header[MAX_SLV_ID];
55 u32 check_sum;
56 u32 reserved;
57 u8 data[];
58};
59
60struct cmd_db_entry {
61 const char resource_id[RESOURCE_ID_LEN + 1]; /* Unique id per entry */
62 const u32 addr; /* TCS Addr Slave ID + Offset address */
63 const u32 priority[NUM_PRIORITY]; /* Bitmask for DRV IDs */
64 u32 len; /* Aux data len */
65 u16 version;
66 u8 data[];
67};
68
69/* CMD DB QUERY TYPES */
70enum cmd_db_query_type {
71 CMD_DB_QUERY_RES_ID = 0,
72 CMD_DB_QUERY_ADDRESS,
73 CMD_DB_QUERY_INVALID,
74 CMD_DB_QUERY_MAX = 0x7ffffff,
75};
76
77static void __iomem *start_addr;
78static struct cmd_db_header *cmd_db_header;
79static int cmd_db_status = -EPROBE_DEFER;
80
81static u64 cmd_db_get_u64_id(const char *id)
82{
83 uint64_t rsc_id = 0;
84 uint8_t *ch = (uint8_t *)&rsc_id;
85 int i;
86
87 for (i = 0; ((i < sizeof(rsc_id)) && id[i]); i++)
88 ch[i] = id[i];
89
90 return rsc_id;
91}
92
93static int cmd_db_get_header(u64 query, struct entry_header *eh,
94 struct rsc_hdr *rh, bool use_addr)
95{
96 struct rsc_hdr *rsc_hdr;
97 int i, j;
98
99 if (!cmd_db_header)
100 return -EPROBE_DEFER;
101
102 if (!eh || !rh)
103 return -EINVAL;
104
105 rsc_hdr = &cmd_db_header->header[0];
106
107 for (i = 0; i < MAX_SLV_ID ; i++, rsc_hdr++) {
108 struct entry_header *ent;
109
110 if (!rsc_hdr->slv_id)
111 break;
112
113 ent = (struct entry_header *)(start_addr
114 + sizeof(*cmd_db_header)
115 + rsc_hdr->header_offset);
116
117 for (j = 0; j < rsc_hdr->cnt; j++, ent++) {
118 if (use_addr) {
119 if (ent->addr == (u32)(query))
120 break;
121 } else if (ent->res_id == query)
122 break;
123 }
124
125 if (j < rsc_hdr->cnt) {
126 memcpy(eh, ent, sizeof(*ent));
127 memcpy(rh, &cmd_db_header->header[i], sizeof(*rh));
128 return 0;
129 }
130 }
131 return -ENODEV;
132}
133
134static int cmd_db_get_header_by_addr(u32 addr,
135 struct entry_header *ent_hdr,
136 struct rsc_hdr *rsc_hdr)
137{
138 return cmd_db_get_header((u64)addr, ent_hdr, rsc_hdr, true);
139}
140
141static int cmd_db_get_header_by_rsc_id(const char *resource_id,
142 struct entry_header *ent_hdr,
143 struct rsc_hdr *rsc_hdr)
144{
145 u64 rsc_id = cmd_db_get_u64_id(resource_id);
146
147 return cmd_db_get_header(rsc_id, ent_hdr, rsc_hdr, false);
148}
149
150u32 cmd_db_get_addr(const char *resource_id)
151{
152 int ret;
153 struct entry_header ent;
154 struct rsc_hdr rsc_hdr;
155
156 ret = cmd_db_get_header_by_rsc_id(resource_id, &ent, &rsc_hdr);
157
158 return ret < 0 ? 0 : ent.addr;
159}
160
161bool cmd_db_get_priority(u32 addr, u8 drv_id)
162{
163 int ret;
164 struct entry_header ent;
165 struct rsc_hdr rsc_hdr;
166
167 ret = cmd_db_get_header_by_addr(addr, &ent, &rsc_hdr);
168
169 return ret < 0 ? false : (bool)(ent.priority[0] & (1 << drv_id));
170
171}
172int cmd_db_get_aux_data(const char *resource_id, u8 *data, int len)
173{
174 int ret;
175 struct entry_header ent;
176 struct rsc_hdr rsc_hdr;
177
178 if (!data)
179 return -EINVAL;
180
181 ret = cmd_db_get_header_by_rsc_id(resource_id, &ent, &rsc_hdr);
182
183 if (ret)
184 return ret;
185
186 if (ent.len < len)
187 return -EINVAL;
188
189 len = (ent.len < len) ? ent.len : len;
190
191 memcpy_fromio(data,
192 start_addr + sizeof(*cmd_db_header)
193 + rsc_hdr.data_offset + ent.offset,
194 len);
195 return len;
196}
197
198int cmd_db_get_aux_data_len(const char *resource_id)
199{
200 int ret;
201 struct entry_header ent;
202 struct rsc_hdr rsc_hdr;
203
204 ret = cmd_db_get_header_by_rsc_id(resource_id, &ent, &rsc_hdr);
205
206 return ret < 0 ? 0 : ent.len;
207}
208
209u16 cmd_db_get_version(const char *resource_id)
210{
211 int ret;
212 struct entry_header ent;
213 struct rsc_hdr rsc_hdr;
214
215 ret = cmd_db_get_header_by_rsc_id(resource_id, &ent, &rsc_hdr);
216 return ret < 0 ? 0 : rsc_hdr.version;
217}
218
219int cmd_db_ready(void)
220{
221 return cmd_db_status;
222}
223
Lina Iyer1c5ab792017-02-01 13:34:54 -0700224int cmd_db_is_standalone(void)
225{
226 if (cmd_db_status < 0)
227 return cmd_db_status;
228
229 return !!(cmd_db_header->reserved & CMD_DB_STANDALONE_MASK);
230}
231
Mahesh Sivasubramaniancb649522016-08-19 14:04:44 -0600232int cmd_db_get_slave_id(const char *resource_id)
233{
234 int ret;
235 struct entry_header ent;
236 struct rsc_hdr rsc_hdr;
237
238 ret = cmd_db_get_header_by_rsc_id(resource_id, &ent, &rsc_hdr);
239 return ret < 0 ? 0 : (ent.addr >> SLAVE_ID_SHIFT) & SLAVE_ID_MASK;
240}
241
242static int cmd_db_dev_probe(struct platform_device *pdev)
243{
244 struct resource *res;
245
246 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
247 if (!res) {
248 cmd_db_status = -ENOMEM;
249 goto failed;
250 }
251
252 start_addr = devm_ioremap_resource(&pdev->dev, res);
253
254 cmd_db_header = devm_kzalloc(&pdev->dev, sizeof(*cmd_db_header),
255 GFP_KERNEL);
256
257 if (!cmd_db_header) {
258 cmd_db_status = -ENOMEM;
259 goto failed;
260 }
261
262 memcpy(cmd_db_header, start_addr, sizeof(*cmd_db_header));
263
264 if (cmd_db_header->magic_num != CMD_DB_MAGIC) {
265 pr_err("%s(): Invalid Magic\n", __func__);
266 cmd_db_status = -EINVAL;
267 goto failed;
268 }
269 cmd_db_status = 0;
270 of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
Lina Iyer1c5ab792017-02-01 13:34:54 -0700271
272 if (cmd_db_is_standalone() == 1)
273 pr_info("Command DB is initialized in standalone mode.\n");
274
Mahesh Sivasubramaniancb649522016-08-19 14:04:44 -0600275failed:
276 return cmd_db_status;
277}
278
279static const struct of_device_id cmd_db_match_table[] = {
280 {.compatible = "qcom,cmd-db"},
281 {},
282};
283
284static struct platform_driver cmd_db_dev_driver = {
285 .probe = cmd_db_dev_probe,
286 .driver = {
287 .name = "cmd-db",
288 .owner = THIS_MODULE,
289 .of_match_table = cmd_db_match_table,
290 },
291};
292
293int __init cmd_db_device_init(void)
294{
295 return platform_driver_register(&cmd_db_dev_driver);
296}
297arch_initcall(cmd_db_device_init);