Linux 4.2.1
[linux/fpc-iii.git] / drivers / usb / chipidea / usbmisc_imx.c
blob3cefd49ddb00facd9e70cc83336e922d3e596eec
1 /*
2 * Copyright 2012 Freescale Semiconductor, Inc.
4 * The code contained herein is licensed under the GNU General Public
5 * License. You may obtain a copy of the GNU General Public License
6 * Version 2 or later at the following locations:
8 * http://www.opensource.org/licenses/gpl-license.html
9 * http://www.gnu.org/copyleft/gpl.html
12 #include <linux/module.h>
13 #include <linux/of_platform.h>
14 #include <linux/err.h>
15 #include <linux/io.h>
16 #include <linux/delay.h>
18 #include "ci_hdrc_imx.h"
20 #define MX25_USB_PHY_CTRL_OFFSET 0x08
21 #define MX25_BM_EXTERNAL_VBUS_DIVIDER BIT(23)
23 #define MX25_EHCI_INTERFACE_SINGLE_UNI (2 << 0)
24 #define MX25_EHCI_INTERFACE_DIFF_UNI (0 << 0)
25 #define MX25_EHCI_INTERFACE_MASK (0xf)
27 #define MX25_OTG_SIC_SHIFT 29
28 #define MX25_OTG_SIC_MASK (0x3 << MX25_OTG_SIC_SHIFT)
29 #define MX25_OTG_PM_BIT BIT(24)
30 #define MX25_OTG_PP_BIT BIT(11)
31 #define MX25_OTG_OCPOL_BIT BIT(3)
33 #define MX25_H1_SIC_SHIFT 21
34 #define MX25_H1_SIC_MASK (0x3 << MX25_H1_SIC_SHIFT)
35 #define MX25_H1_PP_BIT BIT(18)
36 #define MX25_H1_PM_BIT BIT(16)
37 #define MX25_H1_IPPUE_UP_BIT BIT(7)
38 #define MX25_H1_IPPUE_DOWN_BIT BIT(6)
39 #define MX25_H1_TLL_BIT BIT(5)
40 #define MX25_H1_USBTE_BIT BIT(4)
41 #define MX25_H1_OCPOL_BIT BIT(2)
43 #define MX27_H1_PM_BIT BIT(8)
44 #define MX27_H2_PM_BIT BIT(16)
45 #define MX27_OTG_PM_BIT BIT(24)
47 #define MX53_USB_OTG_PHY_CTRL_0_OFFSET 0x08
48 #define MX53_USB_OTG_PHY_CTRL_1_OFFSET 0x0c
49 #define MX53_USB_UH2_CTRL_OFFSET 0x14
50 #define MX53_USB_UH3_CTRL_OFFSET 0x18
51 #define MX53_BM_OVER_CUR_DIS_H1 BIT(5)
52 #define MX53_BM_OVER_CUR_DIS_OTG BIT(8)
53 #define MX53_BM_OVER_CUR_DIS_UHx BIT(30)
54 #define MX53_USB_PHYCTRL1_PLLDIV_MASK 0x3
55 #define MX53_USB_PLL_DIV_24_MHZ 0x01
57 #define MX6_BM_OVER_CUR_DIS BIT(7)
58 #define MX6_BM_WAKEUP_ENABLE BIT(10)
59 #define MX6_BM_ID_WAKEUP BIT(16)
60 #define MX6_BM_VBUS_WAKEUP BIT(17)
61 #define MX6SX_BM_DPDM_WAKEUP_EN BIT(29)
62 #define MX6_BM_WAKEUP_INTR BIT(31)
63 #define MX6_USB_OTG1_PHY_CTRL 0x18
64 /* For imx6dql, it is host-only controller, for later imx6, it is otg's */
65 #define MX6_USB_OTG2_PHY_CTRL 0x1c
66 #define MX6SX_USB_VBUS_WAKEUP_SOURCE(v) (v << 8)
67 #define MX6SX_USB_VBUS_WAKEUP_SOURCE_VBUS MX6SX_USB_VBUS_WAKEUP_SOURCE(0)
68 #define MX6SX_USB_VBUS_WAKEUP_SOURCE_AVALID MX6SX_USB_VBUS_WAKEUP_SOURCE(1)
69 #define MX6SX_USB_VBUS_WAKEUP_SOURCE_BVALID MX6SX_USB_VBUS_WAKEUP_SOURCE(2)
70 #define MX6SX_USB_VBUS_WAKEUP_SOURCE_SESS_END MX6SX_USB_VBUS_WAKEUP_SOURCE(3)
72 #define VF610_OVER_CUR_DIS BIT(7)
74 struct usbmisc_ops {
75 /* It's called once when probe a usb device */
76 int (*init)(struct imx_usbmisc_data *data);
77 /* It's called once after adding a usb device */
78 int (*post)(struct imx_usbmisc_data *data);
79 /* It's called when we need to enable/disable usb wakeup */
80 int (*set_wakeup)(struct imx_usbmisc_data *data, bool enabled);
83 struct imx_usbmisc {
84 void __iomem *base;
85 spinlock_t lock;
86 const struct usbmisc_ops *ops;
89 static int usbmisc_imx25_init(struct imx_usbmisc_data *data)
91 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
92 unsigned long flags;
93 u32 val = 0;
95 if (data->index > 1)
96 return -EINVAL;
98 spin_lock_irqsave(&usbmisc->lock, flags);
99 switch (data->index) {
100 case 0:
101 val = readl(usbmisc->base);
102 val &= ~(MX25_OTG_SIC_MASK | MX25_OTG_PP_BIT);
103 val |= (MX25_EHCI_INTERFACE_DIFF_UNI & MX25_EHCI_INTERFACE_MASK) << MX25_OTG_SIC_SHIFT;
104 val |= (MX25_OTG_PM_BIT | MX25_OTG_OCPOL_BIT);
105 writel(val, usbmisc->base);
106 break;
107 case 1:
108 val = readl(usbmisc->base);
109 val &= ~(MX25_H1_SIC_MASK | MX25_H1_PP_BIT | MX25_H1_IPPUE_UP_BIT);
110 val |= (MX25_EHCI_INTERFACE_SINGLE_UNI & MX25_EHCI_INTERFACE_MASK) << MX25_H1_SIC_SHIFT;
111 val |= (MX25_H1_PM_BIT | MX25_H1_OCPOL_BIT | MX25_H1_TLL_BIT |
112 MX25_H1_USBTE_BIT | MX25_H1_IPPUE_DOWN_BIT);
114 writel(val, usbmisc->base);
116 break;
118 spin_unlock_irqrestore(&usbmisc->lock, flags);
120 return 0;
123 static int usbmisc_imx25_post(struct imx_usbmisc_data *data)
125 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
126 void __iomem *reg;
127 unsigned long flags;
128 u32 val;
130 if (data->index > 2)
131 return -EINVAL;
133 if (data->evdo) {
134 spin_lock_irqsave(&usbmisc->lock, flags);
135 reg = usbmisc->base + MX25_USB_PHY_CTRL_OFFSET;
136 val = readl(reg);
137 writel(val | MX25_BM_EXTERNAL_VBUS_DIVIDER, reg);
138 spin_unlock_irqrestore(&usbmisc->lock, flags);
139 usleep_range(5000, 10000); /* needed to stabilize voltage */
142 return 0;
145 static int usbmisc_imx27_init(struct imx_usbmisc_data *data)
147 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
148 unsigned long flags;
149 u32 val;
151 switch (data->index) {
152 case 0:
153 val = MX27_OTG_PM_BIT;
154 break;
155 case 1:
156 val = MX27_H1_PM_BIT;
157 break;
158 case 2:
159 val = MX27_H2_PM_BIT;
160 break;
161 default:
162 return -EINVAL;
165 spin_lock_irqsave(&usbmisc->lock, flags);
166 if (data->disable_oc)
167 val = readl(usbmisc->base) | val;
168 else
169 val = readl(usbmisc->base) & ~val;
170 writel(val, usbmisc->base);
171 spin_unlock_irqrestore(&usbmisc->lock, flags);
173 return 0;
176 static int usbmisc_imx53_init(struct imx_usbmisc_data *data)
178 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
179 void __iomem *reg = NULL;
180 unsigned long flags;
181 u32 val = 0;
183 if (data->index > 3)
184 return -EINVAL;
186 /* Select a 24 MHz reference clock for the PHY */
187 val = readl(usbmisc->base + MX53_USB_OTG_PHY_CTRL_1_OFFSET);
188 val &= ~MX53_USB_PHYCTRL1_PLLDIV_MASK;
189 val |= MX53_USB_PLL_DIV_24_MHZ;
190 writel(val, usbmisc->base + MX53_USB_OTG_PHY_CTRL_1_OFFSET);
192 if (data->disable_oc) {
193 spin_lock_irqsave(&usbmisc->lock, flags);
194 switch (data->index) {
195 case 0:
196 reg = usbmisc->base + MX53_USB_OTG_PHY_CTRL_0_OFFSET;
197 val = readl(reg) | MX53_BM_OVER_CUR_DIS_OTG;
198 break;
199 case 1:
200 reg = usbmisc->base + MX53_USB_OTG_PHY_CTRL_0_OFFSET;
201 val = readl(reg) | MX53_BM_OVER_CUR_DIS_H1;
202 break;
203 case 2:
204 reg = usbmisc->base + MX53_USB_UH2_CTRL_OFFSET;
205 val = readl(reg) | MX53_BM_OVER_CUR_DIS_UHx;
206 break;
207 case 3:
208 reg = usbmisc->base + MX53_USB_UH3_CTRL_OFFSET;
209 val = readl(reg) | MX53_BM_OVER_CUR_DIS_UHx;
210 break;
212 if (reg && val)
213 writel(val, reg);
214 spin_unlock_irqrestore(&usbmisc->lock, flags);
217 return 0;
220 static int usbmisc_imx6q_set_wakeup
221 (struct imx_usbmisc_data *data, bool enabled)
223 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
224 unsigned long flags;
225 u32 val;
226 u32 wakeup_setting = (MX6_BM_WAKEUP_ENABLE |
227 MX6_BM_VBUS_WAKEUP | MX6_BM_ID_WAKEUP);
228 int ret = 0;
230 if (data->index > 3)
231 return -EINVAL;
233 spin_lock_irqsave(&usbmisc->lock, flags);
234 val = readl(usbmisc->base + data->index * 4);
235 if (enabled) {
236 val |= wakeup_setting;
237 writel(val, usbmisc->base + data->index * 4);
238 } else {
239 if (val & MX6_BM_WAKEUP_INTR)
240 pr_debug("wakeup int at ci_hdrc.%d\n", data->index);
241 val &= ~wakeup_setting;
242 writel(val, usbmisc->base + data->index * 4);
244 spin_unlock_irqrestore(&usbmisc->lock, flags);
246 return ret;
249 static int usbmisc_imx6q_init(struct imx_usbmisc_data *data)
251 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
252 unsigned long flags;
253 u32 reg;
255 if (data->index > 3)
256 return -EINVAL;
258 if (data->disable_oc) {
259 spin_lock_irqsave(&usbmisc->lock, flags);
260 reg = readl(usbmisc->base + data->index * 4);
261 writel(reg | MX6_BM_OVER_CUR_DIS,
262 usbmisc->base + data->index * 4);
263 spin_unlock_irqrestore(&usbmisc->lock, flags);
266 usbmisc_imx6q_set_wakeup(data, false);
268 return 0;
271 static int usbmisc_imx6sx_init(struct imx_usbmisc_data *data)
273 void __iomem *reg = NULL;
274 unsigned long flags;
275 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
276 u32 val;
278 usbmisc_imx6q_init(data);
280 if (data->index == 0 || data->index == 1) {
281 reg = usbmisc->base + MX6_USB_OTG1_PHY_CTRL + data->index * 4;
282 spin_lock_irqsave(&usbmisc->lock, flags);
283 /* Set vbus wakeup source as bvalid */
284 val = readl(reg);
285 writel(val | MX6SX_USB_VBUS_WAKEUP_SOURCE_BVALID, reg);
287 * Disable dp/dm wakeup in device mode when vbus is
288 * not there.
290 val = readl(usbmisc->base + data->index * 4);
291 writel(val & ~MX6SX_BM_DPDM_WAKEUP_EN,
292 usbmisc->base + data->index * 4);
293 spin_unlock_irqrestore(&usbmisc->lock, flags);
296 return 0;
299 static int usbmisc_vf610_init(struct imx_usbmisc_data *data)
301 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
302 u32 reg;
305 * Vybrid only has one misc register set, but in two different
306 * areas. These is reflected in two instances of this driver.
308 if (data->index >= 1)
309 return -EINVAL;
311 if (data->disable_oc) {
312 reg = readl(usbmisc->base);
313 writel(reg | VF610_OVER_CUR_DIS, usbmisc->base);
316 return 0;
319 static const struct usbmisc_ops imx25_usbmisc_ops = {
320 .init = usbmisc_imx25_init,
321 .post = usbmisc_imx25_post,
324 static const struct usbmisc_ops imx27_usbmisc_ops = {
325 .init = usbmisc_imx27_init,
328 static const struct usbmisc_ops imx53_usbmisc_ops = {
329 .init = usbmisc_imx53_init,
332 static const struct usbmisc_ops imx6q_usbmisc_ops = {
333 .set_wakeup = usbmisc_imx6q_set_wakeup,
334 .init = usbmisc_imx6q_init,
337 static const struct usbmisc_ops vf610_usbmisc_ops = {
338 .init = usbmisc_vf610_init,
341 static const struct usbmisc_ops imx6sx_usbmisc_ops = {
342 .set_wakeup = usbmisc_imx6q_set_wakeup,
343 .init = usbmisc_imx6sx_init,
346 int imx_usbmisc_init(struct imx_usbmisc_data *data)
348 struct imx_usbmisc *usbmisc;
350 if (!data)
351 return 0;
353 usbmisc = dev_get_drvdata(data->dev);
354 if (!usbmisc->ops->init)
355 return 0;
356 return usbmisc->ops->init(data);
358 EXPORT_SYMBOL_GPL(imx_usbmisc_init);
360 int imx_usbmisc_init_post(struct imx_usbmisc_data *data)
362 struct imx_usbmisc *usbmisc;
364 if (!data)
365 return 0;
367 usbmisc = dev_get_drvdata(data->dev);
368 if (!usbmisc->ops->post)
369 return 0;
370 return usbmisc->ops->post(data);
372 EXPORT_SYMBOL_GPL(imx_usbmisc_init_post);
374 int imx_usbmisc_set_wakeup(struct imx_usbmisc_data *data, bool enabled)
376 struct imx_usbmisc *usbmisc;
378 if (!data)
379 return 0;
381 usbmisc = dev_get_drvdata(data->dev);
382 if (!usbmisc->ops->set_wakeup)
383 return 0;
384 return usbmisc->ops->set_wakeup(data, enabled);
386 EXPORT_SYMBOL_GPL(imx_usbmisc_set_wakeup);
388 static const struct of_device_id usbmisc_imx_dt_ids[] = {
390 .compatible = "fsl,imx25-usbmisc",
391 .data = &imx25_usbmisc_ops,
394 .compatible = "fsl,imx35-usbmisc",
395 .data = &imx25_usbmisc_ops,
398 .compatible = "fsl,imx27-usbmisc",
399 .data = &imx27_usbmisc_ops,
402 .compatible = "fsl,imx51-usbmisc",
403 .data = &imx53_usbmisc_ops,
406 .compatible = "fsl,imx53-usbmisc",
407 .data = &imx53_usbmisc_ops,
410 .compatible = "fsl,imx6q-usbmisc",
411 .data = &imx6q_usbmisc_ops,
414 .compatible = "fsl,vf610-usbmisc",
415 .data = &vf610_usbmisc_ops,
418 .compatible = "fsl,imx6sx-usbmisc",
419 .data = &imx6sx_usbmisc_ops,
421 { /* sentinel */ }
423 MODULE_DEVICE_TABLE(of, usbmisc_imx_dt_ids);
425 static int usbmisc_imx_probe(struct platform_device *pdev)
427 struct resource *res;
428 struct imx_usbmisc *data;
429 struct of_device_id *tmp_dev;
431 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
432 if (!data)
433 return -ENOMEM;
435 spin_lock_init(&data->lock);
437 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
438 data->base = devm_ioremap_resource(&pdev->dev, res);
439 if (IS_ERR(data->base))
440 return PTR_ERR(data->base);
442 tmp_dev = (struct of_device_id *)
443 of_match_device(usbmisc_imx_dt_ids, &pdev->dev);
444 data->ops = (const struct usbmisc_ops *)tmp_dev->data;
445 platform_set_drvdata(pdev, data);
447 return 0;
450 static int usbmisc_imx_remove(struct platform_device *pdev)
452 return 0;
455 static struct platform_driver usbmisc_imx_driver = {
456 .probe = usbmisc_imx_probe,
457 .remove = usbmisc_imx_remove,
458 .driver = {
459 .name = "usbmisc_imx",
460 .of_match_table = usbmisc_imx_dt_ids,
464 module_platform_driver(usbmisc_imx_driver);
466 MODULE_ALIAS("platform:usbmisc-imx");
467 MODULE_LICENSE("GPL v2");
468 MODULE_DESCRIPTION("driver for imx usb non-core registers");
469 MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");