mv88e6352.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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, 0x14,
  106. 0xc000 | (addr & 0xff));
  107. if (ret < 0)
  108. goto error;
  109. ret = mv88e6xxx_eeprom_busy_wait(ds);
  110. if (ret < 0)
  111. goto error;
  112. ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x15);
  113. error:
  114. mutex_unlock(&ps->eeprom_mutex);
  115. return ret;
  116. }
  117. static int mv88e6352_get_eeprom(struct dsa_switch *ds,
  118. struct ethtool_eeprom *eeprom, u8 *data)
  119. {
  120. int offset;
  121. int len;
  122. int ret;
  123. offset = eeprom->offset;
  124. len = eeprom->len;
  125. eeprom->len = 0;
  126. eeprom->magic = 0xc3ec4951;
  127. ret = mv88e6xxx_eeprom_load_wait(ds);
  128. if (ret < 0)
  129. return ret;
  130. if (offset & 1) {
  131. int word;
  132. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  133. if (word < 0)
  134. return word;
  135. *data++ = (word >> 8) & 0xff;
  136. offset++;
  137. len--;
  138. eeprom->len++;
  139. }
  140. while (len >= 2) {
  141. int word;
  142. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  143. if (word < 0)
  144. return word;
  145. *data++ = word & 0xff;
  146. *data++ = (word >> 8) & 0xff;
  147. offset += 2;
  148. len -= 2;
  149. eeprom->len += 2;
  150. }
  151. if (len) {
  152. int word;
  153. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  154. if (word < 0)
  155. return word;
  156. *data++ = word & 0xff;
  157. offset++;
  158. len--;
  159. eeprom->len++;
  160. }
  161. return 0;
  162. }
  163. static int mv88e6352_eeprom_is_readonly(struct dsa_switch *ds)
  164. {
  165. int ret;
  166. ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x14);
  167. if (ret < 0)
  168. return ret;
  169. if (!(ret & 0x0400))
  170. return -EROFS;
  171. return 0;
  172. }
  173. static int mv88e6352_write_eeprom_word(struct dsa_switch *ds, int addr,
  174. u16 data)
  175. {
  176. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  177. int ret;
  178. mutex_lock(&ps->eeprom_mutex);
  179. ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x15, data);
  180. if (ret < 0)
  181. goto error;
  182. ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14,
  183. 0xb000 | (addr & 0xff));
  184. if (ret < 0)
  185. goto error;
  186. ret = mv88e6xxx_eeprom_busy_wait(ds);
  187. error:
  188. mutex_unlock(&ps->eeprom_mutex);
  189. return ret;
  190. }
  191. static int mv88e6352_set_eeprom(struct dsa_switch *ds,
  192. struct ethtool_eeprom *eeprom, u8 *data)
  193. {
  194. int offset;
  195. int ret;
  196. int len;
  197. if (eeprom->magic != 0xc3ec4951)
  198. return -EINVAL;
  199. ret = mv88e6352_eeprom_is_readonly(ds);
  200. if (ret)
  201. return ret;
  202. offset = eeprom->offset;
  203. len = eeprom->len;
  204. eeprom->len = 0;
  205. ret = mv88e6xxx_eeprom_load_wait(ds);
  206. if (ret < 0)
  207. return ret;
  208. if (offset & 1) {
  209. int word;
  210. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  211. if (word < 0)
  212. return word;
  213. word = (*data++ << 8) | (word & 0xff);
  214. ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
  215. if (ret < 0)
  216. return ret;
  217. offset++;
  218. len--;
  219. eeprom->len++;
  220. }
  221. while (len >= 2) {
  222. int word;
  223. word = *data++;
  224. word |= *data++ << 8;
  225. ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
  226. if (ret < 0)
  227. return ret;
  228. offset += 2;
  229. len -= 2;
  230. eeprom->len += 2;
  231. }
  232. if (len) {
  233. int word;
  234. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  235. if (word < 0)
  236. return word;
  237. word = (word & 0xff00) | *data++;
  238. ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
  239. if (ret < 0)
  240. return ret;
  241. offset++;
  242. len--;
  243. eeprom->len++;
  244. }
  245. return 0;
  246. }
  247. struct dsa_switch_driver mv88e6352_switch_driver = {
  248. .tag_protocol = DSA_TAG_PROTO_EDSA,
  249. .priv_size = sizeof(struct mv88e6xxx_priv_state),
  250. .probe = mv88e6352_probe,
  251. .setup = mv88e6352_setup,
  252. .set_addr = mv88e6xxx_set_addr_indirect,
  253. .phy_read = mv88e6xxx_phy_read_indirect,
  254. .phy_write = mv88e6xxx_phy_write_indirect,
  255. .poll_link = mv88e6xxx_poll_link,
  256. .get_strings = mv88e6xxx_get_strings,
  257. .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
  258. .get_sset_count = mv88e6xxx_get_sset_count,
  259. .set_eee = mv88e6xxx_set_eee,
  260. .get_eee = mv88e6xxx_get_eee,
  261. #ifdef CONFIG_NET_DSA_HWMON
  262. .get_temp = mv88e6xxx_get_temp,
  263. .get_temp_limit = mv88e6xxx_get_temp_limit,
  264. .set_temp_limit = mv88e6xxx_set_temp_limit,
  265. .get_temp_alarm = mv88e6xxx_get_temp_alarm,
  266. #endif
  267. .get_eeprom = mv88e6352_get_eeprom,
  268. .set_eeprom = mv88e6352_set_eeprom,
  269. .get_regs_len = mv88e6xxx_get_regs_len,
  270. .get_regs = mv88e6xxx_get_regs,
  271. .port_join_bridge = mv88e6xxx_join_bridge,
  272. .port_leave_bridge = mv88e6xxx_leave_bridge,
  273. .port_stp_update = mv88e6xxx_port_stp_update,
  274. .fdb_add = mv88e6xxx_port_fdb_add,
  275. .fdb_del = mv88e6xxx_port_fdb_del,
  276. .fdb_getnext = mv88e6xxx_port_fdb_getnext,
  277. };
  278. MODULE_ALIAS("platform:mv88e6172");
  279. MODULE_ALIAS("platform:mv88e6176");
  280. MODULE_ALIAS("platform:mv88e6320");
  281. MODULE_ALIAS("platform:mv88e6321");
  282. MODULE_ALIAS("platform:mv88e6352");