summaryrefslogtreecommitdiff
path: root/drivers/fsi/fsi-core.c
blob: a65e93720096ce4aec979ace1e994fba960799f9 (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
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
/*
 * FSI core driver
 *
 * Copyright (C) IBM Corporation 2016
 *
 * 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.
 */

#include <linux/device.h>
#include <linux/fsi.h>
#include <linux/idr.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/delay.h>

#include "fsi-master.h"

#define DEBUG

#define FSI_N_SLAVES	4
#define FSI_BREAK	0xc0de0000

#define FSI_SLAVE_CONF_NEXT_MASK	0x80000000
#define FSI_SLAVE_CONF_SLOTS_MASK	0x00ff0000
#define FSI_SLAVE_CONF_SLOTS_SHIFT	16
#define FSI_SLAVE_CONF_VERSION_MASK	0x0000f000
#define FSI_SLAVE_CONF_VERSION_SHIFT	12
#define FSI_SLAVE_CONF_TYPE_MASK	0x00000ff0
#define FSI_SLAVE_CONF_TYPE_SHIFT	4
#define FSI_SLAVE_CONF_CRC_SHIFT	4
#define FSI_SLAVE_CONF_CRC_MASK		0x0000000f
#define FSI_SLAVE_CONF_DATA_BITS	28

#define FSI_PEEK_BASE			0x410
#define	FSI_SLAVE_BASE			0x800
#define	FSI_HUB_CONTROL			0x3400

#define	FSI_SLAVE_SMODE_DFLT		0xa0ff0100

#define FSI_IPOLL_PERIOD		msecs_to_jiffies(fsi_ipoll_period_ms)

#define	FSI_ENGID_HUB_MASTER		0x1c
#define	FSI_ENGID_HUB_LINK		0x1d
#define	FSI_HUB_LINK_OFFSET		0x80000
#define	FSI_MASTER_HUB_LINK_SIZE	0x80000
#define	FSI_HUB_MASTER_MAX_LINKS	8

#define	FSI_LINK_ENABLE_SETUP_TIME	10	/* in mS */

static const int engine_page_size = 0x400;
static struct task_struct *master_ipoll;
static unsigned int fsi_ipoll_period_ms = 100;

static DEFINE_IDA(master_ida);

struct fsi_slave {
	struct list_head	list_link;	/* Master's list of slaves */
	struct list_head	my_engines;
	struct device		dev;
	struct fsi_master	*master;
	int			link;
	uint8_t			id;
};

struct fsi_master_hub {
	struct fsi_master	master;
	struct fsi_slave	*slave;
	struct device		dev;
	uint32_t		control_regs;	/* slave-relative addr regs */
	uint32_t		base;		/* slave-relative addr of */
						/* master address space */
};

#define to_fsi_hub(d) container_of(d, struct fsi_master_hub, dev)
#define to_fsi_master_hub(d) container_of(d, struct fsi_master_hub, master)
#define to_fsi_slave(d) container_of(d, struct fsi_slave, dev)

static void fsi_master_unscan(struct fsi_master *master);
static int fsi_slave_read(struct fsi_slave *slave, uint32_t addr,
		void *val, size_t size);
static int fsi_slave_write(struct fsi_slave *slave, uint32_t addr,
		const void *val, size_t size);

/*
 * FSI slave engine control register offsets
 */
#define	FSI_SMODE		0x0	/* R/W: Mode register */
#define FSI_SI1M		0x18	/* R/W: IRQ mask */
#define FSI_SI1S		0x1C	/* R: IRQ status */
#define FSI_SRSIC0		0x68	/* R/W: Remote IRQ condition 0 */
#define FSI_SRSIC1		0x6C	/* R/W: Remote IRQ condition 1 */
#define FSI_SRSIM0		0x70	/* R/W: Remote IRQ mask 0 */
#define FSI_SRSIS0		0x78	/* R: Remote IRQ status 0 */

/*
 * SI1S, SI1M fields
 */
#define FSI_SI1_HUB_SRC		0x00100000	/* hub IRQ source */

/*
 * SMODE fields
 */
#define	FSI_SMODE_WSC		0x80000000	/* Warm start done */
#define	FSI_SMODE_ECRC		0x20000000	/* Hw CRC check */
#define	FSI_SMODE_SID_SHIFT	24		/* ID shift */
#define	FSI_SMODE_SID_MASK	3		/* ID Mask */
#define	FSI_SMODE_ED_SHIFT	20		/* Echo delay shift */
#define	FSI_SMODE_ED_MASK	0xf		/* Echo delay mask */
#define	FSI_SMODE_SD_SHIFT	16		/* Send delay shift */
#define	FSI_SMODE_SD_MASK	0xf		/* Send delay mask */
#define	FSI_SMODE_LBCRR_SHIFT	8		/* Clk ratio shift */
#define	FSI_SMODE_LBCRR_MASK	0xf		/* Clk ratio mask */

/*
 * SRSIS, SRSIM, SRSIC fields
 */
#define	FSI_SRSIX_IRQ1_MASK	0x00aaaaaa	/* SI1 IRQ sources */
#define	FSI_SRSIX_BITS_PER_LINK	8


/* FSI endpoint-device support */
int fsi_device_read(struct fsi_device *dev, uint32_t addr, void *val,
		size_t size)
{
	if (addr > dev->size || size > dev->size || addr > dev->size - size)
		return -EINVAL;

	return fsi_slave_read(dev->slave, dev->addr + addr, val, size);
}
EXPORT_SYMBOL_GPL(fsi_device_read);

int fsi_device_write(struct fsi_device *dev, uint32_t addr, const void *val,
		size_t size)
{
	if (addr > dev->size || size > dev->size || addr > dev->size - size)
		return -EINVAL;

	return fsi_slave_write(dev->slave, dev->addr + addr, val, size);
}
EXPORT_SYMBOL_GPL(fsi_device_write);

int fsi_device_peek(struct fsi_device *dev, void *val)
{
	uint32_t addr = FSI_PEEK_BASE + ((dev->unit - 2) * sizeof(uint32_t));

	return fsi_slave_read(dev->slave, addr, val, sizeof(uint32_t));
}

static void fsi_device_release(struct device *_device)
{
	struct fsi_device *device = to_fsi_dev(_device);

	kfree(device);
}

static struct fsi_device *fsi_create_device(struct fsi_slave *slave)
{
	struct fsi_device *dev;

	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev)
		return NULL;

	dev->dev.parent = &slave->dev;
	dev->dev.bus = &fsi_bus_type;
	dev->dev.release = fsi_device_release;

	return dev;
}

