rtc-sa1100.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. /*
  42. * Calculate the next alarm time given the requested alarm time mask
  43. * and the current time.
  44. */
  45. static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
  46. struct rtc_time *alrm)
  47. {
  48. unsigned long next_time;
  49. unsigned long now_time;
  50. next->tm_year = now->tm_year;
  51. next->tm_mon = now->tm_mon;
  52. next->tm_mday = now->tm_mday;
  53. next->tm_hour = alrm->tm_hour;
  54. next->tm_min = alrm->tm_min;
  55. next->tm_sec = alrm->tm_sec;
  56. rtc_tm_to_time(now, &now_time);
  57. rtc_tm_to_time(next, &next_time);
  58. if (next_time < now_time) {
  59. /* Advance one day */
  60. next_time += 60 * 60 * 24;
  61. rtc_time_to_tm(next_time, next);
  62. }
  63. }
  64. static int rtc_update_alarm(struct rtc_time *alrm)
  65. {
  66. struct rtc_time alarm_tm, now_tm;
  67. unsigned long now, time;
  68. int ret;
  69. do {
  70. now = RCNR;
  71. rtc_time_to_tm(now, &now_tm);
  72. rtc_next_alarm_time(&alarm_tm, &now_tm, alrm);
  73. ret = rtc_tm_to_time(&alarm_tm, &time);
  74. if (ret != 0)
  75. break;
  76. RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL);
  77. RTAR = time;
  78. } while (now != RCNR);
  79. return ret;
  80. }
  81. static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
  82. {
  83. struct platform_device *pdev = to_platform_device(dev_id);
  84. struct rtc_device *rtc = platform_get_drvdata(pdev);
  85. unsigned int rtsr;
  86. unsigned long events = 0;
  87. spin_lock(&sa1100_rtc_lock);
  88. rtsr = RTSR;
  89. /* clear interrupt sources */
  90. RTSR = 0;
  91. /* Fix for a nasty initialization problem the in SA11xx RTSR register.
  92. * See also the comments in sa1100_rtc_probe(). */
  93. if (rtsr & (RTSR_ALE | RTSR_HZE)) {
  94. /* This is the original code, before there was the if test
  95. * above. This code does not clear interrupts that were not
  96. * enabled. */
  97. RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
  98. } else {
  99. /* For some reason, it is possible to enter this routine
  100. * without interruptions enabled, it has been tested with
  101. * several units (Bug in SA11xx chip?).
  102. *
  103. * This situation leads to an infinite "loop" of interrupt
  104. * routine calling and as a result the processor seems to
  105. * lock on its first call to open(). */
  106. RTSR = RTSR_AL | RTSR_HZ;
  107. }
  108. /* clear alarm interrupt if it has occurred */
  109. if (rtsr & RTSR_AL)
  110. rtsr &= ~RTSR_ALE;
  111. RTSR = rtsr & (RTSR_ALE | RTSR_HZE);
  112. /* update irq data & counter */
  113. if (rtsr & RTSR_AL)
  114. events |= RTC_AF | RTC_IRQF;
  115. if (rtsr & RTSR_HZ)
  116. events |= RTC_UF | RTC_IRQF;
  117. rtc_update_irq(rtc, 1, events);
  118. spin_unlock(&sa1100_rtc_lock);
  119. return IRQ_HANDLED;
  120. }
  121. static int sa1100_rtc_open(struct device *dev)
  122. {
  123. int ret;
  124. struct platform_device *plat_dev = to_platform_device(dev);
  125. struct rtc_device *rtc = platform_get_drvdata(plat_dev);
  126. ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED,
  127. "rtc 1Hz", dev);
  128. if (ret) {
  129. dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz);
  130. goto fail_ui;
  131. }
  132. ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED,
  133. "rtc Alrm", dev);
  134. if (ret) {
  135. dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm);
  136. goto fail_ai;
  137. }
  138. rtc->max_user_freq = RTC_FREQ;
  139. rtc_irq_set_freq(rtc, NULL, RTC_FREQ);
  140. return 0;
  141. fail_ai:
  142. free_irq(IRQ_RTC1Hz, dev);
  143. fail_ui:
  144. return ret;
  145. }
  146. static void sa1100_rtc_release(struct device *dev)
  147. {
  148. spin_lock_irq(&sa1100_rtc_lock);
  149. RTSR = 0;
  150. spin_unlock_irq(&sa1100_rtc_lock);
  151. free_irq(IRQ_RTCAlrm, dev);
  152. free_irq(IRQ_RTC1Hz, dev);
  153. }
  154. static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  155. {
  156. spin_lock_irq(&sa1100_rtc_lock);
  157. if (enabled)
  158. RTSR |= RTSR_ALE;
  159. else
  160. RTSR &= ~RTSR_ALE;
  161. spin_unlock_irq(&sa1100_rtc_lock);
  162. return 0;
  163. }
  164. static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
  165. {
  166. rtc_time_to_tm(RCNR, tm);
  167. return 0;
  168. }
  169. static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
  170. {
  171. unsigned long time;
  172. int ret;
  173. ret = rtc_tm_to_time(tm, &time);
  174. if (ret == 0)
  175. RCNR = time;
  176. return ret;
  177. }
  178. static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  179. {
  180. u32 rtsr;
  181. rtsr = RTSR;
  182. alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
  183. alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
  184. return 0;
  185. }
  186. static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  187. {
  188. int ret;
  189. spin_lock_irq(&sa1100_rtc_lock);
  190. ret = rtc_update_alarm(&alrm->time);
  191. if (ret == 0) {
  192. if (alrm->enabled)
  193. RTSR |= RTSR_ALE;
  194. else
  195. RTSR &= ~RTSR_ALE;
  196. }
  197. spin_unlock_irq(&sa1100_rtc_lock);
  198. return ret;
  199. }
  200. static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
  201. {
  202. seq_printf(seq, "trim/divider\t\t: 0x%08x\n", (u32) RTTR);
  203. seq_printf(seq, "RTSR\t\t\t: 0x%08x\n", (u32)RTSR);
  204. return 0;
  205. }
  206. static const struct rtc_class_ops sa1100_rtc_ops = {
  207. .open = sa1100_rtc_open,
  208. .release = sa1100_rtc_release,
  209. .read_time = sa1100_rtc_read_time,
  210. .set_time = sa1100_rtc_set_time,
  211. .read_alarm = sa1100_rtc_read_alarm,
  212. .set_alarm = sa1100_rtc_set_alarm,
  213. .proc = sa1100_rtc_proc,
  214. .alarm_irq_enable = sa1100_rtc_alarm_irq_enable,
  215. };
  216. static int sa1100_rtc_probe(struct platform_device *pdev)
  217. {
  218. struct rtc_device *rtc;
  219. /*
  220. * According to the manual we should be able to let RTTR be zero
  221. * and then a default diviser for a 32.768KHz clock is used.
  222. * Apparently this doesn't work, at least for my SA1110 rev 5.
  223. * If the clock divider is uninitialized then reset it to the
  224. * default value to get the 1Hz clock.
  225. */
  226. if (RTTR == 0) {
  227. RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
  228. dev_warn(&pdev->dev, "warning: "
  229. "initializing default clock divider/trim value\n");
  230. /* The current RTC value probably doesn't make sense either */
  231. RCNR = 0;
  232. }
  233. device_init_wakeup(&pdev->dev, 1);
  234. rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
  235. THIS_MODULE);
  236. if (IS_ERR(rtc))
  237. return PTR_ERR(rtc);
  238. platform_set_drvdata(pdev, rtc);
  239. /* Fix for a nasty initialization problem the in SA11xx RTSR register.
  240. * See also the comments in sa1100_rtc_interrupt().
  241. *
  242. * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
  243. * interrupt pending, even though interrupts were never enabled.
  244. * In this case, this bit it must be reset before enabling
  245. * interruptions to avoid a nonexistent interrupt to occur.
  246. *
  247. * In principle, the same problem would apply to bit 0, although it has
  248. * never been observed to happen.
  249. *
  250. * This issue is addressed both here and in sa1100_rtc_interrupt().
  251. * If the issue is not addressed here, in the times when the processor
  252. * wakes up with the bit set there will be one spurious interrupt.
  253. *
  254. * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
  255. * safe side, once the condition that lead to this strange
  256. * initialization is unknown and could in principle happen during
  257. * normal processing.
  258. *
  259. * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
  260. * the corresponding bits in RTSR. */
  261. RTSR = RTSR_AL | RTSR_HZ;
  262. return 0;
  263. }
  264. static int sa1100_rtc_remove(struct platform_device *pdev)
  265. {
  266. struct rtc_device *rtc = platform_get_drvdata(pdev);
  267. if (rtc)
  268. rtc_device_unregister(rtc);
  269. return 0;
  270. }
  271. #ifdef CONFIG_PM
  272. static int sa1100_rtc_suspend(struct device *dev)
  273. {
  274. if (device_may_wakeup(dev))
  275. enable_irq_wake(IRQ_RTCAlrm);
  276. return 0;
  277. }
  278. static int sa1100_rtc_resume(struct device *dev)
  279. {
  280. if (device_may_wakeup(dev))
  281. disable_irq_wake(IRQ_RTCAlrm);
  282. return 0;
  283. }
  284. static const struct dev_pm_ops sa1100_rtc_pm_ops = {
  285. .suspend = sa1100_rtc_suspend,
  286. .resume = sa1100_rtc_resume,
  287. };
  288. #endif
  289. static struct platform_driver sa1100_rtc_driver = {
  290. .probe = sa1100_rtc_probe,
  291. .remove = sa1100_rtc_remove,
  292. .driver = {
  293. .name = "sa1100-rtc",
  294. #ifdef CONFIG_PM
  295. .pm = &sa1100_rtc_pm_ops,
  296. #endif
  297. },
  298. };
  299. module_platform_driver(sa1100_rtc_driver);
  300. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  301. MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
  302. MODULE_LICENSE("GPL");
  303. MODULE_ALIAS("platform:sa1100-rtc");