summaryrefslogtreecommitdiff
path: root/drivers/staging/fsl_pme2/pme2_suspend.c
blob: e2ef2af59b03972647fd40fb3694fa82c44e063a (plain)
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
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
/* Copyright 2013 Freescale Semiconductor, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Freescale Semiconductor nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 *
 *
 * ALTERNATIVELY, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") as published by the Free Software
 * Foundation, either version 2 of that License or (at your option) any
 * later version.
 *
 * This software is provided by Freescale Semiconductor "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 Freescale Semiconductor 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.
 */

#ifdef CONFIG_PM

#include "pme2_private.h"
#include "pme2_regs.h"
#include <linux/vmalloc.h>

static dma_addr_t pme_suspend_map(struct platform_device *pdev, void *ptr)
{
	return dma_map_single(&pdev->dev, ptr, 1, DMA_BIDIRECTIONAL);
}

/*
 * The following SRAM tables need to be saved
 *	1-byte trigger table
 *	2-byte trigger table
 *	variable length trigger table
 *	confidence table
 *	User-Defined Group Mapping tablle
 *	Equivalent Byte Mapping table
 *	Special Trigger table
 */
enum pme_pmtcc_table_id {
	PME_ONE_BYTE_TRIGGER_TBL	= 0x00,
	PME_TWO_BYTE_TRIGGER_TBL	= 0x01,
	PME_VARIABLE_TRIGGER_TBL	= 0x02,
	PME_CONFIDENCE_TBL		= 0x03,
	PME_UDG_TBL			= 0x05,
	PME_EQUIVALENT_BYTE_TBL		= 0x06,
	PME_SPECIAL_TRIGGER_TBL		= 0x08,
	PME_LAST_TABLE			= PME_SPECIAL_TRIGGER_TBL
};

static enum pme_pmtcc_table_id table_list[] = {PME_ONE_BYTE_TRIGGER_TBL,
	PME_TWO_BYTE_TRIGGER_TBL, PME_VARIABLE_TRIGGER_TBL, PME_CONFIDENCE_TBL,
	PME_UDG_TBL, PME_EQUIVALENT_BYTE_TBL, PME_SPECIAL_TRIGGER_TBL};

struct pme_pmtcc_header_t {
	uint8_t	protocol_version;
	uint8_t	msg_type;
	uint16_t	reserved;
	/* total message length, including the header */
	uint32_t	msg_length;
	uint64_t	msg_id;
	uint8_t	data[0];
} __packed;

/*
 * The next few macros define the sizes (in bytes) of the entries in
 * the different PM H/W tables.
 */
#define PME_ONE_BYTE_TRIGGER_ENTRY_SIZE		32
#define PME_TWO_BYTE_TRIGGER_ENTRY_SIZE		8
#define PME_VARIABLE_TRIGGER_ENTRY_SIZE		8
#define PME_CONFIDENCE_ENTRY_SIZE		4
#define PME_CONFIRMATION_ENTRY_SIZE		128
#define PME_USER_DEFINED_GROUP_READ_ENTRY_SIZE	4
#define PME_USER_DEFINED_GROUP_WRITE_ENTRY_SIZE	256
#define PME_EQUIVALENCE_READ_ENTRY_SIZE		4
#define PME_EQUIVALENCE_WRITE_ENTRY_SIZE	256
#define PME_SESSION_CONTEXT_ENTRY_SIZE		32
#define PME_SPECIAL_TRIGGER_ENTRY_SIZE		32

union pme_table_entry_t {
	/* The next few types define the entries for the different PM tables. */
	uint8_t one_byte_trigger_entry[PME_ONE_BYTE_TRIGGER_ENTRY_SIZE];
	uint8_t two_byte_trigger_entry[PME_TWO_BYTE_TRIGGER_ENTRY_SIZE];
	uint8_t variable_trigger_entry[PME_VARIABLE_TRIGGER_ENTRY_SIZE];
	uint8_t confidence_entry[PME_CONFIDENCE_ENTRY_SIZE];
	uint8_t udg_read_entry[PME_USER_DEFINED_GROUP_READ_ENTRY_SIZE];
	uint8_t udg_write_entry[PME_USER_DEFINED_GROUP_WRITE_ENTRY_SIZE];
	uint8_t equivalence_read_entry[PME_EQUIVALENCE_READ_ENTRY_SIZE];
	uint8_t equivalence_write_entry[PME_EQUIVALENCE_WRITE_ENTRY_SIZE];
	uint8_t special_trigger_entry[PME_SPECIAL_TRIGGER_ENTRY_SIZE];
} __packed;

/* This type defines an indexed table entry. */
struct pme_indexed_table_entry_t {
	uint32_t		index;
	union pme_table_entry_t	entry;
} __packed;

/* table read request */
struct pme_pmtcc_read_request_msg_t {
	struct pme_pmtcc_header_t	header;
	uint32_t	table_id;
	uint32_t	index;
} __packed;

/* table read reply message. */
struct pme_pmtcc_read_reply_msg_t {
	struct pme_pmtcc_header_t	header;
	uint32_t	table_id;
	struct pme_indexed_table_entry_t  indexed_entry;
} __packed;

/* table write request message */
struct pme_pmtcc_write_request_msg_t {
	struct pme_pmtcc_header_t	header;
	uint32_t	table_id;
	struct pme_indexed_table_entry_t  indexed_entry;
} __packed;

/*
 * The next few macros define the number of entries in the different PM
 * H/W tables.
 */
#define PME_CONFIDENCE_ENTRY_NUM_PER_TRIGGER_ENTRY 4

#define PME_ONE_BYTE_TRIGGER_ENTRY_NUM             1

#define PME_TWO_BYTE_TRIGGER_ENTRY_NUM_V1          512
#define PME_VARIABLE_TRIGGER_ENTRY_NUM_V1          4096