/* crc helpers */
static const uint8_t crc4_tab[] = {
	0x0, 0x7, 0xe, 0x9, 0xb, 0xc, 0x5, 0x2,
	0x1, 0x6, 0xf, 0x8, 0xa, 0xd, 0x4, 0x3,
};

uint8_t fsi_crc4(uint8_t c, uint64_t x, int bits)
{
	int i;

	/* Align to 4-bits */
	bits = (bits + 3) & ~0x3;

	/* Calculate crc4 over four-bit nibbles, starting at the MSbit */
	for (i = bits; i >= 0; i -= 4)
		c = crc4_tab[c ^ ((x >> i) & 0xf)];

	return c;
}
EXPORT_SYMBOL_GPL(fsi_crc4);

/* FSI slave support */

/* Encode slave local bus echo delay */
static inline uint32_t fsi_smode_echodly(int x)
{
	return (x & FSI_SMODE_ED_MASK) << FSI_SMODE_ED_SHIFT;
}

/* Encode slave local bus send delay */
static inline uint32_t fsi_smode_senddly(int x)
{
	return (x & FSI_SMODE_SD_MASK) << FSI_SMODE_SD_SHIFT;
}

/* Encode slave local bus clock rate ratio */
static inline uint32_t fsi_smode_lbcrr(int x)
{
	return (x & FSI_SMODE_LBCRR_MASK) << FSI_SMODE_LBCRR_SHIFT;
}

/* Encode slave ID */
static inline uint32_t fsi_smode_sid(int x)
{
	return (x & FSI_SMODE_SID_MASK) << FSI_SMODE_SID_SHIFT;
}

static int fsi_slave_read(struct fsi_slave *slave, uint32_t addr,
			void *val, size_t size)
{
	return slave->master->read(slave->master, slave->link,
			slave->id, addr, val, size);
}

static int fsi_slave_write(struct fsi_slave *slave, uint32_t addr,
			const void *val, size_t size)
{
	return slave->master->write(slave->master, slave->link,
			slave->id, addr, val, size);
}

/*
 * FSI hub master support
 *
 * A hub master increases the number of potential target devices that the
 * primary FSI master can access.  For each link a primary master supports
 * each of those links can in turn be chained to a hub master with multiple
 * hub links of its own.  Hubs differ from cascaded masters (cMFSI) in the
 * total addressable range per link -hubs having address ranges that are much
 * larger.  Hub masters also contain the registers that describe them
 * whereas cascaded masters are described by their parent.
 */
int hub_master_read(struct fsi_master *master, int linkno, uint8_t slave,
			uint32_t addr, void *val, size_t size)
{
	struct fsi_master_hub *hub = to_fsi_master_hub(master);

	addr += (linkno * FSI_MASTER_HUB_LINK_SIZE) + hub->base;
	return fsi_slave_read(hub->slave, addr, val, size);
}

int hub_master_write(struct fsi_master *master, int linkno, uint8_t slave,
			uint32_t addr, const void *val, size_t size)
{
	struct fsi_master_hub *hub = to_fsi_master_hub(master);

	addr += (linkno * FSI_MASTER_HUB_LINK_SIZE) + hub->base;
	return fsi_slave_write(hub->slave, addr, val, size);
}

int hub_master_break(struct fsi_master *master, int linkno)
{
	struct fsi_master_hub *hub = to_fsi_master_hub(master);
	uint32_t command;
	uint32_t break_offset = 0x4; /* hw workaround: hub links require a */
				     /* break to offset 4 instead of the */
				     /* non hub 0 offset. */
	uint32_t addr;

	command = FSI_BREAK;
	addr = (linkno * FSI_MASTER_HUB_LINK_SIZE) + hub->base;
	return fsi_slave_write(hub->slave, addr + break_offset, &command,
			sizeof(command));
}

