rtc-m48t86.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * ST M48T86 / Dallas DS12887 RTC driver
  3. * Copyright (c) 2006 Tower Technologies
  4. *
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This drivers only supports the clock running in BCD and 24H mode.
  12. * If it will be ever adapted to binary and 12H mode, care must be taken
  13. * to not introduce bugs.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/rtc.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/bcd.h>
  19. #include <linux/io.h>
  20. #define M48T86_SEC 0x00
  21. #define M48T86_SECALRM 0x01
  22. #define M48T86_MIN 0x02
  23. #define M48T86_MINALRM 0x03
  24. #define M48T86_HOUR 0x04
  25. #define M48T86_HOURALRM 0x05
  26. #define M48T86_DOW 0x06 /* 1 = sunday */
  27. #define M48T86_DOM 0x07
  28. #define M48T86_MONTH 0x08 /* 1 - 12 */
  29. #define M48T86_YEAR 0x09 /* 0 - 99 */
  30. #define M48T86_A 0x0a
  31. #define M48T86_B 0x0b
  32. #define M48T86_B_SET BIT(7)
  33. #define M48T86_B_DM BIT(2)
  34. #define M48T86_B_H24 BIT(1)
  35. #define M48T86_C 0x0c
  36. #define M48T86_D 0x0d
  37. #define M48T86_D_VRT BIT(7)
  38. #define M48T86_NVRAM(x) (0x0e + (x))
  39. #define M48T86_NVRAM_LEN 114
  40. struct m48t86_rtc_info {
  41. void __iomem *index_reg;
  42. void __iomem *data_reg;
  43. struct rtc_device *rtc;
  44. };
  45. static unsigned char m48t86_readb(struct device *dev, unsigned long addr)
  46. {
  47. struct m48t86_rtc_info *info = dev_get_drvdata(dev);
  48. unsigned char value;
  49. writeb(addr, info->index_reg);
  50. value = readb(info->data_reg);
  51. return value;
  52. }
  53. static void m48t86_writeb(struct device *dev,
  54. unsigned char value, unsigned long addr)
  55. {
  56. struct m48t86_rtc_info *info = dev_get_drvdata(dev);
  57. writeb(addr, info->index_reg);
  58. writeb(value, info->data_reg);
  59. }
  60. static int m48t86_rtc_read_time(struct device *dev, struct rtc_time *tm)
  61. {
  62. unsigned char reg;
  63. reg = m48t86_readb(dev, M48T86_B);
  64. if (reg & M48T86_B_DM) {
  65. /* data (binary) mode */
  66. tm->tm_sec = m48t86_readb(dev, M48T86_SEC);
  67. tm->tm_min = m48t86_readb(dev, M48T86_MIN);
  68. tm->tm_hour = m48t86_readb(dev, M48T86_HOUR) & 0x3f;
  69. tm->tm_mday = m48t86_readb(dev, M48T86_DOM);
  70. /* tm_mon is 0-11 */
  71. tm->tm_mon = m48t86_readb(dev, M48T86_MONTH) - 1;
  72. tm->tm_year = m48t86_readb(dev, M48T86_YEAR) + 100;
  73. tm->tm_wday = m48t86_readb(dev, M48T86_DOW);
  74. } else {
  75. /* bcd mode */
  76. tm->tm_sec = bcd2bin(m48t86_readb(dev, M48T86_SEC));
  77. tm->tm_min = bcd2bin(m48t86_readb(dev, M48T86_MIN));
  78. tm->tm_hour = bcd2bin(m48t86_readb(dev, M48T86_HOUR) &
  79. 0x3f);
  80. tm->tm_mday = bcd2bin(m48t86_readb(dev, M48T86_DOM));
  81. /* tm_mon is 0-11 */
  82. tm->tm_mon = bcd2bin(m48t86_readb(dev, M48T86_MONTH)) - 1;
  83. tm->tm_year = bcd2bin(m48t86_readb(dev, M48T86_YEAR)) + 100;
  84. tm->tm_wday = bcd2bin(m48t86_readb(dev, M48T86_DOW));
  85. }
  86. /* correct the hour if the clock is in 12h mode */
  87. if (!(reg & M48T86_B_H24))
  88. if (m48t86_readb(dev, M48T86_HOUR) & 0x80)
  89. tm->tm_hour += 12;
  90. return rtc_valid_tm(tm);
  91. }
  92. static int m48t86_rtc_set_time(struct device *dev, struct rtc_time *tm)
  93. {
  94. unsigned char reg;
  95. reg = m48t86_readb(dev, M48T86_B);
  96. /* update flag and 24h mode */
  97. reg |= M48T86_B_SET | M48T86_B_H24;
  98. m48t86_writeb(dev, reg, M48T86_B);
  99. if (reg & M48T86_B_DM) {
  100. /* data (binary) mode */
  101. m48t86_writeb(dev, tm->tm_sec, M48T86_SEC);
  102. m48t86_writeb(dev, tm->tm_min, M48T86_MIN);
  103. m48t86_writeb(dev, tm->tm_hour, M48T86_HOUR);
  104. m48t86_writeb(dev, tm->tm_mday, M48T86_DOM);
  105. m48t86_writeb(dev, tm->tm_mon + 1, M48T86_MONTH);
  106. m48t86_writeb(dev, tm->tm_year % 100, M48T86_YEAR);
  107. m48t86_writeb(dev, tm->tm_wday, M48T86_DOW);
  108. } else {
  109. /* bcd mode */
  110. m48t86_writeb(dev, bin2bcd(tm->tm_sec), M48T86_SEC);
  111. m48t86_writeb(dev, bin2bcd(tm->tm_min), M48T86_MIN);
  112. m48t86_writeb(dev, bin2bcd(tm->tm_hour), M48T86_HOUR);
  113. m48t86_writeb(dev, bin2bcd(tm->tm_mday), M48T86_DOM);
  114. m48t86_writeb(dev, bin2bcd(tm->tm_mon + 1), M48T86_MONTH);
  115. m48t86_writeb(dev, bin2bcd(tm->tm_year % 100), M48T86_YEAR);
  116. m48t86_writeb(dev, bin2bcd(tm->tm_wday), M48T86_DOW);
  117. }
  118. /* update ended */
  119. reg &= ~M48T86_B_SET;
  120. m48t86_writeb(dev, reg, M48T86_B);
  121. return 0;
  122. }
  123. static int m48t86_rtc_proc(struct device *dev, struct seq_file *seq)
  124. {
  125. unsigned char reg;
  126. reg = m48t86_readb(dev, M48T86_B);
  127. seq_printf(seq, "mode\t\t: %s\n",
  128. (reg & M48T86_B_DM) ? "binary" : "bcd");
  129. reg = m48t86_readb(dev, M48T86_D);
  130. seq_printf(seq, "battery\t\t: %s\n",
  131. (reg & M48T86_D_VRT) ? "ok" : "exhausted");
  132. return 0;
  133. }
  134. static const struct rtc_class_ops m48t86_rtc_ops = {
  135. .read_time = m48t86_rtc_read_time,
  136. .set_time = m48t86_rtc_set_time,
  137. .proc = m48t86_rtc_proc,
  138. };
  139. static ssize_t m48t86_nvram_read(struct file *filp, struct kobject *kobj,
  140. struct bin_attribute *attr,
  141. char *buf, loff_t off, size_t count)
  142. {
  143. struct device *dev = kobj_to_dev(kobj);
  144. unsigned int i;
  145. for (i = 0; i < count; i++)
  146. buf[i] = m48t86_readb(dev, M48T86_NVRAM(off + i));
  147. return count;
  148. }
  149. static ssize_t m48t86_nvram_write(struct file *filp, struct kobject *kobj,
  150. struct bin_attribute *attr,
  151. char *buf, loff_t off, size_t count)
  152. {
  153. struct device *dev = kobj_to_dev(kobj);
  154. unsigned int i;
  155. for (i = 0; i < count; i++)
  156. m48t86_writeb(dev, buf[i], M48T86_NVRAM(off + i));
  157. return count;
  158. }
  159. static BIN_ATTR(nvram, 0644, m48t86_nvram_read, m48t86_nvram_write,
  160. M48T86_NVRAM_LEN);
  161. /*
  162. * The RTC is an optional feature at purchase time on some Technologic Systems
  163. * boards. Verify that it actually exists by checking if the last two bytes
  164. * of the NVRAM can be changed.
  165. *
  166. * This is based on the method used in their rtc7800.c example.
  167. */
  168. static bool m48t86_verify_chip(struct platform_device *pdev)
  169. {
  170. unsigned int offset0 = M48T86_NVRAM(M48T86_NVRAM_LEN - 2);
  171. unsigned int offset1 = M48T86_NVRAM(M48T86_NVRAM_LEN - 1);
  172. unsigned char tmp0, tmp1;
  173. tmp0 = m48t86_readb(&pdev->dev, offset0);
  174. tmp1 = m48t86_readb(&pdev->dev, offset1);
  175. m48t86_writeb(&pdev->dev, 0x00, offset0);
  176. m48t86_writeb(&pdev->dev, 0x55, offset1);
  177. if (m48t86_readb(&pdev->dev, offset1) == 0x55) {
  178. m48t86_writeb(&pdev->dev, 0xaa, offset1);
  179. if (m48t86_readb(&pdev->dev, offset1) == 0xaa &&
  180. m48t86_readb(&pdev->dev, offset0) == 0x00) {
  181. m48t86_writeb(&pdev->dev, tmp0, offset0);
  182. m48t86_writeb(&pdev->dev, tmp1, offset1);
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. static int m48t86_rtc_probe(struct platform_device *pdev)
  189. {
  190. struct m48t86_rtc_info *info;
  191. struct resource *res;
  192. unsigned char reg;
  193. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  194. if (!info)
  195. return -ENOMEM;
  196. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  197. if (!res)
  198. return -ENODEV;
  199. info->index_reg = devm_ioremap_resource(&pdev->dev, res);
  200. if (IS_ERR(info->index_reg))
  201. return PTR_ERR(info->index_reg);
  202. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  203. if (!res)
  204. return -ENODEV;
  205. info->data_reg = devm_ioremap_resource(&pdev->dev, res);
  206. if (IS_ERR(info->data_reg))
  207. return PTR_ERR(info->data_reg);
  208. dev_set_drvdata(&pdev->dev, info);
  209. if (!m48t86_verify_chip(pdev)) {
  210. dev_info(&pdev->dev, "RTC not present\n");
  211. return -ENODEV;
  212. }
  213. info->rtc = devm_rtc_device_register(&pdev->dev, "m48t86",
  214. &m48t86_rtc_ops, THIS_MODULE);
  215. if (IS_ERR(info->rtc))
  216. return PTR_ERR(info->rtc);
  217. /* read battery status */
  218. reg = m48t86_readb(&pdev->dev, M48T86_D);
  219. dev_info(&pdev->dev, "battery %s\n",
  220. (reg & M48T86_D_VRT) ? "ok" : "exhausted");
  221. if (device_create_bin_file(&pdev->dev, &bin_attr_nvram))
  222. dev_err(&pdev->dev, "failed to create nvram sysfs entry\n");
  223. return 0;
  224. }
  225. static int m48t86_rtc_remove(struct platform_device *pdev)
  226. {
  227. device_remove_bin_file(&pdev->dev, &bin_attr_nvram);
  228. return 0;
  229. }
  230. static struct platform_driver m48t86_rtc_platform_driver = {
  231. .driver = {
  232. .name = "rtc-m48t86",
  233. },
  234. .probe = m48t86_rtc_probe,
  235. .remove = m48t86_rtc_remove,
  236. };
  237. module_platform_driver(m48t86_rtc_platform_driver);
  238. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  239. MODULE_DESCRIPTION("M48T86 RTC driver");
  240. MODULE_LICENSE("GPL");
  241. MODULE_ALIAS("platform:rtc-m48t86");