summaryrefslogtreecommitdiffstats
path: root/bsps/shared/grlib/spw/spwtdp.c
blob: c24ceccddff7240f342d140a80b30814ee43e568 (plain) (blame)
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
/* SPDX-License-Identifier: BSD-2-Clause */

/*  SPWTDP - SpaceWire Time Distribution Protocol. The driver provides
 *  device discovery and interrupt management.
 *
 *  COPYRIGHT (c) 2017.
 *  Cobham Gaisler AB
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <rtems.h>
#include <rtems/bspIo.h>
#include <drvmgr/drvmgr.h>
#include <grlib/ambapp.h>
#include <grlib/ambapp_bus.h>
#include <bsp.h>
#include <grlib/spwtdp.h>

#include <grlib/grlib_impl.h>

/*#define STATIC*/
#define STATIC static

/*#define INLINE*/
#define INLINE inline

/*#define UNUSED*/
#define UNUSED __attribute__((unused))

#define DEBUG 1

#ifdef DEBUG
#define DBG(x...) printf(x)
#else
#define DBG(x...) 
#endif

/* Memory and HW Registers Access routines. All 32-bit access routines */
#define REG_WRITE(addr, val) \
	(*(volatile unsigned int *)(addr) = (unsigned int)(val))
#define REG_READ(addr) (*(volatile unsigned int *)(addr))

/*
 * Configuration register definitions
 * DEFINED in header
 */

/*
 * Control register definitions
 * DEFINED in header
 */

/*
 * TSTX Control register definitions
 */
#define TSTXCTRL_TSTC (0xff<<TSTXCTRL_TSTC_BIT)
#define ETCTRL_PF (0xffff<<ETCTRL_PF_BIT)

#define TSTXCTRL_TSTC_BIT 24
#define ETCTRL_PF_BIT 0

#define DEVNAME_LEN 11
/* Private structure of SPWTDP driver. */
struct spwtdp_priv {
	char devname[DEVNAME_LEN];
	struct drvmgr_dev *dev; /* Device */
	struct spwtdp_regs *regs;
	int open;
	int index;
	int initiator; /* Initiator configured */
	int target; /* Target configured */
	int freq; /* Frequency configured */

	/* Spin-lock ISR protection */
	SPIN_DECLARE(devlock);

	/* Driver semaphore */
	rtems_id sem;
	spwtdp_isr_t isr;
	void * isr_arg;
};
int spwtdp_count = 0;
static struct spwtdp_priv *priv_tab[SPWTDP_MAX];


STATIC void spwtdp_isr(void *data);
STATIC int spwtdp_hw_reset(struct spwtdp_priv *priv);
STATIC int spwtdp_init2(struct drvmgr_dev *dev);

struct drvmgr_drv_ops spwtdp_ops =
{
	{NULL, spwtdp_init2, NULL, NULL},
	NULL,
	NULL
};

struct amba_dev_id spwtdp_ids[] =
{
	{VENDOR_GAISLER, GAISLER_SPWTDP},
	{0, 0}	/* Mark end of table */
};

struct amba_drv_info spwtdp_drv_info =
{
	{
		DRVMGR_OBJ_DRV,			/* Driver */
		NULL,				/* Next driver */
		NULL,				/* Device list */
		DRIVER_AMBAPP_GAISLER_SPWTDP_ID,/* Driver ID */
		"SPWTDP_DRV",			/* Driver Name */
		DRVMGR_BUS_TYPE_AMBAPP,		/* Bus Type */
		&spwtdp_ops,
		NULL,				/* Funcs */
		0,				/* No devices yet */
		sizeof(struct spwtdp_priv),	/* Let DrvMgr allocate priv */
	},
	&spwtdp_ids[0]
};

/* Register the SPWTDP Driver */
void spwtdp_register_drv(void)
{
	DBG("Registering SPWTDP driver\n");
	drvmgr_drv_register(&spwtdp_drv_info.general);
}

