rtc-s3c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /* drivers/rtc/rtc-s3c.c
  2. *
  3. * Copyright (c) 2004,2006 Simtec Electronics
  4. * Ben Dooks, <ben@simtec.co.uk>
  5. * http://armlinux.simtec.co.uk/
  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. * S3C2410/S3C2440/S3C24XX Internal RTC Driver
  12. */
  13. #include <linux/module.h>
  14. #include <linux/fs.h>
  15. #include <linux/string.h>
  16. #include <linux/init.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/rtc.h>
  20. #include <linux/bcd.h>
  21. #include <linux/clk.h>
  22. #include <linux/log2.h>
  23. #include <linux/slab.h>
  24. #include <mach/hardware.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/io.h>
  27. #include <asm/irq.h>
  28. #include <plat/regs-rtc.h>
  29. /* I have yet to find an S3C implementation with more than one
  30. * of these rtc blocks in */
  31. static struct resource *s3c_rtc_mem;
  32. static void __iomem *s3c_rtc_base;
  33. static int s3c_rtc_alarmno = NO_IRQ;
  34. static int s3c_rtc_tickno = NO_IRQ;
  35. static DEFINE_SPINLOCK(s3c_rtc_pie_lock);
  36. /* IRQ Handlers */
  37. static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
  38. {
  39. struct rtc_device *rdev = id;
  40. rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
  41. return IRQ_HANDLED;
  42. }
  43. static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
  44. {
  45. struct rtc_device *rdev = id;
  46. rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
  47. return IRQ_HANDLED;
  48. }
  49. /* Update control registers */
  50. static void s3c_rtc_setaie(int to)
  51. {
  52. unsigned int tmp;
  53. pr_debug("%s: aie=%d\n", __func__, to);
  54. tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
  55. if (to)
  56. tmp |= S3C2410_RTCALM_ALMEN;
  57. writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);
  58. }
  59. static int s3c_rtc_setpie(struct device *dev, int enabled)
  60. {
  61. unsigned int tmp;
  62. pr_debug("%s: pie=%d\n", __func__, enabled);
  63. spin_lock_irq(&s3c_rtc_pie_lock);
  64. tmp = readb(s3c_rtc_base + S3C2410_TICNT) & ~S3C2410_TICNT_ENABLE;
  65. if (enabled)
  66. tmp |= S3C2410_TICNT_ENABLE;
  67. writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
  68. spin_unlock_irq(&s3c_rtc_pie_lock);
  69. return 0;
  70. }
  71. static int s3c_rtc_setfreq(struct device *dev, int freq)
  72. {
  73. unsigned int tmp;
  74. if (!is_power_of_2(freq))
  75. return -EINVAL;
  76. spin_lock_irq(&s3c_rtc_pie_lock);
  77. tmp = readb(s3c_rtc_base + S3C2410_TICNT) & S3C2410_TICNT_ENABLE;
  78. tmp |= (128 / freq)-1;
  79. writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
  80. spin_unlock_irq(&s3c_rtc_pie_lock);
  81. return 0;
  82. }
  83. /* Time read/write */
  84. static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
  85. {
  86. unsigned int have_retried = 0;
  87. void __iomem *base = s3c_rtc_base;
  88. retry_get_time:
  89. rtc_tm->tm_min = readb(base + S3C2410_RTCMIN);
  90. rtc_tm->tm_hour = readb(base + S3C2410_RTCHOUR);
  91. rtc_tm->tm_mday = readb(base + S3C2410_RTCDATE);
  92. rtc_tm->tm_mon = readb(base + S3C2410_RTCMON);
  93. rtc_tm->tm_year = readb(base + S3C2410_RTCYEAR);
  94. rtc_tm->tm_sec = readb(base + S3C2410_RTCSEC);
  95. /* the only way to work out wether the system was mid-update
  96. * when we read it is to check the second counter, and if it
  97. * is zero, then we re-try the entire read
  98. */
  99. if (rtc_tm->tm_sec == 0 && !have_retried) {
  100. have_retried = 1;
  101. goto retry_get_time;
  102. }
  103. pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n",
  104. rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
  105. rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
  106. rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
  107. rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
  108. rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
  109. rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
  110. rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
  111. rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
  112. rtc_tm->tm_year += 100;
  113. rtc_tm->tm_mon -= 1;
  114. return 0;
  115. }
  116. static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
  117. {
  118. void __iomem *base = s3c_rtc_base;
  119. int year = tm->tm_year - 100;
  120. pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d\n",
  121. tm->tm_year, tm->tm_mon, tm->tm_mday,
  122. tm->tm_hour, tm->tm_min, tm->tm_sec);
  123. /* we get around y2k by simply not supporting it */
  124. if (year < 0 || year >= 100) {
  125. dev_err(dev, "rtc only supports 100 years\n");
  126. return -EINVAL;
  127. }
  128. writeb(bin2bcd(tm->tm_sec), base + S3C2410_RTCSEC);
  129. writeb(bin2bcd(tm->tm_min), base + S3C2410_RTCMIN);
  130. writeb(bin2bcd(tm->tm_hour), base + S3C2410_RTCHOUR);
  131. writeb(bin2bcd(tm->tm_mday), base + S3C2410_RTCDATE);
  132. writeb(bin2bcd(tm->tm_mon + 1), base + S3C2410_RTCMON);
  133. writeb(bin2bcd(year), base + S3C2410_RTCYEAR);
  134. return 0;
  135. }
  136. static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
  137. {
  138. struct rtc_time *alm_tm = &alrm->time;
  139. void __iomem *base = s3c_rtc_base;
  140. unsigned int alm_en;
  141. alm_tm->tm_sec = readb(base + S3C2410_ALMSEC);
  142. alm_tm->tm_min = readb(base + S3C2410_ALMMIN);
  143. alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);
  144. alm_tm->tm_mon = readb(base + S3C2410_ALMMON);
  145. alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);
  146. alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);
  147. alm_en = readb(base + S3C2410_RTCALM);
  148. alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
  149. pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
  150. alm_en,
  151. alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
  152. alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
  153. /* decode the alarm enable field */
  154. if (alm_en & S3C2410_RTCALM_SECEN)
  155. alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
  156. else
  157. alm_tm->tm_sec = 0xff;
  158. if (alm_en & S3C2410_RTCALM_MINEN)
  159. alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
  160. else
  161. alm_tm->tm_min = 0xff;
  162. if (alm_en & S3C2410_RTCALM_HOUREN)
  163. alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
  164. else
  165. alm_tm->tm_hour = 0xff;
  166. if (alm_en & S3C2410_RTCALM_DAYEN)
  167. alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
  168. else
  169. alm_tm->tm_mday = 0xff;
  170. if (alm_en & S3C2410_RTCALM_MONEN) {
  171. alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
  172. alm_tm->tm_mon -= 1;
  173. } else {
  174. alm_tm->tm_mon = 0xff;
  175. }
  176. if (alm_en & S3C2410_RTCALM_YEAREN)
  177. alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
  178. else
  179. alm_tm->tm_year = 0xffff;
  180. return 0;
  181. }
  182. static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  183. {
  184. struct rtc_time *tm = &alrm->time;
  185. void __iomem *base = s3c_rtc_base;
  186. unsigned int alrm_en;
  187. pr_debug("s3c_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
  188. alrm->enabled,
  189. tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
  190. tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
  191. alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
  192. writeb(0x00, base + S3C2410_RTCALM);
  193. if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
  194. alrm_en |= S3C2410_RTCALM_SECEN;
  195. writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);
  196. }
  197. if (tm->tm_min < 60 && tm->tm_min >= 0) {
  198. alrm_en |= S3C2410_RTCALM_MINEN;
  199. writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);
  200. }
  201. if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
  202. alrm_en |= S3C2410_RTCALM_HOUREN;
  203. writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR);
  204. }
  205. pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
  206. writeb(alrm_en, base + S3C2410_RTCALM);
  207. s3c_rtc_setaie(alrm->enabled);
  208. if (alrm->enabled)
  209. enable_irq_wake(s3c_rtc_alarmno);
  210. else
  211. disable_irq_wake(s3c_rtc_alarmno);
  212. return 0;
  213. }
  214. static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
  215. {
  216. unsigned int ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
  217. seq_printf(seq, "periodic_IRQ\t: %s\n",
  218. (ticnt & S3C2410_TICNT_ENABLE) ? "yes" : "no" );
  219. return 0;
  220. }
  221. static int s3c_rtc_open(struct device *dev)
  222. {
  223. struct platform_device *pdev = to_platform_device(dev);
  224. struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
  225. int ret;
  226. ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,
  227. IRQF_DISABLED, "s3c2410-rtc alarm", rtc_dev);
  228. if (ret) {
  229. dev_err(dev, "IRQ%d error %d\n", s3c_rtc_alarmno, ret);
  230. return ret;
  231. }
  232. ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,
  233. IRQF_DISABLED, "s3c2410-rtc tick", rtc_dev);
  234. if (ret) {
  235. dev_err(dev, "IRQ%d error %d\n", s3c_rtc_tickno, ret);
  236. goto tick_err;
  237. }
  238. return ret;
  239. tick_err:
  240. free_irq(s3c_rtc_alarmno, rtc_dev);
  241. return ret;
  242. }
  243. static void s3c_rtc_release(struct device *dev)
  244. {
  245. struct platform_device *pdev = to_platform_device(dev);
  246. struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
  247. /* do not clear AIE here, it may be needed for wake */
  248. s3c_rtc_setpie(dev, 0);
  249. free_irq(s3c_rtc_alarmno, rtc_dev);
  250. free_irq(s3c_rtc_tickno, rtc_dev);
  251. }
  252. static const struct rtc_class_ops s3c_rtcops = {
  253. .open = s3c_rtc_open,
  254. .release = s3c_rtc_release,
  255. .read_time = s3c_rtc_gettime,
  256. .set_time = s3c_rtc_settime,
  257. .read_alarm = s3c_rtc_getalarm,
  258. .set_alarm = s3c_rtc_setalarm,
  259. .irq_set_freq = s3c_rtc_setfreq,
  260. .irq_set_state = s3c_rtc_setpie,
  261. .proc = s3c_rtc_proc,
  262. };
  263. static void s3c_rtc_enable(struct platform_device *pdev, int en)
  264. {
  265. void __iomem *base = s3c_rtc_base;
  266. unsigned int tmp;
  267. if (s3c_rtc_base == NULL)
  268. return;
  269. if (!en) {
  270. tmp = readb(base + S3C2410_RTCCON);
  271. writeb(tmp & ~S3C2410_RTCCON_RTCEN, base + S3C2410_RTCCON);
  272. tmp = readb(base + S3C2410_TICNT);
  273. writeb(tmp & ~S3C2410_TICNT_ENABLE, base + S3C2410_TICNT);
  274. } else {
  275. /* re-enable the device, and check it is ok */
  276. if ((readb(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0){
  277. dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
  278. tmp = readb(base + S3C2410_RTCCON);
  279. writeb(tmp|S3C2410_RTCCON_RTCEN, base+S3C2410_RTCCON);
  280. }
  281. if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)){
  282. dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
  283. tmp = readb(base + S3C2410_RTCCON);
  284. writeb(tmp& ~S3C2410_RTCCON_CNTSEL, base+S3C2410_RTCCON);
  285. }
  286. if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)){
  287. dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
  288. tmp = readb(base + S3C2410_RTCCON);
  289. writeb(tmp & ~S3C2410_RTCCON_CLKRST, base+S3C2410_RTCCON);
  290. }
  291. }
  292. }
  293. static int __devexit s3c_rtc_remove(struct platform_device *dev)
  294. {
  295. struct rtc_device *rtc = platform_get_drvdata(dev);
  296. platform_set_drvdata(dev, NULL);
  297. rtc_device_unregister(rtc);
  298. s3c_rtc_setpie(&dev->dev, 0);
  299. s3c_rtc_setaie(0);
  300. iounmap(s3c_rtc_base);
  301. release_resource(s3c_rtc_mem);
  302. kfree(s3c_rtc_mem);
  303. return 0;
  304. }
  305. static int __devinit s3c_rtc_probe(struct platform_device *pdev)
  306. {
  307. struct rtc_device *rtc;
  308. struct resource *res;
  309. int ret;
  310. pr_debug("%s: probe=%p\n", __func__, pdev);
  311. /* find the IRQs */
  312. s3c_rtc_tickno = platform_get_irq(pdev, 1);
  313. if (s3c_rtc_tickno < 0) {
  314. dev_err(&pdev->dev, "no irq for rtc tick\n");
  315. return -ENOENT;
  316. }
  317. s3c_rtc_alarmno = platform_get_irq(pdev, 0);
  318. if (s3c_rtc_alarmno < 0) {
  319. dev_err(&pdev->dev, "no irq for alarm\n");
  320. return -ENOENT;
  321. }
  322. pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
  323. s3c_rtc_tickno, s3c_rtc_alarmno);
  324. /* get the memory region */
  325. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  326. if (res == NULL) {
  327. dev_err(&pdev->dev, "failed to get memory region resource\n");
  328. return -ENOENT;
  329. }
  330. s3c_rtc_mem = request_mem_region(res->start,
  331. res->end-res->start+1,
  332. pdev->name);
  333. if (s3c_rtc_mem == NULL) {
  334. dev_err(&pdev->dev, "failed to reserve memory region\n");
  335. ret = -ENOENT;
  336. goto err_nores;
  337. }
  338. s3c_rtc_base = ioremap(res->start, res->end - res->start + 1);
  339. if (s3c_rtc_base == NULL) {
  340. dev_err(&pdev->dev, "failed ioremap()\n");
  341. ret = -EINVAL;
  342. goto err_nomap;
  343. }
  344. /* check to see if everything is setup correctly */
  345. s3c_rtc_enable(pdev, 1);
  346. pr_debug("s3c2410_rtc: RTCCON=%02x\n",
  347. readb(s3c_rtc_base + S3C2410_RTCCON));
  348. s3c_rtc_setfreq(&pdev->dev, 1);
  349. device_init_wakeup(&pdev->dev, 1);
  350. /* register RTC and exit */
  351. rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
  352. THIS_MODULE);
  353. if (IS_ERR(rtc)) {
  354. dev_err(&pdev->dev, "cannot attach rtc\n");
  355. ret = PTR_ERR(rtc);
  356. goto err_nortc;
  357. }
  358. rtc->max_user_freq = 128;
  359. platform_set_drvdata(pdev, rtc);
  360. return 0;
  361. err_nortc:
  362. s3c_rtc_enable(pdev, 0);
  363. iounmap(s3c_rtc_base);
  364. err_nomap:
  365. release_resource(s3c_rtc_mem);
  366. err_nores:
  367. return ret;
  368. }
  369. #ifdef CONFIG_PM
  370. /* RTC Power management control */
  371. static int ticnt_save;
  372. static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
  373. {
  374. /* save TICNT for anyone using periodic interrupts */
  375. ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
  376. s3c_rtc_enable(pdev, 0);
  377. return 0;
  378. }
  379. static int s3c_rtc_resume(struct platform_device *pdev)
  380. {
  381. s3c_rtc_enable(pdev, 1);
  382. writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
  383. return 0;
  384. }
  385. #else
  386. #define s3c_rtc_suspend NULL
  387. #define s3c_rtc_resume NULL
  388. #endif
  389. static struct platform_driver s3c2410_rtc_driver = {
  390. .probe = s3c_rtc_probe,
  391. .remove = __devexit_p(s3c_rtc_remove),
  392. .suspend = s3c_rtc_suspend,
  393. .resume = s3c_rtc_resume,
  394. .driver = {
  395. .name = "s3c2410-rtc",
  396. .owner = THIS_MODULE,
  397. },
  398. };
  399. static char __initdata banner[] = "S3C24XX RTC, (c) 2004,2006 Simtec Electronics\n";
  400. static int __init s3c_rtc_init(void)
  401. {
  402. printk(banner);
  403. return platform_driver_register(&s3c2410_rtc_driver);
  404. }
  405. static void __exit s3c_rtc_exit(void)
  406. {
  407. platform_driver_unregister(&s3c2410_rtc_driver);
  408. }
  409. module_init(s3c_rtc_init);
  410. module_exit(s3c_rtc_exit);
  411. MODULE_DESCRIPTION("Samsung S3C RTC Driver");
  412. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  413. MODULE_LICENSE("GPL");
  414. MODULE_ALIAS("platform:s3c2410-rtc");