nxp-tja11xx.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* NXP TJA1100 BroadRReach PHY driver
  3. *
  4. * Copyright (C) 2018 Marek Vasut <[email protected]>
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/ethtool.h>
  8. #include <linux/ethtool_netlink.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mdio.h>
  11. #include <linux/mii.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/phy.h>
  15. #include <linux/hwmon.h>
  16. #include <linux/bitfield.h>
  17. #include <linux/of_mdio.h>
  18. #include <linux/of_irq.h>
  19. #define PHY_ID_MASK 0xfffffff0
  20. #define PHY_ID_TJA1100 0x0180dc40
  21. #define PHY_ID_TJA1101 0x0180dd00
  22. #define PHY_ID_TJA1102 0x0180dc80
  23. #define MII_ECTRL 17
  24. #define MII_ECTRL_LINK_CONTROL BIT(15)
  25. #define MII_ECTRL_POWER_MODE_MASK GENMASK(14, 11)
  26. #define MII_ECTRL_POWER_MODE_NO_CHANGE (0x0 << 11)
  27. #define MII_ECTRL_POWER_MODE_NORMAL (0x3 << 11)
  28. #define MII_ECTRL_POWER_MODE_STANDBY (0xc << 11)
  29. #define MII_ECTRL_CABLE_TEST BIT(5)
  30. #define MII_ECTRL_CONFIG_EN BIT(2)
  31. #define MII_ECTRL_WAKE_REQUEST BIT(0)
  32. #define MII_CFG1 18
  33. #define MII_CFG1_MASTER_SLAVE BIT(15)
  34. #define MII_CFG1_AUTO_OP BIT(14)
  35. #define MII_CFG1_INTERFACE_MODE_MASK GENMASK(9, 8)
  36. #define MII_CFG1_MII_MODE (0x0 << 8)
  37. #define MII_CFG1_RMII_MODE_REFCLK_IN BIT(8)
  38. #define MII_CFG1_RMII_MODE_REFCLK_OUT BIT(9)
  39. #define MII_CFG1_REVMII_MODE GENMASK(9, 8)
  40. #define MII_CFG1_SLEEP_CONFIRM BIT(6)
  41. #define MII_CFG1_LED_MODE_MASK GENMASK(5, 4)
  42. #define MII_CFG1_LED_MODE_LINKUP 0
  43. #define MII_CFG1_LED_ENABLE BIT(3)
  44. #define MII_CFG2 19
  45. #define MII_CFG2_SLEEP_REQUEST_TO GENMASK(1, 0)
  46. #define MII_CFG2_SLEEP_REQUEST_TO_16MS 0x3
  47. #define MII_INTSRC 21
  48. #define MII_INTSRC_LINK_FAIL BIT(10)
  49. #define MII_INTSRC_LINK_UP BIT(9)
  50. #define MII_INTSRC_MASK (MII_INTSRC_LINK_FAIL | MII_INTSRC_LINK_UP)
  51. #define MII_INTSRC_UV_ERR BIT(3)
  52. #define MII_INTSRC_TEMP_ERR BIT(1)
  53. #define MII_INTEN 22
  54. #define MII_INTEN_LINK_FAIL BIT(10)
  55. #define MII_INTEN_LINK_UP BIT(9)
  56. #define MII_INTEN_UV_ERR BIT(3)
  57. #define MII_INTEN_TEMP_ERR BIT(1)
  58. #define MII_COMMSTAT 23
  59. #define MII_COMMSTAT_LINK_UP BIT(15)
  60. #define MII_COMMSTAT_SQI_STATE GENMASK(7, 5)
  61. #define MII_COMMSTAT_SQI_MAX 7
  62. #define MII_GENSTAT 24
  63. #define MII_GENSTAT_PLL_LOCKED BIT(14)
  64. #define MII_EXTSTAT 25
  65. #define MII_EXTSTAT_SHORT_DETECT BIT(8)
  66. #define MII_EXTSTAT_OPEN_DETECT BIT(7)
  67. #define MII_EXTSTAT_POLARITY_DETECT BIT(6)
  68. #define MII_COMMCFG 27
  69. #define MII_COMMCFG_AUTO_OP BIT(15)
  70. /* Configure REF_CLK as input in RMII mode */
  71. #define TJA110X_RMII_MODE_REFCLK_IN BIT(0)
  72. struct tja11xx_priv {
  73. char *hwmon_name;
  74. struct device *hwmon_dev;
  75. struct phy_device *phydev;
  76. struct work_struct phy_register_work;
  77. u32 flags;
  78. };
  79. struct tja11xx_phy_stats {
  80. const char *string;
  81. u8 reg;
  82. u8 off;
  83. u16 mask;
  84. };
  85. static struct tja11xx_phy_stats tja11xx_hw_stats[] = {
  86. { "phy_symbol_error_count", 20, 0, GENMASK(15, 0) },
  87. { "phy_polarity_detect", 25, 6, BIT(6) },
  88. { "phy_open_detect", 25, 7, BIT(7) },
  89. { "phy_short_detect", 25, 8, BIT(8) },
  90. { "phy_rem_rcvr_count", 26, 0, GENMASK(7, 0) },
  91. { "phy_loc_rcvr_count", 26, 8, GENMASK(15, 8) },
  92. };
  93. static int tja11xx_check(struct phy_device *phydev, u8 reg, u16 mask, u16 set)
  94. {
  95. int val;
  96. return phy_read_poll_timeout(phydev, reg, val, (val & mask) == set,
  97. 150, 30000, false);
  98. }
  99. static int phy_modify_check(struct phy_device *phydev, u8 reg,
  100. u16 mask, u16 set)
  101. {
  102. int ret;
  103. ret = phy_modify(phydev, reg, mask, set);
  104. if (ret)
  105. return ret;
  106. return tja11xx_check(phydev, reg, mask, set);
  107. }
  108. static int tja11xx_enable_reg_write(struct phy_device *phydev)
  109. {
  110. return phy_set_bits(phydev, MII_ECTRL, MII_ECTRL_CONFIG_EN);
  111. }
  112. static int tja11xx_enable_link_control(struct phy_device *phydev)
  113. {
  114. return phy_set_bits(phydev, MII_ECTRL, MII_ECTRL_LINK_CONTROL);
  115. }
  116. static int tja11xx_disable_link_control(struct phy_device *phydev)
  117. {
  118. return phy_clear_bits(phydev, MII_ECTRL, MII_ECTRL_LINK_CONTROL);
  119. }
  120. static int tja11xx_wakeup(struct phy_device *phydev)
  121. {
  122. int ret;
  123. ret = phy_read(phydev, MII_ECTRL);
  124. if (ret < 0)
  125. return ret;
  126. switch (ret & MII_ECTRL_POWER_MODE_MASK) {
  127. case MII_ECTRL_POWER_MODE_NO_CHANGE:
  128. break;
  129. case MII_ECTRL_POWER_MODE_NORMAL:
  130. ret = phy_set_bits(phydev, MII_ECTRL, MII_ECTRL_WAKE_REQUEST);
  131. if (ret)
  132. return ret;
  133. ret = phy_clear_bits(phydev, MII_ECTRL, MII_ECTRL_WAKE_REQUEST);
  134. if (ret)
  135. return ret;
  136. break;
  137. case MII_ECTRL_POWER_MODE_STANDBY:
  138. ret = phy_modify_check(phydev, MII_ECTRL,
  139. MII_ECTRL_POWER_MODE_MASK,
  140. MII_ECTRL_POWER_MODE_STANDBY);
  141. if (ret)
  142. return ret;
  143. ret = phy_modify(phydev, MII_ECTRL, MII_ECTRL_POWER_MODE_MASK,
  144. MII_ECTRL_POWER_MODE_NORMAL);
  145. if (ret)
  146. return ret;
  147. ret = phy_modify_check(phydev, MII_GENSTAT,
  148. MII_GENSTAT_PLL_LOCKED,
  149. MII_GENSTAT_PLL_LOCKED);
  150. if (ret)
  151. return ret;
  152. return tja11xx_enable_link_control(phydev);
  153. default:
  154. break;
  155. }
  156. return 0;
  157. }
  158. static int tja11xx_soft_reset(struct phy_device *phydev)
  159. {
  160. int ret;
  161. ret = tja11xx_enable_reg_write(phydev);
  162. if (ret)
  163. return ret;
  164. return genphy_soft_reset(phydev);
  165. }
  166. static int tja11xx_config_aneg_cable_test(struct phy_device *phydev)
  167. {
  168. bool finished = false;
  169. int ret;
  170. if (phydev->link)
  171. return 0;
  172. if (!phydev->drv->cable_test_start ||
  173. !phydev->drv->cable_test_get_status)
  174. return 0;
  175. ret = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
  176. if (ret)
  177. return ret;
  178. ret = phydev->drv->cable_test_start(phydev);
  179. if (ret)
  180. return ret;
  181. /* According to the documentation this test takes 100 usec */
  182. usleep_range(100, 200);
  183. ret = phydev->drv->cable_test_get_status(phydev, &finished);
  184. if (ret)
  185. return ret;
  186. if (finished)
  187. ethnl_cable_test_finished(phydev);
  188. return 0;
  189. }
  190. static int tja11xx_config_aneg(struct phy_device *phydev)
  191. {
  192. int ret, changed = 0;
  193. u16 ctl = 0;
  194. switch (phydev->master_slave_set) {
  195. case MASTER_SLAVE_CFG_MASTER_FORCE:
  196. ctl |= MII_CFG1_MASTER_SLAVE;
  197. break;
  198. case MASTER_SLAVE_CFG_SLAVE_FORCE:
  199. break;
  200. case MASTER_SLAVE_CFG_UNKNOWN:
  201. case MASTER_SLAVE_CFG_UNSUPPORTED:
  202. goto do_test;
  203. default:
  204. phydev_warn(phydev, "Unsupported Master/Slave mode\n");
  205. return -ENOTSUPP;
  206. }
  207. changed = phy_modify_changed(phydev, MII_CFG1, MII_CFG1_MASTER_SLAVE, ctl);
  208. if (changed < 0)
  209. return changed;
  210. do_test:
  211. ret = tja11xx_config_aneg_cable_test(phydev);
  212. if (ret)
  213. return ret;
  214. return __genphy_config_aneg(phydev, changed);
  215. }
  216. static int tja11xx_get_interface_mode(struct phy_device *phydev)
  217. {
  218. struct tja11xx_priv *priv = phydev->priv;
  219. int mii_mode;
  220. switch (phydev->interface) {
  221. case PHY_INTERFACE_MODE_MII:
  222. mii_mode = MII_CFG1_MII_MODE;
  223. break;
  224. case PHY_INTERFACE_MODE_REVMII:
  225. mii_mode = MII_CFG1_REVMII_MODE;
  226. break;
  227. case PHY_INTERFACE_MODE_RMII:
  228. if (priv->flags & TJA110X_RMII_MODE_REFCLK_IN)
  229. mii_mode = MII_CFG1_RMII_MODE_REFCLK_IN;
  230. else
  231. mii_mode = MII_CFG1_RMII_MODE_REFCLK_OUT;
  232. break;
  233. default:
  234. return -EINVAL;
  235. }
  236. return mii_mode;
  237. }
  238. static int tja11xx_config_init(struct phy_device *phydev)
  239. {
  240. u16 reg_mask, reg_val;
  241. int ret;
  242. ret = tja11xx_enable_reg_write(phydev);
  243. if (ret)
  244. return ret;
  245. phydev->autoneg = AUTONEG_DISABLE;
  246. phydev->speed = SPEED_100;
  247. phydev->duplex = DUPLEX_FULL;
  248. switch (phydev->phy_id & PHY_ID_MASK) {
  249. case PHY_ID_TJA1100:
  250. reg_mask = MII_CFG1_AUTO_OP | MII_CFG1_LED_MODE_MASK |
  251. MII_CFG1_LED_ENABLE;
  252. reg_val = MII_CFG1_AUTO_OP | MII_CFG1_LED_MODE_LINKUP |
  253. MII_CFG1_LED_ENABLE;
  254. reg_mask |= MII_CFG1_INTERFACE_MODE_MASK;
  255. ret = tja11xx_get_interface_mode(phydev);
  256. if (ret < 0)
  257. return ret;
  258. reg_val |= (ret & 0xffff);
  259. ret = phy_modify(phydev, MII_CFG1, reg_mask, reg_val);
  260. if (ret)
  261. return ret;
  262. break;
  263. case PHY_ID_TJA1101:
  264. reg_mask = MII_CFG1_INTERFACE_MODE_MASK;
  265. ret = tja11xx_get_interface_mode(phydev);
  266. if (ret < 0)
  267. return ret;
  268. reg_val = ret & 0xffff;
  269. ret = phy_modify(phydev, MII_CFG1, reg_mask, reg_val);
  270. if (ret)
  271. return ret;
  272. fallthrough;
  273. case PHY_ID_TJA1102:
  274. ret = phy_set_bits(phydev, MII_COMMCFG, MII_COMMCFG_AUTO_OP);
  275. if (ret)
  276. return ret;
  277. break;
  278. default:
  279. return -EINVAL;
  280. }
  281. ret = phy_clear_bits(phydev, MII_CFG1, MII_CFG1_SLEEP_CONFIRM);
  282. if (ret)
  283. return ret;
  284. ret = phy_modify(phydev, MII_CFG2, MII_CFG2_SLEEP_REQUEST_TO,
  285. MII_CFG2_SLEEP_REQUEST_TO_16MS);
  286. if (ret)
  287. return ret;
  288. ret = tja11xx_wakeup(phydev);
  289. if (ret < 0)
  290. return ret;
  291. /* ACK interrupts by reading the status register */
  292. ret = phy_read(phydev, MII_INTSRC);
  293. if (ret < 0)
  294. return ret;
  295. return 0;
  296. }
  297. static int tja11xx_read_status(struct phy_device *phydev)
  298. {
  299. int ret;
  300. phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN;
  301. phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
  302. ret = genphy_update_link(phydev);
  303. if (ret)
  304. return ret;
  305. ret = phy_read(phydev, MII_CFG1);
  306. if (ret < 0)
  307. return ret;
  308. if (ret & MII_CFG1_MASTER_SLAVE)
  309. phydev->master_slave_get = MASTER_SLAVE_CFG_MASTER_FORCE;
  310. else
  311. phydev->master_slave_get = MASTER_SLAVE_CFG_SLAVE_FORCE;
  312. if (phydev->link) {
  313. ret = phy_read(phydev, MII_COMMSTAT);
  314. if (ret < 0)
  315. return ret;
  316. if (!(ret & MII_COMMSTAT_LINK_UP))
  317. phydev->link = 0;
  318. }
  319. return 0;
  320. }
  321. static int tja11xx_get_sqi(struct phy_device *phydev)
  322. {
  323. int ret;
  324. ret = phy_read(phydev, MII_COMMSTAT);
  325. if (ret < 0)
  326. return ret;
  327. return FIELD_GET(MII_COMMSTAT_SQI_STATE, ret);
  328. }
  329. static int tja11xx_get_sqi_max(struct phy_device *phydev)
  330. {
  331. return MII_COMMSTAT_SQI_MAX;
  332. }
  333. static int tja11xx_get_sset_count(struct phy_device *phydev)
  334. {
  335. return ARRAY_SIZE(tja11xx_hw_stats);
  336. }
  337. static void tja11xx_get_strings(struct phy_device *phydev, u8 *data)
  338. {
  339. int i;
  340. for (i = 0; i < ARRAY_SIZE(tja11xx_hw_stats); i++) {
  341. strncpy(data + i * ETH_GSTRING_LEN,
  342. tja11xx_hw_stats[i].string, ETH_GSTRING_LEN);
  343. }
  344. }
  345. static void tja11xx_get_stats(struct phy_device *phydev,
  346. struct ethtool_stats *stats, u64 *data)
  347. {
  348. int i, ret;
  349. for (i = 0; i < ARRAY_SIZE(tja11xx_hw_stats); i++) {
  350. ret = phy_read(phydev, tja11xx_hw_stats[i].reg);
  351. if (ret < 0)
  352. data[i] = U64_MAX;
  353. else {
  354. data[i] = ret & tja11xx_hw_stats[i].mask;
  355. data[i] >>= tja11xx_hw_stats[i].off;
  356. }
  357. }
  358. }
  359. static int tja11xx_hwmon_read(struct device *dev,
  360. enum hwmon_sensor_types type,
  361. u32 attr, int channel, long *value)
  362. {
  363. struct phy_device *phydev = dev_get_drvdata(dev);
  364. int ret;
  365. if (type == hwmon_in && attr == hwmon_in_lcrit_alarm) {
  366. ret = phy_read(phydev, MII_INTSRC);
  367. if (ret < 0)
  368. return ret;
  369. *value = !!(ret & MII_INTSRC_TEMP_ERR);
  370. return 0;
  371. }
  372. if (type == hwmon_temp && attr == hwmon_temp_crit_alarm) {
  373. ret = phy_read(phydev, MII_INTSRC);
  374. if (ret < 0)
  375. return ret;
  376. *value = !!(ret & MII_INTSRC_UV_ERR);
  377. return 0;
  378. }
  379. return -EOPNOTSUPP;
  380. }
  381. static umode_t tja11xx_hwmon_is_visible(const void *data,
  382. enum hwmon_sensor_types type,
  383. u32 attr, int channel)
  384. {
  385. if (type == hwmon_in && attr == hwmon_in_lcrit_alarm)
  386. return 0444;
  387. if (type == hwmon_temp && attr == hwmon_temp_crit_alarm)
  388. return 0444;
  389. return 0;
  390. }
  391. static const struct hwmon_channel_info *tja11xx_hwmon_info[] = {
  392. HWMON_CHANNEL_INFO(in, HWMON_I_LCRIT_ALARM),
  393. HWMON_CHANNEL_INFO(temp, HWMON_T_CRIT_ALARM),
  394. NULL
  395. };
  396. static const struct hwmon_ops tja11xx_hwmon_hwmon_ops = {
  397. .is_visible = tja11xx_hwmon_is_visible,
  398. .read = tja11xx_hwmon_read,
  399. };
  400. static const struct hwmon_chip_info tja11xx_hwmon_chip_info = {
  401. .ops = &tja11xx_hwmon_hwmon_ops,
  402. .info = tja11xx_hwmon_info,
  403. };
  404. static int tja11xx_hwmon_register(struct phy_device *phydev,
  405. struct tja11xx_priv *priv)
  406. {
  407. struct device *dev = &phydev->mdio.dev;
  408. priv->hwmon_name = devm_hwmon_sanitize_name(dev, dev_name(dev));
  409. if (IS_ERR(priv->hwmon_name))
  410. return PTR_ERR(priv->hwmon_name);
  411. priv->hwmon_dev =
  412. devm_hwmon_device_register_with_info(dev, priv->hwmon_name,
  413. phydev,
  414. &tja11xx_hwmon_chip_info,
  415. NULL);
  416. return PTR_ERR_OR_ZERO(priv->hwmon_dev);
  417. }
  418. static int tja11xx_parse_dt(struct phy_device *phydev)
  419. {
  420. struct device_node *node = phydev->mdio.dev.of_node;
  421. struct tja11xx_priv *priv = phydev->priv;
  422. if (!IS_ENABLED(CONFIG_OF_MDIO))
  423. return 0;
  424. if (of_property_read_bool(node, "nxp,rmii-refclk-in"))
  425. priv->flags |= TJA110X_RMII_MODE_REFCLK_IN;
  426. return 0;
  427. }
  428. static int tja11xx_probe(struct phy_device *phydev)
  429. {
  430. struct device *dev = &phydev->mdio.dev;
  431. struct tja11xx_priv *priv;
  432. int ret;
  433. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  434. if (!priv)
  435. return -ENOMEM;
  436. priv->phydev = phydev;
  437. phydev->priv = priv;
  438. ret = tja11xx_parse_dt(phydev);
  439. if (ret)
  440. return ret;
  441. return tja11xx_hwmon_register(phydev, priv);
  442. }
  443. static void tja1102_p1_register(struct work_struct *work)
  444. {
  445. struct tja11xx_priv *priv = container_of(work, struct tja11xx_priv,
  446. phy_register_work);
  447. struct phy_device *phydev_phy0 = priv->phydev;
  448. struct mii_bus *bus = phydev_phy0->mdio.bus;
  449. struct device *dev = &phydev_phy0->mdio.dev;
  450. struct device_node *np = dev->of_node;
  451. struct device_node *child;
  452. int ret;
  453. for_each_available_child_of_node(np, child) {
  454. struct phy_device *phy;
  455. int addr;
  456. addr = of_mdio_parse_addr(dev, child);
  457. if (addr < 0) {
  458. dev_err(dev, "Can't parse addr\n");
  459. continue;
  460. } else if (addr != phydev_phy0->mdio.addr + 1) {
  461. /* Currently we care only about double PHY chip TJA1102.
  462. * If some day NXP will decide to bring chips with more
  463. * PHYs, this logic should be reworked.
  464. */
  465. dev_err(dev, "Unexpected address. Should be: %i\n",
  466. phydev_phy0->mdio.addr + 1);
  467. continue;
  468. }
  469. if (mdiobus_is_registered_device(bus, addr)) {
  470. dev_err(dev, "device is already registered\n");
  471. continue;
  472. }
  473. /* Real PHY ID of Port 1 is 0 */
  474. phy = phy_device_create(bus, addr, PHY_ID_TJA1102, false, NULL);
  475. if (IS_ERR(phy)) {
  476. dev_err(dev, "Can't create PHY device for Port 1: %i\n",
  477. addr);
  478. continue;
  479. }
  480. /* Overwrite parent device. phy_device_create() set parent to
  481. * the mii_bus->dev, which is not correct in case.
  482. */
  483. phy->mdio.dev.parent = dev;
  484. ret = of_mdiobus_phy_device_register(bus, phy, child, addr);
  485. if (ret) {
  486. /* All resources needed for Port 1 should be already
  487. * available for Port 0. Both ports use the same
  488. * interrupt line, so -EPROBE_DEFER would make no sense
  489. * here.
  490. */
  491. dev_err(dev, "Can't register Port 1. Unexpected error: %i\n",
  492. ret);
  493. phy_device_free(phy);
  494. }
  495. }
  496. }
  497. static int tja1102_p0_probe(struct phy_device *phydev)
  498. {
  499. struct device *dev = &phydev->mdio.dev;
  500. struct tja11xx_priv *priv;
  501. int ret;
  502. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  503. if (!priv)
  504. return -ENOMEM;
  505. priv->phydev = phydev;
  506. INIT_WORK(&priv->phy_register_work, tja1102_p1_register);
  507. ret = tja11xx_hwmon_register(phydev, priv);
  508. if (ret)
  509. return ret;
  510. schedule_work(&priv->phy_register_work);
  511. return 0;
  512. }
  513. static int tja1102_match_phy_device(struct phy_device *phydev, bool port0)
  514. {
  515. int ret;
  516. if ((phydev->phy_id & PHY_ID_MASK) != PHY_ID_TJA1102)
  517. return 0;
  518. ret = phy_read(phydev, MII_PHYSID2);
  519. if (ret < 0)
  520. return ret;
  521. /* TJA1102 Port 1 has phyid 0 and doesn't support temperature
  522. * and undervoltage alarms.
  523. */
  524. if (port0)
  525. return ret ? 1 : 0;
  526. return !ret;
  527. }
  528. static int tja1102_p0_match_phy_device(struct phy_device *phydev)
  529. {
  530. return tja1102_match_phy_device(phydev, true);
  531. }
  532. static int tja1102_p1_match_phy_device(struct phy_device *phydev)
  533. {
  534. return tja1102_match_phy_device(phydev, false);
  535. }
  536. static int tja11xx_ack_interrupt(struct phy_device *phydev)
  537. {
  538. int ret;
  539. ret = phy_read(phydev, MII_INTSRC);
  540. return (ret < 0) ? ret : 0;
  541. }
  542. static int tja11xx_config_intr(struct phy_device *phydev)
  543. {
  544. int value = 0;
  545. int err;
  546. if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
  547. err = tja11xx_ack_interrupt(phydev);
  548. if (err)
  549. return err;
  550. value = MII_INTEN_LINK_FAIL | MII_INTEN_LINK_UP |
  551. MII_INTEN_UV_ERR | MII_INTEN_TEMP_ERR;
  552. err = phy_write(phydev, MII_INTEN, value);
  553. } else {
  554. err = phy_write(phydev, MII_INTEN, value);
  555. if (err)
  556. return err;
  557. err = tja11xx_ack_interrupt(phydev);
  558. }
  559. return err;
  560. }
  561. static irqreturn_t tja11xx_handle_interrupt(struct phy_device *phydev)
  562. {
  563. struct device *dev = &phydev->mdio.dev;
  564. int irq_status;
  565. irq_status = phy_read(phydev, MII_INTSRC);
  566. if (irq_status < 0) {
  567. phy_error(phydev);
  568. return IRQ_NONE;
  569. }
  570. if (irq_status & MII_INTSRC_TEMP_ERR)
  571. dev_warn(dev, "Overtemperature error detected (temp > 155C°).\n");
  572. if (irq_status & MII_INTSRC_UV_ERR)
  573. dev_warn(dev, "Undervoltage error detected.\n");
  574. if (!(irq_status & MII_INTSRC_MASK))
  575. return IRQ_NONE;
  576. phy_trigger_machine(phydev);
  577. return IRQ_HANDLED;
  578. }
  579. static int tja11xx_cable_test_start(struct phy_device *phydev)
  580. {
  581. int ret;
  582. ret = phy_clear_bits(phydev, MII_COMMCFG, MII_COMMCFG_AUTO_OP);
  583. if (ret)
  584. return ret;
  585. ret = tja11xx_wakeup(phydev);
  586. if (ret < 0)
  587. return ret;
  588. ret = tja11xx_disable_link_control(phydev);
  589. if (ret < 0)
  590. return ret;
  591. return phy_set_bits(phydev, MII_ECTRL, MII_ECTRL_CABLE_TEST);
  592. }
  593. /*
  594. * | BI_DA+ | BI_DA- | Result
  595. * | open | open | open
  596. * | + short to - | - short to + | short
  597. * | short to Vdd | open | open
  598. * | open | shot to Vdd | open
  599. * | short to Vdd | short to Vdd | short
  600. * | shot to GND | open | open
  601. * | open | shot to GND | open
  602. * | short to GND | shot to GND | short
  603. * | connected to active link partner (master) | shot and open
  604. */
  605. static int tja11xx_cable_test_report_trans(u32 result)
  606. {
  607. u32 mask = MII_EXTSTAT_SHORT_DETECT | MII_EXTSTAT_OPEN_DETECT;
  608. if ((result & mask) == mask) {
  609. /* connected to active link partner (master) */
  610. return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
  611. } else if ((result & mask) == 0) {
  612. return ETHTOOL_A_CABLE_RESULT_CODE_OK;
  613. } else if (result & MII_EXTSTAT_SHORT_DETECT) {
  614. return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
  615. } else if (result & MII_EXTSTAT_OPEN_DETECT) {
  616. return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
  617. } else {
  618. return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
  619. }
  620. }
  621. static int tja11xx_cable_test_report(struct phy_device *phydev)
  622. {
  623. int ret;
  624. ret = phy_read(phydev, MII_EXTSTAT);
  625. if (ret < 0)
  626. return ret;
  627. ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A,
  628. tja11xx_cable_test_report_trans(ret));
  629. return 0;
  630. }
  631. static int tja11xx_cable_test_get_status(struct phy_device *phydev,
  632. bool *finished)
  633. {
  634. int ret;
  635. *finished = false;
  636. ret = phy_read(phydev, MII_ECTRL);
  637. if (ret < 0)
  638. return ret;
  639. if (!(ret & MII_ECTRL_CABLE_TEST)) {
  640. *finished = true;
  641. ret = phy_set_bits(phydev, MII_COMMCFG, MII_COMMCFG_AUTO_OP);
  642. if (ret)
  643. return ret;
  644. return tja11xx_cable_test_report(phydev);
  645. }
  646. return 0;
  647. }
  648. static struct phy_driver tja11xx_driver[] = {
  649. {
  650. PHY_ID_MATCH_MODEL(PHY_ID_TJA1100),
  651. .name = "NXP TJA1100",
  652. .features = PHY_BASIC_T1_FEATURES,
  653. .probe = tja11xx_probe,
  654. .soft_reset = tja11xx_soft_reset,
  655. .config_aneg = tja11xx_config_aneg,
  656. .config_init = tja11xx_config_init,
  657. .read_status = tja11xx_read_status,
  658. .get_sqi = tja11xx_get_sqi,
  659. .get_sqi_max = tja11xx_get_sqi_max,
  660. .suspend = genphy_suspend,
  661. .resume = genphy_resume,
  662. .set_loopback = genphy_loopback,
  663. /* Statistics */
  664. .get_sset_count = tja11xx_get_sset_count,
  665. .get_strings = tja11xx_get_strings,
  666. .get_stats = tja11xx_get_stats,
  667. }, {
  668. PHY_ID_MATCH_MODEL(PHY_ID_TJA1101),
  669. .name = "NXP TJA1101",
  670. .features = PHY_BASIC_T1_FEATURES,
  671. .probe = tja11xx_probe,
  672. .soft_reset = tja11xx_soft_reset,
  673. .config_aneg = tja11xx_config_aneg,
  674. .config_init = tja11xx_config_init,
  675. .read_status = tja11xx_read_status,
  676. .get_sqi = tja11xx_get_sqi,
  677. .get_sqi_max = tja11xx_get_sqi_max,
  678. .suspend = genphy_suspend,
  679. .resume = genphy_resume,
  680. .set_loopback = genphy_loopback,
  681. /* Statistics */
  682. .get_sset_count = tja11xx_get_sset_count,
  683. .get_strings = tja11xx_get_strings,
  684. .get_stats = tja11xx_get_stats,
  685. }, {
  686. .name = "NXP TJA1102 Port 0",
  687. .features = PHY_BASIC_T1_FEATURES,
  688. .flags = PHY_POLL_CABLE_TEST,
  689. .probe = tja1102_p0_probe,
  690. .soft_reset = tja11xx_soft_reset,
  691. .config_aneg = tja11xx_config_aneg,
  692. .config_init = tja11xx_config_init,
  693. .read_status = tja11xx_read_status,
  694. .get_sqi = tja11xx_get_sqi,
  695. .get_sqi_max = tja11xx_get_sqi_max,
  696. .match_phy_device = tja1102_p0_match_phy_device,
  697. .suspend = genphy_suspend,
  698. .resume = genphy_resume,
  699. .set_loopback = genphy_loopback,
  700. /* Statistics */
  701. .get_sset_count = tja11xx_get_sset_count,
  702. .get_strings = tja11xx_get_strings,
  703. .get_stats = tja11xx_get_stats,
  704. .config_intr = tja11xx_config_intr,
  705. .handle_interrupt = tja11xx_handle_interrupt,
  706. .cable_test_start = tja11xx_cable_test_start,
  707. .cable_test_get_status = tja11xx_cable_test_get_status,
  708. }, {
  709. .name = "NXP TJA1102 Port 1",
  710. .features = PHY_BASIC_T1_FEATURES,
  711. .flags = PHY_POLL_CABLE_TEST,
  712. /* currently no probe for Port 1 is need */
  713. .soft_reset = tja11xx_soft_reset,
  714. .config_aneg = tja11xx_config_aneg,
  715. .config_init = tja11xx_config_init,
  716. .read_status = tja11xx_read_status,
  717. .get_sqi = tja11xx_get_sqi,
  718. .get_sqi_max = tja11xx_get_sqi_max,
  719. .match_phy_device = tja1102_p1_match_phy_device,
  720. .suspend = genphy_suspend,
  721. .resume = genphy_resume,
  722. .set_loopback = genphy_loopback,
  723. /* Statistics */
  724. .get_sset_count = tja11xx_get_sset_count,
  725. .get_strings = tja11xx_get_strings,
  726. .get_stats = tja11xx_get_stats,
  727. .config_intr = tja11xx_config_intr,
  728. .handle_interrupt = tja11xx_handle_interrupt,
  729. .cable_test_start = tja11xx_cable_test_start,
  730. .cable_test_get_status = tja11xx_cable_test_get_status,
  731. }
  732. };
  733. module_phy_driver(tja11xx_driver);
  734. static struct mdio_device_id __maybe_unused tja11xx_tbl[] = {
  735. { PHY_ID_MATCH_MODEL(PHY_ID_TJA1100) },
  736. { PHY_ID_MATCH_MODEL(PHY_ID_TJA1101) },
  737. { PHY_ID_MATCH_MODEL(PHY_ID_TJA1102) },
  738. { }
  739. };
  740. MODULE_DEVICE_TABLE(mdio, tja11xx_tbl);
  741. MODULE_AUTHOR("Marek Vasut <[email protected]>");
  742. MODULE_DESCRIPTION("NXP TJA11xx BoardR-Reach PHY driver");
  743. MODULE_LICENSE("GPL");