aboutsummaryrefslogtreecommitdiff
path: root/arch/powerpc/platforms/powermac/pfunc_core.c
blob: 85434231ae14d5658be4d6c122d8ab78d8f27ed1 (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
/*
 *
 * FIXME: Properly make this race free with refcounting etc...
 *
 * FIXME: LOCKING !!!
 */

#include <linux/init.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/spinlock.h>
#include <linux/module.h>
#include <linux/mutex.h>

#include <asm/semaphore.h>
#include <asm/prom.h>
#include <asm/pmac_pfunc.h>

/* Debug */
#define LOG_PARSE(fmt...)
#define LOG_ERROR(fmt...)	printk(fmt)
#define LOG_BLOB(t,b,c)

#undef DEBUG
#ifdef DEBUG
#define DBG(fmt...)		printk(fmt)
#else
#define DBG(fmt...)
#endif

/* Command numbers */
#define PMF_CMD_LIST			0
#define PMF_CMD_WRITE_GPIO		1
#define PMF_CMD_READ_GPIO		2
#define PMF_CMD_WRITE_REG32		3
#define PMF_CMD_READ_REG32		4
#define PMF_CMD_WRITE_REG16		5
#define PMF_CMD_READ_REG16		6
#define PMF_CMD_WRITE_REG8		7
#define PMF_CMD_READ_REG8		8
#define PMF_CMD_DELAY			9
#define PMF_CMD_WAIT_REG32		10
#define PMF_CMD_WAIT_REG16		11
#define PMF_CMD_WAIT_REG8		12
#define PMF_CMD_READ_I2C		13
#define PMF_CMD_WRITE_I2C		14
#define PMF_CMD_RMW_I2C			15
#define PMF_CMD_GEN_I2C			16
#define PMF_CMD_SHIFT_BYTES_RIGHT	17
#define PMF_CMD_SHIFT_BYTES_LEFT	18
#define PMF_CMD_READ_CFG		19
#define PMF_CMD_WRITE_CFG		20
#define PMF_CMD_RMW_CFG			21
#define PMF_CMD_READ_I2C_SUBADDR	22
#define PMF_CMD_WRITE_I2C_SUBADDR	23
#define PMF_CMD_SET_I2C_MODE		24
#define PMF_CMD_RMW_I2C_SUBADDR		25
#define PMF_CMD_READ_REG32_MASK_SHR_XOR	26
#define PMF_CMD_READ_REG16_MASK_SHR_XOR	27
#define PMF_CMD_READ_REG8_MASK_SHR_XOR	28
#define PMF_CMD_WRITE_REG32_SHL_MASK	29
#define PMF_CMD_WRITE_REG16_SHL_MASK	30
#define PMF_CMD_WRITE_REG8_SHL_MASK	31
#define PMF_CMD_MASK_AND_COMPARE	32
#define PMF_CMD_COUNT			33

/* This structure holds the state of the parser while walking through
 * a function definition
 */
struct pmf_cmd {
	const void		*cmdptr;
	const void		*cmdend;
	struct pmf_function	*func;
	void			*instdata;
	struct pmf_args		*args;
	int			error;
};

#if 0
/* Debug output */
static void print_blob(const char *title, const void *blob, int bytes)
{
	printk("%s", title);
	while(bytes--) {
		printk("%02x ", *((u8 *)blob));
		blob += 1;
	}
	printk("\n");
}
#endif

/*
 * Parser helpers
 */

static u32 pmf_next32(struct pmf_cmd *cmd)
{
	u32 value;
	if ((cmd->cmdend - cmd->cmdptr) < 4) {
		cmd->error = 1;
		return 0;
	}
	value = *((u32 *)cmd->cmdptr);
	cmd->cmdptr += 4;
	return value;
}

static const void* pmf_next_blob(struct pmf_cmd *cmd, int count)
{
	const void *value;
	if ((cmd->cmdend - cmd->cmdptr) < count) {
		cmd->error = 1;
		return NULL;
	}
	value = cmd->cmdptr;
	cmd->cmdptr += count;
	return value;
}

/*
 * Individual command parsers
 */

#define PMF_PARSE_CALL(name, cmd, handlers, p...) \
	do { \
		if (cmd->error) \
			return -ENXIO; \
		if (handlers == NULL) \
			return 0; \
		if (handlers->name)				      \
			return handlers->name(cmd->func, cmd->instdata, \
					      cmd->args, p);	      \
		return -1; \
	} while(0) \


static int pmf_parser_write_gpio(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u8 value = (u8)pmf_next32(cmd);
	u8 mask = (u8)pmf_next32(cmd);

	LOG_PARSE("pmf: write_gpio(value: %02x, mask: %02x)\n", value, mask);

	PMF_PARSE_CALL(write_gpio, cmd, h, value, mask);
}

static int pmf_parser_read_gpio(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u8 mask = (u8)pmf_next32(cmd);
	int rshift = (int)pmf_next32(cmd);
	u8 xor = (u8)pmf_next32(cmd);

	LOG_PARSE("pmf: read_gpio(mask: %02x, rshift: %d, xor: %02x)\n",
		  mask, rshift, xor);

	PMF_PARSE_CALL(read_gpio, cmd, h, mask, rshift, xor);
}

static int pmf_parser_write_reg32(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);
	u32 value = pmf_next32(cmd);
	u32 mask = pmf_next32(cmd);

	LOG_PARSE("pmf: write_reg32(offset: %08x, value: %08x, mask: %08x)\n",
		  offset, value, mask);

	PMF_PARSE_CALL(write_reg32, cmd, h, offset, value, mask);
}

