Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Pengutronix, Juergen Borleis <kernel@pengutronix.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | */ |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/gpio/consumer.h> |
| 17 | #include <linux/regmap.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/mii.h> |
| 20 | |
| 21 | #include "lan9303.h" |
| 22 | |
Egil Hjelmeland | ab78acb | 2017-07-30 19:58:54 +0200 | [diff] [blame] | 23 | /* 13.2 System Control and Status Registers |
| 24 | * Multiply register number by 4 to get address offset. |
| 25 | */ |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 26 | #define LAN9303_CHIP_REV 0x14 |
| 27 | # define LAN9303_CHIP_ID 0x9303 |
| 28 | #define LAN9303_IRQ_CFG 0x15 |
| 29 | # define LAN9303_IRQ_CFG_IRQ_ENABLE BIT(8) |
| 30 | # define LAN9303_IRQ_CFG_IRQ_POL BIT(4) |
| 31 | # define LAN9303_IRQ_CFG_IRQ_TYPE BIT(0) |
| 32 | #define LAN9303_INT_STS 0x16 |
| 33 | # define LAN9303_INT_STS_PHY_INT2 BIT(27) |
| 34 | # define LAN9303_INT_STS_PHY_INT1 BIT(26) |
| 35 | #define LAN9303_INT_EN 0x17 |
| 36 | # define LAN9303_INT_EN_PHY_INT2_EN BIT(27) |
| 37 | # define LAN9303_INT_EN_PHY_INT1_EN BIT(26) |
| 38 | #define LAN9303_HW_CFG 0x1D |
| 39 | # define LAN9303_HW_CFG_READY BIT(27) |
| 40 | # define LAN9303_HW_CFG_AMDX_EN_PORT2 BIT(26) |
| 41 | # define LAN9303_HW_CFG_AMDX_EN_PORT1 BIT(25) |
| 42 | #define LAN9303_PMI_DATA 0x29 |
| 43 | #define LAN9303_PMI_ACCESS 0x2A |
| 44 | # define LAN9303_PMI_ACCESS_PHY_ADDR(x) (((x) & 0x1f) << 11) |
| 45 | # define LAN9303_PMI_ACCESS_MIIRINDA(x) (((x) & 0x1f) << 6) |
| 46 | # define LAN9303_PMI_ACCESS_MII_BUSY BIT(0) |
| 47 | # define LAN9303_PMI_ACCESS_MII_WRITE BIT(1) |
| 48 | #define LAN9303_MANUAL_FC_1 0x68 |
| 49 | #define LAN9303_MANUAL_FC_2 0x69 |
| 50 | #define LAN9303_MANUAL_FC_0 0x6a |
| 51 | #define LAN9303_SWITCH_CSR_DATA 0x6b |
| 52 | #define LAN9303_SWITCH_CSR_CMD 0x6c |
| 53 | #define LAN9303_SWITCH_CSR_CMD_BUSY BIT(31) |
| 54 | #define LAN9303_SWITCH_CSR_CMD_RW BIT(30) |
| 55 | #define LAN9303_SWITCH_CSR_CMD_LANES (BIT(19) | BIT(18) | BIT(17) | BIT(16)) |
| 56 | #define LAN9303_VIRT_PHY_BASE 0x70 |
| 57 | #define LAN9303_VIRT_SPECIAL_CTRL 0x77 |
| 58 | |
Egil Hjelmeland | ab78acb | 2017-07-30 19:58:54 +0200 | [diff] [blame] | 59 | /*13.4 Switch Fabric Control and Status Registers |
| 60 | * Accessed indirectly via SWITCH_CSR_CMD, SWITCH_CSR_DATA. |
| 61 | */ |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 62 | #define LAN9303_SW_DEV_ID 0x0000 |
| 63 | #define LAN9303_SW_RESET 0x0001 |
| 64 | #define LAN9303_SW_RESET_RESET BIT(0) |
| 65 | #define LAN9303_SW_IMR 0x0004 |
| 66 | #define LAN9303_SW_IPR 0x0005 |
| 67 | #define LAN9303_MAC_VER_ID_0 0x0400 |
| 68 | #define LAN9303_MAC_RX_CFG_0 0x0401 |
| 69 | # define LAN9303_MAC_RX_CFG_X_REJECT_MAC_TYPES BIT(1) |
| 70 | # define LAN9303_MAC_RX_CFG_X_RX_ENABLE BIT(0) |
| 71 | #define LAN9303_MAC_RX_UNDSZE_CNT_0 0x0410 |
| 72 | #define LAN9303_MAC_RX_64_CNT_0 0x0411 |
| 73 | #define LAN9303_MAC_RX_127_CNT_0 0x0412 |
| 74 | #define LAN9303_MAC_RX_255_CNT_0 0x413 |
| 75 | #define LAN9303_MAC_RX_511_CNT_0 0x0414 |
| 76 | #define LAN9303_MAC_RX_1023_CNT_0 0x0415 |
| 77 | #define LAN9303_MAC_RX_MAX_CNT_0 0x0416 |
| 78 | #define LAN9303_MAC_RX_OVRSZE_CNT_0 0x0417 |
| 79 | #define LAN9303_MAC_RX_PKTOK_CNT_0 0x0418 |
| 80 | #define LAN9303_MAC_RX_CRCERR_CNT_0 0x0419 |
| 81 | #define LAN9303_MAC_RX_MULCST_CNT_0 0x041a |
| 82 | #define LAN9303_MAC_RX_BRDCST_CNT_0 0x041b |
| 83 | #define LAN9303_MAC_RX_PAUSE_CNT_0 0x041c |
| 84 | #define LAN9303_MAC_RX_FRAG_CNT_0 0x041d |
| 85 | #define LAN9303_MAC_RX_JABB_CNT_0 0x041e |
| 86 | #define LAN9303_MAC_RX_ALIGN_CNT_0 0x041f |
| 87 | #define LAN9303_MAC_RX_PKTLEN_CNT_0 0x0420 |
| 88 | #define LAN9303_MAC_RX_GOODPKTLEN_CNT_0 0x0421 |
| 89 | #define LAN9303_MAC_RX_SYMBL_CNT_0 0x0422 |
| 90 | #define LAN9303_MAC_RX_CTLFRM_CNT_0 0x0423 |
| 91 | |
| 92 | #define LAN9303_MAC_TX_CFG_0 0x0440 |
| 93 | # define LAN9303_MAC_TX_CFG_X_TX_IFG_CONFIG_DEFAULT (21 << 2) |
| 94 | # define LAN9303_MAC_TX_CFG_X_TX_PAD_ENABLE BIT(1) |
| 95 | # define LAN9303_MAC_TX_CFG_X_TX_ENABLE BIT(0) |
| 96 | #define LAN9303_MAC_TX_DEFER_CNT_0 0x0451 |
| 97 | #define LAN9303_MAC_TX_PAUSE_CNT_0 0x0452 |
| 98 | #define LAN9303_MAC_TX_PKTOK_CNT_0 0x0453 |
| 99 | #define LAN9303_MAC_TX_64_CNT_0 0x0454 |
| 100 | #define LAN9303_MAC_TX_127_CNT_0 0x0455 |
| 101 | #define LAN9303_MAC_TX_255_CNT_0 0x0456 |
| 102 | #define LAN9303_MAC_TX_511_CNT_0 0x0457 |
| 103 | #define LAN9303_MAC_TX_1023_CNT_0 0x0458 |
| 104 | #define LAN9303_MAC_TX_MAX_CNT_0 0x0459 |
| 105 | #define LAN9303_MAC_TX_UNDSZE_CNT_0 0x045a |
| 106 | #define LAN9303_MAC_TX_PKTLEN_CNT_0 0x045c |
| 107 | #define LAN9303_MAC_TX_BRDCST_CNT_0 0x045d |
| 108 | #define LAN9303_MAC_TX_MULCST_CNT_0 0x045e |
| 109 | #define LAN9303_MAC_TX_LATECOL_0 0x045f |
| 110 | #define LAN9303_MAC_TX_EXCOL_CNT_0 0x0460 |
| 111 | #define LAN9303_MAC_TX_SNGLECOL_CNT_0 0x0461 |
| 112 | #define LAN9303_MAC_TX_MULTICOL_CNT_0 0x0462 |
| 113 | #define LAN9303_MAC_TX_TOTALCOL_CNT_0 0x0463 |
| 114 | |
| 115 | #define LAN9303_MAC_VER_ID_1 0x0800 |
| 116 | #define LAN9303_MAC_RX_CFG_1 0x0801 |
| 117 | #define LAN9303_MAC_TX_CFG_1 0x0840 |
| 118 | #define LAN9303_MAC_VER_ID_2 0x0c00 |
| 119 | #define LAN9303_MAC_RX_CFG_2 0x0c01 |
| 120 | #define LAN9303_MAC_TX_CFG_2 0x0c40 |
| 121 | #define LAN9303_SWE_ALR_CMD 0x1800 |
| 122 | #define LAN9303_SWE_VLAN_CMD 0x180b |
| 123 | # define LAN9303_SWE_VLAN_CMD_RNW BIT(5) |
| 124 | # define LAN9303_SWE_VLAN_CMD_PVIDNVLAN BIT(4) |
| 125 | #define LAN9303_SWE_VLAN_WR_DATA 0x180c |
| 126 | #define LAN9303_SWE_VLAN_RD_DATA 0x180e |
| 127 | # define LAN9303_SWE_VLAN_MEMBER_PORT2 BIT(17) |
| 128 | # define LAN9303_SWE_VLAN_UNTAG_PORT2 BIT(16) |
| 129 | # define LAN9303_SWE_VLAN_MEMBER_PORT1 BIT(15) |
| 130 | # define LAN9303_SWE_VLAN_UNTAG_PORT1 BIT(14) |
| 131 | # define LAN9303_SWE_VLAN_MEMBER_PORT0 BIT(13) |
| 132 | # define LAN9303_SWE_VLAN_UNTAG_PORT0 BIT(12) |
| 133 | #define LAN9303_SWE_VLAN_CMD_STS 0x1810 |
| 134 | #define LAN9303_SWE_GLB_INGRESS_CFG 0x1840 |
| 135 | #define LAN9303_SWE_PORT_STATE 0x1843 |
| 136 | # define LAN9303_SWE_PORT_STATE_FORWARDING_PORT2 (0) |
| 137 | # define LAN9303_SWE_PORT_STATE_LEARNING_PORT2 BIT(5) |
| 138 | # define LAN9303_SWE_PORT_STATE_BLOCKING_PORT2 BIT(4) |
| 139 | # define LAN9303_SWE_PORT_STATE_FORWARDING_PORT1 (0) |
| 140 | # define LAN9303_SWE_PORT_STATE_LEARNING_PORT1 BIT(3) |
| 141 | # define LAN9303_SWE_PORT_STATE_BLOCKING_PORT1 BIT(2) |
| 142 | # define LAN9303_SWE_PORT_STATE_FORWARDING_PORT0 (0) |
| 143 | # define LAN9303_SWE_PORT_STATE_LEARNING_PORT0 BIT(1) |
| 144 | # define LAN9303_SWE_PORT_STATE_BLOCKING_PORT0 BIT(0) |
| 145 | #define LAN9303_SWE_PORT_MIRROR 0x1846 |
| 146 | # define LAN9303_SWE_PORT_MIRROR_SNIFF_ALL BIT(8) |
| 147 | # define LAN9303_SWE_PORT_MIRROR_SNIFFER_PORT2 BIT(7) |
| 148 | # define LAN9303_SWE_PORT_MIRROR_SNIFFER_PORT1 BIT(6) |
| 149 | # define LAN9303_SWE_PORT_MIRROR_SNIFFER_PORT0 BIT(5) |
| 150 | # define LAN9303_SWE_PORT_MIRROR_MIRRORED_PORT2 BIT(4) |
| 151 | # define LAN9303_SWE_PORT_MIRROR_MIRRORED_PORT1 BIT(3) |
| 152 | # define LAN9303_SWE_PORT_MIRROR_MIRRORED_PORT0 BIT(2) |
| 153 | # define LAN9303_SWE_PORT_MIRROR_ENABLE_RX_MIRRORING BIT(1) |
| 154 | # define LAN9303_SWE_PORT_MIRROR_ENABLE_TX_MIRRORING BIT(0) |
| 155 | #define LAN9303_SWE_INGRESS_PORT_TYPE 0x1847 |
| 156 | #define LAN9303_BM_CFG 0x1c00 |
| 157 | #define LAN9303_BM_EGRSS_PORT_TYPE 0x1c0c |
| 158 | # define LAN9303_BM_EGRSS_PORT_TYPE_SPECIAL_TAG_PORT2 (BIT(17) | BIT(16)) |
| 159 | # define LAN9303_BM_EGRSS_PORT_TYPE_SPECIAL_TAG_PORT1 (BIT(9) | BIT(8)) |
| 160 | # define LAN9303_BM_EGRSS_PORT_TYPE_SPECIAL_TAG_PORT0 (BIT(1) | BIT(0)) |
| 161 | |
| 162 | #define LAN9303_PORT_0_OFFSET 0x400 |
| 163 | #define LAN9303_PORT_1_OFFSET 0x800 |
| 164 | #define LAN9303_PORT_2_OFFSET 0xc00 |
| 165 | |
| 166 | /* the built-in PHYs are of type LAN911X */ |
| 167 | #define MII_LAN911X_SPECIAL_MODES 0x12 |
| 168 | #define MII_LAN911X_SPECIAL_CONTROL_STATUS 0x1f |
| 169 | |
| 170 | static const struct regmap_range lan9303_valid_regs[] = { |
| 171 | regmap_reg_range(0x14, 0x17), /* misc, interrupt */ |
| 172 | regmap_reg_range(0x19, 0x19), /* endian test */ |
| 173 | regmap_reg_range(0x1d, 0x1d), /* hardware config */ |
| 174 | regmap_reg_range(0x23, 0x24), /* general purpose timer */ |
| 175 | regmap_reg_range(0x27, 0x27), /* counter */ |
| 176 | regmap_reg_range(0x29, 0x2a), /* PMI index regs */ |
| 177 | regmap_reg_range(0x68, 0x6a), /* flow control */ |
| 178 | regmap_reg_range(0x6b, 0x6c), /* switch fabric indirect regs */ |
| 179 | regmap_reg_range(0x6d, 0x6f), /* misc */ |
| 180 | regmap_reg_range(0x70, 0x77), /* virtual phy */ |
| 181 | regmap_reg_range(0x78, 0x7a), /* GPIO */ |
| 182 | regmap_reg_range(0x7c, 0x7e), /* MAC & reset */ |
| 183 | regmap_reg_range(0x80, 0xb7), /* switch fabric direct regs (wr only) */ |
| 184 | }; |
| 185 | |
| 186 | static const struct regmap_range lan9303_reserved_ranges[] = { |
| 187 | regmap_reg_range(0x00, 0x13), |
| 188 | regmap_reg_range(0x18, 0x18), |
| 189 | regmap_reg_range(0x1a, 0x1c), |
| 190 | regmap_reg_range(0x1e, 0x22), |
| 191 | regmap_reg_range(0x25, 0x26), |
| 192 | regmap_reg_range(0x28, 0x28), |
| 193 | regmap_reg_range(0x2b, 0x67), |
| 194 | regmap_reg_range(0x7b, 0x7b), |
| 195 | regmap_reg_range(0x7f, 0x7f), |
| 196 | regmap_reg_range(0xb8, 0xff), |
| 197 | }; |
| 198 | |
| 199 | const struct regmap_access_table lan9303_register_set = { |
| 200 | .yes_ranges = lan9303_valid_regs, |
| 201 | .n_yes_ranges = ARRAY_SIZE(lan9303_valid_regs), |
| 202 | .no_ranges = lan9303_reserved_ranges, |
| 203 | .n_no_ranges = ARRAY_SIZE(lan9303_reserved_ranges), |
| 204 | }; |
| 205 | EXPORT_SYMBOL(lan9303_register_set); |
| 206 | |
| 207 | static int lan9303_read(struct regmap *regmap, unsigned int offset, u32 *reg) |
| 208 | { |
| 209 | int ret, i; |
| 210 | |
| 211 | /* we can lose arbitration for the I2C case, because the device |
| 212 | * tries to detect and read an external EEPROM after reset and acts as |
| 213 | * a master on the shared I2C bus itself. This conflicts with our |
| 214 | * attempts to access the device as a slave at the same moment. |
| 215 | */ |
| 216 | for (i = 0; i < 5; i++) { |
| 217 | ret = regmap_read(regmap, offset, reg); |
| 218 | if (!ret) |
| 219 | return 0; |
| 220 | if (ret != -EAGAIN) |
| 221 | break; |
| 222 | msleep(500); |
| 223 | } |
| 224 | |
| 225 | return -EIO; |
| 226 | } |
| 227 | |
| 228 | static int lan9303_virt_phy_reg_read(struct lan9303 *chip, int regnum) |
| 229 | { |
| 230 | int ret; |
| 231 | u32 val; |
| 232 | |
| 233 | if (regnum > MII_EXPANSION) |
| 234 | return -EINVAL; |
| 235 | |
| 236 | ret = lan9303_read(chip->regmap, LAN9303_VIRT_PHY_BASE + regnum, &val); |
| 237 | if (ret) |
| 238 | return ret; |
| 239 | |
| 240 | return val & 0xffff; |
| 241 | } |
| 242 | |
| 243 | static int lan9303_virt_phy_reg_write(struct lan9303 *chip, int regnum, u16 val) |
| 244 | { |
| 245 | if (regnum > MII_EXPANSION) |
| 246 | return -EINVAL; |
| 247 | |
| 248 | return regmap_write(chip->regmap, LAN9303_VIRT_PHY_BASE + regnum, val); |
| 249 | } |
| 250 | |
Egil Hjelmeland | 9e866e5 | 2017-07-30 19:58:55 +0200 | [diff] [blame] | 251 | static int lan9303_indirect_phy_wait_for_completion(struct lan9303 *chip) |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 252 | { |
| 253 | int ret, i; |
| 254 | u32 reg; |
| 255 | |
| 256 | for (i = 0; i < 25; i++) { |
| 257 | ret = lan9303_read(chip->regmap, LAN9303_PMI_ACCESS, ®); |
| 258 | if (ret) { |
| 259 | dev_err(chip->dev, |
| 260 | "Failed to read pmi access status: %d\n", ret); |
| 261 | return ret; |
| 262 | } |
| 263 | if (!(reg & LAN9303_PMI_ACCESS_MII_BUSY)) |
| 264 | return 0; |
| 265 | msleep(1); |
| 266 | } |
| 267 | |
| 268 | return -EIO; |
| 269 | } |
| 270 | |
Egil Hjelmeland | 9e866e5 | 2017-07-30 19:58:55 +0200 | [diff] [blame] | 271 | static int lan9303_indirect_phy_read(struct lan9303 *chip, int addr, int regnum) |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 272 | { |
| 273 | int ret; |
| 274 | u32 val; |
| 275 | |
| 276 | val = LAN9303_PMI_ACCESS_PHY_ADDR(addr); |
| 277 | val |= LAN9303_PMI_ACCESS_MIIRINDA(regnum); |
| 278 | |
| 279 | mutex_lock(&chip->indirect_mutex); |
| 280 | |
Egil Hjelmeland | 9e866e5 | 2017-07-30 19:58:55 +0200 | [diff] [blame] | 281 | ret = lan9303_indirect_phy_wait_for_completion(chip); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 282 | if (ret) |
| 283 | goto on_error; |
| 284 | |
| 285 | /* start the MII read cycle */ |
| 286 | ret = regmap_write(chip->regmap, LAN9303_PMI_ACCESS, val); |
| 287 | if (ret) |
| 288 | goto on_error; |
| 289 | |
Egil Hjelmeland | 9e866e5 | 2017-07-30 19:58:55 +0200 | [diff] [blame] | 290 | ret = lan9303_indirect_phy_wait_for_completion(chip); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 291 | if (ret) |
| 292 | goto on_error; |
| 293 | |
| 294 | /* read the result of this operation */ |
| 295 | ret = lan9303_read(chip->regmap, LAN9303_PMI_DATA, &val); |
| 296 | if (ret) |
| 297 | goto on_error; |
| 298 | |
| 299 | mutex_unlock(&chip->indirect_mutex); |
| 300 | |
| 301 | return val & 0xffff; |
| 302 | |
| 303 | on_error: |
| 304 | mutex_unlock(&chip->indirect_mutex); |
| 305 | return ret; |
| 306 | } |
| 307 | |
Egil Hjelmeland | 9e866e5 | 2017-07-30 19:58:55 +0200 | [diff] [blame] | 308 | static int lan9303_indirect_phy_write(struct lan9303 *chip, int addr, |
| 309 | int regnum, u16 val) |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 310 | { |
| 311 | int ret; |
| 312 | u32 reg; |
| 313 | |
| 314 | reg = LAN9303_PMI_ACCESS_PHY_ADDR(addr); |
| 315 | reg |= LAN9303_PMI_ACCESS_MIIRINDA(regnum); |
| 316 | reg |= LAN9303_PMI_ACCESS_MII_WRITE; |
| 317 | |
| 318 | mutex_lock(&chip->indirect_mutex); |
| 319 | |
Egil Hjelmeland | 9e866e5 | 2017-07-30 19:58:55 +0200 | [diff] [blame] | 320 | ret = lan9303_indirect_phy_wait_for_completion(chip); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 321 | if (ret) |
| 322 | goto on_error; |
| 323 | |
| 324 | /* write the data first... */ |
| 325 | ret = regmap_write(chip->regmap, LAN9303_PMI_DATA, val); |
| 326 | if (ret) |
| 327 | goto on_error; |
| 328 | |
| 329 | /* ...then start the MII write cycle */ |
| 330 | ret = regmap_write(chip->regmap, LAN9303_PMI_ACCESS, reg); |
| 331 | |
| 332 | on_error: |
| 333 | mutex_unlock(&chip->indirect_mutex); |
| 334 | return ret; |
| 335 | } |
| 336 | |
Egil Hjelmeland | 2c34089 | 2017-07-30 19:58:56 +0200 | [diff] [blame^] | 337 | const struct lan9303_phy_ops lan9303_indirect_phy_ops = { |
| 338 | .phy_read = lan9303_indirect_phy_read, |
| 339 | .phy_write = lan9303_indirect_phy_write, |
| 340 | }; |
| 341 | EXPORT_SYMBOL_GPL(lan9303_indirect_phy_ops); |
| 342 | |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 343 | static int lan9303_switch_wait_for_completion(struct lan9303 *chip) |
| 344 | { |
| 345 | int ret, i; |
| 346 | u32 reg; |
| 347 | |
| 348 | for (i = 0; i < 25; i++) { |
| 349 | ret = lan9303_read(chip->regmap, LAN9303_SWITCH_CSR_CMD, ®); |
| 350 | if (ret) { |
| 351 | dev_err(chip->dev, |
| 352 | "Failed to read csr command status: %d\n", ret); |
| 353 | return ret; |
| 354 | } |
| 355 | if (!(reg & LAN9303_SWITCH_CSR_CMD_BUSY)) |
| 356 | return 0; |
| 357 | msleep(1); |
| 358 | } |
| 359 | |
| 360 | return -EIO; |
| 361 | } |
| 362 | |
| 363 | static int lan9303_write_switch_reg(struct lan9303 *chip, u16 regnum, u32 val) |
| 364 | { |
| 365 | u32 reg; |
| 366 | int ret; |
| 367 | |
| 368 | reg = regnum; |
| 369 | reg |= LAN9303_SWITCH_CSR_CMD_LANES; |
| 370 | reg |= LAN9303_SWITCH_CSR_CMD_BUSY; |
| 371 | |
| 372 | mutex_lock(&chip->indirect_mutex); |
| 373 | |
| 374 | ret = lan9303_switch_wait_for_completion(chip); |
| 375 | if (ret) |
| 376 | goto on_error; |
| 377 | |
| 378 | ret = regmap_write(chip->regmap, LAN9303_SWITCH_CSR_DATA, val); |
| 379 | if (ret) { |
| 380 | dev_err(chip->dev, "Failed to write csr data reg: %d\n", ret); |
| 381 | goto on_error; |
| 382 | } |
| 383 | |
| 384 | /* trigger write */ |
| 385 | ret = regmap_write(chip->regmap, LAN9303_SWITCH_CSR_CMD, reg); |
| 386 | if (ret) |
| 387 | dev_err(chip->dev, "Failed to write csr command reg: %d\n", |
| 388 | ret); |
| 389 | |
| 390 | on_error: |
| 391 | mutex_unlock(&chip->indirect_mutex); |
| 392 | return ret; |
| 393 | } |
| 394 | |
| 395 | static int lan9303_read_switch_reg(struct lan9303 *chip, u16 regnum, u32 *val) |
| 396 | { |
| 397 | u32 reg; |
| 398 | int ret; |
| 399 | |
| 400 | reg = regnum; |
| 401 | reg |= LAN9303_SWITCH_CSR_CMD_LANES; |
| 402 | reg |= LAN9303_SWITCH_CSR_CMD_RW; |
| 403 | reg |= LAN9303_SWITCH_CSR_CMD_BUSY; |
| 404 | |
| 405 | mutex_lock(&chip->indirect_mutex); |
| 406 | |
| 407 | ret = lan9303_switch_wait_for_completion(chip); |
| 408 | if (ret) |
| 409 | goto on_error; |
| 410 | |
| 411 | /* trigger read */ |
| 412 | ret = regmap_write(chip->regmap, LAN9303_SWITCH_CSR_CMD, reg); |
| 413 | if (ret) { |
| 414 | dev_err(chip->dev, "Failed to write csr command reg: %d\n", |
| 415 | ret); |
| 416 | goto on_error; |
| 417 | } |
| 418 | |
| 419 | ret = lan9303_switch_wait_for_completion(chip); |
| 420 | if (ret) |
| 421 | goto on_error; |
| 422 | |
| 423 | ret = lan9303_read(chip->regmap, LAN9303_SWITCH_CSR_DATA, val); |
| 424 | if (ret) |
| 425 | dev_err(chip->dev, "Failed to read csr data reg: %d\n", ret); |
| 426 | on_error: |
| 427 | mutex_unlock(&chip->indirect_mutex); |
| 428 | return ret; |
| 429 | } |
| 430 | |
| 431 | static int lan9303_detect_phy_setup(struct lan9303 *chip) |
| 432 | { |
| 433 | int reg; |
| 434 | |
| 435 | /* depending on the 'phy_addr_sel_strap' setting, the three phys are |
| 436 | * using IDs 0-1-2 or IDs 1-2-3. We cannot read back the |
| 437 | * 'phy_addr_sel_strap' setting directly, so we need a test, which |
| 438 | * configuration is active: |
| 439 | * Special reg 18 of phy 3 reads as 0x0000, if 'phy_addr_sel_strap' is 0 |
| 440 | * and the IDs are 0-1-2, else it contains something different from |
| 441 | * 0x0000, which means 'phy_addr_sel_strap' is 1 and the IDs are 1-2-3. |
Egil Hjelmeland | d329ac8 | 2017-07-30 19:58:53 +0200 | [diff] [blame] | 442 | * 0xffff is returned on MDIO read with no response. |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 443 | */ |
Egil Hjelmeland | 2c34089 | 2017-07-30 19:58:56 +0200 | [diff] [blame^] | 444 | reg = chip->ops->phy_read(chip, 3, MII_LAN911X_SPECIAL_MODES); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 445 | if (reg < 0) { |
| 446 | dev_err(chip->dev, "Failed to detect phy config: %d\n", reg); |
| 447 | return reg; |
| 448 | } |
| 449 | |
Egil Hjelmeland | d329ac8 | 2017-07-30 19:58:53 +0200 | [diff] [blame] | 450 | if ((reg != 0) && (reg != 0xffff)) |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 451 | chip->phy_addr_sel_strap = 1; |
| 452 | else |
| 453 | chip->phy_addr_sel_strap = 0; |
| 454 | |
| 455 | dev_dbg(chip->dev, "Phy setup '%s' detected\n", |
| 456 | chip->phy_addr_sel_strap ? "1-2-3" : "0-1-2"); |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | #define LAN9303_MAC_RX_CFG_OFFS (LAN9303_MAC_RX_CFG_0 - LAN9303_PORT_0_OFFSET) |
| 462 | #define LAN9303_MAC_TX_CFG_OFFS (LAN9303_MAC_TX_CFG_0 - LAN9303_PORT_0_OFFSET) |
| 463 | |
| 464 | static int lan9303_disable_packet_processing(struct lan9303 *chip, |
| 465 | unsigned int port) |
| 466 | { |
| 467 | int ret; |
| 468 | |
| 469 | /* disable RX, but keep register reset default values else */ |
| 470 | ret = lan9303_write_switch_reg(chip, LAN9303_MAC_RX_CFG_OFFS + port, |
| 471 | LAN9303_MAC_RX_CFG_X_REJECT_MAC_TYPES); |
| 472 | if (ret) |
| 473 | return ret; |
| 474 | |
| 475 | /* disable TX, but keep register reset default values else */ |
| 476 | return lan9303_write_switch_reg(chip, LAN9303_MAC_TX_CFG_OFFS + port, |
| 477 | LAN9303_MAC_TX_CFG_X_TX_IFG_CONFIG_DEFAULT | |
| 478 | LAN9303_MAC_TX_CFG_X_TX_PAD_ENABLE); |
| 479 | } |
| 480 | |
| 481 | static int lan9303_enable_packet_processing(struct lan9303 *chip, |
| 482 | unsigned int port) |
| 483 | { |
| 484 | int ret; |
| 485 | |
| 486 | /* enable RX and keep register reset default values else */ |
| 487 | ret = lan9303_write_switch_reg(chip, LAN9303_MAC_RX_CFG_OFFS + port, |
| 488 | LAN9303_MAC_RX_CFG_X_REJECT_MAC_TYPES | |
| 489 | LAN9303_MAC_RX_CFG_X_RX_ENABLE); |
| 490 | if (ret) |
| 491 | return ret; |
| 492 | |
| 493 | /* enable TX and keep register reset default values else */ |
| 494 | return lan9303_write_switch_reg(chip, LAN9303_MAC_TX_CFG_OFFS + port, |
| 495 | LAN9303_MAC_TX_CFG_X_TX_IFG_CONFIG_DEFAULT | |
| 496 | LAN9303_MAC_TX_CFG_X_TX_PAD_ENABLE | |
| 497 | LAN9303_MAC_TX_CFG_X_TX_ENABLE); |
| 498 | } |
| 499 | |
| 500 | /* We want a special working switch: |
| 501 | * - do not forward packets between port 1 and 2 |
| 502 | * - forward everything from port 1 to port 0 |
| 503 | * - forward everything from port 2 to port 0 |
| 504 | * - forward special tagged packets from port 0 to port 1 *or* port 2 |
| 505 | */ |
| 506 | static int lan9303_separate_ports(struct lan9303 *chip) |
| 507 | { |
| 508 | int ret; |
| 509 | |
| 510 | ret = lan9303_write_switch_reg(chip, LAN9303_SWE_PORT_MIRROR, |
| 511 | LAN9303_SWE_PORT_MIRROR_SNIFFER_PORT0 | |
| 512 | LAN9303_SWE_PORT_MIRROR_MIRRORED_PORT1 | |
| 513 | LAN9303_SWE_PORT_MIRROR_MIRRORED_PORT2 | |
| 514 | LAN9303_SWE_PORT_MIRROR_ENABLE_RX_MIRRORING | |
| 515 | LAN9303_SWE_PORT_MIRROR_SNIFF_ALL); |
| 516 | if (ret) |
| 517 | return ret; |
| 518 | |
| 519 | /* enable defining the destination port via special VLAN tagging |
| 520 | * for port 0 |
| 521 | */ |
| 522 | ret = lan9303_write_switch_reg(chip, LAN9303_SWE_INGRESS_PORT_TYPE, |
| 523 | 0x03); |
| 524 | if (ret) |
| 525 | return ret; |
| 526 | |
| 527 | /* tag incoming packets at port 1 and 2 on their way to port 0 to be |
| 528 | * able to discover their source port |
| 529 | */ |
| 530 | ret = lan9303_write_switch_reg(chip, LAN9303_BM_EGRSS_PORT_TYPE, |
| 531 | LAN9303_BM_EGRSS_PORT_TYPE_SPECIAL_TAG_PORT0); |
| 532 | if (ret) |
| 533 | return ret; |
| 534 | |
| 535 | /* prevent port 1 and 2 from forwarding packets by their own */ |
| 536 | return lan9303_write_switch_reg(chip, LAN9303_SWE_PORT_STATE, |
| 537 | LAN9303_SWE_PORT_STATE_FORWARDING_PORT0 | |
| 538 | LAN9303_SWE_PORT_STATE_BLOCKING_PORT1 | |
| 539 | LAN9303_SWE_PORT_STATE_BLOCKING_PORT2); |
| 540 | } |
| 541 | |
| 542 | static int lan9303_handle_reset(struct lan9303 *chip) |
| 543 | { |
| 544 | if (!chip->reset_gpio) |
| 545 | return 0; |
| 546 | |
| 547 | if (chip->reset_duration != 0) |
| 548 | msleep(chip->reset_duration); |
| 549 | |
| 550 | /* release (deassert) reset and activate the device */ |
| 551 | gpiod_set_value_cansleep(chip->reset_gpio, 0); |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | /* stop processing packets for all ports */ |
| 557 | static int lan9303_disable_processing(struct lan9303 *chip) |
| 558 | { |
| 559 | int ret; |
| 560 | |
| 561 | ret = lan9303_disable_packet_processing(chip, LAN9303_PORT_0_OFFSET); |
| 562 | if (ret) |
| 563 | return ret; |
| 564 | ret = lan9303_disable_packet_processing(chip, LAN9303_PORT_1_OFFSET); |
| 565 | if (ret) |
| 566 | return ret; |
| 567 | return lan9303_disable_packet_processing(chip, LAN9303_PORT_2_OFFSET); |
| 568 | } |
| 569 | |
| 570 | static int lan9303_check_device(struct lan9303 *chip) |
| 571 | { |
| 572 | int ret; |
| 573 | u32 reg; |
| 574 | |
| 575 | ret = lan9303_read(chip->regmap, LAN9303_CHIP_REV, ®); |
| 576 | if (ret) { |
| 577 | dev_err(chip->dev, "failed to read chip revision register: %d\n", |
| 578 | ret); |
| 579 | if (!chip->reset_gpio) { |
| 580 | dev_dbg(chip->dev, |
| 581 | "hint: maybe failed due to missing reset GPIO\n"); |
| 582 | } |
| 583 | return ret; |
| 584 | } |
| 585 | |
| 586 | if ((reg >> 16) != LAN9303_CHIP_ID) { |
| 587 | dev_err(chip->dev, "expecting LAN9303 chip, but found: %X\n", |
| 588 | reg >> 16); |
| 589 | return ret; |
| 590 | } |
| 591 | |
| 592 | /* The default state of the LAN9303 device is to forward packets between |
| 593 | * all ports (if not configured differently by an external EEPROM). |
| 594 | * The initial state of a DSA device must be forwarding packets only |
| 595 | * between the external and the internal ports and no forwarding |
| 596 | * between the external ports. In preparation we stop packet handling |
| 597 | * at all for now until the LAN9303 device is re-programmed accordingly. |
| 598 | */ |
| 599 | ret = lan9303_disable_processing(chip); |
| 600 | if (ret) |
| 601 | dev_warn(chip->dev, "failed to disable switching %d\n", ret); |
| 602 | |
| 603 | dev_info(chip->dev, "Found LAN9303 rev. %u\n", reg & 0xffff); |
| 604 | |
| 605 | ret = lan9303_detect_phy_setup(chip); |
| 606 | if (ret) { |
| 607 | dev_err(chip->dev, |
| 608 | "failed to discover phy bootstrap setup: %d\n", ret); |
| 609 | return ret; |
| 610 | } |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | /* ---------------------------- DSA -----------------------------------*/ |
| 616 | |
| 617 | static enum dsa_tag_protocol lan9303_get_tag_protocol(struct dsa_switch *ds) |
| 618 | { |
| 619 | return DSA_TAG_PROTO_LAN9303; |
| 620 | } |
| 621 | |
| 622 | static int lan9303_setup(struct dsa_switch *ds) |
| 623 | { |
| 624 | struct lan9303 *chip = ds->priv; |
| 625 | int ret; |
| 626 | |
| 627 | /* Make sure that port 0 is the cpu port */ |
| 628 | if (!dsa_is_cpu_port(ds, 0)) { |
| 629 | dev_err(chip->dev, "port 0 is not the CPU port\n"); |
| 630 | return -EINVAL; |
| 631 | } |
| 632 | |
| 633 | ret = lan9303_separate_ports(chip); |
| 634 | if (ret) |
| 635 | dev_err(chip->dev, "failed to separate ports %d\n", ret); |
| 636 | |
| 637 | ret = lan9303_enable_packet_processing(chip, LAN9303_PORT_0_OFFSET); |
| 638 | if (ret) |
| 639 | dev_err(chip->dev, "failed to re-enable switching %d\n", ret); |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | struct lan9303_mib_desc { |
| 645 | unsigned int offset; /* offset of first MAC */ |
| 646 | const char *name; |
| 647 | }; |
| 648 | |
| 649 | static const struct lan9303_mib_desc lan9303_mib[] = { |
| 650 | { .offset = LAN9303_MAC_RX_BRDCST_CNT_0, .name = "RxBroad", }, |
| 651 | { .offset = LAN9303_MAC_RX_PAUSE_CNT_0, .name = "RxPause", }, |
| 652 | { .offset = LAN9303_MAC_RX_MULCST_CNT_0, .name = "RxMulti", }, |
| 653 | { .offset = LAN9303_MAC_RX_PKTOK_CNT_0, .name = "RxOk", }, |
| 654 | { .offset = LAN9303_MAC_RX_CRCERR_CNT_0, .name = "RxCrcErr", }, |
| 655 | { .offset = LAN9303_MAC_RX_ALIGN_CNT_0, .name = "RxAlignErr", }, |
| 656 | { .offset = LAN9303_MAC_RX_JABB_CNT_0, .name = "RxJabber", }, |
| 657 | { .offset = LAN9303_MAC_RX_FRAG_CNT_0, .name = "RxFragment", }, |
| 658 | { .offset = LAN9303_MAC_RX_64_CNT_0, .name = "Rx64Byte", }, |
| 659 | { .offset = LAN9303_MAC_RX_127_CNT_0, .name = "Rx128Byte", }, |
| 660 | { .offset = LAN9303_MAC_RX_255_CNT_0, .name = "Rx256Byte", }, |
| 661 | { .offset = LAN9303_MAC_RX_511_CNT_0, .name = "Rx512Byte", }, |
| 662 | { .offset = LAN9303_MAC_RX_1023_CNT_0, .name = "Rx1024Byte", }, |
| 663 | { .offset = LAN9303_MAC_RX_MAX_CNT_0, .name = "RxMaxByte", }, |
| 664 | { .offset = LAN9303_MAC_RX_PKTLEN_CNT_0, .name = "RxByteCnt", }, |
| 665 | { .offset = LAN9303_MAC_RX_SYMBL_CNT_0, .name = "RxSymbolCnt", }, |
| 666 | { .offset = LAN9303_MAC_RX_CTLFRM_CNT_0, .name = "RxCfs", }, |
| 667 | { .offset = LAN9303_MAC_RX_OVRSZE_CNT_0, .name = "RxOverFlow", }, |
| 668 | { .offset = LAN9303_MAC_TX_UNDSZE_CNT_0, .name = "TxShort", }, |
| 669 | { .offset = LAN9303_MAC_TX_BRDCST_CNT_0, .name = "TxBroad", }, |
| 670 | { .offset = LAN9303_MAC_TX_PAUSE_CNT_0, .name = "TxPause", }, |
| 671 | { .offset = LAN9303_MAC_TX_MULCST_CNT_0, .name = "TxMulti", }, |
| 672 | { .offset = LAN9303_MAC_RX_UNDSZE_CNT_0, .name = "TxUnderRun", }, |
| 673 | { .offset = LAN9303_MAC_TX_64_CNT_0, .name = "Tx64Byte", }, |
| 674 | { .offset = LAN9303_MAC_TX_127_CNT_0, .name = "Tx128Byte", }, |
| 675 | { .offset = LAN9303_MAC_TX_255_CNT_0, .name = "Tx256Byte", }, |
| 676 | { .offset = LAN9303_MAC_TX_511_CNT_0, .name = "Tx512Byte", }, |
| 677 | { .offset = LAN9303_MAC_TX_1023_CNT_0, .name = "Tx1024Byte", }, |
| 678 | { .offset = LAN9303_MAC_TX_MAX_CNT_0, .name = "TxMaxByte", }, |
| 679 | { .offset = LAN9303_MAC_TX_PKTLEN_CNT_0, .name = "TxByteCnt", }, |
| 680 | { .offset = LAN9303_MAC_TX_PKTOK_CNT_0, .name = "TxOk", }, |
| 681 | { .offset = LAN9303_MAC_TX_TOTALCOL_CNT_0, .name = "TxCollision", }, |
| 682 | { .offset = LAN9303_MAC_TX_MULTICOL_CNT_0, .name = "TxMultiCol", }, |
| 683 | { .offset = LAN9303_MAC_TX_SNGLECOL_CNT_0, .name = "TxSingleCol", }, |
| 684 | { .offset = LAN9303_MAC_TX_EXCOL_CNT_0, .name = "TxExcCol", }, |
| 685 | { .offset = LAN9303_MAC_TX_DEFER_CNT_0, .name = "TxDefer", }, |
| 686 | { .offset = LAN9303_MAC_TX_LATECOL_0, .name = "TxLateCol", }, |
| 687 | }; |
| 688 | |
| 689 | static void lan9303_get_strings(struct dsa_switch *ds, int port, uint8_t *data) |
| 690 | { |
| 691 | unsigned int u; |
| 692 | |
| 693 | for (u = 0; u < ARRAY_SIZE(lan9303_mib); u++) { |
| 694 | strncpy(data + u * ETH_GSTRING_LEN, lan9303_mib[u].name, |
| 695 | ETH_GSTRING_LEN); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | static void lan9303_get_ethtool_stats(struct dsa_switch *ds, int port, |
| 700 | uint64_t *data) |
| 701 | { |
| 702 | struct lan9303 *chip = ds->priv; |
| 703 | u32 reg; |
| 704 | unsigned int u, poff; |
| 705 | int ret; |
| 706 | |
| 707 | poff = port * 0x400; |
| 708 | |
| 709 | for (u = 0; u < ARRAY_SIZE(lan9303_mib); u++) { |
| 710 | ret = lan9303_read_switch_reg(chip, |
| 711 | lan9303_mib[u].offset + poff, |
| 712 | ®); |
| 713 | if (ret) |
| 714 | dev_warn(chip->dev, "Reading status reg %u failed\n", |
| 715 | lan9303_mib[u].offset + poff); |
| 716 | data[u] = reg; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | static int lan9303_get_sset_count(struct dsa_switch *ds) |
| 721 | { |
| 722 | return ARRAY_SIZE(lan9303_mib); |
| 723 | } |
| 724 | |
| 725 | static int lan9303_phy_read(struct dsa_switch *ds, int phy, int regnum) |
| 726 | { |
| 727 | struct lan9303 *chip = ds->priv; |
| 728 | int phy_base = chip->phy_addr_sel_strap; |
| 729 | |
| 730 | if (phy == phy_base) |
| 731 | return lan9303_virt_phy_reg_read(chip, regnum); |
| 732 | if (phy > phy_base + 2) |
| 733 | return -ENODEV; |
| 734 | |
Egil Hjelmeland | 2c34089 | 2017-07-30 19:58:56 +0200 | [diff] [blame^] | 735 | return chip->ops->phy_read(chip, phy, regnum); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | static int lan9303_phy_write(struct dsa_switch *ds, int phy, int regnum, |
| 739 | u16 val) |
| 740 | { |
| 741 | struct lan9303 *chip = ds->priv; |
| 742 | int phy_base = chip->phy_addr_sel_strap; |
| 743 | |
| 744 | if (phy == phy_base) |
| 745 | return lan9303_virt_phy_reg_write(chip, regnum, val); |
| 746 | if (phy > phy_base + 2) |
| 747 | return -ENODEV; |
| 748 | |
Egil Hjelmeland | 2c34089 | 2017-07-30 19:58:56 +0200 | [diff] [blame^] | 749 | return chip->ops->phy_write(chip, phy, regnum, val); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | static int lan9303_port_enable(struct dsa_switch *ds, int port, |
| 753 | struct phy_device *phy) |
| 754 | { |
| 755 | struct lan9303 *chip = ds->priv; |
| 756 | |
| 757 | /* enable internal packet processing */ |
| 758 | switch (port) { |
| 759 | case 1: |
| 760 | return lan9303_enable_packet_processing(chip, |
| 761 | LAN9303_PORT_1_OFFSET); |
| 762 | case 2: |
| 763 | return lan9303_enable_packet_processing(chip, |
| 764 | LAN9303_PORT_2_OFFSET); |
| 765 | default: |
| 766 | dev_dbg(chip->dev, |
| 767 | "Error: request to power up invalid port %d\n", port); |
| 768 | } |
| 769 | |
| 770 | return -ENODEV; |
| 771 | } |
| 772 | |
| 773 | static void lan9303_port_disable(struct dsa_switch *ds, int port, |
| 774 | struct phy_device *phy) |
| 775 | { |
| 776 | struct lan9303 *chip = ds->priv; |
| 777 | |
| 778 | /* disable internal packet processing */ |
| 779 | switch (port) { |
| 780 | case 1: |
| 781 | lan9303_disable_packet_processing(chip, LAN9303_PORT_1_OFFSET); |
Egil Hjelmeland | 2c34089 | 2017-07-30 19:58:56 +0200 | [diff] [blame^] | 782 | lan9303_phy_write(ds, chip->phy_addr_sel_strap + 1, |
| 783 | MII_BMCR, BMCR_PDOWN); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 784 | break; |
| 785 | case 2: |
| 786 | lan9303_disable_packet_processing(chip, LAN9303_PORT_2_OFFSET); |
Egil Hjelmeland | 2c34089 | 2017-07-30 19:58:56 +0200 | [diff] [blame^] | 787 | lan9303_phy_write(ds, chip->phy_addr_sel_strap + 2, |
| 788 | MII_BMCR, BMCR_PDOWN); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 789 | break; |
| 790 | default: |
| 791 | dev_dbg(chip->dev, |
| 792 | "Error: request to power down invalid port %d\n", port); |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | static struct dsa_switch_ops lan9303_switch_ops = { |
| 797 | .get_tag_protocol = lan9303_get_tag_protocol, |
| 798 | .setup = lan9303_setup, |
| 799 | .get_strings = lan9303_get_strings, |
| 800 | .phy_read = lan9303_phy_read, |
| 801 | .phy_write = lan9303_phy_write, |
| 802 | .get_ethtool_stats = lan9303_get_ethtool_stats, |
| 803 | .get_sset_count = lan9303_get_sset_count, |
| 804 | .port_enable = lan9303_port_enable, |
| 805 | .port_disable = lan9303_port_disable, |
| 806 | }; |
| 807 | |
| 808 | static int lan9303_register_switch(struct lan9303 *chip) |
| 809 | { |
| 810 | chip->ds = dsa_switch_alloc(chip->dev, DSA_MAX_PORTS); |
| 811 | if (!chip->ds) |
| 812 | return -ENOMEM; |
| 813 | |
| 814 | chip->ds->priv = chip; |
| 815 | chip->ds->ops = &lan9303_switch_ops; |
| 816 | chip->ds->phys_mii_mask = chip->phy_addr_sel_strap ? 0xe : 0x7; |
| 817 | |
Vivien Didelot | 23c9ee4 | 2017-05-26 18:12:51 -0400 | [diff] [blame] | 818 | return dsa_register_switch(chip->ds); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | static void lan9303_probe_reset_gpio(struct lan9303 *chip, |
| 822 | struct device_node *np) |
| 823 | { |
| 824 | chip->reset_gpio = devm_gpiod_get_optional(chip->dev, "reset", |
| 825 | GPIOD_OUT_LOW); |
| 826 | |
| 827 | if (!chip->reset_gpio) { |
| 828 | dev_dbg(chip->dev, "No reset GPIO defined\n"); |
| 829 | return; |
| 830 | } |
| 831 | |
| 832 | chip->reset_duration = 200; |
| 833 | |
| 834 | if (np) { |
| 835 | of_property_read_u32(np, "reset-duration", |
| 836 | &chip->reset_duration); |
| 837 | } else { |
| 838 | dev_dbg(chip->dev, "reset duration defaults to 200 ms\n"); |
| 839 | } |
| 840 | |
| 841 | /* A sane reset duration should not be longer than 1s */ |
| 842 | if (chip->reset_duration > 1000) |
| 843 | chip->reset_duration = 1000; |
| 844 | } |
| 845 | |
| 846 | int lan9303_probe(struct lan9303 *chip, struct device_node *np) |
| 847 | { |
| 848 | int ret; |
| 849 | |
| 850 | mutex_init(&chip->indirect_mutex); |
| 851 | |
| 852 | lan9303_probe_reset_gpio(chip, np); |
| 853 | |
| 854 | ret = lan9303_handle_reset(chip); |
| 855 | if (ret) |
| 856 | return ret; |
| 857 | |
| 858 | ret = lan9303_check_device(chip); |
| 859 | if (ret) |
| 860 | return ret; |
| 861 | |
| 862 | ret = lan9303_register_switch(chip); |
| 863 | if (ret) { |
| 864 | dev_dbg(chip->dev, "Failed to register switch: %d\n", ret); |
| 865 | return ret; |
| 866 | } |
| 867 | |
| 868 | return 0; |
| 869 | } |
| 870 | EXPORT_SYMBOL(lan9303_probe); |
| 871 | |
| 872 | int lan9303_remove(struct lan9303 *chip) |
| 873 | { |
| 874 | int rc; |
| 875 | |
| 876 | rc = lan9303_disable_processing(chip); |
| 877 | if (rc != 0) |
| 878 | dev_warn(chip->dev, "shutting down failed\n"); |
| 879 | |
| 880 | dsa_unregister_switch(chip->ds); |
| 881 | |
| 882 | /* assert reset to the whole device to prevent it from doing anything */ |
| 883 | gpiod_set_value_cansleep(chip->reset_gpio, 1); |
| 884 | gpiod_unexport(chip->reset_gpio); |
| 885 | |
| 886 | return 0; |
| 887 | } |
| 888 | EXPORT_SYMBOL(lan9303_remove); |
| 889 | |
| 890 | MODULE_AUTHOR("Juergen Borleis <kernel@pengutronix.de>"); |
| 891 | MODULE_DESCRIPTION("Core driver for SMSC/Microchip LAN9303 three port ethernet switch"); |
| 892 | MODULE_LICENSE("GPL v2"); |