rtc-spear.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * drivers/rtc/rtc-spear.c
  3. *
  4. * Copyright (C) 2010 ST Microelectronics
  5. * Rajeev Kumar<rajeev-dlh.kumar@st.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/bcd.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/irq.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/rtc.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. /* RTC registers */
  23. #define TIME_REG 0x00
  24. #define DATE_REG 0x04
  25. #define ALARM_TIME_REG 0x08
  26. #define ALARM_DATE_REG 0x0C
  27. #define CTRL_REG 0x10
  28. #define STATUS_REG 0x14
  29. /* TIME_REG & ALARM_TIME_REG */
  30. #define SECONDS_UNITS (0xf<<0) /* seconds units position */
  31. #define SECONDS_TENS (0x7<<4) /* seconds tens position */
  32. #define MINUTES_UNITS (0xf<<8) /* minutes units position */
  33. #define MINUTES_TENS (0x7<<12) /* minutes tens position */
  34. #define HOURS_UNITS (0xf<<16) /* hours units position */
  35. #define HOURS_TENS (0x3<<20) /* hours tens position */
  36. /* DATE_REG & ALARM_DATE_REG */
  37. #define DAYS_UNITS (0xf<<0) /* days units position */
  38. #define DAYS_TENS (0x3<<4) /* days tens position */
  39. #define MONTHS_UNITS (0xf<<8) /* months units position */
  40. #define MONTHS_TENS (0x1<<12) /* months tens position */
  41. #define YEARS_UNITS (0xf<<16) /* years units position */
  42. #define YEARS_TENS (0xf<<20) /* years tens position */
  43. #define YEARS_HUNDREDS (0xf<<24) /* years hundereds position */
  44. #define YEARS_MILLENIUMS (0xf<<28) /* years millenium position */
  45. /* MASK SHIFT TIME_REG & ALARM_TIME_REG*/
  46. #define SECOND_SHIFT 0x00 /* seconds units */
  47. #define MINUTE_SHIFT 0x08 /* minutes units position */
  48. #define HOUR_SHIFT 0x10 /* hours units position */
  49. #define MDAY_SHIFT 0x00 /* Month day shift */
  50. #define MONTH_SHIFT 0x08 /* Month shift */
  51. #define YEAR_SHIFT 0x10 /* Year shift */
  52. #define SECOND_MASK 0x7F
  53. #define MIN_MASK 0x7F
  54. #define HOUR_MASK 0x3F
  55. #define DAY_MASK 0x3F
  56. #define MONTH_MASK 0x7F
  57. #define YEAR_MASK 0xFFFF
  58. /* date reg equal to time reg, for debug only */
  59. #define TIME_BYP (1<<9)
  60. #define INT_ENABLE (1<<31) /* interrupt enable */
  61. /* STATUS_REG */
  62. #define CLK_UNCONNECTED (1<<0)
  63. #define PEND_WR_TIME (1<<2)
  64. #define PEND_WR_DATE (1<<3)
  65. #define LOST_WR_TIME (1<<4)
  66. #define LOST_WR_DATE (1<<5)
  67. #define RTC_INT_MASK (1<<31)
  68. #define STATUS_BUSY (PEND_WR_TIME | PEND_WR_DATE)
  69. #define STATUS_FAIL (LOST_WR_TIME | LOST_WR_DATE)
  70. struct spear_rtc_config {
  71. struct clk *clk;
  72. spinlock_t lock;
  73. void __iomem *ioaddr;
  74. };
  75. static inline void spear_rtc_clear_interrupt(struct spear_rtc_config *config)
  76. {
  77. unsigned int val;
  78. unsigned long flags;
  79. spin_lock_irqsave(&config->lock, flags);
  80. val = readl(config->ioaddr + STATUS_REG);
  81. val |= RTC_INT_MASK;
  82. writel(val, config->ioaddr + STATUS_REG);
  83. spin_unlock_irqrestore(&config->lock, flags);
  84. }
  85. static inline void spear_rtc_enable_interrupt(struct spear_rtc_config *config)
  86. {
  87. unsigned int val;
  88. val = readl(config->ioaddr + CTRL_REG);
  89. if (!(val & INT_ENABLE)) {
  90. spear_rtc_clear_interrupt(config);
  91. val |= INT_ENABLE;
  92. writel(val, config->ioaddr + CTRL_REG);
  93. }
  94. }
  95. static inline void spear_rtc_disable_interrupt(struct spear_rtc_config *config)
  96. {
  97. unsigned int val;
  98. val = readl(config->ioaddr + CTRL_REG);
  99. if (val & INT_ENABLE) {
  100. val &= ~INT_ENABLE;
  101. writel(val, config->ioaddr + CTRL_REG);
  102. }
  103. }
  104. static inline int is_write_complete(struct spear_rtc_config *config)
  105. {
  106. int ret = 0;
  107. unsigned long flags;
  108. spin_lock_irqsave(&config->lock, flags);
  109. if ((readl(config->ioaddr + STATUS_REG)) & STATUS_FAIL)
  110. ret = -EIO;
  111. spin_unlock_irqrestore(&config->lock, flags);
  112. return ret;
  113. }
  114. static void rtc_wait_not_busy(struct spear_rtc_config *config)
  115. {
  116. int status, count = 0;
  117. unsigned long flags;
  118. /* Assuming BUSY may stay active for 80 msec) */
  119. for (count = 0; count < 80; count++) {
  120. spin_lock_irqsave(&config->lock, flags);
  121. status = readl(config->ioaddr + STATUS_REG);
  122. spin_unlock_irqrestore(&config->lock, flags);
  123. if ((status & STATUS_BUSY) == 0)
  124. break;
  125. /* check status busy, after each msec */
  126. msleep(1);
  127. }
  128. }
  129. static irqreturn_t spear_rtc_irq(int irq, void *dev_id)
  130. {
  131. struct rtc_device *rtc = (struct rtc_device *)dev_id;
  132. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  133. unsigned long flags, events = 0;
  134. unsigned int irq_data;
  135. spin_lock_irqsave(&config->lock, flags);
  136. irq_data = readl(config->ioaddr + STATUS_REG);
  137. spin_unlock_irqrestore(&config->lock, flags);
  138. if ((irq_data & RTC_INT_MASK)) {
  139. spear_rtc_clear_interrupt(config);
  140. events = RTC_IRQF | RTC_AF;
  141. rtc_update_irq(rtc, 1, events);
  142. return IRQ_HANDLED;
  143. } else
  144. return IRQ_NONE;
  145. }
  146. static int tm2bcd(struct rtc_time *tm)
  147. {
  148. if (rtc_valid_tm(tm) != 0)
  149. return -EINVAL;
  150. tm->tm_sec = bin2bcd(tm->tm_sec);
  151. tm->tm_min = bin2bcd(tm->tm_min);
  152. tm->tm_hour = bin2bcd(tm->tm_hour);
  153. tm->tm_mday = bin2bcd(tm->tm_mday);
  154. tm->tm_mon = bin2bcd(tm->tm_mon + 1);
  155. tm->tm_year = bin2bcd(tm->tm_year);
  156. return 0;
  157. }
  158. static void bcd2tm(struct rtc_time *tm)
  159. {
  160. tm->tm_sec = bcd2bin(tm->tm_sec);
  161. tm->tm_min = bcd2bin(tm->tm_min);
  162. tm->tm_hour = bcd2bin(tm->tm_hour);
  163. tm->tm_mday = bcd2bin(tm->tm_mday);
  164. tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
  165. /* epoch == 1900 */
  166. tm->tm_year = bcd2bin(tm->tm_year);
  167. }
  168. /*
  169. * spear_rtc_read_time - set the time
  170. * @dev: rtc device in use
  171. * @tm: holds date and time
  172. *
  173. * This function read time and date. On success it will return 0
  174. * otherwise -ve error is returned.
  175. */
  176. static int spear_rtc_read_time(struct device *dev, struct rtc_time *tm)
  177. {
  178. struct platform_device *pdev = to_platform_device(dev);
  179. struct rtc_device *rtc = platform_get_drvdata(pdev);
  180. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  181. unsigned int time, date;
  182. /* we don't report wday/yday/isdst ... */
  183. rtc_wait_not_busy(config);
  184. time = readl(config->ioaddr + TIME_REG);
  185. date = readl(config->ioaddr + DATE_REG);
  186. tm->tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
  187. tm->tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
  188. tm->tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
  189. tm->tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
  190. tm->tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
  191. tm->tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
  192. bcd2tm(tm);
  193. return 0;
  194. }
  195. /*
  196. * spear_rtc_set_time - set the time
  197. * @dev: rtc device in use
  198. * @tm: holds date and time
  199. *
  200. * This function set time and date. On success it will return 0
  201. * otherwise -ve error is returned.
  202. */
  203. static int spear_rtc_set_time(struct device *dev, struct rtc_time *tm)
  204. {
  205. struct platform_device *pdev = to_platform_device(dev);
  206. struct rtc_device *rtc = platform_get_drvdata(pdev);
  207. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  208. unsigned int time, date, err = 0;
  209. if (tm2bcd(tm) < 0)
  210. return -EINVAL;
  211. rtc_wait_not_busy(config);
  212. time = (tm->tm_sec << SECOND_SHIFT) | (tm->tm_min << MINUTE_SHIFT) |
  213. (tm->tm_hour << HOUR_SHIFT);
  214. date = (tm->tm_mday << MDAY_SHIFT) | (tm->tm_mon << MONTH_SHIFT) |
  215. (tm->tm_year << YEAR_SHIFT);
  216. writel(time, config->ioaddr + TIME_REG);
  217. writel(date, config->ioaddr + DATE_REG);
  218. err = is_write_complete(config);
  219. if (err < 0)
  220. return err;
  221. return 0;
  222. }
  223. /*
  224. * spear_rtc_read_alarm - read the alarm time
  225. * @dev: rtc device in use
  226. * @alm: holds alarm date and time
  227. *
  228. * This function read alarm time and date. On success it will return 0
  229. * otherwise -ve error is returned.
  230. */
  231. static int spear_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  232. {
  233. struct platform_device *pdev = to_platform_device(dev);
  234. struct rtc_device *rtc = platform_get_drvdata(pdev);
  235. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  236. unsigned int time, date;
  237. rtc_wait_not_busy(config);
  238. time = readl(config->ioaddr + ALARM_TIME_REG);
  239. date = readl(config->ioaddr + ALARM_DATE_REG);
  240. alm->time.tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
  241. alm->time.tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
  242. alm->time.tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
  243. alm->time.tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
  244. alm->time.tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
  245. alm->time.tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
  246. bcd2tm(&alm->time);
  247. alm->enabled = readl(config->ioaddr + CTRL_REG) & INT_ENABLE;
  248. return 0;
  249. }
  250. /*
  251. * spear_rtc_set_alarm - set the alarm time
  252. * @dev: rtc device in use
  253. * @alm: holds alarm date and time
  254. *
  255. * This function set alarm time and date. On success it will return 0
  256. * otherwise -ve error is returned.
  257. */
  258. static int spear_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  259. {
  260. struct platform_device *pdev = to_platform_device(dev);
  261. struct rtc_device *rtc = platform_get_drvdata(pdev);
  262. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  263. unsigned int time, date, err = 0;
  264. if (tm2bcd(&alm->time) < 0)
  265. return -EINVAL;
  266. rtc_wait_not_busy(config);
  267. time = (alm->time.tm_sec << SECOND_SHIFT) | (alm->time.tm_min <<
  268. MINUTE_SHIFT) | (alm->time.tm_hour << HOUR_SHIFT);
  269. date = (alm->time.tm_mday << MDAY_SHIFT) | (alm->time.tm_mon <<
  270. MONTH_SHIFT) | (alm->time.tm_year << YEAR_SHIFT);
  271. writel(time, config->ioaddr + ALARM_TIME_REG);
  272. writel(date, config->ioaddr + ALARM_DATE_REG);
  273. err = is_write_complete(config);
  274. if (err < 0)
  275. return err;
  276. if (alm->enabled)
  277. spear_rtc_enable_interrupt(config);
  278. else
  279. spear_rtc_disable_interrupt(config);
  280. return 0;
  281. }
  282. static struct rtc_class_ops spear_rtc_ops = {
  283. .read_time = spear_rtc_read_time,
  284. .set_time = spear_rtc_set_time,
  285. .read_alarm = spear_rtc_read_alarm,
  286. .set_alarm = spear_rtc_set_alarm,
  287. };
  288. static int __devinit spear_rtc_probe(struct platform_device *pdev)
  289. {
  290. struct resource *res;
  291. struct rtc_device *rtc;
  292. struct spear_rtc_config *config;
  293. unsigned int status = 0;
  294. int irq;
  295. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  296. if (!res) {
  297. dev_err(&pdev->dev, "no resource defined\n");
  298. return -EBUSY;
  299. }
  300. if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
  301. dev_err(&pdev->dev, "rtc region already claimed\n");
  302. return -EBUSY;
  303. }
  304. config = kzalloc(sizeof(*config), GFP_KERNEL);
  305. if (!config) {
  306. dev_err(&pdev->dev, "out of memory\n");
  307. status = -ENOMEM;
  308. goto err_release_region;
  309. }
  310. config->clk = clk_get(&pdev->dev, NULL);
  311. if (IS_ERR(config->clk)) {
  312. status = PTR_ERR(config->clk);
  313. goto err_kfree;
  314. }
  315. status = clk_enable(config->clk);
  316. if (status < 0)
  317. goto err_clk_put;
  318. config->ioaddr = ioremap(res->start, resource_size(res));
  319. if (!config->ioaddr) {
  320. dev_err(&pdev->dev, "ioremap fail\n");
  321. status = -ENOMEM;
  322. goto err_disable_clock;
  323. }
  324. spin_lock_init(&config->lock);
  325. rtc = rtc_device_register(pdev->name, &pdev->dev, &spear_rtc_ops,
  326. THIS_MODULE);
  327. if (IS_ERR(rtc)) {
  328. dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
  329. PTR_ERR(rtc));
  330. status = PTR_ERR(rtc);
  331. goto err_iounmap;
  332. }
  333. platform_set_drvdata(pdev, rtc);
  334. dev_set_drvdata(&rtc->dev, config);
  335. /* alarm irqs */
  336. irq = platform_get_irq(pdev, 0);
  337. if (irq < 0) {
  338. dev_err(&pdev->dev, "no update irq?\n");
  339. status = irq;
  340. goto err_clear_platdata;
  341. }
  342. status = request_irq(irq, spear_rtc_irq, 0, pdev->name, rtc);
  343. if (status) {
  344. dev_err(&pdev->dev, "Alarm interrupt IRQ%d already \
  345. claimed\n", irq);
  346. goto err_clear_platdata;
  347. }
  348. if (!device_can_wakeup(&pdev->dev))
  349. device_init_wakeup(&pdev->dev, 1);
  350. return 0;
  351. err_clear_platdata:
  352. platform_set_drvdata(pdev, NULL);
  353. dev_set_drvdata(&rtc->dev, NULL);
  354. rtc_device_unregister(rtc);
  355. err_iounmap:
  356. iounmap(config->ioaddr);
  357. err_disable_clock:
  358. clk_disable(config->clk);
  359. err_clk_put:
  360. clk_put(config->clk);
  361. err_kfree:
  362. kfree(config);
  363. err_release_region:
  364. release_mem_region(res->start, resource_size(res));
  365. return status;
  366. }
  367. static int __devexit spear_rtc_remove(struct platform_device *pdev)
  368. {
  369. struct rtc_device *rtc = platform_get_drvdata(pdev);
  370. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  371. int irq;
  372. struct resource *res;
  373. /* leave rtc running, but disable irqs */
  374. spear_rtc_disable_interrupt(config);
  375. device_init_wakeup(&pdev->dev, 0);
  376. irq = platform_get_irq(pdev, 0);
  377. if (irq)
  378. free_irq(irq, pdev);
  379. clk_disable(config->clk);
  380. clk_put(config->clk);
  381. iounmap(config->ioaddr);
  382. kfree(config);
  383. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  384. if (res)
  385. release_mem_region(res->start, resource_size(res));
  386. platform_set_drvdata(pdev, NULL);
  387. dev_set_drvdata(&rtc->dev, NULL);
  388. rtc_device_unregister(rtc);
  389. return 0;
  390. }
  391. #ifdef CONFIG_PM
  392. static int spear_rtc_suspend(struct platform_device *pdev, pm_message_t state)
  393. {
  394. struct rtc_device *rtc = platform_get_drvdata(pdev);
  395. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  396. int irq;
  397. irq = platform_get_irq(pdev, 0);
  398. if (device_may_wakeup(&pdev->dev))
  399. enable_irq_wake(irq);
  400. else {
  401. spear_rtc_disable_interrupt(config);
  402. clk_disable(config->clk);
  403. }
  404. return 0;
  405. }
  406. static int spear_rtc_resume(struct platform_device *pdev)
  407. {
  408. struct rtc_device *rtc = platform_get_drvdata(pdev);
  409. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  410. int irq;
  411. irq = platform_get_irq(pdev, 0);
  412. if (device_may_wakeup(&pdev->dev))
  413. disable_irq_wake(irq);
  414. else {
  415. clk_enable(config->clk);
  416. spear_rtc_enable_interrupt(config);
  417. }
  418. return 0;
  419. }
  420. #else
  421. #define spear_rtc_suspend NULL
  422. #define spear_rtc_resume NULL
  423. #endif
  424. static void spear_rtc_shutdown(struct platform_device *pdev)
  425. {
  426. struct rtc_device *rtc = platform_get_drvdata(pdev);
  427. struct spear_rtc_config *config = dev_get_drvdata(&rtc->dev);
  428. spear_rtc_disable_interrupt(config);
  429. clk_disable(config->clk);
  430. }
  431. static struct platform_driver spear_rtc_driver = {
  432. .probe = spear_rtc_probe,
  433. .remove = __devexit_p(spear_rtc_remove),
  434. .suspend = spear_rtc_suspend,
  435. .resume = spear_rtc_resume,
  436. .shutdown = spear_rtc_shutdown,
  437. .driver = {
  438. .name = "rtc-spear",
  439. },
  440. };
  441. static int __init rtc_init(void)
  442. {
  443. return platform_driver_register(&spear_rtc_driver);
  444. }
  445. module_init(rtc_init);
  446. static void __exit rtc_exit(void)
  447. {
  448. platform_driver_unregister(&spear_rtc_driver);
  449. }
  450. module_exit(rtc_exit);
  451. MODULE_ALIAS("platform:rtc-spear");
  452. MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
  453. MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)");
  454. MODULE_LICENSE("GPL");