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> |
Egil Hjelmeland | 4d6a78b | 2017-09-19 10:09:24 +0200 | [diff] [blame] | 20 | #include <linux/phy.h> |
Egil Hjelmeland | d99a86a | 2017-10-10 14:49:53 +0200 | [diff] [blame] | 21 | #include <linux/if_bridge.h> |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 22 | |
| 23 | #include "lan9303.h" |
| 24 | |
Egil Hjelmeland | a368ca5 | 2017-08-05 13:05:47 +0200 | [diff] [blame] | 25 | #define LAN9303_NUM_PORTS 3 |
| 26 | |
Egil Hjelmeland | ab78acb | 2017-07-30 19:58:54 +0200 | [diff] [blame] | 27 | /* 13.2 System Control and Status Registers |
| 28 | * Multiply register number by 4 to get address offset. |
| 29 | */ |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 30 | #define LAN9303_CHIP_REV 0x14 |
| 31 | # define LAN9303_CHIP_ID 0x9303 |
| 32 | #define LAN9303_IRQ_CFG 0x15 |
| 33 | # define LAN9303_IRQ_CFG_IRQ_ENABLE BIT(8) |
| 34 | # define LAN9303_IRQ_CFG_IRQ_POL BIT(4) |
| 35 | # define LAN9303_IRQ_CFG_IRQ_TYPE BIT(0) |
| 36 | #define LAN9303_INT_STS 0x16 |
| 37 | # define LAN9303_INT_STS_PHY_INT2 BIT(27) |
| 38 | # define LAN9303_INT_STS_PHY_INT1 BIT(26) |
| 39 | #define LAN9303_INT_EN 0x17 |
| 40 | # define LAN9303_INT_EN_PHY_INT2_EN BIT(27) |
| 41 | # define LAN9303_INT_EN_PHY_INT1_EN BIT(26) |
| 42 | #define LAN9303_HW_CFG 0x1D |
| 43 | # define LAN9303_HW_CFG_READY BIT(27) |
| 44 | # define LAN9303_HW_CFG_AMDX_EN_PORT2 BIT(26) |
| 45 | # define LAN9303_HW_CFG_AMDX_EN_PORT1 BIT(25) |
| 46 | #define LAN9303_PMI_DATA 0x29 |
| 47 | #define LAN9303_PMI_ACCESS 0x2A |
| 48 | # define LAN9303_PMI_ACCESS_PHY_ADDR(x) (((x) & 0x1f) << 11) |
| 49 | # define LAN9303_PMI_ACCESS_MIIRINDA(x) (((x) & 0x1f) << 6) |
| 50 | # define LAN9303_PMI_ACCESS_MII_BUSY BIT(0) |
| 51 | # define LAN9303_PMI_ACCESS_MII_WRITE BIT(1) |
| 52 | #define LAN9303_MANUAL_FC_1 0x68 |
| 53 | #define LAN9303_MANUAL_FC_2 0x69 |
| 54 | #define LAN9303_MANUAL_FC_0 0x6a |
| 55 | #define LAN9303_SWITCH_CSR_DATA 0x6b |
| 56 | #define LAN9303_SWITCH_CSR_CMD 0x6c |
| 57 | #define LAN9303_SWITCH_CSR_CMD_BUSY BIT(31) |
| 58 | #define LAN9303_SWITCH_CSR_CMD_RW BIT(30) |
| 59 | #define LAN9303_SWITCH_CSR_CMD_LANES (BIT(19) | BIT(18) | BIT(17) | BIT(16)) |
| 60 | #define LAN9303_VIRT_PHY_BASE 0x70 |
| 61 | #define LAN9303_VIRT_SPECIAL_CTRL 0x77 |
Egil Hjelmeland | 4d6a78b | 2017-09-19 10:09:24 +0200 | [diff] [blame] | 62 | #define LAN9303_VIRT_SPECIAL_TURBO BIT(10) /*Turbo MII Enable*/ |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 63 | |
Egil Hjelmeland | ab78acb | 2017-07-30 19:58:54 +0200 | [diff] [blame] | 64 | /*13.4 Switch Fabric Control and Status Registers |
| 65 | * Accessed indirectly via SWITCH_CSR_CMD, SWITCH_CSR_DATA. |
| 66 | */ |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 67 | #define LAN9303_SW_DEV_ID 0x0000 |
| 68 | #define LAN9303_SW_RESET 0x0001 |
| 69 | #define LAN9303_SW_RESET_RESET BIT(0) |
| 70 | #define LAN9303_SW_IMR 0x0004 |
| 71 | #define LAN9303_SW_IPR 0x0005 |
| 72 | #define LAN9303_MAC_VER_ID_0 0x0400 |
| 73 | #define LAN9303_MAC_RX_CFG_0 0x0401 |
| 74 | # define LAN9303_MAC_RX_CFG_X_REJECT_MAC_TYPES BIT(1) |
| 75 | # define LAN9303_MAC_RX_CFG_X_RX_ENABLE BIT(0) |
| 76 | #define LAN9303_MAC_RX_UNDSZE_CNT_0 0x0410 |
| 77 | #define LAN9303_MAC_RX_64_CNT_0 0x0411 |
| 78 | #define LAN9303_MAC_RX_127_CNT_0 0x0412 |
| 79 | #define LAN9303_MAC_RX_255_CNT_0 0x413 |
| 80 | #define LAN9303_MAC_RX_511_CNT_0 0x0414 |
| 81 | #define LAN9303_MAC_RX_1023_CNT_0 0x0415 |
| 82 | #define LAN9303_MAC_RX_MAX_CNT_0 0x0416 |
| 83 | #define LAN9303_MAC_RX_OVRSZE_CNT_0 0x0417 |
| 84 | #define LAN9303_MAC_RX_PKTOK_CNT_0 0x0418 |
| 85 | #define LAN9303_MAC_RX_CRCERR_CNT_0 0x0419 |
| 86 | #define LAN9303_MAC_RX_MULCST_CNT_0 0x041a |
| 87 | #define LAN9303_MAC_RX_BRDCST_CNT_0 0x041b |
| 88 | #define LAN9303_MAC_RX_PAUSE_CNT_0 0x041c |
| 89 | #define LAN9303_MAC_RX_FRAG_CNT_0 0x041d |
| 90 | #define LAN9303_MAC_RX_JABB_CNT_0 0x041e |
| 91 | #define LAN9303_MAC_RX_ALIGN_CNT_0 0x041f |
| 92 | #define LAN9303_MAC_RX_PKTLEN_CNT_0 0x0420 |
| 93 | #define LAN9303_MAC_RX_GOODPKTLEN_CNT_0 0x0421 |
| 94 | #define LAN9303_MAC_RX_SYMBL_CNT_0 0x0422 |
| 95 | #define LAN9303_MAC_RX_CTLFRM_CNT_0 0x0423 |
| 96 | |
| 97 | #define LAN9303_MAC_TX_CFG_0 0x0440 |
| 98 | # define LAN9303_MAC_TX_CFG_X_TX_IFG_CONFIG_DEFAULT (21 << 2) |
| 99 | # define LAN9303_MAC_TX_CFG_X_TX_PAD_ENABLE BIT(1) |
| 100 | # define LAN9303_MAC_TX_CFG_X_TX_ENABLE BIT(0) |
| 101 | #define LAN9303_MAC_TX_DEFER_CNT_0 0x0451 |
| 102 | #define LAN9303_MAC_TX_PAUSE_CNT_0 0x0452 |
| 103 | #define LAN9303_MAC_TX_PKTOK_CNT_0 0x0453 |
| 104 | #define LAN9303_MAC_TX_64_CNT_0 0x0454 |
| 105 | #define LAN9303_MAC_TX_127_CNT_0 0x0455 |
| 106 | #define LAN9303_MAC_TX_255_CNT_0 0x0456 |
| 107 | #define LAN9303_MAC_TX_511_CNT_0 0x0457 |
| 108 | #define LAN9303_MAC_TX_1023_CNT_0 0x0458 |
| 109 | #define LAN9303_MAC_TX_MAX_CNT_0 0x0459 |
| 110 | #define LAN9303_MAC_TX_UNDSZE_CNT_0 0x045a |
| 111 | #define LAN9303_MAC_TX_PKTLEN_CNT_0 0x045c |
| 112 | #define LAN9303_MAC_TX_BRDCST_CNT_0 0x045d |
| 113 | #define LAN9303_MAC_TX_MULCST_CNT_0 0x045e |
| 114 | #define LAN9303_MAC_TX_LATECOL_0 0x045f |
| 115 | #define LAN9303_MAC_TX_EXCOL_CNT_0 0x0460 |
| 116 | #define LAN9303_MAC_TX_SNGLECOL_CNT_0 0x0461 |
| 117 | #define LAN9303_MAC_TX_MULTICOL_CNT_0 0x0462 |
| 118 | #define LAN9303_MAC_TX_TOTALCOL_CNT_0 0x0463 |
| 119 | |
| 120 | #define LAN9303_MAC_VER_ID_1 0x0800 |
| 121 | #define LAN9303_MAC_RX_CFG_1 0x0801 |
| 122 | #define LAN9303_MAC_TX_CFG_1 0x0840 |
| 123 | #define LAN9303_MAC_VER_ID_2 0x0c00 |
| 124 | #define LAN9303_MAC_RX_CFG_2 0x0c01 |
| 125 | #define LAN9303_MAC_TX_CFG_2 0x0c40 |
| 126 | #define LAN9303_SWE_ALR_CMD 0x1800 |
Egil Hjelmeland | ab33534 | 2017-10-20 12:19:09 +0200 | [diff] [blame^] | 127 | # define LAN9303_ALR_CMD_MAKE_ENTRY BIT(2) |
| 128 | # define LAN9303_ALR_CMD_GET_FIRST BIT(1) |
| 129 | # define LAN9303_ALR_CMD_GET_NEXT BIT(0) |
| 130 | #define LAN9303_SWE_ALR_WR_DAT_0 0x1801 |
| 131 | #define LAN9303_SWE_ALR_WR_DAT_1 0x1802 |
| 132 | # define LAN9303_ALR_DAT1_VALID BIT(26) |
| 133 | # define LAN9303_ALR_DAT1_END_OF_TABL BIT(25) |
| 134 | # define LAN9303_ALR_DAT1_AGE_OVERRID BIT(25) |
| 135 | # define LAN9303_ALR_DAT1_STATIC BIT(24) |
| 136 | # define LAN9303_ALR_DAT1_PORT_BITOFFS 16 |
| 137 | # define LAN9303_ALR_DAT1_PORT_MASK (7 << LAN9303_ALR_DAT1_PORT_BITOFFS) |
| 138 | #define LAN9303_SWE_ALR_RD_DAT_0 0x1805 |
| 139 | #define LAN9303_SWE_ALR_RD_DAT_1 0x1806 |
| 140 | #define LAN9303_SWE_ALR_CMD_STS 0x1808 |
| 141 | # define ALR_STS_MAKE_PEND BIT(0) |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 142 | #define LAN9303_SWE_VLAN_CMD 0x180b |
| 143 | # define LAN9303_SWE_VLAN_CMD_RNW BIT(5) |
| 144 | # define LAN9303_SWE_VLAN_CMD_PVIDNVLAN BIT(4) |
| 145 | #define LAN9303_SWE_VLAN_WR_DATA 0x180c |
| 146 | #define LAN9303_SWE_VLAN_RD_DATA 0x180e |
| 147 | # define LAN9303_SWE_VLAN_MEMBER_PORT2 BIT(17) |
| 148 | # define LAN9303_SWE_VLAN_UNTAG_PORT2 BIT(16) |
| 149 | # define LAN9303_SWE_VLAN_MEMBER_PORT1 BIT(15) |
| 150 | # define LAN9303_SWE_VLAN_UNTAG_PORT1 BIT(14) |
| 151 | # define LAN9303_SWE_VLAN_MEMBER_PORT0 BIT(13) |
| 152 | # define LAN9303_SWE_VLAN_UNTAG_PORT0 BIT(12) |
| 153 | #define LAN9303_SWE_VLAN_CMD_STS 0x1810 |
| 154 | #define LAN9303_SWE_GLB_INGRESS_CFG 0x1840 |
| 155 | #define LAN9303_SWE_PORT_STATE 0x1843 |
| 156 | # define LAN9303_SWE_PORT_STATE_FORWARDING_PORT2 (0) |
| 157 | # define LAN9303_SWE_PORT_STATE_LEARNING_PORT2 BIT(5) |
| 158 | # define LAN9303_SWE_PORT_STATE_BLOCKING_PORT2 BIT(4) |
| 159 | # define LAN9303_SWE_PORT_STATE_FORWARDING_PORT1 (0) |
| 160 | # define LAN9303_SWE_PORT_STATE_LEARNING_PORT1 BIT(3) |
| 161 | # define LAN9303_SWE_PORT_STATE_BLOCKING_PORT1 BIT(2) |
| 162 | # define LAN9303_SWE_PORT_STATE_FORWARDING_PORT0 (0) |
| 163 | # define LAN9303_SWE_PORT_STATE_LEARNING_PORT0 BIT(1) |
| 164 | # define LAN9303_SWE_PORT_STATE_BLOCKING_PORT0 BIT(0) |
Egil Hjelmeland | d99a86a | 2017-10-10 14:49:53 +0200 | [diff] [blame] | 165 | # define LAN9303_SWE_PORT_STATE_DISABLED_PORT0 (3) |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 166 | #define LAN9303_SWE_PORT_MIRROR 0x1846 |
| 167 | # define LAN9303_SWE_PORT_MIRROR_SNIFF_ALL BIT(8) |
| 168 | # define LAN9303_SWE_PORT_MIRROR_SNIFFER_PORT2 BIT(7) |
| 169 | # define LAN9303_SWE_PORT_MIRROR_SNIFFER_PORT1 BIT(6) |
| 170 | # define LAN9303_SWE_PORT_MIRROR_SNIFFER_PORT0 BIT(5) |
| 171 | # define LAN9303_SWE_PORT_MIRROR_MIRRORED_PORT2 BIT(4) |
| 172 | # define LAN9303_SWE_PORT_MIRROR_MIRRORED_PORT1 BIT(3) |
| 173 | # define LAN9303_SWE_PORT_MIRROR_MIRRORED_PORT0 BIT(2) |
| 174 | # define LAN9303_SWE_PORT_MIRROR_ENABLE_RX_MIRRORING BIT(1) |
| 175 | # define LAN9303_SWE_PORT_MIRROR_ENABLE_TX_MIRRORING BIT(0) |
Egil Hjelmeland | d99a86a | 2017-10-10 14:49:53 +0200 | [diff] [blame] | 176 | # define LAN9303_SWE_PORT_MIRROR_DISABLED 0 |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 177 | #define LAN9303_SWE_INGRESS_PORT_TYPE 0x1847 |
Egil Hjelmeland | f7e3bfa | 2017-10-10 14:49:52 +0200 | [diff] [blame] | 178 | #define LAN9303_SWE_INGRESS_PORT_TYPE_VLAN 3 |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 179 | #define LAN9303_BM_CFG 0x1c00 |
| 180 | #define LAN9303_BM_EGRSS_PORT_TYPE 0x1c0c |
| 181 | # define LAN9303_BM_EGRSS_PORT_TYPE_SPECIAL_TAG_PORT2 (BIT(17) | BIT(16)) |
| 182 | # define LAN9303_BM_EGRSS_PORT_TYPE_SPECIAL_TAG_PORT1 (BIT(9) | BIT(8)) |
| 183 | # define LAN9303_BM_EGRSS_PORT_TYPE_SPECIAL_TAG_PORT0 (BIT(1) | BIT(0)) |
| 184 | |
Egil Hjelmeland | 451d3ca | 2017-08-05 13:05:46 +0200 | [diff] [blame] | 185 | #define LAN9303_SWITCH_PORT_REG(port, reg0) (0x400 * (port) + (reg0)) |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 186 | |
| 187 | /* the built-in PHYs are of type LAN911X */ |
| 188 | #define MII_LAN911X_SPECIAL_MODES 0x12 |
| 189 | #define MII_LAN911X_SPECIAL_CONTROL_STATUS 0x1f |
| 190 | |
| 191 | static const struct regmap_range lan9303_valid_regs[] = { |
| 192 | regmap_reg_range(0x14, 0x17), /* misc, interrupt */ |
| 193 | regmap_reg_range(0x19, 0x19), /* endian test */ |
| 194 | regmap_reg_range(0x1d, 0x1d), /* hardware config */ |
| 195 | regmap_reg_range(0x23, 0x24), /* general purpose timer */ |
| 196 | regmap_reg_range(0x27, 0x27), /* counter */ |
| 197 | regmap_reg_range(0x29, 0x2a), /* PMI index regs */ |
| 198 | regmap_reg_range(0x68, 0x6a), /* flow control */ |
| 199 | regmap_reg_range(0x6b, 0x6c), /* switch fabric indirect regs */ |
| 200 | regmap_reg_range(0x6d, 0x6f), /* misc */ |
| 201 | regmap_reg_range(0x70, 0x77), /* virtual phy */ |
| 202 | regmap_reg_range(0x78, 0x7a), /* GPIO */ |
| 203 | regmap_reg_range(0x7c, 0x7e), /* MAC & reset */ |
| 204 | regmap_reg_range(0x80, 0xb7), /* switch fabric direct regs (wr only) */ |
| 205 | }; |
| 206 | |
| 207 | static const struct regmap_range lan9303_reserved_ranges[] = { |
| 208 | regmap_reg_range(0x00, 0x13), |
| 209 | regmap_reg_range(0x18, 0x18), |
| 210 | regmap_reg_range(0x1a, 0x1c), |
| 211 | regmap_reg_range(0x1e, 0x22), |
| 212 | regmap_reg_range(0x25, 0x26), |
| 213 | regmap_reg_range(0x28, 0x28), |
| 214 | regmap_reg_range(0x2b, 0x67), |
| 215 | regmap_reg_range(0x7b, 0x7b), |
| 216 | regmap_reg_range(0x7f, 0x7f), |
| 217 | regmap_reg_range(0xb8, 0xff), |
| 218 | }; |
| 219 | |
| 220 | const struct regmap_access_table lan9303_register_set = { |
| 221 | .yes_ranges = lan9303_valid_regs, |
| 222 | .n_yes_ranges = ARRAY_SIZE(lan9303_valid_regs), |
| 223 | .no_ranges = lan9303_reserved_ranges, |
| 224 | .n_no_ranges = ARRAY_SIZE(lan9303_reserved_ranges), |
| 225 | }; |
| 226 | EXPORT_SYMBOL(lan9303_register_set); |
| 227 | |
| 228 | static int lan9303_read(struct regmap *regmap, unsigned int offset, u32 *reg) |
| 229 | { |
| 230 | int ret, i; |
| 231 | |
| 232 | /* we can lose arbitration for the I2C case, because the device |
| 233 | * tries to detect and read an external EEPROM after reset and acts as |
| 234 | * a master on the shared I2C bus itself. This conflicts with our |
| 235 | * attempts to access the device as a slave at the same moment. |
| 236 | */ |
| 237 | for (i = 0; i < 5; i++) { |
| 238 | ret = regmap_read(regmap, offset, reg); |
| 239 | if (!ret) |
| 240 | return 0; |
| 241 | if (ret != -EAGAIN) |
| 242 | break; |
| 243 | msleep(500); |
| 244 | } |
| 245 | |
| 246 | return -EIO; |
| 247 | } |
| 248 | |
| 249 | static int lan9303_virt_phy_reg_read(struct lan9303 *chip, int regnum) |
| 250 | { |
| 251 | int ret; |
| 252 | u32 val; |
| 253 | |
| 254 | if (regnum > MII_EXPANSION) |
| 255 | return -EINVAL; |
| 256 | |
| 257 | ret = lan9303_read(chip->regmap, LAN9303_VIRT_PHY_BASE + regnum, &val); |
| 258 | if (ret) |
| 259 | return ret; |
| 260 | |
| 261 | return val & 0xffff; |
| 262 | } |
| 263 | |
| 264 | static int lan9303_virt_phy_reg_write(struct lan9303 *chip, int regnum, u16 val) |
| 265 | { |
| 266 | if (regnum > MII_EXPANSION) |
| 267 | return -EINVAL; |
| 268 | |
| 269 | return regmap_write(chip->regmap, LAN9303_VIRT_PHY_BASE + regnum, val); |
| 270 | } |
| 271 | |
Egil Hjelmeland | 9e866e5 | 2017-07-30 19:58:55 +0200 | [diff] [blame] | 272 | static int lan9303_indirect_phy_wait_for_completion(struct lan9303 *chip) |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 273 | { |
| 274 | int ret, i; |
| 275 | u32 reg; |
| 276 | |
| 277 | for (i = 0; i < 25; i++) { |
| 278 | ret = lan9303_read(chip->regmap, LAN9303_PMI_ACCESS, ®); |
| 279 | if (ret) { |
| 280 | dev_err(chip->dev, |
| 281 | "Failed to read pmi access status: %d\n", ret); |
| 282 | return ret; |
| 283 | } |
| 284 | if (!(reg & LAN9303_PMI_ACCESS_MII_BUSY)) |
| 285 | return 0; |
| 286 | msleep(1); |
| 287 | } |
| 288 | |
| 289 | return -EIO; |
| 290 | } |
| 291 | |
Egil Hjelmeland | 9e866e5 | 2017-07-30 19:58:55 +0200 | [diff] [blame] | 292 | 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] | 293 | { |
| 294 | int ret; |
| 295 | u32 val; |
| 296 | |
| 297 | val = LAN9303_PMI_ACCESS_PHY_ADDR(addr); |
| 298 | val |= LAN9303_PMI_ACCESS_MIIRINDA(regnum); |
| 299 | |
| 300 | mutex_lock(&chip->indirect_mutex); |
| 301 | |
Egil Hjelmeland | 9e866e5 | 2017-07-30 19:58:55 +0200 | [diff] [blame] | 302 | ret = lan9303_indirect_phy_wait_for_completion(chip); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 303 | if (ret) |
| 304 | goto on_error; |
| 305 | |
| 306 | /* start the MII read cycle */ |
| 307 | ret = regmap_write(chip->regmap, LAN9303_PMI_ACCESS, val); |
| 308 | if (ret) |
| 309 | goto on_error; |
| 310 | |
Egil Hjelmeland | 9e866e5 | 2017-07-30 19:58:55 +0200 | [diff] [blame] | 311 | ret = lan9303_indirect_phy_wait_for_completion(chip); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 312 | if (ret) |
| 313 | goto on_error; |
| 314 | |
| 315 | /* read the result of this operation */ |
| 316 | ret = lan9303_read(chip->regmap, LAN9303_PMI_DATA, &val); |
| 317 | if (ret) |
| 318 | goto on_error; |
| 319 | |
| 320 | mutex_unlock(&chip->indirect_mutex); |
| 321 | |
| 322 | return val & 0xffff; |
| 323 | |
| 324 | on_error: |
| 325 | mutex_unlock(&chip->indirect_mutex); |
| 326 | return ret; |
| 327 | } |
| 328 | |
Egil Hjelmeland | 9e866e5 | 2017-07-30 19:58:55 +0200 | [diff] [blame] | 329 | static int lan9303_indirect_phy_write(struct lan9303 *chip, int addr, |
| 330 | int regnum, u16 val) |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 331 | { |
| 332 | int ret; |
| 333 | u32 reg; |
| 334 | |
| 335 | reg = LAN9303_PMI_ACCESS_PHY_ADDR(addr); |
| 336 | reg |= LAN9303_PMI_ACCESS_MIIRINDA(regnum); |
| 337 | reg |= LAN9303_PMI_ACCESS_MII_WRITE; |
| 338 | |
| 339 | mutex_lock(&chip->indirect_mutex); |
| 340 | |
Egil Hjelmeland | 9e866e5 | 2017-07-30 19:58:55 +0200 | [diff] [blame] | 341 | ret = lan9303_indirect_phy_wait_for_completion(chip); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 342 | if (ret) |
| 343 | goto on_error; |
| 344 | |
| 345 | /* write the data first... */ |
| 346 | ret = regmap_write(chip->regmap, LAN9303_PMI_DATA, val); |
| 347 | if (ret) |
| 348 | goto on_error; |
| 349 | |
| 350 | /* ...then start the MII write cycle */ |
| 351 | ret = regmap_write(chip->regmap, LAN9303_PMI_ACCESS, reg); |
| 352 | |
| 353 | on_error: |
| 354 | mutex_unlock(&chip->indirect_mutex); |
| 355 | return ret; |
| 356 | } |
| 357 | |
Egil Hjelmeland | 2c34089 | 2017-07-30 19:58:56 +0200 | [diff] [blame] | 358 | const struct lan9303_phy_ops lan9303_indirect_phy_ops = { |
| 359 | .phy_read = lan9303_indirect_phy_read, |
| 360 | .phy_write = lan9303_indirect_phy_write, |
| 361 | }; |
| 362 | EXPORT_SYMBOL_GPL(lan9303_indirect_phy_ops); |
| 363 | |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 364 | static int lan9303_switch_wait_for_completion(struct lan9303 *chip) |
| 365 | { |
| 366 | int ret, i; |
| 367 | u32 reg; |
| 368 | |
| 369 | for (i = 0; i < 25; i++) { |
| 370 | ret = lan9303_read(chip->regmap, LAN9303_SWITCH_CSR_CMD, ®); |
| 371 | if (ret) { |
| 372 | dev_err(chip->dev, |
| 373 | "Failed to read csr command status: %d\n", ret); |
| 374 | return ret; |
| 375 | } |
| 376 | if (!(reg & LAN9303_SWITCH_CSR_CMD_BUSY)) |
| 377 | return 0; |
| 378 | msleep(1); |
| 379 | } |
| 380 | |
| 381 | return -EIO; |
| 382 | } |
| 383 | |
| 384 | static int lan9303_write_switch_reg(struct lan9303 *chip, u16 regnum, u32 val) |
| 385 | { |
| 386 | u32 reg; |
| 387 | int ret; |
| 388 | |
| 389 | reg = regnum; |
| 390 | reg |= LAN9303_SWITCH_CSR_CMD_LANES; |
| 391 | reg |= LAN9303_SWITCH_CSR_CMD_BUSY; |
| 392 | |
| 393 | mutex_lock(&chip->indirect_mutex); |
| 394 | |
| 395 | ret = lan9303_switch_wait_for_completion(chip); |
| 396 | if (ret) |
| 397 | goto on_error; |
| 398 | |
| 399 | ret = regmap_write(chip->regmap, LAN9303_SWITCH_CSR_DATA, val); |
| 400 | if (ret) { |
| 401 | dev_err(chip->dev, "Failed to write csr data reg: %d\n", ret); |
| 402 | goto on_error; |
| 403 | } |
| 404 | |
| 405 | /* trigger write */ |
| 406 | ret = regmap_write(chip->regmap, LAN9303_SWITCH_CSR_CMD, reg); |
| 407 | if (ret) |
| 408 | dev_err(chip->dev, "Failed to write csr command reg: %d\n", |
| 409 | ret); |
| 410 | |
| 411 | on_error: |
| 412 | mutex_unlock(&chip->indirect_mutex); |
| 413 | return ret; |
| 414 | } |
| 415 | |
| 416 | static int lan9303_read_switch_reg(struct lan9303 *chip, u16 regnum, u32 *val) |
| 417 | { |
| 418 | u32 reg; |
| 419 | int ret; |
| 420 | |
| 421 | reg = regnum; |
| 422 | reg |= LAN9303_SWITCH_CSR_CMD_LANES; |
| 423 | reg |= LAN9303_SWITCH_CSR_CMD_RW; |
| 424 | reg |= LAN9303_SWITCH_CSR_CMD_BUSY; |
| 425 | |
| 426 | mutex_lock(&chip->indirect_mutex); |
| 427 | |
| 428 | ret = lan9303_switch_wait_for_completion(chip); |
| 429 | if (ret) |
| 430 | goto on_error; |
| 431 | |
| 432 | /* trigger read */ |
| 433 | ret = regmap_write(chip->regmap, LAN9303_SWITCH_CSR_CMD, reg); |
| 434 | if (ret) { |
| 435 | dev_err(chip->dev, "Failed to write csr command reg: %d\n", |
| 436 | ret); |
| 437 | goto on_error; |
| 438 | } |
| 439 | |
| 440 | ret = lan9303_switch_wait_for_completion(chip); |
| 441 | if (ret) |
| 442 | goto on_error; |
| 443 | |
| 444 | ret = lan9303_read(chip->regmap, LAN9303_SWITCH_CSR_DATA, val); |
| 445 | if (ret) |
| 446 | dev_err(chip->dev, "Failed to read csr data reg: %d\n", ret); |
| 447 | on_error: |
| 448 | mutex_unlock(&chip->indirect_mutex); |
| 449 | return ret; |
| 450 | } |
| 451 | |
Egil Hjelmeland | 451d3ca | 2017-08-05 13:05:46 +0200 | [diff] [blame] | 452 | static int lan9303_write_switch_port(struct lan9303 *chip, int port, |
| 453 | u16 regnum, u32 val) |
| 454 | { |
| 455 | return lan9303_write_switch_reg( |
| 456 | chip, LAN9303_SWITCH_PORT_REG(port, regnum), val); |
| 457 | } |
| 458 | |
Egil Hjelmeland | 0a967b4 | 2017-08-05 13:05:50 +0200 | [diff] [blame] | 459 | static int lan9303_read_switch_port(struct lan9303 *chip, int port, |
| 460 | u16 regnum, u32 *val) |
| 461 | { |
| 462 | return lan9303_read_switch_reg( |
| 463 | chip, LAN9303_SWITCH_PORT_REG(port, regnum), val); |
| 464 | } |
| 465 | |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 466 | static int lan9303_detect_phy_setup(struct lan9303 *chip) |
| 467 | { |
| 468 | int reg; |
| 469 | |
| 470 | /* depending on the 'phy_addr_sel_strap' setting, the three phys are |
| 471 | * using IDs 0-1-2 or IDs 1-2-3. We cannot read back the |
| 472 | * 'phy_addr_sel_strap' setting directly, so we need a test, which |
| 473 | * configuration is active: |
| 474 | * Special reg 18 of phy 3 reads as 0x0000, if 'phy_addr_sel_strap' is 0 |
| 475 | * and the IDs are 0-1-2, else it contains something different from |
| 476 | * 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] | 477 | * 0xffff is returned on MDIO read with no response. |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 478 | */ |
Egil Hjelmeland | 2c34089 | 2017-07-30 19:58:56 +0200 | [diff] [blame] | 479 | reg = chip->ops->phy_read(chip, 3, MII_LAN911X_SPECIAL_MODES); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 480 | if (reg < 0) { |
| 481 | dev_err(chip->dev, "Failed to detect phy config: %d\n", reg); |
| 482 | return reg; |
| 483 | } |
| 484 | |
Egil Hjelmeland | d329ac8 | 2017-07-30 19:58:53 +0200 | [diff] [blame] | 485 | if ((reg != 0) && (reg != 0xffff)) |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 486 | chip->phy_addr_sel_strap = 1; |
| 487 | else |
| 488 | chip->phy_addr_sel_strap = 0; |
| 489 | |
| 490 | dev_dbg(chip->dev, "Phy setup '%s' detected\n", |
| 491 | chip->phy_addr_sel_strap ? "1-2-3" : "0-1-2"); |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
Egil Hjelmeland | ab33534 | 2017-10-20 12:19:09 +0200 | [diff] [blame^] | 496 | /* Map ALR-port bits to port bitmap, and back */ |
| 497 | static const int alrport_2_portmap[] = {1, 2, 4, 0, 3, 5, 6, 7 }; |
| 498 | static const int portmap_2_alrport[] = {3, 0, 1, 4, 2, 5, 6, 7 }; |
| 499 | |
| 500 | /* Wait a while until mask & reg == value. Otherwise return timeout. */ |
| 501 | static int lan9303_csr_reg_wait(struct lan9303 *chip, int regno, |
| 502 | int mask, char value) |
| 503 | { |
| 504 | int i; |
| 505 | |
| 506 | for (i = 0; i < 0x1000; i++) { |
| 507 | u32 reg; |
| 508 | |
| 509 | lan9303_read_switch_reg(chip, regno, ®); |
| 510 | if ((reg & mask) == value) |
| 511 | return 0; |
| 512 | usleep_range(1000, 2000); |
| 513 | } |
| 514 | return -ETIMEDOUT; |
| 515 | } |
| 516 | |
| 517 | static int lan9303_alr_make_entry_raw(struct lan9303 *chip, u32 dat0, u32 dat1) |
| 518 | { |
| 519 | lan9303_write_switch_reg(chip, LAN9303_SWE_ALR_WR_DAT_0, dat0); |
| 520 | lan9303_write_switch_reg(chip, LAN9303_SWE_ALR_WR_DAT_1, dat1); |
| 521 | lan9303_write_switch_reg(chip, LAN9303_SWE_ALR_CMD, |
| 522 | LAN9303_ALR_CMD_MAKE_ENTRY); |
| 523 | lan9303_csr_reg_wait(chip, LAN9303_SWE_ALR_CMD_STS, ALR_STS_MAKE_PEND, |
| 524 | 0); |
| 525 | lan9303_write_switch_reg(chip, LAN9303_SWE_ALR_CMD, 0); |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | typedef void alr_loop_cb_t(struct lan9303 *chip, u32 dat0, u32 dat1, |
| 531 | int portmap, void *ctx); |
| 532 | |
| 533 | static void lan9303_alr_loop(struct lan9303 *chip, alr_loop_cb_t *cb, void *ctx) |
| 534 | { |
| 535 | int i; |
| 536 | |
| 537 | lan9303_write_switch_reg(chip, LAN9303_SWE_ALR_CMD, |
| 538 | LAN9303_ALR_CMD_GET_FIRST); |
| 539 | lan9303_write_switch_reg(chip, LAN9303_SWE_ALR_CMD, 0); |
| 540 | |
| 541 | for (i = 1; i < LAN9303_NUM_ALR_RECORDS; i++) { |
| 542 | u32 dat0, dat1; |
| 543 | int alrport, portmap; |
| 544 | |
| 545 | lan9303_read_switch_reg(chip, LAN9303_SWE_ALR_RD_DAT_0, &dat0); |
| 546 | lan9303_read_switch_reg(chip, LAN9303_SWE_ALR_RD_DAT_1, &dat1); |
| 547 | if (dat1 & LAN9303_ALR_DAT1_END_OF_TABL) |
| 548 | break; |
| 549 | |
| 550 | alrport = (dat1 & LAN9303_ALR_DAT1_PORT_MASK) >> |
| 551 | LAN9303_ALR_DAT1_PORT_BITOFFS; |
| 552 | portmap = alrport_2_portmap[alrport]; |
| 553 | |
| 554 | cb(chip, dat0, dat1, portmap, ctx); |
| 555 | |
| 556 | lan9303_write_switch_reg(chip, LAN9303_SWE_ALR_CMD, |
| 557 | LAN9303_ALR_CMD_GET_NEXT); |
| 558 | lan9303_write_switch_reg(chip, LAN9303_SWE_ALR_CMD, 0); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | static void alr_reg_to_mac(u32 dat0, u32 dat1, u8 mac[6]) |
| 563 | { |
| 564 | mac[0] = (dat0 >> 0) & 0xff; |
| 565 | mac[1] = (dat0 >> 8) & 0xff; |
| 566 | mac[2] = (dat0 >> 16) & 0xff; |
| 567 | mac[3] = (dat0 >> 24) & 0xff; |
| 568 | mac[4] = (dat1 >> 0) & 0xff; |
| 569 | mac[5] = (dat1 >> 8) & 0xff; |
| 570 | } |
| 571 | |
| 572 | struct del_port_learned_ctx { |
| 573 | int port; |
| 574 | }; |
| 575 | |
| 576 | /* Clear learned (non-static) entry on given port */ |
| 577 | static void alr_loop_cb_del_port_learned(struct lan9303 *chip, u32 dat0, |
| 578 | u32 dat1, int portmap, void *ctx) |
| 579 | { |
| 580 | struct del_port_learned_ctx *del_ctx = ctx; |
| 581 | int port = del_ctx->port; |
| 582 | |
| 583 | if (((BIT(port) & portmap) == 0) || (dat1 & LAN9303_ALR_DAT1_STATIC)) |
| 584 | return; |
| 585 | |
| 586 | /* learned entries has only one port, we can just delete */ |
| 587 | dat1 &= ~LAN9303_ALR_DAT1_VALID; /* delete entry */ |
| 588 | lan9303_alr_make_entry_raw(chip, dat0, dat1); |
| 589 | } |
| 590 | |
| 591 | struct port_fdb_dump_ctx { |
| 592 | int port; |
| 593 | void *data; |
| 594 | dsa_fdb_dump_cb_t *cb; |
| 595 | }; |
| 596 | |
| 597 | static void alr_loop_cb_fdb_port_dump(struct lan9303 *chip, u32 dat0, |
| 598 | u32 dat1, int portmap, void *ctx) |
| 599 | { |
| 600 | struct port_fdb_dump_ctx *dump_ctx = ctx; |
| 601 | u8 mac[ETH_ALEN]; |
| 602 | bool is_static; |
| 603 | |
| 604 | if ((BIT(dump_ctx->port) & portmap) == 0) |
| 605 | return; |
| 606 | |
| 607 | alr_reg_to_mac(dat0, dat1, mac); |
| 608 | is_static = !!(dat1 & LAN9303_ALR_DAT1_STATIC); |
| 609 | dump_ctx->cb(mac, 0, is_static, dump_ctx->data); |
| 610 | } |
| 611 | |
Egil Hjelmeland | 9c84258 | 2017-08-05 13:05:49 +0200 | [diff] [blame] | 612 | static int lan9303_disable_processing_port(struct lan9303 *chip, |
| 613 | unsigned int port) |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 614 | { |
| 615 | int ret; |
| 616 | |
| 617 | /* disable RX, but keep register reset default values else */ |
Egil Hjelmeland | 451d3ca | 2017-08-05 13:05:46 +0200 | [diff] [blame] | 618 | ret = lan9303_write_switch_port(chip, port, LAN9303_MAC_RX_CFG_0, |
| 619 | LAN9303_MAC_RX_CFG_X_REJECT_MAC_TYPES); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 620 | if (ret) |
| 621 | return ret; |
| 622 | |
| 623 | /* disable TX, but keep register reset default values else */ |
Egil Hjelmeland | 451d3ca | 2017-08-05 13:05:46 +0200 | [diff] [blame] | 624 | return lan9303_write_switch_port(chip, port, LAN9303_MAC_TX_CFG_0, |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 625 | LAN9303_MAC_TX_CFG_X_TX_IFG_CONFIG_DEFAULT | |
| 626 | LAN9303_MAC_TX_CFG_X_TX_PAD_ENABLE); |
| 627 | } |
| 628 | |
Egil Hjelmeland | 9c84258 | 2017-08-05 13:05:49 +0200 | [diff] [blame] | 629 | static int lan9303_enable_processing_port(struct lan9303 *chip, |
| 630 | unsigned int port) |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 631 | { |
| 632 | int ret; |
| 633 | |
| 634 | /* enable RX and keep register reset default values else */ |
Egil Hjelmeland | 451d3ca | 2017-08-05 13:05:46 +0200 | [diff] [blame] | 635 | ret = lan9303_write_switch_port(chip, port, LAN9303_MAC_RX_CFG_0, |
| 636 | LAN9303_MAC_RX_CFG_X_REJECT_MAC_TYPES | |
| 637 | LAN9303_MAC_RX_CFG_X_RX_ENABLE); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 638 | if (ret) |
| 639 | return ret; |
| 640 | |
| 641 | /* enable TX and keep register reset default values else */ |
Egil Hjelmeland | 451d3ca | 2017-08-05 13:05:46 +0200 | [diff] [blame] | 642 | return lan9303_write_switch_port(chip, port, LAN9303_MAC_TX_CFG_0, |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 643 | LAN9303_MAC_TX_CFG_X_TX_IFG_CONFIG_DEFAULT | |
| 644 | LAN9303_MAC_TX_CFG_X_TX_PAD_ENABLE | |
| 645 | LAN9303_MAC_TX_CFG_X_TX_ENABLE); |
| 646 | } |
| 647 | |
Egil Hjelmeland | f7e3bfa | 2017-10-10 14:49:52 +0200 | [diff] [blame] | 648 | /* forward special tagged packets from port 0 to port 1 *or* port 2 */ |
| 649 | static int lan9303_setup_tagging(struct lan9303 *chip) |
| 650 | { |
| 651 | int ret; |
| 652 | u32 val; |
| 653 | /* enable defining the destination port via special VLAN tagging |
| 654 | * for port 0 |
| 655 | */ |
| 656 | ret = lan9303_write_switch_reg(chip, LAN9303_SWE_INGRESS_PORT_TYPE, |
| 657 | LAN9303_SWE_INGRESS_PORT_TYPE_VLAN); |
| 658 | if (ret) |
| 659 | return ret; |
| 660 | |
| 661 | /* tag incoming packets at port 1 and 2 on their way to port 0 to be |
| 662 | * able to discover their source port |
| 663 | */ |
| 664 | val = LAN9303_BM_EGRSS_PORT_TYPE_SPECIAL_TAG_PORT0; |
| 665 | return lan9303_write_switch_reg(chip, LAN9303_BM_EGRSS_PORT_TYPE, val); |
| 666 | } |
| 667 | |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 668 | /* We want a special working switch: |
| 669 | * - do not forward packets between port 1 and 2 |
| 670 | * - forward everything from port 1 to port 0 |
| 671 | * - forward everything from port 2 to port 0 |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 672 | */ |
| 673 | static int lan9303_separate_ports(struct lan9303 *chip) |
| 674 | { |
| 675 | int ret; |
| 676 | |
| 677 | ret = lan9303_write_switch_reg(chip, LAN9303_SWE_PORT_MIRROR, |
| 678 | LAN9303_SWE_PORT_MIRROR_SNIFFER_PORT0 | |
| 679 | LAN9303_SWE_PORT_MIRROR_MIRRORED_PORT1 | |
| 680 | LAN9303_SWE_PORT_MIRROR_MIRRORED_PORT2 | |
| 681 | LAN9303_SWE_PORT_MIRROR_ENABLE_RX_MIRRORING | |
| 682 | LAN9303_SWE_PORT_MIRROR_SNIFF_ALL); |
| 683 | if (ret) |
| 684 | return ret; |
| 685 | |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 686 | /* prevent port 1 and 2 from forwarding packets by their own */ |
| 687 | return lan9303_write_switch_reg(chip, LAN9303_SWE_PORT_STATE, |
| 688 | LAN9303_SWE_PORT_STATE_FORWARDING_PORT0 | |
| 689 | LAN9303_SWE_PORT_STATE_BLOCKING_PORT1 | |
| 690 | LAN9303_SWE_PORT_STATE_BLOCKING_PORT2); |
| 691 | } |
| 692 | |
Egil Hjelmeland | d99a86a | 2017-10-10 14:49:53 +0200 | [diff] [blame] | 693 | static void lan9303_bridge_ports(struct lan9303 *chip) |
| 694 | { |
| 695 | /* ports bridged: remove mirroring */ |
| 696 | lan9303_write_switch_reg(chip, LAN9303_SWE_PORT_MIRROR, |
| 697 | LAN9303_SWE_PORT_MIRROR_DISABLED); |
| 698 | |
| 699 | lan9303_write_switch_reg(chip, LAN9303_SWE_PORT_STATE, |
| 700 | chip->swe_port_state); |
| 701 | } |
| 702 | |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 703 | static int lan9303_handle_reset(struct lan9303 *chip) |
| 704 | { |
| 705 | if (!chip->reset_gpio) |
| 706 | return 0; |
| 707 | |
| 708 | if (chip->reset_duration != 0) |
| 709 | msleep(chip->reset_duration); |
| 710 | |
| 711 | /* release (deassert) reset and activate the device */ |
| 712 | gpiod_set_value_cansleep(chip->reset_gpio, 0); |
| 713 | |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | /* stop processing packets for all ports */ |
| 718 | static int lan9303_disable_processing(struct lan9303 *chip) |
| 719 | { |
Egil Hjelmeland | b3d14a2 | 2017-08-05 13:05:48 +0200 | [diff] [blame] | 720 | int p; |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 721 | |
Egil Hjelmeland | b3d14a2 | 2017-08-05 13:05:48 +0200 | [diff] [blame] | 722 | for (p = 0; p < LAN9303_NUM_PORTS; p++) { |
Egil Hjelmeland | 9c84258 | 2017-08-05 13:05:49 +0200 | [diff] [blame] | 723 | int ret = lan9303_disable_processing_port(chip, p); |
Egil Hjelmeland | b3d14a2 | 2017-08-05 13:05:48 +0200 | [diff] [blame] | 724 | |
| 725 | if (ret) |
| 726 | return ret; |
| 727 | } |
| 728 | |
| 729 | return 0; |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | static int lan9303_check_device(struct lan9303 *chip) |
| 733 | { |
| 734 | int ret; |
| 735 | u32 reg; |
| 736 | |
| 737 | ret = lan9303_read(chip->regmap, LAN9303_CHIP_REV, ®); |
| 738 | if (ret) { |
| 739 | dev_err(chip->dev, "failed to read chip revision register: %d\n", |
| 740 | ret); |
| 741 | if (!chip->reset_gpio) { |
| 742 | dev_dbg(chip->dev, |
| 743 | "hint: maybe failed due to missing reset GPIO\n"); |
| 744 | } |
| 745 | return ret; |
| 746 | } |
| 747 | |
| 748 | if ((reg >> 16) != LAN9303_CHIP_ID) { |
| 749 | dev_err(chip->dev, "expecting LAN9303 chip, but found: %X\n", |
| 750 | reg >> 16); |
| 751 | return ret; |
| 752 | } |
| 753 | |
| 754 | /* The default state of the LAN9303 device is to forward packets between |
| 755 | * all ports (if not configured differently by an external EEPROM). |
| 756 | * The initial state of a DSA device must be forwarding packets only |
| 757 | * between the external and the internal ports and no forwarding |
| 758 | * between the external ports. In preparation we stop packet handling |
| 759 | * at all for now until the LAN9303 device is re-programmed accordingly. |
| 760 | */ |
| 761 | ret = lan9303_disable_processing(chip); |
| 762 | if (ret) |
| 763 | dev_warn(chip->dev, "failed to disable switching %d\n", ret); |
| 764 | |
| 765 | dev_info(chip->dev, "Found LAN9303 rev. %u\n", reg & 0xffff); |
| 766 | |
| 767 | ret = lan9303_detect_phy_setup(chip); |
| 768 | if (ret) { |
| 769 | dev_err(chip->dev, |
| 770 | "failed to discover phy bootstrap setup: %d\n", ret); |
| 771 | return ret; |
| 772 | } |
| 773 | |
| 774 | return 0; |
| 775 | } |
| 776 | |
| 777 | /* ---------------------------- DSA -----------------------------------*/ |
| 778 | |
| 779 | static enum dsa_tag_protocol lan9303_get_tag_protocol(struct dsa_switch *ds) |
| 780 | { |
| 781 | return DSA_TAG_PROTO_LAN9303; |
| 782 | } |
| 783 | |
| 784 | static int lan9303_setup(struct dsa_switch *ds) |
| 785 | { |
| 786 | struct lan9303 *chip = ds->priv; |
| 787 | int ret; |
| 788 | |
| 789 | /* Make sure that port 0 is the cpu port */ |
| 790 | if (!dsa_is_cpu_port(ds, 0)) { |
| 791 | dev_err(chip->dev, "port 0 is not the CPU port\n"); |
| 792 | return -EINVAL; |
| 793 | } |
| 794 | |
Egil Hjelmeland | f7e3bfa | 2017-10-10 14:49:52 +0200 | [diff] [blame] | 795 | ret = lan9303_setup_tagging(chip); |
| 796 | if (ret) |
| 797 | dev_err(chip->dev, "failed to setup port tagging %d\n", ret); |
| 798 | |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 799 | ret = lan9303_separate_ports(chip); |
| 800 | if (ret) |
| 801 | dev_err(chip->dev, "failed to separate ports %d\n", ret); |
| 802 | |
Egil Hjelmeland | 9c84258 | 2017-08-05 13:05:49 +0200 | [diff] [blame] | 803 | ret = lan9303_enable_processing_port(chip, 0); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 804 | if (ret) |
| 805 | dev_err(chip->dev, "failed to re-enable switching %d\n", ret); |
| 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | struct lan9303_mib_desc { |
| 811 | unsigned int offset; /* offset of first MAC */ |
| 812 | const char *name; |
| 813 | }; |
| 814 | |
| 815 | static const struct lan9303_mib_desc lan9303_mib[] = { |
| 816 | { .offset = LAN9303_MAC_RX_BRDCST_CNT_0, .name = "RxBroad", }, |
| 817 | { .offset = LAN9303_MAC_RX_PAUSE_CNT_0, .name = "RxPause", }, |
| 818 | { .offset = LAN9303_MAC_RX_MULCST_CNT_0, .name = "RxMulti", }, |
| 819 | { .offset = LAN9303_MAC_RX_PKTOK_CNT_0, .name = "RxOk", }, |
| 820 | { .offset = LAN9303_MAC_RX_CRCERR_CNT_0, .name = "RxCrcErr", }, |
| 821 | { .offset = LAN9303_MAC_RX_ALIGN_CNT_0, .name = "RxAlignErr", }, |
| 822 | { .offset = LAN9303_MAC_RX_JABB_CNT_0, .name = "RxJabber", }, |
| 823 | { .offset = LAN9303_MAC_RX_FRAG_CNT_0, .name = "RxFragment", }, |
| 824 | { .offset = LAN9303_MAC_RX_64_CNT_0, .name = "Rx64Byte", }, |
| 825 | { .offset = LAN9303_MAC_RX_127_CNT_0, .name = "Rx128Byte", }, |
| 826 | { .offset = LAN9303_MAC_RX_255_CNT_0, .name = "Rx256Byte", }, |
| 827 | { .offset = LAN9303_MAC_RX_511_CNT_0, .name = "Rx512Byte", }, |
| 828 | { .offset = LAN9303_MAC_RX_1023_CNT_0, .name = "Rx1024Byte", }, |
| 829 | { .offset = LAN9303_MAC_RX_MAX_CNT_0, .name = "RxMaxByte", }, |
| 830 | { .offset = LAN9303_MAC_RX_PKTLEN_CNT_0, .name = "RxByteCnt", }, |
| 831 | { .offset = LAN9303_MAC_RX_SYMBL_CNT_0, .name = "RxSymbolCnt", }, |
| 832 | { .offset = LAN9303_MAC_RX_CTLFRM_CNT_0, .name = "RxCfs", }, |
| 833 | { .offset = LAN9303_MAC_RX_OVRSZE_CNT_0, .name = "RxOverFlow", }, |
| 834 | { .offset = LAN9303_MAC_TX_UNDSZE_CNT_0, .name = "TxShort", }, |
| 835 | { .offset = LAN9303_MAC_TX_BRDCST_CNT_0, .name = "TxBroad", }, |
| 836 | { .offset = LAN9303_MAC_TX_PAUSE_CNT_0, .name = "TxPause", }, |
| 837 | { .offset = LAN9303_MAC_TX_MULCST_CNT_0, .name = "TxMulti", }, |
| 838 | { .offset = LAN9303_MAC_RX_UNDSZE_CNT_0, .name = "TxUnderRun", }, |
| 839 | { .offset = LAN9303_MAC_TX_64_CNT_0, .name = "Tx64Byte", }, |
| 840 | { .offset = LAN9303_MAC_TX_127_CNT_0, .name = "Tx128Byte", }, |
| 841 | { .offset = LAN9303_MAC_TX_255_CNT_0, .name = "Tx256Byte", }, |
| 842 | { .offset = LAN9303_MAC_TX_511_CNT_0, .name = "Tx512Byte", }, |
| 843 | { .offset = LAN9303_MAC_TX_1023_CNT_0, .name = "Tx1024Byte", }, |
| 844 | { .offset = LAN9303_MAC_TX_MAX_CNT_0, .name = "TxMaxByte", }, |
| 845 | { .offset = LAN9303_MAC_TX_PKTLEN_CNT_0, .name = "TxByteCnt", }, |
| 846 | { .offset = LAN9303_MAC_TX_PKTOK_CNT_0, .name = "TxOk", }, |
| 847 | { .offset = LAN9303_MAC_TX_TOTALCOL_CNT_0, .name = "TxCollision", }, |
| 848 | { .offset = LAN9303_MAC_TX_MULTICOL_CNT_0, .name = "TxMultiCol", }, |
| 849 | { .offset = LAN9303_MAC_TX_SNGLECOL_CNT_0, .name = "TxSingleCol", }, |
| 850 | { .offset = LAN9303_MAC_TX_EXCOL_CNT_0, .name = "TxExcCol", }, |
| 851 | { .offset = LAN9303_MAC_TX_DEFER_CNT_0, .name = "TxDefer", }, |
| 852 | { .offset = LAN9303_MAC_TX_LATECOL_0, .name = "TxLateCol", }, |
| 853 | }; |
| 854 | |
| 855 | static void lan9303_get_strings(struct dsa_switch *ds, int port, uint8_t *data) |
| 856 | { |
| 857 | unsigned int u; |
| 858 | |
| 859 | for (u = 0; u < ARRAY_SIZE(lan9303_mib); u++) { |
| 860 | strncpy(data + u * ETH_GSTRING_LEN, lan9303_mib[u].name, |
| 861 | ETH_GSTRING_LEN); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | static void lan9303_get_ethtool_stats(struct dsa_switch *ds, int port, |
| 866 | uint64_t *data) |
| 867 | { |
| 868 | struct lan9303 *chip = ds->priv; |
Egil Hjelmeland | 0a967b4 | 2017-08-05 13:05:50 +0200 | [diff] [blame] | 869 | unsigned int u; |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 870 | |
| 871 | for (u = 0; u < ARRAY_SIZE(lan9303_mib); u++) { |
Egil Hjelmeland | 0a967b4 | 2017-08-05 13:05:50 +0200 | [diff] [blame] | 872 | u32 reg; |
| 873 | int ret; |
| 874 | |
| 875 | ret = lan9303_read_switch_port( |
| 876 | chip, port, lan9303_mib[u].offset, ®); |
| 877 | |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 878 | if (ret) |
Egil Hjelmeland | 0a967b4 | 2017-08-05 13:05:50 +0200 | [diff] [blame] | 879 | dev_warn(chip->dev, "Reading status port %d reg %u failed\n", |
| 880 | port, lan9303_mib[u].offset); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 881 | data[u] = reg; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | static int lan9303_get_sset_count(struct dsa_switch *ds) |
| 886 | { |
| 887 | return ARRAY_SIZE(lan9303_mib); |
| 888 | } |
| 889 | |
| 890 | static int lan9303_phy_read(struct dsa_switch *ds, int phy, int regnum) |
| 891 | { |
| 892 | struct lan9303 *chip = ds->priv; |
| 893 | int phy_base = chip->phy_addr_sel_strap; |
| 894 | |
| 895 | if (phy == phy_base) |
| 896 | return lan9303_virt_phy_reg_read(chip, regnum); |
| 897 | if (phy > phy_base + 2) |
| 898 | return -ENODEV; |
| 899 | |
Egil Hjelmeland | 2c34089 | 2017-07-30 19:58:56 +0200 | [diff] [blame] | 900 | return chip->ops->phy_read(chip, phy, regnum); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | static int lan9303_phy_write(struct dsa_switch *ds, int phy, int regnum, |
| 904 | u16 val) |
| 905 | { |
| 906 | struct lan9303 *chip = ds->priv; |
| 907 | int phy_base = chip->phy_addr_sel_strap; |
| 908 | |
| 909 | if (phy == phy_base) |
| 910 | return lan9303_virt_phy_reg_write(chip, regnum, val); |
| 911 | if (phy > phy_base + 2) |
| 912 | return -ENODEV; |
| 913 | |
Egil Hjelmeland | 2c34089 | 2017-07-30 19:58:56 +0200 | [diff] [blame] | 914 | return chip->ops->phy_write(chip, phy, regnum, val); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 915 | } |
| 916 | |
Egil Hjelmeland | 4d6a78b | 2017-09-19 10:09:24 +0200 | [diff] [blame] | 917 | static void lan9303_adjust_link(struct dsa_switch *ds, int port, |
| 918 | struct phy_device *phydev) |
| 919 | { |
| 920 | struct lan9303 *chip = ds->priv; |
| 921 | int ctl, res; |
| 922 | |
| 923 | if (!phy_is_pseudo_fixed_link(phydev)) |
| 924 | return; |
| 925 | |
| 926 | ctl = lan9303_phy_read(ds, port, MII_BMCR); |
| 927 | |
| 928 | ctl &= ~BMCR_ANENABLE; |
| 929 | |
| 930 | if (phydev->speed == SPEED_100) |
| 931 | ctl |= BMCR_SPEED100; |
| 932 | else if (phydev->speed == SPEED_10) |
| 933 | ctl &= ~BMCR_SPEED100; |
| 934 | else |
| 935 | dev_err(ds->dev, "unsupported speed: %d\n", phydev->speed); |
| 936 | |
| 937 | if (phydev->duplex == DUPLEX_FULL) |
| 938 | ctl |= BMCR_FULLDPLX; |
| 939 | else |
| 940 | ctl &= ~BMCR_FULLDPLX; |
| 941 | |
| 942 | res = lan9303_phy_write(ds, port, MII_BMCR, ctl); |
| 943 | |
| 944 | if (port == chip->phy_addr_sel_strap) { |
| 945 | /* Virtual Phy: Remove Turbo 200Mbit mode */ |
| 946 | lan9303_read(chip->regmap, LAN9303_VIRT_SPECIAL_CTRL, &ctl); |
| 947 | |
| 948 | ctl &= ~LAN9303_VIRT_SPECIAL_TURBO; |
| 949 | res = regmap_write(chip->regmap, |
| 950 | LAN9303_VIRT_SPECIAL_CTRL, ctl); |
| 951 | } |
| 952 | } |
| 953 | |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 954 | static int lan9303_port_enable(struct dsa_switch *ds, int port, |
| 955 | struct phy_device *phy) |
| 956 | { |
| 957 | struct lan9303 *chip = ds->priv; |
| 958 | |
| 959 | /* enable internal packet processing */ |
| 960 | switch (port) { |
| 961 | case 1: |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 962 | case 2: |
Egil Hjelmeland | 9c84258 | 2017-08-05 13:05:49 +0200 | [diff] [blame] | 963 | return lan9303_enable_processing_port(chip, port); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 964 | default: |
| 965 | dev_dbg(chip->dev, |
| 966 | "Error: request to power up invalid port %d\n", port); |
| 967 | } |
| 968 | |
| 969 | return -ENODEV; |
| 970 | } |
| 971 | |
| 972 | static void lan9303_port_disable(struct dsa_switch *ds, int port, |
| 973 | struct phy_device *phy) |
| 974 | { |
| 975 | struct lan9303 *chip = ds->priv; |
| 976 | |
| 977 | /* disable internal packet processing */ |
| 978 | switch (port) { |
| 979 | case 1: |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 980 | case 2: |
Egil Hjelmeland | 9c84258 | 2017-08-05 13:05:49 +0200 | [diff] [blame] | 981 | lan9303_disable_processing_port(chip, port); |
Egil Hjelmeland | b3d14a2 | 2017-08-05 13:05:48 +0200 | [diff] [blame] | 982 | lan9303_phy_write(ds, chip->phy_addr_sel_strap + port, |
Egil Hjelmeland | 2c34089 | 2017-07-30 19:58:56 +0200 | [diff] [blame] | 983 | MII_BMCR, BMCR_PDOWN); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 984 | break; |
| 985 | default: |
| 986 | dev_dbg(chip->dev, |
| 987 | "Error: request to power down invalid port %d\n", port); |
| 988 | } |
| 989 | } |
| 990 | |
Egil Hjelmeland | d99a86a | 2017-10-10 14:49:53 +0200 | [diff] [blame] | 991 | static int lan9303_port_bridge_join(struct dsa_switch *ds, int port, |
| 992 | struct net_device *br) |
| 993 | { |
| 994 | struct lan9303 *chip = ds->priv; |
| 995 | |
| 996 | dev_dbg(chip->dev, "%s(port %d)\n", __func__, port); |
Vivien Didelot | c8652c8 | 2017-10-16 11:12:19 -0400 | [diff] [blame] | 997 | if (dsa_to_port(ds, 1)->bridge_dev == dsa_to_port(ds, 2)->bridge_dev) { |
Egil Hjelmeland | d99a86a | 2017-10-10 14:49:53 +0200 | [diff] [blame] | 998 | lan9303_bridge_ports(chip); |
| 999 | chip->is_bridged = true; /* unleash stp_state_set() */ |
| 1000 | } |
| 1001 | |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
| 1005 | static void lan9303_port_bridge_leave(struct dsa_switch *ds, int port, |
| 1006 | struct net_device *br) |
| 1007 | { |
| 1008 | struct lan9303 *chip = ds->priv; |
| 1009 | |
| 1010 | dev_dbg(chip->dev, "%s(port %d)\n", __func__, port); |
| 1011 | if (chip->is_bridged) { |
| 1012 | lan9303_separate_ports(chip); |
| 1013 | chip->is_bridged = false; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | static void lan9303_port_stp_state_set(struct dsa_switch *ds, int port, |
| 1018 | u8 state) |
| 1019 | { |
| 1020 | int portmask, portstate; |
| 1021 | struct lan9303 *chip = ds->priv; |
| 1022 | |
| 1023 | dev_dbg(chip->dev, "%s(port %d, state %d)\n", |
| 1024 | __func__, port, state); |
| 1025 | |
| 1026 | switch (state) { |
| 1027 | case BR_STATE_DISABLED: |
| 1028 | portstate = LAN9303_SWE_PORT_STATE_DISABLED_PORT0; |
| 1029 | break; |
| 1030 | case BR_STATE_BLOCKING: |
| 1031 | case BR_STATE_LISTENING: |
| 1032 | portstate = LAN9303_SWE_PORT_STATE_BLOCKING_PORT0; |
| 1033 | break; |
| 1034 | case BR_STATE_LEARNING: |
| 1035 | portstate = LAN9303_SWE_PORT_STATE_LEARNING_PORT0; |
| 1036 | break; |
| 1037 | case BR_STATE_FORWARDING: |
| 1038 | portstate = LAN9303_SWE_PORT_STATE_FORWARDING_PORT0; |
| 1039 | break; |
| 1040 | default: |
| 1041 | portstate = LAN9303_SWE_PORT_STATE_DISABLED_PORT0; |
| 1042 | dev_err(chip->dev, "unknown stp state: port %d, state %d\n", |
| 1043 | port, state); |
| 1044 | } |
| 1045 | |
| 1046 | portmask = 0x3 << (port * 2); |
| 1047 | portstate <<= (port * 2); |
| 1048 | |
| 1049 | chip->swe_port_state = (chip->swe_port_state & ~portmask) | portstate; |
| 1050 | |
| 1051 | if (chip->is_bridged) |
| 1052 | lan9303_write_switch_reg(chip, LAN9303_SWE_PORT_STATE, |
| 1053 | chip->swe_port_state); |
| 1054 | /* else: touching SWE_PORT_STATE would break port separation */ |
| 1055 | } |
| 1056 | |
Egil Hjelmeland | ab33534 | 2017-10-20 12:19:09 +0200 | [diff] [blame^] | 1057 | static void lan9303_port_fast_age(struct dsa_switch *ds, int port) |
| 1058 | { |
| 1059 | struct lan9303 *chip = ds->priv; |
| 1060 | struct del_port_learned_ctx del_ctx = { |
| 1061 | .port = port, |
| 1062 | }; |
| 1063 | |
| 1064 | dev_dbg(chip->dev, "%s(%d)\n", __func__, port); |
| 1065 | lan9303_alr_loop(chip, alr_loop_cb_del_port_learned, &del_ctx); |
| 1066 | } |
| 1067 | |
| 1068 | static int lan9303_port_fdb_dump(struct dsa_switch *ds, int port, |
| 1069 | dsa_fdb_dump_cb_t *cb, void *data) |
| 1070 | { |
| 1071 | struct lan9303 *chip = ds->priv; |
| 1072 | struct port_fdb_dump_ctx dump_ctx = { |
| 1073 | .port = port, |
| 1074 | .data = data, |
| 1075 | .cb = cb, |
| 1076 | }; |
| 1077 | |
| 1078 | dev_dbg(chip->dev, "%s(%d)\n", __func__, port); |
| 1079 | lan9303_alr_loop(chip, alr_loop_cb_fdb_port_dump, &dump_ctx); |
| 1080 | |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
Bhumika Goyal | d78d677 | 2017-08-09 10:34:15 +0530 | [diff] [blame] | 1084 | static const struct dsa_switch_ops lan9303_switch_ops = { |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 1085 | .get_tag_protocol = lan9303_get_tag_protocol, |
| 1086 | .setup = lan9303_setup, |
| 1087 | .get_strings = lan9303_get_strings, |
| 1088 | .phy_read = lan9303_phy_read, |
| 1089 | .phy_write = lan9303_phy_write, |
Egil Hjelmeland | 4d6a78b | 2017-09-19 10:09:24 +0200 | [diff] [blame] | 1090 | .adjust_link = lan9303_adjust_link, |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 1091 | .get_ethtool_stats = lan9303_get_ethtool_stats, |
| 1092 | .get_sset_count = lan9303_get_sset_count, |
| 1093 | .port_enable = lan9303_port_enable, |
| 1094 | .port_disable = lan9303_port_disable, |
Egil Hjelmeland | d99a86a | 2017-10-10 14:49:53 +0200 | [diff] [blame] | 1095 | .port_bridge_join = lan9303_port_bridge_join, |
| 1096 | .port_bridge_leave = lan9303_port_bridge_leave, |
| 1097 | .port_stp_state_set = lan9303_port_stp_state_set, |
Egil Hjelmeland | ab33534 | 2017-10-20 12:19:09 +0200 | [diff] [blame^] | 1098 | .port_fast_age = lan9303_port_fast_age, |
| 1099 | .port_fdb_dump = lan9303_port_fdb_dump, |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 1100 | }; |
| 1101 | |
| 1102 | static int lan9303_register_switch(struct lan9303 *chip) |
| 1103 | { |
Egil Hjelmeland | 274cdb4 | 2017-08-08 00:22:21 +0200 | [diff] [blame] | 1104 | chip->ds = dsa_switch_alloc(chip->dev, LAN9303_NUM_PORTS); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 1105 | if (!chip->ds) |
| 1106 | return -ENOMEM; |
| 1107 | |
| 1108 | chip->ds->priv = chip; |
| 1109 | chip->ds->ops = &lan9303_switch_ops; |
| 1110 | chip->ds->phys_mii_mask = chip->phy_addr_sel_strap ? 0xe : 0x7; |
| 1111 | |
Vivien Didelot | 23c9ee4 | 2017-05-26 18:12:51 -0400 | [diff] [blame] | 1112 | return dsa_register_switch(chip->ds); |
Juergen Beisert | a129259 | 2017-04-18 10:48:25 +0200 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | static void lan9303_probe_reset_gpio(struct lan9303 *chip, |
| 1116 | struct device_node *np) |
| 1117 | { |
| 1118 | chip->reset_gpio = devm_gpiod_get_optional(chip->dev, "reset", |
| 1119 | GPIOD_OUT_LOW); |
| 1120 | |
| 1121 | if (!chip->reset_gpio) { |
| 1122 | dev_dbg(chip->dev, "No reset GPIO defined\n"); |
| 1123 | return; |
| 1124 | } |
| 1125 | |
| 1126 | chip->reset_duration = 200; |
| 1127 | |
| 1128 | if (np) { |
| 1129 | of_property_read_u32(np, "reset-duration", |
| 1130 | &chip->reset_duration); |
| 1131 | } else { |
| 1132 | dev_dbg(chip->dev, "reset duration defaults to 200 ms\n"); |
| 1133 | } |
| 1134 | |
| 1135 | /* A sane reset duration should not be longer than 1s */ |
| 1136 | if (chip->reset_duration > 1000) |
| 1137 | chip->reset_duration = 1000; |
| 1138 | } |
| 1139 | |
| 1140 | int lan9303_probe(struct lan9303 *chip, struct device_node *np) |
| 1141 | { |
| 1142 | int ret; |
| 1143 | |
| 1144 | mutex_init(&chip->indirect_mutex); |
| 1145 | |
| 1146 | lan9303_probe_reset_gpio(chip, np); |
| 1147 | |
| 1148 | ret = lan9303_handle_reset(chip); |
| 1149 | if (ret) |
| 1150 | return ret; |
| 1151 | |
| 1152 | ret = lan9303_check_device(chip); |
| 1153 | if (ret) |
| 1154 | return ret; |
| 1155 | |
| 1156 | ret = lan9303_register_switch(chip); |
| 1157 | if (ret) { |
| 1158 | dev_dbg(chip->dev, "Failed to register switch: %d\n", ret); |
| 1159 | return ret; |
| 1160 | } |
| 1161 | |
| 1162 | return 0; |
| 1163 | } |
| 1164 | EXPORT_SYMBOL(lan9303_probe); |
| 1165 | |
| 1166 | int lan9303_remove(struct lan9303 *chip) |
| 1167 | { |
| 1168 | int rc; |
| 1169 | |
| 1170 | rc = lan9303_disable_processing(chip); |
| 1171 | if (rc != 0) |
| 1172 | dev_warn(chip->dev, "shutting down failed\n"); |
| 1173 | |
| 1174 | dsa_unregister_switch(chip->ds); |
| 1175 | |
| 1176 | /* assert reset to the whole device to prevent it from doing anything */ |
| 1177 | gpiod_set_value_cansleep(chip->reset_gpio, 1); |
| 1178 | gpiod_unexport(chip->reset_gpio); |
| 1179 | |
| 1180 | return 0; |
| 1181 | } |
| 1182 | EXPORT_SYMBOL(lan9303_remove); |
| 1183 | |
| 1184 | MODULE_AUTHOR("Juergen Borleis <kernel@pengutronix.de>"); |
| 1185 | MODULE_DESCRIPTION("Core driver for SMSC/Microchip LAN9303 three port ethernet switch"); |
| 1186 | MODULE_LICENSE("GPL v2"); |