STATIC int spwtdp_init(struct spwtdp_priv *priv)
{
	struct amba_dev_info *ainfo = priv->dev->businfo;
	struct ambapp_apb_info *apb;

	/* Get device information from AMBA PnP information */
	if (ainfo == NULL) {
		return -1;
	}
	apb = ainfo->info.apb_slv;
	priv->regs = (struct spwtdp_regs *)apb->start;

	spwtdp_hw_reset(priv);
	/* Only support 56 bits counter */
	if (REG_READ(&priv->regs->dat_ctrl) != 0x2f00) {
		DBG("SPWTDP only supports 56 bit precission counters.\n");
		return -1;
	}
	DBG("SPWTDP driver initialized\n");

	return 0;
}

/*** INTERFACE TO DRIVER MANAGER ***/
STATIC int spwtdp_init2(struct drvmgr_dev *dev)
{
	int status;
	struct spwtdp_priv *priv;

	DBG("SPWTDP[%d] on bus %s\n", dev->minor_drv, dev->parent->dev->name);

	if (spwtdp_count >= SPWTDP_MAX)
		return DRVMGR_ENORES;

	priv = dev->priv;
	if (priv == NULL)
		return DRVMGR_NOMEM;

	/* Register device */
	priv->dev = dev;
	priv->index = spwtdp_count;
	priv_tab[priv->index] = priv;
	snprintf(priv->devname, DEVNAME_LEN, "spwtdp%01u", priv->index);
	spwtdp_count++;

	/* Initialize Semaphore */
	if (rtems_semaphore_create(
		rtems_build_name('S', 'T', 'P', '0' + priv->index), 1,
		RTEMS_FIFO | RTEMS_SIMPLE_BINARY_SEMAPHORE | \
		RTEMS_NO_INHERIT_PRIORITY | RTEMS_LOCAL | \
		RTEMS_NO_PRIORITY_CEILING, 0, &priv->sem) != RTEMS_SUCCESSFUL) {
		priv->sem = RTEMS_ID_NONE;
		return DRVMGR_FAIL;
	}

	/* Initialize SPWTDP Hardware */
	status = spwtdp_init(priv);
	if (status) {
		printk("Failed to initialize spwtdp driver %d\n", status);
		return -1;
	}

	return DRVMGR_OK;
}

/* Hardware Reset of SPWTDP */
STATIC int spwtdp_hw_reset(struct spwtdp_priv *priv)
{
	int i = 1000;
	SPIN_IRQFLAGS(irqflags);

	SPIN_LOCK_IRQ(&priv->devlock, irqflags);

	/* Clear interrupts */
	REG_WRITE(&priv->regs->ists, SPWTDP_IRQ_WCLEAR);

	/* Reset the SPWTDP core */
	REG_WRITE(&priv->regs->conf[0], CONF0_RS);

	/* Wait for reset */
	while ((REG_READ(&priv->regs->conf[0]) & CONF0_RS) && (i > 0)) {
		i--;
	}

	SPIN_UNLOCK_IRQ(&priv->devlock, irqflags);

	return ((i > 0)? SPWTDP_ERR_OK : SPWTDP_ERR_ERROR);
}

int spwtdp_reset(void *spwtdp)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	/* Check priv and unregister isr */
	int ret = spwtdp_isr_unregister(spwtdp);
	if (ret != SPWTDP_ERR_OK)
		return ret;

	priv->initiator=0;
	priv->target=0;
	priv->freq=0;

	return spwtdp_hw_reset(priv);
}

void *spwtdp_open(int dev_no)
{
	struct spwtdp_priv *priv;

	if (dev_no >= spwtdp_count)
		return NULL;

	/* Get Device */
	priv = priv_tab[dev_no];
	if ((priv == NULL)||(priv->open == 1)) {
		return NULL;
	}

	/* Set initial state of software */
	priv->open = 1;

	return priv;
}

