Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | struct altera_sysid_regs { |
| 17 | u32 id; /* The system build id */ |
| 18 | u32 timestamp; /* Timestamp */ |
| 19 | }; |
| 20 | |
| 21 | struct altera_sysid_platdata { |
| 22 | struct altera_sysid_regs *regs; |
| 23 | }; |
| 24 | |
| 25 | void 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 */ |
| 35 | ret = uclass_first_device(UCLASS_MISC, &dev); |
| 36 | if (ret) |
| 37 | return; |
| 38 | if (!dev) |
| 39 | return; |
| 40 | ret = misc_read(dev, 0, &sysid, sizeof(sysid)); |
| 41 | if (ret) |
| 42 | return; |
| 43 | |
| 44 | stamp = sysid[1]; |
| 45 | localtime_r(&stamp, &t); |
| 46 | asctime_r(&t, asc); |
| 47 | printf("SYSID: %08x, %s", sysid[0], asc); |
| 48 | } |
| 49 | |
| 50 | int do_sysid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 51 | { |
| 52 | display_sysid(); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | U_BOOT_CMD( |
| 57 | sysid, 1, 1, do_sysid, |
| 58 | "display Nios-II system id", |
| 59 | "" |
| 60 | ); |
| 61 | |
| 62 | static int altera_sysid_read(struct udevice *dev, |
| 63 | int offset, void *buf, int size) |
| 64 | { |
| 65 | struct altera_sysid_platdata *plat = dev->platdata; |
| 66 | struct altera_sysid_regs *const regs = plat->regs; |
| 67 | u32 *sysid = buf; |
| 68 | |
| 69 | sysid[0] = readl(®s->id); |
| 70 | sysid[1] = readl(®s->timestamp); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int altera_sysid_ofdata_to_platdata(struct udevice *dev) |
| 76 | { |
| 77 | struct altera_sysid_platdata *plat = dev_get_platdata(dev); |
| 78 | |
| 79 | plat->regs = ioremap(dev_get_addr(dev), |
| 80 | sizeof(struct altera_sysid_regs)); |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static const struct misc_ops altera_sysid_ops = { |
| 86 | .read = altera_sysid_read, |
| 87 | }; |
| 88 | |
| 89 | static const struct udevice_id altera_sysid_ids[] = { |
Thomas Chou | f4c874b | 2015-10-31 20:54:53 +0800 | [diff] [blame^] | 90 | { .compatible = "altr,sysid-1.0" }, |
| 91 | {} |
Thomas Chou | 36b9c9a | 2015-10-14 08:43:31 +0800 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | U_BOOT_DRIVER(altera_sysid) = { |
| 95 | .name = "altera_sysid", |
| 96 | .id = UCLASS_MISC, |
| 97 | .of_match = altera_sysid_ids, |
| 98 | .ofdata_to_platdata = altera_sysid_ofdata_to_platdata, |
| 99 | .platdata_auto_alloc_size = sizeof(struct altera_sysid_platdata), |
| 100 | .ops = &altera_sysid_ops, |
| 101 | }; |