summaryrefslogtreecommitdiff
path: root/nuttx/arch/arm/src/sam34/sam4l_periphclks.c
blob: 2747ac6cbe733fa3f8449d16ccbec5350052caef (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
/****************************************************************************
 * arch/avr/src/sam34/sam4l_periphclks.c
 *
 *   Copyright (C) 2013 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * This file is derived from nuttx/arch/avr/src/at32uc3/at32uc3_clkinit.c
 *
 * 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.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * 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.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <arch/irq.h>
#include <arch/board/board.h>

#include "up_arch.h"

#include "up_internal.h"
#include "chip/sam4l_pm.h"

#include "sam4l_periphclks.h"

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
/* USBC source clock selection */

#ifdef CONFIG_SAM34_USBC
#  if defined(BOARD_USBC_SRC_OSC0)
#    define SAM_USBC_GCLK_SOURCE SCIF_GCCTRL_OSCSEL_OSC0
#  elif defined(BOARD_USBC_SRC_PLL0)
#    define SAM_USBC_GCLK_SOURCE SCIF_GCCTRL_OSCSEL_PLL0
#  elif defined(BOARD_USBC_SRC_DFLL)
#    define SAM_USBC_GCLK_SOURCE SCIF_GCCTRL_OSCSEL_DFLL0
#  elif defined(BOARD_USBC_SRC_GCLKIN0)
#    define SAM_USBC_GCLK_SOURCE SCIF_GCCTRL_OSCSEL_GCLKIN0
#  else
#    error No USBC GCLK7 source clock defined
#  endif
#endif

/****************************************************************************
 * Private Types
 ****************************************************************************/

/****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/****************************************************************************
 * Global Variables
 ****************************************************************************/

/****************************************************************************
 * Private Variables
 ****************************************************************************/

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: sam_init_cpumask
 *
 * Description:
 *   Called during boot to enable clocking on selected peripherals in the
 *   CPU mask register.
 *
 ****************************************************************************/

static inline void sam_init_cpumask(void)
{
  uint32_t mask = 0;

  /* OR in the user selected peripherals */

#ifdef CONFIG_SAM34_RESET_PERIPHCLKS
#ifdef CONFIG_SAM34_OCD
  mask |= PM_CPUMASK_OCD;             /* On-Chip Debug */
#endif
#endif

  /* Save the new CPU mask */

  putreg32(PM_UNLOCK_KEY(0xaa) | PM_UNLOCK_ADDR(SAM_PM_CPUMASK_OFFSET),
           SAM_PM_UNLOCK);
  putreg32(mask, SAM_PM_CPUMASK);
}

/****************************************************************************
 * Name: sam_init_hsbmask
 *
 * Description:
 *   Called during boot to enable clocking on selected peripherals in the
 *   HSB mask register.
 *
 ****************************************************************************/

static inline void sam_init_hsbmask(void)
{
  /* Select the non-optional peripherals */

  uint32_t mask = (PM_HSBMASK_FLASHCALW | PM_HSBMASK_APBB |
                   PM_HSBMASK_APBC | PM_HSBMASK_APBD);

  /* OR in the user selected peripherals */

#ifdef CONFIG_SAM34_RESET_PERIPHCLKS
#ifdef CONFIG_SAM34_PDCA
  mask |= PM_HSBMASK_PDCA;            /* PDCA */
#endif
#ifdef CONFIG_SAM34_HRAMC1
  mask |= PM_HSBMASK_HRAMC1;          /* HRAMC1 (picoCache RAM) */
#endif
#ifdef CONFIG_SAM34_USBC
  mask |= PM_HSBMASK_USBC;            /* USBC */
#endif
#ifdef CONFIG_SAM34_CRCCU
  mask |= PM_HSBMASK_CRCCU;           /* CRCCU */
#endif
#ifdef CONFIG_SAM34_APBA
  mask |= PM_HSBMASK_APBA;            /* APBA bridge */
#endif
#ifdef CONFIG_SAM34_AESA
  mask |= PM_HSBMASK_AESA;            /* AESA */
#endif
#endif

  /* Save the new HSB mask */

  putreg32(PM_UNLOCK_KEY(0xaa) | PM_UNLOCK_ADDR(SAM_PM_HSBMASK_OFFSET),
           SAM_PM_UNLOCK);
  putreg32(mask, SAM_PM_HSBMASK);
}

/****************************************************************************
 * Name: sam_init_pbamask
 *
 * Description:
 *   Called during boot to enable clocking on selected peripherals in the
 *   PBA mask register.
 *
 ****************************************************************************/

static inline void sam_init_pbamask(void)
{
  /* Select the non-optional peripherals */

  uint32_t mask = 0;
  uint32_t divmask = 0;

  /* OR in the user selected peripherals */

#ifdef CONFIG_SAM34_RESET_PERIPHCLKS
#ifdef CONFIG_SAM34_IISC
  mask    |= PM_PBAMASK_IISC;         /* IISC */
#endif
#ifdef CONFIG_SAM34_SPI0
  mask    |= PM_PBAMASK_SPI;          /* SPI */
#endif
#ifdef CONFIG_SAM34_TC0
  mask    |= PM_PBAMASK_TC0;          /* TC0 */
  divmask |= PM_PBADIVMASK_TIMER_CLOCKS;
#endif
#ifdef CONFIG_SAM34_TC1
  mask    |= PM_PBAMASK_TC1;          /* TC1 */
  divmask |= PM_PBADIVMASK_TIMER_CLOCKS;
#endif
#ifdef CONFIG_SAM34_TWIM0
  mask    |= PM_PBAMASK_TWIM0;        /* TWIM0 */
#endif
#ifdef CONFIG_SAM34_TWIS0
  mask    |= PM_PBAMASK_TWIS0;        /* TWIS0 */
#endif
#ifdef CONFIG_SAM34_TWIM1
  mask    |= PM_PBAMASK_TWIM1;        /* TWIM1 */
#endif
#ifdef CONFIG_SAM34_TWIS1
  mask    |= PM_PBAMASK_TWIS1;        /* TWIS1 */
#endif
#ifdef CONFIG_SAM34_USART0
  mask    |= PM_PBAMASK_USART0;       /* USART0 */
  divmask |= PM_PBADIVMASK_CLK_USART;
#endif
#ifdef CONFIG_SAM34_USART1
  mask    |= PM_PBAMASK_USART1;       /* USART1 */
  divmask |= PM_PBADIVMASK_CLK_USART;
#endif
#ifdef CONFIG_SAM34_USART2
  mask    |= PM_PBAMASK_USART2;       /* USART2 */
  divmask |= PM_PBADIVMASK_CLK_USART;
#endif
#ifdef CONFIG_SAM34_USART3
  mask    |= PM_PBAMASK_USART3;       /* USART3 */
  divmask |= PM_PBADIVMASK_CLK_USART;
#endif
#ifdef CONFIG_SAM34_ADCIFE
  mask    |= PM_PBAMASK_ADCIFE;       /* ADCIFE */
#endif
#ifdef CONFIG_SAM34_DACC
  mask    |= PM_PBAMASK_DACC;         /* DACC */
#endif
#ifdef CONFIG_SAM34_ACIFC
  mask    |= PM_PBAMASK_ACIFC;        /* ACIFC */
#endif
#ifdef CONFIG_SAM34_GLOC
  mask    |= PM_PBAMASK_GLOC;         /* GLOC */
#endif
#ifdef CONFIG_SAM34_ABDACB
  mask    |= PM_PBAMASK_ABDACB;       /* ABDACB */
#endif
#ifdef CONFIG_SAM34_TRNG
  mask    |= PM_PBAMASK_TRNG;         /* TRNG */
#endif
#ifdef CONFIG_SAM34_PARC
  mask    |= PM_PBAMASK_PARC;         /* PARC */
#endif
#ifdef CONFIG_SAM34_CATB
  mask    |= PM_PBAMASK_CATB;         /* CATB */
#endif
#ifdef CONFIG_SAM34_TWIM2
  mask    |= PM_PBAMASK_TWIM2;        /* TWIM2 */
#endif
#ifdef CONFIG_SAM34_TWIM3
  mask    |= PM_PBAMASK_TWIM3;        /* TWIM3 */
#endif
#ifdef CONFIG_SAM34_LCDCA
  mask    |= PM_PBAMASK_LCDCA;        /* LCDCA*/
#endif
#endif

  /* Save the new PBA mask */

  putreg32(PM_UNLOCK_KEY(0xaa) | PM_UNLOCK_ADDR(SAM_PM_PBAMASK_OFFSET),
           SAM_PM_UNLOCK);
  putreg32(mask, SAM_PM_PBAMASK);

  /* Set the peripheral divider mask as necessary */

  putreg32(PM_UNLOCK_KEY(0xaa) | PM_UNLOCK_ADDR(SAM_PM_PBADIVMASK_OFFSET),
           SAM_PM_UNLOCK);
  putreg32(divmask, SAM_PM_PBADIVMASK);
}

/****************************************************************************
 * Name: sam_init_pbbmask
 *
 * Description:
 *   Called during boot to enable clocking on selected peripherals in the
 *   PBB mask register.
 *
 ****************************************************************************/

static inline void sam_init_pbbmask(void)
{
  /* Select the non-optional peripherals */

  uint32_t mask = PM_PBBMASK_FLASHCALW;

  /* OR in the user selected peripherals */

#ifdef CONFIG_SAM34_RESET_PERIPHCLKS
#ifdef CONFIG_SAM34_HRAMC1
  mask |= PM_PBBMASK_HRAMC1;          /* HRAMC1 */
#endif
#ifdef CONFIG_SAM34_HMATRIX
  mask |= PM_PBBMASK_HMATRIX;         /* HMATRIX */
#endif
#ifdef CONFIG_SAM34_PDCA
  mask |= PM_PBBMASK_PDCA;            /* PDCA */
#endif
#ifdef CONFIG_SAM34_CRCCU
  mask |= PM_PBBMASK_CRCCU;           /* CRCCU */
#endif
#ifdef CONFIG_SAM34_USBC
  mask |= PM_PBBMASK_USBC;            /* USBC */
#endif
#ifdef CONFIG_SAM34_PEVC
  mask |= PM_PBBMASK_PEVC;            /* PEVC */
#endif
#endif

  /* Save the new PBB mask */

  putreg32(PM_UNLOCK_KEY(0xaa) | PM_UNLOCK_ADDR(SAM_PM_PBBMASK_OFFSET),
           SAM_PM_UNLOCK);
  putreg32(mask, SAM_PM_PBBMASK);
}

/****************************************************************************
 * Name: sam_init_pbcmask
 *
 * Description:
 *   Called during boot to enable clocking on selected peripherals in the
 *   PBC mask register.
 *
 ****************************************************************************/

static inline void sam_init_pbcmask(void)
{
  /* Select the non-optional peripherals */

  uint32_t mask = (PM_PBCMASK_PM | PM_PBCMASK_SCIF | PM_PBCMASK_GPIO);

  /* OR in the user selected peripherals */

#ifdef CONFIG_SAM34_RESET_PERIPHCLKS
#ifdef CONFIG_SAM34_CHIPID
  mask |= PM_PBCMASK_CHIPID;          /* CHIPID */
#endif
#ifdef CONFIG_SAM34_FREQM
  mask |= PM_PBCMASK_FREQM;           /* FREQM */
#endif
#endif

  /* Save the new PBC mask */

  putreg32(PM_UNLOCK_KEY(0xaa) | PM_UNLOCK_ADDR(SAM_PM_PBCMASK_OFFSET),
           SAM_PM_UNLOCK);
  putreg32(mask, SAM_PM_PBCMASK);
}

/****************************************************************************
 * Name: sam_init_pbdmask
 *
 * Description:
 *   Called during boot to enable clocking on selected peripherals in the
 *   PBD mask register.
 *
 ****************************************************************************/

static inline void sam_init_pbdmask(void)
{
  /* Select the non-optional peripherals */

  uint32_t mask = (PM_PBDMASK_BPM | PM_PBDMASK_BSCIF);

  /* OR in the user selected peripherals */

#ifdef CONFIG_SAM34_RESET_PERIPHCLKS
#ifdef CONFIG_SAM34_AST
  mask |= PM_PBDMASK_AST;             /* AST */
#endif
#ifdef CONFIG_SAM34_WDT
  mask |= PM_PBDMASK_WDT;             /* WDT */
#endif
#ifdef CONFIG_SAM34_EIC
  mask |= PM_PBDMASK_EIC;             /* EIC */
#endif
#ifdef CONFIG_SAM34_PICOUART
  mask |= PM_PBDMASK_PICOUART;        /* PICOUART */
#endif
#endif

  /* Save the new PBD mask */

  putreg32(PM_UNLOCK_KEY(0xaa) | PM_UNLOCK_ADDR(SAM_PM_PBDMASK_OFFSET),
           SAM_PM_UNLOCK);
  putreg32(mask, SAM_PM_PBDMASK);
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: sam_init_periphclks
 *
 * Description:
 *   Called during boot to enable clocking on all selected peripherals.
 *
 ****************************************************************************/

void sam_init_periphclks(void)
{
  sam_init_cpumask();
  sam_init_hsbmask();
  sam_init_pbamask();
  sam_init_pbbmask();
  sam_init_pbcmask();
  sam_init_pbdmask();
}

/****************************************************************************
 * Name: sam_modifyperipheral
 *
 * Description:
 *   This is a convenience function that is intended to be used to enable
 *   or disable peripheral module clocking.
 *
 ****************************************************************************/

void sam_modifyperipheral(uintptr_t regaddr, uint32_t clrbits,
                          uint32_t setbits)
{
  irqstate_t flags;
  uint32_t regval;

  /* Make sure that the following operations are atomic */

  flags = irqsave();

  /* Enable/disabling clocking */

  regval  = getreg32(regaddr);
  regval &= ~clrbits;
  regval |= setbits;
  putreg32(PM_UNLOCK_KEY(0xaa) | PM_UNLOCK_ADDR(regaddr - SAM_PM_BASE),
           SAM_PM_UNLOCK);
  putreg32(regval, regaddr);

  irqrestore(flags);
}

/****************************************************************************
 * Name: sam_pba_modifydivmask
 *
 * Description:
 *   This is a convenience function that is intended to be used to modify
 *   bits in the PBA divided clock (DIVMASK) register.
 *
 ****************************************************************************/

void sam_pba_modifydivmask(uint32_t clrbits, uint32_t setbits)
{
  irqstate_t flags;
  uint32_t regval;

  /* Make sure that the following operations are atomic */

  flags = irqsave();

  /* Modify the PBA DIVMASK */

  regval  = getreg32(SAM_PM_PBADIVMASK);
  regval &= ~clrbits;
  regval |= setbits;
  putreg32(PM_UNLOCK_KEY(0xaa) | PM_UNLOCK_ADDR(SAM_PM_PBADIVMASK_OFFSET),
           SAM_PM_UNLOCK);
  putreg32(regval, SAM_PM_PBADIVMASK);

  irqrestore(flags);
}

/****************************************************************************
 * Name: sam_pba_enableperipheral
 *
 * Description:
 *   This is a convenience function to enable a peripheral on the APBA
 *   bridge.
 *
 ****************************************************************************/

void sam_pba_enableperipheral(uint32_t bitset)
{
  irqstate_t flags;

  /* The following operations must be atomic */

  flags = irqsave();

  /* Enable the APBA bridge if necessary */

  if (getreg32(SAM_PM_PBAMASK) == 0)
    {
      sam_hsb_enableperipheral(PM_HSBMASK_APBA);
    }

  irqrestore(flags);

  /* Enable the module */

  sam_enableperipheral(SAM_PM_PBAMASK, bitset);
}

/****************************************************************************
 * Name: sam_pba_disableperipheral
 *
 * Description:
 *   This is a convenience function to disable a peripheral on the APBA
 *   bridge.
 *
 ****************************************************************************/

void sam_pba_disableperipheral(uint32_t bitset)
{
  irqstate_t flags;

  /* Disable clocking to the module */

  sam_disableperipheral(SAM_PM_PBAMASK, bitset);

  /* Disable the APBA bridge if possible */

  flags = irqsave();

  if (getreg32(SAM_PM_PBAMASK) == 0)
    {
      sam_hsb_disableperipheral(PM_HSBMASK_APBA);
    }

   /* Disable PBA UART divided clock if none of the UARTS are in use */

  if ((getreg32(SAM_PM_PBAMASK) & PM_PBAMASK_UARTS) == 0)
    {
      sam_pba_disabledivmask(PM_PBADIVMASK_CLK_USART);
    }

   /* Disable PBA TIMER divided clocks if none of the UARTS are in use */

  if ((getreg32(SAM_PM_PBAMASK) & PM_PBAMASK_TIMERS) == 0)
    {
      sam_pba_disabledivmask(PM_PBADIVMASK_TIMER_CLOCKS);
    }

  irqrestore(flags);
}

/****************************************************************************
 * Name: sam_pbb_enableperipheral
 *
 * Description:
 *   This is a convenience function to enable a peripheral on the APBB
 *   bridge.
 *
 ****************************************************************************/

void sam_pbb_enableperipheral(uint32_t bitset)
{
  irqstate_t flags;

  /* The following operations must be atomic */

  flags = irqsave();

  /* Enable the APBB bridge if necessary */

  if (getreg32(SAM_PM_PBBMASK) == 0)
    {
      sam_hsb_enableperipheral(PM_HSBMASK_APBB);
    }

  irqrestore(flags);

  /* Enable the module */

  sam_enableperipheral(SAM_PM_PBBMASK, bitset);
}

/****************************************************************************
 * Name: sam_pbb_disableperipheral
 *
 * Description:
 *   This is a convenience function to disable a peripheral on the APBA
 *   bridge.
 *
 ****************************************************************************/

void sam_pbb_disableperipheral(uint32_t bitset)
{
  irqstate_t flags;

  /* Disable clocking to the peripheral module */

  sam_disableperipheral(SAM_PM_PBBMASK, bitset);

  /* Disable the APBB bridge if possible */

  flags = irqsave();

  if (getreg32(SAM_PM_PBBMASK) == 0)
    {
      sam_hsb_disableperipheral(PM_HSBMASK_APBB);
    }

  irqrestore(flags);
}

/****************************************************************************
 * Name: sam_usbc_enableclk
 *
 * Description:
 *   Enable clocking for the USBC using settings from the board.h header files.
 *
 *  "The USBC has two bus clocks connected: One High Speed Bus clock
 *   (CLK_USBC_AHB) and one Peripheral Bus clock (CLK_USBC_APB). These clocks
 *   are generated by the Power Manager.  Both clocks are enabled at reset
 *   and can be disabled by the Power Manager. It is recommended to disable
 *   the USBC before disabling the clocks, to avoid freezing the USBC in
 *   an undefined state.
 *
 *  "To follow the usb data rate at 12Mbit/s in full-speed mode, the
 *   CLK_USBC_AHB clock should be at minimum 12MHz.
 *
 *  "The 48MHz USB clock is generated by a dedicated generic clock from
 *   the SCIF module. Before using the USB, the user must ensure that the
 *   USB generic clock (GCLK_USBC) is enabled at 48MHz in the SCIF module."
 *
 ****************************************************************************/

#ifdef CONFIG_SAM34_USBC
void sam_usbc_enableclk(void)
{
  irqstate_t flags;
  uint32_t regval;

  /* Enable USBC clocking (possibly along with the PBB peripheral bridge) */

  flags = irqsave();
  sam_hsb_enableperipheral(PM_HSBMASK_USBC);
  sam_pbb_enableperipheral(PM_PBBMASK_USBC);

  /* Reset generic clock 7 */

  putreg32(0, SAM_SCIF_GCCTRL7);

  /* Set the generic clock source */

  regval  = getreg32(SAM_SCIF_GCCTRL7);
  regval &= ~SCIF_GCCTRL_OSCSEL_MASK;
  regval |= SAM_USBC_GCLK_SOURCE;
  putreg32(regval, SAM_SCIF_GCCTRL7);

  /* Set the generic clock divider */

  regval  = getreg32(SAM_SCIF_GCCTRL7);
  regval &= ~(SCIF_GCCTRL_DIVEN | SCIF_GCCTRL_DIV_MASK);

#if BOARD_USBC_GCLK_DIV > 1
  regval |= SCIF_GCCTRL_DIVEN;
  regval |= SCIF_GCCTRL_DIV(((divider + 1) / 2) - 1);
#endif

  putreg32(regval, SAM_SCIF_GCCTRL7);

  /* Enable the generic clock */

  regval  = getreg32(SAM_SCIF_GCCTRL7);
  regval |= SCIF_GCCTRL_CEN;
  putreg32(regval, SAM_SCIF_GCCTRL7);
  irqrestore(flags);
}
#endif /* CONFIG_SAM34_USBC */

/****************************************************************************
 * Name: sam_usbc_disableclk
 *
 * Description:
 *   Disable clocking to the USBC.
 *
 ****************************************************************************/

#ifdef CONFIG_SAM34_USBC
void sam_usbc_disableclk(void)
{
  putreg32(0, SAM_SCIF_GCCTRL7);
  sam_pbb_enableperipheral(PM_PBBMASK_USBC);
  sam_hsb_enableperipheral(PM_HSBMASK_USBC);
}
#endif /* CONFIG_SAM34_USBC */