timer-tegra.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2010 Google, Inc.
  4. *
  5. * Author:
  6. * Colin Cross <ccross@google.com>
  7. */
  8. #define pr_fmt(fmt) "tegra-timer: " fmt
  9. #include <linux/clk.h>
  10. #include <linux/clockchips.h>
  11. #include <linux/cpu.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/percpu.h>
  19. #include <linux/sched_clock.h>
  20. #include <linux/time.h>
  21. #include "timer-of.h"
  22. #define RTC_SECONDS 0x08
  23. #define RTC_SHADOW_SECONDS 0x0c
  24. #define RTC_MILLISECONDS 0x10
  25. #define TIMERUS_CNTR_1US 0x10
  26. #define TIMERUS_USEC_CFG 0x14
  27. #define TIMERUS_CNTR_FREEZE 0x4c
  28. #define TIMER_PTV 0x0
  29. #define TIMER_PTV_EN BIT(31)
  30. #define TIMER_PTV_PER BIT(30)
  31. #define TIMER_PCR 0x4
  32. #define TIMER_PCR_INTR_CLR BIT(30)
  33. #define TIMER1_BASE 0x00
  34. #define TIMER2_BASE 0x08
  35. #define TIMER3_BASE 0x50
  36. #define TIMER4_BASE 0x58
  37. #define TIMER10_BASE 0x90
  38. #define TIMER1_IRQ_IDX 0
  39. #define TIMER10_IRQ_IDX 10
  40. #define TIMER_1MHz 1000000
  41. static u32 usec_config;
  42. static void __iomem *timer_reg_base;
  43. static int tegra_timer_set_next_event(unsigned long cycles,
  44. struct clock_event_device *evt)
  45. {
  46. void __iomem *reg_base = timer_of_base(to_timer_of(evt));
  47. /*
  48. * Tegra's timer uses n+1 scheme for the counter, i.e. timer will
  49. * fire after one tick if 0 is loaded.
  50. *
  51. * The minimum and maximum numbers of oneshot ticks are defined
  52. * by clockevents_config_and_register(1, 0x1fffffff + 1) invocation
  53. * below in the code. Hence the cycles (ticks) can't be outside of
  54. * a range supportable by hardware.
  55. */
  56. writel_relaxed(TIMER_PTV_EN | (cycles - 1), reg_base + TIMER_PTV);
  57. return 0;
  58. }
  59. static int tegra_timer_shutdown(struct clock_event_device *evt)
  60. {
  61. void __iomem *reg_base = timer_of_base(to_timer_of(evt));
  62. writel_relaxed(0, reg_base + TIMER_PTV);
  63. return 0;
  64. }
  65. static int tegra_timer_set_periodic(struct clock_event_device *evt)
  66. {
  67. void __iomem *reg_base = timer_of_base(to_timer_of(evt));
  68. unsigned long period = timer_of_period(to_timer_of(evt));
  69. writel_relaxed(TIMER_PTV_EN | TIMER_PTV_PER | (period - 1),
  70. reg_base + TIMER_PTV);
  71. return 0;
  72. }
  73. static irqreturn_t tegra_timer_isr(int irq, void *dev_id)
  74. {
  75. struct clock_event_device *evt = dev_id;
  76. void __iomem *reg_base = timer_of_base(to_timer_of(evt));
  77. writel_relaxed(TIMER_PCR_INTR_CLR, reg_base + TIMER_PCR);
  78. evt->event_handler(evt);
  79. return IRQ_HANDLED;
  80. }
  81. static void tegra_timer_suspend(struct clock_event_device *evt)
  82. {
  83. void __iomem *reg_base = timer_of_base(to_timer_of(evt));
  84. writel_relaxed(TIMER_PCR_INTR_CLR, reg_base + TIMER_PCR);
  85. }
  86. static void tegra_timer_resume(struct clock_event_device *evt)
  87. {
  88. writel_relaxed(usec_config, timer_reg_base + TIMERUS_USEC_CFG);
  89. }
  90. static DEFINE_PER_CPU(struct timer_of, tegra_to) = {
  91. .flags = TIMER_OF_CLOCK | TIMER_OF_BASE,
  92. .clkevt = {
  93. .name = "tegra_timer",
  94. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  95. .set_next_event = tegra_timer_set_next_event,
  96. .set_state_shutdown = tegra_timer_shutdown,
  97. .set_state_periodic = tegra_timer_set_periodic,
  98. .set_state_oneshot = tegra_timer_shutdown,
  99. .tick_resume = tegra_timer_shutdown,
  100. .suspend = tegra_timer_suspend,
  101. .resume = tegra_timer_resume,
  102. },
  103. };
  104. static int tegra_timer_setup(unsigned int cpu)
  105. {
  106. struct timer_of *to = per_cpu_ptr(&tegra_to, cpu);
  107. writel_relaxed(0, timer_of_base(to) + TIMER_PTV);
  108. writel_relaxed(TIMER_PCR_INTR_CLR, timer_of_base(to) + TIMER_PCR);
  109. irq_force_affinity(to->clkevt.irq, cpumask_of(cpu));
  110. enable_irq(to->clkevt.irq);
  111. /*
  112. * Tegra's timer uses n+1 scheme for the counter, i.e. timer will
  113. * fire after one tick if 0 is loaded and thus minimum number of
  114. * ticks is 1. In result both of the clocksource's tick limits are
  115. * higher than a minimum and maximum that hardware register can
  116. * take by 1, this is then taken into account by set_next_event
  117. * callback.
  118. */
  119. clockevents_config_and_register(&to->clkevt, timer_of_rate(to),
  120. 1, /* min */
  121. 0x1fffffff + 1); /* max 29 bits + 1 */
  122. return 0;
  123. }
  124. static int tegra_timer_stop(unsigned int cpu)
  125. {
  126. struct timer_of *to = per_cpu_ptr(&tegra_to, cpu);
  127. disable_irq_nosync(to->clkevt.irq);
  128. return 0;
  129. }
  130. static u64 notrace tegra_read_sched_clock(void)
  131. {
  132. return readl_relaxed(timer_reg_base + TIMERUS_CNTR_1US);
  133. }
  134. #ifdef CONFIG_ARM
  135. static unsigned long tegra_delay_timer_read_counter_long(void)
  136. {
  137. return readl_relaxed(timer_reg_base + TIMERUS_CNTR_1US);
  138. }
  139. static struct delay_timer tegra_delay_timer = {
  140. .read_current_timer = tegra_delay_timer_read_counter_long,
  141. .freq = TIMER_1MHz,
  142. };
  143. #endif
  144. static struct timer_of suspend_rtc_to = {
  145. .flags = TIMER_OF_BASE | TIMER_OF_CLOCK,
  146. };
  147. /*
  148. * tegra_rtc_read - Reads the Tegra RTC registers
  149. * Care must be taken that this function is not called while the
  150. * tegra_rtc driver could be executing to avoid race conditions
  151. * on the RTC shadow register
  152. */
  153. static u64 tegra_rtc_read_ms(struct clocksource *cs)
  154. {
  155. void __iomem *reg_base = timer_of_base(&suspend_rtc_to);
  156. u32 ms = readl_relaxed(reg_base + RTC_MILLISECONDS);
  157. u32 s = readl_relaxed(reg_base + RTC_SHADOW_SECONDS);
  158. return (u64)s * MSEC_PER_SEC + ms;
  159. }
  160. static struct clocksource suspend_rtc_clocksource = {
  161. .name = "tegra_suspend_timer",
  162. .rating = 200,
  163. .read = tegra_rtc_read_ms,
  164. .mask = CLOCKSOURCE_MASK(32),
  165. .flags = CLOCK_SOURCE_IS_CONTINUOUS | CLOCK_SOURCE_SUSPEND_NONSTOP,
  166. };
  167. static inline unsigned int tegra_base_for_cpu(int cpu, bool tegra20)
  168. {
  169. if (tegra20) {
  170. switch (cpu) {
  171. case 0:
  172. return TIMER1_BASE;
  173. case 1:
  174. return TIMER2_BASE;
  175. case 2:
  176. return TIMER3_BASE;
  177. default:
  178. return TIMER4_BASE;
  179. }
  180. }
  181. return TIMER10_BASE + cpu * 8;
  182. }
  183. static inline unsigned int tegra_irq_idx_for_cpu(int cpu, bool tegra20)
  184. {
  185. if (tegra20)
  186. return TIMER1_IRQ_IDX + cpu;
  187. return TIMER10_IRQ_IDX + cpu;
  188. }
  189. static inline unsigned long tegra_rate_for_timer(struct timer_of *to,
  190. bool tegra20)
  191. {
  192. /*
  193. * TIMER1-9 are fixed to 1MHz, TIMER10-13 are running off the
  194. * parent clock.
  195. */
  196. if (tegra20)
  197. return TIMER_1MHz;
  198. return timer_of_rate(to);
  199. }
  200. static int __init tegra_init_timer(struct device_node *np, bool tegra20,
  201. int rating)
  202. {
  203. struct timer_of *to;
  204. int cpu, ret;
  205. to = this_cpu_ptr(&tegra_to);
  206. ret = timer_of_init(np, to);
  207. if (ret)
  208. goto out;
  209. timer_reg_base = timer_of_base(to);
  210. /*
  211. * Configure microsecond timers to have 1MHz clock
  212. * Config register is 0xqqww, where qq is "dividend", ww is "divisor"
  213. * Uses n+1 scheme
  214. */
  215. switch (timer_of_rate(to)) {
  216. case 12000000:
  217. usec_config = 0x000b; /* (11+1)/(0+1) */
  218. break;
  219. case 12800000:
  220. usec_config = 0x043f; /* (63+1)/(4+1) */
  221. break;
  222. case 13000000:
  223. usec_config = 0x000c; /* (12+1)/(0+1) */
  224. break;
  225. case 16800000:
  226. usec_config = 0x0453; /* (83+1)/(4+1) */
  227. break;
  228. case 19200000:
  229. usec_config = 0x045f; /* (95+1)/(4+1) */
  230. break;
  231. case 26000000:
  232. usec_config = 0x0019; /* (25+1)/(0+1) */
  233. break;
  234. case 38400000:
  235. usec_config = 0x04bf; /* (191+1)/(4+1) */
  236. break;
  237. case 48000000:
  238. usec_config = 0x002f; /* (47+1)/(0+1) */
  239. break;
  240. default:
  241. ret = -EINVAL;
  242. goto out;
  243. }
  244. writel_relaxed(usec_config, timer_reg_base + TIMERUS_USEC_CFG);
  245. for_each_possible_cpu(cpu) {
  246. struct timer_of *cpu_to = per_cpu_ptr(&tegra_to, cpu);
  247. unsigned long flags = IRQF_TIMER | IRQF_NOBALANCING;
  248. unsigned long rate = tegra_rate_for_timer(to, tegra20);
  249. unsigned int base = tegra_base_for_cpu(cpu, tegra20);
  250. unsigned int idx = tegra_irq_idx_for_cpu(cpu, tegra20);
  251. unsigned int irq = irq_of_parse_and_map(np, idx);
  252. if (!irq) {
  253. pr_err("failed to map irq for cpu%d\n", cpu);
  254. ret = -EINVAL;
  255. goto out_irq;
  256. }
  257. cpu_to->clkevt.irq = irq;
  258. cpu_to->clkevt.rating = rating;
  259. cpu_to->clkevt.cpumask = cpumask_of(cpu);
  260. cpu_to->of_base.base = timer_reg_base + base;
  261. cpu_to->of_clk.period = rate / HZ;
  262. cpu_to->of_clk.rate = rate;
  263. irq_set_status_flags(cpu_to->clkevt.irq, IRQ_NOAUTOEN);
  264. ret = request_irq(cpu_to->clkevt.irq, tegra_timer_isr, flags,
  265. cpu_to->clkevt.name, &cpu_to->clkevt);
  266. if (ret) {
  267. pr_err("failed to set up irq for cpu%d: %d\n",
  268. cpu, ret);
  269. irq_dispose_mapping(cpu_to->clkevt.irq);
  270. cpu_to->clkevt.irq = 0;
  271. goto out_irq;
  272. }
  273. }
  274. sched_clock_register(tegra_read_sched_clock, 32, TIMER_1MHz);
  275. ret = clocksource_mmio_init(timer_reg_base + TIMERUS_CNTR_1US,
  276. "timer_us", TIMER_1MHz, 300, 32,
  277. clocksource_mmio_readl_up);
  278. if (ret)
  279. pr_err("failed to register clocksource: %d\n", ret);
  280. #ifdef CONFIG_ARM
  281. register_current_timer_delay(&tegra_delay_timer);
  282. #endif
  283. ret = cpuhp_setup_state(CPUHP_AP_TEGRA_TIMER_STARTING,
  284. "AP_TEGRA_TIMER_STARTING", tegra_timer_setup,
  285. tegra_timer_stop);
  286. if (ret)
  287. pr_err("failed to set up cpu hp state: %d\n", ret);
  288. return ret;
  289. out_irq:
  290. for_each_possible_cpu(cpu) {
  291. struct timer_of *cpu_to;
  292. cpu_to = per_cpu_ptr(&tegra_to, cpu);
  293. if (cpu_to->clkevt.irq) {
  294. free_irq(cpu_to->clkevt.irq, &cpu_to->clkevt);
  295. irq_dispose_mapping(cpu_to->clkevt.irq);
  296. }
  297. }
  298. to->of_base.base = timer_reg_base;
  299. out:
  300. timer_of_cleanup(to);
  301. return ret;
  302. }
  303. static int __init tegra210_init_timer(struct device_node *np)
  304. {
  305. /*
  306. * Arch-timer can't survive across power cycle of CPU core and
  307. * after CPUPORESET signal due to a system design shortcoming,
  308. * hence tegra-timer is more preferable on Tegra210.
  309. */
  310. return tegra_init_timer(np, false, 460);
  311. }
  312. TIMER_OF_DECLARE(tegra210_timer, "nvidia,tegra210-timer", tegra210_init_timer);
  313. static int __init tegra20_init_timer(struct device_node *np)
  314. {
  315. int rating;
  316. /*
  317. * Tegra20 and Tegra30 have Cortex A9 CPU that has a TWD timer,
  318. * that timer runs off the CPU clock and hence is subjected to
  319. * a jitter caused by DVFS clock rate changes. Tegra-timer is
  320. * more preferable for older Tegra's, while later SoC generations
  321. * have arch-timer as a main per-CPU timer and it is not affected
  322. * by DVFS changes.
  323. */
  324. if (of_machine_is_compatible("nvidia,tegra20") ||
  325. of_machine_is_compatible("nvidia,tegra30"))
  326. rating = 460;
  327. else
  328. rating = 330;
  329. return tegra_init_timer(np, true, rating);
  330. }
  331. TIMER_OF_DECLARE(tegra20_timer, "nvidia,tegra20-timer", tegra20_init_timer);
  332. static int __init tegra20_init_rtc(struct device_node *np)
  333. {
  334. int ret;
  335. ret = timer_of_init(np, &suspend_rtc_to);
  336. if (ret)
  337. return ret;
  338. return clocksource_register_hz(&suspend_rtc_clocksource, 1000);
  339. }
  340. TIMER_OF_DECLARE(tegra20_rtc, "nvidia,tegra20-rtc", tegra20_init_rtc);