Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Generic ULPI USB transceiver support |
| 3 | * |
| 4 | * Copyright (C) 2009 Daniel Mack <daniel@caiaq.de> |
| 5 | * |
| 6 | * Based on sources from |
| 7 | * |
| 8 | * Sascha Hauer <s.hauer@pengutronix.de> |
| 9 | * Freescale Semiconductors |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 28 | #include <linux/usb.h> |
| 29 | #include <linux/usb/otg.h> |
| 30 | #include <linux/usb/ulpi.h> |
| 31 | |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 32 | #define ULPI_ID(vendor, product) (((vendor) << 16) | (product)) |
| 33 | |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 34 | /* ULPI hardcoded IDs, used for probing */ |
| 35 | static unsigned int ulpi_ids[] = { |
| 36 | ULPI_ID(0x04cc, 0x1504), /* NXP ISP1504 */ |
Igor Grinberg | 51a91a5 | 2010-07-15 16:00:15 +0300 | [diff] [blame] | 37 | ULPI_ID(0x0424, 0x0006), /* SMSC USB3319 */ |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 38 | }; |
| 39 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 40 | static int ulpi_set_otg_flags(struct otg_transceiver *otg) |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 41 | { |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 42 | unsigned int flags = ULPI_OTG_CTRL_DP_PULLDOWN | |
| 43 | ULPI_OTG_CTRL_DM_PULLDOWN; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 44 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 45 | if (otg->flags & ULPI_OTG_ID_PULLUP) |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame] | 46 | flags |= ULPI_OTG_CTRL_ID_PULLUP; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 47 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 48 | /* |
| 49 | * ULPI Specification rev.1.1 default |
| 50 | * for Dp/DmPulldown is enabled. |
| 51 | */ |
| 52 | if (otg->flags & ULPI_OTG_DP_PULLDOWN_DIS) |
| 53 | flags &= ~ULPI_OTG_CTRL_DP_PULLDOWN; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 54 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 55 | if (otg->flags & ULPI_OTG_DM_PULLDOWN_DIS) |
| 56 | flags &= ~ULPI_OTG_CTRL_DM_PULLDOWN; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 57 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 58 | if (otg->flags & ULPI_OTG_EXTVBUSIND) |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame] | 59 | flags |= ULPI_OTG_CTRL_EXTVBUSIND; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 60 | |
Eric Bénard | fa345d0 | 2010-07-15 09:20:19 +0200 | [diff] [blame] | 61 | return otg_io_write(otg, flags, ULPI_OTG_CTRL); |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 62 | } |
| 63 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 64 | static int ulpi_set_fc_flags(struct otg_transceiver *otg) |
| 65 | { |
| 66 | unsigned int flags = 0; |
| 67 | |
| 68 | /* |
| 69 | * ULPI Specification rev.1.1 default |
| 70 | * for XcvrSelect is Full Speed. |
| 71 | */ |
| 72 | if (otg->flags & ULPI_FC_HS) |
| 73 | flags |= ULPI_FUNC_CTRL_HIGH_SPEED; |
| 74 | else if (otg->flags & ULPI_FC_LS) |
| 75 | flags |= ULPI_FUNC_CTRL_LOW_SPEED; |
| 76 | else if (otg->flags & ULPI_FC_FS4LS) |
| 77 | flags |= ULPI_FUNC_CTRL_FS4LS; |
| 78 | else |
| 79 | flags |= ULPI_FUNC_CTRL_FULL_SPEED; |
| 80 | |
| 81 | if (otg->flags & ULPI_FC_TERMSEL) |
| 82 | flags |= ULPI_FUNC_CTRL_TERMSELECT; |
| 83 | |
| 84 | /* |
| 85 | * ULPI Specification rev.1.1 default |
| 86 | * for OpMode is Normal Operation. |
| 87 | */ |
| 88 | if (otg->flags & ULPI_FC_OP_NODRV) |
| 89 | flags |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING; |
| 90 | else if (otg->flags & ULPI_FC_OP_DIS_NRZI) |
| 91 | flags |= ULPI_FUNC_CTRL_OPMODE_DISABLE_NRZI; |
| 92 | else if (otg->flags & ULPI_FC_OP_NSYNC_NEOP) |
| 93 | flags |= ULPI_FUNC_CTRL_OPMODE_NOSYNC_NOEOP; |
| 94 | else |
| 95 | flags |= ULPI_FUNC_CTRL_OPMODE_NORMAL; |
| 96 | |
| 97 | /* |
| 98 | * ULPI Specification rev.1.1 default |
| 99 | * for SuspendM is Powered. |
| 100 | */ |
| 101 | flags |= ULPI_FUNC_CTRL_SUSPENDM; |
| 102 | |
| 103 | return otg_io_write(otg, flags, ULPI_FUNC_CTRL); |
| 104 | } |
| 105 | |
| 106 | static int ulpi_set_ic_flags(struct otg_transceiver *otg) |
| 107 | { |
| 108 | unsigned int flags = 0; |
| 109 | |
| 110 | if (otg->flags & ULPI_IC_AUTORESUME) |
| 111 | flags |= ULPI_IFC_CTRL_AUTORESUME; |
| 112 | |
| 113 | if (otg->flags & ULPI_IC_EXTVBUS_INDINV) |
| 114 | flags |= ULPI_IFC_CTRL_EXTERNAL_VBUS; |
| 115 | |
| 116 | if (otg->flags & ULPI_IC_IND_PASSTHRU) |
| 117 | flags |= ULPI_IFC_CTRL_PASSTHRU; |
| 118 | |
| 119 | if (otg->flags & ULPI_IC_PROTECT_DIS) |
| 120 | flags |= ULPI_IFC_CTRL_PROTECT_IFC_DISABLE; |
| 121 | |
| 122 | return otg_io_write(otg, flags, ULPI_IFC_CTRL); |
| 123 | } |
| 124 | |
| 125 | static int ulpi_set_flags(struct otg_transceiver *otg) |
| 126 | { |
| 127 | int ret; |
| 128 | |
| 129 | ret = ulpi_set_otg_flags(otg); |
| 130 | if (ret) |
| 131 | return ret; |
| 132 | |
| 133 | ret = ulpi_set_ic_flags(otg); |
| 134 | if (ret) |
| 135 | return ret; |
| 136 | |
| 137 | return ulpi_set_fc_flags(otg); |
| 138 | } |
| 139 | |
Igor Grinberg | a913819 | 2010-10-10 17:59:18 +0200 | [diff] [blame^] | 140 | static int ulpi_check_integrity(struct otg_transceiver *otg) |
| 141 | { |
| 142 | int ret, i; |
| 143 | unsigned int val = 0x55; |
| 144 | |
| 145 | for (i = 0; i < 2; i++) { |
| 146 | ret = otg_io_write(otg, val, ULPI_SCRATCH); |
| 147 | if (ret < 0) |
| 148 | return ret; |
| 149 | |
| 150 | ret = otg_io_read(otg, ULPI_SCRATCH); |
| 151 | if (ret < 0) |
| 152 | return ret; |
| 153 | |
| 154 | if (ret != val) { |
| 155 | pr_err("ULPI integrity check: failed!"); |
| 156 | return -ENODEV; |
| 157 | } |
| 158 | val = val << 1; |
| 159 | } |
| 160 | |
| 161 | pr_info("ULPI integrity check: passed.\n"); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 166 | static int ulpi_init(struct otg_transceiver *otg) |
| 167 | { |
Wolfram Sang | 7b4a036 | 2010-06-15 12:34:22 +0200 | [diff] [blame] | 168 | int i, vid, pid, ret; |
| 169 | u32 ulpi_id = 0; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 170 | |
Wolfram Sang | 7b4a036 | 2010-06-15 12:34:22 +0200 | [diff] [blame] | 171 | for (i = 0; i < 4; i++) { |
| 172 | ret = otg_io_read(otg, ULPI_PRODUCT_ID_HIGH - i); |
| 173 | if (ret < 0) |
| 174 | return ret; |
| 175 | ulpi_id = (ulpi_id << 8) | ret; |
| 176 | } |
| 177 | vid = ulpi_id & 0xffff; |
| 178 | pid = ulpi_id >> 16; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 179 | |
| 180 | pr_info("ULPI transceiver vendor/product ID 0x%04x/0x%04x\n", vid, pid); |
| 181 | |
| 182 | for (i = 0; i < ARRAY_SIZE(ulpi_ids); i++) |
| 183 | if (ulpi_ids[i] == ULPI_ID(vid, pid)) |
Igor Grinberg | a913819 | 2010-10-10 17:59:18 +0200 | [diff] [blame^] | 184 | break; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 185 | |
Igor Grinberg | a913819 | 2010-10-10 17:59:18 +0200 | [diff] [blame^] | 186 | ret = ulpi_check_integrity(otg); |
| 187 | if (ret) |
| 188 | return ret; |
| 189 | |
| 190 | return ulpi_set_flags(otg); |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 191 | } |
| 192 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 193 | static int ulpi_set_host(struct otg_transceiver *otg, struct usb_bus *host) |
| 194 | { |
| 195 | unsigned int flags = otg_io_read(otg, ULPI_IFC_CTRL); |
| 196 | |
| 197 | if (!host) { |
| 198 | otg->host = NULL; |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | otg->host = host; |
| 203 | |
| 204 | flags &= ~(ULPI_IFC_CTRL_6_PIN_SERIAL_MODE | |
| 205 | ULPI_IFC_CTRL_3_PIN_SERIAL_MODE | |
| 206 | ULPI_IFC_CTRL_CARKITMODE); |
| 207 | |
| 208 | if (otg->flags & ULPI_IC_6PIN_SERIAL) |
| 209 | flags |= ULPI_IFC_CTRL_6_PIN_SERIAL_MODE; |
| 210 | else if (otg->flags & ULPI_IC_3PIN_SERIAL) |
| 211 | flags |= ULPI_IFC_CTRL_3_PIN_SERIAL_MODE; |
| 212 | else if (otg->flags & ULPI_IC_CARKIT) |
| 213 | flags |= ULPI_IFC_CTRL_CARKITMODE; |
| 214 | |
| 215 | return otg_io_write(otg, flags, ULPI_IFC_CTRL); |
| 216 | } |
| 217 | |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 218 | static int ulpi_set_vbus(struct otg_transceiver *otg, bool on) |
| 219 | { |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame] | 220 | unsigned int flags = otg_io_read(otg, ULPI_OTG_CTRL); |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 221 | |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame] | 222 | flags &= ~(ULPI_OTG_CTRL_DRVVBUS | ULPI_OTG_CTRL_DRVVBUS_EXT); |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 223 | |
| 224 | if (on) { |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 225 | if (otg->flags & ULPI_OTG_DRVVBUS) |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame] | 226 | flags |= ULPI_OTG_CTRL_DRVVBUS; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 227 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 228 | if (otg->flags & ULPI_OTG_DRVVBUS_EXT) |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame] | 229 | flags |= ULPI_OTG_CTRL_DRVVBUS_EXT; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 230 | } |
| 231 | |
Eric Bénard | fa345d0 | 2010-07-15 09:20:19 +0200 | [diff] [blame] | 232 | return otg_io_write(otg, flags, ULPI_OTG_CTRL); |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | struct otg_transceiver * |
| 236 | otg_ulpi_create(struct otg_io_access_ops *ops, |
| 237 | unsigned int flags) |
| 238 | { |
| 239 | struct otg_transceiver *otg; |
| 240 | |
| 241 | otg = kzalloc(sizeof(*otg), GFP_KERNEL); |
| 242 | if (!otg) |
| 243 | return NULL; |
| 244 | |
| 245 | otg->label = "ULPI"; |
| 246 | otg->flags = flags; |
| 247 | otg->io_ops = ops; |
| 248 | otg->init = ulpi_init; |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 249 | otg->set_host = ulpi_set_host; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 250 | otg->set_vbus = ulpi_set_vbus; |
| 251 | |
| 252 | return otg; |
| 253 | } |
| 254 | EXPORT_SYMBOL_GPL(otg_ulpi_create); |
| 255 | |