Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 Jana Rapava <fermata7@gmail.com> |
| 4 | * Copyright (C) 2011 CompuLab, Ltd. <www.compulab.co.il> |
| 5 | * |
| 6 | * Authors: Jana Rapava <fermata7@gmail.com> |
| 7 | * Igor Grinberg <grinberg@compulab.co.il> |
| 8 | * |
| 9 | * Based on: |
| 10 | * linux/drivers/usb/otg/ulpi.c |
| 11 | * Generic ULPI USB transceiver support |
| 12 | * |
| 13 | * Original Copyright follow: |
| 14 | * Copyright (C) 2009 Daniel Mack <daniel@caiaq.de> |
| 15 | * |
| 16 | * Based on sources from |
| 17 | * |
| 18 | * Sascha Hauer <s.hauer@pengutronix.de> |
| 19 | * Freescale Semiconductors |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 22 | #include <exports.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 23 | #include <log.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 24 | #include <linux/delay.h> |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 25 | #include <usb/ulpi.h> |
| 26 | |
| 27 | #define ULPI_ID_REGS_COUNT 4 |
| 28 | #define ULPI_TEST_VALUE 0x55 /* 0x55 == 0b01010101 */ |
| 29 | |
| 30 | static struct ulpi_regs *ulpi = (struct ulpi_regs *)0; |
| 31 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 32 | static int ulpi_integrity_check(struct ulpi_viewport *ulpi_vp) |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 33 | { |
Igor Grinberg | 7b4d768 | 2011-12-12 12:08:33 +0200 | [diff] [blame] | 34 | u32 val, tval = ULPI_TEST_VALUE; |
| 35 | int err, i; |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 36 | |
| 37 | /* Use the 'special' test value to check all bits */ |
| 38 | for (i = 0; i < 2; i++, tval <<= 1) { |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 39 | err = ulpi_write(ulpi_vp, &ulpi->scratch, tval); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 40 | if (err) |
| 41 | return err; |
| 42 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 43 | val = ulpi_read(ulpi_vp, &ulpi->scratch); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 44 | if (val != tval) { |
| 45 | printf("ULPI integrity check failed\n"); |
| 46 | return val; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 53 | int ulpi_init(struct ulpi_viewport *ulpi_vp) |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 54 | { |
| 55 | u32 val, id = 0; |
| 56 | u8 *reg = &ulpi->product_id_high; |
| 57 | int i; |
| 58 | |
| 59 | /* Assemble ID from four ULPI ID registers (8 bits each). */ |
| 60 | for (i = 0; i < ULPI_ID_REGS_COUNT; i++) { |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 61 | val = ulpi_read(ulpi_vp, reg - i); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 62 | if (val == ULPI_ERROR) |
| 63 | return val; |
| 64 | |
| 65 | id = (id << 8) | val; |
| 66 | } |
| 67 | |
| 68 | /* Split ID into vendor and product ID. */ |
| 69 | debug("ULPI transceiver ID 0x%04x:0x%04x\n", id >> 16, id & 0xffff); |
| 70 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 71 | return ulpi_integrity_check(ulpi_vp); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 74 | int ulpi_select_transceiver(struct ulpi_viewport *ulpi_vp, unsigned speed) |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 75 | { |
Igor Grinberg | 1e79474 | 2011-12-14 08:16:03 +0200 | [diff] [blame] | 76 | u32 tspeed = ULPI_FC_FULL_SPEED; |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 77 | u32 val; |
| 78 | |
| 79 | switch (speed) { |
| 80 | case ULPI_FC_HIGH_SPEED: |
| 81 | case ULPI_FC_FULL_SPEED: |
| 82 | case ULPI_FC_LOW_SPEED: |
| 83 | case ULPI_FC_FS4LS: |
| 84 | tspeed = speed; |
| 85 | break; |
| 86 | default: |
Igor Grinberg | 695207a | 2011-12-12 12:08:34 +0200 | [diff] [blame] | 87 | printf("ULPI: %s: wrong transceiver speed specified: %u, " |
| 88 | "falling back to full speed\n", __func__, speed); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 91 | val = ulpi_read(ulpi_vp, &ulpi->function_ctrl); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 92 | if (val == ULPI_ERROR) |
| 93 | return val; |
| 94 | |
| 95 | /* clear the previous speed setting */ |
| 96 | val = (val & ~ULPI_FC_XCVRSEL_MASK) | tspeed; |
| 97 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 98 | return ulpi_write(ulpi_vp, &ulpi->function_ctrl, val); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Lucas Stach | c86753b | 2012-10-01 00:44:34 +0200 | [diff] [blame] | 101 | int ulpi_set_vbus(struct ulpi_viewport *ulpi_vp, int on, int ext_power) |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 102 | { |
| 103 | u32 flags = ULPI_OTG_DRVVBUS; |
| 104 | u8 *reg = on ? &ulpi->otg_ctrl_set : &ulpi->otg_ctrl_clear; |
| 105 | |
| 106 | if (ext_power) |
| 107 | flags |= ULPI_OTG_DRVVBUS_EXT; |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 108 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 109 | return ulpi_write(ulpi_vp, reg, flags); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Lucas Stach | c86753b | 2012-10-01 00:44:34 +0200 | [diff] [blame] | 112 | int ulpi_set_vbus_indicator(struct ulpi_viewport *ulpi_vp, int external, |
| 113 | int passthu, int complement) |
| 114 | { |
| 115 | u32 flags, val; |
| 116 | u8 *reg; |
| 117 | |
| 118 | reg = external ? &ulpi->otg_ctrl_set : &ulpi->otg_ctrl_clear; |
| 119 | val = ulpi_write(ulpi_vp, reg, ULPI_OTG_EXTVBUSIND); |
| 120 | if (val) |
| 121 | return val; |
| 122 | |
| 123 | flags = passthu ? ULPI_IFACE_PASSTHRU : 0; |
| 124 | flags |= complement ? ULPI_IFACE_EXTVBUS_COMPLEMENT : 0; |
| 125 | |
| 126 | val = ulpi_read(ulpi_vp, &ulpi->iface_ctrl); |
| 127 | if (val == ULPI_ERROR) |
| 128 | return val; |
| 129 | |
| 130 | val = val & ~(ULPI_IFACE_PASSTHRU & ULPI_IFACE_EXTVBUS_COMPLEMENT); |
| 131 | val |= flags; |
| 132 | val = ulpi_write(ulpi_vp, &ulpi->iface_ctrl, val); |
| 133 | if (val) |
| 134 | return val; |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 139 | int ulpi_set_pd(struct ulpi_viewport *ulpi_vp, int enable) |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 140 | { |
| 141 | u32 val = ULPI_OTG_DP_PULLDOWN | ULPI_OTG_DM_PULLDOWN; |
| 142 | u8 *reg = enable ? &ulpi->otg_ctrl_set : &ulpi->otg_ctrl_clear; |
| 143 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 144 | return ulpi_write(ulpi_vp, reg, val); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 147 | int ulpi_opmode_sel(struct ulpi_viewport *ulpi_vp, unsigned opmode) |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 148 | { |
Igor Grinberg | 1e79474 | 2011-12-14 08:16:03 +0200 | [diff] [blame] | 149 | u32 topmode = ULPI_FC_OPMODE_NORMAL; |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 150 | u32 val; |
| 151 | |
| 152 | switch (opmode) { |
| 153 | case ULPI_FC_OPMODE_NORMAL: |
| 154 | case ULPI_FC_OPMODE_NONDRIVING: |
| 155 | case ULPI_FC_OPMODE_DISABLE_NRZI: |
| 156 | case ULPI_FC_OPMODE_NOSYNC_NOEOP: |
| 157 | topmode = opmode; |
| 158 | break; |
| 159 | default: |
Igor Grinberg | 695207a | 2011-12-12 12:08:34 +0200 | [diff] [blame] | 160 | printf("ULPI: %s: wrong OpMode specified: %u, " |
| 161 | "falling back to OpMode Normal\n", __func__, opmode); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 164 | val = ulpi_read(ulpi_vp, &ulpi->function_ctrl); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 165 | if (val == ULPI_ERROR) |
| 166 | return val; |
| 167 | |
| 168 | /* clear the previous opmode setting */ |
| 169 | val = (val & ~ULPI_FC_OPMODE_MASK) | topmode; |
| 170 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 171 | return ulpi_write(ulpi_vp, &ulpi->function_ctrl, val); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 174 | int ulpi_serial_mode_enable(struct ulpi_viewport *ulpi_vp, unsigned smode) |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 175 | { |
| 176 | switch (smode) { |
| 177 | case ULPI_IFACE_6_PIN_SERIAL_MODE: |
| 178 | case ULPI_IFACE_3_PIN_SERIAL_MODE: |
| 179 | break; |
| 180 | default: |
Igor Grinberg | 695207a | 2011-12-12 12:08:34 +0200 | [diff] [blame] | 181 | printf("ULPI: %s: unrecognized Serial Mode specified: %u\n", |
| 182 | __func__, smode); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 183 | return ULPI_ERROR; |
| 184 | } |
| 185 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 186 | return ulpi_write(ulpi_vp, &ulpi->iface_ctrl_set, smode); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 189 | int ulpi_suspend(struct ulpi_viewport *ulpi_vp) |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 190 | { |
Igor Grinberg | 7b4d768 | 2011-12-12 12:08:33 +0200 | [diff] [blame] | 191 | int err; |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 192 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 193 | err = ulpi_write(ulpi_vp, &ulpi->function_ctrl_clear, |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 194 | ULPI_FC_SUSPENDM); |
| 195 | if (err) |
| 196 | printf("ULPI: %s: failed writing the suspend bit\n", __func__); |
| 197 | |
| 198 | return err; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Wait for ULPI PHY reset to complete. |
| 203 | * Actual wait for reset must be done in a view port specific way, |
| 204 | * because it involves checking the DIR line. |
| 205 | */ |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 206 | static int __ulpi_reset_wait(struct ulpi_viewport *ulpi_vp) |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 207 | { |
| 208 | u32 val; |
Tom Rini | 364d002 | 2023-01-10 11:19:45 -0500 | [diff] [blame] | 209 | int timeout = CFG_USB_ULPI_TIMEOUT; |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 210 | |
| 211 | /* Wait for the RESET bit to become zero */ |
| 212 | while (--timeout) { |
| 213 | /* |
| 214 | * This function is generic and suppose to work |
| 215 | * with any viewport, so we cheat here and don't check |
| 216 | * for the error of ulpi_read(), if there is one, then |
| 217 | * there will be a timeout. |
| 218 | */ |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 219 | val = ulpi_read(ulpi_vp, &ulpi->function_ctrl); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 220 | if (!(val & ULPI_FC_RESET)) |
| 221 | return 0; |
| 222 | |
| 223 | udelay(1); |
| 224 | } |
| 225 | |
| 226 | printf("ULPI: %s: reset timed out\n", __func__); |
| 227 | |
| 228 | return ULPI_ERROR; |
| 229 | } |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 230 | int ulpi_reset_wait(struct ulpi_viewport *ulpi_vp) |
| 231 | __attribute__((weak, alias("__ulpi_reset_wait"))); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 232 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 233 | int ulpi_reset(struct ulpi_viewport *ulpi_vp) |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 234 | { |
Igor Grinberg | 7b4d768 | 2011-12-12 12:08:33 +0200 | [diff] [blame] | 235 | int err; |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 236 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 237 | err = ulpi_write(ulpi_vp, |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 238 | &ulpi->function_ctrl_set, ULPI_FC_RESET); |
| 239 | if (err) { |
| 240 | printf("ULPI: %s: failed writing reset bit\n", __func__); |
| 241 | return err; |
| 242 | } |
| 243 | |
Govindraj.R | 6645d9e | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 244 | return ulpi_reset_wait(ulpi_vp); |
Jana Rapava | a523595 | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 245 | } |