rtc-at91sam9.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 <linux/mfd/syscon.h>
  24. #include <linux/regmap.h>
  25. #include <linux/clk.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. #define AT91_RTT_MR 0x00 /* Real-time Mode Register */
  47. #define AT91_RTT_RTPRES (0xffff << 0) /* Real-time Timer Prescaler Value */
  48. #define AT91_RTT_ALMIEN (1 << 16) /* Alarm Interrupt Enable */
  49. #define AT91_RTT_RTTINCIEN (1 << 17) /* Real Time Timer Increment Interrupt Enable */
  50. #define AT91_RTT_RTTRST (1 << 18) /* Real Time Timer Restart */
  51. #define AT91_RTT_AR 0x04 /* Real-time Alarm Register */
  52. #define AT91_RTT_ALMV (0xffffffff) /* Alarm Value */
  53. #define AT91_RTT_VR 0x08 /* Real-time Value Register */
  54. #define AT91_RTT_CRTV (0xffffffff) /* Current Real-time Value */
  55. #define AT91_RTT_SR 0x0c /* Real-time Status Register */
  56. #define AT91_RTT_ALMS (1 << 0) /* Real-time Alarm Status */
  57. #define AT91_RTT_RTTINC (1 << 1) /* Real-time Timer Increment */
  58. /*
  59. * We store ALARM_DISABLED in ALMV to record that no alarm is set.
  60. * It's also the reset value for that field.
  61. */
  62. #define ALARM_DISABLED ((u32)~0)
  63. struct sam9_rtc {
  64. void __iomem *rtt;
  65. struct rtc_device *rtcdev;
  66. u32 imr;
  67. struct regmap *gpbr;
  68. unsigned int gpbr_offset;
  69. int irq;
  70. struct clk *sclk;
  71. };
  72. #define rtt_readl(rtc, field) \
  73. readl((rtc)->rtt + AT91_RTT_ ## field)
  74. #define rtt_writel(rtc, field, val) \
  75. writel((val), (rtc)->rtt + AT91_RTT_ ## field)
  76. static inline unsigned int gpbr_readl(struct sam9_rtc *rtc)
  77. {
  78. unsigned int val;
  79. regmap_read(rtc->gpbr, rtc->gpbr_offset, &val);
  80. return val;
  81. }
  82. static inline void gpbr_writel(struct sam9_rtc *rtc, unsigned int val)
  83. {
  84. regmap_write(rtc->gpbr, rtc->gpbr_offset, val);
  85. }
  86. /*
  87. * Read current time and date in RTC
  88. */
  89. static int at91_rtc_readtime(struct device *dev, struct rtc_time *tm)
  90. {
  91. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  92. u32 secs, secs2;
  93. u32 offset;
  94. /* read current time offset */
  95. offset = gpbr_readl(rtc);
  96. if (offset == 0)
  97. return -EILSEQ;
  98. /* reread the counter to help sync the two clock domains */
  99. secs = rtt_readl(rtc, VR);
  100. secs2 = rtt_readl(rtc, VR);
  101. if (secs != secs2)
  102. secs = rtt_readl(rtc, VR);
  103. rtc_time_to_tm(offset + secs, tm);
  104. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "readtime",
  105. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  106. tm->tm_hour, tm->tm_min, tm->tm_sec);
  107. return 0;
  108. }
  109. /*
  110. * Set current time and date in RTC
  111. */
  112. static int at91_rtc_settime(struct device *dev, struct rtc_time *tm)
  113. {
  114. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  115. int err;
  116. u32 offset, alarm, mr;
  117. unsigned long secs;
  118. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "settime",
  119. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  120. tm->tm_hour, tm->tm_min, tm->tm_sec);
  121. err = rtc_tm_to_time(tm, &secs);
  122. if (err != 0)
  123. return err;
  124. mr = rtt_readl(rtc, MR);
  125. /* disable interrupts */
  126. rtt_writel(rtc, MR, mr & ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN));
  127. /* read current time offset */
  128. offset = gpbr_readl(rtc);
  129. /* store the new base time in a battery backup register */
  130. secs += 1;
  131. gpbr_writel(rtc, secs);
  132. /* adjust the alarm time for the new base */
  133. alarm = rtt_readl(rtc, AR);
  134. if (alarm != ALARM_DISABLED) {
  135. if (offset > secs) {
  136. /* time jumped backwards, increase time until alarm */
  137. alarm += (offset - secs);
  138. } else if ((alarm + offset) > secs) {
  139. /* time jumped forwards, decrease time until alarm */
  140. alarm -= (secs - offset);
  141. } else {
  142. /* time jumped past the alarm, disable alarm */
  143. alarm = ALARM_DISABLED;
  144. mr &= ~AT91_RTT_ALMIEN;
  145. }
  146. rtt_writel(rtc, AR, alarm);
  147. }
  148. /* reset the timer, and re-enable interrupts */
  149. rtt_writel(rtc, MR, mr | AT91_RTT_RTTRST);
  150. return 0;
  151. }
  152. static int at91_rtc_readalarm(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. u32 alarm = rtt_readl(rtc, AR);
  157. u32 offset;
  158. offset = gpbr_readl(rtc);
  159. if (offset == 0)
  160. return -EILSEQ;
  161. memset(alrm, 0, sizeof(*alrm));
  162. if (alarm != ALARM_DISABLED && offset != 0) {
  163. rtc_time_to_tm(offset + alarm, tm);
  164. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "readalarm",
  165. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  166. tm->tm_hour, tm->tm_min, tm->tm_sec);
  167. if (rtt_readl(rtc, MR) & AT91_RTT_ALMIEN)
  168. alrm->enabled = 1;
  169. }
  170. return 0;
  171. }
  172. static int at91_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  173. {
  174. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  175. struct rtc_time *tm = &alrm->time;
  176. unsigned long secs;
  177. u32 offset;
  178. u32 mr;
  179. int err;
  180. err = rtc_tm_to_time(tm, &secs);
  181. if (err != 0)
  182. return err;
  183. offset = gpbr_readl(rtc);
  184. if (offset == 0) {
  185. /* time is not set */
  186. return -EILSEQ;
  187. }
  188. mr = rtt_readl(rtc, MR);
  189. rtt_writel(rtc, MR, mr & ~AT91_RTT_ALMIEN);
  190. /* alarm in the past? finish and leave disabled */
  191. if (secs <= offset) {
  192. rtt_writel(rtc, AR, ALARM_DISABLED);
  193. return 0;
  194. }
  195. /* else set alarm and maybe enable it */
  196. rtt_writel(rtc, AR, secs - offset);
  197. if (alrm->enabled)
  198. rtt_writel(rtc, MR, mr | AT91_RTT_ALMIEN);
  199. dev_dbg(dev, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "setalarm",
  200. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour,
  201. tm->tm_min, tm->tm_sec);
  202. return 0;
  203. }
  204. static int at91_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  205. {
  206. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  207. u32 mr = rtt_readl(rtc, MR);
  208. dev_dbg(dev, "alarm_irq_enable: enabled=%08x, mr %08x\n", enabled, mr);
  209. if (enabled)
  210. rtt_writel(rtc, MR, mr | AT91_RTT_ALMIEN);
  211. else
  212. rtt_writel(rtc, MR, mr & ~AT91_RTT_ALMIEN);
  213. return 0;
  214. }
  215. /*
  216. * Provide additional RTC information in /proc/driver/rtc
  217. */
  218. static int at91_rtc_proc(struct device *dev, struct seq_file *seq)
  219. {
  220. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  221. u32 mr = mr = rtt_readl(rtc, MR);
  222. seq_printf(seq, "update_IRQ\t: %s\n",
  223. (mr & AT91_RTT_RTTINCIEN) ? "yes" : "no");
  224. return 0;
  225. }
  226. /*
  227. * IRQ handler for the RTC
  228. */
  229. static irqreturn_t at91_rtc_interrupt(int irq, void *_rtc)
  230. {
  231. struct sam9_rtc *rtc = _rtc;
  232. u32 sr, mr;
  233. unsigned long events = 0;
  234. /* Shared interrupt may be for another device. Note: reading
  235. * SR clears it, so we must only read it in this irq handler!
  236. */
  237. mr = rtt_readl(rtc, MR) & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  238. sr = rtt_readl(rtc, SR) & (mr >> 16);
  239. if (!sr)
  240. return IRQ_NONE;
  241. /* alarm status */
  242. if (sr & AT91_RTT_ALMS)
  243. events |= (RTC_AF | RTC_IRQF);
  244. /* timer update/increment */
  245. if (sr & AT91_RTT_RTTINC)
  246. events |= (RTC_UF | RTC_IRQF);
  247. rtc_update_irq(rtc->rtcdev, 1, events);
  248. pr_debug("%s: num=%ld, events=0x%02lx\n", __func__,
  249. events >> 8, events & 0x000000FF);
  250. return IRQ_HANDLED;
  251. }
  252. static const struct rtc_class_ops at91_rtc_ops = {
  253. .read_time = at91_rtc_readtime,
  254. .set_time = at91_rtc_settime,
  255. .read_alarm = at91_rtc_readalarm,
  256. .set_alarm = at91_rtc_setalarm,
  257. .proc = at91_rtc_proc,
  258. .alarm_irq_enable = at91_rtc_alarm_irq_enable,
  259. };
  260. static struct regmap_config gpbr_regmap_config = {
  261. .reg_bits = 32,
  262. .val_bits = 32,
  263. .reg_stride = 4,
  264. };
  265. /*
  266. * Initialize and install RTC driver
  267. */
  268. static int at91_rtc_probe(struct platform_device *pdev)
  269. {
  270. struct resource *r;
  271. struct sam9_rtc *rtc;
  272. int ret, irq;
  273. u32 mr;
  274. unsigned int sclk_rate;
  275. irq = platform_get_irq(pdev, 0);
  276. if (irq < 0) {
  277. dev_err(&pdev->dev, "failed to get interrupt resource\n");
  278. return irq;
  279. }
  280. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  281. if (!rtc)
  282. return -ENOMEM;
  283. rtc->irq = irq;
  284. /* platform setup code should have handled this; sigh */
  285. if (!device_can_wakeup(&pdev->dev))
  286. device_init_wakeup(&pdev->dev, 1);
  287. platform_set_drvdata(pdev, rtc);
  288. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  289. rtc->rtt = devm_ioremap_resource(&pdev->dev, r);
  290. if (IS_ERR(rtc->rtt))
  291. return PTR_ERR(rtc->rtt);
  292. if (!pdev->dev.of_node) {
  293. /*
  294. * TODO: Remove this code chunk when removing non DT board
  295. * support. Remember to remove the gpbr_regmap_config
  296. * variable too.
  297. */
  298. void __iomem *gpbr;
  299. r = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  300. gpbr = devm_ioremap_resource(&pdev->dev, r);
  301. if (IS_ERR(gpbr))
  302. return PTR_ERR(gpbr);
  303. rtc->gpbr = regmap_init_mmio(NULL, gpbr,
  304. &gpbr_regmap_config);
  305. } else {
  306. struct of_phandle_args args;
  307. ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node,
  308. "atmel,rtt-rtc-time-reg", 1, 0,
  309. &args);
  310. if (ret)
  311. return ret;
  312. rtc->gpbr = syscon_node_to_regmap(args.np);
  313. rtc->gpbr_offset = args.args[0];
  314. }
  315. if (IS_ERR(rtc->gpbr)) {
  316. dev_err(&pdev->dev, "failed to retrieve gpbr regmap, aborting.\n");
  317. return -ENOMEM;
  318. }
  319. rtc->sclk = devm_clk_get(&pdev->dev, NULL);
  320. if (IS_ERR(rtc->sclk))
  321. return PTR_ERR(rtc->sclk);
  322. sclk_rate = clk_get_rate(rtc->sclk);
  323. if (!sclk_rate || sclk_rate > AT91_RTT_RTPRES) {
  324. dev_err(&pdev->dev, "Invalid slow clock rate\n");
  325. return -EINVAL;
  326. }
  327. ret = clk_prepare_enable(rtc->sclk);
  328. if (ret) {
  329. dev_err(&pdev->dev, "Could not enable slow clock\n");
  330. return ret;
  331. }
  332. mr = rtt_readl(rtc, MR);
  333. /* unless RTT is counting at 1 Hz, re-initialize it */
  334. if ((mr & AT91_RTT_RTPRES) != sclk_rate) {
  335. mr = AT91_RTT_RTTRST | (sclk_rate & AT91_RTT_RTPRES);
  336. gpbr_writel(rtc, 0);
  337. }
  338. /* disable all interrupts (same as on shutdown path) */
  339. mr &= ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  340. rtt_writel(rtc, MR, mr);
  341. rtc->rtcdev = devm_rtc_device_register(&pdev->dev, pdev->name,
  342. &at91_rtc_ops, THIS_MODULE);
  343. if (IS_ERR(rtc->rtcdev))
  344. return PTR_ERR(rtc->rtcdev);
  345. /* register irq handler after we know what name we'll use */
  346. ret = devm_request_irq(&pdev->dev, rtc->irq, at91_rtc_interrupt,
  347. IRQF_SHARED, dev_name(&rtc->rtcdev->dev), rtc);
  348. if (ret) {
  349. dev_dbg(&pdev->dev, "can't share IRQ %d?\n", rtc->irq);
  350. return ret;
  351. }
  352. /* NOTE: sam9260 rev A silicon has a ROM bug which resets the
  353. * RTT on at least some reboots. If you have that chip, you must
  354. * initialize the time from some external source like a GPS, wall
  355. * clock, discrete RTC, etc
  356. */
  357. if (gpbr_readl(rtc) == 0)
  358. dev_warn(&pdev->dev, "%s: SET TIME!\n",
  359. dev_name(&rtc->rtcdev->dev));
  360. return 0;
  361. }
  362. /*
  363. * Disable and remove the RTC driver
  364. */
  365. static int at91_rtc_remove(struct platform_device *pdev)
  366. {
  367. struct sam9_rtc *rtc = platform_get_drvdata(pdev);
  368. u32 mr = rtt_readl(rtc, MR);
  369. /* disable all interrupts */
  370. rtt_writel(rtc, MR, mr & ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN));
  371. if (!IS_ERR(rtc->sclk))
  372. clk_disable_unprepare(rtc->sclk);
  373. return 0;
  374. }
  375. static void at91_rtc_shutdown(struct platform_device *pdev)
  376. {
  377. struct sam9_rtc *rtc = platform_get_drvdata(pdev);
  378. u32 mr = rtt_readl(rtc, MR);
  379. rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  380. rtt_writel(rtc, MR, mr & ~rtc->imr);
  381. }
  382. #ifdef CONFIG_PM_SLEEP
  383. /* AT91SAM9 RTC Power management control */
  384. static int at91_rtc_suspend(struct device *dev)
  385. {
  386. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  387. u32 mr = rtt_readl(rtc, MR);
  388. /*
  389. * This IRQ is shared with DBGU and other hardware which isn't
  390. * necessarily a wakeup event source.
  391. */
  392. rtc->imr = mr & (AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN);
  393. if (rtc->imr) {
  394. if (device_may_wakeup(dev) && (mr & AT91_RTT_ALMIEN)) {
  395. enable_irq_wake(rtc->irq);
  396. /* don't let RTTINC cause wakeups */
  397. if (mr & AT91_RTT_RTTINCIEN)
  398. rtt_writel(rtc, MR, mr & ~AT91_RTT_RTTINCIEN);
  399. } else
  400. rtt_writel(rtc, MR, mr & ~rtc->imr);
  401. }
  402. return 0;
  403. }
  404. static int at91_rtc_resume(struct device *dev)
  405. {
  406. struct sam9_rtc *rtc = dev_get_drvdata(dev);
  407. u32 mr;
  408. if (rtc->imr) {
  409. if (device_may_wakeup(dev))
  410. disable_irq_wake(rtc->irq);
  411. mr = rtt_readl(rtc, MR);
  412. rtt_writel(rtc, MR, mr | rtc->imr);
  413. }
  414. return 0;
  415. }
  416. #endif
  417. static SIMPLE_DEV_PM_OPS(at91_rtc_pm_ops, at91_rtc_suspend, at91_rtc_resume);
  418. #ifdef CONFIG_OF
  419. static const struct of_device_id at91_rtc_dt_ids[] = {
  420. { .compatible = "atmel,at91sam9260-rtt" },
  421. { /* sentinel */ }
  422. };
  423. MODULE_DEVICE_TABLE(of, at91_rtc_dt_ids);
  424. #endif
  425. static struct platform_driver at91_rtc_driver = {
  426. .probe = at91_rtc_probe,
  427. .remove = at91_rtc_remove,
  428. .shutdown = at91_rtc_shutdown,
  429. .driver = {
  430. .name = "rtc-at91sam9",
  431. .pm = &at91_rtc_pm_ops,
  432. .of_match_table = of_match_ptr(at91_rtc_dt_ids),
  433. },
  434. };
  435. module_platform_driver(at91_rtc_driver);
  436. MODULE_AUTHOR("Michel Benoit");
  437. MODULE_DESCRIPTION("RTC driver for Atmel AT91SAM9x");
  438. MODULE_LICENSE("GPL");