rtc-at91rm9200.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * Real Time Clock interface for Linux on Atmel AT91RM9200
  3. *
  4. * Copyright (C) 2002 Rick Bronson
  5. *
  6. * Converted to RTC class model by Andrew Victor
  7. *
  8. * Ported to Linux 2.6 by Steven Scholz
  9. * Based on s3c2410-rtc.c Simtec Electronics
  10. *
  11. * Based on sa1100-rtc.c by Nils Faerber
  12. * Based on rtc.c by Paul Gortmaker
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. */
  20. #include <linux/bcd.h>
  21. #include <linux/clk.h>
  22. #include <linux/completion.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/ioctl.h>
  25. #include <linux/io.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/of_device.h>
  29. #include <linux/of.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/rtc.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/suspend.h>
  34. #include <linux/time.h>
  35. #include <linux/uaccess.h>
  36. #include "rtc-at91rm9200.h"
  37. #define at91_rtc_read(field) \
  38. readl_relaxed(at91_rtc_regs + field)
  39. #define at91_rtc_write(field, val) \
  40. writel_relaxed((val), at91_rtc_regs + field)
  41. struct at91_rtc_config {
  42. bool use_shadow_imr;
  43. };
  44. static const struct at91_rtc_config *at91_rtc_config;
  45. static DECLARE_COMPLETION(at91_rtc_updated);
  46. static DECLARE_COMPLETION(at91_rtc_upd_rdy);
  47. static void __iomem *at91_rtc_regs;
  48. static int irq;
  49. static DEFINE_SPINLOCK(at91_rtc_lock);
  50. static u32 at91_rtc_shadow_imr;
  51. static bool suspended;
  52. static DEFINE_SPINLOCK(suspended_lock);
  53. static unsigned long cached_events;
  54. static u32 at91_rtc_imr;
  55. static struct clk *sclk;
  56. static void at91_rtc_write_ier(u32 mask)
  57. {
  58. unsigned long flags;
  59. spin_lock_irqsave(&at91_rtc_lock, flags);
  60. at91_rtc_shadow_imr |= mask;
  61. at91_rtc_write(AT91_RTC_IER, mask);
  62. spin_unlock_irqrestore(&at91_rtc_lock, flags);
  63. }
  64. static void at91_rtc_write_idr(u32 mask)
  65. {
  66. unsigned long flags;
  67. spin_lock_irqsave(&at91_rtc_lock, flags);
  68. at91_rtc_write(AT91_RTC_IDR, mask);
  69. /*
  70. * Register read back (of any RTC-register) needed to make sure
  71. * IDR-register write has reached the peripheral before updating
  72. * shadow mask.
  73. *
  74. * Note that there is still a possibility that the mask is updated
  75. * before interrupts have actually been disabled in hardware. The only
  76. * way to be certain would be to poll the IMR-register, which is is
  77. * the very register we are trying to emulate. The register read back
  78. * is a reasonable heuristic.
  79. */
  80. at91_rtc_read(AT91_RTC_SR);
  81. at91_rtc_shadow_imr &= ~mask;
  82. spin_unlock_irqrestore(&at91_rtc_lock, flags);
  83. }
  84. static u32 at91_rtc_read_imr(void)
  85. {
  86. unsigned long flags;
  87. u32 mask;
  88. if (at91_rtc_config->use_shadow_imr) {
  89. spin_lock_irqsave(&at91_rtc_lock, flags);
  90. mask = at91_rtc_shadow_imr;
  91. spin_unlock_irqrestore(&at91_rtc_lock, flags);
  92. } else {
  93. mask = at91_rtc_read(AT91_RTC_IMR);
  94. }
  95. return mask;
  96. }
  97. /*
  98. * Decode time/date into rtc_time structure
  99. */
  100. static void at91_rtc_decodetime(unsigned int timereg, unsigned int calreg,
  101. struct rtc_time *tm)
  102. {
  103. unsigned int time, date;
  104. /* must read twice in case it changes */
  105. do {
  106. time = at91_rtc_read(timereg);
  107. date = at91_rtc_read(calreg);
  108. } while ((time != at91_rtc_read(timereg)) ||
  109. (date != at91_rtc_read(calreg)));
  110. tm->tm_sec = bcd2bin((time & AT91_RTC_SEC) >> 0);
  111. tm->tm_min = bcd2bin((time & AT91_RTC_MIN) >> 8);
  112. tm->tm_hour = bcd2bin((time & AT91_RTC_HOUR) >> 16);
  113. /*
  114. * The Calendar Alarm register does not have a field for
  115. * the year - so these will return an invalid value.
  116. */
  117. tm->tm_year = bcd2bin(date & AT91_RTC_CENT) * 100; /* century */
  118. tm->tm_year += bcd2bin((date & AT91_RTC_YEAR) >> 8); /* year */
  119. tm->tm_wday = bcd2bin((date & AT91_RTC_DAY) >> 21) - 1; /* day of the week [0-6], Sunday=0 */
  120. tm->tm_mon = bcd2bin((date & AT91_RTC_MONTH) >> 16) - 1;
  121. tm->tm_mday = bcd2bin((date & AT91_RTC_DATE) >> 24);
  122. }
  123. /*
  124. * Read current time and date in RTC
  125. */
  126. static int at91_rtc_readtime(struct device *dev, struct rtc_time *tm)
  127. {
  128. at91_rtc_decodetime(AT91_RTC_TIMR, AT91_RTC_CALR, tm);
  129. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
  130. tm->tm_year = tm->tm_year - 1900;
  131. dev_dbg(dev, "%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__,
  132. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  133. tm->tm_hour, tm->tm_min, tm->tm_sec);
  134. return 0;
  135. }
  136. /*
  137. * Set current time and date in RTC
  138. */
  139. static int at91_rtc_settime(struct device *dev, struct rtc_time *tm)
  140. {
  141. unsigned long cr;
  142. dev_dbg(dev, "%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__,
  143. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  144. tm->tm_hour, tm->tm_min, tm->tm_sec);
  145. wait_for_completion(&at91_rtc_upd_rdy);
  146. /* Stop Time/Calendar from counting */
  147. cr = at91_rtc_read(AT91_RTC_CR);
  148. at91_rtc_write(AT91_RTC_CR, cr | AT91_RTC_UPDCAL | AT91_RTC_UPDTIM);
  149. at91_rtc_write_ier(AT91_RTC_ACKUPD);
  150. wait_for_completion(&at91_rtc_updated); /* wait for ACKUPD interrupt */
  151. at91_rtc_write_idr(AT91_RTC_ACKUPD);
  152. at91_rtc_write(AT91_RTC_TIMR,
  153. bin2bcd(tm->tm_sec) << 0
  154. | bin2bcd(tm->tm_min) << 8
  155. | bin2bcd(tm->tm_hour) << 16);
  156. at91_rtc_write(AT91_RTC_CALR,
  157. bin2bcd((tm->tm_year + 1900) / 100) /* century */
  158. | bin2bcd(tm->tm_year % 100) << 8 /* year */
  159. | bin2bcd(tm->tm_mon + 1) << 16 /* tm_mon starts at zero */
  160. | bin2bcd(tm->tm_wday + 1) << 21 /* day of the week [0-6], Sunday=0 */
  161. | bin2bcd(tm->tm_mday) << 24);
  162. /* Restart Time/Calendar */
  163. cr = at91_rtc_read(AT91_RTC_CR);
  164. at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_SECEV);
  165. at91_rtc_write(AT91_RTC_CR, cr & ~(AT91_RTC_UPDCAL | AT91_RTC_UPDTIM));
  166. at91_rtc_write_ier(AT91_RTC_SECEV);
  167. return 0;
  168. }
  169. /*
  170. * Read alarm time and date in RTC
  171. */
  172. static int at91_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
  173. {
  174. struct rtc_time *tm = &alrm->time;
  175. at91_rtc_decodetime(AT91_RTC_TIMALR, AT91_RTC_CALALR, tm);
  176. tm->tm_year = -1;
  177. alrm->enabled = (at91_rtc_read_imr() & AT91_RTC_ALARM)
  178. ? 1 : 0;
  179. dev_dbg(dev, "%s(): %02d-%02d %02d:%02d:%02d %sabled\n", __func__,
  180. tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
  181. alrm->enabled ? "en" : "dis");
  182. return 0;
  183. }
  184. /*
  185. * Set alarm time and date in RTC
  186. */
  187. static int at91_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  188. {
  189. struct rtc_time tm;
  190. at91_rtc_decodetime(AT91_RTC_TIMR, AT91_RTC_CALR, &tm);
  191. tm.tm_mon = alrm->time.tm_mon;
  192. tm.tm_mday = alrm->time.tm_mday;
  193. tm.tm_hour = alrm->time.tm_hour;
  194. tm.tm_min = alrm->time.tm_min;
  195. tm.tm_sec = alrm->time.tm_sec;
  196. at91_rtc_write_idr(AT91_RTC_ALARM);
  197. at91_rtc_write(AT91_RTC_TIMALR,
  198. bin2bcd(tm.tm_sec) << 0
  199. | bin2bcd(tm.tm_min) << 8
  200. | bin2bcd(tm.tm_hour) << 16
  201. | AT91_RTC_HOUREN | AT91_RTC_MINEN | AT91_RTC_SECEN);
  202. at91_rtc_write(AT91_RTC_CALALR,
  203. bin2bcd(tm.tm_mon + 1) << 16 /* tm_mon starts at zero */
  204. | bin2bcd(tm.tm_mday) << 24
  205. | AT91_RTC_DATEEN | AT91_RTC_MTHEN);
  206. if (alrm->enabled) {
  207. at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
  208. at91_rtc_write_ier(AT91_RTC_ALARM);
  209. }
  210. dev_dbg(dev, "%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__,
  211. tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour,
  212. tm.tm_min, tm.tm_sec);
  213. return 0;
  214. }
  215. static int at91_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  216. {
  217. dev_dbg(dev, "%s(): cmd=%08x\n", __func__, enabled);
  218. if (enabled) {
  219. at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
  220. at91_rtc_write_ier(AT91_RTC_ALARM);
  221. } else
  222. at91_rtc_write_idr(AT91_RTC_ALARM);
  223. return 0;
  224. }
  225. /*
  226. * Provide additional RTC information in /proc/driver/rtc
  227. */
  228. static int at91_rtc_proc(struct device *dev, struct seq_file *seq)
  229. {
  230. unsigned long imr = at91_rtc_read_imr();
  231. seq_printf(seq, "update_IRQ\t: %s\n",
  232. (imr & AT91_RTC_ACKUPD) ? "yes" : "no");
  233. seq_printf(seq, "periodic_IRQ\t: %s\n",
  234. (imr & AT91_RTC_SECEV) ? "yes" : "no");
  235. return 0;
  236. }
  237. /*
  238. * IRQ handler for the RTC
  239. */
  240. static irqreturn_t at91_rtc_interrupt(int irq, void *dev_id)
  241. {
  242. struct platform_device *pdev = dev_id;
  243. struct rtc_device *rtc = platform_get_drvdata(pdev);
  244. unsigned int rtsr;
  245. unsigned long events = 0;
  246. int ret = IRQ_NONE;
  247. spin_lock(&suspended_lock);
  248. rtsr = at91_rtc_read(AT91_RTC_SR) & at91_rtc_read_imr();
  249. if (rtsr) { /* this interrupt is shared! Is it ours? */
  250. if (rtsr & AT91_RTC_ALARM)
  251. events |= (RTC_AF | RTC_IRQF);
  252. if (rtsr & AT91_RTC_SECEV) {
  253. complete(&at91_rtc_upd_rdy);
  254. at91_rtc_write_idr(AT91_RTC_SECEV);
  255. }
  256. if (rtsr & AT91_RTC_ACKUPD)
  257. complete(&at91_rtc_updated);
  258. at91_rtc_write(AT91_RTC_SCCR, rtsr); /* clear status reg */
  259. if (!suspended) {
  260. rtc_update_irq(rtc, 1, events);
  261. dev_dbg(&pdev->dev, "%s(): num=%ld, events=0x%02lx\n",
  262. __func__, events >> 8, events & 0x000000FF);
  263. } else {
  264. cached_events |= events;
  265. at91_rtc_write_idr(at91_rtc_imr);
  266. pm_system_wakeup();
  267. }
  268. ret = IRQ_HANDLED;
  269. }
  270. spin_unlock(&suspended_lock);
  271. return ret;
  272. }
  273. static const struct at91_rtc_config at91rm9200_config = {
  274. };
  275. static const struct at91_rtc_config at91sam9x5_config = {
  276. .use_shadow_imr = true,
  277. };
  278. #ifdef CONFIG_OF
  279. static const struct of_device_id at91_rtc_dt_ids[] = {
  280. {
  281. .compatible = "atmel,at91rm9200-rtc",
  282. .data = &at91rm9200_config,
  283. }, {
  284. .compatible = "atmel,at91sam9x5-rtc",
  285. .data = &at91sam9x5_config,
  286. }, {
  287. /* sentinel */
  288. }
  289. };
  290. MODULE_DEVICE_TABLE(of, at91_rtc_dt_ids);
  291. #endif
  292. static const struct at91_rtc_config *
  293. at91_rtc_get_config(struct platform_device *pdev)
  294. {
  295. const struct of_device_id *match;
  296. if (pdev->dev.of_node) {
  297. match = of_match_node(at91_rtc_dt_ids, pdev->dev.of_node);
  298. if (!match)
  299. return NULL;
  300. return (const struct at91_rtc_config *)match->data;
  301. }
  302. return &at91rm9200_config;
  303. }
  304. static const struct rtc_class_ops at91_rtc_ops = {
  305. .read_time = at91_rtc_readtime,
  306. .set_time = at91_rtc_settime,
  307. .read_alarm = at91_rtc_readalarm,
  308. .set_alarm = at91_rtc_setalarm,
  309. .proc = at91_rtc_proc,
  310. .alarm_irq_enable = at91_rtc_alarm_irq_enable,
  311. };
  312. /*
  313. * Initialize and install RTC driver
  314. */
  315. static int __init at91_rtc_probe(struct platform_device *pdev)
  316. {
  317. struct rtc_device *rtc;
  318. struct resource *regs;
  319. int ret = 0;
  320. at91_rtc_config = at91_rtc_get_config(pdev);
  321. if (!at91_rtc_config)
  322. return -ENODEV;
  323. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  324. if (!regs) {
  325. dev_err(&pdev->dev, "no mmio resource defined\n");
  326. return -ENXIO;
  327. }
  328. irq = platform_get_irq(pdev, 0);
  329. if (irq < 0) {
  330. dev_err(&pdev->dev, "no irq resource defined\n");
  331. return -ENXIO;
  332. }
  333. at91_rtc_regs = devm_ioremap(&pdev->dev, regs->start,
  334. resource_size(regs));
  335. if (!at91_rtc_regs) {
  336. dev_err(&pdev->dev, "failed to map registers, aborting.\n");
  337. return -ENOMEM;
  338. }
  339. rtc = devm_rtc_allocate_device(&pdev->dev);
  340. if (IS_ERR(rtc))
  341. return PTR_ERR(rtc);
  342. platform_set_drvdata(pdev, rtc);
  343. sclk = devm_clk_get(&pdev->dev, NULL);
  344. if (IS_ERR(sclk))
  345. return PTR_ERR(sclk);
  346. ret = clk_prepare_enable(sclk);
  347. if (ret) {
  348. dev_err(&pdev->dev, "Could not enable slow clock\n");
  349. return ret;
  350. }
  351. at91_rtc_write(AT91_RTC_CR, 0);
  352. at91_rtc_write(AT91_RTC_MR, 0); /* 24 hour mode */
  353. /* Disable all interrupts */
  354. at91_rtc_write_idr(AT91_RTC_ACKUPD | AT91_RTC_ALARM |
  355. AT91_RTC_SECEV | AT91_RTC_TIMEV |
  356. AT91_RTC_CALEV);
  357. ret = devm_request_irq(&pdev->dev, irq, at91_rtc_interrupt,
  358. IRQF_SHARED | IRQF_COND_SUSPEND,
  359. "at91_rtc", pdev);
  360. if (ret) {
  361. dev_err(&pdev->dev, "IRQ %d already in use.\n", irq);
  362. goto err_clk;
  363. }
  364. /* cpu init code should really have flagged this device as
  365. * being wake-capable; if it didn't, do that here.
  366. */
  367. if (!device_can_wakeup(&pdev->dev))
  368. device_init_wakeup(&pdev->dev, 1);
  369. rtc->ops = &at91_rtc_ops;
  370. rtc->range_min = RTC_TIMESTAMP_BEGIN_1900;
  371. rtc->range_max = RTC_TIMESTAMP_END_2099;
  372. ret = rtc_register_device(rtc);
  373. if (ret)
  374. goto err_clk;
  375. /* enable SECEV interrupt in order to initialize at91_rtc_upd_rdy
  376. * completion.
  377. */
  378. at91_rtc_write_ier(AT91_RTC_SECEV);
  379. dev_info(&pdev->dev, "AT91 Real Time Clock driver.\n");
  380. return 0;
  381. err_clk:
  382. clk_disable_unprepare(sclk);
  383. return ret;
  384. }
  385. /*
  386. * Disable and remove the RTC driver
  387. */
  388. static int __exit at91_rtc_remove(struct platform_device *pdev)
  389. {
  390. /* Disable all interrupts */
  391. at91_rtc_write_idr(AT91_RTC_ACKUPD | AT91_RTC_ALARM |
  392. AT91_RTC_SECEV | AT91_RTC_TIMEV |
  393. AT91_RTC_CALEV);
  394. clk_disable_unprepare(sclk);
  395. return 0;
  396. }
  397. static void at91_rtc_shutdown(struct platform_device *pdev)
  398. {
  399. /* Disable all interrupts */
  400. at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ACKUPD | AT91_RTC_ALARM |
  401. AT91_RTC_SECEV | AT91_RTC_TIMEV |
  402. AT91_RTC_CALEV);
  403. }
  404. #ifdef CONFIG_PM_SLEEP
  405. /* AT91RM9200 RTC Power management control */
  406. static int at91_rtc_suspend(struct device *dev)
  407. {
  408. /* this IRQ is shared with DBGU and other hardware which isn't
  409. * necessarily doing PM like we are...
  410. */
  411. at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
  412. at91_rtc_imr = at91_rtc_read_imr()
  413. & (AT91_RTC_ALARM|AT91_RTC_SECEV);
  414. if (at91_rtc_imr) {
  415. if (device_may_wakeup(dev)) {
  416. unsigned long flags;
  417. enable_irq_wake(irq);
  418. spin_lock_irqsave(&suspended_lock, flags);
  419. suspended = true;
  420. spin_unlock_irqrestore(&suspended_lock, flags);
  421. } else {
  422. at91_rtc_write_idr(at91_rtc_imr);
  423. }
  424. }
  425. return 0;
  426. }
  427. static int at91_rtc_resume(struct device *dev)
  428. {
  429. struct rtc_device *rtc = dev_get_drvdata(dev);
  430. if (at91_rtc_imr) {
  431. if (device_may_wakeup(dev)) {
  432. unsigned long flags;
  433. spin_lock_irqsave(&suspended_lock, flags);
  434. if (cached_events) {
  435. rtc_update_irq(rtc, 1, cached_events);
  436. cached_events = 0;
  437. }
  438. suspended = false;
  439. spin_unlock_irqrestore(&suspended_lock, flags);
  440. disable_irq_wake(irq);
  441. }
  442. at91_rtc_write_ier(at91_rtc_imr);
  443. }
  444. return 0;
  445. }
  446. #endif
  447. static SIMPLE_DEV_PM_OPS(at91_rtc_pm_ops, at91_rtc_suspend, at91_rtc_resume);
  448. static struct platform_driver at91_rtc_driver = {
  449. .remove = __exit_p(at91_rtc_remove),
  450. .shutdown = at91_rtc_shutdown,
  451. .driver = {
  452. .name = "at91_rtc",
  453. .pm = &at91_rtc_pm_ops,
  454. .of_match_table = of_match_ptr(at91_rtc_dt_ids),
  455. },
  456. };
  457. module_platform_driver_probe(at91_rtc_driver, at91_rtc_probe);
  458. MODULE_AUTHOR("Rick Bronson");
  459. MODULE_DESCRIPTION("RTC driver for Atmel AT91RM9200");
  460. MODULE_LICENSE("GPL");
  461. MODULE_ALIAS("platform:at91_rtc");