davinci.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * (C) Copyright 2011 DENX Software Engineering GmbH
  3. * Heiko Schocher <hs@denx.de>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <rtc.h>
  26. #include <asm/io.h>
  27. #include <asm/arch/hardware.h>
  28. #if defined(CONFIG_CMD_DATE)
  29. struct davinci_rtc {
  30. u_int32_t second;
  31. u_int32_t minutes;
  32. u_int32_t hours;
  33. u_int32_t day;
  34. u_int32_t month; /* 0x10 */
  35. u_int32_t year;
  36. u_int32_t dotw;
  37. u_int32_t resv1;
  38. u_int32_t alarmsecond; /* 0x20 */
  39. u_int32_t alarmminute;
  40. u_int32_t alarmhour;
  41. u_int32_t alarmday;
  42. u_int32_t alarmmonth; /* 0x30 */
  43. u_int32_t alarmyear;
  44. u_int32_t resv2[2];
  45. u_int32_t ctrl; /* 0x40 */
  46. u_int32_t status;
  47. u_int32_t irq;
  48. };
  49. #define RTC_STATE_BUSY 0x01
  50. #define RTC_STATE_RUN 0x02
  51. #define davinci_rtc_base ((struct davinci_rtc *)DAVINCI_RTC_BASE)
  52. int rtc_get(struct rtc_time *tmp)
  53. {
  54. struct davinci_rtc *rtc = davinci_rtc_base;
  55. unsigned long sec, min, hour, mday, wday, mon_cent, year;
  56. unsigned long status;
  57. status = readl(&rtc->status);
  58. if ((status & RTC_STATE_RUN) != RTC_STATE_RUN) {
  59. printf("RTC doesn't run\n");
  60. return -1;
  61. }
  62. if ((status & RTC_STATE_BUSY) == RTC_STATE_BUSY)
  63. udelay(20);
  64. sec = readl(&rtc->second);
  65. min = readl(&rtc->minutes);
  66. hour = readl(&rtc->hours);
  67. mday = readl(&rtc->day);
  68. wday = readl(&rtc->dotw);
  69. mon_cent = readl(&rtc->month);
  70. year = readl(&rtc->year);
  71. debug("Get RTC year: %02lx mon/cent: %02lx mday: %02lx wday: %02lx "
  72. "hr: %02lx min: %02lx sec: %02lx\n",
  73. year, mon_cent, mday, wday,
  74. hour, min, sec);
  75. tmp->tm_sec = bcd2bin(sec & 0x7F);
  76. tmp->tm_min = bcd2bin(min & 0x7F);
  77. tmp->tm_hour = bcd2bin(hour & 0x3F);
  78. tmp->tm_mday = bcd2bin(mday & 0x3F);
  79. tmp->tm_mon = bcd2bin(mon_cent & 0x1F);
  80. tmp->tm_year = bcd2bin(year) + 2000;
  81. tmp->tm_wday = bcd2bin(wday & 0x07);
  82. tmp->tm_yday = 0;
  83. tmp->tm_isdst = 0;
  84. debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  85. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  86. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  87. return 0;
  88. }
  89. int rtc_set(struct rtc_time *tmp)
  90. {
  91. struct davinci_rtc *rtc = davinci_rtc_base;
  92. debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  93. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  94. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  95. writel(bin2bcd(tmp->tm_year % 100), &rtc->year);
  96. writel(bin2bcd(tmp->tm_mon), &rtc->month);
  97. writel(bin2bcd(tmp->tm_wday), &rtc->dotw);
  98. writel(bin2bcd(tmp->tm_mday), &rtc->day);
  99. writel(bin2bcd(tmp->tm_hour), &rtc->hours);
  100. writel(bin2bcd(tmp->tm_min), &rtc->minutes);
  101. writel(bin2bcd(tmp->tm_sec), &rtc->second);
  102. return 0;
  103. }
  104. void rtc_reset(void)
  105. {
  106. struct davinci_rtc *rtc = davinci_rtc_base;
  107. /* run RTC counter */
  108. writel(0x01, &rtc->ctrl);
  109. }
  110. #endif