mv88e6060.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * net/dsa/mv88e6060.c - Driver for Marvell 88e6060 switch chips
  3. * Copyright (c) 2008-2009 Marvell Semiconductor
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/list.h>
  13. #include <linux/module.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/phy.h>
  16. #include <net/dsa.h>
  17. #include "mv88e6060.h"
  18. static int reg_read(struct dsa_switch *ds, int addr, int reg)
  19. {
  20. struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
  21. if (bus == NULL)
  22. return -EINVAL;
  23. return mdiobus_read_nested(bus, ds->pd->sw_addr + addr, reg);
  24. }
  25. #define REG_READ(addr, reg) \
  26. ({ \
  27. int __ret; \
  28. \
  29. __ret = reg_read(ds, addr, reg); \
  30. if (__ret < 0) \
  31. return __ret; \
  32. __ret; \
  33. })
  34. static int reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
  35. {
  36. struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
  37. if (bus == NULL)
  38. return -EINVAL;
  39. return mdiobus_write_nested(bus, ds->pd->sw_addr + addr, reg, val);
  40. }
  41. #define REG_WRITE(addr, reg, val) \
  42. ({ \
  43. int __ret; \
  44. \
  45. __ret = reg_write(ds, addr, reg, val); \
  46. if (__ret < 0) \
  47. return __ret; \
  48. })
  49. static char *mv88e6060_probe(struct device *host_dev, int sw_addr)
  50. {
  51. struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
  52. int ret;
  53. if (bus == NULL)
  54. return NULL;
  55. ret = mdiobus_read(bus, sw_addr + REG_PORT(0), PORT_SWITCH_ID);
  56. if (ret >= 0) {
  57. if (ret == PORT_SWITCH_ID_6060)
  58. return "Marvell 88E6060 (A0)";
  59. if (ret == PORT_SWITCH_ID_6060_R1 ||
  60. ret == PORT_SWITCH_ID_6060_R2)
  61. return "Marvell 88E6060 (B0)";
  62. if ((ret & PORT_SWITCH_ID_6060_MASK) == PORT_SWITCH_ID_6060)
  63. return "Marvell 88E6060";
  64. }
  65. return NULL;
  66. }
  67. static int mv88e6060_switch_reset(struct dsa_switch *ds)
  68. {
  69. int i;
  70. int ret;
  71. unsigned long timeout;
  72. /* Set all ports to the disabled state. */
  73. for (i = 0; i < MV88E6060_PORTS; i++) {
  74. ret = REG_READ(REG_PORT(i), PORT_CONTROL);
  75. REG_WRITE(REG_PORT(i), PORT_CONTROL,
  76. ret & ~PORT_CONTROL_STATE_MASK);
  77. }
  78. /* Wait for transmit queues to drain. */
  79. usleep_range(2000, 4000);
  80. /* Reset the switch. */
  81. REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL,
  82. GLOBAL_ATU_CONTROL_SWRESET |
  83. GLOBAL_ATU_CONTROL_LEARNDIS);
  84. /* Wait up to one second for reset to complete. */
  85. timeout = jiffies + 1 * HZ;
  86. while (time_before(jiffies, timeout)) {
  87. ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
  88. if (ret & GLOBAL_STATUS_INIT_READY)
  89. break;
  90. usleep_range(1000, 2000);
  91. }
  92. if (time_after(jiffies, timeout))
  93. return -ETIMEDOUT;
  94. return 0;
  95. }
  96. static int mv88e6060_setup_global(struct dsa_switch *ds)
  97. {
  98. /* Disable discarding of frames with excessive collisions,
  99. * set the maximum frame size to 1536 bytes, and mask all
  100. * interrupt sources.
  101. */
  102. REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL, GLOBAL_CONTROL_MAX_FRAME_1536);
  103. /* Disable automatic address learning.
  104. */
  105. REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL,
  106. GLOBAL_ATU_CONTROL_LEARNDIS);
  107. return 0;
  108. }
  109. static int mv88e6060_setup_port(struct dsa_switch *ds, int p)
  110. {
  111. int addr = REG_PORT(p);
  112. /* Do not force flow control, disable Ingress and Egress
  113. * Header tagging, disable VLAN tunneling, and set the port
  114. * state to Forwarding. Additionally, if this is the CPU
  115. * port, enable Ingress and Egress Trailer tagging mode.
  116. */
  117. REG_WRITE(addr, PORT_CONTROL,
  118. dsa_is_cpu_port(ds, p) ?
  119. PORT_CONTROL_TRAILER |
  120. PORT_CONTROL_INGRESS_MODE |
  121. PORT_CONTROL_STATE_FORWARDING :
  122. PORT_CONTROL_STATE_FORWARDING);
  123. /* Port based VLAN map: give each port its own address
  124. * database, allow the CPU port to talk to each of the 'real'
  125. * ports, and allow each of the 'real' ports to only talk to
  126. * the CPU port.
  127. */
  128. REG_WRITE(addr, PORT_VLAN_MAP,
  129. ((p & 0xf) << PORT_VLAN_MAP_DBNUM_SHIFT) |
  130. (dsa_is_cpu_port(ds, p) ?
  131. ds->phys_port_mask :
  132. BIT(ds->dst->cpu_port)));
  133. /* Port Association Vector: when learning source addresses
  134. * of packets, add the address to the address database using
  135. * a port bitmap that has only the bit for this port set and
  136. * the other bits clear.
  137. */
  138. REG_WRITE(addr, PORT_ASSOC_VECTOR, BIT(p));
  139. return 0;
  140. }
  141. static int mv88e6060_setup(struct dsa_switch *ds)
  142. {
  143. int i;
  144. int ret;
  145. ret = mv88e6060_switch_reset(ds);
  146. if (ret < 0)
  147. return ret;
  148. /* @@@ initialise atu */
  149. ret = mv88e6060_setup_global(ds);
  150. if (ret < 0)
  151. return ret;
  152. for (i = 0; i < MV88E6060_PORTS; i++) {
  153. ret = mv88e6060_setup_port(ds, i);
  154. if (ret < 0)
  155. return ret;
  156. }
  157. return 0;
  158. }
  159. static int mv88e6060_set_addr(struct dsa_switch *ds, u8 *addr)
  160. {
  161. /* Use the same MAC Address as FD Pause frames for all ports */
  162. REG_WRITE(REG_GLOBAL, GLOBAL_MAC_01, (addr[0] << 9) | addr[1]);
  163. REG_WRITE(REG_GLOBAL, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
  164. REG_WRITE(REG_GLOBAL, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
  165. return 0;
  166. }
  167. static int mv88e6060_port_to_phy_addr(int port)
  168. {
  169. if (port >= 0 && port < MV88E6060_PORTS)
  170. return port;
  171. return -1;
  172. }
  173. static int mv88e6060_phy_read(struct dsa_switch *ds, int port, int regnum)
  174. {
  175. int addr;
  176. addr = mv88e6060_port_to_phy_addr(port);
  177. if (addr == -1)
  178. return 0xffff;
  179. return reg_read(ds, addr, regnum);
  180. }
  181. static int
  182. mv88e6060_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
  183. {
  184. int addr;
  185. addr = mv88e6060_port_to_phy_addr(port);
  186. if (addr == -1)
  187. return 0xffff;
  188. return reg_write(ds, addr, regnum, val);
  189. }
  190. static struct dsa_switch_driver mv88e6060_switch_driver = {
  191. .tag_protocol = DSA_TAG_PROTO_TRAILER,
  192. .probe = mv88e6060_probe,
  193. .setup = mv88e6060_setup,
  194. .set_addr = mv88e6060_set_addr,
  195. .phy_read = mv88e6060_phy_read,
  196. .phy_write = mv88e6060_phy_write,
  197. };
  198. static int __init mv88e6060_init(void)
  199. {
  200. register_switch_driver(&mv88e6060_switch_driver);
  201. return 0;
  202. }
  203. module_init(mv88e6060_init);
  204. static void __exit mv88e6060_cleanup(void)
  205. {
  206. unregister_switch_driver(&mv88e6060_switch_driver);
  207. }
  208. module_exit(mv88e6060_cleanup);
  209. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  210. MODULE_DESCRIPTION("Driver for Marvell 88E6060 ethernet switch chip");
  211. MODULE_LICENSE("GPL");
  212. MODULE_ALIAS("platform:mv88e6060");