0009-net-switchlib-add-framework-for-ethernet-switch-driv.patch 5.9 KB

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