rtc-phytium.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Phytium Real Time Clock Driver
  3. *
  4. * Copyright (c) 2019, Phytium Technology Co., Ltd.
  5. *
  6. * Chen Baozi <chenbaozi@phytium.com.cn>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/io.h>
  19. #include <linux/slab.h>
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/rtc.h>
  23. #include <linux/acpi.h>
  24. #define RTC_CMR 0x04
  25. #define RTC_AES_SEL 0x08
  26. #define RTC_AES_SEL_COUNTER 0x100
  27. #define RTC_CCR 0x0C
  28. #define RTC_CCR_IE BIT(0)
  29. #define RTC_CCR_MASK BIT(1)
  30. #define RTC_CCR_EN BIT(2)
  31. #define RTC_CCR_WEN BIT(3)
  32. #define RTC_STAT 0x10
  33. #define RTC_STAT_BIT BIT(0)
  34. #define RTC_RSTAT 0x14
  35. #define RTC_EOI 0x18
  36. #define RTC_VER 0x1C
  37. #define RTC_CDR_LOW 0x20
  38. #define RTC_CCVR 0x24
  39. #define RTC_CLR_LOW 0x28
  40. #define RTC_CLR 0x2c
  41. #define RTC_COUNTER_HB_OFFSET 15
  42. #define RTC_COUNTER_LB_MASK 0x7fff
  43. spinlock_t spinlock_phytium_rtc;
  44. struct phytium_rtc_dev {
  45. struct rtc_device *rtc;
  46. struct device *dev;
  47. unsigned long alarm_time;
  48. void __iomem *csr_base;
  49. struct clk *clk;
  50. unsigned int irq_wake;
  51. unsigned int irq_enabled;
  52. };
  53. static int phytium_rtc_read_time(struct device *dev, struct rtc_time *tm)
  54. {
  55. struct phytium_rtc_dev *pdata = dev_get_drvdata(dev);
  56. unsigned long counter = 0;
  57. unsigned long tmp = 0;
  58. spin_lock(&spinlock_phytium_rtc);
  59. writel(RTC_AES_SEL_COUNTER, pdata->csr_base + RTC_AES_SEL);
  60. counter = readl(pdata->csr_base + RTC_CCVR);
  61. tmp = readl(pdata->csr_base + RTC_CDR_LOW);
  62. printk("%s_%d:counter:0x%lx\n", __func__, __LINE__, counter);
  63. spin_unlock(&spinlock_phytium_rtc);
  64. rtc_time_to_tm(counter, tm);
  65. return rtc_valid_tm(tm);
  66. }
  67. static int phytium_rtc_set_mmss(struct device *dev, unsigned long secs)
  68. {
  69. struct phytium_rtc_dev *pdata = dev_get_drvdata(dev);
  70. unsigned long counter = 0;
  71. unsigned long tmp = 0;
  72. spin_lock(&spinlock_phytium_rtc);
  73. writel(RTC_AES_SEL_COUNTER, pdata->csr_base + RTC_AES_SEL);
  74. writel(0x00000000, pdata->csr_base + RTC_CLR_LOW);
  75. writel((u32)secs, pdata->csr_base + RTC_CLR);
  76. writel(RTC_AES_SEL_COUNTER, pdata->csr_base + RTC_AES_SEL);
  77. counter = readl(pdata->csr_base + RTC_CLR);
  78. tmp = readl(pdata->csr_base + RTC_CLR_LOW);
  79. spin_unlock(&spinlock_phytium_rtc);
  80. return 0;
  81. }
  82. static int phytium_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  83. {
  84. struct phytium_rtc_dev *pdata = dev_get_drvdata(dev);
  85. rtc_time_to_tm(pdata->alarm_time, &alrm->time);
  86. alrm->enabled = readl(pdata->csr_base + RTC_CCR) & RTC_CCR_IE;
  87. return 0;
  88. }
  89. static int phytium_rtc_alarm_irq_enable(struct device *dev, u32 enabled)
  90. {
  91. struct phytium_rtc_dev *pdata = dev_get_drvdata(dev);
  92. u32 ccr;
  93. ccr = readl(pdata->csr_base + RTC_CCR);
  94. if (enabled) {
  95. ccr &= ~RTC_CCR_MASK;
  96. ccr |= RTC_CCR_IE;
  97. } else {
  98. ccr &= ~RTC_CCR_IE;
  99. ccr |= RTC_CCR_MASK;
  100. }
  101. writel(ccr, pdata->csr_base + RTC_CCR);
  102. return 0;
  103. }
  104. static int phytium_rtc_alarm_irq_enabled(struct device *dev)
  105. {
  106. struct phytium_rtc_dev *pdata = dev_get_drvdata(dev);
  107. return readl(pdata->csr_base + RTC_CCR) & RTC_CCR_IE ? 1: 0;
  108. }
  109. static int phytium_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  110. {
  111. struct phytium_rtc_dev *pdata = dev_get_drvdata(dev);
  112. unsigned long rtc_time;
  113. unsigned long alarm_time;
  114. rtc_time = readl(pdata->csr_base + RTC_CCVR);
  115. rtc_tm_to_time(&alrm->time, &alarm_time);
  116. pdata->alarm_time = alarm_time;
  117. writel((u32) pdata->alarm_time, pdata->csr_base + RTC_CMR);
  118. phytium_rtc_alarm_irq_enable(dev, alrm->enabled);
  119. return 0;
  120. }
  121. static const struct rtc_class_ops phytium_rtc_ops = {
  122. .read_time = phytium_rtc_read_time,
  123. .set_mmss = phytium_rtc_set_mmss,
  124. .read_alarm = phytium_rtc_read_alarm,
  125. .set_alarm = phytium_rtc_set_alarm,
  126. .alarm_irq_enable = phytium_rtc_alarm_irq_enable,
  127. };
  128. static irqreturn_t phytium_rtc_interrupt(int irq, void *id)
  129. {
  130. struct phytium_rtc_dev *pdata = (struct phytium_rtc_dev *) id;
  131. /* Check if interrupt asserted */
  132. if (!(readl(pdata->csr_base + RTC_STAT) & RTC_STAT_BIT))
  133. return IRQ_NONE;
  134. /* Clear interrupt */
  135. readl(pdata->csr_base + RTC_EOI);
  136. rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF);
  137. return IRQ_HANDLED;
  138. }
  139. static int phytium_rtc_probe(struct platform_device *pdev)
  140. {
  141. struct phytium_rtc_dev *pdata;
  142. struct resource *res;
  143. int ret;
  144. int irq;
  145. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  146. if (!pdata)
  147. return -ENOMEM;
  148. platform_set_drvdata(pdev, pdata);
  149. pdata->dev = &pdev->dev;
  150. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  151. pdata->csr_base = devm_ioremap_resource(&pdev->dev, res);
  152. if (IS_ERR(pdata->csr_base))
  153. return PTR_ERR(pdata->csr_base);
  154. irq = platform_get_irq(pdev, 0);
  155. if (irq < 0) {
  156. dev_err(&pdev->dev, "No IRQ resource\n");
  157. return irq;
  158. }
  159. ret = devm_request_irq(&pdev->dev, irq, phytium_rtc_interrupt, 0,
  160. dev_name(&pdev->dev), pdata);
  161. if (ret) {
  162. dev_err(&pdev->dev, "Could not request IRQ\n");
  163. return ret;
  164. }
  165. #ifndef CONFIG_ACPI
  166. pdata->clk = devm_clk_get(&pdev->dev, NULL);
  167. if (IS_ERR(pdata->clk)) {
  168. dev_err(&pdev->dev, "Couldn't get the clock for RTC\n");
  169. return -ENODEV;
  170. }
  171. ret = clk_prepare_enable(pdata->clk);
  172. if (ret)
  173. return ret;
  174. #endif
  175. spin_lock_init(&spinlock_phytium_rtc);
  176. /* Turn on the clock and the crystal */
  177. writel(RTC_CCR_EN, pdata->csr_base + RTC_CCR);
  178. ret = device_init_wakeup(&pdev->dev, 1);
  179. if (ret) {
  180. clk_disable_unprepare(pdata->clk);
  181. return ret;
  182. }
  183. pdata->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  184. &phytium_rtc_ops, THIS_MODULE);
  185. if (IS_ERR(pdata->rtc)) {
  186. clk_disable_unprepare(pdata->clk);
  187. return PTR_ERR(pdata->rtc);
  188. }
  189. /* HW does not support update faster than 1 seconds */
  190. pdata->rtc->uie_unsupported = 1;
  191. return 0;
  192. }
  193. static int phytium_rtc_remove(struct platform_device *pdev)
  194. {
  195. struct phytium_rtc_dev *pdata = platform_get_drvdata(pdev);
  196. phytium_rtc_alarm_irq_enable(&pdev->dev, 0);
  197. device_init_wakeup(&pdev->dev, 0);
  198. clk_disable_unprepare(pdata->clk);
  199. return 0;
  200. }
  201. #ifdef CONFIG_PM_SLEEP
  202. static int phytium_rtc_suspend(struct device *dev)
  203. {
  204. struct platform_device *pdev = to_platform_device(dev);
  205. struct phytium_rtc_dev *pdata = platform_get_drvdata(pdev);
  206. int irq;
  207. /*
  208. * If this RTC alarm will be used for waking the system up,
  209. * don't disable it of course. Else we just disable the alarm
  210. * and await suspension.
  211. */
  212. irq = platform_get_irq(pdev, 0);
  213. if (device_may_wakeup(&pdev->dev)) {
  214. if (!enable_irq_wake(irq))
  215. pdata->irq_wake = 1;
  216. } else {
  217. pdata->irq_enabled = phytium_rtc_alarm_irq_enabled(dev);
  218. phytium_rtc_alarm_irq_enable(dev, 0);
  219. clk_disable_unprepare(pdata->clk);
  220. }
  221. return 0;
  222. }
  223. static int phytium_rtc_resume(struct device *dev)
  224. {
  225. struct platform_device *pdev = to_platform_device(dev);
  226. struct phytium_rtc_dev *pdata = platform_get_drvdata(pdev);
  227. int irq;
  228. int rc;
  229. irq = platform_get_irq(pdev, 0);
  230. if (device_may_wakeup(&pdev->dev)) {
  231. if (pdata->irq_wake) {
  232. disable_irq_wake(irq);
  233. pdata->irq_wake = 0;
  234. }
  235. } else {
  236. rc = clk_prepare_enable(pdata->clk);
  237. if (rc) {
  238. dev_err(dev, "Unable to enable clock error %d\n", rc);
  239. return rc;
  240. }
  241. phytium_rtc_alarm_irq_enable(dev, pdata->irq_enabled);
  242. }
  243. return 0;
  244. }
  245. #endif
  246. static SIMPLE_DEV_PM_OPS(phytium_rtc_pm_ops, phytium_rtc_suspend, phytium_rtc_resume);
  247. #ifdef CONFIG_OF
  248. static const struct of_device_id phytium_rtc_of_match[] = {
  249. { .compatible = "phytium,rtc" },
  250. { }
  251. };
  252. MODULE_DEVICE_TABLE(of, phytium_rtc_of_match);
  253. #endif
  254. #ifdef CONFIG_ACPI
  255. static const struct acpi_device_id phytium_rtc_acpi_match[] = {
  256. { "PHYT0002", 0 },
  257. { }
  258. };
  259. #endif
  260. static struct platform_driver phytium_rtc_driver = {
  261. .probe = phytium_rtc_probe,
  262. .remove = phytium_rtc_remove,
  263. .driver = {
  264. .name = "phytium-rtc",
  265. .pm = &phytium_rtc_pm_ops,
  266. .of_match_table = of_match_ptr(phytium_rtc_of_match),
  267. .acpi_match_table = ACPI_PTR(phytium_rtc_acpi_match),
  268. },
  269. };
  270. module_platform_driver(phytium_rtc_driver);
  271. MODULE_DESCRIPTION("Phytium RTC driver");
  272. MODULE_AUTHOR("Chen Baozi <chenbaozi@phytium.com.cn>");
  273. MODULE_LICENSE("GPL");