1 // SPDX-License-Identifier: GPL-2.0+
4 * Copyright (C) 2011 CompuLab, Ltd. <www.compulab.co.il>
10 * linux/drivers/usb/otg/ulpi.c
11 * Generic ULPI USB transceiver support
13 * Original Copyright follow:
16 * Based on sources from
19 * Freescale Semiconductors
25 #include <linux/delay.h>
28 #define ULPI_ID_REGS_COUNT 4
29 #define ULPI_TEST_VALUE 0x55 /* 0x55 == 0b01010101 */
31 static struct ulpi_regs *ulpi = (struct ulpi_regs *)0;
33 static int ulpi_integrity_check(struct ulpi_viewport *ulpi_vp)
35 u32 val, tval = ULPI_TEST_VALUE;
38 /* Use the 'special' test value to check all bits */
39 for (i = 0; i < 2; i++, tval <<= 1) {
40 err = ulpi_write(ulpi_vp, &ulpi->scratch, tval);
44 val = ulpi_read(ulpi_vp, &ulpi->scratch);
46 printf("ULPI integrity check failed\n");
54 int ulpi_init(struct ulpi_viewport *ulpi_vp)
57 u8 *reg = &ulpi->product_id_high;
60 /* Assemble ID from four ULPI ID registers (8 bits each). */
61 for (i = 0; i < ULPI_ID_REGS_COUNT; i++) {
62 val = ulpi_read(ulpi_vp, reg - i);
63 if (val == ULPI_ERROR)
69 /* Split ID into vendor and product ID. */
70 debug("ULPI transceiver ID 0x%04x:0x%04x\n", id >> 16, id & 0xffff);
72 return ulpi_integrity_check(ulpi_vp);
75 int ulpi_select_transceiver(struct ulpi_viewport *ulpi_vp, unsigned speed)
77 u32 tspeed = ULPI_FC_FULL_SPEED;
81 case ULPI_FC_HIGH_SPEED:
82 case ULPI_FC_FULL_SPEED:
83 case ULPI_FC_LOW_SPEED:
88 printf("ULPI: %s: wrong transceiver speed specified: %u, "
89 "falling back to full speed\n", __func__, speed);
92 val = ulpi_read(ulpi_vp, &ulpi->function_ctrl);
93 if (val == ULPI_ERROR)
96 /* clear the previous speed setting */
97 val = (val & ~ULPI_FC_XCVRSEL_MASK) | tspeed;
99 return ulpi_write(ulpi_vp, &ulpi->function_ctrl, val);
102 int ulpi_set_vbus(struct ulpi_viewport *ulpi_vp, int on, int ext_power)
104 u32 flags = ULPI_OTG_DRVVBUS;
105 u8 *reg = on ? &ulpi->otg_ctrl_set : &ulpi->otg_ctrl_clear;
108 flags |= ULPI_OTG_DRVVBUS_EXT;
110 return ulpi_write(ulpi_vp, reg, flags);
113 int ulpi_set_vbus_indicator(struct ulpi_viewport *ulpi_vp, int external,
114 int passthu, int complement)
119 reg = external ? &ulpi->otg_ctrl_set : &ulpi->otg_ctrl_clear;
120 val = ulpi_write(ulpi_vp, reg, ULPI_OTG_EXTVBUSIND);
124 flags = passthu ? ULPI_IFACE_PASSTHRU : 0;
125 flags |= complement ? ULPI_IFACE_EXTVBUS_COMPLEMENT : 0;
127 val = ulpi_read(ulpi_vp, &ulpi->iface_ctrl);
128 if (val == ULPI_ERROR)
131 val = val & ~(ULPI_IFACE_PASSTHRU & ULPI_IFACE_EXTVBUS_COMPLEMENT);
133 val = ulpi_write(ulpi_vp, &ulpi->iface_ctrl, val);
140 int ulpi_set_pd(struct ulpi_viewport *ulpi_vp, int enable)
142 u32 val = ULPI_OTG_DP_PULLDOWN | ULPI_OTG_DM_PULLDOWN;
143 u8 *reg = enable ? &ulpi->otg_ctrl_set : &ulpi->otg_ctrl_clear;
145 return ulpi_write(ulpi_vp, reg, val);
148 int ulpi_opmode_sel(struct ulpi_viewport *ulpi_vp, unsigned opmode)
150 u32 topmode = ULPI_FC_OPMODE_NORMAL;
154 case ULPI_FC_OPMODE_NORMAL:
155 case ULPI_FC_OPMODE_NONDRIVING:
156 case ULPI_FC_OPMODE_DISABLE_NRZI:
157 case ULPI_FC_OPMODE_NOSYNC_NOEOP:
161 printf("ULPI: %s: wrong OpMode specified: %u, "
162 "falling back to OpMode Normal\n", __func__, opmode);
165 val = ulpi_read(ulpi_vp, &ulpi->function_ctrl);
166 if (val == ULPI_ERROR)
169 /* clear the previous opmode setting */
170 val = (val & ~ULPI_FC_OPMODE_MASK) | topmode;
172 return ulpi_write(ulpi_vp, &ulpi->function_ctrl, val);
175 int ulpi_serial_mode_enable(struct ulpi_viewport *ulpi_vp, unsigned smode)
178 case ULPI_IFACE_6_PIN_SERIAL_MODE:
179 case ULPI_IFACE_3_PIN_SERIAL_MODE:
182 printf("ULPI: %s: unrecognized Serial Mode specified: %u\n",
187 return ulpi_write(ulpi_vp, &ulpi->iface_ctrl_set, smode);
190 int ulpi_suspend(struct ulpi_viewport *ulpi_vp)
194 err = ulpi_write(ulpi_vp, &ulpi->function_ctrl_clear,
197 printf("ULPI: %s: failed writing the suspend bit\n", __func__);
203 * Wait for ULPI PHY reset to complete.
204 * Actual wait for reset must be done in a view port specific way,
205 * because it involves checking the DIR line.
207 static int __ulpi_reset_wait(struct ulpi_viewport *ulpi_vp)
210 int timeout = CFG_USB_ULPI_TIMEOUT;
212 /* Wait for the RESET bit to become zero */
215 * This function is generic and suppose to work
216 * with any viewport, so we cheat here and don't check
217 * for the error of ulpi_read(), if there is one, then
218 * there will be a timeout.
220 val = ulpi_read(ulpi_vp, &ulpi->function_ctrl);
221 if (!(val & ULPI_FC_RESET))
227 printf("ULPI: %s: reset timed out\n", __func__);
231 int ulpi_reset_wait(struct ulpi_viewport *ulpi_vp)
232 __attribute__((weak, alias("__ulpi_reset_wait")));
234 int ulpi_reset(struct ulpi_viewport *ulpi_vp)
238 err = ulpi_write(ulpi_vp,
239 &ulpi->function_ctrl_set, ULPI_FC_RESET);
241 printf("ULPI: %s: failed writing reset bit\n", __func__);
245 return ulpi_reset_wait(ulpi_vp);