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>
25 #define DRIVER_NAME "mxs_phy"
27 #define HW_USBPHY_PWD 0x00
28 #define HW_USBPHY_CTRL 0x30
29 #define HW_USBPHY_CTRL_SET 0x34
30 #define HW_USBPHY_CTRL_CLR 0x38
32 #define BM_USBPHY_CTRL_SFTRST BIT(31)
33 #define BM_USBPHY_CTRL_CLKGATE BIT(30)
34 #define BM_USBPHY_CTRL_ENUTMILEVEL3 BIT(15)
35 #define BM_USBPHY_CTRL_ENUTMILEVEL2 BIT(14)
36 #define BM_USBPHY_CTRL_ENHOSTDISCONDETECT BIT(1)
38 #define to_mxs_phy(p) container_of((p), struct mxs_phy, phy)
40 /* Do disconnection between PHY and controller without vbus */
41 #define MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS BIT(0)
44 * The PHY will be in messy if there is a wakeup after putting
45 * bus to suspend (set portsc.suspendM) but before setting PHY to low
46 * power mode (set portsc.phcd).
48 #define MXS_PHY_ABNORMAL_IN_SUSPEND BIT(1)
51 * The SOF sends too fast after resuming, it will cause disconnection
52 * between host and high speed device.
54 #define MXS_PHY_SENDING_SOF_TOO_FAST BIT(2)
60 static const struct mxs_phy_data imx23_phy_data = {
61 .flags = MXS_PHY_ABNORMAL_IN_SUSPEND | MXS_PHY_SENDING_SOF_TOO_FAST,
64 static const struct mxs_phy_data imx6q_phy_data = {
65 .flags = MXS_PHY_SENDING_SOF_TOO_FAST |
66 MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS,
69 static const struct mxs_phy_data imx6sl_phy_data = {
70 .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS,
73 static const struct of_device_id mxs_phy_dt_ids[] = {
74 { .compatible = "fsl,imx6sl-usbphy", .data = &imx6sl_phy_data, },
75 { .compatible = "fsl,imx6q-usbphy", .data = &imx6q_phy_data, },
76 { .compatible = "fsl,imx23-usbphy", .data = &imx23_phy_data, },
79 MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids);
84 const struct mxs_phy_data *data;
87 static int mxs_phy_hw_init(struct mxs_phy *mxs_phy)
90 void __iomem *base = mxs_phy->phy.io_priv;
92 ret = stmp_reset_block(base + HW_USBPHY_CTRL);
96 /* Power up the PHY */
97 writel(0, base + HW_USBPHY_PWD);
99 /* enable FS/LS device */
100 writel(BM_USBPHY_CTRL_ENUTMILEVEL2 |
101 BM_USBPHY_CTRL_ENUTMILEVEL3,
102 base + HW_USBPHY_CTRL_SET);
107 static int mxs_phy_init(struct usb_phy *phy)
110 struct mxs_phy *mxs_phy = to_mxs_phy(phy);
112 ret = clk_prepare_enable(mxs_phy->clk);
116 return mxs_phy_hw_init(mxs_phy);
119 static void mxs_phy_shutdown(struct usb_phy *phy)
121 struct mxs_phy *mxs_phy = to_mxs_phy(phy);
123 writel(BM_USBPHY_CTRL_CLKGATE,
124 phy->io_priv + HW_USBPHY_CTRL_SET);
126 clk_disable_unprepare(mxs_phy->clk);
129 static int mxs_phy_suspend(struct usb_phy *x, int suspend)
132 struct mxs_phy *mxs_phy = to_mxs_phy(x);
135 writel(0xffffffff, x->io_priv + HW_USBPHY_PWD);
136 writel(BM_USBPHY_CTRL_CLKGATE,
137 x->io_priv + HW_USBPHY_CTRL_SET);
138 clk_disable_unprepare(mxs_phy->clk);
140 ret = clk_prepare_enable(mxs_phy->clk);
143 writel(BM_USBPHY_CTRL_CLKGATE,
144 x->io_priv + HW_USBPHY_CTRL_CLR);
145 writel(0, x->io_priv + HW_USBPHY_PWD);
151 static int mxs_phy_on_connect(struct usb_phy *phy,
152 enum usb_device_speed speed)
154 dev_dbg(phy->dev, "%s speed device has connected\n",
155 (speed == USB_SPEED_HIGH) ? "high" : "non-high");
157 if (speed == USB_SPEED_HIGH)
158 writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
159 phy->io_priv + HW_USBPHY_CTRL_SET);
164 static int mxs_phy_on_disconnect(struct usb_phy *phy,
165 enum usb_device_speed speed)
167 dev_dbg(phy->dev, "%s speed device has disconnected\n",
168 (speed == USB_SPEED_HIGH) ? "high" : "non-high");
170 if (speed == USB_SPEED_HIGH)
171 writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
172 phy->io_priv + HW_USBPHY_CTRL_CLR);
177 static int mxs_phy_probe(struct platform_device *pdev)
179 struct resource *res;
182 struct mxs_phy *mxs_phy;
184 const struct of_device_id *of_id =
185 of_match_device(mxs_phy_dt_ids, &pdev->dev);
187 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
188 base = devm_ioremap_resource(&pdev->dev, res);
190 return PTR_ERR(base);
192 clk = devm_clk_get(&pdev->dev, NULL);
195 "can't get the clock, err=%ld", PTR_ERR(clk));
199 mxs_phy = devm_kzalloc(&pdev->dev, sizeof(*mxs_phy), GFP_KERNEL);
201 dev_err(&pdev->dev, "Failed to allocate USB PHY structure!\n");
205 mxs_phy->phy.io_priv = base;
206 mxs_phy->phy.dev = &pdev->dev;
207 mxs_phy->phy.label = DRIVER_NAME;
208 mxs_phy->phy.init = mxs_phy_init;
209 mxs_phy->phy.shutdown = mxs_phy_shutdown;
210 mxs_phy->phy.set_suspend = mxs_phy_suspend;
211 mxs_phy->phy.notify_connect = mxs_phy_on_connect;
212 mxs_phy->phy.notify_disconnect = mxs_phy_on_disconnect;
213 mxs_phy->phy.type = USB_PHY_TYPE_USB2;
216 mxs_phy->data = of_id->data;
218 platform_set_drvdata(pdev, mxs_phy);
220 ret = usb_add_phy_dev(&mxs_phy->phy);
227 static int mxs_phy_remove(struct platform_device *pdev)
229 struct mxs_phy *mxs_phy = platform_get_drvdata(pdev);
231 usb_remove_phy(&mxs_phy->phy);
236 static struct platform_driver mxs_phy_driver = {
237 .probe = mxs_phy_probe,
238 .remove = mxs_phy_remove,
241 .owner = THIS_MODULE,
242 .of_match_table = mxs_phy_dt_ids,
246 static int __init mxs_phy_module_init(void)
248 return platform_driver_register(&mxs_phy_driver);
250 postcore_initcall(mxs_phy_module_init);
252 static void __exit mxs_phy_module_exit(void)
254 platform_driver_unregister(&mxs_phy_driver);
256 module_exit(mxs_phy_module_exit);
258 MODULE_ALIAS("platform:mxs-usb-phy");
259 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
260 MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");
261 MODULE_DESCRIPTION("Freescale MXS USB PHY driver");
262 MODULE_LICENSE("GPL");