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 */
31 struct phy_device phydev;
33 u32 on:1; /* port is not disabled by hardware */
34 u32 phy:1; /* port has a PHY */
35 u32 fiber:1; /* port is fiber */
36 u32 sgmii:1; /* port is SGMII */
38 u32 read:1; /* read MIB counters in background */
39 u32 freeze:1; /* MIB counter freeze is enabled */
41 struct ksz_port_mib mib;
45 struct dsa_switch *ds;
46 struct ksz_platform_data *pdata;
49 struct mutex dev_mutex; /* device access */
50 struct mutex regmap_mutex; /* regmap access */
51 struct mutex alu_mutex; /* ALU access */
52 struct mutex vlan_mutex; /* vlan access */
53 const struct ksz_dev_ops *dev_ops;
56 struct regmap *regmap[3];
60 struct gpio_desc *reset_gpio; /* Optional reset GPIO */
62 /* chip specific data */
67 int cpu_port; /* port connected to CPU */
68 int cpu_ports; /* port bitmap can be cpu port */
74 int last_port; /* ports after that not used */
75 phy_interface_t interface;
80 struct vlan_table *vlan_cache;
82 struct ksz_port *ports;
83 struct timer_list mib_read_timer;
84 struct work_struct mib_read;
85 unsigned long mib_read_interval;
89 u16 on_ports; /* ports enabled by DSA */
94 u32 features; /* chip specific features */
95 u32 overrides; /* chip functions set by user */
120 u32 (*get_port_addr)(int port, int offset);
121 void (*cfg_port_member)(struct ksz_device *dev, int port, u8 member);
122 void (*flush_dyn_mac_table)(struct ksz_device *dev, int port);
123 void (*phy_setup)(struct ksz_device *dev, int port,
124 struct phy_device *phy);
125 void (*port_cleanup)(struct ksz_device *dev, int port);
126 void (*port_setup)(struct ksz_device *dev, int port, bool cpu_port);
127 void (*r_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 *val);
128 void (*w_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 val);
129 int (*r_dyn_mac_table)(struct ksz_device *dev, u16 addr, u8 *mac_addr,
130 u8 *fid, u8 *src_port, u8 *timestamp,
132 int (*r_sta_mac_table)(struct ksz_device *dev, u16 addr,
133 struct alu_struct *alu);
134 void (*w_sta_mac_table)(struct ksz_device *dev, u16 addr,
135 struct alu_struct *alu);
136 void (*r_mib_cnt)(struct ksz_device *dev, int port, u16 addr,
138 void (*r_mib_pkt)(struct ksz_device *dev, int port, u16 addr,
139 u64 *dropped, u64 *cnt);
140 void (*freeze_mib)(struct ksz_device *dev, int port, bool freeze);
141 void (*port_init_cnt)(struct ksz_device *dev, int port);
142 int (*shutdown)(struct ksz_device *dev);
143 int (*detect)(struct ksz_device *dev);
144 int (*init)(struct ksz_device *dev);
145 void (*exit)(struct ksz_device *dev);
148 struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
149 int ksz_switch_register(struct ksz_device *dev,
150 const struct ksz_dev_ops *ops);
151 void ksz_switch_remove(struct ksz_device *dev);
153 int ksz8795_switch_register(struct ksz_device *dev);
154 int ksz9477_switch_register(struct ksz_device *dev);
156 void ksz_update_port_member(struct ksz_device *dev, int port);
157 void ksz_init_mib_timer(struct ksz_device *dev);
159 /* Common DSA access functions */
161 int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg);
162 int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val);
163 void ksz_adjust_link(struct dsa_switch *ds, int port,
164 struct phy_device *phydev);
165 int ksz_sset_count(struct dsa_switch *ds, int port, int sset);
166 void ksz_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *buf);
167 int ksz_port_bridge_join(struct dsa_switch *ds, int port,
168 struct net_device *br);
169 void ksz_port_bridge_leave(struct dsa_switch *ds, int port,
170 struct net_device *br);
171 void ksz_port_fast_age(struct dsa_switch *ds, int port);
172 int ksz_port_vlan_prepare(struct dsa_switch *ds, int port,
173 const struct switchdev_obj_port_vlan *vlan);
174 int ksz_port_fdb_dump(struct dsa_switch *ds, int port, dsa_fdb_dump_cb_t *cb,
176 int ksz_port_mdb_prepare(struct dsa_switch *ds, int port,
177 const struct switchdev_obj_port_mdb *mdb);
178 void ksz_port_mdb_add(struct dsa_switch *ds, int port,
179 const struct switchdev_obj_port_mdb *mdb);
180 int ksz_port_mdb_del(struct dsa_switch *ds, int port,
181 const struct switchdev_obj_port_mdb *mdb);
182 int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy);
183 void ksz_disable_port(struct dsa_switch *ds, int port);
185 /* Common register access functions */
187 static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
190 int ret = regmap_read(dev->regmap[0], reg, &value);
196 static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
199 int ret = regmap_read(dev->regmap[1], reg, &value);
205 static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
208 int ret = regmap_read(dev->regmap[2], reg, &value);
214 static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val)
219 ret = regmap_bulk_read(dev->regmap[2], reg, value, 2);
221 /* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */
222 value[0] = swab32(value[0]);
223 value[1] = swab32(value[1]);
224 *val = swab64((u64)*value);
230 static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
232 return regmap_write(dev->regmap[0], reg, value);
235 static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
237 return regmap_write(dev->regmap[1], reg, value);
240 static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
242 return regmap_write(dev->regmap[2], reg, value);
245 static inline int ksz_write64(struct ksz_device *dev, u32 reg, u64 value)
249 /* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */
250 value = swab64(value);
251 val[0] = swab32(value & 0xffffffffULL);
252 val[1] = swab32(value >> 32ULL);
254 return regmap_bulk_write(dev->regmap[2], reg, val, 2);
257 static inline void ksz_pread8(struct ksz_device *dev, int port, int offset,
260 ksz_read8(dev, dev->dev_ops->get_port_addr(port, offset), data);
263 static inline void ksz_pread16(struct ksz_device *dev, int port, int offset,
266 ksz_read16(dev, dev->dev_ops->get_port_addr(port, offset), data);
269 static inline void ksz_pread32(struct ksz_device *dev, int port, int offset,
272 ksz_read32(dev, dev->dev_ops->get_port_addr(port, offset), data);
275 static inline void ksz_pwrite8(struct ksz_device *dev, int port, int offset,
278 ksz_write8(dev, dev->dev_ops->get_port_addr(port, offset), data);
281 static inline void ksz_pwrite16(struct ksz_device *dev, int port, int offset,
284 ksz_write16(dev, dev->dev_ops->get_port_addr(port, offset), data);
287 static inline void ksz_pwrite32(struct ksz_device *dev, int port, int offset,
290 ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset), data);
293 static inline void ksz_regmap_lock(void *__mtx)
295 struct mutex *mtx = __mtx;
299 static inline void ksz_regmap_unlock(void *__mtx)
301 struct mutex *mtx = __mtx;
305 /* Regmap tables generation */
306 #define KSZ_SPI_OP_RD 3
307 #define KSZ_SPI_OP_WR 2
309 #define swabnot_used(x) 0
311 #define KSZ_SPI_OP_FLAG_MASK(opcode, swp, regbits, regpad) \
312 swab##swp((opcode) << ((regbits) + (regpad)))
314 #define KSZ_REGMAP_ENTRY(width, swp, regbits, regpad, regalign) \
317 .val_bits = (width), \
319 .reg_bits = (regbits) + (regalign), \
320 .pad_bits = (regpad), \
321 .max_register = BIT(regbits) - 1, \
322 .cache_type = REGCACHE_NONE, \
324 KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_RD, swp, \
327 KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_WR, swp, \
329 .lock = ksz_regmap_lock, \
330 .unlock = ksz_regmap_unlock, \
331 .reg_format_endian = REGMAP_ENDIAN_BIG, \
332 .val_format_endian = REGMAP_ENDIAN_BIG \
335 #define KSZ_REGMAP_TABLE(ksz, swp, regbits, regpad, regalign) \
336 static const struct regmap_config ksz##_regmap_config[] = { \
337 KSZ_REGMAP_ENTRY(8, swp, (regbits), (regpad), (regalign)), \
338 KSZ_REGMAP_ENTRY(16, swp, (regbits), (regpad), (regalign)), \
339 KSZ_REGMAP_ENTRY(32, swp, (regbits), (regpad), (regalign)), \