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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
|
/* ehci-msm2.c - HSUSB Host Controller Driver Implementation
*
* Copyright (c) 2008-2012, The Linux Foundation. All rights reserved.
*
* Partly derived from ehci-fsl.c and ehci-hcd.c
* Copyright (c) 2000-2004 by David Brownell
* Copyright (c) 2005 MontaVista Software
*
* All source code in this file is licensed under the following license except
* where indicated.
*
* 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, you can find it at http://www.fsf.org
*/
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/wakelock.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
#include <linux/usb/ulpi.h>
#include <linux/usb/msm_hsusb_hw.h>
#include <linux/usb/msm_hsusb.h>
#include <mach/clk.h>
#include <mach/msm_xo.h>
#include <mach/msm_iomap.h>
#define MSM_USB_BASE (hcd->regs)
#define PDEV_NAME_LEN 20
struct msm_hcd {
struct ehci_hcd ehci;
struct device *dev;
struct clk *iface_clk;
struct clk *core_clk;
struct clk *alt_core_clk;
struct regulator *hsusb_vddcx;
struct regulator *hsusb_3p3;
struct regulator *hsusb_1p8;
struct regulator *vbus;
struct msm_xo_voter *xo_handle;
bool async_int;
bool vbus_on;
atomic_t in_lpm;
int pmic_gpio_dp_irq;
bool pmic_gpio_dp_irq_enabled;
uint32_t pmic_gpio_int_cnt;
atomic_t pm_usage_cnt;
struct wake_lock wlock;
struct work_struct phy_susp_fail_work;
};
static inline struct msm_hcd *hcd_to_mhcd(struct usb_hcd *hcd)
{
return (struct msm_hcd *) (hcd->hcd_priv);
}
static inline struct usb_hcd *mhcd_to_hcd(struct msm_hcd *mhcd)
{
return container_of((void *) mhcd, struct usb_hcd, hcd_priv);
}
#define HSUSB_PHY_3P3_VOL_MIN 3050000 /* uV */
#define HSUSB_PHY_3P3_VOL_MAX 3300000 /* uV */
#define HSUSB_PHY_3P3_HPM_LOAD 50000 /* uA */
#define HSUSB_PHY_1P8_VOL_MIN 1800000 /* uV */
#define HSUSB_PHY_1P8_VOL_MAX 1800000 /* uV */
#define HSUSB_PHY_1P8_HPM_LOAD 50000 /* uA */
#define HSUSB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
#define HSUSB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
#define HSUSB_PHY_VDD_DIG_LOAD 49360 /* uA */
static int msm_ehci_init_vddcx(struct msm_hcd *mhcd, int init)
{
int ret = 0;
if (!init)
goto disable_reg;
mhcd->hsusb_vddcx = devm_regulator_get(mhcd->dev, "HSUSB_VDDCX");
if (IS_ERR(mhcd->hsusb_vddcx)) {
dev_err(mhcd->dev, "unable to get ehci vddcx\n");
return PTR_ERR(mhcd->hsusb_vddcx);
}
ret = regulator_set_voltage(mhcd->hsusb_vddcx,
HSUSB_PHY_VDD_DIG_VOL_MIN,
HSUSB_PHY_VDD_DIG_VOL_MAX);
if (ret) {
dev_err(mhcd->dev, "unable to set the voltage"
"for ehci vddcx\n");
return ret;
}
ret = regulator_set_optimum_mode(mhcd->hsusb_vddcx,
HSUSB_PHY_VDD_DIG_LOAD);
if (ret < 0) {
dev_err(mhcd->dev, "%s: Unable to set optimum mode of the"
" regulator: VDDCX\n", __func__);
goto reg_optimum_mode_err;
}
ret = regulator_enable(mhcd->hsusb_vddcx);
if (ret) {
dev_err(mhcd->dev, "unable to enable ehci vddcx\n");
goto reg_enable_err;
}
return 0;
disable_reg:
regulator_disable(mhcd->hsusb_vddcx);
reg_enable_err:
regulator_set_optimum_mode(mhcd->hsusb_vddcx, 0);
reg_optimum_mode_err:
regulator_set_voltage(mhcd->hsusb_vddcx, 0,
HSUSB_PHY_VDD_DIG_VOL_MIN);
return ret;
}
static int msm_ehci_ldo_init(struct msm_hcd *mhcd, int init)
{
int rc = 0;
if (!init)
goto put_1p8;
mhcd->hsusb_3p3 = devm_regulator_get(mhcd->dev, "HSUSB_3p3");
if (IS_ERR(mhcd->hsusb_3p3)) {
dev_err(mhcd->dev, "unable to get hsusb 3p3\n");
return PTR_ERR(mhcd->hsusb_3p3);
}
rc = regulator_set_voltage(mhcd->hsusb_3p3,
HSUSB_PHY_3P3_VOL_MIN, HSUSB_PHY_3P3_VOL_MAX);
if (rc) {
dev_err(mhcd->dev, "unable to set voltage level for"
"hsusb 3p3\n");
return rc;
}
mhcd->hsusb_1p8 = devm_regulator_get(mhcd->dev, "HSUSB_1p8");
if (IS_ERR(mhcd->hsusb_1p8)) {
dev_err(mhcd->dev, "unable to get hsusb 1p8\n");
rc = PTR_ERR(mhcd->hsusb_1p8);
goto put_3p3_lpm;
}
rc = regulator_set_voltage(mhcd->hsusb_1p8,
HSUSB_PHY_1P8_VOL_MIN, HSUSB_PHY_1P8_VOL_MAX);
if (rc) {
dev_err(mhcd->dev, "unable to set voltage level for"
"hsusb 1p8\n");
goto put_1p8;
}
return 0;
put_1p8:
regulator_set_voltage(mhcd->hsusb_1p8, 0, HSUSB_PHY_1P8_VOL_MAX);
put_3p3_lpm:
regulator_set_voltage(mhcd->hsusb_3p3, 0, HSUSB_PHY_3P3_VOL_MAX);
return rc;
}
#ifdef CONFIG_PM_SLEEP
#define HSUSB_PHY_SUSP_DIG_VOL_P50 500000
#define HSUSB_PHY_SUSP_DIG_VOL_P75 750000
static int msm_ehci_config_vddcx(struct msm_hcd *mhcd, int high)
{
struct msm_usb_host_platform_data *pdata;
int max_vol = HSUSB_PHY_VDD_DIG_VOL_MAX;
int min_vol;
int ret;
pdata = mhcd->dev->platform_data;
if (high)
min_vol = HSUSB_PHY_VDD_DIG_VOL_MIN;
else if (pdata && pdata->dock_connect_irq &&
!irq_read_line(pdata->dock_connect_irq))
min_vol = HSUSB_PHY_SUSP_DIG_VOL_P75;
else
min_vol = HSUSB_PHY_SUSP_DIG_VOL_P50;
ret = regulator_set_voltage(mhcd->hsusb_vddcx, min_vol, max_vol);
if (ret) {
dev_err(mhcd->dev, "%s: unable to set the voltage of regulator"
" HSUSB_VDDCX\n", __func__);
return ret;
}
dev_dbg(mhcd->dev, "%s: min_vol:%d max_vol:%d\n", __func__, min_vol,
max_vol);
return ret;
}
#else
static int msm_ehci_config_vddcx(struct msm_hcd *mhcd, int high)
{
return 0;
}
#endif
static void msm_ehci_vbus_power(struct msm_hcd *mhcd, bool on)
{
int ret;
if (!mhcd->vbus) {
pr_err("vbus is NULL.");
return;
}
if (mhcd->vbus_on == on)
return;
if (on) {
ret = regulator_enable(mhcd->vbus);
if (ret) {
pr_err("unable to enable vbus\n");
return;
}
mhcd->vbus_on = true;
} else {
ret = regulator_disable(mhcd->vbus);
if (ret) {
pr_err("unable to disable vbus\n");
return;
}
mhcd->vbus_on = false;
}
}
static irqreturn_t msm_ehci_dock_connect_irq(int irq, void *data)
{
const struct msm_usb_host_platform_data *pdata;
struct msm_hcd *mhcd = data;
struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
pdata = mhcd->dev->platform_data;
if (atomic_read(&mhcd->in_lpm))
usb_hcd_resume_root_hub(hcd);
if (irq_read_line(pdata->dock_connect_irq)) {
dev_dbg(mhcd->dev, "%s:Dock removed disable vbus\n", __func__);
msm_ehci_vbus_power(mhcd, 0);
} else {
dev_dbg(mhcd->dev, "%s:Dock connected enable vbus\n", __func__);
msm_ehci_vbus_power(mhcd, 1);
}
return IRQ_HANDLED;
}
static int msm_ehci_init_vbus(struct msm_hcd *mhcd, int init)
{
int rc = 0;
struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
const struct msm_usb_host_platform_data *pdata;
pdata = mhcd->dev->platform_data;
if (!init) {
if (pdata && pdata->dock_connect_irq)
free_irq(pdata->dock_connect_irq, mhcd);
return rc;
}
mhcd->vbus = devm_regulator_get(mhcd->dev, "vbus");
if (IS_ERR(mhcd->vbus)) {
pr_err("Unable to get vbus\n");
return -ENODEV;
}
if (pdata) {
hcd->power_budget = pdata->power_budget;
if (pdata->dock_connect_irq) {
rc = request_threaded_irq(pdata->dock_connect_irq, NULL,
msm_ehci_dock_connect_irq,
IRQF_TRIGGER_FALLING |
IRQF_TRIGGER_RISING |
IRQF_ONESHOT, "msm_ehci_host", mhcd);
if (!rc)
enable_irq_wake(pdata->dock_connect_irq);
}
}
return rc;
}
static int msm_ehci_ldo_enable(struct msm_hcd *mhcd, int on)
{
int ret = 0;
if (IS_ERR(mhcd->hsusb_1p8)) {
dev_err(mhcd->dev, "%s: HSUSB_1p8 is not initialized\n",
__func__);
return -ENODEV;
}
if (IS_ERR(mhcd->hsusb_3p3)) {
dev_err(mhcd->dev, "%s: HSUSB_3p3 is not initialized\n",
__func__);
return -ENODEV;
}
if (on) {
ret = regulator_set_optimum_mode(mhcd->hsusb_1p8,
HSUSB_PHY_1P8_HPM_LOAD);
if (ret < 0) {
dev_err(mhcd->dev, "%s: Unable to set HPM of the"
" regulator: HSUSB_1p8\n", __func__);
return ret;
}
ret = regulator_enable(mhcd->hsusb_1p8);
if (ret) {
dev_err(mhcd->dev, "%s: unable to enable the hsusb"
" 1p8\n", __func__);
regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
return ret;
}
ret = regulator_set_optimum_mode(mhcd->hsusb_3p3,
HSUSB_PHY_3P3_HPM_LOAD);
if (ret < 0) {
dev_err(mhcd->dev, "%s: Unable to set HPM of the "
"regulator: HSUSB_3p3\n", __func__);
regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
regulator_disable(mhcd->hsusb_1p8);
return ret;
}
ret = regulator_enable(mhcd->hsusb_3p3);
if (ret) {
dev_err(mhcd->dev, "%s: unable to enable the "
"hsusb 3p3\n", __func__);
regulator_set_optimum_mode(mhcd->hsusb_3p3, 0);
regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
regulator_disable(mhcd->hsusb_1p8);
return ret;
}
} else {
ret = regulator_disable(mhcd->hsusb_1p8);
if (ret) {
dev_err(mhcd->dev, "%s: unable to disable the "
"hsusb 1p8\n", __func__);
return ret;
}
ret = regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
if (ret < 0)
dev_err(mhcd->dev, "%s: Unable to set LPM of the "
"regulator: HSUSB_1p8\n", __func__);
ret = regulator_disable(mhcd->hsusb_3p3);
if (ret) {
dev_err(mhcd->dev, "%s: unable to disable the "
"hsusb 3p3\n", __func__);
return ret;
}
ret = regulator_set_optimum_mode(mhcd->hsusb_3p3, 0);
if (ret < 0)
dev_err(mhcd->dev, "%s: Unable to set LPM of the "
"regulator: HSUSB_3p3\n", __func__);
}
dev_dbg(mhcd->dev, "reg (%s)\n", on ? "HPM" : "LPM");
return ret < 0 ? ret : 0;
}
#define ULPI_IO_TIMEOUT_USECS (10 * 1000)
static int msm_ulpi_read(struct msm_hcd *mhcd, u32 reg)
{
struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
unsigned long timeout;
/* initiate read operation */
writel_relaxed(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
USB_ULPI_VIEWPORT);
/* wait for completion */
timeout = jiffies + usecs_to_jiffies(ULPI_IO_TIMEOUT_USECS);
while (readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN) {
if (time_after(jiffies, timeout)) {
dev_err(mhcd->dev, "msm_ulpi_read: timeout %08x\n",
readl_relaxed(USB_ULPI_VIEWPORT));
return -ETIMEDOUT;
}
udelay(1);
}
return ULPI_DATA_READ(readl_relaxed(USB_ULPI_VIEWPORT));
}
static int msm_ulpi_write(struct msm_hcd *mhcd, u32 val, u32 reg)
{
struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
unsigned long timeout;
/* initiate write operation */
writel_relaxed(ULPI_RUN | ULPI_WRITE |
ULPI_ADDR(reg) | ULPI_DATA(val),
USB_ULPI_VIEWPORT);
/* wait for completion */
timeout = jiffies + usecs_to_jiffies(ULPI_IO_TIMEOUT_USECS);
while (readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN) {
if (time_after(jiffies, timeout)) {
dev_err(mhcd->dev, "msm_ulpi_write: timeout\n");
return -ETIMEDOUT;
}
udelay(1);
}
return 0;
}
static int msm_ehci_link_clk_reset(struct msm_hcd *mhcd, bool assert)
{
int ret;
if (assert) {
ret = clk_reset(mhcd->alt_core_clk, CLK_RESET_ASSERT);
if (ret)
dev_err(mhcd->dev, "usb alt_core_clk assert failed\n");
} else {
ret = clk_reset(mhcd->alt_core_clk, CLK_RESET_DEASSERT);
if (ret)
dev_err(mhcd->dev, "usb alt_core_clk deassert failed\n");
}
return ret;
}
static int msm_ehci_phy_reset(struct msm_hcd *mhcd)
{
struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
u32 val;
int ret;
int retries;
ret = msm_ehci_link_clk_reset(mhcd, 1);
if (ret)
return ret;
udelay(1);
ret = msm_ehci_link_clk_reset(mhcd, 0);
if (ret)
return ret;
val = readl_relaxed(USB_PORTSC) & ~PORTSC_PTS_MASK;
writel_relaxed(val | PORTSC_PTS_ULPI, USB_PORTSC);
for (retries = 3; retries > 0; retries--) {
ret = msm_ulpi_write(mhcd, ULPI_FUNC_CTRL_SUSPENDM,
ULPI_CLR(ULPI_FUNC_CTRL));
if (!ret)
break;
}
if (!retries)
return -ETIMEDOUT;
/* Wakeup the PHY with a reg-access for calibration */
for (retries = 3; retries > 0; retries--) {
ret = msm_ulpi_read(mhcd, ULPI_DEBUG);
if (ret != -ETIMEDOUT)
break;
}
if (!retries)
return -ETIMEDOUT;
dev_info(mhcd->dev, "phy_reset: success\n");
return 0;
}
#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
static int msm_hsusb_reset(struct msm_hcd *mhcd)
{
struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
unsigned long timeout;
int ret;
clk_prepare_enable(mhcd->alt_core_clk);
ret = msm_ehci_phy_reset(mhcd);
if (ret) {
dev_err(mhcd->dev, "phy_reset failed\n");
return ret;
}
writel_relaxed(USBCMD_RESET, USB_USBCMD);
timeout = jiffies + usecs_to_jiffies(LINK_RESET_TIMEOUT_USEC);
while (readl_relaxed(USB_USBCMD) & USBCMD_RESET) {
if (time_after(jiffies, timeout))
return -ETIMEDOUT;
udelay(1);
}
/* select ULPI phy */
writel_relaxed(0x80000000, USB_PORTSC);
msleep(100);
writel_relaxed(0x0, USB_AHBBURST);
writel_relaxed(0x08, USB_AHBMODE);
/* Ensure that RESET operation is completed before turning off clock */
mb();
clk_disable_unprepare(mhcd->alt_core_clk);
/*rising edge interrupts with Dp rise and fall enabled*/
msm_ulpi_write(mhcd, ULPI_INT_DP, ULPI_USB_INT_EN_RISE);
msm_ulpi_write(mhcd, ULPI_INT_DP, ULPI_USB_INT_EN_FALL);
/*Clear the PHY interrupts by reading the PHY interrupt latch register*/
msm_ulpi_read(mhcd, ULPI_USB_INT_LATCH);
return 0;
}
static void msm_ehci_phy_susp_fail_work(struct work_struct *w)
{
struct msm_hcd *mhcd = container_of(w, struct msm_hcd,
phy_susp_fail_work);
struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
msm_ehci_vbus_power(mhcd, 0);
usb_remove_hcd(hcd);
msm_hsusb_reset(mhcd);
usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
msm_ehci_vbus_power(mhcd, 1);
}
#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
#ifdef CONFIG_PM_SLEEP
static int msm_ehci_suspend(struct msm_hcd *mhcd)
{
struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
unsigned long timeout;
int ret;
u32 portsc;
if (atomic_read(&mhcd->in_lpm)) {
dev_dbg(mhcd->dev, "%s called in lpm\n", __func__);
return 0;
}
disable_irq(hcd->irq);
/* Set the PHCD bit, only if it is not set by the controller.
* PHY may take some time or even fail to enter into low power
* mode (LPM). Hence poll for 500 msec and reset the PHY and link
* in failure case.
*/
portsc = readl_relaxed(USB_PORTSC);
if (!(portsc & PORTSC_PHCD)) {
writel_relaxed(portsc | PORTSC_PHCD,
USB_PORTSC);
timeout = jiffies + usecs_to_jiffies(PHY_SUSPEND_TIMEOUT_USEC);
while (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD)) {
if (time_after(jiffies, timeout)) {
dev_err(mhcd->dev, "Unable to suspend PHY\n");
schedule_work(&mhcd->phy_susp_fail_work);
return -ETIMEDOUT;
}
udelay(1);
}
}
/*
* PHY has capability to generate interrupt asynchronously in low
* power mode (LPM). This interrupt is level triggered. So USB IRQ
* line must be disabled till async interrupt enable bit is cleared
* in USBCMD register. Assert STP (ULPI interface STOP signal) to
* block data communication from PHY.
*/
writel_relaxed(readl_relaxed(USB_USBCMD) | ASYNC_INTR_CTRL |
ULPI_STP_CTRL, USB_USBCMD);
/*
* Ensure that hardware is put in low power mode before
* clocks are turned OFF and VDD is allowed to minimize.
*/
mb();
clk_disable_unprepare(mhcd->iface_clk);
clk_disable_unprepare(mhcd->core_clk);
/* usb phy does not require TCXO clock, hence vote for TCXO disable */
ret = msm_xo_mode_vote(mhcd->xo_handle, MSM_XO_MODE_OFF);
if (ret)
dev_err(mhcd->dev, "%s failed to devote for "
"TCXO D0 buffer%d\n", __func__, ret);
msm_ehci_config_vddcx(mhcd, 0);
atomic_set(&mhcd->in_lpm, 1);
enable_irq(hcd->irq);
if (mhcd->pmic_gpio_dp_irq) {
mhcd->pmic_gpio_dp_irq_enabled = 1;
enable_irq_wake(mhcd->pmic_gpio_dp_irq);
enable_irq(mhcd->pmic_gpio_dp_irq);
}
wake_unlock(&mhcd->wlock);
dev_info(mhcd->dev, "EHCI USB in low power mode\n");
return 0;
}
static int msm_ehci_resume(struct msm_hcd *mhcd)
{
struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
unsigned long timeout;
unsigned temp;
int ret;
if (!atomic_read(&mhcd->in_lpm)) {
dev_dbg(mhcd->dev, "%s called in !in_lpm\n", __func__);
return 0;
}
if (mhcd->pmic_gpio_dp_irq_enabled) {
disable_irq_wake(mhcd->pmic_gpio_dp_irq);
disable_irq_nosync(mhcd->pmic_gpio_dp_irq);
mhcd->pmic_gpio_dp_irq_enabled = 0;
}
wake_lock(&mhcd->wlock);
/* Vote for TCXO when waking up the phy */
ret = msm_xo_mode_vote(mhcd->xo_handle, MSM_XO_MODE_ON);
if (ret)
dev_err(mhcd->dev, "%s failed to vote for "
"TCXO D0 buffer%d\n", __func__, ret);
clk_prepare_enable(mhcd->core_clk);
clk_prepare_enable(mhcd->iface_clk);
msm_ehci_config_vddcx(mhcd, 1);
temp = readl_relaxed(USB_USBCMD);
temp &= ~ASYNC_INTR_CTRL;
temp &= ~ULPI_STP_CTRL;
writel_relaxed(temp, USB_USBCMD);
if (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD))
goto skip_phy_resume;
temp = readl_relaxed(USB_PORTSC) & ~PORTSC_PHCD;
writel_relaxed(temp, USB_PORTSC);
timeout = jiffies + usecs_to_jiffies(PHY_RESUME_TIMEOUT_USEC);
while ((readl_relaxed(USB_PORTSC) & PORTSC_PHCD) ||
!(readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_SYNC_STATE)) {
if (time_after(jiffies, timeout)) {
/*This is a fatal error. Reset the link and PHY*/
dev_err(mhcd->dev, "Unable to resume USB. Resetting the h/w\n");
msm_hsusb_reset(mhcd);
break;
}
udelay(1);
}
skip_phy_resume:
usb_hcd_resume_root_hub(hcd);
atomic_set(&mhcd->in_lpm, 0);
if (mhcd->async_int) {
mhcd->async_int = false;
pm_runtime_put_noidle(mhcd->dev);
enable_irq(hcd->irq);
}
if (atomic_read(&mhcd->pm_usage_cnt)) {
atomic_set(&mhcd->pm_usage_cnt, 0);
pm_runtime_put_noidle(mhcd->dev);
}
dev_info(mhcd->dev, "EHCI USB exited from low power mode\n");
return 0;
}
#endif
static irqreturn_t msm_ehci_irq(struct usb_hcd *hcd)
{
struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
if (atomic_read(&mhcd->in_lpm)) {
disable_irq_nosync(hcd->irq);
mhcd->async_int = true;
pm_runtime_get(mhcd->dev);
return IRQ_HANDLED;
}
return ehci_irq(hcd);
}
static irqreturn_t msm_ehci_host_wakeup_irq(int irq, void *data)
{
struct msm_hcd *mhcd = data;
mhcd->pmic_gpio_int_cnt++;
dev_dbg(mhcd->dev, "%s: hsusb host remote wakeup interrupt cnt: %u\n",
__func__, mhcd->pmic_gpio_int_cnt);
wake_lock(&mhcd->wlock);
if (mhcd->pmic_gpio_dp_irq_enabled) {
mhcd->pmic_gpio_dp_irq_enabled = 0;
disable_irq_wake(irq);
disable_irq_nosync(irq);
}
if (!atomic_read(&mhcd->pm_usage_cnt)) {
atomic_set(&mhcd->pm_usage_cnt, 1);
pm_runtime_get(mhcd->dev);
}
return IRQ_HANDLED;
}
static int msm_ehci_reset(struct usb_hcd *hcd)
{
struct ehci_hcd *ehci = hcd_to_ehci(hcd);
int retval;
ehci->caps = USB_CAPLENGTH;
ehci->regs = USB_CAPLENGTH +
HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
dbg_hcs_params(ehci, "reset");
dbg_hcc_params(ehci, "reset");
/* cache the data to minimize the chip reads*/
ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
hcd->has_tt = 1;
ehci->sbrn = HCD_USB2;
retval = ehci_halt(ehci);
if (retval)
return retval;
/* data structure init */
retval = ehci_init(hcd);
if (retval)
return retval;
retval = ehci_reset(ehci);
if (retval)
return retval;
/* bursts of unspecified length. */
writel_relaxed(0, USB_AHBBURST);
/* Use the AHB transactor */
writel_relaxed(0x08, USB_AHBMODE);
/* Disable streaming mode and select host mode */
writel_relaxed(0x13, USB_USBMODE);
ehci_port_power(ehci, 1);
return 0;
}
static struct hc_driver msm_hc2_driver = {
.description = hcd_name,
.product_desc = "Qualcomm EHCI Host Controller",
.hcd_priv_size = sizeof(struct msm_hcd),
/*
* generic hardware linkage
*/
.irq = msm_ehci_irq,
.flags = HCD_USB2 | HCD_MEMORY,
.reset = msm_ehci_reset,
.start = ehci_run,
.stop = ehci_stop,
.shutdown = ehci_shutdown,
/*
* managing i/o requests and associated device resources
*/
.urb_enqueue = ehci_urb_enqueue,
.urb_dequeue = ehci_urb_dequeue,
.endpoint_disable = ehci_endpoint_disable,
.endpoint_reset = ehci_endpoint_reset,
.clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
/*
* scheduling support
*/
.get_frame_number = ehci_get_frame,
/*
* root hub support
*/
.hub_status_data = ehci_hub_status_data,
.hub_control = ehci_hub_control,
.relinquish_port = ehci_relinquish_port,
.port_handed_over = ehci_port_handed_over,
/*
* PM support
*/
.bus_suspend = ehci_bus_suspend,
.bus_resume = ehci_bus_resume,
};
static int msm_ehci_init_clocks(struct msm_hcd *mhcd, u32 init)
{
int ret = 0;
if (!init)
goto put_clocks;
/* 60MHz alt_core_clk is for LINK to be used during PHY RESET */
mhcd->alt_core_clk = clk_get(mhcd->dev, "alt_core_clk");
if (IS_ERR(mhcd->alt_core_clk)) {
dev_err(mhcd->dev, "failed to get alt_core_clk\n");
ret = PTR_ERR(mhcd->alt_core_clk);
return ret;
}
clk_set_rate(mhcd->alt_core_clk, 60000000);
/* iface_clk is required for data transfers */
mhcd->iface_clk = clk_get(mhcd->dev, "iface_clk");
if (IS_ERR(mhcd->iface_clk)) {
dev_err(mhcd->dev, "failed to get iface_clk\n");
ret = PTR_ERR(mhcd->iface_clk);
goto put_alt_core_clk;
}
/* Link's protocol engine is based on pclk which must
* be running >55Mhz and frequency should also not change.
* Hence, vote for maximum clk frequency on its source
*/
mhcd->core_clk = clk_get(mhcd->dev, "core_clk");
if (IS_ERR(mhcd->core_clk)) {
dev_err(mhcd->dev, "failed to get core_clk\n");
ret = PTR_ERR(mhcd->core_clk);
goto put_iface_clk;
}
clk_set_rate(mhcd->core_clk, INT_MAX);
clk_prepare_enable(mhcd->core_clk);
clk_prepare_enable(mhcd->iface_clk);
return 0;
put_clocks:
if (!atomic_read(&mhcd->in_lpm)) {
clk_disable_unprepare(mhcd->iface_clk);
clk_disable_unprepare(mhcd->core_clk);
}
clk_put(mhcd->core_clk);
put_iface_clk:
clk_put(mhcd->iface_clk);
put_alt_core_clk:
clk_put(mhcd->alt_core_clk);
return ret;
}
static int __devinit ehci_msm2_probe(struct platform_device *pdev)
{
struct usb_hcd *hcd;
struct resource *res;
struct msm_hcd *mhcd;
const struct msm_usb_host_platform_data *pdata;
char pdev_name[PDEV_NAME_LEN];
int ret;
dev_dbg(&pdev->dev, "ehci_msm2 probe\n");
hcd = usb_create_hcd(&msm_hc2_driver, &pdev->dev,
dev_name(&pdev->dev));
if (!hcd) {
dev_err(&pdev->dev, "Unable to create HCD\n");
return -ENOMEM;
}
hcd->irq = platform_get_irq(pdev, 0);
if (hcd->irq < 0) {
dev_err(&pdev->dev, "Unable to get IRQ resource\n");
ret = hcd->irq;
goto put_hcd;
}
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res) {
dev_err(&pdev->dev, "Unable to get memory resource\n");
ret = -ENODEV;
goto put_hcd;
}
hcd->rsrc_start = res->start;
hcd->rsrc_len = resource_size(res);
hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
if (!hcd->regs) {
dev_err(&pdev->dev, "ioremap failed\n");
ret = -ENOMEM;
goto put_hcd;
}
mhcd = hcd_to_mhcd(hcd);
mhcd->dev = &pdev->dev;
snprintf(pdev_name, PDEV_NAME_LEN, "%s.%d", pdev->name, pdev->id);
mhcd->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, pdev_name);
if (IS_ERR(mhcd->xo_handle)) {
dev_err(&pdev->dev, "%s not able to get the handle "
"to vote for TCXO D0 buffer\n", __func__);
ret = PTR_ERR(mhcd->xo_handle);
goto unmap;
}
ret = msm_xo_mode_vote(mhcd->xo_handle, MSM_XO_MODE_ON);
if (ret) {
dev_err(&pdev->dev, "%s failed to vote for TCXO "
"D0 buffer%d\n", __func__, ret);
goto free_xo_handle;
}
ret = msm_ehci_init_clocks(mhcd, 1);
if (ret) {
dev_err(&pdev->dev, "unable to initialize clocks\n");
ret = -ENODEV;
goto devote_xo_handle;
}
ret = msm_ehci_init_vddcx(mhcd, 1);
if (ret) {
dev_err(&pdev->dev, "unable to initialize VDDCX\n");
ret = -ENODEV;
goto deinit_clocks;
}
ret = msm_ehci_config_vddcx(mhcd, 1);
if (ret) {
dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
goto deinit_vddcx;
}
ret = msm_ehci_ldo_init(mhcd, 1);
if (ret) {
dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
goto deinit_vddcx;
}
ret = msm_ehci_ldo_enable(mhcd, 1);
if (ret) {
dev_err(&pdev->dev, "hsusb vreg enable failed\n");
goto deinit_ldo;
}
ret = msm_ehci_init_vbus(mhcd, 1);
if (ret) {
dev_err(&pdev->dev, "unable to get vbus\n");
goto disable_ldo;
}
ret = msm_hsusb_reset(mhcd);
if (ret) {
dev_err(&pdev->dev, "hsusb PHY initialization failed\n");
goto vbus_deinit;
}
ret = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
if (ret) {
dev_err(&pdev->dev, "unable to register HCD\n");
goto vbus_deinit;
}
pdata = mhcd->dev->platform_data;
if (pdata && (!pdata->dock_connect_irq ||
!irq_read_line(pdata->dock_connect_irq)))
msm_ehci_vbus_power(mhcd, 1);
device_init_wakeup(&pdev->dev, 1);
wake_lock_init(&mhcd->wlock, WAKE_LOCK_SUSPEND, dev_name(&pdev->dev));
wake_lock(&mhcd->wlock);
INIT_WORK(&mhcd->phy_susp_fail_work, msm_ehci_phy_susp_fail_work);
/*
* This pdev->dev is assigned parent of root-hub by USB core,
* hence, runtime framework automatically calls this driver's
* runtime APIs based on root-hub's state.
*/
/* configure pmic_gpio_irq for D+ change */
if (pdata && pdata->pmic_gpio_dp_irq)
mhcd->pmic_gpio_dp_irq = pdata->pmic_gpio_dp_irq;
if (mhcd->pmic_gpio_dp_irq) {
ret = request_threaded_irq(mhcd->pmic_gpio_dp_irq, NULL,
msm_ehci_host_wakeup_irq,
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
"msm_ehci_host_wakeup", mhcd);
if (!ret) {
disable_irq_nosync(mhcd->pmic_gpio_dp_irq);
} else {
dev_err(&pdev->dev, "request_irq(%d) failed: %d\n",
mhcd->pmic_gpio_dp_irq, ret);
mhcd->pmic_gpio_dp_irq = 0;
}
}
pm_runtime_set_active(&pdev->dev);
pm_runtime_enable(&pdev->dev);
return 0;
vbus_deinit:
msm_ehci_init_vbus(mhcd, 0);
disable_ldo:
msm_ehci_ldo_enable(mhcd, 0);
deinit_ldo:
msm_ehci_ldo_init(mhcd, 0);
deinit_vddcx:
msm_ehci_init_vddcx(mhcd, 0);
deinit_clocks:
msm_ehci_init_clocks(mhcd, 0);
devote_xo_handle:
msm_xo_mode_vote(mhcd->xo_handle, MSM_XO_MODE_OFF);
free_xo_handle:
msm_xo_put(mhcd->xo_handle);
unmap:
iounmap(hcd->regs);
put_hcd:
usb_put_hcd(hcd);
return ret;
}
static int __devexit ehci_msm2_remove(struct platform_device *pdev)
{
struct usb_hcd *hcd = platform_get_drvdata(pdev);
struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
if (mhcd->pmic_gpio_dp_irq) {
if (mhcd->pmic_gpio_dp_irq_enabled)
disable_irq_wake(mhcd->pmic_gpio_dp_irq);
free_irq(mhcd->pmic_gpio_dp_irq, mhcd);
}
device_init_wakeup(&pdev->dev, 0);
pm_runtime_set_suspended(&pdev->dev);
usb_remove_hcd(hcd);
msm_xo_put(mhcd->xo_handle);
msm_ehci_vbus_power(mhcd, 0);
msm_ehci_init_vbus(mhcd, 0);
msm_ehci_ldo_enable(mhcd, 0);
msm_ehci_ldo_init(mhcd, 0);
msm_ehci_init_vddcx(mhcd, 0);
msm_ehci_init_clocks(mhcd, 0);
wake_lock_destroy(&mhcd->wlock);
iounmap(hcd->regs);
usb_put_hcd(hcd);
return 0;
}
#ifdef CONFIG_PM_SLEEP
static int ehci_msm2_pm_suspend(struct device *dev)
{
struct usb_hcd *hcd = dev_get_drvdata(dev);
struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
dev_dbg(dev, "ehci-msm2 PM suspend\n");
if (device_may_wakeup(dev))
enable_irq_wake(hcd->irq);
return msm_ehci_suspend(mhcd);
}
static int ehci_msm2_pm_resume(struct device *dev)
{
int ret;
struct usb_hcd *hcd = dev_get_drvdata(dev);
struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
dev_dbg(dev, "ehci-msm2 PM resume\n");
if (device_may_wakeup(dev))
disable_irq_wake(hcd->irq);
ret = msm_ehci_resume(mhcd);
if (ret)
return ret;
/* Bring the device to full powered state upon system resume */
pm_runtime_disable(dev);
pm_runtime_set_active(dev);
pm_runtime_enable(dev);
return 0;
}
#endif
#ifdef CONFIG_PM_RUNTIME
static int ehci_msm2_runtime_idle(struct device *dev)
{
dev_dbg(dev, "EHCI runtime idle\n");
return 0;
}
static int ehci_msm2_runtime_suspend(struct device *dev)
{
struct usb_hcd *hcd = dev_get_drvdata(dev);
struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
dev_dbg(dev, "EHCI runtime suspend\n");
return msm_ehci_suspend(mhcd);
}
static int ehci_msm2_runtime_resume(struct device *dev)
{
struct usb_hcd *hcd = dev_get_drvdata(dev);
struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
dev_dbg(dev, "EHCI runtime resume\n");
return msm_ehci_resume(mhcd);
}
#endif
#ifdef CONFIG_PM
static const struct dev_pm_ops ehci_msm2_dev_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(ehci_msm2_pm_suspend, ehci_msm2_pm_resume)
SET_RUNTIME_PM_OPS(ehci_msm2_runtime_suspend, ehci_msm2_runtime_resume,
ehci_msm2_runtime_idle)
};
#endif
static struct platform_driver ehci_msm2_driver = {
.probe = ehci_msm2_probe,
.remove = __devexit_p(ehci_msm2_remove),
.driver = {
.name = "msm_ehci_host",
#ifdef CONFIG_PM
.pm = &ehci_msm2_dev_pm_ops,
#endif
},
};
|