static int pmf_parser_read_reg32(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);

	LOG_PARSE("pmf: read_reg32(offset: %08x)\n", offset);

	PMF_PARSE_CALL(read_reg32, cmd, h, offset);
}


static int pmf_parser_write_reg16(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);
	u16 value = (u16)pmf_next32(cmd);
	u16 mask = (u16)pmf_next32(cmd);

	LOG_PARSE("pmf: write_reg16(offset: %08x, value: %04x, mask: %04x)\n",
		  offset, value, mask);

	PMF_PARSE_CALL(write_reg16, cmd, h, offset, value, mask);
}

static int pmf_parser_read_reg16(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);

	LOG_PARSE("pmf: read_reg16(offset: %08x)\n", offset);

	PMF_PARSE_CALL(read_reg16, cmd, h, offset);
}


static int pmf_parser_write_reg8(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);
	u8 value = (u16)pmf_next32(cmd);
	u8 mask = (u16)pmf_next32(cmd);

	LOG_PARSE("pmf: write_reg8(offset: %08x, value: %02x, mask: %02x)\n",
		  offset, value, mask);

	PMF_PARSE_CALL(write_reg8, cmd, h, offset, value, mask);
}

static int pmf_parser_read_reg8(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);

	LOG_PARSE("pmf: read_reg8(offset: %08x)\n", offset);

	PMF_PARSE_CALL(read_reg8, cmd, h, offset);
}

static int pmf_parser_delay(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 duration = pmf_next32(cmd);

	LOG_PARSE("pmf: delay(duration: %d us)\n", duration);

	PMF_PARSE_CALL(delay, cmd, h, duration);
}

static int pmf_parser_wait_reg32(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);
	u32 value = pmf_next32(cmd);
	u32 mask = pmf_next32(cmd);

	LOG_PARSE("pmf: wait_reg32(offset: %08x, comp_value: %08x,mask: %08x)\n",
		  offset, value, mask);

	PMF_PARSE_CALL(wait_reg32, cmd, h, offset, value, mask);
}

static int pmf_parser_wait_reg16(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);
	u16 value = (u16)pmf_next32(cmd);
	u16 mask = (u16)pmf_next32(cmd);

	LOG_PARSE("pmf: wait_reg16(offset: %08x, comp_value: %04x,mask: %04x)\n",
		  offset, value, mask);

	PMF_PARSE_CALL(wait_reg16, cmd, h, offset, value, mask);
}

static int pmf_parser_wait_reg8(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);
	u8 value = (u8)pmf_next32(cmd);
	u8 mask = (u8)pmf_next32(cmd);

	LOG_PARSE("pmf: wait_reg8(offset: %08x, comp_value: %02x,mask: %02x)\n",
		  offset, value, mask);

	PMF_PARSE_CALL(wait_reg8, cmd, h, offset, value, mask);
}

