gw16083.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /*
  2. * drivers/net/phy/gw16083.c
  3. *
  4. * Driver for GW16083 Ventana Ethernet Expansion Mezzanine
  5. *
  6. * Author: Tim Harvey
  7. *
  8. * Copyright (c) 2014 Tim Harvey <tharvey@gateworks.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. */
  16. /*
  17. * The GW16083 interfaces with a Ventana baseboard via the PCIe bus, an i2c
  18. * bus (i2c2), and a couple of GPIO's. On the PCIe bus is an i210 GigE with
  19. * its MAC connected to Port4 of a Marvell MV88E6176 7-port GigE switch via
  20. * MDIO and RGMII. Ports 0-3 are standard copper RJ45 but Ports 5 and 6
  21. * connect to Marvell MV88E1111 dual-mode Copper/Fiber PHY's over SGMII and
  22. * MDIO. The PHY's have both an RG45 for copper and an SFP module.
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/string.h>
  26. #include <linux/errno.h>
  27. #include <linux/unistd.h>
  28. #include <linux/i2c.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/init.h>
  31. #include <linux/delay.h>
  32. #include <linux/device.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/etherdevice.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/mm.h>
  38. #include <linux/module.h>
  39. #include <linux/mii.h>
  40. #include <linux/ethtool.h>
  41. #include <linux/phy.h>
  42. #include <linux/marvell_phy.h>
  43. #include <linux/of_platform.h>
  44. #include <linux/platform_device.h>
  45. #include <linux/io.h>
  46. #include <asm/irq.h>
  47. #include <net/dsa.h>
  48. #include <linux/uaccess.h>
  49. #include "gw16083.h"
  50. #undef FAIL_ON_CHECKSUM_ERR /* fail to configure SFP if checksum bad */
  51. #define PORT_POWER_CONTROL /* ports can be enabled/disabled via sysfs */
  52. #define PORT_MODE_CONTROL /* ports 5/6 can have SFP/RJ45 mode forced */
  53. #define RGMII_DELAY_ON_PHY /* implement Port5/6 tx/rx delay on PHY vs sw */
  54. MODULE_DESCRIPTION("GW16083 driver");
  55. MODULE_AUTHOR("Tim Harvey");
  56. MODULE_LICENSE("GPL");
  57. struct mv88e1111_port_state {
  58. int port;
  59. bool present;
  60. bool serdes;
  61. bool sfp_signal;
  62. bool sfp_present;
  63. bool sfp_compat;
  64. bool sfp_enabled;
  65. char sfp_id[64];
  66. };
  67. struct mv88e1111_priv {
  68. struct phy_device *phydev;
  69. struct i2c_client *client;
  70. struct mv88e1111_port_state port5;
  71. struct mv88e1111_port_state port6;
  72. struct kobject *sysfs_kobj;
  73. struct delayed_work work;
  74. struct workqueue_struct *workq;
  75. };
  76. enum {
  77. mode_copper = 0,
  78. mode_serdes = 1,
  79. };
  80. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
  81. static struct dsa_chip_data switch_chip_data = {
  82. .port_names = {
  83. "lan4",
  84. "lan3",
  85. "lan2",
  86. "lan1",
  87. "cpu",
  88. "lan5",
  89. "lan6",
  90. },
  91. };
  92. static struct dsa_platform_data switch_plat_data = {
  93. .nr_chips = 1,
  94. .chip = &switch_chip_data,
  95. };
  96. static struct platform_device switch_device = {
  97. .name = "dsa",
  98. .id = 0,
  99. .num_resources = 0,
  100. .dev.platform_data = &switch_plat_data,
  101. };
  102. #endif
  103. static struct i2c_client *gw16083_client = NULL;
  104. static int gw16083_read_port_sfp(struct i2c_client *client,
  105. struct mv88e1111_port_state *state);
  106. /* read switch port register from port0-6 */
  107. u16 read_switch_port(struct phy_device *pdev, int port, u8 regaddr)
  108. {
  109. return pdev->bus->read(pdev->bus, MV_BASE + port, regaddr);
  110. }
  111. /* write switch port register to port0-6 */
  112. int write_switch_port(struct phy_device *pdev, int port, u8 regaddr, u16 val)
  113. {
  114. return pdev->bus->write(pdev->bus, MV_BASE + port, regaddr, val);
  115. }
  116. /*
  117. * read_switch_port_phy - write a register for a specific port on 88E6176
  118. * The 88E6176 PHY registers must be accessed thorugh the Global2 address
  119. * using the SMI_PHY_COMMAND_REG and SMI_PHY_DATA_REG.
  120. */
  121. int read_switch_port_phy(struct phy_device *pdev, int port, u8 regaddr)
  122. {
  123. struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
  124. u16 reg;
  125. int i;
  126. dev_dbg(&priv->client->dev, "read_phy: port%d reg=0x%02x\n", port,
  127. regaddr);
  128. reg = SMIBUSY | SMIMODE22 | SMIOP_READ;
  129. reg |= port << DEVADDR;
  130. reg |= regaddr << REGADDR;
  131. pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SMI_PHY_COMMAND, reg);
  132. for (i = 0; i < 10; i++) {
  133. reg = pdev->bus->read(pdev->bus, MV_GLOBAL2,
  134. MV_SMI_PHY_COMMAND);
  135. if (!(reg & (1<<15)))
  136. break;
  137. mdelay(1);
  138. }
  139. /* timeout */
  140. if (i == 10)
  141. return 0xffff;
  142. reg = pdev->bus->read(pdev->bus, MV_GLOBAL2, MV_SMI_PHY_DATA);
  143. return reg;
  144. }
  145. /*
  146. * write_switch_port_phy - write a register for a specific port on 88E6176
  147. * The 88E6176 PHY registers must be accessed thorugh the Global2 address
  148. * using the SMI_PHY_COMMAND_REG and SMI_PHY_DATA_REG.
  149. */
  150. int write_switch_port_phy(struct phy_device *pdev, int port, u8 addr, u16 reg)
  151. {
  152. struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
  153. int i;
  154. dev_dbg(&priv->client->dev, "write_phy: port%d reg=0x%02x val=0x%04x\n",
  155. port, addr, reg);
  156. pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SMI_PHY_DATA, reg);
  157. reg = SMIBUSY | SMIMODE22 | SMIOP_WRITE;
  158. reg |= port << DEVADDR;
  159. reg |= addr << REGADDR;
  160. pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SMI_PHY_COMMAND, reg);
  161. for (i = 0; i < 10; i++) {
  162. reg = pdev->bus->read(pdev->bus, MV_GLOBAL2,
  163. MV_SMI_PHY_COMMAND);
  164. if (!(reg & (1<<15)))
  165. break;
  166. mdelay(1);
  167. }
  168. /* timeout */
  169. if (i == 10)
  170. return -ETIMEDOUT;
  171. return 0;
  172. }
  173. /* read a scratch register from switch */
  174. inline u8 read_switch_scratch(struct phy_device *pdev, u8 reg)
  175. {
  176. pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SCRATCH_MISC, (reg << 8));
  177. return pdev->bus->read(pdev->bus, MV_GLOBAL2, MV_SCRATCH_MISC) & 0xff;
  178. }
  179. /* write a scratch register to switch */
  180. inline void write_switch_scratch(struct phy_device *pdev, u8 reg, u8 val)
  181. {
  182. pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SCRATCH_MISC,
  183. (1 << 15) | (reg << 8) | val);
  184. }
  185. /* enable or disable an SFP's TXEN signal */
  186. static int enable_sfp_txen(struct phy_device *pdev, int port, bool enable)
  187. {
  188. struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
  189. u8 gpio;
  190. int bit;
  191. if (port != 5 && port != 6)
  192. return -EINVAL;
  193. /* GPIO[2:1] output low to enable TXEN */
  194. bit = (port == 5) ? 1 : 2;
  195. gpio = read_switch_scratch(pdev, MV_GPIO_DATA);
  196. if (enable)
  197. gpio |= (1 << bit);
  198. else
  199. gpio &= (1 << bit);
  200. write_switch_scratch(pdev, MV_GPIO_DATA, gpio);
  201. dev_info(&priv->client->dev, "Port%d: SFP TX %s\n", port, enable ?
  202. "enabled" : "disabled");
  203. return 0;
  204. }
  205. /* configure mv88e1111 port for copper or serdes
  206. * For Copper we set auto link/duplex/speed detection
  207. * For SerDes/Fiber we force 1000mbps link up and auto-neg duplex
  208. */
  209. static int config_mv88e1111_port_sfp(struct phy_device *pdev, int port,
  210. bool sfp)
  211. {
  212. struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
  213. u16 reg;
  214. if (port != 5 && port != 6)
  215. return -EINVAL;
  216. dev_dbg(&priv->client->dev, "%s: Port%d %s\n", __func__, port,
  217. sfp ? "SFP" : "copper");
  218. if (sfp) {
  219. enable_sfp_txen(pdev, port, 1);
  220. /* configure MV88E6176 Physical Control Port Register */
  221. dev_info(&priv->client->dev,
  222. "Port%d: SFP: force 1000mbps link up "
  223. "(auto-negotiate duplex)\n",
  224. port);
  225. reg = read_switch_port(pdev, port, MV_PORT_PHYS_CONTROL);
  226. reg &= ~0x3f; /* clear 5-0 */
  227. reg |= (1 << 4) | (1 << 5); /* force link up */
  228. reg |= 2; /* force 1000mbps */
  229. write_switch_port(pdev, port, MV_PORT_PHYS_CONTROL, reg);
  230. reg = read_switch_port(pdev, port, MV_PORT_PHYS_CONTROL);
  231. }
  232. /* copper */
  233. else {
  234. enable_sfp_txen(pdev, port, 0);
  235. /* configure MV88E6176 Physical Control Port Register */
  236. dev_info(&priv->client->dev,
  237. "Port%d: Copper: set auto-neg link/duplex/speed\n",
  238. port);
  239. reg = read_switch_port(pdev, port, MV_PORT_PHYS_CONTROL);
  240. reg &= ~0x3f; /* clear 5-0 */
  241. reg |= 3; /* speed not forced */
  242. write_switch_port(pdev, port, MV_PORT_PHYS_CONTROL, reg);
  243. reg = read_switch_port(pdev, port, MV_PORT_PHYS_CONTROL);
  244. }
  245. dev_dbg(&priv->client->dev, "%s: Port%d %s PORT_PHYS_CONTROL=0x%04x\n",
  246. __func__, port, sfp ? "SFP" : "copper",
  247. read_switch_port(pdev, port, MV_PORT_PHYS_CONTROL));
  248. return 0;
  249. }
  250. #if !IS_ENABLED(CONFIG_NET_DSA_MV88E6352) && defined(PORT_POWER_CONTROL)
  251. static int enable_switch_port(struct phy_device *pdev, int port, bool enable)
  252. {
  253. struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
  254. u16 reg;
  255. /* power up port */
  256. dev_info(&priv->client->dev, "Port%d: %s\n", port,
  257. enable ? "normal operation" : "power down");
  258. reg = read_switch_port_phy(pdev, port, MV_PHY_CONTROL);
  259. if (enable)
  260. reg &= ~(1 << 11); /* Normal Operation */
  261. else
  262. reg |= (1 << 11); /* power down */
  263. write_switch_port_phy(pdev, port, MV_PHY_CONTROL, reg);
  264. reg = read_switch_port_phy(pdev, port, MV_PHY_CONTROL1);
  265. if (enable)
  266. reg &= ~(1 << 2); /* Normal Operation */
  267. else
  268. reg |= (1 << 2); /* power down */
  269. write_switch_port_phy(pdev, port, MV_PHY_CONTROL1, reg);
  270. return 0;
  271. }
  272. #endif
  273. /*
  274. * Sysfs API
  275. */
  276. struct mv88e1111_port_state *get_port_state(struct mv88e1111_priv *priv,
  277. int port)
  278. {
  279. if (port == 5)
  280. return &priv->port5;
  281. if (port == 6)
  282. return &priv->port6;
  283. return NULL;
  284. }
  285. /*
  286. * get MV88E6176 port number for a specific GW16083 port name
  287. * The GW16083 ports as shown on the silkscreen are not mapped according to
  288. * the MV88E6176 ports numbers.
  289. */
  290. static int gw16083_get_port(const char* name)
  291. {
  292. int i;
  293. int map[] = { 3, 2, 1, 0, 5, 6 };
  294. if (strncasecmp(name, "LAN", 3) != 0)
  295. return -1;
  296. i = name[3] - '0';
  297. if (i < 1 || i > 6)
  298. return -1;
  299. return map[i-1];
  300. }
  301. #if !IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
  302. static ssize_t port_show(struct device *dev, struct device_attribute *attr,
  303. char *buf)
  304. {
  305. struct mv88e1111_priv *priv = dev_get_drvdata(dev);
  306. int port = -1;
  307. u16 reg;
  308. if (sscanf(attr->attr.name, "port%d", &port) != 1)
  309. return 0;
  310. if (port < 0 || port > 6)
  311. return 0;
  312. reg = read_switch_port_phy(priv->phydev, port, MV_PHY_CONTROL);
  313. return sprintf(buf, "%s\n", (reg & (1 << 11)) ? "disabled" : "enabled");
  314. }
  315. #if defined(PORT_POWER_CONTROL)
  316. static ssize_t port_store(struct device *dev, struct device_attribute *attr,
  317. const char *buf, size_t count)
  318. {
  319. struct mv88e1111_priv *priv = dev_get_drvdata(dev);
  320. int port = -1;
  321. int val;
  322. port = gw16083_get_port(attr->attr.name);
  323. if (port < 0)
  324. return 0;
  325. if (sscanf(buf, "%d", &val) != 1)
  326. return 0;
  327. enable_switch_port(priv->phydev, port, val ? 1 : 0);
  328. return count;
  329. }
  330. static DEVICE_ATTR(lan1, S_IWUSR | S_IRUGO, port_show, port_store);
  331. static DEVICE_ATTR(lan2, S_IWUSR | S_IRUGO, port_show, port_store);
  332. static DEVICE_ATTR(lan3, S_IWUSR | S_IRUGO, port_show, port_store);
  333. static DEVICE_ATTR(lan4, S_IWUSR | S_IRUGO, port_show, port_store);
  334. static DEVICE_ATTR(lan5, S_IWUSR | S_IRUGO, port_show, port_store);
  335. static DEVICE_ATTR(lan6, S_IWUSR | S_IRUGO, port_show, port_store);
  336. #else
  337. static DEVICE_ATTR(lan1, S_IRUGO, port_show, NULL);
  338. static DEVICE_ATTR(lan2, S_IRUGO, port_show, NULL);
  339. static DEVICE_ATTR(lan3, S_IRUGO, port_show, NULL);
  340. static DEVICE_ATTR(lan4, S_IRUGO, port_show, NULL);
  341. static DEVICE_ATTR(lan5, S_IRUGO, port_show, NULL);
  342. static DEVICE_ATTR(lan6, S_IRUGO, port_show, NULL);
  343. #endif
  344. #endif /* #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352) */
  345. static ssize_t portmode_show(struct device *dev, struct device_attribute *attr,
  346. char *buf)
  347. {
  348. struct mv88e1111_priv *priv = dev_get_drvdata(dev);
  349. struct mv88e1111_port_state *state;
  350. state = get_port_state(priv, gw16083_get_port(attr->attr.name));
  351. if (!state)
  352. return 0;
  353. return sprintf(buf, "%s\n", state->serdes ? "SFP" : "RJ45");
  354. }
  355. #ifdef PORT_MODE_CONTROL
  356. static ssize_t portmode_store(struct device *dev, struct device_attribute *attr,
  357. const char *buf, size_t count)
  358. {
  359. struct mv88e1111_priv *priv = dev_get_drvdata(dev);
  360. struct mv88e1111_port_state *state;
  361. u16 reg;
  362. int port;
  363. port = gw16083_get_port(attr->attr.name);
  364. state = get_port_state(priv, port);
  365. if (!state)
  366. return 0;
  367. reg = read_switch_port_phy(priv->phydev, port, MII_M1111_PHY_EXT_SR);
  368. if (strcasecmp(buf, "auto") == 0) {
  369. reg &= ~(1<<15); /* enable auto-selection */
  370. dev_info(&priv->client->dev, "Port%d: enable auto-selection\n",
  371. port);
  372. } else if (strcasecmp(buf, "RJ45") == 0) {
  373. reg |= (1<<15); /* disable auto-selection */
  374. reg |= 0xb; /* RGMII to Copper */
  375. config_mv88e1111_port_sfp(priv->phydev, port, 0);
  376. dev_info(&priv->client->dev, "Port%d: select RJ45\n", port);
  377. } else if (strcasecmp(buf, "SFP") == 0) {
  378. reg |= (1<<15); /* disable auto-selection */
  379. reg |= 0x3; /* RGMII to Fiber */
  380. config_mv88e1111_port_sfp(priv->phydev, port, 1);
  381. dev_info(&priv->client->dev, "Port%d: select SFP\n", port);
  382. }
  383. write_switch_port_phy(priv->phydev, port, MII_M1111_PHY_EXT_SR, reg);
  384. return count;
  385. }
  386. static DEVICE_ATTR(lan5_mode, S_IWUSR | S_IRUGO, portmode_show, portmode_store);
  387. static DEVICE_ATTR(lan6_mode, S_IWUSR | S_IRUGO, portmode_show, portmode_store);
  388. #else
  389. static DEVICE_ATTR(lan5_mode, S_IRUGO, portmode_show, NULL);
  390. static DEVICE_ATTR(lan6_mode, S_IRUGO, portmode_show, NULL);
  391. #endif
  392. static ssize_t portsfp_show(struct device *dev, struct device_attribute *attr,
  393. char *buf)
  394. {
  395. struct mv88e1111_priv *priv = dev_get_drvdata(dev);
  396. struct mv88e1111_port_state *state;
  397. state = get_port_state(priv, gw16083_get_port(attr->attr.name));
  398. if (!state)
  399. return 0;
  400. if (!state->sfp_present)
  401. return 0;
  402. return sprintf(buf, "%s\n", state->sfp_id);
  403. }
  404. static DEVICE_ATTR(lan5_sfp, S_IRUGO, portsfp_show, NULL);
  405. static DEVICE_ATTR(lan6_sfp, S_IRUGO, portsfp_show, NULL);
  406. /*
  407. * PHY driver
  408. */
  409. /* check MV88E1111 PHY status and MV88E6176 GPIO */
  410. static void
  411. mv88e6176_work(struct work_struct *work)
  412. {
  413. struct mv88e1111_priv *priv =
  414. container_of(work, struct mv88e1111_priv, work.work);
  415. struct phy_device *pdev = priv->phydev;
  416. struct device *dev = &priv->client->dev;
  417. struct mv88e1111_port_state *state;
  418. bool serdes, sfp_present, sfp_signal;
  419. int port;
  420. u16 gpio;
  421. mutex_lock(&pdev->lock);
  422. gpio = read_switch_scratch(pdev, MV_GPIO_DATA);
  423. for (port = 5; port < 7; port++) {
  424. serdes = (read_switch_port_phy(pdev, port, MII_M1111_PHY_EXT_SR)
  425. & (1<<13)) ? 1 : 0;
  426. dev_dbg(dev, "%s: Port%d GPIO:0x%02x SerDes:%d\n",
  427. __func__, port, gpio, serdes);
  428. switch(port) {
  429. case 5:
  430. state = &priv->port5;
  431. sfp_present = !((gpio >> 5) & 1);
  432. sfp_signal = !((gpio >> 6) & 1);
  433. break;
  434. case 6:
  435. state = &priv->port6;
  436. sfp_present = !((gpio >> 3) & 1);
  437. sfp_signal = !((gpio >> 4) & 1);
  438. break;
  439. }
  440. /*
  441. * on sfp_detect read/verify SFP MSA and set sfp_compat
  442. * on sfp_signal issue link down?
  443. * on serdes auto-select
  444. */
  445. if (state->sfp_present != sfp_present) {
  446. state->sfp_present = sfp_present;
  447. dev_info(dev, "Port%d: SFP %s\n",
  448. port, sfp_present ? "inserted" : "removed");
  449. if (state->sfp_present) {
  450. if (gw16083_read_port_sfp(priv->client, state))
  451. state->sfp_compat = false;
  452. else
  453. state->sfp_compat = true;
  454. /* trigger a re-select/enable below */
  455. state->serdes = !serdes;
  456. pdev->state = PHY_RUNNING;
  457. } else {
  458. state->sfp_compat = false;
  459. state->sfp_enabled = false;
  460. pdev->state = PHY_NOLINK;
  461. }
  462. }
  463. if (state->sfp_signal != sfp_signal) {
  464. state->sfp_signal = sfp_signal;
  465. dev_info(dev, "Port%d: SFP signal %s\n",
  466. port, sfp_signal ? "detected" : "lost");
  467. }
  468. if (state->serdes != serdes) {
  469. state->serdes = serdes;
  470. dev_info(dev, "Port%d: %s auto-selected\n",
  471. port, serdes ? "SERDES" : "copper");
  472. /*
  473. * if auto-selection has switched to copper
  474. * disable serdes
  475. */
  476. if (!serdes) {
  477. config_mv88e1111_port_sfp(pdev, port, 0);
  478. state->sfp_enabled = false;
  479. }
  480. }
  481. /* if compatible SFP module and not yet enabled then enable */
  482. if (state->sfp_compat && state->sfp_signal &&
  483. !state->sfp_enabled)
  484. {
  485. if (!config_mv88e1111_port_sfp(pdev, port, 1))
  486. state->sfp_enabled = true;
  487. }
  488. }
  489. mutex_unlock(&pdev->lock);
  490. queue_delayed_work(priv->workq, &priv->work, HZ);
  491. }
  492. static int
  493. mv88e6176_read_status(struct phy_device *pdev)
  494. {
  495. return 0;
  496. }
  497. static int
  498. mv88e6176_config_aneg(struct phy_device *pdev)
  499. {
  500. return 0;
  501. }
  502. static int
  503. mv88e6176_config_init(struct phy_device *pdev)
  504. {
  505. pdev->state = PHY_RUNNING;
  506. return 0;
  507. }
  508. static void
  509. mv88e6176_remove(struct phy_device *pdev)
  510. {
  511. struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
  512. dev_dbg(&priv->client->dev, "%s", __func__);
  513. destroy_workqueue(priv->workq);
  514. #if !IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
  515. device_remove_file(&pdev->dev, &dev_attr_lan1);
  516. device_remove_file(&pdev->dev, &dev_attr_lan2);
  517. device_remove_file(&pdev->dev, &dev_attr_lan3);
  518. device_remove_file(&pdev->dev, &dev_attr_lan4);
  519. device_remove_file(&pdev->dev, &dev_attr_lan5);
  520. device_remove_file(&pdev->dev, &dev_attr_lan6);
  521. #endif
  522. device_remove_file(&pdev->dev, &dev_attr_lan5_sfp);
  523. device_remove_file(&pdev->dev, &dev_attr_lan6_sfp);
  524. device_remove_file(&pdev->dev, &dev_attr_lan5_mode);
  525. device_remove_file(&pdev->dev, &dev_attr_lan6_mode);
  526. sysfs_remove_link(kernel_kobj, "gw16083");
  527. }
  528. /*
  529. * mv88e6176_probe - called any time an MV88E6176 is found on an mdio bus
  530. * Determine if this MV88E6176 is indeed on a GW16083 and if so configure
  531. * the port5/port6 phy and register a background procedure for monitoring
  532. * their states to support SFP vs Copper switching.
  533. *
  534. * Verify this is a GW16083 by ensuring:
  535. * - the phy address matches that of a GW16083
  536. * - the mdio bus is from an i210 device (igb driver)
  537. * - there are MV881111 PHY's hanging off of Port5 and Port6
  538. */
  539. static int
  540. mv88e6176_probe(struct phy_device *pdev)
  541. {
  542. int port;
  543. int ret = 0;
  544. u32 id, reg;
  545. struct mv88e1111_priv *priv;
  546. struct device *dev;
  547. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
  548. struct net_device *netdev = NULL;
  549. #endif
  550. dev_dbg(&pdev->dev, "%s: addr=0x%02x bus=%s:%s gw16083_client=%p\n",
  551. __func__, pdev->addr, pdev->bus->name, pdev->bus->id,
  552. gw16083_client);
  553. /* In single-chip addressing mode the MV88E6176 shows up on 0x10-0x16 */
  554. if (pdev->addr != MV_BASE)
  555. return 0;
  556. /* i2c driver needs to be loaded first */
  557. if (!gw16083_client)
  558. return 0;
  559. dev = &gw16083_client->dev;
  560. /* gw16083 has MV88E1676 hanging off of i210 mdio bus */
  561. if (strcmp(pdev->bus->name, "igb_enet_mii_bus") != 0)
  562. return 0;
  563. /* verify Port5/Port6 have an MV88E1111 PHY hanging off them */
  564. for (port = 5; port < 7; port++) {
  565. id = read_switch_port_phy(pdev, port,
  566. MII_M1111_PHY_IDENT0) << 16;
  567. id |= read_switch_port_phy(pdev, port, MII_M1111_PHY_IDENT1);
  568. if ((id & MII_M1111_PHY_ID_MASK) != MII_M1111_PHY_ID) {
  569. dev_err(dev, "Port%d: No MV88E1111 PHY detected", port);
  570. return 0;
  571. }
  572. }
  573. #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
  574. /*
  575. * Find the netdev this bus is hanging off of and register with DSA:
  576. * The netdev must be an Intel I210 (igb) with Gateworks MAC addr
  577. */
  578. read_lock(&dev_base_lock);
  579. for_each_netdev(&init_net, netdev) {
  580. if (netdev->dev.parent &&
  581. !strcmp(netdev->dev.parent->driver->name, "igb") &&
  582. (netdev->perm_addr[0] == 0x00) &&
  583. (netdev->perm_addr[1] == 0xd0) &&
  584. (netdev->perm_addr[2] == 0x12))
  585. {
  586. switch_plat_data.netdev = &netdev->dev;
  587. switch_plat_data.chip[0].host_dev = &pdev->bus->dev;
  588. break;
  589. }
  590. }
  591. read_unlock(&dev_base_lock);
  592. if (switch_plat_data.netdev) {
  593. platform_device_register(&switch_device);
  594. dev_info(dev, "registered GW16083 DSA switch\n");
  595. } else {
  596. dev_err(dev, "failed to find netdev for DSA switch\n");
  597. }
  598. #endif
  599. /*
  600. * port5/6 config: MV88E1111 PHY
  601. * Register 20: PHY Control Register
  602. * R20_7: add delay to RX_CLK for RXD
  603. * R20_1: add delay to TX_CLK for TXD
  604. * Register 24: LED Control Register
  605. * 0x4111:
  606. * Pulse stretch 170 to 340 ms
  607. * Register 0: Control Register
  608. * R0_15: phy reset
  609. */
  610. dev_info(dev, "Configuring MV88E6176 7-port switch");
  611. for (port = 5; port < 7; port++) {
  612. #ifdef RGMII_DELAY_ON_PHY
  613. /* phy rx/tx delay */
  614. reg = read_switch_port_phy(pdev, port, MII_M1111_PHY_EXT_CR);
  615. reg |= (1<<1) | (1<<7);
  616. write_switch_port_phy(pdev, port, MII_M1111_PHY_EXT_CR, reg);
  617. #else
  618. write_switch_port(pdev, port, MV_PORT_PHYS_CONTROL, 0xC003);
  619. #endif
  620. /* led config */
  621. write_switch_port_phy(pdev, port, MII_M1111_PHY_LED_CONTROL,
  622. MII_M1111_PHY_LED_PULSE_STR);
  623. /* reset phy */
  624. reg = read_switch_port_phy(pdev, port, MII_M1111_PHY_CONTROL);
  625. reg |= MII_M1111_PHY_CONTROL_RESET;
  626. write_switch_port_phy(pdev, port, MII_M1111_PHY_CONTROL, reg);
  627. dev_info(dev, "Port%d: MV88E111 PHY configured\n", port);
  628. }
  629. /*
  630. * GPIO Configuration:
  631. * GPIO1: FIB5_TXEN# (output)
  632. * GPIO2: FIB6_TXEN# (output)
  633. * GPIO3: FIB6_PRES# (input)
  634. * GPIO4: FIB6_LOS (input)
  635. * GPIO5: FIB5_PRES# (input)
  636. * GPIO6: FIB5_LOS (input)
  637. */
  638. write_switch_scratch(pdev, MV_GPIO_DATA, 0x06); /* GPIO[2:1] out hi */
  639. write_switch_scratch(pdev, MV_GPIO_DIR, 0x78); /* GPIO[6:3] inp */
  640. pdev->irq = PHY_POLL;
  641. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  642. if (!priv)
  643. return -ENOMEM;
  644. memset(priv, 0, sizeof(*priv));
  645. priv->phydev = pdev;
  646. priv->client = gw16083_client;
  647. priv->port5.port = 5;
  648. priv->port6.port = 6;
  649. dev_set_drvdata(&pdev->dev, priv);
  650. /* register sysfs API */
  651. #if !IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
  652. ret |= device_create_file(&pdev->dev, &dev_attr_lan1);
  653. ret |= device_create_file(&pdev->dev, &dev_attr_lan2);
  654. ret |= device_create_file(&pdev->dev, &dev_attr_lan3);
  655. ret |= device_create_file(&pdev->dev, &dev_attr_lan4);
  656. ret |= device_create_file(&pdev->dev, &dev_attr_lan5);
  657. ret |= device_create_file(&pdev->dev, &dev_attr_lan6);
  658. #endif
  659. ret |= device_create_file(&pdev->dev, &dev_attr_lan5_sfp);
  660. ret |= device_create_file(&pdev->dev, &dev_attr_lan6_sfp);
  661. ret |= device_create_file(&pdev->dev, &dev_attr_lan5_mode);
  662. ret |= device_create_file(&pdev->dev, &dev_attr_lan6_mode);
  663. if (unlikely(ret))
  664. dev_err(&pdev->dev, "Failed creating attrs\n");
  665. /* Add a nice symlink to the real device */
  666. ret = sysfs_create_link(kernel_kobj, &pdev->dev.kobj, "gw16083");
  667. INIT_DELAYED_WORK(&priv->work, mv88e6176_work);
  668. priv->workq = create_singlethread_workqueue("gw16083");
  669. if (!priv->workq)
  670. return -ENODEV;
  671. queue_delayed_work(priv->workq, &priv->work, 0);
  672. dev_dbg(dev, "initial state: GPIO=0x%02x "
  673. "Port5_serdes=%d Port6_serdes=%d\n",
  674. read_switch_scratch(pdev, MV_GPIO_DATA),
  675. (read_switch_port_phy(pdev, 5, MII_M1111_PHY_EXT_SR)
  676. & (1<<13) ? 1:0),
  677. (read_switch_port_phy(pdev, 6, MII_M1111_PHY_EXT_SR)
  678. & (1<<13) ? 1:0));
  679. return ret;
  680. }
  681. static struct phy_driver mv88e6176_phy_driver = {
  682. .name = "gw16083",
  683. .phy_id = MV_IDENT_VALUE,
  684. .phy_id_mask = MV_IDENT_MASK,
  685. .features = PHY_BASIC_FEATURES,
  686. .probe = &mv88e6176_probe,
  687. .remove = &mv88e6176_remove,
  688. .config_init = &mv88e6176_config_init,
  689. .config_aneg = &mv88e6176_config_aneg,
  690. .read_status = &mv88e6176_read_status,
  691. .driver = { .owner = THIS_MODULE },
  692. };
  693. /*
  694. * I2C driver
  695. */
  696. /* See SFF-8472 */
  697. struct sfp_msa {
  698. /* Basic ID fields */
  699. u8 identifier;
  700. u8 ext_identifier;
  701. u8 connector;
  702. u8 transceiver[8];
  703. u8 encoding;
  704. u8 br_nominal;
  705. u8 rate_identifier;
  706. u8 length_smf_km;
  707. u8 length_smf;
  708. u8 length_om2;
  709. u8 length_om1;
  710. u8 length_om4;
  711. u8 length_om3;
  712. u8 vendor_name[16];
  713. u8 transceiver2;
  714. u8 vendor_oui[3];
  715. u8 vendor_pn[16];
  716. u8 vendor_rev[4];
  717. u8 wavelength[2];
  718. u8 resv1;
  719. u8 cc_base;
  720. /* extended id fields */
  721. u8 options[2];
  722. u8 br_max;
  723. u8 br_min;
  724. u8 vendor_sn[16];
  725. u8 date_code[8];
  726. u8 diags_type;
  727. u8 enhanced_options;
  728. u8 sff8472_compliance;
  729. u8 cc_ext;
  730. /* Vendor specific ID fields */
  731. u8 vendor_data[32];
  732. u8 sff8079[128];
  733. };
  734. enum identifier {
  735. UNKNOWN,
  736. GBIC,
  737. SFF,
  738. SFP,
  739. XBI,
  740. XENPACK,
  741. XFP,
  742. XFF,
  743. XFP_E,
  744. XPAK,
  745. X2,
  746. DWDM_SFP,
  747. QSFP,
  748. MAX_ID,
  749. };
  750. const char* id_names[] = {
  751. "UNKONWN",
  752. "GBIC",
  753. "SFF",
  754. "SFP",
  755. NULL,
  756. };
  757. /* Flags for SFP modules compatible with ETH up to 1Gb */
  758. struct sfp_flags {
  759. u8 e1000_base_sx:1;
  760. u8 e1000_base_lx:1;
  761. u8 e1000_base_cx:1;
  762. u8 e1000_base_t:1;
  763. u8 e100_base_lx:1;
  764. u8 e100_base_fx:1;
  765. u8 e10_base_bx10:1;
  766. u8 e10_base_px:1;
  767. };
  768. #define STRING_APPEND(str, src) \
  769. strncat(str, src, sizeof(src)); \
  770. for (i = 1; i < sizeof(str); i++) \
  771. if (str[i-1] == ' ' && str[i] == ' ') \
  772. str[i] = 0;
  773. static int gw16083_read_port_sfp(struct i2c_client *client,
  774. struct mv88e1111_port_state *state)
  775. {
  776. int ret = 0;
  777. u8 data[256];
  778. struct sfp_flags *eth_flags;
  779. u8 crc;
  780. int i;
  781. u8 *str;
  782. struct sfp_msa *sfp_msa = (struct sfp_msa *)data;
  783. int port = state->port;
  784. union i2c_smbus_data d;
  785. dev_dbg(&client->dev, "%s Port%d\n", __func__, port);
  786. if (!i2c_check_functionality(client->adapter,
  787. I2C_FUNC_SMBUS_READ_I2C_BLOCK))
  788. return -ENODEV;
  789. d.byte = (port == 5) ? 1 : 2;
  790. if (i2c_smbus_xfer(client->adapter, GW16083_I2C_ADDR_PCA9543,
  791. client->flags, I2C_SMBUS_WRITE, 0,
  792. I2C_SMBUS_BYTE_DATA, &d) < 0)
  793. {
  794. dev_err(&client->dev,
  795. "Port%d: failed writing PCA9543 register\n", port);
  796. return ret;
  797. }
  798. /* read all 256 bytes of SFP EEPROM */
  799. for (i = 0; i < sizeof(data); i += I2C_SMBUS_BLOCK_MAX) {
  800. d.block[0] = I2C_SMBUS_BLOCK_MAX;
  801. if (i2c_smbus_xfer(client->adapter, GW16083_I2C_ADDR_SFP1,
  802. client->flags, I2C_SMBUS_READ, i,
  803. I2C_SMBUS_I2C_BLOCK_DATA, &d) < 0)
  804. {
  805. dev_err(&client->dev,
  806. "Port%d: failed reading SFP data\n", port);
  807. return ret;
  808. }
  809. memcpy(data + i, d.block + 1, I2C_SMBUS_BLOCK_MAX);
  810. }
  811. /* Validate checksums */
  812. for (crc = 0, i = 0; i < 63; i++)
  813. crc += data[i];
  814. if (crc != sfp_msa->cc_base) {
  815. dev_err(&client->dev, "Port%d: "
  816. "Checksum failure for Base ID fields: 0x%02x\n", port,
  817. crc);
  818. #ifdef FAIL_ON_CHECKSUM_ERR
  819. return -EINVAL;
  820. #endif
  821. }
  822. for (crc = 0, i = 64; i < 95; i++)
  823. crc += data[i];
  824. if (crc != sfp_msa->cc_ext) {
  825. dev_err(&client->dev, "Port%d: "
  826. "Checksum failure for Extended ID fields: 0x%02x\n",
  827. port, crc);
  828. #ifdef FAIL_ON_CHECKSUM_ERR
  829. return -EINVAL;
  830. #endif
  831. }
  832. state->sfp_id[0] = 0;
  833. for (i = 0; id_names[i]; i++) {
  834. if (sfp_msa->identifier == i) {
  835. sprintf(state->sfp_id, "%s: ", id_names[i]);
  836. break;
  837. }
  838. }
  839. STRING_APPEND(state->sfp_id, sfp_msa->vendor_oui);
  840. STRING_APPEND(state->sfp_id, sfp_msa->vendor_name);
  841. STRING_APPEND(state->sfp_id, sfp_msa->vendor_pn);
  842. STRING_APPEND(state->sfp_id, sfp_msa->vendor_rev);
  843. STRING_APPEND(state->sfp_id, sfp_msa->vendor_sn);
  844. dev_info(&client->dev, "Port%d: %s\n", port, state->sfp_id);
  845. if ((sfp_msa->identifier != GBIC) &&
  846. (sfp_msa->identifier != SFF) &&
  847. (sfp_msa->identifier != SFP))
  848. {
  849. dev_err(&client->dev, "Port%d: Unknown module identifier: %d\n",
  850. port, sfp_msa->identifier);
  851. return -EINVAL;
  852. }
  853. str = "";
  854. eth_flags = (struct sfp_flags *)(sfp_msa->transceiver + 3);
  855. if (eth_flags->e1000_base_sx) {
  856. str = "1000Base-SX (Fiber)";
  857. } else if (eth_flags->e1000_base_lx) {
  858. str = "1000Base-LX (Fiber)";
  859. } else if (eth_flags->e1000_base_t) {
  860. str = "1000Base-T (Copper)";
  861. } else if (eth_flags->e100_base_fx) {
  862. str = "100Base-FX (Fiber) - not supported";
  863. ret = -EINVAL;
  864. } else {
  865. str = "Unknown/Unsupported media type";
  866. ret = -EINVAL;
  867. }
  868. if (ret)
  869. dev_err(&client->dev, "Port%d: %s (0x%02x)\n", port, str,
  870. sfp_msa->transceiver[3]);
  871. else
  872. dev_info(&client->dev, "Port%d: %s (0x%02x)\n", port, str,
  873. sfp_msa->transceiver[3]);
  874. return ret;
  875. }
  876. static int gw16083_probe(struct i2c_client *client,
  877. const struct i2c_device_id *id)
  878. {
  879. int ret;
  880. dev_info(&client->dev, "GW16083 Ethernet Expansion Mezzanine\n");
  881. if (gw16083_client) {
  882. dev_err(&client->dev, "client already registered\n");
  883. return -EINVAL;
  884. }
  885. gw16083_client = client;
  886. ret = phy_driver_register(&mv88e6176_phy_driver);
  887. if (ret)
  888. dev_err(&client->dev,
  889. "failed to register mv88e6176 phy driver: %d\n", ret);
  890. return ret;
  891. }
  892. static int gw16083_remove(struct i2c_client *client)
  893. {
  894. dev_dbg(&client->dev, "%s\n", __func__);
  895. phy_driver_unregister(&mv88e6176_phy_driver);
  896. gw16083_client = NULL;
  897. return 0;
  898. }
  899. static const struct of_device_id gw16083_dt_ids[] = {
  900. { .compatible = "gateworks,gw16083", },
  901. { }
  902. };
  903. MODULE_DEVICE_TABLE(of, gw16083_dt_ids);
  904. static const struct i2c_device_id gw16083_id[] = {
  905. { "gw16083", 0 },
  906. { }
  907. };
  908. MODULE_DEVICE_TABLE(i2c, gw16083_id);
  909. static struct i2c_driver gw16083_driver = {
  910. .driver = {
  911. .name = "gw16083",
  912. .of_match_table = gw16083_dt_ids,
  913. },
  914. .probe = gw16083_probe,
  915. .remove = gw16083_remove,
  916. .id_table = gw16083_id,
  917. };
  918. static int __init mv88e6176_init(void)
  919. {
  920. return i2c_add_driver(&gw16083_driver);
  921. }
  922. static void __exit mv88e6176_exit(void)
  923. {
  924. i2c_del_driver(&gw16083_driver);
  925. }
  926. module_init(mv88e6176_init);
  927. module_exit(mv88e6176_exit);