Merge https://gitlab.denx.de/u-boot/custodians/u-boot-spi into next
[platform/kernel/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 #define OMAP_USB2_DISABLE_CHG_DET               BIT(1)
21
22 #define OMAP_DEV_PHY_PD         BIT(0)
23 #define OMAP_USB2_PHY_PD        BIT(28)
24
25 #define AM437X_USB2_PHY_PD              BIT(0)
26 #define AM437X_USB2_OTG_PD              BIT(1)
27 #define AM437X_USB2_OTGVDET_EN          BIT(19)
28 #define AM437X_USB2_OTGSESSEND_EN       BIT(20)
29
30 #define USB2PHY_DISCON_BYP_LATCH        BIT(31)
31 #define USB2PHY_ANA_CONFIG1             (0x4c)
32
33 #define AM654_USB2_OTG_PD               BIT(8)
34 #define AM654_USB2_VBUS_DET_EN          BIT(5)
35 #define AM654_USB2_VBUSVALID_DET_EN     BIT(4)
36
37 #define USB2PHY_CHRG_DET                 0x14
38 #define USB2PHY_USE_CHG_DET_REG         BIT(29)
39 #define USB2PHY_DIS_CHG_DET             BIT(28)
40
41 DECLARE_GLOBAL_DATA_PTR;
42
43 struct omap_usb2_phy {
44         struct regmap *pwr_regmap;
45         ulong flags;
46         void *phy_base;
47         u32 pwr_reg_offset;
48 };
49
50 struct usb_phy_data {
51         const char *label;
52         u8 flags;
53         u32 mask;
54         u32 power_on;
55         u32 power_off;
56 };
57
58 static const struct usb_phy_data omap5_usb2_data = {
59         .label = "omap5_usb2",
60         .flags = 0,
61         .mask = OMAP_DEV_PHY_PD,
62         .power_off = OMAP_DEV_PHY_PD,
63 };
64
65 static const struct usb_phy_data dra7x_usb2_data = {
66         .label = "dra7x_usb2",
67         .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
68         .mask = OMAP_DEV_PHY_PD,
69         .power_off = OMAP_DEV_PHY_PD,
70 };
71
72 static const struct usb_phy_data dra7x_usb2_phy2_data = {
73         .label = "dra7x_usb2_phy2",
74         .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
75         .mask = OMAP_USB2_PHY_PD,
76         .power_off = OMAP_USB2_PHY_PD,
77 };
78
79 static const struct usb_phy_data am437x_usb2_data = {
80         .label = "am437x_usb2",
81         .flags =  0,
82         .mask = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD |
83                 AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
84         .power_on = AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
85         .power_off = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD,
86 };
87
88 static const struct usb_phy_data am654_usb2_data = {
89         .label = "am654_usb2",
90         .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
91         .mask = AM654_USB2_OTG_PD | AM654_USB2_VBUS_DET_EN |
92                 AM654_USB2_VBUSVALID_DET_EN,
93         .power_on = AM654_USB2_VBUS_DET_EN | AM654_USB2_VBUSVALID_DET_EN,
94         .power_off = AM654_USB2_OTG_PD,
95 };
96
97 static const struct udevice_id omap_usb2_id_table[] = {
98         {
99                 .compatible = "ti,omap5-usb2",
100                 .data = (ulong)&omap5_usb2_data,
101         },
102         {
103                 .compatible = "ti,dra7x-usb2",
104                 .data = (ulong)&dra7x_usb2_data,
105         },
106         {
107                 .compatible = "ti,dra7x-usb2-phy2",
108                 .data = (ulong)&dra7x_usb2_phy2_data,
109         },
110         {
111                 .compatible = "ti,am437x-usb2",
112                 .data = (ulong)&am437x_usb2_data,
113         },
114         {
115                 .compatible = "ti,am654-usb2",
116                 .data = (ulong)&am654_usb2_data,
117         },
118         {},
119 };
120
121 static int omap_usb_phy_power(struct phy *usb_phy, bool on)
122 {
123         struct udevice *dev = usb_phy->dev;
124         const struct usb_phy_data *data;
125         const struct omap_usb2_phy *phy = dev_get_priv(dev);
126         u32 val;
127         int rc;
128
129         data = (const struct usb_phy_data *)dev_get_driver_data(dev);
130         if (!data)
131                 return -EINVAL;
132
133         rc = regmap_read(phy->pwr_regmap, phy->pwr_reg_offset, &val);
134         if (rc)
135                 return rc;
136         val &= ~data->mask;
137         if (on)
138                 val |= data->power_on;
139         else
140                 val |= data->power_off;
141         rc = regmap_write(phy->pwr_regmap, phy->pwr_reg_offset, val);
142         if (rc)
143                 return rc;
144
145         return 0;
146 }
147
148 static int omap_usb2_phy_init(struct phy *usb_phy)
149 {
150         struct udevice *dev = usb_phy->dev;
151         struct omap_usb2_phy *priv = dev_get_priv(dev);
152         u32 val;
153
154         if (priv->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
155                 /*
156                  *
157                  * Reduce the sensitivity of internal PHY by enabling the
158                  * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This
159                  * resolves issues with certain devices which can otherwise
160                  * be prone to false disconnects.
161                  *
162                  */
163                 val = readl(priv->phy_base + USB2PHY_ANA_CONFIG1);
164                 val |= USB2PHY_DISCON_BYP_LATCH;
165                 writel(val, priv->phy_base + USB2PHY_ANA_CONFIG1);
166         }
167
168         if (priv->flags & OMAP_USB2_DISABLE_CHG_DET) {
169                 val = readl(priv->phy_base + USB2PHY_CHRG_DET);
170                 val |= USB2PHY_USE_CHG_DET_REG | USB2PHY_DIS_CHG_DET;
171                 writel(val, priv->phy_base + USB2PHY_CHRG_DET);
172         }
173
174         return 0;
175 }
176
177 static int omap_usb2_phy_power_on(struct phy *usb_phy)
178 {
179         return omap_usb_phy_power(usb_phy, true);
180 }
181
182 static int omap_usb2_phy_power_off(struct phy *usb_phy)
183 {
184         return omap_usb_phy_power(usb_phy, false);
185 }
186
187 static int omap_usb2_phy_exit(struct phy *usb_phy)
188 {
189         return omap_usb_phy_power(usb_phy, false);
190 }
191
192 struct phy_ops omap_usb2_phy_ops = {
193         .init = omap_usb2_phy_init,
194         .power_on = omap_usb2_phy_power_on,
195         .power_off = omap_usb2_phy_power_off,
196         .exit = omap_usb2_phy_exit,
197 };
198
199 int omap_usb2_phy_probe(struct udevice *dev)
200 {
201         int rc;
202         struct regmap *regmap;
203         struct omap_usb2_phy *priv = dev_get_priv(dev);
204         const struct usb_phy_data *data;
205         u32 tmp[2];
206
207         data = (const struct usb_phy_data *)dev_get_driver_data(dev);
208         if (!data)
209                 return -EINVAL;
210
211         priv->phy_base = dev_read_addr_ptr(dev);
212
213         if (!priv->phy_base)
214                 return -EINVAL;
215
216         if (data->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT)
217                 priv->flags |= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT;
218
219         /*
220          * AM654x PG1.0 has a silicon bug that D+ is pulled high after
221          * POR, which could cause enumeration failure with some USB hubs.
222          * Disabling the USB2_PHY Charger Detect function will put D+
223          * into the normal state.
224          *
225          * Using property "ti,dis-chg-det-quirk" in the DT usb2-phy node
226          * to enable this workaround for AM654x PG1.0.
227          */
228         if (dev_read_bool(dev, "ti,dis-chg-det-quirk"))
229                 priv->flags |= OMAP_USB2_DISABLE_CHG_DET;
230
231         regmap = syscon_regmap_lookup_by_phandle(dev, "syscon-phy-power");
232         if (!IS_ERR(regmap)) {
233                 priv->pwr_regmap = regmap;
234                 rc =  dev_read_u32_array(dev, "syscon-phy-power", tmp, 2);
235                 if (rc) {
236                         printf("couldn't get power reg. offset (err %d)\n", rc);
237                         return rc;
238                 }
239                 priv->pwr_reg_offset = tmp[1];
240                 return 0;
241         }
242         regmap = syscon_regmap_lookup_by_phandle(dev, "ctrl-module");
243         if (!IS_ERR(regmap)) {
244                 priv->pwr_regmap = regmap;
245                 priv->pwr_reg_offset = 0;
246                 return 0;
247         }
248
249         printf("can't get regmap (err %ld)\n", PTR_ERR(regmap));
250         return PTR_ERR(regmap);
251 }
252
253 U_BOOT_DRIVER(omap_usb2_phy) = {
254         .name = "omap_usb2_phy",
255         .id = UCLASS_PHY,
256         .of_match = omap_usb2_id_table,
257         .probe = omap_usb2_phy_probe,
258         .ops = &omap_usb2_phy_ops,
259         .priv_auto_alloc_size = sizeof(struct omap_usb2_phy),
260 };