rtc-stmp3xxx.c 12 KB

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