ftrtc010.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Faraday FTRTC010 Real Time Clock
  4. *
  5. * (C) Copyright 2009 Faraday Technology
  6. * Po-Yu Chuang <ratbert@faraday-tech.com>
  7. */
  8. #include <config.h>
  9. #include <common.h>
  10. #include <log.h>
  11. #include <rtc.h>
  12. #include <asm/io.h>
  13. struct ftrtc010 {
  14. unsigned int sec; /* 0x00 */
  15. unsigned int min; /* 0x04 */
  16. unsigned int hour; /* 0x08 */
  17. unsigned int day; /* 0x0c */
  18. unsigned int alarm_sec; /* 0x10 */
  19. unsigned int alarm_min; /* 0x14 */
  20. unsigned int alarm_hour; /* 0x18 */
  21. unsigned int record; /* 0x1c */
  22. unsigned int cr; /* 0x20 */
  23. unsigned int wsec; /* 0x24 */
  24. unsigned int wmin; /* 0x28 */
  25. unsigned int whour; /* 0x2c */
  26. unsigned int wday; /* 0x30 */
  27. unsigned int intr; /* 0x34 */
  28. unsigned int div; /* 0x38 */
  29. unsigned int rev; /* 0x3c */
  30. };
  31. /*
  32. * RTC Control Register
  33. */
  34. #define FTRTC010_CR_ENABLE (1 << 0)
  35. #define FTRTC010_CR_INTERRUPT_SEC (1 << 1) /* per second irq */
  36. #define FTRTC010_CR_INTERRUPT_MIN (1 << 2) /* per minute irq */
  37. #define FTRTC010_CR_INTERRUPT_HR (1 << 3) /* per hour irq */
  38. #define FTRTC010_CR_INTERRUPT_DAY (1 << 4) /* per day irq */
  39. static struct ftrtc010 *rtc = (struct ftrtc010 *)CONFIG_FTRTC010_BASE;
  40. static void ftrtc010_enable(void)
  41. {
  42. writel(FTRTC010_CR_ENABLE, &rtc->cr);
  43. }
  44. /*
  45. * return current time in seconds
  46. */
  47. static unsigned long ftrtc010_time(void)
  48. {
  49. unsigned long day;
  50. unsigned long hour;
  51. unsigned long minute;
  52. unsigned long second;
  53. unsigned long second2;
  54. do {
  55. second = readl(&rtc->sec);
  56. day = readl(&rtc->day);
  57. hour = readl(&rtc->hour);
  58. minute = readl(&rtc->min);
  59. second2 = readl(&rtc->sec);
  60. } while (second != second2);
  61. return day * 24 * 60 * 60 + hour * 60 * 60 + minute * 60 + second;
  62. }
  63. /*
  64. * Get the current time from the RTC
  65. */
  66. int rtc_get(struct rtc_time *tmp)
  67. {
  68. unsigned long now;
  69. debug("%s(): record register: %x\n",
  70. __func__, readl(&rtc->record));
  71. #ifdef CONFIG_FTRTC010_PCLK
  72. now = (ftrtc010_time() + readl(&rtc->record)) / RTC_DIV_COUNT;
  73. #else /* CONFIG_FTRTC010_EXTCLK */
  74. now = ftrtc010_time() + readl(&rtc->record);
  75. #endif
  76. rtc_to_tm(now, tmp);
  77. return 0;
  78. }
  79. /*
  80. * Set the RTC
  81. */
  82. int rtc_set(struct rtc_time *tmp)
  83. {
  84. unsigned long new;
  85. unsigned long now;
  86. debug("%s(): DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  87. __func__,
  88. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  89. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  90. new = rtc_mktime(tmp);
  91. now = ftrtc010_time();
  92. debug("%s(): write %lx to record register\n", __func__, new - now);
  93. writel(new - now, &rtc->record);
  94. return 0;
  95. }
  96. void rtc_reset(void)
  97. {
  98. debug("%s()\n", __func__);
  99. ftrtc010_enable();
  100. }