rtc-mpc5121.c 10 KB

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