mc146818.c 6.8 KB

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