dp83848.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the Texas Instruments DP83848 PHY
  4. *
  5. * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
  6. */
  7. #include <linux/module.h>
  8. #include <linux/phy.h>
  9. #define TI_DP83848C_PHY_ID 0x20005ca0
  10. #define TI_DP83620_PHY_ID 0x20005ce0
  11. #define NS_DP83848C_PHY_ID 0x20005c90
  12. #define TLK10X_PHY_ID 0x2000a210
  13. /* Registers */
  14. #define DP83848_MICR 0x11 /* MII Interrupt Control Register */
  15. #define DP83848_MISR 0x12 /* MII Interrupt Status Register */
  16. /* MICR Register Fields */
  17. #define DP83848_MICR_INT_OE BIT(0) /* Interrupt Output Enable */
  18. #define DP83848_MICR_INTEN BIT(1) /* Interrupt Enable */
  19. /* MISR Register Fields */
  20. #define DP83848_MISR_RHF_INT_EN BIT(0) /* Receive Error Counter */
  21. #define DP83848_MISR_FHF_INT_EN BIT(1) /* False Carrier Counter */
  22. #define DP83848_MISR_ANC_INT_EN BIT(2) /* Auto-negotiation complete */
  23. #define DP83848_MISR_DUP_INT_EN BIT(3) /* Duplex Status */
  24. #define DP83848_MISR_SPD_INT_EN BIT(4) /* Speed status */
  25. #define DP83848_MISR_LINK_INT_EN BIT(5) /* Link status */
  26. #define DP83848_MISR_ED_INT_EN BIT(6) /* Energy detect */
  27. #define DP83848_MISR_LQM_INT_EN BIT(7) /* Link Quality Monitor */
  28. #define DP83848_INT_EN_MASK \
  29. (DP83848_MISR_ANC_INT_EN | \
  30. DP83848_MISR_DUP_INT_EN | \
  31. DP83848_MISR_SPD_INT_EN | \
  32. DP83848_MISR_LINK_INT_EN)
  33. #define DP83848_MISR_RHF_INT BIT(8)
  34. #define DP83848_MISR_FHF_INT BIT(9)
  35. #define DP83848_MISR_ANC_INT BIT(10)
  36. #define DP83848_MISR_DUP_INT BIT(11)
  37. #define DP83848_MISR_SPD_INT BIT(12)
  38. #define DP83848_MISR_LINK_INT BIT(13)
  39. #define DP83848_MISR_ED_INT BIT(14)
  40. #define DP83848_INT_MASK \
  41. (DP83848_MISR_ANC_INT | \
  42. DP83848_MISR_DUP_INT | \
  43. DP83848_MISR_SPD_INT | \
  44. DP83848_MISR_LINK_INT)
  45. static int dp83848_ack_interrupt(struct phy_device *phydev)
  46. {
  47. int err = phy_read(phydev, DP83848_MISR);
  48. return err < 0 ? err : 0;
  49. }
  50. static int dp83848_config_intr(struct phy_device *phydev)
  51. {
  52. int control, ret;
  53. control = phy_read(phydev, DP83848_MICR);
  54. if (control < 0)
  55. return control;
  56. if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
  57. ret = dp83848_ack_interrupt(phydev);
  58. if (ret)
  59. return ret;
  60. control |= DP83848_MICR_INT_OE;
  61. control |= DP83848_MICR_INTEN;
  62. ret = phy_write(phydev, DP83848_MISR, DP83848_INT_EN_MASK);
  63. if (ret < 0)
  64. return ret;
  65. ret = phy_write(phydev, DP83848_MICR, control);
  66. } else {
  67. control &= ~DP83848_MICR_INTEN;
  68. ret = phy_write(phydev, DP83848_MICR, control);
  69. if (ret)
  70. return ret;
  71. ret = dp83848_ack_interrupt(phydev);
  72. }
  73. return ret;
  74. }
  75. static irqreturn_t dp83848_handle_interrupt(struct phy_device *phydev)
  76. {
  77. int irq_status;
  78. irq_status = phy_read(phydev, DP83848_MISR);
  79. if (irq_status < 0) {
  80. phy_error(phydev);
  81. return IRQ_NONE;
  82. }
  83. if (!(irq_status & DP83848_INT_MASK))
  84. return IRQ_NONE;
  85. phy_trigger_machine(phydev);
  86. return IRQ_HANDLED;
  87. }
  88. static int dp83848_config_init(struct phy_device *phydev)
  89. {
  90. int val;
  91. /* DP83620 always reports Auto Negotiation Ability on BMSR. Instead,
  92. * we check initial value of BMCR Auto negotiation enable bit
  93. */
  94. val = phy_read(phydev, MII_BMCR);
  95. if (!(val & BMCR_ANENABLE))
  96. phydev->autoneg = AUTONEG_DISABLE;
  97. return 0;
  98. }
  99. static struct mdio_device_id __maybe_unused dp83848_tbl[] = {
  100. { TI_DP83848C_PHY_ID, 0xfffffff0 },
  101. { NS_DP83848C_PHY_ID, 0xfffffff0 },
  102. { TI_DP83620_PHY_ID, 0xfffffff0 },
  103. { TLK10X_PHY_ID, 0xfffffff0 },
  104. { }
  105. };
  106. MODULE_DEVICE_TABLE(mdio, dp83848_tbl);
  107. #define DP83848_PHY_DRIVER(_id, _name, _config_init) \
  108. { \
  109. .phy_id = _id, \
  110. .phy_id_mask = 0xfffffff0, \
  111. .name = _name, \
  112. /* PHY_BASIC_FEATURES */ \
  113. \
  114. .soft_reset = genphy_soft_reset, \
  115. .config_init = _config_init, \
  116. .suspend = genphy_suspend, \
  117. .resume = genphy_resume, \
  118. \
  119. /* IRQ related */ \
  120. .config_intr = dp83848_config_intr, \
  121. .handle_interrupt = dp83848_handle_interrupt, \
  122. }
  123. static struct phy_driver dp83848_driver[] = {
  124. DP83848_PHY_DRIVER(TI_DP83848C_PHY_ID, "TI DP83848C 10/100 Mbps PHY",
  125. NULL),
  126. DP83848_PHY_DRIVER(NS_DP83848C_PHY_ID, "NS DP83848C 10/100 Mbps PHY",
  127. NULL),
  128. DP83848_PHY_DRIVER(TI_DP83620_PHY_ID, "TI DP83620 10/100 Mbps PHY",
  129. dp83848_config_init),
  130. DP83848_PHY_DRIVER(TLK10X_PHY_ID, "TI TLK10X 10/100 Mbps PHY",
  131. NULL),
  132. };
  133. module_phy_driver(dp83848_driver);
  134. MODULE_DESCRIPTION("Texas Instruments DP83848 PHY driver");
  135. MODULE_AUTHOR("Andrew F. Davis <[email protected]>");
  136. MODULE_LICENSE("GPL v2");