| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* r9a09g077 Clock Pulse Generator / Module Standby and Software Reset |
| 4 |
* |
| 5 |
* Copyright (C) 2025 Renesas Electronics Corp. |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <linux/bitfield.h> |
| 10 |
#include <linux/clk-provider.h> |
| 11 |
#include <linux/device.h> |
| 12 |
#include <linux/init.h> |
| 13 |
#include <linux/kernel.h> |
| 14 |
#include <linux/math.h> |
| 15 |
#include <linux/types.h> |
| 16 |
|
| 17 |
#include <dt-bindings/clock/renesas,r9a09g077-cpg-mssr.h> |
| 18 |
#include <dt-bindings/clock/renesas,r9a09g087-cpg-mssr.h> |
| 19 |
#include "renesas-cpg-mssr.h" |
| 20 |
|
| 21 |
#define RZT2H_REG_BLOCK_SHIFT 11 |
| 22 |
#define RZT2H_REG_OFFSET_MASK GENMASK(10, 0) |
| 23 |
#define RZT2H_REG_CONF(block, offset) (((block) << RZT2H_REG_BLOCK_SHIFT) | \ |
| 24 |
((offset) & RZT2H_REG_OFFSET_MASK)) |
| 25 |
|
| 26 |
#define RZT2H_REG_BLOCK(x) ((x) >> RZT2H_REG_BLOCK_SHIFT) |
| 27 |
#define RZT2H_REG_OFFSET(x) ((x) & RZT2H_REG_OFFSET_MASK) |
| 28 |
|
| 29 |
#define SCKCR RZT2H_REG_CONF(0, 0x00) |
| 30 |
#define SCKCR2 RZT2H_REG_CONF(1, 0x04) |
| 31 |
#define SCKCR3 RZT2H_REG_CONF(0, 0x08) |
| 32 |
|
| 33 |
#define OFFSET_MASK GENMASK(31, 20) |
| 34 |
#define SHIFT_MASK GENMASK(19, 12) |
| 35 |
#define WIDTH_MASK GENMASK(11, 8) |
| 36 |
|
| 37 |
#define CONF_PACK(offset, shift, width) \ |
| 38 |
(FIELD_PREP_CONST(OFFSET_MASK, (offset)) | \ |
| 39 |
FIELD_PREP_CONST(SHIFT_MASK, (shift)) | \ |
| 40 |
FIELD_PREP_CONST(WIDTH_MASK, (width))) |
| 41 |
|
| 42 |
#define GET_SHIFT(val) FIELD_GET(SHIFT_MASK, val) |
| 43 |
#define GET_WIDTH(val) FIELD_GET(WIDTH_MASK, val) |
| 44 |
#define GET_REG_OFFSET(val) FIELD_GET(OFFSET_MASK, val) |
| 45 |
|
| 46 |
#define FSELXSPI0 CONF_PACK(SCKCR, 0, 3) |
| 47 |
#define FSELXSPI1 CONF_PACK(SCKCR, 8, 3) |
| 48 |
#define DIVSEL_XSPI0 CONF_PACK(SCKCR, 6, 1) |
| 49 |
#define DIVSEL_XSPI1 CONF_PACK(SCKCR, 14, 1) |
| 50 |
#define FSELCANFD CONF_PACK(SCKCR, 20, 1) |
| 51 |
#define SEL_PLL CONF_PACK(SCKCR, 22, 1) |
| 52 |
|
| 53 |
#define DIVCA55C0 CONF_PACK(SCKCR2, 8, 1) |
| 54 |
#define DIVCA55C1 CONF_PACK(SCKCR2, 9, 1) |
| 55 |
#define DIVCA55C2 CONF_PACK(SCKCR2, 10, 1) |
| 56 |
#define DIVCA55C3 CONF_PACK(SCKCR2, 11, 1) |
| 57 |
#define DIVCA55S CONF_PACK(SCKCR2, 12, 1) |
| 58 |
#define DIVSPI3ASYNC CONF_PACK(SCKCR2, 16, 2) |
| 59 |
#define DIVSCI5ASYNC CONF_PACK(SCKCR2, 18, 2) |
| 60 |
|
| 61 |
#define DIVSPI0ASYNC CONF_PACK(SCKCR3, 0, 2) |
| 62 |
#define DIVSPI1ASYNC CONF_PACK(SCKCR3, 2, 2) |
| 63 |
#define DIVSPI2ASYNC CONF_PACK(SCKCR3, 4, 2) |
| 64 |
#define DIVSCI0ASYNC CONF_PACK(SCKCR3, 6, 2) |
| 65 |
#define DIVSCI1ASYNC CONF_PACK(SCKCR3, 8, 2) |
| 66 |
#define DIVSCI2ASYNC CONF_PACK(SCKCR3, 10, 2) |
| 67 |
#define DIVSCI3ASYNC CONF_PACK(SCKCR3, 12, 2) |
| 68 |
#define DIVSCI4ASYNC CONF_PACK(SCKCR3, 14, 2) |
| 69 |
|
| 70 |
enum rzt2h_clk_types { |
| 71 |
CLK_TYPE_RZT2H_DIV = CLK_TYPE_CUSTOM, /* Clock with divider */ |
| 72 |
CLK_TYPE_RZT2H_MUX, /* Clock with clock source selector */ |
| 73 |
CLK_TYPE_RZT2H_FSELXSPI, /* Clock with FSELXSPIn source selector */ |
| 74 |
}; |
| 75 |
|
| 76 |
#define DEF_DIV(_name, _id, _parent, _conf, _dtable) \ |
| 77 |
DEF_TYPE(_name, _id, CLK_TYPE_RZT2H_DIV, .conf = _conf, \ |
| 78 |
.parent = _parent, .dtable = _dtable, .flag = 0) |
| 79 |
#define DEF_MUX(_name, _id, _conf, _parent_names, _num_parents, _mux_flags) \ |
| 80 |
DEF_TYPE(_name, _id, CLK_TYPE_RZT2H_MUX, .conf = _conf, \ |
| 81 |
.parent_names = _parent_names, .num_parents = _num_parents, \ |
| 82 |
.flag = CLK_SET_RATE_PARENT, .mux_flags = _mux_flags) |
| 83 |
#define DEF_DIV_FSELXSPI(_name, _id, _parent, _conf, _dtable) \ |
| 84 |
DEF_TYPE(_name, _id, CLK_TYPE_RZT2H_FSELXSPI, .conf = _conf, \ |
| 85 |
.parent = _parent, .dtable = _dtable, .flag = 0) |
| 86 |
|
| 87 |
enum clk_ids { |
| 88 |
/* Core Clock Outputs exported to DT */ |
| 89 |
LAST_DT_CORE_CLK = R9A09G077_PCLKCAN, |
| 90 |
|
| 91 |
/* External Input Clocks */ |
| 92 |
CLK_EXTAL, |
| 93 |
|
| 94 |
/* Internal Core Clocks */ |
| 95 |
CLK_LOCO, |
| 96 |
CLK_PLL0, |
| 97 |
CLK_PLL1, |
| 98 |
CLK_PLL2, |
| 99 |
CLK_PLL4, |
| 100 |
CLK_SEL_CLK_PLL0, |
| 101 |
CLK_SEL_CLK_PLL1, |
| 102 |
CLK_SEL_CLK_PLL2, |
| 103 |
CLK_SEL_CLK_PLL4, |
| 104 |
CLK_PLL4D1, |
| 105 |
CLK_PLL4D1_DIV3, |
| 106 |
CLK_PLL4D1_DIV4, |
| 107 |
CLK_PLL4D3, |
| 108 |
CLK_PLL4D3_DIV10, |
| 109 |
CLK_PLL4D3_DIV20, |
| 110 |
CLK_SCI0ASYNC, |
| 111 |
CLK_SCI1ASYNC, |
| 112 |
CLK_SCI2ASYNC, |
| 113 |
CLK_SCI3ASYNC, |
| 114 |
CLK_SCI4ASYNC, |
| 115 |
CLK_SCI5ASYNC, |
| 116 |
CLK_SPI0ASYNC, |
| 117 |
CLK_SPI1ASYNC, |
| 118 |
CLK_SPI2ASYNC, |
| 119 |
CLK_SPI3ASYNC, |
| 120 |
CLK_DIVSELXSPI0_SCKCR, |
| 121 |
CLK_DIVSELXSPI1_SCKCR, |
| 122 |
|
| 123 |
/* Module Clocks */ |
| 124 |
MOD_CLK_BASE, |
| 125 |
}; |
| 126 |
|
| 127 |
static const struct clk_div_table dtable_1_2[] = { |
| 128 |
{0, 2}, |
| 129 |
{1, 1}, |
| 130 |
{0, 0}, |
| 131 |
}; |
| 132 |
|
| 133 |
static const struct clk_div_table dtable_6_8_16_32_64[] = { |
| 134 |
{6, 64}, |
| 135 |
{5, 32}, |
| 136 |
{4, 16}, |
| 137 |
{3, 8}, |
| 138 |
{2, 6}, |
| 139 |
{0, 0}, |
| 140 |
}; |
| 141 |
|
| 142 |
static const struct clk_div_table dtable_24_25_30_32[] = { |
| 143 |
{0, 32}, |
| 144 |
{1, 30}, |
| 145 |
{2, 25}, |
| 146 |
{3, 24}, |
| 147 |
{0, 0}, |
| 148 |
}; |
| 149 |
|
| 150 |
/* Mux clock tables */ |
| 151 |
|
| 152 |
static const char * const sel_clk_pll0[] = { ".loco", ".pll0" }; |
| 153 |
static const char * const sel_clk_pll1[] = { ".loco", ".pll1" }; |
| 154 |
static const char * const sel_clk_pll2[] = { ".loco", ".pll2" }; |
| 155 |
static const char * const sel_clk_pll4[] = { ".loco", ".pll4" }; |
| 156 |
static const char * const sel_clk_pll4d1_div3_div4[] = { ".pll4d1_div3", ".pll4d1_div4" }; |
| 157 |
static const char * const sel_clk_pll4d3_div10_div20[] = { ".pll4d3_div10", ".pll4d3_div20" }; |
| 158 |
|
| 159 |
static const struct cpg_core_clk r9a09g077_core_clks[] __initconst = { |
| 160 |
/* External Clock Inputs */ |
| 161 |
DEF_INPUT("extal", CLK_EXTAL), |
| 162 |
|
| 163 |
/* Internal Core Clocks */ |
| 164 |
DEF_RATE(".loco", CLK_LOCO, 1000 * 1000), |
| 165 |
DEF_FIXED(".pll0", CLK_PLL0, CLK_EXTAL, 1, 48), |
| 166 |
DEF_FIXED(".pll1", CLK_PLL1, CLK_EXTAL, 1, 40), |
| 167 |
DEF_FIXED(".pll2", CLK_PLL2, CLK_EXTAL, 1, 32), |
| 168 |
DEF_FIXED(".pll4", CLK_PLL4, CLK_EXTAL, 1, 96), |
| 169 |
|
| 170 |
DEF_MUX(".sel_clk_pll0", CLK_SEL_CLK_PLL0, SEL_PLL, |
| 171 |
sel_clk_pll0, ARRAY_SIZE(sel_clk_pll0), CLK_MUX_READ_ONLY), |
| 172 |
DEF_MUX(".sel_clk_pll1", CLK_SEL_CLK_PLL1, SEL_PLL, |
| 173 |
sel_clk_pll1, ARRAY_SIZE(sel_clk_pll1), CLK_MUX_READ_ONLY), |
| 174 |
DEF_MUX(".sel_clk_pll2", CLK_SEL_CLK_PLL2, SEL_PLL, |
| 175 |
sel_clk_pll2, ARRAY_SIZE(sel_clk_pll2), CLK_MUX_READ_ONLY), |
| 176 |
DEF_MUX(".sel_clk_pll4", CLK_SEL_CLK_PLL4, SEL_PLL, |
| 177 |
sel_clk_pll4, ARRAY_SIZE(sel_clk_pll4), CLK_MUX_READ_ONLY), |
| 178 |
|
| 179 |
DEF_FIXED(".pll4d1", CLK_PLL4D1, CLK_SEL_CLK_PLL4, 1, 1), |
| 180 |
DEF_FIXED(".pll4d1_div3", CLK_PLL4D1_DIV3, CLK_PLL4D1, 3, 1), |
| 181 |
DEF_FIXED(".pll4d1_div4", CLK_PLL4D1_DIV4, CLK_PLL4D1, 4, 1), |
| 182 |
DEF_FIXED(".pll4d3", CLK_PLL4D3, CLK_SEL_CLK_PLL4, 3, 1), |
| 183 |
DEF_FIXED(".pll4d3_div10", CLK_PLL4D3_DIV10, CLK_PLL4D3, 10, 1), |
| 184 |
DEF_FIXED(".pll4d3_div20", CLK_PLL4D3_DIV20, CLK_PLL4D3, 20, 1), |
| 185 |
|
| 186 |
DEF_DIV(".sci0async", CLK_SCI0ASYNC, CLK_PLL4D1, DIVSCI0ASYNC, |
| 187 |
dtable_24_25_30_32), |
| 188 |
DEF_DIV(".sci1async", CLK_SCI1ASYNC, CLK_PLL4D1, DIVSCI1ASYNC, |
| 189 |
dtable_24_25_30_32), |
| 190 |
DEF_DIV(".sci2async", CLK_SCI2ASYNC, CLK_PLL4D1, DIVSCI2ASYNC, |
| 191 |
dtable_24_25_30_32), |
| 192 |
DEF_DIV(".sci3async", CLK_SCI3ASYNC, CLK_PLL4D1, DIVSCI3ASYNC, |
| 193 |
dtable_24_25_30_32), |
| 194 |
DEF_DIV(".sci4async", CLK_SCI4ASYNC, CLK_PLL4D1, DIVSCI4ASYNC, |
| 195 |
dtable_24_25_30_32), |
| 196 |
DEF_DIV(".sci5async", CLK_SCI5ASYNC, CLK_PLL4D1, DIVSCI5ASYNC, |
| 197 |
dtable_24_25_30_32), |
| 198 |
|
| 199 |
DEF_DIV(".spi0async", CLK_SPI0ASYNC, CLK_PLL4D1, DIVSPI0ASYNC, |
| 200 |
dtable_24_25_30_32), |
| 201 |
DEF_DIV(".spi1async", CLK_SPI1ASYNC, CLK_PLL4D1, DIVSPI1ASYNC, |
| 202 |
dtable_24_25_30_32), |
| 203 |
DEF_DIV(".spi2async", CLK_SPI2ASYNC, CLK_PLL4D1, DIVSPI2ASYNC, |
| 204 |
dtable_24_25_30_32), |
| 205 |
DEF_DIV(".spi3async", CLK_SPI3ASYNC, CLK_PLL4D1, DIVSPI3ASYNC, |
| 206 |
dtable_24_25_30_32), |
| 207 |
|
| 208 |
DEF_MUX(".divselxspi0", CLK_DIVSELXSPI0_SCKCR, DIVSEL_XSPI0, |
| 209 |
sel_clk_pll4d1_div3_div4, |
| 210 |
ARRAY_SIZE(sel_clk_pll4d1_div3_div4), 0), |
| 211 |
DEF_MUX(".divselxspi1", CLK_DIVSELXSPI1_SCKCR, DIVSEL_XSPI1, |
| 212 |
sel_clk_pll4d1_div3_div4, |
| 213 |
ARRAY_SIZE(sel_clk_pll4d1_div3_div4), 0), |
| 214 |
|
| 215 |
/* Core output clk */ |
| 216 |
DEF_DIV("CA55C0", R9A09G077_CLK_CA55C0, CLK_SEL_CLK_PLL0, DIVCA55C0, |
| 217 |
dtable_1_2), |
| 218 |
DEF_DIV("CA55C1", R9A09G077_CLK_CA55C1, CLK_SEL_CLK_PLL0, DIVCA55C1, |
| 219 |
dtable_1_2), |
| 220 |
DEF_DIV("CA55C2", R9A09G077_CLK_CA55C2, CLK_SEL_CLK_PLL0, DIVCA55C2, |
| 221 |
dtable_1_2), |
| 222 |
DEF_DIV("CA55C3", R9A09G077_CLK_CA55C3, CLK_SEL_CLK_PLL0, DIVCA55C3, |
| 223 |
dtable_1_2), |
| 224 |
DEF_DIV("CA55S", R9A09G077_CLK_CA55S, CLK_SEL_CLK_PLL0, DIVCA55S, |
| 225 |
dtable_1_2), |
| 226 |
DEF_FIXED("PCLKGPTL", R9A09G077_CLK_PCLKGPTL, CLK_SEL_CLK_PLL1, 2, 1), |
| 227 |
DEF_FIXED("PCLKH", R9A09G077_CLK_PCLKH, CLK_SEL_CLK_PLL1, 4, 1), |
| 228 |
DEF_FIXED("PCLKM", R9A09G077_CLK_PCLKM, CLK_SEL_CLK_PLL1, 8, 1), |
| 229 |
DEF_FIXED("PCLKL", R9A09G077_CLK_PCLKL, CLK_SEL_CLK_PLL1, 16, 1), |
| 230 |
DEF_FIXED("PCLKAH", R9A09G077_CLK_PCLKAH, CLK_PLL4D1, 6, 1), |
| 231 |
DEF_FIXED("PCLKAM", R9A09G077_CLK_PCLKAM, CLK_PLL4D1, 12, 1), |
| 232 |
DEF_FIXED("SDHI_CLKHS", R9A09G077_SDHI_CLKHS, CLK_SEL_CLK_PLL2, 1, 1), |
| 233 |
DEF_FIXED("USB_CLK", R9A09G077_USB_CLK, CLK_PLL4D1, 48, 1), |
| 234 |
DEF_FIXED("ETCLKA", R9A09G077_ETCLKA, CLK_SEL_CLK_PLL1, 5, 1), |
| 235 |
DEF_FIXED("ETCLKB", R9A09G077_ETCLKB, CLK_SEL_CLK_PLL1, 8, 1), |
| 236 |
DEF_FIXED("ETCLKC", R9A09G077_ETCLKC, CLK_SEL_CLK_PLL1, 10, 1), |
| 237 |
DEF_FIXED("ETCLKD", R9A09G077_ETCLKD, CLK_SEL_CLK_PLL1, 20, 1), |
| 238 |
DEF_FIXED("ETCLKE", R9A09G077_ETCLKE, CLK_SEL_CLK_PLL1, 40, 1), |
| 239 |
DEF_DIV_FSELXSPI("XSPI_CLK0", R9A09G077_XSPI_CLK0, CLK_DIVSELXSPI0_SCKCR, |
| 240 |
FSELXSPI0, dtable_6_8_16_32_64), |
| 241 |
DEF_DIV_FSELXSPI("XSPI_CLK1", R9A09G077_XSPI_CLK1, CLK_DIVSELXSPI1_SCKCR, |
| 242 |
FSELXSPI1, dtable_6_8_16_32_64), |
| 243 |
DEF_MUX("PCLKCAN", R9A09G077_PCLKCAN, FSELCANFD, |
| 244 |
sel_clk_pll4d3_div10_div20, ARRAY_SIZE(sel_clk_pll4d3_div10_div20), 0), |
| 245 |
}; |
| 246 |
|
| 247 |
static const struct mssr_mod_clk r9a09g077_mod_clks[] __initconst = { |
| 248 |
DEF_MOD("xspi0", 4, R9A09G077_CLK_PCLKH), |
| 249 |
DEF_MOD("xspi1", 5, R9A09G077_CLK_PCLKH), |
| 250 |
DEF_MOD("sci0fck", 8, CLK_SCI0ASYNC), |
| 251 |
DEF_MOD("sci1fck", 9, CLK_SCI1ASYNC), |
| 252 |
DEF_MOD("sci2fck", 10, CLK_SCI2ASYNC), |
| 253 |
DEF_MOD("sci3fck", 11, CLK_SCI3ASYNC), |
| 254 |
DEF_MOD("sci4fck", 12, CLK_SCI4ASYNC), |
| 255 |
DEF_MOD("iic0", 100, R9A09G077_CLK_PCLKL), |
| 256 |
DEF_MOD("iic1", 101, R9A09G077_CLK_PCLKL), |
| 257 |
DEF_MOD("spi0", 104, CLK_SPI0ASYNC), |
| 258 |
DEF_MOD("spi1", 105, CLK_SPI1ASYNC), |
| 259 |
DEF_MOD("spi2", 106, CLK_SPI2ASYNC), |
| 260 |
DEF_MOD("adc0", 206, R9A09G077_CLK_PCLKH), |
| 261 |
DEF_MOD("adc1", 207, R9A09G077_CLK_PCLKH), |
| 262 |
DEF_MOD("adc2", 225, R9A09G077_CLK_PCLKM), |
| 263 |
DEF_MOD("tsu", 307, R9A09G077_CLK_PCLKL), |
| 264 |
DEF_MOD("canfd", 310, R9A09G077_CLK_PCLKM), |
| 265 |
DEF_MOD("gmac0", 400, R9A09G077_CLK_PCLKM), |
| 266 |
DEF_MOD("ethsw", 401, R9A09G077_CLK_PCLKM), |
| 267 |
DEF_MOD("ethss", 403, R9A09G077_CLK_PCLKM), |
| 268 |
DEF_MOD("usb", 408, R9A09G077_CLK_PCLKAM), |
| 269 |
DEF_MOD("gmac1", 416, R9A09G077_CLK_PCLKAM), |
| 270 |
DEF_MOD("gmac2", 417, R9A09G077_CLK_PCLKAM), |
| 271 |
DEF_MOD("sci5fck", 600, CLK_SCI5ASYNC), |
| 272 |
DEF_MOD("iic2", 601, R9A09G077_CLK_PCLKL), |
| 273 |
DEF_MOD("spi3", 602, CLK_SPI3ASYNC), |
| 274 |
DEF_MOD("sdhi0", 1212, R9A09G077_CLK_PCLKAM), |
| 275 |
DEF_MOD("sdhi1", 1213, R9A09G077_CLK_PCLKAM), |
| 276 |
}; |
| 277 |
|
| 278 |
static struct clk * __init |
| 279 |
r9a09g077_cpg_div_clk_register(struct device *dev, |
| 280 |
const struct cpg_core_clk *core, |
| 281 |
void __iomem *addr, struct cpg_mssr_pub *pub) |
| 282 |
{ |
| 283 |
const struct clk *parent; |
| 284 |
const char *parent_name; |
| 285 |
struct clk_hw *clk_hw; |
| 286 |
|
| 287 |
parent = pub->clks[core->parent]; |
| 288 |
if (IS_ERR(parent)) |
| 289 |
return ERR_CAST(parent); |
| 290 |
|
| 291 |
parent_name = __clk_get_name(parent); |
| 292 |
|
| 293 |
if (core->dtable) |
| 294 |
clk_hw = devm_clk_hw_register_divider_table(dev, core->name, |
| 295 |
parent_name, |
| 296 |
CLK_SET_RATE_PARENT, |
| 297 |
addr, |
| 298 |
GET_SHIFT(core->conf), |
| 299 |
GET_WIDTH(core->conf), |
| 300 |
core->flag, |
| 301 |
core->dtable, |
| 302 |
&pub->rmw_lock); |
| 303 |
else |
| 304 |
clk_hw = devm_clk_hw_register_divider(dev, core->name, |
| 305 |
parent_name, |
| 306 |
CLK_SET_RATE_PARENT, |
| 307 |
addr, |
| 308 |
GET_SHIFT(core->conf), |
| 309 |
GET_WIDTH(core->conf), |
| 310 |
core->flag, &pub->rmw_lock); |
| 311 |
|
| 312 |
if (IS_ERR(clk_hw)) |
| 313 |
return ERR_CAST(clk_hw); |
| 314 |
|
| 315 |
return clk_hw->clk; |
| 316 |
} |
| 317 |
|
| 318 |
static struct clk * __init |
| 319 |
r9a09g077_cpg_mux_clk_register(struct device *dev, |
| 320 |
const struct cpg_core_clk *core, |
| 321 |
void __iomem *addr, struct cpg_mssr_pub *pub) |
| 322 |
{ |
| 323 |
struct clk_hw *clk_hw; |
| 324 |
|
| 325 |
clk_hw = devm_clk_hw_register_mux(dev, core->name, |
| 326 |
core->parent_names, core->num_parents, |
| 327 |
core->flag, |
| 328 |
addr, |
| 329 |
GET_SHIFT(core->conf), |
| 330 |
GET_WIDTH(core->conf), |
| 331 |
core->mux_flags, &pub->rmw_lock); |
| 332 |
if (IS_ERR(clk_hw)) |
| 333 |
return ERR_CAST(clk_hw); |
| 334 |
|
| 335 |
return clk_hw->clk; |
| 336 |
} |
| 337 |
|
| 338 |
static unsigned int r9a09g077_cpg_fselxspi_get_divider(struct clk_hw *hw, unsigned long rate, |
| 339 |
unsigned int num_parents) |
| 340 |
{ |
| 341 |
struct clk_fixed_factor *ff; |
| 342 |
struct clk_hw *parent_hw; |
| 343 |
unsigned long best_rate; |
| 344 |
unsigned int i; |
| 345 |
|
| 346 |
for (i = 0; i < num_parents; i++) { |
| 347 |
parent_hw = clk_hw_get_parent_by_index(hw, i); |
| 348 |
best_rate = clk_hw_round_rate(parent_hw, rate); |
| 349 |
|
| 350 |
if (best_rate == rate) { |
| 351 |
ff = to_clk_fixed_factor(parent_hw); |
| 352 |
return ff->div; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
/* No parent could provide the exact rate - this should not happen */ |
| 357 |
return 0; |
| 358 |
} |
| 359 |
|
| 360 |
static int r9a09g077_cpg_fselxspi_determine_rate(struct clk_hw *hw, |
| 361 |
struct clk_rate_request *req) |
| 362 |
{ |
| 363 |
struct clk_divider *divider = to_clk_divider(hw); |
| 364 |
unsigned long parent_rate, best = 0, now; |
| 365 |
const struct clk_div_table *clkt; |
| 366 |
unsigned long rate = req->rate; |
| 367 |
unsigned int num_parents; |
| 368 |
unsigned int divselxspi; |
| 369 |
unsigned int div = 0; |
| 370 |
|
| 371 |
if (!rate) |
| 372 |
rate = 1; |
| 373 |
|
| 374 |
/* Get the number of parents for FSELXSPIn */ |
| 375 |
num_parents = clk_hw_get_num_parents(req->best_parent_hw); |
| 376 |
|
| 377 |
for (clkt = divider->table; clkt->div; clkt++) { |
| 378 |
parent_rate = clk_hw_round_rate(req->best_parent_hw, rate * clkt->div); |
| 379 |
/* Skip if parent can't provide any valid rate */ |
| 380 |
if (!parent_rate) |
| 381 |
continue; |
| 382 |
|
| 383 |
/* Determine which DIVSELXSPIn divider (3 or 4) provides this parent_rate */ |
| 384 |
divselxspi = r9a09g077_cpg_fselxspi_get_divider(req->best_parent_hw, parent_rate, |
| 385 |
num_parents); |
| 386 |
if (!divselxspi) |
| 387 |
continue; |
| 388 |
|
| 389 |
/* |
| 390 |
* DIVSELXSPIx supports 800MHz and 600MHz operation. |
| 391 |
* When divselxspi is 4 (600MHz operation), only FSELXSPIn dividers of 8 and 16 |
| 392 |
* are supported. Otherwise, when divselxspi is 3 (800MHz operation), |
| 393 |
* dividers of 6, 8, 16, 32, and 64 are supported. This check ensures that |
| 394 |
* FSELXSPIx is set correctly based on hardware limitations. |
| 395 |
*/ |
| 396 |
if (divselxspi == 4 && (clkt->div != 8 && clkt->div != 16)) |
| 397 |
continue; |
| 398 |
|
| 399 |
now = DIV_ROUND_UP_ULL(parent_rate, clkt->div); |
| 400 |
if (abs(rate - now) < abs(rate - best)) { |
| 401 |
div = clkt->div; |
| 402 |
best = now; |
| 403 |
req->best_parent_rate = parent_rate; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
if (!div) { |
| 408 |
req->best_parent_rate = clk_hw_round_rate(req->best_parent_hw, 1); |
| 409 |
divselxspi = r9a09g077_cpg_fselxspi_get_divider(req->best_parent_hw, |
| 410 |
req->best_parent_rate, |
| 411 |
num_parents); |
| 412 |
/* default to divider 3 which will result DIVSELXSPIn = 800 MHz */ |
| 413 |
if (!divselxspi) |
| 414 |
divselxspi = 3; |
| 415 |
|
| 416 |
/* |
| 417 |
* Use the maximum divider based on the parent clock rate: |
| 418 |
* - 64 when DIVSELXSPIx is 800 MHz (divider = 3) |
| 419 |
* - 16 when DIVSELXSPIx is 600 MHz (divider = 4) |
| 420 |
*/ |
| 421 |
div = divselxspi == 3 ? 64 : 16; |
| 422 |
} |
| 423 |
|
| 424 |
req->rate = DIV_ROUND_UP_ULL(req->best_parent_rate, div); |
| 425 |
|
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
static struct clk * __init |
| 430 |
r9a09g077_cpg_fselxspi_div_clk_register(struct device *dev, |
| 431 |
const struct cpg_core_clk *core, |
| 432 |
void __iomem *addr, |
| 433 |
struct cpg_mssr_pub *pub) |
| 434 |
{ |
| 435 |
static struct clk_ops *xspi_div_ops; |
| 436 |
struct clk_init_data init = {}; |
| 437 |
const struct clk *parent; |
| 438 |
const char *parent_name; |
| 439 |
struct clk_divider *div; |
| 440 |
struct clk_hw *hw; |
| 441 |
int ret; |
| 442 |
|
| 443 |
parent = pub->clks[core->parent]; |
| 444 |
if (IS_ERR(parent)) |
| 445 |
return ERR_CAST(parent); |
| 446 |
|
| 447 |
div = devm_kzalloc(dev, sizeof(*div), GFP_KERNEL); |
| 448 |
if (!div) |
| 449 |
return ERR_PTR(-ENOMEM); |
| 450 |
|
| 451 |
if (!xspi_div_ops) { |
| 452 |
xspi_div_ops = devm_kzalloc(dev, sizeof(*xspi_div_ops), GFP_KERNEL); |
| 453 |
if (!xspi_div_ops) |
| 454 |
return ERR_PTR(-ENOMEM); |
| 455 |
memcpy(xspi_div_ops, &clk_divider_ops, |
| 456 |
sizeof(const struct clk_ops)); |
| 457 |
xspi_div_ops->determine_rate = r9a09g077_cpg_fselxspi_determine_rate; |
| 458 |
} |
| 459 |
|
| 460 |
parent_name = __clk_get_name(parent); |
| 461 |
init.name = core->name; |
| 462 |
init.ops = xspi_div_ops; |
| 463 |
init.flags = CLK_SET_RATE_PARENT; |
| 464 |
init.parent_names = &parent_name; |
| 465 |
init.num_parents = 1; |
| 466 |
|
| 467 |
div->reg = addr; |
| 468 |
div->shift = GET_SHIFT(core->conf); |
| 469 |
div->width = GET_WIDTH(core->conf); |
| 470 |
div->flags = core->flag; |
| 471 |
div->lock = &pub->rmw_lock; |
| 472 |
div->hw.init = &init; |
| 473 |
div->table = core->dtable; |
| 474 |
|
| 475 |
hw = &div->hw; |
| 476 |
ret = devm_clk_hw_register(dev, hw); |
| 477 |
if (ret) |
| 478 |
return ERR_PTR(ret); |
| 479 |
|
| 480 |
return hw->clk; |
| 481 |
} |
| 482 |
|
| 483 |
static struct clk * __init |
| 484 |
r9a09g077_cpg_clk_register(struct device *dev, const struct cpg_core_clk *core, |
| 485 |
const struct cpg_mssr_info *info, |
| 486 |
struct cpg_mssr_pub *pub) |
| 487 |
{ |
| 488 |
u32 offset = GET_REG_OFFSET(core->conf); |
| 489 |
void __iomem *base = RZT2H_REG_BLOCK(offset) ? pub->base1 : pub->base0; |
| 490 |
void __iomem *addr = base + RZT2H_REG_OFFSET(offset); |
| 491 |
|
| 492 |
switch (core->type) { |
| 493 |
case CLK_TYPE_RZT2H_DIV: |
| 494 |
return r9a09g077_cpg_div_clk_register(dev, core, addr, pub); |
| 495 |
case CLK_TYPE_RZT2H_MUX: |
| 496 |
return r9a09g077_cpg_mux_clk_register(dev, core, addr, pub); |
| 497 |
case CLK_TYPE_RZT2H_FSELXSPI: |
| 498 |
return r9a09g077_cpg_fselxspi_div_clk_register(dev, core, addr, pub); |
| 499 |
default: |
| 500 |
return ERR_PTR(-EINVAL); |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
const struct cpg_mssr_info r9a09g077_cpg_mssr_info = { |
| 505 |
/* Core Clocks */ |
| 506 |
.core_clks = r9a09g077_core_clks, |
| 507 |
.num_core_clks = ARRAY_SIZE(r9a09g077_core_clks), |
| 508 |
.last_dt_core_clk = LAST_DT_CORE_CLK, |
| 509 |
.num_total_core_clks = MOD_CLK_BASE, |
| 510 |
|
| 511 |
/* Module Clocks */ |
| 512 |
.mod_clks = r9a09g077_mod_clks, |
| 513 |
.num_mod_clks = ARRAY_SIZE(r9a09g077_mod_clks), |
| 514 |
.num_hw_mod_clks = 14 * 32, |
| 515 |
|
| 516 |
.reg_layout = CLK_REG_LAYOUT_RZ_T2H, |
| 517 |
.cpg_clk_register = r9a09g077_cpg_clk_register, |
| 518 |
}; |