rtc-at91sam9.c 11 KB

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