summaryrefslogtreecommitdiffstats
path: root/cf-fsi-test.c
blob: 42bf3a3362c933565e68cfa89dedf30af7c1f46b (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <byteswap.h>
#include <stdint.h>
#include <stdbool.h>
#include <getopt.h>
#include <limits.h>
#include <assert.h>
#include <arpa/inet.h>
#include <errno.h>
#include <time.h>

#include "cf-fsi-fw.h"

#define dsb() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 4" \
				    : : "r" (0) : "memory")

static inline uint8_t readb(void *addr)
{
	dsb();
	return *(volatile uint8_t *)addr;
}

static inline uint16_t readw(void *addr)
{
	dsb();
	return *(volatile uint16_t *)addr;
}

static inline uint32_t readl(void *addr)
{
	dsb();
	return *(volatile uint32_t *)addr;
}

static inline void writeb(uint8_t val, void *addr)
{
	dsb();
	*(volatile uint8_t *)addr = val;
	dsb();
}

static inline void writew(uint16_t val, void *addr)
{
	dsb();
	*(volatile uint16_t *)addr = val;
	dsb();
}

static inline void writel(uint32_t val, void *addr)
{
	dsb();
	*(volatile uint32_t *)addr = val;
	dsb();
}

static inline void writeq(uint64_t val, void *addr)
{
	dsb();
	*(volatile uint64_t *)addr = val;
	dsb();
}

#define SCU_REGS		0x000e2000	/* 1e6e2000 */
#define SCU_COPRO_CTRL	(SCU_REGS + 0x100)
#define SCU_COPRO_RESET		0x00000002
#define SCU_COPRO_CLK_EN		0x00000001
#define SCU_COPRO_SEG0	(SCU_REGS + 0x104) /* 1M */
#define SCU_COPRO_SEG1	(SCU_REGS + 0x108) /* 1M */
#define SCU_COPRO_SEG2	(SCU_REGS + 0x10c) /* 1M */
#define SCU_COPRO_SEG3	(SCU_REGS + 0x110) /* 1M */
#define SCU_COPRO_SEG4	(SCU_REGS + 0x114) /* 1M */
#define SCU_COPRO_SEG5	(SCU_REGS + 0x118) /* 1M */
#define SCU_COPRO_SEG6	(SCU_REGS + 0x11c) /* 1M */
#define SCU_COPRO_SEG7	(SCU_REGS + 0x120) /* 1M */
#define SCU_COPRO_SEG8	(SCU_REGS + 0x124) /* 8M */
#define SCU_COPRO_SEG_SWAP		0x00000001
#define SCU_COPRO_CACHE_CTL	(SCU_REGS + 0x128)
#define SCU_COPRO_CACHE_EN		0x00000001
#define SCU_COPRO_SEG0_CACHE_EN	0x00000002
#define SCU_COPRO_SEG1_CACHE_EN	0x00000004
#define SCU_COPRO_SEG2_CACHE_EN	0x00000008
#define SCU_COPRO_SEG3_CACHE_EN	0x00000010
#define SCU_COPRO_SEG4_CACHE_EN	0x00000020
#define SCU_COPRO_SEG5_CACHE_EN	0x00000040
#define SCU_COPRO_SEG6_CACHE_EN	0x00000080
#define SCU_COPRO_SEG7_CACHE_EN	0x00000100
#define SCU_COPRO_SEG8_CACHE_EN	0x00000200

#define COPRO_ICACHE_FLUSH_REG	0x00008000
#define COPRO_DCACHE_FLUSH_REG	0x00008004

#define SRAM_BASE		0x00120000	/* 1e720000 - actually 36K */
#define SRAM_SIZE		0x00008000

#define GPIO_REGS		0x00180000 /* 1e780000 */
#define GPIO_YZAAAB_CMDSRC0	(GPIO_REGS + 0x170)
#define GPIO_YZAAAB_CMDSRC1	(GPIO_REGS + 0x174)
#define GPIO_QRST_CMDSRC0	(GPIO_REGS + 0x110)
#define GPIO_QRST_CMDSRC1	(GPIO_REGS + 0x114)
#define GPIO_QRST_DATA		(GPIO_REGS + 0x080)
#define GPIO_QRST_DIR		(GPIO_REGS + 0x084)
#define GPIO_QRST_DATARD	(GPIO_REGS + 0x0d0)

#define GPIO_AA_SRC_BIT		0x00010000
#define GPIO_R_SRC_BIT		0x00000100

