rtc-sa1100.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Real Time Clock interface for StrongARM SA1x00 and XScale PXA2xx
  3. *
  4. * Copyright (c) 2000 Nils Faerber
  5. *
  6. * Based on rtc.c by Paul Gortmaker
  7. *
  8. * Original Driver by Nils Faerber <nils@kernelconcepts.de>
  9. *
  10. * Modifications from:
  11. * CIH <cih@coventive.com>
  12. * Nicolas Pitre <nico@fluxnic.net>
  13. * Andrew Christian <andrew.christian@hp.com>
  14. *
  15. * Converted to the RTC subsystem and Driver Model
  16. * by Richard Purdie <rpurdie@rpsys.net>
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version
  21. * 2 of the License, or (at your option) any later version.
  22. */
  23. #include <linux/platform_device.h>
  24. #include <linux/module.h>
  25. #include <linux/rtc.h>
  26. #include <linux/init.h>
  27. #include <linux/fs.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/string.h>
  30. #include <linux/pm.h>
  31. #include <linux/bitops.h>
  32. #include <mach/hardware.h>
  33. #include <asm/irq.h>
  34. #ifdef CONFIG_ARCH_PXA
  35. #include <mach/regs-rtc.h>
  36. #endif
  37. #define RTC_DEF_DIVIDER (32768 - 1)
  38. #define RTC_DEF_TRIM 0
  39. static const unsigned long RTC_FREQ = 1024;
  40. static DEFINE_SPINLOCK(sa1100_rtc_lock);
  41. static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
  42. {
  43. struct platform_device *pdev = to_platform_device(dev_id);
  44. struct rtc_device *rtc = platform_get_drvdata(pdev);
  45. unsigned int rtsr;
  46. unsigned long events = 0;
  47. spin_lock(&sa1100_rtc_lock);
  48. rtsr = RTSR;
  49. /* clear interrupt sources */
  50. RTSR = 0;
  51. /* Fix for a nasty initialization problem the in SA11xx RTSR register.
  52. * See also the comments in sa1100_rtc_probe(). */
  53. if (rtsr & (RTSR_ALE | RTSR_HZE)) {
  54. /* This is the original code, before there was the if test
  55. * above. This code does not clear interrupts that were not
  56. * enabled. */
  57. RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
  58. } else {
  59. /* For some reason, it is possible to enter this routine
  60. * without interruptions enabled, it has been tested with
  61. * several units (Bug in SA11xx chip?).
  62. *
  63. * This situation leads to an infinite "loop" of interrupt
  64. * routine calling and as a result the processor seems to
  65. * lock on its first call to open(). */
  66. RTSR = RTSR_AL | RTSR_HZ;
  67. }
  68. /* clear alarm interrupt if it has occurred */
  69. if (rtsr & RTSR_AL)
  70. rtsr &= ~RTSR_ALE;
  71. RTSR = rtsr & (RTSR_ALE | RTSR_HZE);
  72. /* update irq data & counter */
  73. if (rtsr & RTSR_AL)
  74. events |= RTC_AF | RTC_IRQF;
  75. if (rtsr & RTSR_HZ)
  76. events |= RTC_UF | RTC_IRQF;
  77. rtc_update_irq(rtc, 1, events);
  78. spin_unlock(&sa1100_rtc_lock);
  79. return IRQ_HANDLED;
  80. }
  81. static int sa1100_rtc_open(struct device *dev)
  82. {
  83. int ret;
  84. struct platform_device *plat_dev = to_platform_device(dev);
  85. struct rtc_device *rtc = platform_get_drvdata(plat_dev);
  86. ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED,
  87. "rtc 1Hz", dev);
  88. if (ret) {
  89. dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz);
  90. goto fail_ui;
  91. }
  92. ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED,
  93. "rtc Alrm", dev);
  94. if (ret) {
  95. dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm);
  96. goto fail_ai;
  97. }
  98. rtc->max_user_freq = RTC_FREQ;
  99. rtc_irq_set_freq(rtc, NULL, RTC_FREQ);
  100. return 0;
  101. fail_ai:
  102. free_irq(IRQ_RTC1Hz, dev);
  103. fail_ui:
  104. return ret;
  105. }
  106. static void sa1100_rtc_release(struct device *dev)
  107. {
  108. spin_lock_irq(&sa1100_rtc_lock);
  109. RTSR = 0;
  110. spin_unlock_irq(&sa1100_rtc_lock);
  111. free_irq(IRQ_RTCAlrm, dev);
  112. free_irq(IRQ_RTC1Hz, dev);
  113. }
  114. static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  115. {
  116. spin_lock_irq(&sa1100_rtc_lock);
  117. if (enabled)
  118. RTSR |= RTSR_ALE;
  119. else
  120. RTSR &= ~RTSR_ALE;
  121. spin_unlock_irq(&sa1100_rtc_lock);
  122. return 0;
  123. }
  124. static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
  125. {
  126. rtc_time_to_tm(RCNR, tm);
  127. return 0;
  128. }
  129. static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
  130. {
  131. unsigned long time;
  132. int ret;
  133. ret = rtc_tm_to_time(tm, &time);
  134. if (ret == 0)
  135. RCNR = time;
  136. return ret;
  137. }
  138. static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  139. {
  140. u32 rtsr;
  141. rtsr = RTSR;
  142. alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
  143. alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
  144. return 0;
  145. }
  146. static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  147. {
  148. unsigned long time;
  149. int ret;
  150. spin_lock_irq(&sa1100_rtc_lock);
  151. ret = rtc_tm_to_time(&alrm->time, &time);
  152. if (ret != 0)
  153. goto out;
  154. RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL);
  155. RTAR = time;
  156. if (alrm->enabled)
  157. RTSR |= RTSR_ALE;
  158. else
  159. RTSR &= ~RTSR_ALE;
  160. out:
  161. spin_unlock_irq(&sa1100_rtc_lock);
  162. return ret;
  163. }
  164. static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
  165. {
  166. seq_printf(seq, "trim/divider\t\t: 0x%08x\n", (u32) RTTR);
  167. seq_printf(seq, "RTSR\t\t\t: 0x%08x\n", (u32)RTSR);
  168. return 0;
  169. }
  170. static const struct rtc_class_ops sa1100_rtc_ops = {
  171. .open = sa1100_rtc_open,
  172. .release = sa1100_rtc_release,
  173. .read_time = sa1100_rtc_read_time,
  174. .set_time = sa1100_rtc_set_time,
  175. .read_alarm = sa1100_rtc_read_alarm,
  176. .set_alarm = sa1100_rtc_set_alarm,
  177. .proc = sa1100_rtc_proc,
  178. .alarm_irq_enable = sa1100_rtc_alarm_irq_enable,
  179. };
  180. static int sa1100_rtc_probe(struct platform_device *pdev)
  181. {
  182. struct rtc_device *rtc;
  183. /*
  184. * According to the manual we should be able to let RTTR be zero
  185. * and then a default diviser for a 32.768KHz clock is used.
  186. * Apparently this doesn't work, at least for my SA1110 rev 5.
  187. * If the clock divider is uninitialized then reset it to the
  188. * default value to get the 1Hz clock.
  189. */
  190. if (RTTR == 0) {
  191. RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
  192. dev_warn(&pdev->dev, "warning: "
  193. "initializing default clock divider/trim value\n");
  194. /* The current RTC value probably doesn't make sense either */
  195. RCNR = 0;
  196. }
  197. device_init_wakeup(&pdev->dev, 1);
  198. rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
  199. THIS_MODULE);
  200. if (IS_ERR(rtc))
  201. return PTR_ERR(rtc);
  202. platform_set_drvdata(pdev, rtc);
  203. /* Fix for a nasty initialization problem the in SA11xx RTSR register.
  204. * See also the comments in sa1100_rtc_interrupt().
  205. *
  206. * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
  207. * interrupt pending, even though interrupts were never enabled.
  208. * In this case, this bit it must be reset before enabling
  209. * interruptions to avoid a nonexistent interrupt to occur.
  210. *
  211. * In principle, the same problem would apply to bit 0, although it has
  212. * never been observed to happen.
  213. *
  214. * This issue is addressed both here and in sa1100_rtc_interrupt().
  215. * If the issue is not addressed here, in the times when the processor
  216. * wakes up with the bit set there will be one spurious interrupt.
  217. *
  218. * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
  219. * safe side, once the condition that lead to this strange
  220. * initialization is unknown and could in principle happen during
  221. * normal processing.
  222. *
  223. * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
  224. * the corresponding bits in RTSR. */
  225. RTSR = RTSR_AL | RTSR_HZ;
  226. return 0;
  227. }
  228. static int sa1100_rtc_remove(struct platform_device *pdev)
  229. {
  230. struct rtc_device *rtc = platform_get_drvdata(pdev);
  231. if (rtc)
  232. rtc_device_unregister(rtc);
  233. return 0;
  234. }
  235. #ifdef CONFIG_PM
  236. static int sa1100_rtc_suspend(struct device *dev)
  237. {
  238. if (device_may_wakeup(dev))
  239. enable_irq_wake(IRQ_RTCAlrm);
  240. return 0;
  241. }
  242. static int sa1100_rtc_resume(struct device *dev)
  243. {
  244. if (device_may_wakeup(dev))
  245. disable_irq_wake(IRQ_RTCAlrm);
  246. return 0;
  247. }
  248. static const struct dev_pm_ops sa1100_rtc_pm_ops = {
  249. .suspend = sa1100_rtc_suspend,
  250. .resume = sa1100_rtc_resume,
  251. };
  252. #endif
  253. static struct platform_driver sa1100_rtc_driver = {
  254. .probe = sa1100_rtc_probe,
  255. .remove = sa1100_rtc_remove,
  256. .driver = {
  257. .name = "sa1100-rtc",
  258. #ifdef CONFIG_PM
  259. .pm = &sa1100_rtc_pm_ops,
  260. #endif
  261. },
  262. };
  263. module_platform_driver(sa1100_rtc_driver);
  264. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  265. MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
  266. MODULE_LICENSE("GPL");
  267. MODULE_ALIAS("platform:sa1100-rtc");