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 | |
Igor Grinberg | 96b9e83 | 2010-10-10 17:59:19 +0200 | [diff] [blame] | 32 | |
| 33 | struct ulpi_info { |
| 34 | unsigned int id; |
| 35 | char *name; |
| 36 | }; |
| 37 | |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 38 | #define ULPI_ID(vendor, product) (((vendor) << 16) | (product)) |
Igor Grinberg | 96b9e83 | 2010-10-10 17:59:19 +0200 | [diff] [blame] | 39 | #define ULPI_INFO(_id, _name) \ |
| 40 | { \ |
| 41 | .id = (_id), \ |
| 42 | .name = (_name), \ |
| 43 | } |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 44 | |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 45 | /* ULPI hardcoded IDs, used for probing */ |
Igor Grinberg | 96b9e83 | 2010-10-10 17:59:19 +0200 | [diff] [blame] | 46 | static struct ulpi_info ulpi_ids[] = { |
| 47 | ULPI_INFO(ULPI_ID(0x04cc, 0x1504), "NXP ISP1504"), |
Fabio Estevam | 1e4cba8 | 2010-12-20 21:54:30 -0200 | [diff] [blame] | 48 | ULPI_INFO(ULPI_ID(0x0424, 0x0006), "SMSC USB331x"), |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 49 | }; |
| 50 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 51 | static int ulpi_set_otg_flags(struct otg_transceiver *otg) |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 52 | { |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 53 | unsigned int flags = ULPI_OTG_CTRL_DP_PULLDOWN | |
| 54 | ULPI_OTG_CTRL_DM_PULLDOWN; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 55 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 56 | if (otg->flags & ULPI_OTG_ID_PULLUP) |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame] | 57 | flags |= ULPI_OTG_CTRL_ID_PULLUP; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 58 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 59 | /* |
| 60 | * ULPI Specification rev.1.1 default |
| 61 | * for Dp/DmPulldown is enabled. |
| 62 | */ |
| 63 | if (otg->flags & ULPI_OTG_DP_PULLDOWN_DIS) |
| 64 | flags &= ~ULPI_OTG_CTRL_DP_PULLDOWN; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 65 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 66 | if (otg->flags & ULPI_OTG_DM_PULLDOWN_DIS) |
| 67 | flags &= ~ULPI_OTG_CTRL_DM_PULLDOWN; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 68 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 69 | if (otg->flags & ULPI_OTG_EXTVBUSIND) |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame] | 70 | flags |= ULPI_OTG_CTRL_EXTVBUSIND; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 71 | |
Eric Bénard | fa345d0 | 2010-07-15 09:20:19 +0200 | [diff] [blame] | 72 | return otg_io_write(otg, flags, ULPI_OTG_CTRL); |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 73 | } |
| 74 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 75 | static int ulpi_set_fc_flags(struct otg_transceiver *otg) |
| 76 | { |
| 77 | unsigned int flags = 0; |
| 78 | |
| 79 | /* |
| 80 | * ULPI Specification rev.1.1 default |
| 81 | * for XcvrSelect is Full Speed. |
| 82 | */ |
| 83 | if (otg->flags & ULPI_FC_HS) |
| 84 | flags |= ULPI_FUNC_CTRL_HIGH_SPEED; |
| 85 | else if (otg->flags & ULPI_FC_LS) |
| 86 | flags |= ULPI_FUNC_CTRL_LOW_SPEED; |
| 87 | else if (otg->flags & ULPI_FC_FS4LS) |
| 88 | flags |= ULPI_FUNC_CTRL_FS4LS; |
| 89 | else |
| 90 | flags |= ULPI_FUNC_CTRL_FULL_SPEED; |
| 91 | |
| 92 | if (otg->flags & ULPI_FC_TERMSEL) |
| 93 | flags |= ULPI_FUNC_CTRL_TERMSELECT; |
| 94 | |
| 95 | /* |
| 96 | * ULPI Specification rev.1.1 default |
| 97 | * for OpMode is Normal Operation. |
| 98 | */ |
| 99 | if (otg->flags & ULPI_FC_OP_NODRV) |
| 100 | flags |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING; |
| 101 | else if (otg->flags & ULPI_FC_OP_DIS_NRZI) |
| 102 | flags |= ULPI_FUNC_CTRL_OPMODE_DISABLE_NRZI; |
| 103 | else if (otg->flags & ULPI_FC_OP_NSYNC_NEOP) |
| 104 | flags |= ULPI_FUNC_CTRL_OPMODE_NOSYNC_NOEOP; |
| 105 | else |
| 106 | flags |= ULPI_FUNC_CTRL_OPMODE_NORMAL; |
| 107 | |
| 108 | /* |
| 109 | * ULPI Specification rev.1.1 default |
| 110 | * for SuspendM is Powered. |
| 111 | */ |
| 112 | flags |= ULPI_FUNC_CTRL_SUSPENDM; |
| 113 | |
| 114 | return otg_io_write(otg, flags, ULPI_FUNC_CTRL); |
| 115 | } |
| 116 | |
| 117 | static int ulpi_set_ic_flags(struct otg_transceiver *otg) |
| 118 | { |
| 119 | unsigned int flags = 0; |
| 120 | |
| 121 | if (otg->flags & ULPI_IC_AUTORESUME) |
| 122 | flags |= ULPI_IFC_CTRL_AUTORESUME; |
| 123 | |
| 124 | if (otg->flags & ULPI_IC_EXTVBUS_INDINV) |
| 125 | flags |= ULPI_IFC_CTRL_EXTERNAL_VBUS; |
| 126 | |
| 127 | if (otg->flags & ULPI_IC_IND_PASSTHRU) |
| 128 | flags |= ULPI_IFC_CTRL_PASSTHRU; |
| 129 | |
| 130 | if (otg->flags & ULPI_IC_PROTECT_DIS) |
| 131 | flags |= ULPI_IFC_CTRL_PROTECT_IFC_DISABLE; |
| 132 | |
| 133 | return otg_io_write(otg, flags, ULPI_IFC_CTRL); |
| 134 | } |
| 135 | |
| 136 | static int ulpi_set_flags(struct otg_transceiver *otg) |
| 137 | { |
| 138 | int ret; |
| 139 | |
| 140 | ret = ulpi_set_otg_flags(otg); |
| 141 | if (ret) |
| 142 | return ret; |
| 143 | |
| 144 | ret = ulpi_set_ic_flags(otg); |
| 145 | if (ret) |
| 146 | return ret; |
| 147 | |
| 148 | return ulpi_set_fc_flags(otg); |
| 149 | } |
| 150 | |
Igor Grinberg | a913819 | 2010-10-10 17:59:18 +0200 | [diff] [blame] | 151 | static int ulpi_check_integrity(struct otg_transceiver *otg) |
| 152 | { |
| 153 | int ret, i; |
| 154 | unsigned int val = 0x55; |
| 155 | |
| 156 | for (i = 0; i < 2; i++) { |
| 157 | ret = otg_io_write(otg, val, ULPI_SCRATCH); |
| 158 | if (ret < 0) |
| 159 | return ret; |
| 160 | |
| 161 | ret = otg_io_read(otg, ULPI_SCRATCH); |
| 162 | if (ret < 0) |
| 163 | return ret; |
| 164 | |
| 165 | if (ret != val) { |
| 166 | pr_err("ULPI integrity check: failed!"); |
| 167 | return -ENODEV; |
| 168 | } |
| 169 | val = val << 1; |
| 170 | } |
| 171 | |
| 172 | pr_info("ULPI integrity check: passed.\n"); |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 177 | static int ulpi_init(struct otg_transceiver *otg) |
| 178 | { |
Wolfram Sang | 7b4a036 | 2010-06-15 12:34:22 +0200 | [diff] [blame] | 179 | int i, vid, pid, ret; |
| 180 | u32 ulpi_id = 0; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 181 | |
Wolfram Sang | 7b4a036 | 2010-06-15 12:34:22 +0200 | [diff] [blame] | 182 | for (i = 0; i < 4; i++) { |
| 183 | ret = otg_io_read(otg, ULPI_PRODUCT_ID_HIGH - i); |
| 184 | if (ret < 0) |
| 185 | return ret; |
| 186 | ulpi_id = (ulpi_id << 8) | ret; |
| 187 | } |
| 188 | vid = ulpi_id & 0xffff; |
| 189 | pid = ulpi_id >> 16; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 190 | |
| 191 | pr_info("ULPI transceiver vendor/product ID 0x%04x/0x%04x\n", vid, pid); |
| 192 | |
Igor Grinberg | 96b9e83 | 2010-10-10 17:59:19 +0200 | [diff] [blame] | 193 | for (i = 0; i < ARRAY_SIZE(ulpi_ids); i++) { |
| 194 | if (ulpi_ids[i].id == ULPI_ID(vid, pid)) { |
| 195 | pr_info("Found %s ULPI transceiver.\n", |
| 196 | ulpi_ids[i].name); |
Igor Grinberg | a913819 | 2010-10-10 17:59:18 +0200 | [diff] [blame] | 197 | break; |
Igor Grinberg | 96b9e83 | 2010-10-10 17:59:19 +0200 | [diff] [blame] | 198 | } |
| 199 | } |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 200 | |
Igor Grinberg | a913819 | 2010-10-10 17:59:18 +0200 | [diff] [blame] | 201 | ret = ulpi_check_integrity(otg); |
| 202 | if (ret) |
| 203 | return ret; |
| 204 | |
| 205 | return ulpi_set_flags(otg); |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 206 | } |
| 207 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 208 | static int ulpi_set_host(struct otg_transceiver *otg, struct usb_bus *host) |
| 209 | { |
| 210 | unsigned int flags = otg_io_read(otg, ULPI_IFC_CTRL); |
| 211 | |
| 212 | if (!host) { |
| 213 | otg->host = NULL; |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | otg->host = host; |
| 218 | |
| 219 | flags &= ~(ULPI_IFC_CTRL_6_PIN_SERIAL_MODE | |
| 220 | ULPI_IFC_CTRL_3_PIN_SERIAL_MODE | |
| 221 | ULPI_IFC_CTRL_CARKITMODE); |
| 222 | |
| 223 | if (otg->flags & ULPI_IC_6PIN_SERIAL) |
| 224 | flags |= ULPI_IFC_CTRL_6_PIN_SERIAL_MODE; |
| 225 | else if (otg->flags & ULPI_IC_3PIN_SERIAL) |
| 226 | flags |= ULPI_IFC_CTRL_3_PIN_SERIAL_MODE; |
| 227 | else if (otg->flags & ULPI_IC_CARKIT) |
| 228 | flags |= ULPI_IFC_CTRL_CARKITMODE; |
| 229 | |
| 230 | return otg_io_write(otg, flags, ULPI_IFC_CTRL); |
| 231 | } |
| 232 | |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 233 | static int ulpi_set_vbus(struct otg_transceiver *otg, bool on) |
| 234 | { |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame] | 235 | unsigned int flags = otg_io_read(otg, ULPI_OTG_CTRL); |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 236 | |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame] | 237 | flags &= ~(ULPI_OTG_CTRL_DRVVBUS | ULPI_OTG_CTRL_DRVVBUS_EXT); |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 238 | |
| 239 | if (on) { |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 240 | if (otg->flags & ULPI_OTG_DRVVBUS) |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame] | 241 | flags |= ULPI_OTG_CTRL_DRVVBUS; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 242 | |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 243 | if (otg->flags & ULPI_OTG_DRVVBUS_EXT) |
Heikki Krogerus | fc567f0 | 2010-05-03 09:13:02 +0300 | [diff] [blame] | 244 | flags |= ULPI_OTG_CTRL_DRVVBUS_EXT; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 245 | } |
| 246 | |
Eric Bénard | fa345d0 | 2010-07-15 09:20:19 +0200 | [diff] [blame] | 247 | return otg_io_write(otg, flags, ULPI_OTG_CTRL); |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | struct otg_transceiver * |
| 251 | otg_ulpi_create(struct otg_io_access_ops *ops, |
| 252 | unsigned int flags) |
| 253 | { |
| 254 | struct otg_transceiver *otg; |
| 255 | |
| 256 | otg = kzalloc(sizeof(*otg), GFP_KERNEL); |
| 257 | if (!otg) |
| 258 | return NULL; |
| 259 | |
| 260 | otg->label = "ULPI"; |
| 261 | otg->flags = flags; |
| 262 | otg->io_ops = ops; |
| 263 | otg->init = ulpi_init; |
Igor Grinberg | 13dd0c9 | 2010-07-15 16:00:16 +0300 | [diff] [blame] | 264 | otg->set_host = ulpi_set_host; |
Daniel Mack | 2d57a95 | 2009-10-15 17:09:35 +0300 | [diff] [blame] | 265 | otg->set_vbus = ulpi_set_vbus; |
| 266 | |
| 267 | return otg; |
| 268 | } |
| 269 | EXPORT_SYMBOL_GPL(otg_ulpi_create); |
| 270 | |