pl031.c 2.1 KB

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