1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Texas Instruments Ethernet Switch media-access-controller (MAC) submodule/ 4 * Ethernet MAC Sliver (CPGMAC_SL) 5 * 6 * Copyright (C) 2019 Texas Instruments 7 * 8 */ 9 10 #include <linux/delay.h> 11 #include <linux/io.h> 12 #include <linux/kernel.h> 13 14 #include "cpsw_sl.h" 15 16 #define CPSW_SL_REG_NOTUSED U16_MAX 17 18 static const u16 cpsw_sl_reg_map_cpsw[] = { 19 [CPSW_SL_IDVER] = 0x00, 20 [CPSW_SL_MACCONTROL] = 0x04, 21 [CPSW_SL_MACSTATUS] = 0x08, 22 [CPSW_SL_SOFT_RESET] = 0x0c, 23 [CPSW_SL_RX_MAXLEN] = 0x10, 24 [CPSW_SL_BOFFTEST] = 0x14, 25 [CPSW_SL_RX_PAUSE] = 0x18, 26 [CPSW_SL_TX_PAUSE] = 0x1c, 27 [CPSW_SL_EMCONTROL] = 0x20, 28 [CPSW_SL_RX_PRI_MAP] = 0x24, 29 [CPSW_SL_TX_GAP] = 0x28, 30 }; 31 32 static const u16 cpsw_sl_reg_map_66ak2hk[] = { 33 [CPSW_SL_IDVER] = 0x00, 34 [CPSW_SL_MACCONTROL] = 0x04, 35 [CPSW_SL_MACSTATUS] = 0x08, 36 [CPSW_SL_SOFT_RESET] = 0x0c, 37 [CPSW_SL_RX_MAXLEN] = 0x10, 38 [CPSW_SL_BOFFTEST] = CPSW_SL_REG_NOTUSED, 39 [CPSW_SL_RX_PAUSE] = 0x18, 40 [CPSW_SL_TX_PAUSE] = 0x1c, 41 [CPSW_SL_EMCONTROL] = 0x20, 42 [CPSW_SL_RX_PRI_MAP] = 0x24, 43 [CPSW_SL_TX_GAP] = CPSW_SL_REG_NOTUSED, 44 }; 45 46 static const u16 cpsw_sl_reg_map_66ak2x_xgbe[] = { 47 [CPSW_SL_IDVER] = 0x00, 48 [CPSW_SL_MACCONTROL] = 0x04, 49 [CPSW_SL_MACSTATUS] = 0x08, 50 [CPSW_SL_SOFT_RESET] = 0x0c, 51 [CPSW_SL_RX_MAXLEN] = 0x10, 52 [CPSW_SL_BOFFTEST] = CPSW_SL_REG_NOTUSED, 53 [CPSW_SL_RX_PAUSE] = 0x18, 54 [CPSW_SL_TX_PAUSE] = 0x1c, 55 [CPSW_SL_EMCONTROL] = 0x20, 56 [CPSW_SL_RX_PRI_MAP] = CPSW_SL_REG_NOTUSED, 57 [CPSW_SL_TX_GAP] = 0x28, 58 }; 59 60 static const u16 cpsw_sl_reg_map_66ak2elg_am65[] = { 61 [CPSW_SL_IDVER] = CPSW_SL_REG_NOTUSED, 62 [CPSW_SL_MACCONTROL] = 0x00, 63 [CPSW_SL_MACSTATUS] = 0x04, 64 [CPSW_SL_SOFT_RESET] = 0x08, 65 [CPSW_SL_RX_MAXLEN] = CPSW_SL_REG_NOTUSED, 66 [CPSW_SL_BOFFTEST] = 0x0c, 67 [CPSW_SL_RX_PAUSE] = 0x10, 68 [CPSW_SL_TX_PAUSE] = 0x40, 69 [CPSW_SL_EMCONTROL] = 0x70, 70 [CPSW_SL_RX_PRI_MAP] = CPSW_SL_REG_NOTUSED, 71 [CPSW_SL_TX_GAP] = 0x74, 72 }; 73 74 #define CPSW_SL_SOFT_RESET_BIT BIT(0) 75 76 #define CPSW_SL_STATUS_PN_IDLE BIT(31) 77 #define CPSW_SL_AM65_STATUS_PN_E_IDLE BIT(30) 78 #define CPSW_SL_AM65_STATUS_PN_P_IDLE BIT(29) 79 #define CPSW_SL_AM65_STATUS_PN_TX_IDLE BIT(28) 80 81 #define CPSW_SL_STATUS_IDLE_MASK_BASE (CPSW_SL_STATUS_PN_IDLE) 82 83 #define CPSW_SL_STATUS_IDLE_MASK_K3 \ 84 (CPSW_SL_STATUS_IDLE_MASK_BASE | CPSW_SL_AM65_STATUS_PN_E_IDLE | \ 85 CPSW_SL_AM65_STATUS_PN_P_IDLE | CPSW_SL_AM65_STATUS_PN_TX_IDLE) 86 87 #define CPSW_SL_CTL_FUNC_BASE \ 88 (CPSW_SL_CTL_FULLDUPLEX |\ 89 CPSW_SL_CTL_LOOPBACK |\ 90 CPSW_SL_CTL_RX_FLOW_EN |\ 91 CPSW_SL_CTL_TX_FLOW_EN |\ 92 CPSW_SL_CTL_GMII_EN |\ 93 CPSW_SL_CTL_TX_PACE |\ 94 CPSW_SL_CTL_GIG |\ 95 CPSW_SL_CTL_CMD_IDLE |\ 96 CPSW_SL_CTL_IFCTL_A |\ 97 CPSW_SL_CTL_IFCTL_B |\ 98 CPSW_SL_CTL_GIG_FORCE |\ 99 CPSW_SL_CTL_EXT_EN |\ 100 CPSW_SL_CTL_RX_CEF_EN |\ 101 CPSW_SL_CTL_RX_CSF_EN |\ 102 CPSW_SL_CTL_RX_CMF_EN) 103 104 struct cpsw_sl { 105 struct device *dev; 106 void __iomem *sl_base; 107 const u16 *regs; 108 u32 control_features; 109 u32 idle_mask; 110 }; 111 112 struct cpsw_sl_dev_id { 113 const char *device_id; 114 const u16 *regs; 115 const u32 control_features; 116 const u32 regs_offset; 117 const u32 idle_mask; 118 }; 119 120 static const struct cpsw_sl_dev_id cpsw_sl_id_match[] = { 121 { 122 .device_id = "cpsw", 123 .regs = cpsw_sl_reg_map_cpsw, 124 .control_features = CPSW_SL_CTL_FUNC_BASE | 125 CPSW_SL_CTL_MTEST | 126 CPSW_SL_CTL_TX_SHORT_GAP_EN | 127 CPSW_SL_CTL_TX_SG_LIM_EN, 128 .idle_mask = CPSW_SL_STATUS_IDLE_MASK_BASE, 129 }, 130 { 131 .device_id = "66ak2hk", 132 .regs = cpsw_sl_reg_map_66ak2hk, 133 .control_features = CPSW_SL_CTL_FUNC_BASE | 134 CPSW_SL_CTL_TX_SHORT_GAP_EN, 135 .idle_mask = CPSW_SL_STATUS_IDLE_MASK_BASE, 136 }, 137 { 138 .device_id = "66ak2x_xgbe", 139 .regs = cpsw_sl_reg_map_66ak2x_xgbe, 140 .control_features = CPSW_SL_CTL_FUNC_BASE | 141 CPSW_SL_CTL_XGIG | 142 CPSW_SL_CTL_TX_SHORT_GAP_EN | 143 CPSW_SL_CTL_CRC_TYPE | 144 CPSW_SL_CTL_XGMII_EN, 145 .idle_mask = CPSW_SL_STATUS_IDLE_MASK_BASE, 146 }, 147 { 148 .device_id = "66ak2el", 149 .regs = cpsw_sl_reg_map_66ak2elg_am65, 150 .regs_offset = 0x330, 151 .control_features = CPSW_SL_CTL_FUNC_BASE | 152 CPSW_SL_CTL_MTEST | 153 CPSW_SL_CTL_TX_SHORT_GAP_EN | 154 CPSW_SL_CTL_CRC_TYPE | 155 CPSW_SL_CTL_EXT_EN_RX_FLO | 156 CPSW_SL_CTL_EXT_EN_TX_FLO | 157 CPSW_SL_CTL_TX_SG_LIM_EN, 158 .idle_mask = CPSW_SL_STATUS_IDLE_MASK_BASE, 159 }, 160 { 161 .device_id = "66ak2g", 162 .regs = cpsw_sl_reg_map_66ak2elg_am65, 163 .regs_offset = 0x330, 164 .control_features = CPSW_SL_CTL_FUNC_BASE | 165 CPSW_SL_CTL_MTEST | 166 CPSW_SL_CTL_CRC_TYPE | 167 CPSW_SL_CTL_EXT_EN_RX_FLO | 168 CPSW_SL_CTL_EXT_EN_TX_FLO, 169 }, 170 { 171 .device_id = "am65", 172 .regs = cpsw_sl_reg_map_66ak2elg_am65, 173 .regs_offset = 0x330, 174 .control_features = CPSW_SL_CTL_FUNC_BASE | 175 CPSW_SL_CTL_MTEST | 176 CPSW_SL_CTL_XGIG | 177 CPSW_SL_CTL_TX_SHORT_GAP_EN | 178 CPSW_SL_CTL_CRC_TYPE | 179 CPSW_SL_CTL_XGMII_EN | 180 CPSW_SL_CTL_EXT_EN_RX_FLO | 181 CPSW_SL_CTL_EXT_EN_TX_FLO | 182 CPSW_SL_CTL_TX_SG_LIM_EN | 183 CPSW_SL_CTL_EXT_EN_XGIG, 184 .idle_mask = CPSW_SL_STATUS_IDLE_MASK_K3, 185 }, 186 { }, 187 }; 188 189 u32 cpsw_sl_reg_read(struct cpsw_sl *sl, enum cpsw_sl_regs reg) 190 { 191 int val; 192 193 if (sl->regs[reg] == CPSW_SL_REG_NOTUSED) { 194 dev_err(sl->dev, "cpsw_sl: not sup r reg: %04X\n", 195 sl->regs[reg]); 196 return 0; 197 } 198 199 val = readl(sl->sl_base + sl->regs[reg]); 200 dev_dbg(sl->dev, "cpsw_sl: reg: %04X r 0x%08X\n", sl->regs[reg], val); 201 return val; 202 } 203 EXPORT_SYMBOL_GPL(cpsw_sl_reg_read); 204 205 void cpsw_sl_reg_write(struct cpsw_sl *sl, enum cpsw_sl_regs reg, u32 val) 206 { 207 if (sl->regs[reg] == CPSW_SL_REG_NOTUSED) { 208 dev_err(sl->dev, "cpsw_sl: not sup w reg: %04X\n", 209 sl->regs[reg]); 210 return; 211 } 212 213 dev_dbg(sl->dev, "cpsw_sl: reg: %04X w 0x%08X\n", sl->regs[reg], val); 214 writel(val, sl->sl_base + sl->regs[reg]); 215 } 216 EXPORT_SYMBOL_GPL(cpsw_sl_reg_write); 217 218 static const struct cpsw_sl_dev_id *cpsw_sl_match_id( 219 const struct cpsw_sl_dev_id *id, 220 const char *device_id) 221 { 222 if (!id || !device_id) 223 return NULL; 224 225 while (id->device_id) { 226 if (strcmp(device_id, id->device_id) == 0) 227 return id; 228 id++; 229 } 230 return NULL; 231 } 232 233 struct cpsw_sl *cpsw_sl_get(const char *device_id, struct device *dev, 234 void __iomem *sl_base) 235 { 236 const struct cpsw_sl_dev_id *sl_dev_id; 237 struct cpsw_sl *sl; 238 239 sl = devm_kzalloc(dev, sizeof(struct cpsw_sl), GFP_KERNEL); 240 if (!sl) 241 return ERR_PTR(-ENOMEM); 242 sl->dev = dev; 243 sl->sl_base = sl_base; 244 245 sl_dev_id = cpsw_sl_match_id(cpsw_sl_id_match, device_id); 246 if (!sl_dev_id) { 247 dev_err(sl->dev, "cpsw_sl: dev_id %s not found.\n", device_id); 248 return ERR_PTR(-EINVAL); 249 } 250 sl->regs = sl_dev_id->regs; 251 sl->control_features = sl_dev_id->control_features; 252 sl->idle_mask = sl_dev_id->idle_mask; 253 sl->sl_base += sl_dev_id->regs_offset; 254 255 return sl; 256 } 257 EXPORT_SYMBOL_GPL(cpsw_sl_get); 258 259 void cpsw_sl_reset(struct cpsw_sl *sl, unsigned long tmo) 260 { 261 unsigned long timeout = jiffies + msecs_to_jiffies(tmo); 262 263 /* Set the soft reset bit */ 264 cpsw_sl_reg_write(sl, CPSW_SL_SOFT_RESET, CPSW_SL_SOFT_RESET_BIT); 265 266 /* Wait for the bit to clear */ 267 do { 268 usleep_range(100, 200); 269 } while ((cpsw_sl_reg_read(sl, CPSW_SL_SOFT_RESET) & 270 CPSW_SL_SOFT_RESET_BIT) && 271 time_after(timeout, jiffies)); 272 273 if (cpsw_sl_reg_read(sl, CPSW_SL_SOFT_RESET) & CPSW_SL_SOFT_RESET_BIT) 274 dev_err(sl->dev, "cpsw_sl failed to soft-reset.\n"); 275 } 276 EXPORT_SYMBOL_GPL(cpsw_sl_reset); 277 278 u32 cpsw_sl_ctl_set(struct cpsw_sl *sl, u32 ctl_funcs) 279 { 280 u32 val; 281 282 if (ctl_funcs & ~sl->control_features) { 283 dev_err(sl->dev, "cpsw_sl: unsupported func 0x%08X\n", 284 ctl_funcs & (~sl->control_features)); 285 return -EINVAL; 286 } 287 288 val = cpsw_sl_reg_read(sl, CPSW_SL_MACCONTROL); 289 val |= ctl_funcs; 290 cpsw_sl_reg_write(sl, CPSW_SL_MACCONTROL, val); 291 292 return 0; 293 } 294 EXPORT_SYMBOL_GPL(cpsw_sl_ctl_set); 295 296 u32 cpsw_sl_ctl_clr(struct cpsw_sl *sl, u32 ctl_funcs) 297 { 298 u32 val; 299 300 if (ctl_funcs & ~sl->control_features) { 301 dev_err(sl->dev, "cpsw_sl: unsupported func 0x%08X\n", 302 ctl_funcs & (~sl->control_features)); 303 return -EINVAL; 304 } 305 306 val = cpsw_sl_reg_read(sl, CPSW_SL_MACCONTROL); 307 val &= ~ctl_funcs; 308 cpsw_sl_reg_write(sl, CPSW_SL_MACCONTROL, val); 309 310 return 0; 311 } 312 EXPORT_SYMBOL_GPL(cpsw_sl_ctl_clr); 313 314 void cpsw_sl_ctl_reset(struct cpsw_sl *sl) 315 { 316 cpsw_sl_reg_write(sl, CPSW_SL_MACCONTROL, 0); 317 } 318 EXPORT_SYMBOL_GPL(cpsw_sl_ctl_reset); 319 320 int cpsw_sl_wait_for_idle(struct cpsw_sl *sl, unsigned long tmo) 321 { 322 unsigned long timeout = jiffies + msecs_to_jiffies(tmo); 323 324 do { 325 usleep_range(100, 200); 326 } while (!(cpsw_sl_reg_read(sl, CPSW_SL_MACSTATUS) & 327 sl->idle_mask) && time_after(timeout, jiffies)); 328 329 if (!(cpsw_sl_reg_read(sl, CPSW_SL_MACSTATUS) & sl->idle_mask)) { 330 dev_err(sl->dev, "cpsw_sl failed to soft-reset.\n"); 331 return -ETIMEDOUT; 332 } 333 334 return 0; 335 } 336 EXPORT_SYMBOL_GPL(cpsw_sl_wait_for_idle); 337 338 MODULE_LICENSE("GPL"); 339 MODULE_DESCRIPTION("TI Ethernet Switch media-access-controller (MAC) submodule"); 340