18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: BSD-3-Clause
28c2ecf20Sopenharmony_ci/* Copyright (c) 2016-2018, NXP Semiconductors
38c2ecf20Sopenharmony_ci * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci#include <linux/packing.h>
68c2ecf20Sopenharmony_ci#include "sja1105.h"
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#define SJA1105_SIZE_CGU_CMD	4
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci/* Common structure for CFG_PAD_MIIx_RX and CFG_PAD_MIIx_TX */
118c2ecf20Sopenharmony_cistruct sja1105_cfg_pad_mii {
128c2ecf20Sopenharmony_ci	u64 d32_os;
138c2ecf20Sopenharmony_ci	u64 d32_ih;
148c2ecf20Sopenharmony_ci	u64 d32_ipud;
158c2ecf20Sopenharmony_ci	u64 d10_ih;
168c2ecf20Sopenharmony_ci	u64 d10_os;
178c2ecf20Sopenharmony_ci	u64 d10_ipud;
188c2ecf20Sopenharmony_ci	u64 ctrl_os;
198c2ecf20Sopenharmony_ci	u64 ctrl_ih;
208c2ecf20Sopenharmony_ci	u64 ctrl_ipud;
218c2ecf20Sopenharmony_ci	u64 clk_os;
228c2ecf20Sopenharmony_ci	u64 clk_ih;
238c2ecf20Sopenharmony_ci	u64 clk_ipud;
248c2ecf20Sopenharmony_ci};
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct sja1105_cfg_pad_mii_id {
278c2ecf20Sopenharmony_ci	u64 rxc_stable_ovr;
288c2ecf20Sopenharmony_ci	u64 rxc_delay;
298c2ecf20Sopenharmony_ci	u64 rxc_bypass;
308c2ecf20Sopenharmony_ci	u64 rxc_pd;
318c2ecf20Sopenharmony_ci	u64 txc_stable_ovr;
328c2ecf20Sopenharmony_ci	u64 txc_delay;
338c2ecf20Sopenharmony_ci	u64 txc_bypass;
348c2ecf20Sopenharmony_ci	u64 txc_pd;
358c2ecf20Sopenharmony_ci};
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* UM10944 Table 82.
388c2ecf20Sopenharmony_ci * IDIV_0_C to IDIV_4_C control registers
398c2ecf20Sopenharmony_ci * (addr. 10000Bh to 10000Fh)
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_cistruct sja1105_cgu_idiv {
428c2ecf20Sopenharmony_ci	u64 clksrc;
438c2ecf20Sopenharmony_ci	u64 autoblock;
448c2ecf20Sopenharmony_ci	u64 idiv;
458c2ecf20Sopenharmony_ci	u64 pd;
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/* PLL_1_C control register
498c2ecf20Sopenharmony_ci *
508c2ecf20Sopenharmony_ci * SJA1105 E/T: UM10944 Table 81 (address 10000Ah)
518c2ecf20Sopenharmony_ci * SJA1105 P/Q/R/S: UM11040 Table 116 (address 10000Ah)
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_cistruct sja1105_cgu_pll_ctrl {
548c2ecf20Sopenharmony_ci	u64 pllclksrc;
558c2ecf20Sopenharmony_ci	u64 msel;
568c2ecf20Sopenharmony_ci	u64 autoblock;
578c2ecf20Sopenharmony_ci	u64 psel;
588c2ecf20Sopenharmony_ci	u64 direct;
598c2ecf20Sopenharmony_ci	u64 fbsel;
608c2ecf20Sopenharmony_ci	u64 bypass;
618c2ecf20Sopenharmony_ci	u64 pd;
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cienum {
658c2ecf20Sopenharmony_ci	CLKSRC_MII0_TX_CLK	= 0x00,
668c2ecf20Sopenharmony_ci	CLKSRC_MII0_RX_CLK	= 0x01,
678c2ecf20Sopenharmony_ci	CLKSRC_MII1_TX_CLK	= 0x02,
688c2ecf20Sopenharmony_ci	CLKSRC_MII1_RX_CLK	= 0x03,
698c2ecf20Sopenharmony_ci	CLKSRC_MII2_TX_CLK	= 0x04,
708c2ecf20Sopenharmony_ci	CLKSRC_MII2_RX_CLK	= 0x05,
718c2ecf20Sopenharmony_ci	CLKSRC_MII3_TX_CLK	= 0x06,
728c2ecf20Sopenharmony_ci	CLKSRC_MII3_RX_CLK	= 0x07,
738c2ecf20Sopenharmony_ci	CLKSRC_MII4_TX_CLK	= 0x08,
748c2ecf20Sopenharmony_ci	CLKSRC_MII4_RX_CLK	= 0x09,
758c2ecf20Sopenharmony_ci	CLKSRC_PLL0		= 0x0B,
768c2ecf20Sopenharmony_ci	CLKSRC_PLL1		= 0x0E,
778c2ecf20Sopenharmony_ci	CLKSRC_IDIV0		= 0x11,
788c2ecf20Sopenharmony_ci	CLKSRC_IDIV1		= 0x12,
798c2ecf20Sopenharmony_ci	CLKSRC_IDIV2		= 0x13,
808c2ecf20Sopenharmony_ci	CLKSRC_IDIV3		= 0x14,
818c2ecf20Sopenharmony_ci	CLKSRC_IDIV4		= 0x15,
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/* UM10944 Table 83.
858c2ecf20Sopenharmony_ci * MIIx clock control registers 1 to 30
868c2ecf20Sopenharmony_ci * (addresses 100013h to 100035h)
878c2ecf20Sopenharmony_ci */
888c2ecf20Sopenharmony_cistruct sja1105_cgu_mii_ctrl {
898c2ecf20Sopenharmony_ci	u64 clksrc;
908c2ecf20Sopenharmony_ci	u64 autoblock;
918c2ecf20Sopenharmony_ci	u64 pd;
928c2ecf20Sopenharmony_ci};
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic void sja1105_cgu_idiv_packing(void *buf, struct sja1105_cgu_idiv *idiv,
958c2ecf20Sopenharmony_ci				     enum packing_op op)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	const int size = 4;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	sja1105_packing(buf, &idiv->clksrc,    28, 24, size, op);
1008c2ecf20Sopenharmony_ci	sja1105_packing(buf, &idiv->autoblock, 11, 11, size, op);
1018c2ecf20Sopenharmony_ci	sja1105_packing(buf, &idiv->idiv,       5,  2, size, op);
1028c2ecf20Sopenharmony_ci	sja1105_packing(buf, &idiv->pd,         0,  0, size, op);
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic int sja1105_cgu_idiv_config(struct sja1105_private *priv, int port,
1068c2ecf20Sopenharmony_ci				   bool enabled, int factor)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	const struct sja1105_regs *regs = priv->info->regs;
1098c2ecf20Sopenharmony_ci	struct device *dev = priv->ds->dev;
1108c2ecf20Sopenharmony_ci	struct sja1105_cgu_idiv idiv;
1118c2ecf20Sopenharmony_ci	u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	if (enabled && factor != 1 && factor != 10) {
1148c2ecf20Sopenharmony_ci		dev_err(dev, "idiv factor must be 1 or 10\n");
1158c2ecf20Sopenharmony_ci		return -ERANGE;
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	/* Payload for packed_buf */
1198c2ecf20Sopenharmony_ci	idiv.clksrc    = 0x0A;            /* 25MHz */
1208c2ecf20Sopenharmony_ci	idiv.autoblock = 1;               /* Block clk automatically */
1218c2ecf20Sopenharmony_ci	idiv.idiv      = factor - 1;      /* Divide by 1 or 10 */
1228c2ecf20Sopenharmony_ci	idiv.pd        = enabled ? 0 : 1; /* Power down? */
1238c2ecf20Sopenharmony_ci	sja1105_cgu_idiv_packing(packed_buf, &idiv, PACK);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	return sja1105_xfer_buf(priv, SPI_WRITE, regs->cgu_idiv[port],
1268c2ecf20Sopenharmony_ci				packed_buf, SJA1105_SIZE_CGU_CMD);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic void
1308c2ecf20Sopenharmony_cisja1105_cgu_mii_control_packing(void *buf, struct sja1105_cgu_mii_ctrl *cmd,
1318c2ecf20Sopenharmony_ci				enum packing_op op)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	const int size = 4;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->clksrc,    28, 24, size, op);
1368c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->autoblock, 11, 11, size, op);
1378c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->pd,         0,  0, size, op);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic int sja1105_cgu_mii_tx_clk_config(struct sja1105_private *priv,
1418c2ecf20Sopenharmony_ci					 int port, sja1105_mii_role_t role)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	const struct sja1105_regs *regs = priv->info->regs;
1448c2ecf20Sopenharmony_ci	struct sja1105_cgu_mii_ctrl mii_tx_clk;
1458c2ecf20Sopenharmony_ci	const int mac_clk_sources[] = {
1468c2ecf20Sopenharmony_ci		CLKSRC_MII0_TX_CLK,
1478c2ecf20Sopenharmony_ci		CLKSRC_MII1_TX_CLK,
1488c2ecf20Sopenharmony_ci		CLKSRC_MII2_TX_CLK,
1498c2ecf20Sopenharmony_ci		CLKSRC_MII3_TX_CLK,
1508c2ecf20Sopenharmony_ci		CLKSRC_MII4_TX_CLK,
1518c2ecf20Sopenharmony_ci	};
1528c2ecf20Sopenharmony_ci	const int phy_clk_sources[] = {
1538c2ecf20Sopenharmony_ci		CLKSRC_IDIV0,
1548c2ecf20Sopenharmony_ci		CLKSRC_IDIV1,
1558c2ecf20Sopenharmony_ci		CLKSRC_IDIV2,
1568c2ecf20Sopenharmony_ci		CLKSRC_IDIV3,
1578c2ecf20Sopenharmony_ci		CLKSRC_IDIV4,
1588c2ecf20Sopenharmony_ci	};
1598c2ecf20Sopenharmony_ci	u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
1608c2ecf20Sopenharmony_ci	int clksrc;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	if (role == XMII_MAC)
1638c2ecf20Sopenharmony_ci		clksrc = mac_clk_sources[port];
1648c2ecf20Sopenharmony_ci	else
1658c2ecf20Sopenharmony_ci		clksrc = phy_clk_sources[port];
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	/* Payload for packed_buf */
1688c2ecf20Sopenharmony_ci	mii_tx_clk.clksrc    = clksrc;
1698c2ecf20Sopenharmony_ci	mii_tx_clk.autoblock = 1;  /* Autoblock clk while changing clksrc */
1708c2ecf20Sopenharmony_ci	mii_tx_clk.pd        = 0;  /* Power Down off => enabled */
1718c2ecf20Sopenharmony_ci	sja1105_cgu_mii_control_packing(packed_buf, &mii_tx_clk, PACK);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	return sja1105_xfer_buf(priv, SPI_WRITE, regs->mii_tx_clk[port],
1748c2ecf20Sopenharmony_ci				packed_buf, SJA1105_SIZE_CGU_CMD);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic int
1788c2ecf20Sopenharmony_cisja1105_cgu_mii_rx_clk_config(struct sja1105_private *priv, int port)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	const struct sja1105_regs *regs = priv->info->regs;
1818c2ecf20Sopenharmony_ci	struct sja1105_cgu_mii_ctrl mii_rx_clk;
1828c2ecf20Sopenharmony_ci	u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
1838c2ecf20Sopenharmony_ci	const int clk_sources[] = {
1848c2ecf20Sopenharmony_ci		CLKSRC_MII0_RX_CLK,
1858c2ecf20Sopenharmony_ci		CLKSRC_MII1_RX_CLK,
1868c2ecf20Sopenharmony_ci		CLKSRC_MII2_RX_CLK,
1878c2ecf20Sopenharmony_ci		CLKSRC_MII3_RX_CLK,
1888c2ecf20Sopenharmony_ci		CLKSRC_MII4_RX_CLK,
1898c2ecf20Sopenharmony_ci	};
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	/* Payload for packed_buf */
1928c2ecf20Sopenharmony_ci	mii_rx_clk.clksrc    = clk_sources[port];
1938c2ecf20Sopenharmony_ci	mii_rx_clk.autoblock = 1;  /* Autoblock clk while changing clksrc */
1948c2ecf20Sopenharmony_ci	mii_rx_clk.pd        = 0;  /* Power Down off => enabled */
1958c2ecf20Sopenharmony_ci	sja1105_cgu_mii_control_packing(packed_buf, &mii_rx_clk, PACK);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	return sja1105_xfer_buf(priv, SPI_WRITE, regs->mii_rx_clk[port],
1988c2ecf20Sopenharmony_ci				packed_buf, SJA1105_SIZE_CGU_CMD);
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic int
2028c2ecf20Sopenharmony_cisja1105_cgu_mii_ext_tx_clk_config(struct sja1105_private *priv, int port)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	const struct sja1105_regs *regs = priv->info->regs;
2058c2ecf20Sopenharmony_ci	struct sja1105_cgu_mii_ctrl mii_ext_tx_clk;
2068c2ecf20Sopenharmony_ci	u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
2078c2ecf20Sopenharmony_ci	const int clk_sources[] = {
2088c2ecf20Sopenharmony_ci		CLKSRC_IDIV0,
2098c2ecf20Sopenharmony_ci		CLKSRC_IDIV1,
2108c2ecf20Sopenharmony_ci		CLKSRC_IDIV2,
2118c2ecf20Sopenharmony_ci		CLKSRC_IDIV3,
2128c2ecf20Sopenharmony_ci		CLKSRC_IDIV4,
2138c2ecf20Sopenharmony_ci	};
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	/* Payload for packed_buf */
2168c2ecf20Sopenharmony_ci	mii_ext_tx_clk.clksrc    = clk_sources[port];
2178c2ecf20Sopenharmony_ci	mii_ext_tx_clk.autoblock = 1; /* Autoblock clk while changing clksrc */
2188c2ecf20Sopenharmony_ci	mii_ext_tx_clk.pd        = 0; /* Power Down off => enabled */
2198c2ecf20Sopenharmony_ci	sja1105_cgu_mii_control_packing(packed_buf, &mii_ext_tx_clk, PACK);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	return sja1105_xfer_buf(priv, SPI_WRITE, regs->mii_ext_tx_clk[port],
2228c2ecf20Sopenharmony_ci				packed_buf, SJA1105_SIZE_CGU_CMD);
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic int
2268c2ecf20Sopenharmony_cisja1105_cgu_mii_ext_rx_clk_config(struct sja1105_private *priv, int port)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	const struct sja1105_regs *regs = priv->info->regs;
2298c2ecf20Sopenharmony_ci	struct sja1105_cgu_mii_ctrl mii_ext_rx_clk;
2308c2ecf20Sopenharmony_ci	u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
2318c2ecf20Sopenharmony_ci	const int clk_sources[] = {
2328c2ecf20Sopenharmony_ci		CLKSRC_IDIV0,
2338c2ecf20Sopenharmony_ci		CLKSRC_IDIV1,
2348c2ecf20Sopenharmony_ci		CLKSRC_IDIV2,
2358c2ecf20Sopenharmony_ci		CLKSRC_IDIV3,
2368c2ecf20Sopenharmony_ci		CLKSRC_IDIV4,
2378c2ecf20Sopenharmony_ci	};
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* Payload for packed_buf */
2408c2ecf20Sopenharmony_ci	mii_ext_rx_clk.clksrc    = clk_sources[port];
2418c2ecf20Sopenharmony_ci	mii_ext_rx_clk.autoblock = 1; /* Autoblock clk while changing clksrc */
2428c2ecf20Sopenharmony_ci	mii_ext_rx_clk.pd        = 0; /* Power Down off => enabled */
2438c2ecf20Sopenharmony_ci	sja1105_cgu_mii_control_packing(packed_buf, &mii_ext_rx_clk, PACK);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	return sja1105_xfer_buf(priv, SPI_WRITE, regs->mii_ext_rx_clk[port],
2468c2ecf20Sopenharmony_ci				packed_buf, SJA1105_SIZE_CGU_CMD);
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic int sja1105_mii_clocking_setup(struct sja1105_private *priv, int port,
2508c2ecf20Sopenharmony_ci				      sja1105_mii_role_t role)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	struct device *dev = priv->ds->dev;
2538c2ecf20Sopenharmony_ci	int rc;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	dev_dbg(dev, "Configuring MII-%s clocking\n",
2568c2ecf20Sopenharmony_ci		(role == XMII_MAC) ? "MAC" : "PHY");
2578c2ecf20Sopenharmony_ci	/* If role is MAC, disable IDIV
2588c2ecf20Sopenharmony_ci	 * If role is PHY, enable IDIV and configure for 1/1 divider
2598c2ecf20Sopenharmony_ci	 */
2608c2ecf20Sopenharmony_ci	rc = sja1105_cgu_idiv_config(priv, port, (role == XMII_PHY), 1);
2618c2ecf20Sopenharmony_ci	if (rc < 0)
2628c2ecf20Sopenharmony_ci		return rc;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	/* Configure CLKSRC of MII_TX_CLK_n
2658c2ecf20Sopenharmony_ci	 *   * If role is MAC, select TX_CLK_n
2668c2ecf20Sopenharmony_ci	 *   * If role is PHY, select IDIV_n
2678c2ecf20Sopenharmony_ci	 */
2688c2ecf20Sopenharmony_ci	rc = sja1105_cgu_mii_tx_clk_config(priv, port, role);
2698c2ecf20Sopenharmony_ci	if (rc < 0)
2708c2ecf20Sopenharmony_ci		return rc;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	/* Configure CLKSRC of MII_RX_CLK_n
2738c2ecf20Sopenharmony_ci	 * Select RX_CLK_n
2748c2ecf20Sopenharmony_ci	 */
2758c2ecf20Sopenharmony_ci	rc = sja1105_cgu_mii_rx_clk_config(priv, port);
2768c2ecf20Sopenharmony_ci	if (rc < 0)
2778c2ecf20Sopenharmony_ci		return rc;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	if (role == XMII_PHY) {
2808c2ecf20Sopenharmony_ci		/* Per MII spec, the PHY (which is us) drives the TX_CLK pin */
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci		/* Configure CLKSRC of EXT_TX_CLK_n
2838c2ecf20Sopenharmony_ci		 * Select IDIV_n
2848c2ecf20Sopenharmony_ci		 */
2858c2ecf20Sopenharmony_ci		rc = sja1105_cgu_mii_ext_tx_clk_config(priv, port);
2868c2ecf20Sopenharmony_ci		if (rc < 0)
2878c2ecf20Sopenharmony_ci			return rc;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci		/* Configure CLKSRC of EXT_RX_CLK_n
2908c2ecf20Sopenharmony_ci		 * Select IDIV_n
2918c2ecf20Sopenharmony_ci		 */
2928c2ecf20Sopenharmony_ci		rc = sja1105_cgu_mii_ext_rx_clk_config(priv, port);
2938c2ecf20Sopenharmony_ci		if (rc < 0)
2948c2ecf20Sopenharmony_ci			return rc;
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci	return 0;
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic void
3008c2ecf20Sopenharmony_cisja1105_cgu_pll_control_packing(void *buf, struct sja1105_cgu_pll_ctrl *cmd,
3018c2ecf20Sopenharmony_ci				enum packing_op op)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	const int size = 4;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->pllclksrc, 28, 24, size, op);
3068c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->msel,      23, 16, size, op);
3078c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->autoblock, 11, 11, size, op);
3088c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->psel,       9,  8, size, op);
3098c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->direct,     7,  7, size, op);
3108c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->fbsel,      6,  6, size, op);
3118c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->bypass,     1,  1, size, op);
3128c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->pd,         0,  0, size, op);
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_cistatic int sja1105_cgu_rgmii_tx_clk_config(struct sja1105_private *priv,
3168c2ecf20Sopenharmony_ci					   int port, sja1105_speed_t speed)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	const struct sja1105_regs *regs = priv->info->regs;
3198c2ecf20Sopenharmony_ci	struct sja1105_cgu_mii_ctrl txc;
3208c2ecf20Sopenharmony_ci	u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
3218c2ecf20Sopenharmony_ci	int clksrc;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	if (speed == SJA1105_SPEED_1000MBPS) {
3248c2ecf20Sopenharmony_ci		clksrc = CLKSRC_PLL0;
3258c2ecf20Sopenharmony_ci	} else {
3268c2ecf20Sopenharmony_ci		int clk_sources[] = {CLKSRC_IDIV0, CLKSRC_IDIV1, CLKSRC_IDIV2,
3278c2ecf20Sopenharmony_ci				     CLKSRC_IDIV3, CLKSRC_IDIV4};
3288c2ecf20Sopenharmony_ci		clksrc = clk_sources[port];
3298c2ecf20Sopenharmony_ci	}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	/* RGMII: 125MHz for 1000, 25MHz for 100, 2.5MHz for 10 */
3328c2ecf20Sopenharmony_ci	txc.clksrc = clksrc;
3338c2ecf20Sopenharmony_ci	/* Autoblock clk while changing clksrc */
3348c2ecf20Sopenharmony_ci	txc.autoblock = 1;
3358c2ecf20Sopenharmony_ci	/* Power Down off => enabled */
3368c2ecf20Sopenharmony_ci	txc.pd = 0;
3378c2ecf20Sopenharmony_ci	sja1105_cgu_mii_control_packing(packed_buf, &txc, PACK);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	return sja1105_xfer_buf(priv, SPI_WRITE, regs->rgmii_tx_clk[port],
3408c2ecf20Sopenharmony_ci				packed_buf, SJA1105_SIZE_CGU_CMD);
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci/* AGU */
3448c2ecf20Sopenharmony_cistatic void
3458c2ecf20Sopenharmony_cisja1105_cfg_pad_mii_packing(void *buf, struct sja1105_cfg_pad_mii *cmd,
3468c2ecf20Sopenharmony_ci			    enum packing_op op)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	const int size = 4;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->d32_os,   28, 27, size, op);
3518c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->d32_ih,   26, 26, size, op);
3528c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->d32_ipud, 25, 24, size, op);
3538c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->d10_os,   20, 19, size, op);
3548c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->d10_ih,   18, 18, size, op);
3558c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->d10_ipud, 17, 16, size, op);
3568c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->ctrl_os,  12, 11, size, op);
3578c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->ctrl_ih,  10, 10, size, op);
3588c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->ctrl_ipud, 9,  8, size, op);
3598c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->clk_os,    4,  3, size, op);
3608c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->clk_ih,    2,  2, size, op);
3618c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->clk_ipud,  1,  0, size, op);
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_cistatic int sja1105_rgmii_cfg_pad_tx_config(struct sja1105_private *priv,
3658c2ecf20Sopenharmony_ci					   int port)
3668c2ecf20Sopenharmony_ci{
3678c2ecf20Sopenharmony_ci	const struct sja1105_regs *regs = priv->info->regs;
3688c2ecf20Sopenharmony_ci	struct sja1105_cfg_pad_mii pad_mii_tx = {0};
3698c2ecf20Sopenharmony_ci	u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	/* Payload */
3728c2ecf20Sopenharmony_ci	pad_mii_tx.d32_os    = 3; /* TXD[3:2] output stage: */
3738c2ecf20Sopenharmony_ci				  /*          high noise/high speed */
3748c2ecf20Sopenharmony_ci	pad_mii_tx.d10_os    = 3; /* TXD[1:0] output stage: */
3758c2ecf20Sopenharmony_ci				  /*          high noise/high speed */
3768c2ecf20Sopenharmony_ci	pad_mii_tx.d32_ipud  = 2; /* TXD[3:2] input stage: */
3778c2ecf20Sopenharmony_ci				  /*          plain input (default) */
3788c2ecf20Sopenharmony_ci	pad_mii_tx.d10_ipud  = 2; /* TXD[1:0] input stage: */
3798c2ecf20Sopenharmony_ci				  /*          plain input (default) */
3808c2ecf20Sopenharmony_ci	pad_mii_tx.ctrl_os   = 3; /* TX_CTL / TX_ER output stage */
3818c2ecf20Sopenharmony_ci	pad_mii_tx.ctrl_ipud = 2; /* TX_CTL / TX_ER input stage (default) */
3828c2ecf20Sopenharmony_ci	pad_mii_tx.clk_os    = 3; /* TX_CLK output stage */
3838c2ecf20Sopenharmony_ci	pad_mii_tx.clk_ih    = 0; /* TX_CLK input hysteresis (default) */
3848c2ecf20Sopenharmony_ci	pad_mii_tx.clk_ipud  = 2; /* TX_CLK input stage (default) */
3858c2ecf20Sopenharmony_ci	sja1105_cfg_pad_mii_packing(packed_buf, &pad_mii_tx, PACK);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	return sja1105_xfer_buf(priv, SPI_WRITE, regs->pad_mii_tx[port],
3888c2ecf20Sopenharmony_ci				packed_buf, SJA1105_SIZE_CGU_CMD);
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic int sja1105_cfg_pad_rx_config(struct sja1105_private *priv, int port)
3928c2ecf20Sopenharmony_ci{
3938c2ecf20Sopenharmony_ci	const struct sja1105_regs *regs = priv->info->regs;
3948c2ecf20Sopenharmony_ci	struct sja1105_cfg_pad_mii pad_mii_rx = {0};
3958c2ecf20Sopenharmony_ci	u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	/* Payload */
3988c2ecf20Sopenharmony_ci	pad_mii_rx.d32_ih    = 0; /* RXD[3:2] input stage hysteresis: */
3998c2ecf20Sopenharmony_ci				  /*          non-Schmitt (default) */
4008c2ecf20Sopenharmony_ci	pad_mii_rx.d32_ipud  = 2; /* RXD[3:2] input weak pull-up/down */
4018c2ecf20Sopenharmony_ci				  /*          plain input (default) */
4028c2ecf20Sopenharmony_ci	pad_mii_rx.d10_ih    = 0; /* RXD[1:0] input stage hysteresis: */
4038c2ecf20Sopenharmony_ci				  /*          non-Schmitt (default) */
4048c2ecf20Sopenharmony_ci	pad_mii_rx.d10_ipud  = 2; /* RXD[1:0] input weak pull-up/down */
4058c2ecf20Sopenharmony_ci				  /*          plain input (default) */
4068c2ecf20Sopenharmony_ci	pad_mii_rx.ctrl_ih   = 0; /* RX_DV/CRS_DV/RX_CTL and RX_ER */
4078c2ecf20Sopenharmony_ci				  /* input stage hysteresis: */
4088c2ecf20Sopenharmony_ci				  /* non-Schmitt (default) */
4098c2ecf20Sopenharmony_ci	pad_mii_rx.ctrl_ipud = 3; /* RX_DV/CRS_DV/RX_CTL and RX_ER */
4108c2ecf20Sopenharmony_ci				  /* input stage weak pull-up/down: */
4118c2ecf20Sopenharmony_ci				  /* pull-down */
4128c2ecf20Sopenharmony_ci	pad_mii_rx.clk_os    = 2; /* RX_CLK/RXC output stage: */
4138c2ecf20Sopenharmony_ci				  /* medium noise/fast speed (default) */
4148c2ecf20Sopenharmony_ci	pad_mii_rx.clk_ih    = 0; /* RX_CLK/RXC input hysteresis: */
4158c2ecf20Sopenharmony_ci				  /* non-Schmitt (default) */
4168c2ecf20Sopenharmony_ci	pad_mii_rx.clk_ipud  = 2; /* RX_CLK/RXC input pull-up/down: */
4178c2ecf20Sopenharmony_ci				  /* plain input (default) */
4188c2ecf20Sopenharmony_ci	sja1105_cfg_pad_mii_packing(packed_buf, &pad_mii_rx, PACK);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	return sja1105_xfer_buf(priv, SPI_WRITE, regs->pad_mii_rx[port],
4218c2ecf20Sopenharmony_ci				packed_buf, SJA1105_SIZE_CGU_CMD);
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic void
4258c2ecf20Sopenharmony_cisja1105_cfg_pad_mii_id_packing(void *buf, struct sja1105_cfg_pad_mii_id *cmd,
4268c2ecf20Sopenharmony_ci			       enum packing_op op)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	const int size = SJA1105_SIZE_CGU_CMD;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->rxc_stable_ovr, 15, 15, size, op);
4318c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->rxc_delay,      14, 10, size, op);
4328c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->rxc_bypass,      9,  9, size, op);
4338c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->rxc_pd,          8,  8, size, op);
4348c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->txc_stable_ovr,  7,  7, size, op);
4358c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->txc_delay,       6,  2, size, op);
4368c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->txc_bypass,      1,  1, size, op);
4378c2ecf20Sopenharmony_ci	sja1105_packing(buf, &cmd->txc_pd,          0,  0, size, op);
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci/* Valid range in degrees is an integer between 73.8 and 101.7 */
4418c2ecf20Sopenharmony_cistatic u64 sja1105_rgmii_delay(u64 phase)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	/* UM11040.pdf: The delay in degree phase is 73.8 + delay_tune * 0.9.
4448c2ecf20Sopenharmony_ci	 * To avoid floating point operations we'll multiply by 10
4458c2ecf20Sopenharmony_ci	 * and get 1 decimal point precision.
4468c2ecf20Sopenharmony_ci	 */
4478c2ecf20Sopenharmony_ci	phase *= 10;
4488c2ecf20Sopenharmony_ci	return (phase - 738) / 9;
4498c2ecf20Sopenharmony_ci}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci/* The RGMII delay setup procedure is 2-step and gets called upon each
4528c2ecf20Sopenharmony_ci * .phylink_mac_config. Both are strategic.
4538c2ecf20Sopenharmony_ci * The reason is that the RX Tunable Delay Line of the SJA1105 MAC has issues
4548c2ecf20Sopenharmony_ci * with recovering from a frequency change of the link partner's RGMII clock.
4558c2ecf20Sopenharmony_ci * The easiest way to recover from this is to temporarily power down the TDL,
4568c2ecf20Sopenharmony_ci * as it will re-lock at the new frequency afterwards.
4578c2ecf20Sopenharmony_ci */
4588c2ecf20Sopenharmony_ciint sja1105pqrs_setup_rgmii_delay(const void *ctx, int port)
4598c2ecf20Sopenharmony_ci{
4608c2ecf20Sopenharmony_ci	const struct sja1105_private *priv = ctx;
4618c2ecf20Sopenharmony_ci	const struct sja1105_regs *regs = priv->info->regs;
4628c2ecf20Sopenharmony_ci	struct sja1105_cfg_pad_mii_id pad_mii_id = {0};
4638c2ecf20Sopenharmony_ci	u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
4648c2ecf20Sopenharmony_ci	int rc;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	if (priv->rgmii_rx_delay[port])
4678c2ecf20Sopenharmony_ci		pad_mii_id.rxc_delay = sja1105_rgmii_delay(90);
4688c2ecf20Sopenharmony_ci	if (priv->rgmii_tx_delay[port])
4698c2ecf20Sopenharmony_ci		pad_mii_id.txc_delay = sja1105_rgmii_delay(90);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	/* Stage 1: Turn the RGMII delay lines off. */
4728c2ecf20Sopenharmony_ci	pad_mii_id.rxc_bypass = 1;
4738c2ecf20Sopenharmony_ci	pad_mii_id.rxc_pd = 1;
4748c2ecf20Sopenharmony_ci	pad_mii_id.txc_bypass = 1;
4758c2ecf20Sopenharmony_ci	pad_mii_id.txc_pd = 1;
4768c2ecf20Sopenharmony_ci	sja1105_cfg_pad_mii_id_packing(packed_buf, &pad_mii_id, PACK);
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	rc = sja1105_xfer_buf(priv, SPI_WRITE, regs->pad_mii_id[port],
4798c2ecf20Sopenharmony_ci			      packed_buf, SJA1105_SIZE_CGU_CMD);
4808c2ecf20Sopenharmony_ci	if (rc < 0)
4818c2ecf20Sopenharmony_ci		return rc;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	/* Stage 2: Turn the RGMII delay lines on. */
4848c2ecf20Sopenharmony_ci	if (priv->rgmii_rx_delay[port]) {
4858c2ecf20Sopenharmony_ci		pad_mii_id.rxc_bypass = 0;
4868c2ecf20Sopenharmony_ci		pad_mii_id.rxc_pd = 0;
4878c2ecf20Sopenharmony_ci	}
4888c2ecf20Sopenharmony_ci	if (priv->rgmii_tx_delay[port]) {
4898c2ecf20Sopenharmony_ci		pad_mii_id.txc_bypass = 0;
4908c2ecf20Sopenharmony_ci		pad_mii_id.txc_pd = 0;
4918c2ecf20Sopenharmony_ci	}
4928c2ecf20Sopenharmony_ci	sja1105_cfg_pad_mii_id_packing(packed_buf, &pad_mii_id, PACK);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	return sja1105_xfer_buf(priv, SPI_WRITE, regs->pad_mii_id[port],
4958c2ecf20Sopenharmony_ci				packed_buf, SJA1105_SIZE_CGU_CMD);
4968c2ecf20Sopenharmony_ci}
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_cistatic int sja1105_rgmii_clocking_setup(struct sja1105_private *priv, int port,
4998c2ecf20Sopenharmony_ci					sja1105_mii_role_t role)
5008c2ecf20Sopenharmony_ci{
5018c2ecf20Sopenharmony_ci	struct device *dev = priv->ds->dev;
5028c2ecf20Sopenharmony_ci	struct sja1105_mac_config_entry *mac;
5038c2ecf20Sopenharmony_ci	sja1105_speed_t speed;
5048c2ecf20Sopenharmony_ci	int rc;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
5078c2ecf20Sopenharmony_ci	speed = mac[port].speed;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	dev_dbg(dev, "Configuring port %d RGMII at speed %dMbps\n",
5108c2ecf20Sopenharmony_ci		port, speed);
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	switch (speed) {
5138c2ecf20Sopenharmony_ci	case SJA1105_SPEED_1000MBPS:
5148c2ecf20Sopenharmony_ci		/* 1000Mbps, IDIV disabled (125 MHz) */
5158c2ecf20Sopenharmony_ci		rc = sja1105_cgu_idiv_config(priv, port, false, 1);
5168c2ecf20Sopenharmony_ci		break;
5178c2ecf20Sopenharmony_ci	case SJA1105_SPEED_100MBPS:
5188c2ecf20Sopenharmony_ci		/* 100Mbps, IDIV enabled, divide by 1 (25 MHz) */
5198c2ecf20Sopenharmony_ci		rc = sja1105_cgu_idiv_config(priv, port, true, 1);
5208c2ecf20Sopenharmony_ci		break;
5218c2ecf20Sopenharmony_ci	case SJA1105_SPEED_10MBPS:
5228c2ecf20Sopenharmony_ci		/* 10Mbps, IDIV enabled, divide by 10 (2.5 MHz) */
5238c2ecf20Sopenharmony_ci		rc = sja1105_cgu_idiv_config(priv, port, true, 10);
5248c2ecf20Sopenharmony_ci		break;
5258c2ecf20Sopenharmony_ci	case SJA1105_SPEED_AUTO:
5268c2ecf20Sopenharmony_ci		/* Skip CGU configuration if there is no speed available
5278c2ecf20Sopenharmony_ci		 * (e.g. link is not established yet)
5288c2ecf20Sopenharmony_ci		 */
5298c2ecf20Sopenharmony_ci		dev_dbg(dev, "Speed not available, skipping CGU config\n");
5308c2ecf20Sopenharmony_ci		return 0;
5318c2ecf20Sopenharmony_ci	default:
5328c2ecf20Sopenharmony_ci		rc = -EINVAL;
5338c2ecf20Sopenharmony_ci	}
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	if (rc < 0) {
5368c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to configure idiv\n");
5378c2ecf20Sopenharmony_ci		return rc;
5388c2ecf20Sopenharmony_ci	}
5398c2ecf20Sopenharmony_ci	rc = sja1105_cgu_rgmii_tx_clk_config(priv, port, speed);
5408c2ecf20Sopenharmony_ci	if (rc < 0) {
5418c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to configure RGMII Tx clock\n");
5428c2ecf20Sopenharmony_ci		return rc;
5438c2ecf20Sopenharmony_ci	}
5448c2ecf20Sopenharmony_ci	rc = sja1105_rgmii_cfg_pad_tx_config(priv, port);
5458c2ecf20Sopenharmony_ci	if (rc < 0) {
5468c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to configure Tx pad registers\n");
5478c2ecf20Sopenharmony_ci		return rc;
5488c2ecf20Sopenharmony_ci	}
5498c2ecf20Sopenharmony_ci	if (!priv->info->setup_rgmii_delay)
5508c2ecf20Sopenharmony_ci		return 0;
5518c2ecf20Sopenharmony_ci	/* The role has no hardware effect for RGMII. However we use it as
5528c2ecf20Sopenharmony_ci	 * a proxy for this interface being a MAC-to-MAC connection, with
5538c2ecf20Sopenharmony_ci	 * the RGMII internal delays needing to be applied by us.
5548c2ecf20Sopenharmony_ci	 */
5558c2ecf20Sopenharmony_ci	if (role == XMII_MAC)
5568c2ecf20Sopenharmony_ci		return 0;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	return priv->info->setup_rgmii_delay(priv, port);
5598c2ecf20Sopenharmony_ci}
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_cistatic int sja1105_cgu_rmii_ref_clk_config(struct sja1105_private *priv,
5628c2ecf20Sopenharmony_ci					   int port)
5638c2ecf20Sopenharmony_ci{
5648c2ecf20Sopenharmony_ci	const struct sja1105_regs *regs = priv->info->regs;
5658c2ecf20Sopenharmony_ci	struct sja1105_cgu_mii_ctrl ref_clk;
5668c2ecf20Sopenharmony_ci	u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
5678c2ecf20Sopenharmony_ci	const int clk_sources[] = {
5688c2ecf20Sopenharmony_ci		CLKSRC_MII0_TX_CLK,
5698c2ecf20Sopenharmony_ci		CLKSRC_MII1_TX_CLK,
5708c2ecf20Sopenharmony_ci		CLKSRC_MII2_TX_CLK,
5718c2ecf20Sopenharmony_ci		CLKSRC_MII3_TX_CLK,
5728c2ecf20Sopenharmony_ci		CLKSRC_MII4_TX_CLK,
5738c2ecf20Sopenharmony_ci	};
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	/* Payload for packed_buf */
5768c2ecf20Sopenharmony_ci	ref_clk.clksrc    = clk_sources[port];
5778c2ecf20Sopenharmony_ci	ref_clk.autoblock = 1;      /* Autoblock clk while changing clksrc */
5788c2ecf20Sopenharmony_ci	ref_clk.pd        = 0;      /* Power Down off => enabled */
5798c2ecf20Sopenharmony_ci	sja1105_cgu_mii_control_packing(packed_buf, &ref_clk, PACK);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	return sja1105_xfer_buf(priv, SPI_WRITE, regs->rmii_ref_clk[port],
5828c2ecf20Sopenharmony_ci				packed_buf, SJA1105_SIZE_CGU_CMD);
5838c2ecf20Sopenharmony_ci}
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_cistatic int
5868c2ecf20Sopenharmony_cisja1105_cgu_rmii_ext_tx_clk_config(struct sja1105_private *priv, int port)
5878c2ecf20Sopenharmony_ci{
5888c2ecf20Sopenharmony_ci	const struct sja1105_regs *regs = priv->info->regs;
5898c2ecf20Sopenharmony_ci	struct sja1105_cgu_mii_ctrl ext_tx_clk;
5908c2ecf20Sopenharmony_ci	u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	/* Payload for packed_buf */
5938c2ecf20Sopenharmony_ci	ext_tx_clk.clksrc    = CLKSRC_PLL1;
5948c2ecf20Sopenharmony_ci	ext_tx_clk.autoblock = 1;   /* Autoblock clk while changing clksrc */
5958c2ecf20Sopenharmony_ci	ext_tx_clk.pd        = 0;   /* Power Down off => enabled */
5968c2ecf20Sopenharmony_ci	sja1105_cgu_mii_control_packing(packed_buf, &ext_tx_clk, PACK);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	return sja1105_xfer_buf(priv, SPI_WRITE, regs->rmii_ext_tx_clk[port],
5998c2ecf20Sopenharmony_ci				packed_buf, SJA1105_SIZE_CGU_CMD);
6008c2ecf20Sopenharmony_ci}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_cistatic int sja1105_cgu_rmii_pll_config(struct sja1105_private *priv)
6038c2ecf20Sopenharmony_ci{
6048c2ecf20Sopenharmony_ci	const struct sja1105_regs *regs = priv->info->regs;
6058c2ecf20Sopenharmony_ci	u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
6068c2ecf20Sopenharmony_ci	struct sja1105_cgu_pll_ctrl pll = {0};
6078c2ecf20Sopenharmony_ci	struct device *dev = priv->ds->dev;
6088c2ecf20Sopenharmony_ci	int rc;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	/* PLL1 must be enabled and output 50 Mhz.
6118c2ecf20Sopenharmony_ci	 * This is done by writing first 0x0A010941 to
6128c2ecf20Sopenharmony_ci	 * the PLL_1_C register and then deasserting
6138c2ecf20Sopenharmony_ci	 * power down (PD) 0x0A010940.
6148c2ecf20Sopenharmony_ci	 */
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	/* Step 1: PLL1 setup for 50Mhz */
6178c2ecf20Sopenharmony_ci	pll.pllclksrc = 0xA;
6188c2ecf20Sopenharmony_ci	pll.msel      = 0x1;
6198c2ecf20Sopenharmony_ci	pll.autoblock = 0x1;
6208c2ecf20Sopenharmony_ci	pll.psel      = 0x1;
6218c2ecf20Sopenharmony_ci	pll.direct    = 0x0;
6228c2ecf20Sopenharmony_ci	pll.fbsel     = 0x1;
6238c2ecf20Sopenharmony_ci	pll.bypass    = 0x0;
6248c2ecf20Sopenharmony_ci	pll.pd        = 0x1;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	sja1105_cgu_pll_control_packing(packed_buf, &pll, PACK);
6278c2ecf20Sopenharmony_ci	rc = sja1105_xfer_buf(priv, SPI_WRITE, regs->rmii_pll1, packed_buf,
6288c2ecf20Sopenharmony_ci			      SJA1105_SIZE_CGU_CMD);
6298c2ecf20Sopenharmony_ci	if (rc < 0) {
6308c2ecf20Sopenharmony_ci		dev_err(dev, "failed to configure PLL1 for 50MHz\n");
6318c2ecf20Sopenharmony_ci		return rc;
6328c2ecf20Sopenharmony_ci	}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	/* Step 2: Enable PLL1 */
6358c2ecf20Sopenharmony_ci	pll.pd = 0x0;
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	sja1105_cgu_pll_control_packing(packed_buf, &pll, PACK);
6388c2ecf20Sopenharmony_ci	rc = sja1105_xfer_buf(priv, SPI_WRITE, regs->rmii_pll1, packed_buf,
6398c2ecf20Sopenharmony_ci			      SJA1105_SIZE_CGU_CMD);
6408c2ecf20Sopenharmony_ci	if (rc < 0) {
6418c2ecf20Sopenharmony_ci		dev_err(dev, "failed to enable PLL1\n");
6428c2ecf20Sopenharmony_ci		return rc;
6438c2ecf20Sopenharmony_ci	}
6448c2ecf20Sopenharmony_ci	return rc;
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_cistatic int sja1105_rmii_clocking_setup(struct sja1105_private *priv, int port,
6488c2ecf20Sopenharmony_ci				       sja1105_mii_role_t role)
6498c2ecf20Sopenharmony_ci{
6508c2ecf20Sopenharmony_ci	struct device *dev = priv->ds->dev;
6518c2ecf20Sopenharmony_ci	int rc;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	dev_dbg(dev, "Configuring RMII-%s clocking\n",
6548c2ecf20Sopenharmony_ci		(role == XMII_MAC) ? "MAC" : "PHY");
6558c2ecf20Sopenharmony_ci	/* AH1601.pdf chapter 2.5.1. Sources */
6568c2ecf20Sopenharmony_ci	if (role == XMII_MAC) {
6578c2ecf20Sopenharmony_ci		/* Configure and enable PLL1 for 50Mhz output */
6588c2ecf20Sopenharmony_ci		rc = sja1105_cgu_rmii_pll_config(priv);
6598c2ecf20Sopenharmony_ci		if (rc < 0)
6608c2ecf20Sopenharmony_ci			return rc;
6618c2ecf20Sopenharmony_ci	}
6628c2ecf20Sopenharmony_ci	/* Disable IDIV for this port */
6638c2ecf20Sopenharmony_ci	rc = sja1105_cgu_idiv_config(priv, port, false, 1);
6648c2ecf20Sopenharmony_ci	if (rc < 0)
6658c2ecf20Sopenharmony_ci		return rc;
6668c2ecf20Sopenharmony_ci	/* Source to sink mappings */
6678c2ecf20Sopenharmony_ci	rc = sja1105_cgu_rmii_ref_clk_config(priv, port);
6688c2ecf20Sopenharmony_ci	if (rc < 0)
6698c2ecf20Sopenharmony_ci		return rc;
6708c2ecf20Sopenharmony_ci	if (role == XMII_MAC) {
6718c2ecf20Sopenharmony_ci		rc = sja1105_cgu_rmii_ext_tx_clk_config(priv, port);
6728c2ecf20Sopenharmony_ci		if (rc < 0)
6738c2ecf20Sopenharmony_ci			return rc;
6748c2ecf20Sopenharmony_ci	}
6758c2ecf20Sopenharmony_ci	return 0;
6768c2ecf20Sopenharmony_ci}
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ciint sja1105_clocking_setup_port(struct sja1105_private *priv, int port)
6798c2ecf20Sopenharmony_ci{
6808c2ecf20Sopenharmony_ci	struct sja1105_xmii_params_entry *mii;
6818c2ecf20Sopenharmony_ci	struct device *dev = priv->ds->dev;
6828c2ecf20Sopenharmony_ci	sja1105_phy_interface_t phy_mode;
6838c2ecf20Sopenharmony_ci	sja1105_mii_role_t role;
6848c2ecf20Sopenharmony_ci	int rc;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	/* RGMII etc */
6898c2ecf20Sopenharmony_ci	phy_mode = mii->xmii_mode[port];
6908c2ecf20Sopenharmony_ci	/* MAC or PHY, for applicable types (not RGMII) */
6918c2ecf20Sopenharmony_ci	role = mii->phy_mac[port];
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	switch (phy_mode) {
6948c2ecf20Sopenharmony_ci	case XMII_MODE_MII:
6958c2ecf20Sopenharmony_ci		rc = sja1105_mii_clocking_setup(priv, port, role);
6968c2ecf20Sopenharmony_ci		break;
6978c2ecf20Sopenharmony_ci	case XMII_MODE_RMII:
6988c2ecf20Sopenharmony_ci		rc = sja1105_rmii_clocking_setup(priv, port, role);
6998c2ecf20Sopenharmony_ci		break;
7008c2ecf20Sopenharmony_ci	case XMII_MODE_RGMII:
7018c2ecf20Sopenharmony_ci		rc = sja1105_rgmii_clocking_setup(priv, port, role);
7028c2ecf20Sopenharmony_ci		break;
7038c2ecf20Sopenharmony_ci	case XMII_MODE_SGMII:
7048c2ecf20Sopenharmony_ci		/* Nothing to do in the CGU for SGMII */
7058c2ecf20Sopenharmony_ci		rc = 0;
7068c2ecf20Sopenharmony_ci		break;
7078c2ecf20Sopenharmony_ci	default:
7088c2ecf20Sopenharmony_ci		dev_err(dev, "Invalid interface mode specified: %d\n",
7098c2ecf20Sopenharmony_ci			phy_mode);
7108c2ecf20Sopenharmony_ci		return -EINVAL;
7118c2ecf20Sopenharmony_ci	}
7128c2ecf20Sopenharmony_ci	if (rc) {
7138c2ecf20Sopenharmony_ci		dev_err(dev, "Clocking setup for port %d failed: %d\n",
7148c2ecf20Sopenharmony_ci			port, rc);
7158c2ecf20Sopenharmony_ci		return rc;
7168c2ecf20Sopenharmony_ci	}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	/* Internally pull down the RX_DV/CRS_DV/RX_CTL and RX_ER inputs */
7198c2ecf20Sopenharmony_ci	return sja1105_cfg_pad_rx_config(priv, port);
7208c2ecf20Sopenharmony_ci}
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ciint sja1105_clocking_setup(struct sja1105_private *priv)
7238c2ecf20Sopenharmony_ci{
7248c2ecf20Sopenharmony_ci	int port, rc;
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
7278c2ecf20Sopenharmony_ci		rc = sja1105_clocking_setup_port(priv, port);
7288c2ecf20Sopenharmony_ci		if (rc < 0)
7298c2ecf20Sopenharmony_ci			return rc;
7308c2ecf20Sopenharmony_ci	}
7318c2ecf20Sopenharmony_ci	return 0;
7328c2ecf20Sopenharmony_ci}
733