1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * RZ/N1 timers driver 4 * 5 * Copyright (C) 2026 Schneider-Electric 6 * 7 * Author: Herve Codina <herve.codina@bootlin.com> 8 */ 9 10 #include <linux/bits.h> 11 #include <linux/clk.h> 12 #include <linux/clockchips.h> 13 #include <linux/clocksource.h> 14 #include <linux/cpuhotplug.h> 15 #include <linux/interrupt.h> 16 #include <linux/of.h> 17 #include <linux/platform_device.h> 18 #include <linux/sched_clock.h> 19 20 /* Per-timer register offsets */ 21 #define RZN1_TIMER_LOAD_COUNT_REG 0x00 22 #define RZN1_TIMER_CURRENT_COUNT_REG 0x04 23 #define RZN1_TIMER_CONTROL_REG 0x08 24 #define RZN1_TIMER_CONTROL_INT_UNMASK BIT(3) 25 #define RZN1_TIMER_CONTROL_ENABLE BIT(2) 26 #define RZN1_TIMER_CONTROL_AUTORELOAD BIT(1) 27 #define RZN1_TIMER_CONTROL_PRESCALER_DIV25 BIT(0) 28 #define RZN1_TIMER_CLEAR_INT_REG 0x0c 29 #define RZN1_TIMER_STATUS_INT0_REG 0x10 30 #define RZN1_TIMER_STATUS_INT1_REG 0x14 31 32 /* 33 * 8 timers are available. Among those 8 timers, the first 6 timers are 16-bit 34 * timers and the last two ones are 32-bit timers. 35 */ 36 #define RZN1_TIMER_BASE_INDEX_16BIT_TIMERS 0 37 #define RZN1_TIMER_NB_16BIT_TIMERS 6 38 #define RZN1_TIMER_LAST_INDEX_16BIT_TIMERS (RZN1_TIMER_BASE_INDEX_16BIT_TIMERS + \ 39 RZN1_TIMER_NB_16BIT_TIMERS - 1) 40 41 #define RZN1_TIMER_BASE_INDEX_32BIT_TIMERS 6 42 #define RZN1_TIMER_NB_32BIT_TIMERS 2 43 #define RZN1_TIMER_LAST_INDEX_32BIT_TIMERS (RZN1_TIMER_BASE_INDEX_32BIT_TIMERS + \ 44 RZN1_TIMER_NB_32BIT_TIMERS - 1) 45 46 #define RZN1_TIMER_IS_16BIT_TIMER(_i) ((_i) < RZN1_TIMER_BASE_INDEX_32BIT_TIMERS) 47 48 /* Total numbers of timers */ 49 #define RZN1_TIMER_NB_TIMERS (RZN1_TIMER_NB_16BIT_TIMERS + \ 50 RZN1_TIMER_NB_32BIT_TIMERS) 51 52 /* Offset of registers related to the timer number _i in the timers regs area */ 53 #define RZN1_TIMER_REGS_OFFSET(_i) (0x20 * (_i)) 54 55 struct rzn1_timer { 56 void __iomem *base; 57 unsigned int width; 58 unsigned long rate; 59 unsigned long hz_period; 60 struct clock_event_device ced; 61 }; 62 63 static int rzn1_timer_config(struct rzn1_timer *timer, u32 period, bool is_periodic) 64 { 65 u32 ctrl; 66 67 ctrl = readl(timer->base + RZN1_TIMER_CONTROL_REG); 68 69 /* Disable the timer */ 70 writel(ctrl & ~RZN1_TIMER_CONTROL_ENABLE, 71 timer->base + RZN1_TIMER_CONTROL_REG); 72 73 /* Set the new period */ 74 writel(period, timer->base + RZN1_TIMER_LOAD_COUNT_REG); 75 76 /* Prepare the timer mode */ 77 if (is_periodic) 78 ctrl |= RZN1_TIMER_CONTROL_AUTORELOAD; 79 else 80 ctrl &= ~RZN1_TIMER_CONTROL_AUTORELOAD; 81 82 /* 83 * Enable the timer. 84 * 85 * This automatically resets the CURRENT_COUNT register. 86 */ 87 ctrl |= RZN1_TIMER_CONTROL_ENABLE; 88 writel(ctrl, timer->base + RZN1_TIMER_CONTROL_REG); 89 90 return 0; 91 } 92 93 static void rzn1_timer_disable(struct rzn1_timer *timer) 94 { 95 u32 ctrl; 96 97 ctrl = readl(timer->base + RZN1_TIMER_CONTROL_REG); 98 ctrl &= ~RZN1_TIMER_CONTROL_ENABLE; 99 writel(ctrl, timer->base + RZN1_TIMER_CONTROL_REG); 100 } 101 102 static void rzn1_timer_int_enable(struct rzn1_timer *timer) 103 { 104 u32 ctrl; 105 106 ctrl = readl(timer->base + RZN1_TIMER_CONTROL_REG); 107 ctrl |= RZN1_TIMER_CONTROL_INT_UNMASK; 108 writel(ctrl, timer->base + RZN1_TIMER_CONTROL_REG); 109 } 110 111 static irqreturn_t rzn1_timer_interrupt(int irq, void *dev_id) 112 { 113 struct rzn1_timer *timer = dev_id; 114 115 /* Ack the interrupt */ 116 readl(timer->base + RZN1_TIMER_CLEAR_INT_REG); 117 118 if (timer->ced.event_handler) 119 timer->ced.event_handler(&timer->ced); 120 121 return IRQ_HANDLED; 122 } 123 124 static int rzn1_clkevt_set_next_event(unsigned long cycles, 125 struct clock_event_device *evt) 126 { 127 struct rzn1_timer *timer = container_of(evt, struct rzn1_timer, ced); 128 129 return rzn1_timer_config(timer, cycles, false); 130 } 131 132 static int rzn1_clkevt_shutdown(struct clock_event_device *evt) 133 { 134 struct rzn1_timer *timer = container_of(evt, struct rzn1_timer, ced); 135 136 rzn1_timer_disable(timer); 137 return 0; 138 } 139 140 static int rzn1_clkevt_set_periodic(struct clock_event_device *evt) 141 { 142 struct rzn1_timer *timer = container_of(evt, struct rzn1_timer, ced); 143 144 rzn1_timer_config(timer, timer->hz_period, true); 145 return 0; 146 } 147 148 /* 149 * This global lock is used to prevent race conditions during global timers 150 * test and set operation in case the driver is using the async probe option. 151 */ 152 static DEFINE_MUTEX(rzn1_global_timers_lock); 153 154 static int rzn1_timer_instance; 155 156 /* scheduler and clocksource */ 157 static struct rzn1_timer *rzn1_sched_clock; 158 159 static u64 notrace rzn1_sched_read(void) 160 { 161 return readl_relaxed(rzn1_sched_clock->base + RZN1_TIMER_CURRENT_COUNT_REG); 162 } 163 164 static u64 rzn1_clksrc_read(struct clocksource *cs) 165 { 166 return readl_relaxed(rzn1_sched_clock->base + RZN1_TIMER_CURRENT_COUNT_REG); 167 } 168 169 static struct clocksource rzn1_clocksource = { 170 .name = "rzn1_timer_clocksource", 171 .rating = 200, 172 .flags = CLOCK_SOURCE_IS_CONTINUOUS, 173 .read = rzn1_clksrc_read, 174 }; 175 176 /* Timers table where 16-bit timers will be assigned per-cpu */ 177 static struct rzn1_timer *rzn1_tab_timers; 178 179 static int rzn1_local_timer_starting_cpu(unsigned int cpu) 180 { 181 struct rzn1_timer *timer; 182 183 /* We assign the first and second 16-bit timer as per-cpu timers. */ 184 if (cpu >= 2) 185 return 0; 186 187 timer = &rzn1_tab_timers[cpu + RZN1_TIMER_BASE_INDEX_16BIT_TIMERS]; 188 189 timer->ced.cpumask = cpumask_of(cpu); 190 irq_force_affinity(timer->ced.irq, cpumask_of(cpu)); 191 192 clockevents_config_and_register(&timer->ced, timer->rate, 1, 193 GENMASK(timer->width - 1, 0)); 194 /* 195 * Time to enable the interrupt even if no interrupt can be fired yet. 196 * Indeed, the timer is still globally disabled. 197 * It will be enabled when rzn1_timer_config() is called 198 */ 199 enable_irq(timer->ced.irq); 200 return 0; 201 } 202 203 static int rzn1_local_timer_dying_cpu(unsigned int cpu) 204 { 205 struct rzn1_timer *timer; 206 207 if (cpu >= 2) 208 return 0; 209 210 timer = &rzn1_tab_timers[cpu + RZN1_TIMER_BASE_INDEX_16BIT_TIMERS]; 211 212 rzn1_timer_disable(timer); 213 disable_irq_nosync(timer->ced.irq); 214 return 0; 215 } 216 217 static void rzn1_timer_init(struct rzn1_timer *timer, unsigned int index, 218 void __iomem *timers_base, unsigned long clock_rate) 219 { 220 timer->base = timers_base + RZN1_TIMER_REGS_OFFSET(index); 221 222 /* Disable the timer */ 223 writel(0, timer->base + RZN1_TIMER_CONTROL_REG); 224 225 /* Clear potential interrupts */ 226 readl(timer->base + RZN1_TIMER_CLEAR_INT_REG); 227 228 if (RZN1_TIMER_IS_16BIT_TIMER(index)) { 229 timer->width = 16; 230 /* 231 * Enable pre-scaler for 16-bit timers. 232 * 233 * Timers are fed by a 25MHz clock. With this pre-scaler (1/25) 234 * enabled, the 16-bit timers resolution is 1 microsecond. 235 */ 236 writel(RZN1_TIMER_CONTROL_PRESCALER_DIV25, 237 timer->base + RZN1_TIMER_CONTROL_REG); 238 timer->rate = clock_rate / 25; 239 } else { 240 timer->width = 32; 241 timer->rate = clock_rate; 242 } 243 timer->hz_period = DIV_ROUND_UP(timer->rate, HZ); 244 } 245 246 static void rzn1_timer_clkevt_init_ced(struct rzn1_timer *timer, const char *name, 247 int irq) 248 { 249 timer->ced.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT; 250 timer->ced.set_next_event = rzn1_clkevt_set_next_event; 251 timer->ced.set_state_shutdown = rzn1_clkevt_shutdown; 252 timer->ced.set_state_periodic = rzn1_clkevt_set_periodic; 253 timer->ced.cpumask = cpu_possible_mask; 254 255 /* 256 * 32-bit timers are fed with a 25 MHz clock directly derived from the 257 * main PLL (1 GHz). The arch timer, ARMv7 CP15 in RZ/N1 SoCs, is fed 258 * with to a 6.25 MHz clock. 259 * 260 * The clock of the RZ/N1 timers block is a stable one and the RZ/N1 261 * timers have a higher resolution than the ARM CP15 one. 262 * 263 * Use a high rating for the 32-bit RZ/N1 timers. 264 */ 265 timer->ced.rating = timer->width == 16 ? 200 : 460; 266 timer->ced.owner = THIS_MODULE; 267 timer->ced.name = name; 268 timer->ced.irq = irq; 269 } 270 271 static int rzn1_timer_setup_timers(struct platform_device *pdev, 272 struct rzn1_timer *tab_timers, 273 unsigned int first_timer_index, 274 unsigned int last_timer_index, 275 void __iomem *base, unsigned long clock_rate) 276 { 277 struct device *dev = &pdev->dev; 278 struct rzn1_timer *timer; 279 unsigned int i; 280 char *name; 281 int irq; 282 int ret; 283 284 for (i = first_timer_index; i <= last_timer_index; i++) { 285 timer = &tab_timers[i]; 286 287 rzn1_timer_init(timer, i, base, clock_rate); 288 289 irq = platform_get_irq(pdev, i); 290 if (irq < 0) 291 return irq; 292 293 name = devm_kasprintf(dev, GFP_KERNEL, "%s-%u", dev_name(dev), i); 294 if (!name) 295 return -ENOMEM; 296 297 rzn1_timer_clkevt_init_ced(timer, name, irq); 298 299 ret = devm_request_irq(dev, timer->ced.irq, rzn1_timer_interrupt, 300 IRQF_TIMER | IRQF_NO_AUTOEN, 301 timer->ced.name, timer); 302 if (ret < 0) 303 return dev_err_probe(dev, ret, "timer%u: Failed to request IRQ\n", i); 304 305 rzn1_timer_int_enable(timer); 306 } 307 return 0; 308 } 309 310 static void rzn1_timer_register_clkevt(struct rzn1_timer *tab_timers, 311 unsigned int first_timer_index, 312 unsigned int last_timer_index) 313 { 314 struct rzn1_timer *timer; 315 unsigned int i; 316 317 for (i = first_timer_index; i <= last_timer_index; i++) { 318 timer = &tab_timers[i]; 319 clockevents_config_and_register(&timer->ced, timer->rate, 320 1, GENMASK(timer->width - 1, 0)); 321 /* 322 * Time to enable the interrupt even if no interrupt can be 323 * fired yet. Indeed, the timer is still globally disabled. 324 * It will be enabled when rzn1_timer_config() is called 325 */ 326 enable_irq(timer->ced.irq); 327 } 328 } 329 330 static int rzn1_timer_probe_first(struct platform_device *pdev, 331 struct rzn1_timer *tab_timers, 332 void __iomem *base, unsigned long clock_rate) 333 { 334 struct device *dev = &pdev->dev; 335 struct rzn1_timer *timer; 336 int ret; 337 338 /* 339 * Each timer instance has 6 x 16-bit timers and 2 x 32-bit timers. 340 * 341 * Probe the first instance. In that case, timers are assigned as 342 * follow: 343 * - First and second 16-bit timers: clock events per CPU 344 * - Last (second one) 32-bit timer: clocksource and sched_clock 345 * - Other timers: clock events for all possible CPUs 346 * 347 * First step, perform all operation that could fail without calling 348 * clockevents_config_and_register(), sched_clock_register() nor 349 * cpuhp_setup_state(). Those operation don't have unregister nor 350 * teardown counterparts and so, once called, we cannot remove the 351 * related resource. 352 */ 353 354 /* First step for 16-bit timers and the first 32-bit timers. */ 355 ret = rzn1_timer_setup_timers(pdev, tab_timers, 356 RZN1_TIMER_BASE_INDEX_16BIT_TIMERS, 357 RZN1_TIMER_BASE_INDEX_32BIT_TIMERS, 358 base, clock_rate); 359 if (ret) 360 return ret; 361 362 /* 363 * Second step, almost all operations that can fail have been called. 364 * Timers are ready to work. Start with the last operation that can fail, 365 * installing and invoking hotplug callbacks 366 */ 367 rzn1_tab_timers = tab_timers; 368 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, 369 "clockevents/rzn1/global_timer:starting", 370 rzn1_local_timer_starting_cpu, 371 rzn1_local_timer_dying_cpu); 372 if (ret < 0) 373 return dev_err_probe(dev, ret, "Setup CPU hotplug state failed\n"); 374 375 /* 376 * At this point, no more operations can fail. Perform remaining 377 * operations. Starting by handling the last (second one) 32-bit timer 378 */ 379 timer = &tab_timers[RZN1_TIMER_LAST_INDEX_32BIT_TIMERS]; 380 381 rzn1_timer_init(timer, RZN1_TIMER_LAST_INDEX_32BIT_TIMERS, base, clock_rate); 382 rzn1_timer_config(timer, GENMASK(timer->width - 1, 0), true); 383 384 rzn1_sched_clock = timer; 385 386 sched_clock_register(rzn1_sched_read, rzn1_sched_clock->width, rzn1_sched_clock->rate); 387 388 rzn1_clocksource.mask = CLOCKSOURCE_MASK(rzn1_sched_clock->width); 389 clocksource_register_hz(&rzn1_clocksource, rzn1_sched_clock->rate); 390 391 /* 392 * Register clockevents for 16-bit timers (except the first and second 393 * ones) and for the first 32-bit timer. The first and second 16-bit 394 * timers clockevents are registered by CPU hotplug startup function set 395 * previously by the cpuhp_setup_state() call. 396 */ 397 rzn1_timer_register_clkevt(tab_timers, 398 RZN1_TIMER_BASE_INDEX_16BIT_TIMERS + 2, 399 RZN1_TIMER_BASE_INDEX_32BIT_TIMERS); 400 401 return 0; 402 } 403 404 static int rzn1_timer_probe_other(struct platform_device *pdev, 405 struct rzn1_timer *tab_timers, 406 void __iomem *base, unsigned long clock_rate) 407 { 408 int ret; 409 410 /* 411 * Probe other instance(s), i.e. not the first one. In that case, 412 * all timers are used as clock events and available for all possible 413 * CPUs 414 * 415 * First step, perform all operation that could fail without calling 416 * clockevents_config_and_register(). Unregister counterpart does not 417 * exist and so, once called, we cannot remove the related resource. 418 */ 419 ret = rzn1_timer_setup_timers(pdev, tab_timers, 420 RZN1_TIMER_BASE_INDEX_16BIT_TIMERS, 421 RZN1_TIMER_LAST_INDEX_32BIT_TIMERS, 422 base, clock_rate); 423 if (ret) 424 return ret; 425 426 /* 427 * Second step, all operation that can fail have been called. We can 428 * register our timers. 429 */ 430 rzn1_timer_register_clkevt(tab_timers, 431 RZN1_TIMER_BASE_INDEX_16BIT_TIMERS, 432 RZN1_TIMER_LAST_INDEX_32BIT_TIMERS); 433 return 0; 434 } 435 436 static int rzn1_timer_probe(struct platform_device *pdev) 437 { 438 struct device *dev = &pdev->dev; 439 struct rzn1_timer *tab_timers; 440 unsigned long clock_rate; 441 void __iomem *base; 442 struct clk *clk; 443 int ret; 444 445 tab_timers = devm_kcalloc(dev, RZN1_TIMER_NB_TIMERS, sizeof(*tab_timers), 446 GFP_KERNEL); 447 if (!tab_timers) 448 return -ENOMEM; 449 450 base = devm_platform_ioremap_resource(pdev, 0); 451 if (IS_ERR(base)) 452 return PTR_ERR(base); 453 454 clk = devm_clk_get_enabled(dev, "pclk"); 455 if (IS_ERR(clk)) 456 return dev_err_probe(dev, PTR_ERR(clk), "Failed to get pclk\n"); 457 458 clock_rate = clk_get_rate(clk); 459 460 scoped_guard(mutex, &rzn1_global_timers_lock) { 461 ret = !rzn1_timer_instance ? 462 rzn1_timer_probe_first(pdev, tab_timers, base, clock_rate) : 463 rzn1_timer_probe_other(pdev, tab_timers, base, clock_rate); 464 if (!ret) 465 rzn1_timer_instance++; 466 } 467 468 return ret; 469 } 470 471 static const struct of_device_id rzn1_timer_of_match[] = { 472 { .compatible = "renesas,rzn1-timer", }, 473 { /* sentinel */ } 474 }; 475 476 static struct platform_driver rzn1_timer_driver = { 477 .driver = { 478 .name = "rzn1_timer", 479 .of_match_table = rzn1_timer_of_match, 480 .suppress_bind_attrs = true, 481 }, 482 }; 483 builtin_platform_driver_probe(rzn1_timer_driver, rzn1_timer_probe); 484