mv88e6063.c 6.3 KB

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