ftrtc010.c 2.6 KB

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