rtc-rs5c313.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Ricoh RS5C313 RTC device/driver
  3. * Copyright (C) 2007 Nobuhiro Iwamatsu
  4. *
  5. * 2005-09-19 modifed by kogiidena
  6. *
  7. * Based on the old drivers/char/rs5c313_rtc.c by:
  8. * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
  9. * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
  10. *
  11. * Based on code written by Paul Gortmaker.
  12. * Copyright (C) 1996 Paul Gortmaker
  13. *
  14. * This file is subject to the terms and conditions of the GNU General Public
  15. * License. See the file "COPYING" in the main directory of this archive
  16. * for more details.
  17. *
  18. * Based on other minimal char device drivers, like Alan's
  19. * watchdog, Ted's random, etc. etc.
  20. *
  21. * 1.07 Paul Gortmaker.
  22. * 1.08 Miquel van Smoorenburg: disallow certain things on the
  23. * DEC Alpha as the CMOS clock is also used for other things.
  24. * 1.09 Nikita Schmidt: epoch support and some Alpha cleanup.
  25. * 1.09a Pete Zaitcev: Sun SPARC
  26. * 1.09b Jeff Garzik: Modularize, init cleanup
  27. * 1.09c Jeff Garzik: SMP cleanup
  28. * 1.10 Paul Barton-Davis: add support for async I/O
  29. * 1.10a Andrea Arcangeli: Alpha updates
  30. * 1.10b Andrew Morton: SMP lock fix
  31. * 1.10c Cesar Barros: SMP locking fixes and cleanup
  32. * 1.10d Paul Gortmaker: delete paranoia check in rtc_exit
  33. * 1.10e Maciej W. Rozycki: Handle DECstation's year weirdness.
  34. * 1.11 Takashi Iwai: Kernel access functions
  35. * rtc_register/rtc_unregister/rtc_control
  36. * 1.11a Daniele Bellucci: Audit create_proc_read_entry in rtc_init
  37. * 1.12 Venkatesh Pallipadi: Hooks for emulating rtc on HPET base-timer
  38. * CONFIG_HPET_EMULATE_RTC
  39. * 1.13 Nobuhiro Iwamatsu: Updata driver.
  40. */
  41. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  42. #include <linux/module.h>
  43. #include <linux/err.h>
  44. #include <linux/rtc.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/bcd.h>
  47. #include <linux/delay.h>
  48. #include <linux/io.h>
  49. #define DRV_NAME "rs5c313"
  50. #define DRV_VERSION "1.13"
  51. #ifdef CONFIG_SH_LANDISK
  52. /*****************************************************/
  53. /* LANDISK dependence part of RS5C313 */
  54. /*****************************************************/
  55. #define SCSMR1 0xFFE00000
  56. #define SCSCR1 0xFFE00008
  57. #define SCSMR1_CA 0x80
  58. #define SCSCR1_CKE 0x03
  59. #define SCSPTR1 0xFFE0001C
  60. #define SCSPTR1_EIO 0x80
  61. #define SCSPTR1_SPB1IO 0x08
  62. #define SCSPTR1_SPB1DT 0x04
  63. #define SCSPTR1_SPB0IO 0x02
  64. #define SCSPTR1_SPB0DT 0x01
  65. #define SDA_OEN SCSPTR1_SPB1IO
  66. #define SDA SCSPTR1_SPB1DT
  67. #define SCL_OEN SCSPTR1_SPB0IO
  68. #define SCL SCSPTR1_SPB0DT
  69. /* RICOH RS5C313 CE port */
  70. #define RS5C313_CE 0xB0000003
  71. /* RICOH RS5C313 CE port bit */
  72. #define RS5C313_CE_RTCCE 0x02
  73. /* SCSPTR1 data */
  74. unsigned char scsptr1_data;
  75. #define RS5C313_CEENABLE __raw_writeb(RS5C313_CE_RTCCE, RS5C313_CE);
  76. #define RS5C313_CEDISABLE __raw_writeb(0x00, RS5C313_CE)
  77. #define RS5C313_MISCOP __raw_writeb(0x02, 0xB0000008)
  78. static void rs5c313_init_port(void)
  79. {
  80. /* Set SCK as I/O port and Initialize SCSPTR1 data & I/O port. */
  81. __raw_writeb(__raw_readb(SCSMR1) & ~SCSMR1_CA, SCSMR1);
  82. __raw_writeb(__raw_readb(SCSCR1) & ~SCSCR1_CKE, SCSCR1);
  83. /* And Initialize SCL for RS5C313 clock */
  84. scsptr1_data = __raw_readb(SCSPTR1) | SCL; /* SCL:H */
  85. __raw_writeb(scsptr1_data, SCSPTR1);
  86. scsptr1_data = __raw_readb(SCSPTR1) | SCL_OEN; /* SCL output enable */
  87. __raw_writeb(scsptr1_data, SCSPTR1);
  88. RS5C313_CEDISABLE; /* CE:L */
  89. }
  90. static void rs5c313_write_data(unsigned char data)
  91. {
  92. int i;
  93. for (i = 0; i < 8; i++) {
  94. /* SDA:Write Data */
  95. scsptr1_data = (scsptr1_data & ~SDA) |
  96. ((((0x80 >> i) & data) >> (7 - i)) << 2);
  97. __raw_writeb(scsptr1_data, SCSPTR1);
  98. if (i == 0) {
  99. scsptr1_data |= SDA_OEN; /* SDA:output enable */
  100. __raw_writeb(scsptr1_data, SCSPTR1);
  101. }
  102. ndelay(700);
  103. scsptr1_data &= ~SCL; /* SCL:L */
  104. __raw_writeb(scsptr1_data, SCSPTR1);
  105. ndelay(700);
  106. scsptr1_data |= SCL; /* SCL:H */
  107. __raw_writeb(scsptr1_data, SCSPTR1);
  108. }
  109. scsptr1_data &= ~SDA_OEN; /* SDA:output disable */
  110. __raw_writeb(scsptr1_data, SCSPTR1);
  111. }
  112. static unsigned char rs5c313_read_data(void)
  113. {
  114. int i;
  115. unsigned char data = 0;
  116. for (i = 0; i < 8; i++) {
  117. ndelay(700);
  118. /* SDA:Read Data */
  119. data |= ((__raw_readb(SCSPTR1) & SDA) >> 2) << (7 - i);
  120. scsptr1_data &= ~SCL; /* SCL:L */
  121. __raw_writeb(scsptr1_data, SCSPTR1);
  122. ndelay(700);
  123. scsptr1_data |= SCL; /* SCL:H */
  124. __raw_writeb(scsptr1_data, SCSPTR1);
  125. }
  126. return data & 0x0F;
  127. }
  128. #endif /* CONFIG_SH_LANDISK */
  129. /*****************************************************/
  130. /* machine independence part of RS5C313 */
  131. /*****************************************************/
  132. /* RICOH RS5C313 address */
  133. #define RS5C313_ADDR_SEC 0x00
  134. #define RS5C313_ADDR_SEC10 0x01
  135. #define RS5C313_ADDR_MIN 0x02
  136. #define RS5C313_ADDR_MIN10 0x03
  137. #define RS5C313_ADDR_HOUR 0x04
  138. #define RS5C313_ADDR_HOUR10 0x05
  139. #define RS5C313_ADDR_WEEK 0x06
  140. #define RS5C313_ADDR_INTINTVREG 0x07
  141. #define RS5C313_ADDR_DAY 0x08
  142. #define RS5C313_ADDR_DAY10 0x09
  143. #define RS5C313_ADDR_MON 0x0A
  144. #define RS5C313_ADDR_MON10 0x0B
  145. #define RS5C313_ADDR_YEAR 0x0C
  146. #define RS5C313_ADDR_YEAR10 0x0D
  147. #define RS5C313_ADDR_CNTREG 0x0E
  148. #define RS5C313_ADDR_TESTREG 0x0F
  149. /* RICOH RS5C313 control register */
  150. #define RS5C313_CNTREG_ADJ_BSY 0x01
  151. #define RS5C313_CNTREG_WTEN_XSTP 0x02
  152. #define RS5C313_CNTREG_12_24 0x04
  153. #define RS5C313_CNTREG_CTFG 0x08
  154. /* RICOH RS5C313 test register */
  155. #define RS5C313_TESTREG_TEST 0x01
  156. /* RICOH RS5C313 control bit */
  157. #define RS5C313_CNTBIT_READ 0x40
  158. #define RS5C313_CNTBIT_AD 0x20
  159. #define RS5C313_CNTBIT_DT 0x10
  160. static unsigned char rs5c313_read_reg(unsigned char addr)
  161. {
  162. rs5c313_write_data(addr | RS5C313_CNTBIT_READ | RS5C313_CNTBIT_AD);
  163. return rs5c313_read_data();
  164. }
  165. static void rs5c313_write_reg(unsigned char addr, unsigned char data)
  166. {
  167. data &= 0x0f;
  168. rs5c313_write_data(addr | RS5C313_CNTBIT_AD);
  169. rs5c313_write_data(data | RS5C313_CNTBIT_DT);
  170. return;
  171. }
  172. static inline unsigned char rs5c313_read_cntreg(void)
  173. {
  174. return rs5c313_read_reg(RS5C313_ADDR_CNTREG);
  175. }
  176. static inline void rs5c313_write_cntreg(unsigned char data)
  177. {
  178. rs5c313_write_reg(RS5C313_ADDR_CNTREG, data);
  179. }
  180. static inline void rs5c313_write_intintvreg(unsigned char data)
  181. {
  182. rs5c313_write_reg(RS5C313_ADDR_INTINTVREG, data);
  183. }
  184. static int rs5c313_rtc_read_time(struct device *dev, struct rtc_time *tm)
  185. {
  186. int data;
  187. int cnt;
  188. cnt = 0;
  189. while (1) {
  190. RS5C313_CEENABLE; /* CE:H */
  191. /* Initialize control reg. 24 hour */
  192. rs5c313_write_cntreg(0x04);
  193. if (!(rs5c313_read_cntreg() & RS5C313_CNTREG_ADJ_BSY))
  194. break;
  195. RS5C313_CEDISABLE;
  196. ndelay(700); /* CE:L */
  197. if (cnt++ > 100) {
  198. dev_err(dev, "%s: timeout error\n", __func__);
  199. return -EIO;
  200. }
  201. }
  202. data = rs5c313_read_reg(RS5C313_ADDR_SEC);
  203. data |= (rs5c313_read_reg(RS5C313_ADDR_SEC10) << 4);
  204. tm->tm_sec = bcd2bin(data);
  205. data = rs5c313_read_reg(RS5C313_ADDR_MIN);
  206. data |= (rs5c313_read_reg(RS5C313_ADDR_MIN10) << 4);
  207. tm->tm_min = bcd2bin(data);
  208. data = rs5c313_read_reg(RS5C313_ADDR_HOUR);
  209. data |= (rs5c313_read_reg(RS5C313_ADDR_HOUR10) << 4);
  210. tm->tm_hour = bcd2bin(data);
  211. data = rs5c313_read_reg(RS5C313_ADDR_DAY);
  212. data |= (rs5c313_read_reg(RS5C313_ADDR_DAY10) << 4);
  213. tm->tm_mday = bcd2bin(data);
  214. data = rs5c313_read_reg(RS5C313_ADDR_MON);
  215. data |= (rs5c313_read_reg(RS5C313_ADDR_MON10) << 4);
  216. tm->tm_mon = bcd2bin(data) - 1;
  217. data = rs5c313_read_reg(RS5C313_ADDR_YEAR);
  218. data |= (rs5c313_read_reg(RS5C313_ADDR_YEAR10) << 4);
  219. tm->tm_year = bcd2bin(data);
  220. if (tm->tm_year < 70)
  221. tm->tm_year += 100;
  222. data = rs5c313_read_reg(RS5C313_ADDR_WEEK);
  223. tm->tm_wday = bcd2bin(data);
  224. RS5C313_CEDISABLE;
  225. ndelay(700); /* CE:L */
  226. return 0;
  227. }
  228. static int rs5c313_rtc_set_time(struct device *dev, struct rtc_time *tm)
  229. {
  230. int data;
  231. int cnt;
  232. cnt = 0;
  233. /* busy check. */
  234. while (1) {
  235. RS5C313_CEENABLE; /* CE:H */
  236. /* Initiatlize control reg. 24 hour */
  237. rs5c313_write_cntreg(0x04);
  238. if (!(rs5c313_read_cntreg() & RS5C313_CNTREG_ADJ_BSY))
  239. break;
  240. RS5C313_MISCOP;
  241. RS5C313_CEDISABLE;
  242. ndelay(700); /* CE:L */
  243. if (cnt++ > 100) {
  244. dev_err(dev, "%s: timeout error\n", __func__);
  245. return -EIO;
  246. }
  247. }
  248. data = bin2bcd(tm->tm_sec);
  249. rs5c313_write_reg(RS5C313_ADDR_SEC, data);
  250. rs5c313_write_reg(RS5C313_ADDR_SEC10, (data >> 4));
  251. data = bin2bcd(tm->tm_min);
  252. rs5c313_write_reg(RS5C313_ADDR_MIN, data);
  253. rs5c313_write_reg(RS5C313_ADDR_MIN10, (data >> 4));
  254. data = bin2bcd(tm->tm_hour);
  255. rs5c313_write_reg(RS5C313_ADDR_HOUR, data);
  256. rs5c313_write_reg(RS5C313_ADDR_HOUR10, (data >> 4));
  257. data = bin2bcd(tm->tm_mday);
  258. rs5c313_write_reg(RS5C313_ADDR_DAY, data);
  259. rs5c313_write_reg(RS5C313_ADDR_DAY10, (data >> 4));
  260. data = bin2bcd(tm->tm_mon + 1);
  261. rs5c313_write_reg(RS5C313_ADDR_MON, data);
  262. rs5c313_write_reg(RS5C313_ADDR_MON10, (data >> 4));
  263. data = bin2bcd(tm->tm_year % 100);
  264. rs5c313_write_reg(RS5C313_ADDR_YEAR, data);
  265. rs5c313_write_reg(RS5C313_ADDR_YEAR10, (data >> 4));
  266. data = bin2bcd(tm->tm_wday);
  267. rs5c313_write_reg(RS5C313_ADDR_WEEK, data);
  268. RS5C313_CEDISABLE; /* CE:H */
  269. ndelay(700);
  270. return 0;
  271. }
  272. static void rs5c313_check_xstp_bit(void)
  273. {
  274. struct rtc_time tm;
  275. int cnt;
  276. RS5C313_CEENABLE; /* CE:H */
  277. if (rs5c313_read_cntreg() & RS5C313_CNTREG_WTEN_XSTP) {
  278. /* INT interval reg. OFF */
  279. rs5c313_write_intintvreg(0x00);
  280. /* Initialize control reg. 24 hour & adjust */
  281. rs5c313_write_cntreg(0x07);
  282. /* busy check. */
  283. for (cnt = 0; cnt < 100; cnt++) {
  284. if (!(rs5c313_read_cntreg() & RS5C313_CNTREG_ADJ_BSY))
  285. break;
  286. RS5C313_MISCOP;
  287. }
  288. memset(&tm, 0, sizeof(struct rtc_time));
  289. tm.tm_mday = 1;
  290. tm.tm_mon = 1 - 1;
  291. tm.tm_year = 2000 - 1900;
  292. rs5c313_rtc_set_time(NULL, &tm);
  293. pr_err("invalid value, resetting to 1 Jan 2000\n");
  294. }
  295. RS5C313_CEDISABLE;
  296. ndelay(700); /* CE:L */
  297. }
  298. static const struct rtc_class_ops rs5c313_rtc_ops = {
  299. .read_time = rs5c313_rtc_read_time,
  300. .set_time = rs5c313_rtc_set_time,
  301. };
  302. static int rs5c313_rtc_probe(struct platform_device *pdev)
  303. {
  304. struct rtc_device *rtc = devm_rtc_device_register(&pdev->dev, "rs5c313",
  305. &rs5c313_rtc_ops, THIS_MODULE);
  306. if (IS_ERR(rtc))
  307. return PTR_ERR(rtc);
  308. platform_set_drvdata(pdev, rtc);
  309. return 0;
  310. }
  311. static struct platform_driver rs5c313_rtc_platform_driver = {
  312. .driver = {
  313. .name = DRV_NAME,
  314. },
  315. .probe = rs5c313_rtc_probe,
  316. };
  317. static int __init rs5c313_rtc_init(void)
  318. {
  319. int err;
  320. err = platform_driver_register(&rs5c313_rtc_platform_driver);
  321. if (err)
  322. return err;
  323. rs5c313_init_port();
  324. rs5c313_check_xstp_bit();
  325. return 0;
  326. }
  327. static void __exit rs5c313_rtc_exit(void)
  328. {
  329. platform_driver_unregister(&rs5c313_rtc_platform_driver);
  330. }
  331. module_init(rs5c313_rtc_init);
  332. module_exit(rs5c313_rtc_exit);
  333. MODULE_VERSION(DRV_VERSION);
  334. MODULE_AUTHOR("kogiidena , Nobuhiro Iwamatsu <iwamatsu@nigauri.org>");
  335. MODULE_DESCRIPTION("Ricoh RS5C313 RTC device driver");
  336. MODULE_LICENSE("GPL");
  337. MODULE_ALIAS("platform:" DRV_NAME);