1 // SPDX-License-Identifier: GPL-2.0
3 * ulpi.c - DesignWare USB3 Controller's ULPI PHY interface
5 * Copyright (C) 2015 Intel Corporation
7 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
10 #include <linux/ulpi/regs.h>
15 #define DWC3_ULPI_ADDR(a) \
16 ((a >= ULPI_EXT_VENDOR_SPECIFIC) ? \
17 DWC3_GUSB2PHYACC_ADDR(ULPI_ACCESS_EXTENDED) | \
18 DWC3_GUSB2PHYACC_EXTEND_ADDR(a) : DWC3_GUSB2PHYACC_ADDR(a))
20 static int dwc3_ulpi_busyloop(struct dwc3
*dwc
)
22 unsigned count
= 1000;
26 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYACC(0));
27 if (!(reg
& DWC3_GUSB2PHYACC_BUSY
))
35 static int dwc3_ulpi_read(struct device
*dev
, u8 addr
)
37 struct dwc3
*dwc
= dev_get_drvdata(dev
);
41 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(0));
42 if (reg
& DWC3_GUSB2PHYCFG_SUSPHY
) {
43 reg
&= ~DWC3_GUSB2PHYCFG_SUSPHY
;
44 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
47 reg
= DWC3_GUSB2PHYACC_NEWREGREQ
| DWC3_ULPI_ADDR(addr
);
48 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYACC(0), reg
);
50 ret
= dwc3_ulpi_busyloop(dwc
);
54 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYACC(0));
56 return DWC3_GUSB2PHYACC_DATA(reg
);
59 static int dwc3_ulpi_write(struct device
*dev
, u8 addr
, u8 val
)
61 struct dwc3
*dwc
= dev_get_drvdata(dev
);
64 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(0));
65 if (reg
& DWC3_GUSB2PHYCFG_SUSPHY
) {
66 reg
&= ~DWC3_GUSB2PHYCFG_SUSPHY
;
67 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
70 reg
= DWC3_GUSB2PHYACC_NEWREGREQ
| DWC3_ULPI_ADDR(addr
);
71 reg
|= DWC3_GUSB2PHYACC_WRITE
| val
;
72 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYACC(0), reg
);
74 return dwc3_ulpi_busyloop(dwc
);
77 static const struct ulpi_ops dwc3_ulpi_ops
= {
78 .read
= dwc3_ulpi_read
,
79 .write
= dwc3_ulpi_write
,
82 int dwc3_ulpi_init(struct dwc3
*dwc
)
84 /* Register the interface */
85 dwc
->ulpi
= ulpi_register_interface(dwc
->dev
, &dwc3_ulpi_ops
);
86 if (IS_ERR(dwc
->ulpi
)) {
87 dev_err(dwc
->dev
, "failed to register ULPI interface");
88 return PTR_ERR(dwc
->ulpi
);
94 void dwc3_ulpi_exit(struct dwc3
*dwc
)
97 ulpi_unregister_interface(dwc
->ulpi
);