mvrtc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (C) 2011
  3. * Jason Cooper <u-boot@lakedaemon.net>
  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. /*
  24. * Date & Time support for Marvell Integrated RTC
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <rtc.h>
  29. #include "mvrtc.h"
  30. /* This RTC does not support century, so we assume 20 */
  31. #define CENTURY 20
  32. int rtc_get(struct rtc_time *t)
  33. {
  34. u32 time;
  35. u32 date;
  36. struct mvrtc_registers *mvrtc_regs;
  37. mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
  38. /* read the time register */
  39. time = readl(&mvrtc_regs->time);
  40. /* read the date register */
  41. date = readl(&mvrtc_regs->date);
  42. /* test for 12 hour clock (can't tell if it's am/pm) */
  43. if (time & MVRTC_HRFMT_MSK) {
  44. printf("Error: RTC in 12 hour mode, can't determine AM/PM.\n");
  45. return -1;
  46. }
  47. /* time */
  48. t->tm_sec = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK);
  49. t->tm_min = bcd2bin((time >> MVRTC_MIN_SFT) & MVRTC_MIN_MSK);
  50. t->tm_hour = bcd2bin((time >> MVRTC_HOUR_SFT) & MVRTC_HOUR_MSK);
  51. t->tm_wday = bcd2bin((time >> MVRTC_DAY_SFT) & MVRTC_DAY_MSK);
  52. t->tm_wday--;
  53. /* date */
  54. t->tm_mday = bcd2bin((date >> MVRTC_DATE_SFT) & MVRTC_DATE_MSK);
  55. t->tm_mon = bcd2bin((date >> MVRTC_MON_SFT) & MVRTC_MON_MSK);
  56. t->tm_year = bcd2bin((date >> MVRTC_YEAR_SFT) & MVRTC_YEAR_MSK);
  57. t->tm_year += CENTURY * 100;
  58. /* not supported in this RTC */
  59. t->tm_yday = 0;
  60. t->tm_isdst = 0;
  61. return 0;
  62. }
  63. int rtc_set(struct rtc_time *t)
  64. {
  65. u32 time = 0; /* sets hour format bit to zero, 24hr format. */
  66. u32 date = 0;
  67. struct mvrtc_registers *mvrtc_regs;
  68. mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
  69. /* check that this code isn't 80+ years old ;-) */
  70. if ((t->tm_year / 100) != CENTURY)
  71. printf("Warning: Only century %d supported.\n", CENTURY);
  72. /* time */
  73. time |= (bin2bcd(t->tm_sec) & MVRTC_SEC_MSK) << MVRTC_SEC_SFT;
  74. time |= (bin2bcd(t->tm_min) & MVRTC_MIN_MSK) << MVRTC_MIN_SFT;
  75. time |= (bin2bcd(t->tm_hour) & MVRTC_HOUR_MSK) << MVRTC_HOUR_SFT;
  76. time |= (bin2bcd(t->tm_wday + 1) & MVRTC_DAY_MSK) << MVRTC_DAY_SFT;
  77. /* date */
  78. date |= (bin2bcd(t->tm_mday) & MVRTC_DATE_MSK) << MVRTC_DATE_SFT;
  79. date |= (bin2bcd(t->tm_mon) & MVRTC_MON_MSK) << MVRTC_MON_SFT;
  80. date |= (bin2bcd(t->tm_year % 100) & MVRTC_YEAR_MSK) << MVRTC_YEAR_SFT;
  81. /* write the time register */
  82. writel(time, &mvrtc_regs->time);
  83. /* write the date register */
  84. writel(date, &mvrtc_regs->date);
  85. return 0;
  86. }
  87. void rtc_reset(void)
  88. {
  89. u32 time;
  90. u32 sec;
  91. struct mvrtc_registers *mvrtc_regs;
  92. mvrtc_regs = (struct mvrtc_registers *)KW_RTC_BASE;
  93. /* no init routine for this RTC needed, just check that it's working */
  94. time = readl(&mvrtc_regs->time);
  95. sec = bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK);
  96. udelay(1000000);
  97. time = readl(&mvrtc_regs->time);
  98. if (sec == bcd2bin((time >> MVRTC_SEC_SFT) & MVRTC_SEC_MSK))
  99. printf("Error: RTC did not increment.\n");
  100. }