rtc-at91sam9.c 12 KB

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