mv88e6063.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * net/dsa/mv88e6063.c - Driver for Marvell 88e6063 switch chips
  3. * Copyright (c) 2009 Gabor Juhos <juhosg@openwrt.org>
  4. *
  5. * This driver was base on: net/dsa/mv88e6060.c
  6. * net/dsa/mv88e6063.c - Driver for Marvell 88e6060 switch chips
  7. * Copyright (c) 2008-2009 Marvell Semiconductor
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. #include <linux/version.h>
  15. #include <linux/list.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/phy.h>
  18. #include <net/dsa.h>
  19. #include <linux/version.h>
  20. #define REG_BASE 0x10
  21. #define REG_PHY(p) (REG_BASE + (p))
  22. #define REG_PORT(p) (REG_BASE + 8 + (p))
  23. #define REG_GLOBAL (REG_BASE + 0x0f)
  24. #define NUM_PORTS 7
  25. static int reg_read(struct dsa_switch *ds, int addr, int reg)
  26. {
  27. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0)
  28. struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
  29. return mdiobus_read(bus, addr, reg);
  30. #else
  31. struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->dev);
  32. return mdiobus_read(bus, addr, reg);
  33. #endif
  34. }
  35. #define REG_READ(addr, reg) \
  36. ({ \
  37. int __ret; \
  38. \
  39. __ret = reg_read(ds, addr, reg); \
  40. if (__ret < 0) \
  41. return __ret; \
  42. __ret; \
  43. })
  44. static int reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
  45. {
  46. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0)
  47. struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
  48. return mdiobus_write(bus, addr, reg, val);
  49. #else
  50. struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->dev);
  51. return mdiobus_write(bus, addr, reg, val);
  52. #endif
  53. }
  54. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0)
  55. static enum dsa_tag_protocol mv88e6063_get_tag_protocol(struct dsa_switch *ds)
  56. {
  57. return DSA_TAG_PROTO_TRAILER;
  58. }
  59. #endif
  60. #define REG_WRITE(addr, reg, val) \
  61. ({ \
  62. int __ret; \
  63. \
  64. __ret = reg_write(ds, addr, reg, val); \
  65. if (__ret < 0) \
  66. return __ret; \
  67. })
  68. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0)
  69. static char *mv88e6063_drv_probe(struct device *host_dev, int sw_addr)
  70. #else
  71. static const char *mv88e6063_drv_probe(struct device *dsa_dev,
  72. struct device *host_dev, int sw_addr,
  73. void **_priv)
  74. #endif
  75. {
  76. struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
  77. int ret;
  78. if (!bus)
  79. return NULL;
  80. ret = mdiobus_read(bus, REG_PORT(0), 0x03);
  81. if (ret >= 0) {
  82. ret &= 0xfff0;
  83. if (ret == 0x1530)
  84. return "Marvell 88E6063";
  85. }
  86. return NULL;
  87. }
  88. static int mv88e6063_switch_reset(struct dsa_switch *ds)
  89. {
  90. int i;
  91. int ret;
  92. /*
  93. * Set all ports to the disabled state.
  94. */
  95. for (i = 0; i < NUM_PORTS; i++) {
  96. ret = REG_READ(REG_PORT(i), 0x04);
  97. REG_WRITE(REG_PORT(i), 0x04, ret & 0xfffc);
  98. }
  99. /*
  100. * Wait for transmit queues to drain.
  101. */
  102. msleep(2);
  103. /*
  104. * Reset the switch.
  105. */
  106. REG_WRITE(REG_GLOBAL, 0x0a, 0xa130);
  107. /*
  108. * Wait up to one second for reset to complete.
  109. */
  110. for (i = 0; i < 1000; i++) {
  111. ret = REG_READ(REG_GLOBAL, 0x00);
  112. if ((ret & 0x8000) == 0x0000)
  113. break;
  114. msleep(1);
  115. }
  116. if (i == 1000)
  117. return -ETIMEDOUT;
  118. return 0;
  119. }
  120. static int mv88e6063_setup_global(struct dsa_switch *ds)
  121. {
  122. /*
  123. * Disable discarding of frames with excessive collisions,
  124. * set the maximum frame size to 1536 bytes, and mask all
  125. * interrupt sources.
  126. */
  127. REG_WRITE(REG_GLOBAL, 0x04, 0x0800);
  128. /*
  129. * Enable automatic address learning, set the address
  130. * database size to 1024 entries, and set the default aging
  131. * time to 5 minutes.
  132. */
  133. REG_WRITE(REG_GLOBAL, 0x0a, 0x2130);
  134. return 0;
  135. }
  136. static int mv88e6063_setup_port(struct dsa_switch *ds, int p)
  137. {
  138. int addr = REG_PORT(p);
  139. /*
  140. * Do not force flow control, disable Ingress and Egress
  141. * Header tagging, disable VLAN tunneling, and set the port
  142. * state to Forwarding. Additionally, if this is the CPU
  143. * port, enable Ingress and Egress Trailer tagging mode.
  144. */
  145. REG_WRITE(addr, 0x04, dsa_is_cpu_port(ds, p) ? 0x4103 : 0x0003);
  146. /*
  147. * Port based VLAN map: give each port its own address
  148. * database, allow the CPU port to talk to each of the 'real'
  149. * ports, and allow each of the 'real' ports to only talk to
  150. * the CPU port.
  151. */
  152. REG_WRITE(addr, 0x06,
  153. ((p & 0xf) << 12) |
  154. (dsa_is_cpu_port(ds, p) ?
  155. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0)
  156. ds->phys_port_mask :
  157. #else
  158. ds->enabled_port_mask :
  159. #endif
  160. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
  161. (1 << ds->dst->cpu_port)));
  162. #else
  163. (1 << ds->dst->cpu_dp->index)));
  164. #endif
  165. /*
  166. * Port Association Vector: when learning source addresses
  167. * of packets, add the address to the address database using
  168. * a port bitmap that has only the bit for this port set and
  169. * the other bits clear.
  170. */
  171. REG_WRITE(addr, 0x0b, 1 << p);
  172. return 0;
  173. }
  174. static int mv88e6063_setup(struct dsa_switch *ds)
  175. {
  176. int i;
  177. int ret;
  178. ret = mv88e6063_switch_reset(ds);
  179. if (ret < 0)
  180. return ret;
  181. /* @@@ initialise atu */
  182. ret = mv88e6063_setup_global(ds);
  183. if (ret < 0)
  184. return ret;
  185. for (i = 0; i < NUM_PORTS; i++) {
  186. ret = mv88e6063_setup_port(ds, i);
  187. if (ret < 0)
  188. return ret;
  189. }
  190. return 0;
  191. }
  192. static int mv88e6063_set_addr(struct dsa_switch *ds, u8 *addr)
  193. {
  194. REG_WRITE(REG_GLOBAL, 0x01, (addr[0] << 8) | addr[1]);
  195. REG_WRITE(REG_GLOBAL, 0x02, (addr[2] << 8) | addr[3]);
  196. REG_WRITE(REG_GLOBAL, 0x03, (addr[4] << 8) | addr[5]);
  197. return 0;
  198. }
  199. static int mv88e6063_port_to_phy_addr(int port)
  200. {
  201. if (port >= 0 && port <= NUM_PORTS)
  202. return REG_PHY(port);
  203. return -1;
  204. }
  205. static int mv88e6063_phy_read(struct dsa_switch *ds, int port, int regnum)
  206. {
  207. int addr;
  208. addr = mv88e6063_port_to_phy_addr(port);
  209. if (addr == -1)
  210. return 0xffff;
  211. return reg_read(ds, addr, regnum);
  212. }
  213. static int
  214. mv88e6063_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
  215. {
  216. int addr;
  217. addr = mv88e6063_port_to_phy_addr(port);
  218. if (addr == -1)
  219. return 0xffff;
  220. return reg_write(ds, addr, regnum, val);
  221. }
  222. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,9,0)
  223. static struct dsa_switch_driver mv88e6063_switch_ops = {
  224. #else
  225. static struct dsa_switch_ops mv88e6063_switch_ops = {
  226. #endif
  227. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,8,0)
  228. .tag_protocol = htons(ETH_P_TRAILER),
  229. #else
  230. .get_tag_protocol = mv88e6063_get_tag_protocol,
  231. #endif
  232. .probe = mv88e6063_drv_probe,
  233. .setup = mv88e6063_setup,
  234. .set_addr = mv88e6063_set_addr,
  235. .phy_read = mv88e6063_phy_read,
  236. .phy_write = mv88e6063_phy_write,
  237. };
  238. #if LINUX_VERSION_CODE > KERNEL_VERSION(4,13,0)
  239. static struct dsa_switch_driver mv88e6063_switch_drv = {
  240. .ops = &mv88e6063_switch_ops,
  241. };
  242. #endif
  243. static int __init mv88e6063_init(void)
  244. {
  245. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
  246. register_switch_driver(&mv88e6063_switch_ops);
  247. #else
  248. register_switch_driver(&mv88e6063_switch_drv);
  249. #endif
  250. return 0;
  251. }
  252. module_init(mv88e6063_init);
  253. static void __exit mv88e6063_cleanup(void)
  254. {
  255. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
  256. unregister_switch_driver(&mv88e6063_switch_ops);
  257. #else
  258. unregister_switch_driver(&mv88e6063_switch_drv);
  259. #endif
  260. }
  261. module_exit(mv88e6063_cleanup);