#define PME_TWO_BYTE_TRIGGER_ENTRY_NUM_V2_0        2048
#define PME_VARIABLE_TRIGGER_ENTRY_NUM_V2_0        16384

#define PME_TWO_BYTE_TRIGGER_ENTRY_NUM_V2_1        1024
#define PME_VARIABLE_TRIGGER_ENTRY_NUM_V2_1        8192

#define PME_TWO_BYTE_TRIGGER_ENTRY_NUM_V2_2        512
#define PME_VARIABLE_TRIGGER_ENTRY_NUM_V2_2        4096

#define PME_SPECIAL_CONFIDENCE_ENTRY_NUM           64
#define PME_ONE_BYTE_CONFIDENCE_ENTRY_NUM          64

#define PME_SPECIAL_CONFIDENCE_ENTRY_NUM_V2_2      32

#define PME_CONFIDENCE_ENTRY_NUM_V1		\
	((PME_TWO_BYTE_TRIGGER_ENTRY_NUM_V1 +	\
	PME_VARIABLE_TRIGGER_ENTRY_NUM_V1 +	\
	PME_ONE_BYTE_CONFIDENCE_ENTRY_NUM +	\
	PME_SPECIAL_CONFIDENCE_ENTRY_NUM) *	\
	PME_CONFIDENCE_ENTRY_NUM_PER_TRIGGER_ENTRY)

#define PME_CONFIDENCE_ENTRY_NUM_V2_0		\
	((PME_TWO_BYTE_TRIGGER_ENTRY_NUM_V2_0 +	\
	PME_VARIABLE_TRIGGER_ENTRY_NUM_V2_0 +	\
	PME_ONE_BYTE_CONFIDENCE_ENTRY_NUM +	\
	PME_SPECIAL_CONFIDENCE_ENTRY_NUM) *	\
	PME_CONFIDENCE_ENTRY_NUM_PER_TRIGGER_ENTRY)

#define PME_CONFIDENCE_ENTRY_NUM_V2_1		\
	((PME_TWO_BYTE_TRIGGER_ENTRY_NUM_V2_1 +	\
	PME_VARIABLE_TRIGGER_ENTRY_NUM_V2_1 +	\
	PME_ONE_BYTE_CONFIDENCE_ENTRY_NUM +	\
	PME_SPECIAL_CONFIDENCE_ENTRY_NUM) *	\
	PME_CONFIDENCE_ENTRY_NUM_PER_TRIGGER_ENTRY)

#define PME_CONFIDENCE_ENTRY_NUM_V2_2			\
	((PME_TWO_BYTE_TRIGGER_ENTRY_NUM_V2_2 +		\
	PME_VARIABLE_TRIGGER_ENTRY_NUM_V2_2 +		\
	PME_ONE_BYTE_CONFIDENCE_ENTRY_NUM +		\
	PME_SPECIAL_CONFIDENCE_ENTRY_NUM_V2_2) *	\
	PME_CONFIDENCE_ENTRY_NUM_PER_TRIGGER_ENTRY)

#define PME_EQUIVALENCE_ENTRY_NUM                  1
#define PME_USER_DEFINED_GROUP_ENTRY_NUM           1
#define PME_SPECIAL_TRIGGER_ENTRY_NUM              1

/*
 * The next few macros below define the sizes of the different
 * messages.  Note the the macros related to the table read and write
 * messages assume that there is only one entry in the read/write
 * message.
 */
#define PME_TABLE_READ_REQUEST_MSG_SIZE			\
	sizeof(struct pme_pmtcc_read_request_msg_t)

#define PME_ONE_BYTE_TABLE_READ_REPLY_MSG_SIZE		\
	(sizeof(struct pme_pmtcc_read_reply_msg_t) -	\
	sizeof(union pme_table_entry_t) +		\
	PME_ONE_BYTE_TRIGGER_ENTRY_SIZE)

#define PME_TWO_BYTE_TABLE_READ_REPLY_MSG_SIZE		\
	(sizeof(struct pme_pmtcc_read_reply_msg_t) -	\
	sizeof(union pme_table_entry_t) +		\
	PME_TWO_BYTE_TRIGGER_ENTRY_SIZE)

#define PME_VARIABLE_TABLE_READ_REPLY_MSG_SIZE		\
	(sizeof(struct pme_pmtcc_read_reply_msg_t) -	\
	sizeof(union pme_table_entry_t) +		\
	PME_VARIABLE_TRIGGER_ENTRY_SIZE)

#define PME_CONFIDENCE_TABLE_READ_REPLY_MSG_SIZE	\
	(sizeof(struct pme_pmtcc_read_reply_msg_t) -	\
	sizeof(union pme_table_entry_t) +		\
	PME_CONFIDENCE_ENTRY_SIZE)

#define PME_UDG_TABLE_READ_REPLY_MSG_SIZE		\
	(sizeof(struct pme_pmtcc_read_reply_msg_t) -	\
	sizeof(union pme_table_entry_t) +		\
	PME_USER_DEFINED_GROUP_READ_ENTRY_SIZE)

#define PME_EQUIVALENCE_TABLE_READ_REPLY_MSG_SIZE	\
	(sizeof(struct pme_pmtcc_read_reply_msg_t) -	\
	sizeof(union pme_table_entry_t) +		\
	PME_EQUIVALENCE_READ_ENTRY_SIZE)

#define PME_SPECIAL_TABLE_READ_REPLY_MSG_SIZE		\
	(sizeof(struct pme_pmtcc_read_reply_msg_t) -	\
	sizeof(union pme_table_entry_t) +		\
	PME_SPECIAL_TRIGGER_ENTRY_SIZE)

#define PME_ONE_BYTE_TABLE_WRITE_REQUEST_MSG_SIZE	\
	(sizeof(struct pme_pmtcc_write_request_msg_t) -	\
	sizeof(union pme_table_entry_t) +		\
	PME_ONE_BYTE_TRIGGER_ENTRY_SIZE)

