blob: 64cbc3d1ed4c066746b320063c8e4f13cf1b573a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wenyou Yang41bf25c2016-02-03 10:16:48 +08002/*
3 * Copyright (C) 2015 Atmel Corporation
4 * Wenyou Yang <wenyou.yang@atmel.com>
Wenyou Yang41bf25c2016-02-03 10:16:48 +08005 */
6
7#include <common.h>
8#include <asm/io.h>
9#include <asm/arch/hardware.h>
10#include <asm/arch/at91_pmc.h>
11
Wenyou Yang1e70b372016-02-02 11:11:51 +080012#define EN_UPLL_TIMEOUT 500
13
Wenyou Yang41bf25c2016-02-03 10:16:48 +080014void at91_periph_clk_enable(int id)
15{
16 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
17
18#ifdef CPU_HAS_PCR
19 u32 regval;
20 u32 div_value;
21
22 if (id > AT91_PMC_PCR_PID_MASK)
23 return;
24
25 writel(id, &pmc->pcr);
26
27 div_value = readl(&pmc->pcr) & AT91_PMC_PCR_DIV;
28
29 regval = AT91_PMC_PCR_EN | AT91_PMC_PCR_CMD_WRITE | id | div_value;
30
31 writel(regval, &pmc->pcr);
32#else
33 writel(0x01 << id, &pmc->pcer);
34#endif
35}
36
37void at91_periph_clk_disable(int id)
38{
39 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
40
41#ifdef CPU_HAS_PCR
42 u32 regval;
43
44 if (id > AT91_PMC_PCR_PID_MASK)
45 return;
46
47 regval = AT91_PMC_PCR_CMD_WRITE | id;
48
49 writel(regval, &pmc->pcr);
50#else
51 writel(0x01 << id, &pmc->pcdr);
52#endif
53}
54
55void at91_system_clk_enable(int sys_clk)
56{
57 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
58
59 writel(sys_clk, &pmc->scer);
60}
61
62void at91_system_clk_disable(int sys_clk)
63{
64 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
65
66 writel(sys_clk, &pmc->scdr);
67}
Wenyou Yang1e70b372016-02-02 11:11:51 +080068
69int at91_upll_clk_enable(void)
70{
71 struct at91_pmc *pmc = (at91_pmc_t *)ATMEL_BASE_PMC;
72 ulong start_time, tmp_time;
73
74 if ((readl(&pmc->uckr) & AT91_PMC_UPLLEN) == AT91_PMC_UPLLEN)
75 return 0;
76
77 start_time = get_timer(0);
78 writel(AT91_PMC_UPLLEN | AT91_PMC_BIASEN, &pmc->uckr);
79 while ((readl(&pmc->sr) & AT91_PMC_LOCKU) != AT91_PMC_LOCKU) {
80 tmp_time = get_timer(0);
81 if ((tmp_time - start_time) > EN_UPLL_TIMEOUT) {
82 printf("ERROR: failed to enable UPLL\n");
83 return -1;
84 }
85 }
86
87 return 0;
88}
89
90int at91_upll_clk_disable(void)
91{
92 struct at91_pmc *pmc = (at91_pmc_t *)ATMEL_BASE_PMC;
93 ulong start_time, tmp_time;
94
95 start_time = get_timer(0);
96 writel(readl(&pmc->uckr) & ~AT91_PMC_UPLLEN, &pmc->uckr);
97 while ((readl(&pmc->sr) & AT91_PMC_LOCKU) == AT91_PMC_LOCKU) {
98 tmp_time = get_timer(0);
99 if ((tmp_time - start_time) > EN_UPLL_TIMEOUT) {
100 printf("ERROR: failed to stop UPLL\n");
101 return -1;
102 }
103 }
104
105 return 0;
106}
107
108void at91_usb_clk_init(u32 value)
109{
110 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
111
112 writel(value, &pmc->usb);
113}
Wenyou Yangc0b868c2016-02-02 12:46:12 +0800114
115void at91_pllicpr_init(u32 icpr)
116{
117 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
118
119 writel(icpr, &pmc->pllicpr);
120}