pl031.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2008
  4. * Gururaja Hebbar gururajakr@sanyo.co.in
  5. *
  6. * reference linux-2.6.20.6/drivers/rtc/rtc-pl031.c
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <rtc.h>
  11. #if defined(CONFIG_CMD_DATE)
  12. #ifndef CONFIG_SYS_RTC_PL031_BASE
  13. #error CONFIG_SYS_RTC_PL031_BASE is not defined!
  14. #endif
  15. /*
  16. * Register definitions
  17. */
  18. #define RTC_DR 0x00 /* Data read register */
  19. #define RTC_MR 0x04 /* Match register */
  20. #define RTC_LR 0x08 /* Data load register */
  21. #define RTC_CR 0x0c /* Control register */
  22. #define RTC_IMSC 0x10 /* Interrupt mask and set register */
  23. #define RTC_RIS 0x14 /* Raw interrupt status register */
  24. #define RTC_MIS 0x18 /* Masked interrupt status register */
  25. #define RTC_ICR 0x1c /* Interrupt clear register */
  26. #define RTC_CR_START (1 << 0)
  27. #define RTC_WRITE_REG(addr, val) \
  28. (*(volatile unsigned int *)(CONFIG_SYS_RTC_PL031_BASE + (addr)) = (val))
  29. #define RTC_READ_REG(addr) \
  30. (*(volatile unsigned int *)(CONFIG_SYS_RTC_PL031_BASE + (addr)))
  31. static int pl031_initted = 0;
  32. /* Enable RTC Start in Control register*/
  33. void rtc_init(void)
  34. {
  35. RTC_WRITE_REG(RTC_CR, RTC_CR_START);
  36. pl031_initted = 1;
  37. }
  38. /*
  39. * Reset the RTC. We set the date back to 1970-01-01.
  40. */
  41. void rtc_reset(void)
  42. {
  43. RTC_WRITE_REG(RTC_LR, 0x00);
  44. if(!pl031_initted)
  45. rtc_init();
  46. }
  47. /*
  48. * Set the RTC
  49. */
  50. int rtc_set(struct rtc_time *tmp)
  51. {
  52. unsigned long tim;
  53. if(!pl031_initted)
  54. rtc_init();
  55. if (tmp == NULL) {
  56. puts("Error setting the date/time\n");
  57. return -1;
  58. }
  59. /* Calculate number of seconds this incoming time represents */
  60. tim = rtc_mktime(tmp);
  61. RTC_WRITE_REG(RTC_LR, tim);
  62. return -1;
  63. }
  64. /*
  65. * Get the current time from the RTC
  66. */
  67. int rtc_get(struct rtc_time *tmp)
  68. {
  69. ulong tim;
  70. if(!pl031_initted)
  71. rtc_init();
  72. if (tmp == NULL) {
  73. puts("Error getting the date/time\n");
  74. return -1;
  75. }
  76. tim = RTC_READ_REG(RTC_DR);
  77. rtc_to_tm(tim, tmp);
  78. debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  79. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  80. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  81. return 0;
  82. }
  83. #endif