rtc-m48t35.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/platform_device.h>
  19. #include <linux/bcd.h>
  20. #define DRV_VERSION "1.0"
  21. struct m48t35_rtc {
  22. u8 pad[0x7ff8]; /* starts at 0x7ff8 */
  23. u8 control;
  24. u8 sec;
  25. u8 min;
  26. u8 hour;
  27. u8 day;
  28. u8 date;
  29. u8 month;
  30. u8 year;
  31. };
  32. #define M48T35_RTC_SET 0x80
  33. #define M48T35_RTC_READ 0x40
  34. struct m48t35_priv {
  35. struct rtc_device *rtc;
  36. struct m48t35_rtc __iomem *reg;
  37. size_t size;
  38. unsigned long baseaddr;
  39. spinlock_t lock;
  40. };
  41. static int m48t35_read_time(struct device *dev, struct rtc_time *tm)
  42. {
  43. struct m48t35_priv *priv = dev_get_drvdata(dev);
  44. u8 control;
  45. /*
  46. * Only the values that we read from the RTC are set. We leave
  47. * tm_wday, tm_yday and tm_isdst untouched. Even though the
  48. * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
  49. * by the RTC when initially set to a non-zero value.
  50. */
  51. spin_lock_irq(&priv->lock);
  52. control = readb(&priv->reg->control);
  53. writeb(control | M48T35_RTC_READ, &priv->reg->control);
  54. tm->tm_sec = readb(&priv->reg->sec);
  55. tm->tm_min = readb(&priv->reg->min);
  56. tm->tm_hour = readb(&priv->reg->hour);
  57. tm->tm_mday = readb(&priv->reg->date);
  58. tm->tm_mon = readb(&priv->reg->month);
  59. tm->tm_year = readb(&priv->reg->year);
  60. writeb(control, &priv->reg->control);
  61. spin_unlock_irq(&priv->lock);
  62. tm->tm_sec = bcd2bin(tm->tm_sec);
  63. tm->tm_min = bcd2bin(tm->tm_min);
  64. tm->tm_hour = bcd2bin(tm->tm_hour);
  65. tm->tm_mday = bcd2bin(tm->tm_mday);
  66. tm->tm_mon = bcd2bin(tm->tm_mon);
  67. tm->tm_year = bcd2bin(tm->tm_year);
  68. /*
  69. * Account for differences between how the RTC uses the values
  70. * and how they are defined in a struct rtc_time;
  71. */
  72. tm->tm_year += 70;
  73. if (tm->tm_year <= 69)
  74. tm->tm_year += 100;
  75. tm->tm_mon--;
  76. return rtc_valid_tm(tm);
  77. }
  78. static int m48t35_set_time(struct device *dev, struct rtc_time *tm)
  79. {
  80. struct m48t35_priv *priv = dev_get_drvdata(dev);
  81. unsigned char mon, day, hrs, min, sec;
  82. unsigned int yrs;
  83. u8 control;
  84. yrs = tm->tm_year + 1900;
  85. mon = tm->tm_mon + 1; /* tm_mon starts at zero */
  86. day = tm->tm_mday;
  87. hrs = tm->tm_hour;
  88. min = tm->tm_min;
  89. sec = tm->tm_sec;
  90. if (yrs < 1970)
  91. return -EINVAL;
  92. yrs -= 1970;
  93. if (yrs > 255) /* They are unsigned */
  94. return -EINVAL;
  95. if (yrs > 169)
  96. return -EINVAL;
  97. if (yrs >= 100)
  98. yrs -= 100;
  99. sec = bin2bcd(sec);
  100. min = bin2bcd(min);
  101. hrs = bin2bcd(hrs);
  102. day = bin2bcd(day);
  103. mon = bin2bcd(mon);
  104. yrs = bin2bcd(yrs);
  105. spin_lock_irq(&priv->lock);
  106. control = readb(&priv->reg->control);
  107. writeb(control | M48T35_RTC_SET, &priv->reg->control);
  108. writeb(yrs, &priv->reg->year);
  109. writeb(mon, &priv->reg->month);
  110. writeb(day, &priv->reg->date);
  111. writeb(hrs, &priv->reg->hour);
  112. writeb(min, &priv->reg->min);
  113. writeb(sec, &priv->reg->sec);
  114. writeb(control, &priv->reg->control);
  115. spin_unlock_irq(&priv->lock);
  116. return 0;
  117. }
  118. static const struct rtc_class_ops m48t35_ops = {
  119. .read_time = m48t35_read_time,
  120. .set_time = m48t35_set_time,
  121. };
  122. static int __devinit m48t35_probe(struct platform_device *pdev)
  123. {
  124. struct rtc_device *rtc;
  125. struct resource *res;
  126. struct m48t35_priv *priv;
  127. int ret = 0;
  128. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  129. if (!res)
  130. return -ENODEV;
  131. priv = kzalloc(sizeof(struct m48t35_priv), GFP_KERNEL);
  132. if (!priv)
  133. return -ENOMEM;
  134. priv->size = res->end - res->start + 1;
  135. /*
  136. * kludge: remove the #ifndef after ioc3 resource
  137. * conflicts are resolved
  138. */
  139. #ifndef CONFIG_SGI_IP27
  140. if (!request_mem_region(res->start, priv->size, pdev->name)) {
  141. ret = -EBUSY;
  142. goto out;
  143. }
  144. #endif
  145. priv->baseaddr = res->start;
  146. priv->reg = ioremap(priv->baseaddr, priv->size);
  147. if (!priv->reg) {
  148. ret = -ENOMEM;
  149. goto out;
  150. }
  151. spin_lock_init(&priv->lock);
  152. rtc = rtc_device_register("m48t35", &pdev->dev,
  153. &m48t35_ops, THIS_MODULE);
  154. if (IS_ERR(rtc)) {
  155. ret = PTR_ERR(rtc);
  156. goto out;
  157. }
  158. priv->rtc = rtc;
  159. platform_set_drvdata(pdev, priv);
  160. return 0;
  161. out:
  162. if (priv->rtc)
  163. rtc_device_unregister(priv->rtc);
  164. if (priv->reg)
  165. iounmap(priv->reg);
  166. if (priv->baseaddr)
  167. release_mem_region(priv->baseaddr, priv->size);
  168. kfree(priv);
  169. return ret;
  170. }
  171. static int __devexit m48t35_remove(struct platform_device *pdev)
  172. {
  173. struct m48t35_priv *priv = platform_get_drvdata(pdev);
  174. rtc_device_unregister(priv->rtc);
  175. iounmap(priv->reg);
  176. #ifndef CONFIG_SGI_IP27
  177. release_mem_region(priv->baseaddr, priv->size);
  178. #endif
  179. kfree(priv);
  180. return 0;
  181. }
  182. static struct platform_driver m48t35_platform_driver = {
  183. .driver = {
  184. .name = "rtc-m48t35",
  185. .owner = THIS_MODULE,
  186. },
  187. .probe = m48t35_probe,
  188. .remove = __devexit_p(m48t35_remove),
  189. };
  190. static int __init m48t35_init(void)
  191. {
  192. return platform_driver_register(&m48t35_platform_driver);
  193. }
  194. static void __exit m48t35_exit(void)
  195. {
  196. platform_driver_unregister(&m48t35_platform_driver);
  197. }
  198. MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
  199. MODULE_DESCRIPTION("M48T35 RTC driver");
  200. MODULE_LICENSE("GPL");
  201. MODULE_VERSION(DRV_VERSION);
  202. MODULE_ALIAS("platform:rtc-m48t35");
  203. module_init(m48t35_init);
  204. module_exit(m48t35_exit);