b53_priv.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 <net/dsa.h>
  24. #include "b53_regs.h"
  25. struct b53_device;
  26. struct net_device;
  27. struct b53_io_ops {
  28. int (*read8)(struct b53_device *dev, u8 page, u8 reg, u8 *value);
  29. int (*read16)(struct b53_device *dev, u8 page, u8 reg, u16 *value);
  30. int (*read32)(struct b53_device *dev, u8 page, u8 reg, u32 *value);
  31. int (*read48)(struct b53_device *dev, u8 page, u8 reg, u64 *value);
  32. int (*read64)(struct b53_device *dev, u8 page, u8 reg, u64 *value);
  33. int (*write8)(struct b53_device *dev, u8 page, u8 reg, u8 value);
  34. int (*write16)(struct b53_device *dev, u8 page, u8 reg, u16 value);
  35. int (*write32)(struct b53_device *dev, u8 page, u8 reg, u32 value);
  36. int (*write48)(struct b53_device *dev, u8 page, u8 reg, u64 value);
  37. int (*write64)(struct b53_device *dev, u8 page, u8 reg, u64 value);
  38. int (*phy_read16)(struct b53_device *dev, int addr, int reg, u16 *value);
  39. int (*phy_write16)(struct b53_device *dev, int addr, int reg, u16 value);
  40. };
  41. enum {
  42. BCM5325_DEVICE_ID = 0x25,
  43. BCM5365_DEVICE_ID = 0x65,
  44. BCM5395_DEVICE_ID = 0x95,
  45. BCM5397_DEVICE_ID = 0x97,
  46. BCM5398_DEVICE_ID = 0x98,
  47. BCM53115_DEVICE_ID = 0x53115,
  48. BCM53125_DEVICE_ID = 0x53125,
  49. BCM53128_DEVICE_ID = 0x53128,
  50. BCM63XX_DEVICE_ID = 0x6300,
  51. BCM53010_DEVICE_ID = 0x53010,
  52. BCM53011_DEVICE_ID = 0x53011,
  53. BCM53012_DEVICE_ID = 0x53012,
  54. BCM53018_DEVICE_ID = 0x53018,
  55. BCM53019_DEVICE_ID = 0x53019,
  56. BCM58XX_DEVICE_ID = 0x5800,
  57. };
  58. #define B53_N_PORTS 9
  59. #define B53_N_PORTS_25 6
  60. struct b53_port {
  61. u16 vlan_ctl_mask;
  62. struct net_device *bridge_dev;
  63. };
  64. struct b53_vlan {
  65. u16 members;
  66. u16 untag;
  67. bool valid;
  68. };
  69. struct b53_device {
  70. struct dsa_switch *ds;
  71. struct b53_platform_data *pdata;
  72. const char *name;
  73. struct mutex reg_mutex;
  74. struct mutex stats_mutex;
  75. const struct b53_io_ops *ops;
  76. /* chip specific data */
  77. u32 chip_id;
  78. u8 core_rev;
  79. u8 vta_regs[3];
  80. u8 duplex_reg;
  81. u8 jumbo_pm_reg;
  82. u8 jumbo_size_reg;
  83. int reset_gpio;
  84. u8 num_arl_entries;
  85. /* used ports mask */
  86. u16 enabled_ports;
  87. unsigned int cpu_port;
  88. /* connect specific data */
  89. u8 current_page;
  90. struct device *dev;
  91. /* Master MDIO bus we got probed from */
  92. struct mii_bus *bus;
  93. void *priv;
  94. /* run time configuration */
  95. bool enable_jumbo;
  96. unsigned int num_vlans;
  97. struct b53_vlan *vlans;
  98. unsigned int num_ports;
  99. struct b53_port *ports;
  100. };
  101. #define b53_for_each_port(dev, i) \
  102. for (i = 0; i < B53_N_PORTS; i++) \
  103. if (dev->enabled_ports & BIT(i))
  104. static inline int is5325(struct b53_device *dev)
  105. {
  106. return dev->chip_id == BCM5325_DEVICE_ID;
  107. }
  108. static inline int is5365(struct b53_device *dev)
  109. {
  110. #ifdef CONFIG_BCM47XX
  111. return dev->chip_id == BCM5365_DEVICE_ID;
  112. #else
  113. return 0;
  114. #endif
  115. }
  116. static inline int is5397_98(struct b53_device *dev)
  117. {
  118. return dev->chip_id == BCM5397_DEVICE_ID ||
  119. dev->chip_id == BCM5398_DEVICE_ID;
  120. }
  121. static inline int is539x(struct b53_device *dev)
  122. {
  123. return dev->chip_id == BCM5395_DEVICE_ID ||
  124. dev->chip_id == BCM5397_DEVICE_ID ||
  125. dev->chip_id == BCM5398_DEVICE_ID;
  126. }
  127. static inline int is531x5(struct b53_device *dev)
  128. {
  129. return dev->chip_id == BCM53115_DEVICE_ID ||
  130. dev->chip_id == BCM53125_DEVICE_ID ||
  131. dev->chip_id == BCM53128_DEVICE_ID;
  132. }
  133. static inline int is63xx(struct b53_device *dev)
  134. {
  135. #ifdef CONFIG_BCM63XX
  136. return dev->chip_id == BCM63XX_DEVICE_ID;
  137. #else
  138. return 0;
  139. #endif
  140. }
  141. static inline int is5301x(struct b53_device *dev)
  142. {
  143. return dev->chip_id == BCM53010_DEVICE_ID ||
  144. dev->chip_id == BCM53011_DEVICE_ID ||
  145. dev->chip_id == BCM53012_DEVICE_ID ||
  146. dev->chip_id == BCM53018_DEVICE_ID ||
  147. dev->chip_id == BCM53019_DEVICE_ID;
  148. }
  149. #define B53_CPU_PORT_25 5
  150. #define B53_CPU_PORT 8
  151. static inline int is_cpu_port(struct b53_device *dev, int port)
  152. {
  153. return dev->cpu_port;
  154. }
  155. struct b53_device *b53_switch_alloc(struct device *base, struct b53_io_ops *ops,
  156. void *priv);
  157. int b53_switch_detect(struct b53_device *dev);
  158. int b53_switch_register(struct b53_device *dev);
  159. static inline void b53_switch_remove(struct b53_device *dev)
  160. {
  161. dsa_unregister_switch(dev->ds);
  162. }
  163. static inline int b53_read8(struct b53_device *dev, u8 page, u8 reg, u8 *val)
  164. {
  165. int ret;
  166. mutex_lock(&dev->reg_mutex);
  167. ret = dev->ops->read8(dev, page, reg, val);
  168. mutex_unlock(&dev->reg_mutex);
  169. return ret;
  170. }
  171. static inline int b53_read16(struct b53_device *dev, u8 page, u8 reg, u16 *val)
  172. {
  173. int ret;
  174. mutex_lock(&dev->reg_mutex);
  175. ret = dev->ops->read16(dev, page, reg, val);
  176. mutex_unlock(&dev->reg_mutex);
  177. return ret;
  178. }
  179. static inline int b53_read32(struct b53_device *dev, u8 page, u8 reg, u32 *val)
  180. {
  181. int ret;
  182. mutex_lock(&dev->reg_mutex);
  183. ret = dev->ops->read32(dev, page, reg, val);
  184. mutex_unlock(&dev->reg_mutex);
  185. return ret;
  186. }
  187. static inline int b53_read48(struct b53_device *dev, u8 page, u8 reg, u64 *val)
  188. {
  189. int ret;
  190. mutex_lock(&dev->reg_mutex);
  191. ret = dev->ops->read48(dev, page, reg, val);
  192. mutex_unlock(&dev->reg_mutex);
  193. return ret;
  194. }
  195. static inline int b53_read64(struct b53_device *dev, u8 page, u8 reg, u64 *val)
  196. {
  197. int ret;
  198. mutex_lock(&dev->reg_mutex);
  199. ret = dev->ops->read64(dev, page, reg, val);
  200. mutex_unlock(&dev->reg_mutex);
  201. return ret;
  202. }
  203. static inline int b53_write8(struct b53_device *dev, u8 page, u8 reg, u8 value)
  204. {
  205. int ret;
  206. mutex_lock(&dev->reg_mutex);
  207. ret = dev->ops->write8(dev, page, reg, value);
  208. mutex_unlock(&dev->reg_mutex);
  209. return ret;
  210. }
  211. static inline int b53_write16(struct b53_device *dev, u8 page, u8 reg,
  212. u16 value)
  213. {
  214. int ret;
  215. mutex_lock(&dev->reg_mutex);
  216. ret = dev->ops->write16(dev, page, reg, value);
  217. mutex_unlock(&dev->reg_mutex);
  218. return ret;
  219. }
  220. static inline int b53_write32(struct b53_device *dev, u8 page, u8 reg,
  221. u32 value)
  222. {
  223. int ret;
  224. mutex_lock(&dev->reg_mutex);
  225. ret = dev->ops->write32(dev, page, reg, value);
  226. mutex_unlock(&dev->reg_mutex);
  227. return ret;
  228. }
  229. static inline int b53_write48(struct b53_device *dev, u8 page, u8 reg,
  230. u64 value)
  231. {
  232. int ret;
  233. mutex_lock(&dev->reg_mutex);
  234. ret = dev->ops->write48(dev, page, reg, value);
  235. mutex_unlock(&dev->reg_mutex);
  236. return ret;
  237. }
  238. static inline int b53_write64(struct b53_device *dev, u8 page, u8 reg,
  239. u64 value)
  240. {
  241. int ret;
  242. mutex_lock(&dev->reg_mutex);
  243. ret = dev->ops->write64(dev, page, reg, value);
  244. mutex_unlock(&dev->reg_mutex);
  245. return ret;
  246. }
  247. struct b53_arl_entry {
  248. u8 port;
  249. u8 mac[ETH_ALEN];
  250. u16 vid;
  251. u8 is_valid:1;
  252. u8 is_age:1;
  253. u8 is_static:1;
  254. };
  255. static inline void b53_mac_from_u64(u64 src, u8 *dst)
  256. {
  257. unsigned int i;
  258. for (i = 0; i < ETH_ALEN; i++)
  259. dst[ETH_ALEN - 1 - i] = (src >> (8 * i)) & 0xff;
  260. }
  261. static inline u64 b53_mac_to_u64(const u8 *src)
  262. {
  263. unsigned int i;
  264. u64 dst = 0;
  265. for (i = 0; i < ETH_ALEN; i++)
  266. dst |= (u64)src[ETH_ALEN - 1 - i] << (8 * i);
  267. return dst;
  268. }
  269. static inline void b53_arl_to_entry(struct b53_arl_entry *ent,
  270. u64 mac_vid, u32 fwd_entry)
  271. {
  272. memset(ent, 0, sizeof(*ent));
  273. ent->port = fwd_entry & ARLTBL_DATA_PORT_ID_MASK;
  274. ent->is_valid = !!(fwd_entry & ARLTBL_VALID);
  275. ent->is_age = !!(fwd_entry & ARLTBL_AGE);
  276. ent->is_static = !!(fwd_entry & ARLTBL_STATIC);
  277. b53_mac_from_u64(mac_vid, ent->mac);
  278. ent->vid = mac_vid >> ARLTBL_VID_S;
  279. }
  280. static inline void b53_arl_from_entry(u64 *mac_vid, u32 *fwd_entry,
  281. const struct b53_arl_entry *ent)
  282. {
  283. *mac_vid = b53_mac_to_u64(ent->mac);
  284. *mac_vid |= (u64)(ent->vid & ARLTBL_VID_MASK) << ARLTBL_VID_S;
  285. *fwd_entry = ent->port & ARLTBL_DATA_PORT_ID_MASK;
  286. if (ent->is_valid)
  287. *fwd_entry |= ARLTBL_VALID;
  288. if (ent->is_static)
  289. *fwd_entry |= ARLTBL_STATIC;
  290. if (ent->is_age)
  291. *fwd_entry |= ARLTBL_AGE;
  292. }
  293. #ifdef CONFIG_BCM47XX
  294. #include <linux/version.h>
  295. #include <linux/bcm47xx_nvram.h>
  296. #include <bcm47xx_board.h>
  297. static inline int b53_switch_get_reset_gpio(struct b53_device *dev)
  298. {
  299. enum bcm47xx_board board = bcm47xx_board_get();
  300. switch (board) {
  301. case BCM47XX_BOARD_LINKSYS_WRT300NV11:
  302. case BCM47XX_BOARD_LINKSYS_WRT310NV1:
  303. return 8;
  304. default:
  305. return bcm47xx_nvram_gpio_pin("robo_reset");
  306. }
  307. }
  308. #else
  309. static inline int b53_switch_get_reset_gpio(struct b53_device *dev)
  310. {
  311. return -ENOENT;
  312. }
  313. #endif
  314. #endif