rtc-puv3.c 7.7 KB

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