#define PME_TWO_BYTE_TABLE_WRITE_REQUEST_MSG_SIZE	\
	(sizeof(struct pme_pmtcc_write_request_msg_t) -	\
	sizeof(union pme_table_entry_t) +		\
	PME_TWO_BYTE_TRIGGER_ENTRY_SIZE)

#define PME_VARIABLE_TABLE_WRITE_REQUEST_MSG_SIZE	\
	(sizeof(struct pme_pmtcc_write_request_msg_t) -	\
	sizeof(union pme_table_entry_t) +		\
	PME_VARIABLE_TRIGGER_ENTRY_SIZE)

#define PME_CONFIDENCE_TABLE_WRITE_REQUEST_MSG_SIZE	\
	(sizeof(struct pme_pmtcc_write_request_msg_t) -	\
	sizeof(union pme_table_entry_t) +		\
	PME_CONFIDENCE_ENTRY_SIZE)

#define PME_UDG_TABLE_WRITE_REQUEST_MSG_SIZE		\
	(sizeof(struct pme_pmtcc_write_request_msg_t) -	\
	sizeof(union pme_table_entry_t) +		\
	PME_USER_DEFINED_GROUP_WRITE_ENTRY_SIZE)

#define PME_EQUIVALENCE_TABLE_WRITE_REQUEST_MSG_SIZE	\
	(sizeof(struct pme_pmtcc_write_request_msg_t) -	\
	sizeof(union pme_table_entry_t) +		\
	PME_EQUIVALENCE_WRITE_ENTRY_SIZE)

#define PME_SPECIAL_TABLE_WRITE_REQUEST_MSG_SIZE	\
	(sizeof(struct pme_pmtcc_write_request_msg_t) -	\
	sizeof(union pme_table_entry_t)		+	\
	PME_SPECIAL_TRIGGER_ENTRY_SIZE)

/*
 * Index 0..255, bools do indicated which errors are serious
 * 0x40, 0x41, 0x48, 0x49, 0x4c, 0x4e, 0x4f, 0x50, 0x51, 0x59, 0x5a, 0x5b,
 * 0x5c, 0x5d, 0x5f, 0x60, 0x80, 0xc0, 0xc1, 0xc2, 0xc4, 0xd2,
 * 0xd4, 0xd5, 0xd7, 0xd9, 0xda, 0xe0, 0xe7
 */
static u8 serious_error_vec[] = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01,
	0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
	0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

static enum qman_cb_dqrr_result cb_dqrr(struct qman_portal *portal,
			struct qman_fq *fq, const struct qm_dqrr_entry *dq)
{
	u8 status = (u8)pme_fd_res_status(&dq->fd);
	u8 flags = pme_fd_res_flags(&dq->fd);
	struct  pme_pwrmgmt_ctx *ctx = (struct pme_pwrmgmt_ctx *)fq;

	if (unlikely(flags & PME_STATUS_UNRELIABLE))
		pr_err("pme status error 0x%x\n", (u32)flags);
	else if (unlikely((serious_error_vec[status])))
		pr_err("pme error status 0x%x\n", (u32)status);
	memcpy(&ctx->result_fd, &dq->fd, sizeof(*&dq->fd));
	complete(&ctx->done);
	return qman_cb_dqrr_consume;
}

static void cb_fqs(__always_unused struct qman_portal *portal,
			__always_unused struct qman_fq *fq,
			const struct qm_mr_entry *mr)
{
	u8 verb = mr->verb & QM_MR_VERB_TYPE_MASK;
	if (verb == QM_MR_VERB_FQRNI)
		return;
	/* nothing else is supposed to occur */
	BUG();
}

static const struct qman_fq_cb pme_fq_base_out = {
	.dqrr = cb_dqrr,
	.fqs = cb_fqs
};

static const struct qman_fq_cb pme_fq_base_in = {
	.fqs = cb_fqs,
	.ern = NULL
};

static void pme_pwrmgmt_initfq(struct qm_mcc_initfq *initfq, u32 rfqid)
{
	struct pme_context_a *pme_a =
		(struct pme_context_a *)&initfq->fqd.context_a;
	struct pme_context_b *pme_b =
		(struct pme_context_b *)&initfq->fqd.context_b;

	initfq->we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_CONTEXTA |
				QM_INITFQ_WE_CONTEXTB;
	initfq->fqd.dest.channel = qm_channel_pme;
	initfq->fqd.dest.wq = 4;
	pme_a->mode = pme_mode_direct;
	pme_context_a_set64(pme_a, 0);
	pme_b->rfqid = rfqid;
}

static int pme_pwrmgmt_ctx_reconfigure_tx(struct pme_pwrmgmt_ctx *ctx)
{
	struct qm_mcc_initfq initfq;
	u32 flags = QMAN_INITFQ_FLAG_SCHED | QMAN_INITFQ_FLAG_LOCAL;
	int ret;

	memset(&initfq, 0, sizeof(initfq));
	initfq.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL;
	initfq.fqd.dest.wq = 4;
	initfq.fqd.fq_ctrl = 0; /* disable stashing */
	ret = qman_init_fq(&ctx->tx_fq, flags, &initfq);
	return ret;
}

static int pme_pwrmgmt_ctx_reconfigure_rx(struct pme_pwrmgmt_ctx *ctx)
{
	struct qm_mcc_initfq initfq;
	int ret;

	memset(&initfq, 0, sizeof(initfq));
	pme_pwrmgmt_initfq(&initfq, qman_fq_fqid(&ctx->tx_fq));
	ret = qman_init_fq(&ctx->rx_fq, 0, &initfq);
	return ret;
}

