344-v5.18-02-phy-marvell-phy-mvebu-a3700-comphy-Add-native-kernel.patch 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  1. From 934337080c6c59b75db76b180b509f218640ad48 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]>
  3. Date: Thu, 3 Feb 2022 22:44:41 +0100
  4. Subject: [PATCH 2/2] phy: marvell: phy-mvebu-a3700-comphy: Add native kernel
  5. implementation
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. Remove old RPC implementation and add a new native kernel implementation.
  10. The old implementation uses ARM SMC API to issue RPC calls to ARM Trusted
  11. Firmware which provides real implementation of PHY configuration.
  12. But older versions of ARM Trusted Firmware do not provide this PHY
  13. configuration functionality, simply returning: operation not supported; or
  14. worse, some versions provide the configuration functionality incorrectly.
  15. For example the firmware shipped in ESPRESSObin board has this older
  16. version of ARM Trusted Firmware and therefore SATA, USB 3.0 and PCIe
  17. functionality do not work with newer versions of Linux kernel.
  18. Due to the above reasons, the following commits were introduced into Linux,
  19. to workaround these issues by ignoring -EOPNOTSUPP error code from
  20. phy-mvebu-a3700-comphy driver function phy_power_on():
  21. commit 45aefe3d2251 ("ata: ahci: mvebu: Make SATA PHY optional for Armada
  22. 3720")
  23. commit 3241929b67d2 ("usb: host: xhci: mvebu: make USB 3.0 PHY optional for
  24. Armada 3720")
  25. commit b0c6ae0f8948 ("PCI: aardvark: Fix initialization with old Marvell's
  26. Arm Trusted Firmware")
  27. Replace this RPC implementation with proper native kernel implementation,
  28. which is independent on the firmware. Never return -EOPNOTSUPP for proper
  29. arguments.
  30. This should solve multiple issues with real-world boards, where it is not
  31. possible or really inconvenient to change the firmware. Let's eliminate
  32. these issues.
  33. This implementation is ported directly from Armada 3720 comphy driver found
  34. in newest version of ARM Trusted Firmware source code, but with various
  35. fixes of register names, some added comments, some refactoring due to the
  36. original code not conforming to kernel standards. Also PCIe mode poweroff
  37. support was added here, and PHY reset support. These changes are also going
  38. to be sent to ARM Trusted Firmware.
  39. [ Pali did the porting from ATF.
  40. I (Marek) then fixed some register names, some various other things,
  41. added some comments and refactored the code to kernel standards. Also
  42. fixed PHY poweroff and added PHY reset. ]
  43. Signed-off-by: Pali Rohár <[email protected]>
  44. Acked-by: Miquel Raynal <[email protected]>
  45. Signed-off-by: Marek Behún <[email protected]>
  46. Link: https://lore.kernel.org/r/[email protected]
  47. Signed-off-by: Vinod Koul <[email protected]>
  48. ---
  49. drivers/phy/marvell/phy-mvebu-a3700-comphy.c | 1332 ++++++++++++++++--
  50. 1 file changed, 1215 insertions(+), 117 deletions(-)
  51. --- a/drivers/phy/marvell/phy-mvebu-a3700-comphy.c
  52. +++ b/drivers/phy/marvell/phy-mvebu-a3700-comphy.c
  53. @@ -5,12 +5,16 @@
  54. * Authors:
  55. * Evan Wang <[email protected]>
  56. * Miquèl Raynal <[email protected]>
  57. + * Pali Rohár <[email protected]>
  58. + * Marek Behún <[email protected]>
  59. *
  60. * Structure inspired from phy-mvebu-cp110-comphy.c written by Antoine Tenart.
  61. - * SMC call initial support done by Grzegorz Jaszczyk.
  62. + * Comphy code from ARM Trusted Firmware ported by Pali Rohár <[email protected]>
  63. + * and Marek Behún <[email protected]>.
  64. */
  65. -#include <linux/arm-smccc.h>
  66. +#include <linux/bitfield.h>
  67. +#include <linux/clk.h>
  68. #include <linux/io.h>
  69. #include <linux/iopoll.h>
  70. #include <linux/mfd/syscon.h>
  71. @@ -18,103 +22,1118 @@
  72. #include <linux/phy.h>
  73. #include <linux/phy/phy.h>
  74. #include <linux/platform_device.h>
  75. +#include <linux/spinlock.h>
  76. -#define MVEBU_A3700_COMPHY_LANES 3
  77. +#define PLL_SET_DELAY_US 600
  78. +#define COMPHY_PLL_SLEEP 1000
  79. +#define COMPHY_PLL_TIMEOUT 150000
  80. +
  81. +/* Comphy lane2 indirect access register offset */
  82. +#define COMPHY_LANE2_INDIR_ADDR 0x0
  83. +#define COMPHY_LANE2_INDIR_DATA 0x4
  84. +
  85. +/* SATA and USB3 PHY offset compared to SATA PHY */
  86. +#define COMPHY_LANE2_REGS_BASE 0x200
  87. +
  88. +/*
  89. + * When accessing common PHY lane registers directly, we need to shift by 1,
  90. + * since the registers are 16-bit.
  91. + */
  92. +#define COMPHY_LANE_REG_DIRECT(reg) (((reg) & 0x7FF) << 1)
  93. +
  94. +/* COMPHY registers */
  95. +#define COMPHY_POWER_PLL_CTRL 0x01
  96. +#define PU_IVREF_BIT BIT(15)
  97. +#define PU_PLL_BIT BIT(14)
  98. +#define PU_RX_BIT BIT(13)
  99. +#define PU_TX_BIT BIT(12)
  100. +#define PU_TX_INTP_BIT BIT(11)
  101. +#define PU_DFE_BIT BIT(10)
  102. +#define RESET_DTL_RX_BIT BIT(9)
  103. +#define PLL_LOCK_BIT BIT(8)
  104. +#define REF_FREF_SEL_MASK GENMASK(4, 0)
  105. +#define REF_FREF_SEL_SERDES_25MHZ FIELD_PREP(REF_FREF_SEL_MASK, 0x1)
  106. +#define REF_FREF_SEL_SERDES_40MHZ FIELD_PREP(REF_FREF_SEL_MASK, 0x3)
  107. +#define REF_FREF_SEL_SERDES_50MHZ FIELD_PREP(REF_FREF_SEL_MASK, 0x4)
  108. +#define REF_FREF_SEL_PCIE_USB3_25MHZ FIELD_PREP(REF_FREF_SEL_MASK, 0x2)
  109. +#define REF_FREF_SEL_PCIE_USB3_40MHZ FIELD_PREP(REF_FREF_SEL_MASK, 0x3)
  110. +#define COMPHY_MODE_MASK GENMASK(7, 5)
  111. +#define COMPHY_MODE_SATA FIELD_PREP(COMPHY_MODE_MASK, 0x0)
  112. +#define COMPHY_MODE_PCIE FIELD_PREP(COMPHY_MODE_MASK, 0x3)
  113. +#define COMPHY_MODE_SERDES FIELD_PREP(COMPHY_MODE_MASK, 0x4)
  114. +#define COMPHY_MODE_USB3 FIELD_PREP(COMPHY_MODE_MASK, 0x5)
  115. +
  116. +#define COMPHY_KVCO_CAL_CTRL 0x02
  117. +#define USE_MAX_PLL_RATE_BIT BIT(12)
  118. +#define SPEED_PLL_MASK GENMASK(7, 2)
  119. +#define SPEED_PLL_VALUE_16 FIELD_PREP(SPEED_PLL_MASK, 0x10)
  120. +
  121. +#define COMPHY_DIG_LOOPBACK_EN 0x23
  122. +#define SEL_DATA_WIDTH_MASK GENMASK(11, 10)
  123. +#define DATA_WIDTH_10BIT FIELD_PREP(SEL_DATA_WIDTH_MASK, 0x0)
  124. +#define DATA_WIDTH_20BIT FIELD_PREP(SEL_DATA_WIDTH_MASK, 0x1)
  125. +#define DATA_WIDTH_40BIT FIELD_PREP(SEL_DATA_WIDTH_MASK, 0x2)
  126. +#define PLL_READY_TX_BIT BIT(4)
  127. +
  128. +#define COMPHY_SYNC_PATTERN 0x24
  129. +#define TXD_INVERT_BIT BIT(10)
  130. +#define RXD_INVERT_BIT BIT(11)
  131. +
  132. +#define COMPHY_SYNC_MASK_GEN 0x25
  133. +#define PHY_GEN_MAX_MASK GENMASK(11, 10)
  134. +#define PHY_GEN_MAX_USB3_5G FIELD_PREP(PHY_GEN_MAX_MASK, 0x1)
  135. +
  136. +#define COMPHY_ISOLATION_CTRL 0x26
  137. +#define PHY_ISOLATE_MODE BIT(15)
  138. +
  139. +#define COMPHY_GEN2_SET2 0x3e
  140. +#define GS2_TX_SSC_AMP_MASK GENMASK(15, 9)
  141. +#define GS2_TX_SSC_AMP_4128 FIELD_PREP(GS2_TX_SSC_AMP_MASK, 0x20)
  142. +#define GS2_VREG_RXTX_MAS_ISET_MASK GENMASK(8, 7)
  143. +#define GS2_VREG_RXTX_MAS_ISET_60U FIELD_PREP(GS2_VREG_RXTX_MAS_ISET_MASK,\
  144. + 0x0)
  145. +#define GS2_VREG_RXTX_MAS_ISET_80U FIELD_PREP(GS2_VREG_RXTX_MAS_ISET_MASK,\
  146. + 0x1)
  147. +#define GS2_VREG_RXTX_MAS_ISET_100U FIELD_PREP(GS2_VREG_RXTX_MAS_ISET_MASK,\
  148. + 0x2)
  149. +#define GS2_VREG_RXTX_MAS_ISET_120U FIELD_PREP(GS2_VREG_RXTX_MAS_ISET_MASK,\
  150. + 0x3)
  151. +#define GS2_RSVD_6_0_MASK GENMASK(6, 0)
  152. +
  153. +#define COMPHY_GEN3_SET2 0x3f
  154. +
  155. +#define COMPHY_IDLE_SYNC_EN 0x48
  156. +#define IDLE_SYNC_EN BIT(12)
  157. +
  158. +#define COMPHY_MISC_CTRL0 0x4F
  159. +#define CLK100M_125M_EN BIT(4)
  160. +#define TXDCLK_2X_SEL BIT(6)
  161. +#define CLK500M_EN BIT(7)
  162. +#define PHY_REF_CLK_SEL BIT(10)
  163. +
  164. +#define COMPHY_SFT_RESET 0x52
  165. +#define SFT_RST BIT(9)
  166. +#define SFT_RST_NO_REG BIT(10)
  167. +
  168. +#define COMPHY_MISC_CTRL1 0x73
  169. +#define SEL_BITS_PCIE_FORCE BIT(15)
  170. +
  171. +#define COMPHY_GEN2_SET3 0x112
  172. +#define GS3_FFE_CAP_SEL_MASK GENMASK(3, 0)
  173. +#define GS3_FFE_CAP_SEL_VALUE FIELD_PREP(GS3_FFE_CAP_SEL_MASK, 0xF)
  174. +
  175. +/* PIPE registers */
  176. +#define COMPHY_PIPE_LANE_CFG0 0x180
  177. +#define PRD_TXDEEMPH0_MASK BIT(0)
  178. +#define PRD_TXMARGIN_MASK GENMASK(3, 1)
  179. +#define PRD_TXSWING_MASK BIT(4)
  180. +#define CFG_TX_ALIGN_POS_MASK GENMASK(8, 5)
  181. +
  182. +#define COMPHY_PIPE_LANE_CFG1 0x181
  183. +#define PRD_TXDEEMPH1_MASK BIT(15)
  184. +#define USE_MAX_PLL_RATE_EN BIT(9)
  185. +#define TX_DET_RX_MODE BIT(6)
  186. +#define GEN2_TX_DATA_DLY_MASK GENMASK(4, 3)
  187. +#define GEN2_TX_DATA_DLY_DEFT FIELD_PREP(GEN2_TX_DATA_DLY_MASK, 2)
  188. +#define TX_ELEC_IDLE_MODE_EN BIT(0)
  189. +
  190. +#define COMPHY_PIPE_LANE_STAT1 0x183
  191. +#define TXDCLK_PCLK_EN BIT(0)
  192. +
  193. +#define COMPHY_PIPE_LANE_CFG4 0x188
  194. +#define SPREAD_SPECTRUM_CLK_EN BIT(7)
  195. +
  196. +#define COMPHY_PIPE_RST_CLK_CTRL 0x1C1
  197. +#define PIPE_SOFT_RESET BIT(0)
  198. +#define PIPE_REG_RESET BIT(1)
  199. +#define MODE_CORE_CLK_FREQ_SEL BIT(9)
  200. +#define MODE_PIPE_WIDTH_32 BIT(3)
  201. +#define MODE_REFDIV_MASK GENMASK(5, 4)
  202. +#define MODE_REFDIV_BY_4 FIELD_PREP(MODE_REFDIV_MASK, 0x2)
  203. +
  204. +#define COMPHY_PIPE_TEST_MODE_CTRL 0x1C2
  205. +#define MODE_MARGIN_OVERRIDE BIT(2)
  206. +
  207. +#define COMPHY_PIPE_CLK_SRC_LO 0x1C3
  208. +#define MODE_CLK_SRC BIT(0)
  209. +#define BUNDLE_PERIOD_SEL BIT(1)
  210. +#define BUNDLE_PERIOD_SCALE_MASK GENMASK(3, 2)
  211. +#define BUNDLE_SAMPLE_CTRL BIT(4)
  212. +#define PLL_READY_DLY_MASK GENMASK(7, 5)
  213. +#define CFG_SEL_20B BIT(15)
  214. +
  215. +#define COMPHY_PIPE_PWR_MGM_TIM1 0x1D0
  216. +#define CFG_PM_OSCCLK_WAIT_MASK GENMASK(15, 12)
  217. +#define CFG_PM_RXDEN_WAIT_MASK GENMASK(11, 8)
  218. +#define CFG_PM_RXDEN_WAIT_1_UNIT FIELD_PREP(CFG_PM_RXDEN_WAIT_MASK, 0x1)
  219. +#define CFG_PM_RXDLOZ_WAIT_MASK GENMASK(7, 0)
  220. +#define CFG_PM_RXDLOZ_WAIT_7_UNIT FIELD_PREP(CFG_PM_RXDLOZ_WAIT_MASK, 0x7)
  221. +#define CFG_PM_RXDLOZ_WAIT_12_UNIT FIELD_PREP(CFG_PM_RXDLOZ_WAIT_MASK, 0xC)
  222. +
  223. +/*
  224. + * This register is not from PHY lane register space. It only exists in the
  225. + * indirect register space, before the actual PHY lane 2 registers. So the
  226. + * offset is absolute, not relative to COMPHY_LANE2_REGS_BASE.
  227. + * It is used only for SATA PHY initialization.
  228. + */
  229. +#define COMPHY_RESERVED_REG 0x0E
  230. +#define PHYCTRL_FRM_PIN_BIT BIT(13)
  231. -/* COMPHY Fast SMC function identifiers */
  232. -#define COMPHY_SIP_POWER_ON 0x82000001
  233. -#define COMPHY_SIP_POWER_OFF 0x82000002
  234. -#define COMPHY_SIP_PLL_LOCK 0x82000003
  235. -
  236. -#define COMPHY_FW_MODE_SATA 0x1
  237. -#define COMPHY_FW_MODE_SGMII 0x2
  238. -#define COMPHY_FW_MODE_2500BASEX 0x3
  239. -#define COMPHY_FW_MODE_USB3H 0x4
  240. -#define COMPHY_FW_MODE_USB3D 0x5
  241. -#define COMPHY_FW_MODE_PCIE 0x6
  242. -#define COMPHY_FW_MODE_USB3 0xa
  243. -
  244. -#define COMPHY_FW_SPEED_1_25G 0 /* SGMII 1G */
  245. -#define COMPHY_FW_SPEED_2_5G 1
  246. -#define COMPHY_FW_SPEED_3_125G 2 /* 2500BASE-X */
  247. -#define COMPHY_FW_SPEED_5G 3
  248. -#define COMPHY_FW_SPEED_MAX 0x3F
  249. -
  250. -#define COMPHY_FW_MODE(mode) ((mode) << 12)
  251. -#define COMPHY_FW_NET(mode, idx, speed) (COMPHY_FW_MODE(mode) | \
  252. - ((idx) << 8) | \
  253. - ((speed) << 2))
  254. -#define COMPHY_FW_PCIE(mode, speed, width) (COMPHY_FW_NET(mode, 0, speed) | \
  255. - ((width) << 18))
  256. +/* South Bridge PHY Configuration Registers */
  257. +#define COMPHY_PHY_REG(lane, reg) (((1 - (lane)) * 0x28) + ((reg) & 0x3f))
  258. +
  259. +/*
  260. + * lane0: USB3/GbE1 PHY Configuration 1
  261. + * lane1: PCIe/GbE0 PHY Configuration 1
  262. + * (used only by SGMII code)
  263. + */
  264. +#define COMPHY_PHY_CFG1 0x0
  265. +#define PIN_PU_IVREF_BIT BIT(1)
  266. +#define PIN_RESET_CORE_BIT BIT(11)
  267. +#define PIN_RESET_COMPHY_BIT BIT(12)
  268. +#define PIN_PU_PLL_BIT BIT(16)
  269. +#define PIN_PU_RX_BIT BIT(17)
  270. +#define PIN_PU_TX_BIT BIT(18)
  271. +#define PIN_TX_IDLE_BIT BIT(19)
  272. +#define GEN_RX_SEL_MASK GENMASK(25, 22)
  273. +#define GEN_RX_SEL_VALUE(val) FIELD_PREP(GEN_RX_SEL_MASK, (val))
  274. +#define GEN_TX_SEL_MASK GENMASK(29, 26)
  275. +#define GEN_TX_SEL_VALUE(val) FIELD_PREP(GEN_TX_SEL_MASK, (val))
  276. +#define SERDES_SPEED_1_25_G 0x6
  277. +#define SERDES_SPEED_3_125_G 0x8
  278. +#define PHY_RX_INIT_BIT BIT(30)
  279. +
  280. +/*
  281. + * lane0: USB3/GbE1 PHY Status 1
  282. + * lane1: PCIe/GbE0 PHY Status 1
  283. + * (used only by SGMII code)
  284. + */
  285. +#define COMPHY_PHY_STAT1 0x18
  286. +#define PHY_RX_INIT_DONE_BIT BIT(0)
  287. +#define PHY_PLL_READY_RX_BIT BIT(2)
  288. +#define PHY_PLL_READY_TX_BIT BIT(3)
  289. +
  290. +/* PHY Selector */
  291. +#define COMPHY_SELECTOR_PHY_REG 0xFC
  292. +/* bit0: 0: Lane1 is GbE0; 1: Lane1 is PCIe */
  293. +#define COMPHY_SELECTOR_PCIE_GBE0_SEL_BIT BIT(0)
  294. +/* bit4: 0: Lane0 is GbE1; 1: Lane0 is USB3 */
  295. +#define COMPHY_SELECTOR_USB3_GBE1_SEL_BIT BIT(4)
  296. +/* bit8: 0: Lane0 is USB3 instead of GbE1, Lane2 is SATA; 1: Lane2 is USB3 */
  297. +#define COMPHY_SELECTOR_USB3_PHY_SEL_BIT BIT(8)
  298. struct mvebu_a3700_comphy_conf {
  299. unsigned int lane;
  300. enum phy_mode mode;
  301. int submode;
  302. - u32 fw_mode;
  303. };
  304. -#define MVEBU_A3700_COMPHY_CONF(_lane, _mode, _smode, _fw) \
  305. +#define MVEBU_A3700_COMPHY_CONF(_lane, _mode, _smode) \
  306. { \
  307. .lane = _lane, \
  308. .mode = _mode, \
  309. .submode = _smode, \
  310. - .fw_mode = _fw, \
  311. }
  312. -#define MVEBU_A3700_COMPHY_CONF_GEN(_lane, _mode, _fw) \
  313. - MVEBU_A3700_COMPHY_CONF(_lane, _mode, PHY_INTERFACE_MODE_NA, _fw)
  314. +#define MVEBU_A3700_COMPHY_CONF_GEN(_lane, _mode) \
  315. + MVEBU_A3700_COMPHY_CONF(_lane, _mode, PHY_INTERFACE_MODE_NA)
  316. -#define MVEBU_A3700_COMPHY_CONF_ETH(_lane, _smode, _fw) \
  317. - MVEBU_A3700_COMPHY_CONF(_lane, PHY_MODE_ETHERNET, _smode, _fw)
  318. +#define MVEBU_A3700_COMPHY_CONF_ETH(_lane, _smode) \
  319. + MVEBU_A3700_COMPHY_CONF(_lane, PHY_MODE_ETHERNET, _smode)
  320. static const struct mvebu_a3700_comphy_conf mvebu_a3700_comphy_modes[] = {
  321. /* lane 0 */
  322. - MVEBU_A3700_COMPHY_CONF_GEN(0, PHY_MODE_USB_HOST_SS,
  323. - COMPHY_FW_MODE_USB3H),
  324. - MVEBU_A3700_COMPHY_CONF_ETH(0, PHY_INTERFACE_MODE_SGMII,
  325. - COMPHY_FW_MODE_SGMII),
  326. - MVEBU_A3700_COMPHY_CONF_ETH(0, PHY_INTERFACE_MODE_2500BASEX,
  327. - COMPHY_FW_MODE_2500BASEX),
  328. + MVEBU_A3700_COMPHY_CONF_GEN(0, PHY_MODE_USB_HOST_SS),
  329. + MVEBU_A3700_COMPHY_CONF_ETH(0, PHY_INTERFACE_MODE_SGMII),
  330. + MVEBU_A3700_COMPHY_CONF_ETH(0, PHY_INTERFACE_MODE_1000BASEX),
  331. + MVEBU_A3700_COMPHY_CONF_ETH(0, PHY_INTERFACE_MODE_2500BASEX),
  332. /* lane 1 */
  333. - MVEBU_A3700_COMPHY_CONF_GEN(1, PHY_MODE_PCIE, COMPHY_FW_MODE_PCIE),
  334. - MVEBU_A3700_COMPHY_CONF_ETH(1, PHY_INTERFACE_MODE_SGMII,
  335. - COMPHY_FW_MODE_SGMII),
  336. - MVEBU_A3700_COMPHY_CONF_ETH(1, PHY_INTERFACE_MODE_2500BASEX,
  337. - COMPHY_FW_MODE_2500BASEX),
  338. + MVEBU_A3700_COMPHY_CONF_GEN(1, PHY_MODE_PCIE),
  339. + MVEBU_A3700_COMPHY_CONF_ETH(1, PHY_INTERFACE_MODE_SGMII),
  340. + MVEBU_A3700_COMPHY_CONF_ETH(1, PHY_INTERFACE_MODE_1000BASEX),
  341. + MVEBU_A3700_COMPHY_CONF_ETH(1, PHY_INTERFACE_MODE_2500BASEX),
  342. /* lane 2 */
  343. - MVEBU_A3700_COMPHY_CONF_GEN(2, PHY_MODE_SATA, COMPHY_FW_MODE_SATA),
  344. - MVEBU_A3700_COMPHY_CONF_GEN(2, PHY_MODE_USB_HOST_SS,
  345. - COMPHY_FW_MODE_USB3H),
  346. + MVEBU_A3700_COMPHY_CONF_GEN(2, PHY_MODE_SATA),
  347. + MVEBU_A3700_COMPHY_CONF_GEN(2, PHY_MODE_USB_HOST_SS),
  348. +};
  349. +
  350. +struct mvebu_a3700_comphy_priv {
  351. + void __iomem *comphy_regs;
  352. + void __iomem *lane0_phy_regs; /* USB3 and GbE1 */
  353. + void __iomem *lane1_phy_regs; /* PCIe and GbE0 */
  354. + void __iomem *lane2_phy_indirect; /* SATA and USB3 */
  355. + spinlock_t lock; /* for PHY selector access */
  356. + bool xtal_is_40m;
  357. };
  358. struct mvebu_a3700_comphy_lane {
  359. + struct mvebu_a3700_comphy_priv *priv;
  360. struct device *dev;
  361. unsigned int id;
  362. enum phy_mode mode;
  363. int submode;
  364. + bool invert_tx;
  365. + bool invert_rx;
  366. + bool needs_reset;
  367. +};
  368. +
  369. +struct gbe_phy_init_data_fix {
  370. + u16 addr;
  371. + u16 value;
  372. +};
  373. +
  374. +/* Changes to 40M1G25 mode data required for running 40M3G125 init mode */
  375. +static struct gbe_phy_init_data_fix gbe_phy_init_fix[] = {
  376. + { 0x005, 0x07CC }, { 0x015, 0x0000 }, { 0x01B, 0x0000 },
  377. + { 0x01D, 0x0000 }, { 0x01E, 0x0000 }, { 0x01F, 0x0000 },
  378. + { 0x020, 0x0000 }, { 0x021, 0x0030 }, { 0x026, 0x0888 },
  379. + { 0x04D, 0x0152 }, { 0x04F, 0xA020 }, { 0x050, 0x07CC },
  380. + { 0x053, 0xE9CA }, { 0x055, 0xBD97 }, { 0x071, 0x3015 },
  381. + { 0x076, 0x03AA }, { 0x07C, 0x0FDF }, { 0x0C2, 0x3030 },
  382. + { 0x0C3, 0x8000 }, { 0x0E2, 0x5550 }, { 0x0E3, 0x12A4 },
  383. + { 0x0E4, 0x7D00 }, { 0x0E6, 0x0C83 }, { 0x101, 0xFCC0 },
  384. + { 0x104, 0x0C10 }
  385. };
  386. -static int mvebu_a3700_comphy_smc(unsigned long function, unsigned long lane,
  387. - unsigned long mode)
  388. +/* 40M1G25 mode init data */
  389. +static u16 gbe_phy_init[512] = {
  390. + /* 0 1 2 3 4 5 6 7 */
  391. + /*-----------------------------------------------------------*/
  392. + /* 8 9 A B C D E F */
  393. + 0x3110, 0xFD83, 0x6430, 0x412F, 0x82C0, 0x06FA, 0x4500, 0x6D26, /* 00 */
  394. + 0xAFC0, 0x8000, 0xC000, 0x0000, 0x2000, 0x49CC, 0x0BC9, 0x2A52, /* 08 */
  395. + 0x0BD2, 0x0CDE, 0x13D2, 0x0CE8, 0x1149, 0x10E0, 0x0000, 0x0000, /* 10 */
  396. + 0x0000, 0x0000, 0x0000, 0x0001, 0x0000, 0x4134, 0x0D2D, 0xFFFF, /* 18 */
  397. + 0xFFE0, 0x4030, 0x1016, 0x0030, 0x0000, 0x0800, 0x0866, 0x0000, /* 20 */
  398. + 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, /* 28 */
  399. + 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 30 */
  400. + 0x0000, 0x0000, 0x000F, 0x6A62, 0x1988, 0x3100, 0x3100, 0x3100, /* 38 */
  401. + 0x3100, 0xA708, 0x2430, 0x0830, 0x1030, 0x4610, 0xFF00, 0xFF00, /* 40 */
  402. + 0x0060, 0x1000, 0x0400, 0x0040, 0x00F0, 0x0155, 0x1100, 0xA02A, /* 48 */
  403. + 0x06FA, 0x0080, 0xB008, 0xE3ED, 0x5002, 0xB592, 0x7A80, 0x0001, /* 50 */
  404. + 0x020A, 0x8820, 0x6014, 0x8054, 0xACAA, 0xFC88, 0x2A02, 0x45CF, /* 58 */
  405. + 0x000F, 0x1817, 0x2860, 0x064F, 0x0000, 0x0204, 0x1800, 0x6000, /* 60 */
  406. + 0x810F, 0x4F23, 0x4000, 0x4498, 0x0850, 0x0000, 0x000E, 0x1002, /* 68 */
  407. + 0x9D3A, 0x3009, 0xD066, 0x0491, 0x0001, 0x6AB0, 0x0399, 0x3780, /* 70 */
  408. + 0x0040, 0x5AC0, 0x4A80, 0x0000, 0x01DF, 0x0000, 0x0007, 0x0000, /* 78 */
  409. + 0x2D54, 0x00A1, 0x4000, 0x0100, 0xA20A, 0x0000, 0x0000, 0x0000, /* 80 */
  410. + 0x0000, 0x0000, 0x0000, 0x7400, 0x0E81, 0x1000, 0x1242, 0x0210, /* 88 */
  411. + 0x80DF, 0x0F1F, 0x2F3F, 0x4F5F, 0x6F7F, 0x0F1F, 0x2F3F, 0x4F5F, /* 90 */
  412. + 0x6F7F, 0x4BAD, 0x0000, 0x0000, 0x0800, 0x0000, 0x2400, 0xB651, /* 98 */
  413. + 0xC9E0, 0x4247, 0x0A24, 0x0000, 0xAF19, 0x1004, 0x0000, 0x0000, /* A0 */
  414. + 0x0000, 0x0013, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* A8 */
  415. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* B0 */
  416. + 0x0000, 0x0000, 0x0000, 0x0060, 0x0000, 0x0000, 0x0000, 0x0000, /* B8 */
  417. + 0x0000, 0x0000, 0x3010, 0xFA00, 0x0000, 0x0000, 0x0000, 0x0003, /* C0 */
  418. + 0x1618, 0x8200, 0x8000, 0x0400, 0x050F, 0x0000, 0x0000, 0x0000, /* C8 */
  419. + 0x4C93, 0x0000, 0x1000, 0x1120, 0x0010, 0x1242, 0x1242, 0x1E00, /* D0 */
  420. + 0x0000, 0x0000, 0x0000, 0x00F8, 0x0000, 0x0041, 0x0800, 0x0000, /* D8 */
  421. + 0x82A0, 0x572E, 0x2490, 0x14A9, 0x4E00, 0x0000, 0x0803, 0x0541, /* E0 */
  422. + 0x0C15, 0x0000, 0x0000, 0x0400, 0x2626, 0x0000, 0x0000, 0x4200, /* E8 */
  423. + 0x0000, 0xAA55, 0x1020, 0x0000, 0x0000, 0x5010, 0x0000, 0x0000, /* F0 */
  424. + 0x0000, 0x0000, 0x5000, 0x0000, 0x0000, 0x0000, 0x02F2, 0x0000, /* F8 */
  425. + 0x101F, 0xFDC0, 0x4000, 0x8010, 0x0110, 0x0006, 0x0000, 0x0000, /*100 */
  426. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*108 */
  427. + 0x04CF, 0x0000, 0x04CF, 0x0000, 0x04CF, 0x0000, 0x04C6, 0x0000, /*110 */
  428. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*118 */
  429. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*120 */
  430. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*128 */
  431. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*130 */
  432. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*138 */
  433. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*140 */
  434. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*148 */
  435. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*150 */
  436. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*158 */
  437. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*160 */
  438. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*168 */
  439. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*170 */
  440. + 0x0000, 0x0000, 0x0000, 0x00F0, 0x08A2, 0x3112, 0x0A14, 0x0000, /*178 */
  441. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*180 */
  442. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*188 */
  443. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*190 */
  444. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*198 */
  445. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1A0 */
  446. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1A8 */
  447. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1B0 */
  448. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1B8 */
  449. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1C0 */
  450. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1C8 */
  451. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1D0 */
  452. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1D8 */
  453. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1E0 */
  454. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1E8 */
  455. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*1F0 */
  456. + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 /*1F8 */
  457. +};
  458. +
  459. +static inline void comphy_reg_set(void __iomem *addr, u32 data, u32 mask)
  460. {
  461. - struct arm_smccc_res res;
  462. - s32 ret;
  463. + u32 val;
  464. +
  465. + val = readl(addr);
  466. + val = (val & ~mask) | (data & mask);
  467. + writel(val, addr);
  468. +}
  469. - arm_smccc_smc(function, lane, mode, 0, 0, 0, 0, 0, &res);
  470. - ret = res.a0;
  471. +static inline void comphy_reg_set16(void __iomem *addr, u16 data, u16 mask)
  472. +{
  473. + u16 val;
  474. - switch (ret) {
  475. - case SMCCC_RET_SUCCESS:
  476. - return 0;
  477. - case SMCCC_RET_NOT_SUPPORTED:
  478. - return -EOPNOTSUPP;
  479. + val = readw(addr);
  480. + val = (val & ~mask) | (data & mask);
  481. + writew(val, addr);
  482. +}
  483. +
  484. +/* Used for accessing lane 2 registers (SATA/USB3 PHY) */
  485. +static void comphy_set_indirect(struct mvebu_a3700_comphy_priv *priv,
  486. + u32 offset, u16 data, u16 mask)
  487. +{
  488. + writel(offset,
  489. + priv->lane2_phy_indirect + COMPHY_LANE2_INDIR_ADDR);
  490. + comphy_reg_set(priv->lane2_phy_indirect + COMPHY_LANE2_INDIR_DATA,
  491. + data, mask);
  492. +}
  493. +
  494. +static void comphy_lane_reg_set(struct mvebu_a3700_comphy_lane *lane,
  495. + u16 reg, u16 data, u16 mask)
  496. +{
  497. + if (lane->id == 2) {
  498. + /* lane 2 PHY registers are accessed indirectly */
  499. + comphy_set_indirect(lane->priv,
  500. + reg + COMPHY_LANE2_REGS_BASE,
  501. + data, mask);
  502. + } else {
  503. + void __iomem *base = lane->id == 1 ?
  504. + lane->priv->lane1_phy_regs :
  505. + lane->priv->lane0_phy_regs;
  506. +
  507. + comphy_reg_set16(base + COMPHY_LANE_REG_DIRECT(reg),
  508. + data, mask);
  509. + }
  510. +}
  511. +
  512. +static int comphy_lane_reg_poll(struct mvebu_a3700_comphy_lane *lane,
  513. + u16 reg, u16 bits,
  514. + ulong sleep_us, ulong timeout_us)
  515. +{
  516. + int ret;
  517. +
  518. + if (lane->id == 2) {
  519. + u32 data;
  520. +
  521. + /* lane 2 PHY registers are accessed indirectly */
  522. + writel(reg + COMPHY_LANE2_REGS_BASE,
  523. + lane->priv->lane2_phy_indirect +
  524. + COMPHY_LANE2_INDIR_ADDR);
  525. +
  526. + ret = readl_poll_timeout(lane->priv->lane2_phy_indirect +
  527. + COMPHY_LANE2_INDIR_DATA,
  528. + data, (data & bits) == bits,
  529. + sleep_us, timeout_us);
  530. + } else {
  531. + void __iomem *base = lane->id == 1 ?
  532. + lane->priv->lane1_phy_regs :
  533. + lane->priv->lane0_phy_regs;
  534. + u16 data;
  535. +
  536. + ret = readw_poll_timeout(base + COMPHY_LANE_REG_DIRECT(reg),
  537. + data, (data & bits) == bits,
  538. + sleep_us, timeout_us);
  539. + }
  540. +
  541. + return ret;
  542. +}
  543. +
  544. +static void comphy_periph_reg_set(struct mvebu_a3700_comphy_lane *lane,
  545. + u8 reg, u32 data, u32 mask)
  546. +{
  547. + comphy_reg_set(lane->priv->comphy_regs + COMPHY_PHY_REG(lane->id, reg),
  548. + data, mask);
  549. +}
  550. +
  551. +static int comphy_periph_reg_poll(struct mvebu_a3700_comphy_lane *lane,
  552. + u8 reg, u32 bits,
  553. + ulong sleep_us, ulong timeout_us)
  554. +{
  555. + u32 data;
  556. +
  557. + return readl_poll_timeout(lane->priv->comphy_regs +
  558. + COMPHY_PHY_REG(lane->id, reg),
  559. + data, (data & bits) == bits,
  560. + sleep_us, timeout_us);
  561. +}
  562. +
  563. +/* PHY selector configures with corresponding modes */
  564. +static int
  565. +mvebu_a3700_comphy_set_phy_selector(struct mvebu_a3700_comphy_lane *lane)
  566. +{
  567. + u32 old, new, clr = 0, set = 0;
  568. + unsigned long flags;
  569. +
  570. + switch (lane->mode) {
  571. + case PHY_MODE_SATA:
  572. + /* SATA must be in Lane2 */
  573. + if (lane->id == 2)
  574. + clr = COMPHY_SELECTOR_USB3_PHY_SEL_BIT;
  575. + else
  576. + goto error;
  577. + break;
  578. +
  579. + case PHY_MODE_ETHERNET:
  580. + if (lane->id == 0)
  581. + clr = COMPHY_SELECTOR_USB3_GBE1_SEL_BIT;
  582. + else if (lane->id == 1)
  583. + clr = COMPHY_SELECTOR_PCIE_GBE0_SEL_BIT;
  584. + else
  585. + goto error;
  586. + break;
  587. +
  588. + case PHY_MODE_USB_HOST_SS:
  589. + if (lane->id == 2)
  590. + set = COMPHY_SELECTOR_USB3_PHY_SEL_BIT;
  591. + else if (lane->id == 0)
  592. + set = COMPHY_SELECTOR_USB3_GBE1_SEL_BIT;
  593. + else
  594. + goto error;
  595. + break;
  596. +
  597. + case PHY_MODE_PCIE:
  598. + /* PCIE must be in Lane1 */
  599. + if (lane->id == 1)
  600. + set = COMPHY_SELECTOR_PCIE_GBE0_SEL_BIT;
  601. + else
  602. + goto error;
  603. + break;
  604. +
  605. + default:
  606. + goto error;
  607. + }
  608. +
  609. + spin_lock_irqsave(&lane->priv->lock, flags);
  610. +
  611. + old = readl(lane->priv->comphy_regs + COMPHY_SELECTOR_PHY_REG);
  612. + new = (old & ~clr) | set;
  613. + writel(new, lane->priv->comphy_regs + COMPHY_SELECTOR_PHY_REG);
  614. +
  615. + spin_unlock_irqrestore(&lane->priv->lock, flags);
  616. +
  617. + dev_dbg(lane->dev,
  618. + "COMPHY[%d] mode[%d] changed PHY selector 0x%08x -> 0x%08x\n",
  619. + lane->id, lane->mode, old, new);
  620. +
  621. + return 0;
  622. +error:
  623. + dev_err(lane->dev, "COMPHY[%d] mode[%d] is invalid\n", lane->id,
  624. + lane->mode);
  625. + return -EINVAL;
  626. +}
  627. +
  628. +static int
  629. +mvebu_a3700_comphy_sata_power_on(struct mvebu_a3700_comphy_lane *lane)
  630. +{
  631. + u32 mask, data, ref_clk;
  632. + int ret;
  633. +
  634. + /* Configure phy selector for SATA */
  635. + ret = mvebu_a3700_comphy_set_phy_selector(lane);
  636. + if (ret)
  637. + return ret;
  638. +
  639. + /* Clear phy isolation mode to make it work in normal mode */
  640. + comphy_lane_reg_set(lane, COMPHY_ISOLATION_CTRL,
  641. + 0x0, PHY_ISOLATE_MODE);
  642. +
  643. + /* 0. Check the Polarity invert bits */
  644. + data = 0x0;
  645. + if (lane->invert_tx)
  646. + data |= TXD_INVERT_BIT;
  647. + if (lane->invert_rx)
  648. + data |= RXD_INVERT_BIT;
  649. + mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
  650. + comphy_lane_reg_set(lane, COMPHY_SYNC_PATTERN, data, mask);
  651. +
  652. + /* 1. Select 40-bit data width */
  653. + comphy_lane_reg_set(lane, COMPHY_DIG_LOOPBACK_EN,
  654. + DATA_WIDTH_40BIT, SEL_DATA_WIDTH_MASK);
  655. +
  656. + /* 2. Select reference clock(25M) and PHY mode (SATA) */
  657. + if (lane->priv->xtal_is_40m)
  658. + ref_clk = REF_FREF_SEL_SERDES_40MHZ;
  659. + else
  660. + ref_clk = REF_FREF_SEL_SERDES_25MHZ;
  661. +
  662. + data = ref_clk | COMPHY_MODE_SATA;
  663. + mask = REF_FREF_SEL_MASK | COMPHY_MODE_MASK;
  664. + comphy_lane_reg_set(lane, COMPHY_POWER_PLL_CTRL, data, mask);
  665. +
  666. + /* 3. Use maximum PLL rate (no power save) */
  667. + comphy_lane_reg_set(lane, COMPHY_KVCO_CAL_CTRL,
  668. + USE_MAX_PLL_RATE_BIT, USE_MAX_PLL_RATE_BIT);
  669. +
  670. + /* 4. Reset reserved bit */
  671. + comphy_set_indirect(lane->priv, COMPHY_RESERVED_REG,
  672. + 0x0, PHYCTRL_FRM_PIN_BIT);
  673. +
  674. + /* 5. Set vendor-specific configuration (It is done in sata driver) */
  675. + /* XXX: in U-Boot below sequence was executed in this place, in Linux
  676. + * not. Now it is done only in U-Boot before this comphy
  677. + * initialization - tests shows that it works ok, but in case of any
  678. + * future problem it is left for reference.
  679. + * reg_set(MVEBU_REGS_BASE + 0xe00a0, 0, 0xffffffff);
  680. + * reg_set(MVEBU_REGS_BASE + 0xe00a4, BIT(6), BIT(6));
  681. + */
  682. +
  683. + /* Wait for > 55 us to allow PLL be enabled */
  684. + udelay(PLL_SET_DELAY_US);
  685. +
  686. + /* Polling status */
  687. + ret = comphy_lane_reg_poll(lane, COMPHY_DIG_LOOPBACK_EN,
  688. + PLL_READY_TX_BIT, COMPHY_PLL_SLEEP,
  689. + COMPHY_PLL_TIMEOUT);
  690. + if (ret)
  691. + dev_err(lane->dev, "Failed to lock SATA PLL\n");
  692. +
  693. + return ret;
  694. +}
  695. +
  696. +static void comphy_gbe_phy_init(struct mvebu_a3700_comphy_lane *lane,
  697. + bool is_1gbps)
  698. +{
  699. + int addr, fix_idx;
  700. + u16 val;
  701. +
  702. + fix_idx = 0;
  703. + for (addr = 0; addr < 512; addr++) {
  704. + /*
  705. + * All PHY register values are defined in full for 3.125Gbps
  706. + * SERDES speed. The values required for 1.25 Gbps are almost
  707. + * the same and only few registers should be "fixed" in
  708. + * comparison to 3.125 Gbps values. These register values are
  709. + * stored in "gbe_phy_init_fix" array.
  710. + */
  711. + if (!is_1gbps && gbe_phy_init_fix[fix_idx].addr == addr) {
  712. + /* Use new value */
  713. + val = gbe_phy_init_fix[fix_idx].value;
  714. + if (fix_idx < ARRAY_SIZE(gbe_phy_init_fix))
  715. + fix_idx++;
  716. + } else {
  717. + val = gbe_phy_init[addr];
  718. + }
  719. +
  720. + comphy_lane_reg_set(lane, addr, val, 0xFFFF);
  721. + }
  722. +}
  723. +
  724. +static int
  725. +mvebu_a3700_comphy_ethernet_power_on(struct mvebu_a3700_comphy_lane *lane)
  726. +{
  727. + u32 mask, data, speed_sel;
  728. + int ret;
  729. +
  730. + /* Set selector */
  731. + ret = mvebu_a3700_comphy_set_phy_selector(lane);
  732. + if (ret)
  733. + return ret;
  734. +
  735. + /*
  736. + * 1. Reset PHY by setting PHY input port PIN_RESET=1.
  737. + * 2. Set PHY input port PIN_TX_IDLE=1, PIN_PU_IVREF=1 to keep
  738. + * PHY TXP/TXN output to idle state during PHY initialization
  739. + * 3. Set PHY input port PIN_PU_PLL=0, PIN_PU_RX=0, PIN_PU_TX=0.
  740. + */
  741. + data = PIN_PU_IVREF_BIT | PIN_TX_IDLE_BIT | PIN_RESET_COMPHY_BIT;
  742. + mask = data | PIN_RESET_CORE_BIT | PIN_PU_PLL_BIT | PIN_PU_RX_BIT |
  743. + PIN_PU_TX_BIT | PHY_RX_INIT_BIT;
  744. + comphy_periph_reg_set(lane, COMPHY_PHY_CFG1, data, mask);
  745. +
  746. + /* 4. Release reset to the PHY by setting PIN_RESET=0. */
  747. + data = 0x0;
  748. + mask = PIN_RESET_COMPHY_BIT;
  749. + comphy_periph_reg_set(lane, COMPHY_PHY_CFG1, data, mask);
  750. +
  751. + /*
  752. + * 5. Set PIN_PHY_GEN_TX[3:0] and PIN_PHY_GEN_RX[3:0] to decide COMPHY
  753. + * bit rate
  754. + */
  755. + switch (lane->submode) {
  756. + case PHY_INTERFACE_MODE_SGMII:
  757. + case PHY_INTERFACE_MODE_1000BASEX:
  758. + /* SGMII 1G, SerDes speed 1.25G */
  759. + speed_sel = SERDES_SPEED_1_25_G;
  760. + break;
  761. + case PHY_INTERFACE_MODE_2500BASEX:
  762. + /* 2500Base-X, SerDes speed 3.125G */
  763. + speed_sel = SERDES_SPEED_3_125_G;
  764. + break;
  765. default:
  766. + /* Other rates are not supported */
  767. + dev_err(lane->dev,
  768. + "unsupported phy speed %d on comphy lane%d\n",
  769. + lane->submode, lane->id);
  770. return -EINVAL;
  771. }
  772. + data = GEN_RX_SEL_VALUE(speed_sel) | GEN_TX_SEL_VALUE(speed_sel);
  773. + mask = GEN_RX_SEL_MASK | GEN_TX_SEL_MASK;
  774. + comphy_periph_reg_set(lane, COMPHY_PHY_CFG1, data, mask);
  775. +
  776. + /*
  777. + * 6. Wait 10mS for bandgap and reference clocks to stabilize; then
  778. + * start SW programming.
  779. + */
  780. + mdelay(10);
  781. +
  782. + /* 7. Program COMPHY register PHY_MODE */
  783. + data = COMPHY_MODE_SERDES;
  784. + mask = COMPHY_MODE_MASK;
  785. + comphy_lane_reg_set(lane, COMPHY_POWER_PLL_CTRL, data, mask);
  786. +
  787. + /*
  788. + * 8. Set COMPHY register REFCLK_SEL to select the correct REFCLK
  789. + * source
  790. + */
  791. + data = 0x0;
  792. + mask = PHY_REF_CLK_SEL;
  793. + comphy_lane_reg_set(lane, COMPHY_MISC_CTRL0, data, mask);
  794. +
  795. + /*
  796. + * 9. Set correct reference clock frequency in COMPHY register
  797. + * REF_FREF_SEL.
  798. + */
  799. + if (lane->priv->xtal_is_40m)
  800. + data = REF_FREF_SEL_SERDES_50MHZ;
  801. + else
  802. + data = REF_FREF_SEL_SERDES_25MHZ;
  803. +
  804. + mask = REF_FREF_SEL_MASK;
  805. + comphy_lane_reg_set(lane, COMPHY_POWER_PLL_CTRL, data, mask);
  806. +
  807. + /*
  808. + * 10. Program COMPHY register PHY_GEN_MAX[1:0]
  809. + * This step is mentioned in the flow received from verification team.
  810. + * However the PHY_GEN_MAX value is only meaningful for other interfaces
  811. + * (not SERDES). For instance, it selects SATA speed 1.5/3/6 Gbps or
  812. + * PCIe speed 2.5/5 Gbps
  813. + */
  814. +
  815. + /*
  816. + * 11. Program COMPHY register SEL_BITS to set correct parallel data
  817. + * bus width
  818. + */
  819. + data = DATA_WIDTH_10BIT;
  820. + mask = SEL_DATA_WIDTH_MASK;
  821. + comphy_lane_reg_set(lane, COMPHY_DIG_LOOPBACK_EN, data, mask);
  822. +
  823. + /*
  824. + * 12. As long as DFE function needs to be enabled in any mode,
  825. + * COMPHY register DFE_UPDATE_EN[5:0] shall be programmed to 0x3F
  826. + * for real chip during COMPHY power on.
  827. + * The value of the DFE_UPDATE_EN already is 0x3F, because it is the
  828. + * default value after reset of the PHY.
  829. + */
  830. +
  831. + /*
  832. + * 13. Program COMPHY GEN registers.
  833. + * These registers should be programmed based on the lab testing result
  834. + * to achieve optimal performance. Please contact the CEA group to get
  835. + * the related GEN table during real chip bring-up. We only required to
  836. + * run though the entire registers programming flow defined by
  837. + * "comphy_gbe_phy_init" when the REF clock is 40 MHz. For REF clock
  838. + * 25 MHz the default values stored in PHY registers are OK.
  839. + */
  840. + dev_dbg(lane->dev, "Running C-DPI phy init %s mode\n",
  841. + lane->submode == PHY_INTERFACE_MODE_2500BASEX ? "2G5" : "1G");
  842. + if (lane->priv->xtal_is_40m)
  843. + comphy_gbe_phy_init(lane,
  844. + lane->submode != PHY_INTERFACE_MODE_2500BASEX);
  845. +
  846. + /*
  847. + * 14. Check the PHY Polarity invert bit
  848. + */
  849. + data = 0x0;
  850. + if (lane->invert_tx)
  851. + data |= TXD_INVERT_BIT;
  852. + if (lane->invert_rx)
  853. + data |= RXD_INVERT_BIT;
  854. + mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
  855. + comphy_lane_reg_set(lane, COMPHY_SYNC_PATTERN, data, mask);
  856. +
  857. + /*
  858. + * 15. Set PHY input ports PIN_PU_PLL, PIN_PU_TX and PIN_PU_RX to 1 to
  859. + * start PHY power up sequence. All the PHY register programming should
  860. + * be done before PIN_PU_PLL=1. There should be no register programming
  861. + * for normal PHY operation from this point.
  862. + */
  863. + data = PIN_PU_PLL_BIT | PIN_PU_RX_BIT | PIN_PU_TX_BIT;
  864. + mask = data;
  865. + comphy_periph_reg_set(lane, COMPHY_PHY_CFG1, data, mask);
  866. +
  867. + /*
  868. + * 16. Wait for PHY power up sequence to finish by checking output ports
  869. + * PIN_PLL_READY_TX=1 and PIN_PLL_READY_RX=1.
  870. + */
  871. + ret = comphy_periph_reg_poll(lane, COMPHY_PHY_STAT1,
  872. + PHY_PLL_READY_TX_BIT |
  873. + PHY_PLL_READY_RX_BIT,
  874. + COMPHY_PLL_SLEEP, COMPHY_PLL_TIMEOUT);
  875. + if (ret) {
  876. + dev_err(lane->dev, "Failed to lock PLL for SERDES PHY %d\n",
  877. + lane->id);
  878. + return ret;
  879. + }
  880. +
  881. + /*
  882. + * 17. Set COMPHY input port PIN_TX_IDLE=0
  883. + */
  884. + comphy_periph_reg_set(lane, COMPHY_PHY_CFG1, 0x0, PIN_TX_IDLE_BIT);
  885. +
  886. + /*
  887. + * 18. After valid data appear on PIN_RXDATA bus, set PIN_RX_INIT=1. To
  888. + * start RX initialization. PIN_RX_INIT_DONE will be cleared to 0 by the
  889. + * PHY After RX initialization is done, PIN_RX_INIT_DONE will be set to
  890. + * 1 by COMPHY Set PIN_RX_INIT=0 after PIN_RX_INIT_DONE= 1. Please
  891. + * refer to RX initialization part for details.
  892. + */
  893. + comphy_periph_reg_set(lane, COMPHY_PHY_CFG1,
  894. + PHY_RX_INIT_BIT, PHY_RX_INIT_BIT);
  895. +
  896. + ret = comphy_periph_reg_poll(lane, COMPHY_PHY_STAT1,
  897. + PHY_PLL_READY_TX_BIT |
  898. + PHY_PLL_READY_RX_BIT,
  899. + COMPHY_PLL_SLEEP, COMPHY_PLL_TIMEOUT);
  900. + if (ret) {
  901. + dev_err(lane->dev, "Failed to lock PLL for SERDES PHY %d\n",
  902. + lane->id);
  903. + return ret;
  904. + }
  905. +
  906. + ret = comphy_periph_reg_poll(lane, COMPHY_PHY_STAT1,
  907. + PHY_RX_INIT_DONE_BIT,
  908. + COMPHY_PLL_SLEEP, COMPHY_PLL_TIMEOUT);
  909. + if (ret)
  910. + dev_err(lane->dev, "Failed to init RX of SERDES PHY %d\n",
  911. + lane->id);
  912. +
  913. + return ret;
  914. }
  915. -static int mvebu_a3700_comphy_get_fw_mode(int lane,
  916. +static int
  917. +mvebu_a3700_comphy_usb3_power_on(struct mvebu_a3700_comphy_lane *lane)
  918. +{
  919. + u32 mask, data, cfg, ref_clk;
  920. + int ret;
  921. +
  922. + /* Set phy seclector */
  923. + ret = mvebu_a3700_comphy_set_phy_selector(lane);
  924. + if (ret)
  925. + return ret;
  926. +
  927. + /*
  928. + * 0. Set PHY OTG Control(0x5d034), bit 4, Power up OTG module The
  929. + * register belong to UTMI module, so it is set in UTMI phy driver.
  930. + */
  931. +
  932. + /*
  933. + * 1. Set PRD_TXDEEMPH (3.5db de-emph)
  934. + */
  935. + data = PRD_TXDEEMPH0_MASK;
  936. + mask = PRD_TXDEEMPH0_MASK | PRD_TXMARGIN_MASK | PRD_TXSWING_MASK |
  937. + CFG_TX_ALIGN_POS_MASK;
  938. + comphy_lane_reg_set(lane, COMPHY_PIPE_LANE_CFG0, data, mask);
  939. +
  940. + /*
  941. + * 2. Set BIT0: enable transmitter in high impedance mode
  942. + * Set BIT[3:4]: delay 2 clock cycles for HiZ off latency
  943. + * Set BIT6: Tx detect Rx at HiZ mode
  944. + * Unset BIT15: set to 0 to set USB3 De-emphasize level to -3.5db
  945. + * together with bit 0 of COMPHY_PIPE_LANE_CFG0 register
  946. + */
  947. + data = TX_DET_RX_MODE | GEN2_TX_DATA_DLY_DEFT | TX_ELEC_IDLE_MODE_EN;
  948. + mask = PRD_TXDEEMPH1_MASK | TX_DET_RX_MODE | GEN2_TX_DATA_DLY_MASK |
  949. + TX_ELEC_IDLE_MODE_EN;
  950. + comphy_lane_reg_set(lane, COMPHY_PIPE_LANE_CFG1, data, mask);
  951. +
  952. + /*
  953. + * 3. Set Spread Spectrum Clock Enabled
  954. + */
  955. + comphy_lane_reg_set(lane, COMPHY_PIPE_LANE_CFG4,
  956. + SPREAD_SPECTRUM_CLK_EN, SPREAD_SPECTRUM_CLK_EN);
  957. +
  958. + /*
  959. + * 4. Set Override Margining Controls From the MAC:
  960. + * Use margining signals from lane configuration
  961. + */
  962. + comphy_lane_reg_set(lane, COMPHY_PIPE_TEST_MODE_CTRL,
  963. + MODE_MARGIN_OVERRIDE, 0xFFFF);
  964. +
  965. + /*
  966. + * 5. Set Lane-to-Lane Bundle Clock Sampling Period = per PCLK cycles
  967. + * set Mode Clock Source = PCLK is generated from REFCLK
  968. + */
  969. + data = 0x0;
  970. + mask = MODE_CLK_SRC | BUNDLE_PERIOD_SEL | BUNDLE_PERIOD_SCALE_MASK |
  971. + BUNDLE_SAMPLE_CTRL | PLL_READY_DLY_MASK;
  972. + comphy_lane_reg_set(lane, COMPHY_PIPE_CLK_SRC_LO, data, mask);
  973. +
  974. + /*
  975. + * 6. Set G2 Spread Spectrum Clock Amplitude at 4K
  976. + */
  977. + comphy_lane_reg_set(lane, COMPHY_GEN2_SET2,
  978. + GS2_TX_SSC_AMP_4128, GS2_TX_SSC_AMP_MASK);
  979. +
  980. + /*
  981. + * 7. Unset G3 Spread Spectrum Clock Amplitude
  982. + * set G3 TX and RX Register Master Current Select
  983. + */
  984. + data = GS2_VREG_RXTX_MAS_ISET_60U;
  985. + mask = GS2_TX_SSC_AMP_MASK | GS2_VREG_RXTX_MAS_ISET_MASK |
  986. + GS2_RSVD_6_0_MASK;
  987. + comphy_lane_reg_set(lane, COMPHY_GEN3_SET2, data, mask);
  988. +
  989. + /*
  990. + * 8. Check crystal jumper setting and program the Power and PLL Control
  991. + * accordingly Change RX wait
  992. + */
  993. + if (lane->priv->xtal_is_40m) {
  994. + ref_clk = REF_FREF_SEL_PCIE_USB3_40MHZ;
  995. + cfg = CFG_PM_RXDLOZ_WAIT_12_UNIT;
  996. + } else {
  997. + ref_clk = REF_FREF_SEL_PCIE_USB3_25MHZ;
  998. + cfg = CFG_PM_RXDLOZ_WAIT_7_UNIT;
  999. + }
  1000. +
  1001. + data = PU_IVREF_BIT | PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT |
  1002. + PU_TX_INTP_BIT | PU_DFE_BIT | COMPHY_MODE_USB3 | ref_clk;
  1003. + mask = PU_IVREF_BIT | PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT |
  1004. + PU_TX_INTP_BIT | PU_DFE_BIT | PLL_LOCK_BIT | COMPHY_MODE_MASK |
  1005. + REF_FREF_SEL_MASK;
  1006. + comphy_lane_reg_set(lane, COMPHY_POWER_PLL_CTRL, data, mask);
  1007. +
  1008. + data = CFG_PM_RXDEN_WAIT_1_UNIT | cfg;
  1009. + mask = CFG_PM_OSCCLK_WAIT_MASK | CFG_PM_RXDEN_WAIT_MASK |
  1010. + CFG_PM_RXDLOZ_WAIT_MASK;
  1011. + comphy_lane_reg_set(lane, COMPHY_PIPE_PWR_MGM_TIM1, data, mask);
  1012. +
  1013. + /*
  1014. + * 9. Enable idle sync
  1015. + */
  1016. + comphy_lane_reg_set(lane, COMPHY_IDLE_SYNC_EN,
  1017. + IDLE_SYNC_EN, IDLE_SYNC_EN);
  1018. +
  1019. + /*
  1020. + * 10. Enable the output of 500M clock
  1021. + */
  1022. + comphy_lane_reg_set(lane, COMPHY_MISC_CTRL0, CLK500M_EN, CLK500M_EN);
  1023. +
  1024. + /*
  1025. + * 11. Set 20-bit data width
  1026. + */
  1027. + comphy_lane_reg_set(lane, COMPHY_DIG_LOOPBACK_EN,
  1028. + DATA_WIDTH_20BIT, 0xFFFF);
  1029. +
  1030. + /*
  1031. + * 12. Override Speed_PLL value and use MAC PLL
  1032. + */
  1033. + data = SPEED_PLL_VALUE_16 | USE_MAX_PLL_RATE_BIT;
  1034. + mask = 0xFFFF;
  1035. + comphy_lane_reg_set(lane, COMPHY_KVCO_CAL_CTRL, data, mask);
  1036. +
  1037. + /*
  1038. + * 13. Check the Polarity invert bit
  1039. + */
  1040. + data = 0x0;
  1041. + if (lane->invert_tx)
  1042. + data |= TXD_INVERT_BIT;
  1043. + if (lane->invert_rx)
  1044. + data |= RXD_INVERT_BIT;
  1045. + mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
  1046. + comphy_lane_reg_set(lane, COMPHY_SYNC_PATTERN, data, mask);
  1047. +
  1048. + /*
  1049. + * 14. Set max speed generation to USB3.0 5Gbps
  1050. + */
  1051. + comphy_lane_reg_set(lane, COMPHY_SYNC_MASK_GEN,
  1052. + PHY_GEN_MAX_USB3_5G, PHY_GEN_MAX_MASK);
  1053. +
  1054. + /*
  1055. + * 15. Set capacitor value for FFE gain peaking to 0xF
  1056. + */
  1057. + comphy_lane_reg_set(lane, COMPHY_GEN2_SET3,
  1058. + GS3_FFE_CAP_SEL_VALUE, GS3_FFE_CAP_SEL_MASK);
  1059. +
  1060. + /*
  1061. + * 16. Release SW reset
  1062. + */
  1063. + data = MODE_CORE_CLK_FREQ_SEL | MODE_PIPE_WIDTH_32 | MODE_REFDIV_BY_4;
  1064. + mask = 0xFFFF;
  1065. + comphy_lane_reg_set(lane, COMPHY_PIPE_RST_CLK_CTRL, data, mask);
  1066. +
  1067. + /* Wait for > 55 us to allow PCLK be enabled */
  1068. + udelay(PLL_SET_DELAY_US);
  1069. +
  1070. + ret = comphy_lane_reg_poll(lane, COMPHY_PIPE_LANE_STAT1, TXDCLK_PCLK_EN,
  1071. + COMPHY_PLL_SLEEP, COMPHY_PLL_TIMEOUT);
  1072. + if (ret)
  1073. + dev_err(lane->dev, "Failed to lock USB3 PLL\n");
  1074. +
  1075. + return ret;
  1076. +}
  1077. +
  1078. +static int
  1079. +mvebu_a3700_comphy_pcie_power_on(struct mvebu_a3700_comphy_lane *lane)
  1080. +{
  1081. + u32 mask, data, ref_clk;
  1082. + int ret;
  1083. +
  1084. + /* Configure phy selector for PCIe */
  1085. + ret = mvebu_a3700_comphy_set_phy_selector(lane);
  1086. + if (ret)
  1087. + return ret;
  1088. +
  1089. + /* 1. Enable max PLL. */
  1090. + comphy_lane_reg_set(lane, COMPHY_PIPE_LANE_CFG1,
  1091. + USE_MAX_PLL_RATE_EN, USE_MAX_PLL_RATE_EN);
  1092. +
  1093. + /* 2. Select 20 bit SERDES interface. */
  1094. + comphy_lane_reg_set(lane, COMPHY_PIPE_CLK_SRC_LO,
  1095. + CFG_SEL_20B, CFG_SEL_20B);
  1096. +
  1097. + /* 3. Force to use reg setting for PCIe mode */
  1098. + comphy_lane_reg_set(lane, COMPHY_MISC_CTRL1,
  1099. + SEL_BITS_PCIE_FORCE, SEL_BITS_PCIE_FORCE);
  1100. +
  1101. + /* 4. Change RX wait */
  1102. + data = CFG_PM_RXDEN_WAIT_1_UNIT | CFG_PM_RXDLOZ_WAIT_12_UNIT;
  1103. + mask = CFG_PM_OSCCLK_WAIT_MASK | CFG_PM_RXDEN_WAIT_MASK |
  1104. + CFG_PM_RXDLOZ_WAIT_MASK;
  1105. + comphy_lane_reg_set(lane, COMPHY_PIPE_PWR_MGM_TIM1, data, mask);
  1106. +
  1107. + /* 5. Enable idle sync */
  1108. + comphy_lane_reg_set(lane, COMPHY_IDLE_SYNC_EN,
  1109. + IDLE_SYNC_EN, IDLE_SYNC_EN);
  1110. +
  1111. + /* 6. Enable the output of 100M/125M/500M clock */
  1112. + data = CLK500M_EN | TXDCLK_2X_SEL | CLK100M_125M_EN;
  1113. + mask = data;
  1114. + comphy_lane_reg_set(lane, COMPHY_MISC_CTRL0, data, mask);
  1115. +
  1116. + /*
  1117. + * 7. Enable TX, PCIE global register, 0xd0074814, it is done in
  1118. + * PCI-E driver
  1119. + */
  1120. +
  1121. + /*
  1122. + * 8. Check crystal jumper setting and program the Power and PLL
  1123. + * Control accordingly
  1124. + */
  1125. +
  1126. + if (lane->priv->xtal_is_40m)
  1127. + ref_clk = REF_FREF_SEL_PCIE_USB3_40MHZ;
  1128. + else
  1129. + ref_clk = REF_FREF_SEL_PCIE_USB3_25MHZ;
  1130. +
  1131. + data = PU_IVREF_BIT | PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT |
  1132. + PU_TX_INTP_BIT | PU_DFE_BIT | COMPHY_MODE_PCIE | ref_clk;
  1133. + mask = 0xFFFF;
  1134. + comphy_lane_reg_set(lane, COMPHY_POWER_PLL_CTRL, data, mask);
  1135. +
  1136. + /* 9. Override Speed_PLL value and use MAC PLL */
  1137. + comphy_lane_reg_set(lane, COMPHY_KVCO_CAL_CTRL,
  1138. + SPEED_PLL_VALUE_16 | USE_MAX_PLL_RATE_BIT,
  1139. + 0xFFFF);
  1140. +
  1141. + /* 10. Check the Polarity invert bit */
  1142. + data = 0x0;
  1143. + if (lane->invert_tx)
  1144. + data |= TXD_INVERT_BIT;
  1145. + if (lane->invert_rx)
  1146. + data |= RXD_INVERT_BIT;
  1147. + mask = TXD_INVERT_BIT | RXD_INVERT_BIT;
  1148. + comphy_lane_reg_set(lane, COMPHY_SYNC_PATTERN, data, mask);
  1149. +
  1150. + /* 11. Release SW reset */
  1151. + data = MODE_CORE_CLK_FREQ_SEL | MODE_PIPE_WIDTH_32;
  1152. + mask = data | PIPE_SOFT_RESET | MODE_REFDIV_MASK;
  1153. + comphy_lane_reg_set(lane, COMPHY_PIPE_RST_CLK_CTRL, data, mask);
  1154. +
  1155. + /* Wait for > 55 us to allow PCLK be enabled */
  1156. + udelay(PLL_SET_DELAY_US);
  1157. +
  1158. + ret = comphy_lane_reg_poll(lane, COMPHY_PIPE_LANE_STAT1, TXDCLK_PCLK_EN,
  1159. + COMPHY_PLL_SLEEP, COMPHY_PLL_TIMEOUT);
  1160. + if (ret)
  1161. + dev_err(lane->dev, "Failed to lock PCIE PLL\n");
  1162. +
  1163. + return ret;
  1164. +}
  1165. +
  1166. +static void
  1167. +mvebu_a3700_comphy_sata_power_off(struct mvebu_a3700_comphy_lane *lane)
  1168. +{
  1169. + /* Set phy isolation mode */
  1170. + comphy_lane_reg_set(lane, COMPHY_ISOLATION_CTRL,
  1171. + PHY_ISOLATE_MODE, PHY_ISOLATE_MODE);
  1172. +
  1173. + /* Power off PLL, Tx, Rx */
  1174. + comphy_lane_reg_set(lane, COMPHY_POWER_PLL_CTRL,
  1175. + 0x0, PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT);
  1176. +}
  1177. +
  1178. +static void
  1179. +mvebu_a3700_comphy_ethernet_power_off(struct mvebu_a3700_comphy_lane *lane)
  1180. +{
  1181. + u32 mask, data;
  1182. +
  1183. + data = PIN_RESET_CORE_BIT | PIN_RESET_COMPHY_BIT | PIN_PU_IVREF_BIT |
  1184. + PHY_RX_INIT_BIT;
  1185. + mask = data;
  1186. + comphy_periph_reg_set(lane, COMPHY_PHY_CFG1, data, mask);
  1187. +}
  1188. +
  1189. +static void
  1190. +mvebu_a3700_comphy_pcie_power_off(struct mvebu_a3700_comphy_lane *lane)
  1191. +{
  1192. + /* Power off PLL, Tx, Rx */
  1193. + comphy_lane_reg_set(lane, COMPHY_POWER_PLL_CTRL,
  1194. + 0x0, PU_PLL_BIT | PU_RX_BIT | PU_TX_BIT);
  1195. +}
  1196. +
  1197. +static int mvebu_a3700_comphy_reset(struct phy *phy)
  1198. +{
  1199. + struct mvebu_a3700_comphy_lane *lane = phy_get_drvdata(phy);
  1200. + u16 mask, data;
  1201. +
  1202. + dev_dbg(lane->dev, "resetting lane %d\n", lane->id);
  1203. +
  1204. + /* COMPHY reset for internal logic */
  1205. + comphy_lane_reg_set(lane, COMPHY_SFT_RESET,
  1206. + SFT_RST_NO_REG, SFT_RST_NO_REG);
  1207. +
  1208. + /* COMPHY register reset (cleared automatically) */
  1209. + comphy_lane_reg_set(lane, COMPHY_SFT_RESET, SFT_RST, SFT_RST);
  1210. +
  1211. + /* PIPE soft and register reset */
  1212. + data = PIPE_SOFT_RESET | PIPE_REG_RESET;
  1213. + mask = data;
  1214. + comphy_lane_reg_set(lane, COMPHY_PIPE_RST_CLK_CTRL, data, mask);
  1215. +
  1216. + /* Release PIPE register reset */
  1217. + comphy_lane_reg_set(lane, COMPHY_PIPE_RST_CLK_CTRL,
  1218. + 0x0, PIPE_REG_RESET);
  1219. +
  1220. + /* Reset SB configuration register (only for lanes 0 and 1) */
  1221. + if (lane->id == 0 || lane->id == 1) {
  1222. + u32 mask, data;
  1223. +
  1224. + data = PIN_RESET_CORE_BIT | PIN_RESET_COMPHY_BIT |
  1225. + PIN_PU_PLL_BIT | PIN_PU_RX_BIT | PIN_PU_TX_BIT;
  1226. + mask = data | PIN_PU_IVREF_BIT | PIN_TX_IDLE_BIT;
  1227. + comphy_periph_reg_set(lane, COMPHY_PHY_CFG1, data, mask);
  1228. + }
  1229. +
  1230. + return 0;
  1231. +}
  1232. +
  1233. +static bool mvebu_a3700_comphy_check_mode(int lane,
  1234. enum phy_mode mode,
  1235. int submode)
  1236. {
  1237. @@ -122,7 +1141,7 @@ static int mvebu_a3700_comphy_get_fw_mod
  1238. /* Unused PHY mux value is 0x0 */
  1239. if (mode == PHY_MODE_INVALID)
  1240. - return -EINVAL;
  1241. + return false;
  1242. for (i = 0; i < n; i++) {
  1243. if (mvebu_a3700_comphy_modes[i].lane == lane &&
  1244. @@ -132,27 +1151,30 @@ static int mvebu_a3700_comphy_get_fw_mod
  1245. }
  1246. if (i == n)
  1247. - return -EINVAL;
  1248. + return false;
  1249. - return mvebu_a3700_comphy_modes[i].fw_mode;
  1250. + return true;
  1251. }
  1252. static int mvebu_a3700_comphy_set_mode(struct phy *phy, enum phy_mode mode,
  1253. int submode)
  1254. {
  1255. struct mvebu_a3700_comphy_lane *lane = phy_get_drvdata(phy);
  1256. - int fw_mode;
  1257. - if (submode == PHY_INTERFACE_MODE_1000BASEX)
  1258. - submode = PHY_INTERFACE_MODE_SGMII;
  1259. -
  1260. - fw_mode = mvebu_a3700_comphy_get_fw_mode(lane->id, mode,
  1261. - submode);
  1262. - if (fw_mode < 0) {
  1263. + if (!mvebu_a3700_comphy_check_mode(lane->id, mode, submode)) {
  1264. dev_err(lane->dev, "invalid COMPHY mode\n");
  1265. - return fw_mode;
  1266. + return -EINVAL;
  1267. }
  1268. + /* Mode cannot be changed while the PHY is powered on */
  1269. + if (phy->power_count &&
  1270. + (lane->mode != mode || lane->submode != submode))
  1271. + return -EBUSY;
  1272. +
  1273. + /* If changing mode, ensure reset is called */
  1274. + if (lane->mode != PHY_MODE_INVALID && lane->mode != mode)
  1275. + lane->needs_reset = true;
  1276. +
  1277. /* Just remember the mode, ->power_on() will do the real setup */
  1278. lane->mode = mode;
  1279. lane->submode = submode;
  1280. @@ -163,76 +1185,77 @@ static int mvebu_a3700_comphy_set_mode(s
  1281. static int mvebu_a3700_comphy_power_on(struct phy *phy)
  1282. {
  1283. struct mvebu_a3700_comphy_lane *lane = phy_get_drvdata(phy);
  1284. - u32 fw_param;
  1285. - int fw_mode;
  1286. - int fw_port;
  1287. int ret;
  1288. - fw_mode = mvebu_a3700_comphy_get_fw_mode(lane->id,
  1289. - lane->mode, lane->submode);
  1290. - if (fw_mode < 0) {
  1291. + if (!mvebu_a3700_comphy_check_mode(lane->id, lane->mode,
  1292. + lane->submode)) {
  1293. dev_err(lane->dev, "invalid COMPHY mode\n");
  1294. - return fw_mode;
  1295. + return -EINVAL;
  1296. + }
  1297. +
  1298. + if (lane->needs_reset) {
  1299. + ret = mvebu_a3700_comphy_reset(phy);
  1300. + if (ret)
  1301. + return ret;
  1302. +
  1303. + lane->needs_reset = false;
  1304. }
  1305. switch (lane->mode) {
  1306. case PHY_MODE_USB_HOST_SS:
  1307. dev_dbg(lane->dev, "set lane %d to USB3 host mode\n", lane->id);
  1308. - fw_param = COMPHY_FW_MODE(fw_mode);
  1309. - break;
  1310. + return mvebu_a3700_comphy_usb3_power_on(lane);
  1311. case PHY_MODE_SATA:
  1312. dev_dbg(lane->dev, "set lane %d to SATA mode\n", lane->id);
  1313. - fw_param = COMPHY_FW_MODE(fw_mode);
  1314. - break;
  1315. + return mvebu_a3700_comphy_sata_power_on(lane);
  1316. case PHY_MODE_ETHERNET:
  1317. - fw_port = (lane->id == 0) ? 1 : 0;
  1318. - switch (lane->submode) {
  1319. - case PHY_INTERFACE_MODE_SGMII:
  1320. - dev_dbg(lane->dev, "set lane %d to SGMII mode\n",
  1321. - lane->id);
  1322. - fw_param = COMPHY_FW_NET(fw_mode, fw_port,
  1323. - COMPHY_FW_SPEED_1_25G);
  1324. - break;
  1325. - case PHY_INTERFACE_MODE_2500BASEX:
  1326. - dev_dbg(lane->dev, "set lane %d to 2500BASEX mode\n",
  1327. - lane->id);
  1328. - fw_param = COMPHY_FW_NET(fw_mode, fw_port,
  1329. - COMPHY_FW_SPEED_3_125G);
  1330. - break;
  1331. - default:
  1332. - dev_err(lane->dev, "unsupported PHY submode (%d)\n",
  1333. - lane->submode);
  1334. - return -ENOTSUPP;
  1335. - }
  1336. - break;
  1337. + dev_dbg(lane->dev, "set lane %d to Ethernet mode\n", lane->id);
  1338. + return mvebu_a3700_comphy_ethernet_power_on(lane);
  1339. case PHY_MODE_PCIE:
  1340. dev_dbg(lane->dev, "set lane %d to PCIe mode\n", lane->id);
  1341. - fw_param = COMPHY_FW_PCIE(fw_mode, COMPHY_FW_SPEED_5G,
  1342. - phy->attrs.bus_width);
  1343. - break;
  1344. + return mvebu_a3700_comphy_pcie_power_on(lane);
  1345. default:
  1346. dev_err(lane->dev, "unsupported PHY mode (%d)\n", lane->mode);
  1347. - return -ENOTSUPP;
  1348. + return -EOPNOTSUPP;
  1349. }
  1350. -
  1351. - ret = mvebu_a3700_comphy_smc(COMPHY_SIP_POWER_ON, lane->id, fw_param);
  1352. - if (ret == -EOPNOTSUPP)
  1353. - dev_err(lane->dev,
  1354. - "unsupported SMC call, try updating your firmware\n");
  1355. -
  1356. - return ret;
  1357. }
  1358. static int mvebu_a3700_comphy_power_off(struct phy *phy)
  1359. {
  1360. struct mvebu_a3700_comphy_lane *lane = phy_get_drvdata(phy);
  1361. - return mvebu_a3700_comphy_smc(COMPHY_SIP_POWER_OFF, lane->id, 0);
  1362. + switch (lane->mode) {
  1363. + case PHY_MODE_USB_HOST_SS:
  1364. + /*
  1365. + * The USB3 MAC sets the USB3 PHY to low state, so we do not
  1366. + * need to power off USB3 PHY again.
  1367. + */
  1368. + break;
  1369. +
  1370. + case PHY_MODE_SATA:
  1371. + mvebu_a3700_comphy_sata_power_off(lane);
  1372. + break;
  1373. +
  1374. + case PHY_MODE_ETHERNET:
  1375. + mvebu_a3700_comphy_ethernet_power_off(lane);
  1376. + break;
  1377. +
  1378. + case PHY_MODE_PCIE:
  1379. + mvebu_a3700_comphy_pcie_power_off(lane);
  1380. + break;
  1381. +
  1382. + default:
  1383. + dev_err(lane->dev, "invalid COMPHY mode\n");
  1384. + return -EINVAL;
  1385. + }
  1386. +
  1387. + return 0;
  1388. }
  1389. static const struct phy_ops mvebu_a3700_comphy_ops = {
  1390. .power_on = mvebu_a3700_comphy_power_on,
  1391. .power_off = mvebu_a3700_comphy_power_off,
  1392. + .reset = mvebu_a3700_comphy_reset,
  1393. .set_mode = mvebu_a3700_comphy_set_mode,
  1394. .owner = THIS_MODULE,
  1395. };
  1396. @@ -256,13 +1279,75 @@ static struct phy *mvebu_a3700_comphy_xl
  1397. return ERR_PTR(-EINVAL);
  1398. }
  1399. + lane->invert_tx = args->args[1] & BIT(0);
  1400. + lane->invert_rx = args->args[1] & BIT(1);
  1401. +
  1402. return phy;
  1403. }
  1404. static int mvebu_a3700_comphy_probe(struct platform_device *pdev)
  1405. {
  1406. + struct mvebu_a3700_comphy_priv *priv;
  1407. struct phy_provider *provider;
  1408. struct device_node *child;
  1409. + struct resource *res;
  1410. + struct clk *clk;
  1411. + int ret;
  1412. +
  1413. + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  1414. + if (!priv)
  1415. + return -ENOMEM;
  1416. +
  1417. + spin_lock_init(&priv->lock);
  1418. +
  1419. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "comphy");
  1420. + priv->comphy_regs = devm_ioremap_resource(&pdev->dev, res);
  1421. + if (IS_ERR(priv->comphy_regs))
  1422. + return PTR_ERR(priv->comphy_regs);
  1423. +
  1424. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1425. + "lane1_pcie_gbe");
  1426. + priv->lane1_phy_regs = devm_ioremap_resource(&pdev->dev, res);
  1427. + if (IS_ERR(priv->lane1_phy_regs))
  1428. + return PTR_ERR(priv->lane1_phy_regs);
  1429. +
  1430. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1431. + "lane0_usb3_gbe");
  1432. + priv->lane0_phy_regs = devm_ioremap_resource(&pdev->dev, res);
  1433. + if (IS_ERR(priv->lane0_phy_regs))
  1434. + return PTR_ERR(priv->lane0_phy_regs);
  1435. +
  1436. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1437. + "lane2_sata_usb3");
  1438. + priv->lane2_phy_indirect = devm_ioremap_resource(&pdev->dev, res);
  1439. + if (IS_ERR(priv->lane2_phy_indirect))
  1440. + return PTR_ERR(priv->lane2_phy_indirect);
  1441. +
  1442. + /*
  1443. + * Driver needs to know if reference xtal clock is 40MHz or 25MHz.
  1444. + * Old DT bindings do not have xtal clk present. So do not fail here
  1445. + * and expects that default 25MHz reference clock is used.
  1446. + */
  1447. + clk = clk_get(&pdev->dev, "xtal");
  1448. + if (IS_ERR(clk)) {
  1449. + if (PTR_ERR(clk) == -EPROBE_DEFER)
  1450. + return -EPROBE_DEFER;
  1451. + dev_warn(&pdev->dev, "missing 'xtal' clk (%ld)\n",
  1452. + PTR_ERR(clk));
  1453. + } else {
  1454. + ret = clk_prepare_enable(clk);
  1455. + if (ret) {
  1456. + dev_warn(&pdev->dev, "enabling xtal clk failed (%d)\n",
  1457. + ret);
  1458. + } else {
  1459. + if (clk_get_rate(clk) == 40000000)
  1460. + priv->xtal_is_40m = true;
  1461. + clk_disable_unprepare(clk);
  1462. + }
  1463. + clk_put(clk);
  1464. + }
  1465. +
  1466. + dev_set_drvdata(&pdev->dev, priv);
  1467. for_each_available_child_of_node(pdev->dev.of_node, child) {
  1468. struct mvebu_a3700_comphy_lane *lane;
  1469. @@ -277,7 +1362,7 @@ static int mvebu_a3700_comphy_probe(stru
  1470. continue;
  1471. }
  1472. - if (lane_id >= MVEBU_A3700_COMPHY_LANES) {
  1473. + if (lane_id >= 3) {
  1474. dev_err(&pdev->dev, "invalid 'reg' property\n");
  1475. continue;
  1476. }
  1477. @@ -295,15 +1380,26 @@ static int mvebu_a3700_comphy_probe(stru
  1478. return PTR_ERR(phy);
  1479. }
  1480. + lane->priv = priv;
  1481. lane->dev = &pdev->dev;
  1482. lane->mode = PHY_MODE_INVALID;
  1483. lane->submode = PHY_INTERFACE_MODE_NA;
  1484. lane->id = lane_id;
  1485. + lane->invert_tx = false;
  1486. + lane->invert_rx = false;
  1487. phy_set_drvdata(phy, lane);
  1488. +
  1489. + /*
  1490. + * To avoid relying on the bootloader/firmware configuration,
  1491. + * power off all comphys.
  1492. + */
  1493. + mvebu_a3700_comphy_reset(phy);
  1494. + lane->needs_reset = false;
  1495. }
  1496. provider = devm_of_phy_provider_register(&pdev->dev,
  1497. mvebu_a3700_comphy_xlate);
  1498. +
  1499. return PTR_ERR_OR_ZERO(provider);
  1500. }
  1501. @@ -323,5 +1419,7 @@ static struct platform_driver mvebu_a370
  1502. module_platform_driver(mvebu_a3700_comphy_driver);
  1503. MODULE_AUTHOR("Miquèl Raynal <[email protected]>");
  1504. +MODULE_AUTHOR("Pali Rohár <[email protected]>");
  1505. +MODULE_AUTHOR("Marek Behún <[email protected]>");
  1506. MODULE_DESCRIPTION("Common PHY driver for A3700");
  1507. MODULE_LICENSE("GPL v2");