micrel.c 27 KB

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