i2c_rtc_emul.c 5.3 KB

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