phy-miphy28lp.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014 STMicroelectronics
  4. *
  5. * STMicroelectronics PHY driver MiPHY28lp (for SoC STiH407).
  6. *
  7. * Author: Alexandre Torgue <alexandre.torgue@st.com>
  8. */
  9. #include <linux/platform_device.h>
  10. #include <linux/io.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/of_address.h>
  17. #include <linux/clk.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/delay.h>
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/regmap.h>
  22. #include <linux/reset.h>
  23. #include <dt-bindings/phy/phy.h>
  24. /* MiPHY registers */
  25. #define MIPHY_CONF_RESET 0x00
  26. #define RST_APPLI_SW BIT(0)
  27. #define RST_CONF_SW BIT(1)
  28. #define RST_MACRO_SW BIT(2)
  29. #define MIPHY_RESET 0x01
  30. #define RST_PLL_SW BIT(0)
  31. #define RST_COMP_SW BIT(2)
  32. #define MIPHY_STATUS_1 0x02
  33. #define PHY_RDY BIT(0)
  34. #define HFC_RDY BIT(1)
  35. #define HFC_PLL BIT(2)
  36. #define MIPHY_CONTROL 0x04
  37. #define TERM_EN_SW BIT(2)
  38. #define DIS_LINK_RST BIT(3)
  39. #define AUTO_RST_RX BIT(4)
  40. #define PX_RX_POL BIT(5)
  41. #define MIPHY_BOUNDARY_SEL 0x0a
  42. #define TX_SEL BIT(6)
  43. #define SSC_SEL BIT(4)
  44. #define GENSEL_SEL BIT(0)
  45. #define MIPHY_BOUNDARY_1 0x0b
  46. #define MIPHY_BOUNDARY_2 0x0c
  47. #define SSC_EN_SW BIT(2)
  48. #define MIPHY_PLL_CLKREF_FREQ 0x0d
  49. #define MIPHY_SPEED 0x0e
  50. #define TX_SPDSEL_80DEC 0
  51. #define TX_SPDSEL_40DEC 1
  52. #define TX_SPDSEL_20DEC 2
  53. #define RX_SPDSEL_80DEC 0
  54. #define RX_SPDSEL_40DEC (1 << 2)
  55. #define RX_SPDSEL_20DEC (2 << 2)
  56. #define MIPHY_CONF 0x0f
  57. #define MIPHY_CTRL_TEST_SEL 0x20
  58. #define MIPHY_CTRL_TEST_1 0x21
  59. #define MIPHY_CTRL_TEST_2 0x22
  60. #define MIPHY_CTRL_TEST_3 0x23
  61. #define MIPHY_CTRL_TEST_4 0x24
  62. #define MIPHY_FEEDBACK_TEST 0x25
  63. #define MIPHY_DEBUG_BUS 0x26
  64. #define MIPHY_DEBUG_STATUS_MSB 0x27
  65. #define MIPHY_DEBUG_STATUS_LSB 0x28
  66. #define MIPHY_PWR_RAIL_1 0x29
  67. #define MIPHY_PWR_RAIL_2 0x2a
  68. #define MIPHY_SYNCHAR_CONTROL 0x30
  69. #define MIPHY_COMP_FSM_1 0x3a
  70. #define COMP_START BIT(6)
  71. #define MIPHY_COMP_FSM_6 0x3f
  72. #define COMP_DONE BIT(7)
  73. #define MIPHY_COMP_POSTP 0x42
  74. #define MIPHY_TX_CTRL_1 0x49
  75. #define TX_REG_STEP_0V 0
  76. #define TX_REG_STEP_P_25MV 1
  77. #define TX_REG_STEP_P_50MV 2
  78. #define TX_REG_STEP_N_25MV 7
  79. #define TX_REG_STEP_N_50MV 6
  80. #define TX_REG_STEP_N_75MV 5
  81. #define MIPHY_TX_CTRL_2 0x4a
  82. #define TX_SLEW_SW_40_PS 0
  83. #define TX_SLEW_SW_80_PS 1
  84. #define TX_SLEW_SW_120_PS 2
  85. #define MIPHY_TX_CTRL_3 0x4b
  86. #define MIPHY_TX_CAL_MAN 0x4e
  87. #define TX_SLEW_CAL_MAN_EN BIT(0)
  88. #define MIPHY_TST_BIAS_BOOST_2 0x62
  89. #define MIPHY_BIAS_BOOST_1 0x63
  90. #define MIPHY_BIAS_BOOST_2 0x64
  91. #define MIPHY_RX_DESBUFF_FDB_2 0x67
  92. #define MIPHY_RX_DESBUFF_FDB_3 0x68
  93. #define MIPHY_SIGDET_COMPENS1 0x69
  94. #define MIPHY_SIGDET_COMPENS2 0x6a
  95. #define MIPHY_JITTER_PERIOD 0x6b
  96. #define MIPHY_JITTER_AMPLITUDE_1 0x6c
  97. #define MIPHY_JITTER_AMPLITUDE_2 0x6d
  98. #define MIPHY_JITTER_AMPLITUDE_3 0x6e
  99. #define MIPHY_RX_K_GAIN 0x78
  100. #define MIPHY_RX_BUFFER_CTRL 0x7a
  101. #define VGA_GAIN BIT(0)
  102. #define EQ_DC_GAIN BIT(2)
  103. #define EQ_BOOST_GAIN BIT(3)
  104. #define MIPHY_RX_VGA_GAIN 0x7b
  105. #define MIPHY_RX_EQU_GAIN_1 0x7f
  106. #define MIPHY_RX_EQU_GAIN_2 0x80
  107. #define MIPHY_RX_EQU_GAIN_3 0x81
  108. #define MIPHY_RX_CAL_CTRL_1 0x97
  109. #define MIPHY_RX_CAL_CTRL_2 0x98
  110. #define MIPHY_RX_CAL_OFFSET_CTRL 0x99
  111. #define CAL_OFFSET_VGA_64 (0x03 << 0)
  112. #define CAL_OFFSET_THRESHOLD_64 (0x03 << 2)
  113. #define VGA_OFFSET_POLARITY BIT(4)
  114. #define OFFSET_COMPENSATION_EN BIT(6)
  115. #define MIPHY_RX_CAL_VGA_STEP 0x9a
  116. #define MIPHY_RX_CAL_EYE_MIN 0x9d
  117. #define MIPHY_RX_CAL_OPT_LENGTH 0x9f
  118. #define MIPHY_RX_LOCK_CTRL_1 0xc1
  119. #define MIPHY_RX_LOCK_SETTINGS_OPT 0xc2
  120. #define MIPHY_RX_LOCK_STEP 0xc4
  121. #define MIPHY_RX_SIGDET_SLEEP_OA 0xc9
  122. #define MIPHY_RX_SIGDET_SLEEP_SEL 0xca
  123. #define MIPHY_RX_SIGDET_WAIT_SEL 0xcb
  124. #define MIPHY_RX_SIGDET_DATA_SEL 0xcc
  125. #define EN_ULTRA_LOW_POWER BIT(0)
  126. #define EN_FIRST_HALF BIT(1)
  127. #define EN_SECOND_HALF BIT(2)
  128. #define EN_DIGIT_SIGNAL_CHECK BIT(3)
  129. #define MIPHY_RX_POWER_CTRL_1 0xcd
  130. #define MIPHY_RX_POWER_CTRL_2 0xce
  131. #define MIPHY_PLL_CALSET_CTRL 0xd3
  132. #define MIPHY_PLL_CALSET_1 0xd4
  133. #define MIPHY_PLL_CALSET_2 0xd5
  134. #define MIPHY_PLL_CALSET_3 0xd6
  135. #define MIPHY_PLL_CALSET_4 0xd7
  136. #define MIPHY_PLL_SBR_1 0xe3
  137. #define SET_NEW_CHANGE BIT(1)
  138. #define MIPHY_PLL_SBR_2 0xe4
  139. #define MIPHY_PLL_SBR_3 0xe5
  140. #define MIPHY_PLL_SBR_4 0xe6
  141. #define MIPHY_PLL_COMMON_MISC_2 0xe9
  142. #define START_ACT_FILT BIT(6)
  143. #define MIPHY_PLL_SPAREIN 0xeb
  144. /*
  145. * On STiH407 the glue logic can be different among MiPHY devices; for example:
  146. * MiPHY0: OSC_FORCE_EXT means:
  147. * 0: 30MHz crystal clk - 1: 100MHz ext clk routed through MiPHY1
  148. * MiPHY1: OSC_FORCE_EXT means:
  149. * 1: 30MHz crystal clk - 0: 100MHz ext clk routed through MiPHY1
  150. * Some devices have not the possibility to check if the osc is ready.
  151. */
  152. #define MIPHY_OSC_FORCE_EXT BIT(3)
  153. #define MIPHY_OSC_RDY BIT(5)
  154. #define MIPHY_CTRL_MASK 0x0f
  155. #define MIPHY_CTRL_DEFAULT 0
  156. #define MIPHY_CTRL_SYNC_D_EN BIT(2)
  157. /* SATA / PCIe defines */
  158. #define SATA_CTRL_MASK 0x07
  159. #define PCIE_CTRL_MASK 0xff
  160. #define SATA_CTRL_SELECT_SATA 1
  161. #define SATA_CTRL_SELECT_PCIE 0
  162. #define SYSCFG_PCIE_PCIE_VAL 0x80
  163. #define SATA_SPDMODE 1
  164. #define MIPHY_SATA_BANK_NB 3
  165. #define MIPHY_PCIE_BANK_NB 2
  166. enum {
  167. SYSCFG_CTRL,
  168. SYSCFG_STATUS,
  169. SYSCFG_PCI,
  170. SYSCFG_SATA,
  171. SYSCFG_REG_MAX,
  172. };
  173. struct miphy28lp_phy {
  174. struct phy *phy;
  175. struct miphy28lp_dev *phydev;
  176. void __iomem *base;
  177. void __iomem *pipebase;
  178. bool osc_force_ext;
  179. bool osc_rdy;
  180. bool px_rx_pol_inv;
  181. bool ssc;
  182. bool tx_impedance;
  183. struct reset_control *miphy_rst;
  184. u32 sata_gen;
  185. /* Sysconfig registers offsets needed to configure the device */
  186. u32 syscfg_reg[SYSCFG_REG_MAX];
  187. u8 type;
  188. };
  189. struct miphy28lp_dev {
  190. struct device *dev;
  191. struct regmap *regmap;
  192. struct mutex miphy_mutex;
  193. struct miphy28lp_phy **phys;
  194. int nphys;
  195. };
  196. enum miphy_sata_gen { SATA_GEN1, SATA_GEN2, SATA_GEN3 };
  197. static char *PHY_TYPE_name[] = { "sata-up", "pcie-up", "", "usb3-up" };
  198. struct pll_ratio {
  199. int clk_ref;
  200. int calset_1;
  201. int calset_2;
  202. int calset_3;
  203. int calset_4;
  204. int cal_ctrl;
  205. };
  206. static struct pll_ratio sata_pll_ratio = {
  207. .clk_ref = 0x1e,
  208. .calset_1 = 0xc8,
  209. .calset_2 = 0x00,
  210. .calset_3 = 0x00,
  211. .calset_4 = 0x00,
  212. .cal_ctrl = 0x00,
  213. };
  214. static struct pll_ratio pcie_pll_ratio = {
  215. .clk_ref = 0x1e,
  216. .calset_1 = 0xa6,
  217. .calset_2 = 0xaa,
  218. .calset_3 = 0xaa,
  219. .calset_4 = 0x00,
  220. .cal_ctrl = 0x00,
  221. };
  222. static struct pll_ratio usb3_pll_ratio = {
  223. .clk_ref = 0x1e,
  224. .calset_1 = 0xa6,
  225. .calset_2 = 0xaa,
  226. .calset_3 = 0xaa,
  227. .calset_4 = 0x04,
  228. .cal_ctrl = 0x00,
  229. };
  230. struct miphy28lp_pll_gen {
  231. int bank;
  232. int speed;
  233. int bias_boost_1;
  234. int bias_boost_2;
  235. int tx_ctrl_1;
  236. int tx_ctrl_2;
  237. int tx_ctrl_3;
  238. int rx_k_gain;
  239. int rx_vga_gain;
  240. int rx_equ_gain_1;
  241. int rx_equ_gain_2;
  242. int rx_equ_gain_3;
  243. int rx_buff_ctrl;
  244. };
  245. static struct miphy28lp_pll_gen sata_pll_gen[] = {
  246. {
  247. .bank = 0x00,
  248. .speed = TX_SPDSEL_80DEC | RX_SPDSEL_80DEC,
  249. .bias_boost_1 = 0x00,
  250. .bias_boost_2 = 0xae,
  251. .tx_ctrl_2 = 0x53,
  252. .tx_ctrl_3 = 0x00,
  253. .rx_buff_ctrl = EQ_BOOST_GAIN | EQ_DC_GAIN | VGA_GAIN,
  254. .rx_vga_gain = 0x00,
  255. .rx_equ_gain_1 = 0x7d,
  256. .rx_equ_gain_2 = 0x56,
  257. .rx_equ_gain_3 = 0x00,
  258. },
  259. {
  260. .bank = 0x01,
  261. .speed = TX_SPDSEL_40DEC | RX_SPDSEL_40DEC,
  262. .bias_boost_1 = 0x00,
  263. .bias_boost_2 = 0xae,
  264. .tx_ctrl_2 = 0x72,
  265. .tx_ctrl_3 = 0x20,
  266. .rx_buff_ctrl = EQ_BOOST_GAIN | EQ_DC_GAIN | VGA_GAIN,
  267. .rx_vga_gain = 0x00,
  268. .rx_equ_gain_1 = 0x7d,
  269. .rx_equ_gain_2 = 0x56,
  270. .rx_equ_gain_3 = 0x00,
  271. },
  272. {
  273. .bank = 0x02,
  274. .speed = TX_SPDSEL_20DEC | RX_SPDSEL_20DEC,
  275. .bias_boost_1 = 0x00,
  276. .bias_boost_2 = 0xae,
  277. .tx_ctrl_2 = 0xc0,
  278. .tx_ctrl_3 = 0x20,
  279. .rx_buff_ctrl = EQ_BOOST_GAIN | EQ_DC_GAIN | VGA_GAIN,
  280. .rx_vga_gain = 0x00,
  281. .rx_equ_gain_1 = 0x7d,
  282. .rx_equ_gain_2 = 0x56,
  283. .rx_equ_gain_3 = 0x00,
  284. },
  285. };
  286. static struct miphy28lp_pll_gen pcie_pll_gen[] = {
  287. {
  288. .bank = 0x00,
  289. .speed = TX_SPDSEL_40DEC | RX_SPDSEL_40DEC,
  290. .bias_boost_1 = 0x00,
  291. .bias_boost_2 = 0xa5,
  292. .tx_ctrl_1 = TX_REG_STEP_N_25MV,
  293. .tx_ctrl_2 = 0x71,
  294. .tx_ctrl_3 = 0x60,
  295. .rx_k_gain = 0x98,
  296. .rx_buff_ctrl = EQ_BOOST_GAIN | EQ_DC_GAIN | VGA_GAIN,
  297. .rx_vga_gain = 0x00,
  298. .rx_equ_gain_1 = 0x79,
  299. .rx_equ_gain_2 = 0x56,
  300. },
  301. {
  302. .bank = 0x01,
  303. .speed = TX_SPDSEL_20DEC | RX_SPDSEL_20DEC,
  304. .bias_boost_1 = 0x00,
  305. .bias_boost_2 = 0xa5,
  306. .tx_ctrl_1 = TX_REG_STEP_N_25MV,
  307. .tx_ctrl_2 = 0x70,
  308. .tx_ctrl_3 = 0x60,
  309. .rx_k_gain = 0xcc,
  310. .rx_buff_ctrl = EQ_BOOST_GAIN | EQ_DC_GAIN | VGA_GAIN,
  311. .rx_vga_gain = 0x00,
  312. .rx_equ_gain_1 = 0x78,
  313. .rx_equ_gain_2 = 0x07,
  314. },
  315. };
  316. static inline void miphy28lp_set_reset(struct miphy28lp_phy *miphy_phy)
  317. {
  318. void __iomem *base = miphy_phy->base;
  319. u8 val;
  320. /* Putting Macro in reset */
  321. writeb_relaxed(RST_APPLI_SW, base + MIPHY_CONF_RESET);
  322. val = RST_APPLI_SW | RST_CONF_SW;
  323. writeb_relaxed(val, base + MIPHY_CONF_RESET);
  324. writeb_relaxed(RST_APPLI_SW, base + MIPHY_CONF_RESET);
  325. /* Bringing the MIPHY-CPU registers out of reset */
  326. if (miphy_phy->type == PHY_TYPE_PCIE) {
  327. val = AUTO_RST_RX | TERM_EN_SW;
  328. writeb_relaxed(val, base + MIPHY_CONTROL);
  329. } else {
  330. val = AUTO_RST_RX | TERM_EN_SW | DIS_LINK_RST;
  331. writeb_relaxed(val, base + MIPHY_CONTROL);
  332. }
  333. }
  334. static inline void miphy28lp_pll_calibration(struct miphy28lp_phy *miphy_phy,
  335. struct pll_ratio *pll_ratio)
  336. {
  337. void __iomem *base = miphy_phy->base;
  338. u8 val;
  339. /* Applying PLL Settings */
  340. writeb_relaxed(0x1d, base + MIPHY_PLL_SPAREIN);
  341. writeb_relaxed(pll_ratio->clk_ref, base + MIPHY_PLL_CLKREF_FREQ);
  342. /* PLL Ratio */
  343. writeb_relaxed(pll_ratio->calset_1, base + MIPHY_PLL_CALSET_1);
  344. writeb_relaxed(pll_ratio->calset_2, base + MIPHY_PLL_CALSET_2);
  345. writeb_relaxed(pll_ratio->calset_3, base + MIPHY_PLL_CALSET_3);
  346. writeb_relaxed(pll_ratio->calset_4, base + MIPHY_PLL_CALSET_4);
  347. writeb_relaxed(pll_ratio->cal_ctrl, base + MIPHY_PLL_CALSET_CTRL);
  348. writeb_relaxed(TX_SEL, base + MIPHY_BOUNDARY_SEL);
  349. val = (0x68 << 1) | TX_SLEW_CAL_MAN_EN;
  350. writeb_relaxed(val, base + MIPHY_TX_CAL_MAN);
  351. val = VGA_OFFSET_POLARITY | CAL_OFFSET_THRESHOLD_64 | CAL_OFFSET_VGA_64;
  352. if (miphy_phy->type != PHY_TYPE_SATA)
  353. val |= OFFSET_COMPENSATION_EN;
  354. writeb_relaxed(val, base + MIPHY_RX_CAL_OFFSET_CTRL);
  355. if (miphy_phy->type == PHY_TYPE_USB3) {
  356. writeb_relaxed(0x00, base + MIPHY_CONF);
  357. writeb_relaxed(0x70, base + MIPHY_RX_LOCK_STEP);
  358. writeb_relaxed(EN_FIRST_HALF, base + MIPHY_RX_SIGDET_SLEEP_OA);
  359. writeb_relaxed(EN_FIRST_HALF, base + MIPHY_RX_SIGDET_SLEEP_SEL);
  360. writeb_relaxed(EN_FIRST_HALF, base + MIPHY_RX_SIGDET_WAIT_SEL);
  361. val = EN_DIGIT_SIGNAL_CHECK | EN_FIRST_HALF;
  362. writeb_relaxed(val, base + MIPHY_RX_SIGDET_DATA_SEL);
  363. }
  364. }
  365. static inline void miphy28lp_sata_config_gen(struct miphy28lp_phy *miphy_phy)
  366. {
  367. void __iomem *base = miphy_phy->base;
  368. int i;
  369. for (i = 0; i < ARRAY_SIZE(sata_pll_gen); i++) {
  370. struct miphy28lp_pll_gen *gen = &sata_pll_gen[i];
  371. /* Banked settings */
  372. writeb_relaxed(gen->bank, base + MIPHY_CONF);
  373. writeb_relaxed(gen->speed, base + MIPHY_SPEED);
  374. writeb_relaxed(gen->bias_boost_1, base + MIPHY_BIAS_BOOST_1);
  375. writeb_relaxed(gen->bias_boost_2, base + MIPHY_BIAS_BOOST_2);
  376. /* TX buffer Settings */
  377. writeb_relaxed(gen->tx_ctrl_2, base + MIPHY_TX_CTRL_2);
  378. writeb_relaxed(gen->tx_ctrl_3, base + MIPHY_TX_CTRL_3);
  379. /* RX Buffer Settings */
  380. writeb_relaxed(gen->rx_buff_ctrl, base + MIPHY_RX_BUFFER_CTRL);
  381. writeb_relaxed(gen->rx_vga_gain, base + MIPHY_RX_VGA_GAIN);
  382. writeb_relaxed(gen->rx_equ_gain_1, base + MIPHY_RX_EQU_GAIN_1);
  383. writeb_relaxed(gen->rx_equ_gain_2, base + MIPHY_RX_EQU_GAIN_2);
  384. writeb_relaxed(gen->rx_equ_gain_3, base + MIPHY_RX_EQU_GAIN_3);
  385. }
  386. }
  387. static inline void miphy28lp_pcie_config_gen(struct miphy28lp_phy *miphy_phy)
  388. {
  389. void __iomem *base = miphy_phy->base;
  390. int i;
  391. for (i = 0; i < ARRAY_SIZE(pcie_pll_gen); i++) {
  392. struct miphy28lp_pll_gen *gen = &pcie_pll_gen[i];
  393. /* Banked settings */
  394. writeb_relaxed(gen->bank, base + MIPHY_CONF);
  395. writeb_relaxed(gen->speed, base + MIPHY_SPEED);
  396. writeb_relaxed(gen->bias_boost_1, base + MIPHY_BIAS_BOOST_1);
  397. writeb_relaxed(gen->bias_boost_2, base + MIPHY_BIAS_BOOST_2);
  398. /* TX buffer Settings */
  399. writeb_relaxed(gen->tx_ctrl_1, base + MIPHY_TX_CTRL_1);
  400. writeb_relaxed(gen->tx_ctrl_2, base + MIPHY_TX_CTRL_2);
  401. writeb_relaxed(gen->tx_ctrl_3, base + MIPHY_TX_CTRL_3);
  402. writeb_relaxed(gen->rx_k_gain, base + MIPHY_RX_K_GAIN);
  403. /* RX Buffer Settings */
  404. writeb_relaxed(gen->rx_buff_ctrl, base + MIPHY_RX_BUFFER_CTRL);
  405. writeb_relaxed(gen->rx_vga_gain, base + MIPHY_RX_VGA_GAIN);
  406. writeb_relaxed(gen->rx_equ_gain_1, base + MIPHY_RX_EQU_GAIN_1);
  407. writeb_relaxed(gen->rx_equ_gain_2, base + MIPHY_RX_EQU_GAIN_2);
  408. }
  409. }
  410. static inline int miphy28lp_wait_compensation(struct miphy28lp_phy *miphy_phy)
  411. {
  412. u8 val;
  413. /* Waiting for Compensation to complete */
  414. return readb_relaxed_poll_timeout(miphy_phy->base + MIPHY_COMP_FSM_6,
  415. val, val & COMP_DONE, 1, 5 * USEC_PER_SEC);
  416. }
  417. static inline int miphy28lp_compensation(struct miphy28lp_phy *miphy_phy,
  418. struct pll_ratio *pll_ratio)
  419. {
  420. void __iomem *base = miphy_phy->base;
  421. /* Poll for HFC ready after reset release */
  422. /* Compensation measurement */
  423. writeb_relaxed(RST_PLL_SW | RST_COMP_SW, base + MIPHY_RESET);
  424. writeb_relaxed(0x00, base + MIPHY_PLL_COMMON_MISC_2);
  425. writeb_relaxed(pll_ratio->clk_ref, base + MIPHY_PLL_CLKREF_FREQ);
  426. writeb_relaxed(COMP_START, base + MIPHY_COMP_FSM_1);
  427. if (miphy_phy->type == PHY_TYPE_PCIE)
  428. writeb_relaxed(RST_PLL_SW, base + MIPHY_RESET);
  429. writeb_relaxed(0x00, base + MIPHY_RESET);
  430. writeb_relaxed(START_ACT_FILT, base + MIPHY_PLL_COMMON_MISC_2);
  431. writeb_relaxed(SET_NEW_CHANGE, base + MIPHY_PLL_SBR_1);
  432. /* TX compensation offset to re-center TX impedance */
  433. writeb_relaxed(0x00, base + MIPHY_COMP_POSTP);
  434. if (miphy_phy->type == PHY_TYPE_PCIE)
  435. return miphy28lp_wait_compensation(miphy_phy);
  436. return 0;
  437. }
  438. static inline void miphy28_usb3_miphy_reset(struct miphy28lp_phy *miphy_phy)
  439. {
  440. void __iomem *base = miphy_phy->base;
  441. u8 val;
  442. /* MIPHY Reset */
  443. writeb_relaxed(RST_APPLI_SW, base + MIPHY_CONF_RESET);
  444. writeb_relaxed(0x00, base + MIPHY_CONF_RESET);
  445. writeb_relaxed(RST_COMP_SW, base + MIPHY_RESET);
  446. val = RST_COMP_SW | RST_PLL_SW;
  447. writeb_relaxed(val, base + MIPHY_RESET);
  448. writeb_relaxed(0x00, base + MIPHY_PLL_COMMON_MISC_2);
  449. writeb_relaxed(0x1e, base + MIPHY_PLL_CLKREF_FREQ);
  450. writeb_relaxed(COMP_START, base + MIPHY_COMP_FSM_1);
  451. writeb_relaxed(RST_PLL_SW, base + MIPHY_RESET);
  452. writeb_relaxed(0x00, base + MIPHY_RESET);
  453. writeb_relaxed(START_ACT_FILT, base + MIPHY_PLL_COMMON_MISC_2);
  454. writeb_relaxed(0x00, base + MIPHY_CONF);
  455. writeb_relaxed(0x00, base + MIPHY_BOUNDARY_1);
  456. writeb_relaxed(0x00, base + MIPHY_TST_BIAS_BOOST_2);
  457. writeb_relaxed(0x00, base + MIPHY_CONF);
  458. writeb_relaxed(SET_NEW_CHANGE, base + MIPHY_PLL_SBR_1);
  459. writeb_relaxed(0xa5, base + MIPHY_DEBUG_BUS);
  460. writeb_relaxed(0x00, base + MIPHY_CONF);
  461. }
  462. static void miphy_sata_tune_ssc(struct miphy28lp_phy *miphy_phy)
  463. {
  464. void __iomem *base = miphy_phy->base;
  465. u8 val;
  466. /* Compensate Tx impedance to avoid out of range values */
  467. /*
  468. * Enable the SSC on PLL for all banks
  469. * SSC Modulation @ 31 KHz and 4000 ppm modulation amp
  470. */
  471. val = readb_relaxed(base + MIPHY_BOUNDARY_2);
  472. val |= SSC_EN_SW;
  473. writeb_relaxed(val, base + MIPHY_BOUNDARY_2);
  474. val = readb_relaxed(base + MIPHY_BOUNDARY_SEL);
  475. val |= SSC_SEL;
  476. writeb_relaxed(val, base + MIPHY_BOUNDARY_SEL);
  477. for (val = 0; val < MIPHY_SATA_BANK_NB; val++) {
  478. writeb_relaxed(val, base + MIPHY_CONF);
  479. /* Add value to each reference clock cycle */
  480. /* and define the period length of the SSC */
  481. writeb_relaxed(0x3c, base + MIPHY_PLL_SBR_2);
  482. writeb_relaxed(0x6c, base + MIPHY_PLL_SBR_3);
  483. writeb_relaxed(0x81, base + MIPHY_PLL_SBR_4);
  484. /* Clear any previous request */
  485. writeb_relaxed(0x00, base + MIPHY_PLL_SBR_1);
  486. /* requests the PLL to take in account new parameters */
  487. writeb_relaxed(SET_NEW_CHANGE, base + MIPHY_PLL_SBR_1);
  488. /* To be sure there is no other pending requests */
  489. writeb_relaxed(0x00, base + MIPHY_PLL_SBR_1);
  490. }
  491. }
  492. static void miphy_pcie_tune_ssc(struct miphy28lp_phy *miphy_phy)
  493. {
  494. void __iomem *base = miphy_phy->base;
  495. u8 val;
  496. /* Compensate Tx impedance to avoid out of range values */
  497. /*
  498. * Enable the SSC on PLL for all banks
  499. * SSC Modulation @ 31 KHz and 4000 ppm modulation amp
  500. */
  501. val = readb_relaxed(base + MIPHY_BOUNDARY_2);
  502. val |= SSC_EN_SW;
  503. writeb_relaxed(val, base + MIPHY_BOUNDARY_2);
  504. val = readb_relaxed(base + MIPHY_BOUNDARY_SEL);
  505. val |= SSC_SEL;
  506. writeb_relaxed(val, base + MIPHY_BOUNDARY_SEL);
  507. for (val = 0; val < MIPHY_PCIE_BANK_NB; val++) {
  508. writeb_relaxed(val, base + MIPHY_CONF);
  509. /* Validate Step component */
  510. writeb_relaxed(0x69, base + MIPHY_PLL_SBR_3);
  511. writeb_relaxed(0x21, base + MIPHY_PLL_SBR_4);
  512. /* Validate Period component */
  513. writeb_relaxed(0x3c, base + MIPHY_PLL_SBR_2);
  514. writeb_relaxed(0x21, base + MIPHY_PLL_SBR_4);
  515. /* Clear any previous request */
  516. writeb_relaxed(0x00, base + MIPHY_PLL_SBR_1);
  517. /* requests the PLL to take in account new parameters */
  518. writeb_relaxed(SET_NEW_CHANGE, base + MIPHY_PLL_SBR_1);
  519. /* To be sure there is no other pending requests */
  520. writeb_relaxed(0x00, base + MIPHY_PLL_SBR_1);
  521. }
  522. }
  523. static inline void miphy_tune_tx_impedance(struct miphy28lp_phy *miphy_phy)
  524. {
  525. /* Compensate Tx impedance to avoid out of range values */
  526. writeb_relaxed(0x02, miphy_phy->base + MIPHY_COMP_POSTP);
  527. }
  528. static inline int miphy28lp_configure_sata(struct miphy28lp_phy *miphy_phy)
  529. {
  530. void __iomem *base = miphy_phy->base;
  531. int err;
  532. u8 val;
  533. /* Putting Macro in reset */
  534. miphy28lp_set_reset(miphy_phy);
  535. /* PLL calibration */
  536. miphy28lp_pll_calibration(miphy_phy, &sata_pll_ratio);
  537. /* Banked settings Gen1/Gen2/Gen3 */
  538. miphy28lp_sata_config_gen(miphy_phy);
  539. /* Power control */
  540. /* Input bridge enable, manual input bridge control */
  541. writeb_relaxed(0x21, base + MIPHY_RX_POWER_CTRL_1);
  542. /* Macro out of reset */
  543. writeb_relaxed(0x00, base + MIPHY_CONF_RESET);
  544. /* Poll for HFC ready after reset release */
  545. /* Compensation measurement */
  546. err = miphy28lp_compensation(miphy_phy, &sata_pll_ratio);
  547. if (err)
  548. return err;
  549. if (miphy_phy->px_rx_pol_inv) {
  550. /* Invert Rx polarity */
  551. val = readb_relaxed(miphy_phy->base + MIPHY_CONTROL);
  552. val |= PX_RX_POL;
  553. writeb_relaxed(val, miphy_phy->base + MIPHY_CONTROL);
  554. }
  555. if (miphy_phy->ssc)
  556. miphy_sata_tune_ssc(miphy_phy);
  557. if (miphy_phy->tx_impedance)
  558. miphy_tune_tx_impedance(miphy_phy);
  559. return 0;
  560. }
  561. static inline int miphy28lp_configure_pcie(struct miphy28lp_phy *miphy_phy)
  562. {
  563. void __iomem *base = miphy_phy->base;
  564. int err;
  565. /* Putting Macro in reset */
  566. miphy28lp_set_reset(miphy_phy);
  567. /* PLL calibration */
  568. miphy28lp_pll_calibration(miphy_phy, &pcie_pll_ratio);
  569. /* Banked settings Gen1/Gen2 */
  570. miphy28lp_pcie_config_gen(miphy_phy);
  571. /* Power control */
  572. /* Input bridge enable, manual input bridge control */
  573. writeb_relaxed(0x21, base + MIPHY_RX_POWER_CTRL_1);
  574. /* Macro out of reset */
  575. writeb_relaxed(0x00, base + MIPHY_CONF_RESET);
  576. /* Poll for HFC ready after reset release */
  577. /* Compensation measurement */
  578. err = miphy28lp_compensation(miphy_phy, &pcie_pll_ratio);
  579. if (err)
  580. return err;
  581. if (miphy_phy->ssc)
  582. miphy_pcie_tune_ssc(miphy_phy);
  583. if (miphy_phy->tx_impedance)
  584. miphy_tune_tx_impedance(miphy_phy);
  585. return 0;
  586. }
  587. static inline void miphy28lp_configure_usb3(struct miphy28lp_phy *miphy_phy)
  588. {
  589. void __iomem *base = miphy_phy->base;
  590. u8 val;
  591. /* Putting Macro in reset */
  592. miphy28lp_set_reset(miphy_phy);
  593. /* PLL calibration */
  594. miphy28lp_pll_calibration(miphy_phy, &usb3_pll_ratio);
  595. /* Writing The Speed Rate */
  596. writeb_relaxed(0x00, base + MIPHY_CONF);
  597. val = RX_SPDSEL_20DEC | TX_SPDSEL_20DEC;
  598. writeb_relaxed(val, base + MIPHY_SPEED);
  599. /* RX Channel compensation and calibration */
  600. writeb_relaxed(0x1c, base + MIPHY_RX_LOCK_SETTINGS_OPT);
  601. writeb_relaxed(0x51, base + MIPHY_RX_CAL_CTRL_1);
  602. writeb_relaxed(0x70, base + MIPHY_RX_CAL_CTRL_2);
  603. val = OFFSET_COMPENSATION_EN | VGA_OFFSET_POLARITY |
  604. CAL_OFFSET_THRESHOLD_64 | CAL_OFFSET_VGA_64;
  605. writeb_relaxed(val, base + MIPHY_RX_CAL_OFFSET_CTRL);
  606. writeb_relaxed(0x22, base + MIPHY_RX_CAL_VGA_STEP);
  607. writeb_relaxed(0x0e, base + MIPHY_RX_CAL_OPT_LENGTH);
  608. val = EQ_DC_GAIN | VGA_GAIN;
  609. writeb_relaxed(val, base + MIPHY_RX_BUFFER_CTRL);
  610. writeb_relaxed(0x78, base + MIPHY_RX_EQU_GAIN_1);
  611. writeb_relaxed(0x1b, base + MIPHY_SYNCHAR_CONTROL);
  612. /* TX compensation offset to re-center TX impedance */
  613. writeb_relaxed(0x02, base + MIPHY_COMP_POSTP);
  614. /* Enable GENSEL_SEL and SSC */
  615. /* TX_SEL=0 swing preemp forced by pipe registres */
  616. val = SSC_SEL | GENSEL_SEL;
  617. writeb_relaxed(val, base + MIPHY_BOUNDARY_SEL);
  618. /* MIPHY Bias boost */
  619. writeb_relaxed(0x00, base + MIPHY_BIAS_BOOST_1);
  620. writeb_relaxed(0xa7, base + MIPHY_BIAS_BOOST_2);
  621. /* SSC modulation */
  622. writeb_relaxed(SSC_EN_SW, base + MIPHY_BOUNDARY_2);
  623. /* MIPHY TX control */
  624. writeb_relaxed(0x00, base + MIPHY_CONF);
  625. /* Validate Step component */
  626. writeb_relaxed(0x5a, base + MIPHY_PLL_SBR_3);
  627. writeb_relaxed(0xa0, base + MIPHY_PLL_SBR_4);
  628. /* Validate Period component */
  629. writeb_relaxed(0x3c, base + MIPHY_PLL_SBR_2);
  630. writeb_relaxed(0xa1, base + MIPHY_PLL_SBR_4);
  631. /* Clear any previous request */
  632. writeb_relaxed(0x00, base + MIPHY_PLL_SBR_1);
  633. /* requests the PLL to take in account new parameters */
  634. writeb_relaxed(0x02, base + MIPHY_PLL_SBR_1);
  635. /* To be sure there is no other pending requests */
  636. writeb_relaxed(0x00, base + MIPHY_PLL_SBR_1);
  637. /* Rx PI controller settings */
  638. writeb_relaxed(0xca, base + MIPHY_RX_K_GAIN);
  639. /* MIPHY RX input bridge control */
  640. /* INPUT_BRIDGE_EN_SW=1, manual input bridge control[0]=1 */
  641. writeb_relaxed(0x21, base + MIPHY_RX_POWER_CTRL_1);
  642. writeb_relaxed(0x29, base + MIPHY_RX_POWER_CTRL_1);
  643. writeb_relaxed(0x1a, base + MIPHY_RX_POWER_CTRL_2);
  644. /* MIPHY Reset for usb3 */
  645. miphy28_usb3_miphy_reset(miphy_phy);
  646. }
  647. static inline int miphy_is_ready(struct miphy28lp_phy *miphy_phy)
  648. {
  649. u8 mask = HFC_PLL | HFC_RDY;
  650. u8 val;
  651. /*
  652. * For PCIe and USB3 check only that PLL and HFC are ready
  653. * For SATA check also that phy is ready!
  654. */
  655. if (miphy_phy->type == PHY_TYPE_SATA)
  656. mask |= PHY_RDY;
  657. return readb_relaxed_poll_timeout(miphy_phy->base + MIPHY_STATUS_1,
  658. val, (val & mask) == mask, 1,
  659. 5 * USEC_PER_SEC);
  660. }
  661. static int miphy_osc_is_ready(struct miphy28lp_phy *miphy_phy)
  662. {
  663. struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
  664. u32 val;
  665. if (!miphy_phy->osc_rdy)
  666. return 0;
  667. if (!miphy_phy->syscfg_reg[SYSCFG_STATUS])
  668. return -EINVAL;
  669. return regmap_read_poll_timeout(miphy_dev->regmap,
  670. miphy_phy->syscfg_reg[SYSCFG_STATUS],
  671. val, val & MIPHY_OSC_RDY, 1,
  672. 5 * USEC_PER_SEC);
  673. }
  674. static int miphy28lp_get_resource_byname(struct device_node *child,
  675. char *rname, struct resource *res)
  676. {
  677. int index;
  678. index = of_property_match_string(child, "reg-names", rname);
  679. if (index < 0)
  680. return -ENODEV;
  681. return of_address_to_resource(child, index, res);
  682. }
  683. static int miphy28lp_get_one_addr(struct device *dev,
  684. struct device_node *child, char *rname,
  685. void __iomem **base)
  686. {
  687. struct resource res;
  688. int ret;
  689. ret = miphy28lp_get_resource_byname(child, rname, &res);
  690. if (!ret) {
  691. *base = devm_ioremap(dev, res.start, resource_size(&res));
  692. if (!*base) {
  693. dev_err(dev, "failed to ioremap %s address region\n"
  694. , rname);
  695. return -ENOENT;
  696. }
  697. }
  698. return 0;
  699. }
  700. /* MiPHY reset and sysconf setup */
  701. static int miphy28lp_setup(struct miphy28lp_phy *miphy_phy, u32 miphy_val)
  702. {
  703. int err;
  704. struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
  705. if (!miphy_phy->syscfg_reg[SYSCFG_CTRL])
  706. return -EINVAL;
  707. err = reset_control_assert(miphy_phy->miphy_rst);
  708. if (err) {
  709. dev_err(miphy_dev->dev, "unable to bring out of miphy reset\n");
  710. return err;
  711. }
  712. if (miphy_phy->osc_force_ext)
  713. miphy_val |= MIPHY_OSC_FORCE_EXT;
  714. regmap_update_bits(miphy_dev->regmap,
  715. miphy_phy->syscfg_reg[SYSCFG_CTRL],
  716. MIPHY_CTRL_MASK, miphy_val);
  717. err = reset_control_deassert(miphy_phy->miphy_rst);
  718. if (err) {
  719. dev_err(miphy_dev->dev, "unable to bring out of miphy reset\n");
  720. return err;
  721. }
  722. return miphy_osc_is_ready(miphy_phy);
  723. }
  724. static int miphy28lp_init_sata(struct miphy28lp_phy *miphy_phy)
  725. {
  726. struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
  727. int err, sata_conf = SATA_CTRL_SELECT_SATA;
  728. if ((!miphy_phy->syscfg_reg[SYSCFG_SATA]) ||
  729. (!miphy_phy->syscfg_reg[SYSCFG_PCI]) ||
  730. (!miphy_phy->base))
  731. return -EINVAL;
  732. dev_info(miphy_dev->dev, "sata-up mode, addr 0x%p\n", miphy_phy->base);
  733. /* Configure the glue-logic */
  734. sata_conf |= ((miphy_phy->sata_gen - SATA_GEN1) << SATA_SPDMODE);
  735. regmap_update_bits(miphy_dev->regmap,
  736. miphy_phy->syscfg_reg[SYSCFG_SATA],
  737. SATA_CTRL_MASK, sata_conf);
  738. regmap_update_bits(miphy_dev->regmap, miphy_phy->syscfg_reg[SYSCFG_PCI],
  739. PCIE_CTRL_MASK, SATA_CTRL_SELECT_PCIE);
  740. /* MiPHY path and clocking init */
  741. err = miphy28lp_setup(miphy_phy, MIPHY_CTRL_DEFAULT);
  742. if (err) {
  743. dev_err(miphy_dev->dev, "SATA phy setup failed\n");
  744. return err;
  745. }
  746. /* initialize miphy */
  747. miphy28lp_configure_sata(miphy_phy);
  748. return miphy_is_ready(miphy_phy);
  749. }
  750. static int miphy28lp_init_pcie(struct miphy28lp_phy *miphy_phy)
  751. {
  752. struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
  753. int err;
  754. if ((!miphy_phy->syscfg_reg[SYSCFG_SATA]) ||
  755. (!miphy_phy->syscfg_reg[SYSCFG_PCI])
  756. || (!miphy_phy->base) || (!miphy_phy->pipebase))
  757. return -EINVAL;
  758. dev_info(miphy_dev->dev, "pcie-up mode, addr 0x%p\n", miphy_phy->base);
  759. /* Configure the glue-logic */
  760. regmap_update_bits(miphy_dev->regmap,
  761. miphy_phy->syscfg_reg[SYSCFG_SATA],
  762. SATA_CTRL_MASK, SATA_CTRL_SELECT_PCIE);
  763. regmap_update_bits(miphy_dev->regmap, miphy_phy->syscfg_reg[SYSCFG_PCI],
  764. PCIE_CTRL_MASK, SYSCFG_PCIE_PCIE_VAL);
  765. /* MiPHY path and clocking init */
  766. err = miphy28lp_setup(miphy_phy, MIPHY_CTRL_DEFAULT);
  767. if (err) {
  768. dev_err(miphy_dev->dev, "PCIe phy setup failed\n");
  769. return err;
  770. }
  771. /* initialize miphy */
  772. err = miphy28lp_configure_pcie(miphy_phy);
  773. if (err)
  774. return err;
  775. /* PIPE Wrapper Configuration */
  776. writeb_relaxed(0x68, miphy_phy->pipebase + 0x104); /* Rise_0 */
  777. writeb_relaxed(0x61, miphy_phy->pipebase + 0x105); /* Rise_1 */
  778. writeb_relaxed(0x68, miphy_phy->pipebase + 0x108); /* Fall_0 */
  779. writeb_relaxed(0x61, miphy_phy->pipebase + 0x109); /* Fall-1 */
  780. writeb_relaxed(0x68, miphy_phy->pipebase + 0x10c); /* Threshold_0 */
  781. writeb_relaxed(0x60, miphy_phy->pipebase + 0x10d); /* Threshold_1 */
  782. /* Wait for phy_ready */
  783. return miphy_is_ready(miphy_phy);
  784. }
  785. static int miphy28lp_init_usb3(struct miphy28lp_phy *miphy_phy)
  786. {
  787. struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
  788. int err;
  789. if ((!miphy_phy->base) || (!miphy_phy->pipebase))
  790. return -EINVAL;
  791. dev_info(miphy_dev->dev, "usb3-up mode, addr 0x%p\n", miphy_phy->base);
  792. /* MiPHY path and clocking init */
  793. err = miphy28lp_setup(miphy_phy, MIPHY_CTRL_SYNC_D_EN);
  794. if (err) {
  795. dev_err(miphy_dev->dev, "USB3 phy setup failed\n");
  796. return err;
  797. }
  798. /* initialize miphy */
  799. miphy28lp_configure_usb3(miphy_phy);
  800. /* PIPE Wrapper Configuration */
  801. writeb_relaxed(0x68, miphy_phy->pipebase + 0x23);
  802. writeb_relaxed(0x61, miphy_phy->pipebase + 0x24);
  803. writeb_relaxed(0x68, miphy_phy->pipebase + 0x26);
  804. writeb_relaxed(0x61, miphy_phy->pipebase + 0x27);
  805. writeb_relaxed(0x18, miphy_phy->pipebase + 0x29);
  806. writeb_relaxed(0x61, miphy_phy->pipebase + 0x2a);
  807. /* pipe Wrapper usb3 TX swing de-emph margin PREEMPH[7:4], SWING[3:0] */
  808. writeb_relaxed(0X67, miphy_phy->pipebase + 0x68);
  809. writeb_relaxed(0x0d, miphy_phy->pipebase + 0x69);
  810. writeb_relaxed(0X67, miphy_phy->pipebase + 0x6a);
  811. writeb_relaxed(0X0d, miphy_phy->pipebase + 0x6b);
  812. writeb_relaxed(0X67, miphy_phy->pipebase + 0x6c);
  813. writeb_relaxed(0X0d, miphy_phy->pipebase + 0x6d);
  814. writeb_relaxed(0X67, miphy_phy->pipebase + 0x6e);
  815. writeb_relaxed(0X0d, miphy_phy->pipebase + 0x6f);
  816. return miphy_is_ready(miphy_phy);
  817. }
  818. static int miphy28lp_init(struct phy *phy)
  819. {
  820. struct miphy28lp_phy *miphy_phy = phy_get_drvdata(phy);
  821. struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
  822. int ret;
  823. mutex_lock(&miphy_dev->miphy_mutex);
  824. switch (miphy_phy->type) {
  825. case PHY_TYPE_SATA:
  826. ret = miphy28lp_init_sata(miphy_phy);
  827. break;
  828. case PHY_TYPE_PCIE:
  829. ret = miphy28lp_init_pcie(miphy_phy);
  830. break;
  831. case PHY_TYPE_USB3:
  832. ret = miphy28lp_init_usb3(miphy_phy);
  833. break;
  834. default:
  835. ret = -EINVAL;
  836. break;
  837. }
  838. mutex_unlock(&miphy_dev->miphy_mutex);
  839. return ret;
  840. }
  841. static int miphy28lp_get_addr(struct miphy28lp_phy *miphy_phy)
  842. {
  843. struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
  844. struct device_node *phynode = miphy_phy->phy->dev.of_node;
  845. int err;
  846. if ((miphy_phy->type != PHY_TYPE_SATA) &&
  847. (miphy_phy->type != PHY_TYPE_PCIE) &&
  848. (miphy_phy->type != PHY_TYPE_USB3)) {
  849. return -EINVAL;
  850. }
  851. err = miphy28lp_get_one_addr(miphy_dev->dev, phynode,
  852. PHY_TYPE_name[miphy_phy->type - PHY_TYPE_SATA],
  853. &miphy_phy->base);
  854. if (err)
  855. return err;
  856. if ((miphy_phy->type == PHY_TYPE_PCIE) ||
  857. (miphy_phy->type == PHY_TYPE_USB3)) {
  858. err = miphy28lp_get_one_addr(miphy_dev->dev, phynode, "pipew",
  859. &miphy_phy->pipebase);
  860. if (err)
  861. return err;
  862. }
  863. return 0;
  864. }
  865. static struct phy *miphy28lp_xlate(struct device *dev,
  866. const struct of_phandle_args *args)
  867. {
  868. struct miphy28lp_dev *miphy_dev = dev_get_drvdata(dev);
  869. struct miphy28lp_phy *miphy_phy = NULL;
  870. struct device_node *phynode = args->np;
  871. int ret, index = 0;
  872. if (args->args_count != 1) {
  873. dev_err(dev, "Invalid number of cells in 'phy' property\n");
  874. return ERR_PTR(-EINVAL);
  875. }
  876. for (index = 0; index < miphy_dev->nphys; index++)
  877. if (phynode == miphy_dev->phys[index]->phy->dev.of_node) {
  878. miphy_phy = miphy_dev->phys[index];
  879. break;
  880. }
  881. if (!miphy_phy) {
  882. dev_err(dev, "Failed to find appropriate phy\n");
  883. return ERR_PTR(-EINVAL);
  884. }
  885. miphy_phy->type = args->args[0];
  886. ret = miphy28lp_get_addr(miphy_phy);
  887. if (ret < 0)
  888. return ERR_PTR(ret);
  889. return miphy_phy->phy;
  890. }
  891. static const struct phy_ops miphy28lp_ops = {
  892. .init = miphy28lp_init,
  893. .owner = THIS_MODULE,
  894. };
  895. static int miphy28lp_probe_resets(struct device_node *node,
  896. struct miphy28lp_phy *miphy_phy)
  897. {
  898. struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
  899. int err;
  900. miphy_phy->miphy_rst =
  901. of_reset_control_get_shared(node, "miphy-sw-rst");
  902. if (IS_ERR(miphy_phy->miphy_rst)) {
  903. dev_err(miphy_dev->dev,
  904. "miphy soft reset control not defined\n");
  905. return PTR_ERR(miphy_phy->miphy_rst);
  906. }
  907. err = reset_control_deassert(miphy_phy->miphy_rst);
  908. if (err) {
  909. dev_err(miphy_dev->dev, "unable to bring out of miphy reset\n");
  910. return err;
  911. }
  912. return 0;
  913. }
  914. static int miphy28lp_of_probe(struct device_node *np,
  915. struct miphy28lp_phy *miphy_phy)
  916. {
  917. int i;
  918. u32 ctrlreg;
  919. miphy_phy->osc_force_ext =
  920. of_property_read_bool(np, "st,osc-force-ext");
  921. miphy_phy->osc_rdy = of_property_read_bool(np, "st,osc-rdy");
  922. miphy_phy->px_rx_pol_inv =
  923. of_property_read_bool(np, "st,px_rx_pol_inv");
  924. miphy_phy->ssc = of_property_read_bool(np, "st,ssc-on");
  925. miphy_phy->tx_impedance =
  926. of_property_read_bool(np, "st,tx-impedance-comp");
  927. of_property_read_u32(np, "st,sata-gen", &miphy_phy->sata_gen);
  928. if (!miphy_phy->sata_gen)
  929. miphy_phy->sata_gen = SATA_GEN1;
  930. for (i = 0; i < SYSCFG_REG_MAX; i++) {
  931. if (!of_property_read_u32_index(np, "st,syscfg", i, &ctrlreg))
  932. miphy_phy->syscfg_reg[i] = ctrlreg;
  933. }
  934. return 0;
  935. }
  936. static int miphy28lp_probe(struct platform_device *pdev)
  937. {
  938. struct device_node *child, *np = pdev->dev.of_node;
  939. struct miphy28lp_dev *miphy_dev;
  940. struct phy_provider *provider;
  941. struct phy *phy;
  942. int ret, port = 0;
  943. miphy_dev = devm_kzalloc(&pdev->dev, sizeof(*miphy_dev), GFP_KERNEL);
  944. if (!miphy_dev)
  945. return -ENOMEM;
  946. miphy_dev->nphys = of_get_child_count(np);
  947. miphy_dev->phys = devm_kcalloc(&pdev->dev, miphy_dev->nphys,
  948. sizeof(*miphy_dev->phys), GFP_KERNEL);
  949. if (!miphy_dev->phys)
  950. return -ENOMEM;
  951. miphy_dev->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  952. if (IS_ERR(miphy_dev->regmap)) {
  953. dev_err(miphy_dev->dev, "No syscfg phandle specified\n");
  954. return PTR_ERR(miphy_dev->regmap);
  955. }
  956. miphy_dev->dev = &pdev->dev;
  957. dev_set_drvdata(&pdev->dev, miphy_dev);
  958. mutex_init(&miphy_dev->miphy_mutex);
  959. for_each_child_of_node(np, child) {
  960. struct miphy28lp_phy *miphy_phy;
  961. miphy_phy = devm_kzalloc(&pdev->dev, sizeof(*miphy_phy),
  962. GFP_KERNEL);
  963. if (!miphy_phy) {
  964. ret = -ENOMEM;
  965. goto put_child;
  966. }
  967. miphy_dev->phys[port] = miphy_phy;
  968. phy = devm_phy_create(&pdev->dev, child, &miphy28lp_ops);
  969. if (IS_ERR(phy)) {
  970. dev_err(&pdev->dev, "failed to create PHY\n");
  971. ret = PTR_ERR(phy);
  972. goto put_child;
  973. }
  974. miphy_dev->phys[port]->phy = phy;
  975. miphy_dev->phys[port]->phydev = miphy_dev;
  976. ret = miphy28lp_of_probe(child, miphy_phy);
  977. if (ret)
  978. goto put_child;
  979. ret = miphy28lp_probe_resets(child, miphy_dev->phys[port]);
  980. if (ret)
  981. goto put_child;
  982. phy_set_drvdata(phy, miphy_dev->phys[port]);
  983. port++;
  984. }
  985. provider = devm_of_phy_provider_register(&pdev->dev, miphy28lp_xlate);
  986. return PTR_ERR_OR_ZERO(provider);
  987. put_child:
  988. of_node_put(child);
  989. return ret;
  990. }
  991. static const struct of_device_id miphy28lp_of_match[] = {
  992. {.compatible = "st,miphy28lp-phy", },
  993. {},
  994. };
  995. MODULE_DEVICE_TABLE(of, miphy28lp_of_match);
  996. static struct platform_driver miphy28lp_driver = {
  997. .probe = miphy28lp_probe,
  998. .driver = {
  999. .name = "miphy28lp-phy",
  1000. .of_match_table = miphy28lp_of_match,
  1001. }
  1002. };
  1003. module_platform_driver(miphy28lp_driver);
  1004. MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
  1005. MODULE_DESCRIPTION("STMicroelectronics miphy28lp driver");
  1006. MODULE_LICENSE("GPL v2");