rtc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. static int puv3_rtc_setfreq(struct device *dev, int freq)
  70. {
  71. return 0;
  72. }
  73. /* Time read/write */
  74. static int puv3_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
  75. {
  76. rtc_time_to_tm(readl(RTC_RCNR), rtc_tm);
  77. pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n",
  78. rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
  79. rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
  80. return 0;
  81. }
  82. static int puv3_rtc_settime(struct device *dev, struct rtc_time *tm)
  83. {
  84. unsigned long rtc_count = 0;
  85. pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d\n",
  86. tm->tm_year, tm->tm_mon, tm->tm_mday,
  87. tm->tm_hour, tm->tm_min, tm->tm_sec);
  88. rtc_tm_to_time(tm, &rtc_count);
  89. writel(rtc_count, RTC_RCNR);
  90. return 0;
  91. }
  92. static int puv3_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
  93. {
  94. struct rtc_time *alm_tm = &alrm->time;
  95. rtc_time_to_tm(readl(RTC_RTAR), alm_tm);
  96. alrm->enabled = readl(RTC_RTSR) & RTC_RTSR_ALE;
  97. pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
  98. alrm->enabled,
  99. alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
  100. alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
  101. return 0;
  102. }
  103. static int puv3_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  104. {
  105. struct rtc_time *tm = &alrm->time;
  106. unsigned long rtcalarm_count = 0;
  107. pr_debug("puv3_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
  108. alrm->enabled,
  109. tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
  110. tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
  111. rtc_tm_to_time(tm, &rtcalarm_count);
  112. writel(rtcalarm_count, RTC_RTAR);
  113. puv3_rtc_setaie(alrm->enabled);
  114. if (alrm->enabled)
  115. enable_irq_wake(puv3_rtc_alarmno);
  116. else
  117. disable_irq_wake(puv3_rtc_alarmno);
  118. return 0;
  119. }
  120. static int puv3_rtc_proc(struct device *dev, struct seq_file *seq)
  121. {
  122. seq_printf(seq, "periodic_IRQ\t: %s\n",
  123. (readl(RTC_RTSR) & RTC_RTSR_HZE) ? "yes" : "no");
  124. return 0;
  125. }
  126. static int puv3_rtc_open(struct device *dev)
  127. {
  128. struct platform_device *pdev = to_platform_device(dev);
  129. struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
  130. int ret;
  131. ret = request_irq(puv3_rtc_alarmno, puv3_rtc_alarmirq,
  132. IRQF_DISABLED, "pkunity-rtc alarm", rtc_dev);
  133. if (ret) {
  134. dev_err(dev, "IRQ%d error %d\n", puv3_rtc_alarmno, ret);
  135. return ret;
  136. }
  137. ret = request_irq(puv3_rtc_tickno, puv3_rtc_tickirq,
  138. IRQF_DISABLED, "pkunity-rtc tick", rtc_dev);
  139. if (ret) {
  140. dev_err(dev, "IRQ%d error %d\n", puv3_rtc_tickno, ret);
  141. goto tick_err;
  142. }
  143. return ret;
  144. tick_err:
  145. free_irq(puv3_rtc_alarmno, rtc_dev);
  146. return ret;
  147. }
  148. static void puv3_rtc_release(struct device *dev)
  149. {
  150. struct platform_device *pdev = to_platform_device(dev);
  151. struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
  152. /* do not clear AIE here, it may be needed for wake */
  153. puv3_rtc_setpie(dev, 0);
  154. free_irq(puv3_rtc_alarmno, rtc_dev);
  155. free_irq(puv3_rtc_tickno, rtc_dev);
  156. }
  157. static const struct rtc_class_ops puv3_rtcops = {
  158. .open = puv3_rtc_open,
  159. .release = puv3_rtc_release,
  160. .read_time = puv3_rtc_gettime,
  161. .set_time = puv3_rtc_settime,
  162. .read_alarm = puv3_rtc_getalarm,
  163. .set_alarm = puv3_rtc_setalarm,
  164. .irq_set_freq = puv3_rtc_setfreq,
  165. .irq_set_state = puv3_rtc_setpie,
  166. .proc = puv3_rtc_proc,
  167. };
  168. static void puv3_rtc_enable(struct platform_device *pdev, int en)
  169. {
  170. if (!en) {
  171. writel(readl(RTC_RTSR) & ~RTC_RTSR_HZE, RTC_RTSR);
  172. } else {
  173. /* re-enable the device, and check it is ok */
  174. if ((readl(RTC_RTSR) & RTC_RTSR_HZE) == 0) {
  175. dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
  176. writel(readl(RTC_RTSR) | RTC_RTSR_HZE, RTC_RTSR);
  177. }
  178. }
  179. }
  180. static int puv3_rtc_remove(struct platform_device *dev)
  181. {
  182. struct rtc_device *rtc = platform_get_drvdata(dev);
  183. platform_set_drvdata(dev, NULL);
  184. rtc_device_unregister(rtc);
  185. puv3_rtc_setpie(&dev->dev, 0);
  186. puv3_rtc_setaie(0);
  187. release_resource(puv3_rtc_mem);
  188. kfree(puv3_rtc_mem);
  189. return 0;
  190. }
  191. static int puv3_rtc_probe(struct platform_device *pdev)
  192. {
  193. struct rtc_device *rtc;
  194. struct resource *res;
  195. int ret;
  196. pr_debug("%s: probe=%p\n", __func__, pdev);
  197. /* find the IRQs */
  198. puv3_rtc_tickno = platform_get_irq(pdev, 1);
  199. if (puv3_rtc_tickno < 0) {
  200. dev_err(&pdev->dev, "no irq for rtc tick\n");
  201. return -ENOENT;
  202. }
  203. puv3_rtc_alarmno = platform_get_irq(pdev, 0);
  204. if (puv3_rtc_alarmno < 0) {
  205. dev_err(&pdev->dev, "no irq for alarm\n");
  206. return -ENOENT;
  207. }
  208. pr_debug("PKUnity_rtc: tick irq %d, alarm irq %d\n",
  209. puv3_rtc_tickno, puv3_rtc_alarmno);
  210. /* get the memory region */
  211. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  212. if (res == NULL) {
  213. dev_err(&pdev->dev, "failed to get memory region resource\n");
  214. return -ENOENT;
  215. }
  216. puv3_rtc_mem = request_mem_region(res->start,
  217. res->end-res->start+1,
  218. pdev->name);
  219. if (puv3_rtc_mem == NULL) {
  220. dev_err(&pdev->dev, "failed to reserve memory region\n");
  221. ret = -ENOENT;
  222. goto err_nores;
  223. }
  224. puv3_rtc_enable(pdev, 1);
  225. puv3_rtc_setfreq(&pdev->dev, 1);
  226. /* register RTC and exit */
  227. rtc = rtc_device_register("pkunity", &pdev->dev, &puv3_rtcops,
  228. THIS_MODULE);
  229. if (IS_ERR(rtc)) {
  230. dev_err(&pdev->dev, "cannot attach rtc\n");
  231. ret = PTR_ERR(rtc);
  232. goto err_nortc;
  233. }
  234. /* platform setup code should have handled this; sigh */
  235. if (!device_can_wakeup(&pdev->dev))
  236. device_init_wakeup(&pdev->dev, 1);
  237. platform_set_drvdata(pdev, rtc);
  238. return 0;
  239. err_nortc:
  240. puv3_rtc_enable(pdev, 0);
  241. release_resource(puv3_rtc_mem);
  242. err_nores:
  243. return ret;
  244. }
  245. #ifdef CONFIG_PM
  246. /* RTC Power management control */
  247. static int ticnt_save;
  248. static int puv3_rtc_suspend(struct platform_device *pdev, pm_message_t state)
  249. {
  250. /* save RTAR for anyone using periodic interrupts */
  251. ticnt_save = readl(RTC_RTAR);
  252. puv3_rtc_enable(pdev, 0);
  253. return 0;
  254. }
  255. static int puv3_rtc_resume(struct platform_device *pdev)
  256. {
  257. puv3_rtc_enable(pdev, 1);
  258. writel(ticnt_save, RTC_RTAR);
  259. return 0;
  260. }
  261. #else
  262. #define puv3_rtc_suspend NULL
  263. #define puv3_rtc_resume NULL
  264. #endif
  265. static struct platform_driver puv3_rtcdrv = {
  266. .probe = puv3_rtc_probe,
  267. .remove = __devexit_p(puv3_rtc_remove),
  268. .suspend = puv3_rtc_suspend,
  269. .resume = puv3_rtc_resume,
  270. .driver = {
  271. .name = "PKUnity-v3-RTC",
  272. .owner = THIS_MODULE,
  273. }
  274. };
  275. static char __initdata banner[] = "PKUnity-v3 RTC, (c) 2009 PKUnity Co.\n";
  276. static int __init puv3_rtc_init(void)
  277. {
  278. printk(banner);
  279. return platform_driver_register(&puv3_rtcdrv);
  280. }
  281. static void __exit puv3_rtc_exit(void)
  282. {
  283. platform_driver_unregister(&puv3_rtcdrv);
  284. }
  285. module_init(puv3_rtc_init);
  286. module_exit(puv3_rtc_exit);
  287. MODULE_DESCRIPTION("RTC Driver for the PKUnity v3 chip");
  288. MODULE_AUTHOR("Hu Dongliang");
  289. MODULE_LICENSE("GPL v2");