dwc3-st.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. break;
  122. case USB_DR_MODE_HOST:
  123. val &= ~(USB3_DEVICE_NOT_HOST | USB3_FORCE_VBUSVALID
  124. | USB3_SEL_FORCE_OPMODE | USB3_FORCE_OPMODE(0x3)
  125. | USB3_SEL_FORCE_DPPULLDOWN2 | USB3_FORCE_DPPULLDOWN2
  126. | USB3_SEL_FORCE_DMPULLDOWN2 | USB3_FORCE_DMPULLDOWN2);
  127. /*
  128. * USB3_DELAY_VBUSVALID is ANDed with USB_C_VBUSVALID. Thus,
  129. * when set to ‘0‘, it can delay the arrival of VBUSVALID
  130. * information to VBUSVLDEXT2 input of the pico PHY.
  131. * We don't want to do that so we set the bit to '1'.
  132. */
  133. val |= USB3_DELAY_VBUSVALID;
  134. break;
  135. default:
  136. dev_err(dwc3_data->dev, "Unsupported mode of operation %d\n",
  137. dwc3_data->dr_mode);
  138. return -EINVAL;
  139. }
  140. return regmap_write(dwc3_data->regmap, dwc3_data->syscfg_reg_off, val);
  141. }
  142. /**
  143. * st_dwc3_init: init the controller via glue logic
  144. * @dwc3_data: driver private structure
  145. */
  146. static void st_dwc3_init(struct st_dwc3 *dwc3_data)
  147. {
  148. u32 reg = st_dwc3_readl(dwc3_data->glue_base, CLKRST_CTRL);
  149. reg |= AUX_CLK_EN | EXT_CFG_RESET_N | XHCI_REVISION;
  150. reg &= ~SW_PIPEW_RESET_N;
  151. st_dwc3_writel(dwc3_data->glue_base, CLKRST_CTRL, reg);
  152. /* configure mux for vbus, powerpresent and bvalid signals */
  153. reg = st_dwc3_readl(dwc3_data->glue_base, USB2_VBUS_MNGMNT_SEL1);
  154. reg |= SEL_OVERRIDE_VBUSVALID(USB2_VBUS_UTMIOTG) |
  155. SEL_OVERRIDE_POWERPRESENT(USB2_VBUS_UTMIOTG) |
  156. SEL_OVERRIDE_BVALID(USB2_VBUS_UTMIOTG);
  157. st_dwc3_writel(dwc3_data->glue_base, USB2_VBUS_MNGMNT_SEL1, reg);
  158. reg = st_dwc3_readl(dwc3_data->glue_base, CLKRST_CTRL);
  159. reg |= SW_PIPEW_RESET_N;
  160. st_dwc3_writel(dwc3_data->glue_base, CLKRST_CTRL, reg);
  161. }
  162. static int st_dwc3_probe(struct platform_device *pdev)
  163. {
  164. struct st_dwc3 *dwc3_data;
  165. struct resource *res;
  166. struct device *dev = &pdev->dev;
  167. struct device_node *node = dev->of_node, *child;
  168. struct platform_device *child_pdev;
  169. struct regmap *regmap;
  170. int ret;
  171. dwc3_data = devm_kzalloc(dev, sizeof(*dwc3_data), GFP_KERNEL);
  172. if (!dwc3_data)
  173. return -ENOMEM;
  174. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "reg-glue");
  175. dwc3_data->glue_base = devm_ioremap_resource(dev, res);
  176. if (IS_ERR(dwc3_data->glue_base))
  177. return PTR_ERR(dwc3_data->glue_base);
  178. regmap = syscon_regmap_lookup_by_phandle(node, "st,syscfg");
  179. if (IS_ERR(regmap))
  180. return PTR_ERR(regmap);
  181. dma_set_coherent_mask(dev, dev->coherent_dma_mask);
  182. dwc3_data->dev = dev;
  183. dwc3_data->regmap = regmap;
  184. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "syscfg-reg");
  185. if (!res) {
  186. ret = -ENXIO;
  187. goto undo_platform_dev_alloc;
  188. }
  189. dwc3_data->syscfg_reg_off = res->start;
  190. dev_vdbg(&pdev->dev, "glue-logic addr 0x%pK, syscfg-reg offset 0x%x\n",
  191. dwc3_data->glue_base, dwc3_data->syscfg_reg_off);
  192. dwc3_data->rstc_pwrdn = devm_reset_control_get(dev, "powerdown");
  193. if (IS_ERR(dwc3_data->rstc_pwrdn)) {
  194. dev_err(&pdev->dev, "could not get power controller\n");
  195. ret = PTR_ERR(dwc3_data->rstc_pwrdn);
  196. goto undo_platform_dev_alloc;
  197. }
  198. /* Manage PowerDown */
  199. reset_control_deassert(dwc3_data->rstc_pwrdn);
  200. dwc3_data->rstc_rst = devm_reset_control_get(dev, "softreset");
  201. if (IS_ERR(dwc3_data->rstc_rst)) {
  202. dev_err(&pdev->dev, "could not get reset controller\n");
  203. ret = PTR_ERR(dwc3_data->rstc_rst);
  204. goto undo_powerdown;
  205. }
  206. /* Manage SoftReset */
  207. reset_control_deassert(dwc3_data->rstc_rst);
  208. child = of_get_child_by_name(node, "dwc3");
  209. if (!child) {
  210. dev_err(&pdev->dev, "failed to find dwc3 core node\n");
  211. ret = -ENODEV;
  212. goto undo_softreset;
  213. }
  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 undo_softreset;
  225. }
  226. dwc3_data->dr_mode = usb_get_dr_mode(&child_pdev->dev);
  227. /*
  228. * Configure the USB port as device or host according to the static
  229. * configuration passed from DT.
  230. * DRD is the only mode currently supported so this will be enhanced
  231. * as soon as OTG is available.
  232. */
  233. ret = st_dwc3_drd_init(dwc3_data);
  234. if (ret) {
  235. dev_err(dev, "drd initialisation failed\n");
  236. goto undo_softreset;
  237. }
  238. /* ST glue logic init */
  239. st_dwc3_init(dwc3_data);
  240. platform_set_drvdata(pdev, dwc3_data);
  241. return 0;
  242. undo_softreset:
  243. reset_control_assert(dwc3_data->rstc_rst);
  244. undo_powerdown:
  245. reset_control_assert(dwc3_data->rstc_pwrdn);
  246. undo_platform_dev_alloc:
  247. platform_device_put(pdev);
  248. return ret;
  249. }
  250. static int st_dwc3_remove(struct platform_device *pdev)
  251. {
  252. struct st_dwc3 *dwc3_data = platform_get_drvdata(pdev);
  253. of_platform_depopulate(&pdev->dev);
  254. reset_control_assert(dwc3_data->rstc_pwrdn);
  255. reset_control_assert(dwc3_data->rstc_rst);
  256. return 0;
  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 = 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");