#define CVIC_BASE		0x000c2000	/* 1e6c2000 */
#define CVIC_EN_REG		0x10
#define CVIC_TRIG_REG		0x18

static void *sysreg;
#define SYSREG_BASE	0x1e600000	/* System registers */
#define SYSREG_SIZE	0x00200000	/* 2M*/

static void *cfmem;
#define CFMEM_BASE	0x9ef00000	/* Reserved memory */
#define CFMEM_SIZE	0x00100000	/* 1M */

#define	FSI_GPIO_CMD_DPOLL      0x2
#define	FSI_GPIO_CMD_EPOLL      0x3
#define	FSI_GPIO_CMD_TERM	0x3f
#define FSI_GPIO_CMD_ABS_AR	0x4
#define FSI_GPIO_CMD_REL_AR	0x5
#define FSI_GPIO_CMD_SAME_AR	0x3	/* but only a 2-bit opcode... */

#define FSI_SLAVE_BASE			0x800
#define FSI_SMODE		0x0	/* R/W: Mode register */
#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 */

#define LAST_ADDR_INVALID		0x1

uint32_t g_last_addr;
bool trace_enabled;

static void open_mem(void)
{
	int fd;

	fd = open("/dev/mem", O_RDWR | O_SYNC);
	if (fd < 0) {
		perror("can't open /dev/mem");
		exit(1);
	}

	sysreg = mmap(0, SYSREG_SIZE, PROT_READ | PROT_WRITE,
		      MAP_SHARED, fd, SYSREG_BASE);
	if (sysreg == MAP_FAILED) {
		perror("can't map system registers via /dev/mem");
		exit(1);
	}

	cfmem = mmap(0, CFMEM_SIZE, PROT_READ | PROT_WRITE,
		     MAP_SHARED, fd, CFMEM_BASE);
	if (cfmem == MAP_FAILED) {
		perror("can't map CF memory via /dev/mem");
		exit(1);
	}
}

static void setup_cf_maps(void)
{
	/*
	 * Note about byteswap setting: the bus is wired backwards,
	 * so setting the byteswap bit actually makes the ColdFire
	 * work "normally" for a BE processor, ie, put the MSB in
	 * the lowest address byte.
	 *
	 * We thus need to set the bit for our main memory which
	 * contains our program code. We create two mappings for
	 * the register, one with each setting.
	 *
	 * Segments 2 and 3 has a "swapped" mapping (BE)
	 * and 6 and 7 have a non-swapped mapping (LE) which allows
	 * us to avoid byteswapping register accesses since the
	 * registers are all LE.
	 */

	/* Setup segment 0 to our memory region */
	writel(CFMEM_BASE | SCU_COPRO_SEG_SWAP, sysreg + SCU_COPRO_SEG0);

	/* Segments 2 and 3 to sysregs with byteswap (SRAM) */
	writel(SYSREG_BASE | SCU_COPRO_SEG_SWAP, sysreg + SCU_COPRO_SEG2);
	writel(SYSREG_BASE | 0x100000 | SCU_COPRO_SEG_SWAP, sysreg + SCU_COPRO_SEG3);

	/* And segment 6 and 7 to our registers */
	writel(SYSREG_BASE, sysreg + SCU_COPRO_SEG6);
	writel(SYSREG_BASE | 0x100000, sysreg + SCU_COPRO_SEG7);

	/* Memory cachable, regs and SRAM not cachable */
	writel(SCU_COPRO_SEG0_CACHE_EN | SCU_COPRO_CACHE_EN,
	       sysreg + SCU_COPRO_CACHE_CTL);
}

static void reset_cf(void)
{
	writel(SCU_COPRO_RESET, sysreg + SCU_COPRO_CTRL);
	usleep(10);
	writel(0, sysreg + SCU_COPRO_CTRL);
}

static void start_cf(void)
{
	writel(SCU_COPRO_CLK_EN, sysreg + SCU_COPRO_CTRL);
}

static void load_cf_code(void)
{
	extern uint8_t cf_code_start, cf_code_end;
	uint16_t sig, fw_vers, api_vers;
	uint32_t fw_options;

	uint8_t *code = &cf_code_start;
	uint8_t *mem = cfmem;

	while(code < &cf_code_end)
		writeb(*(code++), mem++);

	sig = ntohs(readw(cfmem + HDR_OFFSET + HDR_SYS_SIG));
	fw_vers = ntohs(readw(cfmem + HDR_OFFSET + HDR_FW_VERS));
	api_vers = ntohs(readw(cfmem + HDR_OFFSET + HDR_API_VERS));
	fw_options = ntohl(readl(cfmem + HDR_OFFSET + HDR_FW_OPTIONS));

	trace_enabled = !!(fw_options & FW_OPTION_TRACE_EN);

	printf("SYS_SIG=%.4x FW_VERSION=%d API_VERSION=%d.%d (trace %s)\n",
	       sig, fw_vers, api_vers >> 8, api_vers & 0xff,
	       trace_enabled ? "enabled" : "disabled");

}