static int pmf_parser_read_i2c(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 bytes = pmf_next32(cmd);

	LOG_PARSE("pmf: read_i2c(bytes: %ud)\n", bytes);

	PMF_PARSE_CALL(read_i2c, cmd, h, bytes);
}

static int pmf_parser_write_i2c(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 bytes = pmf_next32(cmd);
	const void *blob = pmf_next_blob(cmd, bytes);

	LOG_PARSE("pmf: write_i2c(bytes: %ud) ...\n", bytes);
	LOG_BLOB("pmf:   data: \n", blob, bytes);

	PMF_PARSE_CALL(write_i2c, cmd, h, bytes, blob);
}


static int pmf_parser_rmw_i2c(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 maskbytes = pmf_next32(cmd);
	u32 valuesbytes = pmf_next32(cmd);
	u32 totalbytes = pmf_next32(cmd);
	const void *maskblob = pmf_next_blob(cmd, maskbytes);
	const void *valuesblob = pmf_next_blob(cmd, valuesbytes);

	LOG_PARSE("pmf: rmw_i2c(maskbytes: %ud, valuebytes: %ud, "
		  "totalbytes: %d) ...\n",
		  maskbytes, valuesbytes, totalbytes);
	LOG_BLOB("pmf:   mask data: \n", maskblob, maskbytes);
	LOG_BLOB("pmf:   values data: \n", valuesblob, valuesbytes);

	PMF_PARSE_CALL(rmw_i2c, cmd, h, maskbytes, valuesbytes, totalbytes,
		       maskblob, valuesblob);
}

static int pmf_parser_read_cfg(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);
	u32 bytes = pmf_next32(cmd);

	LOG_PARSE("pmf: read_cfg(offset: %x, bytes: %ud)\n", offset, bytes);

	PMF_PARSE_CALL(read_cfg, cmd, h, offset, bytes);
}


static int pmf_parser_write_cfg(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);
	u32 bytes = pmf_next32(cmd);
	const void *blob = pmf_next_blob(cmd, bytes);

	LOG_PARSE("pmf: write_cfg(offset: %x, bytes: %ud)\n", offset, bytes);
	LOG_BLOB("pmf:   data: \n", blob, bytes);

	PMF_PARSE_CALL(write_cfg, cmd, h, offset, bytes, blob);
}

static int pmf_parser_rmw_cfg(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);
	u32 maskbytes = pmf_next32(cmd);
	u32 valuesbytes = pmf_next32(cmd);
	u32 totalbytes = pmf_next32(cmd);
	const void *maskblob = pmf_next_blob(cmd, maskbytes);
	const void *valuesblob = pmf_next_blob(cmd, valuesbytes);

	LOG_PARSE("pmf: rmw_cfg(maskbytes: %ud, valuebytes: %ud,"
		  " totalbytes: %d) ...\n",
		  maskbytes, valuesbytes, totalbytes);
	LOG_BLOB("pmf:   mask data: \n", maskblob, maskbytes);
	LOG_BLOB("pmf:   values data: \n", valuesblob, valuesbytes);

	PMF_PARSE_CALL(rmw_cfg, cmd, h, offset, maskbytes, valuesbytes,
		       totalbytes, maskblob, valuesblob);
}


static int pmf_parser_read_i2c_sub(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u8 subaddr = (u8)pmf_next32(cmd);
	u32 bytes = pmf_next32(cmd);

	LOG_PARSE("pmf: read_i2c_sub(subaddr: %x, bytes: %ud)\n",
		  subaddr, bytes);

	PMF_PARSE_CALL(read_i2c_sub, cmd, h, subaddr, bytes);
}

static int pmf_parser_write_i2c_sub(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u8 subaddr = (u8)pmf_next32(cmd);
	u32 bytes = pmf_next32(cmd);
	const void *blob = pmf_next_blob(cmd, bytes);

	LOG_PARSE("pmf: write_i2c_sub(subaddr: %x, bytes: %ud) ...\n",
		  subaddr, bytes);
	LOG_BLOB("pmf:   data: \n", blob, bytes);

	PMF_PARSE_CALL(write_i2c_sub, cmd, h, subaddr, bytes, blob);
}

