rtc-s3c.c 13 KB

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