rtc-at91.c 10 KB

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