dwc3-st.c 10 KB

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