phy-mxs-usb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * Copyright 2012-2014 Freescale Semiconductor, Inc.
  3. * Copyright (C) 2012 Marek Vasut <marex@denx.de>
  4. * on behalf of DENX Software Engineering GmbH
  5. *
  6. * The code contained herein is licensed under the GNU General Public
  7. * License. You may obtain a copy of the GNU General Public License
  8. * Version 2 or later at the following locations:
  9. *
  10. * http://www.opensource.org/licenses/gpl-license.html
  11. * http://www.gnu.org/copyleft/gpl.html
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/clk.h>
  17. #include <linux/usb/otg.h>
  18. #include <linux/stmp_device.h>
  19. #include <linux/delay.h>
  20. #include <linux/err.h>
  21. #include <linux/io.h>
  22. #include <linux/of_device.h>
  23. #include <linux/regmap.h>
  24. #include <linux/mfd/syscon.h>
  25. #define DRIVER_NAME "mxs_phy"
  26. #define HW_USBPHY_PWD 0x00
  27. #define HW_USBPHY_TX 0x10
  28. #define HW_USBPHY_CTRL 0x30
  29. #define HW_USBPHY_CTRL_SET 0x34
  30. #define HW_USBPHY_CTRL_CLR 0x38
  31. #define HW_USBPHY_DEBUG_SET 0x54
  32. #define HW_USBPHY_DEBUG_CLR 0x58
  33. #define HW_USBPHY_IP 0x90
  34. #define HW_USBPHY_IP_SET 0x94
  35. #define HW_USBPHY_IP_CLR 0x98
  36. #define GM_USBPHY_TX_TXCAL45DP(x) (((x) & 0xf) << 16)
  37. #define GM_USBPHY_TX_TXCAL45DN(x) (((x) & 0xf) << 8)
  38. #define GM_USBPHY_TX_D_CAL(x) (((x) & 0xf) << 0)
  39. #define BM_USBPHY_CTRL_SFTRST BIT(31)
  40. #define BM_USBPHY_CTRL_CLKGATE BIT(30)
  41. #define BM_USBPHY_CTRL_OTG_ID_VALUE BIT(27)
  42. #define BM_USBPHY_CTRL_ENAUTOSET_USBCLKS BIT(26)
  43. #define BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE BIT(25)
  44. #define BM_USBPHY_CTRL_ENVBUSCHG_WKUP BIT(23)
  45. #define BM_USBPHY_CTRL_ENIDCHG_WKUP BIT(22)
  46. #define BM_USBPHY_CTRL_ENDPDMCHG_WKUP BIT(21)
  47. #define BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD BIT(20)
  48. #define BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE BIT(19)
  49. #define BM_USBPHY_CTRL_ENAUTO_PWRON_PLL BIT(18)
  50. #define BM_USBPHY_CTRL_ENUTMILEVEL3 BIT(15)
  51. #define BM_USBPHY_CTRL_ENUTMILEVEL2 BIT(14)
  52. #define BM_USBPHY_CTRL_ENHOSTDISCONDETECT BIT(1)
  53. #define BM_USBPHY_IP_FIX (BIT(17) | BIT(18))
  54. #define BM_USBPHY_DEBUG_CLKGATE BIT(30)
  55. /* Anatop Registers */
  56. #define ANADIG_ANA_MISC0 0x150
  57. #define ANADIG_ANA_MISC0_SET 0x154
  58. #define ANADIG_ANA_MISC0_CLR 0x158
  59. #define ANADIG_USB1_VBUS_DET_STAT 0x1c0
  60. #define ANADIG_USB2_VBUS_DET_STAT 0x220
  61. #define ANADIG_USB1_LOOPBACK_SET 0x1e4
  62. #define ANADIG_USB1_LOOPBACK_CLR 0x1e8
  63. #define ANADIG_USB2_LOOPBACK_SET 0x244
  64. #define ANADIG_USB2_LOOPBACK_CLR 0x248
  65. #define ANADIG_USB1_MISC 0x1f0
  66. #define ANADIG_USB2_MISC 0x250
  67. #define BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG BIT(12)
  68. #define BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG_SL BIT(11)
  69. #define BM_ANADIG_USB1_VBUS_DET_STAT_VBUS_VALID BIT(3)
  70. #define BM_ANADIG_USB2_VBUS_DET_STAT_VBUS_VALID BIT(3)
  71. #define BM_ANADIG_USB1_LOOPBACK_UTMI_DIG_TST1 BIT(2)
  72. #define BM_ANADIG_USB1_LOOPBACK_TSTI_TX_EN BIT(5)
  73. #define BM_ANADIG_USB2_LOOPBACK_UTMI_DIG_TST1 BIT(2)
  74. #define BM_ANADIG_USB2_LOOPBACK_TSTI_TX_EN BIT(5)
  75. #define BM_ANADIG_USB1_MISC_RX_VPIN_FS BIT(29)
  76. #define BM_ANADIG_USB1_MISC_RX_VMIN_FS BIT(28)
  77. #define BM_ANADIG_USB2_MISC_RX_VPIN_FS BIT(29)
  78. #define BM_ANADIG_USB2_MISC_RX_VMIN_FS BIT(28)
  79. #define to_mxs_phy(p) container_of((p), struct mxs_phy, phy)
  80. /* Do disconnection between PHY and controller without vbus */
  81. #define MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS BIT(0)
  82. /*
  83. * The PHY will be in messy if there is a wakeup after putting
  84. * bus to suspend (set portsc.suspendM) but before setting PHY to low
  85. * power mode (set portsc.phcd).
  86. */
  87. #define MXS_PHY_ABNORMAL_IN_SUSPEND BIT(1)
  88. /*
  89. * The SOF sends too fast after resuming, it will cause disconnection
  90. * between host and high speed device.
  91. */
  92. #define MXS_PHY_SENDING_SOF_TOO_FAST BIT(2)
  93. /*
  94. * IC has bug fixes logic, they include
  95. * MXS_PHY_ABNORMAL_IN_SUSPEND and MXS_PHY_SENDING_SOF_TOO_FAST
  96. * which are described at above flags, the RTL will handle it
  97. * according to different versions.
  98. */
  99. #define MXS_PHY_NEED_IP_FIX BIT(3)
  100. /* Minimum and maximum values for device tree entries */
  101. #define MXS_PHY_TX_CAL45_MIN 30
  102. #define MXS_PHY_TX_CAL45_MAX 55
  103. #define MXS_PHY_TX_D_CAL_MIN 79
  104. #define MXS_PHY_TX_D_CAL_MAX 119
  105. struct mxs_phy_data {
  106. unsigned int flags;
  107. };
  108. static const struct mxs_phy_data imx23_phy_data = {
  109. .flags = MXS_PHY_ABNORMAL_IN_SUSPEND | MXS_PHY_SENDING_SOF_TOO_FAST,
  110. };
  111. static const struct mxs_phy_data imx6q_phy_data = {
  112. .flags = MXS_PHY_SENDING_SOF_TOO_FAST |
  113. MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
  114. MXS_PHY_NEED_IP_FIX,
  115. };
  116. static const struct mxs_phy_data imx6sl_phy_data = {
  117. .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
  118. MXS_PHY_NEED_IP_FIX,
  119. };
  120. static const struct mxs_phy_data vf610_phy_data = {
  121. .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
  122. MXS_PHY_NEED_IP_FIX,
  123. };
  124. static const struct mxs_phy_data imx6sx_phy_data = {
  125. .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS,
  126. };
  127. static const struct mxs_phy_data imx6ul_phy_data = {
  128. .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS,
  129. };
  130. static const struct of_device_id mxs_phy_dt_ids[] = {
  131. { .compatible = "fsl,imx6sx-usbphy", .data = &imx6sx_phy_data, },
  132. { .compatible = "fsl,imx6sl-usbphy", .data = &imx6sl_phy_data, },
  133. { .compatible = "fsl,imx6q-usbphy", .data = &imx6q_phy_data, },
  134. { .compatible = "fsl,imx23-usbphy", .data = &imx23_phy_data, },
  135. { .compatible = "fsl,vf610-usbphy", .data = &vf610_phy_data, },
  136. { .compatible = "fsl,imx6ul-usbphy", .data = &imx6ul_phy_data, },
  137. { /* sentinel */ }
  138. };
  139. MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids);
  140. struct mxs_phy {
  141. struct usb_phy phy;
  142. struct clk *clk;
  143. const struct mxs_phy_data *data;
  144. struct regmap *regmap_anatop;
  145. int port_id;
  146. u32 tx_reg_set;
  147. u32 tx_reg_mask;
  148. };
  149. static inline bool is_imx6q_phy(struct mxs_phy *mxs_phy)
  150. {
  151. return mxs_phy->data == &imx6q_phy_data;
  152. }
  153. static inline bool is_imx6sl_phy(struct mxs_phy *mxs_phy)
  154. {
  155. return mxs_phy->data == &imx6sl_phy_data;
  156. }
  157. /*
  158. * PHY needs some 32K cycles to switch from 32K clock to
  159. * bus (such as AHB/AXI, etc) clock.
  160. */
  161. static void mxs_phy_clock_switch_delay(void)
  162. {
  163. usleep_range(300, 400);
  164. }
  165. static void mxs_phy_tx_init(struct mxs_phy *mxs_phy)
  166. {
  167. void __iomem *base = mxs_phy->phy.io_priv;
  168. u32 phytx;
  169. /* Update TX register if there is anything to write */
  170. if (mxs_phy->tx_reg_mask) {
  171. phytx = readl(base + HW_USBPHY_TX);
  172. phytx &= ~mxs_phy->tx_reg_mask;
  173. phytx |= mxs_phy->tx_reg_set;
  174. writel(phytx, base + HW_USBPHY_TX);
  175. }
  176. }
  177. static int mxs_phy_hw_init(struct mxs_phy *mxs_phy)
  178. {
  179. int ret;
  180. void __iomem *base = mxs_phy->phy.io_priv;
  181. ret = stmp_reset_block(base + HW_USBPHY_CTRL);
  182. if (ret)
  183. return ret;
  184. /* Power up the PHY */
  185. writel(0, base + HW_USBPHY_PWD);
  186. /*
  187. * USB PHY Ctrl Setting
  188. * - Auto clock/power on
  189. * - Enable full/low speed support
  190. */
  191. writel(BM_USBPHY_CTRL_ENAUTOSET_USBCLKS |
  192. BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE |
  193. BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD |
  194. BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE |
  195. BM_USBPHY_CTRL_ENAUTO_PWRON_PLL |
  196. BM_USBPHY_CTRL_ENUTMILEVEL2 |
  197. BM_USBPHY_CTRL_ENUTMILEVEL3,
  198. base + HW_USBPHY_CTRL_SET);
  199. if (mxs_phy->data->flags & MXS_PHY_NEED_IP_FIX)
  200. writel(BM_USBPHY_IP_FIX, base + HW_USBPHY_IP_SET);
  201. mxs_phy_tx_init(mxs_phy);
  202. return 0;
  203. }
  204. /* Return true if the vbus is there */
  205. static bool mxs_phy_get_vbus_status(struct mxs_phy *mxs_phy)
  206. {
  207. unsigned int vbus_value = 0;
  208. if (!mxs_phy->regmap_anatop)
  209. return false;
  210. if (mxs_phy->port_id == 0)
  211. regmap_read(mxs_phy->regmap_anatop,
  212. ANADIG_USB1_VBUS_DET_STAT,
  213. &vbus_value);
  214. else if (mxs_phy->port_id == 1)
  215. regmap_read(mxs_phy->regmap_anatop,
  216. ANADIG_USB2_VBUS_DET_STAT,
  217. &vbus_value);
  218. if (vbus_value & BM_ANADIG_USB1_VBUS_DET_STAT_VBUS_VALID)
  219. return true;
  220. else
  221. return false;
  222. }
  223. static void __mxs_phy_disconnect_line(struct mxs_phy *mxs_phy, bool disconnect)
  224. {
  225. void __iomem *base = mxs_phy->phy.io_priv;
  226. u32 reg;
  227. if (disconnect)
  228. writel_relaxed(BM_USBPHY_DEBUG_CLKGATE,
  229. base + HW_USBPHY_DEBUG_CLR);
  230. if (mxs_phy->port_id == 0) {
  231. reg = disconnect ? ANADIG_USB1_LOOPBACK_SET
  232. : ANADIG_USB1_LOOPBACK_CLR;
  233. regmap_write(mxs_phy->regmap_anatop, reg,
  234. BM_ANADIG_USB1_LOOPBACK_UTMI_DIG_TST1 |
  235. BM_ANADIG_USB1_LOOPBACK_TSTI_TX_EN);
  236. } else if (mxs_phy->port_id == 1) {
  237. reg = disconnect ? ANADIG_USB2_LOOPBACK_SET
  238. : ANADIG_USB2_LOOPBACK_CLR;
  239. regmap_write(mxs_phy->regmap_anatop, reg,
  240. BM_ANADIG_USB2_LOOPBACK_UTMI_DIG_TST1 |
  241. BM_ANADIG_USB2_LOOPBACK_TSTI_TX_EN);
  242. }
  243. if (!disconnect)
  244. writel_relaxed(BM_USBPHY_DEBUG_CLKGATE,
  245. base + HW_USBPHY_DEBUG_SET);
  246. /* Delay some time, and let Linestate be SE0 for controller */
  247. if (disconnect)
  248. usleep_range(500, 1000);
  249. }
  250. static bool mxs_phy_is_otg_host(struct mxs_phy *mxs_phy)
  251. {
  252. void __iomem *base = mxs_phy->phy.io_priv;
  253. u32 phyctrl = readl(base + HW_USBPHY_CTRL);
  254. if (IS_ENABLED(CONFIG_USB_OTG) &&
  255. !(phyctrl & BM_USBPHY_CTRL_OTG_ID_VALUE))
  256. return true;
  257. return false;
  258. }
  259. static void mxs_phy_disconnect_line(struct mxs_phy *mxs_phy, bool on)
  260. {
  261. bool vbus_is_on = false;
  262. /* If the SoCs don't need to disconnect line without vbus, quit */
  263. if (!(mxs_phy->data->flags & MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS))
  264. return;
  265. /* If the SoCs don't have anatop, quit */
  266. if (!mxs_phy->regmap_anatop)
  267. return;
  268. vbus_is_on = mxs_phy_get_vbus_status(mxs_phy);
  269. if (on && !vbus_is_on && !mxs_phy_is_otg_host(mxs_phy))
  270. __mxs_phy_disconnect_line(mxs_phy, true);
  271. else
  272. __mxs_phy_disconnect_line(mxs_phy, false);
  273. }
  274. static int mxs_phy_init(struct usb_phy *phy)
  275. {
  276. int ret;
  277. struct mxs_phy *mxs_phy = to_mxs_phy(phy);
  278. mxs_phy_clock_switch_delay();
  279. ret = clk_prepare_enable(mxs_phy->clk);
  280. if (ret)
  281. return ret;
  282. return mxs_phy_hw_init(mxs_phy);
  283. }
  284. static void mxs_phy_shutdown(struct usb_phy *phy)
  285. {
  286. struct mxs_phy *mxs_phy = to_mxs_phy(phy);
  287. u32 value = BM_USBPHY_CTRL_ENVBUSCHG_WKUP |
  288. BM_USBPHY_CTRL_ENDPDMCHG_WKUP |
  289. BM_USBPHY_CTRL_ENIDCHG_WKUP |
  290. BM_USBPHY_CTRL_ENAUTOSET_USBCLKS |
  291. BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE |
  292. BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD |
  293. BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE |
  294. BM_USBPHY_CTRL_ENAUTO_PWRON_PLL;
  295. writel(value, phy->io_priv + HW_USBPHY_CTRL_CLR);
  296. writel(0xffffffff, phy->io_priv + HW_USBPHY_PWD);
  297. writel(BM_USBPHY_CTRL_CLKGATE,
  298. phy->io_priv + HW_USBPHY_CTRL_SET);
  299. clk_disable_unprepare(mxs_phy->clk);
  300. }
  301. static bool mxs_phy_is_low_speed_connection(struct mxs_phy *mxs_phy)
  302. {
  303. unsigned int line_state;
  304. /* bit definition is the same for all controllers */
  305. unsigned int dp_bit = BM_ANADIG_USB1_MISC_RX_VPIN_FS,
  306. dm_bit = BM_ANADIG_USB1_MISC_RX_VMIN_FS;
  307. unsigned int reg = ANADIG_USB1_MISC;
  308. /* If the SoCs don't have anatop, quit */
  309. if (!mxs_phy->regmap_anatop)
  310. return false;
  311. if (mxs_phy->port_id == 0)
  312. reg = ANADIG_USB1_MISC;
  313. else if (mxs_phy->port_id == 1)
  314. reg = ANADIG_USB2_MISC;
  315. regmap_read(mxs_phy->regmap_anatop, reg, &line_state);
  316. if ((line_state & (dp_bit | dm_bit)) == dm_bit)
  317. return true;
  318. else
  319. return false;
  320. }
  321. static int mxs_phy_suspend(struct usb_phy *x, int suspend)
  322. {
  323. int ret;
  324. struct mxs_phy *mxs_phy = to_mxs_phy(x);
  325. bool low_speed_connection, vbus_is_on;
  326. low_speed_connection = mxs_phy_is_low_speed_connection(mxs_phy);
  327. vbus_is_on = mxs_phy_get_vbus_status(mxs_phy);
  328. if (suspend) {
  329. /*
  330. * FIXME: Do not power down RXPWD1PT1 bit for low speed
  331. * connect. The low speed connection will have problem at
  332. * very rare cases during usb suspend and resume process.
  333. */
  334. if (low_speed_connection & vbus_is_on) {
  335. /*
  336. * If value to be set as pwd value is not 0xffffffff,
  337. * several 32Khz cycles are needed.
  338. */
  339. mxs_phy_clock_switch_delay();
  340. writel(0xffbfffff, x->io_priv + HW_USBPHY_PWD);
  341. } else {
  342. writel(0xffffffff, x->io_priv + HW_USBPHY_PWD);
  343. }
  344. writel(BM_USBPHY_CTRL_CLKGATE,
  345. x->io_priv + HW_USBPHY_CTRL_SET);
  346. clk_disable_unprepare(mxs_phy->clk);
  347. } else {
  348. mxs_phy_clock_switch_delay();
  349. ret = clk_prepare_enable(mxs_phy->clk);
  350. if (ret)
  351. return ret;
  352. writel(BM_USBPHY_CTRL_CLKGATE,
  353. x->io_priv + HW_USBPHY_CTRL_CLR);
  354. writel(0, x->io_priv + HW_USBPHY_PWD);
  355. }
  356. return 0;
  357. }
  358. static int mxs_phy_set_wakeup(struct usb_phy *x, bool enabled)
  359. {
  360. struct mxs_phy *mxs_phy = to_mxs_phy(x);
  361. u32 value = BM_USBPHY_CTRL_ENVBUSCHG_WKUP |
  362. BM_USBPHY_CTRL_ENDPDMCHG_WKUP |
  363. BM_USBPHY_CTRL_ENIDCHG_WKUP;
  364. if (enabled) {
  365. mxs_phy_disconnect_line(mxs_phy, true);
  366. writel_relaxed(value, x->io_priv + HW_USBPHY_CTRL_SET);
  367. } else {
  368. writel_relaxed(value, x->io_priv + HW_USBPHY_CTRL_CLR);
  369. mxs_phy_disconnect_line(mxs_phy, false);
  370. }
  371. return 0;
  372. }
  373. static int mxs_phy_on_connect(struct usb_phy *phy,
  374. enum usb_device_speed speed)
  375. {
  376. dev_dbg(phy->dev, "%s device has connected\n",
  377. (speed == USB_SPEED_HIGH) ? "HS" : "FS/LS");
  378. if (speed == USB_SPEED_HIGH)
  379. writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
  380. phy->io_priv + HW_USBPHY_CTRL_SET);
  381. return 0;
  382. }
  383. static int mxs_phy_on_disconnect(struct usb_phy *phy,
  384. enum usb_device_speed speed)
  385. {
  386. dev_dbg(phy->dev, "%s device has disconnected\n",
  387. (speed == USB_SPEED_HIGH) ? "HS" : "FS/LS");
  388. /* Sometimes, the speed is not high speed when the error occurs */
  389. if (readl(phy->io_priv + HW_USBPHY_CTRL) &
  390. BM_USBPHY_CTRL_ENHOSTDISCONDETECT)
  391. writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
  392. phy->io_priv + HW_USBPHY_CTRL_CLR);
  393. return 0;
  394. }
  395. static int mxs_phy_probe(struct platform_device *pdev)
  396. {
  397. struct resource *res;
  398. void __iomem *base;
  399. struct clk *clk;
  400. struct mxs_phy *mxs_phy;
  401. int ret;
  402. const struct of_device_id *of_id;
  403. struct device_node *np = pdev->dev.of_node;
  404. u32 val;
  405. of_id = of_match_device(mxs_phy_dt_ids, &pdev->dev);
  406. if (!of_id)
  407. return -ENODEV;
  408. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  409. base = devm_ioremap_resource(&pdev->dev, res);
  410. if (IS_ERR(base))
  411. return PTR_ERR(base);
  412. clk = devm_clk_get(&pdev->dev, NULL);
  413. if (IS_ERR(clk)) {
  414. dev_err(&pdev->dev,
  415. "can't get the clock, err=%ld", PTR_ERR(clk));
  416. return PTR_ERR(clk);
  417. }
  418. mxs_phy = devm_kzalloc(&pdev->dev, sizeof(*mxs_phy), GFP_KERNEL);
  419. if (!mxs_phy)
  420. return -ENOMEM;
  421. /* Some SoCs don't have anatop registers */
  422. if (of_get_property(np, "fsl,anatop", NULL)) {
  423. mxs_phy->regmap_anatop = syscon_regmap_lookup_by_phandle
  424. (np, "fsl,anatop");
  425. if (IS_ERR(mxs_phy->regmap_anatop)) {
  426. dev_dbg(&pdev->dev,
  427. "failed to find regmap for anatop\n");
  428. return PTR_ERR(mxs_phy->regmap_anatop);
  429. }
  430. }
  431. /* Precompute which bits of the TX register are to be updated, if any */
  432. if (!of_property_read_u32(np, "fsl,tx-cal-45-dn-ohms", &val) &&
  433. val >= MXS_PHY_TX_CAL45_MIN && val <= MXS_PHY_TX_CAL45_MAX) {
  434. /* Scale to a 4-bit value */
  435. val = (MXS_PHY_TX_CAL45_MAX - val) * 0xF
  436. / (MXS_PHY_TX_CAL45_MAX - MXS_PHY_TX_CAL45_MIN);
  437. mxs_phy->tx_reg_mask |= GM_USBPHY_TX_TXCAL45DN(~0);
  438. mxs_phy->tx_reg_set |= GM_USBPHY_TX_TXCAL45DN(val);
  439. }
  440. if (!of_property_read_u32(np, "fsl,tx-cal-45-dp-ohms", &val) &&
  441. val >= MXS_PHY_TX_CAL45_MIN && val <= MXS_PHY_TX_CAL45_MAX) {
  442. /* Scale to a 4-bit value. */
  443. val = (MXS_PHY_TX_CAL45_MAX - val) * 0xF
  444. / (MXS_PHY_TX_CAL45_MAX - MXS_PHY_TX_CAL45_MIN);
  445. mxs_phy->tx_reg_mask |= GM_USBPHY_TX_TXCAL45DP(~0);
  446. mxs_phy->tx_reg_set |= GM_USBPHY_TX_TXCAL45DP(val);
  447. }
  448. if (!of_property_read_u32(np, "fsl,tx-d-cal", &val) &&
  449. val >= MXS_PHY_TX_D_CAL_MIN && val <= MXS_PHY_TX_D_CAL_MAX) {
  450. /* Scale to a 4-bit value. Round up the values and heavily
  451. * weight the rounding by adding 2/3 of the denominator.
  452. */
  453. val = ((MXS_PHY_TX_D_CAL_MAX - val) * 0xF
  454. + (MXS_PHY_TX_D_CAL_MAX - MXS_PHY_TX_D_CAL_MIN) * 2/3)
  455. / (MXS_PHY_TX_D_CAL_MAX - MXS_PHY_TX_D_CAL_MIN);
  456. mxs_phy->tx_reg_mask |= GM_USBPHY_TX_D_CAL(~0);
  457. mxs_phy->tx_reg_set |= GM_USBPHY_TX_D_CAL(val);
  458. }
  459. ret = of_alias_get_id(np, "usbphy");
  460. if (ret < 0)
  461. dev_dbg(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  462. mxs_phy->port_id = ret;
  463. mxs_phy->phy.io_priv = base;
  464. mxs_phy->phy.dev = &pdev->dev;
  465. mxs_phy->phy.label = DRIVER_NAME;
  466. mxs_phy->phy.init = mxs_phy_init;
  467. mxs_phy->phy.shutdown = mxs_phy_shutdown;
  468. mxs_phy->phy.set_suspend = mxs_phy_suspend;
  469. mxs_phy->phy.notify_connect = mxs_phy_on_connect;
  470. mxs_phy->phy.notify_disconnect = mxs_phy_on_disconnect;
  471. mxs_phy->phy.type = USB_PHY_TYPE_USB2;
  472. mxs_phy->phy.set_wakeup = mxs_phy_set_wakeup;
  473. mxs_phy->clk = clk;
  474. mxs_phy->data = of_id->data;
  475. platform_set_drvdata(pdev, mxs_phy);
  476. device_set_wakeup_capable(&pdev->dev, true);
  477. return usb_add_phy_dev(&mxs_phy->phy);
  478. }
  479. static int mxs_phy_remove(struct platform_device *pdev)
  480. {
  481. struct mxs_phy *mxs_phy = platform_get_drvdata(pdev);
  482. usb_remove_phy(&mxs_phy->phy);
  483. return 0;
  484. }
  485. #ifdef CONFIG_PM_SLEEP
  486. static void mxs_phy_enable_ldo_in_suspend(struct mxs_phy *mxs_phy, bool on)
  487. {
  488. unsigned int reg = on ? ANADIG_ANA_MISC0_SET : ANADIG_ANA_MISC0_CLR;
  489. /* If the SoCs don't have anatop, quit */
  490. if (!mxs_phy->regmap_anatop)
  491. return;
  492. if (is_imx6q_phy(mxs_phy))
  493. regmap_write(mxs_phy->regmap_anatop, reg,
  494. BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG);
  495. else if (is_imx6sl_phy(mxs_phy))
  496. regmap_write(mxs_phy->regmap_anatop,
  497. reg, BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG_SL);
  498. }
  499. static int mxs_phy_system_suspend(struct device *dev)
  500. {
  501. struct mxs_phy *mxs_phy = dev_get_drvdata(dev);
  502. if (device_may_wakeup(dev))
  503. mxs_phy_enable_ldo_in_suspend(mxs_phy, true);
  504. return 0;
  505. }
  506. static int mxs_phy_system_resume(struct device *dev)
  507. {
  508. struct mxs_phy *mxs_phy = dev_get_drvdata(dev);
  509. if (device_may_wakeup(dev))
  510. mxs_phy_enable_ldo_in_suspend(mxs_phy, false);
  511. return 0;
  512. }
  513. #endif /* CONFIG_PM_SLEEP */
  514. static SIMPLE_DEV_PM_OPS(mxs_phy_pm, mxs_phy_system_suspend,
  515. mxs_phy_system_resume);
  516. static struct platform_driver mxs_phy_driver = {
  517. .probe = mxs_phy_probe,
  518. .remove = mxs_phy_remove,
  519. .driver = {
  520. .name = DRIVER_NAME,
  521. .of_match_table = mxs_phy_dt_ids,
  522. .pm = &mxs_phy_pm,
  523. },
  524. };
  525. static int __init mxs_phy_module_init(void)
  526. {
  527. return platform_driver_register(&mxs_phy_driver);
  528. }
  529. postcore_initcall(mxs_phy_module_init);
  530. static void __exit mxs_phy_module_exit(void)
  531. {
  532. platform_driver_unregister(&mxs_phy_driver);
  533. }
  534. module_exit(mxs_phy_module_exit);
  535. MODULE_ALIAS("platform:mxs-usb-phy");
  536. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  537. MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");
  538. MODULE_DESCRIPTION("Freescale MXS USB PHY driver");
  539. MODULE_LICENSE("GPL");