static int pmf_parser_set_i2c_mode(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u32 mode = pmf_next32(cmd);

	LOG_PARSE("pmf: set_i2c_mode(mode: %d)\n", mode);

	PMF_PARSE_CALL(set_i2c_mode, cmd, h, mode);
}


static int pmf_parser_rmw_i2c_sub(struct pmf_cmd *cmd, struct pmf_handlers *h)
{
	u8 subaddr = (u8)pmf_next32(cmd);
	u32 maskbytes = pmf_next32(cmd);
	u32 valuesbytes = pmf_next32(cmd);
	u32 totalbytes = pmf_next32(cmd);
	const void *maskblob = pmf_next_blob(cmd, maskbytes);
	const void *valuesblob = pmf_next_blob(cmd, valuesbytes);

	LOG_PARSE("pmf: rmw_i2c_sub(subaddr: %x, maskbytes: %ud, valuebytes: %ud"
		  ", totalbytes: %d) ...\n",
		  subaddr, maskbytes, valuesbytes, totalbytes);
	LOG_BLOB("pmf:   mask data: \n", maskblob, maskbytes);
	LOG_BLOB("pmf:   values data: \n", valuesblob, valuesbytes);

	PMF_PARSE_CALL(rmw_i2c_sub, cmd, h, subaddr, maskbytes, valuesbytes,
		       totalbytes, maskblob, valuesblob);
}

static int pmf_parser_read_reg32_msrx(struct pmf_cmd *cmd,
				      struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);
	u32 mask = pmf_next32(cmd);
	u32 shift = pmf_next32(cmd);
	u32 xor = pmf_next32(cmd);

	LOG_PARSE("pmf: read_reg32_msrx(offset: %x, mask: %x, shift: %x,"
		  " xor: %x\n", offset, mask, shift, xor);

	PMF_PARSE_CALL(read_reg32_msrx, cmd, h, offset, mask, shift, xor);
}

static int pmf_parser_read_reg16_msrx(struct pmf_cmd *cmd,
				      struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);
	u32 mask = pmf_next32(cmd);
	u32 shift = pmf_next32(cmd);
	u32 xor = pmf_next32(cmd);

	LOG_PARSE("pmf: read_reg16_msrx(offset: %x, mask: %x, shift: %x,"
		  " xor: %x\n", offset, mask, shift, xor);

	PMF_PARSE_CALL(read_reg16_msrx, cmd, h, offset, mask, shift, xor);
}
static int pmf_parser_read_reg8_msrx(struct pmf_cmd *cmd,
				     struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);
	u32 mask = pmf_next32(cmd);
	u32 shift = pmf_next32(cmd);
	u32 xor = pmf_next32(cmd);

	LOG_PARSE("pmf: read_reg8_msrx(offset: %x, mask: %x, shift: %x,"
		  " xor: %x\n", offset, mask, shift, xor);

	PMF_PARSE_CALL(read_reg8_msrx, cmd, h, offset, mask, shift, xor);
}

static int pmf_parser_write_reg32_slm(struct pmf_cmd *cmd,
				      struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);
	u32 shift = pmf_next32(cmd);
	u32 mask = pmf_next32(cmd);

	LOG_PARSE("pmf: write_reg32_slm(offset: %x, shift: %x, mask: %x\n",
		  offset, shift, mask);

	PMF_PARSE_CALL(write_reg32_slm, cmd, h, offset, shift, mask);
}

static int pmf_parser_write_reg16_slm(struct pmf_cmd *cmd,
				      struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);
	u32 shift = pmf_next32(cmd);
	u32 mask = pmf_next32(cmd);

	LOG_PARSE("pmf: write_reg16_slm(offset: %x, shift: %x, mask: %x\n",
		  offset, shift, mask);

	PMF_PARSE_CALL(write_reg16_slm, cmd, h, offset, shift, mask);
}

static int pmf_parser_write_reg8_slm(struct pmf_cmd *cmd,
				     struct pmf_handlers *h)
{
	u32 offset = pmf_next32(cmd);
	u32 shift = pmf_next32(cmd);
	u32 mask = pmf_next32(cmd);

	LOG_PARSE("pmf: write_reg8_slm(offset: %x, shift: %x, mask: %x\n",
		  offset, shift, mask);

