1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Atmel Corporation
4 * Wenyou Yang <wenyou.yang@atmel.com>
11 #include <asm/arch/hardware.h>
12 #include <asm/arch/at91_pmc.h>
13 #include <asm/arch/at91_wdt.h>
15 #define EN_UPLL_TIMEOUT 500
17 static struct udevice *watchdog_dev __attribute__((section(".data"))) = NULL;
19 void at91_periph_clk_enable(int id)
21 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
27 if (id > AT91_PMC_PCR_PID_MASK)
30 writel(id, &pmc->pcr);
32 div_value = readl(&pmc->pcr) & AT91_PMC_PCR_DIV;
34 regval = AT91_PMC_PCR_EN | AT91_PMC_PCR_CMD_WRITE | id | div_value;
36 writel(regval, &pmc->pcr);
38 writel(0x01 << id, &pmc->pcer);
42 void at91_periph_clk_disable(int id)
44 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
49 if (id > AT91_PMC_PCR_PID_MASK)
52 regval = AT91_PMC_PCR_CMD_WRITE | id;
54 writel(regval, &pmc->pcr);
56 writel(0x01 << id, &pmc->pcdr);
60 void at91_system_clk_enable(int sys_clk)
62 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
64 writel(sys_clk, &pmc->scer);
67 void at91_system_clk_disable(int sys_clk)
69 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
71 writel(sys_clk, &pmc->scdr);
74 int at91_upll_clk_enable(void)
76 struct at91_pmc *pmc = (at91_pmc_t *)ATMEL_BASE_PMC;
77 ulong start_time, tmp_time;
79 if ((readl(&pmc->uckr) & AT91_PMC_UPLLEN) == AT91_PMC_UPLLEN)
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");
95 int at91_upll_clk_disable(void)
97 struct at91_pmc *pmc = (at91_pmc_t *)ATMEL_BASE_PMC;
98 ulong start_time, tmp_time;
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");
113 void at91_usb_clk_init(u32 value)
115 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
117 writel(value, &pmc->usb);
120 void at91_pllicpr_init(u32 icpr)
122 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
124 writel(icpr, &pmc->pllicpr);
127 /* Called by macro WATCHDOG_RESET */
128 void watchdog_reset(void)
130 static ulong next_reset;
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);
145 int arch_early_init_r(void)
147 struct at91_wdt_priv *priv;
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");
158 priv = dev_get_priv(watchdog_dev);
160 printf("Watchdog: priv not available!\n");
164 wdt_start(watchdog_dev, priv->timeout * 1000, 0);
165 printf("Watchdog: Started\n");