int hub_master_link_enable(struct fsi_master *master, int linkno)
{
	struct fsi_master_hub *hub = to_fsi_master_hub(master);
	uint32_t menp = L_MSB_MASK(linkno);
	int rc;

	rc = fsi_slave_write(hub->slave, hub->control_regs + FSI_MSENP0, &menp,
				sizeof(menp));

	/*
	 * Wait for hw to finish enable - there is latency in logic setup
	 * before link operations like break, etc can be done
	 */
	mdelay(FSI_LINK_ENABLE_SETUP_TIME);

	return rc;
}

static int hub_master_init(struct fsi_master_hub *hub)
{
	int rc;
	uint32_t mver;
	struct fsi_master *master = &hub->master;

	master->read = hub_master_read;
	master->write = hub_master_write;
	master->send_break = hub_master_break;
	master->link_enable = hub_master_link_enable;

	/* Initialize the MFSI (hub master) engine */
	rc = fsi_slave_read(hub->slave, hub->control_regs + FSI_MVER, &mver,
				sizeof(mver));
	if (rc)
		return rc;

	mver = FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK
			| FSI_MRESP_RST_MCR | FSI_MRESP_RST_PYE;
	rc = fsi_slave_write(hub->slave, hub->control_regs + FSI_MRESP0, &mver,
				sizeof(mver));
	if (rc)
		return rc;

	mver = FSI_MECTRL_EOAE | FSI_MECTRL_P8_AUTO_TERM;
	rc = fsi_slave_write(hub->slave, hub->control_regs + FSI_MECTRL, &mver,
				sizeof(mver));
	if (rc)
		return rc;

	mver = FSI_MMODE_EIP | FSI_MMODE_ECRC | FSI_MMODE_EPC
			| fsi_mmode_crs0(1) | fsi_mmode_crs1(1)
			| FSI_MMODE_P8_TO_LSB;
	rc = fsi_slave_write(hub->slave, hub->control_regs + FSI_MMODE, &mver,
				sizeof(mver));
	if (rc)
		return rc;

	mver = 0xffff0000;
	rc = fsi_slave_write(hub->slave, hub->control_regs + FSI_MDLYR, &mver,
				sizeof(mver));
	if (rc)
		return rc;

	mver = ~0;
	rc = fsi_slave_write(hub->slave, hub->control_regs + FSI_MSENP0, &mver,
				sizeof(mver));
	if (rc)
		return rc;

	/* Leave enabled long enough for master logic to set up */
	mdelay(FSI_LINK_ENABLE_SETUP_TIME);

	rc = fsi_slave_write(hub->slave, hub->control_regs + FSI_MCENP0, &mver,
				sizeof(mver));
	if (rc)
		return rc;

	rc = fsi_slave_read(hub->slave, hub->control_regs + FSI_MAEB, &mver,
				sizeof(mver));
	if (rc)
		return rc;

	mver = FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK;
	rc = fsi_slave_write(hub->slave, hub->control_regs + FSI_MRESP0, &mver,
				sizeof(mver));
	if (rc)
		return rc;

	rc = fsi_slave_read(hub->slave, hub->control_regs + FSI_MLEVP0, &mver,
				sizeof(mver));
	if (rc)
		return rc;

	/* Reset the master bridge */
	mver = FSI_MRESB_RST_GEN;
	rc = fsi_slave_write(hub->slave, hub->control_regs + FSI_MRESB0, &mver,
				sizeof(mver));
	if (rc)
		return rc;

	mver = FSI_MRESB_RST_ERR;
	return fsi_slave_write(hub->slave, hub->control_regs + FSI_MRESB0,
				&mver, sizeof(mver));
}

static void hub_master_release(struct device *dev)
{
	struct fsi_master_hub *hub = to_fsi_hub(dev);

	kfree(hub);
}