int spwtdp_close(void *spwtdp)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	/* Check priv and reset core */
	int ret = spwtdp_reset(spwtdp);
	if (ret != SPWTDP_ERR_OK)
		return ret;

	priv->open = 0;
	return SPWTDP_ERR_OK;
}

int spwtdp_freq_setup(void *spwtdp, uint32_t fsinc, uint32_t cv, uint8_t etinc)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	/* Check priv */
	if (priv == NULL)
		return SPWTDP_ERR_NOINIT;

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Take SPWTDP lock - Wait until we get semaphore */
	if (rtems_semaphore_obtain(priv->sem, RTEMS_WAIT, RTEMS_NO_TIMEOUT)
		!= RTEMS_SUCCESSFUL)
		return SPWTDP_ERR_ERROR;

	REG_WRITE(&priv->regs->conf[1], fsinc & CONF1_FSINC);
	REG_WRITE(&priv->regs->conf[2],
		((cv<<CONF2_CV_BIT) & CONF2_CV) |
		((uint32_t)etinc & CONF2_ETINC));

	rtems_semaphore_release(priv->sem);
	priv->freq = 1;

	return SPWTDP_ERR_OK;
}

#define CONF0_INI_MASK (CONF0_EP|CONF0_ET|CONF0_SP|CONF0_SE|CONF0_LE| \
		CONF0_TD)
int spwtdp_initiator_conf(void *spwtdp, uint8_t mapping, uint32_t options)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	/* Check priv */
	if (priv == NULL)
		return SPWTDP_ERR_NOINIT;

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check if configured as target */
	if (priv->target == 1)
		return SPWTDP_ERR_EINVAL;

	/* Take SPWTDP lock - Wait until we get semaphore */
	if (rtems_semaphore_obtain(priv->sem, RTEMS_WAIT, RTEMS_NO_TIMEOUT)
		!= RTEMS_SUCCESSFUL)
		return SPWTDP_ERR_ERROR;

	unsigned int conf0 = REG_READ(&priv->regs->conf[0]);
	conf0 &= ~(CONF0_INI_MASK|CONF0_MAP);
	REG_WRITE(&priv->regs->conf[0],
			conf0 | (options & CONF0_INI_MASK) |
			(((uint32_t)mapping << CONF0_MAP_BIT) & CONF0_MAP));

	priv->initiator = 1;

	rtems_semaphore_release(priv->sem);

	return SPWTDP_ERR_OK;
}

#define CONF0_TAR_MASK (CONF0_JE|CONF0_ST|CONF0_EP|CONF0_ET|CONF0_SP| \
		CONF0_SE|CONF0_LE|CONF0_TD|CONF0_ME)
int spwtdp_target_conf(void *spwtdp, uint8_t mapping, uint32_t options)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	/* Check priv */
	if (priv == NULL)
		return SPWTDP_ERR_NOINIT;

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check if configured as initiator */
	if (priv->initiator == 1)
		return SPWTDP_ERR_EINVAL;

	/* Take SPWTDP lock - Wait until we get semaphore */
	if (rtems_semaphore_obtain(priv->sem, RTEMS_WAIT, RTEMS_NO_TIMEOUT)
		!= RTEMS_SUCCESSFUL)
		return SPWTDP_ERR_ERROR;

	unsigned int conf0 = REG_READ(&priv->regs->conf[0]);
	conf0 &= ~(CONF0_TAR_MASK|CONF0_MAP);
	REG_WRITE(&priv->regs->conf[0],
			conf0 | (options & CONF0_TAR_MASK) |
			(((uint32_t)mapping << CONF0_MAP_BIT) & CONF0_MAP));

	priv->initiator = 1;

	rtems_semaphore_release(priv->sem);

	return SPWTDP_ERR_OK;
}

