mvrtc.c 3.6 KB

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