mv88e6060.c 7.1 KB

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