1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip switch driver main logic
5 * Copyright (C) 2017-2018 Microchip Technology Inc.
8 #include <linux/delay.h>
9 #include <linux/export.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_data/microchip-ksz.h>
14 #include <linux/phy.h>
15 #include <linux/etherdevice.h>
16 #include <linux/if_bridge.h>
17 #include <linux/of_net.h>
19 #include <net/switchdev.h>
23 void ksz_update_port_member(struct ksz_device *dev, int port)
28 for (i = 0; i < dev->port_cnt; i++) {
29 if (i == port || i == dev->cpu_port)
32 if (!(dev->member & (1 << i)))
35 /* Port is a member of the bridge and is forwarding. */
36 if (p->stp_state == BR_STATE_FORWARDING &&
37 p->member != dev->member)
38 dev->dev_ops->cfg_port_member(dev, i, dev->member);
41 EXPORT_SYMBOL_GPL(ksz_update_port_member);
43 int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg)
45 struct ksz_device *dev = ds->priv;
48 dev->dev_ops->r_phy(dev, addr, reg, &val);
52 EXPORT_SYMBOL_GPL(ksz_phy_read16);
54 int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
56 struct ksz_device *dev = ds->priv;
58 dev->dev_ops->w_phy(dev, addr, reg, val);
62 EXPORT_SYMBOL_GPL(ksz_phy_write16);
64 int ksz_sset_count(struct dsa_switch *ds, int port, int sset)
66 struct ksz_device *dev = ds->priv;
68 if (sset != ETH_SS_STATS)
73 EXPORT_SYMBOL_GPL(ksz_sset_count);
75 int ksz_port_bridge_join(struct dsa_switch *ds, int port,
76 struct net_device *br)
78 struct ksz_device *dev = ds->priv;
80 dev->br_member |= (1 << port);
82 /* port_stp_state_set() will be called after to put the port in
83 * appropriate state so there is no need to do anything.
88 EXPORT_SYMBOL_GPL(ksz_port_bridge_join);
90 void ksz_port_bridge_leave(struct dsa_switch *ds, int port,
91 struct net_device *br)
93 struct ksz_device *dev = ds->priv;
95 dev->br_member &= ~(1 << port);
96 dev->member &= ~(1 << port);
98 /* port_stp_state_set() will be called after to put the port in
99 * forwarding state so there is no need to do anything.
102 EXPORT_SYMBOL_GPL(ksz_port_bridge_leave);
104 void ksz_port_fast_age(struct dsa_switch *ds, int port)
106 struct ksz_device *dev = ds->priv;
108 dev->dev_ops->flush_dyn_mac_table(dev, port);
110 EXPORT_SYMBOL_GPL(ksz_port_fast_age);
112 int ksz_port_vlan_prepare(struct dsa_switch *ds, int port,
113 const struct switchdev_obj_port_vlan *vlan)
119 EXPORT_SYMBOL_GPL(ksz_port_vlan_prepare);
121 int ksz_port_fdb_dump(struct dsa_switch *ds, int port, dsa_fdb_dump_cb_t *cb,
124 struct ksz_device *dev = ds->priv;
131 struct alu_struct alu;
134 alu.is_static = false;
135 ret = dev->dev_ops->r_dyn_mac_table(dev, i, alu.mac, &fid,
138 if (!ret && (member & BIT(port))) {
139 ret = cb(alu.mac, alu.fid, alu.is_static, data);
144 } while (i < entries);
150 EXPORT_SYMBOL_GPL(ksz_port_fdb_dump);
152 int ksz_port_mdb_prepare(struct dsa_switch *ds, int port,
153 const struct switchdev_obj_port_mdb *mdb)
158 EXPORT_SYMBOL_GPL(ksz_port_mdb_prepare);
160 void ksz_port_mdb_add(struct dsa_switch *ds, int port,
161 const struct switchdev_obj_port_mdb *mdb)
163 struct ksz_device *dev = ds->priv;
164 struct alu_struct alu;
168 alu.port_forward = 0;
169 for (index = 0; index < dev->num_statics; index++) {
170 if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) {
171 /* Found one already in static MAC table. */
172 if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) &&
175 /* Remember the first empty entry. */
181 /* no available entry */
182 if (index == dev->num_statics && !empty)
186 if (index == dev->num_statics) {
188 memset(&alu, 0, sizeof(alu));
189 memcpy(alu.mac, mdb->addr, ETH_ALEN);
190 alu.is_static = true;
192 alu.port_forward |= BIT(port);
194 alu.is_use_fid = true;
196 /* Need a way to map VID to FID. */
199 dev->dev_ops->w_sta_mac_table(dev, index, &alu);
201 EXPORT_SYMBOL_GPL(ksz_port_mdb_add);
203 int ksz_port_mdb_del(struct dsa_switch *ds, int port,
204 const struct switchdev_obj_port_mdb *mdb)
206 struct ksz_device *dev = ds->priv;
207 struct alu_struct alu;
211 for (index = 0; index < dev->num_statics; index++) {
212 if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) {
213 /* Found one already in static MAC table. */
214 if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) &&
220 /* no available entry */
221 if (index == dev->num_statics)
225 alu.port_forward &= ~BIT(port);
226 if (!alu.port_forward)
227 alu.is_static = false;
228 dev->dev_ops->w_sta_mac_table(dev, index, &alu);
233 EXPORT_SYMBOL_GPL(ksz_port_mdb_del);
235 int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
237 struct ksz_device *dev = ds->priv;
239 /* setup slave port */
240 dev->dev_ops->port_setup(dev, port, false);
242 /* port_stp_state_set() will be called after to enable the port so
243 * there is no need to do anything.
248 EXPORT_SYMBOL_GPL(ksz_enable_port);
250 void ksz_disable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
252 struct ksz_device *dev = ds->priv;
254 dev->on_ports &= ~(1 << port);
255 dev->live_ports &= ~(1 << port);
257 /* port_stp_state_set() will be called after to disable the port so
258 * there is no need to do anything.
261 EXPORT_SYMBOL_GPL(ksz_disable_port);
263 struct ksz_device *ksz_switch_alloc(struct device *base,
264 const struct ksz_io_ops *ops,
267 struct dsa_switch *ds;
268 struct ksz_device *swdev;
270 ds = dsa_switch_alloc(base, DSA_MAX_PORTS);
274 swdev = devm_kzalloc(base, sizeof(*swdev), GFP_KERNEL);
287 EXPORT_SYMBOL(ksz_switch_alloc);
289 int ksz_switch_register(struct ksz_device *dev,
290 const struct ksz_dev_ops *ops)
295 dev->chip_id = dev->pdata->chip_id;
297 dev->reset_gpio = devm_gpiod_get_optional(dev->dev, "reset",
299 if (IS_ERR(dev->reset_gpio))
300 return PTR_ERR(dev->reset_gpio);
302 if (dev->reset_gpio) {
303 gpiod_set_value(dev->reset_gpio, 1);
305 gpiod_set_value(dev->reset_gpio, 0);
308 mutex_init(&dev->reg_mutex);
309 mutex_init(&dev->stats_mutex);
310 mutex_init(&dev->alu_mutex);
311 mutex_init(&dev->vlan_mutex);
315 if (dev->dev_ops->detect(dev))
318 ret = dev->dev_ops->init(dev);
322 dev->interface = PHY_INTERFACE_MODE_MII;
323 if (dev->dev->of_node) {
324 ret = of_get_phy_mode(dev->dev->of_node);
326 dev->interface = ret;
329 ret = dsa_register_switch(dev->ds);
331 dev->dev_ops->exit(dev);
337 EXPORT_SYMBOL(ksz_switch_register);
339 void ksz_switch_remove(struct ksz_device *dev)
341 dev->dev_ops->exit(dev);
342 dsa_unregister_switch(dev->ds);
345 gpiod_set_value(dev->reset_gpio, 1);
348 EXPORT_SYMBOL(ksz_switch_remove);
350 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
351 MODULE_DESCRIPTION("Microchip KSZ Series Switch DSA Driver");
352 MODULE_LICENSE("GPL");