rtc-sh.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SuperH On-Chip RTC Support
  4. *
  5. * Copyright (C) 2006 - 2009 Paul Mundt
  6. * Copyright (C) 2006 Jamie Lenehan
  7. * Copyright (C) 2008 Angelo Castello
  8. * Copyright (C) 2025 Wolfram Sang, Renesas Electronics Corporation
  9. *
  10. * Based on the old arch/sh/kernel/cpu/rtc.c by:
  11. *
  12. * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
  13. * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
  14. */
  15. #include <linux/module.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/kernel.h>
  18. #include <linux/bcd.h>
  19. #include <linux/rtc.h>
  20. #include <linux/init.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/io.h>
  26. #include <linux/log2.h>
  27. #include <linux/clk.h>
  28. #include <linux/slab.h>
  29. #ifdef CONFIG_SUPERH
  30. #include <asm/rtc.h>
  31. #else
  32. /* Default values for RZ/A RTC */
  33. #define rtc_reg_size sizeof(u16)
  34. #define RTC_BIT_INVERTED 0 /* no chip bugs */
  35. #define RTC_CAP_4_DIGIT_YEAR BIT(0)
  36. #define RTC_DEF_CAPABILITIES RTC_CAP_4_DIGIT_YEAR
  37. #endif
  38. #define DRV_NAME "sh-rtc"
  39. #define RTC_REG(r) ((r) * rtc_reg_size)
  40. #define R64CNT RTC_REG(0)
  41. #define RSECCNT RTC_REG(1) /* RTC sec */
  42. #define RMINCNT RTC_REG(2) /* RTC min */
  43. #define RHRCNT RTC_REG(3) /* RTC hour */
  44. #define RWKCNT RTC_REG(4) /* RTC week */
  45. #define RDAYCNT RTC_REG(5) /* RTC day */
  46. #define RMONCNT RTC_REG(6) /* RTC month */
  47. #define RYRCNT RTC_REG(7) /* RTC year */
  48. #define RSECAR RTC_REG(8) /* ALARM sec */
  49. #define RMINAR RTC_REG(9) /* ALARM min */
  50. #define RHRAR RTC_REG(10) /* ALARM hour */
  51. #define RWKAR RTC_REG(11) /* ALARM week */
  52. #define RDAYAR RTC_REG(12) /* ALARM day */
  53. #define RMONAR RTC_REG(13) /* ALARM month */
  54. #define RCR1 RTC_REG(14) /* Control */
  55. #define RCR2 RTC_REG(15) /* Control */
  56. /*
  57. * Note on RYRAR and RCR3: Up until this point most of the register
  58. * definitions are consistent across all of the available parts. However,
  59. * the placement of the optional RYRAR and RCR3 (the RYRAR control
  60. * register used to control RYRCNT/RYRAR compare) varies considerably
  61. * across various parts, occasionally being mapped in to a completely
  62. * unrelated address space. For proper RYRAR support a separate resource
  63. * would have to be handed off, but as this is purely optional in
  64. * practice, we simply opt not to support it, thereby keeping the code
  65. * quite a bit more simplified.
  66. */
  67. /* ALARM Bits - or with BCD encoded value */
  68. #define AR_ENB BIT(7) /* Enable for alarm cmp */
  69. /* RCR1 Bits */
  70. #define RCR1_CF BIT(7) /* Carry Flag */
  71. #define RCR1_CIE BIT(4) /* Carry Interrupt Enable */
  72. #define RCR1_AIE BIT(3) /* Alarm Interrupt Enable */
  73. #define RCR1_AF BIT(0) /* Alarm Flag */
  74. /* RCR2 Bits */
  75. #define RCR2_RTCEN BIT(3) /* ENable RTC */
  76. #define RCR2_ADJ BIT(2) /* ADJustment (30-second) */
  77. #define RCR2_RESET BIT(1) /* Reset bit */
  78. #define RCR2_START BIT(0) /* Start bit */
  79. struct sh_rtc {
  80. void __iomem *regbase;
  81. int alarm_irq;
  82. struct clk *clk;
  83. struct rtc_device *rtc_dev;
  84. spinlock_t lock; /* protecting register access */
  85. unsigned long capabilities; /* See asm/rtc.h for cap bits */
  86. };
  87. static irqreturn_t sh_rtc_alarm(int irq, void *dev_id)
  88. {
  89. struct sh_rtc *rtc = dev_id;
  90. unsigned int tmp, pending;
  91. spin_lock(&rtc->lock);
  92. tmp = readb(rtc->regbase + RCR1);
  93. pending = tmp & RCR1_AF;
  94. tmp &= ~(RCR1_AF | RCR1_AIE);
  95. writeb(tmp, rtc->regbase + RCR1);
  96. if (pending)
  97. rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
  98. spin_unlock(&rtc->lock);
  99. return IRQ_RETVAL(pending);
  100. }
  101. static int sh_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
  102. {
  103. struct sh_rtc *rtc = dev_get_drvdata(dev);
  104. unsigned int tmp;
  105. spin_lock_irq(&rtc->lock);
  106. tmp = readb(rtc->regbase + RCR1);
  107. if (enable)
  108. tmp |= RCR1_AIE;
  109. else
  110. tmp &= ~RCR1_AIE;
  111. writeb(tmp, rtc->regbase + RCR1);
  112. spin_unlock_irq(&rtc->lock);
  113. return 0;
  114. }
  115. static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm)
  116. {
  117. struct sh_rtc *rtc = dev_get_drvdata(dev);
  118. unsigned int sec128, sec2, yr, yr100, cf_bit;
  119. if (!(readb(rtc->regbase + RCR2) & RCR2_RTCEN))
  120. return -EINVAL;
  121. do {
  122. unsigned int tmp;
  123. spin_lock_irq(&rtc->lock);
  124. tmp = readb(rtc->regbase + RCR1);
  125. tmp &= ~RCR1_CF; /* Clear CF-bit */
  126. tmp |= RCR1_CIE;
  127. writeb(tmp, rtc->regbase + RCR1);
  128. sec128 = readb(rtc->regbase + R64CNT);
  129. tm->tm_sec = bcd2bin(readb(rtc->regbase + RSECCNT));
  130. tm->tm_min = bcd2bin(readb(rtc->regbase + RMINCNT));
  131. tm->tm_hour = bcd2bin(readb(rtc->regbase + RHRCNT));
  132. tm->tm_wday = bcd2bin(readb(rtc->regbase + RWKCNT));
  133. tm->tm_mday = bcd2bin(readb(rtc->regbase + RDAYCNT));
  134. tm->tm_mon = bcd2bin(readb(rtc->regbase + RMONCNT)) - 1;
  135. if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
  136. yr = readw(rtc->regbase + RYRCNT);
  137. yr100 = bcd2bin(yr >> 8);
  138. yr &= 0xff;
  139. } else {
  140. yr = readb(rtc->regbase + RYRCNT);
  141. yr100 = bcd2bin((yr == 0x99) ? 0x19 : 0x20);
  142. }
  143. tm->tm_year = (yr100 * 100 + bcd2bin(yr)) - 1900;
  144. sec2 = readb(rtc->regbase + R64CNT);
  145. cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF;
  146. spin_unlock_irq(&rtc->lock);
  147. } while (cf_bit != 0 || ((sec128 ^ sec2) & RTC_BIT_INVERTED) != 0);
  148. #if RTC_BIT_INVERTED != 0
  149. if ((sec128 & RTC_BIT_INVERTED))
  150. tm->tm_sec--;
  151. #endif
  152. dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
  153. __func__, tm->tm_sec, tm->tm_min, tm->tm_hour,
  154. tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
  155. return 0;
  156. }
  157. static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm)
  158. {
  159. struct sh_rtc *rtc = dev_get_drvdata(dev);
  160. unsigned int tmp;
  161. int year;
  162. spin_lock_irq(&rtc->lock);
  163. /* Reset pre-scaler & stop RTC */
  164. tmp = readb(rtc->regbase + RCR2);
  165. tmp |= RCR2_RESET;
  166. tmp &= ~RCR2_START;
  167. writeb(tmp, rtc->regbase + RCR2);
  168. writeb(bin2bcd(tm->tm_sec), rtc->regbase + RSECCNT);
  169. writeb(bin2bcd(tm->tm_min), rtc->regbase + RMINCNT);
  170. writeb(bin2bcd(tm->tm_hour), rtc->regbase + RHRCNT);
  171. writeb(bin2bcd(tm->tm_wday), rtc->regbase + RWKCNT);
  172. writeb(bin2bcd(tm->tm_mday), rtc->regbase + RDAYCNT);
  173. writeb(bin2bcd(tm->tm_mon + 1), rtc->regbase + RMONCNT);
  174. if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
  175. year = (bin2bcd((tm->tm_year + 1900) / 100) << 8) |
  176. bin2bcd(tm->tm_year % 100);
  177. writew(year, rtc->regbase + RYRCNT);
  178. } else {
  179. year = tm->tm_year % 100;
  180. writeb(bin2bcd(year), rtc->regbase + RYRCNT);
  181. }
  182. /* Start RTC */
  183. tmp = readb(rtc->regbase + RCR2);
  184. tmp &= ~RCR2_RESET;
  185. tmp |= RCR2_RTCEN | RCR2_START;
  186. writeb(tmp, rtc->regbase + RCR2);
  187. spin_unlock_irq(&rtc->lock);
  188. return 0;
  189. }
  190. static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off)
  191. {
  192. unsigned int byte;
  193. int value = -1; /* return -1 for ignored values */
  194. byte = readb(rtc->regbase + reg_off);
  195. if (byte & AR_ENB) {
  196. byte &= ~AR_ENB; /* strip the enable bit */
  197. value = bcd2bin(byte);
  198. }
  199. return value;
  200. }
  201. static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  202. {
  203. struct sh_rtc *rtc = dev_get_drvdata(dev);
  204. struct rtc_time *tm = &wkalrm->time;
  205. spin_lock_irq(&rtc->lock);
  206. tm->tm_sec = sh_rtc_read_alarm_value(rtc, RSECAR);
  207. tm->tm_min = sh_rtc_read_alarm_value(rtc, RMINAR);
  208. tm->tm_hour = sh_rtc_read_alarm_value(rtc, RHRAR);
  209. tm->tm_wday = sh_rtc_read_alarm_value(rtc, RWKAR);
  210. tm->tm_mday = sh_rtc_read_alarm_value(rtc, RDAYAR);
  211. tm->tm_mon = sh_rtc_read_alarm_value(rtc, RMONAR);
  212. if (tm->tm_mon > 0)
  213. tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */
  214. wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0;
  215. spin_unlock_irq(&rtc->lock);
  216. return 0;
  217. }
  218. static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc,
  219. int value, int reg_off)
  220. {
  221. /* < 0 for a value that is ignored */
  222. if (value < 0)
  223. writeb(0, rtc->regbase + reg_off);
  224. else
  225. writeb(bin2bcd(value) | AR_ENB, rtc->regbase + reg_off);
  226. }
  227. static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  228. {
  229. struct sh_rtc *rtc = dev_get_drvdata(dev);
  230. unsigned int rcr1;
  231. struct rtc_time *tm = &wkalrm->time;
  232. int mon;
  233. spin_lock_irq(&rtc->lock);
  234. /* disable alarm interrupt and clear the alarm flag */
  235. rcr1 = readb(rtc->regbase + RCR1);
  236. rcr1 &= ~(RCR1_AF | RCR1_AIE);
  237. writeb(rcr1, rtc->regbase + RCR1);
  238. /* set alarm time */
  239. sh_rtc_write_alarm_value(rtc, tm->tm_sec, RSECAR);
  240. sh_rtc_write_alarm_value(rtc, tm->tm_min, RMINAR);
  241. sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR);
  242. sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR);
  243. sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR);
  244. mon = tm->tm_mon;
  245. if (mon >= 0)
  246. mon += 1;
  247. sh_rtc_write_alarm_value(rtc, mon, RMONAR);
  248. if (wkalrm->enabled) {
  249. rcr1 |= RCR1_AIE;
  250. writeb(rcr1, rtc->regbase + RCR1);
  251. }
  252. spin_unlock_irq(&rtc->lock);
  253. return 0;
  254. }
  255. static const struct rtc_class_ops sh_rtc_ops = {
  256. .read_time = sh_rtc_read_time,
  257. .set_time = sh_rtc_set_time,
  258. .read_alarm = sh_rtc_read_alarm,
  259. .set_alarm = sh_rtc_set_alarm,
  260. .alarm_irq_enable = sh_rtc_alarm_irq_enable,
  261. };
  262. static int __init sh_rtc_probe(struct platform_device *pdev)
  263. {
  264. struct sh_rtc *rtc;
  265. struct resource *res, *req_res;
  266. char clk_name[14];
  267. int clk_id, ret;
  268. unsigned int tmp;
  269. resource_size_t regsize;
  270. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  271. if (unlikely(!rtc))
  272. return -ENOMEM;
  273. spin_lock_init(&rtc->lock);
  274. ret = platform_get_irq(pdev, 0);
  275. if (unlikely(ret <= 0)) {
  276. dev_err(&pdev->dev, "No IRQ resource\n");
  277. return -ENOENT;
  278. }
  279. if (!pdev->dev.of_node)
  280. rtc->alarm_irq = platform_get_irq(pdev, 2);
  281. else
  282. rtc->alarm_irq = ret;
  283. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  284. if (!res)
  285. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  286. if (!res) {
  287. dev_err(&pdev->dev, "No IO resource\n");
  288. return -ENOENT;
  289. }
  290. regsize = resource_size(res);
  291. req_res = devm_request_mem_region(&pdev->dev, res->start, regsize, pdev->name);
  292. if (!req_res)
  293. return -EBUSY;
  294. rtc->regbase = devm_ioremap(&pdev->dev, req_res->start, regsize);
  295. if (!rtc->regbase)
  296. return -EINVAL;
  297. if (!pdev->dev.of_node) {
  298. clk_id = pdev->id;
  299. /* With a single device, the clock id is still "rtc0" */
  300. if (clk_id < 0)
  301. clk_id = 0;
  302. snprintf(clk_name, sizeof(clk_name), "rtc%d", clk_id);
  303. } else {
  304. snprintf(clk_name, sizeof(clk_name), "fck");
  305. }
  306. rtc->clk = devm_clk_get(&pdev->dev, clk_name);
  307. if (IS_ERR(rtc->clk)) {
  308. /*
  309. * No error handling for rtc->clk intentionally, not all
  310. * platforms will have a unique clock for the RTC, and
  311. * the clk API can handle the struct clk pointer being
  312. * NULL.
  313. */
  314. rtc->clk = NULL;
  315. }
  316. rtc->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
  317. if (IS_ERR(rtc->rtc_dev))
  318. return PTR_ERR(rtc->rtc_dev);
  319. clk_enable(rtc->clk);
  320. rtc->capabilities = RTC_DEF_CAPABILITIES;
  321. #ifdef CONFIG_SUPERH
  322. if (dev_get_platdata(&pdev->dev)) {
  323. struct sh_rtc_platform_info *pinfo =
  324. dev_get_platdata(&pdev->dev);
  325. /*
  326. * Some CPUs have special capabilities in addition to the
  327. * default set. Add those in here.
  328. */
  329. rtc->capabilities |= pinfo->capabilities;
  330. }
  331. #endif
  332. ret = devm_request_irq(&pdev->dev, rtc->alarm_irq, sh_rtc_alarm, 0, "sh-rtc", rtc);
  333. if (ret) {
  334. dev_err(&pdev->dev, "request alarm IRQ failed with %d, IRQ %d\n",
  335. ret, rtc->alarm_irq);
  336. goto err_unmap;
  337. }
  338. platform_set_drvdata(pdev, rtc);
  339. /* everything disabled by default */
  340. tmp = readb(rtc->regbase + RCR1);
  341. tmp &= ~(RCR1_CIE | RCR1_AIE);
  342. writeb(tmp, rtc->regbase + RCR1);
  343. rtc->rtc_dev->ops = &sh_rtc_ops;
  344. if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
  345. rtc->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_1900;
  346. rtc->rtc_dev->range_max = RTC_TIMESTAMP_END_9999;
  347. } else {
  348. rtc->rtc_dev->range_min = mktime64(1999, 1, 1, 0, 0, 0);
  349. rtc->rtc_dev->range_max = mktime64(2098, 12, 31, 23, 59, 59);
  350. }
  351. ret = devm_rtc_register_device(rtc->rtc_dev);
  352. if (ret)
  353. goto err_unmap;
  354. device_init_wakeup(&pdev->dev, true);
  355. return 0;
  356. err_unmap:
  357. clk_disable(rtc->clk);
  358. return ret;
  359. }
  360. static void __exit sh_rtc_remove(struct platform_device *pdev)
  361. {
  362. struct sh_rtc *rtc = platform_get_drvdata(pdev);
  363. sh_rtc_alarm_irq_enable(&pdev->dev, 0);
  364. clk_disable(rtc->clk);
  365. }
  366. static int sh_rtc_suspend(struct device *dev)
  367. {
  368. struct sh_rtc *rtc = dev_get_drvdata(dev);
  369. if (device_may_wakeup(dev))
  370. irq_set_irq_wake(rtc->alarm_irq, 1);
  371. return 0;
  372. }
  373. static int sh_rtc_resume(struct device *dev)
  374. {
  375. struct sh_rtc *rtc = dev_get_drvdata(dev);
  376. if (device_may_wakeup(dev))
  377. irq_set_irq_wake(rtc->alarm_irq, 0);
  378. return 0;
  379. }
  380. static DEFINE_SIMPLE_DEV_PM_OPS(sh_rtc_pm_ops, sh_rtc_suspend, sh_rtc_resume);
  381. static const struct of_device_id sh_rtc_of_match[] = {
  382. { .compatible = "renesas,sh-rtc", },
  383. { /* sentinel */ }
  384. };
  385. MODULE_DEVICE_TABLE(of, sh_rtc_of_match);
  386. /*
  387. * sh_rtc_remove() lives in .exit.text. For drivers registered via
  388. * module_platform_driver_probe() this is ok because they cannot get unbound at
  389. * runtime. So mark the driver struct with __refdata to prevent modpost
  390. * triggering a section mismatch warning.
  391. */
  392. static struct platform_driver sh_rtc_platform_driver __refdata = {
  393. .driver = {
  394. .name = DRV_NAME,
  395. .pm = pm_sleep_ptr(&sh_rtc_pm_ops),
  396. .of_match_table = sh_rtc_of_match,
  397. },
  398. .remove = __exit_p(sh_rtc_remove),
  399. };
  400. module_platform_driver_probe(sh_rtc_platform_driver, sh_rtc_probe);
  401. MODULE_DESCRIPTION("SuperH on-chip RTC driver");
  402. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
  403. MODULE_AUTHOR("Jamie Lenehan <lenehan@twibble.org>");
  404. MODULE_AUTHOR("Angelo Castello <angelo.castello@st.com>");
  405. MODULE_LICENSE("GPL v2");
  406. MODULE_ALIAS("platform:" DRV_NAME);