altera_sysid.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
  4. * Scott McNutt <smcnutt@psyent.com>
  5. */
  6. #include <common.h>
  7. #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. struct altera_sysid_regs {
  14. u32 id; /* The system build id */
  15. u32 timestamp; /* Timestamp */
  16. };
  17. struct altera_sysid_platdata {
  18. struct altera_sysid_regs *regs;
  19. };
  20. void display_sysid(void)
  21. {
  22. struct udevice *dev;
  23. u32 sysid[2];
  24. struct tm t;
  25. char asc[32];
  26. time_t stamp;
  27. int ret;
  28. /* the first misc device will be used */
  29. ret = uclass_first_device_err(UCLASS_MISC, &dev);
  30. if (ret)
  31. return;
  32. ret = misc_read(dev, 0, &sysid, sizeof(sysid));
  33. if (ret < 0)
  34. return;
  35. stamp = sysid[1];
  36. localtime_r(&stamp, &t);
  37. asctime_r(&t, asc);
  38. printf("SYSID: %08x, %s", sysid[0], asc);
  39. }
  40. int do_sysid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  41. {
  42. display_sysid();
  43. return 0;
  44. }
  45. U_BOOT_CMD(
  46. sysid, 1, 1, do_sysid,
  47. "display Nios-II system id",
  48. ""
  49. );
  50. static int altera_sysid_read(struct udevice *dev,
  51. int offset, void *buf, int size)
  52. {
  53. struct altera_sysid_platdata *plat = dev->platdata;
  54. struct altera_sysid_regs *const regs = plat->regs;
  55. u32 *sysid = buf;
  56. sysid[0] = readl(&regs->id);
  57. sysid[1] = readl(&regs->timestamp);
  58. return 0;
  59. }
  60. static int altera_sysid_ofdata_to_platdata(struct udevice *dev)
  61. {
  62. struct altera_sysid_platdata *plat = dev_get_platdata(dev);
  63. plat->regs = map_physmem(devfdt_get_addr(dev),
  64. sizeof(struct altera_sysid_regs),
  65. MAP_NOCACHE);
  66. return 0;
  67. }
  68. static const struct misc_ops altera_sysid_ops = {
  69. .read = altera_sysid_read,
  70. };
  71. static const struct udevice_id altera_sysid_ids[] = {
  72. { .compatible = "altr,sysid-1.0" },
  73. {}
  74. };
  75. U_BOOT_DRIVER(altera_sysid) = {
  76. .name = "altera_sysid",
  77. .id = UCLASS_MISC,
  78. .of_match = altera_sysid_ids,
  79. .ofdata_to_platdata = altera_sysid_ofdata_to_platdata,
  80. .platdata_auto_alloc_size = sizeof(struct altera_sysid_platdata),
  81. .ops = &altera_sysid_ops,
  82. };