rtc-m48t59.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * ST M48T59 RTC driver
  3. *
  4. * Copyright (c) 2007 Wind River Systems, Inc.
  5. *
  6. * Author: Mark Zhan <rongkai.zhan@windriver.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/rtc.h>
  19. #include <linux/rtc/m48t59.h>
  20. #include <linux/bcd.h>
  21. #include <linux/slab.h>
  22. #ifndef NO_IRQ
  23. #define NO_IRQ (-1)
  24. #endif
  25. #define M48T59_READ(reg) (pdata->read_byte(dev, pdata->offset + reg))
  26. #define M48T59_WRITE(val, reg) \
  27. (pdata->write_byte(dev, pdata->offset + reg, val))
  28. #define M48T59_SET_BITS(mask, reg) \
  29. M48T59_WRITE((M48T59_READ(reg) | (mask)), (reg))
  30. #define M48T59_CLEAR_BITS(mask, reg) \
  31. M48T59_WRITE((M48T59_READ(reg) & ~(mask)), (reg))
  32. struct m48t59_private {
  33. void __iomem *ioaddr;
  34. int irq;
  35. struct rtc_device *rtc;
  36. spinlock_t lock; /* serialize the NVRAM and RTC access */
  37. };
  38. /*
  39. * This is the generic access method when the chip is memory-mapped
  40. */
  41. static void
  42. m48t59_mem_writeb(struct device *dev, u32 ofs, u8 val)
  43. {
  44. struct m48t59_private *m48t59 = dev_get_drvdata(dev);
  45. writeb(val, m48t59->ioaddr+ofs);
  46. }
  47. static u8
  48. m48t59_mem_readb(struct device *dev, u32 ofs)
  49. {
  50. struct m48t59_private *m48t59 = dev_get_drvdata(dev);
  51. return readb(m48t59->ioaddr+ofs);
  52. }
  53. /*
  54. * NOTE: M48T59 only uses BCD mode
  55. */
  56. static int m48t59_rtc_read_time(struct device *dev, struct rtc_time *tm)
  57. {
  58. struct m48t59_plat_data *pdata = dev_get_platdata(dev);
  59. struct m48t59_private *m48t59 = dev_get_drvdata(dev);
  60. unsigned long flags;
  61. u8 val;
  62. spin_lock_irqsave(&m48t59->lock, flags);
  63. /* Issue the READ command */
  64. M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
  65. tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR));
  66. /* tm_mon is 0-11 */
  67. tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1;
  68. tm->tm_mday = bcd2bin(M48T59_READ(M48T59_MDAY));
  69. val = M48T59_READ(M48T59_WDAY);
  70. if ((pdata->type == M48T59RTC_TYPE_M48T59) &&
  71. (val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB)) {
  72. dev_dbg(dev, "Century bit is enabled\n");
  73. tm->tm_year += 100; /* one century */
  74. }
  75. #ifdef CONFIG_SPARC
  76. /* Sun SPARC machines count years since 1968 */
  77. tm->tm_year += 68;
  78. #endif
  79. tm->tm_wday = bcd2bin(val & 0x07);
  80. tm->tm_hour = bcd2bin(M48T59_READ(M48T59_HOUR) & 0x3F);
  81. tm->tm_min = bcd2bin(M48T59_READ(M48T59_MIN) & 0x7F);
  82. tm->tm_sec = bcd2bin(M48T59_READ(M48T59_SEC) & 0x7F);
  83. /* Clear the READ bit */
  84. M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL);
  85. spin_unlock_irqrestore(&m48t59->lock, flags);
  86. dev_dbg(dev, "RTC read time %04d-%02d-%02d %02d/%02d/%02d\n",
  87. tm->tm_year + 1900, tm->tm_mon, tm->tm_mday,
  88. tm->tm_hour, tm->tm_min, tm->tm_sec);
  89. return 0;
  90. }
  91. static int m48t59_rtc_set_time(struct device *dev, struct rtc_time *tm)
  92. {
  93. struct m48t59_plat_data *pdata = dev_get_platdata(dev);
  94. struct m48t59_private *m48t59 = dev_get_drvdata(dev);
  95. unsigned long flags;
  96. u8 val = 0;
  97. int year = tm->tm_year;
  98. #ifdef CONFIG_SPARC
  99. /* Sun SPARC machines count years since 1968 */
  100. year -= 68;
  101. #endif
  102. dev_dbg(dev, "RTC set time %04d-%02d-%02d %02d/%02d/%02d\n",
  103. year + 1900, tm->tm_mon, tm->tm_mday,
  104. tm->tm_hour, tm->tm_min, tm->tm_sec);
  105. if (year < 0)
  106. return -EINVAL;
  107. spin_lock_irqsave(&m48t59->lock, flags);
  108. /* Issue the WRITE command */
  109. M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
  110. M48T59_WRITE((bin2bcd(tm->tm_sec) & 0x7F), M48T59_SEC);
  111. M48T59_WRITE((bin2bcd(tm->tm_min) & 0x7F), M48T59_MIN);
  112. M48T59_WRITE((bin2bcd(tm->tm_hour) & 0x3F), M48T59_HOUR);
  113. M48T59_WRITE((bin2bcd(tm->tm_mday) & 0x3F), M48T59_MDAY);
  114. /* tm_mon is 0-11 */
  115. M48T59_WRITE((bin2bcd(tm->tm_mon + 1) & 0x1F), M48T59_MONTH);
  116. M48T59_WRITE(bin2bcd(year % 100), M48T59_YEAR);
  117. if (pdata->type == M48T59RTC_TYPE_M48T59 && (year / 100))
  118. val = (M48T59_WDAY_CEB | M48T59_WDAY_CB);
  119. val |= (bin2bcd(tm->tm_wday) & 0x07);
  120. M48T59_WRITE(val, M48T59_WDAY);
  121. /* Clear the WRITE bit */
  122. M48T59_CLEAR_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
  123. spin_unlock_irqrestore(&m48t59->lock, flags);
  124. return 0;
  125. }
  126. /*
  127. * Read alarm time and date in RTC
  128. */
  129. static int m48t59_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
  130. {
  131. struct m48t59_plat_data *pdata = dev_get_platdata(dev);
  132. struct m48t59_private *m48t59 = dev_get_drvdata(dev);
  133. struct rtc_time *tm = &alrm->time;
  134. unsigned long flags;
  135. u8 val;
  136. /* If no irq, we don't support ALARM */
  137. if (m48t59->irq == NO_IRQ)
  138. return -EIO;
  139. spin_lock_irqsave(&m48t59->lock, flags);
  140. /* Issue the READ command */
  141. M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
  142. tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR));
  143. #ifdef CONFIG_SPARC
  144. /* Sun SPARC machines count years since 1968 */
  145. tm->tm_year += 68;
  146. #endif
  147. /* tm_mon is 0-11 */
  148. tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1;
  149. val = M48T59_READ(M48T59_WDAY);
  150. if ((val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB))
  151. tm->tm_year += 100; /* one century */
  152. tm->tm_mday = bcd2bin(M48T59_READ(M48T59_ALARM_DATE));
  153. tm->tm_hour = bcd2bin(M48T59_READ(M48T59_ALARM_HOUR));
  154. tm->tm_min = bcd2bin(M48T59_READ(M48T59_ALARM_MIN));
  155. tm->tm_sec = bcd2bin(M48T59_READ(M48T59_ALARM_SEC));
  156. /* Clear the READ bit */
  157. M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL);
  158. spin_unlock_irqrestore(&m48t59->lock, flags);
  159. dev_dbg(dev, "RTC read alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
  160. tm->tm_year + 1900, tm->tm_mon, tm->tm_mday,
  161. tm->tm_hour, tm->tm_min, tm->tm_sec);
  162. return rtc_valid_tm(tm);
  163. }
  164. /*
  165. * Set alarm time and date in RTC
  166. */
  167. static int m48t59_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  168. {
  169. struct m48t59_plat_data *pdata = dev_get_platdata(dev);
  170. struct m48t59_private *m48t59 = dev_get_drvdata(dev);
  171. struct rtc_time *tm = &alrm->time;
  172. u8 mday, hour, min, sec;
  173. unsigned long flags;
  174. int year = tm->tm_year;
  175. #ifdef CONFIG_SPARC
  176. /* Sun SPARC machines count years since 1968 */
  177. year -= 68;
  178. #endif
  179. /* If no irq, we don't support ALARM */
  180. if (m48t59->irq == NO_IRQ)
  181. return -EIO;
  182. if (year < 0)
  183. return -EINVAL;
  184. /*
  185. * 0xff means "always match"
  186. */
  187. mday = tm->tm_mday;
  188. mday = (mday >= 1 && mday <= 31) ? bin2bcd(mday) : 0xff;
  189. if (mday == 0xff)
  190. mday = M48T59_READ(M48T59_MDAY);
  191. hour = tm->tm_hour;
  192. hour = (hour < 24) ? bin2bcd(hour) : 0x00;
  193. min = tm->tm_min;
  194. min = (min < 60) ? bin2bcd(min) : 0x00;
  195. sec = tm->tm_sec;
  196. sec = (sec < 60) ? bin2bcd(sec) : 0x00;
  197. spin_lock_irqsave(&m48t59->lock, flags);
  198. /* Issue the WRITE command */
  199. M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
  200. M48T59_WRITE(mday, M48T59_ALARM_DATE);
  201. M48T59_WRITE(hour, M48T59_ALARM_HOUR);
  202. M48T59_WRITE(min, M48T59_ALARM_MIN);
  203. M48T59_WRITE(sec, M48T59_ALARM_SEC);
  204. /* Clear the WRITE bit */
  205. M48T59_CLEAR_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
  206. spin_unlock_irqrestore(&m48t59->lock, flags);
  207. dev_dbg(dev, "RTC set alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
  208. year + 1900, tm->tm_mon, tm->tm_mday,
  209. tm->tm_hour, tm->tm_min, tm->tm_sec);
  210. return 0;
  211. }
  212. /*
  213. * Handle commands from user-space
  214. */
  215. static int m48t59_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  216. {
  217. struct m48t59_plat_data *pdata = dev_get_platdata(dev);
  218. struct m48t59_private *m48t59 = dev_get_drvdata(dev);
  219. unsigned long flags;
  220. spin_lock_irqsave(&m48t59->lock, flags);
  221. if (enabled)
  222. M48T59_WRITE(M48T59_INTR_AFE, M48T59_INTR);
  223. else
  224. M48T59_WRITE(0x00, M48T59_INTR);
  225. spin_unlock_irqrestore(&m48t59->lock, flags);
  226. return 0;
  227. }
  228. static int m48t59_rtc_proc(struct device *dev, struct seq_file *seq)
  229. {
  230. struct m48t59_plat_data *pdata = dev_get_platdata(dev);
  231. struct m48t59_private *m48t59 = dev_get_drvdata(dev);
  232. unsigned long flags;
  233. u8 val;
  234. spin_lock_irqsave(&m48t59->lock, flags);
  235. val = M48T59_READ(M48T59_FLAGS);
  236. spin_unlock_irqrestore(&m48t59->lock, flags);
  237. seq_printf(seq, "battery\t\t: %s\n",
  238. (val & M48T59_FLAGS_BF) ? "low" : "normal");
  239. return 0;
  240. }
  241. /*
  242. * IRQ handler for the RTC
  243. */
  244. static irqreturn_t m48t59_rtc_interrupt(int irq, void *dev_id)
  245. {
  246. struct device *dev = (struct device *)dev_id;
  247. struct m48t59_plat_data *pdata = dev_get_platdata(dev);
  248. struct m48t59_private *m48t59 = dev_get_drvdata(dev);
  249. u8 event;
  250. spin_lock(&m48t59->lock);
  251. event = M48T59_READ(M48T59_FLAGS);
  252. spin_unlock(&m48t59->lock);
  253. if (event & M48T59_FLAGS_AF) {
  254. rtc_update_irq(m48t59->rtc, 1, (RTC_AF | RTC_IRQF));
  255. return IRQ_HANDLED;
  256. }
  257. return IRQ_NONE;
  258. }
  259. static const struct rtc_class_ops m48t59_rtc_ops = {
  260. .read_time = m48t59_rtc_read_time,
  261. .set_time = m48t59_rtc_set_time,
  262. .read_alarm = m48t59_rtc_readalarm,
  263. .set_alarm = m48t59_rtc_setalarm,
  264. .proc = m48t59_rtc_proc,
  265. .alarm_irq_enable = m48t59_rtc_alarm_irq_enable,
  266. };
  267. static const struct rtc_class_ops m48t02_rtc_ops = {
  268. .read_time = m48t59_rtc_read_time,
  269. .set_time = m48t59_rtc_set_time,
  270. };
  271. static int m48t59_nvram_read(void *priv, unsigned int offset, void *val,
  272. size_t size)
  273. {
  274. struct platform_device *pdev = priv;
  275. struct device *dev = &pdev->dev;
  276. struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
  277. struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
  278. ssize_t cnt = 0;
  279. unsigned long flags;
  280. u8 *buf = val;
  281. spin_lock_irqsave(&m48t59->lock, flags);
  282. for (; cnt < size; cnt++)
  283. *buf++ = M48T59_READ(cnt);
  284. spin_unlock_irqrestore(&m48t59->lock, flags);
  285. return 0;
  286. }
  287. static int m48t59_nvram_write(void *priv, unsigned int offset, void *val,
  288. size_t size)
  289. {
  290. struct platform_device *pdev = priv;
  291. struct device *dev = &pdev->dev;
  292. struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
  293. struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
  294. ssize_t cnt = 0;
  295. unsigned long flags;
  296. u8 *buf = val;
  297. spin_lock_irqsave(&m48t59->lock, flags);
  298. for (; cnt < size; cnt++)
  299. M48T59_WRITE(*buf++, cnt);
  300. spin_unlock_irqrestore(&m48t59->lock, flags);
  301. return 0;
  302. }
  303. static int m48t59_rtc_probe(struct platform_device *pdev)
  304. {
  305. struct m48t59_plat_data *pdata = dev_get_platdata(&pdev->dev);
  306. struct m48t59_private *m48t59 = NULL;
  307. struct resource *res;
  308. int ret = -ENOMEM;
  309. const struct rtc_class_ops *ops;
  310. struct nvmem_config nvmem_cfg = {
  311. .name = "m48t59-",
  312. .word_size = 1,
  313. .stride = 1,
  314. .reg_read = m48t59_nvram_read,
  315. .reg_write = m48t59_nvram_write,
  316. .priv = pdev,
  317. };
  318. /* This chip could be memory-mapped or I/O-mapped */
  319. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  320. if (!res) {
  321. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  322. if (!res)
  323. return -EINVAL;
  324. }
  325. if (res->flags & IORESOURCE_IO) {
  326. /* If we are I/O-mapped, the platform should provide
  327. * the operations accessing chip registers.
  328. */
  329. if (!pdata || !pdata->write_byte || !pdata->read_byte)
  330. return -EINVAL;
  331. } else if (res->flags & IORESOURCE_MEM) {
  332. /* we are memory-mapped */
  333. if (!pdata) {
  334. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata),
  335. GFP_KERNEL);
  336. if (!pdata)
  337. return -ENOMEM;
  338. /* Ensure we only kmalloc platform data once */
  339. pdev->dev.platform_data = pdata;
  340. }
  341. if (!pdata->type)
  342. pdata->type = M48T59RTC_TYPE_M48T59;
  343. /* Try to use the generic memory read/write ops */
  344. if (!pdata->write_byte)
  345. pdata->write_byte = m48t59_mem_writeb;
  346. if (!pdata->read_byte)
  347. pdata->read_byte = m48t59_mem_readb;
  348. }
  349. m48t59 = devm_kzalloc(&pdev->dev, sizeof(*m48t59), GFP_KERNEL);
  350. if (!m48t59)
  351. return -ENOMEM;
  352. m48t59->ioaddr = pdata->ioaddr;
  353. if (!m48t59->ioaddr) {
  354. /* ioaddr not mapped externally */
  355. m48t59->ioaddr = devm_ioremap(&pdev->dev, res->start,
  356. resource_size(res));
  357. if (!m48t59->ioaddr)
  358. return ret;
  359. }
  360. /* Try to get irq number. We also can work in
  361. * the mode without IRQ.
  362. */
  363. m48t59->irq = platform_get_irq(pdev, 0);
  364. if (m48t59->irq <= 0)
  365. m48t59->irq = NO_IRQ;
  366. if (m48t59->irq != NO_IRQ) {
  367. ret = devm_request_irq(&pdev->dev, m48t59->irq,
  368. m48t59_rtc_interrupt, IRQF_SHARED,
  369. "rtc-m48t59", &pdev->dev);
  370. if (ret)
  371. return ret;
  372. }
  373. switch (pdata->type) {
  374. case M48T59RTC_TYPE_M48T59:
  375. ops = &m48t59_rtc_ops;
  376. pdata->offset = 0x1ff0;
  377. break;
  378. case M48T59RTC_TYPE_M48T02:
  379. ops = &m48t02_rtc_ops;
  380. pdata->offset = 0x7f0;
  381. break;
  382. case M48T59RTC_TYPE_M48T08:
  383. ops = &m48t02_rtc_ops;
  384. pdata->offset = 0x1ff0;
  385. break;
  386. default:
  387. dev_err(&pdev->dev, "Unknown RTC type\n");
  388. return -ENODEV;
  389. }
  390. spin_lock_init(&m48t59->lock);
  391. platform_set_drvdata(pdev, m48t59);
  392. m48t59->rtc = devm_rtc_allocate_device(&pdev->dev);
  393. if (IS_ERR(m48t59->rtc))
  394. return PTR_ERR(m48t59->rtc);
  395. m48t59->rtc->nvram_old_abi = true;
  396. m48t59->rtc->ops = ops;
  397. nvmem_cfg.size = pdata->offset;
  398. ret = rtc_nvmem_register(m48t59->rtc, &nvmem_cfg);
  399. if (ret)
  400. return ret;
  401. ret = rtc_register_device(m48t59->rtc);
  402. if (ret)
  403. return ret;
  404. return 0;
  405. }
  406. /* work with hotplug and coldplug */
  407. MODULE_ALIAS("platform:rtc-m48t59");
  408. static struct platform_driver m48t59_rtc_driver = {
  409. .driver = {
  410. .name = "rtc-m48t59",
  411. },
  412. .probe = m48t59_rtc_probe,
  413. };
  414. module_platform_driver(m48t59_rtc_driver);
  415. MODULE_AUTHOR("Mark Zhan <rongkai.zhan@windriver.com>");
  416. MODULE_DESCRIPTION("M48T59/M48T02/M48T08 RTC driver");
  417. MODULE_LICENSE("GPL");