rtc-mv.c 8.7 KB

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