rtc-ls1x.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2011 Zhao Zhang <zhzhl555@gmail.com>
  3. *
  4. * Derived from driver/rtc/rtc-au1xxx.c
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/rtc.h>
  14. #include <linux/init.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/delay.h>
  17. #include <linux/types.h>
  18. #include <linux/io.h>
  19. #include <loongson1.h>
  20. #define LS1X_RTC_REG_OFFSET (LS1X_RTC_BASE + 0x20)
  21. #define LS1X_RTC_REGS(x) \
  22. ((void __iomem *)KSEG1ADDR(LS1X_RTC_REG_OFFSET + (x)))
  23. /*RTC programmable counters 0 and 1*/
  24. #define SYS_COUNTER_CNTRL (LS1X_RTC_REGS(0x20))
  25. #define SYS_CNTRL_ERS (1 << 23)
  26. #define SYS_CNTRL_RTS (1 << 20)
  27. #define SYS_CNTRL_RM2 (1 << 19)
  28. #define SYS_CNTRL_RM1 (1 << 18)
  29. #define SYS_CNTRL_RM0 (1 << 17)
  30. #define SYS_CNTRL_RS (1 << 16)
  31. #define SYS_CNTRL_BP (1 << 14)
  32. #define SYS_CNTRL_REN (1 << 13)
  33. #define SYS_CNTRL_BRT (1 << 12)
  34. #define SYS_CNTRL_TEN (1 << 11)
  35. #define SYS_CNTRL_BTT (1 << 10)
  36. #define SYS_CNTRL_E0 (1 << 8)
  37. #define SYS_CNTRL_ETS (1 << 7)
  38. #define SYS_CNTRL_32S (1 << 5)
  39. #define SYS_CNTRL_TTS (1 << 4)
  40. #define SYS_CNTRL_TM2 (1 << 3)
  41. #define SYS_CNTRL_TM1 (1 << 2)
  42. #define SYS_CNTRL_TM0 (1 << 1)
  43. #define SYS_CNTRL_TS (1 << 0)
  44. /* Programmable Counter 0 Registers */
  45. #define SYS_TOYTRIM (LS1X_RTC_REGS(0))
  46. #define SYS_TOYWRITE0 (LS1X_RTC_REGS(4))
  47. #define SYS_TOYWRITE1 (LS1X_RTC_REGS(8))
  48. #define SYS_TOYREAD0 (LS1X_RTC_REGS(0xC))
  49. #define SYS_TOYREAD1 (LS1X_RTC_REGS(0x10))
  50. #define SYS_TOYMATCH0 (LS1X_RTC_REGS(0x14))
  51. #define SYS_TOYMATCH1 (LS1X_RTC_REGS(0x18))
  52. #define SYS_TOYMATCH2 (LS1X_RTC_REGS(0x1C))
  53. /* Programmable Counter 1 Registers */
  54. #define SYS_RTCTRIM (LS1X_RTC_REGS(0x40))
  55. #define SYS_RTCWRITE0 (LS1X_RTC_REGS(0x44))
  56. #define SYS_RTCREAD0 (LS1X_RTC_REGS(0x48))
  57. #define SYS_RTCMATCH0 (LS1X_RTC_REGS(0x4C))
  58. #define SYS_RTCMATCH1 (LS1X_RTC_REGS(0x50))
  59. #define SYS_RTCMATCH2 (LS1X_RTC_REGS(0x54))
  60. #define LS1X_SEC_OFFSET (4)
  61. #define LS1X_MIN_OFFSET (10)
  62. #define LS1X_HOUR_OFFSET (16)
  63. #define LS1X_DAY_OFFSET (21)
  64. #define LS1X_MONTH_OFFSET (26)
  65. #define LS1X_SEC_MASK (0x3f)
  66. #define LS1X_MIN_MASK (0x3f)
  67. #define LS1X_HOUR_MASK (0x1f)
  68. #define LS1X_DAY_MASK (0x1f)
  69. #define LS1X_MONTH_MASK (0x3f)
  70. #define LS1X_YEAR_MASK (0xffffffff)
  71. #define ls1x_get_sec(t) (((t) >> LS1X_SEC_OFFSET) & LS1X_SEC_MASK)
  72. #define ls1x_get_min(t) (((t) >> LS1X_MIN_OFFSET) & LS1X_MIN_MASK)
  73. #define ls1x_get_hour(t) (((t) >> LS1X_HOUR_OFFSET) & LS1X_HOUR_MASK)
  74. #define ls1x_get_day(t) (((t) >> LS1X_DAY_OFFSET) & LS1X_DAY_MASK)
  75. #define ls1x_get_month(t) (((t) >> LS1X_MONTH_OFFSET) & LS1X_MONTH_MASK)
  76. #define RTC_CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
  77. static int ls1x_rtc_read_time(struct device *dev, struct rtc_time *rtm)
  78. {
  79. unsigned long v;
  80. time64_t t;
  81. v = readl(SYS_TOYREAD0);
  82. t = readl(SYS_TOYREAD1);
  83. memset(rtm, 0, sizeof(struct rtc_time));
  84. t = mktime64((t & LS1X_YEAR_MASK), ls1x_get_month(v),
  85. ls1x_get_day(v), ls1x_get_hour(v),
  86. ls1x_get_min(v), ls1x_get_sec(v));
  87. rtc_time64_to_tm(t, rtm);
  88. return 0;
  89. }
  90. static int ls1x_rtc_set_time(struct device *dev, struct rtc_time *rtm)
  91. {
  92. unsigned long v, t, c;
  93. int ret = -ETIMEDOUT;
  94. v = ((rtm->tm_mon + 1) << LS1X_MONTH_OFFSET)
  95. | (rtm->tm_mday << LS1X_DAY_OFFSET)
  96. | (rtm->tm_hour << LS1X_HOUR_OFFSET)
  97. | (rtm->tm_min << LS1X_MIN_OFFSET)
  98. | (rtm->tm_sec << LS1X_SEC_OFFSET);
  99. writel(v, SYS_TOYWRITE0);
  100. c = 0x10000;
  101. /* add timeout check counter, for more safe */
  102. while ((readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_TS) && --c)
  103. usleep_range(1000, 3000);
  104. if (!c) {
  105. dev_err(dev, "set time timeout!\n");
  106. goto err;
  107. }
  108. t = rtm->tm_year + 1900;
  109. writel(t, SYS_TOYWRITE1);
  110. c = 0x10000;
  111. while ((readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_TS) && --c)
  112. usleep_range(1000, 3000);
  113. if (!c) {
  114. dev_err(dev, "set time timeout!\n");
  115. goto err;
  116. }
  117. return 0;
  118. err:
  119. return ret;
  120. }
  121. static const struct rtc_class_ops ls1x_rtc_ops = {
  122. .read_time = ls1x_rtc_read_time,
  123. .set_time = ls1x_rtc_set_time,
  124. };
  125. static int ls1x_rtc_probe(struct platform_device *pdev)
  126. {
  127. struct rtc_device *rtcdev;
  128. unsigned long v;
  129. v = readl(SYS_COUNTER_CNTRL);
  130. if (!(v & RTC_CNTR_OK)) {
  131. dev_err(&pdev->dev, "rtc counters not working\n");
  132. return -ENODEV;
  133. }
  134. /* set to 1 HZ if needed */
  135. if (readl(SYS_TOYTRIM) != 32767) {
  136. v = 0x100000;
  137. while ((readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_TTS) && --v)
  138. usleep_range(1000, 3000);
  139. if (!v) {
  140. dev_err(&pdev->dev, "time out\n");
  141. return -ETIMEDOUT;
  142. }
  143. writel(32767, SYS_TOYTRIM);
  144. }
  145. /* this loop coundn't be endless */
  146. while (readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_TTS)
  147. usleep_range(1000, 3000);
  148. rtcdev = devm_rtc_allocate_device(&pdev->dev);
  149. if (IS_ERR(rtcdev))
  150. return PTR_ERR(rtcdev);
  151. platform_set_drvdata(pdev, rtcdev);
  152. rtcdev->ops = &ls1x_rtc_ops;
  153. rtcdev->range_min = RTC_TIMESTAMP_BEGIN_1900;
  154. rtcdev->range_max = RTC_TIMESTAMP_END_2099;
  155. return rtc_register_device(rtcdev);
  156. }
  157. static struct platform_driver ls1x_rtc_driver = {
  158. .driver = {
  159. .name = "ls1x-rtc",
  160. },
  161. .probe = ls1x_rtc_probe,
  162. };
  163. module_platform_driver(ls1x_rtc_driver);
  164. MODULE_AUTHOR("zhao zhang <zhzhl555@gmail.com>");
  165. MODULE_LICENSE("GPL");