rtc-jz4740.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
  3. * Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net>
  4. * JZ4740 SoC RTC driver
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * You should have received a copy of the GNU General Public License along
  12. * with this program; if not, write to the Free Software Foundation, Inc.,
  13. * 675 Mass Ave, Cambridge, MA 02139, USA.
  14. *
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of_device.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/reboot.h>
  23. #include <linux/rtc.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #define JZ_REG_RTC_CTRL 0x00
  27. #define JZ_REG_RTC_SEC 0x04
  28. #define JZ_REG_RTC_SEC_ALARM 0x08
  29. #define JZ_REG_RTC_REGULATOR 0x0C
  30. #define JZ_REG_RTC_HIBERNATE 0x20
  31. #define JZ_REG_RTC_WAKEUP_FILTER 0x24
  32. #define JZ_REG_RTC_RESET_COUNTER 0x28
  33. #define JZ_REG_RTC_SCRATCHPAD 0x34
  34. /* The following are present on the jz4780 */
  35. #define JZ_REG_RTC_WENR 0x3C
  36. #define JZ_RTC_WENR_WEN BIT(31)
  37. #define JZ_RTC_CTRL_WRDY BIT(7)
  38. #define JZ_RTC_CTRL_1HZ BIT(6)
  39. #define JZ_RTC_CTRL_1HZ_IRQ BIT(5)
  40. #define JZ_RTC_CTRL_AF BIT(4)
  41. #define JZ_RTC_CTRL_AF_IRQ BIT(3)
  42. #define JZ_RTC_CTRL_AE BIT(2)
  43. #define JZ_RTC_CTRL_ENABLE BIT(0)
  44. /* Magic value to enable writes on jz4780 */
  45. #define JZ_RTC_WENR_MAGIC 0xA55A
  46. #define JZ_RTC_WAKEUP_FILTER_MASK 0x0000FFE0
  47. #define JZ_RTC_RESET_COUNTER_MASK 0x00000FE0
  48. enum jz4740_rtc_type {
  49. ID_JZ4740,
  50. ID_JZ4780,
  51. };
  52. struct jz4740_rtc {
  53. void __iomem *base;
  54. enum jz4740_rtc_type type;
  55. struct rtc_device *rtc;
  56. struct clk *clk;
  57. int irq;
  58. spinlock_t lock;
  59. unsigned int min_wakeup_pin_assert_time;
  60. unsigned int reset_pin_assert_time;
  61. };
  62. static struct device *dev_for_power_off;
  63. static inline uint32_t jz4740_rtc_reg_read(struct jz4740_rtc *rtc, size_t reg)
  64. {
  65. return readl(rtc->base + reg);
  66. }
  67. static int jz4740_rtc_wait_write_ready(struct jz4740_rtc *rtc)
  68. {
  69. uint32_t ctrl;
  70. int timeout = 10000;
  71. do {
  72. ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
  73. } while (!(ctrl & JZ_RTC_CTRL_WRDY) && --timeout);
  74. return timeout ? 0 : -EIO;
  75. }
  76. static inline int jz4780_rtc_enable_write(struct jz4740_rtc *rtc)
  77. {
  78. uint32_t ctrl;
  79. int ret, timeout = 10000;
  80. ret = jz4740_rtc_wait_write_ready(rtc);
  81. if (ret != 0)
  82. return ret;
  83. writel(JZ_RTC_WENR_MAGIC, rtc->base + JZ_REG_RTC_WENR);
  84. do {
  85. ctrl = readl(rtc->base + JZ_REG_RTC_WENR);
  86. } while (!(ctrl & JZ_RTC_WENR_WEN) && --timeout);
  87. return timeout ? 0 : -EIO;
  88. }
  89. static inline int jz4740_rtc_reg_write(struct jz4740_rtc *rtc, size_t reg,
  90. uint32_t val)
  91. {
  92. int ret = 0;
  93. if (rtc->type >= ID_JZ4780)
  94. ret = jz4780_rtc_enable_write(rtc);
  95. if (ret == 0)
  96. ret = jz4740_rtc_wait_write_ready(rtc);
  97. if (ret == 0)
  98. writel(val, rtc->base + reg);
  99. return ret;
  100. }
  101. static int jz4740_rtc_ctrl_set_bits(struct jz4740_rtc *rtc, uint32_t mask,
  102. bool set)
  103. {
  104. int ret;
  105. unsigned long flags;
  106. uint32_t ctrl;
  107. spin_lock_irqsave(&rtc->lock, flags);
  108. ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
  109. /* Don't clear interrupt flags by accident */
  110. ctrl |= JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF;
  111. if (set)
  112. ctrl |= mask;
  113. else
  114. ctrl &= ~mask;
  115. ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_CTRL, ctrl);
  116. spin_unlock_irqrestore(&rtc->lock, flags);
  117. return ret;
  118. }
  119. static int jz4740_rtc_read_time(struct device *dev, struct rtc_time *time)
  120. {
  121. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  122. uint32_t secs, secs2;
  123. int timeout = 5;
  124. /* If the seconds register is read while it is updated, it can contain a
  125. * bogus value. This can be avoided by making sure that two consecutive
  126. * reads have the same value.
  127. */
  128. secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
  129. secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
  130. while (secs != secs2 && --timeout) {
  131. secs = secs2;
  132. secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
  133. }
  134. if (timeout == 0)
  135. return -EIO;
  136. rtc_time_to_tm(secs, time);
  137. return 0;
  138. }
  139. static int jz4740_rtc_set_mmss(struct device *dev, unsigned long secs)
  140. {
  141. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  142. return jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC, secs);
  143. }
  144. static int jz4740_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  145. {
  146. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  147. uint32_t secs;
  148. uint32_t ctrl;
  149. secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC_ALARM);
  150. ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
  151. alrm->enabled = !!(ctrl & JZ_RTC_CTRL_AE);
  152. alrm->pending = !!(ctrl & JZ_RTC_CTRL_AF);
  153. rtc_time_to_tm(secs, &alrm->time);
  154. return rtc_valid_tm(&alrm->time);
  155. }
  156. static int jz4740_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  157. {
  158. int ret;
  159. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  160. unsigned long secs;
  161. rtc_tm_to_time(&alrm->time, &secs);
  162. ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC_ALARM, secs);
  163. if (!ret)
  164. ret = jz4740_rtc_ctrl_set_bits(rtc,
  165. JZ_RTC_CTRL_AE | JZ_RTC_CTRL_AF_IRQ, alrm->enabled);
  166. return ret;
  167. }
  168. static int jz4740_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
  169. {
  170. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  171. return jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_AF_IRQ, enable);
  172. }
  173. static const struct rtc_class_ops jz4740_rtc_ops = {
  174. .read_time = jz4740_rtc_read_time,
  175. .set_mmss = jz4740_rtc_set_mmss,
  176. .read_alarm = jz4740_rtc_read_alarm,
  177. .set_alarm = jz4740_rtc_set_alarm,
  178. .alarm_irq_enable = jz4740_rtc_alarm_irq_enable,
  179. };
  180. static irqreturn_t jz4740_rtc_irq(int irq, void *data)
  181. {
  182. struct jz4740_rtc *rtc = data;
  183. uint32_t ctrl;
  184. unsigned long events = 0;
  185. ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
  186. if (ctrl & JZ_RTC_CTRL_1HZ)
  187. events |= (RTC_UF | RTC_IRQF);
  188. if (ctrl & JZ_RTC_CTRL_AF)
  189. events |= (RTC_AF | RTC_IRQF);
  190. rtc_update_irq(rtc->rtc, 1, events);
  191. jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF, false);
  192. return IRQ_HANDLED;
  193. }
  194. static void jz4740_rtc_poweroff(struct device *dev)
  195. {
  196. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  197. jz4740_rtc_reg_write(rtc, JZ_REG_RTC_HIBERNATE, 1);
  198. }
  199. static void jz4740_rtc_power_off(void)
  200. {
  201. struct jz4740_rtc *rtc = dev_get_drvdata(dev_for_power_off);
  202. unsigned long rtc_rate;
  203. unsigned long wakeup_filter_ticks;
  204. unsigned long reset_counter_ticks;
  205. clk_prepare_enable(rtc->clk);
  206. rtc_rate = clk_get_rate(rtc->clk);
  207. /*
  208. * Set minimum wakeup pin assertion time: 100 ms.
  209. * Range is 0 to 2 sec if RTC is clocked at 32 kHz.
  210. */
  211. wakeup_filter_ticks =
  212. (rtc->min_wakeup_pin_assert_time * rtc_rate) / 1000;
  213. if (wakeup_filter_ticks < JZ_RTC_WAKEUP_FILTER_MASK)
  214. wakeup_filter_ticks &= JZ_RTC_WAKEUP_FILTER_MASK;
  215. else
  216. wakeup_filter_ticks = JZ_RTC_WAKEUP_FILTER_MASK;
  217. jz4740_rtc_reg_write(rtc,
  218. JZ_REG_RTC_WAKEUP_FILTER, wakeup_filter_ticks);
  219. /*
  220. * Set reset pin low-level assertion time after wakeup: 60 ms.
  221. * Range is 0 to 125 ms if RTC is clocked at 32 kHz.
  222. */
  223. reset_counter_ticks = (rtc->reset_pin_assert_time * rtc_rate) / 1000;
  224. if (reset_counter_ticks < JZ_RTC_RESET_COUNTER_MASK)
  225. reset_counter_ticks &= JZ_RTC_RESET_COUNTER_MASK;
  226. else
  227. reset_counter_ticks = JZ_RTC_RESET_COUNTER_MASK;
  228. jz4740_rtc_reg_write(rtc,
  229. JZ_REG_RTC_RESET_COUNTER, reset_counter_ticks);
  230. jz4740_rtc_poweroff(dev_for_power_off);
  231. kernel_halt();
  232. }
  233. static const struct of_device_id jz4740_rtc_of_match[] = {
  234. { .compatible = "ingenic,jz4740-rtc", .data = (void *)ID_JZ4740 },
  235. { .compatible = "ingenic,jz4780-rtc", .data = (void *)ID_JZ4780 },
  236. {},
  237. };
  238. MODULE_DEVICE_TABLE(of, jz4740_rtc_of_match);
  239. static int jz4740_rtc_probe(struct platform_device *pdev)
  240. {
  241. int ret;
  242. struct jz4740_rtc *rtc;
  243. uint32_t scratchpad;
  244. struct resource *mem;
  245. const struct platform_device_id *id = platform_get_device_id(pdev);
  246. const struct of_device_id *of_id = of_match_device(
  247. jz4740_rtc_of_match, &pdev->dev);
  248. struct device_node *np = pdev->dev.of_node;
  249. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  250. if (!rtc)
  251. return -ENOMEM;
  252. if (of_id)
  253. rtc->type = (enum jz4740_rtc_type)of_id->data;
  254. else
  255. rtc->type = id->driver_data;
  256. rtc->irq = platform_get_irq(pdev, 0);
  257. if (rtc->irq < 0) {
  258. dev_err(&pdev->dev, "Failed to get platform irq\n");
  259. return -ENOENT;
  260. }
  261. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  262. rtc->base = devm_ioremap_resource(&pdev->dev, mem);
  263. if (IS_ERR(rtc->base))
  264. return PTR_ERR(rtc->base);
  265. rtc->clk = devm_clk_get(&pdev->dev, "rtc");
  266. if (IS_ERR(rtc->clk)) {
  267. dev_err(&pdev->dev, "Failed to get RTC clock\n");
  268. return PTR_ERR(rtc->clk);
  269. }
  270. spin_lock_init(&rtc->lock);
  271. platform_set_drvdata(pdev, rtc);
  272. device_init_wakeup(&pdev->dev, 1);
  273. rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  274. &jz4740_rtc_ops, THIS_MODULE);
  275. if (IS_ERR(rtc->rtc)) {
  276. ret = PTR_ERR(rtc->rtc);
  277. dev_err(&pdev->dev, "Failed to register rtc device: %d\n", ret);
  278. return ret;
  279. }
  280. ret = devm_request_irq(&pdev->dev, rtc->irq, jz4740_rtc_irq, 0,
  281. pdev->name, rtc);
  282. if (ret) {
  283. dev_err(&pdev->dev, "Failed to request rtc irq: %d\n", ret);
  284. return ret;
  285. }
  286. scratchpad = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SCRATCHPAD);
  287. if (scratchpad != 0x12345678) {
  288. ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SCRATCHPAD, 0x12345678);
  289. ret = jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC, 0);
  290. if (ret) {
  291. dev_err(&pdev->dev, "Could not write to RTC registers\n");
  292. return ret;
  293. }
  294. }
  295. if (np && of_device_is_system_power_controller(np)) {
  296. if (!pm_power_off) {
  297. /* Default: 60ms */
  298. rtc->reset_pin_assert_time = 60;
  299. of_property_read_u32(np, "reset-pin-assert-time-ms",
  300. &rtc->reset_pin_assert_time);
  301. /* Default: 100ms */
  302. rtc->min_wakeup_pin_assert_time = 100;
  303. of_property_read_u32(np,
  304. "min-wakeup-pin-assert-time-ms",
  305. &rtc->min_wakeup_pin_assert_time);
  306. dev_for_power_off = &pdev->dev;
  307. pm_power_off = jz4740_rtc_power_off;
  308. } else {
  309. dev_warn(&pdev->dev,
  310. "Poweroff handler already present!\n");
  311. }
  312. }
  313. return 0;
  314. }
  315. #ifdef CONFIG_PM
  316. static int jz4740_rtc_suspend(struct device *dev)
  317. {
  318. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  319. if (device_may_wakeup(dev))
  320. enable_irq_wake(rtc->irq);
  321. return 0;
  322. }
  323. static int jz4740_rtc_resume(struct device *dev)
  324. {
  325. struct jz4740_rtc *rtc = dev_get_drvdata(dev);
  326. if (device_may_wakeup(dev))
  327. disable_irq_wake(rtc->irq);
  328. return 0;
  329. }
  330. static const struct dev_pm_ops jz4740_pm_ops = {
  331. .suspend = jz4740_rtc_suspend,
  332. .resume = jz4740_rtc_resume,
  333. };
  334. #define JZ4740_RTC_PM_OPS (&jz4740_pm_ops)
  335. #else
  336. #define JZ4740_RTC_PM_OPS NULL
  337. #endif /* CONFIG_PM */
  338. static const struct platform_device_id jz4740_rtc_ids[] = {
  339. { "jz4740-rtc", ID_JZ4740 },
  340. { "jz4780-rtc", ID_JZ4780 },
  341. {}
  342. };
  343. MODULE_DEVICE_TABLE(platform, jz4740_rtc_ids);
  344. static struct platform_driver jz4740_rtc_driver = {
  345. .probe = jz4740_rtc_probe,
  346. .driver = {
  347. .name = "jz4740-rtc",
  348. .pm = JZ4740_RTC_PM_OPS,
  349. .of_match_table = of_match_ptr(jz4740_rtc_of_match),
  350. },
  351. .id_table = jz4740_rtc_ids,
  352. };
  353. module_platform_driver(jz4740_rtc_driver);
  354. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  355. MODULE_LICENSE("GPL");
  356. MODULE_DESCRIPTION("RTC driver for the JZ4740 SoC\n");
  357. MODULE_ALIAS("platform:jz4740-rtc");