2 * Copyright 2012-2013 Freescale Semiconductor, Inc.
3 * Copyright (C) 2012 Marek Vasut <marex@denx.de>
4 * on behalf of DENX Software Engineering GmbH
6 * The code contained herein is licensed under the GNU General Public
7 * License. You may obtain a copy of the GNU General Public License
8 * Version 2 or later at the following locations:
10 * http://www.opensource.org/licenses/gpl-license.html
11 * http://www.gnu.org/copyleft/gpl.html
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/platform_device.h>
17 #include <linux/clk.h>
18 #include <linux/usb/otg.h>
19 #include <linux/stmp_device.h>
20 #include <linux/delay.h>
21 #include <linux/err.h>
23 #include <linux/of_device.h>
24 #include <linux/regmap.h>
25 #include <linux/mfd/syscon.h>
27 #define DRIVER_NAME "mxs_phy"
29 #define HW_USBPHY_PWD 0x00
30 #define HW_USBPHY_CTRL 0x30
31 #define HW_USBPHY_CTRL_SET 0x34
32 #define HW_USBPHY_CTRL_CLR 0x38
34 #define HW_USBPHY_IP 0x90
35 #define HW_USBPHY_IP_SET 0x94
36 #define HW_USBPHY_IP_CLR 0x98
38 #define BM_USBPHY_CTRL_SFTRST BIT(31)
39 #define BM_USBPHY_CTRL_CLKGATE BIT(30)
40 #define BM_USBPHY_CTRL_ENAUTOSET_USBCLKS BIT(26)
41 #define BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE BIT(25)
42 #define BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD BIT(20)
43 #define BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE BIT(19)
44 #define BM_USBPHY_CTRL_ENAUTO_PWRON_PLL BIT(18)
45 #define BM_USBPHY_CTRL_ENUTMILEVEL3 BIT(15)
46 #define BM_USBPHY_CTRL_ENUTMILEVEL2 BIT(14)
47 #define BM_USBPHY_CTRL_ENHOSTDISCONDETECT BIT(1)
49 #define BM_USBPHY_IP_FIX (BIT(17) | BIT(18))
51 #define to_mxs_phy(p) container_of((p), struct mxs_phy, phy)
53 /* Do disconnection between PHY and controller without vbus */
54 #define MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS BIT(0)
57 * The PHY will be in messy if there is a wakeup after putting
58 * bus to suspend (set portsc.suspendM) but before setting PHY to low
59 * power mode (set portsc.phcd).
61 #define MXS_PHY_ABNORMAL_IN_SUSPEND BIT(1)
64 * The SOF sends too fast after resuming, it will cause disconnection
65 * between host and high speed device.
67 #define MXS_PHY_SENDING_SOF_TOO_FAST BIT(2)
70 * IC has bug fixes logic, they include
71 * MXS_PHY_ABNORMAL_IN_SUSPEND and MXS_PHY_SENDING_SOF_TOO_FAST
72 * which are described at above flags, the RTL will handle it
73 * according to different versions.
75 #define MXS_PHY_NEED_IP_FIX BIT(3)
81 static const struct mxs_phy_data imx23_phy_data = {
82 .flags = MXS_PHY_ABNORMAL_IN_SUSPEND | MXS_PHY_SENDING_SOF_TOO_FAST,
85 static const struct mxs_phy_data imx6q_phy_data = {
86 .flags = MXS_PHY_SENDING_SOF_TOO_FAST |
87 MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
91 static const struct mxs_phy_data imx6sl_phy_data = {
92 .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
96 static const struct of_device_id mxs_phy_dt_ids[] = {
97 { .compatible = "fsl,imx6sl-usbphy", .data = &imx6sl_phy_data, },
98 { .compatible = "fsl,imx6q-usbphy", .data = &imx6q_phy_data, },
99 { .compatible = "fsl,imx23-usbphy", .data = &imx23_phy_data, },
102 MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids);
107 const struct mxs_phy_data *data;
108 struct regmap *regmap_anatop;
112 static int mxs_phy_hw_init(struct mxs_phy *mxs_phy)
115 void __iomem *base = mxs_phy->phy.io_priv;
117 ret = stmp_reset_block(base + HW_USBPHY_CTRL);
121 /* Power up the PHY */
122 writel(0, base + HW_USBPHY_PWD);
125 * USB PHY Ctrl Setting
126 * - Auto clock/power on
127 * - Enable full/low speed support
129 writel(BM_USBPHY_CTRL_ENAUTOSET_USBCLKS |
130 BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE |
131 BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD |
132 BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE |
133 BM_USBPHY_CTRL_ENAUTO_PWRON_PLL |
134 BM_USBPHY_CTRL_ENUTMILEVEL2 |
135 BM_USBPHY_CTRL_ENUTMILEVEL3,
136 base + HW_USBPHY_CTRL_SET);
138 if (mxs_phy->data->flags & MXS_PHY_NEED_IP_FIX)
139 writel(BM_USBPHY_IP_FIX, base + HW_USBPHY_IP_SET);
144 static int mxs_phy_init(struct usb_phy *phy)
147 struct mxs_phy *mxs_phy = to_mxs_phy(phy);
149 ret = clk_prepare_enable(mxs_phy->clk);
153 return mxs_phy_hw_init(mxs_phy);
156 static void mxs_phy_shutdown(struct usb_phy *phy)
158 struct mxs_phy *mxs_phy = to_mxs_phy(phy);
160 writel(BM_USBPHY_CTRL_CLKGATE,
161 phy->io_priv + HW_USBPHY_CTRL_SET);
163 clk_disable_unprepare(mxs_phy->clk);
166 static int mxs_phy_suspend(struct usb_phy *x, int suspend)
169 struct mxs_phy *mxs_phy = to_mxs_phy(x);
172 writel(0xffffffff, x->io_priv + HW_USBPHY_PWD);
173 writel(BM_USBPHY_CTRL_CLKGATE,
174 x->io_priv + HW_USBPHY_CTRL_SET);
175 clk_disable_unprepare(mxs_phy->clk);
177 ret = clk_prepare_enable(mxs_phy->clk);
180 writel(BM_USBPHY_CTRL_CLKGATE,
181 x->io_priv + HW_USBPHY_CTRL_CLR);
182 writel(0, x->io_priv + HW_USBPHY_PWD);
188 static int mxs_phy_on_connect(struct usb_phy *phy,
189 enum usb_device_speed speed)
191 dev_dbg(phy->dev, "%s device has connected\n",
192 (speed == USB_SPEED_HIGH) ? "HS" : "FS/LS");
194 if (speed == USB_SPEED_HIGH)
195 writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
196 phy->io_priv + HW_USBPHY_CTRL_SET);
201 static int mxs_phy_on_disconnect(struct usb_phy *phy,
202 enum usb_device_speed speed)
204 dev_dbg(phy->dev, "%s device has disconnected\n",
205 (speed == USB_SPEED_HIGH) ? "HS" : "FS/LS");
207 if (speed == USB_SPEED_HIGH)
208 writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
209 phy->io_priv + HW_USBPHY_CTRL_CLR);
214 static int mxs_phy_probe(struct platform_device *pdev)
216 struct resource *res;
219 struct mxs_phy *mxs_phy;
221 const struct of_device_id *of_id =
222 of_match_device(mxs_phy_dt_ids, &pdev->dev);
223 struct device_node *np = pdev->dev.of_node;
225 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
226 base = devm_ioremap_resource(&pdev->dev, res);
228 return PTR_ERR(base);
230 clk = devm_clk_get(&pdev->dev, NULL);
233 "can't get the clock, err=%ld", PTR_ERR(clk));
237 mxs_phy = devm_kzalloc(&pdev->dev, sizeof(*mxs_phy), GFP_KERNEL);
239 dev_err(&pdev->dev, "Failed to allocate USB PHY structure!\n");
243 /* Some SoCs don't have anatop registers */
244 if (of_get_property(np, "fsl,anatop", NULL)) {
245 mxs_phy->regmap_anatop = syscon_regmap_lookup_by_phandle
247 if (IS_ERR(mxs_phy->regmap_anatop)) {
249 "failed to find regmap for anatop\n");
250 return PTR_ERR(mxs_phy->regmap_anatop);
254 ret = of_alias_get_id(np, "usbphy");
256 dev_dbg(&pdev->dev, "failed to get alias id, errno %d\n", ret);
257 mxs_phy->port_id = ret;
259 mxs_phy->phy.io_priv = base;
260 mxs_phy->phy.dev = &pdev->dev;
261 mxs_phy->phy.label = DRIVER_NAME;
262 mxs_phy->phy.init = mxs_phy_init;
263 mxs_phy->phy.shutdown = mxs_phy_shutdown;
264 mxs_phy->phy.set_suspend = mxs_phy_suspend;
265 mxs_phy->phy.notify_connect = mxs_phy_on_connect;
266 mxs_phy->phy.notify_disconnect = mxs_phy_on_disconnect;
267 mxs_phy->phy.type = USB_PHY_TYPE_USB2;
270 mxs_phy->data = of_id->data;
272 platform_set_drvdata(pdev, mxs_phy);
274 ret = usb_add_phy_dev(&mxs_phy->phy);
281 static int mxs_phy_remove(struct platform_device *pdev)
283 struct mxs_phy *mxs_phy = platform_get_drvdata(pdev);
285 usb_remove_phy(&mxs_phy->phy);
290 static struct platform_driver mxs_phy_driver = {
291 .probe = mxs_phy_probe,
292 .remove = mxs_phy_remove,
295 .owner = THIS_MODULE,
296 .of_match_table = mxs_phy_dt_ids,
300 static int __init mxs_phy_module_init(void)
302 return platform_driver_register(&mxs_phy_driver);
304 postcore_initcall(mxs_phy_module_init);
306 static void __exit mxs_phy_module_exit(void)
308 platform_driver_unregister(&mxs_phy_driver);
310 module_exit(mxs_phy_module_exit);
312 MODULE_ALIAS("platform:mxs-usb-phy");
313 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
314 MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");
315 MODULE_DESCRIPTION("Freescale MXS USB PHY driver");
316 MODULE_LICENSE("GPL");