blob: 160a386801e902cc230e583cb39d074d9f8ed72f [file] [log] [blame]
Jean-Jacques Hiblot668257e2018-11-29 10:57:39 +01001// 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>
Simon Glass61b29b82020-02-03 07:36:15 -070016#include <linux/err.h>
Jean-Jacques Hiblot668257e2018-11-29 10:57:39 +010017
18#define OMAP_USB2_CALIBRATE_FALSE_DISCONNECT BIT(0)
19
20#define OMAP_DEV_PHY_PD BIT(0)
21#define OMAP_USB2_PHY_PD BIT(28)
22
Jean-Jacques Hiblot512a84c2018-12-04 11:30:49 +010023#define AM437X_USB2_PHY_PD BIT(0)
24#define AM437X_USB2_OTG_PD BIT(1)
25#define AM437X_USB2_OTGVDET_EN BIT(19)
26#define AM437X_USB2_OTGSESSEND_EN BIT(20)
27
Jean-Jacques Hiblot668257e2018-11-29 10:57:39 +010028#define USB2PHY_DISCON_BYP_LATCH BIT(31)
29#define USB2PHY_ANA_CONFIG1 (0x4c)
30
Vignesh Raghavendra779f40b2019-12-09 10:37:31 +053031#define AM654_USB2_OTG_PD BIT(8)
32#define AM654_USB2_VBUS_DET_EN BIT(5)
33#define AM654_USB2_VBUSVALID_DET_EN BIT(4)
34
Jean-Jacques Hiblot668257e2018-11-29 10:57:39 +010035DECLARE_GLOBAL_DATA_PTR;
36
37struct omap_usb2_phy {
38 struct regmap *pwr_regmap;
39 ulong flags;
40 void *phy_base;
41 u32 pwr_reg_offset;
42};
43
44struct usb_phy_data {
45 const char *label;
46 u8 flags;
47 u32 mask;
48 u32 power_on;
49 u32 power_off;
50};
51
52static const struct usb_phy_data omap5_usb2_data = {
53 .label = "omap5_usb2",
54 .flags = 0,
55 .mask = OMAP_DEV_PHY_PD,
56 .power_off = OMAP_DEV_PHY_PD,
57};
58
59static const struct usb_phy_data dra7x_usb2_data = {
60 .label = "dra7x_usb2",
61 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
62 .mask = OMAP_DEV_PHY_PD,
63 .power_off = OMAP_DEV_PHY_PD,
64};
65
66static const struct usb_phy_data dra7x_usb2_phy2_data = {
67 .label = "dra7x_usb2_phy2",
68 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
69 .mask = OMAP_USB2_PHY_PD,
70 .power_off = OMAP_USB2_PHY_PD,
71};
72
Jean-Jacques Hiblot512a84c2018-12-04 11:30:49 +010073static const struct usb_phy_data am437x_usb2_data = {
74 .label = "am437x_usb2",
75 .flags = 0,
76 .mask = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD |
77 AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
78 .power_on = AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
79 .power_off = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD,
80};
81
Vignesh Raghavendra779f40b2019-12-09 10:37:31 +053082static const struct usb_phy_data am654_usb2_data = {
83 .label = "am654_usb2",
84 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
85 .mask = AM654_USB2_OTG_PD | AM654_USB2_VBUS_DET_EN |
86 AM654_USB2_VBUSVALID_DET_EN,
87 .power_on = AM654_USB2_VBUS_DET_EN | AM654_USB2_VBUSVALID_DET_EN,
88 .power_off = AM654_USB2_OTG_PD,
89};
90
Jean-Jacques Hiblot668257e2018-11-29 10:57:39 +010091static const struct udevice_id omap_usb2_id_table[] = {
92 {
93 .compatible = "ti,omap5-usb2",
94 .data = (ulong)&omap5_usb2_data,
95 },
96 {
97 .compatible = "ti,dra7x-usb2",
98 .data = (ulong)&dra7x_usb2_data,
99 },
100 {
101 .compatible = "ti,dra7x-usb2-phy2",
102 .data = (ulong)&dra7x_usb2_phy2_data,
103 },
Jean-Jacques Hiblot512a84c2018-12-04 11:30:49 +0100104 {
105 .compatible = "ti,am437x-usb2",
106 .data = (ulong)&am437x_usb2_data,
107 },
Vignesh Raghavendra779f40b2019-12-09 10:37:31 +0530108 {
109 .compatible = "ti,am654-usb2",
110 .data = (ulong)&am654_usb2_data,
111 },
Jean-Jacques Hiblot668257e2018-11-29 10:57:39 +0100112 {},
113};
114
115static int omap_usb_phy_power(struct phy *usb_phy, bool on)
116{
117 struct udevice *dev = usb_phy->dev;
118 const struct usb_phy_data *data;
119 const struct omap_usb2_phy *phy = dev_get_priv(dev);
120 u32 val;
121 int rc;
122
123 data = (const struct usb_phy_data *)dev_get_driver_data(dev);
124 if (!data)
125 return -EINVAL;
126
127 rc = regmap_read(phy->pwr_regmap, phy->pwr_reg_offset, &val);
128 if (rc)
129 return rc;
130 val &= ~data->mask;
131 if (on)
132 val |= data->power_on;
133 else
134 val |= data->power_off;
135 rc = regmap_write(phy->pwr_regmap, phy->pwr_reg_offset, val);
136 if (rc)
137 return rc;
138
139 return 0;
140}
141
142static int omap_usb2_phy_init(struct phy *usb_phy)
143{
144 struct udevice *dev = usb_phy->dev;
145 struct omap_usb2_phy *priv = dev_get_priv(dev);
146 u32 val;
147
148 if (priv->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
149 /*
150 *
151 * Reduce the sensitivity of internal PHY by enabling the
152 * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This
153 * resolves issues with certain devices which can otherwise
154 * be prone to false disconnects.
155 *
156 */
157 val = readl(priv->phy_base + USB2PHY_ANA_CONFIG1);
158 val |= USB2PHY_DISCON_BYP_LATCH;
159 writel(val, priv->phy_base + USB2PHY_ANA_CONFIG1);
160 }
161
162 return 0;
163}
164
165static int omap_usb2_phy_power_on(struct phy *usb_phy)
166{
167 return omap_usb_phy_power(usb_phy, true);
168}
169
170static int omap_usb2_phy_power_off(struct phy *usb_phy)
171{
172 return omap_usb_phy_power(usb_phy, false);
173}
174
175static int omap_usb2_phy_exit(struct phy *usb_phy)
176{
177 return omap_usb_phy_power(usb_phy, false);
178}
179
180struct phy_ops omap_usb2_phy_ops = {
181 .init = omap_usb2_phy_init,
182 .power_on = omap_usb2_phy_power_on,
183 .power_off = omap_usb2_phy_power_off,
184 .exit = omap_usb2_phy_exit,
185};
186
187int omap_usb2_phy_probe(struct udevice *dev)
188{
189 int rc;
190 struct regmap *regmap;
191 struct omap_usb2_phy *priv = dev_get_priv(dev);
192 const struct usb_phy_data *data;
193 u32 tmp[2];
194
195 data = (const struct usb_phy_data *)dev_get_driver_data(dev);
196 if (!data)
197 return -EINVAL;
198
199 if (data->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
Vignesh Raghavendrad35f2cf2019-12-09 10:37:30 +0530200 priv->phy_base = dev_read_addr_ptr(dev);
Jean-Jacques Hiblot668257e2018-11-29 10:57:39 +0100201
Vignesh Raghavendrad35f2cf2019-12-09 10:37:30 +0530202 if (!priv->phy_base)
Jean-Jacques Hiblot668257e2018-11-29 10:57:39 +0100203 return -EINVAL;
Jean-Jacques Hiblot668257e2018-11-29 10:57:39 +0100204 priv->flags |= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT;
205 }
206
207 regmap = syscon_regmap_lookup_by_phandle(dev, "syscon-phy-power");
Jean-Jacques Hiblot512a84c2018-12-04 11:30:49 +0100208 if (!IS_ERR(regmap)) {
209 priv->pwr_regmap = regmap;
210 rc = dev_read_u32_array(dev, "syscon-phy-power", tmp, 2);
211 if (rc) {
212 printf("couldn't get power reg. offset (err %d)\n", rc);
213 return rc;
214 }
215 priv->pwr_reg_offset = tmp[1];
216 return 0;
Jean-Jacques Hiblot668257e2018-11-29 10:57:39 +0100217 }
Jean-Jacques Hiblot512a84c2018-12-04 11:30:49 +0100218 regmap = syscon_regmap_lookup_by_phandle(dev, "ctrl-module");
219 if (!IS_ERR(regmap)) {
220 priv->pwr_regmap = regmap;
221 priv->pwr_reg_offset = 0;
222 return 0;
Jean-Jacques Hiblot668257e2018-11-29 10:57:39 +0100223 }
Jean-Jacques Hiblot668257e2018-11-29 10:57:39 +0100224
Jean-Jacques Hiblot512a84c2018-12-04 11:30:49 +0100225 printf("can't get regmap (err %ld)\n", PTR_ERR(regmap));
226 return PTR_ERR(regmap);
Jean-Jacques Hiblot668257e2018-11-29 10:57:39 +0100227}
228
229U_BOOT_DRIVER(omap_usb2_phy) = {
230 .name = "omap_usb2_phy",
231 .id = UCLASS_PHY,
232 .of_match = omap_usb2_id_table,
233 .probe = omap_usb2_phy_probe,
234 .ops = &omap_usb2_phy_ops,
235 .priv_auto_alloc_size = sizeof(struct omap_usb2_phy),
236};