xref: /linux/drivers/usb/phy/phy-ulpi-viewport.c (revision 6fdcba32711044c35c0e1b094cbd8f3f0b4472c9)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2011 Google, Inc.
4  */
5 
6 #include <linux/export.h>
7 #include <linux/kernel.h>
8 #include <linux/usb.h>
9 #include <linux/io.h>
10 #include <linux/usb/otg.h>
11 #include <linux/usb/ulpi.h>
12 
13 #define ULPI_VIEW_WAKEUP	(1 << 31)
14 #define ULPI_VIEW_RUN		(1 << 30)
15 #define ULPI_VIEW_WRITE		(1 << 29)
16 #define ULPI_VIEW_READ		(0 << 29)
17 #define ULPI_VIEW_ADDR(x)	(((x) & 0xff) << 16)
18 #define ULPI_VIEW_DATA_READ(x)	(((x) >> 8) & 0xff)
19 #define ULPI_VIEW_DATA_WRITE(x)	((x) & 0xff)
20 
21 static int ulpi_viewport_wait(void __iomem *view, u32 mask)
22 {
23 	unsigned long usec = 2000;
24 
25 	while (usec--) {
26 		if (!(readl(view) & mask))
27 			return 0;
28 
29 		udelay(1);
30 	}
31 
32 	return -ETIMEDOUT;
33 }
34 
35 static int ulpi_viewport_read(struct usb_phy *otg, u32 reg)
36 {
37 	int ret;
38 	void __iomem *view = otg->io_priv;
39 
40 	writel(ULPI_VIEW_WAKEUP | ULPI_VIEW_WRITE, view);
41 	ret = ulpi_viewport_wait(view, ULPI_VIEW_WAKEUP);
42 	if (ret)
43 		return ret;
44 
45 	writel(ULPI_VIEW_RUN | ULPI_VIEW_READ | ULPI_VIEW_ADDR(reg), view);
46 	ret = ulpi_viewport_wait(view, ULPI_VIEW_RUN);
47 	if (ret)
48 		return ret;
49 
50 	return ULPI_VIEW_DATA_READ(readl(view));
51 }
52 
53 static int ulpi_viewport_write(struct usb_phy *otg, u32 val, u32 reg)
54 {
55 	int ret;
56 	void __iomem *view = otg->io_priv;
57 
58 	writel(ULPI_VIEW_WAKEUP | ULPI_VIEW_WRITE, view);
59 	ret = ulpi_viewport_wait(view, ULPI_VIEW_WAKEUP);
60 	if (ret)
61 		return ret;
62 
63 	writel(ULPI_VIEW_RUN | ULPI_VIEW_WRITE | ULPI_VIEW_DATA_WRITE(val) |
64 						 ULPI_VIEW_ADDR(reg), view);
65 
66 	return ulpi_viewport_wait(view, ULPI_VIEW_RUN);
67 }
68 
69 struct usb_phy_io_ops ulpi_viewport_access_ops = {
70 	.read	= ulpi_viewport_read,
71 	.write	= ulpi_viewport_write,
72 };
73 EXPORT_SYMBOL_GPL(ulpi_viewport_access_ops);
74