Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 2 | /* |
| 3 | * TI serdes driver for keystone2. |
| 4 | * |
| 5 | * (C) Copyright 2014 |
| 6 | * Texas Instruments Incorporated, <www.ti.com> |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 7 | */ |
| 8 | |
Hao Zhang | d890dff | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 9 | #include <errno.h> |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 10 | #include <common.h> |
Andrew Davis | ab82af6 | 2023-11-17 16:38:28 -0600 | [diff] [blame] | 11 | #include <asm/io.h> |
Hao Zhang | d890dff | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 12 | #include <asm/ti-common/keystone_serdes.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 13 | #include <linux/bitops.h> |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 14 | |
Hao Zhang | d890dff | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 15 | #define SERDES_CMU_REGS(x) (0x0000 + (0x0c00 * (x))) |
Hao Zhang | ae3ed41 | 2014-10-22 17:18:22 +0300 | [diff] [blame] | 16 | #define SERDES_LANE_REGS(x) (0x0200 + (0x200 * (x))) |
Hao Zhang | d890dff | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 17 | #define SERDES_COMLANE_REGS 0x0a00 |
| 18 | #define SERDES_WIZ_REGS 0x1fc0 |
| 19 | |
| 20 | #define SERDES_CMU_REG_000(x) (SERDES_CMU_REGS(x) + 0x000) |
| 21 | #define SERDES_CMU_REG_010(x) (SERDES_CMU_REGS(x) + 0x010) |
| 22 | #define SERDES_COMLANE_REG_000 (SERDES_COMLANE_REGS + 0x000) |
| 23 | #define SERDES_LANE_REG_000(x) (SERDES_LANE_REGS(x) + 0x000) |
| 24 | #define SERDES_LANE_REG_028(x) (SERDES_LANE_REGS(x) + 0x028) |
| 25 | #define SERDES_LANE_CTL_STATUS_REG(x) (SERDES_WIZ_REGS + 0x0020 + (4 * (x))) |
| 26 | #define SERDES_PLL_CTL_REG (SERDES_WIZ_REGS + 0x0034) |
| 27 | |
| 28 | #define SERDES_RESET BIT(28) |
| 29 | #define SERDES_LANE_RESET BIT(29) |
| 30 | #define SERDES_LANE_LOOPBACK BIT(30) |
| 31 | #define SERDES_LANE_EN_VAL(x, y, z) (x[y] | (z << 26) | (z << 10)) |
Hao Zhang | ae3ed41 | 2014-10-22 17:18:22 +0300 | [diff] [blame] | 32 | |
Khoronzhuk, Ivan | 138d6da | 2014-10-22 17:18:24 +0300 | [diff] [blame] | 33 | #define SERDES_CMU_CFG_NUM 5 |
| 34 | #define SERDES_COMLANE_CFG_NUM 10 |
| 35 | #define SERDES_LANE_CFG_NUM 10 |
| 36 | |
Hao Zhang | ae3ed41 | 2014-10-22 17:18:22 +0300 | [diff] [blame] | 37 | struct serdes_cfg { |
| 38 | u32 ofs; |
| 39 | u32 val; |
| 40 | u32 mask; |
| 41 | }; |
| 42 | |
Khoronzhuk, Ivan | 138d6da | 2014-10-22 17:18:24 +0300 | [diff] [blame] | 43 | struct cfg_entry { |
| 44 | enum ks2_serdes_clock clk; |
| 45 | enum ks2_serdes_rate rate; |
| 46 | struct serdes_cfg cmu[SERDES_CMU_CFG_NUM]; |
| 47 | struct serdes_cfg comlane[SERDES_COMLANE_CFG_NUM]; |
| 48 | struct serdes_cfg lane[SERDES_LANE_CFG_NUM]; |
| 49 | }; |
| 50 | |
Hao Zhang | d890dff | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 51 | /* SERDES PHY lane enable configuration value, indexed by PHY interface */ |
| 52 | static u32 serdes_cfg_lane_enable[] = { |
| 53 | 0xf000f0c0, /* SGMII */ |
| 54 | 0xf0e9f038, /* PCSR */ |
| 55 | }; |
| 56 | |
| 57 | /* SERDES PHY PLL enable configuration value, indexed by PHY interface */ |
| 58 | static u32 serdes_cfg_pll_enable[] = { |
| 59 | 0xe0000000, /* SGMII */ |
| 60 | 0xee000000, /* PCSR */ |
| 61 | }; |
| 62 | |
Khoronzhuk, Ivan | 138d6da | 2014-10-22 17:18:24 +0300 | [diff] [blame] | 63 | /** |
| 64 | * Array to hold all possible serdes configurations. |
| 65 | * Combination for 5 clock settings and 6 baud rates. |
| 66 | */ |
| 67 | static struct cfg_entry cfgs[] = { |
| 68 | { |
| 69 | .clk = SERDES_CLOCK_156P25M, |
| 70 | .rate = SERDES_RATE_5G, |
| 71 | .cmu = { |
| 72 | {0x0000, 0x00800000, 0xffff0000}, |
| 73 | {0x0014, 0x00008282, 0x0000ffff}, |
| 74 | {0x0060, 0x00142438, 0x00ffffff}, |
| 75 | {0x0064, 0x00c3c700, 0x00ffff00}, |
| 76 | {0x0078, 0x0000c000, 0x0000ff00} |
| 77 | }, |
| 78 | .comlane = { |
| 79 | {0x0a00, 0x00000800, 0x0000ff00}, |
| 80 | {0x0a08, 0x38a20000, 0xffff0000}, |
| 81 | {0x0a30, 0x008a8a00, 0x00ffff00}, |
| 82 | {0x0a84, 0x00000600, 0x0000ff00}, |
| 83 | {0x0a94, 0x10000000, 0xff000000}, |
| 84 | {0x0aa0, 0x81000000, 0xff000000}, |
| 85 | {0x0abc, 0xff000000, 0xff000000}, |
| 86 | {0x0ac0, 0x0000008b, 0x000000ff}, |
| 87 | {0x0b08, 0x583f0000, 0xffff0000}, |
| 88 | {0x0b0c, 0x0000004e, 0x000000ff} |
| 89 | }, |
| 90 | .lane = { |
| 91 | {0x0004, 0x38000080, 0xff0000ff}, |
| 92 | {0x0008, 0x00000000, 0x000000ff}, |
| 93 | {0x000c, 0x02000000, 0xff000000}, |
| 94 | {0x0010, 0x1b000000, 0xff000000}, |
| 95 | {0x0014, 0x00006fb8, 0x0000ffff}, |
| 96 | {0x0018, 0x758000e4, 0xffff00ff}, |
| 97 | {0x00ac, 0x00004400, 0x0000ff00}, |
| 98 | {0x002c, 0x00100800, 0x00ffff00}, |
| 99 | {0x0080, 0x00820082, 0x00ff00ff}, |
| 100 | {0x0084, 0x1d0f0385, 0xffffffff} |
| 101 | }, |
| 102 | }, |
Hao Zhang | ae3ed41 | 2014-10-22 17:18:22 +0300 | [diff] [blame] | 103 | }; |
| 104 | |
Hao Zhang | ae3ed41 | 2014-10-22 17:18:22 +0300 | [diff] [blame] | 105 | static inline void ks2_serdes_rmw(u32 addr, u32 value, u32 mask) |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 106 | { |
Hao Zhang | ae3ed41 | 2014-10-22 17:18:22 +0300 | [diff] [blame] | 107 | writel(((readl(addr) & (~mask)) | (value & mask)), addr); |
| 108 | } |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 109 | |
Hao Zhang | ae3ed41 | 2014-10-22 17:18:22 +0300 | [diff] [blame] | 110 | static void ks2_serdes_cfg_setup(u32 base, struct serdes_cfg *cfg, u32 size) |
| 111 | { |
| 112 | u32 i; |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 113 | |
Hao Zhang | ae3ed41 | 2014-10-22 17:18:22 +0300 | [diff] [blame] | 114 | for (i = 0; i < size; i++) |
| 115 | ks2_serdes_rmw(base + cfg[i].ofs, cfg[i].val, cfg[i].mask); |
| 116 | } |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 117 | |
Hao Zhang | ae3ed41 | 2014-10-22 17:18:22 +0300 | [diff] [blame] | 118 | static void ks2_serdes_lane_config(u32 base, struct serdes_cfg *cfg_lane, |
| 119 | u32 size, u32 lane) |
| 120 | { |
| 121 | u32 i; |
| 122 | |
| 123 | for (i = 0; i < size; i++) |
| 124 | ks2_serdes_rmw(base + cfg_lane[i].ofs + SERDES_LANE_REGS(lane), |
| 125 | cfg_lane[i].val, cfg_lane[i].mask); |
| 126 | } |
| 127 | |
Khoronzhuk, Ivan | 138d6da | 2014-10-22 17:18:24 +0300 | [diff] [blame] | 128 | static int ks2_serdes_init_cfg(u32 base, struct cfg_entry *cfg, u32 num_lanes) |
Hao Zhang | ae3ed41 | 2014-10-22 17:18:22 +0300 | [diff] [blame] | 129 | { |
| 130 | u32 i; |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 131 | |
Khoronzhuk, Ivan | 138d6da | 2014-10-22 17:18:24 +0300 | [diff] [blame] | 132 | ks2_serdes_cfg_setup(base, cfg->cmu, SERDES_CMU_CFG_NUM); |
| 133 | ks2_serdes_cfg_setup(base, cfg->comlane, SERDES_COMLANE_CFG_NUM); |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 134 | |
Hao Zhang | ae3ed41 | 2014-10-22 17:18:22 +0300 | [diff] [blame] | 135 | for (i = 0; i < num_lanes; i++) |
Khoronzhuk, Ivan | 138d6da | 2014-10-22 17:18:24 +0300 | [diff] [blame] | 136 | ks2_serdes_lane_config(base, cfg->lane, SERDES_LANE_CFG_NUM, i); |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 137 | |
Hao Zhang | ae3ed41 | 2014-10-22 17:18:22 +0300 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
Hao Zhang | d890dff | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 141 | static void ks2_serdes_cmu_comlane_enable(u32 base, struct ks2_serdes *serdes) |
Hao Zhang | ae3ed41 | 2014-10-22 17:18:22 +0300 | [diff] [blame] | 142 | { |
Hao Zhang | d890dff | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 143 | /* Bring SerDes out of Reset */ |
| 144 | ks2_serdes_rmw(base + SERDES_CMU_REG_010(0), 0x0, SERDES_RESET); |
| 145 | if (serdes->intf == SERDES_PHY_PCSR) |
| 146 | ks2_serdes_rmw(base + SERDES_CMU_REG_010(1), 0x0, SERDES_RESET); |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 147 | |
Hao Zhang | d890dff | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 148 | /* Enable CMU and COMLANE */ |
| 149 | ks2_serdes_rmw(base + SERDES_CMU_REG_000(0), 0x03, 0x000000ff); |
| 150 | if (serdes->intf == SERDES_PHY_PCSR) |
| 151 | ks2_serdes_rmw(base + SERDES_CMU_REG_000(1), 0x03, 0x000000ff); |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 152 | |
Hao Zhang | d890dff | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 153 | ks2_serdes_rmw(base + SERDES_COMLANE_REG_000, 0x5f, 0x000000ff); |
| 154 | } |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 155 | |
Hao Zhang | d890dff | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 156 | static void ks2_serdes_pll_enable(u32 base, struct ks2_serdes *serdes) |
| 157 | { |
| 158 | writel(serdes_cfg_pll_enable[serdes->intf], |
| 159 | base + SERDES_PLL_CTL_REG); |
| 160 | } |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 161 | |
Hao Zhang | d890dff | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 162 | static void ks2_serdes_lane_reset(u32 base, u32 reset, u32 lane) |
| 163 | { |
| 164 | if (reset) |
| 165 | ks2_serdes_rmw(base + SERDES_LANE_REG_028(lane), |
| 166 | 0x1, SERDES_LANE_RESET); |
| 167 | else |
| 168 | ks2_serdes_rmw(base + SERDES_LANE_REG_028(lane), |
| 169 | 0x0, SERDES_LANE_RESET); |
| 170 | } |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 171 | |
Hao Zhang | d890dff | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 172 | static void ks2_serdes_lane_enable(u32 base, |
| 173 | struct ks2_serdes *serdes, u32 lane) |
| 174 | { |
| 175 | /* Bring lane out of reset */ |
| 176 | ks2_serdes_lane_reset(base, 0, lane); |
| 177 | |
| 178 | writel(SERDES_LANE_EN_VAL(serdes_cfg_lane_enable, serdes->intf, |
| 179 | serdes->rate_mode), |
| 180 | base + SERDES_LANE_CTL_STATUS_REG(lane)); |
| 181 | |
| 182 | /* Set NES bit if Loopback Enabled */ |
| 183 | if (serdes->loopback) |
| 184 | ks2_serdes_rmw(base + SERDES_LANE_REG_000(lane), |
| 185 | 0x1, SERDES_LANE_LOOPBACK); |
| 186 | } |
| 187 | |
| 188 | int ks2_serdes_init(u32 base, struct ks2_serdes *serdes, u32 num_lanes) |
| 189 | { |
| 190 | int i; |
| 191 | int ret = 0; |
| 192 | |
Khoronzhuk, Ivan | 138d6da | 2014-10-22 17:18:24 +0300 | [diff] [blame] | 193 | for (i = 0; i < ARRAY_SIZE(cfgs); i++) |
| 194 | if (serdes->clk == cfgs[i].clk && serdes->rate == cfgs[i].rate) |
| 195 | break; |
| 196 | |
| 197 | if (i >= ARRAY_SIZE(cfgs)) { |
| 198 | puts("Cannot find keystone SerDes configuration"); |
Hao Zhang | d890dff | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 199 | return -EINVAL; |
Khoronzhuk, Ivan | 138d6da | 2014-10-22 17:18:24 +0300 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | ks2_serdes_init_cfg(base, &cfgs[i], num_lanes); |
Hao Zhang | d890dff | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 203 | |
| 204 | ks2_serdes_cmu_comlane_enable(base, serdes); |
| 205 | for (i = 0; i < num_lanes; i++) |
| 206 | ks2_serdes_lane_enable(base, serdes, i); |
| 207 | |
| 208 | ks2_serdes_pll_enable(base, serdes); |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 209 | |
Hao Zhang | d890dff | 2014-10-22 17:18:23 +0300 | [diff] [blame] | 210 | return ret; |
Khoronzhuk, Ivan | dbfecb2 | 2014-10-22 17:18:21 +0300 | [diff] [blame] | 211 | } |