phy-miphy28lp.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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 =
  926. of_reset_control_get_shared(node, "miphy-sw-rst");
  927. if (IS_ERR(miphy_phy->miphy_rst)) {
  928. dev_err(miphy_dev->dev,
  929. "miphy soft reset control not defined\n");
  930. return PTR_ERR(miphy_phy->miphy_rst);
  931. }
  932. err = reset_control_deassert(miphy_phy->miphy_rst);
  933. if (err) {
  934. dev_err(miphy_dev->dev, "unable to bring out of miphy reset\n");
  935. return err;
  936. }
  937. return 0;
  938. }
  939. static int miphy28lp_of_probe(struct device_node *np,
  940. struct miphy28lp_phy *miphy_phy)
  941. {
  942. int i;
  943. u32 ctrlreg;
  944. miphy_phy->osc_force_ext =
  945. of_property_read_bool(np, "st,osc-force-ext");
  946. miphy_phy->osc_rdy = of_property_read_bool(np, "st,osc-rdy");
  947. miphy_phy->px_rx_pol_inv =
  948. of_property_read_bool(np, "st,px_rx_pol_inv");
  949. miphy_phy->ssc = of_property_read_bool(np, "st,ssc-on");
  950. miphy_phy->tx_impedance =
  951. of_property_read_bool(np, "st,tx-impedance-comp");
  952. of_property_read_u32(np, "st,sata-gen", &miphy_phy->sata_gen);
  953. if (!miphy_phy->sata_gen)
  954. miphy_phy->sata_gen = SATA_GEN1;
  955. for (i = 0; i < SYSCFG_REG_MAX; i++) {
  956. if (!of_property_read_u32_index(np, "st,syscfg", i, &ctrlreg))
  957. miphy_phy->syscfg_reg[i] = ctrlreg;
  958. }
  959. return 0;
  960. }
  961. static int miphy28lp_probe(struct platform_device *pdev)
  962. {
  963. struct device_node *child, *np = pdev->dev.of_node;
  964. struct miphy28lp_dev *miphy_dev;
  965. struct phy_provider *provider;
  966. struct phy *phy;
  967. int ret, port = 0;
  968. miphy_dev = devm_kzalloc(&pdev->dev, sizeof(*miphy_dev), GFP_KERNEL);
  969. if (!miphy_dev)
  970. return -ENOMEM;
  971. miphy_dev->nphys = of_get_child_count(np);
  972. miphy_dev->phys = devm_kcalloc(&pdev->dev, miphy_dev->nphys,
  973. sizeof(*miphy_dev->phys), GFP_KERNEL);
  974. if (!miphy_dev->phys)
  975. return -ENOMEM;
  976. miphy_dev->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  977. if (IS_ERR(miphy_dev->regmap)) {
  978. dev_err(miphy_dev->dev, "No syscfg phandle specified\n");
  979. return PTR_ERR(miphy_dev->regmap);
  980. }
  981. miphy_dev->dev = &pdev->dev;
  982. dev_set_drvdata(&pdev->dev, miphy_dev);
  983. mutex_init(&miphy_dev->miphy_mutex);
  984. for_each_child_of_node(np, child) {
  985. struct miphy28lp_phy *miphy_phy;
  986. miphy_phy = devm_kzalloc(&pdev->dev, sizeof(*miphy_phy),
  987. GFP_KERNEL);
  988. if (!miphy_phy) {
  989. ret = -ENOMEM;
  990. goto put_child;
  991. }
  992. miphy_dev->phys[port] = miphy_phy;
  993. phy = devm_phy_create(&pdev->dev, child, &miphy28lp_ops);
  994. if (IS_ERR(phy)) {
  995. dev_err(&pdev->dev, "failed to create PHY\n");
  996. ret = PTR_ERR(phy);
  997. goto put_child;
  998. }
  999. miphy_dev->phys[port]->phy = phy;
  1000. miphy_dev->phys[port]->phydev = miphy_dev;
  1001. ret = miphy28lp_of_probe(child, miphy_phy);
  1002. if (ret)
  1003. goto put_child;
  1004. ret = miphy28lp_probe_resets(child, miphy_dev->phys[port]);
  1005. if (ret)
  1006. goto put_child;
  1007. phy_set_drvdata(phy, miphy_dev->phys[port]);
  1008. port++;
  1009. }
  1010. provider = devm_of_phy_provider_register(&pdev->dev, miphy28lp_xlate);
  1011. return PTR_ERR_OR_ZERO(provider);
  1012. put_child:
  1013. of_node_put(child);
  1014. return ret;
  1015. }
  1016. static const struct of_device_id miphy28lp_of_match[] = {
  1017. {.compatible = "st,miphy28lp-phy", },
  1018. {},
  1019. };
  1020. MODULE_DEVICE_TABLE(of, miphy28lp_of_match);
  1021. static struct platform_driver miphy28lp_driver = {
  1022. .probe = miphy28lp_probe,
  1023. .driver = {
  1024. .name = "miphy28lp-phy",
  1025. .of_match_table = miphy28lp_of_match,
  1026. }
  1027. };
  1028. module_platform_driver(miphy28lp_driver);
  1029. MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
  1030. MODULE_DESCRIPTION("STMicroelectronics miphy28lp driver");
  1031. MODULE_LICENSE("GPL v2");