static void gpio_source_arm(void)
{
	uint32_t val;

	/* ARM = 00 */
	val = readl(sysreg + GPIO_YZAAAB_CMDSRC0);
	val &= ~GPIO_AA_SRC_BIT;
	writel(val, sysreg + GPIO_YZAAAB_CMDSRC0);
	val = readl(sysreg + GPIO_YZAAAB_CMDSRC1);
	val &= ~GPIO_AA_SRC_BIT;
	writel(val, sysreg + GPIO_YZAAAB_CMDSRC1);

	val = readl(sysreg + GPIO_QRST_CMDSRC0);
	val &= ~GPIO_R_SRC_BIT;
	writel(val, sysreg + GPIO_QRST_CMDSRC0);
	val = readl(sysreg + GPIO_QRST_CMDSRC1);
	val &= ~GPIO_R_SRC_BIT;
	writel(val, sysreg + GPIO_QRST_CMDSRC1);
}

static void gpio_source_cf(void)
{
	uint32_t val;

	/* CF = 10 */
	val = readl(sysreg + GPIO_YZAAAB_CMDSRC0);
	val &= ~GPIO_AA_SRC_BIT;
	writel(val, sysreg + GPIO_YZAAAB_CMDSRC0);
	val = readl(sysreg + GPIO_YZAAAB_CMDSRC1);
	val |= GPIO_AA_SRC_BIT;
	writel(val, sysreg + GPIO_YZAAAB_CMDSRC1);

	val = readl(sysreg + GPIO_QRST_CMDSRC0);
	val &= ~GPIO_R_SRC_BIT;
	writel(val, sysreg + GPIO_QRST_CMDSRC0);
	val = readl(sysreg + GPIO_QRST_CMDSRC1);
	val |= GPIO_R_SRC_BIT;
	writel(val, sysreg + GPIO_QRST_CMDSRC1);
}

#ifdef TEST_GPIO

static void copro_gpio_request(void)
{
	int timeout;
	uint32_t val;

	/* Write reqest */
	writeb(ARB_ARM_REQ, sysreg + SRAM_BASE + ARB_REG);

	/* Ring doorbell */
	writel(0x2, sysreg + CVIC_BASE + CVIC_TRIG_REG);

	for (timeout = 0; timeout < 1000000; timeout++) {
		val = readb(sysreg + SRAM_BASE + ARB_REG);
		if (val != ARB_ARM_REQ)
			break;
	}

	/* If it failed, override anyway */
	if (val != ARB_ARM_ACK)
		printf("GPIO request arbitration timeout\n");
}

static void copro_gpio_release(void)
{
	/* Write release */
	writeb(0, sysreg + SRAM_BASE + ARB_REG);

	/* Ring doorbell */
	writel(0x2, sysreg + CVIC_BASE + CVIC_TRIG_REG);
}

static bool no_release = false;
static bool no_switch_own = false;

#define GPIO_TEST_BIT	0x2000
static void test_gpio_stuff(void)
{
	uint32_t cache = readl(sysreg + GPIO_QRST_DATA);
	uint32_t val;
	bool good, first = true;

	if (no_release)
		copro_gpio_request();

	printf("t0: dir=%08x\n", readl(sysreg + GPIO_QRST_DIR));

	for (;;) {
		val = readl(sysreg + GPIO_QRST_DATARD);
		good = ((val ^ cache) & GPIO_TEST_BIT) == 0;
		printf("t1: cache=%08x reg=%08x %c\n", cache, val, good ? ' ' : '$');

		if (!no_release)
			copro_gpio_request();

		writel(0, sysreg + SRAM_BASE + 0x3c);

		if (!no_switch_own || first) {
			first = false;
			val = readl(sysreg + GPIO_QRST_CMDSRC1);
			val &= ~GPIO_R_SRC_BIT;
			writel(val, sysreg + GPIO_QRST_CMDSRC1);
			val = readl(sysreg + GPIO_QRST_CMDSRC0);
			val &= ~GPIO_R_SRC_BIT;
			writel(val, sysreg + GPIO_QRST_CMDSRC0);
		}

		cache ^= GPIO_TEST_BIT;
		writel(cache, sysreg + GPIO_QRST_DATA);

		do {
			val = readl(sysreg + GPIO_QRST_DATARD);
			good = ((val ^ cache) & GPIO_TEST_BIT) == 0;
			printf("t2: cache=%08x reg=%08x %c\n", cache, val, good ? '*' : '!');
		} while(!good);

		if (!no_switch_own) {
			val = readl(sysreg + GPIO_QRST_CMDSRC1);
			val |= GPIO_R_SRC_BIT;
			writel(val, sysreg + GPIO_QRST_CMDSRC1);
			val = readl(sysreg + GPIO_QRST_CMDSRC0);
			val &= ~GPIO_R_SRC_BIT;
			writel(val, sysreg + GPIO_QRST_CMDSRC0);
		}

		if (!no_release)
			copro_gpio_release();

		usleep(1);
		printf("t3: cache=%08x reg=%08x\n---\n",
		       cache, readl(sysreg + GPIO_QRST_DATARD));

		sleep(1);
	}
}

