mv88e6352.c 7.7 KB

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