rtc-mv.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Driver for the RTC in Marvell SoCs.
  3. *
  4. * This file is licensed under the terms of the GNU General Public
  5. * License version 2. This program is licensed "as is" without any
  6. * warranty of any kind, whether express or implied.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/rtc.h>
  11. #include <linux/bcd.h>
  12. #include <linux/bitops.h>
  13. #include <linux/io.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/of.h>
  16. #include <linux/delay.h>
  17. #include <linux/clk.h>
  18. #include <linux/gfp.h>
  19. #include <linux/module.h>
  20. #define RTC_TIME_REG_OFFS 0
  21. #define RTC_SECONDS_OFFS 0
  22. #define RTC_MINUTES_OFFS 8
  23. #define RTC_HOURS_OFFS 16
  24. #define RTC_WDAY_OFFS 24
  25. #define RTC_HOURS_12H_MODE BIT(22) /* 12 hour mode */
  26. #define RTC_DATE_REG_OFFS 4
  27. #define RTC_MDAY_OFFS 0
  28. #define RTC_MONTH_OFFS 8
  29. #define RTC_YEAR_OFFS 16
  30. #define RTC_ALARM_TIME_REG_OFFS 8
  31. #define RTC_ALARM_DATE_REG_OFFS 0xc
  32. #define RTC_ALARM_VALID BIT(7)
  33. #define RTC_ALARM_INTERRUPT_MASK_REG_OFFS 0x10
  34. #define RTC_ALARM_INTERRUPT_CASUE_REG_OFFS 0x14
  35. struct rtc_plat_data {
  36. struct rtc_device *rtc;
  37. void __iomem *ioaddr;
  38. int irq;
  39. struct clk *clk;
  40. };
  41. static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm)
  42. {
  43. struct rtc_plat_data *pdata = dev_get_drvdata(dev);
  44. void __iomem *ioaddr = pdata->ioaddr;
  45. u32 rtc_reg;
  46. rtc_reg = (bin2bcd(tm->tm_sec) << RTC_SECONDS_OFFS) |
  47. (bin2bcd(tm->tm_min) << RTC_MINUTES_OFFS) |
  48. (bin2bcd(tm->tm_hour) << RTC_HOURS_OFFS) |
  49. (bin2bcd(tm->tm_wday) << RTC_WDAY_OFFS);
  50. writel(rtc_reg, ioaddr + RTC_TIME_REG_OFFS);
  51. rtc_reg = (bin2bcd(tm->tm_mday) << RTC_MDAY_OFFS) |
  52. (bin2bcd(tm->tm_mon + 1) << RTC_MONTH_OFFS) |
  53. (bin2bcd(tm->tm_year % 100) << RTC_YEAR_OFFS);
  54. writel(rtc_reg, ioaddr + RTC_DATE_REG_OFFS);
  55. return 0;
  56. }
  57. static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm)
  58. {
  59. struct rtc_plat_data *pdata = dev_get_drvdata(dev);
  60. void __iomem *ioaddr = pdata->ioaddr;
  61. u32 rtc_time, rtc_date;
  62. unsigned int year, month, day, hour, minute, second, wday;
  63. rtc_time = readl(ioaddr + RTC_TIME_REG_OFFS);
  64. rtc_date = readl(ioaddr + RTC_DATE_REG_OFFS);
  65. second = rtc_time & 0x7f;
  66. minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
  67. hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hour mode */
  68. wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
  69. day = rtc_date & 0x3f;
  70. month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
  71. year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
  72. tm->tm_sec = bcd2bin(second);
  73. tm->tm_min = bcd2bin(minute);
  74. tm->tm_hour = bcd2bin(hour);
  75. tm->tm_mday = bcd2bin(day);
  76. tm->tm_wday = bcd2bin(wday);
  77. tm->tm_mon = bcd2bin(month) - 1;
  78. /* hw counts from year 2000, but tm_year is relative to 1900 */
  79. tm->tm_year = bcd2bin(year) + 100;
  80. return rtc_valid_tm(tm);
  81. }
  82. static int mv_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  83. {
  84. struct rtc_plat_data *pdata = dev_get_drvdata(dev);
  85. void __iomem *ioaddr = pdata->ioaddr;
  86. u32 rtc_time, rtc_date;
  87. unsigned int year, month, day, hour, minute, second, wday;
  88. rtc_time = readl(ioaddr + RTC_ALARM_TIME_REG_OFFS);
  89. rtc_date = readl(ioaddr + RTC_ALARM_DATE_REG_OFFS);
  90. second = rtc_time & 0x7f;
  91. minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
  92. hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hour mode */
  93. wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
  94. day = rtc_date & 0x3f;
  95. month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
  96. year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
  97. alm->time.tm_sec = bcd2bin(second);
  98. alm->time.tm_min = bcd2bin(minute);
  99. alm->time.tm_hour = bcd2bin(hour);
  100. alm->time.tm_mday = bcd2bin(day);
  101. alm->time.tm_wday = bcd2bin(wday);
  102. alm->time.tm_mon = bcd2bin(month) - 1;
  103. /* hw counts from year 2000, but tm_year is relative to 1900 */
  104. alm->time.tm_year = bcd2bin(year) + 100;
  105. if (rtc_valid_tm(&alm->time) < 0) {
  106. dev_err(dev, "retrieved alarm date/time is not valid.\n");
  107. rtc_time_to_tm(0, &alm->time);
  108. }
  109. alm->enabled = !!readl(ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
  110. return 0;
  111. }
  112. static int mv_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  113. {
  114. struct rtc_plat_data *pdata = dev_get_drvdata(dev);
  115. void __iomem *ioaddr = pdata->ioaddr;
  116. u32 rtc_reg = 0;
  117. if (alm->time.tm_sec >= 0)
  118. rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_sec))
  119. << RTC_SECONDS_OFFS;
  120. if (alm->time.tm_min >= 0)
  121. rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_min))
  122. << RTC_MINUTES_OFFS;
  123. if (alm->time.tm_hour >= 0)
  124. rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_hour))
  125. << RTC_HOURS_OFFS;
  126. writel(rtc_reg, ioaddr + RTC_ALARM_TIME_REG_OFFS);
  127. if (alm->time.tm_mday >= 0)
  128. rtc_reg = (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mday))
  129. << RTC_MDAY_OFFS;
  130. else
  131. rtc_reg = 0;
  132. if (alm->time.tm_mon >= 0)
  133. rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mon + 1))
  134. << RTC_MONTH_OFFS;
  135. if (alm->time.tm_year >= 0)
  136. rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_year % 100))
  137. << RTC_YEAR_OFFS;
  138. writel(rtc_reg, ioaddr + RTC_ALARM_DATE_REG_OFFS);
  139. writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
  140. writel(alm->enabled ? 1 : 0,
  141. ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
  142. return 0;
  143. }
  144. static int mv_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  145. {
  146. struct platform_device *pdev = to_platform_device(dev);
  147. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  148. void __iomem *ioaddr = pdata->ioaddr;
  149. if (pdata->irq < 0)
  150. return -EINVAL; /* fall back into rtc-dev's emulation */
  151. if (enabled)
  152. writel(1, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
  153. else
  154. writel(0, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
  155. return 0;
  156. }
  157. static irqreturn_t mv_rtc_interrupt(int irq, void *data)
  158. {
  159. struct rtc_plat_data *pdata = data;
  160. void __iomem *ioaddr = pdata->ioaddr;
  161. /* alarm irq? */
  162. if (!readl(ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS))
  163. return IRQ_NONE;
  164. /* clear interrupt */
  165. writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
  166. rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF);
  167. return IRQ_HANDLED;
  168. }
  169. static const struct rtc_class_ops mv_rtc_ops = {
  170. .read_time = mv_rtc_read_time,
  171. .set_time = mv_rtc_set_time,
  172. };
  173. static const struct rtc_class_ops mv_rtc_alarm_ops = {
  174. .read_time = mv_rtc_read_time,
  175. .set_time = mv_rtc_set_time,
  176. .read_alarm = mv_rtc_read_alarm,
  177. .set_alarm = mv_rtc_set_alarm,
  178. .alarm_irq_enable = mv_rtc_alarm_irq_enable,
  179. };
  180. static int __init mv_rtc_probe(struct platform_device *pdev)
  181. {
  182. struct resource *res;
  183. struct rtc_plat_data *pdata;
  184. u32 rtc_time;
  185. u32 rtc_date;
  186. int ret = 0;
  187. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  188. if (!pdata)
  189. return -ENOMEM;
  190. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  191. pdata->ioaddr = devm_ioremap_resource(&pdev->dev, res);
  192. if (IS_ERR(pdata->ioaddr))
  193. return PTR_ERR(pdata->ioaddr);
  194. pdata->clk = devm_clk_get(&pdev->dev, NULL);
  195. /* Not all SoCs require a clock.*/
  196. if (!IS_ERR(pdata->clk))
  197. clk_prepare_enable(pdata->clk);
  198. /* make sure the 24 hour mode is enabled */
  199. rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
  200. if (rtc_time & RTC_HOURS_12H_MODE) {
  201. dev_err(&pdev->dev, "12 Hour mode is enabled but not supported.\n");
  202. ret = -EINVAL;
  203. goto out;
  204. }
  205. /* make sure it is actually functional */
  206. if (rtc_time == 0x01000000) {
  207. ssleep(1);
  208. rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
  209. if (rtc_time == 0x01000000) {
  210. dev_err(&pdev->dev, "internal RTC not ticking\n");
  211. ret = -ENODEV;
  212. goto out;
  213. }
  214. }
  215. /*
  216. * A date after January 19th, 2038 does not fit on 32 bits and
  217. * will confuse the kernel and userspace. Reset to a sane date
  218. * (January 1st, 2013) if we're after 2038.
  219. */
  220. rtc_date = readl(pdata->ioaddr + RTC_DATE_REG_OFFS);
  221. if (bcd2bin((rtc_date >> RTC_YEAR_OFFS) & 0xff) >= 38) {
  222. dev_info(&pdev->dev, "invalid RTC date, resetting to January 1st, 2013\n");
  223. writel(0x130101, pdata->ioaddr + RTC_DATE_REG_OFFS);
  224. }
  225. pdata->irq = platform_get_irq(pdev, 0);
  226. platform_set_drvdata(pdev, pdata);
  227. if (pdata->irq >= 0) {
  228. device_init_wakeup(&pdev->dev, 1);
  229. pdata->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  230. &mv_rtc_alarm_ops,
  231. THIS_MODULE);
  232. } else {
  233. pdata->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  234. &mv_rtc_ops, THIS_MODULE);
  235. }
  236. if (IS_ERR(pdata->rtc)) {
  237. ret = PTR_ERR(pdata->rtc);
  238. goto out;
  239. }
  240. if (pdata->irq >= 0) {
  241. writel(0, pdata->ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
  242. if (devm_request_irq(&pdev->dev, pdata->irq, mv_rtc_interrupt,
  243. IRQF_SHARED,
  244. pdev->name, pdata) < 0) {
  245. dev_warn(&pdev->dev, "interrupt not available.\n");
  246. pdata->irq = -1;
  247. }
  248. }
  249. return 0;
  250. out:
  251. if (!IS_ERR(pdata->clk))
  252. clk_disable_unprepare(pdata->clk);
  253. return ret;
  254. }
  255. static int __exit mv_rtc_remove(struct platform_device *pdev)
  256. {
  257. struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  258. if (pdata->irq >= 0)
  259. device_init_wakeup(&pdev->dev, 0);
  260. if (!IS_ERR(pdata->clk))
  261. clk_disable_unprepare(pdata->clk);
  262. return 0;
  263. }
  264. #ifdef CONFIG_OF
  265. static const struct of_device_id rtc_mv_of_match_table[] = {
  266. { .compatible = "marvell,orion-rtc", },
  267. {}
  268. };
  269. #endif
  270. static struct platform_driver mv_rtc_driver = {
  271. .remove = __exit_p(mv_rtc_remove),
  272. .driver = {
  273. .name = "rtc-mv",
  274. .of_match_table = of_match_ptr(rtc_mv_of_match_table),
  275. },
  276. };
  277. module_platform_driver_probe(mv_rtc_driver, mv_rtc_probe);
  278. MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
  279. MODULE_DESCRIPTION("Marvell RTC driver");
  280. MODULE_LICENSE("GPL");
  281. MODULE_ALIAS("platform:rtc-mv");