mv88e6131.c 11 KB

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