int spwtdp_initiator_int_conf(void *spwtdp, uint8_t stm, uint8_t inrx,
			      uint8_t intx)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	/* Check priv */
	if (priv == NULL)
		return SPWTDP_ERR_NOINIT;

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check if configured as initiator */
	if (priv->initiator != 1)
		return SPWTDP_ERR_EINVAL;

	REG_WRITE(&priv->regs->conf[3],
			(((uint32_t)stm << CONF3_STM_BIT) & CONF3_STM) |
			(((uint32_t)inrx << CONF3_INRX_BIT) & CONF3_INRX) |
			(((uint32_t)intx << CONF3_INTX_BIT) & CONF3_INTX));

	return SPWTDP_ERR_OK;
}

int spwtdp_target_int_conf(void *spwtdp, uint8_t inrx, uint8_t intx,
			   uint32_t options)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	/* Check priv */
	if (priv == NULL) {
		return SPWTDP_ERR_NOINIT;
	}

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check if configured as target */
	if (priv->target != 1)
		return SPWTDP_ERR_EINVAL;

	REG_WRITE(&priv->regs->conf[3],
			(options & CONF3_DI) |
			(((uint32_t)inrx << CONF3_INRX_BIT) & CONF3_INRX) |
			(((uint32_t)intx << CONF3_INTX_BIT) & CONF3_INTX));

	return SPWTDP_ERR_OK;
}

int spwtdp_initiator_enable(void *spwtdp)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	/* Check priv */
	if (priv == NULL) {
		return SPWTDP_ERR_NOINIT;
	}

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check if configured as initiator */
	if (priv->initiator != 1)
		return SPWTDP_ERR_EINVAL;

	/* Check if frequency is configured */
	if (priv->freq != 1)
		return SPWTDP_ERR_EINVAL;

	/* Take SPWTDP lock - Wait until we get semaphore */
	if (rtems_semaphore_obtain(priv->sem, RTEMS_WAIT, RTEMS_NO_TIMEOUT)
		!= RTEMS_SUCCESSFUL)
		return SPWTDP_ERR_ERROR;

	unsigned int conf0 = REG_READ(&priv->regs->conf[0]);
	REG_WRITE(&priv->regs->conf[0], conf0 | CONF0_TE);

	rtems_semaphore_release(priv->sem);

	return SPWTDP_ERR_OK;
}

int spwtdp_target_enable(void *spwtdp)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	/* Check priv */
	if (priv == NULL)
		return SPWTDP_ERR_NOINIT;

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check if configured as target */
	if (priv->target != 1)
		return SPWTDP_ERR_EINVAL;

	/* Check if frequency is configured */
	if (priv->freq != 1)
		return SPWTDP_ERR_EINVAL;

	/* Take SPWTDP lock - Wait until we get semaphore */
	if (rtems_semaphore_obtain(priv->sem, RTEMS_WAIT, RTEMS_NO_TIMEOUT)
		!= RTEMS_SUCCESSFUL)
		return SPWTDP_ERR_ERROR;

	unsigned int conf0 = REG_READ(&priv->regs->conf[0]);
	REG_WRITE(&priv->regs->conf[0], conf0 | CONF0_RE);

	rtems_semaphore_release(priv->sem);

	return SPWTDP_ERR_OK;
}

int spwtdp_initiator_disable(void *spwtdp)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	/* Check priv */
	if (priv == NULL)
		return SPWTDP_ERR_NOINIT;

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Take SPWTDP lock - Wait until we get semaphore */
	if (rtems_semaphore_obtain(priv->sem, RTEMS_WAIT, RTEMS_NO_TIMEOUT)
		!= RTEMS_SUCCESSFUL)
		return SPWTDP_ERR_ERROR;

	unsigned int conf0 = REG_READ(&priv->regs->conf[0]);
	REG_WRITE(&priv->regs->conf[0], conf0 & ~(CONF0_TE));

	rtems_semaphore_release(priv->sem);

	return SPWTDP_ERR_OK;
}

