mv88e6131.c 10 KB

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