2 * Copyright (c) 2016 Linaro Ltd.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/device.h>
15 #include <linux/usb/chipidea.h>
16 #include <linux/ulpi/interface.h>
20 #define ULPI_WAKEUP BIT(31)
21 #define ULPI_RUN BIT(30)
22 #define ULPI_WRITE BIT(29)
23 #define ULPI_SYNC_STATE BIT(27)
24 #define ULPI_ADDR(n) ((n) << 16)
25 #define ULPI_DATA(n) (n)
27 static int ci_ulpi_wait(struct ci_hdrc *ci, u32 mask)
29 unsigned long usec = 10000;
32 if (!hw_read(ci, OP_ULPI_VIEWPORT, mask))
41 static int ci_ulpi_read(struct device *dev, u8 addr)
43 struct ci_hdrc *ci = dev_get_drvdata(dev);
46 hw_write(ci, OP_ULPI_VIEWPORT, 0xffffffff, ULPI_WRITE | ULPI_WAKEUP);
47 ret = ci_ulpi_wait(ci, ULPI_WAKEUP);
51 hw_write(ci, OP_ULPI_VIEWPORT, 0xffffffff, ULPI_RUN | ULPI_ADDR(addr));
52 ret = ci_ulpi_wait(ci, ULPI_RUN);
56 return hw_read(ci, OP_ULPI_VIEWPORT, GENMASK(15, 8)) >> 8;
59 static int ci_ulpi_write(struct device *dev, u8 addr, u8 val)
61 struct ci_hdrc *ci = dev_get_drvdata(dev);
64 hw_write(ci, OP_ULPI_VIEWPORT, 0xffffffff, ULPI_WRITE | ULPI_WAKEUP);
65 ret = ci_ulpi_wait(ci, ULPI_WAKEUP);
69 hw_write(ci, OP_ULPI_VIEWPORT, 0xffffffff,
70 ULPI_RUN | ULPI_WRITE | ULPI_ADDR(addr) | val);
71 return ci_ulpi_wait(ci, ULPI_RUN);
74 int ci_ulpi_init(struct ci_hdrc *ci)
76 if (ci->platdata->phy_mode != USBPHY_INTERFACE_MODE_ULPI)
80 * Set PORTSC correctly so we can read/write ULPI registers for
81 * identification purposes
83 hw_phymode_configure(ci);
85 ci->ulpi_ops.read = ci_ulpi_read;
86 ci->ulpi_ops.write = ci_ulpi_write;
87 ci->ulpi = ulpi_register_interface(ci->dev, &ci->ulpi_ops);
89 dev_err(ci->dev, "failed to register ULPI interface");
91 return PTR_ERR_OR_ZERO(ci->ulpi);
94 void ci_ulpi_exit(struct ci_hdrc *ci)
97 ulpi_unregister_interface(ci->ulpi);
102 int ci_ulpi_resume(struct ci_hdrc *ci)
107 if (hw_read(ci, OP_ULPI_VIEWPORT, ULPI_SYNC_STATE))