rtc-sa1100.c 9.0 KB

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