mv88e61xx.c 26 KB

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