Merge tag 'u-boot-imx-20190426' of git://git.denx.de/u-boot-imx
[platform/kernel/u-boot.git] / drivers / usb / host / ehci-mx5.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
4  * Copyright (C) 2010 Freescale Semiconductor, Inc.
5  */
6
7 #include <common.h>
8 #include <usb.h>
9 #include <errno.h>
10 #include <linux/compiler.h>
11 #include <usb/ehci-ci.h>
12 #include <asm/io.h>
13 #include <asm/arch/imx-regs.h>
14 #include <asm/arch/clock.h>
15 #include <dm.h>
16 #include <power/regulator.h>
17
18 #include "ehci.h"
19
20 #define MX5_USBOTHER_REGS_OFFSET 0x800
21
22
23 #define MXC_OTG_OFFSET                  0
24 #define MXC_H1_OFFSET                   0x200
25 #define MXC_H2_OFFSET                   0x400
26 #define MXC_H3_OFFSET                   0x600
27
28 #define MXC_USBCTRL_OFFSET              0
29 #define MXC_USB_PHY_CTR_FUNC_OFFSET     0x8
30 #define MXC_USB_PHY_CTR_FUNC2_OFFSET    0xc
31 #define MXC_USB_CTRL_1_OFFSET           0x10
32 #define MXC_USBH2CTRL_OFFSET            0x14
33 #define MXC_USBH3CTRL_OFFSET            0x18
34
35 /* USB_CTRL */
36 /* OTG wakeup intr enable */
37 #define MXC_OTG_UCTRL_OWIE_BIT          (1 << 27)
38 /* OTG power mask */
39 #define MXC_OTG_UCTRL_OPM_BIT           (1 << 24)
40 /* OTG power pin polarity */
41 #define MXC_OTG_UCTRL_O_PWR_POL_BIT     (1 << 24)
42 /* Host1 ULPI interrupt enable */
43 #define MXC_H1_UCTRL_H1UIE_BIT          (1 << 12)
44 /* HOST1 wakeup intr enable */
45 #define MXC_H1_UCTRL_H1WIE_BIT          (1 << 11)
46 /* HOST1 power mask */
47 #define MXC_H1_UCTRL_H1PM_BIT           (1 << 8)
48 /* HOST1 power pin polarity */
49 #define MXC_H1_UCTRL_H1_PWR_POL_BIT     (1 << 8)
50
51 /* USB_PHY_CTRL_FUNC */
52 /* OTG Polarity of Overcurrent */
53 #define MXC_OTG_PHYCTRL_OC_POL_BIT      (1 << 9)
54 /* OTG Disable Overcurrent Event */
55 #define MXC_OTG_PHYCTRL_OC_DIS_BIT      (1 << 8)
56 /* UH1 Polarity of Overcurrent */
57 #define MXC_H1_OC_POL_BIT               (1 << 6)
58 /* UH1 Disable Overcurrent Event */
59 #define MXC_H1_OC_DIS_BIT               (1 << 5)
60 /* OTG Power Pin Polarity */
61 #define MXC_OTG_PHYCTRL_PWR_POL_BIT     (1 << 3)
62
63 /* USBH2CTRL */
64 #define MXC_H2_UCTRL_H2_OC_POL_BIT      (1 << 31)
65 #define MXC_H2_UCTRL_H2_OC_DIS_BIT      (1 << 30)
66 #define MXC_H2_UCTRL_H2UIE_BIT          (1 << 8)
67 #define MXC_H2_UCTRL_H2WIE_BIT          (1 << 7)
68 #define MXC_H2_UCTRL_H2PM_BIT           (1 << 4)
69 #define MXC_H2_UCTRL_H2_PWR_POL_BIT     (1 << 4)
70
71 /* USBH3CTRL */
72 #define MXC_H3_UCTRL_H3_OC_POL_BIT      (1 << 31)
73 #define MXC_H3_UCTRL_H3_OC_DIS_BIT      (1 << 30)
74 #define MXC_H3_UCTRL_H3UIE_BIT          (1 << 8)
75 #define MXC_H3_UCTRL_H3WIE_BIT          (1 << 7)
76 #define MXC_H3_UCTRL_H3_PWR_POL_BIT     (1 << 4)
77
78 /* USB_CTRL_1 */
79 #define MXC_USB_CTRL_UH1_EXT_CLK_EN     (1 << 25)
80
81 int mxc_set_usbcontrol(int port, unsigned int flags)
82 {
83         unsigned int v;
84         void __iomem *usb_base = (void __iomem *)OTG_BASE_ADDR;
85         void __iomem *usbother_base;
86         int ret = 0;
87
88         usbother_base = usb_base + MX5_USBOTHER_REGS_OFFSET;
89
90         switch (port) {
91         case 0: /* OTG port */
92                 if (flags & MXC_EHCI_INTERNAL_PHY) {
93                         v = __raw_readl(usbother_base +
94                                         MXC_USB_PHY_CTR_FUNC_OFFSET);
95                         if (flags & MXC_EHCI_OC_PIN_ACTIVE_LOW)
96                                 v |= MXC_OTG_PHYCTRL_OC_POL_BIT;
97                         else
98                                 v &= ~MXC_OTG_PHYCTRL_OC_POL_BIT;
99                         if (flags & MXC_EHCI_POWER_PINS_ENABLED)
100                                 /* OC/USBPWR is used */
101                                 v &= ~MXC_OTG_PHYCTRL_OC_DIS_BIT;
102                         else
103                                 /* OC/USBPWR is not used */
104                                 v |= MXC_OTG_PHYCTRL_OC_DIS_BIT;
105 #ifdef CONFIG_MX51
106                         if (flags & MXC_EHCI_PWR_PIN_ACTIVE_HIGH)
107                                 v |= MXC_OTG_PHYCTRL_PWR_POL_BIT;
108                         else
109                                 v &= ~MXC_OTG_PHYCTRL_PWR_POL_BIT;
110 #endif
111                         __raw_writel(v, usbother_base +
112                                         MXC_USB_PHY_CTR_FUNC_OFFSET);
113
114                         v = __raw_readl(usbother_base + MXC_USBCTRL_OFFSET);
115 #ifdef CONFIG_MX51
116                         if (flags & MXC_EHCI_POWER_PINS_ENABLED)
117                                 v &= ~MXC_OTG_UCTRL_OPM_BIT;
118                         else
119                                 v |= MXC_OTG_UCTRL_OPM_BIT;
120 #endif
121 #ifdef CONFIG_MX53
122                         if (flags & MXC_EHCI_PWR_PIN_ACTIVE_HIGH)
123                                 v |= MXC_OTG_UCTRL_O_PWR_POL_BIT;
124                         else
125                                 v &= ~MXC_OTG_UCTRL_O_PWR_POL_BIT;
126 #endif
127                         __raw_writel(v, usbother_base + MXC_USBCTRL_OFFSET);
128                 }
129                 break;
130         case 1: /* Host 1 ULPI */
131 #ifdef CONFIG_MX51
132                 /* The clock for the USBH1 ULPI port will come externally
133                    from the PHY. */
134                 v = __raw_readl(usbother_base + MXC_USB_CTRL_1_OFFSET);
135                 __raw_writel(v | MXC_USB_CTRL_UH1_EXT_CLK_EN, usbother_base +
136                                 MXC_USB_CTRL_1_OFFSET);
137 #endif
138
139                 v = __raw_readl(usbother_base + MXC_USBCTRL_OFFSET);
140 #ifdef CONFIG_MX51
141                 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
142                         v &= ~MXC_H1_UCTRL_H1PM_BIT; /* H1 power mask unused */
143                 else
144                         v |= MXC_H1_UCTRL_H1PM_BIT; /* H1 power mask used */
145 #endif
146 #ifdef CONFIG_MX53
147                 if (flags & MXC_EHCI_PWR_PIN_ACTIVE_HIGH)
148                         v |= MXC_H1_UCTRL_H1_PWR_POL_BIT;
149                 else
150                         v &= ~MXC_H1_UCTRL_H1_PWR_POL_BIT;
151 #endif
152                 __raw_writel(v, usbother_base + MXC_USBCTRL_OFFSET);
153
154                 v = __raw_readl(usbother_base + MXC_USB_PHY_CTR_FUNC_OFFSET);
155                 if (flags & MXC_EHCI_OC_PIN_ACTIVE_LOW)
156                         v |= MXC_H1_OC_POL_BIT;
157                 else
158                         v &= ~MXC_H1_OC_POL_BIT;
159                 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
160                         v &= ~MXC_H1_OC_DIS_BIT; /* OC is used */
161                 else
162                         v |= MXC_H1_OC_DIS_BIT; /* OC is not used */
163                 __raw_writel(v, usbother_base + MXC_USB_PHY_CTR_FUNC_OFFSET);
164
165                 break;
166         case 2: /* Host 2 ULPI */
167                 v = __raw_readl(usbother_base + MXC_USBH2CTRL_OFFSET);
168 #ifdef CONFIG_MX51
169                 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
170                         v &= ~MXC_H2_UCTRL_H2PM_BIT; /* H2 power mask unused */
171                 else
172                         v |= MXC_H2_UCTRL_H2PM_BIT; /* H2 power mask used */
173 #endif
174 #ifdef CONFIG_MX53
175                 if (flags & MXC_EHCI_OC_PIN_ACTIVE_LOW)
176                         v |= MXC_H2_UCTRL_H2_OC_POL_BIT;
177                 else
178                         v &= ~MXC_H2_UCTRL_H2_OC_POL_BIT;
179                 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
180                         v &= ~MXC_H2_UCTRL_H2_OC_DIS_BIT; /* OC is used */
181                 else
182                         v |= MXC_H2_UCTRL_H2_OC_DIS_BIT; /* OC is not used */
183                 if (flags & MXC_EHCI_PWR_PIN_ACTIVE_HIGH)
184                         v |= MXC_H2_UCTRL_H2_PWR_POL_BIT;
185                 else
186                         v &= ~MXC_H2_UCTRL_H2_PWR_POL_BIT;
187 #endif
188                 __raw_writel(v, usbother_base + MXC_USBH2CTRL_OFFSET);
189                 break;
190 #ifdef CONFIG_MX53
191         case 3: /* Host 3 ULPI */
192                 v = __raw_readl(usbother_base + MXC_USBH3CTRL_OFFSET);
193                 if (flags & MXC_EHCI_OC_PIN_ACTIVE_LOW)
194                         v |= MXC_H3_UCTRL_H3_OC_POL_BIT;
195                 else
196                         v &= ~MXC_H3_UCTRL_H3_OC_POL_BIT;
197                 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
198                         v &= ~MXC_H3_UCTRL_H3_OC_DIS_BIT; /* OC is used */
199                 else
200                         v |= MXC_H3_UCTRL_H3_OC_DIS_BIT; /* OC is not used */
201                 if (flags & MXC_EHCI_PWR_PIN_ACTIVE_HIGH)
202                         v |= MXC_H3_UCTRL_H3_PWR_POL_BIT;
203                 else
204                         v &= ~MXC_H3_UCTRL_H3_PWR_POL_BIT;
205                 __raw_writel(v, usbother_base + MXC_USBH3CTRL_OFFSET);
206                 break;
207 #endif
208         }
209
210         return ret;
211 }
212
213 int __weak board_ehci_hcd_init(int port)
214 {
215         return 0;
216 }
217
218 void __weak board_ehci_hcd_postinit(struct usb_ehci *ehci, int port)
219 {
220 }
221
222 __weak void mx5_ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
223                                    uint32_t *reg)
224 {
225         mdelay(50);
226 }
227
228 #if !CONFIG_IS_ENABLED(DM_USB)
229 static const struct ehci_ops mx5_ehci_ops = {
230         .powerup_fixup          = mx5_ehci_powerup_fixup,
231 };
232
233 int ehci_hcd_init(int index, enum usb_init_type init,
234                 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
235 {
236         struct usb_ehci *ehci;
237
238         /* The only user for this is efikamx-usb */
239         ehci_set_controller_priv(index, NULL, &mx5_ehci_ops);
240         set_usboh3_clk();
241         enable_usboh3_clk(true);
242         set_usb_phy_clk();
243         enable_usb_phy1_clk(true);
244         enable_usb_phy2_clk(true);
245         mdelay(1);
246
247         /* Do board specific initialization */
248         board_ehci_hcd_init(CONFIG_MXC_USB_PORT);
249
250         ehci = (struct usb_ehci *)(OTG_BASE_ADDR +
251                 (0x200 * CONFIG_MXC_USB_PORT));
252         *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
253         *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
254                         HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
255         setbits_le32(&ehci->usbmode, CM_HOST);
256
257         __raw_writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
258         setbits_le32(&ehci->portsc, USB_EN);
259
260         mxc_set_usbcontrol(CONFIG_MXC_USB_PORT, CONFIG_MXC_USB_FLAGS);
261         mdelay(10);
262
263         /* Do board specific post-initialization */
264         board_ehci_hcd_postinit(ehci, CONFIG_MXC_USB_PORT);
265
266         return 0;
267 }
268
269 int ehci_hcd_stop(int index)
270 {
271         return 0;
272 }
273 #else /* CONFIG_IS_ENABLED(DM_USB) */
274 struct ehci_mx5_priv_data {
275         struct ehci_ctrl ctrl;
276         struct usb_ehci *ehci;
277         struct udevice *vbus_supply;
278         enum usb_init_type init_type;
279         int portnr;
280 };
281
282 static const struct ehci_ops mx5_ehci_ops = {
283         .powerup_fixup          = mx5_ehci_powerup_fixup,
284 };
285
286 static int ehci_usb_ofdata_to_platdata(struct udevice *dev)
287 {
288         struct usb_platdata *plat = dev_get_platdata(dev);
289         const char *mode;
290
291         mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "dr_mode", NULL);
292         if (mode) {
293                 if (strcmp(mode, "peripheral") == 0)
294                         plat->init_type = USB_INIT_DEVICE;
295                 else if (strcmp(mode, "host") == 0)
296                         plat->init_type = USB_INIT_HOST;
297                 else
298                         return -EINVAL;
299         }
300
301         return 0;
302 }
303
304 static int ehci_usb_probe(struct udevice *dev)
305 {
306         struct usb_platdata *plat = dev_get_platdata(dev);
307         struct usb_ehci *ehci = (struct usb_ehci *)devfdt_get_addr(dev);
308         struct ehci_mx5_priv_data *priv = dev_get_priv(dev);
309         enum usb_init_type type = plat->init_type;
310         struct ehci_hccr *hccr;
311         struct ehci_hcor *hcor;
312         int ret;
313
314         set_usboh3_clk();
315         enable_usboh3_clk(true);
316         set_usb_phy_clk();
317         enable_usb_phy1_clk(true);
318         enable_usb_phy2_clk(true);
319         mdelay(1);
320
321         priv->ehci = ehci;
322         priv->portnr = dev->seq;
323         priv->init_type = type;
324
325         ret = device_get_supply_regulator(dev, "vbus-supply",
326                                           &priv->vbus_supply);
327         if (ret)
328                 debug("%s: No vbus supply\n", dev->name);
329
330         if (!ret && priv->vbus_supply) {
331                 ret = regulator_set_enable(priv->vbus_supply,
332                                            (type == USB_INIT_DEVICE) ?
333                                            false : true);
334                 if (ret) {
335                         puts("Error enabling VBUS supply\n");
336                         return ret;
337                 }
338         }
339
340         hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
341         hcor = (struct ehci_hcor *)((uint32_t)hccr +
342                         HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
343         setbits_le32(&ehci->usbmode, CM_HOST);
344
345         __raw_writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
346         setbits_le32(&ehci->portsc, USB_EN);
347
348         mxc_set_usbcontrol(priv->portnr, CONFIG_MXC_USB_FLAGS);
349         mdelay(10);
350
351         return ehci_register(dev, hccr, hcor, &mx5_ehci_ops, 0,
352                              priv->init_type);
353 }
354
355 static const struct udevice_id mx5_usb_ids[] = {
356         { .compatible = "fsl,imx53-usb" },
357         { }
358 };
359
360 U_BOOT_DRIVER(usb_mx5) = {
361         .name   = "ehci_mx5",
362         .id     = UCLASS_USB,
363         .of_match = mx5_usb_ids,
364         .ofdata_to_platdata = ehci_usb_ofdata_to_platdata,
365         .probe  = ehci_usb_probe,
366         .remove = ehci_deregister,
367         .ops    = &ehci_usb_ops,
368         .platdata_auto_alloc_size = sizeof(struct usb_platdata),
369         .priv_auto_alloc_size = sizeof(struct ehci_mx5_priv_data),
370         .flags  = DM_FLAG_ALLOC_PRIV_DMA,
371 };
372 #endif /* !CONFIG_IS_ENABLED(DM_USB) */