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/clk.h>
15 #include <linux/err.h>
17 #include <linux/delay.h>
19 #include "ci_hdrc_imx.h"
21 #define MX25_USB_PHY_CTRL_OFFSET 0x08
22 #define MX25_BM_EXTERNAL_VBUS_DIVIDER BIT(23)
24 #define MX25_EHCI_INTERFACE_SINGLE_UNI (2 << 0)
25 #define MX25_EHCI_INTERFACE_DIFF_UNI (0 << 0)
26 #define MX25_EHCI_INTERFACE_MASK (0xf)
28 #define MX25_OTG_SIC_SHIFT 29
29 #define MX25_OTG_SIC_MASK (0x3 << MX25_OTG_SIC_SHIFT)
30 #define MX25_OTG_PM_BIT BIT(24)
31 #define MX25_OTG_PP_BIT BIT(11)
32 #define MX25_OTG_OCPOL_BIT BIT(3)
34 #define MX25_H1_SIC_SHIFT 21
35 #define MX25_H1_SIC_MASK (0x3 << MX25_H1_SIC_SHIFT)
36 #define MX25_H1_PP_BIT BIT(18)
37 #define MX25_H1_PM_BIT BIT(16)
38 #define MX25_H1_IPPUE_UP_BIT BIT(7)
39 #define MX25_H1_IPPUE_DOWN_BIT BIT(6)
40 #define MX25_H1_TLL_BIT BIT(5)
41 #define MX25_H1_USBTE_BIT BIT(4)
42 #define MX25_H1_OCPOL_BIT BIT(2)
44 #define MX27_H1_PM_BIT BIT(8)
45 #define MX27_H2_PM_BIT BIT(16)
46 #define MX27_OTG_PM_BIT BIT(24)
48 #define MX53_USB_OTG_PHY_CTRL_0_OFFSET 0x08
49 #define MX53_USB_OTG_PHY_CTRL_1_OFFSET 0x0c
50 #define MX53_USB_UH2_CTRL_OFFSET 0x14
51 #define MX53_USB_UH3_CTRL_OFFSET 0x18
52 #define MX53_BM_OVER_CUR_DIS_H1 BIT(5)
53 #define MX53_BM_OVER_CUR_DIS_OTG BIT(8)
54 #define MX53_BM_OVER_CUR_DIS_UHx BIT(30)
55 #define MX53_USB_PHYCTRL1_PLLDIV_MASK 0x3
56 #define MX53_USB_PLL_DIV_24_MHZ 0x01
58 #define MX6_BM_OVER_CUR_DIS BIT(7)
61 /* It's called once when probe a usb device */
62 int (*init)(struct imx_usbmisc_data *data);
63 /* It's called once after adding a usb device */
64 int (*post)(struct imx_usbmisc_data *data);
71 const struct usbmisc_ops *ops;
74 static struct imx_usbmisc *usbmisc;
76 static int usbmisc_imx25_init(struct imx_usbmisc_data *data)
84 spin_lock_irqsave(&usbmisc->lock, flags);
85 switch (data->index) {
87 val = readl(usbmisc->base);
88 val &= ~(MX25_OTG_SIC_MASK | MX25_OTG_PP_BIT);
89 val |= (MX25_EHCI_INTERFACE_DIFF_UNI & MX25_EHCI_INTERFACE_MASK) << MX25_OTG_SIC_SHIFT;
90 val |= (MX25_OTG_PM_BIT | MX25_OTG_OCPOL_BIT);
91 writel(val, usbmisc->base);
94 val = readl(usbmisc->base);
95 val &= ~(MX25_H1_SIC_MASK | MX25_H1_PP_BIT | MX25_H1_IPPUE_UP_BIT);
96 val |= (MX25_EHCI_INTERFACE_SINGLE_UNI & MX25_EHCI_INTERFACE_MASK) << MX25_H1_SIC_SHIFT;
97 val |= (MX25_H1_PM_BIT | MX25_H1_OCPOL_BIT | MX25_H1_TLL_BIT |
98 MX25_H1_USBTE_BIT | MX25_H1_IPPUE_DOWN_BIT);
100 writel(val, usbmisc->base);
104 spin_unlock_irqrestore(&usbmisc->lock, flags);
109 static int usbmisc_imx25_post(struct imx_usbmisc_data *data)
118 reg = usbmisc->base + MX25_USB_PHY_CTRL_OFFSET;
121 spin_lock_irqsave(&usbmisc->lock, flags);
123 writel(val | MX25_BM_EXTERNAL_VBUS_DIVIDER, reg);
124 spin_unlock_irqrestore(&usbmisc->lock, flags);
125 usleep_range(5000, 10000); /* needed to stabilize voltage */
131 static int usbmisc_imx27_init(struct imx_usbmisc_data *data)
136 switch (data->index) {
138 val = MX27_OTG_PM_BIT;
141 val = MX27_H1_PM_BIT;
144 val = MX27_H2_PM_BIT;
150 spin_lock_irqsave(&usbmisc->lock, flags);
151 if (data->disable_oc)
152 val = readl(usbmisc->base) | val;
154 val = readl(usbmisc->base) & ~val;
155 writel(val, usbmisc->base);
156 spin_unlock_irqrestore(&usbmisc->lock, flags);
161 static int usbmisc_imx53_init(struct imx_usbmisc_data *data)
163 void __iomem *reg = NULL;
170 /* Select a 24 MHz reference clock for the PHY */
171 reg = usbmisc->base + MX53_USB_OTG_PHY_CTRL_1_OFFSET;
173 val &= ~MX53_USB_PHYCTRL1_PLLDIV_MASK;
174 val |= MX53_USB_PLL_DIV_24_MHZ;
175 writel(val, usbmisc->base + MX53_USB_OTG_PHY_CTRL_1_OFFSET);
177 if (data->disable_oc) {
178 spin_lock_irqsave(&usbmisc->lock, flags);
179 switch (data->index) {
181 reg = usbmisc->base + MX53_USB_OTG_PHY_CTRL_0_OFFSET;
182 val = readl(reg) | MX53_BM_OVER_CUR_DIS_OTG;
185 reg = usbmisc->base + MX53_USB_OTG_PHY_CTRL_0_OFFSET;
186 val = readl(reg) | MX53_BM_OVER_CUR_DIS_H1;
189 reg = usbmisc->base + MX53_USB_UH2_CTRL_OFFSET;
190 val = readl(reg) | MX53_BM_OVER_CUR_DIS_UHx;
193 reg = usbmisc->base + MX53_USB_UH3_CTRL_OFFSET;
194 val = readl(reg) | MX53_BM_OVER_CUR_DIS_UHx;
199 spin_unlock_irqrestore(&usbmisc->lock, flags);
205 static int usbmisc_imx6q_init(struct imx_usbmisc_data *data)
213 if (data->disable_oc) {
214 spin_lock_irqsave(&usbmisc->lock, flags);
215 reg = readl(usbmisc->base + data->index * 4);
216 writel(reg | MX6_BM_OVER_CUR_DIS,
217 usbmisc->base + data->index * 4);
218 spin_unlock_irqrestore(&usbmisc->lock, flags);
224 static const struct usbmisc_ops imx25_usbmisc_ops = {
225 .init = usbmisc_imx25_init,
226 .post = usbmisc_imx25_post,
229 static const struct usbmisc_ops imx27_usbmisc_ops = {
230 .init = usbmisc_imx27_init,
233 static const struct usbmisc_ops imx53_usbmisc_ops = {
234 .init = usbmisc_imx53_init,
237 static const struct usbmisc_ops imx6q_usbmisc_ops = {
238 .init = usbmisc_imx6q_init,
241 int imx_usbmisc_init(struct imx_usbmisc_data *data)
244 return -EPROBE_DEFER;
245 if (!usbmisc->ops->init)
247 return usbmisc->ops->init(data);
249 EXPORT_SYMBOL_GPL(imx_usbmisc_init);
251 int imx_usbmisc_init_post(struct imx_usbmisc_data *data)
254 return -EPROBE_DEFER;
255 if (!usbmisc->ops->post)
257 return usbmisc->ops->post(data);
259 EXPORT_SYMBOL_GPL(imx_usbmisc_init_post);
261 static const struct of_device_id usbmisc_imx_dt_ids[] = {
263 .compatible = "fsl,imx25-usbmisc",
264 .data = &imx25_usbmisc_ops,
267 .compatible = "fsl,imx35-usbmisc",
268 .data = &imx25_usbmisc_ops,
271 .compatible = "fsl,imx27-usbmisc",
272 .data = &imx27_usbmisc_ops,
275 .compatible = "fsl,imx51-usbmisc",
276 .data = &imx53_usbmisc_ops,
279 .compatible = "fsl,imx53-usbmisc",
280 .data = &imx53_usbmisc_ops,
283 .compatible = "fsl,imx6q-usbmisc",
284 .data = &imx6q_usbmisc_ops,
288 MODULE_DEVICE_TABLE(of, usbmisc_imx_dt_ids);
290 static int usbmisc_imx_probe(struct platform_device *pdev)
292 struct resource *res;
293 struct imx_usbmisc *data;
295 struct of_device_id *tmp_dev;
300 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
304 spin_lock_init(&data->lock);
306 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
307 data->base = devm_ioremap_resource(&pdev->dev, res);
308 if (IS_ERR(data->base))
309 return PTR_ERR(data->base);
311 data->clk = devm_clk_get(&pdev->dev, NULL);
312 if (IS_ERR(data->clk)) {
314 "failed to get clock, err=%ld\n", PTR_ERR(data->clk));
315 return PTR_ERR(data->clk);
318 ret = clk_prepare_enable(data->clk);
321 "clk_prepare_enable failed, err=%d\n", ret);
325 tmp_dev = (struct of_device_id *)
326 of_match_device(usbmisc_imx_dt_ids, &pdev->dev);
327 data->ops = (const struct usbmisc_ops *)tmp_dev->data;
333 static int usbmisc_imx_remove(struct platform_device *pdev)
335 clk_disable_unprepare(usbmisc->clk);
340 static struct platform_driver usbmisc_imx_driver = {
341 .probe = usbmisc_imx_probe,
342 .remove = usbmisc_imx_remove,
344 .name = "usbmisc_imx",
345 .owner = THIS_MODULE,
346 .of_match_table = usbmisc_imx_dt_ids,
350 module_platform_driver(usbmisc_imx_driver);
352 MODULE_ALIAS("platform:usbmisc-imx");
353 MODULE_LICENSE("GPL v2");
354 MODULE_DESCRIPTION("driver for imx usb non-core registers");