xref: /linux/drivers/usb/chipidea/ulpi.c (revision 24168c5e6dfbdd5b414f048f47f75d64533296ca)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016 Linaro Ltd.
4  */
5 
6 #include <linux/device.h>
7 #include <linux/usb/chipidea.h>
8 #include <linux/ulpi/interface.h>
9 
10 #include "ci.h"
11 
12 #define ULPI_WAKEUP		BIT(31)
13 #define ULPI_RUN		BIT(30)
14 #define ULPI_WRITE		BIT(29)
15 #define ULPI_SYNC_STATE		BIT(27)
16 #define ULPI_ADDR(n)		((n) << 16)
17 #define ULPI_DATA(n)		(n)
18 
19 static int ci_ulpi_wait(struct ci_hdrc *ci, u32 mask)
20 {
21 	unsigned long usec = 10000;
22 
23 	while (usec--) {
24 		if (!hw_read(ci, OP_ULPI_VIEWPORT, mask))
25 			return 0;
26 
27 		udelay(1);
28 	}
29 
30 	return -ETIMEDOUT;
31 }
32 
33 static int ci_ulpi_read(struct device *dev, u8 addr)
34 {
35 	struct ci_hdrc *ci = dev_get_drvdata(dev);
36 	int ret;
37 
38 	hw_write(ci, OP_ULPI_VIEWPORT, 0xffffffff, ULPI_WRITE | ULPI_WAKEUP);
39 	ret = ci_ulpi_wait(ci, ULPI_WAKEUP);
40 	if (ret)
41 		return ret;
42 
43 	hw_write(ci, OP_ULPI_VIEWPORT, 0xffffffff, ULPI_RUN | ULPI_ADDR(addr));
44 	ret = ci_ulpi_wait(ci, ULPI_RUN);
45 	if (ret)
46 		return ret;
47 
48 	return hw_read(ci, OP_ULPI_VIEWPORT, GENMASK(15, 8)) >> 8;
49 }
50 
51 static int ci_ulpi_write(struct device *dev, u8 addr, u8 val)
52 {
53 	struct ci_hdrc *ci = dev_get_drvdata(dev);
54 	int ret;
55 
56 	hw_write(ci, OP_ULPI_VIEWPORT, 0xffffffff, ULPI_WRITE | ULPI_WAKEUP);
57 	ret = ci_ulpi_wait(ci, ULPI_WAKEUP);
58 	if (ret)
59 		return ret;
60 
61 	hw_write(ci, OP_ULPI_VIEWPORT, 0xffffffff,
62 		 ULPI_RUN | ULPI_WRITE | ULPI_ADDR(addr) | val);
63 	return ci_ulpi_wait(ci, ULPI_RUN);
64 }
65 
66 int ci_ulpi_init(struct ci_hdrc *ci)
67 {
68 	if (ci->platdata->phy_mode != USBPHY_INTERFACE_MODE_ULPI)
69 		return 0;
70 
71 
72 	ci->ulpi_ops.read = ci_ulpi_read;
73 	ci->ulpi_ops.write = ci_ulpi_write;
74 	ci->ulpi = ulpi_register_interface(ci->dev, &ci->ulpi_ops);
75 	if (IS_ERR(ci->ulpi))
76 		dev_err(ci->dev, "failed to register ULPI interface");
77 
78 	return PTR_ERR_OR_ZERO(ci->ulpi);
79 }
80 
81 void ci_ulpi_exit(struct ci_hdrc *ci)
82 {
83 	if (ci->ulpi) {
84 		ulpi_unregister_interface(ci->ulpi);
85 		ci->ulpi = NULL;
86 	}
87 }
88 
89 int ci_ulpi_resume(struct ci_hdrc *ci)
90 {
91 	int cnt = 100000;
92 
93 	if (ci->platdata->phy_mode != USBPHY_INTERFACE_MODE_ULPI)
94 		return 0;
95 
96 	while (cnt-- > 0) {
97 		if (hw_read(ci, OP_ULPI_VIEWPORT, ULPI_SYNC_STATE))
98 			return 0;
99 		udelay(1);
100 	}
101 
102 	return -ETIMEDOUT;
103 }
104