1 From 0dff8c753c8929a478357abb38db0d1c1a60ec94 Mon Sep 17 00:00:00 2001
2 From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
3 Date: Wed, 29 Aug 2012 22:08:15 +0200
4 Subject: net: switchlib: add framework for ethernet switch drivers
6 Add a generic framework similar to phylib for ethernet switch
7 drivers and devices. This is useful to share the init and
8 setup code for switch devices across different boards.
10 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
11 Cc: Joe Hershberger <joe.hershberger@gmail.com>
15 @@ -280,6 +280,7 @@ LIBS-y += drivers/mtd/ubi/libubi.o
16 LIBS-y += drivers/mtd/spi/libspi_flash.o
17 LIBS-y += drivers/net/libnet.o
18 LIBS-y += drivers/net/phy/libphy.o
19 +LIBS-y += drivers/net/switch/libswitch.o
20 LIBS-y += drivers/pci/libpci.o
21 LIBS-y += drivers/pcmcia/libpcmcia.o
22 LIBS-y += drivers/power/libpower.o \
24 +++ b/drivers/net/switch/Makefile
27 +# Copyright (C) 2000-2011 Wolfgang Denk, DENX Software Engineering, wd@denx.de
28 +# Copyright (C) 2011-2013 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
30 +# SPDX-License-Identifier: GPL-2.0+
33 +include $(TOPDIR)/config.mk
35 +LIB := $(obj)libswitch.o
37 +COBJS-$(CONFIG_SWITCH_MULTI) += switch.o
40 +SRCS := $(COBJS:.o=.c)
41 +OBJS := $(addprefix $(obj),$(COBJS))
45 +$(LIB): $(obj).depend $(OBJS)
46 + $(call cmd_link_o_target, $(OBJS))
48 +#########################################################################
50 +# defines $(obj).depend target
51 +include $(SRCTREE)/rules.mk
53 +sinclude $(obj).depend
55 +#########################################################################
57 +++ b/drivers/net/switch/switch.c
60 + * Copyright (C) 2011-2013 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
62 + * SPDX-License-Identifier: GPL-2.0+
70 +static struct list_head switch_drivers;
71 +static struct list_head switch_devices;
73 +void switch_init(void)
75 + INIT_LIST_HEAD(&switch_drivers);
76 + INIT_LIST_HEAD(&switch_devices);
78 + board_switch_init();
81 +void switch_driver_register(struct switch_driver *drv)
83 + INIT_LIST_HEAD(&drv->list);
84 + list_add_tail(&drv->list, &switch_drivers);
87 +int switch_device_register(struct switch_device *dev)
89 + struct switch_driver *drv;
91 + /* Add switch device only, if an adequate driver is registered */
92 + list_for_each_entry(drv, &switch_drivers, list) {
93 + if (!strcmp(drv->name, dev->name)) {
96 + INIT_LIST_HEAD(&dev->list);
97 + list_add_tail(&dev->list, &switch_devices);
106 +struct switch_device *switch_connect(struct mii_dev *bus)
108 + struct switch_device *sw;
111 + list_for_each_entry(sw, &switch_devices, list) {
114 + err = sw->drv->probe(sw);
122 +++ b/include/switch.h
125 + * This file is released under the terms of GPL v2 and any later version.
126 + * See the file COPYING in the root directory of the source tree for details.
128 + * Copyright (C) 2011-2013 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
134 +#include <linux/list.h>
136 +#define SWITCH_NAME_SIZE 32
138 +struct switch_device;
141 +struct switch_driver {
142 + struct list_head list;
144 + /* Switch device name */
145 + const char name[SWITCH_NAME_SIZE];
148 + * Called to probe the switch chip. Must return 0 if the switch
149 + * chip matches the given switch device/driver combination. Otherwise
150 + * 1 must be returned.
152 + int (*probe) (struct switch_device *dev);
155 + * Called to initialize the switch chip.
157 + void (*setup) (struct switch_device *dev);
160 +struct switch_device {
161 + struct list_head list;
162 + struct switch_driver *drv;
164 + /* MII bus the switch chip is connected to */
165 + struct mii_dev *bus;
167 + /* Switch device name */
168 + const char name[SWITCH_NAME_SIZE];
170 + /* Bitmask for board specific setup of used switch ports */
173 + /* Number of switch port that is connected to host CPU */
178 + * Board specific switch initialization.
180 + * Called from switch_init to register the board specific switch_device
183 +extern int board_switch_init(void);
185 +/* Initialize switch subsystem */
186 +#ifdef CONFIG_SWITCH_MULTI
187 +extern void switch_init(void);
189 +static inline void switch_init(void)
194 +/* Register a switch driver */
195 +extern void switch_driver_register(struct switch_driver *drv);
197 +/* Register a switch device */
198 +extern int switch_device_register(struct switch_device *dev);
201 + * Probe the available switch chips and connect the found one
202 + * with the given MII bus
204 +#ifdef CONFIG_SWITCH_MULTI
205 +extern struct switch_device *switch_connect(struct mii_dev *bus);
207 +static inline struct switch_device *switch_connect(struct mii_dev *bus)
214 + * Setup the given switch device
216 +static inline void switch_setup(struct switch_device *dev)
218 + if (dev->drv->setup)
219 + dev->drv->setup(dev);
222 +/* Init functions for supported Switch drivers */
224 +#endif /* __SWITCH_H */
234 void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
236 @@ -287,6 +288,8 @@ int eth_initialize(bd_t *bis)