rtc-armada38x.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * RTC driver for the Armada 38x Marvell SoCs
  3. *
  4. * Copyright (C) 2015 Marvell
  5. *
  6. * Gregory Clement <gregory.clement@free-electrons.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of the
  11. * License, or (at your option) any later version.
  12. *
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/rtc.h>
  20. #define RTC_STATUS 0x0
  21. #define RTC_STATUS_ALARM1 BIT(0)
  22. #define RTC_STATUS_ALARM2 BIT(1)
  23. #define RTC_IRQ1_CONF 0x4
  24. #define RTC_IRQ1_AL_EN BIT(0)
  25. #define RTC_IRQ1_FREQ_EN BIT(1)
  26. #define RTC_IRQ1_FREQ_1HZ BIT(2)
  27. #define RTC_TIME 0xC
  28. #define RTC_ALARM1 0x10
  29. #define SOC_RTC_INTERRUPT 0x8
  30. #define SOC_RTC_ALARM1 BIT(0)
  31. #define SOC_RTC_ALARM2 BIT(1)
  32. #define SOC_RTC_ALARM1_MASK BIT(2)
  33. #define SOC_RTC_ALARM2_MASK BIT(3)
  34. struct armada38x_rtc {
  35. struct rtc_device *rtc_dev;
  36. void __iomem *regs;
  37. void __iomem *regs_soc;
  38. spinlock_t lock;
  39. int irq;
  40. };
  41. /*
  42. * According to the datasheet, the OS should wait 5us after every
  43. * register write to the RTC hard macro so that the required update
  44. * can occur without holding off the system bus
  45. */
  46. static void rtc_delayed_write(u32 val, struct armada38x_rtc *rtc, int offset)
  47. {
  48. writel(val, rtc->regs + offset);
  49. udelay(5);
  50. }
  51. static int armada38x_rtc_read_time(struct device *dev, struct rtc_time *tm)
  52. {
  53. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  54. unsigned long time, time_check, flags;
  55. spin_lock_irqsave(&rtc->lock, flags);
  56. time = readl(rtc->regs + RTC_TIME);
  57. /*
  58. * WA for failing time set attempts. As stated in HW ERRATA if
  59. * more than one second between two time reads is detected
  60. * then read once again.
  61. */
  62. time_check = readl(rtc->regs + RTC_TIME);
  63. if ((time_check - time) > 1)
  64. time_check = readl(rtc->regs + RTC_TIME);
  65. spin_unlock_irqrestore(&rtc->lock, flags);
  66. rtc_time_to_tm(time_check, tm);
  67. return 0;
  68. }
  69. static int armada38x_rtc_set_time(struct device *dev, struct rtc_time *tm)
  70. {
  71. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  72. int ret = 0;
  73. unsigned long time, flags;
  74. ret = rtc_tm_to_time(tm, &time);
  75. if (ret)
  76. goto out;
  77. /*
  78. * According to errata FE-3124064, Write to RTC TIME register
  79. * may fail. As a workaround, after writing to RTC TIME
  80. * register, issue a dummy write of 0x0 twice to RTC Status
  81. * register.
  82. */
  83. spin_lock_irqsave(&rtc->lock, flags);
  84. rtc_delayed_write(time, rtc, RTC_TIME);
  85. rtc_delayed_write(0, rtc, RTC_STATUS);
  86. rtc_delayed_write(0, rtc, RTC_STATUS);
  87. spin_unlock_irqrestore(&rtc->lock, flags);
  88. out:
  89. return ret;
  90. }
  91. static int armada38x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  92. {
  93. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  94. unsigned long time, flags;
  95. u32 val;
  96. spin_lock_irqsave(&rtc->lock, flags);
  97. time = readl(rtc->regs + RTC_ALARM1);
  98. val = readl(rtc->regs + RTC_IRQ1_CONF) & RTC_IRQ1_AL_EN;
  99. spin_unlock_irqrestore(&rtc->lock, flags);
  100. alrm->enabled = val ? 1 : 0;
  101. rtc_time_to_tm(time, &alrm->time);
  102. return 0;
  103. }
  104. static int armada38x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  105. {
  106. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  107. unsigned long time, flags;
  108. int ret = 0;
  109. u32 val;
  110. ret = rtc_tm_to_time(&alrm->time, &time);
  111. if (ret)
  112. goto out;
  113. spin_lock_irqsave(&rtc->lock, flags);
  114. rtc_delayed_write(time, rtc, RTC_ALARM1);
  115. if (alrm->enabled) {
  116. rtc_delayed_write(RTC_IRQ1_AL_EN, rtc, RTC_IRQ1_CONF);
  117. val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
  118. writel(val | SOC_RTC_ALARM1_MASK,
  119. rtc->regs_soc + SOC_RTC_INTERRUPT);
  120. }
  121. spin_unlock_irqrestore(&rtc->lock, flags);
  122. out:
  123. return ret;
  124. }
  125. static int armada38x_rtc_alarm_irq_enable(struct device *dev,
  126. unsigned int enabled)
  127. {
  128. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  129. unsigned long flags;
  130. spin_lock_irqsave(&rtc->lock, flags);
  131. if (enabled)
  132. rtc_delayed_write(RTC_IRQ1_AL_EN, rtc, RTC_IRQ1_CONF);
  133. else
  134. rtc_delayed_write(0, rtc, RTC_IRQ1_CONF);
  135. spin_unlock_irqrestore(&rtc->lock, flags);
  136. return 0;
  137. }
  138. static irqreturn_t armada38x_rtc_alarm_irq(int irq, void *data)
  139. {
  140. struct armada38x_rtc *rtc = data;
  141. u32 val;
  142. int event = RTC_IRQF | RTC_AF;
  143. dev_dbg(&rtc->rtc_dev->dev, "%s:irq(%d)\n", __func__, irq);
  144. spin_lock(&rtc->lock);
  145. val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
  146. writel(val & ~SOC_RTC_ALARM1, rtc->regs_soc + SOC_RTC_INTERRUPT);
  147. val = readl(rtc->regs + RTC_IRQ1_CONF);
  148. /* disable all the interrupts for alarm 1 */
  149. rtc_delayed_write(0, rtc, RTC_IRQ1_CONF);
  150. /* Ack the event */
  151. rtc_delayed_write(RTC_STATUS_ALARM1, rtc, RTC_STATUS);
  152. spin_unlock(&rtc->lock);
  153. if (val & RTC_IRQ1_FREQ_EN) {
  154. if (val & RTC_IRQ1_FREQ_1HZ)
  155. event |= RTC_UF;
  156. else
  157. event |= RTC_PF;
  158. }
  159. rtc_update_irq(rtc->rtc_dev, 1, event);
  160. return IRQ_HANDLED;
  161. }
  162. static struct rtc_class_ops armada38x_rtc_ops = {
  163. .read_time = armada38x_rtc_read_time,
  164. .set_time = armada38x_rtc_set_time,
  165. .read_alarm = armada38x_rtc_read_alarm,
  166. .set_alarm = armada38x_rtc_set_alarm,
  167. .alarm_irq_enable = armada38x_rtc_alarm_irq_enable,
  168. };
  169. static __init int armada38x_rtc_probe(struct platform_device *pdev)
  170. {
  171. struct resource *res;
  172. struct armada38x_rtc *rtc;
  173. int ret;
  174. rtc = devm_kzalloc(&pdev->dev, sizeof(struct armada38x_rtc),
  175. GFP_KERNEL);
  176. if (!rtc)
  177. return -ENOMEM;
  178. spin_lock_init(&rtc->lock);
  179. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc");
  180. rtc->regs = devm_ioremap_resource(&pdev->dev, res);
  181. if (IS_ERR(rtc->regs))
  182. return PTR_ERR(rtc->regs);
  183. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc-soc");
  184. rtc->regs_soc = devm_ioremap_resource(&pdev->dev, res);
  185. if (IS_ERR(rtc->regs_soc))
  186. return PTR_ERR(rtc->regs_soc);
  187. rtc->irq = platform_get_irq(pdev, 0);
  188. if (rtc->irq < 0) {
  189. dev_err(&pdev->dev, "no irq\n");
  190. return rtc->irq;
  191. }
  192. if (devm_request_irq(&pdev->dev, rtc->irq, armada38x_rtc_alarm_irq,
  193. 0, pdev->name, rtc) < 0) {
  194. dev_warn(&pdev->dev, "Interrupt not available.\n");
  195. rtc->irq = -1;
  196. /*
  197. * If there is no interrupt available then we can't
  198. * use the alarm
  199. */
  200. armada38x_rtc_ops.set_alarm = NULL;
  201. armada38x_rtc_ops.alarm_irq_enable = NULL;
  202. }
  203. platform_set_drvdata(pdev, rtc);
  204. if (rtc->irq != -1)
  205. device_init_wakeup(&pdev->dev, 1);
  206. rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, pdev->name,
  207. &armada38x_rtc_ops, THIS_MODULE);
  208. if (IS_ERR(rtc->rtc_dev)) {
  209. ret = PTR_ERR(rtc->rtc_dev);
  210. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  211. return ret;
  212. }
  213. return 0;
  214. }
  215. #ifdef CONFIG_PM_SLEEP
  216. static int armada38x_rtc_suspend(struct device *dev)
  217. {
  218. if (device_may_wakeup(dev)) {
  219. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  220. return enable_irq_wake(rtc->irq);
  221. }
  222. return 0;
  223. }
  224. static int armada38x_rtc_resume(struct device *dev)
  225. {
  226. if (device_may_wakeup(dev)) {
  227. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  228. return disable_irq_wake(rtc->irq);
  229. }
  230. return 0;
  231. }
  232. #endif
  233. static SIMPLE_DEV_PM_OPS(armada38x_rtc_pm_ops,
  234. armada38x_rtc_suspend, armada38x_rtc_resume);
  235. #ifdef CONFIG_OF
  236. static const struct of_device_id armada38x_rtc_of_match_table[] = {
  237. { .compatible = "marvell,armada-380-rtc", },
  238. {}
  239. };
  240. MODULE_DEVICE_TABLE(of, armada38x_rtc_of_match_table);
  241. #endif
  242. static struct platform_driver armada38x_rtc_driver = {
  243. .driver = {
  244. .name = "armada38x-rtc",
  245. .pm = &armada38x_rtc_pm_ops,
  246. .of_match_table = of_match_ptr(armada38x_rtc_of_match_table),
  247. },
  248. };
  249. module_platform_driver_probe(armada38x_rtc_driver, armada38x_rtc_probe);
  250. MODULE_DESCRIPTION("Marvell Armada 38x RTC driver");
  251. MODULE_AUTHOR("Gregory CLEMENT <gregory.clement@free-electrons.com>");
  252. MODULE_LICENSE("GPL");