600-armada_38x_rtc.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. --- /dev/null
  2. +++ b/Documentation/devicetree/bindings/rtc/armada-380-rtc.txt
  3. @@ -0,0 +1,22 @@
  4. +* Real Time Clock of the Armada 38x SoCs
  5. +
  6. +RTC controller for the Armada 38x SoCs
  7. +
  8. +Required properties:
  9. +- compatible : Should be "marvell,armada-380-rtc"
  10. +- reg: a list of base address and size pairs, one for each entry in
  11. + reg-names
  12. +- reg names: should contain:
  13. + * "rtc" for the RTC registers
  14. + * "rtc-soc" for the SoC related registers and among them the one
  15. + related to the interrupt.
  16. +- interrupts: IRQ line for the RTC.
  17. +
  18. +Example:
  19. +
  20. +rtc@a3800 {
  21. + compatible = "marvell,armada-380-rtc";
  22. + reg = <0xa3800 0x20>, <0x184a0 0x0c>;
  23. + reg-names = "rtc", "rtc-soc";
  24. + interrupts = <GIC_SPI 21 IRQ_TYPE_LEVEL_HIGH>;
  25. +};
  26. --- a/drivers/rtc/Kconfig
  27. +++ b/drivers/rtc/Kconfig
  28. @@ -1262,6 +1262,16 @@ config RTC_DRV_MV
  29. This driver can also be built as a module. If so, the module
  30. will be called rtc-mv.
  31. +config RTC_DRV_ARMADA38X
  32. + tristate "Armada 38x Marvell SoC RTC"
  33. + depends on ARCH_MVEBU
  34. + help
  35. + If you say yes here you will get support for the in-chip RTC
  36. + that can be found in the Armada 38x Marvell's SoC device
  37. +
  38. + This driver can also be built as a module. If so, the module
  39. + will be called armada38x-rtc.
  40. +
  41. config RTC_DRV_PS3
  42. tristate "PS3 RTC"
  43. depends on PPC_PS3
  44. --- a/drivers/rtc/Makefile
  45. +++ b/drivers/rtc/Makefile
  46. @@ -24,6 +24,7 @@ obj-$(CONFIG_RTC_DRV_88PM860X) += rtc-8
  47. obj-$(CONFIG_RTC_DRV_88PM80X) += rtc-88pm80x.o
  48. obj-$(CONFIG_RTC_DRV_AB3100) += rtc-ab3100.o
  49. obj-$(CONFIG_RTC_DRV_AB8500) += rtc-ab8500.o
  50. +obj-$(CONFIG_RTC_DRV_ARMADA38X) += rtc-armada38x.o
  51. obj-$(CONFIG_RTC_DRV_AS3722) += rtc-as3722.o
  52. obj-$(CONFIG_RTC_DRV_AT32AP700X)+= rtc-at32ap700x.o
  53. obj-$(CONFIG_RTC_DRV_AT91RM9200)+= rtc-at91rm9200.o
  54. --- /dev/null
  55. +++ b/drivers/rtc/rtc-armada38x.c
  56. @@ -0,0 +1,320 @@
  57. +/*
  58. + * RTC driver for the Armada 38x Marvell SoCs
  59. + *
  60. + * Copyright (C) 2015 Marvell
  61. + *
  62. + * Gregory Clement <gregory.clement@free-electrons.com>
  63. + *
  64. + * This program is free software; you can redistribute it and/or
  65. + * modify it under the terms of the GNU General Public License as
  66. + * published by the Free Software Foundation; either version 2 of the
  67. + * License, or (at your option) any later version.
  68. + *
  69. + */
  70. +
  71. +#include <linux/delay.h>
  72. +#include <linux/io.h>
  73. +#include <linux/module.h>
  74. +#include <linux/of.h>
  75. +#include <linux/platform_device.h>
  76. +#include <linux/rtc.h>
  77. +
  78. +#define RTC_STATUS 0x0
  79. +#define RTC_STATUS_ALARM1 BIT(0)
  80. +#define RTC_STATUS_ALARM2 BIT(1)
  81. +#define RTC_IRQ1_CONF 0x4
  82. +#define RTC_IRQ1_AL_EN BIT(0)
  83. +#define RTC_IRQ1_FREQ_EN BIT(1)
  84. +#define RTC_IRQ1_FREQ_1HZ BIT(2)
  85. +#define RTC_TIME 0xC
  86. +#define RTC_ALARM1 0x10
  87. +
  88. +#define SOC_RTC_INTERRUPT 0x8
  89. +#define SOC_RTC_ALARM1 BIT(0)
  90. +#define SOC_RTC_ALARM2 BIT(1)
  91. +#define SOC_RTC_ALARM1_MASK BIT(2)
  92. +#define SOC_RTC_ALARM2_MASK BIT(3)
  93. +
  94. +struct armada38x_rtc {
  95. + struct rtc_device *rtc_dev;
  96. + void __iomem *regs;
  97. + void __iomem *regs_soc;
  98. + spinlock_t lock;
  99. + int irq;
  100. +};
  101. +
  102. +/*
  103. + * According to the datasheet, the OS should wait 5us after every
  104. + * register write to the RTC hard macro so that the required update
  105. + * can occur without holding off the system bus
  106. + */
  107. +static void rtc_delayed_write(u32 val, struct armada38x_rtc *rtc, int offset)
  108. +{
  109. + writel(val, rtc->regs + offset);
  110. + udelay(5);
  111. +}
  112. +
  113. +static int armada38x_rtc_read_time(struct device *dev, struct rtc_time *tm)
  114. +{
  115. + struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  116. + unsigned long time, time_check, flags;
  117. +
  118. + spin_lock_irqsave(&rtc->lock, flags);
  119. +
  120. + time = readl(rtc->regs + RTC_TIME);
  121. + /*
  122. + * WA for failing time set attempts. As stated in HW ERRATA if
  123. + * more than one second between two time reads is detected
  124. + * then read once again.
  125. + */
  126. + time_check = readl(rtc->regs + RTC_TIME);
  127. + if ((time_check - time) > 1)
  128. + time_check = readl(rtc->regs + RTC_TIME);
  129. +
  130. + spin_unlock_irqrestore(&rtc->lock, flags);
  131. +
  132. + rtc_time_to_tm(time_check, tm);
  133. +
  134. + return 0;
  135. +}
  136. +
  137. +static int armada38x_rtc_set_time(struct device *dev, struct rtc_time *tm)
  138. +{
  139. + struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  140. + int ret = 0;
  141. + unsigned long time, flags;
  142. +
  143. + ret = rtc_tm_to_time(tm, &time);
  144. +
  145. + if (ret)
  146. + goto out;
  147. + /*
  148. + * Setting the RTC time not always succeeds. According to the
  149. + * errata we need to first write on the status register and
  150. + * then wait for 100ms before writing to the time register to be
  151. + * sure that the data will be taken into account.
  152. + */
  153. + spin_lock_irqsave(&rtc->lock, flags);
  154. +
  155. + rtc_delayed_write(0, rtc, RTC_STATUS);
  156. +
  157. + spin_unlock_irqrestore(&rtc->lock, flags);
  158. +
  159. + msleep(100);
  160. +
  161. + spin_lock_irqsave(&rtc->lock, flags);
  162. +
  163. + rtc_delayed_write(time, rtc, RTC_TIME);
  164. +
  165. + spin_unlock_irqrestore(&rtc->lock, flags);
  166. +out:
  167. + return ret;
  168. +}
  169. +
  170. +static int armada38x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  171. +{
  172. + struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  173. + unsigned long time, flags;
  174. + u32 val;
  175. +
  176. + spin_lock_irqsave(&rtc->lock, flags);
  177. +
  178. + time = readl(rtc->regs + RTC_ALARM1);
  179. + val = readl(rtc->regs + RTC_IRQ1_CONF) & RTC_IRQ1_AL_EN;
  180. +
  181. + spin_unlock_irqrestore(&rtc->lock, flags);
  182. +
  183. + alrm->enabled = val ? 1 : 0;
  184. + rtc_time_to_tm(time, &alrm->time);
  185. +
  186. + return 0;
  187. +}
  188. +
  189. +static int armada38x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  190. +{
  191. + struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  192. + unsigned long time, flags;
  193. + int ret = 0;
  194. + u32 val;
  195. +
  196. + ret = rtc_tm_to_time(&alrm->time, &time);
  197. +
  198. + if (ret)
  199. + goto out;
  200. +
  201. + spin_lock_irqsave(&rtc->lock, flags);
  202. +
  203. + rtc_delayed_write(time, rtc, RTC_ALARM1);
  204. +
  205. + if (alrm->enabled) {
  206. + rtc_delayed_write(RTC_IRQ1_AL_EN, rtc, RTC_IRQ1_CONF);
  207. + val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
  208. + writel(val | SOC_RTC_ALARM1_MASK,
  209. + rtc->regs_soc + SOC_RTC_INTERRUPT);
  210. + }
  211. +
  212. + spin_unlock_irqrestore(&rtc->lock, flags);
  213. +
  214. +out:
  215. + return ret;
  216. +}
  217. +
  218. +static int armada38x_rtc_alarm_irq_enable(struct device *dev,
  219. + unsigned int enabled)
  220. +{
  221. + struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  222. + unsigned long flags;
  223. +
  224. + spin_lock_irqsave(&rtc->lock, flags);
  225. +
  226. + if (enabled)
  227. + rtc_delayed_write(RTC_IRQ1_AL_EN, rtc, RTC_IRQ1_CONF);
  228. + else
  229. + rtc_delayed_write(0, rtc, RTC_IRQ1_CONF);
  230. +
  231. + spin_unlock_irqrestore(&rtc->lock, flags);
  232. +
  233. + return 0;
  234. +}
  235. +
  236. +static irqreturn_t armada38x_rtc_alarm_irq(int irq, void *data)
  237. +{
  238. + struct armada38x_rtc *rtc = data;
  239. + u32 val;
  240. + int event = RTC_IRQF | RTC_AF;
  241. +
  242. + dev_dbg(&rtc->rtc_dev->dev, "%s:irq(%d)\n", __func__, irq);
  243. +
  244. + spin_lock(&rtc->lock);
  245. +
  246. + val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
  247. +
  248. + writel(val & ~SOC_RTC_ALARM1, rtc->regs_soc + SOC_RTC_INTERRUPT);
  249. + val = readl(rtc->regs + RTC_IRQ1_CONF);
  250. + /* disable all the interrupts for alarm 1 */
  251. + rtc_delayed_write(0, rtc, RTC_IRQ1_CONF);
  252. + /* Ack the event */
  253. + rtc_delayed_write(RTC_STATUS_ALARM1, rtc, RTC_STATUS);
  254. +
  255. + spin_unlock(&rtc->lock);
  256. +
  257. + if (val & RTC_IRQ1_FREQ_EN) {
  258. + if (val & RTC_IRQ1_FREQ_1HZ)
  259. + event |= RTC_UF;
  260. + else
  261. + event |= RTC_PF;
  262. + }
  263. +
  264. + rtc_update_irq(rtc->rtc_dev, 1, event);
  265. +
  266. + return IRQ_HANDLED;
  267. +}
  268. +
  269. +static struct rtc_class_ops armada38x_rtc_ops = {
  270. + .read_time = armada38x_rtc_read_time,
  271. + .set_time = armada38x_rtc_set_time,
  272. + .read_alarm = armada38x_rtc_read_alarm,
  273. + .set_alarm = armada38x_rtc_set_alarm,
  274. + .alarm_irq_enable = armada38x_rtc_alarm_irq_enable,
  275. +};
  276. +
  277. +static __init int armada38x_rtc_probe(struct platform_device *pdev)
  278. +{
  279. + struct resource *res;
  280. + struct armada38x_rtc *rtc;
  281. + int ret;
  282. +
  283. + rtc = devm_kzalloc(&pdev->dev, sizeof(struct armada38x_rtc),
  284. + GFP_KERNEL);
  285. + if (!rtc)
  286. + return -ENOMEM;
  287. +
  288. + spin_lock_init(&rtc->lock);
  289. +
  290. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc");
  291. + rtc->regs = devm_ioremap_resource(&pdev->dev, res);
  292. + if (IS_ERR(rtc->regs))
  293. + return PTR_ERR(rtc->regs);
  294. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc-soc");
  295. + rtc->regs_soc = devm_ioremap_resource(&pdev->dev, res);
  296. + if (IS_ERR(rtc->regs_soc))
  297. + return PTR_ERR(rtc->regs_soc);
  298. +
  299. + rtc->irq = platform_get_irq(pdev, 0);
  300. +
  301. + if (rtc->irq < 0) {
  302. + dev_err(&pdev->dev, "no irq\n");
  303. + return rtc->irq;
  304. + }
  305. + if (devm_request_irq(&pdev->dev, rtc->irq, armada38x_rtc_alarm_irq,
  306. + 0, pdev->name, rtc) < 0) {
  307. + dev_warn(&pdev->dev, "Interrupt not available.\n");
  308. + rtc->irq = -1;
  309. + /*
  310. + * If there is no interrupt available then we can't
  311. + * use the alarm
  312. + */
  313. + armada38x_rtc_ops.set_alarm = NULL;
  314. + armada38x_rtc_ops.alarm_irq_enable = NULL;
  315. + }
  316. + platform_set_drvdata(pdev, rtc);
  317. + if (rtc->irq != -1)
  318. + device_init_wakeup(&pdev->dev, 1);
  319. +
  320. + rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, pdev->name,
  321. + &armada38x_rtc_ops, THIS_MODULE);
  322. + if (IS_ERR(rtc->rtc_dev)) {
  323. + ret = PTR_ERR(rtc->rtc_dev);
  324. + dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  325. + return ret;
  326. + }
  327. + return 0;
  328. +}
  329. +
  330. +#ifdef CONFIG_PM_SLEEP
  331. +static int armada38x_rtc_suspend(struct device *dev)
  332. +{
  333. + if (device_may_wakeup(dev)) {
  334. + struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  335. +
  336. + return enable_irq_wake(rtc->irq);
  337. + }
  338. +
  339. + return 0;
  340. +}
  341. +
  342. +static int armada38x_rtc_resume(struct device *dev)
  343. +{
  344. + if (device_may_wakeup(dev)) {
  345. + struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  346. +
  347. + return disable_irq_wake(rtc->irq);
  348. + }
  349. +
  350. + return 0;
  351. +}
  352. +#endif
  353. +
  354. +static SIMPLE_DEV_PM_OPS(armada38x_rtc_pm_ops,
  355. + armada38x_rtc_suspend, armada38x_rtc_resume);
  356. +
  357. +#ifdef CONFIG_OF
  358. +static const struct of_device_id armada38x_rtc_of_match_table[] = {
  359. + { .compatible = "marvell,armada-380-rtc", },
  360. + {}
  361. +};
  362. +#endif
  363. +
  364. +static struct platform_driver armada38x_rtc_driver = {
  365. + .driver = {
  366. + .name = "armada38x-rtc",
  367. + .pm = &armada38x_rtc_pm_ops,
  368. + .of_match_table = of_match_ptr(armada38x_rtc_of_match_table),
  369. + },
  370. +};
  371. +
  372. +module_platform_driver_probe(armada38x_rtc_driver, armada38x_rtc_probe);
  373. +
  374. +MODULE_DESCRIPTION("Marvell Armada 38x RTC driver");
  375. +MODULE_AUTHOR("Gregory CLEMENT <gregory.clement@free-electrons.com>");
  376. +MODULE_LICENSE("GPL");
  377. --- a/MAINTAINERS
  378. +++ b/MAINTAINERS
  379. @@ -1136,6 +1136,7 @@ M: Sebastian Hesselbarth <sebastian.hess
  380. L: linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
  381. S: Maintained
  382. F: arch/arm/mach-mvebu/
  383. +F: drivers/rtc/armada38x-rtc
  384. ARM/Marvell Berlin SoC support
  385. M: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
  386. --- a/arch/arm/boot/dts/armada-38x.dtsi
  387. +++ b/arch/arm/boot/dts/armada-38x.dtsi
  388. @@ -420,6 +420,13 @@
  389. clocks = <&gateclk 4>;
  390. };
  391. + rtc@a3800 {
  392. + compatible = "marvell,armada-380-rtc";
  393. + reg = <0xa3800 0x20>, <0x184a0 0x0c>;
  394. + reg-names = "rtc", "rtc-soc";
  395. + interrupts = <GIC_SPI 21 IRQ_TYPE_LEVEL_HIGH>;
  396. + };
  397. +
  398. sata@a8000 {
  399. compatible = "marvell,armada-380-ahci";
  400. reg = <0xa8000 0x2000>;