rtc-sa1100.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Real Time Clock interface for StrongARM SA1x00 and XScale PXA2xx
  4. *
  5. * Copyright (c) 2000 Nils Faerber
  6. *
  7. * Based on rtc.c by Paul Gortmaker
  8. *
  9. * Original Driver by Nils Faerber <[email protected]>
  10. *
  11. * Modifications from:
  12. * CIH <[email protected]>
  13. * Nicolas Pitre <[email protected]>
  14. * Andrew Christian <[email protected]>
  15. *
  16. * Converted to the RTC subsystem and Driver Model
  17. * by Richard Purdie <[email protected]>
  18. */
  19. #include <linux/platform_device.h>
  20. #include <linux/module.h>
  21. #include <linux/clk.h>
  22. #include <linux/rtc.h>
  23. #include <linux/init.h>
  24. #include <linux/fs.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/slab.h>
  27. #include <linux/string.h>
  28. #include <linux/of.h>
  29. #include <linux/pm.h>
  30. #include <linux/bitops.h>
  31. #include <linux/io.h>
  32. #define RTSR_HZE BIT(3) /* HZ interrupt enable */
  33. #define RTSR_ALE BIT(2) /* RTC alarm interrupt enable */
  34. #define RTSR_HZ BIT(1) /* HZ rising-edge detected */
  35. #define RTSR_AL BIT(0) /* RTC alarm detected */
  36. #include "rtc-sa1100.h"
  37. #define RTC_DEF_DIVIDER (32768 - 1)
  38. #define RTC_DEF_TRIM 0
  39. #define RTC_FREQ 1024
  40. static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
  41. {
  42. struct sa1100_rtc *info = dev_get_drvdata(dev_id);
  43. struct rtc_device *rtc = info->rtc;
  44. unsigned int rtsr;
  45. unsigned long events = 0;
  46. spin_lock(&info->lock);
  47. rtsr = readl_relaxed(info->rtsr);
  48. /* clear interrupt sources */
  49. writel_relaxed(0, info->rtsr);
  50. /* Fix for a nasty initialization problem the in SA11xx RTSR register.
  51. * See also the comments in sa1100_rtc_probe(). */
  52. if (rtsr & (RTSR_ALE | RTSR_HZE)) {
  53. /* This is the original code, before there was the if test
  54. * above. This code does not clear interrupts that were not
  55. * enabled. */
  56. writel_relaxed((RTSR_AL | RTSR_HZ) & (rtsr >> 2), info->rtsr);
  57. } else {
  58. /* For some reason, it is possible to enter this routine
  59. * without interruptions enabled, it has been tested with
  60. * several units (Bug in SA11xx chip?).
  61. *
  62. * This situation leads to an infinite "loop" of interrupt
  63. * routine calling and as a result the processor seems to
  64. * lock on its first call to open(). */
  65. writel_relaxed(RTSR_AL | RTSR_HZ, info->rtsr);
  66. }
  67. /* clear alarm interrupt if it has occurred */
  68. if (rtsr & RTSR_AL)
  69. rtsr &= ~RTSR_ALE;
  70. writel_relaxed(rtsr & (RTSR_ALE | RTSR_HZE), info->rtsr);
  71. /* update irq data & counter */
  72. if (rtsr & RTSR_AL)
  73. events |= RTC_AF | RTC_IRQF;
  74. if (rtsr & RTSR_HZ)
  75. events |= RTC_UF | RTC_IRQF;
  76. rtc_update_irq(rtc, 1, events);
  77. spin_unlock(&info->lock);
  78. return IRQ_HANDLED;
  79. }
  80. static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  81. {
  82. u32 rtsr;
  83. struct sa1100_rtc *info = dev_get_drvdata(dev);
  84. spin_lock_irq(&info->lock);
  85. rtsr = readl_relaxed(info->rtsr);
  86. if (enabled)
  87. rtsr |= RTSR_ALE;
  88. else
  89. rtsr &= ~RTSR_ALE;
  90. writel_relaxed(rtsr, info->rtsr);
  91. spin_unlock_irq(&info->lock);
  92. return 0;
  93. }
  94. static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
  95. {
  96. struct sa1100_rtc *info = dev_get_drvdata(dev);
  97. rtc_time64_to_tm(readl_relaxed(info->rcnr), tm);
  98. return 0;
  99. }
  100. static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
  101. {
  102. struct sa1100_rtc *info = dev_get_drvdata(dev);
  103. writel_relaxed(rtc_tm_to_time64(tm), info->rcnr);
  104. return 0;
  105. }
  106. static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  107. {
  108. u32 rtsr;
  109. struct sa1100_rtc *info = dev_get_drvdata(dev);
  110. rtsr = readl_relaxed(info->rtsr);
  111. alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
  112. alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
  113. return 0;
  114. }
  115. static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  116. {
  117. struct sa1100_rtc *info = dev_get_drvdata(dev);
  118. spin_lock_irq(&info->lock);
  119. writel_relaxed(readl_relaxed(info->rtsr) &
  120. (RTSR_HZE | RTSR_ALE | RTSR_AL), info->rtsr);
  121. writel_relaxed(rtc_tm_to_time64(&alrm->time), info->rtar);
  122. if (alrm->enabled)
  123. writel_relaxed(readl_relaxed(info->rtsr) | RTSR_ALE, info->rtsr);
  124. else
  125. writel_relaxed(readl_relaxed(info->rtsr) & ~RTSR_ALE, info->rtsr);
  126. spin_unlock_irq(&info->lock);
  127. return 0;
  128. }
  129. static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
  130. {
  131. struct sa1100_rtc *info = dev_get_drvdata(dev);
  132. seq_printf(seq, "trim/divider\t\t: 0x%08x\n", readl_relaxed(info->rttr));
  133. seq_printf(seq, "RTSR\t\t\t: 0x%08x\n", readl_relaxed(info->rtsr));
  134. return 0;
  135. }
  136. static const struct rtc_class_ops sa1100_rtc_ops = {
  137. .read_time = sa1100_rtc_read_time,
  138. .set_time = sa1100_rtc_set_time,
  139. .read_alarm = sa1100_rtc_read_alarm,
  140. .set_alarm = sa1100_rtc_set_alarm,
  141. .proc = sa1100_rtc_proc,
  142. .alarm_irq_enable = sa1100_rtc_alarm_irq_enable,
  143. };
  144. int sa1100_rtc_init(struct platform_device *pdev, struct sa1100_rtc *info)
  145. {
  146. int ret;
  147. spin_lock_init(&info->lock);
  148. info->clk = devm_clk_get(&pdev->dev, NULL);
  149. if (IS_ERR(info->clk)) {
  150. dev_err(&pdev->dev, "failed to find rtc clock source\n");
  151. return PTR_ERR(info->clk);
  152. }
  153. ret = clk_prepare_enable(info->clk);
  154. if (ret)
  155. return ret;
  156. /*
  157. * According to the manual we should be able to let RTTR be zero
  158. * and then a default diviser for a 32.768KHz clock is used.
  159. * Apparently this doesn't work, at least for my SA1110 rev 5.
  160. * If the clock divider is uninitialized then reset it to the
  161. * default value to get the 1Hz clock.
  162. */
  163. if (readl_relaxed(info->rttr) == 0) {
  164. writel_relaxed(RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16), info->rttr);
  165. dev_warn(&pdev->dev, "warning: "
  166. "initializing default clock divider/trim value\n");
  167. /* The current RTC value probably doesn't make sense either */
  168. writel_relaxed(0, info->rcnr);
  169. }
  170. info->rtc->ops = &sa1100_rtc_ops;
  171. info->rtc->max_user_freq = RTC_FREQ;
  172. info->rtc->range_max = U32_MAX;
  173. ret = devm_rtc_register_device(info->rtc);
  174. if (ret) {
  175. clk_disable_unprepare(info->clk);
  176. return ret;
  177. }
  178. /* Fix for a nasty initialization problem the in SA11xx RTSR register.
  179. * See also the comments in sa1100_rtc_interrupt().
  180. *
  181. * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
  182. * interrupt pending, even though interrupts were never enabled.
  183. * In this case, this bit it must be reset before enabling
  184. * interruptions to avoid a nonexistent interrupt to occur.
  185. *
  186. * In principle, the same problem would apply to bit 0, although it has
  187. * never been observed to happen.
  188. *
  189. * This issue is addressed both here and in sa1100_rtc_interrupt().
  190. * If the issue is not addressed here, in the times when the processor
  191. * wakes up with the bit set there will be one spurious interrupt.
  192. *
  193. * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
  194. * safe side, once the condition that lead to this strange
  195. * initialization is unknown and could in principle happen during
  196. * normal processing.
  197. *
  198. * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
  199. * the corresponding bits in RTSR. */
  200. writel_relaxed(RTSR_AL | RTSR_HZ, info->rtsr);
  201. return 0;
  202. }
  203. EXPORT_SYMBOL_GPL(sa1100_rtc_init);
  204. static int sa1100_rtc_probe(struct platform_device *pdev)
  205. {
  206. struct sa1100_rtc *info;
  207. void __iomem *base;
  208. int irq_1hz, irq_alarm;
  209. int ret;
  210. irq_1hz = platform_get_irq_byname(pdev, "rtc 1Hz");
  211. irq_alarm = platform_get_irq_byname(pdev, "rtc alarm");
  212. if (irq_1hz < 0 || irq_alarm < 0)
  213. return -ENODEV;
  214. info = devm_kzalloc(&pdev->dev, sizeof(struct sa1100_rtc), GFP_KERNEL);
  215. if (!info)
  216. return -ENOMEM;
  217. info->irq_1hz = irq_1hz;
  218. info->irq_alarm = irq_alarm;
  219. info->rtc = devm_rtc_allocate_device(&pdev->dev);
  220. if (IS_ERR(info->rtc))
  221. return PTR_ERR(info->rtc);
  222. ret = devm_request_irq(&pdev->dev, irq_1hz, sa1100_rtc_interrupt, 0,
  223. "rtc 1Hz", &pdev->dev);
  224. if (ret) {
  225. dev_err(&pdev->dev, "IRQ %d already in use.\n", irq_1hz);
  226. return ret;
  227. }
  228. ret = devm_request_irq(&pdev->dev, irq_alarm, sa1100_rtc_interrupt, 0,
  229. "rtc Alrm", &pdev->dev);
  230. if (ret) {
  231. dev_err(&pdev->dev, "IRQ %d already in use.\n", irq_alarm);
  232. return ret;
  233. }
  234. base = devm_platform_ioremap_resource(pdev, 0);
  235. if (IS_ERR(base))
  236. return PTR_ERR(base);
  237. if (IS_ENABLED(CONFIG_ARCH_SA1100) ||
  238. of_device_is_compatible(pdev->dev.of_node, "mrvl,sa1100-rtc")) {
  239. info->rcnr = base + 0x04;
  240. info->rtsr = base + 0x10;
  241. info->rtar = base + 0x00;
  242. info->rttr = base + 0x08;
  243. } else {
  244. info->rcnr = base + 0x0;
  245. info->rtsr = base + 0x8;
  246. info->rtar = base + 0x4;
  247. info->rttr = base + 0xc;
  248. }
  249. platform_set_drvdata(pdev, info);
  250. device_init_wakeup(&pdev->dev, 1);
  251. return sa1100_rtc_init(pdev, info);
  252. }
  253. static int sa1100_rtc_remove(struct platform_device *pdev)
  254. {
  255. struct sa1100_rtc *info = platform_get_drvdata(pdev);
  256. if (info) {
  257. spin_lock_irq(&info->lock);
  258. writel_relaxed(0, info->rtsr);
  259. spin_unlock_irq(&info->lock);
  260. clk_disable_unprepare(info->clk);
  261. }
  262. return 0;
  263. }
  264. #ifdef CONFIG_PM_SLEEP
  265. static int sa1100_rtc_suspend(struct device *dev)
  266. {
  267. struct sa1100_rtc *info = dev_get_drvdata(dev);
  268. if (device_may_wakeup(dev))
  269. enable_irq_wake(info->irq_alarm);
  270. return 0;
  271. }
  272. static int sa1100_rtc_resume(struct device *dev)
  273. {
  274. struct sa1100_rtc *info = dev_get_drvdata(dev);
  275. if (device_may_wakeup(dev))
  276. disable_irq_wake(info->irq_alarm);
  277. return 0;
  278. }
  279. #endif
  280. static SIMPLE_DEV_PM_OPS(sa1100_rtc_pm_ops, sa1100_rtc_suspend,
  281. sa1100_rtc_resume);
  282. #ifdef CONFIG_OF
  283. static const struct of_device_id sa1100_rtc_dt_ids[] = {
  284. { .compatible = "mrvl,sa1100-rtc", },
  285. { .compatible = "mrvl,mmp-rtc", },
  286. {}
  287. };
  288. MODULE_DEVICE_TABLE(of, sa1100_rtc_dt_ids);
  289. #endif
  290. static struct platform_driver sa1100_rtc_driver = {
  291. .probe = sa1100_rtc_probe,
  292. .remove = sa1100_rtc_remove,
  293. .driver = {
  294. .name = "sa1100-rtc",
  295. .pm = &sa1100_rtc_pm_ops,
  296. .of_match_table = of_match_ptr(sa1100_rtc_dt_ids),
  297. },
  298. };
  299. module_platform_driver(sa1100_rtc_driver);
  300. MODULE_AUTHOR("Richard Purdie <[email protected]>");
  301. MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
  302. MODULE_LICENSE("GPL");
  303. MODULE_ALIAS("platform:sa1100-rtc");