static int fsi_slave_scan(struct fsi_slave *slave)
{
	uint32_t engine_addr;
	uint32_t conf;
	int rc, i;
	uint8_t si1s_bit = 1;
	uint8_t conf_link_count = 0;
	struct fsi_master_hub *hub;

	INIT_LIST_HEAD(&slave->my_engines);

	/*
	 * scan engines
	 *
	 * We keep the peek mode and slave engines for the core; so start
	 * at the third slot in the configuration table. We also need to
	 * skip the chip ID entry at the start of the address space.
	 */
	engine_addr = engine_page_size * 3;
	for (i = 2; i < engine_page_size / sizeof(uint32_t); i++) {
		uint8_t slots, version, type, crc;
		struct fsi_device *dev;

		rc = fsi_slave_read(slave, (i + 1) * sizeof(conf),
				&conf, sizeof(conf));
		if (rc) {
			dev_warn(&slave->dev,
				"error reading slave registers\n");
			return -1;
		}

		crc = fsi_crc4(0, conf >> FSI_SLAVE_CONF_CRC_SHIFT,
				FSI_SLAVE_CONF_DATA_BITS);
		if (crc != (conf & FSI_SLAVE_CONF_CRC_MASK)) {
			dev_warn(&slave->dev,
				"crc error in slave register at 0x%04x\n",
				i);
			return -1;
		}

		slots = (conf & FSI_SLAVE_CONF_SLOTS_MASK)
			>> FSI_SLAVE_CONF_SLOTS_SHIFT;
		version = (conf & FSI_SLAVE_CONF_VERSION_MASK)
			>> FSI_SLAVE_CONF_VERSION_SHIFT;
		type = (conf & FSI_SLAVE_CONF_TYPE_MASK)
			>> FSI_SLAVE_CONF_TYPE_SHIFT;

		switch (type) {
		case 0:
			/*
			 * Unused address areas are marked by a zero type
			 * value; this skips the defined address areas
			 */
			break;

		case FSI_ENGID_HUB_MASTER:
			hub = kzalloc(sizeof(*hub), GFP_KERNEL);
			if (!hub)
				return -ENOMEM;

			device_initialize(&hub->dev);
			dev_set_name(&hub->dev, "hub@%02x", slave->master->idx);
			hub->dev.release = hub_master_release;
			hub->master.dev = &hub->dev;
			hub->master.dev->parent = &slave->dev;
			dev_set_drvdata(&hub->dev, hub);
			rc = device_add(&hub->dev);
			if (rc)
				return rc;

			hub->base = FSI_HUB_LINK_OFFSET;
			hub->control_regs = engine_addr;
			hub->slave = slave;
			rc = hub_master_init(hub);

			break;

		case FSI_ENGID_HUB_LINK:
			conf_link_count++;

			break;

		default:
			if (slots == 0)
				break;

			/* create device */
			dev = fsi_create_device(slave);
			if (!dev)
				return -ENOMEM;

			dev->slave = slave;
			dev->engine_type = type;
			dev->version = version;
			dev->unit = i;
			dev->addr = engine_addr;
			dev->size = slots * engine_page_size;
			dev->si1s_bit = si1s_bit++;

			dev_info(&slave->dev,
			"engine[%i]: type %x, version %x, addr %x size %x\n",
					dev->unit, dev->engine_type, version,
					dev->addr, dev->size);

			device_initialize(&dev->dev);
			dev_set_name(&dev->dev, "%02x:%02x:%02x:%02x",
					slave->master->idx, slave->link,
					slave->id, i - 2);

			rc = device_add(&dev->dev);
			if (rc) {
				dev_warn(&slave->dev, "add failed: %d\n", rc);
				put_device(&dev->dev);
				continue;
			}
			list_add(&dev->link, &slave->my_engines);
		}

		engine_addr += slots * engine_page_size;

		if (!(conf & FSI_SLAVE_CONF_NEXT_MASK))
			break;
	}

	if (hub) {
		hub->master.n_links = conf_link_count / 2;
		fsi_master_register(&hub->master);
	}

	return 0;
}

static void fsi_slave_release(struct device *dev)
{
	struct fsi_slave *slave = to_fsi_slave(dev);

	kfree(slave);
}

static uint32_t set_smode_defaults(struct fsi_master *master)
{
	return FSI_SMODE_WSC | FSI_SMODE_ECRC
		| fsi_smode_echodly(0xf) | fsi_smode_senddly(0xf)
		| fsi_smode_lbcrr(1);
}

static int fsi_slave_set_smode(struct fsi_master *master, int link, int id)
{
	uint32_t smode = set_smode_defaults(master);

	smode |= fsi_smode_sid(id);
	return master->write(master, link, 3, FSI_SLAVE_BASE + FSI_SMODE,
				&smode, sizeof(smode));
}

static ssize_t fsi_slave_sysfs_raw_read(struct file *file,
		struct kobject *kobj, struct bin_attribute *attr, char *buf,
		loff_t off, size_t count)
{
	struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
	int rc;

	if (count != 4 || off & 0x3)
		return -EINVAL;

	if (off > 0xfffffffc || off < 0)
		return -EINVAL;

	rc = fsi_slave_read(slave, off, buf, 4);

	return rc ? rc : count;
}

static ssize_t fsi_slave_sysfs_raw_write(struct file *file,
		struct kobject *kobj, struct bin_attribute *attr,
		char *buf, loff_t off, size_t count)
{
	struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
	int rc;

	if (count != 4 || off & 0x3)
		return -EINVAL;

	if (off > 0xfffffffc || off < 0)
		return -EINVAL;

	rc = fsi_slave_write(slave, off, buf, 4);

	return rc ? rc : count;
}


static struct bin_attribute fsi_slave_raw_attr = {
	.attr = {
		.name = "raw",
		.mode = S_IRUSR | S_IWUSR,
	},
	.size = 0,
	.read = fsi_slave_sysfs_raw_read,
	.write = fsi_slave_sysfs_raw_write,
};

static int fsi_slave_irq_clear(struct fsi_slave *slave)
{
	uint32_t clear = ~0;
	int rc;

	rc = fsi_slave_write(slave, FSI_SLAVE_BASE + FSI_SRSIC0, &clear,
				sizeof(clear));
	if (rc) {
		dev_dbg(&slave->dev, "Failed on write to SRSIC0\n");
		return rc;
	}
	return fsi_slave_write(slave, FSI_SLAVE_BASE + FSI_SRSIC1, &clear,
				sizeof(clear));
}

