-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyStone_common.c
4354 lines (3719 loc) · 137 KB
/
KeyStone_common.c
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
/******************************************************************************
Copyright (C), 2001-2012, Texas Instrument.
******************************************************************************
File Name : KeyStone_common.c
Version : Initial Draft
Author : Brighton Feng
Created : 2010-12-12
Last Modified :
Description : KeyStone common miscellaneous functions and definitions
History :
1.Date : 2010-12-12
Author : Brighton Feng
Modification: Created file
2.Date : 2012-10-6
Author : Brighton Feng
Modification : Add memory protection and EDC configuration
3.Date : 2014-12-8
Author : Brighton Feng
Modification : Add common device, CPU, interrupt initialization functions.
Print device information including device type, speed grade,
boot mode, required voltage, ID¡
Add EDMA enable/disable functions.
To support square wave generation by timer.
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tistdtypes.h>
#include <csl_bootcfgAux.h>
#include <csl_pscAux.h>
#include <cslr_chip.h>
#include <csl_edma3.h>
#include "CSL_msmc.h"
#include "CSL_msmcAux.h"
#include "KeyStone_common.h"
/*----------------------------------------------*
* external variables *
*----------------------------------------------*/
/*----------------------------------------------*
* external routine prototypes *
*----------------------------------------------*/
/*----------------------------------------------*
* internal routine prototypes *
*----------------------------------------------*/
/*----------------------------------------------*
* project-wide global variables *
*----------------------------------------------*/
CSL_XmcRegs * gpXMC_regs = (CSL_XmcRegs *) CSL_XMC_CONFIG_REGS;
CSL_CgemRegs * gpCGEM_regs = (CSL_CgemRegs *)CSL_CGEM0_5_REG_BASE_ADDRESS_REGS;
CSL_BootcfgRegs * gpBootCfgRegs = (CSL_BootcfgRegs *)CSL_BOOT_CFG_REGS;
CSL_PllcRegs * gpPLLC_regs = (CSL_PllcRegs * )CSL_PLL_CONTROLLER_REGS;
CSL_PscRegs * gpPSC_regs = (CSL_PscRegs *)CSL_PSC_REGS;
CSL_MsmcRegs * gpMSMC_regs = (CSL_MsmcRegs *)CSL_MSMC_CONFIG_REGS;
CSL_GpioRegs * gpGPIO_regs= (CSL_GpioRegs * )CSL_GPIO_REGS;
CSL_CPINTCRegs* gpCIC0_regs = (CSL_CPINTCRegs*)CSL_CP_INTC_0_REGS;
CSL_CPINTCRegs* gpCIC1_regs = (CSL_CPINTCRegs*)CSL_CP_INTC_1_REGS;
/*The register pointer for the CIC routing events DSP core.
By default, it is CIC0; but for core 4~7 of C6678, it is CIC1*/
CSL_CPINTCRegs* gpCIC_regs= (CSL_CPINTCRegs*)CSL_CP_INTC_0_REGS;
CSL_TpccRegs* gpEDMA_CC0_regs = (CSL_TpccRegs*)CSL_EDMA0CC_REGS;
CSL_TpccRegs* gpEDMA_CC1_regs = (CSL_TpccRegs*)CSL_EDMA1CC_REGS;
CSL_TpccRegs* gpEDMA_CC2_regs = (CSL_TpccRegs*)CSL_EDMA2CC_REGS;
CSL_TpccRegs* gpEDMA_CC_regs[3] = {
(CSL_TpccRegs*)CSL_EDMA0CC_REGS,
(CSL_TpccRegs*)CSL_EDMA1CC_REGS,
(CSL_TpccRegs*)CSL_EDMA2CC_REGS
};
CSL_TptcRegs * gpEDMA_TC_0_0_regs=(CSL_TptcRegs *) CSL_EDMA0TC0_REGS;
CSL_TptcRegs * gpEDMA_TC_0_1_regs=(CSL_TptcRegs *) CSL_EDMA0TC1_REGS;
CSL_TptcRegs * gpEDMA_TC_1_0_regs=(CSL_TptcRegs *) CSL_EDMA1TC0_REGS;
CSL_TptcRegs * gpEDMA_TC_1_1_regs=(CSL_TptcRegs *) CSL_EDMA1TC1_REGS;
CSL_TptcRegs * gpEDMA_TC_1_2_regs=(CSL_TptcRegs *) CSL_EDMA1TC2_REGS;
CSL_TptcRegs * gpEDMA_TC_1_3_regs=(CSL_TptcRegs *) CSL_EDMA1TC3_REGS;
CSL_TptcRegs * gpEDMA_TC_2_0_regs=(CSL_TptcRegs *) CSL_EDMA2TC0_REGS;
CSL_TptcRegs * gpEDMA_TC_2_1_regs=(CSL_TptcRegs *) CSL_EDMA2TC1_REGS;
CSL_TptcRegs * gpEDMA_TC_2_2_regs=(CSL_TptcRegs *) CSL_EDMA2TC2_REGS;
CSL_TptcRegs * gpEDMA_TC_2_3_regs=(CSL_TptcRegs *) CSL_EDMA2TC3_REGS;
CSL_TptcRegs * gpEDMA_TC_regs[10]= {
(CSL_TptcRegs *) CSL_EDMA0TC0_REGS,
(CSL_TptcRegs *) CSL_EDMA0TC1_REGS,
(CSL_TptcRegs *) CSL_EDMA1TC0_REGS,
(CSL_TptcRegs *) CSL_EDMA1TC1_REGS,
(CSL_TptcRegs *) CSL_EDMA1TC2_REGS,
(CSL_TptcRegs *) CSL_EDMA1TC3_REGS,
(CSL_TptcRegs *) CSL_EDMA2TC0_REGS,
(CSL_TptcRegs *) CSL_EDMA2TC1_REGS,
(CSL_TptcRegs *) CSL_EDMA2TC2_REGS,
(CSL_TptcRegs *) CSL_EDMA2TC3_REGS
};
CSL_TmrPlusRegs * gpTimer0Regs = (CSL_TmrPlusRegs *)CSL_TIMER_0_REGS;
CSL_TmrPlusRegs * gpTimer1Regs = (CSL_TmrPlusRegs *)CSL_TIMER_1_REGS;
CSL_TmrPlusRegs * gpTimer2Regs = (CSL_TmrPlusRegs *)CSL_TIMER_2_REGS;
CSL_TmrPlusRegs * gpTimer3Regs = (CSL_TmrPlusRegs *)CSL_TIMER_3_REGS;
CSL_TmrPlusRegs * gpTimer4Regs = (CSL_TmrPlusRegs *)CSL_TIMER_4_REGS;
CSL_TmrPlusRegs * gpTimer5Regs = (CSL_TmrPlusRegs *)CSL_TIMER_5_REGS;
CSL_TmrPlusRegs * gpTimer6Regs = (CSL_TmrPlusRegs *)CSL_TIMER_6_REGS;
CSL_TmrPlusRegs * gpTimer7Regs = (CSL_TmrPlusRegs *)CSL_TIMER_7_REGS;
CSL_TmrPlusRegs * gpTimer8Regs = (CSL_TmrPlusRegs *)(CSL_TIMER_7_REGS+(CSL_TIMER_7_REGS-CSL_TIMER_6_REGS));
CSL_TmrPlusRegs * gpTimerRegs[9] = {
(CSL_TmrPlusRegs *)CSL_TIMER_0_REGS,
(CSL_TmrPlusRegs *)CSL_TIMER_1_REGS,
(CSL_TmrPlusRegs *)CSL_TIMER_2_REGS,
(CSL_TmrPlusRegs *)CSL_TIMER_3_REGS,
(CSL_TmrPlusRegs *)CSL_TIMER_4_REGS,
(CSL_TmrPlusRegs *)CSL_TIMER_5_REGS,
(CSL_TmrPlusRegs *)CSL_TIMER_6_REGS,
(CSL_TmrPlusRegs *)CSL_TIMER_7_REGS,
(CSL_TmrPlusRegs *)(CSL_TIMER_7_REGS+(CSL_TIMER_7_REGS-CSL_TIMER_6_REGS))
};
/*MPU for peripherals registers and data space*/
CSL_MpuRegs * gpMPU0_regs= (CSL_MpuRegs *)CSL_MPU_0_REGS;
CSL_MpuRegs * gpMPU1_regs= (CSL_MpuRegs *)CSL_MPU_1_REGS;
CSL_MpuRegs * gpMPU2_regs= (CSL_MpuRegs *)CSL_MPU_2_REGS;
CSL_MpuRegs * gpMPU3_regs= (CSL_MpuRegs *)CSL_MPU_3_REGS;
CSL_Emif4fRegs * gpDDR_regs= (CSL_Emif4fRegs *)CSL_DDR3_EMIF_CONFIG_REGS;
unsigned int gDSP_Core_Speed_Hz= 1000000000; //DSP core clock speed in Hz
TDSP_Board_Type gDSP_board_type= UNKNOWN;
/*----------------------------------------------*
* module-wide global variables *
*----------------------------------------------*/
/*----------------------------------------------*
* constants *
*----------------------------------------------*/
/*----------------------------------------------*
* macros *
*----------------------------------------------*/
/*----------------------------------------------*
* routines' implementations *
*----------------------------------------------*/
/*****************************************************************************
Prototype : KeyStone_main_PLL_init
Description : DSP core PLL configuration
DSP core will be configured to run at ref_clock_MHz*multiplier/divisor
Input : float ref_clock_MHz
unsigned int multiplier: 1~4096
unsigned int divisor: 1~64
Output :
Return Value :
History :
1.Date : 2010-12-12
Author : Brighton Feng
Modification : Created function
2.Date : May 19, 2013
Author : Brighton Feng
Modification : update parameter check; replace constant with macro
*****************************************************************************/
void KeyStone_main_PLL_init (float ref_clock_MHz,
unsigned int multiplier, unsigned int divisor)
{
unsigned int i;
if(0==divisor)
{
puts("Error: PLL input divider = 0");
return;
}
if(64<divisor)
{
puts("Error: PLL input divider too large");
return;
}
if(0==multiplier)
{
puts("Error: PLL multiplier = 0");
return;
}
if(4096<multiplier)
{
puts("Error: PLL multiplier too large");
return;
}
CSL_BootCfgUnlockKicker();
gDSP_Core_Speed_Hz= ref_clock_MHz*1000000/divisor*multiplier;
printf("Initialize DSP main clock = %.2fMHz/%dx%d = %dMHz\n",
ref_clock_MHz, divisor, multiplier, gDSP_Core_Speed_Hz/1000000);
/*1. If executing this sequence immediately after device power-up, you must allow for*/
/*the PLL to become stable. PLL stabilization time is 100 ¦Ìs. */
for(i=0; i< 20000; i++)
asm(" nop 5");
/*2. Check the status of BYPASS bit in SECCTL register, execute following steps if */
/*BYPASS == 1 (if bypass enabled): */
if(gpPLLC_regs->SECCTL & PLLCTL_BYPASS_MASK)
{
/*a. In MAINPLLCTL1, write ENSAT = 1 (for optimal PLL operation) */
gpBootCfgRegs->CORE_PLL_CTL1 |= PLLCTL1_ENSAT_MASK; /*Set ENSAT bit = 1*/
/*b. In PLLCTL, write PLLEN = 0 (bypass enabled in PLL controller mux) */
gpPLLC_regs->PLLCTL &= ~CSL_PLLC_PLLCTL_PLLEN_MASK;
/*c. In PLLCTL, write PLLENSRC = 0 (enable PLLEN to control PLL controller mux */
gpPLLC_regs->PLLCTL &= ~CSL_PLLC_PLLCTL_PLLENSRC_MASK;
/*d. Wait 4 cycles of the reference clock CLKIN (to make sure the PLL controller */
/*mux switches properly to the bypass) */
for(i=0; i< 4; i++)
asm(" nop 5");
/*e. In SECCTL, write BYPASS = 1 (bypass enabled in PLL mux) */
gpPLLC_regs->SECCTL |= PLLCTL_BYPASS_MASK;
/*f. In PLLCTL, write PLLPWRDN = 1 (power down the PLL) */
gpPLLC_regs->PLLCTL |= CSL_PLLC_PLLCTL_PLLPWRDN_MASK; //Power down the PLL
/*g. Wait for at least 5 ¦Ìs based on the reference clock CLKIN (PLL power down */
/*toggling time) */
for(i=0; i< 1000; i++)
asm(" nop 5");
/*h. In PLLCTL, write PLLPWRDN = 0 (power up the PLL) */
gpPLLC_regs->PLLCTL &= ~CSL_PLLC_PLLCTL_PLLPWRDN_MASK; // Power up PLL
}
else
{
/*a. In PLLCTL, write PLLEN = 0 (bypass enabled in PLL controller mux) */
gpPLLC_regs->PLLCTL &= (~CSL_PLLC_PLLCTL_PLLEN_MASK);
/*b. In PLLCTL, write PLLENSRC = 0 (enable PLLEN to control PLL controller mux */
gpPLLC_regs->PLLCTL &= (~CSL_PLLC_PLLCTL_PLLENSRC_MASK);
/*c. Wait 4 cycles of the reference clock CLKIN (to make sure the PLL controller */
/*mux switches properly to the bypass) */
for(i=0; i< 4*multiplier/divisor; i++)
asm(" nop 5");
}
/*4. PLLM is split in two different registers. Program PLLM[5:0] in PLL multiplier */
/*control register (PLLM) and PLLM[12:6] in MAINPLLCTL0 register */
/*5. BWADJ is split in two different registers. Program BWADJ[7:0] in */
/*MAINPLLCTL0 and BWADJ[11:8] in MAINPLLCTL1 register. BWADJ value */
/*must be set to ((PLLM + 1) >> 1) - 1) */
/*6. Program PLLD in MAINPLLCTL0 register */
gpBootCfgRegs->CORE_PLL_CTL0 = ((multiplier-1)<<PLLCTL0_BWADJ_SHIFT)|
(((multiplier*2-1)&0x1FC0)<<PLLCTL0_PLLM_SHIFT)|(divisor-1);
gpPLLC_regs->PLLM= (multiplier*2-1)&0x3F;
gpBootCfgRegs->CORE_PLL_CTL1 &= ~PLLCTL1_BWADJ_MASK;
gpBootCfgRegs->CORE_PLL_CTL1 |= (multiplier-1)>>8; /*BWADJ[11:8]*/
/*7. In SECCTL, write OD (Output Divide) = 1 (that is divide-by-2) */
gpPLLC_regs->SECCTL &= ~PLLCTL_OD_MASK;
gpPLLC_regs->SECCTL |= 1<<PLLCTL_OD_SHIFT;
#if 0
/*8. If necessary, program PLLDIVn. Note that you must apply the GO operation to */
/*change these dividers to a new ratios (see Section 3.2 ¡®¡®Divider n (D1 to Dn) and*/
/*GO Operation ¡¯¡¯ on page 3-3). */
/* Step 8a: Check that the GOSTAT bit in PLLSTAT is cleared to show that no GO
operation is currently in progress*/
while((gpPLLC_regs->PLLSTAT) & CSL_PLLC_PLLSTAT_GOSTAT_MASK);
/* Step 8b: Program the RATIO field in PLLDIVn to the desired new divide-down rate.
If RATIO field is changed, the PLL controller will flag the change in the
corresponding bit of DCHANGE*/
gpPLLC_regs->PLLDIV1_3[3-1] = (3-1) | CSL_PLLC_PLLDIV1_3_DNEN_MASK; //Set PLLDIV3
gpPLLC_regs->PLLDIV4_16[4-4] = (5-1) | CSL_PLLC_PLLDIV4_16_DNEN_MASK; //Set PLLDIV4
gpPLLC_regs->PLLDIV4_16[7-4] = (6-1) | CSL_PLLC_PLLDIV4_16_DNEN_MASK; //Set PLLDIV7
/* Step 8c: Set GOSET bit in PLLCMD to initiate the GO operation to change the divide
values and align the SYSCLKs as programmed */
gpPLLC_regs->PLLCMD |= CSL_PLLC_PLLCMD_GOSET_MASK;
/*Step 8d/e: Read the GOSTAT bit in PLLSTAT to make sure the bit returns to 0 to
indicate that the GO operation has completed */
while((gpPLLC_regs->PLLSTAT) & CSL_PLLC_PLLSTAT_GOSTAT_MASK);
#endif
/*9. In PLLCTL , write PLLRST = 1 (PLL reset is asserted)*/
gpPLLC_regs->PLLCTL |= CSL_PLLC_PLLCTL_PLLRST_MASK;
/*10. Wait for at least 7 ¦Ìs based on the reference clock CLKIN (PLL reset time) */
for(i=0; i< 2000; i++)
asm(" nop 5");
/*11. In PLLCTL, write PLLRST = 0 (PLL reset is released) */
gpPLLC_regs->PLLCTL &= ~CSL_PLLC_PLLCTL_PLLRST_MASK;
/*12. Wait for at least 2000 ¡Á CLKIN cycles ¡Á (PLLD + 1) (PLL lock time) */
for(i=0; i< 400*multiplier; i++)
asm(" nop 5");
/*13. In SECCTL, write BYPASS = 0 (enable PLL mux to switch to PLL mode) */
gpPLLC_regs->SECCTL &= ~PLLCTL_BYPASS_MASK;
/*14. Set the PLLEN bit in PLLCTL to 1 to enable PLL mode*/
gpPLLC_regs->PLLCTL |= CSL_PLLC_PLLCTL_PLLEN_MASK;
}
/*****************************************************************************
Prototype : KeyStone_PLL_init
Description : Config the PLL of PA and DDR
target clock speed will be ref_clock_MHz/inputDivisor*multiplier/outputDivisor
Input : unsigned int inputDivisor
unsigned int multiplier
unsigned int outputDivisor
Output : None
Return Value : 0 for success, other value for error
History :
1.Date : May 18, 2013
Author : Brighton Feng
Modification : Created function
*****************************************************************************/
int KeyStone_PLL_init (PLL_ControlRegs * PLL_Regs, unsigned int inputDivisor,
unsigned int multiplier, unsigned int outputDivisor)
{
if(0==inputDivisor)
{
puts("Error: PLL input divider = 0");
return 1;
}
if(64<inputDivisor)
{
puts("Error: PLL input divider too large");
return 2;
}
if(0==multiplier)
{
puts("Error: PLL multiplier = 0");
return 3;
}
if(8192<multiplier)
{
puts("Error: PLL multiplier too large");
return 4;
}
if(0==outputDivisor)
{
puts("Error: PLL output divider = 0");
return 5;
}
if(16<outputDivisor)
{
puts("Error: PLL output divider too larger");
return 6;
}
CSL_BootCfgUnlockKicker();
/*1. In PLLCTL1, write ENSAT = 1 (for optimal PLL operation)*/
PLL_Regs->PLL_CTL1 = PLLCTL1_ENSAT_MASK;
/*2. In PLLCTL0, write BYPASS = 1 (set the PLL in Bypass)*/
PLL_Regs->PLL_CTL0 |= PLLCTL_BYPASS_MASK;
/*3. Program PLLM and PLLD in PLLCTL0 register*/
/*4. Program BWADJ[7:0] in PLLCTL0 and BWADJ[11:8] in PLLCTL1 register.
BWADJ value must be set to ((PLLM + 1) >> 1) ¨C 1)*/
PLL_Regs->PLL_CTL0 = ((multiplier/2-1)<<PLLCTL0_BWADJ_SHIFT)
|((outputDivisor-1)<<PLLCTL_OD_SHIFT)
|((multiplier-1)<<PLLCTL0_PLLM_SHIFT)
|(inputDivisor-1);
PLL_Regs->PLL_CTL1 &= ~PLLCTL1_BWADJ_MASK;
PLL_Regs->PLL_CTL1 |= (multiplier/2-1)>>8; /*BWADJ[11:8]*/
/*5. In PLLCTL1, write PLLRST = 1 (PLL is reset)*/
PLL_Regs->PLL_CTL1 |= PLLCTL1_PLLRESET_MASK; //Set RESET bit = 1
/*6. Wait for at least 7 us based on the reference clock (PLL reset time)*/
TSC_delay_us(7);
/*For PASS, In PASSPLLCTL1, write PLLSELECT = 1
(for selecting the output of PA PLL as the input to PASS)*/
if(PLL_Regs==(PLL_ControlRegs *)gpBootCfgRegs->PA_PLL_CTL0)
PLL_Regs->PLL_CTL1 |= PLLCTL1_PAPLL_MASK;
/*7. In PLLCTL1, write PLLRST = 0 (PLL reset is released)*/
PLL_Regs->PLL_CTL1 &= ~PLLCTL1_PLLRESET_MASK; //Clear RESET bit
/*8. Wait for at least 500 * REFCLK cycles * (PLLD + 1) (PLL lock time)*/
TSC_delay_us(500);
/*9. In PLLCTL0, write BYPASS = 0 (switch to PLL mode)*/
PLL_Regs->PLL_CTL0 &= ~PLLCTL_BYPASS_MASK ;
return 0;
}
/*****************************************************************************
Prototype : KeyStone_PASS_PLL_init
Description : Config the PASS PLL
target clock speed will be ref_clock_MHz/divisor*multiplier
Input : float ref_clock_MHz
unsigned int multiplier: 1~4096
unsigned int divisor: 1~64
Output : None
Return Value :
History :
1.Date : 2013-2-14
Author : Brighton Feng
Modification : Created function
*****************************************************************************/
void KeyStone_PASS_PLL_init (float ref_clock_MHz,
unsigned int multiplier, unsigned int divisor)
{
//output divisor for PASS PLL should be 2
if(0==KeyStone_PLL_init((PLL_ControlRegs *)&gpBootCfgRegs->PA_PLL_CTL0,
divisor, multiplier*2, 2))
{
printf("Initialize PASS PLL clock = %.2fMHz/%dx%d = %.3fMHz\n",
ref_clock_MHz, divisor, multiplier,
ref_clock_MHz*multiplier/divisor);
}
}
/*****************************************************************************
Prototype : KeyStone_DDR_PLL_init
Description : Config the DDR PLL
target clock speed will be ref_clock_MHz/divisor*multiplier
Input : float ref_clock_MHz
unsigned int multiplier: 1~4096
unsigned int divisor: 1~64
Output : None
Return Value :
History :
1.Date : 2013-8-18
Author : Brighton Feng
Modification : Created function
*****************************************************************************/
void KeyStone_DDR_PLL_init (float ref_clock_MHz,
unsigned int multiplier, unsigned int divisor)
{
//output divisor for DDR PLL should be 2
if(0==KeyStone_PLL_init((PLL_ControlRegs *)&gpBootCfgRegs->DDR3_PLL_CTL0,
divisor, multiplier, 2))
{
printf("Initialize DDR speed = %.2fMHzx/%dx%d = %.3fMTS\n",
ref_clock_MHz, divisor, multiplier,
ref_clock_MHz*multiplier/divisor);
}
}
/*===============================TSC===================================*/
unsigned int cycle_measure_overhead=50;
/*****************************************************************************
Prototype : calc_cycle_measure_overhead
Description : calclucate the cycles measurement overhead
Input : None
Output : None
Return Value :
History :
1.Date : 2010/12/12
Author : Brighton Feng
Modification : Created function
*****************************************************************************/
void calc_cycle_measure_overhead()
{
unsigned int cycle_cold, cycle_warm;
cycle_cold= TSCL;
cycle_cold = TSC_getDelay(cycle_cold);
cycle_warm= TSCL;
cycle_warm = TSC_getDelay(cycle_warm);
cycle_measure_overhead = (cycle_cold + cycle_warm)/2;
}
/*****************************************************************************
Prototype : TSC_init
Description : Initialize Time stamp counter to measure cycles
Input : None
Output : None
Return Value :
History :
1.Date : 2010/12/12
Author : Brighton Feng
Modification : Created function
*****************************************************************************/
void TSC_init()
{
TSCL = 0; /* Enable the TSC */
calc_cycle_measure_overhead();
}
/*****************************************************************************
Prototype : TSC_delay_ms
Description : Implement the delay function in millisecond
Input : Uint32 ms
Output : None
Return Value :
History :
1.Date : 2010/12/12
Author : Brighton Feng
Modification : Created function
*****************************************************************************/
void TSC_delay_ms(Uint32 ms)
{
volatile unsigned long long startTSC, currentTSC;
unsigned long long delay_cycles;
Uint32 tscl, tsch;
tscl= TSCL;
tsch= TSCH;
startTSC= _itoll(tsch,tscl);
delay_cycles= ((unsigned long long)ms*gDSP_Core_Speed_Hz/1000);
do
{
tscl= TSCL;
tsch= TSCH;
currentTSC= _itoll(tsch,tscl);
}
while((currentTSC-startTSC)<delay_cycles);
}
/*****************************************************************************
Prototype : TSC_delay_us
Description : Implement the delay function in microsecond
Input : Uint32 us
Output : None
Return Value :
*****************************************************************************/
void TSC_delay_us(Uint32 us)
{
volatile unsigned long long startTSC, currentTSC;
unsigned long long delay_cycles;
Uint32 tscl, tsch;
tscl= TSCL;
tsch= TSCH;
startTSC= _itoll(tsch,tscl);
delay_cycles= ((unsigned long long)us*gDSP_Core_Speed_Hz/1000000);
do
{
tscl= TSCL;
tsch= TSCH;
currentTSC= _itoll(tsch,tscl);
}
while((currentTSC-startTSC)<delay_cycles);
}
/*===============================Timer=================================*/
/*****************************************************************************
Prototype : Reset_Timer
Description : Reset the general timer value
Input : int timer_num
Output : None
Return Value :
History :
1.Date : 2010/12/12
Author : Brighton Feng
Modification : Created function
*****************************************************************************/
void Reset_Timer(int timer_num)
{
if(gpTimerRegs[timer_num]->TGCR)
{
gpTimerRegs[timer_num]->TGCR= 0;
gpTimerRegs[timer_num]->TCR= 0;
}
}
/*****************************************************************************
Prototype : Timer64_Init
Description : Initialize a 64-bit timer
Input : Timer64_Config * tmrCfg
Output : None
Return Value :
History :
1.Date : 2010/12/12
Author : Brighton Feng
Modification : Created function
*****************************************************************************/
void Timer64_Init(Timer64_Config * tmrCfg)
{
Reset_Timer(tmrCfg->timer_num);
gpTimerRegs[tmrCfg->timer_num]->CNTLO= 0;
gpTimerRegs[tmrCfg->timer_num]->CNTHI= 0;
/*please note, in clock mode, two timer periods generate a clock,
one timer period output high voltage level, the other timer period
output low voltage level, so, the timer period should be half to the
desired output clock period*/
if(TIMER_PERIODIC_CLOCK==tmrCfg->timerMode)
tmrCfg->period= tmrCfg->period/2;
/*the value written into period register is the expected value minus one*/
gpTimerRegs[tmrCfg->timer_num]->PRDLO= _loll(tmrCfg->period-1);
gpTimerRegs[tmrCfg->timer_num]->PRDHI= _hill(tmrCfg->period-1);
if(tmrCfg->reload_period>1)
{
gpTimerRegs[tmrCfg->timer_num]->RELLO= _loll(tmrCfg->reload_period-1);
gpTimerRegs[tmrCfg->timer_num]->RELHI= _hill(tmrCfg->reload_period-1);
}
if(TIMER_WATCH_DOG==tmrCfg->timerMode)
{
gpTimerRegs[tmrCfg->timer_num]->TGCR=
/*Select watch-dog mode*/
(CSL_TMR_TIMMODE_WDT<<CSL_TMR_TGCR_TIMMODE_SHIFT)
/*Remove the timer from reset*/
|(CSL_TMR_TGCR_TIMLORS_MASK)
|(CSL_TMR_TGCR_TIMHIRS_MASK);
}
else if(TIMER_PERIODIC_WAVE==tmrCfg->timerMode)
{
gpTimerRegs[tmrCfg->timer_num]->TGCR= TMR_TGCR_PLUSEN_MASK
/*for plus featuers, dual 32-bit unchained timer mode should be used*/
|(CSL_TMR_TIMMODE_DUAL_UNCHAINED<<CSL_TMR_TGCR_TIMMODE_SHIFT)
/*Remove the timer from reset*/
|(CSL_TMR_TGCR_TIMLORS_MASK);
//in plus mode, interrupt/event must be enabled manually
gpTimerRegs[tmrCfg->timer_num]->INTCTL_STAT= TMR_INTCTLSTAT_EN_ALL_CLR_ALL;
}
else
{
gpTimerRegs[tmrCfg->timer_num]->TGCR=
/*Select 64-bit general timer mode*/
(CSL_TMR_TIMMODE_GPT<<CSL_TMR_TGCR_TIMMODE_SHIFT)
/*Remove the timer from reset*/
|(CSL_TMR_TGCR_TIMLORS_MASK)
|(CSL_TMR_TGCR_TIMHIRS_MASK);
}
/*make timer stop with emulation*/
gpTimerRegs[tmrCfg->timer_num]->EMUMGT_CLKSPD = (gpTimerRegs[tmrCfg->timer_num]->EMUMGT_CLKSPD&
~(CSL_TMR_EMUMGT_CLKSPD_FREE_MASK|CSL_TMR_EMUMGT_CLKSPD_SOFT_MASK));
if(TIMER_WATCH_DOG==tmrCfg->timerMode)
{
/*enable watchdog timer*/
gpTimerRegs[tmrCfg->timer_num]->WDTCR = CSL_TMR_WDTCR_WDEN_MASK
|(CSL_TMR_WDTCR_WDKEY_CMD1<<CSL_TMR_WDTCR_WDKEY_SHIFT);
gpTimerRegs[tmrCfg->timer_num]->TCR=
(CSL_TMR_CLOCK_INP_NOGATE<<CSL_TMR_TCR_TIEN_LO_SHIFT )
|(CSL_TMR_CLKSRC_INTERNAL<<CSL_TMR_TCR_CLKSRC_LO_SHIFT )
/*The timer is enabled continuously*/
|(CSL_TMR_ENAMODE_CONT<<CSL_TMR_TCR_ENAMODE_LO_SHIFT)
|((tmrCfg->pulseWidth<<CSL_TMR_TCR_PWID_LO_SHIFT)&CSL_TMR_TCR_PWID_LO_MASK)
/*select pulse mode*/
|(CSL_TMR_CP_PULSE<<CSL_TMR_TCR_CP_LO_SHIFT )
|(CSL_TMR_INVINP_UNINVERTED<<CSL_TMR_TCR_INVINP_LO_SHIFT )
|(CSL_TMR_INVOUTP_UNINVERTED<<CSL_TMR_TCR_INVOUTP_LO_SHIFT)
|(0<<CSL_TMR_TCR_TSTAT_LO_SHIFT );
/*active watchdog timer*/
gpTimerRegs[tmrCfg->timer_num]->WDTCR = CSL_TMR_WDTCR_WDEN_MASK
|(CSL_TMR_WDTCR_WDKEY_CMD2<<CSL_TMR_WDTCR_WDKEY_SHIFT);
}
else if(TIMER_ONE_SHOT_PULSE==tmrCfg->timerMode)
{
gpTimerRegs[tmrCfg->timer_num]->TCR=
(CSL_TMR_CLOCK_INP_NOGATE<<CSL_TMR_TCR_TIEN_LO_SHIFT )
|(CSL_TMR_CLKSRC_INTERNAL<<CSL_TMR_TCR_CLKSRC_LO_SHIFT )
/*The timer is enabled one-shot*/
|(CSL_TMR_ENAMODE_ENABLE<<CSL_TMR_TCR_ENAMODE_LO_SHIFT)
|((tmrCfg->pulseWidth<<CSL_TMR_TCR_PWID_LO_SHIFT)&CSL_TMR_TCR_PWID_LO_MASK)
/*select pulse mode*/
|(CSL_TMR_CP_PULSE<<CSL_TMR_TCR_CP_LO_SHIFT )
|(CSL_TMR_INVINP_UNINVERTED<<CSL_TMR_TCR_INVINP_LO_SHIFT )
|(CSL_TMR_INVOUTP_UNINVERTED<<CSL_TMR_TCR_INVOUTP_LO_SHIFT)
|(0<<CSL_TMR_TCR_TSTAT_LO_SHIFT );
}
else if(TIMER_PERIODIC_CLOCK==tmrCfg->timerMode)
{
gpTimerRegs[tmrCfg->timer_num]->TCR=
(CSL_TMR_CLOCK_INP_NOGATE<<CSL_TMR_TCR_TIEN_LO_SHIFT )
|(CSL_TMR_CLKSRC_INTERNAL<<CSL_TMR_TCR_CLKSRC_LO_SHIFT )
/*The timer is enabled continuously*/
|(CSL_TMR_ENAMODE_CONT<<CSL_TMR_TCR_ENAMODE_LO_SHIFT)
|((tmrCfg->pulseWidth<<CSL_TMR_TCR_PWID_LO_SHIFT)&CSL_TMR_TCR_PWID_LO_MASK)
/*select clock mode*/
|(CSL_TMR_CP_CLOCK<<CSL_TMR_TCR_CP_LO_SHIFT )
|(CSL_TMR_INVINP_UNINVERTED<<CSL_TMR_TCR_INVINP_LO_SHIFT )
|(CSL_TMR_INVOUTP_UNINVERTED<<CSL_TMR_TCR_INVOUTP_LO_SHIFT)
|(0<<CSL_TMR_TCR_TSTAT_LO_SHIFT );
}
else if(TIMER_PERIODIC_WAVE==tmrCfg->timerMode)
{
gpTimerRegs[tmrCfg->timer_num]->TCR=
(CSL_TMR_CLOCK_INP_NOGATE<<CSL_TMR_TCR_TIEN_LO_SHIFT )
|(CSL_TMR_CLKSRC_INTERNAL<<CSL_TMR_TCR_CLKSRC_LO_SHIFT )
/*The timer is enabled continuously with period reload*/
|(CSL_TMR_ENAMODE_CONT_RELOAD<<CSL_TMR_TCR_ENAMODE_LO_SHIFT)
|((tmrCfg->pulseWidth<<CSL_TMR_TCR_PWID_LO_SHIFT)&CSL_TMR_TCR_PWID_LO_MASK)
/*select clock mode*/
|(CSL_TMR_CP_CLOCK<<CSL_TMR_TCR_CP_LO_SHIFT )
|(CSL_TMR_INVINP_UNINVERTED<<CSL_TMR_TCR_INVINP_LO_SHIFT )
|(CSL_TMR_INVOUTP_UNINVERTED<<CSL_TMR_TCR_INVOUTP_LO_SHIFT)
|(0<<CSL_TMR_TCR_TSTAT_LO_SHIFT );
}
else /*TIMER_PERIODIC_PULSE*/
{
gpTimerRegs[tmrCfg->timer_num]->TCR=
(CSL_TMR_CLOCK_INP_NOGATE<<CSL_TMR_TCR_TIEN_LO_SHIFT )
|(CSL_TMR_CLKSRC_INTERNAL<<CSL_TMR_TCR_CLKSRC_LO_SHIFT )
/*The timer is enabled continuously*/
|(CSL_TMR_ENAMODE_CONT<<CSL_TMR_TCR_ENAMODE_LO_SHIFT)
|((tmrCfg->pulseWidth<<CSL_TMR_TCR_PWID_LO_SHIFT)&CSL_TMR_TCR_PWID_LO_MASK)
/*select clock mode*/
|(CSL_TMR_CP_PULSE<<CSL_TMR_TCR_CP_LO_SHIFT )
|(CSL_TMR_INVINP_UNINVERTED<<CSL_TMR_TCR_INVINP_LO_SHIFT )
|(CSL_TMR_INVOUTP_UNINVERTED<<CSL_TMR_TCR_INVOUTP_LO_SHIFT)
|(0<<CSL_TMR_TCR_TSTAT_LO_SHIFT );
}
}
/*****************************************************************************
Prototype : Service_Watchdog
Description : Implement the watch dog service
Input : int timer_num
Output : None
Return Value :
History :
1.Date : 2010/12/12
Author : Brighton Feng
Modification : Created function
*****************************************************************************/
void Service_Watchdog(int timer_num)
{
/*write sequence of a A5C6h followed by a DA7Eh
to services the watchdog timer.*/
gpTimerRegs[timer_num]->WDTCR =
(CSL_TMR_WDTCR_WDKEY_CMD1<<CSL_TMR_WDTCR_WDKEY_SHIFT);
gpTimerRegs[timer_num]->WDTCR =
(CSL_TMR_WDTCR_WDKEY_CMD2<<CSL_TMR_WDTCR_WDKEY_SHIFT);
}
/*===============================PSC===================================*/
/*****************************************************************************
Prototype : KeyStone_enable_PSC_module
Description : Enable the PSC module in KeyStone device
Input : Uint32 pwrDmnNum
Uint32 moduleNum
Output : None
Return Value :
History :
1.Date : 2010/12/12
Author : Brighton Feng
Modification : Created function
*****************************************************************************/
Int32 KeyStone_enable_PSC_module (Uint32 pwrDmnNum, Uint32 moduleNum)
{
Uint32 uiStartTSC= TSCL;
if (CSL_PSC_getPowerDomainState(pwrDmnNum) != PSC_PDSTATE_ON)
{
/* Set Power domain to ON */
CSL_PSC_enablePowerDomain (pwrDmnNum);
/* Start the state transition */
CSL_PSC_startStateTransition (pwrDmnNum);
/* Wait until the state transition process is completed. */
while (!CSL_PSC_isStateTransitionDone (pwrDmnNum))
{
if(TSC_count_cycle_from(uiStartTSC)>0x3FFFFFFF)
{
printf("Enable power domain %d timeout!\n", pwrDmnNum);
return -2;
}
}
}
/* Enable the clocks too*/
CSL_PSC_setModuleNextState (moduleNum, PSC_MODSTATE_ENABLE);
/* Start the state transition */
CSL_PSC_startStateTransition (pwrDmnNum);
/* Wait until the state transition process is completed. */
while (!CSL_PSC_isStateTransitionDone (pwrDmnNum))
{
if(TSC_count_cycle_from(uiStartTSC)>0x3FFFFFFF)
{
printf("Enable clock domain %d timeout!\n", moduleNum);
return -2;
}
}
/* Return PSC status */
if ((CSL_PSC_getPowerDomainState(pwrDmnNum) == PSC_PDSTATE_ON) &&
(CSL_PSC_getModuleState (moduleNum) == PSC_MODSTATE_ENABLE))
{
/*Ready for use */
return 0;
}
else
{
/*Return error */
return -1;
}
}
/*****************************************************************************
Prototype : KeyStone_disable_PSC_module
Description : Disable the PSC module in KeyStone device
Input : Uint32 pwrDmnNum
Uint32 moduleNum
Output : None
Return Value :
History :
1.Date : 2010/2/12
Author : Brighton Feng
Modification : Created function
*****************************************************************************/
Int32 KeyStone_disable_PSC_module (Uint32 pwrDmnNum, Uint32 moduleNum)
{
Uint32 uiStartTSC= TSCL;
/* disable the clocks*/
CSL_PSC_setModuleNextState (moduleNum, PSC_MODSTATE_SWRSTDISABLE);
/* Start the state transition */
CSL_PSC_startStateTransition (pwrDmnNum);
/* Wait until the state transition process is completed. */
while (!CSL_PSC_isStateTransitionDone (pwrDmnNum))
{
if(TSC_count_cycle_from(uiStartTSC)>0x3FFFFFFF)
{
printf("Disable clock domain %d timeout!\n", moduleNum);
return -2;
}
}
/* Return PSC status */
if (CSL_PSC_getModuleState (moduleNum) == PSC_MODSTATE_SWRSTDISABLE)
{
/*Ready for use */
return 0;
}
else
{
/*Return error */
return -1;
}
}
/*****************************************************************************
Prototype : KeyStone_disable_PSC_Power_Domain
Description : Disable the PSC power domain
Input : Uint32 pwrDmnNum
Output : None
Return Value :
History :
1.Date : 2010/12/12
Author : Brighton Feng
Modification : Created function
*****************************************************************************/
Int32 KeyStone_disable_PSC_Power_Domain (Uint32 pwrDmnNum)
{
Uint32 uiStartTSC= TSCL;
/* Set Power domain to OFF */
CSL_PSC_disablePowerDomain (pwrDmnNum);
/* Start the state transition */
CSL_PSC_startStateTransition (pwrDmnNum);
/* Wait until the state transition process is completed. */
while (!CSL_PSC_isStateTransitionDone (pwrDmnNum))
{
if(TSC_count_cycle_from(uiStartTSC)>0x3FFFFFFF)
{
printf("Disable power domain %d timeout!\n", pwrDmnNum);
return -2;
}
}
/* Return PSC status */
if (CSL_PSC_getPowerDomainState(pwrDmnNum) == PSC_PDSTATE_OFF)
{
/*Ready for use */
return 0;
}
else
{
/*Return error */
return -1;
}
}
/*============================EDMA=====================================*/
/*****************************************************************************
Prototype : EDMA_channel_TC_cfg
Description : Setup uiChannel of an EDMA to use uiTC
Input : Uint32 uiCC
Uint32 uiChannel
Uint32 uiTC
Output : None
Return Value :
History :
1.Date : 2010/12/12
Author : Brighton Feng
Modification : Created function
*****************************************************************************/
void EDMA_channel_TC_cfg (Uint32 uiCC,
Uint32 uiChannel, Uint32 uiTC)
{
gpEDMA_CC_regs[uiCC]->TPCC_DMAQNUM[uiChannel/8] =
(gpEDMA_CC_regs[uiCC]->TPCC_DMAQNUM[uiChannel/8]&(~(0xF<<((uiChannel&7)*4))))
|(uiTC<<((uiChannel&7)*4));
}
/*****************************************************************************
Prototype : EDMA_TC_priority_cfg
Description : Setup uiChannel of an EDMA TC priority
Input : Uint32 uiCC
Uint32 uiPri
Uint32 uiTC
Output : None
Return Value :
History :
1.Date : 2010/12/12
Author : Brighton Feng
Modification : Created function
*****************************************************************************/
void EDMA_TC_priority_cfg(Uint32 uiCC,
Uint32 uiPri, Uint32 uiTC)
{
gpEDMA_CC_regs[uiCC]->TPCC_QUEPRI=
(gpEDMA_CC_regs[uiCC]->TPCC_QUEPRI&(~(0xF<<((uiTC&3)*4))))
|(uiPri<<((uiTC&3)*4));
}
/*****************************************************************************
Prototype : EDMA_init
Description : Initialize all EDMA registers and clear the event
Input : None
Output : None
Return Value :
History :
1.Date : 2010/12/12
Author : Brighton Feng
Modification : Created function
*****************************************************************************/
void EDMA_init ()
{
int i;
unsigned int * uipPaRAM;