rtc-at91sam9.c 13 KB

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