mc13783-rtc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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(CONFIG_MC13783_SPI_BUS,
  34. CONFIG_MC13783_SPI_CS, 1000000,
  35. SPI_MODE_2 | SPI_CS_HIGH);
  36. if (!slave)
  37. return -1;
  38. }
  39. if (spi_claim_bus(slave))
  40. return -1;
  41. do {
  42. reg = 0x2c000000;
  43. err = spi_xfer(slave, 32, (uchar *)&reg, (uchar *)&day1,
  44. SPI_XFER_BEGIN | SPI_XFER_END);
  45. if (err)
  46. return err;
  47. reg = 0x28000000;
  48. err = spi_xfer(slave, 32, (uchar *)&reg, (uchar *)&time,
  49. SPI_XFER_BEGIN | SPI_XFER_END);
  50. if (err)
  51. return err;
  52. reg = 0x2c000000;
  53. err = spi_xfer(slave, 32, (uchar *)&reg, (uchar *)&day2,
  54. SPI_XFER_BEGIN | SPI_XFER_END);
  55. if (err)
  56. return err;
  57. } while (day1 != day2 && i++ < 3);
  58. spi_release_bus(slave);
  59. tim = day1 * 86400 + time;
  60. to_tm(tim, rtc);
  61. rtc->tm_yday = 0;
  62. rtc->tm_isdst = 0;
  63. return 0;
  64. }
  65. int rtc_set(struct rtc_time *rtc)
  66. {
  67. u32 time, day, reg;
  68. if (!slave) {
  69. /* FIXME: Verify the max SCK rate */
  70. slave = spi_setup_slave(CONFIG_MC13783_SPI_BUS,
  71. CONFIG_MC13783_SPI_CS, 1000000,
  72. SPI_MODE_2 | SPI_CS_HIGH);
  73. if (!slave)
  74. return -1;
  75. }
  76. time = mktime(rtc->tm_year, rtc->tm_mon, rtc->tm_mday,
  77. rtc->tm_hour, rtc->tm_min, rtc->tm_sec);
  78. day = time / 86400;
  79. time %= 86400;
  80. if (spi_claim_bus(slave))
  81. return -1;
  82. reg = 0x2c000000 | day | 0x80000000;
  83. spi_xfer(slave, 32, (uchar *)&reg, (uchar *)&day,
  84. SPI_XFER_BEGIN | SPI_XFER_END);
  85. reg = 0x28000000 | time | 0x80000000;
  86. spi_xfer(slave, 32, (uchar *)&reg, (uchar *)&time,
  87. SPI_XFER_BEGIN | SPI_XFER_END);
  88. spi_release_bus(slave);
  89. return -1;
  90. }
  91. void rtc_reset(void)
  92. {
  93. }