#endif

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

/**
 * crc4 - calculate the 4-bit crc of a value.
 * @crc:  starting crc4
 * @x:    value to checksum
 * @bits: number of bits in @x to checksum
 *
 * Returns the crc4 value of @x, using polynomial 0b10111.
 *
 * The @x value is treated as left-aligned, and bits above @bits are ignored
 * in the crc calculations.
 */
static uint8_t crc4(uint8_t c, uint64_t x, int bits)
{
	int i;

	/* mask off anything above the top bit */
	x &= (1ull << bits) - 1;

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

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

	return c;
}

struct fsi_gpio_msg {
	uint64_t	msg;
	uint8_t		bits;
};

static void msg_push_bits(struct fsi_gpio_msg *msg, uint64_t data, int bits)
{
	msg->msg <<= bits;
	msg->msg |= data & ((1ull << bits) - 1);
	msg->bits += bits;
}

static void msg_push_crc(struct fsi_gpio_msg *msg)
{
	uint8_t crc;
	int top;

	top = msg->bits & 0x3;

	/* start bit, and any non-aligned top bits */
	crc = crc4(0, 1 << top | msg->msg >> (msg->bits - top), top + 1);

	/* aligned bits */
	crc = crc4(crc, msg->msg, msg->bits - top);

	msg_push_bits(msg, crc, 4);
}

static void msg_finish_cmd(struct fsi_gpio_msg *cmd)
{
	/* Left align message */
	cmd->msg <<= (64 - cmd->bits);
}

static bool check_same_address(int id, uint32_t addr)
{
	/* this will also handle LAST_ADDR_INVALID */
	return g_last_addr == (((id & 0x3) << 21) | (addr & ~0x3));
}

static bool check_relative_address(int id, uint32_t addr, uint32_t *rel_addrp)
{
	uint32_t last_addr = g_last_addr;
	int32_t rel_addr;

	if (last_addr == LAST_ADDR_INVALID)
		return false;

	/* We may be in 23-bit addressing mode, which uses the id as the
	 * top two address bits. So, if we're referencing a different ID,
	 * use absolute addresses.
	 */
	if (((last_addr >> 21) & 0x3) != id)
		return false;

	/* remove the top two bits from any 23-bit addressing */
	last_addr &= (1 << 21) - 1;

	/* We know that the addresses are limited to 21 bits, so this won't
	 * overflow the signed rel_addr */
	rel_addr = addr - last_addr;
	if (rel_addr > 255 || rel_addr < -256)
		return false;

	*rel_addrp = (uint32_t)rel_addr;

	return true;
}

static void last_address_update(int id, bool valid, uint32_t addr)
{
	if (!valid)
		g_last_addr = LAST_ADDR_INVALID;
	else
		g_last_addr = ((id & 0x3) << 21) | (addr & ~0x3);
}

