mx27rtc.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Freescale i.MX27 RTC Driver
  3. *
  4. * Copyright (C) 2012 Philippe Reynes <tremyfr@yahoo.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <common.h>
  22. #include <rtc.h>
  23. #include <asm/io.h>
  24. #include <asm/arch/imx-regs.h>
  25. #define HOUR_SHIFT 8
  26. #define HOUR_MASK 0x1f
  27. #define MIN_SHIFT 0
  28. #define MIN_MASK 0x3f
  29. int rtc_get(struct rtc_time *time)
  30. {
  31. struct rtc_regs *rtc_regs = (struct rtc_regs *)IMX_RTC_BASE;
  32. uint32_t day, hour, min, sec;
  33. day = readl(&rtc_regs->dayr);
  34. hour = readl(&rtc_regs->hourmin);
  35. sec = readl(&rtc_regs->seconds);
  36. min = (hour >> MIN_SHIFT) & MIN_MASK;
  37. hour = (hour >> HOUR_SHIFT) & HOUR_MASK;
  38. sec += min * 60 + hour * 3600 + day * 24 * 3600;
  39. to_tm(sec, time);
  40. return 0;
  41. }
  42. int rtc_set(struct rtc_time *time)
  43. {
  44. struct rtc_regs *rtc_regs = (struct rtc_regs *)IMX_RTC_BASE;
  45. uint32_t day, hour, min, sec;
  46. sec = mktime(time->tm_year, time->tm_mon, time->tm_mday,
  47. time->tm_hour, time->tm_min, time->tm_sec);
  48. day = sec / (24 * 3600);
  49. sec = sec % (24 * 3600);
  50. hour = sec / 3600;
  51. sec = sec % 3600;
  52. min = sec / 60;
  53. sec = sec % 60;
  54. hour = (hour & HOUR_MASK) << HOUR_SHIFT;
  55. hour |= (min & MIN_MASK) << MIN_SHIFT;
  56. writel(day, &rtc_regs->dayr);
  57. writel(hour, &rtc_regs->hourmin);
  58. writel(sec, &rtc_regs->seconds);
  59. return 0;
  60. }
  61. void rtc_reset(void)
  62. {
  63. struct rtc_regs *rtc_regs = (struct rtc_regs *)IMX_RTC_BASE;
  64. writel(0, &rtc_regs->dayr);
  65. writel(0, &rtc_regs->hourmin);
  66. writel(0, &rtc_regs->seconds);
  67. }