rtc-stmp3xxx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Freescale STMP37XX/STMP378X Real Time Clock driver
  3. *
  4. * Copyright (c) 2007 Sigmatel, Inc.
  5. * Peter Hartley, <peter.hartley@sigmatel.com>
  6. *
  7. * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
  8. * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
  9. * Copyright 2011 Wolfram Sang, Pengutronix e.K.
  10. */
  11. /*
  12. * The code contained herein is licensed under the GNU General Public
  13. * License. You may obtain a copy of the GNU General Public License
  14. * Version 2 or later at the following locations:
  15. *
  16. * http://www.opensource.org/licenses/gpl-license.html
  17. * http://www.gnu.org/copyleft/gpl.html
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/io.h>
  22. #include <linux/init.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/delay.h>
  26. #include <linux/rtc.h>
  27. #include <linux/slab.h>
  28. #include <linux/of_device.h>
  29. #include <linux/of.h>
  30. #include <linux/stmp_device.h>
  31. #include <linux/stmp3xxx_rtc_wdt.h>
  32. #define STMP3XXX_RTC_CTRL 0x0
  33. #define STMP3XXX_RTC_CTRL_ALARM_IRQ_EN 0x00000001
  34. #define STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN 0x00000002
  35. #define STMP3XXX_RTC_CTRL_ALARM_IRQ 0x00000004
  36. #define STMP3XXX_RTC_CTRL_WATCHDOGEN 0x00000010
  37. #define STMP3XXX_RTC_STAT 0x10
  38. #define STMP3XXX_RTC_STAT_STALE_SHIFT 16
  39. #define STMP3XXX_RTC_STAT_RTC_PRESENT 0x80000000
  40. #define STMP3XXX_RTC_STAT_XTAL32000_PRESENT 0x10000000
  41. #define STMP3XXX_RTC_STAT_XTAL32768_PRESENT 0x08000000
  42. #define STMP3XXX_RTC_SECONDS 0x30
  43. #define STMP3XXX_RTC_ALARM 0x40
  44. #define STMP3XXX_RTC_WATCHDOG 0x50
  45. #define STMP3XXX_RTC_PERSISTENT0 0x60
  46. #define STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE (1 << 0)
  47. #define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN (1 << 1)
  48. #define STMP3XXX_RTC_PERSISTENT0_ALARM_EN (1 << 2)
  49. #define STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP (1 << 4)
  50. #define STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP (1 << 5)
  51. #define STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ (1 << 6)
  52. #define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE (1 << 7)
  53. #define STMP3XXX_RTC_PERSISTENT1 0x70
  54. /* missing bitmask in headers */
  55. #define STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER 0x80000000
  56. struct stmp3xxx_rtc_data {
  57. struct rtc_device *rtc;
  58. void __iomem *io;
  59. int irq_alarm;
  60. };
  61. #if IS_ENABLED(CONFIG_STMP3XXX_RTC_WATCHDOG)
  62. /**
  63. * stmp3xxx_wdt_set_timeout - configure the watchdog inside the STMP3xxx RTC
  64. * @dev: the parent device of the watchdog (= the RTC)
  65. * @timeout: the desired value for the timeout register of the watchdog.
  66. * 0 disables the watchdog
  67. *
  68. * The watchdog needs one register and two bits which are in the RTC domain.
  69. * To handle the resource conflict, the RTC driver will create another
  70. * platform_device for the watchdog driver as a child of the RTC device.
  71. * The watchdog driver is passed the below accessor function via platform_data
  72. * to configure the watchdog. Locking is not needed because accessing SET/CLR
  73. * registers is atomic.
  74. */
  75. static void stmp3xxx_wdt_set_timeout(struct device *dev, u32 timeout)
  76. {
  77. struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
  78. if (timeout) {
  79. writel(timeout, rtc_data->io + STMP3XXX_RTC_WATCHDOG);
  80. writel(STMP3XXX_RTC_CTRL_WATCHDOGEN,
  81. rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_SET);
  82. writel(STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER,
  83. rtc_data->io + STMP3XXX_RTC_PERSISTENT1 + STMP_OFFSET_REG_SET);
  84. } else {
  85. writel(STMP3XXX_RTC_CTRL_WATCHDOGEN,
  86. rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
  87. writel(STMP3XXX_RTC_PERSISTENT1_FORCE_UPDATER,
  88. rtc_data->io + STMP3XXX_RTC_PERSISTENT1 + STMP_OFFSET_REG_CLR);
  89. }
  90. }
  91. static struct stmp3xxx_wdt_pdata wdt_pdata = {
  92. .wdt_set_timeout = stmp3xxx_wdt_set_timeout,
  93. };
  94. static void stmp3xxx_wdt_register(struct platform_device *rtc_pdev)
  95. {
  96. struct platform_device *wdt_pdev =
  97. platform_device_alloc("stmp3xxx_rtc_wdt", rtc_pdev->id);
  98. if (wdt_pdev) {
  99. wdt_pdev->dev.parent = &rtc_pdev->dev;
  100. wdt_pdev->dev.platform_data = &wdt_pdata;
  101. platform_device_add(wdt_pdev);
  102. }
  103. }
  104. #else
  105. static void stmp3xxx_wdt_register(struct platform_device *rtc_pdev)
  106. {
  107. }
  108. #endif /* CONFIG_STMP3XXX_RTC_WATCHDOG */
  109. static int stmp3xxx_wait_time(struct stmp3xxx_rtc_data *rtc_data)
  110. {
  111. int timeout = 5000; /* 3ms according to i.MX28 Ref Manual */
  112. /*
  113. * The i.MX28 Applications Processor Reference Manual, Rev. 1, 2010
  114. * states:
  115. * | The order in which registers are updated is
  116. * | Persistent 0, 1, 2, 3, 4, 5, Alarm, Seconds.
  117. * | (This list is in bitfield order, from LSB to MSB, as they would
  118. * | appear in the STALE_REGS and NEW_REGS bitfields of the HW_RTC_STAT
  119. * | register. For example, the Seconds register corresponds to
  120. * | STALE_REGS or NEW_REGS containing 0x80.)
  121. */
  122. do {
  123. if (!(readl(rtc_data->io + STMP3XXX_RTC_STAT) &
  124. (0x80 << STMP3XXX_RTC_STAT_STALE_SHIFT)))
  125. return 0;
  126. udelay(1);
  127. } while (--timeout > 0);
  128. return (readl(rtc_data->io + STMP3XXX_RTC_STAT) &
  129. (0x80 << STMP3XXX_RTC_STAT_STALE_SHIFT)) ? -ETIME : 0;
  130. }
  131. /* Time read/write */
  132. static int stmp3xxx_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
  133. {
  134. int ret;
  135. struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
  136. ret = stmp3xxx_wait_time(rtc_data);
  137. if (ret)
  138. return ret;
  139. rtc_time_to_tm(readl(rtc_data->io + STMP3XXX_RTC_SECONDS), rtc_tm);
  140. return 0;
  141. }
  142. static int stmp3xxx_rtc_set_mmss(struct device *dev, unsigned long t)
  143. {
  144. struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
  145. writel(t, rtc_data->io + STMP3XXX_RTC_SECONDS);
  146. return stmp3xxx_wait_time(rtc_data);
  147. }
  148. /* interrupt(s) handler */
  149. static irqreturn_t stmp3xxx_rtc_interrupt(int irq, void *dev_id)
  150. {
  151. struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev_id);
  152. u32 status = readl(rtc_data->io + STMP3XXX_RTC_CTRL);
  153. if (status & STMP3XXX_RTC_CTRL_ALARM_IRQ) {
  154. writel(STMP3XXX_RTC_CTRL_ALARM_IRQ,
  155. rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
  156. rtc_update_irq(rtc_data->rtc, 1, RTC_AF | RTC_IRQF);
  157. return IRQ_HANDLED;
  158. }
  159. return IRQ_NONE;
  160. }
  161. static int stmp3xxx_alarm_irq_enable(struct device *dev, unsigned int enabled)
  162. {
  163. struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
  164. if (enabled) {
  165. writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
  166. STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN,
  167. rtc_data->io + STMP3XXX_RTC_PERSISTENT0 +
  168. STMP_OFFSET_REG_SET);
  169. writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
  170. rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_SET);
  171. } else {
  172. writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
  173. STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN,
  174. rtc_data->io + STMP3XXX_RTC_PERSISTENT0 +
  175. STMP_OFFSET_REG_CLR);
  176. writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
  177. rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
  178. }
  179. return 0;
  180. }
  181. static int stmp3xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  182. {
  183. struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
  184. rtc_time_to_tm(readl(rtc_data->io + STMP3XXX_RTC_ALARM), &alm->time);
  185. return 0;
  186. }
  187. static int stmp3xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  188. {
  189. unsigned long t;
  190. struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
  191. rtc_tm_to_time(&alm->time, &t);
  192. writel(t, rtc_data->io + STMP3XXX_RTC_ALARM);
  193. stmp3xxx_alarm_irq_enable(dev, alm->enabled);
  194. return 0;
  195. }
  196. static struct rtc_class_ops stmp3xxx_rtc_ops = {
  197. .alarm_irq_enable =
  198. stmp3xxx_alarm_irq_enable,
  199. .read_time = stmp3xxx_rtc_gettime,
  200. .set_mmss = stmp3xxx_rtc_set_mmss,
  201. .read_alarm = stmp3xxx_rtc_read_alarm,
  202. .set_alarm = stmp3xxx_rtc_set_alarm,
  203. };
  204. static int stmp3xxx_rtc_remove(struct platform_device *pdev)
  205. {
  206. struct stmp3xxx_rtc_data *rtc_data = platform_get_drvdata(pdev);
  207. if (!rtc_data)
  208. return 0;
  209. writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
  210. rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
  211. return 0;
  212. }
  213. static int stmp3xxx_rtc_probe(struct platform_device *pdev)
  214. {
  215. struct stmp3xxx_rtc_data *rtc_data;
  216. struct resource *r;
  217. u32 rtc_stat;
  218. u32 pers0_set, pers0_clr;
  219. u32 crystalfreq = 0;
  220. int err;
  221. rtc_data = devm_kzalloc(&pdev->dev, sizeof(*rtc_data), GFP_KERNEL);
  222. if (!rtc_data)
  223. return -ENOMEM;
  224. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  225. if (!r) {
  226. dev_err(&pdev->dev, "failed to get resource\n");
  227. return -ENXIO;
  228. }
  229. rtc_data->io = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  230. if (!rtc_data->io) {
  231. dev_err(&pdev->dev, "ioremap failed\n");
  232. return -EIO;
  233. }
  234. rtc_data->irq_alarm = platform_get_irq(pdev, 0);
  235. rtc_stat = readl(rtc_data->io + STMP3XXX_RTC_STAT);
  236. if (!(rtc_stat & STMP3XXX_RTC_STAT_RTC_PRESENT)) {
  237. dev_err(&pdev->dev, "no device onboard\n");
  238. return -ENODEV;
  239. }
  240. platform_set_drvdata(pdev, rtc_data);
  241. err = stmp_reset_block(rtc_data->io);
  242. if (err) {
  243. dev_err(&pdev->dev, "stmp_reset_block failed: %d\n", err);
  244. return err;
  245. }
  246. /*
  247. * Obviously the rtc needs a clock input to be able to run.
  248. * This clock can be provided by an external 32k crystal. If that one is
  249. * missing XTAL must not be disabled in suspend which consumes a
  250. * lot of power. Normally the presence and exact frequency (supported
  251. * are 32000 Hz and 32768 Hz) is detectable from fuses, but as reality
  252. * proves these fuses are not blown correctly on all machines, so the
  253. * frequency can be overridden in the device tree.
  254. */
  255. if (rtc_stat & STMP3XXX_RTC_STAT_XTAL32000_PRESENT)
  256. crystalfreq = 32000;
  257. else if (rtc_stat & STMP3XXX_RTC_STAT_XTAL32768_PRESENT)
  258. crystalfreq = 32768;
  259. of_property_read_u32(pdev->dev.of_node, "stmp,crystal-freq",
  260. &crystalfreq);
  261. switch (crystalfreq) {
  262. case 32000:
  263. /* keep 32kHz crystal running in low-power mode */
  264. pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ |
  265. STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP |
  266. STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE;
  267. pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP;
  268. break;
  269. case 32768:
  270. /* keep 32.768kHz crystal running in low-power mode */
  271. pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP |
  272. STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE;
  273. pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP |
  274. STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ;
  275. break;
  276. default:
  277. dev_warn(&pdev->dev,
  278. "invalid crystal-freq specified in device-tree. Assuming no crystal\n");
  279. /* fall-through */
  280. case 0:
  281. /* keep XTAL on in low-power mode */
  282. pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP;
  283. pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP |
  284. STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE;
  285. }
  286. writel(pers0_set, rtc_data->io + STMP3XXX_RTC_PERSISTENT0 +
  287. STMP_OFFSET_REG_SET);
  288. writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
  289. STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
  290. STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE | pers0_clr,
  291. rtc_data->io + STMP3XXX_RTC_PERSISTENT0 + STMP_OFFSET_REG_CLR);
  292. writel(STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN |
  293. STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
  294. rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
  295. rtc_data->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  296. &stmp3xxx_rtc_ops, THIS_MODULE);
  297. if (IS_ERR(rtc_data->rtc))
  298. return PTR_ERR(rtc_data->rtc);
  299. err = devm_request_irq(&pdev->dev, rtc_data->irq_alarm,
  300. stmp3xxx_rtc_interrupt, 0, "RTC alarm", &pdev->dev);
  301. if (err) {
  302. dev_err(&pdev->dev, "Cannot claim IRQ%d\n",
  303. rtc_data->irq_alarm);
  304. return err;
  305. }
  306. stmp3xxx_wdt_register(pdev);
  307. return 0;
  308. }
  309. #ifdef CONFIG_PM_SLEEP
  310. static int stmp3xxx_rtc_suspend(struct device *dev)
  311. {
  312. return 0;
  313. }
  314. static int stmp3xxx_rtc_resume(struct device *dev)
  315. {
  316. struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
  317. stmp_reset_block(rtc_data->io);
  318. writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
  319. STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
  320. STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE,
  321. rtc_data->io + STMP3XXX_RTC_PERSISTENT0 + STMP_OFFSET_REG_CLR);
  322. return 0;
  323. }
  324. #endif
  325. static SIMPLE_DEV_PM_OPS(stmp3xxx_rtc_pm_ops, stmp3xxx_rtc_suspend,
  326. stmp3xxx_rtc_resume);
  327. static const struct of_device_id rtc_dt_ids[] = {
  328. { .compatible = "fsl,stmp3xxx-rtc", },
  329. { /* sentinel */ }
  330. };
  331. MODULE_DEVICE_TABLE(of, rtc_dt_ids);
  332. static struct platform_driver stmp3xxx_rtcdrv = {
  333. .probe = stmp3xxx_rtc_probe,
  334. .remove = stmp3xxx_rtc_remove,
  335. .driver = {
  336. .name = "stmp3xxx-rtc",
  337. .pm = &stmp3xxx_rtc_pm_ops,
  338. .of_match_table = rtc_dt_ids,
  339. },
  340. };
  341. module_platform_driver(stmp3xxx_rtcdrv);
  342. MODULE_DESCRIPTION("STMP3xxx RTC Driver");
  343. MODULE_AUTHOR("dmitry pervushin <dpervushin@embeddedalley.com> and "
  344. "Wolfram Sang <w.sang@pengutronix.de>");
  345. MODULE_LICENSE("GPL");