blob: 8cdf0af156c6ad7c8cf811c075586b8cbeb454a6 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Richard Zhaod142d6b2012-09-12 14:58:05 +03002/*
3 * Copyright 2012 Freescale Semiconductor, Inc.
Richard Zhaod142d6b2012-09-12 14:58:05 +03004 */
5
6#include <linux/module.h>
7#include <linux/of_platform.h>
Richard Zhaod142d6b2012-09-12 14:58:05 +03008#include <linux/err.h>
9#include <linux/io.h>
Michael Grzeschika0685332013-03-30 12:54:01 +020010#include <linux/delay.h>
Richard Zhaod142d6b2012-09-12 14:58:05 +030011
Alexander Shishkin8e229782013-06-24 14:46:36 +030012#include "ci_hdrc_imx.h"
Richard Zhaod142d6b2012-09-12 14:58:05 +030013
Michael Grzeschika0685332013-03-30 12:54:01 +020014#define MX25_USB_PHY_CTRL_OFFSET 0x08
15#define MX25_BM_EXTERNAL_VBUS_DIVIDER BIT(23)
16
Denis Carikli72ee92d2014-04-23 15:56:36 +080017#define MX25_EHCI_INTERFACE_SINGLE_UNI (2 << 0)
18#define MX25_EHCI_INTERFACE_DIFF_UNI (0 << 0)
19#define MX25_EHCI_INTERFACE_MASK (0xf)
20
21#define MX25_OTG_SIC_SHIFT 29
22#define MX25_OTG_SIC_MASK (0x3 << MX25_OTG_SIC_SHIFT)
23#define MX25_OTG_PM_BIT BIT(24)
24#define MX25_OTG_PP_BIT BIT(11)
25#define MX25_OTG_OCPOL_BIT BIT(3)
26
27#define MX25_H1_SIC_SHIFT 21
28#define MX25_H1_SIC_MASK (0x3 << MX25_H1_SIC_SHIFT)
29#define MX25_H1_PP_BIT BIT(18)
30#define MX25_H1_PM_BIT BIT(16)
31#define MX25_H1_IPPUE_UP_BIT BIT(7)
32#define MX25_H1_IPPUE_DOWN_BIT BIT(6)
33#define MX25_H1_TLL_BIT BIT(5)
34#define MX25_H1_USBTE_BIT BIT(4)
35#define MX25_H1_OCPOL_BIT BIT(2)
36
Alexander Shiyan9f90e112013-12-06 16:35:14 +080037#define MX27_H1_PM_BIT BIT(8)
38#define MX27_H2_PM_BIT BIT(16)
39#define MX27_OTG_PM_BIT BIT(24)
40
Michael Grzeschikf0c910b2013-03-30 12:54:00 +020041#define MX53_USB_OTG_PHY_CTRL_0_OFFSET 0x08
Fabio Estevam33f92a82014-05-04 09:24:39 +080042#define MX53_USB_OTG_PHY_CTRL_1_OFFSET 0x0c
Fabien Lahoudered13631b2016-09-26 13:14:19 +020043#define MX53_USB_CTRL_1_OFFSET 0x10
44#define MX53_USB_CTRL_1_H2_XCVR_CLK_SEL_MASK (0x11 << 2)
45#define MX53_USB_CTRL_1_H2_XCVR_CLK_SEL_ULPI BIT(2)
46#define MX53_USB_CTRL_1_H3_XCVR_CLK_SEL_MASK (0x11 << 6)
47#define MX53_USB_CTRL_1_H3_XCVR_CLK_SEL_ULPI BIT(6)
Michael Grzeschikf0c910b2013-03-30 12:54:00 +020048#define MX53_USB_UH2_CTRL_OFFSET 0x14
49#define MX53_USB_UH3_CTRL_OFFSET 0x18
Fabien Lahoudere3be32512016-09-26 13:14:20 +020050#define MX53_USB_CLKONOFF_CTRL_OFFSET 0x24
51#define MX53_USB_CLKONOFF_CTRL_H2_INT60CKOFF BIT(21)
52#define MX53_USB_CLKONOFF_CTRL_H3_INT60CKOFF BIT(22)
Michael Grzeschikf0c910b2013-03-30 12:54:00 +020053#define MX53_BM_OVER_CUR_DIS_H1 BIT(5)
54#define MX53_BM_OVER_CUR_DIS_OTG BIT(8)
55#define MX53_BM_OVER_CUR_DIS_UHx BIT(30)
Fabien Lahoudered13631b2016-09-26 13:14:19 +020056#define MX53_USB_CTRL_1_UH2_ULPI_EN BIT(26)
57#define MX53_USB_CTRL_1_UH3_ULPI_EN BIT(27)
58#define MX53_USB_UHx_CTRL_WAKE_UP_EN BIT(7)
59#define MX53_USB_UHx_CTRL_ULPI_INT_EN BIT(8)
Fabio Estevam33f92a82014-05-04 09:24:39 +080060#define MX53_USB_PHYCTRL1_PLLDIV_MASK 0x3
61#define MX53_USB_PLL_DIV_24_MHZ 0x01
Michael Grzeschikf0c910b2013-03-30 12:54:00 +020062
Peter Chened6e5eb2014-11-05 14:58:32 +080063#define MX6_BM_NON_BURST_SETTING BIT(1)
Marc Kleine-Buddee6091082013-03-30 12:53:59 +020064#define MX6_BM_OVER_CUR_DIS BIT(7)
Li Jun9dba5162016-07-20 16:02:42 +080065#define MX6_BM_OVER_CUR_POLARITY BIT(8)
Peter Chenf636cec2015-02-11 12:44:46 +080066#define MX6_BM_WAKEUP_ENABLE BIT(10)
67#define MX6_BM_ID_WAKEUP BIT(16)
68#define MX6_BM_VBUS_WAKEUP BIT(17)
Peter Chen8721a752015-02-11 12:44:53 +080069#define MX6SX_BM_DPDM_WAKEUP_EN BIT(29)
Peter Chenf636cec2015-02-11 12:44:46 +080070#define MX6_BM_WAKEUP_INTR BIT(31)
Peter Chen8721a752015-02-11 12:44:53 +080071#define MX6_USB_OTG1_PHY_CTRL 0x18
72/* For imx6dql, it is host-only controller, for later imx6, it is otg's */
73#define MX6_USB_OTG2_PHY_CTRL 0x1c
74#define MX6SX_USB_VBUS_WAKEUP_SOURCE(v) (v << 8)
75#define MX6SX_USB_VBUS_WAKEUP_SOURCE_VBUS MX6SX_USB_VBUS_WAKEUP_SOURCE(0)
76#define MX6SX_USB_VBUS_WAKEUP_SOURCE_AVALID MX6SX_USB_VBUS_WAKEUP_SOURCE(1)
77#define MX6SX_USB_VBUS_WAKEUP_SOURCE_BVALID MX6SX_USB_VBUS_WAKEUP_SOURCE(2)
78#define MX6SX_USB_VBUS_WAKEUP_SOURCE_SESS_END MX6SX_USB_VBUS_WAKEUP_SOURCE(3)
Richard Zhaod142d6b2012-09-12 14:58:05 +030079
Stefan Agnerf40017e2014-09-22 08:14:15 +080080#define VF610_OVER_CUR_DIS BIT(7)
81
Peter Chen5cb377c2015-09-09 16:33:02 +080082#define MX7D_USBNC_USB_CTRL2 0x4
83#define MX7D_USB_VBUS_WAKEUP_SOURCE_MASK 0x3
84#define MX7D_USB_VBUS_WAKEUP_SOURCE(v) (v << 0)
85#define MX7D_USB_VBUS_WAKEUP_SOURCE_VBUS MX7D_USB_VBUS_WAKEUP_SOURCE(0)
86#define MX7D_USB_VBUS_WAKEUP_SOURCE_AVALID MX7D_USB_VBUS_WAKEUP_SOURCE(1)
87#define MX7D_USB_VBUS_WAKEUP_SOURCE_BVALID MX7D_USB_VBUS_WAKEUP_SOURCE(2)
88#define MX7D_USB_VBUS_WAKEUP_SOURCE_SESS_END MX7D_USB_VBUS_WAKEUP_SOURCE(3)
89
Sascha Hauer05986ba2013-08-14 12:44:16 +030090struct usbmisc_ops {
91 /* It's called once when probe a usb device */
92 int (*init)(struct imx_usbmisc_data *data);
93 /* It's called once after adding a usb device */
94 int (*post)(struct imx_usbmisc_data *data);
Peter Chenf636cec2015-02-11 12:44:46 +080095 /* It's called when we need to enable/disable usb wakeup */
96 int (*set_wakeup)(struct imx_usbmisc_data *data, bool enabled);
Sascha Hauer05986ba2013-08-14 12:44:16 +030097};
98
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +020099struct imx_usbmisc {
Richard Zhaod142d6b2012-09-12 14:58:05 +0300100 void __iomem *base;
101 spinlock_t lock;
Marc Kleine-Buddee6091082013-03-30 12:53:59 +0200102 const struct usbmisc_ops *ops;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300103};
104
Andrey Smirnov62b97d52017-05-15 06:48:58 -0700105static inline bool is_imx53_usbmisc(struct imx_usbmisc_data *data);
106
Denis Carikli72ee92d2014-04-23 15:56:36 +0800107static int usbmisc_imx25_init(struct imx_usbmisc_data *data)
108{
Stefan Agnerf40017e2014-09-22 08:14:15 +0800109 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
Denis Carikli72ee92d2014-04-23 15:56:36 +0800110 unsigned long flags;
111 u32 val = 0;
112
113 if (data->index > 1)
114 return -EINVAL;
115
116 spin_lock_irqsave(&usbmisc->lock, flags);
117 switch (data->index) {
118 case 0:
119 val = readl(usbmisc->base);
120 val &= ~(MX25_OTG_SIC_MASK | MX25_OTG_PP_BIT);
121 val |= (MX25_EHCI_INTERFACE_DIFF_UNI & MX25_EHCI_INTERFACE_MASK) << MX25_OTG_SIC_SHIFT;
122 val |= (MX25_OTG_PM_BIT | MX25_OTG_OCPOL_BIT);
123 writel(val, usbmisc->base);
124 break;
125 case 1:
126 val = readl(usbmisc->base);
127 val &= ~(MX25_H1_SIC_MASK | MX25_H1_PP_BIT | MX25_H1_IPPUE_UP_BIT);
128 val |= (MX25_EHCI_INTERFACE_SINGLE_UNI & MX25_EHCI_INTERFACE_MASK) << MX25_H1_SIC_SHIFT;
129 val |= (MX25_H1_PM_BIT | MX25_H1_OCPOL_BIT | MX25_H1_TLL_BIT |
130 MX25_H1_USBTE_BIT | MX25_H1_IPPUE_DOWN_BIT);
131
132 writel(val, usbmisc->base);
133
134 break;
135 }
136 spin_unlock_irqrestore(&usbmisc->lock, flags);
137
138 return 0;
139}
140
Sascha Hauer05986ba2013-08-14 12:44:16 +0300141static int usbmisc_imx25_post(struct imx_usbmisc_data *data)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300142{
Stefan Agnerf40017e2014-09-22 08:14:15 +0800143 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
Michael Grzeschika0685332013-03-30 12:54:01 +0200144 void __iomem *reg;
145 unsigned long flags;
146 u32 val;
147
Sascha Hauer05986ba2013-08-14 12:44:16 +0300148 if (data->index > 2)
149 return -EINVAL;
Michael Grzeschika0685332013-03-30 12:54:01 +0200150
Sascha Hauer05986ba2013-08-14 12:44:16 +0300151 if (data->evdo) {
Michael Grzeschika0685332013-03-30 12:54:01 +0200152 spin_lock_irqsave(&usbmisc->lock, flags);
Fabio Estevam8d1dc4d2014-11-26 13:44:25 +0800153 reg = usbmisc->base + MX25_USB_PHY_CTRL_OFFSET;
Michael Grzeschika0685332013-03-30 12:54:01 +0200154 val = readl(reg);
155 writel(val | MX25_BM_EXTERNAL_VBUS_DIVIDER, reg);
156 spin_unlock_irqrestore(&usbmisc->lock, flags);
157 usleep_range(5000, 10000); /* needed to stabilize voltage */
158 }
159
160 return 0;
161}
162
Alexander Shiyan9f90e112013-12-06 16:35:14 +0800163static int usbmisc_imx27_init(struct imx_usbmisc_data *data)
164{
Stefan Agnerf40017e2014-09-22 08:14:15 +0800165 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
Alexander Shiyan9f90e112013-12-06 16:35:14 +0800166 unsigned long flags;
167 u32 val;
168
169 switch (data->index) {
170 case 0:
171 val = MX27_OTG_PM_BIT;
172 break;
173 case 1:
174 val = MX27_H1_PM_BIT;
175 break;
176 case 2:
177 val = MX27_H2_PM_BIT;
178 break;
179 default:
180 return -EINVAL;
Fabio Estevamaa90e992015-05-09 12:15:24 -0300181 }
Alexander Shiyan9f90e112013-12-06 16:35:14 +0800182
183 spin_lock_irqsave(&usbmisc->lock, flags);
184 if (data->disable_oc)
185 val = readl(usbmisc->base) | val;
186 else
187 val = readl(usbmisc->base) & ~val;
188 writel(val, usbmisc->base);
189 spin_unlock_irqrestore(&usbmisc->lock, flags);
190
191 return 0;
192}
193
Sascha Hauer05986ba2013-08-14 12:44:16 +0300194static int usbmisc_imx53_init(struct imx_usbmisc_data *data)
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200195{
Stefan Agnerf40017e2014-09-22 08:14:15 +0800196 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200197 void __iomem *reg = NULL;
198 unsigned long flags;
199 u32 val = 0;
200
Sascha Hauer05986ba2013-08-14 12:44:16 +0300201 if (data->index > 3)
202 return -EINVAL;
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200203
Fabio Estevam33f92a82014-05-04 09:24:39 +0800204 /* Select a 24 MHz reference clock for the PHY */
Fabio Estevam4a1d6cf2014-11-26 13:44:26 +0800205 val = readl(usbmisc->base + MX53_USB_OTG_PHY_CTRL_1_OFFSET);
Fabio Estevam33f92a82014-05-04 09:24:39 +0800206 val &= ~MX53_USB_PHYCTRL1_PLLDIV_MASK;
207 val |= MX53_USB_PLL_DIV_24_MHZ;
208 writel(val, usbmisc->base + MX53_USB_OTG_PHY_CTRL_1_OFFSET);
209
Fabien Lahoudered13631b2016-09-26 13:14:19 +0200210 spin_lock_irqsave(&usbmisc->lock, flags);
211
212 switch (data->index) {
213 case 0:
214 if (data->disable_oc) {
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200215 reg = usbmisc->base + MX53_USB_OTG_PHY_CTRL_0_OFFSET;
216 val = readl(reg) | MX53_BM_OVER_CUR_DIS_OTG;
Fabien Lahoudered13631b2016-09-26 13:14:19 +0200217 writel(val, reg);
218 }
219 break;
220 case 1:
221 if (data->disable_oc) {
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200222 reg = usbmisc->base + MX53_USB_OTG_PHY_CTRL_0_OFFSET;
223 val = readl(reg) | MX53_BM_OVER_CUR_DIS_H1;
Fabien Lahoudered13631b2016-09-26 13:14:19 +0200224 writel(val, reg);
225 }
226 break;
227 case 2:
228 if (data->ulpi) {
229 /* set USBH2 into ULPI-mode. */
230 reg = usbmisc->base + MX53_USB_CTRL_1_OFFSET;
231 val = readl(reg) | MX53_USB_CTRL_1_UH2_ULPI_EN;
232 /* select ULPI clock */
233 val &= ~MX53_USB_CTRL_1_H2_XCVR_CLK_SEL_MASK;
234 val |= MX53_USB_CTRL_1_H2_XCVR_CLK_SEL_ULPI;
235 writel(val, reg);
236 /* Set interrupt wake up enable */
237 reg = usbmisc->base + MX53_USB_UH2_CTRL_OFFSET;
238 val = readl(reg) | MX53_USB_UHx_CTRL_WAKE_UP_EN
239 | MX53_USB_UHx_CTRL_ULPI_INT_EN;
240 writel(val, reg);
Andrey Smirnov62b97d52017-05-15 06:48:58 -0700241 if (is_imx53_usbmisc(data)) {
242 /* Disable internal 60Mhz clock */
243 reg = usbmisc->base +
244 MX53_USB_CLKONOFF_CTRL_OFFSET;
245 val = readl(reg) |
246 MX53_USB_CLKONOFF_CTRL_H2_INT60CKOFF;
247 writel(val, reg);
248 }
249
Fabien Lahoudered13631b2016-09-26 13:14:19 +0200250 }
251 if (data->disable_oc) {
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200252 reg = usbmisc->base + MX53_USB_UH2_CTRL_OFFSET;
253 val = readl(reg) | MX53_BM_OVER_CUR_DIS_UHx;
Fabien Lahoudered13631b2016-09-26 13:14:19 +0200254 writel(val, reg);
255 }
256 break;
257 case 3:
258 if (data->ulpi) {
259 /* set USBH3 into ULPI-mode. */
260 reg = usbmisc->base + MX53_USB_CTRL_1_OFFSET;
261 val = readl(reg) | MX53_USB_CTRL_1_UH3_ULPI_EN;
262 /* select ULPI clock */
263 val &= ~MX53_USB_CTRL_1_H3_XCVR_CLK_SEL_MASK;
264 val |= MX53_USB_CTRL_1_H3_XCVR_CLK_SEL_ULPI;
265 writel(val, reg);
266 /* Set interrupt wake up enable */
267 reg = usbmisc->base + MX53_USB_UH3_CTRL_OFFSET;
268 val = readl(reg) | MX53_USB_UHx_CTRL_WAKE_UP_EN
269 | MX53_USB_UHx_CTRL_ULPI_INT_EN;
270 writel(val, reg);
Andrey Smirnov62b97d52017-05-15 06:48:58 -0700271
272 if (is_imx53_usbmisc(data)) {
273 /* Disable internal 60Mhz clock */
274 reg = usbmisc->base +
275 MX53_USB_CLKONOFF_CTRL_OFFSET;
276 val = readl(reg) |
277 MX53_USB_CLKONOFF_CTRL_H3_INT60CKOFF;
278 writel(val, reg);
279 }
Fabien Lahoudered13631b2016-09-26 13:14:19 +0200280 }
281 if (data->disable_oc) {
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200282 reg = usbmisc->base + MX53_USB_UH3_CTRL_OFFSET;
283 val = readl(reg) | MX53_BM_OVER_CUR_DIS_UHx;
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200284 writel(val, reg);
Fabien Lahoudered13631b2016-09-26 13:14:19 +0200285 }
286 break;
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200287 }
288
Fabien Lahoudered13631b2016-09-26 13:14:19 +0200289 spin_unlock_irqrestore(&usbmisc->lock, flags);
290
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200291 return 0;
292}
293
Peter Chenf636cec2015-02-11 12:44:46 +0800294static int usbmisc_imx6q_set_wakeup
295 (struct imx_usbmisc_data *data, bool enabled)
296{
297 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
298 unsigned long flags;
299 u32 val;
300 u32 wakeup_setting = (MX6_BM_WAKEUP_ENABLE |
301 MX6_BM_VBUS_WAKEUP | MX6_BM_ID_WAKEUP);
302 int ret = 0;
303
304 if (data->index > 3)
305 return -EINVAL;
306
307 spin_lock_irqsave(&usbmisc->lock, flags);
308 val = readl(usbmisc->base + data->index * 4);
309 if (enabled) {
310 val |= wakeup_setting;
311 writel(val, usbmisc->base + data->index * 4);
312 } else {
313 if (val & MX6_BM_WAKEUP_INTR)
314 pr_debug("wakeup int at ci_hdrc.%d\n", data->index);
315 val &= ~wakeup_setting;
316 writel(val, usbmisc->base + data->index * 4);
317 }
318 spin_unlock_irqrestore(&usbmisc->lock, flags);
319
320 return ret;
321}
322
Sascha Hauer05986ba2013-08-14 12:44:16 +0300323static int usbmisc_imx6q_init(struct imx_usbmisc_data *data)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300324{
Stefan Agnerf40017e2014-09-22 08:14:15 +0800325 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300326 unsigned long flags;
327 u32 reg;
328
Sascha Hauer05986ba2013-08-14 12:44:16 +0300329 if (data->index > 3)
330 return -EINVAL;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300331
Peter Chened6e5eb2014-11-05 14:58:32 +0800332 spin_lock_irqsave(&usbmisc->lock, flags);
333
Li Jun9dba5162016-07-20 16:02:42 +0800334 reg = readl(usbmisc->base + data->index * 4);
Sascha Hauer05986ba2013-08-14 12:44:16 +0300335 if (data->disable_oc) {
Li Jun9dba5162016-07-20 16:02:42 +0800336 reg |= MX6_BM_OVER_CUR_DIS;
337 } else if (data->oc_polarity == 1) {
338 /* High active */
339 reg &= ~(MX6_BM_OVER_CUR_DIS | MX6_BM_OVER_CUR_POLARITY);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300340 }
Li Jun9dba5162016-07-20 16:02:42 +0800341 writel(reg, usbmisc->base + data->index * 4);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300342
Peter Chened6e5eb2014-11-05 14:58:32 +0800343 /* SoC non-burst setting */
344 reg = readl(usbmisc->base + data->index * 4);
345 writel(reg | MX6_BM_NON_BURST_SETTING,
346 usbmisc->base + data->index * 4);
347
348 spin_unlock_irqrestore(&usbmisc->lock, flags);
349
Peter Chenf636cec2015-02-11 12:44:46 +0800350 usbmisc_imx6q_set_wakeup(data, false);
351
Richard Zhaod142d6b2012-09-12 14:58:05 +0300352 return 0;
353}
354
Peter Chen8721a752015-02-11 12:44:53 +0800355static int usbmisc_imx6sx_init(struct imx_usbmisc_data *data)
356{
357 void __iomem *reg = NULL;
358 unsigned long flags;
359 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
360 u32 val;
Peter Chen8721a752015-02-11 12:44:53 +0800361
362 usbmisc_imx6q_init(data);
363
364 if (data->index == 0 || data->index == 1) {
365 reg = usbmisc->base + MX6_USB_OTG1_PHY_CTRL + data->index * 4;
366 spin_lock_irqsave(&usbmisc->lock, flags);
367 /* Set vbus wakeup source as bvalid */
368 val = readl(reg);
369 writel(val | MX6SX_USB_VBUS_WAKEUP_SOURCE_BVALID, reg);
370 /*
371 * Disable dp/dm wakeup in device mode when vbus is
372 * not there.
373 */
374 val = readl(usbmisc->base + data->index * 4);
375 writel(val & ~MX6SX_BM_DPDM_WAKEUP_EN,
376 usbmisc->base + data->index * 4);
377 spin_unlock_irqrestore(&usbmisc->lock, flags);
378 }
379
kbuild test robot1fcefbd2015-03-25 07:23:52 +0100380 return 0;
Peter Chen8721a752015-02-11 12:44:53 +0800381}
382
Stefan Agnerf40017e2014-09-22 08:14:15 +0800383static int usbmisc_vf610_init(struct imx_usbmisc_data *data)
384{
385 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
386 u32 reg;
387
388 /*
389 * Vybrid only has one misc register set, but in two different
390 * areas. These is reflected in two instances of this driver.
391 */
392 if (data->index >= 1)
393 return -EINVAL;
394
395 if (data->disable_oc) {
396 reg = readl(usbmisc->base);
397 writel(reg | VF610_OVER_CUR_DIS, usbmisc->base);
398 }
399
400 return 0;
401}
402
Peter Chen5cb377c2015-09-09 16:33:02 +0800403static int usbmisc_imx7d_set_wakeup
404 (struct imx_usbmisc_data *data, bool enabled)
405{
406 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
407 unsigned long flags;
408 u32 val;
409 u32 wakeup_setting = (MX6_BM_WAKEUP_ENABLE |
410 MX6_BM_VBUS_WAKEUP | MX6_BM_ID_WAKEUP);
411
412 spin_lock_irqsave(&usbmisc->lock, flags);
413 val = readl(usbmisc->base);
414 if (enabled) {
415 writel(val | wakeup_setting, usbmisc->base);
416 } else {
417 if (val & MX6_BM_WAKEUP_INTR)
418 dev_dbg(data->dev, "wakeup int\n");
419 writel(val & ~wakeup_setting, usbmisc->base);
420 }
421 spin_unlock_irqrestore(&usbmisc->lock, flags);
422
423 return 0;
424}
425
426static int usbmisc_imx7d_init(struct imx_usbmisc_data *data)
427{
428 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
429 unsigned long flags;
430 u32 reg;
431
432 if (data->index >= 1)
433 return -EINVAL;
434
435 spin_lock_irqsave(&usbmisc->lock, flags);
Li Jun9dba5162016-07-20 16:02:42 +0800436 reg = readl(usbmisc->base);
Peter Chen5cb377c2015-09-09 16:33:02 +0800437 if (data->disable_oc) {
Li Jun9dba5162016-07-20 16:02:42 +0800438 reg |= MX6_BM_OVER_CUR_DIS;
439 } else if (data->oc_polarity == 1) {
440 /* High active */
441 reg &= ~(MX6_BM_OVER_CUR_DIS | MX6_BM_OVER_CUR_POLARITY);
Peter Chen5cb377c2015-09-09 16:33:02 +0800442 }
Li Jun9dba5162016-07-20 16:02:42 +0800443 writel(reg, usbmisc->base);
Peter Chen5cb377c2015-09-09 16:33:02 +0800444
445 reg = readl(usbmisc->base + MX7D_USBNC_USB_CTRL2);
446 reg &= ~MX7D_USB_VBUS_WAKEUP_SOURCE_MASK;
447 writel(reg | MX7D_USB_VBUS_WAKEUP_SOURCE_BVALID,
448 usbmisc->base + MX7D_USBNC_USB_CTRL2);
449 spin_unlock_irqrestore(&usbmisc->lock, flags);
450
451 usbmisc_imx7d_set_wakeup(data, false);
452
453 return 0;
454}
455
Michael Grzeschika0685332013-03-30 12:54:01 +0200456static const struct usbmisc_ops imx25_usbmisc_ops = {
Denis Carikli72ee92d2014-04-23 15:56:36 +0800457 .init = usbmisc_imx25_init,
Michael Grzeschika0685332013-03-30 12:54:01 +0200458 .post = usbmisc_imx25_post,
459};
460
Alexander Shiyan9f90e112013-12-06 16:35:14 +0800461static const struct usbmisc_ops imx27_usbmisc_ops = {
462 .init = usbmisc_imx27_init,
463};
464
Andrey Smirnov62b97d52017-05-15 06:48:58 -0700465static const struct usbmisc_ops imx51_usbmisc_ops = {
466 .init = usbmisc_imx53_init,
467};
468
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200469static const struct usbmisc_ops imx53_usbmisc_ops = {
470 .init = usbmisc_imx53_init,
471};
472
Richard Zhaod142d6b2012-09-12 14:58:05 +0300473static const struct usbmisc_ops imx6q_usbmisc_ops = {
Peter Chenf636cec2015-02-11 12:44:46 +0800474 .set_wakeup = usbmisc_imx6q_set_wakeup,
Richard Zhaod142d6b2012-09-12 14:58:05 +0300475 .init = usbmisc_imx6q_init,
476};
477
Stefan Agnerf40017e2014-09-22 08:14:15 +0800478static const struct usbmisc_ops vf610_usbmisc_ops = {
479 .init = usbmisc_vf610_init,
480};
481
Peter Chen8721a752015-02-11 12:44:53 +0800482static const struct usbmisc_ops imx6sx_usbmisc_ops = {
483 .set_wakeup = usbmisc_imx6q_set_wakeup,
484 .init = usbmisc_imx6sx_init,
485};
486
Peter Chen5cb377c2015-09-09 16:33:02 +0800487static const struct usbmisc_ops imx7d_usbmisc_ops = {
488 .init = usbmisc_imx7d_init,
489 .set_wakeup = usbmisc_imx7d_set_wakeup,
490};
491
Andrey Smirnov62b97d52017-05-15 06:48:58 -0700492static inline bool is_imx53_usbmisc(struct imx_usbmisc_data *data)
493{
494 struct imx_usbmisc *usbmisc = dev_get_drvdata(data->dev);
495
496 return usbmisc->ops == &imx53_usbmisc_ops;
497}
498
Sascha Hauer05986ba2013-08-14 12:44:16 +0300499int imx_usbmisc_init(struct imx_usbmisc_data *data)
500{
Peter Chena4cf1b12015-02-11 12:44:44 +0800501 struct imx_usbmisc *usbmisc;
Stefan Agnerf40017e2014-09-22 08:14:15 +0800502
Peter Chena4cf1b12015-02-11 12:44:44 +0800503 if (!data)
504 return 0;
505
506 usbmisc = dev_get_drvdata(data->dev);
Sascha Hauer05986ba2013-08-14 12:44:16 +0300507 if (!usbmisc->ops->init)
508 return 0;
509 return usbmisc->ops->init(data);
510}
511EXPORT_SYMBOL_GPL(imx_usbmisc_init);
512
513int imx_usbmisc_init_post(struct imx_usbmisc_data *data)
514{
Peter Chena4cf1b12015-02-11 12:44:44 +0800515 struct imx_usbmisc *usbmisc;
Stefan Agnerf40017e2014-09-22 08:14:15 +0800516
Peter Chena4cf1b12015-02-11 12:44:44 +0800517 if (!data)
518 return 0;
519
520 usbmisc = dev_get_drvdata(data->dev);
Sascha Hauer05986ba2013-08-14 12:44:16 +0300521 if (!usbmisc->ops->post)
522 return 0;
523 return usbmisc->ops->post(data);
524}
525EXPORT_SYMBOL_GPL(imx_usbmisc_init_post);
526
Peter Chenf636cec2015-02-11 12:44:46 +0800527int imx_usbmisc_set_wakeup(struct imx_usbmisc_data *data, bool enabled)
528{
529 struct imx_usbmisc *usbmisc;
530
531 if (!data)
532 return 0;
533
534 usbmisc = dev_get_drvdata(data->dev);
535 if (!usbmisc->ops->set_wakeup)
536 return 0;
537 return usbmisc->ops->set_wakeup(data, enabled);
538}
539EXPORT_SYMBOL_GPL(imx_usbmisc_set_wakeup);
540
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200541static const struct of_device_id usbmisc_imx_dt_ids[] = {
Marc Kleine-Buddee6091082013-03-30 12:53:59 +0200542 {
Michael Grzeschika0685332013-03-30 12:54:01 +0200543 .compatible = "fsl,imx25-usbmisc",
544 .data = &imx25_usbmisc_ops,
545 },
546 {
Denis Carikli72ee92d2014-04-23 15:56:36 +0800547 .compatible = "fsl,imx35-usbmisc",
548 .data = &imx25_usbmisc_ops,
549 },
550 {
Alexander Shiyan9f90e112013-12-06 16:35:14 +0800551 .compatible = "fsl,imx27-usbmisc",
552 .data = &imx27_usbmisc_ops,
553 },
554 {
Alexander Shiyanc4962e02013-12-06 16:35:15 +0800555 .compatible = "fsl,imx51-usbmisc",
Andrey Smirnov62b97d52017-05-15 06:48:58 -0700556 .data = &imx51_usbmisc_ops,
Alexander Shiyanc4962e02013-12-06 16:35:15 +0800557 },
558 {
Michael Grzeschikf0c910b2013-03-30 12:54:00 +0200559 .compatible = "fsl,imx53-usbmisc",
560 .data = &imx53_usbmisc_ops,
561 },
562 {
Marc Kleine-Buddee6091082013-03-30 12:53:59 +0200563 .compatible = "fsl,imx6q-usbmisc",
564 .data = &imx6q_usbmisc_ops,
565 },
Stefan Agnerf40017e2014-09-22 08:14:15 +0800566 {
567 .compatible = "fsl,vf610-usbmisc",
568 .data = &vf610_usbmisc_ops,
569 },
Peter Chen8721a752015-02-11 12:44:53 +0800570 {
571 .compatible = "fsl,imx6sx-usbmisc",
572 .data = &imx6sx_usbmisc_ops,
573 },
Peter Chen52fe5682015-09-16 15:52:32 +0800574 {
575 .compatible = "fsl,imx6ul-usbmisc",
576 .data = &imx6sx_usbmisc_ops,
577 },
Li Jun9dba5162016-07-20 16:02:42 +0800578 {
579 .compatible = "fsl,imx7d-usbmisc",
580 .data = &imx7d_usbmisc_ops,
581 },
Richard Zhaod142d6b2012-09-12 14:58:05 +0300582 { /* sentinel */ }
583};
Arnaud Patard (Rtp)269b83d2013-06-20 23:33:25 +0200584MODULE_DEVICE_TABLE(of, usbmisc_imx_dt_ids);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300585
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200586static int usbmisc_imx_probe(struct platform_device *pdev)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300587{
588 struct resource *res;
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200589 struct imx_usbmisc *data;
LABBE Corentin090bc262015-11-12 08:43:33 +0100590 const struct of_device_id *of_id;
591
592 of_id = of_match_device(usbmisc_imx_dt_ids, &pdev->dev);
593 if (!of_id)
594 return -ENODEV;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300595
Richard Zhaod142d6b2012-09-12 14:58:05 +0300596 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
597 if (!data)
598 return -ENOMEM;
599
600 spin_lock_init(&data->lock);
601
602 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding148e1132013-01-21 11:09:22 +0100603 data->base = devm_ioremap_resource(&pdev->dev, res);
604 if (IS_ERR(data->base))
605 return PTR_ERR(data->base);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300606
LABBE Corentin090bc262015-11-12 08:43:33 +0100607 data->ops = (const struct usbmisc_ops *)of_id->data;
Stefan Agnerf40017e2014-09-22 08:14:15 +0800608 platform_set_drvdata(pdev, data);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300609
Richard Zhaod142d6b2012-09-12 14:58:05 +0300610 return 0;
611}
612
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200613static int usbmisc_imx_remove(struct platform_device *pdev)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300614{
Richard Zhaod142d6b2012-09-12 14:58:05 +0300615 return 0;
616}
617
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200618static struct platform_driver usbmisc_imx_driver = {
619 .probe = usbmisc_imx_probe,
620 .remove = usbmisc_imx_remove,
Richard Zhaod142d6b2012-09-12 14:58:05 +0300621 .driver = {
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200622 .name = "usbmisc_imx",
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200623 .of_match_table = usbmisc_imx_dt_ids,
Richard Zhaod142d6b2012-09-12 14:58:05 +0300624 },
625};
626
Philipp Zabel0404ae02013-06-13 17:59:59 +0300627module_platform_driver(usbmisc_imx_driver);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300628
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200629MODULE_ALIAS("platform:usbmisc-imx");
Richard Zhaod142d6b2012-09-12 14:58:05 +0300630MODULE_LICENSE("GPL v2");
Michael Grzeschika7bc2fd2013-03-30 12:53:56 +0200631MODULE_DESCRIPTION("driver for imx usb non-core registers");
Richard Zhaod142d6b2012-09-12 14:58:05 +0300632MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");