1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
/*
* MUSB OTG driver virtual root hub support
*
* Copyright 2005 Mentor Graphics Corporation
* Copyright (C) 2005-2006 by Texas Instruments
* Copyright (C) 2006-2007 Nokia Corporation
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/time.h>
#include <linux/timer.h>
#include <asm/unaligned.h>
#include "musbfsh_core.h"
#include "musbfsh_host.h"
#include "usb.h"
#ifdef MTK_ICUSB_SUPPORT
extern struct my_attr skip_mac_init_attr;
struct my_attr resistor_control_attr = {
.attr.name = "resistor_control",
.attr.mode = 0644,
#ifdef MTK_ICUSB_RESISTOR_CONTROL
.value = 1
#else
.value = 0
#endif
};
struct my_attr skip_port_pm_attr = {
.attr.name = "skip_port_pm",
.attr.mode = 0644,
#ifdef MTK_ICUSB_SKIP_PORT_PM
.value = 1
#else
.value = 0
#endif
};
#endif
#ifdef MTK_USB_RUNTIME_SUPPORT
#include <cust_eint.h>
extern void mt_eint_unmask(unsigned int line);
extern void mt_eint_mask(unsigned int line);
extern void request_wakeup_md_timeout(unsigned int dev_id, unsigned int dev_sub_id);
#endif
static void musbfsh_port_suspend(struct musbfsh *musbfsh, bool do_suspend)
{
u8 power;
u8 intrusbe;
u8 intrusb;
void __iomem *mbase = musbfsh->mregs;
int retries = 0;
/* NOTE: this doesn't necessarily put PHY into low power mode,
* turning off its clock; that's a function of PHY integration and
* MUSBFSH_POWER_ENSUSPEND. PHY may need a clock (sigh) to detect
* SE0 changing to connect (J) or wakeup (K) states.
*/
if (do_suspend) {
/* clean MUSBFSH_INTR_SOF in MUSBFSH_INTRUSBE */
intrusbe = musbfsh_readb(mbase, MUSBFSH_INTRUSBE);
intrusbe &= ~MUSBFSH_INTR_SOF;
musbfsh_writeb(mbase, MUSBFSH_INTRUSBE, intrusbe);
mb();
/* clean MUSBFSH_INTR_SOF in MUSBFSH_INTRUSB */
intrusb = musbfsh_readb(mbase, MUSBFSH_INTRUSB);
intrusb |= MUSBFSH_INTR_SOF;
musbfsh_writeb(mbase, MUSBFSH_INTRUSB, intrusb);
mb();
retries = 10000;
intrusb = musbfsh_readb(mbase, MUSBFSH_INTRUSB);
while (!(intrusb & MUSBFSH_INTR_SOF)) {
intrusb = musbfsh_readb(mbase, MUSBFSH_INTRUSB);
if (retries-- < 1)
break;
}
/* delay 10 us */
udelay(10);
/* set MUSBFSH_POWER_SUSPENDM in MUSBFSH_POWER_SUSPENDM */
power = musbfsh_readb(mbase, MUSBFSH_POWER);
#ifdef MTK_USB_RUNTIME_SUPPORT
/* mask remote wake up IRQ between port suspend and bus suspend.
hub.c will call set_port_feature first then usb_set_device_state, so if EINT comes between them, resume flow may see
device state without USB_STATE_SUSPENDED and do nothing.
So we postpone remote wake up IRQ untill the suspend flow is all done (when bus_suspend is called). Since suspend flow
may be interrupted (root hub is suspended, but not host controller), so we also unmaks EINT when resume is done.
*/
mt_eint_mask(CUST_EINT_MT6280_USB_WAKEUP_NUM);
#endif
retries = 10000;
#ifdef MTK_ICUSB_SUPPORT
if(skip_port_pm_attr.value)
{
MYDBG("skip hw operation for port suspend\n");
}
else
{
power &= ~MUSBFSH_POWER_RESUME;
power |= MUSBFSH_POWER_SUSPENDM;
musbfsh_writeb(mbase, MUSBFSH_POWER, power);
/* Needed for OPT A tests */
power = musbfsh_readb(mbase, MUSBFSH_POWER);
while (power & MUSBFSH_POWER_SUSPENDM) {
power = musbfsh_readb(mbase, MUSBFSH_POWER);
if (retries-- < 1)
break;
}
}
#else
power &= ~MUSBFSH_POWER_RESUME;
power |= MUSBFSH_POWER_SUSPENDM;
musbfsh_writeb(mbase, MUSBFSH_POWER, power);
/* Needed for OPT A tests */
power = musbfsh_readb(mbase, MUSBFSH_POWER);
while (power & MUSBFSH_POWER_SUSPENDM) {
power = musbfsh_readb(mbase, MUSBFSH_POWER);
if (retries-- < 1)
break;
}
#endif
mb();
WARNING( "Root port suspended, power 0x%02x\n", power);
musbfsh->port1_status |= USB_PORT_STAT_SUSPEND;
} else {
power = musbfsh_readb(mbase, MUSBFSH_POWER);
if(!(power & MUSBFSH_POWER_SUSPENDM)) {
WARNING("Root port resuming abort, power 0x%02x\n", power);
if(power & MUSBFSH_POWER_RESUME)
goto finish;
else
return;
}
#ifdef MTK_USB_RUNTIME_SUPPORT
//ERR("EINT to wake up MD for resume\n");
//request_wakeup_md_timeout(0, 0); //wx, wakeup MD first
#endif
#ifdef MTK_ICUSB_SUPPORT
if(skip_port_pm_attr.value)
{
MYDBG("skip hw operation for port resume\n");
}
else
{
power &= ~MUSBFSH_POWER_SUSPENDM;
power |= MUSBFSH_POWER_RESUME;
musbfsh_writeb(mbase, MUSBFSH_POWER, power);
}
#else
power &= ~MUSBFSH_POWER_SUSPENDM;
power |= MUSBFSH_POWER_RESUME;
musbfsh_writeb(mbase, MUSBFSH_POWER, power);
#endif
mb();
WARNING("Root port resuming, power 0x%02x\n", power);
finish:
/* later, GetPortStatus will stop RESUME signaling */
musbfsh->port1_status |= MUSBFSH_PORT_STAT_RESUME;
musbfsh->rh_timer = jiffies + msecs_to_jiffies(20);
}
}
static void musbfsh_port_reset(struct musbfsh *musbfsh, bool do_reset)
{
u8 power;
void __iomem *mbase = musbfsh->mregs;
/* NOTE: caller guarantees it will turn off the reset when
* the appropriate amount of time has passed
*/
power = musbfsh_readb(mbase, MUSBFSH_POWER);
WARNING("reset=%d power=0x%x\n", do_reset, power);
if (do_reset) {
if(power & MUSBFSH_POWER_SUSPENDM) {
WARNING("reset a suspended device\n");
#ifdef MTK_USB_RUNTIME_SUPPORT
//ERR("EINT to wake up MD for reset\n");
//request_wakeup_md_timeout(0, 0); //wx, we may have to reset a suspended MD
#endif
musbfsh_writeb(mbase, MUSBFSH_POWER,
power | MUSBFSH_POWER_RESUME);
mdelay(20);
musbfsh_writeb(mbase, MUSBFSH_POWER,
power & ~MUSBFSH_POWER_RESUME);
}
/*
* If RESUME is set, we must make sure it stays minimum 20 ms.
* Then we must clear RESUME and wait a bit to let musb start
* generating SOFs. If we don't do this, OPT HS A 6.8 tests
* fail with "Error! Did not receive an SOF before suspend
* detected".
*/
if (power & MUSBFSH_POWER_RESUME) {
WARNING("reset a resuming device\n");
while (time_before(jiffies, musbfsh->rh_timer))
mdelay(1);
musbfsh_writeb(mbase, MUSBFSH_POWER,//stop the resume signal
power & ~MUSBFSH_POWER_RESUME);
mdelay(1);
}
musbfsh->ignore_disconnect = true;
power &= 0xf0;
musbfsh_writeb(mbase, MUSBFSH_POWER,
power | MUSBFSH_POWER_RESET);
mb();
musbfsh->port1_status |= USB_PORT_STAT_RESET;
musbfsh->port1_status &= ~USB_PORT_STAT_ENABLE;
musbfsh->rh_timer = jiffies + msecs_to_jiffies(50);
} else {
INFO( "Root port reset stopped\n");
#ifdef MTK_ICUSB_SUPPORT
#include "musbfsh_mt65xx.h"
if(resistor_control_attr.value)
{
#if 0 /* improve signal quality, from Dingjun */
/* FS_DISC_DISABLE */
u32 TM1;
TM1 = musbfsh_readl(mbase, 0x604);
musbfsh_writel(mbase, 0x604, TM1 | 0x4);
MYDBG("set FS_DISC_DISABLE\n");
#endif
/* original flow from SS5
USB11PHY_SET8(U1PHTCR2, force_usb11_dm_rpd | force_usb11_dp_rpd);
USB11PHY_CLR8(U1PHTCR2, RG_USB11_DM_RPD | RG_USB11_DP_RPD); // disconnect host port's pull down resistors on D+ and D-
USB11PHY_SET8(U1PHTCR2, force_usb11_dp_rpu | RG_USB11_DP_RPU); // tell MAC there still is a device attached, ohterwise we will get disconnect interrupt
*/
/* force */
USB11PHY_SET8(0x6a, 0x20 | 0x10);
/* RG */
USB11PHY_CLR8(0x68, 0x80 | 0x40); // disconnect host port's pull down resistors on D+ and D-
//USB11PHY_SET8(U1PHTCR2, force_usb11_dp_rpu | RG_USB11_DP_RPU); // tell MAC there still is a device attached, ohterwise we will get disconnect interrupt
MYDBG("USB1.1 PHY special config for IC-USB\n");
}
else
{
MYDBG("");
}
#endif
musbfsh_writeb(mbase, MUSBFSH_POWER,
power & ~MUSBFSH_POWER_RESET);
#ifdef MTK_ICUSB_SUPPORT
if(resistor_control_attr.value)
{
USB11PHY_CLR8(0x6a, 0x20 | 0x10);
}
else
{
MYDBG("");
}
#endif
mb();
musbfsh->ignore_disconnect = false;
power = musbfsh_readb(mbase, MUSBFSH_POWER);
if (power & MUSBFSH_POWER_HSMODE) {
INFO( "high-speed device connected\n");
musbfsh->port1_status |= USB_PORT_STAT_HIGH_SPEED;
}
#if 0 //IC_USB from SS5
#ifdef IC_USB
USB11PHY_SET8(U1PHTCR2, force_usb11_dm_rpd | force_usb11_dp_rpd);
USB11PHY_CLR8(U1PHTCR2, RG_USB11_DM_RPD | RG_USB11_DP_RPD); // disconnect host port's pull down resistors on D+ and D-
USB11PHY_SET8(U1PHTCR2, force_usb11_dp_rpu | RG_USB11_DP_RPU); // tell MAC there still is a device attached, ohterwise we will get disconnect interrupt
WARNING("USB1.1 PHY special config for IC-USB 0x%X=%x\n", U1PHTCR2, USB11PHY_READ8(U1PHTCR2));
#endif
#endif
musbfsh->port1_status &= ~USB_PORT_STAT_RESET;
musbfsh->port1_status |= USB_PORT_STAT_ENABLE
| (USB_PORT_STAT_C_RESET << 16)
| (USB_PORT_STAT_C_ENABLE << 16);
usb_hcd_poll_rh_status(musbfsh_to_hcd(musbfsh));//call back func to notify the hub thread the state of hub!
musbfsh->vbuserr_retry = VBUSERR_RETRY_COUNT;
}
}
void musbfsh_root_disconnect(struct musbfsh *musbfsh)
{
INFO("musbfsh_root_disconnect++\r\n");
musbfsh->port1_status = USB_PORT_STAT_POWER
| (USB_PORT_STAT_C_CONNECTION << 16);
usb_hcd_poll_rh_status(musbfsh_to_hcd(musbfsh));
musbfsh->is_active = 0;
}
/*---------------------------------------------------------------------*/
/* Caller may or may not hold musbfsh->lock */
int musbfsh_hub_status_data(struct usb_hcd *hcd, char *buf)
{
struct musbfsh *musbfsh = hcd_to_musbfsh(hcd);
int retval = 0;
INFO("musbfsh_hub_status_data++\r\n");
/* called in_irq() via usb_hcd_poll_rh_status() */
if (musbfsh->port1_status & 0xffff0000) {
*buf = 0x02;
retval = 1;
}
return retval;
}
int musbfsh_hub_control(
struct usb_hcd *hcd,
u16 typeReq,
u16 wValue,
u16 wIndex,
char *buf,
u16 wLength)
{
struct musbfsh *musbfsh = hcd_to_musbfsh(hcd);
u32 temp;
int retval = 0;
unsigned long flags;
INFO("musbfsh_hub_control++,typeReq=0x%x,wValue=0x%x,wIndex=0x%x\r\n",typeReq,wValue,wIndex);
spin_lock_irqsave(&musbfsh->lock, flags);
if (unlikely(!HCD_HW_ACCESSIBLE(hcd))) {
spin_unlock_irqrestore(&musbfsh->lock, flags);
return -ESHUTDOWN;
}
/* hub features: always zero, setting is a NOP
* port features: reported, sometimes updated when host is active
* no indicators
*/
switch (typeReq) {
case ClearHubFeature:
case SetHubFeature:
switch (wValue) {
case C_HUB_OVER_CURRENT:
case C_HUB_LOCAL_POWER:
break;
default:
goto error;
}
break;
case ClearPortFeature:
if ((wIndex & 0xff) != 1)//wIndex indicate the port number, here it is should be 1
goto error;
switch (wValue) {
case USB_PORT_FEAT_ENABLE:
break;
case USB_PORT_FEAT_SUSPEND:
musbfsh_port_suspend(musbfsh, false);//here is clearing the suspend
break;
case USB_PORT_FEAT_POWER:
#ifndef MTK_ALPS_BOX_SUPPORT
musbfsh_set_vbus(musbfsh, 0);//only power off the vbus
#else
musbfsh_platform_set_vbus(musbfsh, 0);//only power off the vbus
#endif
break;
case USB_PORT_FEAT_C_CONNECTION:
case USB_PORT_FEAT_C_ENABLE:
case USB_PORT_FEAT_C_OVER_CURRENT:
case USB_PORT_FEAT_C_RESET:
case USB_PORT_FEAT_C_SUSPEND:
break;
default:
goto error;
}
INFO("clear feature %d\n", wValue);
musbfsh->port1_status &= ~(1 << wValue);
break;
case GetHubDescriptor:
{
struct usb_hub_descriptor *desc = (void *)buf;
desc->bDescLength = 9;
desc->bDescriptorType = 0x29;
desc->bNbrPorts = 1;
desc->wHubCharacteristics = cpu_to_le16(
0x0001 /* per-port power switching */
| 0x0010 /* no overcurrent reporting */
);
desc->bPwrOn2PwrGood = 5; /* msec/2 */
desc->bHubContrCurrent = 0;
/* workaround bogus struct definition */
desc->u.hs.DeviceRemovable[0] = 0x02; /* port 1 */
desc->u.hs.DeviceRemovable[1] = 0xff;
}
break;
case GetHubStatus:
temp = 0;
*(__le32 *) buf = cpu_to_le32(temp);
break;
case GetPortStatus:
if (wIndex != 1)
goto error;
/* finish RESET signaling? */
if ((musbfsh->port1_status & USB_PORT_STAT_RESET)
&& time_after_eq(jiffies, musbfsh->rh_timer))
musbfsh_port_reset(musbfsh, false);//stop the reset because the timeout of reset.
/* finish RESUME signaling? */
if ((musbfsh->port1_status & MUSBFSH_PORT_STAT_RESUME)
&& time_after_eq(jiffies, musbfsh->rh_timer)) {
u8 power;
power = musbfsh_readb(musbfsh->mregs, MUSBFSH_POWER);
power &= ~MUSBFSH_POWER_RESUME;
WARNING( "Root port resume stopped, power 0x%02x\n",power);
musbfsh_writeb(musbfsh->mregs, MUSBFSH_POWER, power);
#ifdef MTK_USB_RUNTIME_SUPPORT
//mt_eint_unmask(CUST_EINT_MT6280_USB_WAKEUP_NUM);
#endif
/* ISSUE: DaVinci (RTL 1.300) disconnects after
* resume of high speed peripherals (but not full
* speed ones).
*/
musbfsh->is_active = 1;
musbfsh->port1_status &= ~(USB_PORT_STAT_SUSPEND
| MUSBFSH_PORT_STAT_RESUME);
musbfsh->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
usb_hcd_poll_rh_status(musbfsh_to_hcd(musbfsh));
}
put_unaligned(cpu_to_le32(musbfsh->port1_status
& ~MUSBFSH_PORT_STAT_RESUME),
(__le32 *) buf);
/* port change status is more interesting */
INFO("port status %08x,devctl=0x%x\n",musbfsh->port1_status,musbfsh_readb(musbfsh->mregs, MUSBFSH_DEVCTL));
break;
case SetPortFeature:
if ((wIndex & 0xff) != 1)
goto error;
switch (wValue) {
case USB_PORT_FEAT_POWER:
/* NOTE: this controller has a strange state machine
* that involves "requesting sessions" according to
* magic side effects from incompletely-described
* rules about startup...
*
* This call is what really starts the host mode; be
* very careful about side effects if you reorder any
* initialization logic, e.g. for OTG, or change any
* logic relating to VBUS power-up.
*/
INFO("musbfsh_start is called in hub control\r\n");
#ifdef MTK_ICUSB_SUPPORT
if(skip_mac_init_attr.value)
{
MYDBG("");
}
else
{
musbfsh_start(musbfsh);
}
#else
musbfsh_start(musbfsh);
#endif
break;
case USB_PORT_FEAT_RESET:
musbfsh_port_reset(musbfsh, true);//enable the reset, but not finish
break;
case USB_PORT_FEAT_SUSPEND:
musbfsh_port_suspend(musbfsh, true);
break;
case USB_PORT_FEAT_TEST:
#if 0
if (unlikely(is_host_active(musbfsh)))
goto error;
wIndex >>= 8;
switch (wIndex) {
case 1:
pr_debug("TEST_J\n");
temp = MUSBFSH_TEST_J;
break;
case 2:
pr_debug("TEST_K\n");
temp = MUSBFSH_TEST_K;
break;
case 3:
pr_debug("TEST_SE0_NAK\n");
temp = MUSBFSH_TEST_SE0_NAK;
break;
case 4:
pr_debug("TEST_PACKET\n");
temp = MUSBFSH_TEST_PACKET;
musbfsh_load_testpacket(musbfsh);
break;
case 5:
pr_debug("TEST_FORCE_ENABLE\n");
temp = MUSBFSH_TEST_FORCE_HOST
| MUSBFSH_TEST_FORCE_FS;
musbfsh_writeb(musbfsh->mregs, MUSBFSH_DEVCTL,
MUSBFSH_DEVCTL_SESSION);
break;
case 6:
pr_debug("TEST_FIFO_ACCESS\n");
temp = MUSBFSH_TEST_FIFO_ACCESS;
break;
default:
goto error;
}
musbfsh_writeb(musbfsh->mregs, MUSBFSH_TESTMODE, temp);
#endif
break;
default:
goto error;
}
INFO("set feature %d\n", wValue);
musbfsh->port1_status |= 1 << wValue;
break;
default:
error:
/* "protocol stall" on error */
retval = -EPIPE;
}
spin_unlock_irqrestore(&musbfsh->lock, flags);
return retval;
}
|