	PMF_PARSE_CALL(write_reg8_slm, cmd, h, offset, shift, mask);
}

static int pmf_parser_mask_and_compare(struct pmf_cmd *cmd,
				       struct pmf_handlers *h)
{
	u32 bytes = pmf_next32(cmd);
	const void *maskblob = pmf_next_blob(cmd, bytes);
	const void *valuesblob = pmf_next_blob(cmd, bytes);

	LOG_PARSE("pmf: mask_and_compare(length: %ud ...\n", bytes);
	LOG_BLOB("pmf:   mask data: \n", maskblob, bytes);
	LOG_BLOB("pmf:   values data: \n", valuesblob, bytes);

	PMF_PARSE_CALL(mask_and_compare, cmd, h,
		       bytes, maskblob, valuesblob);
}


typedef int (*pmf_cmd_parser_t)(struct pmf_cmd *cmd, struct pmf_handlers *h);

static pmf_cmd_parser_t pmf_parsers[PMF_CMD_COUNT] =
{
	NULL,
	pmf_parser_write_gpio,
	pmf_parser_read_gpio,
	pmf_parser_write_reg32,
	pmf_parser_read_reg32,
	pmf_parser_write_reg16,
	pmf_parser_read_reg16,
	pmf_parser_write_reg8,
	pmf_parser_read_reg8,
	pmf_parser_delay,
	pmf_parser_wait_reg32,
	pmf_parser_wait_reg16,
	pmf_parser_wait_reg8,
	pmf_parser_read_i2c,
	pmf_parser_write_i2c,
	pmf_parser_rmw_i2c,
	NULL, /* Bogus command */
	NULL, /* Shift bytes right: NYI */
	NULL, /* Shift bytes left: NYI */
	pmf_parser_read_cfg,
	pmf_parser_write_cfg,
	pmf_parser_rmw_cfg,
	pmf_parser_read_i2c_sub,
	pmf_parser_write_i2c_sub,
	pmf_parser_set_i2c_mode,
	pmf_parser_rmw_i2c_sub,
	pmf_parser_read_reg32_msrx,
	pmf_parser_read_reg16_msrx,
	pmf_parser_read_reg8_msrx,
	pmf_parser_write_reg32_slm,
	pmf_parser_write_reg16_slm,
	pmf_parser_write_reg8_slm,
	pmf_parser_mask_and_compare,
};

struct pmf_device {
	struct list_head	link;
	struct device_node	*node;
	struct pmf_handlers	*handlers;
	struct list_head	functions;
	struct kref		ref;
};

static LIST_HEAD(pmf_devices);
static DEFINE_SPINLOCK(pmf_lock);
static DEFINE_MUTEX(pmf_irq_mutex);

static void pmf_release_device(struct kref *kref)
{
	struct pmf_device *dev = container_of(kref, struct pmf_device, ref);
	kfree(dev);
}

static inline void pmf_put_device(struct pmf_device *dev)
{
	kref_put(&dev->ref, pmf_release_device);
}

static inline struct pmf_device *pmf_get_device(struct pmf_device *dev)
{
	kref_get(&dev->ref);
	return dev;
}

static inline struct pmf_device *pmf_find_device(struct device_node *np)
{
	struct pmf_device *dev;

	list_for_each_entry(dev, &pmf_devices, link) {
		if (dev->node == np)
			return pmf_get_device(dev);
	}
	return NULL;
}

