micrel.c 27 KB

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