Commit | Line | Data |
---|---|---|
b3d99681 RZ |
1 | /* |
2 | * Copyright 2012 Freescale Semiconductor, Inc. | |
3 | * 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> | |
23 | ||
24 | #define DRIVER_NAME "mxs_phy" | |
25 | ||
26 | #define HW_USBPHY_PWD 0x00 | |
27 | #define HW_USBPHY_CTRL 0x30 | |
28 | #define HW_USBPHY_CTRL_SET 0x34 | |
29 | #define HW_USBPHY_CTRL_CLR 0x38 | |
30 | ||
31 | #define BM_USBPHY_CTRL_SFTRST BIT(31) | |
32 | #define BM_USBPHY_CTRL_CLKGATE BIT(30) | |
33 | #define BM_USBPHY_CTRL_ENUTMILEVEL3 BIT(15) | |
34 | #define BM_USBPHY_CTRL_ENUTMILEVEL2 BIT(14) | |
35 | #define BM_USBPHY_CTRL_ENHOSTDISCONDETECT BIT(1) | |
36 | ||
37 | struct mxs_phy { | |
38 | struct usb_phy phy; | |
39 | struct clk *clk; | |
40 | }; | |
41 | ||
42 | #define to_mxs_phy(p) container_of((p), struct mxs_phy, phy) | |
43 | ||
51e563e3 | 44 | static int mxs_phy_hw_init(struct mxs_phy *mxs_phy) |
b3d99681 | 45 | { |
51e563e3 | 46 | int ret; |
b3d99681 RZ |
47 | void __iomem *base = mxs_phy->phy.io_priv; |
48 | ||
51e563e3 FE |
49 | ret = stmp_reset_block(base + HW_USBPHY_CTRL); |
50 | if (ret) | |
51 | return ret; | |
b3d99681 RZ |
52 | |
53 | /* Power up the PHY */ | |
b5a726b3 | 54 | writel(0, base + HW_USBPHY_PWD); |
b3d99681 RZ |
55 | |
56 | /* enable FS/LS device */ | |
b5a726b3 MKB |
57 | writel(BM_USBPHY_CTRL_ENUTMILEVEL2 | |
58 | BM_USBPHY_CTRL_ENUTMILEVEL3, | |
59 | base + HW_USBPHY_CTRL_SET); | |
51e563e3 FE |
60 | |
61 | return 0; | |
b3d99681 RZ |
62 | } |
63 | ||
64 | static int mxs_phy_init(struct usb_phy *phy) | |
65 | { | |
67c21fc8 | 66 | int ret; |
b3d99681 RZ |
67 | struct mxs_phy *mxs_phy = to_mxs_phy(phy); |
68 | ||
67c21fc8 FE |
69 | ret = clk_prepare_enable(mxs_phy->clk); |
70 | if (ret) | |
71 | return ret; | |
72 | ||
51e563e3 | 73 | return mxs_phy_hw_init(mxs_phy); |
b3d99681 RZ |
74 | } |
75 | ||
76 | static void mxs_phy_shutdown(struct usb_phy *phy) | |
77 | { | |
78 | struct mxs_phy *mxs_phy = to_mxs_phy(phy); | |
79 | ||
b5a726b3 MKB |
80 | writel(BM_USBPHY_CTRL_CLKGATE, |
81 | phy->io_priv + HW_USBPHY_CTRL_SET); | |
b3d99681 RZ |
82 | |
83 | clk_disable_unprepare(mxs_phy->clk); | |
84 | } | |
85 | ||
04a6221c PC |
86 | static int mxs_phy_suspend(struct usb_phy *x, int suspend) |
87 | { | |
67c21fc8 | 88 | int ret; |
04a6221c PC |
89 | struct mxs_phy *mxs_phy = to_mxs_phy(x); |
90 | ||
91 | if (suspend) { | |
b5a726b3 MKB |
92 | writel(0xffffffff, x->io_priv + HW_USBPHY_PWD); |
93 | writel(BM_USBPHY_CTRL_CLKGATE, | |
94 | x->io_priv + HW_USBPHY_CTRL_SET); | |
04a6221c PC |
95 | clk_disable_unprepare(mxs_phy->clk); |
96 | } else { | |
67c21fc8 FE |
97 | ret = clk_prepare_enable(mxs_phy->clk); |
98 | if (ret) | |
99 | return ret; | |
b5a726b3 MKB |
100 | writel(BM_USBPHY_CTRL_CLKGATE, |
101 | x->io_priv + HW_USBPHY_CTRL_CLR); | |
102 | writel(0, x->io_priv + HW_USBPHY_PWD); | |
04a6221c PC |
103 | } |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
ac96511b PC |
108 | static int mxs_phy_on_connect(struct usb_phy *phy, |
109 | enum usb_device_speed speed) | |
b3d99681 | 110 | { |
ac96511b PC |
111 | dev_dbg(phy->dev, "%s speed device has connected\n", |
112 | (speed == USB_SPEED_HIGH) ? "high" : "non-high"); | |
b3d99681 | 113 | |
ac96511b | 114 | if (speed == USB_SPEED_HIGH) |
b5a726b3 MKB |
115 | writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT, |
116 | phy->io_priv + HW_USBPHY_CTRL_SET); | |
b3d99681 RZ |
117 | |
118 | return 0; | |
119 | } | |
120 | ||
ac96511b PC |
121 | static int mxs_phy_on_disconnect(struct usb_phy *phy, |
122 | enum usb_device_speed speed) | |
b3d99681 | 123 | { |
ac96511b PC |
124 | dev_dbg(phy->dev, "%s speed device has disconnected\n", |
125 | (speed == USB_SPEED_HIGH) ? "high" : "non-high"); | |
b3d99681 | 126 | |
ac96511b | 127 | if (speed == USB_SPEED_HIGH) |
b5a726b3 MKB |
128 | writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT, |
129 | phy->io_priv + HW_USBPHY_CTRL_CLR); | |
b3d99681 RZ |
130 | |
131 | return 0; | |
132 | } | |
133 | ||
134 | static int mxs_phy_probe(struct platform_device *pdev) | |
135 | { | |
136 | struct resource *res; | |
137 | void __iomem *base; | |
138 | struct clk *clk; | |
139 | struct mxs_phy *mxs_phy; | |
25df6397 | 140 | int ret; |
b3d99681 RZ |
141 | |
142 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
148e1134 TR |
143 | base = devm_ioremap_resource(&pdev->dev, res); |
144 | if (IS_ERR(base)) | |
145 | return PTR_ERR(base); | |
b3d99681 RZ |
146 | |
147 | clk = devm_clk_get(&pdev->dev, NULL); | |
148 | if (IS_ERR(clk)) { | |
149 | dev_err(&pdev->dev, | |
150 | "can't get the clock, err=%ld", PTR_ERR(clk)); | |
151 | return PTR_ERR(clk); | |
152 | } | |
153 | ||
154 | mxs_phy = devm_kzalloc(&pdev->dev, sizeof(*mxs_phy), GFP_KERNEL); | |
155 | if (!mxs_phy) { | |
156 | dev_err(&pdev->dev, "Failed to allocate USB PHY structure!\n"); | |
157 | return -ENOMEM; | |
158 | } | |
159 | ||
160 | mxs_phy->phy.io_priv = base; | |
161 | mxs_phy->phy.dev = &pdev->dev; | |
162 | mxs_phy->phy.label = DRIVER_NAME; | |
163 | mxs_phy->phy.init = mxs_phy_init; | |
164 | mxs_phy->phy.shutdown = mxs_phy_shutdown; | |
04a6221c | 165 | mxs_phy->phy.set_suspend = mxs_phy_suspend; |
b3d99681 RZ |
166 | mxs_phy->phy.notify_connect = mxs_phy_on_connect; |
167 | mxs_phy->phy.notify_disconnect = mxs_phy_on_disconnect; | |
4e0aa635 | 168 | mxs_phy->phy.type = USB_PHY_TYPE_USB2; |
b3d99681 | 169 | |
b3d99681 RZ |
170 | mxs_phy->clk = clk; |
171 | ||
97a27f73 | 172 | platform_set_drvdata(pdev, mxs_phy); |
b3d99681 | 173 | |
25df6397 SH |
174 | ret = usb_add_phy_dev(&mxs_phy->phy); |
175 | if (ret) | |
176 | return ret; | |
177 | ||
b3d99681 RZ |
178 | return 0; |
179 | } | |
180 | ||
fb4e98ab | 181 | static int mxs_phy_remove(struct platform_device *pdev) |
b3d99681 | 182 | { |
25df6397 SH |
183 | struct mxs_phy *mxs_phy = platform_get_drvdata(pdev); |
184 | ||
185 | usb_remove_phy(&mxs_phy->phy); | |
186 | ||
b3d99681 RZ |
187 | return 0; |
188 | } | |
189 | ||
190 | static const struct of_device_id mxs_phy_dt_ids[] = { | |
191 | { .compatible = "fsl,imx23-usbphy", }, | |
192 | { /* sentinel */ } | |
193 | }; | |
194 | MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids); | |
195 | ||
196 | static struct platform_driver mxs_phy_driver = { | |
197 | .probe = mxs_phy_probe, | |
7690417d | 198 | .remove = mxs_phy_remove, |
b3d99681 RZ |
199 | .driver = { |
200 | .name = DRIVER_NAME, | |
201 | .owner = THIS_MODULE, | |
202 | .of_match_table = mxs_phy_dt_ids, | |
203 | }, | |
204 | }; | |
205 | ||
206 | static int __init mxs_phy_module_init(void) | |
207 | { | |
208 | return platform_driver_register(&mxs_phy_driver); | |
209 | } | |
210 | postcore_initcall(mxs_phy_module_init); | |
211 | ||
212 | static void __exit mxs_phy_module_exit(void) | |
213 | { | |
214 | platform_driver_unregister(&mxs_phy_driver); | |
215 | } | |
216 | module_exit(mxs_phy_module_exit); | |
217 | ||
218 | MODULE_ALIAS("platform:mxs-usb-phy"); | |
219 | MODULE_AUTHOR("Marek Vasut <marex@denx.de>"); | |
220 | MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>"); | |
221 | MODULE_DESCRIPTION("Freescale MXS USB PHY driver"); | |
222 | MODULE_LICENSE("GPL"); |