b53_priv.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * B53 common definitions
  3. *
  4. * Copyright (C) 2011-2013 Jonas Gorski <jogo@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #ifndef __B53_PRIV_H
  19. #define __B53_PRIV_H
  20. #include <linux/kernel.h>
  21. #include <linux/mutex.h>
  22. #include <linux/phy.h>
  23. #include <linux/etherdevice.h>
  24. #include <net/dsa.h>
  25. #include "b53_regs.h"
  26. struct b53_device;
  27. struct net_device;
  28. struct b53_io_ops {
  29. int (*read8)(struct b53_device *dev, u8 page, u8 reg, u8 *value);
  30. int (*read16)(struct b53_device *dev, u8 page, u8 reg, u16 *value);
  31. int (*read32)(struct b53_device *dev, u8 page, u8 reg, u32 *value);
  32. int (*read48)(struct b53_device *dev, u8 page, u8 reg, u64 *value);
  33. int (*read64)(struct b53_device *dev, u8 page, u8 reg, u64 *value);
  34. int (*write8)(struct b53_device *dev, u8 page, u8 reg, u8 value);
  35. int (*write16)(struct b53_device *dev, u8 page, u8 reg, u16 value);
  36. int (*write32)(struct b53_device *dev, u8 page, u8 reg, u32 value);
  37. int (*write48)(struct b53_device *dev, u8 page, u8 reg, u64 value);
  38. int (*write64)(struct b53_device *dev, u8 page, u8 reg, u64 value);
  39. int (*phy_read16)(struct b53_device *dev, int addr, int reg, u16 *value);
  40. int (*phy_write16)(struct b53_device *dev, int addr, int reg, u16 value);
  41. };
  42. enum {
  43. BCM5325_DEVICE_ID = 0x25,
  44. BCM5365_DEVICE_ID = 0x65,
  45. BCM5389_DEVICE_ID = 0x89,
  46. BCM5395_DEVICE_ID = 0x95,
  47. BCM5397_DEVICE_ID = 0x97,
  48. BCM5398_DEVICE_ID = 0x98,
  49. BCM53115_DEVICE_ID = 0x53115,
  50. BCM53125_DEVICE_ID = 0x53125,
  51. BCM53128_DEVICE_ID = 0x53128,
  52. BCM63XX_DEVICE_ID = 0x6300,
  53. BCM53010_DEVICE_ID = 0x53010,
  54. BCM53011_DEVICE_ID = 0x53011,
  55. BCM53012_DEVICE_ID = 0x53012,
  56. BCM53018_DEVICE_ID = 0x53018,
  57. BCM53019_DEVICE_ID = 0x53019,
  58. BCM58XX_DEVICE_ID = 0x5800,
  59. BCM583XX_DEVICE_ID = 0x58300,
  60. BCM7445_DEVICE_ID = 0x7445,
  61. BCM7278_DEVICE_ID = 0x7278,
  62. };
  63. #define B53_N_PORTS 9
  64. #define B53_N_PORTS_25 6
  65. struct b53_port {
  66. u16 vlan_ctl_mask;
  67. struct ethtool_eee eee;
  68. u16 pvid;
  69. };
  70. struct b53_vlan {
  71. u16 members;
  72. u16 untag;
  73. bool valid;
  74. };
  75. struct b53_device {
  76. struct dsa_switch *ds;
  77. struct b53_platform_data *pdata;
  78. const char *name;
  79. struct mutex reg_mutex;
  80. struct mutex stats_mutex;
  81. const struct b53_io_ops *ops;
  82. /* chip specific data */
  83. u32 chip_id;
  84. u8 core_rev;
  85. u8 vta_regs[3];
  86. u8 duplex_reg;
  87. u8 jumbo_pm_reg;
  88. u8 jumbo_size_reg;
  89. int reset_gpio;
  90. u8 num_arl_entries;
  91. /* used ports mask */
  92. u16 enabled_ports;
  93. unsigned int cpu_port;
  94. /* connect specific data */
  95. u8 current_page;
  96. struct device *dev;
  97. /* Master MDIO bus we got probed from */
  98. struct mii_bus *bus;
  99. void *priv;
  100. /* run time configuration */
  101. bool enable_jumbo;
  102. unsigned int num_vlans;
  103. struct b53_vlan *vlans;
  104. bool vlan_enabled;
  105. bool vlan_filtering_enabled;
  106. unsigned int num_ports;
  107. struct b53_port *ports;
  108. };
  109. #define b53_for_each_port(dev, i) \
  110. for (i = 0; i < B53_N_PORTS; i++) \
  111. if (dev->enabled_ports & BIT(i))
  112. static inline int is5325(struct b53_device *dev)
  113. {
  114. return dev->chip_id == BCM5325_DEVICE_ID;
  115. }
  116. static inline int is5365(struct b53_device *dev)
  117. {
  118. #ifdef CONFIG_BCM47XX
  119. return dev->chip_id == BCM5365_DEVICE_ID;
  120. #else
  121. return 0;
  122. #endif
  123. }
  124. static inline int is5397_98(struct b53_device *dev)
  125. {
  126. return dev->chip_id == BCM5397_DEVICE_ID ||
  127. dev->chip_id == BCM5398_DEVICE_ID;
  128. }
  129. static inline int is539x(struct b53_device *dev)
  130. {
  131. return dev->chip_id == BCM5395_DEVICE_ID ||
  132. dev->chip_id == BCM5397_DEVICE_ID ||
  133. dev->chip_id == BCM5398_DEVICE_ID;
  134. }
  135. static inline int is531x5(struct b53_device *dev)
  136. {
  137. return dev->chip_id == BCM53115_DEVICE_ID ||
  138. dev->chip_id == BCM53125_DEVICE_ID ||
  139. dev->chip_id == BCM53128_DEVICE_ID;
  140. }
  141. static inline int is63xx(struct b53_device *dev)
  142. {
  143. #ifdef CONFIG_BCM63XX
  144. return dev->chip_id == BCM63XX_DEVICE_ID;
  145. #else
  146. return 0;
  147. #endif
  148. }
  149. static inline int is5301x(struct b53_device *dev)
  150. {
  151. return dev->chip_id == BCM53010_DEVICE_ID ||
  152. dev->chip_id == BCM53011_DEVICE_ID ||
  153. dev->chip_id == BCM53012_DEVICE_ID ||
  154. dev->chip_id == BCM53018_DEVICE_ID ||
  155. dev->chip_id == BCM53019_DEVICE_ID;
  156. }
  157. static inline int is58xx(struct b53_device *dev)
  158. {
  159. return dev->chip_id == BCM58XX_DEVICE_ID ||
  160. dev->chip_id == BCM583XX_DEVICE_ID ||
  161. dev->chip_id == BCM7445_DEVICE_ID ||
  162. dev->chip_id == BCM7278_DEVICE_ID;
  163. }
  164. #define B53_CPU_PORT_25 5
  165. #define B53_CPU_PORT 8
  166. struct b53_device *b53_switch_alloc(struct device *base,
  167. const struct b53_io_ops *ops,
  168. void *priv);
  169. int b53_switch_detect(struct b53_device *dev);
  170. int b53_switch_register(struct b53_device *dev);
  171. static inline void b53_switch_remove(struct b53_device *dev)
  172. {
  173. dsa_unregister_switch(dev->ds);
  174. }
  175. #define b53_build_op(type_op_size, val_type) \
  176. static inline int b53_##type_op_size(struct b53_device *dev, u8 page, \
  177. u8 reg, val_type val) \
  178. { \
  179. int ret; \
  180. \
  181. mutex_lock(&dev->reg_mutex); \
  182. ret = dev->ops->type_op_size(dev, page, reg, val); \
  183. mutex_unlock(&dev->reg_mutex); \
  184. \
  185. return ret; \
  186. }
  187. b53_build_op(read8, u8 *);
  188. b53_build_op(read16, u16 *);
  189. b53_build_op(read32, u32 *);
  190. b53_build_op(read48, u64 *);
  191. b53_build_op(read64, u64 *);
  192. b53_build_op(write8, u8);
  193. b53_build_op(write16, u16);
  194. b53_build_op(write32, u32);
  195. b53_build_op(write48, u64);
  196. b53_build_op(write64, u64);
  197. struct b53_arl_entry {
  198. u8 port;
  199. u8 mac[ETH_ALEN];
  200. u16 vid;
  201. u8 is_valid:1;
  202. u8 is_age:1;
  203. u8 is_static:1;
  204. };
  205. static inline void b53_arl_to_entry(struct b53_arl_entry *ent,
  206. u64 mac_vid, u32 fwd_entry)
  207. {
  208. memset(ent, 0, sizeof(*ent));
  209. ent->port = fwd_entry & ARLTBL_DATA_PORT_ID_MASK;
  210. ent->is_valid = !!(fwd_entry & ARLTBL_VALID);
  211. ent->is_age = !!(fwd_entry & ARLTBL_AGE);
  212. ent->is_static = !!(fwd_entry & ARLTBL_STATIC);
  213. u64_to_ether_addr(mac_vid, ent->mac);
  214. ent->vid = mac_vid >> ARLTBL_VID_S;
  215. }
  216. static inline void b53_arl_from_entry(u64 *mac_vid, u32 *fwd_entry,
  217. const struct b53_arl_entry *ent)
  218. {
  219. *mac_vid = ether_addr_to_u64(ent->mac);
  220. *mac_vid |= (u64)(ent->vid & ARLTBL_VID_MASK) << ARLTBL_VID_S;
  221. *fwd_entry = ent->port & ARLTBL_DATA_PORT_ID_MASK;
  222. if (ent->is_valid)
  223. *fwd_entry |= ARLTBL_VALID;
  224. if (ent->is_static)
  225. *fwd_entry |= ARLTBL_STATIC;
  226. if (ent->is_age)
  227. *fwd_entry |= ARLTBL_AGE;
  228. }
  229. #ifdef CONFIG_BCM47XX
  230. #include <linux/bcm47xx_nvram.h>
  231. #include <bcm47xx_board.h>
  232. static inline int b53_switch_get_reset_gpio(struct b53_device *dev)
  233. {
  234. enum bcm47xx_board board = bcm47xx_board_get();
  235. switch (board) {
  236. case BCM47XX_BOARD_LINKSYS_WRT300NV11:
  237. case BCM47XX_BOARD_LINKSYS_WRT310NV1:
  238. return 8;
  239. default:
  240. return bcm47xx_nvram_gpio_pin("robo_reset");
  241. }
  242. }
  243. #else
  244. static inline int b53_switch_get_reset_gpio(struct b53_device *dev)
  245. {
  246. return -ENOENT;
  247. }
  248. #endif
  249. /* Exported functions towards other drivers */
  250. void b53_imp_vlan_setup(struct dsa_switch *ds, int cpu_port);
  251. int b53_configure_vlan(struct dsa_switch *ds);
  252. void b53_get_strings(struct dsa_switch *ds, int port, u32 stringset,
  253. uint8_t *data);
  254. void b53_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data);
  255. int b53_get_sset_count(struct dsa_switch *ds, int port, int sset);
  256. void b53_get_ethtool_phy_stats(struct dsa_switch *ds, int port, uint64_t *data);
  257. int b53_br_join(struct dsa_switch *ds, int port, struct net_device *bridge);
  258. void b53_br_leave(struct dsa_switch *ds, int port, struct net_device *bridge);
  259. void b53_br_set_stp_state(struct dsa_switch *ds, int port, u8 state);
  260. void b53_br_fast_age(struct dsa_switch *ds, int port);
  261. int b53_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering);
  262. int b53_vlan_prepare(struct dsa_switch *ds, int port,
  263. const struct switchdev_obj_port_vlan *vlan);
  264. void b53_vlan_add(struct dsa_switch *ds, int port,
  265. const struct switchdev_obj_port_vlan *vlan);
  266. int b53_vlan_del(struct dsa_switch *ds, int port,
  267. const struct switchdev_obj_port_vlan *vlan);
  268. int b53_fdb_add(struct dsa_switch *ds, int port,
  269. const unsigned char *addr, u16 vid);
  270. int b53_fdb_del(struct dsa_switch *ds, int port,
  271. const unsigned char *addr, u16 vid);
  272. int b53_fdb_dump(struct dsa_switch *ds, int port,
  273. dsa_fdb_dump_cb_t *cb, void *data);
  274. int b53_mirror_add(struct dsa_switch *ds, int port,
  275. struct dsa_mall_mirror_tc_entry *mirror, bool ingress);
  276. enum dsa_tag_protocol b53_get_tag_protocol(struct dsa_switch *ds, int port);
  277. void b53_mirror_del(struct dsa_switch *ds, int port,
  278. struct dsa_mall_mirror_tc_entry *mirror);
  279. int b53_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy);
  280. void b53_disable_port(struct dsa_switch *ds, int port, struct phy_device *phy);
  281. void b53_brcm_hdr_setup(struct dsa_switch *ds, int port);
  282. void b53_eee_enable_set(struct dsa_switch *ds, int port, bool enable);
  283. int b53_eee_init(struct dsa_switch *ds, int port, struct phy_device *phy);
  284. int b53_get_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e);
  285. int b53_set_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e);
  286. #endif