// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2024 AIROHA Inc
* Author: Lorenzo Bianconi <lorenzo@kernel.org>
*/
#include <linux/bitfield.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/phy/phy.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include "phy-airoha-pcie-regs.h"
#define LEQ_LEN_CTRL_MAX_VAL 7
#define FREQ_LOCK_MAX_ATTEMPT 10
enum airoha_pcie_port_gen {
PCIE_PORT_GEN1 = 1,
PCIE_PORT_GEN2,
PCIE_PORT_GEN3,
};
/**
* struct airoha_pcie_phy - PCIe phy driver main structure
* @dev: pointer to device
* @phy: pointer to generic phy
* @csr_2l: Analogic lane IO mapped register base address
* @pma0: IO mapped register base address of PMA0-PCIe
* @pma1: IO mapped register base address of PMA1-PCIe
* @p0_xr_dtime: IO mapped register base address of port0 Tx-Rx detection time
* @p1_xr_dtime: IO mapped register base address of port1 Tx-Rx detection time
* @rx_aeq: IO mapped register base address of Rx AEQ training
*/
struct airoha_pcie_phy {
struct device *dev;
struct phy *phy;
void __iomem *csr_2l;
void __iomem *pma0;
void __iomem *pma1;
void __iomem *p0_xr_dtime;
void __iomem *p1_xr_dtime;
void __iomem *rx_aeq;
};
static void airoha_phy_clear_bits(void __iomem *reg, u32 mask)
{
u32 val = readl(reg) & ~mask;
writel(val, reg);
}
static void airoha_phy_set_bits(void __iomem *reg, u32 mask)
{
u32 val = readl(reg) | mask;
writel(val, reg);
}
static void airoha_phy_update_bits(void __iomem *reg, u32 mask, u32 val)
{
u32 tmp = readl(reg);
tmp &= ~mask;
tmp |= val & mask;
writel(tmp, reg);
}
#define airoha_phy_update_field(reg, mask, val) \
do { \
BUILD_BUG_ON_MSG(!__builtin_constant_p((mask)), \
"mask is not constant"); \
airoha_phy_update_bits((reg), (mask), \
FIELD_PREP((mask), (val))); \
} while (0)
#define airoha_phy_csr_2l_clear_bits(pcie_phy, reg, mask) \
airoha_phy_clear_bits((pcie_phy)->csr_2l + (reg), (mask))
#define airoha_phy_csr_2l_set_bits(pcie_phy, reg, mask) \
airoha_phy_set_bits((pcie_phy)->csr_2l + (reg), (mask))
#define airoha_phy_csr_2l_update_field(pcie_phy, reg, mask, val) \
airoha_phy_update_field((pcie_phy)->csr_2l + (reg), (mask), (val))
#define airoha_phy_pma0_clear_bits(pcie_phy, reg, mask) \
airoha_phy_clear_bits((pcie_phy)->pma0 + (reg), (mask))
#define airoha_phy_pma1_clear_bits(pcie_phy, reg, mask) \
airoha_phy_clear_bits((pcie_phy)->pma1 + (reg), (mask))
#define airoha_phy_pma0_set_bits(pcie_phy, reg, mask) \
airoha_phy_set_bits((pcie_phy)->pma0 + (reg), (mask))
#define airoha_phy_pma1_set_bits(pcie_phy, reg, mask) \
airoha_phy_set_bits((pcie_phy)->pma1 + (reg), (mask))
#define airoha_phy_pma0_update_field(pcie_phy, reg, mask, val) \
airoha_phy_update_field((pcie_phy)->pma0 + (reg), (mask), (val))
#define airoha_phy_pma1_update_field(pcie_phy, reg, mask, val) \
airoha_phy_update_field((pcie_phy)->pma1 + (reg), (mask), (val))
static void
airoha_phy_init_lane0_rx_fw_pre_calib(struct airoha_pcie_phy *pcie_phy,
enum airoha_pcie_port_gen gen)
{
u32 fl_out_target = gen == PCIE_PORT_GEN3 ? 41600 : 41941;
u32 lock_cyclecnt = gen == PCIE_PORT_GEN3 ? 26000 : 32767;
u32 pr_idac, val, cdr_pr_idac_tmp = 0;
int i;
airoha_phy_pma0_set_bits(pcie_phy,
REG_PCIE_PMA_SS_LCPLL_PWCTL_SETTING_1,
PCIE_LCPLL_MAN_PWDB);
airoha_phy_pma0_update_field(pcie_phy, REG_PCIE_PMA_SS_RX_FREQ_DET2,
PCIE_LOCK_TARGET_BEG,
fl_out_target - 100);
airoha_phy_pma0_update_field(pcie_phy, REG_PCIE_PMA_SS_RX_FREQ_DET2,
PCIE_LOCK_TARGET_END,
fl_out_target + 100);
airoha_phy_pma0_update_field(pcie_phy, REG_PCIE_PMA_SS_RX_FREQ_DET1,
PCIE_PLL_FT_LOCK_CYCLECNT, lock_cyclecnt);
airoha_phy_pma0_update_field(pcie_phy, REG_PCIE_PMA_SS_RX_FREQ_DET4,
PCIE_LOCK_LOCKTH, 0x3);
airoha_phy_pma0_update_field(pcie_phy, REG_PCIE_PMA_SS_RX_FREQ_DET3,
PCIE_UNLOCK_TARGET_BEG