rtc-sa1100.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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@cam.org>
  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/pxa-regs.h>
  36. #endif
  37. #define TIMER_FREQ CLOCK_TICK_RATE
  38. #define RTC_DEF_DIVIDER 32768 - 1
  39. #define RTC_DEF_TRIM 0
  40. static unsigned long rtc_freq = 1024;
  41. static struct rtc_time rtc_alarm;
  42. static DEFINE_SPINLOCK(sa1100_rtc_lock);
  43. static inline int rtc_periodic_alarm(struct rtc_time *tm)
  44. {
  45. return (tm->tm_year == -1) ||
  46. ((unsigned)tm->tm_mon >= 12) ||
  47. ((unsigned)(tm->tm_mday - 1) >= 31) ||
  48. ((unsigned)tm->tm_hour > 23) ||
  49. ((unsigned)tm->tm_min > 59) ||
  50. ((unsigned)tm->tm_sec > 59);
  51. }
  52. /*
  53. * Calculate the next alarm time given the requested alarm time mask
  54. * and the current time.
  55. */
  56. static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, struct rtc_time *alrm)
  57. {
  58. unsigned long next_time;
  59. unsigned long now_time;
  60. next->tm_year = now->tm_year;
  61. next->tm_mon = now->tm_mon;
  62. next->tm_mday = now->tm_mday;
  63. next->tm_hour = alrm->tm_hour;
  64. next->tm_min = alrm->tm_min;
  65. next->tm_sec = alrm->tm_sec;
  66. rtc_tm_to_time(now, &now_time);
  67. rtc_tm_to_time(next, &next_time);
  68. if (next_time < now_time) {
  69. /* Advance one day */
  70. next_time += 60 * 60 * 24;
  71. rtc_time_to_tm(next_time, next);
  72. }
  73. }
  74. static int rtc_update_alarm(struct rtc_time *alrm)
  75. {
  76. struct rtc_time alarm_tm, now_tm;
  77. unsigned long now, time;
  78. int ret;
  79. do {
  80. now = RCNR;
  81. rtc_time_to_tm(now, &now_tm);
  82. rtc_next_alarm_time(&alarm_tm, &now_tm, alrm);
  83. ret = rtc_tm_to_time(&alarm_tm, &time);
  84. if (ret != 0)
  85. break;
  86. RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL);
  87. RTAR = time;
  88. } while (now != RCNR);
  89. return ret;
  90. }
  91. static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
  92. {
  93. struct platform_device *pdev = to_platform_device(dev_id);
  94. struct rtc_device *rtc = platform_get_drvdata(pdev);
  95. unsigned int rtsr;
  96. unsigned long events = 0;
  97. spin_lock(&sa1100_rtc_lock);
  98. rtsr = RTSR;
  99. /* clear interrupt sources */
  100. RTSR = 0;
  101. RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
  102. /* clear alarm interrupt if it has occurred */
  103. if (rtsr & RTSR_AL)
  104. rtsr &= ~RTSR_ALE;
  105. RTSR = rtsr & (RTSR_ALE | RTSR_HZE);
  106. /* update irq data & counter */
  107. if (rtsr & RTSR_AL)
  108. events |= RTC_AF | RTC_IRQF;
  109. if (rtsr & RTSR_HZ)
  110. events |= RTC_UF | RTC_IRQF;
  111. rtc_update_irq(rtc, 1, events);
  112. if (rtsr & RTSR_AL && rtc_periodic_alarm(&rtc_alarm))
  113. rtc_update_alarm(&rtc_alarm);
  114. spin_unlock(&sa1100_rtc_lock);
  115. return IRQ_HANDLED;
  116. }
  117. static int rtc_timer1_count;
  118. static irqreturn_t timer1_interrupt(int irq, void *dev_id)
  119. {
  120. struct platform_device *pdev = to_platform_device(dev_id);
  121. struct rtc_device *rtc = platform_get_drvdata(pdev);
  122. /*
  123. * If we match for the first time, rtc_timer1_count will be 1.
  124. * Otherwise, we wrapped around (very unlikely but
  125. * still possible) so compute the amount of missed periods.
  126. * The match reg is updated only when the data is actually retrieved
  127. * to avoid unnecessary interrupts.
  128. */
  129. OSSR = OSSR_M1; /* clear match on timer1 */
  130. rtc_update_irq(rtc, rtc_timer1_count, RTC_PF | RTC_IRQF);
  131. if (rtc_timer1_count == 1)
  132. rtc_timer1_count = (rtc_freq * ((1<<30)/(TIMER_FREQ>>2)));
  133. return IRQ_HANDLED;
  134. }
  135. static int sa1100_rtc_read_callback(struct device *dev, int data)
  136. {
  137. if (data & RTC_PF) {
  138. /* interpolate missed periods and set match for the next */
  139. unsigned long period = TIMER_FREQ/rtc_freq;
  140. unsigned long oscr = OSCR;
  141. unsigned long osmr1 = OSMR1;
  142. unsigned long missed = (oscr - osmr1)/period;
  143. data += missed << 8;
  144. OSSR = OSSR_M1; /* clear match on timer 1 */
  145. OSMR1 = osmr1 + (missed + 1)*period;
  146. /* Ensure we didn't miss another match in the mean time.
  147. * Here we compare (match - OSCR) 8 instead of 0 --
  148. * see comment in pxa_timer_interrupt() for explanation.
  149. */
  150. while( (signed long)((osmr1 = OSMR1) - OSCR) <= 8 ) {
  151. data += 0x100;
  152. OSSR = OSSR_M1; /* clear match on timer 1 */
  153. OSMR1 = osmr1 + period;
  154. }
  155. }
  156. return data;
  157. }
  158. static int sa1100_rtc_open(struct device *dev)
  159. {
  160. int ret;
  161. ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED,
  162. "rtc 1Hz", dev);
  163. if (ret) {
  164. dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz);
  165. goto fail_ui;
  166. }
  167. ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED,
  168. "rtc Alrm", dev);
  169. if (ret) {
  170. dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm);
  171. goto fail_ai;
  172. }
  173. ret = request_irq(IRQ_OST1, timer1_interrupt, IRQF_DISABLED,
  174. "rtc timer", dev);
  175. if (ret) {
  176. dev_err(dev, "IRQ %d already in use.\n", IRQ_OST1);
  177. goto fail_pi;
  178. }
  179. return 0;
  180. fail_pi:
  181. free_irq(IRQ_RTCAlrm, dev);
  182. fail_ai:
  183. free_irq(IRQ_RTC1Hz, dev);
  184. fail_ui:
  185. return ret;
  186. }
  187. static void sa1100_rtc_release(struct device *dev)
  188. {
  189. spin_lock_irq(&sa1100_rtc_lock);
  190. RTSR = 0;
  191. OIER &= ~OIER_E1;
  192. OSSR = OSSR_M1;
  193. spin_unlock_irq(&sa1100_rtc_lock);
  194. free_irq(IRQ_OST1, dev);
  195. free_irq(IRQ_RTCAlrm, dev);
  196. free_irq(IRQ_RTC1Hz, dev);
  197. }
  198. static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd,
  199. unsigned long arg)
  200. {
  201. switch(cmd) {
  202. case RTC_AIE_OFF:
  203. spin_lock_irq(&sa1100_rtc_lock);
  204. RTSR &= ~RTSR_ALE;
  205. spin_unlock_irq(&sa1100_rtc_lock);
  206. return 0;
  207. case RTC_AIE_ON:
  208. spin_lock_irq(&sa1100_rtc_lock);
  209. RTSR |= RTSR_ALE;
  210. spin_unlock_irq(&sa1100_rtc_lock);
  211. return 0;
  212. case RTC_UIE_OFF:
  213. spin_lock_irq(&sa1100_rtc_lock);
  214. RTSR &= ~RTSR_HZE;
  215. spin_unlock_irq(&sa1100_rtc_lock);
  216. return 0;
  217. case RTC_UIE_ON:
  218. spin_lock_irq(&sa1100_rtc_lock);
  219. RTSR |= RTSR_HZE;
  220. spin_unlock_irq(&sa1100_rtc_lock);
  221. return 0;
  222. case RTC_PIE_OFF:
  223. spin_lock_irq(&sa1100_rtc_lock);
  224. OIER &= ~OIER_E1;
  225. spin_unlock_irq(&sa1100_rtc_lock);
  226. return 0;
  227. case RTC_PIE_ON:
  228. spin_lock_irq(&sa1100_rtc_lock);
  229. OSMR1 = TIMER_FREQ/rtc_freq + OSCR;
  230. OIER |= OIER_E1;
  231. rtc_timer1_count = 1;
  232. spin_unlock_irq(&sa1100_rtc_lock);
  233. return 0;
  234. case RTC_IRQP_READ:
  235. return put_user(rtc_freq, (unsigned long *)arg);
  236. case RTC_IRQP_SET:
  237. if (arg < 1 || arg > TIMER_FREQ)
  238. return -EINVAL;
  239. rtc_freq = arg;
  240. return 0;
  241. }
  242. return -ENOIOCTLCMD;
  243. }
  244. static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
  245. {
  246. rtc_time_to_tm(RCNR, tm);
  247. return 0;
  248. }
  249. static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
  250. {
  251. unsigned long time;
  252. int ret;
  253. ret = rtc_tm_to_time(tm, &time);
  254. if (ret == 0)
  255. RCNR = time;
  256. return ret;
  257. }
  258. static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  259. {
  260. u32 rtsr;
  261. memcpy(&alrm->time, &rtc_alarm, sizeof(struct rtc_time));
  262. rtsr = RTSR;
  263. alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
  264. alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
  265. return 0;
  266. }
  267. static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  268. {
  269. int ret;
  270. spin_lock_irq(&sa1100_rtc_lock);
  271. ret = rtc_update_alarm(&alrm->time);
  272. if (ret == 0) {
  273. if (alrm->enabled)
  274. RTSR |= RTSR_ALE;
  275. else
  276. RTSR &= ~RTSR_ALE;
  277. }
  278. spin_unlock_irq(&sa1100_rtc_lock);
  279. return ret;
  280. }
  281. static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
  282. {
  283. seq_printf(seq, "trim/divider\t: 0x%08x\n", (u32) RTTR);
  284. seq_printf(seq, "update_IRQ\t: %s\n",
  285. (RTSR & RTSR_HZE) ? "yes" : "no");
  286. seq_printf(seq, "periodic_IRQ\t: %s\n",
  287. (OIER & OIER_E1) ? "yes" : "no");
  288. seq_printf(seq, "periodic_freq\t: %ld\n", rtc_freq);
  289. return 0;
  290. }
  291. static const struct rtc_class_ops sa1100_rtc_ops = {
  292. .open = sa1100_rtc_open,
  293. .read_callback = sa1100_rtc_read_callback,
  294. .release = sa1100_rtc_release,
  295. .ioctl = sa1100_rtc_ioctl,
  296. .read_time = sa1100_rtc_read_time,
  297. .set_time = sa1100_rtc_set_time,
  298. .read_alarm = sa1100_rtc_read_alarm,
  299. .set_alarm = sa1100_rtc_set_alarm,
  300. .proc = sa1100_rtc_proc,
  301. };
  302. static int sa1100_rtc_probe(struct platform_device *pdev)
  303. {
  304. struct rtc_device *rtc;
  305. /*
  306. * According to the manual we should be able to let RTTR be zero
  307. * and then a default diviser for a 32.768KHz clock is used.
  308. * Apparently this doesn't work, at least for my SA1110 rev 5.
  309. * If the clock divider is uninitialized then reset it to the
  310. * default value to get the 1Hz clock.
  311. */
  312. if (RTTR == 0) {
  313. RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
  314. dev_warn(&pdev->dev, "warning: initializing default clock divider/trim value\n");
  315. /* The current RTC value probably doesn't make sense either */
  316. RCNR = 0;
  317. }
  318. device_init_wakeup(&pdev->dev, 1);
  319. rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
  320. THIS_MODULE);
  321. if (IS_ERR(rtc))
  322. return PTR_ERR(rtc);
  323. platform_set_drvdata(pdev, rtc);
  324. return 0;
  325. }
  326. static int sa1100_rtc_remove(struct platform_device *pdev)
  327. {
  328. struct rtc_device *rtc = platform_get_drvdata(pdev);
  329. if (rtc)
  330. rtc_device_unregister(rtc);
  331. return 0;
  332. }
  333. #ifdef CONFIG_PM
  334. static int sa1100_rtc_suspend(struct platform_device *pdev, pm_message_t state)
  335. {
  336. if (device_may_wakeup(&pdev->dev))
  337. enable_irq_wake(IRQ_RTCAlrm);
  338. return 0;
  339. }
  340. static int sa1100_rtc_resume(struct platform_device *pdev)
  341. {
  342. if (device_may_wakeup(&pdev->dev))
  343. disable_irq_wake(IRQ_RTCAlrm);
  344. return 0;
  345. }
  346. #else
  347. #define sa1100_rtc_suspend NULL
  348. #define sa1100_rtc_resume NULL
  349. #endif
  350. static struct platform_driver sa1100_rtc_driver = {
  351. .probe = sa1100_rtc_probe,
  352. .remove = sa1100_rtc_remove,
  353. .suspend = sa1100_rtc_suspend,
  354. .resume = sa1100_rtc_resume,
  355. .driver = {
  356. .name = "sa1100-rtc",
  357. },
  358. };
  359. static int __init sa1100_rtc_init(void)
  360. {
  361. return platform_driver_register(&sa1100_rtc_driver);
  362. }
  363. static void __exit sa1100_rtc_exit(void)
  364. {
  365. platform_driver_unregister(&sa1100_rtc_driver);
  366. }
  367. module_init(sa1100_rtc_init);
  368. module_exit(sa1100_rtc_exit);
  369. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  370. MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
  371. MODULE_LICENSE("GPL");
  372. MODULE_ALIAS("platform:sa1100-rtc");