b53_priv.h 8.9 KB

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