s3c2410-rtc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /* drivers/char/s3c2410_rtc.c
  2. *
  3. * Copyright (c) 2004 Simtec Electronics <linux@simtec.co.uk>
  4. * http://www.simtec.co.uk/products/SWLINUX/
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * S3C2410 Internal RTC Driver
  11. *
  12. * Changelog:
  13. * 08-Nov-2004 BJD Initial creation
  14. * 12-Nov-2004 BJD Added periodic IRQ and PM code
  15. * 22-Nov-2004 BJD Sign-test on alarm code to check for <0
  16. * 10-Mar-2005 LCVR Changed S3C2410_VA_RTC to S3C24XX_VA_RTC
  17. */
  18. #include <linux/module.h>
  19. #include <linux/fs.h>
  20. #include <linux/string.h>
  21. #include <linux/init.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/rtc.h>
  25. #include <linux/bcd.h>
  26. #include <linux/clk.h>
  27. #include <asm/hardware.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/io.h>
  30. #include <asm/irq.h>
  31. #include <asm/rtc.h>
  32. #include <asm/mach/time.h>
  33. #include <asm/arch/regs-rtc.h>
  34. /* need this for the RTC_AF definitions */
  35. #include <linux/mc146818rtc.h>
  36. #undef S3C24XX_VA_RTC
  37. #define S3C24XX_VA_RTC s3c2410_rtc_base
  38. static struct resource *s3c2410_rtc_mem;
  39. static void __iomem *s3c2410_rtc_base;
  40. static int s3c2410_rtc_alarmno = NO_IRQ;
  41. static int s3c2410_rtc_tickno = NO_IRQ;
  42. static int s3c2410_rtc_freq = 1;
  43. static DEFINE_SPINLOCK(s3c2410_rtc_pie_lock);
  44. /* IRQ Handlers */
  45. static irqreturn_t s3c2410_rtc_alarmirq(int irq, void *id, struct pt_regs *r)
  46. {
  47. rtc_update(1, RTC_AF | RTC_IRQF);
  48. return IRQ_HANDLED;
  49. }
  50. static irqreturn_t s3c2410_rtc_tickirq(int irq, void *id, struct pt_regs *r)
  51. {
  52. rtc_update(1, RTC_PF | RTC_IRQF);
  53. return IRQ_HANDLED;
  54. }
  55. /* Update control registers */
  56. static void s3c2410_rtc_setaie(int to)
  57. {
  58. unsigned int tmp;
  59. pr_debug("%s: aie=%d\n", __FUNCTION__, to);
  60. tmp = readb(S3C2410_RTCALM);
  61. if (to)
  62. tmp |= S3C2410_RTCALM_ALMEN;
  63. else
  64. tmp &= ~S3C2410_RTCALM_ALMEN;
  65. writeb(tmp, S3C2410_RTCALM);
  66. }
  67. static void s3c2410_rtc_setpie(int to)
  68. {
  69. unsigned int tmp;
  70. pr_debug("%s: pie=%d\n", __FUNCTION__, to);
  71. spin_lock_irq(&s3c2410_rtc_pie_lock);
  72. tmp = readb(S3C2410_TICNT) & ~S3C2410_TICNT_ENABLE;
  73. if (to)
  74. tmp |= S3C2410_TICNT_ENABLE;
  75. writeb(tmp, S3C2410_TICNT);
  76. spin_unlock_irq(&s3c2410_rtc_pie_lock);
  77. }
  78. static void s3c2410_rtc_setfreq(int freq)
  79. {
  80. unsigned int tmp;
  81. spin_lock_irq(&s3c2410_rtc_pie_lock);
  82. tmp = readb(S3C2410_TICNT) & S3C2410_TICNT_ENABLE;
  83. s3c2410_rtc_freq = freq;
  84. tmp |= (128 / freq)-1;
  85. writeb(tmp, S3C2410_TICNT);
  86. spin_unlock_irq(&s3c2410_rtc_pie_lock);
  87. }
  88. /* Time read/write */
  89. static int s3c2410_rtc_gettime(struct rtc_time *rtc_tm)
  90. {
  91. unsigned int have_retried = 0;
  92. retry_get_time:
  93. rtc_tm->tm_min = readb(S3C2410_RTCMIN);
  94. rtc_tm->tm_hour = readb(S3C2410_RTCHOUR);
  95. rtc_tm->tm_mday = readb(S3C2410_RTCDATE);
  96. rtc_tm->tm_mon = readb(S3C2410_RTCMON);
  97. rtc_tm->tm_year = readb(S3C2410_RTCYEAR);
  98. rtc_tm->tm_sec = readb(S3C2410_RTCSEC);
  99. /* the only way to work out wether the system was mid-update
  100. * when we read it is to check the second counter, and if it
  101. * is zero, then we re-try the entire read
  102. */
  103. if (rtc_tm->tm_sec == 0 && !have_retried) {
  104. have_retried = 1;
  105. goto retry_get_time;
  106. }
  107. pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n",
  108. rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
  109. rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
  110. BCD_TO_BIN(rtc_tm->tm_sec);
  111. BCD_TO_BIN(rtc_tm->tm_min);
  112. BCD_TO_BIN(rtc_tm->tm_hour);
  113. BCD_TO_BIN(rtc_tm->tm_mday);
  114. BCD_TO_BIN(rtc_tm->tm_mon);
  115. BCD_TO_BIN(rtc_tm->tm_year);
  116. rtc_tm->tm_year += 100;
  117. rtc_tm->tm_mon -= 1;
  118. return 0;
  119. }
  120. static int s3c2410_rtc_settime(struct rtc_time *tm)
  121. {
  122. /* the rtc gets round the y2k problem by just not supporting it */
  123. if (tm->tm_year < 100)
  124. return -EINVAL;
  125. writeb(BIN2BCD(tm->tm_sec), S3C2410_RTCSEC);
  126. writeb(BIN2BCD(tm->tm_min), S3C2410_RTCMIN);
  127. writeb(BIN2BCD(tm->tm_hour), S3C2410_RTCHOUR);
  128. writeb(BIN2BCD(tm->tm_mday), S3C2410_RTCDATE);
  129. writeb(BIN2BCD(tm->tm_mon + 1), S3C2410_RTCMON);
  130. writeb(BIN2BCD(tm->tm_year - 100), S3C2410_RTCYEAR);
  131. return 0;
  132. }
  133. static int s3c2410_rtc_getalarm(struct rtc_wkalrm *alrm)
  134. {
  135. struct rtc_time *alm_tm = &alrm->time;
  136. unsigned int alm_en;
  137. alm_tm->tm_sec = readb(S3C2410_ALMSEC);
  138. alm_tm->tm_min = readb(S3C2410_ALMMIN);
  139. alm_tm->tm_hour = readb(S3C2410_ALMHOUR);
  140. alm_tm->tm_mon = readb(S3C2410_ALMMON);
  141. alm_tm->tm_mday = readb(S3C2410_ALMDATE);
  142. alm_tm->tm_year = readb(S3C2410_ALMYEAR);
  143. alm_en = readb(S3C2410_RTCALM);
  144. pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
  145. alm_en,
  146. alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
  147. alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
  148. /* decode the alarm enable field */
  149. if (alm_en & S3C2410_RTCALM_SECEN) {
  150. BCD_TO_BIN(alm_tm->tm_sec);
  151. } else {
  152. alm_tm->tm_sec = 0xff;
  153. }
  154. if (alm_en & S3C2410_RTCALM_MINEN) {
  155. BCD_TO_BIN(alm_tm->tm_min);
  156. } else {
  157. alm_tm->tm_min = 0xff;
  158. }
  159. if (alm_en & S3C2410_RTCALM_HOUREN) {
  160. BCD_TO_BIN(alm_tm->tm_hour);
  161. } else {
  162. alm_tm->tm_hour = 0xff;
  163. }
  164. if (alm_en & S3C2410_RTCALM_DAYEN) {
  165. BCD_TO_BIN(alm_tm->tm_mday);
  166. } else {
  167. alm_tm->tm_mday = 0xff;
  168. }
  169. if (alm_en & S3C2410_RTCALM_MONEN) {
  170. BCD_TO_BIN(alm_tm->tm_mon);
  171. alm_tm->tm_mon -= 1;
  172. } else {
  173. alm_tm->tm_mon = 0xff;
  174. }
  175. if (alm_en & S3C2410_RTCALM_YEAREN) {
  176. BCD_TO_BIN(alm_tm->tm_year);
  177. } else {
  178. alm_tm->tm_year = 0xffff;
  179. }
  180. /* todo - set alrm->enabled ? */
  181. return 0;
  182. }
  183. static int s3c2410_rtc_setalarm(struct rtc_wkalrm *alrm)
  184. {
  185. struct rtc_time *tm = &alrm->time;
  186. unsigned int alrm_en;
  187. pr_debug("s3c2410_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. if (alrm->enabled || 1) {
  192. alrm_en = readb(S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
  193. writeb(0x00, S3C2410_RTCALM);
  194. if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
  195. alrm_en |= S3C2410_RTCALM_SECEN;
  196. writeb(BIN2BCD(tm->tm_sec), S3C2410_ALMSEC);
  197. }
  198. if (tm->tm_min < 60 && tm->tm_min >= 0) {
  199. alrm_en |= S3C2410_RTCALM_MINEN;
  200. writeb(BIN2BCD(tm->tm_min), S3C2410_ALMMIN);
  201. }
  202. if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
  203. alrm_en |= S3C2410_RTCALM_HOUREN;
  204. writeb(BIN2BCD(tm->tm_hour), S3C2410_ALMHOUR);
  205. }
  206. pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
  207. writeb(alrm_en, S3C2410_RTCALM);
  208. enable_irq_wake(s3c2410_rtc_alarmno);
  209. } else {
  210. alrm_en = readb(S3C2410_RTCALM);
  211. alrm_en &= ~S3C2410_RTCALM_ALMEN;
  212. writeb(alrm_en, S3C2410_RTCALM);
  213. disable_irq_wake(s3c2410_rtc_alarmno);
  214. }
  215. return 0;
  216. }
  217. static int s3c2410_rtc_ioctl(unsigned int cmd, unsigned long arg)
  218. {
  219. switch (cmd) {
  220. case RTC_AIE_OFF:
  221. case RTC_AIE_ON:
  222. s3c2410_rtc_setaie((cmd == RTC_AIE_ON) ? 1 : 0);
  223. return 0;
  224. case RTC_PIE_OFF:
  225. case RTC_PIE_ON:
  226. s3c2410_rtc_setpie((cmd == RTC_PIE_ON) ? 1 : 0);
  227. return 0;
  228. case RTC_IRQP_READ:
  229. return put_user(s3c2410_rtc_freq, (unsigned long __user *)arg);
  230. case RTC_IRQP_SET:
  231. if (arg < 1 || arg > 64)
  232. return -EINVAL;
  233. if (!capable(CAP_SYS_RESOURCE))
  234. return -EACCES;
  235. /* check for power of 2 */
  236. if ((arg & (arg-1)) != 0)
  237. return -EINVAL;
  238. pr_debug("s3c2410_rtc: setting frequency %ld\n", arg);
  239. s3c2410_rtc_setfreq(arg);
  240. return 0;
  241. case RTC_UIE_ON:
  242. case RTC_UIE_OFF:
  243. return -EINVAL;
  244. }
  245. return -EINVAL;
  246. }
  247. static int s3c2410_rtc_proc(char *buf)
  248. {
  249. unsigned int rtcalm = readb(S3C2410_RTCALM);
  250. unsigned int ticnt = readb (S3C2410_TICNT);
  251. char *p = buf;
  252. p += sprintf(p, "alarm_IRQ\t: %s\n",
  253. (rtcalm & S3C2410_RTCALM_ALMEN) ? "yes" : "no" );
  254. p += sprintf(p, "periodic_IRQ\t: %s\n",
  255. (ticnt & S3C2410_TICNT_ENABLE) ? "yes" : "no" );
  256. p += sprintf(p, "periodic_freq\t: %d\n", s3c2410_rtc_freq);
  257. return p - buf;
  258. }
  259. static int s3c2410_rtc_open(void)
  260. {
  261. int ret;
  262. ret = request_irq(s3c2410_rtc_alarmno, s3c2410_rtc_alarmirq,
  263. SA_INTERRUPT, "s3c2410-rtc alarm", NULL);
  264. if (ret)
  265. printk(KERN_ERR "IRQ%d already in use\n", s3c2410_rtc_alarmno);
  266. ret = request_irq(s3c2410_rtc_tickno, s3c2410_rtc_tickirq,
  267. SA_INTERRUPT, "s3c2410-rtc tick", NULL);
  268. if (ret) {
  269. printk(KERN_ERR "IRQ%d already in use\n", s3c2410_rtc_tickno);
  270. goto tick_err;
  271. }
  272. return ret;
  273. tick_err:
  274. free_irq(s3c2410_rtc_alarmno, NULL);
  275. return ret;
  276. }
  277. static void s3c2410_rtc_release(void)
  278. {
  279. /* do not clear AIE here, it may be needed for wake */
  280. s3c2410_rtc_setpie(0);
  281. free_irq(s3c2410_rtc_alarmno, NULL);
  282. free_irq(s3c2410_rtc_tickno, NULL);
  283. }
  284. static struct rtc_ops s3c2410_rtcops = {
  285. .owner = THIS_MODULE,
  286. .open = s3c2410_rtc_open,
  287. .release = s3c2410_rtc_release,
  288. .ioctl = s3c2410_rtc_ioctl,
  289. .read_time = s3c2410_rtc_gettime,
  290. .set_time = s3c2410_rtc_settime,
  291. .read_alarm = s3c2410_rtc_getalarm,
  292. .set_alarm = s3c2410_rtc_setalarm,
  293. .proc = s3c2410_rtc_proc,
  294. };
  295. static void s3c2410_rtc_enable(struct platform_device *pdev, int en)
  296. {
  297. unsigned int tmp;
  298. if (s3c2410_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 S3C2410_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 S3C2410_RTCCON_CLKRST\n");
  319. tmp = readb(S3C2410_RTCCON);
  320. writeb(tmp & ~S3C2410_RTCCON_CLKRST, S3C2410_RTCCON);
  321. }
  322. }
  323. }
  324. static int s3c2410_rtc_remove(struct platform_device *dev)
  325. {
  326. unregister_rtc(&s3c2410_rtcops);
  327. s3c2410_rtc_setpie(0);
  328. s3c2410_rtc_setaie(0);
  329. if (s3c2410_rtc_mem != NULL) {
  330. pr_debug("s3c2410_rtc: releasing s3c2410_rtc_mem\n");
  331. iounmap(s3c2410_rtc_base);
  332. release_resource(s3c2410_rtc_mem);
  333. kfree(s3c2410_rtc_mem);
  334. }
  335. return 0;
  336. }
  337. static int s3c2410_rtc_probe(struct platform_device *pdev)
  338. {
  339. struct resource *res;
  340. int ret;
  341. pr_debug("%s: probe=%p\n", __FUNCTION__, pdev);
  342. /* find the IRQs */
  343. s3c2410_rtc_tickno = platform_get_irq(pdev, 1);
  344. if (s3c2410_rtc_tickno < 0) {
  345. dev_err(&pdev->dev, "no irq for rtc tick\n");
  346. return -ENOENT;
  347. }
  348. s3c2410_rtc_alarmno = platform_get_irq(pdev, 0);
  349. if (s3c2410_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. s3c2410_rtc_tickno, s3c2410_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. s3c2410_rtc_mem = request_mem_region(res->start, res->end-res->start+1,
  362. pdev->name);
  363. if (s3c2410_rtc_mem == NULL) {
  364. dev_err(&pdev->dev, "failed to reserve memory region\n");
  365. ret = -ENOENT;
  366. goto exit_err;
  367. }
  368. s3c2410_rtc_base = ioremap(res->start, res->end - res->start + 1);
  369. if (s3c2410_rtc_base == NULL) {
  370. dev_err(&pdev->dev, "failed ioremap()\n");
  371. ret = -EINVAL;
  372. goto exit_err;
  373. }
  374. s3c2410_rtc_mem = res;
  375. pr_debug("s3c2410_rtc_base=%p\n", s3c2410_rtc_base);
  376. pr_debug("s3c2410_rtc: RTCCON=%02x\n", readb(S3C2410_RTCCON));
  377. /* check to see if everything is setup correctly */
  378. s3c2410_rtc_enable(pdev, 1);
  379. pr_debug("s3c2410_rtc: RTCCON=%02x\n", readb(S3C2410_RTCCON));
  380. s3c2410_rtc_setfreq(s3c2410_rtc_freq);
  381. /* register RTC and exit */
  382. register_rtc(&s3c2410_rtcops);
  383. return 0;
  384. exit_err:
  385. dev_err(&pdev->dev, "error %d during initialisation\n", ret);
  386. return ret;
  387. }
  388. #ifdef CONFIG_PM
  389. /* S3C2410 RTC Power management control */
  390. static struct timespec s3c2410_rtc_delta;
  391. static int ticnt_save;
  392. static int s3c2410_rtc_suspend(struct platform_device *pdev, pm_message_t state)
  393. {
  394. struct rtc_time tm;
  395. struct timespec time;
  396. time.tv_nsec = 0;
  397. /* save TICNT for anyone using periodic interrupts */
  398. ticnt_save = readb(S3C2410_TICNT);
  399. /* calculate time delta for suspend */
  400. s3c2410_rtc_gettime(&tm);
  401. rtc_tm_to_time(&tm, &time.tv_sec);
  402. save_time_delta(&s3c2410_rtc_delta, &time);
  403. s3c2410_rtc_enable(pdev, 0);
  404. return 0;
  405. }
  406. static int s3c2410_rtc_resume(struct platform_device *pdev)
  407. {
  408. struct rtc_time tm;
  409. struct timespec time;
  410. time.tv_nsec = 0;
  411. s3c2410_rtc_enable(pdev, 1);
  412. s3c2410_rtc_gettime(&tm);
  413. rtc_tm_to_time(&tm, &time.tv_sec);
  414. restore_time_delta(&s3c2410_rtc_delta, &time);
  415. writeb(ticnt_save, S3C2410_TICNT);
  416. return 0;
  417. }
  418. #else
  419. #define s3c2410_rtc_suspend NULL
  420. #define s3c2410_rtc_resume NULL
  421. #endif
  422. static struct platform_driver s3c2410_rtcdrv = {
  423. .probe = s3c2410_rtc_probe,
  424. .remove = s3c2410_rtc_remove,
  425. .suspend = s3c2410_rtc_suspend,
  426. .resume = s3c2410_rtc_resume,
  427. .driver = {
  428. .name = "s3c2410-rtc",
  429. .owner = THIS_MODULE,
  430. },
  431. };
  432. static char __initdata banner[] = "S3C2410 RTC, (c) 2004 Simtec Electronics\n";
  433. static int __init s3c2410_rtc_init(void)
  434. {
  435. printk(banner);
  436. return platform_driver_register(&s3c2410_rtcdrv);
  437. }
  438. static void __exit s3c2410_rtc_exit(void)
  439. {
  440. platform_driver_unregister(&s3c2410_rtcdrv);
  441. }
  442. module_init(s3c2410_rtc_init);
  443. module_exit(s3c2410_rtc_exit);
  444. MODULE_DESCRIPTION("S3C24XX RTC Driver");
  445. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  446. MODULE_LICENSE("GPL");