int spwtdp_target_disable(void *spwtdp)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	/* Check priv */
	if (priv == NULL)
		return SPWTDP_ERR_NOINIT;

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Take SPWTDP lock - Wait until we get semaphore */
	if (rtems_semaphore_obtain(priv->sem, RTEMS_WAIT, RTEMS_NO_TIMEOUT)
		!= RTEMS_SUCCESSFUL)
		return SPWTDP_ERR_ERROR;

	unsigned int conf0 = REG_READ(&priv->regs->conf[0]);
	REG_WRITE(&priv->regs->conf[0], conf0 & ~(CONF0_RE));

	rtems_semaphore_release(priv->sem);

	return SPWTDP_ERR_OK;
}

/* Get and Clear status */
int spwtdp_status(void *spwtdp, uint32_t *sts, uint32_t clrmask)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	if (priv == NULL) {
		/* SPWTDP not initialized */
		return SPWTDP_ERR_NOINIT;
	}

	unsigned int status = REG_READ(&priv->regs->stat[0]);
	REG_WRITE(&priv->regs->stat[0], status & clrmask);

	if (sts != NULL)
		*sts = status;

	return SPWTDP_ERR_OK;
}

/* Get and Clear interrupts */
int spwtdp_interrupt_status(void *spwtdp, uint32_t *sts, uint32_t clrmask)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;
	SPIN_IRQFLAGS(irqflags);

	if (priv == NULL) {
		/* SPWTDP not initialized */
		return SPWTDP_ERR_NOINIT;
	}

	SPIN_LOCK_IRQ(&priv->devlock, irqflags);
	unsigned int status = REG_READ(&priv->regs->ists);
	REG_WRITE(&priv->regs->ists, status & clrmask);
	SPIN_UNLOCK_IRQ(&priv->devlock, irqflags);

	if (sts != NULL)
		*sts = status;

	return SPWTDP_ERR_OK;
}

/* Unmask interrupts */
int spwtdp_interrupt_unmask(void *spwtdp, uint32_t irqmask)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	if (priv == NULL) {
		/* SPWTDP not initialized */
		return SPWTDP_ERR_NOINIT;
	}

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	unsigned int ctrl = REG_READ(&priv->regs->ien);
	REG_WRITE(&priv->regs->ien, ctrl | irqmask);

	return SPWTDP_ERR_OK;
}

/* Mask interrupts */
int spwtdp_interrupt_mask(void *spwtdp, uint32_t irqmask)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	if (priv == NULL) {
		/* SPWTDP not initialized */
		return SPWTDP_ERR_NOINIT;
	}

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	unsigned int ctrl = REG_READ(&priv->regs->ien);
	REG_WRITE(&priv->regs->ien, ctrl & ~(irqmask));

	return SPWTDP_ERR_OK;
}

int spwtdp_isr_register(void *spwtdp, spwtdp_isr_t func, void *data)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;
	SPIN_ISR_IRQFLAGS(irqflags);

	if (priv == NULL) {
		/* SPWTDP not initialized */
		return SPWTDP_ERR_NOINIT;
	}

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check isr */
	if (func == NULL) {
		/* No ISR */
		return SPWTDP_ERR_EINVAL;
	}

	priv->isr = func;
	priv->isr_arg = data;

	/* Register and Enable Interrupt at Interrupt controller */
	drvmgr_interrupt_register(priv->dev, 0, "spwtdp", spwtdp_isr, priv);

	/* Enable AMBA Interrupts */
	SPIN_LOCK(&priv->devlock, irqflags);
	unsigned int cfg0 = REG_READ(&priv->regs->conf[0]);
	REG_WRITE(&priv->regs->conf[0], cfg0 | CONF0_AE);
	SPIN_UNLOCK(&priv->devlock, irqflags);

	return SPWTDP_ERR_OK;
}

