colibri_imx6: fix video stdout in default environment
[oweals/u-boot.git] / drivers / phy / omap-usb2-phy.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * OMAP USB2 PHY LAYER
4  *
5  * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com
6  * Written by Jean-Jacques Hiblot <jjhiblot@ti.com>
7  */
8
9 #include <common.h>
10 #include <asm/io.h>
11 #include <dm.h>
12 #include <errno.h>
13 #include <generic-phy.h>
14 #include <regmap.h>
15 #include <syscon.h>
16 #include <linux/bitops.h>
17 #include <linux/err.h>
18
19 #define OMAP_USB2_CALIBRATE_FALSE_DISCONNECT    BIT(0)
20
21 #define OMAP_DEV_PHY_PD         BIT(0)
22 #define OMAP_USB2_PHY_PD        BIT(28)
23
24 #define AM437X_USB2_PHY_PD              BIT(0)
25 #define AM437X_USB2_OTG_PD              BIT(1)
26 #define AM437X_USB2_OTGVDET_EN          BIT(19)
27 #define AM437X_USB2_OTGSESSEND_EN       BIT(20)
28
29 #define USB2PHY_DISCON_BYP_LATCH        BIT(31)
30 #define USB2PHY_ANA_CONFIG1             (0x4c)
31
32 #define AM654_USB2_OTG_PD               BIT(8)
33 #define AM654_USB2_VBUS_DET_EN          BIT(5)
34 #define AM654_USB2_VBUSVALID_DET_EN     BIT(4)
35
36 DECLARE_GLOBAL_DATA_PTR;
37
38 struct omap_usb2_phy {
39         struct regmap *pwr_regmap;
40         ulong flags;
41         void *phy_base;
42         u32 pwr_reg_offset;
43 };
44
45 struct usb_phy_data {
46         const char *label;
47         u8 flags;
48         u32 mask;
49         u32 power_on;
50         u32 power_off;
51 };
52
53 static const struct usb_phy_data omap5_usb2_data = {
54         .label = "omap5_usb2",
55         .flags = 0,
56         .mask = OMAP_DEV_PHY_PD,
57         .power_off = OMAP_DEV_PHY_PD,
58 };
59
60 static const struct usb_phy_data dra7x_usb2_data = {
61         .label = "dra7x_usb2",
62         .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
63         .mask = OMAP_DEV_PHY_PD,
64         .power_off = OMAP_DEV_PHY_PD,
65 };
66
67 static const struct usb_phy_data dra7x_usb2_phy2_data = {
68         .label = "dra7x_usb2_phy2",
69         .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
70         .mask = OMAP_USB2_PHY_PD,
71         .power_off = OMAP_USB2_PHY_PD,
72 };
73
74 static const struct usb_phy_data am437x_usb2_data = {
75         .label = "am437x_usb2",
76         .flags =  0,
77         .mask = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD |
78                 AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
79         .power_on = AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
80         .power_off = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD,
81 };
82
83 static const struct usb_phy_data am654_usb2_data = {
84         .label = "am654_usb2",
85         .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
86         .mask = AM654_USB2_OTG_PD | AM654_USB2_VBUS_DET_EN |
87                 AM654_USB2_VBUSVALID_DET_EN,
88         .power_on = AM654_USB2_VBUS_DET_EN | AM654_USB2_VBUSVALID_DET_EN,
89         .power_off = AM654_USB2_OTG_PD,
90 };
91
92 static const struct udevice_id omap_usb2_id_table[] = {
93         {
94                 .compatible = "ti,omap5-usb2",
95                 .data = (ulong)&omap5_usb2_data,
96         },
97         {
98                 .compatible = "ti,dra7x-usb2",
99                 .data = (ulong)&dra7x_usb2_data,
100         },
101         {
102                 .compatible = "ti,dra7x-usb2-phy2",
103                 .data = (ulong)&dra7x_usb2_phy2_data,
104         },
105         {
106                 .compatible = "ti,am437x-usb2",
107                 .data = (ulong)&am437x_usb2_data,
108         },
109         {
110                 .compatible = "ti,am654-usb2",
111                 .data = (ulong)&am654_usb2_data,
112         },
113         {},
114 };
115
116 static int omap_usb_phy_power(struct phy *usb_phy, bool on)
117 {
118         struct udevice *dev = usb_phy->dev;
119         const struct usb_phy_data *data;
120         const struct omap_usb2_phy *phy = dev_get_priv(dev);
121         u32 val;
122         int rc;
123
124         data = (const struct usb_phy_data *)dev_get_driver_data(dev);
125         if (!data)
126                 return -EINVAL;
127
128         rc = regmap_read(phy->pwr_regmap, phy->pwr_reg_offset, &val);
129         if (rc)
130                 return rc;
131         val &= ~data->mask;
132         if (on)
133                 val |= data->power_on;
134         else
135                 val |= data->power_off;
136         rc = regmap_write(phy->pwr_regmap, phy->pwr_reg_offset, val);
137         if (rc)
138                 return rc;
139
140         return 0;
141 }
142
143 static int omap_usb2_phy_init(struct phy *usb_phy)
144 {
145         struct udevice *dev = usb_phy->dev;
146         struct omap_usb2_phy *priv = dev_get_priv(dev);
147         u32 val;
148
149         if (priv->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
150                 /*
151                  *
152                  * Reduce the sensitivity of internal PHY by enabling the
153                  * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This
154                  * resolves issues with certain devices which can otherwise
155                  * be prone to false disconnects.
156                  *
157                  */
158                 val = readl(priv->phy_base + USB2PHY_ANA_CONFIG1);
159                 val |= USB2PHY_DISCON_BYP_LATCH;
160                 writel(val, priv->phy_base + USB2PHY_ANA_CONFIG1);
161         }
162
163         return 0;
164 }
165
166 static int omap_usb2_phy_power_on(struct phy *usb_phy)
167 {
168         return omap_usb_phy_power(usb_phy, true);
169 }
170
171 static int omap_usb2_phy_power_off(struct phy *usb_phy)
172 {
173         return omap_usb_phy_power(usb_phy, false);
174 }
175
176 static int omap_usb2_phy_exit(struct phy *usb_phy)
177 {
178         return omap_usb_phy_power(usb_phy, false);
179 }
180
181 struct phy_ops omap_usb2_phy_ops = {
182         .init = omap_usb2_phy_init,
183         .power_on = omap_usb2_phy_power_on,
184         .power_off = omap_usb2_phy_power_off,
185         .exit = omap_usb2_phy_exit,
186 };
187
188 int omap_usb2_phy_probe(struct udevice *dev)
189 {
190         int rc;
191         struct regmap *regmap;
192         struct omap_usb2_phy *priv = dev_get_priv(dev);
193         const struct usb_phy_data *data;
194         u32 tmp[2];
195
196         data = (const struct usb_phy_data *)dev_get_driver_data(dev);
197         if (!data)
198                 return -EINVAL;
199
200         if (data->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
201                 priv->phy_base = dev_read_addr_ptr(dev);
202
203                 if (!priv->phy_base)
204                         return -EINVAL;
205                 priv->flags |= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT;
206         }
207
208         regmap = syscon_regmap_lookup_by_phandle(dev, "syscon-phy-power");
209         if (!IS_ERR(regmap)) {
210                 priv->pwr_regmap = regmap;
211                 rc =  dev_read_u32_array(dev, "syscon-phy-power", tmp, 2);
212                 if (rc) {
213                         printf("couldn't get power reg. offset (err %d)\n", rc);
214                         return rc;
215                 }
216                 priv->pwr_reg_offset = tmp[1];
217                 return 0;
218         }
219         regmap = syscon_regmap_lookup_by_phandle(dev, "ctrl-module");
220         if (!IS_ERR(regmap)) {
221                 priv->pwr_regmap = regmap;
222                 priv->pwr_reg_offset = 0;
223                 return 0;
224         }
225
226         printf("can't get regmap (err %ld)\n", PTR_ERR(regmap));
227         return PTR_ERR(regmap);
228 }
229
230 U_BOOT_DRIVER(omap_usb2_phy) = {
231         .name = "omap_usb2_phy",
232         .id = UCLASS_PHY,
233         .of_match = omap_usb2_id_table,
234         .probe = omap_usb2_phy_probe,
235         .ops = &omap_usb2_phy_ops,
236         .priv_auto_alloc_size = sizeof(struct omap_usb2_phy),
237 };