rtc-at91rm9200.c 14 KB

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