m41t94.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Driver for ST M41T94 SPI RTC
  3. *
  4. * Taken from the Linux kernel drivier:
  5. * Copyright (C) 2008 Kim B. Heino
  6. *
  7. * Adaptation for U-Boot:
  8. * Copyright (C) 2009
  9. * Albin Tonnerre, Free Electrons <albin.tonnerre@free-electrons.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <common.h>
  16. #include <rtc.h>
  17. #include <spi.h>
  18. static struct spi_slave *slave;
  19. #define M41T94_REG_SECONDS 0x01
  20. #define M41T94_REG_MINUTES 0x02
  21. #define M41T94_REG_HOURS 0x03
  22. #define M41T94_REG_WDAY 0x04
  23. #define M41T94_REG_DAY 0x05
  24. #define M41T94_REG_MONTH 0x06
  25. #define M41T94_REG_YEAR 0x07
  26. #define M41T94_REG_HT 0x0c
  27. #define M41T94_BIT_HALT 0x40
  28. #define M41T94_BIT_STOP 0x80
  29. #define M41T94_BIT_CB 0x40
  30. #define M41T94_BIT_CEB 0x80
  31. int rtc_set(struct rtc_time *tm)
  32. {
  33. u8 buf[8]; /* write cmd + 7 registers */
  34. int ret;
  35. if (!slave) {
  36. slave = spi_setup_slave(CONFIG_M41T94_SPI_BUS,
  37. CONFIG_M41T94_SPI_CS, 1000000,
  38. SPI_MODE_3);
  39. if (!slave)
  40. return -1;
  41. }
  42. spi_claim_bus(slave);
  43. buf[0] = 0x80 | M41T94_REG_SECONDS; /* write time + date */
  44. buf[M41T94_REG_SECONDS] = bin2bcd(tm->tm_sec);
  45. buf[M41T94_REG_MINUTES] = bin2bcd(tm->tm_min);
  46. buf[M41T94_REG_HOURS] = bin2bcd(tm->tm_hour);
  47. buf[M41T94_REG_WDAY] = bin2bcd(tm->tm_wday + 1);
  48. buf[M41T94_REG_DAY] = bin2bcd(tm->tm_mday);
  49. buf[M41T94_REG_MONTH] = bin2bcd(tm->tm_mon + 1);
  50. buf[M41T94_REG_HOURS] |= M41T94_BIT_CEB;
  51. if (tm->tm_year >= 100)
  52. buf[M41T94_REG_HOURS] |= M41T94_BIT_CB;
  53. buf[M41T94_REG_YEAR] = bin2bcd(tm->tm_year % 100);
  54. ret = spi_xfer(slave, 64, buf, NULL, SPI_XFER_BEGIN | SPI_XFER_END);
  55. spi_release_bus(slave);
  56. return ret;
  57. }
  58. int rtc_get(struct rtc_time *tm)
  59. {
  60. u8 buf[2];
  61. int ret, hour;
  62. if (!slave) {
  63. slave = spi_setup_slave(CONFIG_M41T94_SPI_BUS,
  64. CONFIG_M41T94_SPI_CS, 1000000,
  65. SPI_MODE_3);
  66. if (!slave)
  67. return -1;
  68. }
  69. spi_claim_bus(slave);
  70. /* clear halt update bit */
  71. ret = spi_w8r8(slave, M41T94_REG_HT);
  72. if (ret < 0)
  73. return ret;
  74. if (ret & M41T94_BIT_HALT) {
  75. buf[0] = 0x80 | M41T94_REG_HT;
  76. buf[1] = ret & ~M41T94_BIT_HALT;
  77. spi_xfer(slave, 16, buf, NULL, SPI_XFER_BEGIN | SPI_XFER_END);
  78. }
  79. /* clear stop bit */
  80. ret = spi_w8r8(slave, M41T94_REG_SECONDS);
  81. if (ret < 0)
  82. return ret;
  83. if (ret & M41T94_BIT_STOP) {
  84. buf[0] = 0x80 | M41T94_REG_SECONDS;
  85. buf[1] = ret & ~M41T94_BIT_STOP;
  86. spi_xfer(slave, 16, buf, NULL, SPI_XFER_BEGIN | SPI_XFER_END);
  87. }
  88. tm->tm_sec = bcd2bin(spi_w8r8(slave, M41T94_REG_SECONDS));
  89. tm->tm_min = bcd2bin(spi_w8r8(slave, M41T94_REG_MINUTES));
  90. hour = spi_w8r8(slave, M41T94_REG_HOURS);
  91. tm->tm_hour = bcd2bin(hour & 0x3f);
  92. tm->tm_wday = bcd2bin(spi_w8r8(slave, M41T94_REG_WDAY)) - 1;
  93. tm->tm_mday = bcd2bin(spi_w8r8(slave, M41T94_REG_DAY));
  94. tm->tm_mon = bcd2bin(spi_w8r8(slave, M41T94_REG_MONTH)) - 1;
  95. tm->tm_year = bcd2bin(spi_w8r8(slave, M41T94_REG_YEAR));
  96. if ((hour & M41T94_BIT_CB) || !(hour & M41T94_BIT_CEB))
  97. tm->tm_year += 100;
  98. spi_release_bus(slave);
  99. return 0;
  100. }
  101. void rtc_reset(void)
  102. {
  103. /*
  104. * Could not be tested as the reset pin is not wired on
  105. * the sbc35-ag20 board
  106. */
  107. }