mc13783-rtc.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (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,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <rtc.h>
  24. #include <spi.h>
  25. static struct spi_slave *slave;
  26. int rtc_get(struct rtc_time *rtc)
  27. {
  28. u32 day1, day2, time;
  29. u32 reg;
  30. int err, tim, i = 0;
  31. if (!slave) {
  32. /* FIXME: Verify the max SCK rate */
  33. slave = spi_setup_slave(1, 0, 1000000,
  34. SPI_MODE_2 | SPI_CS_HIGH);
  35. if (!slave)
  36. return -1;
  37. }
  38. if (spi_claim_bus(slave))
  39. return -1;
  40. do {
  41. reg = 0x2c000000;
  42. err = spi_xfer(slave, 32, (uchar *)&reg, (uchar *)&day1,
  43. SPI_XFER_BEGIN | SPI_XFER_END);
  44. if (err)
  45. return err;
  46. reg = 0x28000000;
  47. err = spi_xfer(slave, 32, (uchar *)&reg, (uchar *)&time,
  48. SPI_XFER_BEGIN | SPI_XFER_END);
  49. if (err)
  50. return err;
  51. reg = 0x2c000000;
  52. err = spi_xfer(slave, 32, (uchar *)&reg, (uchar *)&day2,
  53. SPI_XFER_BEGIN | SPI_XFER_END);
  54. if (err)
  55. return err;
  56. } while (day1 != day2 && i++ < 3);
  57. spi_release_bus(slave);
  58. tim = day1 * 86400 + time;
  59. to_tm(tim, rtc);
  60. rtc->tm_yday = 0;
  61. rtc->tm_isdst = 0;
  62. return 0;
  63. }
  64. void rtc_set(struct rtc_time *rtc)
  65. {
  66. u32 time, day, reg;
  67. if (!slave) {
  68. /* FIXME: Verify the max SCK rate */
  69. slave = spi_setup_slave(1, 0, 1000000,
  70. SPI_MODE_2 | SPI_CS_HIGH);
  71. if (!slave)
  72. return;
  73. }
  74. time = mktime(rtc->tm_year, rtc->tm_mon, rtc->tm_mday,
  75. rtc->tm_hour, rtc->tm_min, rtc->tm_sec);
  76. day = time / 86400;
  77. time %= 86400;
  78. if (spi_claim_bus(slave))
  79. return;
  80. reg = 0x2c000000 | day | 0x80000000;
  81. spi_xfer(slave, 32, (uchar *)&reg, (uchar *)&day,
  82. SPI_XFER_BEGIN | SPI_XFER_END);
  83. reg = 0x28000000 | time | 0x80000000;
  84. spi_xfer(slave, 32, (uchar *)&reg, (uchar *)&time,
  85. SPI_XFER_BEGIN | SPI_XFER_END);
  86. spi_release_bus(slave);
  87. }
  88. void rtc_reset(void)
  89. {
  90. }