1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2a43febdeSKhoronzhuk, Ivan /*
3a43febdeSKhoronzhuk, Ivan * TI serdes driver for keystone2.
4a43febdeSKhoronzhuk, Ivan *
5a43febdeSKhoronzhuk, Ivan * (C) Copyright 2014
6a43febdeSKhoronzhuk, Ivan * Texas Instruments Incorporated, <www.ti.com>
7a43febdeSKhoronzhuk, Ivan */
8a43febdeSKhoronzhuk, Ivan
992a16c81SHao Zhang #include <errno.h>
10a43febdeSKhoronzhuk, Ivan #include <common.h>
1192a16c81SHao Zhang #include <asm/ti-common/keystone_serdes.h>
12a43febdeSKhoronzhuk, Ivan
1392a16c81SHao Zhang #define SERDES_CMU_REGS(x) (0x0000 + (0x0c00 * (x)))
1495f74dadSHao Zhang #define SERDES_LANE_REGS(x) (0x0200 + (0x200 * (x)))
1592a16c81SHao Zhang #define SERDES_COMLANE_REGS 0x0a00
1692a16c81SHao Zhang #define SERDES_WIZ_REGS 0x1fc0
1792a16c81SHao Zhang
1892a16c81SHao Zhang #define SERDES_CMU_REG_000(x) (SERDES_CMU_REGS(x) + 0x000)
1992a16c81SHao Zhang #define SERDES_CMU_REG_010(x) (SERDES_CMU_REGS(x) + 0x010)
2092a16c81SHao Zhang #define SERDES_COMLANE_REG_000 (SERDES_COMLANE_REGS + 0x000)
2192a16c81SHao Zhang #define SERDES_LANE_REG_000(x) (SERDES_LANE_REGS(x) + 0x000)
2292a16c81SHao Zhang #define SERDES_LANE_REG_028(x) (SERDES_LANE_REGS(x) + 0x028)
2392a16c81SHao Zhang #define SERDES_LANE_CTL_STATUS_REG(x) (SERDES_WIZ_REGS + 0x0020 + (4 * (x)))
2492a16c81SHao Zhang #define SERDES_PLL_CTL_REG (SERDES_WIZ_REGS + 0x0034)
2592a16c81SHao Zhang
2692a16c81SHao Zhang #define SERDES_RESET BIT(28)
2792a16c81SHao Zhang #define SERDES_LANE_RESET BIT(29)
2892a16c81SHao Zhang #define SERDES_LANE_LOOPBACK BIT(30)
2992a16c81SHao Zhang #define SERDES_LANE_EN_VAL(x, y, z) (x[y] | (z << 26) | (z << 10))
3095f74dadSHao Zhang
31496191c7SKhoronzhuk, Ivan #define SERDES_CMU_CFG_NUM 5
32496191c7SKhoronzhuk, Ivan #define SERDES_COMLANE_CFG_NUM 10
33496191c7SKhoronzhuk, Ivan #define SERDES_LANE_CFG_NUM 10
34496191c7SKhoronzhuk, Ivan
3595f74dadSHao Zhang struct serdes_cfg {
3695f74dadSHao Zhang u32 ofs;
3795f74dadSHao Zhang u32 val;
3895f74dadSHao Zhang u32 mask;
3995f74dadSHao Zhang };
4095f74dadSHao Zhang
41496191c7SKhoronzhuk, Ivan struct cfg_entry {
42496191c7SKhoronzhuk, Ivan enum ks2_serdes_clock clk;
43496191c7SKhoronzhuk, Ivan enum ks2_serdes_rate rate;
44496191c7SKhoronzhuk, Ivan struct serdes_cfg cmu[SERDES_CMU_CFG_NUM];
45496191c7SKhoronzhuk, Ivan struct serdes_cfg comlane[SERDES_COMLANE_CFG_NUM];
46496191c7SKhoronzhuk, Ivan struct serdes_cfg lane[SERDES_LANE_CFG_NUM];
47496191c7SKhoronzhuk, Ivan };
48496191c7SKhoronzhuk, Ivan
4992a16c81SHao Zhang /* SERDES PHY lane enable configuration value, indexed by PHY interface */
5092a16c81SHao Zhang static u32 serdes_cfg_lane_enable[] = {
5192a16c81SHao Zhang 0xf000f0c0, /* SGMII */
5292a16c81SHao Zhang 0xf0e9f038, /* PCSR */
5392a16c81SHao Zhang };
5492a16c81SHao Zhang
5592a16c81SHao Zhang /* SERDES PHY PLL enable configuration value, indexed by PHY interface */
5692a16c81SHao Zhang static u32 serdes_cfg_pll_enable[] = {
5792a16c81SHao Zhang 0xe0000000, /* SGMII */
5892a16c81SHao Zhang 0xee000000, /* PCSR */
5992a16c81SHao Zhang };
6092a16c81SHao Zhang
61496191c7SKhoronzhuk, Ivan /**
62496191c7SKhoronzhuk, Ivan * Array to hold all possible serdes configurations.
63496191c7SKhoronzhuk, Ivan * Combination for 5 clock settings and 6 baud rates.
64496191c7SKhoronzhuk, Ivan */
65496191c7SKhoronzhuk, Ivan static struct cfg_entry cfgs[] = {
66496191c7SKhoronzhuk, Ivan {
67496191c7SKhoronzhuk, Ivan .clk = SERDES_CLOCK_156P25M,
68496191c7SKhoronzhuk, Ivan .rate = SERDES_RATE_5G,
69496191c7SKhoronzhuk, Ivan .cmu = {
7095f74dadSHao Zhang {0x0000, 0x00800000, 0xffff0000},
7195f74dadSHao Zhang {0x0014, 0x00008282, 0x0000ffff},
7295f74dadSHao Zhang {0x0060, 0x00142438, 0x00ffffff},
7395f74dadSHao Zhang {0x0064, 0x00c3c700, 0x00ffff00},
7495f74dadSHao Zhang {0x0078, 0x0000c000, 0x0000ff00}
75496191c7SKhoronzhuk, Ivan },
76496191c7SKhoronzhuk, Ivan .comlane = {
7795f74dadSHao Zhang {0x0a00, 0x00000800, 0x0000ff00},
7895f74dadSHao Zhang {0x0a08, 0x38a20000, 0xffff0000},
7995f74dadSHao Zhang {0x0a30, 0x008a8a00, 0x00ffff00},
8095f74dadSHao Zhang {0x0a84, 0x00000600, 0x0000ff00},
8195f74dadSHao Zhang {0x0a94, 0x10000000, 0xff000000},
8295f74dadSHao Zhang {0x0aa0, 0x81000000, 0xff000000},
8395f74dadSHao Zhang {0x0abc, 0xff000000, 0xff000000},
8495f74dadSHao Zhang {0x0ac0, 0x0000008b, 0x000000ff},
8595f74dadSHao Zhang {0x0b08, 0x583f0000, 0xffff0000},
8695f74dadSHao Zhang {0x0b0c, 0x0000004e, 0x000000ff}
87496191c7SKhoronzhuk, Ivan },
88496191c7SKhoronzhuk, Ivan .lane = {
8995f74dadSHao Zhang {0x0004, 0x38000080, 0xff0000ff},
9095f74dadSHao Zhang {0x0008, 0x00000000, 0x000000ff},
9195f74dadSHao Zhang {0x000c, 0x02000000, 0xff000000},
9295f74dadSHao Zhang {0x0010, 0x1b000000, 0xff000000},
9395f74dadSHao Zhang {0x0014, 0x00006fb8, 0x0000ffff},
9495f74dadSHao Zhang {0x0018, 0x758000e4, 0xffff00ff},
9595f74dadSHao Zhang {0x00ac, 0x00004400, 0x0000ff00},
9695f74dadSHao Zhang {0x002c, 0x00100800, 0x00ffff00},
9795f74dadSHao Zhang {0x0080, 0x00820082, 0x00ff00ff},
9895f74dadSHao Zhang {0x0084, 0x1d0f0385, 0xffffffff}
99496191c7SKhoronzhuk, Ivan },
100496191c7SKhoronzhuk, Ivan },
10195f74dadSHao Zhang };
10295f74dadSHao Zhang
ks2_serdes_rmw(u32 addr,u32 value,u32 mask)10395f74dadSHao Zhang static inline void ks2_serdes_rmw(u32 addr, u32 value, u32 mask)
10495f74dadSHao Zhang {
10595f74dadSHao Zhang writel(((readl(addr) & (~mask)) | (value & mask)), addr);
10695f74dadSHao Zhang }
10795f74dadSHao Zhang
ks2_serdes_cfg_setup(u32 base,struct serdes_cfg * cfg,u32 size)10895f74dadSHao Zhang static void ks2_serdes_cfg_setup(u32 base, struct serdes_cfg *cfg, u32 size)
10995f74dadSHao Zhang {
11095f74dadSHao Zhang u32 i;
11195f74dadSHao Zhang
11295f74dadSHao Zhang for (i = 0; i < size; i++)
11395f74dadSHao Zhang ks2_serdes_rmw(base + cfg[i].ofs, cfg[i].val, cfg[i].mask);
11495f74dadSHao Zhang }
11595f74dadSHao Zhang
ks2_serdes_lane_config(u32 base,struct serdes_cfg * cfg_lane,u32 size,u32 lane)11695f74dadSHao Zhang static void ks2_serdes_lane_config(u32 base, struct serdes_cfg *cfg_lane,
11795f74dadSHao Zhang u32 size, u32 lane)
11895f74dadSHao Zhang {
11995f74dadSHao Zhang u32 i;
12095f74dadSHao Zhang
12195f74dadSHao Zhang for (i = 0; i < size; i++)
12295f74dadSHao Zhang ks2_serdes_rmw(base + cfg_lane[i].ofs + SERDES_LANE_REGS(lane),
12395f74dadSHao Zhang cfg_lane[i].val, cfg_lane[i].mask);
12495f74dadSHao Zhang }
12595f74dadSHao Zhang
ks2_serdes_init_cfg(u32 base,struct cfg_entry * cfg,u32 num_lanes)126496191c7SKhoronzhuk, Ivan static int ks2_serdes_init_cfg(u32 base, struct cfg_entry *cfg, u32 num_lanes)
12795f74dadSHao Zhang {
12895f74dadSHao Zhang u32 i;
12995f74dadSHao Zhang
130496191c7SKhoronzhuk, Ivan ks2_serdes_cfg_setup(base, cfg->cmu, SERDES_CMU_CFG_NUM);
131496191c7SKhoronzhuk, Ivan ks2_serdes_cfg_setup(base, cfg->comlane, SERDES_COMLANE_CFG_NUM);
13295f74dadSHao Zhang
13395f74dadSHao Zhang for (i = 0; i < num_lanes; i++)
134496191c7SKhoronzhuk, Ivan ks2_serdes_lane_config(base, cfg->lane, SERDES_LANE_CFG_NUM, i);
13595f74dadSHao Zhang
13695f74dadSHao Zhang return 0;
13795f74dadSHao Zhang }
13895f74dadSHao Zhang
ks2_serdes_cmu_comlane_enable(u32 base,struct ks2_serdes * serdes)13992a16c81SHao Zhang static void ks2_serdes_cmu_comlane_enable(u32 base, struct ks2_serdes *serdes)
140a43febdeSKhoronzhuk, Ivan {
14192a16c81SHao Zhang /* Bring SerDes out of Reset */
14292a16c81SHao Zhang ks2_serdes_rmw(base + SERDES_CMU_REG_010(0), 0x0, SERDES_RESET);
14392a16c81SHao Zhang if (serdes->intf == SERDES_PHY_PCSR)
14492a16c81SHao Zhang ks2_serdes_rmw(base + SERDES_CMU_REG_010(1), 0x0, SERDES_RESET);
145a43febdeSKhoronzhuk, Ivan
14692a16c81SHao Zhang /* Enable CMU and COMLANE */
14792a16c81SHao Zhang ks2_serdes_rmw(base + SERDES_CMU_REG_000(0), 0x03, 0x000000ff);
14892a16c81SHao Zhang if (serdes->intf == SERDES_PHY_PCSR)
14992a16c81SHao Zhang ks2_serdes_rmw(base + SERDES_CMU_REG_000(1), 0x03, 0x000000ff);
150a43febdeSKhoronzhuk, Ivan
15192a16c81SHao Zhang ks2_serdes_rmw(base + SERDES_COMLANE_REG_000, 0x5f, 0x000000ff);
15292a16c81SHao Zhang }
153a43febdeSKhoronzhuk, Ivan
ks2_serdes_pll_enable(u32 base,struct ks2_serdes * serdes)15492a16c81SHao Zhang static void ks2_serdes_pll_enable(u32 base, struct ks2_serdes *serdes)
15592a16c81SHao Zhang {
15692a16c81SHao Zhang writel(serdes_cfg_pll_enable[serdes->intf],
15792a16c81SHao Zhang base + SERDES_PLL_CTL_REG);
15892a16c81SHao Zhang }
159a43febdeSKhoronzhuk, Ivan
ks2_serdes_lane_reset(u32 base,u32 reset,u32 lane)16092a16c81SHao Zhang static void ks2_serdes_lane_reset(u32 base, u32 reset, u32 lane)
16192a16c81SHao Zhang {
16292a16c81SHao Zhang if (reset)
16392a16c81SHao Zhang ks2_serdes_rmw(base + SERDES_LANE_REG_028(lane),
16492a16c81SHao Zhang 0x1, SERDES_LANE_RESET);
16592a16c81SHao Zhang else
16692a16c81SHao Zhang ks2_serdes_rmw(base + SERDES_LANE_REG_028(lane),
16792a16c81SHao Zhang 0x0, SERDES_LANE_RESET);
16892a16c81SHao Zhang }
169a43febdeSKhoronzhuk, Ivan
ks2_serdes_lane_enable(u32 base,struct ks2_serdes * serdes,u32 lane)17092a16c81SHao Zhang static void ks2_serdes_lane_enable(u32 base,
17192a16c81SHao Zhang struct ks2_serdes *serdes, u32 lane)
17292a16c81SHao Zhang {
17392a16c81SHao Zhang /* Bring lane out of reset */
17492a16c81SHao Zhang ks2_serdes_lane_reset(base, 0, lane);
175a43febdeSKhoronzhuk, Ivan
17692a16c81SHao Zhang writel(SERDES_LANE_EN_VAL(serdes_cfg_lane_enable, serdes->intf,
17792a16c81SHao Zhang serdes->rate_mode),
17892a16c81SHao Zhang base + SERDES_LANE_CTL_STATUS_REG(lane));
17992a16c81SHao Zhang
18092a16c81SHao Zhang /* Set NES bit if Loopback Enabled */
18192a16c81SHao Zhang if (serdes->loopback)
18292a16c81SHao Zhang ks2_serdes_rmw(base + SERDES_LANE_REG_000(lane),
18392a16c81SHao Zhang 0x1, SERDES_LANE_LOOPBACK);
18492a16c81SHao Zhang }
18592a16c81SHao Zhang
ks2_serdes_init(u32 base,struct ks2_serdes * serdes,u32 num_lanes)18692a16c81SHao Zhang int ks2_serdes_init(u32 base, struct ks2_serdes *serdes, u32 num_lanes)
18792a16c81SHao Zhang {
18892a16c81SHao Zhang int i;
18992a16c81SHao Zhang int ret = 0;
19092a16c81SHao Zhang
191496191c7SKhoronzhuk, Ivan for (i = 0; i < ARRAY_SIZE(cfgs); i++)
192496191c7SKhoronzhuk, Ivan if (serdes->clk == cfgs[i].clk && serdes->rate == cfgs[i].rate)
193496191c7SKhoronzhuk, Ivan break;
194496191c7SKhoronzhuk, Ivan
195496191c7SKhoronzhuk, Ivan if (i >= ARRAY_SIZE(cfgs)) {
196496191c7SKhoronzhuk, Ivan puts("Cannot find keystone SerDes configuration");
19792a16c81SHao Zhang return -EINVAL;
198496191c7SKhoronzhuk, Ivan }
199496191c7SKhoronzhuk, Ivan
200496191c7SKhoronzhuk, Ivan ks2_serdes_init_cfg(base, &cfgs[i], num_lanes);
20192a16c81SHao Zhang
20292a16c81SHao Zhang ks2_serdes_cmu_comlane_enable(base, serdes);
20392a16c81SHao Zhang for (i = 0; i < num_lanes; i++)
20492a16c81SHao Zhang ks2_serdes_lane_enable(base, serdes, i);
20592a16c81SHao Zhang
20692a16c81SHao Zhang ks2_serdes_pll_enable(base, serdes);
20792a16c81SHao Zhang
20892a16c81SHao Zhang return ret;
209a43febdeSKhoronzhuk, Ivan }
210