b53_priv.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/switch.h>
  23. struct b53_device;
  24. struct b53_io_ops {
  25. int (*read8)(struct b53_device *dev, u8 page, u8 reg, u8 *value);
  26. int (*read16)(struct b53_device *dev, u8 page, u8 reg, u16 *value);
  27. int (*read32)(struct b53_device *dev, u8 page, u8 reg, u32 *value);
  28. int (*read48)(struct b53_device *dev, u8 page, u8 reg, u64 *value);
  29. int (*read64)(struct b53_device *dev, u8 page, u8 reg, u64 *value);
  30. int (*write8)(struct b53_device *dev, u8 page, u8 reg, u8 value);
  31. int (*write16)(struct b53_device *dev, u8 page, u8 reg, u16 value);
  32. int (*write32)(struct b53_device *dev, u8 page, u8 reg, u32 value);
  33. int (*write48)(struct b53_device *dev, u8 page, u8 reg, u64 value);
  34. int (*write64)(struct b53_device *dev, u8 page, u8 reg, u64 value);
  35. int (*phy_read16)(struct b53_device *dev, int addr, u8 reg, u16 *value);
  36. int (*phy_write16)(struct b53_device *dev, int addr, u8 reg, u16 value);
  37. };
  38. enum {
  39. BCM5325_DEVICE_ID = 0x25,
  40. BCM5365_DEVICE_ID = 0x65,
  41. BCM5395_DEVICE_ID = 0x95,
  42. BCM5397_DEVICE_ID = 0x97,
  43. BCM5398_DEVICE_ID = 0x98,
  44. BCM53115_DEVICE_ID = 0x53115,
  45. BCM53125_DEVICE_ID = 0x53125,
  46. BCM53128_DEVICE_ID = 0x53128,
  47. BCM63XX_DEVICE_ID = 0x6300,
  48. BCM53010_DEVICE_ID = 0x53010,
  49. BCM53011_DEVICE_ID = 0x53011,
  50. BCM53012_DEVICE_ID = 0x53012,
  51. BCM53018_DEVICE_ID = 0x53018,
  52. BCM53019_DEVICE_ID = 0x53019,
  53. };
  54. #define B53_N_PORTS 9
  55. #define B53_N_PORTS_25 6
  56. struct b53_vlan {
  57. unsigned int members:B53_N_PORTS;
  58. unsigned int untag:B53_N_PORTS;
  59. };
  60. struct b53_port {
  61. unsigned int pvid:12;
  62. };
  63. struct b53_device {
  64. struct switch_dev sw_dev;
  65. struct b53_platform_data *pdata;
  66. struct mutex reg_mutex;
  67. const struct b53_io_ops *ops;
  68. /* chip specific data */
  69. u32 chip_id;
  70. u8 core_rev;
  71. u8 vta_regs[3];
  72. u8 duplex_reg;
  73. u8 jumbo_pm_reg;
  74. u8 jumbo_size_reg;
  75. int reset_gpio;
  76. /* used ports mask */
  77. u16 enabled_ports;
  78. /* connect specific data */
  79. u8 current_page;
  80. struct device *dev;
  81. void *priv;
  82. /* run time configuration */
  83. unsigned enable_vlan:1;
  84. unsigned enable_jumbo:1;
  85. unsigned allow_vid_4095:1;
  86. struct b53_port *ports;
  87. struct b53_vlan *vlans;
  88. char *buf;
  89. };
  90. #define b53_for_each_port(dev, i) \
  91. for (i = 0; i < B53_N_PORTS; i++) \
  92. if (dev->enabled_ports & BIT(i))
  93. static inline int is5325(struct b53_device *dev)
  94. {
  95. return dev->chip_id == BCM5325_DEVICE_ID;
  96. }
  97. static inline int is5365(struct b53_device *dev)
  98. {
  99. #ifdef CONFIG_BCM47XX
  100. return dev->chip_id == BCM5365_DEVICE_ID;
  101. #else
  102. return 0;
  103. #endif
  104. }
  105. static inline int is5397_98(struct b53_device *dev)
  106. {
  107. return dev->chip_id == BCM5397_DEVICE_ID ||
  108. dev->chip_id == BCM5398_DEVICE_ID;
  109. }
  110. static inline int is539x(struct b53_device *dev)
  111. {
  112. return dev->chip_id == BCM5395_DEVICE_ID ||
  113. dev->chip_id == BCM5397_DEVICE_ID ||
  114. dev->chip_id == BCM5398_DEVICE_ID;
  115. }
  116. static inline int is531x5(struct b53_device *dev)
  117. {
  118. return dev->chip_id == BCM53115_DEVICE_ID ||
  119. dev->chip_id == BCM53125_DEVICE_ID ||
  120. dev->chip_id == BCM53128_DEVICE_ID;
  121. }
  122. static inline int is63xx(struct b53_device *dev)
  123. {
  124. #ifdef CONFIG_BCM63XX
  125. return dev->chip_id == BCM63XX_DEVICE_ID;
  126. #else
  127. return 0;
  128. #endif
  129. }
  130. static inline int is5301x(struct b53_device *dev)
  131. {
  132. return dev->chip_id == BCM53010_DEVICE_ID ||
  133. dev->chip_id == BCM53011_DEVICE_ID ||
  134. dev->chip_id == BCM53012_DEVICE_ID ||
  135. dev->chip_id == BCM53018_DEVICE_ID ||
  136. dev->chip_id == BCM53019_DEVICE_ID;
  137. }
  138. #define B53_CPU_PORT_25 5
  139. #define B53_CPU_PORT 8
  140. static inline int is_cpu_port(struct b53_device *dev, int port)
  141. {
  142. return dev->sw_dev.cpu_port == port;
  143. }
  144. static inline struct b53_device *sw_to_b53(struct switch_dev *sw)
  145. {
  146. return container_of(sw, struct b53_device, sw_dev);
  147. }
  148. struct b53_device *b53_switch_alloc(struct device *base, struct b53_io_ops *ops,
  149. void *priv);
  150. int b53_switch_detect(struct b53_device *dev);
  151. int b53_switch_register(struct b53_device *dev);
  152. static inline void b53_switch_remove(struct b53_device *dev)
  153. {
  154. unregister_switch(&dev->sw_dev);
  155. }
  156. static inline int b53_read8(struct b53_device *dev, u8 page, u8 reg, u8 *val)
  157. {
  158. int ret;
  159. mutex_lock(&dev->reg_mutex);
  160. ret = dev->ops->read8(dev, page, reg, val);
  161. mutex_unlock(&dev->reg_mutex);
  162. return ret;
  163. }
  164. static inline int b53_read16(struct b53_device *dev, u8 page, u8 reg, u16 *val)
  165. {
  166. int ret;
  167. mutex_lock(&dev->reg_mutex);
  168. ret = dev->ops->read16(dev, page, reg, val);
  169. mutex_unlock(&dev->reg_mutex);
  170. return ret;
  171. }
  172. static inline int b53_read32(struct b53_device *dev, u8 page, u8 reg, u32 *val)
  173. {
  174. int ret;
  175. mutex_lock(&dev->reg_mutex);
  176. ret = dev->ops->read32(dev, page, reg, val);
  177. mutex_unlock(&dev->reg_mutex);
  178. return ret;
  179. }
  180. static inline int b53_read48(struct b53_device *dev, u8 page, u8 reg, u64 *val)
  181. {
  182. int ret;
  183. mutex_lock(&dev->reg_mutex);
  184. ret = dev->ops->read48(dev, page, reg, val);
  185. mutex_unlock(&dev->reg_mutex);
  186. return ret;
  187. }
  188. static inline int b53_read64(struct b53_device *dev, u8 page, u8 reg, u64 *val)
  189. {
  190. int ret;
  191. mutex_lock(&dev->reg_mutex);
  192. ret = dev->ops->read64(dev, page, reg, val);
  193. mutex_unlock(&dev->reg_mutex);
  194. return ret;
  195. }
  196. static inline int b53_write8(struct b53_device *dev, u8 page, u8 reg, u8 value)
  197. {
  198. int ret;
  199. mutex_lock(&dev->reg_mutex);
  200. ret = dev->ops->write8(dev, page, reg, value);
  201. mutex_unlock(&dev->reg_mutex);
  202. return ret;
  203. }
  204. static inline int b53_write16(struct b53_device *dev, u8 page, u8 reg,
  205. u16 value)
  206. {
  207. int ret;
  208. mutex_lock(&dev->reg_mutex);
  209. ret = dev->ops->write16(dev, page, reg, value);
  210. mutex_unlock(&dev->reg_mutex);
  211. return ret;
  212. }
  213. static inline int b53_write32(struct b53_device *dev, u8 page, u8 reg,
  214. u32 value)
  215. {
  216. int ret;
  217. mutex_lock(&dev->reg_mutex);
  218. ret = dev->ops->write32(dev, page, reg, value);
  219. mutex_unlock(&dev->reg_mutex);
  220. return ret;
  221. }
  222. static inline int b53_write48(struct b53_device *dev, u8 page, u8 reg,
  223. u64 value)
  224. {
  225. int ret;
  226. mutex_lock(&dev->reg_mutex);
  227. ret = dev->ops->write48(dev, page, reg, value);
  228. mutex_unlock(&dev->reg_mutex);
  229. return ret;
  230. }
  231. static inline int b53_write64(struct b53_device *dev, u8 page, u8 reg,
  232. u64 value)
  233. {
  234. int ret;
  235. mutex_lock(&dev->reg_mutex);
  236. ret = dev->ops->write64(dev, page, reg, value);
  237. mutex_unlock(&dev->reg_mutex);
  238. return ret;
  239. }
  240. #ifdef CONFIG_BCM47XX
  241. #include <linux/version.h>
  242. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0))
  243. #include <linux/bcm47xx_nvram.h>
  244. #else
  245. #include <bcm47xx_nvram.h>
  246. #endif
  247. #include <bcm47xx_board.h>
  248. static inline int b53_switch_get_reset_gpio(struct b53_device *dev)
  249. {
  250. enum bcm47xx_board board = bcm47xx_board_get();
  251. switch (board) {
  252. case BCM47XX_BOARD_LINKSYS_WRT300NV11:
  253. case BCM47XX_BOARD_LINKSYS_WRT310NV1:
  254. return 8;
  255. default:
  256. return bcm47xx_nvram_gpio_pin("robo_reset");
  257. }
  258. }
  259. #else
  260. static inline int b53_switch_get_reset_gpio(struct b53_device *dev)
  261. {
  262. return -ENOENT;
  263. }
  264. #endif
  265. #endif