micrel.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * drivers/net/phy/micrel.c
  3. *
  4. * Driver for Micrel PHYs
  5. *
  6. * Author: David J. Choi
  7. *
  8. * Copyright (c) 2010-2013 Micrel, Inc.
  9. * Copyright (c) 2014 Johan Hovold <johan@kernel.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. * Support : Micrel Phys:
  17. * Giga phys: ksz9021, ksz9031
  18. * 100/10 Phys : ksz8001, ksz8721, ksz8737, ksz8041
  19. * ksz8021, ksz8031, ksz8051,
  20. * ksz8081, ksz8091,
  21. * ksz8061,
  22. * Switch : ksz8873, ksz886x
  23. * ksz9477
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/phy.h>
  28. #include <linux/micrel_phy.h>
  29. #include <linux/of.h>
  30. #include <linux/clk.h>
  31. #include <linux/delay.h>
  32. /* Operation Mode Strap Override */
  33. #define MII_KSZPHY_OMSO 0x16
  34. #define KSZPHY_OMSO_B_CAST_OFF BIT(9)
  35. #define KSZPHY_OMSO_NAND_TREE_ON BIT(5)
  36. #define KSZPHY_OMSO_RMII_OVERRIDE BIT(1)
  37. #define KSZPHY_OMSO_MII_OVERRIDE BIT(0)
  38. /* general Interrupt control/status reg in vendor specific block. */
  39. #define MII_KSZPHY_INTCS 0x1B
  40. #define KSZPHY_INTCS_JABBER BIT(15)
  41. #define KSZPHY_INTCS_RECEIVE_ERR BIT(14)
  42. #define KSZPHY_INTCS_PAGE_RECEIVE BIT(13)
  43. #define KSZPHY_INTCS_PARELLEL BIT(12)
  44. #define KSZPHY_INTCS_LINK_PARTNER_ACK BIT(11)
  45. #define KSZPHY_INTCS_LINK_DOWN BIT(10)
  46. #define KSZPHY_INTCS_REMOTE_FAULT BIT(9)
  47. #define KSZPHY_INTCS_LINK_UP BIT(8)
  48. #define KSZPHY_INTCS_ALL (KSZPHY_INTCS_LINK_UP |\
  49. KSZPHY_INTCS_LINK_DOWN)
  50. /* PHY Control 1 */
  51. #define MII_KSZPHY_CTRL_1 0x1e
  52. /* PHY Control 2 / PHY Control (if no PHY Control 1) */
  53. #define MII_KSZPHY_CTRL_2 0x1f
  54. #define MII_KSZPHY_CTRL MII_KSZPHY_CTRL_2
  55. /* bitmap of PHY register to set interrupt mode */
  56. #define KSZPHY_CTRL_INT_ACTIVE_HIGH BIT(9)
  57. #define KSZPHY_RMII_REF_CLK_SEL BIT(7)
  58. /* Write/read to/from extended registers */
  59. #define MII_KSZPHY_EXTREG 0x0b
  60. #define KSZPHY_EXTREG_WRITE 0x8000
  61. #define MII_KSZPHY_EXTREG_WRITE 0x0c
  62. #define MII_KSZPHY_EXTREG_READ 0x0d
  63. /* Extended registers */
  64. #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW 0x104
  65. #define MII_KSZPHY_RX_DATA_PAD_SKEW 0x105
  66. #define MII_KSZPHY_TX_DATA_PAD_SKEW 0x106
  67. #define PS_TO_REG 200
  68. struct kszphy_hw_stat {
  69. const char *string;
  70. u8 reg;
  71. u8 bits;
  72. };
  73. static struct kszphy_hw_stat kszphy_hw_stats[] = {
  74. { "phy_receive_errors", 21, 16},
  75. { "phy_idle_errors", 10, 8 },
  76. };
  77. struct kszphy_type {
  78. u32 led_mode_reg;
  79. u16 interrupt_level_mask;
  80. bool has_broadcast_disable;
  81. bool has_nand_tree_disable;
  82. bool has_rmii_ref_clk_sel;
  83. };
  84. struct kszphy_priv {
  85. const struct kszphy_type *type;
  86. int led_mode;
  87. bool rmii_ref_clk_sel;
  88. bool rmii_ref_clk_sel_val;
  89. u64 stats[ARRAY_SIZE(kszphy_hw_stats)];
  90. };
  91. static const struct kszphy_type ksz8021_type = {
  92. .led_mode_reg = MII_KSZPHY_CTRL_2,
  93. .has_broadcast_disable = true,
  94. .has_nand_tree_disable = true,
  95. .has_rmii_ref_clk_sel = true,
  96. };
  97. static const struct kszphy_type ksz8041_type = {
  98. .led_mode_reg = MII_KSZPHY_CTRL_1,
  99. };
  100. static const struct kszphy_type ksz8051_type = {
  101. .led_mode_reg = MII_KSZPHY_CTRL_2,
  102. .has_nand_tree_disable = true,
  103. };
  104. static const struct kszphy_type ksz8081_type = {
  105. .led_mode_reg = MII_KSZPHY_CTRL_2,
  106. .has_broadcast_disable = true,
  107. .has_nand_tree_disable = true,
  108. .has_rmii_ref_clk_sel = true,
  109. };
  110. static const struct kszphy_type ks8737_type = {
  111. .interrupt_level_mask = BIT(14),
  112. };
  113. static const struct kszphy_type ksz9021_type = {
  114. .interrupt_level_mask = BIT(14),
  115. };
  116. static int kszphy_extended_write(struct phy_device *phydev,
  117. u32 regnum, u16 val)
  118. {
  119. phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum);
  120. return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val);
  121. }
  122. static int kszphy_extended_read(struct phy_device *phydev,
  123. u32 regnum)
  124. {
  125. phy_write(phydev, MII_KSZPHY_EXTREG, regnum);
  126. return phy_read(phydev, MII_KSZPHY_EXTREG_READ);
  127. }
  128. static int kszphy_ack_interrupt(struct phy_device *phydev)
  129. {
  130. /* bit[7..0] int status, which is a read and clear register. */
  131. int rc;
  132. rc = phy_read(phydev, MII_KSZPHY_INTCS);
  133. return (rc < 0) ? rc : 0;
  134. }
  135. static int kszphy_config_intr(struct phy_device *phydev)
  136. {
  137. const struct kszphy_type *type = phydev->drv->driver_data;
  138. int temp;
  139. u16 mask;
  140. if (type && type->interrupt_level_mask)
  141. mask = type->interrupt_level_mask;
  142. else
  143. mask = KSZPHY_CTRL_INT_ACTIVE_HIGH;
  144. /* set the interrupt pin active low */
  145. temp = phy_read(phydev, MII_KSZPHY_CTRL);
  146. if (temp < 0)
  147. return temp;
  148. temp &= ~mask;
  149. phy_write(phydev, MII_KSZPHY_CTRL, temp);
  150. /* enable / disable interrupts */
  151. if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
  152. temp = KSZPHY_INTCS_ALL;
  153. else
  154. temp = 0;
  155. return phy_write(phydev, MII_KSZPHY_INTCS, temp);
  156. }
  157. static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val)
  158. {
  159. int ctrl;
  160. ctrl = phy_read(phydev, MII_KSZPHY_CTRL);
  161. if (ctrl < 0)
  162. return ctrl;
  163. if (val)
  164. ctrl |= KSZPHY_RMII_REF_CLK_SEL;
  165. else
  166. ctrl &= ~KSZPHY_RMII_REF_CLK_SEL;
  167. return phy_write(phydev, MII_KSZPHY_CTRL, ctrl);
  168. }
  169. static int kszphy_setup_led(struct phy_device *phydev, u32 reg, int val)
  170. {
  171. int rc, temp, shift;
  172. switch (reg) {
  173. case MII_KSZPHY_CTRL_1:
  174. shift = 14;
  175. break;
  176. case MII_KSZPHY_CTRL_2:
  177. shift = 4;
  178. break;
  179. default:
  180. return -EINVAL;
  181. }
  182. temp = phy_read(phydev, reg);
  183. if (temp < 0) {
  184. rc = temp;
  185. goto out;
  186. }
  187. temp &= ~(3 << shift);
  188. temp |= val << shift;
  189. rc = phy_write(phydev, reg, temp);
  190. out:
  191. if (rc < 0)
  192. phydev_err(phydev, "failed to set led mode\n");
  193. return rc;
  194. }
  195. /* Disable PHY address 0 as the broadcast address, so that it can be used as a
  196. * unique (non-broadcast) address on a shared bus.
  197. */
  198. static int kszphy_broadcast_disable(struct phy_device *phydev)
  199. {
  200. int ret;
  201. ret = phy_read(phydev, MII_KSZPHY_OMSO);
  202. if (ret < 0)
  203. goto out;
  204. ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF);
  205. out:
  206. if (ret)
  207. phydev_err(phydev, "failed to disable broadcast address\n");
  208. return ret;
  209. }
  210. static int kszphy_nand_tree_disable(struct phy_device *phydev)
  211. {
  212. int ret;
  213. ret = phy_read(phydev, MII_KSZPHY_OMSO);
  214. if (ret < 0)
  215. goto out;
  216. if (!(ret & KSZPHY_OMSO_NAND_TREE_ON))
  217. return 0;
  218. ret = phy_write(phydev, MII_KSZPHY_OMSO,
  219. ret & ~KSZPHY_OMSO_NAND_TREE_ON);
  220. out:
  221. if (ret)
  222. phydev_err(phydev, "failed to disable NAND tree mode\n");
  223. return ret;
  224. }
  225. /* Some config bits need to be set again on resume, handle them here. */
  226. static int kszphy_config_reset(struct phy_device *phydev)
  227. {
  228. struct kszphy_priv *priv = phydev->priv;
  229. int ret;
  230. if (priv->rmii_ref_clk_sel) {
  231. ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val);
  232. if (ret) {
  233. phydev_err(phydev,
  234. "failed to set rmii reference clock\n");
  235. return ret;
  236. }
  237. }
  238. if (priv->led_mode >= 0)
  239. kszphy_setup_led(phydev, priv->type->led_mode_reg, priv->led_mode);
  240. return 0;
  241. }
  242. static int kszphy_config_init(struct phy_device *phydev)
  243. {
  244. struct kszphy_priv *priv = phydev->priv;
  245. const struct kszphy_type *type;
  246. if (!priv)
  247. return 0;
  248. type = priv->type;
  249. if (type->has_broadcast_disable)
  250. kszphy_broadcast_disable(phydev);
  251. if (type->has_nand_tree_disable)
  252. kszphy_nand_tree_disable(phydev);
  253. return kszphy_config_reset(phydev);
  254. }
  255. static int ksz8041_config_init(struct phy_device *phydev)
  256. {
  257. struct device_node *of_node = phydev->mdio.dev.of_node;
  258. /* Limit supported and advertised modes in fiber mode */
  259. if (of_property_read_bool(of_node, "micrel,fiber-mode")) {
  260. phydev->dev_flags |= MICREL_PHY_FXEN;
  261. phydev->supported &= SUPPORTED_100baseT_Full |
  262. SUPPORTED_100baseT_Half;
  263. phydev->supported |= SUPPORTED_FIBRE;
  264. phydev->advertising &= ADVERTISED_100baseT_Full |
  265. ADVERTISED_100baseT_Half;
  266. phydev->advertising |= ADVERTISED_FIBRE;
  267. phydev->autoneg = AUTONEG_DISABLE;
  268. }
  269. return kszphy_config_init(phydev);
  270. }
  271. static int ksz8041_config_aneg(struct phy_device *phydev)
  272. {
  273. /* Skip auto-negotiation in fiber mode */
  274. if (phydev->dev_flags & MICREL_PHY_FXEN) {
  275. phydev->speed = SPEED_100;
  276. return 0;
  277. }
  278. return genphy_config_aneg(phydev);
  279. }
  280. static int ksz8061_config_init(struct phy_device *phydev)
  281. {
  282. int ret;
  283. ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A);
  284. if (ret)
  285. return ret;
  286. return kszphy_config_init(phydev);
  287. }
  288. static int ksz9021_load_values_from_of(struct phy_device *phydev,
  289. const struct device_node *of_node,
  290. u16 reg,
  291. const char *field1, const char *field2,
  292. const char *field3, const char *field4)
  293. {
  294. int val1 = -1;
  295. int val2 = -2;
  296. int val3 = -3;
  297. int val4 = -4;
  298. int newval;
  299. int matches = 0;
  300. if (!of_property_read_u32(of_node, field1, &val1))
  301. matches++;
  302. if (!of_property_read_u32(of_node, field2, &val2))
  303. matches++;
  304. if (!of_property_read_u32(of_node, field3, &val3))
  305. matches++;
  306. if (!of_property_read_u32(of_node, field4, &val4))
  307. matches++;
  308. if (!matches)
  309. return 0;
  310. if (matches < 4)
  311. newval = kszphy_extended_read(phydev, reg);
  312. else
  313. newval = 0;
  314. if (val1 != -1)
  315. newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0);
  316. if (val2 != -2)
  317. newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4);
  318. if (val3 != -3)
  319. newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8);
  320. if (val4 != -4)
  321. newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12);
  322. return kszphy_extended_write(phydev, reg, newval);
  323. }
  324. static int ksz9021_config_init(struct phy_device *phydev)
  325. {
  326. const struct device *dev = &phydev->mdio.dev;
  327. const struct device_node *of_node = dev->of_node;
  328. const struct device *dev_walker;
  329. /* The Micrel driver has a deprecated option to place phy OF
  330. * properties in the MAC node. Walk up the tree of devices to
  331. * find a device with an OF node.
  332. */
  333. dev_walker = &phydev->mdio.dev;
  334. do {
  335. of_node = dev_walker->of_node;
  336. dev_walker = dev_walker->parent;
  337. } while (!of_node && dev_walker);
  338. if (of_node) {
  339. ksz9021_load_values_from_of(phydev, of_node,
  340. MII_KSZPHY_CLK_CONTROL_PAD_SKEW,
  341. "txen-skew-ps", "txc-skew-ps",
  342. "rxdv-skew-ps", "rxc-skew-ps");
  343. ksz9021_load_values_from_of(phydev, of_node,
  344. MII_KSZPHY_RX_DATA_PAD_SKEW,
  345. "rxd0-skew-ps", "rxd1-skew-ps",
  346. "rxd2-skew-ps", "rxd3-skew-ps");
  347. ksz9021_load_values_from_of(phydev, of_node,
  348. MII_KSZPHY_TX_DATA_PAD_SKEW,
  349. "txd0-skew-ps", "txd1-skew-ps",
  350. "txd2-skew-ps", "txd3-skew-ps");
  351. }
  352. return 0;
  353. }
  354. #define MII_KSZ9031RN_MMD_CTRL_REG 0x0d
  355. #define MII_KSZ9031RN_MMD_REGDATA_REG 0x0e
  356. #define OP_DATA 1
  357. #define KSZ9031_PS_TO_REG 60
  358. /* Extended registers */
  359. /* MMD Address 0x0 */
  360. #define MII_KSZ9031RN_FLP_BURST_TX_LO 3
  361. #define MII_KSZ9031RN_FLP_BURST_TX_HI 4
  362. /* MMD Address 0x2 */
  363. #define MII_KSZ9031RN_CONTROL_PAD_SKEW 4
  364. #define MII_KSZ9031RN_RX_DATA_PAD_SKEW 5
  365. #define MII_KSZ9031RN_TX_DATA_PAD_SKEW 6
  366. #define MII_KSZ9031RN_CLK_PAD_SKEW 8
  367. /* MMD Address 0x1C */
  368. #define MII_KSZ9031RN_EDPD 0x23
  369. #define MII_KSZ9031RN_EDPD_ENABLE BIT(0)
  370. static int ksz9031_extended_write(struct phy_device *phydev,
  371. u8 mode, u32 dev_addr, u32 regnum, u16 val)
  372. {
  373. phy_write(phydev, MII_KSZ9031RN_MMD_CTRL_REG, dev_addr);
  374. phy_write(phydev, MII_KSZ9031RN_MMD_REGDATA_REG, regnum);
  375. phy_write(phydev, MII_KSZ9031RN_MMD_CTRL_REG, (mode << 14) | dev_addr);
  376. return phy_write(phydev, MII_KSZ9031RN_MMD_REGDATA_REG, val);
  377. }
  378. static int ksz9031_extended_read(struct phy_device *phydev,
  379. u8 mode, u32 dev_addr, u32 regnum)
  380. {
  381. phy_write(phydev, MII_KSZ9031RN_MMD_CTRL_REG, dev_addr);
  382. phy_write(phydev, MII_KSZ9031RN_MMD_REGDATA_REG, regnum);
  383. phy_write(phydev, MII_KSZ9031RN_MMD_CTRL_REG, (mode << 14) | dev_addr);
  384. return phy_read(phydev, MII_KSZ9031RN_MMD_REGDATA_REG);
  385. }
  386. static int ksz9031_of_load_skew_values(struct phy_device *phydev,
  387. const struct device_node *of_node,
  388. u16 reg, size_t field_sz,
  389. const char *field[], u8 numfields)
  390. {
  391. int val[4] = {-1, -2, -3, -4};
  392. int matches = 0;
  393. u16 mask;
  394. u16 maxval;
  395. u16 newval;
  396. int i;
  397. for (i = 0; i < numfields; i++)
  398. if (!of_property_read_u32(of_node, field[i], val + i))
  399. matches++;
  400. if (!matches)
  401. return 0;
  402. if (matches < numfields)
  403. newval = ksz9031_extended_read(phydev, OP_DATA, 2, reg);
  404. else
  405. newval = 0;
  406. maxval = (field_sz == 4) ? 0xf : 0x1f;
  407. for (i = 0; i < numfields; i++)
  408. if (val[i] != -(i + 1)) {
  409. mask = 0xffff;
  410. mask ^= maxval << (field_sz * i);
  411. newval = (newval & mask) |
  412. (((val[i] / KSZ9031_PS_TO_REG) & maxval)
  413. << (field_sz * i));
  414. }
  415. return ksz9031_extended_write(phydev, OP_DATA, 2, reg, newval);
  416. }
  417. /* Center KSZ9031RNX FLP timing at 16ms. */
  418. static int ksz9031_center_flp_timing(struct phy_device *phydev)
  419. {
  420. int result;
  421. result = ksz9031_extended_write(phydev, OP_DATA, 0,
  422. MII_KSZ9031RN_FLP_BURST_TX_HI, 0x0006);
  423. if (result)
  424. return result;
  425. result = ksz9031_extended_write(phydev, OP_DATA, 0,
  426. MII_KSZ9031RN_FLP_BURST_TX_LO, 0x1A80);
  427. if (result)
  428. return result;
  429. return genphy_restart_aneg(phydev);
  430. }
  431. /* Enable energy-detect power-down mode */
  432. static int ksz9031_enable_edpd(struct phy_device *phydev)
  433. {
  434. int reg;
  435. reg = ksz9031_extended_read(phydev, OP_DATA, 0x1C, MII_KSZ9031RN_EDPD);
  436. if (reg < 0)
  437. return reg;
  438. return ksz9031_extended_write(phydev, OP_DATA, 0x1C, MII_KSZ9031RN_EDPD,
  439. reg | MII_KSZ9031RN_EDPD_ENABLE);
  440. }
  441. static int ksz9031_config_init(struct phy_device *phydev)
  442. {
  443. const struct device *dev = &phydev->mdio.dev;
  444. const struct device_node *of_node = dev->of_node;
  445. static const char *clk_skews[2] = {"rxc-skew-ps", "txc-skew-ps"};
  446. static const char *rx_data_skews[4] = {
  447. "rxd0-skew-ps", "rxd1-skew-ps",
  448. "rxd2-skew-ps", "rxd3-skew-ps"
  449. };
  450. static const char *tx_data_skews[4] = {
  451. "txd0-skew-ps", "txd1-skew-ps",
  452. "txd2-skew-ps", "txd3-skew-ps"
  453. };
  454. static const char *control_skews[2] = {"txen-skew-ps", "rxdv-skew-ps"};
  455. const struct device *dev_walker;
  456. int result;
  457. result = ksz9031_enable_edpd(phydev);
  458. if (result < 0)
  459. return result;
  460. /* The Micrel driver has a deprecated option to place phy OF
  461. * properties in the MAC node. Walk up the tree of devices to
  462. * find a device with an OF node.
  463. */
  464. dev_walker = &phydev->mdio.dev;
  465. do {
  466. of_node = dev_walker->of_node;
  467. dev_walker = dev_walker->parent;
  468. } while (!of_node && dev_walker);
  469. if (of_node) {
  470. ksz9031_of_load_skew_values(phydev, of_node,
  471. MII_KSZ9031RN_CLK_PAD_SKEW, 5,
  472. clk_skews, 2);
  473. ksz9031_of_load_skew_values(phydev, of_node,
  474. MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
  475. control_skews, 2);
  476. ksz9031_of_load_skew_values(phydev, of_node,
  477. MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
  478. rx_data_skews, 4);
  479. ksz9031_of_load_skew_values(phydev, of_node,
  480. MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
  481. tx_data_skews, 4);
  482. /* Silicon Errata Sheet (DS80000691D or DS80000692D):
  483. * When the device links in the 1000BASE-T slave mode only,
  484. * the optional 125MHz reference output clock (CLK125_NDO)
  485. * has wide duty cycle variation.
  486. *
  487. * The optional CLK125_NDO clock does not meet the RGMII
  488. * 45/55 percent (min/max) duty cycle requirement and therefore
  489. * cannot be used directly by the MAC side for clocking
  490. * applications that have setup/hold time requirements on
  491. * rising and falling clock edges.
  492. *
  493. * Workaround:
  494. * Force the phy to be the master to receive a stable clock
  495. * which meets the duty cycle requirement.
  496. */
  497. if (of_property_read_bool(of_node, "micrel,force-master")) {
  498. result = phy_read(phydev, MII_CTRL1000);
  499. if (result < 0)
  500. goto err_force_master;
  501. /* enable master mode, config & prefer master */
  502. result |= CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER;
  503. result = phy_write(phydev, MII_CTRL1000, result);
  504. if (result < 0)
  505. goto err_force_master;
  506. }
  507. }
  508. return ksz9031_center_flp_timing(phydev);
  509. err_force_master:
  510. phydev_err(phydev, "failed to force the phy to master mode\n");
  511. return result;
  512. }
  513. #define KSZ8873MLL_GLOBAL_CONTROL_4 0x06
  514. #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX BIT(6)
  515. #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED BIT(4)
  516. static int ksz8873mll_read_status(struct phy_device *phydev)
  517. {
  518. int regval;
  519. /* dummy read */
  520. regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
  521. regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
  522. if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX)
  523. phydev->duplex = DUPLEX_HALF;
  524. else
  525. phydev->duplex = DUPLEX_FULL;
  526. if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
  527. phydev->speed = SPEED_10;
  528. else
  529. phydev->speed = SPEED_100;
  530. phydev->link = 1;
  531. phydev->pause = phydev->asym_pause = 0;
  532. return 0;
  533. }
  534. static int ksz9031_read_status(struct phy_device *phydev)
  535. {
  536. int err;
  537. int regval;
  538. err = genphy_read_status(phydev);
  539. if (err)
  540. return err;
  541. /* Make sure the PHY is not broken. Read idle error count,
  542. * and reset the PHY if it is maxed out.
  543. */
  544. regval = phy_read(phydev, MII_STAT1000);
  545. if ((regval & 0xFF) == 0xFF) {
  546. phy_init_hw(phydev);
  547. phydev->link = 0;
  548. if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev))
  549. phydev->drv->config_intr(phydev);
  550. return genphy_config_aneg(phydev);
  551. }
  552. return 0;
  553. }
  554. static int ksz8873mll_config_aneg(struct phy_device *phydev)
  555. {
  556. return 0;
  557. }
  558. static int kszphy_get_sset_count(struct phy_device *phydev)
  559. {
  560. return ARRAY_SIZE(kszphy_hw_stats);
  561. }
  562. static void kszphy_get_strings(struct phy_device *phydev, u8 *data)
  563. {
  564. int i;
  565. for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) {
  566. strlcpy(data + i * ETH_GSTRING_LEN,
  567. kszphy_hw_stats[i].string, ETH_GSTRING_LEN);
  568. }
  569. }
  570. static u64 kszphy_get_stat(struct phy_device *phydev, int i)
  571. {
  572. struct kszphy_hw_stat stat = kszphy_hw_stats[i];
  573. struct kszphy_priv *priv = phydev->priv;
  574. int val;
  575. u64 ret;
  576. val = phy_read(phydev, stat.reg);
  577. if (val < 0) {
  578. ret = U64_MAX;
  579. } else {
  580. val = val & ((1 << stat.bits) - 1);
  581. priv->stats[i] += val;
  582. ret = priv->stats[i];
  583. }
  584. return ret;
  585. }
  586. static void kszphy_get_stats(struct phy_device *phydev,
  587. struct ethtool_stats *stats, u64 *data)
  588. {
  589. int i;
  590. for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++)
  591. data[i] = kszphy_get_stat(phydev, i);
  592. }
  593. static int kszphy_suspend(struct phy_device *phydev)
  594. {
  595. /* Disable PHY Interrupts */
  596. if (phy_interrupt_is_valid(phydev)) {
  597. phydev->interrupts = PHY_INTERRUPT_DISABLED;
  598. if (phydev->drv->config_intr)
  599. phydev->drv->config_intr(phydev);
  600. }
  601. return genphy_suspend(phydev);
  602. }
  603. static int kszphy_resume(struct phy_device *phydev)
  604. {
  605. int ret;
  606. genphy_resume(phydev);
  607. /* After switching from power-down to normal mode, an internal global
  608. * reset is automatically generated. Wait a minimum of 1 ms before
  609. * read/write access to the PHY registers.
  610. */
  611. usleep_range(1000, 2000);
  612. ret = kszphy_config_reset(phydev);
  613. if (ret)
  614. return ret;
  615. /* Enable PHY Interrupts */
  616. if (phy_interrupt_is_valid(phydev)) {
  617. phydev->interrupts = PHY_INTERRUPT_ENABLED;
  618. if (phydev->drv->config_intr)
  619. phydev->drv->config_intr(phydev);
  620. }
  621. return 0;
  622. }
  623. static int kszphy_probe(struct phy_device *phydev)
  624. {
  625. const struct kszphy_type *type = phydev->drv->driver_data;
  626. const struct device_node *np = phydev->mdio.dev.of_node;
  627. struct kszphy_priv *priv;
  628. struct clk *clk;
  629. int ret;
  630. priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
  631. if (!priv)
  632. return -ENOMEM;
  633. phydev->priv = priv;
  634. priv->type = type;
  635. if (type->led_mode_reg) {
  636. ret = of_property_read_u32(np, "micrel,led-mode",
  637. &priv->led_mode);
  638. if (ret)
  639. priv->led_mode = -1;
  640. if (priv->led_mode > 3) {
  641. phydev_err(phydev, "invalid led mode: 0x%02x\n",
  642. priv->led_mode);
  643. priv->led_mode = -1;
  644. }
  645. } else {
  646. priv->led_mode = -1;
  647. }
  648. clk = devm_clk_get(&phydev->mdio.dev, "rmii-ref");
  649. /* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */
  650. if (!IS_ERR_OR_NULL(clk)) {
  651. unsigned long rate = clk_get_rate(clk);
  652. bool rmii_ref_clk_sel_25_mhz;
  653. priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel;
  654. rmii_ref_clk_sel_25_mhz = of_property_read_bool(np,
  655. "micrel,rmii-reference-clock-select-25-mhz");
  656. if (rate > 24500000 && rate < 25500000) {
  657. priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz;
  658. } else if (rate > 49500000 && rate < 50500000) {
  659. priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz;
  660. } else {
  661. phydev_err(phydev, "Clock rate out of range: %ld\n",
  662. rate);
  663. return -EINVAL;
  664. }
  665. }
  666. /* Support legacy board-file configuration */
  667. if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
  668. priv->rmii_ref_clk_sel = true;
  669. priv->rmii_ref_clk_sel_val = true;
  670. }
  671. return 0;
  672. }
  673. static struct phy_driver ksphy_driver[] = {
  674. {
  675. .phy_id = PHY_ID_KS8737,
  676. .phy_id_mask = MICREL_PHY_ID_MASK,
  677. .name = "Micrel KS8737",
  678. .features = PHY_BASIC_FEATURES,
  679. .flags = PHY_HAS_INTERRUPT,
  680. .driver_data = &ks8737_type,
  681. .config_init = kszphy_config_init,
  682. .ack_interrupt = kszphy_ack_interrupt,
  683. .config_intr = kszphy_config_intr,
  684. .suspend = genphy_suspend,
  685. .resume = genphy_resume,
  686. }, {
  687. .phy_id = PHY_ID_KSZ8021,
  688. .phy_id_mask = 0x00ffffff,
  689. .name = "Micrel KSZ8021 or KSZ8031",
  690. .features = PHY_BASIC_FEATURES,
  691. .flags = PHY_HAS_INTERRUPT,
  692. .driver_data = &ksz8021_type,
  693. .probe = kszphy_probe,
  694. .config_init = kszphy_config_init,
  695. .ack_interrupt = kszphy_ack_interrupt,
  696. .config_intr = kszphy_config_intr,
  697. .get_sset_count = kszphy_get_sset_count,
  698. .get_strings = kszphy_get_strings,
  699. .get_stats = kszphy_get_stats,
  700. .suspend = genphy_suspend,
  701. .resume = genphy_resume,
  702. }, {
  703. .phy_id = PHY_ID_KSZ8031,
  704. .phy_id_mask = 0x00ffffff,
  705. .name = "Micrel KSZ8031",
  706. .features = PHY_BASIC_FEATURES,
  707. .flags = PHY_HAS_INTERRUPT,
  708. .driver_data = &ksz8021_type,
  709. .probe = kszphy_probe,
  710. .config_init = kszphy_config_init,
  711. .ack_interrupt = kszphy_ack_interrupt,
  712. .config_intr = kszphy_config_intr,
  713. .get_sset_count = kszphy_get_sset_count,
  714. .get_strings = kszphy_get_strings,
  715. .get_stats = kszphy_get_stats,
  716. .suspend = genphy_suspend,
  717. .resume = genphy_resume,
  718. }, {
  719. .phy_id = PHY_ID_KSZ8041,
  720. .phy_id_mask = MICREL_PHY_ID_MASK,
  721. .name = "Micrel KSZ8041",
  722. .features = PHY_BASIC_FEATURES,
  723. .flags = PHY_HAS_INTERRUPT,
  724. .driver_data = &ksz8041_type,
  725. .probe = kszphy_probe,
  726. .config_init = ksz8041_config_init,
  727. .config_aneg = ksz8041_config_aneg,
  728. .ack_interrupt = kszphy_ack_interrupt,
  729. .config_intr = kszphy_config_intr,
  730. .get_sset_count = kszphy_get_sset_count,
  731. .get_strings = kszphy_get_strings,
  732. .get_stats = kszphy_get_stats,
  733. .suspend = genphy_suspend,
  734. .resume = genphy_resume,
  735. }, {
  736. .phy_id = PHY_ID_KSZ8041RNLI,
  737. .phy_id_mask = MICREL_PHY_ID_MASK,
  738. .name = "Micrel KSZ8041RNLI",
  739. .features = PHY_BASIC_FEATURES,
  740. .flags = PHY_HAS_INTERRUPT,
  741. .driver_data = &ksz8041_type,
  742. .probe = kszphy_probe,
  743. .config_init = kszphy_config_init,
  744. .ack_interrupt = kszphy_ack_interrupt,
  745. .config_intr = kszphy_config_intr,
  746. .get_sset_count = kszphy_get_sset_count,
  747. .get_strings = kszphy_get_strings,
  748. .get_stats = kszphy_get_stats,
  749. .suspend = genphy_suspend,
  750. .resume = genphy_resume,
  751. }, {
  752. .phy_id = PHY_ID_KSZ8051,
  753. .phy_id_mask = MICREL_PHY_ID_MASK,
  754. .name = "Micrel KSZ8051",
  755. .features = PHY_BASIC_FEATURES,
  756. .flags = PHY_HAS_INTERRUPT,
  757. .driver_data = &ksz8051_type,
  758. .probe = kszphy_probe,
  759. .config_init = kszphy_config_init,
  760. .ack_interrupt = kszphy_ack_interrupt,
  761. .config_intr = kszphy_config_intr,
  762. .get_sset_count = kszphy_get_sset_count,
  763. .get_strings = kszphy_get_strings,
  764. .get_stats = kszphy_get_stats,
  765. .suspend = genphy_suspend,
  766. .resume = genphy_resume,
  767. }, {
  768. .phy_id = PHY_ID_KSZ8001,
  769. .name = "Micrel KSZ8001 or KS8721",
  770. .phy_id_mask = 0x00fffffc,
  771. .features = PHY_BASIC_FEATURES,
  772. .flags = PHY_HAS_INTERRUPT,
  773. .driver_data = &ksz8041_type,
  774. .probe = kszphy_probe,
  775. .config_init = kszphy_config_init,
  776. .ack_interrupt = kszphy_ack_interrupt,
  777. .config_intr = kszphy_config_intr,
  778. .get_sset_count = kszphy_get_sset_count,
  779. .get_strings = kszphy_get_strings,
  780. .get_stats = kszphy_get_stats,
  781. .suspend = genphy_suspend,
  782. .resume = genphy_resume,
  783. }, {
  784. .phy_id = PHY_ID_KSZ8081,
  785. .name = "Micrel KSZ8081 or KSZ8091",
  786. .phy_id_mask = MICREL_PHY_ID_MASK,
  787. .features = PHY_BASIC_FEATURES,
  788. .flags = PHY_HAS_INTERRUPT,
  789. .driver_data = &ksz8081_type,
  790. .probe = kszphy_probe,
  791. .config_init = kszphy_config_init,
  792. .ack_interrupt = kszphy_ack_interrupt,
  793. .config_intr = kszphy_config_intr,
  794. .get_sset_count = kszphy_get_sset_count,
  795. .get_strings = kszphy_get_strings,
  796. .get_stats = kszphy_get_stats,
  797. .suspend = kszphy_suspend,
  798. .resume = kszphy_resume,
  799. }, {
  800. .phy_id = PHY_ID_KSZ8061,
  801. .name = "Micrel KSZ8061",
  802. .phy_id_mask = MICREL_PHY_ID_MASK,
  803. .features = PHY_BASIC_FEATURES,
  804. .flags = PHY_HAS_INTERRUPT,
  805. .config_init = ksz8061_config_init,
  806. .ack_interrupt = kszphy_ack_interrupt,
  807. .config_intr = kszphy_config_intr,
  808. .suspend = genphy_suspend,
  809. .resume = genphy_resume,
  810. }, {
  811. .phy_id = PHY_ID_KSZ9021,
  812. .phy_id_mask = 0x000ffffe,
  813. .name = "Micrel KSZ9021 Gigabit PHY",
  814. .features = PHY_GBIT_FEATURES,
  815. .flags = PHY_HAS_INTERRUPT,
  816. .driver_data = &ksz9021_type,
  817. .probe = kszphy_probe,
  818. .config_init = ksz9021_config_init,
  819. .ack_interrupt = kszphy_ack_interrupt,
  820. .config_intr = kszphy_config_intr,
  821. .get_sset_count = kszphy_get_sset_count,
  822. .get_strings = kszphy_get_strings,
  823. .get_stats = kszphy_get_stats,
  824. .suspend = genphy_suspend,
  825. .resume = genphy_resume,
  826. .read_mmd = genphy_read_mmd_unsupported,
  827. .write_mmd = genphy_write_mmd_unsupported,
  828. }, {
  829. .phy_id = PHY_ID_KSZ9031,
  830. .phy_id_mask = MICREL_PHY_ID_MASK,
  831. .name = "Micrel KSZ9031 Gigabit PHY",
  832. .features = PHY_GBIT_FEATURES,
  833. .flags = PHY_HAS_INTERRUPT,
  834. .driver_data = &ksz9021_type,
  835. .probe = kszphy_probe,
  836. .config_init = ksz9031_config_init,
  837. .soft_reset = genphy_soft_reset,
  838. .read_status = ksz9031_read_status,
  839. .ack_interrupt = kszphy_ack_interrupt,
  840. .config_intr = kszphy_config_intr,
  841. .get_sset_count = kszphy_get_sset_count,
  842. .get_strings = kszphy_get_strings,
  843. .get_stats = kszphy_get_stats,
  844. .suspend = genphy_suspend,
  845. .resume = kszphy_resume,
  846. }, {
  847. .phy_id = PHY_ID_KSZ8873MLL,
  848. .phy_id_mask = MICREL_PHY_ID_MASK,
  849. .name = "Micrel KSZ8873MLL Switch",
  850. .config_init = kszphy_config_init,
  851. .config_aneg = ksz8873mll_config_aneg,
  852. .read_status = ksz8873mll_read_status,
  853. .suspend = genphy_suspend,
  854. .resume = genphy_resume,
  855. }, {
  856. .phy_id = PHY_ID_KSZ886X,
  857. .phy_id_mask = MICREL_PHY_ID_MASK,
  858. .name = "Micrel KSZ886X Switch",
  859. .features = PHY_BASIC_FEATURES,
  860. .flags = PHY_HAS_INTERRUPT,
  861. .config_init = kszphy_config_init,
  862. .suspend = genphy_suspend,
  863. .resume = genphy_resume,
  864. }, {
  865. .phy_id = PHY_ID_KSZ8795,
  866. .phy_id_mask = MICREL_PHY_ID_MASK,
  867. .name = "Micrel KSZ8795",
  868. .features = PHY_BASIC_FEATURES,
  869. .flags = PHY_HAS_INTERRUPT,
  870. .config_init = kszphy_config_init,
  871. .config_aneg = ksz8873mll_config_aneg,
  872. .read_status = ksz8873mll_read_status,
  873. .suspend = genphy_suspend,
  874. .resume = genphy_resume,
  875. }, {
  876. .phy_id = PHY_ID_KSZ9477,
  877. .phy_id_mask = MICREL_PHY_ID_MASK,
  878. .name = "Microchip KSZ9477",
  879. .features = PHY_GBIT_FEATURES,
  880. .config_init = kszphy_config_init,
  881. .suspend = genphy_suspend,
  882. .resume = genphy_resume,
  883. } };
  884. module_phy_driver(ksphy_driver);
  885. MODULE_DESCRIPTION("Micrel PHY driver");
  886. MODULE_AUTHOR("David J. Choi");
  887. MODULE_LICENSE("GPL");
  888. static struct mdio_device_id __maybe_unused micrel_tbl[] = {
  889. { PHY_ID_KSZ9021, 0x000ffffe },
  890. { PHY_ID_KSZ9031, MICREL_PHY_ID_MASK },
  891. { PHY_ID_KSZ8001, 0x00fffffc },
  892. { PHY_ID_KS8737, MICREL_PHY_ID_MASK },
  893. { PHY_ID_KSZ8021, 0x00ffffff },
  894. { PHY_ID_KSZ8031, 0x00ffffff },
  895. { PHY_ID_KSZ8041, MICREL_PHY_ID_MASK },
  896. { PHY_ID_KSZ8051, MICREL_PHY_ID_MASK },
  897. { PHY_ID_KSZ8061, MICREL_PHY_ID_MASK },
  898. { PHY_ID_KSZ8081, MICREL_PHY_ID_MASK },
  899. { PHY_ID_KSZ8873MLL, MICREL_PHY_ID_MASK },
  900. { PHY_ID_KSZ886X, MICREL_PHY_ID_MASK },
  901. { }
  902. };
  903. MODULE_DEVICE_TABLE(mdio, micrel_tbl);