blob: 926c997ef3106be1b2a887cbc362f444cceb43e3 [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
Alexander Shishkin8e229782013-06-24 14:46:36 +030019#include "ci_hdrc_imx.h"
Richard Zhaod142d6b2012-09-12 14:58:05 +030020
Michael Grzeschika0685332013-03-30 12:54:01 +020021#define MX25_USB_PHY_CTRL_OFFSET 0x08
22#define MX25_BM_EXTERNAL_VBUS_DIVIDER BIT(23)
23
Denis Carikli72ee92d2014-04-23 15:56:36 +080024#define MX25_EHCI_INTERFACE_SINGLE_UNI (2 << 0)
25#define MX25_EHCI_INTERFACE_DIFF_UNI (0 << 0)
26#define MX25_EHCI_INTERFACE_MASK (0xf)
27
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)
33
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)
43
Alexander Shiyan9f90e112013-12-06 16:35:14 +080044#define MX27_H1_PM_BIT BIT(8)
45#define MX27_H2_PM_BIT BIT(16)
46#define MX27_OTG_PM_BIT BIT(24)
47
Michael Grzeschikf0c910b2013-03-30 12:54:00 +020048#define MX53_USB_OTG_PHY_CTRL_0_OFFSET 0x08
Fabio Estevam33f92a82014-05-04 09:24:39 +080049#define MX53_USB_OTG_PHY_CTRL_1_OFFSET 0x0c
Michael Grzeschikf0c910b2013-03-30 12:54:00 +020050#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)
Fabio Estevam33f92a82014-05-04 09:24:39 +080055#define MX53_USB_PHYCTRL1_PLLDIV_MASK 0x3
56#define MX53_USB_PLL_DIV_24_MHZ 0x01
Michael Grzeschikf0c910b2013-03-30 12:54:00 +020057
Marc Kleine-Buddee6091082013-03-30 12:53:59 +020058#define MX6_BM_OVER_CUR_DIS BIT(7)
Richard Zhaod142d6b2012-09-12 14:58:05 +030059
Stefan Agnerf40017e2014-09-22 08:14:15 +080060#define VF610_OVER_CUR_DIS BIT(7)
61
Sascha Hauer05986ba2013-08-14 12:44:16 +030062struct usbmisc_ops {
63 /* It's called once when probe a usb device */
64 int (*init)(struct imx_usbmisc_data *data);
65 /* It's called once after adding a usb device */
66 int (*post)(struct imx_usbmisc_data *data);
67};
68
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +020069struct imx_usbmisc {
Richard Zhaod142d6b2012-09-12 14:58:05 +030070 void __iomem *base;
71 spinlock_t lock;
72 struct clk *clk;
Marc Kleine-Buddee6091082013-03-30 12:53:59 +020073 const struct usbmisc_ops *ops;
Richard Zhaod142d6b2012-09-12 14:58:05 +030074};
75
Denis Carikli72ee92d2014-04-23 15:56:36 +080076static int usbmisc_imx25_init(struct imx_usbmisc_data *data)
77{
Stefan Agnerf40017e2014-09-22 08:14:15 +080078 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
Denis Carikli72ee92d2014-04-23 15:56:36 +080079 unsigned long flags;
80 u32 val = 0;
81
82 if (data->index > 1)
83 return -EINVAL;
84
85 spin_lock_irqsave(&usbmisc->lock, flags);
86 switch (data->index) {
87 case 0:
88 val = readl(usbmisc->base);
89 val &= ~(MX25_OTG_SIC_MASK | MX25_OTG_PP_BIT);
90 val |= (MX25_EHCI_INTERFACE_DIFF_UNI & MX25_EHCI_INTERFACE_MASK) << MX25_OTG_SIC_SHIFT;
91 val |= (MX25_OTG_PM_BIT | MX25_OTG_OCPOL_BIT);
92 writel(val, usbmisc->base);
93 break;
94 case 1:
95 val = readl(usbmisc->base);
96 val &= ~(MX25_H1_SIC_MASK | MX25_H1_PP_BIT | MX25_H1_IPPUE_UP_BIT);
97 val |= (MX25_EHCI_INTERFACE_SINGLE_UNI & MX25_EHCI_INTERFACE_MASK) << MX25_H1_SIC_SHIFT;
98 val |= (MX25_H1_PM_BIT | MX25_H1_OCPOL_BIT | MX25_H1_TLL_BIT |
99 MX25_H1_USBTE_BIT | MX25_H1_IPPUE_DOWN_BIT);
100
101 writel(val, usbmisc->base);
102
103 break;
104 }
105 spin_unlock_irqrestore(&usbmisc->lock, flags);
106
107 return 0;
108}
109
Sascha Hauer05986ba2013-08-14 12:44:16 +0300110static int usbmisc_imx25_post(struct imx_usbmisc_data *data)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300111{
Stefan Agnerf40017e2014-09-22 08:14:15 +0800112 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
Michael Grzeschika0685332013-03-30 12:54:01 +0200113 void __iomem *reg;
114 unsigned long flags;
115 u32 val;
116
Sascha Hauer05986ba2013-08-14 12:44:16 +0300117 if (data->index > 2)
118 return -EINVAL;
Michael Grzeschika0685332013-03-30 12:54:01 +0200119
120 reg = usbmisc->base + MX25_USB_PHY_CTRL_OFFSET;
121
Sascha Hauer05986ba2013-08-14 12:44:16 +0300122 if (data->evdo) {
Michael Grzeschika0685332013-03-30 12:54:01 +0200123 spin_lock_irqsave(&usbmisc->lock, flags);
124 val = readl(reg);
125 writel(val | MX25_BM_EXTERNAL_VBUS_DIVIDER, reg);
126 spin_unlock_irqrestore(&usbmisc->lock, flags);
127 usleep_range(5000, 10000); /* needed to stabilize voltage */
128 }
129
130 return 0;
131}
132
Alexander Shiyan9f90e112013-12-06 16:35:14 +0800133static int usbmisc_imx27_init(struct imx_usbmisc_data *data)
134{
Stefan Agnerf40017e2014-09-22 08:14:15 +0800135 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
Alexander Shiyan9f90e112013-12-06 16:35:14 +0800136 unsigned long flags;
137 u32 val;
138
139 switch (data->index) {
140 case 0:
141 val = MX27_OTG_PM_BIT;
142 break;
143 case 1:
144 val = MX27_H1_PM_BIT;
145 break;
146 case 2:
147 val = MX27_H2_PM_BIT;
148 break;
149 default:
150 return -EINVAL;
151 };
152
153 spin_lock_irqsave(&usbmisc->lock, flags);
154 if (data->disable_oc)
155 val = readl(usbmisc->base) | val;
156 else
157 val = readl(usbmisc->base) & ~val;
158 writel(val, usbmisc->base);
159 spin_unlock_irqrestore(&usbmisc->lock, flags);
160
161 return 0;
162}
163
Sascha Hauer05986ba2013-08-14 12:44:16 +0300164static int usbmisc_imx53_init(struct imx_usbmisc_data *data)
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200165{
Stefan Agnerf40017e2014-09-22 08:14:15 +0800166 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200167 void __iomem *reg = NULL;
168 unsigned long flags;
169 u32 val = 0;
170
Sascha Hauer05986ba2013-08-14 12:44:16 +0300171 if (data->index > 3)
172 return -EINVAL;
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200173
Fabio Estevam33f92a82014-05-04 09:24:39 +0800174 /* Select a 24 MHz reference clock for the PHY */
175 reg = usbmisc->base + MX53_USB_OTG_PHY_CTRL_1_OFFSET;
176 val = readl(reg);
177 val &= ~MX53_USB_PHYCTRL1_PLLDIV_MASK;
178 val |= MX53_USB_PLL_DIV_24_MHZ;
179 writel(val, usbmisc->base + MX53_USB_OTG_PHY_CTRL_1_OFFSET);
180
Sascha Hauer05986ba2013-08-14 12:44:16 +0300181 if (data->disable_oc) {
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200182 spin_lock_irqsave(&usbmisc->lock, flags);
Sascha Hauer05986ba2013-08-14 12:44:16 +0300183 switch (data->index) {
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200184 case 0:
185 reg = usbmisc->base + MX53_USB_OTG_PHY_CTRL_0_OFFSET;
186 val = readl(reg) | MX53_BM_OVER_CUR_DIS_OTG;
187 break;
188 case 1:
189 reg = usbmisc->base + MX53_USB_OTG_PHY_CTRL_0_OFFSET;
190 val = readl(reg) | MX53_BM_OVER_CUR_DIS_H1;
191 break;
192 case 2:
193 reg = usbmisc->base + MX53_USB_UH2_CTRL_OFFSET;
194 val = readl(reg) | MX53_BM_OVER_CUR_DIS_UHx;
195 break;
196 case 3:
197 reg = usbmisc->base + MX53_USB_UH3_CTRL_OFFSET;
198 val = readl(reg) | MX53_BM_OVER_CUR_DIS_UHx;
199 break;
200 }
201 if (reg && val)
202 writel(val, reg);
203 spin_unlock_irqrestore(&usbmisc->lock, flags);
204 }
205
206 return 0;
207}
208
Sascha Hauer05986ba2013-08-14 12:44:16 +0300209static int usbmisc_imx6q_init(struct imx_usbmisc_data *data)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300210{
Stefan Agnerf40017e2014-09-22 08:14:15 +0800211 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300212 unsigned long flags;
213 u32 reg;
214
Sascha Hauer05986ba2013-08-14 12:44:16 +0300215 if (data->index > 3)
216 return -EINVAL;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300217
Sascha Hauer05986ba2013-08-14 12:44:16 +0300218 if (data->disable_oc) {
Richard Zhaod142d6b2012-09-12 14:58:05 +0300219 spin_lock_irqsave(&usbmisc->lock, flags);
Sascha Hauer05986ba2013-08-14 12:44:16 +0300220 reg = readl(usbmisc->base + data->index * 4);
Marc Kleine-Buddee6091082013-03-30 12:53:59 +0200221 writel(reg | MX6_BM_OVER_CUR_DIS,
Sascha Hauer05986ba2013-08-14 12:44:16 +0300222 usbmisc->base + data->index * 4);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300223 spin_unlock_irqrestore(&usbmisc->lock, flags);
224 }
225
226 return 0;
227}
228
Stefan Agnerf40017e2014-09-22 08:14:15 +0800229static int usbmisc_vf610_init(struct imx_usbmisc_data *data)
230{
231 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
232 u32 reg;
233
234 /*
235 * Vybrid only has one misc register set, but in two different
236 * areas. These is reflected in two instances of this driver.
237 */
238 if (data->index >= 1)
239 return -EINVAL;
240
241 if (data->disable_oc) {
242 reg = readl(usbmisc->base);
243 writel(reg | VF610_OVER_CUR_DIS, usbmisc->base);
244 }
245
246 return 0;
247}
248
Michael Grzeschika0685332013-03-30 12:54:01 +0200249static const struct usbmisc_ops imx25_usbmisc_ops = {
Denis Carikli72ee92d2014-04-23 15:56:36 +0800250 .init = usbmisc_imx25_init,
Michael Grzeschika0685332013-03-30 12:54:01 +0200251 .post = usbmisc_imx25_post,
252};
253
Alexander Shiyan9f90e112013-12-06 16:35:14 +0800254static const struct usbmisc_ops imx27_usbmisc_ops = {
255 .init = usbmisc_imx27_init,
256};
257
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200258static const struct usbmisc_ops imx53_usbmisc_ops = {
259 .init = usbmisc_imx53_init,
260};
261
Richard Zhaod142d6b2012-09-12 14:58:05 +0300262static const struct usbmisc_ops imx6q_usbmisc_ops = {
263 .init = usbmisc_imx6q_init,
264};
265
Stefan Agnerf40017e2014-09-22 08:14:15 +0800266static const struct usbmisc_ops vf610_usbmisc_ops = {
267 .init = usbmisc_vf610_init,
268};
269
Sascha Hauer05986ba2013-08-14 12:44:16 +0300270int imx_usbmisc_init(struct imx_usbmisc_data *data)
271{
Stefan Agnerf40017e2014-09-22 08:14:15 +0800272 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
273
Sascha Hauer05986ba2013-08-14 12:44:16 +0300274 if (!usbmisc->ops->init)
275 return 0;
276 return usbmisc->ops->init(data);
277}
278EXPORT_SYMBOL_GPL(imx_usbmisc_init);
279
280int imx_usbmisc_init_post(struct imx_usbmisc_data *data)
281{
Stefan Agnerf40017e2014-09-22 08:14:15 +0800282 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
283
Sascha Hauer05986ba2013-08-14 12:44:16 +0300284 if (!usbmisc->ops->post)
285 return 0;
286 return usbmisc->ops->post(data);
287}
288EXPORT_SYMBOL_GPL(imx_usbmisc_init_post);
289
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200290static const struct of_device_id usbmisc_imx_dt_ids[] = {
Marc Kleine-Buddee6091082013-03-30 12:53:59 +0200291 {
Michael Grzeschika0685332013-03-30 12:54:01 +0200292 .compatible = "fsl,imx25-usbmisc",
293 .data = &imx25_usbmisc_ops,
294 },
295 {
Denis Carikli72ee92d2014-04-23 15:56:36 +0800296 .compatible = "fsl,imx35-usbmisc",
297 .data = &imx25_usbmisc_ops,
298 },
299 {
Alexander Shiyan9f90e112013-12-06 16:35:14 +0800300 .compatible = "fsl,imx27-usbmisc",
301 .data = &imx27_usbmisc_ops,
302 },
303 {
Alexander Shiyanc4962e02013-12-06 16:35:15 +0800304 .compatible = "fsl,imx51-usbmisc",
305 .data = &imx53_usbmisc_ops,
306 },
307 {
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200308 .compatible = "fsl,imx53-usbmisc",
309 .data = &imx53_usbmisc_ops,
310 },
311 {
Marc Kleine-Buddee6091082013-03-30 12:53:59 +0200312 .compatible = "fsl,imx6q-usbmisc",
313 .data = &imx6q_usbmisc_ops,
314 },
Stefan Agnerf40017e2014-09-22 08:14:15 +0800315 {
316 .compatible = "fsl,vf610-usbmisc",
317 .data = &vf610_usbmisc_ops,
318 },
Richard Zhaod142d6b2012-09-12 14:58:05 +0300319 { /* sentinel */ }
320};
Arnaud Patard (Rtp)269b83d2013-06-20 23:33:25 +0200321MODULE_DEVICE_TABLE(of, usbmisc_imx_dt_ids);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300322
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200323static int usbmisc_imx_probe(struct platform_device *pdev)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300324{
325 struct resource *res;
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200326 struct imx_usbmisc *data;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300327 int ret;
Marc Kleine-Buddee6091082013-03-30 12:53:59 +0200328 struct of_device_id *tmp_dev;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300329
Richard Zhaod142d6b2012-09-12 14:58:05 +0300330 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
331 if (!data)
332 return -ENOMEM;
333
334 spin_lock_init(&data->lock);
335
336 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding148e1132013-01-21 11:09:22 +0100337 data->base = devm_ioremap_resource(&pdev->dev, res);
338 if (IS_ERR(data->base))
339 return PTR_ERR(data->base);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300340
341 data->clk = devm_clk_get(&pdev->dev, NULL);
342 if (IS_ERR(data->clk)) {
343 dev_err(&pdev->dev,
344 "failed to get clock, err=%ld\n", PTR_ERR(data->clk));
345 return PTR_ERR(data->clk);
346 }
347
348 ret = clk_prepare_enable(data->clk);
349 if (ret) {
350 dev_err(&pdev->dev,
351 "clk_prepare_enable failed, err=%d\n", ret);
352 return ret;
353 }
354
Marc Kleine-Buddee6091082013-03-30 12:53:59 +0200355 tmp_dev = (struct of_device_id *)
356 of_match_device(usbmisc_imx_dt_ids, &pdev->dev);
357 data->ops = (const struct usbmisc_ops *)tmp_dev->data;
Stefan Agnerf40017e2014-09-22 08:14:15 +0800358 platform_set_drvdata(pdev, data);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300359
Richard Zhaod142d6b2012-09-12 14:58:05 +0300360 return 0;
361}
362
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200363static int usbmisc_imx_remove(struct platform_device *pdev)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300364{
Stefan Agnerf40017e2014-09-22 08:14:15 +0800365 struct imx_usbmisc *usbmisc = dev_get_drvdata(&pdev->dev);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300366 clk_disable_unprepare(usbmisc->clk);
367 return 0;
368}
369
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200370static struct platform_driver usbmisc_imx_driver = {
371 .probe = usbmisc_imx_probe,
372 .remove = usbmisc_imx_remove,
Richard Zhaod142d6b2012-09-12 14:58:05 +0300373 .driver = {
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200374 .name = "usbmisc_imx",
Richard Zhaod142d6b2012-09-12 14:58:05 +0300375 .owner = THIS_MODULE,
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200376 .of_match_table = usbmisc_imx_dt_ids,
Richard Zhaod142d6b2012-09-12 14:58:05 +0300377 },
378};
379
Philipp Zabel0404ae02013-06-13 17:59:59 +0300380module_platform_driver(usbmisc_imx_driver);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300381
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200382MODULE_ALIAS("platform:usbmisc-imx");
Richard Zhaod142d6b2012-09-12 14:58:05 +0300383MODULE_LICENSE("GPL v2");
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200384MODULE_DESCRIPTION("driver for imx usb non-core registers");
Richard Zhaod142d6b2012-09-12 14:58:05 +0300385MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");