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/iopoll.h>
11 #include <linux/usb/otg.h>
12 #include <linux/usb/ulpi.h>
13 
14 #define ULPI_VIEW_WAKEUP	(1 << 31)
15 #define ULPI_VIEW_RUN		(1 << 30)
16 #define ULPI_VIEW_WRITE		(1 << 29)
17 #define ULPI_VIEW_READ		(0 << 29)
18 #define ULPI_VIEW_ADDR(x)	(((x) & 0xff) << 16)
19 #define ULPI_VIEW_DATA_READ(x)	(((x) >> 8) & 0xff)
20 #define ULPI_VIEW_DATA_WRITE(x)	((x) & 0xff)
21 
22 static int ulpi_viewport_wait(void __iomem *view, u32 mask)
23 {
24 	u32 val;
25 
26 	return readl_poll_timeout_atomic(view, val, !(val & mask), 1, 2000);
27 }
28 
29 static int ulpi_viewport_read(struct usb_phy *otg, u32 reg)
30 {
31 	int ret;
32 	void __iomem *view = otg->io_priv;
33 
34 	writel(ULPI_VIEW_WAKEUP | ULPI_VIEW_WRITE, view);
35 	ret = ulpi_viewport_wait(view, ULPI_VIEW_WAKEUP);
36 	if (ret)
37 		return ret;
38 
39 	writel(ULPI_VIEW_RUN | ULPI_VIEW_READ | ULPI_VIEW_ADDR(reg), view);
40 	ret = ulpi_viewport_wait(view, ULPI_VIEW_RUN);
41 	if (ret)
42 		return ret;
43 
44 	return ULPI_VIEW_DATA_READ(readl(view));
45 }
46 
47 static int ulpi_viewport_write(struct usb_phy *otg, u32 val, u32 reg)
48 {
49 	int ret;
50 	void __iomem *view = otg->io_priv;
51 
52 	writel(ULPI_VIEW_WAKEUP | ULPI_VIEW_WRITE, view);
53 	ret = ulpi_viewport_wait(view, ULPI_VIEW_WAKEUP);
54 	if (ret)
55 		return ret;
56 
57 	writel(ULPI_VIEW_RUN | ULPI_VIEW_WRITE | ULPI_VIEW_DATA_WRITE(val) |
58 						 ULPI_VIEW_ADDR(reg), view);
59 
60 	return ulpi_viewport_wait(view, ULPI_VIEW_RUN);
61 }
62 
63 struct usb_phy_io_ops ulpi_viewport_access_ops = {
64 	.read	= ulpi_viewport_read,
65 	.write	= ulpi_viewport_write,
66 };
67 EXPORT_SYMBOL_GPL(ulpi_viewport_access_ops);
68