mv88e6171.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /* net/dsa/mv88e6171.c - Marvell 88e6171/8826172 switch chip support
  2. * Copyright (c) 2008-2009 Marvell Semiconductor
  3. * Copyright (c) 2014 Claudio Leite <leitec@staticky.com>
  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 *mv88e6171_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. if ((ret & 0xfff0) == 0x1710)
  27. return "Marvell 88E6171";
  28. if ((ret & 0xfff0) == 0x1720)
  29. return "Marvell 88E6172";
  30. }
  31. return NULL;
  32. }
  33. static int mv88e6171_switch_reset(struct dsa_switch *ds)
  34. {
  35. int i;
  36. int ret;
  37. unsigned long timeout;
  38. /* Set all ports to the disabled state. */
  39. for (i = 0; i < 8; i++) {
  40. ret = REG_READ(REG_PORT(i), 0x04);
  41. REG_WRITE(REG_PORT(i), 0x04, ret & 0xfffc);
  42. }
  43. /* Wait for transmit queues to drain. */
  44. usleep_range(2000, 4000);
  45. /* Reset the switch. Keep PPU active. The PPU needs to be
  46. * active to support indirect phy register accesses through
  47. * global registers 0x18 and 0x19.
  48. */
  49. REG_WRITE(REG_GLOBAL, 0x04, 0xc000);
  50. /* Wait up to one second for reset to complete. */
  51. timeout = jiffies + 1 * HZ;
  52. while (time_before(jiffies, timeout)) {
  53. ret = REG_READ(REG_GLOBAL, 0x00);
  54. if ((ret & 0xc800) == 0xc800)
  55. break;
  56. usleep_range(1000, 2000);
  57. }
  58. if (time_after(jiffies, timeout))
  59. return -ETIMEDOUT;
  60. /* Enable ports not under DSA, e.g. WAN port */
  61. for (i = 0; i < 8; i++) {
  62. if (dsa_is_cpu_port(ds, i) || ds->phys_port_mask & (1 << i))
  63. continue;
  64. ret = REG_READ(REG_PORT(i), 0x04);
  65. REG_WRITE(REG_PORT(i), 0x04, ret | 0x03);
  66. }
  67. return 0;
  68. }
  69. static int mv88e6171_setup_global(struct dsa_switch *ds)
  70. {
  71. int ret;
  72. int i;
  73. /* Discard packets with excessive collisions, mask all
  74. * interrupt sources, enable PPU.
  75. */
  76. REG_WRITE(REG_GLOBAL, 0x04, 0x6000);
  77. /* Set the default address aging time to 5 minutes, and
  78. * enable address learn messages to be sent to all message
  79. * ports.
  80. */
  81. REG_WRITE(REG_GLOBAL, 0x0a, 0x0148);
  82. /* Configure the priority mapping registers. */
  83. ret = mv88e6xxx_config_prio(ds);
  84. if (ret < 0)
  85. return ret;
  86. /* Configure the upstream port, and configure the upstream
  87. * port as the port to which ingress and egress monitor frames
  88. * are to be sent.
  89. */
  90. if (REG_READ(REG_PORT(0), 0x03) == 0x1710)
  91. REG_WRITE(REG_GLOBAL, 0x1a, (dsa_upstream_port(ds) * 0x1111));
  92. else
  93. REG_WRITE(REG_GLOBAL, 0x1a, (dsa_upstream_port(ds) * 0x1110));
  94. /* Disable remote management for now, and set the switch's
  95. * DSA device number.
  96. */
  97. REG_WRITE(REG_GLOBAL, 0x1c, ds->index & 0x1f);
  98. /* Send all frames with destination addresses matching
  99. * 01:80:c2:00:00:2x to the CPU port.
  100. */
  101. REG_WRITE(REG_GLOBAL2, 0x02, 0xffff);
  102. /* Send all frames with destination addresses matching
  103. * 01:80:c2:00:00:0x to the CPU port.
  104. */
  105. REG_WRITE(REG_GLOBAL2, 0x03, 0xffff);
  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. /* Program the DSA routing table. */
  115. for (i = 0; i < 32; i++) {
  116. int nexthop;
  117. nexthop = 0x1f;
  118. if (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) | 0xff);
  125. /* Clear all trunk mappings. */
  126. for (i = 0; i < 16; i++)
  127. REG_WRITE(REG_GLOBAL2, 0x08, 0x8000 | (i << 11));
  128. /* Disable ingress rate limiting by resetting all ingress
  129. * rate limit registers to their initial state.
  130. */
  131. for (i = 0; i < 6; i++)
  132. REG_WRITE(REG_GLOBAL2, 0x09, 0x9000 | (i << 8));
  133. /* Initialise cross-chip port VLAN table to reset defaults. */
  134. REG_WRITE(REG_GLOBAL2, 0x0b, 0x9000);
  135. /* Clear the priority override table. */
  136. for (i = 0; i < 16; i++)
  137. REG_WRITE(REG_GLOBAL2, 0x0f, 0x8000 | (i << 8));
  138. /* @@@ initialise AVB (22/23) watchdog (27) sdet (29) registers */
  139. return 0;
  140. }
  141. static int mv88e6171_setup_port(struct dsa_switch *ds, int p)
  142. {
  143. int addr = REG_PORT(p);
  144. u16 val;
  145. /* MAC Forcing register: don't force link, speed, duplex
  146. * or flow control state to any particular values on physical
  147. * ports, but force the CPU port and all DSA ports to 1000 Mb/s
  148. * full duplex.
  149. */
  150. val = REG_READ(addr, 0x01);
  151. if (dsa_is_cpu_port(ds, p) || ds->dsa_port_mask & (1 << p))
  152. REG_WRITE(addr, 0x01, val | 0x003e);
  153. else
  154. REG_WRITE(addr, 0x01, val | 0x0003);
  155. /* Do not limit the period of time that this port can be
  156. * paused for by the remote end or the period of time that
  157. * this port can pause the remote end.
  158. */
  159. REG_WRITE(addr, 0x02, 0x0000);
  160. /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
  161. * disable Header mode, enable IGMP/MLD snooping, disable VLAN
  162. * tunneling, determine priority by looking at 802.1p and IP
  163. * priority fields (IP prio has precedence), and set STP state
  164. * to Forwarding.
  165. *
  166. * If this is the CPU link, use DSA or EDSA tagging depending
  167. * on which tagging mode was configured.
  168. *
  169. * If this is a link to another switch, use DSA tagging mode.
  170. *
  171. * If this is the upstream port for this switch, enable
  172. * forwarding of unknown unicasts and multicasts.
  173. */
  174. val = 0x0433;
  175. if (dsa_is_cpu_port(ds, p)) {
  176. if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
  177. val |= 0x3300;
  178. else
  179. val |= 0x0100;
  180. }
  181. if (ds->dsa_port_mask & (1 << p))
  182. val |= 0x0100;
  183. if (p == dsa_upstream_port(ds))
  184. val |= 0x000c;
  185. REG_WRITE(addr, 0x04, val);
  186. /* Port Control 1: disable trunking. Also, if this is the
  187. * CPU port, enable learn messages to be sent to this port.
  188. */
  189. REG_WRITE(addr, 0x05, dsa_is_cpu_port(ds, p) ? 0x8000 : 0x0000);
  190. /* Port based VLAN map: give each port its own address
  191. * database, allow the CPU port to talk to each of the 'real'
  192. * ports, and allow each of the 'real' ports to only talk to
  193. * the upstream port.
  194. */
  195. val = (p & 0xf) << 12;
  196. if (dsa_is_cpu_port(ds, p))
  197. val |= ds->phys_port_mask;
  198. else
  199. val |= 1 << dsa_upstream_port(ds);
  200. REG_WRITE(addr, 0x06, val);
  201. /* Default VLAN ID and priority: don't set a default VLAN
  202. * ID, and set the default packet priority to zero.
  203. */
  204. REG_WRITE(addr, 0x07, 0x0000);
  205. /* Port Control 2: don't force a good FCS, set the maximum
  206. * frame size to 10240 bytes, don't let the switch add or
  207. * strip 802.1q tags, don't discard tagged or untagged frames
  208. * on this port, do a destination address lookup on all
  209. * received packets as usual, disable ARP mirroring and don't
  210. * send a copy of all transmitted/received frames on this port
  211. * to the CPU.
  212. */
  213. REG_WRITE(addr, 0x08, 0x2080);
  214. /* Egress rate control: disable egress rate control. */
  215. REG_WRITE(addr, 0x09, 0x0001);
  216. /* Egress rate control 2: disable egress rate control. */
  217. REG_WRITE(addr, 0x0a, 0x0000);
  218. /* Port Association Vector: when learning source addresses
  219. * of packets, add the address to the address database using
  220. * a port bitmap that has only the bit for this port set and
  221. * the other bits clear.
  222. */
  223. REG_WRITE(addr, 0x0b, 1 << p);
  224. /* Port ATU control: disable limiting the number of address
  225. * database entries that this port is allowed to use.
  226. */
  227. REG_WRITE(addr, 0x0c, 0x0000);
  228. /* Priority Override: disable DA, SA and VTU priority override. */
  229. REG_WRITE(addr, 0x0d, 0x0000);
  230. /* Port Ethertype: use the Ethertype DSA Ethertype value. */
  231. REG_WRITE(addr, 0x0f, ETH_P_EDSA);
  232. /* Tag Remap: use an identity 802.1p prio -> switch prio
  233. * mapping.
  234. */
  235. REG_WRITE(addr, 0x18, 0x3210);
  236. /* Tag Remap 2: use an identity 802.1p prio -> switch prio
  237. * mapping.
  238. */
  239. REG_WRITE(addr, 0x19, 0x7654);
  240. return 0;
  241. }
  242. static int mv88e6171_setup(struct dsa_switch *ds)
  243. {
  244. struct mv88e6xxx_priv_state *ps = (void *)(ds + 1);
  245. int i;
  246. int ret;
  247. mutex_init(&ps->smi_mutex);
  248. mutex_init(&ps->stats_mutex);
  249. ret = mv88e6171_switch_reset(ds);
  250. if (ret < 0)
  251. return ret;
  252. /* @@@ initialise vtu and atu */
  253. ret = mv88e6171_setup_global(ds);
  254. if (ret < 0)
  255. return ret;
  256. for (i = 0; i < 8; i++) {
  257. if (!(dsa_is_cpu_port(ds, i) || ds->phys_port_mask & (1 << i)))
  258. continue;
  259. ret = mv88e6171_setup_port(ds, i);
  260. if (ret < 0)
  261. return ret;
  262. }
  263. mutex_init(&ps->phy_mutex);
  264. return 0;
  265. }
  266. static int mv88e6171_port_to_phy_addr(int port)
  267. {
  268. if (port >= 0 && port <= 4)
  269. return port;
  270. return -1;
  271. }
  272. static int
  273. mv88e6171_phy_read(struct dsa_switch *ds, int port, int regnum)
  274. {
  275. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  276. int addr = mv88e6171_port_to_phy_addr(port);
  277. int ret;
  278. mutex_lock(&ps->phy_mutex);
  279. ret = mv88e6xxx_phy_read_indirect(ds, addr, regnum);
  280. mutex_unlock(&ps->phy_mutex);
  281. return ret;
  282. }
  283. static int
  284. mv88e6171_phy_write(struct dsa_switch *ds,
  285. int port, int regnum, u16 val)
  286. {
  287. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  288. int addr = mv88e6171_port_to_phy_addr(port);
  289. int ret;
  290. mutex_lock(&ps->phy_mutex);
  291. ret = mv88e6xxx_phy_write_indirect(ds, addr, regnum, val);
  292. mutex_unlock(&ps->phy_mutex);
  293. return ret;
  294. }
  295. static struct mv88e6xxx_hw_stat mv88e6171_hw_stats[] = {
  296. { "in_good_octets", 8, 0x00, },
  297. { "in_bad_octets", 4, 0x02, },
  298. { "in_unicast", 4, 0x04, },
  299. { "in_broadcasts", 4, 0x06, },
  300. { "in_multicasts", 4, 0x07, },
  301. { "in_pause", 4, 0x16, },
  302. { "in_undersize", 4, 0x18, },
  303. { "in_fragments", 4, 0x19, },
  304. { "in_oversize", 4, 0x1a, },
  305. { "in_jabber", 4, 0x1b, },
  306. { "in_rx_error", 4, 0x1c, },
  307. { "in_fcs_error", 4, 0x1d, },
  308. { "out_octets", 8, 0x0e, },
  309. { "out_unicast", 4, 0x10, },
  310. { "out_broadcasts", 4, 0x13, },
  311. { "out_multicasts", 4, 0x12, },
  312. { "out_pause", 4, 0x15, },
  313. { "excessive", 4, 0x11, },
  314. { "collisions", 4, 0x1e, },
  315. { "deferred", 4, 0x05, },
  316. { "single", 4, 0x14, },
  317. { "multiple", 4, 0x17, },
  318. { "out_fcs_error", 4, 0x03, },
  319. { "late", 4, 0x1f, },
  320. { "hist_64bytes", 4, 0x08, },
  321. { "hist_65_127bytes", 4, 0x09, },
  322. { "hist_128_255bytes", 4, 0x0a, },
  323. { "hist_256_511bytes", 4, 0x0b, },
  324. { "hist_512_1023bytes", 4, 0x0c, },
  325. { "hist_1024_max_bytes", 4, 0x0d, },
  326. };
  327. static void
  328. mv88e6171_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
  329. {
  330. mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6171_hw_stats),
  331. mv88e6171_hw_stats, port, data);
  332. }
  333. static void
  334. mv88e6171_get_ethtool_stats(struct dsa_switch *ds,
  335. int port, uint64_t *data)
  336. {
  337. mv88e6xxx_get_ethtool_stats(ds, ARRAY_SIZE(mv88e6171_hw_stats),
  338. mv88e6171_hw_stats, port, data);
  339. }
  340. static int mv88e6171_get_sset_count(struct dsa_switch *ds)
  341. {
  342. return ARRAY_SIZE(mv88e6171_hw_stats);
  343. }
  344. struct dsa_switch_driver mv88e6171_switch_driver = {
  345. .tag_protocol = DSA_TAG_PROTO_EDSA,
  346. .priv_size = sizeof(struct mv88e6xxx_priv_state),
  347. .probe = mv88e6171_probe,
  348. .setup = mv88e6171_setup,
  349. .set_addr = mv88e6xxx_set_addr_indirect,
  350. .phy_read = mv88e6171_phy_read,
  351. .phy_write = mv88e6171_phy_write,
  352. .poll_link = mv88e6xxx_poll_link,
  353. .get_strings = mv88e6171_get_strings,
  354. .get_ethtool_stats = mv88e6171_get_ethtool_stats,
  355. .get_sset_count = mv88e6171_get_sset_count,
  356. #ifdef CONFIG_NET_DSA_HWMON
  357. .get_temp = mv88e6xxx_get_temp,
  358. #endif
  359. .get_regs_len = mv88e6xxx_get_regs_len,
  360. .get_regs = mv88e6xxx_get_regs,
  361. };
  362. MODULE_ALIAS("platform:mv88e6171");
  363. MODULE_ALIAS("platform:mv88e6172");