rtc-at91sam9.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * "RTT as Real Time Clock" driver for AT91SAM9 SoC family
  3. *
  4. * (C) 2007 Michel Benoit
  5. *
  6. * Based on rtc-at91rm9200.c by Rick Bronson
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/time.h>
  17. #include <linux/rtc.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/ioctl.h>
  20. #include <linux/slab.h>
  21. #include <mach/board.h>
  22. #include <mach/at91_rtt.h>
  23. #include <mach/cpu.h>
  24. /*
  25. * This driver uses two configurable hardware resources that live in the
  26. * AT91SAM9 backup power domain (intended to be powered at all times)
  27. * to implement the Real Time Clock interfaces
  28. *
  29. * - A "Real-time Timer" (RTT) counts up in seconds from a base time.
  30. * We can't assign the counter value (CRTV) ... but we can reset it.
  31. *
  32. * - One of the "General Purpose Backup Registers" (GPBRs) holds the
  33. * base time, normally an offset from the beginning of the POSIX
  34. * epoch (1970-Jan-1 00:00:00 UTC). Some systems also include the
  35. * local timezone's offset.
  36. *
  37. * The RTC's value is the RTT counter plus that offset. The RTC's alarm
  38. * is likewise a base (ALMV) plus that offset.
  39. *
  40. * Not all RTTs will be used as RTCs; some systems have multiple RTTs to
  41. * choose from, or a "real" RTC module. All systems have multiple GPBR
  42. * registers available, likewise usable for more than "RTC" support.
  43. */
  44. /*
  45. * We store ALARM_DISABLED in ALMV to record that no alarm is set.
  46. * It's also the reset value for that field.
  47. */
  48. #define ALARM_DISABLED ((u32)~0)
  49. struct sam9_rtc {
  50. void __iomem *rtt;
  51. struct rtc_device *rtcdev;
  52. u32 imr;
  53. void __iomem *gpbr;
  54. };
  55. #define rtt_readl(rtc, field) \
  56. __raw_readl((rtc)->rtt + AT91_RTT_ ## field)
  57. #define rtt_writel(rtc, field, val) \
  58. __raw_writel((val), (rtc)->rtt + AT91_RTT_ ## field)
  59. #define gpbr_readl(rtc) \
  60. __raw_readl((rtc)->gpbr)
  61. #define gpbr_writel(rtc, val) \
  62. __raw_writel((val), (rtc)->gpbr)
  63. /*
  64. * Read current time and date in RTC
  65. */
  66. static int at91_rtc_readtime(struct device *dev, struct rtc_time *tm)
  67. {
  68. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  69. u32 secs, secs2;
  70. u32 offset;
  71. /* read current time offset */
  72. offset = gpbr_readl(rtc);
  73. if (offset == 0)
  74. return -EILSEQ;
  75. /* reread the counter to help sync the two clock domains */
  76. secs = rtt_readl(rtc, VR);
  77. secs2 = rtt_readl(rtc, VR);
  78. if (secs != secs2)
  79. secs = rtt_readl(rtc, VR);
  80. rtc_time_to_tm(offset + secs, tm);
  81. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "readtime",
  82. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  83. tm->tm_hour, tm->tm_min, tm->tm_sec);
  84. return 0;
  85. }
  86. /*
  87. * Set current time and date in RTC
  88. */
  89. static int at91_rtc_settime(struct device *dev, struct rtc_time *tm)
  90. {
  91. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  92. int err;
  93. u32 offset, alarm, mr;
  94. unsigned long secs;
  95. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "settime",
  96. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  97. tm->tm_hour, tm->tm_min, tm->tm_sec);
  98. err = rtc_tm_to_time(tm, &secs);
  99. if (err != 0)
  100. return err;
  101. mr = rtt_readl(rtc, MR);
  102. /* disable interrupts */
  103. rtt_writel(rtc, MR, mr & ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN));
  104. /* read current time offset */
  105. offset = gpbr_readl(rtc);
  106. /* store the new base time in a battery backup register */
  107. secs += 1;
  108. gpbr_writel(rtc, secs);
  109. /* adjust the alarm time for the new base */
  110. alarm = rtt_readl(rtc, AR);
  111. if (alarm != ALARM_DISABLED) {
  112. if (offset > secs) {
  113. /* time jumped backwards, increase time until alarm */
  114. alarm += (offset - secs);
  115. } else if ((alarm + offset) > secs) {
  116. /* time jumped forwards, decrease time until alarm */
  117. alarm -= (secs - offset);
  118. } else {
  119. /* time jumped past the alarm, disable alarm */
  120. alarm = ALARM_DISABLED;
  121. mr &= ~AT91_RTT_ALMIEN;
  122. }
  123. rtt_writel(rtc, AR, alarm);
  124. }
  125. /* reset the timer, and re-enable interrupts */
  126. rtt_writel(rtc, MR, mr | AT91_RTT_RTTRST);
  127. return 0;
  128. }
  129. static int at91_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
  130. {
  131. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  132. struct rtc_time *tm = &alrm->time;
  133. u32 alarm = rtt_readl(rtc, AR);
  134. u32 offset;
  135. offset = gpbr_readl(rtc);
  136. if (offset == 0)
  137. return -EILSEQ;
  138. memset(alrm, 0, sizeof(*alrm));
  139. if (alarm != ALARM_DISABLED && offset != 0) {
  140. rtc_time_to_tm(offset + alarm, tm);
  141. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "readalarm",
  142. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  143. tm->tm_hour, tm->tm_min, tm->tm_sec);
  144. if (rtt_readl(rtc, MR) & AT91_RTT_ALMIEN)
  145. alrm->enabled = 1;
  146. }
  147. return 0;
  148. }
  149. static int at91_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  150. {
  151. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  152. struct rtc_time *tm = &alrm->time;
  153. unsigned long secs;
  154. u32 offset;
  155. u32 mr;
  156. int err;
  157. err = rtc_tm_to_time(tm, &secs);
  158. if (err != 0)
  159. return err;
  160. offset = gpbr_readl(rtc);
  161. if (offset == 0) {
  162. /* time is not set */
  163. return -EILSEQ;
  164. }
  165. mr = rtt_readl(rtc, MR);
  166. rtt_writel(rtc, MR, mr & ~AT91_RTT_ALMIEN);
  167. /* alarm in the past? finish and leave disabled */
  168. if (secs <= offset) {
  169. rtt_writel(rtc, AR, ALARM_DISABLED);
  170. return 0;
  171. }
  172. /* else set alarm and maybe enable it */
  173. rtt_writel(rtc, AR, secs - offset);
  174. if (alrm->enabled)
  175. rtt_writel(rtc, MR, mr | AT91_RTT_ALMIEN);
  176. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "setalarm",
  177. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour,
  178. tm->tm_min, tm->tm_sec);
  179. return 0;
  180. }
  181. static int at91_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  182. {
  183. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  184. u32 mr = rtt_readl(rtc, MR);
  185. dev_dbg(dev, "alarm_irq_enable: enabled=%08x, mr %08x\n", enabled, mr);
  186. if (enabled)
  187. rtt_writel(rtc, MR, mr | AT91_RTT_ALMIEN);
  188. else
  189. rtt_writel(rtc, MR, mr & ~AT91_RTT_ALMIEN);
  190. return 0;
  191. }
  192. /*
  193. * Provide additional RTC information in /proc/driver/rtc
  194. */
  195. static int at91_rtc_proc(struct device *dev, struct seq_file *seq)
  196. {
  197. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  198. u32 mr = mr = rtt_readl(rtc, MR);
  199. seq_printf(seq, "update_IRQ\t: %s\n",
  200. (mr & AT91_RTT_RTTINCIEN) ? "yes" : "no");
  201. return 0;
  202. }
  203. /*
  204. * IRQ handler for the RTC
  205. */
  206. static irqreturn_t at91_rtc_interrupt(int irq, void *_rtc)
  207. {
  208. struct sam9_rtc *rtc = _rtc;
  209. u32 sr, mr;
  210. unsigned long events = 0;
  211. /* Shared interrupt may be for another device. Note: reading
  212. * SR clears it, so we must only read it in this irq handler!
  213. */
  214. mr = rtt_readl(rtc, MR) & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  215. sr = rtt_readl(rtc, SR) & (mr >> 16);
  216. if (!sr)
  217. return IRQ_NONE;
  218. /* alarm status */
  219. if (sr & AT91_RTT_ALMS)
  220. events |= (RTC_AF | RTC_IRQF);
  221. /* timer update/increment */
  222. if (sr & AT91_RTT_RTTINC)
  223. events |= (RTC_UF | RTC_IRQF);
  224. rtc_update_irq(rtc->rtcdev, 1, events);
  225. pr_debug("%s: num=%ld, events=0x%02lx\n", __func__,
  226. events >> 8, events & 0x000000FF);
  227. return IRQ_HANDLED;
  228. }
  229. static const struct rtc_class_ops at91_rtc_ops = {
  230. .read_time = at91_rtc_readtime,
  231. .set_time = at91_rtc_settime,
  232. .read_alarm = at91_rtc_readalarm,
  233. .set_alarm = at91_rtc_setalarm,
  234. .proc = at91_rtc_proc,
  235. .alarm_irq_enable = at91_rtc_alarm_irq_enable,
  236. };
  237. /*
  238. * Initialize and install RTC driver
  239. */
  240. static int __devinit at91_rtc_probe(struct platform_device *pdev)
  241. {
  242. struct resource *r, *r_gpbr;
  243. struct sam9_rtc *rtc;
  244. int ret;
  245. u32 mr;
  246. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  247. r_gpbr = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  248. if (!r || !r_gpbr) {
  249. dev_err(&pdev->dev, "need 2 ressources\n");
  250. return -ENODEV;
  251. }
  252. rtc = kzalloc(sizeof *rtc, GFP_KERNEL);
  253. if (!rtc)
  254. return -ENOMEM;
  255. /* platform setup code should have handled this; sigh */
  256. if (!device_can_wakeup(&pdev->dev))
  257. device_init_wakeup(&pdev->dev, 1);
  258. platform_set_drvdata(pdev, rtc);
  259. rtc->rtt = ioremap(r->start, resource_size(r));
  260. if (!rtc->rtt) {
  261. dev_err(&pdev->dev, "failed to map registers, aborting.\n");
  262. ret = -ENOMEM;
  263. goto fail;
  264. }
  265. rtc->gpbr = ioremap(r_gpbr->start, resource_size(r_gpbr));
  266. if (!rtc->gpbr) {
  267. dev_err(&pdev->dev, "failed to map gpbr registers, aborting.\n");
  268. ret = -ENOMEM;
  269. goto fail_gpbr;
  270. }
  271. mr = rtt_readl(rtc, MR);
  272. /* unless RTT is counting at 1 Hz, re-initialize it */
  273. if ((mr & AT91_RTT_RTPRES) != AT91_SLOW_CLOCK) {
  274. mr = AT91_RTT_RTTRST | (AT91_SLOW_CLOCK & AT91_RTT_RTPRES);
  275. gpbr_writel(rtc, 0);
  276. }
  277. /* disable all interrupts (same as on shutdown path) */
  278. mr &= ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  279. rtt_writel(rtc, MR, mr);
  280. rtc->rtcdev = rtc_device_register(pdev->name, &pdev->dev,
  281. &at91_rtc_ops, THIS_MODULE);
  282. if (IS_ERR(rtc->rtcdev)) {
  283. ret = PTR_ERR(rtc->rtcdev);
  284. goto fail_register;
  285. }
  286. /* register irq handler after we know what name we'll use */
  287. ret = request_irq(AT91_ID_SYS, at91_rtc_interrupt,
  288. IRQF_SHARED,
  289. dev_name(&rtc->rtcdev->dev), rtc);
  290. if (ret) {
  291. dev_dbg(&pdev->dev, "can't share IRQ %d?\n", AT91_ID_SYS);
  292. rtc_device_unregister(rtc->rtcdev);
  293. goto fail_register;
  294. }
  295. /* NOTE: sam9260 rev A silicon has a ROM bug which resets the
  296. * RTT on at least some reboots. If you have that chip, you must
  297. * initialize the time from some external source like a GPS, wall
  298. * clock, discrete RTC, etc
  299. */
  300. if (gpbr_readl(rtc) == 0)
  301. dev_warn(&pdev->dev, "%s: SET TIME!\n",
  302. dev_name(&rtc->rtcdev->dev));
  303. return 0;
  304. fail_register:
  305. iounmap(rtc->gpbr);
  306. fail_gpbr:
  307. iounmap(rtc->rtt);
  308. fail:
  309. platform_set_drvdata(pdev, NULL);
  310. kfree(rtc);
  311. return ret;
  312. }
  313. /*
  314. * Disable and remove the RTC driver
  315. */
  316. static int __devexit at91_rtc_remove(struct platform_device *pdev)
  317. {
  318. struct sam9_rtc *rtc = platform_get_drvdata(pdev);
  319. u32 mr = rtt_readl(rtc, MR);
  320. /* disable all interrupts */
  321. rtt_writel(rtc, MR, mr & ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN));
  322. free_irq(AT91_ID_SYS, rtc);
  323. rtc_device_unregister(rtc->rtcdev);
  324. iounmap(rtc->gpbr);
  325. iounmap(rtc->rtt);
  326. platform_set_drvdata(pdev, NULL);
  327. kfree(rtc);
  328. return 0;
  329. }
  330. static void at91_rtc_shutdown(struct platform_device *pdev)
  331. {
  332. struct sam9_rtc *rtc = platform_get_drvdata(pdev);
  333. u32 mr = rtt_readl(rtc, MR);
  334. rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  335. rtt_writel(rtc, MR, mr & ~rtc->imr);
  336. }
  337. #ifdef CONFIG_PM
  338. /* AT91SAM9 RTC Power management control */
  339. static int at91_rtc_suspend(struct platform_device *pdev,
  340. pm_message_t state)
  341. {
  342. struct sam9_rtc *rtc = platform_get_drvdata(pdev);
  343. u32 mr = rtt_readl(rtc, MR);
  344. /*
  345. * This IRQ is shared with DBGU and other hardware which isn't
  346. * necessarily a wakeup event source.
  347. */
  348. rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  349. if (rtc->imr) {
  350. if (device_may_wakeup(&pdev->dev) && (mr & AT91_RTT_ALMIEN)) {
  351. enable_irq_wake(AT91_ID_SYS);
  352. /* don't let RTTINC cause wakeups */
  353. if (mr & AT91_RTT_RTTINCIEN)
  354. rtt_writel(rtc, MR, mr & ~AT91_RTT_RTTINCIEN);
  355. } else
  356. rtt_writel(rtc, MR, mr & ~rtc->imr);
  357. }
  358. return 0;
  359. }
  360. static int at91_rtc_resume(struct platform_device *pdev)
  361. {
  362. struct sam9_rtc *rtc = platform_get_drvdata(pdev);
  363. u32 mr;
  364. if (rtc->imr) {
  365. if (device_may_wakeup(&pdev->dev))
  366. disable_irq_wake(AT91_ID_SYS);
  367. mr = rtt_readl(rtc, MR);
  368. rtt_writel(rtc, MR, mr | rtc->imr);
  369. }
  370. return 0;
  371. }
  372. #else
  373. #define at91_rtc_suspend NULL
  374. #define at91_rtc_resume NULL
  375. #endif
  376. static struct platform_driver at91_rtc_driver = {
  377. .probe = at91_rtc_probe,
  378. .remove = __devexit_p(at91_rtc_remove),
  379. .shutdown = at91_rtc_shutdown,
  380. .suspend = at91_rtc_suspend,
  381. .resume = at91_rtc_resume,
  382. .driver = {
  383. .name = "rtc-at91sam9",
  384. .owner = THIS_MODULE,
  385. },
  386. };
  387. static int __init at91_rtc_init(void)
  388. {
  389. return platform_driver_register(&at91_rtc_driver);
  390. }
  391. module_init(at91_rtc_init);
  392. static void __exit at91_rtc_exit(void)
  393. {
  394. platform_driver_unregister(&at91_rtc_driver);
  395. }
  396. module_exit(at91_rtc_exit);
  397. MODULE_AUTHOR("Michel Benoit");
  398. MODULE_DESCRIPTION("RTC driver for Atmel AT91SAM9x");
  399. MODULE_LICENSE("GPL");