mv88e6352.c 7.6 KB

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