rtc-spear.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/rtc/rtc-spear.c
  4. *
  5. * Copyright (C) 2010 ST Microelectronics
  6. * Rajeev Kumar<[email protected]>
  7. */
  8. #include <linux/bcd.h>
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/init.h>
  12. #include <linux/io.h>
  13. #include <linux/irq.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/rtc.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. /* RTC registers */
  21. #define TIME_REG 0x00
  22. #define DATE_REG 0x04
  23. #define ALARM_TIME_REG 0x08
  24. #define ALARM_DATE_REG 0x0C
  25. #define CTRL_REG 0x10
  26. #define STATUS_REG 0x14
  27. /* TIME_REG & ALARM_TIME_REG */
  28. #define SECONDS_UNITS (0xf<<0) /* seconds units position */
  29. #define SECONDS_TENS (0x7<<4) /* seconds tens position */
  30. #define MINUTES_UNITS (0xf<<8) /* minutes units position */
  31. #define MINUTES_TENS (0x7<<12) /* minutes tens position */
  32. #define HOURS_UNITS (0xf<<16) /* hours units position */
  33. #define HOURS_TENS (0x3<<20) /* hours tens position */
  34. /* DATE_REG & ALARM_DATE_REG */
  35. #define DAYS_UNITS (0xf<<0) /* days units position */
  36. #define DAYS_TENS (0x3<<4) /* days tens position */
  37. #define MONTHS_UNITS (0xf<<8) /* months units position */
  38. #define MONTHS_TENS (0x1<<12) /* months tens position */
  39. #define YEARS_UNITS (0xf<<16) /* years units position */
  40. #define YEARS_TENS (0xf<<20) /* years tens position */
  41. #define YEARS_HUNDREDS (0xf<<24) /* years hundereds position */
  42. #define YEARS_MILLENIUMS (0xf<<28) /* years millenium position */
  43. /* MASK SHIFT TIME_REG & ALARM_TIME_REG*/
  44. #define SECOND_SHIFT 0x00 /* seconds units */
  45. #define MINUTE_SHIFT 0x08 /* minutes units position */
  46. #define HOUR_SHIFT 0x10 /* hours units position */
  47. #define MDAY_SHIFT 0x00 /* Month day shift */
  48. #define MONTH_SHIFT 0x08 /* Month shift */
  49. #define YEAR_SHIFT 0x10 /* Year shift */
  50. #define SECOND_MASK 0x7F
  51. #define MIN_MASK 0x7F
  52. #define HOUR_MASK 0x3F
  53. #define DAY_MASK 0x3F
  54. #define MONTH_MASK 0x7F
  55. #define YEAR_MASK 0xFFFF
  56. /* date reg equal to time reg, for debug only */
  57. #define TIME_BYP (1<<9)
  58. #define INT_ENABLE (1<<31) /* interrupt enable */
  59. /* STATUS_REG */
  60. #define CLK_UNCONNECTED (1<<0)
  61. #define PEND_WR_TIME (1<<2)
  62. #define PEND_WR_DATE (1<<3)
  63. #define LOST_WR_TIME (1<<4)
  64. #define LOST_WR_DATE (1<<5)
  65. #define RTC_INT_MASK (1<<31)
  66. #define STATUS_BUSY (PEND_WR_TIME | PEND_WR_DATE)
  67. #define STATUS_FAIL (LOST_WR_TIME | LOST_WR_DATE)
  68. struct spear_rtc_config {
  69. struct rtc_device *rtc;
  70. struct clk *clk;
  71. spinlock_t lock;
  72. void __iomem *ioaddr;
  73. unsigned int irq_wake;
  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 spear_rtc_config *config = dev_id;
  132. unsigned long events = 0;
  133. unsigned int irq_data;
  134. spin_lock(&config->lock);
  135. irq_data = readl(config->ioaddr + STATUS_REG);
  136. spin_unlock(&config->lock);
  137. if ((irq_data & RTC_INT_MASK)) {
  138. spear_rtc_clear_interrupt(config);
  139. events = RTC_IRQF | RTC_AF;
  140. rtc_update_irq(config->rtc, 1, events);
  141. return IRQ_HANDLED;
  142. } else
  143. return IRQ_NONE;
  144. }
  145. static void tm2bcd(struct rtc_time *tm)
  146. {
  147. tm->tm_sec = bin2bcd(tm->tm_sec);
  148. tm->tm_min = bin2bcd(tm->tm_min);
  149. tm->tm_hour = bin2bcd(tm->tm_hour);
  150. tm->tm_mday = bin2bcd(tm->tm_mday);
  151. tm->tm_mon = bin2bcd(tm->tm_mon + 1);
  152. tm->tm_year = bin2bcd(tm->tm_year);
  153. }
  154. static void bcd2tm(struct rtc_time *tm)
  155. {
  156. tm->tm_sec = bcd2bin(tm->tm_sec);
  157. tm->tm_min = bcd2bin(tm->tm_min);
  158. tm->tm_hour = bcd2bin(tm->tm_hour);
  159. tm->tm_mday = bcd2bin(tm->tm_mday);
  160. tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
  161. /* epoch == 1900 */
  162. tm->tm_year = bcd2bin(tm->tm_year);
  163. }
  164. /*
  165. * spear_rtc_read_time - set the time
  166. * @dev: rtc device in use
  167. * @tm: holds date and time
  168. *
  169. * This function read time and date. On success it will return 0
  170. * otherwise -ve error is returned.
  171. */
  172. static int spear_rtc_read_time(struct device *dev, struct rtc_time *tm)
  173. {
  174. struct spear_rtc_config *config = dev_get_drvdata(dev);
  175. unsigned int time, date;
  176. /* we don't report wday/yday/isdst ... */
  177. rtc_wait_not_busy(config);
  178. do {
  179. time = readl(config->ioaddr + TIME_REG);
  180. date = readl(config->ioaddr + DATE_REG);
  181. } while (time == readl(config->ioaddr + TIME_REG));
  182. tm->tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
  183. tm->tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
  184. tm->tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
  185. tm->tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
  186. tm->tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
  187. tm->tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
  188. bcd2tm(tm);
  189. return 0;
  190. }
  191. /*
  192. * spear_rtc_set_time - set the time
  193. * @dev: rtc device in use
  194. * @tm: holds date and time
  195. *
  196. * This function set time and date. On success it will return 0
  197. * otherwise -ve error is returned.
  198. */
  199. static int spear_rtc_set_time(struct device *dev, struct rtc_time *tm)
  200. {
  201. struct spear_rtc_config *config = dev_get_drvdata(dev);
  202. unsigned int time, date;
  203. tm2bcd(tm);
  204. rtc_wait_not_busy(config);
  205. time = (tm->tm_sec << SECOND_SHIFT) | (tm->tm_min << MINUTE_SHIFT) |
  206. (tm->tm_hour << HOUR_SHIFT);
  207. date = (tm->tm_mday << MDAY_SHIFT) | (tm->tm_mon << MONTH_SHIFT) |
  208. (tm->tm_year << YEAR_SHIFT);
  209. writel(time, config->ioaddr + TIME_REG);
  210. writel(date, config->ioaddr + DATE_REG);
  211. return is_write_complete(config);
  212. }
  213. /*
  214. * spear_rtc_read_alarm - read the alarm time
  215. * @dev: rtc device in use
  216. * @alm: holds alarm date and time
  217. *
  218. * This function read alarm time and date. On success it will return 0
  219. * otherwise -ve error is returned.
  220. */
  221. static int spear_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  222. {
  223. struct spear_rtc_config *config = dev_get_drvdata(dev);
  224. unsigned int time, date;
  225. rtc_wait_not_busy(config);
  226. time = readl(config->ioaddr + ALARM_TIME_REG);
  227. date = readl(config->ioaddr + ALARM_DATE_REG);
  228. alm->time.tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
  229. alm->time.tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
  230. alm->time.tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
  231. alm->time.tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
  232. alm->time.tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
  233. alm->time.tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
  234. bcd2tm(&alm->time);
  235. alm->enabled = readl(config->ioaddr + CTRL_REG) & INT_ENABLE;
  236. return 0;
  237. }
  238. /*
  239. * spear_rtc_set_alarm - set the alarm time
  240. * @dev: rtc device in use
  241. * @alm: holds alarm date and time
  242. *
  243. * This function set alarm time and date. On success it will return 0
  244. * otherwise -ve error is returned.
  245. */
  246. static int spear_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  247. {
  248. struct spear_rtc_config *config = dev_get_drvdata(dev);
  249. unsigned int time, date;
  250. int err;
  251. tm2bcd(&alm->time);
  252. rtc_wait_not_busy(config);
  253. time = (alm->time.tm_sec << SECOND_SHIFT) | (alm->time.tm_min <<
  254. MINUTE_SHIFT) | (alm->time.tm_hour << HOUR_SHIFT);
  255. date = (alm->time.tm_mday << MDAY_SHIFT) | (alm->time.tm_mon <<
  256. MONTH_SHIFT) | (alm->time.tm_year << YEAR_SHIFT);
  257. writel(time, config->ioaddr + ALARM_TIME_REG);
  258. writel(date, config->ioaddr + ALARM_DATE_REG);
  259. err = is_write_complete(config);
  260. if (err < 0)
  261. return err;
  262. if (alm->enabled)
  263. spear_rtc_enable_interrupt(config);
  264. else
  265. spear_rtc_disable_interrupt(config);
  266. return 0;
  267. }
  268. static int spear_alarm_irq_enable(struct device *dev, unsigned int enabled)
  269. {
  270. struct spear_rtc_config *config = dev_get_drvdata(dev);
  271. int ret = 0;
  272. spear_rtc_clear_interrupt(config);
  273. switch (enabled) {
  274. case 0:
  275. /* alarm off */
  276. spear_rtc_disable_interrupt(config);
  277. break;
  278. case 1:
  279. /* alarm on */
  280. spear_rtc_enable_interrupt(config);
  281. break;
  282. default:
  283. ret = -EINVAL;
  284. break;
  285. }
  286. return ret;
  287. }
  288. static const struct rtc_class_ops spear_rtc_ops = {
  289. .read_time = spear_rtc_read_time,
  290. .set_time = spear_rtc_set_time,
  291. .read_alarm = spear_rtc_read_alarm,
  292. .set_alarm = spear_rtc_set_alarm,
  293. .alarm_irq_enable = spear_alarm_irq_enable,
  294. };
  295. static int spear_rtc_probe(struct platform_device *pdev)
  296. {
  297. struct spear_rtc_config *config;
  298. int status = 0;
  299. int irq;
  300. config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL);
  301. if (!config)
  302. return -ENOMEM;
  303. config->rtc = devm_rtc_allocate_device(&pdev->dev);
  304. if (IS_ERR(config->rtc))
  305. return PTR_ERR(config->rtc);
  306. /* alarm irqs */
  307. irq = platform_get_irq(pdev, 0);
  308. if (irq < 0)
  309. return irq;
  310. status = devm_request_irq(&pdev->dev, irq, spear_rtc_irq, 0, pdev->name,
  311. config);
  312. if (status) {
  313. dev_err(&pdev->dev, "Alarm interrupt IRQ%d already claimed\n",
  314. irq);
  315. return status;
  316. }
  317. config->ioaddr = devm_platform_ioremap_resource(pdev, 0);
  318. if (IS_ERR(config->ioaddr))
  319. return PTR_ERR(config->ioaddr);
  320. config->clk = devm_clk_get(&pdev->dev, NULL);
  321. if (IS_ERR(config->clk))
  322. return PTR_ERR(config->clk);
  323. status = clk_prepare_enable(config->clk);
  324. if (status < 0)
  325. return status;
  326. spin_lock_init(&config->lock);
  327. platform_set_drvdata(pdev, config);
  328. config->rtc->ops = &spear_rtc_ops;
  329. config->rtc->range_min = RTC_TIMESTAMP_BEGIN_0000;
  330. config->rtc->range_max = RTC_TIMESTAMP_END_9999;
  331. status = devm_rtc_register_device(config->rtc);
  332. if (status)
  333. goto err_disable_clock;
  334. if (!device_can_wakeup(&pdev->dev))
  335. device_init_wakeup(&pdev->dev, 1);
  336. return 0;
  337. err_disable_clock:
  338. clk_disable_unprepare(config->clk);
  339. return status;
  340. }
  341. static int spear_rtc_remove(struct platform_device *pdev)
  342. {
  343. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  344. spear_rtc_disable_interrupt(config);
  345. clk_disable_unprepare(config->clk);
  346. device_init_wakeup(&pdev->dev, 0);
  347. return 0;
  348. }
  349. #ifdef CONFIG_PM_SLEEP
  350. static int spear_rtc_suspend(struct device *dev)
  351. {
  352. struct platform_device *pdev = to_platform_device(dev);
  353. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  354. int irq;
  355. irq = platform_get_irq(pdev, 0);
  356. if (device_may_wakeup(&pdev->dev)) {
  357. if (!enable_irq_wake(irq))
  358. config->irq_wake = 1;
  359. } else {
  360. spear_rtc_disable_interrupt(config);
  361. clk_disable(config->clk);
  362. }
  363. return 0;
  364. }
  365. static int spear_rtc_resume(struct device *dev)
  366. {
  367. struct platform_device *pdev = to_platform_device(dev);
  368. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  369. int irq;
  370. irq = platform_get_irq(pdev, 0);
  371. if (device_may_wakeup(&pdev->dev)) {
  372. if (config->irq_wake) {
  373. disable_irq_wake(irq);
  374. config->irq_wake = 0;
  375. }
  376. } else {
  377. clk_enable(config->clk);
  378. spear_rtc_enable_interrupt(config);
  379. }
  380. return 0;
  381. }
  382. #endif
  383. static SIMPLE_DEV_PM_OPS(spear_rtc_pm_ops, spear_rtc_suspend, spear_rtc_resume);
  384. static void spear_rtc_shutdown(struct platform_device *pdev)
  385. {
  386. struct spear_rtc_config *config = platform_get_drvdata(pdev);
  387. spear_rtc_disable_interrupt(config);
  388. clk_disable(config->clk);
  389. }
  390. #ifdef CONFIG_OF
  391. static const struct of_device_id spear_rtc_id_table[] = {
  392. { .compatible = "st,spear600-rtc" },
  393. {}
  394. };
  395. MODULE_DEVICE_TABLE(of, spear_rtc_id_table);
  396. #endif
  397. static struct platform_driver spear_rtc_driver = {
  398. .probe = spear_rtc_probe,
  399. .remove = spear_rtc_remove,
  400. .shutdown = spear_rtc_shutdown,
  401. .driver = {
  402. .name = "rtc-spear",
  403. .pm = &spear_rtc_pm_ops,
  404. .of_match_table = of_match_ptr(spear_rtc_id_table),
  405. },
  406. };
  407. module_platform_driver(spear_rtc_driver);
  408. MODULE_ALIAS("platform:rtc-spear");
  409. MODULE_AUTHOR("Rajeev Kumar <[email protected]>");
  410. MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)");
  411. MODULE_LICENSE("GPL");