rtc-rp5c01.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Ricoh RP5C01 RTC Driver
  3. *
  4. * Copyright 2009 Geert Uytterhoeven
  5. *
  6. * Based on the A3000 TOD code in arch/m68k/amiga/config.c
  7. * Copyright (C) 1993 Hamish Macdonald
  8. */
  9. #include <linux/io.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/rtc.h>
  14. #include <linux/slab.h>
  15. enum {
  16. RP5C01_1_SECOND = 0x0, /* MODE 00 */
  17. RP5C01_10_SECOND = 0x1, /* MODE 00 */
  18. RP5C01_1_MINUTE = 0x2, /* MODE 00 and MODE 01 */
  19. RP5C01_10_MINUTE = 0x3, /* MODE 00 and MODE 01 */
  20. RP5C01_1_HOUR = 0x4, /* MODE 00 and MODE 01 */
  21. RP5C01_10_HOUR = 0x5, /* MODE 00 and MODE 01 */
  22. RP5C01_DAY_OF_WEEK = 0x6, /* MODE 00 and MODE 01 */
  23. RP5C01_1_DAY = 0x7, /* MODE 00 and MODE 01 */
  24. RP5C01_10_DAY = 0x8, /* MODE 00 and MODE 01 */
  25. RP5C01_1_MONTH = 0x9, /* MODE 00 */
  26. RP5C01_10_MONTH = 0xa, /* MODE 00 */
  27. RP5C01_1_YEAR = 0xb, /* MODE 00 */
  28. RP5C01_10_YEAR = 0xc, /* MODE 00 */
  29. RP5C01_12_24_SELECT = 0xa, /* MODE 01 */
  30. RP5C01_LEAP_YEAR = 0xb, /* MODE 01 */
  31. RP5C01_MODE = 0xd, /* all modes */
  32. RP5C01_TEST = 0xe, /* all modes */
  33. RP5C01_RESET = 0xf, /* all modes */
  34. };
  35. #define RP5C01_12_24_SELECT_12 (0 << 0)
  36. #define RP5C01_12_24_SELECT_24 (1 << 0)
  37. #define RP5C01_10_HOUR_AM (0 << 1)
  38. #define RP5C01_10_HOUR_PM (1 << 1)
  39. #define RP5C01_MODE_TIMER_EN (1 << 3) /* timer enable */
  40. #define RP5C01_MODE_ALARM_EN (1 << 2) /* alarm enable */
  41. #define RP5C01_MODE_MODE_MASK (3 << 0)
  42. #define RP5C01_MODE_MODE00 (0 << 0) /* time */
  43. #define RP5C01_MODE_MODE01 (1 << 0) /* alarm, 12h/24h, leap year */
  44. #define RP5C01_MODE_RAM_BLOCK10 (2 << 0) /* RAM 4 bits x 13 */
  45. #define RP5C01_MODE_RAM_BLOCK11 (3 << 0) /* RAM 4 bits x 13 */
  46. #define RP5C01_RESET_1HZ_PULSE (1 << 3)
  47. #define RP5C01_RESET_16HZ_PULSE (1 << 2)
  48. #define RP5C01_RESET_SECOND (1 << 1) /* reset divider stages for */
  49. /* seconds or smaller units */
  50. #define RP5C01_RESET_ALARM (1 << 0) /* reset all alarm registers */
  51. struct rp5c01_priv {
  52. u32 __iomem *regs;
  53. struct rtc_device *rtc;
  54. spinlock_t lock; /* against concurrent RTC/NVRAM access */
  55. };
  56. static inline unsigned int rp5c01_read(struct rp5c01_priv *priv,
  57. unsigned int reg)
  58. {
  59. return __raw_readl(&priv->regs[reg]) & 0xf;
  60. }
  61. static inline void rp5c01_write(struct rp5c01_priv *priv, unsigned int val,
  62. unsigned int reg)
  63. {
  64. __raw_writel(val, &priv->regs[reg]);
  65. }
  66. static void rp5c01_lock(struct rp5c01_priv *priv)
  67. {
  68. rp5c01_write(priv, RP5C01_MODE_MODE00, RP5C01_MODE);
  69. }
  70. static void rp5c01_unlock(struct rp5c01_priv *priv)
  71. {
  72. rp5c01_write(priv, RP5C01_MODE_TIMER_EN | RP5C01_MODE_MODE01,
  73. RP5C01_MODE);
  74. }
  75. static int rp5c01_read_time(struct device *dev, struct rtc_time *tm)
  76. {
  77. struct rp5c01_priv *priv = dev_get_drvdata(dev);
  78. spin_lock_irq(&priv->lock);
  79. rp5c01_lock(priv);
  80. tm->tm_sec = rp5c01_read(priv, RP5C01_10_SECOND) * 10 +
  81. rp5c01_read(priv, RP5C01_1_SECOND);
  82. tm->tm_min = rp5c01_read(priv, RP5C01_10_MINUTE) * 10 +
  83. rp5c01_read(priv, RP5C01_1_MINUTE);
  84. tm->tm_hour = rp5c01_read(priv, RP5C01_10_HOUR) * 10 +
  85. rp5c01_read(priv, RP5C01_1_HOUR);
  86. tm->tm_mday = rp5c01_read(priv, RP5C01_10_DAY) * 10 +
  87. rp5c01_read(priv, RP5C01_1_DAY);
  88. tm->tm_wday = rp5c01_read(priv, RP5C01_DAY_OF_WEEK);
  89. tm->tm_mon = rp5c01_read(priv, RP5C01_10_MONTH) * 10 +
  90. rp5c01_read(priv, RP5C01_1_MONTH) - 1;
  91. tm->tm_year = rp5c01_read(priv, RP5C01_10_YEAR) * 10 +
  92. rp5c01_read(priv, RP5C01_1_YEAR);
  93. if (tm->tm_year <= 69)
  94. tm->tm_year += 100;
  95. rp5c01_unlock(priv);
  96. spin_unlock_irq(&priv->lock);
  97. return 0;
  98. }
  99. static int rp5c01_set_time(struct device *dev, struct rtc_time *tm)
  100. {
  101. struct rp5c01_priv *priv = dev_get_drvdata(dev);
  102. spin_lock_irq(&priv->lock);
  103. rp5c01_lock(priv);
  104. rp5c01_write(priv, tm->tm_sec / 10, RP5C01_10_SECOND);
  105. rp5c01_write(priv, tm->tm_sec % 10, RP5C01_1_SECOND);
  106. rp5c01_write(priv, tm->tm_min / 10, RP5C01_10_MINUTE);
  107. rp5c01_write(priv, tm->tm_min % 10, RP5C01_1_MINUTE);
  108. rp5c01_write(priv, tm->tm_hour / 10, RP5C01_10_HOUR);
  109. rp5c01_write(priv, tm->tm_hour % 10, RP5C01_1_HOUR);
  110. rp5c01_write(priv, tm->tm_mday / 10, RP5C01_10_DAY);
  111. rp5c01_write(priv, tm->tm_mday % 10, RP5C01_1_DAY);
  112. if (tm->tm_wday != -1)
  113. rp5c01_write(priv, tm->tm_wday, RP5C01_DAY_OF_WEEK);
  114. rp5c01_write(priv, (tm->tm_mon + 1) / 10, RP5C01_10_MONTH);
  115. rp5c01_write(priv, (tm->tm_mon + 1) % 10, RP5C01_1_MONTH);
  116. if (tm->tm_year >= 100)
  117. tm->tm_year -= 100;
  118. rp5c01_write(priv, tm->tm_year / 10, RP5C01_10_YEAR);
  119. rp5c01_write(priv, tm->tm_year % 10, RP5C01_1_YEAR);
  120. rp5c01_unlock(priv);
  121. spin_unlock_irq(&priv->lock);
  122. return 0;
  123. }
  124. static const struct rtc_class_ops rp5c01_rtc_ops = {
  125. .read_time = rp5c01_read_time,
  126. .set_time = rp5c01_set_time,
  127. };
  128. /*
  129. * The NVRAM is organized as 2 blocks of 13 nibbles of 4 bits.
  130. * We provide access to them like AmigaOS does: the high nibble of each 8-bit
  131. * byte is stored in BLOCK10, the low nibble in BLOCK11.
  132. */
  133. static int rp5c01_nvram_read(void *_priv, unsigned int pos, void *val,
  134. size_t bytes)
  135. {
  136. struct rp5c01_priv *priv = _priv;
  137. u8 *buf = val;
  138. spin_lock_irq(&priv->lock);
  139. for (; bytes; bytes--) {
  140. u8 data;
  141. rp5c01_write(priv,
  142. RP5C01_MODE_TIMER_EN | RP5C01_MODE_RAM_BLOCK10,
  143. RP5C01_MODE);
  144. data = rp5c01_read(priv, pos) << 4;
  145. rp5c01_write(priv,
  146. RP5C01_MODE_TIMER_EN | RP5C01_MODE_RAM_BLOCK11,
  147. RP5C01_MODE);
  148. data |= rp5c01_read(priv, pos++);
  149. rp5c01_write(priv, RP5C01_MODE_TIMER_EN | RP5C01_MODE_MODE01,
  150. RP5C01_MODE);
  151. *buf++ = data;
  152. }
  153. spin_unlock_irq(&priv->lock);
  154. return 0;
  155. }
  156. static int rp5c01_nvram_write(void *_priv, unsigned int pos, void *val,
  157. size_t bytes)
  158. {
  159. struct rp5c01_priv *priv = _priv;
  160. u8 *buf = val;
  161. spin_lock_irq(&priv->lock);
  162. for (; bytes; bytes--) {
  163. u8 data = *buf++;
  164. rp5c01_write(priv,
  165. RP5C01_MODE_TIMER_EN | RP5C01_MODE_RAM_BLOCK10,
  166. RP5C01_MODE);
  167. rp5c01_write(priv, data >> 4, pos);
  168. rp5c01_write(priv,
  169. RP5C01_MODE_TIMER_EN | RP5C01_MODE_RAM_BLOCK11,
  170. RP5C01_MODE);
  171. rp5c01_write(priv, data & 0xf, pos++);
  172. rp5c01_write(priv, RP5C01_MODE_TIMER_EN | RP5C01_MODE_MODE01,
  173. RP5C01_MODE);
  174. }
  175. spin_unlock_irq(&priv->lock);
  176. return 0;
  177. }
  178. static int __init rp5c01_rtc_probe(struct platform_device *dev)
  179. {
  180. struct resource *res;
  181. struct rp5c01_priv *priv;
  182. struct rtc_device *rtc;
  183. int error;
  184. struct nvmem_config nvmem_cfg = {
  185. .name = "rp5c01_nvram",
  186. .word_size = 1,
  187. .stride = 1,
  188. .size = RP5C01_MODE,
  189. .reg_read = rp5c01_nvram_read,
  190. .reg_write = rp5c01_nvram_write,
  191. };
  192. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  193. if (!res)
  194. return -ENODEV;
  195. priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL);
  196. if (!priv)
  197. return -ENOMEM;
  198. priv->regs = devm_ioremap(&dev->dev, res->start, resource_size(res));
  199. if (!priv->regs)
  200. return -ENOMEM;
  201. spin_lock_init(&priv->lock);
  202. platform_set_drvdata(dev, priv);
  203. rtc = devm_rtc_allocate_device(&dev->dev);
  204. if (IS_ERR(rtc))
  205. return PTR_ERR(rtc);
  206. rtc->ops = &rp5c01_rtc_ops;
  207. rtc->nvram_old_abi = true;
  208. priv->rtc = rtc;
  209. nvmem_cfg.priv = priv;
  210. error = rtc_nvmem_register(rtc, &nvmem_cfg);
  211. if (error)
  212. return error;
  213. return rtc_register_device(rtc);
  214. }
  215. static struct platform_driver rp5c01_rtc_driver = {
  216. .driver = {
  217. .name = "rtc-rp5c01",
  218. },
  219. };
  220. module_platform_driver_probe(rp5c01_rtc_driver, rp5c01_rtc_probe);
  221. MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>");
  222. MODULE_LICENSE("GPL");
  223. MODULE_DESCRIPTION("Ricoh RP5C01 RTC driver");
  224. MODULE_ALIAS("platform:rtc-rp5c01");