static void build_ar_command(struct fsi_gpio_msg *cmd, uint8_t id,
			     uint32_t addr, size_t size, const void *data)
{
	int i, addr_bits, opcode_bits;
	bool write = !!data;
	uint8_t ds, opcode;
	uint32_t rel_addr;

	cmd->bits = 0;
	cmd->msg = 0;

	/* we have 21 bits of address max */
	addr &= ((1 << 21) - 1);

	/* cmd opcodes are variable length - SAME_AR is only two bits */
	opcode_bits = 3;

	if (check_same_address(id, addr)) {
		/* we still address the byte offset within the word */
		addr_bits = 2;
		opcode_bits = 2;
		opcode = FSI_GPIO_CMD_SAME_AR;

	} else if (check_relative_address(id, addr, &rel_addr)) {
		/* 8 bits plus sign */
		addr_bits = 9;
		addr = rel_addr;
		opcode = FSI_GPIO_CMD_REL_AR;

	} else {
		addr_bits = 21;
		opcode = FSI_GPIO_CMD_ABS_AR;
	}

	/*
	 * The read/write size is encoded in the lower bits of the address
	 * (as it must be naturally-aligned), and the following ds bit.
	 *
	 *	size	addr:1	addr:0	ds
	 *	1	x	x	0
	 *	2	x	0	1
	 *	4	0	1	1
	 *
	 */
	ds = size > 1 ? 1 : 0;
	addr &= ~(size - 1);
	if (size == 4)
		addr |= 1;

	msg_push_bits(cmd, id, 2);
	msg_push_bits(cmd, opcode, opcode_bits);
	msg_push_bits(cmd, write ? 0 : 1, 1);
	msg_push_bits(cmd, addr, addr_bits);
	msg_push_bits(cmd, ds, 1);
	for (i = 0; write && i < size; i++)
		msg_push_bits(cmd, ((uint8_t *)data)[i], 8);

	msg_push_crc(cmd);
	msg_finish_cmd(cmd);
}

static void dump_stuff(void)
{
	int i;

	printf("CMD:%08x RTAG=%02x RCRC=%02x RDATA=%02x #INT=%08x\n",
	       ntohl(readl(sysreg + SRAM_BASE + CMD_STAT_REG)),
	       readb(sysreg + SRAM_BASE + STAT_RTAG),
	       readb(sysreg + SRAM_BASE + STAT_RCRC),
	       ntohl(readl(sysreg + SRAM_BASE + RSP_DATA)),
	       ntohl(readl(sysreg + SRAM_BASE + INT_CNT)));

	for (i = 0; trace_enabled && i < 128; i++) {
		uint8_t v = readb(sysreg + SRAM_BASE + TRACEBUF + i);
		printf("%02x ", v);
		if ((i % 16) == 15)
			printf("\n");
		if (v == TR_END)
			break;
	}
	if (i % 16)
		printf("\n");
}

static int do_command(uint32_t op)
{
	uint32_t timeout = 100000;
	uint8_t stat;

	/* Send command */
	writel(htonl(op), sysreg + SRAM_BASE + CMD_STAT_REG);

	/* Ring doorbell */
	writel(0x2, sysreg + CVIC_BASE + CVIC_TRIG_REG);

	/* Wait for status to indicate completion (or error) */
	do {
		if (timeout-- == 0) {
			printf("Timeout !\n");

			dump_stuff();
			return -ETIMEDOUT;
		}
		stat = readb(sysreg + SRAM_BASE + CMD_STAT_REG);
	} while(stat < STAT_COMPLETE || stat == 0xff);

	if (stat == STAT_COMPLETE)
		return 0;
	switch(stat) {
	case STAT_ERR_INVAL_CMD:
		return -EINVAL;
	case STAT_ERR_INVAL_IRQ:
		return -EIO;
	case STAT_ERR_MTOE:
		return -ETIMEDOUT;
	}
	return -ENXIO;
}

int test_break(void)
{
	printf("Sending break..\n");
	return do_command(CMD_BREAK);
}

int test_rw(uint32_t addr, bool is_write, uint32_t *data)
{
	struct fsi_gpio_msg cmd;
	uint32_t op, resp = 0, crc;
	uint8_t rtag, rcrc, ack;
	uint32_t be_data;
	int rc;

	if (is_write) {
		be_data = htonl(*data);
		build_ar_command(&cmd, 0, addr, 4, &be_data);
	} else
		build_ar_command(&cmd, 0, addr, 4, NULL);

	/* Store message into SRAM */
	writel(htonl(cmd.msg >> 32), sysreg + SRAM_BASE + CMD_DATA);
	writel(htonl(cmd.msg & 0xffffffff), sysreg + SRAM_BASE + CMD_DATA + 4);

	op = CMD_COMMAND;
	op |= cmd.bits  << CMD_REG_CLEN_SHIFT;
	if (!is_write)
		op |= 32 << CMD_REG_RLEN_SHIFT;

	rc = do_command(op);
	if (rc) {
		printf("Error %d sending command\n", rc);
		return rc;
	}

	if (!is_write)
		resp = ntohl(readl(sysreg + SRAM_BASE + RSP_DATA));
	rtag = readb(sysreg + SRAM_BASE + STAT_RTAG);
	rcrc = readb(sysreg + SRAM_BASE + STAT_RCRC);
	ack = rtag & 3;

	/* we have a whole message now; check CRC */
	crc = crc4(0, 1, 1);
	crc = crc4(crc, rtag, 4);
	if (ack == 0 && !is_write)
		crc = crc4(crc, resp, 32);
	crc = crc4(crc, rcrc, 4);
	if (crc) {
		last_address_update(0, false, 0);
		printf("BAD CRC !!!\n");
		dump_stuff();
		return -ETIMEDOUT;
	}
	if (ack) {
		printf("FSI error 0x%x\n", rtag & 3);
		last_address_update(0, false, 0);
		dump_stuff();
		return -EIO;
	}
	last_address_update(0, true, addr);
	if (data && !is_write)
		*data = resp;
	else
		dump_stuff();
	return 0;
}

