rtc-tegra.c 12 KB

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