int spwtdp_isr_unregister(void *spwtdp)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;
	SPIN_ISR_IRQFLAGS(irqflags);

	/* Disable IRQS (and check for priv!=NULL) */
	int ret=spwtdp_interrupt_mask(spwtdp, SPWTDP_IRQ_WCLEAR);
	if (ret != SPWTDP_ERR_OK)
		return ret;

	/* Disable AMBA Interrupts */
	SPIN_LOCK(&priv->devlock, irqflags);
	unsigned int cfg0 = REG_READ(&priv->regs->conf[0]);
	REG_WRITE(&priv->regs->conf[0], cfg0 & ~(CONF0_AE));
	SPIN_UNLOCK(&priv->devlock, irqflags);

	/* Disable Interrupt at Interrupt controller */
	drvmgr_interrupt_unregister(priv->dev, 0, spwtdp_isr, priv);

	/* Unregister isr */
	priv->isr = NULL;
	priv->isr_arg = NULL;

	return SPWTDP_ERR_OK;
}

STATIC void spwtdp_isr(void *arg)
{
	struct spwtdp_priv *priv = arg;
	unsigned int ists = REG_READ(&priv->regs->ists);
	SPIN_ISR_IRQFLAGS(irqflags);

	/* Return if the SPWTDP didn't generate the IRQ */
	if (ists == 0)
		return;

	SPIN_LOCK(&priv->devlock, irqflags);
	REG_WRITE(&priv->regs->ists, ists); /* clear handled interrupt events */
	SPIN_UNLOCK(&priv->devlock, irqflags);

	/* Let user Handle Interrupt */
	if (priv->isr!=NULL)
		priv->isr(ists, priv->isr_arg);

	return;
}

int spwtdp_dat_et_get(void * spwtdp, spwtdp_time_t * val)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	if (priv == NULL) {
		/* SPWTDP not initialized */
		return SPWTDP_ERR_NOINIT;
	}

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check pointer */
	if (val == NULL) {
		return SPWTDP_ERR_EINVAL;
	}

	val->preamble = REG_READ(&priv->regs->dat_ctrl) & ETCTRL_PF;
	unsigned int * buffer = (unsigned int *) val->data;
	buffer[0] = REG_READ(&priv->regs->dat_et[0]);
	buffer[1] = REG_READ(&priv->regs->dat_et[1]);
	buffer[2] = REG_READ(&priv->regs->dat_et[2]);
	buffer[3] = REG_READ(&priv->regs->dat_et[3]);
	buffer[4] = REG_READ(&priv->regs->dat_et[4]);

	return SPWTDP_ERR_OK;
}

int spwtdp_tsrx_et_get(void * spwtdp, spwtdp_time_t * val)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	if (priv == NULL) {
		/* SPWTDP not initialized */
		return SPWTDP_ERR_NOINIT;
	}

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check pointer */
	if (val == NULL) {
		return SPWTDP_ERR_EINVAL;
	}

	val->preamble = REG_READ(&priv->regs->ts_rx_ctrl) & ETCTRL_PF;
	unsigned int * buffer = (unsigned int *) val->data;
	buffer[0] = REG_READ(&priv->regs->ts_rx_et[0]);
	buffer[1] = REG_READ(&priv->regs->ts_rx_et[1]);
	buffer[2] = REG_READ(&priv->regs->ts_rx_et[2]);
	buffer[3] = REG_READ(&priv->regs->ts_rx_et[3]);
	buffer[4] = REG_READ(&priv->regs->ts_rx_et[4]);

	return SPWTDP_ERR_OK;
}

int spwtdp_tstx_et_get(void * spwtdp, spwtdp_time_t * val)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	if (priv == NULL) {
		/* SPWTDP not initialized */
		return SPWTDP_ERR_NOINIT;
	}

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check pointer */
	if (val == NULL) {
		return SPWTDP_ERR_EINVAL;
	}

	val->preamble = REG_READ(&priv->regs->ts_tx_ctrl) & ETCTRL_PF;
	unsigned int * buffer = (unsigned int *) val->data;
	buffer[0] = REG_READ(&priv->regs->ts_tx_et[0]);
	buffer[1] = REG_READ(&priv->regs->ts_tx_et[1]);
	buffer[2] = REG_READ(&priv->regs->ts_tx_et[2]);
	buffer[3] = REG_READ(&priv->regs->ts_tx_et[3]);
	buffer[4] = REG_READ(&priv->regs->ts_tx_et[4]);

	return SPWTDP_ERR_OK;
}

