mxsrtc.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <common.h>
  23. #include <rtc.h>
  24. #include <asm/io.h>
  25. #include <asm/arch/imx-regs.h>
  26. #include <asm/arch/sys_proto.h>
  27. #define MXS_RTC_MAX_TIMEOUT 1000000
  28. /* Set time in seconds since 1970-01-01 */
  29. int mxs_rtc_set_time(uint32_t secs)
  30. {
  31. struct mx28_rtc_regs *rtc_regs = (struct mx28_rtc_regs *)MXS_RTC_BASE;
  32. int ret;
  33. writel(secs, &rtc_regs->hw_rtc_seconds);
  34. /*
  35. * The 0x80 here means seconds were copied to analog. This information
  36. * is taken from the linux kernel driver for the STMP37xx RTC since
  37. * documentation doesn't mention it.
  38. */
  39. ret = mx28_wait_mask_clr(&rtc_regs->hw_rtc_stat_reg,
  40. 0x80 << RTC_STAT_STALE_REGS_OFFSET, MXS_RTC_MAX_TIMEOUT);
  41. if (ret)
  42. printf("MXS RTC: Timeout waiting for update\n");
  43. return ret;
  44. }
  45. int rtc_get(struct rtc_time *time)
  46. {
  47. struct mx28_rtc_regs *rtc_regs = (struct mx28_rtc_regs *)MXS_RTC_BASE;
  48. uint32_t secs;
  49. secs = readl(&rtc_regs->hw_rtc_seconds);
  50. to_tm(secs, time);
  51. return 0;
  52. }
  53. int rtc_set(struct rtc_time *time)
  54. {
  55. uint32_t secs;
  56. secs = mktime(time->tm_year, time->tm_mon, time->tm_mday,
  57. time->tm_hour, time->tm_min, time->tm_sec);
  58. return mxs_rtc_set_time(secs);
  59. }
  60. void rtc_reset(void)
  61. {
  62. struct mx28_rtc_regs *rtc_regs = (struct mx28_rtc_regs *)MXS_RTC_BASE;
  63. int ret;
  64. /* Set time to 1970-01-01 */
  65. mxs_rtc_set_time(0);
  66. /* Reset the RTC block */
  67. ret = mx28_reset_block(&rtc_regs->hw_rtc_ctrl_reg);
  68. if (ret)
  69. printf("MXS RTC: Block reset timeout\n");
  70. }