phy-exynos5-usbdrd.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Samsung Exynos5 SoC series USB DRD PHY driver
  4. *
  5. * Phy provider for USB 3.0 DRD controller on Exynos5 SoC series
  6. *
  7. * Copyright (C) 2014 Samsung Electronics Co., Ltd.
  8. * Author: Vivek Gautam <[email protected]>
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_device.h>
  18. #include <linux/iopoll.h>
  19. #include <linux/phy/phy.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/mutex.h>
  22. #include <linux/mfd/syscon.h>
  23. #include <linux/regmap.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/soc/samsung/exynos-regs-pmu.h>
  26. /* Exynos USB PHY registers */
  27. #define EXYNOS5_FSEL_9MHZ6 0x0
  28. #define EXYNOS5_FSEL_10MHZ 0x1
  29. #define EXYNOS5_FSEL_12MHZ 0x2
  30. #define EXYNOS5_FSEL_19MHZ2 0x3
  31. #define EXYNOS5_FSEL_20MHZ 0x4
  32. #define EXYNOS5_FSEL_24MHZ 0x5
  33. #define EXYNOS5_FSEL_50MHZ 0x7
  34. /* Exynos5: USB 3.0 DRD PHY registers */
  35. #define EXYNOS5_DRD_LINKSYSTEM 0x04
  36. #define LINKSYSTEM_FLADJ_MASK (0x3f << 1)
  37. #define LINKSYSTEM_FLADJ(_x) ((_x) << 1)
  38. #define LINKSYSTEM_XHCI_VERSION_CONTROL BIT(27)
  39. #define EXYNOS5_DRD_PHYUTMI 0x08
  40. #define PHYUTMI_OTGDISABLE BIT(6)
  41. #define PHYUTMI_FORCESUSPEND BIT(1)
  42. #define PHYUTMI_FORCESLEEP BIT(0)
  43. #define EXYNOS5_DRD_PHYPIPE 0x0c
  44. #define EXYNOS5_DRD_PHYCLKRST 0x10
  45. #define PHYCLKRST_EN_UTMISUSPEND BIT(31)
  46. #define PHYCLKRST_SSC_REFCLKSEL_MASK (0xff << 23)
  47. #define PHYCLKRST_SSC_REFCLKSEL(_x) ((_x) << 23)
  48. #define PHYCLKRST_SSC_RANGE_MASK (0x03 << 21)
  49. #define PHYCLKRST_SSC_RANGE(_x) ((_x) << 21)
  50. #define PHYCLKRST_SSC_EN BIT(20)
  51. #define PHYCLKRST_REF_SSP_EN BIT(19)
  52. #define PHYCLKRST_REF_CLKDIV2 BIT(18)
  53. #define PHYCLKRST_MPLL_MULTIPLIER_MASK (0x7f << 11)
  54. #define PHYCLKRST_MPLL_MULTIPLIER_100MHZ_REF (0x19 << 11)
  55. #define PHYCLKRST_MPLL_MULTIPLIER_50M_REF (0x32 << 11)
  56. #define PHYCLKRST_MPLL_MULTIPLIER_24MHZ_REF (0x68 << 11)
  57. #define PHYCLKRST_MPLL_MULTIPLIER_20MHZ_REF (0x7d << 11)
  58. #define PHYCLKRST_MPLL_MULTIPLIER_19200KHZ_REF (0x02 << 11)
  59. #define PHYCLKRST_FSEL_UTMI_MASK (0x7 << 5)
  60. #define PHYCLKRST_FSEL_PIPE_MASK (0x7 << 8)
  61. #define PHYCLKRST_FSEL(_x) ((_x) << 5)
  62. #define PHYCLKRST_FSEL_PAD_100MHZ (0x27 << 5)
  63. #define PHYCLKRST_FSEL_PAD_24MHZ (0x2a << 5)
  64. #define PHYCLKRST_FSEL_PAD_20MHZ (0x31 << 5)
  65. #define PHYCLKRST_FSEL_PAD_19_2MHZ (0x38 << 5)
  66. #define PHYCLKRST_RETENABLEN BIT(4)
  67. #define PHYCLKRST_REFCLKSEL_MASK (0x03 << 2)
  68. #define PHYCLKRST_REFCLKSEL_PAD_REFCLK (0x2 << 2)
  69. #define PHYCLKRST_REFCLKSEL_EXT_REFCLK (0x3 << 2)
  70. #define PHYCLKRST_PORTRESET BIT(1)
  71. #define PHYCLKRST_COMMONONN BIT(0)
  72. #define EXYNOS5_DRD_PHYREG0 0x14
  73. #define PHYREG0_SSC_REF_CLK_SEL BIT(21)
  74. #define PHYREG0_SSC_RANGE BIT(20)
  75. #define PHYREG0_CR_WRITE BIT(19)
  76. #define PHYREG0_CR_READ BIT(18)
  77. #define PHYREG0_CR_DATA_IN(_x) ((_x) << 2)
  78. #define PHYREG0_CR_CAP_DATA BIT(1)
  79. #define PHYREG0_CR_CAP_ADDR BIT(0)
  80. #define EXYNOS5_DRD_PHYREG1 0x18
  81. #define PHYREG1_CR_DATA_OUT(_x) ((_x) << 1)
  82. #define PHYREG1_CR_ACK BIT(0)
  83. #define EXYNOS5_DRD_PHYPARAM0 0x1c
  84. #define PHYPARAM0_REF_USE_PAD BIT(31)
  85. #define PHYPARAM0_REF_LOSLEVEL_MASK (0x1f << 26)
  86. #define PHYPARAM0_REF_LOSLEVEL (0x9 << 26)
  87. #define EXYNOS5_DRD_PHYPARAM1 0x20
  88. #define PHYPARAM1_PCS_TXDEEMPH_MASK (0x1f << 0)
  89. #define PHYPARAM1_PCS_TXDEEMPH (0x1c)
  90. #define EXYNOS5_DRD_PHYTERM 0x24
  91. #define EXYNOS5_DRD_PHYTEST 0x28
  92. #define PHYTEST_POWERDOWN_SSP BIT(3)
  93. #define PHYTEST_POWERDOWN_HSP BIT(2)
  94. #define EXYNOS5_DRD_PHYADP 0x2c
  95. #define EXYNOS5_DRD_PHYUTMICLKSEL 0x30
  96. #define PHYUTMICLKSEL_UTMI_CLKSEL BIT(2)
  97. #define EXYNOS5_DRD_PHYRESUME 0x34
  98. #define EXYNOS5_DRD_LINKPORT 0x44
  99. /* USB 3.0 DRD PHY SS Function Control Reg; accessed by CR_PORT */
  100. #define EXYNOS5_DRD_PHYSS_LOSLEVEL_OVRD_IN (0x15)
  101. #define LOSLEVEL_OVRD_IN_LOS_BIAS_5420 (0x5 << 13)
  102. #define LOSLEVEL_OVRD_IN_LOS_BIAS_DEFAULT (0x0 << 13)
  103. #define LOSLEVEL_OVRD_IN_EN (0x1 << 10)
  104. #define LOSLEVEL_OVRD_IN_LOS_LEVEL_DEFAULT (0x9 << 0)
  105. #define EXYNOS5_DRD_PHYSS_TX_VBOOSTLEVEL_OVRD_IN (0x12)
  106. #define TX_VBOOSTLEVEL_OVRD_IN_VBOOST_5420 (0x5 << 13)
  107. #define TX_VBOOSTLEVEL_OVRD_IN_VBOOST_DEFAULT (0x4 << 13)
  108. #define EXYNOS5_DRD_PHYSS_LANE0_TX_DEBUG (0x1010)
  109. #define LANE0_TX_DEBUG_RXDET_MEAS_TIME_19M2_20M (0x4 << 4)
  110. #define LANE0_TX_DEBUG_RXDET_MEAS_TIME_24M (0x8 << 4)
  111. #define LANE0_TX_DEBUG_RXDET_MEAS_TIME_25M_26M (0x8 << 4)
  112. #define LANE0_TX_DEBUG_RXDET_MEAS_TIME_48M_50M_52M (0x20 << 4)
  113. #define LANE0_TX_DEBUG_RXDET_MEAS_TIME_62M5 (0x20 << 4)
  114. #define LANE0_TX_DEBUG_RXDET_MEAS_TIME_96M_100M (0x40 << 4)
  115. #define KHZ 1000
  116. #define MHZ (KHZ * KHZ)
  117. enum exynos5_usbdrd_phy_id {
  118. EXYNOS5_DRDPHY_UTMI,
  119. EXYNOS5_DRDPHY_PIPE3,
  120. EXYNOS5_DRDPHYS_NUM,
  121. };
  122. struct phy_usb_instance;
  123. struct exynos5_usbdrd_phy;
  124. struct exynos5_usbdrd_phy_config {
  125. u32 id;
  126. void (*phy_isol)(struct phy_usb_instance *inst, u32 on);
  127. void (*phy_init)(struct exynos5_usbdrd_phy *phy_drd);
  128. unsigned int (*set_refclk)(struct phy_usb_instance *inst);
  129. };
  130. struct exynos5_usbdrd_phy_drvdata {
  131. const struct exynos5_usbdrd_phy_config *phy_cfg;
  132. u32 pmu_offset_usbdrd0_phy;
  133. u32 pmu_offset_usbdrd1_phy;
  134. bool has_common_clk_gate;
  135. };
  136. /**
  137. * struct exynos5_usbdrd_phy - driver data for USB 3.0 PHY
  138. * @dev: pointer to device instance of this platform device
  139. * @reg_phy: usb phy controller register memory base
  140. * @clk: phy clock for register access
  141. * @pipeclk: clock for pipe3 phy
  142. * @utmiclk: clock for utmi+ phy
  143. * @itpclk: clock for ITP generation
  144. * @drv_data: pointer to SoC level driver data structure
  145. * @phys: array for 'EXYNOS5_DRDPHYS_NUM' number of PHY
  146. * instances each with its 'phy' and 'phy_cfg'.
  147. * @extrefclk: frequency select settings when using 'separate
  148. * reference clocks' for SS and HS operations
  149. * @ref_clk: reference clock to PHY block from which PHY's
  150. * operational clocks are derived
  151. * @vbus: VBUS regulator for phy
  152. * @vbus_boost: Boost regulator for VBUS present on few Exynos boards
  153. */
  154. struct exynos5_usbdrd_phy {
  155. struct device *dev;
  156. void __iomem *reg_phy;
  157. struct clk *clk;
  158. struct clk *pipeclk;
  159. struct clk *utmiclk;
  160. struct clk *itpclk;
  161. const struct exynos5_usbdrd_phy_drvdata *drv_data;
  162. struct phy_usb_instance {
  163. struct phy *phy;
  164. u32 index;
  165. struct regmap *reg_pmu;
  166. u32 pmu_offset;
  167. const struct exynos5_usbdrd_phy_config *phy_cfg;
  168. } phys[EXYNOS5_DRDPHYS_NUM];
  169. u32 extrefclk;
  170. struct clk *ref_clk;
  171. struct regulator *vbus;
  172. struct regulator *vbus_boost;
  173. };
  174. static inline
  175. struct exynos5_usbdrd_phy *to_usbdrd_phy(struct phy_usb_instance *inst)
  176. {
  177. return container_of((inst), struct exynos5_usbdrd_phy,
  178. phys[(inst)->index]);
  179. }
  180. /*
  181. * exynos5_rate_to_clk() converts the supplied clock rate to the value that
  182. * can be written to the phy register.
  183. */
  184. static unsigned int exynos5_rate_to_clk(unsigned long rate, u32 *reg)
  185. {
  186. /* EXYNOS5_FSEL_MASK */
  187. switch (rate) {
  188. case 9600 * KHZ:
  189. *reg = EXYNOS5_FSEL_9MHZ6;
  190. break;
  191. case 10 * MHZ:
  192. *reg = EXYNOS5_FSEL_10MHZ;
  193. break;
  194. case 12 * MHZ:
  195. *reg = EXYNOS5_FSEL_12MHZ;
  196. break;
  197. case 19200 * KHZ:
  198. *reg = EXYNOS5_FSEL_19MHZ2;
  199. break;
  200. case 20 * MHZ:
  201. *reg = EXYNOS5_FSEL_20MHZ;
  202. break;
  203. case 24 * MHZ:
  204. *reg = EXYNOS5_FSEL_24MHZ;
  205. break;
  206. case 50 * MHZ:
  207. *reg = EXYNOS5_FSEL_50MHZ;
  208. break;
  209. default:
  210. return -EINVAL;
  211. }
  212. return 0;
  213. }
  214. static void exynos5_usbdrd_phy_isol(struct phy_usb_instance *inst,
  215. unsigned int on)
  216. {
  217. unsigned int val;
  218. if (!inst->reg_pmu)
  219. return;
  220. val = on ? 0 : EXYNOS4_PHY_ENABLE;
  221. regmap_update_bits(inst->reg_pmu, inst->pmu_offset,
  222. EXYNOS4_PHY_ENABLE, val);
  223. }
  224. /*
  225. * Sets the pipe3 phy's clk as EXTREFCLK (XXTI) which is internal clock
  226. * from clock core. Further sets multiplier values and spread spectrum
  227. * clock settings for SuperSpeed operations.
  228. */
  229. static unsigned int
  230. exynos5_usbdrd_pipe3_set_refclk(struct phy_usb_instance *inst)
  231. {
  232. u32 reg;
  233. struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
  234. /* restore any previous reference clock settings */
  235. reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYCLKRST);
  236. /* Use EXTREFCLK as ref clock */
  237. reg &= ~PHYCLKRST_REFCLKSEL_MASK;
  238. reg |= PHYCLKRST_REFCLKSEL_EXT_REFCLK;
  239. /* FSEL settings corresponding to reference clock */
  240. reg &= ~PHYCLKRST_FSEL_PIPE_MASK |
  241. PHYCLKRST_MPLL_MULTIPLIER_MASK |
  242. PHYCLKRST_SSC_REFCLKSEL_MASK;
  243. switch (phy_drd->extrefclk) {
  244. case EXYNOS5_FSEL_50MHZ:
  245. reg |= (PHYCLKRST_MPLL_MULTIPLIER_50M_REF |
  246. PHYCLKRST_SSC_REFCLKSEL(0x00));
  247. break;
  248. case EXYNOS5_FSEL_24MHZ:
  249. reg |= (PHYCLKRST_MPLL_MULTIPLIER_24MHZ_REF |
  250. PHYCLKRST_SSC_REFCLKSEL(0x88));
  251. break;
  252. case EXYNOS5_FSEL_20MHZ:
  253. reg |= (PHYCLKRST_MPLL_MULTIPLIER_20MHZ_REF |
  254. PHYCLKRST_SSC_REFCLKSEL(0x00));
  255. break;
  256. case EXYNOS5_FSEL_19MHZ2:
  257. reg |= (PHYCLKRST_MPLL_MULTIPLIER_19200KHZ_REF |
  258. PHYCLKRST_SSC_REFCLKSEL(0x88));
  259. break;
  260. default:
  261. dev_dbg(phy_drd->dev, "unsupported ref clk\n");
  262. break;
  263. }
  264. return reg;
  265. }
  266. /*
  267. * Sets the utmi phy's clk as EXTREFCLK (XXTI) which is internal clock
  268. * from clock core. Further sets the FSEL values for HighSpeed operations.
  269. */
  270. static unsigned int
  271. exynos5_usbdrd_utmi_set_refclk(struct phy_usb_instance *inst)
  272. {
  273. u32 reg;
  274. struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
  275. /* restore any previous reference clock settings */
  276. reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYCLKRST);
  277. reg &= ~PHYCLKRST_REFCLKSEL_MASK;
  278. reg |= PHYCLKRST_REFCLKSEL_EXT_REFCLK;
  279. reg &= ~PHYCLKRST_FSEL_UTMI_MASK |
  280. PHYCLKRST_MPLL_MULTIPLIER_MASK |
  281. PHYCLKRST_SSC_REFCLKSEL_MASK;
  282. reg |= PHYCLKRST_FSEL(phy_drd->extrefclk);
  283. return reg;
  284. }
  285. static void exynos5_usbdrd_pipe3_init(struct exynos5_usbdrd_phy *phy_drd)
  286. {
  287. u32 reg;
  288. reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM1);
  289. /* Set Tx De-Emphasis level */
  290. reg &= ~PHYPARAM1_PCS_TXDEEMPH_MASK;
  291. reg |= PHYPARAM1_PCS_TXDEEMPH;
  292. writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM1);
  293. reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYTEST);
  294. reg &= ~PHYTEST_POWERDOWN_SSP;
  295. writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYTEST);
  296. }
  297. static void exynos5_usbdrd_utmi_init(struct exynos5_usbdrd_phy *phy_drd)
  298. {
  299. u32 reg;
  300. reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM0);
  301. /* Set Loss-of-Signal Detector sensitivity */
  302. reg &= ~PHYPARAM0_REF_LOSLEVEL_MASK;
  303. reg |= PHYPARAM0_REF_LOSLEVEL;
  304. writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM0);
  305. reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM1);
  306. /* Set Tx De-Emphasis level */
  307. reg &= ~PHYPARAM1_PCS_TXDEEMPH_MASK;
  308. reg |= PHYPARAM1_PCS_TXDEEMPH;
  309. writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM1);
  310. /* UTMI Power Control */
  311. writel(PHYUTMI_OTGDISABLE, phy_drd->reg_phy + EXYNOS5_DRD_PHYUTMI);
  312. reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYTEST);
  313. reg &= ~PHYTEST_POWERDOWN_HSP;
  314. writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYTEST);
  315. }
  316. static int exynos5_usbdrd_phy_init(struct phy *phy)
  317. {
  318. int ret;
  319. u32 reg;
  320. struct phy_usb_instance *inst = phy_get_drvdata(phy);
  321. struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
  322. ret = clk_prepare_enable(phy_drd->clk);
  323. if (ret)
  324. return ret;
  325. /* Reset USB 3.0 PHY */
  326. writel(0x0, phy_drd->reg_phy + EXYNOS5_DRD_PHYREG0);
  327. writel(0x0, phy_drd->reg_phy + EXYNOS5_DRD_PHYRESUME);
  328. /*
  329. * Setting the Frame length Adj value[6:1] to default 0x20
  330. * See xHCI 1.0 spec, 5.2.4
  331. */
  332. reg = LINKSYSTEM_XHCI_VERSION_CONTROL |
  333. LINKSYSTEM_FLADJ(0x20);
  334. writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_LINKSYSTEM);
  335. reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM0);
  336. /* Select PHY CLK source */
  337. reg &= ~PHYPARAM0_REF_USE_PAD;
  338. writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM0);
  339. /* This bit must be set for both HS and SS operations */
  340. reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYUTMICLKSEL);
  341. reg |= PHYUTMICLKSEL_UTMI_CLKSEL;
  342. writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYUTMICLKSEL);
  343. /* UTMI or PIPE3 specific init */
  344. inst->phy_cfg->phy_init(phy_drd);
  345. /* reference clock settings */
  346. reg = inst->phy_cfg->set_refclk(inst);
  347. /* Digital power supply in normal operating mode */
  348. reg |= PHYCLKRST_RETENABLEN |
  349. /* Enable ref clock for SS function */
  350. PHYCLKRST_REF_SSP_EN |
  351. /* Enable spread spectrum */
  352. PHYCLKRST_SSC_EN |
  353. /* Power down HS Bias and PLL blocks in suspend mode */
  354. PHYCLKRST_COMMONONN |
  355. /* Reset the port */
  356. PHYCLKRST_PORTRESET;
  357. writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYCLKRST);
  358. udelay(10);
  359. reg &= ~PHYCLKRST_PORTRESET;
  360. writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYCLKRST);
  361. clk_disable_unprepare(phy_drd->clk);
  362. return 0;
  363. }
  364. static int exynos5_usbdrd_phy_exit(struct phy *phy)
  365. {
  366. int ret;
  367. u32 reg;
  368. struct phy_usb_instance *inst = phy_get_drvdata(phy);
  369. struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
  370. ret = clk_prepare_enable(phy_drd->clk);
  371. if (ret)
  372. return ret;
  373. reg = PHYUTMI_OTGDISABLE |
  374. PHYUTMI_FORCESUSPEND |
  375. PHYUTMI_FORCESLEEP;
  376. writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYUTMI);
  377. /* Resetting the PHYCLKRST enable bits to reduce leakage current */
  378. reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYCLKRST);
  379. reg &= ~(PHYCLKRST_REF_SSP_EN |
  380. PHYCLKRST_SSC_EN |
  381. PHYCLKRST_COMMONONN);
  382. writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYCLKRST);
  383. /* Control PHYTEST to remove leakage current */
  384. reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYTEST);
  385. reg |= PHYTEST_POWERDOWN_SSP |
  386. PHYTEST_POWERDOWN_HSP;
  387. writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYTEST);
  388. clk_disable_unprepare(phy_drd->clk);
  389. return 0;
  390. }
  391. static int exynos5_usbdrd_phy_power_on(struct phy *phy)
  392. {
  393. int ret;
  394. struct phy_usb_instance *inst = phy_get_drvdata(phy);
  395. struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
  396. dev_dbg(phy_drd->dev, "Request to power_on usbdrd_phy phy\n");
  397. clk_prepare_enable(phy_drd->ref_clk);
  398. if (!phy_drd->drv_data->has_common_clk_gate) {
  399. clk_prepare_enable(phy_drd->pipeclk);
  400. clk_prepare_enable(phy_drd->utmiclk);
  401. clk_prepare_enable(phy_drd->itpclk);
  402. }
  403. /* Enable VBUS supply */
  404. if (phy_drd->vbus_boost) {
  405. ret = regulator_enable(phy_drd->vbus_boost);
  406. if (ret) {
  407. dev_err(phy_drd->dev,
  408. "Failed to enable VBUS boost supply\n");
  409. goto fail_vbus;
  410. }
  411. }
  412. if (phy_drd->vbus) {
  413. ret = regulator_enable(phy_drd->vbus);
  414. if (ret) {
  415. dev_err(phy_drd->dev, "Failed to enable VBUS supply\n");
  416. goto fail_vbus_boost;
  417. }
  418. }
  419. /* Power-on PHY*/
  420. inst->phy_cfg->phy_isol(inst, 0);
  421. return 0;
  422. fail_vbus_boost:
  423. if (phy_drd->vbus_boost)
  424. regulator_disable(phy_drd->vbus_boost);
  425. fail_vbus:
  426. clk_disable_unprepare(phy_drd->ref_clk);
  427. if (!phy_drd->drv_data->has_common_clk_gate) {
  428. clk_disable_unprepare(phy_drd->itpclk);
  429. clk_disable_unprepare(phy_drd->utmiclk);
  430. clk_disable_unprepare(phy_drd->pipeclk);
  431. }
  432. return ret;
  433. }
  434. static int exynos5_usbdrd_phy_power_off(struct phy *phy)
  435. {
  436. struct phy_usb_instance *inst = phy_get_drvdata(phy);
  437. struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
  438. dev_dbg(phy_drd->dev, "Request to power_off usbdrd_phy phy\n");
  439. /* Power-off the PHY */
  440. inst->phy_cfg->phy_isol(inst, 1);
  441. /* Disable VBUS supply */
  442. if (phy_drd->vbus)
  443. regulator_disable(phy_drd->vbus);
  444. if (phy_drd->vbus_boost)
  445. regulator_disable(phy_drd->vbus_boost);
  446. clk_disable_unprepare(phy_drd->ref_clk);
  447. if (!phy_drd->drv_data->has_common_clk_gate) {
  448. clk_disable_unprepare(phy_drd->itpclk);
  449. clk_disable_unprepare(phy_drd->pipeclk);
  450. clk_disable_unprepare(phy_drd->utmiclk);
  451. }
  452. return 0;
  453. }
  454. static int crport_handshake(struct exynos5_usbdrd_phy *phy_drd,
  455. u32 val, u32 cmd)
  456. {
  457. unsigned int result;
  458. int err;
  459. writel(val | cmd, phy_drd->reg_phy + EXYNOS5_DRD_PHYREG0);
  460. err = readl_poll_timeout(phy_drd->reg_phy + EXYNOS5_DRD_PHYREG1,
  461. result, (result & PHYREG1_CR_ACK), 1, 100);
  462. if (err == -ETIMEDOUT) {
  463. dev_err(phy_drd->dev, "CRPORT handshake timeout1 (0x%08x)\n", val);
  464. return err;
  465. }
  466. writel(val, phy_drd->reg_phy + EXYNOS5_DRD_PHYREG0);
  467. err = readl_poll_timeout(phy_drd->reg_phy + EXYNOS5_DRD_PHYREG1,
  468. result, !(result & PHYREG1_CR_ACK), 1, 100);
  469. if (err == -ETIMEDOUT) {
  470. dev_err(phy_drd->dev, "CRPORT handshake timeout2 (0x%08x)\n", val);
  471. return err;
  472. }
  473. return 0;
  474. }
  475. static int crport_ctrl_write(struct exynos5_usbdrd_phy *phy_drd,
  476. u32 addr, u32 data)
  477. {
  478. int ret;
  479. /* Write Address */
  480. writel(PHYREG0_CR_DATA_IN(addr),
  481. phy_drd->reg_phy + EXYNOS5_DRD_PHYREG0);
  482. ret = crport_handshake(phy_drd, PHYREG0_CR_DATA_IN(addr),
  483. PHYREG0_CR_CAP_ADDR);
  484. if (ret)
  485. return ret;
  486. /* Write Data */
  487. writel(PHYREG0_CR_DATA_IN(data),
  488. phy_drd->reg_phy + EXYNOS5_DRD_PHYREG0);
  489. ret = crport_handshake(phy_drd, PHYREG0_CR_DATA_IN(data),
  490. PHYREG0_CR_CAP_DATA);
  491. if (ret)
  492. return ret;
  493. ret = crport_handshake(phy_drd, PHYREG0_CR_DATA_IN(data),
  494. PHYREG0_CR_WRITE);
  495. return ret;
  496. }
  497. /*
  498. * Calibrate few PHY parameters using CR_PORT register to meet
  499. * SuperSpeed requirements on Exynos5420 and Exynos5800 systems,
  500. * which have 28nm USB 3.0 DRD PHY.
  501. */
  502. static int exynos5420_usbdrd_phy_calibrate(struct exynos5_usbdrd_phy *phy_drd)
  503. {
  504. unsigned int temp;
  505. int ret = 0;
  506. /*
  507. * Change los_bias to (0x5) for 28nm PHY from a
  508. * default value (0x0); los_level is set as default
  509. * (0x9) as also reflected in los_level[30:26] bits
  510. * of PHYPARAM0 register.
  511. */
  512. temp = LOSLEVEL_OVRD_IN_LOS_BIAS_5420 |
  513. LOSLEVEL_OVRD_IN_EN |
  514. LOSLEVEL_OVRD_IN_LOS_LEVEL_DEFAULT;
  515. ret = crport_ctrl_write(phy_drd,
  516. EXYNOS5_DRD_PHYSS_LOSLEVEL_OVRD_IN,
  517. temp);
  518. if (ret) {
  519. dev_err(phy_drd->dev,
  520. "Failed setting Loss-of-Signal level for SuperSpeed\n");
  521. return ret;
  522. }
  523. /*
  524. * Set tx_vboost_lvl to (0x5) for 28nm PHY Tuning,
  525. * to raise Tx signal level from its default value of (0x4)
  526. */
  527. temp = TX_VBOOSTLEVEL_OVRD_IN_VBOOST_5420;
  528. ret = crport_ctrl_write(phy_drd,
  529. EXYNOS5_DRD_PHYSS_TX_VBOOSTLEVEL_OVRD_IN,
  530. temp);
  531. if (ret) {
  532. dev_err(phy_drd->dev,
  533. "Failed setting Tx-Vboost-Level for SuperSpeed\n");
  534. return ret;
  535. }
  536. /*
  537. * Set proper time to wait for RxDetect measurement, for
  538. * desired reference clock of PHY, by tuning the CR_PORT
  539. * register LANE0.TX_DEBUG which is internal to PHY.
  540. * This fixes issue with few USB 3.0 devices, which are
  541. * not detected (not even generate interrupts on the bus
  542. * on insertion) without this change.
  543. * e.g. Samsung SUM-TSB16S 3.0 USB drive.
  544. */
  545. switch (phy_drd->extrefclk) {
  546. case EXYNOS5_FSEL_50MHZ:
  547. temp = LANE0_TX_DEBUG_RXDET_MEAS_TIME_48M_50M_52M;
  548. break;
  549. case EXYNOS5_FSEL_20MHZ:
  550. case EXYNOS5_FSEL_19MHZ2:
  551. temp = LANE0_TX_DEBUG_RXDET_MEAS_TIME_19M2_20M;
  552. break;
  553. case EXYNOS5_FSEL_24MHZ:
  554. default:
  555. temp = LANE0_TX_DEBUG_RXDET_MEAS_TIME_24M;
  556. break;
  557. }
  558. ret = crport_ctrl_write(phy_drd,
  559. EXYNOS5_DRD_PHYSS_LANE0_TX_DEBUG,
  560. temp);
  561. if (ret)
  562. dev_err(phy_drd->dev,
  563. "Fail to set RxDet measurement time for SuperSpeed\n");
  564. return ret;
  565. }
  566. static struct phy *exynos5_usbdrd_phy_xlate(struct device *dev,
  567. struct of_phandle_args *args)
  568. {
  569. struct exynos5_usbdrd_phy *phy_drd = dev_get_drvdata(dev);
  570. if (WARN_ON(args->args[0] >= EXYNOS5_DRDPHYS_NUM))
  571. return ERR_PTR(-ENODEV);
  572. return phy_drd->phys[args->args[0]].phy;
  573. }
  574. static int exynos5_usbdrd_phy_calibrate(struct phy *phy)
  575. {
  576. struct phy_usb_instance *inst = phy_get_drvdata(phy);
  577. struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
  578. if (inst->phy_cfg->id == EXYNOS5_DRDPHY_UTMI)
  579. return exynos5420_usbdrd_phy_calibrate(phy_drd);
  580. return 0;
  581. }
  582. static const struct phy_ops exynos5_usbdrd_phy_ops = {
  583. .init = exynos5_usbdrd_phy_init,
  584. .exit = exynos5_usbdrd_phy_exit,
  585. .power_on = exynos5_usbdrd_phy_power_on,
  586. .power_off = exynos5_usbdrd_phy_power_off,
  587. .calibrate = exynos5_usbdrd_phy_calibrate,
  588. .owner = THIS_MODULE,
  589. };
  590. static int exynos5_usbdrd_phy_clk_handle(struct exynos5_usbdrd_phy *phy_drd)
  591. {
  592. unsigned long ref_rate;
  593. int ret;
  594. phy_drd->clk = devm_clk_get(phy_drd->dev, "phy");
  595. if (IS_ERR(phy_drd->clk)) {
  596. dev_err(phy_drd->dev, "Failed to get phy clock\n");
  597. return PTR_ERR(phy_drd->clk);
  598. }
  599. phy_drd->ref_clk = devm_clk_get(phy_drd->dev, "ref");
  600. if (IS_ERR(phy_drd->ref_clk)) {
  601. dev_err(phy_drd->dev, "Failed to get phy reference clock\n");
  602. return PTR_ERR(phy_drd->ref_clk);
  603. }
  604. ref_rate = clk_get_rate(phy_drd->ref_clk);
  605. ret = exynos5_rate_to_clk(ref_rate, &phy_drd->extrefclk);
  606. if (ret) {
  607. dev_err(phy_drd->dev, "Clock rate (%ld) not supported\n",
  608. ref_rate);
  609. return ret;
  610. }
  611. if (!phy_drd->drv_data->has_common_clk_gate) {
  612. phy_drd->pipeclk = devm_clk_get(phy_drd->dev, "phy_pipe");
  613. if (IS_ERR(phy_drd->pipeclk)) {
  614. dev_info(phy_drd->dev,
  615. "PIPE3 phy operational clock not specified\n");
  616. phy_drd->pipeclk = NULL;
  617. }
  618. phy_drd->utmiclk = devm_clk_get(phy_drd->dev, "phy_utmi");
  619. if (IS_ERR(phy_drd->utmiclk)) {
  620. dev_info(phy_drd->dev,
  621. "UTMI phy operational clock not specified\n");
  622. phy_drd->utmiclk = NULL;
  623. }
  624. phy_drd->itpclk = devm_clk_get(phy_drd->dev, "itp");
  625. if (IS_ERR(phy_drd->itpclk)) {
  626. dev_info(phy_drd->dev,
  627. "ITP clock from main OSC not specified\n");
  628. phy_drd->itpclk = NULL;
  629. }
  630. }
  631. return 0;
  632. }
  633. static const struct exynos5_usbdrd_phy_config phy_cfg_exynos5[] = {
  634. {
  635. .id = EXYNOS5_DRDPHY_UTMI,
  636. .phy_isol = exynos5_usbdrd_phy_isol,
  637. .phy_init = exynos5_usbdrd_utmi_init,
  638. .set_refclk = exynos5_usbdrd_utmi_set_refclk,
  639. },
  640. {
  641. .id = EXYNOS5_DRDPHY_PIPE3,
  642. .phy_isol = exynos5_usbdrd_phy_isol,
  643. .phy_init = exynos5_usbdrd_pipe3_init,
  644. .set_refclk = exynos5_usbdrd_pipe3_set_refclk,
  645. },
  646. };
  647. static const struct exynos5_usbdrd_phy_drvdata exynos5420_usbdrd_phy = {
  648. .phy_cfg = phy_cfg_exynos5,
  649. .pmu_offset_usbdrd0_phy = EXYNOS5_USBDRD_PHY_CONTROL,
  650. .pmu_offset_usbdrd1_phy = EXYNOS5420_USBDRD1_PHY_CONTROL,
  651. .has_common_clk_gate = true,
  652. };
  653. static const struct exynos5_usbdrd_phy_drvdata exynos5250_usbdrd_phy = {
  654. .phy_cfg = phy_cfg_exynos5,
  655. .pmu_offset_usbdrd0_phy = EXYNOS5_USBDRD_PHY_CONTROL,
  656. .has_common_clk_gate = true,
  657. };
  658. static const struct exynos5_usbdrd_phy_drvdata exynos5433_usbdrd_phy = {
  659. .phy_cfg = phy_cfg_exynos5,
  660. .pmu_offset_usbdrd0_phy = EXYNOS5_USBDRD_PHY_CONTROL,
  661. .pmu_offset_usbdrd1_phy = EXYNOS5433_USBHOST30_PHY_CONTROL,
  662. .has_common_clk_gate = false,
  663. };
  664. static const struct exynos5_usbdrd_phy_drvdata exynos7_usbdrd_phy = {
  665. .phy_cfg = phy_cfg_exynos5,
  666. .pmu_offset_usbdrd0_phy = EXYNOS5_USBDRD_PHY_CONTROL,
  667. .has_common_clk_gate = false,
  668. };
  669. static const struct of_device_id exynos5_usbdrd_phy_of_match[] = {
  670. {
  671. .compatible = "samsung,exynos5250-usbdrd-phy",
  672. .data = &exynos5250_usbdrd_phy
  673. }, {
  674. .compatible = "samsung,exynos5420-usbdrd-phy",
  675. .data = &exynos5420_usbdrd_phy
  676. }, {
  677. .compatible = "samsung,exynos5433-usbdrd-phy",
  678. .data = &exynos5433_usbdrd_phy
  679. }, {
  680. .compatible = "samsung,exynos7-usbdrd-phy",
  681. .data = &exynos7_usbdrd_phy
  682. },
  683. { },
  684. };
  685. MODULE_DEVICE_TABLE(of, exynos5_usbdrd_phy_of_match);
  686. static int exynos5_usbdrd_phy_probe(struct platform_device *pdev)
  687. {
  688. struct device *dev = &pdev->dev;
  689. struct device_node *node = dev->of_node;
  690. struct exynos5_usbdrd_phy *phy_drd;
  691. struct phy_provider *phy_provider;
  692. const struct exynos5_usbdrd_phy_drvdata *drv_data;
  693. struct regmap *reg_pmu;
  694. u32 pmu_offset;
  695. int i, ret;
  696. int channel;
  697. phy_drd = devm_kzalloc(dev, sizeof(*phy_drd), GFP_KERNEL);
  698. if (!phy_drd)
  699. return -ENOMEM;
  700. dev_set_drvdata(dev, phy_drd);
  701. phy_drd->dev = dev;
  702. phy_drd->reg_phy = devm_platform_ioremap_resource(pdev, 0);
  703. if (IS_ERR(phy_drd->reg_phy))
  704. return PTR_ERR(phy_drd->reg_phy);
  705. drv_data = of_device_get_match_data(dev);
  706. if (!drv_data)
  707. return -EINVAL;
  708. phy_drd->drv_data = drv_data;
  709. ret = exynos5_usbdrd_phy_clk_handle(phy_drd);
  710. if (ret) {
  711. dev_err(dev, "Failed to initialize clocks\n");
  712. return ret;
  713. }
  714. reg_pmu = syscon_regmap_lookup_by_phandle(dev->of_node,
  715. "samsung,pmu-syscon");
  716. if (IS_ERR(reg_pmu)) {
  717. dev_err(dev, "Failed to lookup PMU regmap\n");
  718. return PTR_ERR(reg_pmu);
  719. }
  720. /*
  721. * Exynos5420 SoC has multiple channels for USB 3.0 PHY, with
  722. * each having separate power control registers.
  723. * 'channel' facilitates to set such registers.
  724. */
  725. channel = of_alias_get_id(node, "usbdrdphy");
  726. if (channel < 0)
  727. dev_dbg(dev, "Not a multi-controller usbdrd phy\n");
  728. switch (channel) {
  729. case 1:
  730. pmu_offset = phy_drd->drv_data->pmu_offset_usbdrd1_phy;
  731. break;
  732. case 0:
  733. default:
  734. pmu_offset = phy_drd->drv_data->pmu_offset_usbdrd0_phy;
  735. break;
  736. }
  737. /* Get Vbus regulators */
  738. phy_drd->vbus = devm_regulator_get(dev, "vbus");
  739. if (IS_ERR(phy_drd->vbus)) {
  740. ret = PTR_ERR(phy_drd->vbus);
  741. if (ret == -EPROBE_DEFER)
  742. return ret;
  743. dev_warn(dev, "Failed to get VBUS supply regulator\n");
  744. phy_drd->vbus = NULL;
  745. }
  746. phy_drd->vbus_boost = devm_regulator_get(dev, "vbus-boost");
  747. if (IS_ERR(phy_drd->vbus_boost)) {
  748. ret = PTR_ERR(phy_drd->vbus_boost);
  749. if (ret == -EPROBE_DEFER)
  750. return ret;
  751. dev_warn(dev, "Failed to get VBUS boost supply regulator\n");
  752. phy_drd->vbus_boost = NULL;
  753. }
  754. dev_vdbg(dev, "Creating usbdrd_phy phy\n");
  755. for (i = 0; i < EXYNOS5_DRDPHYS_NUM; i++) {
  756. struct phy *phy = devm_phy_create(dev, NULL,
  757. &exynos5_usbdrd_phy_ops);
  758. if (IS_ERR(phy)) {
  759. dev_err(dev, "Failed to create usbdrd_phy phy\n");
  760. return PTR_ERR(phy);
  761. }
  762. phy_drd->phys[i].phy = phy;
  763. phy_drd->phys[i].index = i;
  764. phy_drd->phys[i].reg_pmu = reg_pmu;
  765. phy_drd->phys[i].pmu_offset = pmu_offset;
  766. phy_drd->phys[i].phy_cfg = &drv_data->phy_cfg[i];
  767. phy_set_drvdata(phy, &phy_drd->phys[i]);
  768. }
  769. phy_provider = devm_of_phy_provider_register(dev,
  770. exynos5_usbdrd_phy_xlate);
  771. if (IS_ERR(phy_provider)) {
  772. dev_err(phy_drd->dev, "Failed to register phy provider\n");
  773. return PTR_ERR(phy_provider);
  774. }
  775. return 0;
  776. }
  777. static struct platform_driver exynos5_usb3drd_phy = {
  778. .probe = exynos5_usbdrd_phy_probe,
  779. .driver = {
  780. .of_match_table = exynos5_usbdrd_phy_of_match,
  781. .name = "exynos5_usb3drd_phy",
  782. .suppress_bind_attrs = true,
  783. }
  784. };
  785. module_platform_driver(exynos5_usb3drd_phy);
  786. MODULE_DESCRIPTION("Samsung Exynos5 SoCs USB 3.0 DRD controller PHY driver");
  787. MODULE_AUTHOR("Vivek Gautam <[email protected]>");
  788. MODULE_LICENSE("GPL v2");
  789. MODULE_ALIAS("platform:exynos5_usb3drd_phy");