mv88e6060.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 mv88e6060_priv *priv = ds->priv;
  21. return mdiobus_read_nested(priv->bus, priv->sw_addr + addr, reg);
  22. }
  23. #define REG_READ(addr, reg) \
  24. ({ \
  25. int __ret; \
  26. \
  27. __ret = reg_read(ds, addr, reg); \
  28. if (__ret < 0) \
  29. return __ret; \
  30. __ret; \
  31. })
  32. static int reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
  33. {
  34. struct mv88e6060_priv *priv = ds->priv;
  35. return mdiobus_write_nested(priv->bus, priv->sw_addr + addr, reg, val);
  36. }
  37. #define REG_WRITE(addr, reg, val) \
  38. ({ \
  39. int __ret; \
  40. \
  41. __ret = reg_write(ds, addr, reg, val); \
  42. if (__ret < 0) \
  43. return __ret; \
  44. })
  45. static const char *mv88e6060_get_name(struct mii_bus *bus, int sw_addr)
  46. {
  47. int ret;
  48. ret = mdiobus_read(bus, sw_addr + REG_PORT(0), PORT_SWITCH_ID);
  49. if (ret >= 0) {
  50. if (ret == PORT_SWITCH_ID_6060)
  51. return "Marvell 88E6060 (A0)";
  52. if (ret == PORT_SWITCH_ID_6060_R1 ||
  53. ret == PORT_SWITCH_ID_6060_R2)
  54. return "Marvell 88E6060 (B0)";
  55. if ((ret & PORT_SWITCH_ID_6060_MASK) == PORT_SWITCH_ID_6060)
  56. return "Marvell 88E6060";
  57. }
  58. return NULL;
  59. }
  60. static enum dsa_tag_protocol mv88e6060_get_tag_protocol(struct dsa_switch *ds)
  61. {
  62. return DSA_TAG_PROTO_TRAILER;
  63. }
  64. static const char *mv88e6060_drv_probe(struct device *dsa_dev,
  65. struct device *host_dev, int sw_addr,
  66. void **_priv)
  67. {
  68. struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
  69. struct mv88e6060_priv *priv;
  70. const char *name;
  71. name = mv88e6060_get_name(bus, sw_addr);
  72. if (name) {
  73. priv = devm_kzalloc(dsa_dev, sizeof(*priv), GFP_KERNEL);
  74. if (!priv)
  75. return NULL;
  76. *_priv = priv;
  77. priv->bus = bus;
  78. priv->sw_addr = sw_addr;
  79. }
  80. return name;
  81. }
  82. static int mv88e6060_switch_reset(struct dsa_switch *ds)
  83. {
  84. int i;
  85. int ret;
  86. unsigned long timeout;
  87. /* Set all ports to the disabled state. */
  88. for (i = 0; i < MV88E6060_PORTS; i++) {
  89. ret = REG_READ(REG_PORT(i), PORT_CONTROL);
  90. REG_WRITE(REG_PORT(i), PORT_CONTROL,
  91. ret & ~PORT_CONTROL_STATE_MASK);
  92. }
  93. /* Wait for transmit queues to drain. */
  94. usleep_range(2000, 4000);
  95. /* Reset the switch. */
  96. REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL,
  97. GLOBAL_ATU_CONTROL_SWRESET |
  98. GLOBAL_ATU_CONTROL_ATUSIZE_1024 |
  99. GLOBAL_ATU_CONTROL_ATE_AGE_5MIN);
  100. /* Wait up to one second for reset to complete. */
  101. timeout = jiffies + 1 * HZ;
  102. while (time_before(jiffies, timeout)) {
  103. ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
  104. if (ret & GLOBAL_STATUS_INIT_READY)
  105. break;
  106. usleep_range(1000, 2000);
  107. }
  108. if (time_after(jiffies, timeout))
  109. return -ETIMEDOUT;
  110. return 0;
  111. }
  112. static int mv88e6060_setup_global(struct dsa_switch *ds)
  113. {
  114. /* Disable discarding of frames with excessive collisions,
  115. * set the maximum frame size to 1536 bytes, and mask all
  116. * interrupt sources.
  117. */
  118. REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL, GLOBAL_CONTROL_MAX_FRAME_1536);
  119. /* Enable automatic address learning, set the address
  120. * database size to 1024 entries, and set the default aging
  121. * time to 5 minutes.
  122. */
  123. REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL,
  124. GLOBAL_ATU_CONTROL_ATUSIZE_1024 |
  125. GLOBAL_ATU_CONTROL_ATE_AGE_5MIN);
  126. return 0;
  127. }
  128. static int mv88e6060_setup_port(struct dsa_switch *ds, int p)
  129. {
  130. int addr = REG_PORT(p);
  131. /* Do not force flow control, disable Ingress and Egress
  132. * Header tagging, disable VLAN tunneling, and set the port
  133. * state to Forwarding. Additionally, if this is the CPU
  134. * port, enable Ingress and Egress Trailer tagging mode.
  135. */
  136. REG_WRITE(addr, PORT_CONTROL,
  137. dsa_is_cpu_port(ds, p) ?
  138. PORT_CONTROL_TRAILER |
  139. PORT_CONTROL_INGRESS_MODE |
  140. PORT_CONTROL_STATE_FORWARDING :
  141. PORT_CONTROL_STATE_FORWARDING);
  142. /* Port based VLAN map: give each port its own address
  143. * database, allow the CPU port to talk to each of the 'real'
  144. * ports, and allow each of the 'real' ports to only talk to
  145. * the CPU port.
  146. */
  147. REG_WRITE(addr, PORT_VLAN_MAP,
  148. ((p & 0xf) << PORT_VLAN_MAP_DBNUM_SHIFT) |
  149. (dsa_is_cpu_port(ds, p) ?
  150. ds->enabled_port_mask :
  151. BIT(ds->dst->cpu_port)));
  152. /* Port Association Vector: when learning source addresses
  153. * of packets, add the address to the address database using
  154. * a port bitmap that has only the bit for this port set and
  155. * the other bits clear.
  156. */
  157. REG_WRITE(addr, PORT_ASSOC_VECTOR, BIT(p));
  158. return 0;
  159. }
  160. static int mv88e6060_setup(struct dsa_switch *ds)
  161. {
  162. int ret;
  163. int i;
  164. ret = mv88e6060_switch_reset(ds);
  165. if (ret < 0)
  166. return ret;
  167. /* @@@ initialise atu */
  168. ret = mv88e6060_setup_global(ds);
  169. if (ret < 0)
  170. return ret;
  171. for (i = 0; i < MV88E6060_PORTS; i++) {
  172. ret = mv88e6060_setup_port(ds, i);
  173. if (ret < 0)
  174. return ret;
  175. }
  176. return 0;
  177. }
  178. static int mv88e6060_set_addr(struct dsa_switch *ds, u8 *addr)
  179. {
  180. /* Use the same MAC Address as FD Pause frames for all ports */
  181. REG_WRITE(REG_GLOBAL, GLOBAL_MAC_01, (addr[0] << 9) | addr[1]);
  182. REG_WRITE(REG_GLOBAL, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
  183. REG_WRITE(REG_GLOBAL, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
  184. return 0;
  185. }
  186. static int mv88e6060_port_to_phy_addr(int port)
  187. {
  188. if (port >= 0 && port < MV88E6060_PORTS)
  189. return port;
  190. return -1;
  191. }
  192. static int mv88e6060_phy_read(struct dsa_switch *ds, int port, int regnum)
  193. {
  194. int addr;
  195. addr = mv88e6060_port_to_phy_addr(port);
  196. if (addr == -1)
  197. return 0xffff;
  198. return reg_read(ds, addr, regnum);
  199. }
  200. static int
  201. mv88e6060_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
  202. {
  203. int addr;
  204. addr = mv88e6060_port_to_phy_addr(port);
  205. if (addr == -1)
  206. return 0xffff;
  207. return reg_write(ds, addr, regnum, val);
  208. }
  209. static struct dsa_switch_ops mv88e6060_switch_ops = {
  210. .get_tag_protocol = mv88e6060_get_tag_protocol,
  211. .probe = mv88e6060_drv_probe,
  212. .setup = mv88e6060_setup,
  213. .set_addr = mv88e6060_set_addr,
  214. .phy_read = mv88e6060_phy_read,
  215. .phy_write = mv88e6060_phy_write,
  216. };
  217. static int __init mv88e6060_init(void)
  218. {
  219. register_switch_driver(&mv88e6060_switch_ops);
  220. return 0;
  221. }
  222. module_init(mv88e6060_init);
  223. static void __exit mv88e6060_cleanup(void)
  224. {
  225. unregister_switch_driver(&mv88e6060_switch_ops);
  226. }
  227. module_exit(mv88e6060_cleanup);
  228. MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
  229. MODULE_DESCRIPTION("Driver for Marvell 88E6060 ethernet switch chip");
  230. MODULE_LICENSE("GPL");
  231. MODULE_ALIAS("platform:mv88e6060");