timer-tegra186.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2019-2020 NVIDIA Corporation. All rights reserved.
  4. */
  5. #include <linux/clocksource.h>
  6. #include <linux/module.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/io.h>
  9. #include <linux/of.h>
  10. #include <linux/of_device.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm.h>
  13. #include <linux/watchdog.h>
  14. /* shared registers */
  15. #define TKETSC0 0x000
  16. #define TKETSC1 0x004
  17. #define TKEUSEC 0x008
  18. #define TKEOSC 0x00c
  19. #define TKEIE(x) (0x100 + ((x) * 4))
  20. #define TKEIE_WDT_MASK(x, y) ((y) << (16 + 4 * (x)))
  21. /* timer registers */
  22. #define TMRCR 0x000
  23. #define TMRCR_ENABLE BIT(31)
  24. #define TMRCR_PERIODIC BIT(30)
  25. #define TMRCR_PTV(x) ((x) & 0x0fffffff)
  26. #define TMRSR 0x004
  27. #define TMRSR_INTR_CLR BIT(30)
  28. #define TMRCSSR 0x008
  29. #define TMRCSSR_SRC_USEC (0 << 0)
  30. /* watchdog registers */
  31. #define WDTCR 0x000
  32. #define WDTCR_SYSTEM_POR_RESET_ENABLE BIT(16)
  33. #define WDTCR_SYSTEM_DEBUG_RESET_ENABLE BIT(15)
  34. #define WDTCR_REMOTE_INT_ENABLE BIT(14)
  35. #define WDTCR_LOCAL_FIQ_ENABLE BIT(13)
  36. #define WDTCR_LOCAL_INT_ENABLE BIT(12)
  37. #define WDTCR_PERIOD_MASK (0xff << 4)
  38. #define WDTCR_PERIOD(x) (((x) & 0xff) << 4)
  39. #define WDTCR_TIMER_SOURCE_MASK 0xf
  40. #define WDTCR_TIMER_SOURCE(x) ((x) & 0xf)
  41. #define WDTCMDR 0x008
  42. #define WDTCMDR_DISABLE_COUNTER BIT(1)
  43. #define WDTCMDR_START_COUNTER BIT(0)
  44. #define WDTUR 0x00c
  45. #define WDTUR_UNLOCK_PATTERN 0x0000c45a
  46. struct tegra186_timer_soc {
  47. unsigned int num_timers;
  48. unsigned int num_wdts;
  49. };
  50. struct tegra186_tmr {
  51. struct tegra186_timer *parent;
  52. void __iomem *regs;
  53. unsigned int index;
  54. unsigned int hwirq;
  55. };
  56. struct tegra186_wdt {
  57. struct watchdog_device base;
  58. void __iomem *regs;
  59. unsigned int index;
  60. bool locked;
  61. struct tegra186_tmr *tmr;
  62. };
  63. static inline struct tegra186_wdt *to_tegra186_wdt(struct watchdog_device *wdd)
  64. {
  65. return container_of(wdd, struct tegra186_wdt, base);
  66. }
  67. struct tegra186_timer {
  68. const struct tegra186_timer_soc *soc;
  69. struct device *dev;
  70. void __iomem *regs;
  71. struct tegra186_wdt *wdt;
  72. struct clocksource usec;
  73. struct clocksource tsc;
  74. struct clocksource osc;
  75. };
  76. static void tmr_writel(struct tegra186_tmr *tmr, u32 value, unsigned int offset)
  77. {
  78. writel_relaxed(value, tmr->regs + offset);
  79. }
  80. static void wdt_writel(struct tegra186_wdt *wdt, u32 value, unsigned int offset)
  81. {
  82. writel_relaxed(value, wdt->regs + offset);
  83. }
  84. static u32 wdt_readl(struct tegra186_wdt *wdt, unsigned int offset)
  85. {
  86. return readl_relaxed(wdt->regs + offset);
  87. }
  88. static struct tegra186_tmr *tegra186_tmr_create(struct tegra186_timer *tegra,
  89. unsigned int index)
  90. {
  91. unsigned int offset = 0x10000 + index * 0x10000;
  92. struct tegra186_tmr *tmr;
  93. tmr = devm_kzalloc(tegra->dev, sizeof(*tmr), GFP_KERNEL);
  94. if (!tmr)
  95. return ERR_PTR(-ENOMEM);
  96. tmr->parent = tegra;
  97. tmr->regs = tegra->regs + offset;
  98. tmr->index = index;
  99. tmr->hwirq = 0;
  100. return tmr;
  101. }
  102. static const struct watchdog_info tegra186_wdt_info = {
  103. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  104. .identity = "NVIDIA Tegra186 WDT",
  105. };
  106. static void tegra186_wdt_disable(struct tegra186_wdt *wdt)
  107. {
  108. /* unlock and disable the watchdog */
  109. wdt_writel(wdt, WDTUR_UNLOCK_PATTERN, WDTUR);
  110. wdt_writel(wdt, WDTCMDR_DISABLE_COUNTER, WDTCMDR);
  111. /* disable timer */
  112. tmr_writel(wdt->tmr, 0, TMRCR);
  113. }
  114. static void tegra186_wdt_enable(struct tegra186_wdt *wdt)
  115. {
  116. struct tegra186_timer *tegra = wdt->tmr->parent;
  117. u32 value;
  118. /* unmask hardware IRQ, this may have been lost across powergate */
  119. value = TKEIE_WDT_MASK(wdt->index, 1);
  120. writel(value, tegra->regs + TKEIE(wdt->tmr->hwirq));
  121. /* clear interrupt */
  122. tmr_writel(wdt->tmr, TMRSR_INTR_CLR, TMRSR);
  123. /* select microsecond source */
  124. tmr_writel(wdt->tmr, TMRCSSR_SRC_USEC, TMRCSSR);
  125. /* configure timer (system reset happens on the fifth expiration) */
  126. value = TMRCR_PTV(wdt->base.timeout * USEC_PER_SEC / 5) |
  127. TMRCR_PERIODIC | TMRCR_ENABLE;
  128. tmr_writel(wdt->tmr, value, TMRCR);
  129. if (!wdt->locked) {
  130. value = wdt_readl(wdt, WDTCR);
  131. /* select the proper timer source */
  132. value &= ~WDTCR_TIMER_SOURCE_MASK;
  133. value |= WDTCR_TIMER_SOURCE(wdt->tmr->index);
  134. /* single timer period since that's already configured */
  135. value &= ~WDTCR_PERIOD_MASK;
  136. value |= WDTCR_PERIOD(1);
  137. /* enable local interrupt for WDT petting */
  138. value |= WDTCR_LOCAL_INT_ENABLE;
  139. /* enable local FIQ and remote interrupt for debug dump */
  140. if (0)
  141. value |= WDTCR_REMOTE_INT_ENABLE |
  142. WDTCR_LOCAL_FIQ_ENABLE;
  143. /* enable system debug reset (doesn't properly reboot) */
  144. if (0)
  145. value |= WDTCR_SYSTEM_DEBUG_RESET_ENABLE;
  146. /* enable system POR reset */
  147. value |= WDTCR_SYSTEM_POR_RESET_ENABLE;
  148. wdt_writel(wdt, value, WDTCR);
  149. }
  150. wdt_writel(wdt, WDTCMDR_START_COUNTER, WDTCMDR);
  151. }
  152. static int tegra186_wdt_start(struct watchdog_device *wdd)
  153. {
  154. struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
  155. tegra186_wdt_enable(wdt);
  156. return 0;
  157. }
  158. static int tegra186_wdt_stop(struct watchdog_device *wdd)
  159. {
  160. struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
  161. tegra186_wdt_disable(wdt);
  162. return 0;
  163. }
  164. static int tegra186_wdt_ping(struct watchdog_device *wdd)
  165. {
  166. struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
  167. tegra186_wdt_disable(wdt);
  168. tegra186_wdt_enable(wdt);
  169. return 0;
  170. }
  171. static int tegra186_wdt_set_timeout(struct watchdog_device *wdd,
  172. unsigned int timeout)
  173. {
  174. struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
  175. if (watchdog_active(&wdt->base))
  176. tegra186_wdt_disable(wdt);
  177. wdt->base.timeout = timeout;
  178. if (watchdog_active(&wdt->base))
  179. tegra186_wdt_enable(wdt);
  180. return 0;
  181. }
  182. static const struct watchdog_ops tegra186_wdt_ops = {
  183. .owner = THIS_MODULE,
  184. .start = tegra186_wdt_start,
  185. .stop = tegra186_wdt_stop,
  186. .ping = tegra186_wdt_ping,
  187. .set_timeout = tegra186_wdt_set_timeout,
  188. };
  189. static struct tegra186_wdt *tegra186_wdt_create(struct tegra186_timer *tegra,
  190. unsigned int index)
  191. {
  192. unsigned int offset = 0x10000, source;
  193. struct tegra186_wdt *wdt;
  194. u32 value;
  195. int err;
  196. offset += tegra->soc->num_timers * 0x10000 + index * 0x10000;
  197. wdt = devm_kzalloc(tegra->dev, sizeof(*wdt), GFP_KERNEL);
  198. if (!wdt)
  199. return ERR_PTR(-ENOMEM);
  200. wdt->regs = tegra->regs + offset;
  201. wdt->index = index;
  202. /* read the watchdog configuration since it might be locked down */
  203. value = wdt_readl(wdt, WDTCR);
  204. if (value & WDTCR_LOCAL_INT_ENABLE)
  205. wdt->locked = true;
  206. source = value & WDTCR_TIMER_SOURCE_MASK;
  207. wdt->tmr = tegra186_tmr_create(tegra, source);
  208. if (IS_ERR(wdt->tmr))
  209. return ERR_CAST(wdt->tmr);
  210. wdt->base.info = &tegra186_wdt_info;
  211. wdt->base.ops = &tegra186_wdt_ops;
  212. wdt->base.min_timeout = 1;
  213. wdt->base.max_timeout = 255;
  214. wdt->base.parent = tegra->dev;
  215. err = watchdog_init_timeout(&wdt->base, 5, tegra->dev);
  216. if (err < 0) {
  217. dev_err(tegra->dev, "failed to initialize timeout: %d\n", err);
  218. return ERR_PTR(err);
  219. }
  220. err = devm_watchdog_register_device(tegra->dev, &wdt->base);
  221. if (err < 0) {
  222. dev_err(tegra->dev, "failed to register WDT: %d\n", err);
  223. return ERR_PTR(err);
  224. }
  225. return wdt;
  226. }
  227. static u64 tegra186_timer_tsc_read(struct clocksource *cs)
  228. {
  229. struct tegra186_timer *tegra = container_of(cs, struct tegra186_timer,
  230. tsc);
  231. u32 hi, lo, ss;
  232. hi = readl_relaxed(tegra->regs + TKETSC1);
  233. /*
  234. * The 56-bit value of the TSC is spread across two registers that are
  235. * not synchronized. In order to read them atomically, ensure that the
  236. * high 24 bits match before and after reading the low 32 bits.
  237. */
  238. do {
  239. /* snapshot the high 24 bits */
  240. ss = hi;
  241. lo = readl_relaxed(tegra->regs + TKETSC0);
  242. hi = readl_relaxed(tegra->regs + TKETSC1);
  243. } while (hi != ss);
  244. return (u64)hi << 32 | lo;
  245. }
  246. static int tegra186_timer_tsc_init(struct tegra186_timer *tegra)
  247. {
  248. tegra->tsc.name = "tsc";
  249. tegra->tsc.rating = 300;
  250. tegra->tsc.read = tegra186_timer_tsc_read;
  251. tegra->tsc.mask = CLOCKSOURCE_MASK(56);
  252. tegra->tsc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  253. return clocksource_register_hz(&tegra->tsc, 31250000);
  254. }
  255. static u64 tegra186_timer_osc_read(struct clocksource *cs)
  256. {
  257. struct tegra186_timer *tegra = container_of(cs, struct tegra186_timer,
  258. osc);
  259. return readl_relaxed(tegra->regs + TKEOSC);
  260. }
  261. static int tegra186_timer_osc_init(struct tegra186_timer *tegra)
  262. {
  263. tegra->osc.name = "osc";
  264. tegra->osc.rating = 300;
  265. tegra->osc.read = tegra186_timer_osc_read;
  266. tegra->osc.mask = CLOCKSOURCE_MASK(32);
  267. tegra->osc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  268. return clocksource_register_hz(&tegra->osc, 38400000);
  269. }
  270. static u64 tegra186_timer_usec_read(struct clocksource *cs)
  271. {
  272. struct tegra186_timer *tegra = container_of(cs, struct tegra186_timer,
  273. usec);
  274. return readl_relaxed(tegra->regs + TKEUSEC);
  275. }
  276. static int tegra186_timer_usec_init(struct tegra186_timer *tegra)
  277. {
  278. tegra->usec.name = "usec";
  279. tegra->usec.rating = 300;
  280. tegra->usec.read = tegra186_timer_usec_read;
  281. tegra->usec.mask = CLOCKSOURCE_MASK(32);
  282. tegra->usec.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  283. return clocksource_register_hz(&tegra->usec, USEC_PER_SEC);
  284. }
  285. static irqreturn_t tegra186_timer_irq(int irq, void *data)
  286. {
  287. struct tegra186_timer *tegra = data;
  288. if (watchdog_active(&tegra->wdt->base)) {
  289. tegra186_wdt_disable(tegra->wdt);
  290. tegra186_wdt_enable(tegra->wdt);
  291. }
  292. return IRQ_HANDLED;
  293. }
  294. static int tegra186_timer_probe(struct platform_device *pdev)
  295. {
  296. struct device *dev = &pdev->dev;
  297. struct tegra186_timer *tegra;
  298. unsigned int irq;
  299. int err;
  300. tegra = devm_kzalloc(dev, sizeof(*tegra), GFP_KERNEL);
  301. if (!tegra)
  302. return -ENOMEM;
  303. tegra->soc = of_device_get_match_data(dev);
  304. dev_set_drvdata(dev, tegra);
  305. tegra->dev = dev;
  306. tegra->regs = devm_platform_ioremap_resource(pdev, 0);
  307. if (IS_ERR(tegra->regs))
  308. return PTR_ERR(tegra->regs);
  309. err = platform_get_irq(pdev, 0);
  310. if (err < 0)
  311. return err;
  312. irq = err;
  313. /* create a watchdog using a preconfigured timer */
  314. tegra->wdt = tegra186_wdt_create(tegra, 0);
  315. if (IS_ERR(tegra->wdt)) {
  316. err = PTR_ERR(tegra->wdt);
  317. dev_err(dev, "failed to create WDT: %d\n", err);
  318. return err;
  319. }
  320. err = tegra186_timer_tsc_init(tegra);
  321. if (err < 0) {
  322. dev_err(dev, "failed to register TSC counter: %d\n", err);
  323. return err;
  324. }
  325. err = tegra186_timer_osc_init(tegra);
  326. if (err < 0) {
  327. dev_err(dev, "failed to register OSC counter: %d\n", err);
  328. goto unregister_tsc;
  329. }
  330. err = tegra186_timer_usec_init(tegra);
  331. if (err < 0) {
  332. dev_err(dev, "failed to register USEC counter: %d\n", err);
  333. goto unregister_osc;
  334. }
  335. err = devm_request_irq(dev, irq, tegra186_timer_irq, 0,
  336. "tegra186-timer", tegra);
  337. if (err < 0) {
  338. dev_err(dev, "failed to request IRQ#%u: %d\n", irq, err);
  339. goto unregister_usec;
  340. }
  341. return 0;
  342. unregister_usec:
  343. clocksource_unregister(&tegra->usec);
  344. unregister_osc:
  345. clocksource_unregister(&tegra->osc);
  346. unregister_tsc:
  347. clocksource_unregister(&tegra->tsc);
  348. return err;
  349. }
  350. static int tegra186_timer_remove(struct platform_device *pdev)
  351. {
  352. struct tegra186_timer *tegra = platform_get_drvdata(pdev);
  353. clocksource_unregister(&tegra->usec);
  354. clocksource_unregister(&tegra->osc);
  355. clocksource_unregister(&tegra->tsc);
  356. return 0;
  357. }
  358. static int __maybe_unused tegra186_timer_suspend(struct device *dev)
  359. {
  360. struct tegra186_timer *tegra = dev_get_drvdata(dev);
  361. if (watchdog_active(&tegra->wdt->base))
  362. tegra186_wdt_disable(tegra->wdt);
  363. return 0;
  364. }
  365. static int __maybe_unused tegra186_timer_resume(struct device *dev)
  366. {
  367. struct tegra186_timer *tegra = dev_get_drvdata(dev);
  368. if (watchdog_active(&tegra->wdt->base))
  369. tegra186_wdt_enable(tegra->wdt);
  370. return 0;
  371. }
  372. static SIMPLE_DEV_PM_OPS(tegra186_timer_pm_ops, tegra186_timer_suspend,
  373. tegra186_timer_resume);
  374. static const struct tegra186_timer_soc tegra186_timer = {
  375. .num_timers = 10,
  376. .num_wdts = 3,
  377. };
  378. static const struct tegra186_timer_soc tegra234_timer = {
  379. .num_timers = 16,
  380. .num_wdts = 3,
  381. };
  382. static const struct of_device_id tegra186_timer_of_match[] = {
  383. { .compatible = "nvidia,tegra186-timer", .data = &tegra186_timer },
  384. { .compatible = "nvidia,tegra234-timer", .data = &tegra234_timer },
  385. { }
  386. };
  387. MODULE_DEVICE_TABLE(of, tegra186_timer_of_match);
  388. static struct platform_driver tegra186_wdt_driver = {
  389. .driver = {
  390. .name = "tegra186-timer",
  391. .pm = &tegra186_timer_pm_ops,
  392. .of_match_table = tegra186_timer_of_match,
  393. },
  394. .probe = tegra186_timer_probe,
  395. .remove = tegra186_timer_remove,
  396. };
  397. module_platform_driver(tegra186_wdt_driver);
  398. MODULE_AUTHOR("Thierry Reding <[email protected]>");
  399. MODULE_DESCRIPTION("NVIDIA Tegra186 timers driver");
  400. MODULE_LICENSE("GPL v2");