altera_sysid.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_err(UCLASS_MISC, &dev);
  31. if (ret)
  32. return;
  33. ret = misc_read(dev, 0, &sysid, sizeof(sysid));
  34. if (ret)
  35. return;
  36. stamp = sysid[1];
  37. localtime_r(&stamp, &t);
  38. asctime_r(&t, asc);
  39. printf("SYSID: %08x, %s", sysid[0], asc);
  40. }
  41. int do_sysid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  42. {
  43. display_sysid();
  44. return 0;
  45. }
  46. U_BOOT_CMD(
  47. sysid, 1, 1, do_sysid,
  48. "display Nios-II system id",
  49. ""
  50. );
  51. static int altera_sysid_read(struct udevice *dev,
  52. int offset, void *buf, int size)
  53. {
  54. struct altera_sysid_platdata *plat = dev->platdata;
  55. struct altera_sysid_regs *const regs = plat->regs;
  56. u32 *sysid = buf;
  57. sysid[0] = readl(&regs->id);
  58. sysid[1] = readl(&regs->timestamp);
  59. return 0;
  60. }
  61. static int altera_sysid_ofdata_to_platdata(struct udevice *dev)
  62. {
  63. struct altera_sysid_platdata *plat = dev_get_platdata(dev);
  64. plat->regs = map_physmem(dev_get_addr(dev),
  65. sizeof(struct altera_sysid_regs),
  66. MAP_NOCACHE);
  67. return 0;
  68. }
  69. static const struct misc_ops altera_sysid_ops = {
  70. .read = altera_sysid_read,
  71. };
  72. static const struct udevice_id altera_sysid_ids[] = {
  73. { .compatible = "altr,sysid-1.0" },
  74. {}
  75. };
  76. U_BOOT_DRIVER(altera_sysid) = {
  77. .name = "altera_sysid",
  78. .id = UCLASS_MISC,
  79. .of_match = altera_sysid_ids,
  80. .ofdata_to_platdata = altera_sysid_ofdata_to_platdata,
  81. .platdata_auto_alloc_size = sizeof(struct altera_sysid_platdata),
  82. .ops = &altera_sysid_ops,
  83. };