rtc-at91rm9200.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/time.h>
  24. #include <linux/rtc.h>
  25. #include <linux/bcd.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/ioctl.h>
  28. #include <linux/completion.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/rtc.h>
  31. #include <asm/arch/at91_rtc.h>
  32. #define AT91_RTC_FREQ 1
  33. #define AT91_RTC_EPOCH 1900UL /* just like arch/arm/common/rtctime.c */
  34. static DECLARE_COMPLETION(at91_rtc_updated);
  35. static unsigned int at91_alarm_year = AT91_RTC_EPOCH;
  36. /*
  37. * Decode time/date into rtc_time structure
  38. */
  39. static void at91_rtc_decodetime(unsigned int timereg, unsigned int calreg,
  40. struct rtc_time *tm)
  41. {
  42. unsigned int time, date;
  43. /* must read twice in case it changes */
  44. do {
  45. time = at91_sys_read(timereg);
  46. date = at91_sys_read(calreg);
  47. } while ((time != at91_sys_read(timereg)) ||
  48. (date != at91_sys_read(calreg)));
  49. tm->tm_sec = BCD2BIN((time & AT91_RTC_SEC) >> 0);
  50. tm->tm_min = BCD2BIN((time & AT91_RTC_MIN) >> 8);
  51. tm->tm_hour = BCD2BIN((time & AT91_RTC_HOUR) >> 16);
  52. /*
  53. * The Calendar Alarm register does not have a field for
  54. * the year - so these will return an invalid value. When an
  55. * alarm is set, at91_alarm_year wille store the current year.
  56. */
  57. tm->tm_year = BCD2BIN(date & AT91_RTC_CENT) * 100; /* century */
  58. tm->tm_year += BCD2BIN((date & AT91_RTC_YEAR) >> 8); /* year */
  59. tm->tm_wday = BCD2BIN((date & AT91_RTC_DAY) >> 21) - 1; /* day of the week [0-6], Sunday=0 */
  60. tm->tm_mon = BCD2BIN((date & AT91_RTC_MONTH) >> 16) - 1;
  61. tm->tm_mday = BCD2BIN((date & AT91_RTC_DATE) >> 24);
  62. }
  63. /*
  64. * Read current time and date in RTC
  65. */
  66. static int at91_rtc_readtime(struct device *dev, struct rtc_time *tm)
  67. {
  68. at91_rtc_decodetime(AT91_RTC_TIMR, AT91_RTC_CALR, tm);
  69. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
  70. tm->tm_year = tm->tm_year - 1900;
  71. pr_debug("%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__,
  72. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  73. tm->tm_hour, tm->tm_min, tm->tm_sec);
  74. return 0;
  75. }
  76. /*
  77. * Set current time and date in RTC
  78. */
  79. static int at91_rtc_settime(struct device *dev, struct rtc_time *tm)
  80. {
  81. unsigned long cr;
  82. pr_debug("%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__,
  83. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  84. tm->tm_hour, tm->tm_min, tm->tm_sec);
  85. /* Stop Time/Calendar from counting */
  86. cr = at91_sys_read(AT91_RTC_CR);
  87. at91_sys_write(AT91_RTC_CR, cr | AT91_RTC_UPDCAL | AT91_RTC_UPDTIM);
  88. at91_sys_write(AT91_RTC_IER, AT91_RTC_ACKUPD);
  89. wait_for_completion(&at91_rtc_updated); /* wait for ACKUPD interrupt */
  90. at91_sys_write(AT91_RTC_IDR, AT91_RTC_ACKUPD);
  91. at91_sys_write(AT91_RTC_TIMR,
  92. BIN2BCD(tm->tm_sec) << 0
  93. | BIN2BCD(tm->tm_min) << 8
  94. | BIN2BCD(tm->tm_hour) << 16);
  95. at91_sys_write(AT91_RTC_CALR,
  96. BIN2BCD((tm->tm_year + 1900) / 100) /* century */
  97. | BIN2BCD(tm->tm_year % 100) << 8 /* year */
  98. | BIN2BCD(tm->tm_mon + 1) << 16 /* tm_mon starts at zero */
  99. | BIN2BCD(tm->tm_wday + 1) << 21 /* day of the week [0-6], Sunday=0 */
  100. | BIN2BCD(tm->tm_mday) << 24);
  101. /* Restart Time/Calendar */
  102. cr = at91_sys_read(AT91_RTC_CR);
  103. at91_sys_write(AT91_RTC_CR, cr & ~(AT91_RTC_UPDCAL | AT91_RTC_UPDTIM));
  104. return 0;
  105. }
  106. /*
  107. * Read alarm time and date in RTC
  108. */
  109. static int at91_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
  110. {
  111. struct rtc_time *tm = &alrm->time;
  112. at91_rtc_decodetime(AT91_RTC_TIMALR, AT91_RTC_CALALR, tm);
  113. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
  114. tm->tm_year = at91_alarm_year - 1900;
  115. alrm->enabled = (at91_sys_read(AT91_RTC_IMR) & AT91_RTC_ALARM)
  116. ? 1 : 0;
  117. pr_debug("%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__,
  118. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  119. tm->tm_hour, tm->tm_min, tm->tm_sec);
  120. return 0;
  121. }
  122. /*
  123. * Set alarm time and date in RTC
  124. */
  125. static int at91_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  126. {
  127. struct rtc_time tm;
  128. at91_rtc_decodetime(AT91_RTC_TIMR, AT91_RTC_CALR, &tm);
  129. at91_alarm_year = tm.tm_year;
  130. tm.tm_hour = alrm->time.tm_hour;
  131. tm.tm_min = alrm->time.tm_min;
  132. tm.tm_sec = alrm->time.tm_sec;
  133. at91_sys_write(AT91_RTC_IDR, AT91_RTC_ALARM);
  134. at91_sys_write(AT91_RTC_TIMALR,
  135. BIN2BCD(tm.tm_sec) << 0
  136. | BIN2BCD(tm.tm_min) << 8
  137. | BIN2BCD(tm.tm_hour) << 16
  138. | AT91_RTC_HOUREN | AT91_RTC_MINEN | AT91_RTC_SECEN);
  139. at91_sys_write(AT91_RTC_CALALR,
  140. BIN2BCD(tm.tm_mon + 1) << 16 /* tm_mon starts at zero */
  141. | BIN2BCD(tm.tm_mday) << 24
  142. | AT91_RTC_DATEEN | AT91_RTC_MTHEN);
  143. if (alrm->enabled)
  144. at91_sys_write(AT91_RTC_IER, AT91_RTC_ALARM);
  145. pr_debug("%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__,
  146. at91_alarm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour,
  147. tm.tm_min, tm.tm_sec);
  148. return 0;
  149. }
  150. /*
  151. * Handle commands from user-space
  152. */
  153. static int at91_rtc_ioctl(struct device *dev, unsigned int cmd,
  154. unsigned long arg)
  155. {
  156. int ret = 0;
  157. pr_debug("%s(): cmd=%08x, arg=%08lx.\n", __func__, cmd, arg);
  158. switch (cmd) {
  159. case RTC_AIE_OFF: /* alarm off */
  160. at91_sys_write(AT91_RTC_IDR, AT91_RTC_ALARM);
  161. break;
  162. case RTC_AIE_ON: /* alarm on */
  163. at91_sys_write(AT91_RTC_IER, AT91_RTC_ALARM);
  164. break;
  165. case RTC_UIE_OFF: /* update off */
  166. case RTC_PIE_OFF: /* periodic off */
  167. at91_sys_write(AT91_RTC_IDR, AT91_RTC_SECEV);
  168. break;
  169. case RTC_UIE_ON: /* update on */
  170. case RTC_PIE_ON: /* periodic on */
  171. at91_sys_write(AT91_RTC_IER, AT91_RTC_SECEV);
  172. break;
  173. case RTC_IRQP_READ: /* read periodic alarm frequency */
  174. ret = put_user(AT91_RTC_FREQ, (unsigned long *) arg);
  175. break;
  176. case RTC_IRQP_SET: /* set periodic alarm frequency */
  177. if (arg != AT91_RTC_FREQ)
  178. ret = -EINVAL;
  179. break;
  180. default:
  181. ret = -ENOIOCTLCMD;
  182. break;
  183. }
  184. return ret;
  185. }
  186. /*
  187. * Provide additional RTC information in /proc/driver/rtc
  188. */
  189. static int at91_rtc_proc(struct device *dev, struct seq_file *seq)
  190. {
  191. unsigned long imr = at91_sys_read(AT91_RTC_IMR);
  192. seq_printf(seq, "update_IRQ\t: %s\n",
  193. (imr & AT91_RTC_ACKUPD) ? "yes" : "no");
  194. seq_printf(seq, "periodic_IRQ\t: %s\n",
  195. (imr & AT91_RTC_SECEV) ? "yes" : "no");
  196. seq_printf(seq, "periodic_freq\t: %ld\n",
  197. (unsigned long) AT91_RTC_FREQ);
  198. return 0;
  199. }
  200. /*
  201. * IRQ handler for the RTC
  202. */
  203. static irqreturn_t at91_rtc_interrupt(int irq, void *dev_id)
  204. {
  205. struct platform_device *pdev = dev_id;
  206. struct rtc_device *rtc = platform_get_drvdata(pdev);
  207. unsigned int rtsr;
  208. unsigned long events = 0;
  209. rtsr = at91_sys_read(AT91_RTC_SR) & at91_sys_read(AT91_RTC_IMR);
  210. if (rtsr) { /* this interrupt is shared! Is it ours? */
  211. if (rtsr & AT91_RTC_ALARM)
  212. events |= (RTC_AF | RTC_IRQF);
  213. if (rtsr & AT91_RTC_SECEV)
  214. events |= (RTC_UF | RTC_IRQF);
  215. if (rtsr & AT91_RTC_ACKUPD)
  216. complete(&at91_rtc_updated);
  217. at91_sys_write(AT91_RTC_SCCR, rtsr); /* clear status reg */
  218. rtc_update_irq(rtc, 1, events);
  219. pr_debug("%s(): num=%ld, events=0x%02lx\n", __func__,
  220. events >> 8, events & 0x000000FF);
  221. return IRQ_HANDLED;
  222. }
  223. return IRQ_NONE; /* not handled */
  224. }
  225. static const struct rtc_class_ops at91_rtc_ops = {
  226. .ioctl = at91_rtc_ioctl,
  227. .read_time = at91_rtc_readtime,
  228. .set_time = at91_rtc_settime,
  229. .read_alarm = at91_rtc_readalarm,
  230. .set_alarm = at91_rtc_setalarm,
  231. .proc = at91_rtc_proc,
  232. };
  233. /*
  234. * Initialize and install RTC driver
  235. */
  236. static int __init at91_rtc_probe(struct platform_device *pdev)
  237. {
  238. struct rtc_device *rtc;
  239. int ret;
  240. at91_sys_write(AT91_RTC_CR, 0);
  241. at91_sys_write(AT91_RTC_MR, 0); /* 24 hour mode */
  242. /* Disable all interrupts */
  243. at91_sys_write(AT91_RTC_IDR, AT91_RTC_ACKUPD | AT91_RTC_ALARM |
  244. AT91_RTC_SECEV | AT91_RTC_TIMEV |
  245. AT91_RTC_CALEV);
  246. ret = request_irq(AT91_ID_SYS, at91_rtc_interrupt,
  247. IRQF_DISABLED | IRQF_SHARED,
  248. "at91_rtc", pdev);
  249. if (ret) {
  250. printk(KERN_ERR "at91_rtc: IRQ %d already in use.\n",
  251. AT91_ID_SYS);
  252. return ret;
  253. }
  254. /* cpu init code should really have flagged this device as
  255. * being wake-capable; if it didn't, do that here.
  256. */
  257. if (!device_can_wakeup(&pdev->dev))
  258. device_init_wakeup(&pdev->dev, 1);
  259. rtc = rtc_device_register(pdev->name, &pdev->dev,
  260. &at91_rtc_ops, THIS_MODULE);
  261. if (IS_ERR(rtc)) {
  262. free_irq(AT91_ID_SYS, pdev);
  263. return PTR_ERR(rtc);
  264. }
  265. platform_set_drvdata(pdev, rtc);
  266. printk(KERN_INFO "AT91 Real Time Clock driver.\n");
  267. return 0;
  268. }
  269. /*
  270. * Disable and remove the RTC driver
  271. */
  272. static int __exit at91_rtc_remove(struct platform_device *pdev)
  273. {
  274. struct rtc_device *rtc = platform_get_drvdata(pdev);
  275. /* Disable all interrupts */
  276. at91_sys_write(AT91_RTC_IDR, AT91_RTC_ACKUPD | AT91_RTC_ALARM |
  277. AT91_RTC_SECEV | AT91_RTC_TIMEV |
  278. AT91_RTC_CALEV);
  279. free_irq(AT91_ID_SYS, pdev);
  280. rtc_device_unregister(rtc);
  281. platform_set_drvdata(pdev, NULL);
  282. return 0;
  283. }
  284. #ifdef CONFIG_PM
  285. /* AT91RM9200 RTC Power management control */
  286. static u32 at91_rtc_imr;
  287. static int at91_rtc_suspend(struct platform_device *pdev, pm_message_t state)
  288. {
  289. /* this IRQ is shared with DBGU and other hardware which isn't
  290. * necessarily doing PM like we are...
  291. */
  292. at91_rtc_imr = at91_sys_read(AT91_RTC_IMR)
  293. & (AT91_RTC_ALARM|AT91_RTC_SECEV);
  294. if (at91_rtc_imr) {
  295. if (device_may_wakeup(&pdev->dev))
  296. enable_irq_wake(AT91_ID_SYS);
  297. else
  298. at91_sys_write(AT91_RTC_IDR, at91_rtc_imr);
  299. }
  300. return 0;
  301. }
  302. static int at91_rtc_resume(struct platform_device *pdev)
  303. {
  304. if (at91_rtc_imr) {
  305. if (device_may_wakeup(&pdev->dev))
  306. disable_irq_wake(AT91_ID_SYS);
  307. else
  308. at91_sys_write(AT91_RTC_IER, at91_rtc_imr);
  309. }
  310. return 0;
  311. }
  312. #else
  313. #define at91_rtc_suspend NULL
  314. #define at91_rtc_resume NULL
  315. #endif
  316. static struct platform_driver at91_rtc_driver = {
  317. .remove = __exit_p(at91_rtc_remove),
  318. .suspend = at91_rtc_suspend,
  319. .resume = at91_rtc_resume,
  320. .driver = {
  321. .name = "at91_rtc",
  322. .owner = THIS_MODULE,
  323. },
  324. };
  325. static int __init at91_rtc_init(void)
  326. {
  327. return platform_driver_probe(&at91_rtc_driver, at91_rtc_probe);
  328. }
  329. static void __exit at91_rtc_exit(void)
  330. {
  331. platform_driver_unregister(&at91_rtc_driver);
  332. }
  333. module_init(at91_rtc_init);
  334. module_exit(at91_rtc_exit);
  335. MODULE_AUTHOR("Rick Bronson");
  336. MODULE_DESCRIPTION("RTC driver for Atmel AT91RM9200");
  337. MODULE_LICENSE("GPL");
  338. MODULE_ALIAS("platform:at91_rtc");