rtc-sa1100.c 9.6 KB

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