| 1 | /* |
|---|
| 2 | * Driver for the built-in ethernet switch of the Atheros AR7240 SoC |
|---|
| 3 | * Copyright (c) 2010 Gabor Juhos <juhosg@openwrt.org> |
|---|
| 4 | * Copyright (c) 2010 Felix Fietkau <nbd@openwrt.org> |
|---|
| 5 | * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify it |
|---|
| 7 | * under the terms of the GNU General Public License version 2 as published |
|---|
| 8 | * by the Free Software Foundation. |
|---|
| 9 | * |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | #include <linux/etherdevice.h> |
|---|
| 13 | #include <linux/list.h> |
|---|
| 14 | #include <linux/netdevice.h> |
|---|
| 15 | #include <linux/phy.h> |
|---|
| 16 | #include <linux/mii.h> |
|---|
| 17 | #include <linux/bitops.h> |
|---|
| 18 | #include <linux/switch.h> |
|---|
| 19 | #include "ag71xx.h" |
|---|
| 20 | |
|---|
| 21 | #define BITM(_count) (BIT(_count) - 1) |
|---|
| 22 | #define BITS(_shift, _count) (BITM(_count) << _shift) |
|---|
| 23 | |
|---|
| 24 | #define AR7240_REG_MASK_CTRL 0x00 |
|---|
| 25 | #define AR7240_MASK_CTRL_REVISION_M BITM(8) |
|---|
| 26 | #define AR7240_MASK_CTRL_VERSION_M BITM(8) |
|---|
| 27 | #define AR7240_MASK_CTRL_VERSION_S 8 |
|---|
| 28 | #define AR7240_MASK_CTRL_VERSION_AR7240 0x01 |
|---|
| 29 | #define AR7240_MASK_CTRL_VERSION_AR934X 0x02 |
|---|
| 30 | #define AR7240_MASK_CTRL_SOFT_RESET BIT(31) |
|---|
| 31 | |
|---|
| 32 | #define AR7240_REG_MAC_ADDR0 0x20 |
|---|
| 33 | #define AR7240_REG_MAC_ADDR1 0x24 |
|---|
| 34 | |
|---|
| 35 | #define AR7240_REG_FLOOD_MASK 0x2c |
|---|
| 36 | #define AR7240_FLOOD_MASK_BROAD_TO_CPU BIT(26) |
|---|
| 37 | |
|---|
| 38 | #define AR7240_REG_GLOBAL_CTRL 0x30 |
|---|
| 39 | #define AR7240_GLOBAL_CTRL_MTU_M BITM(12) |
|---|
| 40 | |
|---|
| 41 | #define AR7240_REG_VTU 0x0040 |
|---|
| 42 | #define AR7240_VTU_OP BITM(3) |
|---|
| 43 | #define AR7240_VTU_OP_NOOP 0x0 |
|---|
| 44 | #define AR7240_VTU_OP_FLUSH 0x1 |
|---|
| 45 | #define AR7240_VTU_OP_LOAD 0x2 |
|---|
| 46 | #define AR7240_VTU_OP_PURGE 0x3 |
|---|
| 47 | #define AR7240_VTU_OP_REMOVE_PORT 0x4 |
|---|
| 48 | #define AR7240_VTU_ACTIVE BIT(3) |
|---|
| 49 | #define AR7240_VTU_FULL BIT(4) |
|---|
| 50 | #define AR7240_VTU_PORT BITS(8, 4) |
|---|
| 51 | #define AR7240_VTU_PORT_S 8 |
|---|
| 52 | #define AR7240_VTU_VID BITS(16, 12) |
|---|
| 53 | #define AR7240_VTU_VID_S 16 |
|---|
| 54 | #define AR7240_VTU_PRIO BITS(28, 3) |
|---|
| 55 | #define AR7240_VTU_PRIO_S 28 |
|---|
| 56 | #define AR7240_VTU_PRIO_EN BIT(31) |
|---|
| 57 | |
|---|
| 58 | #define AR7240_REG_VTU_DATA 0x0044 |
|---|
| 59 | #define AR7240_VTUDATA_MEMBER BITS(0, 10) |
|---|
| 60 | #define AR7240_VTUDATA_VALID BIT(11) |
|---|
| 61 | |
|---|
| 62 | #define AR7240_REG_ATU 0x50 |
|---|
| 63 | #define AR7240_ATU_FLUSH_ALL 0x1 |
|---|
| 64 | |
|---|
| 65 | #define AR7240_REG_AT_CTRL 0x5c |
|---|
| 66 | #define AR7240_AT_CTRL_AGE_TIME BITS(0, 15) |
|---|
| 67 | #define AR7240_AT_CTRL_AGE_EN BIT(17) |
|---|
| 68 | #define AR7240_AT_CTRL_LEARN_CHANGE BIT(18) |
|---|
| 69 | #define AR7240_AT_CTRL_RESERVED BIT(19) |
|---|
| 70 | #define AR7240_AT_CTRL_ARP_EN BIT(20) |
|---|
| 71 | |
|---|
| 72 | #define AR7240_REG_TAG_PRIORITY 0x70 |
|---|
| 73 | |
|---|
| 74 | #define AR7240_REG_SERVICE_TAG 0x74 |
|---|
| 75 | #define AR7240_SERVICE_TAG_M BITM(16) |
|---|
| 76 | |
|---|
| 77 | #define AR7240_REG_CPU_PORT 0x78 |
|---|
| 78 | #define AR7240_MIRROR_PORT_S 4 |
|---|
| 79 | #define AR7240_CPU_PORT_EN BIT(8) |
|---|
| 80 | |
|---|
| 81 | #define AR7240_REG_MIB_FUNCTION0 0x80 |
|---|
| 82 | #define AR7240_MIB_TIMER_M BITM(16) |
|---|
| 83 | #define AR7240_MIB_AT_HALF_EN BIT(16) |
|---|
| 84 | #define AR7240_MIB_BUSY BIT(17) |
|---|
| 85 | #define AR7240_MIB_FUNC_S 24 |
|---|
| 86 | #define AR7240_MIB_FUNC_NO_OP 0x0 |
|---|
| 87 | #define AR7240_MIB_FUNC_FLUSH 0x1 |
|---|
| 88 | #define AR7240_MIB_FUNC_CAPTURE 0x3 |
|---|
| 89 | |
|---|
| 90 | #define AR7240_REG_MDIO_CTRL 0x98 |
|---|
| 91 | #define AR7240_MDIO_CTRL_DATA_M BITM(16) |
|---|
| 92 | #define AR7240_MDIO_CTRL_REG_ADDR_S 16 |
|---|
| 93 | #define AR7240_MDIO_CTRL_PHY_ADDR_S 21 |
|---|
| 94 | #define AR7240_MDIO_CTRL_CMD_WRITE 0 |
|---|
| 95 | #define AR7240_MDIO_CTRL_CMD_READ BIT(27) |
|---|
| 96 | #define AR7240_MDIO_CTRL_MASTER_EN BIT(30) |
|---|
| 97 | #define AR7240_MDIO_CTRL_BUSY BIT(31) |
|---|
| 98 | |
|---|
| 99 | #define AR7240_REG_PORT_BASE(_port) (0x100 + (_port) * 0x100) |
|---|
| 100 | |
|---|
| 101 | #define AR7240_REG_PORT_STATUS(_port) (AR7240_REG_PORT_BASE((_port)) + 0x00) |
|---|
| 102 | #define AR7240_PORT_STATUS_SPEED_S 0 |
|---|
| 103 | #define AR7240_PORT_STATUS_SPEED_M BITM(2) |
|---|
| 104 | #define AR7240_PORT_STATUS_SPEED_10 0 |
|---|
| 105 | #define AR7240_PORT_STATUS_SPEED_100 1 |
|---|
| 106 | #define AR7240_PORT_STATUS_SPEED_1000 2 |
|---|
| 107 | #define AR7240_PORT_STATUS_TXMAC BIT(2) |
|---|
| 108 | #define AR7240_PORT_STATUS_RXMAC BIT(3) |
|---|
| 109 | #define AR7240_PORT_STATUS_TXFLOW BIT(4) |
|---|
| 110 | #define AR7240_PORT_STATUS_RXFLOW BIT(5) |
|---|
| 111 | #define AR7240_PORT_STATUS_DUPLEX BIT(6) |
|---|
| 112 | #define AR7240_PORT_STATUS_LINK_UP BIT(8) |
|---|
| 113 | #define AR7240_PORT_STATUS_LINK_AUTO BIT(9) |
|---|
| 114 | #define AR7240_PORT_STATUS_LINK_PAUSE BIT(10) |
|---|
| 115 | |
|---|
| 116 | #define AR7240_REG_PORT_CTRL(_port) (AR7240_REG_PORT_BASE((_port)) + 0x04) |
|---|
| 117 | #define AR7240_PORT_CTRL_STATE_M BITM(3) |
|---|
| 118 | #define AR7240_PORT_CTRL_STATE_DISABLED 0 |
|---|
| 119 | #define AR7240_PORT_CTRL_STATE_BLOCK 1 |
|---|
| 120 | #define AR7240_PORT_CTRL_STATE_LISTEN 2 |
|---|
| 121 | #define AR7240_PORT_CTRL_STATE_LEARN 3 |
|---|
| 122 | #define AR7240_PORT_CTRL_STATE_FORWARD 4 |
|---|
| 123 | #define AR7240_PORT_CTRL_LEARN_LOCK BIT(7) |
|---|
| 124 | #define AR7240_PORT_CTRL_VLAN_MODE_S 8 |
|---|
| 125 | #define AR7240_PORT_CTRL_VLAN_MODE_KEEP 0 |
|---|
| 126 | #define AR7240_PORT_CTRL_VLAN_MODE_STRIP 1 |
|---|
| 127 | #define AR7240_PORT_CTRL_VLAN_MODE_ADD 2 |
|---|
| 128 | #define AR7240_PORT_CTRL_VLAN_MODE_DOUBLE_TAG 3 |
|---|
| 129 | #define AR7240_PORT_CTRL_IGMP_SNOOP BIT(10) |
|---|
| 130 | #define AR7240_PORT_CTRL_HEADER BIT(11) |
|---|
| 131 | #define AR7240_PORT_CTRL_MAC_LOOP BIT(12) |
|---|
| 132 | #define AR7240_PORT_CTRL_SINGLE_VLAN BIT(13) |
|---|
| 133 | #define AR7240_PORT_CTRL_LEARN BIT(14) |
|---|
| 134 | #define AR7240_PORT_CTRL_DOUBLE_TAG BIT(15) |
|---|
| 135 | #define AR7240_PORT_CTRL_MIRROR_TX BIT(16) |
|---|
| 136 | #define AR7240_PORT_CTRL_MIRROR_RX BIT(17) |
|---|
| 137 | |
|---|
| 138 | #define AR7240_REG_PORT_VLAN(_port) (AR7240_REG_PORT_BASE((_port)) + 0x08) |
|---|
| 139 | |
|---|
| 140 | #define AR7240_PORT_VLAN_DEFAULT_ID_S 0 |
|---|
| 141 | #define AR7240_PORT_VLAN_DEST_PORTS_S 16 |
|---|
| 142 | #define AR7240_PORT_VLAN_MODE_S 30 |
|---|
| 143 | #define AR7240_PORT_VLAN_MODE_PORT_ONLY 0 |
|---|
| 144 | #define AR7240_PORT_VLAN_MODE_PORT_FALLBACK 1 |
|---|
| 145 | #define AR7240_PORT_VLAN_MODE_VLAN_ONLY 2 |
|---|
| 146 | #define AR7240_PORT_VLAN_MODE_SECURE 3 |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | #define AR7240_REG_STATS_BASE(_port) (0x20000 + (_port) * 0x100) |
|---|
| 150 | |
|---|
| 151 | #define AR7240_STATS_RXBROAD 0x00 |
|---|
| 152 | #define AR7240_STATS_RXPAUSE 0x04 |
|---|
| 153 | #define AR7240_STATS_RXMULTI 0x08 |
|---|
| 154 | #define AR7240_STATS_RXFCSERR 0x0c |
|---|
| 155 | #define AR7240_STATS_RXALIGNERR 0x10 |
|---|
| 156 | #define AR7240_STATS_RXRUNT 0x14 |
|---|
| 157 | #define AR7240_STATS_RXFRAGMENT 0x18 |
|---|
| 158 | #define AR7240_STATS_RX64BYTE 0x1c |
|---|
| 159 | #define AR7240_STATS_RX128BYTE 0x20 |
|---|
| 160 | #define AR7240_STATS_RX256BYTE 0x24 |
|---|
| 161 | #define AR7240_STATS_RX512BYTE 0x28 |
|---|
| 162 | #define AR7240_STATS_RX1024BYTE 0x2c |
|---|
| 163 | #define AR7240_STATS_RX1518BYTE 0x30 |
|---|
| 164 | #define AR7240_STATS_RXMAXBYTE 0x34 |
|---|
| 165 | #define AR7240_STATS_RXTOOLONG 0x38 |
|---|
| 166 | #define AR7240_STATS_RXGOODBYTE 0x3c |
|---|
| 167 | #define AR7240_STATS_RXBADBYTE 0x44 |
|---|
| 168 | #define AR7240_STATS_RXOVERFLOW 0x4c |
|---|
| 169 | #define AR7240_STATS_FILTERED 0x50 |
|---|
| 170 | #define AR7240_STATS_TXBROAD 0x54 |
|---|
| 171 | #define AR7240_STATS_TXPAUSE 0x58 |
|---|
| 172 | #define AR7240_STATS_TXMULTI 0x5c |
|---|
| 173 | #define AR7240_STATS_TXUNDERRUN 0x60 |
|---|
| 174 | #define AR7240_STATS_TX64BYTE 0x64 |
|---|
| 175 | #define AR7240_STATS_TX128BYTE 0x68 |
|---|
| 176 | #define AR7240_STATS_TX256BYTE 0x6c |
|---|
| 177 | #define AR7240_STATS_TX512BYTE 0x70 |
|---|
| 178 | #define AR7240_STATS_TX1024BYTE 0x74 |
|---|
| 179 | #define AR7240_STATS_TX1518BYTE 0x78 |
|---|
| 180 | #define AR7240_STATS_TXMAXBYTE 0x7c |
|---|
| 181 | #define AR7240_STATS_TXOVERSIZE 0x80 |
|---|
| 182 | #define AR7240_STATS_TXBYTE 0x84 |
|---|
| 183 | #define AR7240_STATS_TXCOLLISION 0x8c |
|---|
| 184 | #define AR7240_STATS_TXABORTCOL 0x90 |
|---|
| 185 | #define AR7240_STATS_TXMULTICOL 0x94 |
|---|
| 186 | #define AR7240_STATS_TXSINGLECOL 0x98 |
|---|
| 187 | #define AR7240_STATS_TXEXCDEFER 0x9c |
|---|
| 188 | #define AR7240_STATS_TXDEFER 0xa0 |
|---|
| 189 | #define AR7240_STATS_TXLATECOL 0xa4 |
|---|
| 190 | |
|---|
| 191 | #define AR7240_PORT_CPU 0 |
|---|
| 192 | #define AR7240_NUM_PORTS 6 |
|---|
| 193 | #define AR7240_NUM_PHYS 5 |
|---|
| 194 | |
|---|
| 195 | #define AR7240_PHY_ID1 0x004d |
|---|
| 196 | #define AR7240_PHY_ID2 0xd041 |
|---|
| 197 | |
|---|
| 198 | #define AR934X_PHY_ID1 0x004d |
|---|
| 199 | #define AR934X_PHY_ID2 0xd042 |
|---|
| 200 | |
|---|
| 201 | #define AR7240_MAX_VLANS 16 |
|---|
| 202 | |
|---|
| 203 | #define AR934X_REG_OPER_MODE0 0x04 |
|---|
| 204 | #define AR934X_OPER_MODE0_MAC_GMII_EN BIT(6) |
|---|
| 205 | #define AR934X_OPER_MODE0_PHY_MII_EN BIT(10) |
|---|
| 206 | |
|---|
| 207 | #define AR934X_REG_OPER_MODE1 0x08 |
|---|
| 208 | #define AR934X_REG_OPER_MODE1_PHY4_MII_EN BIT(28) |
|---|
| 209 | |
|---|
| 210 | #define AR934X_REG_FLOOD_MASK 0x2c |
|---|
| 211 | #define AR934X_FLOOD_MASK_BC_DP(_p) BIT(25 + (_p)) |
|---|
| 212 | |
|---|
| 213 | #define AR934X_REG_QM_CTRL 0x3c |
|---|
| 214 | #define AR934X_QM_CTRL_ARP_EN BIT(15) |
|---|
| 215 | |
|---|
| 216 | #define AR934X_REG_AT_CTRL 0x5c |
|---|
| 217 | #define AR934X_AT_CTRL_AGE_TIME BITS(0, 15) |
|---|
| 218 | #define AR934X_AT_CTRL_AGE_EN BIT(17) |
|---|
| 219 | #define AR934X_AT_CTRL_LEARN_CHANGE BIT(18) |
|---|
| 220 | |
|---|
| 221 | #define AR934X_REG_PORT_BASE(_port) (0x100 + (_port) * 0x100) |
|---|
| 222 | |
|---|
| 223 | #define AR934X_REG_PORT_VLAN1(_port) (AR934X_REG_PORT_BASE((_port)) + 0x08) |
|---|
| 224 | #define AR934X_PORT_VLAN1_DEFAULT_SVID_S 0 |
|---|
| 225 | #define AR934X_PORT_VLAN1_FORCE_DEFAULT_VID_EN BIT(12) |
|---|
| 226 | #define AR934X_PORT_VLAN1_PORT_TLS_MODE BIT(13) |
|---|
| 227 | #define AR934X_PORT_VLAN1_PORT_VLAN_PROP_EN BIT(14) |
|---|
| 228 | #define AR934X_PORT_VLAN1_PORT_CLONE_EN BIT(15) |
|---|
| 229 | #define AR934X_PORT_VLAN1_DEFAULT_CVID_S 16 |
|---|
| 230 | #define AR934X_PORT_VLAN1_FORCE_PORT_VLAN_EN BIT(28) |
|---|
| 231 | #define AR934X_PORT_VLAN1_ING_PORT_PRI_S 29 |
|---|
| 232 | |
|---|
| 233 | #define AR934X_REG_PORT_VLAN2(_port) (AR934X_REG_PORT_BASE((_port)) + 0x0c) |
|---|
| 234 | #define AR934X_PORT_VLAN2_PORT_VID_MEM_S 16 |
|---|
| 235 | #define AR934X_PORT_VLAN2_8021Q_MODE_S 30 |
|---|
| 236 | #define AR934X_PORT_VLAN2_8021Q_MODE_PORT_ONLY 0 |
|---|
| 237 | #define AR934X_PORT_VLAN2_8021Q_MODE_PORT_FALLBACK 1 |
|---|
| 238 | #define AR934X_PORT_VLAN2_8021Q_MODE_VLAN_ONLY 2 |
|---|
| 239 | #define AR934X_PORT_VLAN2_8021Q_MODE_SECURE 3 |
|---|
| 240 | |
|---|
| 241 | #define sw_to_ar7240(_dev) container_of(_dev, struct ar7240sw, swdev) |
|---|
| 242 | |
|---|
| 243 | struct ar7240sw_port_stat { |
|---|
| 244 | unsigned long rx_broadcast; |
|---|
| 245 | unsigned long rx_pause; |
|---|
| 246 | unsigned long rx_multicast; |
|---|
| 247 | unsigned long rx_fcs_error; |
|---|
| 248 | unsigned long rx_align_error; |
|---|
| 249 | unsigned long rx_runt; |
|---|
| 250 | unsigned long rx_fragments; |
|---|
| 251 | unsigned long rx_64byte; |
|---|
| 252 | unsigned long rx_128byte; |
|---|
| 253 | unsigned long rx_256byte; |
|---|
| 254 | unsigned long rx_512byte; |
|---|
| 255 | unsigned long rx_1024byte; |
|---|
| 256 | unsigned long rx_1518byte; |
|---|
| 257 | unsigned long rx_maxbyte; |
|---|
| 258 | unsigned long rx_toolong; |
|---|
| 259 | unsigned long rx_good_byte; |
|---|
| 260 | unsigned long rx_bad_byte; |
|---|
| 261 | unsigned long rx_overflow; |
|---|
| 262 | unsigned long filtered; |
|---|
| 263 | |
|---|
| 264 | unsigned long tx_broadcast; |
|---|
| 265 | unsigned long tx_pause; |
|---|
| 266 | unsigned long tx_multicast; |
|---|
| 267 | unsigned long tx_underrun; |
|---|
| 268 | unsigned long tx_64byte; |
|---|
| 269 | unsigned long tx_128byte; |
|---|
| 270 | unsigned long tx_256byte; |
|---|
| 271 | unsigned long tx_512byte; |
|---|
| 272 | unsigned long tx_1024byte; |
|---|
| 273 | unsigned long tx_1518byte; |
|---|
| 274 | unsigned long tx_maxbyte; |
|---|
| 275 | unsigned long tx_oversize; |
|---|
| 276 | unsigned long tx_byte; |
|---|
| 277 | unsigned long tx_collision; |
|---|
| 278 | unsigned long tx_abortcol; |
|---|
| 279 | unsigned long tx_multicol; |
|---|
| 280 | unsigned long tx_singlecol; |
|---|
| 281 | unsigned long tx_excdefer; |
|---|
| 282 | unsigned long tx_defer; |
|---|
| 283 | unsigned long tx_xlatecol; |
|---|
| 284 | }; |
|---|
| 285 | |
|---|
| 286 | struct ar7240sw { |
|---|
| 287 | struct mii_bus *mii_bus; |
|---|
| 288 | struct ag71xx_switch_platform_data *swdata; |
|---|
| 289 | struct switch_dev swdev; |
|---|
| 290 | int num_ports; |
|---|
| 291 | u8 ver; |
|---|
| 292 | bool vlan; |
|---|
| 293 | bool init; |
|---|
| 294 | u16 vlan_id[AR7240_MAX_VLANS]; |
|---|
| 295 | u8 vlan_table[AR7240_MAX_VLANS]; |
|---|
| 296 | u8 vlan_tagged; |
|---|
| 297 | u16 pvid[AR7240_NUM_PORTS]; |
|---|
| 298 | char buf[80]; |
|---|
| 299 | |
|---|
| 300 | rwlock_t stats_lock; |
|---|
| 301 | struct ar7240sw_port_stat port_stats[AR7240_NUM_PORTS]; |
|---|
| 302 | }; |
|---|
| 303 | |
|---|
| 304 | struct ar7240sw_hw_stat { |
|---|
| 305 | char string[ETH_GSTRING_LEN]; |
|---|
| 306 | int sizeof_stat; |
|---|
| 307 | int reg; |
|---|
| 308 | }; |
|---|
| 309 | |
|---|
| 310 | static DEFINE_MUTEX(reg_mutex); |
|---|
| 311 | |
|---|
| 312 | static inline int sw_is_ar7240(struct ar7240sw *as) |
|---|
| 313 | { |
|---|
| 314 | return as->ver == AR7240_MASK_CTRL_VERSION_AR7240; |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | static inline int sw_is_ar934x(struct ar7240sw *as) |
|---|
| 318 | { |
|---|
| 319 | return as->ver == AR7240_MASK_CTRL_VERSION_AR934X; |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | static inline u32 ar7240sw_port_mask(struct ar7240sw *as, int port) |
|---|
| 323 | { |
|---|
| 324 | return BIT(port); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | static inline u32 ar7240sw_port_mask_all(struct ar7240sw *as) |
|---|
| 328 | { |
|---|
| 329 | return BIT(as->swdev.ports) - 1; |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | static inline u32 ar7240sw_port_mask_but(struct ar7240sw *as, int port) |
|---|
| 333 | { |
|---|
| 334 | return ar7240sw_port_mask_all(as) & ~BIT(port); |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | static inline u16 mk_phy_addr(u32 reg) |
|---|
| 338 | { |
|---|
| 339 | return 0x17 & ((reg >> 4) | 0x10); |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | static inline u16 mk_phy_reg(u32 reg) |
|---|
| 343 | { |
|---|
| 344 | return (reg << 1) & 0x1e; |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | static inline u16 mk_high_addr(u32 reg) |
|---|
| 348 | { |
|---|
| 349 | return (reg >> 7) & 0x1ff; |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | static u32 __ar7240sw_reg_read(struct mii_bus *mii, u32 reg) |
|---|
| 353 | { |
|---|
| 354 | unsigned long flags; |
|---|
| 355 | u16 phy_addr; |
|---|
| 356 | u16 phy_reg; |
|---|
| 357 | u32 hi, lo; |
|---|
| 358 | |
|---|
| 359 | reg = (reg & 0xfffffffc) >> 2; |
|---|
| 360 | phy_addr = mk_phy_addr(reg); |
|---|
| 361 | phy_reg = mk_phy_reg(reg); |
|---|
| 362 | |
|---|
| 363 | local_irq_save(flags); |
|---|
| 364 | ag71xx_mdio_mii_write(mii->priv, 0x1f, 0x10, mk_high_addr(reg)); |
|---|
| 365 | lo = (u32) ag71xx_mdio_mii_read(mii->priv, phy_addr, phy_reg); |
|---|
| 366 | hi = (u32) ag71xx_mdio_mii_read(mii->priv, phy_addr, phy_reg + 1); |
|---|
| 367 | local_irq_restore(flags); |
|---|
| 368 | |
|---|
| 369 | return (hi << 16) | lo; |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | static void __ar7240sw_reg_write(struct mii_bus *mii, u32 reg, u32 val) |
|---|
| 373 | { |
|---|
| 374 | unsigned long flags; |
|---|
| 375 | u16 phy_addr; |
|---|
| 376 | u16 phy_reg; |
|---|
| 377 | |
|---|
| 378 | reg = (reg & 0xfffffffc) >> 2; |
|---|
| 379 | phy_addr = mk_phy_addr(reg); |
|---|
| 380 | phy_reg = mk_phy_reg(reg); |
|---|
| 381 | |
|---|
| 382 | local_irq_save(flags); |
|---|
| 383 | ag71xx_mdio_mii_write(mii->priv, 0x1f, 0x10, mk_high_addr(reg)); |
|---|
| 384 | ag71xx_mdio_mii_write(mii->priv, phy_addr, phy_reg + 1, (val >> 16)); |
|---|
| 385 | ag71xx_mdio_mii_write(mii->priv, phy_addr, phy_reg, (val & 0xffff)); |
|---|
| 386 | local_irq_restore(flags); |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | static u32 ar7240sw_reg_read(struct mii_bus *mii, u32 reg_addr) |
|---|
| 390 | { |
|---|
| 391 | u32 ret; |
|---|
| 392 | |
|---|
| 393 | mutex_lock(®_mutex); |
|---|
| 394 | ret = __ar7240sw_reg_read(mii, reg_addr); |
|---|
| 395 | mutex_unlock(®_mutex); |
|---|
| 396 | |
|---|
| 397 | return ret; |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | static void ar7240sw_reg_write(struct mii_bus *mii, u32 reg_addr, u32 reg_val) |
|---|
| 401 | { |
|---|
| 402 | mutex_lock(®_mutex); |
|---|
| 403 | __ar7240sw_reg_write(mii, reg_addr, reg_val); |
|---|
| 404 | mutex_unlock(®_mutex); |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | static u32 ar7240sw_reg_rmw(struct mii_bus *mii, u32 reg, u32 mask, u32 val) |
|---|
| 408 | { |
|---|
| 409 | u32 t; |
|---|
| 410 | |
|---|
| 411 | mutex_lock(®_mutex); |
|---|
| 412 | t = __ar7240sw_reg_read(mii, reg); |
|---|
| 413 | t &= ~mask; |
|---|
| 414 | t |= val; |
|---|
| 415 | __ar7240sw_reg_write(mii, reg, t); |
|---|
| 416 | mutex_unlock(®_mutex); |
|---|
| 417 | |
|---|
| 418 | return t; |
|---|
| 419 | } |
|---|
| 420 | |
|---|
| 421 | static void ar7240sw_reg_set(struct mii_bus *mii, u32 reg, u32 val) |
|---|
| 422 | { |
|---|
| 423 | u32 t; |
|---|
| 424 | |
|---|
| 425 | mutex_lock(®_mutex); |
|---|
| 426 | t = __ar7240sw_reg_read(mii, reg); |
|---|
| 427 | t |= val; |
|---|
| 428 | __ar7240sw_reg_write(mii, reg, t); |
|---|
| 429 | mutex_unlock(®_mutex); |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | static int __ar7240sw_reg_wait(struct mii_bus *mii, u32 reg, u32 mask, u32 val, |
|---|
| 433 | unsigned timeout) |
|---|
| 434 | { |
|---|
| 435 | int i; |
|---|
| 436 | |
|---|
| 437 | for (i = 0; i < timeout; i++) { |
|---|
| 438 | u32 t; |
|---|
| 439 | |
|---|
| 440 | t = __ar7240sw_reg_read(mii, reg); |
|---|
| 441 | if ((t & mask) == val) |
|---|
| 442 | return 0; |
|---|
| 443 | |
|---|
| 444 | msleep(1); |
|---|
| 445 | } |
|---|
| 446 | |
|---|
| 447 | return -ETIMEDOUT; |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | static int ar7240sw_reg_wait(struct mii_bus *mii, u32 reg, u32 mask, u32 val, |
|---|
| 451 | unsigned timeout) |
|---|
| 452 | { |
|---|
| 453 | int ret; |
|---|
| 454 | |
|---|
| 455 | mutex_lock(®_mutex); |
|---|
| 456 | ret = __ar7240sw_reg_wait(mii, reg, mask, val, timeout); |
|---|
| 457 | mutex_unlock(®_mutex); |
|---|
| 458 | return ret; |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | u16 ar7240sw_phy_read(struct mii_bus *mii, unsigned phy_addr, |
|---|
| 462 | unsigned reg_addr) |
|---|
| 463 | { |
|---|
| 464 | u32 t, val = 0xffff; |
|---|
| 465 | int err; |
|---|
| 466 | |
|---|
| 467 | if (phy_addr >= AR7240_NUM_PHYS) |
|---|
| 468 | return 0xffff; |
|---|
| 469 | |
|---|
| 470 | mutex_lock(®_mutex); |
|---|
| 471 | t = (reg_addr << AR7240_MDIO_CTRL_REG_ADDR_S) | |
|---|
| 472 | (phy_addr << AR7240_MDIO_CTRL_PHY_ADDR_S) | |
|---|
| 473 | AR7240_MDIO_CTRL_MASTER_EN | |
|---|
| 474 | AR7240_MDIO_CTRL_BUSY | |
|---|
| 475 | AR7240_MDIO_CTRL_CMD_READ; |
|---|
| 476 | |
|---|
| 477 | __ar7240sw_reg_write(mii, AR7240_REG_MDIO_CTRL, t); |
|---|
| 478 | err = __ar7240sw_reg_wait(mii, AR7240_REG_MDIO_CTRL, |
|---|
| 479 | AR7240_MDIO_CTRL_BUSY, 0, 5); |
|---|
| 480 | if (!err) |
|---|
| 481 | val = __ar7240sw_reg_read(mii, AR7240_REG_MDIO_CTRL); |
|---|
| 482 | mutex_unlock(®_mutex); |
|---|
| 483 | |
|---|
| 484 | return val & AR7240_MDIO_CTRL_DATA_M; |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| 487 | int ar7240sw_phy_write(struct mii_bus *mii, unsigned phy_addr, |
|---|
| 488 | unsigned reg_addr, u16 reg_val) |
|---|
| 489 | { |
|---|
| 490 | u32 t; |
|---|
| 491 | int ret; |
|---|
| 492 | |
|---|
| 493 | if (phy_addr >= AR7240_NUM_PHYS) |
|---|
| 494 | return -EINVAL; |
|---|
| 495 | |
|---|
| 496 | mutex_lock(®_mutex); |
|---|
| 497 | t = (phy_addr << AR7240_MDIO_CTRL_PHY_ADDR_S) | |
|---|
| 498 | (reg_addr << AR7240_MDIO_CTRL_REG_ADDR_S) | |
|---|
| 499 | AR7240_MDIO_CTRL_MASTER_EN | |
|---|
| 500 | AR7240_MDIO_CTRL_BUSY | |
|---|
| 501 | AR7240_MDIO_CTRL_CMD_WRITE | |
|---|
| 502 | reg_val; |
|---|
| 503 | |
|---|
| 504 | __ar7240sw_reg_write(mii, AR7240_REG_MDIO_CTRL, t); |
|---|
| 505 | ret = __ar7240sw_reg_wait(mii, AR7240_REG_MDIO_CTRL, |
|---|
| 506 | AR7240_MDIO_CTRL_BUSY, 0, 5); |
|---|
| 507 | mutex_unlock(®_mutex); |
|---|
| 508 | |
|---|
| 509 | return ret; |
|---|
| 510 | } |
|---|
| 511 | |
|---|
| 512 | static int ar7240sw_capture_stats(struct ar7240sw *as) |
|---|
| 513 | { |
|---|
| 514 | struct mii_bus *mii = as->mii_bus; |
|---|
| 515 | int port; |
|---|
| 516 | int ret; |
|---|
| 517 | |
|---|
| 518 | write_lock(&as->stats_lock); |
|---|
| 519 | |
|---|
| 520 | /* Capture the hardware statistics for all ports */ |
|---|
| 521 | ar7240sw_reg_write(mii, AR7240_REG_MIB_FUNCTION0, |
|---|
| 522 | (AR7240_MIB_FUNC_CAPTURE << AR7240_MIB_FUNC_S)); |
|---|
| 523 | |
|---|
| 524 | /* Wait for the capturing to complete. */ |
|---|
| 525 | ret = ar7240sw_reg_wait(mii, AR7240_REG_MIB_FUNCTION0, |
|---|
| 526 | AR7240_MIB_BUSY, 0, 10); |
|---|
| 527 | |
|---|
| 528 | if (ret) |
|---|
| 529 | goto unlock; |
|---|
| 530 | |
|---|
| 531 | for (port = 0; port < AR7240_NUM_PORTS; port++) { |
|---|
| 532 | unsigned int base; |
|---|
| 533 | struct ar7240sw_port_stat *stats; |
|---|
| 534 | |
|---|
| 535 | base = AR7240_REG_STATS_BASE(port); |
|---|
| 536 | stats = &as->port_stats[port]; |
|---|
| 537 | |
|---|
| 538 | #define READ_STAT(_r) ar7240sw_reg_read(mii, base + AR7240_STATS_ ## _r) |
|---|
| 539 | |
|---|
| 540 | stats->rx_good_byte += READ_STAT(RXGOODBYTE); |
|---|
| 541 | stats->tx_byte += READ_STAT(TXBYTE); |
|---|
| 542 | |
|---|
| 543 | #undef READ_STAT |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | ret = 0; |
|---|
| 547 | |
|---|
| 548 | unlock: |
|---|
| 549 | write_unlock(&as->stats_lock); |
|---|
| 550 | return ret; |
|---|
| 551 | } |
|---|
| 552 | |
|---|
| 553 | static void ar7240sw_disable_port(struct ar7240sw *as, unsigned port) |
|---|
| 554 | { |
|---|
| 555 | ar7240sw_reg_write(as->mii_bus, AR7240_REG_PORT_CTRL(port), |
|---|
| 556 | AR7240_PORT_CTRL_STATE_DISABLED); |
|---|
| 557 | } |
|---|
| 558 | |
|---|
| 559 | static void ar7240sw_setup(struct ar7240sw *as) |
|---|
| 560 | { |
|---|
| 561 | struct mii_bus *mii = as->mii_bus; |
|---|
| 562 | |
|---|
| 563 | /* Enable CPU port, and disable mirror port */ |
|---|
| 564 | ar7240sw_reg_write(mii, AR7240_REG_CPU_PORT, |
|---|
| 565 | AR7240_CPU_PORT_EN | |
|---|
| 566 | (15 << AR7240_MIRROR_PORT_S)); |
|---|
| 567 | |
|---|
| 568 | /* Setup TAG priority mapping */ |
|---|
| 569 | ar7240sw_reg_write(mii, AR7240_REG_TAG_PRIORITY, 0xfa50); |
|---|
| 570 | |
|---|
| 571 | if (sw_is_ar934x(as)) { |
|---|
| 572 | /* Enable aging, MAC replacing */ |
|---|
| 573 | ar7240sw_reg_write(mii, AR934X_REG_AT_CTRL, |
|---|
| 574 | 0x2b /* 5 min age time */ | |
|---|
| 575 | AR934X_AT_CTRL_AGE_EN | |
|---|
| 576 | AR934X_AT_CTRL_LEARN_CHANGE); |
|---|
| 577 | /* Enable ARP frame acknowledge */ |
|---|
| 578 | ar7240sw_reg_set(mii, AR934X_REG_QM_CTRL, |
|---|
| 579 | AR934X_QM_CTRL_ARP_EN); |
|---|
| 580 | /* Enable Broadcast frames transmitted to the CPU */ |
|---|
| 581 | ar7240sw_reg_set(mii, AR934X_REG_FLOOD_MASK, |
|---|
| 582 | AR934X_FLOOD_MASK_BC_DP(0)); |
|---|
| 583 | } else { |
|---|
| 584 | /* Enable ARP frame acknowledge, aging, MAC replacing */ |
|---|
| 585 | ar7240sw_reg_write(mii, AR7240_REG_AT_CTRL, |
|---|
| 586 | AR7240_AT_CTRL_RESERVED | |
|---|
| 587 | 0x2b /* 5 min age time */ | |
|---|
| 588 | AR7240_AT_CTRL_AGE_EN | |
|---|
| 589 | AR7240_AT_CTRL_ARP_EN | |
|---|
| 590 | AR7240_AT_CTRL_LEARN_CHANGE); |
|---|
| 591 | /* Enable Broadcast frames transmitted to the CPU */ |
|---|
| 592 | ar7240sw_reg_set(mii, AR7240_REG_FLOOD_MASK, |
|---|
| 593 | AR7240_FLOOD_MASK_BROAD_TO_CPU); |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | /* setup MTU */ |
|---|
| 597 | ar7240sw_reg_rmw(mii, AR7240_REG_GLOBAL_CTRL, AR7240_GLOBAL_CTRL_MTU_M, |
|---|
| 598 | 1536); |
|---|
| 599 | |
|---|
| 600 | /* setup Service TAG */ |
|---|
| 601 | ar7240sw_reg_rmw(mii, AR7240_REG_SERVICE_TAG, AR7240_SERVICE_TAG_M, 0); |
|---|
| 602 | } |
|---|
| 603 | |
|---|
| 604 | static int ar7240sw_reset(struct ar7240sw *as) |
|---|
| 605 | { |
|---|
| 606 | struct mii_bus *mii = as->mii_bus; |
|---|
| 607 | int ret; |
|---|
| 608 | int i; |
|---|
| 609 | |
|---|
| 610 | /* Set all ports to disabled state. */ |
|---|
| 611 | for (i = 0; i < AR7240_NUM_PORTS; i++) |
|---|
| 612 | ar7240sw_disable_port(as, i); |
|---|
| 613 | |
|---|
| 614 | /* Wait for transmit queues to drain. */ |
|---|
| 615 | msleep(2); |
|---|
| 616 | |
|---|
| 617 | /* Reset the switch. */ |
|---|
| 618 | ar7240sw_reg_write(mii, AR7240_REG_MASK_CTRL, |
|---|
| 619 | AR7240_MASK_CTRL_SOFT_RESET); |
|---|
| 620 | |
|---|
| 621 | ret = ar7240sw_reg_wait(mii, AR7240_REG_MASK_CTRL, |
|---|
| 622 | AR7240_MASK_CTRL_SOFT_RESET, 0, 1000); |
|---|
| 623 | |
|---|
| 624 | /* setup PHYs */ |
|---|
| 625 | for (i = 0; i < AR7240_NUM_PHYS; i++) { |
|---|
| 626 | ar7240sw_phy_write(mii, i, MII_ADVERTISE, |
|---|
| 627 | ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | |
|---|
| 628 | ADVERTISE_PAUSE_ASYM); |
|---|
| 629 | ar7240sw_phy_write(mii, i, MII_BMCR, |
|---|
| 630 | BMCR_RESET | BMCR_ANENABLE); |
|---|
| 631 | } |
|---|
| 632 | msleep(1000); |
|---|
| 633 | |
|---|
| 634 | ar7240sw_setup(as); |
|---|
| 635 | return ret; |
|---|
| 636 | } |
|---|
| 637 | |
|---|
| 638 | static void ar7240sw_setup_port(struct ar7240sw *as, unsigned port, u8 portmask) |
|---|
| 639 | { |
|---|
| 640 | struct mii_bus *mii = as->mii_bus; |
|---|
| 641 | u32 ctrl; |
|---|
| 642 | u32 vid, mode; |
|---|
| 643 | |
|---|
| 644 | ctrl = AR7240_PORT_CTRL_STATE_FORWARD | AR7240_PORT_CTRL_LEARN | |
|---|
| 645 | AR7240_PORT_CTRL_SINGLE_VLAN; |
|---|
| 646 | |
|---|
| 647 | if (port == AR7240_PORT_CPU) { |
|---|
| 648 | ar7240sw_reg_write(mii, AR7240_REG_PORT_STATUS(port), |
|---|
| 649 | AR7240_PORT_STATUS_SPEED_1000 | |
|---|
| 650 | AR7240_PORT_STATUS_TXFLOW | |
|---|
| 651 | AR7240_PORT_STATUS_RXFLOW | |
|---|
| 652 | AR7240_PORT_STATUS_TXMAC | |
|---|
| 653 | AR7240_PORT_STATUS_RXMAC | |
|---|
| 654 | AR7240_PORT_STATUS_DUPLEX); |
|---|
| 655 | } else { |
|---|
| 656 | ar7240sw_reg_write(mii, AR7240_REG_PORT_STATUS(port), |
|---|
| 657 | AR7240_PORT_STATUS_LINK_AUTO); |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | /* Set the default VID for this port */ |
|---|
| 661 | if (as->vlan) { |
|---|
| 662 | vid = as->vlan_id[as->pvid[port]]; |
|---|
| 663 | mode = AR7240_PORT_VLAN_MODE_SECURE; |
|---|
| 664 | } else { |
|---|
| 665 | vid = port; |
|---|
| 666 | mode = AR7240_PORT_VLAN_MODE_PORT_ONLY; |
|---|
| 667 | } |
|---|
| 668 | |
|---|
| 669 | if (as->vlan) { |
|---|
| 670 | if (as->vlan_tagged & BIT(port)) |
|---|
| 671 | ctrl |= AR7240_PORT_CTRL_VLAN_MODE_ADD << |
|---|
| 672 | AR7240_PORT_CTRL_VLAN_MODE_S; |
|---|
| 673 | else |
|---|
| 674 | ctrl |= AR7240_PORT_CTRL_VLAN_MODE_STRIP << |
|---|
| 675 | AR7240_PORT_CTRL_VLAN_MODE_S; |
|---|
| 676 | } else { |
|---|
| 677 | ctrl |= AR7240_PORT_CTRL_VLAN_MODE_KEEP << |
|---|
| 678 | AR7240_PORT_CTRL_VLAN_MODE_S; |
|---|
| 679 | } |
|---|
| 680 | |
|---|
| 681 | if (!portmask) { |
|---|
| 682 | if (port == AR7240_PORT_CPU) |
|---|
| 683 | portmask = ar7240sw_port_mask_but(as, AR7240_PORT_CPU); |
|---|
| 684 | else |
|---|
| 685 | portmask = ar7240sw_port_mask(as, AR7240_PORT_CPU); |
|---|
| 686 | } |
|---|
| 687 | |
|---|
| 688 | /* allow the port to talk to all other ports, but exclude its |
|---|
| 689 | * own ID to prevent frames from being reflected back to the |
|---|
| 690 | * port that they came from */ |
|---|
| 691 | portmask &= ar7240sw_port_mask_but(as, port); |
|---|
| 692 | |
|---|
| 693 | ar7240sw_reg_write(mii, AR7240_REG_PORT_CTRL(port), ctrl); |
|---|
| 694 | if (sw_is_ar934x(as)) { |
|---|
| 695 | u32 vlan1, vlan2; |
|---|
| 696 | |
|---|
| 697 | vlan1 = (vid << AR934X_PORT_VLAN1_DEFAULT_CVID_S); |
|---|
| 698 | vlan2 = (portmask << AR934X_PORT_VLAN2_PORT_VID_MEM_S) | |
|---|
| 699 | (mode << AR934X_PORT_VLAN2_8021Q_MODE_S); |
|---|
| 700 | ar7240sw_reg_write(mii, AR934X_REG_PORT_VLAN1(port), vlan1); |
|---|
| 701 | ar7240sw_reg_write(mii, AR934X_REG_PORT_VLAN2(port), vlan2); |
|---|
| 702 | } else { |
|---|
| 703 | u32 vlan; |
|---|
| 704 | |
|---|
| 705 | vlan = vid | (mode << AR7240_PORT_VLAN_MODE_S) | |
|---|
| 706 | (portmask << AR7240_PORT_VLAN_DEST_PORTS_S); |
|---|
| 707 | |
|---|
| 708 | ar7240sw_reg_write(mii, AR7240_REG_PORT_VLAN(port), vlan); |
|---|
| 709 | } |
|---|
| 710 | } |
|---|
| 711 | |
|---|
| 712 | static int ar7240_set_addr(struct ar7240sw *as, u8 *addr) |
|---|
| 713 | { |
|---|
| 714 | struct mii_bus *mii = as->mii_bus; |
|---|
| 715 | u32 t; |
|---|
| 716 | |
|---|
| 717 | t = (addr[4] << 8) | addr[5]; |
|---|
| 718 | ar7240sw_reg_write(mii, AR7240_REG_MAC_ADDR0, t); |
|---|
| 719 | |
|---|
| 720 | t = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]; |
|---|
| 721 | ar7240sw_reg_write(mii, AR7240_REG_MAC_ADDR1, t); |
|---|
| 722 | |
|---|
| 723 | return 0; |
|---|
| 724 | } |
|---|
| 725 | |
|---|
| 726 | static int |
|---|
| 727 | ar7240_set_vid(struct switch_dev *dev, const struct switch_attr *attr, |
|---|
| 728 | struct switch_val *val) |
|---|
| 729 | { |
|---|
| 730 | struct ar7240sw *as = sw_to_ar7240(dev); |
|---|
| 731 | as->vlan_id[val->port_vlan] = val->value.i; |
|---|
| 732 | return 0; |
|---|
| 733 | } |
|---|
| 734 | |
|---|
| 735 | static int |
|---|
| 736 | ar7240_get_vid(struct switch_dev *dev, const struct switch_attr *attr, |
|---|
| 737 | struct switch_val *val) |
|---|
| 738 | { |
|---|
| 739 | struct ar7240sw *as = sw_to_ar7240(dev); |
|---|
| 740 | val->value.i = as->vlan_id[val->port_vlan]; |
|---|
| 741 | return 0; |
|---|
| 742 | } |
|---|
| 743 | |
|---|
| 744 | static int |
|---|
| 745 | ar7240_set_pvid(struct switch_dev *dev, int port, int vlan) |
|---|
| 746 | { |
|---|
| 747 | struct ar7240sw *as = sw_to_ar7240(dev); |
|---|
| 748 | |
|---|
| 749 | /* make sure no invalid PVIDs get set */ |
|---|
| 750 | |
|---|
| 751 | if (vlan >= dev->vlans) |
|---|
| 752 | return -EINVAL; |
|---|
| 753 | |
|---|
| 754 | as->pvid[port] = vlan; |
|---|
| 755 | return 0; |
|---|
| 756 | } |
|---|
| 757 | |
|---|
| 758 | static int |
|---|
| 759 | ar7240_get_pvid(struct switch_dev *dev, int port, int *vlan) |
|---|
| 760 | { |
|---|
| 761 | struct ar7240sw *as = sw_to_ar7240(dev); |
|---|
| 762 | *vlan = as->pvid[port]; |
|---|
| 763 | return 0; |
|---|
| 764 | } |
|---|
| 765 | |
|---|
| 766 | static int |
|---|
| 767 | ar7240_get_ports(struct switch_dev *dev, struct switch_val *val) |
|---|
| 768 | { |
|---|
| 769 | struct ar7240sw *as = sw_to_ar7240(dev); |
|---|
| 770 | u8 ports = as->vlan_table[val->port_vlan]; |
|---|
| 771 | int i; |
|---|
| 772 | |
|---|
| 773 | val->len = 0; |
|---|
| 774 | for (i = 0; i < as->swdev.ports; i++) { |
|---|
| 775 | struct switch_port *p; |
|---|
| 776 | |
|---|
| 777 | if (!(ports & (1 << i))) |
|---|
| 778 | continue; |
|---|
| 779 | |
|---|
| 780 | p = &val->value.ports[val->len++]; |
|---|
| 781 | p->id = i; |
|---|
| 782 | if (as->vlan_tagged & (1 << i)) |
|---|
| 783 | p->flags = (1 << SWITCH_PORT_FLAG_TAGGED); |
|---|
| 784 | else |
|---|
| 785 | p->flags = 0; |
|---|
| 786 | } |
|---|
| 787 | return 0; |
|---|
| 788 | } |
|---|
| 789 | |
|---|
| 790 | static int |
|---|
| 791 | ar7240_set_ports(struct switch_dev *dev, struct switch_val *val) |
|---|
| 792 | { |
|---|
| 793 | struct ar7240sw *as = sw_to_ar7240(dev); |
|---|
| 794 | u8 *vt = &as->vlan_table[val->port_vlan]; |
|---|
| 795 | int i, j; |
|---|
| 796 | |
|---|
| 797 | *vt = 0; |
|---|
| 798 | for (i = 0; i < val->len; i++) { |
|---|
| 799 | struct switch_port *p = &val->value.ports[i]; |
|---|
| 800 | |
|---|
| 801 | if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) |
|---|
| 802 | as->vlan_tagged |= (1 << p->id); |
|---|
| 803 | else { |
|---|
| 804 | as->vlan_tagged &= ~(1 << p->id); |
|---|
| 805 | as->pvid[p->id] = val->port_vlan; |
|---|
| 806 | |
|---|
| 807 | /* make sure that an untagged port does not |
|---|
| 808 | * appear in other vlans */ |
|---|
| 809 | for (j = 0; j < AR7240_MAX_VLANS; j++) { |
|---|
| 810 | if (j == val->port_vlan) |
|---|
| 811 | continue; |
|---|
| 812 | as->vlan_table[j] &= ~(1 << p->id); |
|---|
| 813 | } |
|---|
| 814 | } |
|---|
| 815 | |
|---|
| 816 | *vt |= 1 << p->id; |
|---|
| 817 | } |
|---|
| 818 | return 0; |
|---|
| 819 | } |
|---|
| 820 | |
|---|
| 821 | static int |
|---|
| 822 | ar7240_set_vlan(struct switch_dev *dev, const struct switch_attr *attr, |
|---|
| 823 | struct switch_val *val) |
|---|
| 824 | { |
|---|
| 825 | struct ar7240sw *as = sw_to_ar7240(dev); |
|---|
| 826 | as->vlan = !!val->value.i; |
|---|
| 827 | return 0; |
|---|
| 828 | } |
|---|
| 829 | |
|---|
| 830 | static int |
|---|
| 831 | ar7240_get_vlan(struct switch_dev *dev, const struct switch_attr *attr, |
|---|
| 832 | struct switch_val *val) |
|---|
| 833 | { |
|---|
| 834 | struct ar7240sw *as = sw_to_ar7240(dev); |
|---|
| 835 | val->value.i = as->vlan; |
|---|
| 836 | return 0; |
|---|
| 837 | } |
|---|
| 838 | |
|---|
| 839 | static void |
|---|
| 840 | ar7240_vtu_op(struct ar7240sw *as, u32 op, u32 val) |
|---|
| 841 | { |
|---|
| 842 | struct mii_bus *mii = as->mii_bus; |
|---|
| 843 | |
|---|
| 844 | if (ar7240sw_reg_wait(mii, AR7240_REG_VTU, AR7240_VTU_ACTIVE, 0, 5)) |
|---|
| 845 | return; |
|---|
| 846 | |
|---|
| 847 | if ((op & AR7240_VTU_OP) == AR7240_VTU_OP_LOAD) { |
|---|
| 848 | val &= AR7240_VTUDATA_MEMBER; |
|---|
| 849 | val |= AR7240_VTUDATA_VALID; |
|---|
| 850 | ar7240sw_reg_write(mii, AR7240_REG_VTU_DATA, val); |
|---|
| 851 | } |
|---|
| 852 | op |= AR7240_VTU_ACTIVE; |
|---|
| 853 | ar7240sw_reg_write(mii, AR7240_REG_VTU, op); |
|---|
| 854 | } |
|---|
| 855 | |
|---|
| 856 | static int |
|---|
| 857 | ar7240_hw_apply(struct switch_dev *dev) |
|---|
| 858 | { |
|---|
| 859 | struct ar7240sw *as = sw_to_ar7240(dev); |
|---|
| 860 | u8 portmask[AR7240_NUM_PORTS]; |
|---|
| 861 | int i, j; |
|---|
| 862 | |
|---|
| 863 | /* flush all vlan translation unit entries */ |
|---|
| 864 | ar7240_vtu_op(as, AR7240_VTU_OP_FLUSH, 0); |
|---|
| 865 | |
|---|
| 866 | memset(portmask, 0, sizeof(portmask)); |
|---|
| 867 | if (!as->init) { |
|---|
| 868 | /* calculate the port destination masks and load vlans |
|---|
| 869 | * into the vlan translation unit */ |
|---|
| 870 | for (j = 0; j < AR7240_MAX_VLANS; j++) { |
|---|
| 871 | u8 vp = as->vlan_table[j]; |
|---|
| 872 | |
|---|
| 873 | if (!vp) |
|---|
| 874 | continue; |
|---|
| 875 | |
|---|
| 876 | for (i = 0; i < as->swdev.ports; i++) { |
|---|
| 877 | u8 mask = (1 << i); |
|---|
| 878 | if (vp & mask) |
|---|
| 879 | portmask[i] |= vp & ~mask; |
|---|
| 880 | } |
|---|
| 881 | |
|---|
| 882 | ar7240_vtu_op(as, |
|---|
| 883 | AR7240_VTU_OP_LOAD | |
|---|
| 884 | (as->vlan_id[j] << AR7240_VTU_VID_S), |
|---|
| 885 | as->vlan_table[j]); |
|---|
| 886 | } |
|---|
| 887 | } else { |
|---|
| 888 | /* vlan disabled: |
|---|
| 889 | * isolate all ports, but connect them to the cpu port */ |
|---|
| 890 | for (i = 0; i < as->swdev.ports; i++) { |
|---|
| 891 | if (i == AR7240_PORT_CPU) |
|---|
| 892 | continue; |
|---|
| 893 | |
|---|
| 894 | portmask[i] = 1 << AR7240_PORT_CPU; |
|---|
| 895 | portmask[AR7240_PORT_CPU] |= (1 << i); |
|---|
| 896 | } |
|---|
| 897 | } |
|---|
| 898 | |
|---|
| 899 | /* update the port destination mask registers and tag settings */ |
|---|
| 900 | for (i = 0; i < as->swdev.ports; i++) |
|---|
| 901 | ar7240sw_setup_port(as, i, portmask[i]); |
|---|
| 902 | |
|---|
| 903 | return 0; |
|---|
| 904 | } |
|---|
| 905 | |
|---|
| 906 | static int |
|---|
| 907 | ar7240_reset_switch(struct switch_dev *dev) |
|---|
| 908 | { |
|---|
| 909 | struct ar7240sw *as = sw_to_ar7240(dev); |
|---|
| 910 | as->init = false; |
|---|
| 911 | ar7240sw_reset(as); |
|---|
| 912 | return 0; |
|---|
| 913 | } |
|---|
| 914 | |
|---|
| 915 | static int |
|---|
| 916 | ar7240_get_port_link(struct switch_dev *dev, int port, |
|---|
| 917 | struct switch_port_link *link) |
|---|
| 918 | { |
|---|
| 919 | struct ar7240sw *as = sw_to_ar7240(dev); |
|---|
| 920 | struct mii_bus *mii = as->mii_bus; |
|---|
| 921 | u32 status; |
|---|
| 922 | |
|---|
| 923 | if (port > AR7240_NUM_PORTS) |
|---|
| 924 | return -EINVAL; |
|---|
| 925 | |
|---|
| 926 | status = ar7240sw_reg_read(mii, AR7240_REG_PORT_STATUS(port)); |
|---|
| 927 | link->aneg = !!(status & AR7240_PORT_STATUS_LINK_AUTO); |
|---|
| 928 | if (link->aneg) { |
|---|
| 929 | link->link = !!(status & AR7240_PORT_STATUS_LINK_UP); |
|---|
| 930 | if (!link->link) |
|---|
| 931 | return 0; |
|---|
| 932 | } else { |
|---|
| 933 | link->link = true; |
|---|
| 934 | } |
|---|
| 935 | |
|---|
| 936 | link->duplex = !!(status & AR7240_PORT_STATUS_DUPLEX); |
|---|
| 937 | link->tx_flow = !!(status & AR7240_PORT_STATUS_TXFLOW); |
|---|
| 938 | link->rx_flow = !!(status & AR7240_PORT_STATUS_RXFLOW); |
|---|
| 939 | switch (status & AR7240_PORT_STATUS_SPEED_M) { |
|---|
| 940 | case AR7240_PORT_STATUS_SPEED_10: |
|---|
| 941 | link->speed = SWITCH_PORT_SPEED_10; |
|---|
| 942 | break; |
|---|
| 943 | case AR7240_PORT_STATUS_SPEED_100: |
|---|
| 944 | link->speed = SWITCH_PORT_SPEED_100; |
|---|
| 945 | break; |
|---|
| 946 | case AR7240_PORT_STATUS_SPEED_1000: |
|---|
| 947 | link->speed = SWITCH_PORT_SPEED_1000; |
|---|
| 948 | break; |
|---|
| 949 | } |
|---|
| 950 | |
|---|
| 951 | return 0; |
|---|
| 952 | } |
|---|
| 953 | |
|---|
| 954 | static int |
|---|
| 955 | ar7240_get_port_stats(struct switch_dev *dev, int port, |
|---|
| 956 | struct switch_port_stats *stats) |
|---|
| 957 | { |
|---|
| 958 | struct ar7240sw *as = sw_to_ar7240(dev); |
|---|
| 959 | |
|---|
| 960 | if (port > AR7240_NUM_PORTS) |
|---|
| 961 | return -EINVAL; |
|---|
| 962 | |
|---|
| 963 | ar7240sw_capture_stats(as); |
|---|
| 964 | |
|---|
| 965 | read_lock(&as->stats_lock); |
|---|
| 966 | stats->rx_bytes = as->port_stats[port].rx_good_byte; |
|---|
| 967 | stats->tx_bytes = as->port_stats[port].tx_byte; |
|---|
| 968 | read_unlock(&as->stats_lock); |
|---|
| 969 | |
|---|
| 970 | return 0; |
|---|
| 971 | } |
|---|
| 972 | |
|---|
| 973 | static struct switch_attr ar7240_globals[] = { |
|---|
| 974 | { |
|---|
| 975 | .type = SWITCH_TYPE_INT, |
|---|
| 976 | .name = "enable_vlan", |
|---|
| 977 | .description = "Enable VLAN mode", |
|---|
| 978 | .set = ar7240_set_vlan, |
|---|
| 979 | .get = ar7240_get_vlan, |
|---|
| 980 | .max = 1 |
|---|
| 981 | }, |
|---|
| 982 | }; |
|---|
| 983 | |
|---|
| 984 | static struct switch_attr ar7240_port[] = { |
|---|
| 985 | }; |
|---|
| 986 | |
|---|
| 987 | static struct switch_attr ar7240_vlan[] = { |
|---|
| 988 | { |
|---|
| 989 | .type = SWITCH_TYPE_INT, |
|---|
| 990 | .name = "vid", |
|---|
| 991 | .description = "VLAN ID", |
|---|
| 992 | .set = ar7240_set_vid, |
|---|
| 993 | .get = ar7240_get_vid, |
|---|
| 994 | .max = 4094, |
|---|
| 995 | }, |
|---|
| 996 | }; |
|---|
| 997 | |
|---|
| 998 | static const struct switch_dev_ops ar7240_ops = { |
|---|
| 999 | .attr_global = { |
|---|
| 1000 | .attr = ar7240_globals, |
|---|
| 1001 | .n_attr = ARRAY_SIZE(ar7240_globals), |
|---|
| 1002 | }, |
|---|
| 1003 | .attr_port = { |
|---|
| 1004 | .attr = ar7240_port, |
|---|
| 1005 | .n_attr = ARRAY_SIZE(ar7240_port), |
|---|
| 1006 | }, |
|---|
| 1007 | .attr_vlan = { |
|---|
| 1008 | .attr = ar7240_vlan, |
|---|
| 1009 | .n_attr = ARRAY_SIZE(ar7240_vlan), |
|---|
| 1010 | }, |
|---|
| 1011 | .get_port_pvid = ar7240_get_pvid, |
|---|
| 1012 | .set_port_pvid = ar7240_set_pvid, |
|---|
| 1013 | .get_vlan_ports = ar7240_get_ports, |
|---|
| 1014 | .set_vlan_ports = ar7240_set_ports, |
|---|
| 1015 | .apply_config = ar7240_hw_apply, |
|---|
| 1016 | .reset_switch = ar7240_reset_switch, |
|---|
| 1017 | .get_port_link = ar7240_get_port_link, |
|---|
| 1018 | .get_port_stats = ar7240_get_port_stats, |
|---|
| 1019 | }; |
|---|
| 1020 | |
|---|
| 1021 | static struct ar7240sw *ar7240_probe(struct ag71xx *ag) |
|---|
| 1022 | { |
|---|
| 1023 | struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag); |
|---|
| 1024 | struct mii_bus *mii = ag->mii_bus; |
|---|
| 1025 | struct ar7240sw *as; |
|---|
| 1026 | struct switch_dev *swdev; |
|---|
| 1027 | u32 ctrl; |
|---|
| 1028 | u16 phy_id1; |
|---|
| 1029 | u16 phy_id2; |
|---|
| 1030 | int i; |
|---|
| 1031 | |
|---|
| 1032 | phy_id1 = ar7240sw_phy_read(mii, 0, MII_PHYSID1); |
|---|
| 1033 | phy_id2 = ar7240sw_phy_read(mii, 0, MII_PHYSID2); |
|---|
| 1034 | if ((phy_id1 != AR7240_PHY_ID1 || phy_id2 != AR7240_PHY_ID2) && |
|---|
| 1035 | (phy_id1 != AR934X_PHY_ID1 || phy_id2 != AR934X_PHY_ID2)) { |
|---|
| 1036 | pr_err("%s: unknown phy id '%04x:%04x'\n", |
|---|
| 1037 | ag->dev->name, phy_id1, phy_id2); |
|---|
| 1038 | return NULL; |
|---|
| 1039 | } |
|---|
| 1040 | |
|---|
| 1041 | as = kzalloc(sizeof(*as), GFP_KERNEL); |
|---|
| 1042 | if (!as) |
|---|
| 1043 | return NULL; |
|---|
| 1044 | |
|---|
| 1045 | as->mii_bus = mii; |
|---|
| 1046 | as->swdata = pdata->switch_data; |
|---|
| 1047 | |
|---|
| 1048 | swdev = &as->swdev; |
|---|
| 1049 | |
|---|
| 1050 | ctrl = ar7240sw_reg_read(mii, AR7240_REG_MASK_CTRL); |
|---|
| 1051 | as->ver = (ctrl >> AR7240_MASK_CTRL_VERSION_S) & |
|---|
| 1052 | AR7240_MASK_CTRL_VERSION_M; |
|---|
| 1053 | |
|---|
| 1054 | if (sw_is_ar7240(as)) { |
|---|
| 1055 | swdev->name = "AR7240/AR9330 built-in switch"; |
|---|
| 1056 | swdev->ports = AR7240_NUM_PORTS - 1; |
|---|
| 1057 | } else if (sw_is_ar934x(as)) { |
|---|
| 1058 | swdev->name = "AR934X built-in switch"; |
|---|
| 1059 | |
|---|
| 1060 | if (pdata->phy_if_mode == PHY_INTERFACE_MODE_GMII) { |
|---|
| 1061 | ar7240sw_reg_set(mii, AR934X_REG_OPER_MODE0, |
|---|
| 1062 | AR934X_OPER_MODE0_MAC_GMII_EN); |
|---|
| 1063 | } else if (pdata->phy_if_mode == PHY_INTERFACE_MODE_MII) { |
|---|
| 1064 | ar7240sw_reg_set(mii, AR934X_REG_OPER_MODE0, |
|---|
| 1065 | AR934X_OPER_MODE0_PHY_MII_EN); |
|---|
| 1066 | } else { |
|---|
| 1067 | pr_err("%s: invalid PHY interface mode\n", |
|---|
| 1068 | ag->dev->name); |
|---|
| 1069 | goto err_free; |
|---|
| 1070 | } |
|---|
| 1071 | |
|---|
| 1072 | if (as->swdata->phy4_mii_en) { |
|---|
| 1073 | ar7240sw_reg_set(mii, AR934X_REG_OPER_MODE1, |
|---|
| 1074 | AR934X_REG_OPER_MODE1_PHY4_MII_EN); |
|---|
| 1075 | swdev->ports = AR7240_NUM_PORTS - 1; |
|---|
| 1076 | } else { |
|---|
| 1077 | swdev->ports = AR7240_NUM_PORTS; |
|---|
| 1078 | } |
|---|
| 1079 | } else { |
|---|
| 1080 | pr_err("%s: unsupported chip, ctrl=%08x\n", |
|---|
| 1081 | ag->dev->name, ctrl); |
|---|
| 1082 | goto err_free; |
|---|
| 1083 | } |
|---|
| 1084 | |
|---|
| 1085 | swdev->cpu_port = AR7240_PORT_CPU; |
|---|
| 1086 | swdev->vlans = AR7240_MAX_VLANS; |
|---|
| 1087 | swdev->ops = &ar7240_ops; |
|---|
| 1088 | |
|---|
| 1089 | if (register_switch(&as->swdev, ag->dev) < 0) |
|---|
| 1090 | goto err_free; |
|---|
| 1091 | |
|---|
| 1092 | pr_info("%s: Found an %s\n", ag->dev->name, swdev->name); |
|---|
| 1093 | |
|---|
| 1094 | /* initialize defaults */ |
|---|
| 1095 | for (i = 0; i < AR7240_MAX_VLANS; i++) |
|---|
| 1096 | as->vlan_id[i] = i; |
|---|
| 1097 | |
|---|
| 1098 | as->vlan_table[0] = ar7240sw_port_mask_all(as); |
|---|
| 1099 | as->init = true; |
|---|
| 1100 | |
|---|
| 1101 | return as; |
|---|
| 1102 | |
|---|
| 1103 | err_free: |
|---|
| 1104 | kfree(as); |
|---|
| 1105 | return NULL; |
|---|
| 1106 | } |
|---|
| 1107 | |
|---|
| 1108 | static void link_function(struct work_struct *work) { |
|---|
| 1109 | struct ag71xx *ag = container_of(work, struct ag71xx, link_work.work); |
|---|
| 1110 | struct ar7240sw *as = ag->phy_priv; |
|---|
| 1111 | unsigned long flags; |
|---|
| 1112 | int i; |
|---|
| 1113 | int status = 0; |
|---|
| 1114 | |
|---|
| 1115 | for (i = 0; i < as->swdev.ports; i++) { |
|---|
| 1116 | int link = ar7240sw_phy_read(ag->mii_bus, i, MII_BMSR); |
|---|
| 1117 | if(link & BMSR_LSTATUS) { |
|---|
| 1118 | status = 1; |
|---|
| 1119 | break; |
|---|
| 1120 | } |
|---|
| 1121 | } |
|---|
| 1122 | |
|---|
| 1123 | spin_lock_irqsave(&ag->lock, flags); |
|---|
| 1124 | if(status != ag->link) { |
|---|
| 1125 | ag->link = status; |
|---|
| 1126 | ag71xx_link_adjust(ag); |
|---|
| 1127 | } |
|---|
| 1128 | spin_unlock_irqrestore(&ag->lock, flags); |
|---|
| 1129 | |
|---|
| 1130 | schedule_delayed_work(&ag->link_work, HZ / 2); |
|---|
| 1131 | } |
|---|
| 1132 | |
|---|
| 1133 | void ag71xx_ar7240_start(struct ag71xx *ag) |
|---|
| 1134 | { |
|---|
| 1135 | struct ar7240sw *as = ag->phy_priv; |
|---|
| 1136 | |
|---|
| 1137 | ar7240sw_reset(as); |
|---|
| 1138 | |
|---|
| 1139 | ag->speed = SPEED_1000; |
|---|
| 1140 | ag->duplex = 1; |
|---|
| 1141 | |
|---|
| 1142 | ar7240_set_addr(as, ag->dev->dev_addr); |
|---|
| 1143 | ar7240_hw_apply(&as->swdev); |
|---|
| 1144 | |
|---|
| 1145 | schedule_delayed_work(&ag->link_work, HZ / 10); |
|---|
| 1146 | } |
|---|
| 1147 | |
|---|
| 1148 | void ag71xx_ar7240_stop(struct ag71xx *ag) |
|---|
| 1149 | { |
|---|
| 1150 | cancel_delayed_work_sync(&ag->link_work); |
|---|
| 1151 | } |
|---|
| 1152 | |
|---|
| 1153 | int __devinit ag71xx_ar7240_init(struct ag71xx *ag) |
|---|
| 1154 | { |
|---|
| 1155 | struct ar7240sw *as; |
|---|
| 1156 | |
|---|
| 1157 | as = ar7240_probe(ag); |
|---|
| 1158 | if (!as) |
|---|
| 1159 | return -ENODEV; |
|---|
| 1160 | |
|---|
| 1161 | ag->phy_priv = as; |
|---|
| 1162 | ar7240sw_reset(as); |
|---|
| 1163 | |
|---|
| 1164 | rwlock_init(&as->stats_lock); |
|---|
| 1165 | INIT_DELAYED_WORK(&ag->link_work, link_function); |
|---|
| 1166 | |
|---|
| 1167 | return 0; |
|---|
| 1168 | } |
|---|
| 1169 | |
|---|
| 1170 | void ag71xx_ar7240_cleanup(struct ag71xx *ag) |
|---|
| 1171 | { |
|---|
| 1172 | struct ar7240sw *as = ag->phy_priv; |
|---|
| 1173 | |
|---|
| 1174 | if (!as) |
|---|
| 1175 | return; |
|---|
| 1176 | |
|---|
| 1177 | unregister_switch(&as->swdev); |
|---|
| 1178 | kfree(as); |
|---|
| 1179 | ag->phy_priv = NULL; |
|---|
| 1180 | } |
|---|