rtc-s3c.c 13 KB

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