/* 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;
}


void bench(void)
{
	struct timespec t0, t1;
	uint32_t val, orig;
	uint64_t tns0, tns1;
	int i, rc;

	printf("Bench...\n");
	rc = test_rw(0, false, &orig);
	if (rc)
		return;
	clock_gettime(CLOCK_MONOTONIC, &t0);
	for (i = 0; i < (0x100000 / 4); i++) {
		rc = test_rw(0, false, &val);
		if (rc) {
			printf("Failed after %d iterations\n", i);
			break;
		}
		if (val != orig) {
			printf("mismatch ! %08x vs. %08x\n", val, orig);
			break;
		}
	}
	printf("\n");
	clock_gettime(CLOCK_MONOTONIC, &t1);
	tns0 = t0.tv_sec * 1000000000ull + t0.tv_nsec;
	tns1 = t1.tv_sec * 1000000000ull + t1.tv_nsec;
	fprintf(stderr, "Spent: %lld ms\n", (tns1 - tns0) / 1000000);
}


int main(int argc, char *argv[])
{
	uint32_t val;

	open_mem();

	printf("Resetting ColdFire...\n");
	reset_cf();

	printf("Setting up and starting ColdFire...\n");

	setup_cf_maps();

	load_cf_code();

	gpio_source_cf();

	/* Clear SRAM */
	memset(sysreg + SRAM_BASE, 0x00, 0x1000);
	dsb();

	/* Start ColdFire */
	start_cf();

	/* Wait for status register to say command complete */
	do {
		val = readl(sysreg + SRAM_BASE + CF_STARTED);
	} while (val == 0x00);

	/* Configure echo & send delay */
	writeb(16, sysreg + SRAM_BASE + ECHO_DLY_REG);
	writeb(16, sysreg + SRAM_BASE + SEND_DLY_REG);

	/* Enable interrupt */
	writel(0x2, sysreg + CVIC_BASE + CVIC_EN_REG);

	last_address_update(0, false, 0);

	/* Send break */
	test_break();

	/* Test read */
	test_rw(0, false, NULL);
	test_rw(4, false, NULL);

	/* Read smode */
	test_rw(FSI_SLAVE_BASE + FSI_SMODE, false, &val);
	dump_stuff();
	printf("old smode: %08x\n", val);

	/* 6 seems to be the "sweet spot" for performance */
#define ECHO_SEND_DELAY		6

	/* Change it to 2,2 */
	val = FSI_SMODE_WSC | FSI_SMODE_ECRC
		| fsi_smode_echodly(ECHO_SEND_DELAY - 1)
		| fsi_smode_senddly(ECHO_SEND_DELAY - 1)
		| fsi_smode_lbcrr(0x8);
	printf("writing smode 0x%08x..\n", val);
	test_rw(FSI_SLAVE_BASE + FSI_SMODE, true, &val);
	do_command(CMD_IDLE_CLOCKS | (16 << CMD_REG_CLEN_SHIFT));
	writeb(ECHO_SEND_DELAY, sysreg + SRAM_BASE + ECHO_DLY_REG);
	writeb(ECHO_SEND_DELAY, sysreg + SRAM_BASE + SEND_DLY_REG);

	test_rw(FSI_SLAVE_BASE + FSI_SMODE, false, &val);
	printf("new smode: %08x\n", val);

#ifdef TEST_GPIO
	test_gpio_stuff();
#else
	bench();
#endif
	gpio_source_arm();

	return 0;
}


OpenPOWER on IntegriCloud