rx8010sj.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Epson RX8010 RTC driver.
  3. *
  4. * Copyright (c) 2017, General Electric Company
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <command.h>
  19. #include <common.h>
  20. #include <dm.h>
  21. #include <i2c.h>
  22. #include <rtc.h>
  23. #include <linux/bitops.h>
  24. /*---------------------------------------------------------------------*/
  25. /* #undef DEBUG_RTC */
  26. #ifdef DEBUG_RTC
  27. #define DEBUGR(fmt, args...) printf(fmt, ##args)
  28. #else
  29. #define DEBUGR(fmt, args...)
  30. #endif
  31. /*---------------------------------------------------------------------*/
  32. #ifndef CONFIG_SYS_I2C_RTC_ADDR
  33. # define CONFIG_SYS_I2C_RTC_ADDR 0x32
  34. #endif
  35. /*
  36. * RTC register addresses
  37. */
  38. #define RX8010_SEC 0x10
  39. #define RX8010_MIN 0x11
  40. #define RX8010_HOUR 0x12
  41. #define RX8010_WDAY 0x13
  42. #define RX8010_MDAY 0x14
  43. #define RX8010_MONTH 0x15
  44. #define RX8010_YEAR 0x16
  45. #define RX8010_YEAR 0x16
  46. #define RX8010_RESV17 0x17
  47. #define RX8010_ALMIN 0x18
  48. #define RX8010_ALHOUR 0x19
  49. #define RX8010_ALWDAY 0x1A
  50. #define RX8010_TCOUNT0 0x1B
  51. #define RX8010_TCOUNT1 0x1C
  52. #define RX8010_EXT 0x1D
  53. #define RX8010_FLAG 0x1E
  54. #define RX8010_CTRL 0x1F
  55. /* 0x20 to 0x2F are user registers */
  56. #define RX8010_RESV30 0x30
  57. #define RX8010_RESV31 0x32
  58. #define RX8010_IRQ 0x32
  59. #define RX8010_EXT_WADA BIT(3)
  60. #define RX8010_FLAG_VLF BIT(1)
  61. #define RX8010_FLAG_AF BIT(3)
  62. #define RX8010_FLAG_TF BIT(4)
  63. #define RX8010_FLAG_UF BIT(5)
  64. #define RX8010_CTRL_AIE BIT(3)
  65. #define RX8010_CTRL_UIE BIT(5)
  66. #define RX8010_CTRL_STOP BIT(6)
  67. #define RX8010_CTRL_TEST BIT(7)
  68. #define RX8010_ALARM_AE BIT(7)
  69. #ifdef CONFIG_DM_RTC
  70. #define DEV_TYPE struct udevice
  71. #else
  72. /* Local udevice */
  73. struct ludevice {
  74. u8 chip;
  75. };
  76. #define DEV_TYPE struct ludevice
  77. #endif
  78. static int rx8010sj_rtc_read8(DEV_TYPE *dev, unsigned int reg)
  79. {
  80. u8 val;
  81. int ret;
  82. #ifdef CONFIG_DM_RTC
  83. ret = dm_i2c_read(dev, reg, &val, sizeof(val));
  84. #else
  85. ret = i2c_read(dev->chip, reg, 1, &val, 1);
  86. #endif
  87. return ret < 0 ? ret : val;
  88. }
  89. static int rx8010sj_rtc_write8(DEV_TYPE *dev, unsigned int reg, int val)
  90. {
  91. int ret;
  92. u8 lval = val;
  93. #ifdef CONFIG_DM_RTC
  94. ret = dm_i2c_write(dev, reg, &lval, 1);
  95. #else
  96. ret = i2c_write(dev->chip, reg, 1, &lval, 1);
  97. #endif
  98. return ret < 0 ? ret : 0;
  99. }
  100. static int validate_time(const struct rtc_time *tm)
  101. {
  102. if ((tm->tm_year < 2000) || (tm->tm_year > 2099))
  103. return -EINVAL;
  104. if ((tm->tm_mon < 1) || (tm->tm_mon > 12))
  105. return -EINVAL;
  106. if ((tm->tm_mday < 1) || (tm->tm_mday > 31))
  107. return -EINVAL;
  108. if ((tm->tm_wday < 0) || (tm->tm_wday > 6))
  109. return -EINVAL;
  110. if ((tm->tm_hour < 0) || (tm->tm_hour > 23))
  111. return -EINVAL;
  112. if ((tm->tm_min < 0) || (tm->tm_min > 59))
  113. return -EINVAL;
  114. if ((tm->tm_sec < 0) || (tm->tm_sec > 59))
  115. return -EINVAL;
  116. return 0;
  117. }
  118. void rx8010sj_rtc_init(DEV_TYPE *dev)
  119. {
  120. u8 ctrl[2];
  121. int need_clear = 0, ret = 0;
  122. /* Initialize reserved registers as specified in datasheet */
  123. ret = rx8010sj_rtc_write8(dev, RX8010_RESV17, 0xD8);
  124. if (ret < 0)
  125. goto error;
  126. ret = rx8010sj_rtc_write8(dev, RX8010_RESV30, 0x00);
  127. if (ret < 0)
  128. goto error;
  129. ret = rx8010sj_rtc_write8(dev, RX8010_RESV31, 0x08);
  130. if (ret < 0)
  131. goto error;
  132. ret = rx8010sj_rtc_write8(dev, RX8010_IRQ, 0x00);
  133. if (ret < 0)
  134. goto error;
  135. for (int i = 0; i < 2; i++) {
  136. ret = rx8010sj_rtc_read8(dev, RX8010_FLAG + i);
  137. if (ret < 0)
  138. goto error;
  139. ctrl[i] = ret;
  140. }
  141. if (ctrl[0] & RX8010_FLAG_VLF)
  142. printf("RTC low voltage detected\n");
  143. if (ctrl[0] & RX8010_FLAG_AF) {
  144. printf("Alarm was detected\n");
  145. need_clear = 1;
  146. }
  147. if (ctrl[0] & RX8010_FLAG_TF)
  148. need_clear = 1;
  149. if (ctrl[0] & RX8010_FLAG_UF)
  150. need_clear = 1;
  151. if (need_clear) {
  152. ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF);
  153. ret = rx8010sj_rtc_write8(dev, RX8010_FLAG, ctrl[0]);
  154. if (ret < 0)
  155. goto error;
  156. }
  157. return;
  158. error:
  159. printf("Error rtc init.\n");
  160. }
  161. /* Get the current time from the RTC */
  162. static int rx8010sj_rtc_get(DEV_TYPE *dev, struct rtc_time *tmp)
  163. {
  164. u8 date[7];
  165. int flagreg;
  166. int ret;
  167. flagreg = rx8010sj_rtc_read8(dev, RX8010_FLAG);
  168. if (flagreg < 0) {
  169. DEBUGR("Error reading from RTC. err: %d\n", flagreg);
  170. return -EIO;
  171. }
  172. if (flagreg & RX8010_FLAG_VLF) {
  173. DEBUGR("RTC low voltage detected\n");
  174. return -EINVAL;
  175. }
  176. for (int i = 0; i < 7; i++) {
  177. ret = rx8010sj_rtc_read8(dev, RX8010_SEC + i);
  178. if (ret < 0) {
  179. DEBUGR("Error reading from RTC. err: %d\n", ret);
  180. return -EIO;
  181. }
  182. date[i] = ret;
  183. }
  184. tmp->tm_sec = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f);
  185. tmp->tm_min = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f);
  186. tmp->tm_hour = bcd2bin(date[RX8010_HOUR - RX8010_SEC] & 0x3f);
  187. tmp->tm_mday = bcd2bin(date[RX8010_MDAY - RX8010_SEC] & 0x3f);
  188. tmp->tm_mon = bcd2bin(date[RX8010_MONTH - RX8010_SEC] & 0x1f);
  189. tmp->tm_year = bcd2bin(date[RX8010_YEAR - RX8010_SEC]) + 2000;
  190. tmp->tm_wday = 0;
  191. tmp->tm_yday = 0;
  192. tmp->tm_isdst = 0;
  193. DEBUGR("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  194. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  195. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  196. return 0;
  197. }
  198. /* Set the RTC */
  199. static int rx8010sj_rtc_set(DEV_TYPE *dev, const struct rtc_time *tm)
  200. {
  201. u8 date[7];
  202. int ctrl, flagreg;
  203. int ret;
  204. ret = validate_time(tm);
  205. if (ret < 0)
  206. return -EINVAL;
  207. /* set STOP bit before changing clock/calendar */
  208. ctrl = rx8010sj_rtc_read8(dev, RX8010_CTRL);
  209. if (ctrl < 0)
  210. return ctrl;
  211. ret = rx8010sj_rtc_write8(dev, RX8010_CTRL, ctrl | RX8010_CTRL_STOP);
  212. if (ret < 0)
  213. return ret;
  214. date[RX8010_SEC - RX8010_SEC] = bin2bcd(tm->tm_sec);
  215. date[RX8010_MIN - RX8010_SEC] = bin2bcd(tm->tm_min);
  216. date[RX8010_HOUR - RX8010_SEC] = bin2bcd(tm->tm_hour);
  217. date[RX8010_MDAY - RX8010_SEC] = bin2bcd(tm->tm_mday);
  218. date[RX8010_MONTH - RX8010_SEC] = bin2bcd(tm->tm_mon);
  219. date[RX8010_YEAR - RX8010_SEC] = bin2bcd(tm->tm_year - 2000);
  220. date[RX8010_WDAY - RX8010_SEC] = bin2bcd(tm->tm_wday);
  221. for (int i = 0; i < 7; i++) {
  222. ret = rx8010sj_rtc_write8(dev, RX8010_SEC + i, date[i]);
  223. if (ret < 0) {
  224. DEBUGR("Error writing to RTC. err: %d\n", ret);
  225. return -EIO;
  226. }
  227. }
  228. /* clear STOP bit after changing clock/calendar */
  229. ctrl = rx8010sj_rtc_read8(dev, RX8010_CTRL);
  230. if (ctrl < 0)
  231. return ctrl;
  232. ret = rx8010sj_rtc_write8(dev, RX8010_CTRL, ctrl & ~RX8010_CTRL_STOP);
  233. if (ret < 0)
  234. return ret;
  235. flagreg = rx8010sj_rtc_read8(dev, RX8010_FLAG);
  236. if (flagreg < 0)
  237. return flagreg;
  238. if (flagreg & RX8010_FLAG_VLF)
  239. ret = rx8010sj_rtc_write8(dev, RX8010_FLAG,
  240. flagreg & ~RX8010_FLAG_VLF);
  241. return 0;
  242. }
  243. /* Reset the RTC. */
  244. static int rx8010sj_rtc_reset(DEV_TYPE *dev)
  245. {
  246. /* Not needed */
  247. return 0;
  248. }
  249. #ifndef CONFIG_DM_RTC
  250. int rtc_get(struct rtc_time *tm)
  251. {
  252. struct ludevice dev = {
  253. .chip = CONFIG_SYS_I2C_RTC_ADDR,
  254. };
  255. return rx8010sj_rtc_get(&dev, tm);
  256. }
  257. int rtc_set(struct rtc_time *tm)
  258. {
  259. struct ludevice dev = {
  260. .chip = CONFIG_SYS_I2C_RTC_ADDR,
  261. };
  262. return rx8010sj_rtc_set(&dev, tm);
  263. }
  264. void rtc_reset(void)
  265. {
  266. struct ludevice dev = {
  267. .chip = CONFIG_SYS_I2C_RTC_ADDR,
  268. };
  269. rx8010sj_rtc_reset(&dev);
  270. }
  271. void rtc_init(void)
  272. {
  273. struct ludevice dev = {
  274. .chip = CONFIG_SYS_I2C_RTC_ADDR,
  275. };
  276. rx8010sj_rtc_init(&dev);
  277. }
  278. #else
  279. static int rx8010sj_probe(struct udevice *dev)
  280. {
  281. rx8010sj_rtc_init(dev);
  282. return 0;
  283. }
  284. static const struct rtc_ops rx8010sj_rtc_ops = {
  285. .get = rx8010sj_rtc_get,
  286. .set = rx8010sj_rtc_set,
  287. .read8 = rx8010sj_rtc_read8,
  288. .write8 = rx8010sj_rtc_write8,
  289. .reset = rx8010sj_rtc_reset,
  290. };
  291. static const struct udevice_id rx8010sj_rtc_ids[] = {
  292. { .compatible = "epson,rx8010sj-rtc" },
  293. { .compatible = "epson,rx8010" },
  294. { }
  295. };
  296. U_BOOT_DRIVER(rx8010sj_rtc) = {
  297. .name = "rx8010sj_rtc",
  298. .id = UCLASS_RTC,
  299. .probe = rx8010sj_probe,
  300. .of_match = rx8010sj_rtc_ids,
  301. .ops = &rx8010sj_rtc_ops,
  302. };
  303. #endif