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