aboutsummaryrefslogtreecommitdiff
path: root/drivers/phy/meson-gxbb-usb2.c
blob: 7a2e3d2739e88a92475b8b82ef35aa44b27ec22e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// SPDX-License-Identifier: GPL-2.0+
/*
 * Meson8, Meson8b and GXBB USB2 PHY driver
 *
 * Copyright (C) 2016 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
 * Copyright (C) 2018 BayLibre, SAS
 *
 * Author: Beniamino Galvani <b.galvani@gmail.com>
 */

#include <common.h>
#include <clk.h>
#include <dm.h>
#include <generic-phy.h>
#include <power/regulator.h>
#include <regmap.h>
#include <reset.h>
#include <linux/bitops.h>

#define REG_CONFIG					0x00
	#define REG_CONFIG_CLK_EN			BIT(0)
	#define REG_CONFIG_CLK_SEL_MASK			GENMASK(3, 1)
	#define REG_CONFIG_CLK_DIV_MASK			GENMASK(10, 4)
	#define REG_CONFIG_CLK_32k_ALTSEL		BIT(15)
	#define REG_CONFIG_TEST_TRIG			BIT(31)

#define REG_CTRL					0x04
	#define REG_CTRL_SOFT_PRST			BIT(0)
	#define REG_CTRL_SOFT_HRESET			BIT(1)
	#define REG_CTRL_SS_SCALEDOWN_MODE_MASK		GENMASK(3, 2)
	#define REG_CTRL_CLK_DET_RST			BIT(4)
	#define REG_CTRL_INTR_SEL			BIT(5)
	#define REG_CTRL_CLK_DETECTED			BIT(8)
	#define REG_CTRL_SOF_SENT_RCVD_TGL		BIT(9)
	#define REG_CTRL_SOF_TOGGLE_OUT			BIT(10)
	#define REG_CTRL_POWER_ON_RESET			BIT(15)
	#define REG_CTRL_SLEEPM				BIT(16)
	#define REG_CTRL_TX_BITSTUFF_ENN_H		BIT(17)
	#define REG_CTRL_TX_BITSTUFF_ENN		BIT(18)
	#define REG_CTRL_COMMON_ON			BIT(19)
	#define REG_CTRL_REF_CLK_SEL_MASK		GENMASK(21, 20)
	#define REG_CTRL_REF_CLK_SEL_SHIFT		20
	#define REG_CTRL_FSEL_MASK			GENMASK(24, 22)
	#define REG_CTRL_FSEL_SHIFT			22
	#define REG_CTRL_PORT_RESET			BIT(25)
	#define REG_CTRL_THREAD_ID_MASK			GENMASK(31, 26)

/* bits [31:26], [24:21] and [15:3] seem to be read-only */
#define REG_ADP_BC					0x0c
	#define REG_ADP_BC_VBUS_VLD_EXT_SEL		BIT(0)
	#define REG_ADP_BC_VBUS_VLD_EXT			BIT(1)
	#define REG_ADP_BC_OTG_DISABLE			BIT(2)
	#define REG_ADP_BC_ID_PULLUP			BIT(3)
	#define REG_ADP_BC_DRV_VBUS			BIT(4)
	#define REG_ADP_BC_ADP_PRB_EN			BIT(5)
	#define REG_ADP_BC_ADP_DISCHARGE		BIT(6)
	#define REG_ADP_BC_ADP_CHARGE			BIT(7)
	#define REG_ADP_BC_SESS_END			BIT(8)
	#define REG_ADP_BC_DEVICE_SESS_VLD		BIT(9)
	#define REG_ADP_BC_B_VALID			BIT(10)
	#define REG_ADP_BC_A_VALID			BIT(11)
	#define REG_ADP_BC_ID_DIG			BIT(12)
	#define REG_ADP_BC_VBUS_VALID			BIT(13)
	#define REG_ADP_BC_ADP_PROBE			BIT(14)
	#define REG_ADP_BC_ADP_SENSE			BIT(15)
	#define REG_ADP_BC_ACA_ENABLE			BIT(16)
	#define REG_ADP_BC_DCD_ENABLE			BIT(17)
	#define REG_ADP_BC_VDAT_DET_EN_B		BIT(18)
	#define REG_ADP_BC_VDAT_SRC_EN_B		BIT(19)
	#define REG_ADP_BC_CHARGE_SEL			BIT(20)
	#define REG_ADP_BC_CHARGE_DETECT		BIT(21)
	#define REG_ADP_BC_ACA_PIN_RANGE_C		BIT(22)
	#define REG_ADP_BC_ACA_PIN_RANGE_B		BIT(23)
	#define REG_ADP_BC_ACA_PIN_RANGE_A		BIT(24)
	#define REG_ADP_BC_ACA_PIN_GND			BIT(25)
	#define REG_ADP_BC_ACA_PIN_FLOAT		BIT(26)

#define RESET_COMPLETE_TIME				500
#define ACA_ENABLE_COMPLETE_TIME			50

struct phy_meson_gxbb_usb2_priv {
	struct regmap *regmap;
	struct reset_ctl_bulk resets;
#if CONFIG_IS_ENABLED(DM_REGULATOR)
	struct udevice *phy_supply;
#endif
};

