i2c: mlxbf: Fix frequency calculation

The i2c-mlxbf.c driver is currently broken because there is a bug
in the calculation of the frequency. core_f, core_r and core_od
are components read from hardware registers and are used to
compute the frequency used to compute different timing parameters.
The shifting mechanism used to get core_f, core_r and core_od is
wrong. Use FIELD_GET to mask and shift the bitfields properly.

Fixes: b5b5b32081cd206b (i2c: mlxbf: I2C SMBus driver for Mellanox BlueField SoC)
Reviewed-by: Khalil Blaiech <kblaiech@nvidia.com>
Signed-off-by: Asmaa Mnebhi <asmaa@nvidia.com>
Signed-off-by: Wolfram Sang <wsa@kernel.org>

authored by Asmaa Mnebhi and committed by Wolfram Sang 37f071ec de24aceb

Changed files
+23 -40
drivers
i2c
busses
+23 -40
drivers/i2c/busses/i2c-mlxbf.c
··· 6 6 */ 7 7 8 8 #include <linux/acpi.h> 9 + #include <linux/bitfield.h> 9 10 #include <linux/delay.h> 10 11 #include <linux/err.h> 11 12 #include <linux/interrupt.h> ··· 64 63 */ 65 64 #define MLXBF_I2C_TYU_PLL_OUT_FREQ (400 * 1000 * 1000) 66 65 /* Reference clock for Bluefield - 156 MHz. */ 67 - #define MLXBF_I2C_PLL_IN_FREQ (156 * 1000 * 1000) 66 + #define MLXBF_I2C_PLL_IN_FREQ 156250000ULL 68 67 69 68 /* Constant used to determine the PLL frequency. */ 70 - #define MLNXBF_I2C_COREPLL_CONST 16384 69 + #define MLNXBF_I2C_COREPLL_CONST 16384ULL 70 + 71 + #define MLXBF_I2C_FREQUENCY_1GHZ 1000000000ULL 71 72 72 73 /* PLL registers. */ 73 - #define MLXBF_I2C_CORE_PLL_REG0 0x0 74 74 #define MLXBF_I2C_CORE_PLL_REG1 0x4 75 75 #define MLXBF_I2C_CORE_PLL_REG2 0x8 76 76 ··· 183 181 #define MLXBF_I2C_COREPLL_FREQ MLXBF_I2C_TYU_PLL_OUT_FREQ 184 182 185 183 /* Core PLL TYU configuration. */ 186 - #define MLXBF_I2C_COREPLL_CORE_F_TYU_MASK GENMASK(12, 0) 187 - #define MLXBF_I2C_COREPLL_CORE_OD_TYU_MASK GENMASK(3, 0) 188 - #define MLXBF_I2C_COREPLL_CORE_R_TYU_MASK GENMASK(5, 0) 189 - 190 - #define MLXBF_I2C_COREPLL_CORE_F_TYU_SHIFT 3 191 - #define MLXBF_I2C_COREPLL_CORE_OD_TYU_SHIFT 16 192 - #define MLXBF_I2C_COREPLL_CORE_R_TYU_SHIFT 20 184 + #define MLXBF_I2C_COREPLL_CORE_F_TYU_MASK GENMASK(15, 3) 185 + #define MLXBF_I2C_COREPLL_CORE_OD_TYU_MASK GENMASK(19, 16) 186 + #define MLXBF_I2C_COREPLL_CORE_R_TYU_MASK GENMASK(25, 20) 193 187 194 188 /* Core PLL YU configuration. */ 195 189 #define MLXBF_I2C_COREPLL_CORE_F_YU_MASK GENMASK(25, 0) 196 190 #define MLXBF_I2C_COREPLL_CORE_OD_YU_MASK GENMASK(3, 0) 197 - #define MLXBF_I2C_COREPLL_CORE_R_YU_MASK GENMASK(5, 0) 191 + #define MLXBF_I2C_COREPLL_CORE_R_YU_MASK GENMASK(31, 26) 198 192 199 - #define MLXBF_I2C_COREPLL_CORE_F_YU_SHIFT 0 200 - #define MLXBF_I2C_COREPLL_CORE_OD_YU_SHIFT 1 201 - #define MLXBF_I2C_COREPLL_CORE_R_YU_SHIFT 26 202 193 203 194 /* Core PLL frequency. */ 204 195 static u64 mlxbf_i2c_corepll_frequency; ··· 473 478 474 479 #define MLXBF_I2C_MASK_8 GENMASK(7, 0) 475 480 #define MLXBF_I2C_MASK_16 GENMASK(15, 0) 476 - 477 - #define MLXBF_I2C_FREQUENCY_1GHZ 1000000000 478 481 479 482 /* 480 483 * Function to poll a set of bits at a specific address; it checks whether ··· 1403 1410 return 0; 1404 1411 } 1405 1412 1406 - static u64 mlxbf_calculate_freq_from_tyu(struct mlxbf_i2c_resource *corepll_res) 1413 + static u64 mlxbf_i2c_calculate_freq_from_tyu(struct mlxbf_i2c_resource *corepll_res) 1407 1414 { 1408 - u64 core_frequency, pad_frequency; 1415 + u64 core_frequency; 1409 1416 u8 core_od, core_r; 1410 1417 u32 corepll_val; 1411 1418 u16 core_f; 1412 1419 1413 - pad_frequency = MLXBF_I2C_PLL_IN_FREQ; 1414 - 1415 1420 corepll_val = readl(corepll_res->io + MLXBF_I2C_CORE_PLL_REG1); 1416 1421 1417 1422 /* Get Core PLL configuration bits. */ 1418 - core_f = rol32(corepll_val, MLXBF_I2C_COREPLL_CORE_F_TYU_SHIFT) & 1419 - MLXBF_I2C_COREPLL_CORE_F_TYU_MASK; 1420 - core_od = rol32(corepll_val, MLXBF_I2C_COREPLL_CORE_OD_TYU_SHIFT) & 1421 - MLXBF_I2C_COREPLL_CORE_OD_TYU_MASK; 1422 - core_r = rol32(corepll_val, MLXBF_I2C_COREPLL_CORE_R_TYU_SHIFT) & 1423 - MLXBF_I2C_COREPLL_CORE_R_TYU_MASK; 1423 + core_f = FIELD_GET(MLXBF_I2C_COREPLL_CORE_F_TYU_MASK, corepll_val); 1424 + core_od = FIELD_GET(MLXBF_I2C_COREPLL_CORE_OD_TYU_MASK, corepll_val); 1425 + core_r = FIELD_GET(MLXBF_I2C_COREPLL_CORE_R_TYU_MASK, corepll_val); 1424 1426 1425 1427 /* 1426 1428 * Compute PLL output frequency as follow: ··· 1427 1439 * Where PLL_OUT_FREQ and PLL_IN_FREQ refer to CoreFrequency 1428 1440 * and PadFrequency, respectively. 1429 1441 */ 1430 - core_frequency = pad_frequency * (++core_f); 1442 + core_frequency = MLXBF_I2C_PLL_IN_FREQ * (++core_f); 1431 1443 core_frequency /= (++core_r) * (++core_od); 1432 1444 1433 1445 return core_frequency; 1434 1446 } 1435 1447 1436 - static u64 mlxbf_calculate_freq_from_yu(struct mlxbf_i2c_resource *corepll_res) 1448 + static u64 mlxbf_i2c_calculate_freq_from_yu(struct mlxbf_i2c_resource *corepll_res) 1437 1449 { 1438 1450 u32 corepll_reg1_val, corepll_reg2_val; 1439 - u64 corepll_frequency, pad_frequency; 1451 + u64 corepll_frequency; 1440 1452 u8 core_od, core_r; 1441 1453 u32 core_f; 1442 - 1443 - pad_frequency = MLXBF_I2C_PLL_IN_FREQ; 1444 1454 1445 1455 corepll_reg1_val = readl(corepll_res->io + MLXBF_I2C_CORE_PLL_REG1); 1446 1456 corepll_reg2_val = readl(corepll_res->io + MLXBF_I2C_CORE_PLL_REG2); 1447 1457 1448 1458 /* Get Core PLL configuration bits */ 1449 - core_f = rol32(corepll_reg1_val, MLXBF_I2C_COREPLL_CORE_F_YU_SHIFT) & 1450 - MLXBF_I2C_COREPLL_CORE_F_YU_MASK; 1451 - core_r = rol32(corepll_reg1_val, MLXBF_I2C_COREPLL_CORE_R_YU_SHIFT) & 1452 - MLXBF_I2C_COREPLL_CORE_R_YU_MASK; 1453 - core_od = rol32(corepll_reg2_val, MLXBF_I2C_COREPLL_CORE_OD_YU_SHIFT) & 1454 - MLXBF_I2C_COREPLL_CORE_OD_YU_MASK; 1459 + core_f = FIELD_GET(MLXBF_I2C_COREPLL_CORE_F_YU_MASK, corepll_reg1_val); 1460 + core_r = FIELD_GET(MLXBF_I2C_COREPLL_CORE_R_YU_MASK, corepll_reg1_val); 1461 + core_od = FIELD_GET(MLXBF_I2C_COREPLL_CORE_OD_YU_MASK, corepll_reg2_val); 1455 1462 1456 1463 /* 1457 1464 * Compute PLL output frequency as follow: ··· 1458 1475 * Where PLL_OUT_FREQ and PLL_IN_FREQ refer to CoreFrequency 1459 1476 * and PadFrequency, respectively. 1460 1477 */ 1461 - corepll_frequency = (pad_frequency * core_f) / MLNXBF_I2C_COREPLL_CONST; 1478 + corepll_frequency = (MLXBF_I2C_PLL_IN_FREQ * core_f) / MLNXBF_I2C_COREPLL_CONST; 1462 1479 corepll_frequency /= (++core_r) * (++core_od); 1463 1480 1464 1481 return corepll_frequency; ··· 2166 2183 [1] = &mlxbf_i2c_corepll_res[MLXBF_I2C_CHIP_TYPE_1], 2167 2184 [2] = &mlxbf_i2c_gpio_res[MLXBF_I2C_CHIP_TYPE_1] 2168 2185 }, 2169 - .calculate_freq = mlxbf_calculate_freq_from_tyu 2186 + .calculate_freq = mlxbf_i2c_calculate_freq_from_tyu 2170 2187 }, 2171 2188 [MLXBF_I2C_CHIP_TYPE_2] = { 2172 2189 .type = MLXBF_I2C_CHIP_TYPE_2, 2173 2190 .shared_res = { 2174 2191 [0] = &mlxbf_i2c_corepll_res[MLXBF_I2C_CHIP_TYPE_2] 2175 2192 }, 2176 - .calculate_freq = mlxbf_calculate_freq_from_yu 2193 + .calculate_freq = mlxbf_i2c_calculate_freq_from_yu 2177 2194 } 2178 2195 }; 2179 2196