int pme_pwrmgmt_ctx_init(struct pme_pwrmgmt_ctx *ctx)
{
	int ret;

	ctx->tx_fq.cb = pme_fq_base_out;
	ctx->rx_fq.cb = pme_fq_base_in;

	/* Create tx (from pme point of view) frame queue */
	ret = qman_create_fq(0, QMAN_FQ_FLAG_TO_DCPORTAL |
			QMAN_FQ_FLAG_DYNAMIC_FQID | QMAN_FQ_FLAG_LOCKED,
			&ctx->rx_fq);
	if (ret)
		return ret;

	ret = qman_create_fq(0, QMAN_FQ_FLAG_NO_ENQUEUE |
			QMAN_FQ_FLAG_DYNAMIC_FQID | QMAN_FQ_FLAG_LOCKED,
			&ctx->tx_fq);
	if (ret)
		goto create_rx_failed;

	ret = pme_pwrmgmt_ctx_reconfigure_rx(ctx);
	if (ret)
		goto config_rx_failed;

	ret = pme_pwrmgmt_ctx_reconfigure_tx(ctx);
	if (ret)
		goto config_tx_failed;

	return 0;
config_tx_failed:
config_rx_failed:
	qman_destroy_fq(&ctx->rx_fq, 0);
create_rx_failed:
	qman_destroy_fq(&ctx->tx_fq, 0);
	return ret;
}

static void pme_pwrmgmt_ctx_finish(struct pme_pwrmgmt_ctx *ctx)
{
	u32 flags;
	int ret;

	ret = qman_retire_fq(&ctx->tx_fq, &flags);
	BUG_ON(ret);
	BUG_ON(flags & QMAN_FQ_STATE_BLOCKOOS);
	ret = qman_retire_fq(&ctx->rx_fq, &flags);
	BUG_ON(ret);
	BUG_ON(flags & QMAN_FQ_STATE_BLOCKOOS);
	ret = qman_oos_fq(&ctx->tx_fq);
	BUG_ON(ret);
	ret = qman_oos_fq(&ctx->rx_fq);
	BUG_ON(ret);
	qman_destroy_fq(&ctx->tx_fq, 0);
	qman_destroy_fq(&ctx->rx_fq, 0);
}

static int create_pwrmgmt_ctx(struct portal_backup_info *save_db)
{
	int ret;

	/* check to see if context already exists */
	if (save_db->ctx)
		return 0;

	save_db->ctx = kzalloc(sizeof(*save_db->ctx), GFP_KERNEL);
	if (!save_db->ctx)
		return -ENOMEM;

	init_completion(&save_db->ctx->done);
	ret = pme_pwrmgmt_ctx_init(save_db->ctx);
	if (ret) {
		pr_err("Error pme_pwrmgmt_ctx_init\n");
		goto error_free_mem;
	}
	return 0;

error_free_mem:
	kfree(save_db->ctx);
	save_db->ctx = NULL;
	return ret;
}

static int delete_pwrmgmt_ctx(struct portal_backup_info *save_db)
{
	if (!save_db->ctx)
		return 0;

	pme_pwrmgmt_ctx_finish(save_db->ctx);
	kfree(save_db->ctx);
	save_db->ctx = NULL;

	return 0;
}

/* Send a pmtcc pme frame */
static int pme_pwrmgmt_ctx_pmtcc(struct pme_pwrmgmt_ctx *ctx, u32 flags,
			struct qm_fd *fd)
{
	int ret;

	struct pme_cmd_pmtcc *pmtcc = (struct pme_cmd_pmtcc *)&fd->cmd;
	pmtcc->cmd = pme_cmd_pmtcc;

	ret = qman_enqueue(&ctx->rx_fq, fd, flags &
			(QMAN_ENQUEUE_FLAG_WAIT | QMAN_ENQUEUE_FLAG_WAIT_INT));

	return ret;
}

