mv88e6131.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * net/dsa/mv88e6131.c - Marvell 88e6131 switch chip support
  3. * Copyright (c) 2008 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. #include "mv88e6xxx.h"
  15. static char *mv88e6131_probe(struct mii_bus *bus, int sw_addr)
  16. {
  17. int ret;
  18. ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), 0x03);
  19. if (ret >= 0) {
  20. ret &= 0xfff0;
  21. if (ret == 0x1060)
  22. return "Marvell 88E6131";
  23. }
  24. return NULL;
  25. }
  26. static int mv88e6131_switch_reset(struct dsa_switch *ds)
  27. {
  28. int i;
  29. int ret;
  30. /*
  31. * Set all ports to the disabled state.
  32. */
  33. for (i = 0; i < 8; i++) {
  34. ret = REG_READ(REG_PORT(i), 0x04);
  35. REG_WRITE(REG_PORT(i), 0x04, ret & 0xfffc);
  36. }
  37. /*
  38. * Wait for transmit queues to drain.
  39. */
  40. msleep(2);
  41. /*
  42. * Reset the switch.
  43. */
  44. REG_WRITE(REG_GLOBAL, 0x04, 0xc400);
  45. /*
  46. * Wait up to one second for reset to complete.
  47. */
  48. for (i = 0; i < 1000; i++) {
  49. ret = REG_READ(REG_GLOBAL, 0x00);
  50. if ((ret & 0xc800) == 0xc800)
  51. break;
  52. msleep(1);
  53. }
  54. if (i == 1000)
  55. return -ETIMEDOUT;
  56. return 0;
  57. }
  58. static int mv88e6131_setup_global(struct dsa_switch *ds)
  59. {
  60. int ret;
  61. int i;
  62. /*
  63. * Enable the PHY polling unit, don't discard packets with
  64. * excessive collisions, use a weighted fair queueing scheme
  65. * to arbitrate between packet queues, set the maximum frame
  66. * size to 1632, and mask all interrupt sources.
  67. */
  68. REG_WRITE(REG_GLOBAL, 0x04, 0x4400);
  69. /*
  70. * Set the default address aging time to 5 minutes, and
  71. * enable address learn messages to be sent to all message
  72. * ports.
  73. */
  74. REG_WRITE(REG_GLOBAL, 0x0a, 0x0148);
  75. /*
  76. * Configure the priority mapping registers.
  77. */
  78. ret = mv88e6xxx_config_prio(ds);
  79. if (ret < 0)
  80. return ret;
  81. /*
  82. * Set the VLAN ethertype to 0x8100.
  83. */
  84. REG_WRITE(REG_GLOBAL, 0x19, 0x8100);
  85. /*
  86. * Disable ARP mirroring, and configure the cpu port as the
  87. * port to which ingress and egress monitor frames are to be
  88. * sent.
  89. */
  90. REG_WRITE(REG_GLOBAL, 0x1a, (ds->cpu_port * 0x1100) | 0x00f0);
  91. /*
  92. * Disable cascade port functionality, and set the switch's
  93. * DSA device number to zero.
  94. */
  95. REG_WRITE(REG_GLOBAL, 0x1c, 0xe000);
  96. /*
  97. * Send all frames with destination addresses matching
  98. * 01:80:c2:00:00:0x to the CPU port.
  99. */
  100. REG_WRITE(REG_GLOBAL2, 0x03, 0xffff);
  101. /*
  102. * Ignore removed tag data on doubly tagged packets, disable
  103. * flow control messages, force flow control priority to the
  104. * highest, and send all special multicast frames to the CPU
  105. * port at the higest priority.
  106. */
  107. REG_WRITE(REG_GLOBAL2, 0x05, 0x00ff);
  108. /*
  109. * Map all DSA device IDs to the CPU port.
  110. */
  111. for (i = 0; i < 32; i++)
  112. REG_WRITE(REG_GLOBAL2, 0x06, 0x8000 | (i << 8) | ds->cpu_port);
  113. /*
  114. * Clear all trunk masks.
  115. */
  116. for (i = 0; i < 8; i++)
  117. REG_WRITE(REG_GLOBAL2, 0x07, 0x8000 | (i << 12) | 0xff);
  118. /*
  119. * Clear all trunk mappings.
  120. */
  121. for (i = 0; i < 16; i++)
  122. REG_WRITE(REG_GLOBAL2, 0x08, 0x8000 | (i << 11));
  123. /*
  124. * Force the priority of IGMP/MLD snoop frames and ARP frames
  125. * to the highest setting.
  126. */
  127. REG_WRITE(REG_GLOBAL2, 0x0f, 0x00ff);
  128. return 0;
  129. }
  130. static int mv88e6131_setup_port(struct dsa_switch *ds, int p)
  131. {
  132. int addr = REG_PORT(p);
  133. /*
  134. * MAC Forcing register: don't force link, speed, duplex
  135. * or flow control state to any particular values.
  136. */
  137. REG_WRITE(addr, 0x01, 0x0003);
  138. /*
  139. * Port Control: disable Core Tag, disable Drop-on-Lock,
  140. * transmit frames unmodified, disable Header mode,
  141. * enable IGMP/MLD snoop, disable DoubleTag, disable VLAN
  142. * tunneling, determine priority by looking at 802.1p and
  143. * IP priority fields (IP prio has precedence), and set STP
  144. * state to Forwarding. Finally, if this is the CPU port,
  145. * additionally enable DSA tagging and forwarding of unknown
  146. * unicast addresses.
  147. */
  148. REG_WRITE(addr, 0x04, (p == ds->cpu_port) ? 0x0537 : 0x0433);
  149. /*
  150. * Port Control 1: disable trunking. Also, if this is the
  151. * CPU port, enable learn messages to be sent to this port.
  152. */
  153. REG_WRITE(addr, 0x05, (p == ds->cpu_port) ? 0x8000 : 0x0000);
  154. /*
  155. * Port based VLAN map: give each port its own address
  156. * database, allow the CPU port to talk to each of the 'real'
  157. * ports, and allow each of the 'real' ports to only talk to
  158. * the CPU port.
  159. */
  160. REG_WRITE(addr, 0x06,
  161. ((p & 0xf) << 12) |
  162. ((p == ds->cpu_port) ?
  163. ds->valid_port_mask :
  164. (1 << ds->cpu_port)));
  165. /*
  166. * Default VLAN ID and priority: don't set a default VLAN
  167. * ID, and set the default packet priority to zero.
  168. */
  169. REG_WRITE(addr, 0x07, 0x0000);
  170. /*
  171. * Port Control 2: don't force a good FCS, don't use
  172. * VLAN-based, source address-based or destination
  173. * address-based priority overrides, don't let the switch
  174. * add or strip 802.1q tags, don't discard tagged or
  175. * untagged frames on this port, do a destination address
  176. * lookup on received packets as usual, don't send a copy
  177. * of all transmitted/received frames on this port to the
  178. * CPU, and configure the CPU port number. Also, if this
  179. * is the CPU port, enable forwarding of unknown multicast
  180. * addresses.
  181. */
  182. REG_WRITE(addr, 0x08,
  183. ((p == ds->cpu_port) ? 0x00c0 : 0x0080) |
  184. ds->cpu_port);
  185. /*
  186. * Rate Control: disable ingress rate limiting.
  187. */
  188. REG_WRITE(addr, 0x09, 0x0000);
  189. /*
  190. * Rate Control 2: disable egress rate limiting.
  191. */
  192. REG_WRITE(addr, 0x0a, 0x0000);
  193. /*
  194. * Port Association Vector: when learning source addresses
  195. * of packets, add the address to the address database using
  196. * a port bitmap that has only the bit for this port set and
  197. * the other bits clear.
  198. */
  199. REG_WRITE(addr, 0x0b, 1 << p);
  200. /*
  201. * Tag Remap: use an identity 802.1p prio -> switch prio
  202. * mapping.
  203. */
  204. REG_WRITE(addr, 0x18, 0x3210);
  205. /*
  206. * Tag Remap 2: use an identity 802.1p prio -> switch prio
  207. * mapping.
  208. */
  209. REG_WRITE(addr, 0x19, 0x7654);
  210. return 0;
  211. }
  212. static int mv88e6131_setup(struct dsa_switch *ds)
  213. {
  214. struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
  215. int i;
  216. int ret;
  217. mutex_init(&ps->smi_mutex);
  218. mv88e6xxx_ppu_state_init(ds);
  219. mutex_init(&ps->stats_mutex);
  220. ret = mv88e6131_switch_reset(ds);
  221. if (ret < 0)
  222. return ret;
  223. /* @@@ initialise vtu and atu */
  224. ret = mv88e6131_setup_global(ds);
  225. if (ret < 0)
  226. return ret;
  227. for (i = 0; i < 6; i++) {
  228. ret = mv88e6131_setup_port(ds, i);
  229. if (ret < 0)
  230. return ret;
  231. }
  232. return 0;
  233. }
  234. static int mv88e6131_port_to_phy_addr(int port)
  235. {
  236. if (port >= 0 && port != 3 && port <= 7)
  237. return port;
  238. return -1;
  239. }
  240. static int
  241. mv88e6131_phy_read(struct dsa_switch *ds, int port, int regnum)
  242. {
  243. int addr = mv88e6131_port_to_phy_addr(port);
  244. return mv88e6xxx_phy_read_ppu(ds, addr, regnum);
  245. }
  246. static int
  247. mv88e6131_phy_write(struct dsa_switch *ds,
  248. int port, int regnum, u16 val)
  249. {
  250. int addr = mv88e6131_port_to_phy_addr(port);
  251. return mv88e6xxx_phy_write_ppu(ds, addr, regnum, val);
  252. }
  253. static struct mv88e6xxx_hw_stat mv88e6131_hw_stats[] = {
  254. { "in_good_octets", 8, 0x00, },
  255. { "in_bad_octets", 4, 0x02, },
  256. { "in_unicast", 4, 0x04, },
  257. { "in_broadcasts", 4, 0x06, },
  258. { "in_multicasts", 4, 0x07, },
  259. { "in_pause", 4, 0x16, },
  260. { "in_undersize", 4, 0x18, },
  261. { "in_fragments", 4, 0x19, },
  262. { "in_oversize", 4, 0x1a, },
  263. { "in_jabber", 4, 0x1b, },
  264. { "in_rx_error", 4, 0x1c, },
  265. { "in_fcs_error", 4, 0x1d, },
  266. { "out_octets", 8, 0x0e, },
  267. { "out_unicast", 4, 0x10, },
  268. { "out_broadcasts", 4, 0x13, },
  269. { "out_multicasts", 4, 0x12, },
  270. { "out_pause", 4, 0x15, },
  271. { "excessive", 4, 0x11, },
  272. { "collisions", 4, 0x1e, },
  273. { "deferred", 4, 0x05, },
  274. { "single", 4, 0x14, },
  275. { "multiple", 4, 0x17, },
  276. { "out_fcs_error", 4, 0x03, },
  277. { "late", 4, 0x1f, },
  278. { "hist_64bytes", 4, 0x08, },
  279. { "hist_65_127bytes", 4, 0x09, },
  280. { "hist_128_255bytes", 4, 0x0a, },
  281. { "hist_256_511bytes", 4, 0x0b, },
  282. { "hist_512_1023bytes", 4, 0x0c, },
  283. { "hist_1024_max_bytes", 4, 0x0d, },
  284. };
  285. static void
  286. mv88e6131_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
  287. {
  288. mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6131_hw_stats),
  289. mv88e6131_hw_stats, port, data);
  290. }
  291. static void
  292. mv88e6131_get_ethtool_stats(struct dsa_switch *ds,
  293. int port, uint64_t *data)
  294. {
  295. mv88e6xxx_get_ethtool_stats(ds, ARRAY_SIZE(mv88e6131_hw_stats),
  296. mv88e6131_hw_stats, port, data);
  297. }
  298. static int mv88e6131_get_sset_count(struct dsa_switch *ds)
  299. {
  300. return ARRAY_SIZE(mv88e6131_hw_stats);
  301. }
  302. static struct dsa_switch_driver mv88e6131_switch_driver = {
  303. .tag_protocol = __constant_htons(ETH_P_DSA),
  304. .priv_size = sizeof(struct mv88e6xxx_priv_state),
  305. .probe = mv88e6131_probe,
  306. .setup = mv88e6131_setup,
  307. .set_addr = mv88e6xxx_set_addr_direct,
  308. .phy_read = mv88e6131_phy_read,
  309. .phy_write = mv88e6131_phy_write,
  310. .poll_link = mv88e6xxx_poll_link,
  311. .get_strings = mv88e6131_get_strings,
  312. .get_ethtool_stats = mv88e6131_get_ethtool_stats,
  313. .get_sset_count = mv88e6131_get_sset_count,
  314. };
  315. int __init mv88e6131_init(void)
  316. {
  317. register_switch_driver(&mv88e6131_switch_driver);
  318. return 0;
  319. }
  320. module_init(mv88e6131_init);
  321. void __exit mv88e6131_cleanup(void)
  322. {
  323. unregister_switch_driver(&mv88e6131_switch_driver);
  324. }
  325. module_exit(mv88e6131_cleanup);