static int pmf_parse_one(struct pmf_function *func,
			 struct pmf_handlers *handlers,
			 void *instdata, struct pmf_args *args)
{
	struct pmf_cmd cmd;
	u32 ccode;
	int count, rc;

	cmd.cmdptr		= func->data;
	cmd.cmdend		= func->data + func->length;
	cmd.func       		= func;
	cmd.instdata		= instdata;
	cmd.args		= args;
	cmd.error		= 0;

	LOG_PARSE("pmf: func %s, %d bytes, %s...\n",
		  func->name, func->length,
		  handlers ? "executing" : "parsing");

	/* One subcommand to parse for now */
	count = 1;

	while(count-- && cmd.cmdptr < cmd.cmdend) {
		/* Get opcode */
		ccode = pmf_next32(&cmd);
		/* Check if we are hitting a command list, fetch new count */
		if (ccode == 0) {
			count = pmf_next32(&cmd) - 1;
			ccode = pmf_next32(&cmd);
		}
		if (cmd.error) {
			LOG_ERROR("pmf: parse error, not enough data\n");
			return -ENXIO;
		}
		if (ccode >= PMF_CMD_COUNT) {
			LOG_ERROR("pmf: command code %d unknown !\n", ccode);
			return -ENXIO;
		}
		if (pmf_parsers[ccode] == NULL) {
			LOG_ERROR("pmf: no parser for command %d !\n", ccode);
			return -ENXIO;
		}
		rc = pmf_parsers[ccode](&cmd, handlers);
		if (rc != 0) {
			LOG_ERROR("pmf: parser for command %d returned"
				  " error %d\n", ccode, rc);
			return rc;
		}
	}

	/* We are doing an initial parse pass, we need to adjust the size */
	if (handlers == NULL)
		func->length = cmd.cmdptr - func->data;

	return 0;
}

static int pmf_add_function_prop(struct pmf_device *dev, void *driverdata,
				 const char *name, u32 *data,
				 unsigned int length)
{
	int count = 0;
	struct pmf_function *func = NULL;

	DBG("pmf: Adding functions for platform-do-%s\n", name);

	while (length >= 12) {
		/* Allocate a structure */
		func = kzalloc(sizeof(struct pmf_function), GFP_KERNEL);
		if (func == NULL)
			goto bail;
		kref_init(&func->ref);
		INIT_LIST_HEAD(&func->irq_clients);
		func->node = dev->node;
		func->driver_data = driverdata;
		func->name = name;
		func->phandle = data[0];
		func->flags = data[1];
		data += 2;
		length -= 8;
		func->data = data;
		func->length = length;
		func->dev = dev;
		DBG("pmf: idx %d: flags=%08x, phandle=%08x "
		    " %d bytes remaining, parsing...\n",
		    count+1, func->flags, func->phandle, length);
		if (pmf_parse_one(func, NULL, NULL, NULL)) {
			kfree(func);
			goto bail;
		}
		length -= func->length;
		data = (u32 *)(((u8 *)data) + func->length);
		list_add(&func->link, &dev->functions);
		pmf_get_device(dev);
		count++;
	}
 bail:
	DBG("pmf: Added %d functions\n", count);

	return count;
}

static int pmf_add_functions(struct pmf_device *dev, void *driverdata)
{
	struct property *pp;
#define PP_PREFIX "platform-do-"
	const int plen = strlen(PP_PREFIX);
	int count = 0;

	for (pp = dev->node->properties; pp != 0; pp = pp->next) {
		char *name;
		if (strncmp(pp->name, PP_PREFIX, plen) != 0)
			continue;
		name = pp->name + plen;
		if (strlen(name) && pp->length >= 12)
			count += pmf_add_function_prop(dev, driverdata, name,
						       pp->value, pp->length);
	}
	return count;
}


int pmf_register_driver(struct device_node *np,
			struct pmf_handlers *handlers,
			void *driverdata)
{
	struct pmf_device *dev;
	unsigned long flags;
	int rc = 0;

	if (handlers == NULL)
		return -EINVAL;

	DBG("pmf: registering driver for node %s\n", np->full_name);

	spin_lock_irqsave(&pmf_lock, flags);
	dev = pmf_find_device(np);
	spin_unlock_irqrestore(&pmf_lock, flags);
	if (dev != NULL) {
		DBG("pmf: already there !\n");
		pmf_put_device(dev);
		return -EBUSY;
	}

	dev = kzalloc(sizeof(struct pmf_device), GFP_KERNEL);
	if (dev == NULL) {
		DBG("pmf: no memory !\n");
		return -ENOMEM;
	}
	kref_init(&dev->ref);
	dev->node = of_node_get(np);
	dev->handlers = handlers;
	INIT_LIST_HEAD(&dev->functions);

	rc = pmf_add_functions(dev, driverdata);
	if (rc == 0) {
		DBG("pmf: no functions, disposing.. \n");
		of_node_put(np);
		kfree(dev);
		return -ENODEV;
	}

	spin_lock_irqsave(&pmf_lock, flags);
	list_add(&dev->link, &pmf_devices);
	spin_unlock_irqrestore(&pmf_lock, flags);

	return 0;
}
EXPORT_SYMBOL_GPL(pmf_register_driver);

