summaryrefslogtreecommitdiffstats
path: root/drivers/usb/imx/imx-usb-phy.c
blob: e3c6832cc80f2ad1675f17ad2f4699fb5e52850b (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
237
238
239
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2013 Sascha Hauer <s.hauer@pengutronix.de>
 */

#include <common.h>
#include <init.h>
#include <io.h>
#include <of.h>
#include <errno.h>
#include <driver.h>
#include <malloc.h>
#include <usb/phy.h>
#include <linux/phy/phy.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <stmp-device.h>
#include <mfd/syscon.h>

#define HW_USBPHY_PWD				0x00
#define HW_USBPHY_TX				0x10
#define HW_USBPHY_CTRL				0x30
#define HW_USBPHY_CTRL_SET			0x34
#define HW_USBPHY_CTRL_CLR			0x38

#define BM_USBPHY_CTRL_ENUTMILEVEL3		BIT(15)
#define BM_USBPHY_CTRL_ENUTMILEVEL2		BIT(14)
#define BM_USBPHY_CTRL_ENHOSTDISCONDETECT	BIT(1)

#define ANADIG_USB1_CHRG_DETECT_SET		0x1b4
#define ANADIG_USB1_CHRG_DETECT_EN_B		BIT(20)
#define ANADIG_USB1_CHRG_DETECT_CHK_CHRG_B	BIT(19)
#define ANADIG_USB1_VBUS_DETECT_STAT		0x1c0
#define ANADIG_USB1_VBUS_DETECT_STAT_VBUS_VALID	BIT(3)
#define ANADIG_USB2_CHRG_DETECT_SET		0x214
#define ANADIG_USB2_VBUS_DETECT_STAT		0x220

struct imx_usbphy {
	struct usb_phy usb_phy;
	struct phy *phy;
	void __iomem *base;
	void __iomem *anatop;
	struct clk *clk;
	struct phy_provider *provider;
	int port_id;

	unsigned int vbus_valid;
};

static int imx_usbphy_phy_init(struct phy *phy)
{
	struct imx_usbphy *imxphy = phy_get_drvdata(phy);
	int ret;

	clk_enable(imxphy->clk);
	mdelay(1);

	ret = stmp_reset_block(imxphy->base + HW_USBPHY_CTRL, false);
	if (ret)
		return ret;

	/* Power up the PHY */
	writel(0, imxphy->base + HW_USBPHY_PWD);

	/* set utmilvl2/3 */
	writel(BM_USBPHY_CTRL_ENUTMILEVEL3 | BM_USBPHY_CTRL_ENUTMILEVEL2,
	       imxphy->base + HW_USBPHY_CTRL_SET);

	if (imxphy->anatop) {
		unsigned int reg = imxphy->port_id ?
			ANADIG_USB1_CHRG_DETECT_SET :
			ANADIG_USB2_CHRG_DETECT_SET;
		/*
		 * The external charger detector needs to be disabled,
		 * or the signal at DP will be poor
		 */
		writel(ANADIG_USB1_CHRG_DETECT_EN_B |
		       ANADIG_USB1_CHRG_DETECT_CHK_CHRG_B,
		       imxphy->anatop + reg);
	}

	return 0;
}

static int imx_usbphy_notify_connect(struct usb_phy *phy,
				     enum usb_device_speed speed)
{
	struct imx_usbphy *imxphy = container_of(phy, struct imx_usbphy,
						 usb_phy);

	if (speed == USB_SPEED_HIGH) {
		writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
		       imxphy->base + HW_USBPHY_CTRL_SET);
	}

	return 0;
}

static int imx_usbphy_notify_disconnect(struct usb_phy *phy,
					enum usb_device_speed speed)
{
	struct imx_usbphy *imxphy = container_of(phy, struct imx_usbphy,
						 usb_phy);

	writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
	       imxphy->base + HW_USBPHY_CTRL_CLR);

	return 0;
}

static struct phy *imx_usbphy_xlate(struct device_d *dev,
				    struct of_phandle_args *args)
{
	struct imx_usbphy *imxphy = dev->priv;

