b53_priv.h 8.9 KB

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