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