rtc-m48t35.c 4.7 KB

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