int spwtdp_lat_et_get(void * spwtdp, spwtdp_time_t * val)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	if (priv == NULL) {
		/* SPWTDP not initialized */
		return SPWTDP_ERR_NOINIT;
	}

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check pointer */
	if (val == NULL) {
		return SPWTDP_ERR_EINVAL;
	}

	val->preamble = REG_READ(&priv->regs->lat_ctrl) & ETCTRL_PF;
	unsigned int * buffer = (unsigned int *) val->data;
	buffer[0] = REG_READ(&priv->regs->lat_et[0]);
	buffer[1] = REG_READ(&priv->regs->lat_et[1]);
	buffer[2] = REG_READ(&priv->regs->lat_et[2]);
	buffer[3] = REG_READ(&priv->regs->lat_et[3]);
	buffer[4] = REG_READ(&priv->regs->lat_et[4]);

	return SPWTDP_ERR_OK;
}

int spwtdp_cmd_et_get(void * spwtdp, spwtdp_time_t * val)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	if (priv == NULL) {
		/* SPWTDP not initialized */
		return SPWTDP_ERR_NOINIT;
	}

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check pointer */
	if (val == NULL) {
		return SPWTDP_ERR_EINVAL;
	}

	val->preamble = REG_READ(&priv->regs->cmd_ctrl) & ETCTRL_PF;
	unsigned int * buffer = (unsigned int *) val->data;
	buffer[0] = REG_READ(&priv->regs->cmd_et[0]);
	buffer[1] = REG_READ(&priv->regs->cmd_et[1]);
	buffer[2] = REG_READ(&priv->regs->cmd_et[2]);
	buffer[3] = REG_READ(&priv->regs->cmd_et[3]);
	buffer[4] = REG_READ(&priv->regs->cmd_et[4]);

	return SPWTDP_ERR_OK;
}

int spwtdp_initiator_tstx_conf(void * spwtdp, uint8_t tstc)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	if (priv == NULL) {
		/* SPWTDP not initialized */
		return SPWTDP_ERR_NOINIT;
	}

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check if configured as initiator */
	if (priv->initiator != 1)
		return SPWTDP_ERR_EINVAL;

	REG_WRITE(&priv->regs->ts_tx_ctrl,
		  (((uint32_t)tstc) << TSTXCTRL_TSTC_BIT) & TSTXCTRL_TSTC);

	return SPWTDP_ERR_OK;
}

int spwtdp_initiator_cmd_et_set(void *spwtdp, spwtdp_time_t val)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	if (priv == NULL) {
		/* SPWTDP not initialized */
		return SPWTDP_ERR_NOINIT;
	}

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check if configured as initiator */
	if (priv->initiator != 1)
		return SPWTDP_ERR_EINVAL;

	unsigned int * buffer = (unsigned int *) val.data;
	REG_WRITE(&priv->regs->lat_et[0], buffer[0]);
	REG_WRITE(&priv->regs->lat_et[1], buffer[1]);
	REG_WRITE(&priv->regs->lat_et[2], buffer[2]);
	REG_WRITE(&priv->regs->lat_et[3], buffer[3]);
	REG_WRITE(&priv->regs->lat_et[4], buffer[4]);


	/* Take SPWTDP lock - Wait until we get semaphore */
	if (rtems_semaphore_obtain(priv->sem, RTEMS_WAIT, RTEMS_NO_TIMEOUT)
		!= RTEMS_SUCCESSFUL)
		return SPWTDP_ERR_ERROR;

	/* Signal new command */
	unsigned int ctrl = REG_READ(&priv->regs->cmd_ctrl);
	REG_WRITE(&priv->regs->cmd_ctrl, ctrl | CTRL_NC);

	rtems_semaphore_release(priv->sem);

	return SPWTDP_ERR_OK;
}