	return imxphy->phy;
}

static struct usb_phy *imx_usbphy_to_usbphy(struct phy *phy)
{
	struct imx_usbphy *imxphy = phy_get_drvdata(phy);

	return &imxphy->usb_phy;
}

static const struct phy_ops imx_phy_ops = {
	.init = imx_usbphy_phy_init,
	.to_usbphy = imx_usbphy_to_usbphy,
};

static int imx_usbphy_get_vbus_state(struct param_d *p, void *priv)
{
	struct imx_usbphy *imxphy = priv;
	unsigned int reg, val;

	reg = imxphy->port_id ?
		ANADIG_USB1_VBUS_DETECT_STAT :
		ANADIG_USB2_VBUS_DETECT_STAT;
	val = readl(imxphy->anatop + reg);

	imxphy->vbus_valid  = !!(val & ANADIG_USB1_VBUS_DETECT_STAT_VBUS_VALID);

	return 0;
}

static int imx_usbphy_probe(struct device_d *dev)
{
	struct resource *iores;
	struct device_node *np = dev->of_node;
	int ret;
	struct imx_usbphy *imxphy;

	imxphy = xzalloc(sizeof(*imxphy));

	ret = of_alias_get_id(np, "usbphy");
	if (ret < 0) {
		dev_dbg(dev, "failed to get alias id, errno %d\n", ret);
		goto err_free;
	}
	imxphy->port_id = ret;

	if (of_get_property(np, "fsl,anatop", NULL)) {
		imxphy->anatop =
			syscon_base_lookup_by_phandle(np, "fsl,anatop");
		ret = PTR_ERR_OR_ZERO(imxphy->anatop);
		if (ret)
			goto err_free;

		/*
		 * This is useful in case of usb-otg = device. In host case
		 * it isn't that useful since we are the supplier of the vbus
		 * signal.
		 */
		dev_add_param_bool(dev, "vbus_valid", param_set_readonly,
				   imx_usbphy_get_vbus_state,
				   &imxphy->vbus_valid, imxphy);
	}

	iores = dev_request_mem_resource(dev, 0);
	if (IS_ERR(iores)) {
		ret = PTR_ERR(iores);
		goto err_free;
	}
	imxphy->base = IOMEM(iores->start);

	imxphy->clk = clk_get(dev, NULL);
	if (IS_ERR(imxphy->clk)) {
		dev_err(dev, "could not get clk: %pe\n", imxphy->clk);
		ret = PTR_ERR(imxphy->clk);
		goto err_clk;
	}

	dev->priv = imxphy;

	imxphy->usb_phy.dev = dev;
	imxphy->usb_phy.notify_connect = imx_usbphy_notify_connect;
	imxphy->usb_phy.notify_disconnect = imx_usbphy_notify_disconnect;
	imxphy->phy = phy_create(dev, NULL, &imx_phy_ops);
	if (IS_ERR(imxphy->phy)) {
		ret = PTR_ERR(imxphy->phy);
		goto err_clk;
	}

	phy_set_drvdata(imxphy->phy, imxphy);

	imxphy->provider = of_phy_provider_register(dev, imx_usbphy_xlate);
	if (IS_ERR(imxphy->provider)) {
		ret = PTR_ERR(imxphy->provider);
		goto err_clk;
	}

	dev_dbg(dev, "phy enabled\n");

	return 0;

err_clk:
err_free:
	free(imxphy);

	return ret;
};

static __maybe_unused struct of_device_id imx_usbphy_dt_ids[] = {
	{
		.compatible = "fsl,imx23-usbphy",
	}, {
		.compatible = "fsl,vf610-usbphy",
	}, {
		/* sentinel */
	},
};

static struct driver_d imx_usbphy_driver = {
	.name   = "imx-usb-phy",
	.probe  = imx_usbphy_probe,
	.of_compatible = DRV_OF_COMPAT(imx_usbphy_dt_ids),
};

fs_platform_driver(imx_usbphy_driver);