1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
4  * Scott McNutt <smcnutt@psyent.com>
5  */
6 
7 #include <command.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <misc.h>
11 #include <linux/time.h>
12 #include <asm/io.h>
13 
14 struct altera_sysid_regs {
15 	u32	id;		/* The system build id */
16 	u32	timestamp;	/* Timestamp */
17 };
18 
19 struct altera_sysid_plat {
20 	struct altera_sysid_regs *regs;
21 };
22 
display_sysid(void)23 void display_sysid(void)
24 {
25 	struct udevice *dev;
26 	u32 sysid[2];
27 	struct tm t;
28 	char asc[32];
29 	time_t stamp;
30 	int ret;
31 
32 	/* the first misc device will be used */
33 	ret = uclass_first_device_err(UCLASS_MISC, &dev);
34 	if (ret)
35 		return;
36 	ret = misc_read(dev, 0, &sysid, sizeof(sysid));
37 	if (ret < 0)
38 		return;
39 
40 	stamp = sysid[1];
41 	localtime_r(&stamp, &t);
42 	asctime_r(&t, asc);
43 	printf("SYSID: %08x, %s", sysid[0], asc);
44 }
45 
do_sysid(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])46 int do_sysid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
47 {
48 	display_sysid();
49 	return 0;
50 }
51 
52 U_BOOT_CMD(
53 	sysid,	1,	1,	do_sysid,
54 	"display Nios-II system id",
55 	""
56 );
57 
altera_sysid_read(struct udevice * dev,int offset,void * buf,int size)58 static int altera_sysid_read(struct udevice *dev,
59 			     int offset, void *buf, int size)
60 {
61 	struct altera_sysid_plat *plat = dev_get_plat(dev);
62 	struct altera_sysid_regs *const regs = plat->regs;
63 	u32 *sysid = buf;
64 
65 	sysid[0] = readl(&regs->id);
66 	sysid[1] = readl(&regs->timestamp);
67 
68 	return 0;
69 }
70 
altera_sysid_of_to_plat(struct udevice * dev)71 static int altera_sysid_of_to_plat(struct udevice *dev)
72 {
73 	struct altera_sysid_plat *plat = dev_get_plat(dev);
74 
75 	plat->regs = map_physmem(dev_read_addr(dev),
76 				 sizeof(struct altera_sysid_regs),
77 				 MAP_NOCACHE);
78 
79 	return 0;
80 }
81 
82 static const struct misc_ops altera_sysid_ops = {
83 	.read = altera_sysid_read,
84 };
85 
86 static const struct udevice_id altera_sysid_ids[] = {
87 	{ .compatible = "altr,sysid-1.0" },
88 	{}
89 };
90 
91 U_BOOT_DRIVER(altera_sysid) = {
92 	.name	= "altera_sysid",
93 	.id	= UCLASS_MISC,
94 	.of_match = altera_sysid_ids,
95 	.of_to_plat = altera_sysid_of_to_plat,
96 	.plat_auto	= sizeof(struct altera_sysid_plat),
97 	.ops	= &altera_sysid_ops,
98 };
99