rtc-stmp3xxx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. int rc = -1;
  97. struct platform_device *wdt_pdev =
  98. platform_device_alloc("stmp3xxx_rtc_wdt", rtc_pdev->id);
  99. if (wdt_pdev) {
  100. wdt_pdev->dev.parent = &rtc_pdev->dev;
  101. wdt_pdev->dev.platform_data = &wdt_pdata;
  102. rc = platform_device_add(wdt_pdev);
  103. }
  104. if (rc)
  105. dev_err(&rtc_pdev->dev,
  106. "failed to register stmp3xxx_rtc_wdt\n");
  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 + STMP_OFFSET_REG_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 +
  172. STMP_OFFSET_REG_SET);
  173. writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
  174. rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_SET);
  175. } else {
  176. writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
  177. STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN,
  178. rtc_data->io + STMP3XXX_RTC_PERSISTENT0 +
  179. STMP_OFFSET_REG_CLR);
  180. writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
  181. rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
  182. }
  183. return 0;
  184. }
  185. static int stmp3xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  186. {
  187. struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
  188. rtc_time_to_tm(readl(rtc_data->io + STMP3XXX_RTC_ALARM), &alm->time);
  189. return 0;
  190. }
  191. static int stmp3xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  192. {
  193. unsigned long t;
  194. struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
  195. rtc_tm_to_time(&alm->time, &t);
  196. writel(t, rtc_data->io + STMP3XXX_RTC_ALARM);
  197. stmp3xxx_alarm_irq_enable(dev, alm->enabled);
  198. return 0;
  199. }
  200. static const struct rtc_class_ops stmp3xxx_rtc_ops = {
  201. .alarm_irq_enable =
  202. stmp3xxx_alarm_irq_enable,
  203. .read_time = stmp3xxx_rtc_gettime,
  204. .set_mmss = stmp3xxx_rtc_set_mmss,
  205. .read_alarm = stmp3xxx_rtc_read_alarm,
  206. .set_alarm = stmp3xxx_rtc_set_alarm,
  207. };
  208. static int stmp3xxx_rtc_remove(struct platform_device *pdev)
  209. {
  210. struct stmp3xxx_rtc_data *rtc_data = platform_get_drvdata(pdev);
  211. if (!rtc_data)
  212. return 0;
  213. writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
  214. rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
  215. return 0;
  216. }
  217. static int stmp3xxx_rtc_probe(struct platform_device *pdev)
  218. {
  219. struct stmp3xxx_rtc_data *rtc_data;
  220. struct resource *r;
  221. u32 rtc_stat;
  222. u32 pers0_set, pers0_clr;
  223. u32 crystalfreq = 0;
  224. int err;
  225. rtc_data = devm_kzalloc(&pdev->dev, sizeof(*rtc_data), GFP_KERNEL);
  226. if (!rtc_data)
  227. return -ENOMEM;
  228. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  229. if (!r) {
  230. dev_err(&pdev->dev, "failed to get resource\n");
  231. return -ENXIO;
  232. }
  233. rtc_data->io = devm_ioremap(&pdev->dev, r->start, resource_size(r));
  234. if (!rtc_data->io) {
  235. dev_err(&pdev->dev, "ioremap failed\n");
  236. return -EIO;
  237. }
  238. rtc_data->irq_alarm = platform_get_irq(pdev, 0);
  239. rtc_stat = readl(rtc_data->io + STMP3XXX_RTC_STAT);
  240. if (!(rtc_stat & STMP3XXX_RTC_STAT_RTC_PRESENT)) {
  241. dev_err(&pdev->dev, "no device onboard\n");
  242. return -ENODEV;
  243. }
  244. platform_set_drvdata(pdev, rtc_data);
  245. err = stmp_reset_block(rtc_data->io);
  246. if (err) {
  247. dev_err(&pdev->dev, "stmp_reset_block failed: %d\n", err);
  248. return err;
  249. }
  250. /*
  251. * Obviously the rtc needs a clock input to be able to run.
  252. * This clock can be provided by an external 32k crystal. If that one is
  253. * missing XTAL must not be disabled in suspend which consumes a
  254. * lot of power. Normally the presence and exact frequency (supported
  255. * are 32000 Hz and 32768 Hz) is detectable from fuses, but as reality
  256. * proves these fuses are not blown correctly on all machines, so the
  257. * frequency can be overridden in the device tree.
  258. */
  259. if (rtc_stat & STMP3XXX_RTC_STAT_XTAL32000_PRESENT)
  260. crystalfreq = 32000;
  261. else if (rtc_stat & STMP3XXX_RTC_STAT_XTAL32768_PRESENT)
  262. crystalfreq = 32768;
  263. of_property_read_u32(pdev->dev.of_node, "stmp,crystal-freq",
  264. &crystalfreq);
  265. switch (crystalfreq) {
  266. case 32000:
  267. /* keep 32kHz crystal running in low-power mode */
  268. pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ |
  269. STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP |
  270. STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE;
  271. pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP;
  272. break;
  273. case 32768:
  274. /* keep 32.768kHz crystal running in low-power mode */
  275. pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP |
  276. STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE;
  277. pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP |
  278. STMP3XXX_RTC_PERSISTENT0_XTAL32_FREQ;
  279. break;
  280. default:
  281. dev_warn(&pdev->dev,
  282. "invalid crystal-freq specified in device-tree. Assuming no crystal\n");
  283. /* fall-through */
  284. case 0:
  285. /* keep XTAL on in low-power mode */
  286. pers0_set = STMP3XXX_RTC_PERSISTENT0_XTAL24MHZ_PWRUP;
  287. pers0_clr = STMP3XXX_RTC_PERSISTENT0_XTAL32KHZ_PWRUP |
  288. STMP3XXX_RTC_PERSISTENT0_CLOCKSOURCE;
  289. }
  290. writel(pers0_set, rtc_data->io + STMP3XXX_RTC_PERSISTENT0 +
  291. STMP_OFFSET_REG_SET);
  292. writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
  293. STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
  294. STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE | pers0_clr,
  295. rtc_data->io + STMP3XXX_RTC_PERSISTENT0 + STMP_OFFSET_REG_CLR);
  296. writel(STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN |
  297. STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
  298. rtc_data->io + STMP3XXX_RTC_CTRL + STMP_OFFSET_REG_CLR);
  299. rtc_data->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  300. &stmp3xxx_rtc_ops, THIS_MODULE);
  301. if (IS_ERR(rtc_data->rtc))
  302. return PTR_ERR(rtc_data->rtc);
  303. err = devm_request_irq(&pdev->dev, rtc_data->irq_alarm,
  304. stmp3xxx_rtc_interrupt, 0, "RTC alarm", &pdev->dev);
  305. if (err) {
  306. dev_err(&pdev->dev, "Cannot claim IRQ%d\n",
  307. rtc_data->irq_alarm);
  308. return err;
  309. }
  310. stmp3xxx_wdt_register(pdev);
  311. return 0;
  312. }
  313. #ifdef CONFIG_PM_SLEEP
  314. static int stmp3xxx_rtc_suspend(struct device *dev)
  315. {
  316. return 0;
  317. }
  318. static int stmp3xxx_rtc_resume(struct device *dev)
  319. {
  320. struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
  321. stmp_reset_block(rtc_data->io);
  322. writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
  323. STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
  324. STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE,
  325. rtc_data->io + STMP3XXX_RTC_PERSISTENT0 + STMP_OFFSET_REG_CLR);
  326. return 0;
  327. }
  328. #endif
  329. static SIMPLE_DEV_PM_OPS(stmp3xxx_rtc_pm_ops, stmp3xxx_rtc_suspend,
  330. stmp3xxx_rtc_resume);
  331. static const struct of_device_id rtc_dt_ids[] = {
  332. { .compatible = "fsl,stmp3xxx-rtc", },
  333. { /* sentinel */ }
  334. };
  335. MODULE_DEVICE_TABLE(of, rtc_dt_ids);
  336. static struct platform_driver stmp3xxx_rtcdrv = {
  337. .probe = stmp3xxx_rtc_probe,
  338. .remove = stmp3xxx_rtc_remove,
  339. .driver = {
  340. .name = "stmp3xxx-rtc",
  341. .pm = &stmp3xxx_rtc_pm_ops,
  342. .of_match_table = rtc_dt_ids,
  343. },
  344. };
  345. module_platform_driver(stmp3xxx_rtcdrv);
  346. MODULE_DESCRIPTION("STMP3xxx RTC Driver");
  347. MODULE_AUTHOR("dmitry pervushin <dpervushin@embeddedalley.com> and "
  348. "Wolfram Sang <w.sang@pengutronix.de>");
  349. MODULE_LICENSE("GPL");