i2c_rtc_emul.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. /**
  28. * struct sandbox_i2c_rtc_plat_data - platform data for the RTC
  29. *
  30. * @base_time: Base system time when RTC device was bound
  31. * @offset: RTC offset from current system time
  32. * @use_system_time: true to use system time, false to use @base_time
  33. * @reg: Register values
  34. */
  35. struct sandbox_i2c_rtc_plat_data {
  36. long base_time;
  37. long offset;
  38. bool use_system_time;
  39. u8 reg[REG_COUNT];
  40. };
  41. struct sandbox_i2c_rtc {
  42. unsigned int offset_secs;
  43. };
  44. long sandbox_i2c_rtc_set_offset(struct udevice *dev, bool use_system_time,
  45. int offset)
  46. {
  47. struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(dev);
  48. long old_offset;
  49. old_offset = plat->offset;
  50. plat->use_system_time = use_system_time;
  51. if (offset != -1)
  52. plat->offset = offset;
  53. return old_offset;
  54. }
  55. long sandbox_i2c_rtc_get_set_base_time(struct udevice *dev, long base_time)
  56. {
  57. struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(dev);
  58. long old_base_time;
  59. old_base_time = plat->base_time;
  60. if (base_time != -1)
  61. plat->base_time = base_time;
  62. return old_base_time;
  63. }
  64. static void reset_time(struct udevice *dev)
  65. {
  66. struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(dev);
  67. struct rtc_time now;
  68. os_localtime(&now);
  69. plat->base_time = rtc_mktime(&now);
  70. plat->offset = 0;
  71. plat->use_system_time = true;
  72. }
  73. static int sandbox_i2c_rtc_get(struct udevice *dev, struct rtc_time *time)
  74. {
  75. struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(dev);
  76. struct rtc_time tm_now;
  77. long now;
  78. if (plat->use_system_time) {
  79. os_localtime(&tm_now);
  80. now = rtc_mktime(&tm_now);
  81. } else {
  82. now = plat->base_time;
  83. }
  84. rtc_to_tm(now + plat->offset, time);
  85. return 0;
  86. }
  87. static int sandbox_i2c_rtc_set(struct udevice *dev, const struct rtc_time *time)
  88. {
  89. struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(dev);
  90. struct rtc_time tm_now;
  91. long now;
  92. if (plat->use_system_time) {
  93. os_localtime(&tm_now);
  94. now = rtc_mktime(&tm_now);
  95. } else {
  96. now = plat->base_time;
  97. }
  98. plat->offset = rtc_mktime(time) - now;
  99. return 0;
  100. }
  101. /* Update the current time in the registers */
  102. static int sandbox_i2c_rtc_prepare_read(struct udevice *emul)
  103. {
  104. struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(emul);
  105. struct rtc_time time;
  106. int ret;
  107. ret = sandbox_i2c_rtc_get(emul, &time);
  108. if (ret)
  109. return ret;
  110. plat->reg[REG_SEC] = time.tm_sec;
  111. plat->reg[REG_MIN] = time.tm_min;
  112. plat->reg[REG_HOUR] = time.tm_hour;
  113. plat->reg[REG_MDAY] = time.tm_mday;
  114. plat->reg[REG_MON] = time.tm_mon;
  115. plat->reg[REG_YEAR] = time.tm_year - 1900;
  116. plat->reg[REG_WDAY] = time.tm_wday;
  117. return 0;
  118. }
  119. static int sandbox_i2c_rtc_complete_write(struct udevice *emul)
  120. {
  121. struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(emul);
  122. struct rtc_time time;
  123. int ret;
  124. time.tm_sec = plat->reg[REG_SEC];
  125. time.tm_min = plat->reg[REG_MIN];
  126. time.tm_hour = plat->reg[REG_HOUR];
  127. time.tm_mday = plat->reg[REG_MDAY];
  128. time.tm_mon = plat->reg[REG_MON];
  129. time.tm_year = plat->reg[REG_YEAR] + 1900;
  130. time.tm_wday = plat->reg[REG_WDAY];
  131. ret = sandbox_i2c_rtc_set(emul, &time);
  132. if (ret)
  133. return ret;
  134. return 0;
  135. }
  136. static int sandbox_i2c_rtc_xfer(struct udevice *emul, struct i2c_msg *msg,
  137. int nmsgs)
  138. {
  139. struct sandbox_i2c_rtc_plat_data *plat = dev_get_platdata(emul);
  140. uint offset = 0;
  141. int ret;
  142. debug("\n%s\n", __func__);
  143. ret = sandbox_i2c_rtc_prepare_read(emul);
  144. if (ret)
  145. return ret;
  146. for (; nmsgs > 0; nmsgs--, msg++) {
  147. int len;
  148. u8 *ptr;
  149. len = msg->len;
  150. debug(" %s: msg->len=%d",
  151. msg->flags & I2C_M_RD ? "read" : "write",
  152. msg->len);
  153. if (msg->flags & I2C_M_RD) {
  154. debug(", offset %x, len %x: ", offset, len);
  155. /* Read the register */
  156. memcpy(msg->buf, plat->reg + offset, len);
  157. memset(msg->buf + len, '\xff', msg->len - len);
  158. debug_buffer(0, msg->buf, 1, msg->len, 0);
  159. } else if (len >= 1) {
  160. ptr = msg->buf;
  161. offset = *ptr++ & (REG_COUNT - 1);
  162. len--;
  163. debug(", set offset %x: ", offset);
  164. debug_buffer(0, msg->buf, 1, msg->len, 0);
  165. /* Write the register */
  166. memcpy(plat->reg + offset, ptr, len);
  167. /* If the reset register was written to, do reset. */
  168. if (offset <= REG_RESET && REG_RESET < offset + len)
  169. reset_time(emul);
  170. }
  171. }
  172. ret = sandbox_i2c_rtc_complete_write(emul);
  173. if (ret)
  174. return ret;
  175. return 0;
  176. }
  177. struct dm_i2c_ops sandbox_i2c_rtc_emul_ops = {
  178. .xfer = sandbox_i2c_rtc_xfer,
  179. };
  180. static int sandbox_i2c_rtc_bind(struct udevice *dev)
  181. {
  182. reset_time(dev);
  183. return 0;
  184. }
  185. static const struct udevice_id sandbox_i2c_rtc_ids[] = {
  186. { .compatible = "sandbox,i2c-rtc" },
  187. { }
  188. };
  189. U_BOOT_DRIVER(sandbox_i2c_rtc_emul) = {
  190. .name = "sandbox_i2c_rtc_emul",
  191. .id = UCLASS_I2C_EMUL,
  192. .of_match = sandbox_i2c_rtc_ids,
  193. .bind = sandbox_i2c_rtc_bind,
  194. .priv_auto_alloc_size = sizeof(struct sandbox_i2c_rtc),
  195. .platdata_auto_alloc_size = sizeof(struct sandbox_i2c_rtc_plat_data),
  196. .ops = &sandbox_i2c_rtc_emul_ops,
  197. };