mv88e6060.c 7.5 KB

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