blob: da2eb6c968cae8605afa932e4b247c55f060d7c3 [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>
Peter Chen0d896532014-02-24 10:20:57 +080024#include <linux/regmap.h>
25#include <linux/mfd/syscon.h>
Richard Zhaob3d99682012-07-07 22:56:47 +080026
27#define DRIVER_NAME "mxs_phy"
28
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
33
Peter Chen3f126502014-02-24 10:21:02 +080034#define HW_USBPHY_DEBUG_SET 0x54
35#define HW_USBPHY_DEBUG_CLR 0x58
36
Peter Chen22db05e2014-02-24 10:20:59 +080037#define HW_USBPHY_IP 0x90
38#define HW_USBPHY_IP_SET 0x94
39#define HW_USBPHY_IP_CLR 0x98
40
Richard Zhaob3d99682012-07-07 22:56:47 +080041#define BM_USBPHY_CTRL_SFTRST BIT(31)
42#define BM_USBPHY_CTRL_CLKGATE BIT(30)
Peter Chen13644142014-02-24 10:20:55 +080043#define BM_USBPHY_CTRL_ENAUTOSET_USBCLKS BIT(26)
44#define BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE BIT(25)
Peter Chen3f126502014-02-24 10:21:02 +080045#define BM_USBPHY_CTRL_ENVBUSCHG_WKUP BIT(23)
46#define BM_USBPHY_CTRL_ENIDCHG_WKUP BIT(22)
47#define BM_USBPHY_CTRL_ENDPDMCHG_WKUP BIT(21)
Peter Chen13644142014-02-24 10:20:55 +080048#define BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD BIT(20)
49#define BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE BIT(19)
50#define BM_USBPHY_CTRL_ENAUTO_PWRON_PLL BIT(18)
Richard Zhaob3d99682012-07-07 22:56:47 +080051#define BM_USBPHY_CTRL_ENUTMILEVEL3 BIT(15)
52#define BM_USBPHY_CTRL_ENUTMILEVEL2 BIT(14)
53#define BM_USBPHY_CTRL_ENHOSTDISCONDETECT BIT(1)
54
Peter Chen22db05e2014-02-24 10:20:59 +080055#define BM_USBPHY_IP_FIX (BIT(17) | BIT(18))
56
Peter Chen3f126502014-02-24 10:21:02 +080057#define BM_USBPHY_DEBUG_CLKGATE BIT(30)
58
59/* Anatop Registers */
60#define ANADIG_USB1_VBUS_DET_STAT 0x1c0
61#define ANADIG_USB2_VBUS_DET_STAT 0x220
62
63#define ANADIG_USB1_LOOPBACK_SET 0x1e4
64#define ANADIG_USB1_LOOPBACK_CLR 0x1e8
65#define ANADIG_USB2_LOOPBACK_SET 0x244
66#define ANADIG_USB2_LOOPBACK_CLR 0x248
67
68#define BM_ANADIG_USB1_VBUS_DET_STAT_VBUS_VALID BIT(3)
69#define BM_ANADIG_USB2_VBUS_DET_STAT_VBUS_VALID BIT(3)
70
71#define BM_ANADIG_USB1_LOOPBACK_UTMI_DIG_TST1 BIT(2)
72#define BM_ANADIG_USB1_LOOPBACK_TSTI_TX_EN BIT(5)
73#define BM_ANADIG_USB2_LOOPBACK_UTMI_DIG_TST1 BIT(2)
74#define BM_ANADIG_USB2_LOOPBACK_TSTI_TX_EN BIT(5)
75
Peter Chen24007802014-02-24 10:20:54 +080076#define to_mxs_phy(p) container_of((p), struct mxs_phy, phy)
77
78/* Do disconnection between PHY and controller without vbus */
79#define MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS BIT(0)
80
81/*
82 * The PHY will be in messy if there is a wakeup after putting
83 * bus to suspend (set portsc.suspendM) but before setting PHY to low
84 * power mode (set portsc.phcd).
85 */
86#define MXS_PHY_ABNORMAL_IN_SUSPEND BIT(1)
87
88/*
89 * The SOF sends too fast after resuming, it will cause disconnection
90 * between host and high speed device.
91 */
92#define MXS_PHY_SENDING_SOF_TOO_FAST BIT(2)
93
Peter Chen22db05e2014-02-24 10:20:59 +080094/*
95 * IC has bug fixes logic, they include
96 * MXS_PHY_ABNORMAL_IN_SUSPEND and MXS_PHY_SENDING_SOF_TOO_FAST
97 * which are described at above flags, the RTL will handle it
98 * according to different versions.
99 */
100#define MXS_PHY_NEED_IP_FIX BIT(3)
101
Peter Chen24007802014-02-24 10:20:54 +0800102struct mxs_phy_data {
103 unsigned int flags;
104};
105
106static const struct mxs_phy_data imx23_phy_data = {
107 .flags = MXS_PHY_ABNORMAL_IN_SUSPEND | MXS_PHY_SENDING_SOF_TOO_FAST,
108};
109
110static const struct mxs_phy_data imx6q_phy_data = {
111 .flags = MXS_PHY_SENDING_SOF_TOO_FAST |
Peter Chen22db05e2014-02-24 10:20:59 +0800112 MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
113 MXS_PHY_NEED_IP_FIX,
Peter Chen24007802014-02-24 10:20:54 +0800114};
115
116static const struct mxs_phy_data imx6sl_phy_data = {
Peter Chen22db05e2014-02-24 10:20:59 +0800117 .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
118 MXS_PHY_NEED_IP_FIX,
Peter Chen24007802014-02-24 10:20:54 +0800119};
120
121static const struct of_device_id mxs_phy_dt_ids[] = {
122 { .compatible = "fsl,imx6sl-usbphy", .data = &imx6sl_phy_data, },
123 { .compatible = "fsl,imx6q-usbphy", .data = &imx6q_phy_data, },
124 { .compatible = "fsl,imx23-usbphy", .data = &imx23_phy_data, },
125 { /* sentinel */ }
126};
127MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids);
128
Richard Zhaob3d99682012-07-07 22:56:47 +0800129struct mxs_phy {
130 struct usb_phy phy;
131 struct clk *clk;
Peter Chen24007802014-02-24 10:20:54 +0800132 const struct mxs_phy_data *data;
Peter Chen0d896532014-02-24 10:20:57 +0800133 struct regmap *regmap_anatop;
Peter Chen83be1812014-02-24 10:21:00 +0800134 int port_id;
Richard Zhaob3d99682012-07-07 22:56:47 +0800135};
136
Fabio Estevam51e563e2013-07-03 16:34:13 -0300137static int mxs_phy_hw_init(struct mxs_phy *mxs_phy)
Richard Zhaob3d99682012-07-07 22:56:47 +0800138{
Fabio Estevam51e563e2013-07-03 16:34:13 -0300139 int ret;
Richard Zhaob3d99682012-07-07 22:56:47 +0800140 void __iomem *base = mxs_phy->phy.io_priv;
141
Fabio Estevam51e563e2013-07-03 16:34:13 -0300142 ret = stmp_reset_block(base + HW_USBPHY_CTRL);
143 if (ret)
144 return ret;
Richard Zhaob3d99682012-07-07 22:56:47 +0800145
146 /* Power up the PHY */
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100147 writel(0, base + HW_USBPHY_PWD);
Richard Zhaob3d99682012-07-07 22:56:47 +0800148
Peter Chen13644142014-02-24 10:20:55 +0800149 /*
150 * USB PHY Ctrl Setting
151 * - Auto clock/power on
152 * - Enable full/low speed support
153 */
154 writel(BM_USBPHY_CTRL_ENAUTOSET_USBCLKS |
155 BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE |
156 BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD |
157 BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE |
158 BM_USBPHY_CTRL_ENAUTO_PWRON_PLL |
159 BM_USBPHY_CTRL_ENUTMILEVEL2 |
160 BM_USBPHY_CTRL_ENUTMILEVEL3,
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100161 base + HW_USBPHY_CTRL_SET);
Fabio Estevam51e563e2013-07-03 16:34:13 -0300162
Peter Chen22db05e2014-02-24 10:20:59 +0800163 if (mxs_phy->data->flags & MXS_PHY_NEED_IP_FIX)
164 writel(BM_USBPHY_IP_FIX, base + HW_USBPHY_IP_SET);
165
Fabio Estevam51e563e2013-07-03 16:34:13 -0300166 return 0;
Richard Zhaob3d99682012-07-07 22:56:47 +0800167}
168
Peter Chen3f126502014-02-24 10:21:02 +0800169/* Return true if the vbus is there */
170static bool mxs_phy_get_vbus_status(struct mxs_phy *mxs_phy)
171{
172 unsigned int vbus_value;
173
174 if (mxs_phy->port_id == 0)
175 regmap_read(mxs_phy->regmap_anatop,
176 ANADIG_USB1_VBUS_DET_STAT,
177 &vbus_value);
178 else if (mxs_phy->port_id == 1)
179 regmap_read(mxs_phy->regmap_anatop,
180 ANADIG_USB2_VBUS_DET_STAT,
181 &vbus_value);
182
183 if (vbus_value & BM_ANADIG_USB1_VBUS_DET_STAT_VBUS_VALID)
184 return true;
185 else
186 return false;
187}
188
189static void __mxs_phy_disconnect_line(struct mxs_phy *mxs_phy, bool disconnect)
190{
191 void __iomem *base = mxs_phy->phy.io_priv;
192 u32 reg;
193
194 if (disconnect)
195 writel_relaxed(BM_USBPHY_DEBUG_CLKGATE,
196 base + HW_USBPHY_DEBUG_CLR);
197
198 if (mxs_phy->port_id == 0) {
199 reg = disconnect ? ANADIG_USB1_LOOPBACK_SET
200 : ANADIG_USB1_LOOPBACK_CLR;
201 regmap_write(mxs_phy->regmap_anatop, reg,
202 BM_ANADIG_USB1_LOOPBACK_UTMI_DIG_TST1 |
203 BM_ANADIG_USB1_LOOPBACK_TSTI_TX_EN);
204 } else if (mxs_phy->port_id == 1) {
205 reg = disconnect ? ANADIG_USB2_LOOPBACK_SET
206 : ANADIG_USB2_LOOPBACK_CLR;
207 regmap_write(mxs_phy->regmap_anatop, reg,
208 BM_ANADIG_USB2_LOOPBACK_UTMI_DIG_TST1 |
209 BM_ANADIG_USB2_LOOPBACK_TSTI_TX_EN);
210 }
211
212 if (!disconnect)
213 writel_relaxed(BM_USBPHY_DEBUG_CLKGATE,
214 base + HW_USBPHY_DEBUG_SET);
215
216 /* Delay some time, and let Linestate be SE0 for controller */
217 if (disconnect)
218 usleep_range(500, 1000);
219}
220
221static void mxs_phy_disconnect_line(struct mxs_phy *mxs_phy, bool on)
222{
223 bool vbus_is_on = false;
224
225 /* If the SoCs don't need to disconnect line without vbus, quit */
226 if (!(mxs_phy->data->flags & MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS))
227 return;
228
229 /* If the SoCs don't have anatop, quit */
230 if (!mxs_phy->regmap_anatop)
231 return;
232
233 vbus_is_on = mxs_phy_get_vbus_status(mxs_phy);
234
235 if (on && !vbus_is_on)
236 __mxs_phy_disconnect_line(mxs_phy, true);
237 else
238 __mxs_phy_disconnect_line(mxs_phy, false);
239
240}
241
Richard Zhaob3d99682012-07-07 22:56:47 +0800242static int mxs_phy_init(struct usb_phy *phy)
243{
Fabio Estevam67c21fc2013-12-02 01:02:34 -0200244 int ret;
Richard Zhaob3d99682012-07-07 22:56:47 +0800245 struct mxs_phy *mxs_phy = to_mxs_phy(phy);
246
Fabio Estevam67c21fc2013-12-02 01:02:34 -0200247 ret = clk_prepare_enable(mxs_phy->clk);
248 if (ret)
249 return ret;
250
Fabio Estevam51e563e2013-07-03 16:34:13 -0300251 return mxs_phy_hw_init(mxs_phy);
Richard Zhaob3d99682012-07-07 22:56:47 +0800252}
253
254static void mxs_phy_shutdown(struct usb_phy *phy)
255{
256 struct mxs_phy *mxs_phy = to_mxs_phy(phy);
257
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100258 writel(BM_USBPHY_CTRL_CLKGATE,
259 phy->io_priv + HW_USBPHY_CTRL_SET);
Richard Zhaob3d99682012-07-07 22:56:47 +0800260
261 clk_disable_unprepare(mxs_phy->clk);
262}
263
Peter Chen04a62212013-01-10 16:35:53 +0800264static int mxs_phy_suspend(struct usb_phy *x, int suspend)
265{
Fabio Estevam67c21fc2013-12-02 01:02:34 -0200266 int ret;
Peter Chen04a62212013-01-10 16:35:53 +0800267 struct mxs_phy *mxs_phy = to_mxs_phy(x);
268
269 if (suspend) {
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100270 writel(0xffffffff, x->io_priv + HW_USBPHY_PWD);
271 writel(BM_USBPHY_CTRL_CLKGATE,
272 x->io_priv + HW_USBPHY_CTRL_SET);
Peter Chen04a62212013-01-10 16:35:53 +0800273 clk_disable_unprepare(mxs_phy->clk);
274 } else {
Fabio Estevam67c21fc2013-12-02 01:02:34 -0200275 ret = clk_prepare_enable(mxs_phy->clk);
276 if (ret)
277 return ret;
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100278 writel(BM_USBPHY_CTRL_CLKGATE,
279 x->io_priv + HW_USBPHY_CTRL_CLR);
280 writel(0, x->io_priv + HW_USBPHY_PWD);
Peter Chen04a62212013-01-10 16:35:53 +0800281 }
282
283 return 0;
284}
285
Peter Chen3f126502014-02-24 10:21:02 +0800286static int mxs_phy_set_wakeup(struct usb_phy *x, bool enabled)
287{
288 struct mxs_phy *mxs_phy = to_mxs_phy(x);
289 u32 value = BM_USBPHY_CTRL_ENVBUSCHG_WKUP |
290 BM_USBPHY_CTRL_ENDPDMCHG_WKUP |
291 BM_USBPHY_CTRL_ENIDCHG_WKUP;
292 if (enabled) {
293 mxs_phy_disconnect_line(mxs_phy, true);
294 writel_relaxed(value, x->io_priv + HW_USBPHY_CTRL_SET);
295 } else {
296 writel_relaxed(value, x->io_priv + HW_USBPHY_CTRL_CLR);
297 mxs_phy_disconnect_line(mxs_phy, false);
298 }
299
300 return 0;
301}
302
Peter Chenac965112012-11-09 09:44:44 +0800303static int mxs_phy_on_connect(struct usb_phy *phy,
304 enum usb_device_speed speed)
Richard Zhaob3d99682012-07-07 22:56:47 +0800305{
Peter Chenf6a15822014-02-24 10:20:58 +0800306 dev_dbg(phy->dev, "%s device has connected\n",
307 (speed == USB_SPEED_HIGH) ? "HS" : "FS/LS");
Richard Zhaob3d99682012-07-07 22:56:47 +0800308
Peter Chenac965112012-11-09 09:44:44 +0800309 if (speed == USB_SPEED_HIGH)
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100310 writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
311 phy->io_priv + HW_USBPHY_CTRL_SET);
Richard Zhaob3d99682012-07-07 22:56:47 +0800312
313 return 0;
314}
315
Peter Chenac965112012-11-09 09:44:44 +0800316static int mxs_phy_on_disconnect(struct usb_phy *phy,
317 enum usb_device_speed speed)
Richard Zhaob3d99682012-07-07 22:56:47 +0800318{
Peter Chenf6a15822014-02-24 10:20:58 +0800319 dev_dbg(phy->dev, "%s device has disconnected\n",
320 (speed == USB_SPEED_HIGH) ? "HS" : "FS/LS");
Richard Zhaob3d99682012-07-07 22:56:47 +0800321
Peter Chenac965112012-11-09 09:44:44 +0800322 if (speed == USB_SPEED_HIGH)
Marc Kleine-Buddeb5a726b2013-02-28 11:52:30 +0100323 writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
324 phy->io_priv + HW_USBPHY_CTRL_CLR);
Richard Zhaob3d99682012-07-07 22:56:47 +0800325
326 return 0;
327}
328
329static int mxs_phy_probe(struct platform_device *pdev)
330{
331 struct resource *res;
332 void __iomem *base;
333 struct clk *clk;
334 struct mxs_phy *mxs_phy;
Sascha Hauer25df6392013-02-27 15:16:30 +0100335 int ret;
Peter Chen24007802014-02-24 10:20:54 +0800336 const struct of_device_id *of_id =
337 of_match_device(mxs_phy_dt_ids, &pdev->dev);
Peter Chen0d896532014-02-24 10:20:57 +0800338 struct device_node *np = pdev->dev.of_node;
Richard Zhaob3d99682012-07-07 22:56:47 +0800339
340 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding148e1132013-01-21 11:09:22 +0100341 base = devm_ioremap_resource(&pdev->dev, res);
342 if (IS_ERR(base))
343 return PTR_ERR(base);
Richard Zhaob3d99682012-07-07 22:56:47 +0800344
345 clk = devm_clk_get(&pdev->dev, NULL);
346 if (IS_ERR(clk)) {
347 dev_err(&pdev->dev,
348 "can't get the clock, err=%ld", PTR_ERR(clk));
349 return PTR_ERR(clk);
350 }
351
352 mxs_phy = devm_kzalloc(&pdev->dev, sizeof(*mxs_phy), GFP_KERNEL);
353 if (!mxs_phy) {
354 dev_err(&pdev->dev, "Failed to allocate USB PHY structure!\n");
355 return -ENOMEM;
356 }
357
Peter Chen0d896532014-02-24 10:20:57 +0800358 /* Some SoCs don't have anatop registers */
359 if (of_get_property(np, "fsl,anatop", NULL)) {
360 mxs_phy->regmap_anatop = syscon_regmap_lookup_by_phandle
361 (np, "fsl,anatop");
362 if (IS_ERR(mxs_phy->regmap_anatop)) {
363 dev_dbg(&pdev->dev,
364 "failed to find regmap for anatop\n");
365 return PTR_ERR(mxs_phy->regmap_anatop);
366 }
367 }
368
Peter Chen83be1812014-02-24 10:21:00 +0800369 ret = of_alias_get_id(np, "usbphy");
370 if (ret < 0)
371 dev_dbg(&pdev->dev, "failed to get alias id, errno %d\n", ret);
372 mxs_phy->port_id = ret;
373
Richard Zhaob3d99682012-07-07 22:56:47 +0800374 mxs_phy->phy.io_priv = base;
375 mxs_phy->phy.dev = &pdev->dev;
376 mxs_phy->phy.label = DRIVER_NAME;
377 mxs_phy->phy.init = mxs_phy_init;
378 mxs_phy->phy.shutdown = mxs_phy_shutdown;
Peter Chen04a62212013-01-10 16:35:53 +0800379 mxs_phy->phy.set_suspend = mxs_phy_suspend;
Richard Zhaob3d99682012-07-07 22:56:47 +0800380 mxs_phy->phy.notify_connect = mxs_phy_on_connect;
381 mxs_phy->phy.notify_disconnect = mxs_phy_on_disconnect;
Michael Grzeschik4e0aa632013-05-15 15:03:14 +0200382 mxs_phy->phy.type = USB_PHY_TYPE_USB2;
Peter Chen3f126502014-02-24 10:21:02 +0800383 mxs_phy->phy.set_wakeup = mxs_phy_set_wakeup;
Richard Zhaob3d99682012-07-07 22:56:47 +0800384
Richard Zhaob3d99682012-07-07 22:56:47 +0800385 mxs_phy->clk = clk;
Peter Chen24007802014-02-24 10:20:54 +0800386 mxs_phy->data = of_id->data;
Richard Zhaob3d99682012-07-07 22:56:47 +0800387
Jisheng Zhang97a27f72013-11-07 10:55:49 +0800388 platform_set_drvdata(pdev, mxs_phy);
Richard Zhaob3d99682012-07-07 22:56:47 +0800389
Sascha Hauer25df6392013-02-27 15:16:30 +0100390 ret = usb_add_phy_dev(&mxs_phy->phy);
391 if (ret)
392 return ret;
393
Richard Zhaob3d99682012-07-07 22:56:47 +0800394 return 0;
395}
396
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500397static int mxs_phy_remove(struct platform_device *pdev)
Richard Zhaob3d99682012-07-07 22:56:47 +0800398{
Sascha Hauer25df6392013-02-27 15:16:30 +0100399 struct mxs_phy *mxs_phy = platform_get_drvdata(pdev);
400
401 usb_remove_phy(&mxs_phy->phy);
402
Richard Zhaob3d99682012-07-07 22:56:47 +0800403 return 0;
404}
405
Richard Zhaob3d99682012-07-07 22:56:47 +0800406static struct platform_driver mxs_phy_driver = {
407 .probe = mxs_phy_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500408 .remove = mxs_phy_remove,
Richard Zhaob3d99682012-07-07 22:56:47 +0800409 .driver = {
410 .name = DRIVER_NAME,
411 .owner = THIS_MODULE,
412 .of_match_table = mxs_phy_dt_ids,
413 },
414};
415
416static int __init mxs_phy_module_init(void)
417{
418 return platform_driver_register(&mxs_phy_driver);
419}
420postcore_initcall(mxs_phy_module_init);
421
422static void __exit mxs_phy_module_exit(void)
423{
424 platform_driver_unregister(&mxs_phy_driver);
425}
426module_exit(mxs_phy_module_exit);
427
428MODULE_ALIAS("platform:mxs-usb-phy");
429MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
430MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");
431MODULE_DESCRIPTION("Freescale MXS USB PHY driver");
432MODULE_LICENSE("GPL");