static int get_table_attributes(enum pme_pmtcc_table_id tbl_id,
	uint32_t pme_rev1, int *num_read_entries, int *num_write_entries,
	int *read_size, int *read_reply_size, int *write_size,
	int *read_entry_size, int *write_entry_size)
{
	*read_size = PME_TABLE_READ_REQUEST_MSG_SIZE;

	switch (tbl_id) {
	case PME_ONE_BYTE_TRIGGER_TBL:
		*num_read_entries = PME_ONE_BYTE_TRIGGER_ENTRY_NUM;
		*num_write_entries = PME_ONE_BYTE_TRIGGER_ENTRY_NUM;
		*read_reply_size = PME_ONE_BYTE_TABLE_READ_REPLY_MSG_SIZE;
		*write_size = PME_ONE_BYTE_TABLE_WRITE_REQUEST_MSG_SIZE;
		*read_entry_size = PME_ONE_BYTE_TRIGGER_ENTRY_SIZE;
		*write_entry_size = PME_ONE_BYTE_TRIGGER_ENTRY_SIZE;
		break;

	case PME_TWO_BYTE_TRIGGER_TBL:
		if (is_version(pme_rev1, 2, 0))
			*num_read_entries = PME_TWO_BYTE_TRIGGER_ENTRY_NUM_V2_0;
		else if (is_version(pme_rev1, 2, 1))
			*num_read_entries = PME_TWO_BYTE_TRIGGER_ENTRY_NUM_V2_1;
		else if (is_version(pme_rev1, 2, 2))
			*num_read_entries = PME_TWO_BYTE_TRIGGER_ENTRY_NUM_V2_2;
		else {
			pr_err("pme suspend: unsupported pme version %u\n",
				pme_rev1);
			return -EINVAL;
		}
		*num_write_entries = *num_read_entries;
		*read_reply_size = PME_TWO_BYTE_TABLE_READ_REPLY_MSG_SIZE;
		*write_size = PME_TWO_BYTE_TABLE_WRITE_REQUEST_MSG_SIZE;
		*read_entry_size = PME_TWO_BYTE_TRIGGER_ENTRY_SIZE;
		*write_entry_size = PME_TWO_BYTE_TRIGGER_ENTRY_SIZE;
		break;

	case PME_VARIABLE_TRIGGER_TBL:
		if (is_version(pme_rev1, 2, 0))
			*num_read_entries = PME_VARIABLE_TRIGGER_ENTRY_NUM_V2_0;
		else if (is_version(pme_rev1, 2, 1))
			*num_read_entries = PME_VARIABLE_TRIGGER_ENTRY_NUM_V2_1;
		else if (is_version(pme_rev1, 2, 2))
			*num_read_entries = PME_VARIABLE_TRIGGER_ENTRY_NUM_V2_2;
		else {
			pr_err("pme suspend: unsupported pme version %u\n",
				pme_rev1);
			return -EINVAL;
		}
		*num_write_entries = *num_read_entries;
		*read_reply_size = PME_VARIABLE_TABLE_READ_REPLY_MSG_SIZE;
		*write_size = PME_VARIABLE_TABLE_WRITE_REQUEST_MSG_SIZE;
		*read_entry_size = PME_VARIABLE_TRIGGER_ENTRY_SIZE;
		*write_entry_size = PME_VARIABLE_TRIGGER_ENTRY_SIZE;
		break;

	case PME_CONFIDENCE_TBL:
		if (is_version(pme_rev1, 2, 0))
			*num_read_entries = PME_CONFIDENCE_ENTRY_NUM_V2_0;
		else if (is_version(pme_rev1, 2, 1))
			*num_read_entries = PME_CONFIDENCE_ENTRY_NUM_V2_1;
		else if (is_version(pme_rev1, 2, 2))
			*num_read_entries = PME_CONFIDENCE_ENTRY_NUM_V2_2;
		else {
			pr_err("pme suspend: unsupported pme version %u\n",
				pme_rev1);
			return -EINVAL;
		}
		*num_write_entries = *num_read_entries;
		*read_reply_size = PME_CONFIDENCE_TABLE_READ_REPLY_MSG_SIZE;
		*write_size = PME_CONFIDENCE_TABLE_WRITE_REQUEST_MSG_SIZE;
		*read_entry_size = PME_CONFIDENCE_ENTRY_SIZE;
		*write_entry_size = PME_CONFIDENCE_ENTRY_SIZE;
		break;
	case PME_UDG_TBL:
		*num_read_entries = 256;
		*num_write_entries = PME_USER_DEFINED_GROUP_ENTRY_NUM;
		*read_reply_size = PME_UDG_TABLE_READ_REPLY_MSG_SIZE;
		*write_size = PME_UDG_TABLE_WRITE_REQUEST_MSG_SIZE;
		*read_entry_size = (PME_UDG_TABLE_READ_REPLY_MSG_SIZE -
				   PME_TABLE_READ_REQUEST_MSG_SIZE);
		*write_entry_size = PME_USER_DEFINED_GROUP_WRITE_ENTRY_SIZE;
		break;

	case PME_EQUIVALENT_BYTE_TBL:
		*num_read_entries = 256;
		*num_write_entries = PME_EQUIVALENCE_ENTRY_NUM;
		*read_reply_size = PME_EQUIVALENCE_TABLE_READ_REPLY_MSG_SIZE;
		*write_size = PME_EQUIVALENCE_TABLE_WRITE_REQUEST_MSG_SIZE;
		*read_entry_size = (PME_EQUIVALENCE_TABLE_READ_REPLY_MSG_SIZE -
			PME_TABLE_READ_REQUEST_MSG_SIZE);
		*write_entry_size = PME_EQUIVALENCE_WRITE_ENTRY_SIZE;
		break;

	case PME_SPECIAL_TRIGGER_TBL:
		*num_read_entries = PME_SPECIAL_TRIGGER_ENTRY_NUM;
		*num_write_entries = PME_SPECIAL_TRIGGER_ENTRY_NUM;
		*read_reply_size = PME_SPECIAL_TABLE_READ_REPLY_MSG_SIZE;
		*write_size = PME_SPECIAL_TABLE_WRITE_REQUEST_MSG_SIZE;
		*read_entry_size = PME_SPECIAL_TRIGGER_ENTRY_SIZE;
		*write_entry_size = PME_SPECIAL_TRIGGER_ENTRY_SIZE;
		break;
	}
	return 0;
}

#ifdef PME_SUSPEND_DEBUG
static int total_size_read_request(enum pme_pmtcc_table_id tbl_id,
	uint32_t pme_rev1)
{
	int ret, num_read_entries, read_size, read_reply_size, write_size,
		read_entry_size, write_entry_size, num_write_entries;

	ret = get_table_attributes(tbl_id, pme_rev1, &num_read_entries,
		&num_write_entries, &read_size, &read_reply_size, &write_size,
		&read_entry_size, &write_entry_size);

	if (ret)
		return ret;

	return num_read_entries * read_size;
}

static int total_size_read_response_request(enum pme_pmtcc_table_id tbl_id,
	uint32_t pme_rev1)
{
	int ret, num_read_entries, read_size, read_reply_size, write_size,
		read_entry_size, write_entry_size, num_write_entries;

	ret = get_table_attributes(tbl_id, pme_rev1, &num_read_entries,
		&num_write_entries, &read_size, &read_reply_size, &write_size,
		&read_entry_size, &write_entry_size);

	if (ret)
		return ret;

	return num_read_entries * read_reply_size;
}

static int total_size_write_request(enum pme_pmtcc_table_id tbl_id,
	uint32_t pme_rev1)
{
	int ret, num_read_entries, read_size, read_reply_size, write_size,
		read_entry_size, write_entry_size, num_write_entries;

	ret = get_table_attributes(tbl_id, pme_rev1, &num_read_entries,
		&num_write_entries, &read_size, &read_reply_size, &write_size,
		&read_entry_size, &write_entry_size);

	if (ret)
		return ret;

	return num_write_entries * write_entry_size;
}
#endif

