1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 Linaro Ltd.
6 #include <linux/device.h>
7 #include <linux/usb/chipidea.h>
8 #include <linux/ulpi/interface.h>
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)
19 static int ci_ulpi_wait(struct ci_hdrc
*ci
, u32 mask
)
21 unsigned long usec
= 10000;
24 if (!hw_read(ci
, OP_ULPI_VIEWPORT
, mask
))
33 static int ci_ulpi_read(struct device
*dev
, u8 addr
)
35 struct ci_hdrc
*ci
= dev_get_drvdata(dev
);
38 hw_write(ci
, OP_ULPI_VIEWPORT
, 0xffffffff, ULPI_WRITE
| ULPI_WAKEUP
);
39 ret
= ci_ulpi_wait(ci
, ULPI_WAKEUP
);
43 hw_write(ci
, OP_ULPI_VIEWPORT
, 0xffffffff, ULPI_RUN
| ULPI_ADDR(addr
));
44 ret
= ci_ulpi_wait(ci
, ULPI_RUN
);
48 return hw_read(ci
, OP_ULPI_VIEWPORT
, GENMASK(15, 8)) >> 8;
51 static int ci_ulpi_write(struct device
*dev
, u8 addr
, u8 val
)
53 struct ci_hdrc
*ci
= dev_get_drvdata(dev
);
56 hw_write(ci
, OP_ULPI_VIEWPORT
, 0xffffffff, ULPI_WRITE
| ULPI_WAKEUP
);
57 ret
= ci_ulpi_wait(ci
, ULPI_WAKEUP
);
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
);
66 int ci_ulpi_init(struct ci_hdrc
*ci
)
68 if (ci
->platdata
->phy_mode
!= USBPHY_INTERFACE_MODE_ULPI
)
72 * Set PORTSC correctly so we can read/write ULPI registers for
73 * identification purposes
75 hw_phymode_configure(ci
);
77 ci
->ulpi_ops
.read
= ci_ulpi_read
;
78 ci
->ulpi_ops
.write
= ci_ulpi_write
;
79 ci
->ulpi
= ulpi_register_interface(ci
->dev
, &ci
->ulpi_ops
);
81 dev_err(ci
->dev
, "failed to register ULPI interface");
83 return PTR_ERR_OR_ZERO(ci
->ulpi
);
86 void ci_ulpi_exit(struct ci_hdrc
*ci
)
89 ulpi_unregister_interface(ci
->ulpi
);
94 int ci_ulpi_resume(struct ci_hdrc
*ci
)
98 if (ci
->platdata
->phy_mode
!= USBPHY_INTERFACE_MODE_ULPI
)
102 if (hw_read(ci
, OP_ULPI_VIEWPORT
, ULPI_SYNC_STATE
))