rtc-mpc5121.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Real-time clock driver for MPC5121
  4. *
  5. * Copyright 2007, Domen Puncer <[email protected]>
  6. * Copyright 2008, Freescale Semiconductor, Inc. All rights reserved.
  7. * Copyright 2011, Dmitry Eremin-Solenikov
  8. */
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/rtc.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_device.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/io.h>
  18. #include <linux/slab.h>
  19. struct mpc5121_rtc_regs {
  20. u8 set_time; /* RTC + 0x00 */
  21. u8 hour_set; /* RTC + 0x01 */
  22. u8 minute_set; /* RTC + 0x02 */
  23. u8 second_set; /* RTC + 0x03 */
  24. u8 set_date; /* RTC + 0x04 */
  25. u8 month_set; /* RTC + 0x05 */
  26. u8 weekday_set; /* RTC + 0x06 */
  27. u8 date_set; /* RTC + 0x07 */
  28. u8 write_sw; /* RTC + 0x08 */
  29. u8 sw_set; /* RTC + 0x09 */
  30. u16 year_set; /* RTC + 0x0a */
  31. u8 alm_enable; /* RTC + 0x0c */
  32. u8 alm_hour_set; /* RTC + 0x0d */
  33. u8 alm_min_set; /* RTC + 0x0e */
  34. u8 int_enable; /* RTC + 0x0f */
  35. u8 reserved1;
  36. u8 hour; /* RTC + 0x11 */
  37. u8 minute; /* RTC + 0x12 */
  38. u8 second; /* RTC + 0x13 */
  39. u8 month; /* RTC + 0x14 */
  40. u8 wday_mday; /* RTC + 0x15 */
  41. u16 year; /* RTC + 0x16 */
  42. u8 int_alm; /* RTC + 0x18 */
  43. u8 int_sw; /* RTC + 0x19 */
  44. u8 alm_status; /* RTC + 0x1a */
  45. u8 sw_minute; /* RTC + 0x1b */
  46. u8 bus_error_1; /* RTC + 0x1c */
  47. u8 int_day; /* RTC + 0x1d */
  48. u8 int_min; /* RTC + 0x1e */
  49. u8 int_sec; /* RTC + 0x1f */
  50. /*
  51. * target_time:
  52. * intended to be used for hibernation but hibernation
  53. * does not work on silicon rev 1.5 so use it for non-volatile
  54. * storage of offset between the actual_time register and linux
  55. * time
  56. */
  57. u32 target_time; /* RTC + 0x20 */
  58. /*
  59. * actual_time:
  60. * readonly time since VBAT_RTC was last connected
  61. */
  62. u32 actual_time; /* RTC + 0x24 */
  63. u32 keep_alive; /* RTC + 0x28 */
  64. };
  65. struct mpc5121_rtc_data {
  66. unsigned irq;
  67. unsigned irq_periodic;
  68. struct mpc5121_rtc_regs __iomem *regs;
  69. struct rtc_device *rtc;
  70. struct rtc_wkalrm wkalarm;
  71. };
  72. /*
  73. * Update second/minute/hour registers.
  74. *
  75. * This is just so alarm will work.
  76. */
  77. static void mpc5121_rtc_update_smh(struct mpc5121_rtc_regs __iomem *regs,
  78. struct rtc_time *tm)
  79. {
  80. out_8(&regs->second_set, tm->tm_sec);
  81. out_8(&regs->minute_set, tm->tm_min);
  82. out_8(&regs->hour_set, tm->tm_hour);
  83. /* set time sequence */
  84. out_8(&regs->set_time, 0x1);
  85. out_8(&regs->set_time, 0x3);
  86. out_8(&regs->set_time, 0x1);
  87. out_8(&regs->set_time, 0x0);
  88. }
  89. static int mpc5121_rtc_read_time(struct device *dev, struct rtc_time *tm)
  90. {
  91. struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
  92. struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
  93. unsigned long now;
  94. /*
  95. * linux time is actual_time plus the offset saved in target_time
  96. */
  97. now = in_be32(&regs->actual_time) + in_be32(&regs->target_time);
  98. rtc_time64_to_tm(now, tm);
  99. /*
  100. * update second minute hour registers
  101. * so alarms will work
  102. */
  103. mpc5121_rtc_update_smh(regs, tm);
  104. return 0;
  105. }
  106. static int mpc5121_rtc_set_time(struct device *dev, struct rtc_time *tm)
  107. {
  108. struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
  109. struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
  110. unsigned long now;
  111. /*
  112. * The actual_time register is read only so we write the offset
  113. * between it and linux time to the target_time register.
  114. */
  115. now = rtc_tm_to_time64(tm);
  116. out_be32(&regs->target_time, now - in_be32(&regs->actual_time));
  117. /*
  118. * update second minute hour registers
  119. * so alarms will work
  120. */
  121. mpc5121_rtc_update_smh(regs, tm);
  122. return 0;
  123. }
  124. static int mpc5200_rtc_read_time(struct device *dev, struct rtc_time *tm)
  125. {
  126. struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
  127. struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
  128. int tmp;
  129. tm->tm_sec = in_8(&regs->second);
  130. tm->tm_min = in_8(&regs->minute);
  131. /* 12 hour format? */
  132. if (in_8(&regs->hour) & 0x20)
  133. tm->tm_hour = (in_8(&regs->hour) >> 1) +
  134. (in_8(&regs->hour) & 1 ? 12 : 0);
  135. else
  136. tm->tm_hour = in_8(&regs->hour);
  137. tmp = in_8(&regs->wday_mday);
  138. tm->tm_mday = tmp & 0x1f;
  139. tm->tm_mon = in_8(&regs->month) - 1;
  140. tm->tm_year = in_be16(&regs->year) - 1900;
  141. tm->tm_wday = (tmp >> 5) % 7;
  142. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
  143. tm->tm_isdst = 0;
  144. return 0;
  145. }
  146. static int mpc5200_rtc_set_time(struct device *dev, struct rtc_time *tm)
  147. {
  148. struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
  149. struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
  150. mpc5121_rtc_update_smh(regs, tm);
  151. /* date */
  152. out_8(&regs->month_set, tm->tm_mon + 1);
  153. out_8(&regs->weekday_set, tm->tm_wday ? tm->tm_wday : 7);
  154. out_8(&regs->date_set, tm->tm_mday);
  155. out_be16(&regs->year_set, tm->tm_year + 1900);
  156. /* set date sequence */
  157. out_8(&regs->set_date, 0x1);
  158. out_8(&regs->set_date, 0x3);
  159. out_8(&regs->set_date, 0x1);
  160. out_8(&regs->set_date, 0x0);
  161. return 0;
  162. }
  163. static int mpc5121_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  164. {
  165. struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
  166. struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
  167. *alarm = rtc->wkalarm;
  168. alarm->pending = in_8(&regs->alm_status);
  169. return 0;
  170. }
  171. static int mpc5121_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  172. {
  173. struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
  174. struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
  175. alarm->time.tm_mday = -1;
  176. alarm->time.tm_mon = -1;
  177. alarm->time.tm_year = -1;
  178. out_8(&regs->alm_min_set, alarm->time.tm_min);
  179. out_8(&regs->alm_hour_set, alarm->time.tm_hour);
  180. out_8(&regs->alm_enable, alarm->enabled);
  181. rtc->wkalarm = *alarm;
  182. return 0;
  183. }
  184. static irqreturn_t mpc5121_rtc_handler(int irq, void *dev)
  185. {
  186. struct mpc5121_rtc_data *rtc = dev_get_drvdata((struct device *)dev);
  187. struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
  188. if (in_8(&regs->int_alm)) {
  189. /* acknowledge and clear status */
  190. out_8(&regs->int_alm, 1);
  191. out_8(&regs->alm_status, 1);
  192. rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
  193. return IRQ_HANDLED;
  194. }
  195. return IRQ_NONE;
  196. }
  197. static irqreturn_t mpc5121_rtc_handler_upd(int irq, void *dev)
  198. {
  199. struct mpc5121_rtc_data *rtc = dev_get_drvdata((struct device *)dev);
  200. struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
  201. if (in_8(&regs->int_sec) && (in_8(&regs->int_enable) & 0x1)) {
  202. /* acknowledge */
  203. out_8(&regs->int_sec, 1);
  204. rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_UF);
  205. return IRQ_HANDLED;
  206. }
  207. return IRQ_NONE;
  208. }
  209. static int mpc5121_rtc_alarm_irq_enable(struct device *dev,
  210. unsigned int enabled)
  211. {
  212. struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
  213. struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
  214. int val;
  215. if (enabled)
  216. val = 1;
  217. else
  218. val = 0;
  219. out_8(&regs->alm_enable, val);
  220. rtc->wkalarm.enabled = val;
  221. return 0;
  222. }
  223. static const struct rtc_class_ops mpc5121_rtc_ops = {
  224. .read_time = mpc5121_rtc_read_time,
  225. .set_time = mpc5121_rtc_set_time,
  226. .read_alarm = mpc5121_rtc_read_alarm,
  227. .set_alarm = mpc5121_rtc_set_alarm,
  228. .alarm_irq_enable = mpc5121_rtc_alarm_irq_enable,
  229. };
  230. static const struct rtc_class_ops mpc5200_rtc_ops = {
  231. .read_time = mpc5200_rtc_read_time,
  232. .set_time = mpc5200_rtc_set_time,
  233. .read_alarm = mpc5121_rtc_read_alarm,
  234. .set_alarm = mpc5121_rtc_set_alarm,
  235. .alarm_irq_enable = mpc5121_rtc_alarm_irq_enable,
  236. };
  237. static int mpc5121_rtc_probe(struct platform_device *op)
  238. {
  239. struct mpc5121_rtc_data *rtc;
  240. int err = 0;
  241. rtc = devm_kzalloc(&op->dev, sizeof(*rtc), GFP_KERNEL);
  242. if (!rtc)
  243. return -ENOMEM;
  244. rtc->regs = devm_platform_ioremap_resource(op, 0);
  245. if (IS_ERR(rtc->regs)) {
  246. dev_err(&op->dev, "%s: couldn't map io space\n", __func__);
  247. return PTR_ERR(rtc->regs);
  248. }
  249. device_init_wakeup(&op->dev, 1);
  250. platform_set_drvdata(op, rtc);
  251. rtc->irq = irq_of_parse_and_map(op->dev.of_node, 1);
  252. err = devm_request_irq(&op->dev, rtc->irq, mpc5121_rtc_handler, 0,
  253. "mpc5121-rtc", &op->dev);
  254. if (err) {
  255. dev_err(&op->dev, "%s: could not request irq: %i\n",
  256. __func__, rtc->irq);
  257. goto out_dispose;
  258. }
  259. rtc->irq_periodic = irq_of_parse_and_map(op->dev.of_node, 0);
  260. err = devm_request_irq(&op->dev, rtc->irq_periodic,
  261. mpc5121_rtc_handler_upd, 0, "mpc5121-rtc_upd",
  262. &op->dev);
  263. if (err) {
  264. dev_err(&op->dev, "%s: could not request irq: %i\n",
  265. __func__, rtc->irq_periodic);
  266. goto out_dispose2;
  267. }
  268. rtc->rtc = devm_rtc_allocate_device(&op->dev);
  269. if (IS_ERR(rtc->rtc)) {
  270. err = PTR_ERR(rtc->rtc);
  271. goto out_dispose2;
  272. }
  273. rtc->rtc->ops = &mpc5200_rtc_ops;
  274. set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->rtc->features);
  275. clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->rtc->features);
  276. rtc->rtc->range_min = RTC_TIMESTAMP_BEGIN_0000;
  277. rtc->rtc->range_max = 65733206399ULL; /* 4052-12-31 23:59:59 */
  278. if (of_device_is_compatible(op->dev.of_node, "fsl,mpc5121-rtc")) {
  279. u32 ka;
  280. ka = in_be32(&rtc->regs->keep_alive);
  281. if (ka & 0x02) {
  282. dev_warn(&op->dev,
  283. "mpc5121-rtc: Battery or oscillator failure!\n");
  284. out_be32(&rtc->regs->keep_alive, ka);
  285. }
  286. rtc->rtc->ops = &mpc5121_rtc_ops;
  287. /*
  288. * This is a limitation of the driver that abuses the target
  289. * time register, the actual maximum year for the mpc5121 is
  290. * also 4052.
  291. */
  292. rtc->rtc->range_min = 0;
  293. rtc->rtc->range_max = U32_MAX;
  294. }
  295. err = devm_rtc_register_device(rtc->rtc);
  296. if (err)
  297. goto out_dispose2;
  298. return 0;
  299. out_dispose2:
  300. irq_dispose_mapping(rtc->irq_periodic);
  301. out_dispose:
  302. irq_dispose_mapping(rtc->irq);
  303. return err;
  304. }
  305. static int mpc5121_rtc_remove(struct platform_device *op)
  306. {
  307. struct mpc5121_rtc_data *rtc = platform_get_drvdata(op);
  308. struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
  309. /* disable interrupt, so there are no nasty surprises */
  310. out_8(&regs->alm_enable, 0);
  311. out_8(&regs->int_enable, in_8(&regs->int_enable) & ~0x1);
  312. irq_dispose_mapping(rtc->irq);
  313. irq_dispose_mapping(rtc->irq_periodic);
  314. return 0;
  315. }
  316. #ifdef CONFIG_OF
  317. static const struct of_device_id mpc5121_rtc_match[] = {
  318. { .compatible = "fsl,mpc5121-rtc", },
  319. { .compatible = "fsl,mpc5200-rtc", },
  320. {},
  321. };
  322. MODULE_DEVICE_TABLE(of, mpc5121_rtc_match);
  323. #endif
  324. static struct platform_driver mpc5121_rtc_driver = {
  325. .driver = {
  326. .name = "mpc5121-rtc",
  327. .of_match_table = of_match_ptr(mpc5121_rtc_match),
  328. },
  329. .probe = mpc5121_rtc_probe,
  330. .remove = mpc5121_rtc_remove,
  331. };
  332. module_platform_driver(mpc5121_rtc_driver);
  333. MODULE_LICENSE("GPL");
  334. MODULE_AUTHOR("John Rigby <[email protected]>");