int spwtdp_initiator_cmd_spwtc_set(void *spwtdp, uint8_t spwtc)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	if (priv == NULL) {
		/* SPWTDP not initialized */
		return SPWTDP_ERR_NOINIT;
	}

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check if configured as initiator */
	if (priv->initiator != 1)
		return SPWTDP_ERR_EINVAL;

	/* Take SPWTDP lock - Wait until we get semaphore */
	if (rtems_semaphore_obtain(priv->sem, RTEMS_WAIT, RTEMS_NO_TIMEOUT)
		!= RTEMS_SUCCESSFUL)
		return SPWTDP_ERR_ERROR;

	unsigned int ctrl = (REG_READ(&priv->regs->cmd_ctrl) &~ CTRL_SPWTC);
	REG_WRITE(&priv->regs->cmd_ctrl,
		  ctrl | (((uint32_t)spwtc << CTRL_SPWTC_BIT) & CTRL_SPWTC));

	rtems_semaphore_release(priv->sem);

	return SPWTDP_ERR_OK;
}

#define CTRL_TAR_MASK (CTRL_NC|CTRL_IS)
int spwtdp_target_cmd_conf(void *spwtdp, uint8_t spwtc, uint16_t cpf,
			   uint32_t options)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;

	if (priv == NULL) {
		/* SPWTDP not initialized */
		return SPWTDP_ERR_NOINIT;
	}

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	/* Check if configured as target */
	if (priv->target != 1)
		return SPWTDP_ERR_EINVAL;

	/* Take SPWTDP lock - Wait until we get semaphore */
	if (rtems_semaphore_obtain(priv->sem, RTEMS_WAIT, RTEMS_NO_TIMEOUT)
		!= RTEMS_SUCCESSFUL)
		return SPWTDP_ERR_ERROR;

	REG_WRITE(&priv->regs->cmd_ctrl, 
			(options & CTRL_TAR_MASK) |
			((cpf << CTRL_CPF_BIT) & CTRL_CPF) |
			(((uint32_t)spwtc << CTRL_SPWTC_BIT) & CTRL_SPWTC));

	rtems_semaphore_release(priv->sem);

	return SPWTDP_ERR_OK;
}

int spwtdp_precision_get(void *spwtdp, uint8_t *fine, uint8_t *coarse)
{
	struct spwtdp_priv *priv = (struct spwtdp_priv *)spwtdp;
	int coarse_precision, fine_precision;

	if (priv == NULL) {
		/* SPWTDP not initialized */
		return SPWTDP_ERR_NOINIT;
	}

	if (priv->open == 0)
		return SPWTDP_ERR_EINVAL;

	unsigned int preamble = REG_READ(&priv->regs->dat_ctrl);

	if (preamble & 0x80) {
		DBG("Pfield second extension set: unknown format");
		return SPWTDP_ERR_ERROR;
	}
	if (!((preamble & 0x7000) == 0x2000 || (preamble & 0x7000) == 0x1000)) {
		DBG(" PField indicates not unsegmented code: unknown format");
		return SPWTDP_ERR_ERROR;
	}
	/*
	 * coarse_precision = 32;
	 * fine_precision = 24;
	 */
	coarse_precision = ((preamble >> 10) & 0x3) + 1;
	if (preamble & 0x80)
		coarse_precision += (preamble >> 5) & 0x3;
	fine_precision = (preamble >> 8) & 0x3;
	if (preamble & 0x80)
		fine_precision += (preamble >> 2) & 0x7;
	if (coarse!=NULL)
		*coarse = coarse_precision;
	if (fine!=NULL)
		*fine = fine_precision;

	return SPWTDP_ERR_OK;
}