phy-omap-usb2.c 13 KB

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