USB: DM: Convert i.MX5 ehci code to driver model
[oweals/u-boot.git] / arch / arm / mach-at91 / clock.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Atmel Corporation
4  *                    Wenyou Yang <wenyou.yang@atmel.com>
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <wdt.h>
10 #include <asm/io.h>
11 #include <asm/arch/hardware.h>
12 #include <asm/arch/at91_pmc.h>
13 #include <asm/arch/at91_wdt.h>
14
15 #define EN_UPLL_TIMEOUT         500
16
17 static struct udevice *watchdog_dev __attribute__((section(".data"))) = NULL;
18
19 void 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
42 void 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
60 void 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
67 void 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 }
73
74 int 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
95 int 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
113 void 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 }
119
120 void at91_pllicpr_init(u32 icpr)
121 {
122         struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
123
124         writel(icpr, &pmc->pllicpr);
125 }
126
127 /* Called by macro WATCHDOG_RESET */
128 void 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
145 int 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 }