phy-qcom-usb-hs-28nm.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2009-2018, Linux Foundation. All rights reserved.
  4. * Copyright (c) 2018-2020, Linaro Limited
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/delay.h>
  8. #include <linux/io.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_graph.h>
  13. #include <linux/phy/phy.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/reset.h>
  17. #include <linux/slab.h>
  18. /* PHY register and bit definitions */
  19. #define PHY_CTRL_COMMON0 0x078
  20. #define SIDDQ BIT(2)
  21. #define PHY_IRQ_CMD 0x0d0
  22. #define PHY_INTR_MASK0 0x0d4
  23. #define PHY_INTR_CLEAR0 0x0dc
  24. #define DPDM_MASK 0x1e
  25. #define DP_1_0 BIT(4)
  26. #define DP_0_1 BIT(3)
  27. #define DM_1_0 BIT(2)
  28. #define DM_0_1 BIT(1)
  29. enum hsphy_voltage {
  30. VOL_NONE,
  31. VOL_MIN,
  32. VOL_MAX,
  33. VOL_NUM,
  34. };
  35. enum hsphy_vreg {
  36. VDD,
  37. VDDA_1P8,
  38. VDDA_3P3,
  39. VREG_NUM,
  40. };
  41. struct hsphy_init_seq {
  42. int offset;
  43. int val;
  44. int delay;
  45. };
  46. struct hsphy_data {
  47. const struct hsphy_init_seq *init_seq;
  48. unsigned int init_seq_num;
  49. };
  50. struct hsphy_priv {
  51. void __iomem *base;
  52. struct clk_bulk_data *clks;
  53. int num_clks;
  54. struct reset_control *phy_reset;
  55. struct reset_control *por_reset;
  56. struct regulator_bulk_data vregs[VREG_NUM];
  57. const struct hsphy_data *data;
  58. enum phy_mode mode;
  59. };
  60. static int qcom_snps_hsphy_set_mode(struct phy *phy, enum phy_mode mode,
  61. int submode)
  62. {
  63. struct hsphy_priv *priv = phy_get_drvdata(phy);
  64. priv->mode = PHY_MODE_INVALID;
  65. if (mode > 0)
  66. priv->mode = mode;
  67. return 0;
  68. }
  69. static void qcom_snps_hsphy_enable_hv_interrupts(struct hsphy_priv *priv)
  70. {
  71. u32 val;
  72. /* Clear any existing interrupts before enabling the interrupts */
  73. val = readb(priv->base + PHY_INTR_CLEAR0);
  74. val |= DPDM_MASK;
  75. writeb(val, priv->base + PHY_INTR_CLEAR0);
  76. writeb(0x0, priv->base + PHY_IRQ_CMD);
  77. usleep_range(200, 220);
  78. writeb(0x1, priv->base + PHY_IRQ_CMD);
  79. /* Make sure the interrupts are cleared */
  80. usleep_range(200, 220);
  81. val = readb(priv->base + PHY_INTR_MASK0);
  82. switch (priv->mode) {
  83. case PHY_MODE_USB_HOST_HS:
  84. case PHY_MODE_USB_HOST_FS:
  85. case PHY_MODE_USB_DEVICE_HS:
  86. case PHY_MODE_USB_DEVICE_FS:
  87. val |= DP_1_0 | DM_0_1;
  88. break;
  89. case PHY_MODE_USB_HOST_LS:
  90. case PHY_MODE_USB_DEVICE_LS:
  91. val |= DP_0_1 | DM_1_0;
  92. break;
  93. default:
  94. /* No device connected */
  95. val |= DP_0_1 | DM_0_1;
  96. break;
  97. }
  98. writeb(val, priv->base + PHY_INTR_MASK0);
  99. }
  100. static void qcom_snps_hsphy_disable_hv_interrupts(struct hsphy_priv *priv)
  101. {
  102. u32 val;
  103. val = readb(priv->base + PHY_INTR_MASK0);
  104. val &= ~DPDM_MASK;
  105. writeb(val, priv->base + PHY_INTR_MASK0);
  106. /* Clear any pending interrupts */
  107. val = readb(priv->base + PHY_INTR_CLEAR0);
  108. val |= DPDM_MASK;
  109. writeb(val, priv->base + PHY_INTR_CLEAR0);
  110. writeb(0x0, priv->base + PHY_IRQ_CMD);
  111. usleep_range(200, 220);
  112. writeb(0x1, priv->base + PHY_IRQ_CMD);
  113. usleep_range(200, 220);
  114. }
  115. static void qcom_snps_hsphy_enter_retention(struct hsphy_priv *priv)
  116. {
  117. u32 val;
  118. val = readb(priv->base + PHY_CTRL_COMMON0);
  119. val |= SIDDQ;
  120. writeb(val, priv->base + PHY_CTRL_COMMON0);
  121. }
  122. static void qcom_snps_hsphy_exit_retention(struct hsphy_priv *priv)
  123. {
  124. u32 val;
  125. val = readb(priv->base + PHY_CTRL_COMMON0);
  126. val &= ~SIDDQ;
  127. writeb(val, priv->base + PHY_CTRL_COMMON0);
  128. }
  129. static int qcom_snps_hsphy_power_on(struct phy *phy)
  130. {
  131. struct hsphy_priv *priv = phy_get_drvdata(phy);
  132. int ret;
  133. ret = regulator_bulk_enable(VREG_NUM, priv->vregs);
  134. if (ret)
  135. return ret;
  136. qcom_snps_hsphy_disable_hv_interrupts(priv);
  137. qcom_snps_hsphy_exit_retention(priv);
  138. return 0;
  139. }
  140. static int qcom_snps_hsphy_power_off(struct phy *phy)
  141. {
  142. struct hsphy_priv *priv = phy_get_drvdata(phy);
  143. qcom_snps_hsphy_enter_retention(priv);
  144. qcom_snps_hsphy_enable_hv_interrupts(priv);
  145. regulator_bulk_disable(VREG_NUM, priv->vregs);
  146. return 0;
  147. }
  148. static int qcom_snps_hsphy_reset(struct hsphy_priv *priv)
  149. {
  150. int ret;
  151. ret = reset_control_assert(priv->phy_reset);
  152. if (ret)
  153. return ret;
  154. usleep_range(10, 15);
  155. ret = reset_control_deassert(priv->phy_reset);
  156. if (ret)
  157. return ret;
  158. usleep_range(80, 100);
  159. return 0;
  160. }
  161. static void qcom_snps_hsphy_init_sequence(struct hsphy_priv *priv)
  162. {
  163. const struct hsphy_data *data = priv->data;
  164. const struct hsphy_init_seq *seq;
  165. int i;
  166. /* Device match data is optional. */
  167. if (!data)
  168. return;
  169. seq = data->init_seq;
  170. for (i = 0; i < data->init_seq_num; i++, seq++) {
  171. writeb(seq->val, priv->base + seq->offset);
  172. if (seq->delay)
  173. usleep_range(seq->delay, seq->delay + 10);
  174. }
  175. }
  176. static int qcom_snps_hsphy_por_reset(struct hsphy_priv *priv)
  177. {
  178. int ret;
  179. ret = reset_control_assert(priv->por_reset);
  180. if (ret)
  181. return ret;
  182. /*
  183. * The Femto PHY is POR reset in the following scenarios.
  184. *
  185. * 1. After overriding the parameter registers.
  186. * 2. Low power mode exit from PHY retention.
  187. *
  188. * Ensure that SIDDQ is cleared before bringing the PHY
  189. * out of reset.
  190. */
  191. qcom_snps_hsphy_exit_retention(priv);
  192. /*
  193. * As per databook, 10 usec delay is required between
  194. * PHY POR assert and de-assert.
  195. */
  196. usleep_range(10, 20);
  197. ret = reset_control_deassert(priv->por_reset);
  198. if (ret)
  199. return ret;
  200. /*
  201. * As per databook, it takes 75 usec for PHY to stabilize
  202. * after the reset.
  203. */
  204. usleep_range(80, 100);
  205. return 0;
  206. }
  207. static int qcom_snps_hsphy_init(struct phy *phy)
  208. {
  209. struct hsphy_priv *priv = phy_get_drvdata(phy);
  210. int ret;
  211. ret = clk_bulk_prepare_enable(priv->num_clks, priv->clks);
  212. if (ret)
  213. return ret;
  214. ret = qcom_snps_hsphy_reset(priv);
  215. if (ret)
  216. goto disable_clocks;
  217. qcom_snps_hsphy_init_sequence(priv);
  218. ret = qcom_snps_hsphy_por_reset(priv);
  219. if (ret)
  220. goto disable_clocks;
  221. return 0;
  222. disable_clocks:
  223. clk_bulk_disable_unprepare(priv->num_clks, priv->clks);
  224. return ret;
  225. }
  226. static int qcom_snps_hsphy_exit(struct phy *phy)
  227. {
  228. struct hsphy_priv *priv = phy_get_drvdata(phy);
  229. clk_bulk_disable_unprepare(priv->num_clks, priv->clks);
  230. return 0;
  231. }
  232. static const struct phy_ops qcom_snps_hsphy_ops = {
  233. .init = qcom_snps_hsphy_init,
  234. .exit = qcom_snps_hsphy_exit,
  235. .power_on = qcom_snps_hsphy_power_on,
  236. .power_off = qcom_snps_hsphy_power_off,
  237. .set_mode = qcom_snps_hsphy_set_mode,
  238. .owner = THIS_MODULE,
  239. };
  240. static const char * const qcom_snps_hsphy_clks[] = {
  241. "ref",
  242. "ahb",
  243. "sleep",
  244. };
  245. static int qcom_snps_hsphy_probe(struct platform_device *pdev)
  246. {
  247. struct device *dev = &pdev->dev;
  248. struct phy_provider *provider;
  249. struct hsphy_priv *priv;
  250. struct phy *phy;
  251. int ret;
  252. int i;
  253. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  254. if (!priv)
  255. return -ENOMEM;
  256. priv->base = devm_platform_ioremap_resource(pdev, 0);
  257. if (IS_ERR(priv->base))
  258. return PTR_ERR(priv->base);
  259. priv->num_clks = ARRAY_SIZE(qcom_snps_hsphy_clks);
  260. priv->clks = devm_kcalloc(dev, priv->num_clks, sizeof(*priv->clks),
  261. GFP_KERNEL);
  262. if (!priv->clks)
  263. return -ENOMEM;
  264. for (i = 0; i < priv->num_clks; i++)
  265. priv->clks[i].id = qcom_snps_hsphy_clks[i];
  266. ret = devm_clk_bulk_get(dev, priv->num_clks, priv->clks);
  267. if (ret)
  268. return ret;
  269. priv->phy_reset = devm_reset_control_get_exclusive(dev, "phy");
  270. if (IS_ERR(priv->phy_reset))
  271. return PTR_ERR(priv->phy_reset);
  272. priv->por_reset = devm_reset_control_get_exclusive(dev, "por");
  273. if (IS_ERR(priv->por_reset))
  274. return PTR_ERR(priv->por_reset);
  275. priv->vregs[VDD].supply = "vdd";
  276. priv->vregs[VDDA_1P8].supply = "vdda1p8";
  277. priv->vregs[VDDA_3P3].supply = "vdda3p3";
  278. ret = devm_regulator_bulk_get(dev, VREG_NUM, priv->vregs);
  279. if (ret)
  280. return ret;
  281. /* Get device match data */
  282. priv->data = device_get_match_data(dev);
  283. phy = devm_phy_create(dev, dev->of_node, &qcom_snps_hsphy_ops);
  284. if (IS_ERR(phy))
  285. return PTR_ERR(phy);
  286. phy_set_drvdata(phy, priv);
  287. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  288. if (IS_ERR(provider))
  289. return PTR_ERR(provider);
  290. ret = regulator_set_load(priv->vregs[VDDA_1P8].consumer, 19000);
  291. if (ret < 0)
  292. return ret;
  293. ret = regulator_set_load(priv->vregs[VDDA_3P3].consumer, 16000);
  294. if (ret < 0)
  295. goto unset_1p8_load;
  296. return 0;
  297. unset_1p8_load:
  298. regulator_set_load(priv->vregs[VDDA_1P8].consumer, 0);
  299. return ret;
  300. }
  301. /*
  302. * The macro is used to define an initialization sequence. Each tuple
  303. * is meant to program 'value' into phy register at 'offset' with 'delay'
  304. * in us followed.
  305. */
  306. #define HSPHY_INIT_CFG(o, v, d) { .offset = o, .val = v, .delay = d, }
  307. static const struct hsphy_init_seq init_seq_femtophy[] = {
  308. HSPHY_INIT_CFG(0xc0, 0x01, 0),
  309. HSPHY_INIT_CFG(0xe8, 0x0d, 0),
  310. HSPHY_INIT_CFG(0x74, 0x12, 0),
  311. HSPHY_INIT_CFG(0x98, 0x63, 0),
  312. HSPHY_INIT_CFG(0x9c, 0x03, 0),
  313. HSPHY_INIT_CFG(0xa0, 0x1d, 0),
  314. HSPHY_INIT_CFG(0xa4, 0x03, 0),
  315. HSPHY_INIT_CFG(0x8c, 0x23, 0),
  316. HSPHY_INIT_CFG(0x78, 0x08, 0),
  317. HSPHY_INIT_CFG(0x7c, 0xdc, 0),
  318. HSPHY_INIT_CFG(0x90, 0xe0, 20),
  319. HSPHY_INIT_CFG(0x74, 0x10, 0),
  320. HSPHY_INIT_CFG(0x90, 0x60, 0),
  321. };
  322. static const struct hsphy_init_seq init_seq_mdm9607[] = {
  323. HSPHY_INIT_CFG(0x80, 0x44, 0),
  324. HSPHY_INIT_CFG(0x81, 0x38, 0),
  325. HSPHY_INIT_CFG(0x82, 0x24, 0),
  326. HSPHY_INIT_CFG(0x83, 0x13, 0),
  327. };
  328. static const struct hsphy_data hsphy_data_femtophy = {
  329. .init_seq = init_seq_femtophy,
  330. .init_seq_num = ARRAY_SIZE(init_seq_femtophy),
  331. };
  332. static const struct hsphy_data hsphy_data_mdm9607 = {
  333. .init_seq = init_seq_mdm9607,
  334. .init_seq_num = ARRAY_SIZE(init_seq_mdm9607),
  335. };
  336. static const struct of_device_id qcom_snps_hsphy_match[] = {
  337. { .compatible = "qcom,usb-hs-28nm-femtophy", .data = &hsphy_data_femtophy, },
  338. { .compatible = "qcom,usb-hs-28nm-mdm9607", .data = &hsphy_data_mdm9607, },
  339. { },
  340. };
  341. MODULE_DEVICE_TABLE(of, qcom_snps_hsphy_match);
  342. static struct platform_driver qcom_snps_hsphy_driver = {
  343. .probe = qcom_snps_hsphy_probe,
  344. .driver = {
  345. .name = "qcom,usb-hs-28nm-phy",
  346. .of_match_table = qcom_snps_hsphy_match,
  347. },
  348. };
  349. module_platform_driver(qcom_snps_hsphy_driver);
  350. MODULE_DESCRIPTION("Qualcomm 28nm Hi-Speed USB PHY driver");
  351. MODULE_LICENSE("GPL v2");