timer-tegra186.c 12 KB

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