blob: de6f51bb094f032b0ac175c298462ec134e80c40 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001From 0dff8c753c8929a478357abb38db0d1c1a60ec94 Mon Sep 17 00:00:00 2001
2From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
3Date: Wed, 29 Aug 2012 22:08:15 +0200
4Subject: net: switchlib: add framework for ethernet switch drivers
5
6Add a generic framework similar to phylib for ethernet switch
7drivers and devices. This is useful to share the init and
8setup code for switch devices across different boards.
9
10Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
11Cc: Joe Hershberger <joe.hershberger@gmail.com>
12
13--- a/Makefile
14+++ b/Makefile
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 \
23--- /dev/null
24+++ b/drivers/net/switch/Makefile
25@@ -0,0 +1,30 @@
26+#
27+# Copyright (C) 2000-2011 Wolfgang Denk, DENX Software Engineering, wd@denx.de
28+# Copyright (C) 2011-2013 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
29+#
30+# SPDX-License-Identifier: GPL-2.0+
31+#
32+
33+include $(TOPDIR)/config.mk
34+
35+LIB := $(obj)libswitch.o
36+
37+COBJS-$(CONFIG_SWITCH_MULTI) += switch.o
38+
39+COBJS := $(COBJS-y)
40+SRCS := $(COBJS:.o=.c)
41+OBJS := $(addprefix $(obj),$(COBJS))
42+
43+all: $(LIB)
44+
45+$(LIB): $(obj).depend $(OBJS)
46+ $(call cmd_link_o_target, $(OBJS))
47+
48+#########################################################################
49+
50+# defines $(obj).depend target
51+include $(SRCTREE)/rules.mk
52+
53+sinclude $(obj).depend
54+
55+#########################################################################
56--- /dev/null
57+++ b/drivers/net/switch/switch.c
58@@ -0,0 +1,62 @@
59+/*
60+ * Copyright (C) 2011-2013 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
61+ *
62+ * SPDX-License-Identifier: GPL-2.0+
63+ */
64+
65+#include <common.h>
66+#include <netdev.h>
67+#include <miiphy.h>
68+#include <switch.h>
69+
70+static struct list_head switch_drivers;
71+static struct list_head switch_devices;
72+
73+void switch_init(void)
74+{
75+ INIT_LIST_HEAD(&switch_drivers);
76+ INIT_LIST_HEAD(&switch_devices);
77+
78+ board_switch_init();
79+}
80+
81+void switch_driver_register(struct switch_driver *drv)
82+{
83+ INIT_LIST_HEAD(&drv->list);
84+ list_add_tail(&drv->list, &switch_drivers);
85+}
86+
87+int switch_device_register(struct switch_device *dev)
88+{
89+ struct switch_driver *drv;
90+
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)) {
94+ dev->drv = drv;
95+
96+ INIT_LIST_HEAD(&dev->list);
97+ list_add_tail(&dev->list, &switch_devices);
98+
99+ return 0;
100+ }
101+ }
102+
103+ return -1;
104+}
105+
106+struct switch_device *switch_connect(struct mii_dev *bus)
107+{
108+ struct switch_device *sw;
109+ int err;
110+
111+ list_for_each_entry(sw, &switch_devices, list) {
112+ sw->bus = bus;
113+
114+ err = sw->drv->probe(sw);
115+ if (!err)
116+ return sw;
117+ }
118+
119+ return NULL;
120+}
121--- /dev/null
122+++ b/include/switch.h
123@@ -0,0 +1,102 @@
124+/*
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.
127+ *
128+ * Copyright (C) 2011-2013 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
129+ */
130+
131+#ifndef __SWITCH_H
132+#define __SWITCH_H
133+
134+#include <linux/list.h>
135+
136+#define SWITCH_NAME_SIZE 32
137+
138+struct switch_device;
139+struct mii_dev;
140+
141+struct switch_driver {
142+ struct list_head list;
143+
144+ /* Switch device name */
145+ const char name[SWITCH_NAME_SIZE];
146+
147+ /*
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.
151+ */
152+ int (*probe) (struct switch_device *dev);
153+
154+ /*
155+ * Called to initialize the switch chip.
156+ */
157+ void (*setup) (struct switch_device *dev);
158+};
159+
160+struct switch_device {
161+ struct list_head list;
162+ struct switch_driver *drv;
163+
164+ /* MII bus the switch chip is connected to */
165+ struct mii_dev *bus;
166+
167+ /* Switch device name */
168+ const char name[SWITCH_NAME_SIZE];
169+
170+ /* Bitmask for board specific setup of used switch ports */
171+ u16 port_mask;
172+
173+ /* Number of switch port that is connected to host CPU */
174+ u16 cpu_port;
175+};
176+
177+/*
178+ * Board specific switch initialization.
179+ *
180+ * Called from switch_init to register the board specific switch_device
181+ * structure.
182+ */
183+extern int board_switch_init(void);
184+
185+/* Initialize switch subsystem */
186+#ifdef CONFIG_SWITCH_MULTI
187+extern void switch_init(void);
188+#else
189+static inline void switch_init(void)
190+{
191+}
192+#endif
193+
194+/* Register a switch driver */
195+extern void switch_driver_register(struct switch_driver *drv);
196+
197+/* Register a switch device */
198+extern int switch_device_register(struct switch_device *dev);
199+
200+/*
201+ * Probe the available switch chips and connect the found one
202+ * with the given MII bus
203+ */
204+#ifdef CONFIG_SWITCH_MULTI
205+extern struct switch_device *switch_connect(struct mii_dev *bus);
206+#else
207+static inline struct switch_device *switch_connect(struct mii_dev *bus)
208+{
209+ return NULL;
210+}
211+#endif
212+
213+/*
214+ * Setup the given switch device
215+ */
216+static inline void switch_setup(struct switch_device *dev)
217+{
218+ if (dev->drv->setup)
219+ dev->drv->setup(dev);
220+}
221+
222+/* Init functions for supported Switch drivers */
223+
224+#endif /* __SWITCH_H */
225+
226--- a/net/eth.c
227+++ b/net/eth.c
228@@ -10,6 +10,7 @@
229 #include <net.h>
230 #include <miiphy.h>
231 #include <phy.h>
232+#include <switch.h>
233
234 void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
235 {
236@@ -287,6 +288,8 @@ int eth_initialize(bd_t *bis)
237 phy_init();
238 #endif
239
240+ switch_init();
241+
242 eth_env_init(bis);
243
244 /*