ehci: msm: Add missing platdata
[oweals/u-boot.git] / drivers / usb / host / ehci-msm.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Qualcomm EHCI driver
4  *
5  * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
6  *
7  * Based on Linux driver
8  */
9
10 #include <common.h>
11 #include <dm.h>
12 #include <errno.h>
13 #include <fdtdec.h>
14 #include <linux/libfdt.h>
15 #include <usb.h>
16 #include <usb/ehci-ci.h>
17 #include <usb/ulpi.h>
18 #include <wait_bit.h>
19 #include <asm/gpio.h>
20 #include <asm/io.h>
21 #include <linux/compat.h>
22 #include "ehci.h"
23
24 /* PHY viewport regs */
25 #define ULPI_MISC_A_READ         0x96
26 #define ULPI_MISC_A_SET          0x97
27 #define ULPI_MISC_A_CLEAR        0x98
28 #define ULPI_MISC_A_VBUSVLDEXTSEL    (1 << 1)
29 #define ULPI_MISC_A_VBUSVLDEXT       (1 << 0)
30
31 #define GEN2_SESS_VLD_CTRL_EN (1 << 7)
32
33 #define SESS_VLD_CTRL         (1 << 25)
34
35 struct msm_ehci_priv {
36         struct ehci_ctrl ctrl; /* Needed by EHCI */
37         struct usb_ehci *ehci; /* Start of IP core*/
38         struct ulpi_viewport ulpi_vp; /* ULPI Viewport */
39 };
40
41 static void setup_usb_phy(struct msm_ehci_priv *priv)
42 {
43         /* Select and enable external configuration with USB PHY */
44         ulpi_write(&priv->ulpi_vp, (u8 *)ULPI_MISC_A_SET,
45                    ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT);
46 }
47
48 static void reset_usb_phy(struct msm_ehci_priv *priv)
49 {
50         /* Disable VBUS mimicing in the controller. */
51         ulpi_write(&priv->ulpi_vp, (u8 *)ULPI_MISC_A_CLEAR,
52                    ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT);
53 }
54
55
56 static int msm_init_after_reset(struct ehci_ctrl *dev)
57 {
58         struct msm_ehci_priv *p = container_of(dev, struct msm_ehci_priv, ctrl);
59         struct usb_ehci *ehci = p->ehci;
60
61         /* select ULPI phy */
62         writel(PORT_PTS_ULPI, &ehci->portsc);
63         setup_usb_phy(p);
64
65         /* Enable sess_vld */
66         setbits_le32(&ehci->genconfig2, GEN2_SESS_VLD_CTRL_EN);
67
68         /* Enable external vbus configuration in the LINK */
69         setbits_le32(&ehci->usbcmd, SESS_VLD_CTRL);
70
71         /* USB_OTG_HS_AHB_BURST */
72         writel(0x0, &ehci->sbuscfg);
73
74         /* USB_OTG_HS_AHB_MODE: HPROT_MODE */
75         /* Bus access related config. */
76         writel(0x08, &ehci->sbusmode);
77
78         /* set mode to host controller */
79         writel(CM_HOST, &ehci->usbmode);
80
81         return 0;
82 }
83
84 static const struct ehci_ops msm_ehci_ops = {
85         .init_after_reset = msm_init_after_reset
86 };
87
88 static int ehci_usb_probe(struct udevice *dev)
89 {
90         struct msm_ehci_priv *p = dev_get_priv(dev);
91         struct usb_ehci *ehci = p->ehci;
92         struct ehci_hccr *hccr;
93         struct ehci_hcor *hcor;
94         int ret;
95
96         hccr = (struct ehci_hccr *)((phys_addr_t)&ehci->caplength);
97         hcor = (struct ehci_hcor *)((phys_addr_t)hccr +
98                         HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
99
100         ret = board_usb_init(0, USB_INIT_HOST);
101         if (ret < 0)
102                 return ret;
103
104         return ehci_register(dev, hccr, hcor, &msm_ehci_ops, 0, USB_INIT_HOST);
105 }
106
107 static int ehci_usb_remove(struct udevice *dev)
108 {
109         struct msm_ehci_priv *p = dev_get_priv(dev);
110         struct usb_ehci *ehci = p->ehci;
111         int ret;
112
113         ret = ehci_deregister(dev);
114         if (ret)
115                 return ret;
116
117         /* Stop controller. */
118         clrbits_le32(&ehci->usbcmd, CMD_RUN);
119
120         reset_usb_phy(p);
121
122         ret = board_usb_init(0, USB_INIT_DEVICE); /* Board specific hook */
123         if (ret < 0)
124                 return ret;
125
126         /* Reset controller */
127         setbits_le32(&ehci->usbcmd, CMD_RESET);
128
129         /* Wait for reset */
130         if (wait_for_bit_le32(&ehci->usbcmd, CMD_RESET, false, 30, false)) {
131                 printf("Stuck on USB reset.\n");
132                 return -ETIMEDOUT;
133         }
134
135         return 0;
136 }
137
138 static int ehci_usb_ofdata_to_platdata(struct udevice *dev)
139 {
140         struct msm_ehci_priv *priv = dev_get_priv(dev);
141
142         priv->ulpi_vp.port_num = 0;
143         priv->ehci = (void *)devfdt_get_addr(dev);
144
145         if (priv->ehci == (void *)FDT_ADDR_T_NONE)
146                 return -EINVAL;
147
148         /* Warning: this will not work if viewport address is > 64 bit due to
149          * ULPI design.
150          */
151         priv->ulpi_vp.viewport_addr = (phys_addr_t)&priv->ehci->ulpi_viewpoint;
152
153         return 0;
154 }
155
156 static const struct udevice_id ehci_usb_ids[] = {
157         { .compatible = "qcom,ehci-host", },
158         { }
159 };
160
161 U_BOOT_DRIVER(usb_ehci) = {
162         .name   = "ehci_msm",
163         .id     = UCLASS_USB,
164         .of_match = ehci_usb_ids,
165         .ofdata_to_platdata = ehci_usb_ofdata_to_platdata,
166         .probe = ehci_usb_probe,
167         .remove = ehci_usb_remove,
168         .ops    = &ehci_usb_ops,
169         .priv_auto_alloc_size = sizeof(struct msm_ehci_priv),
170         .platdata_auto_alloc_size = sizeof(struct usb_platdata),
171         .flags  = DM_FLAG_ALLOC_PRIV_DMA,
172 };