bfin_rtc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2004-2008 Analog Devices Inc.
  3. *
  4. * (C) Copyright 2001
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #include <common.h>
  10. #include <command.h>
  11. #include <rtc.h>
  12. #if defined(CONFIG_CMD_DATE)
  13. #include <asm/blackfin.h>
  14. #include <asm/mach-common/bits/rtc.h>
  15. #define pr_stamp() debug("%s:%s:%i: here i am\n", __FILE__, __func__, __LINE__)
  16. #define MIN_TO_SECS(x) (60 * (x))
  17. #define HRS_TO_SECS(x) (60 * MIN_TO_SECS(x))
  18. #define DAYS_TO_SECS(x) (24 * HRS_TO_SECS(x))
  19. #define NUM_SECS_IN_MIN MIN_TO_SECS(1)
  20. #define NUM_SECS_IN_HR HRS_TO_SECS(1)
  21. #define NUM_SECS_IN_DAY DAYS_TO_SECS(1)
  22. /* Enable the RTC prescaler enable register */
  23. void rtc_init(void)
  24. {
  25. if (!(bfin_read_RTC_PREN() & 0x1))
  26. bfin_write_RTC_PREN(0x1);
  27. }
  28. /* Our on-chip RTC has no notion of "reset" */
  29. void rtc_reset(void)
  30. {
  31. rtc_init();
  32. }
  33. /* Wait for pending writes to complete */
  34. static void wait_for_complete(void)
  35. {
  36. pr_stamp();
  37. while (!(bfin_read_RTC_ISTAT() & WRITE_COMPLETE))
  38. if (!(bfin_read_RTC_ISTAT() & WRITE_PENDING))
  39. break;
  40. bfin_write_RTC_ISTAT(WRITE_COMPLETE);
  41. }
  42. /* Set the time. Get the time_in_secs which is the number of seconds since Jan 1970 and set the RTC registers
  43. * based on this value.
  44. */
  45. int rtc_set(struct rtc_time *tmp)
  46. {
  47. unsigned long remain, days, hrs, mins, secs;
  48. pr_stamp();
  49. if (tmp == NULL) {
  50. puts("Error setting the date/time\n");
  51. return -1;
  52. }
  53. rtc_init();
  54. wait_for_complete();
  55. /* Calculate number of seconds this incoming time represents */
  56. remain = rtc_mktime(tmp);
  57. /* Figure out how many days since epoch */
  58. days = remain / NUM_SECS_IN_DAY;
  59. /* From the remaining secs, compute the hrs(0-23), mins(0-59) and secs(0-59) */
  60. remain = remain % NUM_SECS_IN_DAY;
  61. hrs = remain / NUM_SECS_IN_HR;
  62. remain = remain % NUM_SECS_IN_HR;
  63. mins = remain / NUM_SECS_IN_MIN;
  64. secs = remain % NUM_SECS_IN_MIN;
  65. /* Encode these time values into our RTC_STAT register */
  66. bfin_write_RTC_STAT(SET_ALARM(days, hrs, mins, secs));
  67. return 0;
  68. }
  69. /* Read the time from the RTC_STAT. time_in_seconds is seconds since Jan 1970 */
  70. int rtc_get(struct rtc_time *tmp)
  71. {
  72. uint32_t cur_rtc_stat;
  73. int time_in_sec;
  74. int tm_sec, tm_min, tm_hr, tm_day;
  75. pr_stamp();
  76. if (tmp == NULL) {
  77. puts("Error getting the date/time\n");
  78. return -1;
  79. }
  80. rtc_init();
  81. wait_for_complete();
  82. /* Read the RTC_STAT register */
  83. cur_rtc_stat = bfin_read_RTC_STAT();
  84. /* Convert our encoded format into actual time values */
  85. tm_sec = (cur_rtc_stat & RTC_SEC) >> RTC_SEC_P;
  86. tm_min = (cur_rtc_stat & RTC_MIN) >> RTC_MIN_P;
  87. tm_hr = (cur_rtc_stat & RTC_HR ) >> RTC_HR_P;
  88. tm_day = (cur_rtc_stat & RTC_DAY) >> RTC_DAY_P;
  89. /* Calculate the total number of seconds since epoch */
  90. time_in_sec = (tm_sec) + MIN_TO_SECS(tm_min) + HRS_TO_SECS(tm_hr) + DAYS_TO_SECS(tm_day);
  91. rtc_to_tm(time_in_sec, tmp);
  92. return 0;
  93. }
  94. #endif