18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/**
38c2ecf20Sopenharmony_ci * PCIe SERDES driver for AM654x SoC
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2018 - 2019 Texas Instruments Incorporated - http://www.ti.com/
68c2ecf20Sopenharmony_ci * Author: Kishon Vijay Abraham I <kishon@ti.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <dt-bindings/phy/phy.h>
108c2ecf20Sopenharmony_ci#include <linux/clk.h>
118c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
128c2ecf20Sopenharmony_ci#include <linux/delay.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
158c2ecf20Sopenharmony_ci#include <linux/mux/consumer.h>
168c2ecf20Sopenharmony_ci#include <linux/of_address.h>
178c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
188c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
198c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
208c2ecf20Sopenharmony_ci#include <linux/regmap.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define CMU_R004		0x4
238c2ecf20Sopenharmony_ci#define CMU_R060		0x60
248c2ecf20Sopenharmony_ci#define CMU_R07C		0x7c
258c2ecf20Sopenharmony_ci#define CMU_R088		0x88
268c2ecf20Sopenharmony_ci#define CMU_R0D0		0xd0
278c2ecf20Sopenharmony_ci#define CMU_R0E8		0xe8
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define LANE_R048		0x248
308c2ecf20Sopenharmony_ci#define LANE_R058		0x258
318c2ecf20Sopenharmony_ci#define LANE_R06c		0x26c
328c2ecf20Sopenharmony_ci#define LANE_R070		0x270
338c2ecf20Sopenharmony_ci#define LANE_R070		0x270
348c2ecf20Sopenharmony_ci#define LANE_R19C		0x39c
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define COMLANE_R004		0xa04
378c2ecf20Sopenharmony_ci#define COMLANE_R138		0xb38
388c2ecf20Sopenharmony_ci#define VERSION_VAL		0x70
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define COMLANE_R190		0xb90
418c2ecf20Sopenharmony_ci#define COMLANE_R194		0xb94
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define COMRXEQ_R004		0x1404
448c2ecf20Sopenharmony_ci#define COMRXEQ_R008		0x1408
458c2ecf20Sopenharmony_ci#define COMRXEQ_R00C		0x140c
468c2ecf20Sopenharmony_ci#define COMRXEQ_R014		0x1414
478c2ecf20Sopenharmony_ci#define COMRXEQ_R018		0x1418
488c2ecf20Sopenharmony_ci#define COMRXEQ_R01C		0x141c
498c2ecf20Sopenharmony_ci#define COMRXEQ_R04C		0x144c
508c2ecf20Sopenharmony_ci#define COMRXEQ_R088		0x1488
518c2ecf20Sopenharmony_ci#define COMRXEQ_R094		0x1494
528c2ecf20Sopenharmony_ci#define COMRXEQ_R098		0x1498
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define SERDES_CTRL		0x1fd0
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define WIZ_LANEXCTL_STS	0x1fe0
578c2ecf20Sopenharmony_ci#define TX0_DISABLE_STATE	0x4
588c2ecf20Sopenharmony_ci#define TX0_SLEEP_STATE		0x5
598c2ecf20Sopenharmony_ci#define TX0_SNOOZE_STATE	0x6
608c2ecf20Sopenharmony_ci#define TX0_ENABLE_STATE	0x7
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define RX0_DISABLE_STATE	0x4
638c2ecf20Sopenharmony_ci#define RX0_SLEEP_STATE		0x5
648c2ecf20Sopenharmony_ci#define RX0_SNOOZE_STATE	0x6
658c2ecf20Sopenharmony_ci#define RX0_ENABLE_STATE	0x7
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define WIZ_PLL_CTRL		0x1ff4
688c2ecf20Sopenharmony_ci#define PLL_DISABLE_STATE	0x4
698c2ecf20Sopenharmony_ci#define PLL_SLEEP_STATE		0x5
708c2ecf20Sopenharmony_ci#define PLL_SNOOZE_STATE	0x6
718c2ecf20Sopenharmony_ci#define PLL_ENABLE_STATE	0x7
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define PLL_LOCK_TIME		100000	/* in microseconds */
748c2ecf20Sopenharmony_ci#define SLEEP_TIME		100	/* in microseconds */
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#define LANE_USB3		0x0
778c2ecf20Sopenharmony_ci#define LANE_PCIE0_LANE0	0x1
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define LANE_PCIE1_LANE0	0x0
808c2ecf20Sopenharmony_ci#define LANE_PCIE0_LANE1	0x1
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define SERDES_NUM_CLOCKS	3
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define AM654_SERDES_CTRL_CLKSEL_MASK	GENMASK(7, 4)
858c2ecf20Sopenharmony_ci#define AM654_SERDES_CTRL_CLKSEL_SHIFT	4
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistruct serdes_am654_clk_mux {
888c2ecf20Sopenharmony_ci	struct clk_hw	hw;
898c2ecf20Sopenharmony_ci	struct regmap	*regmap;
908c2ecf20Sopenharmony_ci	unsigned int	reg;
918c2ecf20Sopenharmony_ci	int		clk_id;
928c2ecf20Sopenharmony_ci	struct clk_init_data clk_data;
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#define to_serdes_am654_clk_mux(_hw)	\
968c2ecf20Sopenharmony_ci		container_of(_hw, struct serdes_am654_clk_mux, hw)
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic const struct regmap_config serdes_am654_regmap_config = {
998c2ecf20Sopenharmony_ci	.reg_bits = 32,
1008c2ecf20Sopenharmony_ci	.val_bits = 32,
1018c2ecf20Sopenharmony_ci	.reg_stride = 4,
1028c2ecf20Sopenharmony_ci	.fast_io = true,
1038c2ecf20Sopenharmony_ci	.max_register = 0x1ffc,
1048c2ecf20Sopenharmony_ci};
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cienum serdes_am654_fields {
1078c2ecf20Sopenharmony_ci	/* CMU PLL Control */
1088c2ecf20Sopenharmony_ci	CMU_PLL_CTRL,
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	LANE_PLL_CTRL_RXEQ_RXIDLE,
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	/* CMU VCO bias current and VREG setting */
1138c2ecf20Sopenharmony_ci	AHB_PMA_CM_VCO_VBIAS_VREG,
1148c2ecf20Sopenharmony_ci	AHB_PMA_CM_VCO_BIAS_VREG,
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	AHB_PMA_CM_SR,
1178c2ecf20Sopenharmony_ci	AHB_SSC_GEN_Z_O_20_13,
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	/* AHB PMA Lane Configuration */
1208c2ecf20Sopenharmony_ci	AHB_PMA_LN_AGC_THSEL_VREGH,
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	/* AGC and Signal detect threshold for Gen3 */
1238c2ecf20Sopenharmony_ci	AHB_PMA_LN_GEN3_AGC_SD_THSEL,
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	AHB_PMA_LN_RX_SELR_GEN3,
1268c2ecf20Sopenharmony_ci	AHB_PMA_LN_TX_DRV,
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	/* CMU Master Reset */
1298c2ecf20Sopenharmony_ci	CMU_MASTER_CDN,
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	/* P2S ring buffer initial startup pointer difference */
1328c2ecf20Sopenharmony_ci	P2S_RBUF_PTR_DIFF,
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	CONFIG_VERSION,
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	/* Lane 1 Master Reset */
1378c2ecf20Sopenharmony_ci	L1_MASTER_CDN,
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	/* CMU OK Status */
1408c2ecf20Sopenharmony_ci	CMU_OK_I_0,
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	/* Mid-speed initial calibration control */
1438c2ecf20Sopenharmony_ci	COMRXEQ_MS_INIT_CTRL_7_0,
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/* High-speed initial calibration control */
1468c2ecf20Sopenharmony_ci	COMRXEQ_HS_INIT_CAL_7_0,
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	/* Mid-speed recalibration control */
1498c2ecf20Sopenharmony_ci	COMRXEQ_MS_RECAL_CTRL_7_0,
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	/* High-speed recalibration control */
1528c2ecf20Sopenharmony_ci	COMRXEQ_HS_RECAL_CTRL_7_0,
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	/* ATT configuration */
1558c2ecf20Sopenharmony_ci	COMRXEQ_CSR_ATT_CONFIG,
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	/* Edge based boost adaptation window length */
1588c2ecf20Sopenharmony_ci	COMRXEQ_CSR_EBSTADAPT_WIN_LEN,
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	/* COMRXEQ control 3 & 4 */
1618c2ecf20Sopenharmony_ci	COMRXEQ_CTRL_3_4,
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	/* COMRXEQ control 14, 15 and 16*/
1648c2ecf20Sopenharmony_ci	COMRXEQ_CTRL_14_15_16,
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	/* Threshold for errors in pattern data  */
1678c2ecf20Sopenharmony_ci	COMRXEQ_CSR_DLEV_ERR_THRESH,
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	/* COMRXEQ control 25 */
1708c2ecf20Sopenharmony_ci	COMRXEQ_CTRL_25,
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	/* Mid-speed rate change calibration control */
1738c2ecf20Sopenharmony_ci	CSR_RXEQ_RATE_CHANGE_CAL_RUN_RATE2_O,
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	/* High-speed rate change calibration control */
1768c2ecf20Sopenharmony_ci	COMRXEQ_HS_RCHANGE_CTRL_7_0,
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	/* Serdes reset */
1798c2ecf20Sopenharmony_ci	POR_EN,
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	/* Tx Enable Value */
1828c2ecf20Sopenharmony_ci	TX0_ENABLE,
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	/* Rx Enable Value */
1858c2ecf20Sopenharmony_ci	RX0_ENABLE,
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	/* PLL Enable Value */
1888c2ecf20Sopenharmony_ci	PLL_ENABLE,
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	/* PLL ready for use */
1918c2ecf20Sopenharmony_ci	PLL_OK,
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	/* sentinel */
1948c2ecf20Sopenharmony_ci	MAX_FIELDS
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci};
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic const struct reg_field serdes_am654_reg_fields[] = {
1998c2ecf20Sopenharmony_ci	[CMU_PLL_CTRL]			= REG_FIELD(CMU_R004, 8, 15),
2008c2ecf20Sopenharmony_ci	[AHB_PMA_CM_VCO_VBIAS_VREG]	= REG_FIELD(CMU_R060, 8, 15),
2018c2ecf20Sopenharmony_ci	[CMU_MASTER_CDN]		= REG_FIELD(CMU_R07C, 24, 31),
2028c2ecf20Sopenharmony_ci	[AHB_PMA_CM_VCO_BIAS_VREG]	= REG_FIELD(CMU_R088, 24, 31),
2038c2ecf20Sopenharmony_ci	[AHB_PMA_CM_SR]			= REG_FIELD(CMU_R0D0, 24, 31),
2048c2ecf20Sopenharmony_ci	[AHB_SSC_GEN_Z_O_20_13]		= REG_FIELD(CMU_R0E8, 8, 15),
2058c2ecf20Sopenharmony_ci	[LANE_PLL_CTRL_RXEQ_RXIDLE]	= REG_FIELD(LANE_R048, 8, 15),
2068c2ecf20Sopenharmony_ci	[AHB_PMA_LN_AGC_THSEL_VREGH]	= REG_FIELD(LANE_R058, 16, 23),
2078c2ecf20Sopenharmony_ci	[AHB_PMA_LN_GEN3_AGC_SD_THSEL]	= REG_FIELD(LANE_R06c, 0, 7),
2088c2ecf20Sopenharmony_ci	[AHB_PMA_LN_RX_SELR_GEN3]	= REG_FIELD(LANE_R070, 16, 23),
2098c2ecf20Sopenharmony_ci	[AHB_PMA_LN_TX_DRV]		= REG_FIELD(LANE_R19C, 16, 23),
2108c2ecf20Sopenharmony_ci	[P2S_RBUF_PTR_DIFF]		= REG_FIELD(COMLANE_R004, 0, 7),
2118c2ecf20Sopenharmony_ci	[CONFIG_VERSION]		= REG_FIELD(COMLANE_R138, 16, 23),
2128c2ecf20Sopenharmony_ci	[L1_MASTER_CDN]			= REG_FIELD(COMLANE_R190, 8, 15),
2138c2ecf20Sopenharmony_ci	[CMU_OK_I_0]			= REG_FIELD(COMLANE_R194, 19, 19),
2148c2ecf20Sopenharmony_ci	[COMRXEQ_MS_INIT_CTRL_7_0]	= REG_FIELD(COMRXEQ_R004, 24, 31),
2158c2ecf20Sopenharmony_ci	[COMRXEQ_HS_INIT_CAL_7_0]	= REG_FIELD(COMRXEQ_R008, 0, 7),
2168c2ecf20Sopenharmony_ci	[COMRXEQ_MS_RECAL_CTRL_7_0]	= REG_FIELD(COMRXEQ_R00C, 8, 15),
2178c2ecf20Sopenharmony_ci	[COMRXEQ_HS_RECAL_CTRL_7_0]	= REG_FIELD(COMRXEQ_R00C, 16, 23),
2188c2ecf20Sopenharmony_ci	[COMRXEQ_CSR_ATT_CONFIG]	= REG_FIELD(COMRXEQ_R014, 16, 23),
2198c2ecf20Sopenharmony_ci	[COMRXEQ_CSR_EBSTADAPT_WIN_LEN]	= REG_FIELD(COMRXEQ_R018, 16, 23),
2208c2ecf20Sopenharmony_ci	[COMRXEQ_CTRL_3_4]		= REG_FIELD(COMRXEQ_R01C, 8, 15),
2218c2ecf20Sopenharmony_ci	[COMRXEQ_CTRL_14_15_16]		= REG_FIELD(COMRXEQ_R04C, 0, 7),
2228c2ecf20Sopenharmony_ci	[COMRXEQ_CSR_DLEV_ERR_THRESH]	= REG_FIELD(COMRXEQ_R088, 16, 23),
2238c2ecf20Sopenharmony_ci	[COMRXEQ_CTRL_25]		= REG_FIELD(COMRXEQ_R094, 24, 31),
2248c2ecf20Sopenharmony_ci	[CSR_RXEQ_RATE_CHANGE_CAL_RUN_RATE2_O] = REG_FIELD(COMRXEQ_R098, 8, 15),
2258c2ecf20Sopenharmony_ci	[COMRXEQ_HS_RCHANGE_CTRL_7_0]	= REG_FIELD(COMRXEQ_R098, 16, 23),
2268c2ecf20Sopenharmony_ci	[POR_EN]			= REG_FIELD(SERDES_CTRL, 29, 29),
2278c2ecf20Sopenharmony_ci	[TX0_ENABLE]			= REG_FIELD(WIZ_LANEXCTL_STS, 29, 31),
2288c2ecf20Sopenharmony_ci	[RX0_ENABLE]			= REG_FIELD(WIZ_LANEXCTL_STS, 13, 15),
2298c2ecf20Sopenharmony_ci	[PLL_ENABLE]			= REG_FIELD(WIZ_PLL_CTRL, 29, 31),
2308c2ecf20Sopenharmony_ci	[PLL_OK]			= REG_FIELD(WIZ_PLL_CTRL, 28, 28),
2318c2ecf20Sopenharmony_ci};
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistruct serdes_am654 {
2348c2ecf20Sopenharmony_ci	struct regmap		*regmap;
2358c2ecf20Sopenharmony_ci	struct regmap_field	*fields[MAX_FIELDS];
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	struct device		*dev;
2388c2ecf20Sopenharmony_ci	struct mux_control	*control;
2398c2ecf20Sopenharmony_ci	bool			busy;
2408c2ecf20Sopenharmony_ci	u32			type;
2418c2ecf20Sopenharmony_ci	struct device_node	*of_node;
2428c2ecf20Sopenharmony_ci	struct clk_onecell_data	clk_data;
2438c2ecf20Sopenharmony_ci	struct clk		*clks[SERDES_NUM_CLOCKS];
2448c2ecf20Sopenharmony_ci};
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic int serdes_am654_enable_pll(struct serdes_am654 *phy)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	int ret;
2498c2ecf20Sopenharmony_ci	u32 val;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	ret = regmap_field_write(phy->fields[PLL_ENABLE], PLL_ENABLE_STATE);
2528c2ecf20Sopenharmony_ci	if (ret)
2538c2ecf20Sopenharmony_ci		return ret;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	return regmap_field_read_poll_timeout(phy->fields[PLL_OK], val, val,
2568c2ecf20Sopenharmony_ci					      1000, PLL_LOCK_TIME);
2578c2ecf20Sopenharmony_ci}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_cistatic void serdes_am654_disable_pll(struct serdes_am654 *phy)
2608c2ecf20Sopenharmony_ci{
2618c2ecf20Sopenharmony_ci	struct device *dev = phy->dev;
2628c2ecf20Sopenharmony_ci	int ret;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	ret = regmap_field_write(phy->fields[PLL_ENABLE], PLL_DISABLE_STATE);
2658c2ecf20Sopenharmony_ci	if (ret)
2668c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to disable PLL\n");
2678c2ecf20Sopenharmony_ci}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cistatic int serdes_am654_enable_txrx(struct serdes_am654 *phy)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	int ret = 0;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	/* Enable TX */
2748c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[TX0_ENABLE], TX0_ENABLE_STATE);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	/* Enable RX */
2778c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[RX0_ENABLE], RX0_ENABLE_STATE);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	if (ret)
2808c2ecf20Sopenharmony_ci		return -EIO;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	return 0;
2838c2ecf20Sopenharmony_ci}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_cistatic int serdes_am654_disable_txrx(struct serdes_am654 *phy)
2868c2ecf20Sopenharmony_ci{
2878c2ecf20Sopenharmony_ci	int ret = 0;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	/* Disable TX */
2908c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[TX0_ENABLE], TX0_DISABLE_STATE);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	/* Disable RX */
2938c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[RX0_ENABLE], RX0_DISABLE_STATE);
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	if (ret)
2968c2ecf20Sopenharmony_ci		return -EIO;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	return 0;
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cistatic int serdes_am654_power_on(struct phy *x)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	struct serdes_am654 *phy = phy_get_drvdata(x);
3048c2ecf20Sopenharmony_ci	struct device *dev = phy->dev;
3058c2ecf20Sopenharmony_ci	int ret;
3068c2ecf20Sopenharmony_ci	u32 val;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	ret = serdes_am654_enable_pll(phy);
3098c2ecf20Sopenharmony_ci	if (ret) {
3108c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to enable PLL\n");
3118c2ecf20Sopenharmony_ci		return ret;
3128c2ecf20Sopenharmony_ci	}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	ret = serdes_am654_enable_txrx(phy);
3158c2ecf20Sopenharmony_ci	if (ret) {
3168c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to enable TX RX\n");
3178c2ecf20Sopenharmony_ci		return ret;
3188c2ecf20Sopenharmony_ci	}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	return regmap_field_read_poll_timeout(phy->fields[CMU_OK_I_0], val,
3218c2ecf20Sopenharmony_ci					      val, SLEEP_TIME, PLL_LOCK_TIME);
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic int serdes_am654_power_off(struct phy *x)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	struct serdes_am654 *phy = phy_get_drvdata(x);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	serdes_am654_disable_txrx(phy);
3298c2ecf20Sopenharmony_ci	serdes_am654_disable_pll(phy);
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	return 0;
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci#define SERDES_AM654_CFG(offset, a, b, val) \
3358c2ecf20Sopenharmony_ci	regmap_update_bits(phy->regmap, (offset),\
3368c2ecf20Sopenharmony_ci			   GENMASK((a), (b)), (val) << (b))
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_cistatic int serdes_am654_usb3_init(struct serdes_am654 *phy)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0000, 31, 24, 0x17);
3418c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0004, 15, 8, 0x02);
3428c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0004, 7, 0, 0x0e);
3438c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0008, 23, 16, 0x2e);
3448c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0008, 31, 24, 0x2e);
3458c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0060, 7, 0, 0x4b);
3468c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0060, 15, 8, 0x98);
3478c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0060, 23, 16, 0x60);
3488c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x00d0, 31, 24, 0x45);
3498c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x00e8, 15, 8, 0x0e);
3508c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0220, 7, 0, 0x34);
3518c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0220, 15, 8, 0x34);
3528c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0220, 31, 24, 0x37);
3538c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0224, 7, 0, 0x37);
3548c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0224, 15, 8, 0x37);
3558c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0228, 23, 16, 0x37);
3568c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0228, 31, 24, 0x37);
3578c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x022c, 7, 0, 0x37);
3588c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x022c, 15, 8, 0x37);
3598c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0230, 15, 8, 0x2a);
3608c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0230, 23, 16, 0x2a);
3618c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0240, 23, 16, 0x10);
3628c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0240, 31, 24, 0x34);
3638c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0244, 7, 0, 0x40);
3648c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0244, 23, 16, 0x34);
3658c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0248, 15, 8, 0x0d);
3668c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0258, 15, 8, 0x16);
3678c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0258, 23, 16, 0x84);
3688c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0258, 31, 24, 0xf2);
3698c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x025c, 7, 0, 0x21);
3708c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0260, 7, 0, 0x27);
3718c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0260, 15, 8, 0x04);
3728c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0268, 15, 8, 0x04);
3738c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0288, 15, 8, 0x2c);
3748c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0330, 31, 24, 0xa0);
3758c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0338, 23, 16, 0x03);
3768c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0338, 31, 24, 0x00);
3778c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x033c, 7, 0, 0x00);
3788c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0344, 31, 24, 0x18);
3798c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x034c, 7, 0, 0x18);
3808c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x039c, 23, 16, 0x3b);
3818c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0a04, 7, 0, 0x03);
3828c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0a14, 31, 24, 0x3c);
3838c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0a18, 15, 8, 0x3c);
3848c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0a38, 7, 0, 0x3e);
3858c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0a38, 15, 8, 0x3e);
3868c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0ae0, 7, 0, 0x07);
3878c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0b6c, 23, 16, 0xcd);
3888c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0b6c, 31, 24, 0x04);
3898c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0b98, 23, 16, 0x03);
3908c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1400, 7, 0, 0x3f);
3918c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1404, 23, 16, 0x6f);
3928c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1404, 31, 24, 0x6f);
3938c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x140c, 7, 0, 0x6f);
3948c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x140c, 15, 8, 0x6f);
3958c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1410, 15, 8, 0x27);
3968c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1414, 7, 0, 0x0c);
3978c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1414, 23, 16, 0x07);
3988c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1418, 23, 16, 0x40);
3998c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x141c, 7, 0, 0x00);
4008c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x141c, 15, 8, 0x1f);
4018c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1428, 31, 24, 0x08);
4028c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1434, 31, 24, 0x00);
4038c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1444, 7, 0, 0x94);
4048c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1460, 31, 24, 0x7f);
4058c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1464, 7, 0, 0x43);
4068c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1464, 23, 16, 0x6f);
4078c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1464, 31, 24, 0x43);
4088c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1484, 23, 16, 0x8f);
4098c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1498, 7, 0, 0x4f);
4108c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x1498, 23, 16, 0x4f);
4118c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x007c, 31, 24, 0x0d);
4128c2ecf20Sopenharmony_ci	SERDES_AM654_CFG(0x0b90, 15, 8, 0x0f);
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	return 0;
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_cistatic int serdes_am654_pcie_init(struct serdes_am654 *phy)
4188c2ecf20Sopenharmony_ci{
4198c2ecf20Sopenharmony_ci	int ret = 0;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[CMU_PLL_CTRL], 0x2);
4228c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[AHB_PMA_CM_VCO_VBIAS_VREG], 0x98);
4238c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[AHB_PMA_CM_VCO_BIAS_VREG], 0x98);
4248c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[AHB_PMA_CM_SR], 0x45);
4258c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[AHB_SSC_GEN_Z_O_20_13], 0xe);
4268c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[LANE_PLL_CTRL_RXEQ_RXIDLE], 0x5);
4278c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[AHB_PMA_LN_AGC_THSEL_VREGH], 0x83);
4288c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[AHB_PMA_LN_GEN3_AGC_SD_THSEL], 0x83);
4298c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[AHB_PMA_LN_RX_SELR_GEN3],	0x81);
4308c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[AHB_PMA_LN_TX_DRV], 0x3b);
4318c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[P2S_RBUF_PTR_DIFF], 0x3);
4328c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[CONFIG_VERSION], VERSION_VAL);
4338c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[COMRXEQ_MS_INIT_CTRL_7_0], 0xf);
4348c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[COMRXEQ_HS_INIT_CAL_7_0], 0x4f);
4358c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[COMRXEQ_MS_RECAL_CTRL_7_0], 0xf);
4368c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[COMRXEQ_HS_RECAL_CTRL_7_0], 0x4f);
4378c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[COMRXEQ_CSR_ATT_CONFIG], 0x7);
4388c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[COMRXEQ_CSR_EBSTADAPT_WIN_LEN], 0x7f);
4398c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[COMRXEQ_CTRL_3_4], 0xf);
4408c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[COMRXEQ_CTRL_14_15_16], 0x9a);
4418c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[COMRXEQ_CSR_DLEV_ERR_THRESH], 0x32);
4428c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[COMRXEQ_CTRL_25], 0x80);
4438c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[CSR_RXEQ_RATE_CHANGE_CAL_RUN_RATE2_O], 0xf);
4448c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[COMRXEQ_HS_RCHANGE_CTRL_7_0], 0x4f);
4458c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[CMU_MASTER_CDN], 0x1);
4468c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[L1_MASTER_CDN], 0x2);
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	if (ret)
4498c2ecf20Sopenharmony_ci		return -EIO;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	return 0;
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_cistatic int serdes_am654_init(struct phy *x)
4558c2ecf20Sopenharmony_ci{
4568c2ecf20Sopenharmony_ci	struct serdes_am654 *phy = phy_get_drvdata(x);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	switch (phy->type) {
4598c2ecf20Sopenharmony_ci	case PHY_TYPE_PCIE:
4608c2ecf20Sopenharmony_ci		return serdes_am654_pcie_init(phy);
4618c2ecf20Sopenharmony_ci	case PHY_TYPE_USB3:
4628c2ecf20Sopenharmony_ci		return serdes_am654_usb3_init(phy);
4638c2ecf20Sopenharmony_ci	default:
4648c2ecf20Sopenharmony_ci		return -EINVAL;
4658c2ecf20Sopenharmony_ci	}
4668c2ecf20Sopenharmony_ci}
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_cistatic int serdes_am654_reset(struct phy *x)
4698c2ecf20Sopenharmony_ci{
4708c2ecf20Sopenharmony_ci	struct serdes_am654 *phy = phy_get_drvdata(x);
4718c2ecf20Sopenharmony_ci	int ret = 0;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	serdes_am654_disable_pll(phy);
4748c2ecf20Sopenharmony_ci	serdes_am654_disable_txrx(phy);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[POR_EN], 0x1);
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	mdelay(1);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	ret |= regmap_field_write(phy->fields[POR_EN], 0x0);
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	if (ret)
4838c2ecf20Sopenharmony_ci		return -EIO;
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	return 0;
4868c2ecf20Sopenharmony_ci}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_cistatic void serdes_am654_release(struct phy *x)
4898c2ecf20Sopenharmony_ci{
4908c2ecf20Sopenharmony_ci	struct serdes_am654 *phy = phy_get_drvdata(x);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	phy->type = PHY_NONE;
4938c2ecf20Sopenharmony_ci	phy->busy = false;
4948c2ecf20Sopenharmony_ci	mux_control_deselect(phy->control);
4958c2ecf20Sopenharmony_ci}
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_cistatic struct phy *serdes_am654_xlate(struct device *dev,
4988c2ecf20Sopenharmony_ci				      struct of_phandle_args *args)
4998c2ecf20Sopenharmony_ci{
5008c2ecf20Sopenharmony_ci	struct serdes_am654 *am654_phy;
5018c2ecf20Sopenharmony_ci	struct phy *phy;
5028c2ecf20Sopenharmony_ci	int ret;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	phy = of_phy_simple_xlate(dev, args);
5058c2ecf20Sopenharmony_ci	if (IS_ERR(phy))
5068c2ecf20Sopenharmony_ci		return phy;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	am654_phy = phy_get_drvdata(phy);
5098c2ecf20Sopenharmony_ci	if (am654_phy->busy)
5108c2ecf20Sopenharmony_ci		return ERR_PTR(-EBUSY);
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	ret = mux_control_select(am654_phy->control, args->args[1]);
5138c2ecf20Sopenharmony_ci	if (ret) {
5148c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to select SERDES Lane Function\n");
5158c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
5168c2ecf20Sopenharmony_ci	}
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	am654_phy->busy = true;
5198c2ecf20Sopenharmony_ci	am654_phy->type = args->args[0];
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	return phy;
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_cistatic const struct phy_ops ops = {
5258c2ecf20Sopenharmony_ci	.reset		= serdes_am654_reset,
5268c2ecf20Sopenharmony_ci	.init		= serdes_am654_init,
5278c2ecf20Sopenharmony_ci	.power_on	= serdes_am654_power_on,
5288c2ecf20Sopenharmony_ci	.power_off	= serdes_am654_power_off,
5298c2ecf20Sopenharmony_ci	.release	= serdes_am654_release,
5308c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
5318c2ecf20Sopenharmony_ci};
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci#define SERDES_NUM_MUX_COMBINATIONS 16
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci#define LICLK 0
5368c2ecf20Sopenharmony_ci#define EXT_REFCLK 1
5378c2ecf20Sopenharmony_ci#define RICLK 2
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_cistatic const int
5408c2ecf20Sopenharmony_ciserdes_am654_mux_table[SERDES_NUM_MUX_COMBINATIONS][SERDES_NUM_CLOCKS] = {
5418c2ecf20Sopenharmony_ci	/*
5428c2ecf20Sopenharmony_ci	 * Each combination maps to one of
5438c2ecf20Sopenharmony_ci	 * "Figure 12-1986. SerDes Reference Clock Distribution"
5448c2ecf20Sopenharmony_ci	 * in TRM.
5458c2ecf20Sopenharmony_ci	 */
5468c2ecf20Sopenharmony_ci	 /* Parent of CMU refclk, Left output, Right output
5478c2ecf20Sopenharmony_ci	  * either of EXT_REFCLK, LICLK, RICLK
5488c2ecf20Sopenharmony_ci	  */
5498c2ecf20Sopenharmony_ci	{ EXT_REFCLK, EXT_REFCLK, EXT_REFCLK },	/* 0000 */
5508c2ecf20Sopenharmony_ci	{ RICLK, EXT_REFCLK, EXT_REFCLK },	/* 0001 */
5518c2ecf20Sopenharmony_ci	{ EXT_REFCLK, RICLK, LICLK },		/* 0010 */
5528c2ecf20Sopenharmony_ci	{ RICLK, RICLK, EXT_REFCLK },		/* 0011 */
5538c2ecf20Sopenharmony_ci	{ LICLK, EXT_REFCLK, EXT_REFCLK },	/* 0100 */
5548c2ecf20Sopenharmony_ci	{ EXT_REFCLK, EXT_REFCLK, EXT_REFCLK },	/* 0101 */
5558c2ecf20Sopenharmony_ci	{ LICLK, RICLK, LICLK },		/* 0110 */
5568c2ecf20Sopenharmony_ci	{ EXT_REFCLK, RICLK, LICLK },		/* 0111 */
5578c2ecf20Sopenharmony_ci	{ EXT_REFCLK, EXT_REFCLK, LICLK },	/* 1000 */
5588c2ecf20Sopenharmony_ci	{ RICLK, EXT_REFCLK, LICLK },		/* 1001 */
5598c2ecf20Sopenharmony_ci	{ EXT_REFCLK, RICLK, EXT_REFCLK },	/* 1010 */
5608c2ecf20Sopenharmony_ci	{ RICLK, RICLK, EXT_REFCLK },		/* 1011 */
5618c2ecf20Sopenharmony_ci	{ LICLK, EXT_REFCLK, LICLK },		/* 1100 */
5628c2ecf20Sopenharmony_ci	{ EXT_REFCLK, EXT_REFCLK, LICLK },	/* 1101 */
5638c2ecf20Sopenharmony_ci	{ LICLK, RICLK, EXT_REFCLK },		/* 1110 */
5648c2ecf20Sopenharmony_ci	{ EXT_REFCLK, RICLK, EXT_REFCLK },	/* 1111 */
5658c2ecf20Sopenharmony_ci};
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_cistatic u8 serdes_am654_clk_mux_get_parent(struct clk_hw *hw)
5688c2ecf20Sopenharmony_ci{
5698c2ecf20Sopenharmony_ci	struct serdes_am654_clk_mux *mux = to_serdes_am654_clk_mux(hw);
5708c2ecf20Sopenharmony_ci	struct regmap *regmap = mux->regmap;
5718c2ecf20Sopenharmony_ci	unsigned int reg = mux->reg;
5728c2ecf20Sopenharmony_ci	unsigned int val;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	regmap_read(regmap, reg, &val);
5758c2ecf20Sopenharmony_ci	val &= AM654_SERDES_CTRL_CLKSEL_MASK;
5768c2ecf20Sopenharmony_ci	val >>= AM654_SERDES_CTRL_CLKSEL_SHIFT;
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	return serdes_am654_mux_table[val][mux->clk_id];
5798c2ecf20Sopenharmony_ci}
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_cistatic int serdes_am654_clk_mux_set_parent(struct clk_hw *hw, u8 index)
5828c2ecf20Sopenharmony_ci{
5838c2ecf20Sopenharmony_ci	struct serdes_am654_clk_mux *mux = to_serdes_am654_clk_mux(hw);
5848c2ecf20Sopenharmony_ci	struct regmap *regmap = mux->regmap;
5858c2ecf20Sopenharmony_ci	const char *name = clk_hw_get_name(hw);
5868c2ecf20Sopenharmony_ci	unsigned int reg = mux->reg;
5878c2ecf20Sopenharmony_ci	int clk_id = mux->clk_id;
5888c2ecf20Sopenharmony_ci	int parents[SERDES_NUM_CLOCKS];
5898c2ecf20Sopenharmony_ci	const int *p;
5908c2ecf20Sopenharmony_ci	u32 val;
5918c2ecf20Sopenharmony_ci	int found, i;
5928c2ecf20Sopenharmony_ci	int ret;
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	/* get existing setting */
5958c2ecf20Sopenharmony_ci	regmap_read(regmap, reg, &val);
5968c2ecf20Sopenharmony_ci	val &= AM654_SERDES_CTRL_CLKSEL_MASK;
5978c2ecf20Sopenharmony_ci	val >>= AM654_SERDES_CTRL_CLKSEL_SHIFT;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	for (i = 0; i < SERDES_NUM_CLOCKS; i++)
6008c2ecf20Sopenharmony_ci		parents[i] = serdes_am654_mux_table[val][i];
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	/* change parent of this clock. others left intact */
6038c2ecf20Sopenharmony_ci	parents[clk_id] = index;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	/* Find the match */
6068c2ecf20Sopenharmony_ci	for (val = 0; val < SERDES_NUM_MUX_COMBINATIONS; val++) {
6078c2ecf20Sopenharmony_ci		p = serdes_am654_mux_table[val];
6088c2ecf20Sopenharmony_ci		found = 1;
6098c2ecf20Sopenharmony_ci		for (i = 0; i < SERDES_NUM_CLOCKS; i++) {
6108c2ecf20Sopenharmony_ci			if (parents[i] != p[i]) {
6118c2ecf20Sopenharmony_ci				found = 0;
6128c2ecf20Sopenharmony_ci				break;
6138c2ecf20Sopenharmony_ci			}
6148c2ecf20Sopenharmony_ci		}
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci		if (found)
6178c2ecf20Sopenharmony_ci			break;
6188c2ecf20Sopenharmony_ci	}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	if (!found) {
6218c2ecf20Sopenharmony_ci		/*
6228c2ecf20Sopenharmony_ci		 * This can never happen, unless we missed
6238c2ecf20Sopenharmony_ci		 * a valid combination in serdes_am654_mux_table.
6248c2ecf20Sopenharmony_ci		 */
6258c2ecf20Sopenharmony_ci		WARN(1, "Failed to find the parent of %s clock\n", name);
6268c2ecf20Sopenharmony_ci		return -EINVAL;
6278c2ecf20Sopenharmony_ci	}
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	val <<= AM654_SERDES_CTRL_CLKSEL_SHIFT;
6308c2ecf20Sopenharmony_ci	ret = regmap_update_bits(regmap, reg, AM654_SERDES_CTRL_CLKSEL_MASK,
6318c2ecf20Sopenharmony_ci				 val);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	return ret;
6348c2ecf20Sopenharmony_ci}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_cistatic const struct clk_ops serdes_am654_clk_mux_ops = {
6378c2ecf20Sopenharmony_ci	.set_parent = serdes_am654_clk_mux_set_parent,
6388c2ecf20Sopenharmony_ci	.get_parent = serdes_am654_clk_mux_get_parent,
6398c2ecf20Sopenharmony_ci};
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_cistatic int serdes_am654_clk_register(struct serdes_am654 *am654_phy,
6428c2ecf20Sopenharmony_ci				     const char *clock_name, int clock_num)
6438c2ecf20Sopenharmony_ci{
6448c2ecf20Sopenharmony_ci	struct device_node *node = am654_phy->of_node;
6458c2ecf20Sopenharmony_ci	struct device *dev = am654_phy->dev;
6468c2ecf20Sopenharmony_ci	struct serdes_am654_clk_mux *mux;
6478c2ecf20Sopenharmony_ci	struct device_node *regmap_node;
6488c2ecf20Sopenharmony_ci	const char **parent_names;
6498c2ecf20Sopenharmony_ci	struct clk_init_data *init;
6508c2ecf20Sopenharmony_ci	unsigned int num_parents;
6518c2ecf20Sopenharmony_ci	struct regmap *regmap;
6528c2ecf20Sopenharmony_ci	const __be32 *addr;
6538c2ecf20Sopenharmony_ci	unsigned int reg;
6548c2ecf20Sopenharmony_ci	struct clk *clk;
6558c2ecf20Sopenharmony_ci	int ret = 0;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
6588c2ecf20Sopenharmony_ci	if (!mux)
6598c2ecf20Sopenharmony_ci		return -ENOMEM;
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	init = &mux->clk_data;
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	regmap_node = of_parse_phandle(node, "ti,serdes-clk", 0);
6648c2ecf20Sopenharmony_ci	if (!regmap_node) {
6658c2ecf20Sopenharmony_ci		dev_err(dev, "Fail to get serdes-clk node\n");
6668c2ecf20Sopenharmony_ci		ret = -ENODEV;
6678c2ecf20Sopenharmony_ci		goto out_put_node;
6688c2ecf20Sopenharmony_ci	}
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	regmap = syscon_node_to_regmap(regmap_node->parent);
6718c2ecf20Sopenharmony_ci	if (IS_ERR(regmap)) {
6728c2ecf20Sopenharmony_ci		dev_err(dev, "Fail to get Syscon regmap\n");
6738c2ecf20Sopenharmony_ci		ret = PTR_ERR(regmap);
6748c2ecf20Sopenharmony_ci		goto out_put_node;
6758c2ecf20Sopenharmony_ci	}
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	num_parents = of_clk_get_parent_count(node);
6788c2ecf20Sopenharmony_ci	if (num_parents < 2) {
6798c2ecf20Sopenharmony_ci		dev_err(dev, "SERDES clock must have parents\n");
6808c2ecf20Sopenharmony_ci		ret = -EINVAL;
6818c2ecf20Sopenharmony_ci		goto out_put_node;
6828c2ecf20Sopenharmony_ci	}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	parent_names = devm_kzalloc(dev, (sizeof(char *) * num_parents),
6858c2ecf20Sopenharmony_ci				    GFP_KERNEL);
6868c2ecf20Sopenharmony_ci	if (!parent_names) {
6878c2ecf20Sopenharmony_ci		ret = -ENOMEM;
6888c2ecf20Sopenharmony_ci		goto out_put_node;
6898c2ecf20Sopenharmony_ci	}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	of_clk_parent_fill(node, parent_names, num_parents);
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	addr = of_get_address(regmap_node, 0, NULL, NULL);
6948c2ecf20Sopenharmony_ci	if (!addr) {
6958c2ecf20Sopenharmony_ci		ret = -EINVAL;
6968c2ecf20Sopenharmony_ci		goto out_put_node;
6978c2ecf20Sopenharmony_ci	}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	reg = be32_to_cpu(*addr);
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	init->ops = &serdes_am654_clk_mux_ops;
7028c2ecf20Sopenharmony_ci	init->flags = CLK_SET_RATE_NO_REPARENT;
7038c2ecf20Sopenharmony_ci	init->parent_names = parent_names;
7048c2ecf20Sopenharmony_ci	init->num_parents = num_parents;
7058c2ecf20Sopenharmony_ci	init->name = clock_name;
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	mux->regmap = regmap;
7088c2ecf20Sopenharmony_ci	mux->reg = reg;
7098c2ecf20Sopenharmony_ci	mux->clk_id = clock_num;
7108c2ecf20Sopenharmony_ci	mux->hw.init = init;
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	clk = devm_clk_register(dev, &mux->hw);
7138c2ecf20Sopenharmony_ci	if (IS_ERR(clk)) {
7148c2ecf20Sopenharmony_ci		ret = PTR_ERR(clk);
7158c2ecf20Sopenharmony_ci		goto out_put_node;
7168c2ecf20Sopenharmony_ci	}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	am654_phy->clks[clock_num] = clk;
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ciout_put_node:
7218c2ecf20Sopenharmony_ci	of_node_put(regmap_node);
7228c2ecf20Sopenharmony_ci	return ret;
7238c2ecf20Sopenharmony_ci}
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_cistatic const struct of_device_id serdes_am654_id_table[] = {
7268c2ecf20Sopenharmony_ci	{
7278c2ecf20Sopenharmony_ci		.compatible = "ti,phy-am654-serdes",
7288c2ecf20Sopenharmony_ci	},
7298c2ecf20Sopenharmony_ci	{}
7308c2ecf20Sopenharmony_ci};
7318c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, serdes_am654_id_table);
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_cistatic int serdes_am654_regfield_init(struct serdes_am654 *am654_phy)
7348c2ecf20Sopenharmony_ci{
7358c2ecf20Sopenharmony_ci	struct regmap *regmap = am654_phy->regmap;
7368c2ecf20Sopenharmony_ci	struct device *dev = am654_phy->dev;
7378c2ecf20Sopenharmony_ci	int i;
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_FIELDS; i++) {
7408c2ecf20Sopenharmony_ci		am654_phy->fields[i] = devm_regmap_field_alloc(dev,
7418c2ecf20Sopenharmony_ci							       regmap,
7428c2ecf20Sopenharmony_ci							       serdes_am654_reg_fields[i]);
7438c2ecf20Sopenharmony_ci		if (IS_ERR(am654_phy->fields[i])) {
7448c2ecf20Sopenharmony_ci			dev_err(dev, "Unable to allocate regmap field %d\n", i);
7458c2ecf20Sopenharmony_ci			return PTR_ERR(am654_phy->fields[i]);
7468c2ecf20Sopenharmony_ci		}
7478c2ecf20Sopenharmony_ci	}
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	return 0;
7508c2ecf20Sopenharmony_ci}
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_cistatic int serdes_am654_probe(struct platform_device *pdev)
7538c2ecf20Sopenharmony_ci{
7548c2ecf20Sopenharmony_ci	struct phy_provider *phy_provider;
7558c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
7568c2ecf20Sopenharmony_ci	struct device_node *node = dev->of_node;
7578c2ecf20Sopenharmony_ci	struct clk_onecell_data *clk_data;
7588c2ecf20Sopenharmony_ci	struct serdes_am654 *am654_phy;
7598c2ecf20Sopenharmony_ci	struct mux_control *control;
7608c2ecf20Sopenharmony_ci	const char *clock_name;
7618c2ecf20Sopenharmony_ci	struct regmap *regmap;
7628c2ecf20Sopenharmony_ci	void __iomem *base;
7638c2ecf20Sopenharmony_ci	struct phy *phy;
7648c2ecf20Sopenharmony_ci	int ret;
7658c2ecf20Sopenharmony_ci	int i;
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	am654_phy = devm_kzalloc(dev, sizeof(*am654_phy), GFP_KERNEL);
7688c2ecf20Sopenharmony_ci	if (!am654_phy)
7698c2ecf20Sopenharmony_ci		return -ENOMEM;
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	base = devm_platform_ioremap_resource(pdev, 0);
7728c2ecf20Sopenharmony_ci	if (IS_ERR(base))
7738c2ecf20Sopenharmony_ci		return PTR_ERR(base);
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	regmap = devm_regmap_init_mmio(dev, base, &serdes_am654_regmap_config);
7768c2ecf20Sopenharmony_ci	if (IS_ERR(regmap)) {
7778c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to initialize regmap\n");
7788c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
7798c2ecf20Sopenharmony_ci	}
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	control = devm_mux_control_get(dev, NULL);
7828c2ecf20Sopenharmony_ci	if (IS_ERR(control))
7838c2ecf20Sopenharmony_ci		return PTR_ERR(control);
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	am654_phy->dev = dev;
7868c2ecf20Sopenharmony_ci	am654_phy->of_node = node;
7878c2ecf20Sopenharmony_ci	am654_phy->regmap = regmap;
7888c2ecf20Sopenharmony_ci	am654_phy->control = control;
7898c2ecf20Sopenharmony_ci	am654_phy->type = PHY_NONE;
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	ret = serdes_am654_regfield_init(am654_phy);
7928c2ecf20Sopenharmony_ci	if (ret) {
7938c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to initialize regfields\n");
7948c2ecf20Sopenharmony_ci		return ret;
7958c2ecf20Sopenharmony_ci	}
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, am654_phy);
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	for (i = 0; i < SERDES_NUM_CLOCKS; i++) {
8008c2ecf20Sopenharmony_ci		ret = of_property_read_string_index(node, "clock-output-names",
8018c2ecf20Sopenharmony_ci						    i, &clock_name);
8028c2ecf20Sopenharmony_ci		if (ret) {
8038c2ecf20Sopenharmony_ci			dev_err(dev, "Failed to get clock name\n");
8048c2ecf20Sopenharmony_ci			return ret;
8058c2ecf20Sopenharmony_ci		}
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci		ret = serdes_am654_clk_register(am654_phy, clock_name, i);
8088c2ecf20Sopenharmony_ci		if (ret) {
8098c2ecf20Sopenharmony_ci			dev_err(dev, "Failed to initialize clock %s\n",
8108c2ecf20Sopenharmony_ci				clock_name);
8118c2ecf20Sopenharmony_ci			return ret;
8128c2ecf20Sopenharmony_ci		}
8138c2ecf20Sopenharmony_ci	}
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	clk_data = &am654_phy->clk_data;
8168c2ecf20Sopenharmony_ci	clk_data->clks = am654_phy->clks;
8178c2ecf20Sopenharmony_ci	clk_data->clk_num = SERDES_NUM_CLOCKS;
8188c2ecf20Sopenharmony_ci	ret = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
8198c2ecf20Sopenharmony_ci	if (ret)
8208c2ecf20Sopenharmony_ci		return ret;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	pm_runtime_enable(dev);
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	phy = devm_phy_create(dev, NULL, &ops);
8258c2ecf20Sopenharmony_ci	if (IS_ERR(phy)) {
8268c2ecf20Sopenharmony_ci		ret = PTR_ERR(phy);
8278c2ecf20Sopenharmony_ci		goto clk_err;
8288c2ecf20Sopenharmony_ci	}
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	phy_set_drvdata(phy, am654_phy);
8318c2ecf20Sopenharmony_ci	phy_provider = devm_of_phy_provider_register(dev, serdes_am654_xlate);
8328c2ecf20Sopenharmony_ci	if (IS_ERR(phy_provider)) {
8338c2ecf20Sopenharmony_ci		ret = PTR_ERR(phy_provider);
8348c2ecf20Sopenharmony_ci		goto clk_err;
8358c2ecf20Sopenharmony_ci	}
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	return 0;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ciclk_err:
8408c2ecf20Sopenharmony_ci	of_clk_del_provider(node);
8418c2ecf20Sopenharmony_ci	pm_runtime_disable(dev);
8428c2ecf20Sopenharmony_ci	return ret;
8438c2ecf20Sopenharmony_ci}
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_cistatic int serdes_am654_remove(struct platform_device *pdev)
8468c2ecf20Sopenharmony_ci{
8478c2ecf20Sopenharmony_ci	struct serdes_am654 *am654_phy = platform_get_drvdata(pdev);
8488c2ecf20Sopenharmony_ci	struct device_node *node = am654_phy->of_node;
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
8518c2ecf20Sopenharmony_ci	of_clk_del_provider(node);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	return 0;
8548c2ecf20Sopenharmony_ci}
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_cistatic struct platform_driver serdes_am654_driver = {
8578c2ecf20Sopenharmony_ci	.probe		= serdes_am654_probe,
8588c2ecf20Sopenharmony_ci	.remove		= serdes_am654_remove,
8598c2ecf20Sopenharmony_ci	.driver		= {
8608c2ecf20Sopenharmony_ci		.name	= "phy-am654",
8618c2ecf20Sopenharmony_ci		.of_match_table = serdes_am654_id_table,
8628c2ecf20Sopenharmony_ci	},
8638c2ecf20Sopenharmony_ci};
8648c2ecf20Sopenharmony_cimodule_platform_driver(serdes_am654_driver);
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ciMODULE_AUTHOR("Texas Instruments Inc.");
8678c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TI AM654x SERDES driver");
8688c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
869