phy-omap-usb2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * omap-usb2.c - USB PHY, talking to USB controller on TI SoCs.
  4. *
  5. * Copyright (C) 2012-2020 Texas Instruments Incorporated - http://www.ti.com
  6. * Author: Kishon Vijay Abraham I <kishon@ti.com>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/phy/omap_control_phy.h>
  17. #include <linux/phy/omap_usb.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/property.h>
  22. #include <linux/regmap.h>
  23. #include <linux/slab.h>
  24. #include <linux/sys_soc.h>
  25. #include <linux/usb/phy_companion.h>
  26. #define USB2PHY_ANA_CONFIG1 0x4c
  27. #define USB2PHY_DISCON_BYP_LATCH BIT(31)
  28. #define USB2PHY_CHRG_DET 0x14
  29. #define USB2PHY_CHRG_DET_USE_CHG_DET_REG BIT(29)
  30. #define USB2PHY_CHRG_DET_DIS_CHG_DET BIT(28)
  31. /* SoC Specific USB2_OTG register definitions */
  32. #define AM654_USB2_OTG_PD BIT(8)
  33. #define AM654_USB2_VBUS_DET_EN BIT(5)
  34. #define AM654_USB2_VBUSVALID_DET_EN BIT(4)
  35. #define OMAP_DEV_PHY_PD BIT(0)
  36. #define OMAP_USB2_PHY_PD BIT(28)
  37. #define AM437X_USB2_PHY_PD BIT(0)
  38. #define AM437X_USB2_OTG_PD BIT(1)
  39. #define AM437X_USB2_OTGVDET_EN BIT(19)
  40. #define AM437X_USB2_OTGSESSEND_EN BIT(20)
  41. /* Driver Flags */
  42. #define OMAP_USB2_HAS_START_SRP BIT(0)
  43. #define OMAP_USB2_HAS_SET_VBUS BIT(1)
  44. #define OMAP_USB2_CALIBRATE_FALSE_DISCONNECT BIT(2)
  45. #define OMAP_USB2_DISABLE_CHRG_DET BIT(3)
  46. struct omap_usb {
  47. struct usb_phy phy;
  48. struct phy_companion *comparator;
  49. void __iomem *pll_ctrl_base;
  50. void __iomem *phy_base;
  51. struct device *dev;
  52. struct device *control_dev;
  53. struct clk *wkupclk;
  54. struct clk *optclk;
  55. u8 flags;
  56. struct regmap *syscon_phy_power; /* ctrl. reg. acces */
  57. unsigned int power_reg; /* power reg. index within syscon */
  58. u32 mask;
  59. u32 power_on;
  60. u32 power_off;
  61. };
  62. #define phy_to_omapusb(x) container_of((x), struct omap_usb, phy)
  63. struct usb_phy_data {
  64. const char *label;
  65. u8 flags;
  66. u32 mask;
  67. u32 power_on;
  68. u32 power_off;
  69. };
  70. static inline u32 omap_usb_readl(void __iomem *addr, unsigned int offset)
  71. {
  72. return __raw_readl(addr + offset);
  73. }
  74. static inline void omap_usb_writel(void __iomem *addr, unsigned int offset,
  75. u32 data)
  76. {
  77. __raw_writel(data, addr + offset);
  78. }
  79. /**
  80. * omap_usb2_set_comparator() - links the comparator present in the system with this phy
  81. *
  82. * @comparator: the companion phy(comparator) for this phy
  83. *
  84. * The phy companion driver should call this API passing the phy_companion
  85. * filled with set_vbus and start_srp to be used by usb phy.
  86. *
  87. * For use by phy companion driver
  88. */
  89. int omap_usb2_set_comparator(struct phy_companion *comparator)
  90. {
  91. struct omap_usb *phy;
  92. struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2);
  93. if (IS_ERR(x))
  94. return -ENODEV;
  95. phy = phy_to_omapusb(x);
  96. phy->comparator = comparator;
  97. return 0;
  98. }
  99. EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
  100. static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled)
  101. {
  102. struct omap_usb *phy = phy_to_omapusb(otg->usb_phy);
  103. if (!phy->comparator || !phy->comparator->set_vbus)
  104. return -ENODEV;
  105. return phy->comparator->set_vbus(phy->comparator, enabled);
  106. }
  107. static int omap_usb_start_srp(struct usb_otg *otg)
  108. {
  109. struct omap_usb *phy = phy_to_omapusb(otg->usb_phy);
  110. if (!phy->comparator || !phy->comparator->start_srp)
  111. return -ENODEV;
  112. return phy->comparator->start_srp(phy->comparator);
  113. }
  114. static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
  115. {
  116. otg->host = host;
  117. if (!host)
  118. otg->state = OTG_STATE_UNDEFINED;
  119. return 0;
  120. }
  121. static int omap_usb_set_peripheral(struct usb_otg *otg,
  122. struct usb_gadget *gadget)
  123. {
  124. otg->gadget = gadget;
  125. if (!gadget)
  126. otg->state = OTG_STATE_UNDEFINED;
  127. return 0;
  128. }
  129. static int omap_usb_phy_power(struct omap_usb *phy, int on)
  130. {
  131. u32 val;
  132. int ret;
  133. if (!phy->syscon_phy_power) {
  134. omap_control_phy_power(phy->control_dev, on);
  135. return 0;
  136. }
  137. if (on)
  138. val = phy->power_on;
  139. else
  140. val = phy->power_off;
  141. ret = regmap_update_bits(phy->syscon_phy_power, phy->power_reg,
  142. phy->mask, val);
  143. return ret;
  144. }
  145. static int omap_usb_power_off(struct phy *x)
  146. {
  147. struct omap_usb *phy = phy_get_drvdata(x);
  148. return omap_usb_phy_power(phy, false);
  149. }
  150. static int omap_usb_power_on(struct phy *x)
  151. {
  152. struct omap_usb *phy = phy_get_drvdata(x);
  153. return omap_usb_phy_power(phy, true);
  154. }
  155. static int omap_usb2_disable_clocks(struct omap_usb *phy)
  156. {
  157. clk_disable_unprepare(phy->wkupclk);
  158. if (!IS_ERR(phy->optclk))
  159. clk_disable_unprepare(phy->optclk);
  160. return 0;
  161. }
  162. static int omap_usb2_enable_clocks(struct omap_usb *phy)
  163. {
  164. int ret;
  165. ret = clk_prepare_enable(phy->wkupclk);
  166. if (ret < 0) {
  167. dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
  168. goto err0;
  169. }
  170. if (!IS_ERR(phy->optclk)) {
  171. ret = clk_prepare_enable(phy->optclk);
  172. if (ret < 0) {
  173. dev_err(phy->dev, "Failed to enable optclk %d\n", ret);
  174. goto err1;
  175. }
  176. }
  177. return 0;
  178. err1:
  179. clk_disable_unprepare(phy->wkupclk);
  180. err0:
  181. return ret;
  182. }
  183. static int omap_usb_init(struct phy *x)
  184. {
  185. struct omap_usb *phy = phy_get_drvdata(x);
  186. u32 val;
  187. omap_usb2_enable_clocks(phy);
  188. if (phy->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
  189. /*
  190. *
  191. * Reduce the sensitivity of internal PHY by enabling the
  192. * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This
  193. * resolves issues with certain devices which can otherwise
  194. * be prone to false disconnects.
  195. *
  196. */
  197. val = omap_usb_readl(phy->phy_base, USB2PHY_ANA_CONFIG1);
  198. val |= USB2PHY_DISCON_BYP_LATCH;
  199. omap_usb_writel(phy->phy_base, USB2PHY_ANA_CONFIG1, val);
  200. }
  201. if (phy->flags & OMAP_USB2_DISABLE_CHRG_DET) {
  202. val = omap_usb_readl(phy->phy_base, USB2PHY_CHRG_DET);
  203. val |= USB2PHY_CHRG_DET_USE_CHG_DET_REG |
  204. USB2PHY_CHRG_DET_DIS_CHG_DET;
  205. omap_usb_writel(phy->phy_base, USB2PHY_CHRG_DET, val);
  206. }
  207. return 0;
  208. }
  209. static int omap_usb_exit(struct phy *x)
  210. {
  211. struct omap_usb *phy = phy_get_drvdata(x);
  212. return omap_usb2_disable_clocks(phy);
  213. }
  214. static const struct phy_ops ops = {
  215. .init = omap_usb_init,
  216. .exit = omap_usb_exit,
  217. .power_on = omap_usb_power_on,
  218. .power_off = omap_usb_power_off,
  219. .owner = THIS_MODULE,
  220. };
  221. static const struct usb_phy_data omap_usb2_data = {
  222. .label = "omap_usb2",
  223. .flags = OMAP_USB2_HAS_START_SRP | OMAP_USB2_HAS_SET_VBUS,
  224. .mask = OMAP_DEV_PHY_PD,
  225. .power_off = OMAP_DEV_PHY_PD,
  226. };
  227. static const struct usb_phy_data omap5_usb2_data = {
  228. .label = "omap5_usb2",
  229. .flags = 0,
  230. .mask = OMAP_DEV_PHY_PD,
  231. .power_off = OMAP_DEV_PHY_PD,
  232. };
  233. static const struct usb_phy_data dra7x_usb2_data = {
  234. .label = "dra7x_usb2",
  235. .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
  236. .mask = OMAP_DEV_PHY_PD,
  237. .power_off = OMAP_DEV_PHY_PD,
  238. };
  239. static const struct usb_phy_data dra7x_usb2_phy2_data = {
  240. .label = "dra7x_usb2_phy2",
  241. .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
  242. .mask = OMAP_USB2_PHY_PD,
  243. .power_off = OMAP_USB2_PHY_PD,
  244. };
  245. static const struct usb_phy_data am437x_usb2_data = {
  246. .label = "am437x_usb2",
  247. .flags = 0,
  248. .mask = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD |
  249. AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
  250. .power_on = AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
  251. .power_off = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD,
  252. };
  253. static const struct usb_phy_data am654_usb2_data = {
  254. .label = "am654_usb2",
  255. .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
  256. .mask = AM654_USB2_OTG_PD | AM654_USB2_VBUS_DET_EN |
  257. AM654_USB2_VBUSVALID_DET_EN,
  258. .power_on = AM654_USB2_VBUS_DET_EN | AM654_USB2_VBUSVALID_DET_EN,
  259. .power_off = AM654_USB2_OTG_PD,
  260. };
  261. static const struct of_device_id omap_usb2_id_table[] = {
  262. {
  263. .compatible = "ti,omap-usb2",
  264. .data = &omap_usb2_data,
  265. },
  266. {
  267. .compatible = "ti,omap5-usb2",
  268. .data = &omap5_usb2_data,
  269. },
  270. {
  271. .compatible = "ti,dra7x-usb2",
  272. .data = &dra7x_usb2_data,
  273. },
  274. {
  275. .compatible = "ti,dra7x-usb2-phy2",
  276. .data = &dra7x_usb2_phy2_data,
  277. },
  278. {
  279. .compatible = "ti,am437x-usb2",
  280. .data = &am437x_usb2_data,
  281. },
  282. {
  283. .compatible = "ti,am654-usb2",
  284. .data = &am654_usb2_data,
  285. },
  286. {},
  287. };
  288. MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
  289. static void omap_usb2_init_errata(struct omap_usb *phy)
  290. {
  291. static const struct soc_device_attribute am65x_sr10_soc_devices[] = {
  292. { .family = "AM65X", .revision = "SR1.0" },
  293. { /* sentinel */ }
  294. };
  295. /*
  296. * Errata i2075: USB2PHY: USB2PHY Charger Detect is Enabled by
  297. * Default Without VBUS Presence.
  298. *
  299. * AM654x SR1.0 has a silicon bug due to which D+ is pulled high after
  300. * POR, which could cause enumeration failure with some USB hubs.
  301. * Disabling the USB2_PHY Charger Detect function will put D+
  302. * into the normal state.
  303. */
  304. if (soc_device_match(am65x_sr10_soc_devices))
  305. phy->flags |= OMAP_USB2_DISABLE_CHRG_DET;
  306. }
  307. static void omap_usb2_put_device(void *_dev)
  308. {
  309. struct device *dev = _dev;
  310. put_device(dev);
  311. }
  312. static int omap_usb2_probe(struct platform_device *pdev)
  313. {
  314. struct omap_usb *phy;
  315. struct phy *generic_phy;
  316. struct phy_provider *phy_provider;
  317. struct usb_otg *otg;
  318. struct device_node *node = pdev->dev.of_node;
  319. struct device_node *control_node;
  320. struct platform_device *control_pdev;
  321. const struct usb_phy_data *phy_data;
  322. int ret;
  323. phy_data = device_get_match_data(&pdev->dev);
  324. if (!phy_data)
  325. return -EINVAL;
  326. phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
  327. if (!phy)
  328. return -ENOMEM;
  329. otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
  330. if (!otg)
  331. return -ENOMEM;
  332. phy->dev = &pdev->dev;
  333. phy->phy.dev = phy->dev;
  334. phy->phy.label = phy_data->label;
  335. phy->phy.otg = otg;
  336. phy->phy.type = USB_PHY_TYPE_USB2;
  337. phy->mask = phy_data->mask;
  338. phy->power_on = phy_data->power_on;
  339. phy->power_off = phy_data->power_off;
  340. phy->flags = phy_data->flags;
  341. omap_usb2_init_errata(phy);
  342. phy->phy_base = devm_platform_ioremap_resource(pdev, 0);
  343. if (IS_ERR(phy->phy_base))
  344. return PTR_ERR(phy->phy_base);
  345. phy->syscon_phy_power = syscon_regmap_lookup_by_phandle(node,
  346. "syscon-phy-power");
  347. if (IS_ERR(phy->syscon_phy_power)) {
  348. dev_dbg(&pdev->dev,
  349. "can't get syscon-phy-power, using control device\n");
  350. phy->syscon_phy_power = NULL;
  351. control_node = of_parse_phandle(node, "ctrl-module", 0);
  352. if (!control_node) {
  353. dev_err(&pdev->dev,
  354. "Failed to get control device phandle\n");
  355. return -EINVAL;
  356. }
  357. control_pdev = of_find_device_by_node(control_node);
  358. if (!control_pdev) {
  359. dev_err(&pdev->dev, "Failed to get control device\n");
  360. return -EINVAL;
  361. }
  362. phy->control_dev = &control_pdev->dev;
  363. ret = devm_add_action_or_reset(&pdev->dev, omap_usb2_put_device,
  364. phy->control_dev);
  365. if (ret)
  366. return ret;
  367. } else {
  368. if (of_property_read_u32_index(node,
  369. "syscon-phy-power", 1,
  370. &phy->power_reg)) {
  371. dev_err(&pdev->dev,
  372. "couldn't get power reg. offset\n");
  373. return -EINVAL;
  374. }
  375. }
  376. phy->wkupclk = devm_clk_get(phy->dev, "wkupclk");
  377. if (IS_ERR(phy->wkupclk)) {
  378. if (PTR_ERR(phy->wkupclk) == -EPROBE_DEFER)
  379. return -EPROBE_DEFER;
  380. dev_warn(&pdev->dev, "unable to get wkupclk %ld, trying old name\n",
  381. PTR_ERR(phy->wkupclk));
  382. phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
  383. if (IS_ERR(phy->wkupclk))
  384. return dev_err_probe(&pdev->dev, PTR_ERR(phy->wkupclk),
  385. "unable to get usb_phy_cm_clk32k\n");
  386. dev_warn(&pdev->dev,
  387. "found usb_phy_cm_clk32k, please fix DTS\n");
  388. }
  389. phy->optclk = devm_clk_get(phy->dev, "refclk");
  390. if (IS_ERR(phy->optclk)) {
  391. if (PTR_ERR(phy->optclk) == -EPROBE_DEFER)
  392. return -EPROBE_DEFER;
  393. dev_dbg(&pdev->dev, "unable to get refclk, trying old name\n");
  394. phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
  395. if (IS_ERR(phy->optclk)) {
  396. if (PTR_ERR(phy->optclk) != -EPROBE_DEFER) {
  397. dev_dbg(&pdev->dev,
  398. "unable to get usb_otg_ss_refclk960m\n");
  399. }
  400. } else {
  401. dev_warn(&pdev->dev,
  402. "found usb_otg_ss_refclk960m, please fix DTS\n");
  403. }
  404. }
  405. otg->set_host = omap_usb_set_host;
  406. otg->set_peripheral = omap_usb_set_peripheral;
  407. if (phy_data->flags & OMAP_USB2_HAS_SET_VBUS)
  408. otg->set_vbus = omap_usb_set_vbus;
  409. if (phy_data->flags & OMAP_USB2_HAS_START_SRP)
  410. otg->start_srp = omap_usb_start_srp;
  411. otg->usb_phy = &phy->phy;
  412. platform_set_drvdata(pdev, phy);
  413. pm_runtime_enable(phy->dev);
  414. generic_phy = devm_phy_create(phy->dev, NULL, &ops);
  415. if (IS_ERR(generic_phy)) {
  416. pm_runtime_disable(phy->dev);
  417. return PTR_ERR(generic_phy);
  418. }
  419. phy_set_drvdata(generic_phy, phy);
  420. omap_usb_power_off(generic_phy);
  421. phy_provider = devm_of_phy_provider_register(phy->dev,
  422. of_phy_simple_xlate);
  423. if (IS_ERR(phy_provider)) {
  424. pm_runtime_disable(phy->dev);
  425. return PTR_ERR(phy_provider);
  426. }
  427. usb_add_phy_dev(&phy->phy);
  428. return 0;
  429. }
  430. static void omap_usb2_remove(struct platform_device *pdev)
  431. {
  432. struct omap_usb *phy = platform_get_drvdata(pdev);
  433. usb_remove_phy(&phy->phy);
  434. pm_runtime_disable(phy->dev);
  435. }
  436. static struct platform_driver omap_usb2_driver = {
  437. .probe = omap_usb2_probe,
  438. .remove = omap_usb2_remove,
  439. .driver = {
  440. .name = "omap-usb2",
  441. .of_match_table = omap_usb2_id_table,
  442. },
  443. };
  444. module_platform_driver(omap_usb2_driver);
  445. MODULE_AUTHOR("Texas Instruments Inc.");
  446. MODULE_DESCRIPTION("OMAP USB2 phy driver");
  447. MODULE_LICENSE("GPL v2");