rtc-m48t86.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 0;
  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 int m48t86_nvram_read(void *priv, unsigned int off, void *buf,
  140. size_t count)
  141. {
  142. struct device *dev = priv;
  143. unsigned int i;
  144. for (i = 0; i < count; i++)
  145. ((u8 *)buf)[i] = m48t86_readb(dev, M48T86_NVRAM(off + i));
  146. return 0;
  147. }
  148. static int m48t86_nvram_write(void *priv, unsigned int off, void *buf,
  149. size_t count)
  150. {
  151. struct device *dev = priv;
  152. unsigned int i;
  153. for (i = 0; i < count; i++)
  154. m48t86_writeb(dev, ((u8 *)buf)[i], M48T86_NVRAM(off + i));
  155. return 0;
  156. }
  157. /*
  158. * The RTC is an optional feature at purchase time on some Technologic Systems
  159. * boards. Verify that it actually exists by checking if the last two bytes
  160. * of the NVRAM can be changed.
  161. *
  162. * This is based on the method used in their rtc7800.c example.
  163. */
  164. static bool m48t86_verify_chip(struct platform_device *pdev)
  165. {
  166. unsigned int offset0 = M48T86_NVRAM(M48T86_NVRAM_LEN - 2);
  167. unsigned int offset1 = M48T86_NVRAM(M48T86_NVRAM_LEN - 1);
  168. unsigned char tmp0, tmp1;
  169. tmp0 = m48t86_readb(&pdev->dev, offset0);
  170. tmp1 = m48t86_readb(&pdev->dev, offset1);
  171. m48t86_writeb(&pdev->dev, 0x00, offset0);
  172. m48t86_writeb(&pdev->dev, 0x55, offset1);
  173. if (m48t86_readb(&pdev->dev, offset1) == 0x55) {
  174. m48t86_writeb(&pdev->dev, 0xaa, offset1);
  175. if (m48t86_readb(&pdev->dev, offset1) == 0xaa &&
  176. m48t86_readb(&pdev->dev, offset0) == 0x00) {
  177. m48t86_writeb(&pdev->dev, tmp0, offset0);
  178. m48t86_writeb(&pdev->dev, tmp1, offset1);
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. static int m48t86_rtc_probe(struct platform_device *pdev)
  185. {
  186. struct m48t86_rtc_info *info;
  187. struct resource *res;
  188. unsigned char reg;
  189. int err;
  190. struct nvmem_config m48t86_nvmem_cfg = {
  191. .name = "m48t86_nvram",
  192. .word_size = 1,
  193. .stride = 1,
  194. .size = M48T86_NVRAM_LEN,
  195. .reg_read = m48t86_nvram_read,
  196. .reg_write = m48t86_nvram_write,
  197. .priv = &pdev->dev,
  198. };
  199. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  200. if (!info)
  201. return -ENOMEM;
  202. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  203. if (!res)
  204. return -ENODEV;
  205. info->index_reg = devm_ioremap_resource(&pdev->dev, res);
  206. if (IS_ERR(info->index_reg))
  207. return PTR_ERR(info->index_reg);
  208. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  209. if (!res)
  210. return -ENODEV;
  211. info->data_reg = devm_ioremap_resource(&pdev->dev, res);
  212. if (IS_ERR(info->data_reg))
  213. return PTR_ERR(info->data_reg);
  214. dev_set_drvdata(&pdev->dev, info);
  215. if (!m48t86_verify_chip(pdev)) {
  216. dev_info(&pdev->dev, "RTC not present\n");
  217. return -ENODEV;
  218. }
  219. info->rtc = devm_rtc_allocate_device(&pdev->dev);
  220. if (IS_ERR(info->rtc))
  221. return PTR_ERR(info->rtc);
  222. info->rtc->ops = &m48t86_rtc_ops;
  223. info->rtc->nvram_old_abi = true;
  224. err = rtc_register_device(info->rtc);
  225. if (err)
  226. return err;
  227. rtc_nvmem_register(info->rtc, &m48t86_nvmem_cfg);
  228. /* read battery status */
  229. reg = m48t86_readb(&pdev->dev, M48T86_D);
  230. dev_info(&pdev->dev, "battery %s\n",
  231. (reg & M48T86_D_VRT) ? "ok" : "exhausted");
  232. return 0;
  233. }
  234. static struct platform_driver m48t86_rtc_platform_driver = {
  235. .driver = {
  236. .name = "rtc-m48t86",
  237. },
  238. .probe = m48t86_rtc_probe,
  239. };
  240. module_platform_driver(m48t86_rtc_platform_driver);
  241. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  242. MODULE_DESCRIPTION("M48T86 RTC driver");
  243. MODULE_LICENSE("GPL");
  244. MODULE_ALIAS("platform:rtc-m48t86");