mv88e6060.c 7.1 KB

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