static int fsi_slave_init(struct fsi_master *master,
		int link, uint8_t slave_id)
{
	struct fsi_slave *slave;
	uint32_t chip_id;
	int rc;
	uint8_t crc;

	/*
	 * todo: Due to CFAM hardware issues related to BREAK commands we're
	 * limited to only one CFAM per link.  Once issues are resolved this
	 * restriction can be removed.
	 */
	if (slave_id > 0)
		return 0;

	rc = fsi_slave_set_smode(master, link, slave_id);
	if (rc) {
		dev_warn(master->dev, "can't set smode on slave:%02x:%02x %d\n",
				link, slave_id, rc);
		return -ENODEV;
	}

	rc = master->read(master, link, slave_id, 0, &chip_id, sizeof(chip_id));
	if (rc) {
		dev_warn(master->dev, "can't read slave %02x:%02x: %d\n",
				link, slave_id, rc);
		return -ENODEV;
	}
	crc = fsi_crc4(0, chip_id >> FSI_SLAVE_CONF_CRC_SHIFT,
			FSI_SLAVE_CONF_DATA_BITS);
	if (crc != (chip_id & FSI_SLAVE_CONF_CRC_MASK)) {
		dev_warn(master->dev, "slave %02x:%02x invalid chip id CRC!\n",
				link, slave_id);
		return -EIO;
	}

	pr_debug("fsi: found chip %08x at %02x:%02x:%02x\n",
			master->idx, chip_id, link, slave_id);

	/* we can communicate with a slave; create devices and scan */
	slave = kzalloc(sizeof(*slave), GFP_KERNEL);
	if (!slave)
		return -ENOMEM;

	slave->master = master;
	slave->id = slave_id;
	slave->dev.parent = master->dev;
	slave->dev.release = fsi_slave_release;
	slave->link = link;

	dev_set_name(&slave->dev, "slave@%02x:%02x", link, slave_id);
	rc = device_register(&slave->dev);
	if (rc < 0) {
		dev_warn(master->dev, "failed to create slave device: %d\n",
				rc);
		put_device(&slave->dev);
		return rc;
	}

	rc = device_create_bin_file(&slave->dev, &fsi_slave_raw_attr);
	if (rc)
		dev_warn(&slave->dev, "failed to create raw attr: %d\n", rc);

	list_add(&slave->list_link, &master->my_slaves);
	rc = fsi_slave_scan(slave);
	if (rc)
		return rc;

	return fsi_slave_irq_clear(slave);
}

/* FSI master support */

static int fsi_master_link_enable(struct fsi_master *master, int link)
{
	if (master->link_enable)
		return master->link_enable(master, link);

	return 0;
}

/*
 * Issue a break command on this link
 */
static int fsi_master_break(struct fsi_master *master, int link)
{
	if (master->send_break)
		return master->send_break(master, link);

	return 0;
}

void fsi_master_handle_error(struct fsi_master *master, uint32_t addr)
{
	uint32_t smode = FSI_SLAVE_SMODE_DFLT;
	static atomic_t in_err_cleanup = ATOMIC_INIT(-1);

	if (!atomic_inc_and_test(&in_err_cleanup))
		return;

	fsi_master_break(master, 0);
	udelay(200);
	master->write(master, 0, 0, FSI_SLAVE_BASE + FSI_SMODE, &smode,
			sizeof(smode));
	smode = FSI_MRESB_RST_GEN | FSI_MRESB_RST_ERR;
	master->write(master, 0, 0, FSI_HUB_CONTROL + FSI_MRESB0, &smode,
			sizeof(smode));

	if (addr > FSI_HUB_LINK_OFFSET) {
		smode = FSI_BREAK;
		master->write(master, 0, 0, 0x100004, &smode, sizeof(smode));
		smode = FSI_SLAVE_SMODE_DFLT;
		master->write(master, 0, 0, 0x100800, &smode, sizeof(smode));
	}

	atomic_set(&in_err_cleanup, -1);
}
EXPORT_SYMBOL(fsi_master_handle_error);

static int fsi_master_scan(struct fsi_master *master)
{
	int link, slave_id, rc;
	uint32_t smode;

	if (!master->slave_list) {
		INIT_LIST_HEAD(&master->my_slaves);
		master->slave_list = true;
	}

	for (link = 0; link < master->n_links; link++) {
		rc = fsi_master_link_enable(master, link);
		if (rc) {
			dev_dbg(master->dev,
				"enable link:%d failed with:%d\n", link, rc);
			continue;
		}
		rc = fsi_master_break(master, link);
		if (rc) {
			dev_dbg(master->dev,
				"Break to link:%d failed with:%d\n", link, rc);
			continue;
		}

		/*
		 * Verify can read slave at default ID location. If fails
		 * there must be nothing on other end of link
		 */
		rc = master->read(master, link, 3, FSI_SLAVE_BASE + FSI_SMODE,
				&smode, sizeof(smode));
		if (rc) {
			dev_dbg(master->dev,
				"Read link:%d smode default id failed:%d\n",
				link, rc);
			continue;
		}

		for (slave_id = 0; slave_id < FSI_N_SLAVES; slave_id++)
			fsi_slave_init(master, link, slave_id);

	}

	return 0;
}

static ssize_t fsi_ipoll_period_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE - 1, "%u\n", fsi_ipoll_period_ms);
}

static ssize_t fsi_ipoll_period_store(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	int rc;
	unsigned long val = 0;

	rc = kstrtoul(buf, 0, &val);

	if (val > 1 && val < 10000)
	fsi_ipoll_period_ms = val;

	return count;
}

