s3c2410-rtc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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/device.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/rtc.h>
  25. #include <linux/bcd.h>
  26. #include <asm/hardware.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/io.h>
  29. #include <asm/irq.h>
  30. #include <asm/rtc.h>
  31. #include <asm/mach/time.h>
  32. #include <asm/hardware/clock.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 void 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. }
  119. static int s3c2410_rtc_settime(struct rtc_time *tm)
  120. {
  121. /* the rtc gets round the y2k problem by just not supporting it */
  122. if (tm->tm_year < 100)
  123. return -EINVAL;
  124. writeb(BIN2BCD(tm->tm_sec), S3C2410_RTCSEC);
  125. writeb(BIN2BCD(tm->tm_min), S3C2410_RTCMIN);
  126. writeb(BIN2BCD(tm->tm_hour), S3C2410_RTCHOUR);
  127. writeb(BIN2BCD(tm->tm_mday), S3C2410_RTCDATE);
  128. writeb(BIN2BCD(tm->tm_mon + 1), S3C2410_RTCMON);
  129. writeb(BIN2BCD(tm->tm_year - 100), S3C2410_RTCYEAR);
  130. return 0;
  131. }
  132. static void s3c2410_rtc_getalarm(struct rtc_wkalrm *alrm)
  133. {
  134. struct rtc_time *alm_tm = &alrm->time;
  135. unsigned int alm_en;
  136. alm_tm->tm_sec = readb(S3C2410_ALMSEC);
  137. alm_tm->tm_min = readb(S3C2410_ALMMIN);
  138. alm_tm->tm_hour = readb(S3C2410_ALMHOUR);
  139. alm_tm->tm_mon = readb(S3C2410_ALMMON);
  140. alm_tm->tm_mday = readb(S3C2410_ALMDATE);
  141. alm_tm->tm_year = readb(S3C2410_ALMYEAR);
  142. alm_en = readb(S3C2410_RTCALM);
  143. pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
  144. alm_en,
  145. alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
  146. alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
  147. /* decode the alarm enable field */
  148. if (alm_en & S3C2410_RTCALM_SECEN) {
  149. BCD_TO_BIN(alm_tm->tm_sec);
  150. } else {
  151. alm_tm->tm_sec = 0xff;
  152. }
  153. if (alm_en & S3C2410_RTCALM_MINEN) {
  154. BCD_TO_BIN(alm_tm->tm_min);
  155. } else {
  156. alm_tm->tm_min = 0xff;
  157. }
  158. if (alm_en & S3C2410_RTCALM_HOUREN) {
  159. BCD_TO_BIN(alm_tm->tm_hour);
  160. } else {
  161. alm_tm->tm_hour = 0xff;
  162. }
  163. if (alm_en & S3C2410_RTCALM_DAYEN) {
  164. BCD_TO_BIN(alm_tm->tm_mday);
  165. } else {
  166. alm_tm->tm_mday = 0xff;
  167. }
  168. if (alm_en & S3C2410_RTCALM_MONEN) {
  169. BCD_TO_BIN(alm_tm->tm_mon);
  170. alm_tm->tm_mon -= 1;
  171. } else {
  172. alm_tm->tm_mon = 0xff;
  173. }
  174. if (alm_en & S3C2410_RTCALM_YEAREN) {
  175. BCD_TO_BIN(alm_tm->tm_year);
  176. } else {
  177. alm_tm->tm_year = 0xffff;
  178. }
  179. /* todo - set alrm->enabled ? */
  180. }
  181. static int s3c2410_rtc_setalarm(struct rtc_wkalrm *alrm)
  182. {
  183. struct rtc_time *tm = &alrm->time;
  184. unsigned int alrm_en;
  185. pr_debug("s3c2410_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
  186. alrm->enabled,
  187. tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
  188. tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
  189. if (alrm->enabled || 1) {
  190. alrm_en = readb(S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
  191. writeb(0x00, S3C2410_RTCALM);
  192. if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
  193. alrm_en |= S3C2410_RTCALM_SECEN;
  194. writeb(BIN2BCD(tm->tm_sec), S3C2410_ALMSEC);
  195. }
  196. if (tm->tm_min < 60 && tm->tm_min >= 0) {
  197. alrm_en |= S3C2410_RTCALM_MINEN;
  198. writeb(BIN2BCD(tm->tm_min), S3C2410_ALMMIN);
  199. }
  200. if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
  201. alrm_en |= S3C2410_RTCALM_HOUREN;
  202. writeb(BIN2BCD(tm->tm_hour), S3C2410_ALMHOUR);
  203. }
  204. pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
  205. writeb(alrm_en, S3C2410_RTCALM);
  206. enable_irq_wake(s3c2410_rtc_alarmno);
  207. } else {
  208. alrm_en = readb(S3C2410_RTCALM);
  209. alrm_en &= ~S3C2410_RTCALM_ALMEN;
  210. writeb(alrm_en, S3C2410_RTCALM);
  211. disable_irq_wake(s3c2410_rtc_alarmno);
  212. }
  213. return 0;
  214. }
  215. static int s3c2410_rtc_ioctl(unsigned int cmd, unsigned long arg)
  216. {
  217. switch (cmd) {
  218. case RTC_AIE_OFF:
  219. case RTC_AIE_ON:
  220. s3c2410_rtc_setaie((cmd == RTC_AIE_ON) ? 1 : 0);
  221. return 0;
  222. case RTC_PIE_OFF:
  223. case RTC_PIE_ON:
  224. s3c2410_rtc_setpie((cmd == RTC_PIE_ON) ? 1 : 0);
  225. return 0;
  226. case RTC_IRQP_READ:
  227. return put_user(s3c2410_rtc_freq, (unsigned long __user *)arg);
  228. case RTC_IRQP_SET:
  229. if (arg < 1 || arg > 64)
  230. return -EINVAL;
  231. if (!capable(CAP_SYS_RESOURCE))
  232. return -EACCES;
  233. /* check for power of 2 */
  234. if ((arg & (arg-1)) != 0)
  235. return -EINVAL;
  236. pr_debug("s3c2410_rtc: setting frequency %ld\n", arg);
  237. s3c2410_rtc_setfreq(arg);
  238. return 0;
  239. case RTC_UIE_ON:
  240. case RTC_UIE_OFF:
  241. return -EINVAL;
  242. }
  243. return -EINVAL;
  244. }
  245. static int s3c2410_rtc_proc(char *buf)
  246. {
  247. unsigned int rtcalm = readb(S3C2410_RTCALM);
  248. unsigned int ticnt = readb (S3C2410_TICNT);
  249. char *p = buf;
  250. p += sprintf(p, "alarm_IRQ\t: %s\n",
  251. (rtcalm & S3C2410_RTCALM_ALMEN) ? "yes" : "no" );
  252. p += sprintf(p, "periodic_IRQ\t: %s\n",
  253. (ticnt & S3C2410_TICNT_ENABLE) ? "yes" : "no" );
  254. p += sprintf(p, "periodic_freq\t: %d\n", s3c2410_rtc_freq);
  255. return p - buf;
  256. }
  257. static int s3c2410_rtc_open(void)
  258. {
  259. int ret;
  260. ret = request_irq(s3c2410_rtc_alarmno, s3c2410_rtc_alarmirq,
  261. SA_INTERRUPT, "s3c2410-rtc alarm", NULL);
  262. if (ret)
  263. printk(KERN_ERR "IRQ%d already in use\n", s3c2410_rtc_alarmno);
  264. ret = request_irq(s3c2410_rtc_tickno, s3c2410_rtc_tickirq,
  265. SA_INTERRUPT, "s3c2410-rtc tick", NULL);
  266. if (ret) {
  267. printk(KERN_ERR "IRQ%d already in use\n", s3c2410_rtc_tickno);
  268. goto tick_err;
  269. }
  270. return ret;
  271. tick_err:
  272. free_irq(s3c2410_rtc_alarmno, NULL);
  273. return ret;
  274. }
  275. static void s3c2410_rtc_release(void)
  276. {
  277. /* do not clear AIE here, it may be needed for wake */
  278. s3c2410_rtc_setpie(0);
  279. free_irq(s3c2410_rtc_alarmno, NULL);
  280. free_irq(s3c2410_rtc_tickno, NULL);
  281. }
  282. static struct rtc_ops s3c2410_rtcops = {
  283. .owner = THIS_MODULE,
  284. .open = s3c2410_rtc_open,
  285. .release = s3c2410_rtc_release,
  286. .ioctl = s3c2410_rtc_ioctl,
  287. .read_time = s3c2410_rtc_gettime,
  288. .set_time = s3c2410_rtc_settime,
  289. .read_alarm = s3c2410_rtc_getalarm,
  290. .set_alarm = s3c2410_rtc_setalarm,
  291. .proc = s3c2410_rtc_proc,
  292. };
  293. static void s3c2410_rtc_enable(struct device *dev, int en)
  294. {
  295. unsigned int tmp;
  296. if (s3c2410_rtc_base == NULL)
  297. return;
  298. if (!en) {
  299. tmp = readb(S3C2410_RTCCON);
  300. writeb(tmp & ~S3C2410_RTCCON_RTCEN, S3C2410_RTCCON);
  301. tmp = readb(S3C2410_TICNT);
  302. writeb(tmp & ~S3C2410_TICNT_ENABLE, S3C2410_TICNT);
  303. } else {
  304. /* re-enable the device, and check it is ok */
  305. if ((readb(S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0){
  306. dev_info(dev, "rtc disabled, re-enabling\n");
  307. tmp = readb(S3C2410_RTCCON);
  308. writeb(tmp | S3C2410_RTCCON_RTCEN , S3C2410_RTCCON);
  309. }
  310. if ((readb(S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)){
  311. dev_info(dev, "removing S3C2410_RTCCON_CNTSEL\n");
  312. tmp = readb(S3C2410_RTCCON);
  313. writeb(tmp& ~S3C2410_RTCCON_CNTSEL , S3C2410_RTCCON);
  314. }
  315. if ((readb(S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)){
  316. dev_info(dev, "removing S3C2410_RTCCON_CLKRST\n");
  317. tmp = readb(S3C2410_RTCCON);
  318. writeb(tmp & ~S3C2410_RTCCON_CLKRST, S3C2410_RTCCON);
  319. }
  320. }
  321. }
  322. static int s3c2410_rtc_remove(struct device *dev)
  323. {
  324. unregister_rtc(&s3c2410_rtcops);
  325. s3c2410_rtc_setpie(0);
  326. s3c2410_rtc_setaie(0);
  327. if (s3c2410_rtc_mem != NULL) {
  328. pr_debug("s3c2410_rtc: releasing s3c2410_rtc_mem\n");
  329. iounmap(s3c2410_rtc_base);
  330. release_resource(s3c2410_rtc_mem);
  331. kfree(s3c2410_rtc_mem);
  332. }
  333. return 0;
  334. }
  335. static int s3c2410_rtc_probe(struct device *dev)
  336. {
  337. struct platform_device *pdev = to_platform_device(dev);
  338. struct resource *res;
  339. int ret;
  340. pr_debug("%s: probe=%p, device=%p\n", __FUNCTION__, pdev, dev);
  341. /* find the IRQs */
  342. s3c2410_rtc_tickno = platform_get_irq(pdev, 1);
  343. if (s3c2410_rtc_tickno <= 0) {
  344. dev_err(dev, "no irq for rtc tick\n");
  345. return -ENOENT;
  346. }
  347. s3c2410_rtc_alarmno = platform_get_irq(pdev, 0);
  348. if (s3c2410_rtc_alarmno <= 0) {
  349. dev_err(dev, "no irq for alarm\n");
  350. return -ENOENT;
  351. }
  352. pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
  353. s3c2410_rtc_tickno, s3c2410_rtc_alarmno);
  354. /* get the memory region */
  355. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  356. if (res == NULL) {
  357. dev_err(dev, "failed to get memory region resource\n");
  358. return -ENOENT;
  359. }
  360. s3c2410_rtc_mem = request_mem_region(res->start, res->end-res->start+1,
  361. pdev->name);
  362. if (s3c2410_rtc_mem == NULL) {
  363. dev_err(dev, "failed to reserve memory region\n");
  364. ret = -ENOENT;
  365. goto exit_err;
  366. }
  367. s3c2410_rtc_base = ioremap(res->start, res->end - res->start + 1);
  368. if (s3c2410_rtc_base == NULL) {
  369. dev_err(dev, "failed ioremap()\n");
  370. ret = -EINVAL;
  371. goto exit_err;
  372. }
  373. s3c2410_rtc_mem = res;
  374. pr_debug("s3c2410_rtc_base=%p\n", s3c2410_rtc_base);
  375. pr_debug("s3c2410_rtc: RTCCON=%02x\n", readb(S3C2410_RTCCON));
  376. /* check to see if everything is setup correctly */
  377. s3c2410_rtc_enable(dev, 1);
  378. pr_debug("s3c2410_rtc: RTCCON=%02x\n", readb(S3C2410_RTCCON));
  379. s3c2410_rtc_setfreq(s3c2410_rtc_freq);
  380. /* register RTC and exit */
  381. register_rtc(&s3c2410_rtcops);
  382. return 0;
  383. exit_err:
  384. dev_err(dev, "error %d during initialisation\n", ret);
  385. return ret;
  386. }
  387. #ifdef CONFIG_PM
  388. /* S3C2410 RTC Power management control */
  389. static struct timespec s3c2410_rtc_delta;
  390. static int ticnt_save;
  391. static int s3c2410_rtc_suspend(struct device *dev, pm_message_t state, u32 level)
  392. {
  393. struct rtc_time tm;
  394. struct timespec time;
  395. time.tv_nsec = 0;
  396. if (level == SUSPEND_POWER_DOWN) {
  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(dev, 0);
  404. }
  405. return 0;
  406. }
  407. static int s3c2410_rtc_resume(struct device *dev, u32 level)
  408. {
  409. struct rtc_time tm;
  410. struct timespec time;
  411. time.tv_nsec = 0;
  412. s3c2410_rtc_enable(dev, 1);
  413. s3c2410_rtc_gettime(&tm);
  414. rtc_tm_to_time(&tm, &time.tv_sec);
  415. restore_time_delta(&s3c2410_rtc_delta, &time);
  416. writeb(ticnt_save, S3C2410_TICNT);
  417. return 0;
  418. }
  419. #else
  420. #define s3c2410_rtc_suspend NULL
  421. #define s3c2410_rtc_resume NULL
  422. #endif
  423. static struct device_driver s3c2410_rtcdrv = {
  424. .name = "s3c2410-rtc",
  425. .bus = &platform_bus_type,
  426. .probe = s3c2410_rtc_probe,
  427. .remove = s3c2410_rtc_remove,
  428. .suspend = s3c2410_rtc_suspend,
  429. .resume = s3c2410_rtc_resume,
  430. };
  431. static char __initdata banner[] = "S3C2410 RTC, (c) 2004 Simtec Electronics\n";
  432. static int __init s3c2410_rtc_init(void)
  433. {
  434. printk(banner);
  435. return driver_register(&s3c2410_rtcdrv);
  436. }
  437. static void __exit s3c2410_rtc_exit(void)
  438. {
  439. driver_unregister(&s3c2410_rtcdrv);
  440. }
  441. module_init(s3c2410_rtc_init);
  442. module_exit(s3c2410_rtc_exit);
  443. MODULE_DESCRIPTION("S3C24XX RTC Driver");
  444. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  445. MODULE_LICENSE("GPL");