micrel.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/phy.h>
  27. #include <linux/micrel_phy.h>
  28. #include <linux/of.h>
  29. #include <linux/clk.h>
  30. #include <uapi/linux/mdio.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_type {
  68. u32 led_mode_reg;
  69. u16 interrupt_level_mask;
  70. bool has_broadcast_disable;
  71. bool has_nand_tree_disable;
  72. bool has_rmii_ref_clk_sel;
  73. };
  74. struct kszphy_priv {
  75. const struct kszphy_type *type;
  76. int led_mode;
  77. bool rmii_ref_clk_sel;
  78. bool rmii_ref_clk_sel_val;
  79. };
  80. static const struct kszphy_type ksz8021_type = {
  81. .led_mode_reg = MII_KSZPHY_CTRL_2,
  82. .has_broadcast_disable = true,
  83. .has_nand_tree_disable = true,
  84. .has_rmii_ref_clk_sel = true,
  85. };
  86. static const struct kszphy_type ksz8041_type = {
  87. .led_mode_reg = MII_KSZPHY_CTRL_1,
  88. };
  89. static const struct kszphy_type ksz8051_type = {
  90. .led_mode_reg = MII_KSZPHY_CTRL_2,
  91. .has_nand_tree_disable = true,
  92. };
  93. static const struct kszphy_type ksz8081_type = {
  94. .led_mode_reg = MII_KSZPHY_CTRL_2,
  95. .has_broadcast_disable = true,
  96. .has_nand_tree_disable = true,
  97. .has_rmii_ref_clk_sel = true,
  98. };
  99. static const struct kszphy_type ks8737_type = {
  100. .interrupt_level_mask = BIT(14),
  101. };
  102. static const struct kszphy_type ksz9021_type = {
  103. .interrupt_level_mask = BIT(14),
  104. };
  105. static int kszphy_extended_write(struct phy_device *phydev,
  106. u32 regnum, u16 val)
  107. {
  108. phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum);
  109. return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val);
  110. }
  111. static int kszphy_extended_read(struct phy_device *phydev,
  112. u32 regnum)
  113. {
  114. phy_write(phydev, MII_KSZPHY_EXTREG, regnum);
  115. return phy_read(phydev, MII_KSZPHY_EXTREG_READ);
  116. }
  117. static int kszphy_ack_interrupt(struct phy_device *phydev)
  118. {
  119. /* bit[7..0] int status, which is a read and clear register. */
  120. int rc;
  121. rc = phy_read(phydev, MII_KSZPHY_INTCS);
  122. return (rc < 0) ? rc : 0;
  123. }
  124. static int kszphy_config_intr(struct phy_device *phydev)
  125. {
  126. const struct kszphy_type *type = phydev->drv->driver_data;
  127. int temp;
  128. u16 mask;
  129. if (type && type->interrupt_level_mask)
  130. mask = type->interrupt_level_mask;
  131. else
  132. mask = KSZPHY_CTRL_INT_ACTIVE_HIGH;
  133. /* set the interrupt pin active low */
  134. temp = phy_read(phydev, MII_KSZPHY_CTRL);
  135. if (temp < 0)
  136. return temp;
  137. temp &= ~mask;
  138. phy_write(phydev, MII_KSZPHY_CTRL, temp);
  139. /* enable / disable interrupts */
  140. if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
  141. temp = KSZPHY_INTCS_ALL;
  142. else
  143. temp = 0;
  144. return phy_write(phydev, MII_KSZPHY_INTCS, temp);
  145. }
  146. static int kszphy_rmii_clk_sel(struct phy_device *phydev, bool val)
  147. {
  148. int ctrl;
  149. ctrl = phy_read(phydev, MII_KSZPHY_CTRL);
  150. if (ctrl < 0)
  151. return ctrl;
  152. if (val)
  153. ctrl |= KSZPHY_RMII_REF_CLK_SEL;
  154. else
  155. ctrl &= ~KSZPHY_RMII_REF_CLK_SEL;
  156. return phy_write(phydev, MII_KSZPHY_CTRL, ctrl);
  157. }
  158. static int kszphy_setup_led(struct phy_device *phydev, u32 reg, int val)
  159. {
  160. int rc, temp, shift;
  161. switch (reg) {
  162. case MII_KSZPHY_CTRL_1:
  163. shift = 14;
  164. break;
  165. case MII_KSZPHY_CTRL_2:
  166. shift = 4;
  167. break;
  168. default:
  169. return -EINVAL;
  170. }
  171. temp = phy_read(phydev, reg);
  172. if (temp < 0) {
  173. rc = temp;
  174. goto out;
  175. }
  176. temp &= ~(3 << shift);
  177. temp |= val << shift;
  178. rc = phy_write(phydev, reg, temp);
  179. out:
  180. if (rc < 0)
  181. dev_err(&phydev->dev, "failed to set led mode\n");
  182. return rc;
  183. }
  184. /* Disable PHY address 0 as the broadcast address, so that it can be used as a
  185. * unique (non-broadcast) address on a shared bus.
  186. */
  187. static int kszphy_broadcast_disable(struct phy_device *phydev)
  188. {
  189. int ret;
  190. ret = phy_read(phydev, MII_KSZPHY_OMSO);
  191. if (ret < 0)
  192. goto out;
  193. ret = phy_write(phydev, MII_KSZPHY_OMSO, ret | KSZPHY_OMSO_B_CAST_OFF);
  194. out:
  195. if (ret)
  196. dev_err(&phydev->dev, "failed to disable broadcast address\n");
  197. return ret;
  198. }
  199. static int kszphy_nand_tree_disable(struct phy_device *phydev)
  200. {
  201. int ret;
  202. ret = phy_read(phydev, MII_KSZPHY_OMSO);
  203. if (ret < 0)
  204. goto out;
  205. if (!(ret & KSZPHY_OMSO_NAND_TREE_ON))
  206. return 0;
  207. ret = phy_write(phydev, MII_KSZPHY_OMSO,
  208. ret & ~KSZPHY_OMSO_NAND_TREE_ON);
  209. out:
  210. if (ret)
  211. dev_err(&phydev->dev, "failed to disable NAND tree mode\n");
  212. return ret;
  213. }
  214. static int kszphy_config_init(struct phy_device *phydev)
  215. {
  216. struct kszphy_priv *priv = phydev->priv;
  217. const struct kszphy_type *type;
  218. int ret;
  219. if (!priv)
  220. return 0;
  221. type = priv->type;
  222. if (type->has_broadcast_disable)
  223. kszphy_broadcast_disable(phydev);
  224. if (type->has_nand_tree_disable)
  225. kszphy_nand_tree_disable(phydev);
  226. if (priv->rmii_ref_clk_sel) {
  227. ret = kszphy_rmii_clk_sel(phydev, priv->rmii_ref_clk_sel_val);
  228. if (ret) {
  229. dev_err(&phydev->dev, "failed to set rmii reference clock\n");
  230. return ret;
  231. }
  232. }
  233. if (priv->led_mode >= 0)
  234. kszphy_setup_led(phydev, type->led_mode_reg, priv->led_mode);
  235. return 0;
  236. }
  237. static int ksz8061_config_init(struct phy_device *phydev)
  238. {
  239. int ret;
  240. ret = phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_DEVID1, 0xB61A);
  241. if (ret)
  242. return ret;
  243. return kszphy_config_init(phydev);
  244. }
  245. static int ksz9021_load_values_from_of(struct phy_device *phydev,
  246. const struct device_node *of_node,
  247. u16 reg,
  248. const char *field1, const char *field2,
  249. const char *field3, const char *field4)
  250. {
  251. int val1 = -1;
  252. int val2 = -2;
  253. int val3 = -3;
  254. int val4 = -4;
  255. int newval;
  256. int matches = 0;
  257. if (!of_property_read_u32(of_node, field1, &val1))
  258. matches++;
  259. if (!of_property_read_u32(of_node, field2, &val2))
  260. matches++;
  261. if (!of_property_read_u32(of_node, field3, &val3))
  262. matches++;
  263. if (!of_property_read_u32(of_node, field4, &val4))
  264. matches++;
  265. if (!matches)
  266. return 0;
  267. if (matches < 4)
  268. newval = kszphy_extended_read(phydev, reg);
  269. else
  270. newval = 0;
  271. if (val1 != -1)
  272. newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0);
  273. if (val2 != -2)
  274. newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4);
  275. if (val3 != -3)
  276. newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8);
  277. if (val4 != -4)
  278. newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12);
  279. return kszphy_extended_write(phydev, reg, newval);
  280. }
  281. static int ksz9021_config_init(struct phy_device *phydev)
  282. {
  283. const struct device *dev = &phydev->dev;
  284. const struct device_node *of_node = dev->of_node;
  285. const struct device *dev_walker;
  286. /* The Micrel driver has a deprecated option to place phy OF
  287. * properties in the MAC node. Walk up the tree of devices to
  288. * find a device with an OF node.
  289. */
  290. dev_walker = &phydev->dev;
  291. do {
  292. of_node = dev_walker->of_node;
  293. dev_walker = dev_walker->parent;
  294. } while (!of_node && dev_walker);
  295. if (of_node) {
  296. ksz9021_load_values_from_of(phydev, of_node,
  297. MII_KSZPHY_CLK_CONTROL_PAD_SKEW,
  298. "txen-skew-ps", "txc-skew-ps",
  299. "rxdv-skew-ps", "rxc-skew-ps");
  300. ksz9021_load_values_from_of(phydev, of_node,
  301. MII_KSZPHY_RX_DATA_PAD_SKEW,
  302. "rxd0-skew-ps", "rxd1-skew-ps",
  303. "rxd2-skew-ps", "rxd3-skew-ps");
  304. ksz9021_load_values_from_of(phydev, of_node,
  305. MII_KSZPHY_TX_DATA_PAD_SKEW,
  306. "txd0-skew-ps", "txd1-skew-ps",
  307. "txd2-skew-ps", "txd3-skew-ps");
  308. }
  309. return 0;
  310. }
  311. #define MII_KSZ9031RN_MMD_CTRL_REG 0x0d
  312. #define MII_KSZ9031RN_MMD_REGDATA_REG 0x0e
  313. #define OP_DATA 1
  314. #define KSZ9031_PS_TO_REG 60
  315. /* Extended registers */
  316. /* MMD Address 0x0 */
  317. #define MII_KSZ9031RN_FLP_BURST_TX_LO 3
  318. #define MII_KSZ9031RN_FLP_BURST_TX_HI 4
  319. /* MMD Address 0x2 */
  320. #define MII_KSZ9031RN_CONTROL_PAD_SKEW 4
  321. #define MII_KSZ9031RN_RX_DATA_PAD_SKEW 5
  322. #define MII_KSZ9031RN_TX_DATA_PAD_SKEW 6
  323. #define MII_KSZ9031RN_CLK_PAD_SKEW 8
  324. static int ksz9031_extended_write(struct phy_device *phydev,
  325. u8 mode, u32 dev_addr, u32 regnum, u16 val)
  326. {
  327. phy_write(phydev, MII_KSZ9031RN_MMD_CTRL_REG, dev_addr);
  328. phy_write(phydev, MII_KSZ9031RN_MMD_REGDATA_REG, regnum);
  329. phy_write(phydev, MII_KSZ9031RN_MMD_CTRL_REG, (mode << 14) | dev_addr);
  330. return phy_write(phydev, MII_KSZ9031RN_MMD_REGDATA_REG, val);
  331. }
  332. static int ksz9031_extended_read(struct phy_device *phydev,
  333. u8 mode, u32 dev_addr, u32 regnum)
  334. {
  335. phy_write(phydev, MII_KSZ9031RN_MMD_CTRL_REG, dev_addr);
  336. phy_write(phydev, MII_KSZ9031RN_MMD_REGDATA_REG, regnum);
  337. phy_write(phydev, MII_KSZ9031RN_MMD_CTRL_REG, (mode << 14) | dev_addr);
  338. return phy_read(phydev, MII_KSZ9031RN_MMD_REGDATA_REG);
  339. }
  340. static int ksz9031_of_load_skew_values(struct phy_device *phydev,
  341. const struct device_node *of_node,
  342. u16 reg, size_t field_sz,
  343. const char *field[], u8 numfields)
  344. {
  345. int val[4] = {-1, -2, -3, -4};
  346. int matches = 0;
  347. u16 mask;
  348. u16 maxval;
  349. u16 newval;
  350. int i;
  351. for (i = 0; i < numfields; i++)
  352. if (!of_property_read_u32(of_node, field[i], val + i))
  353. matches++;
  354. if (!matches)
  355. return 0;
  356. if (matches < numfields)
  357. newval = ksz9031_extended_read(phydev, OP_DATA, 2, reg);
  358. else
  359. newval = 0;
  360. maxval = (field_sz == 4) ? 0xf : 0x1f;
  361. for (i = 0; i < numfields; i++)
  362. if (val[i] != -(i + 1)) {
  363. mask = 0xffff;
  364. mask ^= maxval << (field_sz * i);
  365. newval = (newval & mask) |
  366. (((val[i] / KSZ9031_PS_TO_REG) & maxval)
  367. << (field_sz * i));
  368. }
  369. return ksz9031_extended_write(phydev, OP_DATA, 2, reg, newval);
  370. }
  371. static int ksz9031_center_flp_timing(struct phy_device *phydev)
  372. {
  373. int result;
  374. /* Center KSZ9031RNX FLP timing at 16ms. */
  375. result = ksz9031_extended_write(phydev, OP_DATA, 0,
  376. MII_KSZ9031RN_FLP_BURST_TX_HI, 0x0006);
  377. result = ksz9031_extended_write(phydev, OP_DATA, 0,
  378. MII_KSZ9031RN_FLP_BURST_TX_LO, 0x1A80);
  379. if (result)
  380. return result;
  381. return genphy_restart_aneg(phydev);
  382. }
  383. static int ksz9031_config_init(struct phy_device *phydev)
  384. {
  385. const struct device *dev = &phydev->dev;
  386. const struct device_node *of_node = dev->of_node;
  387. static const char *clk_skews[2] = {"rxc-skew-ps", "txc-skew-ps"};
  388. static const char *rx_data_skews[4] = {
  389. "rxd0-skew-ps", "rxd1-skew-ps",
  390. "rxd2-skew-ps", "rxd3-skew-ps"
  391. };
  392. static const char *tx_data_skews[4] = {
  393. "txd0-skew-ps", "txd1-skew-ps",
  394. "txd2-skew-ps", "txd3-skew-ps"
  395. };
  396. static const char *control_skews[2] = {"txen-skew-ps", "rxdv-skew-ps"};
  397. if (!of_node && dev->parent->of_node)
  398. of_node = dev->parent->of_node;
  399. if (of_node) {
  400. ksz9031_of_load_skew_values(phydev, of_node,
  401. MII_KSZ9031RN_CLK_PAD_SKEW, 5,
  402. clk_skews, 2);
  403. ksz9031_of_load_skew_values(phydev, of_node,
  404. MII_KSZ9031RN_CONTROL_PAD_SKEW, 4,
  405. control_skews, 2);
  406. ksz9031_of_load_skew_values(phydev, of_node,
  407. MII_KSZ9031RN_RX_DATA_PAD_SKEW, 4,
  408. rx_data_skews, 4);
  409. ksz9031_of_load_skew_values(phydev, of_node,
  410. MII_KSZ9031RN_TX_DATA_PAD_SKEW, 4,
  411. tx_data_skews, 4);
  412. }
  413. return ksz9031_center_flp_timing(phydev);
  414. }
  415. #define KSZ8873MLL_GLOBAL_CONTROL_4 0x06
  416. #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX BIT(6)
  417. #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED BIT(4)
  418. static int ksz8873mll_read_status(struct phy_device *phydev)
  419. {
  420. int regval;
  421. /* dummy read */
  422. regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
  423. regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
  424. if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX)
  425. phydev->duplex = DUPLEX_HALF;
  426. else
  427. phydev->duplex = DUPLEX_FULL;
  428. if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
  429. phydev->speed = SPEED_10;
  430. else
  431. phydev->speed = SPEED_100;
  432. phydev->link = 1;
  433. phydev->pause = phydev->asym_pause = 0;
  434. return 0;
  435. }
  436. static int ksz9031_read_status(struct phy_device *phydev)
  437. {
  438. int err;
  439. int regval;
  440. err = genphy_read_status(phydev);
  441. if (err)
  442. return err;
  443. /* Make sure the PHY is not broken. Read idle error count,
  444. * and reset the PHY if it is maxed out.
  445. */
  446. regval = phy_read(phydev, MII_STAT1000);
  447. if ((regval & 0xFF) == 0xFF) {
  448. phy_init_hw(phydev);
  449. phydev->link = 0;
  450. if (phydev->drv->config_intr && phy_interrupt_is_valid(phydev))
  451. phydev->drv->config_intr(phydev);
  452. return genphy_config_aneg(phydev);
  453. }
  454. return 0;
  455. }
  456. static int ksz8873mll_config_aneg(struct phy_device *phydev)
  457. {
  458. return 0;
  459. }
  460. /* This routine returns -1 as an indication to the caller that the
  461. * Micrel ksz9021 10/100/1000 PHY does not support standard IEEE
  462. * MMD extended PHY registers.
  463. */
  464. static int
  465. ksz9021_rd_mmd_phyreg(struct phy_device *phydev, int ptrad, int devnum,
  466. int regnum)
  467. {
  468. return -1;
  469. }
  470. /* This routine does nothing since the Micrel ksz9021 does not support
  471. * standard IEEE MMD extended PHY registers.
  472. */
  473. static void
  474. ksz9021_wr_mmd_phyreg(struct phy_device *phydev, int ptrad, int devnum,
  475. int regnum, u32 val)
  476. {
  477. }
  478. static int kszphy_probe(struct phy_device *phydev)
  479. {
  480. const struct kszphy_type *type = phydev->drv->driver_data;
  481. const struct device_node *np = phydev->dev.of_node;
  482. struct kszphy_priv *priv;
  483. struct clk *clk;
  484. int ret;
  485. priv = devm_kzalloc(&phydev->dev, sizeof(*priv), GFP_KERNEL);
  486. if (!priv)
  487. return -ENOMEM;
  488. phydev->priv = priv;
  489. priv->type = type;
  490. if (type->led_mode_reg) {
  491. ret = of_property_read_u32(np, "micrel,led-mode",
  492. &priv->led_mode);
  493. if (ret)
  494. priv->led_mode = -1;
  495. if (priv->led_mode > 3) {
  496. dev_err(&phydev->dev, "invalid led mode: 0x%02x\n",
  497. priv->led_mode);
  498. priv->led_mode = -1;
  499. }
  500. } else {
  501. priv->led_mode = -1;
  502. }
  503. clk = devm_clk_get(&phydev->dev, "rmii-ref");
  504. /* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */
  505. if (!IS_ERR_OR_NULL(clk)) {
  506. unsigned long rate = clk_get_rate(clk);
  507. bool rmii_ref_clk_sel_25_mhz;
  508. priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel;
  509. rmii_ref_clk_sel_25_mhz = of_property_read_bool(np,
  510. "micrel,rmii-reference-clock-select-25-mhz");
  511. if (rate > 24500000 && rate < 25500000) {
  512. priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz;
  513. } else if (rate > 49500000 && rate < 50500000) {
  514. priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz;
  515. } else {
  516. dev_err(&phydev->dev, "Clock rate out of range: %ld\n", rate);
  517. return -EINVAL;
  518. }
  519. }
  520. /* Support legacy board-file configuration */
  521. if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
  522. priv->rmii_ref_clk_sel = true;
  523. priv->rmii_ref_clk_sel_val = true;
  524. }
  525. return 0;
  526. }
  527. static struct phy_driver ksphy_driver[] = {
  528. {
  529. .phy_id = PHY_ID_KS8737,
  530. .phy_id_mask = 0x00fffff0,
  531. .name = "Micrel KS8737",
  532. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  533. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  534. .driver_data = &ks8737_type,
  535. .config_init = kszphy_config_init,
  536. .config_aneg = genphy_config_aneg,
  537. .read_status = genphy_read_status,
  538. .ack_interrupt = kszphy_ack_interrupt,
  539. .config_intr = kszphy_config_intr,
  540. .suspend = genphy_suspend,
  541. .resume = genphy_resume,
  542. .driver = { .owner = THIS_MODULE,},
  543. }, {
  544. .phy_id = PHY_ID_KSZ8021,
  545. .phy_id_mask = 0x00ffffff,
  546. .name = "Micrel KSZ8021 or KSZ8031",
  547. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause |
  548. SUPPORTED_Asym_Pause),
  549. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  550. .driver_data = &ksz8021_type,
  551. .probe = kszphy_probe,
  552. .config_init = kszphy_config_init,
  553. .config_aneg = genphy_config_aneg,
  554. .read_status = genphy_read_status,
  555. .ack_interrupt = kszphy_ack_interrupt,
  556. .config_intr = kszphy_config_intr,
  557. .suspend = genphy_suspend,
  558. .resume = genphy_resume,
  559. .driver = { .owner = THIS_MODULE,},
  560. }, {
  561. .phy_id = PHY_ID_KSZ8031,
  562. .phy_id_mask = 0x00ffffff,
  563. .name = "Micrel KSZ8031",
  564. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause |
  565. SUPPORTED_Asym_Pause),
  566. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  567. .driver_data = &ksz8021_type,
  568. .probe = kszphy_probe,
  569. .config_init = kszphy_config_init,
  570. .config_aneg = genphy_config_aneg,
  571. .read_status = genphy_read_status,
  572. .ack_interrupt = kszphy_ack_interrupt,
  573. .config_intr = kszphy_config_intr,
  574. .suspend = genphy_suspend,
  575. .resume = genphy_resume,
  576. .driver = { .owner = THIS_MODULE,},
  577. }, {
  578. .phy_id = PHY_ID_KSZ8041,
  579. .phy_id_mask = 0x00fffff0,
  580. .name = "Micrel KSZ8041",
  581. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  582. | SUPPORTED_Asym_Pause),
  583. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  584. .driver_data = &ksz8041_type,
  585. .probe = kszphy_probe,
  586. .config_init = kszphy_config_init,
  587. .config_aneg = genphy_config_aneg,
  588. .read_status = genphy_read_status,
  589. .ack_interrupt = kszphy_ack_interrupt,
  590. .config_intr = kszphy_config_intr,
  591. .suspend = genphy_suspend,
  592. .resume = genphy_resume,
  593. .driver = { .owner = THIS_MODULE,},
  594. }, {
  595. .phy_id = PHY_ID_KSZ8041RNLI,
  596. .phy_id_mask = 0x00fffff0,
  597. .name = "Micrel KSZ8041RNLI",
  598. .features = PHY_BASIC_FEATURES |
  599. SUPPORTED_Pause | SUPPORTED_Asym_Pause,
  600. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  601. .driver_data = &ksz8041_type,
  602. .probe = kszphy_probe,
  603. .config_init = kszphy_config_init,
  604. .config_aneg = genphy_config_aneg,
  605. .read_status = genphy_read_status,
  606. .ack_interrupt = kszphy_ack_interrupt,
  607. .config_intr = kszphy_config_intr,
  608. .suspend = genphy_suspend,
  609. .resume = genphy_resume,
  610. .driver = { .owner = THIS_MODULE,},
  611. }, {
  612. .phy_id = PHY_ID_KSZ8051,
  613. .phy_id_mask = 0x00fffff0,
  614. .name = "Micrel KSZ8051",
  615. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  616. | SUPPORTED_Asym_Pause),
  617. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  618. .driver_data = &ksz8051_type,
  619. .probe = kszphy_probe,
  620. .config_init = kszphy_config_init,
  621. .config_aneg = genphy_config_aneg,
  622. .read_status = genphy_read_status,
  623. .ack_interrupt = kszphy_ack_interrupt,
  624. .config_intr = kszphy_config_intr,
  625. .suspend = genphy_suspend,
  626. .resume = genphy_resume,
  627. .driver = { .owner = THIS_MODULE,},
  628. }, {
  629. .phy_id = PHY_ID_KSZ8001,
  630. .name = "Micrel KSZ8001 or KS8721",
  631. .phy_id_mask = 0x00ffffff,
  632. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  633. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  634. .driver_data = &ksz8041_type,
  635. .probe = kszphy_probe,
  636. .config_init = kszphy_config_init,
  637. .config_aneg = genphy_config_aneg,
  638. .read_status = genphy_read_status,
  639. .ack_interrupt = kszphy_ack_interrupt,
  640. .config_intr = kszphy_config_intr,
  641. .suspend = genphy_suspend,
  642. .resume = genphy_resume,
  643. .driver = { .owner = THIS_MODULE,},
  644. }, {
  645. .phy_id = PHY_ID_KSZ8081,
  646. .name = "Micrel KSZ8081 or KSZ8091",
  647. .phy_id_mask = 0x00fffff0,
  648. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  649. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  650. .driver_data = &ksz8081_type,
  651. .probe = kszphy_probe,
  652. .config_init = kszphy_config_init,
  653. .config_aneg = genphy_config_aneg,
  654. .read_status = genphy_read_status,
  655. .ack_interrupt = kszphy_ack_interrupt,
  656. .config_intr = kszphy_config_intr,
  657. .suspend = genphy_suspend,
  658. .resume = genphy_resume,
  659. .driver = { .owner = THIS_MODULE,},
  660. }, {
  661. .phy_id = PHY_ID_KSZ8061,
  662. .name = "Micrel KSZ8061",
  663. .phy_id_mask = 0x00fffff0,
  664. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  665. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  666. .config_init = ksz8061_config_init,
  667. .config_aneg = genphy_config_aneg,
  668. .read_status = genphy_read_status,
  669. .ack_interrupt = kszphy_ack_interrupt,
  670. .config_intr = kszphy_config_intr,
  671. .suspend = genphy_suspend,
  672. .resume = genphy_resume,
  673. .driver = { .owner = THIS_MODULE,},
  674. }, {
  675. .phy_id = PHY_ID_KSZ9021,
  676. .phy_id_mask = 0x000ffffe,
  677. .name = "Micrel KSZ9021 Gigabit PHY",
  678. .features = (PHY_GBIT_FEATURES | SUPPORTED_Pause),
  679. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  680. .driver_data = &ksz9021_type,
  681. .config_init = ksz9021_config_init,
  682. .config_aneg = genphy_config_aneg,
  683. .read_status = genphy_read_status,
  684. .ack_interrupt = kszphy_ack_interrupt,
  685. .config_intr = kszphy_config_intr,
  686. .suspend = genphy_suspend,
  687. .resume = genphy_resume,
  688. .read_mmd_indirect = ksz9021_rd_mmd_phyreg,
  689. .write_mmd_indirect = ksz9021_wr_mmd_phyreg,
  690. .driver = { .owner = THIS_MODULE, },
  691. }, {
  692. .phy_id = PHY_ID_KSZ9031,
  693. .phy_id_mask = 0x00fffff0,
  694. .name = "Micrel KSZ9031 Gigabit PHY",
  695. .features = (PHY_GBIT_FEATURES | SUPPORTED_Pause),
  696. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  697. .driver_data = &ksz9021_type,
  698. .config_init = ksz9031_config_init,
  699. .config_aneg = genphy_config_aneg,
  700. .read_status = ksz9031_read_status,
  701. .ack_interrupt = kszphy_ack_interrupt,
  702. .config_intr = kszphy_config_intr,
  703. .suspend = genphy_suspend,
  704. .resume = genphy_resume,
  705. .driver = { .owner = THIS_MODULE, },
  706. }, {
  707. .phy_id = PHY_ID_KSZ8873MLL,
  708. .phy_id_mask = 0x00fffff0,
  709. .name = "Micrel KSZ8873MLL Switch",
  710. .features = (SUPPORTED_Pause | SUPPORTED_Asym_Pause),
  711. .flags = PHY_HAS_MAGICANEG,
  712. .config_init = kszphy_config_init,
  713. .config_aneg = ksz8873mll_config_aneg,
  714. .read_status = ksz8873mll_read_status,
  715. .suspend = genphy_suspend,
  716. .resume = genphy_resume,
  717. .driver = { .owner = THIS_MODULE, },
  718. }, {
  719. .phy_id = PHY_ID_KSZ886X,
  720. .phy_id_mask = 0x00fffff0,
  721. .name = "Micrel KSZ886X Switch",
  722. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  723. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  724. .config_init = kszphy_config_init,
  725. .config_aneg = genphy_config_aneg,
  726. .read_status = genphy_read_status,
  727. .suspend = genphy_suspend,
  728. .resume = genphy_resume,
  729. .driver = { .owner = THIS_MODULE, },
  730. } };
  731. module_phy_driver(ksphy_driver);
  732. MODULE_DESCRIPTION("Micrel PHY driver");
  733. MODULE_AUTHOR("David J. Choi");
  734. MODULE_LICENSE("GPL");
  735. static struct mdio_device_id __maybe_unused micrel_tbl[] = {
  736. { PHY_ID_KSZ9021, 0x000ffffe },
  737. { PHY_ID_KSZ9031, 0x00fffff0 },
  738. { PHY_ID_KSZ8001, 0x00ffffff },
  739. { PHY_ID_KS8737, 0x00fffff0 },
  740. { PHY_ID_KSZ8021, 0x00ffffff },
  741. { PHY_ID_KSZ8031, 0x00ffffff },
  742. { PHY_ID_KSZ8041, 0x00fffff0 },
  743. { PHY_ID_KSZ8051, 0x00fffff0 },
  744. { PHY_ID_KSZ8061, 0x00fffff0 },
  745. { PHY_ID_KSZ8081, 0x00fffff0 },
  746. { PHY_ID_KSZ8873MLL, 0x00fffff0 },
  747. { PHY_ID_KSZ886X, 0x00fffff0 },
  748. { }
  749. };
  750. MODULE_DEVICE_TABLE(mdio, micrel_tbl);