mv88e61xx.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2015
  4. * Elecsys Corporation <www.elecsyscorp.com>
  5. * Kevin Smith <kevin.smith@elecsyscorp.com>
  6. *
  7. * Original driver:
  8. * (C) Copyright 2009
  9. * Marvell Semiconductor <www.marvell.com>
  10. * Prafulla Wadaskar <prafulla@marvell.com>
  11. */
  12. /*
  13. * PHY driver for mv88e61xx ethernet switches.
  14. *
  15. * This driver configures the mv88e61xx for basic use as a PHY. The switch
  16. * supports a VLAN configuration that determines how traffic will be routed
  17. * between the ports. This driver uses a simple configuration that routes
  18. * traffic from each PHY port only to the CPU port, and from the CPU port to
  19. * any PHY port.
  20. *
  21. * The configuration determines which PHY ports to activate using the
  22. * CONFIG_MV88E61XX_PHY_PORTS bitmask. Setting bit 0 will activate port 0, bit
  23. * 1 activates port 1, etc. Do not set the bit for the port the CPU is
  24. * connected to unless it is connected over a PHY interface (not MII).
  25. *
  26. * This driver was written for and tested on the mv88e6176 with an SGMII
  27. * connection. Other configurations should be supported, but some additions or
  28. * changes may be required.
  29. */
  30. #include <common.h>
  31. #include <bitfield.h>
  32. #include <errno.h>
  33. #include <malloc.h>
  34. #include <miiphy.h>
  35. #include <netdev.h>
  36. #define PHY_AUTONEGOTIATE_TIMEOUT 5000
  37. #define PORT_COUNT 11
  38. #define PORT_MASK ((1 << PORT_COUNT) - 1)
  39. /* Device addresses */
  40. #define DEVADDR_PHY(p) (p)
  41. #define DEVADDR_PORT(p) (0x10 + (p))
  42. #define DEVADDR_SERDES 0x0F
  43. #define DEVADDR_GLOBAL_1 0x1B
  44. #define DEVADDR_GLOBAL_2 0x1C
  45. /* SMI indirection registers for multichip addressing mode */
  46. #define SMI_CMD_REG 0x00
  47. #define SMI_DATA_REG 0x01
  48. /* Global registers */
  49. #define GLOBAL1_STATUS 0x00
  50. #define GLOBAL1_CTRL 0x04
  51. #define GLOBAL1_MON_CTRL 0x1A
  52. /* Global 2 registers */
  53. #define GLOBAL2_REG_PHY_CMD 0x18
  54. #define GLOBAL2_REG_PHY_DATA 0x19
  55. /* Port registers */
  56. #define PORT_REG_STATUS 0x00
  57. #define PORT_REG_PHYS_CTRL 0x01
  58. #define PORT_REG_SWITCH_ID 0x03
  59. #define PORT_REG_CTRL 0x04
  60. #define PORT_REG_VLAN_MAP 0x06
  61. #define PORT_REG_VLAN_ID 0x07
  62. /* Phy registers */
  63. #define PHY_REG_CTRL1 0x10
  64. #define PHY_REG_STATUS1 0x11
  65. #define PHY_REG_PAGE 0x16
  66. /* Serdes registers */
  67. #define SERDES_REG_CTRL_1 0x10
  68. /* Phy page numbers */
  69. #define PHY_PAGE_COPPER 0
  70. #define PHY_PAGE_SERDES 1
  71. /* Register fields */
  72. #define GLOBAL1_CTRL_SWRESET BIT(15)
  73. #define GLOBAL1_MON_CTRL_CPUDEST_SHIFT 4
  74. #define GLOBAL1_MON_CTRL_CPUDEST_WIDTH 4
  75. #define PORT_REG_STATUS_LINK BIT(11)
  76. #define PORT_REG_STATUS_DUPLEX BIT(10)
  77. #define PORT_REG_STATUS_SPEED_SHIFT 8
  78. #define PORT_REG_STATUS_SPEED_WIDTH 2
  79. #define PORT_REG_STATUS_SPEED_10 0
  80. #define PORT_REG_STATUS_SPEED_100 1
  81. #define PORT_REG_STATUS_SPEED_1000 2
  82. #define PORT_REG_STATUS_CMODE_MASK 0xF
  83. #define PORT_REG_STATUS_CMODE_100BASE_X 0x8
  84. #define PORT_REG_STATUS_CMODE_1000BASE_X 0x9
  85. #define PORT_REG_STATUS_CMODE_SGMII 0xa
  86. #define PORT_REG_PHYS_CTRL_PCS_AN_EN BIT(10)
  87. #define PORT_REG_PHYS_CTRL_PCS_AN_RST BIT(9)
  88. #define PORT_REG_PHYS_CTRL_FC_VALUE BIT(7)
  89. #define PORT_REG_PHYS_CTRL_FC_FORCE BIT(6)
  90. #define PORT_REG_PHYS_CTRL_LINK_VALUE BIT(5)
  91. #define PORT_REG_PHYS_CTRL_LINK_FORCE BIT(4)
  92. #define PORT_REG_PHYS_CTRL_DUPLEX_VALUE BIT(3)
  93. #define PORT_REG_PHYS_CTRL_DUPLEX_FORCE BIT(2)
  94. #define PORT_REG_PHYS_CTRL_SPD1000 BIT(1)
  95. #define PORT_REG_PHYS_CTRL_SPD_MASK (BIT(1) | BIT(0))
  96. #define PORT_REG_CTRL_PSTATE_SHIFT 0
  97. #define PORT_REG_CTRL_PSTATE_WIDTH 2
  98. #define PORT_REG_VLAN_ID_DEF_VID_SHIFT 0
  99. #define PORT_REG_VLAN_ID_DEF_VID_WIDTH 12
  100. #define PORT_REG_VLAN_MAP_TABLE_SHIFT 0
  101. #define PORT_REG_VLAN_MAP_TABLE_WIDTH 11
  102. #define SERDES_REG_CTRL_1_FORCE_LINK BIT(10)
  103. #define PHY_REG_CTRL1_ENERGY_DET_SHIFT 8
  104. #define PHY_REG_CTRL1_ENERGY_DET_WIDTH 2
  105. /* Field values */
  106. #define PORT_REG_CTRL_PSTATE_DISABLED 0
  107. #define PORT_REG_CTRL_PSTATE_FORWARD 3
  108. #define PHY_REG_CTRL1_ENERGY_DET_OFF 0
  109. #define PHY_REG_CTRL1_ENERGY_DET_SENSE_ONLY 2
  110. #define PHY_REG_CTRL1_ENERGY_DET_SENSE_XMIT 3
  111. /* PHY Status Register */
  112. #define PHY_REG_STATUS1_SPEED 0xc000
  113. #define PHY_REG_STATUS1_GBIT 0x8000
  114. #define PHY_REG_STATUS1_100 0x4000
  115. #define PHY_REG_STATUS1_DUPLEX 0x2000
  116. #define PHY_REG_STATUS1_SPDDONE 0x0800
  117. #define PHY_REG_STATUS1_LINK 0x0400
  118. #define PHY_REG_STATUS1_ENERGY 0x0010
  119. /*
  120. * Macros for building commands for indirect addressing modes. These are valid
  121. * for both the indirect multichip addressing mode and the PHY indirection
  122. * required for the writes to any PHY register.
  123. */
  124. #define SMI_BUSY BIT(15)
  125. #define SMI_CMD_CLAUSE_22 BIT(12)
  126. #define SMI_CMD_CLAUSE_22_OP_READ (2 << 10)
  127. #define SMI_CMD_CLAUSE_22_OP_WRITE (1 << 10)
  128. #define SMI_CMD_READ (SMI_BUSY | SMI_CMD_CLAUSE_22 | \
  129. SMI_CMD_CLAUSE_22_OP_READ)
  130. #define SMI_CMD_WRITE (SMI_BUSY | SMI_CMD_CLAUSE_22 | \
  131. SMI_CMD_CLAUSE_22_OP_WRITE)
  132. #define SMI_CMD_ADDR_SHIFT 5
  133. #define SMI_CMD_ADDR_WIDTH 5
  134. #define SMI_CMD_REG_SHIFT 0
  135. #define SMI_CMD_REG_WIDTH 5
  136. /* Check for required macros */
  137. #ifndef CONFIG_MV88E61XX_PHY_PORTS
  138. #error Define CONFIG_MV88E61XX_PHY_PORTS to indicate which physical ports \
  139. to activate
  140. #endif
  141. #ifndef CONFIG_MV88E61XX_CPU_PORT
  142. #error Define CONFIG_MV88E61XX_CPU_PORT to the port the CPU is attached to
  143. #endif
  144. /*
  145. * These are ports without PHYs that may be wired directly
  146. * to other serdes interfaces
  147. */
  148. #ifndef CONFIG_MV88E61XX_FIXED_PORTS
  149. #define CONFIG_MV88E61XX_FIXED_PORTS 0
  150. #endif
  151. /* ID register values for different switch models */
  152. #define PORT_SWITCH_ID_6096 0x0980
  153. #define PORT_SWITCH_ID_6097 0x0990
  154. #define PORT_SWITCH_ID_6172 0x1720
  155. #define PORT_SWITCH_ID_6176 0x1760
  156. #define PORT_SWITCH_ID_6240 0x2400
  157. #define PORT_SWITCH_ID_6352 0x3520
  158. struct mv88e61xx_phy_priv {
  159. struct mii_dev *mdio_bus;
  160. int smi_addr;
  161. int id;
  162. };
  163. static inline int smi_cmd(int cmd, int addr, int reg)
  164. {
  165. cmd = bitfield_replace(cmd, SMI_CMD_ADDR_SHIFT, SMI_CMD_ADDR_WIDTH,
  166. addr);
  167. cmd = bitfield_replace(cmd, SMI_CMD_REG_SHIFT, SMI_CMD_REG_WIDTH, reg);
  168. return cmd;
  169. }
  170. static inline int smi_cmd_read(int addr, int reg)
  171. {
  172. return smi_cmd(SMI_CMD_READ, addr, reg);
  173. }
  174. static inline int smi_cmd_write(int addr, int reg)
  175. {
  176. return smi_cmd(SMI_CMD_WRITE, addr, reg);
  177. }
  178. __weak int mv88e61xx_hw_reset(struct phy_device *phydev)
  179. {
  180. return 0;
  181. }
  182. /* Wait for the current SMI indirect command to complete */
  183. static int mv88e61xx_smi_wait(struct mii_dev *bus, int smi_addr)
  184. {
  185. int val;
  186. u32 timeout = 100;
  187. do {
  188. val = bus->read(bus, smi_addr, MDIO_DEVAD_NONE, SMI_CMD_REG);
  189. if (val >= 0 && (val & SMI_BUSY) == 0)
  190. return 0;
  191. mdelay(1);
  192. } while (--timeout);
  193. puts("SMI busy timeout\n");
  194. return -ETIMEDOUT;
  195. }
  196. /*
  197. * The mv88e61xx has three types of addresses: the smi bus address, the device
  198. * address, and the register address. The smi bus address distinguishes it on
  199. * the smi bus from other PHYs or switches. The device address determines
  200. * which on-chip register set you are reading/writing (the various PHYs, their
  201. * associated ports, or global configuration registers). The register address
  202. * is the offset of the register you are reading/writing.
  203. *
  204. * When the mv88e61xx is hardware configured to have address zero, it behaves in
  205. * single-chip addressing mode, where it responds to all SMI addresses, using
  206. * the smi address as its device address. This obviously only works when this
  207. * is the only chip on the SMI bus. This allows the driver to access device
  208. * registers without using indirection. When the chip is configured to a
  209. * non-zero address, it only responds to that SMI address and requires indirect
  210. * writes to access the different device addresses.
  211. */
  212. static int mv88e61xx_reg_read(struct phy_device *phydev, int dev, int reg)
  213. {
  214. struct mv88e61xx_phy_priv *priv = phydev->priv;
  215. struct mii_dev *mdio_bus = priv->mdio_bus;
  216. int smi_addr = priv->smi_addr;
  217. int res;
  218. /* In single-chip mode, the device can be addressed directly */
  219. if (smi_addr == 0)
  220. return mdio_bus->read(mdio_bus, dev, MDIO_DEVAD_NONE, reg);
  221. /* Wait for the bus to become free */
  222. res = mv88e61xx_smi_wait(mdio_bus, smi_addr);
  223. if (res < 0)
  224. return res;
  225. /* Issue the read command */
  226. res = mdio_bus->write(mdio_bus, smi_addr, MDIO_DEVAD_NONE, SMI_CMD_REG,
  227. smi_cmd_read(dev, reg));
  228. if (res < 0)
  229. return res;
  230. /* Wait for the read command to complete */
  231. res = mv88e61xx_smi_wait(mdio_bus, smi_addr);
  232. if (res < 0)
  233. return res;
  234. /* Read the data */
  235. res = mdio_bus->read(mdio_bus, smi_addr, MDIO_DEVAD_NONE, SMI_DATA_REG);
  236. if (res < 0)
  237. return res;
  238. return bitfield_extract(res, 0, 16);
  239. }
  240. /* See the comment above mv88e61xx_reg_read */
  241. static int mv88e61xx_reg_write(struct phy_device *phydev, int dev, int reg,
  242. u16 val)
  243. {
  244. struct mv88e61xx_phy_priv *priv = phydev->priv;
  245. struct mii_dev *mdio_bus = priv->mdio_bus;
  246. int smi_addr = priv->smi_addr;
  247. int res;
  248. /* In single-chip mode, the device can be addressed directly */
  249. if (smi_addr == 0) {
  250. return mdio_bus->write(mdio_bus, dev, MDIO_DEVAD_NONE, reg,
  251. val);
  252. }
  253. /* Wait for the bus to become free */
  254. res = mv88e61xx_smi_wait(mdio_bus, smi_addr);
  255. if (res < 0)
  256. return res;
  257. /* Set the data to write */
  258. res = mdio_bus->write(mdio_bus, smi_addr, MDIO_DEVAD_NONE,
  259. SMI_DATA_REG, val);
  260. if (res < 0)
  261. return res;
  262. /* Issue the write command */
  263. res = mdio_bus->write(mdio_bus, smi_addr, MDIO_DEVAD_NONE, SMI_CMD_REG,
  264. smi_cmd_write(dev, reg));
  265. if (res < 0)
  266. return res;
  267. /* Wait for the write command to complete */
  268. res = mv88e61xx_smi_wait(mdio_bus, smi_addr);
  269. if (res < 0)
  270. return res;
  271. return 0;
  272. }
  273. static int mv88e61xx_phy_wait(struct phy_device *phydev)
  274. {
  275. int val;
  276. u32 timeout = 100;
  277. do {
  278. val = mv88e61xx_reg_read(phydev, DEVADDR_GLOBAL_2,
  279. GLOBAL2_REG_PHY_CMD);
  280. if (val >= 0 && (val & SMI_BUSY) == 0)
  281. return 0;
  282. mdelay(1);
  283. } while (--timeout);
  284. return -ETIMEDOUT;
  285. }
  286. static int mv88e61xx_phy_read_indirect(struct mii_dev *smi_wrapper, int dev,
  287. int devad, int reg)
  288. {
  289. struct phy_device *phydev;
  290. int res;
  291. phydev = (struct phy_device *)smi_wrapper->priv;
  292. /* Issue command to read */
  293. res = mv88e61xx_reg_write(phydev, DEVADDR_GLOBAL_2,
  294. GLOBAL2_REG_PHY_CMD,
  295. smi_cmd_read(dev, reg));
  296. /* Wait for data to be read */
  297. res = mv88e61xx_phy_wait(phydev);
  298. if (res < 0)
  299. return res;
  300. /* Read retrieved data */
  301. return mv88e61xx_reg_read(phydev, DEVADDR_GLOBAL_2,
  302. GLOBAL2_REG_PHY_DATA);
  303. }
  304. static int mv88e61xx_phy_write_indirect(struct mii_dev *smi_wrapper, int dev,
  305. int devad, int reg, u16 data)
  306. {
  307. struct phy_device *phydev;
  308. int res;
  309. phydev = (struct phy_device *)smi_wrapper->priv;
  310. /* Set the data to write */
  311. res = mv88e61xx_reg_write(phydev, DEVADDR_GLOBAL_2,
  312. GLOBAL2_REG_PHY_DATA, data);
  313. if (res < 0)
  314. return res;
  315. /* Issue the write command */
  316. res = mv88e61xx_reg_write(phydev, DEVADDR_GLOBAL_2,
  317. GLOBAL2_REG_PHY_CMD,
  318. smi_cmd_write(dev, reg));
  319. if (res < 0)
  320. return res;
  321. /* Wait for command to complete */
  322. return mv88e61xx_phy_wait(phydev);
  323. }
  324. /* Wrapper function to make calls to phy_read_indirect simpler */
  325. static int mv88e61xx_phy_read(struct phy_device *phydev, int phy, int reg)
  326. {
  327. return mv88e61xx_phy_read_indirect(phydev->bus, DEVADDR_PHY(phy),
  328. MDIO_DEVAD_NONE, reg);
  329. }
  330. /* Wrapper function to make calls to phy_read_indirect simpler */
  331. static int mv88e61xx_phy_write(struct phy_device *phydev, int phy,
  332. int reg, u16 val)
  333. {
  334. return mv88e61xx_phy_write_indirect(phydev->bus, DEVADDR_PHY(phy),
  335. MDIO_DEVAD_NONE, reg, val);
  336. }
  337. static int mv88e61xx_port_read(struct phy_device *phydev, u8 port, u8 reg)
  338. {
  339. return mv88e61xx_reg_read(phydev, DEVADDR_PORT(port), reg);
  340. }
  341. static int mv88e61xx_port_write(struct phy_device *phydev, u8 port, u8 reg,
  342. u16 val)
  343. {
  344. return mv88e61xx_reg_write(phydev, DEVADDR_PORT(port), reg, val);
  345. }
  346. static int mv88e61xx_set_page(struct phy_device *phydev, u8 phy, u8 page)
  347. {
  348. return mv88e61xx_phy_write(phydev, phy, PHY_REG_PAGE, page);
  349. }
  350. static int mv88e61xx_get_switch_id(struct phy_device *phydev)
  351. {
  352. int res;
  353. res = mv88e61xx_port_read(phydev, 0, PORT_REG_SWITCH_ID);
  354. if (res < 0)
  355. return res;
  356. return res & 0xfff0;
  357. }
  358. static bool mv88e61xx_6352_family(struct phy_device *phydev)
  359. {
  360. struct mv88e61xx_phy_priv *priv = phydev->priv;
  361. switch (priv->id) {
  362. case PORT_SWITCH_ID_6172:
  363. case PORT_SWITCH_ID_6176:
  364. case PORT_SWITCH_ID_6240:
  365. case PORT_SWITCH_ID_6352:
  366. return true;
  367. }
  368. return false;
  369. }
  370. static int mv88e61xx_get_cmode(struct phy_device *phydev, u8 port)
  371. {
  372. int res;
  373. res = mv88e61xx_port_read(phydev, port, PORT_REG_STATUS);
  374. if (res < 0)
  375. return res;
  376. return res & PORT_REG_STATUS_CMODE_MASK;
  377. }
  378. static int mv88e61xx_parse_status(struct phy_device *phydev)
  379. {
  380. unsigned int speed;
  381. unsigned int mii_reg;
  382. mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, PHY_REG_STATUS1);
  383. if ((mii_reg & PHY_REG_STATUS1_LINK) &&
  384. !(mii_reg & PHY_REG_STATUS1_SPDDONE)) {
  385. int i = 0;
  386. puts("Waiting for PHY realtime link");
  387. while (!(mii_reg & PHY_REG_STATUS1_SPDDONE)) {
  388. /* Timeout reached ? */
  389. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  390. puts(" TIMEOUT !\n");
  391. phydev->link = 0;
  392. break;
  393. }
  394. if ((i++ % 1000) == 0)
  395. putc('.');
  396. udelay(1000);
  397. mii_reg = phy_read(phydev, MDIO_DEVAD_NONE,
  398. PHY_REG_STATUS1);
  399. }
  400. puts(" done\n");
  401. udelay(500000); /* another 500 ms (results in faster booting) */
  402. } else {
  403. if (mii_reg & PHY_REG_STATUS1_LINK)
  404. phydev->link = 1;
  405. else
  406. phydev->link = 0;
  407. }
  408. if (mii_reg & PHY_REG_STATUS1_DUPLEX)
  409. phydev->duplex = DUPLEX_FULL;
  410. else
  411. phydev->duplex = DUPLEX_HALF;
  412. speed = mii_reg & PHY_REG_STATUS1_SPEED;
  413. switch (speed) {
  414. case PHY_REG_STATUS1_GBIT:
  415. phydev->speed = SPEED_1000;
  416. break;
  417. case PHY_REG_STATUS1_100:
  418. phydev->speed = SPEED_100;
  419. break;
  420. default:
  421. phydev->speed = SPEED_10;
  422. break;
  423. }
  424. return 0;
  425. }
  426. static int mv88e61xx_switch_reset(struct phy_device *phydev)
  427. {
  428. int time;
  429. int val;
  430. u8 port;
  431. /* Disable all ports */
  432. for (port = 0; port < PORT_COUNT; port++) {
  433. val = mv88e61xx_port_read(phydev, port, PORT_REG_CTRL);
  434. if (val < 0)
  435. return val;
  436. val = bitfield_replace(val, PORT_REG_CTRL_PSTATE_SHIFT,
  437. PORT_REG_CTRL_PSTATE_WIDTH,
  438. PORT_REG_CTRL_PSTATE_DISABLED);
  439. val = mv88e61xx_port_write(phydev, port, PORT_REG_CTRL, val);
  440. if (val < 0)
  441. return val;
  442. }
  443. /* Wait 2 ms for queues to drain */
  444. udelay(2000);
  445. /* Reset switch */
  446. val = mv88e61xx_reg_read(phydev, DEVADDR_GLOBAL_1, GLOBAL1_CTRL);
  447. if (val < 0)
  448. return val;
  449. val |= GLOBAL1_CTRL_SWRESET;
  450. val = mv88e61xx_reg_write(phydev, DEVADDR_GLOBAL_1,
  451. GLOBAL1_CTRL, val);
  452. if (val < 0)
  453. return val;
  454. /* Wait up to 1 second for switch reset complete */
  455. for (time = 1000; time; time--) {
  456. val = mv88e61xx_reg_read(phydev, DEVADDR_GLOBAL_1,
  457. GLOBAL1_CTRL);
  458. if (val >= 0 && ((val & GLOBAL1_CTRL_SWRESET) == 0))
  459. break;
  460. udelay(1000);
  461. }
  462. if (!time)
  463. return -ETIMEDOUT;
  464. return 0;
  465. }
  466. static int mv88e61xx_serdes_init(struct phy_device *phydev)
  467. {
  468. int val;
  469. val = mv88e61xx_set_page(phydev, DEVADDR_SERDES, PHY_PAGE_SERDES);
  470. if (val < 0)
  471. return val;
  472. /* Power up serdes module */
  473. val = mv88e61xx_phy_read(phydev, DEVADDR_SERDES, MII_BMCR);
  474. if (val < 0)
  475. return val;
  476. val &= ~(BMCR_PDOWN);
  477. val = mv88e61xx_phy_write(phydev, DEVADDR_SERDES, MII_BMCR, val);
  478. if (val < 0)
  479. return val;
  480. return 0;
  481. }
  482. static int mv88e61xx_port_enable(struct phy_device *phydev, u8 port)
  483. {
  484. int val;
  485. val = mv88e61xx_port_read(phydev, port, PORT_REG_CTRL);
  486. if (val < 0)
  487. return val;
  488. val = bitfield_replace(val, PORT_REG_CTRL_PSTATE_SHIFT,
  489. PORT_REG_CTRL_PSTATE_WIDTH,
  490. PORT_REG_CTRL_PSTATE_FORWARD);
  491. val = mv88e61xx_port_write(phydev, port, PORT_REG_CTRL, val);
  492. if (val < 0)
  493. return val;
  494. return 0;
  495. }
  496. static int mv88e61xx_port_set_vlan(struct phy_device *phydev, u8 port,
  497. u16 mask)
  498. {
  499. int val;
  500. /* Set VID to port number plus one */
  501. val = mv88e61xx_port_read(phydev, port, PORT_REG_VLAN_ID);
  502. if (val < 0)
  503. return val;
  504. val = bitfield_replace(val, PORT_REG_VLAN_ID_DEF_VID_SHIFT,
  505. PORT_REG_VLAN_ID_DEF_VID_WIDTH,
  506. port + 1);
  507. val = mv88e61xx_port_write(phydev, port, PORT_REG_VLAN_ID, val);
  508. if (val < 0)
  509. return val;
  510. /* Set VID mask */
  511. val = mv88e61xx_port_read(phydev, port, PORT_REG_VLAN_MAP);
  512. if (val < 0)
  513. return val;
  514. val = bitfield_replace(val, PORT_REG_VLAN_MAP_TABLE_SHIFT,
  515. PORT_REG_VLAN_MAP_TABLE_WIDTH,
  516. mask);
  517. val = mv88e61xx_port_write(phydev, port, PORT_REG_VLAN_MAP, val);
  518. if (val < 0)
  519. return val;
  520. return 0;
  521. }
  522. static int mv88e61xx_read_port_config(struct phy_device *phydev, u8 port)
  523. {
  524. int res;
  525. int val;
  526. bool forced = false;
  527. val = mv88e61xx_port_read(phydev, port, PORT_REG_STATUS);
  528. if (val < 0)
  529. return val;
  530. if (!(val & PORT_REG_STATUS_LINK)) {
  531. /* Temporarily force link to read port configuration */
  532. u32 timeout = 100;
  533. forced = true;
  534. val = mv88e61xx_port_read(phydev, port, PORT_REG_PHYS_CTRL);
  535. if (val < 0)
  536. return val;
  537. val |= (PORT_REG_PHYS_CTRL_LINK_FORCE |
  538. PORT_REG_PHYS_CTRL_LINK_VALUE);
  539. val = mv88e61xx_port_write(phydev, port, PORT_REG_PHYS_CTRL,
  540. val);
  541. if (val < 0)
  542. return val;
  543. /* Wait for status register to reflect forced link */
  544. do {
  545. val = mv88e61xx_port_read(phydev, port,
  546. PORT_REG_STATUS);
  547. if (val < 0) {
  548. res = -EIO;
  549. goto unforce;
  550. }
  551. if (val & PORT_REG_STATUS_LINK)
  552. break;
  553. } while (--timeout);
  554. if (timeout == 0) {
  555. res = -ETIMEDOUT;
  556. goto unforce;
  557. }
  558. }
  559. if (val & PORT_REG_STATUS_DUPLEX)
  560. phydev->duplex = DUPLEX_FULL;
  561. else
  562. phydev->duplex = DUPLEX_HALF;
  563. val = bitfield_extract(val, PORT_REG_STATUS_SPEED_SHIFT,
  564. PORT_REG_STATUS_SPEED_WIDTH);
  565. switch (val) {
  566. case PORT_REG_STATUS_SPEED_1000:
  567. phydev->speed = SPEED_1000;
  568. break;
  569. case PORT_REG_STATUS_SPEED_100:
  570. phydev->speed = SPEED_100;
  571. break;
  572. default:
  573. phydev->speed = SPEED_10;
  574. break;
  575. }
  576. res = 0;
  577. unforce:
  578. if (forced) {
  579. val = mv88e61xx_port_read(phydev, port, PORT_REG_PHYS_CTRL);
  580. if (val < 0)
  581. return val;
  582. val &= ~(PORT_REG_PHYS_CTRL_LINK_FORCE |
  583. PORT_REG_PHYS_CTRL_LINK_VALUE);
  584. val = mv88e61xx_port_write(phydev, port, PORT_REG_PHYS_CTRL,
  585. val);
  586. if (val < 0)
  587. return val;
  588. }
  589. return res;
  590. }
  591. static int mv88e61xx_fixed_port_setup(struct phy_device *phydev, u8 port)
  592. {
  593. int val;
  594. val = mv88e61xx_port_read(phydev, port, PORT_REG_PHYS_CTRL);
  595. if (val < 0)
  596. return val;
  597. val &= ~(PORT_REG_PHYS_CTRL_SPD_MASK |
  598. PORT_REG_PHYS_CTRL_FC_VALUE);
  599. val |= PORT_REG_PHYS_CTRL_PCS_AN_EN |
  600. PORT_REG_PHYS_CTRL_PCS_AN_RST |
  601. PORT_REG_PHYS_CTRL_FC_FORCE |
  602. PORT_REG_PHYS_CTRL_DUPLEX_VALUE |
  603. PORT_REG_PHYS_CTRL_DUPLEX_FORCE |
  604. PORT_REG_PHYS_CTRL_SPD1000;
  605. if (port == CONFIG_MV88E61XX_CPU_PORT)
  606. val |= PORT_REG_PHYS_CTRL_LINK_VALUE |
  607. PORT_REG_PHYS_CTRL_LINK_FORCE;
  608. return mv88e61xx_port_write(phydev, port, PORT_REG_PHYS_CTRL,
  609. val);
  610. }
  611. static int mv88e61xx_set_cpu_port(struct phy_device *phydev)
  612. {
  613. int val;
  614. /* Set CPUDest */
  615. val = mv88e61xx_reg_read(phydev, DEVADDR_GLOBAL_1, GLOBAL1_MON_CTRL);
  616. if (val < 0)
  617. return val;
  618. val = bitfield_replace(val, GLOBAL1_MON_CTRL_CPUDEST_SHIFT,
  619. GLOBAL1_MON_CTRL_CPUDEST_WIDTH,
  620. CONFIG_MV88E61XX_CPU_PORT);
  621. val = mv88e61xx_reg_write(phydev, DEVADDR_GLOBAL_1,
  622. GLOBAL1_MON_CTRL, val);
  623. if (val < 0)
  624. return val;
  625. /* Allow CPU to route to any port */
  626. val = PORT_MASK & ~(1 << CONFIG_MV88E61XX_CPU_PORT);
  627. val = mv88e61xx_port_set_vlan(phydev, CONFIG_MV88E61XX_CPU_PORT, val);
  628. if (val < 0)
  629. return val;
  630. /* Enable CPU port */
  631. val = mv88e61xx_port_enable(phydev, CONFIG_MV88E61XX_CPU_PORT);
  632. if (val < 0)
  633. return val;
  634. val = mv88e61xx_read_port_config(phydev, CONFIG_MV88E61XX_CPU_PORT);
  635. if (val < 0)
  636. return val;
  637. /* If CPU is connected to serdes, initialize serdes */
  638. if (mv88e61xx_6352_family(phydev)) {
  639. val = mv88e61xx_get_cmode(phydev, CONFIG_MV88E61XX_CPU_PORT);
  640. if (val < 0)
  641. return val;
  642. if (val == PORT_REG_STATUS_CMODE_100BASE_X ||
  643. val == PORT_REG_STATUS_CMODE_1000BASE_X ||
  644. val == PORT_REG_STATUS_CMODE_SGMII) {
  645. val = mv88e61xx_serdes_init(phydev);
  646. if (val < 0)
  647. return val;
  648. }
  649. } else {
  650. val = mv88e61xx_fixed_port_setup(phydev,
  651. CONFIG_MV88E61XX_CPU_PORT);
  652. if (val < 0)
  653. return val;
  654. }
  655. return 0;
  656. }
  657. static int mv88e61xx_switch_init(struct phy_device *phydev)
  658. {
  659. static int init;
  660. int res;
  661. if (init)
  662. return 0;
  663. res = mv88e61xx_switch_reset(phydev);
  664. if (res < 0)
  665. return res;
  666. res = mv88e61xx_set_cpu_port(phydev);
  667. if (res < 0)
  668. return res;
  669. init = 1;
  670. return 0;
  671. }
  672. static int mv88e61xx_phy_enable(struct phy_device *phydev, u8 phy)
  673. {
  674. int val;
  675. val = mv88e61xx_phy_read(phydev, phy, MII_BMCR);
  676. if (val < 0)
  677. return val;
  678. val &= ~(BMCR_PDOWN);
  679. val = mv88e61xx_phy_write(phydev, phy, MII_BMCR, val);
  680. if (val < 0)
  681. return val;
  682. return 0;
  683. }
  684. static int mv88e61xx_phy_setup(struct phy_device *phydev, u8 phy)
  685. {
  686. int val;
  687. /*
  688. * Enable energy-detect sensing on PHY, used to determine when a PHY
  689. * port is physically connected
  690. */
  691. val = mv88e61xx_phy_read(phydev, phy, PHY_REG_CTRL1);
  692. if (val < 0)
  693. return val;
  694. val = bitfield_replace(val, PHY_REG_CTRL1_ENERGY_DET_SHIFT,
  695. PHY_REG_CTRL1_ENERGY_DET_WIDTH,
  696. PHY_REG_CTRL1_ENERGY_DET_SENSE_XMIT);
  697. val = mv88e61xx_phy_write(phydev, phy, PHY_REG_CTRL1, val);
  698. if (val < 0)
  699. return val;
  700. return 0;
  701. }
  702. static int mv88e61xx_phy_config_port(struct phy_device *phydev, u8 phy)
  703. {
  704. int val;
  705. val = mv88e61xx_port_enable(phydev, phy);
  706. if (val < 0)
  707. return val;
  708. val = mv88e61xx_port_set_vlan(phydev, phy,
  709. 1 << CONFIG_MV88E61XX_CPU_PORT);
  710. if (val < 0)
  711. return val;
  712. return 0;
  713. }
  714. static int mv88e61xx_probe(struct phy_device *phydev)
  715. {
  716. struct mii_dev *smi_wrapper;
  717. struct mv88e61xx_phy_priv *priv;
  718. int res;
  719. res = mv88e61xx_hw_reset(phydev);
  720. if (res < 0)
  721. return res;
  722. priv = malloc(sizeof(*priv));
  723. if (!priv)
  724. return -ENOMEM;
  725. memset(priv, 0, sizeof(*priv));
  726. /*
  727. * This device requires indirect reads/writes to the PHY registers
  728. * which the generic PHY code can't handle. Make a wrapper MII device
  729. * to handle reads/writes
  730. */
  731. smi_wrapper = mdio_alloc();
  732. if (!smi_wrapper) {
  733. free(priv);
  734. return -ENOMEM;
  735. }
  736. /*
  737. * Store the mdio bus in the private data, as we are going to replace
  738. * the bus with the wrapper bus
  739. */
  740. priv->mdio_bus = phydev->bus;
  741. /*
  742. * Store the smi bus address in private data. This lets us use the
  743. * phydev addr field for device address instead, as the genphy code
  744. * expects.
  745. */
  746. priv->smi_addr = phydev->addr;
  747. /*
  748. * Store the phy_device in the wrapper mii device. This lets us get it
  749. * back when genphy functions call phy_read/phy_write.
  750. */
  751. smi_wrapper->priv = phydev;
  752. strncpy(smi_wrapper->name, "indirect mii", sizeof(smi_wrapper->name));
  753. smi_wrapper->read = mv88e61xx_phy_read_indirect;
  754. smi_wrapper->write = mv88e61xx_phy_write_indirect;
  755. /* Replace the bus with the wrapper device */
  756. phydev->bus = smi_wrapper;
  757. phydev->priv = priv;
  758. priv->id = mv88e61xx_get_switch_id(phydev);
  759. return 0;
  760. }
  761. static int mv88e61xx_phy_config(struct phy_device *phydev)
  762. {
  763. int res;
  764. int i;
  765. int ret = -1;
  766. res = mv88e61xx_switch_init(phydev);
  767. if (res < 0)
  768. return res;
  769. for (i = 0; i < PORT_COUNT; i++) {
  770. if ((1 << i) & CONFIG_MV88E61XX_PHY_PORTS) {
  771. phydev->addr = i;
  772. res = mv88e61xx_phy_enable(phydev, i);
  773. if (res < 0) {
  774. printf("Error enabling PHY %i\n", i);
  775. continue;
  776. }
  777. res = mv88e61xx_phy_setup(phydev, i);
  778. if (res < 0) {
  779. printf("Error setting up PHY %i\n", i);
  780. continue;
  781. }
  782. res = mv88e61xx_phy_config_port(phydev, i);
  783. if (res < 0) {
  784. printf("Error configuring PHY %i\n", i);
  785. continue;
  786. }
  787. res = genphy_config_aneg(phydev);
  788. if (res < 0) {
  789. printf("Error setting PHY %i autoneg\n", i);
  790. continue;
  791. }
  792. res = phy_reset(phydev);
  793. if (res < 0) {
  794. printf("Error resetting PHY %i\n", i);
  795. continue;
  796. }
  797. /* Return success if any PHY succeeds */
  798. ret = 0;
  799. } else if ((1 << i) & CONFIG_MV88E61XX_FIXED_PORTS) {
  800. res = mv88e61xx_fixed_port_setup(phydev, i);
  801. if (res < 0) {
  802. printf("Error configuring port %i\n", i);
  803. continue;
  804. }
  805. }
  806. }
  807. return ret;
  808. }
  809. static int mv88e61xx_phy_is_connected(struct phy_device *phydev)
  810. {
  811. int val;
  812. val = mv88e61xx_phy_read(phydev, phydev->addr, PHY_REG_STATUS1);
  813. if (val < 0)
  814. return 0;
  815. /*
  816. * After reset, the energy detect signal remains high for a few seconds
  817. * regardless of whether a cable is connected. This function will
  818. * return false positives during this time.
  819. */
  820. return (val & PHY_REG_STATUS1_ENERGY) == 0;
  821. }
  822. static int mv88e61xx_phy_startup(struct phy_device *phydev)
  823. {
  824. int i;
  825. int link = 0;
  826. int res;
  827. int speed = phydev->speed;
  828. int duplex = phydev->duplex;
  829. for (i = 0; i < PORT_COUNT; i++) {
  830. if ((1 << i) & CONFIG_MV88E61XX_PHY_PORTS) {
  831. phydev->addr = i;
  832. if (!mv88e61xx_phy_is_connected(phydev))
  833. continue;
  834. res = genphy_update_link(phydev);
  835. if (res < 0)
  836. continue;
  837. res = mv88e61xx_parse_status(phydev);
  838. if (res < 0)
  839. continue;
  840. link = (link || phydev->link);
  841. }
  842. }
  843. phydev->link = link;
  844. /* Restore CPU interface speed and duplex after it was changed for
  845. * other ports */
  846. phydev->speed = speed;
  847. phydev->duplex = duplex;
  848. return 0;
  849. }
  850. static struct phy_driver mv88e61xx_driver = {
  851. .name = "Marvell MV88E61xx",
  852. .uid = 0x01410eb1,
  853. .mask = 0xfffffff0,
  854. .features = PHY_GBIT_FEATURES,
  855. .probe = mv88e61xx_probe,
  856. .config = mv88e61xx_phy_config,
  857. .startup = mv88e61xx_phy_startup,
  858. .shutdown = &genphy_shutdown,
  859. };
  860. static struct phy_driver mv88e609x_driver = {
  861. .name = "Marvell MV88E609x",
  862. .uid = 0x1410c89,
  863. .mask = 0xfffffff0,
  864. .features = PHY_GBIT_FEATURES,
  865. .probe = mv88e61xx_probe,
  866. .config = mv88e61xx_phy_config,
  867. .startup = mv88e61xx_phy_startup,
  868. .shutdown = &genphy_shutdown,
  869. };
  870. int phy_mv88e61xx_init(void)
  871. {
  872. phy_register(&mv88e61xx_driver);
  873. phy_register(&mv88e609x_driver);
  874. return 0;
  875. }
  876. /*
  877. * Overload weak get_phy_id definition since we need non-standard functions
  878. * to read PHY registers
  879. */
  880. int get_phy_id(struct mii_dev *bus, int smi_addr, int devad, u32 *phy_id)
  881. {
  882. struct phy_device temp_phy;
  883. struct mv88e61xx_phy_priv temp_priv;
  884. struct mii_dev temp_mii;
  885. int val;
  886. /*
  887. * Buid temporary data structures that the chip reading code needs to
  888. * read the ID
  889. */
  890. temp_priv.mdio_bus = bus;
  891. temp_priv.smi_addr = smi_addr;
  892. temp_phy.priv = &temp_priv;
  893. temp_mii.priv = &temp_phy;
  894. val = mv88e61xx_phy_read_indirect(&temp_mii, 0, devad, MII_PHYSID1);
  895. if (val < 0)
  896. return -EIO;
  897. *phy_id = val << 16;
  898. val = mv88e61xx_phy_read_indirect(&temp_mii, 0, devad, MII_PHYSID2);
  899. if (val < 0)
  900. return -EIO;
  901. *phy_id |= (val & 0xffff);
  902. return 0;
  903. }