summaryrefslogtreecommitdiffstats
path: root/drivers/vme/bridges/vme_fake.c
blob: 30b3acc9383309509862e0d1b942c22bc82a5361 (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
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
/*
 * Fake VME bridge support.
 *
 * This drive provides a fake VME bridge chip, this enables debugging of the
 * VME framework in the absence of a VME system.
 *
 * This driver has to do a number of things in software that would be driven
 * by hardware if it was available, it will also result in extra overhead at
 * times when compared with driving actual hardware.
 *
 * Author: Martyn Welch <martyn@welches.me.uk>
 * Copyright (c) 2014 Martyn Welch
 *
 * Based on vme_tsi148.c:
 *
 * Author: Martyn Welch <martyn.welch@ge.com>
 * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
 *
 * Based on work by Tom Armistead and Ajit Prem
 * Copyright 2004 Motorola Inc.
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 */

#include <linux/device.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/types.h>
#include <linux/vme.h>

#include "../vme_bridge.h"

/*
 *  Define the number of each that the fake driver supports.
 */
#define FAKE_MAX_MASTER		8	/* Max Master Windows */
#define FAKE_MAX_SLAVE		8	/* Max Slave Windows */

/* Structures to hold information normally held in device registers */
struct fake_slave_window {
	int enabled;
	unsigned long long vme_base;
	unsigned long long size;
	void *buf_base;
	u32 aspace;
	u32 cycle;
};

struct fake_master_window {
	int enabled;
	unsigned long long vme_base;
	unsigned long long size;
	u32 aspace;
	u32 cycle;
	u32 dwidth;
};

/* Structure used to hold driver specific information */
struct fake_driver {
	struct vme_bridge *parent;
	struct fake_slave_window slaves[FAKE_MAX_SLAVE];
	struct fake_master_window masters[FAKE_MAX_MASTER];
	u32 lm_enabled;
	unsigned long long lm_base;
	u32 lm_aspace;
	u32 lm_cycle;
	void (*lm_callback[4])(void *);
	void *lm_data[4];
	struct tasklet_struct int_tasklet;
	int int_level;
	int int_statid;
	void *crcsr_kernel;
	dma_addr_t crcsr_bus;
	/* Only one VME interrupt can be generated at a time, provide locking */
	struct mutex vme_int;
};

/* Module parameter */
static int geoid;

static const char driver_name[] = "vme_fake";

static struct vme_bridge *exit_pointer;

static struct device *vme_root;

/*
 * Calling VME bus interrupt callback if provided.
 */
static void fake_VIRQ_tasklet(unsigned long data)
{
	struct vme_bridge *fake_bridge;
	struct fake_driver *bridge;

	fake_bridge = (struct vme_bridge *) data;
	bridge = fake_bridge->driver_priv;

	vme_irq_handler(fake_bridge, bridge->int_level, bridge->int_statid);
}

/*
 * Configure VME interrupt
 */
static void fake_irq_set(struct vme_bridge *fake_bridge, int level,
		int state, int sync)
{
	/* Nothing to do */
}

static void *fake_pci_to_ptr(dma_addr_t addr)
{
	return (void *)(uintptr_t)addr;
}

static dma_addr_t fake_ptr_to_pci(void *addr)
{
	return (dma_addr_t)(uintptr_t)addr;
}

/*
 * Generate a VME bus interrupt at the requested level & vector. Wait for
 * interrupt to be acked.
 */
static int fake_irq_generate(struct vme_bridge *fake_bridge, int level,
		int statid)
{
	struct fake_driver *bridge;

	bridge = fake_bridge->driver_priv;

	mutex_lock(&bridge->vme_int);

	bridge->int_level = level;

	bridge->int_statid = statid;

	/*
	 * Schedule tasklet to run VME handler to emulate normal VME interrupt
	 * handler behaviour.
	 */
	tasklet_schedule(&bridge->int_tasklet);

	mutex_unlock(&bridge->vme_int);

	return 0;
}

/*
 * Initialize a slave window with the requested attributes.
 */
static int fake_slave_set(struct vme_slave_resource *image, int enabled,
		unsigned long long vme_base, unsigned long long size,
		dma_addr_t buf_base, u32 aspace, u32 cycle)
{
	unsigned int i, granularity = 0;
	unsigned long long vme_bound;
	struct vme_bridge *fake_bridge;
	struct fake_driver *bridge;

	fake_bridge = image->parent;
	bridge = fake_bridge->driver_priv;

	i = image->number;

	switch (aspace) {
	case VME_A16:
		granularity = 0x10;
		break;
	case VME_A24:
		granularity = 0x1000;
		break;
	case VME_A32:
		granularity = 0x10000;
		break;
	case VME_A64:
		granularity = 0x10000;
		break;
	case VME_CRCSR:
	case VME_USER1:
	case VME_USER2:
	case VME_USER3:
	case VME_USER4:
	default:
		pr_err("Invalid address space\n");
		return -EINVAL;
	}

	/*
	 * Bound address is a valid address for the window, adjust
	 * accordingly
	 */
	vme_bound = vme_base + size - granularity;

	if (vme_base & (granularity - 1)) {
		pr_err("Invalid VME base alignment\n");
		return -EINVAL;
	}
	if (vme_bound & (granularity - 1)) {
		pr_err("Invalid VME bound alignment\n");
		return -EINVAL;
	}

	mutex_lock(&image->mtx);

	bridge->slaves[i].enabled = enabled;
	bridge->slaves[i].vme_base = vme_base;
	bridge->slaves[i].size = size;
	bridge->slaves[i].buf_base = fake_pci_to_ptr(buf_base);
	bridge->slaves[i].aspace = aspace;
	bridge->slaves[i].cycle = cycle;

	mutex_unlock(&image->mtx);

	return 0;
}

/*
 * Get slave window configuration.
 */
static int fake_slave_get(struct vme_slave_resource *image, int *enabled,
		unsigned long long *vme_base, unsigned long long *size,
		dma_addr_t *buf_base, u32 *aspace, u32 *cycle)
{
	unsigned int i;
	struct fake_driver *bridge;

	bridge = image->parent->driver_priv;

	i = image->number;

	mutex_lock(&image->mtx);

	*enabled = bridge->slaves[i].enabled;
	*vme_base = bridge->slaves[i].vme_base;
	*size = bridge->slaves[i].size;
	*buf_base = fake_ptr_to_pci(bridge->slaves[i].buf_base);
	*aspace = bridge->slaves[i].aspace;
	*cycle = bridge->slaves[i].cycle;

	mutex_unlock(&image->mtx);

	return 0;
}

/*
 * Set the attributes of an outbound window.
 */
static int fake_master_set(struct vme_master_resource *image, int enabled,
		unsigned long long vme_base, unsigned long long size,
		u32 aspace, u32 cycle, u32 dwidth)
{
	int retval = 0;
	unsigned int i;
	struct vme_bridge *fake_bridge;
	struct fake_driver *bridge;

	fake_bridge = image->parent;

	bridge = fake_bridge->driver_priv;

	/* Verify input data */
	if (vme_base & 0xFFFF) {
		pr_err("Invalid VME Window alignment\n");
		retval = -EINVAL;
		goto err_window;
	}

	if (size & 0xFFFF) {
		pr_err("Invalid size alignment\n");
		retval = -EINVAL;
		goto err_window;
	}

	if ((size == 0) && (enabled != 0)) {
		pr_err("Size must be non-zero for enabled windows\n");
		retval = -EINVAL;
		goto err_window;
	}

	/* Setup data width */
	switch (dwidth) {
	case VME_D8:
	case VME_D16:
	case VME_D32:
		break;
	default:
		pr_err("Invalid data width\n");
		retval = -EINVAL;
		goto err_dwidth;
	}

	/* Setup address space */
	switch (aspace) {
	case VME_A16:
	case VME_A24:
	case VME_A32:
	case VME_A64:
	case VME_CRCSR:
	case VME_USER1:
	case VME_USER2:
	case VME_USER3:
	case VME_USER4:
		break;
	default:
		pr_err("Invalid address space\n");
		retval = -EINVAL;
		goto err_aspace;
	}

	spin_lock(&image->lock);

	i = image->number;

	bridge->masters[i].enabled = enabled;
	bridge->masters[i].vme_base = vme_base;
	bridge->masters[i].size = size;
	bridge->masters[i].aspace = aspace;
	bridge->masters[i].cycle = cycle;
	bridge->masters[i].dwidth = dwidth;

	spin_unlock(&image->lock);

	return 0;

err_aspace:
err_dwidth:
err_window:
	return retval;

}

/*
 * Set the attributes of an outbound window.
 */
static int __fake_master_get(struct vme_master_resource *image, int *enabled,
		unsigned long long *vme_base, unsigned long long *size,
		u32 *aspace, u32 *cycle, u32 *dwidth)
{
	unsigned int i;
	struct fake_driver *bridge;

	bridge = image->parent->driver_priv;

	i = image->number;

	*enabled = bridge->masters[i].enabled;
	*vme_base = bridge->masters[i].vme_base;
	*size = bridge->masters[i].size;
	*aspace = bridge->masters[i].aspace;
	*cycle = bridge->masters[i].cycle;
	*dwidth = bridge->masters[i].dwidth;

	return 0;
}


static int fake_master_get(struct vme_master_resource *image, int *enabled,
		unsigned long long *vme_base, unsigned long long *size,
		u32 *aspace, u32 *cycle, u32 *dwidth)
{
	int retval;

	spin_lock(&image->lock);

	retval = __fake_master_get(image, enabled, vme_base, size, aspace,
			cycle, dwidth);

	spin_unlock(&image->lock);

	return retval;
}


static void fake_lm_check(struct fake_driver *bridge, unsigned long long addr,
			  u32 aspace, u32 cycle)
{
	struct vme_bridge *fake_bridge;
	unsigned long long lm_base;
	u32 lm_aspace, lm_cycle;
	int i;
	struct vme_lm_resource *lm;
	struct list_head *pos = NULL, *n;

	/* Get vme_bridge */
	fake_bridge = bridge->parent;

	/* Loop through each location monitor resource */
	list_for_each_safe(pos, n, &fake_bridge->lm_resources) {
		lm = list_entry(pos, struct vme_lm_resource, list);

		/* If disabled, we're done */
		if (bridge->lm_enabled == 0)
			return;

		lm_base = bridge->lm_base;
		lm_aspace = bridge->lm_aspace;
		lm_cycle = bridge->lm_cycle;

		/* First make sure that the cycle and address space match */
		if ((lm_aspace == aspace) && (lm_cycle == cycle)) {
			for (i = 0; i < lm->monitors; i++) {
				/* Each location monitor covers 8 bytes */
				if (((lm_base + (8 * i)) <= addr) &&
				    ((lm_base + (8 * i) + 8) > addr)) {
					if (bridge->lm_callback[i] != NULL)
						bridge->lm_callback[i](
							bridge->lm_data[i]);
				}
			}
		}
	}
}

static u8 fake_vmeread8(struct fake_driver *bridge, unsigned long long addr,
		u32 aspace, u32 cycle)
{
	u8 retval = 0xff;
	int i;
	unsigned long long start, end, offset;
	u8 *loc;

	for (i = 0; i < FAKE_MAX_SLAVE; i++) {
		start = bridge->slaves[i].vme_base;
		end = bridge->slaves[i].vme_base + bridge->slaves[i].size;

		if (aspace != bridge->slaves[i].aspace)
			continue;

		if (cycle != bridge->slaves[i].cycle)
			continue;

		if ((addr >= start) && (addr < end)) {
			offset = addr - bridge->slaves[i].vme_base;
			loc = (u8 *)(bridge->slaves[i].buf_base + offset);
			retval = *loc;

			break;
		}
	}

	fake_lm_check(bridge, addr, aspace, cycle);

	return retval;
}

static u16 fake_vmeread16(struct fake_driver *bridge, unsigned long long addr,
		u32 aspace, u32 cycle)
{
	u16 retval = 0xffff;
	int i;
	unsigned long long start, end, offset;
	u16 *loc;

	for (i = 0; i < FAKE_MAX_SLAVE; i++) {
		if (aspace != bridge->slaves[i].aspace)
			continue;

		if (cycle != bridge->slaves[i].cycle)
			continue;

		start = bridge->slaves[i].vme_base;
		end = bridge->slaves[i].vme_base + bridge->slaves[i].size;

		if ((addr >= start) && ((addr + 1) < end)) {
			offset = addr - bridge->slaves[i].vme_base;
			loc = (u16 *)(bridge->slaves[i].buf_base + offset);
			retval = *loc;

			break;
		}
	}

	fake_lm_check(bridge, addr, aspace, cycle);

	return retval;
}

static u32 fake_vmeread32(struct fake_driver *bridge, unsigned long long addr,
		u32 aspace, u32 cycle)
{
	u32 retval = 0xffffffff;
	int i;
	unsigned long long start, end, offset;
	u32 *loc;

	for (i = 0; i < FAKE_MAX_SLAVE; i++) {
		if (aspace != bridge->slaves[i].aspace)
			continue;

		if (cycle != bridge->slaves[i].cycle)
			continue;

		start = bridge->slaves[i].vme_base;
		end = bridge->slaves[i].vme_base + bridge->slaves[i].size;

		if ((addr >= start) && ((addr + 3) < end)) {
			offset = addr - bridge->slaves[i].vme_base;
			loc = (u32 *)(bridge->slaves[i].buf_base + offset);
			retval = *loc;

			break;
		}
	}

	fake_lm_check(bridge, addr, aspace, cycle);

	return retval;
}

static ssize_t fake_master_read(struct vme_master_resource *image, void *buf,
		size_t count, loff_t offset)
{
	int retval;
	u32 aspace, cycle, dwidth;
	struct vme_bridge *fake_bridge;
	struct fake_driver *priv;
	int i;
	unsigned long long addr;
	unsigned int done = 0;
	unsigned int count32;

	fake_bridge = image->parent;

	priv = fake_bridge->driver_priv;

	i = image->number;

	addr = (unsigned long long)priv->masters[i].vme_base + offset;
	aspace = priv->masters[i].aspace;
	cycle = priv->masters[i].cycle;
	dwidth = priv->masters[i].dwidth;

	spin_lock(&image->lock);

	/* The following code handles VME address alignment. We cannot use
	 * memcpy_xxx here because it may cut data transfers in to 8-bit
	 * cycles when D16 or D32 cycles are required on the VME bus.
	 * On the other hand, the bridge itself assures that the maximum data
	 * cycle configured for the transfer is used and splits it
	 * automatically for non-aligned addresses, so we don't want the
	 * overhead of needlessly forcing small transfers for the entire cycle.
	 */
	if (addr & 0x1) {
		*(u8 *)buf = fake_vmeread8(priv, addr, aspace, cycle);
		done += 1;
		if (done == count)
			goto out;
	}
	if ((dwidth == VME_D16) || (dwidth == VME_D32)) {
		if ((addr + done) & 0x2) {
			if ((count - done) < 2) {
				*(u8 *)(buf + done) = fake_vmeread8(priv,
						addr + done, aspace, cycle);
				done += 1;
				goto out;
			} else {
				*(u16 *)(buf + done) = fake_vmeread16(priv,
						addr + done, aspace, cycle);
				done += 2;
			}
		}
	}

	if (dwidth == VME_D32) {
		count32 = (count - done) & ~0x3;
		while (done < count32) {
			*(u32 *)(buf + done) = fake_vmeread32(priv, addr + done,
					aspace, cycle);
			done += 4;
		}
	} else if (dwidth == VME_D16) {
		count32 = (count - done) & ~0x3;
		while (done < count32) {
			*(u16 *)(buf + done) = fake_vmeread16(priv, addr + done,
					aspace, cycle);
			done += 2;
		}
	} else if (dwidth == VME_D8) {
		count32 = (count - done);
		while (done < count32) {
			*(u8 *)(buf + done) = fake_vmeread8(priv, addr + done,
					aspace, cycle);
			done += 1;
		}

	}

	if ((dwidth == VME_D16) || (dwidth == VME_D32)) {
		if ((count - done) & 0x2) {
			*(u16 *)(buf + done) = fake_vmeread16(priv, addr + done,
					aspace, cycle);
			done += 2;
		}
	}
	if ((count - done) & 0x1) {
		*(u8 *)(buf + done) = fake_vmeread8(priv, addr + done, aspace,
				cycle);
		done += 1;
	}

out:
	retval = count;

	spin_unlock(&image->lock);

	return retval;
}

static void fake_vmewrite8(struct fake_driver *bridge, u8 *buf,
			   unsigned long long addr, u32 aspace, u32 cycle)
{
	int i;
	unsigned long long start, end, offset;
	u8 *loc;

	for (i = 0; i < FAKE_MAX_SLAVE; i++) {
		if (aspace != bridge->slaves[i].aspace)
			continue;

		if (cycle != bridge->slaves[i].cycle)
			continue;

		start = bridge->slaves[i].vme_base;
		end = bridge->slaves[i].vme_base + bridge->slaves[i].size;

		if ((addr >= start) && (addr < end)) {
			offset = addr - bridge->slaves[i].vme_base;
			loc = (u8 *)((void *)bridge->slaves[i].buf_base + offset);
			*loc = *buf;

			break;
		}
	}

	fake_lm_check(bridge, addr, aspace, cycle);

}

static void fake_vmewrite16(struct fake_driver *bridge, u16 *buf,
			    unsigned long long addr, u32 aspace, u32 cycle)
{
	int i;
	unsigned long long start, end, offset;
	u16 *loc;

	for (i = 0; i < FAKE_MAX_SLAVE; i++) {
		if (aspace != bridge->slaves[i].aspace)
			continue;

		if (cycle != bridge->slaves[i].cycle)
			continue;

		start = bridge->slaves[i].vme_base;
		end = bridge->slaves[i].vme_base + bridge->slaves[i].size;

		if ((addr >= start) && ((addr + 1) < end)) {
			offset = addr - bridge->slaves[i].vme_base;
			loc = (u16 *)((void *)bridge->slaves[i].buf_base + offset);
			*loc = *buf;

			break;
		}
	}

	fake_lm_check(bridge, addr, aspace, cycle);

}

static void fake_vmewrite32(struct fake_driver *bridge, u32 *buf,
			    unsigned long long addr, u32 aspace, u32 cycle)
{
	int i;
	unsigned long long start, end, offset;
	u32 *loc;

	for (i = 0; i < FAKE_MAX_SLAVE; i++) {
		if (aspace != bridge->slaves[i].aspace)
			continue;

		if (cycle != bridge->slaves[i].cycle)
			continue;

		start = bridge->slaves[i].vme_base;
		end = bridge->slaves[i].vme_base + bridge->slaves[i].size;

		if ((addr >= start) && ((addr + 3) < end)) {
			offset = addr - bridge->slaves[i].vme_base;
			loc = (u32 *)((void *)bridge->slaves[i].buf_base + offset);
			*loc = *buf;

			break;
		}
	}

	fake_lm_check(bridge, addr, aspace, cycle);

}

static ssize_t fake_master_write(struct vme_master_resource *image, void *buf,
		size_t count, loff_t offset)
{
	int retval = 0;
	u32 aspace, cycle, dwidth;
	unsigned long long addr;
	int i;
	unsigned int done = 0;
	unsigned int count32;

	struct vme_bridge *fake_bridge;
	struct fake_driver *bridge;

	fake_bridge = image->parent;

	bridge = fake_bridge->driver_priv;

	i = image->number;

	addr = bridge->masters[i].vme_base + offset;
	aspace = bridge->masters[i].aspace;
	cycle = bridge->masters[i].cycle;
	dwidth = bridge->masters[i].dwidth;

	spin_lock(&image->lock);

	/* Here we apply for the same strategy we do in master_read
	 * function in order to assure the correct cycles.
	 */
	if (addr & 0x1) {
		fake_vmewrite8(bridge, (u8 *)buf, addr, aspace, cycle);
		done += 1;
		if (done == count)
			goto out;
	}

	if ((dwidth == VME_D16) || (dwidth == VME_D32)) {
		if ((addr + done) & 0x2) {
			if ((count - done) < 2) {
				fake_vmewrite8(bridge, (u8 *)(buf + done),
						addr + done, aspace, cycle);
				done += 1;
				goto out;
			} else {
				fake_vmewrite16(bridge, (u16 *)(buf + done),
						addr + done, aspace, cycle);
				done += 2;
			}
		}
	}

	if (dwidth == VME_D32) {
		count32 = (count - done) & ~0x3;
		while (done < count32) {
			fake_vmewrite32(bridge, (u32 *)(buf + done),
					addr + done, aspace, cycle);
			done += 4;
		}
	} else if (dwidth == VME_D16) {
		count32 = (count - done) & ~0x3;
		while (done < count32) {
			fake_vmewrite16(bridge, (u16 *)(buf + done),
					addr + done, aspace, cycle);
			done += 2;
		}
	} else if (dwidth == VME_D8) {
		count32 = (count - done);
		while (done < count32) {
			fake_vmewrite8(bridge, (u8 *)(buf + done), addr + done,
					aspace, cycle);
			done += 1;
		}

	}

	if ((dwidth == VME_D16) || (dwidth == VME_D32)) {
		if ((count - done) & 0x2) {
			fake_vmewrite16(bridge, (u16 *)(buf + done),
					addr + done, aspace, cycle);
			done += 2;
		}
	}

	if ((count - done) & 0x1) {
		fake_vmewrite8(bridge, (u8 *)(buf + done), addr + done, aspace,
				cycle);
		done += 1;
	}

out:
	retval = count;

	spin_unlock(&image->lock);

	return retval;
}

/*
 * Perform an RMW cycle on the VME bus.
 *
 * Requires a previously configured master window, returns final value.
 */
static unsigned int fake_master_rmw(struct vme_master_resource *image,
		unsigned int mask, unsigned int compare, unsigned int swap,
		loff_t offset)
{
	u32 tmp, base;
	u32 aspace, cycle;
	int i;
	struct fake_driver *bridge;

	bridge = image->parent->driver_priv;

	/* Find the PCI address that maps to the desired VME address */
	i = image->number;

	base = bridge->masters[i].vme_base;
	aspace = bridge->masters[i].aspace;
	cycle = bridge->masters[i].cycle;

	/* Lock image */
	spin_lock(&image->lock);

	/* Read existing value */
	tmp = fake_vmeread32(bridge, base + offset, aspace, cycle);

	/* Perform check */
	if ((tmp && mask) == (compare && mask)) {
		tmp = tmp | (mask | swap);
		tmp = tmp & (~mask | swap);

		/* Write back */
		fake_vmewrite32(bridge, &tmp, base + offset, aspace, cycle);
	}

	/* Unlock image */
	spin_unlock(&image->lock);

	return tmp;
}

/*
 * All 4 location monitors reside at the same base - this is therefore a
 * system wide configuration.
 *
 * This does not enable the LM monitor - that should be done when the first
 * callback is attached and disabled when the last callback is removed.
 */
static int fake_lm_set(struct vme_lm_resource *lm, unsigned long long lm_base,
		u32 aspace, u32 cycle)
{
	int i;
	struct vme_bridge *fake_bridge;
	struct fake_driver *bridge;

	fake_bridge = lm->parent;

	bridge = fake_bridge->driver_priv;

	mutex_lock(&lm->mtx);

	/* If we already have a callback attached, we can't move it! */
	for (i = 0; i < lm->monitors; i++) {
		if (bridge->lm_callback[i] != NULL) {
			mutex_unlock(&lm->mtx);
			pr_err("Location monitor callback attached, can't reset\n");
			return -EBUSY;
		}
	}

	switch (aspace) {
	case VME_A16:
	case VME_A24:
	case VME_A32:
	case VME_A64:
		break;
	default:
		mutex_unlock(&lm->mtx);
		pr_err("Invalid address space\n");
		return -EINVAL;
	}

	bridge->lm_base = lm_base;
	bridge->lm_aspace = aspace;
	bridge->lm_cycle = cycle;

	mutex_unlock(&lm->mtx);

	return 0;
}

/* Get configuration of the callback monitor and return whether it is enabled
 * or disabled.
 */
static int fake_lm_get(struct vme_lm_resource *lm,
		unsigned long long *lm_base, u32 *aspace, u32 *cycle)
{
	struct fake_driver *bridge;

	bridge = lm->parent->driver_priv;

	mutex_lock(&lm->mtx);

	*lm_base = bridge->lm_base;
	*aspace = bridge->lm_aspace;
	*cycle = bridge->lm_cycle;

	mutex_unlock(&lm->mtx);

	return bridge->lm_enabled;
}

/*
 * Attach a callback to a specific location monitor.
 *
 * Callback will be passed the monitor triggered.
 */
static int fake_lm_attach(struct vme_lm_resource *lm, int monitor,
		void (*callback)(void *), void *data)
{
	struct vme_bridge *fake_bridge;
	struct fake_driver *bridge;

	fake_bridge = lm->parent;

	bridge = fake_bridge->driver_priv;

	mutex_lock(&lm->mtx);

	/* Ensure that the location monitor is configured - need PGM or DATA */
	if (bridge->lm_cycle == 0) {
		mutex_unlock(&lm->mtx);
		pr_err("Location monitor not properly configured\n");
		return -EINVAL;
	}

	/* Check that a callback isn't already attached */
	if (bridge->lm_callback[monitor] != NULL) {
		mutex_unlock(&lm->mtx);
		pr_err("Existing callback attached\n");
		return -EBUSY;
	}

	/* Attach callback */
	bridge->lm_callback[monitor] = callback;
	bridge->lm_data[monitor] = data;

	/* Ensure that global Location Monitor Enable set */
	bridge->lm_enabled = 1;

	mutex_unlock(&lm->mtx);

	return 0;
}

/*
 * Detach a callback function forn a specific location monitor.
 */
static int fake_lm_detach(struct vme_lm_resource *lm, int monitor)
{
	u32 tmp;
	int i;
	struct fake_driver *bridge;

	bridge = lm->parent->driver_priv;

	mutex_lock(&lm->mtx);

	/* Detach callback */
	bridge->lm_callback[monitor] = NULL;
	bridge->lm_data[monitor] = NULL;

	/* If all location monitors disabled, disable global Location Monitor */
	tmp = 0;
	for (i = 0; i < lm->monitors; i++) {
		if (bridge->lm_callback[i] != NULL)
			tmp = 1;
	}

	if (tmp == 0)
		bridge->lm_enabled = 0;

	mutex_unlock(&lm->mtx);

	return 0;
}

/*
 * Determine Geographical Addressing
 */
static int fake_slot_get(struct vme_bridge *fake_bridge)
{
	return geoid;
}

static void *fake_alloc_consistent(struct device *parent, size_t size,
		dma_addr_t *dma)
{
	void *alloc = kmalloc(size, GFP_KERNEL);

	if (alloc != NULL)
		*dma = fake_ptr_to_pci(alloc);

	return alloc;
}

static void fake_free_consistent(struct device *parent, size_t size,
		void *vaddr, dma_addr_t dma)
{
	kfree(vaddr);
/*
	dma_free_coherent(parent, size, vaddr, dma);
*/
}

/*
 * Configure CR/CSR space
 *
 * Access to the CR/CSR can be configured at power-up. The location of the
 * CR/CSR registers in the CR/CSR address space is determined by the boards
 * Geographic address.
 *
 * Each board has a 512kB window, with the highest 4kB being used for the
 * boards registers, this means there is a fix length 508kB window which must
 * be mapped onto PCI memory.
 */
static int fake_crcsr_init(struct vme_bridge *fake_bridge)
{
	u32 vstat;
	struct fake_driver *bridge;

	bridge = fake_bridge->driver_priv;

	/* Allocate mem for CR/CSR image */
	bridge->crcsr_kernel = kzalloc(VME_CRCSR_BUF_SIZE, GFP_KERNEL);
	bridge->crcsr_bus = fake_ptr_to_pci(bridge->crcsr_kernel);
	if (bridge->crcsr_kernel == NULL)
		return -ENOMEM;

	vstat = fake_slot_get(fake_bridge);

	pr_info("CR/CSR Offset: %d\n", vstat);

	return 0;
}

static void fake_crcsr_exit(struct vme_bridge *fake_bridge)
{
	struct fake_driver *bridge;

	bridge = fake_bridge->driver_priv;

	kfree(bridge->crcsr_kernel);
}


static int __init fake_init(void)
{
	int retval, i;
	struct list_head *pos = NULL, *n;
	struct vme_bridge *fake_bridge;
	struct fake_driver *fake_device;
	struct vme_master_resource *master_image;
	struct vme_slave_resource *slave_image;
	struct vme_lm_resource *lm;

	/* We need a fake parent device */
	vme_root = __root_device_register("vme", THIS_MODULE);

	/* If we want to support more than one bridge at some point, we need to
	 * dynamically allocate this so we get one per device.
	 */
	fake_bridge = kzalloc(sizeof(struct vme_bridge), GFP_KERNEL);
	if (fake_bridge == NULL) {
		retval = -ENOMEM;
		goto err_struct;
	}

	fake_device = kzalloc(sizeof(struct fake_driver), GFP_KERNEL);
	if (fake_device == NULL) {
		retval = -ENOMEM;
		goto err_driver;
	}

	fake_bridge->driver_priv = fake_device;

	fake_bridge->parent = vme_root;

	fake_device->parent = fake_bridge;

	/* Initialize wait queues & mutual exclusion flags */
	mutex_init(&fake_device->vme_int);
	mutex_init(&fake_bridge->irq_mtx);
	tasklet_init(&fake_device->int_tasklet, fake_VIRQ_tasklet,
			(unsigned long) fake_bridge);

	strcpy(fake_bridge->name, driver_name);

	/* Add master windows to list */
	INIT_LIST_HEAD(&fake_bridge->master_resources);
	for (i = 0; i < FAKE_MAX_MASTER; i++) {
		master_image = kmalloc(sizeof(struct vme_master_resource),
				GFP_KERNEL);
		if (master_image == NULL) {
			retval = -ENOMEM;
			goto err_master;
		}
		master_image->parent = fake_bridge;
		spin_lock_init(&master_image->lock);
		master_image->locked = 0;
		master_image->number = i;
		master_image->address_attr = VME_A16 | VME_A24 | VME_A32 |
			VME_A64;
		master_image->cycle_attr = VME_SCT | VME_BLT | VME_MBLT |
			VME_2eVME | VME_2eSST | VME_2eSSTB | VME_2eSST160 |
			VME_2eSST267 | VME_2eSST320 | VME_SUPER | VME_USER |
			VME_PROG | VME_DATA;
		master_image->width_attr = VME_D16 | VME_D32;
		memset(&master_image->bus_resource, 0,
				sizeof(struct resource));
		master_image->kern_base  = NULL;
		list_add_tail(&master_image->list,
				&fake_bridge->master_resources);
	}

	/* Add slave windows to list */
	INIT_LIST_HEAD(&fake_bridge->slave_resources);
	for (i = 0; i < FAKE_MAX_SLAVE; i++) {
		slave_image = kmalloc(sizeof(struct vme_slave_resource),
				GFP_KERNEL);
		if (slave_image == NULL) {
			retval = -ENOMEM;
			goto err_slave;
		}
		slave_image->parent = fake_bridge;
		mutex_init(&slave_image->mtx);
		slave_image->locked = 0;
		slave_image->number = i;
		slave_image->address_attr = VME_A16 | VME_A24 | VME_A32 |
			VME_A64 | VME_CRCSR | VME_USER1 | VME_USER2 |
			VME_USER3 | VME_USER4;
		slave_image->cycle_attr = VME_SCT | VME_BLT | VME_MBLT |
			VME_2eVME | VME_2eSST | VME_2eSSTB | VME_2eSST160 |
			VME_2eSST267 | VME_2eSST320 | VME_SUPER | VME_USER |
			VME_PROG | VME_DATA;
		list_add_tail(&slave_image->list,
				&fake_bridge->slave_resources);
	}

	/* Add location monitor to list */
	INIT_LIST_HEAD(&fake_bridge->lm_resources);
	lm = kmalloc(sizeof(struct vme_lm_resource), GFP_KERNEL);
	if (lm == NULL) {
		pr_err("Failed to allocate memory for location monitor resource structure\n");
		retval = -ENOMEM;
		goto err_lm;
	}
	lm->parent = fake_bridge;
	mutex_init(&lm->mtx);
	lm->locked = 0;
	lm->number = 1;
	lm->monitors = 4;
	list_add_tail(&lm->list, &fake_bridge->lm_resources);

	fake_bridge->slave_get = fake_slave_get;
	fake_bridge->slave_set = fake_slave_set;
	fake_bridge->master_get = fake_master_get;
	fake_bridge->master_set = fake_master_set;
	fake_bridge->master_read = fake_master_read;
	fake_bridge->master_write = fake_master_write;
	fake_bridge->master_rmw = fake_master_rmw;
	fake_bridge->irq_set = fake_irq_set;
	fake_bridge->irq_generate = fake_irq_generate;
	fake_bridge->lm_set = fake_lm_set;
	fake_bridge->lm_get = fake_lm_get;
	fake_bridge->lm_attach = fake_lm_attach;
	fake_bridge->lm_detach = fake_lm_detach;
	fake_bridge->slot_get = fake_slot_get;
	fake_bridge->alloc_consistent = fake_alloc_consistent;
	fake_bridge->free_consistent = fake_free_consistent;

	pr_info("Board is%s the VME system controller\n",
			(geoid == 1) ? "" : " not");

	pr_info("VME geographical address is set to %d\n", geoid);

	retval = fake_crcsr_init(fake_bridge);
	if (retval) {
		pr_err("CR/CSR configuration failed.\n");
		goto err_crcsr;
	}

	retval = vme_register_bridge(fake_bridge);
	if (retval != 0) {
		pr_err("Chip Registration failed.\n");
		goto err_reg;
	}

	exit_pointer = fake_bridge;

	return 0;

err_reg:
	fake_crcsr_exit(fake_bridge);
err_crcsr:
err_lm:
	/* resources are stored in link list */
	list_for_each_safe(pos, n, &fake_bridge->lm_resources) {
		lm = list_entry(pos, struct vme_lm_resource, list);
		list_del(pos);
		kfree(lm);
	}
err_slave:
	/* resources are stored in link list */
	list_for_each_safe(pos, n, &fake_bridge->slave_resources) {
		slave_image = list_entry(pos, struct vme_slave_resource, list);
		list_del(pos);
		kfree(slave_image);
	}
err_master:
	/* resources are stored in link list */
	list_for_each_safe(pos, n, &fake_bridge->master_resources) {
		master_image = list_entry(pos, struct vme_master_resource,
				list);
		list_del(pos);
		kfree(master_image);
	}

	kfree(fake_device);
err_driver:
	kfree(fake_bridge);
err_struct:
	return retval;

}


static void __exit fake_exit(void)
{
	struct list_head *pos = NULL;
	struct list_head *tmplist;
	struct vme_master_resource *master_image;
	struct vme_slave_resource *slave_image;
	int i;
	struct vme_bridge *fake_bridge;
	struct fake_driver *bridge;

	fake_bridge = exit_pointer;

	bridge = fake_bridge->driver_priv;

	pr_debug("Driver is being unloaded.\n");

	/*
	 *  Shutdown all inbound and outbound windows.
	 */
	for (i = 0; i < FAKE_MAX_MASTER; i++)
		bridge->masters[i].enabled = 0;

	for (i = 0; i < FAKE_MAX_SLAVE; i++)
		bridge->slaves[i].enabled = 0;

	/*
	 *  Shutdown Location monitor.
	 */
	bridge->lm_enabled = 0;

	vme_unregister_bridge(fake_bridge);

	fake_crcsr_exit(fake_bridge);
	/* resources are stored in link list */
	list_for_each_safe(pos, tmplist, &fake_bridge->slave_resources) {
		slave_image = list_entry(pos, struct vme_slave_resource, list);
		list_del(pos);
		kfree(slave_image);
	}

	/* resources are stored in link list */
	list_for_each_safe(pos, tmplist, &fake_bridge->master_resources) {
		master_image = list_entry(pos, struct vme_master_resource,
				list);
		list_del(pos);
		kfree(master_image);
	}

	kfree(fake_bridge->driver_priv);

	kfree(fake_bridge);

	root_device_unregister(vme_root);
}


MODULE_PARM_DESC(geoid, "Set geographical addressing");
module_param(geoid, int, 0);

MODULE_DESCRIPTION("Fake VME bridge driver");
MODULE_LICENSE("GPL");

module_init(fake_init);
module_exit(fake_exit);
OpenPOWER on IntegriCloud