rtc.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * linux/arch/unicore32/kernel/rtc.c
  3. *
  4. * Code specific to PKUnity SoC and UniCore ISA
  5. *
  6. * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
  7. * Copyright (C) 2001-2010 Guan Xuetao
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/fs.h>
  15. #include <linux/string.h>
  16. #include <linux/init.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/rtc.h>
  20. #include <linux/bcd.h>
  21. #include <linux/clk.h>
  22. #include <linux/log2.h>
  23. #include <linux/slab.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/io.h>
  26. #include <asm/irq.h>
  27. #include <mach/hardware.h>
  28. static struct resource *puv3_rtc_mem;
  29. static int puv3_rtc_alarmno = IRQ_RTCAlarm;
  30. static int puv3_rtc_tickno = IRQ_RTC;
  31. static DEFINE_SPINLOCK(puv3_rtc_pie_lock);
  32. /* IRQ Handlers */
  33. static irqreturn_t puv3_rtc_alarmirq(int irq, void *id)
  34. {
  35. struct rtc_device *rdev = id;
  36. writel(readl(RTC_RTSR) | RTC_RTSR_AL, RTC_RTSR);
  37. rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
  38. return IRQ_HANDLED;
  39. }
  40. static irqreturn_t puv3_rtc_tickirq(int irq, void *id)
  41. {
  42. struct rtc_device *rdev = id;
  43. writel(readl(RTC_RTSR) | RTC_RTSR_HZ, RTC_RTSR);
  44. rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
  45. return IRQ_HANDLED;
  46. }
  47. /* Update control registers */
  48. static void puv3_rtc_setaie(int to)
  49. {
  50. unsigned int tmp;
  51. pr_debug("%s: aie=%d\n", __func__, to);
  52. tmp = readl(RTC_RTSR) & ~RTC_RTSR_ALE;
  53. if (to)
  54. tmp |= RTC_RTSR_ALE;
  55. writel(tmp, RTC_RTSR);
  56. }
  57. static int puv3_rtc_setpie(struct device *dev, int enabled)
  58. {
  59. unsigned int tmp;
  60. pr_debug("%s: pie=%d\n", __func__, enabled);
  61. spin_lock_irq(&puv3_rtc_pie_lock);
  62. tmp = readl(RTC_RTSR) & ~RTC_RTSR_HZE;
  63. if (enabled)
  64. tmp |= RTC_RTSR_HZE;
  65. writel(tmp, RTC_RTSR);
  66. spin_unlock_irq(&puv3_rtc_pie_lock);
  67. return 0;
  68. }
  69. /* Time read/write */
  70. static int puv3_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
  71. {
  72. rtc_time_to_tm(readl(RTC_RCNR), rtc_tm);
  73. pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n",
  74. rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
  75. rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
  76. return 0;
  77. }
  78. static int puv3_rtc_settime(struct device *dev, struct rtc_time *tm)
  79. {
  80. unsigned long rtc_count = 0;
  81. pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d\n",
  82. tm->tm_year, tm->tm_mon, tm->tm_mday,
  83. tm->tm_hour, tm->tm_min, tm->tm_sec);
  84. rtc_tm_to_time(tm, &rtc_count);
  85. writel(rtc_count, RTC_RCNR);
  86. return 0;
  87. }
  88. static int puv3_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
  89. {
  90. struct rtc_time *alm_tm = &alrm->time;
  91. rtc_time_to_tm(readl(RTC_RTAR), alm_tm);
  92. alrm->enabled = readl(RTC_RTSR) & RTC_RTSR_ALE;
  93. pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
  94. alrm->enabled,
  95. alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
  96. alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
  97. return 0;
  98. }
  99. static int puv3_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  100. {
  101. struct rtc_time *tm = &alrm->time;
  102. unsigned long rtcalarm_count = 0;
  103. pr_debug("puv3_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
  104. alrm->enabled,
  105. tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
  106. tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
  107. rtc_tm_to_time(tm, &rtcalarm_count);
  108. writel(rtcalarm_count, RTC_RTAR);
  109. puv3_rtc_setaie(alrm->enabled);
  110. if (alrm->enabled)
  111. enable_irq_wake(puv3_rtc_alarmno);
  112. else
  113. disable_irq_wake(puv3_rtc_alarmno);
  114. return 0;
  115. }
  116. static int puv3_rtc_proc(struct device *dev, struct seq_file *seq)
  117. {
  118. seq_printf(seq, "periodic_IRQ\t: %s\n",
  119. (readl(RTC_RTSR) & RTC_RTSR_HZE) ? "yes" : "no");
  120. return 0;
  121. }
  122. static int puv3_rtc_open(struct device *dev)
  123. {
  124. struct platform_device *pdev = to_platform_device(dev);
  125. struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
  126. int ret;
  127. ret = request_irq(puv3_rtc_alarmno, puv3_rtc_alarmirq,
  128. IRQF_DISABLED, "pkunity-rtc alarm", rtc_dev);
  129. if (ret) {
  130. dev_err(dev, "IRQ%d error %d\n", puv3_rtc_alarmno, ret);
  131. return ret;
  132. }
  133. ret = request_irq(puv3_rtc_tickno, puv3_rtc_tickirq,
  134. IRQF_DISABLED, "pkunity-rtc tick", rtc_dev);
  135. if (ret) {
  136. dev_err(dev, "IRQ%d error %d\n", puv3_rtc_tickno, ret);
  137. goto tick_err;
  138. }
  139. return ret;
  140. tick_err:
  141. free_irq(puv3_rtc_alarmno, rtc_dev);
  142. return ret;
  143. }
  144. static void puv3_rtc_release(struct device *dev)
  145. {
  146. struct platform_device *pdev = to_platform_device(dev);
  147. struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
  148. /* do not clear AIE here, it may be needed for wake */
  149. puv3_rtc_setpie(dev, 0);
  150. free_irq(puv3_rtc_alarmno, rtc_dev);
  151. free_irq(puv3_rtc_tickno, rtc_dev);
  152. }
  153. static const struct rtc_class_ops puv3_rtcops = {
  154. .open = puv3_rtc_open,
  155. .release = puv3_rtc_release,
  156. .read_time = puv3_rtc_gettime,
  157. .set_time = puv3_rtc_settime,
  158. .read_alarm = puv3_rtc_getalarm,
  159. .set_alarm = puv3_rtc_setalarm,
  160. .proc = puv3_rtc_proc,
  161. };
  162. static void puv3_rtc_enable(struct platform_device *pdev, int en)
  163. {
  164. if (!en) {
  165. writel(readl(RTC_RTSR) & ~RTC_RTSR_HZE, RTC_RTSR);
  166. } else {
  167. /* re-enable the device, and check it is ok */
  168. if ((readl(RTC_RTSR) & RTC_RTSR_HZE) == 0) {
  169. dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
  170. writel(readl(RTC_RTSR) | RTC_RTSR_HZE, RTC_RTSR);
  171. }
  172. }
  173. }
  174. static int puv3_rtc_remove(struct platform_device *dev)
  175. {
  176. struct rtc_device *rtc = platform_get_drvdata(dev);
  177. platform_set_drvdata(dev, NULL);
  178. rtc_device_unregister(rtc);
  179. puv3_rtc_setpie(&dev->dev, 0);
  180. puv3_rtc_setaie(0);
  181. release_resource(puv3_rtc_mem);
  182. kfree(puv3_rtc_mem);
  183. return 0;
  184. }
  185. static int puv3_rtc_probe(struct platform_device *pdev)
  186. {
  187. struct rtc_device *rtc;
  188. struct resource *res;
  189. int ret;
  190. pr_debug("%s: probe=%p\n", __func__, pdev);
  191. /* find the IRQs */
  192. puv3_rtc_tickno = platform_get_irq(pdev, 1);
  193. if (puv3_rtc_tickno < 0) {
  194. dev_err(&pdev->dev, "no irq for rtc tick\n");
  195. return -ENOENT;
  196. }
  197. puv3_rtc_alarmno = platform_get_irq(pdev, 0);
  198. if (puv3_rtc_alarmno < 0) {
  199. dev_err(&pdev->dev, "no irq for alarm\n");
  200. return -ENOENT;
  201. }
  202. pr_debug("PKUnity_rtc: tick irq %d, alarm irq %d\n",
  203. puv3_rtc_tickno, puv3_rtc_alarmno);
  204. /* get the memory region */
  205. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  206. if (res == NULL) {
  207. dev_err(&pdev->dev, "failed to get memory region resource\n");
  208. return -ENOENT;
  209. }
  210. puv3_rtc_mem = request_mem_region(res->start,
  211. res->end-res->start+1,
  212. pdev->name);
  213. if (puv3_rtc_mem == NULL) {
  214. dev_err(&pdev->dev, "failed to reserve memory region\n");
  215. ret = -ENOENT;
  216. goto err_nores;
  217. }
  218. puv3_rtc_enable(pdev, 1);
  219. /* register RTC and exit */
  220. rtc = rtc_device_register("pkunity", &pdev->dev, &puv3_rtcops,
  221. THIS_MODULE);
  222. if (IS_ERR(rtc)) {
  223. dev_err(&pdev->dev, "cannot attach rtc\n");
  224. ret = PTR_ERR(rtc);
  225. goto err_nortc;
  226. }
  227. /* platform setup code should have handled this; sigh */
  228. if (!device_can_wakeup(&pdev->dev))
  229. device_init_wakeup(&pdev->dev, 1);
  230. platform_set_drvdata(pdev, rtc);
  231. return 0;
  232. err_nortc:
  233. puv3_rtc_enable(pdev, 0);
  234. release_resource(puv3_rtc_mem);
  235. err_nores:
  236. return ret;
  237. }
  238. #ifdef CONFIG_PM
  239. /* RTC Power management control */
  240. static int ticnt_save;
  241. static int puv3_rtc_suspend(struct platform_device *pdev, pm_message_t state)
  242. {
  243. /* save RTAR for anyone using periodic interrupts */
  244. ticnt_save = readl(RTC_RTAR);
  245. puv3_rtc_enable(pdev, 0);
  246. return 0;
  247. }
  248. static int puv3_rtc_resume(struct platform_device *pdev)
  249. {
  250. puv3_rtc_enable(pdev, 1);
  251. writel(ticnt_save, RTC_RTAR);
  252. return 0;
  253. }
  254. #else
  255. #define puv3_rtc_suspend NULL
  256. #define puv3_rtc_resume NULL
  257. #endif
  258. static struct platform_driver puv3_rtcdrv = {
  259. .probe = puv3_rtc_probe,
  260. .remove = __devexit_p(puv3_rtc_remove),
  261. .suspend = puv3_rtc_suspend,
  262. .resume = puv3_rtc_resume,
  263. .driver = {
  264. .name = "PKUnity-v3-RTC",
  265. .owner = THIS_MODULE,
  266. }
  267. };
  268. static char __initdata banner[] = "PKUnity-v3 RTC, (c) 2009 PKUnity Co.\n";
  269. static int __init puv3_rtc_init(void)
  270. {
  271. printk(banner);
  272. return platform_driver_register(&puv3_rtcdrv);
  273. }
  274. static void __exit puv3_rtc_exit(void)
  275. {
  276. platform_driver_unregister(&puv3_rtcdrv);
  277. }
  278. module_init(puv3_rtc_init);
  279. module_exit(puv3_rtc_exit);
  280. MODULE_DESCRIPTION("RTC Driver for the PKUnity v3 chip");
  281. MODULE_AUTHOR("Hu Dongliang");
  282. MODULE_LICENSE("GPL v2");