blob: cf58d8ec9901cd4c5f20348853a20eeb1dfa1210 [file] [log] [blame]
Richard Zhaob3d99682012-07-07 22:56:47 +08001/*
Peter Chen24007802014-02-24 10:20:54 +08002 * Copyright 2012-2013 Freescale Semiconductor, Inc.
Richard Zhaob3d99682012-07-07 22:56:47 +08003 * Copyright (C) 2012 Marek Vasut <marex@denx.de>
4 * on behalf of DENX Software Engineering GmbH
5 *
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:
9 *
10 * http://www.opensource.org/licenses/gpl-license.html
11 * http://www.gnu.org/copyleft/gpl.html
12 */
13
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>
22#include <linux/io.h>
Peter Chen24007802014-02-24 10:20:54 +080023#include <linux/of_device.h>
Richard Zhaob3d99682012-07-07 22:56:47 +080024
25#define DRIVER_NAME "mxs_phy"
26
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
31
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)
37
Peter Chen24007802014-02-24 10:20:54 +080038#define to_mxs_phy(p) container_of((p), struct mxs_phy, phy)
39
40/* Do disconnection between PHY and controller without vbus */
41#define MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS BIT(0)
42
43/*
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).
47 */
48#define MXS_PHY_ABNORMAL_IN_SUSPEND BIT(1)
49
50/*
51 * The SOF sends too fast after resuming, it will cause disconnection
52 * between host and high speed device.
53 */
54#define MXS_PHY_SENDING_SOF_TOO_FAST BIT(2)
55
56struct mxs_phy_data {
57 unsigned int flags;
58};
59
60static const struct mxs_phy_data imx23_phy_data = {
61 .flags = MXS_PHY_ABNORMAL_IN_SUSPEND | MXS_PHY_SENDING_SOF_TOO_FAST,
62};
63
64static const struct mxs_phy_data imx6q_phy_data = {
65 .flags = MXS_PHY_SENDING_SOF_TOO_FAST |
66 MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS,
67};
68
69static const struct mxs_phy_data imx6sl_phy_data = {
70 .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS,
71};
72
73static 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, },
77 { /* sentinel */ }
78};
79MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids);
80
Richard Zhaob3d99682012-07-07 22:56:47 +080081struct mxs_phy {
82 struct usb_phy phy;
83 struct clk *clk;
Peter Chen24007802014-02-24 10:20:54 +080084 const struct mxs_phy_data *data;
Richard Zhaob3d99682012-07-07 22:56:47 +080085};
86
Fabio Estevam51e563e2013-07-03 16:34:13 -030087static int mxs_phy_hw_init(struct mxs_phy *mxs_phy)
Richard Zhaob3d99682012-07-07 22:56:47 +080088{
Fabio Estevam51e563e2013-07-03 16:34:13 -030089 int ret;
Richard Zhaob3d99682012-07-07 22:56:47 +080090 void __iomem *base = mxs_phy->phy.io_priv;
91
Fabio Estevam51e563e2013-07-03 16:34:13 -030092 ret = stmp_reset_block(base + HW_USBPHY_CTRL);
93 if (ret)
94 return ret;
Richard Zhaob3d99682012-07-07 22:56:47 +080095
96 /* Power up the PHY */
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +010097 writel(0, base + HW_USBPHY_PWD);
Richard Zhaob3d99682012-07-07 22:56:47 +080098
99 /* enable FS/LS device */
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100100 writel(BM_USBPHY_CTRL_ENUTMILEVEL2 |
101 BM_USBPHY_CTRL_ENUTMILEVEL3,
102 base + HW_USBPHY_CTRL_SET);
Fabio Estevam51e563e2013-07-03 16:34:13 -0300103
104 return 0;
Richard Zhaob3d99682012-07-07 22:56:47 +0800105}
106
107static int mxs_phy_init(struct usb_phy *phy)
108{
Fabio Estevam67c21fc2013-12-02 01:02:34 -0200109 int ret;
Richard Zhaob3d99682012-07-07 22:56:47 +0800110 struct mxs_phy *mxs_phy = to_mxs_phy(phy);
111
Fabio Estevam67c21fc2013-12-02 01:02:34 -0200112 ret = clk_prepare_enable(mxs_phy->clk);
113 if (ret)
114 return ret;
115
Fabio Estevam51e563e2013-07-03 16:34:13 -0300116 return mxs_phy_hw_init(mxs_phy);
Richard Zhaob3d99682012-07-07 22:56:47 +0800117}
118
119static void mxs_phy_shutdown(struct usb_phy *phy)
120{
121 struct mxs_phy *mxs_phy = to_mxs_phy(phy);
122
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100123 writel(BM_USBPHY_CTRL_CLKGATE,
124 phy->io_priv + HW_USBPHY_CTRL_SET);
Richard Zhaob3d99682012-07-07 22:56:47 +0800125
126 clk_disable_unprepare(mxs_phy->clk);
127}
128
Peter Chen04a62212013-01-10 16:35:53 +0800129static int mxs_phy_suspend(struct usb_phy *x, int suspend)
130{
Fabio Estevam67c21fc2013-12-02 01:02:34 -0200131 int ret;
Peter Chen04a62212013-01-10 16:35:53 +0800132 struct mxs_phy *mxs_phy = to_mxs_phy(x);
133
134 if (suspend) {
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100135 writel(0xffffffff, x->io_priv + HW_USBPHY_PWD);
136 writel(BM_USBPHY_CTRL_CLKGATE,
137 x->io_priv + HW_USBPHY_CTRL_SET);
Peter Chen04a62212013-01-10 16:35:53 +0800138 clk_disable_unprepare(mxs_phy->clk);
139 } else {
Fabio Estevam67c21fc2013-12-02 01:02:34 -0200140 ret = clk_prepare_enable(mxs_phy->clk);
141 if (ret)
142 return ret;
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100143 writel(BM_USBPHY_CTRL_CLKGATE,
144 x->io_priv + HW_USBPHY_CTRL_CLR);
145 writel(0, x->io_priv + HW_USBPHY_PWD);
Peter Chen04a62212013-01-10 16:35:53 +0800146 }
147
148 return 0;
149}
150
Peter Chenac965112012-11-09 09:44:44 +0800151static int mxs_phy_on_connect(struct usb_phy *phy,
152 enum usb_device_speed speed)
Richard Zhaob3d99682012-07-07 22:56:47 +0800153{
Peter Chenac965112012-11-09 09:44:44 +0800154 dev_dbg(phy->dev, "%s speed device has connected\n",
155 (speed == USB_SPEED_HIGH) ? "high" : "non-high");
Richard Zhaob3d99682012-07-07 22:56:47 +0800156
Peter Chenac965112012-11-09 09:44:44 +0800157 if (speed == USB_SPEED_HIGH)
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100158 writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
159 phy->io_priv + HW_USBPHY_CTRL_SET);
Richard Zhaob3d99682012-07-07 22:56:47 +0800160
161 return 0;
162}
163
Peter Chenac965112012-11-09 09:44:44 +0800164static int mxs_phy_on_disconnect(struct usb_phy *phy,
165 enum usb_device_speed speed)
Richard Zhaob3d99682012-07-07 22:56:47 +0800166{
Peter Chenac965112012-11-09 09:44:44 +0800167 dev_dbg(phy->dev, "%s speed device has disconnected\n",
168 (speed == USB_SPEED_HIGH) ? "high" : "non-high");
Richard Zhaob3d99682012-07-07 22:56:47 +0800169
Peter Chenac965112012-11-09 09:44:44 +0800170 if (speed == USB_SPEED_HIGH)
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100171 writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
172 phy->io_priv + HW_USBPHY_CTRL_CLR);
Richard Zhaob3d99682012-07-07 22:56:47 +0800173
174 return 0;
175}
176
177static int mxs_phy_probe(struct platform_device *pdev)
178{
179 struct resource *res;
180 void __iomem *base;
181 struct clk *clk;
182 struct mxs_phy *mxs_phy;
Sascha Hauer25df6392013-02-27 15:16:30 +0100183 int ret;
Peter Chen24007802014-02-24 10:20:54 +0800184 const struct of_device_id *of_id =
185 of_match_device(mxs_phy_dt_ids, &pdev->dev);
Richard Zhaob3d99682012-07-07 22:56:47 +0800186
187 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding148e1132013-01-21 11:09:22 +0100188 base = devm_ioremap_resource(&pdev->dev, res);
189 if (IS_ERR(base))
190 return PTR_ERR(base);
Richard Zhaob3d99682012-07-07 22:56:47 +0800191
192 clk = devm_clk_get(&pdev->dev, NULL);
193 if (IS_ERR(clk)) {
194 dev_err(&pdev->dev,
195 "can't get the clock, err=%ld", PTR_ERR(clk));
196 return PTR_ERR(clk);
197 }
198
199 mxs_phy = devm_kzalloc(&pdev->dev, sizeof(*mxs_phy), GFP_KERNEL);
200 if (!mxs_phy) {
201 dev_err(&pdev->dev, "Failed to allocate USB PHY structure!\n");
202 return -ENOMEM;
203 }
204
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;
Peter Chen04a62212013-01-10 16:35:53 +0800210 mxs_phy->phy.set_suspend = mxs_phy_suspend;
Richard Zhaob3d99682012-07-07 22:56:47 +0800211 mxs_phy->phy.notify_connect = mxs_phy_on_connect;
212 mxs_phy->phy.notify_disconnect = mxs_phy_on_disconnect;
Michael Grzeschik4e0aa632013-05-15 15:03:14 +0200213 mxs_phy->phy.type = USB_PHY_TYPE_USB2;
Richard Zhaob3d99682012-07-07 22:56:47 +0800214
Richard Zhaob3d99682012-07-07 22:56:47 +0800215 mxs_phy->clk = clk;
Peter Chen24007802014-02-24 10:20:54 +0800216 mxs_phy->data = of_id->data;
Richard Zhaob3d99682012-07-07 22:56:47 +0800217
Jisheng Zhang97a27f72013-11-07 10:55:49 +0800218 platform_set_drvdata(pdev, mxs_phy);
Richard Zhaob3d99682012-07-07 22:56:47 +0800219
Sascha Hauer25df6392013-02-27 15:16:30 +0100220 ret = usb_add_phy_dev(&mxs_phy->phy);
221 if (ret)
222 return ret;
223
Richard Zhaob3d99682012-07-07 22:56:47 +0800224 return 0;
225}
226
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500227static int mxs_phy_remove(struct platform_device *pdev)
Richard Zhaob3d99682012-07-07 22:56:47 +0800228{
Sascha Hauer25df6392013-02-27 15:16:30 +0100229 struct mxs_phy *mxs_phy = platform_get_drvdata(pdev);
230
231 usb_remove_phy(&mxs_phy->phy);
232
Richard Zhaob3d99682012-07-07 22:56:47 +0800233 return 0;
234}
235
Richard Zhaob3d99682012-07-07 22:56:47 +0800236static struct platform_driver mxs_phy_driver = {
237 .probe = mxs_phy_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500238 .remove = mxs_phy_remove,
Richard Zhaob3d99682012-07-07 22:56:47 +0800239 .driver = {
240 .name = DRIVER_NAME,
241 .owner = THIS_MODULE,
242 .of_match_table = mxs_phy_dt_ids,
243 },
244};
245
246static int __init mxs_phy_module_init(void)
247{
248 return platform_driver_register(&mxs_phy_driver);
249}
250postcore_initcall(mxs_phy_module_init);
251
252static void __exit mxs_phy_module_exit(void)
253{
254 platform_driver_unregister(&mxs_phy_driver);
255}
256module_exit(mxs_phy_module_exit);
257
258MODULE_ALIAS("platform:mxs-usb-phy");
259MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
260MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");
261MODULE_DESCRIPTION("Freescale MXS USB PHY driver");
262MODULE_LICENSE("GPL");