treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / usb / cdns3 / cdns3-imx.c
blobaba988e71958674276371234f0302077511b9436
1 // SPDX-License-Identifier: GPL-2.0
2 /**
3 * cdns3-imx.c - NXP i.MX specific Glue layer for Cadence USB Controller
5 * Copyright (C) 2019 NXP
6 */
8 #include <linux/bits.h>
9 #include <linux/clk.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/io.h>
16 #include <linux/of_platform.h>
17 #include <linux/iopoll.h>
19 #define USB3_CORE_CTRL1 0x00
20 #define USB3_CORE_CTRL2 0x04
21 #define USB3_INT_REG 0x08
22 #define USB3_CORE_STATUS 0x0c
23 #define XHCI_DEBUG_LINK_ST 0x10
24 #define XHCI_DEBUG_BUS 0x14
25 #define USB3_SSPHY_CTRL1 0x40
26 #define USB3_SSPHY_CTRL2 0x44
27 #define USB3_SSPHY_STATUS 0x4c
28 #define USB2_PHY_CTRL1 0x50
29 #define USB2_PHY_CTRL2 0x54
30 #define USB2_PHY_STATUS 0x5c
32 /* Register bits definition */
34 /* USB3_CORE_CTRL1 */
35 #define SW_RESET_MASK (0x3f << 26)
36 #define PWR_SW_RESET BIT(31)
37 #define APB_SW_RESET BIT(30)
38 #define AXI_SW_RESET BIT(29)
39 #define RW_SW_RESET BIT(28)
40 #define PHY_SW_RESET BIT(27)
41 #define PHYAHB_SW_RESET BIT(26)
42 #define ALL_SW_RESET (PWR_SW_RESET | APB_SW_RESET | AXI_SW_RESET | \
43 RW_SW_RESET | PHY_SW_RESET | PHYAHB_SW_RESET)
44 #define OC_DISABLE BIT(9)
45 #define MDCTRL_CLK_SEL BIT(7)
46 #define MODE_STRAP_MASK (0x7)
47 #define DEV_MODE (1 << 2)
48 #define HOST_MODE (1 << 1)
49 #define OTG_MODE (1 << 0)
51 /* USB3_INT_REG */
52 #define CLK_125_REQ BIT(29)
53 #define LPM_CLK_REQ BIT(28)
54 #define DEVU3_WAEKUP_EN BIT(14)
55 #define OTG_WAKEUP_EN BIT(12)
56 #define DEV_INT_EN (3 << 8) /* DEV INT b9:8 */
57 #define HOST_INT1_EN (1 << 0) /* HOST INT b7:0 */
59 /* USB3_CORE_STATUS */
60 #define MDCTRL_CLK_STATUS BIT(15)
61 #define DEV_POWER_ON_READY BIT(13)
62 #define HOST_POWER_ON_READY BIT(12)
64 /* USB3_SSPHY_STATUS */
65 #define CLK_VALID_MASK (0x3f << 26)
66 #define CLK_VALID_COMPARE_BITS (0xf << 28)
67 #define PHY_REFCLK_REQ (1 << 0)
69 struct cdns_imx {
70 struct device *dev;
71 void __iomem *noncore;
72 struct clk_bulk_data *clks;
73 int num_clks;
76 static inline u32 cdns_imx_readl(struct cdns_imx *data, u32 offset)
78 return readl(data->noncore + offset);
81 static inline void cdns_imx_writel(struct cdns_imx *data, u32 offset, u32 value)
83 writel(value, data->noncore + offset);
86 static const struct clk_bulk_data imx_cdns3_core_clks[] = {
87 { .id = "usb3_lpm_clk" },
88 { .id = "usb3_bus_clk" },
89 { .id = "usb3_aclk" },
90 { .id = "usb3_ipg_clk" },
91 { .id = "usb3_core_pclk" },
94 static int cdns_imx_noncore_init(struct cdns_imx *data)
96 u32 value;
97 int ret;
98 struct device *dev = data->dev;
100 cdns_imx_writel(data, USB3_SSPHY_STATUS, CLK_VALID_MASK);
101 udelay(1);
102 ret = readl_poll_timeout(data->noncore + USB3_SSPHY_STATUS, value,
103 (value & CLK_VALID_COMPARE_BITS) == CLK_VALID_COMPARE_BITS,
104 10, 100000);
105 if (ret) {
106 dev_err(dev, "wait clkvld timeout\n");
107 return ret;
110 value = cdns_imx_readl(data, USB3_CORE_CTRL1);
111 value |= ALL_SW_RESET;
112 cdns_imx_writel(data, USB3_CORE_CTRL1, value);
113 udelay(1);
115 value = cdns_imx_readl(data, USB3_CORE_CTRL1);
116 value = (value & ~MODE_STRAP_MASK) | OTG_MODE | OC_DISABLE;
117 cdns_imx_writel(data, USB3_CORE_CTRL1, value);
119 value = cdns_imx_readl(data, USB3_INT_REG);
120 value |= HOST_INT1_EN | DEV_INT_EN;
121 cdns_imx_writel(data, USB3_INT_REG, value);
123 value = cdns_imx_readl(data, USB3_CORE_CTRL1);
124 value &= ~ALL_SW_RESET;
125 cdns_imx_writel(data, USB3_CORE_CTRL1, value);
126 return ret;
129 static int cdns_imx_probe(struct platform_device *pdev)
131 struct device *dev = &pdev->dev;
132 struct device_node *node = dev->of_node;
133 struct cdns_imx *data;
134 int ret;
136 if (!node)
137 return -ENODEV;
139 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
140 if (!data)
141 return -ENOMEM;
143 platform_set_drvdata(pdev, data);
144 data->dev = dev;
145 data->noncore = devm_platform_ioremap_resource(pdev, 0);
146 if (IS_ERR(data->noncore)) {
147 dev_err(dev, "can't map IOMEM resource\n");
148 return PTR_ERR(data->noncore);
151 data->num_clks = ARRAY_SIZE(imx_cdns3_core_clks);
152 data->clks = (struct clk_bulk_data *)imx_cdns3_core_clks;
153 ret = devm_clk_bulk_get(dev, data->num_clks, data->clks);
154 if (ret)
155 return ret;
157 ret = clk_bulk_prepare_enable(data->num_clks, data->clks);
158 if (ret)
159 return ret;
161 ret = cdns_imx_noncore_init(data);
162 if (ret)
163 goto err;
165 ret = of_platform_populate(node, NULL, NULL, dev);
166 if (ret) {
167 dev_err(dev, "failed to create children: %d\n", ret);
168 goto err;
171 return ret;
173 err:
174 clk_bulk_disable_unprepare(data->num_clks, data->clks);
175 return ret;
178 static int cdns_imx_remove_core(struct device *dev, void *data)
180 struct platform_device *pdev = to_platform_device(dev);
182 platform_device_unregister(pdev);
184 return 0;
187 static int cdns_imx_remove(struct platform_device *pdev)
189 struct device *dev = &pdev->dev;
191 device_for_each_child(dev, NULL, cdns_imx_remove_core);
192 platform_set_drvdata(pdev, NULL);
194 return 0;
197 static const struct of_device_id cdns_imx_of_match[] = {
198 { .compatible = "fsl,imx8qm-usb3", },
201 MODULE_DEVICE_TABLE(of, cdns_imx_of_match);
203 static struct platform_driver cdns_imx_driver = {
204 .probe = cdns_imx_probe,
205 .remove = cdns_imx_remove,
206 .driver = {
207 .name = "cdns3-imx",
208 .of_match_table = cdns_imx_of_match,
211 module_platform_driver(cdns_imx_driver);
213 MODULE_ALIAS("platform:cdns3-imx");
214 MODULE_AUTHOR("Peter Chen <peter.chen@nxp.com>");
215 MODULE_LICENSE("GPL v2");
216 MODULE_DESCRIPTION("Cadence USB3 i.MX Glue Layer");