1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Microchip switch driver common header
4 * Copyright (C) 2017-2019 Microchip Technology Inc.
10 #include <linux/etherdevice.h>
11 #include <linux/kernel.h>
12 #include <linux/mutex.h>
13 #include <linux/phy.h>
14 #include <linux/regmap.h>
16 #include <linux/irq.h>
18 #define KSZ_MAX_NUM_PORTS 8
27 struct mutex cnt_mutex; /* structure access */
30 struct rtnl_link_stats64 stats64;
31 struct ethtool_pause_stats pause_stats;
32 struct spinlock stats64_lock;
35 struct ksz_mib_names {
37 char string[ETH_GSTRING_LEN];
40 struct ksz_chip_data {
48 const struct ksz_dev_ops *ops;
50 bool ksz87xx_eee_link_erratum;
51 const struct ksz_mib_names *mib_names;
60 int broadcast_ctrl_reg;
61 int multicast_ctrl_reg;
63 bool supports_mii[KSZ_MAX_NUM_PORTS];
64 bool supports_rmii[KSZ_MAX_NUM_PORTS];
65 bool supports_rgmii[KSZ_MAX_NUM_PORTS];
66 bool internal_phy[KSZ_MAX_NUM_PORTS];
67 bool gbit_capable[KSZ_MAX_NUM_PORTS];
68 const struct regmap_access_table *wr_table;
69 const struct regmap_access_table *rd_table;
75 struct irq_domain *domain;
81 bool remove_tag; /* Remove Tag flag set, for ksz8795 only */
84 struct phy_device phydev;
86 u32 on:1; /* port is not disabled by hardware */
87 u32 fiber:1; /* port is fiber */
89 u32 read:1; /* read MIB counters in background */
90 u32 freeze:1; /* MIB counter freeze is enabled */
92 struct ksz_port_mib mib;
93 phy_interface_t interface;
97 struct ksz_device *ksz_dev;
103 struct dsa_switch *ds;
104 struct ksz_platform_data *pdata;
105 const struct ksz_chip_data *info;
107 struct mutex dev_mutex; /* device access */
108 struct mutex regmap_mutex; /* regmap access */
109 struct mutex alu_mutex; /* ALU access */
110 struct mutex vlan_mutex; /* vlan access */
111 const struct ksz_dev_ops *dev_ops;
114 struct regmap *regmap[3];
119 struct gpio_desc *reset_gpio; /* Optional reset GPIO */
121 /* chip specific data */
124 int cpu_port; /* port connected to CPU */
126 phy_interface_t compat_interface;
128 bool synclko_disable;
130 struct vlan_table *vlan_cache;
132 struct ksz_port *ports;
133 struct delayed_work mib_read;
134 unsigned long mib_read_interval;
138 struct mutex lock_irq; /* IRQ Access */
142 /* List of supported models */
162 KSZ8563_CHIP_ID = 0x8563,
163 KSZ8795_CHIP_ID = 0x8795,
164 KSZ8794_CHIP_ID = 0x8794,
165 KSZ8765_CHIP_ID = 0x8765,
166 KSZ8830_CHIP_ID = 0x8830,
167 KSZ9477_CHIP_ID = 0x00947700,
168 KSZ9896_CHIP_ID = 0x00989600,
169 KSZ9897_CHIP_ID = 0x00989700,
170 KSZ9893_CHIP_ID = 0x00989300,
171 KSZ9567_CHIP_ID = 0x00956700,
172 LAN9370_CHIP_ID = 0x00937000,
173 LAN9371_CHIP_ID = 0x00937100,
174 LAN9372_CHIP_ID = 0x00937200,
175 LAN9373_CHIP_ID = 0x00937300,
176 LAN9374_CHIP_ID = 0x00937400,
203 PORT_802_1P_REMAPPING,
205 MIB_COUNTER_OVERFLOW,
208 VLAN_TABLE_MEMBERSHIP,
210 STATIC_MAC_TABLE_VALID,
211 STATIC_MAC_TABLE_USE_FID,
212 STATIC_MAC_TABLE_FID,
213 STATIC_MAC_TABLE_OVERRIDE,
214 STATIC_MAC_TABLE_FWD_PORTS,
215 DYNAMIC_MAC_TABLE_ENTRIES_H,
216 DYNAMIC_MAC_TABLE_MAC_EMPTY,
217 DYNAMIC_MAC_TABLE_NOT_READY,
218 DYNAMIC_MAC_TABLE_ENTRIES,
219 DYNAMIC_MAC_TABLE_FID,
220 DYNAMIC_MAC_TABLE_SRC_PORT,
221 DYNAMIC_MAC_TABLE_TIMESTAMP,
229 VLAN_TABLE_MEMBERSHIP_S,
231 STATIC_MAC_FWD_PORTS,
233 DYNAMIC_MAC_ENTRIES_H,
236 DYNAMIC_MAC_TIMESTAMP,
237 DYNAMIC_MAC_SRC_PORT,
241 enum ksz_xmii_ctrl0 {
248 enum ksz_xmii_ctrl1 {
277 int (*setup)(struct dsa_switch *ds);
278 void (*teardown)(struct dsa_switch *ds);
279 u32 (*get_port_addr)(int port, int offset);
280 void (*cfg_port_member)(struct ksz_device *dev, int port, u8 member);
281 void (*flush_dyn_mac_table)(struct ksz_device *dev, int port);
282 void (*port_cleanup)(struct ksz_device *dev, int port);
283 void (*port_setup)(struct ksz_device *dev, int port, bool cpu_port);
284 int (*r_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 *val);
285 int (*w_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 val);
286 void (*r_mib_cnt)(struct ksz_device *dev, int port, u16 addr,
288 void (*r_mib_pkt)(struct ksz_device *dev, int port, u16 addr,
289 u64 *dropped, u64 *cnt);
290 void (*r_mib_stat64)(struct ksz_device *dev, int port);
291 int (*vlan_filtering)(struct ksz_device *dev, int port,
292 bool flag, struct netlink_ext_ack *extack);
293 int (*vlan_add)(struct ksz_device *dev, int port,
294 const struct switchdev_obj_port_vlan *vlan,
295 struct netlink_ext_ack *extack);
296 int (*vlan_del)(struct ksz_device *dev, int port,
297 const struct switchdev_obj_port_vlan *vlan);
298 int (*mirror_add)(struct ksz_device *dev, int port,
299 struct dsa_mall_mirror_tc_entry *mirror,
300 bool ingress, struct netlink_ext_ack *extack);
301 void (*mirror_del)(struct ksz_device *dev, int port,
302 struct dsa_mall_mirror_tc_entry *mirror);
303 int (*fdb_add)(struct ksz_device *dev, int port,
304 const unsigned char *addr, u16 vid, struct dsa_db db);
305 int (*fdb_del)(struct ksz_device *dev, int port,
306 const unsigned char *addr, u16 vid, struct dsa_db db);
307 int (*fdb_dump)(struct ksz_device *dev, int port,
308 dsa_fdb_dump_cb_t *cb, void *data);
309 int (*mdb_add)(struct ksz_device *dev, int port,
310 const struct switchdev_obj_port_mdb *mdb,
312 int (*mdb_del)(struct ksz_device *dev, int port,
313 const struct switchdev_obj_port_mdb *mdb,
315 void (*get_caps)(struct ksz_device *dev, int port,
316 struct phylink_config *config);
317 int (*change_mtu)(struct ksz_device *dev, int port, int mtu);
318 int (*max_mtu)(struct ksz_device *dev, int port);
319 void (*freeze_mib)(struct ksz_device *dev, int port, bool freeze);
320 void (*port_init_cnt)(struct ksz_device *dev, int port);
321 void (*phylink_mac_config)(struct ksz_device *dev, int port,
323 const struct phylink_link_state *state);
324 void (*phylink_mac_link_up)(struct ksz_device *dev, int port,
326 phy_interface_t interface,
327 struct phy_device *phydev, int speed,
328 int duplex, bool tx_pause, bool rx_pause);
329 void (*setup_rgmii_delay)(struct ksz_device *dev, int port);
330 void (*config_cpu_port)(struct dsa_switch *ds);
331 int (*enable_stp_addr)(struct ksz_device *dev);
332 int (*reset)(struct ksz_device *dev);
333 int (*init)(struct ksz_device *dev);
334 void (*exit)(struct ksz_device *dev);
337 struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
338 int ksz_switch_register(struct ksz_device *dev);
339 void ksz_switch_remove(struct ksz_device *dev);
341 void ksz_init_mib_timer(struct ksz_device *dev);
342 void ksz_r_mib_stats64(struct ksz_device *dev, int port);
343 void ksz_port_stp_state_set(struct dsa_switch *ds, int port, u8 state);
344 bool ksz_get_gbit(struct ksz_device *dev, int port);
345 phy_interface_t ksz_get_xmii(struct ksz_device *dev, int port, bool gbit);
346 extern const struct ksz_chip_data ksz_switch_chips[];
348 /* Common register access functions */
350 static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
353 int ret = regmap_read(dev->regmap[0], reg, &value);
356 dev_err(dev->dev, "can't read 8bit reg: 0x%x %pe\n", reg,
363 static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
366 int ret = regmap_read(dev->regmap[1], reg, &value);
369 dev_err(dev->dev, "can't read 16bit reg: 0x%x %pe\n", reg,
376 static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
379 int ret = regmap_read(dev->regmap[2], reg, &value);
382 dev_err(dev->dev, "can't read 32bit reg: 0x%x %pe\n", reg,
389 static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val)
394 ret = regmap_bulk_read(dev->regmap[2], reg, value, 2);
396 dev_err(dev->dev, "can't read 64bit reg: 0x%x %pe\n", reg,
399 *val = (u64)value[0] << 32 | value[1];
404 static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
408 ret = regmap_write(dev->regmap[0], reg, value);
410 dev_err(dev->dev, "can't write 8bit reg: 0x%x %pe\n", reg,
416 static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
420 ret = regmap_write(dev->regmap[1], reg, value);
422 dev_err(dev->dev, "can't write 16bit reg: 0x%x %pe\n", reg,
428 static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
432 ret = regmap_write(dev->regmap[2], reg, value);
434 dev_err(dev->dev, "can't write 32bit reg: 0x%x %pe\n", reg,
440 static inline int ksz_write64(struct ksz_device *dev, u32 reg, u64 value)
444 /* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */
445 value = swab64(value);
446 val[0] = swab32(value & 0xffffffffULL);
447 val[1] = swab32(value >> 32ULL);
449 return regmap_bulk_write(dev->regmap[2], reg, val, 2);
452 static inline int ksz_pread8(struct ksz_device *dev, int port, int offset,
455 return ksz_read8(dev, dev->dev_ops->get_port_addr(port, offset), data);
458 static inline int ksz_pread16(struct ksz_device *dev, int port, int offset,
461 return ksz_read16(dev, dev->dev_ops->get_port_addr(port, offset), data);
464 static inline int ksz_pread32(struct ksz_device *dev, int port, int offset,
467 return ksz_read32(dev, dev->dev_ops->get_port_addr(port, offset), data);
470 static inline int ksz_pwrite8(struct ksz_device *dev, int port, int offset,
473 return ksz_write8(dev, dev->dev_ops->get_port_addr(port, offset), data);
476 static inline int ksz_pwrite16(struct ksz_device *dev, int port, int offset,
479 return ksz_write16(dev, dev->dev_ops->get_port_addr(port, offset),
483 static inline int ksz_pwrite32(struct ksz_device *dev, int port, int offset,
486 return ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset),
490 static inline void ksz_prmw8(struct ksz_device *dev, int port, int offset,
493 regmap_update_bits(dev->regmap[0],
494 dev->dev_ops->get_port_addr(port, offset),
498 static inline void ksz_regmap_lock(void *__mtx)
500 struct mutex *mtx = __mtx;
504 static inline void ksz_regmap_unlock(void *__mtx)
506 struct mutex *mtx = __mtx;
510 static inline bool ksz_is_ksz88x3(struct ksz_device *dev)
512 return dev->chip_id == KSZ8830_CHIP_ID;
515 static inline int is_lan937x(struct ksz_device *dev)
517 return dev->chip_id == LAN9370_CHIP_ID ||
518 dev->chip_id == LAN9371_CHIP_ID ||
519 dev->chip_id == LAN9372_CHIP_ID ||
520 dev->chip_id == LAN9373_CHIP_ID ||
521 dev->chip_id == LAN9374_CHIP_ID;
524 /* STP State Defines */
525 #define PORT_TX_ENABLE BIT(2)
526 #define PORT_RX_ENABLE BIT(1)
527 #define PORT_LEARN_DISABLE BIT(0)
529 /* Switch ID Defines */
530 #define REG_CHIP_ID0 0x00
532 #define SW_FAMILY_ID_M GENMASK(15, 8)
533 #define KSZ87_FAMILY_ID 0x87
534 #define KSZ88_FAMILY_ID 0x88
536 #define KSZ8_PORT_STATUS_0 0x08
537 #define KSZ8_PORT_FIBER_MODE BIT(7)
539 #define SW_CHIP_ID_M GENMASK(7, 4)
540 #define KSZ87_CHIP_ID_94 0x6
541 #define KSZ87_CHIP_ID_95 0x9
542 #define KSZ88_CHIP_ID_63 0x3
544 #define SW_REV_ID_M GENMASK(7, 4)
546 /* KSZ9893, KSZ9563, KSZ8563 specific register */
547 #define REG_CHIP_ID4 0x0f
548 #define SKU_ID_KSZ8563 0x3c
550 /* Driver set switch broadcast storm protection at 10% rate. */
551 #define BROADCAST_STORM_PROT_RATE 10
553 /* 148,800 frames * 67 ms / 100 */
554 #define BROADCAST_STORM_VALUE 9969
556 #define BROADCAST_STORM_RATE_HI 0x07
557 #define BROADCAST_STORM_RATE_LO 0xFF
558 #define BROADCAST_STORM_RATE 0x07FF
560 #define MULTICAST_STORM_DISABLE BIT(6)
562 #define SW_START 0x01
564 /* xMII configuration */
565 #define P_MII_DUPLEX_M BIT(6)
566 #define P_MII_100MBIT_M BIT(4)
568 #define P_GMII_1GBIT_M BIT(6)
569 #define P_RGMII_ID_IG_ENABLE BIT(4)
570 #define P_RGMII_ID_EG_ENABLE BIT(3)
571 #define P_MII_MAC_MODE BIT(2)
572 #define P_MII_SEL_M 0x3
574 /* Regmap tables generation */
575 #define KSZ_SPI_OP_RD 3
576 #define KSZ_SPI_OP_WR 2
578 #define swabnot_used(x) 0
580 #define KSZ_SPI_OP_FLAG_MASK(opcode, swp, regbits, regpad) \
581 swab##swp((opcode) << ((regbits) + (regpad)))
583 #define KSZ_REGMAP_ENTRY(width, swp, regbits, regpad, regalign) \
586 .val_bits = (width), \
588 .reg_bits = (regbits) + (regalign), \
589 .pad_bits = (regpad), \
590 .max_register = BIT(regbits) - 1, \
591 .cache_type = REGCACHE_NONE, \
593 KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_RD, swp, \
596 KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_WR, swp, \
598 .lock = ksz_regmap_lock, \
599 .unlock = ksz_regmap_unlock, \
600 .reg_format_endian = REGMAP_ENDIAN_BIG, \
601 .val_format_endian = REGMAP_ENDIAN_BIG \
604 #define KSZ_REGMAP_TABLE(ksz, swp, regbits, regpad, regalign) \
605 static const struct regmap_config ksz##_regmap_config[] = { \
606 KSZ_REGMAP_ENTRY(8, swp, (regbits), (regpad), (regalign)), \
607 KSZ_REGMAP_ENTRY(16, swp, (regbits), (regpad), (regalign)), \
608 KSZ_REGMAP_ENTRY(32, swp, (regbits), (regpad), (regalign)), \