rtc-armada38x.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. /*
  40. * While setting the time, the RTC TIME register should not be
  41. * accessed. Setting the RTC time involves sleeping during
  42. * 100ms, so a mutex instead of a spinlock is used to protect
  43. * it
  44. */
  45. struct mutex mutex_time;
  46. int irq;
  47. };
  48. /*
  49. * According to the datasheet, the OS should wait 5us after every
  50. * register write to the RTC hard macro so that the required update
  51. * can occur without holding off the system bus
  52. */
  53. static void rtc_delayed_write(u32 val, struct armada38x_rtc *rtc, int offset)
  54. {
  55. writel(val, rtc->regs + offset);
  56. udelay(5);
  57. }
  58. static int armada38x_rtc_read_time(struct device *dev, struct rtc_time *tm)
  59. {
  60. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  61. unsigned long time, time_check;
  62. mutex_lock(&rtc->mutex_time);
  63. time = readl(rtc->regs + RTC_TIME);
  64. /*
  65. * WA for failing time set attempts. As stated in HW ERRATA if
  66. * more than one second between two time reads is detected
  67. * then read once again.
  68. */
  69. time_check = readl(rtc->regs + RTC_TIME);
  70. if ((time_check - time) > 1)
  71. time_check = readl(rtc->regs + RTC_TIME);
  72. mutex_unlock(&rtc->mutex_time);
  73. rtc_time_to_tm(time_check, tm);
  74. return 0;
  75. }
  76. static int armada38x_rtc_set_time(struct device *dev, struct rtc_time *tm)
  77. {
  78. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  79. int ret = 0;
  80. unsigned long time, flags;
  81. ret = rtc_tm_to_time(tm, &time);
  82. if (ret)
  83. goto out;
  84. /*
  85. * Setting the RTC time not always succeeds. According to the
  86. * errata we need to first write on the status register and
  87. * then wait for 100ms before writing to the time register to be
  88. * sure that the data will be taken into account.
  89. */
  90. mutex_lock(&rtc->mutex_time);
  91. rtc_delayed_write(0, rtc, RTC_STATUS);
  92. msleep(100);
  93. rtc_delayed_write(time, rtc, RTC_TIME);
  94. mutex_unlock(&rtc->mutex_time);
  95. out:
  96. return ret;
  97. }
  98. static int armada38x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  99. {
  100. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  101. unsigned long time, flags;
  102. u32 val;
  103. spin_lock_irqsave(&rtc->lock, flags);
  104. time = readl(rtc->regs + RTC_ALARM1);
  105. val = readl(rtc->regs + RTC_IRQ1_CONF) & RTC_IRQ1_AL_EN;
  106. spin_unlock_irqrestore(&rtc->lock, flags);
  107. alrm->enabled = val ? 1 : 0;
  108. rtc_time_to_tm(time, &alrm->time);
  109. return 0;
  110. }
  111. static int armada38x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  112. {
  113. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  114. unsigned long time, flags;
  115. int ret = 0;
  116. u32 val;
  117. ret = rtc_tm_to_time(&alrm->time, &time);
  118. if (ret)
  119. goto out;
  120. spin_lock_irqsave(&rtc->lock, flags);
  121. rtc_delayed_write(time, rtc, RTC_ALARM1);
  122. if (alrm->enabled) {
  123. rtc_delayed_write(RTC_IRQ1_AL_EN, rtc, RTC_IRQ1_CONF);
  124. val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
  125. writel(val | SOC_RTC_ALARM1_MASK,
  126. rtc->regs_soc + SOC_RTC_INTERRUPT);
  127. }
  128. spin_unlock_irqrestore(&rtc->lock, flags);
  129. out:
  130. return ret;
  131. }
  132. static int armada38x_rtc_alarm_irq_enable(struct device *dev,
  133. unsigned int enabled)
  134. {
  135. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  136. unsigned long flags;
  137. spin_lock_irqsave(&rtc->lock, flags);
  138. if (enabled)
  139. rtc_delayed_write(RTC_IRQ1_AL_EN, rtc, RTC_IRQ1_CONF);
  140. else
  141. rtc_delayed_write(0, rtc, RTC_IRQ1_CONF);
  142. spin_unlock_irqrestore(&rtc->lock, flags);
  143. return 0;
  144. }
  145. static irqreturn_t armada38x_rtc_alarm_irq(int irq, void *data)
  146. {
  147. struct armada38x_rtc *rtc = data;
  148. u32 val;
  149. int event = RTC_IRQF | RTC_AF;
  150. dev_dbg(&rtc->rtc_dev->dev, "%s:irq(%d)\n", __func__, irq);
  151. spin_lock(&rtc->lock);
  152. val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
  153. writel(val & ~SOC_RTC_ALARM1, rtc->regs_soc + SOC_RTC_INTERRUPT);
  154. val = readl(rtc->regs + RTC_IRQ1_CONF);
  155. /* disable all the interrupts for alarm 1 */
  156. rtc_delayed_write(0, rtc, RTC_IRQ1_CONF);
  157. /* Ack the event */
  158. rtc_delayed_write(RTC_STATUS_ALARM1, rtc, RTC_STATUS);
  159. spin_unlock(&rtc->lock);
  160. if (val & RTC_IRQ1_FREQ_EN) {
  161. if (val & RTC_IRQ1_FREQ_1HZ)
  162. event |= RTC_UF;
  163. else
  164. event |= RTC_PF;
  165. }
  166. rtc_update_irq(rtc->rtc_dev, 1, event);
  167. return IRQ_HANDLED;
  168. }
  169. static struct rtc_class_ops armada38x_rtc_ops = {
  170. .read_time = armada38x_rtc_read_time,
  171. .set_time = armada38x_rtc_set_time,
  172. .read_alarm = armada38x_rtc_read_alarm,
  173. .set_alarm = armada38x_rtc_set_alarm,
  174. .alarm_irq_enable = armada38x_rtc_alarm_irq_enable,
  175. };
  176. static __init int armada38x_rtc_probe(struct platform_device *pdev)
  177. {
  178. struct resource *res;
  179. struct armada38x_rtc *rtc;
  180. int ret;
  181. rtc = devm_kzalloc(&pdev->dev, sizeof(struct armada38x_rtc),
  182. GFP_KERNEL);
  183. if (!rtc)
  184. return -ENOMEM;
  185. spin_lock_init(&rtc->lock);
  186. mutex_init(&rtc->mutex_time);
  187. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc");
  188. rtc->regs = devm_ioremap_resource(&pdev->dev, res);
  189. if (IS_ERR(rtc->regs))
  190. return PTR_ERR(rtc->regs);
  191. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc-soc");
  192. rtc->regs_soc = devm_ioremap_resource(&pdev->dev, res);
  193. if (IS_ERR(rtc->regs_soc))
  194. return PTR_ERR(rtc->regs_soc);
  195. rtc->irq = platform_get_irq(pdev, 0);
  196. if (rtc->irq < 0) {
  197. dev_err(&pdev->dev, "no irq\n");
  198. return rtc->irq;
  199. }
  200. if (devm_request_irq(&pdev->dev, rtc->irq, armada38x_rtc_alarm_irq,
  201. 0, pdev->name, rtc) < 0) {
  202. dev_warn(&pdev->dev, "Interrupt not available.\n");
  203. rtc->irq = -1;
  204. /*
  205. * If there is no interrupt available then we can't
  206. * use the alarm
  207. */
  208. armada38x_rtc_ops.set_alarm = NULL;
  209. armada38x_rtc_ops.alarm_irq_enable = NULL;
  210. }
  211. platform_set_drvdata(pdev, rtc);
  212. if (rtc->irq != -1)
  213. device_init_wakeup(&pdev->dev, 1);
  214. rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, pdev->name,
  215. &armada38x_rtc_ops, THIS_MODULE);
  216. if (IS_ERR(rtc->rtc_dev)) {
  217. ret = PTR_ERR(rtc->rtc_dev);
  218. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  219. return ret;
  220. }
  221. return 0;
  222. }
  223. #ifdef CONFIG_PM_SLEEP
  224. static int armada38x_rtc_suspend(struct device *dev)
  225. {
  226. if (device_may_wakeup(dev)) {
  227. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  228. return enable_irq_wake(rtc->irq);
  229. }
  230. return 0;
  231. }
  232. static int armada38x_rtc_resume(struct device *dev)
  233. {
  234. if (device_may_wakeup(dev)) {
  235. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  236. return disable_irq_wake(rtc->irq);
  237. }
  238. return 0;
  239. }
  240. #endif
  241. static SIMPLE_DEV_PM_OPS(armada38x_rtc_pm_ops,
  242. armada38x_rtc_suspend, armada38x_rtc_resume);
  243. #ifdef CONFIG_OF
  244. static const struct of_device_id armada38x_rtc_of_match_table[] = {
  245. { .compatible = "marvell,armada-380-rtc", },
  246. {}
  247. };
  248. #endif
  249. static struct platform_driver armada38x_rtc_driver = {
  250. .driver = {
  251. .name = "armada38x-rtc",
  252. .pm = &armada38x_rtc_pm_ops,
  253. .of_match_table = of_match_ptr(armada38x_rtc_of_match_table),
  254. },
  255. };
  256. module_platform_driver_probe(armada38x_rtc_driver, armada38x_rtc_probe);
  257. MODULE_DESCRIPTION("Marvell Armada 38x RTC driver");
  258. MODULE_AUTHOR("Gregory CLEMENT <gregory.clement@free-electrons.com>");
  259. MODULE_LICENSE("GPL");