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>
22 struct mutex cnt_mutex; /* structure access */
25 struct rtnl_link_stats64 stats64;
26 struct spinlock stats64_lock;
30 bool remove_tag; /* Remove Tag flag set, for ksz8795 only */
32 struct phy_device phydev;
34 u32 on:1; /* port is not disabled by hardware */
35 u32 phy:1; /* port has a PHY */
36 u32 fiber:1; /* port is fiber */
37 u32 sgmii:1; /* port is SGMII */
39 u32 read:1; /* read MIB counters in background */
40 u32 freeze:1; /* MIB counter freeze is enabled */
42 struct ksz_port_mib mib;
43 phy_interface_t interface;
47 struct dsa_switch *ds;
48 struct ksz_platform_data *pdata;
51 struct mutex dev_mutex; /* device access */
52 struct mutex regmap_mutex; /* regmap access */
53 struct mutex alu_mutex; /* ALU access */
54 struct mutex vlan_mutex; /* vlan access */
55 const struct ksz_dev_ops *dev_ops;
58 struct regmap *regmap[3];
62 struct gpio_desc *reset_gpio; /* Optional reset GPIO */
64 /* chip specific data */
69 int cpu_port; /* port connected to CPU */
70 int cpu_ports; /* port bitmap can be cpu port */
75 const struct mib_names *mib_names;
76 phy_interface_t compat_interface;
82 struct vlan_table *vlan_cache;
84 struct ksz_port *ports;
85 struct delayed_work mib_read;
86 unsigned long mib_read_interval;
89 u32 features; /* chip specific features */
90 u32 overrides; /* chip functions set by user */
115 u32 (*get_port_addr)(int port, int offset);
116 void (*cfg_port_member)(struct ksz_device *dev, int port, u8 member);
117 void (*flush_dyn_mac_table)(struct ksz_device *dev, int port);
118 void (*port_cleanup)(struct ksz_device *dev, int port);
119 void (*port_setup)(struct ksz_device *dev, int port, bool cpu_port);
120 void (*r_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 *val);
121 void (*w_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 val);
122 int (*r_dyn_mac_table)(struct ksz_device *dev, u16 addr, u8 *mac_addr,
123 u8 *fid, u8 *src_port, u8 *timestamp,
125 int (*r_sta_mac_table)(struct ksz_device *dev, u16 addr,
126 struct alu_struct *alu);
127 void (*w_sta_mac_table)(struct ksz_device *dev, u16 addr,
128 struct alu_struct *alu);
129 void (*r_mib_cnt)(struct ksz_device *dev, int port, u16 addr,
131 void (*r_mib_pkt)(struct ksz_device *dev, int port, u16 addr,
132 u64 *dropped, u64 *cnt);
133 void (*r_mib_stat64)(struct ksz_device *dev, int port);
134 void (*freeze_mib)(struct ksz_device *dev, int port, bool freeze);
135 void (*port_init_cnt)(struct ksz_device *dev, int port);
136 int (*shutdown)(struct ksz_device *dev);
137 int (*detect)(struct ksz_device *dev);
138 int (*init)(struct ksz_device *dev);
139 void (*exit)(struct ksz_device *dev);
142 struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
143 int ksz_switch_register(struct ksz_device *dev,
144 const struct ksz_dev_ops *ops);
145 void ksz_switch_remove(struct ksz_device *dev);
147 int ksz8_switch_register(struct ksz_device *dev);
148 int ksz9477_switch_register(struct ksz_device *dev);
150 void ksz_update_port_member(struct ksz_device *dev, int port);
151 void ksz_init_mib_timer(struct ksz_device *dev);
153 /* Common DSA access functions */
155 int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg);
156 int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val);
157 void ksz_mac_link_down(struct dsa_switch *ds, int port, unsigned int mode,
158 phy_interface_t interface);
159 int ksz_sset_count(struct dsa_switch *ds, int port, int sset);
160 void ksz_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *buf);
161 int ksz_port_bridge_join(struct dsa_switch *ds, int port,
162 struct dsa_bridge bridge, bool *tx_fwd_offload);
163 void ksz_port_bridge_leave(struct dsa_switch *ds, int port,
164 struct dsa_bridge bridge);
165 void ksz_port_fast_age(struct dsa_switch *ds, int port);
166 int ksz_port_fdb_dump(struct dsa_switch *ds, int port, dsa_fdb_dump_cb_t *cb,
168 int ksz_port_mdb_add(struct dsa_switch *ds, int port,
169 const struct switchdev_obj_port_mdb *mdb,
171 int ksz_port_mdb_del(struct dsa_switch *ds, int port,
172 const struct switchdev_obj_port_mdb *mdb,
174 int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy);
176 /* Common register access functions */
178 static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
181 int ret = regmap_read(dev->regmap[0], reg, &value);
187 static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
190 int ret = regmap_read(dev->regmap[1], reg, &value);
196 static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
199 int ret = regmap_read(dev->regmap[2], reg, &value);
205 static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val)
210 ret = regmap_bulk_read(dev->regmap[2], reg, value, 2);
212 *val = (u64)value[0] << 32 | value[1];
217 static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
219 return regmap_write(dev->regmap[0], reg, value);
222 static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
224 return regmap_write(dev->regmap[1], reg, value);
227 static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
229 return regmap_write(dev->regmap[2], reg, value);
232 static inline int ksz_write64(struct ksz_device *dev, u32 reg, u64 value)
236 /* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */
237 value = swab64(value);
238 val[0] = swab32(value & 0xffffffffULL);
239 val[1] = swab32(value >> 32ULL);
241 return regmap_bulk_write(dev->regmap[2], reg, val, 2);
244 static inline void ksz_pread8(struct ksz_device *dev, int port, int offset,
247 ksz_read8(dev, dev->dev_ops->get_port_addr(port, offset), data);
250 static inline void ksz_pread16(struct ksz_device *dev, int port, int offset,
253 ksz_read16(dev, dev->dev_ops->get_port_addr(port, offset), data);
256 static inline void ksz_pread32(struct ksz_device *dev, int port, int offset,
259 ksz_read32(dev, dev->dev_ops->get_port_addr(port, offset), data);
262 static inline void ksz_pwrite8(struct ksz_device *dev, int port, int offset,
265 ksz_write8(dev, dev->dev_ops->get_port_addr(port, offset), data);
268 static inline void ksz_pwrite16(struct ksz_device *dev, int port, int offset,
271 ksz_write16(dev, dev->dev_ops->get_port_addr(port, offset), data);
274 static inline void ksz_pwrite32(struct ksz_device *dev, int port, int offset,
277 ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset), data);
280 static inline void ksz_regmap_lock(void *__mtx)
282 struct mutex *mtx = __mtx;
286 static inline void ksz_regmap_unlock(void *__mtx)
288 struct mutex *mtx = __mtx;
292 /* Regmap tables generation */
293 #define KSZ_SPI_OP_RD 3
294 #define KSZ_SPI_OP_WR 2
296 #define swabnot_used(x) 0
298 #define KSZ_SPI_OP_FLAG_MASK(opcode, swp, regbits, regpad) \
299 swab##swp((opcode) << ((regbits) + (regpad)))
301 #define KSZ_REGMAP_ENTRY(width, swp, regbits, regpad, regalign) \
304 .val_bits = (width), \
306 .reg_bits = (regbits) + (regalign), \
307 .pad_bits = (regpad), \
308 .max_register = BIT(regbits) - 1, \
309 .cache_type = REGCACHE_NONE, \
311 KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_RD, swp, \
314 KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_WR, swp, \
316 .lock = ksz_regmap_lock, \
317 .unlock = ksz_regmap_unlock, \
318 .reg_format_endian = REGMAP_ENDIAN_BIG, \
319 .val_format_endian = REGMAP_ENDIAN_BIG \
322 #define KSZ_REGMAP_TABLE(ksz, swp, regbits, regpad, regalign) \
323 static const struct regmap_config ksz##_regmap_config[] = { \
324 KSZ_REGMAP_ENTRY(8, swp, (regbits), (regpad), (regalign)), \
325 KSZ_REGMAP_ENTRY(16, swp, (regbits), (regpad), (regalign)), \
326 KSZ_REGMAP_ENTRY(32, swp, (regbits), (regpad), (regalign)), \