mv88e6060.c 6.3 KB

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