ksz_priv.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Microchip KSZ series switch common definitions
  3. *
  4. * Copyright (C) 2017
  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 __KSZ_PRIV_H
  19. #define __KSZ_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 "ksz_9477_reg.h"
  26. struct ksz_io_ops;
  27. struct vlan_table {
  28. u32 table[3];
  29. };
  30. struct ksz_device {
  31. struct dsa_switch *ds;
  32. struct ksz_platform_data *pdata;
  33. const char *name;
  34. struct mutex reg_mutex; /* register access */
  35. struct mutex stats_mutex; /* status access */
  36. struct mutex alu_mutex; /* ALU access */
  37. struct mutex vlan_mutex; /* vlan access */
  38. const struct ksz_io_ops *ops;
  39. struct device *dev;
  40. void *priv;
  41. /* chip specific data */
  42. u32 chip_id;
  43. int num_vlans;
  44. int num_alus;
  45. int num_statics;
  46. int cpu_port; /* port connected to CPU */
  47. int cpu_ports; /* port bitmap can be cpu port */
  48. int port_cnt;
  49. struct vlan_table *vlan_cache;
  50. u64 mib_value[TOTAL_SWITCH_COUNTER_NUM];
  51. };
  52. struct ksz_io_ops {
  53. int (*read8)(struct ksz_device *dev, u32 reg, u8 *value);
  54. int (*read16)(struct ksz_device *dev, u32 reg, u16 *value);
  55. int (*read24)(struct ksz_device *dev, u32 reg, u32 *value);
  56. int (*read32)(struct ksz_device *dev, u32 reg, u32 *value);
  57. int (*write8)(struct ksz_device *dev, u32 reg, u8 value);
  58. int (*write16)(struct ksz_device *dev, u32 reg, u16 value);
  59. int (*write24)(struct ksz_device *dev, u32 reg, u32 value);
  60. int (*write32)(struct ksz_device *dev, u32 reg, u32 value);
  61. int (*phy_read16)(struct ksz_device *dev, int addr, int reg,
  62. u16 *value);
  63. int (*phy_write16)(struct ksz_device *dev, int addr, int reg,
  64. u16 value);
  65. };
  66. struct ksz_device *ksz_switch_alloc(struct device *base,
  67. const struct ksz_io_ops *ops, void *priv);
  68. int ksz_switch_detect(struct ksz_device *dev);
  69. int ksz_switch_register(struct ksz_device *dev);
  70. void ksz_switch_remove(struct ksz_device *dev);
  71. static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
  72. {
  73. int ret;
  74. mutex_lock(&dev->reg_mutex);
  75. ret = dev->ops->read8(dev, reg, val);
  76. mutex_unlock(&dev->reg_mutex);
  77. return ret;
  78. }
  79. static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
  80. {
  81. int ret;
  82. mutex_lock(&dev->reg_mutex);
  83. ret = dev->ops->read16(dev, reg, val);
  84. mutex_unlock(&dev->reg_mutex);
  85. return ret;
  86. }
  87. static inline int ksz_read24(struct ksz_device *dev, u32 reg, u32 *val)
  88. {
  89. int ret;
  90. mutex_lock(&dev->reg_mutex);
  91. ret = dev->ops->read24(dev, reg, val);
  92. mutex_unlock(&dev->reg_mutex);
  93. return ret;
  94. }
  95. static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
  96. {
  97. int ret;
  98. mutex_lock(&dev->reg_mutex);
  99. ret = dev->ops->read32(dev, reg, val);
  100. mutex_unlock(&dev->reg_mutex);
  101. return ret;
  102. }
  103. static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
  104. {
  105. int ret;
  106. mutex_lock(&dev->reg_mutex);
  107. ret = dev->ops->write8(dev, reg, value);
  108. mutex_unlock(&dev->reg_mutex);
  109. return ret;
  110. }
  111. static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
  112. {
  113. int ret;
  114. mutex_lock(&dev->reg_mutex);
  115. ret = dev->ops->write16(dev, reg, value);
  116. mutex_unlock(&dev->reg_mutex);
  117. return ret;
  118. }
  119. static inline int ksz_write24(struct ksz_device *dev, u32 reg, u32 value)
  120. {
  121. int ret;
  122. mutex_lock(&dev->reg_mutex);
  123. ret = dev->ops->write24(dev, reg, value);
  124. mutex_unlock(&dev->reg_mutex);
  125. return ret;
  126. }
  127. static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
  128. {
  129. int ret;
  130. mutex_lock(&dev->reg_mutex);
  131. ret = dev->ops->write32(dev, reg, value);
  132. mutex_unlock(&dev->reg_mutex);
  133. return ret;
  134. }
  135. static inline void ksz_pread8(struct ksz_device *dev, int port, int offset,
  136. u8 *data)
  137. {
  138. ksz_read8(dev, PORT_CTRL_ADDR(port, offset), data);
  139. }
  140. static inline void ksz_pread16(struct ksz_device *dev, int port, int offset,
  141. u16 *data)
  142. {
  143. ksz_read16(dev, PORT_CTRL_ADDR(port, offset), data);
  144. }
  145. static inline void ksz_pread32(struct ksz_device *dev, int port, int offset,
  146. u32 *data)
  147. {
  148. ksz_read32(dev, PORT_CTRL_ADDR(port, offset), data);
  149. }
  150. static inline void ksz_pwrite8(struct ksz_device *dev, int port, int offset,
  151. u8 data)
  152. {
  153. ksz_write8(dev, PORT_CTRL_ADDR(port, offset), data);
  154. }
  155. static inline void ksz_pwrite16(struct ksz_device *dev, int port, int offset,
  156. u16 data)
  157. {
  158. ksz_write16(dev, PORT_CTRL_ADDR(port, offset), data);
  159. }
  160. static inline void ksz_pwrite32(struct ksz_device *dev, int port, int offset,
  161. u32 data)
  162. {
  163. ksz_write32(dev, PORT_CTRL_ADDR(port, offset), data);
  164. }
  165. #endif