mv88e6352.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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_6352_A0)
  36. return "Marvell 88E6352 (A0)";
  37. if (ret == PORT_SWITCH_ID_6352_A1)
  38. return "Marvell 88E6352 (A1)";
  39. if ((ret & 0xfff0) == PORT_SWITCH_ID_6352)
  40. return "Marvell 88E6352";
  41. }
  42. return NULL;
  43. }
  44. static int mv88e6352_setup_global(struct dsa_switch *ds)
  45. {
  46. u32 upstream_port = dsa_upstream_port(ds);
  47. int ret;
  48. u32 reg;
  49. ret = mv88e6xxx_setup_global(ds);
  50. if (ret)
  51. return ret;
  52. /* Discard packets with excessive collisions,
  53. * mask all interrupt sources, enable PPU (bit 14, undocumented).
  54. */
  55. REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL,
  56. GLOBAL_CONTROL_PPU_ENABLE | GLOBAL_CONTROL_DISCARD_EXCESS);
  57. /* Configure the upstream port, and configure the upstream
  58. * port as the port to which ingress and egress monitor frames
  59. * are to be sent.
  60. */
  61. reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
  62. upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
  63. upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
  64. REG_WRITE(REG_GLOBAL, GLOBAL_MONITOR_CONTROL, reg);
  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. return 0;
  70. }
  71. #ifdef CONFIG_NET_DSA_HWMON
  72. static int mv88e6352_get_temp(struct dsa_switch *ds, int *temp)
  73. {
  74. int ret;
  75. *temp = 0;
  76. ret = mv88e6xxx_phy_page_read(ds, 0, 6, 27);
  77. if (ret < 0)
  78. return ret;
  79. *temp = (ret & 0xff) - 25;
  80. return 0;
  81. }
  82. static int mv88e6352_get_temp_limit(struct dsa_switch *ds, int *temp)
  83. {
  84. int ret;
  85. *temp = 0;
  86. ret = mv88e6xxx_phy_page_read(ds, 0, 6, 26);
  87. if (ret < 0)
  88. return ret;
  89. *temp = (((ret >> 8) & 0x1f) * 5) - 25;
  90. return 0;
  91. }
  92. static int mv88e6352_set_temp_limit(struct dsa_switch *ds, int temp)
  93. {
  94. int ret;
  95. ret = mv88e6xxx_phy_page_read(ds, 0, 6, 26);
  96. if (ret < 0)
  97. return ret;
  98. temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
  99. return mv88e6xxx_phy_page_write(ds, 0, 6, 26,
  100. (ret & 0xe0ff) | (temp << 8));
  101. }
  102. static int mv88e6352_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
  103. {
  104. int ret;
  105. *alarm = false;
  106. ret = mv88e6xxx_phy_page_read(ds, 0, 6, 26);
  107. if (ret < 0)
  108. return ret;
  109. *alarm = !!(ret & 0x40);
  110. return 0;
  111. }
  112. #endif /* CONFIG_NET_DSA_HWMON */
  113. static int mv88e6352_setup(struct dsa_switch *ds)
  114. {
  115. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  116. int ret;
  117. ret = mv88e6xxx_setup_common(ds);
  118. if (ret < 0)
  119. return ret;
  120. ps->num_ports = 7;
  121. mutex_init(&ps->eeprom_mutex);
  122. ret = mv88e6xxx_switch_reset(ds, true);
  123. if (ret < 0)
  124. return ret;
  125. ret = mv88e6352_setup_global(ds);
  126. if (ret < 0)
  127. return ret;
  128. return mv88e6xxx_setup_ports(ds);
  129. }
  130. static int mv88e6352_read_eeprom_word(struct dsa_switch *ds, int addr)
  131. {
  132. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  133. int ret;
  134. mutex_lock(&ps->eeprom_mutex);
  135. ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14,
  136. 0xc000 | (addr & 0xff));
  137. if (ret < 0)
  138. goto error;
  139. ret = mv88e6xxx_eeprom_busy_wait(ds);
  140. if (ret < 0)
  141. goto error;
  142. ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x15);
  143. error:
  144. mutex_unlock(&ps->eeprom_mutex);
  145. return ret;
  146. }
  147. static int mv88e6352_get_eeprom(struct dsa_switch *ds,
  148. struct ethtool_eeprom *eeprom, u8 *data)
  149. {
  150. int offset;
  151. int len;
  152. int ret;
  153. offset = eeprom->offset;
  154. len = eeprom->len;
  155. eeprom->len = 0;
  156. eeprom->magic = 0xc3ec4951;
  157. ret = mv88e6xxx_eeprom_load_wait(ds);
  158. if (ret < 0)
  159. return ret;
  160. if (offset & 1) {
  161. int word;
  162. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  163. if (word < 0)
  164. return word;
  165. *data++ = (word >> 8) & 0xff;
  166. offset++;
  167. len--;
  168. eeprom->len++;
  169. }
  170. while (len >= 2) {
  171. int word;
  172. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  173. if (word < 0)
  174. return word;
  175. *data++ = word & 0xff;
  176. *data++ = (word >> 8) & 0xff;
  177. offset += 2;
  178. len -= 2;
  179. eeprom->len += 2;
  180. }
  181. if (len) {
  182. int word;
  183. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  184. if (word < 0)
  185. return word;
  186. *data++ = word & 0xff;
  187. offset++;
  188. len--;
  189. eeprom->len++;
  190. }
  191. return 0;
  192. }
  193. static int mv88e6352_eeprom_is_readonly(struct dsa_switch *ds)
  194. {
  195. int ret;
  196. ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x14);
  197. if (ret < 0)
  198. return ret;
  199. if (!(ret & 0x0400))
  200. return -EROFS;
  201. return 0;
  202. }
  203. static int mv88e6352_write_eeprom_word(struct dsa_switch *ds, int addr,
  204. u16 data)
  205. {
  206. struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
  207. int ret;
  208. mutex_lock(&ps->eeprom_mutex);
  209. ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x15, data);
  210. if (ret < 0)
  211. goto error;
  212. ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14,
  213. 0xb000 | (addr & 0xff));
  214. if (ret < 0)
  215. goto error;
  216. ret = mv88e6xxx_eeprom_busy_wait(ds);
  217. error:
  218. mutex_unlock(&ps->eeprom_mutex);
  219. return ret;
  220. }
  221. static int mv88e6352_set_eeprom(struct dsa_switch *ds,
  222. struct ethtool_eeprom *eeprom, u8 *data)
  223. {
  224. int offset;
  225. int ret;
  226. int len;
  227. if (eeprom->magic != 0xc3ec4951)
  228. return -EINVAL;
  229. ret = mv88e6352_eeprom_is_readonly(ds);
  230. if (ret)
  231. return ret;
  232. offset = eeprom->offset;
  233. len = eeprom->len;
  234. eeprom->len = 0;
  235. ret = mv88e6xxx_eeprom_load_wait(ds);
  236. if (ret < 0)
  237. return ret;
  238. if (offset & 1) {
  239. int word;
  240. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  241. if (word < 0)
  242. return word;
  243. word = (*data++ << 8) | (word & 0xff);
  244. ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
  245. if (ret < 0)
  246. return ret;
  247. offset++;
  248. len--;
  249. eeprom->len++;
  250. }
  251. while (len >= 2) {
  252. int word;
  253. word = *data++;
  254. word |= *data++ << 8;
  255. ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
  256. if (ret < 0)
  257. return ret;
  258. offset += 2;
  259. len -= 2;
  260. eeprom->len += 2;
  261. }
  262. if (len) {
  263. int word;
  264. word = mv88e6352_read_eeprom_word(ds, offset >> 1);
  265. if (word < 0)
  266. return word;
  267. word = (word & 0xff00) | *data++;
  268. ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
  269. if (ret < 0)
  270. return ret;
  271. offset++;
  272. len--;
  273. eeprom->len++;
  274. }
  275. return 0;
  276. }
  277. struct dsa_switch_driver mv88e6352_switch_driver = {
  278. .tag_protocol = DSA_TAG_PROTO_EDSA,
  279. .priv_size = sizeof(struct mv88e6xxx_priv_state),
  280. .probe = mv88e6352_probe,
  281. .setup = mv88e6352_setup,
  282. .set_addr = mv88e6xxx_set_addr_indirect,
  283. .phy_read = mv88e6xxx_phy_read_indirect,
  284. .phy_write = mv88e6xxx_phy_write_indirect,
  285. .poll_link = mv88e6xxx_poll_link,
  286. .get_strings = mv88e6xxx_get_strings,
  287. .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
  288. .get_sset_count = mv88e6xxx_get_sset_count,
  289. .set_eee = mv88e6xxx_set_eee,
  290. .get_eee = mv88e6xxx_get_eee,
  291. #ifdef CONFIG_NET_DSA_HWMON
  292. .get_temp = mv88e6352_get_temp,
  293. .get_temp_limit = mv88e6352_get_temp_limit,
  294. .set_temp_limit = mv88e6352_set_temp_limit,
  295. .get_temp_alarm = mv88e6352_get_temp_alarm,
  296. #endif
  297. .get_eeprom = mv88e6352_get_eeprom,
  298. .set_eeprom = mv88e6352_set_eeprom,
  299. .get_regs_len = mv88e6xxx_get_regs_len,
  300. .get_regs = mv88e6xxx_get_regs,
  301. .port_join_bridge = mv88e6xxx_join_bridge,
  302. .port_leave_bridge = mv88e6xxx_leave_bridge,
  303. .port_stp_update = mv88e6xxx_port_stp_update,
  304. .fdb_add = mv88e6xxx_port_fdb_add,
  305. .fdb_del = mv88e6xxx_port_fdb_del,
  306. .fdb_getnext = mv88e6xxx_port_fdb_getnext,
  307. };
  308. MODULE_ALIAS("platform:mv88e6352");
  309. MODULE_ALIAS("platform:mv88e6172");