phy-brcm-usb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * phy-brcm-usb.c - Broadcom USB Phy Driver
  3. *
  4. * Copyright (C) 2015-2017 Broadcom
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/phy/phy.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/soc/brcmstb/brcmstb.h>
  25. #include <dt-bindings/phy/phy.h>
  26. #include "phy-brcm-usb-init.h"
  27. static DEFINE_MUTEX(sysfs_lock);
  28. enum brcm_usb_phy_id {
  29. BRCM_USB_PHY_2_0 = 0,
  30. BRCM_USB_PHY_3_0,
  31. BRCM_USB_PHY_ID_MAX
  32. };
  33. struct value_to_name_map {
  34. int value;
  35. const char *name;
  36. };
  37. static struct value_to_name_map brcm_dr_mode_to_name[] = {
  38. { USB_CTLR_MODE_HOST, "host" },
  39. { USB_CTLR_MODE_DEVICE, "peripheral" },
  40. { USB_CTLR_MODE_DRD, "drd" },
  41. { USB_CTLR_MODE_TYPEC_PD, "typec-pd" }
  42. };
  43. static struct value_to_name_map brcm_dual_mode_to_name[] = {
  44. { 0, "host" },
  45. { 1, "device" },
  46. { 2, "auto" },
  47. };
  48. struct brcm_usb_phy {
  49. struct phy *phy;
  50. unsigned int id;
  51. bool inited;
  52. };
  53. struct brcm_usb_phy_data {
  54. struct brcm_usb_init_params ini;
  55. bool has_eohci;
  56. bool has_xhci;
  57. struct clk *usb_20_clk;
  58. struct clk *usb_30_clk;
  59. struct mutex mutex; /* serialize phy init */
  60. int init_count;
  61. struct brcm_usb_phy phys[BRCM_USB_PHY_ID_MAX];
  62. };
  63. static int brcm_usb_phy_init(struct phy *gphy)
  64. {
  65. struct brcm_usb_phy *phy = phy_get_drvdata(gphy);
  66. struct brcm_usb_phy_data *priv =
  67. container_of(phy, struct brcm_usb_phy_data, phys[phy->id]);
  68. /*
  69. * Use a lock to make sure a second caller waits until
  70. * the base phy is inited before using it.
  71. */
  72. mutex_lock(&priv->mutex);
  73. if (priv->init_count++ == 0) {
  74. clk_enable(priv->usb_20_clk);
  75. clk_enable(priv->usb_30_clk);
  76. brcm_usb_init_common(&priv->ini);
  77. }
  78. mutex_unlock(&priv->mutex);
  79. if (phy->id == BRCM_USB_PHY_2_0)
  80. brcm_usb_init_eohci(&priv->ini);
  81. else if (phy->id == BRCM_USB_PHY_3_0)
  82. brcm_usb_init_xhci(&priv->ini);
  83. phy->inited = true;
  84. dev_dbg(&gphy->dev, "INIT, id: %d, total: %d\n", phy->id,
  85. priv->init_count);
  86. return 0;
  87. }
  88. static int brcm_usb_phy_exit(struct phy *gphy)
  89. {
  90. struct brcm_usb_phy *phy = phy_get_drvdata(gphy);
  91. struct brcm_usb_phy_data *priv =
  92. container_of(phy, struct brcm_usb_phy_data, phys[phy->id]);
  93. dev_dbg(&gphy->dev, "EXIT\n");
  94. if (phy->id == BRCM_USB_PHY_2_0)
  95. brcm_usb_uninit_eohci(&priv->ini);
  96. if (phy->id == BRCM_USB_PHY_3_0)
  97. brcm_usb_uninit_xhci(&priv->ini);
  98. /* If both xhci and eohci are gone, reset everything else */
  99. mutex_lock(&priv->mutex);
  100. if (--priv->init_count == 0) {
  101. brcm_usb_uninit_common(&priv->ini);
  102. clk_disable(priv->usb_20_clk);
  103. clk_disable(priv->usb_30_clk);
  104. }
  105. mutex_unlock(&priv->mutex);
  106. phy->inited = false;
  107. return 0;
  108. }
  109. static struct phy_ops brcm_usb_phy_ops = {
  110. .init = brcm_usb_phy_init,
  111. .exit = brcm_usb_phy_exit,
  112. .owner = THIS_MODULE,
  113. };
  114. static struct phy *brcm_usb_phy_xlate(struct device *dev,
  115. struct of_phandle_args *args)
  116. {
  117. struct brcm_usb_phy_data *data = dev_get_drvdata(dev);
  118. /*
  119. * values 0 and 1 are for backward compatibility with
  120. * device tree nodes from older bootloaders.
  121. */
  122. switch (args->args[0]) {
  123. case 0:
  124. case PHY_TYPE_USB2:
  125. if (data->phys[BRCM_USB_PHY_2_0].phy)
  126. return data->phys[BRCM_USB_PHY_2_0].phy;
  127. dev_warn(dev, "Error, 2.0 Phy not found\n");
  128. break;
  129. case 1:
  130. case PHY_TYPE_USB3:
  131. if (data->phys[BRCM_USB_PHY_3_0].phy)
  132. return data->phys[BRCM_USB_PHY_3_0].phy;
  133. dev_warn(dev, "Error, 3.0 Phy not found\n");
  134. break;
  135. }
  136. return ERR_PTR(-ENODEV);
  137. }
  138. static int name_to_value(struct value_to_name_map *table, int count,
  139. const char *name, int *value)
  140. {
  141. int x;
  142. *value = 0;
  143. for (x = 0; x < count; x++) {
  144. if (sysfs_streq(name, table[x].name)) {
  145. *value = x;
  146. return 0;
  147. }
  148. }
  149. return -EINVAL;
  150. }
  151. static const char *value_to_name(struct value_to_name_map *table, int count,
  152. int value)
  153. {
  154. if (value >= count)
  155. return "unknown";
  156. return table[value].name;
  157. }
  158. static ssize_t dr_mode_show(struct device *dev,
  159. struct device_attribute *attr,
  160. char *buf)
  161. {
  162. struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
  163. return sprintf(buf, "%s\n",
  164. value_to_name(&brcm_dr_mode_to_name[0],
  165. ARRAY_SIZE(brcm_dr_mode_to_name),
  166. priv->ini.mode));
  167. }
  168. static DEVICE_ATTR_RO(dr_mode);
  169. static ssize_t dual_select_store(struct device *dev,
  170. struct device_attribute *attr,
  171. const char *buf, size_t len)
  172. {
  173. struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
  174. int value;
  175. int res;
  176. mutex_lock(&sysfs_lock);
  177. res = name_to_value(&brcm_dual_mode_to_name[0],
  178. ARRAY_SIZE(brcm_dual_mode_to_name), buf, &value);
  179. if (!res) {
  180. brcm_usb_init_set_dual_select(&priv->ini, value);
  181. res = len;
  182. }
  183. mutex_unlock(&sysfs_lock);
  184. return res;
  185. }
  186. static ssize_t dual_select_show(struct device *dev,
  187. struct device_attribute *attr,
  188. char *buf)
  189. {
  190. struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
  191. int value;
  192. mutex_lock(&sysfs_lock);
  193. value = brcm_usb_init_get_dual_select(&priv->ini);
  194. mutex_unlock(&sysfs_lock);
  195. return sprintf(buf, "%s\n",
  196. value_to_name(&brcm_dual_mode_to_name[0],
  197. ARRAY_SIZE(brcm_dual_mode_to_name),
  198. value));
  199. }
  200. static DEVICE_ATTR_RW(dual_select);
  201. static struct attribute *brcm_usb_phy_attrs[] = {
  202. &dev_attr_dr_mode.attr,
  203. &dev_attr_dual_select.attr,
  204. NULL
  205. };
  206. static const struct attribute_group brcm_usb_phy_group = {
  207. .attrs = brcm_usb_phy_attrs,
  208. };
  209. static int brcm_usb_phy_dvr_init(struct device *dev,
  210. struct brcm_usb_phy_data *priv,
  211. struct device_node *dn)
  212. {
  213. struct phy *gphy;
  214. int err;
  215. priv->usb_20_clk = of_clk_get_by_name(dn, "sw_usb");
  216. if (IS_ERR(priv->usb_20_clk)) {
  217. dev_info(dev, "Clock not found in Device Tree\n");
  218. priv->usb_20_clk = NULL;
  219. }
  220. err = clk_prepare_enable(priv->usb_20_clk);
  221. if (err)
  222. return err;
  223. if (priv->has_eohci) {
  224. gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
  225. if (IS_ERR(gphy)) {
  226. dev_err(dev, "failed to create EHCI/OHCI PHY\n");
  227. return PTR_ERR(gphy);
  228. }
  229. priv->phys[BRCM_USB_PHY_2_0].phy = gphy;
  230. priv->phys[BRCM_USB_PHY_2_0].id = BRCM_USB_PHY_2_0;
  231. phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_2_0]);
  232. }
  233. if (priv->has_xhci) {
  234. gphy = devm_phy_create(dev, NULL, &brcm_usb_phy_ops);
  235. if (IS_ERR(gphy)) {
  236. dev_err(dev, "failed to create XHCI PHY\n");
  237. return PTR_ERR(gphy);
  238. }
  239. priv->phys[BRCM_USB_PHY_3_0].phy = gphy;
  240. priv->phys[BRCM_USB_PHY_3_0].id = BRCM_USB_PHY_3_0;
  241. phy_set_drvdata(gphy, &priv->phys[BRCM_USB_PHY_3_0]);
  242. priv->usb_30_clk = of_clk_get_by_name(dn, "sw_usb3");
  243. if (IS_ERR(priv->usb_30_clk)) {
  244. dev_info(dev,
  245. "USB3.0 clock not found in Device Tree\n");
  246. priv->usb_30_clk = NULL;
  247. }
  248. err = clk_prepare_enable(priv->usb_30_clk);
  249. if (err)
  250. return err;
  251. }
  252. return 0;
  253. }
  254. static int brcm_usb_phy_probe(struct platform_device *pdev)
  255. {
  256. struct resource *res;
  257. struct device *dev = &pdev->dev;
  258. struct brcm_usb_phy_data *priv;
  259. struct phy_provider *phy_provider;
  260. struct device_node *dn = pdev->dev.of_node;
  261. int err;
  262. const char *mode;
  263. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  264. if (!priv)
  265. return -ENOMEM;
  266. platform_set_drvdata(pdev, priv);
  267. priv->ini.family_id = brcmstb_get_family_id();
  268. priv->ini.product_id = brcmstb_get_product_id();
  269. brcm_usb_set_family_map(&priv->ini);
  270. dev_dbg(dev, "Best mapping table is for %s\n",
  271. priv->ini.family_name);
  272. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  273. if (!res) {
  274. dev_err(dev, "can't get USB_CTRL base address\n");
  275. return -EINVAL;
  276. }
  277. priv->ini.ctrl_regs = devm_ioremap_resource(dev, res);
  278. if (IS_ERR(priv->ini.ctrl_regs)) {
  279. dev_err(dev, "can't map CTRL register space\n");
  280. return -EINVAL;
  281. }
  282. /* The XHCI EC registers are optional */
  283. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  284. if (res) {
  285. priv->ini.xhci_ec_regs =
  286. devm_ioremap_resource(dev, res);
  287. if (IS_ERR(priv->ini.xhci_ec_regs)) {
  288. dev_err(dev, "can't map XHCI EC register space\n");
  289. return -EINVAL;
  290. }
  291. }
  292. of_property_read_u32(dn, "brcm,ipp", &priv->ini.ipp);
  293. of_property_read_u32(dn, "brcm,ioc", &priv->ini.ioc);
  294. priv->ini.mode = USB_CTLR_MODE_HOST;
  295. err = of_property_read_string(dn, "dr_mode", &mode);
  296. if (err == 0) {
  297. name_to_value(&brcm_dr_mode_to_name[0],
  298. ARRAY_SIZE(brcm_dr_mode_to_name),
  299. mode, &priv->ini.mode);
  300. }
  301. if (of_property_read_bool(dn, "brcm,has-xhci"))
  302. priv->has_xhci = true;
  303. if (of_property_read_bool(dn, "brcm,has-eohci"))
  304. priv->has_eohci = true;
  305. err = brcm_usb_phy_dvr_init(dev, priv, dn);
  306. if (err)
  307. return err;
  308. mutex_init(&priv->mutex);
  309. /* make sure invert settings are correct */
  310. brcm_usb_init_ipp(&priv->ini);
  311. /*
  312. * Create sysfs entries for mode.
  313. * Remove "dual_select" attribute if not in dual mode
  314. */
  315. if (priv->ini.mode != USB_CTLR_MODE_DRD)
  316. brcm_usb_phy_attrs[1] = NULL;
  317. err = sysfs_create_group(&dev->kobj, &brcm_usb_phy_group);
  318. if (err)
  319. dev_warn(dev, "Error creating sysfs attributes\n");
  320. /* start with everything off */
  321. if (priv->has_xhci)
  322. brcm_usb_uninit_xhci(&priv->ini);
  323. if (priv->has_eohci)
  324. brcm_usb_uninit_eohci(&priv->ini);
  325. brcm_usb_uninit_common(&priv->ini);
  326. clk_disable(priv->usb_20_clk);
  327. clk_disable(priv->usb_30_clk);
  328. phy_provider = devm_of_phy_provider_register(dev, brcm_usb_phy_xlate);
  329. if (IS_ERR(phy_provider))
  330. return PTR_ERR(phy_provider);
  331. return 0;
  332. }
  333. static int brcm_usb_phy_remove(struct platform_device *pdev)
  334. {
  335. sysfs_remove_group(&pdev->dev.kobj, &brcm_usb_phy_group);
  336. return 0;
  337. }
  338. #ifdef CONFIG_PM_SLEEP
  339. static int brcm_usb_phy_suspend(struct device *dev)
  340. {
  341. struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
  342. if (priv->init_count) {
  343. clk_disable(priv->usb_20_clk);
  344. clk_disable(priv->usb_30_clk);
  345. }
  346. return 0;
  347. }
  348. static int brcm_usb_phy_resume(struct device *dev)
  349. {
  350. struct brcm_usb_phy_data *priv = dev_get_drvdata(dev);
  351. clk_enable(priv->usb_20_clk);
  352. clk_enable(priv->usb_30_clk);
  353. brcm_usb_init_ipp(&priv->ini);
  354. /*
  355. * Initialize anything that was previously initialized.
  356. * Uninitialize anything that wasn't previously initialized.
  357. */
  358. if (priv->init_count) {
  359. brcm_usb_init_common(&priv->ini);
  360. if (priv->phys[BRCM_USB_PHY_2_0].inited) {
  361. brcm_usb_init_eohci(&priv->ini);
  362. } else if (priv->has_eohci) {
  363. brcm_usb_uninit_eohci(&priv->ini);
  364. clk_disable(priv->usb_20_clk);
  365. }
  366. if (priv->phys[BRCM_USB_PHY_3_0].inited) {
  367. brcm_usb_init_xhci(&priv->ini);
  368. } else if (priv->has_xhci) {
  369. brcm_usb_uninit_xhci(&priv->ini);
  370. clk_disable(priv->usb_30_clk);
  371. }
  372. } else {
  373. if (priv->has_xhci)
  374. brcm_usb_uninit_xhci(&priv->ini);
  375. if (priv->has_eohci)
  376. brcm_usb_uninit_eohci(&priv->ini);
  377. brcm_usb_uninit_common(&priv->ini);
  378. clk_disable(priv->usb_20_clk);
  379. clk_disable(priv->usb_30_clk);
  380. }
  381. return 0;
  382. }
  383. #endif /* CONFIG_PM_SLEEP */
  384. static const struct dev_pm_ops brcm_usb_phy_pm_ops = {
  385. SET_LATE_SYSTEM_SLEEP_PM_OPS(brcm_usb_phy_suspend, brcm_usb_phy_resume)
  386. };
  387. static const struct of_device_id brcm_usb_dt_ids[] = {
  388. { .compatible = "brcm,brcmstb-usb-phy" },
  389. { /* sentinel */ }
  390. };
  391. MODULE_DEVICE_TABLE(of, brcm_usb_dt_ids);
  392. static struct platform_driver brcm_usb_driver = {
  393. .probe = brcm_usb_phy_probe,
  394. .remove = brcm_usb_phy_remove,
  395. .driver = {
  396. .name = "brcmstb-usb-phy",
  397. .owner = THIS_MODULE,
  398. .pm = &brcm_usb_phy_pm_ops,
  399. .of_match_table = brcm_usb_dt_ids,
  400. },
  401. };
  402. module_platform_driver(brcm_usb_driver);
  403. MODULE_ALIAS("platform:brcmstb-usb-phy");
  404. MODULE_AUTHOR("Al Cooper <acooper@broadcom.com>");
  405. MODULE_DESCRIPTION("BRCM USB PHY driver");
  406. MODULE_LICENSE("GPL v2");