ftrtc010.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Faraday FTRTC010 Real Time Clock
  3. *
  4. * (C) Copyright 2009 Faraday Technology
  5. * Po-Yu Chuang <ratbert@faraday-tech.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <config.h>
  22. #include <common.h>
  23. #include <rtc.h>
  24. #include <asm/io.h>
  25. struct ftrtc010 {
  26. unsigned int sec; /* 0x00 */
  27. unsigned int min; /* 0x04 */
  28. unsigned int hour; /* 0x08 */
  29. unsigned int day; /* 0x0c */
  30. unsigned int alarm_sec; /* 0x10 */
  31. unsigned int alarm_min; /* 0x14 */
  32. unsigned int alarm_hour; /* 0x18 */
  33. unsigned int record; /* 0x1c */
  34. unsigned int cr; /* 0x20 */
  35. };
  36. /*
  37. * RTC Control Register
  38. */
  39. #define FTRTC010_CR_ENABLE (1 << 0)
  40. #define FTRTC010_CR_INTERRUPT_SEC (1 << 1) /* per second irq */
  41. #define FTRTC010_CR_INTERRUPT_MIN (1 << 2) /* per minute irq */
  42. #define FTRTC010_CR_INTERRUPT_HR (1 << 3) /* per hour irq */
  43. #define FTRTC010_CR_INTERRUPT_DAY (1 << 4) /* per day irq */
  44. static struct ftrtc010 *rtc = (struct ftrtc010 *)CONFIG_FTRTC010_BASE;
  45. static void ftrtc010_enable(void)
  46. {
  47. writel(FTRTC010_CR_ENABLE, &rtc->cr);
  48. }
  49. /*
  50. * return current time in seconds
  51. */
  52. static unsigned long ftrtc010_time(void)
  53. {
  54. unsigned long day;
  55. unsigned long hour;
  56. unsigned long minute;
  57. unsigned long second;
  58. unsigned long second2;
  59. do {
  60. second = readl(&rtc->sec);
  61. day = readl(&rtc->day);
  62. hour = readl(&rtc->hour);
  63. minute = readl(&rtc->min);
  64. second2 = readl(&rtc->sec);
  65. } while (second != second2);
  66. return day * 24 * 60 * 60 + hour * 60 * 60 + minute * 60 + second;
  67. }
  68. /*
  69. * Get the current time from the RTC
  70. */
  71. int rtc_get(struct rtc_time *tmp)
  72. {
  73. unsigned long now;
  74. debug("%s(): record register: %x\n",
  75. __func__, readl(&rtc->record));
  76. now = ftrtc010_time() + readl(&rtc->record);
  77. to_tm(now, tmp);
  78. return 0;
  79. }
  80. /*
  81. * Set the RTC
  82. */
  83. int rtc_set(struct rtc_time *tmp)
  84. {
  85. unsigned long new;
  86. unsigned long now;
  87. debug("%s(): DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  88. __func__,
  89. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  90. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  91. new = mktime(tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_hour,
  92. tmp->tm_min, tmp->tm_sec);
  93. now = ftrtc010_time();
  94. debug("%s(): write %lx to record register\n", __func__, new - now);
  95. writel(new - now, &rtc->record);
  96. return 0;
  97. }
  98. void rtc_reset(void)
  99. {
  100. debug("%s()\n", __func__);
  101. ftrtc010_enable();
  102. }