Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

spi: rzv2h-rspi: add support for RZ/T2H and RZ/N2H

Compared to the previously supported RZ/V2H, the Renesas RZ/T2H
(R9A09G077) and RZ/N2H (R9A09G087) SoCs have a smaller FIFO, no resets,
and only two clocks: PCLKSPIn and PCLK. PCLKSPIn, being the clock from
which the SPI transfer clock is generated, is the equivalent of the TCLK
clock from RZ/V2H. They also support generating the SPI transfer clock
from PCLK.

PCLKSPIn supports multiple dividers, generating multiple possible
frequencies from its parent. To handle this, do the following changes.

Use the minimum frequency of SPI clock to calculate the SPI controller's
min_speed_hz, and the maximum frequency to calculate max_speed_hz.

Add a new function, rzv2h_rspi_find_rate_variable(), which is used for
the .find_tclk_rate() callback, and which supports handling clocks with
a variable rate, with the following overall logic.

Iterate through all possible BRDV values.

For each BRDV, calculate two different SPRs, one for the clock's minimum
frequency, and one for the maxmimum, and iterate through each SPR
between them.

If the minimum SPR is higher than the upper SPR limit, the minimum rate
is too high to achieve the requested SPI frequency, skip to the next
BRDV.

For each SPR, calculate a rate and let the clock framework round it to
the closest supported rate of the clock.

The rate and SPR that generate a transfer frequency closest to the
requested SPI transfer frequency will be picked.

Signed-off-by: Cosmin Tanislav <cosmin-gabriel.tanislav.xa@renesas.com>
Link: https://patch.msgid.link/20251119161434.595677-12-cosmin-gabriel.tanislav.xa@renesas.com
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Cosmin Tanislav and committed by
Mark Brown
0cc8cd82 e93d7b2d

+108
+108
drivers/spi/spi-rzv2h-rspi.c
··· 261 261 return DIV_ROUND_UP(tclk_rate, (2 * (spr + 1) * (1 << brdv))); 262 262 } 263 263 264 + static void rzv2h_rspi_find_rate_variable(struct clk *clk, u32 hz, 265 + u8 spr_min, u8 spr_max, 266 + struct rzv2h_rspi_best_clock *best) 267 + { 268 + long clk_rate, clk_min_rate, clk_max_rate; 269 + int min_rate_spr, max_rate_spr; 270 + unsigned long error; 271 + u32 actual_hz; 272 + u8 brdv; 273 + int spr; 274 + 275 + /* 276 + * On T2H / N2H, the source for the SPI clock is PCLKSPIn, which is a 277 + * 1/32, 1/30, 1/25 or 1/24 divider of PLL4, which is 2400MHz, 278 + * resulting in either 75MHz, 80MHz, 96MHz or 100MHz. 279 + */ 280 + clk_min_rate = clk_round_rate(clk, 0); 281 + if (clk_min_rate < 0) 282 + return; 283 + 284 + clk_max_rate = clk_round_rate(clk, ULONG_MAX); 285 + if (clk_max_rate < 0) 286 + return; 287 + 288 + /* 289 + * From the manual: 290 + * Bit rate = f(PCLKSPIn) / (2 * (n + 1) * 2^N) 291 + * 292 + * If we adapt it to the current context, we get the following: 293 + * hz = rate / ((spr + 1) * (1 << (brdv + 1))) 294 + * 295 + * This can be written in multiple forms depending on what we want to 296 + * determine. 297 + * 298 + * To find the rate, having hz, spr and brdv: 299 + * rate = hz * (spr + 1) * (1 << (brdv + 1) 300 + * 301 + * To find the spr, having rate, hz, and spr: 302 + * spr = rate / (hz * (1 << (brdv + 1)) - 1 303 + */ 304 + 305 + for (brdv = RSPI_SPCMD_BRDV_MIN; brdv <= RSPI_SPCMD_BRDV_MAX; brdv++) { 306 + /* Calculate the divisor needed to find the SPR from a rate. */ 307 + u32 rate_div = hz * (1 << (brdv + 1)); 308 + 309 + /* 310 + * If the SPR for the minimum rate is greater than the maximum 311 + * allowed value skip this BRDV. The divisor increases with each 312 + * BRDV iteration, so the following BRDV might result in a 313 + * minimum SPR that is in the valid range. 314 + */ 315 + min_rate_spr = DIV_ROUND_CLOSEST(clk_min_rate, rate_div) - 1; 316 + if (min_rate_spr > spr_max) 317 + continue; 318 + 319 + /* 320 + * If the SPR for the maximum rate is less than the minimum 321 + * allowed value, exit. The divisor only increases with each 322 + * BRDV iteration, so the following BRDV cannot result in a 323 + * maximum SPR that is in the valid range. 324 + */ 325 + max_rate_spr = DIV_ROUND_CLOSEST(clk_max_rate, rate_div) - 1; 326 + if (max_rate_spr < spr_min) 327 + break; 328 + 329 + if (min_rate_spr < spr_min) 330 + min_rate_spr = spr_min; 331 + 332 + if (max_rate_spr > spr_max) 333 + max_rate_spr = spr_max; 334 + 335 + for (spr = min_rate_spr; spr <= max_rate_spr; spr++) { 336 + clk_rate = (spr + 1) * rate_div; 337 + 338 + clk_rate = clk_round_rate(clk, clk_rate); 339 + if (clk_rate <= 0) 340 + continue; 341 + 342 + actual_hz = rzv2h_rspi_calc_bitrate(clk_rate, spr, brdv); 343 + error = abs((long)hz - (long)actual_hz); 344 + 345 + if (error >= best->error) 346 + continue; 347 + 348 + *best = (struct rzv2h_rspi_best_clock) { 349 + .clk = clk, 350 + .clk_rate = clk_rate, 351 + .error = error, 352 + .actual_hz = actual_hz, 353 + .brdv = brdv, 354 + .spr = spr, 355 + }; 356 + 357 + if (!error) 358 + return; 359 + } 360 + } 361 + } 362 + 264 363 static void rzv2h_rspi_find_rate_fixed(struct clk *clk, u32 hz, 265 364 u8 spr_min, u8 spr_max, 266 365 struct rzv2h_rspi_best_clock *best) ··· 657 558 .num_clks = 3, 658 559 }; 659 560 561 + static const struct rzv2h_rspi_info rzt2h_info = { 562 + .find_tclk_rate = rzv2h_rspi_find_rate_variable, 563 + .find_pclk_rate = rzv2h_rspi_find_rate_fixed, 564 + .tclk_name = "pclkspi", 565 + .fifo_size = 4, 566 + .num_clks = 2, 567 + }; 568 + 660 569 static const struct of_device_id rzv2h_rspi_match[] = { 661 570 { .compatible = "renesas,r9a09g057-rspi", &rzv2h_info }, 571 + { .compatible = "renesas,r9a09g077-rspi", &rzt2h_info }, 662 572 { /* sentinel */ } 663 573 }; 664 574 MODULE_DEVICE_TABLE(of, rzv2h_rspi_match);