rtc-tegra.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * An RTC driver for the NVIDIA Tegra 200 series internal RTC.
  3. *
  4. * Copyright (c) 2010, NVIDIA Corporation.
  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 as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/clk.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/irq.h>
  26. #include <linux/io.h>
  27. #include <linux/delay.h>
  28. #include <linux/rtc.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/pm.h>
  31. /* set to 1 = busy every eight 32kHz clocks during copy of sec+msec to AHB */
  32. #define TEGRA_RTC_REG_BUSY 0x004
  33. #define TEGRA_RTC_REG_SECONDS 0x008
  34. /* when msec is read, the seconds are buffered into shadow seconds. */
  35. #define TEGRA_RTC_REG_SHADOW_SECONDS 0x00c
  36. #define TEGRA_RTC_REG_MILLI_SECONDS 0x010
  37. #define TEGRA_RTC_REG_SECONDS_ALARM0 0x014
  38. #define TEGRA_RTC_REG_SECONDS_ALARM1 0x018
  39. #define TEGRA_RTC_REG_MILLI_SECONDS_ALARM0 0x01c
  40. #define TEGRA_RTC_REG_INTR_MASK 0x028
  41. /* write 1 bits to clear status bits */
  42. #define TEGRA_RTC_REG_INTR_STATUS 0x02c
  43. /* bits in INTR_MASK */
  44. #define TEGRA_RTC_INTR_MASK_MSEC_CDN_ALARM (1<<4)
  45. #define TEGRA_RTC_INTR_MASK_SEC_CDN_ALARM (1<<3)
  46. #define TEGRA_RTC_INTR_MASK_MSEC_ALARM (1<<2)
  47. #define TEGRA_RTC_INTR_MASK_SEC_ALARM1 (1<<1)
  48. #define TEGRA_RTC_INTR_MASK_SEC_ALARM0 (1<<0)
  49. /* bits in INTR_STATUS */
  50. #define TEGRA_RTC_INTR_STATUS_MSEC_CDN_ALARM (1<<4)
  51. #define TEGRA_RTC_INTR_STATUS_SEC_CDN_ALARM (1<<3)
  52. #define TEGRA_RTC_INTR_STATUS_MSEC_ALARM (1<<2)
  53. #define TEGRA_RTC_INTR_STATUS_SEC_ALARM1 (1<<1)
  54. #define TEGRA_RTC_INTR_STATUS_SEC_ALARM0 (1<<0)
  55. struct tegra_rtc_info {
  56. struct platform_device *pdev;
  57. struct rtc_device *rtc_dev;
  58. void __iomem *rtc_base; /* NULL if not initialized. */
  59. struct clk *clk;
  60. int tegra_rtc_irq; /* alarm and periodic irq */
  61. spinlock_t tegra_rtc_lock;
  62. };
  63. /* RTC hardware is busy when it is updating its values over AHB once
  64. * every eight 32kHz clocks (~250uS).
  65. * outside of these updates the CPU is free to write.
  66. * CPU is always free to read.
  67. */
  68. static inline u32 tegra_rtc_check_busy(struct tegra_rtc_info *info)
  69. {
  70. return readl(info->rtc_base + TEGRA_RTC_REG_BUSY) & 1;
  71. }
  72. /* Wait for hardware to be ready for writing.
  73. * This function tries to maximize the amount of time before the next update.
  74. * It does this by waiting for the RTC to become busy with its periodic update,
  75. * then returning once the RTC first becomes not busy.
  76. * This periodic update (where the seconds and milliseconds are copied to the
  77. * AHB side) occurs every eight 32kHz clocks (~250uS).
  78. * The behavior of this function allows us to make some assumptions without
  79. * introducing a race, because 250uS is plenty of time to read/write a value.
  80. */
  81. static int tegra_rtc_wait_while_busy(struct device *dev)
  82. {
  83. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  84. int retries = 500; /* ~490 us is the worst case, ~250 us is best. */
  85. /* first wait for the RTC to become busy. this is when it
  86. * posts its updated seconds+msec registers to AHB side. */
  87. while (tegra_rtc_check_busy(info)) {
  88. if (!retries--)
  89. goto retry_failed;
  90. udelay(1);
  91. }
  92. /* now we have about 250 us to manipulate registers */
  93. return 0;
  94. retry_failed:
  95. dev_err(dev, "write failed:retry count exceeded.\n");
  96. return -ETIMEDOUT;
  97. }
  98. static int tegra_rtc_read_time(struct device *dev, struct rtc_time *tm)
  99. {
  100. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  101. unsigned long sec, msec;
  102. unsigned long sl_irq_flags;
  103. /* RTC hardware copies seconds to shadow seconds when a read
  104. * of milliseconds occurs. use a lock to keep other threads out. */
  105. spin_lock_irqsave(&info->tegra_rtc_lock, sl_irq_flags);
  106. msec = readl(info->rtc_base + TEGRA_RTC_REG_MILLI_SECONDS);
  107. sec = readl(info->rtc_base + TEGRA_RTC_REG_SHADOW_SECONDS);
  108. spin_unlock_irqrestore(&info->tegra_rtc_lock, sl_irq_flags);
  109. rtc_time_to_tm(sec, tm);
  110. dev_vdbg(dev, "time read as %lu. %d/%d/%d %d:%02u:%02u\n",
  111. sec,
  112. tm->tm_mon + 1,
  113. tm->tm_mday,
  114. tm->tm_year + 1900,
  115. tm->tm_hour,
  116. tm->tm_min,
  117. tm->tm_sec
  118. );
  119. return 0;
  120. }
  121. static int tegra_rtc_set_time(struct device *dev, struct rtc_time *tm)
  122. {
  123. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  124. unsigned long sec;
  125. int ret;
  126. /* convert tm to seconds. */
  127. ret = rtc_valid_tm(tm);
  128. if (ret)
  129. return ret;
  130. rtc_tm_to_time(tm, &sec);
  131. dev_vdbg(dev, "time set to %lu. %d/%d/%d %d:%02u:%02u\n",
  132. sec,
  133. tm->tm_mon+1,
  134. tm->tm_mday,
  135. tm->tm_year+1900,
  136. tm->tm_hour,
  137. tm->tm_min,
  138. tm->tm_sec
  139. );
  140. /* seconds only written if wait succeeded. */
  141. ret = tegra_rtc_wait_while_busy(dev);
  142. if (!ret)
  143. writel(sec, info->rtc_base + TEGRA_RTC_REG_SECONDS);
  144. dev_vdbg(dev, "time read back as %d\n",
  145. readl(info->rtc_base + TEGRA_RTC_REG_SECONDS));
  146. return ret;
  147. }
  148. static int tegra_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  149. {
  150. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  151. unsigned long sec;
  152. unsigned tmp;
  153. sec = readl(info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0);
  154. if (sec == 0) {
  155. /* alarm is disabled. */
  156. alarm->enabled = 0;
  157. } else {
  158. /* alarm is enabled. */
  159. alarm->enabled = 1;
  160. rtc_time_to_tm(sec, &alarm->time);
  161. }
  162. tmp = readl(info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
  163. alarm->pending = (tmp & TEGRA_RTC_INTR_STATUS_SEC_ALARM0) != 0;
  164. return 0;
  165. }
  166. static int tegra_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  167. {
  168. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  169. unsigned status;
  170. unsigned long sl_irq_flags;
  171. tegra_rtc_wait_while_busy(dev);
  172. spin_lock_irqsave(&info->tegra_rtc_lock, sl_irq_flags);
  173. /* read the original value, and OR in the flag. */
  174. status = readl(info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
  175. if (enabled)
  176. status |= TEGRA_RTC_INTR_MASK_SEC_ALARM0; /* set it */
  177. else
  178. status &= ~TEGRA_RTC_INTR_MASK_SEC_ALARM0; /* clear it */
  179. writel(status, info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
  180. spin_unlock_irqrestore(&info->tegra_rtc_lock, sl_irq_flags);
  181. return 0;
  182. }
  183. static int tegra_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  184. {
  185. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  186. unsigned long sec;
  187. if (alarm->enabled)
  188. rtc_tm_to_time(&alarm->time, &sec);
  189. else
  190. sec = 0;
  191. tegra_rtc_wait_while_busy(dev);
  192. writel(sec, info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0);
  193. dev_vdbg(dev, "alarm read back as %d\n",
  194. readl(info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0));
  195. /* if successfully written and alarm is enabled ... */
  196. if (sec) {
  197. tegra_rtc_alarm_irq_enable(dev, 1);
  198. dev_vdbg(dev, "alarm set as %lu. %d/%d/%d %d:%02u:%02u\n",
  199. sec,
  200. alarm->time.tm_mon+1,
  201. alarm->time.tm_mday,
  202. alarm->time.tm_year+1900,
  203. alarm->time.tm_hour,
  204. alarm->time.tm_min,
  205. alarm->time.tm_sec);
  206. } else {
  207. /* disable alarm if 0 or write error. */
  208. dev_vdbg(dev, "alarm disabled\n");
  209. tegra_rtc_alarm_irq_enable(dev, 0);
  210. }
  211. return 0;
  212. }
  213. static int tegra_rtc_proc(struct device *dev, struct seq_file *seq)
  214. {
  215. if (!dev || !dev->driver)
  216. return 0;
  217. seq_printf(seq, "name\t\t: %s\n", dev_name(dev));
  218. return 0;
  219. }
  220. static irqreturn_t tegra_rtc_irq_handler(int irq, void *data)
  221. {
  222. struct device *dev = data;
  223. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  224. unsigned long events = 0;
  225. unsigned status;
  226. unsigned long sl_irq_flags;
  227. status = readl(info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
  228. if (status) {
  229. /* clear the interrupt masks and status on any irq. */
  230. tegra_rtc_wait_while_busy(dev);
  231. spin_lock_irqsave(&info->tegra_rtc_lock, sl_irq_flags);
  232. writel(0, info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
  233. writel(status, info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
  234. spin_unlock_irqrestore(&info->tegra_rtc_lock, sl_irq_flags);
  235. }
  236. /* check if Alarm */
  237. if ((status & TEGRA_RTC_INTR_STATUS_SEC_ALARM0))
  238. events |= RTC_IRQF | RTC_AF;
  239. /* check if Periodic */
  240. if ((status & TEGRA_RTC_INTR_STATUS_SEC_CDN_ALARM))
  241. events |= RTC_IRQF | RTC_PF;
  242. rtc_update_irq(info->rtc_dev, 1, events);
  243. return IRQ_HANDLED;
  244. }
  245. static const struct rtc_class_ops tegra_rtc_ops = {
  246. .read_time = tegra_rtc_read_time,
  247. .set_time = tegra_rtc_set_time,
  248. .read_alarm = tegra_rtc_read_alarm,
  249. .set_alarm = tegra_rtc_set_alarm,
  250. .proc = tegra_rtc_proc,
  251. .alarm_irq_enable = tegra_rtc_alarm_irq_enable,
  252. };
  253. static const struct of_device_id tegra_rtc_dt_match[] = {
  254. { .compatible = "nvidia,tegra20-rtc", },
  255. {}
  256. };
  257. MODULE_DEVICE_TABLE(of, tegra_rtc_dt_match);
  258. static int __init tegra_rtc_probe(struct platform_device *pdev)
  259. {
  260. struct tegra_rtc_info *info;
  261. struct resource *res;
  262. int ret;
  263. info = devm_kzalloc(&pdev->dev, sizeof(struct tegra_rtc_info),
  264. GFP_KERNEL);
  265. if (!info)
  266. return -ENOMEM;
  267. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  268. info->rtc_base = devm_ioremap_resource(&pdev->dev, res);
  269. if (IS_ERR(info->rtc_base))
  270. return PTR_ERR(info->rtc_base);
  271. info->tegra_rtc_irq = platform_get_irq(pdev, 0);
  272. if (info->tegra_rtc_irq <= 0)
  273. return -EBUSY;
  274. info->clk = devm_clk_get(&pdev->dev, NULL);
  275. if (IS_ERR(info->clk))
  276. return PTR_ERR(info->clk);
  277. ret = clk_prepare_enable(info->clk);
  278. if (ret < 0)
  279. return ret;
  280. /* set context info. */
  281. info->pdev = pdev;
  282. spin_lock_init(&info->tegra_rtc_lock);
  283. platform_set_drvdata(pdev, info);
  284. /* clear out the hardware. */
  285. writel(0, info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0);
  286. writel(0xffffffff, info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
  287. writel(0, info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
  288. device_init_wakeup(&pdev->dev, 1);
  289. info->rtc_dev = devm_rtc_device_register(&pdev->dev,
  290. dev_name(&pdev->dev), &tegra_rtc_ops,
  291. THIS_MODULE);
  292. if (IS_ERR(info->rtc_dev)) {
  293. ret = PTR_ERR(info->rtc_dev);
  294. dev_err(&pdev->dev, "Unable to register device (err=%d).\n",
  295. ret);
  296. goto disable_clk;
  297. }
  298. ret = devm_request_irq(&pdev->dev, info->tegra_rtc_irq,
  299. tegra_rtc_irq_handler, IRQF_TRIGGER_HIGH,
  300. dev_name(&pdev->dev), &pdev->dev);
  301. if (ret) {
  302. dev_err(&pdev->dev,
  303. "Unable to request interrupt for device (err=%d).\n",
  304. ret);
  305. goto disable_clk;
  306. }
  307. dev_notice(&pdev->dev, "Tegra internal Real Time Clock\n");
  308. return 0;
  309. disable_clk:
  310. clk_disable_unprepare(info->clk);
  311. return ret;
  312. }
  313. static int tegra_rtc_remove(struct platform_device *pdev)
  314. {
  315. struct tegra_rtc_info *info = platform_get_drvdata(pdev);
  316. clk_disable_unprepare(info->clk);
  317. return 0;
  318. }
  319. #ifdef CONFIG_PM_SLEEP
  320. static int tegra_rtc_suspend(struct device *dev)
  321. {
  322. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  323. tegra_rtc_wait_while_busy(dev);
  324. /* only use ALARM0 as a wake source. */
  325. writel(0xffffffff, info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
  326. writel(TEGRA_RTC_INTR_STATUS_SEC_ALARM0,
  327. info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
  328. dev_vdbg(dev, "alarm sec = %d\n",
  329. readl(info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0));
  330. dev_vdbg(dev, "Suspend (device_may_wakeup=%d) irq:%d\n",
  331. device_may_wakeup(dev), info->tegra_rtc_irq);
  332. /* leave the alarms on as a wake source. */
  333. if (device_may_wakeup(dev))
  334. enable_irq_wake(info->tegra_rtc_irq);
  335. return 0;
  336. }
  337. static int tegra_rtc_resume(struct device *dev)
  338. {
  339. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  340. dev_vdbg(dev, "Resume (device_may_wakeup=%d)\n",
  341. device_may_wakeup(dev));
  342. /* alarms were left on as a wake source, turn them off. */
  343. if (device_may_wakeup(dev))
  344. disable_irq_wake(info->tegra_rtc_irq);
  345. return 0;
  346. }
  347. #endif
  348. static SIMPLE_DEV_PM_OPS(tegra_rtc_pm_ops, tegra_rtc_suspend, tegra_rtc_resume);
  349. static void tegra_rtc_shutdown(struct platform_device *pdev)
  350. {
  351. dev_vdbg(&pdev->dev, "disabling interrupts.\n");
  352. tegra_rtc_alarm_irq_enable(&pdev->dev, 0);
  353. }
  354. MODULE_ALIAS("platform:tegra_rtc");
  355. static struct platform_driver tegra_rtc_driver = {
  356. .remove = tegra_rtc_remove,
  357. .shutdown = tegra_rtc_shutdown,
  358. .driver = {
  359. .name = "tegra_rtc",
  360. .of_match_table = tegra_rtc_dt_match,
  361. .pm = &tegra_rtc_pm_ops,
  362. },
  363. };
  364. module_platform_driver_probe(tegra_rtc_driver, tegra_rtc_probe);
  365. MODULE_AUTHOR("Jon Mayo <jmayo@nvidia.com>");
  366. MODULE_DESCRIPTION("driver for Tegra internal RTC");
  367. MODULE_LICENSE("GPL");