DEVICE_ATTR(fsi_ipoll_period, S_IRUGO | S_IWUSR, fsi_ipoll_period_show,
		fsi_ipoll_period_store);

static int fsi_unregister_hubs(struct device *dev, void *data)
{
	struct fsi_master_hub *hub = dev_get_drvdata(dev);

	if (!hub)
		return 0;

	ida_simple_remove(&master_ida, hub->master.idx);
	hub->master.idx = -1;
	device_remove_file(dev, &dev_attr_fsi_ipoll_period);
	fsi_master_unscan(&hub->master);
	device_del(dev);

	return 0;
}

static void fsi_master_unscan(struct fsi_master *master)
{
	struct fsi_slave *slave, *slave_tmp;
	struct fsi_device *fsi_dev, *fsi_dev_tmp;

	if (!master->slave_list)
		return;

	list_for_each_entry_safe(slave, slave_tmp, &master->my_slaves,
							list_link) {
		list_del(&slave->list_link);
		list_for_each_entry_safe(fsi_dev, fsi_dev_tmp,
					&slave->my_engines, link) {
			list_del(&fsi_dev->link);
			device_del(&fsi_dev->dev);
			put_device(&fsi_dev->dev);
		}
		/* Remove any hub masters */
		device_for_each_child(&slave->dev, NULL, fsi_unregister_hubs);
		device_unregister(&slave->dev);
	}
	master->slave_list = false;
}

/* TODO: Add support for hub links 4-7 */
static int next_hublink_source(struct fsi_slave *slave, uint32_t srsis)
{
	int index;

	if (!slave)
		return -EINVAL;

	if (!(srsis & FSI_SRSIX_IRQ1_MASK)) {
		dev_dbg(&slave->dev, "Unexpected IRQ source SRSIS:0x%08x\n",
			srsis);
		return -EINVAL;
	}

	/*
	 * TODO: add a fair scheduler to ensure we don't favor lower
	 * hublink IRQ sources over others
	 */
	index = __clz(srsis);
	dev_dbg(&slave->dev, "SRSIS:0x%08x index:%d\n", srsis, index);
	return index / FSI_SRSIX_BITS_PER_LINK;
}

static int __fsi_dev_irq(struct device *dev, void *data);

static int __fsi_hub_slave_irq(struct device *dev, void *data)
{
	int rc;
	struct fsi_slave *hub_slave = to_fsi_slave(dev);
	uint32_t si1s;

	if (!hub_slave) {
		dev_dbg(dev, "Could not find hub slave\n");
		return -ENODEV;
	}

	rc = fsi_slave_read(hub_slave, FSI_SLAVE_BASE + FSI_SI1S, &si1s,
			sizeof(si1s));
	if (rc) {
		dev_dbg(dev, "Fail on read of hub slave si1s\n");
		return rc;
	}

	if (!si1s)
		return 0;

	return device_for_each_child(dev, &si1s, __fsi_dev_irq);
}

static int __fsi_dev_irq(struct device *dev, void *data)
{
	uint32_t *si1s = data, srsis;
	struct fsi_device *fsi_dev = to_fsi_dev(dev);
	struct fsi_slave *slave;
	struct fsi_master_hub *hub;
	int rc, hublink;

	if (!fsi_dev || !si1s) {
		dev_dbg(dev, "Invalid input: %p %p\n", fsi_dev, si1s);
		return -EINVAL;
	}

	if (*si1s & (0x80000000 >> fsi_dev->si1s_bit) && fsi_dev->irq_handler) {
		fsi_dev->irq_handler(0, &fsi_dev->dev);
		return 1;
	}

	if (!(*si1s & FSI_SI1_HUB_SRC)) {
		dev_dbg(dev, "IRQ not from a hub source\n");
		return 0;
	}

	hub = dev_get_drvdata(dev);
	if (!hub) {
		dev_dbg(dev, "Not a hub device\n");
		return 0;
	}

	/* Scan the hub links for the source of IRQ */
	slave = to_fsi_slave(dev->parent);
	if (!slave) {
		dev_dbg(dev, "Could not retrieve device's slave\n");
		return -ENODEV;
	}

	rc = fsi_slave_read(slave, FSI_SLAVE_BASE + FSI_SRSIS0, &srsis,
			sizeof(srsis));
	if (rc) {
		dev_dbg(&slave->dev, "Failed to read SRSIS0\n");
		return rc;
	}
	if (srsis) {
		hublink = next_hublink_source(slave, srsis);

		if (!hub->master.dev)
			return 0;

		device_for_each_child(dev, &hublink, __fsi_hub_slave_irq);

		/* Clear out the interrupting condition */
		srsis = 0xff000000 >> (hublink * FSI_SRSIX_BITS_PER_LINK);
		rc =  fsi_slave_write(slave, FSI_SLAVE_BASE + FSI_SRSIC0,
					&srsis, sizeof(srsis));
		if (rc) {
			dev_dbg(&slave->dev, "Failed to clear out SRSIC\n");
			return rc;
		}
	} else {
		dev_dbg(&slave->dev, "SI1S HUB src but no SRSIS0 bits!\n");
		return -EINVAL;
	}

	return 1;
}

static int __fsi_slave_irq(struct device *dev, void *data)
{
	return device_for_each_child(dev, data, __fsi_dev_irq);
}

