microchip_t1.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Microchip Technology
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/delay.h>
  6. #include <linux/mii.h>
  7. #include <linux/phy.h>
  8. /* External Register Control Register */
  9. #define LAN87XX_EXT_REG_CTL (0x14)
  10. #define LAN87XX_EXT_REG_CTL_RD_CTL (0x1000)
  11. #define LAN87XX_EXT_REG_CTL_WR_CTL (0x0800)
  12. /* External Register Read Data Register */
  13. #define LAN87XX_EXT_REG_RD_DATA (0x15)
  14. /* External Register Write Data Register */
  15. #define LAN87XX_EXT_REG_WR_DATA (0x16)
  16. /* Interrupt Source Register */
  17. #define LAN87XX_INTERRUPT_SOURCE (0x18)
  18. /* Interrupt Mask Register */
  19. #define LAN87XX_INTERRUPT_MASK (0x19)
  20. #define LAN87XX_MASK_LINK_UP (0x0004)
  21. #define LAN87XX_MASK_LINK_DOWN (0x0002)
  22. /* phyaccess nested types */
  23. #define PHYACC_ATTR_MODE_READ 0
  24. #define PHYACC_ATTR_MODE_WRITE 1
  25. #define PHYACC_ATTR_MODE_MODIFY 2
  26. #define PHYACC_ATTR_BANK_SMI 0
  27. #define PHYACC_ATTR_BANK_MISC 1
  28. #define PHYACC_ATTR_BANK_PCS 2
  29. #define PHYACC_ATTR_BANK_AFE 3
  30. #define PHYACC_ATTR_BANK_MAX 7
  31. #define DRIVER_AUTHOR "Nisar Sayed <nisar.sayed@microchip.com>"
  32. #define DRIVER_DESC "Microchip LAN87XX T1 PHY driver"
  33. struct access_ereg_val {
  34. u8 mode;
  35. u8 bank;
  36. u8 offset;
  37. u16 val;
  38. u16 mask;
  39. };
  40. static int access_ereg(struct phy_device *phydev, u8 mode, u8 bank,
  41. u8 offset, u16 val)
  42. {
  43. u16 ereg = 0;
  44. int rc = 0;
  45. if (mode > PHYACC_ATTR_MODE_WRITE || bank > PHYACC_ATTR_BANK_MAX)
  46. return -EINVAL;
  47. if (bank == PHYACC_ATTR_BANK_SMI) {
  48. if (mode == PHYACC_ATTR_MODE_WRITE)
  49. rc = phy_write(phydev, offset, val);
  50. else
  51. rc = phy_read(phydev, offset);
  52. return rc;
  53. }
  54. if (mode == PHYACC_ATTR_MODE_WRITE) {
  55. ereg = LAN87XX_EXT_REG_CTL_WR_CTL;
  56. rc = phy_write(phydev, LAN87XX_EXT_REG_WR_DATA, val);
  57. if (rc < 0)
  58. return rc;
  59. } else {
  60. ereg = LAN87XX_EXT_REG_CTL_RD_CTL;
  61. }
  62. ereg |= (bank << 8) | offset;
  63. rc = phy_write(phydev, LAN87XX_EXT_REG_CTL, ereg);
  64. if (rc < 0)
  65. return rc;
  66. if (mode == PHYACC_ATTR_MODE_READ)
  67. rc = phy_read(phydev, LAN87XX_EXT_REG_RD_DATA);
  68. return rc;
  69. }
  70. static int access_ereg_modify_changed(struct phy_device *phydev,
  71. u8 bank, u8 offset, u16 val, u16 mask)
  72. {
  73. int new = 0, rc = 0;
  74. if (bank > PHYACC_ATTR_BANK_MAX)
  75. return -EINVAL;
  76. rc = access_ereg(phydev, PHYACC_ATTR_MODE_READ, bank, offset, val);
  77. if (rc < 0)
  78. return rc;
  79. new = val | (rc & (mask ^ 0xFFFF));
  80. rc = access_ereg(phydev, PHYACC_ATTR_MODE_WRITE, bank, offset, new);
  81. return rc;
  82. }
  83. static int lan87xx_phy_init(struct phy_device *phydev)
  84. {
  85. static const struct access_ereg_val init[] = {
  86. /* TX Amplitude = 5 */
  87. {PHYACC_ATTR_MODE_MODIFY, PHYACC_ATTR_BANK_AFE, 0x0B,
  88. 0x000A, 0x001E},
  89. /* Clear SMI interrupts */
  90. {PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_SMI, 0x18,
  91. 0, 0},
  92. /* Clear MISC interrupts */
  93. {PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_MISC, 0x08,
  94. 0, 0},
  95. /* Turn on TC10 Ring Oscillator (ROSC) */
  96. {PHYACC_ATTR_MODE_MODIFY, PHYACC_ATTR_BANK_MISC, 0x20,
  97. 0x0020, 0x0020},
  98. /* WUR Detect Length to 1.2uS, LPC Detect Length to 1.09uS */
  99. {PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_PCS, 0x20,
  100. 0x283C, 0},
  101. /* Wake_In Debounce Length to 39uS, Wake_Out Length to 79uS */
  102. {PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_MISC, 0x21,
  103. 0x274F, 0},
  104. /* Enable Auto Wake Forward to Wake_Out, ROSC on, Sleep,
  105. * and Wake_In to wake PHY
  106. */
  107. {PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_MISC, 0x20,
  108. 0x80A7, 0},
  109. /* Enable WUP Auto Fwd, Enable Wake on MDI, Wakeup Debouncer
  110. * to 128 uS
  111. */
  112. {PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_MISC, 0x24,
  113. 0xF110, 0},
  114. /* Enable HW Init */
  115. {PHYACC_ATTR_MODE_MODIFY, PHYACC_ATTR_BANK_SMI, 0x1A,
  116. 0x0100, 0x0100},
  117. };
  118. int rc, i;
  119. /* Start manual initialization procedures in Managed Mode */
  120. rc = access_ereg_modify_changed(phydev, PHYACC_ATTR_BANK_SMI,
  121. 0x1a, 0x0000, 0x0100);
  122. if (rc < 0)
  123. return rc;
  124. /* Soft Reset the SMI block */
  125. rc = access_ereg_modify_changed(phydev, PHYACC_ATTR_BANK_SMI,
  126. 0x00, 0x8000, 0x8000);
  127. if (rc < 0)
  128. return rc;
  129. /* Check to see if the self-clearing bit is cleared */
  130. usleep_range(1000, 2000);
  131. rc = access_ereg(phydev, PHYACC_ATTR_MODE_READ,
  132. PHYACC_ATTR_BANK_SMI, 0x00, 0);
  133. if (rc < 0)
  134. return rc;
  135. if ((rc & 0x8000) != 0)
  136. return -ETIMEDOUT;
  137. /* PHY Initialization */
  138. for (i = 0; i < ARRAY_SIZE(init); i++) {
  139. if (init[i].mode == PHYACC_ATTR_MODE_MODIFY) {
  140. rc = access_ereg_modify_changed(phydev, init[i].bank,
  141. init[i].offset,
  142. init[i].val,
  143. init[i].mask);
  144. } else {
  145. rc = access_ereg(phydev, init[i].mode, init[i].bank,
  146. init[i].offset, init[i].val);
  147. }
  148. if (rc < 0)
  149. return rc;
  150. }
  151. return 0;
  152. }
  153. static int lan87xx_phy_config_intr(struct phy_device *phydev)
  154. {
  155. int rc, val = 0;
  156. if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
  157. /* unmask all source and clear them before enable */
  158. rc = phy_write(phydev, LAN87XX_INTERRUPT_MASK, 0x7FFF);
  159. rc = phy_read(phydev, LAN87XX_INTERRUPT_SOURCE);
  160. val = LAN87XX_MASK_LINK_UP | LAN87XX_MASK_LINK_DOWN;
  161. }
  162. rc = phy_write(phydev, LAN87XX_INTERRUPT_MASK, val);
  163. return rc < 0 ? rc : 0;
  164. }
  165. static int lan87xx_phy_ack_interrupt(struct phy_device *phydev)
  166. {
  167. int rc = phy_read(phydev, LAN87XX_INTERRUPT_SOURCE);
  168. return rc < 0 ? rc : 0;
  169. }
  170. static int lan87xx_config_init(struct phy_device *phydev)
  171. {
  172. int rc = lan87xx_phy_init(phydev);
  173. return rc < 0 ? rc : 0;
  174. }
  175. static struct phy_driver microchip_t1_phy_driver[] = {
  176. {
  177. .phy_id = 0x0007c150,
  178. .phy_id_mask = 0xfffffff0,
  179. .name = "Microchip LAN87xx T1",
  180. .features = PHY_BASIC_T1_FEATURES,
  181. .config_init = lan87xx_config_init,
  182. .config_aneg = genphy_config_aneg,
  183. .ack_interrupt = lan87xx_phy_ack_interrupt,
  184. .config_intr = lan87xx_phy_config_intr,
  185. .suspend = genphy_suspend,
  186. .resume = genphy_resume,
  187. }
  188. };
  189. module_phy_driver(microchip_t1_phy_driver);
  190. static struct mdio_device_id __maybe_unused microchip_t1_tbl[] = {
  191. { 0x0007c150, 0xfffffff0 },
  192. { }
  193. };
  194. MODULE_DEVICE_TABLE(mdio, microchip_t1_tbl);
  195. MODULE_AUTHOR(DRIVER_AUTHOR);
  196. MODULE_DESCRIPTION(DRIVER_DESC);
  197. MODULE_LICENSE("GPL");