mv88e6352.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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_6172)
  32. return "Marvell 88E6172";
  33. if ((ret & 0xfff0) == PORT_SWITCH_ID_6176)
  34. return "Marvell 88E6176";
  35. if (ret == PORT_SWITCH_ID_6320_A1)
  36. return "Marvell 88E6320 (A1)";
  37. if (ret == PORT_SWITCH_ID_6320_A2)
  38. return "Marvell 88e6320 (A2)";
  39. if ((ret & 0xfff0) == PORT_SWITCH_ID_6320)
  40. return "Marvell 88E6320";
  41. if (ret == PORT_SWITCH_ID_6321_A1)
  42. return "Marvell 88E6321 (A1)";
  43. if (ret == PORT_SWITCH_ID_6321_A2)
  44. return "Marvell 88e6321 (A2)";
  45. if ((ret & 0xfff0) == PORT_SWITCH_ID_6321)
  46. return "Marvell 88E6321";
  47. if (ret == PORT_SWITCH_ID_6352_A0)
  48. return "Marvell 88E6352 (A0)";
  49. if (ret == PORT_SWITCH_ID_6352_A1)
  50. return "Marvell 88E6352 (A1)";
  51. if ((ret & 0xfff0) == PORT_SWITCH_ID_6352)
  52. return "Marvell 88E6352";
  53. }
  54. return NULL;
  55. }
  56. static int mv88e6352_setup_global(struct dsa_switch *ds)
  57. {
  58. u32 upstream_port = dsa_upstream_port(ds);
  59. int ret;
  60. u32 reg;
  61. ret = mv88e6xxx_setup_global(ds);
  62. if (ret)
  63. return ret;
  64. /* Discard packets with excessive collisions,
  65. * mask all interrupt sources, enable PPU (bit 14, undocumented).
  66. */
  67. REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL,
  68. GLOBAL_CONTROL_PPU_ENABLE | GLOBAL_CONTROL_DISCARD_EXCESS);
  69. /* Configure the upstream port, and configure the upstream
  70. * port as the port to which ingress and egress monitor frames
  71. * are to be sent.
  72. */
  73. reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
  74. upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
  75. upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
  76. REG_WRITE(REG_GLOBAL, GLOBAL_MONITOR_CONTROL, reg);
  77. /* Disable remote management for now, and set the switch's
  78. * DSA device number.
  79. */
  80. REG_WRITE(REG_GLOBAL, 0x1c, ds->index & 0x1f);
  81. return 0;
  82. }
  83. static int mv88e6352_setup(struct dsa_switch *ds)
  84. {
  85. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  86. int ret;
  87. ret = mv88e6xxx_setup_common(ds);
  88. if (ret < 0)
  89. return ret;
  90. ps->num_ports = 7;
  91. mutex_init(&ps->eeprom_mutex);
  92. ret = mv88e6xxx_switch_reset(ds, true);
  93. if (ret < 0)
  94. return ret;
  95. ret = mv88e6352_setup_global(ds);
  96. if (ret < 0)
  97. return ret;
  98. return mv88e6xxx_setup_ports(ds);
  99. }
  100. static int mv88e6352_read_eeprom_word(struct dsa_switch *ds, int addr)
  101. {
  102. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  103. int ret;
  104. mutex_lock(&ps->eeprom_mutex);
  105. ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
  106. GLOBAL2_EEPROM_OP_READ |
  107. (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
  108. if (ret < 0)
  109. goto error;
  110. ret = mv88e6xxx_eeprom_busy_wait(ds);
  111. if (ret < 0)
  112. goto error;
  113. ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_EEPROM_DATA);
  114. error:
  115. mutex_unlock(&ps->eeprom_mutex);
  116. return ret;
  117. }
  118. static int mv88e6352_get_eeprom(struct dsa_switch *ds,
  119. struct ethtool_eeprom *eeprom, u8 *data)
  120. {
  121. int offset;
  122. int len;
  123. int ret;
  124. offset = eeprom->offset;
  125. len = eeprom->len;
  126. eeprom->len = 0;
  127. eeprom->magic = 0xc3ec4951;
  128. ret = mv88e6xxx_eeprom_load_wait(ds);
  129. if (ret < 0)
  130. return ret;
  131. if (offset & 1) {
  132. int word;
  133. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  134. if (word < 0)
  135. return word;
  136. *data++ = (word >> 8) & 0xff;
  137. offset++;
  138. len--;
  139. eeprom->len++;
  140. }
  141. while (len >= 2) {
  142. int word;
  143. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  144. if (word < 0)
  145. return word;
  146. *data++ = word & 0xff;
  147. *data++ = (word >> 8) & 0xff;
  148. offset += 2;
  149. len -= 2;
  150. eeprom->len += 2;
  151. }
  152. if (len) {
  153. int word;
  154. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  155. if (word < 0)
  156. return word;
  157. *data++ = word & 0xff;
  158. offset++;
  159. len--;
  160. eeprom->len++;
  161. }
  162. return 0;
  163. }
  164. static int mv88e6352_eeprom_is_readonly(struct dsa_switch *ds)
  165. {
  166. int ret;
  167. ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP);
  168. if (ret < 0)
  169. return ret;
  170. if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN))
  171. return -EROFS;
  172. return 0;
  173. }
  174. static int mv88e6352_write_eeprom_word(struct dsa_switch *ds, int addr,
  175. u16 data)
  176. {
  177. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  178. int ret;
  179. mutex_lock(&ps->eeprom_mutex);
  180. ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
  181. if (ret < 0)
  182. goto error;
  183. ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
  184. GLOBAL2_EEPROM_OP_WRITE |
  185. (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
  186. if (ret < 0)
  187. goto error;
  188. ret = mv88e6xxx_eeprom_busy_wait(ds);
  189. error:
  190. mutex_unlock(&ps->eeprom_mutex);
  191. return ret;
  192. }
  193. static int mv88e6352_set_eeprom(struct dsa_switch *ds,
  194. struct ethtool_eeprom *eeprom, u8 *data)
  195. {
  196. int offset;
  197. int ret;
  198. int len;
  199. if (eeprom->magic != 0xc3ec4951)
  200. return -EINVAL;
  201. ret = mv88e6352_eeprom_is_readonly(ds);
  202. if (ret)
  203. return ret;
  204. offset = eeprom->offset;
  205. len = eeprom->len;
  206. eeprom->len = 0;
  207. ret = mv88e6xxx_eeprom_load_wait(ds);
  208. if (ret < 0)
  209. return ret;
  210. if (offset & 1) {
  211. int word;
  212. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  213. if (word < 0)
  214. return word;
  215. word = (*data++ << 8) | (word & 0xff);
  216. ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
  217. if (ret < 0)
  218. return ret;
  219. offset++;
  220. len--;
  221. eeprom->len++;
  222. }
  223. while (len >= 2) {
  224. int word;
  225. word = *data++;
  226. word |= *data++ << 8;
  227. ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
  228. if (ret < 0)
  229. return ret;
  230. offset += 2;
  231. len -= 2;
  232. eeprom->len += 2;
  233. }
  234. if (len) {
  235. int word;
  236. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  237. if (word < 0)
  238. return word;
  239. word = (word & 0xff00) | *data++;
  240. ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
  241. if (ret < 0)
  242. return ret;
  243. offset++;
  244. len--;
  245. eeprom->len++;
  246. }
  247. return 0;
  248. }
  249. struct dsa_switch_driver mv88e6352_switch_driver = {
  250. .tag_protocol = DSA_TAG_PROTO_EDSA,
  251. .priv_size = sizeof(struct mv88e6xxx_priv_state),
  252. .probe = mv88e6352_probe,
  253. .setup = mv88e6352_setup,
  254. .set_addr = mv88e6xxx_set_addr_indirect,
  255. .phy_read = mv88e6xxx_phy_read_indirect,
  256. .phy_write = mv88e6xxx_phy_write_indirect,
  257. .poll_link = mv88e6xxx_poll_link,
  258. .get_strings = mv88e6xxx_get_strings,
  259. .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
  260. .get_sset_count = mv88e6xxx_get_sset_count,
  261. .adjust_link = mv88e6xxx_adjust_link,
  262. .set_eee = mv88e6xxx_set_eee,
  263. .get_eee = mv88e6xxx_get_eee,
  264. #ifdef CONFIG_NET_DSA_HWMON
  265. .get_temp = mv88e6xxx_get_temp,
  266. .get_temp_limit = mv88e6xxx_get_temp_limit,
  267. .set_temp_limit = mv88e6xxx_set_temp_limit,
  268. .get_temp_alarm = mv88e6xxx_get_temp_alarm,
  269. #endif
  270. .get_eeprom = mv88e6352_get_eeprom,
  271. .set_eeprom = mv88e6352_set_eeprom,
  272. .get_regs_len = mv88e6xxx_get_regs_len,
  273. .get_regs = mv88e6xxx_get_regs,
  274. .port_join_bridge = mv88e6xxx_join_bridge,
  275. .port_leave_bridge = mv88e6xxx_leave_bridge,
  276. .port_stp_update = mv88e6xxx_port_stp_update,
  277. .port_pvid_get = mv88e6xxx_port_pvid_get,
  278. .port_pvid_set = mv88e6xxx_port_pvid_set,
  279. .port_vlan_add = mv88e6xxx_port_vlan_add,
  280. .port_vlan_del = mv88e6xxx_port_vlan_del,
  281. .vlan_getnext = mv88e6xxx_vlan_getnext,
  282. .port_fdb_add = mv88e6xxx_port_fdb_add,
  283. .port_fdb_del = mv88e6xxx_port_fdb_del,
  284. .port_fdb_getnext = mv88e6xxx_port_fdb_getnext,
  285. };
  286. MODULE_ALIAS("platform:mv88e6172");
  287. MODULE_ALIAS("platform:mv88e6176");
  288. MODULE_ALIAS("platform:mv88e6320");
  289. MODULE_ALIAS("platform:mv88e6321");
  290. MODULE_ALIAS("platform:mv88e6352");