i2c_rtc_emul.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Simulate an I2C real time clock
  4. *
  5. * Copyright (c) 2015 Google, Inc
  6. * Written by Simon Glass <sjg@chromium.org>
  7. */
  8. /*
  9. * This is a test driver. It starts off with the current time of the machine,
  10. * but also supports setting the time, using an offset from the current
  11. * clock. This driver is only intended for testing, not accurate
  12. * time-keeping. It does not change the system time.
  13. */
  14. #include <common.h>
  15. #include <dm.h>
  16. #include <i2c.h>
  17. #include <log.h>
  18. #include <os.h>
  19. #include <rtc.h>
  20. #include <asm/rtc.h>
  21. #include <asm/test.h>
  22. #ifdef DEBUG
  23. #define debug_buffer print_buffer
  24. #else
  25. #define debug_buffer(x, ...)
  26. #endif
  27. long sandbox_i2c_rtc_set_offset(struct udevice *dev, bool use_system_time,
  28. int offset)
  29. {
  30. struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
  31. long old_offset;
  32. old_offset = plat->offset;
  33. plat->use_system_time = use_system_time;
  34. if (offset != -1)
  35. plat->offset = offset;
  36. os_set_time_offset(plat->offset);
  37. return old_offset;
  38. }
  39. long sandbox_i2c_rtc_get_set_base_time(struct udevice *dev, long base_time)
  40. {
  41. struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
  42. long old_base_time;
  43. old_base_time = plat->base_time;
  44. if (base_time != -1)
  45. plat->base_time = base_time;
  46. return old_base_time;
  47. }
  48. static void reset_time(struct udevice *dev)
  49. {
  50. struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
  51. struct rtc_time now;
  52. os_localtime(&now);
  53. plat->base_time = rtc_mktime(&now);
  54. plat->offset = os_get_time_offset();
  55. plat->use_system_time = true;
  56. }
  57. static int sandbox_i2c_rtc_get(struct udevice *dev, struct rtc_time *time)
  58. {
  59. struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
  60. struct rtc_time tm_now;
  61. long now;
  62. if (plat->use_system_time) {
  63. os_localtime(&tm_now);
  64. now = rtc_mktime(&tm_now);
  65. } else {
  66. now = plat->base_time;
  67. }
  68. rtc_to_tm(now + plat->offset, time);
  69. return 0;
  70. }
  71. static int sandbox_i2c_rtc_set(struct udevice *dev, const struct rtc_time *time)
  72. {
  73. struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
  74. struct rtc_time tm_now;
  75. long now;
  76. if (plat->use_system_time) {
  77. os_localtime(&tm_now);
  78. now = rtc_mktime(&tm_now);
  79. } else {
  80. now = plat->base_time;
  81. }
  82. plat->offset = rtc_mktime(time) - now;
  83. os_set_time_offset(plat->offset);
  84. return 0;
  85. }
  86. /* Update the current time in the registers */
  87. static int sandbox_i2c_rtc_prepare_read(struct udevice *emul)
  88. {
  89. struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul);
  90. struct rtc_time time;
  91. int ret;
  92. ret = sandbox_i2c_rtc_get(emul, &time);
  93. if (ret)
  94. return ret;
  95. plat->reg[REG_SEC] = time.tm_sec;
  96. plat->reg[REG_MIN] = time.tm_min;
  97. plat->reg[REG_HOUR] = time.tm_hour;
  98. plat->reg[REG_MDAY] = time.tm_mday;
  99. plat->reg[REG_MON] = time.tm_mon;
  100. plat->reg[REG_YEAR] = time.tm_year - 1900;
  101. plat->reg[REG_WDAY] = time.tm_wday;
  102. return 0;
  103. }
  104. static int sandbox_i2c_rtc_complete_write(struct udevice *emul)
  105. {
  106. struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul);
  107. struct rtc_time time;
  108. int ret;
  109. time.tm_sec = plat->reg[REG_SEC];
  110. time.tm_min = plat->reg[REG_MIN];
  111. time.tm_hour = plat->reg[REG_HOUR];
  112. time.tm_mday = plat->reg[REG_MDAY];
  113. time.tm_mon = plat->reg[REG_MON];
  114. time.tm_year = plat->reg[REG_YEAR] + 1900;
  115. time.tm_wday = plat->reg[REG_WDAY];
  116. ret = sandbox_i2c_rtc_set(emul, &time);
  117. if (ret)
  118. return ret;
  119. return 0;
  120. }
  121. static int sandbox_i2c_rtc_xfer(struct udevice *emul, struct i2c_msg *msg,
  122. int nmsgs)
  123. {
  124. struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(emul);
  125. uint offset = 0;
  126. int ret;
  127. debug("\n%s\n", __func__);
  128. ret = sandbox_i2c_rtc_prepare_read(emul);
  129. if (ret)
  130. return ret;
  131. for (; nmsgs > 0; nmsgs--, msg++) {
  132. int len;
  133. u8 *ptr;
  134. len = msg->len;
  135. debug(" %s: msg->len=%d",
  136. msg->flags & I2C_M_RD ? "read" : "write",
  137. msg->len);
  138. if (msg->flags & I2C_M_RD) {
  139. debug(", offset %x, len %x: ", offset, len);
  140. /* Read the register */
  141. memcpy(msg->buf, plat->reg + offset, len);
  142. memset(msg->buf + len, '\xff', msg->len - len);
  143. debug_buffer(0, msg->buf, 1, msg->len, 0);
  144. } else if (len >= 1) {
  145. ptr = msg->buf;
  146. offset = *ptr++ & (REG_COUNT - 1);
  147. len--;
  148. debug(", set offset %x: ", offset);
  149. debug_buffer(0, msg->buf, 1, msg->len, 0);
  150. /* Write the register */
  151. memcpy(plat->reg + offset, ptr, len);
  152. /* If the reset register was written to, do reset. */
  153. if (offset <= REG_RESET && REG_RESET < offset + len)
  154. reset_time(emul);
  155. }
  156. }
  157. ret = sandbox_i2c_rtc_complete_write(emul);
  158. if (ret)
  159. return ret;
  160. return 0;
  161. }
  162. struct dm_i2c_ops sandbox_i2c_rtc_emul_ops = {
  163. .xfer = sandbox_i2c_rtc_xfer,
  164. };
  165. static int sandbox_i2c_rtc_bind(struct udevice *dev)
  166. {
  167. reset_time(dev);
  168. return 0;
  169. }
  170. static const struct udevice_id sandbox_i2c_rtc_ids[] = {
  171. { .compatible = "sandbox,i2c-rtc-emul" },
  172. { }
  173. };
  174. U_BOOT_DRIVER(sandbox_i2c_rtc_emul) = {
  175. .name = "sandbox_i2c_rtc_emul",
  176. .id = UCLASS_I2C_EMUL,
  177. .of_match = sandbox_i2c_rtc_ids,
  178. .bind = sandbox_i2c_rtc_bind,
  179. .priv_auto = sizeof(struct sandbox_i2c_rtc),
  180. .plat_auto = sizeof(struct sandbox_i2c_rtc_plat_data),
  181. .ops = &sandbox_i2c_rtc_emul_ops,
  182. };