phy-miphy28lp.c 33 KB

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