mc146818.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2001
  4. * Denis Peter MPL AG Switzerland. d.peter@mpl.ch
  5. */
  6. /*
  7. * Date & Time support for the MC146818 (PIXX4) RTC
  8. */
  9. #include <common.h>
  10. #include <command.h>
  11. #include <dm.h>
  12. #include <rtc.h>
  13. #if defined(CONFIG_X86) || defined(CONFIG_MALTA)
  14. #include <asm/io.h>
  15. #define in8(p) inb(p)
  16. #define out8(p, v) outb(v, p)
  17. #endif
  18. #if defined(CONFIG_CMD_DATE)
  19. /* Set this to 1 to clear the CMOS RAM */
  20. #define CLEAR_CMOS 0
  21. #define RTC_PORT_MC146818 CONFIG_SYS_ISA_IO_BASE_ADDRESS + 0x70
  22. #define RTC_SECONDS 0x00
  23. #define RTC_SECONDS_ALARM 0x01
  24. #define RTC_MINUTES 0x02
  25. #define RTC_MINUTES_ALARM 0x03
  26. #define RTC_HOURS 0x04
  27. #define RTC_HOURS_ALARM 0x05
  28. #define RTC_DAY_OF_WEEK 0x06
  29. #define RTC_DATE_OF_MONTH 0x07
  30. #define RTC_MONTH 0x08
  31. #define RTC_YEAR 0x09
  32. #define RTC_CONFIG_A 0x0a
  33. #define RTC_CONFIG_B 0x0b
  34. #define RTC_CONFIG_C 0x0c
  35. #define RTC_CONFIG_D 0x0d
  36. #define RTC_REG_SIZE 0x80
  37. #define RTC_CONFIG_A_REF_CLCK_32KHZ (1 << 5)
  38. #define RTC_CONFIG_A_RATE_1024HZ 6
  39. #define RTC_CONFIG_B_24H (1 << 1)
  40. #define RTC_CONFIG_D_VALID_RAM_AND_TIME 0x80
  41. static int mc146818_read8(int reg)
  42. {
  43. #ifdef CONFIG_SYS_RTC_REG_BASE_ADDR
  44. return in8(CONFIG_SYS_RTC_REG_BASE_ADDR + reg);
  45. #else
  46. int ofs = 0;
  47. if (reg >= 128) {
  48. ofs = 2;
  49. reg -= 128;
  50. }
  51. out8(RTC_PORT_MC146818 + ofs, reg);
  52. return in8(RTC_PORT_MC146818 + ofs + 1);
  53. #endif
  54. }
  55. static void mc146818_write8(int reg, uchar val)
  56. {
  57. #ifdef CONFIG_SYS_RTC_REG_BASE_ADDR
  58. out8(CONFIG_SYS_RTC_REG_BASE_ADDR + reg, val);
  59. #else
  60. int ofs = 0;
  61. if (reg >= 128) {
  62. ofs = 2;
  63. reg -= 128;
  64. }
  65. out8(RTC_PORT_MC146818 + ofs, reg);
  66. out8(RTC_PORT_MC146818 + ofs + 1, val);
  67. #endif
  68. }
  69. static int mc146818_get(struct rtc_time *tmp)
  70. {
  71. uchar sec, min, hour, mday, wday, mon, year;
  72. /* here check if rtc can be accessed */
  73. while ((mc146818_read8(RTC_CONFIG_A) & 0x80) == 0x80)
  74. ;
  75. sec = mc146818_read8(RTC_SECONDS);
  76. min = mc146818_read8(RTC_MINUTES);
  77. hour = mc146818_read8(RTC_HOURS);
  78. mday = mc146818_read8(RTC_DATE_OF_MONTH);
  79. wday = mc146818_read8(RTC_DAY_OF_WEEK);
  80. mon = mc146818_read8(RTC_MONTH);
  81. year = mc146818_read8(RTC_YEAR);
  82. #ifdef RTC_DEBUG
  83. printf("Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x hr: %02x min: %02x sec: %02x\n",
  84. year, mon, mday, wday, hour, min, sec);
  85. printf("Alarms: mday: %02x hour: %02x min: %02x sec: %02x\n",
  86. mc146818_read8(RTC_CONFIG_D) & 0x3f,
  87. mc146818_read8(RTC_HOURS_ALARM),
  88. mc146818_read8(RTC_MINUTES_ALARM),
  89. mc146818_read8(RTC_SECONDS_ALARM));
  90. #endif
  91. tmp->tm_sec = bcd2bin(sec & 0x7f);
  92. tmp->tm_min = bcd2bin(min & 0x7f);
  93. tmp->tm_hour = bcd2bin(hour & 0x3f);
  94. tmp->tm_mday = bcd2bin(mday & 0x3f);
  95. tmp->tm_mon = bcd2bin(mon & 0x1f);
  96. tmp->tm_year = bcd2bin(year);
  97. tmp->tm_wday = bcd2bin(wday & 0x07);
  98. if (tmp->tm_year < 70)
  99. tmp->tm_year += 2000;
  100. else
  101. tmp->tm_year += 1900;
  102. tmp->tm_yday = 0;
  103. tmp->tm_isdst = 0;
  104. #ifdef RTC_DEBUG
  105. printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  106. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  107. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  108. #endif
  109. return 0;
  110. }
  111. static int mc146818_set(struct rtc_time *tmp)
  112. {
  113. #ifdef RTC_DEBUG
  114. printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  115. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  116. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  117. #endif
  118. /* Disable the RTC to update the regs */
  119. mc146818_write8(RTC_CONFIG_B, 0x82);
  120. mc146818_write8(RTC_YEAR, bin2bcd(tmp->tm_year % 100));
  121. mc146818_write8(RTC_MONTH, bin2bcd(tmp->tm_mon));
  122. mc146818_write8(RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
  123. mc146818_write8(RTC_DATE_OF_MONTH, bin2bcd(tmp->tm_mday));
  124. mc146818_write8(RTC_HOURS, bin2bcd(tmp->tm_hour));
  125. mc146818_write8(RTC_MINUTES, bin2bcd(tmp->tm_min));
  126. mc146818_write8(RTC_SECONDS, bin2bcd(tmp->tm_sec));
  127. /* Enable the RTC to update the regs */
  128. mc146818_write8(RTC_CONFIG_B, 0x02);
  129. return 0;
  130. }
  131. static void mc146818_reset(void)
  132. {
  133. /* Disable the RTC to update the regs */
  134. mc146818_write8(RTC_CONFIG_B, 0x82);
  135. /* Normal OP */
  136. mc146818_write8(RTC_CONFIG_A, 0x20);
  137. mc146818_write8(RTC_CONFIG_B, 0x00);
  138. mc146818_write8(RTC_CONFIG_B, 0x00);
  139. /* Enable the RTC to update the regs */
  140. mc146818_write8(RTC_CONFIG_B, 0x02);
  141. }
  142. static void mc146818_init(void)
  143. {
  144. #if CLEAR_CMOS
  145. int i;
  146. rtc_write8(RTC_SECONDS_ALARM, 0);
  147. rtc_write8(RTC_MINUTES_ALARM, 0);
  148. rtc_write8(RTC_HOURS_ALARM, 0);
  149. for (i = RTC_CONFIG_A; i < RTC_REG_SIZE; i++)
  150. rtc_write8(i, 0);
  151. printf("RTC: zeroing CMOS RAM\n");
  152. #endif
  153. /* Setup the real time clock */
  154. mc146818_write8(RTC_CONFIG_B, RTC_CONFIG_B_24H);
  155. /* Setup the frequency it operates at */
  156. mc146818_write8(RTC_CONFIG_A, RTC_CONFIG_A_REF_CLCK_32KHZ |
  157. RTC_CONFIG_A_RATE_1024HZ);
  158. /* Ensure all reserved bits are 0 in register D */
  159. mc146818_write8(RTC_CONFIG_D, RTC_CONFIG_D_VALID_RAM_AND_TIME);
  160. /* Clear any pending interrupts */
  161. mc146818_read8(RTC_CONFIG_C);
  162. }
  163. #endif /* CONFIG_CMD_DATE */
  164. #ifdef CONFIG_DM_RTC
  165. static int rtc_mc146818_get(struct udevice *dev, struct rtc_time *time)
  166. {
  167. return mc146818_get(time);
  168. }
  169. static int rtc_mc146818_set(struct udevice *dev, const struct rtc_time *time)
  170. {
  171. return mc146818_set((struct rtc_time *)time);
  172. }
  173. static int rtc_mc146818_reset(struct udevice *dev)
  174. {
  175. mc146818_reset();
  176. return 0;
  177. }
  178. static int rtc_mc146818_read8(struct udevice *dev, unsigned int reg)
  179. {
  180. return mc146818_read8(reg);
  181. }
  182. static int rtc_mc146818_write8(struct udevice *dev, unsigned int reg, int val)
  183. {
  184. mc146818_write8(reg, val);
  185. return 0;
  186. }
  187. static int rtc_mc146818_probe(struct udevice *dev)
  188. {
  189. mc146818_init();
  190. return 0;
  191. }
  192. static const struct rtc_ops rtc_mc146818_ops = {
  193. .get = rtc_mc146818_get,
  194. .set = rtc_mc146818_set,
  195. .reset = rtc_mc146818_reset,
  196. .read8 = rtc_mc146818_read8,
  197. .write8 = rtc_mc146818_write8,
  198. };
  199. static const struct udevice_id rtc_mc146818_ids[] = {
  200. { .compatible = "motorola,mc146818" },
  201. { }
  202. };
  203. U_BOOT_DRIVER(rtc_mc146818) = {
  204. .name = "rtc_mc146818",
  205. .id = UCLASS_RTC,
  206. .of_match = rtc_mc146818_ids,
  207. .probe = rtc_mc146818_probe,
  208. .ops = &rtc_mc146818_ops,
  209. };
  210. #else /* !CONFIG_DM_RTC */
  211. int rtc_get(struct rtc_time *tmp)
  212. {
  213. return mc146818_get(tmp);
  214. }
  215. int rtc_set(struct rtc_time *tmp)
  216. {
  217. return mc146818_set(tmp);
  218. }
  219. void rtc_reset(void)
  220. {
  221. mc146818_reset();
  222. }
  223. int rtc_read8(int reg)
  224. {
  225. return mc146818_read8(reg);
  226. }
  227. void rtc_write8(int reg, uchar val)
  228. {
  229. mc146818_write8(reg, val);
  230. }
  231. u32 rtc_read32(int reg)
  232. {
  233. u32 value = 0;
  234. int i;
  235. for (i = 0; i < sizeof(value); i++)
  236. value |= rtc_read8(reg + i) << (i << 3);
  237. return value;
  238. }
  239. void rtc_write32(int reg, u32 value)
  240. {
  241. int i;
  242. for (i = 0; i < sizeof(value); i++)
  243. rtc_write8(reg + i, (value >> (i << 3)) & 0xff);
  244. }
  245. void rtc_init(void)
  246. {
  247. mc146818_init();
  248. }
  249. #endif /* CONFIG_DM_RTC */