blob: 1d3df2c09de0dcd3345fc3f8faf651a0b094aa81 [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>
Stefan Roese256c2ff2019-04-03 07:37:40 +02008#include <dm.h>
9#include <wdt.h>
Wenyou Yang41bf25c2016-02-03 10:16:48 +080010#include <asm/io.h>
11#include <asm/arch/hardware.h>
12#include <asm/arch/at91_pmc.h>
Stefan Roese256c2ff2019-04-03 07:37:40 +020013#include <asm/arch/at91_wdt.h>
Wenyou Yang41bf25c2016-02-03 10:16:48 +080014
Wenyou Yang1e70b372016-02-02 11:11:51 +080015#define EN_UPLL_TIMEOUT 500
16
Stefan Roese256c2ff2019-04-03 07:37:40 +020017static struct udevice *watchdog_dev __attribute__((section(".data"))) = NULL;
18
Wenyou Yang41bf25c2016-02-03 10:16:48 +080019void at91_periph_clk_enable(int id)
20{
21 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
22
23#ifdef CPU_HAS_PCR
24 u32 regval;
25 u32 div_value;
26
27 if (id > AT91_PMC_PCR_PID_MASK)
28 return;
29
30 writel(id, &pmc->pcr);
31
32 div_value = readl(&pmc->pcr) & AT91_PMC_PCR_DIV;
33
34 regval = AT91_PMC_PCR_EN | AT91_PMC_PCR_CMD_WRITE | id | div_value;
35
36 writel(regval, &pmc->pcr);
37#else
38 writel(0x01 << id, &pmc->pcer);
39#endif
40}
41
42void at91_periph_clk_disable(int id)
43{
44 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
45
46#ifdef CPU_HAS_PCR
47 u32 regval;
48
49 if (id > AT91_PMC_PCR_PID_MASK)
50 return;
51
52 regval = AT91_PMC_PCR_CMD_WRITE | id;
53
54 writel(regval, &pmc->pcr);
55#else
56 writel(0x01 << id, &pmc->pcdr);
57#endif
58}
59
60void at91_system_clk_enable(int sys_clk)
61{
62 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
63
64 writel(sys_clk, &pmc->scer);
65}
66
67void at91_system_clk_disable(int sys_clk)
68{
69 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
70
71 writel(sys_clk, &pmc->scdr);
72}
Wenyou Yang1e70b372016-02-02 11:11:51 +080073
74int at91_upll_clk_enable(void)
75{
76 struct at91_pmc *pmc = (at91_pmc_t *)ATMEL_BASE_PMC;
77 ulong start_time, tmp_time;
78
79 if ((readl(&pmc->uckr) & AT91_PMC_UPLLEN) == AT91_PMC_UPLLEN)
80 return 0;
81
82 start_time = get_timer(0);
83 writel(AT91_PMC_UPLLEN | AT91_PMC_BIASEN, &pmc->uckr);
84 while ((readl(&pmc->sr) & AT91_PMC_LOCKU) != AT91_PMC_LOCKU) {
85 tmp_time = get_timer(0);
86 if ((tmp_time - start_time) > EN_UPLL_TIMEOUT) {
87 printf("ERROR: failed to enable UPLL\n");
88 return -1;
89 }
90 }
91
92 return 0;
93}
94
95int at91_upll_clk_disable(void)
96{
97 struct at91_pmc *pmc = (at91_pmc_t *)ATMEL_BASE_PMC;
98 ulong start_time, tmp_time;
99
100 start_time = get_timer(0);
101 writel(readl(&pmc->uckr) & ~AT91_PMC_UPLLEN, &pmc->uckr);
102 while ((readl(&pmc->sr) & AT91_PMC_LOCKU) == AT91_PMC_LOCKU) {
103 tmp_time = get_timer(0);
104 if ((tmp_time - start_time) > EN_UPLL_TIMEOUT) {
105 printf("ERROR: failed to stop UPLL\n");
106 return -1;
107 }
108 }
109
110 return 0;
111}
112
113void at91_usb_clk_init(u32 value)
114{
115 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
116
117 writel(value, &pmc->usb);
118}
Wenyou Yangc0b868c2016-02-02 12:46:12 +0800119
120void at91_pllicpr_init(u32 icpr)
121{
122 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
123
124 writel(icpr, &pmc->pllicpr);
125}
Stefan Roese256c2ff2019-04-03 07:37:40 +0200126
127/* Called by macro WATCHDOG_RESET */
128void watchdog_reset(void)
129{
130 static ulong next_reset;
131 ulong now;
132
133 if (!watchdog_dev)
134 return;
135
136 now = get_timer(0);
137
138 /* Do not reset the watchdog too often */
139 if (now > next_reset) {
140 next_reset = now + 1000; /* reset every 1000ms */
141 wdt_reset(watchdog_dev);
142 }
143}
144
145int arch_early_init_r(void)
146{
147 struct at91_wdt_priv *priv;
148
149 /* Init watchdog */
150 if (uclass_get_device_by_seq(UCLASS_WDT, 0, &watchdog_dev)) {
151 debug("Watchdog: Not found by seq!\n");
152 if (uclass_get_device(UCLASS_WDT, 0, &watchdog_dev)) {
153 puts("Watchdog: Not found!\n");
154 return 0;
155 }
156 }
157
158 priv = dev_get_priv(watchdog_dev);
159 if (!priv) {
160 printf("Watchdog: priv not available!\n");
161 return 0;
162 }
163
164 wdt_start(watchdog_dev, priv->timeout * 1000, 0);
165 printf("Watchdog: Started\n");
166
167 return 0;
168}