blob: 714a6bd810ede0de96b28d3523bed2baa703f1de [file] [log] [blame]
Richard Zhaod142d6b2012-09-12 14:58:05 +03001/*
2 * Copyright 2012 Freescale Semiconductor, Inc.
3 *
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:
7 *
8 * http://www.opensource.org/licenses/gpl-license.html
9 * http://www.gnu.org/copyleft/gpl.html
10 */
11
12#include <linux/module.h>
13#include <linux/of_platform.h>
14#include <linux/clk.h>
15#include <linux/err.h>
16#include <linux/io.h>
Michael Grzeschika0685332013-03-30 12:54:01 +020017#include <linux/delay.h>
Richard Zhaod142d6b2012-09-12 14:58:05 +030018
19#include "ci13xxx_imx.h"
20
21#define USB_DEV_MAX 4
22
Michael Grzeschika0685332013-03-30 12:54:01 +020023#define MX25_USB_PHY_CTRL_OFFSET 0x08
24#define MX25_BM_EXTERNAL_VBUS_DIVIDER BIT(23)
25
Michael Grzeschikf0c910b2013-03-30 12:54:00 +020026#define MX53_USB_OTG_PHY_CTRL_0_OFFSET 0x08
27#define MX53_USB_UH2_CTRL_OFFSET 0x14
28#define MX53_USB_UH3_CTRL_OFFSET 0x18
29#define MX53_BM_OVER_CUR_DIS_H1 BIT(5)
30#define MX53_BM_OVER_CUR_DIS_OTG BIT(8)
31#define MX53_BM_OVER_CUR_DIS_UHx BIT(30)
32
Marc Kleine-Buddee6091082013-03-30 12:53:59 +020033#define MX6_BM_OVER_CUR_DIS BIT(7)
Richard Zhaod142d6b2012-09-12 14:58:05 +030034
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +020035struct imx_usbmisc {
Richard Zhaod142d6b2012-09-12 14:58:05 +030036 void __iomem *base;
37 spinlock_t lock;
38 struct clk *clk;
39 struct usbmisc_usb_device usbdev[USB_DEV_MAX];
Marc Kleine-Buddee6091082013-03-30 12:53:59 +020040 const struct usbmisc_ops *ops;
Richard Zhaod142d6b2012-09-12 14:58:05 +030041};
42
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +020043static struct imx_usbmisc *usbmisc;
Richard Zhaod142d6b2012-09-12 14:58:05 +030044
45static struct usbmisc_usb_device *get_usbdev(struct device *dev)
46{
47 int i, ret;
48
49 for (i = 0; i < USB_DEV_MAX; i++) {
50 if (usbmisc->usbdev[i].dev == dev)
51 return &usbmisc->usbdev[i];
52 else if (!usbmisc->usbdev[i].dev)
53 break;
54 }
55
56 if (i >= USB_DEV_MAX)
57 return ERR_PTR(-EBUSY);
58
59 ret = usbmisc_get_init_data(dev, &usbmisc->usbdev[i]);
60 if (ret)
61 return ERR_PTR(ret);
62
63 return &usbmisc->usbdev[i];
64}
65
Michael Grzeschika0685332013-03-30 12:54:01 +020066static int usbmisc_imx25_post(struct device *dev)
67{
68 struct usbmisc_usb_device *usbdev;
69 void __iomem *reg;
70 unsigned long flags;
71 u32 val;
72
73 usbdev = get_usbdev(dev);
74 if (IS_ERR(usbdev))
75 return PTR_ERR(usbdev);
76
77 reg = usbmisc->base + MX25_USB_PHY_CTRL_OFFSET;
78
79 if (usbdev->evdo) {
80 spin_lock_irqsave(&usbmisc->lock, flags);
81 val = readl(reg);
82 writel(val | MX25_BM_EXTERNAL_VBUS_DIVIDER, reg);
83 spin_unlock_irqrestore(&usbmisc->lock, flags);
84 usleep_range(5000, 10000); /* needed to stabilize voltage */
85 }
86
87 return 0;
88}
89
Michael Grzeschikf0c910b2013-03-30 12:54:00 +020090static int usbmisc_imx53_init(struct device *dev)
91{
92 struct usbmisc_usb_device *usbdev;
93 void __iomem *reg = NULL;
94 unsigned long flags;
95 u32 val = 0;
96
97 usbdev = get_usbdev(dev);
98 if (IS_ERR(usbdev))
99 return PTR_ERR(usbdev);
100
101 if (usbdev->disable_oc) {
102 spin_lock_irqsave(&usbmisc->lock, flags);
103 switch (usbdev->index) {
104 case 0:
105 reg = usbmisc->base + MX53_USB_OTG_PHY_CTRL_0_OFFSET;
106 val = readl(reg) | MX53_BM_OVER_CUR_DIS_OTG;
107 break;
108 case 1:
109 reg = usbmisc->base + MX53_USB_OTG_PHY_CTRL_0_OFFSET;
110 val = readl(reg) | MX53_BM_OVER_CUR_DIS_H1;
111 break;
112 case 2:
113 reg = usbmisc->base + MX53_USB_UH2_CTRL_OFFSET;
114 val = readl(reg) | MX53_BM_OVER_CUR_DIS_UHx;
115 break;
116 case 3:
117 reg = usbmisc->base + MX53_USB_UH3_CTRL_OFFSET;
118 val = readl(reg) | MX53_BM_OVER_CUR_DIS_UHx;
119 break;
120 }
121 if (reg && val)
122 writel(val, reg);
123 spin_unlock_irqrestore(&usbmisc->lock, flags);
124 }
125
126 return 0;
127}
128
Richard Zhaod142d6b2012-09-12 14:58:05 +0300129static int usbmisc_imx6q_init(struct device *dev)
130{
131
132 struct usbmisc_usb_device *usbdev;
133 unsigned long flags;
134 u32 reg;
135
136 usbdev = get_usbdev(dev);
137 if (IS_ERR(usbdev))
138 return PTR_ERR(usbdev);
139
140 if (usbdev->disable_oc) {
141 spin_lock_irqsave(&usbmisc->lock, flags);
142 reg = readl(usbmisc->base + usbdev->index * 4);
Marc Kleine-Buddee6091082013-03-30 12:53:59 +0200143 writel(reg | MX6_BM_OVER_CUR_DIS,
Richard Zhaod142d6b2012-09-12 14:58:05 +0300144 usbmisc->base + usbdev->index * 4);
145 spin_unlock_irqrestore(&usbmisc->lock, flags);
146 }
147
148 return 0;
149}
150
Michael Grzeschika0685332013-03-30 12:54:01 +0200151static const struct usbmisc_ops imx25_usbmisc_ops = {
152 .post = usbmisc_imx25_post,
153};
154
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200155static const struct usbmisc_ops imx53_usbmisc_ops = {
156 .init = usbmisc_imx53_init,
157};
158
Richard Zhaod142d6b2012-09-12 14:58:05 +0300159static const struct usbmisc_ops imx6q_usbmisc_ops = {
160 .init = usbmisc_imx6q_init,
161};
162
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200163static const struct of_device_id usbmisc_imx_dt_ids[] = {
Marc Kleine-Buddee6091082013-03-30 12:53:59 +0200164 {
Michael Grzeschika0685332013-03-30 12:54:01 +0200165 .compatible = "fsl,imx25-usbmisc",
166 .data = &imx25_usbmisc_ops,
167 },
168 {
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200169 .compatible = "fsl,imx53-usbmisc",
170 .data = &imx53_usbmisc_ops,
171 },
172 {
Marc Kleine-Buddee6091082013-03-30 12:53:59 +0200173 .compatible = "fsl,imx6q-usbmisc",
174 .data = &imx6q_usbmisc_ops,
175 },
Richard Zhaod142d6b2012-09-12 14:58:05 +0300176 { /* sentinel */ }
177};
178
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200179static int usbmisc_imx_probe(struct platform_device *pdev)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300180{
181 struct resource *res;
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200182 struct imx_usbmisc *data;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300183 int ret;
Marc Kleine-Buddee6091082013-03-30 12:53:59 +0200184 struct of_device_id *tmp_dev;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300185
186 if (usbmisc)
187 return -EBUSY;
188
189 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
190 if (!data)
191 return -ENOMEM;
192
193 spin_lock_init(&data->lock);
194
195 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding148e1132013-01-21 11:09:22 +0100196 data->base = devm_ioremap_resource(&pdev->dev, res);
197 if (IS_ERR(data->base))
198 return PTR_ERR(data->base);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300199
200 data->clk = devm_clk_get(&pdev->dev, NULL);
201 if (IS_ERR(data->clk)) {
202 dev_err(&pdev->dev,
203 "failed to get clock, err=%ld\n", PTR_ERR(data->clk));
204 return PTR_ERR(data->clk);
205 }
206
207 ret = clk_prepare_enable(data->clk);
208 if (ret) {
209 dev_err(&pdev->dev,
210 "clk_prepare_enable failed, err=%d\n", ret);
211 return ret;
212 }
213
Marc Kleine-Buddee6091082013-03-30 12:53:59 +0200214 tmp_dev = (struct of_device_id *)
215 of_match_device(usbmisc_imx_dt_ids, &pdev->dev);
216 data->ops = (const struct usbmisc_ops *)tmp_dev->data;
Marc Kleine-Budde00b9a1f92013-03-30 12:53:58 +0200217 usbmisc = data;
Marc Kleine-Buddee6091082013-03-30 12:53:59 +0200218 ret = usbmisc_set_ops(data->ops);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300219 if (ret) {
Marc Kleine-Budde00b9a1f92013-03-30 12:53:58 +0200220 usbmisc = NULL;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300221 clk_disable_unprepare(data->clk);
222 return ret;
223 }
224
Richard Zhaod142d6b2012-09-12 14:58:05 +0300225 return 0;
226}
227
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200228static int usbmisc_imx_remove(struct platform_device *pdev)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300229{
Marc Kleine-Buddee6091082013-03-30 12:53:59 +0200230 usbmisc_unset_ops(usbmisc->ops);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300231 clk_disable_unprepare(usbmisc->clk);
Marc Kleine-Budded48a24d2013-03-30 12:53:57 +0200232 usbmisc = NULL;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300233 return 0;
234}
235
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200236static struct platform_driver usbmisc_imx_driver = {
237 .probe = usbmisc_imx_probe,
238 .remove = usbmisc_imx_remove,
Richard Zhaod142d6b2012-09-12 14:58:05 +0300239 .driver = {
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200240 .name = "usbmisc_imx",
Richard Zhaod142d6b2012-09-12 14:58:05 +0300241 .owner = THIS_MODULE,
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200242 .of_match_table = usbmisc_imx_dt_ids,
Richard Zhaod142d6b2012-09-12 14:58:05 +0300243 },
244};
245
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200246int usbmisc_imx_drv_init(void)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300247{
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200248 return platform_driver_register(&usbmisc_imx_driver);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300249}
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200250subsys_initcall(usbmisc_imx_drv_init);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300251
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200252void usbmisc_imx_drv_exit(void)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300253{
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200254 platform_driver_unregister(&usbmisc_imx_driver);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300255}
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200256module_exit(usbmisc_imx_drv_exit);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300257
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200258MODULE_ALIAS("platform:usbmisc-imx");
Richard Zhaod142d6b2012-09-12 14:58:05 +0300259MODULE_LICENSE("GPL v2");
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200260MODULE_DESCRIPTION("driver for imx usb non-core registers");
Richard Zhaod142d6b2012-09-12 14:58:05 +0300261MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");