blob: 0b0466728fdc38e63a29a5c5375b712c6347df54 [file] [log] [blame]
Daniel Mack2d57a952009-10-15 17:09:35 +03001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040028#include <linux/export.h>
Daniel Mack2d57a952009-10-15 17:09:35 +030029#include <linux/usb.h>
30#include <linux/usb/otg.h>
31#include <linux/usb/ulpi.h>
32
Igor Grinberg96b9e832010-10-10 17:59:19 +020033
34struct ulpi_info {
35 unsigned int id;
36 char *name;
37};
38
Daniel Mack2d57a952009-10-15 17:09:35 +030039#define ULPI_ID(vendor, product) (((vendor) << 16) | (product))
Igor Grinberg96b9e832010-10-10 17:59:19 +020040#define ULPI_INFO(_id, _name) \
41 { \
42 .id = (_id), \
43 .name = (_name), \
44 }
Daniel Mack2d57a952009-10-15 17:09:35 +030045
Daniel Mack2d57a952009-10-15 17:09:35 +030046/* ULPI hardcoded IDs, used for probing */
Igor Grinberg96b9e832010-10-10 17:59:19 +020047static struct ulpi_info ulpi_ids[] = {
48 ULPI_INFO(ULPI_ID(0x04cc, 0x1504), "NXP ISP1504"),
Fabio Estevam1e4cba82010-12-20 21:54:30 -020049 ULPI_INFO(ULPI_ID(0x0424, 0x0006), "SMSC USB331x"),
Daniel Mack2d57a952009-10-15 17:09:35 +030050};
51
Igor Grinberg13dd0c92010-07-15 16:00:16 +030052static int ulpi_set_otg_flags(struct otg_transceiver *otg)
Daniel Mack2d57a952009-10-15 17:09:35 +030053{
Igor Grinberg13dd0c92010-07-15 16:00:16 +030054 unsigned int flags = ULPI_OTG_CTRL_DP_PULLDOWN |
55 ULPI_OTG_CTRL_DM_PULLDOWN;
Daniel Mack2d57a952009-10-15 17:09:35 +030056
Igor Grinberg13dd0c92010-07-15 16:00:16 +030057 if (otg->flags & ULPI_OTG_ID_PULLUP)
Heikki Krogerusfc567f02010-05-03 09:13:02 +030058 flags |= ULPI_OTG_CTRL_ID_PULLUP;
Daniel Mack2d57a952009-10-15 17:09:35 +030059
Igor Grinberg13dd0c92010-07-15 16:00:16 +030060 /*
61 * ULPI Specification rev.1.1 default
62 * for Dp/DmPulldown is enabled.
63 */
64 if (otg->flags & ULPI_OTG_DP_PULLDOWN_DIS)
65 flags &= ~ULPI_OTG_CTRL_DP_PULLDOWN;
Daniel Mack2d57a952009-10-15 17:09:35 +030066
Igor Grinberg13dd0c92010-07-15 16:00:16 +030067 if (otg->flags & ULPI_OTG_DM_PULLDOWN_DIS)
68 flags &= ~ULPI_OTG_CTRL_DM_PULLDOWN;
Daniel Mack2d57a952009-10-15 17:09:35 +030069
Igor Grinberg13dd0c92010-07-15 16:00:16 +030070 if (otg->flags & ULPI_OTG_EXTVBUSIND)
Heikki Krogerusfc567f02010-05-03 09:13:02 +030071 flags |= ULPI_OTG_CTRL_EXTVBUSIND;
Daniel Mack2d57a952009-10-15 17:09:35 +030072
Eric Bénardfa345d02010-07-15 09:20:19 +020073 return otg_io_write(otg, flags, ULPI_OTG_CTRL);
Daniel Mack2d57a952009-10-15 17:09:35 +030074}
75
Igor Grinberg13dd0c92010-07-15 16:00:16 +030076static int ulpi_set_fc_flags(struct otg_transceiver *otg)
77{
78 unsigned int flags = 0;
79
80 /*
81 * ULPI Specification rev.1.1 default
82 * for XcvrSelect is Full Speed.
83 */
84 if (otg->flags & ULPI_FC_HS)
85 flags |= ULPI_FUNC_CTRL_HIGH_SPEED;
86 else if (otg->flags & ULPI_FC_LS)
87 flags |= ULPI_FUNC_CTRL_LOW_SPEED;
88 else if (otg->flags & ULPI_FC_FS4LS)
89 flags |= ULPI_FUNC_CTRL_FS4LS;
90 else
91 flags |= ULPI_FUNC_CTRL_FULL_SPEED;
92
93 if (otg->flags & ULPI_FC_TERMSEL)
94 flags |= ULPI_FUNC_CTRL_TERMSELECT;
95
96 /*
97 * ULPI Specification rev.1.1 default
98 * for OpMode is Normal Operation.
99 */
100 if (otg->flags & ULPI_FC_OP_NODRV)
101 flags |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
102 else if (otg->flags & ULPI_FC_OP_DIS_NRZI)
103 flags |= ULPI_FUNC_CTRL_OPMODE_DISABLE_NRZI;
104 else if (otg->flags & ULPI_FC_OP_NSYNC_NEOP)
105 flags |= ULPI_FUNC_CTRL_OPMODE_NOSYNC_NOEOP;
106 else
107 flags |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
108
109 /*
110 * ULPI Specification rev.1.1 default
111 * for SuspendM is Powered.
112 */
113 flags |= ULPI_FUNC_CTRL_SUSPENDM;
114
115 return otg_io_write(otg, flags, ULPI_FUNC_CTRL);
116}
117
118static int ulpi_set_ic_flags(struct otg_transceiver *otg)
119{
120 unsigned int flags = 0;
121
122 if (otg->flags & ULPI_IC_AUTORESUME)
123 flags |= ULPI_IFC_CTRL_AUTORESUME;
124
125 if (otg->flags & ULPI_IC_EXTVBUS_INDINV)
126 flags |= ULPI_IFC_CTRL_EXTERNAL_VBUS;
127
128 if (otg->flags & ULPI_IC_IND_PASSTHRU)
129 flags |= ULPI_IFC_CTRL_PASSTHRU;
130
131 if (otg->flags & ULPI_IC_PROTECT_DIS)
132 flags |= ULPI_IFC_CTRL_PROTECT_IFC_DISABLE;
133
134 return otg_io_write(otg, flags, ULPI_IFC_CTRL);
135}
136
137static int ulpi_set_flags(struct otg_transceiver *otg)
138{
139 int ret;
140
141 ret = ulpi_set_otg_flags(otg);
142 if (ret)
143 return ret;
144
145 ret = ulpi_set_ic_flags(otg);
146 if (ret)
147 return ret;
148
149 return ulpi_set_fc_flags(otg);
150}
151
Igor Grinberga9138192010-10-10 17:59:18 +0200152static int ulpi_check_integrity(struct otg_transceiver *otg)
153{
154 int ret, i;
155 unsigned int val = 0x55;
156
157 for (i = 0; i < 2; i++) {
158 ret = otg_io_write(otg, val, ULPI_SCRATCH);
159 if (ret < 0)
160 return ret;
161
162 ret = otg_io_read(otg, ULPI_SCRATCH);
163 if (ret < 0)
164 return ret;
165
166 if (ret != val) {
167 pr_err("ULPI integrity check: failed!");
168 return -ENODEV;
169 }
170 val = val << 1;
171 }
172
173 pr_info("ULPI integrity check: passed.\n");
174
175 return 0;
176}
177
Daniel Mack2d57a952009-10-15 17:09:35 +0300178static int ulpi_init(struct otg_transceiver *otg)
179{
Wolfram Sang7b4a0362010-06-15 12:34:22 +0200180 int i, vid, pid, ret;
181 u32 ulpi_id = 0;
Daniel Mack2d57a952009-10-15 17:09:35 +0300182
Wolfram Sang7b4a0362010-06-15 12:34:22 +0200183 for (i = 0; i < 4; i++) {
184 ret = otg_io_read(otg, ULPI_PRODUCT_ID_HIGH - i);
185 if (ret < 0)
186 return ret;
187 ulpi_id = (ulpi_id << 8) | ret;
188 }
189 vid = ulpi_id & 0xffff;
190 pid = ulpi_id >> 16;
Daniel Mack2d57a952009-10-15 17:09:35 +0300191
192 pr_info("ULPI transceiver vendor/product ID 0x%04x/0x%04x\n", vid, pid);
193
Igor Grinberg96b9e832010-10-10 17:59:19 +0200194 for (i = 0; i < ARRAY_SIZE(ulpi_ids); i++) {
195 if (ulpi_ids[i].id == ULPI_ID(vid, pid)) {
196 pr_info("Found %s ULPI transceiver.\n",
197 ulpi_ids[i].name);
Igor Grinberga9138192010-10-10 17:59:18 +0200198 break;
Igor Grinberg96b9e832010-10-10 17:59:19 +0200199 }
200 }
Daniel Mack2d57a952009-10-15 17:09:35 +0300201
Igor Grinberga9138192010-10-10 17:59:18 +0200202 ret = ulpi_check_integrity(otg);
203 if (ret)
204 return ret;
205
206 return ulpi_set_flags(otg);
Daniel Mack2d57a952009-10-15 17:09:35 +0300207}
208
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300209static int ulpi_set_host(struct otg_transceiver *otg, struct usb_bus *host)
210{
211 unsigned int flags = otg_io_read(otg, ULPI_IFC_CTRL);
212
213 if (!host) {
214 otg->host = NULL;
215 return 0;
216 }
217
218 otg->host = host;
219
220 flags &= ~(ULPI_IFC_CTRL_6_PIN_SERIAL_MODE |
221 ULPI_IFC_CTRL_3_PIN_SERIAL_MODE |
222 ULPI_IFC_CTRL_CARKITMODE);
223
224 if (otg->flags & ULPI_IC_6PIN_SERIAL)
225 flags |= ULPI_IFC_CTRL_6_PIN_SERIAL_MODE;
226 else if (otg->flags & ULPI_IC_3PIN_SERIAL)
227 flags |= ULPI_IFC_CTRL_3_PIN_SERIAL_MODE;
228 else if (otg->flags & ULPI_IC_CARKIT)
229 flags |= ULPI_IFC_CTRL_CARKITMODE;
230
231 return otg_io_write(otg, flags, ULPI_IFC_CTRL);
232}
233
Daniel Mack2d57a952009-10-15 17:09:35 +0300234static int ulpi_set_vbus(struct otg_transceiver *otg, bool on)
235{
Heikki Krogerusfc567f02010-05-03 09:13:02 +0300236 unsigned int flags = otg_io_read(otg, ULPI_OTG_CTRL);
Daniel Mack2d57a952009-10-15 17:09:35 +0300237
Heikki Krogerusfc567f02010-05-03 09:13:02 +0300238 flags &= ~(ULPI_OTG_CTRL_DRVVBUS | ULPI_OTG_CTRL_DRVVBUS_EXT);
Daniel Mack2d57a952009-10-15 17:09:35 +0300239
240 if (on) {
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300241 if (otg->flags & ULPI_OTG_DRVVBUS)
Heikki Krogerusfc567f02010-05-03 09:13:02 +0300242 flags |= ULPI_OTG_CTRL_DRVVBUS;
Daniel Mack2d57a952009-10-15 17:09:35 +0300243
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300244 if (otg->flags & ULPI_OTG_DRVVBUS_EXT)
Heikki Krogerusfc567f02010-05-03 09:13:02 +0300245 flags |= ULPI_OTG_CTRL_DRVVBUS_EXT;
Daniel Mack2d57a952009-10-15 17:09:35 +0300246 }
247
Eric Bénardfa345d02010-07-15 09:20:19 +0200248 return otg_io_write(otg, flags, ULPI_OTG_CTRL);
Daniel Mack2d57a952009-10-15 17:09:35 +0300249}
250
251struct otg_transceiver *
252otg_ulpi_create(struct otg_io_access_ops *ops,
253 unsigned int flags)
254{
255 struct otg_transceiver *otg;
256
257 otg = kzalloc(sizeof(*otg), GFP_KERNEL);
258 if (!otg)
259 return NULL;
260
261 otg->label = "ULPI";
262 otg->flags = flags;
263 otg->io_ops = ops;
264 otg->init = ulpi_init;
Igor Grinberg13dd0c92010-07-15 16:00:16 +0300265 otg->set_host = ulpi_set_host;
Daniel Mack2d57a952009-10-15 17:09:35 +0300266 otg->set_vbus = ulpi_set_vbus;
267
268 return otg;
269}
270EXPORT_SYMBOL_GPL(otg_ulpi_create);
271