phy-miphy28lp.c 33 KB

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