phy-mtk-tphy.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015 MediaTek Inc.
  4. * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
  5. *
  6. */
  7. #include <dt-bindings/phy/phy.h>
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/io.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/module.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_device.h>
  15. #include <linux/phy/phy.h>
  16. #include <linux/platform_device.h>
  17. /* version V1 sub-banks offset base address */
  18. /* banks shared by multiple phys */
  19. #define SSUSB_SIFSLV_V1_SPLLC 0x000 /* shared by u3 phys */
  20. #define SSUSB_SIFSLV_V1_U2FREQ 0x100 /* shared by u2 phys */
  21. #define SSUSB_SIFSLV_V1_CHIP 0x300 /* shared by u3 phys */
  22. /* u2 phy bank */
  23. #define SSUSB_SIFSLV_V1_U2PHY_COM 0x000
  24. /* u3/pcie/sata phy banks */
  25. #define SSUSB_SIFSLV_V1_U3PHYD 0x000
  26. #define SSUSB_SIFSLV_V1_U3PHYA 0x200
  27. /* version V2 sub-banks offset base address */
  28. /* u2 phy banks */
  29. #define SSUSB_SIFSLV_V2_MISC 0x000
  30. #define SSUSB_SIFSLV_V2_U2FREQ 0x100
  31. #define SSUSB_SIFSLV_V2_U2PHY_COM 0x300
  32. /* u3/pcie/sata phy banks */
  33. #define SSUSB_SIFSLV_V2_SPLLC 0x000
  34. #define SSUSB_SIFSLV_V2_CHIP 0x100
  35. #define SSUSB_SIFSLV_V2_U3PHYD 0x200
  36. #define SSUSB_SIFSLV_V2_U3PHYA 0x400
  37. #define U3P_USBPHYACR0 0x000
  38. #define PA0_RG_U2PLL_FORCE_ON BIT(15)
  39. #define PA0_RG_USB20_INTR_EN BIT(5)
  40. #define U3P_USBPHYACR1 0x004
  41. #define PA1_RG_VRT_SEL GENMASK(14, 12)
  42. #define PA1_RG_VRT_SEL_VAL(x) ((0x7 & (x)) << 12)
  43. #define PA1_RG_TERM_SEL GENMASK(10, 8)
  44. #define PA1_RG_TERM_SEL_VAL(x) ((0x7 & (x)) << 8)
  45. #define U3P_USBPHYACR2 0x008
  46. #define PA2_RG_SIF_U2PLL_FORCE_EN BIT(18)
  47. #define U3P_USBPHYACR5 0x014
  48. #define PA5_RG_U2_HSTX_SRCAL_EN BIT(15)
  49. #define PA5_RG_U2_HSTX_SRCTRL GENMASK(14, 12)
  50. #define PA5_RG_U2_HSTX_SRCTRL_VAL(x) ((0x7 & (x)) << 12)
  51. #define PA5_RG_U2_HS_100U_U3_EN BIT(11)
  52. #define U3P_USBPHYACR6 0x018
  53. #define PA6_RG_U2_BC11_SW_EN BIT(23)
  54. #define PA6_RG_U2_OTG_VBUSCMP_EN BIT(20)
  55. #define PA6_RG_U2_SQTH GENMASK(3, 0)
  56. #define PA6_RG_U2_SQTH_VAL(x) (0xf & (x))
  57. #define U3P_U2PHYACR4 0x020
  58. #define P2C_RG_USB20_GPIO_CTL BIT(9)
  59. #define P2C_USB20_GPIO_MODE BIT(8)
  60. #define P2C_U2_GPIO_CTR_MSK (P2C_RG_USB20_GPIO_CTL | P2C_USB20_GPIO_MODE)
  61. #define U3D_U2PHYDCR0 0x060
  62. #define P2C_RG_SIF_U2PLL_FORCE_ON BIT(24)
  63. #define U3P_U2PHYDTM0 0x068
  64. #define P2C_FORCE_UART_EN BIT(26)
  65. #define P2C_FORCE_DATAIN BIT(23)
  66. #define P2C_FORCE_DM_PULLDOWN BIT(21)
  67. #define P2C_FORCE_DP_PULLDOWN BIT(20)
  68. #define P2C_FORCE_XCVRSEL BIT(19)
  69. #define P2C_FORCE_SUSPENDM BIT(18)
  70. #define P2C_FORCE_TERMSEL BIT(17)
  71. #define P2C_RG_DATAIN GENMASK(13, 10)
  72. #define P2C_RG_DATAIN_VAL(x) ((0xf & (x)) << 10)
  73. #define P2C_RG_DMPULLDOWN BIT(7)
  74. #define P2C_RG_DPPULLDOWN BIT(6)
  75. #define P2C_RG_XCVRSEL GENMASK(5, 4)
  76. #define P2C_RG_XCVRSEL_VAL(x) ((0x3 & (x)) << 4)
  77. #define P2C_RG_SUSPENDM BIT(3)
  78. #define P2C_RG_TERMSEL BIT(2)
  79. #define P2C_DTM0_PART_MASK \
  80. (P2C_FORCE_DATAIN | P2C_FORCE_DM_PULLDOWN | \
  81. P2C_FORCE_DP_PULLDOWN | P2C_FORCE_XCVRSEL | \
  82. P2C_FORCE_TERMSEL | P2C_RG_DMPULLDOWN | \
  83. P2C_RG_DPPULLDOWN | P2C_RG_TERMSEL)
  84. #define U3P_U2PHYDTM1 0x06C
  85. #define P2C_RG_UART_EN BIT(16)
  86. #define P2C_FORCE_IDDIG BIT(9)
  87. #define P2C_RG_VBUSVALID BIT(5)
  88. #define P2C_RG_SESSEND BIT(4)
  89. #define P2C_RG_AVALID BIT(2)
  90. #define P2C_RG_IDDIG BIT(1)
  91. #define U3P_U2PHYBC12C 0x080
  92. #define P2C_RG_CHGDT_EN BIT(0)
  93. #define U3P_U3_CHIP_GPIO_CTLD 0x0c
  94. #define P3C_REG_IP_SW_RST BIT(31)
  95. #define P3C_MCU_BUS_CK_GATE_EN BIT(30)
  96. #define P3C_FORCE_IP_SW_RST BIT(29)
  97. #define U3P_U3_CHIP_GPIO_CTLE 0x10
  98. #define P3C_RG_SWRST_U3_PHYD BIT(25)
  99. #define P3C_RG_SWRST_U3_PHYD_FORCE_EN BIT(24)
  100. #define U3P_U3_PHYA_REG0 0x000
  101. #define P3A_RG_CLKDRV_OFF GENMASK(3, 2)
  102. #define P3A_RG_CLKDRV_OFF_VAL(x) ((0x3 & (x)) << 2)
  103. #define U3P_U3_PHYA_REG1 0x004
  104. #define P3A_RG_CLKDRV_AMP GENMASK(31, 29)
  105. #define P3A_RG_CLKDRV_AMP_VAL(x) ((0x7 & (x)) << 29)
  106. #define U3P_U3_PHYA_REG6 0x018
  107. #define P3A_RG_TX_EIDLE_CM GENMASK(31, 28)
  108. #define P3A_RG_TX_EIDLE_CM_VAL(x) ((0xf & (x)) << 28)
  109. #define U3P_U3_PHYA_REG9 0x024
  110. #define P3A_RG_RX_DAC_MUX GENMASK(5, 1)
  111. #define P3A_RG_RX_DAC_MUX_VAL(x) ((0x1f & (x)) << 1)
  112. #define U3P_U3_PHYA_DA_REG0 0x100
  113. #define P3A_RG_XTAL_EXT_PE2H GENMASK(17, 16)
  114. #define P3A_RG_XTAL_EXT_PE2H_VAL(x) ((0x3 & (x)) << 16)
  115. #define P3A_RG_XTAL_EXT_PE1H GENMASK(13, 12)
  116. #define P3A_RG_XTAL_EXT_PE1H_VAL(x) ((0x3 & (x)) << 12)
  117. #define P3A_RG_XTAL_EXT_EN_U3 GENMASK(11, 10)
  118. #define P3A_RG_XTAL_EXT_EN_U3_VAL(x) ((0x3 & (x)) << 10)
  119. #define U3P_U3_PHYA_DA_REG4 0x108
  120. #define P3A_RG_PLL_DIVEN_PE2H GENMASK(21, 19)
  121. #define P3A_RG_PLL_BC_PE2H GENMASK(7, 6)
  122. #define P3A_RG_PLL_BC_PE2H_VAL(x) ((0x3 & (x)) << 6)
  123. #define U3P_U3_PHYA_DA_REG5 0x10c
  124. #define P3A_RG_PLL_BR_PE2H GENMASK(29, 28)
  125. #define P3A_RG_PLL_BR_PE2H_VAL(x) ((0x3 & (x)) << 28)
  126. #define P3A_RG_PLL_IC_PE2H GENMASK(15, 12)
  127. #define P3A_RG_PLL_IC_PE2H_VAL(x) ((0xf & (x)) << 12)
  128. #define U3P_U3_PHYA_DA_REG6 0x110
  129. #define P3A_RG_PLL_IR_PE2H GENMASK(19, 16)
  130. #define P3A_RG_PLL_IR_PE2H_VAL(x) ((0xf & (x)) << 16)
  131. #define U3P_U3_PHYA_DA_REG7 0x114
  132. #define P3A_RG_PLL_BP_PE2H GENMASK(19, 16)
  133. #define P3A_RG_PLL_BP_PE2H_VAL(x) ((0xf & (x)) << 16)
  134. #define U3P_U3_PHYA_DA_REG20 0x13c
  135. #define P3A_RG_PLL_DELTA1_PE2H GENMASK(31, 16)
  136. #define P3A_RG_PLL_DELTA1_PE2H_VAL(x) ((0xffff & (x)) << 16)
  137. #define U3P_U3_PHYA_DA_REG25 0x148
  138. #define P3A_RG_PLL_DELTA_PE2H GENMASK(15, 0)
  139. #define P3A_RG_PLL_DELTA_PE2H_VAL(x) (0xffff & (x))
  140. #define U3P_U3_PHYD_LFPS1 0x00c
  141. #define P3D_RG_FWAKE_TH GENMASK(21, 16)
  142. #define P3D_RG_FWAKE_TH_VAL(x) ((0x3f & (x)) << 16)
  143. #define U3P_U3_PHYD_CDR1 0x05c
  144. #define P3D_RG_CDR_BIR_LTD1 GENMASK(28, 24)
  145. #define P3D_RG_CDR_BIR_LTD1_VAL(x) ((0x1f & (x)) << 24)
  146. #define P3D_RG_CDR_BIR_LTD0 GENMASK(12, 8)
  147. #define P3D_RG_CDR_BIR_LTD0_VAL(x) ((0x1f & (x)) << 8)
  148. #define U3P_U3_PHYD_RXDET1 0x128
  149. #define P3D_RG_RXDET_STB2_SET GENMASK(17, 9)
  150. #define P3D_RG_RXDET_STB2_SET_VAL(x) ((0x1ff & (x)) << 9)
  151. #define U3P_U3_PHYD_RXDET2 0x12c
  152. #define P3D_RG_RXDET_STB2_SET_P3 GENMASK(8, 0)
  153. #define P3D_RG_RXDET_STB2_SET_P3_VAL(x) (0x1ff & (x))
  154. #define U3P_SPLLC_XTALCTL3 0x018
  155. #define XC3_RG_U3_XTAL_RX_PWD BIT(9)
  156. #define XC3_RG_U3_FRC_XTAL_RX_PWD BIT(8)
  157. #define U3P_U2FREQ_FMCR0 0x00
  158. #define P2F_RG_MONCLK_SEL GENMASK(27, 26)
  159. #define P2F_RG_MONCLK_SEL_VAL(x) ((0x3 & (x)) << 26)
  160. #define P2F_RG_FREQDET_EN BIT(24)
  161. #define P2F_RG_CYCLECNT GENMASK(23, 0)
  162. #define P2F_RG_CYCLECNT_VAL(x) ((P2F_RG_CYCLECNT) & (x))
  163. #define U3P_U2FREQ_VALUE 0x0c
  164. #define U3P_U2FREQ_FMMONR1 0x10
  165. #define P2F_USB_FM_VALID BIT(0)
  166. #define P2F_RG_FRCK_EN BIT(8)
  167. #define U3P_REF_CLK 26 /* MHZ */
  168. #define U3P_SLEW_RATE_COEF 28
  169. #define U3P_SR_COEF_DIVISOR 1000
  170. #define U3P_FM_DET_CYCLE_CNT 1024
  171. /* SATA register setting */
  172. #define PHYD_CTRL_SIGNAL_MODE4 0x1c
  173. /* CDR Charge Pump P-path current adjustment */
  174. #define RG_CDR_BICLTD1_GEN1_MSK GENMASK(23, 20)
  175. #define RG_CDR_BICLTD1_GEN1_VAL(x) ((0xf & (x)) << 20)
  176. #define RG_CDR_BICLTD0_GEN1_MSK GENMASK(11, 8)
  177. #define RG_CDR_BICLTD0_GEN1_VAL(x) ((0xf & (x)) << 8)
  178. #define PHYD_DESIGN_OPTION2 0x24
  179. /* Symbol lock count selection */
  180. #define RG_LOCK_CNT_SEL_MSK GENMASK(5, 4)
  181. #define RG_LOCK_CNT_SEL_VAL(x) ((0x3 & (x)) << 4)
  182. #define PHYD_DESIGN_OPTION9 0x40
  183. /* COMWAK GAP width window */
  184. #define RG_TG_MAX_MSK GENMASK(20, 16)
  185. #define RG_TG_MAX_VAL(x) ((0x1f & (x)) << 16)
  186. /* COMINIT GAP width window */
  187. #define RG_T2_MAX_MSK GENMASK(13, 8)
  188. #define RG_T2_MAX_VAL(x) ((0x3f & (x)) << 8)
  189. /* COMWAK GAP width window */
  190. #define RG_TG_MIN_MSK GENMASK(7, 5)
  191. #define RG_TG_MIN_VAL(x) ((0x7 & (x)) << 5)
  192. /* COMINIT GAP width window */
  193. #define RG_T2_MIN_MSK GENMASK(4, 0)
  194. #define RG_T2_MIN_VAL(x) (0x1f & (x))
  195. #define ANA_RG_CTRL_SIGNAL1 0x4c
  196. /* TX driver tail current control for 0dB de-empahsis mdoe for Gen1 speed */
  197. #define RG_IDRV_0DB_GEN1_MSK GENMASK(13, 8)
  198. #define RG_IDRV_0DB_GEN1_VAL(x) ((0x3f & (x)) << 8)
  199. #define ANA_RG_CTRL_SIGNAL4 0x58
  200. #define RG_CDR_BICLTR_GEN1_MSK GENMASK(23, 20)
  201. #define RG_CDR_BICLTR_GEN1_VAL(x) ((0xf & (x)) << 20)
  202. /* Loop filter R1 resistance adjustment for Gen1 speed */
  203. #define RG_CDR_BR_GEN2_MSK GENMASK(10, 8)
  204. #define RG_CDR_BR_GEN2_VAL(x) ((0x7 & (x)) << 8)
  205. #define ANA_RG_CTRL_SIGNAL6 0x60
  206. /* I-path capacitance adjustment for Gen1 */
  207. #define RG_CDR_BC_GEN1_MSK GENMASK(28, 24)
  208. #define RG_CDR_BC_GEN1_VAL(x) ((0x1f & (x)) << 24)
  209. #define RG_CDR_BIRLTR_GEN1_MSK GENMASK(4, 0)
  210. #define RG_CDR_BIRLTR_GEN1_VAL(x) (0x1f & (x))
  211. #define ANA_EQ_EYE_CTRL_SIGNAL1 0x6c
  212. /* RX Gen1 LEQ tuning step */
  213. #define RG_EQ_DLEQ_LFI_GEN1_MSK GENMASK(11, 8)
  214. #define RG_EQ_DLEQ_LFI_GEN1_VAL(x) ((0xf & (x)) << 8)
  215. #define ANA_EQ_EYE_CTRL_SIGNAL4 0xd8
  216. #define RG_CDR_BIRLTD0_GEN1_MSK GENMASK(20, 16)
  217. #define RG_CDR_BIRLTD0_GEN1_VAL(x) ((0x1f & (x)) << 16)
  218. #define ANA_EQ_EYE_CTRL_SIGNAL5 0xdc
  219. #define RG_CDR_BIRLTD0_GEN3_MSK GENMASK(4, 0)
  220. #define RG_CDR_BIRLTD0_GEN3_VAL(x) (0x1f & (x))
  221. enum mtk_phy_version {
  222. MTK_PHY_V1 = 1,
  223. MTK_PHY_V2,
  224. };
  225. struct mtk_phy_pdata {
  226. /* avoid RX sensitivity level degradation only for mt8173 */
  227. bool avoid_rx_sen_degradation;
  228. enum mtk_phy_version version;
  229. };
  230. struct u2phy_banks {
  231. void __iomem *misc;
  232. void __iomem *fmreg;
  233. void __iomem *com;
  234. };
  235. struct u3phy_banks {
  236. void __iomem *spllc;
  237. void __iomem *chip;
  238. void __iomem *phyd; /* include u3phyd_bank2 */
  239. void __iomem *phya; /* include u3phya_da */
  240. };
  241. struct mtk_phy_instance {
  242. struct phy *phy;
  243. void __iomem *port_base;
  244. union {
  245. struct u2phy_banks u2_banks;
  246. struct u3phy_banks u3_banks;
  247. };
  248. struct clk *ref_clk; /* reference clock of anolog phy */
  249. u32 index;
  250. u8 type;
  251. int eye_src;
  252. int eye_vrt;
  253. int eye_term;
  254. bool bc12_en;
  255. };
  256. struct mtk_tphy {
  257. struct device *dev;
  258. void __iomem *sif_base; /* only shared sif */
  259. /* deprecated, use @ref_clk instead in phy instance */
  260. struct clk *u3phya_ref; /* reference clock of usb3 anolog phy */
  261. const struct mtk_phy_pdata *pdata;
  262. struct mtk_phy_instance **phys;
  263. int nphys;
  264. int src_ref_clk; /* MHZ, reference clock for slew rate calibrate */
  265. int src_coef; /* coefficient for slew rate calibrate */
  266. };
  267. static void hs_slew_rate_calibrate(struct mtk_tphy *tphy,
  268. struct mtk_phy_instance *instance)
  269. {
  270. struct u2phy_banks *u2_banks = &instance->u2_banks;
  271. void __iomem *fmreg = u2_banks->fmreg;
  272. void __iomem *com = u2_banks->com;
  273. int calibration_val;
  274. int fm_out;
  275. u32 tmp;
  276. /* use force value */
  277. if (instance->eye_src)
  278. return;
  279. /* enable USB ring oscillator */
  280. tmp = readl(com + U3P_USBPHYACR5);
  281. tmp |= PA5_RG_U2_HSTX_SRCAL_EN;
  282. writel(tmp, com + U3P_USBPHYACR5);
  283. udelay(1);
  284. /*enable free run clock */
  285. tmp = readl(fmreg + U3P_U2FREQ_FMMONR1);
  286. tmp |= P2F_RG_FRCK_EN;
  287. writel(tmp, fmreg + U3P_U2FREQ_FMMONR1);
  288. /* set cycle count as 1024, and select u2 channel */
  289. tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
  290. tmp &= ~(P2F_RG_CYCLECNT | P2F_RG_MONCLK_SEL);
  291. tmp |= P2F_RG_CYCLECNT_VAL(U3P_FM_DET_CYCLE_CNT);
  292. if (tphy->pdata->version == MTK_PHY_V1)
  293. tmp |= P2F_RG_MONCLK_SEL_VAL(instance->index >> 1);
  294. writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
  295. /* enable frequency meter */
  296. tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
  297. tmp |= P2F_RG_FREQDET_EN;
  298. writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
  299. /* ignore return value */
  300. readl_poll_timeout(fmreg + U3P_U2FREQ_FMMONR1, tmp,
  301. (tmp & P2F_USB_FM_VALID), 10, 200);
  302. fm_out = readl(fmreg + U3P_U2FREQ_VALUE);
  303. /* disable frequency meter */
  304. tmp = readl(fmreg + U3P_U2FREQ_FMCR0);
  305. tmp &= ~P2F_RG_FREQDET_EN;
  306. writel(tmp, fmreg + U3P_U2FREQ_FMCR0);
  307. /*disable free run clock */
  308. tmp = readl(fmreg + U3P_U2FREQ_FMMONR1);
  309. tmp &= ~P2F_RG_FRCK_EN;
  310. writel(tmp, fmreg + U3P_U2FREQ_FMMONR1);
  311. if (fm_out) {
  312. /* ( 1024 / FM_OUT ) x reference clock frequency x coef */
  313. tmp = tphy->src_ref_clk * tphy->src_coef;
  314. tmp = (tmp * U3P_FM_DET_CYCLE_CNT) / fm_out;
  315. calibration_val = DIV_ROUND_CLOSEST(tmp, U3P_SR_COEF_DIVISOR);
  316. } else {
  317. /* if FM detection fail, set default value */
  318. calibration_val = 4;
  319. }
  320. dev_dbg(tphy->dev, "phy:%d, fm_out:%d, calib:%d (clk:%d, coef:%d)\n",
  321. instance->index, fm_out, calibration_val,
  322. tphy->src_ref_clk, tphy->src_coef);
  323. /* set HS slew rate */
  324. tmp = readl(com + U3P_USBPHYACR5);
  325. tmp &= ~PA5_RG_U2_HSTX_SRCTRL;
  326. tmp |= PA5_RG_U2_HSTX_SRCTRL_VAL(calibration_val);
  327. writel(tmp, com + U3P_USBPHYACR5);
  328. /* disable USB ring oscillator */
  329. tmp = readl(com + U3P_USBPHYACR5);
  330. tmp &= ~PA5_RG_U2_HSTX_SRCAL_EN;
  331. writel(tmp, com + U3P_USBPHYACR5);
  332. }
  333. static void u3_phy_instance_init(struct mtk_tphy *tphy,
  334. struct mtk_phy_instance *instance)
  335. {
  336. struct u3phy_banks *u3_banks = &instance->u3_banks;
  337. u32 tmp;
  338. /* gating PCIe Analog XTAL clock */
  339. tmp = readl(u3_banks->spllc + U3P_SPLLC_XTALCTL3);
  340. tmp |= XC3_RG_U3_XTAL_RX_PWD | XC3_RG_U3_FRC_XTAL_RX_PWD;
  341. writel(tmp, u3_banks->spllc + U3P_SPLLC_XTALCTL3);
  342. /* gating XSQ */
  343. tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG0);
  344. tmp &= ~P3A_RG_XTAL_EXT_EN_U3;
  345. tmp |= P3A_RG_XTAL_EXT_EN_U3_VAL(2);
  346. writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG0);
  347. tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG9);
  348. tmp &= ~P3A_RG_RX_DAC_MUX;
  349. tmp |= P3A_RG_RX_DAC_MUX_VAL(4);
  350. writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG9);
  351. tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG6);
  352. tmp &= ~P3A_RG_TX_EIDLE_CM;
  353. tmp |= P3A_RG_TX_EIDLE_CM_VAL(0xe);
  354. writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG6);
  355. tmp = readl(u3_banks->phyd + U3P_U3_PHYD_CDR1);
  356. tmp &= ~(P3D_RG_CDR_BIR_LTD0 | P3D_RG_CDR_BIR_LTD1);
  357. tmp |= P3D_RG_CDR_BIR_LTD0_VAL(0xc) | P3D_RG_CDR_BIR_LTD1_VAL(0x3);
  358. writel(tmp, u3_banks->phyd + U3P_U3_PHYD_CDR1);
  359. tmp = readl(u3_banks->phyd + U3P_U3_PHYD_LFPS1);
  360. tmp &= ~P3D_RG_FWAKE_TH;
  361. tmp |= P3D_RG_FWAKE_TH_VAL(0x34);
  362. writel(tmp, u3_banks->phyd + U3P_U3_PHYD_LFPS1);
  363. tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET1);
  364. tmp &= ~P3D_RG_RXDET_STB2_SET;
  365. tmp |= P3D_RG_RXDET_STB2_SET_VAL(0x10);
  366. writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET1);
  367. tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET2);
  368. tmp &= ~P3D_RG_RXDET_STB2_SET_P3;
  369. tmp |= P3D_RG_RXDET_STB2_SET_P3_VAL(0x10);
  370. writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET2);
  371. dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
  372. }
  373. static void u2_phy_instance_init(struct mtk_tphy *tphy,
  374. struct mtk_phy_instance *instance)
  375. {
  376. struct u2phy_banks *u2_banks = &instance->u2_banks;
  377. void __iomem *com = u2_banks->com;
  378. u32 index = instance->index;
  379. u32 tmp;
  380. /* switch to USB function, and enable usb pll */
  381. tmp = readl(com + U3P_U2PHYDTM0);
  382. tmp &= ~(P2C_FORCE_UART_EN | P2C_FORCE_SUSPENDM);
  383. tmp |= P2C_RG_XCVRSEL_VAL(1) | P2C_RG_DATAIN_VAL(0);
  384. writel(tmp, com + U3P_U2PHYDTM0);
  385. tmp = readl(com + U3P_U2PHYDTM1);
  386. tmp &= ~P2C_RG_UART_EN;
  387. writel(tmp, com + U3P_U2PHYDTM1);
  388. tmp = readl(com + U3P_USBPHYACR0);
  389. tmp |= PA0_RG_USB20_INTR_EN;
  390. writel(tmp, com + U3P_USBPHYACR0);
  391. /* disable switch 100uA current to SSUSB */
  392. tmp = readl(com + U3P_USBPHYACR5);
  393. tmp &= ~PA5_RG_U2_HS_100U_U3_EN;
  394. writel(tmp, com + U3P_USBPHYACR5);
  395. if (!index) {
  396. tmp = readl(com + U3P_U2PHYACR4);
  397. tmp &= ~P2C_U2_GPIO_CTR_MSK;
  398. writel(tmp, com + U3P_U2PHYACR4);
  399. }
  400. if (tphy->pdata->avoid_rx_sen_degradation) {
  401. if (!index) {
  402. tmp = readl(com + U3P_USBPHYACR2);
  403. tmp |= PA2_RG_SIF_U2PLL_FORCE_EN;
  404. writel(tmp, com + U3P_USBPHYACR2);
  405. tmp = readl(com + U3D_U2PHYDCR0);
  406. tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
  407. writel(tmp, com + U3D_U2PHYDCR0);
  408. } else {
  409. tmp = readl(com + U3D_U2PHYDCR0);
  410. tmp |= P2C_RG_SIF_U2PLL_FORCE_ON;
  411. writel(tmp, com + U3D_U2PHYDCR0);
  412. tmp = readl(com + U3P_U2PHYDTM0);
  413. tmp |= P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM;
  414. writel(tmp, com + U3P_U2PHYDTM0);
  415. }
  416. }
  417. tmp = readl(com + U3P_USBPHYACR6);
  418. tmp &= ~PA6_RG_U2_BC11_SW_EN; /* DP/DM BC1.1 path Disable */
  419. tmp &= ~PA6_RG_U2_SQTH;
  420. tmp |= PA6_RG_U2_SQTH_VAL(2);
  421. writel(tmp, com + U3P_USBPHYACR6);
  422. dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
  423. }
  424. static void u2_phy_instance_power_on(struct mtk_tphy *tphy,
  425. struct mtk_phy_instance *instance)
  426. {
  427. struct u2phy_banks *u2_banks = &instance->u2_banks;
  428. void __iomem *com = u2_banks->com;
  429. u32 index = instance->index;
  430. u32 tmp;
  431. tmp = readl(com + U3P_U2PHYDTM0);
  432. tmp &= ~(P2C_RG_XCVRSEL | P2C_RG_DATAIN | P2C_DTM0_PART_MASK);
  433. writel(tmp, com + U3P_U2PHYDTM0);
  434. /* OTG Enable */
  435. tmp = readl(com + U3P_USBPHYACR6);
  436. tmp |= PA6_RG_U2_OTG_VBUSCMP_EN;
  437. writel(tmp, com + U3P_USBPHYACR6);
  438. tmp = readl(com + U3P_U2PHYDTM1);
  439. tmp |= P2C_RG_VBUSVALID | P2C_RG_AVALID;
  440. tmp &= ~P2C_RG_SESSEND;
  441. writel(tmp, com + U3P_U2PHYDTM1);
  442. if (tphy->pdata->avoid_rx_sen_degradation && index) {
  443. tmp = readl(com + U3D_U2PHYDCR0);
  444. tmp |= P2C_RG_SIF_U2PLL_FORCE_ON;
  445. writel(tmp, com + U3D_U2PHYDCR0);
  446. tmp = readl(com + U3P_U2PHYDTM0);
  447. tmp |= P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM;
  448. writel(tmp, com + U3P_U2PHYDTM0);
  449. }
  450. dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
  451. }
  452. static void u2_phy_instance_power_off(struct mtk_tphy *tphy,
  453. struct mtk_phy_instance *instance)
  454. {
  455. struct u2phy_banks *u2_banks = &instance->u2_banks;
  456. void __iomem *com = u2_banks->com;
  457. u32 index = instance->index;
  458. u32 tmp;
  459. tmp = readl(com + U3P_U2PHYDTM0);
  460. tmp &= ~(P2C_RG_XCVRSEL | P2C_RG_DATAIN);
  461. writel(tmp, com + U3P_U2PHYDTM0);
  462. /* OTG Disable */
  463. tmp = readl(com + U3P_USBPHYACR6);
  464. tmp &= ~PA6_RG_U2_OTG_VBUSCMP_EN;
  465. writel(tmp, com + U3P_USBPHYACR6);
  466. tmp = readl(com + U3P_U2PHYDTM1);
  467. tmp &= ~(P2C_RG_VBUSVALID | P2C_RG_AVALID);
  468. tmp |= P2C_RG_SESSEND;
  469. writel(tmp, com + U3P_U2PHYDTM1);
  470. if (tphy->pdata->avoid_rx_sen_degradation && index) {
  471. tmp = readl(com + U3P_U2PHYDTM0);
  472. tmp &= ~(P2C_RG_SUSPENDM | P2C_FORCE_SUSPENDM);
  473. writel(tmp, com + U3P_U2PHYDTM0);
  474. tmp = readl(com + U3D_U2PHYDCR0);
  475. tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
  476. writel(tmp, com + U3D_U2PHYDCR0);
  477. }
  478. dev_dbg(tphy->dev, "%s(%d)\n", __func__, index);
  479. }
  480. static void u2_phy_instance_exit(struct mtk_tphy *tphy,
  481. struct mtk_phy_instance *instance)
  482. {
  483. struct u2phy_banks *u2_banks = &instance->u2_banks;
  484. void __iomem *com = u2_banks->com;
  485. u32 index = instance->index;
  486. u32 tmp;
  487. if (tphy->pdata->avoid_rx_sen_degradation && index) {
  488. tmp = readl(com + U3D_U2PHYDCR0);
  489. tmp &= ~P2C_RG_SIF_U2PLL_FORCE_ON;
  490. writel(tmp, com + U3D_U2PHYDCR0);
  491. tmp = readl(com + U3P_U2PHYDTM0);
  492. tmp &= ~P2C_FORCE_SUSPENDM;
  493. writel(tmp, com + U3P_U2PHYDTM0);
  494. }
  495. }
  496. static void u2_phy_instance_set_mode(struct mtk_tphy *tphy,
  497. struct mtk_phy_instance *instance,
  498. enum phy_mode mode)
  499. {
  500. struct u2phy_banks *u2_banks = &instance->u2_banks;
  501. u32 tmp;
  502. tmp = readl(u2_banks->com + U3P_U2PHYDTM1);
  503. switch (mode) {
  504. case PHY_MODE_USB_DEVICE:
  505. tmp |= P2C_FORCE_IDDIG | P2C_RG_IDDIG;
  506. break;
  507. case PHY_MODE_USB_HOST:
  508. tmp |= P2C_FORCE_IDDIG;
  509. tmp &= ~P2C_RG_IDDIG;
  510. break;
  511. case PHY_MODE_USB_OTG:
  512. tmp &= ~(P2C_FORCE_IDDIG | P2C_RG_IDDIG);
  513. break;
  514. default:
  515. return;
  516. }
  517. writel(tmp, u2_banks->com + U3P_U2PHYDTM1);
  518. }
  519. static void pcie_phy_instance_init(struct mtk_tphy *tphy,
  520. struct mtk_phy_instance *instance)
  521. {
  522. struct u3phy_banks *u3_banks = &instance->u3_banks;
  523. u32 tmp;
  524. if (tphy->pdata->version != MTK_PHY_V1)
  525. return;
  526. tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG0);
  527. tmp &= ~(P3A_RG_XTAL_EXT_PE1H | P3A_RG_XTAL_EXT_PE2H);
  528. tmp |= P3A_RG_XTAL_EXT_PE1H_VAL(0x2) | P3A_RG_XTAL_EXT_PE2H_VAL(0x2);
  529. writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG0);
  530. /* ref clk drive */
  531. tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG1);
  532. tmp &= ~P3A_RG_CLKDRV_AMP;
  533. tmp |= P3A_RG_CLKDRV_AMP_VAL(0x4);
  534. writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG1);
  535. tmp = readl(u3_banks->phya + U3P_U3_PHYA_REG0);
  536. tmp &= ~P3A_RG_CLKDRV_OFF;
  537. tmp |= P3A_RG_CLKDRV_OFF_VAL(0x1);
  538. writel(tmp, u3_banks->phya + U3P_U3_PHYA_REG0);
  539. /* SSC delta -5000ppm */
  540. tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG20);
  541. tmp &= ~P3A_RG_PLL_DELTA1_PE2H;
  542. tmp |= P3A_RG_PLL_DELTA1_PE2H_VAL(0x3c);
  543. writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG20);
  544. tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG25);
  545. tmp &= ~P3A_RG_PLL_DELTA_PE2H;
  546. tmp |= P3A_RG_PLL_DELTA_PE2H_VAL(0x36);
  547. writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG25);
  548. /* change pll BW 0.6M */
  549. tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG5);
  550. tmp &= ~(P3A_RG_PLL_BR_PE2H | P3A_RG_PLL_IC_PE2H);
  551. tmp |= P3A_RG_PLL_BR_PE2H_VAL(0x1) | P3A_RG_PLL_IC_PE2H_VAL(0x1);
  552. writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG5);
  553. tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG4);
  554. tmp &= ~(P3A_RG_PLL_DIVEN_PE2H | P3A_RG_PLL_BC_PE2H);
  555. tmp |= P3A_RG_PLL_BC_PE2H_VAL(0x3);
  556. writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG4);
  557. tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG6);
  558. tmp &= ~P3A_RG_PLL_IR_PE2H;
  559. tmp |= P3A_RG_PLL_IR_PE2H_VAL(0x2);
  560. writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG6);
  561. tmp = readl(u3_banks->phya + U3P_U3_PHYA_DA_REG7);
  562. tmp &= ~P3A_RG_PLL_BP_PE2H;
  563. tmp |= P3A_RG_PLL_BP_PE2H_VAL(0xa);
  564. writel(tmp, u3_banks->phya + U3P_U3_PHYA_DA_REG7);
  565. /* Tx Detect Rx Timing: 10us -> 5us */
  566. tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET1);
  567. tmp &= ~P3D_RG_RXDET_STB2_SET;
  568. tmp |= P3D_RG_RXDET_STB2_SET_VAL(0x10);
  569. writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET1);
  570. tmp = readl(u3_banks->phyd + U3P_U3_PHYD_RXDET2);
  571. tmp &= ~P3D_RG_RXDET_STB2_SET_P3;
  572. tmp |= P3D_RG_RXDET_STB2_SET_P3_VAL(0x10);
  573. writel(tmp, u3_banks->phyd + U3P_U3_PHYD_RXDET2);
  574. /* wait for PCIe subsys register to active */
  575. usleep_range(2500, 3000);
  576. dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
  577. }
  578. static void pcie_phy_instance_power_on(struct mtk_tphy *tphy,
  579. struct mtk_phy_instance *instance)
  580. {
  581. struct u3phy_banks *bank = &instance->u3_banks;
  582. u32 tmp;
  583. tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD);
  584. tmp &= ~(P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST);
  585. writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLD);
  586. tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLE);
  587. tmp &= ~(P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD);
  588. writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLE);
  589. }
  590. static void pcie_phy_instance_power_off(struct mtk_tphy *tphy,
  591. struct mtk_phy_instance *instance)
  592. {
  593. struct u3phy_banks *bank = &instance->u3_banks;
  594. u32 tmp;
  595. tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLD);
  596. tmp |= P3C_FORCE_IP_SW_RST | P3C_REG_IP_SW_RST;
  597. writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLD);
  598. tmp = readl(bank->chip + U3P_U3_CHIP_GPIO_CTLE);
  599. tmp |= P3C_RG_SWRST_U3_PHYD_FORCE_EN | P3C_RG_SWRST_U3_PHYD;
  600. writel(tmp, bank->chip + U3P_U3_CHIP_GPIO_CTLE);
  601. }
  602. static void sata_phy_instance_init(struct mtk_tphy *tphy,
  603. struct mtk_phy_instance *instance)
  604. {
  605. struct u3phy_banks *u3_banks = &instance->u3_banks;
  606. void __iomem *phyd = u3_banks->phyd;
  607. u32 tmp;
  608. /* charge current adjustment */
  609. tmp = readl(phyd + ANA_RG_CTRL_SIGNAL6);
  610. tmp &= ~(RG_CDR_BIRLTR_GEN1_MSK | RG_CDR_BC_GEN1_MSK);
  611. tmp |= RG_CDR_BIRLTR_GEN1_VAL(0x6) | RG_CDR_BC_GEN1_VAL(0x1a);
  612. writel(tmp, phyd + ANA_RG_CTRL_SIGNAL6);
  613. tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL4);
  614. tmp &= ~RG_CDR_BIRLTD0_GEN1_MSK;
  615. tmp |= RG_CDR_BIRLTD0_GEN1_VAL(0x18);
  616. writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL4);
  617. tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL5);
  618. tmp &= ~RG_CDR_BIRLTD0_GEN3_MSK;
  619. tmp |= RG_CDR_BIRLTD0_GEN3_VAL(0x06);
  620. writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL5);
  621. tmp = readl(phyd + ANA_RG_CTRL_SIGNAL4);
  622. tmp &= ~(RG_CDR_BICLTR_GEN1_MSK | RG_CDR_BR_GEN2_MSK);
  623. tmp |= RG_CDR_BICLTR_GEN1_VAL(0x0c) | RG_CDR_BR_GEN2_VAL(0x07);
  624. writel(tmp, phyd + ANA_RG_CTRL_SIGNAL4);
  625. tmp = readl(phyd + PHYD_CTRL_SIGNAL_MODE4);
  626. tmp &= ~(RG_CDR_BICLTD0_GEN1_MSK | RG_CDR_BICLTD1_GEN1_MSK);
  627. tmp |= RG_CDR_BICLTD0_GEN1_VAL(0x08) | RG_CDR_BICLTD1_GEN1_VAL(0x02);
  628. writel(tmp, phyd + PHYD_CTRL_SIGNAL_MODE4);
  629. tmp = readl(phyd + PHYD_DESIGN_OPTION2);
  630. tmp &= ~RG_LOCK_CNT_SEL_MSK;
  631. tmp |= RG_LOCK_CNT_SEL_VAL(0x02);
  632. writel(tmp, phyd + PHYD_DESIGN_OPTION2);
  633. tmp = readl(phyd + PHYD_DESIGN_OPTION9);
  634. tmp &= ~(RG_T2_MIN_MSK | RG_TG_MIN_MSK |
  635. RG_T2_MAX_MSK | RG_TG_MAX_MSK);
  636. tmp |= RG_T2_MIN_VAL(0x12) | RG_TG_MIN_VAL(0x04) |
  637. RG_T2_MAX_VAL(0x31) | RG_TG_MAX_VAL(0x0e);
  638. writel(tmp, phyd + PHYD_DESIGN_OPTION9);
  639. tmp = readl(phyd + ANA_RG_CTRL_SIGNAL1);
  640. tmp &= ~RG_IDRV_0DB_GEN1_MSK;
  641. tmp |= RG_IDRV_0DB_GEN1_VAL(0x20);
  642. writel(tmp, phyd + ANA_RG_CTRL_SIGNAL1);
  643. tmp = readl(phyd + ANA_EQ_EYE_CTRL_SIGNAL1);
  644. tmp &= ~RG_EQ_DLEQ_LFI_GEN1_MSK;
  645. tmp |= RG_EQ_DLEQ_LFI_GEN1_VAL(0x03);
  646. writel(tmp, phyd + ANA_EQ_EYE_CTRL_SIGNAL1);
  647. dev_dbg(tphy->dev, "%s(%d)\n", __func__, instance->index);
  648. }
  649. static void phy_v1_banks_init(struct mtk_tphy *tphy,
  650. struct mtk_phy_instance *instance)
  651. {
  652. struct u2phy_banks *u2_banks = &instance->u2_banks;
  653. struct u3phy_banks *u3_banks = &instance->u3_banks;
  654. switch (instance->type) {
  655. case PHY_TYPE_USB2:
  656. u2_banks->misc = NULL;
  657. u2_banks->fmreg = tphy->sif_base + SSUSB_SIFSLV_V1_U2FREQ;
  658. u2_banks->com = instance->port_base + SSUSB_SIFSLV_V1_U2PHY_COM;
  659. break;
  660. case PHY_TYPE_USB3:
  661. case PHY_TYPE_PCIE:
  662. u3_banks->spllc = tphy->sif_base + SSUSB_SIFSLV_V1_SPLLC;
  663. u3_banks->chip = tphy->sif_base + SSUSB_SIFSLV_V1_CHIP;
  664. u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD;
  665. u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V1_U3PHYA;
  666. break;
  667. case PHY_TYPE_SATA:
  668. u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V1_U3PHYD;
  669. break;
  670. default:
  671. dev_err(tphy->dev, "incompatible PHY type\n");
  672. return;
  673. }
  674. }
  675. static void phy_v2_banks_init(struct mtk_tphy *tphy,
  676. struct mtk_phy_instance *instance)
  677. {
  678. struct u2phy_banks *u2_banks = &instance->u2_banks;
  679. struct u3phy_banks *u3_banks = &instance->u3_banks;
  680. switch (instance->type) {
  681. case PHY_TYPE_USB2:
  682. u2_banks->misc = instance->port_base + SSUSB_SIFSLV_V2_MISC;
  683. u2_banks->fmreg = instance->port_base + SSUSB_SIFSLV_V2_U2FREQ;
  684. u2_banks->com = instance->port_base + SSUSB_SIFSLV_V2_U2PHY_COM;
  685. break;
  686. case PHY_TYPE_USB3:
  687. case PHY_TYPE_PCIE:
  688. u3_banks->spllc = instance->port_base + SSUSB_SIFSLV_V2_SPLLC;
  689. u3_banks->chip = instance->port_base + SSUSB_SIFSLV_V2_CHIP;
  690. u3_banks->phyd = instance->port_base + SSUSB_SIFSLV_V2_U3PHYD;
  691. u3_banks->phya = instance->port_base + SSUSB_SIFSLV_V2_U3PHYA;
  692. break;
  693. default:
  694. dev_err(tphy->dev, "incompatible PHY type\n");
  695. return;
  696. }
  697. }
  698. static void phy_parse_property(struct mtk_tphy *tphy,
  699. struct mtk_phy_instance *instance)
  700. {
  701. struct device *dev = &instance->phy->dev;
  702. if (instance->type != PHY_TYPE_USB2)
  703. return;
  704. instance->bc12_en = device_property_read_bool(dev, "mediatek,bc12");
  705. device_property_read_u32(dev, "mediatek,eye-src",
  706. &instance->eye_src);
  707. device_property_read_u32(dev, "mediatek,eye-vrt",
  708. &instance->eye_vrt);
  709. device_property_read_u32(dev, "mediatek,eye-term",
  710. &instance->eye_term);
  711. dev_dbg(dev, "bc12:%d, src:%d, vrt:%d, term:%d\n",
  712. instance->bc12_en, instance->eye_src,
  713. instance->eye_vrt, instance->eye_term);
  714. }
  715. static void u2_phy_props_set(struct mtk_tphy *tphy,
  716. struct mtk_phy_instance *instance)
  717. {
  718. struct u2phy_banks *u2_banks = &instance->u2_banks;
  719. void __iomem *com = u2_banks->com;
  720. u32 tmp;
  721. if (instance->bc12_en) {
  722. tmp = readl(com + U3P_U2PHYBC12C);
  723. tmp |= P2C_RG_CHGDT_EN; /* BC1.2 path Enable */
  724. writel(tmp, com + U3P_U2PHYBC12C);
  725. }
  726. if (instance->eye_src) {
  727. tmp = readl(com + U3P_USBPHYACR5);
  728. tmp &= ~PA5_RG_U2_HSTX_SRCTRL;
  729. tmp |= PA5_RG_U2_HSTX_SRCTRL_VAL(instance->eye_src);
  730. writel(tmp, com + U3P_USBPHYACR5);
  731. }
  732. if (instance->eye_vrt) {
  733. tmp = readl(com + U3P_USBPHYACR1);
  734. tmp &= ~PA1_RG_VRT_SEL;
  735. tmp |= PA1_RG_VRT_SEL_VAL(instance->eye_vrt);
  736. writel(tmp, com + U3P_USBPHYACR1);
  737. }
  738. if (instance->eye_term) {
  739. tmp = readl(com + U3P_USBPHYACR1);
  740. tmp &= ~PA1_RG_TERM_SEL;
  741. tmp |= PA1_RG_TERM_SEL_VAL(instance->eye_term);
  742. writel(tmp, com + U3P_USBPHYACR1);
  743. }
  744. }
  745. static int mtk_phy_init(struct phy *phy)
  746. {
  747. struct mtk_phy_instance *instance = phy_get_drvdata(phy);
  748. struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
  749. int ret;
  750. ret = clk_prepare_enable(tphy->u3phya_ref);
  751. if (ret) {
  752. dev_err(tphy->dev, "failed to enable u3phya_ref\n");
  753. return ret;
  754. }
  755. ret = clk_prepare_enable(instance->ref_clk);
  756. if (ret) {
  757. dev_err(tphy->dev, "failed to enable ref_clk\n");
  758. return ret;
  759. }
  760. switch (instance->type) {
  761. case PHY_TYPE_USB2:
  762. u2_phy_instance_init(tphy, instance);
  763. u2_phy_props_set(tphy, instance);
  764. break;
  765. case PHY_TYPE_USB3:
  766. u3_phy_instance_init(tphy, instance);
  767. break;
  768. case PHY_TYPE_PCIE:
  769. pcie_phy_instance_init(tphy, instance);
  770. break;
  771. case PHY_TYPE_SATA:
  772. sata_phy_instance_init(tphy, instance);
  773. break;
  774. default:
  775. dev_err(tphy->dev, "incompatible PHY type\n");
  776. return -EINVAL;
  777. }
  778. return 0;
  779. }
  780. static int mtk_phy_power_on(struct phy *phy)
  781. {
  782. struct mtk_phy_instance *instance = phy_get_drvdata(phy);
  783. struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
  784. if (instance->type == PHY_TYPE_USB2) {
  785. u2_phy_instance_power_on(tphy, instance);
  786. hs_slew_rate_calibrate(tphy, instance);
  787. } else if (instance->type == PHY_TYPE_PCIE) {
  788. pcie_phy_instance_power_on(tphy, instance);
  789. }
  790. return 0;
  791. }
  792. static int mtk_phy_power_off(struct phy *phy)
  793. {
  794. struct mtk_phy_instance *instance = phy_get_drvdata(phy);
  795. struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
  796. if (instance->type == PHY_TYPE_USB2)
  797. u2_phy_instance_power_off(tphy, instance);
  798. else if (instance->type == PHY_TYPE_PCIE)
  799. pcie_phy_instance_power_off(tphy, instance);
  800. return 0;
  801. }
  802. static int mtk_phy_exit(struct phy *phy)
  803. {
  804. struct mtk_phy_instance *instance = phy_get_drvdata(phy);
  805. struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
  806. if (instance->type == PHY_TYPE_USB2)
  807. u2_phy_instance_exit(tphy, instance);
  808. clk_disable_unprepare(instance->ref_clk);
  809. clk_disable_unprepare(tphy->u3phya_ref);
  810. return 0;
  811. }
  812. static int mtk_phy_set_mode(struct phy *phy, enum phy_mode mode)
  813. {
  814. struct mtk_phy_instance *instance = phy_get_drvdata(phy);
  815. struct mtk_tphy *tphy = dev_get_drvdata(phy->dev.parent);
  816. if (instance->type == PHY_TYPE_USB2)
  817. u2_phy_instance_set_mode(tphy, instance, mode);
  818. return 0;
  819. }
  820. static struct phy *mtk_phy_xlate(struct device *dev,
  821. struct of_phandle_args *args)
  822. {
  823. struct mtk_tphy *tphy = dev_get_drvdata(dev);
  824. struct mtk_phy_instance *instance = NULL;
  825. struct device_node *phy_np = args->np;
  826. int index;
  827. if (args->args_count != 1) {
  828. dev_err(dev, "invalid number of cells in 'phy' property\n");
  829. return ERR_PTR(-EINVAL);
  830. }
  831. for (index = 0; index < tphy->nphys; index++)
  832. if (phy_np == tphy->phys[index]->phy->dev.of_node) {
  833. instance = tphy->phys[index];
  834. break;
  835. }
  836. if (!instance) {
  837. dev_err(dev, "failed to find appropriate phy\n");
  838. return ERR_PTR(-EINVAL);
  839. }
  840. instance->type = args->args[0];
  841. if (!(instance->type == PHY_TYPE_USB2 ||
  842. instance->type == PHY_TYPE_USB3 ||
  843. instance->type == PHY_TYPE_PCIE ||
  844. instance->type == PHY_TYPE_SATA)) {
  845. dev_err(dev, "unsupported device type: %d\n", instance->type);
  846. return ERR_PTR(-EINVAL);
  847. }
  848. if (tphy->pdata->version == MTK_PHY_V1) {
  849. phy_v1_banks_init(tphy, instance);
  850. } else if (tphy->pdata->version == MTK_PHY_V2) {
  851. phy_v2_banks_init(tphy, instance);
  852. } else {
  853. dev_err(dev, "phy version is not supported\n");
  854. return ERR_PTR(-EINVAL);
  855. }
  856. phy_parse_property(tphy, instance);
  857. return instance->phy;
  858. }
  859. static const struct phy_ops mtk_tphy_ops = {
  860. .init = mtk_phy_init,
  861. .exit = mtk_phy_exit,
  862. .power_on = mtk_phy_power_on,
  863. .power_off = mtk_phy_power_off,
  864. .set_mode = mtk_phy_set_mode,
  865. .owner = THIS_MODULE,
  866. };
  867. static const struct mtk_phy_pdata tphy_v1_pdata = {
  868. .avoid_rx_sen_degradation = false,
  869. .version = MTK_PHY_V1,
  870. };
  871. static const struct mtk_phy_pdata tphy_v2_pdata = {
  872. .avoid_rx_sen_degradation = false,
  873. .version = MTK_PHY_V2,
  874. };
  875. static const struct mtk_phy_pdata mt8173_pdata = {
  876. .avoid_rx_sen_degradation = true,
  877. .version = MTK_PHY_V1,
  878. };
  879. static const struct of_device_id mtk_tphy_id_table[] = {
  880. { .compatible = "mediatek,mt2701-u3phy", .data = &tphy_v1_pdata },
  881. { .compatible = "mediatek,mt2712-u3phy", .data = &tphy_v2_pdata },
  882. { .compatible = "mediatek,mt8173-u3phy", .data = &mt8173_pdata },
  883. { .compatible = "mediatek,generic-tphy-v1", .data = &tphy_v1_pdata },
  884. { .compatible = "mediatek,generic-tphy-v2", .data = &tphy_v2_pdata },
  885. { },
  886. };
  887. MODULE_DEVICE_TABLE(of, mtk_tphy_id_table);
  888. static int mtk_tphy_probe(struct platform_device *pdev)
  889. {
  890. struct device *dev = &pdev->dev;
  891. struct device_node *np = dev->of_node;
  892. struct device_node *child_np;
  893. struct phy_provider *provider;
  894. struct resource *sif_res;
  895. struct mtk_tphy *tphy;
  896. struct resource res;
  897. int port, retval;
  898. tphy = devm_kzalloc(dev, sizeof(*tphy), GFP_KERNEL);
  899. if (!tphy)
  900. return -ENOMEM;
  901. tphy->pdata = of_device_get_match_data(dev);
  902. if (!tphy->pdata)
  903. return -EINVAL;
  904. tphy->nphys = of_get_child_count(np);
  905. tphy->phys = devm_kcalloc(dev, tphy->nphys,
  906. sizeof(*tphy->phys), GFP_KERNEL);
  907. if (!tphy->phys)
  908. return -ENOMEM;
  909. tphy->dev = dev;
  910. platform_set_drvdata(pdev, tphy);
  911. sif_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  912. /* SATA phy of V1 needn't it if not shared with PCIe or USB */
  913. if (sif_res && tphy->pdata->version == MTK_PHY_V1) {
  914. /* get banks shared by multiple phys */
  915. tphy->sif_base = devm_ioremap_resource(dev, sif_res);
  916. if (IS_ERR(tphy->sif_base)) {
  917. dev_err(dev, "failed to remap sif regs\n");
  918. return PTR_ERR(tphy->sif_base);
  919. }
  920. }
  921. /* it's deprecated, make it optional for backward compatibility */
  922. tphy->u3phya_ref = devm_clk_get(dev, "u3phya_ref");
  923. if (IS_ERR(tphy->u3phya_ref)) {
  924. if (PTR_ERR(tphy->u3phya_ref) == -EPROBE_DEFER)
  925. return -EPROBE_DEFER;
  926. tphy->u3phya_ref = NULL;
  927. }
  928. tphy->src_ref_clk = U3P_REF_CLK;
  929. tphy->src_coef = U3P_SLEW_RATE_COEF;
  930. /* update parameters of slew rate calibrate if exist */
  931. device_property_read_u32(dev, "mediatek,src-ref-clk-mhz",
  932. &tphy->src_ref_clk);
  933. device_property_read_u32(dev, "mediatek,src-coef", &tphy->src_coef);
  934. port = 0;
  935. for_each_child_of_node(np, child_np) {
  936. struct mtk_phy_instance *instance;
  937. struct phy *phy;
  938. instance = devm_kzalloc(dev, sizeof(*instance), GFP_KERNEL);
  939. if (!instance) {
  940. retval = -ENOMEM;
  941. goto put_child;
  942. }
  943. tphy->phys[port] = instance;
  944. phy = devm_phy_create(dev, child_np, &mtk_tphy_ops);
  945. if (IS_ERR(phy)) {
  946. dev_err(dev, "failed to create phy\n");
  947. retval = PTR_ERR(phy);
  948. goto put_child;
  949. }
  950. retval = of_address_to_resource(child_np, 0, &res);
  951. if (retval) {
  952. dev_err(dev, "failed to get address resource(id-%d)\n",
  953. port);
  954. goto put_child;
  955. }
  956. instance->port_base = devm_ioremap_resource(&phy->dev, &res);
  957. if (IS_ERR(instance->port_base)) {
  958. dev_err(dev, "failed to remap phy regs\n");
  959. retval = PTR_ERR(instance->port_base);
  960. goto put_child;
  961. }
  962. instance->phy = phy;
  963. instance->index = port;
  964. phy_set_drvdata(phy, instance);
  965. port++;
  966. /* if deprecated clock is provided, ignore instance's one */
  967. if (tphy->u3phya_ref)
  968. continue;
  969. instance->ref_clk = devm_clk_get(&phy->dev, "ref");
  970. if (IS_ERR(instance->ref_clk)) {
  971. dev_err(dev, "failed to get ref_clk(id-%d)\n", port);
  972. retval = PTR_ERR(instance->ref_clk);
  973. goto put_child;
  974. }
  975. }
  976. provider = devm_of_phy_provider_register(dev, mtk_phy_xlate);
  977. return PTR_ERR_OR_ZERO(provider);
  978. put_child:
  979. of_node_put(child_np);
  980. return retval;
  981. }
  982. static struct platform_driver mtk_tphy_driver = {
  983. .probe = mtk_tphy_probe,
  984. .driver = {
  985. .name = "mtk-tphy",
  986. .of_match_table = mtk_tphy_id_table,
  987. },
  988. };
  989. module_platform_driver(mtk_tphy_driver);
  990. MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
  991. MODULE_DESCRIPTION("MediaTek T-PHY driver");
  992. MODULE_LICENSE("GPL v2");