struct pmf_function *pmf_get_function(struct pmf_function *func)
{
	if (!try_module_get(func->dev->handlers->owner))
		return NULL;
	kref_get(&func->ref);
	return func;
}
EXPORT_SYMBOL_GPL(pmf_get_function);

static void pmf_release_function(struct kref *kref)
{
	struct pmf_function *func =
		container_of(kref, struct pmf_function, ref);
	pmf_put_device(func->dev);
	kfree(func);
}

static inline void __pmf_put_function(struct pmf_function *func)
{
	kref_put(&func->ref, pmf_release_function);
}

void pmf_put_function(struct pmf_function *func)
{
	if (func == NULL)
		return;
	module_put(func->dev->handlers->owner);
	__pmf_put_function(func);
}
EXPORT_SYMBOL_GPL(pmf_put_function);

void pmf_unregister_driver(struct device_node *np)
{
	struct pmf_device *dev;
	unsigned long flags;

	DBG("pmf: unregistering driver for node %s\n", np->full_name);

	spin_lock_irqsave(&pmf_lock, flags);
	dev = pmf_find_device(np);
	if (dev == NULL) {
		DBG("pmf: not such driver !\n");
		spin_unlock_irqrestore(&pmf_lock, flags);
		return;
	}
	list_del(&dev->link);

	while(!list_empty(&dev->functions)) {
		struct pmf_function *func =
			list_entry(dev->functions.next, typeof(*func), link);
		list_del(&func->link);
		__pmf_put_function(func);
	}

	pmf_put_device(dev);
	spin_unlock_irqrestore(&pmf_lock, flags);
}
EXPORT_SYMBOL_GPL(pmf_unregister_driver);

struct pmf_function *__pmf_find_function(struct device_node *target,
					 const char *name, u32 flags)
{
	struct device_node *actor = of_node_get(target);
	struct pmf_device *dev;
	struct pmf_function *func, *result = NULL;
	char fname[64];
	const u32 *prop;
	u32 ph;

	/*
	 * Look for a "platform-*" function reference. If we can't find
	 * one, then we fallback to a direct call attempt
	 */
	snprintf(fname, 63, "platform-%s", name);
	prop = of_get_property(target, fname, NULL);
	if (prop == NULL)
		goto find_it;
	ph = *prop;
	if (ph == 0)
		goto find_it;

	/*
	 * Ok, now try to find the actor. If we can't find it, we fail,
	 * there is no point in falling back there
	 */
	of_node_put(actor);
	actor = of_find_node_by_phandle(ph);
	if (actor == NULL)
		return NULL;
 find_it:
	dev = pmf_find_device(actor);
	if (dev == NULL)
		return NULL;

	list_for_each_entry(func, &dev->functions, link) {
		if (name && strcmp(name, func->name))
			continue;
		if (func->phandle && target->node != func->phandle)
			continue;
		if ((func->flags & flags) == 0)
			continue;
		result = func;
		break;
	}
	of_node_put(actor);
	pmf_put_device(dev);
	return result;
}


int pmf_register_irq_client(struct device_node *target,
			    const char *name,
			    struct pmf_irq_client *client)
{
	struct pmf_function *func;
	unsigned long flags;

	spin_lock_irqsave(&pmf_lock, flags);
	func = __pmf_find_function(target, name, PMF_FLAGS_INT_GEN);
	if (func)
		func = pmf_get_function(func);
	spin_unlock_irqrestore(&pmf_lock, flags);
	if (func == NULL)
		return -ENODEV;

	/* guard against manipulations of list */
	mutex_lock(&pmf_irq_mutex);
	if (list_empty(&func->irq_clients))
		func->dev->handlers->irq_enable(func);

	/* guard against pmf_do_irq while changing list */
	spin_lock_irqsave(&pmf_lock, flags);
	list_add(&client->link, &func->irq_clients);
	spin_unlock_irqrestore(&pmf_lock, flags);

