bfin_rtc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_RTC_BFIN) && 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. /* Our on-chip RTC has no notion of "reset" */
  23. void rtc_reset(void)
  24. {
  25. return;
  26. }
  27. /* Wait for pending writes to complete */
  28. static void wait_for_complete(void)
  29. {
  30. pr_stamp();
  31. while (!(bfin_read_RTC_ISTAT() & WRITE_COMPLETE))
  32. if (!(bfin_read_RTC_ISTAT() & WRITE_PENDING))
  33. break;
  34. bfin_write_RTC_ISTAT(WRITE_COMPLETE);
  35. }
  36. /* Enable the RTC prescaler enable register */
  37. int rtc_init(void)
  38. {
  39. pr_stamp();
  40. bfin_write_RTC_PREN(0x1);
  41. return 0;
  42. }
  43. /* Set the time. Get the time_in_secs which is the number of seconds since Jan 1970 and set the RTC registers
  44. * based on this value.
  45. */
  46. void rtc_set(struct rtc_time *tmp)
  47. {
  48. unsigned long remain, days, hrs, mins, secs;
  49. pr_stamp();
  50. if (tmp == NULL) {
  51. puts("Error setting the date/time\n");
  52. return;
  53. }
  54. wait_for_complete();
  55. /* Calculate number of seconds this incoming time represents */
  56. remain = mktime(tmp->tm_year, tmp->tm_mon, tmp->tm_mday,
  57. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  58. /* Figure out how many days since epoch */
  59. days = remain / NUM_SECS_IN_DAY;
  60. /* From the remaining secs, compute the hrs(0-23), mins(0-59) and secs(0-59) */
  61. remain = remain % NUM_SECS_IN_DAY;
  62. hrs = remain / NUM_SECS_IN_HR;
  63. remain = remain % NUM_SECS_IN_HR;
  64. mins = remain / NUM_SECS_IN_MIN;
  65. secs = remain % NUM_SECS_IN_MIN;
  66. /* Encode these time values into our RTC_STAT register */
  67. bfin_write_RTC_STAT(SET_ALARM(days, hrs, mins, secs));
  68. }
  69. /* Read the time from the RTC_STAT. time_in_seconds is seconds since Jan 1970 */
  70. void 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;
  79. }
  80. wait_for_complete();
  81. /* Read the RTC_STAT register */
  82. cur_rtc_stat = bfin_read_RTC_STAT();
  83. /* Convert our encoded format into actual time values */
  84. tm_sec = (cur_rtc_stat & RTC_SEC) >> RTC_SEC_P;
  85. tm_min = (cur_rtc_stat & RTC_MIN) >> RTC_MIN_P;
  86. tm_hr = (cur_rtc_stat & RTC_HR ) >> RTC_HR_P;
  87. tm_day = (cur_rtc_stat & RTC_DAY) >> RTC_DAY_P;
  88. /* Calculate the total number of seconds since epoch */
  89. time_in_sec = (tm_sec) + MIN_TO_SECS(tm_min) + HRS_TO_SECS(tm_hr) + DAYS_TO_SECS(tm_day);
  90. to_tm(time_in_sec, tmp);
  91. }
  92. #endif