Florian Fainelli | 967dd82 | 2016-06-09 18:23:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * B53 common definitions |
| 3 | * |
| 4 | * Copyright (C) 2011-2013 Jonas Gorski <jogo@openwrt.org> |
| 5 | * |
| 6 | * Permission to use, copy, modify, and/or distribute this software for any |
| 7 | * purpose with or without fee is hereby granted, provided that the above |
| 8 | * copyright notice and this permission notice appear in all copies. |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | */ |
| 18 | |
| 19 | #ifndef __B53_PRIV_H |
| 20 | #define __B53_PRIV_H |
| 21 | |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/mutex.h> |
| 24 | #include <linux/phy.h> |
| 25 | #include <net/dsa.h> |
| 26 | |
Florian Fainelli | 1da6df8 | 2016-06-09 18:23:55 -0700 | [diff] [blame] | 27 | #include "b53_regs.h" |
| 28 | |
Florian Fainelli | 967dd82 | 2016-06-09 18:23:53 -0700 | [diff] [blame] | 29 | struct b53_device; |
Florian Fainelli | ff39c2d | 2016-06-09 18:23:56 -0700 | [diff] [blame] | 30 | struct net_device; |
Florian Fainelli | 967dd82 | 2016-06-09 18:23:53 -0700 | [diff] [blame] | 31 | |
| 32 | struct b53_io_ops { |
| 33 | int (*read8)(struct b53_device *dev, u8 page, u8 reg, u8 *value); |
| 34 | int (*read16)(struct b53_device *dev, u8 page, u8 reg, u16 *value); |
| 35 | int (*read32)(struct b53_device *dev, u8 page, u8 reg, u32 *value); |
| 36 | int (*read48)(struct b53_device *dev, u8 page, u8 reg, u64 *value); |
| 37 | int (*read64)(struct b53_device *dev, u8 page, u8 reg, u64 *value); |
| 38 | int (*write8)(struct b53_device *dev, u8 page, u8 reg, u8 value); |
| 39 | int (*write16)(struct b53_device *dev, u8 page, u8 reg, u16 value); |
| 40 | int (*write32)(struct b53_device *dev, u8 page, u8 reg, u32 value); |
| 41 | int (*write48)(struct b53_device *dev, u8 page, u8 reg, u64 value); |
| 42 | int (*write64)(struct b53_device *dev, u8 page, u8 reg, u64 value); |
| 43 | int (*phy_read16)(struct b53_device *dev, int addr, int reg, u16 *value); |
| 44 | int (*phy_write16)(struct b53_device *dev, int addr, int reg, u16 value); |
| 45 | }; |
| 46 | |
| 47 | enum { |
| 48 | BCM5325_DEVICE_ID = 0x25, |
| 49 | BCM5365_DEVICE_ID = 0x65, |
| 50 | BCM5395_DEVICE_ID = 0x95, |
| 51 | BCM5397_DEVICE_ID = 0x97, |
| 52 | BCM5398_DEVICE_ID = 0x98, |
| 53 | BCM53115_DEVICE_ID = 0x53115, |
| 54 | BCM53125_DEVICE_ID = 0x53125, |
| 55 | BCM53128_DEVICE_ID = 0x53128, |
| 56 | BCM63XX_DEVICE_ID = 0x6300, |
| 57 | BCM53010_DEVICE_ID = 0x53010, |
| 58 | BCM53011_DEVICE_ID = 0x53011, |
| 59 | BCM53012_DEVICE_ID = 0x53012, |
| 60 | BCM53018_DEVICE_ID = 0x53018, |
| 61 | BCM53019_DEVICE_ID = 0x53019, |
Florian Fainelli | 991a36b | 2016-07-08 11:39:13 -0700 | [diff] [blame] | 62 | BCM58XX_DEVICE_ID = 0x5800, |
Florian Fainelli | 967dd82 | 2016-06-09 18:23:53 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | #define B53_N_PORTS 9 |
| 66 | #define B53_N_PORTS_25 6 |
| 67 | |
| 68 | struct b53_port { |
Florian Fainelli | ff39c2d | 2016-06-09 18:23:56 -0700 | [diff] [blame] | 69 | u16 vlan_ctl_mask; |
| 70 | struct net_device *bridge_dev; |
Florian Fainelli | 967dd82 | 2016-06-09 18:23:53 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Florian Fainelli | a2482d2 | 2016-06-09 18:23:57 -0700 | [diff] [blame] | 73 | struct b53_vlan { |
| 74 | u16 members; |
| 75 | u16 untag; |
| 76 | bool valid; |
| 77 | }; |
| 78 | |
Florian Fainelli | 967dd82 | 2016-06-09 18:23:53 -0700 | [diff] [blame] | 79 | struct b53_device { |
| 80 | struct dsa_switch *ds; |
| 81 | struct b53_platform_data *pdata; |
| 82 | const char *name; |
| 83 | |
| 84 | struct mutex reg_mutex; |
| 85 | struct mutex stats_mutex; |
| 86 | const struct b53_io_ops *ops; |
| 87 | |
| 88 | /* chip specific data */ |
| 89 | u32 chip_id; |
| 90 | u8 core_rev; |
| 91 | u8 vta_regs[3]; |
| 92 | u8 duplex_reg; |
| 93 | u8 jumbo_pm_reg; |
| 94 | u8 jumbo_size_reg; |
| 95 | int reset_gpio; |
Florian Fainelli | 1da6df8 | 2016-06-09 18:23:55 -0700 | [diff] [blame] | 96 | u8 num_arl_entries; |
Florian Fainelli | 967dd82 | 2016-06-09 18:23:53 -0700 | [diff] [blame] | 97 | |
| 98 | /* used ports mask */ |
| 99 | u16 enabled_ports; |
| 100 | unsigned int cpu_port; |
| 101 | |
| 102 | /* connect specific data */ |
| 103 | u8 current_page; |
| 104 | struct device *dev; |
| 105 | |
| 106 | /* Master MDIO bus we got probed from */ |
| 107 | struct mii_bus *bus; |
| 108 | |
Florian Fainelli | 967dd82 | 2016-06-09 18:23:53 -0700 | [diff] [blame] | 109 | void *priv; |
| 110 | |
| 111 | /* run time configuration */ |
Florian Fainelli | a2482d2 | 2016-06-09 18:23:57 -0700 | [diff] [blame] | 112 | bool enable_jumbo; |
| 113 | |
Florian Fainelli | 967dd82 | 2016-06-09 18:23:53 -0700 | [diff] [blame] | 114 | unsigned int num_vlans; |
Florian Fainelli | a2482d2 | 2016-06-09 18:23:57 -0700 | [diff] [blame] | 115 | struct b53_vlan *vlans; |
Florian Fainelli | 967dd82 | 2016-06-09 18:23:53 -0700 | [diff] [blame] | 116 | unsigned int num_ports; |
| 117 | struct b53_port *ports; |
| 118 | }; |
| 119 | |
| 120 | #define b53_for_each_port(dev, i) \ |
| 121 | for (i = 0; i < B53_N_PORTS; i++) \ |
| 122 | if (dev->enabled_ports & BIT(i)) |
| 123 | |
| 124 | |
| 125 | static inline int is5325(struct b53_device *dev) |
| 126 | { |
| 127 | return dev->chip_id == BCM5325_DEVICE_ID; |
| 128 | } |
| 129 | |
| 130 | static inline int is5365(struct b53_device *dev) |
| 131 | { |
| 132 | #ifdef CONFIG_BCM47XX |
| 133 | return dev->chip_id == BCM5365_DEVICE_ID; |
| 134 | #else |
| 135 | return 0; |
| 136 | #endif |
| 137 | } |
| 138 | |
| 139 | static inline int is5397_98(struct b53_device *dev) |
| 140 | { |
| 141 | return dev->chip_id == BCM5397_DEVICE_ID || |
| 142 | dev->chip_id == BCM5398_DEVICE_ID; |
| 143 | } |
| 144 | |
| 145 | static inline int is539x(struct b53_device *dev) |
| 146 | { |
| 147 | return dev->chip_id == BCM5395_DEVICE_ID || |
| 148 | dev->chip_id == BCM5397_DEVICE_ID || |
| 149 | dev->chip_id == BCM5398_DEVICE_ID; |
| 150 | } |
| 151 | |
| 152 | static inline int is531x5(struct b53_device *dev) |
| 153 | { |
| 154 | return dev->chip_id == BCM53115_DEVICE_ID || |
| 155 | dev->chip_id == BCM53125_DEVICE_ID || |
| 156 | dev->chip_id == BCM53128_DEVICE_ID; |
| 157 | } |
| 158 | |
| 159 | static inline int is63xx(struct b53_device *dev) |
| 160 | { |
| 161 | #ifdef CONFIG_BCM63XX |
| 162 | return dev->chip_id == BCM63XX_DEVICE_ID; |
| 163 | #else |
| 164 | return 0; |
| 165 | #endif |
| 166 | } |
| 167 | |
| 168 | static inline int is5301x(struct b53_device *dev) |
| 169 | { |
| 170 | return dev->chip_id == BCM53010_DEVICE_ID || |
| 171 | dev->chip_id == BCM53011_DEVICE_ID || |
| 172 | dev->chip_id == BCM53012_DEVICE_ID || |
| 173 | dev->chip_id == BCM53018_DEVICE_ID || |
| 174 | dev->chip_id == BCM53019_DEVICE_ID; |
| 175 | } |
| 176 | |
| 177 | #define B53_CPU_PORT_25 5 |
| 178 | #define B53_CPU_PORT 8 |
| 179 | |
| 180 | static inline int is_cpu_port(struct b53_device *dev, int port) |
| 181 | { |
| 182 | return dev->cpu_port; |
| 183 | } |
| 184 | |
Julia Lawall | 0dff88d | 2016-08-09 19:09:45 +0200 | [diff] [blame^] | 185 | struct b53_device *b53_switch_alloc(struct device *base, |
| 186 | const struct b53_io_ops *ops, |
Florian Fainelli | 967dd82 | 2016-06-09 18:23:53 -0700 | [diff] [blame] | 187 | void *priv); |
| 188 | |
| 189 | int b53_switch_detect(struct b53_device *dev); |
| 190 | |
| 191 | int b53_switch_register(struct b53_device *dev); |
| 192 | |
| 193 | static inline void b53_switch_remove(struct b53_device *dev) |
| 194 | { |
| 195 | dsa_unregister_switch(dev->ds); |
| 196 | } |
| 197 | |
| 198 | static inline int b53_read8(struct b53_device *dev, u8 page, u8 reg, u8 *val) |
| 199 | { |
| 200 | int ret; |
| 201 | |
| 202 | mutex_lock(&dev->reg_mutex); |
| 203 | ret = dev->ops->read8(dev, page, reg, val); |
| 204 | mutex_unlock(&dev->reg_mutex); |
| 205 | |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | static inline int b53_read16(struct b53_device *dev, u8 page, u8 reg, u16 *val) |
| 210 | { |
| 211 | int ret; |
| 212 | |
| 213 | mutex_lock(&dev->reg_mutex); |
| 214 | ret = dev->ops->read16(dev, page, reg, val); |
| 215 | mutex_unlock(&dev->reg_mutex); |
| 216 | |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | static inline int b53_read32(struct b53_device *dev, u8 page, u8 reg, u32 *val) |
| 221 | { |
| 222 | int ret; |
| 223 | |
| 224 | mutex_lock(&dev->reg_mutex); |
| 225 | ret = dev->ops->read32(dev, page, reg, val); |
| 226 | mutex_unlock(&dev->reg_mutex); |
| 227 | |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | static inline int b53_read48(struct b53_device *dev, u8 page, u8 reg, u64 *val) |
| 232 | { |
| 233 | int ret; |
| 234 | |
| 235 | mutex_lock(&dev->reg_mutex); |
| 236 | ret = dev->ops->read48(dev, page, reg, val); |
| 237 | mutex_unlock(&dev->reg_mutex); |
| 238 | |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | static inline int b53_read64(struct b53_device *dev, u8 page, u8 reg, u64 *val) |
| 243 | { |
| 244 | int ret; |
| 245 | |
| 246 | mutex_lock(&dev->reg_mutex); |
| 247 | ret = dev->ops->read64(dev, page, reg, val); |
| 248 | mutex_unlock(&dev->reg_mutex); |
| 249 | |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | static inline int b53_write8(struct b53_device *dev, u8 page, u8 reg, u8 value) |
| 254 | { |
| 255 | int ret; |
| 256 | |
| 257 | mutex_lock(&dev->reg_mutex); |
| 258 | ret = dev->ops->write8(dev, page, reg, value); |
| 259 | mutex_unlock(&dev->reg_mutex); |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | static inline int b53_write16(struct b53_device *dev, u8 page, u8 reg, |
| 265 | u16 value) |
| 266 | { |
| 267 | int ret; |
| 268 | |
| 269 | mutex_lock(&dev->reg_mutex); |
| 270 | ret = dev->ops->write16(dev, page, reg, value); |
| 271 | mutex_unlock(&dev->reg_mutex); |
| 272 | |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | static inline int b53_write32(struct b53_device *dev, u8 page, u8 reg, |
| 277 | u32 value) |
| 278 | { |
| 279 | int ret; |
| 280 | |
| 281 | mutex_lock(&dev->reg_mutex); |
| 282 | ret = dev->ops->write32(dev, page, reg, value); |
| 283 | mutex_unlock(&dev->reg_mutex); |
| 284 | |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | static inline int b53_write48(struct b53_device *dev, u8 page, u8 reg, |
| 289 | u64 value) |
| 290 | { |
| 291 | int ret; |
| 292 | |
| 293 | mutex_lock(&dev->reg_mutex); |
| 294 | ret = dev->ops->write48(dev, page, reg, value); |
| 295 | mutex_unlock(&dev->reg_mutex); |
| 296 | |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | static inline int b53_write64(struct b53_device *dev, u8 page, u8 reg, |
| 301 | u64 value) |
| 302 | { |
| 303 | int ret; |
| 304 | |
| 305 | mutex_lock(&dev->reg_mutex); |
| 306 | ret = dev->ops->write64(dev, page, reg, value); |
| 307 | mutex_unlock(&dev->reg_mutex); |
| 308 | |
| 309 | return ret; |
| 310 | } |
| 311 | |
Florian Fainelli | 1da6df8 | 2016-06-09 18:23:55 -0700 | [diff] [blame] | 312 | struct b53_arl_entry { |
| 313 | u8 port; |
| 314 | u8 mac[ETH_ALEN]; |
| 315 | u16 vid; |
| 316 | u8 is_valid:1; |
| 317 | u8 is_age:1; |
| 318 | u8 is_static:1; |
| 319 | }; |
| 320 | |
| 321 | static inline void b53_mac_from_u64(u64 src, u8 *dst) |
| 322 | { |
| 323 | unsigned int i; |
| 324 | |
| 325 | for (i = 0; i < ETH_ALEN; i++) |
| 326 | dst[ETH_ALEN - 1 - i] = (src >> (8 * i)) & 0xff; |
| 327 | } |
| 328 | |
| 329 | static inline u64 b53_mac_to_u64(const u8 *src) |
| 330 | { |
| 331 | unsigned int i; |
| 332 | u64 dst = 0; |
| 333 | |
| 334 | for (i = 0; i < ETH_ALEN; i++) |
| 335 | dst |= (u64)src[ETH_ALEN - 1 - i] << (8 * i); |
| 336 | |
| 337 | return dst; |
| 338 | } |
| 339 | |
| 340 | static inline void b53_arl_to_entry(struct b53_arl_entry *ent, |
| 341 | u64 mac_vid, u32 fwd_entry) |
| 342 | { |
| 343 | memset(ent, 0, sizeof(*ent)); |
| 344 | ent->port = fwd_entry & ARLTBL_DATA_PORT_ID_MASK; |
| 345 | ent->is_valid = !!(fwd_entry & ARLTBL_VALID); |
| 346 | ent->is_age = !!(fwd_entry & ARLTBL_AGE); |
| 347 | ent->is_static = !!(fwd_entry & ARLTBL_STATIC); |
| 348 | b53_mac_from_u64(mac_vid, ent->mac); |
| 349 | ent->vid = mac_vid >> ARLTBL_VID_S; |
| 350 | } |
| 351 | |
| 352 | static inline void b53_arl_from_entry(u64 *mac_vid, u32 *fwd_entry, |
| 353 | const struct b53_arl_entry *ent) |
| 354 | { |
| 355 | *mac_vid = b53_mac_to_u64(ent->mac); |
| 356 | *mac_vid |= (u64)(ent->vid & ARLTBL_VID_MASK) << ARLTBL_VID_S; |
| 357 | *fwd_entry = ent->port & ARLTBL_DATA_PORT_ID_MASK; |
| 358 | if (ent->is_valid) |
| 359 | *fwd_entry |= ARLTBL_VALID; |
| 360 | if (ent->is_static) |
| 361 | *fwd_entry |= ARLTBL_STATIC; |
| 362 | if (ent->is_age) |
| 363 | *fwd_entry |= ARLTBL_AGE; |
| 364 | } |
| 365 | |
Florian Fainelli | 967dd82 | 2016-06-09 18:23:53 -0700 | [diff] [blame] | 366 | #ifdef CONFIG_BCM47XX |
| 367 | |
| 368 | #include <linux/version.h> |
| 369 | #include <linux/bcm47xx_nvram.h> |
| 370 | #include <bcm47xx_board.h> |
| 371 | static inline int b53_switch_get_reset_gpio(struct b53_device *dev) |
| 372 | { |
| 373 | enum bcm47xx_board board = bcm47xx_board_get(); |
| 374 | |
| 375 | switch (board) { |
| 376 | case BCM47XX_BOARD_LINKSYS_WRT300NV11: |
| 377 | case BCM47XX_BOARD_LINKSYS_WRT310NV1: |
| 378 | return 8; |
| 379 | default: |
| 380 | return bcm47xx_nvram_gpio_pin("robo_reset"); |
| 381 | } |
| 382 | } |
| 383 | #else |
| 384 | static inline int b53_switch_get_reset_gpio(struct b53_device *dev) |
| 385 | { |
| 386 | return -ENOENT; |
| 387 | } |
| 388 | #endif |
| 389 | #endif |