micrel.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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. return genphy_config_aneg(phydev);
  513. }
  514. return 0;
  515. }
  516. static int ksz8873mll_config_aneg(struct phy_device *phydev)
  517. {
  518. return 0;
  519. }
  520. /* This routine returns -1 as an indication to the caller that the
  521. * Micrel ksz9021 10/100/1000 PHY does not support standard IEEE
  522. * MMD extended PHY registers.
  523. */
  524. static int
  525. ksz9021_rd_mmd_phyreg(struct phy_device *phydev, int ptrad, int devnum,
  526. int regnum)
  527. {
  528. return -1;
  529. }
  530. /* This routine does nothing since the Micrel ksz9021 does not support
  531. * standard IEEE MMD extended PHY registers.
  532. */
  533. static void
  534. ksz9021_wr_mmd_phyreg(struct phy_device *phydev, int ptrad, int devnum,
  535. int regnum, u32 val)
  536. {
  537. }
  538. static int kszphy_get_sset_count(struct phy_device *phydev)
  539. {
  540. return ARRAY_SIZE(kszphy_hw_stats);
  541. }
  542. static void kszphy_get_strings(struct phy_device *phydev, u8 *data)
  543. {
  544. int i;
  545. for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++) {
  546. memcpy(data + i * ETH_GSTRING_LEN,
  547. kszphy_hw_stats[i].string, ETH_GSTRING_LEN);
  548. }
  549. }
  550. #ifndef UINT64_MAX
  551. #define UINT64_MAX (u64)(~((u64)0))
  552. #endif
  553. static u64 kszphy_get_stat(struct phy_device *phydev, int i)
  554. {
  555. struct kszphy_hw_stat stat = kszphy_hw_stats[i];
  556. struct kszphy_priv *priv = phydev->priv;
  557. int val;
  558. u64 ret;
  559. val = phy_read(phydev, stat.reg);
  560. if (val < 0) {
  561. ret = UINT64_MAX;
  562. } else {
  563. val = val & ((1 << stat.bits) - 1);
  564. priv->stats[i] += val;
  565. ret = priv->stats[i];
  566. }
  567. return ret;
  568. }
  569. static void kszphy_get_stats(struct phy_device *phydev,
  570. struct ethtool_stats *stats, u64 *data)
  571. {
  572. int i;
  573. for (i = 0; i < ARRAY_SIZE(kszphy_hw_stats); i++)
  574. data[i] = kszphy_get_stat(phydev, i);
  575. }
  576. static int kszphy_suspend(struct phy_device *phydev)
  577. {
  578. /* Disable PHY Interrupts */
  579. if (phy_interrupt_is_valid(phydev)) {
  580. phydev->interrupts = PHY_INTERRUPT_DISABLED;
  581. if (phydev->drv->config_intr)
  582. phydev->drv->config_intr(phydev);
  583. }
  584. return genphy_suspend(phydev);
  585. }
  586. static int kszphy_resume(struct phy_device *phydev)
  587. {
  588. genphy_resume(phydev);
  589. /* Enable PHY Interrupts */
  590. if (phy_interrupt_is_valid(phydev)) {
  591. phydev->interrupts = PHY_INTERRUPT_ENABLED;
  592. if (phydev->drv->config_intr)
  593. phydev->drv->config_intr(phydev);
  594. }
  595. return 0;
  596. }
  597. static int kszphy_probe(struct phy_device *phydev)
  598. {
  599. const struct kszphy_type *type = phydev->drv->driver_data;
  600. const struct device_node *np = phydev->mdio.dev.of_node;
  601. struct kszphy_priv *priv;
  602. struct clk *clk;
  603. int ret;
  604. priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
  605. if (!priv)
  606. return -ENOMEM;
  607. phydev->priv = priv;
  608. priv->type = type;
  609. if (type->led_mode_reg) {
  610. ret = of_property_read_u32(np, "micrel,led-mode",
  611. &priv->led_mode);
  612. if (ret)
  613. priv->led_mode = -1;
  614. if (priv->led_mode > 3) {
  615. phydev_err(phydev, "invalid led mode: 0x%02x\n",
  616. priv->led_mode);
  617. priv->led_mode = -1;
  618. }
  619. } else {
  620. priv->led_mode = -1;
  621. }
  622. clk = devm_clk_get(&phydev->mdio.dev, "rmii-ref");
  623. /* NOTE: clk may be NULL if building without CONFIG_HAVE_CLK */
  624. if (!IS_ERR_OR_NULL(clk)) {
  625. unsigned long rate = clk_get_rate(clk);
  626. bool rmii_ref_clk_sel_25_mhz;
  627. priv->rmii_ref_clk_sel = type->has_rmii_ref_clk_sel;
  628. rmii_ref_clk_sel_25_mhz = of_property_read_bool(np,
  629. "micrel,rmii-reference-clock-select-25-mhz");
  630. if (rate > 24500000 && rate < 25500000) {
  631. priv->rmii_ref_clk_sel_val = rmii_ref_clk_sel_25_mhz;
  632. } else if (rate > 49500000 && rate < 50500000) {
  633. priv->rmii_ref_clk_sel_val = !rmii_ref_clk_sel_25_mhz;
  634. } else {
  635. phydev_err(phydev, "Clock rate out of range: %ld\n",
  636. rate);
  637. return -EINVAL;
  638. }
  639. }
  640. /* Support legacy board-file configuration */
  641. if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
  642. priv->rmii_ref_clk_sel = true;
  643. priv->rmii_ref_clk_sel_val = true;
  644. }
  645. return 0;
  646. }
  647. static struct phy_driver ksphy_driver[] = {
  648. {
  649. .phy_id = PHY_ID_KS8737,
  650. .phy_id_mask = MICREL_PHY_ID_MASK,
  651. .name = "Micrel KS8737",
  652. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  653. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  654. .driver_data = &ks8737_type,
  655. .config_init = kszphy_config_init,
  656. .config_aneg = genphy_config_aneg,
  657. .read_status = genphy_read_status,
  658. .ack_interrupt = kszphy_ack_interrupt,
  659. .config_intr = kszphy_config_intr,
  660. .suspend = genphy_suspend,
  661. .resume = genphy_resume,
  662. }, {
  663. .phy_id = PHY_ID_KSZ8021,
  664. .phy_id_mask = 0x00ffffff,
  665. .name = "Micrel KSZ8021 or KSZ8031",
  666. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause |
  667. SUPPORTED_Asym_Pause),
  668. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  669. .driver_data = &ksz8021_type,
  670. .probe = kszphy_probe,
  671. .config_init = kszphy_config_init,
  672. .config_aneg = genphy_config_aneg,
  673. .read_status = genphy_read_status,
  674. .ack_interrupt = kszphy_ack_interrupt,
  675. .config_intr = kszphy_config_intr,
  676. .get_sset_count = kszphy_get_sset_count,
  677. .get_strings = kszphy_get_strings,
  678. .get_stats = kszphy_get_stats,
  679. .suspend = genphy_suspend,
  680. .resume = genphy_resume,
  681. }, {
  682. .phy_id = PHY_ID_KSZ8031,
  683. .phy_id_mask = 0x00ffffff,
  684. .name = "Micrel KSZ8031",
  685. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause |
  686. SUPPORTED_Asym_Pause),
  687. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  688. .driver_data = &ksz8021_type,
  689. .probe = kszphy_probe,
  690. .config_init = kszphy_config_init,
  691. .config_aneg = genphy_config_aneg,
  692. .read_status = genphy_read_status,
  693. .ack_interrupt = kszphy_ack_interrupt,
  694. .config_intr = kszphy_config_intr,
  695. .get_sset_count = kszphy_get_sset_count,
  696. .get_strings = kszphy_get_strings,
  697. .get_stats = kszphy_get_stats,
  698. .suspend = genphy_suspend,
  699. .resume = genphy_resume,
  700. }, {
  701. .phy_id = PHY_ID_KSZ8041,
  702. .phy_id_mask = MICREL_PHY_ID_MASK,
  703. .name = "Micrel KSZ8041",
  704. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  705. | SUPPORTED_Asym_Pause),
  706. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  707. .driver_data = &ksz8041_type,
  708. .probe = kszphy_probe,
  709. .config_init = ksz8041_config_init,
  710. .config_aneg = ksz8041_config_aneg,
  711. .read_status = genphy_read_status,
  712. .ack_interrupt = kszphy_ack_interrupt,
  713. .config_intr = kszphy_config_intr,
  714. .get_sset_count = kszphy_get_sset_count,
  715. .get_strings = kszphy_get_strings,
  716. .get_stats = kszphy_get_stats,
  717. .suspend = genphy_suspend,
  718. .resume = genphy_resume,
  719. }, {
  720. .phy_id = PHY_ID_KSZ8041RNLI,
  721. .phy_id_mask = MICREL_PHY_ID_MASK,
  722. .name = "Micrel KSZ8041RNLI",
  723. .features = PHY_BASIC_FEATURES |
  724. SUPPORTED_Pause | SUPPORTED_Asym_Pause,
  725. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  726. .driver_data = &ksz8041_type,
  727. .probe = kszphy_probe,
  728. .config_init = kszphy_config_init,
  729. .config_aneg = genphy_config_aneg,
  730. .read_status = genphy_read_status,
  731. .ack_interrupt = kszphy_ack_interrupt,
  732. .config_intr = kszphy_config_intr,
  733. .get_sset_count = kszphy_get_sset_count,
  734. .get_strings = kszphy_get_strings,
  735. .get_stats = kszphy_get_stats,
  736. .suspend = genphy_suspend,
  737. .resume = genphy_resume,
  738. }, {
  739. .phy_id = PHY_ID_KSZ8051,
  740. .phy_id_mask = MICREL_PHY_ID_MASK,
  741. .name = "Micrel KSZ8051",
  742. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  743. | SUPPORTED_Asym_Pause),
  744. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  745. .driver_data = &ksz8051_type,
  746. .probe = kszphy_probe,
  747. .config_init = kszphy_config_init,
  748. .config_aneg = genphy_config_aneg,
  749. .read_status = genphy_read_status,
  750. .ack_interrupt = kszphy_ack_interrupt,
  751. .config_intr = kszphy_config_intr,
  752. .get_sset_count = kszphy_get_sset_count,
  753. .get_strings = kszphy_get_strings,
  754. .get_stats = kszphy_get_stats,
  755. .suspend = genphy_suspend,
  756. .resume = genphy_resume,
  757. }, {
  758. .phy_id = PHY_ID_KSZ8001,
  759. .name = "Micrel KSZ8001 or KS8721",
  760. .phy_id_mask = 0x00fffffc,
  761. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  762. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  763. .driver_data = &ksz8041_type,
  764. .probe = kszphy_probe,
  765. .config_init = kszphy_config_init,
  766. .config_aneg = genphy_config_aneg,
  767. .read_status = genphy_read_status,
  768. .ack_interrupt = kszphy_ack_interrupt,
  769. .config_intr = kszphy_config_intr,
  770. .get_sset_count = kszphy_get_sset_count,
  771. .get_strings = kszphy_get_strings,
  772. .get_stats = kszphy_get_stats,
  773. .suspend = genphy_suspend,
  774. .resume = genphy_resume,
  775. }, {
  776. .phy_id = PHY_ID_KSZ8081,
  777. .name = "Micrel KSZ8081 or KSZ8091",
  778. .phy_id_mask = MICREL_PHY_ID_MASK,
  779. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  780. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  781. .driver_data = &ksz8081_type,
  782. .probe = kszphy_probe,
  783. .config_init = kszphy_config_init,
  784. .config_aneg = genphy_config_aneg,
  785. .read_status = genphy_read_status,
  786. .ack_interrupt = kszphy_ack_interrupt,
  787. .config_intr = kszphy_config_intr,
  788. .get_sset_count = kszphy_get_sset_count,
  789. .get_strings = kszphy_get_strings,
  790. .get_stats = kszphy_get_stats,
  791. .suspend = kszphy_suspend,
  792. .resume = kszphy_resume,
  793. }, {
  794. .phy_id = PHY_ID_KSZ8061,
  795. .name = "Micrel KSZ8061",
  796. .phy_id_mask = MICREL_PHY_ID_MASK,
  797. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  798. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  799. .config_init = kszphy_config_init,
  800. .config_aneg = genphy_config_aneg,
  801. .read_status = genphy_read_status,
  802. .ack_interrupt = kszphy_ack_interrupt,
  803. .config_intr = kszphy_config_intr,
  804. .suspend = genphy_suspend,
  805. .resume = genphy_resume,
  806. }, {
  807. .phy_id = PHY_ID_KSZ9021,
  808. .phy_id_mask = 0x000ffffe,
  809. .name = "Micrel KSZ9021 Gigabit PHY",
  810. .features = (PHY_GBIT_FEATURES | SUPPORTED_Pause),
  811. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  812. .driver_data = &ksz9021_type,
  813. .probe = kszphy_probe,
  814. .config_init = ksz9021_config_init,
  815. .config_aneg = genphy_config_aneg,
  816. .read_status = genphy_read_status,
  817. .ack_interrupt = kszphy_ack_interrupt,
  818. .config_intr = kszphy_config_intr,
  819. .get_sset_count = kszphy_get_sset_count,
  820. .get_strings = kszphy_get_strings,
  821. .get_stats = kszphy_get_stats,
  822. .suspend = genphy_suspend,
  823. .resume = genphy_resume,
  824. .read_mmd_indirect = ksz9021_rd_mmd_phyreg,
  825. .write_mmd_indirect = ksz9021_wr_mmd_phyreg,
  826. }, {
  827. .phy_id = PHY_ID_KSZ9031,
  828. .phy_id_mask = MICREL_PHY_ID_MASK,
  829. .name = "Micrel KSZ9031 Gigabit PHY",
  830. .features = (PHY_GBIT_FEATURES | SUPPORTED_Pause),
  831. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  832. .driver_data = &ksz9021_type,
  833. .probe = kszphy_probe,
  834. .config_init = ksz9031_config_init,
  835. .config_aneg = genphy_config_aneg,
  836. .read_status = ksz9031_read_status,
  837. .ack_interrupt = kszphy_ack_interrupt,
  838. .config_intr = kszphy_config_intr,
  839. .get_sset_count = kszphy_get_sset_count,
  840. .get_strings = kszphy_get_strings,
  841. .get_stats = kszphy_get_stats,
  842. .suspend = genphy_suspend,
  843. .resume = kszphy_resume,
  844. }, {
  845. .phy_id = PHY_ID_KSZ8873MLL,
  846. .phy_id_mask = MICREL_PHY_ID_MASK,
  847. .name = "Micrel KSZ8873MLL Switch",
  848. .features = (SUPPORTED_Pause | SUPPORTED_Asym_Pause),
  849. .flags = PHY_HAS_MAGICANEG,
  850. .config_init = kszphy_config_init,
  851. .config_aneg = ksz8873mll_config_aneg,
  852. .read_status = ksz8873mll_read_status,
  853. .suspend = genphy_suspend,
  854. .resume = genphy_resume,
  855. }, {
  856. .phy_id = PHY_ID_KSZ886X,
  857. .phy_id_mask = MICREL_PHY_ID_MASK,
  858. .name = "Micrel KSZ886X Switch",
  859. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  860. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  861. .config_init = kszphy_config_init,
  862. .config_aneg = genphy_config_aneg,
  863. .read_status = genphy_read_status,
  864. .suspend = genphy_suspend,
  865. .resume = genphy_resume,
  866. }, {
  867. .phy_id = PHY_ID_KSZ8795,
  868. .phy_id_mask = MICREL_PHY_ID_MASK,
  869. .name = "Micrel KSZ8795",
  870. .features = PHY_BASIC_FEATURES,
  871. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  872. .config_init = kszphy_config_init,
  873. .config_aneg = ksz8873mll_config_aneg,
  874. .read_status = ksz8873mll_read_status,
  875. .suspend = genphy_suspend,
  876. .resume = genphy_resume,
  877. } };
  878. module_phy_driver(ksphy_driver);
  879. MODULE_DESCRIPTION("Micrel PHY driver");
  880. MODULE_AUTHOR("David J. Choi");
  881. MODULE_LICENSE("GPL");
  882. static struct mdio_device_id __maybe_unused micrel_tbl[] = {
  883. { PHY_ID_KSZ9021, 0x000ffffe },
  884. { PHY_ID_KSZ9031, MICREL_PHY_ID_MASK },
  885. { PHY_ID_KSZ8001, 0x00fffffc },
  886. { PHY_ID_KS8737, MICREL_PHY_ID_MASK },
  887. { PHY_ID_KSZ8021, 0x00ffffff },
  888. { PHY_ID_KSZ8031, 0x00ffffff },
  889. { PHY_ID_KSZ8041, MICREL_PHY_ID_MASK },
  890. { PHY_ID_KSZ8051, MICREL_PHY_ID_MASK },
  891. { PHY_ID_KSZ8061, MICREL_PHY_ID_MASK },
  892. { PHY_ID_KSZ8081, MICREL_PHY_ID_MASK },
  893. { PHY_ID_KSZ8873MLL, MICREL_PHY_ID_MASK },
  894. { PHY_ID_KSZ886X, MICREL_PHY_ID_MASK },
  895. { }
  896. };
  897. MODULE_DEVICE_TABLE(mdio, micrel_tbl);