static int sizeof_all_db_tables(uint32_t pme_rev1)
{
	enum pme_pmtcc_table_id tbl_id;
	int i, ret, size = 0;

	for (i = 0; i < ARRAY_SIZE(table_list); i++) {
		int num_read_entries, read_size, read_reply_size, write_size,
			read_entry_size, write_entry_size, num_write_entries;
		tbl_id = table_list[i];

		ret = get_table_attributes(tbl_id, pme_rev1, &num_read_entries,
			&num_write_entries, &read_size, &read_reply_size,
			&write_size, &read_entry_size, &write_entry_size);

		if (ret)
			return ret;
		size += (write_entry_size * num_write_entries);
	}
	return size;
}

#ifdef PME_SUSPEND_DEBUG
static void print_debug(uint32_t pme_rev1)
{
	int i = 0;

	pr_info("size of db is %d\n", sizeof_all_db_tables(pme_rev1));

	do {
		int num_read_entries, read_size, read_reply_size, write_size,
			num_write_entries, read_entry_size, write_entry_size;

		get_table_attributes(table_list[i], pme_rev1, &num_read_entries,
			&num_write_entries, &read_size, &read_reply_size,
			&write_size, &read_entry_size, &write_entry_size);

		pr_info("Table Id %d\n", table_list[i]);
		pr_info(" num_read_entries %d, r_sz %d, rr_sz %d, w_sz %d\n",
			num_read_entries, read_size, read_reply_size,
			write_size);
		pr_info(" num_wr_entries %d, r_entry_size %d w_entry_size %d\n",
			num_write_entries, read_entry_size, write_entry_size);
		pr_info(" total read request size %d\n",
			total_size_read_request(table_list[i], pme_rev1));
		pr_info(" total read reply request size %d\n",
			total_size_read_response_request(table_list[i],
				pme_rev1));
		pr_info(" total write request size %d\n",
			total_size_write_request(table_list[i], pme_rev1));

		if (table_list[i] == PME_LAST_TABLE)
			break;
		i++;
	} while (1);
}
#endif

static void free_databases(struct portal_backup_info *save_db)
{
	vfree(save_db->db.alldb);
	save_db->db.alldb = NULL;
}

static int alloc_databases(struct pme2_private_data *priv_data)
{
	int sizedb;

	sizedb = sizeof_all_db_tables(priv_data->pme_rev1);
	if (sizedb < 0) {
		pr_err("Error getting db size\n");
		return -EINVAL;
	}

	priv_data->save_db.db.alldb = vzalloc(sizedb);
	if (!priv_data->save_db.db.alldb)
		return -ENOMEM;

	return 0;
}

static int save_all_tables(struct portal_backup_info *save_db,
			   uint32_t pme_rev1)
{
	struct pmtcc_raw_db *db = &save_db->db;
	enum pme_pmtcc_table_id tbl_id;
	int i, ret;
	uint8_t *current_tbl = db->alldb;

	for (i = 0; i < ARRAY_SIZE(table_list); i++) {
		int num_read_entries, read_size, read_reply_size, write_size,
			read_entry_size, write_entry_size, num_write_entries;
		int idx;
		struct pme_pmtcc_read_request_msg_t *entry;
		struct qm_fd fd;
		struct qm_sg_entry *sg_table = NULL;
		uint8_t *input_data, *output_data;
		enum pme_status status;

		tbl_id = table_list[i];
		ret = get_table_attributes(tbl_id, pme_rev1, &num_read_entries,
			&num_write_entries, &read_size, &read_reply_size,
			&write_size, &read_entry_size, &write_entry_size);

		/* Allocate input and output frame data */
		output_data = kzalloc(read_reply_size, GFP_KERNEL);
		input_data = kzalloc(read_size, GFP_KERNEL);
		sg_table = kzalloc(2 * sizeof(*sg_table), GFP_KERNEL);

		entry = (struct pme_pmtcc_read_request_msg_t *)
				input_data;
		entry->header.protocol_version = pme_rev1;
		entry->header.msg_length = read_size;
		entry->table_id = tbl_id;

		/* build fd */
		memset(&fd, 0, sizeof(fd));
		qm_sg_entry_set64(&sg_table[0], pme_suspend_map(save_db->pdev,
				output_data));
		sg_table[0].length = read_reply_size;
		qm_sg_entry_set64(&sg_table[1], pme_suspend_map(save_db->pdev,
				input_data));
		sg_table[1].length = read_size;
		sg_table[1].final = 1;
		fd.format = qm_fd_compound;
		qm_fd_addr_set64(&fd, pme_suspend_map(save_db->pdev, sg_table));
#ifdef PME_SUSPEND_DEBUG
		pr_info("Doing table %d\n", tbl_id);
#endif
		for (idx = 0; idx < num_read_entries; idx++) {
			entry->index = idx;
			memset(output_data, 0, read_reply_size);
			ret = pme_pwrmgmt_ctx_pmtcc(save_db->ctx,
				QMAN_ENQUEUE_FLAG_WAIT, &fd);

			if (ret)
				pr_err("error with pme_pwrmgmt_ctx_pmtcc\n");

			wait_for_completion(&save_db->ctx->done);

			status = pme_fd_res_status(&save_db->ctx->result_fd);
			if (status) {
				ret = -EINVAL;
				pr_err("PMTCC read status failed %d\n", status);
				save_db->backup_failed = 1;
				break;
			}
			if (pme_fd_res_flags(&save_db->ctx->result_fd) &
			    PME_STATUS_UNRELIABLE) {
				pr_err("pme %x\n", pme_fd_res_flags(
					&save_db->ctx->result_fd));
				ret = -EINVAL;
				save_db->backup_failed = 1;
				break;
			}
			/* copy the response */
			if (tbl_id == PME_UDG_TBL ||
			    tbl_id == PME_EQUIVALENT_BYTE_TBL) {
				/* Only copy over 8 lower bits to first byte */
				uint32_t tmp32;
				uint8_t tmp8;
				memcpy(&tmp32, output_data + read_size,
				       read_entry_size);
				tmp8 = (uint8_t)tmp32;
				memcpy(current_tbl + (idx * 1), &tmp8, 1);
			} else {
				memcpy(current_tbl + (idx * write_entry_size),
				       output_data + read_size,
				       write_entry_size);
			}
		}
		current_tbl += num_write_entries * write_entry_size;

		/* Free input and output frame data */
		kfree(output_data);
		kfree(input_data);
		kfree(sg_table);
		/* if failed, stop saving database */
		if (ret)
			break;
	}
	return ret;
}

