mv88e6063.c 6.8 KB

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