blob: a43c49369a60213bd5ad8848ba388b1dcaa5280e [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Daniel Mack2d57a952009-10-15 17:09:35 +03002/*
3 * Generic ULPI USB transceiver support
4 *
5 * Copyright (C) 2009 Daniel Mack <daniel@caiaq.de>
6 *
7 * Based on sources from
8 *
9 * Sascha Hauer <s.hauer@pengutronix.de>
10 * Freescale Semiconductors
Daniel Mack2d57a952009-10-15 17:09:35 +030011 */
12
13#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040015#include <linux/export.h>
Daniel Mack2d57a952009-10-15 17:09:35 +030016#include <linux/usb.h>
17#include <linux/usb/otg.h>
18#include <linux/usb/ulpi.h>
19
Igor Grinberg96b9e832010-10-10 17:59:19 +020020
21struct ulpi_info {
22 unsigned int id;
23 char *name;
24};
25
Daniel Mack2d57a952009-10-15 17:09:35 +030026#define ULPI_ID(vendor, product) (((vendor) << 16) | (product))
Igor Grinberg96b9e832010-10-10 17:59:19 +020027#define ULPI_INFO(_id, _name) \
28 { \
29 .id = (_id), \
30 .name = (_name), \
31 }
Daniel Mack2d57a952009-10-15 17:09:35 +030032
Daniel Mack2d57a952009-10-15 17:09:35 +030033/* ULPI hardcoded IDs, used for probing */
Igor Grinberg96b9e832010-10-10 17:59:19 +020034static struct ulpi_info ulpi_ids[] = {
35 ULPI_INFO(ULPI_ID(0x04cc, 0x1504), "NXP ISP1504"),
Fabio Estevam1e4cba82010-12-20 21:54:30 -020036 ULPI_INFO(ULPI_ID(0x0424, 0x0006), "SMSC USB331x"),
Michal Simekead51782014-03-11 13:23:14 +010037 ULPI_INFO(ULPI_ID(0x0424, 0x0007), "SMSC USB3320"),
Liviu Dudau79cb5b52014-05-14 21:32:39 +010038 ULPI_INFO(ULPI_ID(0x0424, 0x0009), "SMSC USB334x"),
Michal Simekead51782014-03-11 13:23:14 +010039 ULPI_INFO(ULPI_ID(0x0451, 0x1507), "TI TUSB1210"),
Daniel Mack2d57a952009-10-15 17:09:35 +030040};
41
Heikki Krogerus298b0832012-02-13 13:24:13 +020042static int ulpi_set_otg_flags(struct usb_phy *phy)
Daniel Mack2d57a952009-10-15 17:09:35 +030043{
Igor Grinberg13dd0c92010-07-15 16:00:16 +030044 unsigned int flags = ULPI_OTG_CTRL_DP_PULLDOWN |
45 ULPI_OTG_CTRL_DM_PULLDOWN;
Daniel Mack2d57a952009-10-15 17:09:35 +030046
Heikki Krogerus298b0832012-02-13 13:24:13 +020047 if (phy->flags & ULPI_OTG_ID_PULLUP)
Heikki Krogerusfc567f02010-05-03 09:13:02 +030048 flags |= ULPI_OTG_CTRL_ID_PULLUP;
Daniel Mack2d57a952009-10-15 17:09:35 +030049
Igor Grinberg13dd0c92010-07-15 16:00:16 +030050 /*
51 * ULPI Specification rev.1.1 default
52 * for Dp/DmPulldown is enabled.
53 */
Heikki Krogerus298b0832012-02-13 13:24:13 +020054 if (phy->flags & ULPI_OTG_DP_PULLDOWN_DIS)
Igor Grinberg13dd0c92010-07-15 16:00:16 +030055 flags &= ~ULPI_OTG_CTRL_DP_PULLDOWN;
Daniel Mack2d57a952009-10-15 17:09:35 +030056
Heikki Krogerus298b0832012-02-13 13:24:13 +020057 if (phy->flags & ULPI_OTG_DM_PULLDOWN_DIS)
Igor Grinberg13dd0c92010-07-15 16:00:16 +030058 flags &= ~ULPI_OTG_CTRL_DM_PULLDOWN;
Daniel Mack2d57a952009-10-15 17:09:35 +030059
Heikki Krogerus298b0832012-02-13 13:24:13 +020060 if (phy->flags & ULPI_OTG_EXTVBUSIND)
Heikki Krogerusfc567f02010-05-03 09:13:02 +030061 flags |= ULPI_OTG_CTRL_EXTVBUSIND;
Daniel Mack2d57a952009-10-15 17:09:35 +030062
Heikki Krogerus298b0832012-02-13 13:24:13 +020063 return usb_phy_io_write(phy, flags, ULPI_OTG_CTRL);
Daniel Mack2d57a952009-10-15 17:09:35 +030064}
65
Heikki Krogerus298b0832012-02-13 13:24:13 +020066static int ulpi_set_fc_flags(struct usb_phy *phy)
Igor Grinberg13dd0c92010-07-15 16:00:16 +030067{
68 unsigned int flags = 0;
69
70 /*
71 * ULPI Specification rev.1.1 default
72 * for XcvrSelect is Full Speed.
73 */
Heikki Krogerus298b0832012-02-13 13:24:13 +020074 if (phy->flags & ULPI_FC_HS)
Igor Grinberg13dd0c92010-07-15 16:00:16 +030075 flags |= ULPI_FUNC_CTRL_HIGH_SPEED;
Heikki Krogerus298b0832012-02-13 13:24:13 +020076 else if (phy->flags & ULPI_FC_LS)
Igor Grinberg13dd0c92010-07-15 16:00:16 +030077 flags |= ULPI_FUNC_CTRL_LOW_SPEED;
Heikki Krogerus298b0832012-02-13 13:24:13 +020078 else if (phy->flags & ULPI_FC_FS4LS)
Igor Grinberg13dd0c92010-07-15 16:00:16 +030079 flags |= ULPI_FUNC_CTRL_FS4LS;
80 else
81 flags |= ULPI_FUNC_CTRL_FULL_SPEED;
82
Heikki Krogerus298b0832012-02-13 13:24:13 +020083 if (phy->flags & ULPI_FC_TERMSEL)
Igor Grinberg13dd0c92010-07-15 16:00:16 +030084 flags |= ULPI_FUNC_CTRL_TERMSELECT;
85
86 /*
87 * ULPI Specification rev.1.1 default
88 * for OpMode is Normal Operation.
89 */
Heikki Krogerus298b0832012-02-13 13:24:13 +020090 if (phy->flags & ULPI_FC_OP_NODRV)
Igor Grinberg13dd0c92010-07-15 16:00:16 +030091 flags |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus298b0832012-02-13 13:24:13 +020092 else if (phy->flags & ULPI_FC_OP_DIS_NRZI)
Igor Grinberg13dd0c92010-07-15 16:00:16 +030093 flags |= ULPI_FUNC_CTRL_OPMODE_DISABLE_NRZI;
Heikki Krogerus298b0832012-02-13 13:24:13 +020094 else if (phy->flags & ULPI_FC_OP_NSYNC_NEOP)
Igor Grinberg13dd0c92010-07-15 16:00:16 +030095 flags |= ULPI_FUNC_CTRL_OPMODE_NOSYNC_NOEOP;
96 else
97 flags |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
98
99 /*
100 * ULPI Specification rev.1.1 default
101 * for SuspendM is Powered.
102 */
103 flags |= ULPI_FUNC_CTRL_SUSPENDM;
104
Heikki Krogerus298b0832012-02-13 13:24:13 +0200105 return usb_phy_io_write(phy, flags, ULPI_FUNC_CTRL);
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300106}
107
Heikki Krogerus298b0832012-02-13 13:24:13 +0200108static int ulpi_set_ic_flags(struct usb_phy *phy)
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300109{
110 unsigned int flags = 0;
111
Heikki Krogerus298b0832012-02-13 13:24:13 +0200112 if (phy->flags & ULPI_IC_AUTORESUME)
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300113 flags |= ULPI_IFC_CTRL_AUTORESUME;
114
Heikki Krogerus298b0832012-02-13 13:24:13 +0200115 if (phy->flags & ULPI_IC_EXTVBUS_INDINV)
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300116 flags |= ULPI_IFC_CTRL_EXTERNAL_VBUS;
117
Heikki Krogerus298b0832012-02-13 13:24:13 +0200118 if (phy->flags & ULPI_IC_IND_PASSTHRU)
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300119 flags |= ULPI_IFC_CTRL_PASSTHRU;
120
Heikki Krogerus298b0832012-02-13 13:24:13 +0200121 if (phy->flags & ULPI_IC_PROTECT_DIS)
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300122 flags |= ULPI_IFC_CTRL_PROTECT_IFC_DISABLE;
123
Heikki Krogerus298b0832012-02-13 13:24:13 +0200124 return usb_phy_io_write(phy, flags, ULPI_IFC_CTRL);
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300125}
126
Heikki Krogerus298b0832012-02-13 13:24:13 +0200127static int ulpi_set_flags(struct usb_phy *phy)
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300128{
129 int ret;
130
Heikki Krogerus298b0832012-02-13 13:24:13 +0200131 ret = ulpi_set_otg_flags(phy);
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300132 if (ret)
133 return ret;
134
Heikki Krogerus298b0832012-02-13 13:24:13 +0200135 ret = ulpi_set_ic_flags(phy);
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300136 if (ret)
137 return ret;
138
Heikki Krogerus298b0832012-02-13 13:24:13 +0200139 return ulpi_set_fc_flags(phy);
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300140}
141
Heikki Krogerus298b0832012-02-13 13:24:13 +0200142static int ulpi_check_integrity(struct usb_phy *phy)
Igor Grinberga9138192010-10-10 17:59:18 +0200143{
144 int ret, i;
145 unsigned int val = 0x55;
146
147 for (i = 0; i < 2; i++) {
Heikki Krogerus298b0832012-02-13 13:24:13 +0200148 ret = usb_phy_io_write(phy, val, ULPI_SCRATCH);
Igor Grinberga9138192010-10-10 17:59:18 +0200149 if (ret < 0)
150 return ret;
151
Heikki Krogerus298b0832012-02-13 13:24:13 +0200152 ret = usb_phy_io_read(phy, ULPI_SCRATCH);
Igor Grinberga9138192010-10-10 17:59:18 +0200153 if (ret < 0)
154 return ret;
155
156 if (ret != val) {
157 pr_err("ULPI integrity check: failed!");
158 return -ENODEV;
159 }
160 val = val << 1;
161 }
162
163 pr_info("ULPI integrity check: passed.\n");
164
165 return 0;
166}
167
Heikki Krogerus298b0832012-02-13 13:24:13 +0200168static int ulpi_init(struct usb_phy *phy)
Daniel Mack2d57a952009-10-15 17:09:35 +0300169{
Wolfram Sang7b4a0362010-06-15 12:34:22 +0200170 int i, vid, pid, ret;
171 u32 ulpi_id = 0;
Daniel Mack2d57a952009-10-15 17:09:35 +0300172
Wolfram Sang7b4a0362010-06-15 12:34:22 +0200173 for (i = 0; i < 4; i++) {
Heikki Krogerus298b0832012-02-13 13:24:13 +0200174 ret = usb_phy_io_read(phy, ULPI_PRODUCT_ID_HIGH - i);
Wolfram Sang7b4a0362010-06-15 12:34:22 +0200175 if (ret < 0)
176 return ret;
177 ulpi_id = (ulpi_id << 8) | ret;
178 }
179 vid = ulpi_id & 0xffff;
180 pid = ulpi_id >> 16;
Daniel Mack2d57a952009-10-15 17:09:35 +0300181
182 pr_info("ULPI transceiver vendor/product ID 0x%04x/0x%04x\n", vid, pid);
183
Igor Grinberg96b9e832010-10-10 17:59:19 +0200184 for (i = 0; i < ARRAY_SIZE(ulpi_ids); i++) {
185 if (ulpi_ids[i].id == ULPI_ID(vid, pid)) {
186 pr_info("Found %s ULPI transceiver.\n",
187 ulpi_ids[i].name);
Igor Grinberga9138192010-10-10 17:59:18 +0200188 break;
Igor Grinberg96b9e832010-10-10 17:59:19 +0200189 }
190 }
Daniel Mack2d57a952009-10-15 17:09:35 +0300191
Heikki Krogerus298b0832012-02-13 13:24:13 +0200192 ret = ulpi_check_integrity(phy);
Igor Grinberga9138192010-10-10 17:59:18 +0200193 if (ret)
194 return ret;
195
Heikki Krogerus298b0832012-02-13 13:24:13 +0200196 return ulpi_set_flags(phy);
Daniel Mack2d57a952009-10-15 17:09:35 +0300197}
198
Heikki Krogerus298b0832012-02-13 13:24:13 +0200199static int ulpi_set_host(struct usb_otg *otg, struct usb_bus *host)
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300200{
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100201 struct usb_phy *phy = otg->usb_phy;
Heikki Krogerus298b0832012-02-13 13:24:13 +0200202 unsigned int flags = usb_phy_io_read(phy, ULPI_IFC_CTRL);
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300203
204 if (!host) {
205 otg->host = NULL;
206 return 0;
207 }
208
209 otg->host = host;
210
211 flags &= ~(ULPI_IFC_CTRL_6_PIN_SERIAL_MODE |
212 ULPI_IFC_CTRL_3_PIN_SERIAL_MODE |
213 ULPI_IFC_CTRL_CARKITMODE);
214
Heikki Krogerus298b0832012-02-13 13:24:13 +0200215 if (phy->flags & ULPI_IC_6PIN_SERIAL)
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300216 flags |= ULPI_IFC_CTRL_6_PIN_SERIAL_MODE;
Heikki Krogerus298b0832012-02-13 13:24:13 +0200217 else if (phy->flags & ULPI_IC_3PIN_SERIAL)
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300218 flags |= ULPI_IFC_CTRL_3_PIN_SERIAL_MODE;
Heikki Krogerus298b0832012-02-13 13:24:13 +0200219 else if (phy->flags & ULPI_IC_CARKIT)
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300220 flags |= ULPI_IFC_CTRL_CARKITMODE;
221
Heikki Krogerus298b0832012-02-13 13:24:13 +0200222 return usb_phy_io_write(phy, flags, ULPI_IFC_CTRL);
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300223}
224
Heikki Krogerus298b0832012-02-13 13:24:13 +0200225static int ulpi_set_vbus(struct usb_otg *otg, bool on)
Daniel Mack2d57a952009-10-15 17:09:35 +0300226{
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100227 struct usb_phy *phy = otg->usb_phy;
Heikki Krogerus298b0832012-02-13 13:24:13 +0200228 unsigned int flags = usb_phy_io_read(phy, ULPI_OTG_CTRL);
Daniel Mack2d57a952009-10-15 17:09:35 +0300229
Heikki Krogerusfc567f02010-05-03 09:13:02 +0300230 flags &= ~(ULPI_OTG_CTRL_DRVVBUS | ULPI_OTG_CTRL_DRVVBUS_EXT);
Daniel Mack2d57a952009-10-15 17:09:35 +0300231
232 if (on) {
Heikki Krogerus298b0832012-02-13 13:24:13 +0200233 if (phy->flags & ULPI_OTG_DRVVBUS)
Heikki Krogerusfc567f02010-05-03 09:13:02 +0300234 flags |= ULPI_OTG_CTRL_DRVVBUS;
Daniel Mack2d57a952009-10-15 17:09:35 +0300235
Heikki Krogerus298b0832012-02-13 13:24:13 +0200236 if (phy->flags & ULPI_OTG_DRVVBUS_EXT)
Heikki Krogerusfc567f02010-05-03 09:13:02 +0300237 flags |= ULPI_OTG_CTRL_DRVVBUS_EXT;
Daniel Mack2d57a952009-10-15 17:09:35 +0300238 }
239
Heikki Krogerus298b0832012-02-13 13:24:13 +0200240 return usb_phy_io_write(phy, flags, ULPI_OTG_CTRL);
Daniel Mack2d57a952009-10-15 17:09:35 +0300241}
242
Heikki Krogerus86753812012-02-13 13:24:02 +0200243struct usb_phy *
Heikki Krogerus298b0832012-02-13 13:24:13 +0200244otg_ulpi_create(struct usb_phy_io_ops *ops,
Daniel Mack2d57a952009-10-15 17:09:35 +0300245 unsigned int flags)
246{
Heikki Krogerus298b0832012-02-13 13:24:13 +0200247 struct usb_phy *phy;
248 struct usb_otg *otg;
Daniel Mack2d57a952009-10-15 17:09:35 +0300249
Heikki Krogerus298b0832012-02-13 13:24:13 +0200250 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
251 if (!phy)
Daniel Mack2d57a952009-10-15 17:09:35 +0300252 return NULL;
253
Heikki Krogerus298b0832012-02-13 13:24:13 +0200254 otg = kzalloc(sizeof(*otg), GFP_KERNEL);
255 if (!otg) {
256 kfree(phy);
257 return NULL;
258 }
259
260 phy->label = "ULPI";
261 phy->flags = flags;
262 phy->io_ops = ops;
263 phy->otg = otg;
264 phy->init = ulpi_init;
265
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100266 otg->usb_phy = phy;
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300267 otg->set_host = ulpi_set_host;
Daniel Mack2d57a952009-10-15 17:09:35 +0300268 otg->set_vbus = ulpi_set_vbus;
269
Heikki Krogerus298b0832012-02-13 13:24:13 +0200270 return phy;
Daniel Mack2d57a952009-10-15 17:09:35 +0300271}
272EXPORT_SYMBOL_GPL(otg_ulpi_create);
273