/* don't need to write zero to PME sram since POR is all zero */
static int is_all_zero(uint8_t *buf, int size)
{
	int i;
	for (i = 0; i < size; i++) {
		if (buf[i] != 0)
			return 0;
	}
	return 1;
}

static int restore_all_tables(struct portal_backup_info *save_db,
			      uint32_t pme_rev1)
{
	struct pmtcc_raw_db *db = &save_db->db;
	enum pme_pmtcc_table_id tbl_id;
	int i, ret;
	uint8_t *current_tbl = db->alldb;

	for (i = 0; i < ARRAY_SIZE(table_list); i++) {
		int num_read_entries, read_size, read_reply_size, write_size,
			read_entry_size, write_entry_size, num_write_entries;
		int idx;
		struct pme_pmtcc_write_request_msg_t *entry;
		struct qm_fd fd;
		uint8_t *input_data;
		enum pme_status status;

		tbl_id = table_list[i];
		ret = get_table_attributes(tbl_id, pme_rev1, &num_read_entries,
			&num_write_entries, &read_size, &read_reply_size,
			&write_size, &read_entry_size, &write_entry_size);

		/* Allocate input frame data */
		input_data = kzalloc(write_size, GFP_KERNEL);

		entry = (struct pme_pmtcc_write_request_msg_t *)
				input_data;
		entry->header.protocol_version = pme_rev1;
		entry->header.msg_type = 0x01; /* write */
		entry->header.msg_length = write_size;
		entry->table_id = tbl_id;

		/* build fd */
		memset(&fd, 0, sizeof(fd));
		qm_fd_addr_set64(&fd, pme_suspend_map(save_db->pdev,
				input_data));
		fd.format = qm_fd_contig_big;
		fd.length29 = write_size;
#ifdef PME_SUSPEND_DEBUG
		pr_info("Doing table %d\n", tbl_id);
#endif
		for (idx = 0; idx < num_write_entries; idx++) {
			if (is_all_zero(current_tbl + (idx * write_entry_size),
					write_entry_size)) {
				continue;
			}
			entry->indexed_entry.index = idx;

			memcpy(input_data + (write_size - write_entry_size),
			       current_tbl + (idx * write_entry_size),
			       write_entry_size);

			ret = pme_pwrmgmt_ctx_pmtcc(save_db->ctx,
				QMAN_ENQUEUE_FLAG_WAIT, &fd);

			if (ret)
				pr_err("error with pmtcc\n");

			wait_for_completion(&save_db->ctx->done);

			status = pme_fd_res_status(&save_db->ctx->result_fd);
			if (status) {
				ret = -EINVAL;
				pr_err("PMTCC write status fail %d\n", status);
				break;
			}
			if (pme_fd_res_flags(&save_db->ctx->result_fd) &
			    PME_STATUS_UNRELIABLE) {
				pr_err("pme %x\n", pme_fd_res_flags(
					&save_db->ctx->result_fd));
				ret = -EINVAL;
				break;
			}
		}
		current_tbl += num_write_entries * write_entry_size;

		/* Free input and output frame data */
		kfree(input_data);
		/* if failed, stop restoring database */
		if (ret)
			break;
	}
	return ret;
}

int fsl_pme_save_db(struct pme2_private_data *priv_data)
{
	int ret;
	struct portal_backup_info *save_db = &priv_data->save_db;

#ifdef PME_SUSPEND_DEBUG
	print_debug(priv_data->pme_rev1);
#endif
	ret = save_all_tables(save_db, priv_data->pme_rev1);

	return ret;
}

static int is_pme_active(void)
{
	uint32_t val;
	int ret;

	ret = pme_attr_get(pme_attr_pmstat, &val);
	if (ret) {
		pr_err("Error reading activity bit\n");
		return ret;
	}
	return val;
}

static void reset_db_saved_state(struct portal_backup_info *db_info)
{
	db_info->backup_failed = 0;
}

/**
 * pme_suspend - power management suspend function
 *
 * @priv_data: pme2 device private data
 *
 * Saves the pme device volatile state prior to suspension.
 * CCSR space and SRAM state is saved to DDR
 */
