dwc3-st.c 9.8 KB

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