mv88e6060.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/list.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/phy.h>
  13. #include "dsa_priv.h"
  14. #define REG_PORT(p) (8 + (p))
  15. #define REG_GLOBAL 0x0f
  16. static int reg_read(struct dsa_switch *ds, int addr, int reg)
  17. {
  18. return mdiobus_read(ds->master_mii_bus, addr, reg);
  19. }
  20. #define REG_READ(addr, reg) \
  21. ({ \
  22. int __ret; \
  23. \
  24. __ret = reg_read(ds, addr, reg); \
  25. if (__ret < 0) \
  26. return __ret; \
  27. __ret; \
  28. })
  29. static int reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
  30. {
  31. return mdiobus_write(ds->master_mii_bus, addr, reg, val);
  32. }
  33. #define REG_WRITE(addr, reg, val) \
  34. ({ \
  35. int __ret; \
  36. \
  37. __ret = reg_write(ds, addr, reg, val); \
  38. if (__ret < 0) \
  39. return __ret; \
  40. })
  41. static char *mv88e6060_probe(struct mii_bus *bus, int sw_addr)
  42. {
  43. int ret;
  44. ret = mdiobus_read(bus, REG_PORT(0), 0x03);
  45. if (ret >= 0) {
  46. ret &= 0xfff0;
  47. if (ret == 0x0600)
  48. return "Marvell 88E6060";
  49. }
  50. return NULL;
  51. }
  52. static int mv88e6060_switch_reset(struct dsa_switch *ds)
  53. {
  54. int i;
  55. int ret;
  56. /*
  57. * Set all ports to the disabled state.
  58. */
  59. for (i = 0; i < 6; i++) {
  60. ret = REG_READ(REG_PORT(i), 0x04);
  61. REG_WRITE(REG_PORT(i), 0x04, ret & 0xfffc);
  62. }
  63. /*
  64. * Wait for transmit queues to drain.
  65. */
  66. msleep(2);
  67. /*
  68. * Reset the switch.
  69. */
  70. REG_WRITE(REG_GLOBAL, 0x0a, 0xa130);
  71. /*
  72. * Wait up to one second for reset to complete.
  73. */
  74. for (i = 0; i < 1000; i++) {
  75. ret = REG_READ(REG_GLOBAL, 0x00);
  76. if ((ret & 0x8000) == 0x0000)
  77. break;
  78. msleep(1);
  79. }
  80. if (i == 1000)
  81. return -ETIMEDOUT;
  82. return 0;
  83. }
  84. static int mv88e6060_setup_global(struct dsa_switch *ds)
  85. {
  86. /*
  87. * Disable discarding of frames with excessive collisions,
  88. * set the maximum frame size to 1536 bytes, and mask all
  89. * interrupt sources.
  90. */
  91. REG_WRITE(REG_GLOBAL, 0x04, 0x0800);
  92. /*
  93. * Enable automatic address learning, set the address
  94. * database size to 1024 entries, and set the default aging
  95. * time to 5 minutes.
  96. */
  97. REG_WRITE(REG_GLOBAL, 0x0a, 0x2130);
  98. return 0;
  99. }
  100. static int mv88e6060_setup_port(struct dsa_switch *ds, int p)
  101. {
  102. int addr = REG_PORT(p);
  103. /*
  104. * Do not force flow control, disable Ingress and Egress
  105. * Header tagging, disable VLAN tunneling, and set the port
  106. * state to Forwarding. Additionally, if this is the CPU
  107. * port, enable Ingress and Egress Trailer tagging mode.
  108. */
  109. REG_WRITE(addr, 0x04, dsa_is_cpu_port(ds, p) ? 0x4103 : 0x0003);
  110. /*
  111. * Port based VLAN map: give each port its own address
  112. * database, allow the CPU port to talk to each of the 'real'
  113. * ports, and allow each of the 'real' ports to only talk to
  114. * the CPU port.
  115. */
  116. REG_WRITE(addr, 0x06,
  117. ((p & 0xf) << 12) |
  118. (dsa_is_cpu_port(ds, p) ?
  119. ds->phys_port_mask :
  120. (1 << ds->dst->cpu_port)));
  121. /*
  122. * Port Association Vector: when learning source addresses
  123. * of packets, add the address to the address database using
  124. * a port bitmap that has only the bit for this port set and
  125. * the other bits clear.
  126. */
  127. REG_WRITE(addr, 0x0b, 1 << p);
  128. return 0;
  129. }
  130. static int mv88e6060_setup(struct dsa_switch *ds)
  131. {
  132. int i;
  133. int ret;
  134. ret = mv88e6060_switch_reset(ds);
  135. if (ret < 0)
  136. return ret;
  137. /* @@@ initialise atu */
  138. ret = mv88e6060_setup_global(ds);
  139. if (ret < 0)
  140. return ret;
  141. for (i = 0; i < 6; i++) {
  142. ret = mv88e6060_setup_port(ds, i);
  143. if (ret < 0)
  144. return ret;
  145. }
  146. return 0;
  147. }
  148. static int mv88e6060_set_addr(struct dsa_switch *ds, u8 *addr)
  149. {
  150. REG_WRITE(REG_GLOBAL, 0x01, (addr[0] << 8) | addr[1]);
  151. REG_WRITE(REG_GLOBAL, 0x02, (addr[2] << 8) | addr[3]);
  152. REG_WRITE(REG_GLOBAL, 0x03, (addr[4] << 8) | addr[5]);
  153. return 0;
  154. }
  155. static int mv88e6060_port_to_phy_addr(int port)
  156. {
  157. if (port >= 0 && port <= 5)
  158. return port;
  159. return -1;
  160. }
  161. static int mv88e6060_phy_read(struct dsa_switch *ds, int port, int regnum)
  162. {
  163. int addr;
  164. addr = mv88e6060_port_to_phy_addr(port);
  165. if (addr == -1)
  166. return 0xffff;
  167. return reg_read(ds, addr, regnum);
  168. }
  169. static int
  170. mv88e6060_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
  171. {
  172. int addr;
  173. addr = mv88e6060_port_to_phy_addr(port);
  174. if (addr == -1)
  175. return 0xffff;
  176. return reg_write(ds, addr, regnum, val);
  177. }
  178. static void mv88e6060_poll_link(struct dsa_switch *ds)
  179. {
  180. int i;
  181. for (i = 0; i < DSA_MAX_PORTS; i++) {
  182. struct net_device *dev;
  183. int uninitialized_var(port_status);
  184. int link;
  185. int speed;
  186. int duplex;
  187. int fc;
  188. dev = ds->ports[i];
  189. if (dev == NULL)
  190. continue;
  191. link = 0;
  192. if (dev->flags & IFF_UP) {
  193. port_status = reg_read(ds, REG_PORT(i), 0x00);
  194. if (port_status < 0)
  195. continue;
  196. link = !!(port_status & 0x1000);
  197. }
  198. if (!link) {
  199. if (netif_carrier_ok(dev)) {
  200. printk(KERN_INFO "%s: link down\n", dev->name);
  201. netif_carrier_off(dev);
  202. }
  203. continue;
  204. }
  205. speed = (port_status & 0x0100) ? 100 : 10;
  206. duplex = (port_status & 0x0200) ? 1 : 0;
  207. fc = ((port_status & 0xc000) == 0xc000) ? 1 : 0;
  208. if (!netif_carrier_ok(dev)) {
  209. printk(KERN_INFO "%s: link up, %d Mb/s, %s duplex, "
  210. "flow control %sabled\n", dev->name,
  211. speed, duplex ? "full" : "half",
  212. fc ? "en" : "dis");
  213. netif_carrier_on(dev);
  214. }
  215. }
  216. }
  217. static struct dsa_switch_driver mv88e6060_switch_driver = {
  218. .tag_protocol = htons(ETH_P_TRAILER),
  219. .probe = mv88e6060_probe,
  220. .setup = mv88e6060_setup,
  221. .set_addr = mv88e6060_set_addr,
  222. .phy_read = mv88e6060_phy_read,
  223. .phy_write = mv88e6060_phy_write,
  224. .poll_link = mv88e6060_poll_link,
  225. };
  226. static int __init mv88e6060_init(void)
  227. {
  228. register_switch_driver(&mv88e6060_switch_driver);
  229. return 0;
  230. }
  231. module_init(mv88e6060_init);
  232. static void __exit mv88e6060_cleanup(void)
  233. {
  234. unregister_switch_driver(&mv88e6060_switch_driver);
  235. }
  236. module_exit(mv88e6060_cleanup);