int pme_suspend(struct pme2_private_data *priv_data)
{
	int ret;
	struct ccsr_backup_info *ccsr_info;
	struct portal_backup_info *db_info;

	ccsr_info = &priv_data->save_ccsr;
	db_info = &priv_data->save_db;

	reset_db_saved_state(db_info);

	pme_attr_get(pme_attr_faconf_en, &ccsr_info->save_faconf_en);
	pme_attr_get(pme_attr_cdcr, &ccsr_info->save_cdcr);

	/* disable pme */
	pme_attr_set(pme_attr_faconf_en, 0);
	/* disable caching, only SRE will be flushed. FC caching already off */
	pme_attr_set(pme_attr_cdcr, 0xffffffff);

	/* wait until device is not active */
	while (is_pme_active()) {
		cpu_relax();
		/* TODO: sanity check */
	}
#ifdef PME_SUSPEND_DEBUG
	pr_info("PME is quiescent\n");
#endif

	/* save CCSR space */
	save_all_ccsr(ccsr_info, priv_data->regs);

#ifdef PME_SUSPEND_DEBUG
	pr_info("First reg read is %u\n",
		ccsr_info->regdb.pmfa.faconf);
	pr_info("Last reg read is %u\n",
		ccsr_info->regdb.gen.pm_ip_rev_2);
#endif

	/* save sram, must first configure the new exclusive fq before
	 * enabling pme */
	ret = pme2_exclusive_set(&db_info->ctx->rx_fq);
	if (ret)
		pr_err("Error getting exclusive mode\n");

	/* save sram database, hook into pme_suspend. enable pme first */
	pme_attr_set(pme_attr_faconf_en, 1);
	ret = fsl_pme_save_db(priv_data);
	/* disable pme */
	pme_attr_set(pme_attr_faconf_en, 0);

	/* wait until device is not active */
	while (is_pme_active()) {
		cpu_relax();
		/* TODO: sanity check */
	}
#ifdef PME_SUSPEND_DEBUG
	pr_info("PME is quiescent\n");
#endif

	/* if saving db failed, reset internal state explicitly */
	if (db_info->backup_failed) {
		/* set the PME reset bit */
		pme_attr_set(pme_attr_faconf_rst, 1);
		/* clear the PME reset bit */
		pme_attr_set(pme_attr_faconf_rst, 0);
		/* wait until device is not active */
		while (is_pme_active()) {
			cpu_relax();
			/* TODO: sanity check */
		}
	}
	return 0;
}

/**
 * pme_resume - power management resume function
 *
 * @priv_data: pme2 device private data
 *
 * Restores the pme device to its original state prior to suspension.
 * CCSR space and SRAM state is restored
 */
int pme_resume(struct pme2_private_data *priv_data)
{
	int ret;
	struct ccsr_backup_info *ccsr_info;
	struct portal_backup_info *db_info;
	int db_restore_failed = 0;

	ccsr_info = &priv_data->save_ccsr;
	db_info = &priv_data->save_db;

#ifdef PME_SUSPEND_DEBUG
	pr_info("fsl_pme_restore_db\n");
	print_debug(priv_data->pme_rev1);
#endif

	/* when PME was saved, it was disabled. Therefore it will remain */
	restore_all_ccsr(ccsr_info, priv_data->regs);
	/* restore caching state */
	pme_attr_set(pme_attr_cdcr, ccsr_info->save_cdcr);

	/* Don't restore database if it wasn't saved properly */
	if (db_info->backup_failed)
		goto skip_db_restore;
	/* set private exclusive mode before enabling pme */
	/* save sram, must first configure the new exclusive fq before
	 * enabling pme */
	ret = pme2_exclusive_set(&db_info->ctx->rx_fq);
	if (ret)
		pr_err("Error getting exclusive mode\n");

	/* save sram database, hook into pme_suspend. enable pme first */
	pme_attr_set(pme_attr_faconf_en, 1);

	ret = restore_all_tables(db_info, priv_data->pme_rev1);
	if (ret)
		db_restore_failed = 1;

	/* disable pme */
	pme_attr_set(pme_attr_faconf_en, 0);
	/* wait until device is not active */
	while (is_pme_active()) {
		cpu_relax();
		/* TODO: sanity check */
	}
	if (db_restore_failed) {
		/* set the PME reset bit */
		pme_attr_set(pme_attr_faconf_rst, 1);
		/* clear the PME reset bit */
		pme_attr_set(pme_attr_faconf_rst, 0);
		/* when PME was saved, it was disabled. Therefore it will
		 * remain disabled */
		restore_all_ccsr(ccsr_info, priv_data->regs);
		/* restore caching state */
		pme_attr_set(pme_attr_cdcr, ccsr_info->save_cdcr);
	}

	/* restore EFQC register */
	pme_attr_set(pme_attr_efqc, ccsr_info->regdb.pmfa.efqc);

skip_db_restore:
	/* restore pme enable state */
	pme_attr_set(pme_attr_faconf_en, ccsr_info->save_faconf_en);

	return 0;
}


/**
 * init_pme_suspend - initialize pme resources for power management
 *
 * @priv_data: pme2 device private data
 *
 * All resources required to suspend the PME device are allocated.
 * They include memory,frame queues, platform device
 */
int init_pme_suspend(struct pme2_private_data *priv_data)
{
	int ret;
	struct ccsr_backup_info *ccsr_info;
	struct portal_backup_info *db_info;

	ccsr_info = &priv_data->save_ccsr;
	db_info = &priv_data->save_db;

	db_info->pdev = platform_device_alloc("fsl_pme_suspend", -1);
	if (!db_info->pdev)
		goto failed_alloc_device;
	if (dma_set_mask(&db_info->pdev->dev, DMA_BIT_MASK(40)))
		goto failed_dma_mask;
	if (platform_device_add(db_info->pdev))
		goto failed_device_add;

	/* allocate frame queues */
	ret = create_pwrmgmt_ctx(db_info);
	if (ret)
		goto failed_create_pwrmgmt_ctx;

	ret = alloc_databases(priv_data);
	if (ret)
		goto failed_alloc_databases;

	return 0;

failed_alloc_databases:
	delete_pwrmgmt_ctx(db_info);
failed_create_pwrmgmt_ctx:
	platform_device_del(db_info->pdev);
failed_device_add:
failed_dma_mask:
	platform_device_put(db_info->pdev);
	db_info->pdev = NULL;
failed_alloc_device:
	return -ENOMEM;
}

/**
 * exit_pme_suspend - release pme resources for power management
 *
 * @priv_data: pme2 device private data
 *
 * All resources required to suspend the PME device are released.
 * They include memory,frame queues, platform device
 */
void exit_pme_suspend(struct pme2_private_data *priv_data)
{
	struct portal_backup_info *db_info;

	db_info = &priv_data->save_db;

	free_databases(db_info);
	delete_pwrmgmt_ctx(db_info);
	platform_device_del(db_info->pdev);
	platform_device_put(db_info->pdev);
	db_info->pdev = NULL;
}

#endif /* CONFIG_PM */