static int phy_meson_gxbb_usb2_power_on(struct phy *phy)
{
	struct udevice *dev = phy->dev;
	struct phy_meson_gxbb_usb2_priv *priv = dev_get_priv(dev);
	uint val;

#if CONFIG_IS_ENABLED(DM_REGULATOR)
	if (priv->phy_supply) {
		int ret = regulator_set_enable(priv->phy_supply, true);

		if (ret)
			return ret;
	}
#endif

	regmap_update_bits(priv->regmap, REG_CONFIG,
			   REG_CONFIG_CLK_32k_ALTSEL,
			   REG_CONFIG_CLK_32k_ALTSEL);
	regmap_update_bits(priv->regmap, REG_CTRL,
			   REG_CTRL_REF_CLK_SEL_MASK,
			   0x2 << REG_CTRL_REF_CLK_SEL_SHIFT);
	regmap_update_bits(priv->regmap, REG_CTRL,
			   REG_CTRL_FSEL_MASK,
			   0x5 << REG_CTRL_FSEL_SHIFT);

	/* reset the PHY */
	regmap_update_bits(priv->regmap, REG_CTRL,
			   REG_CTRL_POWER_ON_RESET,
			   REG_CTRL_POWER_ON_RESET);
	udelay(RESET_COMPLETE_TIME);
	regmap_update_bits(priv->regmap, REG_CTRL,
			   REG_CTRL_POWER_ON_RESET,
			   0);
	udelay(RESET_COMPLETE_TIME);

	regmap_update_bits(priv->regmap, REG_CTRL,
			   REG_CTRL_SOF_TOGGLE_OUT,
			   REG_CTRL_SOF_TOGGLE_OUT);

	/* Set host mode */
	regmap_update_bits(priv->regmap, REG_ADP_BC,
			   REG_ADP_BC_ACA_ENABLE,
			   REG_ADP_BC_ACA_ENABLE);
	udelay(ACA_ENABLE_COMPLETE_TIME);

	regmap_read(priv->regmap, REG_ADP_BC, &val);
	if (val & REG_ADP_BC_ACA_PIN_FLOAT) {
		pr_err("Error powering on GXBB USB PHY\n");
		return -EINVAL;
	}

	return 0;
}

static int phy_meson_gxbb_usb2_power_off(struct phy *phy)
{
#if CONFIG_IS_ENABLED(DM_REGULATOR)
	struct udevice *dev = phy->dev;
	struct phy_meson_gxbb_usb2_priv *priv = dev_get_priv(dev);

	if (priv->phy_supply) {
		int ret = regulator_set_enable(priv->phy_supply, false);

		if (ret)
			return ret;
	}
#endif

	return 0;
}

static struct phy_ops meson_gxbb_usb2_phy_ops = {
	.power_on = phy_meson_gxbb_usb2_power_on,
	.power_off = phy_meson_gxbb_usb2_power_off,
};

static int meson_gxbb_usb2_phy_probe(struct udevice *dev)
{
	struct phy_meson_gxbb_usb2_priv *priv = dev_get_priv(dev);
	struct clk clk_usb_general, clk_usb;
	int ret;

	ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
	if (ret)
		return ret;

	ret = clk_get_by_name(dev, "usb_general", &clk_usb_general);
	if (ret)
		return ret;

	ret = clk_enable(&clk_usb_general);
	if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
		pr_err("Failed to enable PHY general clock\n");
		return ret;
	}

	ret = clk_get_by_name(dev, "usb", &clk_usb);
	if (ret)
		return ret;

	ret = clk_enable(&clk_usb);
	if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
		pr_err("Failed to enable PHY clock\n");
		return ret;
	}

#if CONFIG_IS_ENABLED(DM_REGULATOR)
	ret = device_get_supply_regulator(dev, "phy-supply", &priv->phy_supply);
	if (ret && ret != -ENOENT) {
		pr_err("Failed to get PHY regulator\n");
		return ret;
	}
#endif
	ret = reset_get_bulk(dev, &priv->resets);
	if (!ret) {
		ret = reset_deassert_bulk(&priv->resets);
		if (ret) {
			pr_err("Failed to deassert reset\n");
			return ret;
		}
	}

	return 0;
}

static int meson_gxbb_usb2_phy_remove(struct udevice *dev)
{
	struct phy_meson_gxbb_usb2_priv *priv = dev_get_priv(dev);

	return reset_release_bulk(&priv->resets);
}

static const struct udevice_id meson_gxbb_usb2_phy_ids[] = {
	{ .compatible = "amlogic,meson8-usb2-phy" },
	{ .compatible = "amlogic,meson8b-usb2-phy" },
	{ .compatible = "amlogic,meson-gxbb-usb2-phy" },
	{ }
};

U_BOOT_DRIVER(meson_gxbb_usb2_phy) = {
	.name = "meson_gxbb_usb2_phy",
	.id = UCLASS_PHY,
	.of_match = meson_gxbb_usb2_phy_ids,
	.probe = meson_gxbb_usb2_phy_probe,
	.remove = meson_gxbb_usb2_phy_remove,
	.ops = &meson_gxbb_usb2_phy_ops,
	.priv_auto	= sizeof(struct phy_meson_gxbb_usb2_priv),
};