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