mxsrtc.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Freescale i.MX28 RTC Driver
  3. *
  4. * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
  5. * on behalf of DENX Software Engineering GmbH
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <rtc.h>
  11. #include <asm/io.h>
  12. #include <asm/arch/imx-regs.h>
  13. #include <asm/arch/sys_proto.h>
  14. #define MXS_RTC_MAX_TIMEOUT 1000000
  15. /* Set time in seconds since 1970-01-01 */
  16. int mxs_rtc_set_time(uint32_t secs)
  17. {
  18. struct mxs_rtc_regs *rtc_regs = (struct mxs_rtc_regs *)MXS_RTC_BASE;
  19. int ret;
  20. writel(secs, &rtc_regs->hw_rtc_seconds);
  21. /*
  22. * The 0x80 here means seconds were copied to analog. This information
  23. * is taken from the linux kernel driver for the STMP37xx RTC since
  24. * documentation doesn't mention it.
  25. */
  26. ret = mxs_wait_mask_clr(&rtc_regs->hw_rtc_stat_reg,
  27. 0x80 << RTC_STAT_STALE_REGS_OFFSET, MXS_RTC_MAX_TIMEOUT);
  28. if (ret)
  29. printf("MXS RTC: Timeout waiting for update\n");
  30. return ret;
  31. }
  32. int rtc_get(struct rtc_time *time)
  33. {
  34. struct mxs_rtc_regs *rtc_regs = (struct mxs_rtc_regs *)MXS_RTC_BASE;
  35. uint32_t secs;
  36. secs = readl(&rtc_regs->hw_rtc_seconds);
  37. rtc_to_tm(secs, time);
  38. return 0;
  39. }
  40. int rtc_set(struct rtc_time *time)
  41. {
  42. uint32_t secs;
  43. secs = rtc_mktime(time);
  44. return mxs_rtc_set_time(secs);
  45. }
  46. void rtc_reset(void)
  47. {
  48. struct mxs_rtc_regs *rtc_regs = (struct mxs_rtc_regs *)MXS_RTC_BASE;
  49. int ret;
  50. /* Set time to 1970-01-01 */
  51. mxs_rtc_set_time(0);
  52. /* Reset the RTC block */
  53. ret = mxs_reset_block(&rtc_regs->hw_rtc_ctrl_reg);
  54. if (ret)
  55. printf("MXS RTC: Block reset timeout\n");
  56. }