mv88e6352.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * net/dsa/mv88e6352.c - Marvell 88e6352 switch chip support
  3. *
  4. * Copyright (c) 2014 Guenter Roeck
  5. *
  6. * Derived from mv88e6123_61_65.c
  7. * Copyright (c) 2008-2009 Marvell Semiconductor
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/list.h>
  17. #include <linux/module.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/phy.h>
  21. #include <net/dsa.h>
  22. #include "mv88e6xxx.h"
  23. static char *mv88e6352_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), PORT_SWITCH_ID);
  30. if (ret >= 0) {
  31. if ((ret & 0xfff0) == PORT_SWITCH_ID_6176)
  32. return "Marvell 88E6176";
  33. if (ret == PORT_SWITCH_ID_6352_A0)
  34. return "Marvell 88E6352 (A0)";
  35. if (ret == PORT_SWITCH_ID_6352_A1)
  36. return "Marvell 88E6352 (A1)";
  37. if ((ret & 0xfff0) == PORT_SWITCH_ID_6352)
  38. return "Marvell 88E6352";
  39. }
  40. return NULL;
  41. }
  42. static int mv88e6352_setup_global(struct dsa_switch *ds)
  43. {
  44. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  45. int ret;
  46. int i;
  47. /* Discard packets with excessive collisions,
  48. * mask all interrupt sources, enable PPU (bit 14, undocumented).
  49. */
  50. REG_WRITE(REG_GLOBAL, 0x04, 0x6000);
  51. /* Set the default address aging time to 5 minutes, and
  52. * enable address learn messages to be sent to all message
  53. * ports.
  54. */
  55. REG_WRITE(REG_GLOBAL, 0x0a, 0x0148);
  56. /* Configure the priority mapping registers. */
  57. ret = mv88e6xxx_config_prio(ds);
  58. if (ret < 0)
  59. return ret;
  60. /* Configure the upstream port, and configure the upstream
  61. * port as the port to which ingress and egress monitor frames
  62. * are to be sent.
  63. */
  64. REG_WRITE(REG_GLOBAL, 0x1a, (dsa_upstream_port(ds) * 0x1110));
  65. /* Disable remote management for now, and set the switch's
  66. * DSA device number.
  67. */
  68. REG_WRITE(REG_GLOBAL, 0x1c, ds->index & 0x1f);
  69. /* Send all frames with destination addresses matching
  70. * 01:80:c2:00:00:2x to the CPU port.
  71. */
  72. REG_WRITE(REG_GLOBAL2, 0x02, 0xffff);
  73. /* Send all frames with destination addresses matching
  74. * 01:80:c2:00:00:0x to the CPU port.
  75. */
  76. REG_WRITE(REG_GLOBAL2, 0x03, 0xffff);
  77. /* Disable the loopback filter, disable flow control
  78. * messages, disable flood broadcast override, disable
  79. * removing of provider tags, disable ATU age violation
  80. * interrupts, disable tag flow control, force flow
  81. * control priority to the highest, and send all special
  82. * multicast frames to the CPU at the highest priority.
  83. */
  84. REG_WRITE(REG_GLOBAL2, 0x05, 0x00ff);
  85. /* Program the DSA routing table. */
  86. for (i = 0; i < 32; i++) {
  87. int nexthop = 0x1f;
  88. if (i != ds->index && i < ds->dst->pd->nr_chips)
  89. nexthop = ds->pd->rtable[i] & 0x1f;
  90. REG_WRITE(REG_GLOBAL2, 0x06, 0x8000 | (i << 8) | nexthop);
  91. }
  92. /* Clear all trunk masks. */
  93. for (i = 0; i < 8; i++)
  94. REG_WRITE(REG_GLOBAL2, 0x07, 0x8000 | (i << 12) | 0x7f);
  95. /* Clear all trunk mappings. */
  96. for (i = 0; i < 16; i++)
  97. REG_WRITE(REG_GLOBAL2, 0x08, 0x8000 | (i << 11));
  98. /* Disable ingress rate limiting by resetting all ingress
  99. * rate limit registers to their initial state.
  100. */
  101. for (i = 0; i < ps->num_ports; i++)
  102. REG_WRITE(REG_GLOBAL2, 0x09, 0x9000 | (i << 8));
  103. /* Initialise cross-chip port VLAN table to reset defaults. */
  104. REG_WRITE(REG_GLOBAL2, 0x0b, 0x9000);
  105. /* Clear the priority override table. */
  106. for (i = 0; i < 16; i++)
  107. REG_WRITE(REG_GLOBAL2, 0x0f, 0x8000 | (i << 8));
  108. /* @@@ initialise AVB (22/23) watchdog (27) sdet (29) registers */
  109. return 0;
  110. }
  111. static int mv88e6352_setup_port(struct dsa_switch *ds, int p)
  112. {
  113. int addr = REG_PORT(p);
  114. u16 val;
  115. /* MAC Forcing register: don't force link, speed, duplex
  116. * or flow control state to any particular values on physical
  117. * ports, but force the CPU port and all DSA ports to 1000 Mb/s
  118. * full duplex.
  119. */
  120. if (dsa_is_cpu_port(ds, p) || ds->dsa_port_mask & (1 << p))
  121. REG_WRITE(addr, 0x01, 0x003e);
  122. else
  123. REG_WRITE(addr, 0x01, 0x0003);
  124. /* Do not limit the period of time that this port can be
  125. * paused for by the remote end or the period of time that
  126. * this port can pause the remote end.
  127. */
  128. REG_WRITE(addr, 0x02, 0x0000);
  129. /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
  130. * disable Header mode, enable IGMP/MLD snooping, disable VLAN
  131. * tunneling, determine priority by looking at 802.1p and IP
  132. * priority fields (IP prio has precedence), and set STP state
  133. * to Forwarding.
  134. *
  135. * If this is the CPU link, use DSA or EDSA tagging depending
  136. * on which tagging mode was configured.
  137. *
  138. * If this is a link to another switch, use DSA tagging mode.
  139. *
  140. * If this is the upstream port for this switch, enable
  141. * forwarding of unknown unicasts and multicasts.
  142. */
  143. val = 0x0433;
  144. if (dsa_is_cpu_port(ds, p)) {
  145. if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
  146. val |= 0x3300;
  147. else
  148. val |= 0x0100;
  149. }
  150. if (ds->dsa_port_mask & (1 << p))
  151. val |= 0x0100;
  152. if (p == dsa_upstream_port(ds))
  153. val |= 0x000c;
  154. REG_WRITE(addr, 0x04, val);
  155. /* Port Control 2: don't force a good FCS, set the maximum
  156. * frame size to 10240 bytes, don't let the switch add or
  157. * strip 802.1q tags, don't discard tagged or untagged frames
  158. * on this port, do a destination address lookup on all
  159. * received packets as usual, disable ARP mirroring and don't
  160. * send a copy of all transmitted/received frames on this port
  161. * to the CPU.
  162. */
  163. REG_WRITE(addr, 0x08, 0x2080);
  164. /* Egress rate control: disable egress rate control. */
  165. REG_WRITE(addr, 0x09, 0x0001);
  166. /* Egress rate control 2: disable egress rate control. */
  167. REG_WRITE(addr, 0x0a, 0x0000);
  168. /* Port Association Vector: when learning source addresses
  169. * of packets, add the address to the address database using
  170. * a port bitmap that has only the bit for this port set and
  171. * the other bits clear.
  172. */
  173. REG_WRITE(addr, 0x0b, 1 << p);
  174. /* Port ATU control: disable limiting the number of address
  175. * database entries that this port is allowed to use.
  176. */
  177. REG_WRITE(addr, 0x0c, 0x0000);
  178. /* Priority Override: disable DA, SA and VTU priority override. */
  179. REG_WRITE(addr, 0x0d, 0x0000);
  180. /* Port Ethertype: use the Ethertype DSA Ethertype value. */
  181. REG_WRITE(addr, 0x0f, ETH_P_EDSA);
  182. /* Tag Remap: use an identity 802.1p prio -> switch prio
  183. * mapping.
  184. */
  185. REG_WRITE(addr, 0x18, 0x3210);
  186. /* Tag Remap 2: use an identity 802.1p prio -> switch prio
  187. * mapping.
  188. */
  189. REG_WRITE(addr, 0x19, 0x7654);
  190. return mv88e6xxx_setup_port_common(ds, p);
  191. }
  192. #ifdef CONFIG_NET_DSA_HWMON
  193. static int mv88e6352_get_temp(struct dsa_switch *ds, int *temp)
  194. {
  195. int ret;
  196. *temp = 0;
  197. ret = mv88e6xxx_phy_page_read(ds, 0, 6, 27);
  198. if (ret < 0)
  199. return ret;
  200. *temp = (ret & 0xff) - 25;
  201. return 0;
  202. }
  203. static int mv88e6352_get_temp_limit(struct dsa_switch *ds, int *temp)
  204. {
  205. int ret;
  206. *temp = 0;
  207. ret = mv88e6xxx_phy_page_read(ds, 0, 6, 26);
  208. if (ret < 0)
  209. return ret;
  210. *temp = (((ret >> 8) & 0x1f) * 5) - 25;
  211. return 0;
  212. }
  213. static int mv88e6352_set_temp_limit(struct dsa_switch *ds, int temp)
  214. {
  215. int ret;
  216. ret = mv88e6xxx_phy_page_read(ds, 0, 6, 26);
  217. if (ret < 0)
  218. return ret;
  219. temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
  220. return mv88e6xxx_phy_page_write(ds, 0, 6, 26,
  221. (ret & 0xe0ff) | (temp << 8));
  222. }
  223. static int mv88e6352_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
  224. {
  225. int ret;
  226. *alarm = false;
  227. ret = mv88e6xxx_phy_page_read(ds, 0, 6, 26);
  228. if (ret < 0)
  229. return ret;
  230. *alarm = !!(ret & 0x40);
  231. return 0;
  232. }
  233. #endif /* CONFIG_NET_DSA_HWMON */
  234. static int mv88e6352_setup(struct dsa_switch *ds)
  235. {
  236. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  237. int ret;
  238. int i;
  239. ret = mv88e6xxx_setup_common(ds);
  240. if (ret < 0)
  241. return ret;
  242. ps->num_ports = 7;
  243. mutex_init(&ps->eeprom_mutex);
  244. ret = mv88e6xxx_switch_reset(ds, true);
  245. if (ret < 0)
  246. return ret;
  247. /* @@@ initialise vtu and atu */
  248. ret = mv88e6352_setup_global(ds);
  249. if (ret < 0)
  250. return ret;
  251. for (i = 0; i < ps->num_ports; i++) {
  252. ret = mv88e6352_setup_port(ds, i);
  253. if (ret < 0)
  254. return ret;
  255. }
  256. return 0;
  257. }
  258. static int mv88e6352_read_eeprom_word(struct dsa_switch *ds, int addr)
  259. {
  260. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  261. int ret;
  262. mutex_lock(&ps->eeprom_mutex);
  263. ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14,
  264. 0xc000 | (addr & 0xff));
  265. if (ret < 0)
  266. goto error;
  267. ret = mv88e6xxx_eeprom_busy_wait(ds);
  268. if (ret < 0)
  269. goto error;
  270. ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x15);
  271. error:
  272. mutex_unlock(&ps->eeprom_mutex);
  273. return ret;
  274. }
  275. static int mv88e6352_get_eeprom(struct dsa_switch *ds,
  276. struct ethtool_eeprom *eeprom, u8 *data)
  277. {
  278. int offset;
  279. int len;
  280. int ret;
  281. offset = eeprom->offset;
  282. len = eeprom->len;
  283. eeprom->len = 0;
  284. eeprom->magic = 0xc3ec4951;
  285. ret = mv88e6xxx_eeprom_load_wait(ds);
  286. if (ret < 0)
  287. return ret;
  288. if (offset & 1) {
  289. int word;
  290. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  291. if (word < 0)
  292. return word;
  293. *data++ = (word >> 8) & 0xff;
  294. offset++;
  295. len--;
  296. eeprom->len++;
  297. }
  298. while (len >= 2) {
  299. int word;
  300. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  301. if (word < 0)
  302. return word;
  303. *data++ = word & 0xff;
  304. *data++ = (word >> 8) & 0xff;
  305. offset += 2;
  306. len -= 2;
  307. eeprom->len += 2;
  308. }
  309. if (len) {
  310. int word;
  311. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  312. if (word < 0)
  313. return word;
  314. *data++ = word & 0xff;
  315. offset++;
  316. len--;
  317. eeprom->len++;
  318. }
  319. return 0;
  320. }
  321. static int mv88e6352_eeprom_is_readonly(struct dsa_switch *ds)
  322. {
  323. int ret;
  324. ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x14);
  325. if (ret < 0)
  326. return ret;
  327. if (!(ret & 0x0400))
  328. return -EROFS;
  329. return 0;
  330. }
  331. static int mv88e6352_write_eeprom_word(struct dsa_switch *ds, int addr,
  332. u16 data)
  333. {
  334. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  335. int ret;
  336. mutex_lock(&ps->eeprom_mutex);
  337. ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x15, data);
  338. if (ret < 0)
  339. goto error;
  340. ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14,
  341. 0xb000 | (addr & 0xff));
  342. if (ret < 0)
  343. goto error;
  344. ret = mv88e6xxx_eeprom_busy_wait(ds);
  345. error:
  346. mutex_unlock(&ps->eeprom_mutex);
  347. return ret;
  348. }
  349. static int mv88e6352_set_eeprom(struct dsa_switch *ds,
  350. struct ethtool_eeprom *eeprom, u8 *data)
  351. {
  352. int offset;
  353. int ret;
  354. int len;
  355. if (eeprom->magic != 0xc3ec4951)
  356. return -EINVAL;
  357. ret = mv88e6352_eeprom_is_readonly(ds);
  358. if (ret)
  359. return ret;
  360. offset = eeprom->offset;
  361. len = eeprom->len;
  362. eeprom->len = 0;
  363. ret = mv88e6xxx_eeprom_load_wait(ds);
  364. if (ret < 0)
  365. return ret;
  366. if (offset & 1) {
  367. int word;
  368. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  369. if (word < 0)
  370. return word;
  371. word = (*data++ << 8) | (word & 0xff);
  372. ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
  373. if (ret < 0)
  374. return ret;
  375. offset++;
  376. len--;
  377. eeprom->len++;
  378. }
  379. while (len >= 2) {
  380. int word;
  381. word = *data++;
  382. word |= *data++ << 8;
  383. ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
  384. if (ret < 0)
  385. return ret;
  386. offset += 2;
  387. len -= 2;
  388. eeprom->len += 2;
  389. }
  390. if (len) {
  391. int word;
  392. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  393. if (word < 0)
  394. return word;
  395. word = (word & 0xff00) | *data++;
  396. ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
  397. if (ret < 0)
  398. return ret;
  399. offset++;
  400. len--;
  401. eeprom->len++;
  402. }
  403. return 0;
  404. }
  405. struct dsa_switch_driver mv88e6352_switch_driver = {
  406. .tag_protocol = DSA_TAG_PROTO_EDSA,
  407. .priv_size = sizeof(struct mv88e6xxx_priv_state),
  408. .probe = mv88e6352_probe,
  409. .setup = mv88e6352_setup,
  410. .set_addr = mv88e6xxx_set_addr_indirect,
  411. .phy_read = mv88e6xxx_phy_read_indirect,
  412. .phy_write = mv88e6xxx_phy_write_indirect,
  413. .poll_link = mv88e6xxx_poll_link,
  414. .get_strings = mv88e6xxx_get_strings,
  415. .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
  416. .get_sset_count = mv88e6xxx_get_sset_count,
  417. .set_eee = mv88e6xxx_set_eee,
  418. .get_eee = mv88e6xxx_get_eee,
  419. #ifdef CONFIG_NET_DSA_HWMON
  420. .get_temp = mv88e6352_get_temp,
  421. .get_temp_limit = mv88e6352_get_temp_limit,
  422. .set_temp_limit = mv88e6352_set_temp_limit,
  423. .get_temp_alarm = mv88e6352_get_temp_alarm,
  424. #endif
  425. .get_eeprom = mv88e6352_get_eeprom,
  426. .set_eeprom = mv88e6352_set_eeprom,
  427. .get_regs_len = mv88e6xxx_get_regs_len,
  428. .get_regs = mv88e6xxx_get_regs,
  429. .port_join_bridge = mv88e6xxx_join_bridge,
  430. .port_leave_bridge = mv88e6xxx_leave_bridge,
  431. .port_stp_update = mv88e6xxx_port_stp_update,
  432. .fdb_add = mv88e6xxx_port_fdb_add,
  433. .fdb_del = mv88e6xxx_port_fdb_del,
  434. .fdb_getnext = mv88e6xxx_port_fdb_getnext,
  435. };
  436. MODULE_ALIAS("platform:mv88e6352");