static void fsi_master_irq(struct fsi_master *master, int link, uint32_t si1s)
{
	device_for_each_child(master->dev, &si1s, __fsi_slave_irq);
}

static int fsi_master_ipoll(void *data)
{
	int rc;
	uint32_t si1s;
	unsigned long elapsed = 0;
	unsigned long previous_jiffies = jiffies;
	struct fsi_master *master = data;

	while (!kthread_should_stop()) {
		if (!master->ipoll)
			goto done;

		/* Ignore errors for now */
		rc = master->read(master, 0, 0, FSI_SLAVE_BASE + FSI_SI1S,
				  &si1s, sizeof(uint32_t));
		if (rc)
			goto done;

		if (si1s & master->ipoll)
			fsi_master_irq(master, 0, si1s);
done:
		elapsed = jiffies - previous_jiffies;
		if (elapsed < FSI_IPOLL_PERIOD) {
			set_current_state(TASK_UNINTERRUPTIBLE);
			schedule_timeout(FSI_IPOLL_PERIOD - elapsed);
		}
		previous_jiffies = jiffies;
	}

	return 0;
}

int fsi_master_register(struct fsi_master *master)
{
	if (!master || !master->dev)
		return -EINVAL;

	master->idx = ida_simple_get(&master_ida, 0, INT_MAX, GFP_KERNEL);
	master->slave_list = false;
	get_device(master->dev);
	fsi_master_scan(master);
	device_create_file(master->dev, &dev_attr_fsi_ipoll_period);
	return 0;
}
EXPORT_SYMBOL_GPL(fsi_master_register);

void fsi_master_unregister(struct fsi_master *master)
{
	if (!master || !master->dev || master->idx < 0)
		return;

	ida_simple_remove(&master_ida, master->idx);
	master->idx = -1;
	device_remove_file(master->dev, &dev_attr_fsi_ipoll_period);
	fsi_master_unscan(master);
	put_device(master->dev);
	if (master_ipoll) {
		kthread_stop(master_ipoll);
		master_ipoll = NULL;
	}
}
EXPORT_SYMBOL_GPL(fsi_master_unregister);

/*
 * TODO: move this to master->start_ipoll( ) -each master may have its
 * own way of doing this
 */
int fsi_master_start_ipoll(struct fsi_master *master)
{
	if (master_ipoll) {
		dev_err(master->dev, "Already polling for irqs\n");
		return -EALREADY;
	}
	master_ipoll = kthread_create(fsi_master_ipoll, master,
				"fsi_master_ipoll");
	if (IS_ERR(master_ipoll)) {
		dev_err(master->dev, "Couldn't create ipoll thread rc:%d\n",
			(int)PTR_ERR(master_ipoll));
		return PTR_ERR(master_ipoll);
	}
	wake_up_process(master_ipoll);

	return 0;
}
EXPORT_SYMBOL_GPL(fsi_master_start_ipoll);

/* FSI core & Linux bus type definitions */

static int fsi_bus_match(struct device *dev, struct device_driver *drv)
{
	struct fsi_device *fsi_dev = to_fsi_dev(dev);
	struct fsi_driver *fsi_drv = to_fsi_drv(drv);
	const struct fsi_device_id *id;

	if (!fsi_drv->id_table)
		return 0;

	for (id = fsi_drv->id_table; id->engine_type; id++) {
		if (id->engine_type != fsi_dev->engine_type)
			continue;
		if (id->version == FSI_VERSION_ANY ||
				id->version == fsi_dev->version)
			return 1;
	}

	return 0;
}

int fsi_driver_register(struct fsi_driver *fsi_drv)
{
	if (!fsi_drv)
		return -EINVAL;
	if (!fsi_drv->id_table)
		return -EINVAL;

	return driver_register(&fsi_drv->drv);
}
EXPORT_SYMBOL_GPL(fsi_driver_register);

void fsi_driver_unregister(struct fsi_driver *fsi_drv)
{
	driver_unregister(&fsi_drv->drv);
}
EXPORT_SYMBOL_GPL(fsi_driver_unregister);

static uint32_t link_to_srsim_mask(int link)
{
	return ((0x80000000 >> 6) >> FSI_SRSIX_BITS_PER_LINK*link);
}

static uint32_t link_to_msiep_mask(int link)
{
	return (0xf0000000 >> (FSI_MSIEP_BITS_PER_LINK*link));
}

static int set_si1m(struct fsi_slave *slave, uint32_t mask, int on)
{
	int rc;
	uint32_t si1m;

	rc = fsi_slave_read(slave, FSI_SLAVE_BASE + FSI_SI1M, &si1m,
			sizeof(si1m));
	if (rc) {
		dev_dbg(&slave->dev, "Failed to read SI1M\n");
		return rc;
	}

	if (on)
		si1m |= mask;
	else
		si1m &= ~mask;

	return fsi_slave_write(slave, FSI_SLAVE_BASE + FSI_SI1M, &si1m,
			sizeof(si1m));
}

static int set_upstream_irq_masks(struct fsi_master *master,
				struct fsi_slave *slave, int on)
{
	struct fsi_slave *upstream_slave;
	struct fsi_master *upstream_master;
	uint32_t mask, si1m;
	int rc;

