mv88e6060.c 7.1 KB

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