phy-miphy28lp.c 33 KB

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