1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2011 Jana Rapava <fermata7@gmail.com>
4 * Copyright (C) 2011 CompuLab, Ltd. <www.compulab.co.il>
6 * Authors: Jana Rapava <fermata7@gmail.com>
7 * Igor Grinberg <grinberg@compulab.co.il>
10 * linux/drivers/usb/otg/ulpi.c
11 * Generic ULPI USB transceiver support
13 * Original Copyright follow:
14 * Copyright (C) 2009 Daniel Mack <daniel@caiaq.de>
16 * Based on sources from
18 * Sascha Hauer <s.hauer@pengutronix.de>
19 * Freescale Semiconductors
26 #define ULPI_ID_REGS_COUNT 4
27 #define ULPI_TEST_VALUE 0x55 /* 0x55 == 0b01010101 */
29 static struct ulpi_regs *ulpi = (struct ulpi_regs *)0;
31 static int ulpi_integrity_check(struct ulpi_viewport *ulpi_vp)
33 u32 val, tval = ULPI_TEST_VALUE;
36 /* Use the 'special' test value to check all bits */
37 for (i = 0; i < 2; i++, tval <<= 1) {
38 err = ulpi_write(ulpi_vp, &ulpi->scratch, tval);
42 val = ulpi_read(ulpi_vp, &ulpi->scratch);
44 printf("ULPI integrity check failed\n");
52 int ulpi_init(struct ulpi_viewport *ulpi_vp)
55 u8 *reg = &ulpi->product_id_high;
58 /* Assemble ID from four ULPI ID registers (8 bits each). */
59 for (i = 0; i < ULPI_ID_REGS_COUNT; i++) {
60 val = ulpi_read(ulpi_vp, reg - i);
61 if (val == ULPI_ERROR)
67 /* Split ID into vendor and product ID. */
68 debug("ULPI transceiver ID 0x%04x:0x%04x\n", id >> 16, id & 0xffff);
70 return ulpi_integrity_check(ulpi_vp);
73 int ulpi_select_transceiver(struct ulpi_viewport *ulpi_vp, unsigned speed)
75 u32 tspeed = ULPI_FC_FULL_SPEED;
79 case ULPI_FC_HIGH_SPEED:
80 case ULPI_FC_FULL_SPEED:
81 case ULPI_FC_LOW_SPEED:
86 printf("ULPI: %s: wrong transceiver speed specified: %u, "
87 "falling back to full speed\n", __func__, speed);
90 val = ulpi_read(ulpi_vp, &ulpi->function_ctrl);
91 if (val == ULPI_ERROR)
94 /* clear the previous speed setting */
95 val = (val & ~ULPI_FC_XCVRSEL_MASK) | tspeed;
97 return ulpi_write(ulpi_vp, &ulpi->function_ctrl, val);
100 int ulpi_set_vbus(struct ulpi_viewport *ulpi_vp, int on, int ext_power)
102 u32 flags = ULPI_OTG_DRVVBUS;
103 u8 *reg = on ? &ulpi->otg_ctrl_set : &ulpi->otg_ctrl_clear;
106 flags |= ULPI_OTG_DRVVBUS_EXT;
108 return ulpi_write(ulpi_vp, reg, flags);
111 int ulpi_set_vbus_indicator(struct ulpi_viewport *ulpi_vp, int external,
112 int passthu, int complement)
117 reg = external ? &ulpi->otg_ctrl_set : &ulpi->otg_ctrl_clear;
118 val = ulpi_write(ulpi_vp, reg, ULPI_OTG_EXTVBUSIND);
122 flags = passthu ? ULPI_IFACE_PASSTHRU : 0;
123 flags |= complement ? ULPI_IFACE_EXTVBUS_COMPLEMENT : 0;
125 val = ulpi_read(ulpi_vp, &ulpi->iface_ctrl);
126 if (val == ULPI_ERROR)
129 val = val & ~(ULPI_IFACE_PASSTHRU & ULPI_IFACE_EXTVBUS_COMPLEMENT);
131 val = ulpi_write(ulpi_vp, &ulpi->iface_ctrl, val);
138 int ulpi_set_pd(struct ulpi_viewport *ulpi_vp, int enable)
140 u32 val = ULPI_OTG_DP_PULLDOWN | ULPI_OTG_DM_PULLDOWN;
141 u8 *reg = enable ? &ulpi->otg_ctrl_set : &ulpi->otg_ctrl_clear;
143 return ulpi_write(ulpi_vp, reg, val);
146 int ulpi_opmode_sel(struct ulpi_viewport *ulpi_vp, unsigned opmode)
148 u32 topmode = ULPI_FC_OPMODE_NORMAL;
152 case ULPI_FC_OPMODE_NORMAL:
153 case ULPI_FC_OPMODE_NONDRIVING:
154 case ULPI_FC_OPMODE_DISABLE_NRZI:
155 case ULPI_FC_OPMODE_NOSYNC_NOEOP:
159 printf("ULPI: %s: wrong OpMode specified: %u, "
160 "falling back to OpMode Normal\n", __func__, opmode);
163 val = ulpi_read(ulpi_vp, &ulpi->function_ctrl);
164 if (val == ULPI_ERROR)
167 /* clear the previous opmode setting */
168 val = (val & ~ULPI_FC_OPMODE_MASK) | topmode;
170 return ulpi_write(ulpi_vp, &ulpi->function_ctrl, val);
173 int ulpi_serial_mode_enable(struct ulpi_viewport *ulpi_vp, unsigned smode)
176 case ULPI_IFACE_6_PIN_SERIAL_MODE:
177 case ULPI_IFACE_3_PIN_SERIAL_MODE:
180 printf("ULPI: %s: unrecognized Serial Mode specified: %u\n",
185 return ulpi_write(ulpi_vp, &ulpi->iface_ctrl_set, smode);
188 int ulpi_suspend(struct ulpi_viewport *ulpi_vp)
192 err = ulpi_write(ulpi_vp, &ulpi->function_ctrl_clear,
195 printf("ULPI: %s: failed writing the suspend bit\n", __func__);
201 * Wait for ULPI PHY reset to complete.
202 * Actual wait for reset must be done in a view port specific way,
203 * because it involves checking the DIR line.
205 static int __ulpi_reset_wait(struct ulpi_viewport *ulpi_vp)
208 int timeout = CONFIG_USB_ULPI_TIMEOUT;
210 /* Wait for the RESET bit to become zero */
213 * This function is generic and suppose to work
214 * with any viewport, so we cheat here and don't check
215 * for the error of ulpi_read(), if there is one, then
216 * there will be a timeout.
218 val = ulpi_read(ulpi_vp, &ulpi->function_ctrl);
219 if (!(val & ULPI_FC_RESET))
225 printf("ULPI: %s: reset timed out\n", __func__);
229 int ulpi_reset_wait(struct ulpi_viewport *ulpi_vp)
230 __attribute__((weak, alias("__ulpi_reset_wait")));
232 int ulpi_reset(struct ulpi_viewport *ulpi_vp)
236 err = ulpi_write(ulpi_vp,
237 &ulpi->function_ctrl_set, ULPI_FC_RESET);
239 printf("ULPI: %s: failed writing reset bit\n", __func__);
243 return ulpi_reset_wait(ulpi_vp);