dwc3-st.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * dwc3-st.c Support for dwc3 platform devices on ST Microelectronics platforms
  4. *
  5. * This is a small driver for the dwc3 to provide the glue logic
  6. * to configure the controller. Tested on STi platforms.
  7. *
  8. * Copyright (C) 2014 Stmicroelectronics
  9. *
  10. * Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  11. * Contributors: Aymen Bouattay <aymen.bouattay@st.com>
  12. * Peter Griffin <peter.griffin@linaro.org>
  13. *
  14. * Inspired by dwc3-omap.c and dwc3-exynos.c.
  15. */
  16. #include <linux/cleanup.h>
  17. #include <linux/delay.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/ioport.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mfd/syscon.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/regmap.h>
  29. #include <linux/reset.h>
  30. #include <linux/pinctrl/consumer.h>
  31. #include <linux/usb/of.h>
  32. #include "core.h"
  33. #include "io.h"
  34. /* glue registers */
  35. #define CLKRST_CTRL 0x00
  36. #define AUX_CLK_EN BIT(0)
  37. #define SW_PIPEW_RESET_N BIT(4)
  38. #define EXT_CFG_RESET_N BIT(8)
  39. /*
  40. * 1'b0 : The host controller complies with the xHCI revision 0.96
  41. * 1'b1 : The host controller complies with the xHCI revision 1.0
  42. */
  43. #define XHCI_REVISION BIT(12)
  44. #define USB2_VBUS_MNGMNT_SEL1 0x2C
  45. /*
  46. * For all fields in USB2_VBUS_MNGMNT_SEL1
  47. * 2’b00 : Override value from Reg 0x30 is selected
  48. * 2’b01 : utmiotg_<signal_name> from usb3_top is selected
  49. * 2’b10 : pipew_<signal_name> from PIPEW instance is selected
  50. * 2’b11 : value is 1'b0
  51. */
  52. #define USB2_VBUS_REG30 0x0
  53. #define USB2_VBUS_UTMIOTG 0x1
  54. #define USB2_VBUS_PIPEW 0x2
  55. #define USB2_VBUS_ZERO 0x3
  56. #define SEL_OVERRIDE_VBUSVALID(n) (n << 0)
  57. #define SEL_OVERRIDE_POWERPRESENT(n) (n << 4)
  58. #define SEL_OVERRIDE_BVALID(n) (n << 8)
  59. /* Static DRD configuration */
  60. #define USB3_CONTROL_MASK 0xf77
  61. #define USB3_DEVICE_NOT_HOST BIT(0)
  62. #define USB3_FORCE_VBUSVALID BIT(1)
  63. #define USB3_DELAY_VBUSVALID BIT(2)
  64. #define USB3_SEL_FORCE_OPMODE BIT(4)
  65. #define USB3_FORCE_OPMODE(n) (n << 5)
  66. #define USB3_SEL_FORCE_DPPULLDOWN2 BIT(8)
  67. #define USB3_FORCE_DPPULLDOWN2 BIT(9)
  68. #define USB3_SEL_FORCE_DMPULLDOWN2 BIT(10)
  69. #define USB3_FORCE_DMPULLDOWN2 BIT(11)
  70. /**
  71. * struct st_dwc3 - dwc3-st driver private structure
  72. * @dev: device pointer
  73. * @glue_base: ioaddr for the glue registers
  74. * @regmap: regmap pointer for getting syscfg
  75. * @syscfg_reg_off: usb syscfg control offset
  76. * @dr_mode: drd static host/device config
  77. * @rstc_pwrdn: rest controller for powerdown signal
  78. * @rstc_rst: reset controller for softreset signal
  79. */
  80. struct st_dwc3 {
  81. struct device *dev;
  82. void __iomem *glue_base;
  83. struct regmap *regmap;
  84. int syscfg_reg_off;
  85. enum usb_dr_mode dr_mode;
  86. struct reset_control *rstc_pwrdn;
  87. struct reset_control *rstc_rst;
  88. };
  89. static inline u32 st_dwc3_readl(void __iomem *base, u32 offset)
  90. {
  91. return readl_relaxed(base + offset);
  92. }
  93. static inline void st_dwc3_writel(void __iomem *base, u32 offset, u32 value)
  94. {
  95. writel_relaxed(value, base + offset);
  96. }
  97. /**
  98. * st_dwc3_drd_init: program the port
  99. * @dwc3_data: driver private structure
  100. * Description: this function is to program the port as either host or device
  101. * according to the static configuration passed from devicetree.
  102. * OTG and dual role are not yet supported!
  103. */
  104. static int st_dwc3_drd_init(struct st_dwc3 *dwc3_data)
  105. {
  106. u32 val;
  107. int err;
  108. err = regmap_read(dwc3_data->regmap, dwc3_data->syscfg_reg_off, &val);
  109. if (err)
  110. return err;
  111. val &= USB3_CONTROL_MASK;
  112. switch (dwc3_data->dr_mode) {
  113. case USB_DR_MODE_PERIPHERAL:
  114. val &= ~(USB3_DELAY_VBUSVALID
  115. | USB3_SEL_FORCE_OPMODE | USB3_FORCE_OPMODE(0x3)
  116. | USB3_SEL_FORCE_DPPULLDOWN2 | USB3_FORCE_DPPULLDOWN2
  117. | USB3_SEL_FORCE_DMPULLDOWN2 | USB3_FORCE_DMPULLDOWN2);
  118. /*
  119. * USB3_PORT2_FORCE_VBUSVALID When '1' and when
  120. * USB3_PORT2_DEVICE_NOT_HOST = 1, forces VBUSVLDEXT2 input
  121. * of the pico PHY to 1.
  122. */
  123. val |= USB3_DEVICE_NOT_HOST | USB3_FORCE_VBUSVALID;
  124. break;
  125. case USB_DR_MODE_HOST:
  126. val &= ~(USB3_DEVICE_NOT_HOST | USB3_FORCE_VBUSVALID
  127. | USB3_SEL_FORCE_OPMODE | USB3_FORCE_OPMODE(0x3)
  128. | USB3_SEL_FORCE_DPPULLDOWN2 | USB3_FORCE_DPPULLDOWN2
  129. | USB3_SEL_FORCE_DMPULLDOWN2 | USB3_FORCE_DMPULLDOWN2);
  130. /*
  131. * USB3_DELAY_VBUSVALID is ANDed with USB_C_VBUSVALID. Thus,
  132. * when set to ‘0‘, it can delay the arrival of VBUSVALID
  133. * information to VBUSVLDEXT2 input of the pico PHY.
  134. * We don't want to do that so we set the bit to '1'.
  135. */
  136. val |= USB3_DELAY_VBUSVALID;
  137. break;
  138. default:
  139. dev_err(dwc3_data->dev, "Unsupported mode of operation %d\n",
  140. dwc3_data->dr_mode);
  141. return -EINVAL;
  142. }
  143. return regmap_write(dwc3_data->regmap, dwc3_data->syscfg_reg_off, val);
  144. }
  145. /**
  146. * st_dwc3_init: init the controller via glue logic
  147. * @dwc3_data: driver private structure
  148. */
  149. static void st_dwc3_init(struct st_dwc3 *dwc3_data)
  150. {
  151. u32 reg = st_dwc3_readl(dwc3_data->glue_base, CLKRST_CTRL);
  152. reg |= AUX_CLK_EN | EXT_CFG_RESET_N | XHCI_REVISION;
  153. reg &= ~SW_PIPEW_RESET_N;
  154. st_dwc3_writel(dwc3_data->glue_base, CLKRST_CTRL, reg);
  155. /* configure mux for vbus, powerpresent and bvalid signals */
  156. reg = st_dwc3_readl(dwc3_data->glue_base, USB2_VBUS_MNGMNT_SEL1);
  157. reg |= SEL_OVERRIDE_VBUSVALID(USB2_VBUS_UTMIOTG) |
  158. SEL_OVERRIDE_POWERPRESENT(USB2_VBUS_UTMIOTG) |
  159. SEL_OVERRIDE_BVALID(USB2_VBUS_UTMIOTG);
  160. st_dwc3_writel(dwc3_data->glue_base, USB2_VBUS_MNGMNT_SEL1, reg);
  161. reg = st_dwc3_readl(dwc3_data->glue_base, CLKRST_CTRL);
  162. reg |= SW_PIPEW_RESET_N;
  163. st_dwc3_writel(dwc3_data->glue_base, CLKRST_CTRL, reg);
  164. }
  165. static int st_dwc3_probe(struct platform_device *pdev)
  166. {
  167. struct st_dwc3 *dwc3_data;
  168. struct resource *res;
  169. struct device *dev = &pdev->dev;
  170. struct device_node *node = dev->of_node;
  171. struct platform_device *child_pdev;
  172. struct regmap *regmap;
  173. int ret;
  174. dwc3_data = devm_kzalloc(dev, sizeof(*dwc3_data), GFP_KERNEL);
  175. if (!dwc3_data)
  176. return -ENOMEM;
  177. dwc3_data->glue_base =
  178. devm_platform_ioremap_resource_byname(pdev, "reg-glue");
  179. if (IS_ERR(dwc3_data->glue_base))
  180. return PTR_ERR(dwc3_data->glue_base);
  181. regmap = syscon_regmap_lookup_by_phandle(node, "st,syscfg");
  182. if (IS_ERR(regmap))
  183. return PTR_ERR(regmap);
  184. dwc3_data->dev = dev;
  185. dwc3_data->regmap = regmap;
  186. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "syscfg-reg");
  187. if (!res)
  188. return -ENXIO;
  189. dwc3_data->syscfg_reg_off = res->start;
  190. dev_vdbg(dev, "glue-logic addr 0x%pK, syscfg-reg offset 0x%x\n",
  191. dwc3_data->glue_base, dwc3_data->syscfg_reg_off);
  192. struct device_node *child __free(device_node) = of_get_compatible_child(node,
  193. "snps,dwc3");
  194. if (!child) {
  195. dev_err(dev, "failed to find dwc3 core node\n");
  196. return -ENODEV;
  197. }
  198. dwc3_data->rstc_pwrdn =
  199. devm_reset_control_get_exclusive(dev, "powerdown");
  200. if (IS_ERR(dwc3_data->rstc_pwrdn))
  201. return dev_err_probe(dev, PTR_ERR(dwc3_data->rstc_pwrdn),
  202. "could not get power controller\n");
  203. /* Manage PowerDown */
  204. reset_control_deassert(dwc3_data->rstc_pwrdn);
  205. dwc3_data->rstc_rst =
  206. devm_reset_control_get_shared(dev, "softreset");
  207. if (IS_ERR(dwc3_data->rstc_rst)) {
  208. ret = dev_err_probe(dev, PTR_ERR(dwc3_data->rstc_rst),
  209. "could not get reset controller\n");
  210. goto undo_powerdown;
  211. }
  212. /* Manage SoftReset */
  213. reset_control_deassert(dwc3_data->rstc_rst);
  214. /* Allocate and initialize the core */
  215. ret = of_platform_populate(node, NULL, NULL, dev);
  216. if (ret) {
  217. dev_err(dev, "failed to add dwc3 core\n");
  218. goto undo_softreset;
  219. }
  220. child_pdev = of_find_device_by_node(child);
  221. if (!child_pdev) {
  222. dev_err(dev, "failed to find dwc3 core device\n");
  223. ret = -ENODEV;
  224. goto depopulate;
  225. }
  226. dwc3_data->dr_mode = usb_get_dr_mode(&child_pdev->dev);
  227. platform_device_put(child_pdev);
  228. /*
  229. * Configure the USB port as device or host according to the static
  230. * configuration passed from DT.
  231. * DRD is the only mode currently supported so this will be enhanced
  232. * as soon as OTG is available.
  233. */
  234. ret = st_dwc3_drd_init(dwc3_data);
  235. if (ret) {
  236. dev_err(dev, "drd initialisation failed\n");
  237. goto depopulate;
  238. }
  239. /* ST glue logic init */
  240. st_dwc3_init(dwc3_data);
  241. platform_set_drvdata(pdev, dwc3_data);
  242. return 0;
  243. depopulate:
  244. of_platform_depopulate(dev);
  245. undo_softreset:
  246. reset_control_assert(dwc3_data->rstc_rst);
  247. undo_powerdown:
  248. reset_control_assert(dwc3_data->rstc_pwrdn);
  249. return ret;
  250. }
  251. static void st_dwc3_remove(struct platform_device *pdev)
  252. {
  253. struct st_dwc3 *dwc3_data = platform_get_drvdata(pdev);
  254. of_platform_depopulate(&pdev->dev);
  255. reset_control_assert(dwc3_data->rstc_pwrdn);
  256. reset_control_assert(dwc3_data->rstc_rst);
  257. }
  258. #ifdef CONFIG_PM_SLEEP
  259. static int st_dwc3_suspend(struct device *dev)
  260. {
  261. struct st_dwc3 *dwc3_data = dev_get_drvdata(dev);
  262. reset_control_assert(dwc3_data->rstc_pwrdn);
  263. reset_control_assert(dwc3_data->rstc_rst);
  264. pinctrl_pm_select_sleep_state(dev);
  265. return 0;
  266. }
  267. static int st_dwc3_resume(struct device *dev)
  268. {
  269. struct st_dwc3 *dwc3_data = dev_get_drvdata(dev);
  270. int ret;
  271. pinctrl_pm_select_default_state(dev);
  272. reset_control_deassert(dwc3_data->rstc_pwrdn);
  273. reset_control_deassert(dwc3_data->rstc_rst);
  274. ret = st_dwc3_drd_init(dwc3_data);
  275. if (ret) {
  276. dev_err(dev, "drd initialisation failed\n");
  277. return ret;
  278. }
  279. /* ST glue logic init */
  280. st_dwc3_init(dwc3_data);
  281. return 0;
  282. }
  283. #endif /* CONFIG_PM_SLEEP */
  284. static SIMPLE_DEV_PM_OPS(st_dwc3_dev_pm_ops, st_dwc3_suspend, st_dwc3_resume);
  285. static const struct of_device_id st_dwc3_match[] = {
  286. { .compatible = "st,stih407-dwc3" },
  287. { /* sentinel */ },
  288. };
  289. MODULE_DEVICE_TABLE(of, st_dwc3_match);
  290. static struct platform_driver st_dwc3_driver = {
  291. .probe = st_dwc3_probe,
  292. .remove_new = st_dwc3_remove,
  293. .driver = {
  294. .name = "usb-st-dwc3",
  295. .of_match_table = st_dwc3_match,
  296. .pm = &st_dwc3_dev_pm_ops,
  297. },
  298. };
  299. module_platform_driver(st_dwc3_driver);
  300. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  301. MODULE_DESCRIPTION("DesignWare USB3 STi Glue Layer");
  302. MODULE_LICENSE("GPL v2");