mv88e6060.c 6.2 KB

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