mv88e6123_61_65.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * net/dsa/mv88e6123_61_65.c - Marvell 88e6123/6161/6165 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 *mv88e6123_61_65_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 == 0x1210)
  22. return "Marvell 88E6123";
  23. if (ret == 0x1610)
  24. return "Marvell 88E6161";
  25. if (ret == 0x1650)
  26. return "Marvell 88E6165";
  27. }
  28. return NULL;
  29. }
  30. static int mv88e6123_61_65_switch_reset(struct dsa_switch *ds)
  31. {
  32. int i;
  33. int ret;
  34. /*
  35. * Set all ports to the disabled state.
  36. */
  37. for (i = 0; i < 8; i++) {
  38. ret = REG_READ(REG_PORT(i), 0x04);
  39. REG_WRITE(REG_PORT(i), 0x04, ret & 0xfffc);
  40. }
  41. /*
  42. * Wait for transmit queues to drain.
  43. */
  44. msleep(2);
  45. /*
  46. * Reset the switch.
  47. */
  48. REG_WRITE(REG_GLOBAL, 0x04, 0xc400);
  49. /*
  50. * Wait up to one second for reset to complete.
  51. */
  52. for (i = 0; i < 1000; i++) {
  53. ret = REG_READ(REG_GLOBAL, 0x00);
  54. if ((ret & 0xc800) == 0xc800)
  55. break;
  56. msleep(1);
  57. }
  58. if (i == 1000)
  59. return -ETIMEDOUT;
  60. return 0;
  61. }
  62. static int mv88e6123_61_65_setup_global(struct dsa_switch *ds)
  63. {
  64. int ret;
  65. int i;
  66. /*
  67. * Disable the PHY polling unit (since there won't be any
  68. * external PHYs to poll), don't discard packets with
  69. * excessive collisions, and mask all interrupt sources.
  70. */
  71. REG_WRITE(REG_GLOBAL, 0x04, 0x0000);
  72. /*
  73. * Set the default address aging time to 5 minutes, and
  74. * enable address learn messages to be sent to all message
  75. * ports.
  76. */
  77. REG_WRITE(REG_GLOBAL, 0x0a, 0x0148);
  78. /*
  79. * Configure the priority mapping registers.
  80. */
  81. ret = mv88e6xxx_config_prio(ds);
  82. if (ret < 0)
  83. return ret;
  84. /*
  85. * Configure the cpu port, and configure the cpu port as the
  86. * port to which ingress and egress monitor frames are to be
  87. * sent.
  88. */
  89. REG_WRITE(REG_GLOBAL, 0x1a, (ds->cpu_port * 0x1110));
  90. /*
  91. * Disable remote management for now, and set the switch's
  92. * DSA device number to zero.
  93. */
  94. REG_WRITE(REG_GLOBAL, 0x1c, 0x0000);
  95. /*
  96. * Send all frames with destination addresses matching
  97. * 01:80:c2:00:00:2x to the CPU port.
  98. */
  99. REG_WRITE(REG_GLOBAL2, 0x02, 0xffff);
  100. /*
  101. * Send all frames with destination addresses matching
  102. * 01:80:c2:00:00:0x to the CPU port.
  103. */
  104. REG_WRITE(REG_GLOBAL2, 0x03, 0xffff);
  105. /*
  106. * Disable the loopback filter, disable flow control
  107. * messages, disable flood broadcast override, disable
  108. * removing of provider tags, disable ATU age violation
  109. * interrupts, disable tag flow control, force flow
  110. * control priority to the highest, and send all special
  111. * multicast frames to the CPU at the highest priority.
  112. */
  113. REG_WRITE(REG_GLOBAL2, 0x05, 0x00ff);
  114. /*
  115. * Map all DSA device IDs to the CPU port.
  116. */
  117. for (i = 0; i < 32; i++)
  118. REG_WRITE(REG_GLOBAL2, 0x06, 0x8000 | (i << 8) | ds->cpu_port);
  119. /*
  120. * Clear all trunk masks.
  121. */
  122. for (i = 0; i < 8; i++)
  123. REG_WRITE(REG_GLOBAL2, 0x07, 0x8000 | (i << 12) | 0xff);
  124. /*
  125. * Clear all trunk mappings.
  126. */
  127. for (i = 0; i < 16; i++)
  128. REG_WRITE(REG_GLOBAL2, 0x08, 0x8000 | (i << 11));
  129. /*
  130. * Disable ingress rate limiting by resetting all ingress
  131. * rate limit registers to their initial state.
  132. */
  133. for (i = 0; i < 6; i++)
  134. REG_WRITE(REG_GLOBAL2, 0x09, 0x9000 | (i << 8));
  135. /*
  136. * Initialise cross-chip port VLAN table to reset defaults.
  137. */
  138. REG_WRITE(REG_GLOBAL2, 0x0b, 0x9000);
  139. /*
  140. * Clear the priority override table.
  141. */
  142. for (i = 0; i < 16; i++)
  143. REG_WRITE(REG_GLOBAL2, 0x0f, 0x8000 | (i << 8));
  144. /* @@@ initialise AVB (22/23) watchdog (27) sdet (29) registers */
  145. return 0;
  146. }
  147. static int mv88e6123_61_65_setup_port(struct dsa_switch *ds, int p)
  148. {
  149. int addr = REG_PORT(p);
  150. /*
  151. * MAC Forcing register: don't force link, speed, duplex
  152. * or flow control state to any particular values.
  153. */
  154. REG_WRITE(addr, 0x01, 0x0003);
  155. /*
  156. * Do not limit the period of time that this port can be
  157. * paused for by the remote end or the period of time that
  158. * this port can pause the remote end.
  159. */
  160. REG_WRITE(addr, 0x02, 0x0000);
  161. /*
  162. * Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
  163. * configure the requested (DSA/EDSA) tagging mode if this is
  164. * the CPU port, disable Header mode, enable IGMP/MLD snooping,
  165. * disable VLAN tunneling, determine priority by looking at
  166. * 802.1p and IP priority fields (IP prio has precedence), and
  167. * set STP state to Forwarding. Finally, if this is the CPU
  168. * port, additionally enable forwarding of unknown unicast and
  169. * multicast addresses.
  170. */
  171. REG_WRITE(addr, 0x04,
  172. (p == ds->cpu_port) ?
  173. (ds->tag_protocol == htons(ETH_P_DSA)) ?
  174. 0x053f : 0x373f :
  175. 0x0433);
  176. /*
  177. * Port Control 1: disable trunking. Also, if this is the
  178. * CPU port, enable learn messages to be sent to this port.
  179. */
  180. REG_WRITE(addr, 0x05, (p == ds->cpu_port) ? 0x8000 : 0x0000);
  181. /*
  182. * Port based VLAN map: give each port its own address
  183. * database, allow the CPU port to talk to each of the 'real'
  184. * ports, and allow each of the 'real' ports to only talk to
  185. * the CPU port.
  186. */
  187. REG_WRITE(addr, 0x06,
  188. ((p & 0xf) << 12) |
  189. ((p == ds->cpu_port) ?
  190. ds->valid_port_mask :
  191. (1 << ds->cpu_port)));
  192. /*
  193. * Default VLAN ID and priority: don't set a default VLAN
  194. * ID, and set the default packet priority to zero.
  195. */
  196. REG_WRITE(addr, 0x07, 0x0000);
  197. /*
  198. * Port Control 2: don't force a good FCS, set the maximum
  199. * frame size to 10240 bytes, don't let the switch add or
  200. * strip 802.1q tags, don't discard tagged or untagged frames
  201. * on this port, do a destination address lookup on all
  202. * received packets as usual, disable ARP mirroring and don't
  203. * send a copy of all transmitted/received frames on this port
  204. * to the CPU.
  205. */
  206. REG_WRITE(addr, 0x08, 0x2080);
  207. /*
  208. * Egress rate control: disable egress rate control.
  209. */
  210. REG_WRITE(addr, 0x09, 0x0001);
  211. /*
  212. * Egress rate control 2: disable egress rate control.
  213. */
  214. REG_WRITE(addr, 0x0a, 0x0000);
  215. /*
  216. * Port Association Vector: when learning source addresses
  217. * of packets, add the address to the address database using
  218. * a port bitmap that has only the bit for this port set and
  219. * the other bits clear.
  220. */
  221. REG_WRITE(addr, 0x0b, 1 << p);
  222. /*
  223. * Port ATU control: disable limiting the number of address
  224. * database entries that this port is allowed to use.
  225. */
  226. REG_WRITE(addr, 0x0c, 0x0000);
  227. /*
  228. * Priorit Override: disable DA, SA and VTU priority override.
  229. */
  230. REG_WRITE(addr, 0x0d, 0x0000);
  231. /*
  232. * Port Ethertype: use the Ethertype DSA Ethertype value.
  233. */
  234. REG_WRITE(addr, 0x0f, ETH_P_EDSA);
  235. /*
  236. * Tag Remap: use an identity 802.1p prio -> switch prio
  237. * mapping.
  238. */
  239. REG_WRITE(addr, 0x18, 0x3210);
  240. /*
  241. * Tag Remap 2: use an identity 802.1p prio -> switch prio
  242. * mapping.
  243. */
  244. REG_WRITE(addr, 0x19, 0x7654);
  245. return 0;
  246. }
  247. static int mv88e6123_61_65_setup(struct dsa_switch *ds)
  248. {
  249. struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
  250. int i;
  251. int ret;
  252. mutex_init(&ps->smi_mutex);
  253. mutex_init(&ps->stats_mutex);
  254. ret = mv88e6123_61_65_switch_reset(ds);
  255. if (ret < 0)
  256. return ret;
  257. /* @@@ initialise vtu and atu */
  258. ret = mv88e6123_61_65_setup_global(ds);
  259. if (ret < 0)
  260. return ret;
  261. for (i = 0; i < 6; i++) {
  262. ret = mv88e6123_61_65_setup_port(ds, i);
  263. if (ret < 0)
  264. return ret;
  265. }
  266. return 0;
  267. }
  268. static int mv88e6123_61_65_port_to_phy_addr(int port)
  269. {
  270. if (port >= 0 && port <= 4)
  271. return port;
  272. return -1;
  273. }
  274. static int
  275. mv88e6123_61_65_phy_read(struct dsa_switch *ds, int port, int regnum)
  276. {
  277. int addr = mv88e6123_61_65_port_to_phy_addr(port);
  278. return mv88e6xxx_phy_read(ds, addr, regnum);
  279. }
  280. static int
  281. mv88e6123_61_65_phy_write(struct dsa_switch *ds,
  282. int port, int regnum, u16 val)
  283. {
  284. int addr = mv88e6123_61_65_port_to_phy_addr(port);
  285. return mv88e6xxx_phy_write(ds, addr, regnum, val);
  286. }
  287. static struct mv88e6xxx_hw_stat mv88e6123_61_65_hw_stats[] = {
  288. { "in_good_octets", 8, 0x00, },
  289. { "in_bad_octets", 4, 0x02, },
  290. { "in_unicast", 4, 0x04, },
  291. { "in_broadcasts", 4, 0x06, },
  292. { "in_multicasts", 4, 0x07, },
  293. { "in_pause", 4, 0x16, },
  294. { "in_undersize", 4, 0x18, },
  295. { "in_fragments", 4, 0x19, },
  296. { "in_oversize", 4, 0x1a, },
  297. { "in_jabber", 4, 0x1b, },
  298. { "in_rx_error", 4, 0x1c, },
  299. { "in_fcs_error", 4, 0x1d, },
  300. { "out_octets", 8, 0x0e, },
  301. { "out_unicast", 4, 0x10, },
  302. { "out_broadcasts", 4, 0x13, },
  303. { "out_multicasts", 4, 0x12, },
  304. { "out_pause", 4, 0x15, },
  305. { "excessive", 4, 0x11, },
  306. { "collisions", 4, 0x1e, },
  307. { "deferred", 4, 0x05, },
  308. { "single", 4, 0x14, },
  309. { "multiple", 4, 0x17, },
  310. { "out_fcs_error", 4, 0x03, },
  311. { "late", 4, 0x1f, },
  312. { "hist_64bytes", 4, 0x08, },
  313. { "hist_65_127bytes", 4, 0x09, },
  314. { "hist_128_255bytes", 4, 0x0a, },
  315. { "hist_256_511bytes", 4, 0x0b, },
  316. { "hist_512_1023bytes", 4, 0x0c, },
  317. { "hist_1024_max_bytes", 4, 0x0d, },
  318. };
  319. static void
  320. mv88e6123_61_65_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
  321. {
  322. mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6123_61_65_hw_stats),
  323. mv88e6123_61_65_hw_stats, port, data);
  324. }
  325. static void
  326. mv88e6123_61_65_get_ethtool_stats(struct dsa_switch *ds,
  327. int port, uint64_t *data)
  328. {
  329. mv88e6xxx_get_ethtool_stats(ds, ARRAY_SIZE(mv88e6123_61_65_hw_stats),
  330. mv88e6123_61_65_hw_stats, port, data);
  331. }
  332. static int mv88e6123_61_65_get_sset_count(struct dsa_switch *ds)
  333. {
  334. return ARRAY_SIZE(mv88e6123_61_65_hw_stats);
  335. }
  336. static struct dsa_switch_driver mv88e6123_61_65_switch_driver = {
  337. .tag_protocol = __constant_htons(ETH_P_EDSA),
  338. .priv_size = sizeof(struct mv88e6xxx_priv_state),
  339. .probe = mv88e6123_61_65_probe,
  340. .setup = mv88e6123_61_65_setup,
  341. .set_addr = mv88e6xxx_set_addr_indirect,
  342. .phy_read = mv88e6123_61_65_phy_read,
  343. .phy_write = mv88e6123_61_65_phy_write,
  344. .poll_link = mv88e6xxx_poll_link,
  345. .get_strings = mv88e6123_61_65_get_strings,
  346. .get_ethtool_stats = mv88e6123_61_65_get_ethtool_stats,
  347. .get_sset_count = mv88e6123_61_65_get_sset_count,
  348. };
  349. static int __init mv88e6123_61_65_init(void)
  350. {
  351. register_switch_driver(&mv88e6123_61_65_switch_driver);
  352. return 0;
  353. }
  354. module_init(mv88e6123_61_65_init);
  355. static void __exit mv88e6123_61_65_cleanup(void)
  356. {
  357. unregister_switch_driver(&mv88e6123_61_65_switch_driver);
  358. }
  359. module_exit(mv88e6123_61_65_cleanup);