	if (master->idx <= 0)
		return 0;

	upstream_slave = to_fsi_slave(slave->master->dev->parent);
	if (!upstream_slave) {
		dev_dbg(&slave->dev, "No upstream slave found\n");
		return -ENODEV;
	}

	rc = fsi_slave_read(upstream_slave, FSI_SLAVE_BASE + FSI_SRSIM0, &si1m,
			sizeof(si1m));
	if (rc) {
		dev_dbg(&slave->dev, "Failed to read SRSIM0\n");
		return rc;
	}

	mask = link_to_srsim_mask(slave->link);
	if (on)
		si1m |= mask;
	else
		si1m &= ~mask;
	rc = fsi_slave_write(upstream_slave, FSI_SLAVE_BASE + FSI_SRSIM0, &si1m,
			sizeof(si1m));
	if (rc) {
		dev_dbg(&slave->dev, "Failed to write SRSIM0\n");
		return rc;
	}

	upstream_master = upstream_slave->master;
	if (!upstream_master) {
		dev_dbg(&upstream_slave->dev, "Cannot find master\n");
		return -ENODEV;
	}

	rc = upstream_master->read(upstream_master, 0, 0,
				FSI_HUB_CONTROL + FSI_MSIEP0, &si1m,
				sizeof(si1m));
	if (rc) {
		dev_dbg(&upstream_slave->dev,
			"Could not read master's MSIEP\n");
		return rc;
	}

	/* TODO: merge this into above on/off check */
	mask = link_to_msiep_mask(slave->link);
	if (on) {
		upstream_master->ipoll |= FSI_SI1_HUB_SRC;
		si1m |= mask;
	} else {
		upstream_master->ipoll &= ~FSI_SI1_HUB_SRC;
		si1m &= ~mask;
	}

	rc = upstream_master->write(upstream_master, 0, 0,
				FSI_HUB_CONTROL + FSI_MSIEP0, &si1m,
				sizeof(si1m));
	if (rc) {
		dev_dbg(&upstream_slave->dev,
			"Failed to write to master's MSIEP\n");
		return rc;
	}
	si1m = 0xd0040410;
	rc = upstream_master->write(upstream_master, 0, 0,
				FSI_HUB_CONTROL + FSI_MMODE, &si1m,
				sizeof(si1m));
	if (rc) {
		dev_dbg(&upstream_slave->dev,
			"Failed to set hub I POLL\n");
	}

	si1m = FSI_SI1_HUB_SRC;
	rc = upstream_master->write(upstream_master, 0, 0,
				FSI_SLAVE_BASE + FSI_SI1M, &si1m,
				sizeof(si1m));
	if (rc) {
		dev_dbg(&upstream_slave->dev,
			"Failed to set hub mask in SI1M\n");
	}

	return set_si1m(upstream_slave, FSI_SI1_HUB_SRC, on);
}

int fsi_enable_irq(struct fsi_device *dev)
{
	int rc;
	u32 si1m;
	u32 bit = 0x80000000 >> dev->si1s_bit;
	struct fsi_master *master = dev->slave->master;
	struct fsi_slave *slave = dev->slave;
	int link = slave->link;

	if (!dev->irq_handler)
		return -EINVAL;

	rc = master->read(master, link, 0, FSI_SLAVE_BASE + FSI_SI1M, &si1m,
			sizeof(u32));
	if (rc) {
		dev_err(master->dev, "couldn't read si1m:%d\n", rc);
		return rc;
	}

	si1m |= bit;
	rc = master->write(master, link, 0, FSI_SLAVE_BASE + FSI_SI1M, &si1m,
			sizeof(u32));
	if (rc) {
		dev_err(master->dev, "couldn't write si1m:%d\n", rc);
		return rc;
	}

	master->ipoll |= bit;
	return set_upstream_irq_masks(master, slave, 1);
}
EXPORT_SYMBOL_GPL(fsi_enable_irq);

void fsi_disable_irq(struct fsi_device *dev)
{
	int rc;
	u32 si1m;
	u32 bits = ~(0x80000000 >> dev->si1s_bit);
	struct fsi_master *master = dev->slave->master;
	struct fsi_slave *slave = dev->slave;
	int link = dev->slave->link;

	master->ipoll &= bits;

	rc = master->read(master, link, 0, FSI_SLAVE_BASE + FSI_SI1M, &si1m,
			sizeof(u32));
	if (rc) {
		dev_err(master->dev, "couldn't read si1m:%d\n", rc);
		return;
	}

	si1m &= bits;
	rc = master->write(master, link, 0, FSI_SLAVE_BASE + FSI_SI1M, &si1m,
			sizeof(u32));
	if (rc) {
		dev_err(master->dev, "couldn't write si1m:%d\n", rc);
		return;
	}

	if (!master->ipoll)
		set_upstream_irq_masks(master, slave, 0);
}

struct bus_type fsi_bus_type = {
	.name		= "fsi",
	.match		= fsi_bus_match,
};
EXPORT_SYMBOL_GPL(fsi_bus_type);

static int fsi_init(void)
{
	return bus_register(&fsi_bus_type);
}

static void fsi_exit(void)
{
	bus_unregister(&fsi_bus_type);
}

module_init(fsi_init);
module_exit(fsi_exit);