	client->func = func;
	mutex_unlock(&pmf_irq_mutex);

	return 0;
}
EXPORT_SYMBOL_GPL(pmf_register_irq_client);

void pmf_unregister_irq_client(struct pmf_irq_client *client)
{
	struct pmf_function *func = client->func;
	unsigned long flags;

	BUG_ON(func == NULL);

	/* guard against manipulations of list */
	mutex_lock(&pmf_irq_mutex);
	client->func = NULL;

	/* guard against pmf_do_irq while changing list */
	spin_lock_irqsave(&pmf_lock, flags);
	list_del(&client->link);
	spin_unlock_irqrestore(&pmf_lock, flags);

	if (list_empty(&func->irq_clients))
		func->dev->handlers->irq_disable(func);
	mutex_unlock(&pmf_irq_mutex);
	pmf_put_function(func);
}
EXPORT_SYMBOL_GPL(pmf_unregister_irq_client);


void pmf_do_irq(struct pmf_function *func)
{
	unsigned long flags;
	struct pmf_irq_client *client;

	/* For now, using a spinlock over the whole function. Can be made
	 * to drop the lock using 2 lists if necessary
	 */
	spin_lock_irqsave(&pmf_lock, flags);
	list_for_each_entry(client, &func->irq_clients, link) {
		if (!try_module_get(client->owner))
			continue;
		client->handler(client->data);
		module_put(client->owner);
	}
	spin_unlock_irqrestore(&pmf_lock, flags);
}
EXPORT_SYMBOL_GPL(pmf_do_irq);


int pmf_call_one(struct pmf_function *func, struct pmf_args *args)
{
	struct pmf_device *dev = func->dev;
	void *instdata = NULL;
	int rc = 0;

	DBG(" ** pmf_call_one(%s/%s) **\n", dev->node->full_name, func->name);

	if (dev->handlers->begin)
		instdata = dev->handlers->begin(func, args);
	rc = pmf_parse_one(func, dev->handlers, instdata, args);
	if (dev->handlers->end)
		dev->handlers->end(func, instdata);

	return rc;
}
EXPORT_SYMBOL_GPL(pmf_call_one);

int pmf_do_functions(struct device_node *np, const char *name,
		     u32 phandle, u32 fflags, struct pmf_args *args)
{
	struct pmf_device *dev;
	struct pmf_function *func, *tmp;
	unsigned long flags;
	int rc = -ENODEV;

	spin_lock_irqsave(&pmf_lock, flags);

	dev = pmf_find_device(np);
	if (dev == NULL) {
		spin_unlock_irqrestore(&pmf_lock, flags);
		return -ENODEV;
	}
	list_for_each_entry_safe(func, tmp, &dev->functions, link) {
		if (name && strcmp(name, func->name))
			continue;
		if (phandle && func->phandle && phandle != func->phandle)
			continue;
		if ((func->flags & fflags) == 0)
			continue;
		if (pmf_get_function(func) == NULL)
			continue;
		spin_unlock_irqrestore(&pmf_lock, flags);
		rc = pmf_call_one(func, args);
		pmf_put_function(func);
		spin_lock_irqsave(&pmf_lock, flags);
	}
	pmf_put_device(dev);
	spin_unlock_irqrestore(&pmf_lock, flags);

	return rc;
}
EXPORT_SYMBOL_GPL(pmf_do_functions);


struct pmf_function *pmf_find_function(struct device_node *target,
				       const char *name)
{
	struct pmf_function *func;
	unsigned long flags;

	spin_lock_irqsave(&pmf_lock, flags);
	func = __pmf_find_function(target, name, PMF_FLAGS_ON_DEMAND);
	if (func)
		func = pmf_get_function(func);
	spin_unlock_irqrestore(&pmf_lock, flags);
	return func;
}
EXPORT_SYMBOL_GPL(pmf_find_function);

int pmf_call_function(struct device_node *target, const char *name,
		      struct pmf_args *args)
{
	struct pmf_function *func = pmf_find_function(target, name);
	int rc;

	if (func == NULL)
		return -ENODEV;

	rc = pmf_call_one(func, args);
	pmf_put_function(func);
	return rc;
}
EXPORT_SYMBOL_GPL(pmf_call_function);