// SPDX-License-Identifier: GPL-2.0 /* * Common clock framework driver for the Versaclock7 family of timing devices. * * Copyright (c) 2022 Renesas Electronics Corporation
*/
/* * 16-bit register address: the lower 8 bits of the register address come * from the offset addr byte and the upper 8 bits come from the page register.
*/ #define VC7_PAGE_ADDR 0xFD #define VC7_PAGE_WINDOW 256 #define VC7_MAX_REG 0x364
/* Maximum number of banks supported by VC7 */ #define VC7_NUM_BANKS 7
/* Maximum number of FODs supported by VC7 */ #define VC7_NUM_FOD 3
/* Maximum number of IODs supported by VC7 */ #define VC7_NUM_IOD 4
/* Maximum number of outputs supported by VC7 */ #define VC7_NUM_OUT 12
/* VCO valid range is 9.5 GHz to 10.7 GHz */ #define VC7_APLL_VCO_MIN 9500000000UL #define VC7_APLL_VCO_MAX 10700000000UL
/* APLL denominator is fixed at 2^27 */ #define VC7_APLL_DENOMINATOR_BITS 27
/* FOD 1st stage denominator is fixed 2^34 */ #define VC7_FOD_DENOMINATOR_BITS 34
/* IOD can operate between 1kHz and 650MHz */ #define VC7_IOD_RATE_MIN 1000UL #define VC7_IOD_RATE_MAX 650000000UL #define VC7_IOD_MIN_DIVISOR 14 #define VC7_IOD_MAX_DIVISOR 0x1ffffff /* 25-bit */
/* * Changing the APLL frequency is currently not supported. * The APLL will consist of an opaque block between the XO and FOD/IODs and * its frequency will be computed based on the current state of the device.
*/ struct vc7_apll_data { struct clk *clk; struct vc7_driver_data *vc7;
u8 xo_ib_h_div;
u8 en_doubler;
u16 apll_fb_div_int;
u32 apll_fb_div_frac;
};
/** * vc7_64_mul_64_to_128() - Multiply two u64 and return an unsigned 128-bit integer * as an upper and lower part. * * @left: The left argument. * @right: The right argument. * @hi: The upper 64-bits of the 128-bit product. * @lo: The lower 64-bits of the 128-bit product. * * From mul_64_64 in crypto/ecc.c:350 in the linux kernel, accessed in v5.17.2.
*/ staticvoid vc7_64_mul_64_to_128(u64 left, u64 right, u64 *hi, u64 *lo)
{
u64 a0 = left & 0xffffffffull;
u64 a1 = left >> 32;
u64 b0 = right & 0xffffffffull;
u64 b1 = right >> 32;
u64 m0 = a0 * b0;
u64 m1 = a0 * b1;
u64 m2 = a1 * b0;
u64 m3 = a1 * b1;
/** * vc7_128_div_64_to_64() - Divides a 128-bit uint by a 64-bit divisor, return a 64-bit quotient. * * @numhi: The uppper 64-bits of the dividend. * @numlo: The lower 64-bits of the dividend. * @den: The denominator (divisor). * @r: The remainder, pass NULL if the remainder is not needed. * * Originally from libdivide, modified to use kernel u64/u32 types. * * See https://github.com/ridiculousfish/libdivide/blob/master/libdivide.h#L471. * * Return: The 64-bit quotient of the division. * * In case of overflow of division by zero, max(u64) is returned.
*/ static u64 vc7_128_div_64_to_64(u64 numhi, u64 numlo, u64 den, u64 *r)
{ /* * We work in base 2**32. * A uint32 holds a single digit. A uint64 holds two digits. * Our numerator is conceptually [num3, num2, num1, num0]. * Our denominator is [den1, den0].
*/ const u64 b = ((u64)1 << 32);
/* The high and low digits of our computed quotient. */
u32 q1, q0;
/* The normalization shift factor */ int shift;
/* * The high and low digits of our denominator (after normalizing). * Also the low 2 digits of our numerator (after normalizing).
*/
u32 den1, den0, num1, num0;
/* A partial remainder; */
u64 rem;
/* * The estimated quotient, and its corresponding remainder (unrelated * to true remainder).
*/
u64 qhat, rhat;
/* Variables used to correct the estimated quotient. */
u64 c1, c2;
/* Check for overflow and divide by 0. */ if (numhi >= den) { if (r)
*r = ~0ull; return ~0ull;
}
/* * Determine the normalization factor. We multiply den by this, so that * its leading digit is at least half b. In binary this means just * shifting left by the number of leading zeros, so that there's a 1 in * the MSB. * * We also shift numer by the same amount. This cannot overflow because * numhi < den. The expression (-shift & 63) is the same as (64 - * shift), except it avoids the UB of shifting by 64. The funny bitwise * 'and' ensures that numlo does not get shifted into numhi if shift is * 0. clang 11 has an x86 codegen bug here: see LLVM bug 50118. The * sequence below avoids it.
*/
shift = __builtin_clzll(den);
den <<= shift;
numhi <<= shift;
numhi |= (numlo >> (-shift & 63)) & (-(s64)shift >> 63);
numlo <<= shift;
/* * Extract the low digits of the numerator and both digits of the * denominator.
*/
num1 = (u32)(numlo >> 32);
num0 = (u32)(numlo & 0xFFFFFFFFu);
den1 = (u32)(den >> 32);
den0 = (u32)(den & 0xFFFFFFFFu);
/* * We wish to compute q1 = [n3 n2 n1] / [d1 d0]. * Estimate q1 as [n3 n2] / [d1], and then correct it. * Note while qhat may be 2 digits, q1 is always 1 digit.
*/
qhat = div64_u64_rem(numhi, den1, &rhat);
c1 = qhat * den0;
c2 = rhat * b + num1; if (c1 > c2)
qhat -= (c1 - c2 > den) ? 2 : 1;
q1 = (u32)qhat;
/* Compute the true (partial) remainder. */
rem = numhi * b + num1 - q1 * den;
/* * We wish to compute q0 = [rem1 rem0 n0] / [d1 d0]. * Estimate q0 as [rem1 rem0] / [d1] and correct it.
*/
qhat = div64_u64_rem(rem, den1, &rhat);
c1 = qhat * den0;
c2 = rhat * b + num0; if (c1 > c2)
qhat -= (c1 - c2 > den) ? 2 : 1;
q0 = (u32)qhat;
/* Return remainder if requested. */ if (r)
*r = (rem * b + num0 - q0 * den) >> shift; return ((u64)q1 << 32) | q0;
}
/* * FOD dividers are part of an atomic group where fod_1st_int, * fod_2nd_int, and fod_frac must be written together. The new divider * is applied when the MSB of fod_frac is written.
*/
err = regmap_bulk_read(vc7->regmap,
VC7_REG_FOD_INT_CNFG(idx),
(u64 *)&val,
VC7_REG_FOD_INT_CNFG_COUNT); if (err) {
dev_err(&vc7->client->dev, "failed to read FOD%d\n", idx); return err;
}
val = u64_replace_bits(val,
vc7->clk_fod[idx].fod_1st_int,
VC7_REG_FOD_1ST_INT_MASK);
val = u64_replace_bits(val,
vc7->clk_fod[idx].fod_2nd_int,
VC7_REG_FOD_2ND_INT_MASK);
val = u64_replace_bits(val,
vc7->clk_fod[idx].fod_frac,
VC7_REG_FOD_FRAC_MASK);
err = regmap_bulk_write(vc7->regmap,
VC7_REG_FOD_INT_CNFG(idx),
(u64 *)&val, sizeof(u64)); if (err) {
dev_err(&vc7->client->dev, "failed to write FOD%d\n", idx); return err;
}
/* * There is a div-by-2 preceding the 2nd stage integer divider * (not shown on block diagram) so the actual 2nd stage integer * divisor is 2 * N.
*/ return div64_u64(fod_1st_stage_rate >> 1, fod_2nd_int);
}
/* Do we need the second stage integer divider? */ if (first_stage_rate < VC7_FOD_1ST_STAGE_RATE_MIN) {
allow_frac = 0;
best_frac_i = VC7_FOD_2ND_INT_MIN;
for (i = VC7_FOD_2ND_INT_MIN; i <= VC7_FOD_2ND_INT_MAX; i++) { /* * 1) There is a div-by-2 preceding the 2nd stage integer divider * (not shown on block diagram) so the actual 2nd stage integer * divisor is 2 * N. * 2) Attempt to find an integer solution first. This means stepping * through each 2nd stage integer and recalculating the 1st stage * until the 1st stage frequency is out of bounds. If no integer * solution is found, use the best fractional solution.
*/
vc7_calc_fod_1st_stage(parent_rate, rate * 2 * i, fod_1st_int, fod_frac);
first_stage_rate = vc7_calc_fod_1st_stage_rate(parent_rate,
*fod_1st_int,
*fod_frac);
/* Remember the first viable fractional solution */ if (best_frac_i == VC7_FOD_2ND_INT_MIN &&
first_stage_rate > VC7_FOD_1ST_STAGE_RATE_MIN) {
best_frac_i = i;
}
/* Is the divider viable? Prefer integer solutions over fractional. */ if (*fod_1st_int < VC7_FOD_1ST_INT_MAX &&
first_stage_rate >= VC7_FOD_1ST_STAGE_RATE_MIN &&
(allow_frac || *fod_frac == 0)) {
*fod_2nd_int = i; break;
}
/* Ran out of divisors or the 1st stage frequency is out of range */ if (i >= VC7_FOD_2ND_INT_MAX ||
first_stage_rate > VC7_FOD_1ST_STAGE_RATE_MAX) {
allow_frac = 1;
i = best_frac_i;
/* Restore the best frac and rerun the loop for the last time */ if (best_frac_i != VC7_FOD_2ND_INT_MIN)
i--;
if (rate < VC7_FOD_RATE_MIN || rate > VC7_FOD_RATE_MAX) {
dev_err(&vc7->client->dev, "requested frequency %lu Hz for %s is out of range\n",
rate, clk_hw_get_name(hw)); return -EINVAL;
}
if (rate < VC7_IOD_RATE_MIN || rate > VC7_IOD_RATE_MAX) {
dev_err(&vc7->client->dev, "requested frequency %lu Hz for %s is out of range\n",
rate, clk_hw_get_name(hw)); return -EINVAL;
}
vc7->pin_xin = devm_clk_get(&client->dev, "xin"); if (PTR_ERR(vc7->pin_xin) == -EPROBE_DEFER) { return dev_err_probe(&client->dev, -EPROBE_DEFER, "xin not specified\n");
}
vc7->regmap = devm_regmap_init_i2c(client, &vc7_regmap_config); if (IS_ERR(vc7->regmap)) { return dev_err_probe(&client->dev, PTR_ERR(vc7->regmap), "failed to allocate register map\n");
}
if (of_property_read_string(client->dev.of_node, "clock-output-names",
&node_name))
node_name = client->dev.of_node->name;
/* Register APLL */
apll_rate = vc7_get_apll_rate(vc7);
apll_name = kasprintf(GFP_KERNEL, "%s_apll", node_name);
vc7->clk_apll.clk = clk_register_fixed_rate(&client->dev, apll_name,
__clk_get_name(vc7->pin_xin), 0, apll_rate);
kfree(apll_name); /* ccf made a copy of the name */ if (IS_ERR(vc7->clk_apll.clk)) { return dev_err_probe(&client->dev, PTR_ERR(vc7->clk_apll.clk), "failed to register apll\n");
}
/* Register FODs */ for (i = 0; i < VC7_NUM_FOD; i++) {
memset(&clk_init, 0, sizeof(clk_init));
clk_init.name = kasprintf(GFP_KERNEL, "%s_fod%d", node_name, i);
clk_init.ops = &vc7_fod_ops;
clk_init.parent_names = parent_names;
parent_names[0] = __clk_get_name(vc7->clk_apll.clk);
clk_init.num_parents = 1;
vc7->clk_fod[i].num = i;
vc7->clk_fod[i].vc7 = vc7;
vc7->clk_fod[i].hw.init = &clk_init;
ret = devm_clk_hw_register(&client->dev, &vc7->clk_fod[i].hw); if (ret) goto err_clk_register;
kfree(clk_init.name); /* ccf made a copy of the name */
}
/* Register IODs */ for (i = 0; i < VC7_NUM_IOD; i++) {
memset(&clk_init, 0, sizeof(clk_init));
clk_init.name = kasprintf(GFP_KERNEL, "%s_iod%d", node_name, i);
clk_init.ops = &vc7_iod_ops;
clk_init.parent_names = parent_names;
parent_names[0] = __clk_get_name(vc7->clk_apll.clk);
clk_init.num_parents = 1;
vc7->clk_iod[i].num = i;
vc7->clk_iod[i].vc7 = vc7;
vc7->clk_iod[i].hw.init = &clk_init;
ret = devm_clk_hw_register(&client->dev, &vc7->clk_iod[i].hw); if (ret) goto err_clk_register;
kfree(clk_init.name); /* ccf made a copy of the name */
}
/* Register outputs */ for (i = 0; i < vc7->chip_info->num_outputs; i++) {
out_num = vc7_map_index_to_output(vc7->chip_info->model, i);
/* * This driver does not support remapping FOD/IOD to banks. * The device state is read and the driver is setup to match * the device's existing mapping.
*/
bank_idx = output_bank_mapping[out_num];
regmap_read(vc7->regmap, VC7_REG_OUT_BANK_CNFG(bank_idx), &val);
val &= VC7_REG_OUTPUT_BANK_SRC_MASK;
memset(&bank_src_map, 0, sizeof(bank_src_map));
ret = vc7_get_bank_clk(vc7, bank_idx, val, &bank_src_map); if (ret) {
dev_err_probe(&client->dev, ret, "unable to register output %d\n", i); return ret;
}
switch (bank_src_map.type) { case VC7_FOD:
parent_names[0] = clk_hw_get_name(&bank_src_map.src.fod->hw); break; case VC7_IOD:
parent_names[0] = clk_hw_get_name(&bank_src_map.src.iod->hw); break;
}
memset(&clk_init, 0, sizeof(clk_init));
clk_init.name = kasprintf(GFP_KERNEL, "%s_out%d", node_name, i);
clk_init.ops = &vc7_clk_out_ops;
clk_init.flags = CLK_SET_RATE_PARENT;
clk_init.parent_names = parent_names;
clk_init.num_parents = 1;
vc7->clk_out[i].num = i;
vc7->clk_out[i].vc7 = vc7;
vc7->clk_out[i].hw.init = &clk_init;
ret = devm_clk_hw_register(&client->dev, &vc7->clk_out[i].hw); if (ret) goto err_clk_register;
kfree(clk_init.name); /* ccf made a copy of the name */
}
ret = of_clk_add_hw_provider(client->dev.of_node, vc7_of_clk_get, vc7); if (ret) {
dev_err_probe(&client->dev, ret, "unable to add clk provider\n"); goto err_clk;
}
return ret;
err_clk_register:
dev_err_probe(&client->dev, ret, "unable to register %s\n", clk_init.name);
kfree(clk_init.name); /* ccf made a copy of the name */
err_clk:
clk_unregister_fixed_rate(vc7->clk_apll.clk); return ret;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.