blob: 21e64fa3e6fef88bdc2b0a6a0a16b83761bb35ec [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Thomas Chou36b9c9a2015-10-14 08:43:31 +08002/*
3 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
4 * Scott McNutt <smcnutt@psyent.com>
Thomas Chou36b9c9a2015-10-14 08:43:31 +08005 */
6
Thomas Chou36b9c9a2015-10-14 08:43:31 +08007#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
14struct altera_sysid_regs {
15 u32 id; /* The system build id */
16 u32 timestamp; /* Timestamp */
17};
18
Simon Glassb75b15b2020-12-03 16:55:23 -070019struct altera_sysid_plat {
Thomas Chou36b9c9a2015-10-14 08:43:31 +080020 struct altera_sysid_regs *regs;
21};
22
23void 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 */
Simon Glassc7298e72016-02-11 13:23:26 -070033 ret = uclass_first_device_err(UCLASS_MISC, &dev);
Thomas Chou36b9c9a2015-10-14 08:43:31 +080034 if (ret)
35 return;
Thomas Chou36b9c9a2015-10-14 08:43:31 +080036 ret = misc_read(dev, 0, &sysid, sizeof(sysid));
Simon Glass587dc402018-11-06 15:21:39 -070037 if (ret < 0)
Thomas Chou36b9c9a2015-10-14 08:43:31 +080038 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
Simon Glassed38aef2020-05-10 11:40:03 -060046int do_sysid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Thomas Chou36b9c9a2015-10-14 08:43:31 +080047{
48 display_sysid();
49 return 0;
50}
51
52U_BOOT_CMD(
53 sysid, 1, 1, do_sysid,
54 "display Nios-II system id",
55 ""
56);
57
58static int altera_sysid_read(struct udevice *dev,
59 int offset, void *buf, int size)
60{
Simon Glass95588622020-12-22 19:30:28 -070061 struct altera_sysid_plat *plat = dev_get_plat(dev);
Thomas Chou36b9c9a2015-10-14 08:43:31 +080062 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
Simon Glassaad29ae2020-12-03 16:55:21 -070071static int altera_sysid_of_to_plat(struct udevice *dev)
Thomas Chou36b9c9a2015-10-14 08:43:31 +080072{
Simon Glassb75b15b2020-12-03 16:55:23 -070073 struct altera_sysid_plat *plat = dev_get_plat(dev);
Thomas Chou36b9c9a2015-10-14 08:43:31 +080074
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +090075 plat->regs = map_physmem(dev_read_addr(dev),
Thomas Chou61a99062015-11-14 11:18:52 +080076 sizeof(struct altera_sysid_regs),
77 MAP_NOCACHE);
Thomas Chou36b9c9a2015-10-14 08:43:31 +080078
79 return 0;
80}
81
82static const struct misc_ops altera_sysid_ops = {
83 .read = altera_sysid_read,
84};
85
86static const struct udevice_id altera_sysid_ids[] = {
Thomas Chouf4c874b2015-10-31 20:54:53 +080087 { .compatible = "altr,sysid-1.0" },
88 {}
Thomas Chou36b9c9a2015-10-14 08:43:31 +080089};
90
91U_BOOT_DRIVER(altera_sysid) = {
92 .name = "altera_sysid",
93 .id = UCLASS_MISC,
94 .of_match = altera_sysid_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -070095 .of_to_plat = altera_sysid_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -070096 .plat_auto = sizeof(struct altera_sysid_plat),
Thomas Chou36b9c9a2015-10-14 08:43:31 +080097 .ops = &altera_sysid_ops,
98};