Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2004, Psyent Corporation <www.psyent.com> |
| 4 | * Scott McNutt <smcnutt@psyent.com> |
Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 7 | #include <common.h> |
Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 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 | |
| 15 | struct altera_sysid_regs { |
| 16 | u32 id; /* The system build id */ |
| 17 | u32 timestamp; /* Timestamp */ |
| 18 | }; |
| 19 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 20 | struct altera_sysid_plat { |
Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 21 | struct altera_sysid_regs *regs; |
| 22 | }; |
| 23 | |
| 24 | void 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 Glass | c7298e7 | 2016-02-11 13:23:26 -0700 | [diff] [blame] | 34 | ret = uclass_first_device_err(UCLASS_MISC, &dev); |
Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 35 | if (ret) |
| 36 | return; |
Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 37 | ret = misc_read(dev, 0, &sysid, sizeof(sysid)); |
Simon Glass | 587dc40 | 2018-11-06 15:21:39 -0700 | [diff] [blame] | 38 | if (ret < 0) |
Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 39 | 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 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 47 | int do_sysid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 48 | { |
| 49 | display_sysid(); |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | U_BOOT_CMD( |
| 54 | sysid, 1, 1, do_sysid, |
| 55 | "display Nios-II system id", |
| 56 | "" |
| 57 | ); |
| 58 | |
| 59 | static int altera_sysid_read(struct udevice *dev, |
| 60 | int offset, void *buf, int size) |
| 61 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 62 | struct altera_sysid_plat *plat = dev_get_plat(dev); |
Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 63 | struct altera_sysid_regs *const regs = plat->regs; |
| 64 | u32 *sysid = buf; |
| 65 | |
| 66 | sysid[0] = readl(®s->id); |
| 67 | sysid[1] = readl(®s->timestamp); |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 72 | static int altera_sysid_of_to_plat(struct udevice *dev) |
Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 73 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 74 | struct altera_sysid_plat *plat = dev_get_plat(dev); |
Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 75 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 76 | plat->regs = map_physmem(dev_read_addr(dev), |
Thomas Chou | 61a9906 | 2015-11-14 11:18:52 +0800 | [diff] [blame] | 77 | sizeof(struct altera_sysid_regs), |
| 78 | MAP_NOCACHE); |
Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static const struct misc_ops altera_sysid_ops = { |
| 84 | .read = altera_sysid_read, |
| 85 | }; |
| 86 | |
| 87 | static const struct udevice_id altera_sysid_ids[] = { |
Thomas Chou | f4c874b | 2015-10-31 20:54:53 +0800 | [diff] [blame] | 88 | { .compatible = "altr,sysid-1.0" }, |
| 89 | {} |
Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | U_BOOT_DRIVER(altera_sysid) = { |
| 93 | .name = "altera_sysid", |
| 94 | .id = UCLASS_MISC, |
| 95 | .of_match = altera_sysid_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 96 | .of_to_plat = altera_sysid_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 97 | .plat_auto = sizeof(struct altera_sysid_plat), |
Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 98 | .ops = &altera_sysid_ops, |
| 99 | }; |