rtc-tegra.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/init.h>
  23. #include <linux/io.h>
  24. #include <linux/irq.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pm.h>
  29. #include <linux/rtc.h>
  30. #include <linux/slab.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. rtc_tm_to_time(tm, &sec);
  128. dev_vdbg(dev, "time set to %lu. %d/%d/%d %d:%02u:%02u\n",
  129. sec,
  130. tm->tm_mon+1,
  131. tm->tm_mday,
  132. tm->tm_year+1900,
  133. tm->tm_hour,
  134. tm->tm_min,
  135. tm->tm_sec
  136. );
  137. /* seconds only written if wait succeeded. */
  138. ret = tegra_rtc_wait_while_busy(dev);
  139. if (!ret)
  140. writel(sec, info->rtc_base + TEGRA_RTC_REG_SECONDS);
  141. dev_vdbg(dev, "time read back as %d\n",
  142. readl(info->rtc_base + TEGRA_RTC_REG_SECONDS));
  143. return ret;
  144. }
  145. static int tegra_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  146. {
  147. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  148. unsigned long sec;
  149. unsigned tmp;
  150. sec = readl(info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0);
  151. if (sec == 0) {
  152. /* alarm is disabled. */
  153. alarm->enabled = 0;
  154. } else {
  155. /* alarm is enabled. */
  156. alarm->enabled = 1;
  157. rtc_time_to_tm(sec, &alarm->time);
  158. }
  159. tmp = readl(info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
  160. alarm->pending = (tmp & TEGRA_RTC_INTR_STATUS_SEC_ALARM0) != 0;
  161. return 0;
  162. }
  163. static int tegra_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  164. {
  165. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  166. unsigned status;
  167. unsigned long sl_irq_flags;
  168. tegra_rtc_wait_while_busy(dev);
  169. spin_lock_irqsave(&info->tegra_rtc_lock, sl_irq_flags);
  170. /* read the original value, and OR in the flag. */
  171. status = readl(info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
  172. if (enabled)
  173. status |= TEGRA_RTC_INTR_MASK_SEC_ALARM0; /* set it */
  174. else
  175. status &= ~TEGRA_RTC_INTR_MASK_SEC_ALARM0; /* clear it */
  176. writel(status, info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
  177. spin_unlock_irqrestore(&info->tegra_rtc_lock, sl_irq_flags);
  178. return 0;
  179. }
  180. static int tegra_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  181. {
  182. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  183. unsigned long sec;
  184. if (alarm->enabled)
  185. rtc_tm_to_time(&alarm->time, &sec);
  186. else
  187. sec = 0;
  188. tegra_rtc_wait_while_busy(dev);
  189. writel(sec, info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0);
  190. dev_vdbg(dev, "alarm read back as %d\n",
  191. readl(info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0));
  192. /* if successfully written and alarm is enabled ... */
  193. if (sec) {
  194. tegra_rtc_alarm_irq_enable(dev, 1);
  195. dev_vdbg(dev, "alarm set as %lu. %d/%d/%d %d:%02u:%02u\n",
  196. sec,
  197. alarm->time.tm_mon+1,
  198. alarm->time.tm_mday,
  199. alarm->time.tm_year+1900,
  200. alarm->time.tm_hour,
  201. alarm->time.tm_min,
  202. alarm->time.tm_sec);
  203. } else {
  204. /* disable alarm if 0 or write error. */
  205. dev_vdbg(dev, "alarm disabled\n");
  206. tegra_rtc_alarm_irq_enable(dev, 0);
  207. }
  208. return 0;
  209. }
  210. static int tegra_rtc_proc(struct device *dev, struct seq_file *seq)
  211. {
  212. if (!dev || !dev->driver)
  213. return 0;
  214. seq_printf(seq, "name\t\t: %s\n", dev_name(dev));
  215. return 0;
  216. }
  217. static irqreturn_t tegra_rtc_irq_handler(int irq, void *data)
  218. {
  219. struct device *dev = data;
  220. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  221. unsigned long events = 0;
  222. unsigned status;
  223. unsigned long sl_irq_flags;
  224. status = readl(info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
  225. if (status) {
  226. /* clear the interrupt masks and status on any irq. */
  227. tegra_rtc_wait_while_busy(dev);
  228. spin_lock_irqsave(&info->tegra_rtc_lock, sl_irq_flags);
  229. writel(0, info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
  230. writel(status, info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
  231. spin_unlock_irqrestore(&info->tegra_rtc_lock, sl_irq_flags);
  232. }
  233. /* check if Alarm */
  234. if ((status & TEGRA_RTC_INTR_STATUS_SEC_ALARM0))
  235. events |= RTC_IRQF | RTC_AF;
  236. /* check if Periodic */
  237. if ((status & TEGRA_RTC_INTR_STATUS_SEC_CDN_ALARM))
  238. events |= RTC_IRQF | RTC_PF;
  239. rtc_update_irq(info->rtc_dev, 1, events);
  240. return IRQ_HANDLED;
  241. }
  242. static const struct rtc_class_ops tegra_rtc_ops = {
  243. .read_time = tegra_rtc_read_time,
  244. .set_time = tegra_rtc_set_time,
  245. .read_alarm = tegra_rtc_read_alarm,
  246. .set_alarm = tegra_rtc_set_alarm,
  247. .proc = tegra_rtc_proc,
  248. .alarm_irq_enable = tegra_rtc_alarm_irq_enable,
  249. };
  250. static const struct of_device_id tegra_rtc_dt_match[] = {
  251. { .compatible = "nvidia,tegra20-rtc", },
  252. {}
  253. };
  254. MODULE_DEVICE_TABLE(of, tegra_rtc_dt_match);
  255. static int __init tegra_rtc_probe(struct platform_device *pdev)
  256. {
  257. struct tegra_rtc_info *info;
  258. struct resource *res;
  259. int ret;
  260. info = devm_kzalloc(&pdev->dev, sizeof(struct tegra_rtc_info),
  261. GFP_KERNEL);
  262. if (!info)
  263. return -ENOMEM;
  264. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  265. info->rtc_base = devm_ioremap_resource(&pdev->dev, res);
  266. if (IS_ERR(info->rtc_base))
  267. return PTR_ERR(info->rtc_base);
  268. info->tegra_rtc_irq = platform_get_irq(pdev, 0);
  269. if (info->tegra_rtc_irq <= 0)
  270. return -EBUSY;
  271. info->clk = devm_clk_get(&pdev->dev, NULL);
  272. if (IS_ERR(info->clk))
  273. return PTR_ERR(info->clk);
  274. ret = clk_prepare_enable(info->clk);
  275. if (ret < 0)
  276. return ret;
  277. /* set context info. */
  278. info->pdev = pdev;
  279. spin_lock_init(&info->tegra_rtc_lock);
  280. platform_set_drvdata(pdev, info);
  281. /* clear out the hardware. */
  282. writel(0, info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0);
  283. writel(0xffffffff, info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
  284. writel(0, info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
  285. device_init_wakeup(&pdev->dev, 1);
  286. info->rtc_dev = devm_rtc_device_register(&pdev->dev,
  287. dev_name(&pdev->dev), &tegra_rtc_ops,
  288. THIS_MODULE);
  289. if (IS_ERR(info->rtc_dev)) {
  290. ret = PTR_ERR(info->rtc_dev);
  291. dev_err(&pdev->dev, "Unable to register device (err=%d).\n",
  292. ret);
  293. goto disable_clk;
  294. }
  295. ret = devm_request_irq(&pdev->dev, info->tegra_rtc_irq,
  296. tegra_rtc_irq_handler, IRQF_TRIGGER_HIGH,
  297. dev_name(&pdev->dev), &pdev->dev);
  298. if (ret) {
  299. dev_err(&pdev->dev,
  300. "Unable to request interrupt for device (err=%d).\n",
  301. ret);
  302. goto disable_clk;
  303. }
  304. dev_notice(&pdev->dev, "Tegra internal Real Time Clock\n");
  305. return 0;
  306. disable_clk:
  307. clk_disable_unprepare(info->clk);
  308. return ret;
  309. }
  310. static int tegra_rtc_remove(struct platform_device *pdev)
  311. {
  312. struct tegra_rtc_info *info = platform_get_drvdata(pdev);
  313. clk_disable_unprepare(info->clk);
  314. return 0;
  315. }
  316. #ifdef CONFIG_PM_SLEEP
  317. static int tegra_rtc_suspend(struct device *dev)
  318. {
  319. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  320. tegra_rtc_wait_while_busy(dev);
  321. /* only use ALARM0 as a wake source. */
  322. writel(0xffffffff, info->rtc_base + TEGRA_RTC_REG_INTR_STATUS);
  323. writel(TEGRA_RTC_INTR_STATUS_SEC_ALARM0,
  324. info->rtc_base + TEGRA_RTC_REG_INTR_MASK);
  325. dev_vdbg(dev, "alarm sec = %d\n",
  326. readl(info->rtc_base + TEGRA_RTC_REG_SECONDS_ALARM0));
  327. dev_vdbg(dev, "Suspend (device_may_wakeup=%d) irq:%d\n",
  328. device_may_wakeup(dev), info->tegra_rtc_irq);
  329. /* leave the alarms on as a wake source. */
  330. if (device_may_wakeup(dev))
  331. enable_irq_wake(info->tegra_rtc_irq);
  332. return 0;
  333. }
  334. static int tegra_rtc_resume(struct device *dev)
  335. {
  336. struct tegra_rtc_info *info = dev_get_drvdata(dev);
  337. dev_vdbg(dev, "Resume (device_may_wakeup=%d)\n",
  338. device_may_wakeup(dev));
  339. /* alarms were left on as a wake source, turn them off. */
  340. if (device_may_wakeup(dev))
  341. disable_irq_wake(info->tegra_rtc_irq);
  342. return 0;
  343. }
  344. #endif
  345. static SIMPLE_DEV_PM_OPS(tegra_rtc_pm_ops, tegra_rtc_suspend, tegra_rtc_resume);
  346. static void tegra_rtc_shutdown(struct platform_device *pdev)
  347. {
  348. dev_vdbg(&pdev->dev, "disabling interrupts.\n");
  349. tegra_rtc_alarm_irq_enable(&pdev->dev, 0);
  350. }
  351. MODULE_ALIAS("platform:tegra_rtc");
  352. static struct platform_driver tegra_rtc_driver = {
  353. .remove = tegra_rtc_remove,
  354. .shutdown = tegra_rtc_shutdown,
  355. .driver = {
  356. .name = "tegra_rtc",
  357. .of_match_table = tegra_rtc_dt_match,
  358. .pm = &tegra_rtc_pm_ops,
  359. },
  360. };
  361. module_platform_driver_probe(tegra_rtc_driver, tegra_rtc_probe);
  362. MODULE_AUTHOR("Jon Mayo <jmayo@nvidia.com>");
  363. MODULE_DESCRIPTION("driver for Tegra internal RTC");
  364. MODULE_LICENSE("GPL");