phy-exynos5-usbdrd.c 26 KB

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