altera_sysid.c 2.0 KB

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