blob: a3493eefe359b4ebe44e30801b2d334840f18bd8 [file] [log] [blame]
Christina Quastb0a05702014-11-28 10:27:32 +01001/* ----------------------------------------------------------------------------
2 * ATMEL Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2009, Atmel Corporation
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the disclaimer below.
13 *
14 * Atmel's name may not be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * ----------------------------------------------------------------------------
28 */
29
30/**
31 * \file
32 *
33 * Provides the low-level initialization function that called on chip startup.
34 */
35
36/*----------------------------------------------------------------------------
37 * Headers
38 *----------------------------------------------------------------------------*/
39
40#include "board.h"
Christina Quastb0a05702014-11-28 10:27:32 +010041
42/*----------------------------------------------------------------------------
43 * Local definitions
44 *----------------------------------------------------------------------------*/
45
Christina Quast530f2082014-12-05 13:03:59 +010046/* Clock settings at 48MHz for 18 MHz crystal */
Christina Quastb0a05702014-11-28 10:27:32 +010047#if (BOARD_MCK == 48000000)
48#define BOARD_OSCOUNT (CKGR_MOR_MOSCXTST(0x8))
49#define BOARD_PLLAR (CKGR_PLLAR_STUCKTO1 \
Christina Quast530f2082014-12-05 13:03:59 +010050 | CKGR_PLLAR_MULA(0xc) \
Christina Quastb0a05702014-11-28 10:27:32 +010051 | CKGR_PLLAR_PLLACOUNT(0x1) \
Christina Quast530f2082014-12-05 13:03:59 +010052 | CKGR_PLLAR_DIVA(0x5))
53#define BOARD_MCKR (PMC_MCKR_PRES_CLK | PMC_MCKR_CSS_PLLA_CLK)
Christina Quastb0a05702014-11-28 10:27:32 +010054
Christina Quast530f2082014-12-05 13:03:59 +010055/* Clock settings at 64MHz for 18 MHz crystal */
Christina Quastb0a05702014-11-28 10:27:32 +010056#elif (BOARD_MCK == 64000000)
57#define BOARD_OSCOUNT (CKGR_MOR_MOSCXTST(0x8))
58#define BOARD_PLLAR (CKGR_PLLAR_STUCKTO1 \
Christina Quast530f2082014-12-05 13:03:59 +010059 | CKGR_PLLAR_MULA(0x06) \
Christina Quastb0a05702014-11-28 10:27:32 +010060 | CKGR_PLLAR_PLLACOUNT(0x1) \
Christina Quast530f2082014-12-05 13:03:59 +010061 | CKGR_PLLAR_DIVA(0x2))
Christina Quastb0a05702014-11-28 10:27:32 +010062#define BOARD_MCKR (PMC_MCKR_PRES_CLK | PMC_MCKR_CSS_PLLA_CLK)
63
64#else
65 #error "No settings for current BOARD_MCK."
66#endif
67
68/* Define clock timeout */
69#define CLOCK_TIMEOUT 0xFFFFFFFF
70
71/*----------------------------------------------------------------------------
72 * Exported functions
73 *----------------------------------------------------------------------------*/
74
75/**
76 * \brief Performs the low-level initialization of the chip.
77 * This includes EFC and master clock configuration.
78 * It also enable a low level on the pin NRST triggers a user reset.
79 */
Christina Quast8be71e42014-12-02 13:06:01 +010080extern WEAK void LowLevelInit( void )
Christina Quastb0a05702014-11-28 10:27:32 +010081{
82 uint32_t timeout = 0;
83
84 /* Set 3 FWS for Embedded Flash Access */
85 EFC->EEFC_FMR = EEFC_FMR_FWS(3);
86
87 /* Select external slow clock */
Christina Quast8be71e42014-12-02 13:06:01 +010088/* if ((SUPC->SUPC_SR & SUPC_SR_OSCSEL) != SUPC_SR_OSCSEL_CRYST)
Christina Quastb0a05702014-11-28 10:27:32 +010089 {
90 SUPC->SUPC_CR = (uint32_t)(SUPC_CR_XTALSEL_CRYSTAL_SEL | SUPC_CR_KEY(0xA5));
91 timeout = 0;
92 while (!(SUPC->SUPC_SR & SUPC_SR_OSCSEL_CRYST) );
93 }
Christina Quast8be71e42014-12-02 13:06:01 +010094*/
Christina Quastb0a05702014-11-28 10:27:32 +010095
96 /* Initialize main oscillator */
Christina Quast8be71e42014-12-02 13:06:01 +010097/* if ( !(PMC->CKGR_MOR & CKGR_MOR_MOSCSEL) )
Christina Quastb0a05702014-11-28 10:27:32 +010098 {
99 PMC->CKGR_MOR = CKGR_MOR_KEY(0x37) | BOARD_OSCOUNT | CKGR_MOR_MOSCRCEN | CKGR_MOR_MOSCXTEN;
100 timeout = 0;
101 while (!(PMC->PMC_SR & PMC_SR_MOSCXTS) && (timeout++ < CLOCK_TIMEOUT));
Christina Quast8be71e42014-12-02 13:06:01 +0100102 }*/
Christina Quastb0a05702014-11-28 10:27:32 +0100103
104 /* Switch to 3-20MHz Xtal oscillator */
105 PMC->CKGR_MOR = CKGR_MOR_KEY(0x37) | BOARD_OSCOUNT | CKGR_MOR_MOSCRCEN | CKGR_MOR_MOSCXTEN | CKGR_MOR_MOSCSEL;
Harald Welte46783882016-02-29 19:45:59 +0100106 /* wait for Main XTAL oscillator stabilization */
Christina Quastb0a05702014-11-28 10:27:32 +0100107 timeout = 0;
108 while (!(PMC->PMC_SR & PMC_SR_MOSCSELS) && (timeout++ < CLOCK_TIMEOUT));
Harald Welte46783882016-02-29 19:45:59 +0100109
110 /* "switch" to main clock as master clock source (should already be the case */
Christina Quastb0a05702014-11-28 10:27:32 +0100111 PMC->PMC_MCKR = (PMC->PMC_MCKR & ~(uint32_t)PMC_MCKR_CSS_Msk) | PMC_MCKR_CSS_MAIN_CLK;
Harald Welte46783882016-02-29 19:45:59 +0100112 /* wait for master clock to be ready */
Christina Quastb0a05702014-11-28 10:27:32 +0100113 for ( timeout = 0; !(PMC->PMC_SR & PMC_SR_MCKRDY) && (timeout++ < CLOCK_TIMEOUT) ; );
114
115 /* Initialize PLLA */
116 PMC->CKGR_PLLAR = BOARD_PLLAR;
Harald Welte46783882016-02-29 19:45:59 +0100117 /* Wait for PLLA to lock */
Christina Quastb0a05702014-11-28 10:27:32 +0100118 timeout = 0;
119 while (!(PMC->PMC_SR & PMC_SR_LOCKA) && (timeout++ < CLOCK_TIMEOUT));
120
Harald Welte46783882016-02-29 19:45:59 +0100121 /* Switch to main clock (again ?!?) */
Christina Quastb0a05702014-11-28 10:27:32 +0100122 PMC->PMC_MCKR = (BOARD_MCKR & ~PMC_MCKR_CSS_Msk) | PMC_MCKR_CSS_MAIN_CLK;
Harald Welte46783882016-02-29 19:45:59 +0100123 /* wait for master clock to be ready */
Christina Quastb0a05702014-11-28 10:27:32 +0100124 for ( timeout = 0; !(PMC->PMC_SR & PMC_SR_MCKRDY) && (timeout++ < CLOCK_TIMEOUT) ; );
125
Harald Welte46783882016-02-29 19:45:59 +0100126 /* switch to PLLA as master clock source */
Christina Quastb0a05702014-11-28 10:27:32 +0100127 PMC->PMC_MCKR = BOARD_MCKR ;
Harald Welte46783882016-02-29 19:45:59 +0100128 /* wait for master clock to be ready */
Christina Quastb0a05702014-11-28 10:27:32 +0100129 for ( timeout = 0; !(PMC->PMC_SR & PMC_SR_MCKRDY) && (timeout++ < CLOCK_TIMEOUT) ; );
130}