rtc-armada38x.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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/of_device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/rtc.h>
  21. #define RTC_STATUS 0x0
  22. #define RTC_STATUS_ALARM1 BIT(0)
  23. #define RTC_STATUS_ALARM2 BIT(1)
  24. #define RTC_IRQ1_CONF 0x4
  25. #define RTC_IRQ2_CONF 0x8
  26. #define RTC_IRQ_AL_EN BIT(0)
  27. #define RTC_IRQ_FREQ_EN BIT(1)
  28. #define RTC_IRQ_FREQ_1HZ BIT(2)
  29. #define RTC_TIME 0xC
  30. #define RTC_ALARM1 0x10
  31. #define RTC_ALARM2 0x14
  32. /* Armada38x SoC registers */
  33. #define RTC_38X_BRIDGE_TIMING_CTL 0x0
  34. #define RTC_38X_PERIOD_OFFS 0
  35. #define RTC_38X_PERIOD_MASK (0x3FF << RTC_38X_PERIOD_OFFS)
  36. #define RTC_38X_READ_DELAY_OFFS 26
  37. #define RTC_38X_READ_DELAY_MASK (0x1F << RTC_38X_READ_DELAY_OFFS)
  38. /* Armada 7K/8K registers */
  39. #define RTC_8K_BRIDGE_TIMING_CTL0 0x0
  40. #define RTC_8K_WRCLK_PERIOD_OFFS 0
  41. #define RTC_8K_WRCLK_PERIOD_MASK (0xFFFF << RTC_8K_WRCLK_PERIOD_OFFS)
  42. #define RTC_8K_WRCLK_SETUP_OFFS 16
  43. #define RTC_8K_WRCLK_SETUP_MASK (0xFFFF << RTC_8K_WRCLK_SETUP_OFFS)
  44. #define RTC_8K_BRIDGE_TIMING_CTL1 0x4
  45. #define RTC_8K_READ_DELAY_OFFS 0
  46. #define RTC_8K_READ_DELAY_MASK (0xFFFF << RTC_8K_READ_DELAY_OFFS)
  47. #define RTC_8K_ISR 0x10
  48. #define RTC_8K_IMR 0x14
  49. #define RTC_8K_ALARM2 BIT(0)
  50. #define SOC_RTC_INTERRUPT 0x8
  51. #define SOC_RTC_ALARM1 BIT(0)
  52. #define SOC_RTC_ALARM2 BIT(1)
  53. #define SOC_RTC_ALARM1_MASK BIT(2)
  54. #define SOC_RTC_ALARM2_MASK BIT(3)
  55. #define SAMPLE_NR 100
  56. struct value_to_freq {
  57. u32 value;
  58. u8 freq;
  59. };
  60. struct armada38x_rtc {
  61. struct rtc_device *rtc_dev;
  62. void __iomem *regs;
  63. void __iomem *regs_soc;
  64. spinlock_t lock;
  65. int irq;
  66. struct value_to_freq *val_to_freq;
  67. struct armada38x_rtc_data *data;
  68. };
  69. #define ALARM1 0
  70. #define ALARM2 1
  71. #define ALARM_REG(base, alarm) ((base) + (alarm) * sizeof(u32))
  72. struct armada38x_rtc_data {
  73. /* Initialize the RTC-MBUS bridge timing */
  74. void (*update_mbus_timing)(struct armada38x_rtc *rtc);
  75. u32 (*read_rtc_reg)(struct armada38x_rtc *rtc, u8 rtc_reg);
  76. void (*clear_isr)(struct armada38x_rtc *rtc);
  77. void (*unmask_interrupt)(struct armada38x_rtc *rtc);
  78. u32 alarm;
  79. };
  80. /*
  81. * According to the datasheet, the OS should wait 5us after every
  82. * register write to the RTC hard macro so that the required update
  83. * can occur without holding off the system bus
  84. * According to errata RES-3124064, Write to any RTC register
  85. * may fail. As a workaround, before writing to RTC
  86. * register, issue a dummy write of 0x0 twice to RTC Status
  87. * register.
  88. */
  89. static void rtc_delayed_write(u32 val, struct armada38x_rtc *rtc, int offset)
  90. {
  91. writel(0, rtc->regs + RTC_STATUS);
  92. writel(0, rtc->regs + RTC_STATUS);
  93. writel(val, rtc->regs + offset);
  94. udelay(5);
  95. }
  96. /* Update RTC-MBUS bridge timing parameters */
  97. static void rtc_update_38x_mbus_timing_params(struct armada38x_rtc *rtc)
  98. {
  99. u32 reg;
  100. reg = readl(rtc->regs_soc + RTC_38X_BRIDGE_TIMING_CTL);
  101. reg &= ~RTC_38X_PERIOD_MASK;
  102. reg |= 0x3FF << RTC_38X_PERIOD_OFFS; /* Maximum value */
  103. reg &= ~RTC_38X_READ_DELAY_MASK;
  104. reg |= 0x1F << RTC_38X_READ_DELAY_OFFS; /* Maximum value */
  105. writel(reg, rtc->regs_soc + RTC_38X_BRIDGE_TIMING_CTL);
  106. }
  107. static void rtc_update_8k_mbus_timing_params(struct armada38x_rtc *rtc)
  108. {
  109. u32 reg;
  110. reg = readl(rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL0);
  111. reg &= ~RTC_8K_WRCLK_PERIOD_MASK;
  112. reg |= 0x3FF << RTC_8K_WRCLK_PERIOD_OFFS;
  113. reg &= ~RTC_8K_WRCLK_SETUP_MASK;
  114. reg |= 0x29 << RTC_8K_WRCLK_SETUP_OFFS;
  115. writel(reg, rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL0);
  116. reg = readl(rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL1);
  117. reg &= ~RTC_8K_READ_DELAY_MASK;
  118. reg |= 0x3F << RTC_8K_READ_DELAY_OFFS;
  119. writel(reg, rtc->regs_soc + RTC_8K_BRIDGE_TIMING_CTL1);
  120. }
  121. static u32 read_rtc_register(struct armada38x_rtc *rtc, u8 rtc_reg)
  122. {
  123. return readl(rtc->regs + rtc_reg);
  124. }
  125. static u32 read_rtc_register_38x_wa(struct armada38x_rtc *rtc, u8 rtc_reg)
  126. {
  127. int i, index_max = 0, max = 0;
  128. for (i = 0; i < SAMPLE_NR; i++) {
  129. rtc->val_to_freq[i].value = readl(rtc->regs + rtc_reg);
  130. rtc->val_to_freq[i].freq = 0;
  131. }
  132. for (i = 0; i < SAMPLE_NR; i++) {
  133. int j = 0;
  134. u32 value = rtc->val_to_freq[i].value;
  135. while (rtc->val_to_freq[j].freq) {
  136. if (rtc->val_to_freq[j].value == value) {
  137. rtc->val_to_freq[j].freq++;
  138. break;
  139. }
  140. j++;
  141. }
  142. if (!rtc->val_to_freq[j].freq) {
  143. rtc->val_to_freq[j].value = value;
  144. rtc->val_to_freq[j].freq = 1;
  145. }
  146. if (rtc->val_to_freq[j].freq > max) {
  147. index_max = j;
  148. max = rtc->val_to_freq[j].freq;
  149. }
  150. /*
  151. * If a value already has half of the sample this is the most
  152. * frequent one and we can stop the research right now
  153. */
  154. if (max > SAMPLE_NR / 2)
  155. break;
  156. }
  157. return rtc->val_to_freq[index_max].value;
  158. }
  159. static void armada38x_clear_isr(struct armada38x_rtc *rtc)
  160. {
  161. u32 val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
  162. writel(val & ~SOC_RTC_ALARM1, rtc->regs_soc + SOC_RTC_INTERRUPT);
  163. }
  164. static void armada38x_unmask_interrupt(struct armada38x_rtc *rtc)
  165. {
  166. u32 val = readl(rtc->regs_soc + SOC_RTC_INTERRUPT);
  167. writel(val | SOC_RTC_ALARM1_MASK, rtc->regs_soc + SOC_RTC_INTERRUPT);
  168. }
  169. static void armada8k_clear_isr(struct armada38x_rtc *rtc)
  170. {
  171. writel(RTC_8K_ALARM2, rtc->regs_soc + RTC_8K_ISR);
  172. }
  173. static void armada8k_unmask_interrupt(struct armada38x_rtc *rtc)
  174. {
  175. writel(RTC_8K_ALARM2, rtc->regs_soc + RTC_8K_IMR);
  176. }
  177. static int armada38x_rtc_read_time(struct device *dev, struct rtc_time *tm)
  178. {
  179. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  180. unsigned long time, flags;
  181. spin_lock_irqsave(&rtc->lock, flags);
  182. time = rtc->data->read_rtc_reg(rtc, RTC_TIME);
  183. spin_unlock_irqrestore(&rtc->lock, flags);
  184. rtc_time_to_tm(time, tm);
  185. return 0;
  186. }
  187. static int armada38x_rtc_set_time(struct device *dev, struct rtc_time *tm)
  188. {
  189. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  190. int ret = 0;
  191. unsigned long time, flags;
  192. ret = rtc_tm_to_time(tm, &time);
  193. if (ret)
  194. goto out;
  195. spin_lock_irqsave(&rtc->lock, flags);
  196. rtc_delayed_write(time, rtc, RTC_TIME);
  197. spin_unlock_irqrestore(&rtc->lock, flags);
  198. out:
  199. return ret;
  200. }
  201. static int armada38x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  202. {
  203. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  204. unsigned long time, flags;
  205. u32 reg = ALARM_REG(RTC_ALARM1, rtc->data->alarm);
  206. u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
  207. u32 val;
  208. spin_lock_irqsave(&rtc->lock, flags);
  209. time = rtc->data->read_rtc_reg(rtc, reg);
  210. val = rtc->data->read_rtc_reg(rtc, reg_irq) & RTC_IRQ_AL_EN;
  211. spin_unlock_irqrestore(&rtc->lock, flags);
  212. alrm->enabled = val ? 1 : 0;
  213. rtc_time_to_tm(time, &alrm->time);
  214. return 0;
  215. }
  216. static int armada38x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  217. {
  218. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  219. u32 reg = ALARM_REG(RTC_ALARM1, rtc->data->alarm);
  220. u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
  221. unsigned long time, flags;
  222. int ret = 0;
  223. ret = rtc_tm_to_time(&alrm->time, &time);
  224. if (ret)
  225. goto out;
  226. spin_lock_irqsave(&rtc->lock, flags);
  227. rtc_delayed_write(time, rtc, reg);
  228. if (alrm->enabled) {
  229. rtc_delayed_write(RTC_IRQ_AL_EN, rtc, reg_irq);
  230. rtc->data->unmask_interrupt(rtc);
  231. }
  232. spin_unlock_irqrestore(&rtc->lock, flags);
  233. out:
  234. return ret;
  235. }
  236. static int armada38x_rtc_alarm_irq_enable(struct device *dev,
  237. unsigned int enabled)
  238. {
  239. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  240. u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
  241. unsigned long flags;
  242. spin_lock_irqsave(&rtc->lock, flags);
  243. if (enabled)
  244. rtc_delayed_write(RTC_IRQ_AL_EN, rtc, reg_irq);
  245. else
  246. rtc_delayed_write(0, rtc, reg_irq);
  247. spin_unlock_irqrestore(&rtc->lock, flags);
  248. return 0;
  249. }
  250. static irqreturn_t armada38x_rtc_alarm_irq(int irq, void *data)
  251. {
  252. struct armada38x_rtc *rtc = data;
  253. u32 val;
  254. int event = RTC_IRQF | RTC_AF;
  255. u32 reg_irq = ALARM_REG(RTC_IRQ1_CONF, rtc->data->alarm);
  256. dev_dbg(&rtc->rtc_dev->dev, "%s:irq(%d)\n", __func__, irq);
  257. spin_lock(&rtc->lock);
  258. rtc->data->clear_isr(rtc);
  259. val = rtc->data->read_rtc_reg(rtc, reg_irq);
  260. /* disable all the interrupts for alarm*/
  261. rtc_delayed_write(0, rtc, reg_irq);
  262. /* Ack the event */
  263. rtc_delayed_write(1 << rtc->data->alarm, rtc, RTC_STATUS);
  264. spin_unlock(&rtc->lock);
  265. if (val & RTC_IRQ_FREQ_EN) {
  266. if (val & RTC_IRQ_FREQ_1HZ)
  267. event |= RTC_UF;
  268. else
  269. event |= RTC_PF;
  270. }
  271. rtc_update_irq(rtc->rtc_dev, 1, event);
  272. return IRQ_HANDLED;
  273. }
  274. static const struct rtc_class_ops armada38x_rtc_ops = {
  275. .read_time = armada38x_rtc_read_time,
  276. .set_time = armada38x_rtc_set_time,
  277. .read_alarm = armada38x_rtc_read_alarm,
  278. .set_alarm = armada38x_rtc_set_alarm,
  279. .alarm_irq_enable = armada38x_rtc_alarm_irq_enable,
  280. };
  281. static const struct rtc_class_ops armada38x_rtc_ops_noirq = {
  282. .read_time = armada38x_rtc_read_time,
  283. .set_time = armada38x_rtc_set_time,
  284. .read_alarm = armada38x_rtc_read_alarm,
  285. };
  286. static const struct armada38x_rtc_data armada38x_data = {
  287. .update_mbus_timing = rtc_update_38x_mbus_timing_params,
  288. .read_rtc_reg = read_rtc_register_38x_wa,
  289. .clear_isr = armada38x_clear_isr,
  290. .unmask_interrupt = armada38x_unmask_interrupt,
  291. .alarm = ALARM1,
  292. };
  293. static const struct armada38x_rtc_data armada8k_data = {
  294. .update_mbus_timing = rtc_update_8k_mbus_timing_params,
  295. .read_rtc_reg = read_rtc_register,
  296. .clear_isr = armada8k_clear_isr,
  297. .unmask_interrupt = armada8k_unmask_interrupt,
  298. .alarm = ALARM2,
  299. };
  300. #ifdef CONFIG_OF
  301. static const struct of_device_id armada38x_rtc_of_match_table[] = {
  302. {
  303. .compatible = "marvell,armada-380-rtc",
  304. .data = &armada38x_data,
  305. },
  306. {
  307. .compatible = "marvell,armada-8k-rtc",
  308. .data = &armada8k_data,
  309. },
  310. {}
  311. };
  312. MODULE_DEVICE_TABLE(of, armada38x_rtc_of_match_table);
  313. #endif
  314. static __init int armada38x_rtc_probe(struct platform_device *pdev)
  315. {
  316. const struct rtc_class_ops *ops;
  317. struct resource *res;
  318. struct armada38x_rtc *rtc;
  319. const struct of_device_id *match;
  320. int ret;
  321. match = of_match_device(armada38x_rtc_of_match_table, &pdev->dev);
  322. if (!match)
  323. return -ENODEV;
  324. rtc = devm_kzalloc(&pdev->dev, sizeof(struct armada38x_rtc),
  325. GFP_KERNEL);
  326. if (!rtc)
  327. return -ENOMEM;
  328. rtc->val_to_freq = devm_kcalloc(&pdev->dev, SAMPLE_NR,
  329. sizeof(struct value_to_freq), GFP_KERNEL);
  330. if (!rtc->val_to_freq)
  331. return -ENOMEM;
  332. spin_lock_init(&rtc->lock);
  333. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc");
  334. rtc->regs = devm_ioremap_resource(&pdev->dev, res);
  335. if (IS_ERR(rtc->regs))
  336. return PTR_ERR(rtc->regs);
  337. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rtc-soc");
  338. rtc->regs_soc = devm_ioremap_resource(&pdev->dev, res);
  339. if (IS_ERR(rtc->regs_soc))
  340. return PTR_ERR(rtc->regs_soc);
  341. rtc->irq = platform_get_irq(pdev, 0);
  342. if (rtc->irq < 0) {
  343. dev_err(&pdev->dev, "no irq\n");
  344. return rtc->irq;
  345. }
  346. if (devm_request_irq(&pdev->dev, rtc->irq, armada38x_rtc_alarm_irq,
  347. 0, pdev->name, rtc) < 0) {
  348. dev_warn(&pdev->dev, "Interrupt not available.\n");
  349. rtc->irq = -1;
  350. }
  351. platform_set_drvdata(pdev, rtc);
  352. if (rtc->irq != -1) {
  353. device_init_wakeup(&pdev->dev, 1);
  354. ops = &armada38x_rtc_ops;
  355. } else {
  356. /*
  357. * If there is no interrupt available then we can't
  358. * use the alarm
  359. */
  360. ops = &armada38x_rtc_ops_noirq;
  361. }
  362. rtc->data = (struct armada38x_rtc_data *)match->data;
  363. /* Update RTC-MBUS bridge timing parameters */
  364. rtc->data->update_mbus_timing(rtc);
  365. rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, pdev->name,
  366. ops, THIS_MODULE);
  367. if (IS_ERR(rtc->rtc_dev)) {
  368. ret = PTR_ERR(rtc->rtc_dev);
  369. dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
  370. return ret;
  371. }
  372. return 0;
  373. }
  374. #ifdef CONFIG_PM_SLEEP
  375. static int armada38x_rtc_suspend(struct device *dev)
  376. {
  377. if (device_may_wakeup(dev)) {
  378. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  379. return enable_irq_wake(rtc->irq);
  380. }
  381. return 0;
  382. }
  383. static int armada38x_rtc_resume(struct device *dev)
  384. {
  385. if (device_may_wakeup(dev)) {
  386. struct armada38x_rtc *rtc = dev_get_drvdata(dev);
  387. /* Update RTC-MBUS bridge timing parameters */
  388. rtc->data->update_mbus_timing(rtc);
  389. return disable_irq_wake(rtc->irq);
  390. }
  391. return 0;
  392. }
  393. #endif
  394. static SIMPLE_DEV_PM_OPS(armada38x_rtc_pm_ops,
  395. armada38x_rtc_suspend, armada38x_rtc_resume);
  396. static struct platform_driver armada38x_rtc_driver = {
  397. .driver = {
  398. .name = "armada38x-rtc",
  399. .pm = &armada38x_rtc_pm_ops,
  400. .of_match_table = of_match_ptr(armada38x_rtc_of_match_table),
  401. },
  402. };
  403. module_platform_driver_probe(armada38x_rtc_driver, armada38x_rtc_probe);
  404. MODULE_DESCRIPTION("Marvell Armada 38x RTC driver");
  405. MODULE_AUTHOR("Gregory CLEMENT <gregory.clement@free-electrons.com>");
  406. MODULE_LICENSE("GPL");