rtc-m48t35.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for the SGS-Thomson M48T35 Timekeeper RAM chip
  4. *
  5. * Copyright (C) 2000 Silicon Graphics, Inc.
  6. * Written by Ulf Carlsson ([email protected])
  7. *
  8. * Copyright (C) 2008 Thomas Bogendoerfer
  9. *
  10. * Based on code written by Paul Gortmaker.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/rtc.h>
  14. #include <linux/slab.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/bcd.h>
  17. #include <linux/io.h>
  18. #include <linux/err.h>
  19. struct m48t35_rtc {
  20. u8 pad[0x7ff8]; /* starts at 0x7ff8 */
  21. #ifdef CONFIG_SGI_IP27
  22. u8 hour;
  23. u8 min;
  24. u8 sec;
  25. u8 control;
  26. u8 year;
  27. u8 month;
  28. u8 date;
  29. u8 day;
  30. #else
  31. u8 control;
  32. u8 sec;
  33. u8 min;
  34. u8 hour;
  35. u8 day;
  36. u8 date;
  37. u8 month;
  38. u8 year;
  39. #endif
  40. };
  41. #define M48T35_RTC_SET 0x80
  42. #define M48T35_RTC_READ 0x40
  43. struct m48t35_priv {
  44. struct rtc_device *rtc;
  45. struct m48t35_rtc __iomem *reg;
  46. size_t size;
  47. unsigned long baseaddr;
  48. spinlock_t lock;
  49. };
  50. static int m48t35_read_time(struct device *dev, struct rtc_time *tm)
  51. {
  52. struct m48t35_priv *priv = dev_get_drvdata(dev);
  53. u8 control;
  54. /*
  55. * Only the values that we read from the RTC are set. We leave
  56. * tm_wday, tm_yday and tm_isdst untouched. Even though the
  57. * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
  58. * by the RTC when initially set to a non-zero value.
  59. */
  60. spin_lock_irq(&priv->lock);
  61. control = readb(&priv->reg->control);
  62. writeb(control | M48T35_RTC_READ, &priv->reg->control);
  63. tm->tm_sec = readb(&priv->reg->sec);
  64. tm->tm_min = readb(&priv->reg->min);
  65. tm->tm_hour = readb(&priv->reg->hour);
  66. tm->tm_mday = readb(&priv->reg->date);
  67. tm->tm_mon = readb(&priv->reg->month);
  68. tm->tm_year = readb(&priv->reg->year);
  69. writeb(control, &priv->reg->control);
  70. spin_unlock_irq(&priv->lock);
  71. tm->tm_sec = bcd2bin(tm->tm_sec);
  72. tm->tm_min = bcd2bin(tm->tm_min);
  73. tm->tm_hour = bcd2bin(tm->tm_hour);
  74. tm->tm_mday = bcd2bin(tm->tm_mday);
  75. tm->tm_mon = bcd2bin(tm->tm_mon);
  76. tm->tm_year = bcd2bin(tm->tm_year);
  77. /*
  78. * Account for differences between how the RTC uses the values
  79. * and how they are defined in a struct rtc_time;
  80. */
  81. tm->tm_year += 70;
  82. if (tm->tm_year <= 69)
  83. tm->tm_year += 100;
  84. tm->tm_mon--;
  85. return 0;
  86. }
  87. static int m48t35_set_time(struct device *dev, struct rtc_time *tm)
  88. {
  89. struct m48t35_priv *priv = dev_get_drvdata(dev);
  90. unsigned char mon, day, hrs, min, sec;
  91. unsigned int yrs;
  92. u8 control;
  93. yrs = tm->tm_year + 1900;
  94. mon = tm->tm_mon + 1; /* tm_mon starts at zero */
  95. day = tm->tm_mday;
  96. hrs = tm->tm_hour;
  97. min = tm->tm_min;
  98. sec = tm->tm_sec;
  99. if (yrs < 1970)
  100. return -EINVAL;
  101. yrs -= 1970;
  102. if (yrs > 255) /* They are unsigned */
  103. return -EINVAL;
  104. if (yrs > 169)
  105. return -EINVAL;
  106. if (yrs >= 100)
  107. yrs -= 100;
  108. sec = bin2bcd(sec);
  109. min = bin2bcd(min);
  110. hrs = bin2bcd(hrs);
  111. day = bin2bcd(day);
  112. mon = bin2bcd(mon);
  113. yrs = bin2bcd(yrs);
  114. spin_lock_irq(&priv->lock);
  115. control = readb(&priv->reg->control);
  116. writeb(control | M48T35_RTC_SET, &priv->reg->control);
  117. writeb(yrs, &priv->reg->year);
  118. writeb(mon, &priv->reg->month);
  119. writeb(day, &priv->reg->date);
  120. writeb(hrs, &priv->reg->hour);
  121. writeb(min, &priv->reg->min);
  122. writeb(sec, &priv->reg->sec);
  123. writeb(control, &priv->reg->control);
  124. spin_unlock_irq(&priv->lock);
  125. return 0;
  126. }
  127. static const struct rtc_class_ops m48t35_ops = {
  128. .read_time = m48t35_read_time,
  129. .set_time = m48t35_set_time,
  130. };
  131. static int m48t35_probe(struct platform_device *pdev)
  132. {
  133. struct resource *res;
  134. struct m48t35_priv *priv;
  135. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  136. if (!res)
  137. return -ENODEV;
  138. priv = devm_kzalloc(&pdev->dev, sizeof(struct m48t35_priv), GFP_KERNEL);
  139. if (!priv)
  140. return -ENOMEM;
  141. priv->size = resource_size(res);
  142. if (!devm_request_mem_region(&pdev->dev, res->start, priv->size,
  143. pdev->name))
  144. return -EBUSY;
  145. priv->baseaddr = res->start;
  146. priv->reg = devm_ioremap(&pdev->dev, priv->baseaddr, priv->size);
  147. if (!priv->reg)
  148. return -ENOMEM;
  149. spin_lock_init(&priv->lock);
  150. platform_set_drvdata(pdev, priv);
  151. priv->rtc = devm_rtc_device_register(&pdev->dev, "m48t35",
  152. &m48t35_ops, THIS_MODULE);
  153. return PTR_ERR_OR_ZERO(priv->rtc);
  154. }
  155. static struct platform_driver m48t35_platform_driver = {
  156. .driver = {
  157. .name = "rtc-m48t35",
  158. },
  159. .probe = m48t35_probe,
  160. };
  161. module_platform_driver(m48t35_platform_driver);
  162. MODULE_AUTHOR("Thomas Bogendoerfer <[email protected]>");
  163. MODULE_DESCRIPTION("M48T35 RTC driver");
  164. MODULE_LICENSE("GPL");
  165. MODULE_ALIAS("platform:rtc-m48t35");