xref: /linux/drivers/soc/qcom/socinfo.c (revision 73edcd38d7720bb6a761966ea14c0bc64e95dc26)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2009-2017, The Linux Foundation. All rights reserved.
4  * Copyright (c) 2017-2019, Linaro Ltd.
5  */
6 
7 #include <linux/debugfs.h>
8 #include <linux/err.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/random.h>
12 #include <linux/slab.h>
13 #include <linux/soc/qcom/smem.h>
14 #include <linux/string.h>
15 #include <linux/sys_soc.h>
16 #include <linux/types.h>
17 
18 /*
19  * SoC version type with major number in the upper 16 bits and minor
20  * number in the lower 16 bits.
21  */
22 #define SOCINFO_MAJOR(ver) (((ver) >> 16) & 0xffff)
23 #define SOCINFO_MINOR(ver) ((ver) & 0xffff)
24 #define SOCINFO_VERSION(maj, min)  ((((maj) & 0xffff) << 16)|((min) & 0xffff))
25 
26 #define SMEM_SOCINFO_BUILD_ID_LENGTH           32
27 
28 /*
29  * SMEM item id, used to acquire handles to respective
30  * SMEM region.
31  */
32 #define SMEM_HW_SW_BUILD_ID            137
33 
34 #ifdef CONFIG_DEBUG_FS
35 #define SMEM_IMAGE_VERSION_BLOCKS_COUNT        32
36 #define SMEM_IMAGE_VERSION_SIZE                4096
37 #define SMEM_IMAGE_VERSION_NAME_SIZE           75
38 #define SMEM_IMAGE_VERSION_VARIANT_SIZE        20
39 #define SMEM_IMAGE_VERSION_OEM_SIZE            32
40 
41 /*
42  * SMEM Image table indices
43  */
44 #define SMEM_IMAGE_TABLE_BOOT_INDEX     0
45 #define SMEM_IMAGE_TABLE_TZ_INDEX       1
46 #define SMEM_IMAGE_TABLE_RPM_INDEX      3
47 #define SMEM_IMAGE_TABLE_APPS_INDEX     10
48 #define SMEM_IMAGE_TABLE_MPSS_INDEX     11
49 #define SMEM_IMAGE_TABLE_ADSP_INDEX     12
50 #define SMEM_IMAGE_TABLE_CNSS_INDEX     13
51 #define SMEM_IMAGE_TABLE_VIDEO_INDEX    14
52 #define SMEM_IMAGE_VERSION_TABLE       469
53 
54 /*
55  * SMEM Image table names
56  */
57 static const char *const socinfo_image_names[] = {
58 	[SMEM_IMAGE_TABLE_ADSP_INDEX] = "adsp",
59 	[SMEM_IMAGE_TABLE_APPS_INDEX] = "apps",
60 	[SMEM_IMAGE_TABLE_BOOT_INDEX] = "boot",
61 	[SMEM_IMAGE_TABLE_CNSS_INDEX] = "cnss",
62 	[SMEM_IMAGE_TABLE_MPSS_INDEX] = "mpss",
63 	[SMEM_IMAGE_TABLE_RPM_INDEX] = "rpm",
64 	[SMEM_IMAGE_TABLE_TZ_INDEX] = "tz",
65 	[SMEM_IMAGE_TABLE_VIDEO_INDEX] = "video",
66 };
67 
68 static const char *const pmic_models[] = {
69 	[0]  = "Unknown PMIC model",
70 	[9]  = "PM8994",
71 	[11] = "PM8916",
72 	[13] = "PM8058",
73 	[14] = "PM8028",
74 	[15] = "PM8901",
75 	[16] = "PM8027",
76 	[17] = "ISL9519",
77 	[18] = "PM8921",
78 	[19] = "PM8018",
79 	[20] = "PM8015",
80 	[21] = "PM8014",
81 	[22] = "PM8821",
82 	[23] = "PM8038",
83 	[24] = "PM8922",
84 	[25] = "PM8917",
85 };
86 #endif /* CONFIG_DEBUG_FS */
87 
88 /* Socinfo SMEM item structure */
89 struct socinfo {
90 	__le32 fmt;
91 	__le32 id;
92 	__le32 ver;
93 	char build_id[SMEM_SOCINFO_BUILD_ID_LENGTH];
94 	/* Version 2 */
95 	__le32 raw_id;
96 	__le32 raw_ver;
97 	/* Version 3 */
98 	__le32 hw_plat;
99 	/* Version 4 */
100 	__le32 plat_ver;
101 	/* Version 5 */
102 	__le32 accessory_chip;
103 	/* Version 6 */
104 	__le32 hw_plat_subtype;
105 	/* Version 7 */
106 	__le32 pmic_model;
107 	__le32 pmic_die_rev;
108 	/* Version 8 */
109 	__le32 pmic_model_1;
110 	__le32 pmic_die_rev_1;
111 	__le32 pmic_model_2;
112 	__le32 pmic_die_rev_2;
113 	/* Version 9 */
114 	__le32 foundry_id;
115 	/* Version 10 */
116 	__le32 serial_num;
117 	/* Version 11 */
118 	__le32 num_pmics;
119 	__le32 pmic_array_offset;
120 	/* Version 12 */
121 	__le32 chip_family;
122 	__le32 raw_device_family;
123 	__le32 raw_device_num;
124 };
125 
126 #ifdef CONFIG_DEBUG_FS
127 struct socinfo_params {
128 	u32 raw_device_family;
129 	u32 hw_plat_subtype;
130 	u32 accessory_chip;
131 	u32 raw_device_num;
132 	u32 chip_family;
133 	u32 foundry_id;
134 	u32 plat_ver;
135 	u32 raw_ver;
136 	u32 hw_plat;
137 	u32 fmt;
138 };
139 
140 struct smem_image_version {
141 	char name[SMEM_IMAGE_VERSION_NAME_SIZE];
142 	char variant[SMEM_IMAGE_VERSION_VARIANT_SIZE];
143 	char pad;
144 	char oem[SMEM_IMAGE_VERSION_OEM_SIZE];
145 };
146 #endif /* CONFIG_DEBUG_FS */
147 
148 struct qcom_socinfo {
149 	struct soc_device *soc_dev;
150 	struct soc_device_attribute attr;
151 #ifdef CONFIG_DEBUG_FS
152 	struct dentry *dbg_root;
153 	struct socinfo_params info;
154 #endif /* CONFIG_DEBUG_FS */
155 };
156 
157 struct soc_id {
158 	unsigned int id;
159 	const char *name;
160 };
161 
162 static const struct soc_id soc_id[] = {
163 	{ 87, "MSM8960" },
164 	{ 109, "APQ8064" },
165 	{ 122, "MSM8660A" },
166 	{ 123, "MSM8260A" },
167 	{ 124, "APQ8060A" },
168 	{ 126, "MSM8974" },
169 	{ 130, "MPQ8064" },
170 	{ 138, "MSM8960AB" },
171 	{ 139, "APQ8060AB" },
172 	{ 140, "MSM8260AB" },
173 	{ 141, "MSM8660AB" },
174 	{ 178, "APQ8084" },
175 	{ 184, "APQ8074" },
176 	{ 185, "MSM8274" },
177 	{ 186, "MSM8674" },
178 	{ 194, "MSM8974PRO" },
179 	{ 206, "MSM8916" },
180 	{ 208, "APQ8074-AA" },
181 	{ 209, "APQ8074-AB" },
182 	{ 210, "APQ8074PRO" },
183 	{ 211, "MSM8274-AA" },
184 	{ 212, "MSM8274-AB" },
185 	{ 213, "MSM8274PRO" },
186 	{ 214, "MSM8674-AA" },
187 	{ 215, "MSM8674-AB" },
188 	{ 216, "MSM8674PRO" },
189 	{ 217, "MSM8974-AA" },
190 	{ 218, "MSM8974-AB" },
191 	{ 233, "MSM8936" },
192 	{ 239, "MSM8939" },
193 	{ 240, "APQ8036" },
194 	{ 241, "APQ8039" },
195 	{ 246, "MSM8996" },
196 	{ 247, "APQ8016" },
197 	{ 248, "MSM8216" },
198 	{ 249, "MSM8116" },
199 	{ 250, "MSM8616" },
200 	{ 291, "APQ8096" },
201 	{ 305, "MSM8996SG" },
202 	{ 310, "MSM8996AU" },
203 	{ 311, "APQ8096AU" },
204 	{ 312, "APQ8096SG" },
205 	{ 318, "SDM630" },
206 	{ 321, "SDM845" },
207 	{ 341, "SDA845" },
208 };
209 
210 static const char *socinfo_machine(struct device *dev, unsigned int id)
211 {
212 	int idx;
213 
214 	for (idx = 0; idx < ARRAY_SIZE(soc_id); idx++) {
215 		if (soc_id[idx].id == id)
216 			return soc_id[idx].name;
217 	}
218 
219 	return NULL;
220 }
221 
222 #ifdef CONFIG_DEBUG_FS
223 
224 #define QCOM_OPEN(name, _func)						\
225 static int qcom_open_##name(struct inode *inode, struct file *file)	\
226 {									\
227 	return single_open(file, _func, inode->i_private);		\
228 }									\
229 									\
230 static const struct file_operations qcom_ ##name## _ops = {		\
231 	.open = qcom_open_##name,					\
232 	.read = seq_read,						\
233 	.llseek = seq_lseek,						\
234 	.release = single_release,					\
235 }
236 
237 #define DEBUGFS_ADD(info, name)						\
238 	debugfs_create_file(__stringify(name), 0400,			\
239 			    qcom_socinfo->dbg_root,			\
240 			    info, &qcom_ ##name## _ops)
241 
242 
243 static int qcom_show_build_id(struct seq_file *seq, void *p)
244 {
245 	struct socinfo *socinfo = seq->private;
246 
247 	seq_printf(seq, "%s\n", socinfo->build_id);
248 
249 	return 0;
250 }
251 
252 static int qcom_show_pmic_model(struct seq_file *seq, void *p)
253 {
254 	struct socinfo *socinfo = seq->private;
255 	int model = SOCINFO_MINOR(le32_to_cpu(socinfo->pmic_model));
256 
257 	if (model < 0)
258 		return -EINVAL;
259 
260 	seq_printf(seq, "%s\n", pmic_models[model]);
261 
262 	return 0;
263 }
264 
265 static int qcom_show_pmic_die_revision(struct seq_file *seq, void *p)
266 {
267 	struct socinfo *socinfo = seq->private;
268 
269 	seq_printf(seq, "%u.%u\n",
270 		   SOCINFO_MAJOR(le32_to_cpu(socinfo->pmic_die_rev)),
271 		   SOCINFO_MINOR(le32_to_cpu(socinfo->pmic_die_rev)));
272 
273 	return 0;
274 }
275 
276 QCOM_OPEN(build_id, qcom_show_build_id);
277 QCOM_OPEN(pmic_model, qcom_show_pmic_model);
278 QCOM_OPEN(pmic_die_rev, qcom_show_pmic_die_revision);
279 
280 #define DEFINE_IMAGE_OPS(type)					\
281 static int show_image_##type(struct seq_file *seq, void *p)		  \
282 {								  \
283 	struct smem_image_version *image_version = seq->private;  \
284 	seq_puts(seq, image_version->type);			  \
285 	seq_putc(seq, '\n');					  \
286 	return 0;						  \
287 }								  \
288 static int open_image_##type(struct inode *inode, struct file *file)	  \
289 {									  \
290 	return single_open(file, show_image_##type, inode->i_private); \
291 }									  \
292 									  \
293 static const struct file_operations qcom_image_##type##_ops = {	  \
294 	.open = open_image_##type,					  \
295 	.read = seq_read,						  \
296 	.llseek = seq_lseek,						  \
297 	.release = single_release,					  \
298 }
299 
300 DEFINE_IMAGE_OPS(name);
301 DEFINE_IMAGE_OPS(variant);
302 DEFINE_IMAGE_OPS(oem);
303 
304 static void socinfo_debugfs_init(struct qcom_socinfo *qcom_socinfo,
305 				 struct socinfo *info)
306 {
307 	struct smem_image_version *versions;
308 	struct dentry *dentry;
309 	size_t size;
310 	int i;
311 
312 	qcom_socinfo->dbg_root = debugfs_create_dir("qcom_socinfo", NULL);
313 
314 	qcom_socinfo->info.fmt = __le32_to_cpu(info->fmt);
315 
316 	switch (qcom_socinfo->info.fmt) {
317 	case SOCINFO_VERSION(0, 12):
318 		qcom_socinfo->info.chip_family =
319 			__le32_to_cpu(info->chip_family);
320 		qcom_socinfo->info.raw_device_family =
321 			__le32_to_cpu(info->raw_device_family);
322 		qcom_socinfo->info.raw_device_num =
323 			__le32_to_cpu(info->raw_device_num);
324 
325 		debugfs_create_x32("chip_family", 0400, qcom_socinfo->dbg_root,
326 				   &qcom_socinfo->info.chip_family);
327 		debugfs_create_x32("raw_device_family", 0400,
328 				   qcom_socinfo->dbg_root,
329 				   &qcom_socinfo->info.raw_device_family);
330 		debugfs_create_x32("raw_device_number", 0400,
331 				   qcom_socinfo->dbg_root,
332 				   &qcom_socinfo->info.raw_device_num);
333 		/* Fall through */
334 	case SOCINFO_VERSION(0, 11):
335 	case SOCINFO_VERSION(0, 10):
336 	case SOCINFO_VERSION(0, 9):
337 		qcom_socinfo->info.foundry_id = __le32_to_cpu(info->foundry_id);
338 
339 		debugfs_create_u32("foundry_id", 0400, qcom_socinfo->dbg_root,
340 				   &qcom_socinfo->info.foundry_id);
341 		/* Fall through */
342 	case SOCINFO_VERSION(0, 8):
343 	case SOCINFO_VERSION(0, 7):
344 		DEBUGFS_ADD(info, pmic_model);
345 		DEBUGFS_ADD(info, pmic_die_rev);
346 		/* Fall through */
347 	case SOCINFO_VERSION(0, 6):
348 		qcom_socinfo->info.hw_plat_subtype =
349 			__le32_to_cpu(info->hw_plat_subtype);
350 
351 		debugfs_create_u32("hardware_platform_subtype", 0400,
352 				   qcom_socinfo->dbg_root,
353 				   &qcom_socinfo->info.hw_plat_subtype);
354 		/* Fall through */
355 	case SOCINFO_VERSION(0, 5):
356 		qcom_socinfo->info.accessory_chip =
357 			__le32_to_cpu(info->accessory_chip);
358 
359 		debugfs_create_u32("accessory_chip", 0400,
360 				   qcom_socinfo->dbg_root,
361 				   &qcom_socinfo->info.accessory_chip);
362 		/* Fall through */
363 	case SOCINFO_VERSION(0, 4):
364 		qcom_socinfo->info.plat_ver = __le32_to_cpu(info->plat_ver);
365 
366 		debugfs_create_u32("platform_version", 0400,
367 				   qcom_socinfo->dbg_root,
368 				   &qcom_socinfo->info.plat_ver);
369 		/* Fall through */
370 	case SOCINFO_VERSION(0, 3):
371 		qcom_socinfo->info.hw_plat = __le32_to_cpu(info->hw_plat);
372 
373 		debugfs_create_u32("hardware_platform", 0400,
374 				   qcom_socinfo->dbg_root,
375 				   &qcom_socinfo->info.hw_plat);
376 		/* Fall through */
377 	case SOCINFO_VERSION(0, 2):
378 		qcom_socinfo->info.raw_ver  = __le32_to_cpu(info->raw_ver);
379 
380 		debugfs_create_u32("raw_version", 0400, qcom_socinfo->dbg_root,
381 				   &qcom_socinfo->info.raw_ver);
382 		/* Fall through */
383 	case SOCINFO_VERSION(0, 1):
384 		DEBUGFS_ADD(info, build_id);
385 		break;
386 	}
387 
388 	versions = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_IMAGE_VERSION_TABLE,
389 				 &size);
390 
391 	for (i = 0; i < ARRAY_SIZE(socinfo_image_names); i++) {
392 		if (!socinfo_image_names[i])
393 			continue;
394 
395 		dentry = debugfs_create_dir(socinfo_image_names[i],
396 					    qcom_socinfo->dbg_root);
397 		debugfs_create_file("name", 0400, dentry, &versions[i],
398 				    &qcom_image_name_ops);
399 		debugfs_create_file("variant", 0400, dentry, &versions[i],
400 				    &qcom_image_variant_ops);
401 		debugfs_create_file("oem", 0400, dentry, &versions[i],
402 				    &qcom_image_oem_ops);
403 	}
404 }
405 
406 static void socinfo_debugfs_exit(struct qcom_socinfo *qcom_socinfo)
407 {
408 	debugfs_remove_recursive(qcom_socinfo->dbg_root);
409 }
410 #else
411 static void socinfo_debugfs_init(struct qcom_socinfo *qcom_socinfo,
412 				 struct socinfo *info)
413 {
414 }
415 static void socinfo_debugfs_exit(struct qcom_socinfo *qcom_socinfo) {  }
416 #endif /* CONFIG_DEBUG_FS */
417 
418 static int qcom_socinfo_probe(struct platform_device *pdev)
419 {
420 	struct qcom_socinfo *qs;
421 	struct socinfo *info;
422 	size_t item_size;
423 
424 	info = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_HW_SW_BUILD_ID,
425 			      &item_size);
426 	if (IS_ERR(info)) {
427 		dev_err(&pdev->dev, "Couldn't find socinfo\n");
428 		return PTR_ERR(info);
429 	}
430 
431 	qs = devm_kzalloc(&pdev->dev, sizeof(*qs), GFP_KERNEL);
432 	if (!qs)
433 		return -ENOMEM;
434 
435 	qs->attr.family = "Snapdragon";
436 	qs->attr.machine = socinfo_machine(&pdev->dev,
437 					   le32_to_cpu(info->id));
438 	qs->attr.soc_id = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%u",
439 					 le32_to_cpu(info->id));
440 	qs->attr.revision = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%u.%u",
441 					   SOCINFO_MAJOR(le32_to_cpu(info->ver)),
442 					   SOCINFO_MINOR(le32_to_cpu(info->ver)));
443 	if (offsetof(struct socinfo, serial_num) <= item_size)
444 		qs->attr.serial_number = devm_kasprintf(&pdev->dev, GFP_KERNEL,
445 							"%u",
446 							le32_to_cpu(info->serial_num));
447 
448 	qs->soc_dev = soc_device_register(&qs->attr);
449 	if (IS_ERR(qs->soc_dev))
450 		return PTR_ERR(qs->soc_dev);
451 
452 	socinfo_debugfs_init(qs, info);
453 
454 	/* Feed the soc specific unique data into entropy pool */
455 	add_device_randomness(info, item_size);
456 
457 	platform_set_drvdata(pdev, qs->soc_dev);
458 
459 	return 0;
460 }
461 
462 static int qcom_socinfo_remove(struct platform_device *pdev)
463 {
464 	struct qcom_socinfo *qs = platform_get_drvdata(pdev);
465 
466 	soc_device_unregister(qs->soc_dev);
467 
468 	socinfo_debugfs_exit(qs);
469 
470 	return 0;
471 }
472 
473 static struct platform_driver qcom_socinfo_driver = {
474 	.probe = qcom_socinfo_probe,
475 	.remove = qcom_socinfo_remove,
476 	.driver  = {
477 		.name = "qcom-socinfo",
478 	},
479 };
480 
481 module_platform_driver(qcom_socinfo_driver);
482 
483 MODULE_DESCRIPTION("Qualcomm SoCinfo driver");
484 MODULE_LICENSE("GPL v2");
485 MODULE_ALIAS("platform:qcom-socinfo");
486